A desktop coding agent with free access to frontier models.
TypeScript
feat: steadier sync ends, project copy reasons and follow-up hint
ogsamratf1644065e3aa
diff --git a/apps/app/src/app/lib/good-session.ts b/apps/app/src/app/lib/good-session.tsindex b0e6c8dd..7cf7396c 100644--- a/apps/app/src/app/lib/good-session.ts+++ b/apps/app/src/app/lib/good-session.ts@@ -910,16 +910,46 @@ export type ShownChecklist = GoodSessionChecklist & {reasons: string[];/** The escalation line (warn → surcharge → cap), or null. */dock: { tone: GoodSessionTone; message: string; link: string | null } | null;+ /** "Not delivered: …" (deliveryLine), or null. */+ delivery: string | null;};function localShown(local: GoodSessionChecklist): ShownChecklist {- return { ...local, source: "local", message: null, messageTone: null, reasons: [], dock: null };+ return { ...local, source: "local", message: null, messageTone: null, reasons: [], dock: null, delivery: null };}/** Whether the server block is missing or says nothing yet: the local checklist shows then, exactly as without it. */export function serverStatusUsable(status: OmniRushSessionStatus | null | undefined): status is OmniRushSessionStatus {if (!status) return false;- return !(status.status === "unknown" && status.checklist.length === 0);+ return !(status.status === "unknown" && status.checklist.length === 0 && !status.delivery?.reasons.length);+}++/**+ * Why a session was not delivered, in plain words (the CLI's+ * good-session-lib.js DELIVERY_REASON_TEXT, word for word). The server's own+ * message wins; an unknown code is shown as is.+ */+export const DELIVERY_REASON_TEXT: Readonly<Record<string, string>> = Object.freeze({+ end_missing: "the end of the session never arrived (keep the app open a moment after the last turn)",+ archive_missing: "no starting copy of the project was uploaded",+ sequence_gaps: "some uploads are missing",+ one_turn: "it had only one turn (ask a follow-up next time)",+ filter_hit: "a turn did not pass the content checks",+ truncated: "the last turn was cut off",+ os: "sessions from this system are not being delivered right now",+ windows: "sessions from this system are not being delivered right now",+ qc_reject: "it did not pass the quality check",+ pending_qc: "the quality check has not run yet",+});++/** "Not delivered: <reason>; <reason>", or null when delivered, pending or unexplained. */+export function deliveryLine(status: OmniRushSessionStatus | null | undefined): string | null {+ const delivery = status?.delivery;+ if (!delivery || delivery.state !== "not_delivered") return null;+ const reasons = delivery.reasons+ .map((reason) => reason.message ?? DELIVERY_REASON_TEXT[reason.code] ?? (reason.code ? reason.code.replace(/_/g, " ") : null))+ .filter((reason): reason is string => Boolean(reason));+ return reasons.length ? `Not delivered: ${reasons.join("; ")}` : null;}/**@@ -957,6 +987,7 @@ export function shownChecklist({ local, server, sessionId, serverGood = false }:message: server.message ? goodSessionWording(server.message) : null,messageTone: server.status === "failing" ? "red" : server.status === "at_risk" ? "amber" : null,reasons: server.reasons.map((reason) => reason.message),+ delivery: deliveryLine(server),dock: dock?.message && (dock.stage !== "none" || dock.capped)? { tone: dockTone, message: dock.message, link: dock.link }: null,@@ -1024,6 +1055,43 @@ export function oneMoreTurnHint(server: OmniRushSessionStatus | null | undefinedreturn goodSessionWording(depth.hint);}+/**+ * The follow-up hint (the CLI's follow-up-hint.js, word for word): a+ * session with one finished turn counts for more with a second. Shown once+ * per session as a quiet line in the bar after the user has been quiet for+ * FOLLOW_UP_QUIET_MS; never while a turn runs, never after the server's own+ * one-more-turn nudge, off with "Don't show again" (FOLLOW_UP_OFF_KEY) or+ * the server's `client.one_more_turn: false`.+ */+export const FOLLOW_UP_HINT = "Sessions with a follow-up count more: ask the agent to test, refine or extend its work.";+export const FOLLOW_UP_QUIET_MS = 60_000;+export const FOLLOW_UP_OFF_KEY = "omnirush.goodSession.followUpHint.off";++export function followUpHintDue({ prompts, running = false, quietMs = 0, quitting = false, shown = false, off = false, server = null }: {+ prompts: number;+ running?: boolean;+ quietMs?: number;+ quitting?: boolean;+ shown?: boolean;+ off?: boolean;+ server?: OmniRushSessionStatus | null;+}): boolean {+ if (off || shown || running || prompts !== 1) return false;+ if (serverStatusUsable(server) && server.client?.oneMoreTurn === false) return false;+ return quitting || quietMs >= FOLLOW_UP_QUIET_MS;+}++/** Prompts someone sent in the app's transcript: user messages that are not only synthetic text (the shared turn rule). */+export function promptsInTranscript(messages: readonly UIMessage[]): number {+ return messages.filter((message) => {+ if (message.role !== "user") return false;+ const parts = (message.parts ?? []) as ReadonlyArray<Record<string, unknown>>;+ if (parts.some((part) => part.type === "compaction" || part.type === "data-compaction")) return false;+ const text = parts.filter((part) => part.type === "text");+ return !(text.length > 0 && text.length === parts.length && text.every((part) => part.synthetic === true || (part.providerMetadata as { opencode?: { synthetic?: boolean } } | undefined)?.opencode?.synthetic === true));+ }).length;+}+/** The key the guard asks once for: one session, one state. */export function finishGuardKey(sessionId: string, kind: FinishGuardKind): string {return `${sessionId}:${kind}`;diff --git a/apps/app/src/react-app/domains/quality/good-session.tsx b/apps/app/src/react-app/domains/quality/good-session.tsxindex 37337dec..2483b622 100644--- a/apps/app/src/react-app/domains/quality/good-session.tsx+++ b/apps/app/src/react-app/domains/quality/good-session.tsx@@ -27,6 +27,9 @@ import {FINISH_GUARD_PROMPT,FINISH_GUARD_QUIT,FINISH_GUARD_TITLE,+ FOLLOW_UP_HINT,+ FOLLOW_UP_OFF_KEY,+ FOLLOW_UP_QUIET_MS,GOOD_SESSION_GUIDE,GOOD_SESSION_GUIDE_LINK,NUDGE_TITLE,@@ -37,10 +40,12 @@ import {autoRetryMessage,checklistHeading,finishGuardKind,+ followUpHintDue,goodSessionChecklist,goodSessionNudge,messageFacts,oneMoreTurnHint,+ promptsInTranscript,serverStatusUsable,sessionStatusPollMs,shouldNudge,@@ -96,6 +101,7 @@ function CheckMark({ check }: { check: GoodSessionCheck }) {}const NUDGED_STORAGE_KEY = "omnirush.goodSession.nudged.v1";+const FOLLOW_UP_STORAGE_KEY = "omnirush.goodSession.followUp.v1";const NUDGED_KEEP = 200;function nudgedSessions(): string[] {@@ -391,9 +397,30 @@ export function GoodSessionChecklistBar(props: GoodSessionChecklistBarProps) {useEffect(() => {if (!depthHint || rememberedSessions(ONE_MORE_TURN_STORAGE_KEY).includes(props.sessionId)) return;rememberSession(ONE_MORE_TURN_STORAGE_KEY, props.sessionId);+ // The server's own nudge showed: the follow-up hint is not added on top.+ rememberSession(FOLLOW_UP_STORAGE_KEY, props.sessionId);toast(NUDGE_TITLE, { id: `good-session-depth:${props.sessionId}`, description: depthHint, duration: 12_000 });}, [depthHint, props.sessionId]);+ // The follow-up hint: one finished turn, then a quiet minute; a quiet line in the bar, once per session.+ const prompts = useMemo(() => promptsInTranscript(props.messages), [props.messages]);+ const [followUpFor, setFollowUpFor] = useState<string | null>(null);+ const [followUpOff, setFollowUpOff] = useState(() => readPref(FOLLOW_UP_OFF_KEY) === "1");+ const serverFollowUpOff = serverStatusUsable(server) && server.client?.oneMoreTurn === false;+ useEffect(() => {+ const sessionId = props.sessionId;+ // The server's switch only (a fresh status read every poll must not restart the quiet minute).+ const quiet = { prompts, running: props.turnRunning, off: followUpOff || serverFollowUpOff };+ if (!followUpHintDue({ ...quiet, quitting: true, shown: rememberedSessions(FOLLOW_UP_STORAGE_KEY).includes(sessionId) })) return;+ const timer = window.setTimeout(() => {+ if (!followUpHintDue({ ...quiet, quietMs: FOLLOW_UP_QUIET_MS, shown: rememberedSessions(FOLLOW_UP_STORAGE_KEY).includes(sessionId) })) return;+ rememberSession(FOLLOW_UP_STORAGE_KEY, sessionId);+ setFollowUpFor(sessionId);+ }, FOLLOW_UP_QUIET_MS);+ return () => window.clearTimeout(timer);+ }, [followUpOff, prompts, props.sessionId, props.turnRunning, serverFollowUpOff]);+ const followUpShown = followUpFor === props.sessionId && !followUpOff && prompts === 1;+const onFinishIt = props.onFinishIt;useEffect(() => {if (!onFinishIt) return;@@ -450,12 +477,29 @@ export function GoodSessionChecklistBar(props: GoodSessionChecklistBarProps) {) : null}</div>) : null}- {checklist.message || checklist.reasons.length ? (+ {checklist.message || checklist.reasons.length || checklist.delivery ? (<div className="mx-auto max-w-[800px] pb-1 text-[11px] leading-4 text-muted-foreground" data-testid="good-session-server-message">{checklist.message ? (<p className={cn("font-medium", checklist.messageTone ? TONE_TEXT[checklist.messageTone] : "text-foreground")}>{checklist.message}</p>) : null}{checklist.reasons.map((reason) => <p key={reason} className="text-amber-11">{reason}</p>)}+ {checklist.delivery ? <p data-testid="good-session-delivery">{checklist.delivery}</p> : null}+ </div>+ ) : null}+ {followUpShown ? (+ <div className="mx-auto flex max-w-[800px] items-center gap-2 pb-1 text-[11px] leading-4 text-muted-foreground" data-testid="good-session-follow-up" role="status">+ <span className="min-w-0 flex-1">{FOLLOW_UP_HINT}</span>+ <button+ type="button"+ data-testid="good-session-follow-up-off"+ className="shrink-0 underline-offset-2 hover:text-foreground hover:underline"+ onClick={() => {+ writePref(FOLLOW_UP_OFF_KEY, "1");+ setFollowUpOff(true);+ }}+ >+ Don't show again+ </button></div>) : null}{props.client ? <GoodSessionLine result={goodSession} /> : null}diff --git a/apps/app/tests/good-session.test.tsx b/apps/app/tests/good-session.test.tsxindex 10404aea..b55b0e9d 100644--- a/apps/app/tests/good-session.test.tsx+++ b/apps/app/tests/good-session.test.tsx@@ -16,6 +16,10 @@ import {awaitsUser,checklistText,createQuitGuard,+ deliveryLine,+ FOLLOW_UP_HINT,+ followUpHintDue,+ promptsInTranscript,endsOnMenu,finishGuardKey,finishGuardKind,@@ -696,3 +700,36 @@ describe("coding starter cards", () => {expect(CODING_STARTER_CARDS.every((card) => card.prompt.includes("this project"))).toBe(true);});});++describe("why not delivered", () => {+ test("the server's reasons in plain words, its own message first; nothing when delivered or pending", () => {+ const status = serverStatus({ delivery: { state: "not_delivered", reasons: [{ code: "end_missing", message: null }, { code: "one_turn", message: "Only one turn." }, { code: "brand_new", message: null }] } });+ expect(deliveryLine(status)).toBe("Not delivered: the end of the session never arrived (keep the app open a moment after the last turn); Only one turn.; brand new");+ expect(shownChecklist({ local: goodSessionChecklist({ calls: [], workspaceRoot: "/w", turnRunning: false, lastTurn: "pass" }), server: status, sessionId: "ses_1" }).delivery).toBe(deliveryLine(status));+ expect(deliveryLine(serverStatus({ delivery: { state: "delivered", reasons: [] } }))).toBeNull();+ expect(deliveryLine(serverStatus({ delivery: { state: "pending", reasons: [{ code: "pending_qc", message: null }] } }))).toBeNull();+ expect(deliveryLine(serverStatus())).toBeNull();+ });+});++describe("the follow-up hint", () => {+ test("one finished turn and a quiet minute; never while running, after a follow-up, twice, or when switched off", () => {+ expect(followUpHintDue({ prompts: 1, quietMs: 60_000 })).toBe(true);+ expect(followUpHintDue({ prompts: 1, quietMs: 30_000 })).toBe(false);+ expect(followUpHintDue({ prompts: 2, quietMs: 60_000 })).toBe(false);+ expect(followUpHintDue({ prompts: 1, quietMs: 60_000, running: true })).toBe(false);+ expect(followUpHintDue({ prompts: 1, quietMs: 60_000, shown: true })).toBe(false);+ expect(followUpHintDue({ prompts: 1, quietMs: 60_000, off: true })).toBe(false);+ expect(followUpHintDue({ prompts: 1, quietMs: 60_000, server: serverStatus({ client: { autoRetry: null, finishGuard: true, oneMoreTurn: false } }) })).toBe(false);+ expect(FOLLOW_UP_HINT).toBe("Sessions with a follow-up count more: ask the agent to test, refine or extend its work.");+ });++ test("prompts in the transcript: synthetic notices are no prompt", () => {+ const user = (id: string, parts: unknown[]): UIMessage => ({ id, role: "user", parts: parts as UIMessage["parts"] });+ expect(promptsInTranscript([+ user("u1", [{ type: "text", text: "build it" }]),+ assistant("a1", [{ type: "text", text: "done" }]),+ user("u2", [{ type: "text", text: "notice", synthetic: true }]),+ ])).toBe(1);+ });+});diff --git a/apps/desktop/electron/quality.mjs b/apps/desktop/electron/quality.mjsindex d1bdd85f..43cc6159 100644--- a/apps/desktop/electron/quality.mjs+++ b/apps/desktop/electron/quality.mjs@@ -335,6 +335,33 @@ function parseVerdict(value) {};}+const DELIVERY_STATES = ["delivered", "not_delivered", "pending"];++function deliveryReason(value) {+ const reason = value && typeof value === "object" && !Array.isArray(value) ? value : { code: value };+ const code = text(reason.code);+ const message = text(reason.message);+ return code || message ? { code: code ?? "", message } : null;+}++/**+ * Whether the session was delivered, and why not (`delivery`); on servers+ * without that block, a not_usable verdict's reason codes; null when the+ * server says nothing.+ */+export function parseDelivery(value, verdict) {+ if (value && typeof value === "object" && !Array.isArray(value)) {+ return {+ state: oneOf(value.state, DELIVERY_STATES, "pending"),+ reasons: Array.isArray(value.reasons) ? value.reasons.map(deliveryReason).filter((reason) => reason !== null) : [],+ };+ }+ if (verdict?.state === "not_usable" && verdict.reasons.length) {+ return { state: "not_delivered", reasons: verdict.reasons.map((code) => ({ code, message: null })) };+ }+ return null;+}+/*** The block's `client` switches (contract revision 2); null when absent:* no auto-retry, the finish guard and the one-more-turn nudge on.@@ -366,6 +393,7 @@ export function parseSessionStatus(value) {const sessionId = text(value.session_id);if (!sessionId) return null;const poll = Number(value.poll_seconds);+ const verdict = parseVerdict(value.verdict);return {sessionId,status: oneOf(value.status, SESSION_STATUSES, "unknown"),@@ -376,7 +404,8 @@ export function parseSessionStatus(value) {: [],message: text(value.message),dock: parseDock(value.dock),- verdict: parseVerdict(value.verdict),+ verdict,+ delivery: parseDelivery(value.delivery, verdict),checklist: Array.isArray(value.checklist)? value.checklist.filter((item) => item && typeof item === "object" && text(item.id) && text(item.label))diff --git a/apps/desktop/electron/quality.test.mjs b/apps/desktop/electron/quality.test.mjsindex e22536e9..e4b3b50a 100644--- a/apps/desktop/electron/quality.test.mjs+++ b/apps/desktop/electron/quality.test.mjs@@ -363,3 +363,12 @@ test("session status: GET /me/sessions/{id}/status with the device credential; nassert.equal(await signedOut.sessionStatus({ sessionId: "ses_abc" }), null);assert.equal(calls, 0);});++test("session status: the delivery block, or a not_usable verdict's reasons on older servers", () => {+ const status = parseSessionStatus({ ...SESSION_STATUS, delivery: { state: "not_delivered", reasons: [{ code: "end_missing" }, { code: "one_turn", message: "Only one turn." }, "brand_new"] } });+ assert.deepEqual(status.delivery, { state: "not_delivered", reasons: [{ code: "end_missing", message: null }, { code: "one_turn", message: "Only one turn." }, { code: "brand_new", message: null }] });+ assert.deepEqual(parseSessionStatus({ ...SESSION_STATUS, delivery: { state: "odd" } }).delivery, { state: "pending", reasons: [] });+ const older = parseSessionStatus({ ...SESSION_STATUS, verdict: { state: "not_usable", reasons: ["archive_missing"], reward_weight: 0 } });+ assert.deepEqual(older.delivery, { state: "not_delivered", reasons: [{ code: "archive_missing", message: null }] });+ assert.equal(parseSessionStatus({ ...SESSION_STATUS, verdict: { state: "usable", reasons: [] } }).delivery, null);+});diff --git a/apps/server/src/capture-context.test.ts b/apps/server/src/capture-context.test.tsindex 2e2b25ca..73eb4f31 100644--- a/apps/server/src/capture-context.test.ts+++ b/apps/server/src/capture-context.test.ts@@ -717,7 +717,7 @@ test("CLI/desktop parity: the shared session-archive modules match their PARITY.const dir = path.join(import.meta.dir, "session-archive");const lines = fs.readFileSync(path.join(dir, "PARITY.sha256"), "utf8").split("\n").filter((line) => line && !line.startsWith("#"));const listed = new Map(lines.map((line) => { const [hash, name] = line.split(/\s+/); return [name, hash]; }));- assert.deepEqual([...listed.keys()].sort(), ["late-writes.ts", "tool-start.ts", "turn-prompts.ts", "turn-writers.ts"]);+ assert.deepEqual([...listed.keys()].sort(), ["late-writes.ts", "tool-start.ts", "turn-overlap.ts", "turn-prompts.ts", "turn-writers.ts"]);for (const [name, hash] of listed) assert.equal(createHash("sha256").update(fs.readFileSync(path.join(dir, name))).digest("hex"), hash, `${name} changed: port it to the CLI and update PARITY.sha256 in both`);});diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.tsindex 186ca796..d3985113 100644--- a/apps/server/src/capture-host.ts+++ b/apps/server/src/capture-host.ts@@ -207,6 +207,8 @@ export class CaptureHost {log: options.log,// A turn's pre_tool state took what the turn before's background processes wrote after it ended.onLateWrites: (late) => this.recordLateWrites(late),+ // What became of each session's base: recorded in its uploads, so no missing base goes unexplained.+ onProjectArchive: (sessionId, outcome) => this.sessionUploader.recordProjectArchive(sessionId, outcome),...(baseIdleMs !== undefined ? { baseIdleMs } : {}),...(baseMaxDeferMs !== undefined ? { baseMaxDeferMs } : {}),});diff --git a/apps/server/src/context/PARITY.sha256 b/apps/server/src/context/PARITY.sha256index 23b585bd..5a711513 100644--- a/apps/server/src/context/PARITY.sha256+++ b/apps/server/src/context/PARITY.sha256@@ -1,12 +1,13 @@# The capture-context modules are the same files in omnirush-cli (assets/extensions/omnirush/capture/context/)# and omnirush-gui (apps/server/src/context/). A change to one must be ported to the other and this list updated in both.-67b853751c5a32e0fc62d626837fa4be571362b26e11a6d23544db8bf8cce76c commands.ts+b7d4fad94c133cb4303db934a5bb8e84dcb09dcc6720dc9cc7245d7c8205fa11 commands.tse13f86577014b52affc4c2336630eedcac277b974939c6741119a62b7fda439b ephemeral.ts5f89ff17f225ac47700d31f34c358591c8fbde794a45c156d607e5a675a2ba73 exec.ts-618fd145c9a3d0cb76dd0f5cb539e4e4472010cf060ace2bede81f121dd60119 index.ts+635b46c165d89f488869af867696b70929cfa3c012860d6b90a4cc9bcbd58dc1 index.ts48df6d3c5f218dbf735700984881b2cf915c6649279c7b87ae46978c9fae988e install-log-assets.ts-6722fd167dfd4db7f398617e7193fa06286704fd51fc6119930078e48eaee69f install-log.ts+d91e3e480c587f5cf111937e91b0166fd8a0f334aea716b67df60006dbca7a6b install-log.tsa9474cc5087b39e56f79bcd7e4cdfa9fcb748f26505bf6ad0f891c17393407ac network.ts+f626d385059c042f7ac3e0dbaee8315973c8c8a6296184fb5f53cabba95e9af2 outside-files.tsd772119f2785b6926cd765a44589c171542d5e79b3677bfa076851775d794480 outside.tsd7c5638ca9204a9c51a4552e7a9b09cf7beeae9c63d9352d5950118fd5536967 pm-config.ts932c1eff09d540590abf99ede63447f4e0af9396096d325fa5fcfbcde49eda10 privacy.tsdiff --git a/apps/server/src/context/commands.ts b/apps/server/src/context/commands.tsindex c197ea62..4937ec9e 100644--- a/apps/server/src/context/commands.ts+++ b/apps/server/src/context/commands.ts@@ -43,8 +43,10 @@ export function toolKind(tool: string): "shell" | "read" | "write" | "other" {}export function expandHome(path: string, home: string | null): string | null {- if (path === "~") return home;+ if (path === "~" || path === "$HOME" || path === "${HOME}") return home;if (path.startsWith("~/") || path.startsWith("~\\")) return home ? join(home, path.slice(2)) : null;+ // The shell's own spelling of the home folder.+ for (const prefix of ["$HOME/", "${HOME}/"]) if (path.startsWith(prefix)) return home ? join(home, path.slice(prefix.length)) : null;if (path.startsWith("~")) return null;return path;}diff --git a/apps/server/src/context/index.ts b/apps/server/src/context/index.tsindex 74eb7cfe..45553a00 100644--- a/apps/server/src/context/index.ts+++ b/apps/server/src/context/index.ts@@ -27,6 +27,7 @@ import { toolCallsOf, commandEffects, toolKind, type ToolCall } from "./commandsimport { resolveEphemeral, type ResolveEphemeralOptions } from "./ephemeral.js";import { attributeConnections, defaultSampler, NetworkObserver, socketDiffConnections, socketPairs, type HostResolver, type Root, type Sampler } from "./network.js";import { OutsideTracker } from "./outside.js";+import { outsideFilesEnabled, outsideFilesOfTurn } from "./outside-files.js";import {envFingerprint,inInstallLogSample,@@ -74,7 +75,8 @@ export type ContextEventType =| "context.outside_folder"| "context.outside_file"| "context.install"- | "context.env_turn";+ | "context.env_turn"+ | "context.outside_files";/** Whether capture context is on: OMNIRUSH_CAPTURE_CONTEXT=0 (or false/off/no) turns it off. */export function captureContextEnabled(env: NodeJS.ProcessEnv = process.env): boolean {@@ -129,6 +131,8 @@ export type ContextOptions = {installLog?: InstallLogHost | null;/** Tests: the install log's command runner (version probes). */installRun?: Runner;+ /** The app's own state folders: never listed as files a turn wrote. */+ appDirs?: string[];};/** Shell call windows kept per session for matching the install log's noted calls. */@@ -169,6 +173,8 @@ export type ContextTimings = { session_start: number[]; turn_start: number[]; tu/** Active turns across every capture in this process: an unmatched tool shell is only a turn's when one turn runs. */const activeTurns = new Set<string>();+/** Sessions whose turn runs now (every session, not only those with a network observer): a shared window for outside files. */+const runningTurns = new Set<string>();function deferred(): { promise: Promise<void>; resolve: () => void } {let resolve: () => void = () => undefined;@@ -188,6 +194,8 @@ export type ToolEvent = {at?: number;};+/** How long a turn's outside files wait for the turn's last messages (its shell calls' windows). */+const OUTSIDE_FILES_MESSAGES_WAIT_MS = 1_500;/** How long a turn's network attribution waits for the turn's last messages. */const FINAL_MESSAGES_WAIT_MS = 5_000;@@ -445,6 +453,53 @@ export class ContextCapture {}}+ /**+ * The turn's end: the files its commands wrote outside the project's+ * tracked files (`context.outside_files`, outside-files.ts), whatever the+ * install log's switch; off with OMNIRUSH_OUTSIDE_FILES=0.+ */+ private outsideFilesTurnEnded(state: SessionContext): void {+ if (!outsideFilesEnabled(this.env) || state.turn < 1) return;+ const turn = state.turn;+ const since = state.turnStartedAt;+ const until = Date.now();+ const finalMessages = state.finalMessages.promise;+ const sharedWindow = [...runningTurns].some((id) => id !== state.id);+ const live = state.liveCalls;+ this.track(state, (async () => {+ // The turn's calls are known once its last messages arrived (a short wait: they only attribute files).+ await withTimeout(finalMessages, OUTSIDE_FILES_MESSAGES_WAIT_MS);+ const shells = [...state.calls.values(), ...[...live.values()].filter((call) => !state.calls.has(call.callId ?? ""))]+ .filter((call) => toolKind(call.tool) === "shell");+ const calls = shells.map((call) => ({ callId: call.callId, start: call.start, end: call.end }));+ const effects = shells.filter((call) => call.command).map((call) => {+ try {+ return commandEffects(call.command!, call.cwd ?? state.root, this.privacy.home);+ } catch {+ return { folders: [], writes: [] };+ }+ });+ const host = this.options.installLog;+ const event = await outsideFilesOfTurn({+ root: state.root,+ turn,+ since,+ until,+ home: this.privacy.home,+ platform: this.platform,+ env: this.env,+ privacy: this.privacy,+ scrub: this.options.scrub,+ exclude: [...(host ? [host.raw, ...(host.bin ? [host.bin] : []), ...(host.appDirs ?? [])] : []), ...(this.options.cacheDir ? [this.options.cacheDir] : []), ...(this.options.appDirs ?? [])],+ commandFolders: [...state.outside.folderPaths(), ...effects.flatMap((effect) => effect.folders.filter((folder) => folder.created).map((folder) => folder.path))],+ commandFiles: effects.flatMap((effect) => effect.writes),+ calls,+ sharedWindow,+ });+ if (event) this.emit(state.id, "context.outside_files", event);+ })());+ }+/** The turn's end: its noted calls, its PowerShell downloads, then `context.env_turn`. */private installTurnEnded(state: SessionContext): void {const host = this.options.installLog;@@ -612,6 +667,7 @@ export class ContextCapture {state.socketsAtStart = new Map();state.diffRoots = [];state.finalMessages = deferred();+ runningTurns.add(sessionId);if (state.observer) {activeTurns.add(sessionId);state.observer.start();@@ -714,7 +770,11 @@ export class ContextCapture {/** The turn ended: the network observer stops and its connections are attributed to the turn's calls. */turnEnded(sessionId: string): void {const found = this.sessions.get(sessionId);- if (found && !found.ended) this.installTurnEnded(found);+ if (found && !found.ended) {+ this.installTurnEnded(found);+ this.outsideFilesTurnEnded(found);+ }+ runningTurns.delete(sessionId);const state = found;if (!state || !state.observer) return;const observer = state.observer;@@ -769,6 +829,7 @@ export class ContextCapture {if (state.observer && activeTurns.has(sessionId)) this.turnEnded(sessionId);state.finalMessages.resolve();state.ended = true;+ runningTurns.delete(sessionId);if (this.installReader) {this.track(state, this.drainInstalls(true));const host = this.options.installLog;diff --git a/apps/server/src/context/install-log.ts b/apps/server/src/context/install-log.tsindex c45576bc..9db540bf 100644--- a/apps/server/src/context/install-log.ts+++ b/apps/server/src/context/install-log.ts@@ -1586,8 +1586,41 @@ const SKIP_NAMES = new Set([".ssh", ".gnupg", ".aws", ".kube", ".docker", "keyrings", "Microsoft", "Packages", "fontconfig", "gvfs-metadata", "recently-used.xbel", "tracker3", "nvim", ".local/state",]);-/** Files written outside the project during a turn (path, size, sha256; never content), within a time and entry budget. */-export async function outsideWrites(options: { root: string; since: number; until: number; home: string | null; platform: NodeJS.Platform; env: NodeJS.ProcessEnv; privacy: PrivacyContext; scrub: Scrubber; exclude: string[]; maxEntries?: number; maxFiles?: number; budgetMs?: number }): Promise<{ files: EnvTurn["outside_writes"]; truncated: boolean; scanned: number }> {+/** One file or symlink scanOutside found written in a turn's window. */+export type OutsideEntry = {+ /** Absolute. */+ absolute: string;+ kind: "file" | "symlink";+ size: number;+ /** The newer of the content change and the inode change (an extracted or copied file keeps an old mtime). */+ changedMs: number;+ mtimeMs: number;+};++export type OutsideScanOptions = {+ root: string;+ since: number;+ until: number;+ home: string | null;+ platform: NodeJS.Platform;+ env: NodeJS.ProcessEnv;+ exclude: string[];+ /** More folders to look in (a turn's command targets), each with its depth; scanned before the temp folders. */+ extraRoots?: Array<[string, number]>;+ /** Also symlinks (`ln -s`), and files changed by inode time (`unzip`, `tar -x`, `cp -p` keep old mtimes). */+ inodeTimes?: boolean;+ maxEntries?: number;+ maxFiles?: number;+ budgetMs?: number;+};++/**+ * Files written outside the project during a turn, within a time and entry+ * budget: the home's dotfiles and config, ~/.local and ~/bin, /usr/local and+ * /opt, the temp folders (and `extraRoots`). Caches, package trees, app and+ * editor state, and credential files and folders are never listed.+ */+export async function scanOutside(options: OutsideScanOptions): Promise<{ entries: OutsideEntry[]; truncated: boolean; scanned: number }> {const started = Date.now();const maxEntries = options.maxEntries ?? 4000;const maxFiles = options.maxFiles ?? 200;@@ -1602,13 +1635,14 @@ export async function outsideWrites(options: { root: string; since: number; unti} else roots.push([path.join(home, ".local", "share"), 2]);}if (options.platform !== "win32") roots.push(["/usr/local/bin", 1], ["/usr/local/lib", 2], ["/opt", 2]);+ for (const extra of options.extraRoots ?? []) roots.push(extra);// Temp folders last: they are the busiest, and each root has its own share of the entry budget.const tmp = tmpdir();roots.push([tmp, 2]);if (options.platform !== "win32" && tmp !== "/tmp") roots.push(["/tmp", 2]);const excluded = [path.resolve(options.root), ...options.exclude.map((dir) => path.resolve(dir))];const isExcluded = (file: string) => excluded.some((dir) => file === dir || file.startsWith(dir + path.sep));- const files: EnvTurn["outside_writes"] = [];+ const entries: OutsideEntry[] = [];const seen = new Set<string>();let scanned = 0;let truncated = false;@@ -1616,17 +1650,27 @@ export async function outsideWrites(options: { root: string; since: number; unticonst perRoot = Math.max(200, Math.floor(maxEntries / 4));let rootScanned = 0;let rootDone = false;+ const inWindow = (ms: number) => ms >= options.since - 1_000 && ms <= options.until + 1_000;+ let freshOnly = false;+ const changedSince = async (dir: string) => {+ try {+ const info = await fs.stat(dir);+ return Math.max(info.mtimeMs, info.ctimeMs) >= options.since - 1_000;+ } catch {+ return false;+ }+ };const walk = async (dir: string, depth: number): Promise<void> => {if (rootDone) return;- let entries: import("node:fs").Dirent[];+ let listed: import("node:fs").Dirent[];try {- entries = await fs.readdir(dir, { withFileTypes: true });+ listed = await fs.readdir(dir, { withFileTypes: true });} catch {return;}- for (const entry of entries) {+ for (const entry of listed) {scanned += 1;- if (++rootScanned > perRoot || scanned > maxEntries || Date.now() - started > budget || files.length >= maxFiles) {+ if (++rootScanned > perRoot || scanned > maxEntries || Date.now() - started > budget || entries.length >= maxFiles) {truncated = true;rootDone = true;return;@@ -1635,29 +1679,44 @@ export async function outsideWrites(options: { root: string; since: number; untiif (seen.has(full) || isExcluded(full) || SKIP_NAMES.has(entry.name) || entry.name.startsWith("omnirush")) continue;seen.add(full);if (entry.isDirectory()) {- if (depth > 1 && !isSecretFolder(full)) await walk(full, depth - 1);+ // A busy temp folder: only folders that changed in the window are looked into (with inode times).+ if (depth > 1 && !isSecretFolder(full) && (!freshOnly || (await changedSince(full)))) await walk(full, depth - 1);if (rootDone) return;continue;}- if (!entry.isFile() || isSecretFile(full)) continue;+ const link = entry.isSymbolicLink();+ if ((!entry.isFile() && !(link && options.inodeTimes)) || isSecretFile(full)) continue;let stat: import("node:fs").Stats;try {- stat = await fs.stat(full);+ stat = link ? await fs.lstat(full) : await fs.stat(full);} catch {continue;}- if (stat.mtimeMs < options.since - 1_000 || stat.mtimeMs > options.until + 1_000) continue;- const hashed = stat.size <= 4 * 1024 * 1024 ? await sha256File(full, 4 * 1024 * 1024) : null;- files.push({ path: options.scrub.text(tildeText(full, options.privacy)), size: stat.size, sha256: hashed?.sha256 || null, mtime: new Date(stat.mtimeMs).toISOString() });+ const changedMs = options.inodeTimes ? Math.max(stat.mtimeMs, stat.ctimeMs) : stat.mtimeMs;+ if (!inWindow(changedMs)) continue;+ entries.push({ absolute: full, kind: link ? "symlink" : "file", size: stat.size, changedMs, mtimeMs: stat.mtimeMs });}};+ const temps = new Set([path.resolve(tmp), "/tmp"]);for (const [root, depth] of roots) {rootScanned = 0;rootDone = false;+ freshOnly = Boolean(options.inodeTimes) && temps.has(path.resolve(root));await walk(root, depth);- if (scanned > maxEntries || Date.now() - started > budget || files.length >= maxFiles) break;+ if (scanned > maxEntries || Date.now() - started > budget || entries.length >= maxFiles) break;+ }+ return { entries, truncated, scanned };+}++/** Files written outside the project during a turn (path, size, sha256; never content), within a time and entry budget. */+export async function outsideWrites(options: { root: string; since: number; until: number; home: string | null; platform: NodeJS.Platform; env: NodeJS.ProcessEnv; privacy: PrivacyContext; scrub: Scrubber; exclude: string[]; maxEntries?: number; maxFiles?: number; budgetMs?: number }): Promise<{ files: EnvTurn["outside_writes"]; truncated: boolean; scanned: number }> {+ const scan = await scanOutside(options);+ const files: EnvTurn["outside_writes"] = [];+ for (const entry of scan.entries) {+ const hashed = entry.size <= 4 * 1024 * 1024 ? await sha256File(entry.absolute, 4 * 1024 * 1024) : null;+ files.push({ path: options.scrub.text(tildeText(entry.absolute, options.privacy)), size: entry.size, sha256: hashed?.sha256 || null, mtime: new Date(entry.mtimeMs).toISOString() });}- return { files, truncated, scanned };+ return { files, truncated: scan.truncated, scanned: scan.scanned };}// --- the per-instance reader -------------------------------------------------------diff --git a/apps/server/src/context/outside-files.ts b/apps/server/src/context/outside-files.tsnew file mode 100644index 00000000..793e16a2--- /dev/null+++ b/apps/server/src/context/outside-files.ts@@ -0,0 +1,302 @@+/**+ * Files a turn's commands wrote OUTSIDE the project's tracked files+ * (`context.outside_files`, one event per turn): the temp folders, the+ * home's dotfiles and config, ~/.local and ~/bin, /usr/local and /opt,+ * folders the turn's commands created or extracted into (unzip -d, tar -C,+ * git clone, mkdir), symlinks (ln -s), and gitignored output inside the+ * project (build output, generated files). A rebuild of the turn restores+ * them; the project archive does not hold them.+ *+ * Each file: its path (home as ~, scrubbed), size, sha256 and the shell call+ * whose time window it was written in; its content only when it is a small+ * text file (OUTSIDE_FILE_CONTENT_BYTES, OUTSIDE_TURN_CONTENT_BYTES in all),+ * passed through the upload's credential rules (`scrub.content`). Credential+ * files and folders (keys, .env files, netrc, cloud and package manager+ * credentials, browser and password stores) are never listed, let alone+ * read.+ *+ * Not gated by the install log: it runs whenever capture context is on,+ * unless OMNIRUSH_OUTSIDE_FILES=0. Additive: a server that does not know the+ * event keeps it as an opaque trace event.+ *+ * The same file in omnirush-cli (assets/extensions/omnirush/capture/context/)+ * and omnirush-gui (apps/server/src/context/), listed in PARITY.sha256.+ */++import { execFile } from "node:child_process";+import { createHash } from "node:crypto";+import { lstat, readFile, readlink, readdir } from "node:fs/promises";+import { tmpdir } from "node:os";+import path from "node:path";++import { BUILD_OUTPUT_DIR_NAMES, DEPENDENCY_DIR_NAMES, isSecretUsePath } from "../session-archive/files-used-policy.js";+import { scanOutside, type OutsideEntry } from "./install-log.js";+import { isSecretFile, isSecretFolder, isTextBuffer, tildeText, type PrivacyContext, type Scrubber } from "./privacy.js";++export const OUTSIDE_FILES_SCHEMA = "omnirush.outside-files.v1";+/** A text file at most this big rides along whole; a bigger one is listed with its hash only. */+export const OUTSIDE_FILE_CONTENT_BYTES = 32 * 1024;+/** Content per turn, in all. */+export const OUTSIDE_TURN_CONTENT_BYTES = 256 * 1024;+/** Files hashed at most this big. */+const HASH_MAX_BYTES = 16 * 1024 * 1024;+const MAX_FILES = 200;+const MAX_PROJECT_FILES = 200;+const SCAN_BUDGET_MS = 400;+const GIT_TIMEOUT_MS = 3_000;++export type OutsideFileScope = "tmp" | "home" | "system" | "command_target" | "project_ignored";++export type OutsideFileItem = {+ /** Outside: absolute, home as `~`; project_ignored: relative to the project root, `/`-separated. */+ path: string;+ scope: OutsideFileScope;+ kind: "file" | "symlink";+ size: number;+ sha256: string | null;+ mtime: string;+ /** A symlink's target (home as `~`). */+ link_target?: string;+ /** The shell call it was written during, when exactly one matches. */+ tool_call_id?: string;+ /** The file's text (credential rules applied), for a small text file. */+ content?: string;+ /** Why the content is not here. */+ withheld?: "too_large" | "binary" | "budget" | "unreadable" | "not_attributed";+};++export type OutsideFilesEvent = {+ schema: typeof OUTSIDE_FILES_SCHEMA;+ turn: number;+ at: string;+ since: string;+ files: OutsideFileItem[];+ truncated: boolean;+ scanned: number;+ content_bytes: number;+ /** Another session's turn ran at the same time: a file may be its. */+ shared_window: boolean;+ timings_ms: Record<string, number>;+};++/** A turn's shell call, for attributing a file to it. */+export type OutsideCallWindow = { callId: string | null; start: number | null; end: number | null };++/** OMNIRUSH_OUTSIDE_FILES=0 (false, no, off) turns the step off. */+export function outsideFilesEnabled(env: NodeJS.ProcessEnv = process.env): boolean {+ return !["0", "false", "no", "off"].includes(String(env.OMNIRUSH_OUTSIDE_FILES ?? "").trim().toLowerCase());+}++/** Never listed, never read: a credential file or folder, a `.env` file, a name that says secret/token/password. */+export function isWithheldPath(absolute: string): boolean {+ return isSecretUsePath(absolute) || isSecretFile(absolute) || isSecretFolder(absolute);+}++function scopeOf(absolute: string, home: string | null, extraRoots: readonly string[], tmpRoots: readonly string[]): OutsideFileScope {+ const under = (dir: string) => absolute === dir || absolute.startsWith(dir.endsWith(path.sep) ? dir : dir + path.sep);+ if (tmpRoots.some(under)) return "tmp";+ if (extraRoots.some(under)) return "command_target";+ if (home && under(home)) return "home";+ return "system";+}++/** The shell calls running when a file changed: the one (when exactly one) and whether any. */+function attribute(changedMs: number, calls: readonly OutsideCallWindow[]): { callId: string | undefined; during: boolean } {+ const within = (slack: number) => calls.filter((call) => call.start !== null && changedMs >= call.start - slack && changedMs <= (call.end ?? Number.POSITIVE_INFINITY) + slack);+ // Inside one call's own window first; quick calls one after another overlap once given slack.+ const strict = within(0);+ const loose = within(1_000);+ const one = strict.length === 1 ? strict[0] : loose.length === 1 ? loose[0] : undefined;+ return { callId: one?.callId ?? undefined, during: loose.length > 0 };+}++async function sha256Of(file: string, size: number): Promise<string | null> {+ if (size > HASH_MAX_BYTES) return null;+ try {+ return createHash("sha256").update(await readFile(file)).digest("hex");+ } catch {+ return null;+ }+}++function git(root: string, args: string[]): Promise<string | null> {+ return new Promise((resolve) => {+ execFile("git", ["-C", root, ...args], { timeout: GIT_TIMEOUT_MS, maxBuffer: 8 * 1024 * 1024, windowsHide: true }, (error, stdout) => resolve(error ? null : stdout));+ });+}++/**+ * Gitignored files inside the project changed in the window (build output,+ * generated files), never dependency folders or caches. Through git (its+ * own ignore rules); none without a repository.+ */+export async function ignoredProjectWrites(root: string, since: number, until: number, budgetMs = SCAN_BUDGET_MS, max = MAX_PROJECT_FILES): Promise<{ entries: OutsideEntry[]; truncated: boolean; scanned: number }> {+ const started = Date.now();+ const listed = await git(root, ["ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"]);+ if (listed === null) return { entries: [], truncated: false, scanned: 0 };+ const entries: OutsideEntry[] = [];+ let scanned = 0;+ let truncated = false;+ const skipPart = (part: string) => DEPENDENCY_DIR_NAMES.has(part) || part === ".git" || part.startsWith("omnirush");+ const consider = async (absolute: string, depth: number): Promise<void> => {+ if (truncated) return;+ if (entries.length >= max || Date.now() - started > budgetMs) {+ truncated = true;+ return;+ }+ scanned += 1;+ let info: import("node:fs").Stats;+ try {+ info = await lstat(absolute);+ } catch {+ return;+ }+ if (info.isDirectory()) {+ if (depth <= 0 || isSecretFolder(absolute)) return;+ let names: string[];+ try {+ names = await readdir(absolute);+ } catch {+ return;+ }+ for (const name of names) {+ if (skipPart(name)) continue;+ await consider(path.join(absolute, name), depth - 1);+ if (truncated) return;+ }+ return;+ }+ if (!info.isFile() && !info.isSymbolicLink()) return;+ const changedMs = Math.max(info.mtimeMs, info.ctimeMs);+ if (changedMs < since - 1_000 || changedMs > until + 1_000) return;+ entries.push({ absolute, kind: info.isSymbolicLink() ? "symlink" : "file", size: info.size, changedMs, mtimeMs: info.mtimeMs });+ };+ for (const item of listed.split("\0")) {+ const rel = item.replace(/\/+$/, "");+ if (!rel) continue;+ const parts = rel.split("/");+ if (parts.some(skipPart)) continue;+ // An ignored folder is looked into (a build output folder deepest); an ignored file is itself.+ const depth = parts.some((part) => BUILD_OUTPUT_DIR_NAMES.has(part)) ? 8 : item.endsWith("/") ? 4 : 0;+ await consider(path.join(root, ...parts), depth);+ if (truncated) break;+ }+ return { entries, truncated, scanned };+}++export type OutsideFilesInput = {+ root: string;+ turn: number;+ since: number;+ until: number;+ home: string | null;+ platform: NodeJS.Platform;+ env: NodeJS.ProcessEnv;+ privacy: PrivacyContext;+ scrub: Scrubber;+ /** App state, install log and cache folders: never listed. */+ exclude: string[];+ /** Folders the turn's commands created or extracted into (OutsideTracker.folderPaths(), commandEffects folders). */+ commandFolders: string[];+ /** Files the turn's shell commands name as written (redirects, cp, tee, curl -o: commandEffects writes). */+ commandFiles?: string[];+ calls: readonly OutsideCallWindow[];+ sharedWindow: boolean;+ /** Tests. */+ tmpRoots?: string[];+ budgetMs?: number;+};++/** The turn's `context.outside_files` event; null when it found nothing. */+export async function outsideFilesOfTurn(input: OutsideFilesInput): Promise<OutsideFilesEvent | null> {+ const started = Date.now();+ const timings: Record<string, number> = {};+ const rootReal = path.resolve(input.root);+ const commandFolders = [...new Set(input.commandFolders.map((dir) => path.resolve(dir)))]+ .filter((dir) => dir !== rootReal && !dir.startsWith(rootReal + path.sep) && !isWithheldPath(dir))+ .slice(0, 16);+ const outside = await scanOutside({+ root: input.root,+ since: input.since,+ until: input.until,+ home: input.home,+ platform: input.platform,+ env: input.env,+ exclude: input.exclude,+ extraRoots: commandFolders.map((dir) => [dir, 4] as [string, number]),+ inodeTimes: true,+ maxFiles: MAX_FILES,+ budgetMs: input.budgetMs ?? SCAN_BUDGET_MS,+ });+ timings.outside = Date.now() - started;+ let at = Date.now();+ const ignored = await ignoredProjectWrites(input.root, input.since, input.until, input.budgetMs ?? SCAN_BUDGET_MS);+ timings.project_ignored = Date.now() - at;+ at = Date.now();+ const tmpRoots = input.tmpRoots ?? [...new Set([path.resolve(tmpdir()), ...(input.platform === "win32" ? [] : ["/tmp"])])];+ // The files the commands named as written: found even where a busy folder's scan ran out of budget.+ const scanned = new Set(outside.entries.map((entry) => entry.absolute));+ const excluded = input.exclude.map((dir) => path.resolve(dir));+ for (const file of [...new Set((input.commandFiles ?? []).map((item) => path.resolve(item)))].slice(0, MAX_FILES)) {+ if (scanned.has(file) || file === rootReal || file.startsWith(rootReal + path.sep) || excluded.some((dir) => file === dir || file.startsWith(dir + path.sep))) continue;+ const info = await lstat(file).catch(() => null);+ if (!info || (!info.isFile() && !info.isSymbolicLink())) continue;+ const changedMs = Math.max(info.mtimeMs, info.ctimeMs);+ if (changedMs < input.since - 1_000 || changedMs > input.until + 1_000) continue;+ outside.entries.push({ absolute: file, kind: info.isSymbolicLink() ? "symlink" : "file", size: info.size, changedMs, mtimeMs: info.mtimeMs });+ scanned.add(file);+ }+ const files: OutsideFileItem[] = [];+ let contentBytes = 0;+ const describe = async (entry: OutsideEntry, scope: OutsideFileScope, shownPath: string): Promise<void> => {+ if (isWithheldPath(entry.absolute)) return;+ const item: OutsideFileItem = { path: shownPath, scope, kind: entry.kind, size: entry.size, sha256: null, mtime: new Date(entry.mtimeMs).toISOString() };+ const { callId, during } = attribute(entry.changedMs, input.calls);+ if (callId) item.tool_call_id = callId;+ if (entry.kind === "symlink") {+ const target = await readlink(entry.absolute).catch(() => null);+ if (target !== null) item.link_target = input.scrub.text(tildeText(target, input.privacy));+ files.push(item);+ return;+ }+ item.sha256 = await sha256Of(entry.absolute, entry.size);+ // Content only for what the agent's own commands wrote: a file in the home or the temp folders that+ // changed while no shell call of the turn ran is another program's (an editor, a browser), listed only.+ const agents = during || scope === "command_target" || scope === "project_ignored";+ if (!agents) item.withheld = "not_attributed";+ else if (entry.size > OUTSIDE_FILE_CONTENT_BYTES) item.withheld = "too_large";+ else if (contentBytes + entry.size > OUTSIDE_TURN_CONTENT_BYTES) item.withheld = "budget";+ else {+ const bytes = await readFile(entry.absolute).catch(() => null);+ if (!bytes) item.withheld = "unreadable";+ else if (!isTextBuffer(bytes)) item.withheld = "binary";+ else {+ const text = tildeText(input.scrub.content(entry.absolute, bytes.toString("utf8")), input.privacy);+ item.content = text;+ contentBytes += Buffer.byteLength(text, "utf8");+ }+ }+ files.push(item);+ };+ for (const entry of outside.entries) {+ await describe(entry, scopeOf(entry.absolute, input.home, commandFolders, tmpRoots), input.scrub.text(tildeText(entry.absolute, input.privacy)));+ }+ for (const entry of ignored.entries) {+ const rel = path.relative(rootReal, entry.absolute).split(path.sep).join("/");+ await describe(entry, "project_ignored", input.scrub.text(rel));+ }+ timings.read = Date.now() - at;+ if (files.length === 0) return null;+ return {+ schema: OUTSIDE_FILES_SCHEMA,+ turn: input.turn,+ at: new Date(input.until).toISOString(),+ since: new Date(input.since).toISOString(),+ files,+ truncated: outside.truncated || ignored.truncated,+ scanned: outside.scanned + ignored.scanned,+ content_bytes: contentBytes,+ shared_window: input.sharedWindow,+ timings_ms: timings,+ };+}diff --git a/apps/server/src/session-archive/PARITY.sha256 b/apps/server/src/session-archive/PARITY.sha256index 6f719713..5fc34aaa 100644--- a/apps/server/src/session-archive/PARITY.sha256+++ b/apps/server/src/session-archive/PARITY.sha256@@ -2,5 +2,6 @@# and omnirush-gui (apps/server/src/session-archive/). A change to one must be ported to the other and this list updated in both.8fd84683b5542236f0c88750e3d9bd756e15a304d263141b57998b17c4a1ab9c late-writes.ts4d9a13a5df136cd6e13d0aeb67983249ca4a6913b66c5a8f768daa63da511dda tool-start.ts+38dc65a461de6905134c22837f5230d03383ec0bdd6df35090dd269160446edf turn-overlap.ts1906cab8c93370abd1d024d3552fe919a0006af9ebef084ada7b64c290d1bc95 turn-writers.ts402d175a7cffb8fdac490a97430b7590d72f4dde637e134e0a8de7c88ba06687 turn-prompts.tsdiff --git a/apps/server/src/session-archive/capture-v2.test.ts b/apps/server/src/session-archive/capture-v2.test.tsindex b6f2254f..a1d12cf0 100644--- a/apps/server/src/session-archive/capture-v2.test.ts+++ b/apps/server/src/session-archive/capture-v2.test.ts@@ -168,7 +168,7 @@ describe("capture v2", () => {const sessionId = "ses_v2_states_1";await archiver.startManifest(sessionId, root);expect((await open(await archiver.captureBase(sessionId, root, 0))).state!.state.kind).toBe("start");- expect(await archiver.captureState(sessionId, 1)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await archiver.captureState(sessionId, 1)).toMatchObject({ status: "skipped", reason: "unchanged" });write(join(root, "src", "app.py"), "print('edited between turns')\n");const pre = await open(await archiver.captureState(sessionId, 1));expect([pre.manifest.trigger, pre.manifest.turn]).toEqual(["pre_tool", 0]);diff --git a/apps/server/src/session-archive/files-used.test.ts b/apps/server/src/session-archive/files-used.test.tsindex 8c47bba1..f4a65c4b 100644--- a/apps/server/src/session-archive/files-used.test.ts+++ b/apps/server/src/session-archive/files-used.test.ts@@ -473,7 +473,7 @@ describe("a files-used chain", () => {expect(archiver.filesUsedMaybe()).toBe(false);const result = await archiver.filesUsedTurnEnded(sessionId, root, turn(toolPart("c", "read", { filePath: join(root, "config.local.yaml") })));expect(result.items).toBe(0);- expect(await archiver.captureDelta(sessionId, root, 1)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await archiver.captureDelta(sessionId, root, 1)).toMatchObject({ status: "skipped", reason: "unchanged" });});test("caps: a used file over the per-file cap is listed with its size only, never copied", async () => {diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.tsindex b38401c8..04198bef 100644--- a/apps/server/src/session-archive/index.test.ts+++ b/apps/server/src/session-archive/index.test.ts@@ -10,6 +10,9 @@ import { FakeArchiveServer, slowPartTwo } from "./fake-archive-server.js";import { ARCHIVE_STATE_DIRECTORY, POLICY_TTL_MS, SessionArchiver, type SessionArchiverOptions } from "./index.js";import { cleanupTempDirs, manifestOf, openArchive, tempDir } from "./test-helpers.js";+/** A capture result without its `detail` (the reason it was skipped, more precisely). */+const reasonOf = (result: { status: string; reason?: string }) => ("reason" in result ? { status: result.status, reason: result.reason } : result);+const execFileAsync = promisify(execFile);afterEach(cleanupTempDirs);@@ -122,7 +125,7 @@ describe("SessionArchiver", () => {expect(deltaMembers.find((member) => member.name === "src/app.ts")!.content.toString()).toBe("export const app = 2;\n");// Nothing changed: no archive and no sequence number used.- expect(await subject.captureDelta("ses_whole_folder", root, 2)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await subject.captureDelta("ses_whole_folder", root, 2)).toMatchObject({ status: "skipped", reason: "unchanged" });expect(await subject.captureDelta("ses_whole_folder", root, 1)).toEqual({ status: "skipped", reason: "stale_turn" });expect(await subject.captureBase("ses_whole_folder", root)).toEqual({ status: "skipped", reason: "exists" });await writeFile(join(root, "src/app.ts"), "export const app = 3;\n");@@ -158,7 +161,7 @@ describe("SessionArchiver", () => {await writeFile(join(root, "notes.md"), "hello");const state = await tempDir("state");const subject = archiver(server, state, { folderGate: { homeDir: home } });- expect(await subject.captureBase("ses_plain_folder", root)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_plain_folder", root)).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(await subject.captureDelta("ses_plain_folder", root, 1)).toEqual({ status: "skipped", reason: "no_base" });expect(server.callPaths()).toEqual(["GET archives/key 200"]);expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "sessions"))).toEqual([]);@@ -173,7 +176,7 @@ describe("SessionArchiver", () => {await mkdir(userData, { recursive: true });const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: home, userDataDir: userData } });for (const root of [home, userData, join(home, "Library"), "/usr"]) {- expect({ root, result: await subject.captureBase("ses_refused_folder", root) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });+ expect({ root, result: reasonOf(await subject.captureBase("ses_refused_folder", root)) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });}expect(server.calls).toEqual([]);});@@ -244,7 +247,7 @@ describe("SessionArchiver", () => {const detectors = [gitMarkerDetector, (dir: string) => gitParentDetector(dir, { systemDirs: [], homeDir: home })];const subject = archiver(server, await tempDir("state"), { detectors });- expect(await subject.captureBase("ses_empty_git", root)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_empty_git", root)).toMatchObject({ status: "skipped", reason: "not_archivable" });// At most the all-folders policy is asked (it is off); nothing is created.expect(server.calls.filter((call) => call.path !== "archives/key")).toEqual([]);@@ -347,7 +350,7 @@ describe("SessionArchiver", () => {const roots = [...new Set(Object.keys(files).map((path) => join(home, path, "..")))];roots.push(join(home, ".aws"), join(home, ".config"), join(home, ".local"), join(home, "Library"), join(home, "AppData"));for (const [index, root] of roots.entries()) {- expect({ root, result: await subject.captureBase(`ses_credential_${index}`, root) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });+ expect({ root, result: reasonOf(await subject.captureBase(`ses_credential_${index}`, root)) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });}expect(server.calls).toEqual([]);expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "sessions"))).toEqual([]);@@ -381,7 +384,7 @@ describe("SessionArchiver", () => {});const started = Date.now();for (const dir of ["notes", "drafts", "scratch"]) {- expect({ name, result: await subject.captureBase(`ses_${dir}_folder`, join(home, dir)) }).toEqual({ name, result: { status: "skipped", reason: "not_archivable" } });+ expect({ name, result: reasonOf(await subject.captureBase(`ses_${dir}_folder`, join(home, dir))) }).toEqual({ name, result: { status: "skipped", reason: "not_archivable" } });}expect(Date.now() - started).toBeLessThan(2_000);// One probe for the burst, kept as off; nothing slept, refreshed, written or switched off.@@ -410,12 +413,12 @@ describe("SessionArchiver", () => {const creates = () => server.calls.filter((call) => call.method === "POST" && call.path === "archives").length;// Off: a burst of folder sessions sends one probe.- for (const dir of ["a", "b"]) expect(await subject.captureBase(`ses_ttl_${dir}_folder`, join(home, dir))).toEqual({ status: "skipped", reason: "not_archivable" });+ for (const dir of ["a", "b"]) expect(await subject.captureBase(`ses_ttl_${dir}_folder`, join(home, dir))).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(keyReads()).toBe(1);// Turned on for this user: not seen until the kept answer is POLICY_TTL_MS old.server.policy = { all_folders: true };clock += POLICY_TTL_MS - 1;- expect(await subject.captureBase("ses_ttl_c_folder", join(home, "c"))).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_ttl_c_folder", join(home, "c"))).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(keyReads()).toBe(1);clock += 1;expect(await subject.captureBase("ses_ttl_d_folder", join(home, "d"))).toMatchObject({ status: "queued", kind: "base" });@@ -430,13 +433,13 @@ describe("SessionArchiver", () => {// Turned off again: that full fetch sees it, the folder is not archived, and the answer is kept as off.server.policy = { all_folders: false };- expect(await subject.captureBase("ses_ttl_f_folder", join(home, "f"))).toEqual({ status: "skipped", reason: "not_archivable" });- expect(await subject.captureBase("ses_ttl_g_folder", join(home, "g"))).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_ttl_f_folder", join(home, "f"))).toMatchObject({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_ttl_g_folder", join(home, "g"))).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(keyReads()).toBe(4);// A folder session's deltas pause while it is off, and catch up once it is on again.await writeFile(join(home, "d", "notes.md"), "d, edited\n");const createsBefore = creates();- expect(await subject.captureDelta("ses_ttl_d_folder", join(home, "d"), 1)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureDelta("ses_ttl_d_folder", join(home, "d"), 1)).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(creates()).toBe(createsBefore);server.policy = { all_folders: true };clock += POLICY_TTL_MS;@@ -477,8 +480,8 @@ describe("SessionArchiver", () => {const { root } = await project();const state = await tempDir("state");const subject = archiver(server, state);- expect(await subject.captureBase("ses_no_consent", root)).toEqual({ status: "skipped", reason: "disabled" });- expect(await subject.captureDelta("ses_no_consent", root, 1)).toEqual({ status: "skipped", reason: "disabled" });+ expect(await subject.captureBase("ses_no_consent", root)).toMatchObject({ status: "skipped", reason: "disabled" });+ expect(await subject.captureDelta("ses_no_consent", root, 1)).toMatchObject({ status: "skipped", reason: "disabled" });expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "pending"))).toEqual([]);expect(server.calls.map((call) => call.path)).toEqual(["archives/key"]);server.gate = null;@@ -496,7 +499,7 @@ describe("SessionArchiver", () => {expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "pending"))).toEqual([]);server.gate = null;await writeFile(join(root, "src/app.ts"), "changed");- expect(await subject.captureDelta("ses_withdrawn", root, 1)).toEqual({ status: "skipped", reason: "disabled" });+ expect(await subject.captureDelta("ses_withdrawn", root, 1)).toMatchObject({ status: "skipped", reason: "disabled" });// A new session re-checks consent and starts over with a new base; the old chain stays stopped.expect((await subject.captureBase("ses_after_consent", root)).status).toBe("queued");expect(await subject.captureDelta("ses_withdrawn", root, 2)).toEqual({ status: "skipped", reason: "stopped" });@@ -704,7 +707,7 @@ describe("SessionArchiver final archives", () => {expect(await subject.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "no_base" });expect((await subject.captureBase(id, root)).status).toBe("queued");// Nothing changed since the base: no archive, no request.- expect(await subject.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await subject.captureFinal(id, "idle")).toMatchObject({ status: "skipped", reason: "unchanged" });await writeFile(join(root, "src/app.ts"), "export const app = 2;\n");expect(await subject.captureDelta(id, root, 3)).toMatchObject({ status: "queued", sequence: 1 });@@ -733,7 +736,7 @@ describe("SessionArchiver final archives", () => {expect(manifests[3]).toMatchObject({ archive_id: archiveIdOf(quit), deleted: ["src/user.ts"] });// The server took a final archive: the chain point kept for a refusal is gone, one baseline is left.- expect(await subject.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await subject.captureFinal(id, "idle")).toMatchObject({ status: "skipped", reason: "unchanged" });expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "baselines"))).toHaveLength(1);});@@ -756,7 +759,7 @@ describe("SessionArchiver final archives", () => {server.policy = { all_folders: false };now += POLICY_TTL_MS;await writeFile(join(root, "chapter.md"), "three\n");- expect(await subject.captureFinal("ses_folder_final", "app_quit")).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureFinal("ses_folder_final", "app_quit")).toMatchObject({ status: "skipped", reason: "not_archivable" });});test("a server without final archives: the refused final is dropped once, the chain goes back, and the turn delta queued behind it is captured again", async () => {@@ -842,7 +845,7 @@ describe("SessionArchiver final archives", () => {hang = false;const second = archiver(server, state, { detectors });expect(await second.startFinalCandidates()).toEqual(expect.arrayContaining(["ses_quit_fast_01", "ses_quit_slow_01"]));- expect(await second.captureFinal("ses_quit_fast_01", "app_start")).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await second.captureFinal("ses_quit_fast_01", "app_start")).toMatchObject({ status: "skipped", reason: "unchanged" });expect(await second.captureFinal("ses_quit_slow_01", "app_start")).toMatchObject({ status: "queued", sequence: 1 });expect(await second.drain()).toMatchObject({ uploaded: 2, pending: 0 });const finals = await Promise.all(server.objects().slice(2).map(async (object) => [object.request.session_id, object.request.turn, manifestOf(await openArchive(object.object!)).reason]));@@ -936,7 +939,7 @@ describe("SessionArchiver final archives", () => {clock += day;expect(await third.captureDelta(id, root, 2)).toMatchObject({ status: "queued", sequence: 3 });clock += 2 * day;- expect(await third.captureDelta(id, root, 3)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await third.captureDelta(id, root, 3)).toMatchObject({ status: "skipped", reason: "unchanged" });await writeFile(join(root, "outside.txt"), "day 15\n");expect(await third.captureFinal(id, "idle")).toMatchObject({ status: "queued", sequence: 4 });expect(await third.drain()).toMatchObject({ uploaded: 2, pending: 0 });@@ -1007,7 +1010,7 @@ describe("SessionArchiver final archives", () => {expect((await subject.captureBase("ses_deleted_gone", root)).status).toBe("queued");await subject.drain();await rm(root, { recursive: true, force: true });- expect(await subject.captureFinal("ses_deleted_gone", "session_deleted")).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureFinal("ses_deleted_gone", "session_deleted")).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(await subject.startFinalCandidates()).toEqual([]);expect(server.objects()).toHaveLength(1);});@@ -1049,7 +1052,7 @@ describe("SessionArchiver touched files", () => {const id = "ses_touched_files";// Session start registers the session; nothing is packed yet.- expect(await subject.captureBase(id, root)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await subject.captureBase(id, root)).toMatchObject({ status: "skipped", reason: "unchanged" });expect(server.callPaths()).toEqual(["GET archives/key 200"]);// The agent reads the PDF, writes a binary, and names a credential file and a link out of the folder.subject.recordTouched(id, "brief.pdf");@@ -1095,7 +1098,7 @@ describe("SessionArchiver touched files", () => {// Nothing touched changed: nothing is sent, even though an untouched file did.await writeFile(join(root, "notes.txt"), "edited by the user, never touched by the agent\n");- expect(await subject.captureDelta(id, root, 3)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await subject.captureDelta(id, root, 3)).toMatchObject({ status: "skipped", reason: "unchanged" });for (const object of server.objects()) {expect(await names(object)).not.toContain("notes.txt");expect(object.object!.includes(Buffer.from("OUTSIDE_MARKER"))).toBe(false);@@ -1128,7 +1131,7 @@ describe("SessionArchiver touched files", () => {for (const policy of [undefined, { touched_files: false }, { touched_files: "true" }, { all_folders: false }]) {const { server, root, state, subject } = await touchedSetup({ policy });await writeFile(join(root, "brief.pdf"), randomBytes(1024));- expect(await subject.captureBase("ses_touched_off", root)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_touched_off", root)).toMatchObject({ status: "skipped", reason: "not_archivable" });subject.recordTouched("ses_touched_off", "brief.pdf");expect(await subject.captureDelta("ses_touched_off", root, 1)).toEqual({ status: "skipped", reason: "no_base" });expect(await subject.captureFinal("ses_touched_off", "app_quit")).toEqual({ status: "skipped", reason: "no_base" });@@ -1144,7 +1147,7 @@ describe("SessionArchiver touched files", () => {for (const dir of [".ssh", "work/OmniRush.ai/workdir", "Library/Application Support/x"]) await mkdir(join(home, dir), { recursive: true });const subject = make({ folderGate: { homeDir: home, userDataDir: userData } });for (const root of [home, join(home, ".ssh"), userData, join(userData, "workdir"), join(home, "Library/Application Support/x"), "/usr/share", join(state, ARCHIVE_STATE_DIRECTORY)]) {- expect({ root, result: await subject.captureBase("ses_touched_refused", root) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });+ expect({ root, result: reasonOf(await subject.captureBase("ses_touched_refused", root)) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });}expect(server.calls).toEqual([]);});@@ -1155,9 +1158,9 @@ describe("SessionArchiver touched files", () => {const first = make();const id = "ses_touched_final";const unbased = "ses_touched_nobase";- expect(await first.captureBase(id, root, 4)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await first.captureBase(id, root, 4)).toMatchObject({ status: "skipped", reason: "unchanged" });// A final archive with nothing touched yet: nothing, and still no base.- expect(await first.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await first.captureFinal(id, "idle")).toMatchObject({ status: "skipped", reason: "unchanged" });await writeFile(join(root, "draft.md"), "draft one\n");first.recordTouched(id, "draft.md");// The idle final is the first capture with a touched file: the base, numbered with the turns seen.@@ -1167,7 +1170,7 @@ describe("SessionArchiver touched files", () => {expect(await first.captureFinal(id, "app_quit")).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });// Another session on the folder touches a file, and the app quits before any capture of it.now += 1_000;- expect(await first.captureBase(unbased, root, 0)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await first.captureBase(unbased, root, 0)).toMatchObject({ status: "skipped", reason: "unchanged" });await writeFile(join(root, "table.csv"), "a,b\n");first.recordTouched(unbased, "table.csv");await first.stop();@@ -1210,7 +1213,7 @@ describe("SessionArchiver touched files", () => {const { server, root, state, make } = await touchedSetup({ now: () => new Date(now) });const first = make();const id = "ses_touched_offline";- expect(await first.captureBase(id, root, 0)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await first.captureBase(id, root, 0)).toMatchObject({ status: "skipped", reason: "unchanged" });await writeFile(join(root, "draft.md"), "draft\n");first.recordTouched(id, "draft.md");await first.stop();@@ -1223,11 +1226,11 @@ describe("SessionArchiver touched files", () => {expect((await second.captureFinal(id, "app_start")).status).toBe("skipped");// The session is resumed: its registration asks nothing again and drops nothing, and what it touches now is kept too.const calls = server.calls.length;- expect(await second.captureBase(id, root, 1)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await second.captureBase(id, root, 1)).toMatchObject({ status: "skipped", reason: "unchanged" });expect(server.calls.length).toBe(calls);await writeFile(join(root, "chart.svg"), "<svg/>\n");second.recordTouched(id, "chart.svg");- expect(await second.captureDelta(id, root, 2)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await second.captureDelta(id, root, 2)).toMatchObject({ status: "skipped", reason: "not_archivable" });await second.stop();expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "touched"))).toHaveLength(1);@@ -1253,8 +1256,8 @@ describe("SessionArchiver touched files", () => {server.policy = { touched_files: false };now += POLICY_TTL_MS;await writeFile(join(root, "a.bin"), randomBytes(512));- expect(await subject.captureDelta(id, root, 2)).toEqual({ status: "skipped", reason: "not_archivable" });- expect(await subject.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureDelta(id, root, 2)).toMatchObject({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureFinal(id, "idle")).toMatchObject({ status: "skipped", reason: "not_archivable" });server.policy = TOUCHED_ON;now += POLICY_TTL_MS;expect(await subject.captureDelta(id, root, 3)).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });@@ -1283,10 +1286,10 @@ describe("SessionArchiver touched files", () => {expect(await subject.drain()).toMatchObject({ uploaded: 0, dropped: 0, pending: 0 });// The kept policy is off for touched files now: another folder session is not archivable, without a request.const calls = server.calls.length;- expect(await subject.captureBase("ses_touched_other", root, 0)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_touched_other", root, 0)).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(server.calls.length).toBe(calls);now += POLICY_TTL_MS;- expect(await subject.captureBase("ses_touched_later", root, 0)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureBase("ses_touched_later", root, 0)).toMatchObject({ status: "skipped", reason: "not_archivable" });expect(server.callPaths().at(-1)).toBe("GET archives/key 200");});});diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex cac0de07..247e55f3 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -178,6 +178,8 @@ const ARCHIVE_PACK_RESERVE_BYTES = 1024 * 1024;* this time.*/export const POLICY_TTL_MS = 5 * 60_000;+/** A policy probe that failed (timeout, 5xx, offline) counts as off for this long only. */+export const POLICY_OUTAGE_TTL_MS = 60_000;/** The hash cache of a root's outside files is kept under the root's key with this suffix. */const OUTSIDE_CACHE_SUFFIX = "outside";@@ -264,7 +266,8 @@ export type CaptureSkipReason =export type CaptureResult =| { status: "queued"; archiveId: string; kind: ArchiveKind; sequence: number; size: number; late?: string[] }| { status: "paused"; reason: "disk_budget"; requiredBytes: number; pendingBytes: number; freeBytes: number | null }- | { status: "skipped"; reason: CaptureSkipReason };+ /** `detail`: why, more precisely (a base's gate reason, the server's code), for the upload's project_archive reason. */+ | { status: "skipped"; reason: CaptureSkipReason; detail?: string };export type DrainResult = {uploaded: number;@@ -1194,7 +1197,9 @@ export class SessionArchiver {return await task();} catch (error) {this.log("warn", "OmniRush archive capture failed", { sessionId, kind, error: errorSummary(error) });- return { status: "skipped", reason: "failed" };+ // The error's kind only (its message can name paths).+ const code = error && typeof error === "object" && typeof (error as { code?: unknown }).code === "string" ? (error as { code: string }).code : error instanceof Error ? error.name : "unknown";+ return { status: "skipped", reason: "failed", detail: `error_${code}`.slice(0, 64) };}}@@ -1215,7 +1220,7 @@ export class SessionArchiver {* a touched file (at session start, `lazy`, only registered).*/private async captureBaseLocked(sessionId: string, root: string, turn: number, generation: number, lazy = false): Promise<CaptureResult> {- if (!this.uploader.configured) return { status: "skipped", reason: "disabled" };+ if (!this.uploader.configured) return { status: "skipped", reason: "disabled", detail: "not_configured" };// A root no git detector qualified, and that no folder refusal stops, asks for the folder policy (4.4).const probe: { fetched: KeyResult | null } = { fetched: null };const gate = await isArchivableProject(root, [...this.detectors, this.folderDetector(generation, probe)], { appDirs: this.appDirs });@@ -1224,7 +1229,9 @@ export class SessionArchiver {if (!gate.archivable || !gate.marker) {this.dropPrescan(sessionId);await this.touched.forget(sessionId);- return { status: "skipped", reason: "not_archivable" };+ // no_marker: a folder without git while the server's folder policies are off (or could not be asked).+ const detail = gate.reason === "no_marker" ? (probe.fetched?.status === "unavailable" ? "policy_unavailable" : "no_git_folder_policy_off") : gate.reason;+ return { status: "skipped", reason: "not_archivable", detail };}// The key and the consent check (7.2): a probe made for this base already holds them; else the full fetch.const fetched = probe.fetched ?? await this.uploader.fetchKey();@@ -1232,13 +1239,13 @@ export class SessionArchiver {if (fetched.status === "disabled") {this.dropPrescan(sessionId);await this.disable(fetched.code);- return { status: "skipped", reason: "disabled" };+ return { status: "skipped", reason: "disabled", detail: String(fetched.code) };}// The policy was turned off since the answer this folder was let in on.if (fetched.status === "ok" && !markerAllowed(gate.marker, fetched.policy)) {this.dropPrescan(sessionId);await this.touched.forget(sessionId);- return { status: "skipped", reason: "not_archivable" };+ return { status: "skipped", reason: "not_archivable", detail: "folder_policy_off" };}const state: SessionState = {v: 1,@@ -1265,12 +1272,12 @@ export class SessionArchiver {// Remembered with next_sequence 0: the next captureDelta tries the base again (a folder passes the gate and the policy again).if (generation === this.generation) await this.saveSession(state);this.log("info", "OmniRush archive key unavailable; the base archive is retried after the next turn", { sessionId, reason: fetched.reason });- return { status: "skipped", reason: "unavailable" };+ return { status: "skipped", reason: "unavailable", detail: "key_unavailable" };}await this.enable(fetched.key);if (touched && lazy) {if (generation === this.generation) await this.saveSession(state);- return { status: "skipped", reason: "unchanged" };+ return { status: "skipped", reason: "unchanged", detail: "touched_files_lazy" };}try {return await this.captureArchive(state, "base", turn, fetched.key, generation);@@ -1356,7 +1363,10 @@ export class SessionArchiver {private rememberPolicy(fetched: KeyResult, generation: number, probe: boolean): void {if (generation !== this.generation) return;if (fetched.status === "unavailable" && !probe) return;- this.policy = { value: fetched.status === "ok" ? fetched.policy : POLICY_OFF, at: this.now().getTime() };+ // An outage (timeout, 5xx, offline) is kept as off for a minute only, not POLICY_TTL_MS: a folder+ // without git that starts during a blip is asked again at its next turn instead of going without a base.+ const at = this.now().getTime() - (fetched.status === "unavailable" ? Math.max(0, POLICY_TTL_MS - POLICY_OUTAGE_TTL_MS) : 0);+ this.policy = { value: fetched.status === "ok" ? fetched.policy : POLICY_OFF, at };// The uploader's rules follow the server's switch; anything but a yes keeps the old rules.const rulesBefore = redactionV3();setRedactionPolicy(fetched.status === "ok" && fetched.policy.redactionV3 === true ? CLIENT_V3 : "legacy");diff --git a/apps/server/src/session-archive/lifecycle.ts b/apps/server/src/session-archive/lifecycle.tsindex b7d890cd..7f6e6fe1 100644--- a/apps/server/src/session-archive/lifecycle.ts+++ b/apps/server/src/session-archive/lifecycle.ts@@ -63,8 +63,32 @@ export type ProjectArchiveLifecycleOptions = {now?: () => number;/** A pre_tool state took files written after the turn before ended (the session uploader records them). */onLateWrites?: (late: LateWrites) => void;+ /**+ * What became of a session's base (its starting project copy): archived+ * (or held from an earlier run), deferred (touched-files mode: taken with+ * the first edit), or skipped with the reason. The session uploader+ * records it in the session's upload, so no missing base goes unexplained.+ */+ onProjectArchive?: (sessionId: string, outcome: ProjectArchiveOutcome) => void;+};++export type ProjectArchiveOutcome = {+ status: "archived" | "deferred" | "skipped";+ /** Skipped: why (a CaptureSkipReason, or archive_off, consent_off, root_missing, disk_budget). */+ reason?: string;+ /** More precisely (the gate's reason, the server's code, the bytes it needed). */+ detail?: string;};+/** A base's capture result as a ProjectArchiveOutcome; null for a result that says nothing yet (unavailable is retried). */+export function projectArchiveOutcome(result: CaptureResult): ProjectArchiveOutcome | null {+ if (result.status === "queued") return { status: "archived" };+ if (result.status === "paused") return { status: "skipped", reason: "disk_budget", detail: `needs_${result.requiredBytes}_bytes` };+ if (result.reason === "exists") return { status: "archived", detail: "earlier_run" };+ if (result.reason === "unchanged") return { status: "deferred", reason: "touched_files_lazy" };+ return { status: "skipped", reason: result.reason, ...(result.detail ? { detail: result.detail } : {}) };+}+const DEFAULT_CONSENT_RECHECK_MS = 10 * 60_000;/** The quiet window after a turn: with no new prompt by then, the session gets a final archive. */export const FINAL_IDLE_MS = 10 * 60_000;@@ -170,12 +194,17 @@ export class ProjectArchiveLifecycle {private readonly quitBudgetMs: number;private readonly now: () => number;private readonly onLateWrites: ((late: LateWrites) => void) | undefined;+ private readonly onProjectArchive: ((sessionId: string, outcome: ProjectArchiveOutcome) => void) | undefined;private readonly enabled: boolean;private readonly accountMissing: boolean;private signedOut = false;private stopped = false;/** Archiving is off for the account until then (428 / 503 archive_disabled). */private consentOffUntil = 0;+ /** The run is ending: bases stop waiting for their quiet period (baseSettled). */+ private hurried = false;+ /** Sessions told their base is off for this run (archive_off, consent_off): once each. */+ private readonly offReported = new Set<string>();/** Sessions handed to captureBase in this app run. */private readonly sessions = new Map<string, SessionRecord>();private readonly tails = new Map<string, Promise<void>>();@@ -203,6 +232,16 @@ export class ProjectArchiveLifecycle {this.quitBudgetMs = Math.max(0, options.quitBudgetMs ?? QUIT_FINAL_BUDGET_MS);this.now = options.now ?? Date.now;this.onLateWrites = options.onLateWrites;+ this.onProjectArchive = options.onProjectArchive;+ }++ private reportArchive(sessionId: string, outcome: ProjectArchiveOutcome | null): void {+ if (!outcome) return;+ try {+ this.onProjectArchive?.(sessionId, outcome);+ } catch (error) {+ this.warn("report", error);+ }}private get active(): boolean {@@ -244,7 +283,13 @@ export class ProjectArchiveLifecycle {sessionStarted(input: { sessionId: string; root: string; engine: ArchiveEngineReads }): void {const known = this.sessions.get(input.sessionId);if (known) this.cancelIdleFinal(known);- if (!this.active || this.consentOff) return;+ if (!this.active || this.consentOff) {+ if (!known && !this.stopped && !this.offReported.has(input.sessionId)) {+ this.offReported.add(input.sessionId);+ this.reportArchive(input.sessionId, { status: "skipped", reason: this.consentOff ? "consent_off" : this.signedOut ? "signed_out" : "archive_off" });+ }+ return;+ }const promptedAt = this.now();this.lastPromptAt = promptedAt;if (known) {@@ -513,6 +558,20 @@ export class ProjectArchiveLifecycle {await this.archiver.stop(finals.length > 0 ? { finals, budgetMs: this.quitBudgetMs } : {}).catch((error: unknown) => this.warn("stop", error));}+ /**+ * The run is ending: a base still waiting for its quiet period is taken+ * now. Resolves once the session's queued steps ran (callers bound the+ * wait), so its outcome (onProjectArchive) is known before its end goes out.+ */+ async baseSettled(sessionId: string): Promise<void> {+ const record = this.sessions.get(sessionId);+ // Nothing waiting (resolved) and nothing running.+ if (!record || (!record.start && !record.starting)) return;+ this.hurried = true;+ this.wakeSleepers();+ await this.tails.get(sessionId)?.catch(() => undefined);+ }+/** Resolves once every scheduled step and drain has finished (tests). */async settled(): Promise<void> {while (this.tails.size > 0 || this.background.size > 0) await Promise.all([...this.tails.values(), ...this.background]);@@ -539,6 +598,7 @@ export class ProjectArchiveLifecycle {const root = await realpath(start.root).catch(() => null);if (!root) {record.start = null;+ this.reportArchive(sessionId, { status: "skipped", reason: "root_missing" });return true;}const count = turns ?? completedTurnCount(await start.engine.messages().catch(() => null));@@ -547,6 +607,7 @@ export class ProjectArchiveLifecycle {record.start = null;record.root = root;const result = await this.archiver.captureBase(sessionId, root, count);+ this.reportArchive(sessionId, projectArchiveOutcome(result));if (result.status === "paused") {record.start = { root, engine: start.engine };this.unresolved(sessionId, record, `archive paused: ${result.reason}`);@@ -703,7 +764,7 @@ export class ProjectArchiveLifecycle {*/private async quietPeriod(since: number): Promise<void> {if (this.baseIdleMs === 0) return;- while (this.active) {+ while (this.active && !this.hurried) {const now = this.now();const wait = Math.min(this.lastPromptAt + this.baseIdleMs, since + this.baseMaxDeferMs) - now;if (wait <= 0) return;diff --git a/apps/server/src/session-archive/project-scope.test.ts b/apps/server/src/session-archive/project-scope.test.tsindex a393e2da..c37c9954 100644--- a/apps/server/src/session-archive/project-scope.test.ts+++ b/apps/server/src/session-archive/project-scope.test.ts@@ -136,7 +136,7 @@ describe("a folder without git", () => {const { archiver } = rig({ all_folders: false, touched_files: true, capture_v2: true });const root = temp("omnirush-scope-oldbackend-");write(join(root, "package.json"), "{}");- expect(await archiver.captureBase("ses_scope_oldbackend", root, 0)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await archiver.captureBase("ses_scope_oldbackend", root, 0)).toMatchObject({ status: "skipped", reason: "unchanged" });});test("touched scope lists every file it does not archive with size and sha256 (a big one with its mtime), never silently", async () => {@@ -151,7 +151,7 @@ describe("a folder without git", () => {write(join(root, ".env"), "TOKEN=abcdefgh123\n");write(join(root, "node_modules", "x", "i.js"), "1\n");const sessionId = "ses_scope_touched";- expect(await archiver.captureBase(sessionId, root, 0)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await archiver.captureBase(sessionId, root, 0)).toMatchObject({ status: "skipped", reason: "unchanged" });write(join(root, "NOTES.txt"), "the agent wrote this\n");archiver.recordTouched(sessionId, "NOTES.txt");const base = await open(await archiver.captureDelta(sessionId, root, 1));diff --git a/apps/server/src/session-archive/reproducible.test.ts b/apps/server/src/session-archive/reproducible.test.tsindex 74d05c24..ff88c4d5 100644--- a/apps/server/src/session-archive/reproducible.test.ts+++ b/apps/server/src/session-archive/reproducible.test.ts@@ -111,7 +111,7 @@ describe("reproducible-task capture", () => {// The next turn changes nothing the agent touched: the touched file stays in the chain.await writeFile(join(root, "outputs/untouched.txt"), "edited, still never touched\n");- expect(await subject.captureDelta(id, root, 2)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await subject.captureDelta(id, root, 2)).toMatchObject({ status: "skipped", reason: "unchanged" });for (const object of server.objects()) {const names = (await openArchive(object.object!)).map((member) => member.name);expect(names).not.toContain("outputs/untouched.txt");@@ -124,7 +124,7 @@ describe("reproducible-task capture", () => {await restarted.start();expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "touched"))).toHaveLength(1);// The restarted app still holds the touched binary: no delta deletes it.- expect(await restarted.captureDelta(id, root, 3)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await restarted.captureDelta(id, root, 3)).toMatchObject({ status: "skipped", reason: "unchanged" });await restarted.stop();});@@ -138,7 +138,7 @@ describe("reproducible-task capture", () => {const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: home } });const id = "ses_repro_touched";- expect(await subject.captureBase(id, root)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(await subject.captureBase(id, root)).toMatchObject({ status: "skipped", reason: "unchanged" });subject.recordTouched(id, "outputs/plot.png");subject.recordTouched(id, "node_modules/dep/index.js");subject.recordTouched(id, "node_modules/dep/package-lock.json");diff --git a/apps/server/src/session-archive/turn-count.test.ts b/apps/server/src/session-archive/turn-count.test.tsnew file mode 100644index 00000000..47037398--- /dev/null+++ b/apps/server/src/session-archive/turn-count.test.ts@@ -0,0 +1,29 @@+// One turn count for the CLI and the desktop app (turn-prompts.ts, byte-identical+// in both, listed in PARITY.sha256). The CLI's test/turn-count.test.js counts+// the same list to the same numbers.+import { describe, expect, test } from "bun:test";++import { completedTurnCount, promptTurnAt } from "./turn-prompts.js";++const user = (id: string, created: number, parts: unknown[] = [{ type: "text", text: "do it" }], extra: Record<string, unknown> = {}) => ({ info: { id, role: "user", time: { created }, ...extra }, parts });+const done = (id: string, created: number) => ({ info: { id, role: "assistant", time: { created, completed: created + 1 }, finish: "stop" }, parts: [{ type: "step-finish" }] });+const running = (id: string, created: number) => ({ info: { id, role: "assistant", time: { created } }, parts: [] });++const SESSION = [+ user("m1", 10), done("m2", 11),+ user("m3", 20, [{ type: "compaction" }]), done("m4", 21),+ user("m5", 30, [{ type: "text", text: "notice", synthetic: true }]), done("m6", 31),+ user("m7", 40), done("m8", 41), done("m9", 42),+ user("m10", 50),+ user("m11", 60, [{ type: "text", text: "summarize" }], { agent: "compaction" }), done("m12", 61),+ user("m13", 70), running("m14", 71),+];++describe("turn count (shared with the CLI)", () => {+ test("compaction and synthetic messages are no turn; a stopped prompt is one; the running one is not yet", () => {+ expect(completedTurnCount(SESSION)).toBe(3);+ expect(promptTurnAt(SESSION, 45)).toBe(2);+ expect(promptTurnAt(SESSION, 75)).toBe(4);+ expect(completedTurnCount(null)).toBeNull();+ });+});diff --git a/apps/server/src/session-archive/turn-overlap.ts b/apps/server/src/session-archive/turn-overlap.tsnew file mode 100644index 00000000..76a94402--- /dev/null+++ b/apps/server/src/session-archive/turn-overlap.ts@@ -0,0 +1,124 @@+/**+ * Parallel chats on one folder: a turn's diff is a whole-workspace+ * comparison, so what another chat's agent wrote during the same window+ * lands in it too. Each turn's window is noted in a small registry per+ * project folder (`<state>/omnirush-turns/<folder hash>/<session>.json`),+ * shared by every process of the app on this machine (two terminals, the+ * desktop app's chats), and a turn whose window met another session's is+ * marked (`turn.overlap`): which sessions, and which of its changed paths+ * no tool of its own wrote.+ *+ * The same file in omnirush-cli (assets/extensions/omnirush/capture/session-archive/)+ * and omnirush-gui (apps/server/src/session-archive/), listed in PARITY.sha256.+ */++import { createHash } from "node:crypto";+import { mkdir, readdir, readFile, rename, rm, writeFile } from "node:fs/promises";+import path from "node:path";++export const TURN_OVERLAP_DIRECTORY = "omnirush-turns";+/** A window older than this is no one's any more (removed when seen). */+const STALE_MS = 24 * 60 * 60_000;+/** An open window whose process is gone counts as ended this long after it was last noted. */+const OPEN_GRACE_MS = 60_000;+const MAX_PATHS = 200;++export type TurnWindow = { session_id: string; pid: number; start: number; end: number | null; noted_at: number };++const SESSION_ID = /^[A-Za-z0-9._:-]{1,128}$/;++function folderKey(root: string): string {+ return createHash("sha256").update(path.resolve(root)).digest("hex").slice(0, 24);+}++function alive(pid: number): boolean {+ if (!Number.isSafeInteger(pid) || pid <= 0) return false;+ if (pid === process.pid) return true;+ try {+ process.kill(pid, 0);+ return true;+ } catch (error) {+ return (error as NodeJS.ErrnoException)?.code === "EPERM";+ }+}++/** The registry of one machine's turn windows, per project folder. */+export class TurnWindows {+ constructor(private readonly stateDir: string | null, private readonly now: () => number = Date.now) {}++ private dir(root: string): string | null {+ return this.stateDir ? path.join(this.stateDir, TURN_OVERLAP_DIRECTORY, folderKey(root)) : null;+ }++ private async write(root: string, window: TurnWindow): Promise<void> {+ const dir = this.dir(root);+ if (!dir || !SESSION_ID.test(window.session_id)) return;+ try {+ await mkdir(dir, { recursive: true });+ const file = path.join(dir, `${window.session_id}.json`);+ const temp = `${file}.${process.pid}.tmp`;+ await writeFile(temp, JSON.stringify(window));+ await rename(temp, file);+ } catch {+ /* best effort: without it only this process's own sessions are compared */+ }+ }++ /** A turn of `sessionId` on `root` began at `start`. */+ async started(root: string, sessionId: string, start: number): Promise<void> {+ await this.write(root, { session_id: sessionId, pid: process.pid, start, end: null, noted_at: this.now() });+ }++ /** The turn that began at `start` ended at `end`. */+ async ended(root: string, sessionId: string, start: number, end: number): Promise<void> {+ await this.write(root, { session_id: sessionId, pid: process.pid, start, end, noted_at: this.now() });+ }++ /** Every other session whose noted window on `root` met [start, end]. */+ async overlapping(root: string, sessionId: string, start: number, end: number): Promise<string[]> {+ const dir = this.dir(root);+ if (!dir) return [];+ let names: string[];+ try {+ names = (await readdir(dir)).filter((name) => name.endsWith(".json"));+ } catch {+ return [];+ }+ const found: string[] = [];+ for (const name of names.slice(0, 256)) {+ const file = path.join(dir, name);+ let window: TurnWindow;+ try {+ window = JSON.parse(await readFile(file, "utf8")) as TurnWindow;+ } catch {+ continue;+ }+ if (!window || typeof window.session_id !== "string" || window.session_id === sessionId || typeof window.start !== "number") continue;+ const last = typeof window.end === "number" ? window.end : alive(window.pid) ? this.now() : (window.noted_at ?? window.start) + OPEN_GRACE_MS;+ if (this.now() - last > STALE_MS) {+ await rm(file, { force: true }).catch(() => undefined);+ continue;+ }+ if (window.start <= end && last >= start) found.push(window.session_id);+ }+ return found.sort();+ }+}++/**+ * The `turn.overlap` event of a turn whose window met other sessions' on+ * the same folder, or null: the other sessions, and the changed paths no+ * tool of this session wrote (each may be another chat's).+ */+export function turnOverlapEvent(input: { others: readonly string[]; changed: readonly string[]; ownWrites: ReadonlySet<string>; start: number; end: number }): Record<string, unknown> | null {+ const others = [...new Set(input.others)].sort();+ if (others.length === 0) return null;+ const foreign = input.changed.filter((changed) => !input.ownWrites.has(changed));+ return {+ other_session_ids: others.slice(0, 32),+ window: { start: new Date(input.start).toISOString(), end: new Date(input.end).toISOString() },+ changed: input.changed.length,+ not_written_here: foreign.slice(0, MAX_PATHS),+ not_written_here_truncated: foreign.length > MAX_PATHS,+ };+}diff --git a/apps/server/src/session-uploader.test.ts b/apps/server/src/session-uploader.test.tsindex c2f234ca..388a4346 100644--- a/apps/server/src/session-uploader.test.ts+++ b/apps/server/src/session-uploader.test.ts@@ -404,6 +404,78 @@ describe("session uploader privacy", () => {expect(envelopes.slice(2).map((item) => [item.snapshot_type, item.session_segment, item.sequence])).toEqual([["start", 2, 3], ["trace", 2, 4], ["end", 2, 5]]);}, 20_000);+ test("an app that went away after a finished turn: the next launch sends the chat's end (marked recovered), once", async () => {+ const root = await mkdtemp(join(tmpdir(), "omnirush-upload-endrec-"));+ const stateDir = await mkdtemp(join(tmpdir(), "omnirush-upload-endrec-state-"));+ roots.push(root, stateDir);+ await writeFile(join(root, "app.txt"), "hello\n");+ const envelopes: Array<Record<string, unknown>> = [];+ const upload = async (_sessionId: string, compressed: Uint8Array) => {+ envelopes.push(withTraceEvents(JSON.parse(zstdDecompressSync(compressed).toString("utf8"))) as Record<string, unknown>);+ return Response.json({ ok: true }, { status: 201 });+ };+ const crashed = new SessionUploader({ stateDir, upload, fallbackScanMs: 60_000 });+ const sessionId = "session-endrec-1234";+ crashed.startSession(sessionId, "workspace-endrec", root);+ crashed.captureSnapshot(sessionId, "prompt");+ crashed.recordTrace(sessionId, "tool.call", { n: 1 });+ crashed.captureSnapshot(sessionId, "turn_completed");+ crashed.flushTrace(sessionId, { messages: [] });+ await crashed.idle(sessionId);+ await new Promise((resolve) => setTimeout(resolve, 400));+ // The app dies here (a crash, a kill, a power cut): no stop(), so no end.+ expect(envelopes.map((item) => item.snapshot_type)).not.toContain("end");++ const next = new SessionUploader({ stateDir, upload, fallbackScanMs: 60_000 });+ for (let waited = 0; waited < 5_000 && !envelopes.some((item) => item.snapshot_type === "end"); waited += 50) await new Promise((resolve) => setTimeout(resolve, 50));+ const end = envelopes.find((item) => item.snapshot_type === "end");+ expect(end).toMatchObject({ session_id: sessionId, trigger: "session_end" });+ const marked = envelopes.filter((item) => item.snapshot_type === "trace").flatMap((item) => (item.trace as Array<{ type: string }>) ?? []).map((event) => event.type);+ expect(marked).toContain("session.end_recovered");+ for (let waited = 0; waited < 3_000 && (await readdir(join(stateDir, "omnirush-upload-journal"))).length > 0; waited += 50) await new Promise((resolve) => setTimeout(resolve, 50));+ expect(await readdir(join(stateDir, "omnirush-upload-journal"))).toEqual([]);+ await next.stop();+ // A third launch finds nothing to send.+ const count = envelopes.length;+ const third = new SessionUploader({ stateDir, upload, fallbackScanMs: 60_000 });+ await new Promise((resolve) => setTimeout(resolve, 500));+ await third.stop();+ expect(envelopes.length).toBe(count);+ }, 20_000);++ test("the base's outcome rides along (collector.project_archive, workspace.project_archive); two chats on one folder mark turn.overlap", async () => {+ const root = await mkdtemp(join(tmpdir(), "omnirush-upload-gaps-"));+ const stateDir = await mkdtemp(join(tmpdir(), "omnirush-upload-gaps-state-"));+ roots.push(root, stateDir);+ await writeFile(join(root, "app.txt"), "hello\n");+ const envelopes: Array<Record<string, unknown>> = [];+ const upload = async (_sessionId: string, compressed: Uint8Array) => {+ envelopes.push(withTraceEvents(JSON.parse(zstdDecompressSync(compressed).toString("utf8"))) as Record<string, unknown>);+ return Response.json({ ok: true }, { status: 201 });+ };+ const uploader = new SessionUploader({ stateDir, upload, fallbackScanMs: 60_000 });+ const a = "session-gaps-a-1234";+ const b = "session-gaps-b-1234";+ uploader.startSession(a, "ws", root);+ uploader.startSession(b, "ws", root);+ await Promise.all([uploader.idle(a), uploader.idle(b)]);+ uploader.recordProjectArchive(a, { status: "skipped", reason: "not_archivable", detail: "no_git_folder_policy_off" });+ uploader.captureSnapshot(a, "prompt");+ uploader.captureSnapshot(b, "prompt");+ await Promise.all([uploader.idle(a), uploader.idle(b)]);+ await writeFile(join(root, "from-b.txt"), "b wrote this\n");+ uploader.captureSnapshot(a, "turn_completed");+ await uploader.idle(a);+ await uploader.stop();+ const traceOf = (id: string) => envelopes.filter((item) => item.session_id === id && item.snapshot_type === "trace").flatMap((item) => (item.trace as Array<{ type: string; data: Record<string, unknown> }>) ?? []);+ expect(traceOf(a).find((event) => event.type === "collector.project_archive")?.data).toEqual({ status: "skipped", reason: "not_archivable", detail: "no_git_folder_policy_off" });+ const end = envelopes.find((item) => item.session_id === a && item.snapshot_type === "end") as { workspace: Record<string, unknown> };+ expect(end.workspace.project_archive).toEqual({ status: "skipped", reason: "not_archivable", detail: "no_git_folder_policy_off" });+ const overlap = traceOf(a).find((event) => event.type === "turn.overlap");+ expect(overlap?.data.other_session_ids).toEqual([b]);+ expect(overlap?.data.not_written_here).toEqual(["from-b.txt"]);+ }, 20_000);+test("the journal holds only what is not yet handed to an upload, and is gone once the chat ends", async () => {const root = await mkdtemp(join(tmpdir(), "omnirush-upload-journal2-"));const stateDir = await mkdtemp(join(tmpdir(), "omnirush-upload-journal2-state-"));diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.tsindex 64cd44a1..83b789ed 100644--- a/apps/server/src/session-uploader.ts+++ b/apps/server/src/session-uploader.ts@@ -21,6 +21,7 @@ import {removeLegacyUploadContent,} from "./session-upload-state.js";import { TurnBaseStore, TurnDiffBuilder, type TurnDiffInput } from "./turn-diff.js";+import { TurnWindows, turnOverlapEvent } from "./session-archive/turn-overlap.js";import { binaryReason, isKnownBinaryPath } from "./session-archive/binary.js";import { captureGitEnv } from "./session-archive/repos.js";import { SUBAGENT_MODEL_FALLBACK_TRACE } from "./omnirush-swarm.js";@@ -589,6 +590,12 @@ type SessionState = {turnStartedAt: number | null;/** A prompt was dispatched and its turn has not completed (its turn_completed milestone ends it). */turnInProgress: boolean;+ /** When the current turn's window began (its prompt), for parallel chats on the folder (turn-overlap.ts). */+ turnWindowStart: number | null;+ /** The paths this session's own tools wrote in the current turn (its trace's tool evidence; not the watchers'). */+ turnToolWrites: Set<string>;+ /** What became of the session's base (the project archive lifecycle's onProjectArchive), sent with its later uploads. */+ projectArchive: { status: string; reason?: string; detail?: string } | null;/*** Edits were made on this session's root while it had no turn in progress* and another session on the root had one: they are that turn's, and this@@ -4484,6 +4491,8 @@ export class SessionUploader {private ledger: SessionLedger = { version: 1, sessions: {} };private ledgerWriteTail: Promise<void> = Promise.resolve();private readonly sessions = new Map<string, SessionState>();+ /** Turn windows per folder, shared by the app's processes on this machine (turn-overlap.ts). */+ private readonly turnWindows: TurnWindows;private readonly changeDebounceMs: number;private readonly fallbackScanMs: number;private readonly appVersion?: string;@@ -4591,6 +4600,7 @@ export class SessionUploader {this.log = options.log ?? (() => undefined);const stateDir = options.stateDir ? resolve(options.stateDir) : null;this.stateDir = stateDir;+ this.turnWindows = new TurnWindows(stateDir);this.uploadFormat = new UploadFormat<TraceCapabilities>({file: stateDir ? join(stateDir, UPLOAD_FORMAT_FILE) : null,// The broker's gateway when uploads go through it (one per install).@@ -5104,6 +5114,7 @@ export class SessionUploader {turnManifest: null,turnStartedAt: null,turnInProgress: false,+ turnWindowStart: null, turnToolWrites: new Set(), projectArchive: null,changesHeld: false,turnSkipped: new Map(),turnWrittenPaths: new Map(),@@ -5536,6 +5547,27 @@ export class SessionUploader {};}+ /**+ * What became of the session's base (its starting project copy): a+ * `collector.project_archive` trace event when it changed, and+ * `workspace.project_archive` on the session's later uploads, so a missing+ * base always says why. Additive: a server that does not know a reason+ * drops it.+ */+ recordProjectArchive(sessionId: string, outcome: { status: string; reason?: string; detail?: string }): void {+ const state = this.sessions.get(sessionId);+ if (!state || state.finished) return;+ const next = {+ status: outcome.status,+ ...(outcome.reason ? { reason: outcome.reason.slice(0, 64) } : {}),+ ...(outcome.detail ? { detail: outcome.detail.slice(0, 120) } : {}),+ };+ const previous = state.projectArchive;+ if (previous && previous.status === next.status && previous.reason === next.reason && previous.detail === next.detail) return;+ state.projectArchive = next;+ this.recordTrace(sessionId, "collector.project_archive", next);+ }+recordTrace(sessionId: string, type: string, data?: unknown): void {const state = this.sessions.get(sessionId);if (!state || state.finished) return;@@ -5576,7 +5608,10 @@ export class SessionUploader {this.reportTouched(state, path);const current = state.relevance.get(path);if (current === undefined || relevance < current) state.relevance.set(path, relevance);- if (relevance === 0 && state.turnStartedAt !== null) state.turnWrittenPaths.set(path, Date.now());+ if (relevance === 0 && state.turnStartedAt !== null) {+ state.turnWrittenPaths.set(path, Date.now());+ state.turnToolWrites.add(path);+ }if (isUploadPathDenied(path) || state.touchedPaths.has(path)) return;state.touchedPaths.add(path);this.markDirty(state, path);@@ -5834,6 +5869,9 @@ export class SessionUploader {// A file nothing saw from before the next turn keeps its entry here.return this.recordTurnDiff(state, new Map([...taken].map(([path, entry]) => [path, entry ?? current.get(path)!])), next);}+ // The turn's window, noted for parallel chats on the folder (turn-overlap.ts).+ state.turnWindowStart = startedAt ?? Date.now();+ void this.turnWindows.started(state.root, state.id, state.turnWindowStart);// The turn is measured from the workspace as its prompt found it.const forcePaths = new Set([...state.turnWrittenPaths].filter(([, at]) => startedAt === null || at >= startedAt).map(([path]) => path),@@ -5856,6 +5894,15 @@ export class SessionUploader {private async recordTurnDiff(state: SessionState, after: Manifest | null, next: number | null): Promise<void> {const before = state.turnManifest;const skipped = state.turnSkipped;+ // Parallel chats on the folder: this turn's window, and the paths this session's own tools wrote.+ const windowStart = state.turnWindowStart;+ const ownWrites = state.turnToolWrites;+ const windowEnd = Date.now();+ if (windowStart !== null) {+ state.turnWindowStart = null;+ state.turnToolWrites = new Set();+ void this.turnWindows.ended(state.root, state.id, windowStart, windowEnd);+ }state.turnManifest = after;state.turnSkipped = new Map(next === null ? [] : [...skipped].filter(([, at]) => at >= next));state.turnWrittenPaths = new Map(next === null ? [] : [...state.turnWrittenPaths].filter(([, at]) => at >= next));@@ -5872,6 +5919,7 @@ export class SessionUploader {if ((next === null || at < next) && !after.has(path) && !before.has(path)) changes.push({ path, before: null, after: null, unseen: false });}changes.sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));+ if (windowStart !== null) await this.recordTurnOverlap(state, windowStart, windowEnd, changes.map((change) => change.path), ownWrites);const builder = new TurnDiffBuilder();const pool = new ReadPool(1);const ancestors: AncestorCache = new Map();@@ -6105,6 +6153,21 @@ export class SessionUploader {if (milestone) this.appendTrace(state, "collector.trigger", { trigger, captured, ...(outcome ? { outcome } : {}) });}+ /**+ * A turn whose window met another session's on the same folder (in this+ * process, or another one on this machine, turn-overlap.ts) is marked+ * `turn.overlap`: its diff may hold that session's writes.+ */+ private async recordTurnOverlap(state: SessionState, start: number, end: number, changed: string[], ownWrites: ReadonlySet<string>): Promise<void> {+ const others = new Set<string>();+ for (const other of this.sessions.values()) {+ if (other !== state && !other.finished && other.cache === state.cache && other.turnInProgress) others.add(other.id);+ }+ for (const id of await this.turnWindows.overlapping(state.root, state.id, start, end).catch(() => [] as string[])) others.add(id);+ const event = turnOverlapEvent({ others: [...others], changed, ownWrites, start, end });+ if (event) this.appendTrace(state, "turn.overlap", event);+ }+/** Whether another live session on `state`'s root has a turn in progress. */private turnOnRootElsewhere(state: SessionState): boolean {for (const other of this.sessions.values()) {@@ -6427,7 +6490,8 @@ export class SessionUploader {const extras: Record<string, unknown> = {workspace: { root_name: rootName, git, ...(gitDeferred ? { git_deferred: "session_end" } : {}),...(git || gitDeferred ? {} : await gitSkippedField()),- ...(type === "start" && !git ? await projectArchiveField(state.root) : {}) },+ ...(type === "start" && !git ? await projectArchiveField(state.root) : {}),+ ...(type !== "start" && state.projectArchive ? { project_archive: state.projectArchive } : {}) },environment,touched_paths: touchedPaths,files_scope: filesScope,@@ -7218,7 +7282,8 @@ export class SessionUploader {try {const file = await stat(path);const parsed = Date.now() - file.mtimeMs > this.spoolMaxAgeMs ? null : parseJournal(await readFile(path, "utf8"));- if (!parsed || (parsed.events.length === 0 && !parsed.meta.turn_open)) {+ // A chat the app never ended (it quit hard, crashed or was killed) still gets its end: kept even with no events.+ if (!parsed || (parsed.events.length === 0 && !parsed.meta.turn_open && !this.endMissing(parsed.meta))) {await rm(path, { force: true });continue;}@@ -7243,10 +7308,17 @@ export class SessionUploader {void Promise.allSettled(recovered);}+ /** A chat's journal outlived the app, its turn is not open, and the ledger has no end for it: the end never went out. */+ private endMissing(meta: JournalMeta): boolean {+ const record = this.ledger.sessions[meta.session_id];+ return !meta.turn_open && Boolean(record) && !record?.endedAt && !this.sessions.has(meta.session_id);+ }+private async recoverJournal(journal: { path: string; meta: JournalMeta; events: TraceEvent[] }): Promise<void> {const { meta, events } = journal;const record = this.ledger.sessions[meta.session_id];- if (events.length > 0) {+ const endMissing = this.endMissing(meta);+ if (events.length > 0 || endMissing) {// The segment the events were recorded in, numbered after what it already sent.const state = this.newSessionState(meta.session_id, meta.workspace_id, meta.root, new FileHashCache());state.journal.off = true;@@ -7274,13 +7346,23 @@ export class SessionUploader {type: "collector.recovered",data: { reason: "restart", turn_open: meta.turn_open, events: events.length, journal_at: meta.at, session_segment: state.segment },};+ // The end that follows was taken at this launch, not when the app went away.+ const endMarker: TraceEvent[] = endMissing+ ? [{ at: marker.at, type: "session.end_recovered", data: { reason: "run_ended_without_end", last_message_id: state.lastMessageId ?? null } }]+ : [];try {- await this.uploadTrace(state, [marker, ...events]);+ await this.uploadTrace(state, [marker, ...events, ...endMarker]);} catch (error) {// Refused (a 422): recovered data is never dropped; the next start tries again, up to the age limit.this.log("warn", "OmniRush session journal upload refused; kept for the next start", { sessionId: meta.session_id, error: error instanceof Error ? error.message : "unknown" });return;}+ if (endMissing) {+ // Sent, or spooled for a retry: either way the end exists now.+ if (await this.uploadWorkspace(state, "end", "session_end").catch(() => false)) state.endedAt = new Date().toISOString();+ this.log("info", "OmniRush session end recovered", { sessionId: meta.session_id, sent: Boolean(state.endedAt) });+ await this.persistSession(state).catch(() => undefined);+ }this.log("info", "OmniRush session journal recovered", { sessionId: meta.session_id, events: events.length, turnOpen: meta.turn_open });}await rm(journal.path, { force: true });diff --git a/packages/types/src/desktop-ipc.ts b/packages/types/src/desktop-ipc.tsindex 41dbaf0f..3de8c46f 100644--- a/packages/types/src/desktop-ipc.ts+++ b/packages/types/src/desktop-ipc.ts@@ -303,6 +303,12 @@ export type OmniRushSessionStatus = {link: string | null;} | null;verdict: { state: "pending" | "usable" | "not_usable"; reasons: string[]; rewardWeight: number | null } | null;+ /**+ * Whether the session was delivered and, when not, why (`delivery`; on+ * servers without it, read from a not_usable verdict's reasons). A reason+ * without the server's message has `message: null` (the app words it).+ */+ delivery?: { state: "delivered" | "not_delivered" | "pending"; reasons: { code: string; message: string | null }[] } | null;/** In order; ids may grow. */checklist: { id: string; state: "pass" | "fail" | "warn" | "pending"; label: string; hint: string }[];evaluatedAt: string | null;