Skip to content

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.

fix(mcp): opening a scene stops throwing away the scene you just built

yhhd087bfa65ac8
diff --git a/desktop/scripts/editor-mcp-tools.mjs b/desktop/scripts/editor-mcp-tools.mjs
index 2721f5fd7..585d2361e 100644
--- a/desktop/scripts/editor-mcp-tools.mjs
+++ b/desktop/scripts/editor-mcp-tools.mjs
@@ -176,9 +176,10 @@ export const TOOLS = [
js: (i) => `window.__estellaEditor.open(${JSON.stringify(i.root)})
.then((ok) => { window.__estellaEditor.enterEditor(); return window.__estellaEditor.sceneReady().then(() => ok); })` },
{ name: 'open_scene',
- description: 'Open a scene by project-relative path (e.g. assets/scenes/main.esscene) in the editor. Resolves once the scene is adopted — get_scene_tree is immediately valid.',
- schema: obj({ path: { type: 'string' } }, ['path']),
- method: 'openScene', args: (i) => [i.path], root: 'editor' },
+ description: 'Open a scene by project-relative path (e.g. assets/scenes/main.esscene) in the editor. Resolves once the scene is adopted — get_scene_tree is immediately valid. '
+ + 'REFUSES while the open scene has unsaved changes (opening reloads from disk, so anything just authored would be gone): save_scene first, or pass discardChanges to throw the edits away on purpose.',
+ schema: obj({ path: { type: 'string' }, discardChanges: { type: 'boolean' } }, ['path']),
+ method: 'openScene', args: (i) => [i.path, i.discardChanges === true], root: 'editor' },
{ name: 'save_scene', write: true,
description: 'Save the open scene to disk (the toolbar Save).',
schema: obj({}), method: 'save', args: () => [], root: 'editor' },
diff --git a/desktop/src/main.tsx b/desktop/src/main.tsx
index d9831b305..e26aadb0d 100644
--- a/desktop/src/main.tsx
+++ b/desktop/src/main.tsx
@@ -45,6 +45,7 @@ import { useSelection } from './store/selectionStore';
import { PlayRealm } from './engine/PlayRealm';
import { dockApi } from './layout/dockApi';
import { EditorControlSurface } from './engine/EditorSession';
+import { EditorHistory } from './engine/EditorHistory';
import { SceneModel } from './engine/SceneModel';
import { EngineHost } from './engine/EngineHost';
import { Particle, getComponent } from 'esengine';
@@ -97,7 +98,16 @@ if (new URLSearchParams(location.search).has('automation')) {
/** Resolves once the scene is ADOPTED and readable (drivers must not need
* their own get_scene_tree polling): waits out the model-version bump the
* adopt performs, racing the engine boot on a freshly opened project. */
- openScene: async (rel: string) => {
+ openScene: async (rel: string, discardChanges = false) => {
+ // A person gets a "save your work?" prompt here. A driver got silence and
+ // lost whatever it had built — opening the scene it had just authored into
+ // reloads it from disk, and the hundred entities never written are gone.
+ // Refuse instead, and make discarding something the caller says out loud.
+ if (!discardChanges && EditorHistory.isDirty()) {
+ throw new Error(
+ 'the open scene has unsaved changes — save_scene first, or pass discardChanges to throw them away',
+ );
+ }
const v0 = EditorControlSurface.worldVersion();
await ProjectStore.openScene(rel);
const t0 = Date.now();
diff --git a/desktop/tests/editor-mcp-tools.test.ts b/desktop/tests/editor-mcp-tools.test.ts
index ade149628..fe83fb4ff 100644
--- a/desktop/tests/editor-mcp-tools.test.ts
+++ b/desktop/tests/editor-mcp-tools.test.ts
@@ -194,3 +194,24 @@ describe('apply_scene_ops program sources', () => {
expect(driver).toHaveBeenCalledWith('applyOps', [ops, 'Inline'], 'editor');
});
});
+
+describe('open_scene guards unsaved work', () => {
+ const openScene = () => TOOLS.find((t) => t.name === 'open_scene');
+
+ it('passes the discard flag through, defaulting to refusing', async () => {
+ const driver = vi.fn().mockResolvedValue(undefined);
+ await runTool(openScene(), driver, { path: 'assets/scenes/a.esscene' });
+ expect(driver).toHaveBeenCalledWith('openScene', ['assets/scenes/a.esscene', false], 'editor');
+
+ driver.mockClear();
+ await runTool(openScene(), driver, { path: 'assets/scenes/a.esscene', discardChanges: true });
+ expect(driver).toHaveBeenCalledWith('openScene', ['assets/scenes/a.esscene', true], 'editor');
+ });
+
+ it('surfaces the refusal as an error the caller can act on', async () => {
+ const driver = vi.fn().mockRejectedValue(new Error('the open scene has unsaved changes — save_scene first'));
+ const res = await runTool(openScene(), driver, { path: 'assets/scenes/a.esscene' });
+ expect(res.isError).toBe(true);
+ expect(res.content[0].text).toMatch(/unsaved changes/);
+ });
+});