feat: usage-alignment hold (04-08 KST daily + one-off hold until a set time)

Bring back the dawn gap that keeps Claude's 5h windows on the primer
slots (08-13, 13-18, 18-23, 23-04): it was dropped by the 2026-06-08
upstream merge (1dd948a).

- usage-alignment-hold.ts: hold during 04:00-08:00 KST, or until the ISO
  time in router_state `usage_alignment_hold_until` (ignored once past,
  so only the daily gap remains). A one-off end inside the gap runs on to
  08:00.
- Claude turns during a hold pause through the existing usage-pause path
  (messages kept, room resumed at the hold end) with an alignment notice.
  The stored pause is marked alignmentOnly so it does not act as an
  account-wide usage limit.
- Due Claude scheduled tasks stay due until the hold ends (Codex and
  GitHub CI watchers are not held).
- Urgent bypass like the old gate: a human @-mention lets that room run
  until the hold ends.
- Tests stub the wall-clock hold in test/vitest-env.ts so the suite does
  not pause when run between 04 and 08 KST.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-16 05:54:39 +09:00
parent 9dc5d9ea70
commit f944f97e54
13 changed files with 547 additions and 21 deletions

View File

@@ -134,6 +134,7 @@ import {
pauseRoomForClaudeUsage,
} from './claude-usage-pause.js';
import { TIMEZONE } from './config.js';
import * as usageAlignmentHold from './usage-alignment-hold.js';
import * as serviceRouting from './service-routing.js';
import * as tokenRefresh from './token-refresh.js';
import { createTaskStatusTracker } from './task-status-tracker.js';
@@ -381,6 +382,58 @@ describe('task scheduler', () => {
expect(enqueueMessageCheck).toHaveBeenCalledTimes(1);
});
it('holds due Claude tasks, not Codex tasks, until the usage alignment hold ends', async () => {
const dueAt = new Date(Date.now() - 60_000).toISOString();
for (const [id, agentType] of [
['task-claude-held', 'claude-code'],
['task-codex-not-held', 'codex'],
] as const) {
createTask({
id,
group_folder: 'shared-group',
chat_jid: 'shared@g.us',
agent_type: agentType,
prompt: id,
schedule_type: 'once',
schedule_value: dueAt,
context_mode: 'isolated',
next_run: dueAt,
status: 'active',
created_at: '2026-02-22T00:00:00.000Z',
});
}
const enqueueTask = vi.fn();
const deps = {
serviceAgentType: 'claude-code' as const,
roomBindings: () => ({
'shared@g.us': {
name: 'Shared',
folder: 'shared-group',
trigger: '@Andy',
added_at: '2026-02-22T00:00:00.000Z',
},
}),
getSessions: () => ({}),
queue: { enqueueTask, enqueueMessageCheck: vi.fn() } as any,
onProcess: () => {},
sendMessage: async () => {},
};
vi.mocked(usageAlignmentHold.getUsageAlignmentHold).mockReturnValueOnce({
until: new Date(Date.now() + 60 * 60_000),
});
await runSchedulerTickOnce(deps);
expect(enqueueTask.mock.calls.map((call) => call[1])).toEqual([
'task-codex-not-held',
]);
enqueueTask.mockClear();
await runSchedulerTickOnce(deps);
expect(enqueueTask.mock.calls.map((call) => call[1])).toContain(
'task-claude-held',
);
});
it('can execute one scheduler tick without starting the timer loop', async () => {
const dueAt = new Date(Date.now() - 60_000).toISOString();
createTask({