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:
@@ -125,7 +125,14 @@ import {
|
||||
createPairedTask,
|
||||
createTask,
|
||||
getTaskById,
|
||||
setRouterState,
|
||||
} from './db.js';
|
||||
import {
|
||||
CLAUDE_USAGE_PAUSE_KEY,
|
||||
CLAUDE_USAGE_RESUME_NOTICE,
|
||||
isRoomPausedForClaudeUsage,
|
||||
pauseRoomForClaudeUsage,
|
||||
} from './claude-usage-pause.js';
|
||||
import { TIMEZONE } from './config.js';
|
||||
import * as serviceRouting from './service-routing.js';
|
||||
import * as tokenRefresh from './token-refresh.js';
|
||||
@@ -321,6 +328,59 @@ describe('task scheduler', () => {
|
||||
expect(enqueueMessageCheck).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('resumes rooms paused by the Claude usage limit once it has reset', async () => {
|
||||
pauseRoomForClaudeUsage({
|
||||
chatJid: 'paused@g.us',
|
||||
resumeAt: new Date(Date.now() + 60 * 60_000),
|
||||
resetKnown: true,
|
||||
});
|
||||
const enqueueMessageCheck = vi.fn();
|
||||
const sendMessage = vi.fn(async () => {});
|
||||
const deps = {
|
||||
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,
|
||||
};
|
||||
|
||||
await runSchedulerTickOnce(deps);
|
||||
expect(enqueueMessageCheck).not.toHaveBeenCalled();
|
||||
expect(sendMessage).not.toHaveBeenCalled();
|
||||
|
||||
// The limit has reset (the stored reset time is now in the past).
|
||||
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() },
|
||||
}),
|
||||
);
|
||||
|
||||
await runSchedulerTickOnce(deps);
|
||||
expect(sendMessage).toHaveBeenCalledWith(
|
||||
'paused@g.us',
|
||||
CLAUDE_USAGE_RESUME_NOTICE,
|
||||
);
|
||||
expect(enqueueMessageCheck).toHaveBeenCalledWith(
|
||||
'paused@g.us',
|
||||
expect.stringContaining('paused-group'),
|
||||
);
|
||||
expect(isRoomPausedForClaudeUsage('paused@g.us')).toBe(false);
|
||||
|
||||
await runSchedulerTickOnce(deps);
|
||||
expect(enqueueMessageCheck).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('can execute one scheduler tick without starting the timer loop', async () => {
|
||||
const dueAt = new Date(Date.now() - 60_000).toISOString();
|
||||
createTask({
|
||||
|
||||
Reference in New Issue
Block a user