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.
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.