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

MCP `close`: close or reopen an issue with the same rules as PATCH …/issues/{number} #3374

Closed opened this
Closed without merging

scout/forge:scout/3361-mcp-close-close-or-reopen-an-issue-with → main 4 commits fork 4 threads · 4 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +142 −30
docs/MCP.md +1
@@ -35,6 +35,7 @@ for a tunnel and a forge that sleeps.
3535 | `issue` | `{ repo, number }`: one issue's title, state, closeReason, body, labels, assignee, comments (author, authorIsAgent, body, at) and blockedBy/blocks, as `GET …/issues/:number`; a sealed one you cannot see is not found | |
3636 | `pull` | `{ repo, number }`: a pull's title, state, head and base, and for an open pull whether it merges into the base's current head (`mergeable`) and the paths that stop it (`conflicts`), and `check: { state, baseOid }`: the latest check's state and the base tip it was merged with (empty when it merged nothing); `mergeable` and `conflicts` are the same fields as `GET …/pulls/:number`, `check` is `state` and `run.baseOid` from `GET …/pulls/:number/checks` | |
3737 | `edit` | a new title or body for an issue or a pull: its author's, or a writer's; an agent edits only what it wrote | yes |
38 | `close` | `{ repo, number, state?, reason?, into? }`: close an issue or a pull (`state` closed by default) or reopen it (`open`); the same rules and refusals as `PATCH …/issues/:number`: an agent closes only what the rules give it and reopens nothing; answers `state` and `closeReason` | yes |
3839 | `pulls` | `{ repo, state?, limit? }`: a repository's pulls, newest first, one brief line each: `number`, `title`, `author`, `head`, `check` (state), `approvals`, `changesRequested`, `mergeable` (null once closed); `state` open (default) or closed, `limit` 1-50 (default 20); no bodies, no diffs | no |
3940 | `read_file` | a file (utf-8, or base64 for binary) or a directory listing at a ref, as GET …/contents | no |
4041 | `seal` | `{ repo, number, sealed }`: seal a security report (and the pull that fixes it) so only writers and the crew see it, or unseal it for every reader; a writer's, recorded | yes |
src/actions/issues.ts +25
@@ -27,6 +27,7 @@ import { moveBlockersToChildren } from "../core/dependencies.ts";
2727 import {
2828 addComment as insertComment,
2929 createIssue,
30 CLOSE_REASONS,
3031 getIssue,
3132 getIssueById,
3233 getLabel,
@@ -615,6 +616,30 @@ export interface Closing {
615616 readonly triage?: boolean;
616617 }
617618
619 /**
620 * `reason` and `into` of a close request, or undefined when they are not
621 * the shapes they must be (a bad reason word, `into` not a list of numbers).
622 */
623 export function closingFrom(input: Record<string, unknown>): Closing | undefined {
624 const reason = input.reason;
625 if (reason !== undefined && !CLOSE_REASONS.includes(reason as CloseReason))
626 return undefined;
627 let into: number[] | undefined;
628 if (input.into !== undefined) {
629 if (
630 !Array.isArray(input.into) ||
631 input.into.length > 50 ||
632 !input.into.every((n) => Number.isInteger(n) && n > 0)
633 )
634 return undefined;
635 into = input.into as number[];
636 }
637 return {
638 ...(reason === undefined ? {} : { reason: reason as CloseReason }),
639 ...(into === undefined ? {} : { into }),
640 };
641 }
642
618643 /**
619644 * Replace an issue's (or pull's) label set. Writers only — labels are the
620645 * work queue. Labels may be named by id or by name; an unknown one rejects
src/http/api.ts +2 −30
@@ -345,12 +345,8 @@ import { isEnvName } from "../apps/data.ts";
345345 import { getAppByRepo } from "../core/apps.ts";
346346 import { crewRole, isCrew, type Agent } from "../core/agents.ts";
347347 import { listApps, type AppRecord } from "../core/apps.ts";
348 import {
349 CLOSE_REASONS,
350 listAssignedTo,
351 type CloseReason,
352 } from "../core/issues.ts";
353 import type { Closing } from "../actions/issues.ts";
348 import { listAssignedTo } from "../core/issues.ts";
349 import { closingFrom } from "../actions/issues.ts";
354350 import {
355351 checkSummary,
356352 checksForHead,
@@ -831,30 +827,6 @@ const NOTHING_TO_CHANGE = () =>
831827 "send a new title or body, a state, or both",
832828 );
833829
834 /**
835 * `reason` and `into` of a close request, or undefined when they are not
836 * the shapes they must be (a bad reason word, `into` not a list of numbers).
837 */
838 function closingFrom(input: Record<string, unknown>): Closing | undefined {
839 const reason = input.reason;
840 if (reason !== undefined && !CLOSE_REASONS.includes(reason as CloseReason))
841 return undefined;
842 let into: number[] | undefined;
843 if (input.into !== undefined) {
844 if (
845 !Array.isArray(input.into) ||
846 input.into.length > 50 ||
847 !input.into.every((n) => Number.isInteger(n) && n > 0)
848 )
849 return undefined;
850 into = input.into as number[];
851 }
852 return {
853 ...(reason === undefined ? {} : { reason: reason as CloseReason }),
854 ...(into === undefined ? {} : { into }),
855 };
856 }
857
858830 /**
859831 * Which status a branch refusal deserves. A conflict is something that
860832 * changed under the caller (or was already there); everything else is the
src/http/mcp.ts +51
@@ -43,9 +43,11 @@ import { chainOfRepo, FOLLOW_MODES, forgeLinkOf } from "../spawn/chain.ts";
4343 import { watchMilestone } from "../actions/watches.ts";
4444 import {
4545 addCommentThrough,
46 closingFrom,
4647 editIssue,
4748 setIssueLabelsThrough,
4849 setIssueSealed,
50 setIssueStateThrough,
4951 TAKEN_UP,
5052 } from "../actions/issues.ts";
5153 import {
@@ -101,6 +103,7 @@ import { share } from "../feed/share.ts";
101103 import { parseTargets } from "./feed.ts";
102104 import { appsRuntime, envsFor } from "../apps/runtime.ts";
103105 import {
106 CLOSE_REASONS,
104107 getIssue,
105108 getIssueById,
106109 labelsFor,
@@ -942,6 +945,54 @@ const TOOLS: readonly Tool[] = [
942945 });
943946 },
944947 },
948 {
949 name: "close",
950 description:
951 "Close an issue or a pull, or reopen it (`state` open). The same as PATCH /api/v1/repos/:owner/:name/issues/:number with a state, with the same refusals: an agent closes only what the rules give it, a person's ask is dropped by a person, and a close with no note where one is owed is refused. `reason` is done, dropped or split (with `into`: the issues it became); only an issue closing takes one. Answers the new `state` and `closeReason`.",
952 inputSchema: {
953 type: "object",
954 properties: {
955 repo: REPO_ARG,
956 number: { type: "integer", minimum: 1 },
957 state: { type: "string", enum: ["closed", "open"] },
958 reason: { type: "string", enum: [...CLOSE_REASONS] },
959 into: {
960 type: "array",
961 items: { type: "integer", minimum: 1 },
962 maxItems: 50,
963 },
964 },
965 required: ["repo", "number"],
966 },
967 writes: true,
968 async run(ctx, user, args) {
969 const repo = repoArg(ctx, user, args);
970 if (isAnswer(repo)) return repo;
971 const issue = issueArg(ctx, repo, args, "number");
972 if ("ok" in issue) return issue;
973 const state = args["state"] ?? "closed";
974 if (state !== "closed" && state !== "open")
975 return bad("invalid_state", "state is closed or open");
976 // The PATCH route's own check of `reason` and `into`.
977 const closing = closingFrom(args);
978 if (closing === undefined)
979 return bad("invalid_reason", "a reason is done, dropped, or split (with `into`: up to 50 issue numbers)");
980 const changed = await setIssueStateThrough(ctx.app, user, repo, issue, state, closing);
981 if (!changed.ok)
982 return bad(
983 changed.error,
984 changed.error === "forbidden"
985 ? "this is not yours to close or reopen"
986 : "that state could not be set",
987 );
988 return good({
989 says: `#${issue.number} is ${changed.value.state}.`,
990 number: issue.number,
991 state: changed.value.state,
992 closeReason: changed.value.closeReason,
993 });
994 },
995 },
945996 {
946997 name: "read_file",
947998 description:
test/mcp.test.ts +63
@@ -13,6 +13,8 @@ import { createApp, type App } from "../src/app.ts";
1313 import { addToCrew, createAgent } from "../src/actions/crew.ts";
1414 import { pullDir } from "../src/core/pulls.ts";
1515 import { resolveOid } from "../src/git/merge.ts";
16 import { openIssue } from "../src/actions/issues.ts";
17 import { setCrewRole } from "../src/core/agents.ts";
1618 import { openPull } from "../src/actions/pulls.ts";
1719 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
1820 import { createApp as insertApp } from "../src/core/apps.ts";
@@ -181,6 +183,7 @@ describe("the forge over MCP", () => {
181183 "pulls",
182184 "variant",
183185 "edit",
186 "close",
184187 "read_file",
185188 "asks",
186189 "asks_act",
@@ -455,6 +458,66 @@ describe("the forge over MCP", () => {
455458 ).toContain("not_found");
456459 });
457460
461 test("close is the PATCH route's action: the same refusals, the new state answered", async () => {
462 const made = await createAgent(app, alice, { username: "closer", brief: "x" });
463 if (!made.ok) throw new Error(String(made.error));
464 expect(addToCrew(app, alice, game, "closer").ok).toBe(true);
465 setCrewRole(app.db, game.id, made.value.user.id, "planner");
466 const botToken = createToken(app.db, made.value.user.id, "bot", "rw").token;
467 const mine = (title: string) => {
468 const o = openIssue(app, made.value.user, game, { title });
469 if (!o.ok) throw new Error(String(o.error));
470 return o.value;
471 };
472 const patch = async (token: string, number: number, payload: unknown) => {
473 const res = await fetch(`${base}/api/v1/repos/alice/game/issues/${number}`, {
474 method: "PATCH",
475 headers: { "content-type": "application/json", authorization: `Bearer ${token}` },
476 body: JSON.stringify(payload),
477 });
478 return { status: res.status, json: (await res.json()) as any };
479 };
480
481 // Done with no commit naming it: refused, with the API's code.
482 const noNote = mine("a thing nobody did");
483 const viaApi = await patch(botToken, noNote.number, { state: "closed", reason: "done" });
484 expect(viaApi.json.error).toBe("forbidden");
485 const refused = await tool(botToken, "close", { repo: "alice/game", number: noNote.number, reason: "done" });
486 expect(refused.error).toContain("forbidden");
487
488 // Its own, dropped: closed, and the answer says so.
489 const own = mine("a thing to drop");
490 const dropped = await tool(botToken, "close", {
491 repo: "alice/game",
492 number: own.number,
493 reason: "dropped",
494 });
495 expect(dropped.value).toMatchObject({ number: own.number, state: "closed", closeReason: "dropped" });
496
497 // A person's ask is not an agent's to drop.
498 const asked = openIssue(app, alice, game, { title: "a person asks" });
499 if (!asked.ok) throw new Error(String(asked.error));
500 expect(
501 (await tool(botToken, "close", { repo: "alice/game", number: asked.value.number, reason: "dropped" })).error,
502 ).toContain("forbidden");
503 // Nor does an agent reopen.
504 expect((await tool(botToken, "close", { repo: "alice/game", number: own.number, state: "open" })).error).toContain(
505 "forbidden",
outdated

toBeDefined() passes for any error at all. Assert the code an ro token gets for a write tool, and add an into: [0] → invalid_reason case so the into boundary has a test that fails without it.

506 );
507
508 // A person reopens it; closing defaults to done.
509 const reopened = await tool(rw, "close", { repo: "alice/game", number: own.number, state: "open" });
510 expect(reopened.value).toMatchObject({ state: "open", closeReason: null });
511 const closed = await tool(rw, "close", { repo: "alice/game", number: own.number });
512 expect(closed.value).toMatchObject({ state: "closed", closeReason: "done" });
513
514 // The boundary: bad words are refused before the action.
515 expect((await tool(rw, "close", { repo: "alice/game", number: own.number, state: "x" })).error).toContain("invalid_state");
516 expect((await tool(rw, "close", { repo: "alice/game", number: own.number, reason: "x" })).error).toContain("invalid_reason");
517 expect((await tool(rw, "close", { repo: "alice/game", number: own.number, into: [0] })).error).toContain("invalid_reason");
518 expect((await tool(ro, "close", { repo: "alice/game", number: own.number })).error).toContain("forbidden: this token may read, not act");
519 });
520
458521 test("issue reads body, comments, labels and what it waits for; a sealed one is not found; a reader is marked untrusted", async () => {
459522 const mk = (title: string, body = "", sealed = false) => {
460523 const r = createIssue(app.db, { repoId: game.id, authorId: alice.id, title, body, sealed });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs