diff --git a/src/bot/AGENTS.md b/src/bot/AGENTS.md index 1b05f7e4..57e895ae 100644 --- a/src/bot/AGENTS.md +++ b/src/bot/AGENTS.md @@ -69,7 +69,7 @@ Once a mission is evaluable (`Completed`, or `Scheduled`/`Active` past its start ## Feature flow: Friday open-slot notice -At 8 PM Eastern on Wednesday (`America/New_York`, evaluated via `isNoticeDue` in `src/lib/intelligence/fridayOpsNotice.ts`; catch-up until ET midnight, a fully missed Wednesday skips to next week), if no non-cancelled mission starts on that week's upcoming Friday, `fridayOpsNotice` posts once in #side-ops pinging the Community Zeus role: the slot is open, hosting is welcome, and if nobody picks it up the slot stays empty or Jason hosts it himself. When a non-superuser creator later claims the slot (`getFridayClaimant`, earliest-created mission wins; superuser/staff-created missions never claim), the notice is edited to append "This slot was claimed by \. Thank you for hosting for our community!"; if that op is later cancelled or deleted, the claim line is stripped and the notice reverts to the open-slot state (a between-polls claim swap re-credits the new claimant). Role: `DISCORD_COMMUNITY_ZEUS_ROLE_ID` or resolved by name in the guild; channel: `DISCORD_SIDE_OPS_CHANNEL_ID`. Dedup without DB state: the message embeds a stable per-Friday `` token and the service scans recent channel messages for it (claim idempotency via a marker in the message content). Drafts (Concept) count as the slot being taken. +At 8 PM Eastern on Wednesday (`America/New_York`, evaluated via `isNoticeDue` in `src/lib/intelligence/fridayOpsNotice.ts`; catch-up until ET midnight, a fully missed Wednesday skips to next week), if no non-cancelled mission starts on that week's upcoming Friday, `fridayOpsNotice` posts once in #side-ops pinging the Community Zeus role: the slot is open, hosting is welcome, and if nobody picks it up the slot stays empty or Jason hosts it himself. The slot window is Friday 00:00–23:59:59.999 **America/New_York** computed from the Eastern calendar date, never the server clock (the UTC deploy container once treated Wed 8 PM ET as Thursday and missed Friday-evening ops entirely); when it is already Friday Eastern, that day itself is the slot, so the dedup token stays stable from Wednesday through the op day and late claims still credit. When a non-superuser creator later claims the slot (`getFridayClaimant`, earliest-created mission wins; superuser/staff-created missions never claim), the notice is edited to append "This slot was claimed by \. Thank you for hosting for our community!"; if that op is later cancelled or deleted, the claim line is stripped and the notice reverts to the open-slot state (a between-polls claim swap re-credits the new claimant). Role: `DISCORD_COMMUNITY_ZEUS_ROLE_ID` or resolved by name in the guild; channel: `DISCORD_SIDE_OPS_CHANNEL_ID`. Dedup without DB state: the message embeds a stable per-Friday `` token and the service scans recent channel messages for it (claim idempotency via a marker in the message content). Drafts (Concept) count as the slot being taken. ## Feature flow: assignment transfers diff --git a/src/lib/intelligence/fridayOpsNotice.ts b/src/lib/intelligence/fridayOpsNotice.ts index 5ab3114b..b5933bb2 100644 --- a/src/lib/intelligence/fridayOpsNotice.ts +++ b/src/lib/intelligence/fridayOpsNotice.ts @@ -46,19 +46,97 @@ export const isNoticeDue = (now: Date): boolean => { return weekday === "Wed" && hour >= NOTICE_HOUR_ET; }; +/** en-US short weekday names in JS getDay() numbering. */ +const WEEKDAY_SHORT = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; + +interface EtCalendarDate { + year: number; + /** 1-12 */ + month: number; + day: number; + /** 0 = Sunday, matching getDay() numbering. */ + weekday: number; +} + +/** The calendar date and weekday of `now` as seen in America/New_York. */ +const etDate = (now: Date): EtCalendarDate => { + const parts = new Intl.DateTimeFormat("en-US", { + timeZone: ET_TIME_ZONE, + weekday: "short", + year: "numeric", + month: "2-digit", + day: "2-digit", + }).formatToParts(now); + const get = (type: string): string => parts.find((part) => part.type === type)?.value ?? ""; + return { + year: Number.parseInt(get("year"), 10), + month: Number.parseInt(get("month"), 10), + day: Number.parseInt(get("day"), 10), + weekday: WEEKDAY_SHORT.indexOf(get("weekday")), + }; +}; + +/** The offset (Eastern wall clock minus UTC) in ms at an instant. */ +const etOffsetMs = (instant: Date): number => { + const parts = new Intl.DateTimeFormat("en-US", { + timeZone: ET_TIME_ZONE, + hourCycle: "h23", + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + }).formatToParts(instant); + const get = (type: string): string => parts.find((part) => part.type === type)?.value ?? "0"; + const wallClockAsUtc = Date.UTC( + Number.parseInt(get("year"), 10), + Number.parseInt(get("month"), 10) - 1, + Number.parseInt(get("day"), 10), + Number.parseInt(get("hour"), 10), + Number.parseInt(get("minute"), 10), + Number.parseInt(get("second"), 10), + ); + return wallClockAsUtc - Math.floor(instant.getTime() / 1000) * 1000; +}; + /** - * Start/end/epoch of the upcoming Friday (server-local), strictly after - * `now`. Used both for the mission-existence query and for the stable - * per-Friday dedup token. + * The UTC instant of midnight Eastern on a calendar date. Two offset passes + * converge for zones whose DST transitions never straddle midnight + * (America/New_York switches at 2 AM local). + */ +const etMidnightUtc = (year: number, month: number, day: number): number => { + const midnightUtc = Date.UTC(year, month - 1, day); + let ts = midnightUtc - etOffsetMs(new Date(midnightUtc)); + ts = midnightUtc - etOffsetMs(new Date(ts)); + return ts; +}; + +/** + * Start/end/epoch of the unit's Friday slot: Friday 00:00:00.000 through + * 23:59:59.999 America/New_York, computed from the Eastern calendar date so + * the window is identical on any server timezone (the deploy container runs + * UTC, where Wednesday 8 PM ET is already Thursday and an op starting Friday + * evening Eastern once fell outside the old server-local window). When `now` + * is already Friday Eastern, that day itself is the slot, keeping the dedup + * token stable from Wednesday through the op day so late claims still credit. + * Used both for the mission-existence query and for the per-Friday token. */ export const upcomingFridayRange = (now: Date): { start: Date; end: Date; epoch: number } => { - const start = new Date(now); - const daysUntilFriday = (5 - start.getDay() + 7) % 7 || 7; - start.setDate(start.getDate() + daysUntilFriday); - start.setHours(0, 0, 0, 0); - const end = new Date(start); - end.setHours(23, 59, 59, 999); - return { start, end, epoch: Math.floor(start.getTime() / 1000) }; + const et = etDate(now); + const daysUntilFriday = (5 - et.weekday + 7) % 7; + const friday = new Date(Date.UTC(et.year, et.month - 1, et.day + daysUntilFriday)); + const saturday = new Date( + Date.UTC(friday.getUTCFullYear(), friday.getUTCMonth(), friday.getUTCDate() + 1), + ); + const startMs = etMidnightUtc( + friday.getUTCFullYear(), + friday.getUTCMonth() + 1, + friday.getUTCDate(), + ); + const endMs = + etMidnightUtc(saturday.getUTCFullYear(), saturday.getUTCMonth() + 1, saturday.getUTCDate()) - 1; + return { start: new Date(startMs), end: new Date(endMs), epoch: Math.floor(startMs / 1000) }; }; /** diff --git a/tests/int/friday-ops-notice.int.spec.ts b/tests/int/friday-ops-notice.int.spec.ts index 440fe8b9..b88d5483 100644 --- a/tests/int/friday-ops-notice.int.spec.ts +++ b/tests/int/friday-ops-notice.int.spec.ts @@ -16,12 +16,19 @@ let payload: Payload; const RUN = `fon-${Date.now().toString(36)}`; // Hermetic window: 2027-01-13 is a Wednesday far enough out that the shared -// dev database holds no real missions on its Friday (2027-01-15). -const WEDNESDAY = new Date(2027, 0, 13, 12, 0, 0, 0); -const TUESDAY = new Date(2027, 0, 12, 12, 0, 0, 0); -const FRIDAY = new Date(2027, 0, 15, 12, 0, 0, 0); -const FRIDAY_EVENING = new Date(2027, 0, 15, 20, 0, 0, 0); -const SATURDAY = new Date(2027, 0, 16, 20, 0, 0, 0); +// dev database holds no real missions on its Friday (2027-01-15). All fixtures +// are explicit UTC instants so the suite behaves identically on any machine +// timezone (January 2027 is EST, UTC-5: Eastern midnight = 05:00Z). +const WEDNESDAY = new Date("2027-01-13T17:00:00Z"); // Wed 12:00 PM ET +// Wed 8:00 PM ET — already Thursday 01:00 UTC, the instant that broke the +// old server-local window math on the UTC deploy container. +const WEDNESDAY_EVENING = new Date("2027-01-14T01:00:00Z"); +const TUESDAY = new Date("2027-01-12T17:00:00Z"); // Tue 12:00 PM ET +const FRIDAY = new Date("2027-01-15T17:00:00Z"); // Fri 12:00 PM ET +const FRIDAY_EVENING = new Date("2027-01-16T01:00:00Z"); // Fri 8:00 PM ET +const SATURDAY = new Date("2027-01-17T01:00:00Z"); // Sat 8:00 PM ET +const FRIDAY_SLOT_START = "2027-01-15T05:00:00.000Z"; // Fri 00:00 EST +const FRIDAY_SLOT_END = "2027-01-16T04:59:59.999Z"; // Fri 23:59:59.999 EST describe("Friday open-slot notice", () => { let mapId: number; @@ -226,24 +233,37 @@ describe("Friday open-slot notice", () => { expect(isNoticeDue(new Date("2026-01-15T01:00:00Z"))).toBe(true); // Wed 20:00 ET (EST) }); - it("targets the upcoming Friday, strictly in the future", () => { + it("targets Friday 00:00-23:59:59.999 Eastern regardless of server timezone", () => { const fromWednesday = upcomingFridayRange(WEDNESDAY); - expect(fromWednesday.start).toEqual(new Date(2027, 0, 15, 0, 0, 0, 0)); - expect(fromWednesday.end).toEqual(new Date(2027, 0, 15, 23, 59, 59, 999)); + expect(fromWednesday.start.toISOString()).toBe(FRIDAY_SLOT_START); + expect(fromWednesday.end.toISOString()).toBe(FRIDAY_SLOT_END); expect(fromWednesday.epoch).toBe(Math.floor(fromWednesday.start.getTime() / 1000)); // Earlier in the week still lands on the same Friday. - expect(upcomingFridayRange(new Date(2027, 0, 11, 8, 0, 0, 0)).start).toEqual( - fromWednesday.start, - ); - // On a Friday itself, the next Friday is targeted instead. - expect(upcomingFridayRange(FRIDAY).start).toEqual(new Date(2027, 0, 22, 0, 0, 0, 0)); + expect(upcomingFridayRange(TUESDAY).start).toEqual(fromWednesday.start); + + // Regression (prod incident 2026-10-07): Wed 8 PM ET is Thursday UTC on + // the UTC deploy container; the slot must stay THIS week's Eastern Friday, + // and an op starting Friday 8 PM ET — the exact instant that fell outside + // the old server-local window — must count as inside it. + const fromWednesdayEvening = upcomingFridayRange(WEDNESDAY_EVENING); + expect(fromWednesdayEvening.start).toEqual(fromWednesday.start); + expect(FRIDAY_EVENING >= fromWednesdayEvening.start).toBe(true); + expect(FRIDAY_EVENING <= fromWednesdayEvening.end).toBe(true); + + // On the Friday itself (Eastern) that day is still the slot, so the + // notice keeps tracking late claims; Saturday rolls to the next Friday. + expect(upcomingFridayRange(FRIDAY).start).toEqual(fromWednesday.start); + expect(upcomingFridayRange(SATURDAY).start.toISOString()).toBe("2027-01-22T05:00:00.000Z"); }); it("builds a stable per-Friday dedup token", () => { expect(noticeToken(WEDNESDAY)).toBe(noticeToken(TUESDAY)); expect(noticeToken(WEDNESDAY)).toBe(``); - expect(noticeToken(FRIDAY)).not.toBe(noticeToken(WEDNESDAY)); + // Stable through the op day, so claims made after Thursday ET midnight + // still match the notice posted on Wednesday. + expect(noticeToken(FRIDAY)).toBe(noticeToken(WEDNESDAY)); + expect(noticeToken(SATURDAY)).not.toBe(noticeToken(WEDNESDAY)); }); it("treats the Friday slot as open only when a non-cancelled mission starts that Friday", async () => { @@ -251,6 +271,9 @@ describe("Friday open-slot notice", () => { const concept = await makeMission("concept-friday", FRIDAY_EVENING, "Concept"); await expect(hasMissionOnFriday(payload, WEDNESDAY)).resolves.toBe(true); + // Same answer when asked at Wed 8 PM ET (= Thursday UTC): a Friday-evening + // op holds the slot on any server timezone. + await expect(hasMissionOnFriday(payload, WEDNESDAY_EVENING)).resolves.toBe(true); await makeMission("cancelled-friday", FRIDAY_EVENING, "Cancelled"); await expect(hasMissionOnFriday(payload, WEDNESDAY)).resolves.toBe(true); @@ -269,6 +292,11 @@ describe("Friday open-slot notice", () => { const zeusMission = await makeMission("zeus-claim", FRIDAY_EVENING, "Scheduled", zeusId); await expect(getFridayClaimant(payload, WEDNESDAY)).resolves.toEqual({ userName: "ZEUS" }); + // A Friday-evening claim is still credited when checked at Wed 8 PM ET + // (= Thursday UTC on the deploy container). + await expect(getFridayClaimant(payload, WEDNESDAY_EVENING)).resolves.toEqual({ + userName: "ZEUS", + }); await makeMission("su-claim", FRIDAY_EVENING, "Scheduled", superuserId); await expect(getFridayClaimant(payload, WEDNESDAY)).resolves.toEqual({ userName: "ZEUS" });