Skip to content

Keep the agent of a conversation that is open in a tab #1237

Open

Description

@Mistic92

Problem

I run codeg-server on a VM and use it from a laptop and a phone. A common evening for me: I start an agent on the laptop, close the lid, and pick up the same conversation on the phone in bed. By then the agent is gone.

The idle sweep ends any idle agent nobody has touched for CODEG_ACP_IDLE_TIMEOUT_SECS (180 s). Only a running page touches its connections, so the agent dies whenever no page is running:

  • the laptop goes to sleep or its browser is closed;
  • mobile Chrome freezes the page as soon as it goes to the background or the screen locks.

The conversation is still open in my tabs on both devices the whole time. It resumes when I send a message, but the CLI is a new process: its MCP servers start again, and anything it held in memory is gone.

Proposal

Do not sweep an agent whose conversation is open in a workspace tab.

The open tabs are already stored on the server (the opened_tab table, written by save_opened_tabs) and shared by every client. That is how the phone shows the same tabs as the laptop. So the server knows which conversations the user still has open, independently of which device is looking at the moment.

  • Each sweep tick reads the open conversation ids and skips connections bound to one of them.
  • Closing a tab is the explicit "I am done": it removes the conversation from the set and already ends an idle agent, as today.
  • Everything else keeps the 180 s timeout: conversations not open in any tab, historical ones, automations, codeg-cli and one-shot runs.

The memory cost stays in the user's hands. It is one agent per open tab, and the tab bar shows exactly how many that is. Unlike a longer timeout, it does not keep agents alive for pages that never come back.

A second device that opens the conversation attaches as a viewer of the running agent. I use that every day and have not missed the owner-only controls.

Scope

A small backend change, no frontend change:

  • tab_service::open_conversations returns the open conversation ids, plus their agent sessions (agent_type + external_id). A connection binds its conversation only on its first prompt, so one opened from history and only read is matched by its session, the way lock_conversation_connections already does.
  • ConnectionManager::sweep_idle_except(idle_timeout, &open) skips connections working on one of them. sweep_idle stays as is and calls it with an empty set.
  • idle_sweep_task gets the database connection and reads the set each tick. If the read fails, the tick spares nothing.

Not covered: on mobile, switching from the conversation to the file pane or to an overlay route (Automations, Tasks) unmounts the conversation view, and the frontend ends an idle owner's agent although the tab stays open. That lives in the frontend unmount logic and deserves its own change.

This follows the discussion in #1124: the timeout stays at 3 minutes, and only conversations the user explicitly keeps open are spared. I have it implemented with tests and can open a PR if this direction works for you.

Questions

  • Should this apply to the desktop app as well, or only to codeg-server? On the desktop an open window keeps touching anyway, so it only matters while the app's windows are minimised or the machine sleeps.
  • Is a safety limit wanted, for example ignoring open tabs past some number, or past a very long idle time such as a day?

Activity

  1. Mistic92 commented on Dec 6, 2026

    @Mistic92
    Author

    An update after running this for a few days.

    Since Dec 4 my codeg-server runs with #1124 and #1268 applied (on 0.51.1, now rebased on 0.52.0). Agents of open tabs survive the laptop going to sleep and the phone browser going to the background, and /loop wakeups are no longer lost.

    Memory needed a limit, though. One idle Claude Code session takes about 0.6–0.7 GB here (PSS of the whole process tree on Linux; about half of it is the stdio MCP servers each session starts). With six tabs open that is 4 GB held by idle agents, too much for a small server. So the server now keeps the agents of at most N open tabs, the ones with the most recent activity, and the others go back to the normal 180 s timeout. N comes from CODEG_ACP_OPEN_TAB_KEEP_MAX (default 3; 0 turns the exemption off). I added this to #1268.

    About #1423: it handles the opposite case. While a page is running, the frontend touches every open tab, so their agents are never swept and memory grows. #1423 stops touching tabs that have been in the background for 30 minutes. It does not help when no page is running, which is what this issue is about: a sleeping or frozen page touches nothing in either case, and the agent is still swept after 180 s.

    The two PRs overlap in one place. With #1268 the server spares the agent of an open tab whether or not a page touches it, so a tab released by #1423 would still be kept, up to the limit above. If both go in, I think the limit belongs on the server: only the server sees every client, and it also covers the case where no page is running. The 30-minute window from #1423 could become a server setting as well. I'm happy to change #1268 in whichever direction you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions