JuliusBrussee/caveman · error
withTask: a callback is required
Error message
withTask: a callback is required
What it means
withTask binds a task context (optionally a session) to the async scope so the span processor can stamp caveman.task.id / caveman.session.id on every span beneath it. It has two shapes — withTask(taskId, fn) and withTask(taskId, sessionId, fn) — and throws when neither the second nor the third argument is a function, i.e. the callback carrying the wrapped work is missing.
Solutions
- Pass the callback as the last argument: withTask(taskId, () => { ... })
- With a session: withTask(taskId, sessionId, () => { ... })
- Pass the function itself, not its result — wrap the body in an arrow function
Example fix
// before — callback forgotten withTask(task.id, sessionId); await runAgent(); // after await withTask(task.id, sessionId, () => runAgent());
Defensive patterns
Strategy: type-guard
Validate before calling
function hasWithTaskCallback(args: unknown[]): boolean {
return args.length >= 2 && args.slice(1).some((a) => typeof a === 'function');
} Type guard
function isWithTaskCall(taskId: unknown, second: unknown, third?: unknown): boolean {
return typeof taskId === 'string' &&
(typeof second === 'function' || typeof third === 'function');
} Prevention
- Rely on the TypeScript overloads — they reject callback-less calls at compile time
- In plain JS, always end withTask calls with an arrow function
- Pass the function reference, never its invoked result
When it happens
Trigger: withTask('t1') with no callback at all; withTask('t1', sessionId) where the session was passed but the callback was forgotten; a refactor from the 2-arg to the 3-arg form that dropped the final arrow function.
Common situations: Refactoring call sites and losing the trailing callback; accidentally calling the function instead of passing it (withTask(id, runAgent()) passes the result, not the function — TypeScript flags this, plain JavaScript does not).
Related errors
- cave_mastra_max_steps_invalid
- cave_mastra_terminal_failure
- cave_mastra_usage_missing
- recordOutcome: evidence.
- recordOutcome: evidence.
AI-assisted analysis of JuliusBrussee/caveman@766dce6b13 (2026-08-18).
Data as JSON: /api/errors/295c338497ae8db1.
Report an issue: GitHub.
Appendix: source
Thrown at packages/mastra/src/index.ts:158
/** The task context of the currently executing async scope, if any. */
export function currentTask(): TaskContext | undefined {
return taskStorage.getStore();
}
/**
* Runs `fn` with a task (and optional session) bound to the async scope, so the
* span processor can stamp `caveman.task.id` / `caveman.session.id` onto every
* Mastra span the agent or workflow produces underneath it.
*/
export function withTask<T>(taskId: string, fn: () => T): T;
export function withTask<T>(taskId: string, sessionId: string | undefined, fn: () => T): T;
export function withTask<T>(
taskId: string,
sessionIdOrFn: string | undefined | (() => T),
maybeFn?: () => T,
): T {
const fn = typeof sessionIdOrFn === "function" ? sessionIdOrFn : maybeFn;
if (typeof fn !== "function") throw new Error("withTask: a callback is required");
const id = requireNonEmpty(taskId, "withTask: taskId");
const sessionId = typeof sessionIdOrFn === "function" ? undefined : sessionIdOrFn;
const context: TaskContext = sessionId ? { taskId: id, sessionId } : { taskId: id };
return taskStorage.run(context, fn);
}
// ---------------------------------------------------------------------------
// 3. Span processor
// ---------------------------------------------------------------------------
/** The structural slice of an OTel span this processor touches. */
export interface MastraSpanLike {
name?: string;
attributes?: Record<string, unknown>;
setAttribute?(key: string, value: string | number | boolean): unknown;
spanContext?(): { traceId?: string; spanId?: string };
}
View on GitHub (pinned to 766dce6b13)