Skip to content

agentHost: make startup session discovery registry-first - #331176

Merged
Benjamin Christopher Simmonds (benibenj) merged 3 commits into
mainfrom
perf/agent-host-startup-discovery-331155
Aug 17, 2026
Merged

Benjamin Christopher Simmonds (benibenj) merged 3 commits into
mainfrom
perf/agent-host-startup-discovery-331155

Conversation

@benibenj

Copy link
Copy Markdown
Contributor

agentHost: make startup session discovery registry-first (#331155)

A 2026-08-17 Insiders reproduction with 941 SDK sessions and 1,437
per-session databases (~946 MB) showed the ~60s delay is not migration —
sessionRegistryBackfilled:copilotcli was already true and migration was
skipped. The time went to per-session SQLite work during discovery and
listing: classification took 66.9s (113 external, 342 adoptable
extension-host, 486 already known), registration of the 113 emitted
candidates another 21.9s, and the first two AHP listSessions calls
109.8s and 91.0s, with a third 73.2s window after discovery.

Each measured phase is addressed:

  • Classification (66.9s): discovery is now registry-first. IAgent gains
    an optional setKnownSessionsFilter seam that AgentService installs
    at provider registration; it answers, in one registry query for the
    whole candidate set, which sessions the host already owns.
    CopilotAgent drops those candidates instead of opening a session
    database each, so the 486 already-known sessions cost no DB opens.
    Tombstoned sessions are absent from the registry and therefore never
    reported as known, so an explicitly deleted session still reaches
    register, whose atomic tombstone check declines it. Provenance of a
    registered row stays owned by the explicit create/restore paths.

  • Classification (the 342 adoptable rows): while migrate-legacy is off,
    adoptable extension-host candidates are never emitted, so their
    Git-touching project resolution is now skipped entirely instead of
    being computed and then filtered away. _emitCopilotChats keeps its
    filter as a re-check, since the setting can flip mid-pass.

  • Registration (21.9s): _registerDiscoveredChats rejects an
    already-registered candidate with unchanged provenance before
    _isChatBacking() or any other per-session I/O.

  • Listing (109.8s / 91.0s / 73.2s): listSessions() coalesces concurrent
    computations per external-sessions mode, so the burst a multi-window
    restore produces shares one registry traversal. The shared entry
    records the registry epoch it started at and every registry mutation
    invalidates it, so a caller arriving after a mutation starts a fresh
    pass rather than joining a possibly pre-mutation one. Each caller gets
    its own array; rejections are shared only with callers already waiting.

  • _readStoredSessionMetadata / _readSessionMetadata now issue one
    bulk getMetadataObject() query instead of nine and six single-key
    reads, shrinking the cost of the fallback path that runs when no host
    filter is installed.

Deferred deliberately: seeding a newly discovered external session as
read still creates its database purely to hold one flag. Dropping the
write without a durable default would flip every discovered external
session to unread, because the list overlay only applies IsRead when
the key is present. The correct default belongs on the registry row and
is left to the registry list-projection change; the reasoning is
recorded on _initializeExternalSessionReadState. Removal of
_awaitInitialProviderMigration() is likewise not attempted: the
reproduction proves migration was already skipped.

Tests assert call/open counts rather than wall-clock thresholds:
registry-known candidates cause zero session DB opens; disabled
adoptable candidates resolve no projects; re-registering a known
discovered chat performs no per-session I/O; the known-sessions filter
reports registered sessions only and leaves tombstones to registration;
concurrent list calls share one computation but not their arrays; a
mutation during an in-flight list is not served from it; and stored
metadata is read with a single bulk query.

Co-authored-by: Copilot [email protected]

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.

Pull request overview

Optimizes Agent Host startup discovery and session listing for large Copilot catalogs.

Changes:

  • Filters registry-known sessions before per-session database access.
  • Coalesces concurrent session-list computations with mutation-aware invalidation.
  • Bulk-reads metadata and adds performance-focused coverage.
Show a summary per file
File Description
AGENTS.md Documents registry-first discovery and list coalescing.
common/agent.ts Adds the known-session filter provider seam.
node/agentService.ts Implements filtering, early rejection, and list coalescing.
node/copilot/copilotAgent.ts Avoids unnecessary database and Git work.
test/node/agentService.test.ts Tests registration and list coalescing behavior.
test/node/copilotAgent.test.ts Tests discovery and bulk metadata optimizations.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Balanced

A 2026-08-17 Insiders reproduction with 941 SDK sessions and 1,437
per-session databases (~946 MB) showed the ~60s delay is not migration —
`sessionRegistryBackfilled:copilotcli` was already true and migration was
skipped. The time went to per-session SQLite work during discovery and
listing: classification took 66.9s (113 external, 342 adoptable
extension-host, 486 already known), registration of the 113 emitted
candidates another 21.9s, and the first two AHP `listSessions` calls
109.8s and 91.0s, with a third 73.2s window after discovery.

Each measured phase is addressed:

- Classification (66.9s): discovery is now registry-first. `IAgent` gains
  an optional `setKnownSessionsFilter` seam that `AgentService` installs
  at provider registration; it answers, in one registry query for the
  whole candidate set, which sessions the host already owns.
  `CopilotAgent` drops those candidates instead of opening a session
  database each, so the 486 already-known sessions cost no DB opens.
  Tombstoned sessions are absent from the registry and therefore never
  reported as known, so an explicitly deleted session still reaches
  `register`, whose atomic tombstone check declines it. Provenance of a
  registered row stays owned by the explicit create/restore paths.

- Classification (the 342 adoptable rows): while migrate-legacy is off,
  adoptable extension-host candidates are never emitted, so their
  Git-touching project resolution is now skipped entirely instead of
  being computed and then filtered away. `_emitCopilotChats` keeps its
  filter as a re-check, since the setting can flip mid-pass.

- Registration (21.9s): `_registerDiscoveredChats` rejects an
  already-registered candidate with unchanged provenance before
  `_isChatBacking()` or any other per-session I/O.

- Listing (109.8s / 91.0s / 73.2s): `listSessions()` coalesces concurrent
  computations per external-sessions mode, so the burst a multi-window
  restore produces shares one registry traversal. The shared entry
  records the registry epoch it started at and every registry mutation
  invalidates it, so a caller arriving after a mutation starts a fresh
  pass rather than joining a possibly pre-mutation one. Each caller gets
  its own array; rejections are shared only with callers already waiting.

- `_readStoredSessionMetadata` / `_readSessionMetadata` now issue one
  bulk `getMetadataObject()` query instead of nine and six single-key
  reads, shrinking the cost of the fallback path that runs when no host
  filter is installed.

Deferred deliberately: seeding a newly discovered external session as
read still creates its database purely to hold one flag. Dropping the
write without a durable default would flip every discovered external
session to unread, because the list overlay only applies `IsRead` when
the key is present. The correct default belongs on the registry row and
is left to the registry list-projection change; the reasoning is
recorded on `_initializeExternalSessionReadState`. Removal of
`_awaitInitialProviderMigration()` is likewise not attempted: the
reproduction proves migration was already skipped.

Tests assert call/open counts rather than wall-clock thresholds:
registry-known candidates cause zero session DB opens; disabled
adoptable candidates resolve no projects; re-registering a known
discovered chat performs no per-session I/O; the known-sessions filter
reports registered sessions only and leaves tombstones to registration;
concurrent list calls share one computation but not their arrays; a
mutation during an in-flight list is not served from it; and stored
metadata is read with a single bulk query.

Co-authored-by: Copilot <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants