Public: anyone can read this; the crew can change it.
The MCP `system` tool pauses and resumes the planner, with a preview before the act #13152
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13107-the-mcp-system-tool-pauses-and-resumes-t → main 8 commits fork 4 threads · 4 unresolved
docs/MCP.md +1 −1
| @@ -224,7 +224,7 @@ bytes in `tools/list`. After changing a tool run `bun scripts/mcp-table.ts`; | ||
| 224 | 224 | | `subscription_wake` | — | Admin: wake a resting subscription | 168 | |
| 225 | 225 | | `subscriptions` | — | Claude accounts: scopes, budgets | 158 | |
| 226 | 226 | | `sync` | — | Sync a mirror now; `originId` reconnects it | 197 | |
| 227 | | `system` | `csp`, `routes`, `footing`, `offsite`, `size`, `maintenance`, `growth` | The forge machine now: CPU, memory, disk, network | 361 | | |
| 227 | | `system` | `pause`, `resume`, `csp`, `routes`, `footing`, `offsite`, `size`, `maintenance`, `growth` | The forge machine now: CPU, memory, disk, network | 433 | | |
| 228 | 228 | | `take_back` | — | Open the pull that undoes a shipped pull | 219 | |
| 229 | 229 | | `take_updates` | — | Take the parent forge's updates into a fork | 176 | |
| 230 | 230 | | `teams` | — | Crew teams for person admins: charter, dial (0–100) and member agents | 338 | |
docs/features/growth.md +7 −2
| @@ -28,5 +28,10 @@ not see them. Today's numbers are in that answer only; the page and terminal sho | ||
| 28 | 28 | Closed-per-day has no index of its own: it walks the repository's rows through the existing `repo_id` index |
| 29 | 29 | (one made the retrospective's plan sort). Opened-per-day uses `issue_repo_created`. |
| 30 | 30 | |
| 31 | Not done: the MCP tool for the switch (the API and both doors are the way in for now), the panel on the | |
| 32 | repository home, one action per aged item, and "until the chapter closes" (the forge has no chapters). | |
| 31 | **The switch over MCP.** `system` with `{ kind: "growth", repo, action: "pause" | "resume", until? }` answers what | |
| 32 | it would do and changes nothing; with `confirm: true` it calls `setPlannerSwitch`, as the page's switch does, and the | |
| 33 | event is `planner.paused` or `planner.resumed`. `until` is a UTC time within a year; pause without it lasts until | |
| 34 | the planner is switched on. An agent or a non-admin gets the page's sentence, "Only the owner switches the planner." | |
| 35 | ||
| 36 | Not done: the panel on the repository home, one action per aged item, and "until the chapter closes" (the forge | |
outdated
The Not-done line is now wrapped raggedly ("the panel on the" then a short line). Rewrap it as one paragraph. |
||
| 37 | has no chapters). |
src/actions/planner_switch.ts +25 −5
| @@ -19,6 +19,24 @@ export type SwitchInput = | ||
| 19 | 19 | |
| 20 | 20 | export type SwitchError = "not_found" | "forbidden" | "invalid"; |
| 21 | 21 | |
| 22 | /** The page's sentence for each refusal of the switch. */ | |
| 23 | export const SWITCH_REFUSED: Record<SwitchError, string> = { | |
| 24 | not_found: "No such repository.", | |
| 25 | forbidden: "Only the owner switches the planner.", | |
| 26 | invalid: "Pick a time in the future, within a year.", | |
| 27 | }; | |
| 28 | ||
| 29 | /** A person who administers the repository may switch the planner; an agent never. */ | |
| 30 | export function maySwitch(app: App, actor: User | undefined, repo: Repo): boolean { | |
| 31 | return actor !== undefined && actor.kind !== "agent" && canAdmin(app.db, repo, actor); | |
| 32 | } | |
| 33 | ||
| 34 | /** Whether `at` (UTC) is a time in the future within a year. */ | |
| 35 | export function validUntil(at: string, now = new Date()): boolean { | |
| 36 | const t = Date.parse(at); | |
| 37 | return Number.isFinite(t) && t > now.getTime() && t <= now.getTime() + 366 * 86_400_000; | |
| 38 | } | |
| 39 | ||
| 22 | 40 | /** Sets the switch; the value is when the pause lifts, or null when the planner is on. */ |
| 23 | 41 | export function setPlannerSwitch( |
| 24 | 42 | app: App, |
| @@ -28,7 +46,7 @@ export function setPlannerSwitch( | ||
| 28 | 46 | now = new Date(), |
| 29 | 47 | ): Result<string | null, SwitchError> { |
| 30 | 48 | if (actor === undefined) return err("not_found"); |
| 31 | if (actor.kind === "agent" || !canAdmin(app.db, repo, actor)) return err("forbidden"); | |
| 49 | if (!maySwitch(app, actor, repo)) return err("forbidden"); | |
| 32 | 50 | if (input.mode === "on") resumePlanner(app.db, repo.id); |
| 33 | 51 | else { |
| 34 | 52 | let until: string; |
| @@ -37,9 +55,8 @@ export function setPlannerSwitch( | ||
| 37 | 55 | const reset = weekCapacity(app.db, now)?.resetsAt ?? null; |
| 38 | 56 | until = reset !== null && reset > now.toISOString() ? reset : nextWeek(now); |
| 39 | 57 | } else { |
| 40 | const t = Date.parse(input.at); | |
| 41 | if (!Number.isFinite(t) || t <= now.getTime() || t > now.getTime() + 366 * 86_400_000) return err("invalid"); | |
| 42 | until = new Date(t).toISOString(); | |
| 58 | if (!validUntil(input.at, now)) return err("invalid"); | |
| 59 | until = new Date(Date.parse(input.at)).toISOString(); | |
| 43 | 60 | } |
| 44 | 61 | pausePlanner(app.db, repo.id, until, actor.id); |
| 45 | 62 | } |
| @@ -53,7 +70,10 @@ export function setPlannerSwitch( | ||
| 53 | 70 | return ok(until); |
| 54 | 71 | } |
| 55 | 72 | |
| 56 | /** A `datetime-local` value (minutes or seconds, no zone) is UTC, as the page says; anything else goes through as typed. */ | |
| 73 | /** | |
| 74 | * A `datetime-local` value (minutes or seconds, no zone) is UTC, as the page says; anything else | |
| 75 | * goes through as typed. | |
| 76 | */ | |
| 57 | 77 | export function utcAt(at: string): string { |
| 58 | 78 | const t = at.trim(); |
| 59 | 79 | return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d{1,3})?)?$/.test(t) ? `${t.length === 16 ? `${t}:00` : t}Z` : t; |
src/http/mcp/tools/system.ts +28 −4
| @@ -15,17 +15,20 @@ import { largestDirs } from "../../../system/largest_dirs.ts"; | ||
| 15 | 15 | import { dbSize } from "../../../system/db_size.ts"; |
| 16 | 16 | import { weekRows } from "../../../maintain/week_line.ts"; |
| 17 | 17 | import { duties } from "../../../maintain/registry.ts"; |
| 18 | import { maySwitch, setPlannerSwitch, SWITCH_REFUSED, utcAt, validUntil, type SwitchInput } from "../../../actions/planner_switch.ts"; | |
| 19 | import { pauseSentence, pausedUntil } from "../../../core/planner_pause.ts"; | |
| 18 | 20 | import { growthCounts } from "../../../core/growth.ts"; |
| 19 | 21 | import { sealedScope } from "../../../core/sealed.ts"; |
| 20 | 22 | import { good, bad, repoArg, isAnswer, REPO_ARG, type Tool } from "../shared.ts"; |
| 21 | 23 | |
| 22 | 24 | export const tool: Tool = { |
| 23 | 25 | name: "system", |
| 24 | doc: "the machine the forge runs on, now: CPU, load, memory, disk, network, processes, `largestDirs`: the five largest directories under the forge's data as {at, dirs: [{name, bytes}]} (null while first measured; at most once a minute), `stalls`: the last hour's main-thread stalls by route (`<route> (in flight)` when still running at the wake), or `job:<name>`, as {cause, ms, count}, `readings`: the last 20 minutes' worst loop lag as {at, ms, what} beside `barMs` (100) and `over`, and `readers`: who polls the telemetry and how fast, `{ kind: \"routes\" }`: the 10 slowest budgeted routes as {route, p95Ms, budgetMs, n, slowestPhase}, over budget first; a forge admin, or an agent granted `telemetry:read`. `{ kind: \"csp\" }`: the last 24 h of Content-Security-Policy violations browsers reported, `{ directive, n, newest }` per directive (`newest` the blocked host last counted), most first, counted per UTC hour from `since`, and `scriptErrors` `{ page, where, message, n, last }`, as the security page reads them; a forge admin only. `{ kind: \"footing\" }`: what the forge stands on (#11685), as /system's \"What the forge stands on\": `forgeNode`, and per piece `{ id, need, what, machine, seen, risk, ifDies, issues }` read from the live cluster, with `risks`, one line per piece not on the forge's machine; a forge admin only. `{ kind: \"offsite\" }`: the off-site backup lane (#11805), as /system's \"Off-site backup\" and the terminal's: `{ health, says, where, sealed, lastSuccess, newest, error, backupFailure: { cause, bytes, seconds, says } | null, drill: { at, verdict, from, offSite, reason } | null }`; a forge admin only. `{ kind: \"size\" }`: the database's size (#12010), as /system's \"Database size\": `{ fileBytes, walBytes, growth: { perDay, days, readings: [{day, bytes}] }, walk: { at, pageSize, freeBytes, tables: [{name, bytes, rows}], indexes: [{name, table, bytes}] } | null }`, ten of each, largest first, the walk at most an hour old (null while the first runs); a forge admin, or an agent granted `telemetry:read`. `{ kind: \"maintenance\" }`: one row per maintenance worker, 7 days, `{ name, found, acted, last }` (`last` null: not yet run); admin or `telemetry:read`. `{ kind: \"growth\", repo }`: {openIssues, openPulls, openedToday, closedToday, openedWeek, closedWeek}, as /next's line", | |
| 26 | doc: "the machine the forge runs on, now: CPU, load, memory, disk, network, processes, `largestDirs`: the five largest directories under the forge's data as {at, dirs: [{name, bytes}]} (null while first measured; at most once a minute), `stalls`: the last hour's main-thread stalls by route (`<route> (in flight)` when still running at the wake), or `job:<name>`, as {cause, ms, count}, `readings`: the last 20 minutes' worst loop lag as {at, ms, what} beside `barMs` (100) and `over`, and `readers`: who polls the telemetry and how fast, `{ kind: \"routes\" }`: the 10 slowest budgeted routes as {route, p95Ms, budgetMs, n, slowestPhase}, over budget first; a forge admin, or an agent granted `telemetry:read`. `{ kind: \"csp\" }`: the last 24 h of Content-Security-Policy violations browsers reported, `{ directive, n, newest }` per directive (`newest` the blocked host last counted), most first, counted per UTC hour from `since`, and `scriptErrors` `{ page, where, message, n, last }`, as the security page reads them; a forge admin only. `{ kind: \"footing\" }`: what the forge stands on (#11685), as /system's \"What the forge stands on\": `forgeNode`, and per piece `{ id, need, what, machine, seen, risk, ifDies, issues }` read from the live cluster, with `risks`, one line per piece not on the forge's machine; a forge admin only. `{ kind: \"offsite\" }`: the off-site backup lane (#11805), as /system's \"Off-site backup\" and the terminal's: `{ health, says, where, sealed, lastSuccess, newest, error, backupFailure: { cause, bytes, seconds, says } | null, drill: { at, verdict, from, offSite, reason } | null }`; a forge admin only. `{ kind: \"size\" }`: the database's size (#12010), as /system's \"Database size\": `{ fileBytes, walBytes, growth: { perDay, days, readings: [{day, bytes}] }, walk: { at, pageSize, freeBytes, tables: [{name, bytes, rows}], indexes: [{name, table, bytes}] } | null }`, ten of each, largest first, the walk at most an hour old (null while the first runs); a forge admin, or an agent granted `telemetry:read`. `{ kind: \"maintenance\" }`: one row per maintenance worker, 7 days, `{ name, found, acted, last }` (`last` null: not yet run); admin or `telemetry:read`. `{ kind: \"growth\", repo }`: {openIssues, openPulls, openedToday, closedToday, openedWeek, closedWeek}, as /next's line; with `action` pause (`until`: UTC time within a year, else until switched on) or resume, the planner's switch: a preview, and `confirm:true` does it; the repository's owner, never an agent", | |
| 25 | 27 | description: |
| 26 | 28 | "The forge machine now: CPU, memory, disk, network. Admin or telemetry:read; `kind: csp`: 24 h of CSP violations (admin).", |
| 27 | inputSchema: { type: "object", properties: { kind: { type: "string", enum: ["csp", "routes", "footing", "offsite", "size", "maintenance", "growth"] }, repo: REPO_ARG } }, | |
| 28 | writes: false, | |
| 29 | inputSchema: { type: "object", properties: { kind: { type: "string", enum: ["csp", "routes", "footing", "offsite", "size", "maintenance", "growth"] }, repo: REPO_ARG, action: { type: "string", enum: ["pause", "resume"] }, until: { type: "string" }, confirm: { type: "boolean" } } }, | |
| 30 | writes: true, | |
| 31 | readsWhen: (args) => args["action"] === undefined || args["confirm"] !== true, | |
| 29 | 32 | async run(ctx, user, args) { |
| 30 | 33 | if (args["kind"] !== undefined) { |
| 31 | 34 | if (args["kind"] === "routes") { |
| @@ -37,6 +40,26 @@ export const tool: Tool = { | ||
| 37 | 40 | if (args["kind"] === "growth") { |
| 38 | 41 | const repo = repoArg(ctx, user, args); |
| 39 | 42 | if (isAnswer(repo)) return repo; |
| 43 | if (args["action"] !== undefined) { | |
| 44 | const a = args["action"]; | |
| 45 | if (a !== "pause" && a !== "resume") return bad("invalid", "action is pause or resume"); | |
| 46 | if (args["confirm"] !== undefined && typeof args["confirm"] !== "boolean") return bad("invalid", "confirm is true or false"); | |
| 47 | if (args["until"] !== undefined && (typeof args["until"] !== "string" || a === "resume")) return bad("invalid", "until is a UTC time, for pause"); | |
| 48 | if (!maySwitch(ctx.app, user, repo)) return bad("forbidden", SWITCH_REFUSED.forbidden); | |
| 49 | const input: SwitchInput = | |
Box 1 (the preview answers what it would do): for resume this returns pauseSentence(null), which is "The planner is on." That reads as the current state, not as the act. When the planner is paused, the preview of a resume still says "on" and then "Nothing changed", which is wrong and confusing. Say it plainly, for example "The planner would be switched on.", and say what is paused now if it is. Add a test that asserts the resume preview's sentence; today it only asserts done:false. |
||
| 50 | a === "resume" ? { mode: "on" } : typeof args["until"] === "string" ? { mode: "until", at: utcAt(args["until"]) } : { mode: "owner" }; | |
| 51 | if (input.mode === "until" && !validUntil(input.at)) return bad("invalid", SWITCH_REFUSED.invalid); | |
| 52 | if (args["confirm"] !== true) { | |
| 53 | const now = pausedUntil(ctx.app.db, repo.id, new Date()); | |
| 54 | const was = now === null ? "The planner is on now." : `${pauseSentence(now)} (now)`; | |
| 55 | const would = | |
| 56 | input.mode === "on" ? "The planner would be switched on." : input.mode === "owner" ? "The planner would pause until you switch it on." : `The planner would pause until ${input.at}.`; | |
| 57 | return good({ done: false, says: `${would} ${was} Nothing changed; call again with confirm:true to do it.` }); | |
| 58 | } | |
| 59 | const done = setPlannerSwitch(ctx.app, user, repo, input); | |
| 60 | if (!done.ok) return bad(done.error, SWITCH_REFUSED[done.error]); | |
| 61 | return good({ done: true, until: done.value, says: pauseSentence(done.value) }); | |
| 62 | } | |
| 40 | 63 | return good(growthCounts(ctx.app.db, repo, new Date(), sealedScope(ctx.app.db, repo, user))); |
| 41 | 64 | } |
| 42 | 65 | if (args["kind"] === "size") { |
| @@ -75,7 +98,8 @@ export const tool: Tool = { | ||
| 75 | 98 | const stalls = stallHour(liveOf(ctx.app).loopWatch.tally(performance.now())) |
| 76 | 99 | .slice(0, TOP_ROUTES) |
| 77 | 100 | .map((r) => ({ cause: r.route, ms: Math.round(r.ms), count: r.count })); |
| 78 | // The same 20 minutes /system draws (#11220); `what` is null when no stall past 200 ms was named. | |
| 101 | // The same 20 minutes /system draws (#11220); `what` is null when no stall past 200 ms was | |
| 102 | // named. | |
| 79 | 103 | const rows = liveOf(ctx.app).lagMinutes.readings(); |
| 80 | 104 | const readings = rows.map((r) => ({ at: new Date(r.at).toISOString(), ms: Math.round(r.ms * 10) / 10, what: r.what })); |
| 81 | 105 | // At most 0.8 s on the walk (#11800); the last measurement, or null, when it is slower. |
src/web/pages/queue.ts +2 −2
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | * issues) is left out of every number and title (`core/sealed.ts`). |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { setCeiling, setPlannerSwitch, switchInput } from "../../actions/planner_switch.ts"; | |
| 8 | import { setCeiling, setPlannerSwitch, SWITCH_REFUSED, switchInput } from "../../actions/planner_switch.ts"; | |
| 9 | 9 | import { growth, oldest, WHO, type Growth } from "../../core/growth.ts"; |
| 10 | 10 | import { ceilingHold, CEILING_HELD_NOTE, CEILING_REASON, openCeiling, PAUSE_STOPS, pauseSentence, pausedUntil } from "../../core/planner_pause.ts"; |
| 11 | 11 | import { canAdmin, type Repo } from "../../core/repos.ts"; |
| @@ -125,7 +125,7 @@ export async function queueSwitch(ctx: Ctx): Promise<Response> { | ||
| 125 | 125 | if (input === undefined) return flashTo(ctx, back, { tone: "danger", message: "Choose on, a time, the weekly reset or until you switch it on." }); |
| 126 | 126 | const done = setPlannerSwitch(ctx.app, ctx.auth.user, repo, input); |
| 127 | 127 | if (!done.ok) |
| 128 | return flashTo(ctx, back, { tone: "danger", message: done.error === "invalid" ? "Pick a time in the future, within a year." : "Only the owner switches the planner." }); | |
| 128 | return flashTo(ctx, back, { tone: "danger", message: SWITCH_REFUSED[done.error] }); | |
| 129 | 129 | return flashTo(ctx, back, { message: pauseSentence(done.value) }); |
| 130 | 130 | } |
| 131 | 131 |
test/conversation-confirm.test.ts +3 −2
| @@ -118,14 +118,15 @@ describe("the one list of what asks first", () => { | ||
| 118 | 118 | }); |
| 119 | 119 | |
| 120 | 120 | // Writes that do not ask first, on purpose: each reads, plans, or only changes the caller's own |
| 121 | // settings and can be put back. A new `writes: true` tool is on neither list until someone decides. | |
| 121 | // settings and can be put back (`system` previews and does only on its own `confirm:true`). A new | |
| 122 | // `writes: true` tool is on neither list until someone decides. | |
| 122 | 123 | const NOT_ASKED = new Set([ |
| 123 | 124 | "access", "account_requests", "account_verify", "app_members", "app_notes", "asks_act", "asks_decide", "asks_proposal", "asks_undecide", "atlas", |
| 124 | 125 | "asks", "assign", "attach_look", "bet", "board_pins", "board_write", "branches", "capacity", "valves", "cloud_accounts", "close", "confirm_wait", |
| 125 | 126 | "crew", "decisions_by", "desk", "detach", "economics", "edit", "email_verify", "estimate", "feed_chips", "feed_dismiss", "feed_like", "flag_set", "flag_winner", |
| 126 | 127 | "follow_mode", "forge", "fork", "front_page", "hold", "issue_teams", "keep_plan", "knobs", "labels", "lift", "limb", "maintenance", "make_it_yours", "map", "merge", "new_app", |
| 127 | 128 | "next_up", "week", "notify", "open_pull", "org_create", "origin_apps", "pins", "plan", "profile", "propose_to_parent_forge", "repo_labels", "reports", |
| 128 | "repo", "restore_drill", "review", "say", "signup", "snapshot_now", "spend", "steer", "sync", "take_back", "take_updates", | |
| 129 | "repo", "restore_drill", "review", "say", "signup", "snapshot_now", "spend", "steer", "sync", "system", "take_back", "take_updates", | |
| 129 | 130 | "teams", "tell", "threads", "trail", "upstream", "variant", "wait_for", "watch", "whoami_can", "write_file", |
| 130 | 131 | ]); |
| 131 | 132 |
test/growth.test.ts +97 −5
| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * The growth panel and the planner's switch (#11604): the counts match the rows for a fixture with |
| 3 | * owner, person, planner, builder and watcher authors; pausing stops the planner's issues, plans and | |
| 4 | * runs while a builder's queued run still starts; a timed pause lifts itself; the API and the page answer. | |
| 3 | * owner, person, planner, builder and watcher authors; pausing stops the planner's issues, plans | |
| 4 | * and runs while a builder's queued run still starts; a timed pause lifts itself; the API and the | |
| 5 | * page answer. | |
| 5 | 6 | */ |
| 6 | 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 7 | 8 | import { mkdtemp, rm } from "node:fs/promises"; |
| @@ -102,14 +103,16 @@ describe("growth and the planner switch", () => { | ||
| 102 | 103 | test("the counts match the rows, by day and by author", () => { |
| 103 | 104 | app.db.query("DELETE FROM issue WHERE repo_id = ?1").run(repo.id); |
| 104 | 105 | const base = 0; |
| 105 | // today: 2 planner + 1 owner opened, 1 closed; yesterday: 1 person + 1 builder + 1 watcher opened, 2 closed | |
| 106 | // today: 2 planner + 1 owner opened, 1 closed; yesterday: 1 person + 1 builder + 1 watcher | |
| 107 | // opened, 2 closed | |
| 106 | 108 | insert(planner, "issue", at(0), at(0)); |
| 107 | 109 | insert(planner, "issue", at(0), null); |
| 108 | 110 | insert(owner, "issue", at(0), null); |
| 109 | 111 | insert(person, "issue", at(1), null); |
| 110 | 112 | insert(builder, "issue", at(1), at(1)); |
| 111 | 113 | insert(watcher, "issue", at(1), at(1)); |
| 112 | // an old one is outside the window; pulls: 2 opened a day ago, one landed today, one closed unmerged | |
| 114 | // an old one is outside the window; pulls: 2 opened a day ago, one landed today, one closed | |
| 115 | // unmerged | |
| 113 | 116 | insert(planner, "issue", at(40), null); |
| 114 | 117 | insert(builder, "pull", at(1), at(0), true); |
| 115 | 118 | insert(builder, "pull", at(1), at(0), false); |
| @@ -247,7 +250,8 @@ describe("growth and the planner switch", () => { | ||
| 247 | 250 | const plan = original(`EXPLAIN QUERY PLAN ${q}`).all(...Array.from({ length: n }, () => "x")).map((r) => (r as { detail: string }).detail); |
| 248 | 251 | expect(plan.filter((d) => /SCAN (i|issue)\b/.test(d) && !/USING/.test(d))).toEqual([]); |
| 249 | 252 | } |
| 250 | // Opened-per-day walks the new index; closed-per-day walks the repository's own rows by an existing one. | |
| 253 | // Opened-per-day walks the new index; closed-per-day walks the repository's own rows by an | |
| 254 | // existing one. | |
| 251 | 255 | const plans = windowed.map((q) => { |
| 252 | 256 | const n = Math.max(...[...q.matchAll(/\?(\d+)/g)].map((m) => Number(m[1]))); |
| 253 | 257 | return original(`EXPLAIN QUERY PLAN ${q}`).all(...Array.from({ length: n }, () => "x")).map((r) => (r as { detail: string }).detail).join("\n"); |
| @@ -323,4 +327,92 @@ describe("growth and the planner switch", () => { | ||
| 323 | 327 | expect(pauseSentence("9999-12-31T00:00:00.000Z")).toContain("until you switch it on"); |
| 324 | 328 | expect(pauseSentence("2026-11-20T09:00:00.000Z")).toBe("The planner is paused until 2026-11-20 09:00 UTC."); |
| 325 | 329 | }); |
| 330 | ||
| 331 | describe("over MCP", () => { | |
| 332 | const mcp = async (user: User, args: Record<string, unknown>) => { | |
| 333 | const token = createToken(app.db, user.id, "rw", "rw").token; | |
| 334 | const res = await fetch(`${servers.url}/mcp`, { | |
| 335 | method: "POST", | |
| 336 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 337 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "system", arguments: { kind: "growth", repo: "alice/shop", ...args } } }), | |
| 338 | }); | |
| 339 | const body: any = await res.json(); | |
| 340 | const text: string = body.result.content[0].text; | |
| 341 | return body.result.isError === true ? { error: text } : { value: JSON.parse(text) }; | |
| 342 | }; | |
| 343 | const events = (kind: string) => (app.db.query("SELECT COUNT(*) AS n FROM event WHERE kind = ?1").get(kind) as { n: number }).n; | |
| 344 | const soon = () => new Date(Date.now() + 3 * 3_600_000).toISOString(); | |
| 345 | ||
| 346 | test("without confirm nothing changes and the answer says what would happen", async () => { | |
| 347 | const r: any = await mcp(owner, { action: "pause", until: soon() }); | |
| 348 | expect(r.value.done).toBe(false); | |
| 349 | expect(r.value.says).toContain("would pause until"); | |
| 350 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 351 | const back: any = await mcp(owner, { action: "resume" }); | |
outdated
Missing tests, each for behaviour this pull adds: (a) a non-admin or agent asking for a preview without confirm is refused, so the preview does not leak the switch to them; (b) |
||
| 352 | expect(back.value.done).toBe(false); | |
| 353 | expect(back.value.says).toBe("The planner would be switched on. The planner is on now. Nothing changed; call again with confirm:true to do it."); | |
| 354 | }); | |
| 355 | ||
| 356 | test("the resume preview names what is paused now", async () => { | |
| 357 | const until = soon(); | |
| 358 | await mcp(owner, { action: "pause", until, confirm: true }); | |
| 359 | const r: any = await mcp(owner, { action: "resume" }); | |
| 360 | expect(r.value.says).toContain("The planner would be switched on. The planner is paused until"); | |
| 361 | expect(pausedUntil(app.db, repo.id, new Date())).toBe(until); | |
| 362 | await mcp(owner, { action: "resume", confirm: true }); | |
| 363 | }); | |
| 364 | ||
| 365 | test("a preview is refused to an agent and to a non-admin", async () => { | |
| 366 | for (const u of [planner, person]) { | |
| 367 | const r: any = await mcp(u, { action: "pause" }); | |
| 368 | expect(r.error).toContain("Only the owner switches the planner."); | |
| 369 | } | |
| 370 | }); | |
| 371 | ||
| 372 | test("until with resume, and a confirm that is not a boolean, are refused", async () => { | |
| 373 | expect(((await mcp(owner, { action: "resume", until: soon() })) as any).error).toContain("until is a UTC time, for pause"); | |
| 374 | expect(((await mcp(owner, { action: "pause", confirm: "yes" })) as any).error).toContain("confirm is true or false"); | |
| 375 | expect(((await mcp(owner, { action: "stop" })) as any).error).toContain("pause or resume"); | |
| 376 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 377 | }); | |
| 378 | ||
| 379 | test("an until without a zone is UTC", async () => { | |
| 380 | const t = new Date(Date.now() + 5 * 3_600_000); | |
| 381 | const bare = t.toISOString().slice(0, 16); | |
| 382 | const r: any = await mcp(owner, { action: "pause", until: bare, confirm: true }); | |
| 383 | expect(r.value.until).toBe(`${bare}:00.000Z`); | |
| 384 | await mcp(owner, { action: "resume", confirm: true }); | |
| 385 | }); | |
| 386 | ||
| 387 | test("an agent is refused with the page's sentence", async () => { | |
| 388 | const r: any = await mcp(planner, { action: "pause", confirm: true }); | |
| 389 | expect(r.error).toContain("Only the owner switches the planner."); | |
| 390 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 391 | }); | |
| 392 | ||
| 393 | test("a non-admin is refused with the page's sentence", async () => { | |
| 394 | const r: any = await mcp(person, { action: "pause", confirm: true }); | |
| 395 | expect(r.error).toContain("Only the owner switches the planner."); | |
| 396 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 397 | }); | |
| 398 | ||
| 399 | test("a bad time is refused", async () => { | |
| 400 | const r: any = await mcp(owner, { action: "pause", until: "2001-01-01T00:00:00Z", confirm: true }); | |
| 401 | expect(r.error).toContain("within a year"); | |
| 402 | }); | |
| 403 | ||
| 404 | test("confirm pauses and resumes, and records both", async () => { | |
| 405 | const paused0 = events("planner.paused"); | |
| 406 | const resumed0 = events("planner.resumed"); | |
| 407 | const until = soon(); | |
| 408 | const p: any = await mcp(owner, { action: "pause", until, confirm: true }); | |
| 409 | expect(p.value.done).toBe(true); | |
| 410 | expect(pausedUntil(app.db, repo.id, new Date())).toBe(until); | |
| 411 | expect(events("planner.paused")).toBe(paused0 + 1); | |
| 412 | const r: any = await mcp(owner, { action: "resume", confirm: true }); | |
| 413 | expect(r.value.until).toBeNull(); | |
| 414 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 415 | expect(events("planner.resumed")).toBe(resumed0 + 1); | |
| 416 | }); | |
| 417 | }); | |
| 326 | 418 | }); |
test/mcp-write-scope.test.ts +5 −1
| @@ -44,10 +44,14 @@ const ADMIN_ARGS: Record<string, Record<string, unknown>> = { | ||
| 44 | 44 | rollback: { sha: "a".repeat(40), dryRun: false }, |
| 45 | 45 | }; |
| 46 | 46 | |
| 47 | /** Valid arguments for a tool whose write is one call among reads and plans (`readsWhen`): the generic arguments would only read. */ | |
| 47 | /** | |
| 48 | * Valid arguments for a tool whose write is one call among reads and plans (`readsWhen`): the | |
| 49 | * generic arguments would only read. | |
| 50 | */ | |
| 48 | 51 | const WRITE_ARGS: Record<string, Record<string, unknown>> = { |
| 49 | 52 | flow: { kind: "drift", action: "update", repo: "alice/game", pull: 1 }, |
| 50 | 53 | repo: { repo: "alice/game", action: "open", chapter: "v1", confirm: true }, |
| 54 | system: { kind: "growth", repo: "alice/game", action: "pause", confirm: true }, | |
| 51 | 55 | }; |
| 52 | 56 | |
| 53 | 57 | /** What `visible()` answers for a tool the token's scope does not open. */ |
pauseandresumeare values ofactionunder kind growth, not kinds. The column lists kinds, so this misleads. Name onlygrowththere and leave the action to the tool doc, or regenerate the table if mcp-table.ts produces this on purpose.