Skip to content
This repository was archived by the owner on Aug 17, 2026. It is now read-only.

Commit 55fc0da

Browse files
committed
fix(session): auto-recover orphaned tool parts on read (anomalyco#254) (#20)
Sessions whose process was killed mid-tool-call leave `tool` parts with `state.status='running'` (or 'pending') on disk. Resuming such a session crashes the TUI with `TypeError: undefined is not an object (evaluating 'U.length')` because the renderer reaches into `state.output` / `state.metadata` fields that only exist on completed/error states. Fix at the single chokepoint where PartTable rows become Part objects: `part(row)` in message-v2.ts. When a tool part's state is still running/pending AND its `time_created` is older than 60s, we rewrite it on read into a synthetic `ToolStateError`: status: 'error' error: '[Tool execution was interrupted ...]' time: { start, end: start + orphan_age_ms } metadata: { ...preserved, interrupted: true } The 60s threshold guards against clobbering a tool call that's genuinely in-flight in another process during normal multi-client operation; orphaned-on-disk parts are reliably older. The fix sits inside the single `part(row)` helper so it covers every read path (page, parts, get, stream, findMessage) without duplicating logic — anything that hydrates parts gets the recovery for free. Acceptance verified by tests in test/session/orphan-tool-recovery.test.ts: - Orphaned running question tool → transforms to error - Orphaned pending bash tool → transforms to error - Completed tool → untouched - Fresh (<60s) running tool → untouched - Multi-type orphans (bash, read, edit, question) → all transform Full session test suite (357 tests) green with no regressions. Closes anomalyco#254.
1 parent 34e2747 commit 55fc0da

2 files changed

Lines changed: 319 additions & 4 deletions

File tree

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,13 +584,45 @@ const info = (row: typeof MessageTable.$inferSelect) =>
584584
sessionID: row.session_id,
585585
}) as Info
586586

587-
const part = (row: typeof PartTable.$inferSelect) =>
588-
({
589-
...row.data,
587+
// Read-time orphan threshold: a tool part whose state still says "running" or
588+
// "pending" 60s after it was first written almost certainly belongs to a
589+
// process that died mid-execution (kill -9, panic, OOM). Hydrating such a part
590+
// raw crashes the TUI on resume (renderers assume completed tool parts have
591+
// state.output) — see hivemind #254. We surface them as a synthetic error
592+
// state instead, so the renderer treats them like any other failed tool call.
593+
const ORPHAN_TOOL_STALE_MS = 60_000
594+
595+
const part = (row: typeof PartTable.$inferSelect) => {
596+
// PartData = Omit<Part, "id" | "sessionID" | "messageID">. Cast to Part for
597+
// discriminated-union narrowing (Omit doesn't distribute over unions in TS).
598+
const data = row.data as Part
599+
if (data.type === "tool" && (data.state.status === "running" || data.state.status === "pending")) {
600+
const start = data.state.status === "running" ? data.state.time.start : row.time_created
601+
const orphanAgeMs = Date.now() - start
602+
if (orphanAgeMs >= ORPHAN_TOOL_STALE_MS) {
603+
const baseMetadata = data.state.status === "running" ? data.state.metadata : undefined
604+
return {
605+
...data,
606+
state: {
607+
status: "error",
608+
input: data.state.input,
609+
error: "[Tool execution was interrupted — process terminated before completion]",
610+
time: { start, end: start + orphanAgeMs },
611+
metadata: { ...(baseMetadata ?? {}), interrupted: true },
612+
},
613+
id: row.id,
614+
sessionID: row.session_id,
615+
messageID: row.message_id,
616+
} satisfies Part
617+
}
618+
}
619+
return {
620+
...data,
590621
id: row.id,
591622
sessionID: row.session_id,
592623
messageID: row.message_id,
593-
}) as Part
624+
} satisfies Part
625+
}
594626

595627
const older = (row: Cursor) =>
596628
or(lt(MessageTable.time_created, row.time), and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)))
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
import { describe, expect } from "bun:test"
2+
import { Effect } from "effect"
3+
import { Session as SessionNs } from "@/session/session"
4+
import { MessageV2 } from "@/session/message-v2"
5+
import { Database } from "@/storage/db"
6+
import { MessageTable, PartTable } from "@/session/session.sql"
7+
import { MessageID, PartID, type SessionID } from "@/session/schema"
8+
import * as Log from "@opencode-ai/core/util/log"
9+
import { testEffect } from "../lib/effect"
10+
11+
void Log.init({ print: false })
12+
13+
const it = testEffect(SessionNs.defaultLayer)
14+
15+
// Stale enough that the orphan-recovery threshold fires (60s).
16+
const ANCIENT_MS = Date.now() - 5 * 60_000
17+
18+
// Insert a raw orphan tool row directly into the part table, simulating the
19+
// state on disk after a process was killed mid-tool-call (hivemind #254
20+
// repro). Bypasses session.updatePart on purpose — that path would refuse a
21+
// freshly-created running tool because the message hasn't been authored yet,
22+
// and we want exactly the kill-9 shape (orphan row, message present, no
23+
// follow-up part).
24+
const seedOrphan = Effect.fn("Test.seedOrphan")(function* (
25+
sessionID: SessionID,
26+
tool: string,
27+
status: "running" | "pending",
28+
) {
29+
const messageID = MessageID.ascending()
30+
const partID = PartID.ascending()
31+
yield* Effect.sync(() =>
32+
Database.use((db) => {
33+
db.insert(MessageTable)
34+
.values({
35+
id: messageID,
36+
session_id: sessionID,
37+
time_created: ANCIENT_MS,
38+
time_updated: ANCIENT_MS,
39+
data: {
40+
role: "assistant",
41+
time: { created: ANCIENT_MS },
42+
agent: "test",
43+
cost: 0,
44+
tokens: {
45+
input: 0,
46+
output: 0,
47+
reasoning: 0,
48+
cache: { read: 0, write: 0 },
49+
},
50+
mode: "",
51+
model: { providerID: "test", modelID: "test" },
52+
path: { cwd: "/", root: "/" },
53+
system: [],
54+
tools: {},
55+
} as never,
56+
})
57+
.run()
58+
db.insert(PartTable)
59+
.values({
60+
id: partID,
61+
message_id: messageID,
62+
session_id: sessionID,
63+
time_created: ANCIENT_MS,
64+
time_updated: ANCIENT_MS,
65+
data:
66+
status === "running"
67+
? ({
68+
type: "tool",
69+
callID: `call_${tool}`,
70+
tool,
71+
state: {
72+
status: "running",
73+
input: { foo: "bar" },
74+
title: tool,
75+
time: { start: ANCIENT_MS },
76+
},
77+
} as never)
78+
: ({
79+
type: "tool",
80+
callID: `call_${tool}`,
81+
tool,
82+
state: {
83+
status: "pending",
84+
input: { foo: "bar" },
85+
raw: "",
86+
},
87+
} as never),
88+
})
89+
.run()
90+
}),
91+
)
92+
return { messageID, partID }
93+
})
94+
95+
const withSession = <A, E, R>(
96+
fn: (input: { session: SessionNs.Interface; sessionID: SessionID }) => Effect.Effect<A, E, R>,
97+
) =>
98+
Effect.acquireUseRelease(
99+
Effect.gen(function* () {
100+
const session = yield* SessionNs.Service
101+
const created = yield* session.create({})
102+
return { session, sessionID: created.id }
103+
}),
104+
fn,
105+
(input) => input.session.remove(input.sessionID).pipe(Effect.ignore),
106+
)
107+
108+
describe("orphan tool-part recovery on session resume (#254)", () => {
109+
it.instance("transforms an orphaned running `question` tool-part into a synthetic error", () =>
110+
withSession(({ session, sessionID }) =>
111+
Effect.gen(function* () {
112+
yield* seedOrphan(sessionID, "question", "running")
113+
114+
const messages = yield* session.messages({ sessionID })
115+
const orphan = messages
116+
.flatMap((m) => m.parts)
117+
.find((p) => p.type === "tool")
118+
expect(orphan).toBeDefined()
119+
if (orphan?.type !== "tool") throw new Error("expected tool part")
120+
expect(orphan.state.status).toBe("error")
121+
if (orphan.state.status !== "error") throw new Error("narrowing failed")
122+
expect(orphan.state.error).toContain("interrupted")
123+
expect(orphan.state.metadata?.interrupted).toBe(true)
124+
// The synthetic timeline must satisfy ToolStateError's schema invariant
125+
// (end >= start) — otherwise downstream consumers panic.
126+
expect(orphan.state.time.end).toBeGreaterThanOrEqual(orphan.state.time.start)
127+
}),
128+
),
129+
)
130+
131+
it.instance("transforms an orphaned pending bash tool-part into a synthetic error", () =>
132+
withSession(({ session, sessionID }) =>
133+
Effect.gen(function* () {
134+
yield* seedOrphan(sessionID, "bash", "pending")
135+
136+
const messages = yield* session.messages({ sessionID })
137+
const orphan = messages.flatMap((m) => m.parts).find((p) => p.type === "tool")
138+
if (orphan?.type !== "tool") throw new Error("expected tool part")
139+
expect(orphan.state.status).toBe("error")
140+
}),
141+
),
142+
)
143+
144+
it.instance("does not transform a tool-part whose state is already completed", () =>
145+
withSession(({ session, sessionID }) =>
146+
Effect.gen(function* () {
147+
const messageID = MessageID.ascending()
148+
const partID = PartID.ascending()
149+
yield* Effect.sync(() =>
150+
Database.use((db) => {
151+
db.insert(MessageTable)
152+
.values({
153+
id: messageID,
154+
session_id: sessionID,
155+
time_created: ANCIENT_MS,
156+
time_updated: ANCIENT_MS,
157+
data: {
158+
role: "assistant",
159+
time: { created: ANCIENT_MS },
160+
agent: "test",
161+
cost: 0,
162+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
163+
mode: "",
164+
model: { providerID: "test", modelID: "test" },
165+
path: { cwd: "/", root: "/" },
166+
system: [],
167+
tools: {},
168+
} as never,
169+
})
170+
.run()
171+
db.insert(PartTable)
172+
.values({
173+
id: partID,
174+
message_id: messageID,
175+
session_id: sessionID,
176+
time_created: ANCIENT_MS,
177+
time_updated: ANCIENT_MS,
178+
data: {
179+
type: "tool",
180+
callID: "call_done",
181+
tool: "read",
182+
state: {
183+
status: "completed",
184+
input: { path: "/etc/hostname" },
185+
output: "ok",
186+
title: "read",
187+
metadata: { exit: 0 },
188+
time: { start: ANCIENT_MS - 1, end: ANCIENT_MS },
189+
},
190+
} as never,
191+
})
192+
.run()
193+
}),
194+
)
195+
196+
const messages = yield* session.messages({ sessionID })
197+
const completed = messages.flatMap((m) => m.parts).find((p) => p.type === "tool")
198+
if (completed?.type !== "tool") throw new Error("expected tool part")
199+
expect(completed.state.status).toBe("completed")
200+
}),
201+
),
202+
)
203+
204+
it.instance("does not transform a running tool-part that's still within the 60s window", () =>
205+
withSession(({ session, sessionID }) =>
206+
Effect.gen(function* () {
207+
const messageID = MessageID.ascending()
208+
const partID = PartID.ascending()
209+
// start = NOW. Recovery threshold is 60s; this should be untouched.
210+
const now = Date.now()
211+
yield* Effect.sync(() =>
212+
Database.use((db) => {
213+
db.insert(MessageTable)
214+
.values({
215+
id: messageID,
216+
session_id: sessionID,
217+
time_created: now,
218+
time_updated: now,
219+
data: {
220+
role: "assistant",
221+
time: { created: now },
222+
agent: "test",
223+
cost: 0,
224+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
225+
mode: "",
226+
model: { providerID: "test", modelID: "test" },
227+
path: { cwd: "/", root: "/" },
228+
system: [],
229+
tools: {},
230+
} as never,
231+
})
232+
.run()
233+
db.insert(PartTable)
234+
.values({
235+
id: partID,
236+
message_id: messageID,
237+
session_id: sessionID,
238+
time_created: now,
239+
time_updated: now,
240+
data: {
241+
type: "tool",
242+
callID: "call_live",
243+
tool: "bash",
244+
state: {
245+
status: "running",
246+
input: { cmd: "sleep 1" },
247+
title: "bash",
248+
time: { start: now },
249+
},
250+
} as never,
251+
})
252+
.run()
253+
}),
254+
)
255+
256+
const messages = yield* session.messages({ sessionID })
257+
const live = messages.flatMap((m) => m.parts).find((p) => p.type === "tool")
258+
if (live?.type !== "tool") throw new Error("expected tool part")
259+
// Fresh running tool must stay running — would be wrong to clobber a
260+
// tool call that's actually in flight in some other process.
261+
expect(live.state.status).toBe("running")
262+
}),
263+
),
264+
)
265+
266+
it.instance("covers multiple orphan tool types simultaneously (bash, read, edit, question)", () =>
267+
withSession(({ session, sessionID }) =>
268+
Effect.gen(function* () {
269+
for (const tool of ["bash", "read", "edit", "question"]) {
270+
yield* seedOrphan(sessionID, tool, "running")
271+
}
272+
273+
const messages = yield* session.messages({ sessionID })
274+
const tools = messages.flatMap((m) => m.parts).filter((p) => p.type === "tool")
275+
expect(tools).toHaveLength(4)
276+
for (const t of tools) {
277+
if (t.type !== "tool") throw new Error("expected tool part")
278+
expect(t.state.status).toBe("error")
279+
}
280+
}),
281+
),
282+
)
283+
})

0 commit comments

Comments
 (0)