fix: keep usage resets on the slot grid when Claude hits 100%

Evidence (journal): 10/04 and 10/05 08:00 and 10/12 23:00 the primer
failed (exit 1) because the account was at 100% in an off-slot window
(resets 9am / 3am); the first request after that reset then opened the
next window off the slot too, and the drift carried on.

- Usage limit that resets off the slot grid (not ~04/08/13/18/23:00 KST):
  extend the alignment hold to the next window start (08/13/18/23), so
  nothing opens a window between the reset and the primer. The pause
  notice names both times. Rooms paused by the limit stay paused
  (silently, now alignment-only, @-mention bypass works) until the hold
  ends; an expired real limit no longer counts as a limit.
- A real limit takes precedence over the hold in the pre-run gate.
- Primer: when the current 5h window (cached usage) ends within 10 min
  after the slot, wait until 5 s after that reset before firing, so the
  primer is not counted in, or rejected by, the old window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-16 06:03:38 +09:00
parent f944f97e54
commit a2b9898c0b
11 changed files with 334 additions and 10 deletions

View File

@@ -131,6 +131,7 @@ import {
CLAUDE_USAGE_PAUSE_KEY,
CLAUDE_USAGE_RESUME_NOTICE,
isRoomPausedForClaudeUsage,
isRoomPausedOnlyForAlignment,
pauseRoomForClaudeUsage,
} from './claude-usage-pause.js';
import { TIMEZONE } from './config.js';
@@ -434,6 +435,44 @@ describe('task scheduler', () => {
);
});
it('keeps limit-paused rooms paused, silently, while an alignment hold follows the reset', async () => {
setRouterState(
CLAUDE_USAGE_PAUSE_KEY,
JSON.stringify({
until: new Date(Date.now() - 2 * 60_000).toISOString(),
resetKnown: true,
rooms: { 'paused@g.us': new Date().toISOString() },
}),
);
const holdUntil = new Date(Date.now() + 60 * 60_000);
vi.mocked(usageAlignmentHold.getUsageAlignmentHold).mockReturnValueOnce({
until: holdUntil,
});
const enqueueMessageCheck = vi.fn();
const sendMessage = vi.fn(async () => {});
await runSchedulerTickOnce({
serviceAgentType: 'claude-code' as const,
roomBindings: () => ({
'paused@g.us': {
name: 'Paused',
folder: 'paused-group',
trigger: '@Andy',
added_at: '2026-10-13T00:00:00.000Z',
},
}),
getSessions: () => ({}),
queue: { enqueueTask: vi.fn(), enqueueMessageCheck } as any,
onProcess: () => {},
sendMessage,
});
expect(sendMessage).not.toHaveBeenCalled();
expect(enqueueMessageCheck).not.toHaveBeenCalled();
expect(isRoomPausedForClaudeUsage('paused@g.us')).toBe(true);
expect(isRoomPausedOnlyForAlignment('paused@g.us')).toBe(true);
});
it('can execute one scheduler tick without starting the timer loop', async () => {
const dueAt = new Date(Date.now() - 60_000).toISOString();
createTask({