caelum0x 2b686594a3 feat: Ration phase 1 — full polyglot build for Airwallex Starter Kit 5
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.
2026-10-19 03:04:13 +03:00

127 lines
3.5 KiB
Go

// Package activity implements a small Go-channel fan-out event bus that
// feeds the gateway.proto StreamActivity RPC. Every card, deposit, ledger
// and agent-decision action publishes an Event here; the TUI (and any
// other StreamActivity subscriber) gets each event pushed to it live.
package activity
import (
"sync"
"time"
"github.com/caelum0x/ration/services/gateway/internal/idgen"
)
// Event mirrors gateway.proto's ActivityEvent, kept as a plain Go struct
// here so this package has zero dependency on generated pb types or
// gRPC — internal/grpcserver does that mapping at the edge.
type Event struct {
ID string
TenantID string
Kind string // "deposit", "card", "agent_decision", "ledger_entry"
Summary string
AtUnix int64
}
// NewEvent fills in ID and AtUnix for a caller that only has the
// business fields to report.
func NewEvent(tenantID, kind, summary string) Event {
return Event{
ID: idgen.New("evt"),
TenantID: tenantID,
Kind: kind,
Summary: summary,
AtUnix: time.Now().Unix(),
}
}
// subscriberBufferSize bounds how many unread events a slow subscriber
// can accumulate before Publish starts dropping its oldest unread event
// rather than blocking every other subscriber (and the publisher) on one
// stuck consumer — appropriate for a live activity feed where a dropped
// stale event is better than a wedged gRPC stream.
const subscriberBufferSize = 256
// Bus is a fan-out event bus: every Publish is delivered to every
// subscriber whose tenant filter matches (empty filter = all tenants).
type Bus struct {
mu sync.Mutex
nextID int64
subs map[int64]*subscriber
}
type subscriber struct {
tenantFilter string // "" = all tenants
ch chan Event
}
// NewBus constructs an empty event bus.
func NewBus() *Bus {
return &Bus{subs: make(map[int64]*subscriber)}
}
// Subscribe registers a new subscriber scoped to tenantFilter ("" for
// every tenant) and returns a receive-only channel plus an unsubscribe
// function the caller must call exactly once when done (e.g. via
// defer) to release resources.
func (b *Bus) Subscribe(tenantFilter string) (<-chan Event, func()) {
b.mu.Lock()
defer b.mu.Unlock()
id := b.nextID
b.nextID++
sub := &subscriber{
tenantFilter: tenantFilter,
ch: make(chan Event, subscriberBufferSize),
}
b.subs[id] = sub
unsubscribe := func() {
b.mu.Lock()
defer b.mu.Unlock()
if existing, ok := b.subs[id]; ok {
close(existing.ch)
delete(b.subs, id)
}
}
return sub.ch, unsubscribe
}
// Publish fans e out to every matching subscriber. Delivery is
// non-blocking per subscriber: if a subscriber's buffer is full, the
// oldest buffered event for that subscriber is dropped to make room,
// rather than blocking the publisher (and every other subscriber) on one
// slow consumer.
func (b *Bus) Publish(e Event) {
b.mu.Lock()
defer b.mu.Unlock()
for _, sub := range b.subs {
if sub.tenantFilter != "" && sub.tenantFilter != e.TenantID {
continue
}
select {
case sub.ch <- e:
default:
// Buffer full: drop the oldest event, then push the new one.
select {
case <-sub.ch:
default:
}
select {
case sub.ch <- e:
default:
// Extremely unlikely race with another drain; skip rather
// than block.
}
}
}
}
// SubscriberCount reports the current number of active subscribers,
// primarily for tests and health/debug introspection.
func (b *Bus) SubscriberCount() int {
b.mu.Lock()
defer b.mu.Unlock()
return len(b.subs)
}