fix(opencode): show reported context usage for OpenRouter threads #22761
Showing 4 changed files with 168 additions and 0 deletions (loading 3).
x
168 additions and 0 deletions+168-0apps/server/scripts/opencode-openrouter-usage-fixture.tsAddedView file
@@ -0,0 +1,168 @@
+#!/usr/bin/env node
+// @effect-diagnostics nodeBuiltinImport:off globalConsole:off globalDate:off - A dependency-free stub so the fixture runs against any checkout, including the pre-fix base.
+/**
+ * Credential-free OpenRouter stand-in for reproducing OpenCode context usage
+ * (issue 8388) through a real OpenCode CLI and a real T3 dev server.
+ *
+ * Every chat completion streams one reply and a final usage chunk of
+ * prompt 12000 (1000 cached) and completion 32, so OpenCode reports input
+ * 11000 + cache read 1000 + output 32 and the composer meter should read
+ * 12032 of the 128000 context limit set in the generated config.
+ *
+ * node apps/server/scripts/opencode-openrouter-usage-fixture.ts prepare <dir> [--port 18988]
+ * Writes <dir>/project/opencode.json, isolated XDG dirs, and <dir>/opencode,
+ * a wrapper that runs `opencode` (or $OPENCODE_BIN) with that config.
+ * node apps/server/scripts/opencode-openrouter-usage-fixture.ts serve [--port 18988]
+ * Serves the stub on 127.0.0.1 and logs one JSON line per request.
+ *
+ * Reproduce, from a worktree seeded with `vp run migrate-dev-db`:
+ * 1. `prepare` then `serve`, and `vp run dev`.
+ * 2. Settings > Providers > OpenCode: enable, set binary path to <dir>/opencode.
+ * Settings > General > Legacy features: enable Context window indicator.
+ * 3. Add project <dir>/project, start an OpenCode thread on model
+ * openrouter/z-ai/glm-5.3-flash, and send any message.
+ * 4. Extract the thread's provider turns:
+ * node apps/server/scripts/t3-sqlite-state.ts query --base-dir .t3 --sql \
+ * "select status, json_extract(payload_json,'$.tokenUsage') as tokenUsage, \
+ * json_extract(payload_json,'$.turnTokenUsage') as turnTokenUsage \
+ * from orchestration_v2_projection_provider_turns where thread_id='<threadId>' order by ordinal"
+ *
+ * Quick check without T3: `<dir>/opencode run --model openrouter/z-ai/glm-5.3-flash --format json hi`