{"record":{"id":"ffa2aa9cc17a9961","repo":"Yeachan-Heo/oh-my-codex","slug":"unable-to-allocate-unique-autopilot-context-snapsh","errorCode":null,"errorMessage":"Unable to allocate unique Autopilot context snapshot for ${slug}","messagePattern":"Unable to allocate unique Autopilot context snapshot for (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/hooks/keyword-detector.ts","lineNumber":403,"sourceCode":"  nowIso: string,\n  body: string,\n): Promise<string> {\n  const contextDir = await ensureSafeAutopilotContextDir(sourceCwd);\n  const timestamp = utcCompactTimestamp(nowIso);\n  for (let attempt = 0; attempt < 100; attempt += 1) {\n    const suffix = attempt === 0 ? '' : `-${attempt + 1}`;\n    const filename = `${slug}-${timestamp}${suffix}.md`;\n    const relativePath = `.omx/context/${filename}`;\n    const absolutePath = resolve(contextDir, filename);\n    try {\n      await writeFile(absolutePath, body, { encoding: 'utf-8', flag: 'wx' });\n      return relativePath;\n    } catch (error) {\n      if ((error as NodeJS.ErrnoException).code === 'EEXIST') continue;\n      throw error;\n    }\n  }\n  throw new Error(`Unable to allocate unique Autopilot context snapshot for ${slug}`);\n}\n\nasync function ensureAutopilotContextSnapshot(\n  sourceCwd: string,\n  nowIso: string,\n  activationText: string,\n  existingSnapshot?: AutopilotContextSnapshotDescriptor,\n  options: { allowTaskSnapshotCreation?: boolean; recoveryReason?: AutopilotContextRecoveryReason } = {},\n): Promise<AutopilotContextSnapshotResult> {\n  if (existingSnapshot) {\n    if (isSafeAutopilotContextSnapshotPath(existingSnapshot.path)) {\n      return {\n        path: existingSnapshot.path,\n        kind: existingSnapshot.kind,\n        original_task_status: existingSnapshot.kind === 'legacy' ? 'legacy-unverified' : 'activation-prompt',\n      };\n    }\n    throw new Error(`Unsafe Autopilot context snapshot path: ${existingSnapshot.path}`);","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/Yeachan-Heo/oh-my-codex/blob/3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2/src/hooks/keyword-detector.ts#L385-L421","documentation":"Thrown by writeUniqueAutopilotContextSnapshot when it exhausts its allocation attempts: it tries to create snapshot files with an exclusive (O_EXCL-style) create, and on every attempt the candidate filename already exists (EEXIST), eventually exceeding the retry budget for the given slug. Non-EEXIST errors propagate immediately.","triggerScenarios":"Many snapshots created within the same timestamp window for the same slug (e.g. a loop or fan-out invoking the hook dozens of times in the same second), so all candidate suffixed filenames are taken; or a hostile/pre-existing set of files squatting every candidate name for that slug.","commonSituations":"Parallel test suites triggering the hook hundreds of times; automation retry storms re-invoking the detector with the same slug and timestamp; clock skew making 'now' identical across many invocations; leftover snapshot files filling the namespace.","solutions":["Reduce parallelism or stagger invocations so timestamps differ (pass distinct nowIso values per invocation)","Clean up old snapshot files for the slug under .omx/context if they're stale/disposable","Pass a more unique slug per invocation so candidate names don't collide","Report upstream if legitimate sequential usage exhausts the attempt budget — the retry count may need increasing"],"exampleFix":"// before\nfor (const item of items) await ensureAutopilotContextSnapshot(cwd, sameIso, item.slug); // same slug+time repeatedly\n\n// after\nfor (const item of items) {\n  await ensureAutopilotContextSnapshot(cwd, new Date().toISOString(), `${item.slug}-${crypto.randomUUID().slice(0,6)}`);\n}","handlingStrategy":"retry","validationCode":"import { access } from 'node:fs/promises';\n\nasync function snapshotNameLikelyFree(dir: string, slug: string, iso: string): Promise<boolean> {\n  const base = `${slug}-${iso.replace(/[-:]/g, '').replace(/\\.\\d{3}Z$/, 'Z')}`;\n  try { await access(join(dir, base + '.json')); return false; } catch { return true; }\n}","typeGuard":null,"tryCatchPattern":"for (let i = 0; i < 3; i++) {\n  try { return await ensureAutopilotContextSnapshot(cwd, new Date().toISOString(), slug); }\n  catch (err) {\n    if (!(err as Error).message.includes('Unable to allocate unique Autopilot context snapshot')) throw err;\n    slug = `${slug}-${crypto.randomUUID().slice(0, 6)}`;\n  }\n}\nthrow new Error('snapshot allocation failed');","preventionTips":["Pass distinct nowIso values when invoking in loops or parallel workers","Make slugs unique per invocation (append a short random suffix)","Periodically prune stale snapshots to keep the namespace from filling"],"tags":["file-allocation","collision","filesystem"],"backgroundTag":"file-name-collision","analyzedSha":"3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2","analyzedAt":"2026-08-27T22:18:39.783Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}