handleAdjustCardLimit forwarded the model-supplied card_id straight to
gateway.AdjustCardLimit with no check that the card belongs to the
tenant this decision cycle is for. BuildToolSchemas's own doc comment
claims tenant_id is deliberately never a model parameter "so the model
can never redirect an action at another tenant" — true for
ration_bridge_funding/issue_card/notify_tenant (all pass tc.TenantID
explicitly), false for adjust_card_limit, whose only tenant binding is
an indirect card_id->tenant lookup that gateway's own tenantauth
interceptor explicitly skips for an operator credential (exactly what
this agent always authenticates as). A hallucinated or
prompt-injected card_id (e.g. via the tenant-controlled TenantName
field already rendered verbatim into the prompt) could silently
tighten or loosen ANY tenant's card limit.
Reproduced live against the real bare-metal stack (ledger-core, auth,
tenant-registry, card-issuance, gateway): minted the agent's real
operator JWT, called the real exported tool handler with tenant A's
TenantID but tenant B's real card_id — tenant B's card limit dropped
from 1,000.00 EUR to 0.00 EUR and flipped to RATIONED, with zero
errors anywhere in the chain. Fix: resolve the card via gateway.GetCard
first and decline (recording a real DECLINE audit entry) when its
tenant_id doesn't match tc.TenantID. Re-ran the identical repro
post-fix: cross-tenant attempt now rejected with the card untouched;
same-tenant adjustment still succeeds normally.
go build/vet/test clean for every go.work module; 0 regressions.
6 of 7 services that mint a credential from auth (agent, card-issuance,
tenant-registry, webhook-ingestion, cli, tui) called authclient.Dial(addr)
with zero DialOptions, while loading and correctly applying mTLS creds to
every OTHER peer dial in the same binary. gateway was the only caller that
got this right (authMTLS/authDialCreds pattern).
Reproduced live: with AUTH_MTLS_ENABLED=true (generated via
infra/dev-ca/generate-dev-ca.sh, the exact production-hardening path
docs/SCALE-EXPANSION.md section 8 documents), auth's server correctly
requires and verifies a client cert (tls.RequireAndVerifyClientCert) — a
plaintext dial against it doesn't degrade gracefully, it fails the TLS
handshake outright ("error reading server preface: EOF"/connection reset).
Confirmed with the real pre-fix card-issuance binary: it crashes at startup
("mint operator JWT: ... error reading server preface: EOF") the moment
mTLS is actually turned on, for every one of the 6 affected services.
Also found and fixed a second instance of the same root cause while
checking sibling call sites: the CLI's apikey issue/revoke/list and tenant
create/list commands each dial auth directly (bypassing clients.go's
dialClients entirely) and never attached mTLS creds either; tenant
create/list's own tenant-registry dial had the identical gap. Added a
shared dialAuthClient() helper in clients.go so this bug class can't
quietly reappear a 6th time within the same CLI binary.
Verified live end-to-end against the real 8-service bare-metal stack with
mTLS fully enabled (dev-ca-generated certs for every service): auth,
card-issuance, tenant-registry, webhook-ingestion, gateway, and agent all
now mint/verify successfully over mTLS at startup; a full CLI cycle (seed,
status, apikey issue/list/revoke, tenant create/list, card issue,
simulate-deposit --late, agent auto-grant, audit-check, verify-chain) ran
clean end to end. Re-verified the default mTLS-disabled path (every prior
round's own verification baseline) is completely unaffected by re-running
the identical demo cycle with mTLS off.
go build/go vet clean across all 11 go.work members, gofmt clean on every
touched file, go test clean for every affected package (0 regressions).
tools.go's handleRationBridgeFunding tagged EVERY nil-error gateway
response as AGENT_ACTION_RATION_BRIDGE, even when GrantedMinorUnits was
0. bridge.Service.RationBridgeFunding has three documented paths that
return exactly that shape (below the $5 minimum-useful-grant floor —
including its own independent, ledger-entry-based velocity check
disagreeing with this agent's decision-history-based
hasOpenGrantWithinWindow — a ledger-core-rechecked reserve-floor
violation, or "deposit already received"). None of those is a Go
error, so every one of them was recorded as if money had actually
moved.
This both corrupted the audit trail (a "granted $0" decision labeled
as a bridge grant) and fed hasOpenGrantWithinWindow's wasActuallyGranted
check a false "actually executed" signal, since the snapshot map always
carries a granted_minor_units key even at 0 — permanently
velocity-capping a tenant that was never actually granted anything, the
same fund-stranding-via-misclassification failure mode round 6 fixed
via a missing deposit_id, reached here instead via a wrong action tag.
Fix: tag the decision AGENT_ACTION_DECLINE whenever
GrantedMinorUnits <= 0, regardless of error.
Reproduced live against the real running stack (real auth + ledger-core,
real RecordAgentDecision/ListAgentDecisions, a stubbed gateway forcing
the exact zero-grant/nil-error response) before fixing, and confirmed
fixed the same way afterward. New regression tests in runner_test.go
cover both the misclassification itself and its downstream
velocity-cap consequence; both fail on the pre-fix code and pass after.
processLateDeposit unconditionally records a RATION_BRIDGE-tagged
"envelope pre-check" decision before ever attempting the LLM/tool call
that would actually move money — found live while verifying the
circuit breaker (internal/llm/breaker.go): when that first LLM attempt
failed outright (e.g. the provider being transiently unreachable,
before the breaker even trips), hasOpenGrantWithinWindow treated that
pre-check recording as "an open unrepaid grant" and velocity-capped
every later attempt against the same deposit for a full 24h — even
though no money had ever actually moved and the breaker's whole point
is to execute that same envelope directly once tripped.
hasOpenGrantWithinWindow now only counts RATION_BRIDGE decisions whose
snapshot actually includes granted_minor_units (new
wasActuallyGranted helper) — the key only llm/tools.go's
handleRationBridgeFunding sets, on a real executed grant, never the
pre-check recording. Updated the two existing velocity-cap test
fixtures to include it, since a real "open prior grant" always has it
too.
docs/AGENT-DESIGN.md states plainly: "there is no confirmed... no
circuit breaker: a slow/erroring LLM provider stalls that tick's
decision loop." Add llm.CircuitBreaker (new internal/llm/breaker.go):
trips after N consecutive failures within a rolling window
(RATION_AGENT_LLM_BREAKER_* env vars), short-circuiting every call with
ErrCircuitOpen until a cooldown elapses, then allows one half-open
trial before fully closing again.
internal/loop.Runner now treats ErrCircuitOpen as a signal to execute
the already-computed, deterministic hard-rule envelope directly (same
tool-handler code path the LLM itself would call, canned
reason/message instead of LLM-authored text) rather than skipping the
tenant's decision entirely — the breaker's only job is making sure a
stuck provider never blocks or delays the money movement the hard
rules already decided was safe, matching the existing "LLM decides
phrasing only" design.
Verified live: with no ANTHROPIC_API_KEY configured, the breaker trips
after 3 failures, then correctly executes real bridge-funding grants
(HOLD entries landing on ledger-core, held balance increasing) via the
fallback path with zero LLM calls, and the velocity cap correctly
blocks further grants afterward.
docs/THREAT-MODEL.md names this "the biggest one" sub-gap of item 1:
cli/tui/agent (and gateway's own forwarding, card-issuance,
webhook-ingestion, tenant-registry) all dial ledger-core directly,
bypassing gateway's internal/tenantauth entirely, so any operator or
compromised caller could act as any tenant_id against the system's
actual source of truth regardless of what gateway enforces upstream.
Add services/ledger-core/src/tenantauth.rs (authorize_tenant /
authorize_operator, checking x-api-key / authorization: Bearer headers
against a new src/authclient.rs that dials Auth's VerifyAPIKey/
VerifyJWT directly) and call it from every LedgerCore RPC handler.
RATION_LEDGER_REQUIRE_TENANT_AUTH defaults to true; ledger-core refuses
to start with it on but no AUTH_GRPC_ADDR configured.
Every direct ledger-core caller now mints and presents its own
operator-scoped credential: cli/tui/agent (reusing their existing
gateway credential), and new internal/authclient packages for gateway,
card-issuance, webhook-ingestion and tenant-registry.
Verified end-to-end against the live demo stack (seed, simulate-deposit,
status, and the agent's poll/bridge-funding loop all succeed with the
new enforcement on; zero Unauthenticated/PermissionDenied anywhere in
any service's logs).
Close THREAT-MODEL.md's top-named gap end-to-end: services/cli,
services/tui and services/agent each mint an operator-scoped
credential (new internal/authclient in all three, mirroring gateway's
existing authclient) at startup and attach it as x-api-key/
authorization metadata on every gateway call, so
RATION_GATEWAY_REQUIRE_TENANT_AUTH now defaults to true instead of
false.
Also close the two named sub-gaps this left open:
- GetCard/AdjustCardLimit carry only card_id, not tenant_id, so
tenantauth and ratelimit previously passed them through unscoped.
Add internal/gateway/cardtenant, a cached card_id -> tenant_id
resolver backed by card-issuance, and use it in both interceptors.
- StreamActivity accepted an empty tenant_id (meaning "all tenants")
from any caller, authenticated or not. Add
tenantauth.StreamServerInterceptor: a non-operator credential may
only subscribe to its own tenant_id; only an operator credential may
request the all-tenants view.
Phase 2 service decomposition per docs/SCALE-EXPANSION.md section 1: extracts
internal/{airwallex,bridge,card,tenant,httpserver} out of the monolithic
gateway into four independently deployable gRPC services (each own go.mod,
Dockerfile, and tests), with gateway now a thin BFF that fans out through new
cardissuanceclient/webhookeventsclient gRPC clients. gateway.proto's external
surface is unchanged — agent/tui/cli still talk to gateway exactly as before.
Verified: all 10 go.work modules build and `go test ./...` passes across
every one, including the new gateway client packages against the live split
services (via `make build && make test`).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Six services, independently built then integrated:
- services/wal (C): crash-safe write-ahead log, CRC32-checksummed
frames, torn-write recovery via truncate-to-last-valid-frame.
- services/ledger-core (Rust): double-entry ledger over the C WAL via
FFI, reserve-floor enforcement BEFORE any WAL write, idempotent
ApplyEntry, full LedgerCore gRPC service.
- services/gateway (Go): Airwallex Issuing/Global-Accounts client
(mock mode default, live mode ready), the actual rationing math
(reserve floor, 60% bridge cap, 24h velocity cap, partial-over-decline).
- services/agent (Go): deterministic hard-rule policy envelope computed
BEFORE any LLM call; Claude only decides within that pre-approved
envelope; every decision audit-logged even when the LLM call fails.
- services/tui (Go, Bubble Tea) + services/cli (Go, cobra): the only
UI — no web frontend, per explicit direction.
Integration found and fixed three real bugs, not just compile errors:
- proto regen collision shadowing checked-in pkg/pb packages
- ledger-core/gateway address-variable mismatch (stack would never
actually connect on default config)
- the big one: MarkDepositReceived never credited the ledger, so every
tenant's balance was permanently stuck at 0 and bridge-funding could
only ever decline — the core agentic-rationing feature could never
produce a real grant. Fixed at the ledger level with idempotent
mark-and-credit in one WAL frame, covered by 3 new tests.
End-to-end verified live, reproducibly: seed -> simulate a late deposit
-> agent grants a partial bridge with a computed rationale -> second
grant correctly declines once the 24h velocity cap engages. Verified
independently by the orchestrator too: 'make build' and 'make test'
both clean (Rust: 29 tests; Go: all packages; C: WAL test suite).
Airwallex sandbox credentials still pending organizer approval —
tracked as an owner blocker in docs/AIRWALLEX-SETUP.md, not a code gap.