Skip to content

fix(automation): purge expired local-mode run workspaces - #277

Open
trungminhdo4-glitch wants to merge 4 commits into
OpenHands:mainfrom
trungminhdo4-glitch:fix/issue-271-workspace-retention
Open

fix(automation): purge expired local-mode run workspaces#277
trungminhdo4-glitch wants to merge 4 commits into
OpenHands:mainfrom
trungminhdo4-glitch:fix/issue-271-workspace-retention

Conversation

@trungminhdo4-glitch

@trungminhdo4-glitch trungminhdo4-glitch commented Jul 30, 2026

Copy link
Copy Markdown

Summary

  • add configurable retention for terminal local-mode automation workspaces
  • purge eligible workspaces during startup and periodically in bounded batches
  • protect active runs and reject unsafe paths, links, and junction escapes
  • keep filesystem retention independent from database-row retention

Why

Persistent local agent-server runs leave their workspaces under
${AUTOMATION_WORKSPACE_BASE}/automation-runs/<run_id> indefinitely.
This eventually consumes significant local disk space.

Safety properties

  • pending and running workspaces are never removed
  • candidate paths must remain under the configured workspace root
  • symlink and junction escapes are rejected
  • cleanup runs in bounded batches
  • missing directories and individual deletion failures are tolerated
  • filesystem removal is moved off the event loop
  • the periodic cleaner is owned and awaited by the application lifecycle

Tests

  • 96 targeted tests passed
  • 2 Windows symlink tests skipped because directory symlink privileges were unavailable
  • Ruff check and format check passed
  • Python compilation check passed
  • retested after synchronizing with the current main branch

A full Docker/PostgreSQL suite was not run locally.
GitHub Actions for this fork PR are awaiting maintainer approval.

Fixes #271

Disclosure: This contribution was prepared with AI assistance and independently
reviewed and tested locally before publication.

…ble retention period

Add a periodic workspace purger for local mode that safely removes
workspace directories for runs in terminal states (COMPLETED, FAILED,
CANCELLED, SKIPPED) after a configurable retention period.

- New openhands/automation/workspace_cleaner.py: purge_terminal_workspaces()
  queries terminal runs with completed_at < now - retention, deletes
  workspace dirs in bounded batches, logs bytes freed and errors.
  Never touches PENDING/RUNNING runs. Tolerates missing directories.

- New config: AUTOMATION_WORKSPACE_RETENTION_SECONDS (default 7 days),
  AUTOMATION_PURGER_INTERVAL_SECONDS (default 1 hour),
  AUTOMATION_PURGER_BATCH_SIZE (default 50).

- app.py: purger_loop background task (local mode only), initial
  startup purge, graceful shutdown.

- tests/test_workspace_cleaner.py: 18 tests covering active-run
  protection, retention cutoff, batch size, missing dirs, all terminal
  states, empty DB, and workspace_base expansion.

Fixes OpenHands#271
@github-actions github-actions Bot added the type: fix A bug fix label Jul 30, 2026
@trungminhdo4-glitch
trungminhdo4-glitch marked this pull request as ready for review July 30, 2026 21:36
@trungminhdo4-glitch

Copy link
Copy Markdown
Author

HUMAN: (Owner-authorized; posted by my agent on my behalf.)

I am supervising this contribution, and the final decisions remain mine. This PR is intentionally limited to terminal local-mode workspaces. I am treating active-run protection, bounded cleanup, path containment, and application-lifecycle ownership as the key review boundaries. The two lightweight PR checks pass; the four full fork workflows remain action_required and require maintainer approval before they can run. I will personally decide how to respond to any requested changes and remain available for technical questions.

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

Labels

type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Purge terminal local-mode automation workspaces after configurable retention period

1 participant