Skip to content

feat(workboard): measure each run's turn-end context against the smart zone - #51

Merged
tomorrowflow merged 3 commits into
mainfrom
feat/workboard-smart-zone
Oct 13, 2026
Merged

tomorrowflow merged 3 commits into
mainfrom
feat/workboard-smart-zone

Conversation

@tomorrowflow

Copy link
Copy Markdown
Owner

Problem

Draft D16 sizes every ticket to the smart zone (about 150k tokens, roughly 20% of the window). It also asks for that to be measured, as context use at turn end per run plus breaches, and fed into the trust KPIs and the retro. Workboard recorded nothing about context, so a class with oversized tickets was invisible.

Solution

  • Recording. When a run ends, syncWorkboardAgentEnded reads the context of the run's last model call from the agent_end messages. It stamps that value as contextTokens on every card attempt carrying the run's runId, via recordAttemptContext. The stamp happens after the lifecycle write, so an attempt linked only by session key has already received its run id.
  • How the value is read.
    • Only the last assistant message after the final user prompt counts, because agent_end messages also carry the session's earlier runs.
    • A run that failed before replying stays unmeasured.
    • When usage carries contextUsage.promptTokens (Codex sums usage across the turn's calls), that value wins. Otherwise the value is input + cacheRead + cacheWrite.
  • Storage. A nullable column workboard_card_attempts.context_tokens, added with the existing ensureColumn pattern. The rebuild of an attempt from its execution now keeps the field, where before it dropped it on the next update.
  • Projection. Trust counts gain contextMeasured and smartZoneBreaches (runs above SMART_ZONE_TOKENS, 150,000). The weekly bucket gains medianContextTokens, and the result carries smartZoneTokens.

Impact

  • Update behavior. An existing workboard database gets the new column on open, which is an additive ALTER TABLE ADD COLUMN on the STRICT table. A fresh database creates it in CREATE TABLE.
  • Rollback. An older Gateway reads and writes by named columns, so it ignores the column; values it rewrites are lost.
  • Existing attempts stay unmeasured. No config changes.
  • Write cost. One card update per run that has a usage figure and a matching Workboard attempt, skipped when the value is unchanged. Agent runs that aren't Workboard cards cost one pass over their messages and no writes.
  • Known limit. A Claude Code run that compacted mid-run reports its context after the compaction, since the CLI hands over only its last call's usage.

Evidence

  • lifecycle-sync.context.test.ts runs through the real node-ticket start and agent_end path:
    • History from an earlier run in the session is ignored.
    • A run with no reply records nothing.
    • Codex contextUsage wins over summed usage.
    • A prior-schema database without the column gets it on reopen. That test fails with no column named context_tokens when ensureColumn is removed.
  • trust-kpis.test.ts covers breaches, unmeasured legacy attempts, and the weekly medians.
  • pnpm test extensions/workboard: 32 files, 458 tests pass. The new file runs in about 11 s wall with --maxWorkers=1.
  • tsgo:extensions, tsgo:extensions:test and tsgo:ui show no new errors (only the existing qa-lab crabbox-provider module errors). oxlint and oxfmt are clean.
  • A fresh review found that history leaked into the measurement, that Codex usage is a sum, that session-linked attempts were missed, and that unchanged values were rewritten. All four are fixed in the second commit.

🤖 Generated with Claude Code

@tomorrowflow
tomorrowflow merged commit 7da29d6 into main Oct 13, 2026
1 check passed
tomorrowflow added a commit that referenced this pull request Oct 13, 2026
* style(workboard): bring store-card-helpers back under the 700-line cap

#51 and #52 each stayed under the cap and together reached 701 lines on
main. The attempt rebuild carries contextTokens in one line.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(workboard): run git diff --check at node-ticket import

Both of dev's defect verdicts in the Phase 4 window were a failing
`git diff --check` that the session reported done anyway. The host clone
now runs it over the imported range before it creates the ticket branch.
A failure adds a failed `import check` proof entry and blocks the card
with the command and output first, so the re-run session reads them and
fixes them with a new commit in the same node worktree.

A rework round checks only its own commits: the rework record gains
`startCommit`, the branch tip the round started from, so a reviewer's
commits or an "Update branch" merge of main never block the session.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(workboard): leave a failed import's pending record to the re-run

A re-run claims the card while it still carries the pending handoff of
the failed import, and the handoff sweep fires on that claim. It then
imported the old head again: an import-check failure blocked the re-run
before its session could fix it, and a generic failure jumped to review
mid-start. A pending record older than the card's claim now belongs to
the start that replaces it.

The rework round's starting tip is checked as a commit id before it
reaches host git. The start queue tests move to their own file to keep
the handoff tests under the line cap.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant