Files
workinf_Blender_Wasm/web/protocol/web-engine-revisions.ts
mes123456 a42073c09e fix(web): compare the NLA stack base revision
setNLAStack declared an optional baseRevision that the Worker never
compared, so a stale tab replaced the whole NLA stack without a
conflict. It now compares a supplied baseRevision against the current
SceneIR and reports REVISION_CONFLICT when it is stale or not a safe
integer; an absent revision stays allowed under the declared optional
policy. This leaves no command in the contract's unchecked set.

The Chromium probe confirms a stale submission is rejected, an absent one
and a fresh one are accepted, and the track count survives.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-02 21:17:21 +08:00

335 lines
14 KiB
TypeScript

import type { WebEngineCommandType } from "./web-engine-capabilities";
/**
* The protocol-level revision contract for every engine edit command.
*
* The engine Worker currently checks revisions per command inside
* assertFutureCapability, with three different semantics and a few commands
* that carry a revision field without rejecting a stale value. This module
* makes the intended contract explicit, exhaustive and testable; the Worker
* adoption is a separate infrastructure batch, so an enforced policy here
* does not by itself change dispatch behaviour.
*/
export const WEB_ENGINE_REVISION_SCHEMA = 1 as const;
export type WebEngineRevisionPolicy = "REQUIRED" | "OPTIONAL" | "NONE";
export type WebEngineRevisionEnforcement =
/** assertFutureCapability or the NLA prepare helpers compare currentSnapshot. */
| "WORKER_PREFLIGHT"
/** A protocol validator (Grease Pencil reorder) rejects the stale command. */
| "PROTOCOL_VALIDATOR"
/** The Worker folds staleness into INVALID_ARGUMENT instead of REVISION_CONFLICT. */
| "WORKER_FOLDED"
/** The payload declares the field but the Worker performs no stale rejection yet. */
| "UNCHECKED"
| "NONE";
export type WebEngineRevisionField =
| "baseRevision"
| "profile.sourceMeshRevision"
| "timeline.revision"
| "attributes.revision";
export interface WebEngineRevisionContract {
readonly policy: WebEngineRevisionPolicy;
readonly field: WebEngineRevisionField | null;
readonly enforcement: WebEngineRevisionEnforcement;
readonly errorCode: "REVISION_CONFLICT" | "INVALID_ARGUMENT" | null;
readonly note?: string;
}
const NONE: WebEngineRevisionContract = { policy: "NONE", field: null, enforcement: "NONE", errorCode: null };
export const WEB_ENGINE_REVISION_CONTRACTS = {
setFrame: NONE,
setObjectVisibility: NONE,
setModifierEnabled: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setModifierVisibility: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
addModifier: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
removeModifier: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
moveModifier: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setModifierParameters: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT", note: "curveGesture begin additionally requires baseRevision." },
renameModifier: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
decimateMesh: { policy: "REQUIRED", field: "profile.sourceMeshRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
previewDecimateMesh: { policy: "REQUIRED", field: "profile.sourceMeshRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
createPrimitive: NONE,
duplicateObject: NONE,
deleteObject: NONE,
setObjectTransform: NONE,
translateMeshVertices: NONE,
transformMeshElements: NONE,
meshEdit: NONE,
meshSubdivide: NONE,
meshTriangulate: NONE,
meshRecalculateNormals: NONE,
meshBridgeLoops: NONE,
meshDeleteElements: NONE,
meshUVProject: NONE,
createUVMap: NONE,
setActiveUVMap: NONE,
unwrapUV: NONE,
addMaterialSlot: NONE,
assignMaterialSlot: NONE,
packImage: NONE,
makeImageLocal: NONE,
removeMaterialSlot: NONE,
assignMaterialFaces: NONE,
setMaterialPrincipled: NONE,
importImage: NONE,
setMaterialImageNode: NONE,
insertObjectKeyframe: NONE,
deleteObjectKeyframe: NONE,
setFCurveInterpolation: NONE,
setActiveAction: NONE,
createAction: NONE,
removeAction: NONE,
setKeyframe: NONE,
setTimelineMarkers: NONE,
setFCurveKeyframes: NONE,
setDriverExpression: NONE,
setActionGroups: NONE,
setKeyingSet: NONE,
bakeAnimation: NONE,
setArmatureBones: NONE,
setPoseBoneTransform: NONE,
armatureEditOperations: NONE,
setBoneRoll: NONE,
setBoneSymmetry: NONE,
setBoneParenting: NONE,
setPoseConstraint: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setConstraint: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
editConstraint: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
moveConstraint: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
renameConstraint: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setParent: NONE,
setParents: NONE,
createCollection: NONE,
moveObjectToCollection: NONE,
moveCollection: NONE,
deleteCollection: NONE,
setScene: NONE,
setActiveCollection: NONE,
renameId: NONE,
joinObjects: NONE,
separateMeshFaces: NONE,
applyObjectTransform: NONE,
setObjectOrigin: NONE,
clearObjectTransform: NONE,
createObjectData: NONE,
setObjectSelection: NONE,
setCollectionVisibility: NONE,
createCurve: NONE,
deleteNonMeshData: NONE,
setCurveControlPoints: NONE,
setCurveBevel: NONE,
setCurveTaper: NONE,
convertCurveType: NONE,
setCurveHandle: NONE,
setCurveTopology: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setCurveSplines: NONE,
setSurfaceTopology: NONE,
setSurfaceResolution: NONE,
setFontBody: NONE,
setFontProperties: NONE,
setFontAdvanced: NONE,
importVFont: NONE,
setFontLinks: NONE,
setTextSource: NONE,
createText: NONE,
editEditorContext: { policy: "REQUIRED", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
editShaderNode: { policy: "REQUIRED", field: "baseRevision", enforcement: "WORKER_FOLDED", errorCode: "INVALID_ARGUMENT" },
setSoundProperties: NONE,
setSpeakerProperties: NONE,
setParticleSettings: NONE,
setTextureProperties: NONE,
setVolumeProperties: NONE,
createGreasePencilLayer: NONE,
removeGreasePencilLayer: NONE,
moveGreasePencilLayer: { policy: "REQUIRED", field: "baseRevision", enforcement: "PROTOCOL_VALIDATOR", errorCode: "REVISION_CONFLICT" },
moveGreasePencilFrame: { policy: "REQUIRED", field: "baseRevision", enforcement: "PROTOCOL_VALIDATOR", errorCode: "REVISION_CONFLICT" },
insertGreasePencilFrame: NONE,
removeGreasePencilFrame: NONE,
setGreasePencilStrokes: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setGreasePencilDrawing: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setGreasePencilMaterial: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setGreasePencilSelection: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setGreasePencilInterpolate: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setVertexColors: NONE,
setVertexWeights: NONE,
setLightProperties: NONE,
setCameraProperties: NONE,
setWorldProperties: NONE,
setSceneProperties: NONE,
setSceneRenderSettings: NONE,
setSceneAudioSettings: NONE,
setSceneUnitSettings: NONE,
setSceneTimelineSettings: NONE,
setScenePipelineSettings: NONE,
setSceneBindings: NONE,
setSceneViewLayerSettings: NONE,
setSceneCollectionSettings: NONE,
setSceneBorderSettings: NONE,
setSceneSimplifySettings: NONE,
setSceneColorManagement: NONE,
setSceneImageFormat: NONE,
setSceneMotionBlur: NONE,
setSceneHairSettings: NONE,
setSceneCompositorSettings: NONE,
setScenePerformanceSettings: NONE,
setSceneSamplingSettings: NONE,
setSceneMultiviewSettings: NONE,
setSceneStampSettings: NONE,
setSceneLineSettings: NONE,
setSceneViewLayerPassSettings: NONE,
setSceneViewLayerAOVs: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setSceneViewLayerLightgroups: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
renderFrame: NONE,
renderAnimation: NONE,
setRenderPasses: NONE,
setCameraView: NONE,
setLightLinking: NONE,
setObjectLightgroup: NONE,
setWorldLightgroup: NONE,
setMetaballElements: NONE,
sculptStroke: NONE,
setSculptMeshAttributes: { policy: "REQUIRED", field: "attributes.revision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setGeometryNodeGraph: NONE,
setPhysicsSimulation: NONE,
setRigidBodySettings: NONE,
setClothSettings: NONE,
setFluidSettings: NONE,
setParticleCache: NONE,
setSimulationPlayback: NONE,
setShaderGraph: NONE,
setNLAStack: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setNLAProperty: { policy: "OPTIONAL", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setSequencerStrips: { policy: "REQUIRED", field: "timeline.revision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setSequencerStripModifiers: NONE,
setSequencerTransitions: { policy: "REQUIRED", field: "timeline.revision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
setSequencerColorBalance: NONE,
setCompositorNodeGraph: NONE,
setTrackingMarkers: NONE,
setTrackingSolve: NONE,
setMaskSpline: NONE,
setMaskPoint: NONE,
moveNLAStrip: { policy: "REQUIRED", field: "baseRevision", enforcement: "WORKER_PREFLIGHT", errorCode: "REVISION_CONFLICT" },
undo: NONE,
redo: NONE,
} satisfies Record<WebEngineCommandType, WebEngineRevisionContract>;
export type WebEngineRevisionVerdict =
| { readonly status: "OK"; readonly contract: WebEngineRevisionContract }
| {
readonly status: "REVISION_REQUIRED";
readonly field: WebEngineRevisionField;
readonly code: "REVISION_CONFLICT" | "INVALID_ARGUMENT";
readonly message: string;
}
| {
readonly status: "REVISION_INVALID";
readonly field: WebEngineRevisionField;
readonly code: "REVISION_CONFLICT" | "INVALID_ARGUMENT";
readonly message: string;
}
| {
readonly status: "REVISION_CONFLICT";
readonly field: WebEngineRevisionField;
readonly expected: number | null;
readonly actual: number;
readonly code: "REVISION_CONFLICT" | "INVALID_ARGUMENT";
readonly message: string;
}
| { readonly status: "UNKNOWN_COMMAND"; readonly type: string };
function readRevisionField(
command: Readonly<Record<string, unknown>>,
field: WebEngineRevisionField,
): unknown {
if (field === "baseRevision") return command.baseRevision;
const separator = field.indexOf(".");
const container = command[field.slice(0, separator)];
return container !== null && typeof container === "object"
? (container as Record<string, unknown>)[field.slice(separator + 1)]
: undefined;
}
/**
* Evaluate one edit command against a rendered SceneIR revision.
* `revision` is the current snapshot revision, or null when no Main is open.
*/
export function evaluateWebEngineRevision(
command: { readonly type: string } & Readonly<Record<string, unknown>>,
revision: number | null,
): WebEngineRevisionVerdict {
if (!Object.prototype.hasOwnProperty.call(WEB_ENGINE_REVISION_CONTRACTS, command.type)) {
return { status: "UNKNOWN_COMMAND", type: command.type };
}
const contract = WEB_ENGINE_REVISION_CONTRACTS[command.type as WebEngineCommandType];
if (contract.policy === "NONE" || contract.field === null) {
return { status: "OK", contract };
}
const field = contract.field;
const code = contract.errorCode ?? "REVISION_CONFLICT";
const value = readRevisionField(command as Readonly<Record<string, unknown>>, field);
if (value === undefined) {
return contract.policy === "OPTIONAL"
? { status: "OK", contract }
: { status: "REVISION_REQUIRED", field, code, message: `${command.type} requires ${field}` };
}
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
return {
status: "REVISION_INVALID",
field,
code,
message: `${command.type} ${field} is not a safe non-negative integer`,
};
}
if (revision === null || value !== revision) {
return {
status: "REVISION_CONFLICT",
field,
expected: revision,
actual: value,
code,
message: `${command.type} ${field} does not match the current revision`,
};
}
return { status: "OK", contract };
}
export class WebEngineRevisionError extends Error {
readonly code: string;
constructor(code: string, message: string) {
super(message);
this.name = "WebEngineRevisionError";
this.code = code;
}
}
export type WebEngineRevisionFail = (code: string, message: string) => never;
/**
* Assert the revision contract. Without a `fail` callback this throws a
* WebEngineRevisionError; callers that already have a reporter can pass one.
*/
export function assertWebEngineRevision(
command: { readonly type: string } & Readonly<Record<string, unknown>>,
revision: number | null,
fail?: WebEngineRevisionFail,
): void {
const verdict = evaluateWebEngineRevision(command, revision);
if (verdict.status === "OK") return;
const code = verdict.status === "UNKNOWN_COMMAND" ? "COMMAND_UNSUPPORTED" : verdict.code;
const message =
verdict.status === "UNKNOWN_COMMAND"
? `Unknown engine command: ${verdict.type}`
: verdict.message;
if (fail) {
fail(code, message);
return;
}
throw new WebEngineRevisionError(code, message);
}