From ce09360241c5f87d014100ff6b9bb613d2ea9c7e Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:55:35 +0000 Subject: [PATCH] fix(server): agents archived while not loaded get the archive time as their last update (#9926) * fix(server): move updatedAt when archiving an agent that is only in storage Archiving a loaded agent sets its updatedAt to the archive time (markRecordArchived). Archiving an agent that is only in storage, which is also how a parent's archive reaches its children, kept the old updatedAt. Lists sorted by updated_at, such as History, then left a just-archived agent where its last turn put it, sometimes days back, and clients paging such a list by time never reached it. archiveSnapshotUnlocked now records the archive time as updatedAt too. Co-authored-by: RoundRedDot * fix(server): keep updatedAt from moving back when an archive is restored Archiving a stored-only agent now sets updatedAt to the archive time. When the archive time is earlier than the record's updatedAt, as when a failed send or resume puts an agent's original archive back after unarchiving it, updatedAt stays where it is. Clients drop agent updates that are older than the one they hold, so moving it back left them showing the agent active. Co-authored-by: luke Co-Authored-By: Claude Opus 5.5 * fix(server): keep updatedAt from moving back when a loaded agent's archive is restored The rule that archiving never moves updatedAt backwards now lives in buildArchivedAgentRecord, which both archive paths use. A failed send that resumed the agent before a later loading step failed puts its original archive back through the loaded path, which had the same backwards move. The workspace archive stamp test now uses stamps taken after the agents' activity, as real archive stamps are. Co-authored-by: luke Co-Authored-By: Claude Opus 5.5 * refactor(server): require the archive time when building an archived agent buildArchivedAgentRecord no longer accepts an archive that leaves updatedAt unset, the gap #9920 came through, and its tests now assert the rule: a later archive time moves updatedAt, an earlier one keeps it. Co-authored-by: luke Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: luke Co-authored-by: RoundRedDot Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../src/server/agent/agent-archive.test.ts | 36 +++--- .../server/src/server/agent/agent-archive.ts | 19 ++- .../src/server/agent/agent-manager.test.ts | 116 +++++++++++++++++- .../server/src/server/agent/agent-manager.ts | 8 +- 4 files changed, 153 insertions(+), 26 deletions(-) diff --git a/packages/server/src/server/agent/agent-archive.test.ts b/packages/server/src/server/agent/agent-archive.test.ts index c4dd34b7e2..21be7eef5f 100644 --- a/packages/server/src/server/agent/agent-archive.test.ts +++ b/packages/server/src/server/agent/agent-archive.test.ts @@ -14,18 +14,19 @@ const BASE_RECORD: StoredAgentRecord = { config: null, }; +const ARCHIVE = { + archivedAt: "2025-01-03T00:00:00.000Z", + updatedAt: "2025-01-03T00:00:00.000Z", +}; + test("archives a stored agent without changing terminal statuses", () => { const statuses: Array = ["idle", "error", "closed"]; for (const status of statuses) { - const archived = buildArchivedAgentRecord( - { ...BASE_RECORD, lastStatus: status }, - { archivedAt: "2025-01-03T00:00:00.000Z" }, - ); + const archived = buildArchivedAgentRecord({ ...BASE_RECORD, lastStatus: status }, ARCHIVE); expect(archived.lastStatus).toBe(status); expect(archived.archivedAt).toBe("2025-01-03T00:00:00.000Z"); - expect(archived.updatedAt).toBe(BASE_RECORD.updatedAt); } }); @@ -33,10 +34,7 @@ test("archives busy stored agents as idle", () => { const statuses: Array = ["initializing", "running"]; for (const status of statuses) { - const archived = buildArchivedAgentRecord( - { ...BASE_RECORD, lastStatus: status }, - { archivedAt: "2025-01-03T00:00:00.000Z" }, - ); + const archived = buildArchivedAgentRecord({ ...BASE_RECORD, lastStatus: status }, ARCHIVE); expect(archived.lastStatus).toBe("idle"); } @@ -50,7 +48,7 @@ test("clears persisted attention when archiving", () => { attentionReason: "finished", attentionTimestamp: "2025-01-02T12:00:00.000Z", }, - { archivedAt: "2025-01-03T00:00:00.000Z" }, + ARCHIVE, ); expect(archived).toMatchObject({ @@ -60,11 +58,19 @@ test("clears persisted attention when archiving", () => { }); }); -test("can stamp updatedAt to the archive timestamp", () => { - const archived = buildArchivedAgentRecord(BASE_RECORD, { - archivedAt: "2025-01-03T00:00:00.000Z", - updatedAt: "2025-01-03T00:00:00.000Z", - }); +test("moves updatedAt to the archive time", () => { + const archived = buildArchivedAgentRecord(BASE_RECORD, ARCHIVE); expect(archived.updatedAt).toBe("2025-01-03T00:00:00.000Z"); }); + +test("keeps a later updatedAt when the archive time is earlier", () => { + // Putting an original archive back after a failed unarchive must not move updatedAt backwards. + const archived = buildArchivedAgentRecord( + { ...BASE_RECORD, updatedAt: "2025-01-04T00:00:00.000Z" }, + ARCHIVE, + ); + + expect(archived.archivedAt).toBe("2025-01-03T00:00:00.000Z"); + expect(archived.updatedAt).toBe("2025-01-04T00:00:00.000Z"); +}); diff --git a/packages/server/src/server/agent/agent-archive.ts b/packages/server/src/server/agent/agent-archive.ts index 7e35d98f89..a66b82c620 100644 --- a/packages/server/src/server/agent/agent-archive.ts +++ b/packages/server/src/server/agent/agent-archive.ts @@ -3,19 +3,19 @@ import type { StoredAgentRecord } from "./agent-storage.js"; export type ArchivedStoredAgentRecord = StoredAgentRecord & { archivedAt: string }; interface BuildArchivedAgentRecordOptions { - archivedAt?: string; - updatedAt?: string; + archivedAt: string; + /** When this archive happened. Re-archiving keeps the original archivedAt but still records it. */ + updatedAt: string; } export function buildArchivedAgentRecord( record: StoredAgentRecord, - options?: BuildArchivedAgentRecordOptions, + options: BuildArchivedAgentRecordOptions, ): ArchivedStoredAgentRecord { - const archivedAt = options?.archivedAt ?? new Date().toISOString(); return { ...record, - archivedAt, - updatedAt: options?.updatedAt ?? record.updatedAt, + archivedAt: options.archivedAt, + updatedAt: latestTimestamp(record.updatedAt, options.updatedAt), lastStatus: normalizeArchivedStatus(record.lastStatus), requiresAttention: false, attentionReason: null, @@ -23,6 +23,13 @@ export function buildArchivedAgentRecord( }; } +// Archiving is the agent's latest change. An earlier archive time (an original archive put back +// after a failed unarchive) keeps the newer updatedAt, since clients drop an agent update that is +// older than the one they hold. +function latestTimestamp(current: string, archiveTime: string): string { + return Date.parse(archiveTime) > Date.parse(current) ? archiveTime : current; +} + function normalizeArchivedStatus( status: StoredAgentRecord["lastStatus"], ): StoredAgentRecord["lastStatus"] { diff --git a/packages/server/src/server/agent/agent-manager.test.ts b/packages/server/src/server/agent/agent-manager.test.ts index 352391108f..5274953e0d 100644 --- a/packages/server/src/server/agent/agent-manager.test.ts +++ b/packages/server/src/server/agent/agent-manager.test.ts @@ -6494,6 +6494,114 @@ test("archiveSnapshot dispatches archived state for stored-only agents", async ( expect(last.lifecycle).toBe("closed"); }); +test("archiveSnapshot moves a stored-only agent's updatedAt to its archive time, as a live archive does", async () => { + const workdir = mkdtempSync(join(tmpdir(), "agent-manager-archive-snapshot-updated-at-")); + const storage = new AgentStorage(join(workdir, "agents"), logger); + const manager = new AgentManager({ + clients: { codex: new TestAgentClient() }, + registry: storage, + logger, + }); + + const created = await manager.createAgent( + { provider: "codex", cwd: workdir, title: "Stored archive updatedAt" }, + undefined, + { workspaceId: undefined }, + ); + await manager.closeAgent(created.id); + const before = await storage.get(created.id); + expect(before?.updatedAt).toEqual(expect.any(String)); + + // Later than anything the agent did: lists sorted by updated_at put it first, as they do + // for an agent archived while loaded. + const archivedAt = new Date(Date.parse(before!.updatedAt) + 60_000).toISOString(); + const archived = await manager.archiveSnapshot(created.id, archivedAt); + + expect(archived.updatedAt).toBe(archivedAt); + expect((await storage.get(created.id))?.updatedAt).toBe(archivedAt); +}); + +test("archiveSnapshot restoring an earlier archive time keeps updatedAt at the unarchive", async () => { + const workdir = mkdtempSync(join(tmpdir(), "agent-manager-archive-snapshot-restore-")); + const storage = new AgentStorage(join(workdir, "agents"), logger); + const manager = new AgentManager({ + clients: { codex: new TestAgentClient() }, + registry: storage, + logger, + }); + + const created = await manager.createAgent( + { provider: "codex", cwd: workdir, title: "Restore archive updatedAt" }, + undefined, + { workspaceId: undefined }, + ); + await manager.closeAgent(created.id); + const originalArchivedAt = (await manager.archiveSnapshot(created.id, new Date().toISOString())) + .archivedAt!; + await new Promise((resolve) => setTimeout(resolve, 5)); + await manager.unarchiveSnapshot(created.id); + const unarchivedAt = (await storage.get(created.id))!.updatedAt; + + const events: ManagedAgent[] = []; + manager.subscribe( + (event) => { + if (event.type === "agent_state" && event.agent.id === created.id) { + events.push(event.agent); + } + }, + { agentId: created.id, replayState: false }, + ); + + // A send that could not load the agent puts its original archive back. + await manager.archiveSnapshot(created.id, originalArchivedAt); + + // Clients keep the newer of two updates, so an older updatedAt would leave them showing it active. + const stored = await storage.get(created.id); + expect(stored?.archivedAt).toBe(originalArchivedAt); + expect(stored?.updatedAt).toBe(unarchivedAt); + expect(events.at(-1)?.updatedAt.toISOString()).toBe(unarchivedAt); +}); + +test("archiveSnapshot restoring an earlier archive time on a loaded agent keeps updatedAt at the unarchive", async () => { + const workdir = mkdtempSync(join(tmpdir(), "agent-manager-archive-snapshot-restore-loaded-")); + const storage = new AgentStorage(join(workdir, "agents"), logger); + const manager = new AgentManager({ + clients: { codex: new TestAgentClient() }, + registry: storage, + logger, + }); + + const created = await manager.createAgent( + { provider: "codex", cwd: workdir, title: "Restore loaded archive updatedAt" }, + undefined, + { workspaceId: undefined }, + ); + const { archivedAt: originalArchivedAt } = await manager.archiveAgent(created.id); + await new Promise((resolve) => setTimeout(resolve, 5)); + await manager.unarchiveSnapshot(created.id); + const unarchivedAt = Date.parse((await storage.get(created.id))!.updatedAt); + // The send resumed the agent before a later loading step failed, so it is still loaded. + await ensureAgentLoaded(created.id, { agentManager: manager, agentStorage: storage, logger }); + expect(manager.getAgent(created.id)).not.toBeNull(); + + const events: ManagedAgent[] = []; + manager.subscribe( + (event) => { + if (event.type === "agent_state" && event.agent.id === created.id) { + events.push(event.agent); + } + }, + { agentId: created.id, replayState: false }, + ); + + await manager.archiveSnapshot(created.id, originalArchivedAt); + + const stored = await storage.get(created.id); + expect(stored?.archivedAt).toBe(originalArchivedAt); + expect(Date.parse(stored!.updatedAt)).toBeGreaterThanOrEqual(unarchivedAt); + expect(events.at(-1)?.updatedAt.getTime()).toBeGreaterThanOrEqual(unarchivedAt); +}); + test("markAgentUnread restores error attention on a read errored agent", async () => { const workdir = mkdtempSync(join(tmpdir(), "agent-manager-mark-unread-error-")); const storage = new AgentStorage(join(workdir, "agents"), logger); @@ -14606,14 +14714,16 @@ test("workspace archive preserves a loaded history agent stamp and stamps cascad labels: { [PARENT_AGENT_ID_LABEL]: parent.id }, workspaceId: undefined, }); - const stamp = "2026-11-09T00:00:00.000Z"; + // Archive stamps are taken when the archive happens, so they follow the agents' activity. + const stamp = new Date(Date.now() + 60_000).toISOString(); + const laterStamp = new Date(Date.now() + 120_000).toISOString(); await manager.archiveAgent(parent.id, stamp); expect((await storage.get(parent.id))?.archivedAt).toBe(stamp); expect((await storage.get(child.id))?.archivedAt).toBe(stamp); await ensureAgentLoaded(parent.id, { agentManager: manager, agentStorage: storage, logger }); - await manager.archiveAgent(parent.id, "2026-11-09T01:00:00.000Z"); + await manager.archiveAgent(parent.id, laterStamp); expect((await storage.get(parent.id))?.archivedAt).toBe(stamp); - expect((await storage.get(parent.id))?.updatedAt).toBe("2026-11-09T01:00:00.000Z"); + expect((await storage.get(parent.id))?.updatedAt).toBe(laterStamp); } finally { await storage.flush(); rmSync(workdir, { recursive: true, force: true }); diff --git a/packages/server/src/server/agent/agent-manager.ts b/packages/server/src/server/agent/agent-manager.ts index ece45289e2..323c830483 100644 --- a/packages/server/src/server/agent/agent-manager.ts +++ b/packages/server/src/server/agent/agent-manager.ts @@ -2350,7 +2350,7 @@ export class AgentManager { private async persistArchivedRecord( record: StoredAgentRecord, - options: { archivedAt: string; updatedAt?: string }, + options: { archivedAt: string; updatedAt: string }, ): Promise { const archivedRecord = buildArchivedAgentRecord(record, options); await this.requireRegistry().upsert(archivedRecord); @@ -2870,7 +2870,11 @@ export class AgentManager { throw new Error(`Agent not found: ${agentId}`); } - const nextRecord = await this.persistArchivedRecord(record, { archivedAt }); + // Archiving is the agent's latest change, as markRecordArchived records it for a loaded agent. + const nextRecord = await this.persistArchivedRecord(record, { + archivedAt, + updatedAt: archivedAt, + }); await this.syncNativeArchiveState(record.provider, record.persistence, "archive");