Repository navigation
feat(cost): track OpenCode local history in the cost store (#4757) - #4786
Conversation
Structural foundation for the #4752-approved adapter direction: a versioned, backward-compatible source discriminator on the files scan_state JSON (absent = native Codex, so upgrade needs no migration), a typed versioned OpenCodeSourceCheckpoint payload for the adapter's table windows, part continuation, updated-watermarks and reconciliation progress, and source-aware restore dispatch so adapter records never decode as Codex JSONL state. Tests cover the legacy-payload upgrade path, checkpoint version gating, and a discriminated round trip through upsert/readSnapshot.
Stage 1+2 of the #4752 checklist. The adapter decodes OpenCode assistant turns inside short read-only source transactions and commits usage rows, per-file day aggregates, global deltas and its typed checkpoint atomically through applyOpenCodeLocalHistoryPass (baseline revalidation, rollback on stale). Turns keep hash-stable identities over <table>:<sourceID>; windows walk by exclusive rowid boundaries pinned by their own SELECT with raw-lookahead exhaustion. Explicit regressions for the three #4744 cursor failures: invalid rows consume budget without starving older valid rows, oversized messages checkpoint a strict (messageRowid, partRowid) continuation and resume mid-message without re-emitting, and appends after a snapshot stay eligible because boundaries never come from a later MAX(rowid). Tests: 5 adapter tests + 4 checkpoint seam tests, all passing.
Upstream UPDATEs rows in place while streaming, so the pass now walks time_updated watermarks per table and re-evaluates the containing message (or session row), replacing the prior contribution at the same identity: old payloads resolve via the store's readUsagePayloads point lookup, their negation folds into the global merge, and per-file aggregates recompute as absolute stored+new-old values. A pure delete (a session row that becomes excluded or invalid) removes its footprint. Late step-finish parts swap the fallback row for the part. Tests: covered-code mutation, late parts, and session updates all pass (8 adapter tests total).
Stage 4 of the #4752 checklist. The adapter's contribution now lands in the Codex daily report with provenance resolved at read time: - sourceRecorded marker on ModelBreakdown (report-model case only, never persisted in store enums); merge keeps recorded and priced breakdowns of the same model apart via provenanceKey, so a recorded /bin/zsh subscription day can never fold into a same-named priced estimate. - Recorded zero stays a PRESENT zero in costUSD — presence is the signal, per the recorded-cost-as-is contract. - CostUsageStoreAccess.sharedScanStore gives the adapter the scanner's own shared store actor (one persistence owner); the pass runs in the fetcher's async seam beside the synchronous scan executor. - Options.includeOpenCodeLocalHistory (default off) flows from SettingsStore.openCodeLocalHistoryEnabled through UsageStore into the fetcher; the ambient gate excludes any scan with an explicit codexSessionsRoot or CODEX_HOME override. - Preferences toggle under the Codex provider detail. Tests: 4 new stage-4 tests (recorded-/bin/zsh conversion + provenance, merge keeps recorded/priced apart, ambient gate matrix, default-off). All 12 adapter tests pass; full-suite regressions are unrelated environment clusters (keychain access absent in this shell, one keyboard-layout- dependent shortcut test, load-timing flakes that pass in isolation).
Merge current main and finish the approved shared-store adapter. Preserve native persistence ownership, recorded zero costs, source provenance, and ambient-only publication. Reconcile updates and deletions with resumable raw-row sweeps, bounded payload/page work, and cancellation. Keep cached spend visible during partial window refreshes without turning unknown token counts into zero. Add regression and scale coverage, native parser compatibility, translations, documentation, and contributor credit. Validation: 693 focused tests; 286 merged-main integration tests; 43 final source tests; make check with zero lint violations. The focused source run keeps SQL VM work flat across synthetic 1k/10k/100k histories. Fixes #4752 Refs #4757 Co-authored-by: Daniel930902 <daniel0902life@gmail.com>
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ode-50-finish # Conflicts: # CHANGELOG.md
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0723cce20b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| var isAssistant: Bool { | ||
| self.providerID == "openai" && self.role == "assistant" |
There was a problem hiding this comment.
Include v2 compaction requests in imported usage
When an OpenCode v2 database contains an OpenAI-backed session_message with type = 'compaction', this predicate rejects it even when it has valid recorded costs and tokens. decodeRow maps the table's type column into role, so these requests are silently omitted and the scan can still claim complete coverage. The existing OpenCodeGoLocalUsageReader.sessionMessageUsageSQL and OpenCodeGoV2LocalUsageReaderTests explicitly handle this same schema's compaction costs. Accept compaction records for the v2 source while retaining the assistant-only restriction for legacy messages.
Useful? React with 👍 / 👎.
|
Codex review: blocked before merge. What this changesAdds optional OpenCode local usage ingestion into the existing ambient Codex cost ledger. Example: Enable OpenCode local history with an OpenAI-backed request recording $0 in opencode.db.
Review scores
ProductKind: Feature · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Blocked before merge - 2 items remain Keep this maintainer-authored PR open: it implements an approved capability absent from main, but a correctness finding blocks a clean verdict. Priority: P2 Before merge
Findings
Tests
Agent review detailsHow this fits togetherThe adapter reads OpenCode SQLite usage metadata, persists bounded reconciliation progress and contributions through CostUsageStore, and merges source-labelled totals into ambient Codex reports. flowchart LR
A[OpenCode database] --> B[Read only adapter]
B --> C[Shared CostUsageStore]
D[Native Codex scanner] --> C
C --> E[Scoped read view]
E --> F[Ambient Codex history]
Technical reviewBest possible solution: Land the approved adapter with complete supported v2 usage accounting and demonstrated preservation of existing release data. Do we have a high-confidence way to reproduce the issue? Source inspection shows that an OpenAI-backed v2 compaction row with recorded cost 2 is rejected before ingestion; no executable reproduction was run. Is this the best way to solve the issue? The owner-approved adapter appropriately reuses the existing persistence and read-view owners; its v2 eligibility filter needs a narrow correction. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against c3d8ca5d7a82. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceAcceptance criteria:
What I checked:
Likely related people:
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed October 26, 2026, 9:53 AM ET / 13:53 UTC. |
Finishes #4757 by @Danielweng0902, implementing the design approved in #4752: OpenCode's local history (
opencode.db) feeds the existing cost-usage store as an opt-in source for the ambient Codex ledger.This PR exists only because GitHub denies maintainer pushes to the contributor fork (
Danielweng0902/Win-CodexBaris a fork of a fork, so "allow edits by maintainers" does not reach it). Daniel's four commits are preserved unchanged in this branch's history; the maintainer work is one commit on top plus a main merge.What changed relative to #4757:
Proof: focused performance/store/settings/gate suites (693 tests) and the merged-main integration run (286 tests) pass,
make checkpasses, and an isolated Codex review through P2 is clean. PR CI is the broad gate.Closes #4752.
Supersedes #4757.