Repository navigation
fix(opencode): goals wait for a delegated task's wake instead of re-prompting #37713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e3e357b
8ebb035
2a7c56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -339,6 +339,8 @@ interface ThreadState { | |
| goalRevision: number; | ||
| goalFresh: boolean; | ||
| goalRefreshing: boolean; | ||
| /** The adapter paused the goal for a T3 wake; the next prompted turn resumes it. */ | ||
| goalPausedForWake: boolean; | ||
| readonly providerTurns: Map<string, OrchestrationV2ProviderTurn>; | ||
| active: ActiveTurn | undefined; | ||
| model: ModelRef | undefined; | ||
|
|
@@ -901,6 +903,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | |
| goalRevision: 0, | ||
| goalFresh: false, | ||
| goalRefreshing: false, | ||
| goalPausedForWake: false, | ||
| providerTurns: new Map(), | ||
| active: undefined, | ||
| model: undefined, | ||
|
|
@@ -2870,6 +2873,15 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | |
| return; | ||
| case "session.step.ended": | ||
| case "session.step.failed": { | ||
| // OpenCode runs its goal check, and any continuation, right after a step that stops. | ||
| if ( | ||
| event.type === "session.step.ended" && | ||
| event.data.finish !== "tool-calls" && | ||
| state.providerThread.goal?.status === "active" && | ||
| !state.goalPausedForWake | ||
| ) { | ||
| yield* Effect.forkIn(pauseGoalForWake(state, turn), sessionScope); | ||
| } | ||
| const tokens = event.data.tokens; | ||
| if (tokens === undefined) return; | ||
| turn.steps += 1; | ||
|
|
@@ -3150,6 +3162,55 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | |
| yield* finishTurn(state, end); | ||
| }); | ||
|
|
||
| // OpenCode defers goal continuations only for its own subagents, so without a pause the goal | ||
| // re-prompts the parent while a T3 delegated task that will wake it is still running. | ||
| const pauseGoalForWake = Effect.fn("OpenCode2Adapter.pauseGoalForWake")(function* ( | ||
| state: ThreadState, | ||
| turn: ActiveTurn, | ||
| ) { | ||
| const pendingWake = (yield* mcpSessions.read(turn.input.threadId))?.hasPendingWake; | ||
| if (pendingWake === undefined || !(yield* pendingWake)) return; | ||
| // OpenCode's goal check can pause for no progress before this read resolves; the wake still | ||
| // owns that goal, so a paused goal is adopted for resume without another pause. | ||
| const pausing = yield* lock.withPermit( | ||
| Effect.sync(() => { | ||
| const status = state.providerThread.goal?.status; | ||
| if ( | ||
| (state.active !== undefined && state.active !== turn) || | ||
| turn.interrupted || | ||
| state.goalPausedForWake || | ||
| (status !== "active" && status !== "paused") | ||
| ) { | ||
| return false; | ||
| } | ||
| state.goalPausedForWake = true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invalidate pending ownership acquisition after explicit goal commands. If the pending-wake read is delayed, OpenCode can pause and finish the original turn first. A user can then complete Use an invalidation token or cancel the pending acquisition when an explicit goal command clears ownership. The active-turn comparison alone does not protect a completed control turn.
|
||
| return status === "active"; | ||
| }), | ||
| ); | ||
| if (!pausing) return; | ||
| yield* client.session | ||
| .command({ sessionID: Session.ID.make(state.sessionId), name: "goal", text: "pause" }) | ||
| .pipe( | ||
| Effect.timeout(REQUEST_REPLY_TIMEOUT), | ||
| Effect.catch((cause) => | ||
| Effect.logWarning("Could not pause an OpenCode goal awaiting a T3 wake.", { | ||
| cause, | ||
| }).pipe( | ||
| Effect.andThen( | ||
| lock.withPermit( | ||
| Effect.sync(() => { | ||
| // A failure because OpenCode paused it first still leaves the wake to resume it. | ||
| if (state.providerThread.goal?.status === "active") { | ||
| state.goalPausedForWake = false; | ||
| } | ||
| }), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| const endIfSettled = (state: ThreadState, turn: ActiveTurn) => | ||
| turn.heldEnd !== undefined && turn.steers.size === 0 && !goalHoldsTurn(state) | ||
| ? finishTurn(state, turn.heldEnd) | ||
|
|
@@ -4549,6 +4610,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | |
| const bare = turnInput.message.attachments.length === 0; | ||
| const id = turnPromptId(sessionId, turnInput.attemptId); | ||
| const goalCommand = supportsNativeGoals() ? parseGoalCommand(turnInput.message) : null; | ||
| if (goalCommand !== null && goalCommand.type !== "show") state.goalPausedForWake = false; | ||
| if (goalCommand?.type === "show") { | ||
| if (!sending()) return; | ||
| yield* readGoal(state); | ||
|
|
@@ -4590,7 +4652,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | |
| const files = yield* promptFiles(turnInput.message.attachments); | ||
| const messageText = turnInput.resume ? "" : yield* promptText(turnInput); | ||
| if (!sending()) return; | ||
| return yield* client.session | ||
| const prompt = client.session | ||
| .prompt({ | ||
| sessionID, | ||
| id, | ||
|
|
@@ -4601,6 +4663,34 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | |
| : { skills: skills.map((id) => ({ id: Skill.ID.make(id) })) }), | ||
| }) | ||
| .pipe(Effect.asVoid); | ||
| if (turnInput.resume === true || turnInput.restartContinuationOfRunId !== undefined) { | ||
| return yield* prompt; | ||
| } | ||
| const resumeGoal = state.goalPausedForWake && state.providerThread.goal?.status === "paused"; | ||
| state.goalPausedForWake = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve wake ownership when submission or resumption fails. This assignment consumes ownership before either operation succeeds. If the prompt fails with Consume ownership after successful resumption. Preserve retry state on failure without restoring ownership invalidated by Stop or an explicit goal command.
|
||
| if (!resumeGoal) return yield* prompt; | ||
| // Resuming after the prompt keeps the wake result ahead of the goal's next check; the turn | ||
| // stays held until the resumed goal is read back. | ||
| state.goalRefreshing = true; | ||
| yield* Effect.gen(function* () { | ||
| yield* prompt; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium A stopped wake still sends 🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||
| yield* client.session.command({ sessionID, name: "goal", text: "resume" }).pipe( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check Stop again before resuming the goal. If the prompt response is delayed, Before sending
|
||
| Effect.andThen(readGoal(state)), | ||
| Effect.timeout(REQUEST_REPLY_TIMEOUT), | ||
| Effect.catch((cause) => | ||
| Effect.logWarning("Could not resume an OpenCode goal after a T3 wake.", { cause }), | ||
| ), | ||
| ); | ||
| }).pipe( | ||
| Effect.ensuring( | ||
| lock.withPermit( | ||
| Effect.gen(function* () { | ||
| state.goalRefreshing = false; | ||
| if (state.active === turn) yield* endIfSettled(state, turn); | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| const runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime = { | ||
|
|
@@ -5011,6 +5101,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | |
| return; | ||
| } | ||
| turn.interrupted = true; | ||
| state.goalPausedForWake = false; | ||
| const reply = yield* client.session | ||
| .interrupt({ sessionID: Session.ID.make(sessionId) }) | ||
| .pipe( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idle release can unload a parent that awaits a T3 wake, and reload drops its wake ownership.
The idle unload checks only
hasPendingBackgroundWorkForThread, which returnshasBackground(state).hasBackgrounddoes not includegoalPausedForWakeor the pending-wake state. When the parent has no native background work, the session manager unloads it.delegatedChildArchivepins only a completed app-owned child, not a waiting parent. On reload,goalPausedForWakestarts asfalse. The wake turn then sends only the prompt, so the goal stays paused and does not resume.Choose where ownership survives. Either keep the thread loaded while
goalPausedForWakeis set, and makeunloadThreadhonor the same condition. Or restore ownership on reload when the pending wake still exists. Pinning can hold a thread loaded until Stop clears ownership, so the choice affects memory. The inspected code does not establish which option fits the wake lifecycle, so no code change is proposed here.