Skip to content

fix: 修复 lastCacheSafeParams 跨会话残留导致 post-turn fork 上下文污染#1304

Merged
claude-code-best merged 1 commit into
claude-code-best:mainfrom
universe-hcy:fix/cache-safe-params-slot
Jul 20, 2026
Merged

fix: 修复 lastCacheSafeParams 跨会话残留导致 post-turn fork 上下文污染#1304
claude-code-best merged 1 commit into
claude-code-best:mainfrom
universe-hcy:fix/cache-safe-params-slot

Conversation

@universe-hcy

@universe-hcy universe-hcy commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

整体修复的起因是我在实际使用上遇到了输出信息与输入信息不一致的问题,排查的思路分两个方向:

  1. Anthropic的opus 4.8等模型在推理时出现混乱,将其他用户的请求返回至了当前任务
  2. harness层上下文机制可能存在一些问题
image

我目前在采用mitmproxy抓包API信息排查第一种情况,(从token消耗量上来看,第一种可能性较大,尽管这有些不合常理,毕竟Anthropic这么大的公司)同时也在看harness层的一些问题。
以下为发现的一些问题,对应改动以及可复现单测(这里确实是存在一些问题,不过个人评估与上面的问题并不完全rootcase):
全局槽 lastCacheSafeParams 从不清空,/clear(regenerateSessionId)与进程内 /resume(switchSession)后残留旧会话完整历史,会被 /recap、SDK
side_question/promptSuggestion 拼进发给 API 的请求前缀,导致回复引用另一段 对话。

  • 槽迁到 src/utils/cacheSafeParamsSlot.ts:保存时记 sessionId,读取时不匹配 则自愈清空并释放旧消息数组
  • clearConversation 显式 saveCacheSafeParams(null) 立即释放内存
  • 迁移四处调用点 import(stopHooks/print/btw/generateRecap)
  • biome.json 排除 dist-stable 构建产物快照,避免 precheck 扫描改写
  • 新增 8 个单测,用真实 regenerateSessionId/switchSession 复现两条路径

Summary by CodeRabbit

  • Bug Fixes

    • Improved cache-safe parameter handling across conversation clearing, session changes, resume flows, recaps, and related commands.
    • Prevented stale cached data from being reused after a session changes or is reset.
    • Ensured clearing a conversation immediately releases its cached snapshot.
  • Tests

    • Added coverage for persistence, clearing, overwriting, and session-based invalidation of cached parameters.
  • Chores

    • Excluded stable build output from configuration processing.

全局槽 lastCacheSafeParams 从不清空,/clear(regenerateSessionId)与进程内
/resume(switchSession)后残留旧会话完整历史,会被 /recap、SDK
side_question/promptSuggestion 拼进发给 API 的请求前缀,导致回复引用另一段
对话。

- 槽迁到 src/utils/cacheSafeParamsSlot.ts:保存时记 sessionId,读取时不匹配
  则自愈清空并释放旧消息数组
- clearConversation 显式 saveCacheSafeParams(null) 立即释放内存
- 迁移四处调用点 import(stopHooks/print/btw/generateRecap)
- biome.json 排除 dist-stable 构建产物快照,避免 precheck 扫描改写
- 新增 8 个单测,用真实 regenerateSessionId/switchSession 复现两条路径

Co-Authored-By: claude-fable-5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7cc8cdf6-3c23-49f9-a293-d86fc0ab698a

📥 Commits

Reviewing files that changed from the base of the PR and between 52554d7 and 73a07c6.

📒 Files selected for processing (9)
  • biome.json
  • src/cli/print.ts
  • src/commands/btw/btw.tsx
  • src/commands/clear/conversation.ts
  • src/commands/recap/generateRecap.ts
  • src/query/stopHooks.ts
  • src/utils/__tests__/cacheSafeParamsSlot.test.ts
  • src/utils/cacheSafeParamsSlot.ts
  • src/utils/forkedAgent.ts

📝 Walkthrough

Walkthrough

Changes

Cache-safe parameter lifecycle

Layer / File(s) Summary
Session-scoped slot and validation
src/utils/cacheSafeParamsSlot.ts, src/utils/__tests__/cacheSafeParamsSlot.test.ts
Adds session-bound storage, clearing, stale-session invalidation, and coverage for persistence, overwrites, and session changes.
Caller rewiring and clear invalidation
src/utils/forkedAgent.ts, src/query/stopHooks.ts, src/cli/print.ts, src/commands/btw/btw.tsx, src/commands/recap/generateRecap.ts, src/commands/clear/conversation.ts
Moves cache-safe parameter access to the new utility and explicitly clears the snapshot during conversation reset.

Biome path exclusion

Layer / File(s) Summary
dist-stable exclusion
biome.json
Adds !!**/dist-stable to the Biome file exclusion patterns.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: claude-code-best

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title accurately summarizes the main fix: session-scoped lastCacheSafeParams to prevent cross-session context pollution.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@claude-code-best
claude-code-best merged commit 7beeb9c into claude-code-best:main Jul 20, 2026
2 checks passed
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