{"record":{"id":"295c338497ae8db1","repo":"JuliusBrussee/caveman","slug":"withtask-a-callback-is-required","errorCode":null,"errorMessage":"withTask: a callback is required","messagePattern":"withTask: a callback is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/mastra/src/index.ts","lineNumber":158,"sourceCode":"/** The task context of the currently executing async scope, if any. */\nexport function currentTask(): TaskContext | undefined {\n  return taskStorage.getStore();\n}\n\n/**\n * Runs `fn` with a task (and optional session) bound to the async scope, so the\n * span processor can stamp `caveman.task.id` / `caveman.session.id` onto every\n * Mastra span the agent or workflow produces underneath it.\n */\nexport function withTask<T>(taskId: string, fn: () => T): T;\nexport function withTask<T>(taskId: string, sessionId: string | undefined, fn: () => T): T;\nexport function withTask<T>(\n  taskId: string,\n  sessionIdOrFn: string | undefined | (() => T),\n  maybeFn?: () => T,\n): T {\n  const fn = typeof sessionIdOrFn === \"function\" ? sessionIdOrFn : maybeFn;\n  if (typeof fn !== \"function\") throw new Error(\"withTask: a callback is required\");\n  const id = requireNonEmpty(taskId, \"withTask: taskId\");\n  const sessionId = typeof sessionIdOrFn === \"function\" ? undefined : sessionIdOrFn;\n  const context: TaskContext = sessionId ? { taskId: id, sessionId } : { taskId: id };\n  return taskStorage.run(context, fn);\n}\n\n// ---------------------------------------------------------------------------\n// 3. Span processor\n// ---------------------------------------------------------------------------\n\n/** The structural slice of an OTel span this processor touches. */\nexport interface MastraSpanLike {\n  name?: string;\n  attributes?: Record<string, unknown>;\n  setAttribute?(key: string, value: string | number | boolean): unknown;\n  spanContext?(): { traceId?: string; spanId?: string };\n}\n","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/packages/mastra/src/index.ts#L140-L176","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"// before — callback forgotten\nwithTask(task.id, sessionId);\nawait runAgent();\n\n// after\nawait withTask(task.id, sessionId, () => runAgent());","handlingStrategy":"type-guard","validationCode":"function hasWithTaskCallback(args: unknown[]): boolean {\n  return args.length >= 2 && args.slice(1).some((a) => typeof a === 'function');\n}","typeGuard":"function isWithTaskCall(taskId: unknown, second: unknown, third?: unknown): boolean {\n  return typeof taskId === 'string' &&\n    (typeof second === 'function' || typeof third === 'function');\n}","tryCatchPattern":null,"preventionTips":["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"],"tags":["typescript","api-misuse","async-context","mastra"],"backgroundTag":"missing-callback-argument","analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}