{"record":{"id":"dc02b0ce7bf9f99e","repo":"siyuan-note/siyuan","slug":"invalid-image-operation-state","errorCode":null,"errorMessage":"invalid image operation state","messagePattern":"invalid image operation state","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/tools/image.go","lineNumber":290,"sourceCode":"\tif record.AssetPath != \"\" && !imageOperationAssetExists(record.DocumentID, record.AssetPath) {\n\t\tremoveImageOperationRecord(key)\n\t\treturn CallToolResult{}, false\n\t}\n\treturn record.Result, true\n}\n\nfunc saveImageOperationRecord(key string, meta imageOperationMeta, state string, result CallToolResult) error {\n\tif !validImageOperationKey(key) {\n\t\treturn errors.New(\"invalid image operation key\")\n\t}\n\tassetPath := meta.AssetPath\n\tif state == imageOperationStateCompleted {\n\t\tif resultPath := imageResultAssetPath(result); resultPath != \"\" {\n\t\t\tassetPath = resultPath\n\t\t}\n\t}\n\tif state != imageOperationStateRunning && state != imageOperationStateCompleted {\n\t\treturn errors.New(\"invalid image operation state\")\n\t}\n\trecord := imageOperationRecord{\n\t\tVersion: 1, CreatedAt: time.Now().UnixMilli(), State: state, Action: meta.Action, DocumentID: meta.DocumentID,\n\t\tAssetPath: assetPath, Result: result,\n\t}\n\tdata, err := json.Marshal(record)\n\tif err != nil {\n\t\treturn err\n\t}\n\tpath := imageOperationRecordPath(key)\n\tif err = os.MkdirAll(filepath.Dir(path), 0755); err != nil {\n\t\treturn err\n\t}\n\treturn filelock.WriteFile(path, data)\n}\n\nfunc removeImageOperationRecord(key string) {\n\tif !validImageOperationKey(key) {","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/tools/image.go#L272-L308","documentation":"Image operation records accept exactly two states: the running state and imageOperationStateCompleted. saveImageOperationRecord checks the state argument after key validation and rejects anything else with this error, keeping the persisted operation journal a valid state machine.","triggerScenarios":"Calling saveImageOperationRecord (directly or via runImageOperation) with a state string other than the two allowed constants — e.g. a typo, a new state added at one call site but not the constant list, or a stale/failed state string passed by mistake.","commonSituations":"Refactoring that renames state constants but leaves one caller with a literal string; adding a 'failed' state without extending the validation; test code inventing states.","solutions":["Pass only the defined constants (imageOperationStateRunning / imageOperationStateCompleted), never string literals","If a new lifecycle state is needed, add it to the validation list and the record schema (bump Version) deliberately","Grep call sites for the literal state strings and replace them with the constants"],"exampleFix":"// before\nsaveImageOperationRecord(key, meta, \"done\", result)\n// after\nsaveImageOperationRecord(key, meta, imageOperationStateCompleted, result)","handlingStrategy":"type-guard","validationCode":"const ALLOWED = ['running', 'completed'];\nif (!ALLOWED.includes(state)) {\n  throw new Error(`state must be one of ${ALLOWED.join(', ')}`);\n}","typeGuard":"function isImageOperationState(s) {\n  return s === imageOperationStateRunning || s === imageOperationStateCompleted;\n}","tryCatchPattern":"try {\n  saveImageOperationRecord(key, meta, state, result);\n} catch (e) {\n  if (e.message === 'invalid image operation state') {\n    // log and persist with the closest allowed state or drop the record\n  }\n}","preventionTips":["Reference exported state constants, never string literals","When adding lifecycle states, update validation and the record schema together","Type the state parameter with a union/enum so invalid values fail at compile time"],"tags":["validation","state-management","image","state-machine"],"backgroundTag":"invalid-state-transition","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}