Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Notify callers when a delegated work is interrupted mid-turn
A delegated work whose turn fails with a transport-class error after tool
execution is held fail-closed in needs_verification so side effects are never
duplicated. But nothing ever told the caller: the required wait stayed open,
status reporting said the Bot was still working, and the user had to poke the
team to discover the interruption.

Keep the fail-closed hold exactly as designed and close the observability
gap instead:

- insert exactly one interruption notice (deduped per call) into the caller's
  work inbox when a non-foreground delegated work enters needs_verification
  for a transport interruption, naming the work ref, summarizing the tools
  that already ran, and spelling out the actionable path: cancel_work the
  held work, then delegate_work afresh (the callee session keeps its history,
  so a fresh delegation sees the completed tool results);
- report held delegated works in statusOf/list_bots as interrupted and
  awaiting verification instead of claiming they are waiting for the user or
  still working;
- track per-turn tool names through the turn lifecycle so the notice can
  summarize what executed before the interruption.

Foreground works, normal long turns, retry backoff, and the needs_verification
fail-closed semantics are unchanged: nothing is auto-settled, auto-resumed,
or re-run.

Fixes #1656
  • Loading branch information
Fei2411 committed Dec 5, 2026
commit 12120019a4e3d08d33480a5c34ca9af9f2dc221b
84 changes: 84 additions & 0 deletions ds-bot/src/host/coordination-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,70 @@ export function openCoordinationDb({
return undefined
}

// #1656: when a delegated work is interrupted mid-turn (transport-class failure) after
// tool execution, it is held in `needs_verification` so side effects are never duplicated
// — but without a notice nobody ever learns the work stopped progressing: the caller waits
// forever and reports the Bot as still working. Notify each live caller exactly once
// (dedup key per call) with the work ref, a summary of what ran, and the actionable path
// (`cancel_work` + `delegate_work` afresh). Fail-closed semantics stay untouched: nothing
// is auto-settled, auto-resumed, or re-run here.
const INTERRUPTION_NOTICE_REASONS = new Set(['transient_failure_after_tool_execution', 'transient_failure_exhausted'])
function notifyInterruptionInternal(workRow, { ts, toolNames, toolCalls, failure } = {}) {
if (!workRow || workRow.status !== 'needs_verification') return
if (!INTERRUPTION_NOTICE_REASONS.has(workRow.pause_reason)) return
if (workRow.kind === 'foreground' || workRow.origin_kind === 'group' || workRow.origin_kind === 'group_report') return
const toolList = Array.isArray(toolNames) ? toolNames.filter(Boolean).slice(0, 8) : []
const ranSummary = toolList.length > 0
? `it had already run: ${toolList.join(', ')}`
: (Number.isFinite(toolCalls) && toolCalls > 0 ? `it had already run ${toolCalls} tool${toolCalls === 1 ? '' : 's'}` : 'it may have partially executed tools')
const ref = workRow.work_ref ?? workRow.work_id
const failureLine = failure?.code ? ` (${failure.code}${failure.status ? ` ${failure.status}` : ''})` : ''
for (const call of stmt.openCallsForChild.all(workRow.work_id)) {
const parent = stmt.getWork.get(call.parent_work_id)
if (!parent || TERMINAL_WORK_STATUSES.has(parent.status)) continue
const dedupKey = `call:${call.call_id}:interrupted`
if (stmt.getInboxByDedup.get(parent.work_id, dedupKey)) continue
const renderedText = `[Work #${ref} interrupted${failureLine}] The delegated work is paused awaiting verification: the turn carrying it was interrupted after tool execution, so it is held to avoid duplicating side effects — ${ranSummary}. It will not make further progress or report back on its own. Decide what to do: use cancel_work on #${ref} to close it, then delegate_work again if the task is still needed (the session history keeps its completed tool results, so a fresh delegation can pick up where it stopped).`
const seq = stmt.nextInboxSeq.get(parent.work_id).next_seq
stmt.insertInbox.run(
parent.work_id,
seq,
dedupKey,
'result',
workRow.logical_address,
workRow.bot_id,
call.call_id,
null,
null,
null,
renderedText.slice(0, 400),
toJson({
role: 'reply',
replyTo: workRow.work_id,
...(workRow.work_ref ? { workRef: workRow.work_ref } : {}),
interrupted: true,
held: true,
renderedText,
...(toolList.length > 0 ? { toolNames: toolList } : {}),
...(failure !== undefined ? { error: failure } : {}),
source: { replyTo: workRow.work_id, ...(workRow.work_ref ? { workRef: workRow.work_ref } : {}) },
}),
null,
0,
null,
ts,
)
const latestParent = stmt.getWork.get(parent.work_id)
if (latestParent && (latestParent.kind === 'foreground' || blockingRequiredWaitsFor(latestParent) === 0)) {
wakeWorkIfSchedulable(latestParent, {
priority: latestParent.kind === 'foreground' ? 'foreground' : latestParent.priority,
reason: 'interrupted_child_notice',
ts,
})
}
}
}

// Pausing or holding (`paused` / `needs_verification`) the last live child of a non-group
// parent that already holds unread waking results (e.g. from finished siblings) unblocks
// that parent immediately instead of stranding those results behind a held child.
Expand Down Expand Up @@ -3142,6 +3206,15 @@ export function openCoordinationDb({
const wokenParents = (targetStatus === 'paused' || targetStatus === 'needs_verification')
? unblockParentsOfHeldWorksInternal([workId], ts, 'sibling_paused_unblock')
: []
if (targetStatus === 'needs_verification') {
const detail = pauseDetail && typeof pauseDetail === 'object' ? pauseDetail : {}
notifyInterruptionInternal(stmt.getWork.get(workId), {
ts,
toolNames: detail.toolNames,
toolCalls: detail.toolCalls,
failure: detail.failure,
})
}

return {
yielded: true,
Expand Down Expand Up @@ -3617,6 +3690,17 @@ export function openCoordinationDb({
// results (e.g. from finished siblings) unblocks that parent immediately instead of
// stranding those results behind a paused child awaiting manual action.
const wokenParents = unblockParentsOfHeldWorksInternal(pausedWorkIds, ts, 'sibling_paused_unblock')
if (status === 'needs_verification') {
const detailInfo = detail && typeof detail === 'object' ? detail : {}
for (const targetId of pausedWorkIds) {
notifyInterruptionInternal(stmt.getWork.get(targetId), {
ts,
toolNames: detailInfo.toolNames,
toolCalls: detailInfo.toolCalls,
failure: detailInfo.failure,
})
}
}
return {
paused: true,
pausedWorkIds,
Expand Down
1 change: 1 addition & 0 deletions ds-bot/src/host/dsh-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3571,6 +3571,7 @@ export function install(rt) {
pauseDetail: {
failure,
toolCalls: toolCalls ?? null,
toolNames: Array.isArray(event.toolNames) ? event.toolNames.slice(0, 20) : [],
evidence: toolCalls === undefined ? 'unavailable' : 'turn_tool_calls',
inputCommitted: deliveryCommitted,
deliveryKey: active.deliveryKey,
Expand Down
10 changes: 9 additions & 1 deletion ds-bot/src/host/handoffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,15 @@ export function install(rt) {
for (const work of activeWorks) {
if (work.originKind === 'request_dedup') continue
if (work.status === 'paused' || work.status === 'needs_verification') {
lines.push(`paused work ${work.workRef ?? work.workId} (${work.pauseReason ?? work.status}; waiting for the user to continue or cancel)`)
if (work.status === 'needs_verification' && work.kind !== 'foreground') {
// #1656: a delegated work interrupted mid-turn must never read as "still working"
// or as waiting for the user — it is held for verification and its caller decides.
lines.push(`interrupted work ${work.workRef ?? work.workId} (paused awaiting verification after a mid-turn transport failure; its caller was notified to cancel it or delegate it afresh)`)
} else if (work.status === 'needs_verification') {
lines.push(`held work ${work.workRef ?? work.workId} (${work.pauseReason ?? work.status}; waiting for the user to continue or cancel)`)
} else {
lines.push(`paused work ${work.workRef ?? work.workId} (${work.pauseReason ?? work.status}; waiting for the user to continue or cancel)`)
}
continue
}
const openWaits = db.listWaits({ workId: work.workId, status: 'open', limit: 20 })
Expand Down
8 changes: 8 additions & 0 deletions ds-bot/src/host/turns.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,13 @@ export function install(rt) {
const name = String(ev.data?.name ?? '')
if (name !== PROGRAM_TOOL) {
entry.toolCount = (entry.toolCount ?? 0) + 1
if (name !== '') (entry.toolNames ??= []).push(name)
if (USER_UI_TOOLS.has(name)) entry.uiToolCount = (entry.uiToolCount ?? 0) + 1
}
} else if (ev?.type === 'tool/ptc-dispatch-start') {
const name = String(ev.data?.name ?? '')
entry.toolCount = (entry.toolCount ?? 0) + 1
if (name !== '') (entry.toolNames ??= []).push(name)
if (USER_UI_TOOLS.has(name)) entry.uiToolCount = (entry.uiToolCount ?? 0) + 1
} else if (ev?.type === 'tool/result' || ev?.type === 'tool/ptc-dispatch') {
const isError = ev.data?.isError === true || ev.data?.ok === false || ev.data?.error !== undefined
Expand Down Expand Up @@ -1722,6 +1724,7 @@ export function install(rt) {
break
}
entry.toolCount = (entry.toolCount ?? 0) + 1
if (name !== '') (entry.toolNames ??= []).push(name)
if (USER_UI_TOOLS.has(name)) entry.uiToolCount = (entry.uiToolCount ?? 0) + 1
if (callId !== undefined) {
guardOf(id).pendingArgs.set(callId, { name, sig: callSignatureOf(name, rawArgs), program: false })
Expand All @@ -1737,6 +1740,7 @@ export function install(rt) {
const rawArgs = JSON.stringify(event.data?.arguments ?? {})
startCall(id, entry, subCallId, name, rawArgs)
entry.toolCount = (entry.toolCount ?? 0) + 1
if (name !== '') (entry.toolNames ??= []).push(name)
if (USER_UI_TOOLS.has(name)) entry.uiToolCount = (entry.uiToolCount ?? 0) + 1
if (subCallId !== undefined) {
guardOf(id).pendingArgs.set(subCallId, { name, sig: callSignatureOf(name, rawArgs) })
Expand Down Expand Up @@ -1874,6 +1878,7 @@ export function install(rt) {
entry.step = undefined
entry.info = undefined
entry.toolCount = 0
entry.toolNames = []
entry.localSideEffectTools = 0
entry.workToolCount = 0
entry.uiToolCount = 0
Expand Down Expand Up @@ -2283,6 +2288,7 @@ export function install(rt) {
const localRoom = entry.local === true && speaksInGroup(id, info)
const usage = entry.usage
const toolCount = entry.toolCount ?? 0
const toolNames = entry.toolNames ?? []
const localSideEffectTools = entry.localSideEffectTools ?? 0
const workToolCount = entry.workToolCount ?? 0
const uiToolCount = entry.uiToolCount ?? 0
Expand All @@ -2302,6 +2308,7 @@ export function install(rt) {
entry.localProgress = undefined
entry.step = undefined
entry.toolCount = 0
entry.toolNames = []
entry.localSideEffectTools = 0
entry.workToolCount = 0
entry.uiToolCount = 0
Expand Down Expand Up @@ -2580,6 +2587,7 @@ export function install(rt) {
...(failure !== undefined ? { failure } : {}),
text: replyText(),
toolCount: toolCount + localSideEffectTools,
toolNames,
workToolCount,
uiToolCount,
info,
Expand Down
Loading