Skip to content

refactor: rename session persistence to EPISODIC_MEMORY_SAVE_SDK_LOGS (default: off)#39

Draft
techjoec wants to merge 1 commit intoobra:mainfrom
techjoec:fix/session-file-pollution
Draft

refactor: rename session persistence to EPISODIC_MEMORY_SAVE_SDK_LOGS (default: off)#39
techjoec wants to merge 1 commit intoobra:mainfrom
techjoec:fix/session-file-pollution

Conversation

@techjoec
Copy link
Contributor

@techjoec techjoec commented Dec 22, 2025

Summary

Refactor SDK session persistence to be opt-in (default: false) instead of opt-out. Rename env var to better reflect its purpose as a debug feature.

Problem

When running sync or --rebuild, the summarizer calls the Claude Agent SDK's query() function for each conversation. The SDK creates .jsonl session files in ~/.claude/projects/<cwd-path>/ for each call, where <cwd-path> is based on where the CLI/agent runs from.

Impact observed:

  • A rebuild of 1,574 conversations created 23,013 files (178MB)
  • Files include main sessions + SDK agent coordination files
  • These files serve no purpose - summaries are already extracted and stored in the archive
  • Pollutes user's Claude Code projects directory

Solution

Add opt-in EPISODIC_MEMORY_SAVE_SDK_LOGS environment variable (default: false):

# Enable SDK session file creation for debugging
export EPISODIC_MEMORY_SAVE_SDK_LOGS=true
Env Var Value Behavior
(unset) No session files (default)
false No session files
true Creates session files for debugging

Changes

  • Renamed EPISODIC_MEMORY_PERSIST_SESSIONSEPISODIC_MEMORY_SAVE_SDK_LOGS
  • Flipped default: now opt-in instead of opt-out
  • Added documentation to README.md explaining behavior and file locations

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • New Features

    • Added EPISODIC_MEMORY_SAVE_SDK_LOGS environment variable to optionally save SDK session logs. Logs are stored in ~/.claude/projects// (default: disabled).
  • Documentation

    • Updated configuration documentation with the new EPISODIC_MEMORY_SAVE_SDK_LOGS environment variable and its usage details.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 22, 2025

Walkthrough

Adds a new environment variable EPISODIC_MEMORY_SAVE_SDK_LOGS (default: false) and uses it to pass persistSession to the Claude API call in src/summarizer.ts, enabling optional saving of SDK session logs for debugging.

Changes

Cohort / File(s) Change Summary
Summarizer logic
src/summarizer.ts
Read EPISODIC_MEMORY_SAVE_SDK_LOGS and pass persistSession in the callClaude options to enable optional SDK session saving.
Docs / configuration
README.md
Documented new EPISODIC_MEMORY_SAVE_SDK_LOGS env var, default value, and where SDK session logs are saved when enabled.

Sequence Diagram(s)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: renaming the environment variable to EPISODIC_MEMORY_SAVE_SDK_LOGS and making session persistence opt-in (default: off).
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2bc6592 and bf422b7.

⛔ Files ignored due to path filters (1)
  • dist/summarizer.js is excluded by !**/dist/**
📒 Files selected for processing (2)
  • README.md
  • src/summarizer.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/summarizer.ts
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: techjoec
Repo: obra/episodic-memory PR: 0
File: :0-0
Timestamp: 2025-12-22T10:29:41.101Z
Learning: The Claude Agent SDK's query() function supports a `persistSession` boolean option. When set to false, it disables session persistence to disk—preventing session files from being saved to `~/.claude/projects/` directory. This is useful for ephemeral or automated workflows where session history is not needed. The default value is true.
Learnt from: techjoec
Repo: obra/episodic-memory PR: 37
File: src/summarizer.ts:5-31
Timestamp: 2025-12-21T08:53:10.642Z
Learning: The Claude Agent SDK's query() function supports an `env` parameter in its Options type for passing environment variables to subprocesses. The env parameter accepts `{ [envVar: string]: string | undefined }` and should be merged with process.env to preserve essential environment variables.
📚 Learning: 2025-12-22T10:29:41.101Z
Learnt from: techjoec
Repo: obra/episodic-memory PR: 0
File: :0-0
Timestamp: 2025-12-22T10:29:41.101Z
Learning: The Claude Agent SDK's query() function supports a `persistSession` boolean option. When set to false, it disables session persistence to disk—preventing session files from being saved to `~/.claude/projects/` directory. This is useful for ephemeral or automated workflows where session history is not needed. The default value is true.

Applied to files:

  • README.md
📚 Learning: 2025-12-21T09:28:53.838Z
Learnt from: techjoec
Repo: obra/episodic-memory PR: 0
File: :0-0
Timestamp: 2025-12-21T09:28:53.838Z
Learning: API_TIMEOUT_MS is a valid environment variable supported by the Claude Agent SDK for configuring API call timeouts. It can be passed via the SDK's env parameter and is respected by the SDK's timeout mechanism.

Applied to files:

  • README.md
🔇 Additional comments (2)
README.md (2)

147-152: ✅ Clear and accurate documentation for the new environment variable.

The documentation correctly describes the opt-in behavior (default false), purpose (debugging), and file location. The format is consistent with adjacent environment variable documentation in the same section.


161-161: ✅ Appropriate update to the "What's Affected" table.

The addition of "SDK session logs | Yes (SAVE_SDK_LOGS only)" accurately reflects that this component is now controlled by the new environment variable and correctly qualifies it as specific to that setting.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9640aef and c8c4821.

📒 Files selected for processing (1)
  • src/summarizer.ts
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: techjoec
Repo: obra/episodic-memory PR: 37
File: src/summarizer.ts:5-31
Timestamp: 2025-12-21T08:52:59.897Z
Learning: The Claude Agent SDK's query() function supports an `env` parameter in its Options type for passing environment variables to subprocesses. The env parameter accepts `{ [envVar: string]: string | undefined }` and should be merged with process.env to preserve essential environment variables.
Learnt from: techjoec
Repo: obra/episodic-memory PR: 0
File: :0-0
Timestamp: 2025-12-21T09:28:53.818Z
Learning: API_TIMEOUT_MS is a valid environment variable supported by the Claude Agent SDK for configuring API call timeouts. It can be passed via the SDK's env parameter and is respected by the SDK's timeout mechanism.
📚 Learning: 2025-12-21T08:52:59.897Z
Learnt from: techjoec
Repo: obra/episodic-memory PR: 37
File: src/summarizer.ts:5-31
Timestamp: 2025-12-21T08:52:59.897Z
Learning: When using the Claude Agent SDK query() in TypeScript, ensure Options.env is merged with the current process.env. The env type is { [envVar: string]: string | undefined }. Create a merged env object (e.g., const env = { ...process.env, ...options.env };) and pass env to the subprocess so essential environment variables are preserved while allowing overrides.

Applied to files:

  • src/summarizer.ts

options: {
model,
max_tokens: 4096,
persistSession: process.env.EPISODIC_MEMORY_PERSIST_SESSIONS !== 'false',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

# Find and examine the src/summarizer.ts file
find . -name "summarizer.ts" -type f 2>/dev/null | head -5

Repository: obra/episodic-memory

Length of output: 83


🏁 Script executed:

cat -n src/summarizer.ts | head -40

Repository: obra/episodic-memory

Length of output: 1900


Remove the unsupported persistSession option—session persistence cannot be controlled via query() options.

The Claude Agent SDK's query() function accepts an Options type, but persistSession is not a documented option. The property will be silently ignored due to the as any type cast on line 35, meaning sessions will persist regardless of the environment variable setting.

The goal of preventing session pollution during summarization cannot be achieved this way. The SDK automatically creates and persists sessions; there is no public API option to disable this behavior at query time. Consider:

  • Whether sessions should be explicitly closed or discarded after summarization
  • Whether to use a different approach for ephemeral operations

Also, line 28's environment variable parsing only recognizes the exact string 'false'—values like 'FALSE', '0', or 'no' would not prevent session creation, which could confuse users.

🤖 Prompt for AI Agents
In src/summarizer.ts around line 28, remove the unsupported persistSession:
process.env.EPISODIC_MEMORY_PERSIST_SESSIONS !== 'false' option (it is not a
valid query() option and is currently ignored); instead, stop attempting to
control session persistence via query() options and implement explicit session
management: either close/discard the session returned by the SDK after
summarization or use an ephemeral/session-less workflow for these operations per
the SDK capabilities; additionally, normalize the environment flag parsing
(accept case-insensitive values like 'false', '0', 'no') by canonicalizing the
string and checking against a set of falsey values so users aren’t confused by
variants.

@techjoec
Copy link
Contributor Author

@coderabbitai persistSession is a documented SDK option. From agentSdkTypes.d.ts:

/**
 * When false, disables session persistence to disk. Sessions will not be
 * saved to ~/.claude/projects/ and cannot be resumed later. Useful for
 * ephemeral or automated workflows where session history is not needed.
 *
 * @default true
 */
persistSession?: boolean;

Verified in isolated Docker container:

  • Default (no env var) → 3 session files created
  • EPISODIC_MEMORY_PERSIST_SESSIONS=false → 0 session files

The as any cast is for other reasons (SDK types may be incomplete for some options like env), not because persistSession is invalid.

Re: case-insensitivity - this follows the pattern of other env vars in this codebase (PR #37) which use exact string matching. Happy to normalize if maintainer prefers.

@coderabbitai
Copy link

coderabbitai bot commented Dec 22, 2025

Tip

For best results, initiate chat on the files or code changes.

@techjoec, thank you for the detailed clarification and documentation! I appreciate you providing the SDK type definition and the thorough Docker container verification.

You're absolutely right—persistSession is a fully documented SDK option, and your implementation correctly leverages it to prevent session file pollution. The testing results clearly demonstrate the toggle works as intended:

  • Default behavior preserves backward compatibility (3 session files)
  • With EPISODIC_MEMORY_PERSIST_SESSIONS=false → 0 session files as expected

The consistency with PR #37's env var handling pattern (exact string matching) also makes sense for maintaining a uniform approach across the codebase.

Great work on the implementation and testing! 🎉

(\\_/)
(o.o)
(> <) *scribbles notes*

✏️ Learnings added
Learnt from: techjoec
URL: https://github.com/obra/episodic-memory/pull/39

Timestamp: 2025-12-22T10:29:41.092Z
Learning: The Claude Agent SDK's query() function supports a `persistSession` boolean option. When set to false, it disables session persistence to disk—preventing session files from being saved to `~/.claude/projects/` directory. This is useful for ephemeral or automated workflows where session history is not needed. The default value is true.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: techjoec
Repo: obra/episodic-memory PR: 37
File: src/summarizer.ts:5-31
Timestamp: 2025-12-21T08:52:59.897Z
Learning: The Claude Agent SDK's query() function supports an `env` parameter in its Options type for passing environment variables to subprocesses. The env parameter accepts `{ [envVar: string]: string | undefined }` and should be merged with process.env to preserve essential environment variables.

@obra
Copy link
Owner

obra commented Dec 22, 2025

@techjoec - I...had missed the availability of this option and think it should actually default to false. What's your take there?

@techjoec
Copy link
Contributor Author

@techjoec - I...had missed the availability of this option and think it should actually default to false. What's your take there?

💯 But was trying to respect the existing behaviors 😊

@techjoec techjoec force-pushed the fix/session-file-pollution branch from c8c4821 to 2bc6592 Compare December 23, 2025 04:15
@techjoec techjoec marked this pull request as draft December 23, 2025 04:17
…SAVE_SDK_LOGS

Change session file persistence to opt-in (default: false) instead of opt-out.
This better reflects that SDK session files are debug artifacts.

When enabled, creates .jsonl files in ~/.claude/projects/<cwd-path>/
where <cwd-path> is based on where the CLI/agent runs from.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@techjoec techjoec force-pushed the fix/session-file-pollution branch from 2bc6592 to bf422b7 Compare December 23, 2025 12:13
@techjoec techjoec marked this pull request as ready for review December 23, 2025 12:14
@techjoec techjoec changed the title feat: add EPISODIC_MEMORY_PERSIST_SESSIONS toggle for session file creation refactor: rename session persistence to EPISODIC_MEMORY_SAVE_SDK_LOGS (default: off) Dec 23, 2025
@techjoec
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Dec 23, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@techjoec techjoec marked this pull request as draft December 24, 2025 03:51
@techjoec
Copy link
Contributor Author

Actually..I am going to submit another (actually 3 I think...) PR first suggesting some changes to exclusions all together as well as configuration knobs @obra . Curious your feedback..I hate the piles of ENV vars I am ending up with (not just here) and have what to me seems like a better way to do it, and some other goodies. BRB!

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.

2 participants