-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-your-agent.ts
More file actions
418 lines (361 loc) · 16.9 KB
/
Copy pathdeploy-your-agent.ts
File metadata and controls
418 lines (361 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* deploy-your-agent.ts — Deploy an Autark agent in one command.
*
* Usage:
* npx tsx deploy-your-agent.ts [--name "my-agent"] [--selftest] [--verbose]
*
* --selftest Fund a throwaway consumer from your wallet, propose a job,
* let the agent work it autonomously, then assert scoreCompleted 0→1.
* --name Display name embedded in endpointUrl (e.g. "my-agent").
* --verbose Print all logs including RPC noise and AutarkAgent internals.
*
* Keypair is persisted at .agent/agent-keypair.json (gitignored, stable identity).
* RPC from SOLANA_RPC_URL env — Helius devnet recommended for reliability.
*/
import "dotenv/config";
import * as fs from "fs";
import * as path from "path";
import crypto from "crypto";
import {
Connection,
Keypair,
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import {
getOrCreateAssociatedTokenAccount,
getAssociatedTokenAddressSync,
transfer as splTransfer,
} from "@solana/spl-token";
import { EventParser } from "@anchor-lang/core";
import {
AutarkAgent,
AutarkClient,
fetchAgent,
agentPda,
jobOfferPda,
} from "./sdk/src";
import type { JobOfferData } from "./sdk/src";
// ── Config ─────────────────────────────────────────────────────────────────────
const CONFIG_PATH = path.join(__dirname, "devnet.config.json");
const KEYPAIR_PATH = path.join(__dirname, ".agent", "agent-keypair.json");
const RPC_URL = process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com";
const PROGRAM_ID = new PublicKey("FgkicN5V1fYLFJaY6nH9er3vvCr1nJCQVA9Wy7e3kLhy");
const MIN_SOL = 0.05;
const MIN_USDC = 20;
// ── Args ───────────────────────────────────────────────────────────────────────
const _args = process.argv.slice(2);
const SELFTEST = _args.includes("--selftest");
const VERBOSE = _args.includes("--verbose");
const _ni = _args.indexOf("--name");
const AGENT_NAME = _ni >= 0 && _args[_ni + 1] ? _args[_ni + 1] : "autark-agent";
// ── Noise suppression ─────────────────────────────────────────────────────────
const _log = console.log.bind(console);
const _warn = console.warn.bind(console);
const _error = console.error.bind(console);
const _isNoise = (m: string) =>
m.startsWith("[AutarkAgent]") ||
m.includes("429") ||
m.includes("Too Many Requests") ||
m.includes("Retrying after") ||
m.startsWith("ws error:") ||
m.startsWith("Server responded with");
console.log = (...a) => { if (!VERBOSE && _isNoise(String(a[0] ?? ""))) return; _log(...a); };
console.warn = (...a) => { if (!VERBOSE && _isNoise(String(a[0] ?? ""))) return; _warn(...a); };
console.error = (...a) => { if (!VERBOSE && _isNoise(String(a[0] ?? ""))) return; _error(...a); };
// ── Helpers ────────────────────────────────────────────────────────────────────
function sleep(ms: number): Promise<void> {
return new Promise(r => setTimeout(r, ms));
}
async function withRetry<T>(label: string, fn: () => Promise<T>, tries = 6): Promise<T> {
for (let i = 0; i < tries; i++) {
try { return await fn(); }
catch (e: any) {
if (i === tries - 1) throw e;
const msg = String(e?.message ?? e);
const transient =
msg.includes("Blockhash not found") ||
msg.includes("block height exceeded") ||
msg.includes("timeout") || msg.includes("429") || msg.includes("503");
if (!transient) throw e;
await sleep(2_000 * (i + 1));
}
}
throw new Error("unreachable");
}
function loadOrCreateKeypair(filepath: string): Keypair {
if (fs.existsSync(filepath)) {
return Keypair.fromSecretKey(
Buffer.from(JSON.parse(fs.readFileSync(filepath, "utf-8")))
);
}
const kp = Keypair.generate();
fs.mkdirSync(path.dirname(filepath), { recursive: true });
fs.writeFileSync(filepath, JSON.stringify(Array.from(kp.secretKey)));
_log(`New keypair generated and saved to ${filepath}`);
return kp;
}
async function getUsdcBalance(conn: Connection, owner: PublicKey, mint: PublicKey): Promise<number> {
try {
const ata = getAssociatedTokenAddressSync(mint, owner, false);
const bal = await conn.getTokenAccountBalance(ata);
return bal.value.uiAmount ?? 0;
} catch {
return 0;
}
}
// ── onJob ──────────────────────────────────────────────────────────────────────
// Uses sdk/src/intelligence/analyze.ts for on-chain signals + role-locked LLM.
// Works in both modes:
// - With ANTHROPIC_API_KEY: fetches real on-chain signals, calls Claude, returns
// a structured token risk verdict in character.
// - Without ANTHROPIC_API_KEY: returns a deterministic in-character stub in the
// same format — never out-of-character meta-commentary.
//
// Job target convention: call registerJobRequest(jobPubkey.toBase58(), target)
// before proposing the job. If not set, defaults to the payment mint address.
// See agents/research-agent.ts for the full convention docs.
import { analyze } from "./sdk/src/intelligence/analyze";
async function onJob(
job: JobOfferData
): Promise<{ deliver: true; result: string } | { decline: true }> {
const amountUsdc = (job.amount / 1_000_000).toFixed(2);
// Target defaults to the payment mint. In a real consumer integration,
// register the actual token to analyze before calling proposeJob.
const target = job.mint.toBase58();
try {
const { Connection } = await import("@solana/web3.js");
const conn = new Connection(
process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com",
"confirmed"
);
const verdict = await analyze(target, conn, process.env.ANTHROPIC_API_KEY);
_log(`[${AGENT_NAME}] Delivered verdict (${verdict.length} chars):\n${verdict}`);
return { deliver: true, result: verdict };
} catch (e: any) {
_error(`[${AGENT_NAME}] onJob error: ${e?.message ?? e}`);
return { decline: true };
}
}
// ── Simple event poller (selftest only) ────────────────────────────────────────
async function waitForEvent(
conn: Connection,
parser: EventParser,
eventName: string,
filterFn: (data: any) => boolean,
seen: Set<string>,
timeoutMs: number
): Promise<{ data: any; signature: string; slot: number }> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
let sigs: Awaited<ReturnType<typeof conn.getSignaturesForAddress>>;
try {
sigs = await conn.getSignaturesForAddress(PROGRAM_ID, { limit: 30 });
} catch { await sleep(2_000); continue; }
for (const sigInfo of [...sigs].reverse()) {
if (sigInfo.err || seen.has(sigInfo.signature)) continue;
seen.add(sigInfo.signature);
let tx: any = null;
try {
tx = await conn.getTransaction(sigInfo.signature, {
maxSupportedTransactionVersion: 0,
commitment: "confirmed",
});
} catch { continue; }
if (!tx?.meta?.logMessages) continue;
try {
for (const ev of parser.parseLogs(tx.meta.logMessages, false)) {
if (ev.name === eventName && filterFn(ev.data)) {
return { data: ev.data, signature: sigInfo.signature, slot: tx.slot };
}
}
} catch { /* malformed logs */ }
}
await sleep(2_000);
}
throw new Error(`Timeout (${timeoutMs / 1000}s) waiting for ${eventName}`);
}
// ── Selftest ───────────────────────────────────────────────────────────────────
async function runSelftest(
agentKp: Keypair,
agentClient: AutarkClient,
testMint: PublicKey
): Promise<void> {
const conn = agentClient.connection;
const meStr = agentKp.publicKey.toBase58();
_log("\n════════════════════════════════════════════════════");
_log(" SELF-TEST: autonomous hire → deliver → pay loop");
_log("════════════════════════════════════════════════════\n");
// Wait two poll cycles so the agent can settle any stale SettlementPending
// jobs left over from previous runs before we snapshot scoreBefore.
_log("Waiting for agent warm-up (2 poll cycles)…");
await sleep(12_000);
// Read scoreCompleted before — now clean of any prior-run leftovers
const agentAddr = agentPda(agentKp.publicKey);
const before = await fetchAgent(agentClient.program, agentAddr);
const scoreBefore = before.scoreCompleted;
_log(`Agent scoreCompleted before: ${scoreBefore}`);
// 1. Warm up seen set (captures current chain state)
const seen = new Set<string>();
const preSigs = await conn.getSignaturesForAddress(PROGRAM_ID, { limit: 50 });
for (const s of preSigs) seen.add(s.signature);
_log(`Event seen-set warmed: ${seen.size} signatures\n`);
// 2. Create throwaway consumer
const consumerKp = Keypair.generate();
_log(`Consumer (throwaway): ${consumerKp.publicKey.toBase58()}`);
// 3. Fund consumer: 0.05 SOL for fees
_log("Sending 0.05 SOL to consumer…");
{
const tx = new Transaction().add(
SystemProgram.transfer({
fromPubkey: agentKp.publicKey,
toPubkey: consumerKp.publicKey,
lamports: Math.floor(0.05 * LAMPORTS_PER_SOL),
})
);
const sig = await withRetry("sol-to-consumer", () =>
conn.sendTransaction(tx, [agentKp])
);
await conn.confirmTransaction(sig, "confirmed");
_log(` ✓ SOL funded (${sig.slice(0, 20)}…)`);
}
// 4. Fund consumer: 10 USDC (from agent ATA)
_log("Sending 10 USDC to consumer…");
{
const consumerAta = await withRetry("consumer-ata", () =>
getOrCreateAssociatedTokenAccount(conn, agentKp, testMint, consumerKp.publicKey)
);
const agentAta = getAssociatedTokenAddressSync(testMint, agentKp.publicKey, false);
const sig = await withRetry("usdc-to-consumer", () =>
splTransfer(conn, agentKp, agentAta, consumerAta.address, agentKp, 10_000_000)
);
_log(` ✓ USDC funded (${sig.slice(0, 20)}…)`);
}
// 5. Consumer proposes 5 USDC job (5s challenge + defense window for fast selftest)
_log("\nProposing 5 USDC job to agent…");
const jobId = Array.from(crypto.randomBytes(32));
const consumerClient = AutarkClient.fromKeypair(consumerKp, RPC_URL);
const now = Math.floor(Date.now() / 1000);
const jobSig = await withRetry("proposeJob", () =>
consumerClient.ix.proposeJob({
jobId: Uint8Array.from(jobId),
provider: agentKp.publicKey,
amount: 5,
acceptanceDeadline: now + 120,
deliveryDeadline: now + 600,
challengeWindowSeconds: 8,
defenseWindowSeconds: 8,
mint: testMint,
}).rpc()
);
_log(` ✓ Job proposed (${jobSig.slice(0, 20)}…)`);
_log("\nWaiting for autonomous agent execution…\n");
// Compute the job PDA for precise event filtering
const jobPda = jobOfferPda(consumerKp.publicKey, Uint8Array.from(jobId));
const jobPdaStr = jobPda.toBase58();
// Build parser for event watching
const parser = new EventParser(PROGRAM_ID, agentClient.program.coder);
const byProvider = (d: any) => d.provider?.toBase58() === meStr;
const byJob = (d: any) => d.job?.toBase58() === jobPdaStr;
// 6. Watch events in sequence
_log(" → jobAccepted …");
const acc = await waitForEvent(conn, parser, "jobAccepted", byProvider, seen, 90_000);
_log(` ✓ jobAccepted slot=${acc.slot} tx=${acc.signature.slice(0, 20)}…`);
_log(" → settlementPendingEvent …");
const pend = await waitForEvent(conn, parser, "settlementPendingEvent", byProvider, seen, 60_000);
_log(` ✓ settlementPending slot=${pend.slot} tx=${pend.signature.slice(0, 20)}…`);
_log(" → jobSettled (waiting up to 90s for challenge window + crank) …");
// Filter by THIS job's PDA so concurrent settlements from prior runs don't confuse the assertion.
const settled = await waitForEvent(conn, parser, "jobSettled", byJob, seen, 90_000);
_log(` ✓ jobSettled slot=${settled.slot} tx=${settled.signature.slice(0, 20)}…`);
// 7. Verify scoreCompleted 0→N+1 via on-chain read
const after = await fetchAgent(agentClient.program, agentAddr);
const scoreAfter = after.scoreCompleted;
_log(`\nAgent scoreCompleted: ${scoreBefore} → ${scoreAfter}`);
if (scoreAfter <= scoreBefore) {
throw new Error(
`Assertion FAIL: scoreCompleted did not increase (${scoreBefore} → ${scoreAfter})`
);
}
const agentUsdc = await getUsdcBalance(conn, agentKp.publicKey, testMint);
_log(`Agent USDC balance: ${agentUsdc.toFixed(2)}`);
_log(`\n✓ ASSERTION PASS — scoreCompleted incremented (${scoreBefore} → ${scoreAfter})\n`);
_log("════════════════════════════════════════════════════");
_log(" Self-test COMPLETE. Agent is hired, paid, and live.");
_log("════════════════════════════════════════════════════\n");
}
// ── Main ───────────────────────────────────────────────────────────────────────
async function main() {
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8"));
const testMint = new PublicKey(config.testMint);
const agentKp = loadOrCreateKeypair(KEYPAIR_PATH);
const conn = new Connection(RPC_URL, "confirmed");
_log("\n╔══════════════════════════════════════════╗");
_log("║ Autark Agent Deployer ║");
_log("╚══════════════════════════════════════════╝");
_log(` Wallet : ${agentKp.publicKey.toBase58()}`);
_log(` RPC : ${RPC_URL}`);
_log(` Mint : ${testMint.toBase58()}\n`);
// Best-effort devnet SOL airdrop
const solBefore = (await conn.getBalance(agentKp.publicKey)) / LAMPORTS_PER_SOL;
if (solBefore < MIN_SOL) {
_log("Attempting devnet SOL airdrop (best-effort)…");
try {
const sig = await conn.requestAirdrop(agentKp.publicKey, LAMPORTS_PER_SOL);
await conn.confirmTransaction(sig, "confirmed");
_log(" ✓ Airdrop OK.\n");
} catch {
_log(" Airdrop failed (normal on public devnet).\n");
}
}
// Funding check
const sol = (await conn.getBalance(agentKp.publicKey)) / LAMPORTS_PER_SOL;
const usdc = await getUsdcBalance(conn, agentKp.publicKey, testMint);
if (sol < MIN_SOL || usdc < MIN_USDC) {
_log(`⚠ Not funded yet.`);
_log(` Address : ${agentKp.publicKey.toBase58()}`);
_log(` Balance : ${sol.toFixed(4)} SOL / ${usdc} USDC`);
_log(` Need : ≥${MIN_SOL} SOL + ≥${MIN_USDC} USDC (test tokens, no real money)`);
_log(`\n → DM @naaku_builds on X with your address to get funded.`);
_log(` Keypair saved at .agent/agent-keypair.json — run again after funding.\n`);
process.exit(0);
}
_log(`Balances OK — SOL: ${sol.toFixed(4)} USDC: ${usdc}`);
// Build client (for selftest; AutarkAgent builds its own internally)
const agentClient = AutarkClient.fromKeypair(agentKp, RPC_URL);
// Start agent
const agent = new AutarkAgent({
keypair: agentKp,
capabilities: ["token-research"],
endpointUrl: `autark:${AGENT_NAME}`,
mint: testMint,
minStake: MIN_USDC,
pollIntervalMs: SELFTEST ? 5_000 : 10_000,
onJob,
});
await agent.start();
_log(`\n✓ Agent LIVE and listening`);
_log(` Name : ${AGENT_NAME}`);
_log(` Pubkey : ${agentKp.publicKey.toBase58()}\n`);
if (!SELFTEST) {
_log("Press Ctrl+C to stop.\n");
return; // keep process alive — setInterval inside AutarkAgent holds the event loop
}
// --selftest mode
try {
await sleep(2_000); // let agent poll loop initialize
await runSelftest(agentKp, agentClient, testMint);
} catch (e: any) {
_error("✗ Self-test failed:", e?.message ?? e);
agent.stop();
process.exit(1);
}
agent.stop();
process.exit(0);
}
main().catch((e: any) => {
_error("✗ Fatal:", e?.message ?? e);
process.exit(1);
});