From ee137a9b3f9f7e8de28cdb87aeffb8b72b93c4c0 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 11:30:20 -0600 Subject: [PATCH] fix(route-proxy): attribute an overnight window's after-midnight leg to the day it opened (#3874) --- bin/opencode-route-proxy.mjs | 25 ++++++++++---- bin/opencode-route-proxy.test.mjs | 54 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/bin/opencode-route-proxy.mjs b/bin/opencode-route-proxy.mjs index 7cc7f1047e..3ba10bd8ff 100755 --- a/bin/opencode-route-proxy.mjs +++ b/bin/opencode-route-proxy.mjs @@ -402,7 +402,11 @@ const parseHHMM = (s) => { // Is window `win` active at `now` (epoch ms)? Day and wall time are // read in the window's own timezone; start inclusive, end exclusive; -// an end at or before start wraps past midnight. +// an end at or before start wraps past midnight. A wrapping window's +// 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; const start = parseHHMM(win.start); @@ -422,12 +426,21 @@ const windowActive = (win, now) => { } const get = (type) => parts.find((part) => part.type === type)?.value; const day = String(get("weekday") || "").slice(0, 3).toLowerCase(); - if (Array.isArray(win.daysOfWeek) && win.daysOfWeek.length) { - const days = win.daysOfWeek.map((d) => String(d).slice(0, 3).toLowerCase()); - if (!days.includes(day)) return false; - } + const days = + Array.isArray(win.daysOfWeek) && win.daysOfWeek.length + ? win.daysOfWeek.map((d) => String(d).slice(0, 3).toLowerCase()) + : null; + const dayListed = (d) => !days || days.includes(d); const mins = Number(get("hour")) * 60 + Number(get("minute")); - return start <= end ? mins >= start && mins < end : mins >= start || mins < end; + if (start <= end) + return dayListed(day) && mins >= start && mins < end; + if (mins >= start) return dayListed(day); + if (mins < end) { + if (!days) return true; + const idx = WEEKDAY_NAMES.indexOf(day); + return idx >= 0 && days.includes(WEEKDAY_NAMES[(idx + 6) % 7]); + } + return false; }; const targetEligible = (cfg, t, now) => { diff --git a/bin/opencode-route-proxy.test.mjs b/bin/opencode-route-proxy.test.mjs index 14a7d9c4e2..2a039b109d 100644 --- a/bin/opencode-route-proxy.test.mjs +++ b/bin/opencode-route-proxy.test.mjs @@ -178,6 +178,7 @@ 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" }, }, "model_routes": { "t-ordered": { "selection": "ordered", "targets": ["leafA/m-a", "leafB/m-b"] }, @@ -187,6 +188,7 @@ const startProxy = async (arg = {}) => { "t-window": { "selection": "ordered", "targets": [ { "model": "leafA/m-a", "window": "peak" }, "leafB/m-b" ] }, "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-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"] }, @@ -489,6 +491,58 @@ test("a gate naming an unknown window fails closed", async () => { } }); +// Overnight windows (#3874): the fixture "overnight" window is +// Fri 22:00-02:00 UTC. 2026-11-06 is a Friday, 2026-11-07 a Saturday. +// The after-midnight leg belongs to the day the window opened. +const FRI_2300 = Date.UTC(2026, 10, 6, 23, 0); +const SAT_0100 = Date.UTC(2026, 10, 7, 1, 0); +const FRI_0100 = Date.UTC(2026, 10, 6, 1, 0); +const SAT_2300 = Date.UTC(2026, 10, 7, 23, 0); + +test("overnight window is active on its opening evening", async () => { + const p = await startProxy({ nowMs: FRI_2300 }); + try { + const res = await p.post({ model: "opencode-route/t-window-overnight", messages: MSGS }); + assert.equal(res.status, 200); + assert.equal((await res.json()).choices[0].message.content, "LEAF-A"); + } finally { + await p.cleanup(); + } +}); + +test("overnight window stays active on the after-midnight leg", async () => { + const p = await startProxy({ nowMs: SAT_0100 }); + try { + const res = await p.post({ model: "opencode-route/t-window-overnight", messages: MSGS }); + assert.equal(res.status, 200); + assert.equal((await res.json()).choices[0].message.content, "LEAF-A"); + } finally { + await p.cleanup(); + } +}); + +test("overnight window is inactive on an unlisted day's after-midnight leg", async () => { + const p = await startProxy({ nowMs: FRI_0100 }); + try { + const res = await p.post({ model: "opencode-route/t-window-overnight", messages: MSGS }); + assert.equal(res.status, 200); + assert.equal((await res.json()).choices[0].message.content, "LEAF-B"); + } finally { + await p.cleanup(); + } +}); + +test("overnight window is inactive on an unlisted evening", async () => { + const p = await startProxy({ nowMs: SAT_2300 }); + try { + const res = await p.post({ model: "opencode-route/t-window-overnight", messages: MSGS }); + assert.equal(res.status, 200); + assert.equal((await res.json()).choices[0].message.content, "LEAF-B"); + } finally { + await p.cleanup(); + } +}); + test("provider route (ordered) expands to member providers in group order", async () => { const p = await startProxy(); try {