{"record":{"id":"7a96bb6decf1a688","repo":"affaan-m/ECC","slug":"rollback-requires-state","errorCode":null,"errorMessage":"rollback requires --state","messagePattern":"rollback requires --state","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/codex/legacy-sync-state.js","lineNumber":48,"sourceCode":"    })}\\n`);\n    return;\n  }\n  if (command === 'record') {\n    const statePath = readFlag(argv, '--state');\n    const filePath = readFlag(argv, '--path');\n    if (!statePath || !filePath) throw new Error('record requires --state and --path');\n    recordLegacySyncPath({ statePath, filePath });\n    return;\n  }\n  if (command === 'finalize') {\n    const statePath = readFlag(argv, '--state');\n    if (!statePath) throw new Error('finalize requires --state');\n    finalizeLegacySyncState({ statePath });\n    return;\n  }\n  if (command === 'rollback') {\n    const statePath = readFlag(argv, '--state');\n    if (!statePath) throw new Error('rollback requires --state');\n    const result = rollbackLegacyCodexSync({ statePath });\n    process.stdout.write(`${JSON.stringify(result)}\\n`);\n    if (result.status !== 'rolled-back') process.exitCode = 1;\n    return;\n  }\n  throw new Error('Usage: legacy-sync-state.js <begin|record|finalize|rollback> [options]');\n}\n\nmodule.exports = { main, readFlag };\n\nif (require.main === module) {\n  try {\n    main();\n  } catch (error) {\n    process.stderr.write(`[ecc-sync] ERROR: ${error.message}\\n`);\n    process.exit(1);\n  }\n}","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/affaan-m/ECC/blob/06c5e118c4d3e6c3b7f9445f973a2194c82de193/scripts/codex/legacy-sync-state.js#L30-L66","documentation":"In scripts/codex/legacy-sync-state.js, the 'rollback' subcommand restores files recorded by a previous sync session. rollbackLegacyCodexSync needs the --state flag to locate the state file that lists what was changed, so readFlag returning undefined causes this throw. On success the script prints a JSON result; if the rollback could not fully revert, it sets process.exitCode = 1 instead of throwing.","triggerScenarios":"`node scripts/codex/legacy-sync-state.js rollback` with no flags; `rollback --state` where the value is missing because the flag is last; passing the flag in a form readFlag does not recognize (e.g. `--state=/path` if only space-separated values are supported).","commonSituations":"Undoing a Codex sync gone wrong in an unfamiliar shell where the state path variable was never exported; running rollback hours after begin and losing the state filename; CI scripts that assume rollback needs no arguments.","solutions":["Pass the same state file used with begin/record/finalize: `node scripts/codex/legacy-sync-state.js rollback --state <state-file>`","If the state file is gone, do not retry rollback; manually restore from your backup or re-run the sync and rollback again","Verify the state path exists (`ls <state-file>`) before invoking so a different file-not-found error is not confused with this one"],"exampleFix":"// before\nnode scripts/codex/legacy-sync-state.js rollback\n// after\nnode scripts/codex/legacy-sync-state.js rollback --state .codex-sync-state.json","handlingStrategy":"validation","validationCode":"const i = process.argv.indexOf('--state');\nconst statePath = i !== -1 ? process.argv[i + 1] : null;\nif (process.argv[2] === 'rollback' && !statePath) {\n  console.error('rollback needs the --state file created by begin');\n  process.exit(2);\n}","typeGuard":null,"tryCatchPattern":"try {\n  main();\n} catch (e) {\n  if (/rollback requires --state/.test(e.message)) console.error('Re-run with: rollback --state <begin-state-file>');\n  else throw e;\n}","preventionTips":["Keep begin/record/finalize/rollback invocations in one script that defines the state path once","Verify the state file exists before rollback so a missing-flag error is never mixed with a missing-file error"],"tags":["cli","argument-validation","codex","rollback"],"backgroundTag":"missing-required-cli-flag","analyzedSha":"06c5e118c4d3e6c3b7f9445f973a2194c82de193","analyzedAt":"2026-08-18T11:27:13.915Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}