xai-org/grok-build · error
Error: Session ID {session_id} is already in use.
Error message
Error: Session ID {session_id} is already in use. What it means
`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.
Source
Thrown at crates/codegen/xai-grok-pager/src/app/session_startup.rs:799
/// Pre-TUI remote restore (session state and memory only).
/// Codebase checkout is never applied on this path; `--restore-code` requires `--worktree`.
const REMOTE_RESTORE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(90);
/// `--restore-code` without `--worktree` on a remote miss: refuse in-place checkout.
const REMOTE_RESTORE_NEEDS_WORKTREE: &str = "--restore-code on a remote session requires --worktree \
(refusing to check out snapshot code into the current directory)";
/// `--worktree` resume without `--restore-code`: conversation only.
pub(crate) const WORKTREE_NO_RESTORE_CODE_NOTICE: &str =
"Snapshot code will not be restored into the worktree; pass --restore-code to restore it.";
/// Preflight: preferred id must be a UUID and not a persisted session under `cwd`.
///
/// Agent `session/new` rejects non-UUID `_meta.sessionId`; fail fast here so
/// CLI users get a clear error before ACP.
pub fn ensure_session_id_available(session_id: &str, cwd: &str) -> anyhow::Result<()> {
if uuid::Uuid::try_parse(session_id).is_err() {
anyhow::bail!("Error: --session-id must be a valid UUID (got '{session_id}').");
}
if xai_grok_shell::session::persistence::session_exists_for_cwd(session_id, cwd) {
anyhow::bail!("Error: Session ID {session_id} is already in use.");
}
Ok(())
}
/// Materialize CLI intent into a concrete startup plan (I/O + remote restore).
pub async fn materialize_startup(
ctx: MaterializeCtx,
intent: SessionStartupIntent,
) -> anyhow::Result<MaterializedStartup> {
let cwd = std::env::current_dir()
.map_err(|e| anyhow::anyhow!("Failed to get cwd: {e}"))?
.to_string_lossy()
.to_string();
materialize_startup_for_cwd(ctx, intent, &cwd).await
}
/// Same as [`materialize_startup`] but with an explicit process cwd (tests, headless).
pub async fn materialize_startup_for_cwd(
ctx: MaterializeCtx,
intent: SessionStartupIntent,View on GitHub (pinned to bc7f02eddd)
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.
Example fix
// before (second run collides) xai-grok --session-id 3f8a1c2e-... // after xai-grok --session-id "$(uuidgen)" # or resume instead of new: xai-grok --resume 3f8a1c2e-...
Defensive patterns
Strategy: validation
Validate before calling
// Before creating a new session with an explicit ID, check persistence yourself.
if xai_grok_shell::session::persistence::session_exists_for_cwd(&session_id, cwd) {
session_id = uuid::Uuid::new_v4().to_string(); // or switch to --resume
}
xai_grok_pager::app::session_startup::ensure_session_id_available(&session_id, cwd)?; Type guard
fn session_id_free_for_cwd(session_id: &str, cwd: &str) -> bool {
!xai_grok_shell::session::persistence::session_exists_for_cwd(session_id, cwd)
} Try / catch
match ensure_session_id_available(&session_id, cwd) {
Err(e) if e.to_string().contains("is already in use") => {
eprintln!("ID taken; retrying with a fresh UUID or use --resume.");
let fresh = uuid::Uuid::new_v4().to_string();
ensure_session_id_available(&fresh, cwd)?;
}
other => other?,
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Failed to clear auth: {e}
- no target specified
- send failed: {body}
- screen query failed: {body}
- resize failed: {body}
AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31).
Data as JSON: /api/errors/0a3eddf981aad73a.
Report an issue: GitHub.