| 470 | } |
| 471 | |
| 472 | async function resolve( |
| 473 | id: AgentId, |
| 474 | depth: number, |
| 475 | skipScopesAbove?: AgentDefinitionScope |
| 476 | ): Promise<string> { |
| 477 | if (depth > MAX_INHERITANCE_DEPTH) { |
| 478 | throw new Error( |
| 479 | `Agent inheritance depth exceeded for '${id}' (max: ${MAX_INHERITANCE_DEPTH})` |
| 480 | ); |
| 481 | } |
| 482 | |
| 483 | const pkg = await readAgentDefinition(runtime, workspacePath, id, { |
| 484 | roots: options?.roots, |
| 485 | skipScopesAbove, |
| 486 | }); |
| 487 | |
| 488 | const visitKey = agentVisitKey(pkg.id, pkg.scope); |
| 489 | if (visited.has(visitKey)) { |
| 490 | throw new Error(`Circular agent inheritance detected: ${pkg.id} (${pkg.scope})`); |
| 491 | } |
| 492 | visited.add(visitKey); |
| 493 | |
| 494 | const baseId = pkg.frontmatter.base; |
| 495 | const shouldAppend = pkg.frontmatter.prompt?.append !== false; |
| 496 | |
| 497 | if (!baseId || !shouldAppend) { |
| 498 | return pkg.body; |
| 499 | } |
| 500 | |
| 501 | const baseBody = await resolve( |
| 502 | baseId, |
| 503 | depth + 1, |
| 504 | mergeSkipScopesAbove(skipScopesAbove, computeBaseSkipScope(baseId, id, pkg.scope)) |
| 505 | ); |
| 506 | const separator = baseBody.trim() && pkg.body.trim() ? "\n\n" : ""; |
| 507 | return `${baseBody}${separator}${pkg.body}`; |
| 508 | } |
| 509 | |
| 510 | return resolve(agentId, 0, options?.skipScopesAbove); |
| 511 | } |