Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

bugfix: on refresh previous turns events are leaking in current running turn if runiing turns does not have any event (#949)

harshil-209668137584a1d6
diff --git a/.changeset/resume-user-tip-baseline.md b/.changeset/resume-user-tip-baseline.md
new file mode 100644
index 000000000..93db3d2ed
--- /dev/null
+++ b/.changeset/resume-user-tip-baseline.md
@@ -0,0 +1,5 @@
+---
+"@truefoundry/trueforge-assistant-ui-runtime": patch
+---
+
+On refresh mid-turn, baseline all prior root model messages for a new user tip so the previous turn's content does not leak into the resumed stream.
diff --git a/packages/assistant-ui-runtime/src/convertTurnMessages.ts b/packages/assistant-ui-runtime/src/convertTurnMessages.ts
index adaa04b8b..6901efdb4 100644
--- a/packages/assistant-ui-runtime/src/convertTurnMessages.ts
+++ b/packages/assistant-ui-runtime/src/convertTurnMessages.ts
@@ -467,10 +467,18 @@ function attachRunningTurn(snapshot: SessionSnapshot, runningTurn: Turn | undefi
return snapshot;
}
const pendingUserText = extractTurnUserText(runningTurn.input);
+ // Tip is not in `turns` yet. A new user tip must baseline every prior root
+ // model.message (same as live send) — otherwise computeGroupRootBaseline
+ // treats the last completed user turn as the active group and that turn's
+ // content leaks into resume after refresh.
+ const groupRootBaseline =
+ pendingUserText !== undefined
+ ? [...(snapshot.fold.threads.get(ROOT_THREAD_ID)?.modelMessageIds ?? [])]
+ : computeGroupRootBaseline(snapshot.turns);
return replaceSessionSnapshot(snapshot, {
runningTurn,
unstable_resume: true,
- groupRootBaseline: computeGroupRootBaseline(snapshot.turns),
+ groupRootBaseline,
...(pendingUserText !== undefined
? {
pendingUser: {
diff --git a/packages/assistant-ui-runtime/test/convertTurnMessages.test.ts b/packages/assistant-ui-runtime/test/convertTurnMessages.test.ts
index a319b0a53..98fa4cc84 100644
--- a/packages/assistant-ui-runtime/test/convertTurnMessages.test.ts
+++ b/packages/assistant-ui-runtime/test/convertTurnMessages.test.ts
@@ -2553,13 +2553,107 @@ describe('buildSnapshotFromSessionEvents', () => {
expect(snapshot.turns[0]?.id).toBe('t1');
expect(snapshot.runningTurn).toBe(runningTurn);
expect(snapshot.unstable_resume).toBe(true);
- expect(snapshot.groupRootBaseline).toBeDefined();
+ expect(snapshot.groupRootBaseline).toEqual(['m1']);
expect(snapshot.pendingUser).toMatchObject({
turnId: 't2',
content: 'in progress',
});
});
+ it('baselines all prior root model messages when resuming a new user tip', async () => {
+ const runningTurn = {
+ id: 't3',
+ state: { status: 'running' },
+ input: [{ type: 'user.message', content: 'search random opic' }],
+ createdAt,
+ } as unknown as Turn;
+
+ const items: SessionEventItem[] = [
+ {
+ turnId: 't1',
+ event: {
+ type: 'turn.created',
+ id: 'evt-c1',
+ turnId: 't1',
+ input: [{ type: 'user.message', content: 'test' }],
+ state: { status: 'running' },
+ createdAt,
+ },
+ },
+ {
+ turnId: 't1',
+ event: modelMessage({ id: 'm1', threadId: ROOT_THREAD_ID, content: 'Hello! How can I assist you today?' }),
+ },
+ {
+ turnId: 't1',
+ event: {
+ type: 'turn.done',
+ id: 'evt-d1',
+ state: { status: 'done', requiredActions: [], completedAt: createdAt },
+ createdAt,
+ } as TurnDoneEvent,
+ },
+ {
+ turnId: 't2',
+ event: {
+ type: 'turn.created',
+ id: 'evt-c2',
+ turnId: 't2',
+ input: [{ type: 'user.message', content: 'hello' }],
+ state: { status: 'running' },
+ createdAt,
+ },
+ },
+ {
+ turnId: 't2',
+ event: modelMessage({
+ id: 'm2',
+ threadId: ROOT_THREAD_ID,
+ content: 'Hello! How can I help you today?',
+ reasoningContent: 'The user says "test" then "hello".',
+ }),
+ },
+ {
+ turnId: 't2',
+ event: {
+ type: 'turn.done',
+ id: 'evt-d2',
+ state: { status: 'done', requiredActions: [], completedAt: createdAt },
+ createdAt,
+ } as TurnDoneEvent,
+ },
+ {
+ turnId: 't3',
+ event: {
+ type: 'turn.created',
+ id: 'evt-c3',
+ turnId: 't3',
+ input: [{ type: 'user.message', content: 'search random opic' }],
+ state: { status: 'running' },
+ createdAt,
+ },
+ },
+ ];
+
+ const snapshot = await buildSnapshotFromSessionEvents(mockServerWithEvents([runningTurn], items), SESSION_ID);
+
+ expect(snapshot.turns).toHaveLength(2);
+ // Must include m2 — computeGroupRootBaseline(completedTurns) would omit it and
+ // leak turn 2 content onto the resumed tip.
+ expect(snapshot.groupRootBaseline).toEqual(['m1', 'm2']);
+ expect(snapshot.pendingUser).toMatchObject({
+ turnId: 't3',
+ content: 'search random opic',
+ });
+
+ const messages = projectSessionMessages(snapshot);
+ expect(messages.map(message => message.role)).toEqual(['user', 'assistant', 'user', 'assistant', 'user']);
+ expect(messages.at(-1)).toMatchObject({
+ id: 't3-user',
+ role: 'user',
+ });
+ });
+
it('calls onProgress after each completed turn', async () => {
const items: SessionEventItem[] = [
{