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

An MCP answer can be voice-sized: a `brief` argument returns one sentence under 280 characters #15785

Merged opened this

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

Merged

torque/forge:torque/15749-an-mcp-answer-can-be-voice-sized-a-brief → main 0 commits fork 4 threads · 4 unresolved

  • Merged. torque/15749-an-mcp-answer-can-be-voice-sized-a-brief was merged into main as 9b34006.

Review

Sign in to review this change.
7 files changed +147 −10
docs/MCP.md +5
@@ -242,6 +242,11 @@ longer sentences:
242242 - `hold.why`: why, in one line; it shows on the pull. `runs.limit`: how many, 1 to 50; 20 when absent.
243243 - `boards.step`: with `panel`, seconds per bucket, or a count of `s`, `m`, `h` or `d`. `access.since`: ISO time; default a day, max 14 days.
244244
245 ## A brief answer
outdated

Put the how-to in docs/features/crew-now.md and link it here.

246
247 `now` takes `brief: true` and answers one plain sentence under 280 characters, for a person
248 asking from a car. No new tool. How to use it: [docs/features/crew-now.md](features/crew-now.md).
249
245250 ## Ask for less: the default list and `find_tool` (#11957)
246251
247252 The MCP is the forge's control surface; repo work is git's (clone and push).
docs/features/crew-now.md +4
@@ -11,3 +11,7 @@ Only public repositories are read: the page is open to anyone holding the link,
1111 In the terminal, the palette's **Your crew's latest work** shows the same three runs in the same words.
1212
1313 Code: `src/core/crew_now.ts` (the read and the words), `src/web/crew_now.ts` (the section), `src/tui/screens/crew_now.ts` (the screen). Test: `test/link-crew-now.test.ts`.
14
15 ## A brief answer over MCP
16
17 Call the `now` tool with `brief: true`. It answers one plain sentence under 280 characters: what waits on you first (a review or a declined plan, named by its title, cut on a word), then how many runs are in flight, or "Nothing needs you and nothing is running." Anything but true or false for `brief` is refused. Without `brief`, `now` answers its rows as before, and its description still says who, on what, since when, cost. Code: `src/actions/now_brief.ts`. Test: `test/mcp-brief.test.ts`.
src/actions/now_brief.ts added +28
@@ -0,0 +1,28 @@
1 /** A brief answer is read aloud in a car: one plain sentence under this many characters. */
2 export const BRIEF_MAX = 280;
3
4 /** The fields of a `crewNow` row a brief reads. */
5 type Row = { run: string | null; app: string; on: string | null; why?: string; says?: string };
6
7 const plural = (n: number, one: string, many: string): string => `${n} ${n === 1 ? one : many}`;
8
9 /**
10 * The crew's state in one sentence: what waits on the person first (rows with no run, in the
11 * order `crewNow` gives them), the first of them named, then how many runs are shipping. The
12 * name is cut on a word so the whole sentence stays under BRIEF_MAX.
13 */
14 export function nowBrief(rows: readonly Row[]): string {
15 const waiting = rows.filter((r) => r.run === null);
16 const running = rows.length - waiting.length;
17 const tail = running === 0 ? "nothing is running" : `${plural(running, "run is", "runs are")} in flight`;
18 if (waiting.length === 0) return running === 0 ? "Nothing needs you and nothing is running." : `Nothing needs you and ${tail}.`;
19 const first = waiting[0]!;
20 const kind = first.why === "review" ? "review of" : "an answer on";
21 const more = waiting.length === 1 ? "" : ` and ${waiting.length - 1} more`;
22 const name = (first.on ?? first.app).replace(/\s+/g, " ").replace(/[.!?]+$/, "").trim();
23 const head = (n: string): string => `${plural(waiting.length, "thing needs", "things need")} you, first ${kind} ${n}${more}; ${tail}.`;
24 if (head(name).length <= BRIEF_MAX) return head(name);
25 const room = BRIEF_MAX - head("…").length;
26 const cut = name.slice(0, Math.max(1, room)).replace(/\s+\S*$/, "").trim();
27 return head(`${cut === "" ? name.slice(0, Math.max(1, room)) : cut}…`);
28 }
src/http/mcp/tools/now.ts +12 −6
@@ -1,14 +1,20 @@
11 import { crewNow } from "../../../actions/crew_now.ts";
2 import { good, type Tool } from "../shared.ts";
2 import { nowBrief } from "../../../actions/now_brief.ts";
3 import { bad, good, type Tool } from "../shared.ts";
34
45 export const tool: Tool = {
56 name: "now",
6 doc: "what the crew is doing right now: who, on what, since when, at what cost; a planner that declined the direction and waits for you comes first (`run: null`, its words in `says`); the `ui://forge/crew` card",
7 doc: "what the crew is doing right now: who, on what, since when, at what cost; a planner that declined the direction and waits for you comes first (`run: null`, its words in `says`); the `ui://forge/crew` card; `brief` true answers in one plain sentence under 280 characters: what needs you first, what is running",
78 description:
8 "What the crew does across your apps: who, on what, since when, cost; what awaits review.",
9 inputSchema: { type: "object", properties: {} },
9 "What the crew does now: who, on what, cost. brief: one sentence.",
outdated

Description lost 'cost' to pay for the tools/list budget. Restore the meaning or say why it went.

10 inputSchema: {
11 type: "object",
12 properties: { brief: { type: "boolean" } },
13 },
1014 writes: false,
11 run(ctx, user) {
12 return good(crewNow(ctx, user));
15 run(ctx, user, args) {
16 if (args["brief"] !== undefined && typeof args["brief"] !== "boolean") return bad("invalid_brief", "brief is true or false");
17 const rows = crewNow(ctx, user);
18 return good(args["brief"] === true ? nowBrief(rows) : rows);
1319 },
1420 };
test/helpers/mcp.ts +6
@@ -38,3 +38,9 @@ export function mcpSource(root: string): string {
3838 /** Whether `text` imports `module` (`actions/x.ts`) from any depth under src/. */
3939 export const importsFrom = (text: string, module: string): boolean =>
4040 new RegExp(`from "(\\.\\./)+${module.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}"`).test(text);
41
42 /** The text an MCP tool result carries: a JSON string is unquoted, anything else is as sent. */
43 export const resultText = (r: any): string => {
44 const t: string = r.structuredContent?.result ?? r.content[0].text;
45 return t.startsWith('"') ? (JSON.parse(t) as string) : t;
46 };
test/mcp-brief.test.ts added +90
@@ -0,0 +1,90 @@
1 /**
2 * `now` with `brief: true`: one plain sentence under 280 characters naming what waits on the
3 * person.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { createToken } from "../src/core/tokens.ts";
11 import { createUser, getUserByName } from "../src/core/users.ts";
12 import { serve, type Servers } from "../src/http/server.ts";
13 import { BRIEF_MAX, nowBrief } from "../src/actions/now_brief.ts";
14 import { quietLogs } from "./helpers/globals.ts";
15 import { resultText } from "./helpers/mcp.ts";
16 import { createRepo } from "../src/actions/repos.ts";
17 import { createApp as createAppRecord } from "../src/core/apps.ts";
18 import { createIssue } from "../src/core/issues.ts";
19 import { createRun } from "../src/core/runs.ts";
20
21 quietLogs();
22
23 let dataDir: string;
24 let app: App;
25 let servers: Servers;
26 let token: string;
27
28 beforeAll(async () => {
29 dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-brief-"));
30 app = createApp({ dataDir, port: 0 });
31 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
32 if (!made.ok) throw new Error(made.error);
33 token = createToken(app.db, getUserByName(app.db, "alice")!.id, "admin", "admin").token;
34 servers = await serve(app);
35 });
36
37 afterAll(async () => {
38 servers.stop();
39 app.close();
40 await rm(dataDir, { recursive: true, force: true });
41 });
42
43 const call = async (args: Record<string, unknown>): Promise<any> =>
44 ((await (await fetch(`${servers.url}/mcp`, {
outdated

Near copy of text in mcp-health-voice.test.ts: share one helper in test/helpers.

45 method: "POST",
46 headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` },
47 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "now", arguments: args } }),
48 })).json()) as any).result;
49 const said = resultText;
50
51 test("over MCP, brief is one sentence and a non-boolean is refused", async () => {
outdated

Only the calm forge goes through MCP. Add a real waiting row through the now tool and assert it is named and under 280.

52 const s = said(await call({ brief: true }));
53 expect(s).toBe("Nothing needs you and nothing is running.");
54 expect(await call({ brief: "yes" })).toMatchObject({ isError: true });
55 expect(Array.isArray(JSON.parse(said(await call({}))))).toBe(true);
56 });
57
58 test("a declined direction waiting on the person is named through the now tool, under the ceiling", async () => {
59 const alice = getUserByName(app.db, "alice")!;
60 const repo = await createRepo(app, alice, { name: "hello", visibility: "public" });
61 if (!repo.ok) throw new Error(String(repo.error));
62 createAppRecord(app.db, { repoId: repo.value.id, slug: "hello", access: "public" });
63 const issue = createIssue(app.db, { repoId: repo.value.id, authorId: alice.id, title: "make it rounder" });
64 if (!issue.ok) throw new Error("issue");
65 const run = createRun(app.db, { repoId: repo.value.id, kind: "agent", issueId: issue.value.id, reason: "plan" });
66 app.db.query("UPDATE run SET state = 'failed', summary = 'declined: too vague' WHERE id = ?1").run(run.id);
67 app.db.query("UPDATE repo SET autonomy = 'autonomous', direction_issue_id = ?2 WHERE id = ?1").run(repo.value.id, issue.value.id);
68 const rows = JSON.parse(said(await call({}))) as { why?: string }[];
69 expect(rows[0]?.why).toBe("declined");
70 const s = said(await call({ brief: true }));
71 expect(s).toContain("make it rounder");
72 expect(s.length).toBeLessThanOrEqual(BRIEF_MAX);
73 });
74
75 test("the first thing waiting on the person is named, and the sentence stays under the ceiling", () => {
76 const rows = [
77 { run: null, app: "a/b", on: "#7 Declined direction", why: "declined" },
78 { run: null, app: "a/b", on: "#9 Review me", why: "review" },
79 { run: "r1", app: "a/b", on: "#3 Busy" },
80 ];
81 const s = nowBrief(rows);
82 expect(s).toContain("#7 Declined direction");
83 expect(s).toContain("and 1 more");
84 expect(s).toContain("1 run is in flight");
85 expect(s.length).toBeLessThanOrEqual(BRIEF_MAX);
86 expect(s.match(/[.!?](\s|$)/g)!.length).toBe(1);
87 const long = nowBrief([{ run: null, app: "a/b", on: `#1 ${"word ".repeat(200)}`, why: "review" }]);
88 expect(long.length).toBeLessThanOrEqual(BRIEF_MAX);
89 expect(long).toContain("review of #1 word");
90 });
test/mcp-health-voice.test.ts +2 −4
@@ -11,6 +11,7 @@ import { readHealth } from "../src/actions/health.ts";
1111 import { spoken, voiceHealth, VOICE_MAX, type HealthAnswer } from "../src/actions/health_voice.ts";
1212 import { writeFileSync, mkdirSync } from "node:fs";
1313 import { quietLogs } from "./helpers/globals.ts";
14 import { resultText } from "./helpers/mcp.ts";
1415
1516 quietLogs();
1617
@@ -40,10 +41,7 @@ const post = (args: Record<string, unknown>, auth = true): Promise<Response> =>
4041 headers: { "content-type": "application/json", accept: "application/json, text/event-stream", ...(auth ? { authorization: `Bearer ${token}` } : {}) },
4142 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "health", arguments: args } }),
4243 });
43 const text = (r: any): string => {
44 const t = r.structuredContent?.result ?? r.content[0].text;
45 return t.startsWith('"') ? JSON.parse(t) : t;
46 };
44 const text = resultText;
4745 const call = async (args: Record<string, unknown>): Promise<any> => ((await (await post(args)).json()) as any).result;
4846
4947 const base = { ok: true, version: "1", commit: null, draining: false, rollout: { state: "ok", reason: "" }, incidents: [], stale: false, checkedAt: null } as const;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs