Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opencode/src/cli/cmd/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export const RunCommand = effectCmd({
"metadata" in part.state && typeof part.state.metadata?.runId === "string"
? part.state.metadata.runId
: undefined
background.launch({ sessionID: taskID!, parentSessionID: part.sessionID, run })
background.launch({ sessionID: taskID!, parentSessionID: part.sessionID, run, launchID: part.id })
}
}
if (part.type === "text") {
Expand Down
9 changes: 8 additions & 1 deletion packages/opencode/src/cli/cmd/run/background-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const make = (rootSessionID: string) => {
// Latest launch attribution per session, for the reachability walk.
const parents = new Map<string, string>()
const resolved = new Set<string>()
// Task part IDs are stable across event replay, even on older servers that
// do not emit run IDs. A real task_id resume has its own task part.
const launchParts = new Set<string>()

const known = (sessionID: string): boolean => {
const seen = new Set<string>()
Expand All @@ -31,8 +34,12 @@ export const make = (rootSessionID: string) => {
.map(([, entry]) => entry.sessionID)

return {
launch(input: { sessionID: string; parentSessionID: string; run?: string }) {
launch(input: { sessionID: string; parentSessionID: string; run?: string; launchID?: string }) {
if (input.sessionID === rootSessionID) return false
if (input.launchID !== undefined) {
if (launchParts.has(input.launchID)) return false
launchParts.add(input.launchID)
}
parents.set(input.sessionID, input.parentSessionID)
const key = input.run ?? input.sessionID
// Explicit execution keys stay idempotent after delivery. Legacy keys
Expand Down
29 changes: 29 additions & 0 deletions packages/opencode/test/cli/run/background-tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,32 @@ test("a completed explicit run launch replay stays settled while a new run remai
tasks.resolve(`${child}@2`)
expect(tasks.settled()).toBe(true)
})

test("legacy launch part replay cannot resurrect a delivered run, but a distinct task part can resume", () => {
const tasks = BackgroundTasks.make(root)
const first = { ...launch(root, child), launchID: "prt_first" }
expect(tasks.launch(first)).toBe(true)
tasks.resolve(child)
expect(tasks.launch(first)).toBe(false)
expect(tasks.settled()).toBe(true)
expect(tasks.launch({ ...launch(root, child), launchID: "prt_resume" })).toBe(true)
expect(tasks.launch(first)).toBe(false)
expect(tasks.pending()).toEqual([child])
tasks.resolve(child)
expect(tasks.settled()).toBe(true)
})

test("legacy launch part dedupes while pending and a delivery before its first observation stays settled", () => {
const tasks = BackgroundTasks.make(root)
const first = { ...launch(root, child), launchID: "prt_first" }
expect(tasks.launch(first)).toBe(true)
expect(tasks.launch(first)).toBe(false)
expect(tasks.pending()).toEqual([child])
tasks.resolve(child)
expect(tasks.launch(first)).toBe(false)
const early = BackgroundTasks.make(root)
early.resolve(child)
expect(early.launch(first)).toBe(false)
expect(early.settled()).toBe(true)
expect(early.launch(first)).toBe(false)
})
Loading