{"record":{"id":"46e5396022feef92","repo":"aaif-goose/goose","slug":"agent-session-id-not-set-initialize-terminal-inte","errorCode":null,"errorMessage":"AGENT_SESSION_ID not set. Initialize terminal integration with `goose term init <shell>` and reload your shell first.","messagePattern":"AGENT_SESSION_ID not set\\. Initialize terminal integration with `goose term init <shell>` and reload your shell first\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/goose-cli/src/commands/term.rs","lineNumber":232,"sourceCode":"\n            session\n        }\n    };\n\n    let goose_bin = std::env::current_exe()\n        .map(|p| p.to_string_lossy().into_owned())\n        .unwrap_or_else(|_| \"goose\".to_string());\n\n    println!(\n        \"{}\",\n        render_term_init_script(shell, &session.id, &goose_bin, with_command_not_found)\n    );\n    Ok(())\n}\n\npub async fn handle_term_log(command: String) -> Result<()> {\n    let session_id = std::env::var(\"AGENT_SESSION_ID\").map_err(|_| {\n        anyhow!(\n            \"AGENT_SESSION_ID not set. Initialize terminal integration with `goose term init <shell>` and reload your shell first.\"\n        )\n    })?;\n\n    let message = Message::new(\n        Role::User,\n        chrono::Utc::now().timestamp_millis(),\n        vec![MessageContent::text(command)],\n    )\n    .with_metadata(MessageMetadata::user_only())\n    .with_generated_id();\n\n    let session_manager = SessionManager::instance();\n    session_manager.add_message(&session_id, &message).await?;\n\n    Ok(())\n}\n","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/aaif-goose/goose/blob/3810898a7447ec3299be72e223d3570a7aabf0ab/crates/goose-cli/src/commands/term.rs#L214-L250","documentation":"`goose term log` records a command in the session bound to the current terminal. That binding is the AGENT_SESSION_ID environment variable, exported by the shell-integration script emitted by `goose term init <shell>`. If the variable is absent — shell never sourced the script, or the command runs outside an integrated shell — the handler has no session to append to and fails.","triggerScenarios":"Running `goose term log <cmd>` in a shell that has not sourced the init script; running from cron, a clean env subprocess, or a terminal where the integration was installed but the shell was not reloaded.","commonSituations":"Forgetting `exec $SHELL` / reopening the terminal after `goose term init`; SSH sessions without the profile; scripts stripping the environment.","solutions":["Run `goose term init <shell>` and ensure its output is evaluated in your shell profile, then reload the shell","Verify with `echo $AGENT_SESSION_ID` that the variable is set","Only call `goose term log` from an integrated terminal session"],"exampleFix":"# before\ngoose term log 'cargo build'\n# after\ngoose term init bash >> ~/.bashrc && exec bash\necho $AGENT_SESSION_ID        # non-empty now\ngoose term log 'cargo build'","handlingStrategy":"validation","validationCode":"let session_id = match std::env::var(\"AGENT_SESSION_ID\") {\n    Ok(v) => v,\n    Err(_) => {\n        eprintln!(\"run `goose term init <shell>` and reload your shell first\");\n        return Ok(());\n    }\n};","typeGuard":"fn term_integration_active() -> bool {\n    std::env::var(\"AGENT_SESSION_ID\").map(|v| !v.is_empty()).unwrap_or(false)\n}","tryCatchPattern":"if let Err(e) = handle_term_log(cmd).await {\n    if e.to_string().contains(\"AGENT_SESSION_ID not set\") {\n        // environment problem, not a command failure: guide the user, exit success\n    } else {\n        return Err(e);\n    }\n}","preventionTips":["Source the `goose term init` script from your shell profile so every shell has AGENT_SESSION_ID","Assert `test -n \"$AGENT_SESSION_ID\"` before any term command in scripts"],"tags":["term","environment","shell-integration","env-var"],"backgroundTag":null,"analyzedSha":"3810898a7447ec3299be72e223d3570a7aabf0ab","analyzedAt":"2026-08-16T10:14:26.282Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}