ration/proto/card_issuance.proto
caelum0x 5bb6bc92f2 fix(card-issuance): persist card metadata — a restart silently lost every issued card forever
cardsvc.Service's own `cards map[string]*cardissuancepb.Card` was the
only place this repo stores Card metadata (ledger-core's proto has no
Card message), and it was pure in-memory state with zero durability.
A card-issuance restart lost every issued card's id/cardholder/limit/
status while the ledger HOLD backing its limit lived on in ledger-core
forever, unreachable by GetCard/AdjustCardLimit/CloseCard ever again
(all three look the card up by id first). card_issuance.proto's own
service comment also claimed this service was "stateless... safe to
run as N replicas" — false given this in-memory map; corrected.

Adds a file-backed store (services/card-issuance/internal/cardsvc/
store.go): atomic write-then-fsync-then-rename JSON file, loaded on
startup via a new EnablePersistence(path) method (opt-in, so existing
unit tests that never call it are unaffected) and written after every
IssueCard/AdjustCardLimit/CloseCard. New RATION_CARD_ISSUANCE_DATA_DIR
env var (default ./data, same convention as RATION_AUDIT_LOG_DATA_DIR),
wired in main.go.

Verified live against the real bare-metal stack: issued a card,
confirmed data/cards.json held it, killed and restarted card-issuance,
and confirmed `ration card get`/`ration status` still found the card
(pre-fix this would have been NotFound). go build/vet/test clean for
card-issuance (0 regressions), full `make build` clean.

Does not make this service safe to run as N replicas (each replica
would still load/save its own independent copy of the file) — that
needs a real shared store, the same class of gap already named and
left open for services/auth's apikey.Store; documented, not attempted,
in this round.
2026-10-29 13:53:46 +03:00

102 lines
3.3 KiB
Protocol Buffer

syntax = "proto3";
package ration.cardissuance.v1;
import "common.proto";
option go_package = "github.com/caelum0x/ration/pkg/pb/cardissuance";
// CardIssuance is an internal-only service (docs/SCALE-EXPANSION.md
// section 1): a direct extraction of services/gateway's old
// internal/card (Airwallex issuing-card management) and internal/bridge
// (the rationing/bridge-funding math) — the highest-value service in the
// system per docs/adr/0004, since this is where real money decisions
// happen. Money-moving correctness under concurrent calls for one
// tenant rests on ledger-core's idempotent, reserve-floor-checked
// ApplyEntry, not on any lock here. That is NOT the same as "safe to
// run as N replicas" — this service's own internal/cardsvc owns the
// only copy of each Card's metadata (id, cardholder, limit, status;
// ledger-core's proto has no Card message), persisted to a single
// local file per process (cardsvc.Service.EnablePersistence). Two
// replicas behind a load balancer would each load/save their own
// independent copy of that file and can genuinely diverge — a card
// IssueCard creates on replica A is invisible to GetCard/ListCards on
// replica B. Multi-replica safety for this service needs a real shared
// store, not a local file; see docs/THREAT-MODEL.md for the write-up
// of what IS fixed (durability across a single process's own restart)
// versus what still requires that bigger, intentionally out-of-scope
// change.
//
// gateway.proto (the one public surface the TUI/CLI/agent dial) is
// unchanged by this split — services/gateway is the thin BFF that calls
// this service's RPCs and republishes their results on its own
// activity bus.
service CardIssuance {
rpc IssueCard(IssueCardRequest) returns (Card);
rpc AdjustCardLimit(AdjustCardLimitRequest) returns (Card);
rpc GetCard(GetCardRequest) returns (Card);
rpc ListCards(ListCardsRequest) returns (ListCardsResponse);
// CloseCard permanently closes a card: releases its full remaining
// ledger hold back to the tenant's available balance and marks the
// card CARD_STATUS_CLOSED via Airwallex. Idempotent — closing an
// already-closed card returns it unchanged rather than erroring.
rpc CloseCard(CloseCardRequest) returns (Card);
rpc RationBridgeFunding(RationBridgeFundingRequest) returns (RationBridgeFundingResponse);
}
message Card {
string id = 1;
string tenant_id = 2;
string airwallex_card_id = 3;
string cardholder_name = 4;
ration.common.v1.CardStatus status = 5;
int64 limit_minor_units = 6;
string limit_interval = 7; // "daily" | "monthly"
string currency = 8;
int64 created_at_unix = 9;
}
message IssueCardRequest {
string tenant_id = 1;
string cardholder_name = 2;
int64 limit_minor_units = 3;
string limit_interval = 4;
string currency = 5;
}
message AdjustCardLimitRequest {
string card_id = 1;
int64 new_limit_minor_units = 2;
string reason = 3;
}
message GetCardRequest {
string card_id = 1;
}
message CloseCardRequest {
string card_id = 1;
}
message ListCardsRequest {
string tenant_id = 1;
}
message ListCardsResponse {
repeated Card cards = 1;
}
message RationBridgeFundingRequest {
string tenant_id = 1;
int64 requested_minor_units = 2;
string reason = 3;
}
message RationBridgeFundingResponse {
int64 granted_minor_units = 1;
bool fully_granted = 2;
string explanation = 3;
}