feat(KB-147): auto-pause engine on API usage limit errors

- Add UsageLimitPauser class and isUsageLimitError detector for rate limits, overloaded, and quota errors
- Integrate usage limit detection into executor, triage, and merger error handlers
- Wire shared UsageLimitPauser instance in dashboard startup across all agents
- Export UsageLimitPauser and isUsageLimitError from @kb/engine public API
- Add comprehensive tests for detector patterns and agent integration
This commit is contained in:
Dustin Byrne
2026-03-28 01:21:55 -04:00
parent 50821fc820
commit 90764b9657
11 changed files with 671 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import { createKbAgent } from "./pi.js";
import type { WorktreePool } from "./worktree-pool.js";
import { AgentLogger } from "./agent-logger.js";
import { mergerLog } from "./logger.js";
import { isUsageLimitError, type UsageLimitPauser } from "./usage-limit-detector.js";
/**
* Build the merge system prompt. When `includeTaskId` is true (default),
@@ -104,6 +105,8 @@ export interface MergerOptions {
/** Worktree pool — when provided and `recycleWorktrees` is enabled,
* worktrees are released to the pool instead of being removed. */
pool?: WorktreePool;
/** Usage limit pauser — triggers global pause when API limits are detected. */
usageLimitPauser?: UsageLimitPauser;
}
/**
@@ -269,6 +272,10 @@ export async function aiMergeTask(
} catch (err: any) {
// Agent failed — try to abort the merge
mergerLog.error(`Agent failed: ${err.message}`);
// Check if the error is a usage-limit error and trigger global pause
if (options.usageLimitPauser && isUsageLimitError(err.message)) {
await options.usageLimitPauser.onUsageLimitHit("merger", taskId, err.message);
}
try {
execSync("git reset --merge", { cwd: rootDir, stdio: "pipe" });
} catch { /* */ }