From 7fd82eb14613cd4b920fc11b6949f3f1a01de9cc Mon Sep 17 00:00:00 2001 From: Dizzle <112548150+maxmad64bis@users.noreply.github.com> Date: Thu, 20 Aug 2026 20:46:57 +0200 Subject: [PATCH] fix(api): guard combo updates against emptying, and fix copilot combo targets (#10866) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Obrigado — bug real e bem raiz-causado, dois bugs da mesma família: (1) PUT /api/combos/ com models:[] zerava os targets sem aviso, quebrando um combo funcionando (o invariante "combo tem ≥1 modelo" já era reforçado por ~9 consumidores downstream, menos o write path); (2) as tools do Copilot escreviam em targets em vez de models, então todo combo criado via Copilot reportava sucesso mas roteava para lugar nenhum. Validação (worktree combinado a partir de origin/release/v3.8.50, 0 conflitos): - typecheck:core limpo, complexity/cognitive-complexity dentro do baseline - tests/unit/combo-empty-models.test.ts — 4/4 passando - Suíte combo completa (tests/unit/combo*.test.ts) — 1213 testes, 1208 passando, 5 falhas idênticas em ambos os lados (confirmadas DRIFT pré-existente do #9985 via probe contra o tip puro do release) --- changelog.d/fixes/10866-combo-empty-models.md | 1 + src/lib/copilot/tools.ts | 8 +-- src/shared/validation/schemas/combo.ts | 7 ++- tests/unit/combo-empty-models.test.ts | 59 +++++++++++++++++++ 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 changelog.d/fixes/10866-combo-empty-models.md create mode 100644 tests/unit/combo-empty-models.test.ts diff --git a/changelog.d/fixes/10866-combo-empty-models.md b/changelog.d/fixes/10866-combo-empty-models.md new file mode 100644 index 0000000000..e71092d5d4 --- /dev/null +++ b/changelog.d/fixes/10866-combo-empty-models.md @@ -0,0 +1 @@ +- fix(api): reject a combo update that removes every model, and store the copilot's combo targets where the router reads them (#10866) diff --git a/src/lib/copilot/tools.ts b/src/lib/copilot/tools.ts index a4bc59321c..095f7cf4cd 100644 --- a/src/lib/copilot/tools.ts +++ b/src/lib/copilot/tools.ts @@ -121,11 +121,7 @@ export const COPILOT_TOOLS: CopilotTool[] = [ let output = `**${combos.length} combo(s) configured**\n\n`; for (const c of combos as any[]) { const active = c.isActive ? "✅" : "⛔"; - const targets = c.targets - ? typeof c.targets === "string" - ? JSON.parse(c.targets).length - : c.targets.length - : 0; + const targets = Array.isArray(c.models) ? c.models.length : 0; output += `${active} **${c.name}** — strategy: \`${c.strategy}\` — ${targets} target(s)\n`; } return output; @@ -165,7 +161,7 @@ export const COPILOT_TOOLS: CopilotTool[] = [ const combo = await createCombo({ name, strategy, - targets: JSON.stringify(targets), + models: targets, isActive: true, }); const anyCombo = combo as any; diff --git a/src/shared/validation/schemas/combo.ts b/src/shared/validation/schemas/combo.ts index f9b7e3c832..79bb9ca56c 100644 --- a/src/shared/validation/schemas/combo.ts +++ b/src/shared/validation/schemas/combo.ts @@ -379,7 +379,12 @@ export const updateComboSchema = z .object({ name: comboNameSchema.optional(), description: z.string().max(2000).optional().nullable(), - models: z.array(comboModelEntry).optional(), + // Creation may leave `models` empty (`omniroute combo create` drafts one + // that way); an update may not, or a working combo loses every target. + models: z + .array(comboModelEntry) + .min(1, "an update cannot remove every model from a combo") + .optional(), strategy: comboStrategySchema.optional(), config: comboRuntimeConfigSchema.optional(), isActive: z.boolean().optional(), diff --git a/tests/unit/combo-empty-models.test.ts b/tests/unit/combo-empty-models.test.ts new file mode 100644 index 0000000000..1486628ec5 --- /dev/null +++ b/tests/unit/combo-empty-models.test.ts @@ -0,0 +1,59 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import assert from "node:assert/strict"; +import test from "node:test"; + +const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-empty-models-")); +process.env.DATA_DIR = TEST_DATA_DIR; + +const { createComboSchema, updateComboSchema } = + await import("../../src/shared/validation/schemas/combo.ts"); +const { getCopilotTool } = await import("../../src/lib/copilot/tools.ts"); +const combosDb = await import("../../src/lib/db/combos.ts"); +const core = await import("../../src/lib/db/core.ts"); + +test.after(() => { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); +}); + +test("an update cannot remove every model from a combo", () => { + assert.equal(updateComboSchema.safeParse({ models: [] }).success, false); + assert.equal(updateComboSchema.safeParse({ models: ["openai/gpt-4o-mini"] }).success, true); + assert.equal(updateComboSchema.safeParse({ name: "renamed" }).success, true); +}); + +test("creating a combo with no model stays allowed — the CLI does it on purpose", () => { + assert.equal(createComboSchema.safeParse({ name: "drafted", models: [] }).success, true); + assert.equal(createComboSchema.safeParse({ name: "drafted" }).success, true); +}); + +test("the copilot createCombo tool stores targets where the router looks for them", async () => { + const tool = getCopilotTool("createCombo"); + assert.ok(tool); + + await tool.handler({ + name: "copilot-stored", + strategy: "priority", + targets: JSON.stringify([{ provider: "openai", model: "gpt-4o-mini", weight: 100 }]), + }); + + const stored = (await combosDb.getComboByName("copilot-stored")) as { models?: unknown[] } | null; + assert.ok(stored, "the combo should exist"); + assert.equal(stored.models?.length, 1); +}); + +test("the copilot combo list counts the targets the router will use", async () => { + const list = getCopilotTool("listCombos"); + assert.ok(list); + + await combosDb.createCombo({ + name: "dashboard-made", + strategy: "priority", + models: ["openai/gpt-4o-mini", "openai/gpt-4o"], + }); + + const output = await list.handler({}); + assert.match(output, /\*\*dashboard-made\*\* — strategy: `priority` — 2 target\(s\)/); +});