fix(tasks): teardown archived and cold workspaces#2887
Conversation
Greptile SummaryThis PR extends persisted resource teardown to archived and cold workspaces. The main changes are:
Confidence Score: 4/5Concurrent provisioning and teardown can invalidate the workspace being set up.
apps/emdash-desktop/src/main/core/tasks/operations/beginTaskProvisioning.ts, apps/emdash-desktop/src/main/core/tasks/task-service.ts, and apps/emdash-desktop/src/main/core/tasks/operations/teardownTaskResources.ts
|
| Filename | Overview |
|---|---|
| apps/emdash-desktop/src/main/core/tasks/operations/teardownTaskResources.ts | Adds phase-aware live and cold teardown with in-flight teardown coalescing. |
| apps/emdash-desktop/src/main/core/tasks/operations/beginTaskProvisioning.ts | Clears persisted and in-memory teardown state without excluding concurrent teardown. |
| apps/emdash-desktop/src/main/core/workspaces/teardown-stored-workspace.ts | Reconstructs cold workspaces in teardown-only mode and fails closed for unavailable transports. |
| apps/emdash-desktop/src/main/core/workspaces/workspace-registry.ts | Adds distinct archive and provider-destroy hooks with guaranteed resource release. |
| apps/emdash-desktop/src/main/db/schema.ts | Adds nullable lifecycle and provider teardown timestamps to task rows. |
Sequence Diagram
%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant P as Provision
participant DB as Task row
participant W as Workspace setup
participant D as Delete or archive
participant T as Teardown
P->>DB: Clear completion markers
P->>W: Acquire and configure workspace
D->>DB: Read cleared markers
D->>T: Start cold teardown
T->>W: Destroy workspace resources
W-->>P: Setup fails or persists invalid state
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant P as Provision
participant DB as Task row
participant W as Workspace setup
participant D as Delete or archive
participant T as Teardown
P->>DB: Clear completion markers
P->>W: Acquire and configure workspace
D->>DB: Read cleared markers
D->>T: Start cold teardown
T->>W: Destroy workspace resources
W-->>P: Setup fails or persists invalid state
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
apps/emdash-desktop/src/main/core/tasks/operations/beginTaskProvisioning.ts:11-15
**Provision Races With Teardown**
When delete or archive starts after these markers are cleared but before workspace setup finishes, teardown treats the task as cold and can destroy the workspace being provisioned. Setup then continues against released runtime resources or a removed worktree, leaving the task partially provisioned or failing with provider and filesystem errors.
Reviews (1): Last reviewed commit: "fix(tasks): teardown archived and cold w..." | Re-trigger Greptile
| await db | ||
| .update(tasks) | ||
| .set({ lifecycleTeardownAt: null, providerDestroyAt: null }) | ||
| .where(eq(tasks.id, taskId)); | ||
| clearTaskResourceTeardown(taskId); |
There was a problem hiding this comment.
When delete or archive starts after these markers are cleared but before workspace setup finishes, teardown treats the task as cold and can destroy the workspace being provisioned. Setup then continues against released runtime resources or a removed worktree, leaving the task partially provisioned or failing with provider and filesystem errors.
Context Used: AGENTS.md (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/emdash-desktop/src/main/core/tasks/operations/beginTaskProvisioning.ts
Line: 11-15
Comment:
**Provision Races With Teardown**
When delete or archive starts after these markers are cleared but before workspace setup finishes, teardown treats the task as cold and can destroy the workspace being provisioned. Setup then continues against released runtime resources or a removed worktree, leaving the task partially provisioned or failing with provider and filesystem errors.
**Context Used:** AGENTS.md ([source](https://app.greptile.com/emdash/github/generalaction/emdash/-/custom-context?memory=c9624c9f-4bb0-42b3-aa19-4ea456b59255))
How can I resolve this? If you propose a fix, please make it concise.|
Addressed the provisioning/teardown race in
Validation:
The original thread remains anchored to |
Description
Archive and cold-delete paths now run the same persisted resource teardown as live task deletion. The change separates lifecycle teardown from provider destruction, records each completed phase in the task row, and resumes only unfinished phases after restart.
This also makes provisioning clear stale teardown markers before acquiring workspace resources, keeps BYOI and missing-transport failures fail-closed, and guarantees registered workspace resources are released even when a teardown hook throws.
The schema change is additive and includes migration metadata, pre-migration/baseline/empty fixtures, migration coverage, and updated worktree lifecycle documentation.
Related issues
Fixes #2886.
Testing
pnpm --filter @emdash/emdash-desktop test(Node: 288 files / 2026 tests; main-db: 26 / 161; migrations: 8 / 21; browser: 3 / 37 withCI=1 --maxWorkers=1; scripts: 1 / 14)pnpm --filter @emdash/emdash-desktop format:checkpnpm --filter @emdash/emdash-desktop lintpnpm --filter @emdash/emdash-desktop typecheckgit diff --checkThe repository-wide test target also reaches two pre-existing runtime session-manager assertion failures; both reproduce on upstream
mainatf6249ae56. The complete desktop test matrix above passes.Screenshot/Recording (if applicable)
Not applicable; this changes background task lifecycle handling and persistence.
Checklist
messages and, when possible, the PR title