From 85bcd0a11e42f5b885a8962d372d80adfa1da29d Mon Sep 17 00:00:00 2001 From: liugddx Date: Fri, 28 Aug 2026 17:14:49 +0800 Subject: [PATCH 1/4] fix(desktop): offer safe resume after completed tool timeout --- .../main/__tests__/interrupted-resume.test.ts | 44 +++++++++++++++++++ .../session-status-presentation.test.ts | 1 + .../src/renderer/interrupted-resume.ts | 16 +++++-- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 apps/desktop/src/main/__tests__/interrupted-resume.test.ts diff --git a/apps/desktop/src/main/__tests__/interrupted-resume.test.ts b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts new file mode 100644 index 0000000000..9ec5f59efe --- /dev/null +++ b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts @@ -0,0 +1,44 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { latestInterruptedResumeTurnId } from '../../renderer/interrupted-resume.js'; + +describe('latest interrupted resume candidate', () => { + it('recognizes a timeout after a completed tool result', () => { + assert.equal( + latestInterruptedResumeTurnId([ + { + turnId: 'turn-1', + status: 'failed', + errorClass: 'timeout', + tools: [{ status: 'completed' }], + }, + ]), + 'turn-1', + ); + }); + + it('does not offer continuation for an incomplete or errored tool', () => { + assert.equal( + latestInterruptedResumeTurnId([ + { + turnId: 'turn-1', + status: 'failed', + errorClass: 'timeout', + tools: [{ status: 'running' }], + }, + ]), + undefined, + ); + assert.equal( + latestInterruptedResumeTurnId([ + { + turnId: 'turn-1', + status: 'failed', + errorClass: 'timeout', + tools: [{ status: 'errored' }], + }, + ]), + undefined, + ); + }); +}); diff --git a/apps/desktop/src/main/__tests__/session-status-presentation.test.ts b/apps/desktop/src/main/__tests__/session-status-presentation.test.ts index 8b193d7d2e..7f2473c70f 100644 --- a/apps/desktop/src/main/__tests__/session-status-presentation.test.ts +++ b/apps/desktop/src/main/__tests__/session-status-presentation.test.ts @@ -94,4 +94,5 @@ describe('failed turn execution state', () => { /郹戆曞答/, ); }); + }); diff --git a/apps/desktop/src/renderer/interrupted-resume.ts b/apps/desktop/src/renderer/interrupted-resume.ts index 5760fc0b60..1aa3fa1fb2 100644 --- a/apps/desktop/src/renderer/interrupted-resume.ts +++ b/apps/desktop/src/renderer/interrupted-resume.ts @@ -21,14 +21,22 @@ export interface InterruptedResumeTurn { turnId: string; status: string; errorClass?: string; + /** Tool activity already has a durable result in the rendered Turn. */ + tools?: readonly { status: string }[]; } export function latestInterruptedResumeTurnId( turns: readonly InterruptedResumeTurn[], ): string | undefined { const latestTurn = turns.at(-1); - return latestTurn?.status === 'failed' - && latestTurn.errorClass?.toLowerCase() === 'app_restarted' - ? latestTurn.turnId - : undefined; + if (latestTurn?.status !== 'failed') return undefined; + const errorClass = latestTurn.errorClass?.toLowerCase(); + if (errorClass === 'app_restarted') return latestTurn.turnId; + if ( + errorClass?.includes('timeout') && + latestTurn.tools?.some((tool) => tool.status === 'completed') + ) { + return latestTurn.turnId; + } + return undefined; } From 63b7606a17136431de00c39738654143abdc80ba Mon Sep 17 00:00:00 2001 From: liugddx Date: Fri, 28 Aug 2026 17:55:00 +0800 Subject: [PATCH 2/4] chore: add ASF header to recovery test --- .../main/__tests__/interrupted-resume.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apps/desktop/src/main/__tests__/interrupted-resume.test.ts b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts index 9ec5f59efe..b1e7f66a81 100644 --- a/apps/desktop/src/main/__tests__/interrupted-resume.test.ts +++ b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { latestInterruptedResumeTurnId } from '../../renderer/interrupted-resume.js'; From 367aaf519b17ae5c9a439e231e19a3549bd8205b Mon Sep 17 00:00:00 2001 From: liugddx Date: Fri, 28 Aug 2026 23:03:20 +0800 Subject: [PATCH 3/4] fix(desktop): gate timeout resume on settled tools --- .../src/main/__tests__/interrupted-resume.test.ts | 11 +++++++++++ apps/desktop/src/renderer/interrupted-resume.ts | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/main/__tests__/interrupted-resume.test.ts b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts index b1e7f66a81..6374ec727b 100644 --- a/apps/desktop/src/main/__tests__/interrupted-resume.test.ts +++ b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts @@ -48,6 +48,17 @@ describe('latest interrupted resume candidate', () => { ]), undefined, ); + assert.equal( + latestInterruptedResumeTurnId([ + { + turnId: 'turn-1', + status: 'failed', + errorClass: 'timeout', + tools: [{ status: 'completed' }, { status: 'interrupted' }], + }, + ]), + undefined, + ); assert.equal( latestInterruptedResumeTurnId([ { diff --git a/apps/desktop/src/renderer/interrupted-resume.ts b/apps/desktop/src/renderer/interrupted-resume.ts index 1aa3fa1fb2..d3421aa6d9 100644 --- a/apps/desktop/src/renderer/interrupted-resume.ts +++ b/apps/desktop/src/renderer/interrupted-resume.ts @@ -34,7 +34,9 @@ export function latestInterruptedResumeTurnId( if (errorClass === 'app_restarted') return latestTurn.turnId; if ( errorClass?.includes('timeout') && - latestTurn.tools?.some((tool) => tool.status === 'completed') + latestTurn.tools !== undefined && + latestTurn.tools.length > 0 && + latestTurn.tools.every((tool) => tool.status === 'completed') ) { return latestTurn.turnId; } From 1eb6ab0067722378afb6c9f8fdd00c24340739da Mon Sep 17 00:00:00 2001 From: liugddx Date: Fri, 28 Aug 2026 23:08:23 +0800 Subject: [PATCH 4/4] test(desktop): cover mixed tool timeout recovery --- .../main/__tests__/interrupted-resume.test.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/apps/desktop/src/main/__tests__/interrupted-resume.test.ts b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts index 6374ec727b..5a066e95ae 100644 --- a/apps/desktop/src/main/__tests__/interrupted-resume.test.ts +++ b/apps/desktop/src/main/__tests__/interrupted-resume.test.ts @@ -19,6 +19,7 @@ import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; +import { materializeTurns } from '@maka/ui'; import { latestInterruptedResumeTurnId } from '../../renderer/interrupted-resume.js'; describe('latest interrupted resume candidate', () => { @@ -71,4 +72,33 @@ describe('latest interrupted resume candidate', () => { undefined, ); }); + + it('rejects the mixed shape produced by materializeTurns', () => { + const [turn] = materializeTurns([ + { type: 'user', id: 'user-1', turnId: 'turn-1', ts: 1, text: 'inspect' }, + { + type: 'turn_state', + id: 'state-1', + turnId: 'turn-1', + ts: 2, + status: 'failed', + errorClass: 'timeout', + partialOutputRetained: false, + }, + { type: 'tool_call', id: 'call-1', turnId: 'turn-1', ts: 3, toolName: 'Read', args: {} }, + { + type: 'tool_result', + id: 'result-1', + turnId: 'turn-1', + ts: 4, + toolUseId: 'call-1', + isError: false, + content: { kind: 'text', text: 'ok' }, + }, + { type: 'tool_call', id: 'call-2', turnId: 'turn-1', ts: 5, toolName: 'Read', args: {} }, + ]); + + assert.deepEqual(turn?.tools.map((tool) => tool.status), ['completed', 'interrupted']); + assert.equal(latestInterruptedResumeTurnId(turn ? [turn] : []), undefined); + }); });