5.1 KiB
Plan for goose-api Review and Improvements
This document outlines the plan to address the user's request regarding goose-api's interaction with goose-cli, session sharing, and reported resource exhaustion/memory leaks. All changes will be confined to the crates/goose-api crate.
Summary of Findings
Session Sharing
- Both
goose-apiandgoose-clileverage thegoosecrate's session management, storing sessions as.jsonlfiles in a common directory (~/.local/share/goose/sessionsby default). goose-apigenerates aUuidfor each new session and returns it. This UUID is used as the session name for file persistence.goose-cli'ssession resumecommand can accept a session name or path. Therefore, the UUID returned bygoose-apican be used directly withgoose-cli session --resume --name <UUID>.
Resource Exhaustion and Memory Leaks
- Primary Suspect: Partial Stream Consumption in
agent.reply: Incrates/goose-api/src/handlers.rs, bothstart_session_handlerandreply_session_handleronly consume the first item from theBoxStreamreturned byagent.reply. Ifagent.replyproduces a stream of multiple messages (common for LLM interactions), the remaining messages and associated resources are not consumed or released, leading to memory accumulation. This is highly likely to be the root cause of single-session resource exhaustion. - Per-Session
AgentInstances:goose-apicreates a newAgentinstance for each session and stores it in an in-memoryDashMap(SESSIONS). While this provides session isolation, it means moreAgentinstances (each with its own internal state and resources) are held in memory. - Session Cleanup:
cleanup_expired_sessions()is called to remove inactive sessions from theDashMapafterSESSION_TIMEOUT_SECS(currently 1 hour). If this timeout is too long, or ifAgentinstances don't fully release resources upon being dropped, memory can accumulate. - LLM Calls for Summarization:
generate_description(ingoose::session::storage) andagent.summarize_context(ingoosecrate) involve additional LLM calls, which are resource-intensive operations. - Extension Management:
Stdioextensions can spawn external processes. If these processes are not properly terminated when their associatedAgentis dropped, they could contribute to leaks.
Detailed Plan
Phase 1: Address Immediate Resource Leak (Critical)
- Fully Consume
agent.replyStream incrates/goose-api/src/handlers.rs:-
Action: Modify
start_session_handlerandreply_session_handlerto iterate through the entireBoxStream<anyhow::Result<Message>>returned byagent.reply. All messages from the stream will be collected and concatenated to form the complete response. This ensures all resources associated with the stream are properly released. -
Mermaid Diagram for Stream Consumption:
graph TD A[Call agent.reply()] --> B{Receive BoxStream<Message>}; B --> C{Loop: stream.try_next().await}; C -- Has Message --> D[Append Message to history]; C -- No More Messages / Error --> E[Process complete response]; D --> C;
-
Phase 2: Improve Session Sharing (Documentation within goose-api)
- Clarify Session ID Usage in
crates/goose-api/README.md:- Action: Add a clear note or example in the "Session Management" section of
crates/goose-api/README.mddemonstrating that thesession_id(UUID) returned by the API can be directly used withgoose-cli session --resume --name <UUID>.
- Action: Add a clear note or example in the "Session Management" section of
Phase 3: Investigate and Mitigate Potential Resource Issues (within goose-api only)
-
Review
ApiSessionandcleanup_expired_sessionsincrates/goose-api/src/api_sessions.rs:- Action: No code change is immediately required.
- Recommendation (for user consideration): The
SESSION_TIMEOUT_SECSconstant (currently 1 hour) is a critical parameter. If resource issues persist after Phase 1, reducing this timeout (e.g., to 5-15 minutes) would cause inactiveAgentinstances to be dropped more quickly, freeing up their resources. This would be a configuration/tuning step.
-
Monitor
generate_descriptionandsummarize_contextcalls:- Action: No direct code change in
goose-apiis possible for the implementation of these functions as they reside in thegoosecrate. - Recommendation (for user consideration): These LLM calls add to the overall load. If resource issues are observed, especially during summarization, it might indicate a bottleneck in the LLM provider interaction or the
goosecrate's handling of large contexts.
- Action: No direct code change in
-
Extension Management:
- Action: No direct code change in
goose-apiis possible to fix potential leaks within thegoosecrate'sExtensionManager. - Recommendation (for user consideration): If specific
Stdioextensions are identified as problematic, the user might need to investigate their implementation or consider ifgoose-apicould offer a way to explicitly terminate processes associated with a session'sAgentwhen the session expires.
- Action: No direct code change in