Skip to content

feat(core): add per-session memory extraction to SessionSummaryService#24765

Open
SandyTao520 wants to merge 7 commits intomainfrom
st/feat/session-memory-extraction
Open

feat(core): add per-session memory extraction to SessionSummaryService#24765
SandyTao520 wants to merge 7 commits intomainfrom
st/feat/session-memory-extraction

Conversation

@SandyTao520
Copy link
Copy Markdown
Contributor

Summary

Add per-session memory extraction that generates structured notes from past conversations in a single LLM call, saving both a 1-line summary and a full markdown scratchpad (memoryScratchpad) into the session JSON file. This is the foundation for a future memory consolidation system.

Details

  • New method generateMemoryExtraction() on SessionSummaryService — reuses the same filter/window/format pattern as generateSummary() but with a larger message window (50 vs 20), longer truncation (2000 chars vs 500), 30s timeout, and a structured extraction prompt.
  • Single LLM call produces both the summary (parsed from the # heading) and the full markdown scratchpad with sections: ## What was done, ## How the user works, ## What we learned, ## What went wrong.
  • New memoryScratchpad field on ConversationRecord stores the extraction inline in the session JSON.
  • sessionSummaryUtils.ts updated to call generateMemoryExtraction() first, falling back to generateSummary() if extraction fails.
  • Bug fix in getPreviousSession() — previously only checked the most recent session file (which is typically the current empty session). Now iterates through files to find the first eligible previous session.

Related Issues

Related to #18007

How to Validate

  1. Run unit tests:

    npm test -w @google/gemini-cli-core -- src/services/sessionSummaryService.test.ts
    npm test -w @google/gemini-cli-core -- src/services/sessionSummaryUtils.test.ts
  2. Manual test:

    • Start a session, have a multi-message conversation, then exit.
    • Start a new session — the previous session should get memoryScratchpad populated.
    • Check with:
      cat <session-file> | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('memoryScratchpad','(none)')[:500])"

Pre-Merge Checklist

  • Updated relevant documentation and README (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

Add generateMemoryExtraction() to SessionSummaryService that produces
structured memory notes (memoryScratchpad) alongside a 1-line summary
in a single LLM call. Notes include sections for what was done, how the
user works, what was learned, and what went wrong.

Also fix getPreviousSession() to iterate past the current active session
instead of only checking the most recent file.
@SandyTao520 SandyTao520 requested a review from a team as a code owner April 6, 2026 18:44
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli bot commented Apr 6, 2026

Hi @SandyTao520, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this.

We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines.

Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed.

Thank you for your understanding and for being a part of our community!

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

Size Change: +11 kB (+0.03%)

Total Size: 34 MB

Filename Size Change
./bundle/chunk-6XX6OGDS.js 0 B -3.16 MB (removed) 🏆
./bundle/chunk-COROWTX4.js 0 B -14.8 MB (removed) 🏆
./bundle/core-HTSUUI7S.js 0 B -45.5 kB (removed) 🏆
./bundle/devtoolsService-UGLGCZV3.js 0 B -28.4 kB (removed) 🏆
./bundle/interactiveCli-KHINQZSC.js 0 B -1.65 MB (removed) 🏆
./bundle/oauth2-provider-OELU3DIV.js 0 B -9.16 kB (removed) 🏆
./bundle/chunk-OSRGEMKN.js 14.8 MB +14.8 MB (new file) 🆕
./bundle/chunk-SQPZY3UZ.js 3.16 MB +3.16 MB (new file) 🆕
./bundle/core-JOV64QI2.js 45.5 kB +45.5 kB (new file) 🆕
./bundle/devtoolsService-N7Y5Z3MZ.js 28.4 kB +28.4 kB (new file) 🆕
./bundle/interactiveCli-W4FH6VHJ.js 1.65 MB +1.65 MB (new file) 🆕
./bundle/oauth2-provider-7W4B5FCY.js 9.16 kB +9.16 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size
./bundle/bundled/third_party/index.js 8 MB
./bundle/chunk-34MYV7JD.js 2.45 kB
./bundle/chunk-5AUYMPVF.js 858 B
./bundle/chunk-5PS3AYFU.js 1.18 kB
./bundle/chunk-664ZODQF.js 124 kB
./bundle/chunk-DAHVX5MI.js 206 kB
./bundle/chunk-IUUIT4SU.js 56.5 kB
./bundle/chunk-OGWWODAT.js 1.96 MB
./bundle/chunk-RJTRUG2J.js 39.8 kB
./bundle/devtools-36NN55EP.js 696 kB
./bundle/dist-T73EYRDX.js 356 B
./bundle/events-XB7DADIJ.js 418 B
./bundle/gemini.js 554 kB
./bundle/getMachineId-bsd-TXG52NKR.js 1.55 kB
./bundle/getMachineId-darwin-7OE4DDZ6.js 1.55 kB
./bundle/getMachineId-linux-SHIFKOOX.js 1.34 kB
./bundle/getMachineId-unsupported-5U5DOEYY.js 1.06 kB
./bundle/getMachineId-win-6KLLGOI4.js 1.72 kB
./bundle/memoryDiscovery-JNNGTYL3.js 980 B
./bundle/multipart-parser-KPBZEGQU.js 11.7 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 222 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 229 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 13.4 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB
./bundle/src-QVCVGIUX.js 47 kB
./bundle/tree-sitter-7U6MW5PS.js 274 kB
./bundle/tree-sitter-bash-34ZGLXVX.js 1.84 MB

compressed-size-action

@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 implements a new memory extraction feature designed to consolidate session information into structured markdown notes. By leveraging a single LLM call, the system now generates both a concise summary and a detailed scratchpad containing insights on user goals, preferences, and outcomes. This change serves as a foundational step for future memory consolidation capabilities and includes robust fallback logic and improved session file discovery to ensure reliable data collection.

Highlights

  • New Memory Extraction Service: Introduced generateMemoryExtraction() in SessionSummaryService to generate structured markdown notes (memoryScratchpad) and a 1-line summary in a single LLM call.
  • Data Model Update: Added memoryScratchpad field to ConversationRecord to store extracted session notes.
  • Improved Session Selection: Updated getPreviousSession() to iterate through session files to find the first eligible session, rather than only checking the most recent one.
  • Fallback Mechanism: Configured sessionSummaryUtils to attempt memory extraction first, falling back to the standard summary generation if extraction fails.
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 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 counter productive. 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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 implements a memory extraction feature within the SessionSummaryService, transitioning from simple summaries to structured markdown notes that capture session outcomes, keywords, and user preferences. The ConversationRecord interface now includes a memoryScratchpad field, and the summary utility has been updated to search for eligible sessions and perform this extraction with a fallback mechanism. Feedback focuses on enhancing security by sanitizing prompt inputs to prevent injection, ensuring Unicode-safe message truncation, and optimizing performance by limiting the number of session files scanned.

@gemini-cli gemini-cli bot added the area/unknown Triage automation assigns this label to issues that it is unable to classify label Apr 6, 2026
Add MEMORY_EXTRACTION and MEMORY_EXTRACTION_SKIPPED events to track
extraction success/failure rates, duration, output size, and fallback
usage. Emit events from sessionSummaryUtils at each decision point.
…y-extraction

# Conflicts:
#	packages/core/src/telemetry/clearcut-logger/clearcut-logger.ts
#	packages/core/src/telemetry/clearcut-logger/event-metadata-key.ts
- Revert check in getPreviousSession to look for `conversation.summary`
  to prevent infinite retry loops and unintended backfilling.
- Add lookback window slice to avoid unbounded sequential reads of
  all session files.
- Refactor generateSummary to use the same formatConversation function
  to remove code duplication.
- Fix telemetry bug on extraction fallback.
- Implement unicode-safe truncation.
- Add memoryScratchpad to check in saveSummaryOnly race guard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/unknown Triage automation assigns this label to issues that it is unable to classify

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant