Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Godogen: restore keyboard control and rider framing (#225)

Dee3a7bd8d26439
diff --git a/store/agents/godogen/AGENTS.md b/store/agents/godogen/AGENTS.md
index a4d1c72a6..9ae547ce0 100644
--- a/store/agents/godogen/AGENTS.md
+++ b/store/agents/godogen/AGENTS.md
@@ -22,6 +22,7 @@ Keep the `harness:ready` event and `window.harnessGame` bridge. Report ready onl
has rendered and its required assets loaded. Preserve play/explore, pause, restart, and keyboard
support. Every visible button must work. Do not call the task done based on compilation alone:
test input, movement, the goal, losing/restarting where appropriate, and browser runtime errors.
+Reset the follow camera with the player on restart; the first frame should show where to play.
Preserve `captureState()` and `restoreState()` for Rewind and saved playtest moments. Capture
complete JSON game state; validate snapshots before mutation and restore the visuals as well
diff --git a/store/agents/godogen/template/src/main.ts b/store/agents/godogen/template/src/main.ts
index 21f7edbf7..ce2d38df4 100644
--- a/store/agents/godogen/template/src/main.ts
+++ b/store/agents/godogen/template/src/main.ts
@@ -385,11 +385,18 @@ function reset() {
}
hud();
positionRider();
+ if (mode === "play") resetPlayCamera();
dispatchEvent(new CustomEvent("harness:timeline-reset"));
}
function positionRider() {
rider.position.set(x, groundY(x, z) + jump, z);
}
+function resetPlayCamera() {
+ camera.setTarget(new Vector3(x, groundY(x, z) + 2, z + 7));
+ camera.alpha = -Math.PI / 2;
+ camera.beta = 1.03;
+ camera.radius = 24;
+}
function end(crashed: boolean) {
running = false;
outcome = crashed ? "crashed" : "finished";
@@ -410,10 +417,7 @@ function setMode(value: Mode) {
if (value === "play") {
camera.detachControl();
if (!running) reset();
- camera.setTarget(new Vector3(x, groundY(x, z) + 2, z + 7));
- camera.alpha = -Math.PI / 2;
- camera.beta = 1.03;
- camera.radius = 24;
+ resetPlayCamera();
} else {
if (!paused) camera.attachControl(canvas, true);
camera.setTarget(new Vector3(0, 4, 22));
diff --git a/store/viewers/game-viewer/test/studio-focus.test.mjs b/store/viewers/game-viewer/test/studio-focus.test.mjs
new file mode 100644
index 000000000..579f33523
--- /dev/null
+++ b/store/viewers/game-viewer/test/studio-focus.test.mjs
@@ -0,0 +1,209 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { randomUUID } from "node:crypto";
+import { runInNewContext } from "node:vm";
+
+const studio = readFileSync(new URL("../web/studio.js", import.meta.url), "utf8")
+ .replace(/^import .*;\n/, "");
+const playtest = readFileSync(new URL("../web/playtest.js", import.meta.url), "utf8")
+ .replace("export function createPlaytest", "function createPlaytest");
+
+// Execute both real controllers with a small DOM/bridge adapter. This checks
+// focus ownership through asynchronous toolbar/dialog flows, not rendered UI.
+async function fixture() {
+ const elements = new Map(), handlers = new Map(), focusEvents = [];
+ let source, frame, rejectRestore = false;
+ const document = {
+ activeElement: null,
+ getElementById: (id) => {
+ if (!elements.has(id)) elements.set(id, new Element(id));
+ return elements.get(id);
+ },
+ querySelector: (id) => document.getElementById(id),
+ createElement: (tag) => new Element(tag),
+ };
+ class Element {
+ constructor(tag) {
+ this.tag = tag;
+ this.style = {};
+ this.content = "fixture-token";
+ this.children = [];
+ this.attributes = new Map();
+ this.listeners = new Map();
+ const classes = new Set();
+ this.classList = {
+ toggle(name, on = !classes.has(name)) {
+ if (on) classes.add(name);
+ else classes.delete(name);
+ return on;
+ },
+ contains: (name) => classes.has(name),
+ };
+ if (tag === "iframe") {
+ frame = this;
+ this.contentWindow = {
+ focus: () => {
+ focusEvents.push(`window:${this.tag}`);
+ if (!this.inert) document.activeElement = this;
+ },
+ postMessage: ({ action, value, requestId }) => {
+ if (!requestId) return;
+ const result = action === "capture" || action === "restore"
+ ? { state: value || { score: 1 }, stats: { time: 2, score: 1 }, mode: "play" }
+ : { ok: true };
+ queueMicrotask(() => message({
+ type: "response", requestId, result,
+ ...(action === "restore" && rejectRestore ? { error: "Snapshot rejected" } : {}),
+ }));
+ },
+ };
+ }
+ }
+ append(...children) { this.children.push(...children); }
+ replaceChildren(...children) { this.children = children; }
+ querySelectorAll() { return this.children.filter((child) => child.tag === "button"); }
+ setAttribute(name, value) {
+ this.attributes.set(name, value);
+ if (name === "inert") this.inert = true;
+ }
+ removeAttribute(name) {
+ this.attributes.delete(name);
+ if (name === "inert") this.inert = false;
+ }
+ addEventListener(name, handler) { this.listeners.set(name, handler); }
+ focus() {
+ focusEvents.push(`element:${this.tag}`);
+ if (!this.inert) document.activeElement = this;
+ }
+ showModal() { this.open = true; }
+ close() {
+ this.open = false;
+ this.listeners.get("close")?.();
+ }
+ remove() {}
+ }
+ const origin = "http://127.0.0.1:4111";
+ const message = (data) => handlers.get("message")({
+ origin, source: frame.contentWindow, data: { studio: true, id: "game", ...data },
+ });
+ runInNewContext(`const createPlaytest = (() => { ${playtest}; return createPlaytest; })();\n${studio}`, {
+ document,
+ location: { origin },
+ crypto: { randomUUID },
+ setInterval() {}, setTimeout() {}, clearTimeout() {},
+ addEventListener: (name, handler) => handlers.set(name, handler),
+ EventSource: class {
+ constructor() { source = this; this.handlers = new Map(); }
+ addEventListener(name, handler) { this.handlers.set(name, handler); }
+ },
+ fetch: async (path, options) => ({
+ ok: true,
+ json: async () => path === "/api/playtests" && !options
+ ? [] : { path: "out/playtests/saved" },
+ }),
+ });
+ const revision = { id: "game", number: 1, url: "/game/index.html", project: {} };
+ source.handlers.get("state")({ data: JSON.stringify({
+ status: "ready", project: {}, progress: {}, activity: [], history: [revision], latest: revision,
+ }) });
+ await message({ type: "ready" });
+ const stats = () => message({
+ type: "stats", stats: {}, features: { play: true, pause: true, restart: true, rewind: true },
+ });
+ await stats();
+ const click = async (id) => {
+ const target = document.getElementById(id);
+ target.focus();
+ await target.onclick?.({ preventDefault() {} });
+ };
+ return {
+ click, stats, document, frame, message, focusEvents,
+ element: document.getElementById,
+ rejectRestore() { rejectRestore = true; },
+ async save() {
+ document.getElementById("moment-save").focus();
+ await document.getElementById("moment-form").onsubmit({ preventDefault() {} });
+ },
+ };
+}
+
+test("starting Play focuses the iframe element before its window", async () => {
+ const app = await fixture();
+ app.focusEvents.length = 0;
+ await app.click("play");
+ // A native WebKit pane can keep keyboard input in the neighboring terminal
+ // when only the child window is focused. Exercise the real Play handler.
+ assert.deepEqual(app.focusEvents, ["element:play", "element:iframe", "window:iframe"]);
+ assert.equal(app.document.activeElement, app.frame);
+});
+
+test("Resume returns keyboard focus to the game; Pause and Explore retain toolbar focus", async () => {
+ const app = await fixture();
+ await app.click("play");
+ assert.equal(app.document.activeElement, app.frame);
+ await app.click("pause");
+ assert.equal(app.document.activeElement, app.element("pause"));
+ await app.click("pause");
+ assert.equal(app.document.activeElement, app.frame);
+ await app.click("explore");
+ await app.click("pause");
+ await app.click("pause");
+ assert.equal(app.document.activeElement, app.element("pause"));
+});
+
+test("Back to live unlocks the game before returning keyboard focus", async () => {
+ const app = await fixture();
+ await app.click("play");
+ for (let i = 0; i < 2; i++)
+ await app.message({ type: "sample", at: i * 200, state: { score: i }, stats: { time: i } });
+ await app.click("rewind");
+ assert.equal(app.frame.inert, true);
+ await app.click("back-live");
+ assert.equal(app.frame.inert, false);
+ assert.equal(app.document.activeElement, app.frame);
+});
+
+test("canceling or saving a playtest note returns focus only after the game is unlocked", async () => {
+ for (const save of [false, true]) {
+ const app = await fixture();
+ await app.click("play");
+ await app.click("pin-moment");
+ assert.equal(app.frame.inert, true);
+ assert.equal(app.document.activeElement, app.element("moment-note"));
+ if (save) await app.save();
+ else await app.click("moment-cancel");
+ assert.equal(app.frame.inert, false);
+ assert.equal(app.document.activeElement, app.frame);
+ }
+});
+
+test("a paused game and subsequent stats updates do not steal focus", async () => {
+ const app = await fixture();
+ await app.click("play");
+ await app.click("pause");
+ await app.click("pin-moment");
+ await app.click("moment-cancel");
+ assert.equal(app.frame.inert, false);
+ assert.notEqual(app.document.activeElement, app.frame);
+ app.element("details").focus();
+ await app.stats();
+ assert.equal(app.document.activeElement, app.element("details"));
+ await app.click("pause");
+ assert.equal(app.document.activeElement, app.frame);
+ app.element("details").focus();
+ await app.stats();
+ assert.equal(app.document.activeElement, app.element("details"));
+});
+
+test("failed restoration retains the rewind input lock", async () => {
+ const app = await fixture();
+ await app.click("play");
+ for (let i = 0; i < 2; i++)
+ await app.message({ type: "sample", at: i * 200, state: { score: i }, stats: { time: i } });
+ await app.click("rewind");
+ app.rejectRestore();
+ await app.click("back-live");
+ assert.equal(app.frame.inert, true);
+ assert.notEqual(app.document.activeElement, app.frame);
+});
diff --git a/store/viewers/game-viewer/web/studio.js b/store/viewers/game-viewer/web/studio.js
index 450e58c67..0278d70d7 100644
--- a/store/viewers/game-viewer/web/studio.js
+++ b/store/viewers/game-viewer/web/studio.js
@@ -61,6 +61,13 @@ function command(action, value) {
location.origin,
);
}
+function focusGame() {
+ const frame = frames.get(active)?.frame;
+ // WebKit must focus the iframe element when keyboard input comes from a
+ // different native pane; focusing only its window can leave that pane active.
+ frame?.focus({ preventScroll: true });
+ frame?.contentWindow?.focus();
+}
function request(action, value) {
const entry = frames.get(active);
if (!entry?.ready) return Promise.reject(new Error("The game is not ready"));
@@ -103,10 +110,14 @@ const playtest = createPlaytest({
request,
toast,
lock(value) {
+ const wasLocked = momentLock;
momentLock = value;
controls();
+ // Resume keyboard play after a rewind or note, once the iframe is interactive.
+ if (wasLocked && !value && mode === "play" && !paused)
+ focusGame();
},
- focus: () => frames.get(active)?.frame.contentWindow?.focus(),
+ focus: focusGame,
async resume() {
if (mode !== "play") setMode("play");
setPaused(false);
@@ -219,7 +230,7 @@ function setMode(value) {
state?.project.controls ||
"Click the game to use its controls"
: "Drag to orbit · Scroll to look closer";
- if (value === "play") frames.get(active)?.frame.contentWindow?.focus();
+ if (value === "play") focusGame();
}
function setPaused(value) {
paused = value;
@@ -235,11 +246,15 @@ $("play").onclick = () => {
setMode("play");
playtest.render();
};
-$("pause").onclick = () => setPaused(!paused);
+$("pause").onclick = () => {
+ setPaused(!paused);
+ if (!paused && mode === "play")
+ focusGame();
+};
$("restart").onclick = () => {
command("restart");
setPaused(false);
- if (mode === "play") frames.get(active)?.frame.contentWindow?.focus();
+ if (mode === "play") focusGame();
};
$("update").onclick = () => {
followLatest = true;