From 0063f039a71ef02219339642174d167b601982fd Mon Sep 17 00:00:00 2001 From: DarkSky <25152247+darkskygit@users.noreply.github.com> Date: Fri, 10 Oct 2025 12:43:21 +0800 Subject: [PATCH] feat(server): allow cleanup session for deleted docs (#13720) ## Summary by CodeRabbit * **Bug Fixes** * Resolved occasional errors when removing document links from sessions, ensuring cleanup completes reliably. * Improved reliability during maintenance actions by preventing unnecessary validation failures in system-initiated updates, while preserving existing checks for user-initiated changes. * **Chores** * Internal adjustments to the session update flow to better support maintenance operations without affecting user-facing behavior. --- .../server/src/models/copilot-session.ts | 23 +++++++++++-------- .../server/src/plugins/copilot/session.ts | 9 ++++---- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/backend/server/src/models/copilot-session.ts b/packages/backend/server/src/models/copilot-session.ts index 4c2e8db9d..1d9586768 100644 --- a/packages/backend/server/src/models/copilot-session.ts +++ b/packages/backend/server/src/models/copilot-session.ts @@ -396,7 +396,10 @@ export class CopilotSessionModel extends BaseModel { } @Transactional() - async update(options: UpdateChatSessionOptions): Promise { + async update( + options: UpdateChatSessionOptions, + internalCall = false + ): Promise { const { userId, sessionId, docId, promptName, pinned, title } = options; const session = await this.getExists( sessionId, @@ -415,14 +418,16 @@ export class CopilotSessionModel extends BaseModel { } // not allow to update action session - if (session.prompt.action) { - throw new CopilotSessionInvalidInput( - `Cannot update action: ${session.id}` - ); - } else if (docId && session.parentSessionId) { - throw new CopilotSessionInvalidInput( - `Cannot update docId for forked session: ${session.id}` - ); + if (!internalCall) { + if (session.prompt.action) { + throw new CopilotSessionInvalidInput( + `Cannot update action: ${session.id}` + ); + } else if (docId && session.parentSessionId) { + throw new CopilotSessionInvalidInput( + `Cannot update docId for forked session: ${session.id}` + ); + } } if (promptName) { diff --git a/packages/backend/server/src/plugins/copilot/session.ts b/packages/backend/server/src/plugins/copilot/session.ts index 9c37007fc..a2021516e 100644 --- a/packages/backend/server/src/plugins/copilot/session.ts +++ b/packages/backend/server/src/plugins/copilot/session.ts @@ -636,11 +636,10 @@ export class ChatSessionService { }) .then(s => s.map(s => [s.userId, s.id])); for (const [userId, sessionId] of sessionIds) { - await this.models.copilotSession.update({ - userId, - sessionId, - docId: null, - }); + await this.models.copilotSession.update( + { userId, sessionId, docId: null }, + true + ); } }