Skip to content

Write HTML. Render video. Built for agents.

feat(studio-server): a host can make Cmd+Z undo the newest change, whoever made it (#4603)

Miguel Ángelb165b64eda84
diff --git a/packages/studio-server/src/history/projectHistory.test.ts b/packages/studio-server/src/history/projectHistory.test.ts
index deafb1ce7a..ef6571dd4c 100644
--- a/packages/studio-server/src/history/projectHistory.test.ts
+++ b/packages/studio-server/src/history/projectHistory.test.ts
@@ -1736,6 +1736,66 @@ describe("claim: a writer that records after writing", () => {
expect([read("index.html"), read("r.js")]).toEqual(["B", "2"]);
});
+ describe("with undoScope everyone", () => {
+ async function editThenTurn() {
+ const opened = await project({ "index.html": "A" }, { undoScope: "everyone" });
+ await change(opened.history, you, "Moved Title", () => opened.write("index.html", "B"));
+ const window = await opened.history.beginWindow(agent, "Agent turn");
+ opened.write("index.html", "C");
+ await window.close();
+ return opened;
+ }
+
+ it("Cmd+Z undoes the newest change whoever made it: the agent's turn, then the person's edit", async () => {
+ const { history, read } = await editThenTurn();
+
+ expect(await history.step("back", you)).toMatchObject({
+ ok: true,
+ entry: { label: "Undid: Agent turn" },
+ });
+ expect(read("index.html")).toBe("B");
+ expect(await history.step("back", you)).toMatchObject({
+ ok: true,
+ entry: { label: "Undid: Moved Title" },
+ });
+ expect(read("index.html")).toBe("A");
+ });
+
+ it("Shift+Cmd+Z brings them back in turn", async () => {
+ const { history, read } = await editThenTurn();
+ await history.step("back", you);
+ await history.step("back", you);
+
+ await history.step("forward", you);
+ expect(read("index.html")).toBe("B");
+ await history.step("forward", you);
+ expect(read("index.html")).toBe("C");
+ });
+
+ it("a new change by anyone ends Shift+Cmd+Z", async () => {
+ const { history, write } = await editThenTurn();
+ await history.step("back", you);
+ await change(history, agent, "Another turn", () => write("index.html", "D"));
+
+ expect(history.next("forward", you)).toBeUndefined();
+ });
+
+ it("leaves an agent's turn that is still open alone", async () => {
+ const { history, write, read } = await project(
+ { "index.html": "A", "r.js": "1" },
+ { undoScope: "everyone" },
+ );
+ await change(history, you, "Moved Title", () => write("index.html", "B"));
+ const window = await history.beginWindow(agent, "Agent turn");
+ write("r.js", "2");
+
+ expect(history.next("back", you)?.label).toBe("Moved Title");
+ await history.step("back", you);
+ expect([read("index.html"), read("r.js")]).toEqual(["A", "2"]);
+ await window.close();
+ });
+ });
+
it("an agent's turn the person undid and redid is the person's to Cmd+Z", async () => {
const { history, write } = await project({ "index.html": "A" });
const window = await history.beginWindow(agent, "Agent turn");
diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts
index 609b8f391a..75a957bbba 100644
--- a/packages/studio-server/src/history/projectHistory.ts
+++ b/packages/studio-server/src/history/projectHistory.ts
@@ -76,6 +76,7 @@ export interface ProjectHistoryOptions {
ownerWaitMs?: number;
/** A CLI turn's window from an earlier open: writes since, within its idle limit, become the entry with its id. */
closedWindow?: ClosedWindow;
+ undoScope?: "own" | "everyone";
}
interface ClaimOptions {
@@ -143,7 +144,7 @@ export interface ProjectHistory {
/** A watcher saw `path` change (project-relative or absolute). */
noteChange(path: string): void;
list(): HistoryListItem[];
- /** Cmd+Z (back) and Cmd+Shift+Z (forward) over `who`'s own and outside changes; `writeToken` labels the echo. */
+ /** Cmd+Z (back) and Cmd+Shift+Z (forward) over `who`'s own and outside changes, or all (undoScope everyone). */
step(direction: "back" | "forward", who: HistoryWho, options?: Writing): Promise<HistoryResult>;
/** The entry `who`'s next step reverts, pending changes included, as of the last scan (a step scans first). */
next(direction: "back" | "forward", who: HistoryWho): HistoryEntry | undefined;
@@ -183,6 +184,7 @@ interface Group {
lastWriteAt?: number;
idleTimer?: NodeJS.Timeout;
entry?: HistoryEntry | null;
+ parts?: Set<string>;
}
/**
@@ -621,6 +623,7 @@ class Engine {
part.changes.set(path, window.changes.get(path)!);
window.changes.delete(path);
}
+ (window.parts ??= new Set()).add(part.id);
window.entry = await this.commit(part);
}
@@ -909,7 +912,14 @@ class Engine {
(group) => group?.changes.size && mine(group.who),
);
if (pending) return direction === "back" ? this.pendingEntry(pending) : undefined;
- return stepTarget(this.log.entries, direction, (entry) => mine(entry.who));
+ const everyone = this.options.undoScope === "everyone";
+ const ofOpenTurn = (entry: HistoryEntry) =>
+ this.windows.some((open) => open.parts?.has(entry.id));
+ return stepTarget(
+ this.log.entries,
+ direction,
+ (entry) => mine(entry.who) || (everyone && !ofOpenTurn(entry)),
+ );
}
/** A pending group as the entry it becomes once committed (a window's part gets a fresh id). */