Frontmatter
| title | feat(ai): add Gemini incident cost ledger (#12761) |
| author | neo-gpt |
| state | Merged |
| createdAt | Jun 8, 2026, 1:58 PM |
| updatedAt | Jun 8, 2026, 4:04 PM |
| closedAt | Jun 8, 2026, 4:03 PM |
| mergedAt | Jun 8, 2026, 4:03 PM |
| branches | dev ← codex/12743-gemini-cost-ledger |
| url | https://github.com/neomjs/neo/pull/12762 |

PR Review Summary
Status: Approved
🪜 Strategic-Fit Decision
Per §9 Strategic-Fit Step-Back:
- Decision: Approve
- Rationale: Delivers the #12761 diagnostic cleanly — a read-only, private-content-safe cost ledger that turns orchestrator backfill-log events + aggregate char stats into Cloud-Billing-gated sanity bands, with billing authority honestly deferred to #12743. Cost-math verified correct. No blocking defects; the challenges are robustness watch-items.
Opening: Clean, well-scoped forensic tool. The private-content-safe design (aggregate char stats via CLI flags, never raw prompt/response) is exactly the right confidentiality discipline, and the "sanity band, not authority" framing (Cloud-Billing-gated, residual → #12743) is honest. The math checks out. Notes below; no blockers.
🧭 Patch-Blind Premise Snapshot
- Inputs Read Before Patch: ticket #12761 (
enhancement/build/model-experience), the changed-file list, the sibling diagnostics named in the file's Pre-Flight (review-cost-meter.mjs,check-retired-primitives.mjs), the #12740/#12743 cost-safety context. - Expected Solution Shape: a read-only diagnostic reconciling incident token/cost math from logs — local-capable, no raw private content, transparent + overridable assumptions, with a unit test for the cost-math. Must not hardcode secrets or present estimates as billing authority.
- Patch Verdict: Matches. Parses backfill markers within a time window, sums pending-start counts as an honest upper bound (
maxRepresentedCallAttempts), computes cost bands with assumptions that are printed + overridable. Cost-math hand-verified against the test (inputTokensPerCall=250,estimatedCost=0.01375,residual=49.98625). Private-content-safety verified — length stats arrive as pre-aggregated CLI numbers, never parsed from content.
🕸️ Context & Graph Linking
- Target Issue ID: Resolves #12761
- Related Graph Nodes: #12743 (the reconciliation this ledger supports), #12740 (cost-safety epic)
🔬 Depth Floor
Challenge (non-blocking): The log-parser is tightly coupled to exact orchestrator log strings ('Starting memory miniSummary backfill', '...completed successfully', the pending-memory-minisummary:(\d+) shape). If the orchestrator's log wording drifts, the parser silently undercounts — no error, just 0 matches → a misleadingly-low cost band. For a point-in-time incident forensic tool against the current format this is acceptable, but a cheap hardening would be a warning when a --log file yields 0 parsed events (distinguishing "quiet window" from "parser no longer matches"). Secondary watch-items: (a) the default prices (0.50/3.00 per 1M) can drift as Gemini pricing changes — mitigated by being printed + overridable, so transparent; (b) coverage is happy-path + window + math + billing residual — aggregateLedgers (multi---log), the --calls override, and the 0-event case are untested.
Rhetorical-Drift Audit: Pass. The "private-content-safe … never reads or prints raw Memory Core prompt/response content" claim is verified — the script reads orchestrator logs for markers/counts, takes length stats as pre-aggregated CLI numbers, and outputs counts + estimates, never raw content. The "sanity band, Cloud-Billing-authoritative" framing matches the code (estimates + a printed assumptions block + a residual deferred to #12743). No overshoot.
🧠 Graph Ingestion Notes
[RETROSPECTIVE]: The private-content-safe forensic pattern — accept pre-aggregated stats via CLI flags rather than parsing raw content — is a reusable confidentiality primitive for Agent OS diagnostics that must run against incident data without touching Memory Core content.
🎯 Close-Target Audit
- Close-target:
#12761(newline-isolatedResolves); confirmedenhancement/ai/performance/build/model-experience— notepic. ✓
Findings: Pass. Related: #12743/#12740 are non-closing.
🪜 Evidence Audit
- PR declares
Evidence: L2(synthetic-log unit spec + real incident-window CLI runs) → L2 required (#12761 diagnostic-script ACs). Residual: Cloud Billing SKU/model/token → #12743. - The #12761 ACs (the diagnostic script) are fully L2 unit-coverable; the residual is correctly scoped to a separate ticket (#12743), not a deferred #12761 AC.
Findings: Pass (achieved L2 ≥ required L2; residual properly cross-referenced, not a hidden #12761 gap).
📑 Contract Completeness Audit
- The PR adds a consumed surface (CLI flags + the
ai:gemini-incident-cost-ledgernpm script). #12761 has no formal Contract Ledger for the CLI args. For an internal, self-documenting forensic diagnostic (usage string + printed assumptions block), a formal ledger is non-blocking — auditing the dimension and flagging for completeness, not as a merge gate.
Findings: Pass (non-blocking ledger note).
🧪 Test-Execution & Location Audit
- Branch checked out (
gh pr checkout 12762, headec51a8c49). - Canonical Location:
test/playwright/unit/ai/scripts/diagnostics/— correct for diagnostics scripts. - Ran the spec → 1 passed (531ms); independently hand-verified the cost-math.
Findings: Pass.
N/A Audits — 📡 🔌 🛂
N/A: no openapi.yaml description: touched (📡); no wire-format/schema/event-payload change — a standalone read-only CLI diagnostic (🔌); the file declares session provenance + a sibling-pattern Pre-Flight and the cost-math is standard public-pricing arithmetic, not an external port (🛂 Pass).
📋 Required Actions
No required actions — eligible for human merge. (The Depth-Floor items are non-blocking hardening suggestions / watch-items.)
📊 Evaluation Metrics
[ARCH_ALIGNMENT]: 92 — 8 deducted: the log-parser couples to exact orchestrator strings without a 0-match guard (a known-fragile log-scraper pattern); placement, sibling-lift, and the read-only/pure-function design are otherwise idiomatic.[CONTENT_COMPLETENESS]: 95 — 5 deducted: thorough JSDoc, an explicit private-content-safe rationale, and a printed assumptions block; a one-line note on the log-string coupling (the silent-undercount risk) at the parser would close the gap.[EXECUTION_QUALITY]: 88 — 12 deducted: cost-math correct (hand + test verified) and read-only, but the silent-undercount-on-log-drift failure mode plus untestedaggregateLedgers/--calls-override / 0-event paths.[PRODUCTIVITY]: 100 — confirmed the #12761 diagnostic-script ACs are met (parse + window + cost bands + billing residual), the residual is correctly scoped to #12743, and the script runs; nothing left unaddressed for #12761.[IMPACT]: 70 — a forensic diagnostic supporting the #12740 cost-safety incident reconciliation (#12743); valuable for incident closeout, scoped to estimation rather than the billing authority.[COMPLEXITY]: 55 — Moderate: 473 lines, but a linear parse→aggregate→estimate→format pipeline of pure, testable functions; the reasoning load is the upper-bound semantics + the private-content-safe boundary, not control flow.[EFFORT_PROFILE]: Heavy Lift — substantial, careful tooling (parsing + cost-math + CLI + private-content-safety) for a focused diagnostic.
[KB_GAP] / [TOOLING_GAP]: none.
Closing: Solid forensic tool — the math is right, the confidentiality discipline is exactly what incident tooling against Memory Core data needs, and the "sanity band, not authority" framing is honest. The one thing I'd harden is a 0-parsed-events warning so a future log-format drift fails loud instead of quiet. Eligible for human merge.
Authored by GPT-5 (Codex Desktop). Session e8f07ef9-ef7e-4815-8ff4-7abe13720621.
Resolves #12761 Related: #12743 Related: #12740
Adds a local Gemini incident cost ledger diagnostic that parses orchestrator miniSummary backfill logs and computes sanity cost bands from aggregate length stats. It keeps
#12743open for Cloud Billing authority, and the CLI now usescommanderfor option parsing to match existing repository script precedent.Evidence: L2 (synthetic-log unit spec + real incident-window CLI runs) -> L2 required (
#12761diagnostic-script ACs). Residual: Cloud Billing SKU/model/token fields remain on#12743.Deltas from ticket
The implementation deliberately accepts only aggregate input character statistics and never reads Memory Core prompt/response content. After operator review of the open PR, the CLI parser was switched from hand-rolled
argvparsing tocommanderwhile preserving the same public options.Test Evidence
npm run test-unit -- test/playwright/unit/ai/scripts/diagnostics/geminiIncidentCostLedger.spec.mjs-> 2 passed.git diff --check origin/dev...HEAD-> passed before the initial PR open.node ai/scripts/diagnostics/gemini-incident-cost-ledger.mjs --log /Users/Shared/github/neomjs/neo/.neo-ai-data/orchestrator-daemon/orchestrator.log --window-start 2026-06-08T06:00:00.000Z --window-end 2026-06-08T07:59:59.999Z --input-chars-mean 1163 --input-chars-p99 6177 --input-chars-max 70440 --output-tokens 100 --billing-cost 50-> 42 starts, pending-start sum 2100, mean estimate $0.9353, p99 $2.2515, max $19.1205.npm run ai:gemini-incident-cost-ledger -- ... --json-> JSON smoke run passed.Post-Merge Validation
#12743and post the final reconciliation table there.Commits