{"record":{"id":"b6b6a16b1322158b","repo":"siyuan-note/siyuan","slug":"failed-to-remove-agent-session","errorCode":null,"errorMessage":"Failed to remove agent session","messagePattern":"Failed to remove agent session","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/src/layout/dock/agent/SessionStore.ts","lineNumber":211,"sourceCode":"            }\n            return {revision, session: resp.data?.session};\n        };\n        const save = previous ? previous.then((result) => persist(result.revision)) : persist(baseRevision);\n        sessionSaveQueues.set(snapshot.id, save);\n        try {\n            return await save;\n        } finally {\n            if (sessionSaveQueues.get(snapshot.id) === save) {\n                sessionSaveQueues.delete(snapshot.id);\n            }\n        }\n    },\n\n    async remove(id: string): Promise<void> {\n        await waitForPendingSave(id);\n        const resp = await fetchSyncPost(API + \"/removeSession\", {id}, APP_HEADER) as {code: number; msg?: string};\n        if (!resp || resp.code !== 0) {\n            throw new Error(resp?.msg || \"Failed to remove agent session\");\n        }\n        sessionRevisions.delete(id);\n        sessionRuntimeRevisions.delete(id);\n    },\n\n    async rename(id: string, newTitle: string): Promise<void> {\n        const session = await this.load(id);\n        if (!session) { return; }\n        session.title = newTitle;\n        await this.save(session);\n    },\n\n    async setPermission(id: string, permissionMode: AgentPermissionMode): Promise<AgentPermissionMode> {\n        await waitForPendingSave(id);\n        const resp = await fetchSyncPost(API + \"/setPermission\", {\n            sessionID: id,\n            permissionMode,\n        }, APP_HEADER) as {code: number; msg?: string; data?: {permissionMode?: AgentPermissionMode}};","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/layout/dock/agent/SessionStore.ts#L193-L229","documentation":"SessionStore.remove() calls the kernel endpoint /api/agent/removeSession and requires an API envelope with code===0. If the POST returns nothing or a non-zero code, it throws with the kernel-provided msg, or this fallback message when msg is absent. Local revision maps are only cleaned up after a successful removal.","triggerScenarios":"Calling remove(id) on an agent session whose backend session no longer exists, whose ID is invalid, or when the kernel request fails (network error producing an empty/undefined response, or a kernel-side failure without a msg field).","commonSituations":"Stale session list where the .sy/agent data was deleted on disk or by another client; workspace sync conflicts; kernel restarted with a different session store; temporary network/API failure while a session cleanup runs.","solutions":["Refresh the session list and retry removal on a session that still exists","Inspect the kernel logs for the removeSession failure cause","Check network/API availability (fetchSyncPost returning undefined indicates a failed request)","If the session is already gone server-side, treat removal as done and clear local state"],"exampleFix":"// before\nawait store.remove(id); // throws if server session missing\n// after\ntry { await store.remove(id); } catch (e) { sessionList.refresh(); }","handlingStrategy":"try-catch","validationCode":"const exists = (await fetchSyncPost(API + \"/getSession\", {id}, APP_HEADER))?.code === 0;","typeGuard":"const isApiOk = (r: unknown): r is {code: number; msg?: string} => !!r && typeof (r as any).code === \"number\";","tryCatchPattern":"try { await store.remove(id); } catch (e) { refreshSessionList(); showMessage(e.message); }","preventionTips":["Refresh the session list before removal","Handle empty responses from fetchSyncPost explicitly","Clean up local state in a finally block after successful removal only"],"tags":["api","network","agent-sessions"],"backgroundTag":"api-error-response","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}