Repository navigation
REF: consolidate domain logic across TUI and web - #96
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Renamed allowances retain stale monitor labels, and negative usage corrections can render as full browser bars because the peak is allowed to become negative.
2 open issues
What changed in this PR
This PR moves shared business rules into the Go core so the terminal and web interfaces use consistent calculations while retaining separate rendering and security responsibilities.
Changes:
- Centralizes quota identity, session status, token sampling, and API-equivalent estimation.
- Shares Pace projections and Usage aggregation, including fractional bar scaling.
- Generates browser contracts and numerical fixtures, adds freshness checks, and documents the workflow.
| File | Description |
|---|---|
web/tests/projections.generated.json |
Adds Go-generated numerical fixtures. |
web/tests/browser.spec.ts |
Uses shared fixtures and tests fractional bars. |
web/src/UsageReports.svelte |
Renders Go-projected Usage distributions. |
web/src/state.svelte.ts |
Replaces handwritten types with generated exports. |
web/src/SessionCommands.svelte |
Uses generated commands and input-review metadata. |
web/src/SessionActions.svelte |
Adopts generated types and optional timestamps. |
web/src/Quota.svelte |
Passes server-produced Pace geometry. |
web/src/GoalStatus.svelte |
Accepts nullable goal data. |
web/src/contracts.generated.ts |
Defines generated browser contracts and commands. |
web/src/ConsumptionPace.svelte |
Replaces browser regression calculations with projections. |
web/.prettierignore |
Excludes generated contracts and fixtures. |
README.md |
Documents shared ownership and contract generation. |
Makefile |
Adds the web-contracts target. |
internal/web/zone_test.go |
Updates quota-slot identity assertions. |
internal/web/state.go |
Integrates shared identity, sampling, status, and projections. |
internal/web/schedules.go |
Names the browser-safe scheduled-job projection. |
internal/web/projection_fixtures_test.go |
Generates and checks numerical fixtures. |
internal/web/dist/index.html |
References the rebuilt frontend bundle. |
internal/web/control_commands.go |
Exposes input-review metadata. |
internal/web/contracts_test.go |
Generates browser contracts and checks freshness. |
internal/ui/usage_reports.go |
Uses shared Usage distributions and scaling. |
internal/ui/shared_projection_test.go |
Tests status and label-independent quota identity. |
internal/ui/quota_tier_test.go |
Adapts tier tests to the extracted estimator. |
internal/ui/quota_api_eq.go |
Retains presentation around the shared estimator. |
internal/ui/quota_api_eq_test.go |
Updates estimator integration tests. |
internal/ui/monitor.go |
Uses shared attention predicates and sample fields. |
internal/ui/monitor_test.go |
Updates sampling and rendering tests. |
internal/ui/monitor_statusline.go |
Uses the shared session-status classification. |
internal/ui/monitor_filter.go |
Delegates recent-activity classification. |
internal/ui/monitor_context.go |
Delegates attention-label selection. |
internal/ui/monitor_context_test.go |
Updates sample fixtures. |
internal/ui/monitor_close_all_test.go |
Updates retained measurement fixtures. |
internal/ui/model.go |
Integrates estimator state, quota keys, and sampling. |
internal/ui/model_test.go |
Updates accounting coverage assertions. |
internal/ui/localisation_test.go |
Adapts confidence assertions to exported fields. |
internal/ui/documentation_screenshots_test.go |
Updates screenshot sample construction. |
internal/ui/benchmark_quota_test.go |
Adapts benchmark accounting tests. |
internal/schedule/quota.go |
Uses shared quota-definition keys. |
internal/schedule/identity_test.go |
Tests missing and changed quota windows. |
internal/quotagraph/graph.go |
Shares quota identity and numeric Pace projections. |
internal/quotaestimate/estimate.go |
Extracts process-local API-equivalent learning. |
internal/quotaestimate/estimate_test.go |
Tests estimator isolation, validation, and confidence. |
internal/codex/usage_reports.go |
Adds projected distribution fields. |
internal/codex/usage_categories.go |
Centralizes Usage aggregation, ordering, and scaling. |
internal/codex/types.go |
Defines stable quota identities and slot keys. |
internal/codex/session_state.go |
Centralizes observed session-state rules. |
internal/codex/session_samples.go |
Shares bounded token-interval sampling. |
internal/codex/session_projection_test.go |
Tests shared identity, status, Usage, and sampling. |
assets/screenshots/manifest.json |
Refreshes the screenshot source fingerprint. |
.gitattributes |
Marks generated browser artifacts. |
Review effort: Balanced
|
@copilot how effective was this PR in its goal? |
Quite effective against the stated goal: the main duplicated rules now have shared Go implementations or Go-generated contracts/fixtures, and the two reported regressions—stale renamed quota labels and negative corrections producing full-width bars—were fixed with targeted coverage. The reported maintained production code fell by 202 lines and embedded JavaScript by 1,275 bytes. The standalone binary grew by 8 KB (~0.05%), so this wasn’t a binary-size win; the stronger result is reduced divergence risk backed by broad parity and regression tests. |
|
@copilot can you find any remaining duplication or lack of functional consolidation within the project? |
Yes—there are a few residual candidates, though I found no obvious new correctness defect:
One deliberate functional gap is quota-health scoring: it exists in |
|
Addressed the remaining consolidation suggestions from #96 (comment) in 9609afb (
Validation: full core Go race/coverage suite, all 111 browser tests, FMA-enabled projection checks, frontend checks/build, go vet, and all seven README screenshot checks passed. The screenshots remain unchanged. As recommended, terminal-only quota-health scoring remains out of browser scope, and immediate UI validation stays separate from authoritative server-side validation at the security boundary. |

Why
The architecture audit found several business rules maintained separately by the terminal and web interfaces. Some had already diverged: mutable quota labels could collide, a missing primary allowance could pass scheduled-recovery coverage checks, an optional statusline could report
INPUT NEEDEDwhile working, and fractional Usage bars had different scales between renderers.This PR consolidates those rules at their source, removes redundant state and handwritten browser contracts, and keeps rendering and browser security boundaries separate. It introduces no runtime dependency, service, persistence, or new browser write capability.
Shared identity and session accounting
Interface-independent calculations
internal/quotaestimate; the TUI retains formatting, localisation, diagnostics and benchmark coordination. Remove its unused issues map. This does not add API-EQ estimates to the browser or change the estimator's confidence policy.Contracts and maintenance
make web-contractsand freshness tests. Numerical browser fixtures are generated by the actual Go projectors, so browser tests do not maintain another copy of the maths.Size measurements
Re-measured after successful CI, comparing final PR commit
9609afbwith default-branch baseline93b3fb9:Source counts now cover all tracked non-test Go files, including the root entrypoints, plus maintained Svelte/TypeScript under
web/src. They exclude generated contracts, tests and embedded bundles. This corrects the earlier narrower source-count scope: both totals now include 750 unchanged root-level Go lines; the net reduction remains 182 lines.Both binaries were freshly built with Go 1.27.3,
CGO_ENABLED=0,GOOS=linux,GOARCH=amd64,GOAMD64=v1,-buildvcs=false,-trimpath,-s -wand the same injected version0.27.1. This removes development/VCS metadata differences from the comparison.There is no standalone binary-size win. The measurable savings are in maintained source and browser JavaScript; the principal benefit is fewer independently maintained business rules. Generated contracts, numerical fixtures and additional regression tests make the overall diff larger despite the production-source reduction.
Validation
Final commit
9609afb: CI and Security completed successfully, including Linux, macOS, Windows, browser tests and both vulnerability scans.Go race and coverage suite across all packages, with the locale subprocess matrix run separately.
All 17 terminal locale cases under the race detector; English presentation snapshot unchanged.
Direct tests for the extracted estimator: 91.7% statement coverage, including account/plan/window changes, tainted intervals, evidence validation and confidence bounds. Existing TUI accounting integration tests retained.
Targeted identity, session-status, sampling, schedule-coverage, fractional/signed Usage, elapsed-cycle boundaries, reset rounding and historical warning-level regressions.
All 111 Playwright browser tests, including Go-produced Pace fixtures and fractional-bar scaling and signed corrections.
Frontend type/Svelte checks, formatting and production build;
go vet ./...; generated-contract and fixture freshness checks.make screenshots-check: all seven README terminal screenshots verified for v0.27.1.Controlled standalone builds and
git diff --check.Deliberately retained boundaries
Terminal/SVG geometry, colours, localisation, selection, transport lifecycles and browser-origin/write-authority protections remain interface-specific. No common abstraction is introduced for code that has genuinely different responsibilities.