bug: prevent spawn_agent from returning stale final text - #81
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
spawn_agentcan return stale partial text instead of the child agent's final answer when the final assistantmessage_endcontains multiple non-empty text parts.getFinalText()documents and is used as the last non-empty assistant text block selector, but it searched text parts in forward order inside the final assistant message.Severity and impact
This is a correctness bug in normal subagent delegation. When a child emits an early text part, performs a tool call, and then emits the actual final answer as a later text part in the same assistant message, the parent receives the earlier draft/partial text. That can mislead the parent agent or user even though the child produced the correct final output.
Evidence
Relevant files and functions:
home/.pi/agent/extensions/subagents/process.ts,getFinalText()home/.pi/agent/extensions/subagents/index.ts,execute()returnsgetFinalText(details.messages)as the tool contenthome/.pi/agent/extensions/subagents/tests/process.test.ts, new regression testObserved incorrect behavior before the fix:
Given a final assistant message with content parts:
getFinalText()returned"draft answer"because it iteratedmessage.contentfrom the beginning.Why this is a real defect:
spawn_agentuses this value as the child result returned to the parent.Reproduce
Setup:
cd home/.pi/agent/extensions npm installTriggering condition:
A child assistant message contains multiple non-empty text parts in the same final
message_end.Exact command:
Actual result before the fix:
Expected result:
getFinalText()returns"final answer after tool".Root cause
getFinalText()searched messages from newest to oldest, but searched each assistant message's content parts from oldest to newest. For the newest assistant message, that selected the first non-empty text part instead of the last one.Fix
Iterate the final assistant message's
contentarray in reverse order, returning the newest non-empty text part.Validation
Commands run:
Regression test added:
getFinalText returns the last text part within the final assistant messageScope
This PR only changes final text selection for subagent output and adds focused regression coverage. It does not change subprocess spawning, JSON event parsing, tool-call accounting, rendering, or agent discovery.