agentscope-java
agentscope-java copied to clipboard
streamEvents() ignores RuntimeContext.runId, breaking SessionLog and AG-UI run correlation
Summary
ReActAgent.streamEvents(msgs, context) does not adopt RuntimeContext.runId when it creates the RunControl for the execution. call(msgs, context) does. The same RuntimeContext therefore produces different execution run ids depending on the invocation path.
Impact
SessionRecorderwritesSessionEvent.executionRunIdfromRunControl.runId(), so the session log records a framework-generated 32-char hex instead of the caller's id.AgentEvent.execution().runId()reports the same generated id.- Any integration that correlates an external run id (AG-UI
runId, tracing id, request id) with the session log breaks. In our case AG-UI attach filtersSessionEvents byexecutionRunIdand drops every event, so recovery replays nothing.
Reproduction
RuntimeContext rc = RuntimeContext.builder()
.sessionId("s1").userId("u1").runId("run-explicit").build();
agent.call(List.of(msg), rc) // control.runId() == "run-explicit"
agent.streamEvents(List.of(msg), rc) // control.runId() == <generated hex>
Root cause
io.agentscope.core.ReActAgent#buildAgentStream:
RunControl inherited = RunControl.current(view, getAgentId());
RunControl control = inherited != null ? inherited : new RunControl(getAgentId());
Compare io.agentscope.core.agent.AgentBase#runInContext:
RunControl control = managed
? supplied
: new RunControl(getAgentId(), rc == null ? null : rc.getRunId());
Expected
streamEvents should adopt RuntimeContext.runId, consistent with call and with the existing javadoc:
RuntimeContext#getRunId: "aligned with AgentRun#runId()"AgentRun#create: "so the handle and the RuntimeContext identify the same execution"
Fix
RuntimeContext source = context != null
? context
: view.getOrDefault(RUNTIME_CONTEXT_KEY, RuntimeContext.empty());
RunControl inherited = RunControl.current(view, getAgentId());
RunControl control = inherited != null
? inherited
: new RunControl(getAgentId(), source.getRunId());
inherited still wins, so subagent chains that already share a RunControl are unaffected. When context is null the behaviour is unchanged because RuntimeContext.empty() already carries a generated id.
Tests
Add a regression test asserting that:
streamEvents(msgs, rc)reportsrc.getRunId()as the execution run id.call(msgs, rc)andstreamEvents(msgs, rc)agree on the run id.- The
SessionEvent.executionRunIdwritten by the session log matchesrc.getRunId().