{"version":3,"file":"pi.cjs","names":["readStartupBootstrapStatus","DOOMPI_EXTENSIONS_PROVIDED_ENV","findSyncedRoot","acquireBootstrapClaim","pathToFileURL"],"sources":["../../src/extensions/pi.ts"],"sourcesContent":["import { pathToFileURL } from 'node:url';\n\nimport { DOOMPI_EXTENSIONS_PROVIDED_ENV } from '@agimon-ai/doompi-core/child-process';\nimport type { ExtensionAPI } from '@earendil-works/pi-coding-agent';\n\nimport { acquireBootstrapClaim } from '../builders/cli/bootstrapClaim';\nimport { findSyncedRoot, readStartupBootstrapStatus } from '../builders/cli/bootstrapLocator';\n\ntype ExtensionFactory = (api: ExtensionAPI) => void | Promise<void>;\n\nconst SESSION_START = 'session_start';\nconst WARNING = 'warning';\nconst ENABLED_FLAG = '1';\nconst UNUSABLE_STATE_MESSAGE = 'doompi could not read its synchronized state. Run doompi sync.';\n\nasync function currentBootstrap(repoRoot: string): Promise<string> {\n  const status = readStartupBootstrapStatus(repoRoot);\n  if (status.fresh && status.bootstrap) return status.bootstrap;\n  throw new Error('DoomPi synchronized bootstrap is missing or stale. Run doompi sync.');\n}\n\n/**\n * Reports a state this install cannot honour without failing Pi's startup.\n *\n * A state file written by a different DoomPi version, or by a different\n * repository, is a synchronization problem the user fixes with one command.\n * Throwing here would only turn it into an extension load failure, so the\n * detail is carried to the UI instead, which does not exist yet at load time.\n */\nfunction reportUnusableState(pi: ExtensionAPI): void {\n  pi.on(SESSION_START, (_event, ctx) => {\n    ctx.ui.notify(UNUSABLE_STATE_MESSAGE, WARNING);\n  });\n}\n\n/** Stable user-package entry that delegates to a fresh synchronized bootstrap. */\nexport async function packageBootstrap(pi: ExtensionAPI): Promise<void> {\n  // A launcher-provided aggregate already contains the complete composed set.\n  // Stand down before touching synchronized state so launch does not pay for a\n  // bootstrap it will never import.\n  if (process.env[DOOMPI_EXTENSIONS_PROVIDED_ENV] === ENABLED_FLAG) return;\n\n  const repoRoot = findSyncedRoot(process.cwd());\n  // DoomPi is registered in user settings, so it is also discovered outside a\n  // synchronized repository. Staying inert avoids loading the runtime globally.\n  if (!repoRoot) return;\n\n  // Claimed before the first await, so a sibling install registered from the\n  // other settings scope cannot interleave. It reads nothing, compiles nothing,\n  // and writes nothing: one install owns this repository's load cycle.\n  const releaseClaim = acquireBootstrapClaim(repoRoot);\n  if (!releaseClaim) return;\n\n  let bootstrap: string;\n  try {\n    bootstrap = await currentBootstrap(repoRoot);\n  } catch {\n    releaseClaim();\n    reportUnusableState(pi);\n    return;\n  }\n\n  try {\n    const module = (await import(pathToFileURL(bootstrap).href)) as { default?: unknown };\n    if (typeof module.default !== 'function') {\n      throw new Error(`DoomPi bootstrap does not export an extension factory: ${bootstrap}`);\n    }\n    await (module.default as ExtensionFactory)(pi);\n    // Released once Pi has started the runner this load produced, so a later\n    // /reload composes again instead of finding the cycle still owned.\n    pi.on(SESSION_START, () => {\n      releaseClaim();\n    });\n  } catch (error) {\n    releaseClaim();\n    throw error;\n  }\n}\n\nexport default packageBootstrap;\n"],"mappings":";;;;;;;;;AAUA,MAAM,gBAAgB;AACtB,MAAM,UAAU;AAChB,MAAM,eAAe;AACrB,MAAM,yBAAyB;AAE/B,eAAe,iBAAiB,UAAmC;CACjE,MAAM,SAASA,cAAAA,2BAA2B,QAAQ;CAClD,IAAI,OAAO,SAAS,OAAO,WAAW,OAAO,OAAO;CACpD,MAAM,IAAI,MAAM,qEAAqE;AACvF;;;;;;;;;AAUA,SAAS,oBAAoB,IAAwB;CACnD,GAAG,GAAG,gBAAgB,QAAQ,QAAQ;EACpC,IAAI,GAAG,OAAO,wBAAwB,OAAO;CAC/C,CAAC;AACH;;AAGA,eAAsB,iBAAiB,IAAiC;CAItE,IAAI,QAAQ,IAAIC,qCAAAA,oCAAoC,cAAc;CAElE,MAAM,WAAWC,cAAAA,eAAe,QAAQ,IAAI,CAAC;CAG7C,IAAI,CAAC,UAAU;CAKf,MAAM,eAAeC,uBAAAA,sBAAsB,QAAQ;CACnD,IAAI,CAAC,cAAc;CAEnB,IAAI;CACJ,IAAI;EACF,YAAY,MAAM,iBAAiB,QAAQ;CAC7C,QAAQ;EACN,aAAa;EACb,oBAAoB,EAAE;EACtB;CACF;CAEA,IAAI;EACF,MAAM,SAAU,MAAM,QAAA,GAAOC,SAAAA,cAAAA,CAAc,SAAS,CAAC,CAAC;EACtD,IAAI,OAAO,OAAO,YAAY,YAC5B,MAAM,IAAI,MAAM,0DAA0D,WAAW;EAEvF,MAAO,OAAO,QAA6B,EAAE;EAG7C,GAAG,GAAG,qBAAqB;GACzB,aAAa;EACf,CAAC;CACH,SAAS,OAAO;EACd,aAAa;EACb,MAAM;CACR;AACF"}