diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts index d90d0f1b7c..3462955fcc 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts @@ -427,6 +427,28 @@ describe("ProviderRuntimeIngestion", () => { expect(latest?.agents).toHaveLength(1); expect(latest?.agents[0]?.status).toBe("stopped"); expect(latest?.agents[0]?.errorMessage).toBe("orphaned on session exit"); + + // The session-exit sweep releases the in-memory roster; a later session on + // the same thread re-hydrates from the persisted snapshot instead of the + // evicted map (agent-1 stays stopped, not resurrected). + harness.emit({ + type: "task.started", + eventId: asEventId("evt-agent-restart"), + provider: ProviderDriverKind.make("claudeAgent"), + threadId: asThreadId("thread-1"), + createdAt: "2026-01-01T00:00:04.000Z", + payload: { taskId: "agent-2", description: "Follow-up", taskType: "local_agent" }, + }); + await harness.drain(); + thread = (await harness.readModel()).threads.find((entry) => entry.id === "thread-1"); + const rehydrated = + thread?.activities.filter((activity) => activity.kind === "agent.snapshot") ?? []; + expect(rehydrated).toHaveLength(3); + const roster = rehydrated.at(-1)?.payload as { agents: Array } | undefined; + expect(roster?.agents.map((agent) => [agent.agentId, agent.status])).toEqual([ + ["agent-1", "stopped"], + ["agent-2", "running"], + ]); }); it("maps turn started/completed events into thread session updates", async () => { diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts index 6706a7da9b..377d5f5796 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -910,6 +910,10 @@ const make = Effect.gen(function* () { const serverSettingsService = yield* ServerSettingsService; const agentsByThread = new Map>(); const hydratedAgentThreads = new Set(); + // Threads whose latest roster failed to persist: the in-memory state is + // newer than the last agent.snapshot activity, so the next event must + // re-dispatch even if it is not material on its own. + const pendingRosterRedispatch = new Set(); let agentSnapshotDispatchCount = 0; const providerCommandId = (event: ProviderRuntimeEvent, tag: string) => crypto.randomUUIDv4.pipe( @@ -1524,6 +1528,12 @@ const make = Effect.gen(function* () { }); if (!hydratedAgentThreads.has(thread.id)) { + // Hydration source is the 500-capped activities projection — the same + // list clients derive from. If the last agent.snapshot has aged past + // the cap (500 activities with no material agent transition), both + // sides have already lost it and hydrating empty is consistent, not a + // regression; the roster only ever contains agents from the era the + // timeline still shows. const detail = yield* getLoadedThreadDetail(); const latestSnapshot = detail?.activities.findLast( (activity) => activity.kind === THREAD_AGENTS_ACTIVITY_KIND, @@ -1568,6 +1578,9 @@ const make = Effect.gen(function* () { agentRosterMaterial = true; } } + if (pendingRosterRedispatch.has(thread.id) && threadAgents.size > 0) { + agentRosterMaterial = true; + } if (agentRosterMaterial) { pruneSettledAgents(threadAgents); const roster = Array.from(threadAgents.values()); @@ -1589,26 +1602,22 @@ const make = Effect.gen(function* () { payload: { agents: roster }, turnId: toTurnId(event.turnId) ?? null, }; - // The reducer map was already mutated; if the append fails, drop the - // hydration marker so the next event re-hydrates from the last - // *persisted* snapshot and re-detects the missed material change — - // otherwise the roster silently stays stale until the next transition. - yield* orchestrationEngine - .dispatch({ - type: "thread.activity.append", - commandId: yield* providerCommandId(event, "agent-snapshot-append"), - threadId: thread.id, - activity, - createdAt: activity.createdAt, - }) - .pipe( - Effect.tapError(() => - Effect.sync(() => { - hydratedAgentThreads.delete(thread.id); - agentsByThread.delete(thread.id); - }), - ), - ); + // The in-memory roster is authoritative (already folded); if the + // append fails, keep it and flag the thread so the NEXT event + // re-dispatches the full roster — discarding memory here would lose + // transitions newer than the last persisted snapshot. + // Armed before the dispatch and cleared only on success: if the append + // fails (the error propagates to processInputSafely as usual), the + // flag makes the NEXT event re-dispatch the full roster. + pendingRosterRedispatch.add(thread.id); + yield* orchestrationEngine.dispatch({ + type: "thread.activity.append", + commandId: yield* providerCommandId(event, "agent-snapshot-append"), + threadId: thread.id, + activity, + createdAt: activity.createdAt, + }); + pendingRosterRedispatch.delete(thread.id); agentSnapshotDispatchCount += 1; yield* Effect.logDebug("provider agent snapshot appended", { threadId: thread.id, @@ -1617,6 +1626,14 @@ const make = Effect.gen(function* () { }); } + // Sessions are done producing agent events once they exit; release the + // per-thread reducer state. A later session re-hydrates from the + // persisted snapshot (the exit sweep above just wrote the final roster). + if (event.type === "session.exited" && !pendingRosterRedispatch.has(thread.id)) { + agentsByThread.delete(thread.id); + hydratedAgentThreads.delete(thread.id); + } + const now = event.createdAt; const eventTurnId = toTurnId(event.turnId); const activeTurnId = thread.session?.activeTurnId ?? null;