An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Release stale agent session locks after background turns finish
Daniel40bc9223bfc9
diff --git a/app/src/layout/dock/agent/AgentChat.error.test.ts b/app/src/layout/dock/agent/AgentChat.error.test.tsindex 4fbe98e999..9fe0add81e 100644--- a/app/src/layout/dock/agent/AgentChat.error.test.ts+++ b/app/src/layout/dock/agent/AgentChat.error.test.ts@@ -41,6 +41,7 @@ const createChat = () => {sessionId: "first",entries: sessions.get("first").entries,sessionErrors: new Map(),+ mirrorLocked: false,pendingRecoverySessionIDs: new Set(),recoveryInFlightSessionIDs: new Set(),recoveryCommitTurnIDs: new Map(),@@ -52,7 +53,7 @@ const createChat = () => {appendChild: (element: typeof elements[number]) => elements.push(element),querySelectorAll: () => elements.filter(element => element.className.includes("agent-chat__msg--error")),},- sessionRuns: {begin: () => ({controller: {}}), markRead: () => undefined},+ sessionRuns: {begin: () => ({controller: {}}), markRead: () => undefined, get: () => undefined},isScrolledToBottom: () => true,});for (const method of ["finishActiveThinking", "clearThinking", "scrollToBottom", "flushThinkingStep",diff --git a/app/src/layout/dock/agent/AgentChat.sessions.test.ts b/app/src/layout/dock/agent/AgentChat.sessions.test.tsnew file mode 100644index 0000000000..177a58983a--- /dev/null+++ b/app/src/layout/dock/agent/AgentChat.sessions.test.ts@@ -0,0 +1,196 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {test} from "node:test";+import {runInNewContext} from "node:vm";+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";+import {AgentSessionRuns} from "./AgentSessionRuns";+import type {AgentSession} from "./SessionStore";++const compiled = transpileModule(readFileSync("src/layout/dock/agent/AgentChat.ts", "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+}).outputText;++const createChat = (load: (id: string) => Promise<AgentSession>) => {+ const exports = {} as {AgentChat: {prototype: object}};+ runInNewContext(compiled, {+ exports,+ require: () => ({Model: class {}, SessionStore: {load}}),+ window: {+ siyuan: {languages: {}},+ setTimeout: (callback: () => void) => { queueMicrotask(callback); return 1; },+ clearTimeout: (): void => undefined,+ },+ document: {hasFocus: () => true},+ console,+ });+ const chat = Object.create(exports.AgentChat.prototype);+ const classList = {add: (): void => undefined, remove: (): void => undefined, toggle: (): void => undefined};+ const attributes = new Set<string>();+ Object.assign(chat, {+ sessionId: "second",+ sessionRuns: new AgentSessionRuns(),+ isStreaming: true,+ mirrorLocked: false,+ pendingRecoverySessionIDs: new Set(),+ recoveryInFlightSessionIDs: new Set(),+ recoveryCommitTurnIDs: new Map(),+ pendingSessionTitles: new Map(),+ scrollBottomBySession: new Map(),+ host: {},+ messagesContainer: {+ classList,+ scrollTop: 0,+ addEventListener: () => undefined,+ removeEventListener: () => undefined,+ querySelectorAll: () => [],+ },+ titleElement: {},+ sendBtn: {+ classList,+ setAttribute: (name: string) => attributes.add(name),+ removeAttribute: (name: string) => attributes.delete(name),+ },+ stopBtn: {classList},+ composerHost: {classList},+ modelOptions: [{id: "model"}],+ entries: [],+ defaultTitle: "Agent",+ buildEntriesFromSession: (session: AgentSession) => session.entries || [],+ hasComposerInput: () => true,+ isScrolledToBottom: () => true,+ });+ for (const method of ["destroyEditingComposer", "flushTokenUpdate", "cancelTokenUpdate",+ "updateHostRunStatus", "prepareSessionRunForDetach", "captureSessionRunView", "renderLoadedSession",+ "rebuildNavMarkers", "scrollToBottom", "removeMirrorPlaceholder", "showMirrorPlaceholder",+ "applyPermissionMode", "applySessionModelIfValid", "updateTokenDisplay", "finishActiveThinking",+ "clearThinking", "observeStickTarget", "flushThinkingStep"]) {+ chat[method] = (): void => undefined;+ }+ return {chat, attributes};+};++const session = (overrides: Partial<AgentSession> = {}): AgentSession => ({+ id: "first",+ title: "First",+ entries: [{id: "snapshot", type: "snapshot", snapshotID: "snapshot-id"}],+ createdAt: 1,+ updatedAt: 2,+ ...overrides,+});++test("a background turn ending during a session switch releases the stale mirror lock after recovery", async () => {+ let loads = 0;+ let commits = 0;+ const {chat, attributes} = createChat(async () => {+ loads++;+ if (loads === 1) {+ await chat.finishSessionRun(firstRun);+ return session({agentRunning: true});+ }+ return session({recoveryTurnID: "first-turn", recoveryState: "finished"});+ });+ const firstRun = chat.sessionRuns.begin("first");+ firstRun.turnID = "first-turn";+ chat.sessionRuns.detach("first");+ const secondRun = chat.sessionRuns.begin("second");+ chat.abortController = secondRun.controller;+ chat.saveSession = async (): Promise<null> => { commits++; return null; };+ let recovery: Promise<void>;+ const recover = chat.recoverInterruptedTurn.bind(chat);+ chat.recoverInterruptedTurn = (...args: unknown[]) => {+ recovery = recover(...args);+ return recovery;+ };++ await chat.performSwitchSession("first");+ await recovery;++ assert.equal(commits, 1);+ assert.equal(chat.pendingRecoverySessionIDs.has("first"), false);+ assert.equal(chat.mirrorLocked, false);+ assert.equal(chat.isCurrentSessionRunning(), false);+ assert.equal(attributes.has("disabled"), false);+ assert.equal(await chat.prepareForNewTurn(), true);+ assert.equal(chat.sessionRuns.get("second"), secondRun);+ assert.equal(secondRun.controller.signal.aborted, false);+});++test("recovery unlocks a completed session whose turn was already committed", async () => {+ const {chat, attributes} = createChat(async () => session());+ chat.sessionId = "first";+ chat.isStreaming = false;+ chat.mirrorLocked = true;+ chat.currentTurnID = "first-turn";+ chat.updateSendButtonState();+ assert.equal(attributes.has("disabled"), true);++ await chat.recoverInterruptedTurn("first", "first-turn");++ assert.equal(chat.mirrorLocked, false);+ assert.equal(chat.currentTurnID, "");+ assert.equal(attributes.has("disabled"), false);+});++test("authoritative reload keeps another instance's active session locked", async () => {+ const {chat, attributes} = createChat(async () => session({agentRunning: true}));+ chat.sessionId = "first";+ chat.isStreaming = false;+ chat.mirrorLocked = true;+ chat.updateSendButtonState();++ await chat.reloadFromDisk(true);++ assert.equal(chat.mirrorLocked, true);+ assert.equal(chat.isCurrentSessionRunning(), true);+ assert.equal(attributes.has("disabled"), true);+});++test("a completed background turn does not change the foreground stream or its controller", async () => {+ const {chat} = createChat(async () => session());+ const firstRun = chat.sessionRuns.begin("first");+ chat.sessionRuns.detach("first");+ const secondRun = chat.sessionRuns.begin("second");+ chat.abortController = secondRun.controller;+ chat.currentThinkingReasoningContent = "second session reasoning";++ await chat.handleSessionRunEvent(firstRun, {type: "snapshot", snapshotID: "snapshot-id"});+ await chat.handleSessionRunEvent(firstRun, {type: "done", turnID: "first-turn"});++ await chat.finishSessionRun(firstRun);++ assert.equal(chat.isStreaming, true);+ assert.equal(chat.abortController, secondRun.controller);+ assert.equal(secondRun.controller.signal.aborted, false);+ assert.equal(chat.sessionRuns.resolveStatus("second"), "running");+ assert.equal(chat.currentThinkingReasoningContent, "second session reasoning");+ assert.deepEqual(firstRun.pendingEvents.map((event: {type: string}) => event.type), ["snapshot", "done"]);+});++test("a local stream uses its own run instead of acquiring a mirror lock", () => {+ const {chat} = createChat(async () => session());+ chat.sessionId = "first";+ const run = chat.sessionRuns.begin("first");+ chat.abortController = run.controller;++ chat.updateMetaFromSession(session({agentRunning: true}));++ assert.equal(chat.mirrorLocked, false);+ assert.equal(chat.isStreaming, true);+ assert.equal(chat.abortController, run.controller);+});++test("a late recovery read cannot unlock or commit a different foreground session", async () => {+ const {chat} = createChat(async () => {+ chat.sessionId = "second";+ chat.mirrorLocked = true;+ return session({recoveryTurnID: "first-turn", recoveryState: "finished"});+ });+ chat.sessionId = "first";+ chat.isStreaming = false;+ chat.saveSession = async () => assert.fail("the recovery must not save another session");++ await chat.recoverInterruptedTurn("first", "first-turn");++ assert.equal(chat.mirrorLocked, true);+ assert.equal(chat.pendingRecoverySessionIDs.has("first"), true);+});diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.tsindex 6499d3a285..e223b0dcbd 100644--- a/app/src/layout/dock/agent/AgentChat.ts+++ b/app/src/layout/dock/agent/AgentChat.ts@@ -1745,6 +1745,12 @@ export class AgentChat extends Model {console.error("recover interrupted agent turn failed:", e);continue;}+ if (this.sessionId !== sessionID || this.isStreaming) {+ return;+ }+ if (session) {+ this.updateSessionRunLock(session);+ }if (!session?.recoveryTurnID) {if (session && !session.agentRunning) {this.pendingRecoverySessionIDs.delete(sessionID);@@ -1799,8 +1805,9 @@ export class AgentChat extends Model {}}- // 从 session 更新标题/时间戳/token 计数/model 等元数据,不动 entries 与 DOM。+ // 从 session 更新运行锁、标题/时间戳/token 计数/model 等元数据,保留消息内容。private updateMetaFromSession(session: AgentSession) {+ this.updateSessionRunLock(session);this.sessionTitle = this.pendingSessionTitle || session.title || this.defaultTitle;this.hasTitled = session.titled !== false;this.sessionCreatedAt = session.createdAt || this.sessionCreatedAt;@@ -1821,6 +1828,23 @@ export class AgentChat extends Model {this.updateTokenDisplay();}+ // 根据权威运行状态更新当前会话的镜像锁,本地运行由 sessionRuns 独立管理。+ private updateSessionRunLock(session: AgentSession) {+ if (session.id !== this.sessionId) {+ return;+ }+ const locked = !!session.agentRunning && !this.sessionRuns.get(session.id);+ if (this.mirrorLocked === locked) {+ return;+ }+ this.mirrorLocked = locked;+ this.updateRegenerateButtons();+ this.updateSendButtonState();+ if (!locked) {+ this.removeMirrorPlaceholder();+ }+ }+// 浅比较两个 entries 数组是否等价(用于判断是否需要重绘)。用 JSON 序列化比较,简单可靠。private entriesEqual(a: SessionEntry[], b: SessionEntry[]): boolean {if (a === b) {