Skip to content

fix(agent-hooks): keep each agent's prompt and tool memory separate on a shared pane (STA-9582) - #26391

Open
Jinwoo-H wants to merge 9 commits into
mainfrom
jinwoo/sta-9582-listener-per-producer
Open

Jinwoo-H wants to merge 9 commits into
mainfrom
jinwoo/sta-9582-listener-per-producer

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 12 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​462 $\color{#cf222e}{\Huge{\mathbf{−}}}$​28 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​434
Prod 29 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​224 $\color{#cf222e}{\Huge{\mathbf{−}}}$​150 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​74

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 \0 suffix 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:

    • OpenCode's main-agent clock could continue from another agent's clock.
    • Pi could restate another agent's row as a Pi model change.
    • The OMP approval hold and Cursor's late-response rule read another agent's state.

    "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.

    • Claude's manual compact could copy another agent's prompt into Claude's memory, but only in the relay's own cache. On the desktop, canAcceptClaudeCompactCompletion already requires the previous row to be Claude's. That check covers local ingest and the relay-row prompt carry in server-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:

  • Store the reporting agent next to one shared prompt. Rejected: agents take turns. The outer agent's prompt must survive the inner run, so both must be stored.
  • Scope by session instead of agent. Rejected: an agent's session id changes on /clear and 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.
  • Hide the wrong prompt in the sidebar. Rejected: that would be a reader-side override. Mobile, worktree ps and 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-code reuses Claude's normalizer, so it shares Claude's memory and every claude* 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.
  • A row with no recorded source counts as no agent's history until that agent's next event. Rows have recorded their source since August 2026, and current relays always send it.
  • lastLaunchTokenByPaneKey stays 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.
  • Follow-ups:
    • Claude's process-exit SessionEnd re-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.ts passes its store's previous mainAgent into 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.ts

    • Sends real Claude and Codex hook bodies over HTTP to AgentHookServer in 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.
    • Codex events leave Claude's prompt, tool, lead and session-owner state unchanged.
    • Pane move and teardown move and clear both agents' entries.
    • Closing the tab drops them.
  • 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 Codex Interrupt is 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* and src/shared/agent-hook* suites, plus pnpm tc, oxlint, oxfmt and check: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

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH/WSL: the same listener code runs in the relay, so remote panes get the same fix. No relay-to-desktop data changes.
  • Version mix: no wire change. An older relay keeps its old behavior.
  • Saved state: the prompt and tool memories are not saved to disk, so there is nothing to migrate.
  • Performance: one string concatenation per hook event.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

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.
@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fbe103f8-a66b-4c0f-bdb1-6ff4398303c5
📥 Commits

Reviewing files that changed from the base of the PR and between a6aa0ad and 5d9ec04.

📒 Files selected for processing (23)
  • config/scripts/agent-hook-normalizer-roundtrip-benchmark.mjs
  • src/main/agent-hooks/server-closed-tab-suppression.test.ts
  • src/main/agent-hooks/server-nested-agent-producer-caches.test.ts
  • src/main/agent-hooks/server/server-tab-cleanup.ts
  • src/shared/agent-hook-listener-claude-subagents.test.ts
  • src/shared/agent-hook-listener-claude-turn-state.test.ts
  • src/shared/agent-hook-listener-extraction-characterization.test.ts
  • src/shared/agent-hook-listener-opencode-tui-identity.test.ts
  • src/shared/agent-hook-listener-producer-previous-row.test.ts
  • src/shared/agent-hook-listener.ts
  • src/shared/agent-hook-listener/listener-state.ts
  • src/shared/agent-hook-listener/prompt-fields.ts
  • src/shared/agent-hook-listener/providers/amp-events.ts
  • src/shared/agent-hook-listener/providers/antigravity-events.ts
  • src/shared/agent-hook-listener/providers/claude-events.ts
  • src/shared/agent-hook-listener/providers/claude-roster-state.ts
  • src/shared/agent-hook-listener/providers/claude-status-build.ts
  • src/shared/agent-hook-listener/providers/codex-events.ts
  • src/shared/agent-hook-listener/providers/compatible-lifecycle-events.ts
  • src/shared/agent-hook-listener/providers/jcode-events.ts
  • src/shared/agent-hook-listener/providers/pi-family-events.ts
  • src/shared/agent-hook-listener/providers/zcode-events.ts
  • src/shared/agent-hook-status-cache.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/shared/agent-hook-listener/providers/compatible-lifecycle-events.ts
  • src/shared/agent-hook-listener/providers/jcode-events.ts
  • src/shared/agent-hook-listener/providers/zcode-events.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

Agent 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 5d9ec

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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 34.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 50 functions across 41 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly and concisely identifies the main change: separating prompt and tool memory for agents sharing a pane.
Description check ✅ Passed The description is comprehensive and follows the required structure. It explains the user impact, implementation, rationale, linked issue identifier, visual proof, testing, known gaps, and compatibili…
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

…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.

This branch has not been deployed

No deployments
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.

1 participant