From 341ce9a2f22ea6e6451628d24dee90c833933ee4 Mon Sep 17 00:00:00 2001 From: rebuildup Date: Mon, 23 Nov 2026 20:31:22 +0900 Subject: [PATCH] fix(agent): reject malformed fallback profile tables A present non-table fallback_profile entry was treated as absent, so agent check could report a malformed config as healthy. Parse and list now fail closed, and the canonical spend wording no longer presents declared caps as measured billing guarantees. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- docs/adr/ADR-0018.md | 8 +++++++- docs/agent-launcher.md | 5 +++++ script/agent.fallback.test.ts | 12 ++++++++++++ script/agent/fallback.ts | 15 ++++++++++++--- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/adr/ADR-0018.md b/docs/adr/ADR-0018.md index 3bddf5e..aa2dbf6 100644 --- a/docs/adr/ADR-0018.md +++ b/docs/adr/ADR-0018.md @@ -102,7 +102,13 @@ the running total, so a budget-only check let it slip under a zero budget. Candidates whose billing can bill at all are therefore rejected outright on a zero-budget profile. Under a **positive** budget a billable candidate must declare an explicit cap, otherwise `totalSpend` counts it as 0 and any budget -admits it — the cap would only bound spend someone remembered to declare. +admits it. + +`max_spend` and `global_max_spend` are operator-declared admission ceilings, +not measured spend controls. The resolver does not estimate task cost or +observe provider billing; a positive budget therefore cannot be described as +a guaranteed spending cap. Where a provider offers an account-side limit or +pay-as-you-go switch, that account control remains the only hard stop. A **present** spend cap must be valid. The generic optional-integer helper mapped any malformed value to "absent", which for a cap means "uncapped", so diff --git a/docs/agent-launcher.md b/docs/agent-launcher.md index 5289556..900e105 100644 --- a/docs/agent-launcher.md +++ b/docs/agent-launcher.md @@ -331,6 +331,11 @@ rejects a candidate when: profile/global cap (`spend-cap-invalid` / `spend-cap-exceeded`). A `global_max_spend = 0` profile admits only candidates that cannot bill at all; +- for billable candidates, `max_spend` is only the operator-declared ceiling + used by admission arithmetic. The resolver does not estimate task cost or + measure provider spend, so a positive budget is not a spending guarantee; + the account-side limit remains the only hard stop where the provider offers + one; - the candidate's *effective* scope equals the primary's (`quota-exhausted-scope`) — e.g. two models on the *same* Kilo account are not fresh capacity — or its scope is not catalogued (`quota-scope-unknown`). diff --git a/script/agent.fallback.test.ts b/script/agent.fallback.test.ts index 17b66e0..5d7dffc 100755 --- a/script/agent.fallback.test.ts +++ b/script/agent.fallback.test.ts @@ -232,6 +232,18 @@ describe("parseFallbackProfile", () => { expect(profile).toBeNull(); }); + it("rejects a present non-table profile instead of treating it as absent", () => { + const badToml = { fallback_profile: { bad: "not-a-table" } }; + expect(() => parseFallbackProfile(badToml, "bad")).toThrow(ContractError); + expect(() => listFallbackProfiles(badToml)).not.toThrow(); + }); + + it("rejects a non-table fallback_profile section", () => { + const badToml = { fallback_profile: "not-a-table" }; + expect(() => parseFallbackProfile(badToml, "bad")).toThrow(ContractError); + expect(() => listFallbackProfiles(badToml)).toThrow(ContractError); + }); + it("rejects mismatched id", () => { const badToml = { fallback_profile: { diff --git a/script/agent/fallback.ts b/script/agent/fallback.ts index 25d4496..2475292 100644 --- a/script/agent/fallback.ts +++ b/script/agent/fallback.ts @@ -272,9 +272,15 @@ export function parseFallbackProfile( ): FallbackProfile | null { // Bun.TOML.parse returns nested structure: { fallback_profile: { "omp-mistral-quota": {...} } } const fallbackSection = toml.fallback_profile; - if (!isPlainObject(fallbackSection)) return null; + if (fallbackSection === undefined) return null; + if (!isPlainObject(fallbackSection)) { + throw new ContractError("schema", "fallback_profile: expected table"); + } const table = fallbackSection[profileId]; - if (!isPlainObject(table)) return null; + if (table === undefined) return null; + if (!isPlainObject(table)) { + throw new ContractError("schema", `fallback_profile.${profileId}: expected table`); + } const t = table; const key = `fallback_profile.${profileId}`; @@ -353,7 +359,10 @@ export function parseFallbackProfile( */ export function listFallbackProfiles(toml: Record): string[] { const fallbackSection = toml.fallback_profile; - if (!isPlainObject(fallbackSection)) return []; + if (fallbackSection === undefined) return []; + if (!isPlainObject(fallbackSection)) { + throw new ContractError("schema", "fallback_profile: expected table"); + } return Object.keys(fallbackSection).sort(); }