Public: anyone can read this; the crew can change it.
The planner can be paused until the open chapter closes #13286
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13108-the-planner-can-be-paused-until-the-open → main 5 commits fork 3 threads · 3 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` | `pause`, `resume`, `csp`, `routes`, `footing`, `offsite`, `size`, `maintenance`, `growth` | The forge machine now: CPU, memory, disk, network | 433 | | |
| 227 | | `system` | `pause`, `resume`, `csp`, `routes`, `footing`, `offsite`, `size`, `maintenance`, `growth` | The forge machine now: CPU, memory, disk, network | 462 | | |
| 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 +9 −2
| @@ -33,5 +33,12 @@ it would do and changes nothing; with `confirm: true` it calls `setPlannerSwitch | ||
| 33 | 33 | event is `planner.paused` or `planner.resumed`. `until` is a UTC time within a year; pause without it lasts until |
| 34 | 34 | the planner is switched on. An agent or a non-admin gets the page's sentence, "Only the owner switches the planner." |
| 35 | 35 | |
| 36 | Not done: the panel on the repository home, one action per aged item, and "until the chapter closes" (the forge | |
| 37 | has no chapters). | |
| 36 | **Until the chapter closes** (#13108). The pause has a fourth choice: the queue page's "until the open chapter | |
| 37 | closes", the terminal's `h` (`c` is the ceiling's) and the MCP `system` growth pause with `chapter: true`, all | |
| 38 | through `setPlannerSwitch` (`{ mode: "chapter" }`). With no open chapter it is refused with "There is no open chapter | |
| 39 | to wait for." and nothing changes. The pause is a row in `planner_pause` at `CHAPTER_UNTIL` plus the chapter's id in | |
| 40 | `forge_setting`; `pausedUntil` reads it as lifted once that chapter is not open, so shipping or closing lifts it | |
| 41 | without touching the close. Any other pause or a switch-on forgets the chapter. Pinned by | |
| 42 | `test/planner-pause-chapter.test.ts`. | |
| 43 | ||
| 44 | Not done: the panel on the repository home, and one action per aged item. |
src/actions/planner_switch.ts +9 −5
| @@ -8,22 +8,24 @@ import type { App } from "../app.ts"; | ||
| 8 | 8 | import { canAdmin, type Repo } from "../core/repos.ts"; |
| 9 | 9 | import type { User } from "../core/users.ts"; |
| 10 | 10 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 11 | import { FOREVER, liftCeilingOnce, nextWeek, parseCeiling, pausePlanner, pausedUntil, resumePlanner, setOpenCeiling } from "../core/planner_pause.ts"; | |
| 11 | import { FOREVER, liftCeilingOnce, nextWeek, pauseForChapter, parseCeiling, pausePlanner, pausedUntil, resumePlanner, setOpenCeiling } from "../core/planner_pause.ts"; | |
| 12 | 12 | import { weekCapacity } from "../flow/week.ts"; |
| 13 | 13 | |
| 14 | 14 | export type SwitchInput = |
| 15 | 15 | | { readonly mode: "on" } |
| 16 | 16 | | { readonly mode: "until"; readonly at: string } |
| 17 | 17 | | { readonly mode: "week" } |
| 18 | | { readonly mode: "owner" }; | |
| 18 | | { readonly mode: "owner" } | |
| 19 | | { readonly mode: "chapter" }; | |
| 19 | 20 | |
| 20 | export type SwitchError = "not_found" | "forbidden" | "invalid"; | |
| 21 | export type SwitchError = "not_found" | "forbidden" | "invalid" | "no_chapter"; | |
| 21 | 22 | |
| 22 | 23 | /** The page's sentence for each refusal of the switch. */ |
| 23 | 24 | export const SWITCH_REFUSED: Record<SwitchError, string> = { |
| 24 | 25 | not_found: "No such repository.", |
| 25 | 26 | forbidden: "Only the owner switches the planner.", |
| 26 | 27 | invalid: "Pick a time in the future, within a year.", |
| 28 | no_chapter: "There is no open chapter to wait for.", | |
| 27 | 29 | }; |
| 28 | 30 | |
| 29 | 31 | /** A person who administers the repository may switch the planner; an agent never. */ |
| @@ -48,7 +50,9 @@ export function setPlannerSwitch( | ||
| 48 | 50 | if (actor === undefined) return err("not_found"); |
| 49 | 51 | if (!maySwitch(app, actor, repo)) return err("forbidden"); |
| 50 | 52 | if (input.mode === "on") resumePlanner(app.db, repo.id); |
| 51 | else { | |
| 53 | else if (input.mode === "chapter") { | |
| 54 | if (!pauseForChapter(app.db, repo.id, actor.id)) return err("no_chapter"); | |
| 55 | } else { | |
| 52 | 56 | let until: string; |
| 53 | 57 | if (input.mode === "owner") until = FOREVER; |
| 54 | 58 | else if (input.mode === "week") { |
| @@ -81,7 +85,7 @@ export function utcAt(at: string): string { | ||
| 81 | 85 | |
| 82 | 86 | /** The switch's input from a form or the terminal's words; undefined when it is none of them. */ |
| 83 | 87 | export function switchInput(mode: string, at: string): SwitchInput | undefined { |
| 84 | if (mode === "on" || mode === "week" || mode === "owner") return { mode }; | |
| 88 | if (mode === "on" || mode === "week" || mode === "owner" || mode === "chapter") return { mode }; | |
| 85 | 89 | return mode === "until" ? { mode, at: utcAt(at) } : undefined; |
| 86 | 90 | } |
| 87 | 91 |
src/core/planner_pause.ts +40 −5
| @@ -1,22 +1,55 @@ | ||
| 1 | 1 | /** |
| 2 | * The planner's switch (#11604): paused, the repository's planner opens and splits nothing; builders | |
| 3 | * and reviewers carry on. A pause lifts itself at its time; "until I switch it on" is a time so far | |
| 4 | * ahead that only the owner ends it. Plain data in and out: the db, a repository id, a clock. | |
| 2 | * The planner's switch (#11604): paused, the repository's planner opens and splits nothing; | |
| 3 | * builders and reviewers carry on. A pause lifts itself at its time; "until I switch it on" is a | |
| 4 | * time so far ahead that only the owner ends it. Plain data in and out: the db, a repository id, a | |
| 5 | * clock. | |
| 5 | 6 | */ |
| 6 | 7 | |
| 7 | 8 | import type { Database } from "bun:sqlite"; |
| 8 | 9 | |
| 9 | 10 | export const FOREVER = "9999-12-31T00:00:00.000Z"; |
| 11 | /** The time a pause held for the open chapter carries: also far ahead, but it names its end. */ | |
| 12 | export const CHAPTER_UNTIL = "9999-12-30T00:00:00.000Z"; | |
| 10 | 13 | |
| 11 | /** When the pause lifts, or null when the planner is on (a pause already past is the same as none). */ | |
| 14 | /** | |
| 15 | * When the pause lifts, or null when the planner is on (a pause already past is the same as none). | |
| 16 | */ | |
| 12 | 17 | export function pausedUntil(db: Database, repoId: string, now: Date): string | null { |
| 13 | 18 | const row = db |
| 14 | 19 | .query<{ until: string }, [string]>("SELECT until FROM planner_pause WHERE repo_id = ?1") |
| 15 | 20 | .get(repoId); |
| 16 | return row !== null && row.until > now.toISOString() ? row.until : null; | |
| 21 | if (row === null || row.until <= now.toISOString()) return null; | |
| 22 | // A pause held for a chapter lifts the moment that chapter is no longer open. | |
| 23 | return row.until === CHAPTER_UNTIL && chapterHeld(db, repoId) === "closed" ? null : row.until; | |
| 24 | } | |
| 25 | ||
| 26 | const chapterKey = (repoId: string) => `planner.pauseChapter.${repoId}`; | |
| 27 | ||
| 28 | /** | |
| 29 | * "held" while the pause is bound to a chapter that is still open, "closed" once it is not, else | |
| 30 | * null. | |
| 31 | */ | |
| 32 | function chapterHeld(db: Database, repoId: string): "held" | "closed" | null { | |
| 33 | const row = db.query<{ value: string }, [string]>("SELECT value FROM forge_setting WHERE key = ?1").get(chapterKey(repoId)); | |
| 34 | if (row === null) return null; | |
| 35 | const c = db.query<{ state: string }, [string]>("SELECT state FROM chapter WHERE id = ?1").get(row.value); | |
| 36 | return c !== null && c.state === "open" ? "held" : "closed"; | |
| 37 | } | |
| 38 | ||
| 39 | /** Pauses until the repository's open chapter closes; false when it has none. */ | |
| 40 | export function pauseForChapter(db: Database, repoId: string, byId: string): boolean { | |
| 41 | const open = db.query<{ id: string }, [string]>("SELECT id FROM chapter WHERE repo_id = ?1 AND state = 'open'").get(repoId); | |
| 42 | if (open === null) return false; | |
| 43 | pausePlanner(db, repoId, CHAPTER_UNTIL, byId); | |
| 44 | db.query( | |
| 45 | `INSERT INTO forge_setting (key, value, updated_by, updated_at) VALUES (?1, ?2, ?3, strftime('%Y-%m-%dT%H:%M:%fZ','now')) | |
| 46 | ON CONFLICT(key) DO UPDATE SET value = ?2, updated_by = ?3, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')`, | |
| 47 | ).run(chapterKey(repoId), open.id, byId); | |
| 48 | return true; | |
| 17 | 49 | } |
| 18 | 50 | |
| 19 | 51 | export function pausePlanner(db: Database, repoId: string, until: string, byId: string): void { |
| 52 | db.query("DELETE FROM forge_setting WHERE key = ?1").run(chapterKey(repoId)); | |
| 20 | 53 | db.query( |
| 21 | 54 | `INSERT INTO planner_pause (repo_id, until, by_id) VALUES (?1, ?2, ?3) |
| 22 | 55 | ON CONFLICT(repo_id) DO UPDATE SET until = ?2, by_id = ?3, |
| @@ -25,6 +58,7 @@ export function pausePlanner(db: Database, repoId: string, until: string, byId: | ||
| 25 | 58 | } |
| 26 | 59 | |
| 27 | 60 | export function resumePlanner(db: Database, repoId: string): void { |
| 61 | db.query("DELETE FROM forge_setting WHERE key = ?1").run(chapterKey(repoId)); | |
| 28 | 62 | db.query("DELETE FROM planner_pause WHERE repo_id = ?1").run(repoId); |
| 29 | 63 | } |
| 30 | 64 | |
| @@ -43,6 +77,7 @@ export const PAUSE_STOPS = | ||
| 43 | 77 | /** The state in one sentence, for every door. */ |
| 44 | 78 | export function pauseSentence(until: string | null): string { |
| 45 | 79 | if (until === null) return "The planner is on."; |
| 80 | if (until === CHAPTER_UNTIL) return "The planner is paused until the open chapter closes."; | |
| 46 | 81 | if (until.startsWith("9999")) return "The planner is paused until you switch it on."; |
| 47 | 82 | return `The planner is paused until ${until.slice(0, 16).replace("T", " ")} UTC.`; |
| 48 | 83 | } |
src/http/mcp/tools/system.ts +5 −4
| @@ -23,10 +23,10 @@ import { good, bad, repoArg, isAnswer, REPO_ARG, type Tool } from "../shared.ts" | ||
| 23 | 23 | |
| 24 | 24 | export const tool: Tool = { |
| 25 | 25 | name: "system", |
| 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", | |
| 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, or `chapter: true`: until the open chapter closes, refused with no open chapter; else until switched on) or resume, the planner's switch: a preview, and `confirm:true` does it; the repository's owner, never an agent", | |
| 27 | 27 | description: |
| 28 | 28 | "The forge machine now: CPU, memory, disk, network. Admin or telemetry:read; `kind: csp`: 24 h of CSP violations (admin).", |
| 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" } } }, | |
| 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" }, chapter: { type: "boolean" }, confirm: { type: "boolean" } } }, | |
| 30 | 30 | writes: true, |
| 31 | 31 | readsWhen: (args) => args["action"] === undefined || args["confirm"] !== true, |
| 32 | 32 | async run(ctx, user, args) { |
| @@ -43,17 +43,18 @@ export const tool: Tool = { | ||
| 43 | 43 | if (args["action"] !== undefined) { |
| 44 | 44 | const a = args["action"]; |
| 45 | 45 | if (a !== "pause" && a !== "resume") return bad("invalid", "action is pause or resume"); |
| 46 | if (args["chapter"] !== undefined && (args["chapter"] !== true || a === "resume" || args["until"] !== undefined)) return bad("invalid", "chapter is true, for pause without until"); | |
No test drives this path (chapter:true preview, confirm, and the invalid combinations with until/resume). Every behaviour added needs a test that fails without it. |
||
| 46 | 47 | if (args["confirm"] !== undefined && typeof args["confirm"] !== "boolean") return bad("invalid", "confirm is true or false"); |
| 47 | 48 | if (args["until"] !== undefined && (typeof args["until"] !== "string" || a === "resume")) return bad("invalid", "until is a UTC time, for pause"); |
| 48 | 49 | if (!maySwitch(ctx.app, user, repo)) return bad("forbidden", SWITCH_REFUSED.forbidden); |
| 49 | 50 | const input: SwitchInput = |
| 50 | a === "resume" ? { mode: "on" } : typeof args["until"] === "string" ? { mode: "until", at: utcAt(args["until"]) } : { mode: "owner" }; | |
| 51 | a === "resume" ? { mode: "on" } : args["chapter"] === true ? { mode: "chapter" } : typeof args["until"] === "string" ? { mode: "until", at: utcAt(args["until"]) } : { mode: "owner" }; | |
| 51 | 52 | if (input.mode === "until" && !validUntil(input.at)) return bad("invalid", SWITCH_REFUSED.invalid); |
| 52 | 53 | if (args["confirm"] !== true) { |
| 53 | 54 | const now = pausedUntil(ctx.app.db, repo.id, new Date()); |
| 54 | 55 | const was = now === null ? "The planner is on now." : `${pauseSentence(now)} (now)`; |
| 55 | 56 | 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 | input.mode === "on" ? "The planner would be switched on." : input.mode === "chapter" ? "The planner would pause until the open chapter closes." : input.mode === "owner" ? "The planner would pause until you switch it on." : `The planner would pause until ${input.at}.`; | |
| 57 | 58 | return good({ done: false, says: `${would} ${was} Nothing changed; call again with confirm:true to do it.` }); |
| 58 | 59 | } |
| 59 | 60 | const done = setPlannerSwitch(ctx.app, user, repo, input); |
src/tui/screens/queuegrowth.ts +8 −4
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | * (#12868) is the same: `c` sets it, `r` resumes the planner for one plan, through `setCeiling`. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { setCeiling, setPlannerSwitch, switchInput, type SwitchInput } from "../../actions/planner_switch.ts"; | |
| 10 | import { setCeiling, setPlannerSwitch, SWITCH_REFUSED, switchInput, type SwitchInput } from "../../actions/planner_switch.ts"; | |
| 11 | 11 | import { growth, oldest, WHO } from "../../core/growth.ts"; |
| 12 | 12 | import { ceilingHold, CEILING_REASON, openCeiling, PAUSE_STOPS, pauseSentence, pausedUntil } from "../../core/planner_pause.ts"; |
| 13 | 13 | import { canAdmin, getRepoById, type Repo } from "../../core/repos.ts"; |
| @@ -22,7 +22,10 @@ import { promptScreen } from "./common.ts"; | ||
| 22 | 22 | |
| 23 | 23 | const DAYS = 14; |
| 24 | 24 | |
| 25 | /** Whether this session may flip the switch: a person who administers the repository, never an agent. */ | |
| 25 | /** | |
| 26 | * Whether this session may flip the switch: a person who administers the repository, never an | |
| 27 | * agent. | |
| 28 | */ | |
| 26 | 29 | const mayFlip = (f: Session, repo: Repo): boolean => |
| 27 | 30 | f.user !== undefined && f.user.kind !== "agent" && canAdmin(f.db, repo, f.user); |
| 28 | 31 | |
| @@ -61,7 +64,7 @@ export function queueScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 61 | 64 | const flip = (input: SwitchInput): string | undefined => { |
| 62 | 65 | const done = setPlannerSwitch(f.app, f.user, current(), input); |
| 63 | 66 | if (done.ok) ctx.toast(pauseSentence(done.value), "success"); |
| 64 | else ctx.toast(done.error === "invalid" ? "Pick a time in the future, within a year." : "Only the owner switches the planner.", "danger"); | |
| 67 | else ctx.toast(SWITCH_REFUSED[done.error], "danger"); | |
| 65 | 68 | ctx.invalidate(); |
| 66 | 69 | return undefined; |
| 67 | 70 | }; |
| @@ -75,7 +78,7 @@ export function queueScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 75 | 78 | title: "Queue", |
| 76 | 79 | get keys() { |
| 77 | 80 | return mayFlip(f, current()) |
| 78 | ? [binding(["o"], "pause until on"), binding(["w"], "pause to weekly reset"), binding(["t"], "pause until a time"), binding(["n"], "switch on"), binding(["c"], "set the ceiling"), binding(["r"], "resume past the ceiling")] | |
| 81 | ? [binding(["o"], "pause until on"), binding(["w"], "pause to weekly reset"), binding(["h"], "pause until the chapter closes"), binding(["t"], "pause until a time"), binding(["n"], "switch on"), binding(["c"], "set the ceiling"), binding(["r"], "resume past the ceiling")] | |
| 79 | 82 | : []; |
| 80 | 83 | }, |
| 81 | 84 | render(area, s) { |
| @@ -88,6 +91,7 @@ export function queueScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 88 | 91 | if (!mayFlip(f, current())) return false; |
| 89 | 92 | if (e.key === "o") return flip({ mode: "owner" }), true; |
| 90 | 93 | if (e.key === "w") return flip({ mode: "week" }), true; |
| 94 | if (e.key === "h") return flip({ mode: "chapter" }), true; | |
| 91 | 95 | if (e.key === "n") return flip({ mode: "on" }), true; |
| 92 | 96 | if (e.key === "r") return ceil({ mode: "resume" }), true; |
| 93 | 97 | if (e.key === "c") { |
src/web/pages/queue.ts +2 −1
| @@ -56,6 +56,7 @@ function switchRow(repo: Repo, until: string | null, owner: boolean): Html { | ||
| 56 | 56 | : html`${state}<form method="post" action="${post}" class="bar"> |
| 57 | 57 | <label>Pause <select class="input" name="mode"> |
| 58 | 58 | <option value="owner">until I switch it on</option> |
| 59 | <option value="chapter">until the open chapter closes</option> | |
| 59 | 60 | <option value="week">until the weekly reset</option> |
| 60 | 61 | <option value="until">until the time here (UTC)</option> |
| 61 | 62 | </select></label> |
| @@ -122,7 +123,7 @@ export async function queueSwitch(ctx: Ctx): Promise<Response> { | ||
| 122 | 123 | if (form instanceof Response) return form; |
| 123 | 124 | const back = backTo(form, `${repoBase(repo)}/queue`); |
| 124 | 125 | const input = switchInput(formField(form, "mode"), formField(form, "at")); |
| 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 | if (input === undefined) return flashTo(ctx, back, { tone: "danger", message: "Choose on, a time, the weekly reset, the chapter's close or until you switch it on." }); | |
| 126 | 127 | const done = setPlannerSwitch(ctx.app, ctx.auth.user, repo, input); |
| 127 | 128 | if (!done.ok) |
| 128 | 129 | return flashTo(ctx, back, { tone: "danger", message: SWITCH_REFUSED[done.error] }); |
test/planner-pause-chapter.test.ts added +174
| @@ -0,0 +1,174 @@ | ||
| 1 | /** | |
| 2 | * The planner can be paused until the open chapter closes (#13108): refused with none open, held | |
| 3 | * while open, lifted at close. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, 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 { newApp } from "../src/actions/apps.ts"; | |
| 10 | import { setPlannerSwitch, switchInput, SWITCH_REFUSED } from "../src/actions/planner_switch.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { draftChapter, getChapter, startChapter } from "../src/core/chapters.ts"; | |
| 13 | import { pauseSentence, pausedUntil } from "../src/core/planner_pause.ts"; | |
| 14 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 15 | import { createToken } from "../src/core/tokens.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { tool as systemTool } from "../src/http/mcp/tools/system.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import type { Ctx } from "../src/tui/app/app.ts"; | |
| 20 | import { queueScreen } from "../src/tui/screens/queuegrowth.ts"; | |
| 21 | import type { Session } from "../src/tui/session.ts"; | |
| 22 | import { quietLogs } from "./helpers/globals.ts"; | |
| 23 | ||
| 24 | quietLogs(); | |
| 25 | ||
| 26 | let dir = ""; | |
| 27 | let app: App; | |
| 28 | let owner: User; | |
| 29 | let repo: Repo; | |
| 30 | ||
| 31 | beforeAll(async () => { | |
| 32 | dir = await mkdtemp(join(tmpdir(), "forge-pause-chapter-")); | |
| 33 | app = createApp({ dataDir: dir, port: 0, host: "forge.test" }); | |
| 34 | const made = await createUser(app.db, { username: "alice", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true, adminIfFirst: true }); | |
| 35 | if (!made.ok) throw new Error(String(made.error)); | |
| 36 | owner = made.value; | |
| 37 | const c = await newApp(app, owner, { name: "shop", description: "Words", visibility: "public" }); | |
| 38 | if (!c.ok) throw new Error(String(c.error)); | |
| 39 | repo = getRepo(app.db, "alice", "shop")!; | |
| 40 | }, 30_000); | |
| 41 | ||
| 42 | afterAll(async () => { | |
| 43 | app.close(); | |
| 44 | await rm(dir, { recursive: true, force: true, maxRetries: 20, retryDelay: 100 }); | |
| 45 | }); | |
| 46 | ||
| 47 | test("with no open chapter the choice is refused in one sentence and nothing is paused", () => { | |
| 48 | const done = setPlannerSwitch(app, owner, repo, { mode: "chapter" }); | |
| 49 | expect(done.ok).toBe(false); | |
| 50 | if (!done.ok) expect(SWITCH_REFUSED[done.error]).toBe("There is no open chapter to wait for."); | |
| 51 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 52 | }); | |
| 53 | ||
| 54 | test("the form's word is understood", () => { | |
| 55 | expect(switchInput("chapter", "")).toEqual({ mode: "chapter" }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("it holds while the chapter is open and lifts when it closes", () => { | |
| 59 | const d = draftChapter(app.db, repo.id, { name: "v1.0.0", means: "First" }, owner.id); | |
| 60 | if (!d.ok) throw new Error(d.error); | |
| 61 | const o = startChapter(app.db, d.value, new Date()); | |
| 62 | if (!o.ok) throw new Error(o.error); | |
| 63 | const done = setPlannerSwitch(app, owner, repo, { mode: "chapter" }); | |
| 64 | expect(done.ok).toBe(true); | |
| 65 | const until = pausedUntil(app.db, repo.id, new Date()); | |
| 66 | expect(until).not.toBeNull(); | |
| 67 | expect(pauseSentence(until)).toBe("The planner is paused until the open chapter closes."); | |
| 68 | // Still held later: it is not a clock. | |
| 69 | expect(pausedUntil(app.db, repo.id, new Date(Date.now() + 400 * 86_400_000))).not.toBeNull(); | |
| 70 | app.db.query("UPDATE chapter SET state = 'closed', closed_at = ?2 WHERE id = ?1").run(getChapter(app.db, repo.id, "v1.0.0")!.id, new Date().toISOString()); | |
| 71 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 72 | }); | |
| 73 | ||
| 74 | test("a plain pause is not lifted by a chapter closing", () => { | |
| 75 | const d = draftChapter(app.db, repo.id, { name: "v1.1.0", means: "Second" }, owner.id); | |
| 76 | if (!d.ok) throw new Error(d.error); | |
| 77 | const o = startChapter(app.db, d.value, new Date()); | |
| 78 | if (!o.ok) throw new Error(o.error); | |
| 79 | expect(setPlannerSwitch(app, owner, repo, { mode: "owner" }).ok).toBe(true); | |
outdated
Vacuous: it finds the key whose JSON contains chapter and asserts the list contains it, which holds whenever any such key exists and fails only by accident. Assert the exact binding for h. Also this test is named for a plain pause surviving a chapter closing, but never closes a chapter or checks that; open a chapter, pause with owner, close it, and expect the pause still held. The h key is only tested for the refusal, not for a successful pause with a chapter open. The queue page form post with mode=chapter and its option are untested too. |
||
| 80 | app.db.query("UPDATE chapter SET state = 'closed', closed_at = ?2 WHERE id = ?1").run(getChapter(app.db, repo.id, "v1.1.0")!.id, new Date().toISOString()); | |
| 81 | expect(pausedUntil(app.db, repo.id, new Date())).not.toBeNull(); | |
| 82 | expect(setPlannerSwitch(app, owner, repo, { mode: "on" }).ok).toBe(true); | |
| 83 | }); | |
| 84 | ||
| 85 | const press = (key: string) => { | |
| 86 | const toasts: string[] = []; | |
| 87 | const session = { app, db: app.db, user: owner } as unknown as Session; | |
| 88 | const ctx = { toast: (m: string) => toasts.push(m), invalidate() {} } as unknown as Ctx; | |
| 89 | const screen = queueScreen(session, ctx, repo); | |
| 90 | screen.key({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 91 | return { toasts, screen }; | |
| 92 | }; | |
| 93 | ||
| 94 | test("the terminal's h binding is listed and refuses with no chapter, pauses with one", () => { | |
| 95 | const { toasts, screen } = press("h"); | |
| 96 | expect(screen.keys.map((k) => JSON.stringify(k)).some((k) => k.includes("pause until the chapter closes") && k.includes('"h"'))).toBe(true); | |
| 97 | expect(toasts).toEqual(["There is no open chapter to wait for."]); | |
| 98 | const d = draftChapter(app.db, repo.id, { name: "v2.0.0", means: "Third" }, owner.id); | |
| 99 | if (!d.ok) throw new Error(d.error); | |
| 100 | const o = startChapter(app.db, d.value, new Date()); | |
| 101 | if (!o.ok) throw new Error(o.error); | |
| 102 | expect(press("h").toasts).toEqual(["The planner is paused until the open chapter closes."]); | |
| 103 | expect(pausedUntil(app.db, repo.id, new Date())).not.toBeNull(); | |
| 104 | setPlannerSwitch(app, owner, repo, { mode: "on" }); | |
| 105 | app.db.query("UPDATE chapter SET state = 'closed', closed_at = ?2 WHERE id = ?1").run(getChapter(app.db, repo.id, "v2.0.0")!.id, new Date().toISOString()); | |
| 106 | }); | |
| 107 | ||
| 108 | describe("the page and MCP", () => { | |
| 109 | let servers: Servers; | |
| 110 | let token = ""; | |
| 111 | beforeAll(async () => { | |
| 112 | servers = await serve(app); | |
| 113 | token = createToken(app.db, owner.id, "rw", "rw").token; | |
| 114 | }); | |
| 115 | afterAll(() => servers.stop()); | |
| 116 | ||
| 117 | const openChapter = (name: string) => { | |
| 118 | const d = draftChapter(app.db, repo.id, { name, means: "x" }, owner.id); | |
| 119 | if (!d.ok) throw new Error(d.error); | |
| 120 | const o = startChapter(app.db, d.value, new Date()); | |
| 121 | if (!o.ok) throw new Error(o.error); | |
| 122 | }; | |
| 123 | const closeChapter = (name: string) => app.db.query("UPDATE chapter SET state = 'closed', closed_at = ?2 WHERE id = ?1").run(getChapter(app.db, repo.id, name)!.id, new Date().toISOString()); | |
| 124 | const post = (body: string) => | |
| 125 | fetch(`${servers.url}/alice/shop/queue/planner`, { method: "POST", redirect: "manual", headers: { authorization: `Bearer ${token}`, "content-type": "application/x-www-form-urlencoded", origin: servers.url }, body }); | |
| 126 | const mcp = async (args: Record<string, unknown>) => { | |
| 127 | const res = await fetch(`${servers.url}/mcp`, { | |
| 128 | method: "POST", | |
| 129 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 130 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "system", arguments: { kind: "growth", repo: "alice/shop", ...args } } }), | |
| 131 | }); | |
| 132 | const body: any = await res.json(); | |
| 133 | const text: string = body.result.content[0].text; | |
| 134 | return body.result.isError === true ? { error: text } : { value: JSON.parse(text) }; | |
| 135 | }; | |
| 136 | ||
| 137 | test("the queue page offers the choice to the owner", async () => { | |
| 138 | const res = await fetch(`${servers.url}/alice/shop/queue`, { headers: { authorization: `Bearer ${token}` } }); | |
| 139 | expect(await res.text()).toContain('<option value="chapter">until the open chapter closes</option>'); | |
| 140 | }); | |
| 141 | ||
| 142 | test("the form's mode=chapter is refused with none open and pauses with one", async () => { | |
| 143 | expect((await post("mode=chapter")).status).toBeLessThan(400); | |
| 144 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 145 | openChapter("v3.0.0"); | |
| 146 | expect((await post("mode=chapter")).status).toBeLessThan(400); | |
| 147 | expect(pauseSentence(pausedUntil(app.db, repo.id, new Date()))).toBe("The planner is paused until the open chapter closes."); | |
| 148 | closeChapter("v3.0.0"); | |
| 149 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 150 | }); | |
| 151 | ||
| 152 | test("the tool's schema carries chapter", () => { | |
| 153 | expect((systemTool.inputSchema["properties"] as Record<string, unknown>)["chapter"]).toEqual({ type: "boolean" }); | |
| 154 | }); | |
| 155 | ||
| 156 | test("chapter:true previews, refuses with none open, confirms with one, and rejects bad combinations", async () => { | |
| 157 | const none: any = await mcp({ action: "pause", chapter: true, confirm: true }); | |
| 158 | expect(none.error).toContain("no open chapter"); | |
| 159 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 160 | openChapter("v4.0.0"); | |
| 161 | const preview: any = await mcp({ action: "pause", chapter: true }); | |
| 162 | expect(preview.value.done).toBe(false); | |
| 163 | expect(preview.value.says).toContain("pause until the open chapter closes"); | |
| 164 | expect(pausedUntil(app.db, repo.id, new Date())).toBeNull(); | |
| 165 | const done: any = await mcp({ action: "pause", chapter: true, confirm: true }); | |
| 166 | expect(done.value.done).toBe(true); | |
| 167 | expect(done.value.says).toBe("The planner is paused until the open chapter closes."); | |
| 168 | expect(pausedUntil(app.db, repo.id, new Date())).not.toBeNull(); | |
| 169 | for (const bad of [{ action: "resume", chapter: true }, { action: "pause", chapter: true, until: "2099-01-01T00:00:00Z" }, { action: "pause", chapter: false }, { action: "pause", chapter: "yes" }]) | |
| 170 | expect(((await mcp(bad)) as any).error).toContain("chapter is true"); | |
| 171 | await mcp({ action: "resume", confirm: true }); | |
| 172 | closeChapter("v4.0.0"); | |
| 173 | }); | |
| 174 | }); |
inputSchema lacks
chapter: { type: "boolean" }though the handler reads it. Add it so the door is discoverable and schema-validating clients can send it.