{"record":{"id":"9c7470f0c63bd0fc","repo":"siyuan-note/siyuan","slug":"failed-to-update-agent-session-permission","errorCode":null,"errorMessage":"Failed to update agent session permission","messagePattern":"Failed to update agent session permission","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/layout/dock/agent/SessionStore.ts","lineNumber":223,"sourceCode":"        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}};\n        if (!resp || resp.code !== 0) {\n            throw new Error(resp?.msg || \"Failed to update agent session permission\");\n        }\n        return resp.data?.permissionMode || permissionMode;\n    },\n\n    getRevision(id: string): number {\n        return sessionRevisions.get(id) ?? 0;\n    },\n\n    newSessionId,\n};\n","sourceCodeStart":205,"sourceCodeEnd":234,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/app/src/layout/dock/agent/SessionStore.ts#L205-L234","documentation":"Thrown by SessionStore.setPermission() when POST /api/ai/agent/setPermission replies with a falsy body or code !== 0. The endpoint accepts a sessionID plus an AgentPermissionMode ('confirm' | 'allowSession'); a non-zero code typically means the session does not exist, the mode value is invalid, or authorization failed.","triggerScenarios":"Calling setPermission with a session id that no longer exists on the server, passing a permissionMode outside the allowed union, hitting the endpoint while not authenticated, or the kernel being unreachable so fetchSyncPost returns null.","commonSituations":"Permission toggle clicked right after the session was deleted elsewhere; plugin/frontend out of sync with backend enum values after a partial upgrade; read-only / lock mode active; transport drop mid-call.","solutions":["Validate permissionMode is one of 'confirm' or 'allowSession' before the call.","Wrap the call and on failure re-check the session via load(id); if absent, drop the local entry instead of retrying blindly.","Read resp.msg and show it to the user so the actual kernel-side reason is visible.","If the kernel was just restarted, wait for the WebSocket reconnect event before retrying."],"exampleFix":"// before\nawait SessionStore.setPermission(id, mode);\n// after\nconst valid: AgentPermissionMode[] = ['confirm', 'allowSession'];\nif (!valid.includes(mode)) { throw new Error('Invalid permission mode'); }\ntry { await SessionStore.setPermission(id, mode); }\ncatch (e) { showMessage(e.message); }","handlingStrategy":"validation","validationCode":"const valid: AgentPermissionMode[] = ['confirm', 'allowSession'];\nif (!valid.includes(mode)) throw new Error('Invalid permission mode: ' + String(mode));","typeGuard":"function isPermissionMode(v: unknown): v is AgentPermissionMode { return v === 'confirm' || v === 'allowSession'; }","tryCatchPattern":"try { await SessionStore.setPermission(id, mode); }\ncatch (e) { showMessage(e.message || 'Failed to update agent session permission'); }","preventionTips":["Validate permissionMode against the AgentPermissionMode union before calling.","Confirm the session still exists via load(id) before toggling.","Disable the toggle during the in-flight request.","Show resp.msg so the user sees the kernel's reason."],"tags":["network","agent-session","validation","typescript"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}