{"record":{"id":"d5152964edb8cc45","repo":"aaif-goose/goose","slug":"agent-session-id-not-set-initialize-terminal-int","errorCode":null,"errorMessage":"AGENT_SESSION_ID not set.\n\nInitialize terminal integration with `goose term init <shell>` in your shell profile, then restart or reload that shell.","messagePattern":"AGENT_SESSION_ID not set\\.\n\nInitialize terminal integration with `goose term init <shell>` in your shell profile, then restart or reload that shell\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/goose-cli/src/commands/term.rs","lineNumber":268,"sourceCode":"\nfn shell_history_text(newest_first_tail: &[&Message]) -> Option<String> {\n    let history: Vec<String> = newest_first_tail\n        .iter()\n        .filter(|m| !m.is_turn_context())\n        .rev()\n        .map(|m| m.as_concat_text())\n        .collect();\n    if history.is_empty() {\n        None\n    } else {\n        Some(history.join(\"\\n\"))\n    }\n}\n\npub async fn handle_term_run(prompt: Vec<String>) -> Result<()> {\n    let prompt = prompt.join(\" \");\n    let session_id = std::env::var(\"AGENT_SESSION_ID\").map_err(|_| {\n        anyhow!(\n            \"AGENT_SESSION_ID not set.\\n\\n\\\n             Initialize terminal integration with `goose term init <shell>` in your shell profile, \\\n             then restart or reload that shell.\"\n        )\n    })?;\n\n    let working_dir = std::env::current_dir()?;\n    let session_manager = SessionManager::instance();\n\n    session_manager\n        .update(&session_id)\n        .working_dir(working_dir)\n        .apply()\n        .await?;\n\n    let session = session_manager.get_session(&session_id, true).await?;\n    let user_messages_after_last_assistant: Vec<&Message> =\n        if let Some(conv) = &session.conversation {","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/aaif-goose/goose/blob/3810898a7447ec3299be72e223d3570a7aabf0ab/crates/goose-cli/src/commands/term.rs#L250-L286","documentation":"`goose term run` forwards a prompt to the agent session attached to the current terminal, identified by the AGENT_SESSION_ID env var set up by `goose term init <shell>`. Without the var there is no session to update (it also sets working_dir on that session), so the handler fails before touching the session manager.","triggerScenarios":"Running `goose term run <prompt>` in a non-integrated shell; after `env -i`; in a new terminal tab that did not source the profile containing the init script.","commonSituations":"Same as term log: shell profile not reloaded, SSH/cron contexts, or the integration script removed from rc files.","solutions":["Initialize the integration (`goose term init zsh` etc.) in your profile and restart/reload the shell","Confirm `echo $AGENT_SESSION_ID` is set before calling term run","In non-interactive contexts use `goose run`/`goose session` commands that do not depend on the env var"],"exampleFix":"# before\ngoose term run 'fix the failing test'\n# after\ngoose term init zsh > ~/.config/goose/term.zsh && echo 'source ~/.config/goose/term.zsh' >> ~/.zshrc && exec zsh\ngoose term run 'fix the failing test'","handlingStrategy":"validation","validationCode":"let Some(session_id) = std::env::var(\"AGENT_SESSION_ID\").ok().filter(|v| !v.is_empty()) else {\n    eprintln!(\"terminal integration not initialized; run `goose term init <shell>`\");\n    return Ok(());\n};","typeGuard":"fn term_integration_active() -> bool {\n    std::env::var(\"AGENT_SESSION_ID\").map(|v| !v.is_empty()).unwrap_or(false)\n}","tryCatchPattern":"match handle_term_run(prompt).await {\n    Err(e) if e.to_string().contains(\"AGENT_SESSION_ID not set\") => {\n        // fall back to a non-integrated entry point, e.g. goose run\n        run_via_goose_run(&prompt).await\n    }\n    other => other,\n}","preventionTips":["Gate term commands on the env var and fall back to `goose run` in non-integrated shells","Re-run `goose term init` after changing shells or profiles"],"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"}