siyuan-note/siyuan · error

invalid image operation state

Error message

invalid image operation state

What it means

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.

Solutions

  1. Pass only the defined constants (imageOperationStateRunning / imageOperationStateCompleted), never string literals
  2. If a new lifecycle state is needed, add it to the validation list and the record schema (bump Version) deliberately
  3. Grep call sites for the literal state strings and replace them with the constants

Example fix

// before
saveImageOperationRecord(key, meta, "done", result)
// after
saveImageOperationRecord(key, meta, imageOperationStateCompleted, result)
Defensive patterns

Strategy: type-guard

Validate before calling

const ALLOWED = ['running', 'completed'];
if (!ALLOWED.includes(state)) {
  throw new Error(`state must be one of ${ALLOWED.join(', ')}`);
}

Type guard

function isImageOperationState(s) {
  return s === imageOperationStateRunning || s === imageOperationStateCompleted;
}

Try / catch

try {
  saveImageOperationRecord(key, meta, state, result);
} catch (e) {
  if (e.message === 'invalid image operation state') {
    // log and persist with the closest allowed state or drop the record
  }
}

Prevention

When it happens

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

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

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

	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
	}
	path := imageOperationRecordPath(key)
	if err = os.MkdirAll(filepath.Dir(path), 0755); err != nil {
		return err
	}
	return filelock.WriteFile(path, data)
}

func removeImageOperationRecord(key string) {
	if !validImageOperationKey(key) {

View on GitHub (pinned to 9f775e8a12)