Repository navigation
fix(llminternal): omit foreign thoughts from agent context - #1719
Conversation
…vent When presenting one agent's event to another as "For context:" input, ConvertForeignEvent had no guard for thought parts: a thought with text was re-emitted as "[author] said: <reasoning>" and an empty-text thought was copied through verbatim, replaying an agent's private chain-of-thought into another agent's context. A foreign turn made up only of thoughts produced a bare "For context:" content instead of being dropped. Match adk-python's _present_other_agent_message: skip thought parts as the first step of the per-part loop, and return nil (event dropped by the caller) when only the header remains. ConvertForeignEvent was ported from the pre-fix _convert_foreign_event and never picked up adk-python commit a30851e. Fixes google#1096
Exercise foreign-thought filtering through request construction, including compaction summaries and current-turn boundaries. Preserve the current user when a foreign thought-only event is omitted. Adapt the multiple-thought regression from nuthalapativarun's earlier PR google#1104 (d27bc4d). PratikDhanave's original google#1203 implementation remains in the preceding commit.
jjsasha63
left a comment
There was a problem hiding this comment.
I found no blocking or important issues.
I checked out b71a9a4 and ran go test -race ./internal/llminternal/, which passes. With contents_processor.go reverted to 33377de and the new tests kept,all five new test functions fail. Deleting only the current-turn guard at contents_processor.go:776 fails the four "does not start a turn" subtests, so the guard is covered on its own.
Both call sites test the same predicate (isOtherAgentReply, !HasUsableSummary, ConvertForeignEvent(...) == nil), so an event cannot be chosen as the IncludeContents "none" boundary and then disappear in buildContentsDefaultWithCallSource. Those are the only two callers of ConvertForeignEvent, and both handle nil. When a thought-only foreign event is the last one in the history, the request ends on a model turn. The continuation at contents_processor.go:107-111 then closes it with a user turn.
Parity vs src/google/adk/flows/llm_flows/context/_fencing.py and _contents.py @ adk-python 2.11.0
- Matches: thought parts are dropped before the per-part switch, including thought-marked function calls. The event is omitted when only the preamble remains, as in
_present_other_agent_message. - Diverges: Go still copies empty and ThoughtSignature-only parts through the default branch, where Python drops them. This predates the PR, and the descripton calls it out.
- Not ported:
RunConfig.include_thoughts_from_other_agents. Fencing is handled seperately in #1360.
#785, #1719, #1804, #1802) (#1837) * fix(utils): respect Nullable field on nil values during schema validation (#1566) (cherry picked from commit 0e5797f) * fix(llminternal): re-evaluate toolsets on every step in toolProcessor (#785) (cherry picked from commit 707561d) * fix(llminternal): omit foreign thoughts from agent context (#1719) (cherry picked from commit a03e29e) * fix(llminternal): correct misleading "global" in appendInstructions error (#1804) (cherry picked from commit a3afd7f) * fix(llminternal): guard nil first candidate in streaming aggregator (#1802) (cherry picked from commit 1862aae)
Linked issue
Closes #1096. Continues the stale #1203, with the earlier #1104 regression included.
Problem and solution
ConvertForeignEventcurrently relays another agent'sThoughtparts as context. I carried forward PratikDhanave's fix from #1203, preserving the original commit author, and added request-processing regressions on currentmain. The multiple-thought case is adapted from nuthalapativarun's #1104, as requested in the earlier review.The converter now skips
Thoughtparts and returns nil when filtering leaves no public parts. Its caller omits that event while retaining theHasUsableSummarybypass. Tests also cover compaction holes and confirm that stored session events remain unchanged.Testing the caller exposed one related edge case: with
IncludeContents: "none", a thought-only foreign event could become the current-turn boundary and then disappear during conversion, cutting out the preceding user input. The boundary search now skips foreign events that the converter would discard. Public foreign replies, user messages, own-agent thoughts and usable summaries retain their boundary behavior.Behavior change
Agents no longer receive another agent's
Thoughtparts in relayed conversation history. Mixed events retain their public parts; thought-only foreign events are omitted, including when selecting the current turn. Own-agent thought history and compaction summaries remain available.The existing empty-event contract and
ThoughtSignature-only handling are unchanged. There are no dependency or public API changes.Testing Plan
go mod tidy -diff,golangci-lint runandgolangci-lint fmt --diffpass in both the root module andplugin/agentanalytics, with both modules ingo.work. Linux/amd64, Go 1.26.7, golangci-lint 2.3.1.33377de49cf24da7cda478250708460f5e6479feand the final tests retained, 13 fail and 8 controls pass; there are no compilation failures. Failures includeTestConvertForeignEvent/MultipleThoughtsOnlyDropped,TestContentsRequestProcessor_ForeignThoughts/thought-only_foreign_event_is_omitted, andTestContentsRequestProcessor_ForeignThoughtCurrentTurn/foreign_text_thought_does_not_start_a_turn.Focused command:
Manual Runner integration: reproducible probe, setup and before/after logs. I ran the same five scenarios through the real Runner, in-memory session service and a two-agent sequence. Baseline: 3 fail / 2 pass. This branch: 5 pass. The probe checks complete model-bound contents and verifies that stored events are unchanged.
Both model backends in that probe are deterministic local stubs. I did not call live Gemini; live provider behavior is not verified.
Python alignment and merge order
The filtering follows the default behavior of
_present_other_agent_message. Python also filters invisible events before selecting the current-turn boundary. This fix reuses Go's foreign-event conversion decision; it does not port Python's broader visibility predicate or its signature/function-call exceptions.#1360 independently changes the same converter to fence relayed content. I can rebase onto it if it lands first. The thought-only nil result must survive either merge order; an empty-payload fallback must not restore the original thoughts. Its recording updates remain with that PR's contributors.
Checklist