Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: claude access is checked at each turn and picked up without a restart

ogsamrat9be76627d787
diff --git a/apps/app/src/react-app/domains/session/surface/claude-models.ts b/apps/app/src/react-app/domains/session/surface/claude-models.ts
index 809c87c5..465b34cf 100644
--- a/apps/app/src/react-app/domains/session/surface/claude-models.ts
+++ b/apps/app/src/react-app/domains/session/surface/claude-models.ts
@@ -106,6 +106,9 @@ export async function setUpClaudeSandbox(client: Pick<OmniRushServerClient, "get
return box;
}
+/** How often the open project's Claude status is read again. */
+export const CLAUDE_STATUS_POLL_MS = 30_000;
+
type StatusClient = { claude: Pick<OmniRushServerClient["claude"], "request"> };
/** The open project's Claude models, read from its server (again when the window comes back). */
@@ -127,7 +130,15 @@ export function useClaudeModelSync(client: StatusClient | null, workspaceId: str
const onVisible = () => { if (document.visibilityState === "visible") load(); };
load();
document.addEventListener("visibilitychange", onVisible);
- return () => { closed = true; document.removeEventListener("visibilitychange", onVisible); };
+ window.addEventListener("focus", load);
+ // Access given or taken away while the app runs shows without a restart (the server caches its answer 30 s).
+ const timer = setInterval(() => { if (document.visibilityState === "visible") load(); }, CLAUDE_STATUS_POLL_MS);
+ return () => {
+ closed = true;
+ clearInterval(timer);
+ window.removeEventListener("focus", load);
+ document.removeEventListener("visibilitychange", onVisible);
+ };
}, [client, workspaceId]);
}
diff --git a/apps/server/src/claude-code/access.test.ts b/apps/server/src/claude-code/access.test.ts
new file mode 100644
index 00000000..713e7df9
--- /dev/null
+++ b/apps/server/src/claude-code/access.test.ts
@@ -0,0 +1,49 @@
+// Claude access is read fresh at a turn's start (a grant or a removal counts at once, no restart) and a 403 from the
+// token mint forgets the cached answer and grants.
+import { expect, test } from "bun:test";
+
+import { ClaudeAccessError, ClaudeDeviceAccess } from "./access.js";
+
+function server() {
+ const state = { models: ["claude-opus-5-5"] as string[], capabilitiesDown: false, mintStatus: 200, calls: [] as string[] };
+ const request = async (route: string) => {
+ state.calls.push(route);
+ if (route === "device/me") return Response.json({ id: "acct" });
+ if (route === "collect/capabilities") {
+ if (state.capabilitiesDown) return new Response(null, { status: 503 });
+ return Response.json({ claude_code: { enabled: true, models: state.models, catalog: [{ id: "claude-opus-5-5", display_name: "Claude Opus 5.5" }] } });
+ }
+ if (route === "device/claude-token") return new Response(null, { status: state.mintStatus });
+ throw new Error(route);
+ };
+ return { state, access: new ClaudeDeviceAccess(request, "desktop/test") };
+}
+
+test("a fresh ask sees access taken away or given at once; the cached one waits 30 s", async () => {
+ const { state, access } = server();
+ expect((await access.access()).models).toEqual(["claude-opus-5-5"]);
+ state.models = [];
+ expect((await access.access()).models).toEqual(["claude-opus-5-5"]);
+ expect((await access.access({ fresh: true })).models).toEqual([]);
+ state.models = ["claude-opus-5-5"];
+ expect((await access.access({ fresh: true })).models).toEqual(["claude-opus-5-5"]);
+});
+
+test("a fresh ask that fails keeps the last current answer", async () => {
+ const { state, access } = server();
+ await access.access();
+ state.capabilitiesDown = true;
+ expect((await access.access({ fresh: true })).models).toEqual(["claude-opus-5-5"]);
+});
+
+test("a 403 from the token mint forgets the cached access", async () => {
+ const { state, access } = server();
+ await access.access();
+ state.mintStatus = 403;
+ const context = { owner: "acct", sessionId: "ses_1", model: "claude-opus-5-5", workspaceId: "w", contractSha256: "c" };
+ await expect(access.authorize(context)).rejects.toBeInstanceOf(ClaudeAccessError);
+ state.models = [];
+ const before = state.calls.filter((call) => call === "collect/capabilities").length;
+ expect((await access.access()).models).toEqual([]);
+ expect(state.calls.filter((call) => call === "collect/capabilities").length).toBe(before + 1);
+});
diff --git a/apps/server/src/claude-code/access.ts b/apps/server/src/claude-code/access.ts
index 3988b56a..e11631e2 100644
--- a/apps/server/src/claude-code/access.ts
+++ b/apps/server/src/claude-code/access.ts
@@ -96,10 +96,11 @@ export class ClaudeDeviceAccess {
private capability?: {owner: string; expiresAt: number; models: string[]; catalog: ClaudeCatalogEntry[]; reason: string | null};
constructor(private readonly request: ClaudeDeviceRequest, private readonly client: string) {}
- async access(): Promise<ClaudeAccess> {
+ /** `fresh`: ask the server again now (a turn's start), not the answer of the last 30 s. */
+ async access(options: { fresh?: boolean } = {}): Promise<ClaudeAccess> {
const account = await read(await this.request("device/me"));
if (!record(account) || typeof account.id !== "string" || !account.id) throw new Error("Sign in to OmniRush to use Claude Code");
- if (!this.capability || this.capability.owner !== account.id || Date.now() >= this.capability.expiresAt) {
+ if (options.fresh || !this.capability || this.capability.owner !== account.id || Date.now() >= this.capability.expiresAt) {
let models: string[] = [], catalog: ClaudeCatalogEntry[] = [], reason: string | null = "unavailable";
try {
const value = await read(await this.request("collect/capabilities"));
@@ -114,11 +115,19 @@ export class ClaudeDeviceAccess {
if (code.enabled === true && Array.isArray(code.models)) models = [...new Set(code.models.filter((model): model is string => typeof model === "string" && CLAUDE_MODEL_ID.test(model)))];
if (code.enabled === true && !models.length) reason = "no_models";
}
- } catch { /* Failed eligibility checks deny new sessions, but never hide saved evidence. */ }
+ } catch {
+ // Failed eligibility checks deny new sessions, but never hide saved evidence. A fresh ask that failed keeps
+ // the account's last answer while it is still current.
+ if (this.capability?.owner === account.id && Date.now() < this.capability.expiresAt) return this.answer(account.id);
+ }
this.capability = {owner: account.id, expiresAt: Date.now() + 30_000, models, catalog, reason: models.length ? null : reason};
}
- const {models, catalog, reason} = this.capability;
- return { owner: account.id, capabilities: models.length ? ["claude-code"] : [], models, catalog, reason };
+ return this.answer(account.id);
+ }
+
+ private answer(owner: string): ClaudeAccess {
+ const {models, catalog, reason} = this.capability!;
+ return { owner, capabilities: models.length ? ["claude-code"] : [], models, catalog, reason };
}
async authorize(context: ClaudeAuthorization): Promise<ClaudeGrant> {
@@ -129,7 +138,17 @@ export class ClaudeDeviceAccess {
const existing = this.pending.get(key); if (existing) return existing;
const pending = this.mint(context, key);
this.pending.set(key, pending);
- try { return await pending; } finally { this.pending.delete(key); }
+ try { return await pending; } catch (error) {
+ // Access taken away: the next ask reads it again and the chat's other grants go.
+ if (error instanceof ClaudeAccessError && error.status === 403) this.forget(context.owner);
+ throw error;
+ } finally { this.pending.delete(key); }
+ }
+
+ /** Drops the cached access answer and an account's grants (a 403: its access changed). */
+ forget(owner?: string): void {
+ this.capability = undefined;
+ for (const key of this.grants.keys()) if (!owner || key.startsWith(`${owner}/`)) this.grants.delete(key);
}
private async mint(context: ClaudeAuthorization, key: string): Promise<ClaudeGrant> {
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index 691e6046..c20cdeb5 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -2019,7 +2019,7 @@ export class OmniRushGatewayBroker {
signal: AbortSignal.timeout(8_000),
});
});
- claudeAccess() { return this.claudeAccessClient().access(); }
+ claudeAccess(options: { fresh?: boolean } = {}) { return this.claudeAccessClient().access(options); }
/** Session-bound tokens stay in this host process, outside the renderer and Docker. */
async claudeGrant(context: ClaudeAuthorization) {
const client = this.claudeAccessClient(), grant = await client.authorize(context);
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 02642e62..9ba31ca8 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -1619,7 +1619,8 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
// Claude chats (claude-code/chat.ts): the engine's Claude provider asks /omnirush-claude/turn, with this key, for each turn.
const claudeKey = randomBytes(24).toString("base64url");
const claudeTurn = claudeChatRoute({
- access: () => gatewayBroker.claudeAccess(),
+ // Asked fresh at each turn: access taken away refuses at once (not after the relay's 403s), a grant needs no restart.
+ access: () => gatewayBroker.claudeAccess({ fresh: true }),
authorize: (context) => gatewayBroker.claudeGrant(context),
pin: (sessionId) => gatewayBroker.chatModel(sessionId),
setPin: (sessionId, model) => gatewayBroker.pinChatModel(sessionId, model),