siyuan-note/siyuan · error

Invalid view state patch

Error message

Invalid view state patch

What it means

getPatchByteLength guard in the view-state service: JSON.stringify of the {values, removeKeys} batch did not return a string, meaning the pending patch contains values that cannot be serialized (circular structures, undefined, symbols, or BigInt). The patch size cannot even be computed.

Source

Thrown at app/src/util/viewState.ts:30

export interface IViewStateTransport {
    get(key: string): Promise<TViewStateData>,
    patch(key: string, values: TViewStateData, removeKeys: string[]): Promise<void>,
}

export interface IViewStateServiceOptions {
    flushDelay?: number,
    transport?: IViewStateTransport,
}

const DEFAULT_FLUSH_DELAY = 400;
const MAX_PATCH_ENTRIES = 1000;
const MAX_PATCH_BYTES = 256 * 1024;

const getPatchByteLength = (values: TViewStateData, removeKeys: string[]) => {
    const json = JSON.stringify({values, removeKeys});
    if (typeof json !== "string") {
        throw new Error("Invalid view state patch");
    }
    const goCompatibleJSON = json.replace(/[<>&\u2028\u2029]/g, character => {
        return `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`;
    });
    return new TextEncoder().encode(goCompatibleJSON).byteLength;
};

const isViewStateData = (value: unknown): value is TViewStateData => {
    return typeof value === "object" && value !== null && !Array.isArray(value);
};

const checkResponse = (response: IWebSocketData, action: string) => {
    if (!response || response.code !== 0) {
        throw new Error(response?.msg || `Failed to ${action} view state`);
    }
};

const defaultTransport: IViewStateTransport = {

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Store only JSON-serializable values (plain objects, strings, numbers, booleans, arrays, null)
  2. Strip functions, undefined, symbols, and BigInt before calling set/patch
  3. Circular-reference-safe clone the state before persisting
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at app/src/util/viewState.ts:30 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/3c1c9eac69eae5f9. Report an issue: GitHub.