Skip to content

refactor(coding-agent): query agent peers on demand instead of broadcasting mirrors - #1748

Closed
snimu wants to merge 8 commits into
snimu/remove-empty-selector-timersfrom
snimu/on-demand-agent-peers
Closed

refactor(coding-agent): query agent peers on demand instead of broadcasting mirrors#1748
snimu wants to merge 8 commits into
snimu/remove-empty-selector-timersfrom
snimu/on-demand-agent-peers

Conversation

@snimu

@snimu snimu commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

What was wrong

The supervisor owns the roster of agents across workers, but it also pushed a mutable copy (remoteAgentPeers) into every worker via a per-worker sync queue and a worker_sync_agent_peers message — O(n²) replication with 13 broadcast triggers whose failures were swallowed. Workers made list/reachability decisions from arbitrarily stale mirrors (root cause of the rename bug fixed in #1702) (audit: overlap.md finding 1 ≡ dup-truth.md finding 2).

The fix

The mirror, sync queue, message type, and all 13 broadcast triggers are deleted (+101/−312). Workers now issue an authenticated, read-only list_agent_peers query to the supervisor only when a list/family catalog is actually requested — request-scoped, never cached, so strictly fresher than the old mirror. Local rows keep precedence. Daemon schema revision bumped to 23; mixed-version windows degrade cleanly in both directions during update-restart.

Latency pre-check (plan gate): p50 0.071ms / p95 0.111ms over a real Unix-socket connect+hello harness — three orders of magnitude inside the interactive list budget.

How it's verified

Reviewer enumerated all 6 former mirror readers and all 13 triggers on the base and accounted for each; confirmed no response caching, complete schema-rev machinery, honest mixed-version degradation, preserved local precedence, and untouched #1702/passive-hydration paths. Focused suites green with the 5 remaining failures reproducing identically at the exact base commit. Two-model implement/review loop, approved first pass.

Stacked on #1746 (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

Medium Risk
Changes multi-worker daemon protocol and agent targeting/list behavior; schema bump 22→23 needs compatible rollout, but the change is read-only on the query path with graceful empty-peer fallback.

Overview
Replaces supervisor-driven peer roster broadcasting with on-demand queries so cross-worker agent lists stay current without O(n²) worker_sync_agent_peers pushes or a stale remoteAgentPeers cache on each worker.

Workers call the supervisor via authenticated list_agent_peers (daemon schema revision 23) when building agent message lists and family catalogs; remote root sessions are merged with local rows (locals win, closing sessions excluded). Failures degrade to an empty peer list. syncAgentPeers, the sync queue, and all lifecycle triggers are removed from the supervisor.

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

Linear ticket: ENG-5664
(ticket linked above)

Note

Replace broadcast peer sync with on-demand list_agent_peers query in AgentDaemon

  • Workers now query the supervisor for the current cross-worker agent roster on demand via a new read-only list_agent_peers command, instead of receiving pushed peer rosters
  • Removes the syncAgentPeers push method on DaemonSupervisor, the remoteAgentPeers cache on AgentDaemon, and the worker_sync_agent_peers worker command
  • The supervisor authenticates list_agent_peers requests by matching the supplied workerToken to a registered worker, then returns summaries of other live, ready, connected workers; failures on the worker side yield an empty peer list
  • Bumps DAEMON_SCHEMA_REVISION from 22 to 23 and registers list_agent_peers with minSchemaRevision 23 in daemon-protocol.ts
  • Behavioral Change: clients below schema revision 23 cannot send list_agent_peers; the removed worker_sync_agent_peers command is no longer accepted by workers

Macroscope summarized f39d98f.

Comment thread packages/coding-agent/src/modes/daemon/daemon-mode.ts
Comment thread packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
snimu added 2 commits August 27, 2026 10:19
…d-agent-peers

# Conflicts:
#	packages/coding-agent/test/daemon-mode.test.ts
Comment thread packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
Comment thread packages/coding-agent/src/modes/daemon/daemon-supervisor.ts

@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 f39d98f. Configure here.

this.remoteAgentPeers.set(peer.activeSessionId, peer);
}
this.writeWorkerSuccess(client, command);
return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Mixed-version peer sync can hang

Medium Severity

Removing worker_sync_agent_peers leaves that command unmatched. Worker sockets still route any worker_* type into handleWorkerCommand, which now returns without writing a response. An older supervisor still broadcasts this command and waits 5s per sync, so list, create, and recovery stall for the whole mixed-version window instead of failing fast.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f39d98f. Configure here.

@snimu

snimu commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1829: 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 #1829 instead.

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