{"record":{"id":"8b43579290956b19","repo":"Yeachan-Heo/oh-my-codex","slug":"autoresearch-active-mode-exists","errorCode":"autoresearch_active_mode_exists","errorMessage":"autoresearch_active_mode_exists:${String(existingModeState.run_id || 'unknown')}","messagePattern":"autoresearch_active_mode_exists:(.+?)","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/autoresearch/runtime.ts","lineNumber":913,"sourceCode":"\n  await writeInstructionsFile(contract, manifest);\n  await writeRunManifest(manifest);\n  await writeJsonFile(ledgerFile, {\n    schema_version: 1,\n    run_id: runId,\n    created_at: nowIso(),\n    updated_at: nowIso(),\n    entries: [],\n  });\n  await writeJsonFile(latestEvaluatorFile, {\n    run_id: runId,\n    status: 'not-yet-run',\n    updated_at: nowIso(),\n  });\n\n  const existingModeState = await readModeState('autoresearch', projectRoot);\n  if (existingModeState?.active) {\n    throw new Error(`autoresearch_active_mode_exists:${String(existingModeState.run_id || 'unknown')}`);\n  }\n  await startMode('autoresearch', taskDescription, 1, projectRoot);\n  await activateAutoresearchRun(manifest);\n  await updateModeState('autoresearch', {\n    current_phase: 'evaluating-baseline',\n    run_id: runId,\n    run_tag: runTag,\n    mission_dir: contract.missionDir,\n    mission_file: contract.missionFile,\n    sandbox_file: contract.sandboxFile,\n    mission_slug: contract.missionSlug,\n    repo_root: projectRoot,\n    worktree_path: worktreePath,\n    baseline_commit: baselineCommit,\n    last_kept_commit: manifest.last_kept_commit,\n    results_file: resultsFile,\n    manifest_path: manifestFile,\n    iteration_ledger_path: ledgerFile,","sourceCodeStart":895,"sourceCodeEnd":931,"githubUrl":"https://github.com/Yeachan-Heo/oh-my-codex/blob/3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2/src/autoresearch/runtime.ts#L895-L931","documentation":"During run preparation, the runtime checks the mode-state registry and throws (code autoresearch_active_mode_exists) if the 'autoresearch' mode already reports active. This is a second lock layer beyond the active-run state, preventing overlapping mode sessions in shared .omx state.","triggerScenarios":"Calling prepareAutoresearchRuntime when readModeState('autoresearch', projectRoot) returns active — e.g. after a prior run crashed without ending its mode, or when the active-run state was cleared but the mode state was not.","commonSituations":"Crashed or SIGKILLed runs leaving stale mode state, manually clearing only one of the two lock files, or nested/parallel invocations of the CLI in the same repo.","solutions":["Check for a genuinely running autoresearch process; if none, end the mode via the supported cancel/finalize path for the reported run_id","Clear the stale mode state for 'autoresearch' (or both lock states together) using the CLI's recovery command — never just one","If the run_id is 'unknown', both state files are inconsistent; reset autoresearch state via the documented recovery flow","Serialize autoresearch invocations per repo root to avoid racing the two locks"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// mirror the mode-state check before preparing a run\nconst modeState = await readModeState('autoresearch', repoRoot);\nif (modeState?.active) throw new Error(`autoresearch mode already active (${modeState.run_id ?? 'unknown'})`);","typeGuard":"function isActiveModeState(v: unknown): v is { active: true; run_id?: string } {\n  return typeof v === 'object' && v !== null && (v as any).active === true;\n}","tryCatchPattern":"try { await prepareAutoresearchRuntime(repoRoot, task); }\ncatch (e) { if (e instanceof Error && e.message.startsWith('autoresearch_active_mode_exists')) { /* end the active mode via recovery, then retry */ } throw e; }","preventionTips":["Use try/finally to end the autoresearch mode even when the run throws","When recovering from crashes, clear both active-run and mode state together","Never run two autoresearch commands concurrently in one repo"],"tags":["autoresearch","locking","mode-state","stale-state"],"backgroundTag":"concurrent-run-lock-held","analyzedSha":"3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2","analyzedAt":"2026-08-27T22:18:39.783Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}