{"version":3,"file":"backgroundWorkGate-DsnMxhe0.cjs","names":["AUTO_STOP_BLOCKING_PROVIDERS","AUTO_STOP_RUNNER_WORKING_STATUS","DOOM_BACKGROUND_WORK_SERVICE","readDoomBackgroundWorkService"],"sources":["../src/services/backgroundWorkGate/index.ts"],"sourcesContent":["import {\n  DOOM_BACKGROUND_WORK_SERVICE,\n  readDoomBackgroundWorkService,\n  type BackgroundWorkItem,\n  type DoomBackgroundWorkService,\n} from '@agimon-ai/doompi-core/backgroundWork';\nimport type { Context } from '@deepseek-ai/cordis';\n\nimport {\n  AUTO_STOP_BLOCKING_PROVIDERS,\n  AUTO_STOP_RUNNER_PROVIDER,\n  AUTO_STOP_RUNNER_WORKING_STATUS,\n} from '../../constants/idlePolicy';\n\n/** What is holding a session open, named so a stuck shutdown can say why. */\nexport interface BackgroundWorkSummary {\n  readonly active: boolean;\n  /** `provider:id` for each item the session still owns. */\n  readonly items: readonly string[];\n  /** `provider: message` for each provider whose snapshot failed. */\n  readonly errors: readonly string[];\n}\n\nexport interface BackgroundWorkGate {\n  readonly plugin: (context: Context) => void;\n  hasActiveWork(sessionId: string): boolean;\n  inspect(sessionId: string): BackgroundWorkSummary;\n}\n\nfunction holdsSessionOpen(item: BackgroundWorkItem): boolean {\n  if (!AUTO_STOP_BLOCKING_PROVIDERS.includes(item.provider)) return false;\n  // Every runner item carries its state; one without it is not known to be done.\n  if (item.provider !== AUTO_STOP_RUNNER_PROVIDER || item.status === undefined) return true;\n  return item.status === AUTO_STOP_RUNNER_WORKING_STATUS;\n}\n\nconst NO_WORK: BackgroundWorkSummary = Object.freeze({ active: false, items: [], errors: [] });\n\n/**\n * Tracks the current background-work coordinator. Only runners and subagents\n * count, and it fails closed when their snapshot is uncertain.\n */\nexport function createBackgroundWorkGate(): BackgroundWorkGate {\n  let backgroundWork: DoomBackgroundWorkService | undefined;\n  const inspect = (sessionId: string): BackgroundWorkSummary => {\n    if (!backgroundWork) return NO_WORK;\n    try {\n      const snapshot = backgroundWork.snapshot(sessionId);\n      const items = snapshot.items.filter(holdsSessionOpen).map((item) => `${item.provider}:${item.id}`);\n      const errors = snapshot.errors\n        .filter((error) => AUTO_STOP_BLOCKING_PROVIDERS.includes(error.provider))\n        .map((error) => `${error.provider}: ${error.message}`);\n      return { active: items.length > 0 || errors.length > 0, items, errors };\n    } catch (error) {\n      return {\n        active: true,\n        items: [],\n        errors: [`snapshot: ${error instanceof Error ? error.message : String(error)}`],\n      };\n    }\n  };\n  return {\n    plugin: (context) => {\n      context.inject([DOOM_BACKGROUND_WORK_SERVICE], (serviceContext) => {\n        const service = readDoomBackgroundWorkService(serviceContext);\n        if (!service) return undefined;\n        backgroundWork = service;\n        return () => {\n          if (backgroundWork === service) backgroundWork = undefined;\n        };\n      });\n    },\n    hasActiveWork: (sessionId) => inspect(sessionId).active,\n    inspect,\n  };\n}\n"],"mappings":";;;AA6BA,SAAS,iBAAiB,MAAmC;CAC3D,IAAI,CAACA,mBAAAA,6BAA6B,SAAS,KAAK,QAAQ,GAAG,OAAO;CAElE,IAAI,KAAK,aAAA,iBAA0C,KAAK,WAAW,KAAA,GAAW,OAAO;CACrF,OAAO,KAAK,WAAWC,mBAAAA;AACzB;AAEA,MAAM,UAAiC,OAAO,OAAO;CAAE,QAAQ;CAAO,OAAO,CAAC;CAAG,QAAQ,CAAC;AAAE,CAAC;;;;;AAM7F,SAAgB,2BAA+C;CAC7D,IAAI;CACJ,MAAM,WAAW,cAA6C;EAC5D,IAAI,CAAC,gBAAgB,OAAO;EAC5B,IAAI;GACF,MAAM,WAAW,eAAe,SAAS,SAAS;GAClD,MAAM,QAAQ,SAAS,MAAM,OAAO,gBAAgB,CAAC,CAAC,KAAK,SAAS,GAAG,KAAK,SAAS,GAAG,KAAK,IAAI;GACjG,MAAM,SAAS,SAAS,OACrB,QAAQ,UAAUD,mBAAAA,6BAA6B,SAAS,MAAM,QAAQ,CAAC,CAAC,CACxE,KAAK,UAAU,GAAG,MAAM,SAAS,IAAI,MAAM,SA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