Skip to content

agentHost: apply chat read toggles without restoring the session - #341869

Merged
Sandeep Somavarapu (sandy081) merged 5 commits into
mainfrom
agents/fix-chat-read-toggle-stall-issue
Oct 15, 2026
Merged

Sandeep Somavarapu (sandy081) merged 5 commits into
mainfrom
agents/fix-chat-read-toggle-stall-issue

Conversation

@sandy081

@sandy081 Sandeep Somavarapu (sandy081) commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Problem

In the Agents window, marking a chat read could take seconds to minutes to apply after a restart, and chats looked stuck unread. A real wire log showed chat/isReadChanged actions sent between 19:04:46 and 19:07:44 all applied at 19:08:27; later ones took 4–5 s, and one took 78 s.

Cause: AgentService.dispatchAction routed a ChatIsReadChanged for a chat of an unloaded session, or for a peer whose history isn't loaded yet, through a full session restore and peer materialization. Because client actions run through a per-client queue, every later action from that client waited behind the restore (head-of-line blocking), including actions for unrelated, already-loaded sessions. This predates the Move Chats work: per-chat read toggles (#339500) combined with lazy peer restore (#329071).

Change

Read state is metadata, so it no longer needs a restore:

  • Unloaded session: _applyPassiveChatRead resolves the owner from recorded ownership (never the chat URI), then:
    • persists through the peer chat store;
    • updates the surfaced summary;
    • recomputes the session read aggregate through the existing passive session path, so the clamp from sessions: Persist read state per chat #339500 still holds in both directions;
    • syncs the central catalog with a new chatRead passive update, applied to the chats before the aggregate;
    • echoes the client action.
    • Subagent and ehcli-adoptable sessions, and a chat the surfaced summary doesn't list, fall back to the old restore path.
    • It joins an in-flight restore before and after the write, and re-applies through the live path if the session became resident.
    • A session being deleted rejects the toggle and writes nothing.
  • Loaded session, peer without loaded history: AgentHostStateManager applies the read flag to the registered chat summary without resolving its history. Loading the history later keeps the flag.
  • The known-chat rejection uses recorded ownership.
  • _applyPassiveSessionMetadata re-reads the surfaced summary after persisting, so a concurrent passive update isn't overwritten.
  • listSessions' per-session mapping moved into the exported pure toListedSessionSummary (unchanged code) so tests can drive the real cold-listing path.
  • Per-client action ordering is unchanged. Archive is not included; it's a follow-up.

Also fixes

Restoring a session forced it unread whenever it had an archived chat. The restore projection gave an archived catalog chat a status of IsArchived only, dropping its read state, so the restore clamp saw an unread chat. Sessions with many archived peers came back unread on every restore: the same "stuck unread" symptom.

  • The cached chat catalog now carries each chat's recorded read state, and restore keeps it for archived chats. An unrecorded state counts as read, as elsewhere in restore.
  • A genuinely unread archived chat still restores the session unread, per the existing aggregate rule.
  • The cached peer imports (no local database, legacy migration) now keep an explicitly unread chat unread instead of forcing it read.

Validation

Repro with a 500 ms restore barrier (a cold peer toggle, then an unrelated toggle on a loaded session, then an unknown chat, all from one client):

Echo latencies Restores Unrelated toggle
Before 507 / 507 / 508 ms 1 (session becomes resident) held behind the restore
After 2 / 3 / 3 ms 0 (session stays unloaded) not held

The same holds for a session that has only been listed on a fresh host (legacy and normalized catalogs).

All on this head, at load < 20:

  • ./scripts/test.sh --runGlob "**/agentHost/test/node/**/*.test.js": passed, 11226 passing / 76 pending / 0 failing.
  • The 32 targeted tests in agentService.test.ts: passed. Coverage:
    • the repro;
    • the aggregate in both directions;
    • an unmaterialized peer keeps read/unread after its history loads;
    • survives restore and a restart from the DB;
    • a race with an in-flight restore gives exactly one envelope;
    • cold listing;
    • the deletion fence;
    • undefined chat status;
    • a stale surfaced summary;
    • the subagent, adoptable and unlisted-chat fallbacks;
    • hidden chats;
    • an archived read chat restores its session read, and an archived unread one unread, after restore and after a restart (legacy, legacy cached membership, normalized);
    • a stale session read flag cannot hide an archived unread chat;
    • cached peer imports keep an unread chat's state.
  • Mutation checks: 20/20 killed (16 for the read fix, 4 for the restore fix).
  • npm run typecheck-client: passed. npm run eslint -- <changed files>: passed. Hygiene: passed. npm run valid-layers-check: passed.

A chat/isReadChanged for a chat of an unloaded session restored the whole
session first, and one for an unmaterialized peer resolved its history.
Both routed through the per-client dispatch queue, so every later action
from that client waited behind a restore that takes minutes for a large
session, leaving chats "stuck unread".

- A registered chat without materialized state takes the read flag on its
  summary; the session read aggregate is synchronized as before.
- A chat of an unloaded session (owner from recorded ownership) has its read
  state persisted, published on the surfaced summary and folded into the
  session read aggregate through the passive catalog path, which now also
  carries the chat's read state so a legacy central payload cannot restore
  the previous value. A restore racing the write is joined and re-applied;
  a session being deleted rejects the toggle without writing.
- Passive session metadata re-reads the surfaced summary after persisting so
  a concurrent passive update is not overwritten.
- The listSessions summary projection is extracted unchanged into
  toListedSessionSummary so tests drive the real cold listing path.
- Unknown chats are still rejected; per-client ordering is unchanged.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 15, 2026 21:04
@sandy081 Sandeep Somavarapu (sandy081) added this to the 1.143.0 milestone Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Base: 53afd495 Current: f5a54cc4

No screenshot changes.

Copilot AI left a comment

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.

🟡 Changes recommended

Concurrent passive updates and mid-write restoration can leave session read aggregates inconsistent with chat state.

2 open findings
What changed in this PR

Makes chat read toggles metadata-only operations, avoiding session restoration and delays to later client actions.

Changes:

  • Adds passive persistence and aggregate updates for unloaded sessions.
  • Applies read flags to registered chat summaries without loading history.
  • Adds regression coverage and extracts the existing listing projection for testing.
File Description
src/​vs/​platform/​agentHost/​test/​node/​agentService.test.ts Adds passive-toggle, persistence, fallback, and race tests.
src/​vs/​platform/​agentHost/​node/​protocolServerHandler.ts Extracts the unchanged session-listing projection.
src/​vs/​platform/​agentHost/​node/​agentService.ts Adds passive chat-read dispatch and catalog synchronization.
src/​vs/​platform/​agentHost/​node/​agentHostStateManager.ts Supports read toggles without materialized chat history.
src/​vs/​platform/​agentHost/​AGENTS.md Documents metadata-only read handling.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Restore projected an archived catalog chat as IsArchived only, dropping its
read state, so the restore clamp saw an unread chat and forced the session
unread. Sessions with archived peers came back unread on every restore.

- The cached chat catalog now carries each chat's recorded read state, and
  restore projects an archived chat with it (unrecorded counts as read, as
  elsewhere in restore). A genuinely unread archived chat still restores
  the session unread.
- Cached peer imports (no local database, legacy migration) keep an
  explicitly unread chat unread instead of forcing it read.

Co-authored-by: Copilot <[email protected]>
roblourens
roblourens previously approved these changes Oct 15, 2026
…tores

Two races in the restore-free read path could leave a session's read
aggregate wrong:

- Concurrent clients: client A read the last unread peer and paused while
  persisting the session aggregate; client B marked another chat unread, saw
  the still-unread session and skipped the aggregate; A then published its
  stale read state. Passive read/archive updates for an unloaded session
  (chat- and session-level) now run one at a time per session.
- Restore: a restore starting during a passive write could read the old
  session flag, pause on default-chat history, then register the peer with
  its new flag; replaying the chat toggle saw it already read and skipped the
  unread-to-read aggregate transition. A restore now waits for the queued
  passive update before reading, and an update queued behind a restore that
  is already reading waits for it and applies through the live path, where
  the aggregate is computed from the restored chats.

Tests that read passive archive results after a fixed sleep now await the
dispatch instead.

Co-authored-by: Copilot <[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.

5 participants