Skip to content

Commit b165b64

Browse files
feat(studio-server): a host can make Cmd+Z undo the newest change, whoever made it (#4603)
* feat(studio-server): a host can make Cmd+Z undo the newest change whoever made it * fix(studio-server): Cmd+Z for everyone leaves an agent turn that is still open alone * style(studio-server): format the undo scope check * refactor(studio-server): the undo scope option names itself * fix(studio-server): an open turn's parts are known by id, and a new change ends redo for everyone * style(studio-server): format the open turn check * fix(studio-server): an open turn's part is known before its entry is announced
1 parent 3db3da7 commit b165b64

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎packages/studio-server/src/history/projectHistory.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,66 @@ describe("claim: a writer that records after writing", () => {
17361736
expect([read("index.html"), read("r.js")]).toEqual(["B", "2"]);
17371737
});
17381738

1739+
describe("with undoScope everyone", () => {
1740+
async function editThenTurn() {
1741+
const opened = await project({ "index.html": "A" }, { undoScope: "everyone" });
1742+
await change(opened.history, you, "Moved Title", () => opened.write("index.html", "B"));
1743+
const window = await opened.history.beginWindow(agent, "Agent turn");
1744+
opened.write("index.html", "C");
1745+
await window.close();
1746+
return opened;
1747+
}
1748+
1749+
it("Cmd+Z undoes the newest change whoever made it: the agent's turn, then the person's edit", async () => {
1750+
const { history, read } = await editThenTurn();
1751+
1752+
expect(await history.step("back", you)).toMatchObject({
1753+
ok: true,
1754+
entry: { label: "Undid: Agent turn" },
1755+
});
1756+
expect(read("index.html")).toBe("B");
1757+
expect(await history.step("back", you)).toMatchObject({
1758+
ok: true,
1759+
entry: { label: "Undid: Moved Title" },
1760+
});
1761+
expect(read("index.html")).toBe("A");
1762+
});
1763+
1764+
it("Shift+Cmd+Z brings them back in turn", async () => {
1765+
const { history, read } = await editThenTurn();
1766+
await history.step("back", you);
1767+
await history.step("back", you);
1768+
1769+
await history.step("forward", you);
1770+
expect(read("index.html")).toBe("B");
1771+
await history.step("forward", you);
1772+
expect(read("index.html")).toBe("C");
1773+
});
1774+
1775+
it("a new change by anyone ends Shift+Cmd+Z", async () => {
1776+
const { history, write } = await editThenTurn();
1777+
await history.step("back", you);
1778+
await change(history, agent, "Another turn", () => write("index.html", "D"));
1779+
1780+
expect(history.next("forward", you)).toBeUndefined();
1781+
});
1782+
1783+
it("leaves an agent's turn that is still open alone", async () => {
1784+
const { history, write, read } = await project(
1785+
{ "index.html": "A", "r.js": "1" },
1786+
{ undoScope: "everyone" },
1787+
);
1788+
await change(history, you, "Moved Title", () => write("index.html", "B"));
1789+
const window = await history.beginWindow(agent, "Agent turn");
1790+
write("r.js", "2");
1791+
1792+
expect(history.next("back", you)?.label).toBe("Moved Title");
1793+
await history.step("back", you);
1794+
expect([read("index.html"), read("r.js")]).toEqual(["A", "2"]);
1795+
await window.close();
1796+
});
1797+
});
1798+
17391799
it("an agent's turn the person undid and redid is the person's to Cmd+Z", async () => {
17401800
const { history, write } = await project({ "index.html": "A" });
17411801
const window = await history.beginWindow(agent, "Agent turn");

‎packages/studio-server/src/history/projectHistory.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export interface ProjectHistoryOptions {
7676
ownerWaitMs?: number;
7777
/** A CLI turn's window from an earlier open: writes since, within its idle limit, become the entry with its id. */
7878
closedWindow?: ClosedWindow;
79+
undoScope?: "own" | "everyone";
7980
}
8081

8182
interface ClaimOptions {
@@ -143,7 +144,7 @@ export interface ProjectHistory {
143144
/** A watcher saw `path` change (project-relative or absolute). */
144145
noteChange(path: string): void;
145146
list(): HistoryListItem[];
146-
/** Cmd+Z (back) and Cmd+Shift+Z (forward) over `who`'s own and outside changes; `writeToken` labels the echo. */
147+
/** Cmd+Z (back) and Cmd+Shift+Z (forward) over `who`'s own and outside changes, or all (undoScope everyone). */
147148
step(direction: "back" | "forward", who: HistoryWho, options?: Writing): Promise<HistoryResult>;
148149
/** The entry `who`'s next step reverts, pending changes included, as of the last scan (a step scans first). */
149150
next(direction: "back" | "forward", who: HistoryWho): HistoryEntry | undefined;
@@ -183,6 +184,7 @@ interface Group {
183184
lastWriteAt?: number;
184185
idleTimer?: NodeJS.Timeout;
185186
entry?: HistoryEntry | null;
187+
parts?: Set<string>;
186188
}
187189

188190
/**
@@ -621,6 +623,7 @@ class Engine {
621623
part.changes.set(path, window.changes.get(path)!);
622624
window.changes.delete(path);
623625
}
626+
(window.parts ??= new Set()).add(part.id);
624627
window.entry = await this.commit(part);
625628
}
626629

@@ -909,7 +912,14 @@ class Engine {
909912
(group) => group?.changes.size && mine(group.who),
910913
);
911914
if (pending) return direction === "back" ? this.pendingEntry(pending) : undefined;
912-
return stepTarget(this.log.entries, direction, (entry) => mine(entry.who));
915+
const everyone = this.options.undoScope === "everyone";
916+
const ofOpenTurn = (entry: HistoryEntry) =>
917+
this.windows.some((open) => open.parts?.has(entry.id));
918+
return stepTarget(
919+
this.log.entries,
920+
direction,
921+
(entry) => mine(entry.who) || (everyone && !ofOpenTurn(entry)),
922+
);
913923
}
914924

915925
/** A pending group as the entry it becomes once committed (a window's part gets a fresh id). */

0 commit comments

Comments
 (0)