Skip to content

fix(core): keep recreated session files loadable after deletion#27905

Open
i-anubhav-anand wants to merge 2 commits into
google-gemini:mainfrom
i-anubhav-anand:fix/27279-append-record-metadata
Open

fix(core): keep recreated session files loadable after deletion#27905
i-anubhav-anand wants to merge 2 commits into
google-gemini:mainfrom
i-anubhav-anand:fix/27279-append-record-metadata

Conversation

@i-anubhav-anand

Copy link
Copy Markdown

Summary

Fixes #27279.

appendRecord() guards only on this.conversationFile being non-null — it never checks whether the file still exists on disk. If session cleanup (or a manual delete) removes the session file while the process is still running, the next fs.appendFileSync() recreates it containing only the record being appended, with no leading metadata line. The recreated file then has message records but no sessionId, so loadConversationRecord() can never load it: a zombie session that list/resume silently ignore.

Fix

Before appending, if the file is gone but the recorder still holds the in-memory cachedConversation, re-write the conversation's metadata line first, then append the record. The recreated file stays a valid, loadable session.

  • The check is a single fs.existsSync per append and only does extra work in the (rare) deletion case.
  • It does not fire during initial metadata creation (cachedConversation is not yet set) or normal appends (the file exists), so the happy path is unchanged.

Testing

  • Added a regression test in chatRecordingService.test.ts: initialize a session, record a message, delete the file mid-session, record another message, then load it. Verified it fails before this change (loader returns null) and passes after.
  • npx vitest run src/services/chatRecordingService.test.ts → 51 passed.
  • eslint (touched files) and npm run typecheck (packages/core) clean.

appendRecord() only checked that this.conversationFile was non-null, not
that the file still existed. If session cleanup (or a manual delete)
removed the file while the process was still running, the next
appendFileSync() recreated it containing only the appended record, with
no leading metadata line. The resulting file held messages but no
sessionId, so loadConversationRecord() could never load it — a zombie
session that list/resume silently ignored.

Before appending, if the file is gone but we still hold the in-memory
conversation, re-write its metadata line first so the recreated file
stays a valid, loadable session.

Fixes google-gemini#27279
@i-anubhav-anand i-anubhav-anand requested a review from a team as a code owner June 14, 2026 11:23
@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 addresses an issue where session files could become corrupted or unreadable if they were deleted while the application was still running. By verifying the existence of the session file before appending new records, the system can now gracefully restore the necessary metadata, preventing the creation of 'zombie' sessions that cannot be reloaded.

Highlights

  • Session File Recovery: Implemented a check in appendRecord to detect if the session file has been deleted while the process is still running.
  • Metadata Restoration: Added logic to re-write the conversation metadata line if the file is missing, ensuring that recreated session files remain valid and loadable.
  • Regression Testing: Added a new test case in chatRecordingService.test.ts to verify that session files are correctly recovered after being deleted mid-session.
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/m A medium sized PR label Jun 14, 2026
@github-actions

github-actions Bot commented Jun 14, 2026

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 56
  • Additions: +55
  • Deletions: -1
  • Files changed: 2

@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 adds file recovery logic to ChatRecordingService to handle cases where the session file is deleted mid-session, recreating the file with metadata first. However, the current implementation only writes the metadata and the new record, which silently discards all previous messages in the conversation history. To prevent data loss, the reviewer suggests also writing back all previous messages and the memory scratchpad when recreating the file.

Comment thread packages/core/src/services/chatRecordingService.ts
@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality labels Jun 14, 2026
…file

Addressing review feedback: when the session file is recreated after being
deleted mid-session, write the full in-memory conversation (metadata line
plus every cached message), not just the metadata. Otherwise the prior
messages — still held in memory — were silently dropped from the persisted
file. The test now asserts both the earlier and the new message survive.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality priority/p2 Important but can be addressed in a future release. size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File recreation drops the metadata line

1 participant