Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: resume the project sync chain after sign-in

ogsamrat8f6a2a8309f9
diff --git a/apps/server/src/omnirush-gateway-broker.test.ts b/apps/server/src/omnirush-gateway-broker.test.ts
index d4068b75..a4ff7e08 100644
--- a/apps/server/src/omnirush-gateway-broker.test.ts
+++ b/apps/server/src/omnirush-gateway-broker.test.ts
@@ -750,6 +750,24 @@ describe("OmniRush gateway broker project archive requests", () => {
]);
});
+ test("the session check the archiver reads a lost chain from goes out with its summary query; other paths stay refused", async () => {
+ const urls: string[] = [];
+ const broker = new OmniRushGatewayBroker({
+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1/", accessToken: "access-1", refreshToken: "refresh-1" },
+ engineToken: "local-engine-token",
+ fetch: async (input) => {
+ urls.push(String(input));
+ return Response.json({ archive: { chain: { next_sequence: 0, tip: null } } });
+ },
+ });
+ expect((await broker.archiveRequest("me/sessions/ses_abc12345/integrity?summary=1", { method: "GET" })).status).toBe(200);
+ expect(urls).toEqual(["https://gateway.example/omnirush/me/sessions/ses_abc12345/integrity?summary=1"]);
+ expect((await broker.archiveRequest("me/sessions/ses_abc12345/integrity", { method: "GET" })).status).toBe(404);
+ expect((await broker.archiveRequest("me/sessions/ses_abc12345/integrity?summary=1&x=1", { method: "GET" })).status).toBe(404);
+ expect((await broker.archiveRequest("me/quality", { method: "GET" })).status).toBe(404);
+ expect(urls).toHaveLength(1);
+ });
+
test("refresh: false (the archiver's policy probe) sends once and returns a 401 as it is, with no refresh", async () => {
const calls: string[] = [];
const refreshCalls: string[] = [];
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index a745fc24..b7afad37 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -831,6 +831,8 @@ const VOICE_TRANSCRIBE_TIMEOUT_MS = 35_000;
/** The project archive routes the session archiver calls (archives, archives/key, archives/<id>/parts|complete|abort). */
const ARCHIVE_API_PATH = /^archives(?:\/key|\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/(?:parts|complete|abort))?$/;
+/** The session check the archiver reads a lost chain back from (`archive.chain`), summary only. */
+const ARCHIVE_CHAIN_PATH = /^me\/sessions\/[A-Za-z0-9._%:-]{8,400}\/integrity\?summary=1$/;
/**
* Set by the omnirush-reasoning-effort engine plugin; keep the two
@@ -1551,8 +1553,9 @@ export class OmniRushGatewayBroker {
* request: a 401 is returned as it is, with no refresh and no second try.
*/
archiveRequest(path: string, init: { method: "GET" | "POST"; body?: string; signal?: AbortSignal; refresh?: false }): Promise<Response> {
- if (!ARCHIVE_API_PATH.test(path)) return Promise.resolve(Response.json({ error: "unsupported_archive_path" }, { status: 404 }));
- return this.withDeviceBearer((state) => this.fetcher(apiUrl(state.gatewayUrl, path), {
+ const chain = ARCHIVE_CHAIN_PATH.test(path);
+ if (!chain && !ARCHIVE_API_PATH.test(path)) return Promise.resolve(Response.json({ error: "unsupported_archive_path" }, { status: 404 }));
+ return this.withDeviceBearer((state) => this.fetcher(chain ? `${apiUrl(state.gatewayUrl, path.slice(0, path.indexOf("?")))}?summary=1` : apiUrl(state.gatewayUrl, path), {
method: init.method,
headers: {
Authorization: `Bearer ${state.accessToken}`,
diff --git a/apps/server/src/session-archive/fake-archive-server.ts b/apps/server/src/session-archive/fake-archive-server.ts
index 832ec08e..a8566666 100644
--- a/apps/server/src/session-archive/fake-archive-server.ts
+++ b/apps/server/src/session-archive/fake-archive-server.ts
@@ -46,6 +46,8 @@ export class FakeArchiveServer {
strictTurns = false;
/** The key route's `policy` (backend spec 4.4), left out while undefined. */
policy: unknown = undefined;
+ /** The session check's `archive.chain` (GET me/sessions/{id}/integrity?summary=1); a backend before it answers 404. */
+ chainView = false;
readonly archives = new Map<string, FakeArchive>();
readonly calls: FakeCall[] = [];
readonly puts: Array<{ archiveId: string; partNumber: number; status: number }> = [];
@@ -126,6 +128,20 @@ export class FakeArchiveServer {
if (this.gate && !abort) return json(this.gate.status, { detail: this.gate.detail });
if (method === "GET" && path === "archives/key") return json(200, { kid: testKeys.kid, public_key: testKeys.publicB64, alg: "X25519-HKDF-SHA256-A256GCM", policy: this.policy });
if (method === "POST" && path === "archives") return this.create(body);
+ const view = /^me\/sessions\/([^/?]+)\/integrity\?summary=1$/.exec(path);
+ if (method === "GET" && view && this.chainView) {
+ const sessionId = decodeURIComponent(view[1]!);
+ const rows = [...this.archives.values()].filter((archive) => archive.request.session_id === sessionId);
+ const top = rows.reduce<FakeArchive | null>((best, row) => (!best || row.request.sequence > best.request.sequence ? row : best), null);
+ return json(200, {
+ archive: {
+ chain: {
+ next_sequence: top ? top.request.sequence + 1 : 0,
+ tip: top ? { archive_id: top.request.archive_id, sequence: top.request.sequence, turn: top.request.turn, kind: top.request.kind, marker: (top.request.marker as string | undefined) ?? null, status: top.status, deleted: false } : null,
+ },
+ },
+ });
+ }
const parts = /^archives\/([^/]+)\/parts$/.exec(path);
if (method === "POST" && parts) return this.listParts(parts[1]!, body);
const complete = /^archives\/([^/]+)\/complete$/.exec(path);
diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.ts
index cff70379..b5ea57f1 100644
--- a/apps/server/src/session-archive/index.test.ts
+++ b/apps/server/src/session-archive/index.test.ts
@@ -584,6 +584,42 @@ describe("SessionArchiver", () => {
expect(server.objects().at(-1)!.request).toMatchObject({ session_id: "ses_rotated_key", kind: "base", turn: 1 });
});
+ test("a session whose archive record was lost (sign-out, reinstall) goes on from the server's chain: no base, no stop", async () => {
+ const server = new FakeArchiveServer();
+ server.chainView = true;
+ const { root } = await project();
+ const state = await tempDir("state");
+ const first = archiver(server, state);
+ expect((await first.captureBase("ses_lost_record", root, 0)).status).toBe("queued");
+ await writeFile(join(root, "src/app.ts"), "export const app = 2;\n");
+ expect((await first.captureDelta("ses_lost_record", root, 1)).status).toBe("queued");
+ expect((await first.drain()).uploaded).toBe(2);
+ await first.signOut();
+
+ // Signed in again, the session resumed after its two turns: a delta on the server's chain, every file in it.
+ const second = archiver(server, state);
+ const resumed = await second.captureBase("ses_lost_record", root, 2);
+ expect(resumed).toMatchObject({ status: "queued", kind: "delta", sequence: 2 });
+ expect((await second.drain()).uploaded).toBe(1);
+ await writeFile(join(root, "src/app.ts"), "export const app = 3;\n");
+ expect(await second.captureDelta("ses_lost_record", root, 3)).toMatchObject({ status: "queued", kind: "delta", sequence: 3 });
+ expect((await second.drain()).uploaded).toBe(1);
+ expect(server.objects().filter((archive) => archive.request.session_id === "ses_lost_record").map((archive) => [archive.request.sequence, archive.request.kind])).toEqual([[0, "base"], [1, "delta"], [2, "delta"], [3, "delta"]]);
+ await second.stop();
+
+ // A base the server refuses (409 archive_sequence_conflict): sent again on the chain at once, not stopped.
+ await rm(join(state, ARCHIVE_STATE_DIRECTORY), { recursive: true, force: true });
+ const third = archiver(server, state);
+ expect(await third.captureBase("ses_lost_record", root, 0)).toMatchObject({ status: "queued", kind: "base", sequence: 0 });
+ expect(await third.drain()).toMatchObject({ uploaded: 0, dropped: 1 });
+ for (let i = 0; i < 100 && server.objects().length < 5; i += 1) await new Promise((resolve) => setTimeout(resolve, 20));
+ await third.drain();
+ expect(server.objects().filter((archive) => archive.request.session_id === "ses_lost_record").map((archive) => [archive.request.sequence, archive.request.kind])).toEqual([[0, "base"], [1, "delta"], [2, "delta"], [3, "delta"], [4, "delta"]]);
+ await writeFile(join(root, "src/app.ts"), "export const app = 4;\n");
+ expect(await third.captureDelta("ses_lost_record", root, 4)).toMatchObject({ status: "queued", sequence: 5 });
+ await third.stop();
+ });
+
test("claimAccount keeps the queue for the same account and removes another account's", async () => {
const server = new FakeArchiveServer();
const { root } = await project();
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.ts
index 45488921..1e2d4fb7 100644
--- a/apps/server/src/session-archive/index.ts
+++ b/apps/server/src/session-archive/index.ts
@@ -147,6 +147,7 @@ import {
type ArchiveLog,
type KeyResult,
type RetryPolicy,
+ type ServerChain,
} from "./upload.js";
export { gitMarkerDetector, gitParentDetector, isArchivableProject, type ArchivableProject, type ProjectMarkerDetector } from "./detect.js";
@@ -337,6 +338,12 @@ const sessionStateSchema = z.object({
* server's flag was on then; the flag, as last answered, can still stop it at any capture (kill switch).
*/
files_used: z.boolean().optional(),
+ /**
+ * The chain goes on from the server's (continueServerChain): this machine
+ * lost its own record of it (a sign-out, a reinstall). Until its first
+ * archive here, there is no baseline: that delta carries every file.
+ */
+ server_chain: z.boolean().optional(),
}).transform((state) => ({ ...state, last_activity_at: state.last_activity_at ?? state.updated_at }));
type SessionState = z.infer<typeof sessionStateSchema>;
@@ -564,6 +571,8 @@ export class SessionArchiver {
private readonly prescans = new Map<string, Prescan>();
/** Capture v2: the sessions whose chain is v2 (as last loaded or saved). */
private readonly v2Sessions = new Set<string>();
+ /** Sessions whose server chain was asked in this app run and found empty (or not told): their base goes as a base. */
+ private readonly chainChecked = new Set<string>();
/** Files used (files-used.ts): staged copies and the items waiting for the next state. */
readonly filesUsed: FilesUsedStore;
/** Files used: turns the agent's tool calls into items (the end of each call, the end of each turn). */
@@ -1139,6 +1148,7 @@ export class SessionArchiver {
this.stoppedSessions.clear();
this.resettingSessions.clear();
this.v2Sessions.clear();
+ this.chainChecked.clear();
this.filesUsedSessions.clear();
this.filesUsedServer = null;
for (const sessionId of [...this.prescans.keys()]) this.dropPrescan(sessionId);
@@ -1412,6 +1422,22 @@ export class SessionArchiver {
* a capture's per-entry garbage from stacking on top of the previous one's.
*/
private async captureArchive(state: SessionState, kind: ArchiveKind, turn: number, key: ArchiveKey, generation: number, options: CaptureOptions = {}): Promise<CaptureResult> {
+ // A base after completed turns: the session ran before this machine's record of it began (`--continue` after
+ // a sign-out, a reinstall). The server may hold its chain already: a base would end in 409
+ // archive_sequence_conflict, so the chain goes on from the server's last archive instead.
+ if (kind === "base" && state.next_sequence === 0 && turn > 0 && !this.chainChecked.has(state.session_id)) {
+ const found = await this.continueServerChain(state, generation, options.signal);
+ if (found.status === "continue" && generation === this.generation) await this.saveSession(found.state);
+ if (found.status === "stop") {
+ await this.markStopped(state.session_id, found.code);
+ this.stoppedSessions.set(state.session_id, found.code);
+ return { status: "skipped", reason: "stopped", detail: found.code };
+ }
+ if (found.status === "continue") {
+ this.dropPrescan(state.session_id);
+ return this.captureArchive(found.state, "delta", Math.max(turn, found.state.last_turn ?? 0), key, generation, options.trigger ? options : { ...options, trigger: "turn" });
+ }
+ }
hintGarbageCollection();
try {
return await this.captureArchiveOnce(state, kind, turn, key, generation, options);
@@ -1420,6 +1446,53 @@ export class SessionArchiver {
}
}
+ /**
+ * Whether the server already holds a chain for the session and where it
+ * goes on (ArchiveUploader.serverChain). `continue`: the record to save,
+ * pointing at the server's last archive, with no baseline, so the next
+ * archive is a delta that carries every file. `fresh`: nothing there (or
+ * the server does not say), a base is right. `wait`: not now (offline, the
+ * server's last archive still uploading). `stop`: a chain that cannot be
+ * continued (its last archive failed or was deleted, another marker).
+ */
+ private async continueServerChain(state: SessionState, generation: number, signal?: AbortSignal): Promise<
+ | { status: "continue"; state: SessionState }
+ | { status: "fresh" }
+ | { status: "wait"; reason: string }
+ | { status: "stop"; code: string }
+ > {
+ const sessionId = state.session_id;
+ const answer: ServerChain = await this.uploader.serverChain(sessionId, signal);
+ if (answer.status === "unavailable") return { status: "wait", reason: answer.reason };
+ if (answer.status === "unsupported" || answer.nextSequence === 0 || !answer.tip) {
+ this.chainChecked.add(sessionId);
+ return { status: "fresh" };
+ }
+ const tip = answer.tip;
+ if (tip.status === "uploading" && !tip.deleted) return { status: "wait", reason: "server_archive_uploading" };
+ if (tip.status !== "uploaded" || tip.deleted || tip.sequence + 1 !== answer.nextSequence) {
+ this.log("warn", "OmniRush archive chain on the server cannot be continued", { sessionId, sequence: tip.sequence, status: tip.status, deleted: tip.deleted });
+ return { status: "stop", code: "archive_chain_ended" };
+ }
+ if (tip.marker !== null && tip.marker !== state.marker) {
+ this.log("warn", "OmniRush archive chain on the server is of another kind of folder; not continued", { sessionId, sequence: tip.sequence });
+ return { status: "stop", code: "archive_marker_changed" };
+ }
+ if (generation !== this.generation) return { status: "wait", reason: "signed_out" };
+ this.log("info", "OmniRush archive chain continues from the server", { sessionId, sequence: answer.nextSequence, parentTurn: tip.turn });
+ return { status: "continue", state: {
+ ...state,
+ next_sequence: answer.nextSequence,
+ last_archive_id: tip.archiveId,
+ last_turn: tip.turn,
+ baseline: null,
+ rewind: null,
+ final_due: true,
+ server_chain: true,
+ updated_at: this.now().toISOString(),
+ } };
+ }
+
private budgetLocked<T>(task: () => Promise<T>): Promise<T> {
const run = this.budgetTail.catch(() => undefined).then(task);
this.budgetTail = run.then(() => undefined, () => undefined);
@@ -1989,6 +2062,8 @@ export class SessionArchiver {
/** The entry list after the chain's last archive; null when it cannot be read (the chain cannot go on). */
private async readBaseline(state: SessionState): Promise<ArchiveEntry[] | null> {
+ // A chain continued from the server's (continueServerChain) has no baseline here: its next delta carries every file.
+ if (!state.baseline && state.server_chain) return [];
return state.baseline ? parseBaselineText(await readText(join(this.dirs.baselines, state.baseline))) : null;
}
@@ -2103,6 +2178,16 @@ export class SessionArchiver {
if (record.trigger === "final" && outcome.code === "archive_parent_mismatch" && !this.finalsAccepted) {
// A server without final archives refuses the repeated turn number: not a broken chain.
result.dropped += await this.rewindRefusedFinal(record);
+ } else if (outcome.code === "archive_sequence_conflict") {
+ // The server already holds this number: the chain goes on from the server's (or waits for it).
+ const recovered = await this.recoverSequenceConflict(record, signal);
+ if (recovered === "later") {
+ record.attempts += 1;
+ record.next_attempt_at = new Date(nowMs + Math.min(JOB_RETRY_MAX_MS, JOB_RETRY_BASE_MS * 2 ** (record.attempts - 1))).toISOString();
+ await persist();
+ } else {
+ result.dropped += recovered;
+ }
} else {
// The server does not take this marker: its policy is off here too until the next answer, so no other chain starts meanwhile.
if (outcome.code === ARCHIVE_MARKER_NOT_ALLOWED) this.markerRefused(record.request.marker, generation);
@@ -2284,6 +2369,61 @@ export class SessionArchiver {
return dropped.length;
}
+ /**
+ * 409 archive_sequence_conflict: the server holds the number already
+ * (this machine lost its record of the chain: a sign-out, a reinstall, a
+ * session continued from another machine). The session's jobs go, the
+ * record points at the server's last archive, and the folder is captured
+ * again at once as a delta on it (every file: there is no baseline), so
+ * archiving goes on. "later": the server's chain could not be read now,
+ * the job waits. A chain that cannot be continued stops, as before.
+ * Returns the jobs dropped.
+ */
+ private async recoverSequenceConflict(record: QueueRecord, signal: AbortSignal): Promise<number | "later"> {
+ const sessionId = record.request.session_id;
+ const state = await this.loadSession(sessionId);
+ if (!state || state.stopped) return this.stopSession(sessionId, "archive_sequence_conflict");
+ const found = await this.continueServerChain(state, this.generation, signal);
+ if (found.status === "wait") {
+ this.log("info", "OmniRush archive chain on the server not read yet; the upload waits", { sessionId, reason: found.reason });
+ return "later";
+ }
+ if (found.status === "stop") return this.stopSession(sessionId, found.code);
+ if (found.status === "fresh") return this.stopSession(sessionId, "archive_sequence_conflict");
+ const dropped = (await this.listQueue()).filter((job) => job.request.session_id === sessionId);
+ for (const job of dropped) await this.deleteJob(job);
+ this.resettingSessions.add(sessionId);
+ const generation = this.generation;
+ const turn = Math.max(found.state.last_turn ?? 0, ...dropped.map((job) => job.request.turn));
+ let recaptured = false;
+ void this.withSession(sessionId, async () => {
+ if (generation !== this.generation) return;
+ // Jobs a capture committed meanwhile were numbered on the lost chain too.
+ for (const job of await this.listQueue()) {
+ if (job.request.session_id === sessionId) await this.deleteJob(job);
+ }
+ const current = await this.loadSession(sessionId);
+ if (!current || current.stopped) return;
+ // A capture that held the lock may have saved its own (lost) chain point: the server's stands.
+ const continued: SessionState = current.server_chain && current.last_archive_id === found.state.last_archive_id
+ ? current
+ : { ...current, next_sequence: found.state.next_sequence, last_archive_id: found.state.last_archive_id, last_turn: found.state.last_turn, baseline: null, rewind: null, final_due: true, server_chain: true };
+ if (continued !== current) await this.saveSession(continued);
+ if (current.baseline && continued.baseline === null) await rm(join(this.dirs.baselines, current.baseline), { force: true });
+ if (current.rewind?.baseline && continued.rewind === null) await rm(join(this.dirs.baselines, current.rewind.baseline), { force: true });
+ // Without a key now, the next turn (or final) archive carries the folder.
+ if (!this.key) return;
+ const again = await this.captureArchive(continued, "delta", turn, this.key, generation, { trigger: "turn" });
+ recaptured = again.status === "queued";
+ })
+ .catch((error) => this.log("warn", "OmniRush archive chain recovery failed", { sessionId, error: errorSummary(error) }))
+ .finally(() => {
+ this.resettingSessions.delete(sessionId);
+ if (recaptured && generation === this.generation) void this.drain();
+ });
+ return dropped.length;
+ }
+
/** A base sealed to an unknown kid: forget it so the next capture seals a new base with the current key. */
private async resetToNoBase(sessionId: string): Promise<number> {
const dropped = (await this.listQueue()).filter((record) => record.request.session_id === sessionId);
diff --git a/apps/server/src/session-archive/upload.ts b/apps/server/src/session-archive/upload.ts
index a49a5295..eb743c72 100644
--- a/apps/server/src/session-archive/upload.ts
+++ b/apps/server/src/session-archive/upload.ts
@@ -162,6 +162,31 @@ export type UploadOutcome =
| { status: "retry_later"; reason: string }
| { status: "aborted" };
+/** Where the server's archive chain of a session goes on (GET /me/sessions/{id}/integrity, `archive.chain`). */
+export type ServerChainTip = { archiveId: string; sequence: number; turn: number; kind: string; marker: string | null; status: string; deleted: boolean };
+export type ServerChain =
+ | { status: "ok"; nextSequence: number; tip: ServerChainTip | null }
+ /** A server that does not say where the chain is (no `archive.chain`): nothing can be continued. */
+ | { status: "unsupported" }
+ | { status: "unavailable"; reason: string };
+
+const chainSchema = z.object({
+ archive: z.object({
+ chain: z.object({
+ next_sequence: z.number().int().nonnegative(),
+ tip: z.object({
+ archive_id: z.string().regex(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/),
+ sequence: z.number().int().nonnegative(),
+ turn: z.number().int().nonnegative(),
+ kind: z.string(),
+ marker: z.string().nullable(),
+ status: z.string(),
+ deleted: z.boolean(),
+ }).nullable(),
+ }),
+ }),
+});
+
const keySchema = z.object({ kid: z.string().regex(/^[0-9a-f]{16}$/), public_key: z.string(), alg: z.string() });
const partUrlSchema = z.object({ part_number: z.number().int().min(1), url: z.string().min(1) });
const createSchema = z.object({
@@ -384,6 +409,31 @@ export class ArchiveUploader {
return { status: "unavailable", reason: result.kind === "unavailable" ? result.reason : result.kind };
}
+ /**
+ * Where the server's chain of a session goes on: its next sequence and the
+ * archive below it (GET /me/sessions/{id}/integrity?summary=1, `archive.chain`).
+ * For a client that lost its own record of the chain (a sign-out, a
+ * reinstall, another machine), whose base would end in 409
+ * archive_sequence_conflict.
+ */
+ async serverChain(sessionId: string, signal?: AbortSignal): Promise<ServerChain> {
+ if (!this.configured) return { status: "unavailable", reason: "not_configured" };
+ const result = await this.call("GET", `me/sessions/${encodeURIComponent(sessionId)}/integrity?summary=1`, undefined, signal);
+ if (result.kind === "ok") {
+ const parsed = chainSchema.safeParse(result.body);
+ if (!parsed.success) return { status: "unsupported" };
+ const { next_sequence: nextSequence, tip } = parsed.data.archive.chain;
+ return {
+ status: "ok",
+ nextSequence,
+ tip: tip ? { archiveId: tip.archive_id, sequence: tip.sequence, turn: tip.turn, kind: tip.kind, marker: tip.marker, status: tip.status, deleted: tip.deleted } : null,
+ };
+ }
+ if (result.kind === "error" && (result.status === 404 || result.status === 405)) return { status: "unsupported" };
+ if (result.kind === "error") return { status: "unavailable", reason: `status ${result.status}` };
+ return { status: "unavailable", reason: result.kind === "unavailable" ? result.reason : result.kind };
+ }
+
/** Best-effort POST /archives/{id}/abort (7.7); one attempt, result ignored. */
async abort(archiveId: string, signal?: AbortSignal): Promise<void> {
try {