A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(mcp): a scene batch that fails leaves the scene alone
yhhf5e6842f2a51
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.tsindex 4ac78bf0b..2da334fa3 100644--- a/desktop/src/engine/EditorControlSurface.ts+++ b/desktop/src/engine/EditorControlSurface.ts@@ -335,6 +335,11 @@ export class EditorControlSurfaceImpl {this.s.commands.transact(label, fn);}+ /** Run `fn` as one undo step, rolling structural edits back too if it throws. */+ atomic(label: string, fn: () => void): void {+ this.s.commands.atomic(label, fn);+ }+// =========================================================================// Hierarchy & organization — the World Outliner's model operations, exposed// so the headless host + editor MCP drive the same undoable commands the UIdiff --git a/desktop/src/engine/EditorHistory.ts b/desktop/src/engine/EditorHistory.tsindex f5ac19f5c..b4da68456 100644--- a/desktop/src/engine/EditorHistory.ts+++ b/desktop/src/engine/EditorHistory.ts@@ -111,6 +111,39 @@ export class EditorHistoryImpl {}}+ /**+ * `group`, but a throw puts back what `fn` had already done and records+ * nothing.+ *+ * The difference is who is watching. A UI gesture that fails mid-way leaves+ * the user looking at the result with undo one keystroke away, so `group`+ * keeps the partial work. A program applied over a socket has no such reader:+ * its caller is told the batch failed and reasonably believes the scene is+ * untouched, so a half-built subtree is a lie that outlives the error.+ */+ atomic<T>(label: string, fn: () => T): T {+ if (this.groupOps) return fn(); // nested: the outer owner decides the step+ const ops: HistoryOp[] = [];+ this.groupOps = ops;+ try {+ const out = fn();+ this.groupOps = null;+ this.batch(label, ops);+ return out;+ } catch (e) {+ this.groupOps = null;+ // LIFO, exactly as undoing this entry would, then drop it entirely.+ for (let i = ops.length - 1; i >= 0; i--) {+ try {+ ops[i].reverse();+ } catch (err) {+ console.warn(`[history] rollback op ${i} of "${label}" threw`, err);+ }+ }+ throw e;+ }+ }+// Ops run per-direction under try/catch — one broken closure must not wedge// the rest of the entry (matches the engine TransactionManager this replaced).private static apply(entry: HistoryEntry, dir: 'undo' | 'redo'): void {diff --git a/desktop/src/engine/SceneCommands.ts b/desktop/src/engine/SceneCommands.tsindex 9b8e79c7e..4ed034bd8 100644--- a/desktop/src/engine/SceneCommands.ts+++ b/desktop/src/engine/SceneCommands.ts@@ -333,6 +333,28 @@ export class SceneCommandsImpl {}}+ /**+ * Run `fn` as ONE undo step that rolls all the way back if it throws —+ * structural edits included, which is what separates this from+ * {@link transact}. A gesture only remembers the FIELDS it touched, so a+ * batch that spawns entities and then fails leaves them behind: the fields+ * snap back and the entities stay. Here the structural commands reverse+ * themselves through the history group, and the field gesture aborts as+ * before, so the scene is what it was before the batch started.+ */+ atomic(label: string, fn: () => void): void {+ this.history.atomic(label, () => {+ this.beginGesture(label);+ try {+ fn();+ this.endGesture(); // the coalesced field edits join this step+ } catch (e) {+ this.abortGesture(); // touched fields restored live, nothing recorded+ throw e;+ }+ });+ }+/*** Write a single inspector field to the model (the Reconciler re-projects it* to the World). Always undoable: coalesced into an open gesture, else its own step.diff --git a/desktop/src/engine/sceneOps.ts b/desktop/src/engine/sceneOps.tsindex a9f004291..1e6ea2282 100644--- a/desktop/src/engine/sceneOps.ts+++ b/desktop/src/engine/sceneOps.ts@@ -118,8 +118,9 @@ export async function applySceneOps(}};- // Phase 2 (sync): one transaction — a throw rolls the whole batch back.- EditorControlSurface.transact(label, () => {+ // Phase 2 (sync): one undo step — a throw rolls the whole batch back, the+ // entities it had already spawned included.+ EditorControlSurface.atomic(label, () => {ops.forEach((op, i) => {const at = `op[${i}] ${op.op}`;try {diff --git a/desktop/tests/editor-history-group.test.ts b/desktop/tests/editor-history-group.test.tsindex b0b925cd1..a3959839b 100644--- a/desktop/tests/editor-history-group.test.ts+++ b/desktop/tests/editor-history-group.test.ts@@ -88,4 +88,62 @@ describe('EditorHistory group', () => {h.undo();expect(value).toBe(0);});++ // `atomic` is what a program applied over MCP runs in: nobody is watching the+ // editor to notice a half-built subtree and press undo, so a failed batch has+ // to leave the scene as it found it.+ describe('atomic', () => {+ it('collapses into one step, exactly like group, when nothing throws', () => {+ h.atomic('Batch', () => { add(1); add(2); });+ expect(value).toBe(3);+ expect(h.undoLabel()).toBe('Batch');+ h.undo();+ expect(value).toBe(0);+ });++ it('reverses what it already did when the callback throws', () => {+ expect(() =>+ h.atomic('Boom', () => {+ add(1);+ add(2);+ throw new Error('boom');+ }),+ ).toThrow('boom');+ expect(value).toBe(0); // both reversed, not just the last+ expect(h.canUndo()).toBe(false); // and nothing was recorded to undo+ });++ it('reverses in LIFO order, so an op can depend on what came before it', () => {+ const order: string[] = [];+ expect(() =>+ h.atomic('Boom', () => {+ h.record('a', () => {}, () => order.push('a'));+ h.record('b', () => {}, () => order.push('b'));+ throw new Error('boom');+ }),+ ).toThrow('boom');+ expect(order).toEqual(['b', 'a']);+ });++ it('a rollback op that throws does not strand the rest of the rollback', () => {+ expect(() =>+ h.atomic('Boom', () => {+ add(1);+ h.record('bad', () => {}, () => { throw new Error('reverse failed'); });+ throw new Error('boom');+ }),+ ).toThrow('boom');+ expect(value).toBe(0); // the good reversal still ran+ });++ it('nested in a group, the outer step owns the work and keeps it', () => {+ expect(() =>+ h.group('Outer', () => {+ h.atomic('Inner', () => { add(1); throw new Error('boom'); });+ }),+ ).toThrow('boom');+ expect(value).toBe(1); // the outer gesture decides, as with a nested group+ expect(h.undoLabel()).toBe('Outer');+ });+ });});diff --git a/desktop/tests/scene-ops.test.ts b/desktop/tests/scene-ops.test.tsindex da5e6ad0f..77e38790e 100644--- a/desktop/tests/scene-ops.test.ts+++ b/desktop/tests/scene-ops.test.ts@@ -16,7 +16,12 @@ const state = vi.hoisted(() => ({ nextId: 100, createReturnsNull: false, throwOnvi.mock('@/engine/EditorSession', () => ({EditorControlSurface: {- transact: (label: string, fn: () => void) => {+ // The real one rolls structural edits back too (SceneCommands.atomic); this+ // fake only records the boundary, which is all the interpreter can be asked+ // about. That the rollback HAPPENS is pinned in editor-history-group.test.ts+ // against the real history — a fake that simply rethrows once made this+ // suite report a rollback contract nothing was keeping.+ atomic: (label: string, fn: () => void) => {calls.push(['transact:begin', label]);try {fn();