Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(cycle): stop extract_atoms silently skipping all work on unpriced models (#3691)

Garry Tan37ad1d21049b
diff --git a/src/core/budget/budget-tracker.ts b/src/core/budget/budget-tracker.ts
index b51de3cf40..c78736c4cc 100644
--- a/src/core/budget/budget-tracker.ts
+++ b/src/core/budget/budget-tracker.ts
@@ -223,6 +223,17 @@ function lookupPricing(modelId: string, kind: BudgetKind): ModelPricing | null {
return null;
}
+/**
+ * True when the budget tracker can price this model, i.e. when setting a cost
+ * cap is meaningful. Callers that apply a *default* cap (rather than one the
+ * user asked for) should skip the cap when this returns false — otherwise
+ * `reserve()` hard-fails with BudgetExhausted(reason:'no_pricing') and the
+ * caller silently does no work.
+ */
+export function isModelPriceable(modelId: string, kind: BudgetKind): boolean {
+ return lookupPricing(modelId, kind) !== null;
+}
+
function costForUsage(modelId: string, inputTokens: number, outputTokens: number, kind: BudgetKind): number | null {
const p = lookupPricing(modelId, kind);
if (!p) return null;
diff --git a/src/core/cycle/extract-atoms.ts b/src/core/cycle/extract-atoms.ts
index 158af20f6f..46507404d0 100644
--- a/src/core/cycle/extract-atoms.ts
+++ b/src/core/cycle/extract-atoms.ts
@@ -52,7 +52,7 @@ import type { PhaseResult } from '../cycle.ts';
import type { GBrainConfig } from '../config.ts';
import type { ProgressReporter } from '../progress.ts';
import { chat as gatewayChat, withBudgetTracker } from '../ai/gateway.ts';
-import { BudgetExhausted, BudgetTracker } from '../budget/budget-tracker.ts';
+import { BudgetExhausted, BudgetTracker, isModelPriceable } from '../budget/budget-tracker.ts';
import { writeReceipt } from '../extract/receipt-writer.ts';
import { upsertExtractRollup } from '../extract/rollup-writer.ts';
import { createHash } from 'crypto';
@@ -577,8 +577,21 @@ export async function runPhaseExtractAtoms(
} catch {
// Keep safe defaults: Haiku + $0.30.
}
+ // A cost cap is only meaningful for a model the tracker can price.
+ // BudgetTracker.reserve() hard-fails with BudgetExhausted(reason:'no_pricing')
+ // when the model is absent from the pricing maps AND a cap is set; with no cap
+ // it warns once and proceeds. Because this phase always set a cap, every
+ // non-Anthropic model tripped that hard-fail on the first item, latched
+ // `budgetExhausted`, and skipped the entire workload while reporting ok.
+ const priceable = isModelPriceable(extractModel, 'chat');
+ if (!priceable) {
+ console.error(
+ `[extract_atoms] model "${extractModel}" is not in the pricing maps; ` +
+ `running without a cost gate (a cap cannot be enforced on an unpriced model).`,
+ );
+ }
const budgetTracker = new BudgetTracker({
- maxCostUsd: budgetCap,
+ maxCostUsd: priceable ? budgetCap : undefined,
label: 'cycle.extract_atoms',
});
@@ -754,7 +767,16 @@ export async function runPhaseExtractAtoms(
return {
phase: 'extract_atoms',
- status: failures.length > 0 ? 'warn' : 'ok',
+ // A phase that skipped every work item and produced nothing did not
+ // succeed, even though skips are not failures and leave failures[] empty.
+ // Reporting 'ok' there hides a total no-op behind a green status.
+ status:
+ failures.length > 0 ||
+ (work.length > 0 &&
+ totalAtomsExtracted === 0 &&
+ transcriptsSkipped + pagesSkipped === work.length)
+ ? 'warn'
+ : 'ok',
duration_ms: 0,
summary:
`extract_atoms: ${totalAtomsExtracted} atoms from ` +
diff --git a/test/extract-atoms-unpriced-model.test.ts b/test/extract-atoms-unpriced-model.test.ts
new file mode 100644
index 0000000000..e4d1d0a41f
--- /dev/null
+++ b/test/extract-atoms-unpriced-model.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, test } from 'bun:test';
+import { isModelPriceable } from '../src/core/budget/budget-tracker.ts';
+
+// Regression: extract_atoms applied its DEFAULT cost cap unconditionally.
+// BudgetTracker.reserve() hard-fails with BudgetExhausted(reason:'no_pricing')
+// when a model is absent from the pricing maps AND a cap is set, so the first
+// work item threw, `budgetExhausted` latched, and every remaining item was
+// skipped — while the phase still reported status 'ok' with an empty failures[].
+// Anthropic users never saw it; every Groq / local-llama / OpenRouter user did.
+describe('isModelPriceable', () => {
+ test('priced Anthropic chat models are priceable', () => {
+ expect(isModelPriceable('claude-haiku-4-5-20251001', 'chat')).toBe(true);
+ });
+
+ test('unknown providers are not priceable, so a default cap must be skipped', () => {
+ expect(isModelPriceable('litellm:gemma4-12b', 'chat')).toBe(false);
+ expect(isModelPriceable('llama-server:local-model', 'chat')).toBe(false);
+ });
+
+ test('is a pure predicate — no throw on unusual model ids', () => {
+ expect(() => isModelPriceable('', 'chat')).not.toThrow();
+ expect(() => isModelPriceable('provider-with-no-colon', 'chat')).not.toThrow();
+ });
+});