From b5102e7d7ea844fb0a64f9e6759b8ebe5d8850bb Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 11 Mar 2026 01:14:17 +0700 Subject: [PATCH] fix(tests): iteration 14 --- packages/server/src/server/session.ts | 50 ++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/server/src/server/session.ts b/packages/server/src/server/session.ts index 9fd79539d..a9534588c 100644 --- a/packages/server/src/server/session.ts +++ b/packages/server/src/server/session.ts @@ -1789,7 +1789,7 @@ export class Session { private async handleArchiveAgentRequest(agentId: string, requestId: string): Promise { this.sessionLogger.info({ agentId }, `Archiving agent ${agentId}`) - const { archivedAt } = await this.archiveAgentState(agentId) + const { archivedAt, archivedRecord } = await this.archiveAgentState(agentId) this.emit({ type: 'agent_archived', @@ -1799,6 +1799,8 @@ export class Session { requestId, }, }) + + await this.maybeArchivePaseoWorktreeAfterAgentArchive(archivedRecord, requestId) } private async archiveAgentState(agentId: string): Promise<{ @@ -1872,6 +1874,52 @@ export class Session { await this.unarchiveAgentState(matched.id) } + private async maybeArchivePaseoWorktreeAfterAgentArchive( + archivedRecord: StoredAgentRecord, + requestId: string + ): Promise { + const ownership = await isPaseoOwnedWorktreeCwd(archivedRecord.cwd, { + paseoHome: this.paseoHome, + }) + if (!ownership.allowed || !ownership.repoRoot || !ownership.worktreePath) { + return + } + + const activeAgentIds = await this.listUnarchivedAgentIdsWithinRoot(ownership.worktreePath) + if (activeAgentIds.size > 0) { + return + } + + await this.archivePaseoWorktree({ + targetPath: ownership.worktreePath, + repoRoot: ownership.repoRoot, + requestId, + }) + } + + private async listUnarchivedAgentIdsWithinRoot(rootPath: string): Promise> { + const storedRecords = await this.agentStorage.list() + const archivedIds = new Set( + storedRecords + .filter((record) => this.isPathWithinRoot(rootPath, record.cwd) && Boolean(record.archivedAt)) + .map((record) => record.id) + ) + const activeIds = new Set( + storedRecords + .filter((record) => this.isPathWithinRoot(rootPath, record.cwd) && !record.archivedAt) + .map((record) => record.id) + ) + + for (const agent of this.agentManager.listAgents()) { + if (!this.isPathWithinRoot(rootPath, agent.cwd) || archivedIds.has(agent.id)) { + continue + } + activeIds.add(agent.id) + } + + return activeIds + } + private async handleUpdateAgentRequest( agentId: string, name: string | undefined,