Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

Merge current desktop fixes and preserve single-owner account refresh in capture transport

Task Builderf9f77cc685d6
diff --cc apps/server/src/omnirush-gateway-broker.ts
index 0e2f1b51,cfc11b76..44b752e9
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@@ -3,8 -2,7 +3,8 @@@ import { createReadStream } from "node:
import { SESSION_UPLOAD_BUDGET, SESSION_UPLOAD_ENDPOINT_PATH, sessionUploadTimeoutMs, type SessionUploadBudget } from "./session-upload-budget.js";
import { externalFetch } from "./server-fetch.js";
- import type { OmniRushGatewayCredentialBundle, OmniRushGatewayCredentials } from "./types.js";
+ import type { OmniRushGatewayCredentials } from "./types.js";
+import type { TraceCapabilities } from "./session-uploader.js";
import {
SUBAGENT_FALLBACK_EFFORT_HEADER,
SUBAGENT_FALLBACK_MODEL_HEADER,
@@@ -806,11 -714,9 +801,10 @@@ function responseHeaders(headers: Heade
export class OmniRushGatewayBroker {
private state: CredentialState | null;
private readonly engineToken: string;
- private readonly persist?: OmniRushGatewayCredentials["persist"];
private readonly invalidate?: OmniRushGatewayCredentials["invalidate"];
- private readonly latest?: OmniRushGatewayCredentials["latest"];
+ private readonly refreshOwner?: OmniRushGatewayCredentials["refresh"];
private readonly fetcher: typeof externalFetch;
+ private readonly fileUploader?: BrokerOptions["uploadFile"];
private readonly log?: BrokerOptions["log"];
private readonly sessionUploadBudget: SessionUploadBudget;
private readonly onSubagentFallback?: BrokerOptions["onSubagentFallback"];
@@@ -819,14 -725,6 +813,8 @@@
private refreshInFlight: Promise<boolean> | null = null;
/** Whether the last failed refresh left the session as it was (unreachable, 5xx, contended). */
private lastRefreshTransient = false;
- /**
- * The pair this broker held before it adopted one from the store. Spent as
- * a last resort when the adopted pair turns out to be dead too, so a stale
- * store entry cannot sign out a session this broker still holds.
- */
- private previous: CredentialState | null = null;
+ private capabilityCache: { gatewayUrl: string; accountKey: string; version: 2 | 3; expiresAt: number } | null = null;
+ private capabilityProbe: { key: string; promise: Promise<2 | 3> } | null = null;
constructor(options: BrokerOptions) {
const gatewayUrl = options.credentials ? normalizedGatewayUrl(options.credentials.gatewayUrl) : null;
@@@ -838,12 -736,9 +826,10 @@@
}
: null;
this.engineToken = options.engineToken?.trim() ?? "";
- this.persist = options.credentials?.persist;
this.invalidate = options.credentials?.invalidate;
- this.latest = options.credentials?.latest;
+ this.refreshOwner = options.credentials?.refresh;
this.fetcher = options.fetch ?? externalFetch;
+ this.fileUploader = options.uploadFile;
this.log = options.log;
this.sessionUploadBudget = options.sessionUploadBudget ?? SESSION_UPLOAD_BUDGET;
this.onSubagentFallback = options.onSubagentFallback;
@@@ -1296,38 -1105,33 +1284,34 @@@
}
/**
- * Adopts a rotation performed by another holder of the same device session
- * (the desktop account store refreshes on its own when it checks the
- * profile). The server retires the previous refresh token on rotation and
- * answers it with 401, which this broker would otherwise read as a revoked
- * device and sign the user out. Only a pair rotated more often than this
- * broker's own is newer: a store that still holds the pair this broker
- * already rotated away from (its persist failed) must not win, or the
- * broker would spend a retired token while its own live one goes unused.
- * True when the state changed.
+ * The desktop account store owns the device session: it alone spends the
+ * refresh token, one rotation at a time, and saves the result before it
+ * answers. After a 401 the broker hands it the access token that was
+ * refused and takes the pair it gets back (one it rotated earlier, or a new
+ * rotation): the broker never sends a refresh token, and never acts on a
+ * pair of its own that can go stale. A rejection leaves the session as it
+ * is (the owner could not rotate right now); null means the session is gone.
*/
- private async adoptRotatedCredentials(): Promise<boolean> {
- const current = this.state;
- if (!this.latest || !current) return false;
- let stored: OmniRushGatewayCredentialBundle | null;
- try {
- stored = await this.latest();
- } catch {
+ private async refreshFromOwner(owner: NonNullable<OmniRushGatewayCredentials["refresh"]>, rejectedAccessToken: string): Promise<boolean> {
+ const asked = this.state;
+ if (!asked) return false;
+ const current = await owner(rejectedAccessToken);
+ if (this.state !== asked) return Boolean(this.state);
+ if (!current) {
+ this.state = null;
+ void this.invalidate?.().catch(() => undefined);
+ return false;
+ }
+ if (current.accessToken === rejectedAccessToken) {
+ this.lastRefreshTransient = true;
return false;
}
- if (this.state !== current || !stored?.accessToken || !stored.refreshToken) return false;
- if (stored.refreshToken === current.refreshToken) return false;
- const rotation = rotationOf(stored.rotation);
- if (rotation <= current.rotation) return false;
- this.previous = current;
this.state = {
- gatewayUrl: normalizedGatewayUrl(stored.gatewayUrl) ?? current.gatewayUrl,
- accessToken: stored.accessToken,
- refreshToken: stored.refreshToken,
- rotation,
+ gatewayUrl: normalizedGatewayUrl(current.gatewayUrl) ?? asked.gatewayUrl,
+ accessToken: current.accessToken,
+ refreshToken: current.refreshToken,
};
- if (this.state.gatewayUrl !== current.gatewayUrl) this.resetCapabilities();
- this.log?.("info", "omnirush.ai device credentials adopted from the account store", { rotation });
++ if (this.state.gatewayUrl !== asked.gatewayUrl) this.resetCapabilities();
return true;
}
@@@ -1345,27 -1147,9 +1327,10 @@@
const outcome = await this.rotate(spent);
if (outcome.kind === "rotated") return true;
this.lastRefreshTransient = outcome.kind === "unavailable" || outcome.kind === "contended";
- if (outcome.kind === "unavailable") return false;
+ if (outcome.kind !== "retired") return false;
if (this.state !== spent) return Boolean(this.state);
- // The token may have been spent elsewhere while this call was in flight;
- // a 409 means it is being spent right now. The store settles before it
- // answers, so a rotation that landed there is adopted instead.
- if (await this.adoptRotatedCredentials()) return true;
- if (outcome.kind === "contended") {
- this.log?.("warn", "omnirush.ai device refresh contended; keeping the session for the next attempt");
- return false;
- }
- const previous = this.previous;
- this.previous = null;
- if (previous && previous.refreshToken !== spent.refreshToken) {
- this.log?.("info", "omnirush.ai adopted device credentials rejected; trying the pair held before");
- const fallback = await this.rotate({ ...previous, rotation: Math.max(previous.rotation, spent.rotation) });
- if (fallback.kind === "rotated") return true;
- if (fallback.kind !== "retired") return false;
- if (this.state !== spent) return Boolean(this.state);
- }
this.state = null;
+ this.resetCapabilities();
void this.invalidate?.().catch(() => undefined);
return false;
}
@@@ -1391,24 -1175,7 +1356,8 @@@
const gatewayUrl = normalizedGatewayUrl(String(Reflect.get(payload, "gateway_url") ?? from.gatewayUrl));
if (typeof accessToken !== "string" || typeof refreshToken !== "string" || !gatewayUrl) return { kind: "unavailable" };
if (!this.state) return { kind: "unavailable" };
- const rotated = { accessToken, refreshToken, gatewayUrl, rotation: from.rotation + 1 };
- this.state = rotated;
- if (rotated.gatewayUrl !== from.gatewayUrl) this.resetCapabilities();
- this.previous = null;
- void this.persistLatest(rotated);
+ this.state = { accessToken, refreshToken, gatewayUrl };
++ if (gatewayUrl !== from.gatewayUrl) this.resetCapabilities();
return { kind: "rotated" };
}
-
- private async persistLatest(credentials: OmniRushGatewayCredentialBundle, attempt = 0): Promise<void> {
- if (!this.persist) return;
- try {
- await this.persist(credentials);
- } catch {
- if (attempt >= 5 || this.state?.refreshToken !== credentials.refreshToken) return;
- const timer = setTimeout(() => {
- void this.persistLatest(credentials, attempt + 1);
- }, Math.min(30_000, 1_000 * 2 ** attempt));
- timer.unref?.();
- }
- }
}