JuliusBrussee/caveman · error
maxTools must be a positive integer
Error message
maxTools must be a positive integer
What it means
searchTools validates options.maxTools before issuing the request: it must be a safe positive integer (>= 1). Passing 0, a negative number, a non-integer, or a non-number throws this error client-side.
Solutions
- Pass a positive integer, e.g. maxTools: 5
- Omit maxTools entirely to use the server default
- Clamp the value: Math.max(1, Math.floor(Number(cfg.maxTools)))
- Coerce strings from env/config before passing: Number.parseInt(value, 10)
Example fix
// before
await searchTools(cave, catalog, query, { maxTools: 0 });
// after
await searchTools(cave, catalog, query, { maxTools: 5 }); Defensive patterns
Strategy: validation
Validate before calling
function assertMaxTools(v: unknown): number | undefined {
if (v === undefined) return undefined;
const n = Number(v);
if (!Number.isSafeInteger(n) || n < 1) throw new Error(`maxTools must be a positive integer, got ${v}`);
return n;
} Type guard
function isValidMaxTools(v: unknown): v is number {
return typeof v === "number" && Number.isSafeInteger(v) && v >= 1;
} Try / catch
try {
return await searchTools(cave, catalog, query, { maxTools });
} catch (e) {
if (/maxTools/.test(e.message)) return searchTools(cave, catalog, query);
throw e;
} Prevention
- Validate/normalize config values before passing maxTools
- Use Math.max(1, Math.trunc(n)) when deriving from user input
- Omit the option rather than passing 0 to mean 'default'
When it happens
Trigger: Calling cave.searchTools(..., { maxTools: 0 }) or negative/fractional values like 2.5 or NaN, or a string '5'.
Common situations: Computing maxTools from config where a default of 0 means 'unlimited' in the caller's own convention; parsing a query param without coercion; off-by-one when limiting results.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- artifactId is required
- assemble requires model and sessionId
- at least one report path and the canonical span fixture…
- cave artifact response missing artifact_id
- cave_budget_release_reason_required
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/76547fcbbe62c61b.
Report an issue: GitHub.
Appendix: source
Thrown at packages/sdk/typescript/src/index.ts:1580
}
}
/** The local kill switch, read at decide() time so an operator can flip it mid-process. */
function envKillSet(name: string): boolean {
const raw = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env?.[name];
if (typeof raw !== "string") return false;
const value = raw.trim().toLowerCase();
return value !== "" && value !== "0" && value !== "false" && value !== "no" && value !== "off";
}
async function toolSearch(
cave: Cave,
catalog: CaveTool[],
query: string,
options?: { maxTools?: number; context?: string; workflow?: string; ranker?: "bm25" | "embeddings"; toolSessionId?: string }
): Promise<ToolSearchResult> {
if (options?.maxTools !== undefined && (!Number.isSafeInteger(options.maxTools) || options.maxTools < 1)) {
throw new Error("maxTools must be a positive integer");
}
const body: Record<string, unknown> = {
tools: catalog.map((t) => ({
name: t.name,
description: t.description,
input_schema: t.inputSchema,
read_only: t.readOnly,
idempotent: t.idempotent,
always_load: t.alwaysLoad ?? false
})),
query
};
if (options?.context !== undefined) body["context"] = options.context;
if (options?.maxTools !== undefined) body["max_tools"] = options.maxTools;
if (options?.toolSessionId !== undefined) body["session_id"] = options.toolSessionId;
// Embedding-similarity is a pure passthrough: the SDK forwards the ranker
// choice and the gateway honors "embeddings" only when it has an embedding
// provider wired. The SDK never computes similarity itself (byte-safe, noView on GitHub (pinned to 3ee70a1026)