diff --git a/bin/opencode-route-proxy.mjs b/bin/opencode-route-proxy.mjs index 650de5d8c3..926506ab8a 100755 --- a/bin/opencode-route-proxy.mjs +++ b/bin/opencode-route-proxy.mjs @@ -611,21 +611,54 @@ const parseHHMM = (s) => { 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 WEEKDAY_NAMES = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]; + +const validTimeZone = (tz) => { + try { + new Intl.DateTimeFormat("en-US", { timeZone: tz }); + return true; + } catch { + return false; + } +}; + +// Why a window entry can never match, or null when it is well formed. +// start/end must be HH:MM in range (#4259); a present `timezone` must +// be a real Intl time zone and a present `daysOfWeek` an array of +// weekday names (#11176). windowActive fails closed (never active) for +// every problem named here, so a typo'd window surfaces as a loud +// no-match rather than flipping a route gate to always-on/always-off/ +// end-less, or (a string daysOfWeek) silently widening to every day. +const windowProblems = (win) => { + const problems = []; + if (parseHHMM(win?.start) === null || parseHHMM(win?.end) === null) + problems.push( + `start=${JSON.stringify(win?.start)} end=${JSON.stringify(win?.end)} ` + + `(expected HH:MM with hour 00-23, minute 00-59)`, + ); + if (win?.timezone != null && (typeof win.timezone !== "string" || !validTimeZone(win.timezone))) + problems.push(`timezone=${JSON.stringify(win.timezone)} (expected an IANA time zone such as "America/Chicago")`); + if ( + win?.daysOfWeek != null && + (!Array.isArray(win.daysOfWeek) || + !win.daysOfWeek.every( + (d) => typeof d === "string" && WEEKDAY_NAMES.includes(d.trim().slice(0, 3).toLowerCase()), + )) + ) + problems.push(`daysOfWeek=${JSON.stringify(win.daysOfWeek)} (expected an array of weekday names such as ["mon", "fri"])`); + return problems.length ? problems : null; +}; + +// At config load, name every model_route_windows entry that can never +// match, with the offending values, instead of silently evaluating it. 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) { + const problems = windowProblems(win); + if (problems) { 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`, + `${problems.join("; ")}; window will never match`, ); } } @@ -637,12 +670,11 @@ const warnInvalidWindows = (windows) => { // after-midnight leg belongs to the day the window opened, so its // daysOfWeek check uses the previous day (#3874): a Fri 22:00-02:00 // window is active Sat 01:00 and inactive Fri 01:00. -const WEEKDAY_NAMES = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]; const windowActive = (win, now) => { if (!win || typeof win !== "object") return false; + if (windowProblems(win)) return false; const start = parseHHMM(win.start); const end = parseHHMM(win.end); - if (start === null || end === null) return false; let parts; try { parts = new Intl.DateTimeFormat("en-US", { @@ -659,7 +691,7 @@ const windowActive = (win, now) => { const day = String(get("weekday") || "").slice(0, 3).toLowerCase(); const days = Array.isArray(win.daysOfWeek) && win.daysOfWeek.length - ? win.daysOfWeek.map((d) => String(d).slice(0, 3).toLowerCase()) + ? win.daysOfWeek.map((d) => d.trim().slice(0, 3).toLowerCase()) : null; const dayListed = (d) => !days || days.includes(d); const mins = Number(get("hour")) * 60 + Number(get("minute")); diff --git a/bin/opencode-route-proxy.test.mjs b/bin/opencode-route-proxy.test.mjs index 28243afde1..3628c5280b 100644 --- a/bin/opencode-route-proxy.test.mjs +++ b/bin/opencode-route-proxy.test.mjs @@ -325,6 +325,10 @@ const startProxy = async (arg = {}) => { "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" }, + "bad-tz": { "start": "00:00", "end": "23:59", "timezone": "America/Chicagoo" }, + "bad-days-string": { "daysOfWeek": "fri", "start": "00:00", "end": "23:59", "timezone": "UTC" }, + "bad-days-name": { "daysOfWeek": ["wed", "funday"], "start": "00:00", "end": "23:59", "timezone": "UTC" }, + "full-day-names": { "daysOfWeek": ["Wednesday"], "start": "00:00", "end": "23:59", "timezone": "UTC" }, }, "model_routes": { "t-ordered": { "selection": "ordered", "targets": ["leafA/m-a", "leafB/m-b"] }, @@ -345,6 +349,10 @@ const startProxy = async (arg = {}) => { "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-window-bad-tz": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-tz" }, "leafB/m-b" ] }, + "t-window-bad-days-string": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-days-string" }, "leafB/m-b" ] }, + "t-window-bad-days-name": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "bad-days-name" }, "leafB/m-b" ] }, + "t-window-full-day-names": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "full-day-names" }, "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"] }, @@ -1204,6 +1212,12 @@ test("out-of-range window times fail closed as start and as end", async () => { ["t-window-bad-end", AT_1200], ["t-window-bad-end2500", AT_1200], ["t-window-bad-end2500", AT_2330], + // #11176: an unknown timezone and a malformed daysOfWeek also never + // match. A string daysOfWeek used to widen to every day, and an + // unknown name beside a real one (Wed 12:00 here) used to match. + ["t-window-bad-tz", AT_1200], + ["t-window-bad-days-string", AT_1200], + ["t-window-bad-days-name", AT_1200], ]; for (const [route, nowMs] of cases) { const p = await startProxy({ nowMs }); @@ -1251,6 +1265,17 @@ test("boundary times 00:00 and 23:59 remain valid windows", async () => { } }); +test("full weekday names still match their day (#11176)", async () => { + // 2026-11-04 is a Wednesday; ["Wednesday"] normalizes to "wed". + const p = await startProxy({ nowMs: Date.UTC(2026, 10, 4, 12, 0) }); + try { + const res = await p.post({ model: "opencode-route/t-window-full-day-names", messages: MSGS }); + assert.equal((await res.json()).choices[0].message.content, "LEAF-A"); + } finally { + await p.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 { @@ -1264,6 +1289,11 @@ test("config load names invalid windows instead of silently evaluating them", as 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}`); + // #11176: a bad timezone or daysOfWeek is named with its value too. + assert.ok(log.includes("bad-tz") && log.includes("America/Chicagoo"), `log names a bad timezone:\n${log}`); + assert.ok(log.includes("bad-days-string") && log.includes('daysOfWeek="fri"'), `log names a string daysOfWeek:\n${log}`); + assert.ok(log.includes("bad-days-name") && log.includes("funday"), `log names a bad weekday:\n${log}`); + assert.ok(!log.includes('model_route_windows["full-day-names"]'), `full weekday names are valid:\n${log}`); } finally { await p.cleanup(); }