diff --git a/docs/timeline-sync.md b/docs/timeline-sync.md index b9a80edc0a..252191cab1 100644 --- a/docs/timeline-sync.md +++ b/docs/timeline-sync.md @@ -49,8 +49,8 @@ deleting the agent, or a provider removing one of its own children, deletes hist without its provider. Opening a stored agent that has no provider session to resume starts its first session on the same history. - **Delete** removes the agent's history and every child's, retired ones included, before the - agent record. If the history cannot be removed, the request fails and the agent stays listed, so - the user can delete it again. + agent record. If the history cannot be removed, the request fails and the agent stays listed. The + user can keep using it and delete it again. `epoch` is the stored history's identity, minted once when the history is created. diff --git a/packages/server/src/server/agent/agent-storage.ts b/packages/server/src/server/agent/agent-storage.ts index 5bcdde5942..7510e5eff5 100644 --- a/packages/server/src/server/agent/agent-storage.ts +++ b/packages/server/src/server/agent/agent-storage.ts @@ -364,6 +364,11 @@ export class AgentStorage { this.deleting.add(agentId); } + /** Ends a delete that did not happen; the agent's record takes writes again. */ + abandonDelete(agentId: string): void { + this.deleting.delete(agentId); + } + async remove(agentId: string): Promise { await this.load(); this.beginDelete(agentId); diff --git a/packages/server/src/server/owned-subscriptions.e2e.test.ts b/packages/server/src/server/owned-subscriptions.e2e.test.ts index 6289f0a350..a8cecb120b 100644 --- a/packages/server/src/server/owned-subscriptions.e2e.test.ts +++ b/packages/server/src/server/owned-subscriptions.e2e.test.ts @@ -1,6 +1,8 @@ import { MockLoadTestAgentClient } from "./agent/providers/mock-load-test-agent.js"; import { createPaseoApi } from "@getpaseo/client"; import type { AgentManager } from "./agent/agent-manager.js"; +import { AgentStorage } from "./agent/agent-storage.js"; +import { createTestLogger } from "../test-utils/test-logger.js"; import { execFileSync } from "node:child_process"; import { DatabaseSync } from "node:sqlite"; import { mkdir, readFile, writeFile } from "node:fs/promises"; @@ -1424,7 +1426,7 @@ test("archive and delete replies retain their historical names and reach only th } }); -test("a delete that cannot remove the agent's history fails and can be retried", async () => { +test("a delete that cannot remove the agent's history fails and leaves the agent usable", async () => { const daemon = await createTestPaseoDaemon({ mcpEnabled: false }); const admin = new DaemonClient({ url: `ws://127.0.0.1:${daemon.port}/ws` }); // A second connection to the daemon's database makes the history delete fail. @@ -1439,6 +1441,14 @@ test("a delete that cannot remove the agent's history fails and can be retried", const afterFailure = await admin.fetchAgents(); expect(afterFailure.entries.map((entry) => entry.agent.id)).toEqual([agent.id]); + await admin.updateAgent(agent.id, { name: "After the failed delete" }); + // A fresh reader of the agent directory sees only what reached disk. + const stored = await new AgentStorage( + path.join(daemon.paseoHome, "agents"), + createTestLogger(), + ).get(agent.id); + expect(stored?.title).toBe("After the failed delete"); + database.exec("DROP TRIGGER refuse_history_delete"); await admin.deleteAgent(agent.id); expect((await admin.fetchAgents()).entries).toEqual([]); diff --git a/packages/server/src/server/session.ts b/packages/server/src/server/session.ts index 28889f49c1..df28a44b72 100644 --- a/packages/server/src/server/session.ts +++ b/packages/server/src/server/session.ts @@ -380,6 +380,7 @@ function clientUsesLegacyWorkspaceRestore(appVersion: string | null): boolean { type DeleteFencedAgentStorage = AgentStorage & { beginDelete(agentId: string): void; + abandonDelete(agentId: string): void; }; function beginAgentDeleteIfSupported(agentStorage: AgentStorage, agentId: string): void { @@ -388,6 +389,12 @@ function beginAgentDeleteIfSupported(agentStorage: AgentStorage, agentId: string } } +function abandonAgentDeleteIfSupported(agentStorage: AgentStorage, agentId: string): void { + if ("abandonDelete" in agentStorage && typeof agentStorage.abandonDelete === "function") { + (agentStorage as DeleteFencedAgentStorage).abandonDelete(agentId); + } +} + const FETCH_AGENTS_SORT_KEYS = ["status_priority", "created_at", "updated_at", "title"] as const; export function resolveWaitForFinishError(options: { @@ -3880,8 +3887,13 @@ export class Session { await this.agentManager.flush(); // History goes before the record: if it cannot be removed, the request fails and the record - // stays, so the user still sees the agent and can delete it again. - await this.agentManager.deleteAgentState(agentId); + // stays, so the user still sees the agent, can keep using it, and can delete it again. + try { + await this.agentManager.deleteAgentState(agentId); + } catch (error) { + abandonAgentDeleteIfSupported(this.agentStorage, agentId); + throw error; + } await this.agentStorage.remove(agentId); // Directory removal is daemon-wide; agent_deleted below stays a source-scoped RPC reply. if (knownAgent) this.agentManager.notifyAgentRemoved(knownAgent);