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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14912-mcp-make-it-yours-undo-is-a-real-write-i → main 4 commits fork
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 | ||
| 276 | 276 | |
| 277 | 277 | export const undoWords = (repo: string): string => `${repo} is removed. The original was not touched.`; |
| 278 | 278 | |
| 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 | ||
| 279 | 283 | export type UndoError = |
| 280 | 284 | | { readonly kind: "not_found" } |
| 281 | 285 | | { 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 } | |
| 282 | 288 | /** The copy has work of its own: removing it would lose that. */ |
| 283 | 289 | | { readonly kind: "changed"; readonly message: string } |
| 284 | 290 | | { readonly kind: "failed" }; |
| @@ -329,14 +335,22 @@ async function ownOf(app: App, copy: Repo, source: Repo): Promise<string | undef | ||
| 329 | 335 | * Take back a copy made from `source`: the actor's own fork, with its app, |
| 330 | 336 | * removed — only while it holds nothing of its own (`ownOf`), so nothing a |
| 331 | 337 | * 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. | |
| 333 | 341 | */ |
| 334 | 342 | export async function undoMakeItYours( |
| 335 | 343 | app: App, |
| 336 | 344 | actor: User | undefined, |
| 337 | 345 | 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 | > { | |
| 339 | 350 | 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; | |
| 340 | 354 | const row = app.db |
| 341 | 355 | .query<{ id: string }, [string, string]>( |
| 342 | 356 | `SELECT id FROM repo WHERE owner_id = ?1 AND fork_parent_id = ?2`, |
| @@ -347,6 +361,7 @@ export async function undoMakeItYours( | ||
| 347 | 361 | const name = `${copy.ownerName}/${copy.name}`; |
| 348 | 362 | const own = await ownOf(app, copy, source); |
| 349 | 363 | if (own !== undefined) return err({ kind: "changed", message: own }); |
| 364 | if (dryRun) return ok({ dryRun, repo: name, says: undoPlanWords(name) }); | |
| 350 | 365 | const record = getAppByRepo(app.db, copy.id); |
| 351 | 366 | if (record !== undefined) disableApp(app, actor, copy); |
| 352 | 367 | const gone = await deleteRepo(app, actor, copy); |
| @@ -362,5 +377,5 @@ export async function undoMakeItYours( | ||
| 362 | 377 | : { kind: "failed" }, |
| 363 | 378 | ); |
| 364 | 379 | } |
| 365 | return ok({ repo: name, says: undoWords(name) }); | |
| 380 | return ok({ dryRun, repo: name, says: undoWords(name) }); | |
| 366 | 381 | } |
src/http/mcp/tools/make_it_yours.ts +3 −3
| @@ -32,10 +32,10 @@ export const tool: Tool = { | ||
| 32 | 32 | const repo = repoArg(ctx, user, args); |
| 33 | 33 | if (isAnswer(repo)) return repo; |
| 34 | 34 | 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 }); | |
| 36 | 36 | 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) | |
| 39 | 39 | : undone.error.kind === "unauthorized" |
| 40 | 40 | ? bad("unauthorized", "sign in to undo") |
| 41 | 41 | : undone.error.kind === "failed" |
src/tui/screens/yours.ts +2 −2
| @@ -176,10 +176,10 @@ function undoScreen(f: Session, ctx: Ctx, source: Repo): Screen { | ||
| 176 | 176 | onChoose: (choice) => { |
| 177 | 177 | if (choice !== 0) return; |
| 178 | 178 | 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 }); | |
| 180 | 180 | if (undone.ok) return ctx.toast(undone.value.says, "success"); |
| 181 | 181 | ctx.toast( |
| 182 | undone.error.kind === "changed" | |
| 182 | undone.error.kind === "changed" || undone.error.kind === "invalid" | |
| 183 | 183 | ? undone.error.message |
| 184 | 184 | : undone.error.kind === "unauthorized" |
| 185 | 185 | ? "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> { | ||
| 250 | 250 | if (!ctx.auth.user) return signInFirst(ctx); |
| 251 | 251 | const form = await readForm(ctx, "rw"); |
| 252 | 252 | 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 }); | |
| 254 | 254 | if (!undone.ok) |
| 255 | 255 | return flashTo(ctx, "/explore", { |
| 256 | 256 | 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.", | |
| 258 | 258 | }); |
| 259 | 259 | return flashTo(ctx, "/explore", { message: undone.value.says }); |
| 260 | 260 | } |
test/explorer-make-it-yours.test.ts +18 −6
| @@ -16,6 +16,7 @@ import { getAppByRepo } from "../src/core/apps.ts"; | ||
| 16 | 16 | import { getRepo } from "../src/core/repos.ts"; |
| 17 | 17 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| 18 | 18 | import { serve, type Servers } from "../src/http/server.ts"; |
| 19 | import { READ_ONLY_REFUSAL } from "../src/http/mcp/hints.ts"; | |
| 19 | 20 | import { quietLogs } from "./helpers/globals.ts"; |
| 20 | 21 | import { promote } from "./helpers/members.ts"; |
| 21 | 22 | |
| @@ -27,6 +28,7 @@ describe("explorer: make it yours", () => { | ||
| 27 | 28 | let servers: Servers; |
| 28 | 29 | let bobCookie: string; |
| 29 | 30 | let bobToken: string; |
| 31 | let bobRoToken: string; | |
| 30 | 32 | |
| 31 | 33 | const mk = async (username: string, first = false): Promise<User> => { |
| 32 | 34 | const r = await createUser(app.db, { |
| @@ -54,17 +56,17 @@ describe("explorer: make it yours", () => { | ||
| 54 | 56 | }).then((r) => r.text()); |
| 55 | 57 | |
| 56 | 58 | /** 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> => { | |
| 58 | 60 | const res = await fetch(`${servers.url}/mcp`, { |
| 59 | 61 | 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" }, | |
| 61 | 63 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "make_it_yours", arguments: args } }), |
| 62 | 64 | }); |
| 63 | 65 | const j = (await res.json()) as any; |
| 64 | 66 | return j.result.content[0].text; |
| 65 | 67 | }; |
| 66 | 68 | /** 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)); | |
| 68 | 70 | |
| 69 | 71 | beforeAll(async () => { |
| 70 | 72 | dataDir = await mkdtemp(join(tmpdir(), "forge-explorer-yours-")); |
| @@ -78,6 +80,7 @@ describe("explorer: make it yours", () => { | ||
| 78 | 80 | } |
| 79 | 81 | bobCookie = `forge_session=${createSession(app.db, bob.id).token}`; |
| 80 | 82 | bobToken = createToken(app.db, bob.id, "rw", "rw").token; |
| 83 | bobRoToken = createToken(app.db, bob.id, "ro", "ro").token; | |
| 81 | 84 | }, 60_000); |
| 82 | 85 | |
| 83 | 86 | afterAll(async () => { |
| @@ -161,11 +164,20 @@ describe("explorer: make it yours", () => { | ||
| 161 | 164 | const made = await call({ repo: "alice/kiln", dryRun: false }); |
| 162 | 165 | expect(made.says).toContain("bob/kiln is yours"); |
| 163 | 166 | 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 }); | |
| 165 | 177 | expect(undone.says).toBe("bob/kiln is removed. The original was not touched."); |
| 166 | 178 | expect(getRepo(app.db, "bob", "kiln")).toBeUndefined(); |
| 167 | 179 | // 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( | |
| 169 | 181 | "not_found: nothing of yours was made from that app", |
| 170 | 182 | ); |
| 171 | 183 | }); |
| @@ -219,7 +231,7 @@ describe("explorer: make it yours", () => { | ||
| 219 | 231 | |
| 220 | 232 | test("the MCP undo answers `changed` for a copy with commits of its own, and keeps it", async () => { |
| 221 | 233 | // 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( | |
| 223 | 235 | "changed: bob/garden has changes of your own, so it is kept. Delete it in its settings if you mean to.", |
| 224 | 236 | ); |
| 225 | 237 | 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"; | ||
| 15 | 15 | import { newApp } from "../src/actions/apps.ts"; |
| 16 | 16 | import { deleteRepo, forkRepo } from "../src/actions/repos.ts"; |
| 17 | 17 | 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"; | |
| 19 | 19 | import { envDataDir } from "../src/apps/data.ts"; |
| 20 | 20 | import { getAppByRepo, listApps, type AppRecord } from "../src/core/apps.ts"; |
| 21 | 21 | import { getRepo, type Repo } from "../src/core/repos.ts"; |
| @@ -332,6 +332,32 @@ describe("make it yours", () => { | ||
| 332 | 332 | 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 }); |
| 333 | 333 | }); |
| 334 | 334 | |
| 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 | ||
| 335 | 361 | test("a copy starts private unless chosen public, and never public from a private original (#852)", async () => { |
| 336 | 362 | const frank = await createUser(app.db, { |
| 337 | 363 | 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", () => { | ||
| 60 | 60 | |
| 61 | 61 | test("a copy with nothing of its own is removed", async () => { |
| 62 | 62 | const { source, copy } = await copied("plain"); |
| 63 | const undone = await undoMakeItYours(app, bob, source); | |
| 63 | const undone = await undoMakeItYours(app, bob, source, { dryRun: false }); | |
| 64 | 64 | expect(undone.ok).toBe(true); |
| 65 | 65 | expect(getRepo(app.db, "bob", copy.name)).toBeUndefined(); |
| 66 | 66 | }); |
| @@ -97,7 +97,7 @@ describe("undo make it yours keeps what a person made", () => { | ||
| 97 | 97 | const ownerDir = dirname(repoDir(app.reposRoot, copy.ownerName, copy.name)); |
| 98 | 98 | await chmod(ownerDir, 0o555); |
| 99 | 99 | try { |
| 100 | const undone = await undoMakeItYours(app, bob, source); | |
| 100 | const undone = await undoMakeItYours(app, bob, source, { dryRun: false }); | |
| 101 | 101 | expect(!undone.ok && undone.error.kind).toBe("failed"); |
| 102 | 102 | } finally { |
| 103 | 103 | await chmod(ownerDir, 0o755); |
new above
offline