feat(ai): run agent sessions as background tasks

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-08-26 14:09:08 +08:00
1 parent b37a1f8538
commit eeeb4e5011
34 files changed
+3440 -184

No files matched your search

@@ -0,0 +1,206 @@
import { afterEach, describe, expect, it } from "vitest";
import {
acquireDesktopAiRunSlot,
activeDesktopAiRuns,
blockingDesktopAiRunsForQuit,
bumpDesktopAiRunSeq,
cancelQueuedDesktopAiRun,
desktopAiRun,
finishDesktopAiRun,
hasActiveDesktopAiRun,
isTerminalDesktopAiRunStatus,
registerDesktopAiRun,
releaseDesktopAiRunSlot,
resetDesktopAiRunRegistryForTests,
} from "@/lib/ai/desktopAiRunRegistry";
import { resolveAiRuntimeStrategy, supportsBackgroundAiRuns } from "@/lib/ai/aiRuntimeStrategy";
describe("AI runtime platform strategy", () => {
afterEach(() => resetDesktopAiRunRegistryForTests());
it("enables detached runs only for the Tauri desktop runtime", () => {
expect(resolveAiRuntimeStrategy(true)).toBe("desktop-background");
expect(supportsBackgroundAiRuns("desktop-background")).toBe(true);
expect(resolveAiRuntimeStrategy(false)).toBe("web-cancel-on-navigation");
expect(supportsBackgroundAiRuns("web-cancel-on-navigation")).toBe(false);
});
it("keeps a desktop run addressable by conversation until terminal", () => {
const run = registerDesktopAiRun({
runId: "run-1",
conversationId: "conversation-1",
sessionIds: ["session-1"],
currentSessionId: "session-1",
status: "running",
messages: [{ content: "partial" }],
assistantMessageIndex: 0,
connectionId: "connection-1",
connectionName: "local",
database: "db",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
});
expect(desktopAiRun("conversation-1")).toBe(run);
expect(activeDesktopAiRuns()).toEqual([run]);
finishDesktopAiRun(run, "completed");
expect(desktopAiRun("conversation-1")?.status).toBe("completed");
expect(activeDesktopAiRuns()).toEqual([]);
});
it("admits at most three runs and lets a queued run be cancelled", async () => {
const makeRun = (index: number) =>
registerDesktopAiRun({
runId: `run-${index}`,
conversationId: `conversation-${index}`,
sessionIds: [],
currentSessionId: "",
status: "preparing" as const,
messages: [],
assistantMessageIndex: -1,
connectionId: `connection-${index}`,
connectionName: "local",
database: "db",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
});
const runs = [1, 2, 3, 4].map(makeRun);
await Promise.all(runs.slice(0, 3).map(acquireDesktopAiRunSlot));
const fourthAdmission = acquireDesktopAiRunSlot(runs[3]);
expect(runs[3].status).toBe("queued");
expect(cancelQueuedDesktopAiRun(runs[3])).toBe(true);
await expect(fourthAdmission).resolves.toBe(false);
runs.slice(0, 3).forEach((run) => releaseDesktopAiRunSlot(run.runId));
});
it("keeps a recovered pending-input run addressable without consuming a slot or blocking quit", async () => {
const run = registerDesktopAiRun({
runId: "run-recovered",
conversationId: "conversation-1",
sessionIds: [],
currentSessionId: "",
status: "pending_recoverable",
messages: [],
assistantMessageIndex: -1,
connectionId: "connection-1",
connectionName: "local",
database: "db",
pendingInput: "select * from orders",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
});
// Addressable (so the row can render "pending input"), but neither an
// active run nor a quit-blocking one.
expect(desktopAiRun("conversation-1")).toBe(run);
expect(activeDesktopAiRuns()).toEqual([]);
expect(blockingDesktopAiRunsForQuit()).toEqual([]);
// It also does not consume a global concurrency slot: a fresh preparing run
// is admitted immediately even though the recovered run is registered.
const fresh = registerDesktopAiRun({
runId: "run-fresh",
conversationId: "conversation-2",
sessionIds: [],
currentSessionId: "",
status: "preparing",
messages: [],
assistantMessageIndex: -1,
connectionId: "connection-2",
connectionName: "local",
database: "db",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
});
const admission = acquireDesktopAiRunSlot(fresh);
expect(fresh.status).toBe("preparing");
await expect(admission).resolves.toBe(true);
releaseDesktopAiRunSlot(fresh.runId);
});
it("assigns strictly increasing event seqs that persist on the run", () => {
const run = registerDesktopAiRun({
runId: "run-seq",
conversationId: "conversation-1",
sessionIds: ["session-1", "session-2"],
currentSessionId: "session-2",
status: "running",
messages: [],
assistantMessageIndex: -1,
connectionId: "connection-1",
connectionName: "local",
database: "db",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
});
// Parent PRD §8: seq starts at 1 and strictly increases across sessions.
expect(bumpDesktopAiRunSeq(run)).toBe(1);
expect(bumpDesktopAiRunSeq(run)).toBe(2);
expect(run.maxSeq).toBe(2);
// A recovered run with a persisted maxSeq continues from there.
run.maxSeq = 40;
expect(bumpDesktopAiRunSeq(run)).toBe(41);
expect(desktopAiRun("conversation-1")?.maxSeq).toBe(41);
});
it("treats every non-terminal status as active for queue-send, including pending_recoverable", () => {
const statuses = ["preparing", "queued", "running", "awaiting_write_confirmation", "pending_recoverable"] as const;
for (const status of statuses) {
registerDesktopAiRun({
runId: `run-${status}`,
conversationId: `conversation-${status}`,
sessionIds: [],
currentSessionId: "",
status,
messages: [],
assistantMessageIndex: -1,
connectionId: "connection-1",
connectionName: "local",
database: "db",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
});
expect(hasActiveDesktopAiRun(`conversation-${status}`)).toBe(true);
}
for (const status of ["completed", "failed", "cancelled", "interrupted"] as const) {
const run = registerDesktopAiRun({
runId: `run-${status}`,
conversationId: `conversation-${status}`,
sessionIds: [],
currentSessionId: "",
status,
messages: [],
assistantMessageIndex: -1,
connectionId: "connection-1",
connectionName: "local",
database: "db",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
});
expect(hasActiveDesktopAiRun(`conversation-${status}`)).toBe(false);
finishDesktopAiRun(run, "completed");
}
});
it("classifies terminal statuses for the stop-wait logic (isTerminalDesktopAiRunStatus)", () => {
for (const status of ["completed", "failed", "cancelled"] as const) {
expect(isTerminalDesktopAiRunStatus(status)).toBe(true);
}
// `interrupted` is a persisted-row-only pseudo status, never a runtime one.
for (const status of ["preparing", "queued", "running", "awaiting_write_confirmation", "pending_recoverable"] as const) {
expect(isTerminalDesktopAiRunStatus(status)).toBe(false);
}
});
});
@@ -0,0 +1,171 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createDesktopAiRunSnapshotScheduler } from "@/lib/ai/desktopAiRunSnapshotScheduler";
import type { DesktopAiRunRuntime } from "@/lib/ai/desktopAiRunRegistry";
function makeRun(overrides: Partial<DesktopAiRunRuntime> = {}): DesktopAiRunRuntime {
return {
runId: "run-1",
conversationId: "conversation-1",
sessionIds: [],
currentSessionId: "",
status: "running",
messages: [],
assistantMessageIndex: -1,
connectionId: "connection-1",
connectionName: "local",
database: "db",
createdAt: "2026-08-25T00:00:00.000Z",
updatedAt: "2026-08-25T00:00:00.000Z",
cancelRequested: false,
...overrides,
};
}
// Finding 1 regression coverage: detached streaming deltas used to live only
// in memory (a crash/quit mid-response lost everything after the last
// pre-stream snapshot). The scheduler persists snapshots incrementally from
// the delta flush path - throttled to keep writes cheap, serialized per run so
// a slow write can never let an older snapshot overwrite a newer one.
describe("desktop AI run snapshot scheduler", () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());
it("throttles streaming flushes to one durable save per interval, capturing state at fire time", async () => {
const persistedContentLengths: number[] = [];
const run = makeRun();
const scheduler = createDesktopAiRunSnapshotScheduler({
persist: async () => {
persistedContentLengths.push(run.messages.length);
},
intervalMs: 2000,
});
run.messages.push({ content: "a" });
scheduler.schedule(run);
// Further flushes inside the window must not stack extra timers.
run.messages.push({ content: "b" });
scheduler.schedule(run);
run.messages.push({ content: "c" });
scheduler.schedule(run);
await vi.advanceTimersByTimeAsync(1999);
expect(persistedContentLengths).toEqual([]);
await vi.advanceTimersByTimeAsync(1);
// The save reads the run's state when the timer fires (3 messages), not at
// schedule time - so a crash after the first flush still persists the tail.
expect(persistedContentLengths).toEqual([3]);
});
it("re-arms after each save so a long stream keeps snapshotting", async () => {
const persisted: number[] = [];
const run = makeRun();
const scheduler = createDesktopAiRunSnapshotScheduler({
persist: async () => {
persisted.push(run.messages.length);
},
intervalMs: 2000,
});
scheduler.schedule(run);
await vi.advanceTimersByTimeAsync(2000);
expect(persisted).toEqual([0]);
run.messages.push({ content: "late" });
scheduler.schedule(run);
await vi.advanceTimersByTimeAsync(2000);
expect(persisted).toEqual([0, 1]);
});
it("serializes saves so an older in-flight snapshot can never overwrite a newer one", async () => {
const callOrder: number[] = [];
let resolveFirst!: () => void;
const persist = vi
.fn()
.mockImplementationOnce(() => {
callOrder.push(1);
return new Promise<void>((resolve) => {
resolveFirst = resolve;
});
})
.mockImplementationOnce(() => {
callOrder.push(2);
return Promise.resolve();
});
const scheduler = createDesktopAiRunSnapshotScheduler({ persist, intervalMs: 2000 });
const run = makeRun();
const first = scheduler.save(run);
run.status = "completed";
const second = scheduler.save(run);
// The newer save must not start (let alone finish) before the older one
// resolves - saves are INSERT OR REPLACEs keyed by the same run row.
await vi.advanceTimersByTimeAsync(10_000);
expect(persist).toHaveBeenCalledTimes(1);
resolveFirst();
await Promise.all([first, second]);
expect(callOrder).toEqual([1, 2]);
});
it("skips a queued save if the run is deleted while a prior write is still in flight", async () => {
// Reviewed finding (delete-resurrection race): a save() that was enqueued
// behind a slow in-flight write was NOT re-checked for discardOnFinish at
// execution time. The delete path commits its DELETE first, so the queued
// save firing afterwards recreated the just-deleted conversation via
// INSERT OR REPLACE. The discard marker must be checked at the actual
// execution point, not just at enqueue time.
const persist = vi.fn(async () => {});
let resolveFirst!: () => void;
persist.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
resolveFirst = resolve;
}),
);
const scheduler = createDesktopAiRunSnapshotScheduler({ persist, intervalMs: 2000 });
const run = makeRun();
// First save: flush the microtask so the write actually starts (and blocks
// on the pending mock) BEFORE the second is enqueued behind it.
const first = scheduler.save(run);
await Promise.resolve();
const second = scheduler.save(run);
// The user deletes the conversation while the first write is still running.
run.discardOnFinish = true;
resolveFirst();
await Promise.all([first, second]);
// Only the already-executing first write persisted; the queued one skipped.
expect(persist).toHaveBeenCalledTimes(1);
});
it("never persists a run the user deleted (discardOnFinish), even through the ordered path", async () => {
const persist = vi.fn(async () => {});
const scheduler = createDesktopAiRunSnapshotScheduler({ persist, intervalMs: 2000 });
scheduler.schedule(makeRun({ discardOnFinish: true }));
await vi.advanceTimersByTimeAsync(5000);
expect(persist).not.toHaveBeenCalled();
// A run deleted after scheduling must be skipped when the timer fires.
const run = makeRun();
scheduler.schedule(run);
run.discardOnFinish = true;
await vi.advanceTimersByTimeAsync(5000);
expect(persist).not.toHaveBeenCalled();
// save() on a discarded run is also a no-op (delete-resurrection guard).
await scheduler.save(makeRun({ discardOnFinish: true }));
expect(persist).not.toHaveBeenCalled();
});
it("cancel() drops a pending throttled save so a late fire cannot write stale state", async () => {
const persist = vi.fn(async () => {});
const scheduler = createDesktopAiRunSnapshotScheduler({ persist, intervalMs: 2000 });
const run = makeRun();
scheduler.schedule(run);
scheduler.cancel(run.runId);
await vi.advanceTimersByTimeAsync(5000);
expect(persist).not.toHaveBeenCalled();
});
});