{"record":{"id":"a0d98856f4b77a6c","repo":"can1357/oh-my-pi","slug":"subagent-execution-failed-error-instanceof-erro","errorCode":null,"errorMessage":"Subagent execution failed: ${error instanceof Error ? error.message : String(error)}","messagePattern":"Subagent execution failed: (.+?)","errorType":"exception","errorClass":"StructuredSubagentError","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/task/structured-subagent.ts","lineNumber":664,"sourceCode":"\t\t\telse if (result.patchPath)\n\t\t\t\tmergeSummary = `\\n\\nIsolation: changes captured at \\`${result.patchPath}\\` (apply=false). Not applied.`;\n\t\t\telse if ((result.nestedPatches?.length ?? 0) > 0)\n\t\t\t\tmergeSummary = `\\n\\nIsolation: changes captured for ${result.nestedPatches?.length} nested ${(result.nestedPatches?.length ?? 0) === 1 ? \"repository\" : \"repositories\"} (apply=false). Not applied.`;\n\t\t\telse mergeSummary = \"\\n\\nIsolation: no changes captured.\";\n\t\t}\n\n\t\tcompletedSuccessfully = result.exitCode === 0 && !result.error && !result.aborted;\n\t\treturn {\n\t\t\tresult,\n\t\t\tpolicy,\n\t\t\tmergeSummary,\n\t\t\tchangesApplied,\n\t\t\tartifactsDir: lease.artifactsDir,\n\t\t\ttemporaryArtifacts: lease.temporary,\n\t\t};\n\t} catch (error) {\n\t\tif (error instanceof StructuredSubagentError) throw error;\n\t\tthrow new StructuredSubagentError(\n\t\t\t\"execution\",\n\t\t\t`Subagent execution failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t{ cause: error },\n\t\t);\n\t} finally {\n\t\tconst shouldRetainArtifacts =\n\t\t\t(request.retainArtifacts && completedSuccessfully) ||\n\t\t\t(policy.isIsolated && (!policy.applyChanges || changesApplied === false || requiresRecoveryArtifacts));\n\t\tconst shouldCleanup = lease.temporary && !shouldRetainArtifacts;\n\t\tif (shouldCleanup) {\n\t\t\tconst cleanupArtifacts = async (): Promise<void> => {\n\t\t\t\tawait fs.rm(lease.artifactsDir, { recursive: true, force: true });\n\t\t\t\tlease.unregister?.();\n\t\t\t};\n\t\t\tif (deferredCleanup) {\n\t\t\t\ttrackLateCleanup(deferredCleanup.then(cleanupArtifacts), {\n\t\t\t\t\tresource: \"artifacts\",\n\t\t\t\t\tartifactsDir: lease.artifactsDir,","sourceCodeStart":646,"sourceCodeEnd":682,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/task/structured-subagent.ts#L646-L682","documentation":"Generic execution-stage wrapper: any error thrown while the subagent actually runs (after preflight/isolation succeed) is wrapped in a StructuredSubagentError at stage \"execution\" with the original error as `cause`. Existing StructuredSubagentErrors pass through unwrapped, so this only surfaces unexpected failures from the agent run itself.","triggerScenarios":"The spawned agent run throws — model/API errors, tool crashes, unhandled exceptions inside the subagent loop, aborted sessions — anything not already a StructuredSubagentError.","commonSituations":"LLM provider outages or auth failures during the run; a tool inside the subagent crashing; bugs in agent prompts/handlers causing unhandled rejections; network interruptions mid-run.","solutions":["Inspect `error.cause` (or the message suffix) for the root failure and fix that","Retry the task if the cause was transient (network/provider error)","Add error handling in the subagent's tools/prompts so failures surface as controlled errors"],"exampleFix":"// caller handling\ntry {\n  const result = await runStructuredSubagent(req);\n} catch (e) {\n  if (e instanceof StructuredSubagentError && e.stage === \"execution\") {\n    logger.error(\"subagent failed\", { cause: e.cause }); // inspect and retry if transient\n  }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isExecutionStageError(e: unknown): e is StructuredSubagentError {\n  return e instanceof StructuredSubagentError && e.stage === \"execution\";\n}","tryCatchPattern":"try {\n  return await runStructuredSubagent(req);\n} catch (e) {\n  if (isExecutionStageError(e)) {\n    logger.error(\"subagent execution failed\", { cause: e.cause });\n    if (isTransient(e.cause)) return withRetry(() => runStructuredSubagent(req));\n  }\n  throw e;\n}","preventionTips":["Always inspect error.cause to find the real failure","Add retry/backoff for transient provider/network causes","Handle errors inside subagent tools so failures surface as controlled StructuredSubagentErrors instead"],"tags":["subagent","execution","error-wrapping","runtime"],"backgroundTag":"subagent-execution-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}