Repository navigation
fix(statusline): follow the largest thread when no thread is main - #714
Conversation
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
left a comment
There was a problem hiding this comment.
| 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
- context-guru-plugin/scripts/statusline.py:1037 — the thread swap leaves
next_warm_usd/next_cold_usdon the newest thread, so thenextsegment 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 swapsprefix_tokens,rewrite_usd,model,last_ts,end_ts,ttl_sandpriced, but notnext_warm_usd/next_cold_usd._cache_view(statusline.py:1201-1202) reads those two from the merged dict, so--show=nextrenders the sub-agent's next-request prices beside the main thread's clock, context bar andrewrite_usd. The line mixes two threads' numbers. Before this PR the no-main path returnedcsunchanged, 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-414setsNextWarmUSD/NextColdUSDperThreadCache, and the top-level fields atdash/cachestate.go:409describe the session's newest cache row. The merge key list atstatusline.py:1037omits both keys. The code's own fallback (if cold_usd is None: cold_usd = usd, statusline.py:1203-1204) showscold_usdis 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=nextand no main thread would pin it.
- 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
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 theirprefix_tokenscan 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 ap: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_csfallback and the new golden test. - The fallback's sort key:
(not id.startswith("a:"), prefix_tokens). Non-a:threads win over largera:ones; ties break by size. This matches the docstring and both test cases. - Thread id scheme in
internal/thread:""is primary (server setsmain: trueonly 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
threadskey, an empty list, a non-list, andcs=Noneall keep the old return-cspath. Themaxkey 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_msandkeepaliveare swapped or cleared correctly._agent_rows/_agent_idsread the rawcsbefore_main_csruns, so the agents segment is unaffected. - The golden test's helpers:
runStatuslinesetsNO_COLOR=1, so the plain substring assertions match the rendered line;cgStub/routedEnvmatch 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
left a comment
There was a problem hiding this comment.
| 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 ap: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 onlast_ts(prefer the most recent among near-equal) would narrow it.context-guru-plugin/scripts/statusline.py:1037— the merge swapsprefix_tokens,rewrite_usdandpriced, but notnext_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. Addif 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.gogives""to the main thread,a:<id>to a header-named sub-agent,p:<fingerprint>to a side request or fork.dash/cachestate.go:207marksMainonly for id"". So "no thread is main" means the proxy lost the main id, and thea:/ 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_csreturnscsas before when no thread is main and no fallback candidate exists. - Ran
_main_cson the test's exact data: case 1 picksp:main(500k, not the largera:big800k), case 2 picks the largesta:thread (800k), a marked-main thread still wins, junk entries inthreadsare ignored, ties are deterministic (first listed). - Ran
statusline.pyend-to-end (python3, local stub,--layout=balanced) on both new test cases. Output: case 1cache ● █████░ 4:00 │ ctx ████░░░░ 50%, case 2cache ● █████░ 4:30 │ ctx ██████░░ 80%. Both contain the test'swantstrings. 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_csreturnscsunchanged, so the bar reads 7% (70000/1000000). python3 -m py_compilepasses on the script.- Data dependence: the change reads thread ids and token counts from
/api/cachestateonly. No new stored-data dependence, so nodocs/data-dependence.mdupdate 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]>
|
Round 1 replies. New head: Reviewer 1, finding 1 (blocking): fixed
New test
The fix also applies when a thread is marked main. Reviewer 2 noted that this gap was older than this PR. Non-blocking points
|
amiddavid
left a comment
There was a problem hiding this comment.
| 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
-
Mine, finding 1 (next prices stayed on the newest thread): fixed.
_main_csnow popsnext_warm_usd/next_cold_usdand copies them from the followed thread, in the same loop asheld_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_mssemantics 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'srewrite_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 readsnext $0.20 warm · $2.50 cold; with no thread prices it readsnext $1.80 if cold(the thread'srewrite_usd). Both match the test's wanted strings and the format at statusline.py:1276-1279.
- The followed thread carries its own pair (
-
Reviewer 2, non-blocking (exit code unchecked in the new test): fixed. Both new tests now fail on a non-zero exit.
-
Reviewer 2, non-blocking (fallback can pick a
p:side request): rejected by the author, reason accepted. A tie-break onlast_tscannot fix the stated case: after a compaction the stale side thread is much larger, not near-equal. And #711 (merged, verified) keepsmain: trueacross a resume, so this fallback now runs only in rare cases. Display-only error, bounded by one turn otherwise. -
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 ofp:mainovera:new. - The commit message: conventional title, DCO sign-off,
Assisted-Byattribution — 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
left a comment
There was a problem hiding this comment.
| 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:1048now copiesnext_warm_usdandnext_cold_usdfrom the followed thread and drops the top-level pair when the thread has none. I ranstatusline.pyend-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 ownrewrite_usd, via the_cache_viewfallback atstatusline.py:1206); - old head
4642567c, same body:next $0.01 warm · $0.05 cold— the bug, so the new test pins a real change.
- new head, thread has prices:
- 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. Alast_tstie-break cannot fix it, because the sizes are not near-equal after a compaction. With #711 merged, the main thread keepsmain: 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_mskeeps its exact old behavior (drop, then copy when present); I checked the twoheld_secondscall sites still see the same values.
What I checked
- Last review: round 1,
4642567c235ad37d0b966a544b8ee5f7bfcc670d,2026-10-25T18:16:45Z. - New commits read: 1 —
7128221cfix(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_csswap loop andTestStatuslineNextCostFollowsTheChosenThread. The render path_cache_view→_next_segment(statusline.py:1273-1282) produces bothwantstrings for the test's inputs; verified by running the script, not only by reading. - Consumers of the swapped keys:
next_warm_usd/next_cold_usdare read only in_cache_view(statusline.py:1204-1205);held_until_msonly inheld_seconds. No other reader can see a half-merged dict. - Edge cases: a thread entry with
next_warm_usd: nullbehaves as absent (_numreturns None); a thread withpriced: falsecarries 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 -raceon the eval box passes except the 2 known box failures) matches what I reproduced locally for the script. python3 -m py_compilepasses on the new script.- Fail-open and data dependence: unchanged from round 1 — status-line rendering only, metadata only.
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 withmain: true. With none, it used the top-level fields of/api/cachestate. Those fields follow the session's newest thread.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.a:id. The main conversation never has ana:id.What changed
_main_cs: when no thread is marked main, the status line follows the largest thread byprefix_tokenswithout ana:id.a:id, it follows the largest one.How it was tested
New test
TestStatuslineFollowsTheLargestThreadWhenNoneIsMain. No thread is marked main, and the newest thread is a 70k sub-agent. 2 cases:p:thread and an 800ka:thread: the bar must read 50%.a:threads: the bar must read 80%.All runs on the eval box.
statusline.pyfromorigin/mainctx 7%(the live symptom)ctx 7%a:rulego test -race ./context-guru-plugin/...The 3 other failures:
TestInstallRefusesAnUnverifiedDownloadandTestInstallReportsPATHFromTheSourceFallbackTooalways fail on the box.TestUninstallDropsAStaleRecordEvenWhenNothingElseChangedlogged "Address already in use" (a port clash on the shared box). It passes alone onorigin/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.mdneeds no change.Technical notes