Skip to content

refactor(coding-agent): read messageCount instead of a shadow flag - #1739

Closed
snimu wants to merge 8 commits into
snimu/remove-daemon-lookup-fakefrom
snimu/use-message-count
Closed

refactor(coding-agent): read messageCount instead of a shadow flag#1739
snimu wants to merge 8 commits into
snimu/remove-daemon-lookup-fakefrom
snimu/use-message-count

Conversation

@snimu

@snimu snimu commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

What was wrong

The TUI kept a private sessionHasMessages boolean that shadowed connectionState.messageCount — seeded from a separately rendered transcript and updated by a hand-picked subset of events. Two answers to "does this session have messages?", which had already diverged in definition (the flag ignored some message kinds the count includes).

The fix

The flag and its setter are deleted. isNewChat() and startup notices read connectionState.messageCount, seeded from the authoritative connection snapshot. message_start events bump the count following the exact existing event-patch pattern (snapshots overwrite and self-correct, same as isStreaming/isCompacting), with fresh-chat UI invalidated only on the zero→nonzero transition. Stale test mocks of the deleted setter removed.

How it's verified

Reviewer confirmed the event increment replaces the flag's event-edge maintenance rather than adding a second writer (snapshot remains authoritative), and the count's semantics now match the snapshot definition. Affected suites 202/202; full CI-style suite: no new failures vs stack base. Two-model implement/review loop.

Stacked on #1738 (test the whole stack at the leaf; merge base-first).

Note: intentionally no Linear ticket for this cleanup stack, so that check stays red.


Note

Low Risk
UI-only refactor for hints and startup notices; behavior now follows connection state semantics, with snapshots still authoritative for the count.

Overview
Removes the private sessionHasMessages flag and setSessionHasMessages so “fresh chat” behavior uses connectionState.messageCount (from the connection snapshot) instead of a second, transcript-driven boolean.

isNewChat() now means zero committed messages and not streaming; shortcut tray hints, startup notices, and related guards call that instead of the removed flag. updateConnectionStateFromEvent bumps messageCount on message_end and refreshes the built-in header / subagent summary when the session was still treated as new at agent_start or message_end, including mid-turn runs with no committed messages yet.

Tests and mocks are updated to seed connectionState.messageCount and cover the streaming-with-zero-messages case.

Reviewed by Cursor Bugbot for commit 786da59. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Replace sessionHasMessages flag with connectionState.messageCount in InteractiveMode

  • Removes the sessionHasMessages boolean field and derives new-chat status from connectionState.messageCount and isStreaming via InteractiveMode.isNewChat()
  • Increments messageCount on message_end and invalidates the built-in header and subagent summary on agent_start and message_end when transitioning from a new chat
  • Suppresses startup notices during mid-turn snapshots with zero committed messages but an active stream
  • Updates all test harnesses to use connectionState.messageCount and isStreaming instead of setSessionHasMessages
  • Behavioral Change: isNewChat() now returns true only when messageCount is 0 and isStreaming is not true, which may alter notice visibility for sessions that previously set sessionHasMessages via initial context length
📊 Macroscope summarized 786da59. 3 files reviewed, 1 issue evaluated, 0 issues filtered, 1 comment posted

🗂️ Filtered Issues

Linear ticket: ENG-5659
(ticket linked above)

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 194b7ce. Configure here.

Comment thread packages/coding-agent/src/modes/interactive/interactive-mode.ts
@snimu

snimu commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1825: GitHub's stack lock prevents retargeting stacked PRs, so this PR was recreated as a plain PR against main (same branch, same content, all review threads here are resolved). Please review/merge #1825 instead.

@snimu snimu closed this Aug 27, 2026
}
case "message_end": {
const wasNewChat = this.isNewChat();
this.patchConnectionState({ messageCount: this.connectionState.messageCount + 1 });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Medium interactive/interactive-mode.ts:2662

messageCount is incremented for every message_end, so a transient durable-command error result makes an otherwise empty session appear non-new. This suppresses fresh-session hints and deferred startup notices until a snapshot or rebind corrects the state; only increment the count for messages that were actually persisted, or expose that distinction on the event.

🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file @packages/coding-agent/src/modes/interactive/interactive-mode.ts around line 2662:

`messageCount` is incremented for every `message_end`, so a transient durable-command error result makes an otherwise empty session appear non-new. This suppresses fresh-session hints and deferred startup notices until a snapshot or rebind corrects the state; only increment the count for messages that were actually persisted, or expose that distinction on the event.

Evidence trail:
packages/coding-agent/src/modes/interactive/interactive-mode.ts:2660-2666,6056-6057 @ 786da59ea9f208faada21c519a826ced622d011f
packages/coding-agent/src/core/agent-session.ts:6095-6116,6139-6149 @ 786da59ea9f208faada21c519a826ced622d011f
packages/coding-agent/src/modes/agent-connection/snapshot.ts:27-48 @ 786da59ea9f208faada21c519a826ced622d011f

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