agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

streamEvents() ignores RuntimeContext.runId, breaking SessionLog and AG-UI run correlation

Open chcodex opened this issue 1 month ago • 0 comments

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

  • SessionRecorder writes SessionEvent.executionRunId from RunControl.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 filters SessionEvents by executionRunId and 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:

  1. streamEvents(msgs, rc) reports rc.getRunId() as the execution run id.
  2. call(msgs, rc) and streamEvents(msgs, rc) agree on the run id.
  3. The SessionEvent.executionRunId written by the session log matches rc.getRunId().

chcodex avatar Oct 28 '26 09:10 chcodex