Skip to content

Auto-pause and stop Codegraff-started localhost servers after inactivity #199

Description

@yxlyx

User problem

When Codegraff starts a localhost development/demo server, that server can remain alive for days after the user has stopped looking at it. It is easy to forget because it runs silently in an old terminal/session. Over time, multiple dev servers, file watchers, HMR workers, and related descendants can accumulate and consume memory, CPU, battery, ports, and thermal headroom without the user realizing it.

This was observed during the investigation in #198: a Next.js server on port 3002 had been alive for more than three days. It was mostly CPU-idle, but still retained memory, workers, file watchers, open browser connections, and a port. The broader machine had accumulated many other stale session-owned processes as well.

Feature request

Codegraff should supervise localhost servers that it starts and apply an explicit idle lifecycle, for example:

  1. Running while the server is actively being used.
  2. Paused/sleeping after a configurable period with no meaningful user traffic or interaction.
  3. Stopped after a longer configurable idle period.

The exact defaults and pause mechanism are open to design, but the default should prevent forgotten localhost servers from running indefinitely and invisibly.

Important behavior

  • Track the complete process group/tree, not only the initial npm/shell PID.
  • Record the command, cwd, detected listening ports, owning Codegraff session, start time, and last meaningful activity.
  • Do not treat HMR/WebSocket keepalives from a forgotten browser tab as meaningful user activity forever.
  • Warn visibly before pausing/stopping and log what happened.
  • Provide an easy resume/restart action using the original command.
  • Allow users to pin/keep alive a server that intentionally needs to run indefinitely.
  • Make pause and stop thresholds configurable globally and per server/project.
  • On Codegraff/session exit, either stop the server tree or explicitly transfer/retain it with user confirmation.
  • Only manage processes launched and owned by Codegraff. Never stop an unrelated service merely because it listens on localhost.

UI/CLI visibility

There should be a place to see managed background resources, such as:

localhost:3002  my-website  next dev  idle 42m  [Resume] [Stop] [Keep alive]

A CLI equivalent such as graff processes, graff servers, or graff cleanup would help users discover and clean up forgotten work.

Design note: “pause”

A raw SIGSTOP would retain memory and the port and cannot itself detect a new request. Alternatives could include:

  • suspend and require manual/UI resume;
  • terminate the real server while a lightweight supervisor retains metadata and offers one-click restart; or
  • keep a lightweight port proxy that restarts the server on the next request.

The implementation should make the state clear rather than claiming a process is paused while it still consumes substantial resources.

Acceptance criteria

  • A Codegraff-started localhost server is registered with its full process tree and listening port(s).
  • After configurable inactivity, it transitions according to the idle policy and the user is notified.
  • After the longer timeout, the full server process tree is gone and its resources/port are released.
  • The original command can be restarted easily.
  • A keep-alive/pin option disables idle cleanup for intentional long-running servers.
  • Existing unrelated localhost services are never affected.
  • Tests cover HMR child processes, stale browser WebSockets, session interruption, and process-tree cleanup.

Related lifecycle/process-leak report: #198.

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions