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

MCP `issue`: read one issue with its body, comments, labels and what it waits for #3352

Parked opened this
Closed without merging

scout/forge:scout/3342-mcp-issue-read-one-issue-with-its-body-c → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +142 −2
src/http/mcp.ts +80 −1
@@ -97,7 +97,15 @@ import {
9797 import { share } from "../feed/share.ts";
9898 import { parseTargets } from "./feed.ts";
9999 import { appsRuntime, envsFor } from "../apps/runtime.ts";
100 import { getIssue, getIssueById, listOpenAgentPullsVisibleTo } from "../core/issues.ts";
100 import {
101 getIssue,
102 getIssueById,
103 labelsFor,
104 listComments,
105 listOpenAgentPullsVisibleTo,
106 } from "../core/issues.ts";
107 import { isCrew } from "../core/agents.ts";
108 import { dependenciesOf } from "../core/dependencies.ts";
101109 import { waitsForPerson } from "../crew/review.ts";
102110 import { attachLook } from "../actions/attach_look.ts";
103111 import { lookUrl } from "../core/looks.ts";
@@ -129,6 +137,7 @@ import { audit } from "../contract/audit.ts";
129137 import { probeApp } from "../contract/probes.ts";
130138 import { convergeFork, convergeState } from "../crew/converge.ts";
131139 import {
140 markReaders,
132141 DIRECTION_NOTE,
133142 directionNote,
134143 OUTSIDERS_NOTE,
@@ -616,6 +625,76 @@ const TOOLS: readonly Tool[] = [
616625 : bad("refused", "that could not be said here");
617626 },
618627 },
628 {
629 name: "issue",
630 description:
631 "Read one issue: title, state, closeReason, body, labels, assignee, its comments (author, whether the author is an agent, body, time), and `blockedBy` and `blocks` (what it waits for, and what waits for it). Text written by someone who cannot write the repository carries `untrusted: true`, as in GET /api/v1/repos/:owner/:name/issues/:number (shown to a crew agent only). A sealed or private issue you cannot see is not found.",
632 inputSchema: {
633 type: "object",
634 properties: {
635 repo: REPO_ARG,
636 number: { type: "integer", minimum: 1 },
637 },
638 required: ["repo", "number"],
639 },
640 writes: false,
641 async run(ctx, user, args) {
642 const repo = repoArg(ctx, user, args);
643 if (isAnswer(repo)) return repo;
644 const n = args["number"];
645 const issue =
646 typeof n === "number" && Number.isInteger(n) && n > 0
647 ? getIssue(ctx.app.db, repo.id, n)
648 : undefined;
649 if (
650 issue === undefined ||
651 issue.kind !== "issue" ||
652 !mayView(ctx.app.db, repo, issue, user)
653 )
654 return bad("not_found", `no issue #${String(n)}`);
655 const db = ctx.app.db;
656 // Marked only for a crew agent, as the API does: to anyone else the
657 // mark would say which commenters cannot write.
658 const marking = user.kind === "agent" && isCrew(db, repo.id, user.id);
659 const mark = <T extends { authorName: string; originAuthor: string | null }>(
660 items: readonly T[],
661 ) => (marking ? markReaders(db, repo, items) : [...items]);
662 const comments = mark(listComments(db, issue.id));
663 const authors = getUsersByIds(db, [...new Set(comments.map((c) => c.authorId))]);
664 const deps = dependenciesOf(db, issue.id);
665 const brief = (i: typeof issue) => ({
666 number: i.number,
667 title: i.title,
668 state: i.state,
669 ...(i.closeReason === null ? {} : { closeReason: i.closeReason }),
670 });
671 const seen = (i: typeof issue) => mayView(db, repo, i, user);
672 const flag = (x: { untrusted?: string }) =>
673 x.untrusted === undefined ? {} : { untrusted: true };
674 const [self] = mark([issue]);
675 return good({
676 number: issue.number,
677 title: issue.title,
678 state: issue.state,
679 closeReason: issue.closeReason,
680 body: issue.body,
681 author: issue.authorName,
682 authorIsAgent: issue.authorIsAgent,
683 ...flag(self!),
684 labels: labelsFor(db, issue.id).map((l) => l.name),
685 assignee: issue.assigneeName,
686 comments: comments.map((c) => ({
687 author: c.authorName,
688 authorIsAgent: authors.get(c.authorId)?.kind === "agent",
689 body: c.body,
690 at: c.createdAt,
691 ...flag(c),
692 })),
693 blockedBy: deps.blockedBy.filter(seen).map(brief),
694 blocks: deps.blocks.filter(seen).map(brief),
695 });
696 },
697 },
619698 {
620699 name: "pull",
621700 description:
test/mcp.test.ts +62 −1
@@ -15,7 +15,8 @@ import { resolveOid } from "../src/git/merge.ts";
1515 import { openPull } from "../src/actions/pulls.ts";
1616 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
1717 import { createApp as insertApp } from "../src/core/apps.ts";
18 import { listIssues } from "../src/core/issues.ts";
18 import { addComment, createIssue, listIssues } from "../src/core/issues.ts";
19 import { addDependency } from "../src/core/dependencies.ts";
1920 import { getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts";
2021 import { createRun, listRuns, setRunBase } from "../src/core/runs.ts";
2122 import { getAccountCeiling } from "../src/core/account_ceiling.ts";
@@ -171,6 +172,7 @@ describe("the forge over MCP", () => {
171172 "tell",
172173 "share",
173174 "say",
175 "issue",
174176 "pull",
175177 "variant",
176178 "edit",
@@ -441,6 +443,65 @@ describe("the forge over MCP", () => {
441443 ).toContain("not_found");
442444 });
443445
446 test("issue reads body, comments, labels and what it waits for; a sealed one is not found; a reader is marked untrusted", async () => {
447 const mk = (title: string, body = "", sealed = false) => {
448 const r = createIssue(app.db, { repoId: game.id, authorId: alice.id, title, body, sealed });
449 if (!r.ok) throw new Error(String(r.error));
450 return r.value;
451 };
452 const waiting = mk("wait for it", "the body");
453 const first = mk("first");
454 expect(addDependency(app.db, waiting, first, alice.id).ok).toBe(true);
455 const bot = await createAgent(app, alice, { username: "issuebot", brief: "x" });
456 if (!bot.ok) throw new Error(String(bot.error));
457 expect(addToCrew(app, alice, game, "issuebot").ok).toBe(true);
458 addComment(app.db, { issueId: waiting.id, authorId: alice.id, body: "one" });
459 addComment(app.db, { issueId: waiting.id, authorId: bot.value.user.id, body: "two" });
460
461 const read = (await tool(rw, "issue", { repo: "alice/game", number: waiting.number })).value;
462 expect(read).toMatchObject({
463 number: waiting.number,
464 title: "wait for it",
465 state: "open",
466 closeReason: null,
467 body: "the body",
468 labels: [],
469 assignee: null,
470 blockedBy: [{ number: first.number, title: "first", state: "open" }],
471 blocks: [],
472 });
473 expect(read.comments.map((c: any) => [c.author, c.authorIsAgent, c.body])).toEqual([
474 ["alice", false, "one"],
475 ["issuebot", true, "two"],
476 ]);
477 expect(read.comments[0].at).toBeString();
478 expect(read.untrusted).toBeUndefined();
479 const other = (await tool(rw, "issue", { repo: "alice/game", number: first.number })).value;
480 expect(other.blocks.map((b: any) => b.number)).toEqual([waiting.number]);
481
482 // Not found: no such number, a pull's number is not an issue's.
483 expect((await tool(rw, "issue", { repo: "alice/game", number: 9999 })).error).toContain("not_found");
484
485 // Sealed: the writer reads it; a reader of a public repo is told not found.
486 const pub = await createRepo(app, alice, { name: "sealedpub", visibility: "public" });
487 if (!pub.ok) throw new Error(String(pub.error));
488 const secret = createIssue(app.db, { repoId: pub.value.id, authorId: alice.id, title: "hole", sealed: true });
489 if (!secret.ok) throw new Error(String(secret.error));
490 const open = createIssue(app.db, { repoId: pub.value.id, authorId: alice.id, title: "visible" });
491 if (!open.ok) throw new Error(String(open.error));
492 expect((await tool(rw, "issue", { repo: "alice/sealedpub", number: secret.value.number })).value.title).toBe("hole");
493 expect((await tool(bobRw, "issue", { repo: "alice/sealedpub", number: secret.value.number })).error).toContain("not_found");
494
495 // Untrusted: bob cannot write alice/sealedpub; a crew agent is told so.
496 addComment(app.db, { issueId: open.value.id, authorId: bob.id, body: "from a reader" });
497 addComment(app.db, { issueId: open.value.id, authorId: alice.id, body: "from the owner" });
498 expect(addToCrew(app, alice, pub.value, "issuebot").ok).toBe(true);
499 const botToken = createToken(app.db, bot.value.user.id, "bot", "ro").token;
500 const marked = (await tool(botToken, "issue", { repo: "alice/sealedpub", number: open.value.number })).value;
501 expect(marked.comments.map((c: any) => c.untrusted)).toEqual([true, undefined]);
502 expect(marked.untrusted).toBeUndefined();
503 });
504
444505 test("following, take_updates and converge are the fork's page", async () => {
445506 const pub = await createRepo(app, alice, {
446507 name: "open",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs