{"record":{"id":"dd6b0fbc69e1e909","repo":"coleam00/Archon","slug":"detached-work-stopped-but-cancellation-did-not-wi","errorCode":null,"errorMessage":"Detached work stopped, but cancellation did not win the run state transition. The run status is ${latest?.status ?? 'unknown'}; it was not reported as cancelled.","messagePattern":"Detached work stopped, but cancellation did not win the run state transition\\. The run status is (.+?); it was not reported as cancelled\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/workflow.ts","lineNumber":4437,"sourceCode":"    const target = await requestDetachedRunStop(resolvedId);\n    await target.stop();\n\n    if (containerEnvId) {\n      try {\n        await reclaimContainerEnv(containerEnvId);\n      } catch (error) {\n        throw new Error(\n          'Detached owner process stopped, but the isolation container could not be confirmed stopped. ' +\n            `Run state was not changed. ${(error as Error).message}`\n        );\n      }\n    }\n\n    const { run, cancelled, cascadeFailures, blockedParentRunId } =\n      await abandonWorkflow(resolvedId);\n    if (!cancelled) {\n      const latest = await workflowDb.getWorkflowRun(resolvedId);\n      throw new Error(\n        'Detached work stopped, but cancellation did not win the run state transition. ' +\n          `The run status is ${latest?.status ?? 'unknown'}; it was not reported as cancelled.`\n      );\n    }\n    return {\n      resolvedId,\n      workflowName: run.workflow_name,\n      cascadeFailures,\n      blockedParentRunId,\n    };\n  };\n\n  if (json) {\n    try {\n      const result = await cancel();\n      await writeJsonLine({\n        ok: true,\n        runId: result.resolvedId,","sourceCodeStart":4419,"sourceCodeEnd":4455,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/cli/src/commands/workflow.ts#L4419-L4455","documentation":"After stopping the detached owner process, the CLI calls `abandonWorkflow(resolvedId)` to win the run-state transition to `cancelled`. If it reports `cancelled: false`, another actor changed the run state concurrently; the CLI re-reads the run and reports the actual status instead of falsely claiming a successful cancel.","triggerScenarios":"Calling `archon workflow cancel` while another process (another cancel, an engine transition, a completion) concurrently moves the run out of `running`, so the abandon transition loses the race.","commonSituations":"Two operators cancelling the same run simultaneously; the run finishing naturally between process-stop and state transition; an engine watchdog or resume operation touching the run concurrently.","solutions":["Re-check the run status (`archon workflow status <id>`); if it is completed/failed, the outcome is fine and no cancel was needed","If still running, retry the cancel once the concurrent operation settles","Ensure only one operator/process issues control commands for a given run"],"exampleFix":"// before\nawait cancelWorkflow(runId); // may throw on lost race\n// after\ntry { await cancelWorkflow(runId); }\ncatch (e) {\n  const run = await getWorkflowRun(runId);\n  if (run.status === 'completed') console.log('run already finished; cancel not needed');\n  else throw e;\n}","handlingStrategy":"retry","validationCode":"const run = await workflowDb.getWorkflowRun(id);\nif (run.status !== 'running') { console.log(`run is ${run.status}; nothing to cancel`); return; }","typeGuard":null,"tryCatchPattern":"try {\n  await cancelWorkflow(id);\n} catch (e) {\n  if (String(e).includes('did not win the run state transition')) {\n    const latest = await workflowDb.getWorkflowRun(id);\n    if (latest?.status === 'running') await cancelWorkflow(id); // retry once\n  } else throw e;\n}","preventionTips":["Serialize control commands: one operator/process per run","Check run status right before cancelling","Expect races when the run is near completion; treat non-running end states as success"],"tags":["cli","workflow","cancel","race-condition","state-transition"],"backgroundTag":"concurrent-state-transition","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}