Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: keep the upload format when the check fails

ogsamrat9a92c7a1c8d3
diff --git a/apps/server/src/context/PARITY.sha256 b/apps/server/src/context/PARITY.sha256
index 5e1bf5ef..23b585bd 100644
--- a/apps/server/src/context/PARITY.sha256
+++ b/apps/server/src/context/PARITY.sha256
@@ -18,3 +18,4 @@ d8c0636406c8cd9d55f9fb06af8827b3d1b73247393f48aaca2ef053ccf5a1ff services.ts
d51d434283f757f0ed45bb6996a74fb5bf9e1d56f34146e99df3c392cde5ea19 shell-aliases.ts
5af6e3e4d75e62e0dfc0fee53c583f34463871f9a4d5693e4bc13f688ed2142e system-packages.ts
4208574258fc043bc0cbb1770c718bfec22d996a44da493489d74d8a05bd7684 tool-events.ts
+76ad182f21e1a7805d4986f1b3dad160ecf62f07224d23a438554efc98b44ca2 upload-format.ts
diff --git a/apps/server/src/context/upload-format.ts b/apps/server/src/context/upload-format.ts
new file mode 100644
index 00000000..c5f94e79
--- /dev/null
+++ b/apps/server/src/context/upload-format.ts
@@ -0,0 +1,185 @@
+// The upload format (trace schema 2 or 3) the uploader sends, the same file in
+// omnirush-cli (assets/extensions/omnirush/capture/context/) and omnirush-gui
+// (apps/server/src/context/), listed in PARITY.sha256.
+//
+// GET /collect/capabilities says whether the server takes schema 3. Its answer
+// is remembered on disk per server for REMEMBER_MS, so a failed check (a 401,
+// a 5xx, a timeout, no network) keeps the format the server last advertised
+// instead of falling back. Only an explicit refusal of schema 3 by /collect
+// (422 unsupported_schema: unsupported()) moves a known server back to schema
+// 2. With nothing known and a failed check the upload goes out as schema 2,
+// and the server is asked again after RETRY_MS rather than RECHECK_MS.
+
+import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
+import { dirname } from "node:path";
+
+export const UPLOAD_FORMAT_FILE = "upload-format.json";
+/** How long a server's schema 3 is remembered after it last advertised it. */
+export const REMEMBER_MS = 7 * 24 * 60 * 60_000;
+/** How long an answer (or a refusal) stands before the server is asked again. */
+export const RECHECK_MS = 5 * 60_000;
+/** How soon the server is asked again after a check that failed. */
+export const RETRY_MS = 30_000;
+
+export type UploadSchema = 2 | 3;
+
+export type UploadFormatAnswer = { schema_versions?: unknown; canonical_trace?: unknown };
+
+export type UploadFormatOptions<Answer extends UploadFormatAnswer> = {
+ /** The remembered formats (<state dir>/upload-format.json); null keeps them in memory only. */
+ file: string | null;
+ /** The server the uploads go to; formats are remembered per server. */
+ server: () => string;
+ /** GET /collect/capabilities; throws when it got no usable answer. None: schema 2. */
+ probe?: () => Promise<Answer>;
+ /** Sees each answer (its features); its errors are ignored. */
+ onAnswer?: (answer: Answer) => void;
+ log?: (level: "info" | "warn", message: string, attributes?: Record<string, unknown>) => void;
+ now?: () => number;
+};
+
+type Remembered = { schema: 3; confirmed_at: number };
+type Stored = { servers: Record<string, Remembered> };
+
+/** Whether an answer of GET /collect/capabilities offers schema 3. */
+export function advertisesSchema3(answer: UploadFormatAnswer | null | undefined): boolean {
+ return Boolean(answer)
+ && answer!.canonical_trace === true
+ && Array.isArray(answer!.schema_versions)
+ && answer!.schema_versions.includes(3);
+}
+
+/** The key a server is remembered under: its URL without a trailing slash. */
+export function uploadFormatServerKey(server: string): string {
+ return server.trim().replace(/\/+$/, "").toLowerCase() || "default";
+}
+
+export class UploadFormat<Answer extends UploadFormatAnswer = UploadFormatAnswer> {
+ private cache: { server: string; version: UploadSchema; until: number } | null = null;
+ private pending: { server: string; promise: Promise<UploadSchema> } | null = null;
+ private generation = 0;
+ private memory: Stored = { servers: {} };
+ private readonly now: () => number;
+
+ constructor(private readonly options: UploadFormatOptions<Answer>) {
+ this.now = options.now ?? Date.now;
+ }
+
+ /** The schema to send now; asks the server when the last decision is due. */
+ version(force = false): Promise<UploadSchema> {
+ const server = uploadFormatServerKey(this.options.server());
+ const now = this.now();
+ if (!force && this.cache && this.cache.server === server && this.cache.until > now) return Promise.resolve(this.cache.version);
+ if (this.pending && this.pending.server === server) return this.pending.promise;
+ const generation = this.generation;
+ const promise = this.decide(server).then(({ version, recheckMs }) => {
+ if (generation === this.generation) this.cache = { server, version, until: this.now() + recheckMs };
+ return version;
+ }).finally(() => {
+ if (this.pending?.promise === promise) this.pending = null;
+ });
+ this.pending = { server, promise };
+ return promise;
+ }
+
+ /** /collect refused schema 3 (422 unsupported_schema): schema 2, and the server's schema 3 is forgotten. */
+ unsupported(): void {
+ const server = uploadFormatServerKey(this.options.server());
+ this.generation += 1;
+ this.pending = null;
+ this.cache = { server, version: 2, until: this.now() + RECHECK_MS };
+ const stored = this.read();
+ if (!stored.servers[server]) return;
+ delete stored.servers[server];
+ this.write(stored);
+ this.options.log?.("warn", "OmniRush session upload: the server refused schema 3; sending schema 2", { server });
+ }
+
+ /** Asks again at the next upload (sign-out); what is remembered on disk stays. */
+ reset(): void {
+ this.generation += 1;
+ this.cache = null;
+ this.pending = null;
+ }
+
+ /** Whether `server` advertised schema 3 within REMEMBER_MS (on disk, so other processes' answers count). */
+ remembers(server: string): boolean {
+ const entry = this.read().servers[uploadFormatServerKey(server)];
+ return Boolean(entry) && entry!.schema === 3 && this.now() - entry!.confirmed_at < REMEMBER_MS;
+ }
+
+ private async decide(server: string): Promise<{ version: UploadSchema; recheckMs: number }> {
+ if (!this.options.probe) return { version: 2, recheckMs: RECHECK_MS };
+ let answer: Answer;
+ try {
+ answer = await this.options.probe();
+ } catch (error) {
+ const remembered = this.remembers(server);
+ this.options.log?.("warn", "OmniRush session upload: the upload format check failed", {
+ error: error instanceof Error ? error.message : "unknown",
+ schema: remembered ? 3 : 2,
+ });
+ return { version: remembered ? 3 : 2, recheckMs: RETRY_MS };
+ }
+ try {
+ this.options.onAnswer?.(answer);
+ } catch {
+ /* the answer still counts */
+ }
+ if (advertisesSchema3(answer)) {
+ this.remember(server);
+ return { version: 3, recheckMs: RECHECK_MS };
+ }
+ // An answer without schema 3 never undoes a remembered one: only /collect's refusal does.
+ return { version: this.remembers(server) ? 3 : 2, recheckMs: RECHECK_MS };
+ }
+
+ private remember(server: string): void {
+ const stored = this.read();
+ const entry = stored.servers[server];
+ // Written at most once a minute per server (several processes share the file).
+ if (entry && entry.schema === 3 && this.now() - entry.confirmed_at < 60_000) return;
+ stored.servers[server] = { schema: 3, confirmed_at: this.now() };
+ this.write(stored);
+ }
+
+ private read(): Stored {
+ if (!this.options.file) return this.memory;
+ try {
+ const parsed: unknown = JSON.parse(readFileSync(this.options.file, "utf8"));
+ const servers = parsed && typeof parsed === "object" ? (parsed as { servers?: unknown }).servers : null;
+ const out: Stored = { servers: {} };
+ if (servers && typeof servers === "object") {
+ for (const [key, value] of Object.entries(servers as Record<string, unknown>)) {
+ const entry = value as Partial<Remembered> | null;
+ if (entry && entry.schema === 3 && typeof entry.confirmed_at === "number" && Number.isFinite(entry.confirmed_at)) {
+ out.servers[key] = { schema: 3, confirmed_at: entry.confirmed_at };
+ }
+ }
+ }
+ this.memory = out;
+ return out;
+ } catch {
+ // Missing or unreadable: what this process saw last.
+ return this.memory;
+ }
+ }
+
+ private write(stored: Stored): void {
+ this.memory = stored;
+ const file = this.options.file;
+ if (!file) return;
+ const temp = `${file}.${process.pid}.${Math.random().toString(36).slice(2, 10)}.tmp`;
+ try {
+ mkdirSync(dirname(file), { recursive: true });
+ writeFileSync(temp, `${JSON.stringify(stored)}\n`, { mode: 0o600 });
+ renameSync(temp, file);
+ } catch {
+ try {
+ rmSync(temp, { force: true });
+ } catch {
+ /* nothing to clean */
+ }
+ }
+ }
+}
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index c871a8e0..3034f599 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -19,6 +19,8 @@ const MAX_ATTACHMENT_RETRIES = 2;
const MAX_ATTACHMENT_ERROR_BYTES = 256 * 1024;
const TRACE_CAPABILITY_TTL_MS = 5 * 60_000;
+/** A failed capabilities check is not asked again sooner than this (the uploader keeps what it knew meanwhile). */
+const TRACE_CAPABILITY_RETRY_MS = 30_000;
/** Without the account's catalog (a standalone broker): the built-in catalog decides. */
function defaultReasoningSummaryFor(model: string): boolean {
@@ -1076,7 +1078,8 @@ export class OmniRushGatewayBroker {
*/
private readonly rejectedAttachments = new RejectedAttachments();
private capabilityCache: { gatewayUrl: string; accountKey: string; capabilities: TraceCapabilities; expiresAt: number } | null = null;
- private capabilityProbe: { key: string; promise: Promise<TraceCapabilities> } | null = null;
+ private capabilityProbe: { key: string; promise: Promise<TraceCapabilities | null> } | null = null;
+ private capabilityFailure: { gatewayUrl: string; accountKey: string; until: number } | null = null;
constructor(options: BrokerOptions) {
const gatewayUrl = options.credentials ? normalizedGatewayUrl(options.credentials.gatewayUrl) : null;
@@ -1354,8 +1357,20 @@ export class OmniRushGatewayBroker {
});
}
- async sessionUploadCapabilities(): Promise<TraceCapabilities> {
- if (!this.state) return { schema_versions: [1, 2, 3], canonical_trace: false };
+ /**
+ * The backend's upload capabilities, cached for TRACE_CAPABILITY_TTL_MS. A
+ * check that got no usable answer (a 401 after the refresh, a 5xx, a
+ * timeout) is not an answer: with `strict` (the session uploader, which
+ * then keeps the format the server last advertised) it throws; otherwise
+ * it reads as the oldest backend. Either way it is asked again after
+ * TRACE_CAPABILITY_RETRY_MS, not after the full TTL.
+ */
+ async sessionUploadCapabilities(options: { strict?: boolean } = {}): Promise<TraceCapabilities> {
+ const none: TraceCapabilities = { schema_versions: [1, 2], canonical_trace: false };
+ if (!this.state) {
+ if (options.strict) throw new Error("capabilities unavailable: signed out");
+ return { schema_versions: [1, 2, 3], canonical_trace: false };
+ }
const state = this.state;
const gatewayUrl = state.gatewayUrl;
const key = this.capabilityKey(state);
@@ -1364,6 +1379,15 @@ export class OmniRushGatewayBroker {
&& this.capabilityCache.expiresAt > Date.now()) {
return this.capabilityCache.capabilities;
}
+ const failed = () => {
+ if (options.strict) throw new Error("capabilities request failed");
+ return none;
+ };
+ if (this.capabilityFailure?.gatewayUrl === gatewayUrl
+ && this.capabilityFailure.accountKey === key
+ && this.capabilityFailure.until > Date.now()) {
+ return failed();
+ }
const pending = this.capabilityProbe?.key === key
? this.capabilityProbe.promise
: this.probeSessionUploadCapabilities(gatewayUrl);
@@ -1374,8 +1398,15 @@ export class OmniRushGatewayBroker {
}).catch(() => undefined);
}
const capabilities = await pending;
- if (this.state && this.capabilityKey(this.state) === key) {
- this.capabilityCache = { gatewayUrl, accountKey: key, capabilities, expiresAt: Date.now() + TRACE_CAPABILITY_TTL_MS };
+ // The key after the probe: a 401 inside it may have rotated the pair.
+ const settledKey = this.state ? this.capabilityKey(this.state) : null;
+ if (capabilities === null) {
+ if (settledKey) this.capabilityFailure = { gatewayUrl, accountKey: settledKey, until: Date.now() + TRACE_CAPABILITY_RETRY_MS };
+ return failed();
+ }
+ if (settledKey && this.state?.gatewayUrl === gatewayUrl) {
+ this.capabilityFailure = null;
+ this.capabilityCache = { gatewayUrl, accountKey: settledKey, capabilities, expiresAt: Date.now() + TRACE_CAPABILITY_TTL_MS };
}
return capabilities;
}
@@ -1383,6 +1414,7 @@ export class OmniRushGatewayBroker {
resetCapabilities(): void {
this.capabilityCache = null;
this.capabilityProbe = null;
+ this.capabilityFailure = null;
}
/**
@@ -1460,13 +1492,13 @@ export class OmniRushGatewayBroker {
/**
* The backend's /collect/capabilities: the trace schema and the features
- * it has (a key it does not name is false). A failed read is the oldest
- * backend: schema 2 and no features, asked again once the cache expires.
+ * it has (a key it does not name is false). Null when no usable answer
+ * came (a 401 after the refresh, a 5xx, a timeout, an unreadable body):
+ * that says nothing about what the backend takes.
*/
- private async probeSessionUploadCapabilities(gatewayUrl: string): Promise<TraceCapabilities> {
- const none: TraceCapabilities = { schema_versions: [1, 2], canonical_trace: false };
+ private async probeSessionUploadCapabilities(gatewayUrl: string): Promise<TraceCapabilities | null> {
try {
- if (this.state?.gatewayUrl !== gatewayUrl) return none;
+ if (this.state?.gatewayUrl !== gatewayUrl) return null;
const response = await this.withDeviceBearer((state) => this.fetcher(apiUrl(state.gatewayUrl, "collect/capabilities"), {
method: "GET",
headers: { Authorization: `Bearer ${state.accessToken}`, Accept: "application/json" },
@@ -1474,12 +1506,13 @@ export class OmniRushGatewayBroker {
}));
if (!response.ok) {
await response.body?.cancel().catch(() => undefined);
- return none;
+ // A backend without the route is the oldest one: that is an answer.
+ return response.status === 404 ? { schema_versions: [1, 2], canonical_trace: false } : null;
}
const text = await boundedText(response, 64 * 1024);
- if (text === null) return none;
+ if (text === null) return null;
const payload: unknown = JSON.parse(text);
- if (!isRecord(payload)) return none;
+ if (!isRecord(payload)) return null;
const v3 = payload.canonical_trace === true && Array.isArray(payload.schema_versions) && payload.schema_versions.some((version) => version === 3);
const triggers = Array.isArray(payload.triggers) ? payload.triggers.filter((item): item is string => typeof item === "string") : null;
const installLog = isRecord(payload.install_log) ? payload.install_log : null;
@@ -1493,7 +1526,7 @@ export class OmniRushGatewayBroker {
...(isRecord(payload.local_box) ? { local_box: { default: String(payload.local_box.default ?? "off"), max_turn_bytes: typeof payload.local_box.max_turn_bytes === "number" ? payload.local_box.max_turn_bytes : null } } : {}),
};
} catch {
- return none;
+ return null;
}
}
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 3e8a8774..3ae6592d 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -1124,7 +1124,8 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
? {
upload: (sessionId, compressed, signal, request) => gatewayBroker.uploadSession(sessionId, compressed, signal, request),
uploadFile: (sessionId, path, size, signal, request) => gatewayBroker.uploadSessionFile(sessionId, path, size, signal, request),
- capabilities: () => gatewayBroker.sessionUploadCapabilities(),
+ // Strict: a failed check throws, and the uploader keeps the format the server last advertised.
+ capabilities: () => gatewayBroker.sessionUploadCapabilities({ strict: true }),
refreshAccessToken: () => gatewayBroker.refreshAccessToken(),
}
: {},
diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.ts
index a4534f79..ce0c9330 100644
--- a/apps/server/src/session-uploader.ts
+++ b/apps/server/src/session-uploader.ts
@@ -34,6 +34,7 @@ import type { BoxCaptureInfo } from "./box/service.js";
import { engine2ImportedFrom } from "./engine2/imported.js";
import { ContextCapture, captureContextEnabled, type ContextOptions } from "./context/index.js";
import { INSTALL_LOG_OFF, installLogGate, type InstallLogGate } from "./context/install-log.js";
+import { UPLOAD_FORMAT_FILE, UploadFormat } from "./context/upload-format.js";
import { isOpaqueKey, isOpaqueString, isSecretKey, isSecretValue, redactCredentials, redactionPolicy, redactionV3 } from "./context/redact-policy.js";
import { maskWindowsProfiles } from "./context/privacy.js";
import bundledBestPractices from "./bundled-best-practices.json" with { type: "json" };
@@ -740,7 +741,11 @@ type SessionUploaderOptions = {
confirm?: boolean;
/** GET /omnirush/me/sessions/{id}/integrity for this account (where the server's copy of a chat stands). */
sessionIntegrity?: (sessionId: string, options?: { summary?: boolean; turns?: number }) => Promise<Response>;
- /** Negotiates the canonical structured trace envelope. Failures fall back to schema v2. */
+ /**
+ * Negotiates the canonical structured trace envelope (context/upload-format.ts):
+ * schema 3 once the server has advertised it, kept when a later check fails
+ * (it throws); schema 2 only while nothing is known or after /collect refused schema 3.
+ */
capabilities?: () => Promise<TraceCapabilities>;
/**
* Asks the account layer for a fresh access token once the gateway rejects
@@ -4559,8 +4564,10 @@ export class SessionUploader {
private readonly capabilityProbe?: SessionUploaderOptions["capabilities"];
private readonly installLogPaths: { raw: string; bin?: string | null } | null;
private installGateCache: { value: InstallLogGate; expiresAt: number } | null = null;
- private traceSchemaCache: { version: 2 | 3; features: UploadFeatures; expiresAt: number } | null = null;
- private traceSchemaProbe: Promise<2 | 3> | null = null;
+ /** The trace schema to send (context/upload-format.ts), remembered per server in the state dir. */
+ private readonly uploadFormat: UploadFormat<TraceCapabilities>;
+ /** The features of the last capabilities answer; kept while a later check fails. */
+ private probedFeatures: UploadFeatures | null = null;
/** Work counters for tests and profiling. */
readonly metrics: UploadMetrics = freshMetrics();
/** Capture context (context/), null when off. */
@@ -4584,6 +4591,16 @@ export class SessionUploader {
this.log = options.log ?? (() => undefined);
const stateDir = options.stateDir ? resolve(options.stateDir) : null;
this.stateDir = 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).
+ server: () => this.uploadUrl ?? "gateway",
+ ...(this.capabilityProbe ? { probe: this.capabilityProbe } : {}),
+ onAnswer: (capabilities) => {
+ this.probedFeatures = { idempotencyKey: capabilities.idempotency_key === true, integrity: capabilities.integrity === true };
+ },
+ log: (level, message, attributes) => this.log(level, message, attributes),
+ });
this.ledgerPath = stateDir ? join(stateDir, UPLOAD_SESSION_LEDGER_FILE) : null;
this.spoolDir = stateDir ? join(stateDir, UPLOAD_SPOOL_DIRECTORY) : null;
this.journalDir = stateDir ? join(stateDir, UPLOAD_JOURNAL_DIRECTORY) : null;
@@ -4981,32 +4998,7 @@ export class SessionUploader {
}
private negotiatedTraceSchema(force = false): Promise<2 | 3> {
- if (!force && this.traceSchemaCache && this.traceSchemaCache.expiresAt > Date.now()) {
- return Promise.resolve(this.traceSchemaCache.version);
- }
- if (this.traceSchemaProbe) return this.traceSchemaProbe;
- this.traceSchemaProbe = (async () => {
- let version: 2 | 3 = 2;
- let features: UploadFeatures = NO_UPLOAD_FEATURES;
- if (this.capabilityProbe) {
- try {
- const capabilities = await this.capabilityProbe();
- version = capabilities.canonical_trace
- && Array.isArray(capabilities.schema_versions)
- && capabilities.schema_versions.includes(TRACE_SCHEMA_VERSION)
- ? 3
- : 2;
- features = { idempotencyKey: capabilities.idempotency_key === true, integrity: capabilities.integrity === true };
- } catch {
- version = 2;
- }
- }
- this.traceSchemaCache = { version, features, expiresAt: Date.now() + TRACE_CAPABILITY_TTL_MS };
- return version;
- })().finally(() => {
- this.traceSchemaProbe = null;
- });
- return this.traceSchemaProbe;
+ return this.uploadFormat.version(force);
}
/**
@@ -5039,13 +5031,13 @@ export class SessionUploader {
}
private downgradeTraceSchema(): void {
- this.traceSchemaCache = { version: 2, features: this.traceSchemaCache?.features ?? NO_UPLOAD_FEATURES, expiresAt: Date.now() + TRACE_CAPABILITY_TTL_MS };
+ this.uploadFormat.unsupported();
}
/** What the backend's /collect takes beyond the envelope (Idempotency-Key, the integrity route), asked with the trace schema. */
private async uploadFeatures(): Promise<UploadFeatures> {
await this.negotiatedTraceSchema();
- return this.traceSchemaCache?.features ?? NO_UPLOAD_FEATURES;
+ return this.probedFeatures ?? NO_UPLOAD_FEATURES;
}
/** `turnsBefore`: the turns a reopened chat already had, so capture context numbers its turns on from them. */
@@ -7632,8 +7624,8 @@ export class SessionUploader {
this.draining?.controller.abort();
this.account.abort();
this.account = new AbortController();
- this.traceSchemaCache = null;
- this.traceSchemaProbe = null;
+ this.uploadFormat.reset();
+ this.probedFeatures = null;
this.retryFailures = 0;
return this.spoolLocked(async () => {
// The turn diffs' bases are workspace content on disk too, and so are the journals.
diff --git a/apps/server/src/upload-format.test.ts b/apps/server/src/upload-format.test.ts
new file mode 100644
index 00000000..b0e639ae
--- /dev/null
+++ b/apps/server/src/upload-format.test.ts
@@ -0,0 +1,191 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { zstdDecompressSync } from "node:zlib";
+
+import { REMEMBER_MS, RECHECK_MS, RETRY_MS, UPLOAD_FORMAT_FILE, UploadFormat } from "./context/upload-format.js";
+import { OmniRushGatewayBroker } from "./omnirush-gateway-broker.js";
+import { SessionUploader, type TraceCapabilities } from "./session-uploader.js";
+
+// 3.4.1 cached schema 2 for five minutes after any failed capabilities check
+// (a 401 the refresh did not cure, a 5xx, a timeout during a server hiccup):
+// the broker answered a failure as "the oldest backend" and the uploader
+// believed it, so short runs of schema-2 uploads appeared between schema-3
+// ones. The server's schema 3 is now remembered per server in the state dir
+// (context/upload-format.ts, shared with the CLI); only /collect's 422
+// unsupported_schema undoes it.
+
+const SCHEMA_3: TraceCapabilities = { schema_versions: [1, 2, 3], canonical_trace: true };
+const roots: string[] = [];
+
+afterEach(async () => {
+ await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
+});
+
+async function temp(prefix: string): Promise<string> {
+ const dir = await mkdtemp(join(tmpdir(), prefix));
+ roots.push(dir);
+ return dir;
+}
+
+type Check = "ok" | "fail" | TraceCapabilities;
+
+/** One run (a process) with `stateDir`: the [schema, refused] of each trace upload sent. */
+async function runTraces(stateDir: string, check: Check, options: { refuse3?: boolean } = {}): Promise<Array<[number, boolean]>> {
+ const root = await temp("omnirush-format-ws-");
+ await writeFile(join(root, "README.md"), "# demo\n");
+ const sent: Array<[number, boolean]> = [];
+ const uploader = new SessionUploader({
+ stateDir,
+ capabilities: async () => {
+ if (check === "fail") throw new Error("capabilities request failed");
+ return check === "ok" ? SCHEMA_3 : check;
+ },
+ upload: async (_sessionId, compressed) => {
+ const envelope = JSON.parse(zstdDecompressSync(compressed).toString("utf8")) as { snapshot_type: string; schema_version: number };
+ const refused = envelope.snapshot_type === "trace" && envelope.schema_version === 3 && options.refuse3 === true;
+ if (envelope.snapshot_type === "trace") sent.push([envelope.schema_version, refused]);
+ return refused
+ ? Response.json({ error: "unsupported_schema", schema_version: 3 }, { status: 422 })
+ : Response.json({ ok: true }, { status: 201 });
+ },
+ fallbackScanMs: 60_000,
+ retryBaseMs: 60_000,
+ });
+ const sessionId = `session-format-${Math.random().toString(36).slice(2, 10)}`;
+ try {
+ uploader.startSession(sessionId, "workspace-format", root);
+ await uploader.idle(sessionId);
+ uploader.recordTrace(sessionId, "tool.call", {});
+ uploader.flushTrace(sessionId);
+ await uploader.idle(sessionId);
+ } finally {
+ await uploader.stop();
+ }
+ return sent;
+}
+
+describe("the upload format when the capabilities check fails", () => {
+ test("a failed check after the server advertised schema 3 still sends schema 3", async () => {
+ const stateDir = await temp("omnirush-format-state-");
+ expect(await runTraces(stateDir, "ok")).toEqual([[3, false]]);
+ // The broker throws for a 401 after its refresh, a 5xx, a timeout (sessionUploadCapabilities strict).
+ expect(await runTraces(stateDir, "fail")).toEqual([[3, false]]);
+ }, 30_000);
+
+ test("an answer without schema 3 does not undo a remembered one; /collect's 422 unsupported_schema does", async () => {
+ const stateDir = await temp("omnirush-format-state-");
+ expect(await runTraces(stateDir, "ok")).toEqual([[3, false]]);
+ expect(await runTraces(stateDir, { schema_versions: [1, 2], canonical_trace: false })).toEqual([[3, false]]);
+ expect(await runTraces(stateDir, "ok", { refuse3: true })).toEqual([[3, true], [2, false]]);
+ expect(JSON.parse(await readFile(join(stateDir, UPLOAD_FORMAT_FILE), "utf8")).servers).toEqual({});
+ expect(await runTraces(stateDir, "fail")).toEqual([[2, false]]);
+ }, 30_000);
+
+ test("a fresh install whose first check fails sends schema 2", async () => {
+ expect(await runTraces(await temp("omnirush-format-state-"), "fail")).toEqual([[2, false]]);
+ }, 30_000);
+});
+
+describe("UploadFormat (context/upload-format.ts)", () => {
+ function clock() {
+ let t = 1_000_000;
+ return { now: () => t, advance: (ms: number) => { t += ms; } };
+ }
+
+ test("401, 500 or a network error after an earlier success stays schema 3; asked again after RETRY_MS", async () => {
+ for (const failure of [new Error("status 401"), new Error("status 500"), new TypeError("fetch failed")]) {
+ const time = clock();
+ let fail = false;
+ let asked = 0;
+ const format = new UploadFormat({ file: null, server: () => "gateway", now: time.now, probe: async () => { asked += 1; if (fail) throw failure; return SCHEMA_3; } });
+ expect(await format.version()).toBe(3);
+ fail = true;
+ time.advance(RECHECK_MS + 1);
+ expect(await format.version()).toBe(3);
+ time.advance(RETRY_MS - 1);
+ expect(await format.version()).toBe(3);
+ expect(asked).toBe(2);
+ time.advance(2);
+ expect(await format.version()).toBe(3);
+ expect(asked).toBe(3);
+ }
+ });
+
+ test("an explicit 422 unsupported_schema downgrades; nothing known and a failed check is schema 2", async () => {
+ const time = clock();
+ let fail = false;
+ const format = new UploadFormat({ file: null, server: () => "gateway", now: time.now, probe: async () => { if (fail) throw new Error("down"); return SCHEMA_3; } });
+ expect(await format.version()).toBe(3);
+ format.unsupported();
+ expect(await format.version()).toBe(2);
+ fail = true;
+ time.advance(RECHECK_MS + 1);
+ expect(await format.version()).toBe(2);
+ fail = false;
+ time.advance(RETRY_MS + 1);
+ expect(await format.version()).toBe(3);
+ });
+
+ test("remembered on disk per server for REMEMBER_MS", async () => {
+ const file = join(await temp("omnirush-format-file-"), UPLOAD_FORMAT_FILE);
+ const time = clock();
+ const down = async (): Promise<TraceCapabilities> => { throw new Error("503"); };
+ expect(await new UploadFormat({ file, server: () => "gateway", now: time.now, probe: async () => SCHEMA_3 }).version()).toBe(3);
+ expect(await new UploadFormat({ file, server: () => "gateway", now: time.now, probe: down }).version()).toBe(3);
+ expect(await new UploadFormat({ file, server: () => "other", now: time.now, probe: down }).version()).toBe(2);
+ time.advance(REMEMBER_MS + 1);
+ expect(await new UploadFormat({ file, server: () => "gateway", now: time.now, probe: down }).version()).toBe(2);
+ });
+});
+
+describe("the broker's capabilities for the uploader", () => {
+ function broker(answers: Array<number | "network" | "ok">) {
+ let probes = 0;
+ const instance = new OmniRushGatewayBroker({
+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "access-token", refreshToken: "refresh-token" },
+ engineToken: "local-engine-token",
+ fetch: async (input) => {
+ const pathname = new URL(String(input)).pathname;
+ if (pathname.endsWith("/collect/capabilities")) {
+ probes += 1;
+ const next = answers.shift() ?? "ok";
+ if (next === "network") throw new TypeError("fetch failed");
+ if (next === "ok") return Response.json(SCHEMA_3);
+ return new Response("{}", { status: next });
+ }
+ // The refresh cannot rotate right now (the account server is busy).
+ if (pathname.endsWith("/device/refresh")) return new Response("{}", { status: 503 });
+ return Response.json({ output: [] });
+ },
+ });
+ return { instance, probes: () => probes };
+ }
+
+ test("a failed check throws for the uploader (strict) instead of answering schema 2", async () => {
+ for (const failure of [401, 500, "network"] as const) {
+ const { instance } = broker([failure, failure, failure]);
+ await expect(instance.sessionUploadCapabilities({ strict: true })).rejects.toThrow();
+ }
+ });
+
+ test("a failed check is asked again after 30 s, not after 5 minutes", async () => {
+ const originalNow = Date.now;
+ let now = originalNow();
+ Date.now = () => now;
+ try {
+ const { instance, probes } = broker([500]);
+ await expect(instance.sessionUploadCapabilities({ strict: true })).rejects.toThrow();
+ const asked = probes();
+ // Within the retry window: no new request, and the other callers still read the oldest backend.
+ expect(await instance.sessionUploadCapabilities()).toEqual({ schema_versions: [1, 2], canonical_trace: false });
+ expect(probes()).toBe(asked);
+ now += 30_001;
+ expect((await instance.sessionUploadCapabilities({ strict: true })).canonical_trace).toBe(true);
+ expect(probes()).toBe(asked + 1);
+ } finally {
+ Date.now = originalNow;
+ }
+ });
+});