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

With 'plans need a nod' on, POST …/plan stores one proposal and opens no issues #1860

Closed opened this
Closed without merging

scout/forge:scout/1813-with-plans-need-a-nod-on-post-plan-store → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
9 files changed +223 −16
docs/PLANNING.md +10
@@ -565,3 +565,13 @@ not confirm its own plan. Split also moves the parent's blocked-by edges to
565565 the issues named in `into` (a child that would make a loop is skipped), and
566566 the parent waits for nothing. Reopening clears the reason. Closes that
567567 predate reasons count as done.
568
569 ## Plans need a nod
570
571 A repository setting, `plansNeedNod` (PATCH `/api/v1/repos/:owner/:name`,
572 admin only, never an agent), on by default. On, `POST …/plan` and the MCP
573 `plan` tool run every check as ever (`no_pillar`, loops, unknown keys) and
574 then store one proposal — title, why, the ordered issues with their
575 `blockedBy` — answering `proposed` with its id (HTTP 202). No issue is opened.
576 Off, the plan opens its linked issues all or none. A person's nod that opens
577 a stored proposal is not built yet.
src/actions/plan.ts +73 −2
@@ -25,6 +25,7 @@ import {
2525 import { canRead, canWrite, type Repo } from "../core/repos.ts";
2626 import type { User } from "../core/users.ts";
2727 import { transact } from "../kernel/db.ts";
28 import { id as newId } from "../kernel/id.ts";
2829 import { err, ok, type Result } from "../kernel/result.ts";
2930 import { isMirror } from "../origins/write-through.ts";
3031 import { followLink } from "./pillars.ts";
@@ -186,12 +187,22 @@ function loopAt(items: readonly PlanItem[]): string | undefined {
186187 return undefined;
187188 }
188189
189 export function openPlan(
190 /** What a good plan was checked against, for opening it. */
191 interface Vetted {
192 readonly actor: User;
193 readonly existing: Map<number, Issue>;
194 readonly serves: Map<string, Pillar>;
195 readonly gate: Issue | undefined;
196 readonly later: Issue | undefined;
197 }
198
199 /** Every refusal a plan can meet, before anything is written. */
200 function vet(
190201 app: App,
191202 actor: User | undefined,
192203 repo: Repo,
193204 items: readonly PlanItem[],
194 ): Result<Planned, PlanError> {
205 ): Result<Vetted, PlanError> {
195206 if (!actor || !canRead(app.db, repo, actor))
196207 return refuse("not_found", "no such app or repository (owner/name)");
197208 if (!canWrite(app.db, repo, actor) && !isCrew(app.db, repo.id, actor.id))
@@ -258,6 +269,66 @@ export function openPlan(
258269 `"${item.key}" names no pillar: give it ${namesOf(pillars)}, or later: true and why`,
259270 );
260271 }
272 return ok({ actor, existing, serves, gate, later });
273 }
274
275 export interface Proposed {
276 readonly status: "proposed";
277 readonly id: string;
278 }
279
280 /**
281 * "Plans need a nod": the same checks as `openPlan`, then the plan is kept
282 * as one proposal (title, why, the ordered issues with their blockedBy) and
283 * no issue is opened. A person's nod opens it.
284 */
285 export function proposePlan(
286 app: App,
287 actor: User | undefined,
288 repo: Repo,
289 items: readonly PlanItem[],
290 say: { title?: unknown; why?: unknown } = {},
291 ): Result<Proposed, PlanError> {
292 const title = typeof say.title === "string" ? say.title.trim() : "";
293 if (say.title !== undefined && typeof say.title !== "string")
294 return refuse("invalid", "title must be text");
295 if (title.length > MAX_TITLE)
296 return refuse("title_too_long", `title is over ${MAX_TITLE} characters`);
297 if (say.why !== undefined && typeof say.why !== "string")
298 return refuse("invalid", "why must be text");
299 const why = ((say.why ?? "") as string).trim();
300 if (why.length > MAX_BODY)
301 return refuse("body_too_long", `why is over ${MAX_BODY} characters`);
302 const vetted = vet(app, actor, repo, items);
303 if (!vetted.ok) return vetted;
304 const id = newId();
305 app.db
306 .query(
307 `INSERT INTO plan_proposal (id, repo_id, author_id, title, why, issues, created_at)
308 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)`,
309 )
310 .run(
311 id,
312 repo.id,
313 vetted.value.actor.id,
314 title === "" ? items[0]!.title : title,
315 why,
316 JSON.stringify(items),
317 new Date().toISOString(),
318 );
319 return ok({ status: "proposed", id });
320 }
321
322 export function openPlan(
323 app: App,
324 actor: User | undefined,
325 repo: Repo,
326 items: readonly PlanItem[],
327 ): Result<Planned, PlanError> {
328 const vetted = vet(app, actor, repo, items);
329 if (!vetted.ok) return vetted;
330 const { existing, serves, gate, later } = vetted.value;
331 actor = vetted.value.actor;
261332
262333 const opened: { key: string; number: number }[] = [];
263334 /** The links an item's purpose adds: [the issue that waits, what it waits for]. */
src/actions/repos.ts +3 −1
@@ -249,7 +249,8 @@ export async function updateRepoSettings(
249249 // one on a team that administers the repository (`crew/direct.ts`).
250250 if (
251251 (settings.crewOutsiders !== undefined ||
252 settings.decisionsBy !== undefined) &&
252 settings.decisionsBy !== undefined ||
253 settings.plansNeedNod !== undefined) &&
253254 admin.value.kind === "agent"
254255 )
255256 return err("not_found");
@@ -276,6 +277,7 @@ export async function updateRepoSettings(
276277 "requireChecks",
277278 "crewOutsiders",
278279 "decisionsBy",
280 "plansNeedNod",
279281 ] as const
280282 ).filter((k) => settings[k] !== undefined && settings[k] !== repo[k]);
281283 app.bus.append({
src/core/repos.ts +8 −1
@@ -118,6 +118,8 @@ export interface Repo {
118118 readonly crewOutsiders: boolean;
119119 /** The standing rule for decisions here: `me`, `decider`, or `decider_tell`. */
120120 readonly decisionsBy: DecisionsBy;
121 /** "Plans need a nod": a plan is stored as a proposal, not opened. */
122 readonly plansNeedNod: boolean;
121123 /** The issue the planner is given, when the map has run dry. */
122124 readonly directionIssueId: string | null;
123125 /** The repo this was forked from, or null. */
@@ -163,6 +165,7 @@ interface RepoRow {
163165 autonomy_by: string | null;
164166 crew_outsiders: number;
165167 decisions_by: string;
168 plans_need_nod: number;
166169 direction_issue_id: string | null;
167170 fork_parent_id: string | null;
168171 sealed_for: string | null;
@@ -201,6 +204,7 @@ function toRepo(row: RepoRow): Repo {
201204 autonomyBy: row.autonomy_by,
202205 crewOutsiders: row.crew_outsiders === 1,
203206 decisionsBy: isDecisionsBy(row.decisions_by) ? row.decisions_by : "me",
207 plansNeedNod: row.plans_need_nod === 1,
204208 directionIssueId: row.direction_issue_id,
205209 forkParentId: row.fork_parent_id,
206210 sealedFor: row.sealed_for,
@@ -1036,6 +1040,7 @@ export interface RepoSettings {
10361040 /** Whether someone who cannot write here may start or steer a run. */
10371041 crewOutsiders?: boolean;
10381042 decisionsBy?: DecisionsBy;
1043 plansNeedNod?: boolean;
10391044 }
10401045
10411046 /** Update the mutable repo fields, leaving unspecified ones untouched. */
@@ -1048,7 +1053,8 @@ export function updateRepoSettings(
10481053 `UPDATE repo SET visibility = ?1, description = ?2, protect_default = ?3,
10491054 default_branch = ?4, require_review = ?5, require_checks = ?7,
10501055 autonomy = ?8, autonomy_direction = ?9, autonomy_ceiling_usd = ?10,
1051 autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13, decisions_by = ?14
1056 autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13, decisions_by = ?14,
1057 plans_need_nod = ?15
10521058 WHERE id = ?6`,
10531059 ).run(
10541060 settings.visibility ?? repo.visibility,
@@ -1065,6 +1071,7 @@ export function updateRepoSettings(
10651071 settings.autonomyBy === undefined ? repo.autonomyBy : settings.autonomyBy,
10661072 (settings.crewOutsiders ?? repo.crewOutsiders) ? 1 : 0,
10671073 settings.decisionsBy ?? repo.decisionsBy,
1074 (settings.plansNeedNod ?? repo.plansNeedNod) ? 1 : 0,
10681075 );
10691076 return getRepoById(db, repo.id)!;
10701077 }
src/core/schema.ts +16
@@ -1794,6 +1794,22 @@ export const migrations: readonly string[] = [
17941794 `
17951795 ALTER TABLE issue ADD COLUMN parked INTEGER NOT NULL DEFAULT 0;
17961796 `,
1797 // 90 — plans need a nod (#1813): with the setting on, a plan is stored as
1798 // one proposal (its ordered issues as JSON) and opens nothing until a
1799 // person nods. On is the default.
1800 `
1801 ALTER TABLE repo ADD COLUMN plans_need_nod INTEGER NOT NULL DEFAULT 1;
1802 CREATE TABLE plan_proposal (
1803 id TEXT PRIMARY KEY,
1804 repo_id TEXT NOT NULL,
1805 author_id TEXT NOT NULL,
1806 title TEXT NOT NULL,
1807 why TEXT NOT NULL,
1808 issues TEXT NOT NULL,
1809 created_at TEXT NOT NULL
1810 );
1811 CREATE INDEX plan_proposal_repo ON plan_proposal(repo_id, created_at);
1812 `,
17971813 ];
17981814
17991815 /**
src/http/api.ts +19 −8
@@ -274,7 +274,7 @@ import {
274274 unlinkIssue,
275275 confirmIssue,
276276 } from "../actions/dependencies.ts";
277 import { openPlan, parsePlan } from "../actions/plan.ts";
277 import { openPlan, parsePlan, proposePlan } from "../actions/plan.ts";
278278 import { dependenciesOf, repoGraph } from "../core/dependencies.ts";
279279 import { pillarView } from "../core/pillars.ts";
280280 import { askToLiveFor } from "../web/ask_to_live.ts";
@@ -1816,6 +1816,10 @@ export function apiRoutes(router: Router<Handler>): void {
18161816 ...(isDecisionsBy(input.decisionsBy)
18171817 ? { decisionsBy: input.decisionsBy }
18181818 : {}),
1819 // Whether a plan waits for a nod: an admin's, never an agent's.
1820 ...(typeof input.plansNeedNod === "boolean"
1821 ? { plansNeedNod: input.plansNeedNod }
1822 : {}),
18191823 });
18201824 if (!updated.ok) return respondAccess(updated.error, 422);
18211825 return json(repoJson(ctx, updated.value));
@@ -2580,15 +2584,22 @@ export function apiRoutes(router: Router<Handler>): void {
25802584 const items = parsePlan(input.issues);
25812585 if (!items.ok)
25822586 return apiError(items.error.error, 422, items.error.message);
2583 const planned = openPlan(ctx.app, ctx.auth.user, repo, items.value);
2584 if (!planned.ok) {
2585 const { error, message } = planned.error;
2586 return apiError(
2587 error,
2588 error === "not_found" ? 404 : error === "forbidden" ? 403 : 422,
2589 message,
2587 const failed = (e: { error: string; message: string }) =>
2588 apiError(
2589 e.error,
2590 e.error === "not_found" ? 404 : e.error === "forbidden" ? 403 : 422,
2591 e.message,
25902592 );
2593 // With "plans need a nod" the plan is stored, not opened.
2594 if (repo.plansNeedNod) {
2595 const proposed = proposePlan(ctx.app, ctx.auth.user, repo, items.value, {
2596 title: input.title,
2597 why: input.why,
2598 });
2599 return proposed.ok ? json(proposed.value, 202) : failed(proposed.error);
25912600 }
2601 const planned = openPlan(ctx.app, ctx.auth.user, repo, items.value);
2602 if (!planned.ok) return failed(planned.error);
25922603 return json(planned.value, 201);
25932604 });
25942605 router.add(
src/http/mcp.ts +16 −2
@@ -39,7 +39,7 @@ import {
3939 unlinkIssue,
4040 } from "../actions/dependencies.ts";
4141 import { holdPull, releasePull } from "../actions/holds.ts";
42 import { openPlan, parsePlan, PLAN_MAX } from "../actions/plan.ts";
42 import { openPlan, parsePlan, PLAN_MAX, proposePlan } from "../actions/plan.ts";
4343 import { cancelRepoRun } from "../actions/runs.ts";
4444 import { familyView } from "../spawn/family.ts";
4545 import { clientRows } from "../spawn/clients.ts";
@@ -1883,11 +1883,13 @@ const TOOLS: readonly Tool[] = [
18831883 },
18841884 {
18851885 name: "plan",
1886 description: `Open several small issues and link them in one call: all of them or none. Each item has a key, a title, a body and optionally blockedBy, a list of keys of this plan or numbers of issues already open. Where the app has pillars (umbrella issues titled like "P0 Trust: …"), each item also says what it is for: pillar, the one it serves, or later: true with why, one sentence, which puts it behind the Later gate instead of on the ready list; an item that says neither is refused. A bad link (loop, unknown key) refuses the whole plan and names the item. At most ${PLAN_MAX} issues. Answers with the numbers opened, by key, what is ready now, and what waits for later.`,
1886 description: `Open several small issues and link them in one call: all of them or none. Each item has a key, a title, a body and optionally blockedBy, a list of keys of this plan or numbers of issues already open. Where the app has pillars (umbrella issues titled like "P0 Trust: …"), each item also says what it is for: pillar, the one it serves, or later: true with why, one sentence, which puts it behind the Later gate instead of on the ready list; an item that says neither is refused. A bad link (loop, unknown key) refuses the whole plan and names the item. At most ${PLAN_MAX} issues. Answers with the numbers opened, by key, what is ready now, and what waits for later. Where the app has "plans need a nod" on, the same checks run and one proposal is stored (optional title and why for the whole plan); nothing is opened, and it answers status proposed with the proposal id.`,
18871887 inputSchema: {
18881888 type: "object",
18891889 properties: {
18901890 app: APP_ARG,
1891 title: { type: "string", maxLength: 256 },
1892 why: { type: "string", maxLength: 65536 },
18911893 issues: {
18921894 type: "array",
18931895 minItems: 1,
@@ -1934,6 +1936,18 @@ const TOOLS: readonly Tool[] = [
19341936 if (isAnswer(repo)) return repo;
19351937 const items = parsePlan(args["issues"]);
19361938 if (!items.ok) return bad(items.error.error, items.error.message);
1939 if (repo.plansNeedNod) {
1940 const proposed = proposePlan(ctx.app, user, repo, items.value, {
1941 title: args["title"],
1942 why: args["why"],
1943 });
1944 if (!proposed.ok)
1945 return bad(proposed.error.error, proposed.error.message);
1946 return good({
1947 says: `Proposed plan ${proposed.value.id}: nothing is opened until a person nods.`,
1948 ...proposed.value,
1949 });
1950 }
19371951 const planned = openPlan(ctx.app, user, repo, items.value);
19381952 if (!planned.ok) return bad(planned.error.error, planned.error.message);
19391953 const { opened, ready, later, gate } = planned.value;
test/mcp.test.ts +33 −1
@@ -13,7 +13,7 @@ import { addToCrew, createAgent } from "../src/actions/crew.ts";
1313 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
1414 import { createApp as insertApp } from "../src/core/apps.ts";
1515 import { listIssues } from "../src/core/issues.ts";
16 import { getRepoById, type Repo } from "../src/core/repos.ts";
16 import { getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts";
1717 import { createRun, listRuns } from "../src/core/runs.ts";
1818 import { createToken } from "../src/core/tokens.ts";
1919 import { createUser, type User } from "../src/core/users.ts";
@@ -521,7 +521,39 @@ describe("the forge over MCP", () => {
521521 expect(after.ready.map((i: any) => i.number)).toContain(b);
522522 });
523523
524 test("with plans need a nod on, the plan tool stores one proposal and opens nothing", async () => {
525 const count = async () =>
526 (await tool(ro, "map", { app: "alice/game" })).value.issues.length;
527 const before = await count();
528 const issues = [
529 { key: "a", title: "First" },
530 { key: "b", title: "Second", blockedBy: ["a"] },
531 ];
532 const done = await tool(rw, "plan", {
533 app: "alice/game",
534 title: "Two steps",
535 why: "worth a look",
536 issues,
537 });
538 expect(done.value.status).toBe("proposed");
539 const row = app.db
540 .query<{ title: string; why: string; issues: string }, [string]>(
541 "SELECT title, why, issues FROM plan_proposal WHERE id = ?1",
542 )
543 .get(done.value.id)!;
544 expect(row.title).toBe("Two steps");
545 expect(JSON.parse(row.issues).map((i: any) => i.key)).toEqual(["a", "b"]);
546 expect(await count()).toBe(before);
547 // The checks run as ever: a loop is refused and stores nothing.
548 const loop = await tool(rw, "plan", {
549 app: "alice/game",
550 issues: [{ key: "x", title: "X", blockedBy: ["x"] }],
551 });
552 expect(loop.error).toContain("self");
553 });
554
524555 test("plan opens linked issues all or none", async () => {
556 updateRepoSettings(app.db, getRepoById(app.db, game.id)!, { plansNeedNod: false });
525557 const issues = [
526558 { key: "a", title: "First" },
527559 { key: "b", title: "Second", body: "two" },
test/plan-route.test.ts +45 −1
@@ -12,7 +12,7 @@ import { openIssue } from "../src/actions/issues.ts";
1212 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1313 import { listIssues } from "../src/core/issues.ts";
1414 import { pillarsOf } from "../src/core/pillars.ts";
15 import type { Repo } from "../src/core/repos.ts";
15 import { getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts";
1616 import { createToken } from "../src/core/tokens.ts";
1717 import { createUser, type User } from "../src/core/users.ts";
1818 import { serve, type Servers } from "../src/http/server.ts";
@@ -68,7 +68,50 @@ describe("POST …/plan", () => {
6868 await rm(dataDir, { recursive: true, force: true });
6969 });
7070
71 const proposals = () =>
72 app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM plan_proposal").get()!.n;
73 const nod = (on: boolean) =>
74 updateRepoSettings(app.db, getRepoById(app.db, repo.id)!, { plansNeedNod: on });
75
76 test("plans need a nod is on by default: one proposal is stored and no issue is opened", async () => {
77 expect(getRepoById(app.db, repo.id)!.plansNeedNod).toBe(true);
78 const before = titles().length;
79 const stored = proposals();
80 const res = await post(rw, [
81 { key: "a", title: "Prune the roses", pillar: "P0" },
82 { key: "b", title: "Mulch", pillar: "P0", blockedBy: ["a"] },
83 ]);
84 expect(res.status).toBe(202);
85 const body = (await res.json()) as { status: string; id: string };
86 expect(body.status).toBe("proposed");
87 expect(titles().length).toBe(before);
88 expect(proposals()).toBe(stored + 1);
89 const row = app.db
90 .query<{ issues: string }, [string]>("SELECT issues FROM plan_proposal WHERE id = ?1")
91 .get(body.id)!;
92 expect(JSON.parse(row.issues).map((i: { key: string }) => i.key)).toEqual(["a", "b"]);
93 });
94
95 test("on, a pillarless issue is still refused with no_pillar and nothing is stored", async () => {
96 const stored = proposals();
97 const res = await post(rw, [{ key: "a", title: "Prune" }]);
98 expect(res.status).toBe(422);
99 expect(((await res.json()) as { error: string }).error).toBe("no_pillar");
100 expect(proposals()).toBe(stored);
101 });
102
103 test("off, the plan opens its issues as before and stores no proposal", async () => {
104 nod(false);
105 const stored = proposals();
106 const res = await post(rw, [{ key: "a", title: "Sweep the path", pillar: "P0" }]);
107 expect(res.status).toBe(201);
108 expect(titles()).toContain("Sweep the path");
109 expect(proposals()).toBe(stored);
110 nod(true);
111 });
112
71113 test("a plan with a pillarless issue is refused with no_pillar and opens nothing; with the pillar named it opens the issues under the umbrella", async () => {
114 nod(false);
72115 const before = titles().length;
73116 const first = await post(rw, [{ key: "a", title: "Water the beds" }]);
74117 expect(((await first.json()) as { error: string }).error).toBe("no_pillar");
@@ -90,6 +133,7 @@ describe("POST …/plan", () => {
90133 });
91134
92135 test("a person's token plans the same way, all or nothing, and a read token is refused", async () => {
136 nod(false);
93137 const before = titles().length;
94138 const refused = await post(rw, [
95139 { key: "x", title: "Fine", pillar: "P0" },

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs