Repository navigation
fix(agent-hooks): keep each agent's prompt and tool memory separate on a shared pane (STA-9582) - #26391
fix(agent-hooks): keep each agent's prompt and tool memory separate on a shared pane (STA-9582)#26391Jinwoo-H wants to merge 9 commits into
Conversation
A nested agent on a pane (codex exec run by Claude) wrote its prompt into the pane's shared cache, and the outer agent's later events read it back. Prompt/tool caches are now keyed per hook source, turn resets only clear the resetting agent's caches, and normalizers that read the pane's last row (Codex side-chat fence, OpenCode main-agent clock, Pi model/subagent restatements, OMP approval hold, Cursor late response) only treat it as their history when they wrote it.
…locally and over the relay Real hook bodies through AgentHookServer and through the relay into the desktop: Claude's row ends on Claude's prompt after a nested Codex run; Codex events leave Claude's caches untouched; producer entries move, clear and drop with the pane and tab; a nested Codex Interrupt is not mistaken for a side chat; OpenCode and Pi ignore a row Claude wrote.
…wrote A saved row another agent wrote can keep Claude's label; hydration now decides by the row's hook source, so it never seeds that agent's prompt into Claude's memory.
…amp session-start clear
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (23)
🚧 Files skipped from review as they are similar to previous changes (3)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review. 📝 WalkthroughWalkthroughAgent hook prompt and tool caches now use keys scoped to both pane and producer. Provider handlers also use producer-matched prior statuses in selected paths, and several lifecycle paths clear producer-specific state. Codex lead-turn state records its session ID. Tests cover nested Claude and Codex events, cache isolation and cleanup, persisted-row source checks, and interruption handling. Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to The remaining reported gaps predate this change. No change-specific issue identified here prevents merging after normal checks. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
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. Comment |
…leanups Pi decides 'my row' by hook source alone, like Cursor; the Claude wait clears share one assistant-message trim; Amp's session start clears both keys in one loop; the nested-agent test shares its Claude setup.
ELI5
When one coding agent runs another inside the same terminal (for example Claude runs
codex exec), both agents report to Orca from that terminal. Orca kept a single "last prompt" and "last tool" memory per terminal, so Codex's prompt was saved over Claude's. When Claude reported again, Orca read back Codex's prompt and showed it on Claude's row, even after Claude finished. Each agent now has its own memory.What Changed
Before: Claude is asked "refactor the parser" and runs
codex exec "summarize the diff"as a tool. After Codex finishes, Claude's next tool update and its final "done" both show "summarize the diff" on the Claude row. This happens locally and on SSH/WSL panes.After: Claude's row shows "refactor the parser" through its next update and its final done. Codex's prompt stays with Codex.
How it works:
The hook listener's prompt and tool memories (now
lastPromptByProducerKey,lastToolByProducerKey) are now stored per terminal pane and per reporting agent, under the key<paneKey>\0<source>(producerCacheKey). The\0suffix is the format the existing pane move, pane teardown and tab-close cleanup already handle, so those paths needed no change and still cover every entry.When an agent starts a new session, it clears only its own prompt and tool memory and its own agent-specific state. Before, a Droid, Pi, Devin, jcode, Amp or Grok session start on a pane also cleared Claude's prompt and Grok's, jcode's, Amp's and Antigravity's turn state. This is an intended behavior change.
After a restart, Claude's turn memory is restored only from a saved row whose hook source is Claude. Before, it was restored from any row labelled Claude, including a row another agent had written.
Some normalizers read the pane's last row as their own history. They now read it only when they wrote it (
producerPreviousStatus). Before:"Wrote it" is decided by the row's hook source alone. Cursor and Pi used to decide by the row's agent label, so this is a behavior change for them. A row labelled Cursor or Pi that another agent wrote no longer counts as theirs. A row they wrote that the desktop relabelled as the outer agent now does.
canAcceptClaudeCompactCompletionalready requires the previous row to be Claude's. That check covers local ingest and the relay-row prompt carry inserver-ingest-remote.ts, so neither leaked. The relay defers that check to the desktop, so its listener now applies the same rule.Codex's "ephemeral side chat" filter compared each event with the pane row's session id. When Claude owns the row, that is Claude's session, so a nested Codex's own
Interrupt(which has no transcript path) was dropped as a side chat. Codex now keeps its own root session id in its own state and compares against that.Why
The listener runs in Orca's main process for local panes and in the relay for SSH/WSL panes. It is the one place every agent event passes through. Scoping the memory there fixes local and remote panes the same way, with no per-agent branches and no reader-side overrides.
Alternatives considered:
/clearand resume. Splitting by session would lose the turn label exactly where it should carry over. Every case found here was a different agent, not a different session.worktree psand the relay would still publish the wrong value.This PR does not change who owns a pane's row. That is a separate change; this one makes sure each agent builds its own row from its own state.
Limits and known gaps:
qwen-codereuses Claude's normalizer, so it shares Claude's memory and everyclaude*pane map. A Qwen run nested inside Claude on one pane is not separated. Separating it would change how the server reads those maps by pane.lastLaunchTokenByPaneKeystays per pane. It is the terminal's launch identity, which every agent in that terminal inherits, and OpenCode's shared-server rebinding depends on it being per pane.SessionEndre-publishes the pane's previous row unchanged (agent-hook-listener.ts, around line 215). If another agent wrote that row, Claude's exit carries that agent's fields.tmux-agent-hook-owner.tspasses its store's previousmainAgentinto the OpenCode normalizer without checking which agent wrote it.Linked Issue
STA-9582 (listener part)
Visual Proof
N/A: no layout change. The row text changes from the nested agent's prompt to the owning agent's prompt; the integration tests below show this with real hook bodies.
Testing
New:
src/main/agent-hooks/server-nested-agent-producer-caches.test.tsAgentHookServerin this order: Claude UserPromptSubmit, then Claude PreToolUse (codex exec), then Codex SessionStart, UserPromptSubmit and Stop, then Claude PostToolUse and Stop. The row ends on Claude's prompt.New:
src/relay/agent-hook-server-nested-agent-producer-caches.test.ts. Same order through the relay into the desktop server.New:
src/shared/agent-hook-listener-producer-previous-row.test.ts. OpenCode and Pi ignore a row Claude wrote, and a saved row labelled Claude but written by Codex does not restore Claude's prompt.Added to
agent-hook-listener-codex-interrupt.test.ts: a nested CodexInterruptis applied while Claude owns the row.On the base commit, all four new behaviors fail; each fails for the reason described above. All pass with this change.
Run: the listener,
src/main/agent-hooks,src/relay/agent-hook*andsrc/shared/agent-hook*suites, pluspnpm tc,oxlint,oxfmtandcheck:code-quality:changed.Platforms: macOS unit and integration tests only. The relay path is covered by the relay test, not tested live over SSH.
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Agent skill upstream boundary
Notes
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)