@codespar/cli 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/package.json +1 -1
- package/templates/bills-agent/README.md +1 -1
- package/templates/bills-agent/agents/bills-agent/AGENTS.md +1 -1
- package/templates/bills-agent/agents/bills-agent/CLAUDE.md +1 -1
- package/templates/bills-agent/agents/bills-agent/README.md +6 -5
- package/templates/bills-agent/agents/bills-agent/src/modules/embedded-consent.ts +5 -4
- package/templates/bills-agent/packages/agent-core/README.md +14 -1
- package/templates/bills-agent/packages/agent-core/package.json +1 -1
- package/templates/bills-agent/packages/agent-core/src/agent.ts +39 -1
- package/templates/bills-agent/packages/agent-core/src/api/charge-rail.ts +54 -34
- package/templates/bills-agent/packages/agent-core/src/api/client.ts +28 -5
- package/templates/bills-agent/packages/agent-core/src/api/mandate-status.ts +28 -12
- package/templates/bills-agent/packages/agent-core/src/api/rail.ts +62 -26
- package/templates/bills-agent/packages/agent-core/src/api/sandbox-payer.ts +3 -18
- package/templates/bills-agent/packages/agent-core/src/approval.ts +14 -2
- package/templates/bills-agent/packages/agent-core/src/bundle.ts +35 -3
- package/templates/bills-agent/packages/agent-core/src/channels.ts +49 -3
- package/templates/bills-agent/packages/agent-core/src/check.ts +25 -2
- package/templates/bills-agent/packages/agent-core/src/engine.ts +235 -46
- package/templates/bills-agent/packages/agent-core/src/hash.ts +57 -1
- package/templates/bills-agent/packages/agent-core/src/index.ts +2 -0
- package/templates/bills-agent/packages/agent-core/src/quote.ts +50 -0
- package/templates/bills-agent/packages/agent-core/src/rail.ts +89 -4
- package/templates/bills-agent/packages/agent-core/src/receipt-chain.ts +270 -0
- package/templates/bills-agent/packages/agent-core/src/receipt-verification.ts +194 -18
- package/templates/bills-agent/packages/agent-core/src/revocation.ts +1 -1
- package/templates/bills-agent/packages/agent-core/src/state-machine.ts +10 -1
- package/templates/bills-agent/packages/agent-core/src/stubs/mandate-status.ts +4 -3
- package/templates/bills-agent/packages/agent-core/src/stubs/rail.ts +100 -32
- package/templates/bills-agent/packages/agent-core/src/tools.ts +17 -6
- package/templates/bills-agent/packages/agent-core/src/types.ts +54 -1
- package/templates/bills-agent/packages/agent-core/test/approval-seal.test.ts +233 -0
- package/templates/bills-agent/packages/agent-core/test/attempt-replay.test.ts +265 -0
- package/templates/bills-agent/packages/agent-core/test/composition.test.ts +204 -0
- package/templates/bills-agent/packages/agent-core/test/fixtures/production-receipt-keys.json +1 -0
- package/templates/bills-agent/packages/agent-core/test/helpers.ts +4 -1
- package/templates/bills-agent/packages/agent-core/test/mandate-status.test.ts +85 -9
- package/templates/bills-agent/packages/agent-core/test/quote.test.ts +198 -0
- package/templates/bills-agent/packages/agent-core/test/receipt-chain.test.ts +220 -0
- package/templates/bills-agent/packages/agent-core/test/receipt-read-verification.test.ts +197 -0
- package/templates/bills-agent/packages/agent-core/test/receivables.test.ts +103 -3
- package/templates/bills-agent/packages/agent-runtime/README.md +12 -0
- package/templates/bills-agent/packages/agent-runtime/src/adversarial.ts +32 -2
- package/templates/bills-agent/packages/agent-runtime/src/channels/types.ts +53 -4
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +123 -22
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +30 -20
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +146 -21
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +40 -3
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +8 -1
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +16 -2
- package/templates/bills-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +70 -20
- package/templates/bills-agent/packages/agent-runtime/src/commands/poll.ts +2 -1
- package/templates/bills-agent/packages/agent-runtime/src/commands/resume.ts +8 -2
- package/templates/bills-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +3 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/start.ts +6 -3
- package/templates/bills-agent/packages/agent-runtime/src/commands/verify.ts +239 -38
- package/templates/bills-agent/packages/agent-runtime/src/commands/webhook.ts +3 -2
- package/templates/bills-agent/packages/agent-runtime/src/index.ts +1 -0
- package/templates/bills-agent/packages/agent-runtime/src/inspect.ts +5 -1
- package/templates/bills-agent/packages/agent-runtime/src/kit.ts +25 -2
- package/templates/bills-agent/packages/agent-runtime/src/scenarios.ts +8 -2
- package/templates/bills-agent/packages/agent-runtime/src/setup.ts +13 -3
- package/templates/bills-agent/packages/agent-runtime/src/terminal.ts +127 -18
- package/templates/bills-agent/packages/agent-runtime/test/verify-cli.test.ts +211 -9
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +45 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-session.test.ts +32 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-status-replies.test.ts +182 -0
- package/templates/checkout-agent/LICENSE +21 -0
- package/templates/checkout-agent/README.md +28 -0
- package/templates/checkout-agent/_gitignore +10 -0
- package/templates/checkout-agent/agents/checkout-agent/.env.example +5 -0
- package/templates/checkout-agent/agents/checkout-agent/AGENTS.md +20 -0
- package/templates/checkout-agent/agents/checkout-agent/CLAUDE.md +20 -0
- package/templates/checkout-agent/agents/checkout-agent/README.md +94 -0
- package/templates/checkout-agent/agents/checkout-agent/SYSTEM_PROMPT.md +36 -0
- package/templates/checkout-agent/agents/checkout-agent/agent.yaml +30 -0
- package/templates/checkout-agent/agents/checkout-agent/channels/whatsapp/README.md +28 -0
- package/templates/checkout-agent/agents/checkout-agent/channels/whatsapp/pedido-beatriz.json +11 -0
- package/templates/checkout-agent/agents/checkout-agent/channels/whatsapp/pedido-marina-retomada.json +10 -0
- package/templates/checkout-agent/agents/checkout-agent/channels/whatsapp/pedido-marina.json +11 -0
- package/templates/checkout-agent/agents/checkout-agent/channels/whatsapp/templates.json +49 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/beneficiary-swap-human.json +12 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/beneficiary-swap.json +11 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/beneficiary-swap.transcript.jsonl +5 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/coupon-unknown.json +13 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/coupon-unknown.transcript.jsonl +4 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/exfiltration.json +26 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/exfiltration.transcript.jsonl +3 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/false-authority.json +11 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/false-authority.transcript.jsonl +5 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/fractioning.json +12 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/fractioning.transcript.jsonl +5 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/model-total.json +13 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/model-total.transcript.jsonl +5 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/payment-claimed.json +15 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/payment-claimed.transcript.jsonl +7 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/price-injected.json +12 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/price-injected.transcript.jsonl +5 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/prompt-injection.json +13 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/prompt-injection.transcript.jsonl +4 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/quantity-swapped-human.json +16 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/quantity-swapped-human.transcript.jsonl +9 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/quantity-swapped.json +15 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/quantity-swapped.transcript.jsonl +8 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/adversarial/webhook-replay.json +11 -0
- package/templates/checkout-agent/agents/checkout-agent/evals/eval.yaml +11 -0
- package/templates/checkout-agent/agents/checkout-agent/guardrails.json +25 -0
- package/templates/checkout-agent/agents/checkout-agent/mandate.example.json +28 -0
- package/templates/checkout-agent/agents/checkout-agent/package.json +33 -0
- package/templates/checkout-agent/agents/checkout-agent/runbook.md +19 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/beneficiary-not-allowed.json +42 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/beneficiary-not-allowed.transcript.jsonl +4 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/cap-exceeded.json +32 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/cap-exceeded.transcript.jsonl +4 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/cart-recomposed.json +61 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/cart-recomposed.transcript.jsonl +9 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/cart-replaced.json +61 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/cart-replaced.transcript.jsonl +9 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/charge-expired.json +36 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/charge-expired.transcript.jsonl +6 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/coupon-unknown.json +23 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/coupon-unknown.transcript.jsonl +4 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/escalated-above-threshold.json +72 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/escalated-above-threshold.transcript.jsonl +9 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/happy-path.json +15 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/happy-path.transcript.jsonl +6 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/item-unavailable.json +23 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/item-unavailable.transcript.jsonl +3 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/mandate-revoked.json +42 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/mandate-revoked.transcript.jsonl +12 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/nfse-failed.json +14 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/nfse-failed.transcript.jsonl +9 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/payment-claimed.json +34 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/payment-claimed.transcript.jsonl +7 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/price-injected.json +33 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/price-injected.transcript.jsonl +4 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/prompt-injection.json +32 -0
- package/templates/checkout-agent/agents/checkout-agent/scenarios/prompt-injection.transcript.jsonl +4 -0
- package/templates/checkout-agent/agents/checkout-agent/src/catalog.ts +92 -0
- package/templates/checkout-agent/agents/checkout-agent/src/kit.ts +338 -0
- package/templates/checkout-agent/agents/checkout-agent/src/modules/bolepix-receivables.ts +179 -0
- package/templates/checkout-agent/agents/checkout-agent/src/modules/nfse-invoice.ts +374 -0
- package/templates/checkout-agent/agents/checkout-agent/src/modules/storefront-cart.ts +220 -0
- package/templates/checkout-agent/agents/checkout-agent/src/pricing.ts +297 -0
- package/templates/checkout-agent/agents/checkout-agent/test/adversarial.test.ts +117 -0
- package/templates/checkout-agent/agents/checkout-agent/test/check.test.ts +67 -0
- package/templates/checkout-agent/agents/checkout-agent/test/cli.test.ts +169 -0
- package/templates/checkout-agent/agents/checkout-agent/test/fixtures/order-marina.transcript.jsonl +5 -0
- package/templates/checkout-agent/agents/checkout-agent/test/fixtures/ordered-marina-awaiting-payer.transcript.jsonl +6 -0
- package/templates/checkout-agent/agents/checkout-agent/test/fixtures/tap-falar-agora-marina.transcript.jsonl +2 -0
- package/templates/checkout-agent/agents/checkout-agent/test/nfse.test.ts +170 -0
- package/templates/checkout-agent/agents/checkout-agent/test/pricing.test.ts +138 -0
- package/templates/checkout-agent/agents/checkout-agent/test/scenarios.test.ts +113 -0
- package/templates/checkout-agent/agents/checkout-agent/test/whatsapp.test.ts +171 -0
- package/templates/checkout-agent/agents/checkout-agent/tools.json +68 -0
- package/templates/checkout-agent/agents/checkout-agent/tsconfig.json +10 -0
- package/templates/checkout-agent/package.json +38 -0
- package/templates/checkout-agent/packages/agent-core/README.md +34 -0
- package/templates/checkout-agent/packages/agent-core/package.json +24 -0
- package/templates/checkout-agent/packages/agent-core/src/agent.ts +163 -0
- package/templates/checkout-agent/packages/agent-core/src/api/charge-rail.ts +195 -0
- package/templates/checkout-agent/packages/agent-core/src/api/client.ts +76 -0
- package/templates/checkout-agent/packages/agent-core/src/api/mandate-status.ts +74 -0
- package/templates/checkout-agent/packages/agent-core/src/api/rail.ts +153 -0
- package/templates/checkout-agent/packages/agent-core/src/api/sandbox-payer.ts +43 -0
- package/templates/checkout-agent/packages/agent-core/src/approval.ts +135 -0
- package/templates/checkout-agent/packages/agent-core/src/bundle.ts +191 -0
- package/templates/checkout-agent/packages/agent-core/src/channels.ts +186 -0
- package/templates/checkout-agent/packages/agent-core/src/check.ts +276 -0
- package/templates/checkout-agent/packages/agent-core/src/clock.ts +42 -0
- package/templates/checkout-agent/packages/agent-core/src/engine.ts +1074 -0
- package/templates/checkout-agent/packages/agent-core/src/escalate.ts +85 -0
- package/templates/checkout-agent/packages/agent-core/src/events.ts +40 -0
- package/templates/checkout-agent/packages/agent-core/src/guardrails.ts +45 -0
- package/templates/checkout-agent/packages/agent-core/src/hash.ts +118 -0
- package/templates/checkout-agent/packages/agent-core/src/ids.ts +10 -0
- package/templates/checkout-agent/packages/agent-core/src/index.ts +34 -0
- package/templates/checkout-agent/packages/agent-core/src/mandate.ts +100 -0
- package/templates/checkout-agent/packages/agent-core/src/manifest.ts +76 -0
- package/templates/checkout-agent/packages/agent-core/src/providers/anthropic.ts +76 -0
- package/templates/checkout-agent/packages/agent-core/src/providers/replay.ts +63 -0
- package/templates/checkout-agent/packages/agent-core/src/providers/types.ts +39 -0
- package/templates/checkout-agent/packages/agent-core/src/quote.ts +50 -0
- package/templates/checkout-agent/packages/agent-core/src/rail.ts +186 -0
- package/templates/checkout-agent/packages/agent-core/src/receipt-chain.ts +270 -0
- package/templates/checkout-agent/packages/agent-core/src/receipt-verification.ts +577 -0
- package/templates/checkout-agent/packages/agent-core/src/revocation.ts +30 -0
- package/templates/checkout-agent/packages/agent-core/src/secrets.ts +30 -0
- package/templates/checkout-agent/packages/agent-core/src/state/store.ts +307 -0
- package/templates/checkout-agent/packages/agent-core/src/state-machine.ts +157 -0
- package/templates/checkout-agent/packages/agent-core/src/stubs/charge-rail.ts +195 -0
- package/templates/checkout-agent/packages/agent-core/src/stubs/mandate-status.ts +58 -0
- package/templates/checkout-agent/packages/agent-core/src/stubs/rail.ts +209 -0
- package/templates/checkout-agent/packages/agent-core/src/tools.ts +66 -0
- package/templates/checkout-agent/packages/agent-core/src/types.ts +187 -0
- package/templates/checkout-agent/packages/agent-core/test/approval-seal.test.ts +233 -0
- package/templates/checkout-agent/packages/agent-core/test/approval.test.ts +117 -0
- package/templates/checkout-agent/packages/agent-core/test/attempt-replay.test.ts +265 -0
- package/templates/checkout-agent/packages/agent-core/test/bundle.test.ts +85 -0
- package/templates/checkout-agent/packages/agent-core/test/clock.test.ts +25 -0
- package/templates/checkout-agent/packages/agent-core/test/composition.test.ts +204 -0
- package/templates/checkout-agent/packages/agent-core/test/engine.test.ts +596 -0
- package/templates/checkout-agent/packages/agent-core/test/escalate.test.ts +42 -0
- package/templates/checkout-agent/packages/agent-core/test/fixtures/production-receipt-keys.json +1 -0
- package/templates/checkout-agent/packages/agent-core/test/fixtures/staging-receipt-keys.json +18 -0
- package/templates/checkout-agent/packages/agent-core/test/fixtures/staging-receipt.json +24 -0
- package/templates/checkout-agent/packages/agent-core/test/helpers.ts +142 -0
- package/templates/checkout-agent/packages/agent-core/test/mandate-status.test.ts +229 -0
- package/templates/checkout-agent/packages/agent-core/test/manifest.test.ts +45 -0
- package/templates/checkout-agent/packages/agent-core/test/quote.test.ts +198 -0
- package/templates/checkout-agent/packages/agent-core/test/receipt-chain.test.ts +220 -0
- package/templates/checkout-agent/packages/agent-core/test/receipt-read-verification.test.ts +197 -0
- package/templates/checkout-agent/packages/agent-core/test/receipt-verification.test.ts +316 -0
- package/templates/checkout-agent/packages/agent-core/test/receivables.test.ts +416 -0
- package/templates/checkout-agent/packages/agent-core/test/secrets.test.ts +28 -0
- package/templates/checkout-agent/packages/agent-core/test/state-machine.test.ts +67 -0
- package/templates/checkout-agent/packages/agent-core/test/transitions.type-test.ts +59 -0
- package/templates/checkout-agent/packages/agent-core/tsconfig.json +10 -0
- package/templates/checkout-agent/packages/agent-runtime/README.md +80 -0
- package/templates/checkout-agent/packages/agent-runtime/bin.mjs +32 -0
- package/templates/checkout-agent/packages/agent-runtime/package.json +23 -0
- package/templates/checkout-agent/packages/agent-runtime/src/adversarial.ts +199 -0
- package/templates/checkout-agent/packages/agent-runtime/src/agent.ts +68 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/contact.ts +13 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/index.ts +72 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/rules.ts +120 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/types.ts +173 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +459 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +216 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/evidence.ts +109 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +350 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +180 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/present.ts +47 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +120 -0
- package/templates/checkout-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +127 -0
- package/templates/checkout-agent/packages/agent-runtime/src/cli.ts +98 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/check.ts +15 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/decide.ts +63 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/eval.ts +58 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/inspect.ts +100 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +367 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/poll.ts +145 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/reconcile.ts +42 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/rerun.ts +86 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/resume.ts +49 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +134 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/start.ts +248 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/verify.ts +330 -0
- package/templates/checkout-agent/packages/agent-runtime/src/commands/webhook.ts +40 -0
- package/templates/checkout-agent/packages/agent-runtime/src/default-kit.ts +83 -0
- package/templates/checkout-agent/packages/agent-runtime/src/index.ts +13 -0
- package/templates/checkout-agent/packages/agent-runtime/src/inspect.ts +801 -0
- package/templates/checkout-agent/packages/agent-runtime/src/kit.ts +229 -0
- package/templates/checkout-agent/packages/agent-runtime/src/poll.ts +105 -0
- package/templates/checkout-agent/packages/agent-runtime/src/scenarios.ts +274 -0
- package/templates/checkout-agent/packages/agent-runtime/src/setup.ts +239 -0
- package/templates/checkout-agent/packages/agent-runtime/src/terminal.ts +263 -0
- package/templates/checkout-agent/packages/agent-runtime/src/webhook.ts +131 -0
- package/templates/checkout-agent/packages/agent-runtime/test/inspect.test.ts +336 -0
- package/templates/checkout-agent/packages/agent-runtime/test/verify-cli.test.ts +329 -0
- package/templates/checkout-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +277 -0
- package/templates/checkout-agent/packages/agent-runtime/test/whatsapp-evidence.test.ts +94 -0
- package/templates/checkout-agent/packages/agent-runtime/test/whatsapp-rules.test.ts +174 -0
- package/templates/checkout-agent/packages/agent-runtime/test/whatsapp-session.test.ts +167 -0
- package/templates/checkout-agent/packages/agent-runtime/test/whatsapp-status-replies.test.ts +182 -0
- package/templates/checkout-agent/packages/agent-runtime/tsconfig.json +9 -0
- package/templates/checkout-agent/scripts/whatsapp-emulator.mjs +84 -0
- package/templates/checkout-agent/tsconfig.base.json +16 -0
- package/templates/checkout-agent/vitest.config.ts +12 -0
- package/templates/collections-agent/README.md +6 -1
- package/templates/collections-agent/agents/collections-agent/README.md +12 -8
- package/templates/collections-agent/agents/collections-agent/channels/whatsapp/README.md +8 -0
- package/templates/collections-agent/agents/collections-agent/channels/whatsapp/acordo-1042-retomada.json +10 -0
- package/templates/collections-agent/agents/collections-agent/channels/whatsapp/templates.json +27 -1
- package/templates/collections-agent/agents/collections-agent/src/kit.ts +2 -0
- package/templates/collections-agent/agents/collections-agent/src/modules/bolepix-receivables.ts +12 -2
- package/templates/collections-agent/agents/collections-agent/test/ambiguous-charge.test.ts +55 -0
- package/templates/collections-agent/agents/collections-agent/test/fixtures/tap-emitir-nova-1042.transcript.jsonl +2 -0
- package/templates/collections-agent/agents/collections-agent/test/whatsapp-poll.test.ts +12 -6
- package/templates/collections-agent/agents/collections-agent/test/whatsapp-status-replies.test.ts +148 -0
- package/templates/collections-agent/agents/collections-agent/test/whatsapp.test.ts +5 -0
- package/templates/collections-agent/package.json +1 -0
- package/templates/collections-agent/packages/agent-core/README.md +14 -1
- package/templates/collections-agent/packages/agent-core/package.json +1 -1
- package/templates/collections-agent/packages/agent-core/src/agent.ts +39 -1
- package/templates/collections-agent/packages/agent-core/src/api/charge-rail.ts +54 -34
- package/templates/collections-agent/packages/agent-core/src/api/client.ts +28 -5
- package/templates/collections-agent/packages/agent-core/src/api/mandate-status.ts +28 -12
- package/templates/collections-agent/packages/agent-core/src/api/rail.ts +62 -26
- package/templates/collections-agent/packages/agent-core/src/api/sandbox-payer.ts +3 -18
- package/templates/collections-agent/packages/agent-core/src/approval.ts +14 -2
- package/templates/collections-agent/packages/agent-core/src/bundle.ts +35 -3
- package/templates/collections-agent/packages/agent-core/src/channels.ts +49 -3
- package/templates/collections-agent/packages/agent-core/src/check.ts +25 -2
- package/templates/collections-agent/packages/agent-core/src/engine.ts +235 -46
- package/templates/collections-agent/packages/agent-core/src/hash.ts +57 -1
- package/templates/collections-agent/packages/agent-core/src/index.ts +2 -0
- package/templates/collections-agent/packages/agent-core/src/quote.ts +50 -0
- package/templates/collections-agent/packages/agent-core/src/rail.ts +89 -4
- package/templates/collections-agent/packages/agent-core/src/receipt-chain.ts +270 -0
- package/templates/collections-agent/packages/agent-core/src/receipt-verification.ts +194 -18
- package/templates/collections-agent/packages/agent-core/src/revocation.ts +1 -1
- package/templates/collections-agent/packages/agent-core/src/state-machine.ts +10 -1
- package/templates/collections-agent/packages/agent-core/src/stubs/mandate-status.ts +4 -3
- package/templates/collections-agent/packages/agent-core/src/stubs/rail.ts +100 -32
- package/templates/collections-agent/packages/agent-core/src/tools.ts +17 -6
- package/templates/collections-agent/packages/agent-core/src/types.ts +54 -1
- package/templates/collections-agent/packages/agent-core/test/approval-seal.test.ts +233 -0
- package/templates/collections-agent/packages/agent-core/test/attempt-replay.test.ts +265 -0
- package/templates/collections-agent/packages/agent-core/test/composition.test.ts +204 -0
- package/templates/collections-agent/packages/agent-core/test/fixtures/production-receipt-keys.json +1 -0
- package/templates/collections-agent/packages/agent-core/test/helpers.ts +4 -1
- package/templates/collections-agent/packages/agent-core/test/mandate-status.test.ts +85 -9
- package/templates/collections-agent/packages/agent-core/test/quote.test.ts +198 -0
- package/templates/collections-agent/packages/agent-core/test/receipt-chain.test.ts +220 -0
- package/templates/collections-agent/packages/agent-core/test/receipt-read-verification.test.ts +197 -0
- package/templates/collections-agent/packages/agent-core/test/receivables.test.ts +103 -3
- package/templates/collections-agent/packages/agent-runtime/README.md +12 -0
- package/templates/collections-agent/packages/agent-runtime/src/adversarial.ts +32 -2
- package/templates/collections-agent/packages/agent-runtime/src/channels/types.ts +53 -4
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +123 -22
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +30 -20
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +146 -21
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +40 -3
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +8 -1
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +16 -2
- package/templates/collections-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +70 -20
- package/templates/collections-agent/packages/agent-runtime/src/commands/poll.ts +2 -1
- package/templates/collections-agent/packages/agent-runtime/src/commands/resume.ts +8 -2
- package/templates/collections-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +3 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/start.ts +6 -3
- package/templates/collections-agent/packages/agent-runtime/src/commands/verify.ts +239 -38
- package/templates/collections-agent/packages/agent-runtime/src/commands/webhook.ts +3 -2
- package/templates/collections-agent/packages/agent-runtime/src/index.ts +1 -0
- package/templates/collections-agent/packages/agent-runtime/src/inspect.ts +5 -1
- package/templates/collections-agent/packages/agent-runtime/src/kit.ts +25 -2
- package/templates/collections-agent/packages/agent-runtime/src/scenarios.ts +8 -2
- package/templates/collections-agent/packages/agent-runtime/src/setup.ts +13 -3
- package/templates/collections-agent/packages/agent-runtime/src/terminal.ts +127 -18
- package/templates/collections-agent/packages/agent-runtime/test/verify-cli.test.ts +211 -9
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +45 -0
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-session.test.ts +32 -0
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-status-replies.test.ts +182 -0
- package/templates/collections-agent/scripts/whatsapp-emulator.mjs +84 -0
- package/templates/hello-agent/README.md +1 -1
- package/templates/hello-agent/packages/agent-core/README.md +14 -1
- package/templates/hello-agent/packages/agent-core/package.json +1 -1
- package/templates/hello-agent/packages/agent-core/src/agent.ts +39 -1
- package/templates/hello-agent/packages/agent-core/src/api/charge-rail.ts +54 -34
- package/templates/hello-agent/packages/agent-core/src/api/client.ts +28 -5
- package/templates/hello-agent/packages/agent-core/src/api/mandate-status.ts +28 -12
- package/templates/hello-agent/packages/agent-core/src/api/rail.ts +62 -26
- package/templates/hello-agent/packages/agent-core/src/api/sandbox-payer.ts +3 -18
- package/templates/hello-agent/packages/agent-core/src/approval.ts +14 -2
- package/templates/hello-agent/packages/agent-core/src/bundle.ts +35 -3
- package/templates/hello-agent/packages/agent-core/src/channels.ts +49 -3
- package/templates/hello-agent/packages/agent-core/src/check.ts +25 -2
- package/templates/hello-agent/packages/agent-core/src/engine.ts +235 -46
- package/templates/hello-agent/packages/agent-core/src/hash.ts +57 -1
- package/templates/hello-agent/packages/agent-core/src/index.ts +2 -0
- package/templates/hello-agent/packages/agent-core/src/quote.ts +50 -0
- package/templates/hello-agent/packages/agent-core/src/rail.ts +89 -4
- package/templates/hello-agent/packages/agent-core/src/receipt-chain.ts +270 -0
- package/templates/hello-agent/packages/agent-core/src/receipt-verification.ts +194 -18
- package/templates/hello-agent/packages/agent-core/src/revocation.ts +1 -1
- package/templates/hello-agent/packages/agent-core/src/state-machine.ts +10 -1
- package/templates/hello-agent/packages/agent-core/src/stubs/mandate-status.ts +4 -3
- package/templates/hello-agent/packages/agent-core/src/stubs/rail.ts +100 -32
- package/templates/hello-agent/packages/agent-core/src/tools.ts +17 -6
- package/templates/hello-agent/packages/agent-core/src/types.ts +54 -1
- package/templates/hello-agent/packages/agent-core/test/approval-seal.test.ts +233 -0
- package/templates/hello-agent/packages/agent-core/test/attempt-replay.test.ts +265 -0
- package/templates/hello-agent/packages/agent-core/test/composition.test.ts +204 -0
- package/templates/hello-agent/packages/agent-core/test/fixtures/production-receipt-keys.json +1 -0
- package/templates/hello-agent/packages/agent-core/test/helpers.ts +4 -1
- package/templates/hello-agent/packages/agent-core/test/mandate-status.test.ts +85 -9
- package/templates/hello-agent/packages/agent-core/test/quote.test.ts +198 -0
- package/templates/hello-agent/packages/agent-core/test/receipt-chain.test.ts +220 -0
- package/templates/hello-agent/packages/agent-core/test/receipt-read-verification.test.ts +197 -0
- package/templates/hello-agent/packages/agent-core/test/receivables.test.ts +103 -3
- package/templates/hello-agent/packages/agent-runtime/README.md +12 -0
- package/templates/hello-agent/packages/agent-runtime/src/adversarial.ts +32 -2
- package/templates/hello-agent/packages/agent-runtime/src/channels/types.ts +53 -4
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +123 -22
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +30 -20
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +146 -21
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +40 -3
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +8 -1
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +16 -2
- package/templates/hello-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +70 -20
- package/templates/hello-agent/packages/agent-runtime/src/commands/poll.ts +2 -1
- package/templates/hello-agent/packages/agent-runtime/src/commands/resume.ts +8 -2
- package/templates/hello-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +3 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/start.ts +6 -3
- package/templates/hello-agent/packages/agent-runtime/src/commands/verify.ts +239 -38
- package/templates/hello-agent/packages/agent-runtime/src/commands/webhook.ts +3 -2
- package/templates/hello-agent/packages/agent-runtime/src/index.ts +1 -0
- package/templates/hello-agent/packages/agent-runtime/src/inspect.ts +5 -1
- package/templates/hello-agent/packages/agent-runtime/src/kit.ts +25 -2
- package/templates/hello-agent/packages/agent-runtime/src/scenarios.ts +8 -2
- package/templates/hello-agent/packages/agent-runtime/src/setup.ts +13 -3
- package/templates/hello-agent/packages/agent-runtime/src/terminal.ts +127 -18
- package/templates/hello-agent/packages/agent-runtime/test/verify-cli.test.ts +211 -9
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +45 -0
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-session.test.ts +32 -0
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-status-replies.test.ts +182 -0
- package/templates/kits.lock.json +29 -6
- package/templates/supplier-payments-agent/README.md +1 -1
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/AGENTS.md +1 -1
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/CLAUDE.md +1 -1
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/README.md +11 -9
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/src/kit.ts +1 -1
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/src/modules/batch-payout.ts +67 -20
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/test/batch.test.ts +504 -5
- package/templates/supplier-payments-agent/packages/agent-core/README.md +14 -1
- package/templates/supplier-payments-agent/packages/agent-core/package.json +1 -1
- package/templates/supplier-payments-agent/packages/agent-core/src/agent.ts +39 -1
- package/templates/supplier-payments-agent/packages/agent-core/src/api/charge-rail.ts +54 -34
- package/templates/supplier-payments-agent/packages/agent-core/src/api/client.ts +28 -5
- package/templates/supplier-payments-agent/packages/agent-core/src/api/mandate-status.ts +28 -12
- package/templates/supplier-payments-agent/packages/agent-core/src/api/rail.ts +62 -26
- package/templates/supplier-payments-agent/packages/agent-core/src/api/sandbox-payer.ts +3 -18
- package/templates/supplier-payments-agent/packages/agent-core/src/approval.ts +14 -2
- package/templates/supplier-payments-agent/packages/agent-core/src/bundle.ts +35 -3
- package/templates/supplier-payments-agent/packages/agent-core/src/channels.ts +49 -3
- package/templates/supplier-payments-agent/packages/agent-core/src/check.ts +25 -2
- package/templates/supplier-payments-agent/packages/agent-core/src/engine.ts +235 -46
- package/templates/supplier-payments-agent/packages/agent-core/src/hash.ts +57 -1
- package/templates/supplier-payments-agent/packages/agent-core/src/index.ts +2 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/quote.ts +50 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/rail.ts +89 -4
- package/templates/supplier-payments-agent/packages/agent-core/src/receipt-chain.ts +270 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/receipt-verification.ts +194 -18
- package/templates/supplier-payments-agent/packages/agent-core/src/revocation.ts +1 -1
- package/templates/supplier-payments-agent/packages/agent-core/src/state-machine.ts +10 -1
- package/templates/supplier-payments-agent/packages/agent-core/src/stubs/mandate-status.ts +4 -3
- package/templates/supplier-payments-agent/packages/agent-core/src/stubs/rail.ts +100 -32
- package/templates/supplier-payments-agent/packages/agent-core/src/tools.ts +17 -6
- package/templates/supplier-payments-agent/packages/agent-core/src/types.ts +54 -1
- package/templates/supplier-payments-agent/packages/agent-core/test/approval-seal.test.ts +233 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/attempt-replay.test.ts +265 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/composition.test.ts +204 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/fixtures/production-receipt-keys.json +1 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/helpers.ts +4 -1
- package/templates/supplier-payments-agent/packages/agent-core/test/mandate-status.test.ts +85 -9
- package/templates/supplier-payments-agent/packages/agent-core/test/quote.test.ts +198 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/receipt-chain.test.ts +220 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/receipt-read-verification.test.ts +197 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/receivables.test.ts +103 -3
- package/templates/supplier-payments-agent/packages/agent-runtime/README.md +12 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/adversarial.ts +32 -2
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/types.ts +53 -4
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +123 -22
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +30 -20
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +146 -21
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +40 -3
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +8 -1
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +16 -2
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +70 -20
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/poll.ts +2 -1
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/resume.ts +8 -2
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +3 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/start.ts +6 -3
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/verify.ts +239 -38
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/webhook.ts +3 -2
- package/templates/supplier-payments-agent/packages/agent-runtime/src/index.ts +1 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/inspect.ts +5 -1
- package/templates/supplier-payments-agent/packages/agent-runtime/src/kit.ts +25 -2
- package/templates/supplier-payments-agent/packages/agent-runtime/src/scenarios.ts +8 -2
- package/templates/supplier-payments-agent/packages/agent-runtime/src/setup.ts +13 -3
- package/templates/supplier-payments-agent/packages/agent-runtime/src/terminal.ts +127 -18
- package/templates/supplier-payments-agent/packages/agent-runtime/test/verify-cli.test.ts +211 -9
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +45 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-session.test.ts +32 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-status-replies.test.ts +182 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +0 -204
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +0 -204
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +0 -204
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +0 -204
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a repeated `attempt_id` answers on the deployed API (ent#1671,
|
|
3
|
+
* codespar-enterprise #1683), and what the kit reads each answer as. The
|
|
4
|
+
* CodeSpar rail is driven through the real SDK client against a mocked HTTP
|
|
5
|
+
* server, so the error envelope is parsed the way it is in production; the
|
|
6
|
+
* stub rail is held to the same table, because a stub that answered a repeat
|
|
7
|
+
* more kindly than the API would let a suite pass that the API fails.
|
|
8
|
+
*/
|
|
9
|
+
import { mkdtempSync } from "node:fs";
|
|
10
|
+
import { createServer, type Server } from "node:http";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
14
|
+
import { createCodeSparClient } from "../src/api/client.js";
|
|
15
|
+
import { CodeSparRail } from "../src/api/rail.js";
|
|
16
|
+
import { batchAttemptId } from "../src/hash.js";
|
|
17
|
+
import { quoteFromApproval } from "../src/quote.js";
|
|
18
|
+
import type { PaymentRail, RailOutcome, RailPayment } from "../src/rail.js";
|
|
19
|
+
import { StubRail } from "../src/stubs/rail.js";
|
|
20
|
+
import { StateStore } from "../src/state/store.js";
|
|
21
|
+
import { ESCOLA, harness, MERCADO } from "./helpers.js";
|
|
22
|
+
|
|
23
|
+
const actor = { type: "agent" as const, agent: "bills-agent@0.1.0", on_behalf_of: "usr_demo" };
|
|
24
|
+
|
|
25
|
+
function payment(over: Partial<RailPayment> = {}): RailPayment {
|
|
26
|
+
return {
|
|
27
|
+
attempt_id: "ska_replay_0",
|
|
28
|
+
mandate_id: "mdt_test_0001",
|
|
29
|
+
amount_minor: 185000,
|
|
30
|
+
currency: "BRL",
|
|
31
|
+
payee: ESCOLA,
|
|
32
|
+
purpose: "contas do mes",
|
|
33
|
+
agent_id: "bills-agent",
|
|
34
|
+
quote: { seller: "Escola Aurora", resource: "outubro", price_minor: 185000, payee: ESCOLA },
|
|
35
|
+
approval: { items_hash: `sha256:${"1".repeat(64)}` },
|
|
36
|
+
actor,
|
|
37
|
+
...over,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type Answer = { status: number; body: unknown };
|
|
42
|
+
|
|
43
|
+
let server: Server;
|
|
44
|
+
let baseUrl: string;
|
|
45
|
+
let answer: Answer;
|
|
46
|
+
let posts = 0;
|
|
47
|
+
|
|
48
|
+
beforeAll(async () => {
|
|
49
|
+
server = createServer((req, res) => {
|
|
50
|
+
if (req.method === "POST") posts += 1;
|
|
51
|
+
res.writeHead(answer.status, { "content-type": "application/json" });
|
|
52
|
+
res.end(JSON.stringify(answer.body));
|
|
53
|
+
});
|
|
54
|
+
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
55
|
+
baseUrl = `http://127.0.0.1:${(server.address() as { port: number }).port}`;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
afterAll(async () => {
|
|
59
|
+
server.closeAllConnections();
|
|
60
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
posts = 0;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
function rail(): CodeSparRail {
|
|
68
|
+
// The placeholder passes the `csk_test_` guard and is the one test-key-shaped string the secret scan allows.
|
|
69
|
+
return new CodeSparRail(createCodeSparClient({ apiKey: "csk_test_your_key_here", baseUrl, timeoutMs: 5_000 }));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function refusal(code: string, details?: Record<string, unknown>): Answer {
|
|
73
|
+
return { status: 409, body: { error: { code, message: `api: ${code}`, ...(details ? { details } : {}) }, request_id: null } };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The ORIGINAL settled body, as the API stores it and answers it again with `idempotent_replay: true`. */
|
|
77
|
+
const settledBody = {
|
|
78
|
+
payment: { transactionId: "tx_original", moneyMoved: false },
|
|
79
|
+
receipt: { id: "rcpt_original" },
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
describe("CodeSparRail reads every answer to a repeated attempt_id (ent#1671)", () => {
|
|
83
|
+
it("settled: the original body with idempotent_replay is the original outcome, same transaction and same receipt, marked replayed", async () => {
|
|
84
|
+
answer = { status: 200, body: { ...settledBody, idempotent_replay: true } };
|
|
85
|
+
for (const outcome of [await rail().pay(payment()), await rail().lookup("ska_replay_0", payment())]) {
|
|
86
|
+
expect(outcome).toMatchObject({ status: "settled", transaction_id: "tx_original", receipt_id: "rcpt_original", replayed: true });
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("the call that settles it is not a replay", async () => {
|
|
91
|
+
answer = { status: 200, body: { ...settledBody, idempotent_replay: false } };
|
|
92
|
+
const outcome = await rail().pay(payment());
|
|
93
|
+
expect(outcome).toMatchObject({ status: "settled" });
|
|
94
|
+
expect("replayed" in outcome).toBe(false);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("psp_attempt_in_flight: never a refusal. pay() reads it as unknown, lookup() as still running", async () => {
|
|
98
|
+
answer = refusal("psp_attempt_in_flight");
|
|
99
|
+
expect(await rail().pay(payment())).toMatchObject({ status: "uncertain", code: "psp_attempt_in_flight" });
|
|
100
|
+
expect(await rail().lookup("ska_replay_0", payment())).toEqual({ status: "in_flight" });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("psp_attempt_uncertain: pinned for reconciliation, on both paths", async () => {
|
|
104
|
+
answer = refusal("psp_attempt_uncertain");
|
|
105
|
+
expect(await rail().pay(payment())).toMatchObject({ status: "uncertain", code: "psp_attempt_uncertain" });
|
|
106
|
+
expect(await rail().lookup("ska_replay_0", payment())).toMatchObject({ status: "uncertain", code: "psp_attempt_uncertain" });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("psp_attempt_conflict: failed with no money, and the id is spent", async () => {
|
|
110
|
+
answer = refusal("psp_attempt_conflict");
|
|
111
|
+
expect(await rail().pay(payment())).toEqual({ status: "failed", code: "psp_attempt_conflict", message: "api: psp_attempt_conflict", spent: true });
|
|
112
|
+
expect(await rail().lookup("ska_replay_0", payment())).toMatchObject({ status: "failed", spent: true });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("attempt_id_conflict and attempt_id_unavailable: a refusal that sent nothing, held for another payment or project, and NEVER spent", async () => {
|
|
116
|
+
for (const [code, details, held] of [
|
|
117
|
+
["attempt_id_conflict", { mismatched_fields: ["quote"], attempt_id: "ska_replay_0" }, "conflict"],
|
|
118
|
+
["attempt_id_unavailable", { attempt_id: "ska_replay_0" }, "unavailable"],
|
|
119
|
+
] as const) {
|
|
120
|
+
answer = refusal(code, details);
|
|
121
|
+
for (const outcome of [await rail().pay(payment()), await rail().lookup("ska_replay_0", payment())]) {
|
|
122
|
+
expect(outcome).toEqual({ status: "failed", code, message: `api: ${code}`, held });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("every answer above is one POST: a lookup is the same presentation, never a second call of its own", async () => {
|
|
128
|
+
answer = refusal("psp_attempt_in_flight");
|
|
129
|
+
await rail().lookup("ska_replay_0", payment());
|
|
130
|
+
expect(posts).toBe(1);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("the stub rail answers a repeat the way the API does", () => {
|
|
135
|
+
function stub(options: ConstructorParameters<typeof StubRail>[1] = {}): StubRail {
|
|
136
|
+
return new StubRail(new StateStore(join(mkdtempSync(join(tmpdir(), "agent-core-replay-")), "state.db")), options);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
it("settled: the original outcome, marked replayed, and nothing paid again", async () => {
|
|
140
|
+
const r = stub();
|
|
141
|
+
const first = await r.pay(payment());
|
|
142
|
+
const again = await r.pay(payment());
|
|
143
|
+
expect(r.payCount).toBe(1);
|
|
144
|
+
expect(again).toMatchObject({ status: "settled", replayed: true, transaction_id: (first as { transaction_id: string }).transaction_id, receipt_id: (first as { receipt_id: string }).receipt_id });
|
|
145
|
+
expect("replayed" in first).toBe(false);
|
|
146
|
+
expect((again as { raw: Record<string, unknown> }).raw["idempotent_replay"]).toBe(true);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("a different payment under the same id is attempt_id_conflict, whatever became of the first, and names what differs", async () => {
|
|
150
|
+
const r = stub();
|
|
151
|
+
await r.pay(payment());
|
|
152
|
+
const changed = await r.pay(payment({ quote: { ...payment().quote!, at: "2026-09-23T18:00:00.000Z" } }));
|
|
153
|
+
expect(changed).toMatchObject({ status: "failed", code: "attempt_id_conflict", held: "conflict" });
|
|
154
|
+
expect((changed as { message: string }).message).toContain("quote");
|
|
155
|
+
expect((changed as { spent?: true }).spent).toBeUndefined();
|
|
156
|
+
const otherPayee = await r.pay(payment({ payee: MERCADO, quote: { ...payment().quote!, payee: MERCADO } }));
|
|
157
|
+
expect((otherPayee as { message: string }).message).toContain("payee");
|
|
158
|
+
expect(r.payCount).toBe(1);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("failed: psp_attempt_conflict, spent", async () => {
|
|
162
|
+
const r = stub({ refusePayees: [ESCOLA] });
|
|
163
|
+
expect(await r.pay(payment())).toMatchObject({ status: "failed", code: "psp_dispatch_failed" });
|
|
164
|
+
expect(await r.pay(payment())).toMatchObject({ status: "failed", code: "psp_attempt_conflict", spent: true });
|
|
165
|
+
expect(r.payCount).toBe(1);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("in flight: psp_attempt_in_flight on another presentation, on the shared books another machine reads too", async () => {
|
|
169
|
+
const ledger = new StateStore(join(mkdtempSync(join(tmpdir(), "agent-core-ledger-")), "rail.db"));
|
|
170
|
+
const a = stub({ ledger, uncertainPayees: [ESCOLA] });
|
|
171
|
+
expect(await a.pay(payment())).toMatchObject({ status: "uncertain", code: "psp_dispatch_uncertain" });
|
|
172
|
+
const b = stub({ ledger });
|
|
173
|
+
expect(await b.pay(payment())).toMatchObject({ status: "uncertain", code: "psp_attempt_in_flight" });
|
|
174
|
+
expect(b.payCount).toBe(0);
|
|
175
|
+
expect(await b.lookup("ska_replay_0", payment())).toEqual({ status: "in_flight" });
|
|
176
|
+
expect(await b.lookup("ska_replay_0", payment())).toMatchObject({ status: "settled" });
|
|
177
|
+
expect(b.payCount).toBe(0);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("a batch line's quote is the same on every run of the list", () => {
|
|
182
|
+
it("stable leaves the approval time out; anything else keeps it", () => {
|
|
183
|
+
const artifact = { approved_at: "2026-09-23T18:00:00.000Z", items: [{ beneficiary: "Escola Aurora", description: "outubro", amount: 185000, payee: ESCOLA }] } as never;
|
|
184
|
+
expect(quoteFromApproval(artifact, 0, "contas do mes")).toEqual({ seller: "Escola Aurora", resource: "outubro", price_minor: 185000, payee: ESCOLA, at: "2026-09-23T18:00:00.000Z" });
|
|
185
|
+
expect(quoteFromApproval(artifact, 0, "contas do mes", { stable: true })).toEqual({ seller: "Escola Aurora", resource: "outubro", price_minor: 185000, payee: ESCOLA });
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* When a batch line moves to the next generation of its id: on
|
|
191
|
+
* `psp_attempt_conflict` — the server stating the attempt failed with no
|
|
192
|
+
* money — and on NOTHING else. Advancing on an unknown outcome is how one line
|
|
193
|
+
* gets paid under two ids; advancing on `attempt_id_conflict` or
|
|
194
|
+
* `attempt_id_unavailable` is minting a new payment for an id somebody else's
|
|
195
|
+
* record holds.
|
|
196
|
+
*/
|
|
197
|
+
describe("a batch line's attempt id advances on a spent id, and on nothing else", () => {
|
|
198
|
+
const BATCH = { ref: "folha-teste", batch_hash: `sha256:${"a".repeat(64)}`, index: 0, count: 1 };
|
|
199
|
+
const approver = { id: "usr_demo", channel: "terminal" };
|
|
200
|
+
|
|
201
|
+
/** A rail that answers `script(n)` for its n-th call, and records every attempt id it was shown. */
|
|
202
|
+
function scripted(script: (call: number) => RailOutcome) {
|
|
203
|
+
const seen: string[] = [];
|
|
204
|
+
const wrap = (): PaymentRail => ({
|
|
205
|
+
name: "stub",
|
|
206
|
+
pay: async (p) => (seen.push(p.attempt_id), script(seen.length - 1)),
|
|
207
|
+
lookup: async () => undefined,
|
|
208
|
+
receipt: async () => undefined,
|
|
209
|
+
});
|
|
210
|
+
return { seen, wrap };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function runLine(rail: ReturnType<typeof scripted>) {
|
|
214
|
+
const h = harness({ wrapRail: rail.wrap });
|
|
215
|
+
const d = await h.engine.draft({ items: [{ payee: "escola", amount: 1000 }], batch: BATCH });
|
|
216
|
+
if (!d.ok) throw new Error(`refused before draft: ${d.reason}`);
|
|
217
|
+
h.engine.approve(d.execution.id, approver);
|
|
218
|
+
return h.engine.execute(d.execution.id);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const held: Array<[string, RailOutcome]> = [
|
|
222
|
+
["attempt_id_conflict", { status: "failed", code: "attempt_id_conflict", message: "used for a different payment", held: "conflict" }],
|
|
223
|
+
["attempt_id_unavailable", { status: "failed", code: "attempt_id_unavailable", message: "not available", held: "unavailable" }],
|
|
224
|
+
["an unknown refusal", { status: "failed", code: "some_code_nobody_documented", message: "?" }],
|
|
225
|
+
["psp_attempt_in_flight", { status: "uncertain", code: "psp_attempt_in_flight", message: "claimed" }],
|
|
226
|
+
["psp_attempt_uncertain", { status: "uncertain", code: "psp_attempt_uncertain", message: "pinned" }],
|
|
227
|
+
["psp_dispatch_uncertain", { status: "uncertain", code: "psp_dispatch_uncertain", message: "timeout" }],
|
|
228
|
+
];
|
|
229
|
+
for (const [name, answer] of held) {
|
|
230
|
+
it(`${name}: one presentation, the generation-0 id, no generation recorded`, async () => {
|
|
231
|
+
const rail = scripted(() => answer);
|
|
232
|
+
const out = await runLine(rail);
|
|
233
|
+
const g0 = batchAttemptId(out.mandate.id, BATCH, 0);
|
|
234
|
+
expect(rail.seen).toEqual([g0]);
|
|
235
|
+
expect(out.attempt_generations).toBeUndefined();
|
|
236
|
+
expect(out.state).toBe(answer.status === "failed" ? "failed" : "executing");
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
it("psp_attempt_conflict: the next generation, derived the same way on any machine, and saved on the execution", async () => {
|
|
241
|
+
const settled: RailOutcome = { status: "settled", transaction_id: "tx_g2", receipt_id: null, money_moved: false, sandbox: true, raw: {} };
|
|
242
|
+
const spent: RailOutcome = { status: "failed", code: "psp_attempt_conflict", message: "failed, no money", spent: true };
|
|
243
|
+
const rail = scripted((call) => (call < 2 ? spent : settled));
|
|
244
|
+
const out = await runLine(rail);
|
|
245
|
+
expect(rail.seen).toEqual([0, 1, 2].map((g) => batchAttemptId(out.mandate.id, BATCH, 0, g)));
|
|
246
|
+
expect(out.state).toBe("settled");
|
|
247
|
+
expect(out.outcomes[0]!.attempt_id).toBe(rail.seen[2]);
|
|
248
|
+
expect(out.attempt_generations).toEqual({ 0: 2 });
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("a psp_attempt_conflict the rail did not mark spent does not advance either", async () => {
|
|
252
|
+
const rail = scripted(() => ({ status: "failed", code: "psp_attempt_conflict", message: "no spent flag" }));
|
|
253
|
+
const out = await runLine(rail);
|
|
254
|
+
expect(rail.seen).toHaveLength(1);
|
|
255
|
+
expect(out.attempt_generations).toBeUndefined();
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("stops after eight spent generations, failed, never looping", async () => {
|
|
259
|
+
const rail = scripted(() => ({ status: "failed", code: "psp_attempt_conflict", message: "failed, no money", spent: true }));
|
|
260
|
+
const out = await runLine(rail);
|
|
261
|
+
expect(rail.seen).toHaveLength(9);
|
|
262
|
+
expect(out.state).toBe("failed");
|
|
263
|
+
expect(out.attempt_generations).toEqual({ 0: 8 });
|
|
264
|
+
});
|
|
265
|
+
});
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
// Deterministic ids, so an artifact is the same bytes on every run and its hash can be pinned.
|
|
4
|
+
vi.mock("../src/ids.js", async (original) => {
|
|
5
|
+
const real = await original<typeof import("../src/ids.js")>();
|
|
6
|
+
let n = 0;
|
|
7
|
+
return { ...real, newId: (prefix: string) => `${prefix}_${String((n += 1)).padStart(16, "0")}` };
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
import { checkApprovalArtifact, createApprovalArtifact, hmacSigner } from "../src/approval.js";
|
|
11
|
+
import { canonicalJson, compositionHash, itemsHash, sha256Hex } from "../src/hash.js";
|
|
12
|
+
import type { Execution } from "../src/state-machine.js";
|
|
13
|
+
import { ToolsFileSchema } from "../src/tools.js";
|
|
14
|
+
import type { CompositionLine, ExecutionComposition, ExecutionItem } from "../src/types.js";
|
|
15
|
+
import { harness } from "./helpers.js";
|
|
16
|
+
|
|
17
|
+
const signer = hmacSigner("local-dev-stub", Buffer.alloc(32, 1));
|
|
18
|
+
const now = new Date("2026-09-23T18:00:00.000Z");
|
|
19
|
+
const person = { type: "person" as const, id: "usr_terminal", channel: "terminal" };
|
|
20
|
+
const human = { type: "human" as const, id: "usr_terminal", channel: "terminal" };
|
|
21
|
+
const approver = { id: "usr_terminal", channel: "terminal" };
|
|
22
|
+
|
|
23
|
+
function execution(items: ExecutionItem[], over: Partial<Execution> = {}): Execution {
|
|
24
|
+
return {
|
|
25
|
+
id: "exe_1",
|
|
26
|
+
run_id: "run_1",
|
|
27
|
+
state: "awaiting_approval",
|
|
28
|
+
mode: "human",
|
|
29
|
+
actor: { type: "agent", agent: "bills-agent@0.1.0", on_behalf_of: "usr_demo" },
|
|
30
|
+
items,
|
|
31
|
+
total: items.reduce((s, i) => s + i.amount, 0),
|
|
32
|
+
currency: "BRL",
|
|
33
|
+
items_hash: itemsHash(items),
|
|
34
|
+
mandate: { id: "mdt_test_0001", version: 1 },
|
|
35
|
+
idempotency_key: "idk_1",
|
|
36
|
+
blocking_reasons: [],
|
|
37
|
+
outcomes: [],
|
|
38
|
+
history: [],
|
|
39
|
+
created_at: now.toISOString(),
|
|
40
|
+
updated_at: now.toISOString(),
|
|
41
|
+
...over,
|
|
42
|
+
} as Execution;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const escola: ExecutionItem = { alias: "escola", beneficiary: "Escola Aurora", payee: "escola@exemplo.com.br", amount: 185000, currency: "BRL", description: "outubro" };
|
|
46
|
+
|
|
47
|
+
const line = (ref: string, quantity: number, unit_amount: number, amount = quantity * unit_amount): CompositionLine => ({ ref, quantity, unit_amount, amount, currency: "BRL" });
|
|
48
|
+
const twoLessons = [line("aula-avulsa", 2, 10000)];
|
|
49
|
+
const oneConsulting = [line("consultoria-1h", 1, 20000)];
|
|
50
|
+
const composition = (ref: string, lines: CompositionLine[]): ExecutionComposition => ({ ref, composition_hash: compositionHash(lines), line_count: lines.length });
|
|
51
|
+
|
|
52
|
+
describe("dependency 9.7: an artifact without a composition is the artifact it was before, byte for byte", () => {
|
|
53
|
+
it("hashes to what the same artifact hashed to on main before `composition` existed", () => {
|
|
54
|
+
// The bills-agent's shape: one payee, no batch, no composition. The two
|
|
55
|
+
// constants were computed on origin/main at 768c1a2, before this change,
|
|
56
|
+
// with the same ids, key, clock and execution. If `composition` were
|
|
57
|
+
// written as null — or any key added — both would move.
|
|
58
|
+
const artifact = createApprovalArtifact(signer, { execution: execution([escola]), approver: person, actor: human, now });
|
|
59
|
+
expect("composition" in artifact).toBe(false);
|
|
60
|
+
expect(sha256Hex(canonicalJson(artifact))).toBe("3c7da9ead8078c58e6727dc6645ff1cde3f7e69d78502ea17350887ba201f030");
|
|
61
|
+
expect(artifact.signature.value).toBe("5fd40687454f7a67a84cb6bb375eb91b7d9ba3d20242d1fe95c7845c9dc31a0a");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("dependency 9.7: the composition is carried, signed and compared", () => {
|
|
66
|
+
it("an artifact carries the composition of the execution it approves, and the signature covers it", () => {
|
|
67
|
+
const order: ExecutionItem = { beneficiary: "Marina Costa", payee: "11144477735", amount: 20000, currency: "BRL", due_date: "2026-09-23" };
|
|
68
|
+
const exe = execution([order], { composition: composition("cart-1", twoLessons) });
|
|
69
|
+
const artifact = createApprovalArtifact(signer, { execution: exe, approver: person, actor: human, now });
|
|
70
|
+
expect(artifact.composition).toEqual(exe.composition);
|
|
71
|
+
expect(checkApprovalArtifact(signer, artifact, exe, now)).toEqual({ ok: true });
|
|
72
|
+
for (const changed of [composition("cart-1", oneConsulting), { ...exe.composition!, ref: "cart-2" }, { ...exe.composition!, line_count: 2 }]) {
|
|
73
|
+
expect(checkApprovalArtifact(signer, { ...artifact, composition: changed }, exe, now)).toEqual({ ok: false, problem: "signature_invalid" });
|
|
74
|
+
expect(checkApprovalArtifact(signer, artifact, { ...exe, composition: changed }, now)).toEqual({ ok: false, problem: "composition_mismatch" });
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("the case items_hash cannot see: the same total, another cart", () => {
|
|
79
|
+
const order: ExecutionItem = { beneficiary: "Marina Costa", payee: "11144477735", amount: 20000, currency: "BRL", due_date: "2026-09-23" };
|
|
80
|
+
const approved = execution([order], { composition: composition("cart-1", twoLessons) });
|
|
81
|
+
const artifact = createApprovalArtifact(signer, { execution: approved, approver: person, actor: human, now });
|
|
82
|
+
const recomposed = { ...approved, composition: composition("cart-1", oneConsulting) };
|
|
83
|
+
expect(recomposed.items_hash).toBe(artifact.items_hash);
|
|
84
|
+
expect(checkApprovalArtifact(signer, artifact, recomposed, now)).toEqual({ ok: false, problem: "composition_mismatch" });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("gaining or losing a composition after approval is a different execution", () => {
|
|
88
|
+
const order: ExecutionItem = { beneficiary: "Marina Costa", payee: "11144477735", amount: 20000, currency: "BRL" };
|
|
89
|
+
const bare = execution([order]);
|
|
90
|
+
const bareArtifact = createApprovalArtifact(signer, { execution: bare, approver: person, actor: human, now });
|
|
91
|
+
expect(checkApprovalArtifact(signer, bareArtifact, { ...bare, composition: composition("cart-1", twoLessons) }, now)).toEqual({ ok: false, problem: "composition_mismatch" });
|
|
92
|
+
const composed = execution([order], { composition: composition("cart-1", twoLessons) });
|
|
93
|
+
const composedArtifact = createApprovalArtifact(signer, { execution: composed, approver: person, actor: human, now });
|
|
94
|
+
const { composition: _dropped, ...stripped } = composed;
|
|
95
|
+
expect(checkApprovalArtifact(signer, composedArtifact, stripped as Execution, now)).toEqual({ ok: false, problem: "composition_mismatch" });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("the hash sees quantity, unit price and order, which items_hash's fields cannot", () => {
|
|
99
|
+
// Ten at a 10% discount and nine at list price: the same line amount, not the same sale.
|
|
100
|
+
expect(compositionHash([line("aula-avulsa", 10, 10000, 90000)])).not.toBe(compositionHash([line("aula-avulsa", 9, 10000, 90000)]));
|
|
101
|
+
expect(compositionHash([line("aula-avulsa", 10, 9000, 90000)])).not.toBe(compositionHash([line("aula-avulsa", 10, 10000, 90000)]));
|
|
102
|
+
const a = line("aula-avulsa", 1, 10000);
|
|
103
|
+
const b = line("avaliacao-inicial", 1, 8990);
|
|
104
|
+
expect(compositionHash([a, b])).not.toBe(compositionHash([b, a]));
|
|
105
|
+
expect(compositionHash([a, b])).toMatch(/^sha256:[0-9a-f]{64}$/);
|
|
106
|
+
// A discount line is a line: a coupon swapped at a constant total is another composition.
|
|
107
|
+
expect(compositionHash([a, line("coupon:BEMVINDO10", 1, -1000)])).not.toBe(compositionHash([a, line("discount:order", 1, -1000)]));
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("dependency 9.7 in the engine: a cart recomposed after approval goes back to a person", () => {
|
|
112
|
+
const draftOrder = async (h: ReturnType<typeof harness>, lines: CompositionLine[]) => {
|
|
113
|
+
const d = await h.engine.draft({ items: [{ payee: "escola", amount: 20000 }], composition: composition("cart-1", lines) });
|
|
114
|
+
if (!d.ok) throw new Error("refused");
|
|
115
|
+
return d.execution;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
it("draft stamps the composition on the execution and on every artifact of it", async () => {
|
|
119
|
+
const h = harness({ mode: "human" });
|
|
120
|
+
const drafted = await draftOrder(h, twoLessons);
|
|
121
|
+
expect(drafted.composition).toEqual(composition("cart-1", twoLessons));
|
|
122
|
+
const approved = h.engine.approve(drafted.id, approver);
|
|
123
|
+
expect(h.store.getApproval(approved.approval_id!)!.composition).toEqual(composition("cart-1", twoLessons));
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("restated at a constant total: items_hash unchanged, the last gate refuses, a second yes mints a second artifact", async () => {
|
|
127
|
+
const h = harness({ mode: "human" });
|
|
128
|
+
const approved = h.engine.approve((await draftOrder(h, twoLessons)).id, approver);
|
|
129
|
+
const first = approved.approval_id!;
|
|
130
|
+
const restated = h.engine.restate(approved.id, { items: [{ payee: "escola", amount: 20000 }], composition: composition("cart-1", oneConsulting) });
|
|
131
|
+
expect(restated.state).toBe("approved");
|
|
132
|
+
expect(restated.items_hash).toBe(approved.items_hash);
|
|
133
|
+
expect(restated.approval_id).toBe(first);
|
|
134
|
+
|
|
135
|
+
const back = await h.engine.execute(approved.id);
|
|
136
|
+
expect(back.state).toBe("awaiting_approval");
|
|
137
|
+
expect(back.reason).toBe("items_hash_mismatch");
|
|
138
|
+
expect(back.detail).toContain("composition_mismatch");
|
|
139
|
+
expect(back.approval_id).toBeUndefined();
|
|
140
|
+
expect(h.store.listOutbox()).toHaveLength(0);
|
|
141
|
+
|
|
142
|
+
const again = h.engine.approve(back.id, approver);
|
|
143
|
+
expect(again.approval_id).not.toBe(first);
|
|
144
|
+
expect(h.store.getApproval(again.approval_id!)!.composition).toEqual(composition("cart-1", oneConsulting));
|
|
145
|
+
const ran = await h.engine.execute(again.id);
|
|
146
|
+
expect(ran.state).toBe("settled");
|
|
147
|
+
expect(ran.history.map((t) => t.to)).toEqual(["awaiting_approval", "approved", "awaiting_approval", "approved", "executing", "settled"]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("restated with a new total: the new total is the one judged and run", async () => {
|
|
151
|
+
const h = harness({ mode: "human" });
|
|
152
|
+
const approved = h.engine.approve((await draftOrder(h, twoLessons)).id, approver);
|
|
153
|
+
const fiveLessons = [line("aula-avulsa", 5, 10000)];
|
|
154
|
+
h.engine.restate(approved.id, { items: [{ payee: "escola", amount: 50000 }], composition: composition("cart-1", fiveLessons) });
|
|
155
|
+
const back = await h.engine.execute(approved.id);
|
|
156
|
+
expect(back.state).toBe("awaiting_approval");
|
|
157
|
+
expect(back.total).toBe(50000);
|
|
158
|
+
const ran = await h.engine.execute(h.engine.approve(back.id, approver).id);
|
|
159
|
+
expect(ran.state).toBe("settled");
|
|
160
|
+
expect(h.engine.list({ state: "settled" }).map((e) => e.total)).toEqual([50000]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("restate changes what is charged, never to whom, and never an execution that is no longer open", async () => {
|
|
164
|
+
const h = harness({ mode: "human" });
|
|
165
|
+
const drafted = await draftOrder(h, twoLessons);
|
|
166
|
+
expect(() => h.engine.restate(drafted.id, { items: [{ payee: "mercado", amount: 20000 }] })).toThrow(/payees/);
|
|
167
|
+
expect(() => h.engine.restate(drafted.id, { items: [{ payee: "escola", amount: 10000 }, { payee: "escola", amount: 10000 }] })).toThrow(/payees/);
|
|
168
|
+
expect(() => h.engine.restate(drafted.id, { items: [{ payee: "escola", amount: 20000 }], batch: { ref: "b", batch_hash: "sha256:x", index: 0, count: 1 } })).toThrow(/batch/);
|
|
169
|
+
const denied = h.engine.deny(drafted.id, approver);
|
|
170
|
+
expect(() => h.engine.restate(denied.id, { items: [{ payee: "escola", amount: 20000 }] })).toThrow(/only an open execution/);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("a composition that cannot be a hash of lines is refused where the execution is minted", async () => {
|
|
174
|
+
const h = harness({ mode: "human" });
|
|
175
|
+
await expect(h.engine.draft({ items: [{ payee: "escola", amount: 20000 }], composition: { ref: "cart-1", composition_hash: "sha256:nope", line_count: 1 } })).rejects.toThrow(/composition_hash/);
|
|
176
|
+
await expect(h.engine.draft({ items: [{ payee: "escola", amount: 20000 }], composition: { ...composition("cart-1", twoLessons), line_count: 0 } })).rejects.toThrow(/line_count/);
|
|
177
|
+
await expect(h.engine.draft({ items: [{ payee: "escola", amount: 20000 }], composition: { ...composition("cart-1", twoLessons), ref: " " } })).rejects.toThrow(/ref/);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("an execution without a composition carries no composition key, in state or in the bundle", async () => {
|
|
181
|
+
const h = harness({ mode: "human" });
|
|
182
|
+
const d = await h.engine.draft({ items: [{ payee: "escola", amount: 1000 }] });
|
|
183
|
+
if (!d.ok) throw new Error("refused");
|
|
184
|
+
const approved = h.engine.approve(d.execution.id, approver);
|
|
185
|
+
expect("composition" in approved).toBe(false);
|
|
186
|
+
expect("composition" in h.store.getApproval(approved.approval_id!)!).toBe(false);
|
|
187
|
+
for (const e of h.store.listEvents()) expect(JSON.stringify(e.payload)).not.toContain("composition");
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe("dependency 9.8: `state` is an effect, and only a local tool has it", () => {
|
|
192
|
+
const tool = (effect: string) => ({ name: "cart_update", effect, description: "replaces the cart", input_schema: { type: "object" } });
|
|
193
|
+
it("a local tool may change state", () => {
|
|
194
|
+
expect(ToolsFileSchema.safeParse({ meta_tools: [], local_tools: [tool("state")] }).success).toBe(true);
|
|
195
|
+
});
|
|
196
|
+
it("a meta-tool may not: CodeSpar's names are for money and reads", () => {
|
|
197
|
+
const parsed = ToolsFileSchema.safeParse({ meta_tools: [{ ...tool("state"), name: "codespar_cart" }], local_tools: [] });
|
|
198
|
+
expect(parsed.success).toBe(false);
|
|
199
|
+
expect(parsed.error?.message).toContain("a state tool is local");
|
|
200
|
+
});
|
|
201
|
+
it("an effect outside the enum is still refused", () => {
|
|
202
|
+
expect(ToolsFileSchema.safeParse({ meta_tools: [], local_tools: [tool("write")] }).success).toBe(false);
|
|
203
|
+
});
|
|
204
|
+
});
|
package/templates/collections-agent/packages/agent-core/test/fixtures/production-receipt-keys.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"issuer":"did:web:id.codespar.dev","key_namespace":"production","algorithm":"Ed25519","signing_string":"codespar-receipt:v1:<receipt_id>:<chain>","chain_recipe":{"canonicalization":"RFC 8785 (JCS)","digest":"SHA-256 over the UTF-8 bytes of the canonical JSON, lowercase hex","input":"{ \"v\": <chain_version>, \"links\": [ <each link below that applies, in this order> ] }","version":{"field":"chain_version","rule":"4 when `approval` is present; else 3 when `payment.amount_authorized` is present; else 2 when `payment.amount_atomic` is not null; else 1"},"optional_marker":"?","links":[{"link":"mandate","from":"mandate","when":"always","fields_by_version":{"1":["id","nonce","scope","currency","sig"],"2":["id","nonce","scope","currency","sig"],"3":["id","nonce","scope","currency","sig"],"4":["id","nonce","scope","currency","sig_sha256"]}},{"link":"quote","from":"quote","when":"quote is not null","fields":["seller","resource","price_minor","payee","session_id","sig","at"]},{"link":"approval","from":"approval","when":"chain_version is 4","fields":["items_hash","batch_hash"]},{"link":"payment","from":"payment","when":"always","fields":["rail","provider","tx_id","amount_minor","amount_atomic?","amount_authorized?","amount_charged?","amount_refunded?","metering?","sandbox?","attempt_id","money_moved","at"]},{"link":"delivery","from":"delivery","when":"delivery is not null","fields":["result","proof","kind","nfe_chave","at"]}],"notes":["`mandate.sig_sha256` is SHA-256 over the UTF-8 bytes of `mandate.sig`, lowercase hex. A v4 chain seals it in place of `sig`, which must not be shared; v1-v3 seal `sig` itself, so only the tenant, whose read carries `sig`, can recompute those. Anyone can still verify their signature over `chain`.","Recomputing needs `quote.payee` unmasked. Whoever shares a receipt decides whether to hand it over.","Timestamps are RFC 3339 UTC with milliseconds, exactly as read."]},"keys":[{"kid":"did:web:id.codespar.dev#1","kty":"OKP","crv":"Ed25519","x":"fz1svKf1AFlnIEZSseNqH5zCXkKqPmtkjx9mMNBz8pU","alg":"EdDSA","use":"sig","status":"retired","created_at":"2026-07-05T13:29:23.276Z","retired_at":"2026-09-25T02:43:45.971Z"},{"kid":"did:web:id.codespar.dev#production-2","kty":"OKP","crv":"Ed25519","x":"WKVTphnIma44d5T7bugYnLvI6h79LhaSdNDrkOFxmRs","alg":"EdDSA","use":"sig","status":"active","created_at":"2026-09-25T02:43:45.971Z","retired_at":null}]}
|
|
@@ -9,6 +9,7 @@ import { MandateSchema, type Mandate } from "../src/mandate.js";
|
|
|
9
9
|
import { ManifestSchema, type Manifest } from "../src/manifest.js";
|
|
10
10
|
import { StateStore } from "../src/state/store.js";
|
|
11
11
|
import { LocalMandateStatusStub } from "../src/stubs/mandate-status.js";
|
|
12
|
+
import type { PaymentRail } from "../src/rail.js";
|
|
12
13
|
import type { MandateStatusSource } from "../src/revocation.js";
|
|
13
14
|
import { StubRail, type StubRailOptions } from "../src/stubs/rail.js";
|
|
14
15
|
import type { ApprovalMode } from "../src/types.js";
|
|
@@ -91,6 +92,8 @@ export interface HarnessOptions {
|
|
|
91
92
|
mandate?: Partial<Mandate>;
|
|
92
93
|
manifest?: Partial<Manifest>;
|
|
93
94
|
rail?: StubRailOptions;
|
|
95
|
+
/** What the engine dispatches to instead of the stub itself, e.g. the stub with a different receipt. `rail` on the harness stays the stub. */
|
|
96
|
+
wrapRail?: (stub: StubRail) => PaymentRail;
|
|
94
97
|
dir?: string;
|
|
95
98
|
runId?: string;
|
|
96
99
|
/** The section 4.7 status source; the local stub unless a test wires the API one. */
|
|
@@ -109,7 +112,7 @@ export function harness(options: HarnessOptions = {}): Harness {
|
|
|
109
112
|
const mode = options.mode ?? "human";
|
|
110
113
|
const deps: EngineDeps = {
|
|
111
114
|
store,
|
|
112
|
-
rail,
|
|
115
|
+
rail: options.wrapRail ? options.wrapRail(rail) : rail,
|
|
113
116
|
status: options.status ?? gate,
|
|
114
117
|
signer: hmacSigner("test", Buffer.alloc(32, 1)),
|
|
115
118
|
manifest: testManifest(options.manifest),
|