{"version":3,"file":"lock.mjs","names":[],"sources":["../../../../../../../ai/src/orchestrator/lock.ts"],"sourcesContent":["import type { CheckpointRecord } from \"../contracts/orchestrator/checkpoint-store.contract\";\nimport type { SummarizeConfig } from \"../contracts/orchestrator/orchestrator-config.type\";\nimport type { OrchestratorEngineContext } from \"./engine-context.type\";\n\n/** Framework default for the compaction-lock wait, in ms (§3 / §12.3). */\nexport const DEFAULT_LOCK_MAX_WAIT = 30_000;\n\n/** Poll interval while waiting on a held lock, in ms. */\nconst LOCK_POLL_INTERVAL = 100;\n\n/**\n * Resolve the configured `summarize.lock.maxWait`, falling back to the\n * framework default. The callback form of `summarize` carries no lock\n * config, so it uses the default.\n */\nfunction resolveMaxWait(ctx: OrchestratorEngineContext): number {\n  const summarize = ctx.config.summarize;\n\n  if (typeof summarize === \"function\" || summarize === undefined) {\n    return DEFAULT_LOCK_MAX_WAIT;\n  }\n\n  return (summarize as SummarizeConfig).lock?.maxWait ?? DEFAULT_LOCK_MAX_WAIT;\n}\n\n/** Whether a checkpoint's lock is still held relative to `now`. */\nfunction isLocked(record: CheckpointRecord, now: number): boolean {\n  if (!record.lock_expires_at) {\n    return false;\n  }\n\n  const expiresAt = Date.parse(record.lock_expires_at);\n\n  return Number.isFinite(expiresAt) && expiresAt > now;\n}\n\n/**\n * Outcome of Phase 3. `waited` is true when the turn observed a held\n * lock and spent time waiting; `waitedMs` is how long. `failedOpen` is\n * true when the wait timed out and the turn proceeds without the lock\n * (orchestrator.md §3 / §12.3 — a stuck summarizer must never block a\n * session forever).\n */\nexport type LockOutcome = {\n  waited: boolean;\n  waitedMs: number;\n  failedOpen: boolean;\n};\n\n/**\n * Phase 3 — lock check (orchestrator.md §3 / §4 Phase 3). The loaded\n * checkpoint may carry a compaction lock written by a prior turn's\n * Phase 7 (or by `command(\"compact\")`). When the lock is still live,\n * wait up to `summarize.lock.maxWait` (default 30s), re-loading the\n * latest checkpoint each poll, then **fail open** — proceed without\n * the lock.\n *\n * Emits `orchestrator.lock.waiting` once, only when a held lock is\n * observed (§14.1 — \"only when locked\"). The dispatch then runs\n * against whatever state was written before the lock was taken (§3).\n *\n * A new session (`loaded === undefined`) is never locked, so this\n * returns immediately.\n */\nexport async function acquireLock<TState>(\n  ctx: OrchestratorEngineContext<unknown, TState>,\n  sessionId: string,\n  loaded: CheckpointRecord | undefined,\n): Promise<LockOutcome> {\n  if (!loaded || !isLocked(loaded, Date.now())) {\n    return { waited: false, waitedMs: 0, failedOpen: false };\n  }\n\n  const maxWait = resolveMaxWait(ctx as OrchestratorEngineContext);\n  const startedAt = Date.now();\n\n  ctx.emitter.emit(\"orchestrator.lock.waiting\", { sessionId, waitedMs: 0 });\n\n  let latest: CheckpointRecord | undefined = loaded;\n\n  while (latest && isLocked(latest, Date.now())) {\n    const waitedMs = Date.now() - startedAt;\n\n    if (waitedMs >= maxWait) {\n      return { waited: true, waitedMs, failedOpen: true };\n    }\n\n    await delay(Math.min(LOCK_POLL_INTERVAL, maxWait - waitedMs));\n\n    latest = await ctx.checkpointStore.load(ctx.config.name, sessionId);\n  }\n\n  return { waited: true, waitedMs: Date.now() - startedAt, failedOpen: false };\n}\n\n/** Promise-based sleep used by the cooperative wait loop. */\nfunction delay(ms: number): Promise<void> {\n  return new Promise((resolve) => {\n    setTimeout(resolve, ms);\n  });\n}\n"],"mappings":";;AAKA,MAAa,wBAAwB;;AAGrC,MAAM,qBAAqB;;;;;;AAO3B,SAAS,eAAe,KAAwC;CAC9D,MAAM,YAAY,IAAI,OAAO;CAE7B,IAAI,OAAO,cAAc,cAAc,cAAc,QACnD,OAAO;CAGT,OAAQ,UAA8B,MAAM;AAC9C;;AAGA,SAAS,SAAS,QAA0B,KAAsB;CAChE,IAAI,CAAC,OAAO,iBACV,OAAO;CAGT,MAAM,YAAY,KAAK,MAAM,OAAO,eAAe;CAEnD,OAAO,OAAO,SAAS,SAAS,KAAK,YAAY;AACnD;;;;;;;;;;;;;;;;AA8BA,eAAsB,YACpB,KACA,WACA,QACsB;CAC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