opencode-mission-control icon indicating copy to clipboard operation
opencode-mission-control copied to clipboard

fix: setCheckpoint overwrites concurrent job state updates causing completed jobs to appear as running

Open nigel-dev opened this issue 9 months ago • 0 comments

Bug Description

When a plan has multiple jobs running and one fails, the other jobs that have already sent mc_report(status: "completed") remain stuck as running in the plan status. The plan shows 1 failed and N running, even though the running jobs have completed.

Root Cause

Race condition in Orchestrator.setCheckpoint() → savePlan() that overwrites concurrent updatePlanJob() updates.

When a job fails, handleJobFailed in orchestrator.ts:

  1. Fire-and-forgets updatePlanJob(planId, failedJob, { status: 'failed' })
  2. Calls loadPlan() outside the mutex — gets a stale snapshot (other jobs still show running)
  3. Passes that stale snapshot to setCheckpoint(), which mutates plan.status = 'paused' and calls savePlan(plan)
  4. savePlan() writes the entire stale plan object to disk — overwriting any updatePlanJob() changes that handleJobComplete made for the other jobs

Race sequence

1. Monitor poll detects: Job A = failed, Job B = completed (report), Job C = completed (report)
2. emit('failed', A)  → handleJobFailed starts async chain
3. emit('complete', B) → handleJobComplete calls updatePlanJob(B, 'completed')
4. emit('complete', C) → handleJobComplete calls updatePlanJob(C, 'completed')

Possible ordering:
  a. handleJobFailed: loadPlan() → snapshot has A=running, B=running, C=running
  b. updatePlanJob(A, 'failed')  → plan file: A=failed, B=running, C=running
  c. updatePlanJob(B, 'completed') → plan file: A=failed, B=completed, C=running
  d. updatePlanJob(C, 'completed') → plan file: A=failed, B=completed, C=completed
  e. setCheckpoint → savePlan(staleSnapshot) → plan file: A=failed, B=running, C=running ← OVERWRITES

Result: plan shows 1 failed, 2 running.

The planMutex in savePlan doesn't help because it reads the existing plan only for transition validation — it still writes the passed-in stale object. And since the monitor marks jobs as completed in jobs.json (removing them from getRunningJobs()), they are never re-checked on subsequent polls. There is no recovery path.

Affected Code

  • src/lib/orchestrator.ts — setCheckpoint(), clearCheckpoint(), _doReconcile() savePlan calls
  • src/lib/plan-state.ts — savePlan() does blind overwrite of entire plan object

Proposed Fix

  1. Add updatePlanFields() to plan-state.ts — atomic read-modify-write for plan-level fields only (status, checkpoint, completedAt, prUrl) inside the mutex, preserving job states
  2. Replace savePlan(staleSnapshot) in setCheckpoint with updatePlanFields(planId, { status: 'paused', checkpoint: type })
  3. Apply same fix to clearCheckpoint and _doReconcile savePlan calls
  4. Add reconciliation safety net in _doReconcile — cross-reference jobs.json for plan jobs stuck as running when they've already completed

nigel-dev avatar Feb 14 '26 09:02 nigel-dev