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.
116 lines
4.3 KiB
Go
116 lines
4.3 KiB
Go
// Package ledgerclient is a thin, typed wrapper around the generated
|
|
// ledger-core gRPC client. It exists so the rest of the agent (the loop
|
|
// runner and the LLM tool handlers) talk to ledger-core through a small,
|
|
// purpose-built interface instead of the raw generated client, and so
|
|
// RecordAgentDecision — the one call every tool invocation must make for
|
|
// audit purposes — has a single, consistent call site.
|
|
package ledgerclient
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
common "github.com/caelum0x/ration/pkg/pb/common"
|
|
ledger "github.com/caelum0x/ration/pkg/pb/ledger"
|
|
)
|
|
|
|
// Client wraps ledger.LedgerCoreClient with the subset of operations the
|
|
// agent needs, with Go-native types (time.Time instead of unix int64,
|
|
// errors instead of raw gRPC statuses with no context).
|
|
type Client struct {
|
|
raw ledger.LedgerCoreClient
|
|
}
|
|
|
|
// New builds a Client around an existing generated gRPC client. Accepting
|
|
// the interface (rather than a concrete *grpc.ClientConn) keeps this
|
|
// package trivially fakeable in tests.
|
|
func New(raw ledger.LedgerCoreClient) *Client {
|
|
return &Client{raw: raw}
|
|
}
|
|
|
|
// ListLateDeposits returns every deposit ledger-core considers late as of
|
|
// asOf.
|
|
func (c *Client) ListLateDeposits(ctx context.Context, asOf time.Time) ([]*ledger.Deposit, error) {
|
|
resp, err := c.raw.ListLateDeposits(ctx, &ledger.ListLateDepositsRequest{
|
|
AsOfUnix: asOf.Unix(),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ledgerclient: ListLateDeposits: %w", err)
|
|
}
|
|
return resp.GetDeposits(), nil
|
|
}
|
|
|
|
// ListDeposits returns every deposit recorded for a tenant.
|
|
func (c *Client) ListDeposits(ctx context.Context, tenantID string) ([]*ledger.Deposit, error) {
|
|
resp, err := c.raw.ListDeposits(ctx, &ledger.ListDepositsRequest{TenantId: tenantID})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ledgerclient: ListDeposits(%s): %w", tenantID, err)
|
|
}
|
|
return resp.GetDeposits(), nil
|
|
}
|
|
|
|
// GetTenant fetches a tenant by id.
|
|
func (c *Client) GetTenant(ctx context.Context, tenantID string) (*ledger.Tenant, error) {
|
|
t, err := c.raw.GetTenant(ctx, &ledger.GetTenantRequest{Id: tenantID})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ledgerclient: GetTenant(%s): %w", tenantID, err)
|
|
}
|
|
return t, nil
|
|
}
|
|
|
|
// CheckReserveFloor asks ledger-core how much headroom a tenant has above
|
|
// its reserve floor for a proposed debit. Passing proposedDebitMinorUnits
|
|
// of 0 returns the tenant's current unconditional headroom, which is what
|
|
// the bridge-funding envelope math in internal/policy uses.
|
|
func (c *Client) CheckReserveFloor(ctx context.Context, tenantID string, proposedDebitMinorUnits int64) (*ledger.CheckReserveFloorResponse, error) {
|
|
resp, err := c.raw.CheckReserveFloor(ctx, &ledger.CheckReserveFloorRequest{
|
|
TenantId: tenantID,
|
|
ProposedDebitMinorUnits: proposedDebitMinorUnits,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ledgerclient: CheckReserveFloor(%s): %w", tenantID, err)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// ListAgentDecisions returns the most recent agent decisions recorded for
|
|
// a tenant, most-recent-first (as returned by ledger-core), bounded by
|
|
// limit.
|
|
func (c *Client) ListAgentDecisions(ctx context.Context, tenantID string, limit int32) ([]*ledger.AgentDecision, error) {
|
|
resp, err := c.raw.ListAgentDecisions(ctx, &ledger.ListAgentDecisionsRequest{
|
|
TenantId: tenantID,
|
|
Limit: limit,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ledgerclient: ListAgentDecisions(%s): %w", tenantID, err)
|
|
}
|
|
return resp.GetDecisions(), nil
|
|
}
|
|
|
|
// RecordAgentDecision writes an audit record for a single agent decision —
|
|
// a hard-rule computation or an LLM tool call — before or immediately
|
|
// after any associated money movement. snapshotJSON should capture the
|
|
// exact rule inputs (headroom, deposit amount, velocity state) that
|
|
// produced the decision; pass "{}" if there is nothing to capture.
|
|
func (c *Client) RecordAgentDecision(
|
|
ctx context.Context,
|
|
tenantID string,
|
|
trigger string,
|
|
action common.AgentAction,
|
|
rationale string,
|
|
snapshotJSON string,
|
|
) (*ledger.AgentDecision, error) {
|
|
decision, err := c.raw.RecordAgentDecision(ctx, &ledger.RecordAgentDecisionRequest{
|
|
TenantId: tenantID,
|
|
Trigger: trigger,
|
|
Action: action,
|
|
Rationale: rationale,
|
|
PolicySnapshotJson: snapshotJSON,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ledgerclient: RecordAgentDecision(%s, %s): %w", tenantID, action, err)
|
|
}
|
|
return decision, nil
|
|
}
|