Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions bin/opencode-route-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,17 @@ const loadConfig = () => {
if (cfgCache && cfgCache.path === path && cfgCache.mtimeMs === st.mtimeMs)
return cfgCache;
const raw = parseJsonc(readFileSync(path, "utf8"));
const windows =
raw.experimental?.model_route_windows || raw.model_route_windows || {};
warnInvalidWindows(windows);
cfgCache = {
path,
mtimeMs: st.mtimeMs,
providers: raw.provider || {},
providerRoutes:
raw.experimental?.provider_routes || raw.provider_routes || {},
routes: raw.experimental?.model_routes || raw.model_routes || {},
windows: raw.experimental?.model_route_windows || raw.model_route_windows || {},
windows,
disabled: new Set(raw.disabled_providers || []),
};
return cfgCache;
Expand Down Expand Up @@ -397,7 +400,31 @@ const nowMs = () => {

const parseHHMM = (s) => {
const m = /^([0-9]{1,2}):([0-9]{2})$/.exec(typeof s === "string" ? s.trim() : "");
return m ? Number(m[1]) * 60 + Number(m[2]) : null;
if (!m) return null;
const hour = Number(m[1]);
const minute = Number(m[2]);
if (hour > 23 || minute > 59) return null;
return hour * 60 + minute;
};

// At config load, name any model_route_windows entry whose start/end
// is not a valid HH:MM (hour 00-23, minute 00-59) instead of silently
// evaluating it (#4259). windowActive fails closed (never active) for
// these entries, so a typo'd window surfaces as a loud no-match rather
// than flipping a route gate to always-on/always-off/end-less.
const warnInvalidWindows = (windows) => {
if (!windows || typeof windows !== "object") return;
for (const [name, win] of Object.entries(windows)) {
const start = parseHHMM(win?.start);
const end = parseHHMM(win?.end);
if (start === null || end === null) {
console.error(
`opencode-route-proxy: invalid model_route_windows["${name}"]: ` +
`start=${JSON.stringify(win?.start)} end=${JSON.stringify(win?.end)} ` +
`(expected HH:MM with hour 00-23, minute 00-59); window will never match`,
);
}
}
};

// Is window `win` active at `now` (epoch ms)? Day and wall time are
Expand Down
101 changes: 100 additions & 1 deletion bin/opencode-route-proxy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ const startProxy = async (arg = {}) => {
"model_route_windows": {
"peak": { "daysOfWeek": ["mon", "tue", "wed", "thu", "fri"], "start": "01:00", "end": "04:00", "timezone": "UTC" },
"overnight": { "daysOfWeek": ["fri"], "start": "22:00", "end": "02:00", "timezone": "UTC" },
"bad-start-2400": { "start": "24:00", "end": "12:00", "timezone": "UTC" },
"bad-end-2460": { "start": "09:00", "end": "24:60", "timezone": "UTC" },
"bad-wrap-99": { "start": "99:99", "end": "99:98", "timezone": "UTC" },
"bad-start-2500": { "start": "25:00", "end": "12:00", "timezone": "UTC" },
"bad-end-2500": { "start": "09:00", "end": "25:00", "timezone": "UTC" },
"edge-early": { "start": "00:00", "end": "00:02", "timezone": "UTC" },
"edge-late": { "start": "23:58", "end": "23:59", "timezone": "UTC" },
},
"model_routes": {
"t-ordered": { "selection": "ordered", "targets": ["leafA/m-a", "leafB/m-b"] },
Expand All @@ -189,6 +196,13 @@ const startProxy = async (arg = {}) => {
"t-window-inv": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": { "name": "peak", "invert": true } }, "leafB/m-b" ] },
"t-window-unknown": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "no-such-window" }, "leafB/m-b" ] },
"t-window-overnight": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "overnight" }, "leafB/m-b" ] },
"t-window-bad-start": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-start-2400" }, "leafB/m-b" ] },
"t-window-bad-end": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-end-2460" }, "leafB/m-b" ] },
"t-window-bad-wrap": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-wrap-99" }, "leafB/m-b" ] },
"t-window-bad-start2500": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-start-2500" }, "leafB/m-b" ] },
"t-window-bad-end2500": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-end-2500" }, "leafB/m-b" ] },
"t-window-edge-early": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "edge-early" }, "leafB/m-b" ] },
"t-window-edge-late": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "edge-late" }, "leafB/m-b" ] },
"t-group": { "selection": "ordered", "targets": ["grp-ordered/m-g"] },
"t-group-rr": { "selection": "ordered", "targets": ["grp-rr/m-g"] },
"t-group-weighted": { "selection": "ordered", "targets": ["grp-weighted/m-g"] },
Expand Down Expand Up @@ -249,7 +263,7 @@ const startProxy = async (arg = {}) => {
leafA.server.close();
leafB.server.close();
};
return { base, post, attempts, leafA, leafB, cleanup };
return { base, post, attempts, leafA, leafB, cleanup, log: () => log };
};

const MSGS = [
Expand Down Expand Up @@ -630,3 +644,88 @@ test("empty-shell openai block resolves the built-in endpoint default", async ()
await p.cleanup();
}
});

// Range checks (#4259): out-of-range window times fail closed (never
// active) as a window start and as a window end. Each case pins `now`
// where the old shape-only parseHHMM misread the window as active:
// 24:00-12:00 and 25:00-12:00 wrap into the after-midnight leg at
// 02:30, 09:00-24:60 / 09:00-25:00 cover 23:30 and 12:00, and
// 99:99-99:98 was always-on.
test("out-of-range window times fail closed as start and as end", async () => {
const AT_0230 = Date.UTC(2026, 10, 4, 2, 30);
const AT_1200 = Date.UTC(2026, 10, 4, 12, 0);
const AT_2330 = Date.UTC(2026, 10, 4, 23, 30);
const cases = [
["t-window-bad-start", AT_0230],
["t-window-bad-start2500", AT_0230],
["t-window-bad-wrap", AT_0230],
["t-window-bad-wrap", AT_1200],
["t-window-bad-wrap", AT_2330],
["t-window-bad-end", AT_2330],
["t-window-bad-end", AT_1200],
["t-window-bad-end2500", AT_1200],
["t-window-bad-end2500", AT_2330],
];
for (const [route, nowMs] of cases) {
const p = await startProxy({ nowMs });
try {
const res = await p.post({ model: `opencode-route/${route}`, messages: MSGS });
assert.equal(res.status, 200, route);
assert.equal((await res.json()).choices[0].message.content, "LEAF-B", `${route} at ${new Date(nowMs).toISOString()}`);
assert.deepEqual(
p.attempts().map((r) => r.provider_id),
["leafB"],
route,
);
} finally {
await p.cleanup();
}
}
});

test("boundary times 00:00 and 23:59 remain valid windows", async () => {
const AT_0001 = Date.UTC(2026, 10, 4, 0, 1);
const AT_2358 = Date.UTC(2026, 10, 4, 23, 58);
const AT_1200 = Date.UTC(2026, 10, 4, 12, 0);
const early = await startProxy({ nowMs: AT_0001 });
try {
const res = await early.post({ model: "opencode-route/t-window-edge-early", messages: MSGS });
assert.equal((await res.json()).choices[0].message.content, "LEAF-A");
} finally {
await early.cleanup();
}
const late = await startProxy({ nowMs: AT_2358 });
try {
const res = await late.post({ model: "opencode-route/t-window-edge-late", messages: MSGS });
assert.equal((await res.json()).choices[0].message.content, "LEAF-A");
} finally {
await late.cleanup();
}
const mid = await startProxy({ nowMs: AT_1200 });
try {
for (const route of ["t-window-edge-early", "t-window-edge-late"]) {
const res = await mid.post({ model: `opencode-route/${route}`, messages: MSGS });
assert.equal((await res.json()).choices[0].message.content, "LEAF-B", route);
}
} finally {
await mid.cleanup();
}
});

test("config load names invalid windows instead of silently evaluating them", async () => {
const p = await startProxy({ nowMs: Date.UTC(2026, 10, 4, 2, 30) });
try {
await p.post({ model: "opencode-route/t-window-bad-wrap", messages: MSGS });
let log = "";
for (let i = 0; i < 20; i++) {
log = p.log();
if (log.includes("bad-wrap-99") && log.includes("99:99")) break;
await new Promise((r) => setTimeout(r, 100));
}
assert.ok(log.includes("bad-wrap-99"), `log names the window:\n${log}`);
assert.ok(log.includes("99:99"), `log names the bad value:\n${log}`);
assert.ok(log.includes("bad-start-2400"), `log names each bad window:\n${log}`);
} finally {
await p.cleanup();
}
});