// A fake world for the agents tests (module, XRPC service, web page): the user's PDS (records, // describeRepo, chat.atirc.* behind atproto-proxy), two bidders with compute offerings (hat-7 bids free, hat-9 // refuses the rfp) whose receipt and reports land after one miss each, and the guest's ssh. No network. import { createHash } from "node:crypto"; export const T = "com.publicdomainrelay.temp"; export const HAT7 = "did:plc:7q5lnitxv4meizdon5fmhzrq"; export const HAT9 = "did:plc:4vcig7s43z3nyqrnepmcfzqt"; export const ATIRC = "did:web:irc.fedfork.com#atirc"; export const LAUNCH = "com.fedfork.agents.launch"; const ENDPOINT7 = "https://hat7.example"; const ENDPOINT9 = "https://hat9.example"; // A bidder as the requester reaches it: its DID's market service, at its offering's endpoints. const PEER7 = `${HAT7}#pdr_temp_market`; const OFFERINGS = { [HAT7]: ENDPOINT7, [HAT9]: ENDPOINT9 }; const hex = (key) => createHash("sha256").update(key).digest("hex").slice(0, 32); const report = (collection, prefix, key) => `at://${HAT7}/${T}.compute.events.${collection}/${prefix}-payload-${hex(key)}`; // respond(call) -> { ok: true, output } | { ok: false, error }; calls: every call seen; repo: the user's records. export function world({ did, handle = "bob.test", shellOutput = "agent unit active\natlibs-agents: started\n", token = "eyJhbGciOi.tok", mint = true } = {}) { const repo = new Map(); const calls = []; const misses = new Map(); let n = 0; let released = false; const later = (key, value) => { const seen = misses.get(key) ?? 0; misses.set(key, seen + 1); return seen === 0 ? { ok: false, error: "RecordNotFound" } : { ok: true, output: value }; }; const keys = [{ type: "ssh-ed25519", publicKey: "ssh-ed25519 AAAA", fingerprint: "SHA256:x" }]; function answer(call) { const { nsid, params = {}, input = {} } = call; switch (nsid) { case "com.fedfork.exec.attest": return { ok: true, output: { key: "did:key:zQ3sh", ...(input.record && { record: { ...input.record, signatures: [{ $type: "network.attested.signature", issuer: did }] } }) } }; case "com.atproto.repo.describeRepo": return { ok: true, output: { did, handle } }; case "com.atproto.repo.createRecord": case "com.atproto.repo.putRecord": { const rkey = input.rkey ?? `3mzzaaaaaa${(++n).toString(36).padStart(3, "a")}`; const uri = `at://${input.repo}/${input.collection}/${rkey}`; repo.set(uri, { uri, cid: `bafy${rkey}`, value: input.record }); if (input.collection === `${T}.market.event`) released = true; return { ok: true, output: { uri, cid: `bafy${rkey}` } }; } case "com.atproto.repo.deleteRecord": { const uri = `at://${input.repo}/${input.collection}/${input.rkey}`; repo.delete(uri); return { ok: true, output: {} }; } case `${T}.market.submitRfp`: return call.service === PEER7 ? { ok: true, output: { ok: true, name: "bid-7" } } : { ok: false, error: "submitRfp failed with 503" }; case `${T}.market.submitEvent`: released = true; return call.service === PEER7 ? { ok: true, output: { ok: true } } : { ok: false, error: "submitEvent failed with 404" }; case `${T}.market.submitAccept`: return { ok: true, output: { ok: true, id: "accept-1" } }; case "chat.atirc.mint": if (call.proxy !== ATIRC) return { ok: false, error: "mint must go through the PDS to atirc" }; if (!mint) return { ok: false, error: "403 Forbidden subhandle must be below bob.test" }; return { ok: true, output: { token, subhandle: input.subhandle, nick: input.subhandle.split(".")[0], handle } }; case "chat.atirc.revoke": if (call.proxy !== ATIRC) return { ok: false, error: "revoke must go through the PDS to atirc" }; return { ok: true, output: { subhandle: input.subhandle, revokedAt: 1 } }; case "com.fedfork.exec.iroh.connect": return { ok: true, output: { output: shellOutput, exitCode: 0 } }; case "com.fedfork.exec.webrtc.connect": return { ok: true, output: { tag: input.tag, fingerprint: "sha-256 29:D7", output: shellOutput } }; case "com.atproto.repo.getRecord": break; default: return { ok: false, error: `unexpected ${nsid}` }; } const uri = `at://${params.repo}/${params.collection}/${params.rkey}`; if (params.repo === did) return repo.has(uri) ? { ok: true, output: repo.get(uri) } : { ok: false, error: "RecordNotFound" }; if (params.collection === `${T}.market.offering` && params.rkey === "self" && OFFERINGS[params.repo]) { return { ok: true, output: { uri, cid: "bafyoffer", value: { appliesTo: [`${T}.compute.vm`], endpoints: [{ kind: "https", address: OFFERINGS[params.repo] }] } } }; } const find = (collection) => [...repo.values()].find((r) => r.uri.includes(`/${T}.${collection}/`)); const rfp = find("market.rfp"); if (uri === `at://${HAT7}/${T}.market.bid/bid-7`) { return later(uri, { uri, cid: "bafybid", value: { rfp: { uri: rfp?.uri }, payload: { $type: "com.atproto.repo.strongRef", uri: `at://${HAT7}/${T}.market.bids.free/x` }, submitAccept: ENDPOINT7 } }); } const accept = find("market.accept"); const receipt = accept && `at://${HAT7}/${T}.market.receipt/receipt-${hex(`${accept.uri}\n${accept.cid}`)}`; if (uri === receipt) return later(uri, { uri, cid: "bafyrc", value: { accept: { uri: accept.uri } } }); const reports = receipt && { [report("vm.onNetwork", "onnetwork", `onNetwork:${receipt}`)]: { address: "iroh://endpointaabm6" }, [report("vm.onNetwork", "onnetwork", `onNetwork:ssh:${receipt}`)]: { address: "sha-256 29:D7", kind: "webrtc", tag: "ssh" }, [report("vm.hostKeys", "hostkeys", `hostKeys:${receipt}`)]: { keys }, [report("vm.terminated", "termination", `terminated:${receipt}`)]: { reason: "deleted_on_request" }, }; if (reports?.[uri]) { if (uri.includes("vm.terminated") && !released) return { ok: false, error: "RecordNotFound" }; return later(uri, { uri, cid: "c", value: reports[uri] }); } return { ok: false, error: "RecordNotFound" }; } const respond = (call) => { calls.push(call); return answer(call); }; // The run-module host `call` shape: throws for ok: false. const call = async (nsid, request) => { const out = respond({ nsid, ...request }); if (!out.ok) throw new Error(out.error); return out.output; }; return { calls, repo, respond, call }; }