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
25 changes: 19 additions & 6 deletions bin/opencode-route-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down
54 changes: 54 additions & 0 deletions bin/opencode-route-proxy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"] },
Expand All @@ -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"] },
Expand Down Expand Up @@ -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 {
Expand Down