fix: check every scheduled Claude attempt for the usage limit

The alignment-hold check in runTask only looked at the first attempt's
streamed trigger. A limit that came back only as the final error (no
streamed output) or on the retry after an auth refresh extended nothing.
holdAlignmentAfterClaudeTaskLimit now runs inside runTaskAttempt for
every Claude attempt and reads the streamed trigger, the final error and
the attempt error (classifyClaudeTrigger), then calls the shared
holdUntilSlotAfterLimitReset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-16 13:09:03 +09:00
parent 0a5436e4ad
commit 0d7e345d3d
2 changed files with 146 additions and 26 deletions

View File

@@ -925,6 +925,105 @@ Check the run.
);
});
function startOffSlotLimitTask(id: string): void {
vi.setSystemTime(new Date('2026-10-16T08:30:00+09:00'));
const dueAt = new Date(Date.now() - 60_000).toISOString();
createTask({
id,
group_folder: 'shared-group',
chat_jid: 'shared@g.us',
agent_type: 'claude-code',
prompt: 'claude task',
schedule_type: 'once',
schedule_value: dueAt,
context_mode: 'isolated',
next_run: dueAt,
status: 'active',
created_at: '2026-02-22T00:00:00.000Z',
});
startSchedulerLoop({
serviceAgentType: 'claude-code',
roomBindings: () => ({
'shared@g.us': {
name: 'Shared',
folder: 'shared-group',
trigger: '@Claude',
added_at: '2026-02-22T00:00:00.000Z',
agentType: 'claude-code',
},
}),
getSessions: () => ({}),
queue: {
enqueueTask: vi.fn(
(_groupJid: string, _taskId: string, fn: () => Promise<void>) => {
void fn();
},
),
} as any,
onProcess: () => {},
sendMessage: vi.fn(async () => {}),
});
}
it('holds until the next slot when a scheduled Claude task ends with a 429 error and no streamed output', async () => {
(runAgentProcessMock as any).mockImplementationOnce(async () => ({
status: 'error',
result: null,
error:
"API Error: 429 You've hit your limit · resets 9:20am (Asia/Seoul)",
}));
startOffSlotLimitTask('task-final-429');
await vi.advanceTimersByTimeAsync(10);
expect(runAgentProcessMock).toHaveBeenCalledTimes(1);
expect(getRouterState(USAGE_ALIGNMENT_HOLD_UNTIL_KEY)).toBe(
'2026-10-16T04:00:00.000Z',
);
});
it('holds until the next slot when the retry after an auth refresh hits the limit', async () => {
vi.mocked(tokenRefresh.forceRefreshToken).mockResolvedValueOnce(
'refreshed-token',
);
const authBanner =
'Failed to authenticate. API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Invalid authentication credentials"}}';
(runAgentProcessMock as any)
.mockImplementationOnce(
async (
_group: unknown,
_input: unknown,
_onProcess: unknown,
onOutput?: (output: Record<string, unknown>) => Promise<void>,
) => {
await onOutput?.({ status: 'success', result: authBanner });
return { status: 'success', result: null };
},
)
.mockImplementationOnce(
async (
_group: unknown,
_input: unknown,
_onProcess: unknown,
onOutput?: (output: Record<string, unknown>) => Promise<void>,
) => {
await onOutput?.({
status: 'success',
result: "You're out of extra usage · resets 9:20am (Asia/Seoul)",
});
return { status: 'success', result: null };
},
);
startOffSlotLimitTask('task-retry-limit');
await vi.advanceTimersByTimeAsync(10);
expect(runAgentProcessMock).toHaveBeenCalledTimes(2);
expect(getRouterState(USAGE_ALIGNMENT_HOLD_UNTIL_KEY)).toBe(
'2026-10-16T04:00:00.000Z',
);
});
it('suppresses Claude OAuth expiry banners for scheduled tasks and retries after a token refresh', async () => {
const dueAt = new Date(Date.now() - 60_000).toISOString();
createTask({