Skip to content

fix(llminternal): omit foreign thoughts from agent context - #1719

Merged
jjsasha63 merged 3 commits into
google:mainfrom
jabrailkhalil:fix/foreign-agent-thought-filter
Oct 6, 2026
Merged

jjsasha63 merged 3 commits into
google:mainfrom
jabrailkhalil:fix/foreign-agent-thought-filter

Conversation

@jabrailkhalil

Copy link
Copy Markdown
Contributor

Linked issue

Closes #1096. Continues the stale #1203, with the earlier #1104 regression included.

Problem and solution

ConvertForeignEvent currently relays another agent's Thought parts as context. I carried forward PratikDhanave's fix from #1203, preserving the original commit author, and added request-processing regressions on current main. The multiple-thought case is adapted from nuthalapativarun's #1104, as requested in the earlier review.

The converter now skips Thought parts and returns nil when filtering leaves no public parts. Its caller omits that event while retaining the HasUsableSummary bypass. 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 Thought parts 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

  • Build, uncached shuffled race tests, go mod tidy -diff, golangci-lint run and golangci-lint fmt --diff pass in both the root module and plugin/agentanalytics, with both modules in go.work. Linux/amd64, Go 1.26.7, golangci-lint 2.3.1.
  • Focused regressions: 21 cases pass. With the entire production change reverted to 33377de49cf24da7cda478250708460f5e6479fe and the final tests retained, 13 fail and 8 controls pass; there are no compilation failures. Failures include TestConvertForeignEvent/MultipleThoughtsOnlyDropped, TestContentsRequestProcessor_ForeignThoughts/thought-only_foreign_event_is_omitted, and TestContentsRequestProcessor_ForeignThoughtCurrentTurn/foreign_text_thought_does_not_start_a_turn.
  • Mutation checks separately remove the thought filter, empty-result return, caller nil guard, compaction bypass, and current-turn guard and its individual conditions. Each is caught by a named regression.

Focused command:

go test -mod=readonly ./internal/llminternal \
  -run 'TestContentsRequestProcessor_ForeignThought|TestConvertForeignEvent' -count=1

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

  • I have read CONTRIBUTING.md.
  • I have reviewed the complete diff and its callers.
  • Comments explain the current-turn boundary and empty-result handling.
  • Regression tests reproduce the bug without the fix.
  • New and existing tests pass locally in both modules.
  • I have manually exercised the Runner integration described above.
  • No dependent downstream changes are required.

PratikDhanave and others added 2 commits October 4, 2026 03:38
…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 jjsasha63 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jjsasha63
jjsasha63 merged commit a03e29e into google:main Oct 6, 2026
14 checks passed
wolo-lab added a commit that referenced this pull request Oct 14, 2026
#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)
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.

ConvertForeignEvent does not drop another agent's thought parts — diverges from adk-python's _present_other_agent_message

3 participants