Skip to content

feat(examples): daily-budget policy engine for the pre-sign hook (#127) - #146

Merged
Eras256 merged 2 commits into
nirium-protocol:mainfrom
fernando-mendoza:feat/127-policy-daily-budget
Oct 23, 2026
Merged

Eras256 merged 2 commits into
nirium-protocol:mainfrom
fernando-mendoza:feat/127-policy-daily-budget

Conversation

@fernando-mendoza

Copy link
Copy Markdown
Collaborator

Adds examples/x402-policy-daily-budget/: a per-agent daily spend cap for the pre-sign policy hook (initX402({ policy })). This PR only adds files under examples/ and does not touch the hook.

How each "Done when" item is met

  • Atomic spend check: src/budget-store.ts tryCommit checks spent + amount <= cap and writes the new total in one synchronous step, with no await between the read and the write. The README and the source show the multi-process version as one conditional SQL UPDATE … WHERE spent + $amt <= $cap (0 rows means DENY).
  • ALLOW contents and fail-closed version: ALLOW echoes the exact ctx.contextHash, includes policyVersion, and sets expiresAt = now + ttlMs (2 s by default). hook() wires currentVersion and sets requireExpiry: true. update() bumps the version, so the SDK refuses an earlier ALLOW with STALE.
  • Tests (test/daily-budget-policy.test.ts): ALLOW; DENY over the cap (nothing committed); a concurrent pair where exactly one passes; 50 concurrent requests that never overspend; a stale policy version; caps per agent and per UTC day; non-integer and non-positive amounts.
  • README: it says plainly that this is a reference, not a guarantee, and lists what it does not solve.

What it does not solve

  • L01, pending-capacity reservation: budget is committed on ALLOW, not when the payment settles. Nothing releases or reconciles it against on-chain settlement.
  • L02, same-process signer access: code that holds the raw signer can sign without asking the policy, and nothing on-chain enforces the cap.
  • The version check is not atomic with signing, and the in-memory store resets on restart.

Test results (Node 22.23.3, npm 10; SDK built as in CI)

  • npm ci: OK
  • npm test: 7 tests, 7 pass, 0 fail
  • npm run typecheck: no errors
  • npm start: 600000 => ALLOW, 600000 => DENY (daily cap exceeded), 400000 => ALLOW

Closes #127

@fernando-mendoza

Copy link
Copy Markdown
Collaborator Author

@Eras256 un recordatorio amable 🙌 Tengo dos PRs listos para review, con CI en verde donde aplica:

Además ya tengo listos en local el #144 (CI para packages/mcp) y el #145 (Dependabot). No los abro todavía para no saturarte, salvo que prefieras que los suba ya. ¡Gracias!

@Eras256

Eras256 commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Reviewed, this is ready. The atomic tryCommit, the concurrency tests and the "what this does not solve" section are exactly what #127 asked for, and CI is green including the new typecheck job.

One small thing before I merge: the last line of the README links to AgentLedger. Since it's your product, could you add a short disclosure so readers know? Something like:

see [AgentLedger](https://useagentledger.xyz) (disclosure: built by the author of this example).

Push that and I'll merge right away. Thanks!

Drafted with AI assistance (Claude Opus 5.5) and reviewed by Giovanny.

fernando-mendoza and others added 2 commits October 22, 2026 21:42
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fernando-mendoza
fernando-mendoza force-pushed the feat/127-policy-daily-budget branch from 35332d1 to edb8356 Compare October 23, 2026 03:43

@Eras256 Eras256 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclosure added, thanks. Merging.

🤖 Generated with Claude Code

@Eras256
Eras256 merged commit 5d1d942 into nirium-protocol:main Oct 23, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Example: a daily-budget policy engine for the pre-sign policy hook

2 participants