{"record":{"id":"f005fa0b9f7f6f86","repo":"eyaltoledano/claude-task-master","slug":"invalid-workflow-state-state-may-be-corrupted-co","errorCode":null,"errorMessage":"Invalid workflow state. State may be corrupted. Consider starting a new workflow.","messagePattern":"Invalid workflow state\\. State may be corrupted\\. Consider starting a new workflow\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/tm-core/src/modules/workflow/services/workflow.service.ts","lineNumber":269,"sourceCode":"\t\t// Set main task status to in-progress\n\t\tawait this.updateTaskStatus(taskId, 'in-progress', tag);\n\n\t\treturn this.getStatus();\n\t}\n\n\t/**\n\t * Resume an existing workflow\n\t */\n\tasync resumeWorkflow(): Promise<WorkflowStatus> {\n\t\t// Load state\n\t\tconst state = await this.stateManager.load();\n\n\t\t// Create new orchestrator with loaded context\n\t\tthis.orchestrator = new WorkflowOrchestrator(state.context);\n\n\t\t// Validate and restore state\n\t\tif (!this.orchestrator.canResumeFromState(state)) {\n\t\t\tthrow new Error(\n\t\t\t\t'Invalid workflow state. State may be corrupted. Consider starting a new workflow.'\n\t\t\t);\n\t\t}\n\n\t\tthis.orchestrator.restoreState(state);\n\n\t\t// Re-enable auto-persistence\n\t\tthis.orchestrator.enableAutoPersist(async (newState: WorkflowState) => {\n\t\t\tawait this.stateManager.save(newState);\n\t\t});\n\n\t\t// Initialize activity logger to continue tracking events\n\t\tthis.activityLogger = new WorkflowActivityLogger(\n\t\t\tthis.orchestrator,\n\t\t\tthis.stateManager.getActivityLogPath()\n\t\t);\n\t\tthis.activityLogger.start();\n","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/eyaltoledano/claude-task-master/blob/c0c98d367c55296bfe69e65680625b6db437af02/packages/tm-core/src/modules/workflow/services/workflow.service.ts#L251-L287","documentation":"resumeWorkflow() loads persisted state and asks a fresh orchestrator whether the state can be resumed via canResumeFromState(state). When that validation fails — the phase is unknown/invalid for the context, the context is missing required fields, or the shape doesn't match expectations — the service throws, advising that the state file may be corrupted and a new workflow should be started.","triggerScenarios":"Calling workflowService.resumeWorkflow() (or the `resume` command) where orchestrator.canResumeFromState(state) returns false for the state loaded by stateManager.load(): e.g. state.phase is not a valid WorkflowPhase, state.context.subtasks is missing/empty, currentSubtaskIndex is out of bounds, or the JSON was truncated/manually edited.","commonSituations":"State file truncated by a crash mid-write or full disk; manual editing or a script rewriting the state JSON; resuming a state saved by an older package version whose phase names/context schema changed after an upgrade; the file being committed/shared across machines and mangled (line endings, merge conflicts); deleting or renaming the branch the state references while phase validation requires it.","solutions":["Inspect the workflow state file (stateManager path) and fix obvious corruption — invalid phase value, missing context fields, out-of-range currentSubtaskIndex — or restore it from backup.","If the state is unrecoverable, delete the state file and start a new workflow with startWorkflow({ ..., force: true }).","After upgrading the package, check the changelog for workflow state schema changes; migrate the old state or start fresh.","Reproduce by calling stateManager.load() yourself and logging `state.phase` and `state.context` to see exactly which field fails validation in canResumeFromState()."],"exampleFix":"// before\nawait workflowService.resumeWorkflow(); // throws: state.context.subtasks missing\n// after\nconst state = JSON.parse(fs.readFileSync(statePath, 'utf8'));\nif (!state.context?.subtasks?.length) {\n  fs.rmSync(statePath); // remove corrupted state\n  await workflowService.startWorkflow({ taskId, taskTitle, subtasks, force: true });\n} else {\n  await workflowService.resumeWorkflow();\n}","handlingStrategy":"try-catch","validationCode":"const state = JSON.parse(fs.readFileSync(stateFilePath, 'utf8'));\nconst validPhases = ['INIT', 'PREFLIGHT', 'BRANCH_SETUP', 'SUBTASK_LOOP', 'FINALIZE', 'COMPLETE', 'ERROR'];\nconst valid =\n  validPhases.includes(state?.phase) &&\n  Array.isArray(state?.context?.subtasks) && state.context.subtasks.length > 0 &&\n  typeof state?.context?.taskId === 'string' &&\n  Number.isInteger(state?.context?.currentSubtaskIndex) &&\n  state.context.currentSubtaskIndex >= 0 && state.context.currentSubtaskIndex <= state.context.subtasks.length;\nif (!valid) {\n  fs.rmSync(stateFilePath); // remove corrupted state, then start fresh\n}","typeGuard":"function isValidWorkflowState(state: unknown): state is { phase: string; context: { taskId: string; subtasks: unknown[]; currentSubtaskIndex: number } } {\n  const s = state as any;\n  return (\n    !!s && typeof s.phase === 'string' &&\n    typeof s.context?.taskId === 'string' &&\n    Array.isArray(s.context?.subtasks) && s.context.subtasks.length > 0 &&\n    typeof s.context?.currentSubtaskIndex === 'number' &&\n    Number.isInteger(s.context.currentSubtaskIndex) &&\n    s.context.currentSubtaskIndex >= 0\n  );\n}","tryCatchPattern":"try {\n  await workflowService.resumeWorkflow();\n} catch (err) {\n  if (err instanceof Error && err.message.includes('Invalid workflow state')) {\n    // state is unrecoverable; start fresh\n    await workflowService.startWorkflow({ ...options, force: true });\n  } else {\n    throw err;\n  }\n}","preventionTips":["Back up the workflow state file before upgrading the package or hand-modifying it.","Don't commit the state directory to git; merge conflicts corrupt the JSON.","After any crash, validate the state JSON parses and has the expected shape before resuming.","Check release notes for workflow state schema changes between versions; migrate or restart."],"tags":["workflow","corrupted-state","resume","validation"],"backgroundTag":"corrupted-state-file","analyzedSha":"c0c98d367c55296bfe69e65680625b6db437af02","analyzedAt":"2026-08-29T02:56:26.071Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}