From 0740de5fb4d1f07c80f49bd87cd3451c9c996b26 Mon Sep 17 00:00:00 2001 From: Kiet Ho Date: Thu, 8 Oct 2026 22:15:48 -0700 Subject: [PATCH 1/2] fix(chat): resume a background chat tab instead of opening a blank one The chat journal never stored the agent's session id on the session row, so listSessions/getSession always returned harnessSessionId: null. A chat adopted into a tab the user never opened had no agent session id. When its agent stopped, opening the tab recovered with session/new: a blank chat, and the pane then pointed at that empty session, so switching to the CLI failed with "No conversation found". - The journal projects harnessSessionId from session events, and backfills rows written before this from the journal. - AcpChatPane recovers with the host's stored id when the pane has none. --- .../components/AcpChatPane/AcpChatPane.tsx | 5 +++- .../src/commands/commands/commands.ts | 11 ++++--- .../src/journal/journal/journal.ts | 24 ++++++++++++++- packages/chat-runtime/src/projection/index.ts | 1 + .../src/projection/projection.test.ts | 29 +++++++++++++++---- .../chat-runtime/src/projection/projection.ts | 23 ++++++++------- 6 files changed, 69 insertions(+), 24 deletions(-) diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/AcpChatPane.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/AcpChatPane.tsx index d0b1b91ea80..1032f4a03c8 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/AcpChatPane.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/AcpChatPane.tsx @@ -196,7 +196,10 @@ export function AcpChatPane({ ], ); - const agentSessionId = agent?.sessionId; + // A pane adopted in the background never saw the stream report the agent + // session, so recovery takes it from the host's row instead. + const agentSessionId = + agent?.sessionId ?? stored?.session?.harnessSessionId ?? undefined; // Resuming when there is a session to resume, and a plain new one when the // pane was opened straight onto the chat and no agent has run yet. useEffect(() => { diff --git a/packages/chat-runtime/src/commands/commands/commands.ts b/packages/chat-runtime/src/commands/commands/commands.ts index 603002e2447..43222693963 100644 --- a/packages/chat-runtime/src/commands/commands/commands.ts +++ b/packages/chat-runtime/src/commands/commands/commands.ts @@ -359,16 +359,15 @@ export function createCommands(options: CommandsOptions): ChatCommands { getSession(input) { const parsed: GetSessionInput = getSessionInputSchema.parse(input); + const seq = options.sessions.get(parsed.sessionId) + ? options.journal.cursor(parsed.sessionId).seq + : null; + // Reading the cursor can backfill the row, so read the row after it. const session = options.sessions.get(parsed.sessionId); return { live: options.live.get(parsed.sessionId) !== null, session, - cursor: session - ? { - epoch: session.epoch, - seq: options.journal.cursor(parsed.sessionId).seq, - } - : null, + cursor: session && seq !== null ? { epoch: session.epoch, seq } : null, }; }, diff --git a/packages/chat-runtime/src/journal/journal/journal.ts b/packages/chat-runtime/src/journal/journal/journal.ts index 4dbff2d4828..7741d5e5327 100644 --- a/packages/chat-runtime/src/journal/journal/journal.ts +++ b/packages/chat-runtime/src/journal/journal/journal.ts @@ -11,6 +11,7 @@ import { chatJournal } from "../../db"; import { readSessionRow, removeSessionRow, + setHarnessSessionId, writeSessionProjection, } from "../../projection"; import { readSince } from "../../replay"; @@ -45,11 +46,13 @@ type SessionCache = { queuedItemIds: Set; status: SessionStatus; title: string | null; + harnessSessionId: string | null; }; type NextProjection = { status: SessionStatus; title: string | null; + harnessSessionId: string | null; queuedItemIds: Set; }; @@ -87,6 +90,7 @@ export class ChatJournal { const listingChanged = next.status !== cache.status || next.title !== cache.title || + next.harnessSessionId !== cache.harnessSessionId || next.queuedItemIds.size !== cache.queuedItemIds.size; this.db.transaction(() => { @@ -103,6 +107,7 @@ export class ChatJournal { writeSessionProjection(this.db, sessionId, { status: next.status, title: next.title, + harnessSessionId: next.harnessSessionId, queuedCount: next.queuedItemIds.size, updatedAt: ts, }); @@ -111,6 +116,7 @@ export class ChatJournal { cache.lastSeq = seq; cache.status = next.status; cache.title = next.title; + cache.harnessSessionId = next.harnessSessionId; cache.queuedItemIds = next.queuedItemIds; if (listingChanged) this.notify(sessionId, cache.scopeId, ts); @@ -168,6 +174,8 @@ export class ChatJournal { return { status: event.session.status, title: event.session.title ?? cache.title, + harnessSessionId: + event.session.harnessSessionId ?? cache.harnessSessionId, queuedItemIds: cache.queuedItemIds, }; } @@ -179,12 +187,18 @@ export class ChatJournal { } else { queuedItemIds.delete(event.item.id); } - return { status: cache.status, title: cache.title, queuedItemIds }; + return { + status: cache.status, + title: cache.title, + harnessSessionId: cache.harnessSessionId, + queuedItemIds, + }; } return { status: cache.status, title: cache.title, + harnessSessionId: cache.harnessSessionId, queuedItemIds: cache.queuedItemIds, }; } @@ -208,12 +222,14 @@ export class ChatJournal { let lastSeq = 0; let status = row.status; let title = row.title; + let harnessSessionId = row.harnessSessionId; for (const envelope of replay.envelopes) { lastSeq = envelope.cursor.seq; const event = envelope.event; if (event.type === "session") { status = event.session.status; title = event.session.title ?? title; + harnessSessionId = event.session.harnessSessionId ?? harnessSessionId; continue; } if (event.type !== "item" || event.item.kind !== "user_message") continue; @@ -224,6 +240,11 @@ export class ChatJournal { } } + // Rows written before the projection kept this id have it only in the journal. + if (harnessSessionId !== row.harnessSessionId) { + setHarnessSessionId(this.db, sessionId, harnessSessionId); + } + const cache: SessionCache = { scopeId: row.scopeId, epoch, @@ -231,6 +252,7 @@ export class ChatJournal { queuedItemIds, status, title, + harnessSessionId, }; this.sessions.set(sessionId, cache); return cache; diff --git a/packages/chat-runtime/src/projection/index.ts b/packages/chat-runtime/src/projection/index.ts index 554b9e4892a..4487df4dc98 100644 --- a/packages/chat-runtime/src/projection/index.ts +++ b/packages/chat-runtime/src/projection/index.ts @@ -5,6 +5,7 @@ export { readSessionRow, removeSessionRow, resetSessionForEpoch, + setHarnessSessionId, setSessionEpoch, writeSessionProjection, } from "./projection"; diff --git a/packages/chat-runtime/src/projection/projection.test.ts b/packages/chat-runtime/src/projection/projection.test.ts index 6277b175706..d1d7d1dc2ee 100644 --- a/packages/chat-runtime/src/projection/projection.test.ts +++ b/packages/chat-runtime/src/projection/projection.test.ts @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, test } from "bun:test"; import type { ChatRuntime } from "../index"; import { sessionState } from "../testing/fixtures"; import { createTestRuntime } from "../testing/testRuntime"; +import { setHarnessSessionId } from "./projection"; describe("ChatSessionStore", () => { let runtime: ChatRuntime; @@ -32,15 +33,33 @@ describe("ChatSessionStore", () => { ).toEqual(["codex"]); }); - test("keeps the harness session id in its own column", () => { - runtime.sessions.setHarnessSessionId("s1", "claude-abc"); - const row = runtime.sessions.get("s1"); - expect(row).toMatchObject({ - sessionId: "s1", + test("keeps the harness session id the agent reports", () => { + runtime.journal.append("s1", { + type: "session", + session: sessionState({ harnessSessionId: "claude-abc" }), + }); + runtime.journal.append("s1", { + type: "session", + session: sessionState({ status: "running" }), + }); + expect(runtime.sessions.get("s1")).toMatchObject({ harnessSessionId: "claude-abc", }); }); + test("backfills a harness session id that only the journal has", () => { + runtime.journal.append("s1", { + type: "session", + session: sessionState({ harnessSessionId: "claude-abc" }), + }); + setHarnessSessionId(runtime.db, "s1", null); + runtime.journal.forget("s1"); + + const { session } = runtime.commands.getSession({ sessionId: "s1" }); + + expect(session?.harnessSessionId).toBe("claude-abc"); + }); + test("reflects journal projection writes", () => { runtime.journal.append("s1", { type: "session", diff --git a/packages/chat-runtime/src/projection/projection.ts b/packages/chat-runtime/src/projection/projection.ts index d7d9c645393..bbc984daeaf 100644 --- a/packages/chat-runtime/src/projection/projection.ts +++ b/packages/chat-runtime/src/projection/projection.ts @@ -8,6 +8,7 @@ export type SessionRowInsert = typeof chatSessionsLocal.$inferInsert; export type SessionProjection = { status: SessionStatus; title: string | null; + harnessSessionId: string | null; queuedCount: number; updatedAt: number; }; @@ -54,6 +55,17 @@ export function resetSessionForEpoch( .run(); } +export function setHarnessSessionId( + db: ChatDb, + sessionId: string, + harnessSessionId: string | null, +): void { + db.update(chatSessionsLocal) + .set({ harnessSessionId }) + .where(eq(chatSessionsLocal.sessionId, sessionId)) + .run(); +} + export function removeSessionRow(db: ChatDb, sessionId: string): void { db.delete(chatSessionsLocal) .where(eq(chatSessionsLocal.sessionId, sessionId)) @@ -94,15 +106,4 @@ export class ChatSessionStore { .orderBy(desc(chatSessionsLocal.updatedAt)) .all(); } - - setHarnessSessionId( - sessionId: string, - harnessSessionId: string | null, - ): void { - this.db - .update(chatSessionsLocal) - .set({ harnessSessionId, updatedAt: Date.now() }) - .where(eq(chatSessionsLocal.sessionId, sessionId)) - .run(); - } } From 60ca0063bfe1ade6afe8088cdd0f17247d9d13f8 Mon Sep 17 00:00:00 2001 From: Kiet Ho Date: Thu, 8 Oct 2026 22:30:07 -0700 Subject: [PATCH 2/2] fix(chat): report the session id from the Claude and Codex adapters Only the ACP adapter reported harnessSessionId. The Claude SDK and Codex app-server adapters accept a resume id but never reported one, so their chats hit the same blank-chat recovery. Claude now reports the session_id its stream carries, and Codex reports its thread id. --- .../claudeAdapter/claudeAdapter.test.ts | 27 +++++++++++++++++++ .../claude/claudeAdapter/claudeAdapter.ts | 13 +++++++++ .../codex/codexAdapter/codexAdapter.test.ts | 1 + .../codex/codexAdapter/codexAdapter.ts | 1 + .../src/projection/projection.test.ts | 8 +++--- 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts index 9c7a845d029..7ce57aa227f 100644 --- a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts +++ b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts @@ -119,6 +119,33 @@ describe("ClaudeAdapter", () => { ).toBe(true); }); + test("reports the session id the stream carries, once", async () => { + const harness = createHarness(); + const reported: string[] = []; + const collect = async () => { + for (let index = 0; index < 20; index += 1) { + const next = await harness.iterator.next(); + if (next.done) return; + if (next.value.kind === "turn") return; + if ( + next.value.kind === "session" && + next.value.session.harnessSessionId + ) + reported.push(next.value.session.harnessSessionId); + } + }; + + harness.adapter.prompt([{ type: "text", text: "first" }]); + harness.emit({ type: "system", subtype: "init", session_id: "session-1" }); + harness.emit({ + ...(messageStart("msg_1") as object), + session_id: "session-1", + }); + await collect(); + + expect(reported).toEqual(["session-1"]); + }); + test("dispose aborts the underlying session", async () => { const harness = createHarness(); harness.adapter.prompt([{ type: "text", text: "first" }]); diff --git a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts index 37cc7ac589e..07782ee66e4 100644 --- a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts +++ b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts @@ -181,6 +181,7 @@ export class ClaudeAdapter implements HarnessAdapter { private readonly abortController = new AbortController(); private translator: ClaudeTranslator | null = null; private session: ClaudeSession | null = null; + private sessionId: string | null = null; private modeId: ClaudePermissionMode = "default"; private pump: Promise | null = null; private disposed = false; @@ -325,6 +326,7 @@ export class ClaudeAdapter implements HarnessAdapter { try { for await (const message of stream) { if (this.disposed) return; + this.reportSessionId(message); for (const event of translator.translate(message)) { this.events.push(event); } @@ -336,6 +338,17 @@ export class ClaudeAdapter implements HarnessAdapter { } } + private reportSessionId(message: unknown): void { + if (typeof message !== "object" || message === null) return; + const sessionId = (message as { session_id?: unknown }).session_id; + if (typeof sessionId !== "string" || sessionId === this.sessionId) return; + this.sessionId = sessionId; + this.events.push({ + kind: "session", + session: { harnessSessionId: sessionId }, + }); + } + private requestApproval( input: Record, toolUseId: string, diff --git a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts index 4569dbe5cfb..4ccab1c5151 100644 --- a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts +++ b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts @@ -122,6 +122,7 @@ describe("codexAdapter fixtures", () => { expect(sessionStates[0]).toEqual({ status: "starting" }); expect(sessionStates[1]).toMatchObject({ status: "idle", + harnessSessionId: "019fd2fb-7896-7791-b199-a568c89d4986", modeId: "full-access", modelId: "gpt-5.5", availableModes: [ diff --git a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts index 9c138440ffe..1291d669ae0 100644 --- a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts +++ b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts @@ -269,6 +269,7 @@ export class CodexAdapter implements HarnessAdapter { this.emitSession({ status: "idle", + harnessSessionId: this.threadId, modeId: this.modeId, availableModes: [...CODEX_MODES], ...(this.modelId ? { modelId: this.modelId } : {}), diff --git a/packages/chat-runtime/src/projection/projection.test.ts b/packages/chat-runtime/src/projection/projection.test.ts index d1d7d1dc2ee..2b668635f67 100644 --- a/packages/chat-runtime/src/projection/projection.test.ts +++ b/packages/chat-runtime/src/projection/projection.test.ts @@ -36,28 +36,28 @@ describe("ChatSessionStore", () => { test("keeps the harness session id the agent reports", () => { runtime.journal.append("s1", { type: "session", - session: sessionState({ harnessSessionId: "claude-abc" }), + session: sessionState({ harnessSessionId: "agent-session-1" }), }); runtime.journal.append("s1", { type: "session", session: sessionState({ status: "running" }), }); expect(runtime.sessions.get("s1")).toMatchObject({ - harnessSessionId: "claude-abc", + harnessSessionId: "agent-session-1", }); }); test("backfills a harness session id that only the journal has", () => { runtime.journal.append("s1", { type: "session", - session: sessionState({ harnessSessionId: "claude-abc" }), + session: sessionState({ harnessSessionId: "agent-session-1" }), }); setHarnessSessionId(runtime.db, "s1", null); runtime.journal.forget("s1"); const { session } = runtime.commands.getSession({ sessionId: "s1" }); - expect(session?.harnessSessionId).toBe("claude-abc"); + expect(session?.harnessSessionId).toBe("agent-session-1"); }); test("reflects journal projection writes", () => {