Skip to content

Commit 01f8bd9

Browse files
authored
fix(agents): resume interrupted turns without a recognized abort string (#114121)
1 parent 2880c82 commit 01f8bd9

3 files changed

Lines changed: 85 additions & 40 deletions

File tree

‎docs/gateway/restart-recovery.md‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ message-tool-only turn without reconstructable channel authority is failed
117117
closed and receives the one-time resend notice.
118118

119119
Before resuming, the gateway checks that the transcript tail is safe to
120-
continue from. If it is not (for example, the turn ended on a stale pending
121-
approval), the session is not blindly re-run; the agent instead posts a short
122-
notice asking the user to resend the last request. For WebChat, that notice is
123-
written directly to the session history so it remains visible after reconnect.
120+
continue from. An aborted turn is the interruption itself, so it resumes on a
121+
best-effort basis whatever abort detail the provider or worker recorded with it:
122+
partial streamed text stays in the transcript and the continuation picks up from
123+
the message beneath it, while a tool call left dangling is dropped from the next
124+
provider payload and restricted to restart-safe tools unless it is audited
125+
replay-safe. If the tail is genuinely unsafe (for example a provider failure, or
126+
a turn that ended on a stale pending approval), the session is not blindly
127+
re-run; the agent instead posts a short notice asking the user to resend the
128+
last request. For WebChat, that notice is written directly to the session
129+
history so it remains visible after reconnect.
124130

125131
OpenClaw can also reconstruct interrupted read-only [Code Mode](/tools/code-mode)
126132
work. Code Mode marks these runs as restart-safe and rejects side-effecting

‎src/agents/main-session-restart-recovery-resume-policy.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ export function hasReplaySafeCodeModeCheckpointInCurrentTurn(
231231

232232
// Generic fetch/undici abort strings plus the gateway's own restart abort
233233
// reason (run-termination.ts); which one lands depends on whether the provider
234-
// stream throws or surfaces the abort as an error event.
234+
// stream throws or surfaces the abort as an error event. Only "error" tails
235+
// need this allowlist, so a genuine provider failure is never replayed as
236+
// lifecycle noise.
235237
const RESTART_ABORT_ERROR_MESSAGES = new Set([
236238
"Request was aborted",
237239
"This operation was aborted",
@@ -243,7 +245,15 @@ function isRestartAbortAssistantMessage(message: unknown): boolean {
243245
return false;
244246
}
245247
const stopReason = normalizeOptionalString((message as { stopReason?: unknown }).stopReason);
246-
if (stopReason !== "error" && stopReason !== "aborted") {
248+
// Every row that reaches restart recovery was mid-run when the process went
249+
// down, so an "aborted" tail is that interruption whatever string the
250+
// transport persisted with it ("Worker inference aborted.", a provider cancel
251+
// message, or nothing). Matching on the abort strings alone made ordinary
252+
// restarts fall through to the unresumable notice.
253+
if (stopReason === "aborted") {
254+
return true;
255+
}
256+
if (stopReason !== "error") {
247257
return false;
248258
}
249259
const errorMessage = normalizeOptionalString(

‎src/agents/main-session-restart-recovery.test.ts‎

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ function codeModeWaitCallMessage() {
331331
};
332332
}
333333

334+
// A provider failure is the remaining unresumable transcript tail: restart
335+
// aborts now resume, so notice-delivery tests need a shape that still fails.
336+
function unresumableAssistantMessage(text = "provider failed") {
337+
return {
338+
role: "assistant",
339+
content: [{ type: "text", text }],
340+
stopReason: "error",
341+
errorMessage: "Provider finish_reason: content_filter",
342+
};
343+
}
344+
334345
function cleanedLockForPath(lockPath: string): SessionLockInspection {
335346
// Simulates lock cleanup after process restart: stale lock removed, owning
336347
// PID dead, and the transcript path available for recovery.
@@ -3886,33 +3897,67 @@ describe("main-session-restart-recovery", () => {
38863897
stopReason: "error",
38873898
},
38883899
],
3900+
])("does not resume %s at the transcript tail", async (_label, assistantMessage) => {
3901+
const sessionsDir = await makeSessionsDir();
3902+
await writeStore(sessionsDir, mainSessionStore());
3903+
await writeTranscript(sessionsDir, "main-session", [
3904+
{ role: "user", content: "do the thing" },
3905+
assistantMessage,
3906+
]);
3907+
3908+
await expectRecovery({ recovered: 0, failed: 1, skipped: 0 });
3909+
expect(callGateway).not.toHaveBeenCalled();
3910+
});
3911+
3912+
it.each([
38893913
[
3890-
"aborted tool call",
3914+
"no abort string",
38913915
{
38923916
role: "assistant",
3893-
content: [{ type: "toolCall", id: "call-1", name: "write", arguments: {} }],
3917+
content: [{ type: "text", text: "partial answer" }],
38943918
stopReason: "aborted",
38953919
},
3920+
false,
38963921
],
38973922
[
3898-
"aborted assistant output with text",
3923+
"a worker abort string",
38993924
{
39003925
role: "assistant",
3901-
content: [{ type: "text", text: "partial answer" }],
3926+
content: [{ type: "text", text: "" }],
39023927
stopReason: "aborted",
3928+
errorMessage: "Worker inference aborted.",
39033929
},
3930+
false,
39043931
],
3905-
])("does not resume %s at the transcript tail", async (_label, assistantMessage) => {
3906-
const sessionsDir = await makeSessionsDir();
3907-
await writeStore(sessionsDir, mainSessionStore());
3908-
await writeTranscript(sessionsDir, "main-session", [
3909-
{ role: "user", content: "do the thing" },
3910-
assistantMessage,
3911-
]);
3932+
[
3933+
"a dangling side-effecting call",
3934+
{
3935+
role: "assistant",
3936+
content: [{ type: "toolCall", id: "call-1", name: "write", arguments: {} }],
3937+
stopReason: "aborted",
3938+
errorMessage: "Worker inference aborted.",
3939+
},
3940+
true,
3941+
],
3942+
])(
3943+
"resumes an aborted tail persisted with %s",
3944+
async (_label, assistantMessage, forceRestartSafeTools) => {
3945+
const sessionsDir = await makeSessionsDir();
3946+
await writeStore(sessionsDir, mainSessionStore());
3947+
await writeTranscript(sessionsDir, "main-session", [
3948+
{ role: "user", content: "do the thing" },
3949+
assistantMessage,
3950+
]);
39123951

3913-
await expectRecovery({ recovered: 0, failed: 1, skipped: 0 });
3914-
expect(callGateway).not.toHaveBeenCalled();
3915-
});
3952+
await expectRecovery({ recovered: 1, failed: 0, skipped: 0 });
3953+
expect(callGateway).toHaveBeenCalledTimes(1);
3954+
if (forceRestartSafeTools) {
3955+
expect(gatewayParams()).toMatchObject({ forceRestartSafeTools: true });
3956+
} else {
3957+
expect(gatewayParams()).not.toMatchObject({ forceRestartSafeTools: true });
3958+
}
3959+
},
3960+
);
39163961

39173962
it("keeps an unresumable Control UI notice in history despite a stale external route", async () => {
39183963
const sessionsDir = await makeSessionsDir();
@@ -3926,11 +3971,7 @@ describe("main-session-restart-recovery", () => {
39263971
});
39273972
await writeTranscript(sessionsDir, "main-session", [
39283973
{ role: "user", content: "do the thing" },
3929-
{
3930-
role: "assistant",
3931-
content: [{ type: "text", text: "partial answer" }],
3932-
stopReason: "aborted",
3933-
},
3974+
unresumableAssistantMessage(),
39343975
]);
39353976

39363977
await expectRecovery({ recovered: 0, failed: 1, skipped: 0 });
@@ -3985,11 +4026,7 @@ describe("main-session-restart-recovery", () => {
39854026
);
39864027
await writeTranscript(sessionsDir, "main-session", [
39874028
{ role: "user", content: "do another thing" },
3988-
{
3989-
role: "assistant",
3990-
content: [{ type: "text", text: "another partial answer" }],
3991-
stopReason: "aborted",
3992-
},
4029+
unresumableAssistantMessage("another provider failure"),
39934030
]);
39944031

39954032
await expectRecovery({ recovered: 0, failed: 1, skipped: 0 });
@@ -4030,11 +4067,7 @@ describe("main-session-restart-recovery", () => {
40304067
});
40314068
await writeTranscript(sessionsDir, "main-session", [
40324069
{ role: "user", content: "do the thing" },
4033-
{
4034-
role: "assistant",
4035-
content: [{ type: "text", text: "partial answer" }],
4036-
stopReason: "aborted",
4037-
},
4070+
unresumableAssistantMessage(),
40384071
]);
40394072
transcriptMocks.appendAssistantMessageToSessionTranscript.mockResolvedValueOnce({
40404073
ok: false,
@@ -4091,11 +4124,7 @@ describe("main-session-restart-recovery", () => {
40914124
});
40924125
await writeTranscript(sessionsDir, "interrupted-session", [
40934126
{ role: "user", content: "do the thing" },
4094-
{
4095-
role: "assistant",
4096-
content: [{ type: "text", text: "partial answer" }],
4097-
stopReason: "aborted",
4098-
},
4127+
unresumableAssistantMessage(),
40994128
]);
41004129
let entryAtExternalSend: SessionEntry | undefined;
41014130
vi.mocked(callGateway).mockImplementationOnce(async () => {

0 commit comments

Comments
 (0)