{"record":{"id":"0a3eddf981aad73a","repo":"xai-org/grok-build","slug":"error-session-id-session-id-is-already-in-use","errorCode":null,"errorMessage":"Error: Session ID {session_id} is already in use.","messagePattern":"Error: Session ID (.+?) is already in use\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-pager/src/app/session_startup.rs","lineNumber":799,"sourceCode":"/// Pre-TUI remote restore (session state and memory only).\n/// Codebase checkout is never applied on this path; `--restore-code` requires `--worktree`.\nconst REMOTE_RESTORE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(90);\n/// `--restore-code` without `--worktree` on a remote miss: refuse in-place checkout.\nconst REMOTE_RESTORE_NEEDS_WORKTREE: &str = \"--restore-code on a remote session requires --worktree \\\n     (refusing to check out snapshot code into the current directory)\";\n/// `--worktree` resume without `--restore-code`: conversation only.\npub(crate) const WORKTREE_NO_RESTORE_CODE_NOTICE: &str =\n    \"Snapshot code will not be restored into the worktree; pass --restore-code to restore it.\";\n/// Preflight: preferred id must be a UUID and not a persisted session under `cwd`.\n///\n/// Agent `session/new` rejects non-UUID `_meta.sessionId`; fail fast here so\n/// CLI users get a clear error before ACP.\npub fn ensure_session_id_available(session_id: &str, cwd: &str) -> anyhow::Result<()> {\n    if uuid::Uuid::try_parse(session_id).is_err() {\n        anyhow::bail!(\"Error: --session-id must be a valid UUID (got '{session_id}').\");\n    }\n    if xai_grok_shell::session::persistence::session_exists_for_cwd(session_id, cwd) {\n        anyhow::bail!(\"Error: Session ID {session_id} is already in use.\");\n    }\n    Ok(())\n}\n/// Materialize CLI intent into a concrete startup plan (I/O + remote restore).\npub async fn materialize_startup(\n    ctx: MaterializeCtx,\n    intent: SessionStartupIntent,\n) -> anyhow::Result<MaterializedStartup> {\n    let cwd = std::env::current_dir()\n        .map_err(|e| anyhow::anyhow!(\"Failed to get cwd: {e}\"))?\n        .to_string_lossy()\n        .to_string();\n    materialize_startup_for_cwd(ctx, intent, &cwd).await\n}\n/// Same as [`materialize_startup`] but with an explicit process cwd (tests, headless).\npub async fn materialize_startup_for_cwd(\n    ctx: MaterializeCtx,\n    intent: SessionStartupIntent,","sourceCodeStart":781,"sourceCodeEnd":817,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-pager/src/app/session_startup.rs#L781-L817","documentation":"`ensure_session_id_available` checks session persistence under the working directory before starting. If `xai_grok_shell::session::persistence::session_exists_for_cwd(session_id, cwd)` finds a persisted session already using the supplied UUID for this cwd, startup bails with 'Session ID ... is already in use' to prevent clobbering or colliding with an existing session's stored state.","triggerScenarios":"Calling `ensure_session_id_available(session_id, cwd)` (or `materialize_startup_for_cwd` with `SessionStartupIntent::NewWithId`) when a previously persisted session with the same UUID already exists for the same working directory, and there is no worktree (the `has_worktree` branch skips this check).","commonSituations":"Re-running the CLI with a fixed hard-coded `--session-id` after a previous run in the same project; restoring a config or script that pins an ID already used; copying a session ID between two terminals in the same repo; stale session files left over from a crashed run.","solutions":["Generate and pass a fresh UUID for `--session-id` (e.g. `uuidgen`).","Use `--resume` (Resume intent) with that ID if you meant to continue the existing session instead of creating a new one.","Delete or archive the stale persisted session file for that cwd if it is no longer needed, then retry.","Run from a different working directory, or rely on the default `NewAuto` flow which picks an unused ID."],"exampleFix":"// before (second run collides)\nxai-grok --session-id 3f8a1c2e-...\n// after\nxai-grok --session-id \"$(uuidgen)\"\n# or resume instead of new:\nxai-grok --resume 3f8a1c2e-...","handlingStrategy":"validation","validationCode":"// Before creating a new session with an explicit ID, check persistence yourself.\nif xai_grok_shell::session::persistence::session_exists_for_cwd(&session_id, cwd) {\n    session_id = uuid::Uuid::new_v4().to_string(); // or switch to --resume\n}\nxai_grok_pager::app::session_startup::ensure_session_id_available(&session_id, cwd)?;","typeGuard":"fn session_id_free_for_cwd(session_id: &str, cwd: &str) -> bool {\n    !xai_grok_shell::session::persistence::session_exists_for_cwd(session_id, cwd)\n}","tryCatchPattern":"match ensure_session_id_available(&session_id, cwd) {\n    Err(e) if e.to_string().contains(\"is already in use\") => {\n        eprintln!(\"ID taken; retrying with a fresh UUID or use --resume.\");\n        let fresh = uuid::Uuid::new_v4().to_string();\n        ensure_session_id_available(&fresh, cwd)?;\n    }\n    other => other?,\n}","preventionTips":["Avoid hard-coding session IDs in scripts; generate a fresh UUID per new session.","Use --resume with the existing ID when you want to continue a session.","Periodically clean stale persisted session files for the project cwd.","Check session_exists_for_cwd (or list stored sessions) before picking an ID."],"tags":["cli","session-conflict","persistence","duplicate-id"],"backgroundTag":"session-id-already-in-use","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}