Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(capture): preserve Git baselines while deferring watched scans

Task Builder94ec01cfc8bc
diff --cc apps/server/src/session-uploader.test.ts
index 3483ebf8,5f02ce40..c2f234ca
--- a/apps/server/src/session-uploader.test.ts
+++ b/apps/server/src/session-uploader.test.ts
@@@ -3605,3 -4034,195 +4034,229 @@@ describe("session uploader incremental
expect(start.privacy).toMatchObject({ files_truncated: false });
}, 60_000);
});
+
+ describe("upload features", () => {
+ test("without the backend's idempotency_key feature (or any capability answer) no Idempotency-Key is sent and no integrity read is made", async () => {
+ const root = await mkdtemp(join(tmpdir(), "omnirush-upload-nofeat-"));
+ roots.push(root);
+ const keys: Array<string | undefined> = [];
+ let integrity = 0;
+ for (const capabilities of [undefined, async () => ({ schema_versions: [1, 2], canonical_trace: false })]) {
+ const sessionUploader = new SessionUploader({
+ upload: async (_sessionId, _compressed, _signal, request) => {
+ keys.push(request?.idempotencyKey);
+ return Response.json({ ok: true }, { status: 201 });
+ },
+ sessionIntegrity: async () => {
+ integrity += 1;
+ return Response.json({ max_segment: 4, max_sequence: 9 });
+ },
+ ...(capabilities ? { capabilities } : {}),
+ fallbackScanMs: 60_000,
+ });
+ sessionUploader.startSession("session-nofeat-1234", "workspace-nofeat", root);
+ await sessionUploader.stop();
+ }
+ expect(keys.length).toBeGreaterThan(0);
+ expect(keys.every((key) => key === undefined)).toBe(true);
+ expect(integrity).toBe(0);
+ });
+ });
+
+ describe("integrity confirmation", () => {
+ const FEATURES = async () => ({ schema_versions: [1, 2], canonical_trace: false, idempotency_key: true, integrity: true, integrity_summary: true });
+
+ async function endedChat(options: { refuseSecond?: boolean } = {}) {
+ const root = await mkdtemp(join(tmpdir(), "omnirush-confirm-"));
+ const stateDir = await mkdtemp(join(tmpdir(), "omnirush-confirm-state-"));
+ roots.push(root, stateDir);
+ await writeFile(join(root, "app.txt"), "hello\n");
+ let uploads = 0;
+ const first = new SessionUploader({
+ stateDir,
+ capabilities: FEATURES,
+ upload: async () => {
+ uploads += 1;
+ return options.refuseSecond && uploads === 2 ? Response.json({ detail: "bad" }, { status: 422 }) : Response.json({ ok: true }, { status: 201 });
+ },
+ sessionIntegrity: async () => Response.json({ detail: "session_not_found" }, { status: 404 }),
+ confirm: false,
+ fallbackScanMs: 60_000,
+ });
+ first.startSession("session-confirm-1234", "workspace-confirm", root);
+ first.recordTrace("session-confirm-1234", "tool.call", {});
+ first.flushTrace("session-confirm-1234");
+ await first.stop();
+ return { root, stateDir };
+ }
+
+ test("a good or not-good answer is final; the ledger keeps it", async () => {
+ const { stateDir } = await endedChat();
+ let calls = 0;
+ const uploader = new SessionUploader({
+ stateDir,
+ capabilities: FEATURES,
+ upload: async () => Response.json({ ok: true }, { status: 201 }),
+ sessionIntegrity: async (_sessionId, options) => {
+ calls += 1;
+ expect(options).toEqual({ summary: true });
+ return Response.json({ integrity: "ok", reasons: [], good_session: true, good_session_reasons: [], segments: [{ segment: 1, sequences: [1, 2, 3], gaps: [] }] });
+ },
+ confirm: false,
+ });
+ expect(await uploader.confirmSession("session-confirm-1234")).toMatchObject({ verdict: "good", checks: 1 });
+ expect(calls).toBe(1);
+ await uploader.stop();
+ const ledger = JSON.parse(await readFile(join(stateDir, "omnirush-upload-sessions.json"), "utf8")) as { sessions: Record<string, { confirmation?: { verdict: string; nextAt?: string } }> };
+ expect(ledger.sessions["session-confirm-1234"]?.confirmation).toMatchObject({ verdict: "good" });
+ expect(ledger.sessions["session-confirm-1234"]?.confirmation?.nextAt).toBeUndefined();
+ });
+
+ test("a gap the ledger explains (an upload the server refused) asks for nothing; pending is asked again later", async () => {
+ const { stateDir } = await endedChat({ refuseSecond: true });
+ let repairs = 0;
+ const uploader = new SessionUploader({
+ stateDir,
+ capabilities: FEATURES,
+ upload: async () => Response.json({ ok: true }, { status: 201 }),
+ sessionIntegrity: async () => Response.json({ integrity: "incomplete", reasons: ["sequence_gaps"], good_session: "pending", segments: [{ segment: 1, sequences: [1, 3], gaps: [2] }] }),
+ repairTranscript: async () => {
+ repairs += 1;
+ return [[]];
+ },
+ confirm: false,
+ });
+ const confirmation = await uploader.confirmSession("session-confirm-1234");
+ expect(confirmation).toMatchObject({ verdict: "pending", checks: 1 });
+ expect(confirmation?.nextAt).toBeDefined();
+ expect(confirmation?.repairedAt).toBeUndefined();
+ expect(repairs).toBe(0);
+ await uploader.stop();
+ });
+
+ test("a gap nothing here explains gets the transcript re-sent from the engine, once", async () => {
+ const { stateDir, root } = await endedChat();
+ const sent: Array<Record<string, unknown>> = [];
+ const asked: Array<{ sessionId: string; workspaceId: string; root: string }> = [];
+ const uploader = new SessionUploader({
+ stateDir,
+ capabilities: FEATURES,
+ upload: async (_sessionId, compressed) => {
+ sent.push(withTraceEvents(JSON.parse(zstdDecompressSync(compressed).toString("utf8"))) as Record<string, unknown>);
+ return Response.json({ ok: true }, { status: 201 });
+ },
+ sessionIntegrity: async () => Response.json({ integrity: "incomplete", reasons: ["sequence_gaps"], good_session: "pending", segments: [{ segment: 1, sequences: [1, 3], gaps: [2] }] }),
+ repairTranscript: async (chat) => {
+ asked.push(chat);
+ return [[{ info: { id: "msg_1", role: "user" }, parts: [] }]];
+ },
+ confirm: false,
+ });
+ const first = await uploader.confirmSession("session-confirm-1234");
+ expect(first?.repairedAt).toBeDefined();
+ expect(asked).toEqual([{ sessionId: "session-confirm-1234", workspaceId: "workspace-confirm", root }]);
+ expect(sent).toHaveLength(1);
+ const types = (sent[0]!.trace as Array<{ type: string }>).map((event) => event.type);
+ expect(types[0]).toBe("collector.repair");
+ expect(types).toContain("turn.messages");
+ expect(sent[0]).toMatchObject({ snapshot_type: "trace", session_segment: 1, sequence: 4 });
+ await uploader.confirmSession("session-confirm-1234");
+ expect(asked).toHaveLength(1);
+ await uploader.stop();
+ });
+
+ test("a gap a spooled upload will fill drains the spool instead of re-sending", async () => {
+ const { stateDir } = await endedChat();
+ const spool = join(stateDir, "omnirush-upload-spool");
+ await mkdir(spool, { recursive: true });
+ const id = "000000000009-000009-00000009";
+ await writeFile(join(spool, `${id}.zst`), Buffer.alloc(32, 9));
+ await writeFile(join(spool, `${id}.json`), JSON.stringify({ id, session_id: "session-confirm-1234", snapshot_type: "trace", trigger: "trace_flush", sequence: 2, bytes: 32, created_at: new Date().toISOString(), attempts: 1 }));
+ let repairs = 0;
+ const delivered: string[] = [];
+ const uploader = new SessionUploader({
+ stateDir,
+ capabilities: FEATURES,
+ upload: async (sessionId) => {
+ delivered.push(sessionId);
+ return Response.json({ ok: true }, { status: 201 });
+ },
+ sessionIntegrity: async () => Response.json({ integrity: "incomplete", reasons: ["sequence_gaps"], good_session: "pending", segments: [{ segment: 1, sequences: [1, 3], gaps: [2] }] }),
+ repairTranscript: async () => {
+ repairs += 1;
+ return [[]];
+ },
+ confirm: false,
+ retryBaseMs: 60_000,
+ });
+ await uploader.confirmSession("session-confirm-1234");
+ await uploader.drainSpool();
+ expect(repairs).toBe(0);
+ expect(delivered).toContain("session-confirm-1234");
+ await uploader.stop();
+ });
+
+ test("at launch, recent ended chats without a final verdict are checked; a judged one is not", async () => {
+ const { stateDir } = await endedChat();
+ const ledgerPath = join(stateDir, "omnirush-upload-sessions.json");
+ const ledger = JSON.parse(await readFile(ledgerPath, "utf8")) as { version: 1; sessions: Record<string, Record<string, unknown>> };
+ const record = ledger.sessions["session-confirm-1234"]!;
+ ledger.sessions["session-judged-1234"] = { ...record, confirmation: { verdict: "not_good", checkedAt: new Date().toISOString(), checks: 1 } };
+ await writeFile(ledgerPath, JSON.stringify(ledger));
+ const checked: string[] = [];
+ const uploader = new SessionUploader({
+ stateDir,
+ capabilities: FEATURES,
+ upload: async () => Response.json({ ok: true }, { status: 201 }),
+ sessionIntegrity: async (sessionId) => {
+ checked.push(sessionId);
+ return Response.json({ integrity: "ok", reasons: [], good_session: true, segments: [] });
+ },
+ });
+ for (let waited = 0; waited < 25_000 && checked.length === 0; waited += 100) await new Promise((resolve) => setTimeout(resolve, 100));
+ await new Promise((resolve) => setTimeout(resolve, 300));
+ expect(checked).toEqual(["session-confirm-1234"]);
+ await uploader.stop();
+ }, 40_000);
+
+ test("reads an integrity answer against the refusals the ledger holds", () => {
+ expect(integrityFindings({ good_session: "pending", reasons: ["sequence_gaps"], segments: [{ segment: 2, gaps: [5, 6] }, { segment: null, gaps: [1] }] }, ["2:5"]))
+ .toEqual({ verdict: "pending", gaps: ["2:6", "?:1"], missing: false });
+ expect(integrityFindings({ good_session: false, reasons: ["no_trace"], segments: [] })).toEqual({ verdict: "not_good", gaps: [], missing: true });
+ expect(integrityFindings("nope")).toBeNull();
+ });
++
++ test("keeps the Git baseline and defers only ordinary watched-edit metadata", async () => {
++ const root = await mkdtemp(join(tmpdir(), "omnirush-git-review-"));
++ roots.push(root);
++ await writeFile(join(root, "note.txt"), "baseline\\n");
++ await execFileAsync("git", ["init", "-q", "-b", "main", root]);
++ await execFileAsync("git", ["-C", root, "add", "."]);
++ await execFileAsync("git", ["-C", root, "-c", "[email protected]", "-c", "user.name=t", "commit", "-qm", "baseline"]);
++ const uploads: Array<Record<string, unknown>> = [];
++ const uploader = new SessionUploader({
++ upload: async (_sessionId, compressed) => {
++ uploads.push(JSON.parse(zstdDecompressSync(compressed).toString("utf8")) as Record<string, unknown>);
++ return Response.json({ ok: true }, { status: 201 });
++ },
++ changeDebounceMs: 10,
++ fallbackScanMs: 60_000,
++ });
++ const sessionId = "session-git-review-1234";
++ uploader.startSession(sessionId, "workspace-git", root);
++ await uploader.idle(sessionId);
++ const start = uploads.find((item) => item.snapshot_type === "start")!;
++ const startGit = (start.workspace as { git: { commit: string; branch: string; dirty: string } }).git;
++ expect(startGit.branch).toBe("main");
++ expect(startGit.commit).toMatch(/^[0-9a-f]{40}$/);
++ await writeFile(join(root, "note.txt"), "edited\\n");
++ uploader.captureSnapshot(sessionId, "prompt");
++ await uploader.idle(sessionId);
++ const change = uploads.find((item) => item.snapshot_type === "change")!;
++ expect(change.workspace).toMatchObject({ git: null, git_deferred: "session_end" });
++ await uploader.stop();
++ const end = uploads.find((item) => item.snapshot_type === "end")!;
++ expect((end.workspace as { git: { commit: string } }).git.commit).toBe(startGit.commit);
++ });
++
+ });
diff --cc apps/server/src/session-uploader.ts
index 9f075aff,dd885915..3b9098da
--- a/apps/server/src/session-uploader.ts
+++ b/apps/server/src/session-uploader.ts
@@@ -529,6 -576,6 +576,8 @@@ type SessionState =
/** The dirty set cannot be trusted; the next capture scans the whole tree. */
dirtyOverflow: boolean;
lastHead: string | null;
++ lastBranch: string | null;
++ hasGitSample: boolean;
lastChangeAt: number;
/** Untracked-but-not-ignored files as they stood when the current turn began. */
artifactBaseline: Promise<Map<string, ArtifactStat>> | null;
@@@ -2875,20 -3103,20 +3105,6 @@@ export async function collectGitBlock(r
};
}
--/** Cheap pre-scan git evidence used before the listing/file-count cap. */
--async function gitPriorityPaths(root: string): Promise<Set<string>> {
-- if (await gitText(root, ["rev-parse", "--is-inside-work-tree"]) !== "true") return new Set();
-- const prefixResult = await runGit(root, ["rev-parse", "--show-prefix"], { maxBytes: 64 * 1024, timeoutMs: 5_000 });
-- if (!prefixResult.ok || prefixResult.truncated) return new Set();
-- const prefix = prefixResult.stdout.toString("utf8").trim().replaceAll("\\", "/");
-- const status = await runGit(root, ["status", "--porcelain=v1", "-z", "--no-renames", "--untracked-files=normal", "--", "."], {
-- maxBytes: 2 * 1024 * 1024,
-- timeoutMs: 15_000,
-- });
-- if (!status.ok) return new Set();
-- return new Set(parseGitStatus(status.stdout.toString("utf8"), prefix).entries.map((entry) => entry.path));
--}
--
/**
* Folders whose content is reinstalled or rebuilt from the lockfiles (the
* reproducible-task rule's "not needed"): a touched or locked path in one is
@@@ -4591,6 -4993,6 +4981,8 @@@ export class SessionUploader
// Nothing is known about the tree yet: the first change capture rescans it.
dirtyOverflow: true,
lastHead: null,
++ lastBranch: null,
++ hasGitSample: false,
lastChangeAt: 0,
artifactBaseline: null,
model: null,
@@@ -5571,13 -5998,10 +5988,19 @@@
return state.changesHeld && uploading;
}
- /** Whether anything could have moved since the last accepted snapshot. Git history is finalized at session end. */
- private hasPendingChanges(state: SessionState): boolean {
+ /** Whether anything could have moved since the last accepted snapshot; one `git rev-parse` at most. */
+ private async hasPendingChanges(state: SessionState): Promise<boolean> {
if (state.dirtyOverflow || state.watchMode !== "watching" || state.dirty.size > 0 || this.journalHasChanges(state)) return true;
- // A commit with no working-tree change is represented by the final Git
- // block. Polling HEAD for every turn made Git a recurring capture cost and
- // did not add files to the workspace snapshot.
- return false;
- return (await gitHead(state.root)) !== state.lastHead;
++ return this.headMoved(state);
++ }
++
++ /** Detect commit-only and same-tree branch changes without status/log/diff. */
++ private async headMoved(state: SessionState): Promise<boolean> {
++ const [head, rawBranch] = await Promise.all([
++ gitHead(state.root), gitText(state.root, ["branch", "--show-current"]),
++ ]);
++ const branch = rawBranch ? clampUploadText(portablePathForUpload(rawBranch), MAX_GIT_REF_CHARS) : null;
++ return head !== state.lastHead || branch !== state.lastBranch;
}
private deferChange(state: SessionState, trigger: ChangeTrigger, wait: number): void {
@@@ -5764,12 -6188,7 +6187,13 @@@
// Collect the cheap relevance evidence before the listing cap. A late
// alphabetical path named by a tool or git status must not disappear
// merely because the workspace has many files.
- // Git status is needed once for the initial cap-priority plan and once
- // for the final snapshot. Change snapshots use watcher, journal and tool
- // evidence instead of spawning Git on every turn.
- const preGitPaths = type === "change"
- ? new Set<string>()
- : await gitPriorityPaths(state.root);
- const preGitPaths = targeted ? new Set<string>() : await gitPriorityPaths(state.root);
++ // Keep start/end and full-scan evidence. Only ordinary watched edits
++ // defer full Git; ref-only turns still record their commit and branch.
++ const refsMoved = targeted && previous !== null && await this.headMoved(state);
++ const gitDeferred = targeted && previous !== null && state.hasGitSample && !refsMoved;
++ const git = gitDeferred ? null : await collectGitBlock(state.root);
++ const gitChanged = git ? git.commit !== state.lastHead || git.branch !== state.lastBranch : refsMoved;
++ const preGitPaths = snapshotPriorityPaths([], git);
const preScanPaths = new Set<string>([
...state.relevance.keys(),
...state.touchedPaths,
@@@ -5805,21 -6224,20 +6229,16 @@@
if (state.turnStartedAt !== null) state.turnWrittenPaths.set(path, Date.now());
}
}
- // A change capture with no filesystem or journal evidence has nothing
- // to upload. Git-only history changes are represented by the final
- // session snapshot, where the complete status and diff are collected.
- if (type === "change" && scan.changed !== null && scan.changed.size === 0 && scan.removed === 0 && !this.journalHasChanges(state)) {
+ // A change capture that found nothing moved stops at one `git rev-parse`
+ // (a commit changes history without touching a file); the full git block
+ // with its status, log and diff is read only for a snapshot that goes out.
+ if (type === "change" && scan.changed !== null && scan.changed.size === 0 && scan.removed === 0 && !this.journalHasChanges(state)
- && (await gitHead(state.root)) === state.lastHead) {
++ && !gitChanged) {
accepted = true;
return false;
}
- // The complete Git summary (status, upstream, recent commits and diff)
- // is intentionally collected once, at session end. Earlier snapshots
- // still carry manifests and turn diffs, which are the replay baseline.
- const git = type === "end" ? await collectGitBlock(state.root) : null;
- const gitPaths = new Set<string>([
- ...preGitPaths,
- ...snapshotPriorityPaths([], git),
- ]);
+ // Its status and diff name the files a capped snapshot must keep.
- const git = await collectGitBlock(state.root);
- const gitPaths = new Set<string>([
- ...preGitPaths,
- ...snapshotPriorityPaths([], git),
- ]);
++ const gitPaths = preGitPaths;
const candidates = candidatesFor(scan, state.relevance, gitPaths);
// A full listing names the gitignored files too: a denied one (a gitignored
// `.env`) is left out for privacy and counted with the denied files, so
@@@ -5838,11 -6256,10 +6257,11 @@@
session_resumed: state.resumed,
trigger,
touched_paths: touchedPaths,
- ...PRIVACY_POLICY,
+ ...privacyPolicy(),
denied_file_count: deniedCount,
root_name: rootName,
-- git: { commit: git?.commit ?? null, branch: git?.branch ?? null, dirty: git ? String(git.dirty) : "false" },
- git_deferred: type !== "end",
++ git: { commit: git?.commit ?? null, branch: git?.branch ?? null, dirty: git ? String(git.dirty) : null },
++ ...(gitDeferred ? { git_deferred: "session_end" } : {}),
});
const leading: UploadFile[] = [{ path: "__omnirush__/workspace.json", content: metadata, sha256: sha256Hex(metadata) }];
if (journal.length > 0) {
@@@ -5855,13 -6272,7 +6274,9 @@@
}
const filesScope: "full" | "changed" = type === "start" ? "full" : "changed";
const extras: Record<string, unknown> = {
- workspace: {
- root_name: rootName,
- git,
- ...(git ? {} : await gitSkippedField()),
- ...(type === "start" && !git ? await projectArchiveField(state.root) : {}),
- ...(type === "end" ? {} : { git_deferred: "session_end" }),
- },
- workspace: { root_name: rootName, git, ...(git ? {} : await gitSkippedField()), ...(type === "start" && !git ? await projectArchiveField(state.root) : {}) },
++ workspace: { root_name: rootName, git, ...(gitDeferred ? { git_deferred: "session_end" } : {}),
++ ...(git || gitDeferred ? {} : await gitSkippedField()),
++ ...(type === "start" && !git ? await projectArchiveField(state.root) : {}) },
environment,
touched_paths: touchedPaths,
files_scope: filesScope,
@@@ -5965,7 -6376,7 +6380,11 @@@
state.extras = extraEntries;
state.turnManifest ??= turnBaseline(manifest, state.turnStartedAt, [previous]);
state.listing = { denied: scan.deniedCount, truncated: scan.manifestTruncated };
- if (git) state.lastHead = git.commit;
- state.lastHead = git?.commit ?? null;
++ if (git) {
++ state.lastHead = git.commit;
++ state.lastBranch = git.branch;
++ state.hasGitSample = true;
++ }
if (journal.length > 0) this.acknowledgeJournal(state, journal);
if (type === "change") state.lastChangeAt = Date.now();
return true;