Skip to content

refactor: replace boolean endInvocation flag with atomic.Bool to enable cross-context state propagation#1135

Open
hanorik wants to merge 2 commits into
google:mainfrom
hanorik:end_invocation_propagation
Open

refactor: replace boolean endInvocation flag with atomic.Bool to enable cross-context state propagation#1135
hanorik wants to merge 2 commits into
google:mainfrom
hanorik:end_invocation_propagation

Conversation

@hanorik

@hanorik hanorik commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Fixes an issue where EndInvocation() called inside sub-agent callbacks or child nodes did not propagate to parent/sibling contexts because EndInvocation was stored as a value-copied bool.

Changes

  • Shared State: Replaced EndInvocation bool with *atomic.Bool across InvocationContext implementations (agent and internal/context).
  • Propagation: Added EndInvocationPtr() *atomic.Bool so derived child contexts (WithContext, WithICDelta, parallelagent, workflow.AgentNode) share the same underlying atomic flag.
  • Orchestration: Updated sequentialagent and loopagent to check ctx.Ended() and stop execution when a sub-agent ends the invocation.

Testing Plan

  • Added unit tests in sequentialagent, agent, and internal/context packages verifying that EndInvocation() propagates across child/sub-agent contexts.
  • Ran go test -race ./....

@hanorik
hanorik requested review from baptmont and wolo-lab July 8, 2026 16:25

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

Thanks for this — the value-copy propagation bug is real, and the red→green tests (TestSubAgentCallbackEndInvocationPropagated, TestSequentialAgent_SubAgentEndInvocationPropagated) capture it well. I reproduced it locally: both new tests fail on main and pass with this change, the full go test -race ./... suite is green (one unrelated remoteagent timing flake), and there's no public API change. Keeping EndInvocationPtr() off the public interface via a type assertion is a nice touch.

I left a few inline notes. The main things I'd like to see before merge:

  • Live path isn't coveredsequentialAgent.RunLive doesn't have the Ended() short-circuit that Run now has (inline).
  • ParallelAgent semantics need a deliberate decision — sharing one flag across siblings changes isolation behavior (inline).
  • initEndInvocation isn't thread-safe — worth eager-initializing (inline).
  • Test coverageloopagent, parallelagent (shared atomic), and workflow/agent_node.go are changed but untested; could you add cases for those?
  • Rebase on main — the branch predates the runner.NewInMemory addition, so an API diff against current main looks misleadingly like a removal; a rebase clears it. (It also sets up the small follow-up that #370 describes.)

Overall the core mechanism and the sequential propagation are solid — nice work.

Comment thread agent/workflowagents/sequentialagent/agent.go
Comment thread agent/workflowagents/parallelagent/agent.go Outdated
Comment thread agent/agent.go Outdated
@hanorik
hanorik force-pushed the end_invocation_propagation branch from 5dce794 to 19ab0e0 Compare July 9, 2026 12:50
@hanorik

hanorik commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Also added missing test coverage.

@hanorik
hanorik requested a review from karolpiotrowicz July 9, 2026 13:00
@hanorik
hanorik force-pushed the end_invocation_propagation branch from 4bc0961 to 51eb2fe Compare July 9, 2026 13:27

@wolo-lab wolo-lab 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.

The fix looks good.

There's one thing worrying me. adk-python decided explicitly not to fix it: google/adk-python#1452.

Submitting this fix will introduce the difference in behavior between adks. Let me sync with them first.

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.

3 participants