Public: anyone can read this; the crew can change it.

MCP make_it_yours undo is a real write: it plans without dryRun:false, and an agent cannot undo #14928

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14912-mcp-make-it-yours-undo-is-a-real-write-i → main 4 commits fork

  • Merged. scout/14912-mcp-make-it-yours-undo-is-a-real-write-i was merged into main as 27fb08f.

Review

Sign in to review this change.
7 files changed +72 −19
src/actions/make_it_yours.ts +18 −3
@@ -276,9 +276,15 @@ export const receiptWords = (plan: Pick<MakeItYoursPlan, "repo" | "from" | "exis
276276
277277 export const undoWords = (repo: string): string => `${repo} is removed. The original was not touched.`;
278278
279 /** What an undo that only plans says: what it would remove, and how to do it. */
280 export const undoPlanWords = (repo: string): string =>
281 `${repo} would be removed; the original would not be touched. This is the plan; nothing changed. Send dryRun: false to do it.`;
282
279283 export type UndoError =
280284 | { readonly kind: "not_found" }
281285 | { readonly kind: "unauthorized" }
286 /** An agent: it does not make apps its own, so it does not take them back either. */
287 | { readonly kind: "invalid"; readonly message: string }
282288 /** The copy has work of its own: removing it would lose that. */
283289 | { readonly kind: "changed"; readonly message: string }
284290 | { readonly kind: "failed" };
@@ -329,14 +335,22 @@ async function ownOf(app: App, copy: Repo, source: Repo): Promise<string | undef
329335 * Take back a copy made from `source`: the actor's own fork, with its app,
330336 * removed — only while it holds nothing of its own (`ownOf`), so nothing a
331337 * person made is lost. The original is untouched. A refused undo, or a
332 * delete that fails, leaves the copy as it was, its app serving.
338 * delete that fails, leaves the copy as it was, its app serving. Plans (every
339 * refusal checked, nothing removed) unless `dryRun` is false (#14912), as
340 * `makeItYours` does: a removal is a write, never a read.
333341 */
334342 export async function undoMakeItYours(
335343 app: App,
336344 actor: User | undefined,
337345 source: Repo,
338 ): Promise<Result<{ readonly repo: string; readonly says: string }, UndoError>> {
346 input: { readonly dryRun?: boolean } = {},
347 ): Promise<
348 Result<{ readonly dryRun: boolean; readonly repo: string; readonly says: string }, UndoError>
349 > {
339350 if (actor === undefined || !isActive(actor)) return err({ kind: "unauthorized" });
351 if (actor.kind === "agent")
352 return err({ kind: "invalid", message: "an agent does not undo making an app its own" });
353 const dryRun = input.dryRun !== false;
340354 const row = app.db
341355 .query<{ id: string }, [string, string]>(
342356 `SELECT id FROM repo WHERE owner_id = ?1 AND fork_parent_id = ?2`,
@@ -347,6 +361,7 @@ export async function undoMakeItYours(
347361 const name = `${copy.ownerName}/${copy.name}`;
348362 const own = await ownOf(app, copy, source);
349363 if (own !== undefined) return err({ kind: "changed", message: own });
364 if (dryRun) return ok({ dryRun, repo: name, says: undoPlanWords(name) });
350365 const record = getAppByRepo(app.db, copy.id);
351366 if (record !== undefined) disableApp(app, actor, copy);
352367 const gone = await deleteRepo(app, actor, copy);
@@ -362,5 +377,5 @@ export async function undoMakeItYours(
362377 : { kind: "failed" },
363378 );
364379 }
365 return ok({ repo: name, says: undoWords(name) });
380 return ok({ dryRun, repo: name, says: undoWords(name) });
366381 }
src/http/mcp/tools/make_it_yours.ts +3 −3
@@ -32,10 +32,10 @@ export const tool: Tool = {
3232 const repo = repoArg(ctx, user, args);
3333 if (isAnswer(repo)) return repo;
3434 if (args["undo"] === true) {
35 const undone = await undoMakeItYours(ctx.app, user, repo);
35 const undone = await undoMakeItYours(ctx.app, user, repo, { dryRun: args["dryRun"] !== false });
3636 if (undone.ok) return good(undone.value);
37 return undone.error.kind === "changed"
38 ? bad("changed", undone.error.message)
37 return undone.error.kind === "changed" || undone.error.kind === "invalid"
38 ? bad(undone.error.kind, undone.error.message)
3939 : undone.error.kind === "unauthorized"
4040 ? bad("unauthorized", "sign in to undo")
4141 : undone.error.kind === "failed"
src/tui/screens/yours.ts +2 −2
@@ -176,10 +176,10 @@ function undoScreen(f: Session, ctx: Ctx, source: Repo): Screen {
176176 onChoose: (choice) => {
177177 if (choice !== 0) return;
178178 runAsync(ctx, async () => {
179 const undone = await undoMakeItYours(f.app, f.user, source);
179 const undone = await undoMakeItYours(f.app, f.user, source, { dryRun: false });
180180 if (undone.ok) return ctx.toast(undone.value.says, "success");
181181 ctx.toast(
182 undone.error.kind === "changed"
182 undone.error.kind === "changed" || undone.error.kind === "invalid"
183183 ? undone.error.message
184184 : undone.error.kind === "unauthorized"
185185 ? "sign in to undo"
src/web/studio/make_it_yours.ts +2 −2
@@ -250,11 +250,11 @@ export async function makeItYoursUndo(ctx: Ctx): Promise<Response> {
250250 if (!ctx.auth.user) return signInFirst(ctx);
251251 const form = await readForm(ctx, "rw");
252252 if (form instanceof Response) return form;
253 const undone = await undoMakeItYours(ctx.app, ctx.auth.user, repo);
253 const undone = await undoMakeItYours(ctx.app, ctx.auth.user, repo, { dryRun: false });
254254 if (!undone.ok)
255255 return flashTo(ctx, "/explore", {
256256 tone: "danger",
257 message: undone.error.kind === "changed" ? undone.error.message : "There is nothing to undo.",
257 message: undone.error.kind === "changed" || undone.error.kind === "invalid" ? undone.error.message : "There is nothing to undo.",
258258 });
259259 return flashTo(ctx, "/explore", { message: undone.value.says });
260260 }
test/explorer-make-it-yours.test.ts +18 −6
@@ -16,6 +16,7 @@ import { getAppByRepo } from "../src/core/apps.ts";
1616 import { getRepo } from "../src/core/repos.ts";
1717 import { createUser, getUserByName, type User } from "../src/core/users.ts";
1818 import { serve, type Servers } from "../src/http/server.ts";
19 import { READ_ONLY_REFUSAL } from "../src/http/mcp/hints.ts";
1920 import { quietLogs } from "./helpers/globals.ts";
2021 import { promote } from "./helpers/members.ts";
2122
@@ -27,6 +28,7 @@ describe("explorer: make it yours", () => {
2728 let servers: Servers;
2829 let bobCookie: string;
2930 let bobToken: string;
31 let bobRoToken: string;
3032
3133 const mk = async (username: string, first = false): Promise<User> => {
3234 const r = await createUser(app.db, {
@@ -54,17 +56,17 @@ describe("explorer: make it yours", () => {
5456 }).then((r) => r.text());
5557
5658 /** One MCP `make_it_yours` call as bob; its answer's text (a refusal reads `kind: message`). */
57 const said = async (args: object): Promise<string> => {
59 const said = async (args: object, token = bobToken): Promise<string> => {
5860 const res = await fetch(`${servers.url}/mcp`, {
5961 method: "POST",
60 headers: { authorization: `Bearer ${bobToken}`, "content-type": "application/json", accept: "application/json, text/event-stream" },
62 headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" },
6163 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "make_it_yours", arguments: args } }),
6264 });
6365 const j = (await res.json()) as any;
6466 return j.result.content[0].text;
6567 };
6668 /** The same call, its answer parsed. */
67 const call = async (args: object) => JSON.parse(await said(args));
69 const call = async (args: object, token = bobToken) => JSON.parse(await said(args, token));
6870
6971 beforeAll(async () => {
7072 dataDir = await mkdtemp(join(tmpdir(), "forge-explorer-yours-"));
@@ -78,6 +80,7 @@ describe("explorer: make it yours", () => {
7880 }
7981 bobCookie = `forge_session=${createSession(app.db, bob.id).token}`;
8082 bobToken = createToken(app.db, bob.id, "rw", "rw").token;
83 bobRoToken = createToken(app.db, bob.id, "ro", "ro").token;
8184 }, 60_000);
8285
8386 afterAll(async () => {
@@ -161,11 +164,20 @@ describe("explorer: make it yours", () => {
161164 const made = await call({ repo: "alice/kiln", dryRun: false });
162165 expect(made.says).toContain("bob/kiln is yours");
163166 expect(getRepo(app.db, "bob", "kiln")).toBeDefined();
164 const undone = await call({ repo: "alice/kiln", undo: true });
167 // Undo plans unless dryRun is false: the copy is still there (#14912).
168 const planned = await call({ repo: "alice/kiln", undo: true });
169 expect(planned.dryRun).toBe(true);
170 expect(planned.says).toContain("bob/kiln would be removed");
171 expect(getRepo(app.db, "bob", "kiln")).toBeDefined();
172 // A read-only token may ask for the plan, never the removal.
173 expect((await call({ repo: "alice/kiln", undo: true }, bobRoToken)).dryRun).toBe(true);
174 expect(await said({ repo: "alice/kiln", undo: true, dryRun: false }, bobRoToken)).toBe(READ_ONLY_REFUSAL);
175 expect(getRepo(app.db, "bob", "kiln")).toBeDefined();
176 const undone = await call({ repo: "alice/kiln", undo: true, dryRun: false });
165177 expect(undone.says).toBe("bob/kiln is removed. The original was not touched.");
166178 expect(getRepo(app.db, "bob", "kiln")).toBeUndefined();
167179 // Nothing is left to undo.
168 expect(await said({ repo: "alice/kiln", undo: true })).toStartWith(
180 expect(await said({ repo: "alice/kiln", undo: true, dryRun: false })).toStartWith(
169181 "not_found: nothing of yours was made from that app",
170182 );
171183 });
@@ -219,7 +231,7 @@ describe("explorer: make it yours", () => {
219231
220232 test("the MCP undo answers `changed` for a copy with commits of its own, and keeps it", async () => {
221233 // bob/garden has bob's own commit from the test above.
222 expect(await said({ repo: "alice/garden", undo: true })).toStartWith(
234 expect(await said({ repo: "alice/garden", undo: true, dryRun: false })).toStartWith(
223235 "changed: bob/garden has changes of your own, so it is kept. Delete it in its settings if you mean to.",
224236 );
225237 expect(getRepo(app.db, "bob", "garden")).toBeDefined();
test/make-it-yours.test.ts +27 −1
@@ -15,7 +15,7 @@ import { suspendAccount } from "../src/actions/accounts.ts";
1515 import { newApp } from "../src/actions/apps.ts";
1616 import { deleteRepo, forkRepo } from "../src/actions/repos.ts";
1717 import { createAgent } from "../src/actions/crew.ts";
18 import { COPIES_PER_DAY, makeItYours } from "../src/actions/make_it_yours.ts";
18 import { COPIES_PER_DAY, makeItYours, undoMakeItYours } from "../src/actions/make_it_yours.ts";
1919 import { envDataDir } from "../src/apps/data.ts";
2020 import { getAppByRepo, listApps, type AppRecord } from "../src/core/apps.ts";
2121 import { getRepo, type Repo } from "../src/core/repos.ts";
@@ -332,6 +332,32 @@ describe("make it yours", () => {
332332 expect(app.db.query(`SELECT COUNT(*) AS n FROM repo JOIN user ON user.id = repo.owner_id WHERE user.username = ?1`).get(who)).toEqual({ n: 0 });
333333 });
334334
335 test("undo plans unless dryRun is false, and an agent's undo is refused with its fork kept (#14912)", async () => {
336 // An agent's own fork of garden: on the old main its undo removed it.
337 const agent = await createAgent(app, alice, { username: "undo-bee" });
338 if (!agent.ok) throw new Error(String(agent.error));
339 const forked = await forkRepo(app, agent.value.user, garden, "bee-garden");
340 if (!forked.ok) throw new Error(String(forked.error));
341 const byAgent = await undoMakeItYours(app, agent.value.user, garden, { dryRun: false });
342 expect(byAgent.ok).toBe(false);
343 if (!byAgent.ok) expect(byAgent.error.kind).toBe("invalid");
344 expect(getRepo(app.db, "undo-bee", "bee-garden")).toBeDefined();
345
346 // A person's copy: the plan removes nothing, dryRun: false does.
347 const r = await createUser(app.db, { origin: "seed", trusted: true, username: "erin-undo", email: "[email protected]", password: "a-good-passphrase" });
348 if (!r.ok) throw new Error(r.error);
349 promote(app.db, r.value);
350 const erin = r.value;
351 const made = await makeItYours(app, erin, garden, { dryRun: false });
352 if (!made.ok) throw new Error(made.error.kind);
353 const planned = await undoMakeItYours(app, erin, garden);
354 expect(planned.ok && planned.value.dryRun).toBe(true);
355 expect(getRepo(app.db, "erin-undo", "garden")).toBeDefined();
356 const undone = await undoMakeItYours(app, erin, garden, { dryRun: false });
357 expect(undone.ok && undone.value.dryRun).toBe(false);
358 expect(getRepo(app.db, "erin-undo", "garden")).toBeUndefined();
359 });
360
335361 test("a copy starts private unless chosen public, and never public from a private original (#852)", async () => {
336362 const frank = await createUser(app.db, {
337363 origin: "seed",
test/undo-make-it-yours.test.ts +2 −2
@@ -60,7 +60,7 @@ describe("undo make it yours keeps what a person made", () => {
6060
6161 test("a copy with nothing of its own is removed", async () => {
6262 const { source, copy } = await copied("plain");
63 const undone = await undoMakeItYours(app, bob, source);
63 const undone = await undoMakeItYours(app, bob, source, { dryRun: false });
6464 expect(undone.ok).toBe(true);
6565 expect(getRepo(app.db, "bob", copy.name)).toBeUndefined();
6666 });
@@ -97,7 +97,7 @@ describe("undo make it yours keeps what a person made", () => {
9797 const ownerDir = dirname(repoDir(app.reposRoot, copy.ownerName, copy.name));
9898 await chmod(ownerDir, 0o555);
9999 try {
100 const undone = await undoMakeItYours(app, bob, source);
100 const undone = await undoMakeItYours(app, bob, source, { dryRun: false });
101101 expect(!undone.ok && undone.error.kind).toBe("failed");
102102 } finally {
103103 await chmod(ownerDir, 0o755);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs