You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by as...@apache.org on 2026/09/24 07:36:56 UTC
(maka) branch main updated: fix: place steering by its durable runtime event, not the Host queue echo (#5675)
This is an automated email from the ASF dual-hosted git repository.
Astro-Han pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/maka.git
The following commit(s) were added to refs/heads/main by this push:
new 0a5b9dc951 fix: place steering by its durable runtime event, not the Host queue echo (#5675)
0a5b9dc951 is described below
commit 0a5b9dc9518089c44d183fc4a623a3681e5e8fc7
Author: AstroHan <le...@outlook.com>
AuthorDate: Thu Sep 24 15:36:52 2026 +0800
fix: place steering by its durable runtime event, not the Host queue echo (#5675)
The Runtime pulls steering only at a provider step boundary, so the durable ledger always reads assistant(N), steer, assistant(N+1). The live projection could split a step around a steer: the Host synthesized a steering_message when a queue entry went in_flight, that echo could reach a client ahead of step N's tail, and the renderer sliced the step and needed a Host-relative `replaced` flag that a projector rebuilt on resubscription cannot know. A reseeded completion then rendered `AA [...]
The steering row is now placed only by the runtime's own steering_message, emitted after the durable write in the same FIFO as the step's content:
- Runtime Host projector: delete the in-flight echo synthesis. A steer already durable in the bootstrap is still suppressed (#3316). A placed steer leaves every queue_update, and the filtered queue is emitted with the row, so no surface (including the CLI) shows it twice before the lease ack.
- Runtime Host coordinator: forwarded session events join the subscriber's delivery barrier, so a catch-up still paying an earlier prefix cannot be overtaken. Work held behind that barrier spends the slow-consumer byte budget, so a stuck subscriber is evicted instead of holding unbounded tool output.
- Renderer live projection: one item per step; a completion replaces its step's text whole. Delete boundary slicing, `sliceStart`, completion slicing and the finalize-every-slice pass. A live steer row hands off to the transcript by its durable user id or at the terminal transcript.
- Queue surfaces (main, Side Conversation, WorkHub) show a pulled in_flight entry until the runtime event places it; the composer already disables editing it.
- Fake backend drains steering only at step boundaries like the real Runtime, including hold-open; only the rewrite scenario still continues one message, because streaming-remount needs a secret split across a remount inside one bubble.
Compatibility: `replaced?: true` leaves TextCompleteEvent / ThinkingCompleteEvent; no consumer outside the renderer read it. A steer enters the timeline when the runtime has written it rather than when the Host leased it, and stays in the queue until then.
Fixes #5669
Generated-by: Claude Code
---
apps/desktop/e2e/streaming-remount.spec.ts | 13 +-
apps/desktop/e2e/workhub-layout.spec.ts | 4 +-
.../main/__tests__/message-queue-ui-state.test.ts | 62 ++++----
.../runtime-host-session-observer.test.ts | 114 +++++++++++++-
.../__tests__/transient-message-projection.test.ts | 10 +-
.../main/__tests__/workhub-send-visibility.test.ts | 5 +-
.../contracts/message-queue-projection.ts | 4 +-
.../workhub/controller/use-workhub-controller.ts | 2 +-
packages/core/src/events.ts | 4 -
.../session-continuity-coordinator.test.ts | 87 +++++++++++
.../src/__tests__/session-projector.test.ts | 65 ++++----
.../runtime-host/src/adapter/session-projector.ts | 88 +++--------
.../src/server/session-continuity-coordinator.ts | 110 ++++++++-----
.../runtime/src/__tests__/fake-backend.test.ts | 24 ++-
packages/runtime/src/test-only/fake-backend.ts | 170 +++++++++++----------
.../src/__tests__/chat-turn-steering-order.test.ts | 104 ++-----------
.../ui/src/__tests__/live-turn-projection.test.ts | 13 ++
packages/ui/src/live-turn-projection.ts | 105 +++----------
18 files changed, 514 insertions(+), 470 deletions(-)
diff --git a/apps/desktop/e2e/streaming-remount.spec.ts b/apps/desktop/e2e/streaming-remount.spec.ts
index 64b2233003..d5046f5855 100644
--- a/apps/desktop/e2e/streaming-remount.spec.ts
+++ b/apps/desktop/e2e/streaming-remount.spec.ts
@@ -287,15 +287,17 @@ test('returning to a live conversation settles output accumulated while away', a
let stopped = false;
const sample = () => {
if (stopped) return;
- const bubble = document.querySelector<HTMLElement>('.maka-bubble-streaming');
+ // The steer ends the waiting message; its acknowledgement streams in the last bubble.
+ const bubbles = [...document.querySelectorAll<HTMLElement>('.maka-bubble-streaming')];
+ const bubble = bubbles.at(-1);
if (bubble) {
const text = bubble.textContent ?? '';
if (observed.texts.at(-1) !== text) observed.texts.push(text);
observed.maxActiveAnimations = Math.max(
observed.maxActiveAnimations,
- bubble
+ ...bubbles.map((element) => element
.getAnimations({ subtree: true })
- .filter((animation) => animation.playState !== 'finished').length,
+ .filter((animation) => animation.playState !== 'finished').length),
);
}
window.requestAnimationFrame(sample);
@@ -311,10 +313,9 @@ test('returning to a live conversation settles output accumulated while away', a
window.requestAnimationFrame(sample);
});
await sessionRow(sidebar, originalSessionId!).click();
- await liveBubble.waitFor({ state: 'attached' });
- await expect(liveBubble).toContainText(backgroundSteering);
+ await expect(liveBubble.filter({ hasText: backgroundSteering })).toHaveCount(1);
- expect((await liveBubble.textContent())?.split(accumulatedOutput)).toHaveLength(2);
+ expect((await page.getByRole('log').textContent())?.split(accumulatedOutput)).toHaveLength(2);
// Playwright's toContainText is a DOM check. Stop on the next animation
// frame so the already-queued sample() records that settled paint first.
const backgroundRestoreObserved = await page.evaluate(
diff --git a/apps/desktop/e2e/workhub-layout.spec.ts b/apps/desktop/e2e/workhub-layout.spec.ts
index 2d4891090f..85500f733e 100644
--- a/apps/desktop/e2e/workhub-layout.spec.ts
+++ b/apps/desktop/e2e/workhub-layout.spec.ts
@@ -446,7 +446,9 @@ test('WorkHub keeps the submitted prompt visible while its agent is still runnin
await workhub.locator(COMPOSER_INPUT).fill('立即调整方向,保持当前任务');
await awaitSendReady(workhub);
await workhub.locator(COMPOSER_INPUT).press('ControlOrMeta+Enter');
- await expect(workhub.locator('.maka-bubble-streaming')).toContainText('Acknowledged steering: 立即调整方向,保持当前任务');
+ await expect(workhub.locator('.maka-bubble-streaming', {
+ hasText: 'Acknowledged steering: 立即调整方向,保持当前任务',
+ })).toHaveCount(1);
const steered = workhub.locator('.maka-user-message').filter({ hasText: '立即调整方向,保持当前任务' });
await expect(steered).toHaveCount(1);
// A queued message is only ever durable at the tail, so sending one has to
diff --git a/apps/desktop/src/main/__tests__/message-queue-ui-state.test.ts b/apps/desktop/src/main/__tests__/message-queue-ui-state.test.ts
index fcdfc44ab8..8bf35b846f 100644
--- a/apps/desktop/src/main/__tests__/message-queue-ui-state.test.ts
+++ b/apps/desktop/src/main/__tests__/message-queue-ui-state.test.ts
@@ -130,26 +130,13 @@ test('queue_update events drive the independent desktop queue projection', () =>
});
assert.equal(transientMessages.size, 0, 'Host evidence retires local placeholders');
- handlers.handleEvent('session-1', {
- type: 'steering_message',
- id: 'steering-message-steer',
- turnId: 'turn-1',
- messageId: 'message-steer',
- ts: 2,
- content: { text: 'adjust this run' },
- });
- assert.equal(transientMessages.size, 0);
- assert.deepEqual(controller.getState().messageQueueBySession['session-1'], {
- queueRevision: 3,
- entries: [{
- entryId: 'entry-next',
- messageId: 'message-next',
- content: { text: 'do this next' },
- placement: 'next_turn',
- state: 'queued',
- }],
- });
-
+ const nextEntry = {
+ entryId: 'entry-next',
+ messageId: 'message-next',
+ content: { text: 'do this next' },
+ placement: 'next_turn' as const,
+ state: 'queued' as const,
+ };
handlers.handleEvent('session-1', {
type: 'queue_update',
id: 'queue-2',
@@ -159,24 +146,29 @@ test('queue_update events drive the independent desktop queue projection', () =>
steering: ['adjust this run'],
followup: ['do this next'],
steeringEntries: [inFlightEntry],
- followupEntries: [{
- entryId: 'entry-next',
- messageId: 'message-next',
- content: { text: 'do this next' },
- placement: 'next_turn',
- state: 'queued',
- }],
+ followupEntries: [nextEntry],
});
- assert.deepEqual(controller.getState().messageQueueBySession['session-1']?.entries, [{
- entryId: 'entry-next',
- messageId: 'message-next',
- content: { text: 'do this next' },
- placement: 'next_turn',
- state: 'queued',
- }]);
- assert.equal(transientMessages.size, 0);
+ assert.deepEqual(
+ controller.getState().messageQueueBySession['session-1']?.entries,
+ [inFlightEntry, nextEntry],
+ 'a pulled message stays pending until the runtime places it',
+ );
assert.equal(transientMessages.size, 0, 'in-flight queue projection must not re-add a local row');
+ handlers.handleEvent('session-1', {
+ type: 'steering_message',
+ id: 'steering-message-steer',
+ turnId: 'turn-1',
+ messageId: 'message-steer',
+ ts: 4,
+ content: { text: 'adjust this run' },
+ });
+ assert.equal(transientMessages.size, 0);
+ assert.deepEqual(controller.getState().messageQueueBySession['session-1'], {
+ queueRevision: 4,
+ entries: [nextEntry],
+ });
+
handlers.handleEvent('session-1', {
type: 'message_admission',
id: 'retracted-message-next',
diff --git a/apps/desktop/src/main/__tests__/runtime-host-session-observer.test.ts b/apps/desktop/src/main/__tests__/runtime-host-session-observer.test.ts
index 073b9e0453..e92339ef85 100644
--- a/apps/desktop/src/main/__tests__/runtime-host-session-observer.test.ts
+++ b/apps/desktop/src/main/__tests__/runtime-host-session-observer.test.ts
@@ -22,6 +22,7 @@ import assert from "node:assert/strict";
import { EventEmitter } from "node:events";
import test from "node:test";
import type { SessionEvent } from '@maka/core/events';
+import { applyLiveTurnBufferEvent, overlayLiveTurn, type LiveTurnBuffer } from '@maka/ui';
import type { SessionObservationMessage } from '../../shared/session-execution-projection.js';
import type { StoredMessage } from '@maka/core/session';
import {
@@ -2662,6 +2663,86 @@ test("reconciles terminal, Goal, interaction, and sidecar state after subscripti
await observer.close();
});
+test('lands a reseeded completion whole on a live step a steering row followed', async () => {
+ const firstEvents = new AsyncFrameQueue();
+ const secondEvents = new AsyncFrameQueue();
+ const recoveredSessions: string[] = [];
+ let openCount = 0;
+ const observer = new RuntimeHostSessionObserver({
+ client: {
+ openSession: async () => {
+ openCount += 1;
+ if (openCount === 1) {
+ return runtimeHostSessionFixture({
+ snapshot: continuitySnapshot(),
+ events: firstEvents,
+ async close() {
+ firstEvents.end();
+ },
+ });
+ }
+ return runtimeHostSessionFixture({
+ snapshot: continuitySnapshot({ projectionRevision: 2, rootTurn: settledSnapshot().rootTurn }),
+ transcript: Promise.resolve([
+ { type: 'user' as const, id: 'user-1', turnId: 'turn-1', ts: 1, text: 'request' },
+ {
+ type: 'assistant' as const, id: 'message-1', turnId: 'turn-1', ts: 2,
+ modelId: 'test-model', text: '', thinking: { text: 'ABC' },
+ },
+ {
+ type: 'user' as const, id: 'steer-1', turnId: 'turn-1', ts: 3,
+ text: 'steer', steeringEventId: 'steer-event',
+ },
+ { type: 'turn_state' as const, id: 'state-1', turnId: 'turn-1', ts: 4, status: 'completed' as const },
+ ]),
+ events: secondEvents,
+ async close() {
+ secondEvents.end();
+ },
+ });
+ },
+ },
+ emitSessionsChanged() {},
+ emitSubscriptionRecovered: (sessionId) => {
+ recoveredSessions.push(sessionId);
+ },
+ now: () => 50,
+ });
+ const target = eventTarget(16);
+ await observer.observe('session-1', 'observer-1', target);
+ const thinking = (sequence: number, startOffset: number, text: string): SubscriptionFrame => ({
+ kind: 'subscription.session_delta', hostEpoch: 'host-1', subscriptionId: 'subscription-1',
+ sequence, sessionId: 'session-1',
+ delta: { kind: 'thinking', turnId: 'turn-1', runId: 'run-1', messageId: 'message-1', startOffset, text },
+ });
+ firstEvents.push(thinking(1, 0, 'AAAA'));
+ firstEvents.push({
+ kind: 'subscription.session_event', hostEpoch: 'host-1', subscriptionId: 'subscription-1',
+ sequence: 2, sessionId: 'session-1', runId: 'run-1',
+ event: {
+ type: 'steering_message', id: 'steer-event', turnId: 'turn-1', ts: 3,
+ messageId: 'steer-1', content: { text: 'steer' },
+ },
+ });
+ firstEvents.push(thinking(3, 4, 'BBBB'));
+ await waitFor(() => target.events.some((event) => event.type === 'thinking_delta' && event.text === 'BBBB'));
+ firstEvents.push({
+ kind: 'subscription.closed', hostEpoch: 'host-1', subscriptionId: 'subscription-1',
+ sequence: 4, reason: 'slow_consumer',
+ });
+ await waitFor(() => recoveredSessions.length === 1);
+
+ let buffer: LiveTurnBuffer | undefined;
+ for (const event of target.events) buffer = applyLiveTurnBufferEvent(buffer, event, 'en');
+ const [turn] = overlayLiveTurn([], buffer![0]!, 'en');
+ assert.deepEqual(
+ turn!.timeline.map((item) =>
+ item.kind === 'user' ? `user:${item.message.text}` : item.kind === 'thinking' ? `thinking:${item.text}` : item.kind),
+ ['thinking:ABC', 'user:steer'],
+ );
+ await observer.close();
+});
+
test('replays durable admission before a terminal successor on subscription recovery', async () => {
const firstEvents = new AsyncFrameQueue();
const secondEvents = new AsyncFrameQueue();
@@ -2959,7 +3040,7 @@ test("publishes form answer acknowledgements for renderer queue retirement", asy
await observer.close();
});
-test("projects Host queue revisions and newly delivered steering messages", async () => {
+test("projects Host queue revisions and places steering from the runtime event", async () => {
const events = new AsyncFrameQueue();
const observer = new RuntimeHostSessionObserver({
client: {
@@ -3015,11 +3096,30 @@ test("projects Host queue revisions and newly delivered steering messages", asyn
},
}),
});
- await waitFor(() => target.events.length === 3);
+ await waitFor(() => target.events.length === 2);
+ events.push({
+ kind: "subscription.session_event",
+ hostEpoch: "host-1",
+ subscriptionId: "subscription-1",
+ sequence: 3,
+ sessionId: "session-1",
+ runId: "run-1",
+ event: {
+ type: "steering_message",
+ id: "steering-event-1",
+ turnId: "turn-1",
+ ts: 80,
+ messageId: "message-steer",
+ content: { text: "Change direction" },
+ },
+ });
+ await waitFor(() => target.events.length === 4);
assert.deepEqual(
- target.events.map((event) => event.type),
- ["queue_update", "steering_message", "queue_update"],
+ target.events.map((event) =>
+ event.type === "queue_update" ? event.steeringEntries?.map((entry) => entry.state) : event.type,
+ ),
+ [["queued"], ["in_flight"], [], "steering_message"],
);
assert.deepEqual(target.events[0], {
type: "queue_update",
@@ -3032,12 +3132,12 @@ test("projects Host queue revisions and newly delivered steering messages", asyn
steeringEntries: [queued],
followupEntries: [],
});
- assert.deepEqual(target.events[1], {
+ assert.deepEqual(target.events[3], {
type: "steering_message",
- id: "host-queue:host-1:2:entry-1",
+ id: "steering-event-1",
turnId: "turn-1",
messageId: "message-steer",
- ts: 90,
+ ts: 80,
content: { text: "Change direction" },
});
await observer.close();
diff --git a/apps/desktop/src/main/__tests__/transient-message-projection.test.ts b/apps/desktop/src/main/__tests__/transient-message-projection.test.ts
index d64678990a..c6cd3aebcb 100644
--- a/apps/desktop/src/main/__tests__/transient-message-projection.test.ts
+++ b/apps/desktop/src/main/__tests__/transient-message-projection.test.ts
@@ -161,8 +161,16 @@ test('derives one queue projection for main and Side Conversation consumers', ()
],
});
- assert.deepEqual(projection.entries.map((entry) => entry.entryId), ['steer', 'next']);
+ assert.deepEqual(projection.entries.map((entry) => entry.entryId), ['in-flight', 'steer', 'next']);
assert.deepEqual(projection.transientMessages, [
+ {
+ id: 'message-in-flight',
+ pendingSteering: true,
+ transientPlacement: 'current_turn',
+ hostTurnId: 'turn-1',
+ ts: 7,
+ text: 'in flight',
+ },
{
id: 'message-steer',
pendingSteering: true,
diff --git a/apps/desktop/src/main/__tests__/workhub-send-visibility.test.ts b/apps/desktop/src/main/__tests__/workhub-send-visibility.test.ts
index 72c7393f5a..c2a7b196de 100644
--- a/apps/desktop/src/main/__tests__/workhub-send-visibility.test.ts
+++ b/apps/desktop/src/main/__tests__/workhub-send-visibility.test.ts
@@ -617,10 +617,11 @@ test('WorkHub Host queue owns restored, consumed and retracted rows without tran
await act(() => project('queued'));
assert.deepEqual(h.controller.messageQueue.entries, [entry]);
assert.deepEqual(h.controller.transientMessages, []);
- await act(() => h.emit({ type: 'steering_message', id: 'consumed', turnId: 'active-turn', ts: 3, messageId: entry.messageId, content: entry.content }));
await act(() => project('in_flight'));
+ assert.deepEqual(h.controller.messageQueue.entries, [{ ...entry, state: 'in_flight' }], 'a pulled message stays pending until the runtime places it');
+ await act(() => h.emit({ type: 'steering_message', id: 'consumed', turnId: 'active-turn', ts: 3, messageId: entry.messageId, content: entry.content }));
assert.deepEqual(h.controller.messageQueue.entries, []);
- assert.deepEqual(h.controller.transientMessages, [], 'an in-flight snapshot cannot resurrect consumed steering');
+ assert.deepEqual(h.controller.transientMessages, []);
await act(() => project('queued'));
await act(async () => { await h.controller.deleteQueuedEntry(entry.entryId); });
await act(() => h.emit({ type: 'queue_update', id: 'removed', turnId: 'active-turn', ts: 4, steering: [], followup: [], steeringEntries: [] }));
diff --git a/apps/desktop/src/renderer/application/contracts/message-queue-projection.ts b/apps/desktop/src/renderer/application/contracts/message-queue-projection.ts
index b3f484380d..c3261c3b2b 100644
--- a/apps/desktop/src/renderer/application/contracts/message-queue-projection.ts
+++ b/apps/desktop/src/renderer/application/contracts/message-queue-projection.ts
@@ -35,9 +35,7 @@ export function deriveMessageQueueProjection(
const entries = [
...(event.steeringEntries ?? []),
...(event.followupEntries ?? []),
- ]
- .filter((entry) => entry.state === 'queued')
- .map((entry) => structuredClone(entry));
+ ].map((entry) => structuredClone(entry));
return {
entries,
transientMessages: entries.map((entry) => ({
diff --git a/apps/desktop/src/renderer/features/workhub/controller/use-workhub-controller.ts b/apps/desktop/src/renderer/features/workhub/controller/use-workhub-controller.ts
index 817a204804..a7e6e00b01 100644
--- a/apps/desktop/src/renderer/features/workhub/controller/use-workhub-controller.ts
+++ b/apps/desktop/src/renderer/features/workhub/controller/use-workhub-controller.ts
@@ -389,7 +389,7 @@ export function useWorkHubController(onSubmit?: () => void) {
const ids = new Set(entries.map((entry) => entry.messageId));
if (pendingQueued.current && ids.has(pendingQueued.current.messageId)) pendingQueued.current.observed = true;
setMessagePresentation((previous) => ({
- messageQueue: { entries: entries.filter((entry) => entry.state === 'queued'), revision: event.queueRevision },
+ messageQueue: { entries, revision: event.queueRevision },
transientMessages: previous.transientMessages.filter((message) => !ids.has(message.id)),
}));
}
diff --git a/packages/core/src/events.ts b/packages/core/src/events.ts
index 95fc47a136..e9f53375a6 100644
--- a/packages/core/src/events.ts
+++ b/packages/core/src/events.ts
@@ -680,8 +680,6 @@ export interface TextDeltaEvent extends BaseEvent {
export interface TextCompleteEvent extends BaseEvent {
type: 'text_complete';
interrupted?: true;
- /** `text` replaces the streamed deltas instead of extending them. */
- replaced?: true;
messageId: string;
text: string;
/** Provider-owned text metadata such as Responses URL citations. */
@@ -715,8 +713,6 @@ export function foldAssistantDelta(
export interface ThinkingCompleteEvent extends BaseEvent {
type: 'thinking_complete';
interrupted?: true;
- /** `text` replaces the streamed deltas instead of extending them. */
- replaced?: true;
messageId: string;
text: string;
/** Anthropic signed thinking — MUST be re-sent on replay. */
diff --git a/packages/runtime-host/src/__tests__/session-continuity-coordinator.test.ts b/packages/runtime-host/src/__tests__/session-continuity-coordinator.test.ts
index c584e4a3aa..51a3e0ab4b 100644
--- a/packages/runtime-host/src/__tests__/session-continuity-coordinator.test.ts
+++ b/packages/runtime-host/src/__tests__/session-continuity-coordinator.test.ts
@@ -1443,6 +1443,93 @@ test('a later message cannot complete ahead of the prefix a subscriber is still
coordinator.close();
});
+test('a steering message cannot overtake the prefix a subscriber is still being paid', async () => {
+ const coordinator = new SessionContinuityCoordinator(
+ HOST_EPOCH,
+ async () => canonical(),
+ new SessionAdmissionGate(),
+ );
+ let first = '';
+ for (let index = 0; index < 24; index += 1) {
+ const text = `${index}:${'x'.repeat(8 * 1024)}`;
+ first += text;
+ await coordinator.acceptRuntimeEvent(SESSION_ID, 'run-1', { ...textEvent(index), text });
+ }
+ const sink = new GatedSink();
+ const connection = attachTestConnection(coordinator, 'connection-steer-order', sink);
+ const opened = await open(coordinator, 'connection-steer-order');
+ connection.activate(opened.subscriptionId);
+
+ await coordinator.acceptRuntimeEvent(SESSION_ID, 'run-1', textCompleteEvent('message-1', first));
+ await coordinator.acceptRuntimeEvent(SESSION_ID, 'run-1', {
+ type: 'steering_message',
+ id: 'steering-event-1',
+ turnId: 'turn-1',
+ ts: 7,
+ messageId: 'steering-message-1',
+ content: { text: 'redirect' },
+ });
+ sink.release();
+ await waitFor(() => sink.frames.some((frame) => frame.kind === 'subscription.session_event'));
+
+ const order = sink.frames.flatMap((frame) =>
+ frame.kind === 'subscription.session_delta' && frame.delta.complete
+ ? [`complete:${frame.delta.messageId}`]
+ : frame.kind === 'subscription.session_event'
+ ? [frame.event.type]
+ : [],
+ );
+ assert.deepEqual(order, ['complete:message-1', 'steering_message']);
+ coordinator.close();
+});
+
+test('events held behind an unpaid prefix spend the slow-consumer budget', async () => {
+ const coordinator = new SessionContinuityCoordinator(
+ HOST_EPOCH,
+ async () => canonical(),
+ new SessionAdmissionGate(),
+ );
+ for (let index = 0; index < 24; index += 1) {
+ await coordinator.acceptRuntimeEvent(SESSION_ID, 'run-1', {
+ ...textEvent(index),
+ text: `${index}:${'x'.repeat(8 * 1024)}`,
+ });
+ }
+ const sink = new GatedSink();
+ const connection = attachTestConnection(coordinator, 'connection-held-budget', sink);
+ const opened = await open(coordinator, 'connection-held-budget');
+ connection.activate(opened.subscriptionId);
+
+ for (let seq = 0; seq < 40; seq += 1) {
+ await coordinator.acceptRuntimeEvent(SESSION_ID, 'run-1', {
+ type: 'tool_output_delta',
+ id: `output-${seq}`,
+ turnId: 'turn-1',
+ ts: seq,
+ sessionId: SESSION_ID,
+ toolCallId: 'tool-1',
+ toolUseId: 'tool-1',
+ seq,
+ stream: 'stdout',
+ chunk: 'z'.repeat(8 * 1024),
+ redacted: false,
+ createdAt: seq,
+ });
+ }
+ sink.release();
+ await waitFor(() => sink.frames.some((frame) => frame.kind === 'subscription.closed'));
+
+ // Evicted while the sink was still stuck on its first frame, not after the
+ // whole prefix and the held events had been drained into the queue.
+ assert.deepEqual(
+ sink.frames.map((frame) =>
+ frame.kind === 'subscription.closed' ? `closed:${frame.reason}` : frame.kind,
+ ),
+ ['subscription.session_delta', 'closed:slow_consumer'],
+ );
+ coordinator.close();
+});
+
// #5365: the Turn ending does not unsay what the Host already streamed. The
// terminal publication used to drop every unpaid backlog, so a subscriber was
// left holding a truncated answer that the client then reported as complete.
diff --git a/packages/runtime-host/src/__tests__/session-projector.test.ts b/packages/runtime-host/src/__tests__/session-projector.test.ts
index 214c435d02..6a8849631b 100644
--- a/packages/runtime-host/src/__tests__/session-projector.test.ts
+++ b/packages/runtime-host/src/__tests__/session-projector.test.ts
@@ -92,7 +92,6 @@ test('applies authoritative replacement once and does not complete it again at T
deltaFrame(2, 5, '', { complete: true, interrupted: true }),
).events;
assert.ok(completed[0]?.type === 'text_complete' && completed[0].interrupted === true);
- assert.equal(completed[0].replaced, true);
assert.deepEqual(
completed.map((event) => [event.type, 'text' in event ? event.text : '']),
[['text_complete', 'final']],
@@ -794,15 +793,13 @@ test('projects the durable steering echo even when the in-flight queue state was
});
});
-test('projects a steering message exactly once across both authoritative paths', () => {
- // The queue in-flight synthesis and the durable session-event echo race;
- // whichever projects the message first suppresses the other.
- const inFlightFirst = new RuntimeHostSessionProjector(
+test('leaves an in-flight steering message in the queue until the runtime event places it', () => {
+ const projector = new RuntimeHostSessionProjector(
snapshot({ queue: queue(2, [steeringEntry('queued')]) }),
createRuntimeHostSessionProjectionSeed([], snapshot()),
() => 10,
);
- const synthesized = inFlightFirst.accept({
+ const pulled = projector.accept({
kind: 'subscription.session_projection',
hostEpoch: 'host-1',
subscriptionId: 'subscription-1',
@@ -810,42 +807,34 @@ test('projects a steering message exactly once across both authoritative paths',
snapshot: snapshot({ queue: queue(3, [steeringEntry('in_flight')]) }),
});
assert.deepEqual(
- synthesized.events.map((event) => event.type),
- ['steering_message', 'queue_update'],
+ pulled.events.map((event) => event.type),
+ ['queue_update'],
);
- assert.deepEqual(inFlightFirst.accept(steeringFrame(2)).events, []);
-
- const echoFirst = new RuntimeHostSessionProjector(
- snapshot({ queue: queue(2, [steeringEntry('queued')]) }),
- createRuntimeHostSessionProjectionSeed([], snapshot()),
- () => 10,
+ assert.deepEqual(
+ projector.seedActive(false).map((event) => event.type),
+ ['queue_update'],
+ );
+ // The runtime event takes the entry out of the queue as it places the row…
+ assert.deepEqual(
+ projector
+ .accept(steeringFrame(2))
+ .events.map((event) => (event.type === 'queue_update' ? event.steeringEntries : event.type)),
+ [[], 'steering_message'],
);
- assert.equal(echoFirst.accept(steeringFrame(1)).events.length, 1);
- const suppressed = echoFirst.accept({
+ // …and the lease ack can trail it, so a queue revision before the ack keeps it out.
+ const beforeAck = projector.accept({
kind: 'subscription.session_projection',
hostEpoch: 'host-1',
subscriptionId: 'subscription-1',
- sequence: 2,
- snapshot: snapshot({ queue: queue(3, [steeringEntry('in_flight')]) }),
+ sequence: 3,
+ snapshot: snapshot({ queue: queue(4, [steeringEntry('in_flight')]) }),
});
assert.deepEqual(
- suppressed.events.map((event) => event.type),
- ['queue_update'],
- );
-});
-
-test('seeds an unrendered in-flight steering message once on rejoin', () => {
- const projector = new RuntimeHostSessionProjector(
- snapshot({ queue: queue(3, [steeringEntry('in_flight')]) }),
- createRuntimeHostSessionProjectionSeed([], snapshot()),
- () => 10,
- );
- assert.deepEqual(
- projector.seedActive(false).map((event) => event.type),
- ['steering_message', 'queue_update'],
+ beforeAck.events.map((event) =>
+ event.type === 'queue_update' ? event.steeringEntries : event.type,
+ ),
+ [[]],
);
- // A live echo of the same message arriving after the seed is the duplicate.
- assert.deepEqual(projector.accept(steeringFrame(1)).events, []);
});
test('suppresses the live echo for a steering message already durable in the bootstrap', () => {
@@ -862,10 +851,12 @@ test('suppresses the live echo for a steering message already durable in the boo
() => 10,
);
- // Durable and in-flight: no synthesis seed…
+ // Durable and in-flight: the queue no longer lists it…
assert.deepEqual(
- projector.seedActive(false).map((event) => event.type),
- ['queue_update'],
+ projector
+ .seedActive(false)
+ .map((event) => (event.type === 'queue_update' ? event.steeringEntries : event.type)),
+ [[]],
);
// …and the late echo of the same message is the duplicate.
assert.deepEqual(projector.accept(steeringFrame(1)).events, []);
diff --git a/packages/runtime-host/src/adapter/session-projector.ts b/packages/runtime-host/src/adapter/session-projector.ts
index e05826d45d..32a0feb4ae 100644
--- a/packages/runtime-host/src/adapter/session-projector.ts
+++ b/packages/runtime-host/src/adapter/session-projector.ts
@@ -31,7 +31,6 @@ import type {
SessionAssistantStreamIdentity,
SessionMessageQueueProjection,
SessionSteeringEvent,
- SteeringMessageSnapshot,
SubscriptionFrame,
LiveTurnSnapshot,
TurnSnapshot,
@@ -93,11 +92,8 @@ export class RuntimeHostSessionProjector {
#snapshot: SessionContinuitySnapshot;
readonly #now: () => number;
readonly #durableTurnByMessage: Map<string, string>;
- // Only live/synthesized messages for the current root belong here. Durable
- // transcript identity stays in the admission map above, so this render
- // ledger cannot grow with the lifetime of the session.
- readonly #renderedSteeringMessageIds = new Set<string>();
readonly #accumulators = new Map<string, AssistantAccumulator>();
+ readonly #placedSteeringMessageIds = new Set<string>();
#projectMessageAdmissions: boolean;
constructor(
@@ -168,7 +164,7 @@ export class RuntimeHostSessionProjector {
const events: SessionEvent[] = [];
const queueEvents =
this.#projectMessageAdmissions || queueHasEntries(this.#snapshot.queue)
- ? [projectQueueUpdate(this.#snapshot.queue, root.turnId, this.#now())]
+ ? [projectQueueUpdate(this.#unplacedQueue(this.#snapshot.queue), root.turnId, this.#now())]
: [];
if (this.#projectMessageAdmissions) {
events.push(
@@ -209,22 +205,6 @@ export class RuntimeHostSessionProjector {
for (const interaction of this.#snapshot.interactions.pending) {
events.push(...projectRuntimeHostInteractionRequest(interaction, this.#now()));
}
- for (const entry of rootQueueInFlight(this.#snapshot.queue)) {
- if (
- this.#durableTurnByMessage.has(entry.messageId) ||
- this.#renderedSteeringMessageIds.has(entry.messageId)
- )
- continue;
- this.#renderedSteeringMessageIds.add(entry.messageId);
- events.push({
- type: 'steering_message',
- id: `host-queue:${this.#snapshot.queue.hostEpoch}:${this.#snapshot.queue.queueRevision}:${entry.entryId}`,
- turnId: root.turnId,
- messageId: entry.messageId,
- ts: this.#now(),
- content: structuredClone(entry.content),
- });
- }
return [...events, ...queueEvents];
}
@@ -383,7 +363,6 @@ export class RuntimeHostSessionProjector {
ts: this.#now(),
text: folded.text,
...(delta.interrupted ? { interrupted: true } : {}),
- ...(replaced ? { replaced: true } : {}),
});
} else if (folded.tail && !replaced) {
events.push({
@@ -401,13 +380,12 @@ export class RuntimeHostSessionProjector {
if (frame.kind === 'subscription.session_event') {
const event = projectSessionEvent(frame);
if (event.type === 'steering_message') {
- if (
- this.#durableTurnByMessage.has(event.messageId) ||
- this.#renderedSteeringMessageIds.has(event.messageId)
- ) {
- return emptyUpdate(events);
+ if (this.#durableTurnByMessage.has(event.messageId)) return emptyUpdate(events);
+ this.#placedSteeringMessageIds.add(event.messageId);
+ const { queue, rootTurn } = this.#snapshot;
+ if (rootTurn && queue.steering.some((entry) => entry.messageId === event.messageId)) {
+ events.push(projectQueueUpdate(this.#unplacedQueue(queue), rootTurn.turnId, this.#now()));
}
- this.#renderedSteeringMessageIds.add(event.messageId);
}
events.push(event);
return emptyUpdate(events);
@@ -422,32 +400,11 @@ export class RuntimeHostSessionProjector {
const root = next.rootTurn;
const startedTurn =
root && (!previousRoot || root.runId !== previousRoot.runId) ? root : undefined;
- if (startedTurn) this.#renderedSteeringMessageIds.clear();
for (const interaction of newlyPendingInteractions(previousSnapshot, next)) {
events.push(...projectRuntimeHostInteractionRequest(interaction, this.#now()));
}
- const enteredActiveTurn =
- root && queueChanged(previousSnapshot.queue, next.queue)
- ? newlyInFlight(previousSnapshot.queue, next.queue)
- : [];
if (root && queueChanged(previousSnapshot.queue, next.queue)) {
- for (const entry of enteredActiveTurn) {
- if (
- this.#durableTurnByMessage.has(entry.messageId) ||
- this.#renderedSteeringMessageIds.has(entry.messageId)
- )
- continue;
- this.#renderedSteeringMessageIds.add(entry.messageId);
- events.push({
- type: 'steering_message',
- id: `host-queue:${next.queue.hostEpoch}:${next.queue.queueRevision}:${entry.entryId}`,
- turnId: root.turnId,
- messageId: entry.messageId,
- ts: this.#now(),
- content: structuredClone(entry.content),
- });
- }
- events.push(projectQueueUpdate(next.queue, root.turnId, this.#now()));
+ events.push(projectQueueUpdate(this.#unplacedQueue(next.queue), root.turnId, this.#now()));
}
if (startedTurn) this.#accumulators.clear();
// Emit the presentation-only compaction-started event when the root Turn
@@ -492,7 +449,6 @@ export class RuntimeHostSessionProjector {
ts: this.#now(),
text: accumulator.text,
...(accumulator.interrupted ? { interrupted: true } : {}),
- ...(accumulator.replaced ? { replaced: true } : {}),
});
}
if (root.status === 'completed') {
@@ -531,6 +487,17 @@ export class RuntimeHostSessionProjector {
}
return events;
}
+
+ // The runtime writes a steering message before the Host acknowledges its
+ // lease, so the queue can still list an entry the timeline already shows.
+ #unplacedQueue(queue: SessionMessageQueueProjection): SessionMessageQueueProjection {
+ const steering = queue.steering.filter(
+ (entry) =>
+ !this.#placedSteeringMessageIds.has(entry.messageId) &&
+ !this.#durableTurnByMessage.has(entry.messageId),
+ );
+ return steering.length === queue.steering.length ? queue : { ...queue, steering };
+ }
}
function emptyUpdate(events: readonly SessionEvent[]): RuntimeHostProjectionUpdate {
@@ -752,23 +719,6 @@ function queueChanged(
return previous.hostEpoch !== next.hostEpoch || previous.queueRevision !== next.queueRevision;
}
-function newlyInFlight(
- previous: SessionMessageQueueProjection,
- next: SessionMessageQueueProjection,
-): Extract<SteeringMessageSnapshot, { state: 'in_flight' }>[] {
- const previousIds = new Set(rootQueueInFlight(previous).map((entry) => entry.entryId));
- return rootQueueInFlight(next).filter((entry) => !previousIds.has(entry.entryId));
-}
-
-function rootQueueInFlight(
- queue: SessionMessageQueueProjection,
-): Extract<SteeringMessageSnapshot, { state: 'in_flight' }>[] {
- return queue.steering.filter(
- (entry): entry is Extract<SteeringMessageSnapshot, { state: 'in_flight' }> =>
- entry.state === 'in_flight',
- );
-}
-
function queueHasEntries(queue: SessionMessageQueueProjection): boolean {
return queue.steering.length > 0 || queue.followup.length > 0;
}
diff --git a/packages/runtime-host/src/server/session-continuity-coordinator.ts b/packages/runtime-host/src/server/session-continuity-coordinator.ts
index 4e10ff14cf..7f93e18227 100644
--- a/packages/runtime-host/src/server/session-continuity-coordinator.ts
+++ b/packages/runtime-host/src/server/session-continuity-coordinator.ts
@@ -179,9 +179,10 @@ interface Subscriber {
/**
* Work that arrived while a backlog was still unpaid. A subscriber has one
* delivery order, so anything produced after the text it is catching up on
- * waits here instead of overtaking it.
+ * waits here instead of overtaking it. It spends the queue's byte budget.
*/
- deferred: Array<() => void>;
+ deferred: Array<{ work: () => void; bytes: number }>;
+ deferredBytes: number;
}
interface AssistantBacklog {
@@ -753,8 +754,11 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
// reads the accumulated stream, so it carries this delta already.
this.#payAssistantBacklog(subscriber, state);
} else {
- this.#deliverInOrder(subscriber, () =>
- this.#enqueueAssistantDelta(subscriber, sessionId, runId, event, kind, startOffset),
+ this.#deliverInOrder(
+ subscriber,
+ () =>
+ this.#enqueueAssistantDelta(subscriber, sessionId, runId, event, kind, startOffset),
+ () => Buffer.byteLength(event.text, 'utf8'),
);
}
}
@@ -823,20 +827,26 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
state.toolResultPreviews.delete(event.toolUseId);
}
for (const subscriber of state.subscribers.values()) {
- const frame: SessionEventFrame = {
- kind: 'subscription.session_event',
- hostEpoch: this.#hostEpoch,
- subscriptionId: subscriber.subscriptionId,
- sequence: subscriber.nextSequence,
- sessionId,
- runId,
- event: projectSessionEvent(
- event,
- sessionId,
- subscriber.principalKind === 'session_guest',
- ),
- };
- this.#enqueue(subscriber, frame);
+ this.#deliverInOrder(
+ subscriber,
+ () => {
+ const frame: SessionEventFrame = {
+ kind: 'subscription.session_event',
+ hostEpoch: this.#hostEpoch,
+ subscriptionId: subscriber.subscriptionId,
+ sequence: subscriber.nextSequence,
+ sessionId,
+ runId,
+ event: projectSessionEvent(
+ event,
+ sessionId,
+ subscriber.principalKind === 'session_guest',
+ ),
+ };
+ this.#enqueue(subscriber, frame);
+ },
+ () => Buffer.byteLength(JSON.stringify(event), 'utf8'),
+ );
}
});
}
@@ -1029,6 +1039,7 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
[...committed.state.assistantStreams.keys()].map((key) => [key, { sent: 0 }]),
),
deferred: [],
+ deferredBytes: 0,
...(transcript ? { transcript } : {}),
};
committed.state.subscribers.set(subscriptionId, subscriber);
@@ -1303,6 +1314,8 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
const inFlight = subscriber.pumping ? subscriber.queue[0] : undefined;
subscriber.queue = [];
subscriber.queuedBytes = 0;
+ subscriber.deferred = [];
+ subscriber.deferredBytes = 0;
subscriber.nextSequence = (inFlight?.frame.sequence ?? subscriber.lastFlushedSequence) + 1;
const frame: SubscriptionFrame = {
kind: 'subscription.closed',
@@ -1522,6 +1535,7 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
?.subscriptionIds.delete(subscriber.subscriptionId);
subscriber.assistantBacklog.clear();
subscriber.deferred = [];
+ subscriber.deferredBytes = 0;
if (!this.#closed && state && removed && state.subscribers.size === 0) {
this.#scheduleInactiveStateCleanup(subscriber.sessionId, state);
}
@@ -1602,16 +1616,19 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
return;
}
subscriber.assistantBacklog.delete(key);
- this.#deliverInOrder(subscriber, () =>
- this.#enqueueAssistantCompletion(
- subscriber,
- subscriber.sessionId,
- runId,
- { ...stream, text: held },
- stream.kind,
- finalText,
- interrupted,
- ),
+ this.#deliverInOrder(
+ subscriber,
+ () =>
+ this.#enqueueAssistantCompletion(
+ subscriber,
+ subscriber.sessionId,
+ runId,
+ { ...stream, text: held },
+ stream.kind,
+ finalText,
+ interrupted,
+ ),
+ () => Buffer.byteLength(finalText, 'utf8'),
);
}
@@ -1703,15 +1720,19 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
#broadcastProjection(state: SessionProjectionState, snapshot: SessionContinuitySnapshot): void {
for (const subscriber of state.subscribers.values()) {
- this.#deliverInOrder(subscriber, () => {
- this.#enqueue(subscriber, {
- kind: 'subscription.session_projection',
- hostEpoch: this.#hostEpoch,
- subscriptionId: subscriber.subscriptionId,
- sequence: subscriber.nextSequence,
- snapshot: projectSessionSnapshot(snapshot, subscriber.principalKind),
- });
- });
+ this.#deliverInOrder(
+ subscriber,
+ () => {
+ this.#enqueue(subscriber, {
+ kind: 'subscription.session_projection',
+ hostEpoch: this.#hostEpoch,
+ subscriptionId: subscriber.subscriptionId,
+ sequence: subscriber.nextSequence,
+ snapshot: projectSessionSnapshot(snapshot, subscriber.principalKind),
+ });
+ },
+ () => Buffer.byteLength(JSON.stringify(snapshot), 'utf8'),
+ );
}
}
@@ -1720,12 +1741,19 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
* on. Every assistant frame and projection goes through here, so the order a
* subscriber sees is the order the Host produced.
*/
- #deliverInOrder(subscriber: Subscriber, work: () => void): void {
+ #deliverInOrder(subscriber: Subscriber, work: () => void, bytes: () => number): void {
+ if (subscriber.phase !== 'open') return;
if (subscriber.assistantBacklog.size === 0 && subscriber.deferred.length === 0) {
work();
return;
}
- subscriber.deferred.push(work);
+ const size = bytes();
+ if (subscriber.queuedBytes + subscriber.deferredBytes + size > MAX_SUBSCRIBER_QUEUED_BYTES) {
+ this.#evictSlowSubscriber(subscriber);
+ return;
+ }
+ subscriber.deferred.push({ work, bytes: size });
+ subscriber.deferredBytes += size;
}
#drainDeferred(subscriber: Subscriber): void {
@@ -1734,7 +1762,9 @@ export class SessionContinuityCoordinator implements SessionContinuityService {
subscriber.deferred.length > 0 &&
subscriber.phase === 'open'
) {
- subscriber.deferred.shift()?.();
+ const next = subscriber.deferred.shift()!;
+ subscriber.deferredBytes -= next.bytes;
+ next.work();
}
}
diff --git a/packages/runtime/src/__tests__/fake-backend.test.ts b/packages/runtime/src/__tests__/fake-backend.test.ts
index 553ac67681..4f57fd8db4 100644
--- a/packages/runtime/src/__tests__/fake-backend.test.ts
+++ b/packages/runtime/src/__tests__/fake-backend.test.ts
@@ -101,8 +101,8 @@ test('pullSteering drains queued messages at step boundaries as steering events'
{ id: 'lease-2', messageId: 'message-2', content: { text: 'and Y' } },
];
const acked: string[] = [];
- const steered: string[] = [];
- let completedText = '';
+ const order: string[] = [];
+ const completions: string[] = [];
for await (const event of backend.send({
turnId: 'turn-1',
text: 'hello',
@@ -110,14 +110,26 @@ test('pullSteering drains queued messages at step boundaries as steering events'
pullSteering: () => (pending.length > 0 ? [pending.shift()!] : []),
ackSteering: (leaseIds) => acked.push(...leaseIds),
})) {
- if (event.type === 'steering_message') steered.push(event.content.text);
- if (event.type === 'text_complete') completedText = event.text;
+ if (event.type === 'steering_message') order.push(`steer:${event.content.text}`);
+ if (event.type === 'text_delta' && order.at(-1) !== `step:${event.messageId}`) {
+ order.push(`step:${event.messageId}`);
+ }
+ if (event.type === 'text_complete') {
+ order.push(`complete:${event.messageId}`);
+ completions.push(event.text);
+ }
}
- assert.deepEqual(steered, ['do X', 'and Y']);
+ // Like the real Runtime, steering never lands inside a step: each drained
+ // message precedes the step that answers it.
+ assert.deepEqual(
+ order.map((entry) => entry.replace(/:[0-9a-f-]{36}$/, '')),
+ ['steer:do X', 'step', 'complete', 'steer:and Y', 'step', 'complete'],
+ );
// Delivery is acknowledged lease by lease.
assert.deepEqual(acked, ['lease-1', 'lease-2']);
// The fake acknowledges the steering it saw, proving it reached the model side.
- assert.match(completedText, /Acknowledged steering: do X \| and Y/);
+ assert.match(completions[0]!, /Acknowledged steering: do X$/);
+ assert.equal(completions[1], 'Acknowledged steering: and Y');
});
test('a batch of leases settles per lease: delivered ones ack, undelivered ones nack', async () => {
diff --git a/packages/runtime/src/test-only/fake-backend.ts b/packages/runtime/src/test-only/fake-backend.ts
index a3ff8c7960..2644b9a28e 100644
--- a/packages/runtime/src/test-only/fake-backend.ts
+++ b/packages/runtime/src/test-only/fake-backend.ts
@@ -38,6 +38,10 @@ import {
} from '../interaction-authority.js';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
+// Every delta must concatenate to text_complete; `.` would silently drop
+// line terminators and make structured Markdown reflow only at completion.
+const chunkText = (text: string, large: boolean): string[] =>
+ text.match(large ? /[\s\S]{1,1024}/g : /[\s\S]{1,9}/g) ?? [text];
export const FAKE_ASK_USER_QUESTION_PROMPT = '__e2e_ask_user_question__';
export const FAKE_ASK_USER_QUESTION_DURING_DRAIN_PROMPT = '__e2e_ask_user_question_during_drain__';
export const FAKE_ASK_SANDBOX_BOUNDARY_PROMPT = '__e2e_ask_sandbox_boundary__';
@@ -103,7 +107,7 @@ export class FakeBackend implements AgentBackend {
const isLargeSteeringResponse = input.text === FAKE_WAIT_FOR_STEERING_LARGE_RESPONSE_PROMPT;
const isSteeringScenario =
input.text === FAKE_WAIT_FOR_STEERING_PROMPT || isLargeSteeringResponse;
- let text = isLargeSteeringResponse
+ const text = isLargeSteeringResponse
? `${'Detailed response. '.repeat(1_400)}Large response complete.`
: input.text === FAKE_MERMAID_PROMPT
? [
@@ -149,17 +153,11 @@ export class FakeBackend implements AgentBackend {
'```',
].join('\n')
: `Fake backend received: ${input.text}${attLine}\n\nThis proves the session stream, SQLite storage, and renderer loop are connected.`;
- // Every delta must concatenate to text_complete; `.` would silently drop
- // line terminators and make structured Markdown reflow only at completion.
- const chunks = text.match(isLargeSteeringResponse ? /[\s\S]{1,1024}/g : /[\s\S]{1,9}/g) ?? [
- text,
- ];
-
- // Mid-turn steering: drain the caller's pending steering at each step
- // boundary (here, between streamed chunks), echoing every message as a
- // `steering_message` so the ledger/transcript render the interjection, and
- // remembering them so the fake reply acknowledges them like a real model.
- const steered: string[] = [];
+ // Mid-turn steering: like the real Runtime, drain the caller's pending
+ // steering only at a step boundary (before the first step and after each
+ // completed one), echoing every message as a `steering_message` so the
+ // ledger/transcript render the interjection, and acknowledging it in the
+ // next step like a real model.
// Lease accounting (backend-types contract): settlement is per LEASE,
// never per batch. A lease is acked only after its OWN echoed event has
// been received by the consumer — the fake has no durable ledger, so
@@ -175,27 +173,27 @@ export class FakeBackend implements AgentBackend {
outstanding.splice(index, 1);
input.ackSteering?.([leaseId]);
};
- const drainSteering = async (): Promise<Array<{ leaseId: string; event: SessionEvent }>> => {
+ const drainSteering = async (): Promise<
+ Array<{ leaseId: string; text: string; event: SessionEvent }>
+ > => {
const leases = (await input.pullSteering?.()) ?? [];
if (leases.length === 0) return [];
outstanding.push(...leases.map((lease) => lease.id));
- return leases.map((lease) => {
- steered.push(lease.content.text);
- return {
- leaseId: lease.id,
- event: {
- type: 'steering_message',
- id: randomUUID(),
- turnId,
- ts: Date.now(),
- messageId: lease.messageId,
- content: lease.content,
- ...(lease.submittedContentDigest
- ? { submittedContentDigest: lease.submittedContentDigest }
- : {}),
- } satisfies SessionEvent,
- };
- });
+ return leases.map((lease) => ({
+ leaseId: lease.id,
+ text: lease.content.text,
+ event: {
+ type: 'steering_message',
+ id: randomUUID(),
+ turnId,
+ ts: Date.now(),
+ messageId: lease.messageId,
+ content: lease.content,
+ ...(lease.submittedContentDigest
+ ? { submittedContentDigest: lease.submittedContentDigest }
+ : {}),
+ } satisfies SessionEvent,
+ }));
};
try {
@@ -204,33 +202,52 @@ export class FakeBackend implements AgentBackend {
const waitingPrefix = rewriteTarget
? 'prefix sk-123456789012345'
: 'Fake backend waiting for the test to stop the Turn.';
+ let waitingMessageId = messageId;
let waitingText = waitingPrefix;
yield {
type: 'text_delta',
id: randomUUID(),
turnId,
ts: Date.now(),
- messageId,
+ messageId: waitingMessageId,
text: waitingText,
};
while (!this.stopped) {
const pending = await drainSteering();
+ if (pending.length > 0 && !rewriteTarget) {
+ yield {
+ type: 'text_complete',
+ id: randomUUID(),
+ turnId,
+ ts: Date.now(),
+ messageId: waitingMessageId,
+ text: waitingText,
+ };
+ }
for (const { leaseId, event } of pending) {
yield event;
settleOutstanding(leaseId);
}
if (pending.length > 0) {
- const nextText = rewriteTarget
- ? `${waitingPrefix}6 NEW streamed after the remount`
- : `${waitingPrefix}\n\nAcknowledged steering: ${steered.join(' | ')}`;
- const delta = nextText.slice(waitingText.length);
- waitingText = nextText;
+ let delta: string;
+ if (rewriteTarget) {
+ // Unlike the real Runtime, this continues the same message after
+ // a steer: streaming-remount needs one secret split across a
+ // remount inside one bubble, and steering is its only trigger.
+ const nextText = `${waitingPrefix}6 NEW streamed after the remount`;
+ delta = nextText.slice(waitingText.length);
+ waitingText = nextText;
+ } else {
+ waitingMessageId = randomUUID();
+ waitingText = `Acknowledged steering: ${pending.map(({ text }) => text).join(' | ')}`;
+ delta = waitingText;
+ }
yield {
type: 'text_delta',
id: randomUUID(),
turnId,
ts: Date.now(),
- messageId,
+ messageId: waitingMessageId,
text: delta,
};
}
@@ -247,66 +264,57 @@ export class FakeBackend implements AgentBackend {
return;
}
- if (isSteeringScenario) {
- let pending = await drainSteering();
- while (pending.length === 0 && !this.stopped) {
- await sleep(5);
- pending = await drainSteering();
- }
+ let pending = await drainSteering();
+ while (isSteeringScenario && pending.length === 0 && !this.stopped) {
+ await sleep(5);
+ pending = await drainSteering();
+ }
+ let stepMessageId = messageId;
+ let stepText = text;
+ for (;;) {
for (const { leaseId, event } of pending) {
yield event;
settleOutstanding(leaseId);
}
- }
-
- for (const chunk of chunks) {
- if (this.stopped) {
- yield { type: 'abort', id: randomUUID(), turnId, ts: Date.now(), reason: 'user_stop' };
+ if (pending.length > 0) {
+ const ack = `Acknowledged steering: ${pending.map(({ text }) => text).join(' | ')}`;
+ stepText = stepText ? `${stepText}\n\n${ack}` : ack;
+ }
+ for (const chunk of chunkText(stepText, isLargeSteeringResponse)) {
+ if (this.stopped) {
+ yield { type: 'abort', id: randomUUID(), turnId, ts: Date.now(), reason: 'user_stop' };
+ yield {
+ type: 'complete',
+ id: randomUUID(),
+ turnId,
+ ts: Date.now(),
+ stopReason: 'user_stop',
+ };
+ return;
+ }
+ if (!isSteeringScenario) await sleep(45);
yield {
- type: 'complete',
+ type: 'text_delta',
id: randomUUID(),
turnId,
ts: Date.now(),
- stopReason: 'user_stop',
+ messageId: stepMessageId,
+ text: chunk,
};
- return;
- }
- if (!isSteeringScenario) await sleep(45);
- for (const { leaseId, event } of await drainSteering()) {
- yield event;
- settleOutstanding(leaseId);
}
yield {
- type: 'text_delta',
+ type: 'text_complete',
id: randomUUID(),
turnId,
ts: Date.now(),
- messageId,
- text: chunk,
+ messageId: stepMessageId,
+ text: stepText,
};
+ pending = await drainSteering();
+ if (pending.length === 0) break;
+ stepMessageId = randomUUID();
+ stepText = '';
}
-
- // Final stranded drain (grok-build safety): a steer that landed after the
- // last boundary still lands in this turn instead of being lost.
- for (const { leaseId, event } of await drainSteering()) {
- yield event;
- settleOutstanding(leaseId);
- }
- if (steered.length > 0) {
- const ack = `\n\nAcknowledged steering: ${steered.join(' | ')}`;
- text += ack;
- yield {
- type: 'text_delta',
- id: randomUUID(),
- turnId,
- ts: Date.now(),
- messageId,
- text: ack,
- };
- }
-
- const ts = Date.now();
- yield { type: 'text_complete', id: randomUUID(), turnId, ts, messageId, text };
yield { type: 'complete', id: randomUUID(), turnId, ts: Date.now(), stopReason: 'end_turn' };
} finally {
if (outstanding.length > 0) input.nackSteering?.(outstanding.splice(0));
diff --git a/packages/ui/src/__tests__/chat-turn-steering-order.test.ts b/packages/ui/src/__tests__/chat-turn-steering-order.test.ts
index 64288f36a8..e777714e4d 100644
--- a/packages/ui/src/__tests__/chat-turn-steering-order.test.ts
+++ b/packages/ui/src/__tests__/chat-turn-steering-order.test.ts
@@ -33,7 +33,6 @@ import { renderTranscriptMarkup } from './transcript-test-dom.js';
import { ChatSurfaceLayout } from '../chat-surface-layout.js';
import { armLiveTurn, type LiveTurnProjection } from '../live-turn-projection.js';
import { applyLiveTurnEvent } from './live-turn-zh.js';
-import { redactSecrets } from '../redact.js';
import type { SessionSummary, StoredMessage } from '@maka/core/session';
test('renders a thinking-only interruption as a divider without an empty answer bubble', () => {
@@ -137,7 +136,7 @@ const timelineOrder = (timeline: readonly TurnTimelineItem[]): string[] =>
: `${item.kind}:${item.text}`,
);
-test('keeps post-steering work below the steering row even when it continues the same step', () => {
+test('keeps a step whole above a steering row that its later output arrives after', () => {
let live: LiveTurnProjection | undefined = applyLiveTurnEvent(armLiveTurn('turn-1'), {
type: 'thinking_delta', id: 'e1', turnId: 'turn-1', messageId: 'm1', ts: 1, text: 'pre-steer reasoning',
});
@@ -147,34 +146,26 @@ test('keeps post-steering work below the steering row even when it continues the
live = applyLiveTurnEvent(live, {
type: 'steering_message', id: 'steer-event', turnId: 'turn-1', messageId: 'steer-1', ts: 3, content: { text: 'steer' },
});
- // A late result for a row that already exists updates that row in place; it
- // does not claim the steering because its position was fixed at tool_start.
live = applyLiveTurnEvent(live, {
- type: 'tool_result', id: 'e3', turnId: 'turn-1', toolUseId: 'tool-1', isError: false, ts: 4,
- content: { kind: 'text', text: 'done' },
+ type: 'thinking_delta', id: 'e4', turnId: 'turn-1', messageId: 'm1', ts: 5, text: ' continues',
});
- // While the steering awaits its boundary it must already render as one.
- assert.deepEqual(timelineOrder(overlayLiveTurn([], live, 'en')[0]!.timeline), [
- 'thinking:pre-steer reasoning', 'tools:tool-1', 'user:steer',
- ]);
live = applyLiveTurnEvent(live, {
- type: 'thinking_delta', id: 'e4', turnId: 'turn-1', messageId: 'm1', ts: 5, text: 'post-steer reasoning',
+ type: 'tool_start', id: 'e5', turnId: 'turn-1', stepId: 'm1', toolUseId: 'tool-2', toolName: 'Bash', args: {}, ts: 6,
});
live = applyLiveTurnEvent(live, {
- type: 'tool_start', id: 'e5', turnId: 'turn-1', stepId: 'm1', toolUseId: 'tool-2', toolName: 'Bash', args: {}, ts: 6,
+ type: 'text_delta', id: 'e6', turnId: 'turn-1', messageId: 'm1', ts: 7, text: 'answer',
});
live = applyLiveTurnEvent(live, {
- type: 'text_delta', id: 'e6', turnId: 'turn-1', messageId: 'm1', ts: 7, text: 'answer continues',
+ type: 'text_delta', id: 'e7', turnId: 'turn-1', messageId: 'm2', ts: 8, text: 'reply to steer',
});
const timeline = overlayLiveTurn([], live, 'en')[0]!.timeline;
assert.deepEqual(timelineOrder(timeline), [
- 'thinking:pre-steer reasoning',
- 'tools:tool-1',
+ 'thinking:pre-steer reasoning continues',
+ 'tools:tool-1+tool-2',
+ 'text:answer',
'user:steer',
- 'thinking:post-steer reasoning',
- 'tools:tool-2',
- 'text:answer continues',
+ 'text:reply to steer',
]);
const folded = foldTimeline(timeline).entries.map((entry) =>
entry.kind === 'processing'
@@ -185,9 +176,9 @@ test('keeps post-steering work below the steering row even when it continues the
);
assert.deepEqual(folded, [
'fold:thinking+tools',
+ 'text:answer',
'user:steer',
- 'fold:thinking+tools',
- 'text:answer continues',
+ 'text:reply to steer',
]);
});
@@ -224,28 +215,7 @@ test('keeps pre-steering content in place when its completion lands after the bo
]);
});
-test('splits a completion across the boundary instead of duplicating the sealed portion', () => {
- let live: LiveTurnProjection | undefined = applyLiveTurnEvent(armLiveTurn('turn-1'), {
- type: 'thinking_delta', id: 'e1', turnId: 'turn-1', messageId: 'm1', ts: 1, text: 'pre-',
- });
- live = applyLiveTurnEvent(live, {
- type: 'steering_message', id: 'steer-event', turnId: 'turn-1', messageId: 'steer-1', ts: 2, content: { text: 'steer' },
- });
- live = applyLiveTurnEvent(live, {
- type: 'thinking_delta', id: 'e2', turnId: 'turn-1', messageId: 'm1', ts: 3, text: 'post',
- });
- live = applyLiveTurnEvent(live, {
- type: 'thinking_complete', id: 'e3', turnId: 'turn-1', messageId: 'm1', ts: 4, text: 'pre-post',
- });
-
- assert.deepEqual(timelineOrder(overlayLiveTurn([], live, 'en')[0]!.timeline), [
- 'thinking:pre-',
- 'user:steer',
- 'thinking:post',
- ]);
-});
-
-test('lands a replacing completion whole instead of slicing at the delta offset', () => {
+test('lands a completion after a steering row whole on its step', () => {
let live: LiveTurnProjection | undefined = applyLiveTurnEvent(armLiveTurn('turn-1'), {
type: 'thinking_delta', id: 'e1', turnId: 'turn-1', messageId: 'm1', ts: 1, text: 'AAAA',
});
@@ -256,60 +226,12 @@ test('lands a replacing completion whole instead of slicing at the delta offset'
type: 'thinking_delta', id: 'e2', turnId: 'turn-1', messageId: 'm1', ts: 3, text: 'BBBB',
});
live = applyLiveTurnEvent(live, {
- type: 'thinking_complete', id: 'e3', turnId: 'turn-1', messageId: 'm1', ts: 4, text: 'ABC', replaced: true,
+ type: 'thinking_complete', id: 'e3', turnId: 'turn-1', messageId: 'm1', ts: 4, text: 'ABC',
});
assert.deepEqual(timelineOrder(overlayLiveTurn([], live, 'en')[0]!.timeline), [
- 'thinking:AAAA',
- 'user:steer',
'thinking:ABC',
- ]);
-});
-
-test('applies a reseed across the boundary only to the live slice', () => {
- let live: LiveTurnProjection | undefined = applyLiveTurnEvent(armLiveTurn('turn-1'), {
- type: 'text_delta', id: 'e1', turnId: 'turn-1', messageId: 'm1', ts: 1, startOffset: 0, text: 'pre-',
- });
- live = applyLiveTurnEvent(live, {
- type: 'steering_message', id: 's1', turnId: 'turn-1', messageId: 'steer-1', ts: 2, content: { text: 'steer' },
- });
- live = applyLiveTurnEvent(live, {
- type: 'tool_start', id: 'e2', turnId: 'turn-1', stepId: 'm1', toolUseId: 'tool-1', toolName: 'Read', args: {}, ts: 3,
- });
- live = applyLiveTurnEvent(live, {
- type: 'text_delta', id: 'e3', turnId: 'turn-1', messageId: 'm1', ts: 4, startOffset: 4, text: 'post',
- });
- live = applyLiveTurnEvent(live, {
- type: 'text_delta', id: 'seed', turnId: 'turn-1', messageId: 'm1', ts: 5, startOffset: 0, text: 'pre-post more',
- });
-
- assert.deepEqual(timelineOrder(overlayLiveTurn([], live, 'en')[0]!.timeline), [
- 'text:pre-',
- 'user:steer',
- 'tools:tool-1',
- 'text:post more',
- ]);
-});
-
-test('splits a completion by source offset when the sealed slice shows redacted text', () => {
- const secret = 'sk-abcdef0123456789abcdef0123456789';
- let live: LiveTurnProjection | undefined = applyLiveTurnEvent(armLiveTurn('turn-1'), {
- type: 'thinking_delta', id: 'e1', turnId: 'turn-1', messageId: 'm1', ts: 1, text: `use ${secret}\n`,
- });
- live = applyLiveTurnEvent(live, {
- type: 'steering_message', id: 's1', turnId: 'turn-1', messageId: 'steer-1', ts: 2, content: { text: 'steer' },
- });
- live = applyLiveTurnEvent(live, {
- type: 'thinking_delta', id: 'e2', turnId: 'turn-1', messageId: 'm1', ts: 3, text: 'post',
- });
- live = applyLiveTurnEvent(live, {
- type: 'thinking_complete', id: 'e3', turnId: 'turn-1', messageId: 'm1', ts: 4, text: `use ${secret}\npost`,
- });
-
- assert.deepEqual(timelineOrder(overlayLiveTurn([], live, 'en')[0]!.timeline), [
- `thinking:${redactSecrets(`use ${secret}\n`)}`,
'user:steer',
- 'thinking:post',
]);
});
diff --git a/packages/ui/src/__tests__/live-turn-projection.test.ts b/packages/ui/src/__tests__/live-turn-projection.test.ts
index d63bc97f68..6e265d8c53 100644
--- a/packages/ui/src/__tests__/live-turn-projection.test.ts
+++ b/packages/ui/src/__tests__/live-turn-projection.test.ts
@@ -743,6 +743,19 @@ describe('reconcileTerminalLiveTurn', () => {
}]), toolOnly);
});
+ it('hands a running live steering row to the transcript once its user row is durable', () => {
+ const live = applyLiveTurnEvent(undefined, {
+ type: 'steering_message', id: 'steer-event', messageId: 'steer-1',
+ turnId: 'turn-1', ts: 2, content: { text: 'change direction' },
+ })!;
+
+ assert.equal(reconcileTerminalLiveTurn(live, []), live);
+ assert.deepEqual(reconcileTerminalLiveTurn(live, [{
+ type: 'user', id: 'steer-1', turnId: 'turn-1', ts: 2,
+ text: 'change direction', steeringEventId: 'steer-event',
+ }])?.steps, []);
+ });
+
it('keeps steering-only aborts visible for transcript handoff', () => {
const message = { id: 'steer-1', content: { text: 'change direction' }, ts: 2 };
const withSteering = applyLiveTurnEvent(undefined, {
diff --git a/packages/ui/src/live-turn-projection.ts b/packages/ui/src/live-turn-projection.ts
index 2dfdf76c80..8e5783d4fd 100644
--- a/packages/ui/src/live-turn-projection.ts
+++ b/packages/ui/src/live-turn-projection.ts
@@ -64,13 +64,13 @@ export interface LiveThinkingProjection {
export interface LiveTurnStepProjection {
stepId: string;
- /** Event ts of the first word that opened this step or slice. */
+ /** Event ts of the first word that opened this step. */
startedAt?: number;
contentOrder?: LiveTurnStepContentKind[];
/**
- * A steering boundary slice: the steering row is emitted at this position
- * and the slice holds no content. Content events never resolve to it
- * (`steering:`-prefixed stepIds collide with no real stepId).
+ * A durable steering row, placed where the runtime stream delivered it; it
+ * holds no content. Content events never resolve to it (`steering:`-prefixed
+ * stepIds collide with no real stepId).
*/
steering?: LiveSteeringProjection;
thinking?: LiveThinkingProjection;
@@ -221,11 +221,9 @@ function projectLiveTurnEvent(
const prior = current?.turnId === event.turnId
? current
: { turnId: event.turnId, steps: [] };
- if (liveSteeringMessages(prior).some((message) => message.id === event.messageId)) {
+ if (prior.steps.some((step) => step.steering?.id === event.messageId)) {
return confirmed(prior);
}
- // A steering's position is fixed by stream order: it becomes a boundary
- // slice immediately instead of parking until a later event claims it.
return {
...confirmed(prior),
steps: [
@@ -252,15 +250,13 @@ function projectLiveTurnEvent(
if (event.type === 'error' || event.type === 'abort') {
if (!current || current.turnId !== event.turnId) return current;
const steps = terminalizeLiveSteps(current.steps);
- if (steps.length === 0 && liveSteeringMessages(current).length === 0) return undefined;
+ if (steps.length === 0) return undefined;
const { providerRetry: _providerRetry, ...withoutRetry } = confirmed(current);
return { ...withoutRetry, terminal: true, steps };
}
if (event.type === 'complete') {
if (!current || current.turnId !== event.turnId) return current;
- if (current.steps.length === 0 && liveSteeringMessages(current).length === 0) {
- return undefined;
- }
+ if (current.steps.length === 0) return undefined;
const { providerRetry: _providerRetry, ...withoutRetry } = confirmed(current);
return {
...withoutRetry,
@@ -308,37 +304,16 @@ function projectLiveTurnEvent(
: event.type === 'tool_start'
? event.stepId ?? existingToolStep?.stepId ?? `tool:${event.toolUseId}`
: existingToolStep?.stepId ?? `tool:${event.toolUseId}`;
- // A steering boundary freezes the positions before it: same-stepId deltas
- // arriving after one continue the step in a fresh slice, so a stepId can
- // repeat across the array. Completions and events for an existing tool row
- // are updates to a row whose position is already fixed — they resolve to
- // the row's last slice wherever it sits, on either side of a boundary.
- const boundaryIndex = prior.steps.findLastIndex((candidate) => candidate.steering !== undefined);
- const sameStepIndex = prior.steps.findLastIndex((candidate) => candidate.stepId === stepId);
const stepIndex = existingToolStep === undefined
- ? event.type === 'thinking_complete' || event.type === 'text_complete'
- ? sameStepIndex
- : sameStepIndex > boundaryIndex ? sameStepIndex : -1
- : event.type !== 'tool_start'
- || event.stepId === undefined
- || event.stepId === existingToolStep.stepId
- ? prior.steps.indexOf(existingToolStep)
- : sameStepIndex;
+ || (event.type === 'tool_start' && event.stepId !== undefined && event.stepId !== existingToolStep.stepId)
+ ? prior.steps.findIndex((candidate) => candidate.stepId === stepId)
+ : prior.steps.indexOf(existingToolStep);
const step: LiveTurnStepProjection = stepIndex < 0
? { stepId, startedAt: event.ts, tools: [] }
: prior.steps[stepIndex]!;
- // Slices of one step share a source stream; each begins where the earlier
- // slices of its step stopped. A completion carries the full source text, or
- // a replacement that lands whole in the last slice.
- const sliceStart = (kind: 'thinking' | 'text'): number => {
- const before = stepIndex < 0 ? prior.steps.length : stepIndex;
- return prior.steps.findLast((candidate, index) =>
- index < before && candidate.stepId === stepId && candidate[kind] !== undefined,
- )?.[kind]?.sourceEndOffset ?? 0;
- };
let nextStep: LiveTurnStepProjection;
if (event.type === 'thinking_delta') {
- const delta = foldAssistantDelta(step.thinking?.sourceEndOffset ?? sliceStart('thinking'), event);
+ const delta = foldAssistantDelta(step.thinking?.sourceEndOffset ?? 0, event);
if (!delta) return confirmed(prior);
const applied = applyThinkingDelta(step.thinking?.text ?? '', delta.tail, {
locale,
@@ -359,10 +334,7 @@ function projectLiveTurnEvent(
},
};
} else if (event.type === 'thinking_complete') {
- const applied = applyThinkingComplete(
- event.replaced ? event.text : event.text.slice(sliceStart('thinking')),
- { locale },
- );
+ const applied = applyThinkingComplete(event.text, { locale });
nextStep = {
...step,
thinking: {
@@ -373,7 +345,7 @@ function projectLiveTurnEvent(
},
};
} else if (event.type === 'text_delta') {
- const delta = foldAssistantDelta(step.text?.sourceEndOffset ?? sliceStart('text'), event);
+ const delta = foldAssistantDelta(step.text?.sourceEndOffset ?? 0, event);
if (!delta) return confirmed(prior);
const applied = applyAssistantDelta(step.text?.text ?? '', delta.tail, {
locale,
@@ -394,10 +366,7 @@ function projectLiveTurnEvent(
},
};
} else if (event.type === 'text_complete') {
- const applied = applyAssistantComplete(
- event.replaced ? event.text : event.text.slice(sliceStart('text')),
- { locale },
- );
+ const applied = applyAssistantComplete(event.text, { locale });
nextStep = {
...step,
text: {
@@ -552,35 +521,14 @@ function projectLiveTurnEvent(
? prior.steps.map((candidate, index) => index === stepIndex ? nextStep : candidate)
: [...prior.steps, nextStep];
}
- // A completion finalizes the message across every slice it occupies: an
- // earlier slice keeps the portion it rendered — marked complete — so a
- // steering boundary never relocates pre-steering content into the full text
- // a later slice finalizes.
- const finalizedKind = event.type === 'thinking_complete'
- ? 'thinking'
- : event.type === 'text_complete' ? 'text' : undefined;
- if (finalizedKind !== undefined) {
- steps = steps.map((candidate) =>
- candidate !== nextStep
- && candidate.stepId === stepId
- && candidate[finalizedKind] !== undefined
- ? { ...candidate, [finalizedKind]: { ...candidate[finalizedKind]!, complete: true } }
- : candidate);
- }
return { ...priorWithoutRetry, steps };
}
-function liveSteeringMessages(current: LiveTurnProjection): LiveSteeringProjection[] {
- return current.steps.flatMap((step) => (step.steering ? [step.steering] : []));
-}
-
/**
* Streaming display handoff: drop the committed text/thinking slots for `stepId`.
* Tools that still carry live stream evidence (outputChunks) stay — empty
* shell_run durable results do not cover them, and co-located Bash+answer
- * steps must not lose pre-handoff output when the answer settles. `stepId`
- * can match multiple slices once a steering boundary split the step;
- * steering boundary slices carry their own namespaced id and never match.
+ * steps must not lose pre-handoff output when the answer settles.
*/
export function settleLiveTurnStep(
current: LiveTurnProjection,
@@ -659,18 +607,12 @@ export function reconcileTerminalLiveTurn(
steps: terminalizeLiveSteps(current.steps),
};
}
- if (
- projection.terminal === true
- && liveSteeringMessages(projection).length > 0
- && !transcriptReachedTerminal
- ) return projection;
+ const userIds = new Set(turnMessages.flatMap((message) => message.type === 'user' ? [message.id] : []));
const assistantIds = new Set(turnMessages.flatMap((message) => message.type === 'assistant' ? [message.id] : []));
const toolCallIds = new Set(turnMessages.flatMap((message) => message.type === 'tool_call' ? [message.id] : []));
const toolResultIds = new Set(turnMessages.flatMap((message) => message.type === 'tool_result' ? [message.toolUseId] : []));
- let steps = projection.steps.filter((step) => {
- // Steering boundary slices hold no durable-comparable content; the
- // overlay dedupes against the persisted user row by id.
- if (step.steering !== undefined) return true;
+ const steps = projection.steps.filter((step) => {
+ if (step.steering !== undefined) return !transcriptReachedTerminal && !userIds.has(step.steering.id);
if (step.text?.text.length) return true;
if (step.thinking && !assistantIds.has(step.stepId)) return true;
const toolsCovered = step.tools.every((tool) => {
@@ -685,15 +627,6 @@ export function reconcileTerminalLiveTurn(
});
return !toolsCovered;
});
- // Once persisted turn_state records the terminal handoff, the transcript is
- // authoritative for accepted steering; retaining the live copy would leave
- // a duplicate or a nacked ghost instruction on screen.
- const steeringSettled = projection.terminal === true
- && transcriptReachedTerminal
- && liveSteeringMessages(projection).length > 0;
- if (steeringSettled) {
- steps = steps.filter((step) => step.steering === undefined);
- }
if (
steps.length === 0
&& projection.terminal
@@ -703,6 +636,6 @@ export function reconcileTerminalLiveTurn(
|| steps.length !== projection.steps.length
)
) return undefined;
- if (steps.length === projection.steps.length && !steeringSettled) return projection;
+ if (steps.length === projection.steps.length) return projection;
return { ...projection, steps };
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org