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/15 18:19:21 UTC

(maka) 03/06: fix(runtime): project any committed event prefix without an active mode

This is an automated email from the ASF dual-hosted git repository.

Astro-Han pushed a commit to branch fix/transcript-per-event-watermark
in repository https://gitbox.apache.org/repos/asf/maka.git

commit 28100968a2e8155ccc7d82d72ea3523d3c9fec33
Author: AstroHan <le...@outlook.com>
AuthorDate: Wed Sep 16 01:36:41 2026 +0800

    fix(runtime): project any committed event prefix without an active mode
    
    The transcript projector had an `active` mode that settled partial model
    events and invented an empty assistant row for thinking that had no text
    yet. Without it, thinking left unclaimed at finish was always a hard
    `unsupported_event` diagnostic, so a transcript page cut inside a running
    Turn either failed or showed rows that changed once the Turn continued.
    
    The projector now has one mode. Unclaimed thinking is reported only when
    the projected events include its invocation's terminal event; before that
    it simply has no row yet. This makes every committed prefix project exactly
    the rows the full ledger later attributes to those same events, which the
    new prefix-stability test checks event by event.
    
    activePresentationRuntimeEvents stays, since RuntimeReadModel still uses
    it. The runtime-host transcript reader drops the removed option.
    
    Generated-by: Claude Code
---
 .../src/server/session-transcript-reader.ts        |  11 +-
 .../src/__tests__/runtime-event-read-model.test.ts | 117 +++++++++++++++++----
 packages/runtime/src/runtime-event-read-model.ts   |  34 +++---
 3 files changed, 114 insertions(+), 48 deletions(-)

diff --git a/packages/runtime-host/src/server/session-transcript-reader.ts b/packages/runtime-host/src/server/session-transcript-reader.ts
index 9fa6b47802..37e4a95738 100644
--- a/packages/runtime-host/src/server/session-transcript-reader.ts
+++ b/packages/runtime-host/src/server/session-transcript-reader.ts
@@ -138,10 +138,7 @@ export function createSessionTranscriptReader(input: {
         if (!logical) return [];
         runIds = logical.runIds;
       }
-      const pending = createTranscriptProjection(
-        runIds.map((runId) => invocations.get(runId)!),
-        true,
-      );
+      const pending = createTranscriptProjection(runIds.map((runId) => invocations.get(runId)!));
       for (const runId of runIds)
         await scanActiveRuntimeEvents(input.stores, sessionId, runId, pending.push);
       const projected = await pending.finish(input.canonicalPermissionOutcomes);
@@ -626,10 +623,7 @@ interface PendingTranscriptTurn extends RuntimeTranscriptInvocationHeader {
 }
 
 /** Keep only presentation state while the storage snapshot visits complete facts. */
-function createTranscriptProjection(
-  invocations: readonly RuntimeInvocationRecord[],
-  active = false,
-) {
+function createTranscriptProjection(invocations: readonly RuntimeInvocationRecord[]) {
   const canonicalPermissionOutcomes = new Map<string, CanonicalPermissionOutcomeRecord>();
   let messageCount = 0;
   let messageBytes = 0;
@@ -637,7 +631,6 @@ function createTranscriptProjection(
   let sourceBytes = 0;
   const projector = createRuntimeEventStoredMessageProjector({
     invocations,
-    active,
     canonicalPermissionOutcomes,
     projectToolResult: projectTranscriptToolResult,
     onMessage: (message) => {
diff --git a/packages/runtime/src/__tests__/runtime-event-read-model.test.ts b/packages/runtime/src/__tests__/runtime-event-read-model.test.ts
index ca9813b87c..e5b1dac4fb 100644
--- a/packages/runtime/src/__tests__/runtime-event-read-model.test.ts
+++ b/packages/runtime/src/__tests__/runtime-event-read-model.test.ts
@@ -28,7 +28,6 @@ import type { SessionHeader, SessionSummary, StoredMessage, TurnRecord } from '@
 import { deriveTurnRecords, decodeCanonicalMessage } from '@maka/core/session';
 import type { CanonicalPermissionOutcomeRecord } from '../interaction-authority.js';
 import {
-  activePresentationRuntimeEvents,
   createRuntimeEventStoredMessageProjector,
   isHardRuntimeEventReadModelDiagnostic,
   isUnclaimedRuntimeEventDiagnostic,
@@ -305,36 +304,112 @@ describe('projectRuntimeEventsToStoredMessages', () => {
     );
   });
 
-  test('active streaming projection settles model partials and inserts thinking-only rows in source order', () => {
+  // A transcript page may cut an invocation at any committed event. Rows that
+  // appear for a prefix must be exactly the rows the whole invocation later
+  // attributes to those same events, or a page would change once the Turn ends.
+  test('every event prefix projects the rows the full ledger attributes to it', () => {
     const events = [
       ev({
-        id: 'evt-thinking-partial',
+        id: 'evt-prefix-user',
         ts: ts + 1,
-        partial: true,
+        role: 'user',
+        author: 'user',
+        content: { kind: 'text', text: 'read the file' },
+      }),
+      ev({
+        id: 'evt-prefix-thinking',
+        ts: ts + 2,
         role: 'model',
         author: 'agent',
-        content: { kind: 'thinking', text: 'still thinking' },
-        refs: { providerEventId: 'step-thinking-only' },
+        content: { kind: 'thinking', text: 'look first', signature: 'sig' },
+        refs: { providerEventId: 'step-1' },
       }),
       ev({
-        id: 'evt-later-user',
-        ts: ts + 2,
-        role: 'user',
-        author: 'user',
-        content: { kind: 'text', text: 'steer' },
+        id: 'evt-prefix-text',
+        ts: ts + 3,
+        role: 'model',
+        author: 'agent',
+        content: { kind: 'text', text: 'Reading it.' },
+        refs: { providerEventId: 'step-1' },
+      }),
+      ev({
+        id: 'evt-prefix-call',
+        ts: ts + 4,
+        role: 'model',
+        author: 'agent',
+        content: { kind: 'function_call', id: 'tool-p', name: 'Read', args: { path: '/a' } },
+        refs: { toolCallId: 'tool-p', providerEventId: 'step-1' },
+      }),
+      ev({
+        id: 'evt-prefix-result',
+        ts: ts + 5,
+        role: 'tool',
+        author: 'tool',
+        content: {
+          kind: 'function_response',
+          id: 'tool-p',
+          name: 'Read',
+          result: { kind: 'text', text: 'contents' },
+        },
+        refs: { toolCallId: 'tool-p' },
+      }),
+      ev({
+        id: 'evt-prefix-usage',
+        ts: ts + 6,
+        actions: { tokenUsage: { input: 10, output: 5 } },
+      }),
+      ev({
+        id: 'evt-prefix-ended',
+        ts: ts + 7,
+        status: 'completed',
+        actions: { endInvocation: true },
       }),
     ];
-    const streamed = createRuntimeEventStoredMessageProjector({
-      invocations: [invocation],
-      active: true,
+    const project = (prefix: readonly RuntimeEvent[]) =>
+      projectRuntimeEventsToStoredMessages(prefix, { invocations: [invocation] });
+    const full = project(events);
+    assert.deepStrictEqual(full.diagnostics, []);
+    assert.deepStrictEqual(
+      full.messages.map((message) => message.type),
+      ['user', 'assistant', 'tool_call', 'tool_result', 'token_usage', 'turn_state'],
+    );
+
+    for (let k = 1; k <= events.length; k += 1) {
+      const seen = new Set(events.slice(0, k).map((event) => event.id));
+      const expected = full.messages.filter((_, index) => seen.has(full.sourceEventIds[index]!));
+      const prefix = project(events.slice(0, k));
+      assert.deepStrictEqual(prefix.messages, expected, `prefix of ${k} events`);
+      assert.deepStrictEqual(prefix.diagnostics, [], `prefix of ${k} events`);
+    }
+    assert.deepStrictEqual(
+      project(events.slice(0, 2)).messages.map((message) => message.type),
+      ['user'],
+    );
+  });
+
+  test('unclaimed thinking is a defect only once its invocation has ended', () => {
+    const thinking = ev({
+      id: 'evt-orphan-thinking',
+      role: 'model',
+      author: 'agent',
+      content: { kind: 'thinking', text: 'no answer followed' },
+      refs: { providerEventId: 'step-orphan' },
+    });
+    const ended = ev({
+      id: 'evt-orphan-ended',
+      status: 'completed',
+      actions: { endInvocation: true },
     });
-    for (const event of events) streamed.push(event);
 
     assert.deepStrictEqual(
-      streamed.finish(),
-      projectRuntimeEventsToStoredMessages(activePresentationRuntimeEvents(events), {
+      projectRuntimeEventsToStoredMessages([thinking], { invocations: [invocation] }).diagnostics,
+      [],
+    );
+    assert.deepStrictEqual(
+      projectRuntimeEventsToStoredMessages([thinking, ended], {
         invocations: [invocation],
-      }),
+      }).diagnostics.map((diagnostic) => [diagnostic.code, diagnostic.eventId]),
+      [['unsupported_event', 'evt-orphan-thinking']],
     );
   });
 
@@ -1743,11 +1818,15 @@ describe('projectRuntimeEventsToStoredMessages', () => {
             result: 'plain string is not ToolResultContent',
           },
         }),
+        ev({ id: 'evt-ended', status: 'completed', actions: { endInvocation: true } }),
       ],
       { invocations: [invocation] },
     );
 
-    assert.deepStrictEqual(out.messages, []);
+    assert.deepStrictEqual(
+      out.messages.map((message) => message.type),
+      ['turn_state'],
+    );
     // The orphaned permission decision carries no content, so its catch-all is
     // the soft code — but the projector that tried to build its row and failed
     // still reports `incomplete_event`, which stays hard. Downgrading the
diff --git a/packages/runtime/src/runtime-event-read-model.ts b/packages/runtime/src/runtime-event-read-model.ts
index 9201dbbb48..95a4127263 100644
--- a/packages/runtime/src/runtime-event-read-model.ts
+++ b/packages/runtime/src/runtime-event-read-model.ts
@@ -176,7 +176,6 @@ export interface ProjectRuntimeEventsToStoredMessagesOptions {
     | readonly RuntimeInvocationRecord[]
     | Readonly<Record<string, RuntimeInvocationRecord>>;
   canonicalPermissionOutcomes?: ReadonlyMap<string, CanonicalPermissionOutcomeRecord>;
-  active?: boolean;
   projectToolResult?: (event: RuntimeEvent, decoded: ToolResultContent) => ToolResultContent;
   onMessage?: (message: StoredMessage, sourceEventId: string) => void;
 }
@@ -342,6 +341,7 @@ export function createRuntimeEventStoredMessageProjector(
     diagnosticPosition: number;
   }> = [];
   const permissionRequestIds = new Set<string>();
+  const endedInvocationIds = new Set<string>();
   let nextSourceOrder = 0;
   let finished: RuntimeEventReadModelProjection | undefined;
   const attributeEmitted = (
@@ -565,6 +565,7 @@ export function createRuntimeEventStoredMessageProjector(
       projected = projectTokenUsage(event, state, messages) || projected;
     }
 
+    if (isTerminalRuntimeEvent(event)) endedInvocationIds.add(event.invocationId);
     if (isTerminalRuntimeEvent(event) && !event.actions?.handoffPause) {
       projected = projectTerminalTurnState(event, state, messages) || projected;
     }
@@ -598,8 +599,7 @@ export function createRuntimeEventStoredMessageProjector(
 
   const push = (event: RuntimeEvent): void => {
     if (finished) throw new Error('RuntimeEvent StoredMessage projector is already finished');
-    const sourceOrder = nextSourceOrder++;
-    projectEvent(options.active ? settledPresentationEvent(event) : event, sourceOrder);
+    projectEvent(event, nextSourceOrder++);
   };
 
   const finish = (): RuntimeEventReadModelProjection => {
@@ -620,13 +620,6 @@ export function createRuntimeEventStoredMessageProjector(
       attributeDiagnostics(acceptance.sourceOrder, acceptance.diagnosticPosition);
     }
 
-    if (options.active) {
-      for (const pendingItems of [...state.thinkingByMessageId.values()]) {
-        const pending = pendingItems.at(-1);
-        if (pending) projectEvent(emptyAssistantText(pending.event), pending.sourceOrder);
-      }
-    }
-
     const orderedDiagnostics = state.diagnostics
       .map((diagnostic, index) => ({ diagnostic, source: diagnosticSources[index]! }))
       .sort(
@@ -641,16 +634,17 @@ export function createRuntimeEventStoredMessageProjector(
       ...orderedDiagnostics.map(({ diagnostic }) => diagnostic),
     );
 
-    if (!options.active) {
-      for (const pendingItems of state.thinkingByMessageId.values()) {
-        for (const pending of pendingItems) {
-          diagnostic(
-            state,
-            pending.event,
-            'unsupported_event',
-            'thinking content has no assistant text row with a matching message id',
-          );
-        }
+    // Before its invocation ends, thinking may still get its text in a later
+    // event, so an unended prefix leaves it without a row rather than a defect.
+    for (const pendingItems of state.thinkingByMessageId.values()) {
+      for (const pending of pendingItems) {
+        if (!endedInvocationIds.has(pending.event.invocationId)) continue;
+        diagnostic(
+          state,
+          pending.event,
+          'unsupported_event',
+          'thinking content has no assistant text row with a matching message id',
+        );
       }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org