Skip to content

emrg: session — recompute message_count on compact, count message records only (rant 2026-08-31T14:18:14) - #1083

Merged
argszero merged 1 commit into
masterfrom
feature/session-message-count-compact-fix
Aug 31, 2026
Merged

emrg: session — recompute message_count on compact, count message records only (rant 2026-08-31T14:18:14)#1083
argszero merged 1 commit into
masterfrom
feature/session-message-count-compact-fix

Conversation

@argszero

Copy link
Copy Markdown
Owner

Fix TUI/GUI message count inflation after /compact (rant 2026-08-31T14:18:14).

Problem

The status bar showed "5770 msgs" for a session whose history.jsonl held only 192 records. Root cause: session.py's counter only ever grows:

  1. append_message() incremented _message_count for every record — including tool_result records persisted from the tool loop (daemon.py:2778) and summaries.
  2. Session.compact() replaced the compacted messages with one summary but never decremented _message_count — the inflated value survived (e.g. 5806 = ~5700 pre-compact + ~100 after, matching last_compact_at).
  3. TUI status bar (client/app.py:1018) and GUI session list (widgets.py:333) display meta.message_count directly.

Meanwhile rewind (daemon.py:2083) already recomputed the count as sum(r.type == "message") — compact simply missed the same reconciliation.

Fix (Plan A — confirmed by host)

  • Session.compact(): after writing the new history, recompute _message_count from the surviving records (message-type only) — aligned with rewind semantics.
  • Session.append_message(): increment only for type == "message" records (missing type defaults to message, preserving existing callers) — tool_result/summary records persist but no longer inflate the user-facing count, so the count cannot re-inflate between compacts.

Tests

  • test_append_tool_result_does_not_increment_count — tool_results persist but are excluded from the count.
  • test_compact_recomputes_message_count — mixed message/tool_result history: count shrinks to the surviving message records after compact (regression for the rant).
  • test_compact_count_persists_after_reload — recomputed count survives Session.load() from meta.json.

Verification: pytest 1182 passed + 1 skipped; import + CLI green; Agent.md Python count synced to 1183.

Note: a display-side fallback (Plan B — fall back to the actual record count when message_count deviates wildly) was mentioned as optional in the rant; Plan A alone fixes the reported bug, so B is deferred.

@argszero argszero left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

✅ LGTM — cycle (1/3)

Reviewed head ba65cee against master (1fdc606):

  • Root cause confirmed: append_message() incremented _message_count for every persisted record including tool_result, and Session.compact() never decremented it — the TUI/GUI 'msgs' counter inflated monotonically (e.g. 5770 shown vs 192 records in history.jsonl).
  • Fix is sound: compact() recomputes _message_count from the surviving message-type records (same semantics as the rewind handler in daemon.py), and append_message() only counts type == "message" records (defaulting to message when absent, so existing callers are preserved); tool_result/summary records still persist to history.
  • Tests: 3 new (tool_result exclusion; compact recompute regression with mixed message/tool_result history; recompute survives reload from meta.json). test_session.py 66 passed; CI test + test-windows green (run 33366280252); mergeable_state clean.
    No issues found.

@argszero argszero left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

✅ LGTM — cycle (2/3)

Re-verified head ba65cee against current master (4fd2b63) this cycle:

  • diff unchanged and matches the intended fix: append_message() increments _message_count only for type == "message" records (defaulting to message when absent — existing callers preserved; tool_result/summary still persist without inflating the count); Session.compact() recomputes _message_count from the surviving message-type records, aligned with the rewind handler's semantics in daemon.py.
  • The fix directly addresses the reported symptom (5770 msgs shown vs 192 records in history.jsonl) and prevents re-inflation between compacts.
  • Tests: 3 new regression tests (tool_result exclusion; compact recompute; reload persistence) — branch test_session.py 66 passed; master baseline still green (63 passed). CI test + test-windows green (run 33366280252); mergeable_state clean.
    No issues found.

@argszero argszero left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

✅ LGTM — cycle (3/3)

Re-verified head ba65cee against current master (4fd2b63) this cycle:

  • diff unchanged: append_message() counts only type == "message" records (tool_result/summary persist without inflating the count); Session.compact() recomputes _message_count from surviving message-type records (rewind-aligned).
  • Local test_session.py + test_doc_counts.py all pass (68 passed); CI test + test-windows green (run 33366280252); mergeable_state clean.
    3 consecutive ✅ from distinct cycles (07:04:12Z / 07:11:06Z / this cycle), no ❌ — good to merge.

@argszero
argszero merged commit ad13965 into master Aug 31, 2026
2 checks passed
argszero added a commit that referenced this pull request Aug 31, 2026
…d, GUI tool/text order, LLM stream retry, session message_count) (#1085)

Co-authored-by: EMRG Evolution <emrg@argszero.dev>
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