feat: pause room work on the Claude usage limit and resume after reset

Before, a Claude run that hit the account usage limit closed the open
paired task as claude_usage_limit, posted the reset time and consumed the
user's messages, so the work was lost and had to be re-requested.

Now the room is paused instead:
- The failed turn is released without touching the paired task (no
  failure count, no status change, no follow-up) and the queue keeps the
  human messages unconsumed, so the same turn can run again.
- The room is recorded in router_state `claude_usage_pause` with the reset
  time (error text, else cached usage API reset, else a 10 min retry). A
  banner time that just passed is read as "now", not tomorrow.
- While the limit lasts, paused rooms run nothing and any other room's
  Claude turn pauses that room without spawning a run (the limit is
  account-wide). A stop request still goes through and drops the room
  from the resume list.
- The scheduler tick clears the pause 1 min after the reset, posts a
  resume notice and re-queues the rooms; the pending turn or the kept
  messages then run again. The state survives restarts.

Safety refusals keep closing the task as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-13 13:14:56 +09:00
parent 1167023a13
commit aed7371cfa
19 changed files with 893 additions and 90 deletions

View File

@@ -1,17 +1,18 @@
/**
* User-facing notice for a Claude run that hit the account usage limit.
* Reset time and user-facing notice for a Claude run that hit the account
* usage limit.
*
* There is a single Claude account and no fallback provider, so the room is
* told when Claude becomes available again instead of the work being handed
* off or retried silently.
* There is a single Claude account and no fallback provider, so the room's
* work is paused until the limit resets and then resumed (see
* claude-usage-pause.ts).
*/
import type { AgentTriggerReason } from './agent-error-detection.js';
import { getCachedClaudeUsageLimitResetAt } from './claude-usage.js';
import { TIMEZONE } from './config.js';
const LIMIT_PREFIX = 'Claude 사용 한도에 도달했습니다.';
const RETRY_LATER = '잠시 후 다시 시도해 주세요.';
const PAUSE_PREFIX =
'Claude 사용 한도에 도달해서 이 방의 작업을 일시정지했습니다.';
const MONTHS: Record<string, number> = {
jan: 1,
@@ -198,26 +199,48 @@ function formatResetTime(resetAt: Date, nowMs: number): string {
return sameDay ? time : `${reset.month}월 ${reset.day}일 ${time}`;
}
export function formatClaudeUsageLimitMessage(
resetAt: Date | null,
nowMs: number = Date.now(),
): string {
if (!resetAt || resetAt.getTime() <= nowMs) {
return `${LIMIT_PREFIX} ${RETRY_LATER}`;
}
return `${LIMIT_PREFIX} ${formatResetTime(resetAt, nowMs)}에 재개할 수 있습니다.`;
/** A banner time that passed less than this long ago means "resetting now", not tomorrow. */
const JUST_PASSED_RESET_GRACE_MS = 15 * 60_000;
/** Retry delay when the limit error names no usable reset time. */
export const CLAUDE_USAGE_RETRY_FALLBACK_MS = 10 * 60_000;
export interface ClaudeUsageResume {
resumeAt: Date;
/** False when no reset time was found and resumeAt is only a retry time. */
resetKnown: boolean;
}
/**
* Notice for the room: the reset time comes from the error text, else from
* the cached usage API data, else the user is asked to retry later.
* When paused Claude work may resume: the reset time in the error text, else
* the cached usage API reset, else a short retry delay.
*
* A banner such as "resets 9:40pm" read at 21:41 is treated as "now" instead
* of tomorrow 21:40, so a limit that outlives its reset by a minute is not
* paused for a whole day.
*/
export function buildClaudeUsageLimitMessage(
export function resolveClaudeUsageResume(
errorText: string | null | undefined,
nowMs: number = Date.now(),
): string {
): ClaudeUsageResume {
const resetAt =
parseClaudeLimitResetTime(errorText, nowMs) ??
parseClaudeLimitResetTime(errorText, nowMs - JUST_PASSED_RESET_GRACE_MS) ??
getCachedClaudeUsageLimitResetAt(nowMs);
return formatClaudeUsageLimitMessage(resetAt, nowMs);
if (resetAt && resetAt.getTime() > nowMs) {
return { resumeAt: resetAt, resetKnown: true };
}
return {
resumeAt: new Date(nowMs + CLAUDE_USAGE_RETRY_FALLBACK_MS),
resetKnown: false,
};
}
export function formatClaudeUsagePauseMessage(
resume: ClaudeUsageResume,
nowMs: number = Date.now(),
): string {
const at = formatResetTime(resume.resumeAt, nowMs);
const when = resume.resetKnown
? `${at}에 사용량이 초기화되면 자동으로 이어서 진행합니다.`
: `초기화 시각을 알 수 없어서 ${at}에 다시 시도합니다.`;
return `${PAUSE_PREFIX} ${when} 그 전에 멈추려면 "중단"이라고 보내 주세요.`;
}