Skip to content

feat: /close command to manually terminate a thread session #40

@qijie850

Description

@qijie850

Problem

Currently the only way to free a session slot is to wait for TTL expiry or restart agent-broker. Users have no way to manually end a session when they're done with a thread.

This becomes important when session_ttl_hours is set high (e.g. 7 days) to preserve context — sessions accumulate and can hit max_sessions.

Proposed Solution

Add a /close (and /end alias) command that users can type inside a thread to:

  1. Kill the ACP child process (free RAM)
  2. Remove the session mapping (if session persistence is implemented, ref feat: per-thread isolated working directories #38)
  3. Clean up the working directory (if per-thread dirs are implemented, ref feat: per-thread isolated working directories #38)
  4. Reply with confirmation in the thread

Implementation

In discord.rs, intercept the command before passing to the ACP agent:

// In message handler, before get_or_create:
if prompt.trim().eq_ignore_ascii_case("/close") || prompt.trim().eq_ignore_ascii_case("/end") {
    if in_thread {
        let thread_key = msg.channel_id.get().to_string();
        pool.close_session(&thread_key).await?;
        msg.channel_id.say(&ctx.http, "✅ Session closed.").await?;
    }
    return;
}

In pool.rs, add a close_session method:

pub async fn close_session(&self, thread_id: &str) -> Result<()> {
    let mut conns = self.connections.write().await;
    if conns.remove(thread_id).is_some() {
        info!(thread_id, "closed active session");
    }
    // Optionally clean up working directory and persisted mapping
    Ok(())
}

Additional Considerations

  • Could also support /status to show active session count / current thread info
  • The command should only work inside threads (not in the main channel)
  • Related: feat: per-thread isolated working directories #38 (per-thread working directories — cleanup on close)

We've tested this locally and it works well. Happy to submit a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions