Skip to content

Write HTML. Render video. Built for agents.

fix(studio-server): a gesture sent without an idle time holds until another key (#4513)

Miguel Ángel162de1a6ab5a
diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts
index c55633610e..a97a30af14 100644
--- a/packages/studio-server/src/history/projectHistory.ts
+++ b/packages/studio-server/src/history/projectHistory.ts
@@ -261,7 +261,7 @@ class Engine {
windows: Group[] = [];
outside: Group | null = null;
/** A coalescing claim, open until another key, its idle timer, an operation, a window, or another write. */
- claimed: { group: Group; key: string; timer: NodeJS.Timeout } | null = null;
+ claimed: { group: Group; key: string; timer?: NodeJS.Timeout } | null = null;
writeToken: string | undefined;
quietTimer: NodeJS.Timeout | undefined;
maxTimer: NodeJS.Timeout | undefined;
@@ -578,8 +578,10 @@ class Engine {
idleMs = this.options.quietMs ?? 2000,
): { id: string } | null {
clearTimeout(this.claimed?.timer);
- const timer = setTimeout(() => this.background(() => this.commitClaim()), idleMs);
- timer.unref?.();
+ const timer = Number.isFinite(idleMs)
+ ? setTimeout(() => this.background(() => this.commitClaim()), idleMs)
+ : undefined;
+ timer?.unref?.();
this.claimed = { group, key, timer };
return group.changes.size ? { id: group.id } : null;
}
diff --git a/packages/studio-server/src/routes/history.test.ts b/packages/studio-server/src/routes/history.test.ts
index d0e45ab158..73a3a1ac8a 100644
--- a/packages/studio-server/src/routes/history.test.ts
+++ b/packages/studio-server/src/routes/history.test.ts
@@ -36,12 +36,13 @@ function apiFor(projectDir: string, history?: ProjectHistory) {
}
/** A project whose index.html reads "A", with its history and the routes over it. */
-async function demoProject() {
+async function demoProject({ quietMs }: { quietMs?: number } = {}) {
const projectDir = tempDir("hf-history-routes-");
writeFileSync(join(projectDir, "index.html"), "A");
const history = await openProjectHistory({
projectDir,
historyRoot: tempDir("hf-history-routes-root-"),
+ ...(quietMs && { quietMs }),
});
cleanup.push(() => history.close());
return { projectDir, history, call: apiFor(projectDir, history) };
@@ -190,6 +191,24 @@ describe("history routes", () => {
expect(readFileSync(index, "utf-8")).toBe(saved(undoneTo));
});
+ it("hold a gesture claimed without idleMs until another key, however long it pauses", async () => {
+ const { projectDir, history, call } = await demoProject({ quietMs: 30 });
+ const drag = (text: string) => {
+ writeFileSync(join(projectDir, "index.html"), text);
+ return call("/claim", {
+ label: "Dragged Title",
+ paths: ["index.html"],
+ coalesceKey: "drag",
+ idleMs: null,
+ });
+ };
+ await drag("B");
+ await new Promise((resolve) => setTimeout(resolve, 150));
+ await drag("C");
+ await history.flush();
+ expect(history.list().map((entry) => entry.label)).toEqual(["Dragged Title"]);
+ });
+
it("label an undo's writes with Studio's write token, so their echo reads as Studio's own", async () => {
const { projectDir, call } = await demoProject();
writeFileSync(join(projectDir, "index.html"), "B");
diff --git a/packages/studio-server/src/routes/history.ts b/packages/studio-server/src/routes/history.ts
index 53fec1cf33..62478607e0 100644
--- a/packages/studio-server/src/routes/history.ts
+++ b/packages/studio-server/src/routes/history.ts
@@ -122,16 +122,14 @@ export function registerHistoryRoutes(api: Hono, adapter: StudioApiAdapter): voi
const bytes = await history.readBlob(hash).catch(() => null);
return bytes ? c.body(new Uint8Array(bytes)) : missing();
});
- // Studio records after it writes: its edit claims the paths it just wrote, under the edit's label.
+ // Studio records after it writes, under the edit's label; a gesture with no idleMs holds until another key.
api.post(`${base}/claim`, (c) =>
withHistory(adapter, c, async (history, body) => {
const paths = Array.isArray(body.paths) ? body.paths.filter((path) => text(path)) : [];
const coalesceKey = text(body.coalesceKey) ?? undefined;
- const idleMs = idleOf(body);
const overwrote = versionsOf(body.overwrote);
const claimed = await history.claim(YOU, text(body.label) ?? "Edited in Studio", paths, {
- ...(coalesceKey && { coalesceKey }),
- ...(idleMs && { idleMs }),
+ ...(coalesceKey && { coalesceKey, idleMs: idleOf(body) ?? Infinity }),
...(overwrote && { overwrote }),
});
return { claimed };