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/05 22:08:05 UTC

(maka) 03/06: feat(runtime): make the transcript conversion whole and resumable

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

Astro-Han pushed a commit to branch refactor/4791-single-transcript-authority
in repository https://gitbox.apache.org/repos/asf/maka.git

commit 5837ef9519a5a1b252f8d7a1efb265e6a2142803
Author: AstroHan <le...@outlook.com>
AuthorDate: Sun Sep 6 02:30:34 2026 +0800

    feat(runtime): make the transcript conversion whole and resumable
    
    Every event id is now derived from the run it belongs to and its position
    in that run, so converting the same transcript twice writes the same
    events and the store keeps one copy. That is what lets an interrupted
    conversion resume: a turn is skipped once its invocation has ended, and
    re-derived until then. Before, a turn was skipped as soon as its opening
    existed, which froze a half-converted turn in that state forever.
    
    Maka's own history now converts whole. Only a foreign transcript stays
    conversation-text: another runtime's tool calls belong to its protocol,
    not to the provider this Session talks to next.
    
    Generated-by: Claude Code
---
 .../src/__tests__/runtime-ledger-repair.test.ts    | 84 ++++++++++++++++++++++
 .../runtime/src/__tests__/session-manager.test.ts  |  6 +-
 packages/runtime/src/runtime-ledger-repair.ts      | 48 ++++++++-----
 3 files changed, 121 insertions(+), 17 deletions(-)

diff --git a/packages/runtime/src/__tests__/runtime-ledger-repair.test.ts b/packages/runtime/src/__tests__/runtime-ledger-repair.test.ts
index 3e42ad0777..908e6f4905 100644
--- a/packages/runtime/src/__tests__/runtime-ledger-repair.test.ts
+++ b/packages/runtime/src/__tests__/runtime-ledger-repair.test.ts
@@ -412,6 +412,90 @@ test('an imported turn with no terminal state is repaired to failed', async () =
   }
 });
 
+test("converts Maka's own legacy transcript whole, and resumes an interrupted conversion", async () => {
+  const root = await mkdtemp(join(tmpdir(), 'maka-native-transcript-'));
+  const sessions = createSessionStore(root);
+  const runtimeEvents = createSqliteRuntimeStore(join(root, 'runtime.sqlite'));
+
+  try {
+    const ts = Date.now();
+    const session = await sessions.create({
+      cwd: '/repo',
+      llmConnectionSlug: 'anthropic',
+      model: 'claude-opus-5',
+      permissionMode: 'ask',
+    });
+    await sessions.appendMessages(session.id, [
+      { type: 'user', id: 'n-user', turnId: 'turn-1', ts, text: 'run the tests' },
+      {
+        type: 'tool_call',
+        id: 'n-tool',
+        turnId: 'turn-1',
+        ts: ts + 1,
+        toolName: 'Bash',
+        args: { command: 'npm test' },
+      },
+      {
+        type: 'tool_result',
+        id: 'n-result',
+        turnId: 'turn-1',
+        ts: ts + 2,
+        toolUseId: 'n-tool',
+        isError: false,
+        content: { kind: 'text', text: 'ok' },
+      },
+      {
+        type: 'system_note',
+        id: 'n-note',
+        turnId: 'turn-1',
+        ts: ts + 3,
+        kind: 'step_limit',
+      },
+      {
+        type: 'assistant',
+        id: 'n-assistant',
+        turnId: 'turn-1',
+        ts: ts + 4,
+        text: 'All green.',
+        modelId: 'claude-opus-5',
+      },
+      {
+        type: 'turn_state',
+        id: 'n-state',
+        turnId: 'turn-1',
+        ts: ts + 5,
+        status: 'completed',
+        partialOutputRetained: true,
+      },
+    ]);
+
+    const repair = new RuntimeLedgerRepair({
+      runtimeEventStore: runtimeEvents,
+      readMessages: (sessionId) => sessions.readMessages(sessionId),
+      appendMessage: (sessionId, message) => sessions.appendMessage(sessionId, message),
+      newId: () => 'unused',
+      now: () => 100,
+    });
+
+    await repair.materializeTranscriptLedger(await sessions.readHeader(session.id));
+    // The same transcript converts to the same events, so a second pass — the
+    // retry after an interrupted one — adds nothing.
+    await repair.materializeTranscriptLedger(await sessions.readHeader(session.id));
+
+    const [run] = await runtimeEvents.listSessionInvocations(session.id);
+    assert.ok(run);
+    assert.equal(runtimeInvocationOutcome(run), 'completed');
+    const events = await runtimeEvents.readRuntimeEvents(session.id, run.runId);
+    assert.deepEqual(
+      events.flatMap((event) => (event.content ? [event.content.kind] : [])),
+      ['invocation_opened', 'text', 'function_call', 'function_response', 'system_note', 'text'],
+    );
+  } finally {
+    await runtimeEvents.close?.();
+    await rm(root, { recursive: true, force: true });
+  }
+});
+
 test('a resolved Claude transcript replays as the conversation the user kept', async () => {
   // The whole path, end to end: raw records → lineage resolution → conversion
   // → Ledger materialization → the replay a continuation would be given.
diff --git a/packages/runtime/src/__tests__/session-manager.test.ts b/packages/runtime/src/__tests__/session-manager.test.ts
index b059a7e8af..e950c2bf04 100644
--- a/packages/runtime/src/__tests__/session-manager.test.ts
+++ b/packages/runtime/src/__tests__/session-manager.test.ts
@@ -13198,7 +13198,11 @@ class MemoryAgentRunStore
       this.options.failRuntimeEventAppendAfter = undefined;
       throw new Error('runtime event append failed');
     }
-    assertDoubleRunNotSealed(this.runtimeEvents.get(key(sessionId, runId)) ?? [], event);
+    const existing = this.runtimeEvents.get(key(sessionId, runId)) ?? [];
+    // Same identity, same event: the store writes an id once, so a retry of an
+    // interrupted append lands on what is already there instead of a copy.
+    if (event.partial !== true && existing.some((candidate) => candidate.id === event.id)) return;
+    assertDoubleRunNotSealed(existing, event);
     this.seedRuntimeEvent(sessionId, runId, event);
   }
 
diff --git a/packages/runtime/src/runtime-ledger-repair.ts b/packages/runtime/src/runtime-ledger-repair.ts
index 8ab8949c99..e736ee25f5 100644
--- a/packages/runtime/src/runtime-ledger-repair.ts
+++ b/packages/runtime/src/runtime-ledger-repair.ts
@@ -72,12 +72,13 @@ export class RuntimeLedgerRepair {
   constructor(private readonly deps: RuntimeLedgerRepairDeps) {}
 
   /**
-   * Give an imported transcript a runtime spine: one invocation per turn, opened
-   * by its own opening fact and closed by its own terminal event.
+   * Give a transcript a runtime spine: one invocation per turn, opened by its
+   * own opening fact and closed by its own terminal event.
    *
-   * The transcript is the only evidence there is, so a turn it cannot close is
-   * refused rather than imported half-formed. Re-running is a no-op: a turn
-   * whose invocation already exists is left exactly as it is.
+   * Every event id is derived from the run it belongs to and its position in
+   * that run, so importing the same transcript twice writes the same events and
+   * the store dedupes them. That is what makes an interrupted import resumable:
+   * a turn is skipped once its invocation has ended, and re-derived until then.
    */
   async materializeTranscriptLedger(header: SessionHeader): Promise<void> {
     const sessionId = header.id;
@@ -86,8 +87,10 @@ export class RuntimeLedgerRepair {
       const ledgerMessages = messages.filter(
         (message) => message.type !== 'user' || message.steeringEventId === undefined,
       );
-      const openedTurnIds = new Set(
-        (await this.listInlineInvocations(sessionId)).map((invocation) => invocation.turnId),
+      const endedTurnIds = new Set(
+        (await this.listInlineInvocations(sessionId))
+          .filter((invocation) => invocation.terminalEvent)
+          .map((invocation) => invocation.turnId),
       );
       const messagesByTurn = groupMessagesByTurn(ledgerMessages);
       const turns = deriveTurnRecords(ledgerMessages).filter((turn) =>
@@ -98,25 +101,26 @@ export class RuntimeLedgerRepair {
       const firstOpenedAt = Math.max(0, header.createdAt - turns.length);
 
       for (const [index, turn] of turns.entries()) {
-        if (openedTurnIds.has(turn.turnId)) continue;
+        if (endedTurnIds.has(turn.turnId)) continue;
         const turnMessages = messagesByTurn.get(turn.turnId) ?? [];
         const runId = transcriptRunId(sessionId, turn.turnId);
         const openedAt = firstOpenedAt + index;
         const run = { sessionId, runId, turnId: turn.turnId, invocationId: runId };
         const events = [
-          transcriptOpeningEvent({ header, run, openedAt, newId: this.deps.newId }),
+          transcriptOpeningEvent({ header, run, openedAt }),
           ...backfillRuntimeEventsFromStoredMessages({
             run,
             outcome: transcriptOutcome(turn, turnMessages, openedAt),
             messages: turnMessages,
-            modelHistory: 'conversation_text',
-            newId: this.deps.newId,
+            // Another runtime's tool calls belong to its protocol, not to the
+            // provider this Session will talk to next, so a foreign transcript
+            // converts as the conversation it is. Maka's own history converts
+            // whole: its tool calls are the ones it would replay.
+            modelHistory: header.externalOrigin ? 'conversation_text' : 'full',
+            newId: transcriptEventIds(runId),
             now: this.deps.now,
           }).events,
         ];
-        if (!events.some(isTerminalRuntimeEvent)) {
-          throw new Error(`Imported transcript Run ${runId} has no terminal RuntimeEvent`);
-        }
         for (const event of events) {
           await this.deps.runtimeEventStore.appendRuntimeEvent(sessionId, runId, event);
         }
@@ -178,6 +182,19 @@ function transcriptRunId(sessionId: string, turnId: string): string {
   return `transcript-${digest.slice(0, 48)}`;
 }
 
+/**
+ * Ids for one run's converted events, numbered in the order the converter
+ * emits them. The run id is already derived from the Session and turn, so the
+ * same transcript always produces the same ids and a re-run appends nothing.
+ */
+function transcriptEventIds(runId: string): () => string {
+  let seq = 0;
+  return () => {
+    seq += 1;
+    return `${runId}-e${seq}`;
+  };
+}
+
 /**
  * The opening fact of an imported turn.
  *
@@ -189,7 +206,6 @@ function transcriptOpeningEvent(input: {
   header: SessionHeader;
   run: { sessionId: string; runId: string; turnId: string; invocationId: string };
   openedAt: number;
-  newId: () => string;
 }): RuntimeEvent {
   const opening: RuntimeEventInvocationOpenedContent = {
     kind: 'invocation_opened',
@@ -212,7 +228,7 @@ function transcriptOpeningEvent(input: {
     source: { kind: 'fresh' },
   };
   return buildInvocationOpenedEvent({
-    id: input.newId(),
+    id: `${input.run.runId}-opened`,
     run: input.run,
     openedAt: input.openedAt,
     opening,


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