diff --git a/CHANGELOG.md b/CHANGELOG.md index 574a4283..d5f3df04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,76 @@ ## 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. + `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. + 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`. +- 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 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. 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 + install, panel files that do not match their pins, or, with the Proxy + 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 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. 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 + 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. + ### Hedge is cloud only - Hedge runs your agents on a hub and one or more nodes in the cloud. The 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/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/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..79f50fe3 100644 --- a/packs/hub/hub.test.ts +++ b/packs/hub/hub.test.ts @@ -2,10 +2,12 @@ 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"; -import { cliProxy, codexProviderTable, type ProxyMachine } from "./lib/cli-proxy.ts"; +import { cliProxy, codexProviderTable, proxyUrl, type ProxyMachine } from "./lib/cli-proxy.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"; @@ -267,7 +269,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); @@ -309,6 +311,80 @@ describe("the Own-account balancing workflow", () => { expect(await codexConfig(mac)).toBeUndefined(); }); + const connectedDevbox = (files: Record = {}) => { + 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 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")); + 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("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((await applyOff()).code).toBe(0); + expect(codex()).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 () => { + 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()).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\"" }); @@ -362,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"); - 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); + 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 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([]); }); @@ -410,4 +481,200 @@ 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 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, 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`; + + 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.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-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-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-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-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); + }); + + 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 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 { 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) }; + 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("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([["step:cli-proxy", "apply"], ["check:cli-proxy-accounts", true], ["check:cli-proxy-client", "apply"]]); + expect(existsSync(join(home, ".claude/settings.json"))).toBe(false); + }); + + 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 route = () => (existsSync(join(home, ".claude/settings.json")) ? JSON.parse(readFileSync(join(home, ".claude/settings.json"), "utf8")).env?.ANTHROPIC_BASE_URL : undefined); + const routes = new Set(); + const reach = machine.fetch; + machine.fetch = (input, init) => { + if (String(input).startsWith(proxyUrl)) routes.add(route()); + return reach(input, init); + }; + const hooks = { beforeInstall: () => {} }; + const served = hedgeProxy(machine, result.engineRoot); + const run = async (command: string[]) => { + if (command.at(-1) === "install") hooks.beforeInstall(); + return served(command); + }; + 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"]]; + + 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(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 by the time it ends, and fails", 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("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(); + }); + + 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 = () => { + mkdirSync(join(dataDir(home), "plugins"), { recursive: true }); + writeFileSync(join(dataDir(home), "plugins/other.so"), "plugin"); + }; + const applied = await apply(result, options); + expect(route()).toBeUndefined(); + expect(applied.ok).toBe(false); + 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 () => { + 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(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); + }); + + 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.clear(); + const next = await apply(result, options); + expect(next.ok).toBe(true); + expect(next.actions).toEqual([]); + expect(routes).toEqual(new Set(["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 () => { + const proxy: Proxy = { credentials: [{ provider: "claude" }] }; + 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) }; + 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-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/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-forwards.test.ts b/packs/hub/lib/cli-proxy-forwards.test.ts index 2f21b542..d651efe8 100644 --- a/packs/hub/lib/cli-proxy-forwards.test.ts +++ b/packs/hub/lib/cli-proxy-forwards.test.ts @@ -3,7 +3,8 @@ 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 { 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"; @@ -55,18 +56,31 @@ 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: The proxy does not answer."); + 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"); 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. 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"); +}); + 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; @@ -76,12 +90,43 @@ 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 on the Codex plan sends Codex through the proxy without another apply", async () => { +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 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); +}); + +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, 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 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-panel.test.ts b/packs/hub/lib/cli-proxy-panel.test.ts index 0e74f383..dc4cb075 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"; @@ -191,3 +191,74 @@ 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.`); +}); + +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); +}); + +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); + 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(`${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); + } +}); + +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); + } +}); diff --git a/packs/hub/lib/cli-proxy-testing.ts b/packs/hub/lib/cli-proxy-testing.ts new file mode 100644 index 00000000..eb1906b6 --- /dev/null +++ b/packs/hub/lib/cli-proxy-testing.ts @@ -0,0 +1,90 @@ +import { existsSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +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); +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; stopped?: boolean; refuses?: boolean; modelList?: string; silentModels?: boolean; authFiles?: unknown }; + +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 || 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 }); + 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 }); + }; + 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) => { + 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" }; + if (startsProxy(command) && existsSync(join(home, ".config/systemd/user/hedge-cli-proxy.service"))) proxy.stopped = false; + 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 }; +} + +export 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") }); + +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 89fbdd70..ceb94546 100644 --- a/packs/hub/lib/cli-proxy.test.ts +++ b/packs/hub/lib/cli-proxy.test.ts @@ -1,19 +1,16 @@ -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 { 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(); 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,47 +25,22 @@ 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" }); +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 }); @@ -189,11 +161,43 @@ 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: [], silentModels: true }); + await expect(cliProxy("doctor", silent.machine)).rejects.toThrow(`The proxy at ${proxyUrl} does not answer.`); + 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"); + } + 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 +228,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(4); + 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(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); + 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(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"); }); test("on the Codex plan the doctor counts Codex subscriptions and wants a Codex model, and names the Codex login", async () => { @@ -241,7 +269,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"); }); @@ -257,90 +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" }); - 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); + const noCodex = connecting({ plan: "codex" }); + expect(await cliProxy("connect", noCodex.machine)).toBe(0); + 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. 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 () => { +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 }); @@ -360,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); @@ -369,26 +387,92 @@ 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, 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.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 = 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 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(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.`, + ]); +}); +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}', '{"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); + 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("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("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("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 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); @@ -407,7 +491,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 +501,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(4); expect(refused.printed.join("\n")).toContain("lists no Claude model"); const fresh = client({}, { down: true }); @@ -425,23 +509,25 @@ test("the check fails without a saved key, or when the proxy serves no Claude mo 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 }); @@ -450,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); @@ -469,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-"); @@ -513,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; @@ -546,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); @@ -595,7 +670,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(4); expect(existsSync(codexFile(unserved.home))).toBe(false); }); @@ -609,12 +684,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(4); expect(printed.join("\n")).toContain(`The proxy at ${proxyUrl} lists no Codex model.`); }); @@ -657,7 +732,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(4); 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 +741,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(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 e7d254d3..bef05447 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"; @@ -11,11 +11,14 @@ 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"; import { must, proxyPort } from "./run.ts"; -export { proxyPort, proxyUrl }; -export type ProxyMode = "plan" | "install" | "doctor" | "catalog" | "connect" | "disconnect" | "check" | "remove" | "left"; +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 }; export type AiPlan = "claude" | "codex" | "both"; @@ -25,7 +28,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`; @@ -214,24 +216,20 @@ 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.`); - 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 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"; 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 +240,42 @@ 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?: 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; + 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 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; + } + 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.`; + } +} + 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(); @@ -249,10 +283,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."); @@ -267,7 +297,10 @@ 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; + 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-") }; @@ -276,49 +309,53 @@ 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} 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 { if (typeof models === "string") return models; 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 }; 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`; @@ -331,11 +368,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] } }; @@ -380,13 +413,17 @@ async function sendCodexHome(home: string): Promise { export type Agent = "claude" | "codex"; -async function check(machine: ProxyMachine, agent: Agent): Promise { +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} hedge apply keeps ${name} on its own sign-in until the proxy serves it.` : `${name} reaches your own-account proxy at ${proxyUrl}.`); - return !reason; + machine.print(reason ? `${reason} ${applyWaits(name)}` : `${name} reaches your own-account proxy at ${proxyUrl}.`); + if (!reason) return 0; + return reason === missingModel[agent] ? waitsForPerson : 1; } async function codexUnserved(home: string, models: string[] | string): Promise { @@ -399,56 +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 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") { - 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; - const unroutable = typeof codex === "string" ? codex : agent === "codex" ? undefined : await probe(machine.fetch, key, "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.`); } 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 }); @@ -480,14 +499,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/lib/links.ts b/packs/hub/lib/links.ts index 0f30c2c8..1ad56c76 100644 --- a/packs/hub/lib/links.ts +++ b/packs/hub/lib/links.ts @@ -4,6 +4,7 @@ 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 { 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"; @@ -69,7 +70,10 @@ 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: 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, 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/pack.json b/packs/hub/pack.json index 402e2743..2214a130 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" @@ -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": [ @@ -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). 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" + ], + "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). 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" + ], + "plans": [ + "codex" + ], + "workflow": "own-account-balancing" + }, { "id": "cli-proxy-client", "run": [ @@ -669,7 +704,8 @@ "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", "devbox" @@ -688,7 +724,8 @@ "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", "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..3c80e1dd 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` @@ -149,15 +151,30 @@ 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. 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. The first apply after the proxy + `hedge doctor` name the failed check. When the proxy answers the key but + 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 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 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/adapters/provider-config/adapter.ts b/src/adapters/provider-config/adapter.ts index 898abc8b..72e2a6d8 100644 --- a/src/adapters/provider-config/adapter.ts +++ b/src/adapters/provider-config/adapter.ts @@ -6,7 +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 { staleKeys } from "./stale.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"; @@ -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"; @@ -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), 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" }); @@ -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 } }); } @@ -106,25 +107,32 @@ 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 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[] = []; 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(".")); @@ -147,27 +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 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 [...stale, ...unrecorded]) { - if (get(current, path) === undefined) continue; - remove(current, path); - removed.push(path.join(".")); + 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) { @@ -192,17 +200,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))); diff --git a/src/adapters/provider-config/plugins.ts b/src/adapters/provider-config/plugins.ts index 0403cb81..a6f1460d 100644 --- a/src/adapters/provider-config/plugins.ts +++ b/src/adapters/provider-config/plugins.ts @@ -153,7 +153,7 @@ async function reconcileCodex(plan: Plan, home: string, dryRun: boolean, run: Co continue; } if (!mine.pending && found.enabled !== mine.enabled) { - findings.push(codexFinding(mine.id, `Kept the Codex plugin ${mine.id}, which no pack enables any more, because it changed outside Hedge. It is yours now.`)); + findings.push({ id: `plugin:codex:${mine.id}`, pack: "engine", say: `Kept the Codex plugin ${mine.id}, which no pack enables any more, because it changed outside Hedge. It is yours now.` }); if (!dryRun) record.delete(mine.id); continue; } diff --git a/src/adapters/provider-config/provider-config.test.ts b/src/adapters/provider-config/provider-config.test.ts index 195e6527..420ae14e 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,59 @@ 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", async () => { const home = temp.make("home-"); - 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)"] } }; + 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", 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(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" } }; + 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 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); + + const report = await apply(plan(), { home, adapters: adapters() }); + + expect(read(home, claude)).toEqual(unrecorded); + 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 +135,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 +220,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" }); @@ -215,6 +250,22 @@ 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 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 }); + 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(trustsEveryHook()).toBe(true); + }); + test("leaves a settings file alone when it is a link", async () => { const home = temp.make("home-"); const dotfiles = temp.make("dotfiles-"); @@ -222,11 +273,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 () => { @@ -245,7 +296,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}`); @@ -317,14 +368,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"' }; @@ -412,7 +455,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"); }); @@ -650,7 +693,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] }); @@ -732,12 +774,16 @@ 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.map((finding) => finding.say)).toContainEqual(expect.stringContaining("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(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/adapters/provider-config/proxy-route.ts b/src/adapters/provider-config/proxy-route.ts new file mode 100644 index 00000000..1be0cc76 --- /dev/null +++ b/src/adapters/provider-config/proxy-route.ts @@ -0,0 +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] } }; + +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), + }, +}; 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/cli.test.ts b/src/cli.test.ts index d77a8cf4..50758dd6 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(); @@ -98,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 () => { diff --git a/src/cli.ts b/src/cli.ts index 7b142c50..22f24e9e 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -558,13 +558,14 @@ function printReport(report: Report, command: string, print: (line: string) => v 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}`); printFindings(report.findings, print); - if (command === "doctor") print(report.ok ? "Everything matches your profile." : "Run hedge apply to fix what Hedge can fix."); + 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."); } function printFindings(findings: Report["findings"], print: (line: string) => void) { - for (const finding of findings) print(`${finding.repair === "human" ? "To do" : finding.repair ? "Fix" : "Note"}: ${finding.say}`); + for (const finding of findings) print(`${finding.repair === "human" || finding.todo ? "To do" : finding.repair ? "Fix" : "Note"}: ${finding.say}`); } if (import.meta.main) { diff --git a/src/pack/manifest.test.ts b/src/pack/manifest.test.ts index 37508a69..0f0f5f12 100644 --- a/src/pack/manifest.test.ts +++ b/src/pack/manifest.test.ts @@ -189,15 +189,22 @@ 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"); }); + 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..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`); @@ -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..9778d4c5 100644 --- a/src/pack/types.ts +++ b/src/pack/types.ts @@ -163,7 +163,9 @@ export type PackManifest = { subcommands?: Record; }; -export type PackCheck = Scope & { id: string; run: string[]; say: string }; +export const waitsForPerson = 4; + +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]; @@ -252,9 +254,9 @@ 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" }; +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..308c403e 100644 --- a/src/reconciler/apply.test.ts +++ b/src/reconciler/apply.test.ts @@ -3,7 +3,7 @@ import { existsSync, lstatSync, mkdirSync, readFileSync, rmSync, symlinkSync, wr import { join } from "node:path"; import type { Plan } from "../pack/types.ts"; import { tempDirs } from "../testing/git.ts"; -import { providerConfigAdapter } from "../adapters/provider-config/adapter.ts"; +import { providerConfigAdapter, type CommandRunner } from "../adapters/provider-config/adapter.ts"; import { apply, check, filesAdapter, type Adapter } from "./apply.ts"; const temp = tempDirs(); @@ -258,6 +258,60 @@ 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("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 = 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 () => { + 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([undefined]); + expect(applied.findings).toEqual([down]); + 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 () => { const home = temp.make("home-"); await apply(plan({ files: [], checks, config }), { home, adapters: [providerConfigAdapter()], run: proxy(() => true) }); @@ -270,6 +324,158 @@ 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]); + }); + + const installedPool = () => { + let installed = false; + let code = 4; + 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 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: [{ 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: [] }); + }); + + 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("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: [] }); + }); + + 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 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; + const run = async (command: string[]) => { + if (command.join(" ") === "install-pool") installed = true; + return pool.run(command); + }; + const claude = claudeCli(() => installed); + const home = temp.make("home-"); + 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(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 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("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 }> = []; + 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({}) }; + 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 }]; + 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 8c900baf..8ce42673 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"; @@ -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,22 +102,42 @@ 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; 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))); if (!context.dryRun && gated.failed.some(repairedByHedge)) report.ok = false; - for (const adapter of adapters) { - const result = await adapter.apply(gated.plan, context); + 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).catch(async (error: unknown) => { + 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); if (result.failed) report.ok = false; if (!result.blocked) continue; report.ok = false; - if (!context.dryRun) return report; + if (!context.dryRun) return { report, checks: [] }; } const before = await readLedger(options.home); const record = async (key: string, step?: LedgerStep) => { @@ -175,15 +202,20 @@ 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")); - return report; + 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); + +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)); - 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; - if ((await run(item.run)) !== 0) report.findings.push(checkFinding(item)); + 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); for (const step of plan.steps) { @@ -205,7 +237,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; @@ -213,8 +248,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>; failedChecks: Array>; 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; @@ -236,25 +271,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, boolean>(); - const checkPasses = async (item: Owned) => { - if (!checked.has(item)) checked.set(item, (await run(item.run)) === 0); - return checked.get(item)!; - }; - 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: 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 }); + 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); - 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, issued }; } type Cause = { workflow: string } | "pack" | "plan";