siyuan-note/siyuan · error

invalid image operation key

Error message

invalid image operation key

What it means

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.

Solutions

  1. 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)
  2. Check for empty/nil key propagation from the pending-operation lookup and fail earlier with a clear message
  3. Inspect validImageOperationKey to match the exact expected format before passing keys in

Example fix

// before
saveImageOperationRecord(op.id, meta, state, result) // op.id may be ""
// after
if !validImageOperationKey(op.id) {
    return fmt.Errorf("unknown image operation %q", op.id)
}
saveImageOperationRecord(op.id, meta, state, result)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof opKey !== 'string' || opKey.length === 0) {
  throw new Error('image operation key must be a non-empty string');
}

Type guard

function isValidOperationKey(k) {
  return typeof k === 'string' && /^[A-Za-z0-9_-]+$/.test(k);
}

Try / catch

try {
  await runImageOperation(key, args);
} catch (e) {
  if (e.message === 'invalid image operation key') {
    // regenerate the key via the canonical generator and retry once
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/eee15bb3e3ea782d. Report an issue: GitHub.

Appendix: source

Thrown at kernel/mcp/tools/image.go:281

		return CallToolResult{}, false
	}
	if record.State == imageOperationStateRunning {
		return imageUnknown("a previous image operation was interrupted; its external result is unknown and it must not be retried automatically"), true
	}
	if record.State != imageOperationStateCompleted || record.Result.IsError {
		removeImageOperationRecord(key)
		return CallToolResult{}, false
	}
	if record.AssetPath != "" && !imageOperationAssetExists(record.DocumentID, record.AssetPath) {
		removeImageOperationRecord(key)
		return CallToolResult{}, false
	}
	return record.Result, true
}

func saveImageOperationRecord(key string, meta imageOperationMeta, state string, result CallToolResult) error {
	if !validImageOperationKey(key) {
		return errors.New("invalid image operation key")
	}
	assetPath := meta.AssetPath
	if state == imageOperationStateCompleted {
		if resultPath := imageResultAssetPath(result); resultPath != "" {
			assetPath = resultPath
		}
	}
	if state != imageOperationStateRunning && state != imageOperationStateCompleted {
		return errors.New("invalid image operation state")
	}
	record := imageOperationRecord{
		Version: 1, CreatedAt: time.Now().UnixMilli(), State: state, Action: meta.Action, DocumentID: meta.DocumentID,
		AssetPath: assetPath, Result: result,
	}
	data, err := json.Marshal(record)
	if err != nil {
		return err
	}

View on GitHub (pinned to 9f775e8a12)