Skip to content

Commit a141859

Browse files
authored
feat(sidecar): unified picker candidate set for both sidecars (#2188 L2) (#2204)
* feat(sidecar): unified picker candidate set consumed by the vision options (#2188) pickerVisibleSidecarCandidates = management picker rows (disabled rows dropped) ∪ auth slots, de-duplicated with the slot flag winning; a catalog outage degrades to the logged-in slots instead of an empty 500. visionSidecarCandidates applies rule 2 (drop provably text-only) on top. visionCandidateRows now wraps the unified set, so a hidden Luna/Haiku stays offerable for a logged-in side and the catalog-wide expansion of visionEligibleModelOptions is bounded by picker visibility. * fix(sidecar): keep write-gate evidence unfiltered in visionCandidateRows Rule 2 pre-filtering removed every text-only row before the PUT gate ran, deadening visionDescriberIsProvablyBlind's catalog-row proof step and flipping reject to allow for ids only proven blind by a picker row. Rule 2 stays in the options path; the gate reads the raw union. Adds the reject-path regression the reviewer demanded.
1 parent 377f229 commit a141859

3 files changed

Lines changed: 232 additions & 11 deletions

File tree

‎src/server/management/vision-sidecar-options.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
type VisionSidecarBackend,
1818
} from "../../vision/eligibility";
1919
import { listOpenAiForwardSidecarCandidates } from "../../providers/openai-sidecar";
20-
import { listManagementModelRows } from "./model-rows";
20+
import { pickerVisibleSidecarCandidates } from "../../sidecar/candidates";
21+
import { resolveSidecarAuth } from "../../sidecar/auth";
2122

2223
/**
2324
* Backends whose executor could actually run: openai forward, anthropic OAuth.
@@ -41,17 +42,23 @@ export function enabledVisionBackends(
4142
return backends.length > 0 ? backends : ["openai", "anthropic"];
4243
}
4344

44-
/** Visible catalog rows in the shape the eligibility predicate consumes. */
45+
/**
46+
* Visible catalog rows in the shape the eligibility predicate consumes.
47+
* Sourced from the unified picker set (#2188): picker-visible rows ∪ auth
48+
* slots, UNFILTERED. Rule 2 (− provably text-only) belongs to the OPTIONS
49+
* path only (visionEligibleModelOptions already applies it). The PUT gate
50+
* consumes this list as EVIDENCE: a picker row proving an id text-only is
51+
* exactly what visionDescriberIsProvablyBlind needs to reject that id, so
52+
* pre-filtering here would deaden the gate (review F1: reject → allow flip).
53+
*/
4554
export async function visionCandidateRows(config: OcxConfig): Promise<VisionCandidateModel[]> {
46-
let rows: Awaited<ReturnType<typeof listManagementModelRows>> = [];
47-
// A catalog outage must not 500 the settings route nor reject a write; with []
48-
// the option list degrades to the baselines, which is the intended floor.
49-
try { rows = await listManagementModelRows(config); } catch { rows = []; }
50-
return rows.filter(row => row.disabled !== true).map(row => ({
51-
provider: row.provider,
52-
id: row.id,
53-
...(row.inputModalities ? { inputModalities: row.inputModalities } : {}),
54-
...(row.native ? { native: true } : {}),
55+
const auth = resolveSidecarAuth(config);
56+
const all = await pickerVisibleSidecarCandidates(config, auth);
57+
return all.map(candidate => ({
58+
provider: candidate.provider,
59+
id: candidate.id,
60+
...(candidate.inputModalities ? { inputModalities: candidate.inputModalities } : {}),
61+
...(candidate.native ? { native: true } : {}),
5562
}));
5663
}
5764

‎src/sidecar/candidates.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* The unified picker-visibility candidate set both sidecars consume (#2188).
3+
*
4+
* Rule 1 (picker): a model may be OFFERED as a sidecar backend only if the
5+
* management picker would show it — the same `listManagementModelRows` rows
6+
* the GUI Models tab renders, minus disabled rows. Rule 1's only exception is
7+
* the auth slots: a logged-in side keeps its fixed slot model (Luna/Haiku)
8+
* even when the picker hides it, because the slot is the login's entitlement.
9+
*
10+
* Vision additionally applies rule 2 (− provably text-only); web-search
11+
* applies its own rule 2 (∩ probed backend with an executor) in
12+
* src/web-search/backends.ts. Both start from THIS set so the two sidecars
13+
* cannot diverge on what "visible" means.
14+
*/
15+
import type { OcxConfig } from "../types";
16+
import { listManagementModelRows } from "../server/management/model-rows";
17+
import { modelAcceptsImageInput, type VisionCandidateModel } from "../vision/eligibility";
18+
import { sidecarAuthSlots, type SidecarAuthState } from "./auth";
19+
20+
export interface SidecarCandidate {
21+
provider: string;
22+
id: string;
23+
native?: boolean;
24+
inputModalities?: string[];
25+
/** True when this row is an auth-slot entitlement rather than a picker row. */
26+
authSlot?: boolean;
27+
}
28+
29+
/**
30+
* (picker-visible rows) ∪ (auth slots). De-duplicated by provider+id with the
31+
* auth-slot flag winning, so a slot model that is ALSO picker-visible still
32+
* reads as slot-backed. A catalog outage degrades to slots only — the settings
33+
* routes must not 500 and must keep the logged-in floor populated.
34+
*/
35+
export async function pickerVisibleSidecarCandidates(
36+
config: OcxConfig,
37+
auth: SidecarAuthState,
38+
): Promise<SidecarCandidate[]> {
39+
let rows: Awaited<ReturnType<typeof listManagementModelRows>> = [];
40+
try { rows = await listManagementModelRows(config); } catch { rows = []; }
41+
const byKey = new Map<string, SidecarCandidate>();
42+
for (const row of rows) {
43+
if (row.disabled === true) continue;
44+
byKey.set(`${row.provider}/${row.id}`, {
45+
provider: row.provider,
46+
id: row.id,
47+
...(row.inputModalities ? { inputModalities: row.inputModalities } : {}),
48+
...(row.native ? { native: true } : {}),
49+
});
50+
}
51+
for (const slot of sidecarAuthSlots(auth)) {
52+
byKey.set(`${slot.provider}/${slot.id}`, {
53+
provider: slot.provider,
54+
id: slot.id,
55+
// Slot models are known image-capable; their only exclusion path is the
56+
// provider's explicit consumer list (same stance as baselineCandidate).
57+
inputModalities: ["text", "image"],
58+
authSlot: true,
59+
});
60+
}
61+
return [...byKey.values()];
62+
}
63+
64+
/**
65+
* Rule 2 for vision: drop rows PROVABLY text-only. Unknown stays eligible —
66+
* the picker filter (rule 1) already bounded the set, so permissive-unknown
67+
* no longer expands to the whole catalog.
68+
*/
69+
export function visionSidecarCandidates(
70+
config: Pick<OcxConfig, "providers">,
71+
all: readonly SidecarCandidate[],
72+
): SidecarCandidate[] {
73+
return all.filter(candidate => modelAcceptsImageInput(config, toVisionCandidate(candidate)) !== false);
74+
}
75+
76+
function toVisionCandidate(candidate: SidecarCandidate): VisionCandidateModel {
77+
return {
78+
provider: candidate.provider,
79+
id: candidate.id,
80+
...(candidate.inputModalities ? { inputModalities: candidate.inputModalities } : {}),
81+
...(candidate.native ? { native: true } : {}),
82+
};
83+
}

‎tests/sidecar-candidates.test.ts‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { afterEach, describe, expect, mock, test } from "bun:test";
2+
import * as storeModule from "../src/oauth/store";
3+
import * as usabilityModule from "../src/codex/account-usability";
4+
import * as modelRowsModule from "../src/server/management/model-rows";
5+
6+
let accountSets: Record<string, { accounts: Array<{ id: string; needsReauth?: boolean }>; activeAccountId?: string }> = {};
7+
let usableCodexAccounts: Set<string> = new Set();
8+
let managementRows: Array<Record<string, unknown>> | Error = [];
9+
10+
mock.module("../src/oauth/store", () => ({
11+
...storeModule,
12+
getAccountSet: (provider: string) => accountSets[provider] ?? null,
13+
}));
14+
mock.module("../src/codex/account-usability", () => ({
15+
...usabilityModule,
16+
isCodexAccountUsable: (_config: unknown, accountId: string) => usableCodexAccounts.has(accountId),
17+
}));
18+
mock.module("../src/server/management/model-rows", () => ({
19+
...modelRowsModule,
20+
listManagementModelRows: async () => {
21+
if (managementRows instanceof Error) throw managementRows;
22+
return managementRows;
23+
},
24+
}));
25+
26+
import { pickerVisibleSidecarCandidates, visionSidecarCandidates } from "../src/sidecar/candidates";
27+
import { resolveSidecarAuth } from "../src/sidecar/auth";
28+
import { visionCandidateRows, visionDescriberIsProvablyBlind } from "../src/server/management/vision-sidecar-options";
29+
import { MAIN_CODEX_ACCOUNT_ID } from "../src/codex/account-id";
30+
import type { OcxConfig, OcxProviderConfig } from "../src/types";
31+
32+
const forward: OcxProviderConfig = { adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward" };
33+
const anthropicOAuth: OcxProviderConfig = { adapter: "anthropic", baseUrl: "https://api.anthropic.com", authMode: "oauth" };
34+
35+
function config(overrides: Partial<OcxConfig> = {}): OcxConfig {
36+
return { port: 10100, defaultProvider: "openai", providers: { openai: forward }, ...overrides };
37+
}
38+
39+
afterEach(() => {
40+
accountSets = {};
41+
usableCodexAccounts = new Set();
42+
managementRows = [];
43+
});
44+
45+
function loginBoth(): void {
46+
usableCodexAccounts.add(MAIN_CODEX_ACCOUNT_ID);
47+
accountSets = { claude: { accounts: [{ id: "a1" }], activeAccountId: "a1" } };
48+
}
49+
50+
describe("pickerVisibleSidecarCandidates", () => {
51+
test("disabled rows disappear; hidden Luna survives via the auth slot", async () => {
52+
loginBoth();
53+
managementRows = [
54+
{ provider: "openai", id: "gpt-5.6-luna", disabled: true, native: true },
55+
{ provider: "openai", id: "gpt-5.6-terra", disabled: false, native: true },
56+
{ provider: "routed", id: "some-model", disabled: false },
57+
];
58+
const cfg = config({ providers: { openai: forward, claude: anthropicOAuth } });
59+
const all = await pickerVisibleSidecarCandidates(cfg, resolveSidecarAuth(cfg));
60+
const ids = all.map(c => `${c.provider}/${c.id}`).sort();
61+
expect(ids).toEqual([
62+
"claude/claude-haiku-4-5",
63+
"openai/gpt-5.6-luna",
64+
"openai/gpt-5.6-terra",
65+
"routed/some-model",
66+
]);
67+
expect(all.find(c => c.id === "gpt-5.6-luna")?.authSlot).toBe(true);
68+
});
69+
70+
test("picker-visible slot model keeps the slot flag (slot wins de-dup)", async () => {
71+
loginBoth();
72+
managementRows = [{ provider: "openai", id: "gpt-5.6-luna", disabled: false, native: true }];
73+
const cfg = config({ providers: { openai: forward, claude: anthropicOAuth } });
74+
const all = await pickerVisibleSidecarCandidates(cfg, resolveSidecarAuth(cfg));
75+
expect(all.filter(c => c.id === "gpt-5.6-luna")).toHaveLength(1);
76+
expect(all.find(c => c.id === "gpt-5.6-luna")?.authSlot).toBe(true);
77+
});
78+
79+
test("catalog outage degrades to auth slots only, not a 500", async () => {
80+
loginBoth();
81+
managementRows = new Error("catalog down");
82+
const cfg = config({ providers: { openai: forward, claude: anthropicOAuth } });
83+
const all = await pickerVisibleSidecarCandidates(cfg, resolveSidecarAuth(cfg));
84+
expect(all.map(c => c.id).sort()).toEqual(["claude-haiku-4-5", "gpt-5.6-luna"]);
85+
});
86+
87+
test("no logins and empty catalog -> empty set", async () => {
88+
const all = await pickerVisibleSidecarCandidates(config(), resolveSidecarAuth(config()));
89+
expect(all).toEqual([]);
90+
});
91+
});
92+
93+
describe("visionSidecarCandidates (rule 2: − provably text-only)", () => {
94+
test("provably text-only row excluded; unknown stays; slots stay", async () => {
95+
loginBoth();
96+
managementRows = [
97+
{ provider: "routed", id: "text-model", disabled: false, inputModalities: ["text"] },
98+
{ provider: "routed", id: "unknown-model", disabled: false },
99+
];
100+
const cfg = config({ providers: { openai: forward, claude: anthropicOAuth } });
101+
const all = await pickerVisibleSidecarCandidates(cfg, resolveSidecarAuth(cfg));
102+
const vision = visionSidecarCandidates(cfg, all);
103+
const ids = vision.map(c => c.id).sort();
104+
expect(ids).toEqual(["claude-haiku-4-5", "gpt-5.6-luna", "unknown-model"]);
105+
});
106+
107+
test("noVisionModels consumer listing beats the slot's advertised modalities", async () => {
108+
loginBoth();
109+
const blindLuna: OcxProviderConfig = { ...forward, noVisionModels: ["gpt-5.6-luna"] };
110+
const cfg = config({ providers: { openai: blindLuna, claude: anthropicOAuth } });
111+
const all = await pickerVisibleSidecarCandidates(cfg, resolveSidecarAuth(cfg));
112+
const vision = visionSidecarCandidates(cfg, all);
113+
expect(vision.map(c => c.id)).not.toContain("gpt-5.6-luna");
114+
expect(vision.map(c => c.id)).toContain("claude-haiku-4-5");
115+
});
116+
});
117+
118+
describe("write-gate evidence is NOT pre-filtered (review F1 regression)", () => {
119+
test("a picker row proving an id text-only still reaches the PUT gate and rejects it", async () => {
120+
loginBoth();
121+
managementRows = [
122+
{ provider: "routed", id: "row-proven-blind", disabled: false, inputModalities: ["text"] },
123+
];
124+
const cfg = config({ providers: { openai: forward, claude: anthropicOAuth } });
125+
const candidates = await visionCandidateRows(cfg);
126+
// The evidence row must survive into the candidates list...
127+
expect(candidates.map(c => c.id)).toContain("row-proven-blind");
128+
// ...so the gate can prove blindness from it (neither vendor table knows this id).
129+
expect(visionDescriberIsProvablyBlind(cfg, "row-proven-blind", candidates, undefined)).toBe(true);
130+
});
131+
});

0 commit comments

Comments
 (0)