Add coding-plan usage tracking and fix OpenCode backend issues#157
Add coding-plan usage tracking and fix OpenCode backend issues#157DavidSegun wants to merge 4 commits into
Conversation
Orchestration backends (Claude Code, Codex, OpenCode + Z.AI GLM) run on
subscription plans that meter usage over a rolling ~5-hour window, but
qmax-code never tracked it, so it was unclear how much was used or when the
plan would stop.
Add a time-based PlanWindow tracker (internal/api/planwindow.go) that opens on
the first orch turn, accumulates turns/tokens, rolls over after the window
elapses, and can be marked exhausted from a provider limit hit. Surface it in
the input status bar and a new /plan command (also summarized in /status and
/cost). Window length is configurable via plan_window_hours (default 5).
Also close two backend blind spots that fed the same problem:
- OpenCode and Codex now report per-turn token usage (previously CC-only),
feeding both session cost and the plan window.
- OpenCode `{type:error}` events were being silently dropped; they are now
surfaced, and a 429 / usage-limit refusal is detected as a plan-limit hit,
reading the reset time from rate-limit headers when present.
Build + vet clean; new planwindow unit tests pass. Codex/OpenCode token-field
names are parsed defensively pending confirmation against a live successful run.
…er tests Verified against live `opencode run --format json` output (opencode 1.0.105): successful turns emit a trailing status-code-less UnknownError (an internal schema-validation gripe), and this version emits no token-usage event in the stream. Surfacing every error event would therefore spam a scary line after every good turn, so only errors carrying an HTTP status code (auth/quota/5xx) and detected usage-limit hits are shown by default; status-code-less events appear only in verbose mode. Adds opencode_stream_test.go driving the parser with the real captured streams: success (renders answer, no false limit, no invented tokens), a 429 limit hit (flags the window + reads Retry-After), a 403 subscription error (surfaced but not a limit), and forward-compat token extraction for both field shapes.
… supports it
opencode 1.x removed the `run --auto` flag and now governs tool approvals
through the config `permission` block. qmax-code passed --auto unconditionally,
so on current opencode every turn failed: opencode printed its usage and exited
1 with no output ("opencode exited with error: exit status 1"), making the
OpenCode backend (incl. Z.AI GLM coding plans) unusable.
Probe `opencode run --help` once and only append --auto when the flag is still
advertised, preserving behavior on older opencode while working on 1.x. The
managed config's permission block already enforces the standard/unattended
policy, so nothing is auto-approved that was not before.
Verified against opencode 1.0.105: without --auto the run produces the answer
and the turn completes.
On Windows, npm installs opencode as a `.cmd` shim. Go's os/exec runs `.cmd` files through cmd.exe, which swallows the `--` end-of-flags separator and drops the positional message that follows it — opencode then aborts with "You must provide a message or a command" and exits 1, so no OpenCode turn (including Z.AI GLM / OpenRouter coding plans) can run on Windows. Pass the message without `--` on Windows (sanitizeCCUserPrompt already strips control bytes, so a lone positional is safely taken as the message); keep `--` on other platforms where it protects a message starting with "-". Verified via a Go exec repro: with `--` the message is dropped, without it the turn runs.
Sigilix OverviewEffort: 4/5 (large) Quality gates
Summary — latest pushAdds rolling coding-plan usage window tracking (5-hour default) surfaced via a new /plan command and the status bar, and fixes several OpenCode and Codex backend issues. OpenCode and Codex now report per-turn token usage and surface provider errors (especially 429 plan-limit responses) instead of silently dropping them. It also fixes a Windows compatibility bug where the Important files
Sequence diagramsequenceDiagram
participant User
participant REPL
participant Agent
participant Provider
User->>REPL: /plan or turn
REPL->>Agent: Run(message)
Agent->>Provider: API request
Provider-->>Agent: Stream (tokens, error, 429)
Agent->>Agent: inspectCodexEvent / handleOCError
Agent-->>REPL: LastTurnStats, LastPlanLimit
REPL->>REPL: PlanWindow.Record / MarkExhausted
REPL-->>User: Status bar update
Confidence: 4/5The feature is well-structured with clear separation of concerns, but the plan-limit detection relies on broad heuristic string matching and header parsing that lack visible unit tests in the diff, which could lead to false positives or misparsed reset times.
Suggested labels: Suggested reviewers: @Desperado
|
❌ QualityMax Pipeline
Powered by QualityMax — AI-Powered Test Automation |
Review findings — request changesI found four correctness issues in the new plan-tracking paths:
Verification
|
Adds rolling coding-plan usage tracking with a new /plan command, status bar updates, configurable window duration (plan_window_hours), and automatic handling of plan exhaustion.
Also fixes several OpenCode issues discovered during implementation:
Testing