Skip to content

feat: Add global_teardown_script to run during worktree cleanup#1066

Open
matthewbjones wants to merge 1 commit into
cyrusagents:mainfrom
matthewbjones:feature/global-teardown-script
Open

feat: Add global_teardown_script to run during worktree cleanup#1066
matthewbjones wants to merge 1 commit into
cyrusagents:mainfrom
matthewbjones:feature/global-teardown-script

Conversation

@matthewbjones
Copy link
Copy Markdown

Summary

Adds global_teardown_script config option that runs before worktree deletion when an issue reaches a terminal state (Done, Canceled, or deleted). Mirrors the existing global_setup_script pattern.

The teardown script:

  • Runs in the worktree directory (can read .env.local and other config files written by the setup script)
  • Receives LINEAR_ISSUE_IDENTIFIER as an environment variable
  • Has a 2-minute timeout
  • Is non-blocking on failure — worktree deletion always proceeds

Use Case

Our Rails app's global_setup_script creates 8 isolated Postgres databases per worktree (primary, queue, cache, cable × dev + test). When Cyrus deletes the worktree, those databases are orphaned. The teardown script runs bin/worktree-teardown to drop them before the directory is removed.

Config Example

{
  "global_setup_script": "~/.cyrus/scripts/setup.sh",
  "global_teardown_script": "~/.cyrus/scripts/teardown.sh"
}

Changes

  • packages/core/src/config-schemas.ts — Added global_teardown_script to EdgeConfigSchema
  • packages/core/schemas/ — Updated JSON schema snapshots (auto-regenerated by pre-commit hook)
  • packages/edge-worker/src/GitService.ts — Added runTeardownScript method, called in deleteWorktree before directory removal
  • packages/edge-worker/src/EdgeWorker.ts — Pass global_teardown_script from config to deleteWorktree
  • packages/edge-worker/test/GitService.test.ts — 3 new tests: runs teardown before deletion, continues on failure, skips when unconfigured
  • docs/CONFIG_FILE.md — Documented the new option
  • docs/SETUP_SCRIPTS.md — Added teardown section with examples and error handling docs
  • CHANGELOG.md — Added entry under [Unreleased]

Testing

All existing tests pass (1,259 total). 3 new tests added:

  • Runs global teardown script in worktree directory with correct env vars
  • Continues with deletion when teardown script fails
  • Skips teardown when no script is configured

Closes #1065

Add support for a global teardown script that runs before worktree
deletion when an issue reaches a terminal state (Done, Canceled, or
deleted). Mirrors the existing global_setup_script pattern.

The teardown script runs in the worktree directory so it can read
configuration files (e.g. .env.local) written by the setup script.
Non-blocking on failure — worktree deletion always proceeds.

Use case: cleaning up per-worktree databases, deregistering ports,
or removing temporary credentials provisioned during setup.

Closes cyrusagents#1065
@ipalaus
Copy link
Copy Markdown

ipalaus commented Apr 16, 2026

I was looking exactly for this, we create 1 database per worktree so this is perfect to clean up after something is done! 🙏

@Connoropolous
Copy link
Copy Markdown
Contributor

This would be great.

@matthewbjones we can open a PR to resolve merge conflicts.

@Connoropolous
Copy link
Copy Markdown
Contributor

@cyrusagent can you create a new branch based on this one and resolve merge conflicts and open a new PR attributing this one

@cyrusagent
Copy link
Copy Markdown
Contributor

cyrusagent Bot commented Apr 16, 2026

Created #1111 — a rebased version of #1066 with merge conflicts resolved.

Conflicts resolved:

  • CHANGELOG.md — Added the teardown script entry under [Unreleased] above the existing v0.2.45 release notes
  • packages/edge-worker/src/GitService.ts — Updated deleteWorktree to use the new getDefaultWorktreesDir() helper (from main's refactor) while keeping the options parameter for the teardown script

All 569 edge-worker tests pass and the full build succeeds. The original author (@matthewbjones) is preserved on the commit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request - Add global_teardown_script to run during worktree cleanup

3 participants