{"version":3,"file":"stamp-report-lineage.mjs","names":[],"sources":["../../../../../../../ai/src/utils/stamp-report-lineage.ts"],"sourcesContent":["import type { BaseReport } from \"../contracts/result/base-report.type\";\nimport { REPORT_SCHEMA_VERSION } from \"../contracts/result/base-report.type\";\n\n/**\n * Options for {@link stampReportLineage}. Every field is optional —\n * the caller supplies whichever pieces it knows. Missing pieces are\n * left untouched (existing values on the report survive).\n */\nexport type LineageStamp = {\n  /**\n   * The outermost run-id this subtree belongs to. When set, EVERY\n   * node in the subtree gets its `rootRunId` rewritten to this\n   * value — overrides any inner self-roots produced by nested\n   * `buildResult` calls.\n   */\n  rootRunId: string;\n  /**\n   * Run-id of the immediate parent of THIS subtree's top node. Each\n   * descendant's `parentRunId` is then derived from its own walk\n   * position (its parent's `runId`).\n   */\n  parentRunId?: string;\n  /**\n   * Caller-supplied session identifier — propagates to every node in\n   * the subtree. Skipped when undefined.\n   */\n  sessionId?: string;\n};\n\n/**\n * Walk a freshly-built report tree and stamp lineage fields onto\n * every node:\n *\n * - `rootRunId` — rewritten to `stamp.rootRunId` everywhere. Composite\n *   children built by inner primitives carry their own self-root; this\n *   walk overrides it so the outer root wins (single coherent run id\n *   across the whole tree).\n * - `parentRunId` — root node gets `stamp.parentRunId`; descendants\n *   derive theirs from each parent's own `runId`.\n * - `sessionId` — propagated when provided.\n * - `reportSchemaVersion` — stamped only on the root (the value is the\n *   same for the whole tree; storing it on every node would waste\n *   space).\n *\n * Designed to run ONCE per top-level `buildResult` call. Each\n * primitive's executor invokes this on the assembled root report just\n * before returning; nested primitives produced their own subtree with\n * a self-root, and this pass relinks everything to the outer caller's\n * lineage.\n *\n * Mutates the report in place — internal use only, before the report\n * is exposed via `result.report`.\n *\n * @example\n * const root = this.buildBareReport();\n * stampReportLineage(root, { rootRunId: this.runId, sessionId: this.options?.sessionId });\n * return { ..., report: root };\n */\nexport function stampReportLineage(root: BaseReport, stamp: LineageStamp): void {\n  root.reportSchemaVersion = REPORT_SCHEMA_VERSION;\n\n  walk(root, stamp.rootRunId, stamp.parentRunId, stamp.sessionId);\n}\n\nfunction walk(node: BaseReport, rootRunId: string, parentRunId?: string, sessionId?: string): void {\n  node.rootRunId = rootRunId;\n\n  if (parentRunId !== undefined) {\n    node.parentRunId = parentRunId;\n  } else {\n    delete node.parentRunId;\n  }\n\n  if (sessionId !== undefined) {\n    node.sessionId = sessionId;\n  }\n\n  for (const child of node.children) {\n    walk(child, rootRunId, node.runId, sessionId);\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,SAAgB,mBAAmB,MAAkB,OAA2B;CAC9E,KAAK;CAEL,KAAK,MAAM,MAAM,WAAW,MAAM,aAAa,MAAM,SAAS;AAChE;AAEA,SAAS,KAAK,MAAkB,WAAmB,aAAsB,WAA0B;CACjG,KAAK,YAAY;CAEjB,IAAI,gBAAgB,QAClB,KAAK,cAAc;MAEnB,OAAO,KAAK;CAGd,IAAI,cAAc,QAChB,KAAK,YAAY;CAGnB,KAAK,MAAM,SAAS,KAAK,UACvB,KAAK,OAAO,WAAW,KAAK,OAAO,SAAS;AAEhD"}