Skip to content

Commit f7a504f

Browse files
tella-butlerclaude
andcommitted
Frame interrupt-delivered prompts as mid-task steers
Busy-sends abort the running turn (opencode has no mid-turn steer), and the truncated turn left in history primes a short acknowledge-then-stop reply — the main announce-then-stop trigger (twice today in bks-019f54f8). Mark the session in abortTurnAndDrain and have the next drain append a fenced INTERRUPT_STEER_NOTE telling the model the delivery interrupted its own in-progress turn: fold the message in and keep executing. Fenced via wrapContext so the transcript shows only the user's text; TTL'd mark so a parked drain can't mislabel a later prompt. Stopgap until opencode's v2 SessionRunner supports config providers — its /api/session prompt has real delivery:"steer"|"queue" (verified working on our live server for catalog models; our Meridian/bridge providers hit ModelUnavailableError on both 1.17.15 and the 0.0.0-beta-202607112149 snapshot — that support lands with anomalyco/opencode#36196 on the v2 branch, together with CodeMode MCP tool search). Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent c8fc978 commit f7a504f

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎src/server/auto-continue.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ export const AUTO_CONTINUE_PROMPT =
1717
"until the task is done or you are genuinely blocked on input only the human " +
1818
"can give.";
1919

20+
/**
21+
* Fenced context appended to a prompt that was delivered by ABORTING the
22+
* running turn (busy-send interrupt). The engine has no mid-turn steer (see
23+
* "why opencode stops": every busy-send is an abort, and the truncated turn
24+
* left in history primes a short acknowledge-then-stop reply — the main
25+
* announce-then-stop trigger). This note reframes the delivery as a steer so
26+
* the model folds it into the work in progress instead of parking. Remove
27+
* when the engine gains real mid-turn steering (opencode v2 delivery:"steer").
28+
*/
29+
export const INTERRUPT_STEER_NOTE =
30+
"Delivery note: the user sent this while your previous turn was still " +
31+
"executing; that turn was interrupted to deliver it. Treat it as a mid-task " +
32+
"steer — fold it into the work in progress and continue executing in THIS " +
33+
"turn: pick up any interrupted step that is still needed, act on the new " +
34+
"message, and keep working until done or genuinely blocked. Do not end the " +
35+
"turn on a bare acknowledgment or an announcement of what you will do.";
36+
2037
/**
2138
* Does the reply's final sentence read as a next action the model was about
2239
* to take ("Now let me read the exact code…", "I'll rebase and then open the

‎src/server/run-session.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import {
114114
announcesNextAction,
115115
AUTO_CONTINUE_PROMPT,
116116
AUTO_CONTINUE_USER,
117+
INTERRUPT_STEER_NOTE,
117118
} from "./auto-continue";
118119

119120
const g = globalThis as any;
@@ -124,6 +125,23 @@ const g = globalThis as any;
124125
// arrives or a turn does real (tool-calling) work.
125126
const autoContinueNudged: Set<string> = (g.__autoContinueNudged ??= new Set());
126127

128+
// Sessions whose current queue head was delivered by aborting the running
129+
// turn (busy-send interrupt). The next drain consumes the mark and appends
130+
// INTERRUPT_STEER_NOTE so the model treats the delivery as a mid-task steer
131+
// instead of a fresh turn it can acknowledge-and-park on. Timestamped so a
132+
// mark whose drain never happens (user Stop) expires instead of mislabeling
133+
// a much later, unrelated prompt.
134+
const interruptDrainMarks: Map<string, number> = (g.__interruptDrainMarks ??=
135+
new Map());
136+
const INTERRUPT_DRAIN_MARK_TTL_MS = 5 * 60_000;
137+
138+
function consumeInterruptDrainMark(sessionId: string): boolean {
139+
const at = interruptDrainMarks.get(sessionId);
140+
if (at === undefined) return false;
141+
interruptDrainMarks.delete(sessionId);
142+
return Date.now() - at < INTERRUPT_DRAIN_MARK_TTL_MS;
143+
}
144+
127145
/** Append a message to a session's drainable queue and persist + broadcast. */
128146
export function enqueuePrompt(sessionId: string, item: QueueItem): void {
129147
const queue = promptQueues.get(sessionId) || [];
@@ -499,11 +517,18 @@ export async function drainQueue(sessionId: string): Promise<void> {
499517
if (queue.length === 0) promptQueues.delete(sessionId);
500518
persistQueues();
501519
broadcastQueue(sessionId);
502-
const combined = batch
520+
let combined = batch
503521
.map((m) =>
504522
batch.length > 1 && m.user ? `[${m.user}] ${m.content}` : m.content,
505523
)
506524
.join("\n\n");
525+
// Interrupt delivery (busy-send aborted the turn to land this batch):
526+
// append the fenced steer note so the model resumes the interrupted work
527+
// instead of acknowledge-and-parking. Fenced, so the transcript shows
528+
// only the user's text.
529+
if (consumeInterruptDrainMark(sessionId)) {
530+
combined = `${combined}\n\n${wrapContext(INTERRUPT_STEER_NOTE)}`;
531+
}
507532
// Attachments queued alongside the text ride the drained turn: images are
508533
// decoded to ImageInput, files handed through as staged/inline refs.
509534
const combinedImages = parseImageDataUrls(
@@ -609,6 +634,9 @@ export function abortTurnAndDrain(
609634
// fold any unconfirmed steer receipts back in ahead so nothing is dropped.
610635
stoppedSessions.delete(sessionId);
611636
requeueSteerReceipts(sessionId);
637+
// The drained batch is an interrupt delivery: flag it so the drain frames
638+
// it as a mid-task steer (see INTERRUPT_STEER_NOTE).
639+
interruptDrainMarks.set(sessionId, Date.now());
612640
watchExternalRunAndDrain(sessionId);
613641
return true;
614642
}

0 commit comments

Comments
 (0)