{"record":{"id":"eee15bb3e3ea782d","repo":"siyuan-note/siyuan","slug":"invalid-image-operation-key","errorCode":null,"errorMessage":"invalid image operation key","messagePattern":"invalid image operation key","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/tools/image.go","lineNumber":281,"sourceCode":"\t\treturn CallToolResult{}, false\n\t}\n\tif record.State == imageOperationStateRunning {\n\t\treturn imageUnknown(\"a previous image operation was interrupted; its external result is unknown and it must not be retried automatically\"), true\n\t}\n\tif record.State != imageOperationStateCompleted || record.Result.IsError {\n\t\tremoveImageOperationRecord(key)\n\t\treturn CallToolResult{}, false\n\t}\n\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}","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/tools/image.go#L263-L299","documentation":"saveImageOperationRecord persists the state of a pending image AI operation keyed by an operation key. Before writing the record it re-validates the key with validImageOperationKey; a key that is empty or not in the expected format aborts the save with this error. This is an internal guard so malformed keys never reach the operation-record store.","triggerScenarios":"runImageOperation saving a record for an operation whose key was not generated by the expected key builder (empty string, wrong encoding, or hand-crafted key), including paths exercised by TestRunImageOperationBlocksUnknownPendingOperation.","commonSituations":"Concurrency or refactoring bugs where the key is regenerated with different logic between creation and save; a lookup returning an empty key for an unknown/expired operation; tests or plugin code passing fabricated keys.","solutions":["Ensure the operation key is always produced by the same generator used when the pending operation was created (do not rebuild or trim it in between)","Check for empty/nil key propagation from the pending-operation lookup and fail earlier with a clear message","Inspect validImageOperationKey to match the exact expected format before passing keys in"],"exampleFix":"// before\nsaveImageOperationRecord(op.id, meta, state, result) // op.id may be \"\"\n// after\nif !validImageOperationKey(op.id) {\n    return fmt.Errorf(\"unknown image operation %q\", op.id)\n}\nsaveImageOperationRecord(op.id, meta, state, result)","handlingStrategy":"type-guard","validationCode":"if (typeof opKey !== 'string' || opKey.length === 0) {\n  throw new Error('image operation key must be a non-empty string');\n}","typeGuard":"function isValidOperationKey(k) {\n  return typeof k === 'string' && /^[A-Za-z0-9_-]+$/.test(k);\n}","tryCatchPattern":"try {\n  await runImageOperation(key, args);\n} catch (e) {\n  if (e.message === 'invalid image operation key') {\n    // regenerate the key via the canonical generator and retry once\n  }\n}","preventionTips":["Always use the canonical key generator; never hand-build or trim keys","Propagate the original key object through the operation lifecycle","Add a unit test asserting keys survive create->save round-trips"],"tags":["validation","state-management","image","internal-guard"],"backgroundTag":"invalid-identifier-format","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"}