Skip to content

Copilot CLI: Stop large session stores exhausting file handles - #345095

Merged
Vijay Upadya (vijayupadya) merged 2 commits into
mainfrom
specific-stork
Oct 29, 2026
Merged

Vijay Upadya (vijayupadya) merged 2 commits into
mainfrom
specific-stork

Conversation

@vijayupadya

@vijayupadya Vijay Upadya (vijayupadya) commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #344829. Related: #327021.

Problem

_getAllSessions called sessionManager.listSessions() with no options. For every directory in ~/.copilot/session-state, the SDK then opened events.jsonl (twice) and workspace.yaml, all at the same time. Only after that did we throw away every session VS Code didn't create. With a large store (42k sessions from copilot -p in the report), each window's extension host held 10k–20k open files during startup. On macOS that breaks child-process spawning (spawn EBADF for ESLint, ripgrep and git), and several windows together fill the system file table (ENFILE).

Passing only metadataLimit is not enough. While detached sessions are included (the default), the SDK still opens every events.jsonl at once to detect them.

Solution

The listing in copilot CLI provider now fetches only session IDs, skips sessions the agent host already lists and sessions VS Code didn't create, and reads details just for the rest, at most 10 at a time and cached until their files change

Changes

  • Listing (copilotcliSessionService.ts)
    • Call listSessions({ metadataLimit: 0, includeDetached: false }), which returns only IDs and times and opens no events.jsonl or workspace.yaml.
    • Drop agent-host-owned IDs (agentSessionData/<id>) and sessions VS Code didn't create before any SDK read.
    • Read details (summary, name, clientName, context) with enrichSessionMetadata, 10 at a time, only for the remaining sessions. Output is identical to a full listing, including the SDK's pruning of empty sessions.
    • The clientName === vscode-agent-host filter now runs after the details read; agent-host ownership is re-read on every listing and checked first, so an adopted legacy session still drops out on the next refresh.
  • Cache: details are reused while both the SDK's modifiedTime (events.jsonl) and the workspace.yaml modification time are unchanged. A rename that only rewrites workspace.yaml doesn't change modifiedTime, so both are needed. Also cleared on delete, rename and summary updates. Repeated refreshes become nearly free (Copilot Chat re-walks the entire CLI session store on every refresh — extension host stalls for minutes with a large store #327021).
  • Item building: both session item providers build list items at most 32 at a time instead of all at once.
  • getSessionWorkingDirectory is now async. The listing only records working directories for VS Code-created sessions, so for any other session (e.g. a legacy session opened after declined adoption, or one restored at startup) it reads that one session from the SDK on demand.
  • Logging: a trace line reports listed / agent-host-owned / external / read / cached / shown counts.
  • Docs: copilotcli/AGENTS.md documents the listing contract.

No change to which sessions are shown, their titles, timing or order, or to legacy → agent-host migration, remote (WSL/SSH) or the Agents window. None of those paths call this listing differently.

How to test

Automated:

  • npx vitest run src/extension/chatSessions (in extensions/copilot): 1335 passed, 1 failed. The failure ("preserves order of edit toolCallIds and permissions…" in copilotcliSession.spec.ts) also fails on main without this change.
    • New tests: external sessions are never read; at most 10 reads run at once; the cache is reused, and refreshed when either file changes; an adopted session disappears even with cached details; agent-host clientName and empty sessions are filtered; the working directory resolves for a session that was never listed.
  • copilotCLISDKUpgrade.spec.ts: 4/4 passed. A new test runs the real SDK and fails if a future SDK version changes what metadataLimit: 0 does, or if enrichSessionMetadata stops matching a full listing.
  • tsc --noEmit -p tsconfig.json and eslint --max-warnings=0 on the changed files: clean.

Manual end to end (Windows, generated store of 20,000 external + 100 VS Code-created sessions):

  • Real SDK, old vs new: peak extra handles about 8,700 → about 30; time about 4.1 s → about 2.2 s; the same 100 sessions.
  • Code OSS signed in, cold start: log shows Listed 20100 sessions: … 20000 external, 100 read … 100 shown, and the refresh shows 100 cached (about 1 s). No Code OSS process went above about 1,700 handles. No EMFILE, ENFILE or EBADF in any log. The Sessions view shows only the VS Code-created sessions.
  • WSL remote sessions

To reproduce: generate many session directories under a temporary COPILOT_HOME (script in the issue), launch Code OSS with --log GitHub.copilot-chat:trace, and check the Listed … sessions line in the Copilot Chat log.

Not covered: the macOS-specific spawn EBADF itself (only Windows was tested, though the fix removes its cause).

Follow-ups, out of scope:

Copilot AI balanced review requested due to automatic review settings October 29, 2026 23:16
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Base: 3b216a5d Current: 88e084d0

No screenshot changes.

@vijayupadya Vijay Upadya (vijayupadya) changed the title agentHost: Stop large session stores exhausting file handles Copilot CLI: Stop large session stores exhausting file handles Oct 29, 2026

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

The regression test checks metadata but not whether events.jsonl or workspace.yaml is opened, leaving file-handle exhaustion regressions undetected.

1 open issue
What changed in this PR

This PR reduces file-handle pressure when the Copilot CLI session store is large, addressing #344829 and reducing repeated metadata reads related to #327021.

Changes:

  • Lists IDs first, filters unrelated sessions, and enriches metadata with bounded concurrency and file-change-aware caching.
  • Limits session-item building to 32 concurrent operations and resolves unlisted sessions’ working directories asynchronously.
  • Adds regression coverage and documents the listing contract.
File Description
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​test/​folderRepositoryManager.spec.ts Updates the working-directory mock to return a promise.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​test/​copilotCLIChatSessions.spec.ts Makes the session-service mock asynchronous.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​test/​copilotCLIChatSessionParticipant.spec.ts Updates the participant’s working-directory mock.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​folderRepositoryManagerImpl.ts Awaits fallback working-directory resolution.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​copilotCLIChatSessionsContribution.ts Bounds concurrent session-item construction.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​copilotCLIChatSessions.ts Bounds item construction during list refreshes.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​vscode-node/​test/​copilotCLISDKUpgrade.spec.ts Checks lightweight listing and enrichment results against the real SDK.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​node/​test/​testHelpers.ts Models lightweight listing, enrichment, and modification times.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​node/​test/​copilotCliSessionService.spec.ts Tests filtering, concurrency, caching, adoption, and working-directory fallback.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​node/​copilotcliSessionService.ts Implements filtered, bounded metadata reads, caching, and asynchronous directory lookup.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​AGENTS.md Documents listing constraints and cache behavior.

Review effort: Balanced

The SDK guard test only checked the returned metadata, so an SDK update that
went back to opening every events.jsonl (e.g. for detached detection) while
still returning ids only would have passed. Observe events.jsonl and
workspace.yaml access through Node's fs during the ids-only listing and assert
there is none. The full listing serves as a positive control that the observer
still sees the SDK's reads.

Co-authored-by: Copilot <[email protected]>
@vijayupadya
Vijay Upadya (vijayupadya) marked this pull request as ready for review October 29, 2026 23:51
@vijayupadya
Vijay Upadya (vijayupadya) merged commit 5a35cf7 into main Oct 29, 2026
55 checks passed
@vijayupadya
Vijay Upadya (vijayupadya) deleted the specific-stork branch October 29, 2026 23:56
@vs-code-engineering vs-code-engineering Bot added this to the 1.145.0 milestone Oct 29, 2026
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.

Copilot CLI session listing at startup exhausts the system file table (ENFILE) with a large ~/.copilot/session-state

3 participants