A desktop coding agent with free access to frontier models.
TypeScript
fix: steadier assistant connection in the app
ogsamrat6b924a3e0141
diff --git a/apps/server/src/claude-code/native-capture.test.ts b/apps/server/src/claude-code/native-capture.test.tsindex a02bdbf5..f69dc97c 100644--- a/apps/server/src/claude-code/native-capture.test.ts+++ b/apps/server/src/claude-code/native-capture.test.ts@@ -90,6 +90,7 @@ test("session broker re-mints once on rejected authentication and never replaysconst root = fs.mkdtempSync(path.join(os.tmpdir(), "claude-broker-contract-")); roots.push(root);const id = "00000000-0000-4000-8000-000000000001", contractSha256 = "a".repeat(64);let status = 401, attempts = 0, successful = 0, authorizations = 0, refreshes = 0, first401 = true;+ let transportCalls = 0;const relay = Bun.serve({hostname: "127.0.0.1", port: 0, async fetch(req) {attempts++;expect(req.headers.get("x-api-key")).toMatch(/^orc_/);@@ -104,7 +105,13 @@ test("session broker re-mints once on rejected authentication and never replaysreturn Response.json({type: "error", error: {type: "api_error", message: "fixture"}}, {status,headers: {"x-should-retry": status === 429 ? "false" : "true", "retry-after": "30"}});}});- const runtime = new ClaudeRuntime({directory: root, surface: "gui", access: async () => ({owner: "owner", capabilities: ["claude-code"]}),+ const runtime = new ClaudeRuntime({fetch: (input, init) => {+ transportCalls++;+ expect(input).toBe(`http://127.0.0.1:${relay.port}/v1/messages`);+ expect(init?.redirect).toBe("error");+ expect(init?.signal?.aborted).toBe(false);+ return globalThis.fetch(input, init);+ }, directory: root, surface: "gui", access: async () => ({owner: "owner", capabilities: ["claude-code"]}),authorize: async context => {authorizations++; if (context.refresh) refreshes++;return {owner: "owner", sessionId: id, model: MODEL, upstreamModel: MODEL, client: "desktop/3.7.5",@@ -119,6 +126,7 @@ test("session broker re-mints once on rejected authentication and never replaystry {const accepted = await request(); expect(accepted.status).toBe(200); await accepted.arrayBuffer();expect({attempts, successful, authorizations, refreshes}).toEqual({attempts: 2, successful: 1, authorizations: 2, refreshes: 1});+ expect(transportCalls).toBe(2);const journal = fs.readFileSync(path.join(Reflect.get(runtime, "dir").call(runtime, "owner", id), "policy", "model-requests.jsonl"), "utf8").trim().split("\n").map(line => JSON.parse(line));expect(journal.map(event => event.type)).toEqual(["request", "provider_identity", "response"]);expect(journal.at(-1)).toMatchObject({status: 200, authorizationFailures: [401], completed: true});@@ -128,6 +136,7 @@ test("session broker re-mints once on rejected authentication and never replaysexpect(denied.headers.get("x-should-retry")).toBe(status === 429 ? "false" : "true");expect(denied.headers.get("retry-after")).toBe("30"); await denied.arrayBuffer();expect(attempts).toBe(before + 1); expect(refreshes).toBe(renewed); expect(successful).toBe(1);+ expect(transportCalls).toBe(attempts);}} finally { await broker.close(); relay.stop(true); }});@@ -601,7 +610,7 @@ function savedRuntimeFixture(access?: () => Promise<{owner: string; capabilitiesconst collector = new NativeCapture({directory: path.join(dir, "native"), configDir, sessionId: id, owner, producerVersion: CLAUDE_VERSION, surface: "gui", transport: "sdk"});collector.finish({type: "finish", processStopped: true, successfulResult: false});let accessCalls = 0, grants = 0;- const runtime = new ClaudeRuntime({directory: root, surface: "gui", docker: path.join(root, "docker-is-unavailable"),+ const runtime = new ClaudeRuntime({fetch: globalThis.fetch, directory: root, surface: "gui", docker: path.join(root, "docker-is-unavailable"),access: async () => { accessCalls++; if (access) return access(); throw new Error("account unavailable"); },authorize: async () => { grants++; throw new Error("must not mint"); }});return {root, dir, id, owner, raw, session, runtime, counts: () => ({accessCalls, grants})};@@ -668,7 +677,7 @@ function gate<T>() {test("shutdown fences startup blocked on access and never mints or starts Docker after access resolves", async () => {const root = fs.mkdtempSync(path.join(os.tmpdir(), "claude-startup-")); roots.push(root);const entered = gate<void>(), access = gate<{owner: string; capabilities: string[]}>(); let grants = 0, dockerCalls = 0;- const runtime = new ClaudeRuntime({directory: root, surface: "gui", access: () => { entered.resolve(); return access.promise; }, authorize: async () => { grants++; throw new Error("must not mint"); }});+ const runtime = new ClaudeRuntime({fetch: globalThis.fetch, directory: root, surface: "gui", access: () => { entered.resolve(); return access.promise; }, authorize: async () => { grants++; throw new Error("must not mint"); }});Reflect.set(runtime, "docker", async () => { dockerCalls++; throw new Error("must not start Docker"); });const creation = runtime.create(root, "workspace").then(() => "created", () => "cancelled");await entered.promise;@@ -723,7 +732,7 @@ function startupDockerFixture(stage: "build" | "create" | "cp" | "exec" | "restoconst root = fs.mkdtempSync(path.join(os.tmpdir(), "claude-docker-fixture-")); roots.push(root);const workspace = path.join(root, "workspace"); fs.mkdirSync(workspace);const docker = path.join(root, "docker-fixture.mjs"), ready = path.join(root, "ready"), release = path.join(root, "release"), log = path.join(root, "calls.jsonl");- const runtime = new ClaudeRuntime({directory: path.join(root, "state"), surface: "gui", docker,+ const runtime = new ClaudeRuntime({fetch: globalThis.fetch, directory: path.join(root, "state"), surface: "gui", docker,access: async () => ({owner: "local-owner", capabilities: ["claude-code"]}),authorize: async () => { grants++; throw new Error("must not authorize after cancellation"); }});let grants = 0;@@ -814,7 +823,7 @@ test("broker cancellation during account access prevents both mint and providerlet grants = 0, providerCalls = 0;const relay = Bun.serve({hostname: "127.0.0.1", port: 0, fetch() { providerCalls++; return Response.json({type: "message", model: MODEL, content: [], stop_reason: "end_turn"}); }});const id = "00000000-0000-4000-8000-000000000001", contractSha256 = "a".repeat(64);- const runtime = new ClaudeRuntime({directory: root, surface: "gui", access: () => { entered.resolve(); return access.promise; },+ const runtime = new ClaudeRuntime({fetch: globalThis.fetch, directory: root, surface: "gui", access: () => { entered.resolve(); return access.promise; },authorize: async () => { grants++; return {owner: "owner", sessionId: id, model: MODEL, upstreamModel: MODEL, client: "desktop/3.7.5", baseUrl: `http://127.0.0.1:${relay.port}`, token: "orc_" + "a".repeat(43), contractSha256}; }});const broker = await Reflect.get(runtime, "broker").call(runtime, {owner: "owner", id, model: MODEL, workspaceId: "workspace", contract: {}, contractSha256, turns: []}, () => {}, controller.signal);try {@@ -872,7 +881,7 @@ fs.renameSync=function(from,to){}return result;};-const runtime=new ClaudeRuntime({directory:root,surface:'gui',docker:path.join(root,'no-docker'),access:async()=>{throw new Error('offline');},authorize:async()=>{throw new Error('must not mint');}});+const runtime=new ClaudeRuntime({fetch: globalThis.fetch, directory:root,surface:'gui',docker:path.join(root,'no-docker'),access:async()=>{throw new Error('offline');},authorize:async()=>{throw new Error('must not mint');}});try{const zip=await runtime.export(id);fs.writeFileSync(path.join(root,'result-'+name),JSON.stringify({status:'exported',zip}));}catch(error){fs.writeFileSync(path.join(root,'result-'+name),JSON.stringify({status:'rejected',message:error.message}));}`);diff --git a/apps/server/src/claude-code/runtime.ts b/apps/server/src/claude-code/runtime.tsindex bf4a3a15..b587ecf9 100644--- a/apps/server/src/claude-code/runtime.ts+++ b/apps/server/src/claude-code/runtime.ts@@ -36,6 +36,7 @@ export type ClaudeGrant = {};export type RuntimeOptions = {directory: string; surface: "cli" | "gui";+ fetch: (input: string, init?: RequestInit) => Promise<Response>;access: () => Promise<ClaudeAccess>;authorize: (session: ClaudeAuthorization) => Promise<ClaudeGrant>;revoke?: (session: ClaudeAuthorization) => Promise<void>;@@ -595,7 +596,7 @@ export class ClaudeRuntime {if (inference) isolation.inferenceRequests++;admitted = true;this.modelEvidence(session, {type: "request", requestId, route: inference ? "messages" : "count_tokens", requestedModel: body.model, resolvedModel: grant.upstreamModel, binding: session.authorization?.binding, contractSha256: session.contractSha256, policyVersion: grant.policyVersion ?? null, budget: grant.budget ?? null, transformations, originalBodySha256: createHash("sha256").update(originalBody).digest("hex"), forwardedBodySha256: digest(forwardedBody)});- let upstream = await fetch(grant.baseUrl + req.url, {method: "POST", headers, body: forwardedBody, redirect: "error", signal: abort.signal});+ let upstream = await this.options.fetch(grant.baseUrl + req.url, {method: "POST", headers, body: forwardedBody, redirect: "error", signal: abort.signal});if (upstream.status === 401 && !abort.signal.aborted) {// A rejected token has not admitted inference. Re-mint exactly once;// never replay a started stream, a quota refusal, or a provider error.@@ -604,7 +605,7 @@ export class ClaudeRuntime {grant = await this.grant(session, true, abort.signal);interrupted(abort.signal);headers["x-api-key"] = grant.token;- upstream = await fetch(grant.baseUrl + req.url, {method: "POST", headers, body: forwardedBody, redirect: "error", signal: abort.signal});+ upstream = await this.options.fetch(grant.baseUrl + req.url, {method: "POST", headers, body: forwardedBody, redirect: "error", signal: abort.signal});}responseStatus = upstream.status; res.statusCode = upstream.status;for (const [name, value] of Object.entries(claudeResponseHeaders(upstream.headers))) res.setHeader(name, value);diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 708786da..ae4272b3 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -1540,7 +1540,7 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {);const claudeRuntime = new ClaudeRuntime({- directory: join(runtimeStorageDir(config), "claude-sessions"), surface: "gui",+ directory: join(runtimeStorageDir(config), "claude-sessions"), surface: "gui", fetch: externalFetch,access: () => gatewayBroker.claudeAccess(),authorize: context => gatewayBroker.claudeGrant(context),revoke: context => gatewayBroker.revokeClaudeGrant(context),