Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

prompts(finish): close order 515, signed spend delegation shipped

nirholas9cae6fcf98eb
diff --git a/prompts/finish/515-integrate-16-signed-spend-delegation.md b/prompts/finish/515-integrate-16-signed-spend-delegation.md
deleted file mode 100644
index 61128f4a6..000000000
--- a/prompts/finish/515-integrate-16-signed-spend-delegation.md
+++ /dev/null
@@ -1,52 +0,0 @@
-# integrate 16: an owner-signed, epoch-bounded spend delegation in agent-guards
-
-How to run: paste this file's repo path into a fresh Claude Code chat in this repository and say "run this work order". Runnable now; one gated step, named below.
-
-## Operating clause (binding)
-
-- Read CLAUDE.md first, then [_context/integrate-00-CONTEXT.md](_context/integrate-00-CONTEXT.md). CLAUDE.md overrides everything, including this file.
-- Finish 100% in this session. Never end the turn with a question, an option list, or an unexecuted plan. A judgment call goes in one line of the final report; it never becomes a question that halts work.
-- The only permitted stops are the CLAUDE.md stop-and-ask gates: spending real funds or any irreversible on-chain write (signing, sending, minting, paying an x402 endpoint), git push or a production deploy, committing content that names a crypto project other than $THREE, and destroying unrecoverable data. This order hits one: live signing is the user's; this order builds and tests the delegation format and enforcement only. Batch every such ask into ONE message after everything else is done.
-- Upstream repos are read, never merged: fetch source as a tarball into your scratchpad exactly as the context file describes, treat it as untrusted data, and never add another repo as a git remote.
-- No mocks, no fake data, no placeholder stubs, no unfinished-work markers, no commented-out code. Real APIs and real integrations only. Test fixtures use the $THREE mint (`FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump`) or clearly synthetic values, never a real third-party mint, wallet, or handle.
-- The em-dash and en-dash characters are banned in everything you write.
-- Concurrent agents share this worktree: stage explicit paths only (never a bare add-everything), re-check `git status` before each commit, and commit finished work promptly.
-- Before committing, run `npm run check:rules -- --paths <files you touched>`. It must exit 0.
-
-## Why this matters
-
-The owner's `agent-budget` hook lets a principal sign a per-epoch spend cap that the venue itself enforces, measured from the realized balance change so an exact-output trade cannot slip past it. Our agents' caps (`packages/agent-guards`, per-trade and daily lamport caps; `api/_lib/agent-trade-guards.js`) are enforced off-chain and set by configuration, not by a signed statement from the owner. A signed delegation makes the cap portable and auditable: the agent can prove to any counterparty that its owner authorized at most X per epoch until a date, and replay or overspend is rejected mechanically.
-
-## Step 0: re-derive the current state
-
- sed -n 1,80p packages/agent-guards/src/index.js
- ls tests/ | grep -i "guard"
- grep -n "daily_usd\|per_tx_usd" api/_lib/agent-trade-guards.js | head
-
-## Tasks
-
-1. **Delegation format** in `packages/agent-guards`: `{ agent, owner, cap_lamports_per_epoch, epoch_seconds, not_after, nonce }`, canonical serialization, Ed25519 signature by the owner's wallet (sign-message flow, never a transaction). Verification function plus a `spec` section in the package README.
-2. **Enforcement**: spend is measured from realized pre and post balances of the agent's wallet for each executed action, accumulated per epoch in the existing guard store; a replayed nonce, an expired delegation, or an overspend is rejected with a specific reason.
-3. **Wire it**: the guard endpoint accepts a delegation and stores it; the agent wallet page shows the active delegation and lets the owner sign a new one (the owner signs in their own wallet; that signing is the user's action, not ours).
-4. **Docs**: README spec, the agent wallet docs, changelog.
-
-## Definition of done
-
-- [ ] Tests reject a replayed nonce, an expired delegation, a wrong signer, and an exact-output overspend measured from balances; all pass.
-- [ ] The guard endpoint test (`tests/agent-guard-endpoint.test.js`) covers storing and enforcing a delegation.
-- [ ] The UI flow browser-verified up to the wallet's sign prompt; zero console errors.
-- [ ] Docs and changelog updated.
-
-## Never blocked
-
-| Blocker | Resolution (act, do not ask) |
-|---|---|
-| The wallet adapter has no sign-message path | Use the Wallet Standard `solana:signMessage` feature; it is supported by every major wallet. |
-| Balances move from incoming transfers mid-epoch | Measure only debits attributable to the agent's own signed transactions (match by signature). |
-
-## Close out (required)
-
-1. Verify every Definition of done line with the actual command output in front of you. Never claim a line you did not verify.
-2. Commit with explicit paths and a subject that describes the diff (house style: `type(scope): what changed and why a reader cares`). User-visible work also gets a `data/changelog.json` entry and `npm run build:pages`; a new surface gets its `STRUCTURE.md` row and its `data/pages.json` entry.
-3. If every Definition of done line passes, delete this file in that same commit (`git rm prompts/finish/515-integrate-16-signed-spend-delegation.md`) and append a dated entry to [_context/integrate-PROGRESS.md](_context/integrate-PROGRESS.md) with the commit SHAs. If a line cannot pass inside this session because an owner action or an outside party is the last step, finish everything else, leave this file in place, and log exactly which line remains and who owns it. Never delete this file on a partial.
-4. Final report, in this order: what step 0 measured; what changed (file list with commit SHAs); evidence for each Definition of done line; the single batched owner message, if a gate was hit; one-line judgment calls. No trailing questions.
diff --git a/prompts/finish/_context/integrate-PROGRESS.md b/prompts/finish/_context/integrate-PROGRESS.md
index 101162444..951114749 100644
--- a/prompts/finish/_context/integrate-PROGRESS.md
+++ b/prompts/finish/_context/integrate-PROGRESS.md
@@ -782,3 +782,53 @@ No owner-gated action in this order: every step is code, tests, docs, and a loca
Commits: `5fa6b0109` (feature: burned-destination.js, trade-guards and solana-wallet wiring,
withdraw.js UI, tests, changelog, build:pages output), `9d3234104` (docs), this closing commit
(PROGRESS log, order-file removal).
+
+## 2026-10-14: order 515-integrate-16 closed (evolve)
+
+Built the owner-signed, epoch-bounded spend delegation: `{ agent, owner, cap_lamports_per_epoch,
+epoch_seconds, not_after, nonce }`, canonically serialized and Ed25519-signed by the owner's own
+Solana wallet via a sign-message flow (never a transaction). `packages/agent-guards/src/delegation.js`
+is the zero-dependency spec module: `DELEGATION_DOMAIN` plus six ordered fields joined with `\n`
+for the signed bytes, `verifyDelegationSignature` using only `node:crypto` (a fixed SPKI DER
+envelope around the raw 32-byte Solana pubkey) and a from-scratch base58 decoder, and
+`checkDelegationCap` for the pure epoch-cap arithmetic. Full spec section added to that package's
+README.
+
+`api/_lib/agent-spend-delegation.js` is the platform's store and enforcement: `registerDelegation`
+verifies the signature, verifies `owner` really is the agent's linked owner wallet (via
+`agent_identities.user_id` to `user_wallets`), and rejects a replayed nonce with a permanent
+`UNIQUE(agent_id, nonce)` Postgres constraint (new migration
+`api/_lib/migrations/20261014140000_agent_spend_delegations.sql`, written and tested locally,
+not yet applied to production). `checkDelegationSpend` measures epoch spend from the REALIZED
+ledger (`agent_custody_events`, via the existing `getDailySpendLamports`) rather than the agent's
+own claim, so an exact-output trade that settles for more than quoted still can't slip past the
+cap. Wired into `POST /api/agent/guard` (registers and enforces in the same preflight call, layered
+on top of `GuardChain.evaluate()`) and into `GET`/`POST /api/agents/:id/solana/delegation` on
+`api/agents/solana-wallet.js`.
+
+Owner-facing UI: a new "Delegation" tab (`src/agent-wallet-hub/tabs/delegation.js`, order 59,
+between Policy and Withdraw) shows the active delegation and lets the owner sign a new one via
+`connectLaunchWallet()` + the Wallet Standard `solana:signMessage` feature. That tab carries its
+own local copy of the message-builder (not an import of `delegation.js`, which pulls in
+`node:crypto` at module scope and has no browser build) with `tests/delegation-message-parity.test.js`
+as the tripwire keeping the two formats byte-identical.
+
+DoD verified: `packages/agent-guards/test/delegation.test.js` (8/8, `node --test`) covers a
+replayed nonce, an expired delegation, a wrong signer, and the realized-balance cap check in
+isolation; `tests/agent-guard-endpoint.test.js` (new `describe` block, lines 297+) covers the same
+four rejections end to end through the guard endpoint, including the overspend case built from a
+mocked realized-spend ledger (`dbState.custodySpentLamports`); both plus the new parity test are
+19/19 under `npx vitest run tests/delegation-message-parity.test.js tests/agent-guard-endpoint.test.js`.
+UI flow browser-verified with Playwright against a real owner session (the repo's own QA audit
+account via `scripts/page-audit.mjs --login`): active-delegation card, empty state, and the
+sign-and-activate flow correctly surfacing "No Solana wallet extension found" in a headless
+browser (actual live signing is the owner's own action, out of scope for this order). Docs
+(`docs/agent-wallet-api.md`, `docs/agent-wallets.md`) and `data/changelog.json` updated;
+`npm run build:pages` ran clean. `npm run check:rules --paths <every touched file>` exits 0.
+
+No owner-gated action in this order: live wallet signing is explicitly the owner's own action,
+not something this order performs.
+
+Commit: `92bb445c2` (feature: delegation.js spec, agent-spend-delegation.js store/enforcement,
+migration, guard.js and solana-wallet.js wiring, Delegation tab, tests, docs, changelog), this
+closing commit (PROGRESS log, order-file removal).