Skip to content

fix(subagents): mark background results as non-user messages (#875) - #935

Merged
agegr merged 1 commit into
mainfrom
claude/dazzling-euler-gsngci
Sep 22, 2026
Merged

agegr merged 1 commit into
mainfrom
claude/dazzling-euler-gsngci

Conversation

@agegr

@agegr agegr commented Sep 22, 2026

Copy link
Copy Markdown
Owner

Fixes #875.

The problem is real

Traced through pi 0.87's shipped code to confirm the reported behavior:

  1. A background subagent's completion reaches the parent as a custom message — notifyParent() in lib/subagent-runtime.ts calls sendCustomMessage({ customType: "pi-web:subagent-notification", ... }, { deliverAs: "followUp", triggerTurn: true }).
  2. pi replays custom messages to the model as plain user turns — convertToLlm() in harness/messages.js maps case "custom" to { role: "user", content } with the content verbatim and no marker. The neighboring branchSummary / compactionSummary branches each prepend BRANCH_SUMMARY_PREFIX / COMPACTION_SUMMARY_PREFIX — the pattern the issue points at.
  3. Compaction summarizes from exactly those converted messages — generateSummaryWithRequest (harness/compaction/compaction.js) calls convertToLlm(currentMessages), under a prompt that asks ## Goal [What is the user trying to accomplish?] and ## Constraints & Preferences - [...mentioned by user].

So the whole subagent report arrives unlabeled as user text and gets filed as user intent in the summary's Goal / Constraints sections.

The fix

subagentNotificationText() prefixes the report with a fixed SUBAGENT_NOTIFICATION_PREFIX stating that it is a subagent report, not a message from the user. Fixing it in code rather than through the subagent's prompt keeps the marker from being dropped by the model, as the issue suggests.

Scope is limited to the background notification path. Foreground Agent and get_subagent_result results keep the bare subagentFinalText() — they are already toolResult messages and are structurally distinct from user turns, so they need no marker. The existing foreground test asserts the unprefixed text and locks that in.

  • lib/subagent-extension.ts — add SUBAGENT_NOTIFICATION_PREFIX and subagentNotificationText()
  • lib/subagent-runtime.ts — notifyParent delivers the prefixed text
  • tests in both files; a note under "Built-in subagents" in AGENTS.md

Note on the UI

The prefix line renders inside the chat's custom-message card, which already has a pi-web:subagent-notification header — so it is redundant for a human reader while being necessary for the model. I left it visible rather than stripping it in CustomMessageView, because hiding it would make the displayed text diverge from what is actually persisted and sent, which is misleading when debugging. Happy to change that if you would rather keep the card clean.

Verification

  • node_modules/.bin/tsc --noEmit — clean
  • npm run lint — clean
  • npm test — 1247/1247 pass

🤖 Generated with Claude Code

https://claude.ai/code/session_01JUMhHkpdSYbRoYva7SXjDx


Generated by Claude Code

pi's `convertToLlm` replays every `custom` message to the model as a
plain `user` turn with its content verbatim, and a background subagent's
completion is delivered through `sendCustomMessage`. Compaction then
summarizes from those converted messages under a prompt that asks what
*the user* is trying to accomplish and which constraints *the user*
mentioned, so the subagent's report is read as user intent and lands in
the summary's Goal / Constraints sections.

Prefix the notification with a fixed marker (`SUBAGENT_NOTIFICATION_PREFIX`)
stating that it is a subagent report rather than a user message. Doing it
in code instead of in the subagent's prompt keeps the marker from being
dropped by the model. Foreground `Agent` and `get_subagent_result` results
are unchanged: they are already `toolResult` messages and need no marker.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JUMhHkpdSYbRoYva7SXjDx
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-22T13:19:55.392087Z be95ff4 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@agegr
agegr merged commit 54aa49c into main Sep 22, 2026
2 checks passed
@agegr
agegr deleted the claude/dazzling-euler-gsngci branch October 1, 2026 13:28
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…) (agegr#935)

pi's `convertToLlm` replays every `custom` message to the model as a
plain `user` turn with its content verbatim, and a background subagent's
completion is delivered through `sendCustomMessage`. Compaction then
summarizes from those converted messages under a prompt that asks what
*the user* is trying to accomplish and which constraints *the user*
mentioned, so the subagent's report is read as user intent and lands in
the summary's Goal / Constraints sections.

Prefix the notification with a fixed marker (`SUBAGENT_NOTIFICATION_PREFIX`)
stating that it is a subagent report rather than a user message. Doing it
in code instead of in the subagent's prompt keeps the marker from being
dropped by the model. Foreground `Agent` and `get_subagent_result` results
are unchanged: they are already `toolResult` messages and need no marker.


Claude-Session: https://claude.ai/code/session_01JUMhHkpdSYbRoYva7SXjDx

Co-authored-by: Claude <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…) (agegr#935)

pi's `convertToLlm` replays every `custom` message to the model as a
plain `user` turn with its content verbatim, and a background subagent's
completion is delivered through `sendCustomMessage`. Compaction then
summarizes from those converted messages under a prompt that asks what
*the user* is trying to accomplish and which constraints *the user*
mentioned, so the subagent's report is read as user intent and lands in
the summary's Goal / Constraints sections.

Prefix the notification with a fixed marker (`SUBAGENT_NOTIFICATION_PREFIX`)
stating that it is a subagent report rather than a user message. Doing it
in code instead of in the subagent's prompt keeps the marker from being
dropped by the model. Foreground `Agent` and `get_subagent_result` results
are unchanged: they are already `toolResult` messages and need no marker.


Claude-Session: https://claude.ai/code/session_01JUMhHkpdSYbRoYva7SXjDx

Co-authored-by: Claude <[email protected]>
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.

在 subagent 的结果前拼接一句说明,防止污染 compaction

2 participants