Repository navigation
feat(run): add opt-in cleanup of the Claude Code session directory - #394
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d2e1a75e71
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Both findings are fixed in aec238e. P1 (external workspace). P2 (lossy name match). A canonical-name match is now only a candidate. The cleanup script removes a directory only when every Tests: |
zpzjzj
left a comment
There was a problem hiding this comment.
The cleanup makes sense, but enabling it by default changes existing behavior. Before merging, could we add an explicit opt-in flag?
- Add
--cleanup-sessions, defaulting tofalse, preserving the current behavior of deleting the workspace while retaining sessions. - If exposed in YAML, use
cleanup_sessions. - Let
--no-deletetake precedence: when both flags are set, preserve both the workspace and sessions, and report that session cleanup was skipped. - Document that cleanup currently supports only
claude_codeand removes the entire corresponding project directory, including sessions and memory. Keep the safeguards for externally owned workspaces, uncertain ownership, and shared directories. - Follow up on other built-in agents and custom engines through #267. For unsupported agents, explicitly report that cleanup is unsupported when the flag is enabled.
Please add tests and documentation covering the default behavior, explicit cleanup, and interaction with --no-delete before merging.
Review feedback on alibaba#394: removing the Claude Code project directory by default changes existing behaviour, and those sessions are user-visible history that survives an evaluation today. - --cleanup-sessions (default false) and environment.cleanup_sessions ask for it; the runner ORs the two, so YAML can enable it for a suite. - --no-delete wins over the request, and the run says why cleanup was skipped. - Only claude_code implements SessionDirCleaner. When the flag is set for any other engine the run reports that cleanup is unsupported instead of silently doing nothing. - The existing safeguards stay: externally owned workspaces are preserved, and a directory is removed only when every session file in it records this workspace. - Docs: the flag in both CLI references, a "Session cleanup" section in writing-evals, and the CHANGELOG entry now describes the opt-in. Refs alibaba#366
|
All five points are in 7b79191.
Tests cover each of those: default, explicit cleanup, Real CLI, macOS, |
With environment.type: none, every claude_code case left a directory under ~/.claude/projects/<workspace-key>/ after the run: the workspace was deleted and the transcript directory was not. Repeated runs piled eval sessions into the user's own Claude Code history, where session search and memory sync pick them up as the user's own. The evaluator now calls an optional agent.SessionDirCleaner hook before it closes the per-case runtime, and only when the workspace is deleted too. The Claude Code adapter implements it with the same canonical-name match the session lookup uses, so the directory the adapter read the transcript from is the directory it removes. --no-delete keeps both. Tests: the adapter removes the matching directory and leaves an unrelated one alone; the evaluator calls the hook once when the workspace is deleted and not at all when it is kept. Each fails with its fix removed. Refs alibaba#366
Review feedback on the first version of this change: - The canonical name match is lossy: workspaces whose paths differ only in punctuation share one project directory. A directory is now removed only when every session file in it records this workspace, so a directory shared with another workspace survives. - --workspace passes a directory the caller owns, and NoneRuntime preserves it even when deletion is requested. The project directory is now cleaned only when the runtime reports it owns the workspace (new runtime.WorkspaceOwner). Both behaviours have a test that fails when its guard is removed. Refs alibaba#366
Review feedback on alibaba#394: removing the Claude Code project directory by default changes existing behaviour, and those sessions are user-visible history that survives an evaluation today. - --cleanup-sessions (default false) and environment.cleanup_sessions ask for it; the runner ORs the two, so YAML can enable it for a suite. - --no-delete wins over the request, and the run says why cleanup was skipped. - Only claude_code implements SessionDirCleaner. When the flag is set for any other engine the run reports that cleanup is unsupported instead of silently doing nothing. - The existing safeguards stay: externally owned workspaces are preserved, and a directory is removed only when every session file in it records this workspace. - Docs: the flag in both CLI references, a "Session cleanup" section in writing-evals, and the CHANGELOG entry now describes the opt-in. Refs alibaba#366
7b79191 to
8a3b567
Compare
|
Thanks for the update. I found a couple of issues that should be addressed before merging:
For the cleanup tests, it would be helpful to cover mixed cwd values, nested tool input, and conflicting values beyond the current scan limit. |
|
One correction on the follow-up: #267 is closed as not planned (2026-10-26). Your reasoning there was that a session id alone does not establish live resources and that local CLI history may be useful for debugging — which is also why this PR is opt-in rather than default. I therefore left the other engines on the "reported unsupported" path instead of reopening it. If you want the follow-up tracked, say which you prefer and I will do it: reopen #267, or open a new issue scoped to the qoder/qwen session directories. Either way the shape is one |
|
Reopening #267 sounds reasonable to me. For this PR, I’ll leave the scope up to you. A more complete solution is welcome, but keeping it Claude-only is also fine if Claude is the only engine you can validate in your current environment. Either way, please address the review findings above and clearly document the supported scope. |
Review feedback on alibaba#394: - An explicit --cleanup-sessions=false now overrides environment.cleanup_sessions: true; only an unset flag falls back to the config. - Ownership is decided from each transcript's parsed cwd/directories fields, never a string search: a cwd inside a tool argument no longer counts, and a directory is removed only when every transcript records this workspace. Directories whose transcripts disagree, fail to parse, or record nothing are kept, as is one whose transcript is too large to read in full. - Directory discovery matches the session lookup: canonical-name matches first, every directory when none matches, so a truncated or hashed name is not silently skipped. - Tests cover mixed workspaces, a nested tool-input cwd, a conflict beyond the old 64 KiB scan limit, a malformed transcript, a transcript without identity, and a hashed directory name. Refs alibaba#366
|
All three are in 8312e17. 1. CLI precedence. An explicit 2. Ownership. Cleanup no longer searches for the cwd string. Each transcript line is parsed and only its top-level 3. Discovery. Candidate discovery is now the lookup's two-phase shape: directories whose canonicalised name matches the workspace key first, and every directory at that depth when none does, so a truncated or hashed name is not silently skipped. There is a test for a hashed directory name holding this run's transcript: it is removed, and a sibling without identity is kept. Re-ran the real CLI after the change (macOS, On #267: I cannot reopen an issue I did not open (no triage rights here), so I left the scope Claude-only, as you said is fine, and documented that the other engines report "unsupported". If you want the follow-up tracked, reopen #267 or tell me to open a scoped issue and I will. |
Summary
An evaluation deletes its workspace, but Claude Code keeps a project directory outside it:
~/.claude/projects/<workspace-key>/holds the session transcript and amemory/folder, so repeated runs add up in the user's own session history.This adds an explicit way to remove it, off by default:
--cleanup-sessions(defaultfalse) andenvironment.cleanup_sessions: truerequest it; the runner ORs the two so a suite can turn it on in YAML.--no-deletetakes precedence: the workspace and the sessions are both preserved, and the run reports that cleanup was skipped.claude_codeimplements it today. With the flag set for another engine, the run reports that session cleanup is unsupported rather than doing nothing silently.Related issues
Fixes #366
Changes
agent.SessionDirCleaner: optional interface, implemented byClaudeCodeAgent. It removes the project directory for this run's workspace key, using the same canonical-name match as the session lookup, so the directory the adapter read its transcript from is the one it deletes.evaluator: calls the hook before closing the per-case runtime, and only when cleanup was requested and the workspace is deleted. It reports, once per run, every reason a requested cleanup did not run (no-delete, externally owned workspace, unsupported agent).runtime.WorkspaceOwner:NoneRuntimereports whether it owns the workspace, so an externally owned--workspacekeeps its sessions.--cleanup-sessionsin both CLI references; a "Session cleanup" section inwriting-evals.md; CHANGELOG under Unreleased → Fixed.--no-deleteprecedence with its message, externally owned workspace, unsupported agent with its message, YAML opt-in, and the adapter's own behaviour (matching directory removed, unrelated directory kept, shared directory kept, idempotent).Test plan
make testmake verifyenvironment.type: none, Claude Code 2.1.317 — counting new directories under~/.claude/projects:Negative controls: deleting
rm -rffrom the cleanup script makes the adapter test fail (project directory ... still exists); removing thecleanupSessionsguard makes the "not requested" evaluator case fail (CleanupSessionDir calls = 1, want 0); removing thedeleteWorkspaceguard makes the--no-deletecase fail.Notes for reviewers
memory/because the transcript and the memory entry are written by the CLI into the same directory and nothing in the adapter knows which files are which.*.jsonlin it records this workspace, so a directory shared by two workspaces that differ only in punctuation is left alone.Disclosure
This PR was written with AI assistance: DeepSeek Harness (deepseek-v4.1-flash) drafted the change, the tests and this description.
I am wangchongfei. I reviewed every line of this diff and ran the checks above myself before opening it. I take full responsibility for the change.
I am stating this here, and on my profile, because a maintainer should know who — and what — they are reviewing.