Skip to content

Cost/session layering: ledger snapshot logic in the HTTP layer; import edges point both ways #1583

Closed

Description

@huang195

What

Three layering problems around the session/cost boundary:

  1. The ledger snapshot is built in the HTTP layer. core/sessionapi/usage.go:328 ledgerSnapshotFor (~150 lines) plus its satellites (degradedFrom, bucketSecondsFor, servedSpan, resolutionSpan, :541-626) are pure cost-domain logic — ledger.Fold, saturation flags, caveats→degraded mapping, retention disclosure — with nothing HTTP in them. Meanwhile Fold/FilterAgent/series helpers already live in cost/ledger/query.go. As a result the ledger can only serve a snapshot through the endpoint, not programmatically.

  2. session → cost/usage exists for one type. The store's summary block (core/session/store.go:856-1222) imports the 4k-line usage package for exactly usage.CostSum (a ~45-line saturating-add type that depends on nothing). It belongs beside event.Micros in core/cost/event, which session already imports.

  3. cost/ledger → session exists for a compile-time pin. writer.go:335's production import exists solely for var _ session.Recorder = (*Writer)(nil) — an assertion that belongs in a _test.go.

Fix

Move ledgerSnapshotFor's body into cost/ledger (e.g. Writer.Snapshot(ctx, spec, group, agent)); move CostSum to cost/event; move the Recorder pin to a test file. Three one-file moves, and the session/cost boundary becomes one-directional.

Found in the 2026-10-29 structural audit of code organization.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions