Skip to content

fix(acp): resolve session by exact id and handle listener cleanup on session failure - #29580

Merged
DavidAPierce merged 18 commits into
google-gemini:mainfrom
diegogodinezr:GH-29288
Oct 1, 2026
Merged

DavidAPierce merged 18 commits into
google-gemini:mainfrom
diegogodinezr:GH-29288

Conversation

@diegogodinezr

@diegogodinezr diegogodinezr commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Resolves an issue where ACP session/load fails with "Invalid session identifier" when resuming newly created sessions without conversational turns, and addresses event listener lifecycle management when session resolution fails.

Details

  • Session Resolution: Added SessionSelector.resolveSessionById to allow direct resolution of session UUIDs without applying interactive CLI terminal resumable content filtering (hasResumableContent: false).
  • Index Routing: Ensured numeric strings (indices) continue routing to index-based resolution rather than resolveSessionById, even when allowEmpty: true.
  • Input Format Validation: Added input validation in AcpSessionManager.loadSession to reject directory traversal sequences or path separators (.., /, \) before initializing session storage.
  • Config & Resolution Order: Updated AcpSessionManager.loadSession to validate session existence on disk before instantiating Config and GeminiClient components.
  • Resource Management: Added explicit resource disposal calling config.dispose() on error handling paths in loadSession, prepareSessionConfig (including during AcpFileSystemService setup), and newSession, as well as in Session.dispose() on session termination with unhandled promise rejection protection. This unregisters listeners bound to coreEvents (such as model-changed, memory-changed, and approval-mode-changed).
  • Interactive CLI Semantics Preserved: Standard interactive CLI commands (gemini --resume and gemini --list-sessions) continue to filter out empty sessions as expected.

Related Issues

Closes #29288

How to Validate

  1. Run ACP session manager unit tests:

    npm test -w @google/gemini-cli -- src/acp/acpSessionManager.test.ts

    Validates immediate session/new followed by session/load, loading populated sessions, rejection of traversal paths with format errors, and error handling without listener warnings over repeated invalid session loads.

  2. Run session utility tests:

    npm test -w @google/gemini-cli -- src/utils/sessionUtils.test.ts

    Validates resolveSessionById, numeric index routing, and resolveSession with and without { allowEmpty: true }.

  3. Run ACP test suite:

    npm test -w @google/gemini-cli -- src/acp/
  4. Verify types and linting:

    npm run typecheck
    npm run lint

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

…session failure

Bypass resumable content filtering when resolving explicit ACP session UUIDs, validate session files before constructing client components, and dispose config instances on error paths to release listeners.

Closes google-gemini#29288
@diegogodinezr
diegogodinezr requested a review from a team as a code owner September 30, 2026 18:33
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where ACP sessions could not be resumed if they lacked conversational turns. By allowing direct session resolution via UUID and improving the lifecycle management of configuration resources and event listeners, the changes ensure that sessions are correctly handled and cleaned up, even when initialization fails.

Highlights

  • Session Resolution: Introduced SessionSelector.resolveSessionById to enable direct resolution of session UUIDs, bypassing standard interactive CLI filtering that previously prevented loading empty sessions.
  • Resource Management: Implemented explicit resource disposal by calling config.dispose() on error paths in session loading and ensuring event listeners are unregistered when sessions are terminated.
  • Session Loading Flow: Updated AcpSessionManager.loadSession to validate session existence on disk before component initialization, improving robustness and error handling.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the size/l A large sized PR label Sep 30, 2026
@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 998
  • Additions: +880
  • Deletions: -118
  • Files changed: 8

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request enhances the Agent Client Protocol (ACP) session management by introducing support for loading empty sessions (sessions without resumable content) and ensuring proper resource cleanup. It adds an allowEmpty option to SessionSelector.resolveSession and implements resolveSessionById to bypass interactive terminal list filtering. Additionally, it wraps session loading and authentication steps in try...catch blocks to guarantee that the session configuration is disposed of on failure, preventing resource and event listener leaks. The review feedback highlights a potential resource leak in prepareSessionConfig where an error during file system service setup could leak the config object before it is returned, and provides a code suggestion to handle this failure safely.

Comment thread packages/cli/src/acp/acpSessionManager.ts
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request updates the ACP session management to support loading empty sessions and ensures proper cleanup of session configurations by invoking dispose on failure or session termination. It introduces a new resolveSessionById helper to bypass interactive CLI filters when loading a session by its full UUID, along with comprehensive unit tests. The review feedback highlights three key areas for improvement: validating the sessionId parameter to prevent path traversal vulnerabilities, catching potential promise rejections during asynchronous configuration disposal to prevent process crashes, and ensuring numeric indices are not incorrectly routed to resolveSessionById when allowEmpty is enabled.

Note: Security Review did not run due to the size of the PR.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/acp/acpSession.ts Outdated
Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
@gemini-cli gemini-cli Bot added priority/p1 Important and should be addressed in the near term. area/non-interactive Issues related to GitHub Actions, SDK, 3P Integrations, Shell Scripting, Command line automation labels Sep 30, 2026
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces changes to session loading and resolution within the ACP session manager, including path traversal validation for session identifiers, direct session resolution by UUID, and improved resource disposal on initialization failures. The review feedback highlights two key areas for improvement: first, a potential runtime TypeError when calling .catch() on an optional-chained dispose call that may evaluate to undefined; second, an optimization to resolve the session before initializing storage to prevent unnecessary directory creation on disk for invalid session IDs.

Comment thread packages/cli/src/acp/acpSession.ts Outdated
Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

1 similar comment
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request enhances the ACP session manager by adding path traversal validation for session identifiers, ensuring proper cleanup of configurations via config.dispose() during initialization or authentication failures, and introducing a resolveSessionById method to support loading empty sessions. Feedback was provided to safely handle the optional sessionData.lastUpdated property using the nullish coalescing operator (??) to prevent potential runtime issues when formatting the session display info.

Comment thread packages/cli/src/utils/sessionUtils.ts Outdated

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request enhances the Agent Client Protocol (ACP) session management by adding validation to reject invalid session identifiers early, refactoring session loading to handle configuration initialization and disposal robustly, and introducing a method to resolve sessions directly by UUID. Comprehensive unit tests have been added to verify these changes. The review feedback suggests wrapping configuration disposal calls in try/catch blocks across acpSessionManager.ts to prevent disposal errors from masking original authentication or initialization failures. Additionally, it is recommended to handle the optional lastUpdated property with a fallback default in sessionUtils.ts to comply with repository rules and avoid potential runtime issues.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces safe disposal of session configurations during session teardown and error handling, and adds support for resolving empty sessions by UUID (bypassing interactive terminal filters) via a new resolveSessionById method. It also adds validation to prevent path traversal via the session identifier. The reviewer recommends strengthening the session identifier validation by using a strict regular expression to prevent other potential vulnerabilities like null byte injection or control characters.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces secure session loading and proper resource disposal for the Agent Client Protocol (ACP) session manager. It adds path traversal validation for session identifiers, implements direct session resolution by UUID (bypassing interactive filters when empty sessions are allowed), and ensures that session configurations are properly disposed of during initialization failures or authentication errors. Feedback on the changes highlights a potential resource conflict where an existing session is disposed of only after the new session's configuration is initialized; disposing of the existing session beforehand is recommended to prevent port binding or file lock contention from concurrent MCP servers.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces secure session loading and proper configuration disposal in the Agent Client Protocol (ACP) session manager. Key changes include validating session identifiers against path traversal, allowing resolution of empty sessions by UUID, and ensuring config.dispose() is called during session termination or authentication failures. The review feedback highlights two critical issues: first, Storage should be instantiated with the sessionId to ensure correct scoping; second, the initialization sequence in session creation should be wrapped in a try-catch block to prevent resource leaks of partially initialized configurations when subsequent steps fail.

Comment thread packages/cli/src/acp/acpSessionManager.ts
Comment thread packages/cli/src/acp/acpSessionManager.ts
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request enhances session management in the Gemini CLI by introducing safer configuration disposal during authentication and initialization failures, validating session identifiers to prevent path traversal, and allowing direct session resolution by UUID. While these changes improve robustness and security, the feedback highlights two key areas for improvement: addressing a potential race condition when reloading a session due to synchronous disposal, and wrapping the initialization phase of new sessions in a try-catch block to ensure proper cleanup of active event listeners and subprocesses on failure.

Comment thread packages/cli/src/acp/acpSessionManager.ts
Comment thread packages/cli/src/acp/acpSessionManager.ts
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request makes the Session.dispose method asynchronous to safely handle configuration disposal, introduces path traversal validation for session identifiers, and adds a resolveSessionById method to bypass interactive terminal list filtering when resolving sessions by UUID. The review feedback highlights critical resource leak issues where zombie sessions and event listeners are left behind if errors occur after session creation in newSession and loadSession. Additionally, the reviewer recommends making the session manager's dispose method asynchronous to properly await the now-asynchronous session disposals and prevent race conditions.

Comment thread packages/cli/src/acp/acpSessionManager.ts
Comment thread packages/cli/src/acp/acpSessionManager.ts
Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the disposal lifecycle across GeminiAgent, AcpSessionManager, and Session to be asynchronous, ensuring proper cleanup of configurations and sessions. It also introduces robust error handling during session creation and loading to prevent resource leaks, adds path traversal validation for session identifiers, and implements direct session resolution by UUID via resolveSessionById. A review comment correctly points out that the promise returned by session?.sendAvailableCommands() in acpSessionManager.ts is unhandled and suggests adding a .catch block to prevent unhandled rejections.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the session disposal lifecycle across the ACP (Agent Client Protocol) implementation to be asynchronous, ensuring proper cleanup of resources. It updates GeminiAgent, AcpSessionManager, and Session to support async dispose, and improves error handling during session creation and loading by ensuring configs and sessions are disposed of if initialization fails. Additionally, it introduces path traversal validation for session identifiers, adds a resolveSessionById helper to bypass interactive terminal filtering, and includes comprehensive unit tests. The feedback highlights an opportunity to improve reliability by explicitly catching and logging errors on floating promises in loadSession instead of bypassing the linter with comments.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the session disposal mechanism to be asynchronous, ensuring proper cleanup of configurations and sessions, and adds robust error handling and disposal during session creation and loading. It also introduces a path traversal check for session identifiers and allows resolving empty sessions via a new allowEmpty option in SessionSelector. The review feedback recommends moving deferred background tasks (such as sendAvailableCommands and streamHistory) to the very end of the initialization blocks in newSession and loadSession to prevent side-effects from executing if the session setup subsequently fails.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request transitions session disposal to be asynchronous across GeminiAgent, Session, and AcpSessionManager to ensure proper resource cleanup, and enhances session loading by allowing direct resolution of sessions by UUID. Feedback on these changes focuses on improving robustness and safety: validating sessionId with a strict regular expression to prevent path traversal bypasses, safely handling the optional messages property on sessionData with fallbacks to avoid runtime errors, and consistently trimming optional timestamp strings when formatting session display information.

Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/acp/acpSessionManager.ts Outdated
Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
Comment thread packages/cli/src/utils/sessionUtils.ts
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request transitions the session disposal flow to be asynchronous across the ACP session management classes, adds validation to prevent path traversal via session identifiers, and introduces a mechanism to resolve empty sessions by UUID. The review feedback highlights two critical robustness improvements in AcpSessionManager: wrapping individual session disposals in try-catch blocks within dispose() to prevent a single failure from halting the entire process, and wrapping existingSession.dispose() in a try-catch-finally block during session loading to guarantee stale sessions are always removed from the manager.

Note: Security Review did not run due to the size of the PR.

Comment thread packages/cli/src/acp/acpSessionManager.ts
Comment thread packages/cli/src/acp/acpSessionManager.ts
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request updates the session disposal lifecycle across GeminiAgent, Session, and AcpSessionManager to be asynchronous, ensuring proper resource cleanup. It enhances error handling during session creation and loading to guarantee that sessions and configurations are safely disposed of if initialization fails. Additionally, it introduces input validation on session identifiers to prevent path traversal vulnerabilities and adds a new resolveSessionById method to bypass interactive terminal filters when resolving sessions by UUID. Comprehensive unit tests have been added to verify these changes. No review comments were provided, so I have no additional feedback to offer.

@DavidAPierce
DavidAPierce added this pull request to the merge queue Oct 1, 2026
Merged via the queue into google-gemini:main with commit a5cdfab Oct 1, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/non-interactive Issues related to GitHub Actions, SDK, 3P Integrations, Shell Scripting, Command line automation priority/p1 Important and should be addressed in the near term. size/l A large sized PR

Projects

None yet

2 participants