feat(opencode): surface turn-level cache and spend telemetry - #649
Open
yzxcj797 wants to merge 4 commits into
Open
feat(opencode): surface turn-level cache and spend telemetry#649yzxcj797 wants to merge 4 commits into
yzxcj797 wants to merge 4 commits into
Conversation
…/metrics
The budget now debits the LlmCallUsage the harnesses meter, but an
operator planning which models to run qm on still had to join spend out
of session_llm_requests by hand: turn_metrics carried the cache columns
and no cost, no output tokens.
Harness turn results gain costUsage {outputTokens, costUsd} alongside
cacheUsage -- pi sums its per-call stats, claude reports the SDK's
running cost total with the fallback branch flagging unknown cost, and
the mock harness reports its deterministic usage. The orchestrator
lands both on TurnMetricSample, turn_metrics grows output_tokens and
cost_usd (ALTER TABLE ADD COLUMN IF NOT EXISTS, so existing deployments
migrate in place), and /v1/admin/metrics returns a spend block:
samples, turnsWithKnownCost, costUsdTotal, outputTokensTotal.
Follow-up to yc-software#586
codex accumulated full per-thread usage live but its turn result carried none of it, so codex turns landed in turn_metrics without the cache or spend telemetry pi and claude already report. The turn result now maps the same thread totals the recordLlmRequest flush uses, through one pure usageToTurnTelemetry helper: cacheUsage (cacheRead/cacheWrite/uncachedInput) and costUsage (outputTokens, costUsd). The codex SDK reports no cost, so costUsd stays 0 -- the same value already persisted to session_llm_requests for these calls. opencode still reports no turn telemetry: its usage is only fetched in the post-return flush, so wiring it needs a restructure noted for follow-up. Follow-up to yc-software#586
The mapping is harness-agnostic; the next harness to report turn telemetry imports it from the module that owns the result shape.
opencode's usage only existed on the per-capture llm rows flushed after the turn returned, so opencode turns landed in turn_metrics without the cache or spend telemetry every other reporting harness carries. The flush already awaited before the turn's promise resolved, so the happy path now runs it before building the result and sums each capture's usage into a turn accumulator (children keep their own); the result maps it through the shared usageToTurnTelemetry. Unlike codex, opencode's provider computes cost, so costUsage carries a real number. The finally-block flush stays for every non-happy path and is a no-op after the splice. Follow-up to yc-software#586
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Completes the harness coverage started in #645/#646: opencode's usage only existed on the per-capture
session_llm_requestsrows flushed after the turn returned, so opencode turns landed inturn_metricswith no cache or spend telemetry.Implementation
The flush was already awaited in
finallybefore the turn's promise resolved, so the happy path now runs it before building the result — no user-visible timing change, it only lets the result observe what the flush recorded. Each capture'susageFromInfois summed into a turn accumulator (child sessions keep their own; misaligned captures record without usage and contribute nothing). The result maps the totals through the sharedusageToTurnTelemetry(moved toharness.tsin #646). Unlike codex, opencode's provider computes cost, socostUsagecarries a real number. Thefinallyflush stays for every non-happy path and is a no-op after the splice.Stacked on #646 (which is stacked on #645) — merge order: #645 → #646 → this.
Testing
cacheUsage {cacheRead: 50, cacheWrite: 10, uncachedInput: 100}andcostUsage {outputTokens: 20, costUsd: 0.0353}from the same fixture that feeds the llm row. Passes where the fake-sidecar harness tests run; on this Windows host those tests fail identically with and without the change (spawnof a shebang script), so the local differential for them is inconclusive —tsc --noEmitis clean and the accumulation is pure data flow.Follow-up to #586
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.