Skip to content

Instantly share code, notes, and snippets.

@possibilities
Created February 4, 2026 13:56
Show Gist options
  • Select an option

  • Save possibilities/54f2adba0e95241fe53973f4874789ae to your computer and use it in GitHub Desktop.

Select an option

Save possibilities/54f2adba0e95241fe53973f4874789ae to your computer and use it in GitHub Desktop.
Browser Session Monitor Implementation Summary

Browser Session Monitor Implementation Summary

Problem

The container-side idle detection (--destroy-after-idle-ms) was ineffective because the daemon maintains a persistent CDP connection with keepalives. The cdp-activity-monitor.sh watched all CDP packets via tcpdump, but the constant keepalive traffic prevented idle detection from ever triggering.

Solution

Move idle detection to the host side by:

  1. Having the wrapper script touch activity files on every invocation
  2. A new monitor command checks activity file mtime to detect idle sessions
  3. When idle, the monitor closes sessions via the wrapper's close command

Files Changed

New Files

clis/internalctl/internalctl/run_monitor_browser_sessions.py

New command implementation that:

  • Lists *.activity files in ~/.local/share/agent-browser/
  • Checks each activity file's mtime to determine idle time
  • Closes sessions that exceed the idle threshold
  • Supports --idle-timeout, --check-interval, --dry-run, and --once options

Modified Files

system/arthack/.local/bin/arthack-agent-browser

The wrapper script now:

  • Creates STATE_DIR=~/.local/share/agent-browser on startup
  • Touches ${session_name}.activity on every invocation
  • Writes ${session_name}.container when starting a new browser (maps session to container name)
  • Handles close command specially: closes daemon, stops container, cleans up state files
  • No longer passes --destroy-after-idle-ms to browserctl

clis/internalctl/internalctl/cli.py

Added monitor-browser-sessions command with options:

  • --idle-timeout SECONDS (default: 90)
  • --check-interval SECONDS (default: 15)
  • --dry-run - show what would happen
  • --once - run once and exit (for testing)

clis/browserctl/browserctl/cli.py

Removed --destroy-after-idle-ms option from start-browser command.

clis/browserctl/browserctl/run_start_browser.py

  • Removed DESTROY_AFTER_IDLE_MS environment variable handling
  • Removed destroy_after_idle_ms from saved state

clis/browserctl/browserctl/run_show_browser.py

  • Removed destroy_after_idle_ms display logic from verbose mode
  • Simplified idle info display

clis/browserctl/browserctl/run_list_browsers.py

  • Removed destroy_after_idle_ms display logic from verbose mode
  • Simplified idle info display

clis/browserctl/container/Containerfile

  • Removed tcpdump from installed packages (was only used for CDP monitoring)
  • Removed COPY commands for cdp-activity-monitor.sh and idle-reporter.sh
  • Removed chmod for deleted scripts

clis/internalctl/internalctl/x_browser.py

  • Removed --destroy-after-idle-ms 30000 from browserctl start command
  • Existing close_browser() function handles cleanup

clis/scrapectl/scrapectl/helpers.py

  • Removed DESTROY_AFTER_IDLE_MS constant
  • Removed --destroy-after-idle-ms from browserctl start command
  • Added new close_browser() function for explicit cleanup

Deleted Files

  • clis/browserctl/container/scripts/cdp-activity-monitor.sh - Watched CDP traffic via tcpdump
  • clis/browserctl/container/scripts/idle-reporter.sh - Checked activity file mtime and killed supervisord when idle
  • clis/browserctl/container/services/cdp-activity.conf - Supervisor config for cdp-activity-monitor
  • clis/browserctl/container/services/idle-reporter.conf - Supervisor config for idle-reporter

Usage

Start the monitor (typically in a background service)

internalctl monitor-browser-sessions

With custom settings

internalctl monitor-browser-sessions --idle-timeout 60 --check-interval 10

Test mode (run once)

internalctl monitor-browser-sessions --once --dry-run

Flow

1. Agent calls: arthack-agent-browser --session myname goto https://example.com
2. Wrapper touches ~/.local/share/agent-browser/myname.activity
3. Wrapper writes container name to ~/.local/share/agent-browser/myname.container (on first start)
4. Monitor loop checks mtime of *.activity files
5. If (now - mtime) >= idle_timeout:
   - Runs: arthack-agent-browser close --session myname
   - Wrapper closes daemon, stops container, removes state files

Commits

  1. refactor(browserctl): remove container-side idle detection

    • Removes --destroy-after-idle-ms option
    • Deletes container scripts and supervisor configs
    • Updates all callers
  2. feat(internalctl): add host-side browser session monitor

    • Adds monitor-browser-sessions command
    • Updates wrapper with activity tracking and close handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment