A desktop coding agent with free access to frontier models.
TypeScript
fix: keep unsent uploads across sign-out
ogsamratbe189631a68c
diff --git a/apps/app/src/app/lib/omnirush-server.ts b/apps/app/src/app/lib/omnirush-server.tsindex 9f1f3a7f..58a778f0 100644--- a/apps/app/src/app/lib/omnirush-server.ts+++ b/apps/app/src/app/lib/omnirush-server.ts@@ -413,6 +413,10 @@ export type OmniRushBestPractices = { enabled: boolean };* show once (null when it could not be asked).*/export type OmniRushFilesUsed = { active: boolean | null; available: boolean | null; consentText: string | null };+/** GET /omnirush/capture/pending: uploads and project archives still queued (a sign-out first sends what it can). */+export type OmniRushPendingUploads = { uploads: number; archives: number };+/** The server waits at most 10 s for queued uploads at a sign-out; a little more for the answer. */+const SIGN_OUT_FLUSH_TIMEOUT_MS = 15_000;/** GET /omnirush/capture/status: whether session capture runs; "off" when signed out (nothing to capture). */export type OmniRushCaptureStatus = {running: boolean;@@ -1980,6 +1984,10 @@ export function createOmniRushServerClient(options: { baseUrl: string; token?: srequestJson<OmniRushFilesUsed>(baseUrl, "/runtime-config/files-used", { token, hostToken, timeoutMs: FILES_USED_TIMEOUT_MS }),getCaptureStatus: () =>requestJson<OmniRushCaptureStatus>(baseUrl, "/omnirush/capture/status", { token, hostToken, timeoutMs: timeouts.config }),+ getPendingUploads: () =>+ requestJson<OmniRushPendingUploads>(baseUrl, "/omnirush/capture/pending", { token, hostToken, timeoutMs: timeouts.config }),+ flushPendingUploads: () =>+ requestJson<{ before: number; after: number }>(baseUrl, "/omnirush/capture/flush", { token, hostToken, method: "POST", body: {}, timeoutMs: SIGN_OUT_FLUSH_TIMEOUT_MS }),getWorkspaceBox: (workspaceId: string, options?: { fresh?: boolean }) =>requestJson<OmniRushWorkspaceBox>(baseUrl, `/workspace/${encodeURIComponent(workspaceId)}/box${options?.fresh ? "?fresh=1" : ""}`, {token, hostToken, timeoutMs: timeouts.config,diff --git a/apps/app/src/react-app/domains/settings/pages/general-view.tsx b/apps/app/src/react-app/domains/settings/pages/general-view.tsxindex 84ad59df..abb933eb 100644--- a/apps/app/src/react-app/domains/settings/pages/general-view.tsx+++ b/apps/app/src/react-app/domains/settings/pages/general-view.tsx@@ -38,6 +38,7 @@ import {type ApprovalsClient,} from "../approval-mode";import { DiagnosticsSection } from "./diagnostics-section";+import { keptPendingLine, sendPendingBeforeSignOut } from "../sign-out-uploads";import { BEST_PRACTICES_HELP, useBestPractices, type BestPracticesClient } from "../best-practices";import { useLinuxSandbox, type LinuxSandboxClient } from "../linux-sandbox";import { LinuxSandboxCard } from "../linux-sandbox-card";@@ -330,11 +331,13 @@ export function GeneralSettingsView(props: GeneralSettingsViewProps) {// default server the next sign-in will use, not the one just left.const server = account?.gatewayHost ?? null;try {+ // What is still queued goes first (a few seconds at most); the rest stays on this computer for this account.+ const kept = account?.connected ? await sendPendingBeforeSignOut(setAccountMessage) : 0;const result = await omnirushAccountSignOut();resetAnalyticsAccount();const next = await omnirushAccountStatus().catch(() => null);setAccount(next ?? { connected: false, gatewayConfigured: account?.gatewayConfigured ?? false });- setAccountMessage(accountSignOutMessage(result, server));+ setAccountMessage(kept > 0 ? `${accountSignOutMessage(result, server)} ${keptPendingLine(kept)}` : accountSignOutMessage(result, server));} catch (error) {setAccountMessage(error instanceof Error ? error.message : "The account could not be signed out.");} finally {diff --git a/apps/app/src/react-app/domains/settings/sign-out-uploads.ts b/apps/app/src/react-app/domains/settings/sign-out-uploads.tsnew file mode 100644index 00000000..50bc8528--- /dev/null+++ b/apps/app/src/react-app/domains/settings/sign-out-uploads.ts@@ -0,0 +1,40 @@+// Before a sign-out: what is still queued for upload goes first, for a few+// seconds at most (the server's POST /omnirush/capture/flush). What does not+// go by then stays on this computer for the account, and is sent after its+// next sign-in; another account never sends it.+import { createOmniRushServerClient } from "../../../app/lib/omnirush-server";+import { resolveOmniRushConnection } from "../../shell/omnirush-connection";++export type PendingUploadsClient = {+ getPendingUploads(): Promise<{ uploads: number; archives: number }>;+ flushPendingUploads(): Promise<{ before: number; after: number }>;+};++export function sendingPendingLine(count: number): string {+ return `Sending ${count} pending upload${count === 1 ? "" : "s"}…`;+}++export function keptPendingLine(count: number): string {+ return `${count} upload${count === 1 ? " is" : "s are"} kept on this computer and sent after you sign in to this account again.`;+}++/** Sends what is queued (bounded by the server); resolves with how many are still kept. Never throws. */+export async function sendPendingBeforeSignOut(say: (text: string) => void, client?: PendingUploadsClient | null): Promise<number> {+ try {+ let target = client ?? null;+ if (!target) {+ const connection = await resolveOmniRushConnection();+ if (!connection.normalizedBaseUrl || !connection.resolvedToken) return 0;+ target = createOmniRushServerClient({ baseUrl: connection.normalizedBaseUrl, token: connection.resolvedToken, hostToken: connection.resolvedHostToken });+ }+ const pending = await target.getPendingUploads();+ const count = pending.uploads + pending.archives;+ if (count === 0) return 0;+ say(sendingPendingLine(count));+ const result = await target.flushPendingUploads();+ return result.after;+ } catch {+ // An older server without the routes, or unreachable: nothing is deleted at the sign-out either way.+ return 0;+ }+}diff --git a/apps/app/tests/sign-out-uploads.test.ts b/apps/app/tests/sign-out-uploads.test.tsnew file mode 100644index 00000000..b11e7ff2--- /dev/null+++ b/apps/app/tests/sign-out-uploads.test.ts@@ -0,0 +1,35 @@+import { describe, expect, test } from "bun:test";++import { keptPendingLine, sendPendingBeforeSignOut, sendingPendingLine } from "../src/react-app/domains/settings/sign-out-uploads";++describe("sending pending uploads before a sign-out", () => {+ test("says how many go, sends them, and returns how many stay on this computer", async () => {+ const said: string[] = [];+ let flushed = 0;+ const kept = await sendPendingBeforeSignOut((text) => said.push(text), {+ getPendingUploads: async () => ({ uploads: 2, archives: 1 }),+ flushPendingUploads: async () => {+ flushed += 1;+ return { before: 3, after: 1 };+ },+ });+ expect(said).toEqual(["Sending 3 pending uploads…"]);+ expect(flushed).toBe(1);+ expect(kept).toBe(1);+ expect(keptPendingLine(kept)).toBe("1 upload is kept on this computer and sent after you sign in to this account again.");+ });++ test("nothing pending: nothing said, nothing sent; a server without the routes never stops the sign-out", async () => {+ const said: string[] = [];+ expect(await sendPendingBeforeSignOut((text) => said.push(text), {+ getPendingUploads: async () => ({ uploads: 0, archives: 0 }),+ flushPendingUploads: async () => { throw new Error("not called"); },+ })).toBe(0);+ expect(await sendPendingBeforeSignOut((text) => said.push(text), {+ getPendingUploads: async () => { throw new Error("404"); },+ flushPendingUploads: async () => ({ before: 0, after: 0 }),+ })).toBe(0);+ expect(said).toEqual([]);+ expect(sendingPendingLine(1)).toBe("Sending 1 pending upload…");+ });+});diff --git a/apps/desktop/electron/main.mjs b/apps/desktop/electron/main.mjsindex 8aefd840..55855da6 100644--- a/apps/desktop/electron/main.mjs+++ b/apps/desktop/electron/main.mjs@@ -2462,7 +2462,8 @@ const desktopCommandHandlers = {return result;},"omnirushAccountSignOut": async (event, ...args) => {- // Only a user's sign-out deletes what is queued for upload; a sign-in the server retired keeps it.+ // A user's sign-out: the next server start stops sending what is queued and keeps it on disk for this+ // account (sent after its next sign-in, never under another account). The app sent what it could first.await runtimeManager.markUploadQueueClear();const result = await omnirushAccountStore.clear();await runtimeManager.omnirushServerRestart({ reason: "account_signed_out", source: "account", userInitiated: true });diff --git a/apps/desktop/electron/runtime.mjs b/apps/desktop/electron/runtime.mjsindex e3d4f45e..7ea9dca4 100644--- a/apps/desktop/electron/runtime.mjs+++ b/apps/desktop/electron/runtime.mjs@@ -393,8 +393,9 @@ export function snapshotEngineState(state) {*//*** Set by a user's sign-out, cleared once a server started with it: that- * server deletes the queued uploads and archives first. On disk, so a quit- * before the next start still clears them at the one after.+ * server stops sending what is queued and keeps it on disk for the account+ * that signed out (capture-host signOut). On disk, so a quit before the next+ * start still does it at the one after.*/export function uploadQueueClearMarkerPath(userDataDir) {return path.join(userDataDir, "omnirush-upload-queue-clear");@@ -2682,7 +2683,7 @@ export function createRuntimeManager({engineInstall,omnirushServerInfo,omnirushServerRestart: (options) => withRuntimeLifecycle(() => omnirushServerRestart(options)),- /** A user's sign-out: the next server start deletes the queued uploads and archives. */+ /** A user's sign-out: the next server start keeps the queued uploads and archives for that account only. */markUploadQueueClear: async () => {await mkdir(userDataDir, { recursive: true });await writeFile(uploadQueueClearMarkerPath(userDataDir), `${new Date().toISOString()}\n`, { mode: 0o600 });diff --git a/apps/server/src/capture-client.ts b/apps/server/src/capture-client.tsindex e3bddfb3..e0ff4b3e 100644--- a/apps/server/src/capture-client.ts+++ b/apps/server/src/capture-client.ts@@ -120,8 +120,14 @@ export type CaptureService = {diagnostics(): Promise<CaptureDiagnostics | null>;/** Files used, for the app's one-time notice (null: capture is not running). */filesUsedStatus(): Promise<FilesUsedStatus | null>;+ /** Uploads and project archives queued now (null: capture is not running). */+ pendingUploads(): Promise<PendingUploads | null>;+ /** Before a sign-out: sends what is queued for at most `budgetMs`; the rest stays queued for the account. */+ flushPending(budgetMs: number): Promise<{ before: number; after: number } | null>;};+export type PendingUploads = { uploads: number; archives: number };+/** How much longer than the start gate's own cap the main thread waits for the worker's answer. */const START_GATE_SLACK_MS = 1_000;@@ -296,6 +302,16 @@ class CaptureClient implements CaptureService {return filesUsedStatusOf(await this.query({ kind: "call", id: null, method: "filesUsedStatus", args: [] }));}+ async pendingUploads(): Promise<PendingUploads | null> {+ const value = await this.query({ kind: "call", id: null, method: "pendingUploads", args: [] });+ return countsOf(value, ["uploads", "archives"]) as PendingUploads | null;+ }++ async flushPending(budgetMs: number): Promise<{ before: number; after: number } | null> {+ const value = await this.query({ kind: "call", id: null, method: "flushPending", args: [budgetMs] });+ return countsOf(value, ["before", "after"]) as { before: number; after: number } | null;+ }+stop(options: { archiveFinals?: boolean | Promise<boolean> } = {}): Promise<void> {this.stopping ??= this.shutdown(options.archiveFinals ?? true);return this.stopping;@@ -673,6 +689,18 @@ async function withinTimeout(answer: boolean | Promise<boolean>, ms: number): Pr}}+/** `value` when it is an object with a non-negative integer at each of `keys`, else null. */+function countsOf(value: unknown, keys: readonly string[]): Record<string, number> | null {+ if (!value || typeof value !== "object") return null;+ const out: Record<string, number> = {};+ for (const key of keys) {+ const count = Reflect.get(value, key);+ if (!Number.isSafeInteger(count) || count < 0) return null;+ out[key] = count;+ }+ return out;+}+function filesUsedStatusOf(value: unknown): FilesUsedStatus | null {if (typeof value !== "object" || value === null) return null;const record = value as Record<string, unknown>;diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.tsindex d3985113..97e51fef 100644--- a/apps/server/src/capture-host.ts+++ b/apps/server/src/capture-host.ts@@ -25,10 +25,11 @@ import {type SessionObservers,} from "./session-upload-observer.js";import type { CaptureStopOptions } from "./capture-protocol.js";-import { SessionArchiver, type FilesUsedStatus, type SessionArchiverOptions } from "./session-archive/index.js";+import { ARCHIVE_STATE_DIRECTORY, SessionArchiver, type FilesUsedStatus, type SessionArchiverOptions } from "./session-archive/index.js";+import { archiveOwner } from "./session-archive/account-park.js";import { ProjectArchiveLifecycle, type ArchiveLifecycleLog, type LateWrites } from "./session-archive/lifecycle.js";import { settleLateWriters, type SettleResult } from "./session-archive/late-writes.js";-import { isAbsolute, relative, sep } from "node:path";+import { isAbsolute, join, relative, sep } from "node:path";import { boxSnapshotsDir } from "./box/box.js";import type { BoxCaptureInfo } from "./box/service.js";import { sandboxFilesUsed, type FilesUsedSandbox } from "./session-archive/files-used-sandbox.js";@@ -143,6 +144,7 @@ export class CaptureHost {private readonly sessionRoots = new Map<string, string>();/** The state dir and the app's data/config/cache dirs: never reported or recorded as a touched file. */private readonly appDirs: string[];+ private readonly stateDir: string;private readonly engineTarget?: CaptureHostOptions["engineTarget"];/** When each session's latest prompt was dispatched (archiveStartGate): the turn before's processes started since. */private readonly promptAt = new Map<string, number>();@@ -186,6 +188,7 @@ export class CaptureHost {});const { enabled, excludedDirs, folderGate, baseIdleMs, baseMaxDeferMs, ...auth } = options.archive;this.appDirs = [options.stateDir, ...excludedDirs];+ this.stateDir = options.stateDir;this.archive = new ProjectArchiveLifecycle({archiver: new SessionArchiver({stateDir: options.stateDir,@@ -408,10 +411,47 @@ export class CaptureHost {this.forgetSession(sessionId);}- /** The account is gone: queued archives and spooled uploads are deleted, nothing more is archived. */+ /**+ * The user signed out: nothing more is captured or sent in this app run.+ * What is queued stays on disk for that account (stamped with it): its+ * next sign-in sends it, another account never does (account-park.ts).+ */async signOut(): Promise<void> {+ const owner = await archiveOwner(join(this.stateDir, ARCHIVE_STATE_DIRECTORY)).catch(() => null);await this.archive.signOut();- await this.sessionUploader.clearSpool().catch(() => undefined);+ await this.sessionUploader.keepSpoolFor(owner).catch(() => undefined);+ }++ /** Uploads and project archives queued now (for "Sending N pending uploads…" before a sign-out). */+ async pendingUploads(): Promise<{ uploads: number; archives: number }> {+ const [spool, archives] = await Promise.all([+ this.sessionUploader.spoolStatus().catch(() => ({ entries: 0 })),+ this.archive.pendingStatus().catch(() => ({ jobs: 0 })),+ ]);+ return { uploads: spool.entries, archives: archives.jobs };+ }++ /**+ * Before a sign-out: sends what is queued, for at most `budgetMs`. What is+ * not sent by then stays queued (signOut() keeps it for the account).+ */+ async flushPending(budgetMs: number): Promise<{ before: number; after: number }> {+ const count = async () => {+ const pending = await this.pendingUploads();+ return pending.uploads + pending.archives;+ };+ const before = await count();+ if (before > 0) {+ let timer: ReturnType<typeof setTimeout> | undefined;+ await Promise.race([+ Promise.all([this.sessionUploader.drainSpool().catch(() => undefined), this.archive.drainNow().catch(() => undefined)]),+ new Promise((resolvePromise) => {+ timer = setTimeout(resolvePromise, Math.max(0, Math.min(budgetMs, 30_000)));+ timer.unref?.();+ }),+ ]).finally(() => clearTimeout(timer));+ }+ return { before, after: before > 0 ? await count() : 0 };}/**diff --git a/apps/server/src/capture-protocol.ts b/apps/server/src/capture-protocol.tsindex 13c80d5e..bad69007 100644--- a/apps/server/src/capture-protocol.ts+++ b/apps/server/src/capture-protocol.ts@@ -37,6 +37,8 @@ export type CaptureCalls = {idle: [];diagnostics: [];filesUsedStatus: [];+ pendingUploads: [];+ flushPending: [budgetMs: number];};export type CaptureCallName = keyof CaptureCalls;@@ -205,5 +207,9 @@ export function invokeCapture(host: CaptureHost, call: CaptureCall): unknown {return host.diagnostics();case "filesUsedStatus":return host.filesUsedStatus();+ case "pendingUploads":+ return host.pendingUploads();+ case "flushPending":+ return host.flushPending(...call.args);}}diff --git a/apps/server/src/project-archive.test.ts b/apps/server/src/project-archive.test.tsindex fc16d3f0..f77aeda1 100644--- a/apps/server/src/project-archive.test.ts+++ b/apps/server/src/project-archive.test.ts@@ -80,7 +80,7 @@ describe("createProjectArchive", () => {expect(await readdir(join(stateDir, ARCHIVE_STATE_DIRECTORY))).toContain("sessions");});- test("sign-out aborts the part PUT in flight within a second and aborts the upload through the broker before the queue goes", async () => {+ test("sign-out stops the part PUT in flight within a second and keeps the queue for the account", async () => {const server = new FakeArchiveServer();server.partSize = 256;const gateway = broker(server);@@ -98,10 +98,10 @@ describe("createProjectArchive", () => {expect(slow.puts).toEqual([{ part: 2, bytes: 256, abortedAfterMs: expect.any(Number), completed: false }]);expect(slow.puts[0]!.abortedAfterMs!).toBeLessThan(1_000);const [archiveId] = [...server.archives.keys()];- expect(gateway.paths.filter((path) => path.startsWith(`POST archives/${archiveId}/`))).toEqual([`POST archives/${archiveId}/abort`]);- expect(server.archives.get(archiveId!)!.status).toBe("aborted");+ expect(gateway.paths.filter((path) => path.startsWith(`POST archives/${archiveId}/`))).toEqual([]);+ expect(server.archives.get(archiveId!)!.status).toBe("uploading");await archive.settled();- expect(await readdir(stateDir)).not.toContain(ARCHIVE_STATE_DIRECTORY);+ expect(await readdir(join(stateDir, ARCHIVE_STATE_DIRECTORY, "queue"))).toEqual([`${archiveId}.json`]);expect(slow.puts).toHaveLength(1);});diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 1f78ab7f..7f62c417 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -246,6 +246,8 @@ const autoCompactorsByServer = new WeakMap<ServerConfig, AutoCompactor>();*/const AUTO_COMPACT_HOLD_MS = 10_000;const AUTO_COMPACT_SEED_WAIT_MS = 5_000;+/** How long a sign-out waits for queued uploads to go (POST /omnirush/capture/flush); the rest stays queued for the account. */+const SIGN_OUT_FLUSH_MS = 10_000;const turnActivityByServer = new WeakMap<ServerConfig, TurnActivity>();/** Whether the agent works on this server's engine (turn-activity.ts), for an embedding app that asked (onTurnActivity). */@@ -1219,7 +1221,8 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {}void boxes.ensureAll();if (config.clearUploadQueue) {- // The user signed out: what the last account queued is deleted before anything else runs.+ // The user signed out: what the last account queued stays on disk for it (stamped with it, set aside+ // at the next start under another account, sent after its own next sign-in).await Promise.race([capture.signOut(), new Promise((resolvePromise) => setTimeout(resolvePromise, 10_000).unref?.())]);}try {@@ -4232,6 +4235,18 @@ function createRoutes(return jsonResponse({ running: mode === "worker" || mode === "local", mode, since: since.toISOString(), restarts });});+ // Before a sign-out: what is still queued for upload, and a short bounded send of it (the rest stays+ // queued for the account, sent after its next sign-in; never under another account).+ addRoute(routes, "GET", "/omnirush/capture/pending", "client", async () => {+ const pending = await captureServicesByServer.get(config)?.pendingUploads().catch(() => null);+ return jsonResponse(pending ?? { uploads: 0, archives: 0 });+ });++ addRoute(routes, "POST", "/omnirush/capture/flush", "client", async () => {+ const result = await captureServicesByServer.get(config)?.flushPending(SIGN_OUT_FLUSH_MS).catch(() => null);+ return jsonResponse(result ?? { before: 0, after: 0 });+ });+addRoute(routes, "GET", "/cloud-provider-sync/status", "client", async () => {return jsonResponse(cloudProviderSync.status());});diff --git a/apps/server/src/session-archive/PARITY.sha256 b/apps/server/src/session-archive/PARITY.sha256index 984cc0c1..3586b9e1 100644--- a/apps/server/src/session-archive/PARITY.sha256+++ b/apps/server/src/session-archive/PARITY.sha256@@ -7,3 +7,4 @@ c7f2e9a9c126ea9dfef714d36385e611e2190b0af8043cd52368a08bd0590af5 auto-compact.t1906cab8c93370abd1d024d3552fe919a0006af9ebef084ada7b64c290d1bc95 turn-writers.ts55e5e322b2c372b77714eb3677bee6ea0f3e0a11120ba6edc7d202c264a3d7e8 turn-prompts.tsca3bd7e89143925e6d35ae37757e736b9f8c20f6e5911a4f66433684fa185f1c code-files.ts+21459ba1d35b71195e98c7b5385277838248eb6bae316ad16fa9fcc93646d706 account-park.tsdiff --git a/apps/server/src/session-archive/account-park.ts b/apps/server/src/session-archive/account-park.tsnew file mode 100644index 00000000..6b190ac8--- /dev/null+++ b/apps/server/src/session-archive/account-park.ts@@ -0,0 +1,131 @@+/**+ * The project archive's state per account. A sign-out keeps what the+ * account has not sent yet (its queued archives and the records of its+ * chains). The next start under another account sets that state aside+ * under `accounts/<key>/`, where nothing sends it, and brings back that+ * account's own. A sign-in to the account that left it takes it back+ * as it was. State set aside for longer than PARKED_MAX_AGE_MS is deleted.+ *+ * `<archive dir>/account.json` ({v: 1, account_id}) names the account the+ * state in place belongs to. The hash cache, the key and the temp folder+ * are not per account and stay.+ */+import { createHash } from "node:crypto";+import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";+import { join } from "node:path";++export const ACCOUNT_FILE = "account.json";+export const PARKED_DIRECTORY = "accounts";+/** State another account left is kept this long for it, then deleted. */+export const PARKED_MAX_AGE_MS = 14 * 24 * 60 * 60_000;+/** The folders of the archive state that belong to one account. */+export const ACCOUNT_SCOPED_DIRECTORIES = ["sessions", "baselines", "queue", "pending", "touched", "files-used", "attachments"] as const;++/** The folder name of an account's state set aside (never the id itself). */+export function parkedKey(account: string): string {+ return createHash("sha256").update(account, "utf8").digest("hex").slice(0, 32);+}++async function readAccountFile(path: string): Promise<{ account: string | null; parkedAt: number | null }> {+ try {+ const value: unknown = JSON.parse(await readFile(path, "utf8"));+ if (!value || typeof value !== "object") return { account: null, parkedAt: null };+ const account = Reflect.get(value, "account_id");+ const parkedAt = Date.parse(String(Reflect.get(value, "parked_at") ?? ""));+ return { account: typeof account === "string" && account ? account : null, parkedAt: Number.isFinite(parkedAt) ? parkedAt : null };+ } catch {+ return { account: null, parkedAt: null };+ }+}++/** The account the archive state in place belongs to; null when none is recorded. */+export async function archiveOwner(dir: string): Promise<string | null> {+ return (await readAccountFile(join(dir, ACCOUNT_FILE))).account;+}++export async function setArchiveOwner(dir: string, account: string): Promise<void> {+ await mkdir(dir, { recursive: true, mode: 0o700 });+ const path = join(dir, ACCOUNT_FILE);+ const temporary = `${path}.${process.pid}.${Date.now()}.tmp`;+ await writeFile(temporary, JSON.stringify({ v: 1, account_id: account }), { mode: 0o600 });+ await rename(temporary, path);+}++/** Moves every entry of `from` into `to` (an entry moved replaces one of the same name there). */+async function moveEntries(from: string, to: string): Promise<number> {+ let names: string[];+ try {+ names = await readdir(from);+ } catch {+ return 0;+ }+ if (names.length === 0) return 0;+ await mkdir(to, { recursive: true, mode: 0o700 });+ for (const name of names) {+ const target = join(to, name);+ await rm(target, { recursive: true, force: true });+ await rename(join(from, name), target);+ }+ return names.length;+}++/** Sets the state in place aside for `account` (merged into what it already has aside); nothing is deleted. */+export async function parkAccountState(dir: string, account: string, now = Date.now()): Promise<number> {+ const parked = join(dir, PARKED_DIRECTORY, parkedKey(account));+ let moved = 0;+ for (const name of ACCOUNT_SCOPED_DIRECTORIES) moved += await moveEntries(join(dir, name), join(parked, name));+ await mkdir(parked, { recursive: true, mode: 0o700 });+ await writeFile(join(parked, ACCOUNT_FILE), JSON.stringify({ v: 1, account_id: account, parked_at: new Date(now).toISOString() }), { mode: 0o600 });+ await rm(join(dir, ACCOUNT_FILE), { force: true });+ return moved;+}++/** Brings back what `account` has aside (merged into the state in place); false when it has nothing. */+export async function restoreAccountState(dir: string, account: string): Promise<boolean> {+ const parked = join(dir, PARKED_DIRECTORY, parkedKey(account));+ if (!(await stat(parked).then(() => true, () => false))) return false;+ if ((await readAccountFile(join(parked, ACCOUNT_FILE))).account !== account) return false;+ for (const name of ACCOUNT_SCOPED_DIRECTORIES) await moveEntries(join(parked, name), join(dir, name));+ await rm(parked, { recursive: true, force: true });+ return true;+}++/** Deletes state set aside longer than `maxAgeMs` ago (or unreadable and as old); returns how many. */+export async function pruneParked(dir: string, now = Date.now(), maxAgeMs = PARKED_MAX_AGE_MS): Promise<number> {+ const root = join(dir, PARKED_DIRECTORY);+ let names: string[];+ try {+ names = await readdir(root);+ } catch {+ return 0;+ }+ let pruned = 0;+ for (const name of names) {+ const path = join(root, name);+ const { parkedAt } = await readAccountFile(join(path, ACCOUNT_FILE));+ const at = parkedAt ?? (await stat(path).then((info) => info.mtimeMs, () => now));+ if (now - at > maxAgeMs) {+ await rm(path, { recursive: true, force: true });+ pruned += 1;+ }+ }+ return pruned;+}++/**+ * At a start under `account`: another account's state in place is set+ * aside for it, `account`'s own comes back, and old state set aside goes.+ * `parked`: the account whose state was set aside (null: none).+ */+export async function claimArchiveState(dir: string, account: string, now = Date.now()): Promise<{ parked: string | null; restored: boolean; pruned: number }> {+ const owner = await archiveOwner(dir);+ let parked: string | null = null;+ if (owner && owner !== account) {+ await parkAccountState(dir, owner, now);+ parked = owner;+ }+ const restored = await restoreAccountState(dir, account);+ await setArchiveOwner(dir, account);+ const pruned = await pruneParked(dir, now);+ return { parked, restored, pruned };+}diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.tsindex b928084b..d35feb86 100644--- a/apps/server/src/session-archive/index.test.ts+++ b/apps/server/src/session-archive/index.test.ts@@ -594,7 +594,9 @@ describe("SessionArchiver", () => {await writeFile(join(root, "src/app.ts"), "export const app = 2;\n");expect((await first.captureDelta("ses_lost_record", root, 1)).status).toBe("queued");expect((await first.drain()).uploaded).toBe(2);- await first.signOut();+ await first.stop();+ // The record is lost (a reinstall, an older release's sign-out).+ await rm(join(state, ARCHIVE_STATE_DIRECTORY), { recursive: true, force: true });// Signed in again, the session resumed after its two turns: a delta on the server's chain, every file in it.const second = archiver(server, state);@@ -627,7 +629,8 @@ describe("SessionArchiver", () => {const first = archiver(server, state);expect((await first.captureBase("ses_reopened", root, 0)).status).toBe("queued");expect((await first.drain()).uploaded).toBe(1);- await first.signOut();+ await first.stop();+ await rm(join(state, ARCHIVE_STATE_DIRECTORY), { recursive: true, force: true });// Without the chain view (as before): the conflict stops the session.const second = archiver(server, state);expect((await second.captureBase("ses_reopened", root, 1)).status).toBe("queued");@@ -672,7 +675,35 @@ describe("SessionArchiver", () => {expect(await readFile(join(state, ARCHIVE_STATE_DIRECTORY, "account.json"), "utf8")).toContain("user-b");});- test("signOut aborts uploads in flight and removes every queued archive and record", async () => {+ test("signed out with archives waiting, another account signs in, then the first again: nothing goes under the other, all of it under its own", async () => {+ const server = new FakeArchiveServer();+ const { root } = await project();+ const state = await tempDir("state");+ let account: string | null = "user-a";+ const a1 = archiver(server, state, { accountId: async () => account });+ await a1.claimAccount();+ expect((await a1.captureBase("ses_switch_a", root)).status).toBe("queued");+ await writeFile(join(root, "src/app.ts"), "export const app = 21;\n");+ expect((await a1.captureDelta("ses_switch_a", root, 1)).status).toBe("queued");+ await a1.signOut();+ expect(server.objects()).toEqual([]);++ account = "user-b";+ const b = archiver(server, state, { accountId: async () => account });+ await b.claimAccount();+ expect(await b.drain()).toMatchObject({ uploaded: 0, pending: 0 });+ expect(server.objects()).toEqual([]);+ await b.stop();++ account = "user-a";+ const a2 = archiver(server, state, { accountId: async () => account });+ await a2.claimAccount();+ expect((await a2.drain()).uploaded).toBe(2);+ expect(server.objects().map((archive) => [archive.request.session_id, archive.request.sequence])).toEqual([["ses_switch_a", 0], ["ses_switch_a", 1]]);+ await a2.stop();+ });++ test("signOut stops uploads in flight and keeps every queued archive and record on disk for the account", async () => {const server = new FakeArchiveServer();const { root } = await project();const state = await tempDir("state");@@ -681,17 +712,35 @@ describe("SessionArchiver", () => {server.putHook = ({ partNumber }) => (partNumber === 2 ? "network" : undefined);await subject.drain();await subject.signOut();- expect(server.callPaths()).toContain(`POST archives/${base.status === "queued" ? base.archiveId : ""}/abort 200`);+ const archiveId = base.status === "queued" ? base.archiveId : "";+ // Nothing is aborted on the server: the upload is resumed after the next sign-in to this account.+ expect(server.callPaths().filter((path) => path.includes("/abort"))).toEqual([]);const archiveDir = join(state, ARCHIVE_STATE_DIRECTORY);- await expect(readdir(archiveDir)).rejects.toThrow();- // After sign-in the archiver starts clean.- subject.setAccessToken(server.token);+ expect(await readdir(join(archiveDir, "queue"))).toEqual([`${archiveId}.json`]);+ expect((await readdir(join(archiveDir, "sessions"))).length).toBe(1);+ // Signed in again (the server restarts; the failed part's backoff has passed): the queued base goes out, the chain goes on.server.putHook = null;- expect(await subject.captureDelta("ses_signed_out", root, 1)).toEqual({ status: "skipped", reason: "no_base" });- expect((await subject.captureBase("ses_signed_back_in", root)).status).toBe("queued");+ const again = archiver(server, state, { now: () => new Date(Date.now() + 2 * 60 * 60_000) });+ expect((await again.drain()).uploaded).toBe(1);+ await writeFile(join(root, "src/app.ts"), "export const app = 9;\n");+ expect(await again.captureDelta("ses_signed_out", root, 1)).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });+ await again.stop();+ });++ test("discardAll (archiving turned off on this device) aborts uploads in flight and removes every queued archive and record", async () => {+ const server = new FakeArchiveServer();+ const { root } = await project();+ const state = await tempDir("state");+ const subject = archiver(server, state);+ const base = await subject.captureBase("ses_discarded", root);+ server.putHook = ({ partNumber }) => (partNumber === 2 ? "network" : undefined);+ await subject.drain();+ await subject.discardAll();+ expect(server.callPaths()).toContain(`POST archives/${base.status === "queued" ? base.archiveId : ""}/abort 200`);+ await expect(readdir(join(state, ARCHIVE_STATE_DIRECTORY))).rejects.toThrow();});- test("signOut aborts the part PUT in flight within a second, aborts the upload with the credentials it still holds, and sends nothing more", async () => {+ test("signOut stops the part PUT in flight within a second and sends nothing more; the job stays queued", async () => {const server = new FakeArchiveServer();const { root } = await project();const state = await tempDir("state");@@ -709,11 +758,8 @@ describe("SessionArchiver", () => {expect(slow.puts[0]!.abortedAfterMs!).toBeLessThan(1_000);expect(signOutMs).toBeLessThan(1_000);expect(await draining).toMatchObject({ uploaded: 0 });- // The abort went out with a bearer the server still accepted, and nothing completed the archive.- expect(server.callPaths()).toContain(`POST archives/${archiveId}/abort 200`);- expect(server.callPaths().filter((path) => path.includes("/complete"))).toEqual([]);- expect(server.archives.get(archiveId)!.status).toBe("aborted");- await expect(readdir(join(state, ARCHIVE_STATE_DIRECTORY))).rejects.toThrow();+ expect(server.callPaths().filter((path) => path.includes("/complete") || path.includes("/abort"))).toEqual([]);+ expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "queue"))).toEqual([`${archiveId}.json`]);await Bun.sleep(50);expect(slow.puts).toHaveLength(1);expect(server.puts.filter((put) => put.partNumber > 1)).toEqual([]);diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex 5e674439..82b4f92a 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -32,6 +32,7 @@ import {type ProjectMarkerDetector,} from "./detect.js";import { hintGarbageCollection, readJsonFile, stateKey, writeChunksAtomic, writeJsonAtomic } from "./files.js";+import { claimArchiveState } from "./account-park.js";import {ArchiveHashCache,archiveLabel,@@ -668,16 +669,14 @@ export class SessionArchiver {}),]).finally(() => clearTimeout(timer));if (!id) return;- const path = join(this.dir, "account.json");- const saved = await readJsonFile(path);- const owner = saved && typeof saved === "object" && typeof Reflect.get(saved, "account_id") === "string" ? String(Reflect.get(saved, "account_id")) : null;- if (owner === id) return;- if (owner) {- this.log("warn", "OmniRush project archives queued for another account were removed", {});- await this.signOut();+ // Another account's queue and chain records are set aside for it, never sent under this one (account-park.ts).+ const claimed = await claimArchiveState(this.dir, id, this.now().getTime());+ if (claimed.parked) this.log("info", "OmniRush project archives of another account were set aside for it", {});+ if (claimed.restored) this.log("info", "OmniRush project archives this account left at its sign-out are back", {});+ if (claimed.parked || claimed.restored) {+ await this.draining?.catch(() => undefined);+ this.resetMemory();}- await mkdir(this.dir, { recursive: true, mode: 0o700 });- await writeJsonAtomic(path, { v: 1, account_id: id });}/** The device bearer changed (session uploader token rotation). */@@ -1130,28 +1129,24 @@ export class SessionArchiver {}/**- * The account signed out: abort in-flight uploads (best effort), drop every- * queued archive, baseline, hash cache and session record. Archives are the- * user's; nothing may be uploaded later under another account.+ * The account signed out: nothing more is captured or sent in this app+ * run. Uploads in flight stop here (their multipart uploads stay, resumed+ * later); the queue and the chain records stay on disk for that account+ * (account-park.ts): a sign-in to it sends them, a start under another+ * account sets them aside, and they go after PARKED_MAX_AGE_MS.*/async signOut(): Promise<void> {this.generation += 1;this.touched.clear();this.clearRetryTimer();this.drainController?.abort();- try {- // Without a state dir there is nothing to abort or delete; recovery would only create one.- if (await stat(this.dir).then(() => true, () => false)) {- await this.start();- const records = await this.listQueue();- const aborts = records.filter((record) => record.upload).map((record) => this.uploader.abort(record.archive_id, AbortSignal.timeout(SIGN_OUT_ABORT_TIMEOUT_MS)));- await Promise.race([Promise.all(aborts), new Promise((resolvePromise) => setTimeout(resolvePromise, SIGN_OUT_ABORT_TIMEOUT_MS).unref?.())]);- }- await this.draining?.catch(() => undefined);- await rm(this.dir, { recursive: true, force: true });- } catch (error) {- this.log("warn", "OmniRush archive sign-out cleanup failed", { error: errorSummary(error) });- }+ await this.draining?.catch(() => undefined);+ this.resetMemory();+ this.uploader.setAccessToken(null);+ }++ /** Forgets what this app run knows of the state on disk (it was set aside or brought back): the next call recovers it again. */+ private resetMemory(): void {this.key = null;this.disabled = null;this.policy = null;@@ -1165,10 +1160,36 @@ export class SessionArchiver {for (const sessionId of [...this.prescans.keys()]) this.dropPrescan(sessionId);this.finalsAccepted = false;this.finalsRefused = false;- this.uploader.setAccessToken(null);this.started = null;}+ /**+ * Archiving turned off on this device: abort in-flight uploads (best+ * effort), drop every queued archive, baseline, hash cache and session+ * record, and what other accounts left aside.+ */+ async discardAll(): Promise<void> {+ this.generation += 1;+ this.touched.clear();+ this.clearRetryTimer();+ this.drainController?.abort();+ try {+ // Without a state dir there is nothing to abort or delete; recovery would only create one.+ if (await stat(this.dir).then(() => true, () => false)) {+ await this.start();+ const records = await this.listQueue();+ const aborts = records.filter((record) => record.upload).map((record) => this.uploader.abort(record.archive_id, AbortSignal.timeout(SIGN_OUT_ABORT_TIMEOUT_MS)));+ await Promise.race([Promise.all(aborts), new Promise((resolvePromise) => setTimeout(resolvePromise, SIGN_OUT_ABORT_TIMEOUT_MS).unref?.())]);+ }+ await this.draining?.catch(() => undefined);+ await rm(this.dir, { recursive: true, force: true });+ } catch (error) {+ this.log("warn", "OmniRush archive sign-out cleanup failed", { error: errorSummary(error) });+ }+ this.resetMemory();+ this.uploader.setAccessToken(null);+ }+/*** App shutdown: stops the drain and the retry timer at once; queued* archives stay for the next start. With `finals`, a final archive of eachdiff --git a/apps/server/src/session-archive/lifecycle.ts b/apps/server/src/session-archive/lifecycle.tsindex 14c9f5dd..13a8c458 100644--- a/apps/server/src/session-archive/lifecycle.ts+++ b/apps/server/src/session-archive/lifecycle.ts@@ -22,7 +22,7 @@ import type { UsedToolCall } from "./files-used.js";export type ProjectArchiver = Pick<SessionArchiver,"captureBase" | "captureDelta" | "captureFinal" | "startFinalCandidates" | "recordTouched" | "forgetTouched" | "drain" | "signOut" | "stop"-> & Partial<Pick<SessionArchiver, "claimAccount" | "startManifest" | "hasStartManifest" | "captureState" | "recordAttachments" | "recordBinary" | "filesUsedToolCallEnded" | "filesUsedTurnStarted" | "filesUsedTurnEnded" | "filesUsedMaybe" | "filesUsedStatus">>;+> & Partial<Pick<SessionArchiver, "claimAccount" | "discardAll" | "pendingStatus" | "startManifest" | "hasStartManifest" | "captureState" | "recordAttachments" | "recordBinary" | "filesUsedToolCallEnded" | "filesUsedTurnStarted" | "filesUsedTurnEnded" | "filesUsedMaybe" | "filesUsedStatus">>;/** Engine reads for one session, resolving to the parsed JSON, or null when it cannot be read. */export type ArchiveEngineReads = {@@ -262,13 +262,14 @@ export class ProjectArchiveLifecycle {* unfinished, and checks the folders of recent sessions once for changes* made after their last archive (a final archive the last shutdown did not* get to, or edits while the app was closed); a queue another account left- * is removed first (claimAccount). Without an account the queue waits for- * it (a user's sign-out removes it, signOut()); with archiving turned off- * on this device it is removed.+ * is set aside for it first (claimAccount, account-park.ts). Without an+ * account the queue waits for it (a user's sign-out keeps it too); with+ * archiving turned off on this device it is removed.*/start(): void {if (!this.enabled) {- if (!this.accountMissing) this.track(this.archiver.signOut().catch((error: unknown) => this.warn("clear", error)));+ // Archiving turned off on this device: what is queued goes (a signed-out start keeps it for its account).+ if (!this.accountMissing) this.track((this.archiver.discardAll ?? this.archiver.signOut).call(this.archiver).catch((error: unknown) => this.warn("clear", error)));return;}this.track((async () => {@@ -534,10 +535,21 @@ export class ProjectArchiveLifecycle {this.kick();}+ /** Queued archives now (a sign-out first sends what it can). */+ async pendingStatus(): Promise<{ jobs: number }> {+ return this.archiver.pendingStatus ? this.archiver.pendingStatus() : { jobs: 0 };+ }++ /** Sends what is queued now (before a sign-out); resolves when that drain ends. */+ async drainNow(): Promise<void> {+ if (!this.active) return;+ await this.archiver.drain();+ }+/*** The account signed out or was invalidated: nothing is captured or- * uploaded any more, uploads in flight are aborted and every queued- * archive and session record is deleted.+ * uploaded any more in this app run and uploads in flight stop. The queue+ * and the session records stay on disk for that account (account-park.ts).*/async signOut(): Promise<void> {this.signedOut = true;diff --git a/apps/server/src/session-uploader.test.ts b/apps/server/src/session-uploader.test.tsindex 7cb9d451..924a463b 100644--- a/apps/server/src/session-uploader.test.ts+++ b/apps/server/src/session-uploader.test.ts@@ -1909,8 +1909,8 @@ describe("session upload deadline and spool drain", () => {await sessionUploader.stop();});- test("the default age limit is seven days", () => {- expect(MAX_SPOOL_AGE_MS).toBe(7 * 24 * 60 * 60_000);+ test("the default age limit is fourteen days (an account that signed out gets them after its next sign-in)", () => {+ expect(MAX_SPOOL_AGE_MS).toBe(14 * 24 * 60 * 60_000);});test("a non-retryable answer still drops the entry", async () => {@@ -1973,6 +1973,46 @@ describe("session upload deadline and spool drain", () => {await sessionUploader.stop();});+ test("a sign-out keeps the spool: unstamped entries take the signed-out account, another account never sends them, the same account does", async () => {+ const stateDir = await drainState();+ await spoolEntry(stateDir, 1, 512);+ await spoolEntry(stateDir, 2, 512, { account_id: "acct-b" });+ const leaving = new SessionUploader({ stateDir, retryBaseMs: 60_000, accountId: async () => "acct-a" });+ await leaving.keepSpoolFor("acct-a");+ expect(await leaving.spoolStatus()).toEqual({ entries: 2, bytes: 1_024 });+ await leaving.stop();+ const sentBy = (account: string) => {+ const sent: string[] = [];+ const uploader = new SessionUploader({+ stateDir,+ upload: async (sessionId) => {+ sent.push(sessionId);+ return Response.json({}, { status: 201 });+ },+ accountId: async () => account,+ retryBaseMs: 60_000,+ });+ return { uploader, sent };+ };+ const other = sentBy("acct-c");+ expect(await other.uploader.drainSpool()).toEqual({ delivered: 0, pending: 0 });+ expect(other.sent).toEqual([]);+ await other.uploader.stop();+ const same = sentBy("acct-a");+ expect(await same.uploader.drainSpool()).toEqual({ delivered: 1, pending: 0 });+ expect(same.sent).toEqual(["session-drain-1"]);+ await same.uploader.stop();+ // Never learned whose they were: stamped with an account nobody has.+ const unknown = await drainState();+ await spoolEntry(unknown, 3, 512);+ const signedOut = new SessionUploader({ stateDir: unknown, retryBaseMs: 60_000 });+ await signedOut.keepSpoolFor(null);+ await signedOut.stop();+ const anyone = new SessionUploader({ stateDir: unknown, upload: async () => Response.json({}, { status: 201 }), accountId: async () => "acct-a", retryBaseMs: 60_000 });+ expect(await anyone.drainSpool()).toEqual({ delivered: 0, pending: 0 });+ await anyone.stop();+ });+test("while the account cannot be learned only this process's own entries go out", async () => {const stateDir = await drainState();await spoolEntry(stateDir, 1, 512);diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.tsindex 236101c5..1d5af9a0 100644--- a/apps/server/src/session-uploader.ts+++ b/apps/server/src/session-uploader.ts@@ -151,8 +151,13 @@ const MAX_SPOOL_ENTRIES = 200;/** Trace uploads have a budget of their own: a large snapshot never pushes one out. */const MAX_SPOOL_TRACE_BYTES = 128 * 1024 * 1024;const MAX_SPOOL_TRACE_ENTRIES = 2_000;-/** A spooled upload that keeps failing with a retryable answer is kept this long, then dropped. */-export const MAX_SPOOL_AGE_MS = 7 * 24 * 60 * 60_000;+/**+ * A spooled upload that keeps failing with a retryable answer, or that waits+ * for its account to sign in again, is kept this long, then dropped.+ */+export const MAX_SPOOL_AGE_MS = 14 * 24 * 60 * 60_000;+/** Stamped at a sign-out on spooled uploads whose account was never learned: no account has it, so none sends them. */+export const SIGNED_OUT_UNKNOWN_ACCOUNT = "signed-out:unknown";/** Trace events reach the chat's journal on disk within this long. */const JOURNAL_FLUSH_MS = 250;/** A journal stops taking events past this size (its chat's meta lines still go in). */@@ -7996,6 +8001,33 @@ export class SessionUploader {* retired (a refused refresh) keeps its queue for when the account is* back, each upload stamped with its account.*/+ /**+ * The user signed out: uploads in flight stop, and what is spooled stays on+ * disk for that account. An entry without an account is stamped with+ * `account` (none known: SIGNED_OUT_UNKNOWN_ACCOUNT), so a later account's+ * drain never adopts it; that account's drain sends them after its next+ * sign-in, and MAX_SPOOL_AGE_MS drops the rest.+ */+ keepSpoolFor(account: string | null): Promise<void> {+ this.ownSpoolEntries.clear();+ if (this.retryTimer) {+ clearTimeout(this.retryTimer);+ this.retryTimer = null;+ }+ this.draining?.controller.abort();+ this.account.abort();+ this.account = new AbortController();+ this.accountKnown = null;+ this.accountLookup = null;+ this.retryFailures = 0;+ return this.spoolLocked(async () => {+ for (const entry of await this.listSpool()) {+ if (entry.account_id) continue;+ await this.writeSpoolMeta({ ...entry, account_id: account ?? SIGNED_OUT_UNKNOWN_ACCOUNT });+ }+ });+ }+clearSpool(): Promise<void> {this.ownSpoolEntries.clear();if (this.retryTimer) {