From 32cb807e7f5470e7c788c730691cfd13dae875a4 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:05:39 +0000 Subject: [PATCH 01/32] Never block setup on how many subscriptions the proxy holds With fewer working subscriptions than hub.proxyAccounts, none included, the cli-proxy step installs and starts the proxy and passes. hedge apply and hedge doctor show a To do line with the login command, for the Claude and the Codex pool, and nothing fails. A pack check can now name a todo. When its command exits 4, Hedge prints the todo as a To do line, keeps the config that waits for the check out, and fails neither apply nor doctor. Apply runs such a check after the steps, so it can read what a step installed. hedge cli-proxy check exits 4 while the proxy answers but serves no model for the agent, so each machine stays on its own sign-in until the pool has a subscription. The new hedge cli-proxy accounts counts the working subscriptions. A proxy that is down, refuses the key, or runs a foreign plugin still fails as before. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 18 ++++ packs/hub/SETUP.md | 6 ++ packs/hub/commands/cli-proxy.ts | 4 +- packs/hub/hub.test.ts | 57 ++++++++++++- packs/hub/lib/cli-proxy-testing.ts | 50 +++++++++++ packs/hub/lib/cli-proxy.test.ts | 128 ++++++++++++++++------------ packs/hub/lib/cli-proxy.ts | 46 +++++++--- packs/hub/pack.json | 40 ++++++++- packs/hub/skills/hedge-hub/SKILL.md | 22 +++-- packs/recovery/SETUP.md | 7 +- src/cli.test.ts | 25 +++++- src/cli.ts | 5 +- src/pack/manifest.test.ts | 6 ++ src/pack/manifest.ts | 1 + src/pack/types.ts | 9 +- src/reconciler/apply.test.ts | 42 +++++++++ src/reconciler/apply.ts | 34 +++++--- 17 files changed, 401 insertions(+), 99 deletions(-) create mode 100644 packs/hub/lib/cli-proxy-testing.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a4c66303..3b32c7d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ ## Unreleased +### Setup never waits for proxy subscriptions + +- On a hub with own-account balancing, `hedge apply` and `hedge setup` no + longer fail while the proxy holds fewer of your own Claude or Codex + subscriptions than `hub.proxyAccounts`, none included. The proxy installs + and runs, and `hedge apply` and `hedge doctor` show a `To do:` line with + the login command. `hedge cli-proxy accounts` counts the subscriptions + that work. A subscription whose login expired still does not count, and + `hedge cli-proxy doctor` still warns about it. +- Each machine keeps Claude Code and Codex on their own sign-ins until the + proxy serves them. While the proxy answers but holds none of an agent's + subscriptions, its route check is a `To do:` line, not a failure. A proxy + that is down, refuses the key, or runs a plugin Hedge did not install + still fails `hedge doctor`. +- A pack check can name a `todo`: when its command exits 4, Hedge prints + it as a `To do:` line, keeps the config that waits for the check out, and + fails neither apply nor doctor. Apply runs such a check after the steps. + ### Hedge is cloud only - Hedge runs your agents on a hub and one or more nodes in the cloud. The diff --git a/packs/hub/SETUP.md b/packs/hub/SETUP.md index e85f4d4f..7380ffb9 100644 --- a/packs/hub/SETUP.md +++ b/packs/hub/SETUP.md @@ -18,5 +18,11 @@ Every machine keeps working when the hub is down. own-account balancing, so `packs/recovery/SETUP.md` turns it on when they pick Recovery. Proxy panel needs own-account balancing too; turn it on only when the person asks for the proxy's web panel. + With own-account balancing on, the hub installs and runs the proxy even + while it holds none of their subscriptions, and setup goes on. Each + machine keeps its own sign-in until the proxy serves the agent. Until the + proxy holds `hub.proxyAccounts` of their subscriptions (2 by default), + `hedge doctor` on the hub shows a `To do:` line with the login command. + Relay that line; it does not fail setup. 4. Each node joins when `hedge devbox add` adds it. The hub takes the new node at its next `hedge update`, or at once when it answers. diff --git a/packs/hub/commands/cli-proxy.ts b/packs/hub/commands/cli-proxy.ts index d16ca6e7..9acbfa94 100644 --- a/packs/hub/commands/cli-proxy.ts +++ b/packs/hub/commands/cli-proxy.ts @@ -7,7 +7,7 @@ import { run } from "../lib/run.ts"; import { tunnelTargets } from "../lib/tunnels.ts"; import { numberSetting, stringSetting } from "../../base/lib/settings.ts"; -const modes = ["plan", "install", "doctor", "catalog", "connect", "disconnect", "check", "remove", "left"]; +const modes = ["plan", "install", "doctor", "accounts", "catalog", "connect", "disconnect", "check", "remove", "left"]; const plans = ["claude", "codex", "both"]; const version = "8.0.46"; const archives: Record = { @@ -39,7 +39,7 @@ function panel(historyDays: number): Panel { await runCommand(() => { const [mode = "plan", ...rest] = Bun.argv.slice(2); const agent = mode === "check" && rest.length === 1 && rest[0] === "codex" ? "codex" : rest.length ? undefined : "claude"; - if (!agent || !modes.includes(mode)) return console.error("Usage: hedge cli-proxy [plan|install|doctor|catalog|check [codex]|disconnect|remove|left], or hedge cli-proxy connect < key file"), 2; + if (!agent || !modes.includes(mode)) return console.error("Usage: hedge cli-proxy [plan|install|doctor|accounts|catalog|check [codex]|disconnect|remove|left], or hedge cli-proxy connect < key file"), 2; const days = panelDays(Bun.env); return cliProxy(mode as ProxyMode, { home: homedir(), uid: process.getuid?.() ?? 0, user: userInfo().username, ...(plans.includes(Bun.env.HEDGE_PLAN ?? "") ? { plan: Bun.env.HEDGE_PLAN as AiPlan } : {}), ...(proxyOn(Bun.env) ? { accounts: numberSetting("hub.proxyAccounts") } : {}), ...(days === undefined ? {} : { panel: panel(days) }), diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 0fa93b75..6eeeadb3 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -6,6 +6,7 @@ import { providerConfigAdapter } from "../../src/adapters/provider-config/adapte import { apply, check } from "../../src/reconciler/apply.ts"; import { boxCommand } from "./lib/box-command.ts"; import { cliProxy, codexProviderTable, type ProxyMachine } from "./lib/cli-proxy.ts"; +import { proxyMachine } from "./lib/cli-proxy-testing.ts"; import { Coder } from "./lib/coder.ts"; import { fakeMachine } from "./lib/testing.ts"; import type { AvailableSource, MachineFile, MachineRole, Plan, Profile } from "../../src/pack/types.ts"; @@ -267,7 +268,7 @@ describe("the Own-account balancing workflow", () => { test("with it on, the hub gets the proxy, its balancer, and the route; a devbox gets only the route; the Mac gets nothing", () => { const route = { checks: ["cli-proxy-client", "cli-proxy-codex"], config: ["claude:env.ANTHROPIC_BASE_URL", "claude:apiKeyHelper", ...codexRoute] }; - expect(proxyItems(plan(balancing, hub))).toEqual({ jobs: ["cli-proxy-balance: hedge cli-proxy-balance every 5m"], steps: ["cli-proxy"], ...route }); + expect(proxyItems(plan(balancing, hub))).toEqual({ jobs: ["cli-proxy-balance: hedge cli-proxy-balance every 5m"], steps: ["cli-proxy"], ...route, checks: ["cli-proxy-accounts", ...route.checks] }); expect(proxyItems(plan(balancing, devbox))).toEqual({ jobs: [], steps: [], ...route }); expect(proxyItems(plan(balancing, mac))).toEqual(nothing); expect(plan(balancing, hub).settings["hub.proxyAccounts"]).toBe(2); @@ -410,4 +411,58 @@ describe("the Own-account balancing workflow", () => { expect(codex.model_provider).toBe("hedge-proxy"); }); }); + + describe("apply on a hub whose proxy holds none of your subscriptions", () => { + const proxyOnly = (result: Plan): Plan => ({ ...result, files: [], jobs: [], human: [], steps: result.steps.filter((step) => step.id === "cli-proxy"), checks: result.checks.filter((check) => check.workflow === "own-account-balancing"), config: result.config.filter((entry) => entry.workflow === "own-account-balancing") }); + const hedgeProxy = (machine: ProxyMachine) => async (command: string[]) => { + const words = command.join(" "); + const mode = (["install", "doctor", "accounts", "check codex", "check"] as const).find((name) => words.endsWith(`hedge cli-proxy ${name}`)); + if (!mode) return 0; + return cliProxy(mode === "check codex" ? "check" : mode, machine, mode === "check codex" ? "codex" : "claude").catch(() => 1); + }; + const emptyHub = (ai: "claude" | "codex", credentials: Array<{ provider: string }> = []) => { + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: ai, devboxes: [] }, { credentials, models: [] }); + const result = proxyOnly(plan({ ...balancing, plan: ai }, hub)); + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine) }; + return { home, result, options }; + }; + const login = (agent: string) => `~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -${agent}-login -no-browser`; + + test("installs the proxy, passes its step, keeps Claude Code on its own sign-in, and lists the login as a To do; doctor passes too", async () => { + const { home, result, options } = emptyHub("claude"); + const fresh = await apply(result, options); + expect(fresh.ok).toBe(true); + expect(fresh.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-client", "apply"], ["check:cli-proxy-accounts", "todo"]]); + expect(fresh.findings[1]!.say).toContain(`Run on the hub, once for each subscription: ${login("claude")}.`); + expect(existsSync(join(home, ".local/share/hedge-cli-proxy/cli-proxy-api"))).toBe(true); + const applied = await apply(result, options); + expect(applied.ok).toBe(true); + expect(applied.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-client", "todo"], ["check:cli-proxy-accounts", "todo"]]); + expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); + const doctor = await check(result, options); + expect(doctor.ok).toBe(true); + expect(doctor.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); + }); + + test("on the Codex plan, lists the Codex login as a To do and keeps Codex on its own sign-in", async () => { + const { home, result, options } = emptyHub("codex"); + expect((await apply(result, options)).ok).toBe(true); + const applied = await apply(result, options); + expect(applied.ok).toBe(true); + expect(applied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-codex", true], ["check:cli-proxy-codex-accounts", true]]); + expect(applied.findings.map((finding) => finding.say).join("\n")).toContain(login("codex")); + expect(existsSync(join(home, ".codex/config.toml"))).toBe(false); + expect((await check(result, options)).ok).toBe(true); + }); + + test("with one subscription of the 2 that hub.proxyAccounts asks for, apply routes Claude Code to the proxy that serves it and still lists the To do", async () => { + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, { credentials: [{ provider: "claude" }] }); + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine) }; + const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); + await apply(result, options); + const applied = await apply(result, options); + expect(applied).toMatchObject({ ok: true, findings: [{ id: "check:cli-proxy-accounts", todo: true }] }); + expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).env.ANTHROPIC_BASE_URL).toBe("http://127.0.0.1:8317"); + }); + }); }); diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts new file mode 100644 index 00000000..e3ebd5f9 --- /dev/null +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -0,0 +1,50 @@ +import { existsSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { proxyUrl, type ProxyMachine } from "./cli-proxy.ts"; +import { fakeRun, roleHome } from "./run-testing.ts"; + +export const clientKey = "c1".repeat(32); +export const otherKey = "d2".repeat(32); +export const archive = "fake release archive"; +export const release = { version: "9.9.9", url: "https://example.com/proxy.tar.gz", sha256: new Bun.CryptoHasher("sha256").update(archive).digest("hex") }; +export const dataDir = (home: string) => join(home, ".local/share/hedge-cli-proxy"); + +export type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean; refuses?: boolean }; + +export function fakeProxy(proxy: Proxy = {}) { + const requests: Array<{ path: string; method: string; auth: string }> = []; + const fetch = async (input: string | URL | Request, init?: RequestInit) => { + const url = new URL(String(input)); + const auth = new Headers(init?.headers).get("authorization") ?? ""; + requests.push({ path: url.pathname, method: init?.method ?? "GET", auth }); + if (proxy.catalog !== undefined && url.pathname.endsWith("/codex_client_models.json")) return Response.json(proxy.catalog); + if (proxy.registry !== undefined && url.pathname.endsWith("/models.json")) return Response.json(proxy.registry); + if (proxy.down || url.origin !== proxyUrl) throw new TypeError("connection refused"); + if (url.pathname === "/v1/models") return auth === `Bearer ${clientKey}` && !proxy.refuses ? Response.json({ data: (proxy.models ?? ["claude-sonnet-5-5"]).map((id) => ({ id })) }) : new Response("no", { status: 401 }); + if (url.pathname === "/v0/management/routing/strategy") return Response.json({ strategy: proxy.strategy ?? "fill-first" }); + if (url.pathname === "/v0/management/auth-files") return Response.json({ files: proxy.credentials ?? [{ provider: "claude" }, { provider: "claude" }] }); + return new Response("missing", { status: 404 }); + }; + return { fetch, requests }; +} + +/** A ProxyMachine whose commands, CLIProxyAPI and its management API are fakes, with the state the proxy would hold. */ +export function proxyMachine(make: (prefix: string) => string, fields: Partial = {}, proxy: Proxy = {}, options: { role?: string; checksum?: string } = {}) { + const home = fields.home ?? roleHome(make("hedge-cli-proxy-"), options.role ?? "hub"); + const fake = fakeRun((command) => { + if (command[0] === "curl") writeFileSync(command[command.indexOf("-o") + 1]!, options.checksum ?? archive); + if (command[0] === "install") writeFileSync(command.at(-1)!, "binary"); + const installed = existsSync(join(dataDir(home), "cli-proxy-api")); + if (command[0]!.endsWith("cli-proxy-api")) return { stdout: installed ? `CLIProxyAPI Version: ${release.version}, Commit: x\n` : "", code: installed ? 0 : 127 }; + if (command[0] === "ss") return { stdout: "LISTEN 0 4096 127.0.0.1:8317 0.0.0.0:*\n" }; + return undefined; + }); + const { fetch, requests } = fakeProxy(proxy); + const printed: string[] = []; + const keys = [clientKey, otherKey]; + const machine: ProxyMachine = { + home, uid: 1000, user: "me", accounts: 2, devboxes: ["devbox-1", "box-lane"], release, run: fake.run, fetch, now: () => 1_800_000_000_000, sleep: async () => {}, + readStdin: async () => `${clientKey}\n`, randomKey: () => keys.shift()!, print: (line) => printed.push(line), ...fields, + }; + return { machine, home, calls: fake.calls, inputs: fake.inputs, requests, printed }; +} diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 89fbdd70..f4451145 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -4,16 +4,13 @@ import { join } from "node:path"; import { parse, stringify } from "smol-toml"; import { tempDirs } from "../../../src/testing/git.ts"; import { claudeRoute, cliProxy, keyHelper, proxyUrl, type ProxyMachine } from "./cli-proxy.ts"; +import { archive, clientKey, dataDir, otherKey, proxyMachine, release, type Proxy } from "./cli-proxy-testing.ts"; import { fakeRun, modeOf, roleHome } from "./run-testing.ts"; +import { waitsForPerson } from "../../../src/pack/types.ts"; const temp = tempDirs(); afterEach(() => temp.cleanup()); -const clientKey = "c1".repeat(32); -const otherKey = "d2".repeat(32); -const archive = "fake release archive"; -const release = { version: "9.9.9", url: "https://example.com/proxy.tar.gz", sha256: new Bun.CryptoHasher("sha256").update(archive).digest("hex") }; -const dataDir = (home: string) => join(home, ".local/share/hedge-cli-proxy"); const keyFile = (home: string) => join(home, ".config/hedge/cli-proxy.key"); const settingsFile = (home: string) => join(home, ".claude/settings.json"); const unitFile = (home: string) => join(home, ".config/systemd/user/hedge-cli-proxy.service"); @@ -28,44 +25,7 @@ function codexRouted(home: string, config: Record = { ...mine, writeFileSync(codexFile(home), stringify(config)); } -type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean }; - -function fakeProxy(proxy: Proxy = {}) { - const requests: Array<{ path: string; method: string; auth: string }> = []; - const fetch = async (input: string | URL | Request, init?: RequestInit) => { - const url = new URL(String(input)); - const auth = new Headers(init?.headers).get("authorization") ?? ""; - requests.push({ path: url.pathname, method: init?.method ?? "GET", auth }); - if (proxy.catalog !== undefined && url.pathname.endsWith("/codex_client_models.json")) return Response.json(proxy.catalog); - if (proxy.registry !== undefined && url.pathname.endsWith("/models.json")) return Response.json(proxy.registry); - if (proxy.down || url.origin !== proxyUrl) throw new TypeError("connection refused"); - if (url.pathname === "/v1/models") return auth === `Bearer ${clientKey}` ? Response.json({ data: (proxy.models ?? ["claude-sonnet-5-5"]).map((id) => ({ id })) }) : new Response("no", { status: 401 }); - if (url.pathname === "/v0/management/routing/strategy") return Response.json({ strategy: proxy.strategy ?? "fill-first" }); - if (url.pathname === "/v0/management/auth-files") return Response.json({ files: proxy.credentials ?? [{ provider: "claude" }, { provider: "claude" }] }); - return new Response("missing", { status: 404 }); - }; - return { fetch, requests }; -} - -function hubMachine(fields: Partial = {}, proxy: Proxy = {}, options: { role?: string; checksum?: string } = {}) { - const home = fields.home ?? roleHome(temp.make("hedge-cli-proxy-"), options.role ?? "hub"); - const fake = fakeRun((command) => { - if (command[0] === "curl") writeFileSync(command[command.indexOf("-o") + 1]!, options.checksum ?? archive); - if (command[0] === "install") writeFileSync(command.at(-1)!, "binary"); - const installed = existsSync(join(dataDir(home), "cli-proxy-api")); - if (command[0]!.endsWith("cli-proxy-api")) return { stdout: installed ? `CLIProxyAPI Version: ${release.version}, Commit: x\n` : "", code: installed ? 0 : 127 }; - if (command[0] === "ss") return { stdout: "LISTEN 0 4096 127.0.0.1:8317 0.0.0.0:*\n" }; - return undefined; - }); - const { fetch, requests } = fakeProxy(proxy); - const printed: string[] = []; - const keys = [clientKey, otherKey]; - const machine: ProxyMachine = { - home, uid: 1000, user: "me", accounts: 2, devboxes: ["devbox-1", "box-lane"], release, run: fake.run, fetch, now: () => 1_800_000_000_000, sleep: async () => {}, - readStdin: async () => `${clientKey}\n`, randomKey: () => keys.shift()!, print: (line) => printed.push(line), ...fields, - }; - return { machine, home, calls: fake.calls, inputs: fake.inputs, requests, printed }; -} +const hubMachine = (fields: Partial = {}, proxy: Proxy = {}, options: { role?: string; checksum?: string } = {}) => proxyMachine((prefix) => temp.make(prefix), fields, proxy, options); const client = (fields: Partial = {}, proxy: Proxy = {}) => hubMachine(fields, proxy, { role: "devbox" }); @@ -189,11 +149,39 @@ test("the doctor passes with enough of your own subscriptions, and prints how to expect(doctor.printed.join("\n")).not.toContain(clientKey); }); -test("the doctor names the login command while the proxy holds too few subscriptions, and asks for the balancer when routing is not fill-first", async () => { +const login = (home: string, agent: string) => `${join(dataDir(home), "cli-proxy-api")} -config ${join(dataDir(home), "config.yaml")} -${agent}-login -no-browser`; + +test("the doctor passes while the proxy holds too few subscriptions, none included, and names the login command as a To do", async () => { const { machine, home } = hubMachine(); await cliProxy("install", machine); const few = hubMachine({ home }, { credentials: [{ provider: "claude" }, { provider: "claude", disabled: true }, { provider: "codex" }] }); - await expect(cliProxy("doctor", few.machine)).rejects.toThrow(`Add at least 2 of your own Claude subscriptions that work; found 1. Run: ${join(dataDir(home), "cli-proxy-api")} -config ${join(dataDir(home), "config.yaml")} -claude-login -no-browser`); + expect(await cliProxy("doctor", few.machine)).toBe(0); + expect(few.printed).toContain(`CLI proxy: To do: Add at least 2 of your own Claude subscriptions that work to the proxy; found 1. Run on the hub, once for each subscription: ${login(home, "claude")}`); + const none = hubMachine({ home }, { credentials: [], models: [] }); + expect(await cliProxy("doctor", none.machine)).toBe(0); + expect(none.printed).toContain(`CLI proxy: To do: Add at least 2 of your own Claude subscriptions that work to the proxy; found 0. Until the proxy holds one, Claude Code keeps its own sign-in on each machine. Run on the hub, once for each subscription: ${login(home, "claude")}`); + expect(none.printed.join("\n")).toContain("with 0 of your own Claude subscriptions"); + const enough = hubMachine({ home }); + expect(await cliProxy("doctor", enough.machine)).toBe(0); + expect(enough.printed.join("\n")).not.toContain("To do"); +}); + +test("the doctor still fails with an empty pool while the proxy does not answer its key, its service is down, or routing is not fill-first", async () => { + const { machine, home } = hubMachine(); + await cliProxy("install", machine); + const refused = hubMachine({ home }, { credentials: [], models: [], refuses: true }); + await expect(cliProxy("doctor", refused.machine)).rejects.toThrow(`The proxy at ${proxyUrl} refuses this machine's key (HTTP 401).`); + const silent = hubMachine({ home }, { credentials: [], models: [], down: true }); + await expect(cliProxy("doctor", silent.machine)).rejects.toThrow(); + expect(silent.requests.some((request) => request.path === "/v0/management/routing/strategy")).toBe(true); + const installed = hubMachine({ home }); + const stopped = hubMachine({ home, run: async (command) => (command.includes("is-active") ? { code: 3, stdout: "", stderr: "inactive" } : installed.machine.run(command)) }, { credentials: [], models: [] }); + await expect(cliProxy("doctor", stopped.machine)).rejects.toThrow("is-active"); + mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); + writeFileSync(join(dataDir(home), "plugins/other.so"), "x"); + const foreign = hubMachine({ home }, { credentials: [], models: [] }); + await expect(cliProxy("doctor", foreign.machine)).rejects.toThrow("Remove the plugins Hedge did not install"); + rmSync(join(dataDir(home), "plugins"), { recursive: true }); const spread = hubMachine({ home }, { strategy: "round-robin" }); await expect(cliProxy("doctor", spread.machine)).rejects.toThrow("hedge cli-proxy-balance"); }); @@ -224,13 +212,37 @@ test("the doctor passes but warns about each subscription whose login expired, b expect(doctor.printed.join("\n")).toContain("with 4 of your own Claude subscriptions and 0 Codex subscriptions"); }); -test("the doctor fails when too few subscriptions work, and counts none whose login expired", async () => { +test("too few subscriptions that work is a To do, which counts none whose login expired, in the doctor and in accounts", async () => { const { machine, home } = hubMachine(); await cliProxy("install", machine); const expired = '{"error":"invalid_grant"}'; - const credentials = [{ name: "a.json", provider: "claude" }, { name: "b.json", provider: "claude", status: "error", status_message: expired }]; + const credentials = [{ name: "a.json", provider: "claude" }, { name: "b.json", auth_index: "bbbbcccc", provider: "claude", status: "error", status_message: expired }]; + const todo = `CLI proxy: To do: Add at least 2 of your own Claude subscriptions that work to the proxy; found 1, and 1 whose login expired, which you can sign in again. Run on the hub, once for each subscription: ${login(home, "claude")}`; const doctor = hubMachine({ home }, { credentials }); - await expect(cliProxy("doctor", doctor.machine)).rejects.toThrow(`Add at least 2 of your own Claude subscriptions that work; found 1, and 1 whose login expired, which you can sign in again. Run: ${join(dataDir(home), "cli-proxy-api")} -config ${join(dataDir(home), "config.yaml")} -claude-login -no-browser`); + expect(await cliProxy("doctor", doctor.machine)).toBe(0); + expect(doctor.printed).toContain(todo); + expect(doctor.printed).toContain(`CLI proxy: Warning: the Claude subscription bbbbcccc cannot refresh its login, so the proxy cannot use it and the balancer cannot read its usage. Sign it in again with: ${login(home, "claude")}. Or disable it in the management panel.`); + const counted = hubMachine({ home }, { credentials }); + expect(await cliProxy("accounts", counted.machine)).toBe(waitsForPerson); + expect(counted.printed).toEqual([todo]); +}); + +test("accounts waits for the person below hub.proxyAccounts, passes at it, fails when it cannot count, and runs only on the hub", async () => { + const { machine, home } = hubMachine(); + await cliProxy("install", machine); + const none = hubMachine({ home }, { credentials: [] }); + expect(await cliProxy("accounts", none.machine)).toBe(waitsForPerson); + expect(none.printed.join("\n")).toContain("found 0. Until the proxy holds one, Claude Code keeps its own sign-in on each machine."); + const enough = hubMachine({ home }); + expect(await cliProxy("accounts", enough.machine)).toBe(0); + expect(enough.printed).toEqual(["CLI proxy: 2 Claude subscriptions of yours work, and hub.proxyAccounts asks for 2."]); + const one = hubMachine({ home, accounts: 1 }, { credentials: [{ provider: "claude" }] }); + expect(await cliProxy("accounts", one.machine)).toBe(0); + const codex = hubMachine({ home, plan: "codex" }, { credentials: [{ provider: "codex" }, { provider: "claude" }, { provider: "claude" }] }); + expect(await cliProxy("accounts", codex.machine)).toBe(waitsForPerson); + expect(codex.printed.join("\n")).toContain(`found 1. Run on the hub, once for each subscription: ${login(home, "codex")}`); + await expect(cliProxy("accounts", hubMachine({ home }, { down: true }).machine)).rejects.toThrow(); + await expect(cliProxy("accounts", client().machine)).rejects.toThrow("hub"); }); test("on the Codex plan the doctor counts Codex subscriptions and wants a Codex model, and names the Codex login", async () => { @@ -241,7 +253,11 @@ test("on the Codex plan the doctor counts Codex subscriptions and wants a Codex expect(await cliProxy("doctor", passing.machine)).toBe(0); expect(passing.printed.join("\n")).toContain("with 2 of your own Codex subscriptions"); const few = hubMachine({ home, plan: "codex" }, { credentials: [{ provider: "codex" }, { provider: "claude" }, { provider: "claude" }], models: ["gpt-6.1-sol"] }); - await expect(cliProxy("doctor", few.machine)).rejects.toThrow(`Add at least 2 of your own Codex subscriptions that work; found 1. Run: ${join(dataDir(home), "cli-proxy-api")} -config ${join(dataDir(home), "config.yaml")} -codex-login -no-browser`); + expect(await cliProxy("doctor", few.machine)).toBe(0); + expect(few.printed).toContain(`CLI proxy: To do: Add at least 2 of your own Codex subscriptions that work to the proxy; found 1. Run on the hub, once for each subscription: ${login(home, "codex")}`); + const none = hubMachine({ home, plan: "codex" }, { credentials: [], models: [] }); + expect(await cliProxy("doctor", none.machine)).toBe(0); + expect(none.printed.join("\n")).toContain("found 0. Until the proxy holds one, Codex keeps its own sign-in on each machine."); const noModel = hubMachine({ home, plan: "codex" }, { credentials: codexOnly.credentials }); await expect(cliProxy("doctor", noModel.machine)).rejects.toThrow("lists no Codex model"); }); @@ -407,7 +423,7 @@ test("the check reports an unreachable proxy and changes no file: apply sends Cl expect(existsSync(keyFile(home))).toBe(true); }); -test("the check fails without a saved key, or when the proxy serves no Claude model, and writes nothing", async () => { +test("the check fails without a saved key, waits for the person while the proxy serves no Claude model, and writes nothing", async () => { const missing = client(); connected(missing.home); writeFileSync(keyFile(missing.home), ""); @@ -417,7 +433,7 @@ test("the check fails without a saved key, or when the proxy serves no Claude mo const refused = client({}, { models: ["gpt-6.1-sol"] }); connected(refused.home); - expect(await cliProxy("check", refused.machine)).toBe(1); + expect(await cliProxy("check", refused.machine)).toBe(waitsForPerson); expect(refused.printed.join("\n")).toContain("lists no Claude model"); const fresh = client({}, { down: true }); @@ -595,7 +611,7 @@ test("the Codex check judges the proxy before Hedge routes Codex, so apply can w expect(existsSync(codexFile(served.home))).toBe(false); const unserved = client(); connected(unserved.home); - expect(await cliProxy("check", unserved.machine, "codex")).toBe(1); + expect(await cliProxy("check", unserved.machine, "codex")).toBe(waitsForPerson); expect(existsSync(codexFile(unserved.home))).toBe(false); }); @@ -609,12 +625,12 @@ test("the Codex check reports an unreachable proxy and changes no file", async ( expect(printed.join("\n")).toContain("hedge apply keeps Codex on its own sign-in until the proxy serves it."); }); -test("a proxy that lists no Codex model fails only the Codex check", async () => { +test("a proxy that lists no Codex model holds back only the Codex check, which waits for the person", async () => { const { machine, home, printed } = client(); connected(home); codexRouted(home); expect(await cliProxy("check", machine)).toBe(0); - expect(await cliProxy("check", machine, "codex")).toBe(1); + expect(await cliProxy("check", machine, "codex")).toBe(waitsForPerson); expect(printed.join("\n")).toContain(`The proxy at ${proxyUrl} lists no Codex model.`); }); @@ -657,7 +673,7 @@ test("a linked or unreadable Codex config is left alone and never breaks the Cla writeFileSync(real, stringify({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": hedgeProxy } })); symlinkSync(real, codexFile(linked.home)); expect(await cliProxy("check", linked.machine)).toBe(0); - expect(await cliProxy("check", linked.machine, "codex")).toBe(1); + expect(await cliProxy("check", linked.machine, "codex")).toBe(waitsForPerson); expect(await cliProxy("disconnect", linked.machine)).toBe(0); expect(parse(readFileSync(real, "utf8"))).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": hedgeProxy } }); @@ -666,7 +682,7 @@ test("a linked or unreadable Codex config is left alone and never breaks the Cla mkdirSync(join(broken.home, ".codex"), { recursive: true }); writeFileSync(codexFile(broken.home), "model = ["); expect(await cliProxy("check", broken.machine)).toBe(0); - expect(await cliProxy("check", broken.machine, "codex")).toBe(1); + expect(await cliProxy("check", broken.machine, "codex")).toBe(waitsForPerson); expect(readJson(settingsFile(broken.home)).env.ANTHROPIC_BASE_URL).toBe(proxyUrl); expect(readFileSync(codexFile(broken.home), "utf8")).toBe("model = ["); }); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index fb9544d8..c409b1b6 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -11,11 +11,12 @@ import { changedFiles, ownedFiles, readIfThere, removeUnits, requireInstalled, w import { refuseLinks, writePrivateFile, type Fetch } from "../../mcp-gateway/lib/client.ts"; import type { Run } from "../../mcp-gateway/lib/hub.ts"; import { isKeptForBoxes } from "../../../src/hub/names.ts"; +import { waitsForPerson } from "../../../src/pack/types.ts"; import { refreshCodexCatalog } from "./codex-catalog.ts"; import { must, proxyPort } from "./run.ts"; export { proxyPort, proxyUrl }; -export type ProxyMode = "plan" | "install" | "doctor" | "catalog" | "connect" | "disconnect" | "check" | "remove" | "left"; +export type ProxyMode = "plan" | "install" | "doctor" | "accounts" | "catalog" | "connect" | "disconnect" | "check" | "remove" | "left"; export type Release = { version: string; url: string; sha256: string }; export type Panel = { page: Release; plugin: Release; historyDays: number }; export type AiPlan = "claude" | "codex" | "both"; @@ -219,19 +220,14 @@ async function doctor(machine: ProxyMachine, paths: Layout): Promise { await panelDoctor(machine, paths, managementKey); const strategy = ((await management(machine.fetch, managementKey, "routing/strategy")) as { strategy?: string }).strategy; if (strategy !== "fill-first") throw new Error(`The proxy routes with ${strategy ?? "no strategy"}, not fill-first. Run hedge cli-proxy-balance to restore it.`); - const { files } = (await management(machine.fetch, managementKey, "auth-files")) as { files: AuthFile[] }; - const expired = files.filter((file) => (file.provider === "claude" || file.provider === "codex") && !file.disabled && loginExpired(file)); - const enabled = (agent: Agent) => files.filter((file) => file.provider === agent && !file.disabled && !expired.includes(file)).length; - const agent = pooled(machine.plan); - const count = enabled(agent); - const floor = machine.accounts ?? 2; - const lapsed = expired.filter((file) => file.provider === agent).length; - if (count < floor) throw new Error(`Add at least ${floor} of your own ${subscription[agent]} subscriptions that work; found ${count}${lapsed ? `, and ${lapsed} whose login expired, which you can sign in again` : ""}. Run: ${loginCommand(paths, agent)}`); - const reason = await probe(machine.fetch, key, agent); + const { agent, count, short, expired, enabled } = await pool(machine, paths, managementKey); + const models = await listModels(machine.fetch, key); + const reason = typeof models === "string" ? models : short ? undefined : unserved(models, agent); if (reason) throw new Error(reason); const beside = machine.plan === "both" ? ` and ${subscriptions(enabled("codex"), "codex")}` : ""; const balancer = agent === "claude" ? "puts the one whose weekly quota resets first on top" : "frees each one whose quota cooldown its live usage disproves"; machine.print(`CLI proxy: CLIProxyAPI ${machine.release.version} runs at ${proxyUrl}, loopback only, with ${count} of your own ${subscription[agent]} subscriptions${beside}. Every 5 minutes the balancer ${balancer}.`); + if (short) machine.print(`CLI proxy: To do: ${short}`); for (const file of expired) { const provider = file.provider as Agent; machine.print(`CLI proxy: Warning: the ${subscription[provider]} subscription ${shortId(file)} cannot refresh its login, so the proxy cannot use it and the balancer cannot read its usage. Sign it in again with: ${loginCommand(paths, provider)}. Or disable it in the management panel.`); @@ -242,6 +238,26 @@ async function doctor(machine: ProxyMachine, paths: Layout): Promise { for (const devbox of machine.devboxes.filter((name) => !isKeptForBoxes(name))) machine.print(`Connect ${devbox}: ssh ${devbox} ${quote('sh "$HOME/.local/share/hedge/engine/hedge" cli-proxy connect')} < ~/.config/hedge/cli-proxy.key`); } +async function pool(machine: ProxyMachine, paths: Layout, managementKey: string) { + const { files } = (await management(machine.fetch, managementKey, "auth-files")) as { files: AuthFile[] }; + const expired = files.filter((file) => (file.provider === "claude" || file.provider === "codex") && !file.disabled && loginExpired(file)); + const enabled = (agent: Agent) => files.filter((file) => file.provider === agent && !file.disabled && !expired.includes(file)).length; + const agent = pooled(machine.plan); + const count = enabled(agent); + const floor = machine.accounts ?? 2; + const lapsed = expired.filter((file) => file.provider === agent).length; + const meanwhile = count ? "" : ` Until the proxy holds one, ${agent === "claude" ? "Claude Code" : "Codex"} keeps its own sign-in on each machine.`; + const short = count < floor ? `Add at least ${floor} of your own ${subscription[agent]} subscriptions that work to the proxy; found ${count}${lapsed ? `, and ${lapsed} whose login expired, which you can sign in again` : ""}.${meanwhile} Run on the hub, once for each subscription: ${loginCommand(paths, agent)}` : undefined; + return { agent, count, floor, short, expired, enabled }; +} + +async function accounts(machine: ProxyMachine, paths: Layout): Promise { + await refuseLinkedLayout(machine.home, paths); + const { agent, count, floor, short } = await pool(machine, paths, await readManagementKey(machine.home)); + machine.print(short ? `CLI proxy: To do: ${short}` : `CLI proxy: ${subscriptions(count, agent)} of yours work, and hub.proxyAccounts asks for ${floor}.`); + return short ? waitsForPerson : 0; +} + async function panelDoctor(machine: ProxyMachine, paths: Layout, managementKey: string): Promise { const page = await machine.fetch(panelUrl, { signal: AbortSignal.timeout(5000) }).catch(() => undefined); await page?.body?.cancel(); @@ -381,13 +397,14 @@ async function sendCodexHome(home: string): Promise { export type Agent = "claude" | "codex"; -async function check(machine: ProxyMachine, agent: Agent): Promise { +async function check(machine: ProxyMachine, agent: Agent): Promise { const key = await savedKey(machine.home); const models = key ? await listModels(machine.fetch, key) : `This machine has no proxy key at ${keyPath(machine.home)}. On the hub, hedge cli-proxy doctor prints the connect command.`; const reason = agent === "claude" ? unserved(models, "claude") : await codexUnserved(machine.home, models); const name = agent === "claude" ? "Claude Code" : "Codex"; machine.print(reason ? `${reason} hedge apply keeps ${name} on its own sign-in until the proxy serves it.` : `${name} reaches your own-account proxy at ${proxyUrl}.`); - return !reason; + if (!reason) return 0; + return reason === missingModel[agent] ? waitsForPerson : 1; } async function codexUnserved(home: string, models: string[] | string): Promise { @@ -481,14 +498,15 @@ async function plan(machine: ProxyMachine, paths: Layout): Promise { export async function cliProxy(mode: ProxyMode, machine: ProxyMachine, agent: Agent = "claude"): Promise { if (machine.uid === 0) throw new Error("Run hedge cli-proxy as the person who runs Claude Code, not root."); const paths = layout(machine.home); - if (mode === "install" || mode === "doctor" || mode === "catalog") requireHubRole(machine.home, `cli-proxy ${mode}`); + if (mode === "install" || mode === "doctor" || mode === "accounts" || mode === "catalog") requireHubRole(machine.home, `cli-proxy ${mode}`); if (mode === "catalog") return catalog(machine, paths); + if (mode === "accounts") return accounts(machine, paths); if (mode === "plan") await plan(machine, paths); if (mode === "install") await install(machine, paths); if (mode === "doctor") await doctor(machine, paths); if (mode === "connect") await connect(machine); if (mode === "disconnect") await disconnect(machine); - if (mode === "check") return (await check(machine, agent)) ? 0 : 1; + if (mode === "check") return check(machine, agent); if (mode === "left") return (await ownedFiles(machine.home, [paths.unit], marker, what)).length ? 0 : 1; if (mode === "remove") await removeUnits(machine.home, [paths.unit], marker, what, machine.run); return 0; diff --git a/packs/hub/pack.json b/packs/hub/pack.json index 402e2743..c2be651b 100644 --- a/packs/hub/pack.json +++ b/packs/hub/pack.json @@ -111,7 +111,7 @@ }, { "key": "hub.proxyAccounts", - "question": "How many of your own subscriptions must the hub's proxy hold before hedge cli-proxy doctor passes? They are Claude subscriptions, or Codex ones on the Codex plan.", + "question": "How many of your own subscriptions should the hub's proxy hold? Below that, hedge doctor shows a To do line, and nothing fails. They are Claude subscriptions, or Codex ones on the Codex plan.", "type": "number", "default": 2, "ask": "never" @@ -662,6 +662,41 @@ ], "workflow": "devbox-tokens" }, + { + "id": "cli-proxy-accounts", + "run": [ + "hedge", + "cli-proxy", + "accounts" + ], + "say": "Hedge cannot count the subscriptions in your own-account proxy. Run hedge cli-proxy doctor on the hub to see why.", + "todo": "Add your own Claude subscriptions to the own-account proxy, until it holds as many that work as hub.proxyAccounts asks for (2 by default). Run on the hub, once for each subscription: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser. hedge cli-proxy accounts says how many work now.", + "roles": [ + "hub" + ], + "plans": [ + "claude", + "both" + ], + "workflow": "own-account-balancing" + }, + { + "id": "cli-proxy-codex-accounts", + "run": [ + "hedge", + "cli-proxy", + "accounts" + ], + "say": "Hedge cannot count the subscriptions in your own-account proxy. Run hedge cli-proxy doctor on the hub to see why.", + "todo": "Add your own Codex subscriptions to the own-account proxy, until it holds as many that work as hub.proxyAccounts asks for (2 by default). Run on the hub, once for each subscription: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser. hedge cli-proxy accounts says how many work now.", + "roles": [ + "hub" + ], + "plans": [ + "codex" + ], + "workflow": "own-account-balancing" + }, { "id": "cli-proxy-client", "run": [ @@ -670,6 +705,7 @@ "check" ], "say": "Claude Code cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check here to see why.", + "todo": "Claude Code keeps its own sign-in, because the own-account proxy on the hub serves no Claude model yet: it holds none of your Claude subscriptions that work. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser. The first hedge apply after that sends Claude Code to the proxy.", "roles": [ "hub", "devbox" @@ -689,6 +725,7 @@ "codex" ], "say": "Codex cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check codex here to see why.", + "todo": "Codex keeps its own sign-in, because the own-account proxy on the hub serves no Codex model yet: it holds none of your Codex subscriptions that work. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser. The first hedge apply after that sends Codex to the proxy.", "roles": [ "hub", "devbox" @@ -725,6 +762,7 @@ "plan", "install", "doctor", + "accounts", "catalog", "connect", "disconnect", diff --git a/packs/hub/skills/hedge-hub/SKILL.md b/packs/hub/skills/hedge-hub/SKILL.md index f361ba76..99a839f2 100644 --- a/packs/hub/skills/hedge-hub/SKILL.md +++ b/packs/hub/skills/hedge-hub/SKILL.md @@ -114,11 +114,14 @@ the person's own subscriptions, never for someone else's. `codex-pro`, because a Business seat refuses them. Upstream's own entry replaces the copy once it appears. - **Subscriptions.** The person adds each of their own Claude accounts on the - hub, or their Codex accounts on the `codex` plan. `hedge cli-proxy doctor` - prints the login command until the proxy holds `hub.proxyAccounts` of them - (2 by default). On the `both` plan it counts Claude subscriptions and names - the Codex ones beside them. Keep the login URL and callback values out of - logs and Git. + hub, or their Codex accounts on the `codex` plan. Setup never waits for + them: the proxy installs and runs with none. Until the proxy holds + `hub.proxyAccounts` of them that work (2 by default), `hedge apply` and + `hedge doctor` show a `To do:` line with the login command, and nothing + fails. `hedge cli-proxy accounts` counts them; it exits 4 while too few + work. On the `both` plan it counts Claude subscriptions, and + `hedge cli-proxy doctor` names the Codex ones beside them. Keep the login + URL and callback values out of logs and Git. - **The balancer.** Every 5 minutes, `hedge cli-proxy-balance` reads the weekly usage of each subscription and gives the highest priority to the one whose weekly quota resets first. A full or unreadable subscription gets priority @@ -138,8 +141,7 @@ the person's own subscriptions, never for someone else's. on the hub finds it. When the proxy says a subscription's login expired (`invalid_grant`) while that subscription fails, the balancer's log line says so, and `hedge cli-proxy doctor` warns with the same short id and its sign-in - command. The doctor counts only working - subscriptions toward the minimum and fails when too few work. Usage that is + command. Only working subscriptions count toward `hub.proxyAccounts`. Usage that is unreadable for another reason has no such warning: it clears on a later run. - **Claude Code.** On the `claude` and `both` plans, on the hub and on each devbox, Hedge sets `env.ANTHROPIC_BASE_URL` to the proxy and `apiKeyHelper` @@ -157,7 +159,11 @@ the person's own subscriptions, never for someone else's. not serve an agent with this machine's key, `hedge apply` leaves that agent's settings out and removes Hedge's own values, so the agent uses its own sign-in, and the next apply changes nothing. `hedge apply` and - `hedge doctor` name the failed check. The first apply after the proxy + `hedge doctor` name the failed check. When the proxy answers the key but + serves no model for the agent, because it holds none of that agent's + subscriptions that work, the check exits 4: the agent stays on its own + sign-in, and the line is a `To do:` with the login command, not a + failure. The first apply after the proxy answers again sends the agent back to the proxy. - **The panel.** The Proxy panel workflow is off by default and needs own-account balancing. With diff --git a/packs/recovery/SETUP.md b/packs/recovery/SETUP.md index b3ce1b79..775f619b 100644 --- a/packs/recovery/SETUP.md +++ b/packs/recovery/SETUP.md @@ -9,8 +9,11 @@ Recovery never knows that a provider has capacity, and continues nothing. 1. Add `--add recovery` to the command in item 2 of step 9, and add `provider-recovery=on,own-account-balancing=on` to `--workflows`. -2. Once item 2 of step 9 has turned it on, the proxy needs at least two of their - own subscriptions. This prints the sign-in command that adds one: +2. Once item 2 of step 9 has turned it on, Recovery needs at least one of + their own subscriptions in the proxy, and the proxy should hold two. Setup + does not wait for them: until the proxy holds them, `hedge doctor` shows a + `To do:` line with the sign-in command, and nothing fails. This prints + the sign-in command that adds one: ```sh ssh coder. 'export PATH="$HOME/.local/bin:$HOME/.bun/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"; hedge cli-proxy plan' diff --git a/src/cli.test.ts b/src/cli.test.ts index d77a8cf4..e1873f93 100644 --- a/src/cli.test.ts +++ b/src/cli.test.ts @@ -1,8 +1,9 @@ import { afterEach, describe, expect, test } from "bun:test"; import { createHash } from "node:crypto"; -import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, symlinkSync, writeFileSync } from "node:fs"; +import { cpSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, symlinkSync, writeFileSync } from "node:fs"; import { hostname } from "node:os"; import { join, relative } from "node:path"; +import { main } from "./cli.ts"; import { cliFixtures, repoRoot } from "./testing/cli.ts"; import { spawnGit, tempDirs } from "./testing/git.ts"; @@ -28,6 +29,28 @@ function snapshot(root: string): Record { } describe("hedge apply", () => { + test("a check that waits for the person prints a To do, and neither apply nor doctor fails", async () => { + const engine = temp.make("engine-"); + cpSync(join(repoRoot, "packs/base"), join(engine, "packs/base"), { recursive: true }); + mkdirSync(join(engine, "packs/pool"), { recursive: true }); + writeFileSync(join(engine, "packs/pool/pack.json"), JSON.stringify({ schema: 1, name: "pool", summary: "A pool of accounts.", checks: [{ id: "pool", run: ["pool-check"], say: "Hedge cannot read the pool.", todo: "Add an account to the pool." }] })); + const home = temp.make("home-"); + const profile = profileDir(["pool"]); + const run = async (command: string[]) => (command.at(-1) === "pool-check" ? 4 : 0); + const typed = async (args: string[]) => { + const lines: string[] = []; + const code = await main(args, { home, engineRoot: engine, platform: "linux", env: {}, print: (line) => lines.push(line), run }); + return { code, lines }; + }; + const applied = await typed(["apply", "--profile", profile]); + expect(applied.code).toBe(0); + expect(applied.lines).toContain("To do: Add an account to the pool."); + expect(applied.lines.at(-1)).toStartWith("Applied "); + const doctor = await typed(["doctor", "--profile", profile]); + expect(doctor.code).toBe(0); + expect(doctor.lines.slice(-2)).toEqual(["To do: Add an account to the pool.", "Everything Hedge sets matches your profile. The To do lines above wait for you."]); + }); + test("puts the base pack's rules on a fresh machine and remembers the profile", async () => { const home = temp.make("home-"); const profile = profileDir(); diff --git a/src/cli.ts b/src/cli.ts index d82dd605..1a02e2b5 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -543,8 +543,9 @@ function parseFlags(args: string[]) { function printReport(report: Report, command: string, print: (line: string) => void, dryRun = false) { const changes = report.actions.filter((action) => action.operation !== "keep" && action.operation !== "conflict"); if (command === "apply") for (const action of changes) print(`${action.operation} ${action.target}`); - for (const finding of report.findings) print(`${finding.repair === "human" ? "To do" : finding.repair ? "Fix" : "Note"}: ${finding.say}`); - if (command === "doctor") print(report.ok ? "Everything matches your profile." : "Run hedge apply to fix what Hedge can fix."); + for (const finding of report.findings) print(`${finding.repair === "human" || finding.todo ? "To do" : finding.repair ? "Fix" : "Note"}: ${finding.say}`); + const waits = report.findings.some((finding) => finding.todo); + if (command === "doctor") print(!report.ok ? "Run hedge apply to fix what Hedge can fix." : waits ? "Everything Hedge sets matches your profile. The To do lines above wait for you." : "Everything matches your profile."); else if (dryRun) print(report.ok ? `Preview: ${changes.length} change${changes.length === 1 ? "" : "s"} to apply. Nothing was written.` : "Preview: Hedge could not apply your profile. Nothing was written."); else print(report.ok ? `Applied ${changes.length} change${changes.length === 1 ? "" : "s"}.` : "Hedge could not apply your profile."); } diff --git a/src/pack/manifest.test.ts b/src/pack/manifest.test.ts index 37508a69..e7688ffc 100644 --- a/src/pack/manifest.test.ts +++ b/src/pack/manifest.test.ts @@ -198,6 +198,12 @@ describe("loadPacks", () => { await expect(loadPacks(sourceWith({ base: { ...base, checks, steps: [{ id: "start", run: ["start"], after: "proxy-up", roles: ["devbox"] }] } }))).rejects.toThrow("steps start: after must name a human step of this pack"); }); + test("takes a check's todo only as a sentence that says what the person does", async () => { + const check = { id: "pool", run: ["pool-check"], say: "Hedge cannot read the pool." }; + await expect(loadPacks(sourceWith({ base: { ...base, checks: [{ ...check, todo: "Add an account to the pool." }] } }))).resolves.toHaveLength(1); + for (const todo of ["", " ", 4]) await expect(loadPacks(sourceWith({ base: { ...base, checks: [{ ...check, todo }] } }))).rejects.toThrow("checks pool: todo must say what the person does"); + }); + test("rejects malformed nested items with a clear error", async () => { const setting = base.settings[0]; const row = { role: "review", work: "Review", provider: "codex", model: "m", effort: "high" }; diff --git a/src/pack/manifest.ts b/src/pack/manifest.ts index 05a759fc..c0604853 100644 --- a/src/pack/manifest.ts +++ b/src/pack/manifest.ts @@ -234,6 +234,7 @@ function checkShapes(raw: Record, fail: (message: string) => ne }); each("checks", (item, label) => { need(typeof item.id === "string" && isStrings(item.run) && typeof item.say === "string", `${label}: needs an id, a run list, and a say`); + need(item.todo === undefined || filled(item.todo), `${label}: todo must say what the person does`); filters(item, label); }); } diff --git a/src/pack/types.ts b/src/pack/types.ts index 10ecf18c..34b1a4dc 100644 --- a/src/pack/types.ts +++ b/src/pack/types.ts @@ -163,7 +163,11 @@ export type PackManifest = { subcommands?: Record; }; -export type PackCheck = Scope & { id: string; run: string[]; say: string }; +/** The exit code of a pack check that waits for the person, such as a pool that holds none of their accounts yet. The check does not pass, so config that waits for it stays out, but it fails nothing: Hedge says the check's todo as a To do line. */ +export const waitsForPerson = 4; + +/** say is the Fix line while run fails. todo is the To do line while run exits waitsForPerson. */ +export type PackCheck = Scope & { id: string; run: string[]; say: string; todo?: string }; export const handlerSeams = ["review-bot", "heavy-route", "pr-command", "slack-capture", "secret-rows", "secret-providers", "proof-receipt"] as const; export type HandlerSeam = (typeof handlerSeams)[number]; @@ -255,6 +259,7 @@ export type Plan = { dropped: Array<{ pack: string; item: string; reason: string; check?: string; value?: unknown }>; }; -export type Finding = { id: string; pack: string; say: string; repair?: "apply" | "human" }; +/** repair: who fixes it; a finding with a repair fails apply or doctor. todo: a To do line that fails nothing. */ +export type Finding = { id: string; pack: string; say: string; repair?: "apply" | "human"; todo?: true }; export type Report = { ok: boolean; actions: Action[]; findings: Finding[]; humanLeft: Array> }; diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index 32e8290c..b4cf129a 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -270,6 +270,48 @@ describe("a config entry that waits for a pack check", () => { }); }); +describe("a pack check that waits for the person", () => { + const checks = [{ id: "pool", run: ["pool-check"], say: "Hedge cannot read the pool.", todo: "Add an account to the pool.", pack: "base" }]; + const config = [{ provider: "claude" as const, key: "env.ANTHROPIC_BASE_URL", value: "http://127.0.0.1:8317", after: "pool", pack: "base" }]; + const waiting = { id: "check:pool", pack: "base", say: "Add an account to the pool.", todo: true as const }; + const settings = (home: string) => (existsSync(join(home, ".claude/settings.json")) ? JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")) : undefined); + + test("keeps the config that waits for it out, and apply and doctor say it as a To do that fails nothing", async () => { + const home = temp.make("home-"); + const options = { home, adapters: [providerConfigAdapter()], run: exitCodes({ "pool-check": 4 }) }; + const applied = await apply(plan({ files: [], checks, config }), options); + expect(applied.ok).toBe(true); + expect(applied.findings).toEqual([waiting]); + expect(settings(home)).toBeUndefined(); + const doctor = await check(plan({ files: [], checks, config }), options); + expect(doctor.ok).toBe(true); + expect(doctor.findings).toEqual([waiting]); + }); + + test("that nothing waits for runs after the steps, so it reads what a step installed, and apply names only its To do", async () => { + const order: string[] = []; + let code = 4; + const run = async (command: string[]) => (order.push(command.join(" ")), command.join(" ") === "pool-check" ? code : 0); + const steps = [{ id: "install-pool", run: ["install-pool"], check: ["pool-installed"], pack: "base" }]; + const home = temp.make("home-"); + const applied = await apply(plan({ files: [], checks, steps }), { home, run }); + expect(applied).toMatchObject({ ok: true, findings: [waiting] }); + expect(order.indexOf("pool-check")).toBeGreaterThan(order.indexOf("pool-installed")); + code = 1; + expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [] }); + expect(await check(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: false, findings: [{ id: "check:pool", pack: "base", say: "Hedge cannot read the pool.", repair: "apply" }] }); + code = 0; + expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [] }); + }); + + test("a check without a todo that exits 4 is a Fix, and a check with one stays a Fix for any other failing code", async () => { + const home = temp.make("home-"); + const plain = [{ ...checks[0]!, todo: undefined }]; + expect((await check(plan({ files: [], checks: plain }), { home, run: exitCodes({ "pool-check": 4 }) })).findings).toEqual([{ id: "check:pool", pack: "base", say: "Hedge cannot read the pool.", repair: "apply" }]); + expect((await check(plan({ files: [], checks, config }), { home, adapters: [providerConfigAdapter()], run: exitCodes({ "pool-check": 1 }) })).ok).toBe(false); + }); +}); + describe("check", () => { test("runs pack checks and human probes, and lists the human steps left", async () => { const human = [{ id: "github", say: "Sign in to GitHub.", command: "gh auth login --web", probe: ["gh", "auth", "status"], pack: "base" }]; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 63bad831..13f8f2fe 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -2,7 +2,7 @@ import { existsSync, readdirSync } from "node:fs"; import { join } from "node:path"; import { install, plan as planFiles } from "../files/install.ts"; import { packName } from "../pack/manifest.ts"; -import { cannotTell } from "../pack/types.ts"; +import { cannotTell, waitsForPerson } from "../pack/types.ts"; import type { Action, CatalogEntry, Finding, HumanStep, Owned, PackCheck, Plan, Report } from "../pack/types.ts"; import { engineCommand } from "./engine-command.ts"; import { changeLedger, ledgerKey, readLedger, undoVariable, type Ledger, type LedgerStep } from "./ledger.ts"; @@ -104,7 +104,7 @@ export async function apply(plan: Plan, options: { home: string; dryRun?: boolea const node = plan.onHub || plan.pullsFromHub ? plan.machine.name : undefined; const issue = !context.dryRun && node && options.issue ? (ready: CatalogEntry[]) => options.issue!(ready, node) : undefined; const gated = await gate(plan, run, !context.dryRun, issue); - report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun)), ...gated.failedChecks.map(checkFinding)); + report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun)), ...gated.checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); if (!context.dryRun && gated.failed.some(repairedByHedge)) report.ok = false; for (const adapter of adapters) { const result = await adapter.apply(gated.plan, context); @@ -178,15 +178,27 @@ export async function apply(plan: Plan, options: { home: string; dryRun?: boolea if (!heldCodes.includes(await run(step.probe, { [undoVariable]: ledgerKey(step) }))) await changeLedger(options.home, "human", ledgerKey(step)); } await undo(items.filter((item) => item.cause !== "pack")); + if (!context.dryRun) report.findings.push(...(await waitingChecks(plan, run, gated.checks))); return report; } +/** The To do lines of the checks that wait for the person and that the gate did not run, tested once the steps ran, such as a check that reads what a step installs. */ +async function waitingChecks(plan: Plan, run: Runner, gated: Array<[Owned, number]>): Promise { + const findings: Finding[] = []; + for (const item of plan.checks.filter((check) => check.todo && !gated.some(([ran]) => ran === check))) { + const code = await run(item.run); + if (code === waitsForPerson) findings.push(checkFinding(item, code)); + } + return findings; +} + export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { const run = viaEngine(plan, options.run ?? spawnRunner(options.home, plan.machine.profileDir)); const report = await apply(plan, { ...options, dryRun: true }); for (const item of plan.checks) { if (report.findings.some((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack)) continue; - if ((await run(item.run)) !== 0) report.findings.push(checkFinding(item)); + const code = await run(item.run); + if (code !== 0) report.findings.push(checkFinding(item, code)); } const waits = waiter(plan, run); for (const step of plan.steps) { @@ -208,7 +220,10 @@ export async function check(plan: Plan, options: { home: string; adapters?: Adap return report; } -const checkFinding = (item: Owned): Finding => ({ id: `check:${item.id}`, pack: item.pack, say: item.say, repair: "apply" }); +function checkFinding(item: Owned, code: number): Finding { + const id = `check:${item.id}`; + return code === waitsForPerson && item.todo ? { id, pack: item.pack, say: item.todo, todo: true } : { id, pack: item.pack, say: item.say, repair: "apply" }; +} const repairedByHedge = (step: HumanStep) => step.repair !== undefined && !step.command; @@ -217,7 +232,7 @@ function humanFinding(step: Owned, repairStillToRun: boolean): Findin } /** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ -async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; failedChecks: Array>; issued: Finding[] }> { +async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Array<[Owned, number]>; issued: Finding[] }> { const tested = new Map, boolean>(); const probe = async (step: Owned) => { if ((await run(step.probe)) === 0) return true; @@ -239,10 +254,10 @@ async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: Ca if (!job.after || (await passes(plan.human.find((step) => step.pack === job.pack && step.id === job.after)))) jobs.push(job); else dropped.push({ pack: job.pack, item: `job ${job.id}`, reason: `check ${job.after} does not pass`, check: job.after }); } - const checked = new Map, boolean>(); + const checked = new Map, number>(); const checkPasses = async (item: Owned) => { - if (!checked.has(item)) checked.set(item, (await run(item.run)) === 0); - return checked.get(item)!; + if (!checked.has(item)) checked.set(item, await run(item.run)); + return checked.get(item) === 0; }; const waitsFor = (pack: string, id: string) => { const human = checkOf(pack, id); @@ -256,8 +271,7 @@ async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: Ca } const failed = [...tested].filter(([, passed]) => !passed).map(([step]) => step); const passed = [...tested].filter(([, passed]) => passed).map(([step]) => step); - const failedChecks = [...checked].filter(([, passed]) => !passed).map(([item]) => item); - return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped] }, failed, passed, failedChecks, issued }; + return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped] }, failed, passed, checks: [...checked], issued }; } type Cause = { workflow: string } | "pack" | "plan"; From 08590ec336ac64e75f58ca2dec3afa7783c7d7f9 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:15:56 +0000 Subject: [PATCH 02/32] Fix the review findings on the empty proxy pool - connect saves a key the proxy accepts, and routes each agent only once the proxy serves its model, so a node can connect to an empty pool. - With hub.proxyAccounts at 0, an empty pool passes the doctor: the account count, not the floor, decides whether a missing model is wrong. - A model list the proxy cannot give fails the check and the doctor instead of reading as an empty pool. - Apply checks a waiting check again after the steps, so a fresh hub shows its To do instead of a stale Fix from before the proxy ran. - Each To do ends with the login command, so it pastes as is. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 ++++-- packs/hub/hub.test.ts | 8 ++++-- packs/hub/lib/cli-proxy-testing.ts | 3 +- packs/hub/lib/cli-proxy.test.ts | 44 +++++++++++++++++++++++++++-- packs/hub/lib/cli-proxy.ts | 28 +++++++++--------- packs/hub/pack.json | 8 +++--- packs/hub/skills/hedge-hub/SKILL.md | 5 +++- src/reconciler/apply.test.ts | 34 ++++++++++++++++------ src/reconciler/apply.ts | 16 +++++------ 9 files changed, 110 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b32c7d9..c99c5a57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,11 @@ `hedge cli-proxy doctor` still warns about it. - Each machine keeps Claude Code and Codex on their own sign-ins until the proxy serves them. While the proxy answers but holds none of an agent's - subscriptions, its route check is a `To do:` line, not a failure. A proxy - that is down, refuses the key, or runs a plugin Hedge did not install - still fails `hedge doctor`. + subscriptions, its route check is a `To do:` line, not a failure. + `hedge cli-proxy connect` saves the key on a node in that case, so the + first `hedge apply` after the proxy serves an agent sends it there. A + proxy that is down, refuses the key, answers without a model list, or + runs a plugin Hedge did not install still fails `hedge doctor`. - A pack check can name a `todo`: when its command exits 4, Hedge prints it as a `To do:` line, keeps the config that waits for the check out, and fails neither apply nor doctor. Apply runs such a check after the steps. diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 6eeeadb3..23466801 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -432,8 +432,8 @@ describe("the Own-account balancing workflow", () => { const { home, result, options } = emptyHub("claude"); const fresh = await apply(result, options); expect(fresh.ok).toBe(true); - expect(fresh.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-client", "apply"], ["check:cli-proxy-accounts", "todo"]]); - expect(fresh.findings[1]!.say).toContain(`Run on the hub, once for each subscription: ${login("claude")}.`); + expect(fresh.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-client", "todo"], ["check:cli-proxy-accounts", "todo"]]); + expect(fresh.findings.map((finding) => finding.say.endsWith(`: ${login("claude")}`))).toEqual([true, true]); expect(existsSync(join(home, ".local/share/hedge-cli-proxy/cli-proxy-api"))).toBe(true); const applied = await apply(result, options); expect(applied.ok).toBe(true); @@ -446,7 +446,9 @@ describe("the Own-account balancing workflow", () => { test("on the Codex plan, lists the Codex login as a To do and keeps Codex on its own sign-in", async () => { const { home, result, options } = emptyHub("codex"); - expect((await apply(result, options)).ok).toBe(true); + const fresh = await apply(result, options); + expect(fresh.ok).toBe(true); + expect(fresh.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-codex", "todo"], ["check:cli-proxy-codex-accounts", "todo"]]); const applied = await apply(result, options); expect(applied.ok).toBe(true); expect(applied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-codex", true], ["check:cli-proxy-codex-accounts", true]]); diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts index e3ebd5f9..f6552a12 100644 --- a/packs/hub/lib/cli-proxy-testing.ts +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -9,7 +9,7 @@ export const archive = "fake release archive"; export const release = { version: "9.9.9", url: "https://example.com/proxy.tar.gz", sha256: new Bun.CryptoHasher("sha256").update(archive).digest("hex") }; export const dataDir = (home: string) => join(home, ".local/share/hedge-cli-proxy"); -export type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean; refuses?: boolean }; +export type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean; refuses?: boolean; modelList?: string }; export function fakeProxy(proxy: Proxy = {}) { const requests: Array<{ path: string; method: string; auth: string }> = []; @@ -20,6 +20,7 @@ export function fakeProxy(proxy: Proxy = {}) { if (proxy.catalog !== undefined && url.pathname.endsWith("/codex_client_models.json")) return Response.json(proxy.catalog); if (proxy.registry !== undefined && url.pathname.endsWith("/models.json")) return Response.json(proxy.registry); if (proxy.down || url.origin !== proxyUrl) throw new TypeError("connection refused"); + if (url.pathname === "/v1/models" && proxy.modelList !== undefined) return new Response(proxy.modelList, { status: 200 }); if (url.pathname === "/v1/models") return auth === `Bearer ${clientKey}` && !proxy.refuses ? Response.json({ data: (proxy.models ?? ["claude-sonnet-5-5"]).map((id) => ({ id })) }) : new Response("no", { status: 401 }); if (url.pathname === "/v0/management/routing/strategy") return Response.json({ strategy: proxy.strategy ?? "fill-first" }); if (url.pathname === "/v0/management/auth-files") return Response.json({ files: proxy.credentials ?? [{ provider: "claude" }, { provider: "claude" }] }); diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index f4451145..e7cb3c15 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -284,9 +284,11 @@ test("connect on a Codex plan checks the proxy for a Codex model, sends Codex to expect(await cliProxy("check", machine, "codex")).toBe(0); const noCodex = client({ plan: "codex" }); - await expect(cliProxy("connect", noCodex.machine)).rejects.toThrow("lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Nothing changed."); - expect(existsSync(keyFile(noCodex.home))).toBe(false); + expect(await cliProxy("connect", noCodex.machine)).toBe(0); + expect(noCodex.printed).toEqual([`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Codex keeps its own sign-in.`]); + expect(readFileSync(keyFile(noCodex.home), "utf8")).toBe(`${clientKey}\n`); expect(existsSync(codexFile(noCodex.home))).toBe(false); + expect(await cliProxy("check", noCodex.machine, "codex")).toBe(waitsForPerson); const theirs = client({ plan: "codex" }, { models: ["gpt-6.1-sol"] }); codexRouted(theirs.home, { ...mine, model_provider: "work" }); @@ -390,6 +392,44 @@ test("connect on a devbox checks the key against the proxy first, then saves it expect(printed.join("\n")).toContain("Restart"); }); +test("connect to a proxy that holds none of your subscriptions saves the key, keeps Claude Code on its own sign-in, and leaves the move to a later apply", async () => { + const { machine, home, printed } = client({}, { models: [] }); + mkdirSync(join(home, ".claude"), { recursive: true }); + writeFileSync(settingsFile(home), JSON.stringify({ model: "opus" })); + expect(await cliProxy("connect", machine)).toBe(0); + expect(readFileSync(keyFile(home), "utf8")).toBe(`${clientKey}\n`); + expect(readJson(settingsFile(home))).toEqual({ model: "opus" }); + expect(printed).toEqual([`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Claude Code keeps its own sign-in. The first hedge apply after the proxy serves it sends Claude Code there.`]); + expect(await cliProxy("check", machine)).toBe(waitsForPerson); + const refused = client({}, { models: [], refuses: true }); + await expect(cliProxy("connect", refused.machine)).rejects.toThrow("refuses this machine's key (HTTP 401). Nothing changed."); + expect(existsSync(keyFile(refused.home))).toBe(false); +}); + +test("a model list the proxy cannot give fails the check and the doctor, and never reads as an empty pool", async () => { + for (const modelList of ["not json", "{}", '{"data":null}']) { + const routed = client({}, { modelList }); + connected(routed.home); + expect(await cliProxy("check", routed.machine)).toBe(1); + expect(routed.printed.join("\n")).toContain(`The proxy at ${proxyUrl} answers without a model list.`); + } + const { machine, home } = hubMachine(); + await cliProxy("install", machine); + await expect(cliProxy("doctor", hubMachine({ home }, { credentials: [], modelList: "not json" }).machine)).rejects.toThrow("answers without a model list"); +}); + +test("with hub.proxyAccounts at 0, the doctor and accounts pass with an empty pool, and the doctor still wants a model once the pool holds one", async () => { + const { machine, home } = hubMachine(); + await cliProxy("install", machine); + for (const plan of ["claude", "codex"] as const) { + const empty = hubMachine({ home, plan, accounts: 0 }, { credentials: [], models: [] }); + expect(await cliProxy("doctor", empty.machine)).toBe(0); + expect(empty.printed.join("\n")).not.toContain("To do"); + expect(await cliProxy("accounts", hubMachine({ home, plan, accounts: 0 }, { credentials: [] }).machine)).toBe(0); + } + await expect(cliProxy("doctor", hubMachine({ home, accounts: 0 }, { credentials: [{ provider: "claude" }], models: [] }).machine)).rejects.toThrow("lists no Claude model"); +}); + test("connect changes nothing for a key that is not hexadecimal, a proxy that does not answer, or Claude settings that point elsewhere", async () => { const bad = client({ readStdin: async () => "sk-not-a-key\n" }); await expect(cliProxy("connect", bad.machine)).rejects.toThrow("hexadecimal"); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index c409b1b6..3698fb42 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -222,7 +222,7 @@ async function doctor(machine: ProxyMachine, paths: Layout): Promise { if (strategy !== "fill-first") throw new Error(`The proxy routes with ${strategy ?? "no strategy"}, not fill-first. Run hedge cli-proxy-balance to restore it.`); const { agent, count, short, expired, enabled } = await pool(machine, paths, managementKey); const models = await listModels(machine.fetch, key); - const reason = typeof models === "string" ? models : short ? undefined : unserved(models, agent); + const reason = typeof models === "string" ? models : count ? unserved(models, agent) : undefined; if (reason) throw new Error(reason); const beside = machine.plan === "both" ? ` and ${subscriptions(enabled("codex"), "codex")}` : ""; const balancer = agent === "claude" ? "puts the one whose weekly quota resets first on top" : "frees each one whose quota cooldown its live usage disproves"; @@ -283,7 +283,9 @@ async function listModels(fetch: Fetch, key: string): Promise const response = await fetch(`${proxyUrl}/v1/models`, { headers: { Authorization: `Bearer ${key}` }, signal: AbortSignal.timeout(5000) }).catch(() => undefined); if (!response) return silent; if (!response.ok) return `The proxy at ${proxyUrl} refuses this machine's key (HTTP ${response.status}).`; - return (((await response.json().catch(() => ({}))) as { data?: Array<{ id?: string }> }).data ?? []).flatMap((model) => (typeof model.id === "string" ? [model.id] : [])); + const body = (await response.json().catch(() => undefined)) as { data?: unknown } | undefined; + if (!Array.isArray(body?.data)) return `The proxy at ${proxyUrl} answers without a model list.`; + return (body.data as Array<{ id?: unknown } | null>).flatMap((model) => (typeof model?.id === "string" ? [model.id] : [])); } const servedModel = { claude: (id: string) => id.includes("claude"), codex: (id: string) => id.startsWith("gpt-") }; @@ -297,15 +299,13 @@ function unserved(models: string[] | string, provider: "claude" | "codex"): stri return models.some(servedModel[provider]) ? undefined : missingModel[provider]; } -const probe = async (fetch: Fetch, key: string, agent: Agent) => unserved(await listModels(fetch, key), agent); - -async function answered(machine: ProxyMachine, key: string, agent: Agent): Promise { - let reason = await probe(machine.fetch, key, agent); - for (let attempt = 1; attempt < answerTries && reason === silent; attempt++) { +async function answering(machine: ProxyMachine, key: string): Promise { + let models = await listModels(machine.fetch, key); + for (let attempt = 1; attempt < answerTries && models === silent; attempt++) { await machine.sleep(1000); - reason = await probe(machine.fetch, key, agent); + models = await listModels(machine.fetch, key); } - return reason; + return models; } type Settings = Record & { env?: Record }; @@ -448,17 +448,19 @@ async function connect(machine: ProxyMachine): Promise { if (agent === "claude") await claudeRoute(machine.home, true); const codex = machine.plan === "codex" || machine.plan === "both" ? await codexRoute(machine.home) : undefined; if (agent === "codex" && typeof codex === "string") throw new Error(`${codex} Nothing changed.`); - const reason = await answered(machine, key, agent); - if (reason) throw new Error(`${reason} Nothing changed.`); + const models = await answering(machine, key); + if (typeof models === "string") throw new Error(`${models} Nothing changed.`); await refuseLinks(machine.home, keyPath(machine.home), `${what} path`); await mkdir(dirname(keyPath(machine.home)), { recursive: true, mode: 0o700 }); await writePrivateFile(keyPath(machine.home), `${key}\n`); - if (agent === "claude") { + const waiting = unserved(models, "claude"); + if (agent === "claude" && waiting) machine.print(`${waiting} Claude Code keeps its own sign-in. The first hedge apply after the proxy serves it sends Claude Code there.`); + else if (agent === "claude") { await sendClaude(machine.home, true); machine.print(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); } if (codex === undefined) return; - const unroutable = typeof codex === "string" ? codex : agent === "codex" ? undefined : await probe(machine.fetch, key, "codex"); + const unroutable = typeof codex === "string" ? codex : unserved(models, "codex"); const failed = unroutable ?? (await sendCodex(machine.home, agent === "claude")); if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); diff --git a/packs/hub/pack.json b/packs/hub/pack.json index c2be651b..669db8f6 100644 --- a/packs/hub/pack.json +++ b/packs/hub/pack.json @@ -670,7 +670,7 @@ "accounts" ], "say": "Hedge cannot count the subscriptions in your own-account proxy. Run hedge cli-proxy doctor on the hub to see why.", - "todo": "Add your own Claude subscriptions to the own-account proxy, until it holds as many that work as hub.proxyAccounts asks for (2 by default). Run on the hub, once for each subscription: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser. hedge cli-proxy accounts says how many work now.", + "todo": "Add your own Claude subscriptions to the own-account proxy, until it holds as many that work as hub.proxyAccounts asks for (2 by default). hedge cli-proxy accounts says how many work now. Run on the hub, once for each subscription: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser", "roles": [ "hub" ], @@ -688,7 +688,7 @@ "accounts" ], "say": "Hedge cannot count the subscriptions in your own-account proxy. Run hedge cli-proxy doctor on the hub to see why.", - "todo": "Add your own Codex subscriptions to the own-account proxy, until it holds as many that work as hub.proxyAccounts asks for (2 by default). Run on the hub, once for each subscription: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser. hedge cli-proxy accounts says how many work now.", + "todo": "Add your own Codex subscriptions to the own-account proxy, until it holds as many that work as hub.proxyAccounts asks for (2 by default). hedge cli-proxy accounts says how many work now. Run on the hub, once for each subscription: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser", "roles": [ "hub" ], @@ -705,7 +705,7 @@ "check" ], "say": "Claude Code cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check here to see why.", - "todo": "Claude Code keeps its own sign-in, because the own-account proxy on the hub serves no Claude model yet: it holds none of your Claude subscriptions that work. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser. The first hedge apply after that sends Claude Code to the proxy.", + "todo": "Claude Code keeps its own sign-in, because the own-account proxy on the hub serves no Claude model yet: it holds none of your Claude subscriptions that work. The first hedge apply after you add one sends Claude Code to the proxy. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser", "roles": [ "hub", "devbox" @@ -725,7 +725,7 @@ "codex" ], "say": "Codex cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check codex here to see why.", - "todo": "Codex keeps its own sign-in, because the own-account proxy on the hub serves no Codex model yet: it holds none of your Codex subscriptions that work. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser. The first hedge apply after that sends Codex to the proxy.", + "todo": "Codex keeps its own sign-in, because the own-account proxy on the hub serves no Codex model yet: it holds none of your Codex subscriptions that work. The first hedge apply after you add one sends Codex to the proxy. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser", "roles": [ "hub", "devbox" diff --git a/packs/hub/skills/hedge-hub/SKILL.md b/packs/hub/skills/hedge-hub/SKILL.md index 99a839f2..b31a8ed1 100644 --- a/packs/hub/skills/hedge-hub/SKILL.md +++ b/packs/hub/skills/hedge-hub/SKILL.md @@ -163,7 +163,10 @@ the person's own subscriptions, never for someone else's. serves no model for the agent, because it holds none of that agent's subscriptions that work, the check exits 4: the agent stays on its own sign-in, and the line is a `To do:` with the login command, not a - failure. The first apply after the proxy + failure. `hedge cli-proxy connect` then saves the key and leaves the + agent on its own sign-in; the first `hedge apply` after the proxy serves + the agent sends it there. Connect still changes nothing when the proxy + does not answer or refuses the key. The first apply after the proxy answers again sends the agent back to the proxy. - **The panel.** The Proxy panel workflow is off by default and needs own-account balancing. With diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index b4cf129a..6caf701a 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -288,22 +288,38 @@ describe("a pack check that waits for the person", () => { expect(doctor.findings).toEqual([waiting]); }); - test("that nothing waits for runs after the steps, so it reads what a step installed, and apply names only its To do", async () => { - const order: string[] = []; + const installedPool = () => { + let installed = false; let code = 4; - const run = async (command: string[]) => (order.push(command.join(" ")), command.join(" ") === "pool-check" ? code : 0); - const steps = [{ id: "install-pool", run: ["install-pool"], check: ["pool-installed"], pack: "base" }]; + const run = async (command: string[]) => { + const words = command.join(" "); + if (words === "install-pool") installed = true; + if (words === "pool-installed") return installed ? 0 : 1; + return words === "pool-check" ? (installed ? code : 1) : 0; + }; + return { run, set: (next: number) => (code = next) }; + }; + const steps = [{ id: "install-pool", run: ["install-pool"], check: ["pool-installed"], pack: "base" }]; + + test("that nothing waits for runs after the steps, so it reads what a step installed, and apply names only its To do", async () => { + const { run, set } = installedPool(); const home = temp.make("home-"); - const applied = await apply(plan({ files: [], checks, steps }), { home, run }); - expect(applied).toMatchObject({ ok: true, findings: [waiting] }); - expect(order.indexOf("pool-check")).toBeGreaterThan(order.indexOf("pool-installed")); - code = 1; + expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [waiting] }); + set(1); expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [] }); expect(await check(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: false, findings: [{ id: "check:pool", pack: "base", say: "Hedge cannot read the pool.", repair: "apply" }] }); - code = 0; + set(0); expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [] }); }); + test("that config waits for and that fails before a step installs what it reads becomes a To do once the step ran, and the config stays out", async () => { + const { run } = installedPool(); + const home = temp.make("home-"); + const applied = await apply(plan({ files: [], checks, steps, config }), { home, adapters: [providerConfigAdapter()], run }); + expect(applied).toMatchObject({ ok: true, findings: [waiting] }); + expect(settings(home)).toBeUndefined(); + }); + test("a check without a todo that exits 4 is a Fix, and a check with one stays a Fix for any other failing code", async () => { const home = temp.make("home-"); const plain = [{ ...checks[0]!, todo: undefined }]; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 13f8f2fe..088fd989 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -178,18 +178,18 @@ export async function apply(plan: Plan, options: { home: string; dryRun?: boolea if (!heldCodes.includes(await run(step.probe, { [undoVariable]: ledgerKey(step) }))) await changeLedger(options.home, "human", ledgerKey(step)); } await undo(items.filter((item) => item.cause !== "pack")); - if (!context.dryRun) report.findings.push(...(await waitingChecks(plan, run, gated.checks))); + if (!context.dryRun) await waitingChecks(plan, run, gated.checks, report.findings); return report; } -/** The To do lines of the checks that wait for the person and that the gate did not run, tested once the steps ran, such as a check that reads what a step installs. */ -async function waitingChecks(plan: Plan, run: Runner, gated: Array<[Owned, number]>): Promise { - const findings: Finding[] = []; - for (const item of plan.checks.filter((check) => check.todo && !gated.some(([ran]) => ran === check))) { - const code = await run(item.run); - if (code === waitsForPerson) findings.push(checkFinding(item, code)); +/** Tests once the steps ran each check that can wait for the person and that the gate did not see pass or wait, such as a check that reads what a step installs. When it waits now, its To do replaces what the gate found. */ +async function waitingChecks(plan: Plan, run: Runner, gated: Array<[Owned, number]>, findings: Finding[]): Promise { + for (const item of plan.checks.filter((check) => check.todo)) { + const before = gated.find(([ran]) => ran === item)?.[1]; + if (before === 0 || before === waitsForPerson || (await run(item.run)) !== waitsForPerson) continue; + const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); + findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, checkFinding(item, waitsForPerson)); } - return findings; } export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { From a360a3cf524a29c9de05140580bf2261f17676e3 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:26:38 +0000 Subject: [PATCH 03/32] Fix the review round 2 findings on the empty proxy pool Connect again takes Hedge's routes off an agent the proxy no longer serves. A model list with an entry that has no model id fails, so it never reads as an empty pool. On the both plan, the doctor fails while a Codex subscription works but the proxy serves no Codex model. The added comments are gone. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 ++++- packs/hub/lib/cli-proxy-testing.ts | 1 - packs/hub/lib/cli-proxy.test.ts | 28 +++++++++++++++++++++++++++- packs/hub/lib/cli-proxy.ts | 15 ++++++++++----- src/pack/types.ts | 3 --- src/reconciler/apply.ts | 1 - 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c99c5a57..86abcc00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,10 @@ proxy serves them. While the proxy answers but holds none of an agent's subscriptions, its route check is a `To do:` line, not a failure. `hedge cli-proxy connect` saves the key on a node in that case, so the - first `hedge apply` after the proxy serves an agent sends it there. A + first `hedge apply` after the proxy serves an agent sends it there. It + also takes Hedge's own route off an agent that the proxy no longer + serves. On the both plan, `hedge doctor` fails while a Codex + subscription works but the proxy serves no Codex model. A proxy that is down, refuses the key, answers without a model list, or runs a plugin Hedge did not install still fails `hedge doctor`. - A pack check can name a `todo`: when its command exits 4, Hedge prints diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts index f6552a12..146a7e3b 100644 --- a/packs/hub/lib/cli-proxy-testing.ts +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -29,7 +29,6 @@ export function fakeProxy(proxy: Proxy = {}) { return { fetch, requests }; } -/** A ProxyMachine whose commands, CLIProxyAPI and its management API are fakes, with the state the proxy would hold. */ export function proxyMachine(make: (prefix: string) => string, fields: Partial = {}, proxy: Proxy = {}, options: { role?: string; checksum?: string } = {}) { const home = fields.home ?? roleHome(make("hedge-cli-proxy-"), options.role ?? "hub"); const fake = fakeRun((command) => { diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index e7cb3c15..1efc7da6 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -406,8 +406,25 @@ test("connect to a proxy that holds none of your subscriptions saves the key, ke expect(existsSync(keyFile(refused.home))).toBe(false); }); +test("connect again after the pool empties takes Hedge's routes off Claude Code and Codex, and keeps your own settings", async () => { + const { machine, home, printed } = client({ plan: "both" }, { models: [] }); + connected(home); + codexRouted(home); + expect(await cliProxy("connect", machine)).toBe(0); + expect(readJson(settingsFile(home))).toEqual({ model: "opus", env: { MY_FLAG: "1" } }); + expect(readCodex(home)).toEqual(mine); + expect(printed.join("\n")).toContain("Claude Code keeps its own sign-in."); + expect(printed.at(-1)).toEndWith("Codex keeps its own sign-in."); +}); + +test("connect refuses a model list it cannot read, and saves no key", async () => { + const refused = client({}, { modelList: '{"data":[null,{"id":7}]}' }); + await expect(cliProxy("connect", refused.machine)).rejects.toThrow("answers without a model list. Nothing changed."); + expect(existsSync(keyFile(refused.home))).toBe(false); +}); + test("a model list the proxy cannot give fails the check and the doctor, and never reads as an empty pool", async () => { - for (const modelList of ["not json", "{}", '{"data":null}']) { + for (const modelList of ["not json", "{}", '{"data":null}', '{"data":[null,{"id":7}]}', '{"data":[{"id":""}]}', '{"data":[{"id":"claude-sonnet-5-5"},{}]}']) { const routed = client({}, { modelList }); connected(routed.home); expect(await cliProxy("check", routed.machine)).toBe(1); @@ -418,6 +435,15 @@ test("a model list the proxy cannot give fails the check and the doctor, and nev await expect(cliProxy("doctor", hubMachine({ home }, { credentials: [], modelList: "not json" }).machine)).rejects.toThrow("answers without a model list"); }); +test("on the both plan, the doctor fails while a Codex subscription works but the proxy serves no Codex model", async () => { + const { machine, home } = hubMachine({ plan: "both" }); + await cliProxy("install", machine); + const claudeOnly = [{ provider: "claude" }, { provider: "claude" }]; + await expect(cliProxy("doctor", hubMachine({ home, plan: "both" }, { credentials: [...claudeOnly, { provider: "codex" }], models: ["claude-sonnet-5-5"] }).machine)).rejects.toThrow("lists no Codex model"); + expect(await cliProxy("doctor", hubMachine({ home, plan: "both" }, { credentials: [...claudeOnly, { provider: "codex", disabled: true }], models: ["claude-sonnet-5-5"] }).machine)).toBe(0); + expect(await cliProxy("doctor", hubMachine({ home, plan: "both" }, { credentials: [...claudeOnly, { provider: "codex" }], models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }).machine)).toBe(0); +}); + test("with hub.proxyAccounts at 0, the doctor and accounts pass with an empty pool, and the doctor still wants a model once the pool holds one", async () => { const { machine, home } = hubMachine(); await cliProxy("install", machine); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 3698fb42..303c69ca 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -222,7 +222,8 @@ async function doctor(machine: ProxyMachine, paths: Layout): Promise { if (strategy !== "fill-first") throw new Error(`The proxy routes with ${strategy ?? "no strategy"}, not fill-first. Run hedge cli-proxy-balance to restore it.`); const { agent, count, short, expired, enabled } = await pool(machine, paths, managementKey); const models = await listModels(machine.fetch, key); - const reason = typeof models === "string" ? models : count ? unserved(models, agent) : undefined; + const pooledAgents: Agent[] = machine.plan === "both" ? ["claude", "codex"] : [agent]; + const reason = typeof models === "string" ? models : pooledAgents.map((provider) => (enabled(provider) ? unserved(models, provider) : undefined)).find(Boolean); if (reason) throw new Error(reason); const beside = machine.plan === "both" ? ` and ${subscriptions(enabled("codex"), "codex")}` : ""; const balancer = agent === "claude" ? "puts the one whose weekly quota resets first on top" : "frees each one whose quota cooldown its live usage disproves"; @@ -284,8 +285,9 @@ async function listModels(fetch: Fetch, key: string): Promise if (!response) return silent; if (!response.ok) return `The proxy at ${proxyUrl} refuses this machine's key (HTTP ${response.status}).`; const body = (await response.json().catch(() => undefined)) as { data?: unknown } | undefined; - if (!Array.isArray(body?.data)) return `The proxy at ${proxyUrl} answers without a model list.`; - return (body.data as Array<{ id?: unknown } | null>).flatMap((model) => (typeof model?.id === "string" ? [model.id] : [])); + const ids = Array.isArray(body?.data) ? (body.data as Array<{ id?: unknown } | null>).map((model) => model?.id) : undefined; + if (!ids?.every((id): id is string => typeof id === "string" && id.length > 0)) return `The proxy at ${proxyUrl} answers without a model list.`; + return ids; } const servedModel = { claude: (id: string) => id.includes("claude"), codex: (id: string) => id.startsWith("gpt-") }; @@ -454,13 +456,16 @@ async function connect(machine: ProxyMachine): Promise { await mkdir(dirname(keyPath(machine.home)), { recursive: true, mode: 0o700 }); await writePrivateFile(keyPath(machine.home), `${key}\n`); const waiting = unserved(models, "claude"); - if (agent === "claude" && waiting) machine.print(`${waiting} Claude Code keeps its own sign-in. The first hedge apply after the proxy serves it sends Claude Code there.`); - else if (agent === "claude") { + if (agent === "claude" && waiting) { + await sendClaude(machine.home, false); + machine.print(`${waiting} Claude Code keeps its own sign-in. The first hedge apply after the proxy serves it sends Claude Code there.`); + } else if (agent === "claude") { await sendClaude(machine.home, true); machine.print(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); } if (codex === undefined) return; const unroutable = typeof codex === "string" ? codex : unserved(models, "codex"); + if (unroutable && typeof codex !== "string") await sendCodexHome(machine.home); const failed = unroutable ?? (await sendCodex(machine.home, agent === "claude")); if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); diff --git a/src/pack/types.ts b/src/pack/types.ts index 34b1a4dc..fb07e09c 100644 --- a/src/pack/types.ts +++ b/src/pack/types.ts @@ -163,10 +163,8 @@ export type PackManifest = { subcommands?: Record; }; -/** The exit code of a pack check that waits for the person, such as a pool that holds none of their accounts yet. The check does not pass, so config that waits for it stays out, but it fails nothing: Hedge says the check's todo as a To do line. */ export const waitsForPerson = 4; -/** say is the Fix line while run fails. todo is the To do line while run exits waitsForPerson. */ export type PackCheck = Scope & { id: string; run: string[]; say: string; todo?: string }; export const handlerSeams = ["review-bot", "heavy-route", "pr-command", "slack-capture", "secret-rows", "secret-providers", "proof-receipt"] as const; @@ -259,7 +257,6 @@ export type Plan = { dropped: Array<{ pack: string; item: string; reason: string; check?: string; value?: unknown }>; }; -/** repair: who fixes it; a finding with a repair fails apply or doctor. todo: a To do line that fails nothing. */ export type Finding = { id: string; pack: string; say: string; repair?: "apply" | "human"; todo?: true }; export type Report = { ok: boolean; actions: Action[]; findings: Finding[]; humanLeft: Array> }; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 088fd989..7f5b3fb1 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -182,7 +182,6 @@ export async function apply(plan: Plan, options: { home: string; dryRun?: boolea return report; } -/** Tests once the steps ran each check that can wait for the person and that the gate did not see pass or wait, such as a check that reads what a step installs. When it waits now, its To do replaces what the gate found. */ async function waitingChecks(plan: Plan, run: Runner, gated: Array<[Owned, number]>, findings: Finding[]): Promise { for (const item of plan.checks.filter((check) => check.todo)) { const before = gated.find(([ran]) => ran === item)?.[1]; From b0b6d785b2c9e1e84f526d687c2c7b5c5145da0b Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:35:42 +0000 Subject: [PATCH 04/32] Fix the review round 3 findings on reconnect cleanup Reconnect to an empty pool takes the proxy address off Claude Code only while Hedge's key helper goes with it. When Hedge cannot take its route off Codex, because the config is a link or cannot be written, connect says Codex may still use the proxy instead of failing after the key is saved. Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy.test.ts | 33 +++++++++++++++++++++++++++++++++ packs/hub/lib/cli-proxy.ts | 23 +++++++++++++++++++---- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 1efc7da6..314ea91f 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -417,6 +417,39 @@ test("connect again after the pool empties takes Hedge's routes off Claude Code expect(printed.at(-1)).toEndWith("Codex keeps its own sign-in."); }); +test("connect again after the pool empties leaves a proxy address that Hedge's key helper does not go with", async () => { + const { machine, home } = client({}, { models: [] }); + mkdirSync(join(home, ".claude"), { recursive: true }); + const theirs = { env: { ANTHROPIC_BASE_URL: proxyUrl, ANTHROPIC_API_KEY: "sample" } }; + writeFileSync(settingsFile(home), JSON.stringify(theirs)); + expect(await cliProxy("connect", machine)).toBe(0); + expect(readJson(settingsFile(home))).toEqual(theirs); +}); + +test("connect again after the pool empties says when Codex may still use the proxy, and leaves a linked or locked config", async () => { + const linked = client({ plan: "both" }, { models: [] }); + connected(linked.home); + const outside = temp.make("elsewhere-"); + writeFileSync(join(outside, "config.toml"), stringify({ ...mine, model_provider: "hedge-proxy", model_providers: { ...mine.model_providers, "hedge-proxy": hedgeProxy } })); + symlinkSync(outside, join(linked.home, ".codex")); + expect(await cliProxy("connect", linked.machine)).toBe(0); + expect(readFileSync(join(outside, "config.toml"), "utf8")).toContain('model_provider = "hedge-proxy"'); + expect(linked.printed.at(-1)).toEndWith("Codex may still use the proxy. Fix that, then run hedge apply."); + + const locked = client({ plan: "both" }, { models: [] }); + connected(locked.home); + codexRouted(locked.home); + chmodSync(join(locked.home, ".codex"), 0o500); + try { + expect(await cliProxy("connect", locked.machine)).toBe(0); + } finally { + chmodSync(join(locked.home, ".codex"), 0o700); + } + expect(readCodex(locked.home).model_provider).toBe("hedge-proxy"); + expect(readJson(settingsFile(locked.home)).apiKeyHelper).toBeUndefined(); + expect(locked.printed.at(-1)).toEndWith("Codex may still use the proxy. Fix that, then run hedge apply."); +}); + test("connect refuses a model list it cannot read, and saves no key", async () => { const refused = client({}, { modelList: '{"data":[null,{"id":7}]}' }); await expect(cliProxy("connect", refused.machine)).rejects.toThrow("answers without a model list. Nothing changed."); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 303c69ca..4c46a26d 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -419,9 +419,12 @@ async function codexUnserved(home: string, models: string[] | string): Promise isObject(table) && (["base_url", "wire_api", "auth"] as const).every((field) => Bun.deepEquals(table[field], codexProviderTable[field])); const ownTable = (path: string) => `${path} has its own ${codexProvider} provider table, which does not reach the proxy with this machine's key.`; +const unreadableCodex = "Hedge cannot read ~/.codex/config.toml, or it is a link, so Hedge leaves it alone."; +const codexMayStay = "Codex may still use the proxy. Fix that, then run hedge apply."; + async function codexRoute(home: string): Promise Promise)> { const read = await readableCodex(home); - if (!read) return "Hedge cannot read ~/.codex/config.toml, or it is a link, so Hedge leaves it alone."; + if (!read) return unreadableCodex; const { path, text, config, providers } = read; const table = providers?.[codexProvider]; if (config.model_provider !== undefined && config.model_provider !== codexProvider) return `Keep your Codex setting model_provider in ${path}: Hedge did not set it. Remove it to use the proxy.`; @@ -457,7 +460,7 @@ async function connect(machine: ProxyMachine): Promise { await writePrivateFile(keyPath(machine.home), `${key}\n`); const waiting = unserved(models, "claude"); if (agent === "claude" && waiting) { - await sendClaude(machine.home, false); + if (await hedgeKeyHelper(machine.home)) await sendClaude(machine.home, false); machine.print(`${waiting} Claude Code keeps its own sign-in. The first hedge apply after the proxy serves it sends Claude Code there.`); } else if (agent === "claude") { await sendClaude(machine.home, true); @@ -465,12 +468,24 @@ async function connect(machine: ProxyMachine): Promise { } if (codex === undefined) return; const unroutable = typeof codex === "string" ? codex : unserved(models, "codex"); - if (unroutable && typeof codex !== "string") await sendCodexHome(machine.home); + const left = !unroutable ? undefined : typeof codex !== "string" ? await leaveCodex(machine.home) : codex === unreadableCodex ? codexMayStay : undefined; const failed = unroutable ?? (await sendCodex(machine.home, agent === "claude")); - if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); + if (failed) return machine.print(`${failed} ${left ?? "Codex keeps its own sign-in."}`); machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); } +async function hedgeKeyHelper(home: string): Promise { + const text = await readIfThere(join(home, ".claude/settings.json")); + return text !== undefined && ownHelper((JSON.parse(text) as Settings).apiKeyHelper); +} + +async function leaveCodex(home: string): Promise { + const failed = await sendCodexHome(home).then(() => undefined, (error: Error) => error.message); + const read = await readableCodex(home); + if (!failed && read && !read.routed) return undefined; + return `${failed ?? unreadableCodex} ${codexMayStay}`; +} + async function disconnect(machine: ProxyMachine): Promise { if (readRole(machine.home) === "hub") throw new Error("On the hub, turn the workflow off instead: hedge packs --workflows own-account-balancing=off"); await sendClaude(machine.home, false); From 1731ae4b3ded9ea5adb7b82b19d858d06adfd73b Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:43:54 +0000 Subject: [PATCH 05/32] Leave route cleanup on an empty pool to hedge apply Connect to a proxy that serves an agent no model now saves the key and changes no agent settings. hedge apply already takes Hedge's route off an agent whose route check waits for the person, so the cleanup that connect did twice is gone, with its edge cases. A hub test shows apply taking Claude Code off the proxy once its pool empties. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 6 ++-- packs/hub/hub.test.ts | 18 +++++++++- packs/hub/lib/cli-proxy.test.ts | 53 +++++++---------------------- packs/hub/lib/cli-proxy.ts | 35 ++++++------------- packs/hub/skills/hedge-hub/SKILL.md | 6 ++-- 5 files changed, 45 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86abcc00..b9079338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,9 +15,9 @@ proxy serves them. While the proxy answers but holds none of an agent's subscriptions, its route check is a `To do:` line, not a failure. `hedge cli-proxy connect` saves the key on a node in that case, so the - first `hedge apply` after the proxy serves an agent sends it there. It - also takes Hedge's own route off an agent that the proxy no longer - serves. On the both plan, `hedge doctor` fails while a Codex + first `hedge apply` after the proxy serves an agent sends it there. + Connect changes no agent settings in that case: `hedge apply` takes + Hedge's route off an agent that the proxy no longer serves. On the both plan, `hedge doctor` fails while a Codex subscription works but the proxy serves no Codex model. A proxy that is down, refuses the key, answers without a model list, or runs a plugin Hedge did not install still fails `hedge doctor`. diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 23466801..91171647 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -6,7 +6,7 @@ import { providerConfigAdapter } from "../../src/adapters/provider-config/adapte import { apply, check } from "../../src/reconciler/apply.ts"; import { boxCommand } from "./lib/box-command.ts"; import { cliProxy, codexProviderTable, type ProxyMachine } from "./lib/cli-proxy.ts"; -import { proxyMachine } from "./lib/cli-proxy-testing.ts"; +import { proxyMachine, type Proxy } from "./lib/cli-proxy-testing.ts"; import { Coder } from "./lib/coder.ts"; import { fakeMachine } from "./lib/testing.ts"; import type { AvailableSource, MachineFile, MachineRole, Plan, Profile } from "../../src/pack/types.ts"; @@ -466,5 +466,21 @@ describe("the Own-account balancing workflow", () => { expect(applied).toMatchObject({ ok: true, findings: [{ id: "check:cli-proxy-accounts", todo: true }] }); expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).env.ANTHROPIC_BASE_URL).toBe("http://127.0.0.1:8317"); }); + + test("takes Claude Code off the proxy once its pool empties, and lists the To do", async () => { + const proxy: Proxy = { credentials: [{ provider: "claude" }] }; + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, proxy); + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine) }; + const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); + await apply(result, options); + await apply(result, options); + expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).apiKeyHelper).toBeDefined(); + Object.assign(proxy, { credentials: [], models: [] }); + const emptied = await apply(result, options); + expect(emptied.ok).toBe(true); + expect(emptied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); + const settings = existsSync(join(home, ".claude/settings.json")) ? JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")) : {}; + expect([settings.env?.ANTHROPIC_BASE_URL, settings.apiKeyHelper]).toEqual([undefined, undefined]); + }); }); }); diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 314ea91f..8ff1d4bc 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -285,7 +285,7 @@ test("connect on a Codex plan checks the proxy for a Codex model, sends Codex to const noCodex = client({ plan: "codex" }); expect(await cliProxy("connect", noCodex.machine)).toBe(0); - expect(noCodex.printed).toEqual([`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Codex keeps its own sign-in.`]); + expect(noCodex.printed).toEqual([`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it.`]); expect(readFileSync(keyFile(noCodex.home), "utf8")).toBe(`${clientKey}\n`); expect(existsSync(codexFile(noCodex.home))).toBe(false); expect(await cliProxy("check", noCodex.machine, "codex")).toBe(waitsForPerson); @@ -307,7 +307,7 @@ test("connect on the both plan sends Claude Code to the proxy, and Codex too whi expect(await cliProxy("connect", claudeOnly.machine)).toBe(0); expect(readJson(settingsFile(claudeOnly.home)).apiKeyHelper).toBe(keyHelper); expect(existsSync(codexFile(claudeOnly.home))).toBe(false); - expect(claudeOnly.printed.join("\n")).toContain("lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Codex keeps its own sign-in."); + expect(claudeOnly.printed.join("\n")).toContain("lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it."); }); test("connect never selects a hedge-proxy table that cannot reach the proxy with this machine's key", async () => { @@ -399,55 +399,26 @@ test("connect to a proxy that holds none of your subscriptions saves the key, ke expect(await cliProxy("connect", machine)).toBe(0); expect(readFileSync(keyFile(home), "utf8")).toBe(`${clientKey}\n`); expect(readJson(settingsFile(home))).toEqual({ model: "opus" }); - expect(printed).toEqual([`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Claude Code keeps its own sign-in. The first hedge apply after the proxy serves it sends Claude Code there.`]); + expect(printed).toEqual([`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. hedge apply keeps Claude Code on its own sign-in until the proxy serves it.`]); expect(await cliProxy("check", machine)).toBe(waitsForPerson); const refused = client({}, { models: [], refuses: true }); await expect(cliProxy("connect", refused.machine)).rejects.toThrow("refuses this machine's key (HTTP 401). Nothing changed."); expect(existsSync(keyFile(refused.home))).toBe(false); }); -test("connect again after the pool empties takes Hedge's routes off Claude Code and Codex, and keeps your own settings", async () => { +test("connect again after the pool empties saves the key, and leaves taking the routes off to hedge apply", async () => { const { machine, home, printed } = client({ plan: "both" }, { models: [] }); connected(home); codexRouted(home); + const before = [readFileSync(settingsFile(home), "utf8"), readFileSync(codexFile(home), "utf8")]; expect(await cliProxy("connect", machine)).toBe(0); - expect(readJson(settingsFile(home))).toEqual({ model: "opus", env: { MY_FLAG: "1" } }); - expect(readCodex(home)).toEqual(mine); - expect(printed.join("\n")).toContain("Claude Code keeps its own sign-in."); - expect(printed.at(-1)).toEndWith("Codex keeps its own sign-in."); -}); - -test("connect again after the pool empties leaves a proxy address that Hedge's key helper does not go with", async () => { - const { machine, home } = client({}, { models: [] }); - mkdirSync(join(home, ".claude"), { recursive: true }); - const theirs = { env: { ANTHROPIC_BASE_URL: proxyUrl, ANTHROPIC_API_KEY: "sample" } }; - writeFileSync(settingsFile(home), JSON.stringify(theirs)); - expect(await cliProxy("connect", machine)).toBe(0); - expect(readJson(settingsFile(home))).toEqual(theirs); -}); - -test("connect again after the pool empties says when Codex may still use the proxy, and leaves a linked or locked config", async () => { - const linked = client({ plan: "both" }, { models: [] }); - connected(linked.home); - const outside = temp.make("elsewhere-"); - writeFileSync(join(outside, "config.toml"), stringify({ ...mine, model_provider: "hedge-proxy", model_providers: { ...mine.model_providers, "hedge-proxy": hedgeProxy } })); - symlinkSync(outside, join(linked.home, ".codex")); - expect(await cliProxy("connect", linked.machine)).toBe(0); - expect(readFileSync(join(outside, "config.toml"), "utf8")).toContain('model_provider = "hedge-proxy"'); - expect(linked.printed.at(-1)).toEndWith("Codex may still use the proxy. Fix that, then run hedge apply."); - - const locked = client({ plan: "both" }, { models: [] }); - connected(locked.home); - codexRouted(locked.home); - chmodSync(join(locked.home, ".codex"), 0o500); - try { - expect(await cliProxy("connect", locked.machine)).toBe(0); - } finally { - chmodSync(join(locked.home, ".codex"), 0o700); - } - expect(readCodex(locked.home).model_provider).toBe("hedge-proxy"); - expect(readJson(settingsFile(locked.home)).apiKeyHelper).toBeUndefined(); - expect(locked.printed.at(-1)).toEndWith("Codex may still use the proxy. Fix that, then run hedge apply."); + expect([readFileSync(settingsFile(home), "utf8"), readFileSync(codexFile(home), "utf8")]).toEqual(before); + expect(printed).toEqual([ + `The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. hedge apply keeps Claude Code on its own sign-in until the proxy serves it.`, + `The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it.`, + ]); + expect(await cliProxy("check", machine)).toBe(waitsForPerson); + expect(await cliProxy("check", machine, "codex")).toBe(waitsForPerson); }); test("connect refuses a model list it cannot read, and saves no key", async () => { diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 4c46a26d..d8b1a704 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -399,12 +399,14 @@ async function sendCodexHome(home: string): Promise { export type Agent = "claude" | "codex"; +const applyWaits = (name: string) => `hedge apply keeps ${name} on its own sign-in until the proxy serves it.`; + async function check(machine: ProxyMachine, agent: Agent): Promise { const key = await savedKey(machine.home); const models = key ? await listModels(machine.fetch, key) : `This machine has no proxy key at ${keyPath(machine.home)}. On the hub, hedge cli-proxy doctor prints the connect command.`; const reason = agent === "claude" ? unserved(models, "claude") : await codexUnserved(machine.home, models); const name = agent === "claude" ? "Claude Code" : "Codex"; - machine.print(reason ? `${reason} hedge apply keeps ${name} on its own sign-in until the proxy serves it.` : `${name} reaches your own-account proxy at ${proxyUrl}.`); + machine.print(reason ? `${reason} ${applyWaits(name)}` : `${name} reaches your own-account proxy at ${proxyUrl}.`); if (!reason) return 0; return reason === missingModel[agent] ? waitsForPerson : 1; } @@ -419,12 +421,9 @@ async function codexUnserved(home: string, models: string[] | string): Promise isObject(table) && (["base_url", "wire_api", "auth"] as const).every((field) => Bun.deepEquals(table[field], codexProviderTable[field])); const ownTable = (path: string) => `${path} has its own ${codexProvider} provider table, which does not reach the proxy with this machine's key.`; -const unreadableCodex = "Hedge cannot read ~/.codex/config.toml, or it is a link, so Hedge leaves it alone."; -const codexMayStay = "Codex may still use the proxy. Fix that, then run hedge apply."; - async function codexRoute(home: string): Promise Promise)> { const read = await readableCodex(home); - if (!read) return unreadableCodex; + if (!read) return "Hedge cannot read ~/.codex/config.toml, or it is a link, so Hedge leaves it alone."; const { path, text, config, providers } = read; const table = providers?.[codexProvider]; if (config.model_provider !== undefined && config.model_provider !== codexProvider) return `Keep your Codex setting model_provider in ${path}: Hedge did not set it. Remove it to use the proxy.`; @@ -459,33 +458,19 @@ async function connect(machine: ProxyMachine): Promise { await mkdir(dirname(keyPath(machine.home)), { recursive: true, mode: 0o700 }); await writePrivateFile(keyPath(machine.home), `${key}\n`); const waiting = unserved(models, "claude"); - if (agent === "claude" && waiting) { - if (await hedgeKeyHelper(machine.home)) await sendClaude(machine.home, false); - machine.print(`${waiting} Claude Code keeps its own sign-in. The first hedge apply after the proxy serves it sends Claude Code there.`); - } else if (agent === "claude") { + if (agent === "claude" && waiting) machine.print(`${waiting} ${applyWaits("Claude Code")}`); + else if (agent === "claude") { await sendClaude(machine.home, true); machine.print(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); } if (codex === undefined) return; - const unroutable = typeof codex === "string" ? codex : unserved(models, "codex"); - const left = !unroutable ? undefined : typeof codex !== "string" ? await leaveCodex(machine.home) : codex === unreadableCodex ? codexMayStay : undefined; - const failed = unroutable ?? (await sendCodex(machine.home, agent === "claude")); - if (failed) return machine.print(`${failed} ${left ?? "Codex keeps its own sign-in."}`); + const missing = typeof codex === "string" ? undefined : unserved(models, "codex"); + if (missing) return machine.print(`${missing} ${applyWaits("Codex")}`); + const failed = typeof codex === "string" ? codex : await sendCodex(machine.home, agent === "claude"); + if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); } -async function hedgeKeyHelper(home: string): Promise { - const text = await readIfThere(join(home, ".claude/settings.json")); - return text !== undefined && ownHelper((JSON.parse(text) as Settings).apiKeyHelper); -} - -async function leaveCodex(home: string): Promise { - const failed = await sendCodexHome(home).then(() => undefined, (error: Error) => error.message); - const read = await readableCodex(home); - if (!failed && read && !read.routed) return undefined; - return `${failed ?? unreadableCodex} ${codexMayStay}`; -} - async function disconnect(machine: ProxyMachine): Promise { if (readRole(machine.home) === "hub") throw new Error("On the hub, turn the workflow off instead: hedge packs --workflows own-account-balancing=off"); await sendClaude(machine.home, false); diff --git a/packs/hub/skills/hedge-hub/SKILL.md b/packs/hub/skills/hedge-hub/SKILL.md index b31a8ed1..b402074a 100644 --- a/packs/hub/skills/hedge-hub/SKILL.md +++ b/packs/hub/skills/hedge-hub/SKILL.md @@ -163,9 +163,9 @@ the person's own subscriptions, never for someone else's. serves no model for the agent, because it holds none of that agent's subscriptions that work, the check exits 4: the agent stays on its own sign-in, and the line is a `To do:` with the login command, not a - failure. `hedge cli-proxy connect` then saves the key and leaves the - agent on its own sign-in; the first `hedge apply` after the proxy serves - the agent sends it there. Connect still changes nothing when the proxy + failure. `hedge cli-proxy connect` then saves the key and changes no + agent settings; `hedge apply` keeps the agent on its own sign-in until + the proxy serves it, and then sends it there. Connect still changes nothing when the proxy does not answer or refuses the key. The first apply after the proxy answers again sends the agent back to the proxy. - **The panel.** The Proxy panel workflow is off by default and needs From 6abe2ac50f58d4e0290b24be54914a55d13f2c54 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:49:50 +0000 Subject: [PATCH 06/32] Tell the person to run hedge apply after connect to an empty pool Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy.test.ts | 10 +++++----- packs/hub/lib/cli-proxy.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 8ff1d4bc..651ebca5 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -285,7 +285,7 @@ test("connect on a Codex plan checks the proxy for a Codex model, sends Codex to const noCodex = client({ plan: "codex" }); expect(await cliProxy("connect", noCodex.machine)).toBe(0); - expect(noCodex.printed).toEqual([`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it.`]); + expect(noCodex.printed).toEqual([`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`]); expect(readFileSync(keyFile(noCodex.home), "utf8")).toBe(`${clientKey}\n`); expect(existsSync(codexFile(noCodex.home))).toBe(false); expect(await cliProxy("check", noCodex.machine, "codex")).toBe(waitsForPerson); @@ -307,7 +307,7 @@ test("connect on the both plan sends Claude Code to the proxy, and Codex too whi expect(await cliProxy("connect", claudeOnly.machine)).toBe(0); expect(readJson(settingsFile(claudeOnly.home)).apiKeyHelper).toBe(keyHelper); expect(existsSync(codexFile(claudeOnly.home))).toBe(false); - expect(claudeOnly.printed.join("\n")).toContain("lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it."); + expect(claudeOnly.printed.join("\n")).toContain("lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it."); }); test("connect never selects a hedge-proxy table that cannot reach the proxy with this machine's key", async () => { @@ -399,7 +399,7 @@ test("connect to a proxy that holds none of your subscriptions saves the key, ke expect(await cliProxy("connect", machine)).toBe(0); expect(readFileSync(keyFile(home), "utf8")).toBe(`${clientKey}\n`); expect(readJson(settingsFile(home))).toEqual({ model: "opus" }); - expect(printed).toEqual([`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. hedge apply keeps Claude Code on its own sign-in until the proxy serves it.`]); + expect(printed).toEqual([`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`]); expect(await cliProxy("check", machine)).toBe(waitsForPerson); const refused = client({}, { models: [], refuses: true }); await expect(cliProxy("connect", refused.machine)).rejects.toThrow("refuses this machine's key (HTTP 401). Nothing changed."); @@ -414,8 +414,8 @@ test("connect again after the pool empties saves the key, and leaves taking the expect(await cliProxy("connect", machine)).toBe(0); expect([readFileSync(settingsFile(home), "utf8"), readFileSync(codexFile(home), "utf8")]).toEqual(before); expect(printed).toEqual([ - `The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. hedge apply keeps Claude Code on its own sign-in until the proxy serves it.`, - `The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it.`, + `The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`, + `The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`, ]); expect(await cliProxy("check", machine)).toBe(waitsForPerson); expect(await cliProxy("check", machine, "codex")).toBe(waitsForPerson); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index d8b1a704..da8ac1d4 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -458,14 +458,14 @@ async function connect(machine: ProxyMachine): Promise { await mkdir(dirname(keyPath(machine.home)), { recursive: true, mode: 0o700 }); await writePrivateFile(keyPath(machine.home), `${key}\n`); const waiting = unserved(models, "claude"); - if (agent === "claude" && waiting) machine.print(`${waiting} ${applyWaits("Claude Code")}`); + if (agent === "claude" && waiting) machine.print(`${waiting} Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`); else if (agent === "claude") { await sendClaude(machine.home, true); machine.print(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); } if (codex === undefined) return; const missing = typeof codex === "string" ? undefined : unserved(models, "codex"); - if (missing) return machine.print(`${missing} ${applyWaits("Codex")}`); + if (missing) return machine.print(`${missing} Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`); const failed = typeof codex === "string" ? codex : await sendCodex(machine.home, agent === "claude"); if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); From b17861b978f24ebd1c34645c3807c0ed74ec2019 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:56:34 +0000 Subject: [PATCH 07/32] Fail on a subscription list the proxy cannot give A management reply without a list, or with a row that names no provider, now fails the doctor and hedge cli-proxy accounts instead of reading as an empty pool. The outage test now reaches the model list. Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy-testing.ts | 4 +++- packs/hub/lib/cli-proxy.test.ts | 10 +++++++--- packs/hub/lib/cli-proxy.ts | 3 ++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts index 146a7e3b..a2e4307d 100644 --- a/packs/hub/lib/cli-proxy-testing.ts +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -9,7 +9,7 @@ export const archive = "fake release archive"; export const release = { version: "9.9.9", url: "https://example.com/proxy.tar.gz", sha256: new Bun.CryptoHasher("sha256").update(archive).digest("hex") }; export const dataDir = (home: string) => join(home, ".local/share/hedge-cli-proxy"); -export type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean; refuses?: boolean; modelList?: string }; +export type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean; refuses?: boolean; modelList?: string; silentModels?: boolean; authFiles?: unknown }; export function fakeProxy(proxy: Proxy = {}) { const requests: Array<{ path: string; method: string; auth: string }> = []; @@ -20,9 +20,11 @@ export function fakeProxy(proxy: Proxy = {}) { if (proxy.catalog !== undefined && url.pathname.endsWith("/codex_client_models.json")) return Response.json(proxy.catalog); if (proxy.registry !== undefined && url.pathname.endsWith("/models.json")) return Response.json(proxy.registry); if (proxy.down || url.origin !== proxyUrl) throw new TypeError("connection refused"); + if (url.pathname === "/v1/models" && proxy.silentModels) throw new TypeError("connection refused"); if (url.pathname === "/v1/models" && proxy.modelList !== undefined) return new Response(proxy.modelList, { status: 200 }); if (url.pathname === "/v1/models") return auth === `Bearer ${clientKey}` && !proxy.refuses ? Response.json({ data: (proxy.models ?? ["claude-sonnet-5-5"]).map((id) => ({ id })) }) : new Response("no", { status: 401 }); if (url.pathname === "/v0/management/routing/strategy") return Response.json({ strategy: proxy.strategy ?? "fill-first" }); + if (url.pathname === "/v0/management/auth-files" && proxy.authFiles !== undefined) return Response.json(proxy.authFiles); if (url.pathname === "/v0/management/auth-files") return Response.json({ files: proxy.credentials ?? [{ provider: "claude" }, { provider: "claude" }] }); return new Response("missing", { status: 404 }); }; diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 651ebca5..0444d250 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -171,9 +171,13 @@ test("the doctor still fails with an empty pool while the proxy does not answer await cliProxy("install", machine); const refused = hubMachine({ home }, { credentials: [], models: [], refuses: true }); await expect(cliProxy("doctor", refused.machine)).rejects.toThrow(`The proxy at ${proxyUrl} refuses this machine's key (HTTP 401).`); - const silent = hubMachine({ home }, { credentials: [], models: [], down: true }); - await expect(cliProxy("doctor", silent.machine)).rejects.toThrow(); - expect(silent.requests.some((request) => request.path === "/v0/management/routing/strategy")).toBe(true); + const silent = hubMachine({ home }, { credentials: [], silentModels: true }); + await expect(cliProxy("doctor", silent.machine)).rejects.toThrow(`The proxy at ${proxyUrl} does not answer.`); + for (const authFiles of [{}, { files: null }, { files: [{ name: "auth.json", disabled: false }] }, { files: [null] }]) { + const unreadable = hubMachine({ home }, { authFiles, models: [] }); + await expect(cliProxy("doctor", unreadable.machine)).rejects.toThrow("a list of subscriptions that Hedge cannot read"); + await expect(cliProxy("accounts", hubMachine({ home }, { authFiles }).machine)).rejects.toThrow("a list of subscriptions that Hedge cannot read"); + } const installed = hubMachine({ home }); const stopped = hubMachine({ home, run: async (command) => (command.includes("is-active") ? { code: 3, stdout: "", stderr: "inactive" } : installed.machine.run(command)) }, { credentials: [], models: [] }); await expect(cliProxy("doctor", stopped.machine)).rejects.toThrow("is-active"); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index da8ac1d4..93370fa3 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -240,7 +240,8 @@ async function doctor(machine: ProxyMachine, paths: Layout): Promise { } async function pool(machine: ProxyMachine, paths: Layout, managementKey: string) { - const { files } = (await management(machine.fetch, managementKey, "auth-files")) as { files: AuthFile[] }; + const { files } = (await management(machine.fetch, managementKey, "auth-files")) as { files?: Array }; + if (!Array.isArray(files) || !files.every((file): file is AuthFile => typeof file?.provider === "string")) throw new Error("The proxy answers with a list of subscriptions that Hedge cannot read."); const expired = files.filter((file) => (file.provider === "claude" || file.provider === "codex") && !file.disabled && loginExpired(file)); const enabled = (agent: Agent) => files.filter((file) => file.provider === agent && !file.disabled && !expired.includes(file)).length; const agent = pooled(machine.plan); From 11ffa623f8ca0cb7e6306643554c6b8c43c533be Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:01:01 +0000 Subject: [PATCH 08/32] Name an empty management reply as a list Hedge cannot read Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy.test.ts | 2 +- packs/hub/lib/cli-proxy.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 0444d250..3f2e681b 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -173,7 +173,7 @@ test("the doctor still fails with an empty pool while the proxy does not answer await expect(cliProxy("doctor", refused.machine)).rejects.toThrow(`The proxy at ${proxyUrl} refuses this machine's key (HTTP 401).`); const silent = hubMachine({ home }, { credentials: [], silentModels: true }); await expect(cliProxy("doctor", silent.machine)).rejects.toThrow(`The proxy at ${proxyUrl} does not answer.`); - for (const authFiles of [{}, { files: null }, { files: [{ name: "auth.json", disabled: false }] }, { files: [null] }]) { + for (const authFiles of [null, {}, { files: null }, { files: [{ name: "auth.json", disabled: false }] }, { files: [null] }]) { const unreadable = hubMachine({ home }, { authFiles, models: [] }); await expect(cliProxy("doctor", unreadable.machine)).rejects.toThrow("a list of subscriptions that Hedge cannot read"); await expect(cliProxy("accounts", hubMachine({ home }, { authFiles }).machine)).rejects.toThrow("a list of subscriptions that Hedge cannot read"); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 93370fa3..afdd9bea 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -240,7 +240,7 @@ async function doctor(machine: ProxyMachine, paths: Layout): Promise { } async function pool(machine: ProxyMachine, paths: Layout, managementKey: string) { - const { files } = (await management(machine.fetch, managementKey, "auth-files")) as { files?: Array }; + const files = ((await management(machine.fetch, managementKey, "auth-files")) as { files?: Array } | null)?.files; if (!Array.isArray(files) || !files.every((file): file is AuthFile => typeof file?.provider === "string")) throw new Error("The proxy answers with a list of subscriptions that Hedge cannot read."); const expired = files.filter((file) => (file.provider === "claude" || file.provider === "codex") && !file.disabled && loginExpired(file)); const enabled = (agent: Agent) => files.filter((file) => file.provider === agent && !file.disabled && !expired.includes(file)).length; From 8cdbcf65324c1e538e3c3308493f1053767151bc Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:22:45 +0000 Subject: [PATCH 09/32] Let a waiting check that passes after the steps put its config in On a hub whose proxy is stopped, the route check failed in the gate, the step started the proxy, and the check then passed, but apply kept the stale Fix and left Claude Code's route out, so setup's final doctor failed. Apply now runs each check with a todo again after the steps, unless it passed or waited in the gate. Its finding takes the new result: a pass drops the old one, and any failing code is reported, not only 4. When config waits for a check that passes now, apply reconciles that config. Doctor reads the gate's result of a check that config waits for, so the config and the finding never disagree. The hub test's command dispatch now refuses any command the Hub pack does not run through the engine launcher, and its assertions read the todo flag. Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 38 +++++++++++++++++++--------- src/reconciler/apply.test.ts | 29 ++++++++++++++++++++-- src/reconciler/apply.ts | 48 +++++++++++++++++++++++++++--------- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 91171647..f76ae129 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -414,16 +414,18 @@ describe("the Own-account balancing workflow", () => { describe("apply on a hub whose proxy holds none of your subscriptions", () => { const proxyOnly = (result: Plan): Plan => ({ ...result, files: [], jobs: [], human: [], steps: result.steps.filter((step) => step.id === "cli-proxy"), checks: result.checks.filter((check) => check.workflow === "own-account-balancing"), config: result.config.filter((entry) => entry.workflow === "own-account-balancing") }); - const hedgeProxy = (machine: ProxyMachine) => async (command: string[]) => { - const words = command.join(" "); - const mode = (["install", "doctor", "accounts", "check codex", "check"] as const).find((name) => words.endsWith(`hedge cli-proxy ${name}`)); - if (!mode) return 0; - return cliProxy(mode === "check codex" ? "check" : mode, machine, mode === "check codex" ? "codex" : "claude").catch(() => 1); + const hedgeProxy = (machine: ProxyMachine, engineRoot: string | undefined, started?: () => void) => async (command: string[]) => { + const [shell, launcher, group, mode, ...rest] = command; + const agent = mode === "check" && rest.join(" ") === "codex" ? "codex" : "claude"; + const known = (["install", "doctor", "accounts", "check"] as const).find((name) => name === mode); + if (shell !== "/bin/sh" || !engineRoot || launcher !== join(engineRoot, "hedge") || group !== "cli-proxy" || !known || (rest.length && agent !== "codex")) throw new Error(`The hub pack runs no ${command.join(" ")}`); + if (known === "install") started?.(); + return cliProxy(known, machine, agent).catch(() => 1); }; const emptyHub = (ai: "claude" | "codex", credentials: Array<{ provider: string }> = []) => { const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: ai, devboxes: [] }, { credentials, models: [] }); const result = proxyOnly(plan({ ...balancing, plan: ai }, hub)); - const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine) }; + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }; return { home, result, options }; }; const login = (agent: string) => `~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -${agent}-login -no-browser`; @@ -432,12 +434,12 @@ describe("the Own-account balancing workflow", () => { const { home, result, options } = emptyHub("claude"); const fresh = await apply(result, options); expect(fresh.ok).toBe(true); - expect(fresh.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-client", "todo"], ["check:cli-proxy-accounts", "todo"]]); + expect(fresh.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); expect(fresh.findings.map((finding) => finding.say.endsWith(`: ${login("claude")}`))).toEqual([true, true]); expect(existsSync(join(home, ".local/share/hedge-cli-proxy/cli-proxy-api"))).toBe(true); const applied = await apply(result, options); expect(applied.ok).toBe(true); - expect(applied.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-client", "todo"], ["check:cli-proxy-accounts", "todo"]]); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); const doctor = await check(result, options); expect(doctor.ok).toBe(true); @@ -448,7 +450,7 @@ describe("the Own-account balancing workflow", () => { const { home, result, options } = emptyHub("codex"); const fresh = await apply(result, options); expect(fresh.ok).toBe(true); - expect(fresh.findings.map((finding) => [finding.id, finding.repair ?? "todo"])).toEqual([["check:cli-proxy-codex", "todo"], ["check:cli-proxy-codex-accounts", "todo"]]); + expect(fresh.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-codex", true], ["check:cli-proxy-codex-accounts", true]]); const applied = await apply(result, options); expect(applied.ok).toBe(true); expect(applied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-codex", true], ["check:cli-proxy-codex-accounts", true]]); @@ -459,19 +461,33 @@ describe("the Own-account balancing workflow", () => { test("with one subscription of the 2 that hub.proxyAccounts asks for, apply routes Claude Code to the proxy that serves it and still lists the To do", async () => { const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, { credentials: [{ provider: "claude" }] }); - const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine) }; const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }; await apply(result, options); const applied = await apply(result, options); expect(applied).toMatchObject({ ok: true, findings: [{ id: "check:cli-proxy-accounts", todo: true }] }); expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).env.ANTHROPIC_BASE_URL).toBe("http://127.0.0.1:8317"); }); + test("with one working subscription and a proxy that only its step starts, one apply routes Claude Code to the proxy, and doctor passes", async () => { + const proxy: Proxy = { credentials: [{ provider: "claude" }], down: true }; + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, proxy); + const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot, () => (proxy.down = false)) }; + const applied = await apply(result, options); + expect(applied.ok).toBe(true); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-accounts", true]]); + expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).env.ANTHROPIC_BASE_URL).toBe("http://127.0.0.1:8317"); + const doctor = await check(result, options); + expect(doctor.ok).toBe(true); + expect(doctor.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-accounts", true]]); + }); + test("takes Claude Code off the proxy once its pool empties, and lists the To do", async () => { const proxy: Proxy = { credentials: [{ provider: "claude" }] }; const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, proxy); - const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine) }; const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }; await apply(result, options); await apply(result, options); expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).apiKeyHelper).toBeDefined(); diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index 6caf701a..9dad945e 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -301,12 +301,12 @@ describe("a pack check that waits for the person", () => { }; const steps = [{ id: "install-pool", run: ["install-pool"], check: ["pool-installed"], pack: "base" }]; - test("that nothing waits for runs after the steps, so it reads what a step installed, and apply names only its To do", async () => { + test("that nothing waits for runs after the steps, so it reads what a step installed, and apply reports what it finds then", async () => { const { run, set } = installedPool(); const home = temp.make("home-"); expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [waiting] }); set(1); - expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [] }); + expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [{ id: "check:pool", pack: "base", say: "Hedge cannot read the pool.", repair: "apply" }] }); expect(await check(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: false, findings: [{ id: "check:pool", pack: "base", say: "Hedge cannot read the pool.", repair: "apply" }] }); set(0); expect(await apply(plan({ files: [], checks, steps }), { home, run })).toMatchObject({ ok: true, findings: [] }); @@ -320,6 +320,31 @@ describe("a pack check that waits for the person", () => { expect(settings(home)).toBeUndefined(); }); + test("that config waits for and that fails before a step starts what it reads puts its config in once the step ran, and doctor then passes", async () => { + const { run, set } = installedPool(); + set(0); + const home = temp.make("home-"); + const options = { home, adapters: [providerConfigAdapter()], run }; + const applied = await apply(plan({ files: [], checks, steps, config }), options); + expect(applied).toMatchObject({ ok: true, findings: [] }); + expect(applied.actions.map((action) => [action.target, action.operation])).toEqual([[".claude/settings.json", "create"]]); + expect(settings(home)).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" } }); + expect(await check(plan({ files: [], checks, steps, config }), options)).toMatchObject({ ok: true, findings: [] }); + }); + + test("doctor reads one result of a check that config waits for, for both the config and the finding", async () => { + const home = temp.make("home-"); + const options = { home, adapters: [providerConfigAdapter()], run: exitCodes({}) }; + await apply(plan({ files: [], checks, config }), options); + let probes = 0; + const emptying = async (command: string[]) => (command.join(" ") === "pool-check" ? (probes++ === 0 ? 0 : 4) : 0); + expect(await check(plan({ files: [], checks, config }), { ...options, run: emptying })).toMatchObject({ ok: true, findings: [] }); + probes = 0; + const filling = async (command: string[]) => (command.join(" ") === "pool-check" ? (probes++ === 0 ? 4 : 0) : 0); + const doctor = await check(plan({ files: [], checks, config }), { ...options, run: filling }); + expect(doctor.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:pool", true], ["config:.claude/settings.json", "apply"]]); + }); + test("a check without a todo that exits 4 is a Fix, and a check with one stays a Fix for any other failing code", async () => { const home = temp.make("home-"); const plain = [{ ...checks[0]!, todo: undefined }]; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 500aaea9..2bb790ce 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -83,8 +83,15 @@ function claimFindings(claimed: Action[]): Finding[] { })); } +type ApplyOptions = { home: string; dryRun?: boolean; adopt?: string[]; adoptFrom?: string[]; skip?: string[]; adapters?: Adapter[]; run?: Runner; issue?: Issuer; progress?: (line: string) => void }; +type Checked = Array<[Owned, number]>; + /** Skipped steps, named pack/id, wait for a later apply: this run neither runs nor undoes them. */ -export async function apply(plan: Plan, options: { home: string; dryRun?: boolean; adopt?: string[]; adoptFrom?: string[]; skip?: string[]; adapters?: Adapter[]; run?: Runner; issue?: Issuer; progress?: (line: string) => void }): Promise { +export async function apply(plan: Plan, options: ApplyOptions): Promise { + return (await reconcile(plan, options)).report; +} + +async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: Report; checks: Checked }> { const context = { home: options.home, dryRun: options.dryRun ?? false, adopt: options.adopt ?? [], adoptFrom: options.adoptFrom ?? [] }; const adapters = options.adapters ?? [filesAdapter]; const adoptable = new Set(adapters.flatMap((adapter) => adapter.targets(plan))); @@ -95,7 +102,7 @@ export async function apply(plan: Plan, options: { home: string; dryRun?: boolea if (plan.unanswered.length) { report.ok = false; report.findings.push(...plan.unanswered.map((setting) => ({ id: `unanswered:${setting.key}`, pack: setting.key.split(".")[0] ?? "", say: `${setting.question} Answer it with hedge set ${setting.key}=.`, repair: "human" as const }))); - return report; + return { report, checks: [] }; } const run = viaEngine(plan, options.run ?? spawnRunner(options.home, plan.machine.profileDir)); const node = plan.onHub || plan.pullsFromHub ? plan.machine.name : undefined; @@ -110,7 +117,7 @@ export async function apply(plan: Plan, options: { home: string; dryRun?: boolea if (result.failed) report.ok = false; if (!result.blocked) continue; report.ok = false; - if (!context.dryRun) return report; + if (!context.dryRun) return { report, checks: gated.checks }; } const before = await readLedger(options.home); const record = async (key: string, step?: LedgerStep) => { @@ -175,25 +182,44 @@ export async function apply(plan: Plan, options: { home: string; dryRun?: boolea if (!heldCodes.includes(await run(step.probe, { [undoVariable]: ledgerKey(step) }))) await changeLedger(options.home, "human", ledgerKey(step)); } await undo(items.filter((item) => item.cause !== "pack")); - if (!context.dryRun) await waitingChecks(plan, run, gated.checks, report.findings); - return report; + if (context.dryRun) return { report, checks: gated.checks }; + const settled = await waitingChecks(plan, run, gated.checks, report.findings); + if (!settled.length) return { report, checks: gated.checks }; + for (const adapter of adapters.filter((each) => each.name === "config")) { + const result = await adapter.apply(withSettled(plan, gated.plan, settled), context); + report.actions.push(...result.actions); + report.findings.push(...result.findings.filter((finding) => !report.findings.some((other) => other.id === finding.id && other.pack === finding.pack && other.say === finding.say))); + if (result.failed || result.blocked) report.ok = false; + } + return { report, checks: gated.checks }; } -async function waitingChecks(plan: Plan, run: Runner, gated: Array<[Owned, number]>, findings: Finding[]): Promise { +async function waitingChecks(plan: Plan, run: Runner, gated: Checked, findings: Finding[]): Promise>> { + const settled: Array> = []; for (const item of plan.checks.filter((check) => check.todo)) { const before = gated.find(([ran]) => ran === item)?.[1]; - if (before === 0 || before === waitsForPerson || (await run(item.run)) !== waitsForPerson) continue; + if (before === 0 || before === waitsForPerson) continue; + const code = await run(item.run); const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); - findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, checkFinding(item, waitsForPerson)); + findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, ...(code === 0 ? [] : [checkFinding(item, code)])); + if (code === 0 && before !== undefined) settled.push(item); } + return settled; +} + +function withSettled(plan: Plan, gated: Plan, settled: Array>): Plan { + const passes = (pack: string, id?: string) => settled.some((item) => item.pack === pack && item.id === id); + const config = plan.config.filter((entry) => gated.config.includes(entry) || passes(entry.pack, entry.after)); + const dropped = gated.dropped.filter((entry) => !entry.item.startsWith("config ") || !passes(entry.pack, entry.check)); + return { ...gated, config, dropped }; } export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { const run = viaEngine(plan, options.run ?? spawnRunner(options.home, plan.machine.profileDir)); - const report = await apply(plan, { ...options, dryRun: true }); + const { report, checks } = await reconcile(plan, { ...options, dryRun: true }); for (const item of plan.checks) { if (report.findings.some((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack)) continue; - const code = await run(item.run); + const code = checks.find(([ran]) => ran === item)?.[1] ?? (await run(item.run)); if (code !== 0) report.findings.push(checkFinding(item, code)); } const waits = waiter(plan, run); @@ -228,7 +254,7 @@ function humanFinding(step: Owned, repairStillToRun: boolean): Findin } /** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ -async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Array<[Owned, number]>; issued: Finding[] }> { +async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { const tested = new Map, boolean>(); const probe = async (step: Owned) => { if ((await run(step.probe)) === 0) return true; From 5a473af23e28762fd7d77934b8a3a9df716d1bdd Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:25:33 +0000 Subject: [PATCH 10/32] Show a box's waiting line when the hub hands it over Connect on a box exits 0 when the proxy serves none of its agents yet, and prints that the agent keeps its own sign-in until the proxy serves it. The handover dropped that line, so the person did not learn that the box waits. The hub now says each such line with the box's name. The proxy tests assert the documented exit status 4 itself, not the constant the code exports. Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy-forwards.test.ts | 12 ++++++++++++ packs/hub/lib/cli-proxy.test.ts | 25 ++++++++++++------------ packs/hub/lib/cli-proxy.ts | 6 +++--- packs/hub/lib/links.ts | 5 +++-- packs/hub/lib/run.ts | 1 + 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/packs/hub/lib/cli-proxy-forwards.test.ts b/packs/hub/lib/cli-proxy-forwards.test.ts index 2f21b542..f1d4b5f6 100644 --- a/packs/hub/lib/cli-proxy-forwards.test.ts +++ b/packs/hub/lib/cli-proxy-forwards.test.ts @@ -76,6 +76,18 @@ test("a relay port on the proxy port is refused only while the workflow is on, b expect(hubForward(off, "box-lane")).toContain("8317:127.0.0.1:8317"); }); +test("a box handed over to a proxy that serves no model yet keeps its own sign-in, and the hub says so", async () => { + const hub = fakeMachine(temp.make("hedge-hub-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); + writeHome(hub.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); + const boxHome = roleHome(temp.make("hedge-box-"), "devbox"); + const printed: string[] = []; + const box = (stdin: string): ProxyMachine => ({ home: boxHome, uid: 1000, user: "me", plan: "claude", devboxes: [], release: { version: "", url: "", sha256: "" }, run: async () => ok, fetch: async () => Response.json({ data: [], object: "list" }), now: () => 0, sleep: async () => {}, readStdin: async () => stdin, randomKey: () => clientKey, print: (line) => printed.push(line) }); + const { coder } = fakeCoder({ ssh: async (_name, command, stdin) => (command.includes("cli-proxy connect") ? { ...ok, code: await cliProxy("connect", box(stdin ?? "")), stdout: printed.join("\n") } : ok) }); + await handOver(coder, hub.machine, "box-lane"); + expect(hub.lines).toContain("box-lane: The proxy at http://127.0.0.1:8317 lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it."); + expect(existsSync(join(boxHome, ".claude/settings.json"))).toBe(false); +}); + test("a box handed over on the Codex plan sends Codex through the proxy without another apply", async () => { const hub = fakeMachine(temp.make("hedge-hub-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); writeHome(hub.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 3f2e681b..62482cc3 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -6,7 +6,6 @@ import { tempDirs } from "../../../src/testing/git.ts"; import { claudeRoute, cliProxy, keyHelper, proxyUrl, type ProxyMachine } from "./cli-proxy.ts"; import { archive, clientKey, dataDir, otherKey, proxyMachine, release, type Proxy } from "./cli-proxy-testing.ts"; import { fakeRun, modeOf, roleHome } from "./run-testing.ts"; -import { waitsForPerson } from "../../../src/pack/types.ts"; const temp = tempDirs(); afterEach(() => temp.cleanup()); @@ -227,7 +226,7 @@ test("too few subscriptions that work is a To do, which counts none whose login expect(doctor.printed).toContain(todo); expect(doctor.printed).toContain(`CLI proxy: Warning: the Claude subscription bbbbcccc cannot refresh its login, so the proxy cannot use it and the balancer cannot read its usage. Sign it in again with: ${login(home, "claude")}. Or disable it in the management panel.`); const counted = hubMachine({ home }, { credentials }); - expect(await cliProxy("accounts", counted.machine)).toBe(waitsForPerson); + expect(await cliProxy("accounts", counted.machine)).toBe(4); expect(counted.printed).toEqual([todo]); }); @@ -235,7 +234,7 @@ test("accounts waits for the person below hub.proxyAccounts, passes at it, fails const { machine, home } = hubMachine(); await cliProxy("install", machine); const none = hubMachine({ home }, { credentials: [] }); - expect(await cliProxy("accounts", none.machine)).toBe(waitsForPerson); + expect(await cliProxy("accounts", none.machine)).toBe(4); expect(none.printed.join("\n")).toContain("found 0. Until the proxy holds one, Claude Code keeps its own sign-in on each machine."); const enough = hubMachine({ home }); expect(await cliProxy("accounts", enough.machine)).toBe(0); @@ -243,7 +242,7 @@ test("accounts waits for the person below hub.proxyAccounts, passes at it, fails const one = hubMachine({ home, accounts: 1 }, { credentials: [{ provider: "claude" }] }); expect(await cliProxy("accounts", one.machine)).toBe(0); const codex = hubMachine({ home, plan: "codex" }, { credentials: [{ provider: "codex" }, { provider: "claude" }, { provider: "claude" }] }); - expect(await cliProxy("accounts", codex.machine)).toBe(waitsForPerson); + expect(await cliProxy("accounts", codex.machine)).toBe(4); expect(codex.printed.join("\n")).toContain(`found 1. Run on the hub, once for each subscription: ${login(home, "codex")}`); await expect(cliProxy("accounts", hubMachine({ home }, { down: true }).machine)).rejects.toThrow(); await expect(cliProxy("accounts", client().machine)).rejects.toThrow("hub"); @@ -292,7 +291,7 @@ test("connect on a Codex plan checks the proxy for a Codex model, sends Codex to expect(noCodex.printed).toEqual([`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`]); expect(readFileSync(keyFile(noCodex.home), "utf8")).toBe(`${clientKey}\n`); expect(existsSync(codexFile(noCodex.home))).toBe(false); - expect(await cliProxy("check", noCodex.machine, "codex")).toBe(waitsForPerson); + expect(await cliProxy("check", noCodex.machine, "codex")).toBe(4); const theirs = client({ plan: "codex" }, { models: ["gpt-6.1-sol"] }); codexRouted(theirs.home, { ...mine, model_provider: "work" }); @@ -404,7 +403,7 @@ test("connect to a proxy that holds none of your subscriptions saves the key, ke expect(readFileSync(keyFile(home), "utf8")).toBe(`${clientKey}\n`); expect(readJson(settingsFile(home))).toEqual({ model: "opus" }); expect(printed).toEqual([`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`]); - expect(await cliProxy("check", machine)).toBe(waitsForPerson); + expect(await cliProxy("check", machine)).toBe(4); const refused = client({}, { models: [], refuses: true }); await expect(cliProxy("connect", refused.machine)).rejects.toThrow("refuses this machine's key (HTTP 401). Nothing changed."); expect(existsSync(keyFile(refused.home))).toBe(false); @@ -421,8 +420,8 @@ test("connect again after the pool empties saves the key, and leaves taking the `The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`, `The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`, ]); - expect(await cliProxy("check", machine)).toBe(waitsForPerson); - expect(await cliProxy("check", machine, "codex")).toBe(waitsForPerson); + expect(await cliProxy("check", machine)).toBe(4); + expect(await cliProxy("check", machine, "codex")).toBe(4); }); test("connect refuses a model list it cannot read, and saves no key", async () => { @@ -507,7 +506,7 @@ test("the check fails without a saved key, waits for the person while the proxy const refused = client({}, { models: ["gpt-6.1-sol"] }); connected(refused.home); - expect(await cliProxy("check", refused.machine)).toBe(waitsForPerson); + expect(await cliProxy("check", refused.machine)).toBe(4); expect(refused.printed.join("\n")).toContain("lists no Claude model"); const fresh = client({}, { down: true }); @@ -685,7 +684,7 @@ test("the Codex check judges the proxy before Hedge routes Codex, so apply can w expect(existsSync(codexFile(served.home))).toBe(false); const unserved = client(); connected(unserved.home); - expect(await cliProxy("check", unserved.machine, "codex")).toBe(waitsForPerson); + expect(await cliProxy("check", unserved.machine, "codex")).toBe(4); expect(existsSync(codexFile(unserved.home))).toBe(false); }); @@ -704,7 +703,7 @@ test("a proxy that lists no Codex model holds back only the Codex check, which w connected(home); codexRouted(home); expect(await cliProxy("check", machine)).toBe(0); - expect(await cliProxy("check", machine, "codex")).toBe(waitsForPerson); + expect(await cliProxy("check", machine, "codex")).toBe(4); expect(printed.join("\n")).toContain(`The proxy at ${proxyUrl} lists no Codex model.`); }); @@ -747,7 +746,7 @@ test("a linked or unreadable Codex config is left alone and never breaks the Cla writeFileSync(real, stringify({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": hedgeProxy } })); symlinkSync(real, codexFile(linked.home)); expect(await cliProxy("check", linked.machine)).toBe(0); - expect(await cliProxy("check", linked.machine, "codex")).toBe(waitsForPerson); + expect(await cliProxy("check", linked.machine, "codex")).toBe(4); expect(await cliProxy("disconnect", linked.machine)).toBe(0); expect(parse(readFileSync(real, "utf8"))).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": hedgeProxy } }); @@ -756,7 +755,7 @@ test("a linked or unreadable Codex config is left alone and never breaks the Cla mkdirSync(join(broken.home, ".codex"), { recursive: true }); writeFileSync(codexFile(broken.home), "model = ["); expect(await cliProxy("check", broken.machine)).toBe(0); - expect(await cliProxy("check", broken.machine, "codex")).toBe(waitsForPerson); + expect(await cliProxy("check", broken.machine, "codex")).toBe(4); expect(readJson(settingsFile(broken.home)).env.ANTHROPIC_BASE_URL).toBe(proxyUrl); expect(readFileSync(codexFile(broken.home), "utf8")).toBe("model = ["); }); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index c70e8314..bf2a2625 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -13,7 +13,7 @@ import type { Run } from "../../mcp-gateway/lib/hub.ts"; import { isKeptForBoxes } from "../../../src/hub/names.ts"; import { waitsForPerson } from "../../../src/pack/types.ts"; import { refreshCodexCatalog } from "./codex-catalog.ts"; -import { must, proxyPort } from "./run.ts"; +import { must, proxyPort, untilServed } from "./run.ts"; export { proxyPort, proxyUrl }; export type ProxyMode = "plan" | "install" | "doctor" | "accounts" | "catalog" | "connect" | "disconnect" | "check" | "remove" | "left"; @@ -458,14 +458,14 @@ async function connect(machine: ProxyMachine): Promise { await mkdir(dirname(keyPath(machine.home)), { recursive: true, mode: 0o700 }); await writePrivateFile(keyPath(machine.home), `${key}\n`); const waiting = unserved(models, "claude"); - if (agent === "claude" && waiting) machine.print(`${waiting} Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`); + if (agent === "claude" && waiting) machine.print(`${waiting} Run hedge apply: it keeps Claude Code ${untilServed}`); else if (agent === "claude") { await sendClaude(machine.home, true); machine.print(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); } if (codex === undefined) return; const missing = typeof codex === "string" ? undefined : unserved(models, "codex"); - if (missing) return machine.print(`${missing} Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`); + if (missing) return machine.print(`${missing} Run hedge apply: it keeps Codex ${untilServed}`); const failed = typeof codex === "string" ? codex : await sendCodex(machine.home, agent === "claude"); if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); diff --git a/packs/hub/lib/links.ts b/packs/hub/lib/links.ts index 0f30c2c8..46d6512f 100644 --- a/packs/hub/lib/links.ts +++ b/packs/hub/lib/links.ts @@ -3,7 +3,7 @@ import { mkdir } from "node:fs/promises"; import { dirname, join } from "node:path"; import { tryLockBox } from "./box-lock.ts"; import { slugFor, type Coder, type Result } from "./coder.ts"; -import { proxyPort, relayPortOf } from "./run.ts"; +import { proxyPort, relayPortOf, untilServed } from "./run.ts"; import { quote, remoteHedge, type Machine, type PeerLinkInput, type PeerMachine } from "./machine.ts"; import { GatewayClientError, gatewayPort, localGatewayUrl, readGatewayClient, writePrivateFile, type GatewaySettings } from "../../mcp-gateway/lib/client.ts"; import { boxPrefix, isBoxName } from "../../../src/hub/names.ts"; @@ -69,7 +69,8 @@ async function handOverProxy(coder: Pick, here: Machine, box: stri const key = join(here.home, proxyKeyPath); if (!existsSync(key)) return here.say(`skip ~/${proxyKeyPath}: this machine has none`); const connected = await coder.ssh(box, `umask 077 && ${remoteHedge("cli-proxy connect")}`, readFileSync(key, "utf8")); - if (connected.code !== 0) here.say(`${box} keeps its own sign-in: ${(connected.stderr || connected.stdout).trim()}`); + if (connected.code !== 0) return here.say(`${box} keeps its own sign-in: ${(connected.stderr || connected.stdout).trim()}`); + for (const line of connected.stdout.split("\n").filter((line) => line.endsWith(untilServed))) here.say(`${box}: ${line}`); } export async function handOver(coder: Pick, here: Machine, box: string): Promise { diff --git a/packs/hub/lib/run.ts b/packs/hub/lib/run.ts index 3eeee884..72a89172 100644 --- a/packs/hub/lib/run.ts +++ b/packs/hub/lib/run.ts @@ -19,6 +19,7 @@ export async function must(run: Run, command: string[], input?: string): Promise export const hostPattern = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/; export const proxyPort = 8317; +export const untilServed = "on its own sign-in until the proxy serves it."; export function relayPortOf(port: number, proxy = false): number { if (!Number.isInteger(port) || port < 1024 || port > 65535 || port === gatewayPort) throw new Error(`hub.relayPort must be a port from 1024 to 65535 other than ${gatewayPort}, not ${port}.`); From 82302f9a012afe8a6b1297c00b37e6a343103f99 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:25:38 +0000 Subject: [PATCH 11/32] Name the missing model in the route To do, and list doctor's waiting line The route check's To do said the proxy holds none of the agent's subscriptions, which is wrong when one works but the proxy lists no model for it. It now says which agent's model the proxy does not list, and points to hedge cli-proxy doctor on the hub, which tells the two cases apart. The hedge-hub skill says the same. Step 8 of SETUP.md lists doctor's closing line for To do lines that wait for the person, and says they do not stop setup. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 11 +++++++++-- SETUP.md | 4 ++++ packs/hub/pack.json | 4 ++-- packs/hub/skills/hedge-hub/SKILL.md | 11 +++++++---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 531afc53..1e70941a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,13 +17,20 @@ `hedge cli-proxy connect` saves the key on a node in that case, so the first `hedge apply` after the proxy serves an agent sends it there. Connect changes no agent settings in that case: `hedge apply` takes - Hedge's route off an agent that the proxy no longer serves. On the both plan, `hedge doctor` fails while a Codex + Hedge's route off an agent that the proxy no longer serves. When the hub + hands a box over while the proxy serves none of its agents, the hub shows + the box's line that it keeps its own sign-in. On the both plan, `hedge doctor` fails while a Codex subscription works but the proxy serves no Codex model. A proxy that is down, refuses the key, answers without a model list, or runs a plugin Hedge did not install still fails `hedge doctor`. - A pack check can name a `todo`: when its command exits 4, Hedge prints it as a `To do:` line, keeps the config that waits for the check out, and - fails neither apply nor doctor. Apply runs such a check after the steps. + fails neither apply nor doctor. Apply runs such a check again after the + steps, unless it passed or waited before them, and reports what it finds + then. When it passes then, apply drops its old `Fix:` and writes the + config that waits for it, so a hub whose proxy was stopped routes Claude + Code in one apply. `hedge doctor` reads one result of each check for both + the config and its line. ### Hedge is cloud only diff --git a/SETUP.md b/SETUP.md index 03b06786..f06a9e4d 100644 --- a/SETUP.md +++ b/SETUP.md @@ -539,6 +539,10 @@ the person: and give the file to the command over SSH, as its pack's setup note shows. - `Note:` information only. - `Everything matches your profile.` The box is set up. +- `Everything Hedge sets matches your profile. The To do lines above wait for + you.` The box is set up. Each `To do:` line above it waits for the person, + such as adding a subscription to the hub's own-account proxy, and does not + stop setup. Tell the person what each one asks, and go on. ### When Hedge kept something of theirs diff --git a/packs/hub/pack.json b/packs/hub/pack.json index 669db8f6..499f5871 100644 --- a/packs/hub/pack.json +++ b/packs/hub/pack.json @@ -705,7 +705,7 @@ "check" ], "say": "Claude Code cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check here to see why.", - "todo": "Claude Code keeps its own sign-in, because the own-account proxy on the hub serves no Claude model yet: it holds none of your Claude subscriptions that work. The first hedge apply after you add one sends Claude Code to the proxy. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser", + "todo": "Claude Code keeps its own sign-in, because the own-account proxy on the hub lists no Claude model yet. Most often it holds none of your Claude subscriptions that work. Run hedge cli-proxy doctor on the hub: it counts them, and says so when one works but the proxy still lists no Claude model. The first hedge apply after the proxy lists a Claude model sends Claude Code to the proxy. Add a subscription on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser", "roles": [ "hub", "devbox" @@ -725,7 +725,7 @@ "codex" ], "say": "Codex cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check codex here to see why.", - "todo": "Codex keeps its own sign-in, because the own-account proxy on the hub serves no Codex model yet: it holds none of your Codex subscriptions that work. The first hedge apply after you add one sends Codex to the proxy. Add one on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser", + "todo": "Codex keeps its own sign-in, because the own-account proxy on the hub lists no Codex model yet. Most often it holds none of your Codex subscriptions that work. Run hedge cli-proxy doctor on the hub: it counts them, and says so when one works but the proxy still lists no Codex model. The first hedge apply after the proxy lists a Codex model sends Codex to the proxy. Add a subscription on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser", "roles": [ "hub", "devbox" diff --git a/packs/hub/skills/hedge-hub/SKILL.md b/packs/hub/skills/hedge-hub/SKILL.md index b402074a..077559cb 100644 --- a/packs/hub/skills/hedge-hub/SKILL.md +++ b/packs/hub/skills/hedge-hub/SKILL.md @@ -160,10 +160,13 @@ the person's own subscriptions, never for someone else's. agent's settings out and removes Hedge's own values, so the agent uses its own sign-in, and the next apply changes nothing. `hedge apply` and `hedge doctor` name the failed check. When the proxy answers the key but - serves no model for the agent, because it holds none of that agent's - subscriptions that work, the check exits 4: the agent stays on its own - sign-in, and the line is a `To do:` with the login command, not a - failure. `hedge cli-proxy connect` then saves the key and changes no + lists no model for the agent, the check exits 4: the agent stays on its + own sign-in, and the line is a `To do:` with the login command, not a + failure. The `To do:` names the agent whose model is missing. Most often + the proxy holds none of that agent's subscriptions that work. Run + `hedge cli-proxy doctor` on the hub to tell the two cases apart: it counts + the subscriptions that work, and fails when one works but the proxy lists + no model for it. `hedge cli-proxy connect` then saves the key and changes no agent settings; `hedge apply` keeps the agent on its own sign-in until the proxy serves it, and then sends it there. Connect still changes nothing when the proxy does not answer or refuses the key. The first apply after the proxy From b632f1ed167d7f5360eee3bf668a1fc3e4c19b21 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:10:13 +0000 Subject: [PATCH 12/32] Keep a failed pack's waiting config out, and replace the first config findings When a pack's step failed in this apply, the second config pass no longer writes config that waits for that pack's checks. The second pass now replaces the findings that the config adapter gave in the first pass, so a plugin that waited for Claude Code leaves no stale Fix line. The proxy test fake now answers only once the service starts. Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 21 +++++++++++++++------ packs/hub/lib/cli-proxy-testing.ts | 5 +++-- src/reconciler/apply.test.ts | 25 ++++++++++++++++++++++++- src/reconciler/apply.ts | 11 +++++++---- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index f76ae129..29663e8b 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -6,7 +6,7 @@ import { providerConfigAdapter } from "../../src/adapters/provider-config/adapte import { apply, check } from "../../src/reconciler/apply.ts"; import { boxCommand } from "./lib/box-command.ts"; import { cliProxy, codexProviderTable, type ProxyMachine } from "./lib/cli-proxy.ts"; -import { proxyMachine, type Proxy } from "./lib/cli-proxy-testing.ts"; +import { dataDir, proxyMachine, type Proxy } from "./lib/cli-proxy-testing.ts"; import { Coder } from "./lib/coder.ts"; import { fakeMachine } from "./lib/testing.ts"; import type { AvailableSource, MachineFile, MachineRole, Plan, Profile } from "../../src/pack/types.ts"; @@ -414,12 +414,11 @@ describe("the Own-account balancing workflow", () => { describe("apply on a hub whose proxy holds none of your subscriptions", () => { const proxyOnly = (result: Plan): Plan => ({ ...result, files: [], jobs: [], human: [], steps: result.steps.filter((step) => step.id === "cli-proxy"), checks: result.checks.filter((check) => check.workflow === "own-account-balancing"), config: result.config.filter((entry) => entry.workflow === "own-account-balancing") }); - const hedgeProxy = (machine: ProxyMachine, engineRoot: string | undefined, started?: () => void) => async (command: string[]) => { + const hedgeProxy = (machine: ProxyMachine, engineRoot: string | undefined) => async (command: string[]) => { const [shell, launcher, group, mode, ...rest] = command; const agent = mode === "check" && rest.join(" ") === "codex" ? "codex" : "claude"; const known = (["install", "doctor", "accounts", "check"] as const).find((name) => name === mode); if (shell !== "/bin/sh" || !engineRoot || launcher !== join(engineRoot, "hedge") || group !== "cli-proxy" || !known || (rest.length && agent !== "codex")) throw new Error(`The hub pack runs no ${command.join(" ")}`); - if (known === "install") started?.(); return cliProxy(known, machine, agent).catch(() => 1); }; const emptyHub = (ai: "claude" | "codex", credentials: Array<{ provider: string }> = []) => { @@ -470,10 +469,9 @@ describe("the Own-account balancing workflow", () => { }); test("with one working subscription and a proxy that only its step starts, one apply routes Claude Code to the proxy, and doctor passes", async () => { - const proxy: Proxy = { credentials: [{ provider: "claude" }], down: true }; - const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, proxy); + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, { credentials: [{ provider: "claude" }], stopped: true }); const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); - const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot, () => (proxy.down = false)) }; + const options = { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }; const applied = await apply(result, options); expect(applied.ok).toBe(true); expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-accounts", true]]); @@ -483,6 +481,17 @@ describe("the Own-account balancing workflow", () => { expect(doctor.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-accounts", true]]); }); + test("with a stopped proxy that its step starts but whose doctor finds a plugin Hedge did not install, apply fails the step and keeps Claude Code on its own sign-in", async () => { + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, { credentials: [{ provider: "claude" }], stopped: true }); + mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); + writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); + const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); + const applied = await apply(result, { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }); + expect(applied.ok).toBe(false); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", "apply"], ["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); + expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); + }); + test("takes Claude Code off the proxy once its pool empties, and lists the To do", async () => { const proxy: Proxy = { credentials: [{ provider: "claude" }] }; const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, proxy); diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts index a2e4307d..0520d97b 100644 --- a/packs/hub/lib/cli-proxy-testing.ts +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -9,7 +9,7 @@ export const archive = "fake release archive"; export const release = { version: "9.9.9", url: "https://example.com/proxy.tar.gz", sha256: new Bun.CryptoHasher("sha256").update(archive).digest("hex") }; export const dataDir = (home: string) => join(home, ".local/share/hedge-cli-proxy"); -export type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean; refuses?: boolean; modelList?: string; silentModels?: boolean; authFiles?: unknown }; +export type Proxy = { catalog?: unknown; registry?: unknown; strategy?: string; credentials?: Array<{ name?: string; auth_index?: string; provider: string; disabled?: boolean; status?: string; unavailable?: boolean; status_message?: string }>; models?: string[]; down?: boolean; stopped?: boolean; refuses?: boolean; modelList?: string; silentModels?: boolean; authFiles?: unknown }; export function fakeProxy(proxy: Proxy = {}) { const requests: Array<{ path: string; method: string; auth: string }> = []; @@ -19,7 +19,7 @@ export function fakeProxy(proxy: Proxy = {}) { requests.push({ path: url.pathname, method: init?.method ?? "GET", auth }); if (proxy.catalog !== undefined && url.pathname.endsWith("/codex_client_models.json")) return Response.json(proxy.catalog); if (proxy.registry !== undefined && url.pathname.endsWith("/models.json")) return Response.json(proxy.registry); - if (proxy.down || url.origin !== proxyUrl) throw new TypeError("connection refused"); + if (proxy.down || proxy.stopped || url.origin !== proxyUrl) throw new TypeError("connection refused"); if (url.pathname === "/v1/models" && proxy.silentModels) throw new TypeError("connection refused"); if (url.pathname === "/v1/models" && proxy.modelList !== undefined) return new Response(proxy.modelList, { status: 200 }); if (url.pathname === "/v1/models") return auth === `Bearer ${clientKey}` && !proxy.refuses ? Response.json({ data: (proxy.models ?? ["claude-sonnet-5-5"]).map((id) => ({ id })) }) : new Response("no", { status: 401 }); @@ -39,6 +39,7 @@ export function proxyMachine(make: (prefix: string) => string, fields: Partial

{ expect(await check(plan({ files: [], checks, steps, config }), options)).toMatchObject({ ok: true, findings: [] }); }); + test("that config waits for and that passes once a step ran replaces the first config findings, so a plugin that waited for the step's Claude Code leaves no Fix", async () => { + const pool = installedPool(); + pool.set(0); + let installed = false; + const run = async (command: string[]) => { + if (command.join(" ") === "install-pool") installed = true; + return pool.run(command); + }; + const plugins: string[] = []; + const claude: CommandRunner = async (command) => { + if (!installed) return { code: 127, stdout: "" }; + if (command.join(" ") === "claude plugin list --json") return { code: 0, stdout: JSON.stringify(plugins.map((id) => ({ id, scope: "user" }))) }; + if (command[2] === "install") plugins.push(command[3]!); + return { code: 0, stdout: "[]" }; + }; + const plugin = { provider: "claude" as const, key: "enabledPlugins.context7@claude-plugins-official", value: true, pack: "base" }; + const home = temp.make("home-"); + const applied = await apply(plan({ files: [], checks, steps, config: [...config, plugin] }), { home, adapters: [providerConfigAdapter({ run: claude })], run }); + expect(applied).toMatchObject({ ok: true, findings: [] }); + expect(plugins).toEqual(["context7@claude-plugins-official"]); + expect(settings(home).env).toEqual({ ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }); + }); + test("doctor reads one result of a check that config waits for, for both the config and the finding", async () => { const home = temp.make("home-"); const options = { home, adapters: [providerConfigAdapter()], run: exitCodes({}) }; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 2bb790ce..558d7b0e 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -110,8 +110,10 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R const gated = await gate(plan, run, !context.dryRun, issue); report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun)), ...gated.checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); if (!context.dryRun && gated.failed.some(repairedByHedge)) report.ok = false; + const firstFindings = new Map(); for (const adapter of adapters) { const result = await adapter.apply(gated.plan, context); + firstFindings.set(adapter, result.findings); report.actions.push(...result.actions); report.findings.push(...result.findings); if (result.failed) report.ok = false; @@ -183,22 +185,23 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R } await undo(items.filter((item) => item.cause !== "pack")); if (context.dryRun) return { report, checks: gated.checks }; - const settled = await waitingChecks(plan, run, gated.checks, report.findings); + const settled = await waitingChecks(plan, run, gated.checks, report.findings, failed); if (!settled.length) return { report, checks: gated.checks }; for (const adapter of adapters.filter((each) => each.name === "config")) { const result = await adapter.apply(withSettled(plan, gated.plan, settled), context); + const stale = firstFindings.get(adapter) ?? []; + report.findings.splice(0, report.findings.length, ...report.findings.filter((finding) => !stale.includes(finding)), ...result.findings); report.actions.push(...result.actions); - report.findings.push(...result.findings.filter((finding) => !report.findings.some((other) => other.id === finding.id && other.pack === finding.pack && other.say === finding.say))); if (result.failed || result.blocked) report.ok = false; } return { report, checks: gated.checks }; } -async function waitingChecks(plan: Plan, run: Runner, gated: Checked, findings: Finding[]): Promise>> { +async function waitingChecks(plan: Plan, run: Runner, gated: Checked, findings: Finding[], failed: Set): Promise>> { const settled: Array> = []; for (const item of plan.checks.filter((check) => check.todo)) { const before = gated.find(([ran]) => ran === item)?.[1]; - if (before === 0 || before === waitsForPerson) continue; + if (before === 0 || before === waitsForPerson || (before !== undefined && failed.has(item.pack))) continue; const code = await run(item.run); const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, ...(code === 0 ? [] : [checkFinding(item, code)])); From bac84008c96dca6559e041cdf30030436305b3a4 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:13:07 +0000 Subject: [PATCH 13/32] Print the hub's own words for a box's exact waiting line The handover matched any box line that ended with a fixed suffix and echoed it, so a box could put any text or control characters in the hub's output. The hub now matches the two exact lines that connect prints and prints a message of its own, which tells the person to run hedge apply on the box. The CHANGELOG says so, and names the second config pass rules. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 19 ++++++++++++------- packs/hub/lib/cli-proxy-forwards.test.ts | 18 ++++++++++++++++-- packs/hub/lib/cli-proxy.ts | 13 ++++++++----- packs/hub/lib/links.ts | 7 +++++-- packs/hub/lib/run.ts | 1 - 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e70941a..e6e58139 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,19 +18,24 @@ first `hedge apply` after the proxy serves an agent sends it there. Connect changes no agent settings in that case: `hedge apply` takes Hedge's route off an agent that the proxy no longer serves. When the hub - hands a box over while the proxy serves none of its agents, the hub shows - the box's line that it keeps its own sign-in. On the both plan, `hedge doctor` fails while a Codex - subscription works but the proxy serves no Codex model. A - proxy that is down, refuses the key, answers without a model list, or - runs a plugin Hedge did not install still fails `hedge doctor`. + hands a box over while the proxy serves none of its agents, the hub tells + you to run `hedge apply` on the box, so that the box uses its own sign-in + until the proxy serves it. On the both plan, `hedge doctor` fails while a + Codex subscription works but the proxy serves no Codex model. A proxy + that is down, refuses the key, answers without a model list, or runs a + plugin Hedge did not install still fails `hedge doctor`. - A pack check can name a `todo`: when its command exits 4, Hedge prints it as a `To do:` line, keeps the config that waits for the check out, and fails neither apply nor doctor. Apply runs such a check again after the steps, unless it passed or waited before them, and reports what it finds then. When it passes then, apply drops its old `Fix:` and writes the config that waits for it, so a hub whose proxy was stopped routes Claude - Code in one apply. `hedge doctor` reads one result of each check for both - the config and its line. + Code in one apply. That config pass replaces the lines of the first one, + so a Claude plugin that waited for Claude Code leaves no old `Fix:`. When + a step of the check's pack failed in that apply, the config stays out, so + a hub whose proxy step fails on a plugin Hedge did not install keeps + Claude Code on its own sign-in. `hedge doctor` reads one result of each + check for both the config and its line. ### Hedge is cloud only diff --git a/packs/hub/lib/cli-proxy-forwards.test.ts b/packs/hub/lib/cli-proxy-forwards.test.ts index f1d4b5f6..9e74d42d 100644 --- a/packs/hub/lib/cli-proxy-forwards.test.ts +++ b/packs/hub/lib/cli-proxy-forwards.test.ts @@ -3,7 +3,7 @@ import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { parse } from "smol-toml"; import { tempDirs } from "../../../src/testing/git.ts"; -import { cliProxy, type ProxyMachine } from "./cli-proxy.ts"; +import { cliProxy, waitingLines, type ProxyMachine } from "./cli-proxy.ts"; import { handOver, hubForward } from "./links.ts"; import { roleHome } from "./run-testing.ts"; import { proxyOn, readBoxSettings } from "./machine.ts"; @@ -84,10 +84,24 @@ test("a box handed over to a proxy that serves no model yet keeps its own sign-i const box = (stdin: string): ProxyMachine => ({ home: boxHome, uid: 1000, user: "me", plan: "claude", devboxes: [], release: { version: "", url: "", sha256: "" }, run: async () => ok, fetch: async () => Response.json({ data: [], object: "list" }), now: () => 0, sleep: async () => {}, readStdin: async () => stdin, randomKey: () => clientKey, print: (line) => printed.push(line) }); const { coder } = fakeCoder({ ssh: async (_name, command, stdin) => (command.includes("cli-proxy connect") ? { ...ok, code: await cliProxy("connect", box(stdin ?? "")), stdout: printed.join("\n") } : ok) }); await handOver(coder, hub.machine, "box-lane"); - expect(hub.lines).toContain("box-lane: The proxy at http://127.0.0.1:8317 lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it."); + expect(hub.lines).toContain("box-lane: the proxy lists no Claude model yet. Run hedge apply on box-lane, so that Claude Code there uses its own sign-in until the proxy serves it."); expect(existsSync(join(boxHome, ".claude/settings.json"))).toBe(false); }); +test("a handover prints only the hub's own words for a box line that is exactly a known waiting line", async () => { + const hub = fakeMachine(temp.make("hedge-hub-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); + writeHome(hub.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); + const stdout = [`token=secret ${waitingLines.claude}`, `${waitingLines.codex} token=secret`, `\u001b[2J${waitingLines.claude}`, `${waitingLines.codex}\u0007`].join("\n"); + const { coder } = fakeCoder({ ssh: async (_name, command) => (command.includes("cli-proxy connect") ? { ...ok, stdout } : ok) }); + await handOver(coder, hub.machine, "box-lane"); + expect(hub.lines.filter((line) => line.startsWith("box-lane:"))).toEqual([]); + expect(hub.lines.join("\n")).not.toContain("secret"); + expect(hub.lines.join("\n")).not.toContain("\u001b"); + const exact = fakeCoder({ ssh: async (_name, command) => (command.includes("cli-proxy connect") ? { ...ok, stdout: `${waitingLines.codex}\n` } : ok) }); + await handOver(exact.coder, hub.machine, "box-lane"); + expect(hub.lines.filter((line) => line.startsWith("box-lane:"))).toEqual(["box-lane: the proxy lists no Codex model yet. Run hedge apply on box-lane, so that Codex there uses its own sign-in until the proxy serves it."]); +}); + test("a box handed over on the Codex plan sends Codex through the proxy without another apply", async () => { const hub = fakeMachine(temp.make("hedge-hub-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); writeHome(hub.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index bf2a2625..88dddb3a 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -13,7 +13,7 @@ import type { Run } from "../../mcp-gateway/lib/hub.ts"; import { isKeptForBoxes } from "../../../src/hub/names.ts"; import { waitsForPerson } from "../../../src/pack/types.ts"; import { refreshCodexCatalog } from "./codex-catalog.ts"; -import { must, proxyPort, untilServed } from "./run.ts"; +import { must, proxyPort } from "./run.ts"; export { proxyPort, proxyUrl }; export type ProxyMode = "plan" | "install" | "doctor" | "accounts" | "catalog" | "connect" | "disconnect" | "check" | "remove" | "left"; @@ -297,6 +297,11 @@ const missingModel = { codex: `The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy.`, }; +export const waitingLines = { + claude: `${missingModel.claude} Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`, + codex: `${missingModel.codex} Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`, +}; + function unserved(models: string[] | string, provider: "claude" | "codex"): string | undefined { if (typeof models === "string") return models; return models.some(servedModel[provider]) ? undefined : missingModel[provider]; @@ -457,15 +462,13 @@ async function connect(machine: ProxyMachine): Promise { await refuseLinks(machine.home, keyPath(machine.home), `${what} path`); await mkdir(dirname(keyPath(machine.home)), { recursive: true, mode: 0o700 }); await writePrivateFile(keyPath(machine.home), `${key}\n`); - const waiting = unserved(models, "claude"); - if (agent === "claude" && waiting) machine.print(`${waiting} Run hedge apply: it keeps Claude Code ${untilServed}`); + if (agent === "claude" && unserved(models, "claude")) machine.print(waitingLines.claude); else if (agent === "claude") { await sendClaude(machine.home, true); machine.print(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); } if (codex === undefined) return; - const missing = typeof codex === "string" ? undefined : unserved(models, "codex"); - if (missing) return machine.print(`${missing} Run hedge apply: it keeps Codex ${untilServed}`); + if (typeof codex !== "string" && unserved(models, "codex")) return machine.print(waitingLines.codex); const failed = typeof codex === "string" ? codex : await sendCodex(machine.home, agent === "claude"); if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); diff --git a/packs/hub/lib/links.ts b/packs/hub/lib/links.ts index 46d6512f..c39449ff 100644 --- a/packs/hub/lib/links.ts +++ b/packs/hub/lib/links.ts @@ -3,7 +3,8 @@ import { mkdir } from "node:fs/promises"; import { dirname, join } from "node:path"; import { tryLockBox } from "./box-lock.ts"; import { slugFor, type Coder, type Result } from "./coder.ts"; -import { proxyPort, relayPortOf, untilServed } from "./run.ts"; +import { proxyPort, relayPortOf } from "./run.ts"; +import { waitingLines } from "./cli-proxy.ts"; import { quote, remoteHedge, type Machine, type PeerLinkInput, type PeerMachine } from "./machine.ts"; import { GatewayClientError, gatewayPort, localGatewayUrl, readGatewayClient, writePrivateFile, type GatewaySettings } from "../../mcp-gateway/lib/client.ts"; import { boxPrefix, isBoxName } from "../../../src/hub/names.ts"; @@ -70,7 +71,9 @@ async function handOverProxy(coder: Pick, here: Machine, box: stri if (!existsSync(key)) return here.say(`skip ~/${proxyKeyPath}: this machine has none`); const connected = await coder.ssh(box, `umask 077 && ${remoteHedge("cli-proxy connect")}`, readFileSync(key, "utf8")); if (connected.code !== 0) return here.say(`${box} keeps its own sign-in: ${(connected.stderr || connected.stdout).trim()}`); - for (const line of connected.stdout.split("\n").filter((line) => line.endsWith(untilServed))) here.say(`${box}: ${line}`); + const lines = connected.stdout.split("\n"); + if (lines.includes(waitingLines.claude)) here.say(`${box}: the proxy lists no Claude model yet. Run hedge apply on ${box}, so that Claude Code there uses its own sign-in until the proxy serves it.`); + if (lines.includes(waitingLines.codex)) here.say(`${box}: the proxy lists no Codex model yet. Run hedge apply on ${box}, so that Codex there uses its own sign-in until the proxy serves it.`); } export async function handOver(coder: Pick, here: Machine, box: string): Promise { diff --git a/packs/hub/lib/run.ts b/packs/hub/lib/run.ts index 72a89172..3eeee884 100644 --- a/packs/hub/lib/run.ts +++ b/packs/hub/lib/run.ts @@ -19,7 +19,6 @@ export async function must(run: Run, command: string[], input?: string): Promise export const hostPattern = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/; export const proxyPort = 8317; -export const untilServed = "on its own sign-in until the proxy serves it."; export function relayPortOf(port: number, proxy = false): number { if (!Number.isInteger(port) || port < 1024 || port > 65535 || port === gatewayPort) throw new Error(`hub.relayPort must be a port from 1024 to 65535 other than ${gatewayPort}, not ${port}.`); From e41fa934af3a086c80cdce9c51f159fe7165163e Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:31:39 +0000 Subject: [PATCH 14/32] Print the hub's own words when connect fails on a box When hedge cli-proxy connect exited non-zero, the hub copied the box's stderr or stdout into its own output, which could carry the proxy key or control characters. The hub now names the box and the exit code, and points to hedge cli-proxy check on the box. Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy-forwards.test.ts | 15 ++++++++++++++- packs/hub/lib/links.ts | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packs/hub/lib/cli-proxy-forwards.test.ts b/packs/hub/lib/cli-proxy-forwards.test.ts index 9e74d42d..a2aa71bc 100644 --- a/packs/hub/lib/cli-proxy-forwards.test.ts +++ b/packs/hub/lib/cli-proxy-forwards.test.ts @@ -60,13 +60,26 @@ test("a box that cannot reach the proxy yet keeps its own sign-in, and the hando writeHome(on.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); const { coder } = fakeCoder({ ssh: async (_box, command) => ({ ...ok, code: command.includes("cli-proxy connect") ? 1 : 0, stderr: "The proxy does not answer." }) }); await handOver(coder, on.machine, "box-lane"); - expect(on.lines).toContain("box-lane keeps its own sign-in: The proxy does not answer."); + expect(on.lines).toContain("box-lane keeps its own sign-in: hedge cli-proxy connect exited 1 there. Run hedge cli-proxy check on box-lane to see why."); const none = fakeMachine(temp.make("hedge-box-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); await handOver(fakeCoder().coder, none.machine, "box-lane"); expect(none.lines).toContain("skip ~/.config/hedge/cli-proxy.key: this machine has none"); }); +test("a failed handover prints the hub's own words with the exit code, and neither the box's output nor a key in it", async () => { + const on = fakeMachine(temp.make("hedge-box-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); + writeHome(on.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); + for (const output of [{ stderr: `key ${clientKey} refused\u001b[2J` }, { stdout: `key ${clientKey} refused\u001b[2J` }]) { + const { coder } = fakeCoder({ ssh: async (_box, command) => ({ ...ok, ...output, code: command.includes("cli-proxy connect") ? 3 : 0 }) }); + await handOver(coder, on.machine, "box-lane"); + } + expect(on.lines.filter((line) => line.startsWith("box-lane keeps"))).toEqual(Array(2).fill("box-lane keeps its own sign-in: hedge cli-proxy connect exited 3 there. Run hedge cli-proxy check on box-lane to see why.")); + expect(on.lines.join("\n")).not.toContain(clientKey); + expect(on.lines.join("\n")).not.toContain("\u001b"); + expect(on.lines.join("\n")).not.toContain("refused"); +}); + test("a relay port on the proxy port is refused only while the workflow is on, before any tunnel or box forward is built", () => { expect(() => tunnelUnit("devbox-1", 8317, true)).toThrow("while Own-account balancing is on, hub.relayPort cannot be 8317"); const clash = fakeMachine(temp.make("hedge-box-"), { settings: { ...balancing, relayPort: 8317 } }).machine; diff --git a/packs/hub/lib/links.ts b/packs/hub/lib/links.ts index c39449ff..922fe321 100644 --- a/packs/hub/lib/links.ts +++ b/packs/hub/lib/links.ts @@ -70,7 +70,7 @@ async function handOverProxy(coder: Pick, here: Machine, box: stri const key = join(here.home, proxyKeyPath); if (!existsSync(key)) return here.say(`skip ~/${proxyKeyPath}: this machine has none`); const connected = await coder.ssh(box, `umask 077 && ${remoteHedge("cli-proxy connect")}`, readFileSync(key, "utf8")); - if (connected.code !== 0) return here.say(`${box} keeps its own sign-in: ${(connected.stderr || connected.stdout).trim()}`); + if (connected.code !== 0) return here.say(`${box} keeps its own sign-in: hedge cli-proxy connect exited ${connected.code} there. Run hedge cli-proxy check on ${box} to see why.`); const lines = connected.stdout.split("\n"); if (lines.includes(waitingLines.claude)) here.say(`${box}: the proxy lists no Claude model yet. Run hedge apply on ${box}, so that Claude Code there uses its own sign-in until the proxy serves it.`); if (lines.includes(waitingLines.codex)) here.say(`${box}: the proxy lists no Codex model yet. Run hedge apply on ${box}, so that Codex there uses its own sign-in until the proxy serves it.`); From ada157320cbe55ea9b653ec8ae5325aa25674262 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:31:45 +0000 Subject: [PATCH 15/32] Take back waiting config when its pack's step fails, and keep notices A running proxy could pass its route check before the step, so the first config pass wrote the route, and then the pack's step failed on a plugin Hedge did not install. The second config pass now takes out the config that waits for a check of a pack whose step failed in this apply. Apply runs a waiting check again after the steps also when its pack's step failed, so its line shows what the check finds then. The second pass replaces only the first pass's config lines that it reads again, so the notice that an edited plugin is yours now stays. The Codex notice carries no repair, as the Claude one. The proxy fake now starts only when the installed proxy unit is restarted or enabled with --now by its real name. The fake Claude CLI records each plugin's scope, and the test asserts a user install. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 19 ++++--- packs/hub/hub.test.ts | 19 ++++++- packs/hub/lib/cli-proxy-testing.ts | 4 +- src/adapters/provider-config/plugins.ts | 2 +- .../provider-config/provider-config.test.ts | 2 +- src/reconciler/apply.test.ts | 49 ++++++++++++++----- src/reconciler/apply.ts | 23 +++++---- 7 files changed, 86 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6e58139..45168697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,12 +30,19 @@ steps, unless it passed or waited before them, and reports what it finds then. When it passes then, apply drops its old `Fix:` and writes the config that waits for it, so a hub whose proxy was stopped routes Claude - Code in one apply. That config pass replaces the lines of the first one, - so a Claude plugin that waited for Claude Code leaves no old `Fix:`. When - a step of the check's pack failed in that apply, the config stays out, so - a hub whose proxy step fails on a plugin Hedge did not install keeps - Claude Code on its own sign-in. `hedge doctor` reads one result of each - check for both the config and its line. + Code in one apply. That config pass replaces the lines of the first pass + that it reads again, so a Claude plugin that waited for Claude Code leaves + no old `Fix:`, and a notice such as "It is yours now" stays. When a step + of a pack fails, apply takes out the config that waits for a check of that + pack, also when the check passed before the step. So a hub whose proxy + step fails on a plugin Hedge did not install keeps Claude Code on its own + sign-in, even while the proxy runs. Apply still runs the check again and + reports what it finds. `hedge doctor` reads one result of each check for + both the config and its line. +- When `hedge cli-proxy connect` fails on a box, the hub names the box and + the exit code. It never prints the box's output. +- The notice that a Codex plugin is yours now no longer fails + `hedge doctor`, as for a Claude plugin. ### Hedge is cloud only diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 29663e8b..dabf6186 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -6,7 +6,7 @@ import { providerConfigAdapter } from "../../src/adapters/provider-config/adapte import { apply, check } from "../../src/reconciler/apply.ts"; import { boxCommand } from "./lib/box-command.ts"; import { cliProxy, codexProviderTable, type ProxyMachine } from "./lib/cli-proxy.ts"; -import { dataDir, proxyMachine, type Proxy } from "./lib/cli-proxy-testing.ts"; +import { clientKey, dataDir, proxyMachine, type Proxy } from "./lib/cli-proxy-testing.ts"; import { Coder } from "./lib/coder.ts"; import { fakeMachine } from "./lib/testing.ts"; import type { AvailableSource, MachineFile, MachineRole, Plan, Profile } from "../../src/pack/types.ts"; @@ -488,10 +488,25 @@ describe("the Own-account balancing workflow", () => { const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); const applied = await apply(result, { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }); expect(applied.ok).toBe(false); - expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", "apply"], ["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); }); + test("with a running proxy whose doctor finds a plugin Hedge did not install, one apply fails the step and takes back the route it wrote before the step", async () => { + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, { credentials: [{ provider: "claude" }] }); + mkdirSync(join(home, ".config/hedge"), { recursive: true }); + writeFileSync(join(home, ".config/hedge/cli-proxy.key"), `${clientKey}\n`); + mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); + writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); + const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); + const applied = await apply(result, { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }); + expect(applied.ok).toBe(false); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); + expect(applied.actions.map((action) => [action.target, action.operation])).toEqual([[".claude/settings.json", "create"], [".claude/settings.json", "replace"]]); + const settings = JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")); + expect([settings.env?.ANTHROPIC_BASE_URL, settings.apiKeyHelper]).toEqual([undefined, undefined]); + }); + test("takes Claude Code off the proxy once its pool empties, and lists the To do", async () => { const proxy: Proxy = { credentials: [{ provider: "claude" }] }; const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, proxy); diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts index 0520d97b..8486ed86 100644 --- a/packs/hub/lib/cli-proxy-testing.ts +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -31,6 +31,8 @@ export function fakeProxy(proxy: Proxy = {}) { return { fetch, requests }; } +const startsProxy = (command: string[]) => ["systemctl --user restart hedge-cli-proxy.service", "systemctl --user enable --now hedge-cli-proxy.service"].includes(command.join(" ")); + export function proxyMachine(make: (prefix: string) => string, fields: Partial = {}, proxy: Proxy = {}, options: { role?: string; checksum?: string } = {}) { const home = fields.home ?? roleHome(make("hedge-cli-proxy-"), options.role ?? "hub"); const fake = fakeRun((command) => { @@ -39,7 +41,7 @@ export function proxyMachine(make: (prefix: string) => string, fields: Partial

{ const kept = await apply(plan([wants()]), { home, adapters: [cli.adapter] }); expect(kept.findings.map((finding) => finding.say)).toContainEqual(`Kept the Codex plugin ${github} as you left it: it is disabled now, and Hedge installed it enabled.`); const released = await apply(plan(), { home, adapters: [cli.adapter] }); - expect(released.findings.map((finding) => finding.say)).toContainEqual(expect.stringContaining("It is yours now")); + expect(released.findings).toContainEqual({ id: `plugin:codex:${github}`, pack: "engine", say: `Kept the Codex plugin ${github}, which no pack enables any more, because it changed outside Hedge. It is yours now.` }); expect(cli.changes()).toEqual([["codex", "plugin", "add", github, "--json"]]); expect([...cli.installed.keys()]).toEqual([github]); await apply(plan(), { home, adapters: [cli.adapter] }); diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index 9bfd0fd1..4e9a9083 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -332,7 +332,23 @@ describe("a pack check that waits for the person", () => { expect(await check(plan({ files: [], checks, steps, config }), options)).toMatchObject({ ok: true, findings: [] }); }); - test("that config waits for and that passes once a step ran replaces the first config findings, so a plugin that waited for the step's Claude Code leaves no Fix", async () => { + const claudeCli = (ready: () => boolean) => { + const installed: Array<{ id: string; scope: string }> = []; + const run: CommandRunner = async (command) => { + if (!ready()) return { code: 127, stdout: "" }; + const scope = command.includes("--scope") ? command[command.indexOf("--scope") + 1]! : "user"; + if (!["user", "project", "local"].includes(scope)) return { code: 1, stdout: "" }; + if (command[2] === "list") return { code: 0, stdout: JSON.stringify(installed) }; + if (command[2] === "install") installed.push({ id: command[3]!, scope }); + if (command[2] === "uninstall") installed.splice(installed.findIndex((entry) => entry.id === command[3] && entry.scope === scope), 1); + return { code: 0, stdout: "" }; + }; + return { run, installed }; + }; + const context7 = "context7@claude-plugins-official"; + const plugin = { provider: "claude" as const, key: `enabledPlugins.${context7}`, value: true, pack: "base" }; + + test("that config waits for and that passes once a step ran replaces the first config findings, so a plugin that waited for the step's Claude Code leaves no Fix and is installed for the user", async () => { const pool = installedPool(); pool.set(0); let installed = false; @@ -340,19 +356,30 @@ describe("a pack check that waits for the person", () => { if (command.join(" ") === "install-pool") installed = true; return pool.run(command); }; - const plugins: string[] = []; - const claude: CommandRunner = async (command) => { - if (!installed) return { code: 127, stdout: "" }; - if (command.join(" ") === "claude plugin list --json") return { code: 0, stdout: JSON.stringify(plugins.map((id) => ({ id, scope: "user" }))) }; - if (command[2] === "install") plugins.push(command[3]!); - return { code: 0, stdout: "[]" }; - }; - const plugin = { provider: "claude" as const, key: "enabledPlugins.context7@claude-plugins-official", value: true, pack: "base" }; + const claude = claudeCli(() => installed); const home = temp.make("home-"); - const applied = await apply(plan({ files: [], checks, steps, config: [...config, plugin] }), { home, adapters: [providerConfigAdapter({ run: claude })], run }); + const options = { home, adapters: [providerConfigAdapter({ run: claude.run })], run }; + const applied = await apply(plan({ files: [], checks, steps, config: [...config, plugin] }), options); expect(applied).toMatchObject({ ok: true, findings: [] }); - expect(plugins).toEqual(["context7@claude-plugins-official"]); + expect(claude.installed).toEqual([{ id: context7, scope: "user" }]); + expect(settings(home).env).toEqual({ ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }); + expect(await check(plan({ files: [], checks, steps, config: [...config, plugin] }), options)).toMatchObject({ ok: true, findings: [] }); + claude.installed.splice(0, 1, { id: context7, scope: "project" }); + expect((await check(plan({ files: [], checks, steps, config: [...config, plugin] }), options)).findings).toEqual([{ id: `plugin:${context7}`, pack: "engine", say: `The Claude plugin ${context7} needs to be installed.`, repair: "apply" }]); + }); + + test("that config waits for and that passes once a step ran keeps the first pass's notice that an edited plugin is yours now", async () => { + const pool = installedPool(); + pool.set(0); + const claude = claudeCli(() => true); + const home = temp.make("home-"); + const options = { home, adapters: [providerConfigAdapter({ run: claude.run })], run: pool.run }; + await apply(plan({ files: [], config: [plugin] }), options); + writeFileSync(join(home, ".claude/settings.json"), JSON.stringify({ enabledPlugins: { [context7]: false } })); + const applied = await apply(plan({ files: [], checks, steps, config }), options); + expect(applied.findings).toEqual([{ id: `plugin:${context7}`, pack: "engine", say: `Kept the Claude plugin ${context7}, which no pack enables any more, because it changed outside Hedge. It is yours now.` }]); expect(settings(home).env).toEqual({ ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }); + expect(claude.installed).toEqual([{ id: context7, scope: "user" }]); }); test("doctor reads one result of a check that config waits for, for both the config and the finding", async () => { diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 558d7b0e..89960d5a 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -186,11 +186,13 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R await undo(items.filter((item) => item.cause !== "pack")); if (context.dryRun) return { report, checks: gated.checks }; const settled = await waitingChecks(plan, run, gated.checks, report.findings, failed); - if (!settled.length) return { report, checks: gated.checks }; + const withdrawn = gated.plan.config.filter((entry) => failed.has(entry.pack) && plan.checks.some((item) => item.pack === entry.pack && item.id === entry.after)); + if (!settled.length && !withdrawn.length) return { report, checks: gated.checks }; for (const adapter of adapters.filter((each) => each.name === "config")) { - const result = await adapter.apply(withSettled(plan, gated.plan, settled), context); - const stale = firstFindings.get(adapter) ?? []; - report.findings.splice(0, report.findings.length, ...report.findings.filter((finding) => !stale.includes(finding)), ...result.findings); + const result = await adapter.apply(secondPass(plan, gated.plan, settled, withdrawn), context); + const first = firstFindings.get(adapter) ?? []; + const stale = (finding: Finding) => first.includes(finding) && (finding.repair !== undefined || result.findings.some((fresh) => fresh.id === finding.id && fresh.pack === finding.pack)); + report.findings.splice(0, report.findings.length, ...report.findings.filter((finding) => !stale(finding)), ...result.findings); report.actions.push(...result.actions); if (result.failed || result.blocked) report.ok = false; } @@ -201,20 +203,21 @@ async function waitingChecks(plan: Plan, run: Runner, gated: Checked, findings: const settled: Array> = []; for (const item of plan.checks.filter((check) => check.todo)) { const before = gated.find(([ran]) => ran === item)?.[1]; - if (before === 0 || before === waitsForPerson || (before !== undefined && failed.has(item.pack))) continue; + if (before === 0 || before === waitsForPerson) continue; const code = await run(item.run); const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, ...(code === 0 ? [] : [checkFinding(item, code)])); - if (code === 0 && before !== undefined) settled.push(item); + if (code === 0 && before !== undefined && !failed.has(item.pack)) settled.push(item); } return settled; } -function withSettled(plan: Plan, gated: Plan, settled: Array>): Plan { +function secondPass(plan: Plan, gated: Plan, settled: Array>, withdrawn: Plan["config"]): Plan { const passes = (pack: string, id?: string) => settled.some((item) => item.pack === pack && item.id === id); - const config = plan.config.filter((entry) => gated.config.includes(entry) || passes(entry.pack, entry.after)); - const dropped = gated.dropped.filter((entry) => !entry.item.startsWith("config ") || !passes(entry.pack, entry.check)); - return { ...gated, config, dropped }; + const config = plan.config.filter((entry) => (gated.config.includes(entry) && !withdrawn.includes(entry)) || passes(entry.pack, entry.after)); + const kept = gated.dropped.filter((entry) => !entry.item.startsWith("config ") || !passes(entry.pack, entry.check)); + const failed = withdrawn.map((entry) => ({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `a step of the ${entry.pack} pack failed`, check: entry.after, value: entry.value })); + return { ...gated, config, dropped: [...kept, ...failed] }; } export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { From f62c9650b4edac1aee37b0cfc21eeee8838497ae Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:51:07 +0000 Subject: [PATCH 16/32] Hold waiting config until its pack's steps ran, and keep skipped Fix lines Config that waits for a check of a pack with steps in this apply no longer enters the first config pass. The gate holds it: the config adapter leaves its keys, plugins and MCP servers as they are, so a route that an earlier apply wrote stays and an absent one stays absent. Apply runs its check after the steps and writes the config in the second pass only when the pack's steps passed and the check passes then. When the check fails, the config leaves as it does for any failing check. The round 3 withdraw path is gone, so the second pass never drops a table the first pass kept, such as a hedge-proxy Codex table of your own. The second pass drops a first-pass line only when it says the same line again or reports that the line's operation now succeeded. The config adapter names those operations: plugin and marketplace changes, MCP server changes, and reaching Claude Code or Codex. A Fix for an operation the second pass skips, such as a failed marketplace update, stays. A failed handover names the hub-side command that sends the same key again: hedge box run -- hedge cli-proxy connect < the key file. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 28 +++++---- packs/hub/hub.test.ts | 57 +++++++++++++++---- packs/hub/lib/cli-proxy-forwards.test.ts | 6 +- packs/hub/lib/links.ts | 2 +- src/adapters/provider-config/adapter.ts | 30 +++++++--- .../provider-config/claude-servers.ts | 19 +++++-- src/adapters/provider-config/plugins.ts | 56 +++++++++++------- .../provider-config/provider-config.test.ts | 13 +++-- src/pack/types.ts | 2 + src/reconciler/apply.test.ts | 19 +++++++ src/reconciler/apply.ts | 57 ++++++++++--------- 11 files changed, 201 insertions(+), 88 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45168697..8107d1d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,17 +30,25 @@ steps, unless it passed or waited before them, and reports what it finds then. When it passes then, apply drops its old `Fix:` and writes the config that waits for it, so a hub whose proxy was stopped routes Claude - Code in one apply. That config pass replaces the lines of the first pass - that it reads again, so a Claude plugin that waited for Claude Code leaves - no old `Fix:`, and a notice such as "It is yours now" stays. When a step - of a pack fails, apply takes out the config that waits for a check of that - pack, also when the check passed before the step. So a hub whose proxy - step fails on a plugin Hedge did not install keeps Claude Code on its own - sign-in, even while the proxy runs. Apply still runs the check again and - reports what it finds. `hedge doctor` reads one result of each check for - both the config and its line. + Code in one apply. That config pass drops a line of the first pass only + when it says the same line again, or when the operation of that line now + succeeds. So a Claude plugin that waited for Claude Code leaves no old + `Fix:`, a notice such as "It is yours now" stays, and a `Fix:` for an + operation that the second pass skips, such as a marketplace update, + stays. +- Config that waits for a check of a pack with steps in this apply waits + for those steps. Apply leaves its keys as they are until the steps ran, + then runs the check. It writes the config only when the steps passed and + the check passes then, and takes Hedge's value out when the check fails. + So a hub whose proxy step fails on a plugin Hedge did not install never + routes Claude Code to the proxy, not even while the step runs, and a + `hedge-proxy` Codex table of your own stays. A route that an earlier + apply wrote stays in place while the step runs. `hedge doctor` reads one + result of each check for both the config and its line. - When `hedge cli-proxy connect` fails on a box, the hub names the box and - the exit code. It never prints the box's output. + the exit code, and the command that hands over the same key again: + `hedge box run -- hedge cli-proxy connect < ~/.config/hedge/cli-proxy.key`. + It never prints the box's output. - The notice that a Codex plugin is yours now no longer fails `hedge doctor`, as for a Claude plugin. diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index dabf6186..f9e0bac2 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -492,19 +492,54 @@ describe("the Own-account balancing workflow", () => { expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); }); - test("with a running proxy whose doctor finds a plugin Hedge did not install, one apply fails the step and takes back the route it wrote before the step", async () => { - const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "claude", devboxes: [] }, { credentials: [{ provider: "claude" }] }); - mkdirSync(join(home, ".config/hedge"), { recursive: true }); - writeFileSync(join(home, ".config/hedge/cli-proxy.key"), `${clientKey}\n`); - mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); - writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); - const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); - const applied = await apply(result, { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }); + const runningHub = (ai: "claude" | "codex", proxy: Proxy, files: Record = {}) => { + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: ai, devboxes: [] }, proxy); + for (const [path, text] of Object.entries({ ".config/hedge/cli-proxy.key": `${clientKey}\n`, ...files })) { + mkdirSync(join(home, path, ".."), { recursive: true }); + writeFileSync(join(home, path), text); + } + const result = proxyOnly(plan({ ...balancing, plan: ai }, hub)); + const run = hedgeProxy(machine, result.engineRoot); + const routes: unknown[] = []; + const route = () => (existsSync(join(home, ".claude/settings.json")) ? JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).env?.ANTHROPIC_BASE_URL : undefined); + const watched = async (command: string[]) => { + if (command[3] === "doctor") routes.push(route()); + return run(command); + }; + return { home, result, routes, route, options: { home, adapters: [providerConfigAdapter()], run: watched } }; + }; + const foreignPlugin = { ".local/share/hedge-cli-proxy/plugins/other.so": "plugin" }; + + test("with a running proxy whose doctor finds a plugin Hedge did not install, apply fails the step and never routes Claude Code to the proxy, not even while the step's check runs", async () => { + const { result, routes, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }, foreignPlugin); + const applied = await apply(result, options); expect(applied.ok).toBe(false); expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); - expect(applied.actions.map((action) => [action.target, action.operation])).toEqual([[".claude/settings.json", "create"], [".claude/settings.json", "replace"]]); - const settings = JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")); - expect([settings.env?.ANTHROPIC_BASE_URL, settings.apiKeyHelper]).toEqual([undefined, undefined]); + expect(routes).toEqual([undefined, undefined]); + expect(route()).toBeUndefined(); + }); + + test("with a running proxy whose doctor finds a plugin Hedge did not install, a hedge-proxy Codex table of your own with another name stays, and apply says it kept it", async () => { + const mine = { model_provider: "hedge-proxy", model_providers: { "hedge-proxy": { ...codexProviderTable, name: "Mine" } } }; + const { home, result, options } = runningHub("codex", { credentials: [{ provider: "codex" }], models: ["gpt-6.1-sol"] }, { ...foreignPlugin, ".codex/config.toml": stringify(mine) }); + const applied = await apply(result, options); + expect(applied.ok).toBe(false); + expect(applied.findings.map((finding) => finding.id)).toContain("step:cli-proxy"); + expect(applied.findings).toContainEqual({ id: "kept:.codex/config.toml", pack: "engine", say: "Kept your model_providers.hedge-proxy in .codex/config.toml. Run hedge apply --adopt .codex/config.toml to take Hedge's value." }); + expect(parse(readFileSync(join(home, ".codex/config.toml"), "utf8"))).toEqual(mine); + }); + + test("with a running proxy that passes its step, the route an earlier apply wrote stays in place through the next apply, which changes nothing", async () => { + const { result, routes, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + const first = await apply(result, options); + expect(first.findings.map((finding) => finding.id)).toEqual(["check:cli-proxy-accounts"]); + expect(route()).toBe("http://127.0.0.1:8317"); + routes.splice(0); + const next = await apply(result, options); + expect(next.ok).toBe(true); + expect(next.actions).toEqual([]); + expect(routes).toEqual(["http://127.0.0.1:8317"]); + expect(route()).toBe("http://127.0.0.1:8317"); }); test("takes Claude Code off the proxy once its pool empties, and lists the To do", async () => { diff --git a/packs/hub/lib/cli-proxy-forwards.test.ts b/packs/hub/lib/cli-proxy-forwards.test.ts index a2aa71bc..5ea06627 100644 --- a/packs/hub/lib/cli-proxy-forwards.test.ts +++ b/packs/hub/lib/cli-proxy-forwards.test.ts @@ -55,12 +55,12 @@ test("handing over connects a box to the proxy with the key on stdin only, under expect(skipped.calls.some((call) => call.command.includes("cli-proxy"))).toBe(false); }); -test("a box that cannot reach the proxy yet keeps its own sign-in, and the handover goes on", async () => { +test("a box that cannot reach the proxy yet keeps its own sign-in, the hub names the command that hands over the same key again, and the handover goes on", async () => { const on = fakeMachine(temp.make("hedge-box-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); writeHome(on.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); const { coder } = fakeCoder({ ssh: async (_box, command) => ({ ...ok, code: command.includes("cli-proxy connect") ? 1 : 0, stderr: "The proxy does not answer." }) }); await handOver(coder, on.machine, "box-lane"); - expect(on.lines).toContain("box-lane keeps its own sign-in: hedge cli-proxy connect exited 1 there. Run hedge cli-proxy check on box-lane to see why."); + expect(on.lines).toContain("box-lane keeps its own sign-in: hedge cli-proxy connect exited 1 there. To see why and hand over the same key again, run on the hub: hedge box run box-lane -- hedge cli-proxy connect < ~/.config/hedge/cli-proxy.key"); const none = fakeMachine(temp.make("hedge-box-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); await handOver(fakeCoder().coder, none.machine, "box-lane"); @@ -74,7 +74,7 @@ test("a failed handover prints the hub's own words with the exit code, and neith const { coder } = fakeCoder({ ssh: async (_box, command) => ({ ...ok, ...output, code: command.includes("cli-proxy connect") ? 3 : 0 }) }); await handOver(coder, on.machine, "box-lane"); } - expect(on.lines.filter((line) => line.startsWith("box-lane keeps"))).toEqual(Array(2).fill("box-lane keeps its own sign-in: hedge cli-proxy connect exited 3 there. Run hedge cli-proxy check on box-lane to see why.")); + expect(on.lines.filter((line) => line.startsWith("box-lane keeps"))).toEqual(Array(2).fill("box-lane keeps its own sign-in: hedge cli-proxy connect exited 3 there. To see why and hand over the same key again, run on the hub: hedge box run box-lane -- hedge cli-proxy connect < ~/.config/hedge/cli-proxy.key")); expect(on.lines.join("\n")).not.toContain(clientKey); expect(on.lines.join("\n")).not.toContain("\u001b"); expect(on.lines.join("\n")).not.toContain("refused"); diff --git a/packs/hub/lib/links.ts b/packs/hub/lib/links.ts index 922fe321..e22665b9 100644 --- a/packs/hub/lib/links.ts +++ b/packs/hub/lib/links.ts @@ -70,7 +70,7 @@ async function handOverProxy(coder: Pick, here: Machine, box: stri const key = join(here.home, proxyKeyPath); if (!existsSync(key)) return here.say(`skip ~/${proxyKeyPath}: this machine has none`); const connected = await coder.ssh(box, `umask 077 && ${remoteHedge("cli-proxy connect")}`, readFileSync(key, "utf8")); - if (connected.code !== 0) return here.say(`${box} keeps its own sign-in: hedge cli-proxy connect exited ${connected.code} there. Run hedge cli-proxy check on ${box} to see why.`); + if (connected.code !== 0) return here.say(`${box} keeps its own sign-in: hedge cli-proxy connect exited ${connected.code} there. To see why and hand over the same key again, run on the hub: hedge box run ${box} -- hedge cli-proxy connect < ~/${proxyKeyPath}`); const lines = connected.stdout.split("\n"); if (lines.includes(waitingLines.claude)) here.say(`${box}: the proxy lists no Claude model yet. Run hedge apply on ${box}, so that Claude Code there uses its own sign-in until the proxy serves it.`); if (lines.includes(waitingLines.codex)) here.say(`${box}: the proxy lists no Codex model yet. Run hedge apply on ${box}, so that Codex there uses its own sign-in until the proxy serves it.`); diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 898abc8b..233343db 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -39,11 +39,11 @@ export function providerConfigAdapter(options: { run?: CommandRunner } = {}): Ad const gated = gatedKeys(plan); const reconcile = async () => { const { plugins, claudeEnabled, marketplaces, codexPlugins, ...result } = await reconcileFiles(plan, context.home, context.dryRun, context.adopt, outage, gated); - result.findings.push(...(await reconcilePlugins(plan, context.home, context.dryRun, run, { outage, accepted: plugins, claudeEnabled, marketplaces, acceptedCodex: codexPlugins }))); + const installed = await reconcilePlugins(plan, context.home, context.dryRun, run, { outage, accepted: plugins, claudeEnabled, marketplaces, acceptedCodex: codexPlugins }); const servers = await reconcileServers(plan, context.home, { dryRun: context.dryRun, adopt: context.adopt.includes(claudeJson), outage, gated: gatedPath(gated, "claude"), run }); result.actions.push(...servers.actions); - result.findings.push(...servers.findings); - return result; + result.findings.push(...installed.findings, ...servers.findings); + return { ...result, resolved: [...installed.resolved, ...servers.resolved] }; }; const result = context.dryRun ? await reconcile() : await withLock(context.home, reconcile); if (outage) result.findings.push({ id: "config:outage", pack: "engine", say: "Kept every provider setting and plugin that Hedge would remove until all your pack sources can be fetched.", repair: "apply" }); @@ -65,7 +65,7 @@ async function reconcileFiles(plan: Plan, home: string, dryRun: boolean, adopt: const fields = owned(plan, target); let merged: Merged; try { - merged = merge(target, before, old, fields, adopt.includes(target.path), { outage, gated: gatedPath(gated, target.provider), dropped: gatedFields(plan, target), configPath: join(home, target.path) }); + merged = merge(target, before, old, fields, adopt.includes(target.path), { outage, gated: gatedPath(gated, target.provider), dropped: gatedFields(plan, target), held: heldFields(plan, target), configPath: join(home, target.path) }); } catch (error) { keepOld(); result.findings.push({ id: `kept:${target.path}`, pack: "engine", say: `Kept ${target.path} as it is: Hedge cannot read it (${(error as Error).message}). Fix the file, then run hedge apply.`, repair: "human" }); @@ -106,11 +106,14 @@ function owned(plan: Plan, target: Target): Field[] { return fields; } -function merge(target: Target, text: string | undefined, previous: Field[], wanted: Field[], adopt: boolean, options: { outage: boolean; gated: (path: string[]) => boolean; dropped: GatedField[]; configPath: string }): Merged { +const heldFields = (plan: Plan, target: Target): Field[] => (plan.held ?? []).filter(ownedBy(target)).map((entry) => ({ path: keyPath(entry.key), value: entry.value, pack: entry.pack })); + +function merge(target: Target, text: string | undefined, previous: Field[], wanted: Field[], adopt: boolean, options: { outage: boolean; gated: (path: string[]) => boolean; dropped: GatedField[]; held: Field[]; configPath: string }): Merged { const current: Document = text === undefined ? {} : target.format.decode(text); const original = target.format.encode(current); const edited = previous.filter((field) => get(current, field.path) !== undefined && !same(get(current, field.path), field.value)); - const stale = options.outage ? [] : staleKeys(target.provider, current, (path) => target.owns(path) && !wanted.some((field) => overlaps(field.path, path)) && !edited.some((field) => overlaps(field.path, path))); + const holds = (path: string[]) => options.held.some((field) => overlaps(field.path, path)); + const stale = options.outage ? [] : staleKeys(target.provider, current, (path) => target.owns(path) && !wanted.some((field) => overlaps(field.path, path)) && !edited.some((field) => overlaps(field.path, path)) && !holds(path)); const record: Field[] = []; const kept: string[] = []; const changed: string[] = []; @@ -135,6 +138,17 @@ function merge(target: Target, text: string | undefined, previous: Field[], want return applied; }; const applied = reconcile(wanted); + for (const field of options.held) { + const value = get(current, field.path); + if (value === undefined) continue; + const old = previous.find((item) => samePath(item.path, field.path)); + if (same(value, field.value)) record.push(field); + else if (old && same(value, old.value)) record.push(old); + else { + if (!adopt) kept.push(field.path.join(".")); + if (old) record.push(old); + } + } const trust: Field[] = []; const departed = previous.filter((field) => !wanted.some((item) => overlaps(item.path, field.path)) && !isHookTrust(field)); const keepsEdit = (field: Field) => get(current, field.path) !== undefined && !same(get(current, field.path), field.value); @@ -148,7 +162,7 @@ function merge(target: Target, text: string | undefined, previous: Field[], want } const all = [...wanted, ...trust]; for (const field of previous) { - if (all.some((item) => overlaps(item.path, field.path))) continue; + if (all.some((item) => overlaps(item.path, field.path)) || holds(field.path)) continue; if ((options.outage && !options.gated(field.path)) || keepsEdit(field)) record.push(field); else if (get(current, field.path) !== undefined) { remove(current, field.path); @@ -158,7 +172,7 @@ function merge(target: Target, text: string | undefined, previous: Field[], want const untouched = (field: GatedField) => get(current, field.path) === undefined || same(get(current, field.path), field.value); const unrecorded = options.dropped .filter((field) => options.dropped.every((other) => other.check !== field.check || other.pack !== field.pack || untouched(other))) - .filter((field) => !all.some((item) => overlaps(item.path, field.path))) + .filter((field) => !all.some((item) => overlaps(item.path, field.path)) && !holds(field.path)) .map((field) => field.path); for (const path of [...stale, ...unrecorded]) { if (get(current, path) === undefined) continue; diff --git a/src/adapters/provider-config/claude-servers.ts b/src/adapters/provider-config/claude-servers.ts index 9ccad3ea..f59f47f8 100644 --- a/src/adapters/provider-config/claude-servers.ts +++ b/src/adapters/provider-config/claude-servers.ts @@ -10,7 +10,7 @@ export const serversTarget = ".config/hedge/claude-servers.json"; /** value is what the pack asked for; stored is what Claude Code wrote for it, which can differ in shape. */ type Owned = { name: string; value: unknown; stored: unknown; pack: string }; type Wanted = { name: string; value: unknown; pack: string }; -type Result = { actions: Action[]; findings: Finding[] }; +type Result = { actions: Action[]; findings: Finding[]; resolved: string[] }; export const isMcpServer = (path: string[]) => path[0] === "mcpServers"; @@ -26,8 +26,9 @@ const finding = (id: string, say: string, repair?: Finding["repair"]): Finding = * Claude Code saves that file from several paths, not all of them locked, so no other writer can avoid racing it. */ export async function reconcileServers(plan: Plan, home: string, options: { dryRun: boolean; adopt: boolean; outage: boolean; gated: (path: string[]) => boolean; run: CommandRunner }): Promise { - const result: Result = { actions: [], findings: [] }; + const result: Result = { actions: [], findings: [], resolved: [] }; const wanted = wantedServers(plan); + const held = wantedServers({ ...plan, config: plan.held ?? [] }).map((server) => server.name); const recordText = await readState(home, serversTarget); const previous = parseOwned(recordText); if (!wanted.length && !previous.length) return result; @@ -56,7 +57,7 @@ export async function reconcileServers(plan: Plan, home: string, options: { dryR } for (const old of previous) { if (wanted.some((server) => server.name === old.name)) continue; - if (options.outage && !options.gated(["mcpServers", old.name])) next.push(old); + if (held.includes(old.name) || (options.outage && !options.gated(["mcpServers", old.name]))) next.push(old); else if (same(servers[old.name], old.stored)) remove.push(old.name); } if (kept.length) result.findings.push(finding(`kept:${claudeJson}`, `Kept your ${kept.join(", ")} in ${claudeJson}. Run hedge apply --adopt ${claudeJson} to take Hedge's value.`)); @@ -75,7 +76,10 @@ export async function reconcileServers(plan: Plan, home: string, options: { dryR for (const name of remove) { const removed = await claude(["remove", name]); if (removed.code === 127) return waiting(result, wanted); - if (removed.code === 0) continue; + if (removed.code === 0) { + result.resolved.push(`claude-server:${name}`); + continue; + } blocked.add(name); const old = previous.find((item) => item.name === name); if (old) next.push(old); @@ -85,9 +89,12 @@ export async function reconcileServers(plan: Plan, home: string, options: { dryR for (const server of add.filter((item) => !blocked.has(item.name))) { const outcome = await claude(["add-json", server.name, JSON.stringify(server.value)]); if (outcome.code === 127) return waiting(result, wanted); - if (outcome.code === 0) added.push(server); - else result.findings.push(finding(`claude-server:${server.name}`, `Claude Code did not add the MCP server ${server.name}: ${firstLine(outcome)}. Check its value in the ${server.pack} pack.`, "apply")); + if (outcome.code === 0) { + added.push(server); + result.resolved.push(`claude-server:${server.name}`); + } else result.findings.push(finding(`claude-server:${server.name}`, `Claude Code did not add the MCP server ${server.name}: ${firstLine(outcome)}. Check its value in the ${server.pack} pack.`, "apply")); } + result.resolved.push("claude-servers"); const after = await currentServers(home); for (const server of added) if (after[server.name] !== undefined) next.push({ ...server, stored: after[server.name] }); if (reason) result.actions.push({ target: claudeJson, operation: "replace", reason }); diff --git a/src/adapters/provider-config/plugins.ts b/src/adapters/provider-config/plugins.ts index a6f1460d..7dd2d0c5 100644 --- a/src/adapters/provider-config/plugins.ts +++ b/src/adapters/provider-config/plugins.ts @@ -42,7 +42,7 @@ type PluginSettings = { outage: boolean; accepted?: string[]; claudeEnabled?: Re * Installs the plugins packs enable, through Claude Code and Codex in the given home. Removes only plugins that Hedge installed. * Reads the earlier Hedge's record, and never writes it: a plugin that Hedge installed becomes this Hedge's only once a pack enables it again. */ -export async function reconcilePlugins(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings): Promise { +export async function reconcilePlugins(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings): Promise<{ findings: Finding[]; resolved: string[] }> { const text = await readState(home, pluginsTarget); const earlier = parseOwned(await readState(home, earlierTarget), earlierTarget); const record = adopt(parseOwned(text, pluginsTarget), earlier, { claude: wantedPlugins(plan), codex: wantedCodexPlugins(plan) }); @@ -53,16 +53,20 @@ export async function reconcilePlugins(plan: Plan, home: string, dryRun: boolean await writeWithBackups(home, [{ target: pluginsTarget, before: saved, after }]); saved = after; }; - const claude = await reconcileClaude(plan, home, dryRun, run, settings, record.claude); + const resolved: string[] = []; + const claude = await reconcileClaude(plan, home, dryRun, run, settings, record.claude, resolved); if (claude.owned) await save({ ...record, claude: claude.owned }); const owned = { ...record, ...(claude.owned ? { claude: claude.owned } : {}) }; - const codex = await reconcileCodex(plan, home, dryRun, run, settings, owned.codex, (next) => save({ ...owned, codex: next })); - return [...claude.findings, ...codex]; + const codex = await reconcileCodex(plan, home, dryRun, run, settings, owned.codex, (next) => save({ ...owned, codex: next }), resolved); + return { findings: [...claude.findings, ...codex], resolved }; } -async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, record: ClaudeOwned[]): Promise<{ findings: Finding[]; owned?: ClaudeOwned[] }> { +const heldPlan = (plan: Plan): Plan => ({ ...plan, config: plan.held ?? [] }); + +async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, record: ClaudeOwned[], resolved: string[]): Promise<{ findings: Finding[]; owned?: ClaudeOwned[] }> { const outage = settings.outage; const planned = wantedPlugins(plan); + const held = wantedPlugins(heldPlan(plan)); const owned = record.map((entry) => entry.id); const wanted = planned.filter((id) => settings.accepted?.includes(id)); if (!planned.length && !owned.length) return { findings: [] }; @@ -72,8 +76,9 @@ async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: C if (!installed) { return { findings: planned.length ? [{ id: "plugins:claude", pack: "engine", say: `Claude Code is not installed yet, so its plugins wait: ${planned.join(", ")}.`, repair: "apply" }] : [] }; } + resolved.push("plugins:claude"); const missing = wanted.filter((id) => !installed.includes(id)); - const leaving = outage ? [] : record.filter((entry) => !planned.includes(entry.id) && installed.includes(entry.id)); + const leaving = outage ? [] : record.filter((entry) => !planned.includes(entry.id) && !held.includes(entry.id) && installed.includes(entry.id)); const changedOutside = (entry: ClaudeOwned) => entry.enabled !== undefined && settings.claudeEnabled?.[entry.id] !== undefined && settings.claudeEnabled[entry.id] !== entry.enabled; const released = leaving.filter(changedOutside).map((entry) => entry.id); const departed = leaving.map((entry) => entry.id).filter((id) => !released.includes(id)); @@ -85,30 +90,36 @@ async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: C ...releasedFindings, ] }; } - const { findings, unreachable } = await fetchMarketplaces(claude, missing, { ...officialMarketplaces, ...settings.marketplaces }); + const { findings, unreachable } = await fetchMarketplaces(claude, missing, { ...officialMarketplaces, ...settings.marketplaces }, resolved); findings.push(...releasedFindings); const next = new Map(record.filter((entry) => !released.includes(entry.id) && (installed.includes(entry.id) || (outage && !planned.includes(entry.id)))).map((entry) => [entry.id, entry])); for (const id of missing.filter((id) => !unreachable.includes(marketplaceOf(id)))) { - if ((await claude(["install", id, "--scope", "user"])).code === 0) next.set(id, { id, enabled: true }); - else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not install the Claude plugin ${id}.`, repair: "apply" }); + if ((await claude(["install", id, "--scope", "user"])).code === 0) { + next.set(id, { id, enabled: true }); + resolved.push(`plugin:${id}`); + } else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not install the Claude plugin ${id}.`, repair: "apply" }); } for (const id of departed) { - if ((await claude(["uninstall", id, "--scope", "user", "--keep-data"])).code === 0) next.delete(id); - else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not remove the Claude plugin ${id}.`, repair: "apply" }); + if ((await claude(["uninstall", id, "--scope", "user", "--keep-data"])).code === 0) { + next.delete(id); + resolved.push(`plugin:${id}`); + } else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not remove the Claude plugin ${id}.`, repair: "apply" }); } return { findings, owned: [...next.values()].sort((a, b) => a.id.localeCompare(b.id)) }; } const codexFinding = (id: string, say: string): Finding => ({ id: `plugin:codex:${id}`, pack: "engine", say, repair: "apply" }); -async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, owned: CodexOwned[], save: (next: CodexOwned[]) => Promise): Promise { +async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, owned: CodexOwned[], save: (next: CodexOwned[]) => Promise, resolved: string[]): Promise { const planned = wantedCodexPlugins(plan); + const held = wantedCodexPlugins(heldPlan(plan)); if (!planned.length && !owned.length) return []; if (!settings.acceptedCodex) return planned.length ? [{ id: "plugins:codex", pack: "engine", say: `Codex plugins wait until Hedge can change .codex/config.toml: ${planned.join(", ")}.`, repair: "apply" }] : []; const wanted = planned.filter((id) => settings.acceptedCodex!.includes(id)); const codex: Codex = (args) => run(["codex", "plugin", ...args], { home, clearEnv: ["CODEX_HOME"] }); const installed = await listCodex(codex); if (!installed) return planned.length ? [{ id: "plugins:codex", pack: "engine", say: `Codex is not installed yet, so its plugins wait: ${planned.join(", ")}.`, repair: "apply" }] : []; + resolved.push("plugins:codex"); const findings: Finding[] = []; const record = new Map(owned.map((entry) => [entry.id, entry])); const commit = () => save([...record.values()].sort((a, b) => a.id.localeCompare(b.id))); @@ -123,7 +134,8 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co if (after) record.set(id, after); else if (listed) record.delete(id); await commit(); - if (added.code !== 0 || !after) findings.push(codexFinding(id, `Could not install the Codex plugin ${id}: ${added.code !== 0 ? reasonOf(added) : listed ? "Codex does not list it afterwards" : "Codex cannot list its plugins afterwards"}. Hedge tries again at the next apply.`)); + if (added.code === 0 && after) resolved.push(`plugin:codex:${id}`); + else findings.push(codexFinding(id, `Could not install the Codex plugin ${id}: ${added.code !== 0 ? reasonOf(added) : listed ? "Codex does not list it afterwards" : "Codex cannot list its plugins afterwards"}. Hedge tries again at the next apply.`)); }; for (const id of wanted) { const found = present(id); @@ -145,7 +157,7 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co } if (found.version !== mine.version) record.set(id, { ...mine, version: found.version }); } - for (const mine of [...record.values()].filter((entry) => !planned.includes(entry.id))) { + for (const mine of [...record.values()].filter((entry) => !planned.includes(entry.id) && !held.includes(entry.id))) { if (settings.outage) continue; const found = present(mine.id); if (!found) { @@ -162,8 +174,10 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co continue; } const removed = await codex(["remove", mine.id, "--json"]); - if (removed.code === 0) record.delete(mine.id); - else findings.push(codexFinding(mine.id, `Could not remove the Codex plugin ${mine.id}: ${reasonOf(removed)}.`)); + if (removed.code === 0) { + record.delete(mine.id); + resolved.push(`plugin:codex:${mine.id}`); + } else findings.push(codexFinding(mine.id, `Could not remove the Codex plugin ${mine.id}: ${reasonOf(removed)}.`)); } await commit(); return findings; @@ -199,7 +213,7 @@ function addSource(source: unknown) { return Object.keys(fields).length === 1 && typeof value === "string" ? value : undefined; } -async function fetchMarketplaces(claude: Claude, plugins: string[], sources: Sources): Promise<{ findings: Finding[]; unreachable: string[] }> { +async function fetchMarketplaces(claude: Claude, plugins: string[], sources: Sources, resolved: string[]): Promise<{ findings: Finding[]; unreachable: string[] }> { const needed = [...new Set(plugins.map(marketplaceOf))]; const fetched = needed.length ? await listMarketplaces(claude) : undefined; const findings: Finding[] = []; @@ -208,12 +222,16 @@ async function fetchMarketplaces(claude: Claude, plugins: string[], sources: Sou const source = Object.hasOwn(sources, name) ? sources[name] : undefined; if (fetched!.includes(name)) { const updated = await claude(["marketplace", "update", name]); - if (updated.code !== 0) findings.push({ id: `plugin-marketplace:${name}`, pack: "engine", say: `Could not update the Claude plugin marketplace ${name}: ${reasonOf(updated)}.`, repair: "apply" }); + if (updated.code === 0) resolved.push(`plugin-marketplace:${name}`); + else findings.push({ id: `plugin-marketplace:${name}`, pack: "engine", say: `Could not update the Claude plugin marketplace ${name}: ${reasonOf(updated)}.`, repair: "apply" }); continue; } if (!source) continue; const added = await claude(["marketplace", "add", source, "--scope", "user"]); - if (added.code === 0) continue; + if (added.code === 0) { + resolved.push(`plugin-marketplace:${name}`); + continue; + } unreachable.push(name); const waiting = plugins.filter((id) => marketplaceOf(id) === name).join(", "); findings.push({ id: `plugin-marketplace:${name}`, pack: "engine", say: `Could not add the Claude plugin marketplace ${name} from ${withoutCredentials(source)}: ${reasonOf(added)}. Its plugins wait: ${waiting}.`, repair: "apply" }); diff --git a/src/adapters/provider-config/provider-config.test.ts b/src/adapters/provider-config/provider-config.test.ts index 6506df13..cff65286 100644 --- a/src/adapters/provider-config/provider-config.test.ts +++ b/src/adapters/provider-config/provider-config.test.ts @@ -732,12 +732,17 @@ describe("Codex plugins", () => { cli.installed.set(github, { version: "1.0.0", enabled: false }); const kept = await apply(plan([wants()]), { home, adapters: [cli.adapter] }); expect(kept.findings.map((finding) => finding.say)).toContainEqual(`Kept the Codex plugin ${github} as you left it: it is disabled now, and Hedge installed it enabled.`); + const yours = { id: `plugin:codex:${github}`, pack: "engine", say: `Kept the Codex plugin ${github}, which no pack enables any more, because it changed outside Hedge. It is yours now.` }; + writeFileSync(join(home, codex), stringify({ plugins: { [github]: { enabled: false } } })); + const doctor = await check(plan(), { home, adapters: [cli.adapter] }); + expect(doctor.findings).toEqual([yours]); + expect(doctor.ok).toBe(true); const released = await apply(plan(), { home, adapters: [cli.adapter] }); - expect(released.findings).toContainEqual({ id: `plugin:codex:${github}`, pack: "engine", say: `Kept the Codex plugin ${github}, which no pack enables any more, because it changed outside Hedge. It is yours now.` }); - expect(cli.changes()).toEqual([["codex", "plugin", "add", github, "--json"]]); - expect([...cli.installed.keys()]).toEqual([github]); + expect(released.findings).toContainEqual(yours); await apply(plan(), { home, adapters: [cli.adapter] }); - expect(cli.changes()).toHaveLength(1); + expect(cli.changes()).toEqual([["codex", "plugin", "add", github, "--json"]]); + expect(Object.fromEntries(cli.installed)).toEqual({ [github]: { version: "1.0.0", enabled: false } }); + expect(parse(readFileSync(join(home, codex), "utf8"))).toEqual({ plugins: { [github]: { enabled: false } } }); }); test("keeps the plugins Hedge installed while a pack source is out, and removes them once it is back", async () => { diff --git a/src/pack/types.ts b/src/pack/types.ts index fb07e09c..5310f6b9 100644 --- a/src/pack/types.ts +++ b/src/pack/types.ts @@ -255,6 +255,8 @@ export type Plan = { unanswered: PackSetting[]; ask: PackSetting[]; dropped: Array<{ pack: string; item: string; reason: string; check?: string; value?: unknown }>; + /** Config entries whose check runs only after their pack's steps in this apply. The config adapter leaves their keys as they are. */ + held?: Array>; }; export type Finding = { id: string; pack: string; say: string; repair?: "apply" | "human"; todo?: true }; diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index 4e9a9083..d67967bb 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -382,6 +382,25 @@ describe("a pack check that waits for the person", () => { expect(claude.installed).toEqual([{ id: context7, scope: "user" }]); }); + test("that config waits for and that passes once a step ran keeps a Fix of the first pass for an operation the second pass skips", async () => { + const pool = installedPool(); + pool.set(0); + const installed: Array<{ id: string; scope: string }> = []; + const claude: CommandRunner = async (command) => { + const words = command.slice(2).join(" "); + if (words === "list --json") return { code: 0, stdout: JSON.stringify(installed) }; + if (words === "marketplace list --json") return { code: 0, stdout: JSON.stringify([{ name: "claude-plugins-official" }]) }; + if (words === "marketplace update claude-plugins-official") return { code: 1, stdout: "", stderr: "network down" }; + if (command[2] === "install") installed.push({ id: command[3]!, scope: "user" }); + return { code: 0, stdout: "" }; + }; + const home = temp.make("home-"); + const applied = await apply(plan({ files: [], checks, steps, config: [...config, plugin] }), { home, adapters: [providerConfigAdapter({ run: claude })], run: pool.run }); + expect(applied.findings).toEqual([{ id: "plugin-marketplace:claude-plugins-official", pack: "engine", say: "Could not update the Claude plugin marketplace claude-plugins-official: network down.", repair: "apply" }]); + expect(installed).toEqual([{ id: context7, scope: "user" }]); + expect(settings(home).env).toEqual({ ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }); + }); + test("doctor reads one result of a check that config waits for, for both the config and the finding", async () => { const home = temp.make("home-"); const options = { home, adapters: [providerConfigAdapter()], run: exitCodes({}) }; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 89960d5a..77bef064 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -10,7 +10,8 @@ import { hublessReason } from "./resolve.ts"; import { withLegacyNames } from "../legacy.ts"; export type AdapterContext = { home: string; dryRun: boolean; adopt: string[]; adoptFrom?: string[] }; -export type AdapterResult = { actions: Action[]; findings: Finding[]; blocked?: boolean; failed?: boolean }; +/** resolved names the ids of findings whose operation succeeded in this run, so a later pass drops an earlier pass's line for it. */ +export type AdapterResult = { actions: Action[]; findings: Finding[]; blocked?: boolean; failed?: boolean; resolved?: string[] }; export type Adapter = { name: "files" | "config" | "jobs" | "identity" | "t3" | "fleet"; /** The targets that --adopt may name for this adapter. */ @@ -107,7 +108,9 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R const run = viaEngine(plan, options.run ?? spawnRunner(options.home, plan.machine.profileDir)); const node = plan.onHub || plan.pullsFromHub ? plan.machine.name : undefined; const issue = !context.dryRun && node && options.issue ? (ready: CatalogEntry[]) => options.issue!(ready, node) : undefined; - const gated = await gate(plan, run, !context.dryRun, issue); + const skipped = new Set(options.skip ?? []); + const stepping = new Set(context.dryRun ? [] : plan.steps.filter((step) => !skipped.has(ledgerKey(step))).map((step) => step.pack)); + const gated = await gate(plan, run, !context.dryRun, stepping, issue); report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun)), ...gated.checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); if (!context.dryRun && gated.failed.some(repairedByHedge)) report.ok = false; const firstFindings = new Map(); @@ -154,7 +157,6 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R if (step.pack === "base") report.ok = false; } }; - const skipped = new Set(options.skip ?? []); const items = undoItems(plan, before.steps).filter((item) => !skipped.has(ledgerKey(item.step))); await undo(items.filter((item) => item.cause === "pack")); const waits = waiter(plan, run); @@ -185,13 +187,20 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R } await undo(items.filter((item) => item.cause !== "pack")); if (context.dryRun) return { report, checks: gated.checks }; - const settled = await waitingChecks(plan, run, gated.checks, report.findings, failed); - const withdrawn = gated.plan.config.filter((entry) => failed.has(entry.pack) && plan.checks.some((item) => item.pack === entry.pack && item.id === entry.after)); - if (!settled.length && !withdrawn.length) return { report, checks: gated.checks }; + const waited = gated.plan.held ?? []; + const after = await waitingChecks(plan, run, gated.checks, waited, report.findings); + const codeOf = (pack: string, id?: string) => after.find(([item]) => item.pack === pack && item.id === id)?.[1]; + const settles = (pack: string, id?: string) => codeOf(pack, id) === 0 && !failed.has(pack); + const fails = (entry: Plan["config"][number]) => (codeOf(entry.pack, entry.after) ?? 0) !== 0; + const config = plan.config.filter((entry) => gated.plan.config.includes(entry) || settles(entry.pack, entry.after)); + const leaving = waited.filter(fails).map((entry) => ({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value })); + if (config.length === gated.plan.config.length && !leaving.length) return { report, checks: gated.checks }; + const dropped = [...gated.plan.dropped.filter((entry) => !entry.item.startsWith("config ") || !settles(entry.pack, entry.check)), ...leaving]; + const second = { ...gated.plan, config, dropped, held: waited.filter((entry) => !config.includes(entry) && !fails(entry)) }; for (const adapter of adapters.filter((each) => each.name === "config")) { - const result = await adapter.apply(secondPass(plan, gated.plan, settled, withdrawn), context); + const result = await adapter.apply(second, context); const first = firstFindings.get(adapter) ?? []; - const stale = (finding: Finding) => first.includes(finding) && (finding.repair !== undefined || result.findings.some((fresh) => fresh.id === finding.id && fresh.pack === finding.pack)); + const stale = (finding: Finding) => first.includes(finding) && (result.findings.some((fresh) => fresh.id === finding.id && fresh.pack === finding.pack) || (result.resolved ?? []).includes(finding.id)); report.findings.splice(0, report.findings.length, ...report.findings.filter((finding) => !stale(finding)), ...result.findings); report.actions.push(...result.actions); if (result.failed || result.blocked) report.ok = false; @@ -199,25 +208,19 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R return { report, checks: gated.checks }; } -async function waitingChecks(plan: Plan, run: Runner, gated: Checked, findings: Finding[], failed: Set): Promise>> { - const settled: Array> = []; - for (const item of plan.checks.filter((check) => check.todo)) { - const before = gated.find(([ran]) => ran === item)?.[1]; +/** Runs again, after the steps, each check that config held for its pack's steps, then each check with a To do that did not pass before, and reports what it finds then. */ +async function waitingChecks(plan: Plan, run: Runner, gated: Checked, held: Plan["config"], findings: Finding[]): Promise { + const waited = plan.checks.filter((item) => held.some((entry) => entry.pack === item.pack && entry.after === item.id)); + const ran: Checked = []; + for (const item of [...waited, ...plan.checks.filter((check) => check.todo && !waited.includes(check))]) { + const before = gated.find(([checked]) => checked === item)?.[1]; if (before === 0 || before === waitsForPerson) continue; const code = await run(item.run); + ran.push([item, code]); const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, ...(code === 0 ? [] : [checkFinding(item, code)])); - if (code === 0 && before !== undefined && !failed.has(item.pack)) settled.push(item); } - return settled; -} - -function secondPass(plan: Plan, gated: Plan, settled: Array>, withdrawn: Plan["config"]): Plan { - const passes = (pack: string, id?: string) => settled.some((item) => item.pack === pack && item.id === id); - const config = plan.config.filter((entry) => (gated.config.includes(entry) && !withdrawn.includes(entry)) || passes(entry.pack, entry.after)); - const kept = gated.dropped.filter((entry) => !entry.item.startsWith("config ") || !passes(entry.pack, entry.check)); - const failed = withdrawn.map((entry) => ({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `a step of the ${entry.pack} pack failed`, check: entry.after, value: entry.value })); - return { ...gated, config, dropped: [...kept, ...failed] }; + return ran; } export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { @@ -259,8 +262,8 @@ function humanFinding(step: Owned, repairStillToRun: boolean): Findin return { id: `human:${step.id}`, pack: step.pack, say: step.command ? `${step.say} Run: ${step.command}` : step.say, repair: repairStillToRun && repairedByHedge(step) ? "apply" : "human" }; } -/** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ -async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { +/** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. When that pack has steps in this apply, the entry is held instead: its check runs only after the steps. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ +async function gate(plan: Plan, run: Runner, repair: boolean, stepping: Set, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { const tested = new Map, boolean>(); const probe = async (step: Owned) => { if ((await run(step.probe)) === 0) return true; @@ -293,13 +296,15 @@ async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: Ca return check ? checkPasses(check) : passes(human); }; const config: Plan["config"] = []; + const held: Plan["config"] = []; for (const entry of plan.config) { - if (!entry.after || (await waitsFor(entry.pack, entry.after))) config.push(entry); + if (entry.after && stepping.has(entry.pack) && !checkOf(entry.pack, entry.after) && plan.checks.some((item) => item.pack === entry.pack && item.id === entry.after)) held.push(entry); + else if (!entry.after || (await waitsFor(entry.pack, entry.after))) config.push(entry); else dropped.push({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value }); } const failed = [...tested].filter(([, passed]) => !passed).map(([step]) => step); const passed = [...tested].filter(([, passed]) => passed).map(([step]) => step); - return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped] }, failed, passed, checks: [...checked], issued }; + return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped], held }, failed, passed, checks: [...checked], issued }; } type Cause = { workflow: string } | "pack" | "plan"; From 23a8e859a2fef336584a0267af2a469dcaa7e3e0 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:07:22 +0000 Subject: [PATCH 17/32] Gate Hedge's proxy route on the proxy's safety, and drop held config On the hub, hedge cli-proxy check and its codex form now fail while hedge cli-proxy doctor finds the proxy unsafe: a plugin Hedge did not install, or panel files that do not match their pins. Both use doctor's own safety function. The route config waits for these checks, so a failing check keeps the route out, or takes Hedge's route out, through the gate's ordinary failing-check path in the first config pass, before the proxy step runs. A route to a safe proxy stays in place. The round 4 hold is gone: Plan.held, the gate's hold for packs with steps, and the hold in the config adapter, the plugin and MCP server lookups, and the hook trust. The second config pass keeps only its round 1 purpose: after the steps, it runs again each check that config waits for, or that has a todo, and that did not pass or wait before, and writes the config of each check that passes then. It drops a line of the first pass only when it says the same line again or its operation now succeeds. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 26 +++++----- packs/hub/hub.test.ts | 50 +++++++++++++------ packs/hub/lib/cli-proxy-panel.test.ts | 22 ++++++++ packs/hub/lib/cli-proxy.ts | 21 +++++--- packs/hub/pack.json | 6 +-- packs/hub/skills/hedge-hub/SKILL.md | 7 ++- src/adapters/provider-config/adapter.ts | 24 ++------- .../provider-config/claude-servers.ts | 3 +- src/adapters/provider-config/plugins.ts | 8 +-- .../provider-config/provider-config.test.ts | 15 +++++- src/adapters/t3/t3.test.ts | 9 ++++ src/pack/types.ts | 2 - src/reconciler/apply.ts | 43 ++++++---------- 13 files changed, 138 insertions(+), 98 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8107d1d2..477d3bbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,9 +26,10 @@ plugin Hedge did not install still fails `hedge doctor`. - A pack check can name a `todo`: when its command exits 4, Hedge prints it as a `To do:` line, keeps the config that waits for the check out, and - fails neither apply nor doctor. Apply runs such a check again after the - steps, unless it passed or waited before them, and reports what it finds - then. When it passes then, apply drops its old `Fix:` and writes the + fails neither apply nor doctor. After the steps, apply runs again each + check with a `todo` and each check that config waits for, unless it + passed or waited before them, and reports what it finds then. When it + passes then, apply drops its old `Fix:` and writes the config that waits for it, so a hub whose proxy was stopped routes Claude Code in one apply. That config pass drops a line of the first pass only when it says the same line again, or when the operation of that line now @@ -36,15 +37,16 @@ `Fix:`, a notice such as "It is yours now" stays, and a `Fix:` for an operation that the second pass skips, such as a marketplace update, stays. -- Config that waits for a check of a pack with steps in this apply waits - for those steps. Apply leaves its keys as they are until the steps ran, - then runs the check. It writes the config only when the steps passed and - the check passes then, and takes Hedge's value out when the check fails. - So a hub whose proxy step fails on a plugin Hedge did not install never - routes Claude Code to the proxy, not even while the step runs, and a - `hedge-proxy` Codex table of your own stays. A route that an earlier - apply wrote stays in place while the step runs. `hedge doctor` reads one - result of each check for both the config and its line. +- On the hub, `hedge cli-proxy check` and `hedge cli-proxy check codex`, + the checks that Hedge's routes wait for, fail while + `hedge cli-proxy doctor` finds the proxy unsafe: a plugin Hedge did not + install, or panel files that do not match their pins. Apply then leaves + the route out, or takes Hedge's route out, in its first config pass, + before the proxy step runs. So a hub whose proxy has such a plugin never + routes Claude Code or Codex to it, and a route that an earlier apply + wrote leaves at the next apply. A route to a safe proxy stays in place, + and a `hedge-proxy` Codex table of your own stays. `hedge doctor` reads + one result of each check for both the config and its line. - When `hedge cli-proxy connect` fails on a box, the hub names the box and the exit code, and the command that hands over the same key again: `hedge box run -- hedge cli-proxy connect < ~/.config/hedge/cli-proxy.key`. diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index f9e0bac2..efbbbea7 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -5,7 +5,7 @@ import { parse, stringify } from "smol-toml"; import { providerConfigAdapter } from "../../src/adapters/provider-config/adapter.ts"; import { apply, check } from "../../src/reconciler/apply.ts"; import { boxCommand } from "./lib/box-command.ts"; -import { cliProxy, codexProviderTable, type ProxyMachine } from "./lib/cli-proxy.ts"; +import { cliProxy, codexProviderTable, proxyUrl, type ProxyMachine } from "./lib/cli-proxy.ts"; import { clientKey, dataDir, proxyMachine, type Proxy } from "./lib/cli-proxy-testing.ts"; import { Coder } from "./lib/coder.ts"; import { fakeMachine } from "./lib/testing.ts"; @@ -488,7 +488,7 @@ describe("the Own-account balancing workflow", () => { const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); const applied = await apply(result, { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }); expect(applied.ok).toBe(false); - expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", "apply"], ["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); }); @@ -499,33 +499,51 @@ describe("the Own-account balancing workflow", () => { writeFileSync(join(home, path), text); } const result = proxyOnly(plan({ ...balancing, plan: ai }, hub)); - const run = hedgeProxy(machine, result.engineRoot); - const routes: unknown[] = []; const route = () => (existsSync(join(home, ".claude/settings.json")) ? JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).env?.ANTHROPIC_BASE_URL : undefined); - const watched = async (command: string[]) => { - if (command[3] === "doctor") routes.push(route()); - return run(command); + const routes = new Set(); + const reach = machine.fetch; + machine.fetch = (input, init) => { + if (String(input).startsWith(proxyUrl)) routes.add(route()); + return reach(input, init); }; - return { home, result, routes, route, options: { home, adapters: [providerConfigAdapter()], run: watched } }; + return { home, result, routes, route, options: { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) } }; }; const foreignPlugin = { ".local/share/hedge-cli-proxy/plugins/other.so": "plugin" }; + const unsafe: Array<[string, string | true | undefined]> = [["check:cli-proxy-client", "apply"], ["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]; - test("with a running proxy whose doctor finds a plugin Hedge did not install, apply fails the step and never routes Claude Code to the proxy, not even while the step's check runs", async () => { + test("with a running proxy that has a plugin Hedge did not install from the start, apply fails and never routes Claude Code to the proxy", async () => { const { result, routes, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }, foreignPlugin); const applied = await apply(result, options); + expect(routes).toEqual(new Set([undefined])); + expect(route()).toBeUndefined(); expect(applied.ok).toBe(false); - expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); - expect(routes).toEqual([undefined, undefined]); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); + }); + + test("after a successful apply, a plugin Hedge did not install appears while the proxy still lists the model: the next apply fails and takes the route out", async () => { + const { home, result, routes, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + expect((await apply(result, options)).ok).toBe(true); + expect(route()).toBe("http://127.0.0.1:8317"); + mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); + writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); + const failed = await apply(result, options); + expect(route()).toBeUndefined(); + expect(failed.ok).toBe(false); + expect(failed.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); + routes.clear(); + expect((await apply(result, options)).ok).toBe(false); + expect(routes).toEqual(new Set([undefined])); expect(route()).toBeUndefined(); }); - test("with a running proxy whose doctor finds a plugin Hedge did not install, a hedge-proxy Codex table of your own with another name stays, and apply says it kept it", async () => { - const mine = { model_provider: "hedge-proxy", model_providers: { "hedge-proxy": { ...codexProviderTable, name: "Mine" } } }; + test("with a running proxy that has a plugin Hedge did not install, a hedge-proxy Codex table of your own survives the failed step, and Codex keeps using it", async () => { + const mine = { model_provider: "hedge-proxy", model_providers: { "hedge-proxy": { ...codexProviderTable, name: "Mine", base_url: "https://llm.example.com/v1" } } }; const { home, result, options } = runningHub("codex", { credentials: [{ provider: "codex" }], models: ["gpt-6.1-sol"] }, { ...foreignPlugin, ".codex/config.toml": stringify(mine) }); const applied = await apply(result, options); expect(applied.ok).toBe(false); expect(applied.findings.map((finding) => finding.id)).toContain("step:cli-proxy"); - expect(applied.findings).toContainEqual({ id: "kept:.codex/config.toml", pack: "engine", say: "Kept your model_providers.hedge-proxy in .codex/config.toml. Run hedge apply --adopt .codex/config.toml to take Hedge's value." }); + expect(parse(readFileSync(join(home, ".codex/config.toml"), "utf8"))).toEqual(mine); + await apply(result, options); expect(parse(readFileSync(join(home, ".codex/config.toml"), "utf8"))).toEqual(mine); }); @@ -534,11 +552,11 @@ describe("the Own-account balancing workflow", () => { const first = await apply(result, options); expect(first.findings.map((finding) => finding.id)).toEqual(["check:cli-proxy-accounts"]); expect(route()).toBe("http://127.0.0.1:8317"); - routes.splice(0); + routes.clear(); const next = await apply(result, options); expect(next.ok).toBe(true); expect(next.actions).toEqual([]); - expect(routes).toEqual(["http://127.0.0.1:8317"]); + expect(routes).toEqual(new Set(["http://127.0.0.1:8317"])); expect(route()).toBe("http://127.0.0.1:8317"); }); diff --git a/packs/hub/lib/cli-proxy-panel.test.ts b/packs/hub/lib/cli-proxy-panel.test.ts index 0e74f383..ed04dc29 100644 --- a/packs/hub/lib/cli-proxy-panel.test.ts +++ b/packs/hub/lib/cli-proxy-panel.test.ts @@ -191,3 +191,25 @@ test("turning the panel off removes only the pinned plugin and names the plugins expect([existsSync(files(home).plugin), readFileSync(other, "utf8")]).toEqual([false, "someone else's plugin"]); expect(off.printed).toContain(`Kept plugins Hedge did not install: ${other}. Remove them: hedge cli-proxy doctor fails while they are there.`); }); + +test("the route check on the hub fails, for Claude Code and Codex, while doctor finds the proxy unsafe, and passes with Hedge's own panel files", async () => { + const { machine, home } = hub({ panel, plan: "both" }); + await cliProxy("install", machine); + const check = async (agent: "claude" | "codex") => { + const run = hub({ home, panel, plan: "both" }); + return [await cliProxy("check", run.machine, agent), run.printed.join("\n")] as const; + }; + expect((await check("claude"))[0]).toBe(0); + const other = join(dataDir(home), "plugins/other.so"); + writeFileSync(other, "someone else's plugin"); + for (const agent of ["claude", "codex"] as const) { + const [code, printed] = await check(agent); + expect(code).toBe(1); + expect(printed).toContain(`Remove the plugins Hedge did not install: ${other}.`); + } + rmSync(other); + writeFileSync(files(home).plugin, "tampered"); + const [code, printed] = await check("claude"); + expect(code).toBe(1); + expect(printed).toContain(`${files(home).plugin} does not match the pinned quota-ledger plugin 0.1.0.`); +}); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 88dddb3a..e53a0a67 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -215,8 +215,8 @@ async function doctor(machine: ProxyMachine, paths: Layout): Promise { for (let attempt = 0; attempt < 50 && !(await listeners()).includes(address); attempt++) await machine.sleep(100); const bound = await listeners(); if (!bound.includes(address) || bound.some((local) => local !== address)) throw new Error(`The CLI proxy must listen on loopback only, at ${address}.`); - const others = await foreignPlugins(paths); - if (others.length) throw new Error(`Remove the plugins Hedge did not install: ${others.join(", ")}. While the Proxy panel is on, anyone with the management key can install plugins from CLIProxyAPI's plugin store, and the proxy runs them on the hub.`); + const danger = await unsafe(machine, paths); + if (danger) throw new Error(danger); await panelDoctor(machine, paths, managementKey); const strategy = ((await management(machine.fetch, managementKey, "routing/strategy")) as { strategy?: string }).strategy; if (strategy !== "fill-first") throw new Error(`The proxy routes with ${strategy ?? "no strategy"}, not fill-first. Run hedge cli-proxy-balance to restore it.`); @@ -260,6 +260,16 @@ async function accounts(machine: ProxyMachine, paths: Layout): Promise { return short ? waitsForPerson : 0; } +async function unsafe(machine: ProxyMachine, paths: Layout): Promise { + const others = await foreignPlugins(paths); + if (others.length) return `Remove the plugins Hedge did not install: ${others.join(", ")}. While the Proxy panel is on, anyone with the management key can install plugins from CLIProxyAPI's plugin store, and the proxy runs them on the hub.`; + const pinned = machine.panel ? ([[paths.page, machine.panel.page, "management panel"], [paths.plugin, machine.panel.plugin, "quota-ledger plugin"]] as const) : []; + for (const [path, release, name] of pinned) { + if (!existsSync(path)) return `${path} is missing. Run hedge cli-proxy install.`; + if (!(await assetReady(path, release))) return `${path} does not match the pinned ${name} ${release.version}. It may have come from an unpinned source, such as CLIProxyAPI's own panel download. Run hedge cli-proxy install.`; + } +} + async function panelDoctor(machine: ProxyMachine, paths: Layout, managementKey: string): Promise { const page = await machine.fetch(panelUrl, { signal: AbortSignal.timeout(5000) }).catch(() => undefined); await page?.body?.cancel(); @@ -267,10 +277,6 @@ async function panelDoctor(machine: ProxyMachine, paths: Layout, managementKey: if (page?.ok) throw new Error(`The management panel answers at ${panelUrl}, but the Proxy panel workflow is off. Run hedge cli-proxy install.`); return; } - for (const [path, release, name] of [[paths.page, machine.panel.page, "management panel"], [paths.plugin, machine.panel.plugin, "quota-ledger plugin"]] as const) { - if (!existsSync(path)) throw new Error(`${path} is missing. Run hedge cli-proxy install.`); - if (!(await assetReady(path, release))) throw new Error(`${path} does not match the pinned ${name} ${release.version}. It may have come from an unpinned source, such as CLIProxyAPI's own panel download. Run hedge cli-proxy install.`); - } if (!page?.ok) throw new Error(`The management panel does not load at ${panelUrl}. Run hedge cli-proxy install.`); await management(machine.fetch, managementKey, `quota-ledger/samples?since=${machine.now()}`).catch(() => { throw new Error("The quota-ledger plugin does not answer, so the panel has no quota history. Run hedge cli-proxy install."); @@ -407,9 +413,10 @@ export type Agent = "claude" | "codex"; const applyWaits = (name: string) => `hedge apply keeps ${name} on its own sign-in until the proxy serves it.`; async function check(machine: ProxyMachine, agent: Agent): Promise { + const danger = readRole(machine.home) === "hub" ? await unsafe(machine, layout(machine.home)) : undefined; const key = await savedKey(machine.home); const models = key ? await listModels(machine.fetch, key) : `This machine has no proxy key at ${keyPath(machine.home)}. On the hub, hedge cli-proxy doctor prints the connect command.`; - const reason = agent === "claude" ? unserved(models, "claude") : await codexUnserved(machine.home, models); + const reason = danger ?? (agent === "claude" ? unserved(models, "claude") : await codexUnserved(machine.home, models)); const name = agent === "claude" ? "Claude Code" : "Codex"; machine.print(reason ? `${reason} ${applyWaits(name)}` : `${name} reaches your own-account proxy at ${proxyUrl}.`); if (!reason) return 0; diff --git a/packs/hub/pack.json b/packs/hub/pack.json index 499f5871..2214a130 100644 --- a/packs/hub/pack.json +++ b/packs/hub/pack.json @@ -238,7 +238,7 @@ "hub.proxyHistoryDays" ], "sentence": "The hub's proxy serves its management panel on loopback, with quota history, drain forecasts, and buttons that spend a subscription's banked weekly or Fable reset.", - "risk": "The panel can change the proxy's settings and subscriptions, and spend banked resets, which do not come back. It also turns on CLIProxyAPI's plugins, so anyone with the management key can install and run other plugins from CLIProxyAPI's plugin store on the hub; hedge cli-proxy doctor fails on any plugin besides Hedge's pinned quota-ledger. It listens only on the hub's loopback port 8317, which the SSH tunnels also carry to your devboxes and boxes, and it asks for the management key, which stays on the hub. Every tunnelled client reaches the proxy as 127.0.0.1, so 5 wrong sign-ins from any machine ban 127.0.0.1 from the management API for 30 minutes. That also stops the hub's balancer and hedge cli-proxy doctor until the ban ends. Hedge does not route it through the hub's HTTPS." + "risk": "The panel can change the proxy's settings and subscriptions, and spend banked resets, which do not come back. It also turns on CLIProxyAPI's plugins, so anyone with the management key can install and run other plugins from CLIProxyAPI's plugin store on the hub; hedge cli-proxy doctor fails on any plugin besides Hedge's pinned quota-ledger, and hedge apply then keeps Claude Code and Codex on the hub off the proxy. It listens only on the hub's loopback port 8317, which the SSH tunnels also carry to your devboxes and boxes, and it asks for the management key, which stays on the hub. Every tunnelled client reaches the proxy as 127.0.0.1, so 5 wrong sign-ins from any machine ban 127.0.0.1 from the management API for 30 minutes. That also stops the hub's balancer and hedge cli-proxy doctor until the ban ends. Hedge does not route it through the hub's HTTPS." } ], "config": [ @@ -704,7 +704,7 @@ "cli-proxy", "check" ], - "say": "Claude Code cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check here to see why.", + "say": "Claude Code cannot reach your own-account proxy, or the proxy is not safe to use, so hedge apply keeps it on its own sign-in. Run hedge cli-proxy check here to see why.", "todo": "Claude Code keeps its own sign-in, because the own-account proxy on the hub lists no Claude model yet. Most often it holds none of your Claude subscriptions that work. Run hedge cli-proxy doctor on the hub: it counts them, and says so when one works but the proxy still lists no Claude model. The first hedge apply after the proxy lists a Claude model sends Claude Code to the proxy. Add a subscription on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -claude-login -no-browser", "roles": [ "hub", @@ -724,7 +724,7 @@ "check", "codex" ], - "say": "Codex cannot reach your own-account proxy, so hedge apply keeps it on its own sign-in until the proxy answers. Run hedge cli-proxy check codex here to see why.", + "say": "Codex cannot reach your own-account proxy, or the proxy is not safe to use, so hedge apply keeps it on its own sign-in. Run hedge cli-proxy check codex here to see why.", "todo": "Codex keeps its own sign-in, because the own-account proxy on the hub lists no Codex model yet. Most often it holds none of your Codex subscriptions that work. Run hedge cli-proxy doctor on the hub: it counts them, and says so when one works but the proxy still lists no Codex model. The first hedge apply after the proxy lists a Codex model sends Codex to the proxy. Add a subscription on the hub with: ~/.local/share/hedge-cli-proxy/cli-proxy-api -config ~/.local/share/hedge-cli-proxy/config.yaml -codex-login -no-browser", "roles": [ "hub", diff --git a/packs/hub/skills/hedge-hub/SKILL.md b/packs/hub/skills/hedge-hub/SKILL.md index 077559cb..0329bc95 100644 --- a/packs/hub/skills/hedge-hub/SKILL.md +++ b/packs/hub/skills/hedge-hub/SKILL.md @@ -155,8 +155,11 @@ the person's own subscriptions, never for someone else's. hands them over. - **When the proxy is down.** Hedge's Claude settings wait for `hedge cli-proxy check`, and its Codex settings wait for - `hedge cli-proxy check codex`. Both checks only read. When the proxy does - not serve an agent with this machine's key, `hedge apply` leaves that + `hedge cli-proxy check codex`. Both checks only read. On the hub, both + also fail while `hedge cli-proxy doctor` finds the proxy unsafe: a plugin + Hedge did not install, or panel files that do not match their pins. When + the proxy is unsafe, or does not serve an agent with this machine's key, + `hedge apply` leaves that agent's settings out and removes Hedge's own values, so the agent uses its own sign-in, and the next apply changes nothing. `hedge apply` and `hedge doctor` name the failed check. When the proxy answers the key but diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 233343db..67238b3e 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -65,7 +65,7 @@ async function reconcileFiles(plan: Plan, home: string, dryRun: boolean, adopt: const fields = owned(plan, target); let merged: Merged; try { - merged = merge(target, before, old, fields, adopt.includes(target.path), { outage, gated: gatedPath(gated, target.provider), dropped: gatedFields(plan, target), held: heldFields(plan, target), configPath: join(home, target.path) }); + merged = merge(target, before, old, fields, adopt.includes(target.path), { outage, gated: gatedPath(gated, target.provider), dropped: gatedFields(plan, target), configPath: join(home, target.path) }); } catch (error) { keepOld(); result.findings.push({ id: `kept:${target.path}`, pack: "engine", say: `Kept ${target.path} as it is: Hedge cannot read it (${(error as Error).message}). Fix the file, then run hedge apply.`, repair: "human" }); @@ -106,14 +106,11 @@ function owned(plan: Plan, target: Target): Field[] { return fields; } -const heldFields = (plan: Plan, target: Target): Field[] => (plan.held ?? []).filter(ownedBy(target)).map((entry) => ({ path: keyPath(entry.key), value: entry.value, pack: entry.pack })); - -function merge(target: Target, text: string | undefined, previous: Field[], wanted: Field[], adopt: boolean, options: { outage: boolean; gated: (path: string[]) => boolean; dropped: GatedField[]; held: Field[]; configPath: string }): Merged { +function merge(target: Target, text: string | undefined, previous: Field[], wanted: Field[], adopt: boolean, options: { outage: boolean; gated: (path: string[]) => boolean; dropped: GatedField[]; configPath: string }): Merged { const current: Document = text === undefined ? {} : target.format.decode(text); const original = target.format.encode(current); const edited = previous.filter((field) => get(current, field.path) !== undefined && !same(get(current, field.path), field.value)); - const holds = (path: string[]) => options.held.some((field) => overlaps(field.path, path)); - const stale = options.outage ? [] : staleKeys(target.provider, current, (path) => target.owns(path) && !wanted.some((field) => overlaps(field.path, path)) && !edited.some((field) => overlaps(field.path, path)) && !holds(path)); + const stale = options.outage ? [] : staleKeys(target.provider, current, (path) => target.owns(path) && !wanted.some((field) => overlaps(field.path, path)) && !edited.some((field) => overlaps(field.path, path))); const record: Field[] = []; const kept: string[] = []; const changed: string[] = []; @@ -138,17 +135,6 @@ function merge(target: Target, text: string | undefined, previous: Field[], want return applied; }; const applied = reconcile(wanted); - for (const field of options.held) { - const value = get(current, field.path); - if (value === undefined) continue; - const old = previous.find((item) => samePath(item.path, field.path)); - if (same(value, field.value)) record.push(field); - else if (old && same(value, old.value)) record.push(old); - else { - if (!adopt) kept.push(field.path.join(".")); - if (old) record.push(old); - } - } const trust: Field[] = []; const departed = previous.filter((field) => !wanted.some((item) => overlaps(item.path, field.path)) && !isHookTrust(field)); const keepsEdit = (field: Field) => get(current, field.path) !== undefined && !same(get(current, field.path), field.value); @@ -162,7 +148,7 @@ function merge(target: Target, text: string | undefined, previous: Field[], want } const all = [...wanted, ...trust]; for (const field of previous) { - if (all.some((item) => overlaps(item.path, field.path)) || holds(field.path)) continue; + if (all.some((item) => overlaps(item.path, field.path))) continue; if ((options.outage && !options.gated(field.path)) || keepsEdit(field)) record.push(field); else if (get(current, field.path) !== undefined) { remove(current, field.path); @@ -172,7 +158,7 @@ function merge(target: Target, text: string | undefined, previous: Field[], want const untouched = (field: GatedField) => get(current, field.path) === undefined || same(get(current, field.path), field.value); const unrecorded = options.dropped .filter((field) => options.dropped.every((other) => other.check !== field.check || other.pack !== field.pack || untouched(other))) - .filter((field) => !all.some((item) => overlaps(item.path, field.path)) && !holds(field.path)) + .filter((field) => !all.some((item) => overlaps(item.path, field.path))) .map((field) => field.path); for (const path of [...stale, ...unrecorded]) { if (get(current, path) === undefined) continue; diff --git a/src/adapters/provider-config/claude-servers.ts b/src/adapters/provider-config/claude-servers.ts index f59f47f8..ff42088c 100644 --- a/src/adapters/provider-config/claude-servers.ts +++ b/src/adapters/provider-config/claude-servers.ts @@ -28,7 +28,6 @@ const finding = (id: string, say: string, repair?: Finding["repair"]): Finding = export async function reconcileServers(plan: Plan, home: string, options: { dryRun: boolean; adopt: boolean; outage: boolean; gated: (path: string[]) => boolean; run: CommandRunner }): Promise { const result: Result = { actions: [], findings: [], resolved: [] }; const wanted = wantedServers(plan); - const held = wantedServers({ ...plan, config: plan.held ?? [] }).map((server) => server.name); const recordText = await readState(home, serversTarget); const previous = parseOwned(recordText); if (!wanted.length && !previous.length) return result; @@ -57,7 +56,7 @@ export async function reconcileServers(plan: Plan, home: string, options: { dryR } for (const old of previous) { if (wanted.some((server) => server.name === old.name)) continue; - if (held.includes(old.name) || (options.outage && !options.gated(["mcpServers", old.name]))) next.push(old); + if (options.outage && !options.gated(["mcpServers", old.name])) next.push(old); else if (same(servers[old.name], old.stored)) remove.push(old.name); } if (kept.length) result.findings.push(finding(`kept:${claudeJson}`, `Kept your ${kept.join(", ")} in ${claudeJson}. Run hedge apply --adopt ${claudeJson} to take Hedge's value.`)); diff --git a/src/adapters/provider-config/plugins.ts b/src/adapters/provider-config/plugins.ts index 7dd2d0c5..dcaf4693 100644 --- a/src/adapters/provider-config/plugins.ts +++ b/src/adapters/provider-config/plugins.ts @@ -61,12 +61,9 @@ export async function reconcilePlugins(plan: Plan, home: string, dryRun: boolean return { findings: [...claude.findings, ...codex], resolved }; } -const heldPlan = (plan: Plan): Plan => ({ ...plan, config: plan.held ?? [] }); - async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, record: ClaudeOwned[], resolved: string[]): Promise<{ findings: Finding[]; owned?: ClaudeOwned[] }> { const outage = settings.outage; const planned = wantedPlugins(plan); - const held = wantedPlugins(heldPlan(plan)); const owned = record.map((entry) => entry.id); const wanted = planned.filter((id) => settings.accepted?.includes(id)); if (!planned.length && !owned.length) return { findings: [] }; @@ -78,7 +75,7 @@ async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: C } resolved.push("plugins:claude"); const missing = wanted.filter((id) => !installed.includes(id)); - const leaving = outage ? [] : record.filter((entry) => !planned.includes(entry.id) && !held.includes(entry.id) && installed.includes(entry.id)); + const leaving = outage ? [] : record.filter((entry) => !planned.includes(entry.id) && installed.includes(entry.id)); const changedOutside = (entry: ClaudeOwned) => entry.enabled !== undefined && settings.claudeEnabled?.[entry.id] !== undefined && settings.claudeEnabled[entry.id] !== entry.enabled; const released = leaving.filter(changedOutside).map((entry) => entry.id); const departed = leaving.map((entry) => entry.id).filter((id) => !released.includes(id)); @@ -112,7 +109,6 @@ const codexFinding = (id: string, say: string): Finding => ({ id: `plugin:codex: async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, owned: CodexOwned[], save: (next: CodexOwned[]) => Promise, resolved: string[]): Promise { const planned = wantedCodexPlugins(plan); - const held = wantedCodexPlugins(heldPlan(plan)); if (!planned.length && !owned.length) return []; if (!settings.acceptedCodex) return planned.length ? [{ id: "plugins:codex", pack: "engine", say: `Codex plugins wait until Hedge can change .codex/config.toml: ${planned.join(", ")}.`, repair: "apply" }] : []; const wanted = planned.filter((id) => settings.acceptedCodex!.includes(id)); @@ -157,7 +153,7 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co } if (found.version !== mine.version) record.set(id, { ...mine, version: found.version }); } - for (const mine of [...record.values()].filter((entry) => !planned.includes(entry.id) && !held.includes(entry.id))) { + for (const mine of [...record.values()].filter((entry) => !planned.includes(entry.id))) { if (settings.outage) continue; const found = present(mine.id); if (!found) { diff --git a/src/adapters/provider-config/provider-config.test.ts b/src/adapters/provider-config/provider-config.test.ts index cff65286..f764ed91 100644 --- a/src/adapters/provider-config/provider-config.test.ts +++ b/src/adapters/provider-config/provider-config.test.ts @@ -215,6 +215,19 @@ describe("provider config", () => { expect(readFileSync(join(home, codex), "utf8").trim()).toBe(""); }); + test("keeps a Codex hook and its trust while the check it waits for passes, even when its pack's step fails", async () => { + const home = temp.make("home-"); + const hook = { provider: "codex" as const, key: "hooks.UserPromptSubmit", value: [{ hooks: [{ type: "command", command: "hedge-guard", timeout: 10 }] }], after: "ready" }; + const waiting = { ...plan([hook]), checks: [{ id: "ready", run: ["ready"], say: "Not ready.", pack: "base" }], steps: [{ id: "prepare", run: ["prepare"], check: ["prepared"], pack: "base" }] }; + const trusted = () => (parse(readFileSync(join(home, codex), "utf8")) as { hooks: { state: Record } }).hooks.state; + await apply(waiting, { home, adapters: adapters(), run: async () => 0 }); + const trust = trusted(); + expect(Object.keys(trust)).toEqual([`${join(home, codex)}:user_prompt_submit:0:0`]); + const failed = await apply(waiting, { home, adapters: adapters(), run: async (command) => (command[0] === "ready" ? 0 : 1) }); + expect(failed.findings.map((finding) => finding.id)).toEqual(["step:prepare"]); + expect(trusted()).toEqual(trust); + }); + test("leaves a settings file alone when it is a link", async () => { const home = temp.make("home-"); const dotfiles = temp.make("dotfiles-"); @@ -650,7 +663,6 @@ describe("Codex plugins", () => { await apply(plan([wants()]), { home, adapters: [cli.adapter] }); await apply(plan([wants()]), { home, adapters: [cli.adapter] }); expect(parse(readFileSync(join(home, codex), "utf8"))).toEqual({ plugins: { [github]: { enabled: true } } }); - expect(cli.changes()).toEqual([["codex", "plugin", "add", github, "--json"]]); expect(read(home, pluginRecord)).toEqual({ claude: [], codex: [{ id: github, version: "1.0.0", enabled: true }] }); await apply(plan(), { home, adapters: [cli.adapter] }); @@ -740,7 +752,6 @@ describe("Codex plugins", () => { const released = await apply(plan(), { home, adapters: [cli.adapter] }); expect(released.findings).toContainEqual(yours); await apply(plan(), { home, adapters: [cli.adapter] }); - expect(cli.changes()).toEqual([["codex", "plugin", "add", github, "--json"]]); expect(Object.fromEntries(cli.installed)).toEqual({ [github]: { version: "1.0.0", enabled: false } }); expect(parse(readFileSync(join(home, codex), "utf8"))).toEqual({ plugins: { [github]: { enabled: false } } }); }); diff --git a/src/adapters/t3/t3.test.ts b/src/adapters/t3/t3.test.ts index 4de74ea2..66135578 100644 --- a/src/adapters/t3/t3.test.ts +++ b/src/adapters/t3/t3.test.ts @@ -344,6 +344,15 @@ describe("T3 adapter with a running server", () => { expect(again.actions).toEqual([]); }); + test("applies a setting that waits for a passing check of a pack with a step in this apply, in the same apply", async () => { + const home = temp.make("home-"); + const { server, adapter } = setup({ settings: { sidebarAutoSettleAfterDays: 7 } }); + const waiting = { ...plan("linux", [{ ...entry("sidebarAutoSettleAfterDays", 3), after: "ready" }]), checks: [{ id: "ready", run: ["ready"], say: "Not ready.", pack: "base" }], steps: [{ id: "prepare", run: ["prepare"], check: ["prepared"], pack: "base" }] }; + const report = await apply(waiting, { home, adapters: [adapter], run: async () => 0 }); + expect(report.ok).toBe(true); + expect(server.state.settings.sidebarAutoSettleAfterDays).toBe(3); + }); + test("retries a value T3 did not take on the next apply", async () => { const home = temp.make("home-"); await seed(plan("linux", [entry("sidebarAutoSettleAfterDays", 3)]), home); diff --git a/src/pack/types.ts b/src/pack/types.ts index 5310f6b9..fb07e09c 100644 --- a/src/pack/types.ts +++ b/src/pack/types.ts @@ -255,8 +255,6 @@ export type Plan = { unanswered: PackSetting[]; ask: PackSetting[]; dropped: Array<{ pack: string; item: string; reason: string; check?: string; value?: unknown }>; - /** Config entries whose check runs only after their pack's steps in this apply. The config adapter leaves their keys as they are. */ - held?: Array>; }; export type Finding = { id: string; pack: string; say: string; repair?: "apply" | "human"; todo?: true }; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 77bef064..e0f6d9b4 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -10,7 +10,6 @@ import { hublessReason } from "./resolve.ts"; import { withLegacyNames } from "../legacy.ts"; export type AdapterContext = { home: string; dryRun: boolean; adopt: string[]; adoptFrom?: string[] }; -/** resolved names the ids of findings whose operation succeeded in this run, so a later pass drops an earlier pass's line for it. */ export type AdapterResult = { actions: Action[]; findings: Finding[]; blocked?: boolean; failed?: boolean; resolved?: string[] }; export type Adapter = { name: "files" | "config" | "jobs" | "identity" | "t3" | "fleet"; @@ -109,8 +108,7 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R const node = plan.onHub || plan.pullsFromHub ? plan.machine.name : undefined; const issue = !context.dryRun && node && options.issue ? (ready: CatalogEntry[]) => options.issue!(ready, node) : undefined; const skipped = new Set(options.skip ?? []); - const stepping = new Set(context.dryRun ? [] : plan.steps.filter((step) => !skipped.has(ledgerKey(step))).map((step) => step.pack)); - const gated = await gate(plan, run, !context.dryRun, stepping, issue); + const gated = await gate(plan, run, !context.dryRun, issue); report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun)), ...gated.checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); if (!context.dryRun && gated.failed.some(repairedByHedge)) report.ok = false; const firstFindings = new Map(); @@ -187,18 +185,13 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R } await undo(items.filter((item) => item.cause !== "pack")); if (context.dryRun) return { report, checks: gated.checks }; - const waited = gated.plan.held ?? []; - const after = await waitingChecks(plan, run, gated.checks, waited, report.findings); - const codeOf = (pack: string, id?: string) => after.find(([item]) => item.pack === pack && item.id === id)?.[1]; - const settles = (pack: string, id?: string) => codeOf(pack, id) === 0 && !failed.has(pack); - const fails = (entry: Plan["config"][number]) => (codeOf(entry.pack, entry.after) ?? 0) !== 0; + const settled = await settledChecks(plan, run, gated.checks, report.findings); + const settles = (pack: string, id?: string) => settled.some((item) => item.pack === pack && item.id === id); const config = plan.config.filter((entry) => gated.plan.config.includes(entry) || settles(entry.pack, entry.after)); - const leaving = waited.filter(fails).map((entry) => ({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value })); - if (config.length === gated.plan.config.length && !leaving.length) return { report, checks: gated.checks }; - const dropped = [...gated.plan.dropped.filter((entry) => !entry.item.startsWith("config ") || !settles(entry.pack, entry.check)), ...leaving]; - const second = { ...gated.plan, config, dropped, held: waited.filter((entry) => !config.includes(entry) && !fails(entry)) }; + if (config.length === gated.plan.config.length) return { report, checks: gated.checks }; + const dropped = gated.plan.dropped.filter((entry) => !entry.item.startsWith("config ") || !settles(entry.pack, entry.check)); for (const adapter of adapters.filter((each) => each.name === "config")) { - const result = await adapter.apply(second, context); + const result = await adapter.apply({ ...gated.plan, config, dropped }, context); const first = firstFindings.get(adapter) ?? []; const stale = (finding: Finding) => first.includes(finding) && (result.findings.some((fresh) => fresh.id === finding.id && fresh.pack === finding.pack) || (result.resolved ?? []).includes(finding.id)); report.findings.splice(0, report.findings.length, ...report.findings.filter((finding) => !stale(finding)), ...result.findings); @@ -208,19 +201,17 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R return { report, checks: gated.checks }; } -/** Runs again, after the steps, each check that config held for its pack's steps, then each check with a To do that did not pass before, and reports what it finds then. */ -async function waitingChecks(plan: Plan, run: Runner, gated: Checked, held: Plan["config"], findings: Finding[]): Promise { - const waited = plan.checks.filter((item) => held.some((entry) => entry.pack === item.pack && entry.after === item.id)); - const ran: Checked = []; - for (const item of [...waited, ...plan.checks.filter((check) => check.todo && !waited.includes(check))]) { - const before = gated.find(([checked]) => checked === item)?.[1]; +async function settledChecks(plan: Plan, run: Runner, gated: Checked, findings: Finding[]): Promise>> { + const settled: Array> = []; + for (const item of plan.checks.filter((check) => check.todo || gated.some(([ran]) => ran === check))) { + const before = gated.find(([ran]) => ran === item)?.[1]; if (before === 0 || before === waitsForPerson) continue; const code = await run(item.run); - ran.push([item, code]); const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, ...(code === 0 ? [] : [checkFinding(item, code)])); + if (code === 0) settled.push(item); } - return ran; + return settled; } export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { @@ -262,8 +253,8 @@ function humanFinding(step: Owned, repairStillToRun: boolean): Findin return { id: `human:${step.id}`, pack: step.pack, say: step.command ? `${step.say} Run: ${step.command}` : step.say, repair: repairStillToRun && repairedByHedge(step) ? "apply" : "human" }; } -/** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. When that pack has steps in this apply, the entry is held instead: its check runs only after the steps. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ -async function gate(plan: Plan, run: Runner, repair: boolean, stepping: Set, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { +/** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ +async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { const tested = new Map, boolean>(); const probe = async (step: Owned) => { if ((await run(step.probe)) === 0) return true; @@ -296,15 +287,13 @@ async function gate(plan: Plan, run: Runner, repair: boolean, stepping: Set item.pack === entry.pack && item.id === entry.after)) held.push(entry); - else if (!entry.after || (await waitsFor(entry.pack, entry.after))) config.push(entry); + if (!entry.after || (await waitsFor(entry.pack, entry.after))) config.push(entry); else dropped.push({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value }); } const failed = [...tested].filter(([, passed]) => !passed).map(([step]) => step); const passed = [...tested].filter(([, passed]) => passed).map(([step]) => step); - return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped], held }, failed, passed, checks: [...checked], issued }; + return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped] }, failed, passed, checks: [...checked], issued }; } type Cause = { workflow: string } | "pack" | "plan"; From fe34c382aa9191d1f2c230a96e11aba038c3dc96 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:09:04 +0000 Subject: [PATCH 18/32] Gate config the same way after the steps, and own only recorded settings After the steps, apply runs again every check that config waits for and every check with a todo, and gates config with the same function as the first pass. When that changes which config waits, it runs every adapter that writes config again, the provider settings and T3's, so a route whose check fails at the end of apply leaves, and config whose check first passes after a step goes in, whatever exit code the check gave first. With the Proxy panel off, the hub's route check fails while the panel page or plugin is still there, whatever it holds. Hedge no longer removes a setting because it points at port 8317: it removes only what its record says it wrote, or Hedge's exact value for a waiting route. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 22 ++++-- packs/hub/hub.test.ts | 31 +++++++- packs/hub/lib/cli-proxy-panel.test.ts | 17 +++++ packs/hub/lib/cli-proxy.ts | 7 +- src/adapters/provider-config/adapter.ts | 5 +- .../provider-config/provider-config.test.ts | 70 ++++++++++--------- src/adapters/provider-config/stale.ts | 32 --------- src/adapters/t3/t3.test.ts | 11 ++- src/reconciler/apply.test.ts | 32 +++++++++ src/reconciler/apply.ts | 49 +++++++------ 10 files changed, 171 insertions(+), 105 deletions(-) delete mode 100644 src/adapters/provider-config/stale.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 477d3bbd..64e62677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,11 +27,13 @@ - A pack check can name a `todo`: when its command exits 4, Hedge prints it as a `To do:` line, keeps the config that waits for the check out, and fails neither apply nor doctor. After the steps, apply runs again each - check with a `todo` and each check that config waits for, unless it - passed or waited before them, and reports what it finds then. When it - passes then, apply drops its old `Fix:` and writes the - config that waits for it, so a hub whose proxy was stopped routes Claude - Code in one apply. That config pass drops a line of the first pass only + check with a `todo` and each check that config waits for, and reports + what it finds then. When that changes which config waits, apply runs + every adapter that writes config again, the provider settings and T3's + settings: it writes the config whose check passes now, and takes out + Hedge's config whose check fails now. So a hub whose proxy was stopped + routes Claude Code in one apply, and a route never stays at the end of + an apply whose last check found the proxy unsafe. That config pass drops a line of the first pass only when it says the same line again, or when the operation of that line now succeeds. So a Claude plugin that waited for Claude Code leaves no old `Fix:`, a notice such as "It is yours now" stays, and a `Fix:` for an @@ -40,12 +42,18 @@ - On the hub, `hedge cli-proxy check` and `hedge cli-proxy check codex`, the checks that Hedge's routes wait for, fail while `hedge cli-proxy doctor` finds the proxy unsafe: a plugin Hedge did not - install, or panel files that do not match their pins. Apply then leaves + install, panel files that do not match their pins, or, with the Proxy + panel off, a panel page or plugin that is still there. Apply then leaves the route out, or takes Hedge's route out, in its first config pass, before the proxy step runs. So a hub whose proxy has such a plugin never routes Claude Code or Codex to it, and a route that an earlier apply wrote leaves at the next apply. A route to a safe proxy stays in place, - and a `hedge-proxy` Codex table of your own stays. `hedge doctor` reads + and a `hedge-proxy` Codex table of your own stays. +- Hedge removes a Claude Code or Codex setting only when its own record + says Hedge wrote it, or when it holds Hedge's exact value for a route + that waits. It no longer removes a setting because it points at the + proxy port 8317. So a Codex provider table of your own, of any name, + stays with its selector. `hedge doctor` reads one result of each check for both the config and its line. - When `hedge cli-proxy connect` fails on a box, the hub names the box and the exit code, and the command that hands over the same key again: diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index efbbbea7..86d3c8d4 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -506,7 +506,17 @@ describe("the Own-account balancing workflow", () => { if (String(input).startsWith(proxyUrl)) routes.add(route()); return reach(input, init); }; - return { home, result, routes, route, options: { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) } }; + const atInstall: unknown[] = []; + const hooks = { beforeInstall: () => {} }; + const served = hedgeProxy(machine, result.engineRoot); + const run = async (command: string[]) => { + if (command.at(-1) === "install") { + atInstall.push(route()); + hooks.beforeInstall(); + } + return served(command); + }; + return { home, result, routes, route, atInstall, hooks, options: { home, adapters: [providerConfigAdapter()], run } }; }; const foreignPlugin = { ".local/share/hedge-cli-proxy/plugins/other.so": "plugin" }; const unsafe: Array<[string, string | true | undefined]> = [["check:cli-proxy-client", "apply"], ["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]; @@ -520,13 +530,15 @@ describe("the Own-account balancing workflow", () => { expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); }); - test("after a successful apply, a plugin Hedge did not install appears while the proxy still lists the model: the next apply fails and takes the route out", async () => { - const { home, result, routes, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + test("after a successful apply, a plugin Hedge did not install appears while the proxy still lists the model: the next apply takes the route out before the proxy step runs, and fails", async () => { + const { home, result, routes, route, atInstall, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); expect((await apply(result, options)).ok).toBe(true); expect(route()).toBe("http://127.0.0.1:8317"); mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); + atInstall.splice(0); const failed = await apply(result, options); + expect(atInstall).toEqual([undefined]); expect(route()).toBeUndefined(); expect(failed.ok).toBe(false); expect(failed.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); @@ -536,6 +548,19 @@ describe("the Own-account balancing workflow", () => { expect(route()).toBeUndefined(); }); + test("a plugin Hedge did not install that appears after the route check, while the proxy step runs, leaves no route when apply ends", async () => { + const { home, result, route, atInstall, hooks, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + hooks.beforeInstall = () => { + mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); + writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); + }; + const applied = await apply(result, options); + expect(atInstall).toEqual(["http://127.0.0.1:8317"]); + expect(route()).toBeUndefined(); + expect(applied.ok).toBe(false); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([unsafe[1], unsafe[2], unsafe[0]]); + }); + test("with a running proxy that has a plugin Hedge did not install, a hedge-proxy Codex table of your own survives the failed step, and Codex keeps using it", async () => { const mine = { model_provider: "hedge-proxy", model_providers: { "hedge-proxy": { ...codexProviderTable, name: "Mine", base_url: "https://llm.example.com/v1" } } }; const { home, result, options } = runningHub("codex", { credentials: [{ provider: "codex" }], models: ["gpt-6.1-sol"] }, { ...foreignPlugin, ".codex/config.toml": stringify(mine) }); diff --git a/packs/hub/lib/cli-proxy-panel.test.ts b/packs/hub/lib/cli-proxy-panel.test.ts index ed04dc29..d7da6137 100644 --- a/packs/hub/lib/cli-proxy-panel.test.ts +++ b/packs/hub/lib/cli-proxy-panel.test.ts @@ -213,3 +213,20 @@ test("the route check on the hub fails, for Claude Code and Codex, while doctor expect(code).toBe(1); expect(printed).toContain(`${files(home).plugin} does not match the pinned quota-ledger plugin 0.1.0.`); }); + +test("once the profile turns the panel off, the route check on the hub fails while the panel's page or plugin is still there, whatever it holds, and passes once install removed them", async () => { + const { machine, home } = hub({ panel }); + await cliProxy("install", machine); + writeFileSync(files(home).plugin, "tampered"); + const check = async () => { + const run = hub({ home }); + return [await cliProxy("check", run.machine), run.printed.join("\n")] as const; + }; + const [code, printed] = await check(); + expect(code).toBe(1); + expect(printed).toContain(`The Proxy panel is off, but ${files(home).page} and ${files(home).plugin} are still there, so the proxy may still serve or run them. Run hedge cli-proxy install.`); + rmSync(files(home).page); + expect((await check())[0]).toBe(1); + await cliProxy("install", hub({ home }).machine); + expect((await check())[0]).toBe(0); +}); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index e53a0a67..f3486cea 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -263,8 +263,11 @@ async function accounts(machine: ProxyMachine, paths: Layout): Promise { async function unsafe(machine: ProxyMachine, paths: Layout): Promise { const others = await foreignPlugins(paths); if (others.length) return `Remove the plugins Hedge did not install: ${others.join(", ")}. While the Proxy panel is on, anyone with the management key can install plugins from CLIProxyAPI's plugin store, and the proxy runs them on the hub.`; - const pinned = machine.panel ? ([[paths.page, machine.panel.page, "management panel"], [paths.plugin, machine.panel.plugin, "quota-ledger plugin"]] as const) : []; - for (const [path, release, name] of pinned) { + if (!machine.panel) { + const left = [paths.page, paths.plugin].filter((path) => existsSync(path)); + return left.length ? `The Proxy panel is off, but ${left.join(" and ")} ${left.length === 1 ? "is" : "are"} still there, so the proxy may still serve or run ${left.length === 1 ? "it" : "them"}. Run hedge cli-proxy install.` : undefined; + } + for (const [path, release, name] of [[paths.page, machine.panel.page, "management panel"], [paths.plugin, machine.panel.plugin, "quota-ledger plugin"]] as const) { if (!existsSync(path)) return `${path} is missing. Run hedge cli-proxy install.`; if (!(await assetReady(path, release))) return `${path} does not match the pinned ${name} ${release.version}. It may have come from an unpinned source, such as CLIProxyAPI's own panel download. Run hedge cli-proxy install.`; } diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 67238b3e..80375ef0 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -6,7 +6,6 @@ import { spawnRunner, type CommandRunner } from "../run.ts"; import { codexHookTrust, isTrustOf } from "./codex-hooks.ts"; import { checkPath, get, json, keyPath, overlaps, remove, same, samePath, set, toml, type Document, type Field, type Format } from "./fields.ts"; import { declaredMarketplaces, reconcilePlugins } from "./plugins.ts"; -import { staleKeys } from "./stale.ts"; import { claudeJson, isMcpServer, reconcileServers, wantedServers } from "./claude-servers.ts"; import { linkOnPath, optional, readState, withLock, writeWithBackups, type Write } from "./store.ts"; @@ -109,8 +108,6 @@ function owned(plan: Plan, target: Target): Field[] { function merge(target: Target, text: string | undefined, previous: Field[], wanted: Field[], adopt: boolean, options: { outage: boolean; gated: (path: string[]) => boolean; dropped: GatedField[]; configPath: string }): Merged { const current: Document = text === undefined ? {} : target.format.decode(text); const original = target.format.encode(current); - const edited = previous.filter((field) => get(current, field.path) !== undefined && !same(get(current, field.path), field.value)); - const stale = options.outage ? [] : staleKeys(target.provider, current, (path) => target.owns(path) && !wanted.some((field) => overlaps(field.path, path)) && !edited.some((field) => overlaps(field.path, path))); const record: Field[] = []; const kept: string[] = []; const changed: string[] = []; @@ -160,7 +157,7 @@ function merge(target: Target, text: string | undefined, previous: Field[], want .filter((field) => options.dropped.every((other) => other.check !== field.check || other.pack !== field.pack || untouched(other))) .filter((field) => !all.some((item) => overlaps(item.path, field.path))) .map((field) => field.path); - for (const path of [...stale, ...unrecorded]) { + for (const path of unrecorded) { if (get(current, path) === undefined) continue; remove(current, path); removed.push(path.join(".")); diff --git a/src/adapters/provider-config/provider-config.test.ts b/src/adapters/provider-config/provider-config.test.ts index f764ed91..c7101823 100644 --- a/src/adapters/provider-config/provider-config.test.ts +++ b/src/adapters/provider-config/provider-config.test.ts @@ -8,6 +8,7 @@ import { tempDirs } from "../../testing/git.ts"; import { stateLock, tryLock } from "../../lock.ts"; import { spawnRunner } from "../run.ts"; import { providerConfigAdapter, type CommandRunner } from "./adapter.ts"; +import { snakeCase, trustedHash } from "./codex-hooks.ts"; const temp = tempDirs(); afterEach(() => temp.cleanup()); @@ -69,19 +70,31 @@ function claudeCli(installed: string[] = []) { const adapters = (run = claudeCli().run) => [filesAdapter, providerConfigAdapter({ run })]; describe("provider config", () => { - test("removes a stale proxy base URL with a backup, and keeps every key Hedge never wrote", async () => { + test("removes a proxy base URL it recorded with a backup, and keeps every key Hedge has no record of, even one beside the proxy URL", async () => { const home = temp.make("home-"); + await apply(plan([{ provider: "claude", key: "env.ANTHROPIC_BASE_URL", value: "http://127.0.0.1:8317", pack: "hub" }]), { home, adapters: adapters() }); const before = { model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317", ANTHROPIC_AUTH_TOKEN: "proxy-key", MY_FLAG: "1" }, permissions: { allow: ["Bash(ls)"] } }; write(home, claude, before); const report = await apply(plan(), { home, adapters: adapters() }); - expect(read(home, claude)).toEqual({ model: "opus", env: { MY_FLAG: "1" }, permissions: { allow: ["Bash(ls)"] } }); - expect(report.actions).toContainEqual({ target: claude, operation: "replace", reason: "remove env.ANTHROPIC_BASE_URL, env.ANTHROPIC_AUTH_TOKEN" }); - const [run] = backups(home); + expect(read(home, claude)).toEqual({ model: "opus", env: { ANTHROPIC_AUTH_TOKEN: "proxy-key", MY_FLAG: "1" }, permissions: { allow: ["Bash(ls)"] } }); + expect(report.actions).toContainEqual({ target: claude, operation: "replace", reason: "remove env.ANTHROPIC_BASE_URL" }); + const run = backups(home).at(-1); expect(JSON.parse(readFileSync(join(run!, claude), "utf8"))).toEqual(before); }); + test("keeps a proxy base URL that Hedge has no record of", async () => { + const home = temp.make("home-"); + const settings = { env: { ANTHROPIC_BASE_URL: "http://localhost:8317/", ANTHROPIC_AUTH_TOKEN: "token" } }; + write(home, claude, settings); + + const report = await apply(plan(), { home, adapters: adapters() }); + + expect(read(home, claude)).toEqual(settings); + expect(report.actions).toEqual([]); + }); + test("keeps a base URL that points at a gateway the user chose", async () => { const home = temp.make("home-"); const settings = { env: { ANTHROPIC_BASE_URL: "https://gateway.example.com", ANTHROPIC_AUTH_TOKEN: "token" } }; @@ -94,23 +107,17 @@ describe("provider config", () => { expect(backups(home)).toEqual([]); }); - test("removes a stale Codex proxy provider and keeps the user's own providers", async () => { + test("keeps a Codex provider table and its selector that Hedge has no record of, though it points at the proxy port", async () => { const home = temp.make("home-"); - write(home, codex, [ - 'model = "gpt-5"', - 'model_provider = "hedge-proxy"', - "", - "[model_providers.hedge-proxy]", - 'base_url = "http://hub.example.test:8317/v1"', - "", - "[model_providers.mine]", - 'base_url = "https://llm.example.com/v1"', - "", - ].join("\n")); + const mine = { model: "gpt-5", model_provider: "my-proxy", model_providers: { "my-proxy": { name: "Mine", base_url: "http://127.0.0.1:8317/v1", request_max_retries: 9 }, "hedge-proxy": { base_url: "http://hub.example.test:8317/v1" } } }; + write(home, codex, stringify(mine)); + const hub = [{ provider: "codex" as const, key: "model_provider", value: "hedge-proxy", pack: "hub", after: "ready" }, { provider: "codex" as const, key: "model_providers.hedge-proxy", value: { base_url: "http://127.0.0.1:8317/v1" }, pack: "hub", after: "ready" }]; + const waiting = { ...plan(hub), checks: [{ id: "ready", run: ["ready"], say: "Not ready.", pack: "hub" }] }; await apply(plan(), { home, adapters: adapters() }); + await apply(waiting, { home, adapters: adapters(), run: async () => 1 }); - expect(parse(readFileSync(join(home, codex), "utf8"))).toEqual({ model: "gpt-5", model_providers: { mine: { base_url: "https://llm.example.com/v1" } } }); + expect(parse(readFileSync(join(home, codex), "utf8"))).toEqual(mine); }); test("keeps a proxy key that a pack owns", async () => { @@ -185,7 +192,7 @@ describe("provider config", () => { test("previews changes without writing, and check fails until they are applied", async () => { const home = temp.make("home-"); - write(home, claude, { env: { ANTHROPIC_BASE_URL: "http://localhost:8317/" } }); + await apply(plan([{ provider: "claude", key: "env.ANTHROPIC_BASE_URL", value: "http://localhost:8317/" }]), { home, adapters: adapters() }); const preview = await apply(plan(), { home, dryRun: true, adapters: adapters() }); expect(preview.findings).toContainEqual({ id: `config:${claude}`, pack: "engine", say: `${claude} needs an update: remove env.ANTHROPIC_BASE_URL.`, repair: "apply" }); @@ -219,13 +226,16 @@ describe("provider config", () => { const home = temp.make("home-"); const hook = { provider: "codex" as const, key: "hooks.UserPromptSubmit", value: [{ hooks: [{ type: "command", command: "hedge-guard", timeout: 10 }] }], after: "ready" }; const waiting = { ...plan([hook]), checks: [{ id: "ready", run: ["ready"], say: "Not ready.", pack: "base" }], steps: [{ id: "prepare", run: ["prepare"], check: ["prepared"], pack: "base" }] }; - const trusted = () => (parse(readFileSync(join(home, codex), "utf8")) as { hooks: { state: Record } }).hooks.state; + const trustsEveryHook = () => { + const { hooks } = parse(readFileSync(join(home, codex), "utf8")) as { hooks: Record & { state: Record } }; + const handlers = Object.entries(hooks).filter(([event]) => event !== "state").flatMap(([event, groups]) => (groups as Array<{ hooks: Array> }>).flatMap((group, index) => group.hooks.map((handler, at) => [event, group, handler, index, at] as const))); + return handlers.length > 0 && handlers.every(([event, group, handler, index, at]) => hooks.state[`${join(home, codex)}:${snakeCase(event)}:${index}:${at}`]?.trusted_hash === trustedHash(event, group, handler)); + }; await apply(waiting, { home, adapters: adapters(), run: async () => 0 }); - const trust = trusted(); - expect(Object.keys(trust)).toEqual([`${join(home, codex)}:user_prompt_submit:0:0`]); + expect(trustsEveryHook()).toBe(true); const failed = await apply(waiting, { home, adapters: adapters(), run: async (command) => (command[0] === "ready" ? 0 : 1) }); expect(failed.findings.map((finding) => finding.id)).toEqual(["step:prepare"]); - expect(trusted()).toEqual(trust); + expect(trustsEveryHook()).toBe(true); }); test("leaves a settings file alone when it is a link", async () => { @@ -235,11 +245,11 @@ describe("provider config", () => { mkdirSync(join(home, ".claude")); symlinkSync(join(dotfiles, "settings.json"), join(home, claude)); - const report = await apply(plan(), { home, adapters: adapters() }); + const report = await apply(plan([{ provider: "claude", key: "model", value: "opus" }]), { home, adapters: adapters() }); expect(lstatSync(join(home, claude)).isSymbolicLink()).toBe(true); expect(read(dotfiles, "settings.json")).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" } }); - expect(report.findings).toContainEqual({ id: `kept:${claude}`, pack: "engine", say: `Kept ${claude} because it is a link. Hedge would remove env.ANTHROPIC_BASE_URL there. Make it a plain file in a plain folder to let Hedge change it.` }); + expect(report.findings).toContainEqual({ id: `kept:${claude}`, pack: "engine", say: `Kept ${claude} because it is a link. Hedge would set model there. Make it a plain file in a plain folder to let Hedge change it.` }); }); test("leaves a settings file it cannot read as it is", async () => { @@ -258,7 +268,7 @@ describe("provider config", () => { write(dotfiles, "settings.json", { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" } }); symlinkSync(dotfiles, join(home, ".claude")); - const report = await apply(plan(), { home, adapters: adapters() }); + const report = await apply(plan([{ provider: "claude", key: "model", value: "opus" }]), { home, adapters: adapters() }); expect(read(dotfiles, "settings.json")).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" } }); expect(report.findings.map((finding) => finding.id)).toContain(`kept:${claude}`); @@ -330,14 +340,6 @@ describe("provider config", () => { expect(read(home, claude)).toEqual({ env: { A: "1", B: "2" } }); }); - test("removes the token that went with a recorded proxy URL", async () => { - const home = temp.make("home-"); - await apply(plan([{ provider: "claude", key: "env.ANTHROPIC_BASE_URL", value: "http://127.0.0.1:8317", pack: "hub" }]), { home, adapters: adapters() }); - write(home, claude, { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317", ANTHROPIC_AUTH_TOKEN: "key" } }); - await apply(plan(), { home, adapters: adapters() }); - expect(read(home, claude)).toEqual({}); - }); - test("keeps a key helper Hedge did not write, though it names a file beside the proxy key", async () => { const home = temp.make("home-"); const mine = { apiKeyHelper: 'my-helper --cache "$HOME/.config/hedge/cli-proxy.key-cache"' }; @@ -425,7 +427,7 @@ describe("provider config", () => { write(home, claude, { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317", ANTHROPIC_AUTH_TOKEN: "secret" } }); mkdirSync(join(home, ".local/state/hedge"), { recursive: true }); symlinkSync(elsewhere, join(home, ".local/state/hedge/backups")); - await expect(apply(plan(), { home, adapters: [claudeCli().adapter] })).rejects.toThrow("is a link or not a directory"); + await expect(apply(plan([{ provider: "claude", key: "model", value: "opus" }]), { home, adapters: [claudeCli().adapter] })).rejects.toThrow("is a link or not a directory"); expect(readdirSync(elsewhere)).toEqual([]); expect(read(home, claude).env.ANTHROPIC_AUTH_TOKEN).toBe("secret"); }); diff --git a/src/adapters/provider-config/stale.ts b/src/adapters/provider-config/stale.ts deleted file mode 100644 index fdef3168..00000000 --- a/src/adapters/provider-config/stale.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { Provider } from "../../pack/types.ts"; -import { get, isDocument, type Document } from "./fields.ts"; - -export const proxyPort = "8317"; - -export function pointsAtProxy(value: unknown) { - if (typeof value !== "string") return false; - try { - return new URL(value).port === proxyPort; - } catch { - return false; - } -} - -/** Proxy keys a Hub pack once wrote. free says whether no pack owns a path and the user did not change it. */ -export function staleKeys(provider: Provider, document: Document, isFree: (path: string[]) => boolean): string[][] { - const free = (path: string[]) => isFree(path) && get(document, path) !== undefined; - const stale: string[][] = []; - if (provider === "claude") { - const base = ["env", "ANTHROPIC_BASE_URL"]; - const token = ["env", "ANTHROPIC_AUTH_TOKEN"]; - if (free(base) && pointsAtProxy(get(document, base))) stale.push(base, ...(free(token) ? [token] : [])); - } - if (provider === "codex") { - const providers = get(document, ["model_providers"]); - const names = isDocument(providers) ? Object.keys(providers).filter((name) => free(["model_providers", name]) && pointsAtProxy(get(document, ["model_providers", name, "base_url"]))) : []; - stale.push(...names.map((name) => ["model_providers", name])); - const selected = get(document, ["model_provider"]); - if (free(["model_provider"]) && typeof selected === "string" && names.includes(selected)) stale.push(["model_provider"]); - } - return stale; -} diff --git a/src/adapters/t3/t3.test.ts b/src/adapters/t3/t3.test.ts index 66135578..81275da7 100644 --- a/src/adapters/t3/t3.test.ts +++ b/src/adapters/t3/t3.test.ts @@ -344,12 +344,17 @@ describe("T3 adapter with a running server", () => { expect(again.actions).toEqual([]); }); - test("applies a setting that waits for a passing check of a pack with a step in this apply, in the same apply", async () => { + test("applies a setting whose check first passes once a step of this apply ran, in the same apply", async () => { const home = temp.make("home-"); const { server, adapter } = setup({ settings: { sidebarAutoSettleAfterDays: 7 } }); const waiting = { ...plan("linux", [{ ...entry("sidebarAutoSettleAfterDays", 3), after: "ready" }]), checks: [{ id: "ready", run: ["ready"], say: "Not ready.", pack: "base" }], steps: [{ id: "prepare", run: ["prepare"], check: ["prepared"], pack: "base" }] }; - const report = await apply(waiting, { home, adapters: [adapter], run: async () => 0 }); - expect(report.ok).toBe(true); + let prepared = false; + const run = async ([command]: string[]) => { + if (command === "prepare") prepared = true; + return command === "prepared" || command === "ready" ? (prepared ? 0 : 1) : 0; + }; + const report = await apply(waiting, { home, adapters: [adapter], run }); + expect(report).toMatchObject({ ok: true, findings: [] }); expect(server.state.settings.sidebarAutoSettleAfterDays).toBe(3); }); diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index d67967bb..76de9589 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -258,6 +258,38 @@ describe("a config entry that waits for a pack check", () => { expect(settings(home)).toEqual({ model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "cat key" }); }); + const stepping = (codes: { before: number; after: number }) => { + let ran = false; + const seen: unknown[] = []; + const run = (home: string) => async ([command]: string[]) => { + if (command === "start-proxy") { + seen.push(settings(home)?.env?.ANTHROPIC_BASE_URL); + ran = true; + } + if (command === "proxy-check") return ran ? codes.after : codes.before; + return 0; + }; + return { run, seen }; + }; + const steps = [{ id: "start-proxy", run: ["start-proxy"], pack: "base" }]; + + test("that exits 4 without a todo before a step and passes after it puts its config in within the same apply, and leaves no Fix", async () => { + const home = temp.make("home-"); + const { run } = stepping({ before: 4, after: 0 }); + const applied = await apply(plan({ files: [], checks, config, steps }), { home, adapters: [providerConfigAdapter()], run: run(home) }); + expect(applied).toMatchObject({ ok: true, findings: [] }); + expect(settings(home)).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "cat key" }); + }); + + test("that passes before a step and fails after it takes the config back out before apply ends, and apply names the failed check", async () => { + const home = temp.make("home-"); + const { run, seen } = stepping({ before: 0, after: 1 }); + const applied = await apply(plan({ files: [], checks, config, steps }), { home, adapters: [providerConfigAdapter()], run: run(home) }); + expect(seen).toEqual(["http://127.0.0.1:8317"]); + expect(applied.findings).toEqual([down]); + expect(settings(home)).toEqual({}); + }); + test("doctor changes nothing, reports the failed check once, and fails", async () => { const home = temp.make("home-"); await apply(plan({ files: [], checks, config }), { home, adapters: [providerConfigAdapter()], run: proxy(() => true) }); diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index e0f6d9b4..d19ece85 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -185,13 +185,15 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R } await undo(items.filter((item) => item.cause !== "pack")); if (context.dryRun) return { report, checks: gated.checks }; - const settled = await settledChecks(plan, run, gated.checks, report.findings); - const settles = (pack: string, id?: string) => settled.some((item) => item.pack === pack && item.id === id); - const config = plan.config.filter((entry) => gated.plan.config.includes(entry) || settles(entry.pack, entry.after)); - if (config.length === gated.plan.config.length) return { report, checks: gated.checks }; - const dropped = gated.plan.dropped.filter((entry) => !entry.item.startsWith("config ") || !settles(entry.pack, entry.check)); - for (const adapter of adapters.filter((each) => each.name === "config")) { - const result = await adapter.apply({ ...gated.plan, config, dropped }, context); + const rechecked = await recheck(plan, run, gated.checks, report.findings); + const settled = await gateConfig(plan, async (pack, id) => { + const human = plan.human.find((step) => step.pack === pack && step.id === id); + return human ? gated.passed.includes(human) : rechecked.some(([item, code]) => item.pack === pack && item.id === id && code === 0); + }); + if (settled.config.length === gated.plan.config.length && settled.config.every((entry) => gated.plan.config.includes(entry))) return { report, checks: gated.checks }; + const settledPlan = { ...gated.plan, config: settled.config, dropped: [...gated.plan.dropped.filter((entry) => !gated.configDropped.includes(entry)), ...settled.dropped] }; + for (const adapter of adapters.filter((each) => readsConfig.includes(each.name))) { + const result = await adapter.apply(settledPlan, context); const first = firstFindings.get(adapter) ?? []; const stale = (finding: Finding) => first.includes(finding) && (result.findings.some((fresh) => fresh.id === finding.id && fresh.pack === finding.pack) || (result.resolved ?? []).includes(finding.id)); report.findings.splice(0, report.findings.length, ...report.findings.filter((finding) => !stale(finding)), ...result.findings); @@ -201,17 +203,28 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R return { report, checks: gated.checks }; } -async function settledChecks(plan: Plan, run: Runner, gated: Checked, findings: Finding[]): Promise>> { - const settled: Array> = []; +const readsConfig: Array = ["config", "t3"]; + +/** Runs again, after the steps, every check that config waits for and every check that says what a person does, and puts each one's finding in place of the first. */ +async function recheck(plan: Plan, run: Runner, gated: Checked, findings: Finding[]): Promise { + const rechecked: Checked = []; for (const item of plan.checks.filter((check) => check.todo || gated.some(([ran]) => ran === check))) { - const before = gated.find(([ran]) => ran === item)?.[1]; - if (before === 0 || before === waitsForPerson) continue; const code = await run(item.run); const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, ...(code === 0 ? [] : [checkFinding(item, code)])); - if (code === 0) settled.push(item); + rechecked.push([item, code]); + } + return rechecked; +} + +async function gateConfig(plan: Plan, passes: (pack: string, id: string) => Promise): Promise<{ config: Plan["config"]; dropped: Plan["dropped"] }> { + const config: Plan["config"] = []; + const dropped: Plan["dropped"] = []; + for (const entry of plan.config) { + if (!entry.after || (await passes(entry.pack, entry.after))) config.push(entry); + else dropped.push({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value }); } - return settled; + return { config, dropped }; } export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { @@ -254,7 +267,7 @@ function humanFinding(step: Owned, repairStillToRun: boolean): Findin } /** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ -async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { +async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; configDropped: Plan["dropped"]; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { const tested = new Map, boolean>(); const probe = async (step: Owned) => { if ((await run(step.probe)) === 0) return true; @@ -286,14 +299,10 @@ async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: Ca const check = human ? undefined : plan.checks.find((item) => item.pack === pack && item.id === id); return check ? checkPasses(check) : passes(human); }; - const config: Plan["config"] = []; - for (const entry of plan.config) { - if (!entry.after || (await waitsFor(entry.pack, entry.after))) config.push(entry); - else dropped.push({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value }); - } + const config = await gateConfig(plan, waitsFor); const failed = [...tested].filter(([, passed]) => !passed).map(([step]) => step); const passed = [...tested].filter(([, passed]) => passed).map(([step]) => step); - return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped] }, failed, passed, checks: [...checked], issued }; + return { plan: { ...plan, jobs, config: config.config, dropped: [...plan.dropped, ...dropped, ...config.dropped] }, configDropped: config.dropped, failed, passed, checks: [...checked], issued }; } type Cause = { workflow: string } | "pack" | "plan"; From 01d4b1b50b21e6d270a8034aaba4afd74c216fa8 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:10:02 +0000 Subject: [PATCH 19/32] Keep an unrecorded proxy URL in the CLI test, as Hedge now owns only what it recorded Co-Authored-By: Claude Opus 5.5 --- src/cli.test.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/cli.test.ts b/src/cli.test.ts index e1873f93..50758dd6 100644 --- a/src/cli.test.ts +++ b/src/cli.test.ts @@ -121,19 +121,17 @@ describe("hedge apply", () => { }); }); - test("removes a stale proxy URL from Claude Code's settings, and doctor sees it first", async () => { + test("keeps a proxy URL in Claude Code's settings that Hedge has no record of, and doctor asks for no change to it", async () => { const home = temp.make("home-"); mkdirSync(join(home, ".claude"), { recursive: true }); - writeFileSync(join(home, ".claude/settings.json"), JSON.stringify({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317", ANTHROPIC_AUTH_TOKEN: "proxy-key" }, model: "opus" })); + const mine = { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317", ANTHROPIC_AUTH_TOKEN: "proxy-key" }, model: "opus" }; + writeFileSync(join(home, ".claude/settings.json"), JSON.stringify(mine)); const profile = profileDir(); - await hedge(["apply", "--profile", profile, "--dry-run"], home); const doctor = await hedge(["doctor", "--profile", profile], home); - expect(doctor.output).toMatch(/Fix: \.claude\/settings\.json needs an update: .*remove env\.ANTHROPIC_BASE_URL, env\.ANTHROPIC_AUTH_TOKEN\./); + expect(doctor.output).not.toContain("env.ANTHROPIC"); const result = await hedge(["apply", "--profile", profile], home); - expect(result.output).toContain("replace .claude/settings.json"); - const settings = JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")); - expect(settings.env).toBeUndefined(); - expect(settings.model).toBe("opus"); + expect(result.output).not.toContain("env.ANTHROPIC"); + expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8"))).toMatchObject(mine); }); test("keeps an unmanaged file and a local edit, and says so", async () => { From 4c927f99541ba7aaea3e45c29576c4d444ee101e Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:25:48 +0000 Subject: [PATCH 20/32] Decide config that waits for a check once, after the steps Apply decided gated config twice: once before the steps and again after them, rerunning config adapters when the decision changed. The first decision dropped Hedge's record of a setting the second could not undo, or made Hedge's own old value look like a user edit. The provider config adapter now runs once, after the steps. Before it, apply runs each check that config waits for, and each check with a todo, once, and decides the waiting config from those results. The other adapters still run before the steps. The T3 adapter must, as the peers step needs T3, so a T3 setting can wait only for a human step. No pack has a T3 setting that waits for a check. This removes the second config pass, the rerun of adapters, and the resolved ids that plugins and MCP servers reported for it. Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 26 +++--- src/adapters/provider-config/adapter.ts | 6 +- .../provider-config/claude-servers.ts | 16 ++-- src/adapters/provider-config/plugins.ts | 48 ++++------- src/adapters/t3/t3.test.ts | 14 ---- src/pack/manifest.test.ts | 3 +- src/pack/manifest.ts | 2 +- src/reconciler/apply.test.ts | 31 +++++-- src/reconciler/apply.ts | 84 ++++++------------- 9 files changed, 93 insertions(+), 137 deletions(-) diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 86d3c8d4..0300542d 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -433,26 +433,26 @@ describe("the Own-account balancing workflow", () => { const { home, result, options } = emptyHub("claude"); const fresh = await apply(result, options); expect(fresh.ok).toBe(true); - expect(fresh.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); + expect(fresh.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-accounts", true], ["check:cli-proxy-client", true]]); expect(fresh.findings.map((finding) => finding.say.endsWith(`: ${login("claude")}`))).toEqual([true, true]); expect(existsSync(join(home, ".local/share/hedge-cli-proxy/cli-proxy-api"))).toBe(true); const applied = await apply(result, options); expect(applied.ok).toBe(true); - expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-accounts", true], ["check:cli-proxy-client", true]]); expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); const doctor = await check(result, options); expect(doctor.ok).toBe(true); - expect(doctor.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); + expect(doctor.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-accounts", true], ["check:cli-proxy-client", true]]); }); test("on the Codex plan, lists the Codex login as a To do and keeps Codex on its own sign-in", async () => { const { home, result, options } = emptyHub("codex"); const fresh = await apply(result, options); expect(fresh.ok).toBe(true); - expect(fresh.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-codex", true], ["check:cli-proxy-codex-accounts", true]]); + expect(fresh.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-codex-accounts", true], ["check:cli-proxy-codex", true]]); const applied = await apply(result, options); expect(applied.ok).toBe(true); - expect(applied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-codex", true], ["check:cli-proxy-codex-accounts", true]]); + expect(applied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-codex-accounts", true], ["check:cli-proxy-codex", true]]); expect(applied.findings.map((finding) => finding.say).join("\n")).toContain(login("codex")); expect(existsSync(join(home, ".codex/config.toml"))).toBe(false); expect((await check(result, options)).ok).toBe(true); @@ -488,7 +488,7 @@ describe("the Own-account balancing workflow", () => { const result = proxyOnly(plan({ ...balancing, plan: "claude" }, hub)); const applied = await apply(result, { home, adapters: [providerConfigAdapter()], run: hedgeProxy(machine, result.engineRoot) }); expect(applied.ok).toBe(false); - expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["check:cli-proxy-client", "apply"], ["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true], ["check:cli-proxy-client", "apply"]]); expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); }); @@ -519,7 +519,7 @@ describe("the Own-account balancing workflow", () => { return { home, result, routes, route, atInstall, hooks, options: { home, adapters: [providerConfigAdapter()], run } }; }; const foreignPlugin = { ".local/share/hedge-cli-proxy/plugins/other.so": "plugin" }; - const unsafe: Array<[string, string | true | undefined]> = [["check:cli-proxy-client", "apply"], ["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true]]; + const unsafe: Array<[string, string | true | undefined]> = [["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true], ["check:cli-proxy-client", "apply"]]; test("with a running proxy that has a plugin Hedge did not install from the start, apply fails and never routes Claude Code to the proxy", async () => { const { result, routes, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }, foreignPlugin); @@ -530,7 +530,7 @@ describe("the Own-account balancing workflow", () => { expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); }); - test("after a successful apply, a plugin Hedge did not install appears while the proxy still lists the model: the next apply takes the route out before the proxy step runs, and fails", async () => { + test("after a successful apply, a plugin Hedge did not install appears while the proxy still lists the model: the next apply takes the route out by the time it ends, and fails", async () => { const { home, result, routes, route, atInstall, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); expect((await apply(result, options)).ok).toBe(true); expect(route()).toBe("http://127.0.0.1:8317"); @@ -538,7 +538,7 @@ describe("the Own-account balancing workflow", () => { writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); atInstall.splice(0); const failed = await apply(result, options); - expect(atInstall).toEqual([undefined]); + expect(atInstall).toEqual(["http://127.0.0.1:8317"]); expect(route()).toBeUndefined(); expect(failed.ok).toBe(false); expect(failed.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); @@ -548,17 +548,17 @@ describe("the Own-account balancing workflow", () => { expect(route()).toBeUndefined(); }); - test("a plugin Hedge did not install that appears after the route check, while the proxy step runs, leaves no route when apply ends", async () => { + test("a plugin Hedge did not install that appears while the proxy step runs leaves no route when apply ends", async () => { const { home, result, route, atInstall, hooks, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); hooks.beforeInstall = () => { mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); }; const applied = await apply(result, options); - expect(atInstall).toEqual(["http://127.0.0.1:8317"]); + expect(atInstall).toEqual([undefined]); expect(route()).toBeUndefined(); expect(applied.ok).toBe(false); - expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual([unsafe[1], unsafe[2], unsafe[0]]); + expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); }); test("with a running proxy that has a plugin Hedge did not install, a hedge-proxy Codex table of your own survives the failed step, and Codex keeps using it", async () => { @@ -596,7 +596,7 @@ describe("the Own-account balancing workflow", () => { Object.assign(proxy, { credentials: [], models: [] }); const emptied = await apply(result, options); expect(emptied.ok).toBe(true); - expect(emptied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-client", true], ["check:cli-proxy-accounts", true]]); + expect(emptied.findings.map((finding) => [finding.id, finding.todo])).toEqual([["check:cli-proxy-accounts", true], ["check:cli-proxy-client", true]]); const settings = existsSync(join(home, ".claude/settings.json")) ? JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")) : {}; expect([settings.env?.ANTHROPIC_BASE_URL, settings.apiKeyHelper]).toEqual([undefined, undefined]); }); diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 80375ef0..6c3297aa 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -38,11 +38,11 @@ export function providerConfigAdapter(options: { run?: CommandRunner } = {}): Ad const gated = gatedKeys(plan); const reconcile = async () => { const { plugins, claudeEnabled, marketplaces, codexPlugins, ...result } = await reconcileFiles(plan, context.home, context.dryRun, context.adopt, outage, gated); - const installed = await reconcilePlugins(plan, context.home, context.dryRun, run, { outage, accepted: plugins, claudeEnabled, marketplaces, acceptedCodex: codexPlugins }); + result.findings.push(...(await reconcilePlugins(plan, context.home, context.dryRun, run, { outage, accepted: plugins, claudeEnabled, marketplaces, acceptedCodex: codexPlugins }))); const servers = await reconcileServers(plan, context.home, { dryRun: context.dryRun, adopt: context.adopt.includes(claudeJson), outage, gated: gatedPath(gated, "claude"), run }); result.actions.push(...servers.actions); - result.findings.push(...installed.findings, ...servers.findings); - return { ...result, resolved: [...installed.resolved, ...servers.resolved] }; + result.findings.push(...servers.findings); + return result; }; const result = context.dryRun ? await reconcile() : await withLock(context.home, reconcile); if (outage) result.findings.push({ id: "config:outage", pack: "engine", say: "Kept every provider setting and plugin that Hedge would remove until all your pack sources can be fetched.", repair: "apply" }); diff --git a/src/adapters/provider-config/claude-servers.ts b/src/adapters/provider-config/claude-servers.ts index ff42088c..9ccad3ea 100644 --- a/src/adapters/provider-config/claude-servers.ts +++ b/src/adapters/provider-config/claude-servers.ts @@ -10,7 +10,7 @@ export const serversTarget = ".config/hedge/claude-servers.json"; /** value is what the pack asked for; stored is what Claude Code wrote for it, which can differ in shape. */ type Owned = { name: string; value: unknown; stored: unknown; pack: string }; type Wanted = { name: string; value: unknown; pack: string }; -type Result = { actions: Action[]; findings: Finding[]; resolved: string[] }; +type Result = { actions: Action[]; findings: Finding[] }; export const isMcpServer = (path: string[]) => path[0] === "mcpServers"; @@ -26,7 +26,7 @@ const finding = (id: string, say: string, repair?: Finding["repair"]): Finding = * Claude Code saves that file from several paths, not all of them locked, so no other writer can avoid racing it. */ export async function reconcileServers(plan: Plan, home: string, options: { dryRun: boolean; adopt: boolean; outage: boolean; gated: (path: string[]) => boolean; run: CommandRunner }): Promise { - const result: Result = { actions: [], findings: [], resolved: [] }; + const result: Result = { actions: [], findings: [] }; const wanted = wantedServers(plan); const recordText = await readState(home, serversTarget); const previous = parseOwned(recordText); @@ -75,10 +75,7 @@ export async function reconcileServers(plan: Plan, home: string, options: { dryR for (const name of remove) { const removed = await claude(["remove", name]); if (removed.code === 127) return waiting(result, wanted); - if (removed.code === 0) { - result.resolved.push(`claude-server:${name}`); - continue; - } + if (removed.code === 0) continue; blocked.add(name); const old = previous.find((item) => item.name === name); if (old) next.push(old); @@ -88,12 +85,9 @@ export async function reconcileServers(plan: Plan, home: string, options: { dryR for (const server of add.filter((item) => !blocked.has(item.name))) { const outcome = await claude(["add-json", server.name, JSON.stringify(server.value)]); if (outcome.code === 127) return waiting(result, wanted); - if (outcome.code === 0) { - added.push(server); - result.resolved.push(`claude-server:${server.name}`); - } else result.findings.push(finding(`claude-server:${server.name}`, `Claude Code did not add the MCP server ${server.name}: ${firstLine(outcome)}. Check its value in the ${server.pack} pack.`, "apply")); + if (outcome.code === 0) added.push(server); + else result.findings.push(finding(`claude-server:${server.name}`, `Claude Code did not add the MCP server ${server.name}: ${firstLine(outcome)}. Check its value in the ${server.pack} pack.`, "apply")); } - result.resolved.push("claude-servers"); const after = await currentServers(home); for (const server of added) if (after[server.name] !== undefined) next.push({ ...server, stored: after[server.name] }); if (reason) result.actions.push({ target: claudeJson, operation: "replace", reason }); diff --git a/src/adapters/provider-config/plugins.ts b/src/adapters/provider-config/plugins.ts index dcaf4693..a6f1460d 100644 --- a/src/adapters/provider-config/plugins.ts +++ b/src/adapters/provider-config/plugins.ts @@ -42,7 +42,7 @@ type PluginSettings = { outage: boolean; accepted?: string[]; claudeEnabled?: Re * Installs the plugins packs enable, through Claude Code and Codex in the given home. Removes only plugins that Hedge installed. * Reads the earlier Hedge's record, and never writes it: a plugin that Hedge installed becomes this Hedge's only once a pack enables it again. */ -export async function reconcilePlugins(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings): Promise<{ findings: Finding[]; resolved: string[] }> { +export async function reconcilePlugins(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings): Promise { const text = await readState(home, pluginsTarget); const earlier = parseOwned(await readState(home, earlierTarget), earlierTarget); const record = adopt(parseOwned(text, pluginsTarget), earlier, { claude: wantedPlugins(plan), codex: wantedCodexPlugins(plan) }); @@ -53,15 +53,14 @@ export async function reconcilePlugins(plan: Plan, home: string, dryRun: boolean await writeWithBackups(home, [{ target: pluginsTarget, before: saved, after }]); saved = after; }; - const resolved: string[] = []; - const claude = await reconcileClaude(plan, home, dryRun, run, settings, record.claude, resolved); + const claude = await reconcileClaude(plan, home, dryRun, run, settings, record.claude); if (claude.owned) await save({ ...record, claude: claude.owned }); const owned = { ...record, ...(claude.owned ? { claude: claude.owned } : {}) }; - const codex = await reconcileCodex(plan, home, dryRun, run, settings, owned.codex, (next) => save({ ...owned, codex: next }), resolved); - return { findings: [...claude.findings, ...codex], resolved }; + const codex = await reconcileCodex(plan, home, dryRun, run, settings, owned.codex, (next) => save({ ...owned, codex: next })); + return [...claude.findings, ...codex]; } -async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, record: ClaudeOwned[], resolved: string[]): Promise<{ findings: Finding[]; owned?: ClaudeOwned[] }> { +async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, record: ClaudeOwned[]): Promise<{ findings: Finding[]; owned?: ClaudeOwned[] }> { const outage = settings.outage; const planned = wantedPlugins(plan); const owned = record.map((entry) => entry.id); @@ -73,7 +72,6 @@ async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: C if (!installed) { return { findings: planned.length ? [{ id: "plugins:claude", pack: "engine", say: `Claude Code is not installed yet, so its plugins wait: ${planned.join(", ")}.`, repair: "apply" }] : [] }; } - resolved.push("plugins:claude"); const missing = wanted.filter((id) => !installed.includes(id)); const leaving = outage ? [] : record.filter((entry) => !planned.includes(entry.id) && installed.includes(entry.id)); const changedOutside = (entry: ClaudeOwned) => entry.enabled !== undefined && settings.claudeEnabled?.[entry.id] !== undefined && settings.claudeEnabled[entry.id] !== entry.enabled; @@ -87,27 +85,23 @@ async function reconcileClaude(plan: Plan, home: string, dryRun: boolean, run: C ...releasedFindings, ] }; } - const { findings, unreachable } = await fetchMarketplaces(claude, missing, { ...officialMarketplaces, ...settings.marketplaces }, resolved); + const { findings, unreachable } = await fetchMarketplaces(claude, missing, { ...officialMarketplaces, ...settings.marketplaces }); findings.push(...releasedFindings); const next = new Map(record.filter((entry) => !released.includes(entry.id) && (installed.includes(entry.id) || (outage && !planned.includes(entry.id)))).map((entry) => [entry.id, entry])); for (const id of missing.filter((id) => !unreachable.includes(marketplaceOf(id)))) { - if ((await claude(["install", id, "--scope", "user"])).code === 0) { - next.set(id, { id, enabled: true }); - resolved.push(`plugin:${id}`); - } else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not install the Claude plugin ${id}.`, repair: "apply" }); + if ((await claude(["install", id, "--scope", "user"])).code === 0) next.set(id, { id, enabled: true }); + else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not install the Claude plugin ${id}.`, repair: "apply" }); } for (const id of departed) { - if ((await claude(["uninstall", id, "--scope", "user", "--keep-data"])).code === 0) { - next.delete(id); - resolved.push(`plugin:${id}`); - } else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not remove the Claude plugin ${id}.`, repair: "apply" }); + if ((await claude(["uninstall", id, "--scope", "user", "--keep-data"])).code === 0) next.delete(id); + else findings.push({ id: `plugin:${id}`, pack: "engine", say: `Could not remove the Claude plugin ${id}.`, repair: "apply" }); } return { findings, owned: [...next.values()].sort((a, b) => a.id.localeCompare(b.id)) }; } const codexFinding = (id: string, say: string): Finding => ({ id: `plugin:codex:${id}`, pack: "engine", say, repair: "apply" }); -async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, owned: CodexOwned[], save: (next: CodexOwned[]) => Promise, resolved: string[]): Promise { +async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: CommandRunner, settings: PluginSettings, owned: CodexOwned[], save: (next: CodexOwned[]) => Promise): Promise { const planned = wantedCodexPlugins(plan); if (!planned.length && !owned.length) return []; if (!settings.acceptedCodex) return planned.length ? [{ id: "plugins:codex", pack: "engine", say: `Codex plugins wait until Hedge can change .codex/config.toml: ${planned.join(", ")}.`, repair: "apply" }] : []; @@ -115,7 +109,6 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co const codex: Codex = (args) => run(["codex", "plugin", ...args], { home, clearEnv: ["CODEX_HOME"] }); const installed = await listCodex(codex); if (!installed) return planned.length ? [{ id: "plugins:codex", pack: "engine", say: `Codex is not installed yet, so its plugins wait: ${planned.join(", ")}.`, repair: "apply" }] : []; - resolved.push("plugins:codex"); const findings: Finding[] = []; const record = new Map(owned.map((entry) => [entry.id, entry])); const commit = () => save([...record.values()].sort((a, b) => a.id.localeCompare(b.id))); @@ -130,8 +123,7 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co if (after) record.set(id, after); else if (listed) record.delete(id); await commit(); - if (added.code === 0 && after) resolved.push(`plugin:codex:${id}`); - else findings.push(codexFinding(id, `Could not install the Codex plugin ${id}: ${added.code !== 0 ? reasonOf(added) : listed ? "Codex does not list it afterwards" : "Codex cannot list its plugins afterwards"}. Hedge tries again at the next apply.`)); + if (added.code !== 0 || !after) findings.push(codexFinding(id, `Could not install the Codex plugin ${id}: ${added.code !== 0 ? reasonOf(added) : listed ? "Codex does not list it afterwards" : "Codex cannot list its plugins afterwards"}. Hedge tries again at the next apply.`)); }; for (const id of wanted) { const found = present(id); @@ -170,10 +162,8 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co continue; } const removed = await codex(["remove", mine.id, "--json"]); - if (removed.code === 0) { - record.delete(mine.id); - resolved.push(`plugin:codex:${mine.id}`); - } else findings.push(codexFinding(mine.id, `Could not remove the Codex plugin ${mine.id}: ${reasonOf(removed)}.`)); + if (removed.code === 0) record.delete(mine.id); + else findings.push(codexFinding(mine.id, `Could not remove the Codex plugin ${mine.id}: ${reasonOf(removed)}.`)); } await commit(); return findings; @@ -209,7 +199,7 @@ function addSource(source: unknown) { return Object.keys(fields).length === 1 && typeof value === "string" ? value : undefined; } -async function fetchMarketplaces(claude: Claude, plugins: string[], sources: Sources, resolved: string[]): Promise<{ findings: Finding[]; unreachable: string[] }> { +async function fetchMarketplaces(claude: Claude, plugins: string[], sources: Sources): Promise<{ findings: Finding[]; unreachable: string[] }> { const needed = [...new Set(plugins.map(marketplaceOf))]; const fetched = needed.length ? await listMarketplaces(claude) : undefined; const findings: Finding[] = []; @@ -218,16 +208,12 @@ async function fetchMarketplaces(claude: Claude, plugins: string[], sources: Sou const source = Object.hasOwn(sources, name) ? sources[name] : undefined; if (fetched!.includes(name)) { const updated = await claude(["marketplace", "update", name]); - if (updated.code === 0) resolved.push(`plugin-marketplace:${name}`); - else findings.push({ id: `plugin-marketplace:${name}`, pack: "engine", say: `Could not update the Claude plugin marketplace ${name}: ${reasonOf(updated)}.`, repair: "apply" }); + if (updated.code !== 0) findings.push({ id: `plugin-marketplace:${name}`, pack: "engine", say: `Could not update the Claude plugin marketplace ${name}: ${reasonOf(updated)}.`, repair: "apply" }); continue; } if (!source) continue; const added = await claude(["marketplace", "add", source, "--scope", "user"]); - if (added.code === 0) { - resolved.push(`plugin-marketplace:${name}`); - continue; - } + if (added.code === 0) continue; unreachable.push(name); const waiting = plugins.filter((id) => marketplaceOf(id) === name).join(", "); findings.push({ id: `plugin-marketplace:${name}`, pack: "engine", say: `Could not add the Claude plugin marketplace ${name} from ${withoutCredentials(source)}: ${reasonOf(added)}. Its plugins wait: ${waiting}.`, repair: "apply" }); diff --git a/src/adapters/t3/t3.test.ts b/src/adapters/t3/t3.test.ts index 81275da7..4de74ea2 100644 --- a/src/adapters/t3/t3.test.ts +++ b/src/adapters/t3/t3.test.ts @@ -344,20 +344,6 @@ describe("T3 adapter with a running server", () => { expect(again.actions).toEqual([]); }); - test("applies a setting whose check first passes once a step of this apply ran, in the same apply", async () => { - const home = temp.make("home-"); - const { server, adapter } = setup({ settings: { sidebarAutoSettleAfterDays: 7 } }); - const waiting = { ...plan("linux", [{ ...entry("sidebarAutoSettleAfterDays", 3), after: "ready" }]), checks: [{ id: "ready", run: ["ready"], say: "Not ready.", pack: "base" }], steps: [{ id: "prepare", run: ["prepare"], check: ["prepared"], pack: "base" }] }; - let prepared = false; - const run = async ([command]: string[]) => { - if (command === "prepare") prepared = true; - return command === "prepared" || command === "ready" ? (prepared ? 0 : 1) : 0; - }; - const report = await apply(waiting, { home, adapters: [adapter], run }); - expect(report).toMatchObject({ ok: true, findings: [] }); - expect(server.state.settings.sidebarAutoSettleAfterDays).toBe(3); - }); - test("retries a value T3 did not take on the next apply", async () => { const home = temp.make("home-"); await seed(plan("linux", [entry("sidebarAutoSettleAfterDays", 3)]), home); diff --git a/src/pack/manifest.test.ts b/src/pack/manifest.test.ts index e7688ffc..0f0f5f12 100644 --- a/src/pack/manifest.test.ts +++ b/src/pack/manifest.test.ts @@ -189,12 +189,13 @@ describe("loadPacks", () => { await expect(loadPacks(sourceWith({ base: { ...base, human, jobs: [{ ...job, roles: ["hub", "solo", "devbox"] }] } }))).rejects.toThrow("jobs listen: runs where its human step token does not; give it the same roles"); }); - test("lets a config entry wait for a check of its pack, but only where that check runs", async () => { + test("lets a Claude or Codex config entry wait for a check of its pack, but only where that check runs, and a T3 setting only for a human step", async () => { const checks = [{ id: "proxy-up", run: ["proxy-check"], say: "The proxy is down.", roles: ["devbox"] }]; const entry = { provider: "claude", key: "env.ANTHROPIC_BASE_URL", value: "http://127.0.0.1:8317", after: "proxy-up", roles: ["devbox"] }; await expect(loadPacks(sourceWith({ base: { ...base, checks, config: [entry] } }))).resolves.toHaveLength(1); await expect(loadPacks(sourceWith({ base: { ...base, checks, config: [{ ...entry, after: "proxy-gone" }] } }))).rejects.toThrow("config env.ANTHROPIC_BASE_URL: after must name a human step or a check of this pack"); await expect(loadPacks(sourceWith({ base: { ...base, checks, config: [{ ...entry, roles: ["devbox", "hub"] }] } }))).rejects.toThrow("config env.ANTHROPIC_BASE_URL: runs where its check proxy-up does not; give it the same roles"); + await expect(loadPacks(sourceWith({ base: { ...base, checks, config: [{ ...entry, provider: "t3", key: "sidebarAutoSettleAfterDays", value: 3 }] } }))).rejects.toThrow("config sidebarAutoSettleAfterDays: after must name a human step of this pack"); await expect(loadPacks(sourceWith({ base: { ...base, checks, steps: [{ id: "start", run: ["start"], after: "proxy-up", roles: ["devbox"] }] } }))).rejects.toThrow("steps start: after must name a human step of this pack"); }); diff --git a/src/pack/manifest.ts b/src/pack/manifest.ts index c0604853..cec50658 100644 --- a/src/pack/manifest.ts +++ b/src/pack/manifest.ts @@ -139,7 +139,7 @@ function checkShapes(raw: Record, fail: (message: string) => ne each("config", (item, label) => { const problem = configEntryProblem(item); if (problem) fail(`${label}: ${problem}`); - waitsWithin(item, label, true); + waitsWithin(item, label, item.provider !== "t3"); }); each("served", (item, label) => { need(typeof item.id === "string" && Number.isInteger(item.port) && typeof item.health === "string", `${label}: needs an id, a port, and a health path`); diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index 76de9589..a0d4265b 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -281,13 +281,32 @@ describe("a config entry that waits for a pack check", () => { expect(settings(home)).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "cat key" }); }); - test("that passes before a step and fails after it takes the config back out before apply ends, and apply names the failed check", async () => { + test("with a todo, that exits 4 before a step and passes after it puts its config in within the same apply, and leaves no To do", async () => { + const home = temp.make("home-"); + const { run } = stepping({ before: 4, after: 0 }); + const waiting = [{ ...checks[0]!, todo: "Add an account to the proxy." }]; + const applied = await apply(plan({ files: [], checks: waiting, config, steps }), { home, adapters: [providerConfigAdapter()], run: run(home) }); + expect(applied).toMatchObject({ ok: true, findings: [] }); + expect(settings(home)).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "cat key" }); + }); + + test("that passes before a step and fails after it never puts its config in, and apply names the failed check", async () => { const home = temp.make("home-"); const { run, seen } = stepping({ before: 0, after: 1 }); const applied = await apply(plan({ files: [], checks, config, steps }), { home, adapters: [providerConfigAdapter()], run: run(home) }); - expect(seen).toEqual(["http://127.0.0.1:8317"]); + expect(seen).toEqual([undefined]); expect(applied.findings).toEqual([down]); - expect(settings(home)).toEqual({}); + expect(settings(home)).toBeUndefined(); + }); + + test("that an earlier apply wrote, and whose check fails before a step and passes after it, stays in place, and apply changes nothing", async () => { + const home = temp.make("home-"); + await apply(plan({ files: [], checks, config }), { home, adapters: [providerConfigAdapter()], run: proxy(() => true) }); + const { run, seen } = stepping({ before: 1, after: 0 }); + const applied = await apply(plan({ files: [], checks, config, steps }), { home, adapters: [providerConfigAdapter()], run: run(home) }); + expect(seen).toEqual(["http://127.0.0.1:8317"]); + expect(applied).toMatchObject({ ok: true, actions: [], findings: [] }); + expect(settings(home)).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "cat key" }); }); test("doctor changes nothing, reports the failed check once, and fails", async () => { @@ -380,7 +399,7 @@ describe("a pack check that waits for the person", () => { const context7 = "context7@claude-plugins-official"; const plugin = { provider: "claude" as const, key: `enabledPlugins.${context7}`, value: true, pack: "base" }; - test("that config waits for and that passes once a step ran replaces the first config findings, so a plugin that waited for the step's Claude Code leaves no Fix and is installed for the user", async () => { + test("that config waits for and that passes once a step ran puts its config in after the step, so a plugin that waited for the step's Claude Code leaves no Fix and is installed for the user", async () => { const pool = installedPool(); pool.set(0); let installed = false; @@ -400,7 +419,7 @@ describe("a pack check that waits for the person", () => { expect((await check(plan({ files: [], checks, steps, config: [...config, plugin] }), options)).findings).toEqual([{ id: `plugin:${context7}`, pack: "engine", say: `The Claude plugin ${context7} needs to be installed.`, repair: "apply" }]); }); - test("that config waits for and that passes once a step ran keeps the first pass's notice that an edited plugin is yours now", async () => { + test("that config waits for and that passes once a step ran keeps the notice that an edited plugin is yours now", async () => { const pool = installedPool(); pool.set(0); const claude = claudeCli(() => true); @@ -414,7 +433,7 @@ describe("a pack check that waits for the person", () => { expect(claude.installed).toEqual([{ id: context7, scope: "user" }]); }); - test("that config waits for and that passes once a step ran keeps a Fix of the first pass for an operation the second pass skips", async () => { + test("that config waits for and that passes once a step ran still reports a marketplace update that failed", async () => { const pool = installedPool(); pool.set(0); const installed: Array<{ id: string; scope: string }> = []; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index d19ece85..21aabc14 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -10,7 +10,7 @@ import { hublessReason } from "./resolve.ts"; import { withLegacyNames } from "../legacy.ts"; export type AdapterContext = { home: string; dryRun: boolean; adopt: string[]; adoptFrom?: string[] }; -export type AdapterResult = { actions: Action[]; findings: Finding[]; blocked?: boolean; failed?: boolean; resolved?: string[] }; +export type AdapterResult = { actions: Action[]; findings: Finding[]; blocked?: boolean; failed?: boolean }; export type Adapter = { name: "files" | "config" | "jobs" | "identity" | "t3" | "fleet"; /** The targets that --adopt may name for this adapter. */ @@ -107,20 +107,18 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R const run = viaEngine(plan, options.run ?? spawnRunner(options.home, plan.machine.profileDir)); const node = plan.onHub || plan.pullsFromHub ? plan.machine.name : undefined; const issue = !context.dryRun && node && options.issue ? (ready: CatalogEntry[]) => options.issue!(ready, node) : undefined; - const skipped = new Set(options.skip ?? []); const gated = await gate(plan, run, !context.dryRun, issue); - report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun)), ...gated.checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); + report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun))); if (!context.dryRun && gated.failed.some(repairedByHedge)) report.ok = false; - const firstFindings = new Map(); - for (const adapter of adapters) { + const afterSteps = adapters.filter((adapter) => adapter.name === "config"); + for (const adapter of adapters.filter((each) => !afterSteps.includes(each))) { const result = await adapter.apply(gated.plan, context); - firstFindings.set(adapter, result.findings); report.actions.push(...result.actions); report.findings.push(...result.findings); if (result.failed) report.ok = false; if (!result.blocked) continue; report.ok = false; - if (!context.dryRun) return { report, checks: gated.checks }; + if (!context.dryRun) return { report, checks: [] }; } const before = await readLedger(options.home); const record = async (key: string, step?: LedgerStep) => { @@ -155,6 +153,7 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R if (step.pack === "base") report.ok = false; } }; + const skipped = new Set(options.skip ?? []); const items = undoItems(plan, before.steps).filter((item) => !skipped.has(ledgerKey(item.step))); await undo(items.filter((item) => item.cause === "pack")); const waits = waiter(plan, run); @@ -184,48 +183,24 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R if (!heldCodes.includes(await run(step.probe, { [undoVariable]: ledgerKey(step) }))) await changeLedger(options.home, "human", ledgerKey(step)); } await undo(items.filter((item) => item.cause !== "pack")); - if (context.dryRun) return { report, checks: gated.checks }; - const rechecked = await recheck(plan, run, gated.checks, report.findings); - const settled = await gateConfig(plan, async (pack, id) => { - const human = plan.human.find((step) => step.pack === pack && step.id === id); - return human ? gated.passed.includes(human) : rechecked.some(([item, code]) => item.pack === pack && item.id === id && code === 0); - }); - if (settled.config.length === gated.plan.config.length && settled.config.every((entry) => gated.plan.config.includes(entry))) return { report, checks: gated.checks }; - const settledPlan = { ...gated.plan, config: settled.config, dropped: [...gated.plan.dropped.filter((entry) => !gated.configDropped.includes(entry)), ...settled.dropped] }; - for (const adapter of adapters.filter((each) => readsConfig.includes(each.name))) { - const result = await adapter.apply(settledPlan, context); - const first = firstFindings.get(adapter) ?? []; - const stale = (finding: Finding) => first.includes(finding) && (result.findings.some((fresh) => fresh.id === finding.id && fresh.pack === finding.pack) || (result.resolved ?? []).includes(finding.id)); - report.findings.splice(0, report.findings.length, ...report.findings.filter((finding) => !stale(finding)), ...result.findings); + const checks: Checked = []; + for (const item of plan.checks.filter((check) => check.todo || plan.config.some((entry) => entry.pack === check.pack && entry.after === check.id))) checks.push([item, await run(item.run)]); + report.findings.push(...checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); + const passing = (entry: Plan["config"][number]) => checks.some(([item, code]) => item.pack === entry.pack && item.id === entry.after && code === 0); + const waiting = plan.config.filter((entry) => waitsForCheck(plan, entry)); + const configured = { ...gated.plan, config: plan.config.filter((entry) => (waiting.includes(entry) ? passing(entry) : gated.plan.config.includes(entry))), dropped: [...gated.plan.dropped, ...waiting.filter((entry) => !passing(entry)).map(droppedConfig)] }; + for (const adapter of afterSteps) { + const result = await adapter.apply(configured, context); report.actions.push(...result.actions); + report.findings.push(...result.findings); if (result.failed || result.blocked) report.ok = false; } - return { report, checks: gated.checks }; + return { report, checks }; } -const readsConfig: Array = ["config", "t3"]; - -/** Runs again, after the steps, every check that config waits for and every check that says what a person does, and puts each one's finding in place of the first. */ -async function recheck(plan: Plan, run: Runner, gated: Checked, findings: Finding[]): Promise { - const rechecked: Checked = []; - for (const item of plan.checks.filter((check) => check.todo || gated.some(([ran]) => ran === check))) { - const code = await run(item.run); - const stale = findings.findIndex((finding) => finding.id === `check:${item.id}` && finding.pack === item.pack); - findings.splice(stale < 0 ? findings.length : stale, stale < 0 ? 0 : 1, ...(code === 0 ? [] : [checkFinding(item, code)])); - rechecked.push([item, code]); - } - return rechecked; -} +const waitsForCheck = (plan: Plan, entry: Plan["config"][number]) => entry.after !== undefined && !plan.human.some((step) => step.pack === entry.pack && step.id === entry.after); -async function gateConfig(plan: Plan, passes: (pack: string, id: string) => Promise): Promise<{ config: Plan["config"]; dropped: Plan["dropped"] }> { - const config: Plan["config"] = []; - const dropped: Plan["dropped"] = []; - for (const entry of plan.config) { - if (!entry.after || (await passes(entry.pack, entry.after))) config.push(entry); - else dropped.push({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value }); - } - return { config, dropped }; -} +const droppedConfig = (entry: Plan["config"][number]): Plan["dropped"][number] => ({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value }); export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { const run = viaEngine(plan, options.run ?? spawnRunner(options.home, plan.machine.profileDir)); @@ -266,8 +241,8 @@ function humanFinding(step: Owned, repairStillToRun: boolean): Findin return { id: `human:${step.id}`, pack: step.pack, say: step.command ? `${step.say} Run: ${step.command}` : step.say, repair: repairStillToRun && repairedByHedge(step) ? "apply" : "human" }; } -/** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. A config entry may also wait for a pack check, which has no repair. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ -async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; configDropped: Plan["dropped"]; failed: Array>; passed: Array>; checks: Checked; issued: Finding[] }> { +/** Tests each check once, running a Hedge repair first when a human step's probe fails and repairs are on, then drops the items whose check fails. It tests the checks that served services wait for first, which only the hub can, and fills their credentials, so a repair that reads a credential sees it in the same apply. */ +async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: CatalogEntry[]) => Promise): Promise<{ plan: Plan; failed: Array>; passed: Array>; issued: Finding[] }> { const tested = new Map, boolean>(); const probe = async (step: Owned) => { if ((await run(step.probe)) === 0) return true; @@ -289,20 +264,15 @@ async function gate(plan: Plan, run: Runner, repair: boolean, issue?: (ready: Ca if (!job.after || (await passes(plan.human.find((step) => step.pack === job.pack && step.id === job.after)))) jobs.push(job); else dropped.push({ pack: job.pack, item: `job ${job.id}`, reason: `check ${job.after} does not pass`, check: job.after }); } - const checked = new Map, number>(); - const checkPasses = async (item: Owned) => { - if (!checked.has(item)) checked.set(item, await run(item.run)); - return checked.get(item) === 0; - }; - const waitsFor = (pack: string, id: string) => { - const human = checkOf(pack, id); - const check = human ? undefined : plan.checks.find((item) => item.pack === pack && item.id === id); - return check ? checkPasses(check) : passes(human); - }; - const config = await gateConfig(plan, waitsFor); + const config: Plan["config"] = []; + for (const entry of plan.config) { + if (waitsForCheck(plan, entry)) continue; + if (!entry.after || (await passes(checkOf(entry.pack, entry.after)))) config.push(entry); + else dropped.push(droppedConfig(entry)); + } const failed = [...tested].filter(([, passed]) => !passed).map(([step]) => step); const passed = [...tested].filter(([, passed]) => passed).map(([step]) => step); - return { plan: { ...plan, jobs, config: config.config, dropped: [...plan.dropped, ...dropped, ...config.dropped] }, configDropped: config.dropped, failed, passed, checks: [...checked], issued }; + return { plan: { ...plan, jobs, config, dropped: [...plan.dropped, ...dropped] }, failed, passed, issued }; } type Cause = { workflow: string } | "pack" | "plan"; From d2ec0ad37e395f3f1e0d03c3024e91eee03f03d3 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:26:09 +0000 Subject: [PATCH 21/32] Record the routes that hedge cli-proxy connect writes Connect wrote the Codex selector and table, and the Claude proxy address and key helper, without an ownership record. Apply now removes only settings its record names, so turning own-account balancing off left the route behind. Connect now adds what it sets to the same record apply uses, so apply takes the route off with the workflow. Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 11 +++++++++++ packs/hub/lib/cli-proxy.ts | 17 ++++++++++++++--- src/adapters/provider-config/adapter.ts | 9 +++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 0300542d..cf146752 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -310,6 +310,17 @@ describe("the Own-account balancing workflow", () => { expect(await codexConfig(mac)).toBeUndefined(); }); + test("after connect on a devbox, turning balancing off takes the Claude and Codex routes that connect wrote off at the next apply", async () => { + const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }, { role: "devbox" }); + expect(await cliProxy("connect", machine)).toBe(0); + expect(parse(readFileSync(join(home, ".codex/config.toml"), "utf8"))).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": codexProviderTable } }); + const off = plan({ packs: ["hub"], plan: "both" }, devbox); + const report = await apply({ ...off, config: off.config.filter((entry) => entry.pack === "hub"), checks: [], steps: [], jobs: [], human: [] }, { home, adapters: [providerConfigAdapter()], run: async () => 0 }); + expect(report.ok).toBe(true); + expect(parse(readFileSync(join(home, ".codex/config.toml"), "utf8"))).toEqual({}); + expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8"))).toEqual({}); + }); + test("sends Claude Code to the loopback proxy, and keeps the client key out of the settings: Claude reads it from a private file", () => { const config = plan(balancing, devbox).config.filter((entry) => entry.workflow === "own-account-balancing"); expect(Object.fromEntries(config.filter((entry) => entry.provider === "claude").map((entry) => [entry.key, entry.value]))).toEqual({ "env.ANTHROPIC_BASE_URL": "http://127.0.0.1:8317", apiKeyHelper: "cat \"$HOME/.config/hedge/cli-proxy.key\"" }); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index f3486cea..5a26bc21 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -12,6 +12,7 @@ import { refuseLinks, writePrivateFile, type Fetch } from "../../mcp-gateway/lib import type { Run } from "../../mcp-gateway/lib/hub.ts"; import { isKeptForBoxes } from "../../../src/hub/names.ts"; import { waitsForPerson } from "../../../src/pack/types.ts"; +import { recordWritten } from "../../../src/adapters/provider-config/adapter.ts"; import { refreshCodexCatalog } from "./codex-catalog.ts"; import { must, proxyPort } from "./run.ts"; @@ -348,8 +349,14 @@ export async function claudeRoute(home: string, on: boolean): Promise<(() => Pro if (settings.env && !Object.keys(env).length) delete settings.env; if (ownHelper(settings.apiKeyHelper)) delete settings.apiKeyHelper; } - if (JSON.stringify(settings) === before) return async () => {}; - return () => replaceUnchanged(path, text, `${JSON.stringify(settings, null, 2)}\n`); + const own = async () => { + if (on) await recordWritten(home, ".claude/settings.json", [{ path: ["env", "ANTHROPIC_BASE_URL"], value: proxyUrl, pack: "hub" }, { path: ["apiKeyHelper"], value: keyHelper, pack: "hub" }]); + }; + if (JSON.stringify(settings) === before) return own; + return async () => { + await own(); + await replaceUnchanged(path, text, `${JSON.stringify(settings, null, 2)}\n`); + }; } async function replaceUnchanged(path: string, before: string | undefined, after: string): Promise { @@ -445,7 +452,11 @@ async function codexRoute(home: string): Promise Promise)> if (table !== undefined && !reachesProxy(table)) return ownTable(path); config.model_provider = codexProvider; config.model_providers = { ...providers, [codexProvider]: table ?? codexProviderTable }; - return () => replaceUnchanged(path, text, `${stringify(config)}\n`); + const written = [{ path: ["model_provider"], value: codexProvider, pack: "hub" }, ...(table === undefined || Bun.deepEquals(table, codexProviderTable) ? [{ path: ["model_providers", codexProvider], value: codexProviderTable, pack: "hub" }] : [])]; + return async () => { + await recordWritten(home, ".codex/config.toml", written); + await replaceUnchanged(path, text, `${stringify(config)}\n`); + }; } async function sendCodex(home: string, besideClaude: boolean): Promise { diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 6c3297aa..09ff81c2 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -211,6 +211,15 @@ export function sourceOutage(plan: Plan) { return plan.packs.some((pack) => !pack.available && (pack.reason ?? "").startsWith("not found; unreachable")); } +export async function recordWritten(home: string, target: string, fields: Field[]) { + await withLock(home, async () => { + const before = await readState(home, recordTarget); + const record = await readFieldRecord(home); + record[target] = [...(record[target] ?? []).filter((old) => !fields.some((field) => overlaps(field.path, old.path))), ...fields]; + await writeWithBackups(home, [{ target: recordTarget, before, after: `${JSON.stringify(record, null, 2)}\n` }]); + }); +} + export async function readFieldRecord(home: string, target = recordTarget): Promise { const text = await readState(home, target); if (text === undefined) return {}; From 6a3c0ba03e4c01d2cb0698776baa7270efb5584b Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:26:12 +0000 Subject: [PATCH 22/32] Count a dangling link at the panel's page or plugin as still there With the panel off, the route check used existsSync, which follows links, so a dangling link at either panel path passed while doctor and install reject it. The check now uses lstat. Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy-panel.test.ts | 16 ++++++++++++++-- packs/hub/lib/cli-proxy.ts | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packs/hub/lib/cli-proxy-panel.test.ts b/packs/hub/lib/cli-proxy-panel.test.ts index d7da6137..49447ef7 100644 --- a/packs/hub/lib/cli-proxy-panel.test.ts +++ b/packs/hub/lib/cli-proxy-panel.test.ts @@ -1,6 +1,6 @@ import { afterEach, expect, test } from "bun:test"; -import { existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; -import { join } from "node:path"; +import { existsSync, mkdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; +import { dirname, join } from "node:path"; import { tempDirs } from "../../../src/testing/git.ts"; import { cliProxy, panelUrl, proxyUrl, type Panel, type ProxyMachine } from "./cli-proxy.ts"; import { fakeRun, modeOf, roleHome } from "./run-testing.ts"; @@ -230,3 +230,15 @@ test("once the profile turns the panel off, the route check on the hub fails whi await cliProxy("install", hub({ home }).machine); expect((await check())[0]).toBe(0); }); + +test("once the profile turns the panel off, the route check on the hub fails while a dangling link sits at the panel's page or plugin", async () => { + for (const at of ["page", "plugin"] as const) { + const { machine, home } = hub(); + await cliProxy("install", machine); + mkdirSync(dirname(files(home)[at]), { recursive: true }); + symlinkSync(join(home, "nowhere"), files(home)[at]); + const run = hub({ home }); + expect(await cliProxy("check", run.machine)).toBe(1); + expect(run.printed.join("\n")).toContain(`The Proxy panel is off, but ${files(home)[at]} is still there`); + } +}); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 5a26bc21..1e2fa419 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -1,5 +1,5 @@ import { randomUUID } from "node:crypto"; -import { existsSync, readFileSync, renameSync, rmSync } from "node:fs"; +import { existsSync, lstatSync, readFileSync, renameSync, rmSync } from "node:fs"; import { chmod, copyFile, lstat, mkdir, mkdtemp, readdir, readFile, rename, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { basename, dirname, join } from "node:path"; @@ -265,7 +265,7 @@ async function unsafe(machine: ProxyMachine, paths: Layout): Promise existsSync(path)); + const left = [paths.page, paths.plugin].filter((path) => lstatSync(path, { throwIfNoEntry: false })); return left.length ? `The Proxy panel is off, but ${left.join(" and ")} ${left.length === 1 ? "is" : "are"} still there, so the proxy may still serve or run ${left.length === 1 ? "it" : "them"}. Run hedge cli-proxy install.` : undefined; } for (const [path, release, name] of [[paths.page, machine.panel.page, "management panel"], [paths.plugin, machine.panel.plugin, "quota-ledger plugin"]] as const) { From 9fa872171f02d25cea334f6fb643ed92b781b55e Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:51:01 +0000 Subject: [PATCH 23/32] Own the proxy route by its exact value, and drop connect's record Connect recorded the routes it wrote, which raced a concurrent apply and claimed a hedge-proxy selector the user had set beside a table of their own. Boxes connected before that change had no record, so turning balancing off left their route in place. Hedge now owns a provider setting when its record names it, or when the proxy route holds exactly the values Hedge writes: the Claude proxy address and key helper, or the Codex hedge-proxy selector and table. Apply removes that route whenever it is not wanted, with balancing on or off, and leaves it alone while any part holds another value. Connect writes no record, and the route values live in one engine module that the Hub pack reads. Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 32 +++++++++++--- packs/hub/lib/cli-proxy-balance.ts | 4 +- packs/hub/lib/cli-proxy.ts | 25 +++-------- src/adapters/provider-config/adapter.ts | 42 +++++-------------- .../provider-config/provider-config.test.ts | 16 ++++++- src/adapters/provider-config/proxy-route.ts | 17 ++++++++ src/pack/types.ts | 2 +- src/reconciler/apply.ts | 2 +- 8 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 src/adapters/provider-config/proxy-route.ts diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index cf146752..d158104a 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -310,17 +310,37 @@ describe("the Own-account balancing workflow", () => { expect(await codexConfig(mac)).toBeUndefined(); }); - test("after connect on a devbox, turning balancing off takes the Claude and Codex routes that connect wrote off at the next apply", async () => { + const connectedDevbox = (files: Record = {}) => { const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }, { role: "devbox" }); - expect(await cliProxy("connect", machine)).toBe(0); - expect(parse(readFileSync(join(home, ".codex/config.toml"), "utf8"))).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": codexProviderTable } }); + for (const [path, text] of Object.entries(files)) { + mkdirSync(join(home, path, ".."), { recursive: true }); + writeFileSync(join(home, path), text); + } const off = plan({ packs: ["hub"], plan: "both" }, devbox); - const report = await apply({ ...off, config: off.config.filter((entry) => entry.pack === "hub"), checks: [], steps: [], jobs: [], human: [] }, { home, adapters: [providerConfigAdapter()], run: async () => 0 }); - expect(report.ok).toBe(true); - expect(parse(readFileSync(join(home, ".codex/config.toml"), "utf8"))).toEqual({}); + const applyOff = () => apply({ ...off, config: off.config.filter((entry) => entry.pack === "hub"), checks: [], steps: [], jobs: [], human: [] }, { home, adapters: [providerConfigAdapter()], run: async () => 0 }); + const codex = () => parse(readFileSync(join(home, ".codex/config.toml"), "utf8")); + return { machine, home, applyOff, codex }; + }; + + test("on a devbox that connect sent to the proxy, with no record of it, turning balancing off takes Hedge's exact Claude and Codex routes off at the next apply", async () => { + const { machine, home, applyOff, codex } = connectedDevbox(); + expect(await cliProxy("connect", machine)).toBe(0); + expect(codex()).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": codexProviderTable } }); + expect(existsSync(join(home, ".config/hedge/provider-fields.json"))).toBe(false); + expect((await applyOff()).ok).toBe(true); + expect(codex()).toEqual({}); expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8"))).toEqual({}); }); + test("your own hedge-proxy selector with a Codex table of your own stays, selector and table, after connect and after turning balancing off", async () => { + const mine = { model_provider: "hedge-proxy", model_providers: { "hedge-proxy": { ...codexProviderTable, name: "Mine" } } }; + const { machine, applyOff, codex } = connectedDevbox({ ".codex/config.toml": stringify(mine) }); + expect(await cliProxy("connect", machine)).toBe(0); + expect(codex()).toEqual(mine); + expect((await applyOff()).ok).toBe(true); + expect(codex()).toEqual(mine); + }); + test("sends Claude Code to the loopback proxy, and keeps the client key out of the settings: Claude reads it from a private file", () => { const config = plan(balancing, devbox).config.filter((entry) => entry.workflow === "own-account-balancing"); expect(Object.fromEntries(config.filter((entry) => entry.provider === "claude").map((entry) => [entry.key, entry.value]))).toEqual({ "env.ANTHROPIC_BASE_URL": "http://127.0.0.1:8317", apiKeyHelper: "cat \"$HOME/.config/hedge/cli-proxy.key\"" }); diff --git a/packs/hub/lib/cli-proxy-balance.ts b/packs/hub/lib/cli-proxy-balance.ts index 2c7a70c2..7ac89c5b 100644 --- a/packs/hub/lib/cli-proxy-balance.ts +++ b/packs/hub/lib/cli-proxy-balance.ts @@ -5,6 +5,9 @@ import { join } from "node:path"; import { requireHubRole, stateDir, writeStateFile } from "./hub-job.ts"; import type { Fetch } from "../../mcp-gateway/lib/client.ts"; import { refreshCodexCatalog } from "./codex-catalog.ts"; +import { proxyUrl } from "../../../src/adapters/provider-config/proxy-route.ts"; + +export { proxyUrl }; export type UsageWindow = { id?: string; utilization: number; resetsAt?: number }; /** `extraUsage` is true when the account keeps answering past its windows and bills the overage. */ @@ -22,7 +25,6 @@ type CodexWindow = { used_percent?: number; reset_at?: number; limit_window_seco type CodexLimit = { allowed?: boolean; limit_reached?: boolean; primary_window?: CodexWindow; secondary_window?: CodexWindow } | undefined; type Call = (path: string, init?: RequestInit) => Promise; -export const proxyUrl = "http://127.0.0.1:8317"; export const proxyDir = (home: string) => join(home, ".local/share/hedge-cli-proxy"); export const managementFile = (home: string) => join(proxyDir(home), "management.env"); export const codexCatalogPath = (home: string) => join(proxyDir(home), "codex-catalog.json"); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 1e2fa419..b8530ab9 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -12,11 +12,11 @@ import { refuseLinks, writePrivateFile, type Fetch } from "../../mcp-gateway/lib import type { Run } from "../../mcp-gateway/lib/hub.ts"; import { isKeptForBoxes } from "../../../src/hub/names.ts"; import { waitsForPerson } from "../../../src/pack/types.ts"; -import { recordWritten } from "../../../src/adapters/provider-config/adapter.ts"; +import { codexProvider, codexProviderTable, keyHelper } from "../../../src/adapters/provider-config/proxy-route.ts"; import { refreshCodexCatalog } from "./codex-catalog.ts"; import { must, proxyPort } from "./run.ts"; -export { proxyPort, proxyUrl }; +export { codexProvider, codexProviderTable, keyHelper, proxyPort, proxyUrl }; export type ProxyMode = "plan" | "install" | "doctor" | "accounts" | "catalog" | "connect" | "disconnect" | "check" | "remove" | "left"; export type Release = { version: string; url: string; sha256: string }; export type Panel = { page: Release; plugin: Release; historyDays: number }; @@ -27,7 +27,6 @@ export type ProxyMachine = { }; export const keyPath = (home: string) => join(home, ".config/hedge/cli-proxy.key"); -export const keyHelper = 'cat "$HOME/.config/hedge/cli-proxy.key"'; const fleetKeyHelper = 'cat "$HOME/.config/fleet/cli-proxy.key"'; const ownHelper = (helper: unknown) => helper === keyHelper || helper === fleetKeyHelper; export const panelUrl = `${proxyUrl}/management.html`; @@ -349,14 +348,8 @@ export async function claudeRoute(home: string, on: boolean): Promise<(() => Pro if (settings.env && !Object.keys(env).length) delete settings.env; if (ownHelper(settings.apiKeyHelper)) delete settings.apiKeyHelper; } - const own = async () => { - if (on) await recordWritten(home, ".claude/settings.json", [{ path: ["env", "ANTHROPIC_BASE_URL"], value: proxyUrl, pack: "hub" }, { path: ["apiKeyHelper"], value: keyHelper, pack: "hub" }]); - }; - if (JSON.stringify(settings) === before) return own; - return async () => { - await own(); - await replaceUnchanged(path, text, `${JSON.stringify(settings, null, 2)}\n`); - }; + if (JSON.stringify(settings) === before) return async () => {}; + return () => replaceUnchanged(path, text, `${JSON.stringify(settings, null, 2)}\n`); } async function replaceUnchanged(path: string, before: string | undefined, after: string): Promise { @@ -373,9 +366,7 @@ async function replaceUnchanged(path: string, before: string | undefined, after: const sendClaude = async (home: string, on: boolean) => (await claudeRoute(home, on))?.(); -export const codexProvider = "hedge-proxy"; -const codexUrl = `${proxyUrl}/v1`; -export const codexProviderTable = { name: "Hedge CLI proxy", base_url: codexUrl, wire_api: "responses", auth: { command: "/bin/sh", args: ["-c", keyHelper] } }; +const codexUrl = codexProviderTable.base_url; const fleetProvider = "fleet-proxy"; const fleetProviderTable = { name: "Fleet CLI proxy", base_url: codexUrl, wire_api: "responses", auth: { command: "/bin/sh", args: ["-c", fleetKeyHelper] } }; @@ -452,11 +443,7 @@ async function codexRoute(home: string): Promise Promise)> if (table !== undefined && !reachesProxy(table)) return ownTable(path); config.model_provider = codexProvider; config.model_providers = { ...providers, [codexProvider]: table ?? codexProviderTable }; - const written = [{ path: ["model_provider"], value: codexProvider, pack: "hub" }, ...(table === undefined || Bun.deepEquals(table, codexProviderTable) ? [{ path: ["model_providers", codexProvider], value: codexProviderTable, pack: "hub" }] : [])]; - return async () => { - await recordWritten(home, ".codex/config.toml", written); - await replaceUnchanged(path, text, `${stringify(config)}\n`); - }; + return () => replaceUnchanged(path, text, `${stringify(config)}\n`); } async function sendCodex(home: string, besideClaude: boolean): Promise { diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 09ff81c2..70b40a84 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -6,6 +6,7 @@ import { spawnRunner, type CommandRunner } from "../run.ts"; import { codexHookTrust, isTrustOf } from "./codex-hooks.ts"; import { checkPath, get, json, keyPath, overlaps, remove, same, samePath, set, toml, type Document, type Field, type Format } from "./fields.ts"; import { declaredMarketplaces, reconcilePlugins } from "./plugins.ts"; +import { proxyRoute } from "./proxy-route.ts"; import { claudeJson, isMcpServer, reconcileServers, wantedServers } from "./claude-servers.ts"; import { linkOnPath, optional, readState, withLock, writeWithBackups, type Write } from "./store.ts"; @@ -64,7 +65,7 @@ async function reconcileFiles(plan: Plan, home: string, dryRun: boolean, adopt: const fields = owned(plan, target); let merged: Merged; try { - merged = merge(target, before, old, fields, adopt.includes(target.path), { outage, gated: gatedPath(gated, target.provider), dropped: gatedFields(plan, target), configPath: join(home, target.path) }); + merged = merge(target, before, old, fields, adopt.includes(target.path), { outage, gated: gatedPath(gated, target.provider), configPath: join(home, target.path) }); } catch (error) { keepOld(); result.findings.push({ id: `kept:${target.path}`, pack: "engine", say: `Kept ${target.path} as it is: Hedge cannot read it (${(error as Error).message}). Fix the file, then run hedge apply.`, repair: "human" }); @@ -105,7 +106,7 @@ function owned(plan: Plan, target: Target): Field[] { return fields; } -function merge(target: Target, text: string | undefined, previous: Field[], wanted: Field[], adopt: boolean, options: { outage: boolean; gated: (path: string[]) => boolean; dropped: GatedField[]; configPath: string }): Merged { +function merge(target: Target, text: string | undefined, previous: Field[], wanted: Field[], adopt: boolean, options: { outage: boolean; gated: (path: string[]) => boolean; configPath: string }): Merged { const current: Document = text === undefined ? {} : target.format.decode(text); const original = target.format.encode(current); const record: Field[] = []; @@ -152,15 +153,14 @@ function merge(target: Target, text: string | undefined, previous: Field[], want removed.push(field.path.join(".")); } } - const untouched = (field: GatedField) => get(current, field.path) === undefined || same(get(current, field.path), field.value); - const unrecorded = options.dropped - .filter((field) => options.dropped.every((other) => other.check !== field.check || other.pack !== field.pack || untouched(other))) - .filter((field) => !all.some((item) => overlaps(item.path, field.path))) - .map((field) => field.path); - for (const path of unrecorded) { - if (get(current, path) === undefined) continue; - remove(current, path); - removed.push(path.join(".")); + const route = proxyRoute[target.provider] ?? []; + const held = route.filter((field) => get(current, field.path) !== undefined); + const hedges = held.every((field) => same(get(current, field.path), field.value)); + const wantedRoute = route.some((field) => all.some((item) => overlaps(item.path, field.path))); + const outageKeeps = options.outage && !route.some((field) => options.gated(field.path)); + for (const field of hedges && !wantedRoute && !outageKeeps ? held : []) { + remove(current, field.path); + removed.push(field.path.join(".")); } const reason = [changed.length ? `set ${changed.join(", ")}` : "", removed.length ? `remove ${removed.join(", ")}` : ""].filter(Boolean).join("; "); const after = target.format.encode(current); @@ -189,17 +189,6 @@ function gatedKeys(plan: Plan) { return plan.dropped.flatMap((entry) => (entry.check && entry.item.startsWith("config ") ? [entry.item.slice("config ".length)] : [])); } -type GatedField = Field & { check: string }; - -/** The config entries of this target that the gate dropped, with Hedge's value. Entries that wait for the same check leave together, even during an outage and without a record, unless one of them holds another value. */ -function gatedFields(plan: Plan, target: Target): GatedField[] { - return plan.dropped.flatMap((entry) => { - if (!entry.check || entry.value === undefined || !entry.item.startsWith(`config ${target.provider}:`)) return []; - const path = keyPath(entry.item.slice(`config ${target.provider}:`.length)); - return target.owns(path) ? [{ path, value: entry.value, pack: entry.pack, check: entry.check }] : []; - }); -} - /** Whether a path overlaps a config key of this provider that the gate dropped, so an outage does not keep it. */ function gatedPath(gated: string[], provider: Provider) { const paths = gated.filter((key) => key.startsWith(`${provider}:`)).map((key) => keyPath(key.slice(provider.length + 1))); @@ -211,15 +200,6 @@ export function sourceOutage(plan: Plan) { return plan.packs.some((pack) => !pack.available && (pack.reason ?? "").startsWith("not found; unreachable")); } -export async function recordWritten(home: string, target: string, fields: Field[]) { - await withLock(home, async () => { - const before = await readState(home, recordTarget); - const record = await readFieldRecord(home); - record[target] = [...(record[target] ?? []).filter((old) => !fields.some((field) => overlaps(field.path, old.path))), ...fields]; - await writeWithBackups(home, [{ target: recordTarget, before, after: `${JSON.stringify(record, null, 2)}\n` }]); - }); -} - export async function readFieldRecord(home: string, target = recordTarget): Promise { const text = await readState(home, target); if (text === undefined) return {}; diff --git a/src/adapters/provider-config/provider-config.test.ts b/src/adapters/provider-config/provider-config.test.ts index c7101823..ab2da20b 100644 --- a/src/adapters/provider-config/provider-config.test.ts +++ b/src/adapters/provider-config/provider-config.test.ts @@ -95,6 +95,20 @@ describe("provider config", () => { expect(report.actions).toEqual([]); }); + test("removes Hedge's exact proxy route that it has no record of, and keeps the proxy address beside a key helper of your own", async () => { + const hedges = temp.make("home-"); + write(hedges, claude, { model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: 'cat "$HOME/.config/hedge/cli-proxy.key"' }); + const yours = temp.make("home-"); + const mine = { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "my-helper" }; + write(yours, claude, mine); + + await apply(plan(), { home: hedges, adapters: adapters() }); + await apply(plan(), { home: yours, adapters: adapters() }); + + expect(read(hedges, claude)).toEqual({ model: "opus" }); + expect(read(yours, claude)).toEqual(mine); + }); + test("keeps a base URL that points at a gateway the user chose", async () => { const home = temp.make("home-"); const settings = { env: { ANTHROPIC_BASE_URL: "https://gateway.example.com", ANTHROPIC_AUTH_TOKEN: "token" } }; @@ -249,7 +263,7 @@ describe("provider config", () => { expect(lstatSync(join(home, claude)).isSymbolicLink()).toBe(true); expect(read(dotfiles, "settings.json")).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" } }); - expect(report.findings).toContainEqual({ id: `kept:${claude}`, pack: "engine", say: `Kept ${claude} because it is a link. Hedge would set model there. Make it a plain file in a plain folder to let Hedge change it.` }); + expect(report.findings).toContainEqual({ id: `kept:${claude}`, pack: "engine", say: `Kept ${claude} because it is a link. Hedge would set model; remove env.ANTHROPIC_BASE_URL there. Make it a plain file in a plain folder to let Hedge change it.` }); }); test("leaves a settings file it cannot read as it is", async () => { diff --git a/src/adapters/provider-config/proxy-route.ts b/src/adapters/provider-config/proxy-route.ts new file mode 100644 index 00000000..c12755a4 --- /dev/null +++ b/src/adapters/provider-config/proxy-route.ts @@ -0,0 +1,17 @@ +import type { Provider } from "../../pack/types.ts"; + +export const proxyUrl = "http://127.0.0.1:8317"; +export const keyHelper = 'cat "$HOME/.config/hedge/cli-proxy.key"'; +export const codexProvider = "hedge-proxy"; +export const codexProviderTable = { name: "Hedge CLI proxy", base_url: `${proxyUrl}/v1`, wire_api: "responses", auth: { command: "/bin/sh", args: ["-c", keyHelper] } }; + +export const proxyRoute: Partial>> = { + claude: [ + { path: ["env", "ANTHROPIC_BASE_URL"], value: proxyUrl }, + { path: ["apiKeyHelper"], value: keyHelper }, + ], + codex: [ + { path: ["model_provider"], value: codexProvider }, + { path: ["model_providers", codexProvider], value: codexProviderTable }, + ], +}; diff --git a/src/pack/types.ts b/src/pack/types.ts index fb07e09c..9778d4c5 100644 --- a/src/pack/types.ts +++ b/src/pack/types.ts @@ -254,7 +254,7 @@ export type Plan = { settings: Record; unanswered: PackSetting[]; ask: PackSetting[]; - dropped: Array<{ pack: string; item: string; reason: string; check?: string; value?: unknown }>; + dropped: Array<{ pack: string; item: string; reason: string; check?: string }>; }; export type Finding = { id: string; pack: string; say: string; repair?: "apply" | "human"; todo?: true }; diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 21aabc14..d54141c8 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -200,7 +200,7 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R const waitsForCheck = (plan: Plan, entry: Plan["config"][number]) => entry.after !== undefined && !plan.human.some((step) => step.pack === entry.pack && step.id === entry.after); -const droppedConfig = (entry: Plan["config"][number]): Plan["dropped"][number] => ({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after, value: entry.value }); +const droppedConfig = (entry: Plan["config"][number]): Plan["dropped"][number] => ({ pack: entry.pack, item: `config ${entry.provider}:${entry.key}`, reason: `check ${entry.after} does not pass`, check: entry.after }); export async function check(plan: Plan, options: { home: string; adapters?: Adapter[]; run?: Runner }): Promise { const run = viaEngine(plan, options.run ?? spawnRunner(options.home, plan.machine.profileDir)); From debb77e9ef49ee228d07adc946040733b4f6abee Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:51:15 +0000 Subject: [PATCH 24/32] Run provider config even when an earlier adapter throws Provider config now runs after the steps, so an adapter that ran before it on the base branch, such as Jobs on a job id it refuses, could throw before the route checks and provider cleanup ran. A route to a proxy that turned unsafe then stayed until the unrelated failure was fixed. When an adapter before the steps throws, apply now runs the route checks and the provider config adapter, then fails with the adapter's error. Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 12 ++++++++++++ src/reconciler/apply.ts | 34 ++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index d158104a..ab60919f 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -2,6 +2,7 @@ import { afterEach, beforeAll, describe, expect, test } from "bun:test"; import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { parse, stringify } from "smol-toml"; +import { jobsAdapter } from "../../src/adapters/jobs/adapter.ts"; import { providerConfigAdapter } from "../../src/adapters/provider-config/adapter.ts"; import { apply, check } from "../../src/reconciler/apply.ts"; import { boxCommand } from "./lib/box-command.ts"; @@ -573,6 +574,17 @@ describe("the Own-account balancing workflow", () => { expect(route()).toBeUndefined(); expect(failed.ok).toBe(false); expect(failed.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); + test("a job the Jobs adapter refuses still lets apply take the route to a proxy that turned unsafe out, and apply fails with the job's reason", async () => { + const { home, result, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + expect((await apply(result, options)).ok).toBe(true); + expect(route()).toBe("http://127.0.0.1:8317"); + mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); + writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); + const badJob: Plan = { ...result, jobs: [{ id: "bad id", command: ["hedge", "box", "sweep"], schedule: { every: "always" }, pack: "hub" }] }; + await expect(apply(badJob, { ...options, adapters: [jobsAdapter(), providerConfigAdapter()] })).rejects.toThrow("job id bad id of the hub pack may hold only"); + expect(route()).toBeUndefined(); + }); + routes.clear(); expect((await apply(result, options)).ok).toBe(false); expect(routes).toEqual(new Set([undefined])); diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index d54141c8..9405e96b 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -111,8 +111,26 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R report.findings.push(...gated.issued, ...gated.failed.map((step) => humanFinding(step, context.dryRun))); if (!context.dryRun && gated.failed.some(repairedByHedge)) report.ok = false; const afterSteps = adapters.filter((adapter) => adapter.name === "config"); + const configure = async () => { + const checks: Checked = []; + for (const item of plan.checks.filter((check) => check.todo || plan.config.some((entry) => entry.pack === check.pack && entry.after === check.id))) checks.push([item, await run(item.run)]); + report.findings.push(...checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); + const passing = (entry: Plan["config"][number]) => checks.some(([item, code]) => item.pack === entry.pack && item.id === entry.after && code === 0); + const waiting = plan.config.filter((entry) => waitsForCheck(plan, entry)); + const configured = { ...gated.plan, config: plan.config.filter((entry) => (waiting.includes(entry) ? passing(entry) : gated.plan.config.includes(entry))), dropped: [...gated.plan.dropped, ...waiting.filter((entry) => !passing(entry)).map(droppedConfig)] }; + for (const adapter of afterSteps) { + const result = await adapter.apply(configured, context); + report.actions.push(...result.actions); + report.findings.push(...result.findings); + if (result.failed || result.blocked) report.ok = false; + } + return { report, checks }; + }; for (const adapter of adapters.filter((each) => !afterSteps.includes(each))) { - const result = await adapter.apply(gated.plan, context); + const result = await adapter.apply(gated.plan, context).catch(async (error: unknown) => { + await configure(); + throw error; + }); report.actions.push(...result.actions); report.findings.push(...result.findings); if (result.failed) report.ok = false; @@ -183,19 +201,7 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R if (!heldCodes.includes(await run(step.probe, { [undoVariable]: ledgerKey(step) }))) await changeLedger(options.home, "human", ledgerKey(step)); } await undo(items.filter((item) => item.cause !== "pack")); - const checks: Checked = []; - for (const item of plan.checks.filter((check) => check.todo || plan.config.some((entry) => entry.pack === check.pack && entry.after === check.id))) checks.push([item, await run(item.run)]); - report.findings.push(...checks.filter(([, code]) => code !== 0).map(([item, code]) => checkFinding(item, code))); - const passing = (entry: Plan["config"][number]) => checks.some(([item, code]) => item.pack === entry.pack && item.id === entry.after && code === 0); - const waiting = plan.config.filter((entry) => waitsForCheck(plan, entry)); - const configured = { ...gated.plan, config: plan.config.filter((entry) => (waiting.includes(entry) ? passing(entry) : gated.plan.config.includes(entry))), dropped: [...gated.plan.dropped, ...waiting.filter((entry) => !passing(entry)).map(droppedConfig)] }; - for (const adapter of afterSteps) { - const result = await adapter.apply(configured, context); - report.actions.push(...result.actions); - report.findings.push(...result.findings); - if (result.failed || result.blocked) report.ok = false; - } - return { report, checks }; + return configure(); } const waitsForCheck = (plan: Plan, entry: Plan["config"][number]) => entry.after !== undefined && !plan.human.some((step) => step.pack === entry.pack && step.id === entry.after); From d984961ddb1ecdd2a7cb619b507c42187387bad1 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:51:18 +0000 Subject: [PATCH 25/32] Tell the person to remove a link at a panel path before install Install refuses a link at the panel's page or plugin path, so the route check's advice to run hedge cli-proxy install could not fix a dangling link there. The safety check now names the link and says to remove it, then run install. The test follows that advice to a passing check. Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy-panel.test.ts | 7 +++++-- packs/hub/lib/cli-proxy.ts | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packs/hub/lib/cli-proxy-panel.test.ts b/packs/hub/lib/cli-proxy-panel.test.ts index 49447ef7..61c7c390 100644 --- a/packs/hub/lib/cli-proxy-panel.test.ts +++ b/packs/hub/lib/cli-proxy-panel.test.ts @@ -231,7 +231,7 @@ test("once the profile turns the panel off, the route check on the hub fails whi expect((await check())[0]).toBe(0); }); -test("once the profile turns the panel off, the route check on the hub fails while a dangling link sits at the panel's page or plugin", async () => { +test("once the profile turns the panel off, the route check on the hub fails while a dangling link sits at the panel's page or plugin, and passes once you remove the link and run install as it says", async () => { for (const at of ["page", "plugin"] as const) { const { machine, home } = hub(); await cliProxy("install", machine); @@ -239,6 +239,9 @@ test("once the profile turns the panel off, the route check on the hub fails whi symlinkSync(join(home, "nowhere"), files(home)[at]); const run = hub({ home }); expect(await cliProxy("check", run.machine)).toBe(1); - expect(run.printed.join("\n")).toContain(`The Proxy panel is off, but ${files(home)[at]} is still there`); + expect(run.printed.join("\n")).toContain(`${files(home)[at]} is a link, which Hedge does not follow or replace. Remove it, then run hedge cli-proxy install.`); + rmSync(files(home)[at]); + expect(await cliProxy("install", hub({ home }).machine)).toBe(0); + expect(await cliProxy("check", hub({ home }).machine)).toBe(0); } }); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index b8530ab9..89873bad 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -263,6 +263,8 @@ async function accounts(machine: ProxyMachine, paths: Layout): Promise { async function unsafe(machine: ProxyMachine, paths: Layout): Promise { const others = await foreignPlugins(paths); if (others.length) return `Remove the plugins Hedge did not install: ${others.join(", ")}. While the Proxy panel is on, anyone with the management key can install plugins from CLIProxyAPI's plugin store, and the proxy runs them on the hub.`; + const linked = [paths.page, paths.plugin].filter((path) => lstatSync(path, { throwIfNoEntry: false })?.isSymbolicLink()); + if (linked.length) return `${linked.join(" and ")} ${linked.length === 1 ? "is a link" : "are links"}, which Hedge does not follow or replace. Remove ${linked.length === 1 ? "it" : "them"}, then run hedge cli-proxy install.`; if (!machine.panel) { const left = [paths.page, paths.plugin].filter((path) => lstatSync(path, { throwIfNoEntry: false })); return left.length ? `The Proxy panel is off, but ${left.join(" and ")} ${left.length === 1 ? "is" : "are"} still there, so the proxy may still serve or run ${left.length === 1 ? "it" : "them"}. Run hedge cli-proxy install.` : undefined; From 540aaefd6d6ed18d4a4c6607e58bf5960622b6e0 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:51:21 +0000 Subject: [PATCH 26/32] Prove a waiting check's To do before the step, and drop installer counts The test of a check with a todo that exits 4 before a step never ran the check before the step. It now runs doctor first, which shows the To do, then apply, which leaves no To do and writes the config. Two hub tests also asserted how many times the installer ran. They now assert only the final settings and the failure report. Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 27 ++++++++++----------------- src/reconciler/apply.test.ts | 7 +++++-- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index ab60919f..9c2b3da0 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -538,17 +538,13 @@ describe("the Own-account balancing workflow", () => { if (String(input).startsWith(proxyUrl)) routes.add(route()); return reach(input, init); }; - const atInstall: unknown[] = []; const hooks = { beforeInstall: () => {} }; const served = hedgeProxy(machine, result.engineRoot); const run = async (command: string[]) => { - if (command.at(-1) === "install") { - atInstall.push(route()); - hooks.beforeInstall(); - } + if (command.at(-1) === "install") hooks.beforeInstall(); return served(command); }; - return { home, result, routes, route, atInstall, hooks, options: { home, adapters: [providerConfigAdapter()], run } }; + return { home, result, routes, route, hooks, options: { home, adapters: [providerConfigAdapter()], run } }; }; const foreignPlugin = { ".local/share/hedge-cli-proxy/plugins/other.so": "plugin" }; const unsafe: Array<[string, string | true | undefined]> = [["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true], ["check:cli-proxy-client", "apply"]]; @@ -563,17 +559,21 @@ describe("the Own-account balancing workflow", () => { }); test("after a successful apply, a plugin Hedge did not install appears while the proxy still lists the model: the next apply takes the route out by the time it ends, and fails", async () => { - const { home, result, routes, route, atInstall, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + const { home, result, routes, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); expect((await apply(result, options)).ok).toBe(true); expect(route()).toBe("http://127.0.0.1:8317"); mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); - atInstall.splice(0); const failed = await apply(result, options); - expect(atInstall).toEqual(["http://127.0.0.1:8317"]); expect(route()).toBeUndefined(); expect(failed.ok).toBe(false); expect(failed.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); + routes.clear(); + expect((await apply(result, options)).ok).toBe(false); + expect(routes).toEqual(new Set([undefined])); + expect(route()).toBeUndefined(); + }); + test("a job the Jobs adapter refuses still lets apply take the route to a proxy that turned unsafe out, and apply fails with the job's reason", async () => { const { home, result, route, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); expect((await apply(result, options)).ok).toBe(true); @@ -585,20 +585,13 @@ describe("the Own-account balancing workflow", () => { expect(route()).toBeUndefined(); }); - routes.clear(); - expect((await apply(result, options)).ok).toBe(false); - expect(routes).toEqual(new Set([undefined])); - expect(route()).toBeUndefined(); - }); - test("a plugin Hedge did not install that appears while the proxy step runs leaves no route when apply ends", async () => { - const { home, result, route, atInstall, hooks, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + const { home, result, route, hooks, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); hooks.beforeInstall = () => { mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); }; const applied = await apply(result, options); - expect(atInstall).toEqual([undefined]); expect(route()).toBeUndefined(); expect(applied.ok).toBe(false); expect(applied.findings.map((finding) => [finding.id, finding.todo ?? finding.repair])).toEqual(unsafe); diff --git a/src/reconciler/apply.test.ts b/src/reconciler/apply.test.ts index a0d4265b..308c403e 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -284,10 +284,13 @@ describe("a config entry that waits for a pack check", () => { test("with a todo, that exits 4 before a step and passes after it puts its config in within the same apply, and leaves no To do", async () => { const home = temp.make("home-"); const { run } = stepping({ before: 4, after: 0 }); - const waiting = [{ ...checks[0]!, todo: "Add an account to the proxy." }]; - const applied = await apply(plan({ files: [], checks: waiting, config, steps }), { home, adapters: [providerConfigAdapter()], run: run(home) }); + const waiting = plan({ files: [], checks: [{ ...checks[0]!, todo: "Add an account to the proxy." }], config, steps }); + const options = { home, adapters: [providerConfigAdapter()], run: run(home) }; + expect((await check(waiting, options)).findings).toEqual([{ id: "check:proxy-up", pack: "base", say: "Add an account to the proxy.", todo: true }]); + const applied = await apply(waiting, options); expect(applied).toMatchObject({ ok: true, findings: [] }); expect(settings(home)).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "cat key" }); + expect(await check(waiting, options)).toMatchObject({ ok: true, findings: [] }); }); test("that passes before a step and fails after it never puts its config in, and apply names the failed check", async () => { From 6efc4508320b5377f891a45f5662c9f781b5b6ce Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:53:29 +0000 Subject: [PATCH 27/32] Own the proxy route only when every part holds Hedge's value Removing a lone proxy address that matched Hedge's value would leave an older ANTHROPIC_AUTH_TOKEN beside it, which sends Claude Code to Anthropic with the proxy's key. The route is now Hedge's only when each of its settings is there with Hedge's exact value: the Claude proxy address and key helper, or the Codex selector and table. Co-Authored-By: Claude Opus 5.5 --- src/adapters/provider-config/adapter.ts | 5 ++--- src/adapters/provider-config/provider-config.test.ts | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 70b40a84..4d85b6c7 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -154,11 +154,10 @@ function merge(target: Target, text: string | undefined, previous: Field[], want } } const route = proxyRoute[target.provider] ?? []; - const held = route.filter((field) => get(current, field.path) !== undefined); - const hedges = held.every((field) => same(get(current, field.path), field.value)); + const hedges = route.every((field) => same(get(current, field.path), field.value)); const wantedRoute = route.some((field) => all.some((item) => overlaps(item.path, field.path))); const outageKeeps = options.outage && !route.some((field) => options.gated(field.path)); - for (const field of hedges && !wantedRoute && !outageKeeps ? held : []) { + for (const field of hedges && !wantedRoute && !outageKeeps ? route : []) { remove(current, field.path); removed.push(field.path.join(".")); } diff --git a/src/adapters/provider-config/provider-config.test.ts b/src/adapters/provider-config/provider-config.test.ts index ab2da20b..525697d6 100644 --- a/src/adapters/provider-config/provider-config.test.ts +++ b/src/adapters/provider-config/provider-config.test.ts @@ -263,7 +263,7 @@ describe("provider config", () => { expect(lstatSync(join(home, claude)).isSymbolicLink()).toBe(true); expect(read(dotfiles, "settings.json")).toEqual({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" } }); - expect(report.findings).toContainEqual({ id: `kept:${claude}`, pack: "engine", say: `Kept ${claude} because it is a link. Hedge would set model; remove env.ANTHROPIC_BASE_URL there. Make it a plain file in a plain folder to let Hedge change it.` }); + expect(report.findings).toContainEqual({ id: `kept:${claude}`, pack: "engine", say: `Kept ${claude} because it is a link. Hedge would set model there. Make it a plain file in a plain folder to let Hedge change it.` }); }); test("leaves a settings file it cannot read as it is", async () => { From f2cb11a4cfa3e8123784dffb6894b92eea163ca2 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:54:38 +0000 Subject: [PATCH 28/32] Describe the route cleanup and the single config decision in the changelog The changelog still described two config passes and cleanup by record alone. It now says apply decides provider config once after the steps, runs it even when an earlier adapter fails, owns the proxy route by its exact values, and names the fix for a link at a panel path. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e62677..b664c597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,35 +26,38 @@ plugin Hedge did not install still fails `hedge doctor`. - A pack check can name a `todo`: when its command exits 4, Hedge prints it as a `To do:` line, keeps the config that waits for the check out, and - fails neither apply nor doctor. After the steps, apply runs again each - check with a `todo` and each check that config waits for, and reports - what it finds then. When that changes which config waits, apply runs - every adapter that writes config again, the provider settings and T3's - settings: it writes the config whose check passes now, and takes out - Hedge's config whose check fails now. So a hub whose proxy was stopped - routes Claude Code in one apply, and a route never stays at the end of - an apply whose last check found the proxy unsafe. That config pass drops a line of the first pass only - when it says the same line again, or when the operation of that line now - succeeds. So a Claude plugin that waited for Claude Code leaves no old - `Fix:`, a notice such as "It is yours now" stays, and a `Fix:` for an - operation that the second pass skips, such as a marketplace update, - stays. + fails neither apply nor doctor. +- Apply now writes the provider settings, Claude plugins and MCP servers + once, after the steps. Just before, it runs each check with a `todo` + and each check that config waits for, and decides that config from + those results. So a hub whose proxy was stopped routes Claude Code in + one apply, and a route never stays at the end of an apply whose check + found the proxy unsafe. A route that an earlier apply wrote stays while + the steps run. When an adapter that runs before the steps fails, such + as Jobs on a job id it refuses, apply still decides and writes the + provider settings, then fails with that adapter's error. - On the hub, `hedge cli-proxy check` and `hedge cli-proxy check codex`, the checks that Hedge's routes wait for, fail while `hedge cli-proxy doctor` finds the proxy unsafe: a plugin Hedge did not install, panel files that do not match their pins, or, with the Proxy - panel off, a panel page or plugin that is still there. Apply then leaves - the route out, or takes Hedge's route out, in its first config pass, - before the proxy step runs. So a hub whose proxy has such a plugin never - routes Claude Code or Codex to it, and a route that an earlier apply - wrote leaves at the next apply. A route to a safe proxy stays in place, - and a `hedge-proxy` Codex table of your own stays. + panel off, a panel page or plugin that is still there. A link at either + panel path fails them too, and the check says to remove the link, then + run `hedge cli-proxy install`. Apply then leaves the route out, or takes + Hedge's route out. So a hub whose proxy has such a plugin never routes + Claude Code or Codex to it, and a route that an earlier apply wrote + leaves at the next apply. A route to a safe proxy stays in place, and a + `hedge-proxy` Codex table of your own stays. - Hedge removes a Claude Code or Codex setting only when its own record - says Hedge wrote it, or when it holds Hedge's exact value for a route - that waits. It no longer removes a setting because it points at the - proxy port 8317. So a Codex provider table of your own, of any name, - stays with its selector. `hedge doctor` reads - one result of each check for both the config and its line. + says Hedge wrote it, or when it is part of the route to the proxy and + the whole route holds Hedge's exact values: the Claude proxy address + and key helper, or the Codex `hedge-proxy` selector and table. Apply + removes that route whenever this apply does not write it, with + own-account balancing on or off, so a box that `hedge cli-proxy connect` sent to + the proxy leaves it when you turn balancing off. It no longer removes + a setting because it points at the proxy port 8317. So a Codex + provider table of your own, of any name, stays with its selector. + `hedge doctor` reads one result of each check for both the config and + its line. - When `hedge cli-proxy connect` fails on a box, the hub names the box and the exit code, and the command that hands over the same key again: `hedge box run -- hedge cli-proxy connect < ~/.config/hedge/cli-proxy.key`. From f49f32d7983ba5b77011e986a57834e1c84441ba Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:06:03 +0000 Subject: [PATCH 29/32] Report both errors when provider config fails after an adapter error, the original first Co-Authored-By: Claude Opus 5.5 --- packs/hub/hub.test.ts | 10 ++++++++++ src/reconciler/apply.ts | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index 9c2b3da0..ccf00c54 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -585,6 +585,16 @@ describe("the Own-account balancing workflow", () => { expect(route()).toBeUndefined(); }); + test("a job the Jobs adapter refuses and a provider record Hedge cannot read both reach the person, the job's reason first", async () => { + const { home, result, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); + mkdirSync(join(home, ".config/hedge"), { recursive: true }); + writeFileSync(join(home, ".config/hedge/provider-fields.json"), "{"); + const badJob: Plan = { ...result, jobs: [{ id: "bad id", command: ["hedge", "box", "sweep"], schedule: { every: "always" }, pack: "hub" }] }; + const failure = await apply(badJob, { ...options, adapters: [jobsAdapter(), providerConfigAdapter()] }).then(() => "", (error: Error) => error.message); + expect(failure).toStartWith("job id bad id of the hub pack may hold only"); + expect(failure).toContain(" Hedge also could not set your provider settings: JSON Parse error"); + }); + test("a plugin Hedge did not install that appears while the proxy step runs leaves no route when apply ends", async () => { const { home, result, route, hooks, options } = runningHub("claude", { credentials: [{ provider: "claude" }] }); hooks.beforeInstall = () => { diff --git a/src/reconciler/apply.ts b/src/reconciler/apply.ts index 9405e96b..8ce42673 100644 --- a/src/reconciler/apply.ts +++ b/src/reconciler/apply.ts @@ -128,8 +128,9 @@ async function reconcile(plan: Plan, options: ApplyOptions): Promise<{ report: R }; for (const adapter of adapters.filter((each) => !afterSteps.includes(each))) { const result = await adapter.apply(gated.plan, context).catch(async (error: unknown) => { - await configure(); - throw error; + const also = await configure().then(() => undefined, (second: unknown) => second); + if (also === undefined) throw error; + throw new Error(`${String((error as Error)?.message ?? error)} Hedge also could not set your provider settings: ${String((also as Error)?.message ?? also)}`); }); report.actions.push(...result.actions); report.findings.push(...result.findings); From 948e708e09455f9cd854d14ad8e0492ec5b54cd5 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:06:34 +0000 Subject: [PATCH 30/32] Prove a link at the panel's page or plugin fails the route check with the panel on, pinned bytes or not Co-Authored-By: Claude Opus 5.5 --- packs/hub/lib/cli-proxy-panel.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packs/hub/lib/cli-proxy-panel.test.ts b/packs/hub/lib/cli-proxy-panel.test.ts index 61c7c390..dc4cb075 100644 --- a/packs/hub/lib/cli-proxy-panel.test.ts +++ b/packs/hub/lib/cli-proxy-panel.test.ts @@ -245,3 +245,20 @@ test("once the profile turns the panel off, the route check on the hub fails whi expect(await cliProxy("check", hub({ home }).machine)).toBe(0); } }); + +test("with the panel on, the route check on the hub fails while the panel's page or plugin is a link, even to a file with the pinned bytes, and passes once you remove the link and run install", async () => { + for (const at of ["page", "plugin"] as const) { + const { machine, home } = hub({ panel }); + expect(await cliProxy("install", machine)).toBe(0); + const elsewhere = join(home, `pinned-${at}`); + writeFileSync(elsewhere, readFileSync(files(home)[at])); + rmSync(files(home)[at]); + symlinkSync(elsewhere, files(home)[at]); + const run = hub({ home, panel }); + expect(await cliProxy("check", run.machine)).toBe(1); + expect(run.printed.join("\n")).toContain(`${files(home)[at]} is a link, which Hedge does not follow or replace. Remove it, then run hedge cli-proxy install.`); + rmSync(files(home)[at]); + expect(await cliProxy("install", hub({ home, panel }).machine)).toBe(0); + expect(await cliProxy("check", hub({ home, panel }).machine)).toBe(0); + } +}); From bd521d3a701eaaeee0c0556d3f599577ca119181 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:39:23 +0000 Subject: [PATCH 31/32] Write the proxy route from apply only, and remove it only by its record, as one unit hedge cli-proxy connect saves the key and runs hedge apply, so every route Hedge writes has a record. Apply writes a provider's route keys together or not at all, removes them together, and keeps the route with a To do when removing it would send a key of yours to Anthropic or leave a Codex selector without its table. The exact-value fallback is gone. Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 13 +- packs/hub/hub.test.ts | 94 ++++++--- packs/hub/lib/cli-proxy-forwards.test.ts | 14 +- packs/hub/lib/cli-proxy-testing.ts | 37 +++- packs/hub/lib/cli-proxy.test.ts | 196 ++++++++---------- packs/hub/lib/cli-proxy.ts | 87 +++----- packs/hub/lib/links.ts | 4 +- packs/hub/skills/hedge-hub/SKILL.md | 12 +- src/adapters/provider-config/adapter.ts | 42 ++-- .../provider-config/provider-config.test.ts | 40 ++-- src/adapters/provider-config/proxy-route.ts | 28 ++- 11 files changed, 324 insertions(+), 243 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index a9153464..709b82f5 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -302,10 +302,15 @@ and Hedge does not wrap it in a unit. pack owns only the probe, a check. The config items say `after: route`. - *Apply while the proxy does not answer:* the gate fails, the two items leave the plan, and the adapter removes its own values. Claude Code - uses its own sign-in. A key that holds exactly the pack's value also - goes when no apply recorded it, as after `hedge cli-proxy connect`, - and while a pack source cannot be fetched. The items that wait for - one check go together, so they all stay when one holds another value. + uses its own sign-in. The adapter removes only the keys that its + record names, also while a pack source cannot be fetched. + `hedge cli-proxy connect` writes neither key: it saves the proxy key and + runs `hedge apply`, so every route Hedge writes has a record. + - *One route:* the adapter writes the route keys of a provider together + or not at all, and removes them together. When removing them would + leave a key of yours that Claude Code sends to Anthropic, or a Codex + `model_provider` that names a table that is gone, the route stays and + apply shows a `To do:`. - *Until recovery:* each `hedge apply` and `hedge doctor` reports the failed check. The first apply whose probe passes writes both keys again. Between applies nothing changes the route. diff --git a/packs/hub/hub.test.ts b/packs/hub/hub.test.ts index ccf00c54..79f50fe3 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -7,7 +7,7 @@ import { providerConfigAdapter } from "../../src/adapters/provider-config/adapte import { apply, check } from "../../src/reconciler/apply.ts"; import { boxCommand } from "./lib/box-command.ts"; import { cliProxy, codexProviderTable, proxyUrl, type ProxyMachine } from "./lib/cli-proxy.ts"; -import { clientKey, dataDir, proxyMachine, type Proxy } from "./lib/cli-proxy-testing.ts"; +import { clientKey, dataDir, hedgeApply, hedgeProxy, proxyMachine, proxyOnly, type Proxy } from "./lib/cli-proxy-testing.ts"; import { Coder } from "./lib/coder.ts"; import { fakeMachine } from "./lib/testing.ts"; import type { AvailableSource, MachineFile, MachineRole, Plan, Profile } from "../../src/pack/types.ts"; @@ -312,25 +312,55 @@ describe("the Own-account balancing workflow", () => { }); const connectedDevbox = (files: Record = {}) => { - const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }, { role: "devbox" }); + const { machine, home, printed } = proxyMachine((prefix) => temp.make(prefix), { plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }, { role: "devbox" }); for (const [path, text] of Object.entries(files)) { mkdirSync(join(home, path, ".."), { recursive: true }); writeFileSync(join(home, path), text); } - const off = plan({ packs: ["hub"], plan: "both" }, devbox); - const applyOff = () => apply({ ...off, config: off.config.filter((entry) => entry.pack === "hub"), checks: [], steps: [], jobs: [], human: [] }, { home, adapters: [providerConfigAdapter()], run: async () => 0 }); + const on = proxyOnly(plan({ ...balancing, plan: "both" }, devbox)); + let profile = on; + const applyNow = hedgeApply(machine, () => profile); + const applyWith = (next: Plan) => { + profile = next; + const applied = applyNow(); + profile = on; + return applied; + }; + const applyOff = () => applyWith(proxyOnly(plan({ packs: ["hub"], plan: "both" }, devbox))); const codex = () => parse(readFileSync(join(home, ".codex/config.toml"), "utf8")); - return { machine, home, applyOff, codex }; + const claude = () => JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")); + return { machine, on, printed, applyWith, applyOff, codex, claude }; }; + const helper = 'cat "$HOME/.config/hedge/cli-proxy.key"'; + + test("on a devbox, connect routes Claude Code and Codex through hedge apply, and turning balancing off takes both routes off at the next apply", async () => { + const { machine, applyOff, codex, claude } = connectedDevbox(); + expect(await cliProxy("connect", machine)).toBe(0); + expect(codex()).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": codexProviderTable } }); + expect(claude()).toEqual({ env: { ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: helper }); + expect((await applyOff()).code).toBe(0); + expect(codex()).toEqual({}); + expect(claude()).toEqual({}); + }); - test("on a devbox that connect sent to the proxy, with no record of it, turning balancing off takes Hedge's exact Claude and Codex routes off at the next apply", async () => { - const { machine, home, applyOff, codex } = connectedDevbox(); + test("after an earlier apply wrote only the hedge-proxy Codex table, connect selects it, and turning balancing off takes the table and the selector off together", async () => { + const { machine, on, applyWith, applyOff, codex } = connectedDevbox({ ".config/hedge/cli-proxy.key": `${clientKey}\n` }); + expect((await applyWith({ ...on, config: on.config.filter((entry) => entry.key !== "model_provider") })).code).toBe(0); + expect(codex()).toEqual({ model_providers: { "hedge-proxy": codexProviderTable } }); expect(await cliProxy("connect", machine)).toBe(0); expect(codex()).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": codexProviderTable } }); - expect(existsSync(join(home, ".config/hedge/provider-fields.json"))).toBe(false); - expect((await applyOff()).ok).toBe(true); + expect((await applyOff()).code).toBe(0); expect(codex()).toEqual({}); - expect(JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8"))).toEqual({}); + }); + + test("connect beside a hedge-proxy Codex table of your own with another name selects nothing, and turning balancing off leaves your table", async () => { + const mine = { model_providers: { "hedge-proxy": { ...codexProviderTable, name: "Mine" } } }; + const { machine, printed, applyOff, codex } = connectedDevbox({ ".codex/config.toml": stringify(mine) }); + expect(await cliProxy("connect", machine)).toBe(0); + expect(codex()).toEqual(mine); + expect(printed.at(-1)).toBe("Codex keeps its own sign-in. The hedge apply report above says why."); + expect((await applyOff()).code).toBe(0); + expect(codex()).toEqual(mine); }); test("your own hedge-proxy selector with a Codex table of your own stays, selector and table, after connect and after turning balancing off", async () => { @@ -338,10 +368,23 @@ describe("the Own-account balancing workflow", () => { const { machine, applyOff, codex } = connectedDevbox({ ".codex/config.toml": stringify(mine) }); expect(await cliProxy("connect", machine)).toBe(0); expect(codex()).toEqual(mine); - expect((await applyOff()).ok).toBe(true); + expect((await applyOff()).code).toBe(0); expect(codex()).toEqual(mine); }); + test("Hedge's Claude route beside your own ANTHROPIC_AUTH_TOKEN stays when balancing turns off, with a To do, so the token never goes to Anthropic, and leaves once you remove the token", async () => { + const { machine, applyOff, claude } = connectedDevbox({ ".claude/settings.json": JSON.stringify({ env: { ANTHROPIC_AUTH_TOKEN: "my-token" } }) }); + expect(await cliProxy("connect", machine)).toBe(0); + const routed = { env: { ANTHROPIC_AUTH_TOKEN: "my-token", ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: helper }; + expect(claude()).toEqual(routed); + const kept = await applyOff(); + expect(claude()).toEqual(routed); + expect(kept.stdout).toContain("To do: Kept Hedge's proxy route in .claude/settings.json: without it, Claude Code would send your env.ANTHROPIC_AUTH_TOKEN to Anthropic. Remove env.ANTHROPIC_AUTH_TOKEN from .claude/settings.json, then run hedge apply to take the route out."); + writeFileSync(join(machine.home, ".claude/settings.json"), JSON.stringify({ env: { ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: helper })); + expect((await applyOff()).code).toBe(0); + expect(claude()).toEqual({}); + }); + test("sends Claude Code to the loopback proxy, and keeps the client key out of the settings: Claude reads it from a private file", () => { const config = plan(balancing, devbox).config.filter((entry) => entry.workflow === "own-account-balancing"); expect(Object.fromEntries(config.filter((entry) => entry.provider === "claude").map((entry) => [entry.key, entry.value]))).toEqual({ "env.ANTHROPIC_BASE_URL": "http://127.0.0.1:8317", apiKeyHelper: "cat \"$HOME/.config/hedge/cli-proxy.key\"" }); @@ -395,23 +438,18 @@ describe("the Own-account balancing workflow", () => { expect(reports[1]!.actions).toEqual([]); }); - test("also removes the Claude settings that connect wrote before any apply", async () => { - const home = homeWith({ ".claude/settings.json": JSON.stringify({ model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: 'cat "$HOME/.config/hedge/cli-proxy.key"' }) }); - const { claude, reports } = await applied(home, "down"); + test("takes off the Claude route that an earlier apply wrote even while a pack source cannot be fetched", async () => { + const home = homeWith({ ".claude/settings.json": JSON.stringify({ model: "opus" }) }); + expect((await applied(home, both, 1)).claude.apiKeyHelper).toBe('cat "$HOME/.config/hedge/cli-proxy.key"'); + const { claude, reports } = await applied(home, "down", 2, offline); expect(claude).toEqual({ model: "opus" }); expect(reports[1]!.actions).toEqual([]); }); - test("removes the settings that connect wrote even while a pack source cannot be fetched", async () => { - const connected = { model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: 'cat "$HOME/.config/hedge/cli-proxy.key"' }; - const { claude, reports } = await applied(homeWith({ ".claude/settings.json": JSON.stringify(connected) }), "down", 2, offline); - expect(claude).toEqual({ model: "opus" }); - expect(reports[1]!.actions).toEqual([]); - }); - - test("removes the Codex settings that connect wrote even while a pack source cannot be fetched", async () => { - const connected = { model: "gpt-6.1-sol", model_provider: "hedge-proxy", model_providers: { "hedge-proxy": codexProviderTable } }; - const { codex, reports } = await applied(homeWith({ ".codex/config.toml": stringify(connected) }), "down", 2, offline); + test("takes off the Codex route that an earlier apply wrote even while a pack source cannot be fetched", async () => { + const home = homeWith({ ".codex/config.toml": stringify({ model: "gpt-6.1-sol" }) }); + expect((await applied(home, both, 1)).codex.model_provider).toBe("hedge-proxy"); + const { codex, reports } = await applied(home, "down", 2, offline); expect(codex).toEqual({ model: "gpt-6.1-sol" }); expect(reports[1]!.actions).toEqual([]); }); @@ -445,14 +483,6 @@ describe("the Own-account balancing workflow", () => { }); describe("apply on a hub whose proxy holds none of your subscriptions", () => { - const proxyOnly = (result: Plan): Plan => ({ ...result, files: [], jobs: [], human: [], steps: result.steps.filter((step) => step.id === "cli-proxy"), checks: result.checks.filter((check) => check.workflow === "own-account-balancing"), config: result.config.filter((entry) => entry.workflow === "own-account-balancing") }); - const hedgeProxy = (machine: ProxyMachine, engineRoot: string | undefined) => async (command: string[]) => { - const [shell, launcher, group, mode, ...rest] = command; - const agent = mode === "check" && rest.join(" ") === "codex" ? "codex" : "claude"; - const known = (["install", "doctor", "accounts", "check"] as const).find((name) => name === mode); - if (shell !== "/bin/sh" || !engineRoot || launcher !== join(engineRoot, "hedge") || group !== "cli-proxy" || !known || (rest.length && agent !== "codex")) throw new Error(`The hub pack runs no ${command.join(" ")}`); - return cliProxy(known, machine, agent).catch(() => 1); - }; const emptyHub = (ai: "claude" | "codex", credentials: Array<{ provider: string }> = []) => { const { machine, home } = proxyMachine((prefix) => temp.make(prefix), { plan: ai, devboxes: [] }, { credentials, models: [] }); const result = proxyOnly(plan({ ...balancing, plan: ai }, hub)); diff --git a/packs/hub/lib/cli-proxy-forwards.test.ts b/packs/hub/lib/cli-proxy-forwards.test.ts index 5ea06627..d651efe8 100644 --- a/packs/hub/lib/cli-proxy-forwards.test.ts +++ b/packs/hub/lib/cli-proxy-forwards.test.ts @@ -4,6 +4,7 @@ import { join } from "node:path"; import { parse } from "smol-toml"; import { tempDirs } from "../../../src/testing/git.ts"; import { cliProxy, waitingLines, type ProxyMachine } from "./cli-proxy.ts"; +import { balancingPlan, hedgeApply } from "./cli-proxy-testing.ts"; import { handOver, hubForward } from "./links.ts"; import { roleHome } from "./run-testing.ts"; import { proxyOn, readBoxSettings } from "./machine.ts"; @@ -97,7 +98,7 @@ test("a box handed over to a proxy that serves no model yet keeps its own sign-i const box = (stdin: string): ProxyMachine => ({ home: boxHome, uid: 1000, user: "me", plan: "claude", devboxes: [], release: { version: "", url: "", sha256: "" }, run: async () => ok, fetch: async () => Response.json({ data: [], object: "list" }), now: () => 0, sleep: async () => {}, readStdin: async () => stdin, randomKey: () => clientKey, print: (line) => printed.push(line) }); const { coder } = fakeCoder({ ssh: async (_name, command, stdin) => (command.includes("cli-proxy connect") ? { ...ok, code: await cliProxy("connect", box(stdin ?? "")), stdout: printed.join("\n") } : ok) }); await handOver(coder, hub.machine, "box-lane"); - expect(hub.lines).toContain("box-lane: the proxy lists no Claude model yet. Run hedge apply on box-lane, so that Claude Code there uses its own sign-in until the proxy serves it."); + expect(hub.lines).toContain("box-lane: the proxy lists no Claude model yet, so Claude Code there keeps its own sign-in until the proxy serves it."); expect(existsSync(join(boxHome, ".claude/settings.json"))).toBe(false); }); @@ -112,15 +113,20 @@ test("a handover prints only the hub's own words for a box line that is exactly expect(hub.lines.join("\n")).not.toContain("\u001b"); const exact = fakeCoder({ ssh: async (_name, command) => (command.includes("cli-proxy connect") ? { ...ok, stdout: `${waitingLines.codex}\n` } : ok) }); await handOver(exact.coder, hub.machine, "box-lane"); - expect(hub.lines.filter((line) => line.startsWith("box-lane:"))).toEqual(["box-lane: the proxy lists no Codex model yet. Run hedge apply on box-lane, so that Codex there uses its own sign-in until the proxy serves it."]); + expect(hub.lines.filter((line) => line.startsWith("box-lane:"))).toEqual(["box-lane: the proxy lists no Codex model yet, so Codex there keeps its own sign-in until the proxy serves it."]); }); -test("a box handed over on the Codex plan sends Codex through the proxy without another apply", async () => { +test("a box handed over on the Codex plan sends Codex through the proxy with the hedge apply that connect runs", async () => { const hub = fakeMachine(temp.make("hedge-hub-"), { settings: balancing, run: async () => ({ ...ok, code: 1 }) }); writeHome(hub.machine.home, { ".config/hedge/cli-proxy.key": `${clientKey}\n` }); const boxHome = roleHome(temp.make("hedge-box-"), "devbox"); const fetch = async () => Response.json({ data: [{ id: "gpt-6.1-sol" }] }); - const box = (stdin: string): ProxyMachine => ({ home: boxHome, uid: 1000, user: "me", plan: "codex", devboxes: [], release: { version: "", url: "", sha256: "" }, run: async () => ok, fetch, now: () => 0, sleep: async () => {}, readStdin: async () => stdin, randomKey: () => clientKey, print: () => {} }); + const codexPlan = await balancingPlan((prefix) => temp.make(prefix), "codex"); + const box = (stdin: string): ProxyMachine => { + const machine: ProxyMachine = { home: boxHome, uid: 1000, user: "me", plan: "codex", devboxes: [], release: { version: "", url: "", sha256: "" }, run: async () => ok, fetch, now: () => 0, sleep: async () => {}, readStdin: async () => stdin, randomKey: () => clientKey, print: () => {} }; + hedgeApply(machine, () => codexPlan); + return machine; + }; const { coder } = fakeCoder({ ssh: async (_name, command, stdin) => (command.includes("cli-proxy connect") ? { ...ok, code: await cliProxy("connect", box(stdin ?? "")) } : ok) }); await handOver(coder, hub.machine, "box-lane"); expect(parse(readFileSync(join(boxHome, ".codex/config.toml"), "utf8")).model_provider).toBe("hedge-proxy"); diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts index 8486ed86..eb1906b6 100644 --- a/packs/hub/lib/cli-proxy-testing.ts +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -1,6 +1,10 @@ import { existsSync, writeFileSync } from "node:fs"; import { join } from "node:path"; -import { proxyUrl, type ProxyMachine } from "./cli-proxy.ts"; +import { providerConfigAdapter } from "../../../src/adapters/provider-config/adapter.ts"; +import type { AiPlan, MachineFile, Plan } from "../../../src/pack/types.ts"; +import { apply } from "../../../src/reconciler/apply.ts"; +import { devbox, engineSource, resolveEngine, skillSources } from "../../../src/testing/packs.ts"; +import { applyCommand, cliProxy, proxyUrl, type ProxyMachine } from "./cli-proxy.ts"; import { fakeRun, roleHome } from "./run-testing.ts"; export const clientKey = "c1".repeat(32); @@ -53,3 +57,34 @@ export function proxyMachine(make: (prefix: string) => string, fields: Partial

({ ...result, files: [], jobs: [], human: [], steps: result.steps.filter((step) => step.id === "cli-proxy"), checks: result.checks.filter((check) => check.workflow === "own-account-balancing"), config: result.config.filter((entry) => entry.workflow === "own-account-balancing") }); + +export const hedgeProxy = (machine: ProxyMachine, engineRoot: string | undefined) => async (command: string[]) => { + const [shell, launcher, group, mode, ...rest] = command; + const agent = mode === "check" && rest.join(" ") === "codex" ? "codex" : "claude"; + const known = (["install", "doctor", "accounts", "check"] as const).find((name) => name === mode); + if (shell !== "/bin/sh" || !engineRoot || launcher !== join(engineRoot, "hedge") || group !== "cli-proxy" || !known || (rest.length && agent !== "codex")) throw new Error(`The hub pack runs no ${command.join(" ")}`); + return cliProxy(known, machine, agent).catch(() => 1); +}; + +const answers = { "hub.alias": "my-hub", "hub.coderUrl": "https://coder.example.com", "hub.boxTemplate": "linux" }; + +export async function balancingPlan(make: (prefix: string) => string, plan: AiPlan, on = true, machine: MachineFile = devbox): Promise { + const engine = await engineSource(); + return proxyOnly(resolveEngine([engine, ...skillSources(make("skills-"), engine)], { settings: answers, packs: ["hub"], workflows: { "own-account-balancing": on }, plan }, machine)); +} + +const label = (finding: { todo?: true; repair?: string }) => (finding.todo || finding.repair === "human" ? "To do" : finding.repair ? "Fix" : "Note"); + +export function hedgeApply(machine: ProxyMachine, plan: () => Plan) { + const quiet: ProxyMachine = { ...machine, print: () => {} }; + const run = async () => { + const report = await apply(plan(), { home: machine.home, adapters: [providerConfigAdapter()], run: hedgeProxy(quiet, plan().engineRoot) }).catch((error: Error) => error); + if (report instanceof Error) return { code: 1, stdout: "", stderr: `hedge: ${report.message}\n` }; + return { report, code: report.ok ? 0 : 1, stdout: report.findings.map((finding) => `${label(finding)}: ${finding.say}\n`).join(""), stderr: "" }; + }; + const outer = machine.run; + machine.run = async (command, options) => (command.join(" ") === applyCommand.join(" ") ? run() : outer(command, options)); + return run; +} diff --git a/packs/hub/lib/cli-proxy.test.ts b/packs/hub/lib/cli-proxy.test.ts index 62482cc3..ceb94546 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -1,10 +1,11 @@ -import { afterEach, expect, test } from "bun:test"; +import { afterEach, beforeAll, expect, test } from "bun:test"; import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { parse, stringify } from "smol-toml"; import { tempDirs } from "../../../src/testing/git.ts"; -import { claudeRoute, cliProxy, keyHelper, proxyUrl, type ProxyMachine } from "./cli-proxy.ts"; -import { archive, clientKey, dataDir, otherKey, proxyMachine, release, type Proxy } from "./cli-proxy-testing.ts"; +import { cliProxy, keyHelper, proxyUrl, type ProxyMachine } from "./cli-proxy.ts"; +import { archive, balancingPlan, clientKey, dataDir, hedgeApply, otherKey, proxyMachine, release, type Proxy } from "./cli-proxy-testing.ts"; +import type { AiPlan, Plan } from "../../../src/pack/types.ts"; import { fakeRun, modeOf, roleHome } from "./run-testing.ts"; const temp = tempDirs(); @@ -28,6 +29,18 @@ const hubMachine = (fields: Partial = {}, proxy: Proxy = {}, optio const client = (fields: Partial = {}, proxy: Proxy = {}) => hubMachine(fields, proxy, { role: "devbox" }); +let balancing: Record; +beforeAll(async () => { + const make = (prefix: string) => temp.make(prefix); + balancing = { claude: await balancingPlan(make, "claude"), codex: await balancingPlan(make, "codex"), both: await balancingPlan(make, "both") }; +}); + +const connecting = (fields: Partial = {}, proxy: Proxy = {}) => { + const box = client(fields, proxy); + hedgeApply(box.machine, () => balancing[box.machine.plan ?? "claude"]); + return box; +}; + function connected(home: string, settings: Record = {}) { mkdirSync(join(home, ".config/hedge"), { recursive: true }); mkdirSync(join(home, ".claude"), { recursive: true }); @@ -276,92 +289,76 @@ test("on the both plan the doctor still asks for Claude subscriptions, and count expect(claudeOnly.printed.join("\n")).toContain("with 2 of your own Claude subscriptions."); }); -test("connect on a Codex plan checks the proxy for a Codex model, sends Codex to the proxy, and leaves Claude Code alone", async () => { - const { machine, home, printed } = client({ plan: "codex" }, { models: ["gpt-6.1-sol"] }); +test("connect on a Codex plan checks the proxy for a Codex model, and hedge apply sends Codex to the proxy and leaves Claude Code alone", async () => { + const { machine, home, printed } = connecting({ plan: "codex" }, { models: ["gpt-6.1-sol"] }); codexRouted(home, mine); expect(await cliProxy("connect", machine)).toBe(0); expect(readFileSync(keyFile(home), "utf8")).toBe(`${clientKey}\n`); expect(readCodex(home)).toEqual({ ...mine, model_provider: "hedge-proxy", model_providers: { ...mine.model_providers, "hedge-proxy": hedgeProxy } }); expect(existsSync(settingsFile(home))).toBe(false); - expect(printed).toEqual([`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`]); + expect(printed.at(-1)).toBe(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); expect(await cliProxy("check", machine, "codex")).toBe(0); - const noCodex = client({ plan: "codex" }); + const noCodex = connecting({ plan: "codex" }); expect(await cliProxy("connect", noCodex.machine)).toBe(0); - expect(noCodex.printed).toEqual([`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`]); + expect(noCodex.printed.at(-1)).toBe(`The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it.`); expect(readFileSync(keyFile(noCodex.home), "utf8")).toBe(`${clientKey}\n`); expect(existsSync(codexFile(noCodex.home))).toBe(false); expect(await cliProxy("check", noCodex.machine, "codex")).toBe(4); - const theirs = client({ plan: "codex" }, { models: ["gpt-6.1-sol"] }); + const theirs = connecting({ plan: "codex" }, { models: ["gpt-6.1-sol"] }); codexRouted(theirs.home, { ...mine, model_provider: "work" }); - await expect(cliProxy("connect", theirs.machine)).rejects.toThrow("Keep your Codex setting model_provider"); + expect(await cliProxy("connect", theirs.machine)).toBe(0); expect(readCodex(theirs.home)).toEqual({ ...mine, model_provider: "work" }); - expect(existsSync(keyFile(theirs.home))).toBe(false); + expect(theirs.printed).toContain("Note: Kept your model_provider in .codex/config.toml. Run hedge apply --adopt .codex/config.toml to take Hedge's value."); + expect(theirs.printed.at(-1)).toBe("Codex keeps its own sign-in. The hedge apply report above says why."); }); - -test("connect on the both plan sends Claude Code to the proxy, and Codex too while the proxy serves it", async () => { - const served = client({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); +test("connect on the both plan sends Claude Code to the proxy through hedge apply, and Codex too while the proxy serves it", async () => { + const served = connecting({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); expect(await cliProxy("connect", served.machine)).toBe(0); expect(readJson(settingsFile(served.home)).apiKeyHelper).toBe(keyHelper); expect(readCodex(served.home)).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": hedgeProxy } }); - const claudeOnly = client({ plan: "both" }); + const claudeOnly = connecting({ plan: "both" }); expect(await cliProxy("connect", claudeOnly.machine)).toBe(0); expect(readJson(settingsFile(claudeOnly.home)).apiKeyHelper).toBe(keyHelper); expect(existsSync(codexFile(claudeOnly.home))).toBe(false); - expect(claudeOnly.printed.join("\n")).toContain("lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it."); + expect(claudeOnly.printed.join("\n")).toContain("lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it."); }); - -test("connect never selects a hedge-proxy table that cannot reach the proxy with this machine's key", async () => { +test("connect never selects a hedge-proxy table of yours, such as one that cannot reach the proxy with this machine's key", async () => { const { auth: _auth, ...keyless } = hedgeProxy; const config = { model_providers: { "hedge-proxy": keyless } }; - const codexOnly = client({ plan: "codex" }, { models: ["gpt-6.1-sol"] }); - codexRouted(codexOnly.home, config); - await expect(cliProxy("connect", codexOnly.machine)).rejects.toThrow("which does not reach the proxy with this machine's key. Nothing changed."); - expect(readCodex(codexOnly.home)).toEqual(config); - expect(existsSync(keyFile(codexOnly.home))).toBe(false); - - const both = client({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); - codexRouted(both.home, config); - expect(await cliProxy("connect", both.machine)).toBe(0); - expect(readCodex(both.home)).toEqual(config); - expect(both.printed.join("\n")).toContain("Codex keeps its own sign-in."); + for (const plan of ["codex", "both"] as const) { + const box = connecting({ plan }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); + codexRouted(box.home, config); + expect(await cliProxy("connect", box.machine)).toBe(0); + expect(readCodex(box.home)).toEqual(config); + expect(box.printed.at(-1)).toBe("Codex keeps its own sign-in. The hedge apply report above says why."); + } }); - -test("on the both plan, a Codex config that connect cannot write keeps Codex on its own sign-in, and Claude Code still connects", async () => { - const { machine, home, printed } = client({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); +test("on the both plan, a Codex config that hedge apply cannot write fails connect with apply's reason, and keeps both settings files as they were", async () => { + const { machine, home, printed } = connecting({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); codexRouted(home, mine); chmodSync(join(home, ".codex"), 0o500); try { - expect(await cliProxy("connect", machine)).toBe(0); + await expect(cliProxy("connect", machine)).rejects.toThrow("Saved the key, but hedge apply exited 1"); } finally { chmodSync(join(home, ".codex"), 0o700); } - expect(readJson(settingsFile(home)).apiKeyHelper).toBe(keyHelper); + expect(existsSync(settingsFile(home))).toBe(false); expect(readCodex(home)).toEqual(mine); - expect(printed.at(-1)).toEndWith("Codex keeps its own sign-in."); + expect(printed.join("\n")).toContain("EACCES"); }); - -test("connect leaves a Codex config that becomes a link while it checks the proxy", async () => { - const { machine, home, printed } = client({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); - codexRouted(home, mine); +test("connect leaves a Codex config behind a link, and still sends Claude Code to the proxy", async () => { + const { machine, home, printed } = connecting({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); const outside = temp.make("elsewhere-"); - writeFileSync(join(outside, "config.toml"), readFileSync(codexFile(home), "utf8")); - const fetch = machine.fetch; - machine.fetch = async (input, init) => { - if (!existsSync(join(home, ".codex", "moved"))) { - rmSync(join(home, ".codex"), { recursive: true }); - symlinkSync(outside, join(home, ".codex")); - writeFileSync(join(outside, "moved"), ""); - } - return fetch(input, init); - }; + writeFileSync(join(outside, "config.toml"), stringify(mine)); + symlinkSync(outside, join(home, ".codex")); expect(await cliProxy("connect", machine)).toBe(0); expect(parse(readFileSync(join(outside, "config.toml"), "utf8"))).toEqual(mine); - expect(printed.at(-1)).toEndWith("Codex keeps its own sign-in."); + expect(readJson(settingsFile(home)).apiKeyHelper).toBe(keyHelper); + expect(printed.at(-1)).toBe("Codex keeps its own sign-in. The hedge apply report above says why."); }); - test("on a devbox, plan names the agents that the plan sends through the proxy", async () => { for (const [ai, agents] of [["claude", "Claude Code uses"], ["codex", "Codex uses"], ["both", "Claude Code and Codex use"]] as const) { const { machine, home, printed } = client({ plan: ai }); @@ -381,8 +378,8 @@ test("the doctor finds a changed unit and a proxy that listens beyond loopback", await expect(cliProxy("doctor", open.machine)).rejects.toThrow("loopback only"); }); -test("connect on a devbox checks the key against the proxy first, then saves it privately and sends Claude Code to the proxy", async () => { - const { machine, home, requests, printed } = client(); +test("connect on a devbox checks the key against the proxy first, then saves it privately, and hedge apply sends Claude Code to the proxy", async () => { + const { machine, home, requests, printed } = connecting(); mkdirSync(join(home, ".claude"), { recursive: true }); writeFileSync(settingsFile(home), JSON.stringify({ model: "opus", env: { MY_FLAG: "1" } })); expect(await cliProxy("connect", machine)).toBe(0); @@ -390,40 +387,37 @@ test("connect on a devbox checks the key against the proxy first, then saves it expect(readFileSync(keyFile(home), "utf8")).toBe(`${clientKey}\n`); expect(modeOf(statSync(keyFile(home)))).toBe("600"); expect(readJson(settingsFile(home))).toEqual({ model: "opus", env: { MY_FLAG: "1", ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: keyHelper }); - expect(modeOf(statSync(settingsFile(home)))).toBe("600"); expect(readFileSync(settingsFile(home), "utf8")).not.toContain(clientKey); - expect(printed.join("\n")).toContain("Restart"); + expect(printed.at(-1)).toBe(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); }); - -test("connect to a proxy that holds none of your subscriptions saves the key, keeps Claude Code on its own sign-in, and leaves the move to a later apply", async () => { - const { machine, home, printed } = client({}, { models: [] }); +test("connect to a proxy that holds none of your subscriptions saves the key, and hedge apply keeps Claude Code on its own sign-in", async () => { + const { machine, home, printed } = connecting({}, { models: [] }); mkdirSync(join(home, ".claude"), { recursive: true }); writeFileSync(settingsFile(home), JSON.stringify({ model: "opus" })); expect(await cliProxy("connect", machine)).toBe(0); expect(readFileSync(keyFile(home), "utf8")).toBe(`${clientKey}\n`); expect(readJson(settingsFile(home))).toEqual({ model: "opus" }); - expect(printed).toEqual([`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`]); + expect(printed.at(-1)).toBe(`The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. hedge apply keeps Claude Code on its own sign-in until the proxy serves it.`); expect(await cliProxy("check", machine)).toBe(4); - const refused = client({}, { models: [], refuses: true }); + const refused = connecting({}, { models: [], refuses: true }); await expect(cliProxy("connect", refused.machine)).rejects.toThrow("refuses this machine's key (HTTP 401). Nothing changed."); expect(existsSync(keyFile(refused.home))).toBe(false); }); - -test("connect again after the pool empties saves the key, and leaves taking the routes off to hedge apply", async () => { - const { machine, home, printed } = client({ plan: "both" }, { models: [] }); - connected(home); - codexRouted(home); - const before = [readFileSync(settingsFile(home), "utf8"), readFileSync(codexFile(home), "utf8")]; +test("connect again after the pool empties saves the key, and hedge apply takes off the routes an earlier connect put in", async () => { + const proxy: Proxy = { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }; + const { machine, home, printed } = connecting({ plan: "both" }, proxy); + expect(await cliProxy("connect", machine)).toBe(0); + expect(readCodex(home)).toEqual({ model_provider: "hedge-proxy", model_providers: { "hedge-proxy": hedgeProxy } }); + proxy.models = []; + printed.length = 0; expect(await cliProxy("connect", machine)).toBe(0); - expect([readFileSync(settingsFile(home), "utf8"), readFileSync(codexFile(home), "utf8")]).toEqual(before); - expect(printed).toEqual([ - `The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`, - `The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`, + expect(readJson(settingsFile(home))).toEqual({}); + expect(readCodex(home)).toEqual({}); + expect(printed.slice(-2)).toEqual([ + `The proxy at ${proxyUrl} lists no Claude model. On the hub, run hedge cli-proxy doctor. hedge apply keeps Claude Code on its own sign-in until the proxy serves it.`, + `The proxy at ${proxyUrl} lists no Codex model. Add one of your own Codex subscriptions to the hub's proxy. hedge apply keeps Codex on its own sign-in until the proxy serves it.`, ]); - expect(await cliProxy("check", machine)).toBe(4); - expect(await cliProxy("check", machine, "codex")).toBe(4); }); - test("connect refuses a model list it cannot read, and saves no key", async () => { const refused = client({}, { modelList: '{"data":[null,{"id":7}]}' }); await expect(cliProxy("connect", refused.machine)).rejects.toThrow("answers without a model list. Nothing changed."); @@ -463,21 +457,22 @@ test("with hub.proxyAccounts at 0, the doctor and accounts pass with an empty po await expect(cliProxy("doctor", hubMachine({ home, accounts: 0 }, { credentials: [{ provider: "claude" }], models: [] }).machine)).rejects.toThrow("lists no Claude model"); }); -test("connect changes nothing for a key that is not hexadecimal, a proxy that does not answer, or Claude settings that point elsewhere", async () => { - const bad = client({ readStdin: async () => "sk-not-a-key\n" }); +test("connect changes nothing for a key that is not hexadecimal or a proxy that does not answer, and hedge apply keeps Claude settings that point elsewhere", async () => { + const bad = connecting({ readStdin: async () => "sk-not-a-key\n" }); await expect(cliProxy("connect", bad.machine)).rejects.toThrow("hexadecimal"); - const down = client({}, { down: true }); + const down = connecting({}, { down: true }); await expect(cliProxy("connect", down.machine)).rejects.toThrow("Nothing changed"); expect(existsSync(keyFile(down.home))).toBe(false); expect(existsSync(settingsFile(down.home))).toBe(false); - const elsewhere = client(); + const elsewhere = connecting(); mkdirSync(join(elsewhere.home, ".claude"), { recursive: true }); - writeFileSync(settingsFile(elsewhere.home), JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://gateway.example.com" } })); - await expect(cliProxy("connect", elsewhere.machine)).rejects.toThrow("Keep your Claude setting env.ANTHROPIC_BASE_URL"); - expect(existsSync(keyFile(elsewhere.home))).toBe(false); + const gateway = { env: { ANTHROPIC_BASE_URL: "https://gateway.example.com" } }; + writeFileSync(settingsFile(elsewhere.home), JSON.stringify(gateway)); + expect(await cliProxy("connect", elsewhere.machine)).toBe(0); + expect(readJson(settingsFile(elsewhere.home))).toEqual(gateway); + expect(elsewhere.printed.at(-1)).toBe("Claude Code keeps its own sign-in. The hedge apply report above says why."); await expect(cliProxy("connect", hubMachine().machine)).rejects.toThrow("The hub has its own key"); }); - test("the check passes while the proxy answers the saved key, and keeps Claude Code on the proxy", async () => { const { machine, home, printed } = client(); connected(home); @@ -514,23 +509,25 @@ test("the check fails without a saved key, waits for the person while the proxy expect(existsSync(settingsFile(fresh.home))).toBe(false); }); -test("connect and disconnect take over the key helper and the Codex provider that fleet cli-proxy connect wrote, and keep your own", async () => { +test("disconnect takes off the key helper and the Codex provider that fleet cli-proxy connect wrote, and keeps your own, while connect leaves them to hedge apply --adopt", async () => { const fleetHelper = 'cat "$HOME/.config/fleet/cli-proxy.key"'; const fleetProxy = { name: "Fleet CLI proxy", base_url: `${proxyUrl}/v1`, wire_api: "responses", auth: { command: "/bin/sh", args: ["-c", fleetHelper] } }; const fleetCodex = { ...mine, model_provider: "fleet-proxy", model_providers: { ...mine.model_providers, "fleet-proxy": fleetProxy } }; - const both = client({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); + const fleetClaude = { model: "opus", env: { ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: fleetHelper }; + const both = connecting({ plan: "both" }, { models: ["claude-sonnet-5-5", "gpt-6.1-sol"] }); mkdirSync(join(both.home, ".claude"), { recursive: true }); - writeFileSync(settingsFile(both.home), JSON.stringify({ model: "opus", env: { ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: fleetHelper })); + writeFileSync(settingsFile(both.home), JSON.stringify(fleetClaude)); codexRouted(both.home, fleetCodex); expect(await cliProxy("connect", both.machine)).toBe(0); - expect(readJson(settingsFile(both.home))).toEqual({ model: "opus", env: { ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: keyHelper }); - expect(readCodex(both.home)).toEqual({ ...mine, model_provider: "hedge-proxy", model_providers: { ...mine.model_providers, "hedge-proxy": hedgeProxy } }); + expect(readJson(settingsFile(both.home))).toEqual(fleetClaude); + expect(readCodex(both.home)).toEqual(fleetCodex); + expect(both.printed).toContain("Note: Kept your apiKeyHelper in .claude/settings.json. Run hedge apply --adopt .claude/settings.json to take Hedge's value."); const left = client({ plan: "both" }); mkdirSync(join(left.home, ".claude"), { recursive: true }); - writeFileSync(settingsFile(left.home), JSON.stringify({ model: "opus", env: { ANTHROPIC_BASE_URL: proxyUrl }, apiKeyHelper: fleetHelper })); + writeFileSync(settingsFile(left.home), JSON.stringify(fleetClaude)); codexRouted(left.home, fleetCodex); writeFileSync(keyFile(left.home), `${clientKey}\n`, { mode: 0o600 }); @@ -539,7 +536,6 @@ test("connect and disconnect take over the key helper and the Codex provider tha expect(readJson(settingsFile(left.home))).toEqual({ model: "opus" }); expect(readCodex(left.home)).toEqual(mine); }); - test("disconnect removes only Hedge's own values: a base URL or key helper you set stays", async () => { const { machine, home } = client(); connected(home); @@ -558,17 +554,18 @@ test("disconnect removes Hedge's Claude settings and the key, and says how to si await expect(cliProxy("disconnect", hubMachine().machine)).rejects.toThrow("own-account-balancing=off"); }); -test("a linked Claude settings file stops connect and disconnect", async () => { - const { machine, home } = client(); +test("a linked Claude settings file stops disconnect, and hedge apply leaves it when connect runs", async () => { + const { machine, home, printed } = connecting(); connected(home); const real = join(temp.make("real-"), "settings.json"); - writeFileSync(real, readFileSync(settingsFile(home))); + writeFileSync(real, JSON.stringify({ model: "opus" })); rmSync(settingsFile(home)); symlinkSync(real, settingsFile(home)); await expect(cliProxy("disconnect", machine)).rejects.toThrow("Keep linked Claude settings path"); - await expect(cliProxy("connect", machine)).rejects.toThrow("Keep linked Claude settings path"); + expect(await cliProxy("connect", machine)).toBe(0); + expect(readJson(real)).toEqual({ model: "opus" }); + expect(printed.at(-1)).toBe("Claude Code keeps its own sign-in. The hedge apply report above says why."); }); - test("install refuses a linked auth folder or binary before it changes modes or runs anything", async () => { const auth = hubMachine(); const outside = temp.make("elsewhere-"); @@ -602,7 +599,7 @@ test("install that fails after it writes a new management key restarts the proxy }); test("connect keeps Claude settings that change while it checks the proxy", async () => { - const { machine, home } = client(); + const { machine, home } = connecting(); mkdirSync(join(home, ".claude"), { recursive: true }); writeFileSync(settingsFile(home), JSON.stringify({ model: "opus" })); const fetch = machine.fetch; @@ -635,17 +632,6 @@ test("connect waits for a forward that is still coming up, and gives up after a expect(refusedKey.requests).toHaveLength(1); }); -test("the Claude switch stops and keeps your edit when settings change between its read and its write", async () => { - const home = roleHome(temp.make("hedge-cli-proxy-"), "devbox"); - mkdirSync(join(home, ".claude"), { recursive: true }); - writeFileSync(settingsFile(home), JSON.stringify({ model: "opus" })); - const write = (await claudeRoute(home, true))!; - writeFileSync(settingsFile(home), JSON.stringify({ model: "opus", permissions: { deny: ["Bash(rm:*)"] } })); - await expect(write()).rejects.toThrow("changed while Hedge wrote it"); - expect(readJson(settingsFile(home))).toEqual({ model: "opus", permissions: { deny: ["Bash(rm:*)"] } }); - expect(readdirSync(join(home, ".claude"))).toEqual(["settings.json"]); -}); - test("an install that stops after it replaced the binary restarts the proxy on the next run", async () => { const { machine, home, calls } = hubMachine(); expect(await cliProxy("install", machine)).toBe(0); diff --git a/packs/hub/lib/cli-proxy.ts b/packs/hub/lib/cli-proxy.ts index 89873bad..bef05447 100644 --- a/packs/hub/lib/cli-proxy.ts +++ b/packs/hub/lib/cli-proxy.ts @@ -11,6 +11,7 @@ import { changedFiles, ownedFiles, readIfThere, removeUnits, requireInstalled, w import { refuseLinks, writePrivateFile, type Fetch } from "../../mcp-gateway/lib/client.ts"; import type { Run } from "../../mcp-gateway/lib/hub.ts"; import { isKeptForBoxes } from "../../../src/hub/names.ts"; +import { remoteLauncher } from "../../../src/hub/ssh.ts"; import { waitsForPerson } from "../../../src/pack/types.ts"; import { codexProvider, codexProviderTable, keyHelper } from "../../../src/adapters/provider-config/proxy-route.ts"; import { refreshCodexCatalog } from "./codex-catalog.ts"; @@ -309,8 +310,8 @@ const missingModel = { }; export const waitingLines = { - claude: `${missingModel.claude} Run hedge apply: it keeps Claude Code on its own sign-in until the proxy serves it.`, - codex: `${missingModel.codex} Run hedge apply: it keeps Codex on its own sign-in until the proxy serves it.`, + claude: `${missingModel.claude} hedge apply keeps Claude Code on its own sign-in until the proxy serves it.`, + codex: `${missingModel.codex} hedge apply keeps Codex on its own sign-in until the proxy serves it.`, }; function unserved(models: string[] | string, provider: "claude" | "codex"): string | undefined { @@ -330,30 +331,31 @@ async function answering(machine: ProxyMachine, key: string): Promise & { env?: Record }; const isObject = (value: unknown): value is Record => value !== null && typeof value === "object" && !Array.isArray(value); -export async function claudeRoute(home: string, on: boolean): Promise<(() => Promise) | undefined> { +async function readClaude(home: string) { const path = join(home, ".claude/settings.json"); await refuseLinks(home, path, "Claude settings path"); const text = await readIfThere(path); - if (text === undefined && !on) return undefined; const settings = text === undefined ? {} : (JSON.parse(text) as Settings); if (!isObject(settings) || (settings.env !== undefined && !isObject(settings.env))) throw new Error(`${path} is not a Claude settings object. Fix it, then run this again.`); + return { path, text, settings }; +} + +async function leaveClaude(home: string): Promise { + const { path, text, settings } = await readClaude(home); + if (text === undefined) return; const before = JSON.stringify(settings); const env = settings.env ?? {}; - if (on) { - for (const [name, current, value] of [["env.ANTHROPIC_BASE_URL", env.ANTHROPIC_BASE_URL, proxyUrl], ["apiKeyHelper", ownHelper(settings.apiKeyHelper) ? keyHelper : settings.apiKeyHelper, keyHelper]]) { - if (current !== undefined && current !== value) throw new Error(`Keep your Claude setting ${name} in ${path}: Hedge did not set it. Remove it to use the proxy.`); - } - settings.env = { ...env, ANTHROPIC_BASE_URL: proxyUrl }; - settings.apiKeyHelper = keyHelper; - } else { - if (env.ANTHROPIC_BASE_URL === proxyUrl) delete env.ANTHROPIC_BASE_URL; - if (settings.env && !Object.keys(env).length) delete settings.env; - if (ownHelper(settings.apiKeyHelper)) delete settings.apiKeyHelper; - } - if (JSON.stringify(settings) === before) return async () => {}; - return () => replaceUnchanged(path, text, `${JSON.stringify(settings, null, 2)}\n`); + if (env.ANTHROPIC_BASE_URL === proxyUrl) delete env.ANTHROPIC_BASE_URL; + if (settings.env && !Object.keys(env).length) delete settings.env; + if (ownHelper(settings.apiKeyHelper)) delete settings.apiKeyHelper; + if (JSON.stringify(settings) !== before) await replaceUnchanged(path, text, `${JSON.stringify(settings, null, 2)}\n`); } +const claudeRouted = async (home: string) => { + const { settings } = await readClaude(home).catch(() => ({ settings: {} as Settings })); + return settings.env?.ANTHROPIC_BASE_URL === proxyUrl && settings.apiKeyHelper === keyHelper; +}; + async function replaceUnchanged(path: string, before: string | undefined, after: string): Promise { await mkdir(dirname(path), { recursive: true, mode: 0o700 }); const partial = `${path}.${randomUUID()}.partial`; @@ -366,8 +368,6 @@ async function replaceUnchanged(path: string, before: string | undefined, after: } } -const sendClaude = async (home: string, on: boolean) => (await claudeRoute(home, on))?.(); - const codexUrl = codexProviderTable.base_url; const fleetProvider = "fleet-proxy"; const fleetProviderTable = { name: "Fleet CLI proxy", base_url: codexUrl, wire_api: "responses", auth: { command: "/bin/sh", args: ["-c", fleetKeyHelper] } }; @@ -436,57 +436,38 @@ async function codexUnserved(home: string, models: string[] | string): Promise isObject(table) && (["base_url", "wire_api", "auth"] as const).every((field) => Bun.deepEquals(table[field], codexProviderTable[field])); const ownTable = (path: string) => `${path} has its own ${codexProvider} provider table, which does not reach the proxy with this machine's key.`; -async function codexRoute(home: string): Promise Promise)> { +const codexRouted = async (home: string) => { const read = await readableCodex(home); - if (!read) return "Hedge cannot read ~/.codex/config.toml, or it is a link, so Hedge leaves it alone."; - const { path, text, config, providers } = read; - const table = providers?.[codexProvider]; - if (config.model_provider !== undefined && config.model_provider !== codexProvider) return `Keep your Codex setting model_provider in ${path}: Hedge did not set it. Remove it to use the proxy.`; - if (table !== undefined && !reachesProxy(table)) return ownTable(path); - config.model_provider = codexProvider; - config.model_providers = { ...providers, [codexProvider]: table ?? codexProviderTable }; - return () => replaceUnchanged(path, text, `${stringify(config)}\n`); -} + return read?.config.model_provider === codexProvider && reachesProxy(read.providers?.[codexProvider]); +}; -async function sendCodex(home: string, besideClaude: boolean): Promise { - const route = await codexRoute(home); - if (typeof route === "string") return route; - try { - await route(); - } catch (error) { - if (!besideClaude) throw error; - return (error as Error).message; - } -} +export const applyCommand = ["sh", "-c", `sh ${remoteLauncher} apply`]; +const applyLimitMs = 30 * 60_000; async function connect(machine: ProxyMachine): Promise { if (readRole(machine.home) === "hub") throw new Error("The hub has its own key. Run hedge cli-proxy install there."); const key = (await machine.readStdin()).trim(); if (!keyPattern.test(key)) throw new Error("Pass the proxy key from the hub on standard input: 64 hexadecimal characters."); - const agent = pooled(machine.plan); - if (agent === "claude") await claudeRoute(machine.home, true); - const codex = machine.plan === "codex" || machine.plan === "both" ? await codexRoute(machine.home) : undefined; - if (agent === "codex" && typeof codex === "string") throw new Error(`${codex} Nothing changed.`); const models = await answering(machine, key); if (typeof models === "string") throw new Error(`${models} Nothing changed.`); await refuseLinks(machine.home, keyPath(machine.home), `${what} path`); await mkdir(dirname(keyPath(machine.home)), { recursive: true, mode: 0o700 }); await writePrivateFile(keyPath(machine.home), `${key}\n`); - if (agent === "claude" && unserved(models, "claude")) machine.print(waitingLines.claude); - else if (agent === "claude") { - await sendClaude(machine.home, true); - machine.print(`Claude Code uses your own-account proxy at ${proxyUrl} now. Restart open Claude Code sessions.`); + const applied = await machine.run(applyCommand, { timeoutMs: applyLimitMs }); + for (const line of `${applied.stdout}${applied.stderr}`.split("\n").filter(Boolean)) machine.print(line); + if (applied.code !== 0) throw new Error(`Saved the key, but hedge apply exited ${applied.code}, so this machine may keep its own sign-in. Its report above says why. Run hedge apply again once you fixed it.`); + const agents: Agent[] = machine.plan === "both" ? ["claude", "codex"] : [pooled(machine.plan)]; + for (const agent of agents) { + const name = agent === "claude" ? "Claude Code" : "Codex"; + if (unserved(models, agent)) machine.print(waitingLines[agent]); + else if (await (agent === "claude" ? claudeRouted : codexRouted)(machine.home)) machine.print(`${name} uses your own-account proxy at ${proxyUrl} now. Restart open ${name} sessions.`); + else machine.print(`${name} keeps its own sign-in. The hedge apply report above says why.`); } - if (codex === undefined) return; - if (typeof codex !== "string" && unserved(models, "codex")) return machine.print(waitingLines.codex); - const failed = typeof codex === "string" ? codex : await sendCodex(machine.home, agent === "claude"); - if (failed) return machine.print(`${failed} Codex keeps its own sign-in.`); - machine.print(`Codex uses your own-account proxy at ${proxyUrl} now. Restart open Codex sessions.`); } async function disconnect(machine: ProxyMachine): Promise { if (readRole(machine.home) === "hub") throw new Error("On the hub, turn the workflow off instead: hedge packs --workflows own-account-balancing=off"); - await sendClaude(machine.home, false); + await leaveClaude(machine.home); const codex = await sendCodexHome(machine.home); await refuseLinks(machine.home, keyPath(machine.home), `${what} path`); await rm(keyPath(machine.home), { force: true }); diff --git a/packs/hub/lib/links.ts b/packs/hub/lib/links.ts index e22665b9..1ad56c76 100644 --- a/packs/hub/lib/links.ts +++ b/packs/hub/lib/links.ts @@ -72,8 +72,8 @@ async function handOverProxy(coder: Pick, here: Machine, box: stri const connected = await coder.ssh(box, `umask 077 && ${remoteHedge("cli-proxy connect")}`, readFileSync(key, "utf8")); if (connected.code !== 0) return here.say(`${box} keeps its own sign-in: hedge cli-proxy connect exited ${connected.code} there. To see why and hand over the same key again, run on the hub: hedge box run ${box} -- hedge cli-proxy connect < ~/${proxyKeyPath}`); const lines = connected.stdout.split("\n"); - if (lines.includes(waitingLines.claude)) here.say(`${box}: the proxy lists no Claude model yet. Run hedge apply on ${box}, so that Claude Code there uses its own sign-in until the proxy serves it.`); - if (lines.includes(waitingLines.codex)) here.say(`${box}: the proxy lists no Codex model yet. Run hedge apply on ${box}, so that Codex there uses its own sign-in until the proxy serves it.`); + if (lines.includes(waitingLines.claude)) here.say(`${box}: the proxy lists no Claude model yet, so Claude Code there keeps its own sign-in until the proxy serves it.`); + if (lines.includes(waitingLines.codex)) here.say(`${box}: the proxy lists no Codex model yet, so Codex there keeps its own sign-in until the proxy serves it.`); } export async function handOver(coder: Pick, here: Machine, box: string): Promise { diff --git a/packs/hub/skills/hedge-hub/SKILL.md b/packs/hub/skills/hedge-hub/SKILL.md index 0329bc95..3c80e1dd 100644 --- a/packs/hub/skills/hedge-hub/SKILL.md +++ b/packs/hub/skills/hedge-hub/SKILL.md @@ -151,8 +151,10 @@ the person's own subscriptions, never for someone else's. `model_provider` to `hedge-proxy`, a provider that reads the same key file. - **Devboxes.** The hub's SSH tunnel to each devbox carries port 8317. On the hub, `hedge cli-proxy doctor` prints one connect command per devbox; it - sends the key on standard input. Boxes get the key and connect when the hub - hands them over. + sends the key on standard input. Connect saves the key and runs + `hedge apply`, which writes the Claude and Codex settings. Connect never + writes them itself. Boxes get the key and connect when the hub hands them + over. - **When the proxy is down.** Hedge's Claude settings wait for `hedge cli-proxy check`, and its Codex settings wait for `hedge cli-proxy check codex`. Both checks only read. On the hub, both @@ -169,9 +171,9 @@ the person's own subscriptions, never for someone else's. the proxy holds none of that agent's subscriptions that work. Run `hedge cli-proxy doctor` on the hub to tell the two cases apart: it counts the subscriptions that work, and fails when one works but the proxy lists - no model for it. `hedge cli-proxy connect` then saves the key and changes no - agent settings; `hedge apply` keeps the agent on its own sign-in until - the proxy serves it, and then sends it there. Connect still changes nothing when the proxy + no model for it. `hedge cli-proxy connect` then saves the key, and the + `hedge apply` that it runs keeps the agent on its own sign-in until the + proxy serves it. A later apply then sends the agent there. Connect still changes nothing when the proxy does not answer or refuses the key. The first apply after the proxy answers again sends the agent back to the proxy. - **The panel.** The Proxy panel workflow is off by default and needs diff --git a/src/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 4d85b6c7..72e2a6d8 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -15,7 +15,7 @@ export type { CommandRunner } from "../run.ts"; /** owns says which of the provider's keys live in this file. */ type Target = { provider: Provider; path: string; format: Format; owns(path: string[]): boolean }; export type FieldRecord = { [target: string]: Field[] }; -type Merged = { record: Field[]; kept: string[]; reason: string; after?: string; document: Document }; +type Merged = { record: Field[]; kept: string[]; reason: string; after?: string; document: Document; todo?: string }; type FilesResult = AdapterResult & { plugins?: string[]; claudeEnabled?: Record; marketplaces?: Record; codexPlugins?: string[] }; export const recordTarget = ".config/hedge/provider-fields.json"; @@ -83,6 +83,7 @@ async function reconcileFiles(plan: Plan, home: string, dryRun: boolean, adopt: result.marketplaces = declaredMarketplaces(merged.document); } if (target.provider === "codex" && !link) result.codexPlugins = acceptedCodexPlugins(merged.document); + if (merged.todo) result.findings.push({ id: `route:${target.path}`, pack: "engine", say: merged.todo, todo: true }); if (merged.kept.length) result.findings.push({ id: `kept:${target.path}`, pack: "engine", say: `Kept your ${merged.kept.join(", ")} in ${target.path}. Run hedge apply --adopt ${target.path} to take Hedge's value.` }); if (merged.after !== undefined) writes.push({ target: target.path, before, after: merged.after, action: { target: target.path, operation: before === undefined ? "create" : "replace", reason: merged.reason } }); } @@ -113,16 +114,25 @@ function merge(target: Target, text: string | undefined, previous: Field[], want const kept: string[] = []; const changed: string[] = []; const removed: string[] = []; + const route = proxyRoute[target.provider]; + const onRoute = (path: string[]) => route?.paths.some((item) => samePath(item, path)) ?? false; + const oldOf = (field: Field) => previous.find((item) => samePath(item.path, field.path)); + const keeps = (field: Field) => { + const value = get(current, field.path); + const old = oldOf(field); + return !same(value, field.value) && value !== undefined && !(old && same(value, old.value)) && !adopt; + }; + const routeHeld = wanted.some((field) => onRoute(field.path) && keeps(field)); const reconcile = (fields: Field[]) => { const applied: Field[] = []; for (const field of fields) { - const value = get(current, field.path); - const old = previous.find((item) => samePath(item.path, field.path)); - if (!same(value, field.value) && value !== undefined && !(old && same(value, old.value)) && !adopt) { - kept.push(field.path.join(".")); + const old = oldOf(field); + if (keeps(field) || (routeHeld && onRoute(field.path))) { + if (keeps(field)) kept.push(field.path.join(".")); if (old) record.push(old); continue; } + const value = get(current, field.path); if (!same(value, field.value)) { set(current, field.path, field.value); changed.push(field.path.join(".")); @@ -145,25 +155,27 @@ function merge(target: Target, text: string | undefined, previous: Field[], want trust.push(...retained); } const all = [...wanted, ...trust]; + const leaving: Field[] = []; for (const field of previous) { if (all.some((item) => overlaps(item.path, field.path))) continue; if ((options.outage && !options.gated(field.path)) || keepsEdit(field)) record.push(field); - else if (get(current, field.path) !== undefined) { - remove(current, field.path); - removed.push(field.path.join(".")); - } + else if (get(current, field.path) !== undefined) leaving.push(field); } - const route = proxyRoute[target.provider] ?? []; - const hedges = route.every((field) => same(get(current, field.path), field.value)); - const wantedRoute = route.some((field) => all.some((item) => overlaps(item.path, field.path))); - const outageKeeps = options.outage && !route.some((field) => options.gated(field.path)); - for (const field of hedges && !wantedRoute && !outageKeeps ? route : []) { + const trial = structuredClone(current); + for (const field of leaving.filter((item) => onRoute(item.path))) remove(trial, field.path); + const stranded = leaving.some((field) => onRoute(field.path)) && !route?.strands(current) ? route?.strands(trial) : undefined; + for (const field of leaving) { + if (stranded && onRoute(field.path)) { + record.push(field); + continue; + } remove(current, field.path); removed.push(field.path.join(".")); } const reason = [changed.length ? `set ${changed.join(", ")}` : "", removed.length ? `remove ${removed.join(", ")}` : ""].filter(Boolean).join("; "); const after = target.format.encode(current); - return { record, kept, reason, after: after === original ? undefined : after, document: current }; + const todo = stranded && `Kept Hedge's proxy route in ${target.path}: without it, ${stranded.harm}. Remove ${stranded.path} from ${target.path}, then run hedge apply to take the route out.`; + return { record, kept, reason, after: after === original ? undefined : after, document: current, ...(todo ? { todo } : {}) }; } function acceptedPlugins(document: Document) { diff --git a/src/adapters/provider-config/provider-config.test.ts b/src/adapters/provider-config/provider-config.test.ts index 525697d6..420ae14e 100644 --- a/src/adapters/provider-config/provider-config.test.ts +++ b/src/adapters/provider-config/provider-config.test.ts @@ -70,20 +70,37 @@ function claudeCli(installed: string[] = []) { const adapters = (run = claudeCli().run) => [filesAdapter, providerConfigAdapter({ run })]; describe("provider config", () => { - test("removes a proxy base URL it recorded with a backup, and keeps every key Hedge has no record of, even one beside the proxy URL", async () => { + test("removes a proxy base URL it recorded with a backup, and keeps every key Hedge has no record of", async () => { const home = temp.make("home-"); await apply(plan([{ provider: "claude", key: "env.ANTHROPIC_BASE_URL", value: "http://127.0.0.1:8317", pack: "hub" }]), { home, adapters: adapters() }); - const before = { model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317", ANTHROPIC_AUTH_TOKEN: "proxy-key", MY_FLAG: "1" }, permissions: { allow: ["Bash(ls)"] } }; + const before = { model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317", MY_FLAG: "1" }, permissions: { allow: ["Bash(ls)"] } }; write(home, claude, before); const report = await apply(plan(), { home, adapters: adapters() }); - expect(read(home, claude)).toEqual({ model: "opus", env: { ANTHROPIC_AUTH_TOKEN: "proxy-key", MY_FLAG: "1" }, permissions: { allow: ["Bash(ls)"] } }); + expect(read(home, claude)).toEqual({ model: "opus", env: { MY_FLAG: "1" }, permissions: { allow: ["Bash(ls)"] } }); expect(report.actions).toContainEqual({ target: claude, operation: "replace", reason: "remove env.ANTHROPIC_BASE_URL" }); const run = backups(home).at(-1); expect(JSON.parse(readFileSync(join(run!, claude), "utf8"))).toEqual(before); }); + test("keeps the proxy route it recorded while a token of yours would go to Anthropic without it, says so as a To do, and takes the route out once the token is gone", async () => { + const home = temp.make("home-"); + const route = [{ provider: "claude" as const, key: "env.ANTHROPIC_BASE_URL", value: "http://127.0.0.1:8317", pack: "hub" }, { provider: "claude" as const, key: "apiKeyHelper", value: 'cat "$HOME/.config/hedge/cli-proxy.key"', pack: "hub" }]; + await apply(plan(route), { home, adapters: adapters() }); + const routed = { ...read(home, claude), env: { ...read(home, claude).env, ANTHROPIC_AUTH_TOKEN: "my-token" } }; + write(home, claude, routed); + + const kept = await apply(plan(), { home, adapters: adapters() }); + + expect(read(home, claude)).toEqual(routed); + expect(kept.findings).toContainEqual({ id: `route:${claude}`, pack: "engine", say: `Kept Hedge's proxy route in ${claude}: without it, Claude Code would send your env.ANTHROPIC_AUTH_TOKEN to Anthropic. Remove env.ANTHROPIC_AUTH_TOKEN from ${claude}, then run hedge apply to take the route out.`, todo: true }); + write(home, claude, { ...routed, env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" } }); + const taken = await apply(plan(), { home, adapters: adapters() }); + expect(read(home, claude)).toEqual({}); + expect(taken.findings.map((finding) => finding.id)).not.toContain(`route:${claude}`); + }); + test("keeps a proxy base URL that Hedge has no record of", async () => { const home = temp.make("home-"); const settings = { env: { ANTHROPIC_BASE_URL: "http://localhost:8317/", ANTHROPIC_AUTH_TOKEN: "token" } }; @@ -95,18 +112,15 @@ describe("provider config", () => { expect(report.actions).toEqual([]); }); - test("removes Hedge's exact proxy route that it has no record of, and keeps the proxy address beside a key helper of your own", async () => { - const hedges = temp.make("home-"); - write(hedges, claude, { model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: 'cat "$HOME/.config/hedge/cli-proxy.key"' }); - const yours = temp.make("home-"); - const mine = { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: "my-helper" }; - write(yours, claude, mine); + test("keeps a route with Hedge's own proxy values that Hedge has no record of", async () => { + const home = temp.make("home-"); + const unrecorded = { model: "opus", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:8317" }, apiKeyHelper: 'cat "$HOME/.config/hedge/cli-proxy.key"' }; + write(home, claude, unrecorded); - await apply(plan(), { home: hedges, adapters: adapters() }); - await apply(plan(), { home: yours, adapters: adapters() }); + const report = await apply(plan(), { home, adapters: adapters() }); - expect(read(hedges, claude)).toEqual({ model: "opus" }); - expect(read(yours, claude)).toEqual(mine); + expect(read(home, claude)).toEqual(unrecorded); + expect(report.actions).toEqual([]); }); test("keeps a base URL that points at a gateway the user chose", async () => { diff --git a/src/adapters/provider-config/proxy-route.ts b/src/adapters/provider-config/proxy-route.ts index c12755a4..1be0cc76 100644 --- a/src/adapters/provider-config/proxy-route.ts +++ b/src/adapters/provider-config/proxy-route.ts @@ -1,17 +1,27 @@ import type { Provider } from "../../pack/types.ts"; +import { get, type Document } from "./fields.ts"; export const proxyUrl = "http://127.0.0.1:8317"; export const keyHelper = 'cat "$HOME/.config/hedge/cli-proxy.key"'; export const codexProvider = "hedge-proxy"; export const codexProviderTable = { name: "Hedge CLI proxy", base_url: `${proxyUrl}/v1`, wire_api: "responses", auth: { command: "/bin/sh", args: ["-c", keyHelper] } }; -export const proxyRoute: Partial>> = { - claude: [ - { path: ["env", "ANTHROPIC_BASE_URL"], value: proxyUrl }, - { path: ["apiKeyHelper"], value: keyHelper }, - ], - codex: [ - { path: ["model_provider"], value: codexProvider }, - { path: ["model_providers", codexProvider], value: codexProviderTable }, - ], +type Stranded = { path: string; harm: string }; +type Route = { paths: string[][]; strands(document: Document): Stranded | undefined }; + +const claudeCredentials = [["apiKeyHelper"], ["env", "ANTHROPIC_AUTH_TOKEN"], ["env", "ANTHROPIC_API_KEY"]]; + +export const proxyRoute: Partial> = { + claude: { + paths: [["env", "ANTHROPIC_BASE_URL"], ["apiKeyHelper"]], + strands(document) { + if (get(document, ["env", "ANTHROPIC_BASE_URL"]) !== undefined) return undefined; + const path = claudeCredentials.find((item) => get(document, item) !== undefined)?.join("."); + return path ? { path, harm: `Claude Code would send ${path === "apiKeyHelper" ? "the key your apiKeyHelper prints" : `your ${path}`} to Anthropic` } : undefined; + }, + }, + codex: { + paths: [["model_provider"], ["model_providers", codexProvider]], + strands: (document) => (get(document, ["model_provider"]) === codexProvider && get(document, ["model_providers", codexProvider]) === undefined ? { path: "model_provider", harm: `Codex would select a ${codexProvider} provider table that is gone` } : undefined), + }, }; From 94084a07e9c42ed2fc7de0edbb3b787c69970b59 Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" <755327+Silthus@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:39:29 +0000 Subject: [PATCH 32/32] Describe connect running apply and the one-unit proxy route in the changelog Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b664c597..d5f3df04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,11 +16,9 @@ subscriptions, its route check is a `To do:` line, not a failure. `hedge cli-proxy connect` saves the key on a node in that case, so the first `hedge apply` after the proxy serves an agent sends it there. - Connect changes no agent settings in that case: `hedge apply` takes - Hedge's route off an agent that the proxy no longer serves. When the hub - hands a box over while the proxy serves none of its agents, the hub tells - you to run `hedge apply` on the box, so that the box uses its own sign-in - until the proxy serves it. On the both plan, `hedge doctor` fails while a + When the hub hands a box over while the proxy serves none of its agents, + the hub says that the box keeps its own sign-in until the proxy serves + it. On the both plan, `hedge doctor` fails while a Codex subscription works but the proxy serves no Codex model. A proxy that is down, refuses the key, answers without a model list, or runs a plugin Hedge did not install still fails `hedge doctor`. @@ -35,7 +33,9 @@ found the proxy unsafe. A route that an earlier apply wrote stays while the steps run. When an adapter that runs before the steps fails, such as Jobs on a job id it refuses, apply still decides and writes the - provider settings, then fails with that adapter's error. + provider settings, then fails with that adapter's error. When the + provider settings fail as well, apply names both errors, that adapter's + first. - On the hub, `hedge cli-proxy check` and `hedge cli-proxy check codex`, the checks that Hedge's routes wait for, fail while `hedge cli-proxy doctor` finds the proxy unsafe: a plugin Hedge did not @@ -47,15 +47,22 @@ Claude Code or Codex to it, and a route that an earlier apply wrote leaves at the next apply. A route to a safe proxy stays in place, and a `hedge-proxy` Codex table of your own stays. +- `hedge cli-proxy connect` no longer writes Claude Code or Codex + settings itself. It saves the proxy key and runs `hedge apply`, which + writes the route and records it. So every route that Hedge writes has + a record, and turning balancing off takes it off at the next apply. - Hedge removes a Claude Code or Codex setting only when its own record - says Hedge wrote it, or when it is part of the route to the proxy and - the whole route holds Hedge's exact values: the Claude proxy address - and key helper, or the Codex `hedge-proxy` selector and table. Apply - removes that route whenever this apply does not write it, with - own-account balancing on or off, so a box that `hedge cli-proxy connect` sent to - the proxy leaves it when you turn balancing off. It no longer removes - a setting because it points at the proxy port 8317. So a Codex - provider table of your own, of any name, stays with its selector. + says Hedge wrote it. It no longer removes a setting because it points + at the proxy port 8317. So a Codex provider table of your own, of any + name, stays with its selector. +- The route to the proxy is one unit: the Claude proxy address and key + helper, or the Codex `hedge-proxy` selector and table. Apply writes all + of it or none of it, so it selects no `hedge-proxy` table of your own. + It removes it together too. When removing it would leave a key of + yours that Claude Code then sends to Anthropic, such as your own + `ANTHROPIC_AUTH_TOKEN`, or a Codex `model_provider` that names a table + that is gone, the route stays and apply shows a `To do:` line that + names the setting to remove. `hedge doctor` reads one result of each check for both the config and its line. - When `hedge cli-proxy connect` fails on a box, the hub names the box and