fix(agents): let an agent keep saving its record after a failed delete

The delete RPC fences the record against writes before closing the agent.
Since a failed history delete now leaves the record in place, the fence
stayed up for the rest of the process, so a later rename reported success
and never reached disk. The fence now ends when the delete aborts.
This commit is contained in:
Mohamed Boudra
2026-11-25 16:31:25 +01:00
parent 49715f0eae
commit a3c1ca6dba
4 changed files with 32 additions and 5 deletions
+2 -2
View File
@@ -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.
@@ -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<void> {
await this.load();
this.beginDelete(agentId);
@@ -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([]);
+14 -2
View File
@@ -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);