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

@@ -10,6 +10,7 @@ import { executeMessageAgentAttemptLifecycle } from './message-agent-executor-li
import { runMessageAgentAttempt } from './message-agent-executor-attempt-runner.js';
import {
formatClaudeUsagePauseMessage,
formatResetTime,
isClaudeUsageLimitReason,
resolveClaudeUsageResume,
type ClaudeUsageResume,
@@ -20,6 +21,11 @@ import {
pauseRoomForClaudeUsage,
} from './claude-usage-pause.js';
import { getPairedTaskById } from './db.js';
import {
formatUsageAlignmentHoldMessage,
getUsageAlignmentHold,
isUsageAlignmentBypassed,
} from './usage-alignment-hold.js';
import { completePairedTaskWithReason } from './paired-task-status.js';
import { prepareMessageAgentExecutionTarget } from './message-agent-executor-target.js';
import { shouldRetryFreshSessionOnAgentFailure } from './session-recovery.js';
@@ -93,6 +99,8 @@ async function pauseRunForClaudeUsage(args: {
chatJid: string;
runId: string;
resume: ClaudeUsageResume;
/** Paused by the usage-alignment hold instead of a usage limit. */
alignment?: boolean;
summary: string;
pairedExecutionLifecycle: PauseLifecycle;
onOutput?: (output: AgentOutput) => Promise<void>;
@@ -101,14 +109,17 @@ async function pauseRunForClaudeUsage(args: {
chatJid: args.chatJid,
resumeAt: args.resume.resumeAt,
resetKnown: args.resume.resetKnown,
alignment: args.alignment,
});
markRunPausedForClaudeUsage(args.runId);
args.pairedExecutionLifecycle.updateSummary({ outputText: args.summary });
args.pairedExecutionLifecycle.releaseForClaudeUsagePause();
const notice = formatClaudeUsagePauseMessage({
resumeAt: pause.until,
resetKnown: pause.resetKnown,
});
const notice = args.alignment
? formatUsageAlignmentHoldMessage(formatResetTime(pause.until, Date.now()))
: formatClaudeUsagePauseMessage({
resumeAt: pause.until,
resetKnown: pause.resetKnown,
});
await args.onOutput?.({
status: 'success',
result: null,
@@ -132,20 +143,39 @@ async function pauseWhileClaudeUsageLimited(args: {
log: { warn: (obj: Record<string, unknown>, msg: string) => void };
}): Promise<boolean> {
const pause = getActiveClaudeUsagePause();
if (!pause) return false;
args.log.warn(
{ until: pause.until.toISOString(), role: args.activeRole },
'Claude usage limit still in effect; pausing the room without starting a run',
);
const hold = isUsageAlignmentBypassed(args.chatJid)
? null
: getUsageAlignmentHold();
if (!pause && !hold) return false;
try {
await pauseRunForClaudeUsage({
chatJid: args.chatJid,
runId: args.runId,
resume: { resumeAt: pause.until, resetKnown: pause.resetKnown },
summary: 'Claude usage limit still in effect',
pairedExecutionLifecycle: args.pairedExecutionLifecycle,
onOutput: args.onOutput,
});
if (pause && (!hold || pause.until >= hold.until)) {
args.log.warn(
{ until: pause.until.toISOString(), role: args.activeRole },
'Claude usage limit still in effect; pausing the room without starting a run',
);
await pauseRunForClaudeUsage({
chatJid: args.chatJid,
runId: args.runId,
resume: { resumeAt: pause.until, resetKnown: pause.resetKnown },
summary: 'Claude usage limit still in effect',
pairedExecutionLifecycle: args.pairedExecutionLifecycle,
onOutput: args.onOutput,
});
} else if (hold) {
args.log.warn(
{ until: hold.until.toISOString(), role: args.activeRole },
'Usage alignment hold in effect; pausing the room without starting a run',
);
await pauseRunForClaudeUsage({
chatJid: args.chatJid,
runId: args.runId,
resume: { resumeAt: hold.until, resetKnown: true },
alignment: true,
summary: 'Usage alignment hold in effect',
pairedExecutionLifecycle: args.pairedExecutionLifecycle,
onOutput: args.onOutput,
});
}
} finally {
await args.pairedExecutionLifecycle.asyncFinalize();
}