diff --git a/src/codex-warmup.ts b/src/codex-warmup.ts index 3f64acb..f002f81 100644 --- a/src/codex-warmup.ts +++ b/src/codex-warmup.ts @@ -27,7 +27,7 @@ interface CodexWarmupRuntimeOptions { ignoreZeroUsageWindow?: boolean; /** * Fixed-slot primer mode: bypass the opportunistic warm-up skip gates - * (stagger, post-failure cooldown, min-interval, usage filters) so a real Codex command is attempted at every 08/13/18/23 slot and + * (stagger, post-failure cooldown, min-interval, usage filters) so a real Codex command is attempted at every 07/12/17/22 slot and * its success/failure recorded. Without this, two slots exactly 5h apart fall * just under minIntervalMs (the primer fires a few hundred ms after the slot) * and the later slot is silently skipped as `no_eligible_accounts`. diff --git a/src/message-agent-executor.test.ts b/src/message-agent-executor.test.ts index 003c3e8..c0dd7d2 100644 --- a/src/message-agent-executor.test.ts +++ b/src/message-agent-executor.test.ts @@ -3193,11 +3193,11 @@ describe('runAgentForGroup Claude failure recovery', () => { expect(pause?.until).toBe('2026-10-16T00:20:00.000Z'); expect(pause?.alignmentOnly).toBe(false); expect(routerState.get('usage_alignment_hold_until')).toBe( - '2026-10-16T04:00:00.000Z', + '2026-10-16T03:00:00.000Z', ); expect(outputs).toEqual([ expect.stringMatching( - /사용량은 09:20에 초기화되지만, .*13:00까지 기다렸다가 자동으로 이어서 진행합니다/, + /사용량은 09:20에 초기화되지만, .*12:00까지 기다렸다가 자동으로 이어서 진행합니다/, ), ]); expect(consumeRunPausedForClaudeUsage('run-off-slot-limit')).toBe(true); diff --git a/src/task-scheduler.test.ts b/src/task-scheduler.test.ts index 98e7b84..83fdc8e 100644 --- a/src/task-scheduler.test.ts +++ b/src/task-scheduler.test.ts @@ -991,9 +991,9 @@ Check the run. await vi.advanceTimersByTimeAsync(10); expect(runAgentProcessMock).toHaveBeenCalledTimes(1); - // 09:20 KST reset -> no Claude until the 13:00 KST slot. + // 09:20 KST reset -> no Claude until the 12:00 KST slot. expect(getRouterState(USAGE_ALIGNMENT_HOLD_UNTIL_KEY)).toBe( - '2026-10-16T04:00:00.000Z', + '2026-10-16T03:00:00.000Z', ); }); @@ -1050,7 +1050,7 @@ Check the run. expect(runAgentProcessMock).toHaveBeenCalledTimes(1); expect(getRouterState(USAGE_ALIGNMENT_HOLD_UNTIL_KEY)).toBe( - '2026-10-16T04:00:00.000Z', + '2026-10-16T03:00:00.000Z', ); }); @@ -1092,7 +1092,7 @@ Check the run. expect(runAgentProcessMock).toHaveBeenCalledTimes(2); expect(getRouterState(USAGE_ALIGNMENT_HOLD_UNTIL_KEY)).toBe( - '2026-10-16T04:00:00.000Z', + '2026-10-16T03:00:00.000Z', ); }); diff --git a/src/usage-alignment-hold.test.ts b/src/usage-alignment-hold.test.ts index 0fd4c77..bc15112 100644 --- a/src/usage-alignment-hold.test.ts +++ b/src/usage-alignment-hold.test.ts @@ -53,31 +53,31 @@ describe('usage alignment hold', () => { ); }); - it('holds 04:00-08:00 KST only', () => { - expect(holdUntil('2026-10-16T03:59:59')).toBeUndefined(); - expect(holdUntil('2026-10-16T04:00:00')).toBe('2026-10-15T23:00:00.000Z'); - expect(holdUntil('2026-10-16T07:59:59')).toBe('2026-10-15T23:00:00.000Z'); - expect(holdUntil('2026-10-16T08:00:00')).toBeUndefined(); - expect(holdUntil('2026-10-16T23:30:00')).toBeUndefined(); + it('holds 03:00-07:00 KST only', () => { + expect(holdUntil('2026-10-16T02:59:59')).toBeUndefined(); + expect(holdUntil('2026-10-16T03:00:00')).toBe('2026-10-15T22:00:00.000Z'); + expect(holdUntil('2026-10-16T06:59:59')).toBe('2026-10-15T22:00:00.000Z'); + expect(holdUntil('2026-10-16T07:00:00')).toBeUndefined(); + expect(holdUntil('2026-10-16T22:30:00')).toBeUndefined(); }); it('extends the hold to a one-off end, then falls back to the daily gap', () => { routerState.set( USAGE_ALIGNMENT_HOLD_UNTIL_KEY, - '2026-10-16T13:00:00+09:00', + '2026-10-16T12:00:00+09:00', ); - expect(holdUntil('2026-10-16T05:47:00')).toBe('2026-10-16T04:00:00.000Z'); - expect(holdUntil('2026-10-16T09:00:00')).toBe('2026-10-16T04:00:00.000Z'); - expect(holdUntil('2026-10-16T13:00:00')).toBeUndefined(); - expect(holdUntil('2026-10-17T05:00:00')).toBe('2026-10-16T23:00:00.000Z'); + expect(holdUntil('2026-10-16T04:47:00')).toBe('2026-10-16T03:00:00.000Z'); + expect(holdUntil('2026-10-16T08:00:00')).toBe('2026-10-16T03:00:00.000Z'); + expect(holdUntil('2026-10-16T12:00:00')).toBeUndefined(); + expect(holdUntil('2026-10-17T04:00:00')).toBe('2026-10-16T22:00:00.000Z'); }); - it('runs a one-off hold that ends inside the gap on to 08:00', () => { + it('runs a one-off hold that ends inside the gap on to 07:00', () => { routerState.set( USAGE_ALIGNMENT_HOLD_UNTIL_KEY, - '2026-10-17T05:00:00+09:00', + '2026-10-17T04:00:00+09:00', ); - expect(holdUntil('2026-10-17T02:00:00')).toBe('2026-10-16T23:00:00.000Z'); + expect(holdUntil('2026-10-17T01:00:00')).toBe('2026-10-16T22:00:00.000Z'); }); it('ignores an unreadable one-off value', () => { @@ -113,29 +113,29 @@ describe('usage alignment hold', () => { it('finds the next slot after a limit that resets off the slot grid', () => { const next = (iso: string): string | undefined => nextWindowStartAfterOffSlotReset(new Date(kst(iso)))?.toISOString(); - // On the grid (08/13/18/23 and the 23-04 window's 04), sub-second jitter + // On the grid (07/12/17/22 and the 22-03 window's 03), sub-second jitter // and a minute either side included. - expect(next('2026-10-16T13:00:00.369')).toBeUndefined(); - expect(next('2026-10-16T12:59:00')).toBeUndefined(); - expect(next('2026-10-16T04:00:00')).toBeUndefined(); - expect(next('2026-10-16T23:01:00')).toBeUndefined(); + expect(next('2026-10-16T12:00:00.369')).toBeUndefined(); + expect(next('2026-10-16T11:59:00')).toBeUndefined(); + expect(next('2026-10-16T03:00:00')).toBeUndefined(); + expect(next('2026-10-16T22:01:00')).toBeUndefined(); // Off the grid: wait for the next window start. - expect(next('2026-10-16T09:20:00')).toBe('2026-10-16T04:00:00.000Z'); - expect(next('2026-10-16T03:20:00')).toBe('2026-10-15T23:00:00.000Z'); - expect(next('2026-10-16T05:00:00')).toBe('2026-10-15T23:00:00.000Z'); - expect(next('2026-10-16T18:20:00')).toBe('2026-10-16T14:00:00.000Z'); - expect(next('2026-10-16T23:40:00')).toBe('2026-10-16T23:00:00.000Z'); + expect(next('2026-10-16T08:20:00')).toBe('2026-10-16T03:00:00.000Z'); + expect(next('2026-10-16T02:20:00')).toBe('2026-10-15T22:00:00.000Z'); + expect(next('2026-10-16T04:00:00')).toBe('2026-10-15T22:00:00.000Z'); + expect(next('2026-10-16T17:20:00')).toBe('2026-10-16T13:00:00.000Z'); + expect(next('2026-10-16T22:40:00')).toBe('2026-10-16T22:00:00.000Z'); }); it('only ever extends the one-off hold', () => { - extendUsageAlignmentHold(new Date(kst('2026-10-16T13:00:00'))); - extendUsageAlignmentHold(new Date(kst('2026-10-16T08:00:00'))); + extendUsageAlignmentHold(new Date(kst('2026-10-16T12:00:00'))); + extendUsageAlignmentHold(new Date(kst('2026-10-16T07:00:00'))); expect(routerState.get(USAGE_ALIGNMENT_HOLD_UNTIL_KEY)).toBe( - '2026-10-16T04:00:00.000Z', + '2026-10-16T03:00:00.000Z', ); - extendUsageAlignmentHold(new Date(kst('2026-10-16T18:00:00'))); + extendUsageAlignmentHold(new Date(kst('2026-10-16T17:00:00'))); expect(routerState.get(USAGE_ALIGNMENT_HOLD_UNTIL_KEY)).toBe( - '2026-10-16T09:00:00.000Z', + '2026-10-16T08:00:00.000Z', ); }); }); diff --git a/src/usage-alignment-hold.ts b/src/usage-alignment-hold.ts index ed450eb..124a6e5 100644 --- a/src/usage-alignment-hold.ts +++ b/src/usage-alignment-hold.ts @@ -2,15 +2,15 @@ * Usage-window alignment hold. * * The Claude 5h usage window opens with the first request after the previous - * one closed. The primer (usage-primer.ts) fires at 08/13/18/23 KST so the - * windows run 08-13, 13-18, 18-23 and 23-04. That only works if nothing calls - * Claude between 04:00 and 08:00 KST: a request in that gap opens an off-slot + * one closed. The primer (usage-primer.ts) fires at 07/12/17/22 KST so the + * windows run 07-12, 12-17, 17-22 and 22-03. That only works if nothing calls + * Claude between 03:00 and 07:00 KST: a request in that gap opens an off-slot * window and every later reset of the day drifts. * * While a hold is in effect, Claude work pauses (claude-usage-pause.ts keeps * the messages and resumes the rooms when it ends) and due Claude scheduled * tasks wait. Holds: - * - daily 04:00-08:00 KST; + * - daily 03:00-07:00 KST; * - a one-off hold until the time stored in router_state * `usage_alignment_hold_until` (ISO). It is ignored once that time passes, * so the daily gap is the only hold again afterwards. @@ -19,7 +19,7 @@ * until the hold ends (the user accepts the drift). * * Usage limit: when the account hits 100% and the window resets off the slot - * grid (e.g. 09:20), the first request after the reset would open an off-slot + * grid (e.g. 08:20), the first request after the reset would open an off-slot * window. The one-off hold is then extended to the next slot so the primer * opens the next window on the grid (see claude-usage-pause flow). */ @@ -29,15 +29,15 @@ import type { NewMessage } from './types.js'; export const USAGE_ALIGNMENT_HOLD_UNTIL_KEY = 'usage_alignment_hold_until'; -const GAP_START_HOUR_KST = 4; -const GAP_END_HOUR_KST = 8; +const GAP_START_HOUR_KST = 3; +const GAP_END_HOUR_KST = 7; const KST_OFFSET_MS = 9 * 60 * 60 * 1000; const HOUR_MS = 60 * 60 * 1000; const DAY_MS = 24 * HOUR_MS; /** Primer slots: the hours a usage window should start. */ -const WINDOW_START_HOURS_KST = [8, 13, 18, 23]; -/** Hours an aligned window ends (the 23-04 window ends at 04). */ -const ALIGNED_RESET_HOURS_KST = [4, 8, 13, 18, 23]; +const WINDOW_START_HOURS_KST = [7, 12, 17, 22]; +/** Hours an aligned window ends (the 22-03 window ends at 03). */ +const ALIGNED_RESET_HOURS_KST = [3, 7, 12, 17, 22]; const ALIGNED_RESET_TOLERANCE_MS = 2 * 60_000; export interface UsageAlignmentHold { @@ -152,12 +152,12 @@ export function isUsageAlignmentBypassed( } export function formatUsageAlignmentHoldMessage(at: string): string { - return `사용량 초기화 시각(08·13·18·23시)을 맞추려고 ${at}까지 이 방의 Claude 작업을 보류합니다. ${at} 이후 자동으로 이어서 진행합니다. 급하면 @태그해서 보내 주시고, 멈추려면 "중단"이라고 보내 주세요.`; + return `사용량 초기화 시각(07·12·17·22시)을 맞추려고 ${at}까지 이 방의 Claude 작업을 보류합니다. ${at} 이후 자동으로 이어서 진행합니다. 급하면 @태그해서 보내 주시고, 멈추려면 "중단"이라고 보내 주세요.`; } export function formatUsageLimitAlignedPauseMessage( resetAt: string, resumeAt: string, ): string { - return `Claude 사용 한도에 도달해서 이 방의 작업을 일시정지했습니다. 사용량은 ${resetAt}에 초기화되지만, 초기화 시각(08·13·18·23시)을 맞추려고 ${resumeAt}까지 기다렸다가 자동으로 이어서 진행합니다. 그 전에 멈추려면 "중단"이라고 보내 주세요.`; + return `Claude 사용 한도에 도달해서 이 방의 작업을 일시정지했습니다. 사용량은 ${resetAt}에 초기화되지만, 초기화 시각(07·12·17·22시)을 맞추려고 ${resumeAt}까지 기다렸다가 자동으로 이어서 진행합니다. 그 전에 멈추려면 "중단"이라고 보내 주세요.`; } diff --git a/src/usage-primer.test.ts b/src/usage-primer.test.ts index 359ac40..84b584a 100644 --- a/src/usage-primer.test.ts +++ b/src/usage-primer.test.ts @@ -77,6 +77,19 @@ describe('usage-primer', () => { ); }); + it('schedules the primer at 07/12/17/22 KST', async () => { + const { msUntilNextPrimerSlotKST } = await import('./usage-primer.js'); + const kst = (iso: string): number => Date.parse(`${iso}+09:00`); + const nextSlot = (iso: string): string => + new Date(kst(iso) + msUntilNextPrimerSlotKST(kst(iso))).toISOString(); + + expect(nextSlot('2026-10-16T03:00:00')).toBe('2026-10-15T22:00:00.000Z'); + expect(nextSlot('2026-10-16T07:00:00')).toBe('2026-10-16T03:00:00.000Z'); + expect(nextSlot('2026-10-16T12:30:00')).toBe('2026-10-16T08:00:00.000Z'); + expect(nextSlot('2026-10-16T17:59:00')).toBe('2026-10-16T13:00:00.000Z'); + expect(nextSlot('2026-10-16T22:00:00')).toBe('2026-10-16T22:00:00.000Z'); + }); + it('waits for a window that resets just after the slot, but not for an off-slot one', async () => { const { claudePrimerDelayMs } = await import('./usage-primer.js'); const slot = Date.parse('2026-10-16T13:00:00+09:00'); diff --git a/src/usage-primer.ts b/src/usage-primer.ts index 758bac7..c1e0dde 100644 --- a/src/usage-primer.ts +++ b/src/usage-primer.ts @@ -13,7 +13,7 @@ import { getUsageAlignmentHold } from './usage-alignment-hold.js'; /** * Usage-window warm-up primer. * - * Fires a tiny inference call at fixed KST slots (08, 13, 18, 23) so the + * Fires a tiny inference call at fixed KST slots (07, 12, 17, 22) so the * Claude and Codex accounts are kept warm and any usage-window issues surface in the * logs at predictable times. * @@ -26,7 +26,7 @@ import { getUsageAlignmentHold } from './usage-alignment-hold.js'; * best-effort warm-up + a success/failure record, not a reset-alignment lever. */ -const PRIMER_HOURS_KST = [8, 13, 18, 23]; +const PRIMER_HOURS_KST = [7, 12, 17, 22]; const KST_OFFSET_MS = 9 * 60 * 60 * 1000; const CLAUDE_PRIMER_TIMEOUT_MS = 60_000; const CODEX_PRIMER_MIN_INTERVAL_MS = 5 * 60 * 60 * 1000; @@ -135,7 +135,7 @@ const CLAUDE_PRIMER_RESET_MARGIN_MS = 5_000; /** * How long the primer must wait so it lands after the current 5h window * resets. A primer that reaches Claude a moment before the reset (the reset - * carries sub-second jitter, e.g. 13:00:00.37) is counted in the old window + * carries sub-second jitter, e.g. 12:00:00.37) is counted in the old window * or rejected when that window is at 100%, and the next window then opens on * whatever request comes later, off the slot. */ @@ -155,7 +155,7 @@ export function claudePrimerDelayMs( * End of a hold that outlasts this slot (one-off realignment, or the hold * after an off-slot limit reset), or null. While it lasts nothing may call * Claude; the primer at the hold's end opens the window. The slot timer can - * fire a millisecond early (07:59:59.999 for 08:00), so a hold ending at the + * fire a millisecond early (06:59:59.999 for 07:00), so a hold ending at the * slot itself does not count. */ export function claudePrimerHeldUntil(