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.
68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
// Package depositsim computes the request parameters for
|
|
// `ration simulate-deposit`, kept separate from the cobra command so the
|
|
// late-vs-on-time timestamp math is unit-testable without a gRPC
|
|
// connection.
|
|
package depositsim
|
|
|
|
import "time"
|
|
|
|
// Params are the already-resolved values sent on
|
|
// gateway.SimulateDepositRequest.
|
|
type Params struct {
|
|
ExpectedAtUnix int64
|
|
MarkLate bool
|
|
}
|
|
|
|
// Options configures how far in the future an on-time deposit is expected,
|
|
// and how far in the past a late deposit's expected_at is backdated.
|
|
type Options struct {
|
|
// Now is injected for testability; callers should pass time.Now().
|
|
Now time.Time
|
|
// Late marks the deposit as already overdue.
|
|
Late bool
|
|
// ExpectedIn is how far in the future an on-time deposit is expected
|
|
// to land. Ignored when Late is true.
|
|
ExpectedIn time.Duration
|
|
// LateBy is how far in the past a late deposit's expected_at_unix is
|
|
// backdated, so the agent's next ListLateDeposits poll already picks
|
|
// it up as late.
|
|
LateBy time.Duration
|
|
}
|
|
|
|
// DefaultExpectedIn is used when the caller doesn't override how far out
|
|
// an on-time deposit is expected.
|
|
const DefaultExpectedIn = 48 * time.Hour
|
|
|
|
// DefaultLateBy is used when the caller doesn't override how far overdue
|
|
// a --late deposit is backdated.
|
|
const DefaultLateBy = 48 * time.Hour
|
|
|
|
// Resolve computes the expected_at_unix and mark_late fields to send on
|
|
// SimulateDepositRequest for the given options.
|
|
//
|
|
// A --late deposit gets expected_at_unix backdated into the past (not just
|
|
// mark_late=true) so the result is correct regardless of whether the
|
|
// gateway implementation re-derives lateness from mark_late or from
|
|
// comparing expected_at_unix against wall-clock time.
|
|
func Resolve(opts Options) Params {
|
|
expectedIn := opts.ExpectedIn
|
|
if expectedIn <= 0 {
|
|
expectedIn = DefaultExpectedIn
|
|
}
|
|
lateBy := opts.LateBy
|
|
if lateBy <= 0 {
|
|
lateBy = DefaultLateBy
|
|
}
|
|
|
|
if opts.Late {
|
|
return Params{
|
|
ExpectedAtUnix: opts.Now.Add(-lateBy).Unix(),
|
|
MarkLate: true,
|
|
}
|
|
}
|
|
return Params{
|
|
ExpectedAtUnix: opts.Now.Add(expectedIn).Unix(),
|
|
MarkLate: false,
|
|
}
|
|
}
|