Skip to content

fix(statusline): follow the largest thread when no thread is main - #714

Merged
amiddavid merged 2 commits into
mainfrom
fix/statusline-main-thread
Oct 25, 2026
Merged

amiddavid merged 2 commits into
mainfrom
fix/statusline-main-thread

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

The status line now follows the main conversation when no thread is marked main. It picks the largest thread, and it prefers a thread that Claude Code did not name as a sub-agent. Before, it followed the newest thread, which was often a sub-agent.

Fixes #709.

What we found

  • statusline.py (_main_cs) uses the thread with main: true. With none, it used the top-level fields of /api/cachestate. Those fields follow the session's newest thread.
  • With no thread marked main (Thread tracker: a resumed session loses its main thread id #708), every sub-agent turn moved the context bar and the timer to the sub-agent.
  • Plain "largest thread" is not enough. Live session 327ef650, read-only from /api/cachestate:
    • p:7754bf728db9, the real main conversation: 120,952 tokens.
    • a:investigate-703, a sub-agent: 308,143 tokens.
    • a:fix-708-709, a sub-agent: 132,492 tokens.
  • Claude Code names every sub-agent and teammate thread with its agent-id header, and the proxy gives those threads an a: id. The main conversation never has an a: id.

What changed

  • _main_cs: when no thread is marked main, the status line follows the largest thread by prefix_tokens without an a: id.
  • If every thread has an a: id, it follows the largest one.
  • A thread marked main still wins, as before.

How it was tested

New test TestStatuslineFollowsTheLargestThreadWhenNoneIsMain. No thread is marked main, and the newest thread is a 70k sub-agent. 2 cases:

  • A 500k p: thread and an 800k a: thread: the bar must read 50%.
  • Only a: threads: the bar must read 80%.

All runs on the eval box.

Run Result
New test, statusline.py from origin/main FAIL: both cases read ctx 7% (the live symptom)
New test, this fix PASS (the timer also follows the chosen thread: 4:00, not 4:55)
Mutation 1: the old fallback (top-level fields) FAIL: both cases read ctx 7%
Mutation 2: largest thread, no a: rule FAIL: case 1 reads 80% (the sub-agent)
go test -race ./context-guru-plugin/... PASS, except 3 tests that are not this change (see below)

The 3 other failures:

  • TestInstallRefusesAnUnverifiedDownload and TestInstallReportsPATHFromTheSourceFallbackToo always fail on the box.
  • TestUninstallDropsAStaleRecordEvenWhenNothingElseChanged logged "Address already in use" (a port clash on the shared box). It passes alone on origin/main, and 3 of 3 times alone on this branch.

Data dependence

No change. The status line reads thread ids and token counts from /api/cachestate: metadata only. It works the same at each storage level. docs/data-dependence.md needs no change.

Technical notes

When no thread in /api/cachestate is marked main, the status line used
the top-level fields. Those follow the session's newest thread, so a
70k sub-agent turn made a conversation at 50% read `ctx 7%`, and the
timer followed the sub-agent too.

Now the status line follows the largest thread by prefix size that
Claude Code did not name as a sub-agent (`a:` ids). The largest `a:`
thread counts only when no other thread exists. A sub-agent can be
larger than the main conversation: on the live session, `a:` 308k
against the main conversation's 121k.

Fixes #709

Assisted-By: Claude
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.310 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 4642567c235a blocking

Verdict: blocking

Findings

  1. context-guru-plugin/scripts/statusline.py:1037 — the thread swap leaves next_warm_usd / next_cold_usd on the newest thread, so the next segment quotes the sub-agent's prices
    • What fails: no thread is marked main (#708). The newest touch is a 70k sub-agent; the fallback picks the 500k main conversation on its p: id. The merge swaps prefix_tokens, rewrite_usd, model, last_ts, end_ts, ttl_s and priced, but not next_warm_usd / next_cold_usd. _cache_view (statusline.py:1201-1202) reads those two from the merged dict, so --show=next renders the sub-agent's next-request prices beside the main thread's clock, context bar and rewrite_usd. The line mixes two threads' numbers. Before this PR the no-main path returned cs unchanged, so at least all fields described the same (wrong) thread; after it, they disagree.
    • Proof: the server prices every thread itself — dash/cachestate.go:413-414 sets NextWarmUSD / NextColdUSD per ThreadCache, and the top-level fields at dash/cachestate.go:409 describe the session's newest cache row. The merge key list at statusline.py:1037 omits both keys. The code's own fallback (if cold_usd is None: cold_usd = usd, statusline.py:1203-1204) shows cold_usd is meant to track the merged thread's rewrite price; a present-but-stale top-level value silently wins over it.
    • Fix: add "next_warm_usd" and "next_cold_usd" to the copied keys in the loop at statusline.py:1037. The chosen thread already carries its own pair. A golden case with --show=next and no main thread would pin it.

Non-blocking

  • Issue #709 proposes two things. This PR does item 1 (follow the largest thread). Item 2 ("if a subagent is shown, the status line says so") is not done: when only a: threads exist, the line shows the largest sub-agent's numbers with no mark. The new test codifies that behavior, so it looks deliberate — but the issue stays half open.
  • The fallback can pick a side request, not the true main conversation. Side requests (p: ids) share the main prefix and add one turn (internal/thread/thread.go:222-231), so their prefix_tokens can exceed the main thread's by one turn. The context bar is then off by that turn only. Acceptable for a heuristic.
  • The a: test trusts Claude Code's agent-id header. A sub-agent that never sends it gets a p: id and counts as "not a sub-agent". The docstring scopes the rule to Claude Code, which sends the header on every sub-agent request (proxy/thread.go:11-15). Fine as documented.

What I checked

  • The full diff (one commit): statusline.py _main_cs fallback and the new golden test.
  • The fallback's sort key: (not id.startswith("a:"), prefix_tokens). Non-a: threads win over larger a: ones; ties break by size. This matches the docstring and both test cases.
  • Thread id scheme in internal/thread: "" is primary (server sets main: true only there, dash/cachestate.go:207), a: marks header-named sub-agents, p: marks fingerprint ids — including the main conversation after a resume (#708). The heuristic fits the failure it fixes.
  • Robustness: non-dict thread entries are filtered; a missing threads key, an empty list, a non-list, and cs=None all keep the old return-cs path. The max key cannot raise.
  • Downstream consumers of the merged dict: _cache_view (clock, tokens, prices), _ctx_numbers / _ctx_segment (bar math: 500k/1M → 4 of 8 cells, 50%; 800k/1M → 6 of 8, 80% — both match the test's wanted strings). held_until_ms and keepalive are swapped or cleared correctly. _agent_rows / _agent_ids read the raw cs before _main_cs runs, so the agents segment is unaffected.
  • The golden test's helpers: runStatusline sets NO_COLOR=1, so the plain substring assertions match the rendered line; cgStub / routedEnv match the existing tests' shape.
  • docs/data-dependence.md: not needed — the change reads only metadata the status line already reads (thread ids, token counts); no component or storage level changes.
  • Issues #708 and #709, to confirm the fix matches the reported failure.
  • No Go tests were run (live tests not approved); conclusions are from reading the code only.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.310 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 4642567c235a approve

Verdict: approve

Findings

None.

Non-blocking

  • context-guru-plugin/scripts/statusline.py:1033 — the fallback can follow a p: side thread instead of the conversation. A side request (a title, the auto-mode classifier) extends the conversation's prefix plus its own message, so it can be the largest non-a: thread. Example: a compaction shrinks the conversation, and a pre-compaction side thread stays larger for up to 2 h (the listing window). The bar then overstates the context and the countdown follows the side entry. Display only, and the stated rule accepts it. A tie-break on last_ts (prefer the most recent among near-equal) would narrow it.
  • context-guru-plugin/scripts/statusline.py:1037 — the merge swaps prefix_tokens, rewrite_usd and priced, but not next_warm_usd / next_cold_usd. Those stay priced on the newest thread's model and prefix, so the "next request cost" can disagree with the thread now followed. Pre-existing in the marked-main path; the fallback only widens when it fires.
  • context-guru-plugin/statusline_golden_test.go:309 — the new test does not check the exit code. The other golden tests fail on a non-zero exit. Add if code != 0 { t.Fatalf(...) } for consistency.

What I checked

  • Read the full diff (2 files) and docs/design.md.
  • Traced the thread id conventions on the server: internal/thread/thread.go gives "" to the main thread, a:<id> to a header-named sub-agent, p:<fingerprint> to a side request or fork. dash/cachestate.go:207 marks Main only for id "". So "no thread is main" means the proxy lost the main id, and the a: / not-a: split in the fallback matches what the server can name.
  • Verified the marked-main path is unchanged: the filter step keeps every prior test's inputs (all existing thread lists include a "" main), and _main_cs returns cs as before when no thread is main and no fallback candidate exists.
  • Ran _main_cs on the test's exact data: case 1 picks p:main (500k, not the larger a:big 800k), case 2 picks the largest a: thread (800k), a marked-main thread still wins, junk entries in threads are ignored, ties are deterministic (first listed).
  • Ran statusline.py end-to-end (python3, local stub, --layout=balanced) on both new test cases. Output: case 1 cache ● █████░ 4:00 │ ctx ████░░░░ 50%, case 2 cache ● █████░ 4:30 │ ctx ██████░░ 80%. Both contain the test's want strings. The cache clock follows the chosen thread, not the newest sub-agent.
  • Checked the old behaviour fails the new test: at base, no main thread means _main_cs returns cs unchanged, so the bar reads 7% (70000/1000000).
  • python3 -m py_compile passes on the script.
  • Data dependence: the change reads thread ids and token counts from /api/cachestate only. No new stored-data dependence, so no docs/data-dependence.md update is needed.
  • Fail-open: this is status-line rendering only. No request path, no offload, no reversibility concern.

The thread swap copied the main thread's clock, size and rewrite price,
but not `next_warm_usd` and `next_cold_usd`. So `--show=next` quoted the
newest sub-agent's prices beside the main thread's other figures.

The swap now takes both prices from the followed thread. With no prices
on the thread entry (an older server), it drops the top-level pair, and
the cold price falls back to that thread's `rewrite_usd`.

The new golden test also checks the exit code, as the others do.

Part of #709

Assisted-By: Claude
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Round 1 replies. New head: 7128221c31b7028f953f0a9db9118acd9d8f197d.

Reviewer 1, finding 1 (blocking): fixed

_main_cs now copies next_warm_usd and next_cold_usd from the followed thread, as it does for held_until_ms. If the thread entry has no prices (an older server), the swap drops the top-level pair. Then _cache_view falls back to that thread's rewrite_usd for the cold price.

New test TestStatuslineNextCostFollowsTheChosenThread (--show=next, no main thread, the newest thread is a sub-agent with $0.01 / $0.05):

Run Result
Previous head 4642567c FAIL: next $0.01 warm · $0.05 cold (the sub-agent's prices)
Mutation: copy held_until_ms only FAIL: same output
This head, thread has prices PASS: next $0.20 warm · $2.50 cold
This head, thread has no prices PASS: next $1.80 if cold (the thread's own rewrite_usd)
go test -race ./context-guru-plugin/... on the eval box PASS, except the 2 tests that always fail on the box (TestInstallRefusesAnUnverifiedDownload, TestInstallReportsPATHFromTheSourceFallbackToo)

The fix also applies when a thread is marked main. Reviewer 2 noted that this gap was older than this PR.

Non-blocking points

  • Issue Status line follows the newest thread, so it shows a subagent and not the main conversation #709 point 2 (a label when a sub-agent is shown), both reviewers: not in this PR, on purpose. A label is a UI change. The plugin owner decides on it. With this PR, the status line shows a sub-agent only when every listed thread is one.
  • The fallback can pick a p: side request, both reviewers: agreed. It is a display error, and the context bar is off by at most the side request's own turn. Reviewer 2's case (a side thread from before a compaction stays larger for up to 2 h) is real. A tie-break on last_ts does not fix it, because the sizes are not near-equal there. With fix(thread): keep the main thread when a newer Claude Code resumes it #711 merged, the main thread keeps main: true, and this fallback runs only in rare cases. I leave it as it is.
  • The a: rule trusts Claude Code's agent-id header, reviewer 1: agreed, as documented.
  • The new test did not check the exit code, reviewer 2: fixed. Both new tests now fail on a non-zero exit.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.310 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 7128221c31b7 approve

Verdict: approve

Findings

None.

Round-1 findings

  1. Mine, finding 1 (next prices stayed on the newest thread): fixed. _main_cs now pops next_warm_usd / next_cold_usd and copies them from the followed thread, in the same loop as held_until_ms (statusline.py:1048-1051). I checked the fix myself:

    • The followed thread carries its own pair (dash/cachestate.go:413-414), so the swap has real data to copy.
    • The loop keeps held_until_ms semantics identical to before.
    • With no prices on the thread entry, the pair is dropped, and _cache_view's cold-price fallback (cold_usd = usd, statusline.py:1203-1204) lands on the followed thread's rewrite_usd. That is the documented contract for an older server ("A server that predates next_warm_usd / next_cold_usd still prices the cold case through rewrite_usd").
    • The fix also repairs the marked-main path, which had the same gap.
    • The new golden test pins both branches. I traced the render: the fallback picks p:main; with thread prices 0.20 / 2.50 and a warm clock, the segment reads next $0.20 warm · $2.50 cold; with no thread prices it reads next $1.80 if cold (the thread's rewrite_usd). Both match the test's wanted strings and the format at statusline.py:1276-1279.
  2. Reviewer 2, non-blocking (exit code unchecked in the new test): fixed. Both new tests now fail on a non-zero exit.

  3. Reviewer 2, non-blocking (fallback can pick a p: side request): rejected by the author, reason accepted. A tie-break on last_ts cannot fix the stated case: after a compaction the stale side thread is much larger, not near-equal. And #711 (merged, verified) keeps main: true across a resume, so this fallback now runs only in rare cases. Display-only error, bounded by one turn otherwise.

  4. Mine and reviewer 2, non-blocking (issue #709 item 2, label a shown sub-agent): rejected by the author, reason accepted. A label is a UI change for the plugin owner, and after this PR a sub-agent is shown only when every listed thread is one. The issue stays open for that part; it is not this PR's scope.

Non-blocking

  • Nothing new.

What I checked

  • Last review: round 1, 4642567, 2026-10-25T18:14:37Z.
  • New commits read: 1 — 7128221c "fix(statusline): quote the followed thread's next-request prices".
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files; the inline file is empty).
  • New items since my last review: reviewer 2's round-1 review (approve, 3 non-blocking notes — all covered above), the author's round-1 reply comment with the fix table, and commit 7128221c. Each read and checked.
  • The new commit's full diff and the changed functions around it: _main_cs (statusline.py:1020-1053) and both golden tests.
  • The new test's data and the render path: _cache_view (prices, clock), _next_segment (format), the fallback's choice of p:main over a:new.
  • The commit message: conventional title, DCO sign-off, Assisted-By attribution — matches the repo's rules.
  • Issue #711 state: MERGED, confirming the author's claim that the fallback is now rare.
  • No Go tests were run by me (live tests not approved). The author reports go test -race ./context-guru-plugin/... passing on the eval box, except the two tests that always fail on that box.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.310 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 7128221c31b7 approve

Verdict: approve

Findings

None.

Earlier findings

  • Reviewer 1, finding 1 (blocking, next prices on the wrong thread): fixed. The swap loop at statusline.py:1048 now copies next_warm_usd and next_cold_usd from the followed thread and drops the top-level pair when the thread has none. I ran statusline.py end-to-end (python3, local stub, --show=next) on the new test's exact bodies:
    • new head, thread has prices: next $0.20 warm · $2.50 cold (the followed thread's, not the sub-agent's $0.01 / $0.05);
    • new head, thread has no prices: next $1.80 if cold (the thread's own rewrite_usd, via the _cache_view fallback at statusline.py:1206);
    • old head 4642567c, same body: next $0.01 warm · $0.05 cold — the bug, so the new test pins a real change.
  • Reviewer 2 (mine), non-blocking, next prices not swapped: fixed by the same change. It also covers the marked-main path, where I had called the gap pre-existing.
  • Reviewer 2 (mine), non-blocking, missing exit-code check: fixed. Both new tests now fail on a non-zero exit.
  • Reviewer 2 (mine), non-blocking, fallback can pick a p: side thread: rejected by the author, and I accept the reason. The author agrees the case is real. A last_ts tie-break cannot fix it, because the sizes are not near-equal after a compaction. With #711 merged, the main thread keeps main: true, so this fallback runs only in rare cases. Display only.
  • Reviewer 1, non-blocking, issue #709 point 2 (label when a sub-agent is shown): rejected by the author, and I accept the reason. A label is a UI change for the plugin owner, not this fix.

Non-blocking

  • statusline.py:1048 — the three keys are now handled by one loop. held_until_ms keeps its exact old behavior (drop, then copy when present); I checked the two held_seconds call sites still see the same values.

What I checked

  • Last review: round 1, 4642567c235ad37d0b966a544b8ee5f7bfcc670d, 2026-10-25T18:16:45Z.
  • New commits read: 1 — 7128221c fix(statusline): quote the followed thread's next-request prices.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three saved files).
  • New items since my last review: the commit above (read the full diff and the code around it), and the author's round-1 reply comment (read in full, every point checked above).
  • The new commit's diff: the _main_cs swap loop and TestStatuslineNextCostFollowsTheChosenThread. The render path _cache_view → _next_segment (statusline.py:1273-1282) produces both want strings for the test's inputs; verified by running the script, not only by reading.
  • Consumers of the swapped keys: next_warm_usd / next_cold_usd are read only in _cache_view (statusline.py:1204-1205); held_until_ms only in held_seconds. No other reader can see a half-merged dict.
  • Edge cases: a thread entry with next_warm_usd: null behaves as absent (_num returns None); a thread with priced: false carries no pair, so the segment shows nothing about money — consistent with the CONTRACT comment.
  • The author's test table (previous head fails, mutation fails, new head passes, go test -race on the eval box passes except the 2 known box failures) matches what I reproduced locally for the script.
  • python3 -m py_compile passes on the new script.
  • Fail-open and data dependence: unchanged from round 1 — status-line rendering only, metadata only.

@amiddavid
amiddavid merged commit ff9dcc7 into main Oct 25, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Status line follows the newest thread, so it shows a subagent and not the main conversation

1 participant