diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c68348e96..2d7119e7f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -86,6 +86,19 @@ jobs: - name: The lint gate can fail run: node --test scripts/lint-gate.test.mjs + # One task surface (FIX-2087). Here rather than beside "One word per thing" + # in Process guards: the endings rule reads callers by type, so it needs the + # TypeScript this job installs. The goal's rules.mjs owns the allow lists; + # its goal.md says why. + - name: One task surface + run: node goals/workforce-coordination/runs-on-one-task/sweep.mjs + + # Every plant must be reported, and nothing else, or the sweep above could + # pass because it stopped reading a rule. It runs even when the sweep is red. + - name: The task-surface sweep reports its plants + if: ${{ !cancelled() }} + run: node goals/workforce-coordination/runs-on-one-task/sweep.mjs --control + # The package tests, split across runners by scripts/test-shards.mjs. Each # shard runs its packages one at a time, as `pnpm test` does: two at once on # one runner oversubscribe it and tests start timing out. diff --git a/goals/coordinators/files-tasks-down-the-owners-chain/goal.md b/goals/coordinators/files-tasks-down-the-owners-chain/goal.md index e564932eee..e4cb23ddf4 100644 --- a/goals/coordinators/files-tasks-down-the-owners-chain/goal.md +++ b/goals/coordinators/files-tasks-down-the-owners-chain/goal.md @@ -30,7 +30,7 @@ **Controls:** each is scratch patches to Workforce modules, applied as the process under test loads them (`goals/lib/module-patch.mjs` through `NODE_OPTIONS`); printed in full by the run, and refused when they never reached the code. - `GOAL_CONTROL=unpartitioned`: one ledger for all of a user's conversations (`conversation-board/ledger.ts`: the partition the board's drain claims in, and every other read or write of the ledger at whatever partition its caller names; and the task entry's check that a task session works only its own conversation's partition). Patching each caller instead missed the ones FIX-1802 P2 added, and the control went green (FIX-1992). Leg c must fail **`c:runs-its-own`** (the first conversation's assign and dispatch reach the other's task, and its board runs it) and **`c:lists-its-own`** (each list holds every task: a control that leaves the lists apart never merged the ledger). `c:found-its-own` goes red with them. `c:drain-returns` goes red too, but not on the wait: the task it measures ran under the other conversation, so it was never running when the measure starts. It has no isolating red state. -- `GOAL_CONTROL=no-follow-up`: a task's ending reaches the conversation that filed it and nothing follows, no line and no turn (orchestration's `tasks/notice/task-notice.ts`); a retried attempt still runs again. Legs a and e must fail **`a:one-notice`** and **`e:one-notice`**. In leg e, `e:refiled` and `e:reply-names` go red with it: no turn, so nothing is filed again. +- `GOAL_CONTROL=no-follow-up`: a task's ending reaches the conversation that filed it and nothing follows, no line and no turn (orchestration's `tasks/notice/task-notice.ts`, and the two places Workforce's `conversation-board/task-settled.ts` maps that decision to a turn or a line itself, for a turn-filed failure and an unrouted one); a retried attempt still runs again. Legs a and e must fail **`a:one-notice`** and **`e:one-notice`**. In leg e, `e:refiled` and `e:reply-names` go red with it: no turn, so nothing is filed again. **Before-state:** `GOAL_COMMIT=84e9e072e` is `main` just before FIX-1794's second PR merged (#2935, merge `1854a84f0`). Every leg must fail there. diff --git a/goals/coordinators/files-tasks-down-the-owners-chain/run.mts b/goals/coordinators/files-tasks-down-the-owners-chain/run.mts index 094dc486b1..71f0b563b7 100644 --- a/goals/coordinators/files-tasks-down-the-owners-chain/run.mts +++ b/goals/coordinators/files-tasks-down-the-owners-chain/run.mts @@ -91,6 +91,14 @@ const CONTROLS: Record = { from: String.raw`return policy === "judgment" \? \{ act: "wake-turn" \} : \{ act: "line" \};`, to: 'return { act: "none" };', }, + { + // Workforce's conversation board maps the decision to a turn or a line itself for a + // turn-filed failure (FIX-2115) and an unrouted one (FIX-2122), so `none` there read as a + // line, and leg e's failure was still said: both maps say nothing too. + module: "packages/workforce/src/conversation-board/task-settled.ts", + from: String.raw`const act = (unrouted && )?decideNotice\(notice, options\.policy\(ctx\)\)\.act === "wake-turn" \? "wake-turn" : "line";`, + to: 'const act = "none" as const;', + }, ], does: "a task's ending reaches the conversation that filed it, and nothing follows: no line and no coordinator turn (a retried attempt still runs again)", }, diff --git a/goals/coordinators/files-tasks-down-the-owners-chain/store.mts b/goals/coordinators/files-tasks-down-the-owners-chain/store.mts index 8678d33cb6..da11c88734 100644 --- a/goals/coordinators/files-tasks-down-the-owners-chain/store.mts +++ b/goals/coordinators/files-tasks-down-the-owners-chain/store.mts @@ -45,6 +45,8 @@ export type StoredRequest = { createdAt: number; /** When it ended, or its last write while it runs. */ updatedAt: number; + /** Its result's output, once it has one. */ + output?: unknown; }; export type StoredItem = Record & { type: string; requestId: string }; @@ -99,7 +101,7 @@ export function sessions(file: string): StoredSession[] { export function requests(file: string): StoredRequest[] { return open(file, (db) => (db.prepare("SELECT id, session_id, flow_kind, status, created_at, updated_at, data FROM requests ORDER BY created_at").all() as Array>).map((row) => { - const data = JSON.parse(row.data) as { actionName?: string; source?: string; finalizedAtMs?: number }; + const data = JSON.parse(row.data) as { actionName?: string; source?: string; finalizedAtMs?: number; result?: { output?: unknown } }; return { id: row.id, sessionId: row.session_id ?? null, @@ -109,6 +111,7 @@ export function requests(file: string): StoredRequest[] { source: data.source, createdAt: row.created_at, updatedAt: data.finalizedAtMs ?? row.updated_at, + ...(data.result?.output !== undefined ? { output: data.result.output } : {}), }; }), ); diff --git a/goals/coordinators/hands-each-post-to-its-delegates/controls/patches.mts b/goals/coordinators/hands-each-post-to-its-delegates/controls/patches.mts index 175a36e717..a0d4bbb3a7 100644 --- a/goals/coordinators/hands-each-post-to-its-delegates/controls/patches.mts +++ b/goals/coordinators/hands-each-post-to-its-delegates/controls/patches.mts @@ -14,8 +14,9 @@ * worker's defaults, not the conversation's list. Leg d must fail on *the * reply equals the list*. * - `no-self-choice`: best fit never offers the coordinator itself. Leg h - * must fail on *a hire reaches the chief of staff's own turn*: the EM, its - * one choice, is picked at full confidence. + * must fail on one of its own-job asks: the EM, its one choice, takes + * whichever the evaluation places on it (the hire, when this was written; + * the project since). * - `no-floor`: a delegate pick below `minConfidence:` is used. Leg e must * fail on *a pick below the floor goes to the fallback*, and nothing else: * a pick with no confidence is still a miss. @@ -48,6 +49,8 @@ export interface Control { leg: "a" | "d" | "e" | "f" | "h"; /** The assertion, by name, that must go red. */ assertion: string; + /** When which assertion goes red is best fit's call: any one of these, `assertion` among them. */ + anyOf?: readonly string[]; /** The module, from the checkout root. */ module: string; from: string; @@ -88,6 +91,9 @@ export const CONTROLS: Record = { name: "no-self-choice", leg: "h", assertion: "h:hire-turn", + // Which of leg h's asks best fit now places on the EM is the evaluation's call: on + // 2026-10-24 it was the project ask, 4 of 4 runs, and the hire missed back to the turn. + anyOf: ["h:hire-turn", "h:fire-turn", "h:who-turn", "h:project-turn"], module: "packages/workforce/src/coordinator/coordinator-flow.ts", from: String.raw`const offersSelf = post\.round === 0 && worker\.description !== null;`, to: "const offersSelf = false;", diff --git a/goals/coordinators/hands-each-post-to-its-delegates/goal.md b/goals/coordinators/hands-each-post-to-its-delegates/goal.md index 4e8cb27ddf..482f71c766 100644 --- a/goals/coordinators/hands-each-post-to-its-delegates/goal.md +++ b/goals/coordinators/hands-each-post-to-its-delegates/goal.md @@ -42,7 +42,7 @@ - `GOAL_CONTROL=no-round-limit`: a coordinator whose `rounds:` is above 0 runs to the ceiling of 3 (`coordinator-flow.ts`, the worker's config read). Leg e must fail **`e:four-then-none`**, and nothing else: the patch leaves `rounds: 0` alone. - `GOAL_CONTROL=no-delegate-read`: the delegate read answers from the worker's defaults, not the conversation's list (`delegates/worker-delegates.ts`). Leg d must fail **`d:reply-equals-list`** (*the reply equals the list*), with `d:read-equals-list`. -- `GOAL_CONTROL=no-self-choice`: best fit never offers the coordinator itself (`coordinator-flow.ts`). Leg h must fail **`h:hire-turn`**: the EM, its one choice, takes the hire. +- `GOAL_CONTROL=no-self-choice`: best fit never offers the coordinator itself (`coordinator-flow.ts`). Leg h must fail one of its own-job asks (**`h:hire-turn`**, `h:fire-turn`, `h:who-turn`, `h:project-turn`): the EM, its one choice, takes whichever the evaluation places on it. That was the hire when this was written; on 2026-10-24 it was the project, 4 of 4 runs, with the hire missing back to the coordinator's turn. - `GOAL_CONTROL=no-floor`: a delegate pick below `minConfidence:` is used (`best-fit.ts`); a pick with no confidence is still a miss. Leg e must fail **`e:below-floor`**, and nothing else. - `GOAL_CONTROL=turn-after-route`: the coordinator's own turn also runs on a post best fit placed (`coordinator-flow.ts`). Leg a must fail **`a:no-turn`**. diff --git a/goals/coordinators/hands-each-post-to-its-delegates/run.mts b/goals/coordinators/hands-each-post-to-its-delegates/run.mts index 41f807440c..e810defea7 100644 --- a/goals/coordinators/hands-each-post-to-its-delegates/run.mts +++ b/goals/coordinators/hands-each-post-to-its-delegates/run.mts @@ -314,11 +314,12 @@ await runGoal(async () => { failures.push(...r.failures); } if (control !== undefined) { - const named = failures.some((f) => f.startsWith(`${control.assertion} `)); + const wanted = control.anyOf ?? [control.assertion]; + const named = wanted.find((a) => failures.some((f) => f.startsWith(`${a} `))); failures.unshift( - named - ? `control ${control.name}: leg ${control.leg} failed on ${control.assertion}, as it must` - : `control ${control.name}: leg ${control.leg} did NOT fail on ${control.assertion}, so the check can't see what the control removes`, + named !== undefined + ? `control ${control.name}: leg ${control.leg} failed on ${named}, as it must` + : `control ${control.name}: leg ${control.leg} did NOT fail on ${wanted.join(" or ")}, so the check can't see what the control removes`, ); } return { failures, evidence: `legs ${[...legs].sort().join(", ")} PASS; ${evidence.join("; ")}` }; diff --git a/goals/task-board/a-worker-plans-its-own-steps/goal.md b/goals/task-board/a-worker-plans-its-own-steps/goal.md index cfadc22353..8669baaceb 100644 --- a/goals/task-board/a-worker-plans-its-own-steps/goal.md +++ b/goals/task-board/a-worker-plans-its-own-steps/goal.md @@ -25,7 +25,7 @@ **Run:** `pnpm tsx goals/task-board/a-worker-plans-its-own-steps/run.mts`. `GOAL_LEGS=a1,a2` runs some legs. On demand only, never a CI gate. -**Controls:** `GOAL_CONTROL=no-plan` is today's `main` as a scratch patch applied as the process loads (`goals/lib/module-patch.mts`): the agent's turn runs without its plan, and a filing naming the worker is a task handed to a session of its own. Leg a1 must fail **`a1:steps-in-task-session`** and **`a1:no-child-session`**. +**Controls:** `GOAL_CONTROL=no-plan` is today's `main` as a scratch patch applied as the process loads (`goals/lib/module-patch.mts`): the agent's turn runs without its plan, and a filing naming the worker is a task handed to a session of its own. Leg a1 must fail **`a1:steps-in-task-session`**. `a1:no-child-session` goes red with it when the planless model files steps for itself, as it did on 2026-10-21; on 2026-10-24 it answered in one turn instead (4 of 4 runs), so it isn't required. A step that fails, and the answer naming it, is proven in CI by `packages/workforce/test/worker-plans-its-steps.test.ts`: a real model won't fail a step on cue. FIX-2080 had not landed when this did, so a failed attempt's open steps stay open for the next attempt rather than ending `cancelled`; that assertion joins this leg with FIX-2080. diff --git a/goals/task-board/a-worker-plans-its-own-steps/run.mts b/goals/task-board/a-worker-plans-its-own-steps/run.mts index f35ccb146a..3913d9159a 100644 --- a/goals/task-board/a-worker-plans-its-own-steps/run.mts +++ b/goals/task-board/a-worker-plans-its-own-steps/run.mts @@ -59,7 +59,9 @@ const CONTROLS: Record // Today's main: the agent's turn runs without its plan, and a filing that // names the worker is a task like any other, handed to a session of itself. "no-plan": { - assertions: ["a1:steps-in-task-session", "a1:no-child-session"], + // `a1:no-child-session` goes red with it only when the model, planless, files for itself; + // since 2026-10-24 it mostly answers in one turn instead (4 of 4 runs), so it isn't required. + assertions: ["a1:steps-in-task-session"], patches: [ { module: "packages/workforce/src/agent-worker-flow.ts", diff --git a/goals/task-board/every-ending-reaches-its-filer/controls.mts b/goals/task-board/every-ending-reaches-its-filer/controls.mts new file mode 100644 index 0000000000..13f0204323 --- /dev/null +++ b/goals/task-board/every-ending-reaches-its-filer/controls.mts @@ -0,0 +1,28 @@ +/** + * This goal's named control and its kill point, as scratch patches (`goals/lib/module-patch.mts`). + * Exported so a goal that composes this one (FIX-2087's closure walk) reuses them rather than + * copying them. + * FIX-2087's closure imports these `from`/`to` anchors for its `GOAL_CONTROL` legs: renaming one breaks that run too. + */ +import type { ModulePatch } from "../../lib/index.mts"; + +/** Each named control, by its `GOAL_CONTROL` name. */ +export const PATCHES: Record = { + // The sweeper fires no wake: only a fast path or a touch pays a debt. The + // patch skips the fire of each due wake. The wake step still resumes waits + // past their deadline (FIX-2082), a debt this goal does not grade. + "no-sweeper-payment": [ + { + module: "packages/engine/src/durability/durability-sweeper.ts", + from: String.raw` if \(fireWake === undefined\) continue;`, + to: " continue;" + } + ] +}; +/** Leg b's second process dies the moment an ending's delivery would send a notice it owes. */ +export const KILL_AFTER_ENDING: ModulePatch = { + module: "packages/orchestration/src/tasks/notice/delivery.ts", + // The first line of the delivery's send half, after the ending's write. + from: String.raw` const notices = owedNotices\(row, board\.collectionId, rows, filerOf\);`, + to: ' if (process.env.GOAL_KILL === "after-ending" && Object.keys(row.metadata ?? {}).some((k) => k.startsWith("noticeOwed:"))) process.exit(0); const notices = owedNotices(row, board.collectionId, rows, filerOf);' +}; diff --git a/goals/task-board/every-ending-reaches-its-filer/run.mts b/goals/task-board/every-ending-reaches-its-filer/run.mts index bc0fb89d20..93dc9cf3bc 100644 --- a/goals/task-board/every-ending-reaches-its-filer/run.mts +++ b/goals/task-board/every-ending-reaches-its-filer/run.mts @@ -78,6 +78,7 @@ import { silentLogger, type ModulePatch } from "../../lib/index.mts"; +import { KILL_AFTER_ENDING, PATCHES } from "./controls.mts"; const USER = "u_every_ending"; const ORG = "org_every_ending"; @@ -90,26 +91,6 @@ const INTAKE = join(SCRATCH, "intake.ndjson"); const KILL_DB = join(SCRATCH, "kill.sqlite"); const KILL_PLAN = join(SCRATCH, "kill-plan.json"); -const PATCHES: Record = { - // The sweeper fires no wake: only a fast path or a touch pays a debt. The - // patch skips the fire of each due wake. The wake step still resumes waits - // past their deadline (FIX-2082), a debt this goal does not grade. - "no-sweeper-payment": [ - { - module: "packages/engine/src/durability/durability-sweeper.ts", - from: String.raw` if \(fireWake === undefined\) continue;`, - to: " continue;" - } - ] -}; -/** Leg b's second process dies the moment an ending's delivery would send a notice it owes. */ -const KILL_AFTER_ENDING: ModulePatch = { - module: "packages/orchestration/src/tasks/notice/delivery.ts", - // The first line of the delivery's send half, after the ending's write. - from: String.raw` const notices = owedNotices\(row, board\.collectionId, rows, filerOf\);`, - to: ' if (process.env.GOAL_KILL === "after-ending" && Object.keys(row.metadata ?? {}).some((k) => k.startsWith("noticeOwed:"))) process.exit(0); const notices = owedNotices(row, board.collectionId, rows, filerOf);' -}; - if (CONTROL !== "" && PATCHES[CONTROL] === undefined) { console.error(`unknown GOAL_CONTROL "${CONTROL}"; known: ${Object.keys(PATCHES).join(", ")}`); process.exit(2); diff --git a/goals/task-board/runs-by-its-filing-settings/controls.mts b/goals/task-board/runs-by-its-filing-settings/controls.mts new file mode 100644 index 0000000000..f9e12959cb --- /dev/null +++ b/goals/task-board/runs-by-its-filing-settings/controls.mts @@ -0,0 +1,24 @@ +/** + * This goal's named control, as scratch patches (`goals/lib/module-patch.mts`). Exported so a + * goal that composes this one (FIX-2087's closure walk) reuses it rather than copying it. + * FIX-2087's closure imports these `from`/`to` anchors for its `GOAL_CONTROL` legs: renaming one breaks that run too. + */ +import type { ModulePatch } from "../../lib/index.mts"; + +/** Each named control, by its `GOAL_CONTROL` name. */ +export const PATCHES: Record = { + // Both cascades off: no ending records the debt, so nothing pays one, and a + // touch doesn't sweep for dependents an ending missed. + "no-cascade": [ + { + module: "packages/orchestration/src/tasks/collection/internal.ts", + from: String.raw` if \(recording\?\.owesCascade\?\.\(row as Task, ending\) !== true\) return row;`, + to: " return row;" + }, + { + module: "packages/orchestration/src/tasks/collection/cascade.ts", + from: String.raw` await cancelDeadDependents\(writes, now\);`, + to: "" + } + ] +}; diff --git a/goals/task-board/runs-by-its-filing-settings/run.mts b/goals/task-board/runs-by-its-filing-settings/run.mts index b069b757e4..6ce643453b 100644 --- a/goals/task-board/runs-by-its-filing-settings/run.mts +++ b/goals/task-board/runs-by-its-filing-settings/run.mts @@ -67,6 +67,7 @@ import { untilIdle, type ModulePatch } from "../../lib/index.mts"; +import { PATCHES } from "./controls.mts"; const USER = "u_filing_settings"; const ORG = "org_filing_settings"; @@ -81,22 +82,6 @@ const KILL_PLAN = join(SCRATCH, "kill-plan.json"); /** How long a filed piece's worker runs: long enough to still be open when its parent ends. */ const PIECE_MS = 6000; -const PATCHES: Record = { - // Both cascades off: no ending records the debt, so nothing pays one, and a - // touch doesn't sweep for dependents an ending missed. - "no-cascade": [ - { - module: "packages/orchestration/src/tasks/collection/internal.ts", - from: String.raw` if \(recording\?\.owesCascade\?\.\(row as Task, ending\) !== true\) return row;`, - to: " return row;" - }, - { - module: "packages/orchestration/src/tasks/collection/cascade.ts", - from: String.raw` await cancelDeadDependents\(writes, now\);`, - to: "" - } - ] -}; /** The kill leg's second process dies the moment an ending's own pass would pay its cascade. */ const KILL_PATCH: ModulePatch = { module: "packages/orchestration/src/tasks/collection/cascade.ts", diff --git a/goals/workforce-coordination/runs-on-one-task/cast.mts b/goals/workforce-coordination/runs-on-one-task/cast.mts new file mode 100644 index 0000000000..2fbe389bee --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/cast.mts @@ -0,0 +1,144 @@ +/** + * The concept's cast, written as `WORKER.md` files at run time, so the topic, the held-out + * word and the researcher's fact are picked per run and nothing in the repository holds them. + * + * - `desk.launch`: the built-in coordinator, routing everyone, its one delegate `desk.writer`. + * - `desk.writer`: the built-in `agent` worker, delegate `desk.researcher`. It never knows the + * fact: it asks researcher, then fact-checks the fact in a step of its own run in a session of + * its own (`session: "new"`), then writes the draft. + * - `desk.researcher`: the built-in `agent` worker, no delegates. Only its file holds the fact. + * - `desk.chief` and `desk.editor`, for the team that hands out work (journey j1): a coordinator + * routing everyone to `desk.editor`, which files for researcher and waits on it with + * `getTask` in the same request. + * + * Leg c's tree (`variant: "fails"`) gives researcher a model that refuses every call, and has + * writer file a second research task that depends on the first, so the first's failure has a + * dependent to cancel. + */ +import { randomBytes, randomInt } from "node:crypto"; +import { mkdirSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; + +/** What a run picked: Alice's topic and words, and the fact only researcher's file holds. */ +export interface Cast { + topic: string; + /** The held-out word Alice asks the draft to carry. */ + word: string; + /** A second held-out word, sent only as a steer (journey j2). */ + steerWord: string; + fact: string; + /** The digits in the fact, which a draft that used it carries. */ + factMark: string; +} + +const TOPICS = ["a solar lantern", "a folding kayak", "a pocket espresso maker", "a trail camera", "a smart plant pot", "a cordless glue gun"]; +const WORDS = ["heron", "copper", "lantern", "marble", "saffron", "willow", "ember", "glacier", "orchid", "tinsel"]; +const pickWord = () => `${WORDS[randomInt(WORDS.length)]}${randomBytes(2).toString("hex")}`; + +/** Pick a topic, the held-out words and a fact for one run. */ +export function pickCast(): Cast { + const hours = 100 + randomInt(800); + return { + topic: TOPICS[randomInt(TOPICS.length)]!, + word: pickWord(), + steerWord: pickWord(), + fact: `its battery lasts ${hours} hours on one charge`, + factMark: String(hours), + }; +} + +/** The ids the cast goes by. */ +export const CAST = { + launch: "desk.launch", + writer: "desk.writer", + researcher: "desk.researcher", + chief: "desk.chief", + editor: "desk.editor", +} as const; + +const FIRST_STEP = { + walk: `1. If you have not heard back from desk.researcher yet, file exactly one task for it with createTask (assignee "desk.researcher") asking for the product's key fact, and end your turn with one short line saying you are waiting on research. Don't write anything else yet, and don't wait on the task.`, + fails: `1. If you have not heard back from desk.researcher yet, file two tasks for it, both assignee "desk.researcher", one call at a time. First call createTask alone, asking for the product's key fact, and read the taskId it returns. Only then call createTask a second time, asking for a customer quote about that fact, with deps: [that taskId]. Then end your turn with one short line saying you are waiting on research. Don't write anything else yet, and don't wait on either task.`, +}; + +/** + * Write the tree under `dir` (`teams/desk/workers//WORKER.md`). + * @param variant `walk` (legs a and b, the journeys) or `fails` (leg c) + */ +export function writeCast(dir: string, cast: Cast, variant: "walk" | "fails" = "walk"): string { + const worker = (id: string, text: string) => { + const at = join(dir, "teams", "desk", "workers", id); + mkdirSync(at, { recursive: true }); + writeFileSync(join(at, "WORKER.md"), text, "utf8"); + }; + worker( + "launch", + `--- +description: Takes launch writing requests and hands them to the writer. +flow: coordinator +routing: everyone +model: goal/real +delegates: [desk.writer] +--- + +Hand each request to the writer. +`, + ); + worker( + "writer", + `--- +description: Writes launch posts. Asks desk.researcher for product facts. +model: goal/real +delegates: [desk.researcher] +--- + +You are desk.writer. You write short launch posts. You never know a product fact yourself: desk.researcher does. + +If what you are given starts with "Fact-check", it is a step you filed for yourself: file nothing, call no tool, and answer only "Checked: " followed by the fact it names. Nothing else. + +When you are asked to draft a launch post: +${FIRST_STEP[variant]} +2. When desk.researcher's task comes back with the fact, file exactly one step for yourself with createTask: assignee "desk.writer", session "new", goal "Fact-check: " followed by the fact as researcher stated it. Then end your turn with one short line. Don't write the draft yet. +3. When you are told your steps have ended, answer with the finished post: three sentences at most, using the fact exactly as researcher stated it and every word the person asked for, including any word they added while you worked. + +If research fails, answer with one line saying the post can't be written without the fact. + +When you are asked for a new version of a post you already wrote, write it from the earlier version, keeping the fact and the person's words. +`, + ); + worker( + "researcher", + `--- +description: Knows the product facts. +model: ${variant === "fails" ? "goal/broken" : "goal/real"} +--- + +You are desk.researcher. The product fact you know: ${cast.fact}. When you are asked for a fact, answer with that fact in one sentence, exactly as written here. When you are asked for a customer quote, answer with one short made-up quote that mentions the fact. +`, + ); + worker( + "chief", + `--- +description: Takes fact questions and hands them to the editor. +flow: coordinator +routing: everyone +model: goal/real +delegates: [desk.editor] +--- + +Hand each question to the editor. +`, + ); + worker( + "editor", + `--- +description: Answers product questions by asking desk.researcher. +model: goal/real +delegates: [desk.researcher] +--- + +You are desk.editor. You never know a product fact yourself. When you are asked a question about the product, file exactly one task for desk.researcher with createTask asking it, then wait for it in this same turn with getTask, passing the taskId and wait: true. When it comes back, answer in one sentence with the fact exactly as researcher stated it. +`, + ); + return dir; +} diff --git a/goals/workforce-coordination/runs-on-one-task/children.mts b/goals/workforce-coordination/runs-on-one-task/children.mts new file mode 100644 index 0000000000..19903507d3 --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/children.mts @@ -0,0 +1,287 @@ +/** + * Plan row 3: every child's own goal or check, with its controls, on the closure's commit. The + * set is every child of FIX-2056 blocking FIX-2087 when the run starts, read from Linear (with + * `LINEAR_API_KEY`; when it can't be read, row 3 runs the set below and fails), and printed as read. A child with no + * entry in {@link CHECKS} is a failure: the closure can't grade what it can't run. + * + * Plus every goal under `goals/coordinators/` and `goals/hand-offs/`, and FIX-2005's leg of + * `goals/workforce-privacy/`, which the closure rule names. No child goal is skipped as walked + * by row 1: none has every leg asserted there (each is printed with the leg it was walked in). + * + * A control passes when its run FAILs (exit 1) and the output names at least one of its + * must-fail tags as its goal documents them, or, where the goal names a leg rather than tags, + * a failure tagged with that leg. + */ +import { spawnSync } from "node:child_process"; +import { REPO_ROOT } from "../../lib/index.mts"; + +/** One run: a goal (`goals//run.mts`), a package's vitest files, or a goal's model-free check script (`goals/`). */ +export interface Check { + goal?: string; + vitest?: { filter: string; files: string[] }; + script?: string; + env?: Record; + /** + * Each control by name, and every tag its goal says it must fail: all of them must go red. A + * tag ending in `:` is a leg (`d:`), red when any failure in that leg is. Tags a goal says only + * go red "with it" are not required. + */ + controls?: Record; + /** What row 1 walks of it, printed beside it. */ + walked?: string; + /** + * The runs elsewhere in this closure that grade it end to end, for a closure finding whose fix + * landed in a goal another entry already runs, or whose failing check was a row-1 assertion: + * a goal path some entry here runs, a row-1 assertion tag (`a:follow-up`), or `sweep`. Printed + * beside it; one that names no such run is a failure, so a stale reference can't pass silently. + */ + by?: string[]; +} + +/** Each child's checks, by issue id. */ +export const CHECKS: Record = { + "FIX-2076": [ + { goal: "task-board/a-worker-plans-its-own-steps", controls: { "no-plan": ["a1:steps-in-task-session"] }, walked: "a: writer's plan step" }, + { goal: "task-board/refuses-a-board-built-wrong", controls: { "no-fence": ["b:refused"] } }, + ], + "FIX-2077": [{ goal: "task-board/settles-when-its-work-is-over", controls: { "settle-at-return": ["a:after-the-side-chains"] }, walked: "a: writer's task stays running on its research" }], + "FIX-2078": [{ goal: "task-board/every-ending-reaches-its-filer", controls: { "no-sweeper-payment": ["b:the filer hears within due time", "e:"] }, walked: "b, and a's once" }], + "FIX-2080": [{ goal: "task-board/runs-by-its-filing-settings", controls: { "no-cascade": ["d:the open filed task is cancelled", "d:the dependent chain is cancelled"] }, walked: "c: the dependent cancelled" }], + "FIX-2081": [{ goal: "task-tools/five-tools-one-rule-per-field", controls: { "roster-reach": ["c:a piece for a non-delegate is refused"], "edit-any-state": ["b:running:", "b:finished:"] }, walked: "a: Alice's list; j1's tools" }], + "FIX-2082": [{ goal: "task-board/waits-on-a-task-it-filed", controls: { "deadline-cancels": ["c:at the deadline the task is in_progress on attempt 1", "c:a second wait gets it completed"], "drain-moves-on": ["d:"] }, walked: "a: the drain waits on a dispatched step; j1" }], + "FIX-2083": [{ goal: "coordinators/routes-each-message-as-a-task", controls: { "round-timer": ["b:restart"], "post-map-off": ["e:legacy"] }, walked: "a: the routed task" }], + "FIX-2084": [{ goal: "workers/roles-checked-at-hire", controls: { "no-role-check": ["a:refused"] } }], + "FIX-2085": [{ goal: "scheduled-work/recurring-work-files-a-task", controls: { "memory-dedupe": ["b:exactly-one-task"] } }], + "FIX-2086": [{ goal: "task-tools/steer-and-follow-up", controls: { "drop-late-steer": ["b:one more turn before the settle"], reopen: ["d:the original's output reads byte-identical"] }, walked: "a: the follow-up; j2: the steer" }], + "FIX-2088": [ + { + goal: "task-board/background-error-fails-the-attempt", + controls: { "drop-background-failures": ["a:done-on-attempt-2", "f:done-on-attempt-2", "b:failed-after-1-attempt", "c:failed-after-1-attempt", "h:failed-after-1-attempt", "j:failed-after-1-attempt", "i:failed-after-1-attempt", "k:done-on-attempt-2"], "no-conversation-carve-out": ["k:conversation-done-on-attempt-1"] }, + }, + ], + "FIX-2090": [ + { vitest: { filter: "@flow-state-dev/harness-manager", files: ["test/claim-board-refused.spec.ts"] } }, + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/task-board/record-error-ends-for-good.test.ts", "test/collection/advisory-fail-no-retry.test.ts"] } }, + ], + "FIX-2096": [ + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/task-board/drain-pays-owed-cascade.test.ts", "test/collection/owes-a-touch.test.ts", "test/collection/filing-settings.test.ts", "test/skills/task-tools-capability.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/workforce", files: ["test/conversation-board-filing-settings.test.ts", "test/worker-plans-its-steps.test.ts"] } }, + ], + // The closure's own findings, each a child blocking FIX-2087 once filed: its regression tests, + // and the run that found it, which now grades it (`by`). + "FIX-2097": [{ vitest: { filter: "@flow-state-dev/store-sqlite", files: ["test/write-after-close.test.ts"] }, by: ["workforce/split-to-roster"] }], + "FIX-2110": [{ vitest: { filter: "@flow-state-dev/workforce", files: ["test/worker-plans-its-steps.test.ts"] }, by: ["a:answers-after-steps", "a:lands-once", "task-board/waits-on-a-task-it-filed"] }], + "FIX-2111": [ + { vitest: { filter: "@flow-state-dev/workforce", files: ["test/task-session-stays-open.test.ts", "test/project-coordinator.test.ts"] }, by: ["a:follow-up", "a:once", "task-tools/steer-and-follow-up"] }, + ], + "FIX-2112": [ + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/tasks/wait-on-task.test.ts", "test/tasks/task-notice.test.ts"] }, by: ["j1:read-once", "j1:heard-once", "task-board/waits-on-a-task-it-filed"] }, + ], + "FIX-2113": [ + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/tasks/notice-defer.test.ts", "test/tasks/pass.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/workforce", files: ["test/steer-while-filed-work-open.test.ts"] }, by: ["j2:steer-shows"] }, + ], + "FIX-2114": [ + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/collection/deps-read-committed.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/workforce", files: ["test/deps-read-committed.test.ts"] }, by: ["task-board/runs-by-its-filing-settings"] }, + ], + "FIX-2115": [{ vitest: { filter: "@flow-state-dev/workforce", files: ["test/coordinator-routed-tasks.test.ts"] }, by: ["coordinators/files-tasks-down-the-owners-chain"] }], + "FIX-2116": [{ by: ["coordinators/hands-each-post-to-its-delegates"] }], + "FIX-2117": [{ by: ["task-board/a-worker-plans-its-own-steps"] }], + "FIX-2118": [{ by: ["task-board/every-ending-reaches-its-filer"] }], + "FIX-2119": [{ by: ["sweep"] }], + "FIX-2120": [{ by: ["goal-seek-loop/replans-until-done"] }], + "FIX-2121": [{ by: ["hand-offs/ask-survives-a-restart", "hand-offs/survives-a-restart-and-runs-once"] }], + "FIX-2122": [{ vitest: { filter: "@flow-state-dev/orchestration", files: ["test/collection/filing-settings.test.ts"] }, by: ["a:follow-up", "task-tools/steer-and-follow-up"] }], + "FIX-2123": [ + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/collection/notice-take.test.ts", "test/tasks/notice-claim.test.ts", "test/tasks/delivery-after-ending.test.ts", "test/tasks/notice-defer.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/engine", files: ["test/wait-chain.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/workforce", files: ["test/conversation-board-wait.test.ts"] }, by: ["j1:read-once", "j1:heard-once", "task-board/waits-on-a-task-it-filed"] }, + ], + "FIX-2135": [ + { vitest: { filter: "@flow-state-dev/engine", files: ["test/wait-resume.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/tasks/resume-owed-isolation.test.ts"] }, by: ["j1:waits-in-request", "task-board/waits-on-a-task-it-filed"] }, + ], + "FIX-2136": [ + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/tasks/cascade-notice-unseen-dependent.test.ts"] }, by: ["c:one-failure-notice", "task-board/runs-by-its-filing-settings"] }, + ], + // A failure heard through the filer's own getTask wait: no leg here walks it (j1's research + // succeeds; c hears its failure by notice), so its regression test is its whole check. + "FIX-2138": [{ vitest: { filter: "@flow-state-dev/orchestration", files: ["test/tasks/get-task-names-cascaded.test.ts"] } }], + // Not a product bug: the privacy goal misread a routing coordinator, whose delegate answers in a + // request of its own after the post's. Its fix is in that goal, which NAMED runs. + "FIX-2147": [{ script: "workforce-privacy/two-users-share-a-project-and-nothing-else/check-talk.mts", by: ["workforce-privacy/two-users-share-a-project-and-nothing-else"] }], + // FIX-2149 and FIX-2150 are races inside the park window: a notice held or a step ended, or a + // wait's deadline passing, in the milliseconds while a request parks. A real model's turns take + // seconds, so no leg or goal here lands in that window: their regression tests, which inject the + // timing, are their whole check. + "FIX-2149": [ + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/tasks/delivery-after-ending.test.ts", "test/tasks/notice-claim.test.ts", "test/tasks/task-notice.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/workforce", files: ["test/worker-plans-its-steps.test.ts"] } }, + ], + "FIX-2150": [ + { vitest: { filter: "@flow-state-dev/engine", files: ["test/wait-resume.test.ts"] } }, + { vitest: { filter: "@flow-state-dev/orchestration", files: ["test/tasks/wait-on-task.test.ts"] } }, + ], +}; + +/** The goals the closure rule names besides the children's: coordinators, hand-offs, FIX-2005's privacy leg, and the goal-seek loop (journey j3). */ +export const NAMED: Check[] = [ + { goal: "coordinators/every-coordinator-is-a-worker-file", controls: { "mailbox-left": ["a:no-mailbox-file", "b:after"] } }, + { goal: "coordinators/files-tasks-down-the-owners-chain", controls: { unpartitioned: ["c:runs-its-own", "c:lists-its-own"], "no-follow-up": ["a:one-notice", "e:one-notice"] } }, + { + goal: "coordinators/hands-each-post-to-its-delegates", + controls: { + "no-roster-check": ["f:bobs-worker-refused"], + "no-round-limit": ["e:four-then-none"], + "no-delegate-read": ["d:reply-equals-list", "d:read-equals-list"], + // One of leg h's own-job asks, whichever best fit places on the EM (its goal.md). + "no-self-choice": ["h:"], + "no-floor": ["e:below-floor"], + "turn-after-route": ["a:no-turn"], + }, + }, + { goal: "coordinators/routes-a-follow-up-by-its-conversation", controls: { "no-recent": ["followup:delegate"] } }, + { goal: "coordinators/task-session-stays-open", controls: { "new-session": ["a:names-ticket", "c:names-ticket"], "drop-answer": ["a:names-region"] } }, + { goal: "hand-offs/ask-survives-a-restart", controls: { "no-waker": ["a:completed"] } }, + { goal: "hand-offs/survives-a-restart-and-runs-once" }, + { goal: "workforce-privacy/two-users-share-a-project-and-nothing-else", env: { GOAL_ONLY: "milestone", PLAYWRIGHT_BROWSERS_PATH: "/opt/pw-browsers" } }, + { goal: "workforce/split-to-roster", controls: { "first-answer": ["1:report-is-output"], "hold-session": ["1:waiting-while-b-works"], "wake-coordinator": ["4:desk-hears-tasks-only"] } }, + { goal: "goal-seek-loop/replans-until-done", walked: "j3: a goal-seek loop that replans; refuses-a-board-built-wrong holds its delegation-board refusal" }, +]; + +/** The children blocking FIX-2087, read from Linear. Throws when the set can't be read: no key, a failed request, or an answer without the issue's relations. */ +async function childrenFromLinear(): Promise> { + const key = process.env.LINEAR_API_KEY ?? ""; + if (key === "") throw new Error("LINEAR_API_KEY is unset"); + const response = await fetch("https://api.linear.app/graphql", { + method: "POST", + headers: { authorization: key, "content-type": "application/json" }, + body: JSON.stringify({ query: 'query { issue(id: "FIX-2087") { inverseRelations(first: 100) { nodes { type issue { identifier title parent { identifier } } } } } }' }), + }); + if (!response.ok) throw new Error(`Linear answered HTTP ${response.status}`); + const body = (await response.json()) as any; + if (body.errors !== undefined) throw new Error(`Linear answered with errors: ${JSON.stringify(body.errors).slice(0, 300)}`); + const nodes = body.data?.issue?.inverseRelations?.nodes; + if (!Array.isArray(nodes)) throw new Error("Linear's answer holds no relations for FIX-2087"); + return nodes + .filter((n: any) => n.type === "blocks" && n.issue.parent?.identifier === "FIX-2056") + .map((n: any) => ({ id: n.issue.identifier, title: n.issue.title })); +} + +/** Every goal some entry runs, for checking `by`. */ +const RUN_GOALS = new Set([...Object.values(CHECKS).flat(), ...NAMED].flatMap((c) => (c.goal === undefined ? [] : [c.goal]))); + +/** Report a check's `by`, and fail one that names no run here (`tags` are row 1's assertions). */ +function checkBy(owner: string, check: Check, tags: readonly string[], failures: string[], report: string[]): void { + if (check.by === undefined) return; + const stale = check.by.filter((b) => !(b === "sweep" || tags.includes(b) || RUN_GOALS.has(b))); + report.push(` ${owner} graded by ${check.by.join(", ")}`); + if (stale.length > 0) failures.push(`children:${owner} — graded by ${stale.join(", ")}, which no run in this closure holds`); +} + +/** + * Each run row 3 made and whether it passed, for row 4: a goal by its path, a control as + * ` GOAL_CONTROL=` (passed when it went red as documented), a test file as + * ` `. A run made twice passes only if both did. + */ +export const RAN = new Map(); +const record = (key: string, ok: boolean) => RAN.set(key, (RAN.get(key) ?? true) && ok); + +/** + * The bullets of a goal's `FAIL —` list (goals/lib/verdict.mts prints each failure as a line + * starting ` - `), each the text after its dash, a leading `[` dropped. Lines indented further + * continue a bullet and are not bullets. + */ +function failureBullets(text: string): string[] { + return text.split("\n").flatMap((line) => (/^ {2}- /.test(line) ? [line.slice(4).replace(/^\[/, "")] : [])); +} + +/** Whether a bullet is `tag`'s failure: it starts with the whole tag (`a:x — …`, `[a:x] …`); a leg tag (`d:`), any bullet in that leg. */ +const bulletIs = (bullet: string, tag: string) => bullet.startsWith(tag) && (tag.endsWith(":") || /^($|[\s\]·:])/.test(bullet.slice(tag.length))); + +/** + * A control run's verdict: red only when the goal exits 1, every required tag has a failure + * bullet of its own, and no bullet is the goal saying a control of its own didn't go red + * (`control : did NOT fail …`, or red outside its expected set). Such a bullet + * carries the tag in its text, so reading the output for the tag anywhere would take it for red. + */ +export function controlVerdict(text: string, status: number | null, tags: readonly string[]): { red: boolean; green: string[]; broken: string[] } { + const bullets = failureBullets(text); + const green = tags.filter((t) => !bullets.some((b) => bulletIs(b, t))); + const broken = bullets.filter((b) => /^control\b/i.test(b) && /did not fail|stayed green|is broken|red outside/i.test(b)); + return { red: status === 1 && green.length === 0 && broken.length === 0, green, broken }; +} + +/** Run one check (and its controls); push a failure per red run or control that stayed green. */ +function runCheck(owner: string, check: Check, failures: string[], report: string[], say: (s: string) => void): void { + const runs: Array<{ control?: string; args: string[]; cmd: string }> = []; + if (check.goal !== undefined) { + const args = ["tsx", `goals/${check.goal}/run.mts`]; + runs.push({ args, cmd: "pnpm" }); + for (const name of Object.keys(check.controls ?? {})) runs.push({ control: name, args, cmd: "pnpm" }); + } else if (check.vitest !== undefined) { + runs.push({ cmd: "pnpm", args: ["--filter", check.vitest.filter, "exec", "vitest", "run", ...check.vitest.files] }); + } else if (check.script !== undefined) { + runs.push({ cmd: "pnpm", args: ["tsx", `goals/${check.script}`] }); + } + for (const run of runs) { + const label = `${owner} ${check.goal ?? check.script ?? `${check.vitest!.filter} ${check.vitest!.files.join(" ")}`}${run.control === undefined ? "" : ` GOAL_CONTROL=${run.control}`}`; + say(`row 3: ${label}`); + const started = Date.now(); + const out = spawnSync(run.cmd, run.args, { + cwd: REPO_ROOT, + encoding: "utf8", + timeout: 3_600_000, + maxBuffer: 256 * 1024 * 1024, + env: { ...process.env, ...check.env, GOAL_CONTROL: run.control ?? "" }, + }); + const text = `${out.stdout}${out.stderr}`; + const verdict = /^(PASS|FAIL|CAN'T TELL)\b.*$/m.exec(text)?.[0]?.slice(0, 160) ?? `exit ${out.status}`; + const secs = Math.round((Date.now() - started) / 1000); + if (run.control === undefined) { + report.push(` ${out.status === 0 ? "PASS" : "FAIL"} ${label} (${secs}s)${check.walked ? ` · row 1 walks ${check.walked}` : ""}: ${verdict}`); + if (out.status !== 0) failures.push(`children:${owner} — ${label} ${verdict}: ${(/FAIL —[\s\S]{0,1200}/.exec(text)?.[0] ?? text.slice(-1200)).replace(/\n/g, " | ")}`); + if (check.goal !== undefined) record(check.goal, out.status === 0); + else if (check.script !== undefined) record(check.script, out.status === 0); + else for (const file of check.vitest!.files) record(`${check.vitest!.filter} ${file}`, out.status === 0); + } else { + const tags = check.controls![run.control]!; + const { red, green, broken } = controlVerdict(text, out.status, tags); + record(`${check.goal} GOAL_CONTROL=${run.control}`, red); + const why = [green.length > 0 ? `green: ${green.join(", ")}` : "", ...broken.map((b) => `the goal says: ${b.slice(0, 160)}`)].filter(Boolean).join("; "); + report.push(` ${red ? "FAIL (expected)" : "CONTROL BROKEN"} ${label} (${secs}s): wanted every one of ${tags.join(", ")} red${why === "" ? "" : `; ${why}`}; ${verdict}`); + if (!red) failures.push(`children:${owner} — control ${run.control} of ${check.goal} did not fail as documented (exit ${out.status}; ${why || "no failure list"}): ${text.slice(-800).replace(/\n/g, " | ")}`); + } + } +} + +/** Run row 3. `tags` are row 1's assertions, which a check's `by` may name. */ +export async function runChildren(failures: string[], report: string[], say: (s: string) => void, tags: readonly string[]): Promise { + // Without the live set, row 3 can't tell a child it has no check for: it still runs the + // built-in set, and fails. + const read = await childrenFromLinear().catch((error: unknown) => { + failures.push(`children:cant-tell — the children blocking FIX-2087 weren't read from Linear (${error instanceof Error ? error.message : String(error)}), so a child with no check here would go unseen`); + return undefined; + }); + const blocking = read ?? Object.keys(CHECKS).map((id) => ({ id, title: "(not read from Linear)" })); + // A child with a check here that Linear doesn't list as blocking still runs: a closure fix + // filed without the blocking relation is still a fix the closure rests on. + const unlisted = Object.keys(CHECKS).filter((id) => !blocking.some((c) => c.id === id)); + const set = [...blocking, ...unlisted.map((id) => ({ id, title: "(has a check here; not blocking FIX-2087 in Linear)" }))]; + report.push(`row 3: ${blocking.length} children blocking FIX-2087${read === undefined ? ", the built-in set (Linear not read)" : ", read from Linear"}, and ${unlisted.length} more with a check here:`); + for (const c of set) report.push(` ${c.id} ${c.title}`); + report.push(`row 3 skips: none (no child goal has every leg asserted by row 1)`); + for (const c of set) { + const checks = CHECKS[c.id]; + if (checks === undefined) { + failures.push(`children:${c.id} — a child blocking the closure has no check here`); + continue; + } + for (const check of checks) { + runCheck(c.id, check, failures, report, say); + checkBy(c.id, check, tags, failures, report); + } + } + for (const check of NAMED) runCheck("named", check, failures, report, say); +} diff --git a/goals/workforce-coordination/runs-on-one-task/engine.mjs b/goals/workforce-coordination/runs-on-one-task/engine.mjs new file mode 100644 index 0000000000..ca68bda679 --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/engine.mjs @@ -0,0 +1,508 @@ +/** + * FIX-2087 task-surface engine: reads the three rules of `rules.mjs` off a checkout and + * returns every site it classified. The goal (`run.mts`) and CI (`sweep.mjs`) both call + * {@link sweepTaskSurface}, so they can't give different answers on one commit (D2). + * + * The AST rules (tools, the type-read removed matchers, endings) read one TypeScript program + * over the product code under `packages/` and `apps/`, resolved through the repository's own + * `tsconfig.base.json` paths, so no build is needed. The text matchers read every tracked file + * under the swept roots that `rules.mjs` → `EXCLUDE` doesn't name. + * + * `plants` adds files in memory (never on disk), read through the same paths as a real file: + * `--control` uses it to prove each rule still reports what it exists to catch. + */ +import { execFileSync } from "node:child_process"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import ts from "typescript"; +import { + BEHAVIOUR_ROWS, + CODE, + EXCLUDE, + FORWARDERS, + KEEP, + MIGRATION_TABLE, + PASS_THROUGHS, + RECORDERS, + REFUSAL_MARKER, + REMOVED, + ROOTS, + TEST_CODE, + TOOLS_ALLOWED, +} from "./rules.mjs"; + +/** + * Sweep the checkout at `repo`: its tracked files, or `tracked` when given (a tree extracted + * from another commit, which has no index), reading the migration table at `table` when given. + * @param {{ repo: string, plants?: Array<{ path: string, text: string }>, tracked?: string[], table?: string }} options + * @returns {{ findings: Array<{ rule: string, where: string, what: string }>, cantTell: string[], scanned: Record, rows: Array<{ row: string, by: string }>, tools: string[], recorders: Record, files: { tracked: number, swept: number, excluded: Map, program: number } }} + */ +export function sweepTaskSurface({ repo, plants = [], tracked: given, table: tablePath }) { + const planted = new Map(plants.map((p) => [p.path, p.text])); + const tracked = given ?? execFileSync("git", ["ls-files", "--", ...ROOTS], { cwd: repo, encoding: "utf8" }).split("\n").filter(Boolean); + const all = [...new Set([...tracked, ...planted.keys()])]; + const read = (path) => planted.get(path) ?? readFileSync(join(repo, path), "utf8"); + const findings = []; + const scanned = { tools: 0, removedText: 0, removedTyped: 0, endings: 0, marked: 0, kept: 0 }; + const isMarked = (line) => line.includes(REFUSAL_MARKER); + + // ---- the swept files ------------------------------------------------------------ + const excluded = new Map(); + const swept = []; + for (const path of all) { + const rule = EXCLUDE.find((r) => r.path.test(path) && !(r.notIf && r.notIf.test(path))); + if (rule) excluded.set(rule.why, (excluded.get(rule.why) ?? 0) + 1); + else swept.push(path); + } + + // ---- the migration table: every name row has a matcher, every behaviour row a goal -------- + const table = readFileSync(tablePath ?? join(repo, MIGRATION_TABLE), "utf8"); + const removedCells = []; + let inRemoved = false; + for (const line of table.split("\n")) { + if (/^\| Removed \|/.test(line)) inRemoved = true; + else if (/^\| Changed \|/.test(line)) inRemoved = false; + else if (inRemoved && /^\| (?!---)/.test(line)) removedCells.push(line.split("|")[1].trim()); + } + const rows = []; + for (const cell of removedCells) { + const matchers = REMOVED.filter((m) => cell.includes(m.row)); + const goal = Object.entries(BEHAVIOUR_ROWS).find(([phrase]) => cell.includes(phrase))?.[1]; + if (matchers.length === 0 && goal === undefined) { + findings.push({ rule: "removed", where: MIGRATION_TABLE, what: `a row no matcher or goal covers: ${cell.slice(0, 120)}` }); + } + rows.push({ row: cell, by: [...matchers.map((m) => `${m.kind} matcher`), ...(goal ? [`goal ${goal}`] : [])].join(" + ") || "NOTHING" }); + } + + // ---- removed · text matchers over the swept files --------------------------------------- + const textMatchers = REMOVED.filter((m) => m.kind === "text"); + for (const path of swept) { + const applicable = textMatchers.filter((m) => m.files.test(path) && !(m.notFiles && m.notFiles.test(path))); + if (applicable.length === 0) continue; + const lines = read(path).split("\n"); + const isCodeFile = CODE.test(path); + lines.forEach((line, i) => { + // A comment says what code does; a removed name in one is history, not a live surface. + if (isCodeFile && /^\s*(\/\/|\*|\/\*)/.test(line)) return; + for (const m of applicable) { + if (!m.pattern.test(line)) continue; + scanned.removedText++; + if (isMarked(line)) { + scanned.marked++; + continue; + } + if (KEEP.some((k) => k.path.test(path) && k.line.test(line.trim()))) { + scanned.kept++; + continue; + } + findings.push({ rule: "removed", where: `${path}:${i + 1}`, what: `${m.row} (${m.owner}) live: ${line.trim().slice(0, 140)}` }); + } + }); + } + + // ---- the program -------------------------------------------------------------------------- + const productCode = all.filter((p) => CODE.test(p) && !TEST_CODE.test(p)); + const config = ts.parseJsonConfigFileContent(ts.readConfigFile(join(repo, "tsconfig.base.json"), ts.sys.readFile).config, ts.sys, repo); + const options = { ...config.options, noEmit: true, skipLibCheck: true, allowJs: false, jsx: ts.JsxEmit.Preserve, allowImportingTsExtensions: true }; + const host = ts.createCompilerHost(options); + const plainRead = host.readFile.bind(host); + const plainExists = host.fileExists.bind(host); + const abs = (p) => join(repo, p); + const plantedAbs = new Map([...planted].map(([p, t]) => [abs(p), t])); + host.readFile = (f) => plantedAbs.get(f) ?? plainRead(f); + host.fileExists = (f) => plantedAbs.has(f) || plainExists(f); + const baseGetSourceFile = host.getSourceFile.bind(host); + host.getSourceFile = (f, lang, onError, create) => + plantedAbs.has(f) ? ts.createSourceFile(f, plantedAbs.get(f), lang, true) : baseGetSourceFile(f, lang, onError, create); + const program = ts.createProgram({ rootNames: productCode.map(abs), options, host }); + const checker = program.getTypeChecker(); + const rel = (sf) => sf.fileName.slice(repo.length + 1); + const ownFiles = program.getSourceFiles().filter((sf) => sf.fileName.startsWith(repo) && !sf.fileName.includes("/node_modules/") && productCode.includes(rel(sf))); + const packageOf = (path) => /^(packages|apps)\/([^/]+)\//.exec(path)?.[2] ?? "?"; + const lineOf = (sf, node) => sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1; + const lineText = (sf, node) => sf.text.split("\n")[lineOf(sf, node) - 1] ?? ""; + + // ---- tools · every task tool the capability declares -------------------------------------- + // A tool is a call of one of core's five block builders, read by symbol: any import name, a + // namespace member, a local alias, or `.withDefaults`. Read in the file that defines + // `createTaskToolsCapability` and, transitively, the files it imports from that same directory. + // A tool imported from another directory is not read: the task board's handler blocks live + // there and are not tools. An allowed name that was not read is CAN'T TELL. + const cantTell = []; + const resolveAlias = (sym) => (sym && sym.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(sym) : sym); + const symbolOf = (expr) => resolveAlias(checker.getSymbolAtLocation(ts.isPropertyAccessExpression(expr) ? expr.name : expr)); + // Any of core's five block builders: a generator's tool is any block (`GeneratorTool`). + const CORE_BUILDER = /\/packages\/core\/(src|dist)\/blocks\/(handler|generator|sequencer|router|evaluator)\.(d\.)?ts$/; + const BUILDERS = ["handler", "generator", "sequencer", "router", "evaluator"]; + const isCoreHandler = (sym) => + (sym?.declarations ?? []).some((d) => CORE_BUILDER.test(d.getSourceFile().fileName) && (ts.isFunctionDeclaration(d) || ts.isModuleDeclaration(d)) && BUILDERS.includes(d.name?.text)); + /** Whether `expr` is core's `handler`, through aliases, local variables, conditionals and `.withDefaults`. */ + const buildsTool = (expr, depth = 0) => { + if (expr === undefined || depth > 8) return false; + if (ts.isParenthesizedExpression(expr) || ts.isAsExpression(expr) || ts.isNonNullExpression(expr) || ts.isSatisfiesExpression?.(expr)) return buildsTool(expr.expression, depth + 1); + if (ts.isConditionalExpression(expr)) return buildsTool(expr.whenTrue, depth + 1) || buildsTool(expr.whenFalse, depth + 1); + if (ts.isBinaryExpression(expr)) return buildsTool(expr.left, depth + 1) || buildsTool(expr.right, depth + 1); + if (ts.isCallExpression(expr) && ts.isPropertyAccessExpression(expr.expression) && expr.expression.name.text === "withDefaults") return buildsTool(expr.expression.expression, depth + 1); + if (!ts.isIdentifier(expr) && !ts.isPropertyAccessExpression(expr)) return false; + const sym = symbolOf(expr); + if (isCoreHandler(sym)) return true; + return (sym?.declarations ?? []).some((d) => ts.isVariableDeclaration(d) && d.initializer !== undefined && buildsTool(d.initializer, depth + 1)); + }; + const nameOfTool = (obj) => { + const prop = obj.properties.find((p) => ts.isPropertyAssignment(p) && p.name.getText() === "name"); + if (!prop) return "(no name read)"; + const init = prop.initializer; + if (ts.isStringLiteralLike(init)) return init.text; + if (ts.isCallExpression(init) && init.expression.getText() === "named" && init.arguments[0] && ts.isStringLiteralLike(init.arguments[0])) return init.arguments[0].text; + return `(computed: ${init.getText()})`; + }; + const byFile = new Map(ownFiles.map((sf) => [sf.fileName, sf])); + const capabilityFiles = ownFiles.filter((sf) => sf.statements.some((s) => ts.isFunctionDeclaration(s) && s.name?.text === "createTaskToolsCapability")); + const toolScope = new Set(); + const dirOf = (f) => f.slice(0, f.lastIndexOf("/") + 1); + const pending = capabilityFiles.map((sf) => [sf, dirOf(sf.fileName)]); + while (pending.length > 0) { + const [sf, dir] = pending.pop(); + if (toolScope.has(sf)) continue; + toolScope.add(sf); + for (const s of sf.statements) { + if (!(ts.isImportDeclaration(s) || ts.isExportDeclaration(s)) || !s.moduleSpecifier || !ts.isStringLiteral(s.moduleSpecifier)) continue; + const to = ts.resolveModuleName(s.moduleSpecifier.text, sf.fileName, options, host).resolvedModule?.resolvedFileName; + const next = to === undefined ? undefined : byFile.get(to); + if (next !== undefined && dirOf(next.fileName) === dir) pending.push([next, dir]); + } + } + const tools = []; + for (const sf of toolScope) { + const visit = (node) => { + if (ts.isCallExpression(node) && buildsTool(node.expression)) { + let config = node.arguments[0]; + while (config !== undefined && (ts.isAsExpression(config) || ts.isParenthesizedExpression(config) || ts.isSatisfiesExpression?.(config))) config = config.expression; + const name = config !== undefined && ts.isObjectLiteralExpression(config) ? nameOfTool(config) : `(config not read: ${config?.getText(sf).slice(0, 60) ?? "none"})`; + scanned.tools++; + tools.push(name); + if (!TOOLS_ALLOWED.has(name) && !isMarked(lineText(sf, node))) { + findings.push({ rule: "tools", where: `${rel(sf)}:${lineOf(sf, node)}`, what: `the task surface offers "${name}", which is not one of the five, answerTask or parkOnQuestion` }); + } + } + ts.forEachChild(node, visit); + }; + visit(sf); + } + if (capabilityFiles.length === 0) cantTell.push("tools: no file defines createTaskToolsCapability"); + const unread = [...TOOLS_ALLOWED].filter((n) => !tools.includes(n)); + if (unread.length > 0) cantTell.push(`tools: the allowed tools ${unread.join(", ")} were not read, so the rule stopped reading the capability`); + + // ---- removed · typed matchers (declared props, members called, union members, doors) ----- + const typeDecls = (name, module) => + ownFiles + .filter((sf) => module === undefined || rel(sf).startsWith(`packages/${module.split("/")[0]}/src/`)) + .flatMap((sf) => sf.statements.filter((s) => (ts.isTypeAliasDeclaration(s) || ts.isInterfaceDeclaration(s)) && s.name.text === name).map((s) => ({ sf, s }))); + for (const m of REMOVED.filter((x) => x.kind === "declared" || x.kind === "union")) { + const decls = typeDecls(m.type, m.module); + if (decls.length === 0) { + findings.push({ rule: "removed", where: MIGRATION_TABLE, what: `CAN'T TELL: ${m.row}: no type ${m.type}${m.module ? ` in ${m.module}` : ""} to read` }); + continue; + } + for (const { sf, s } of decls) { + const type = checker.getTypeAtLocation(s.name); + scanned.removedTyped++; + if (m.kind === "declared") { + for (const prop of m.props) { + const sym = type.getProperty(prop) ?? checker.getApparentType(type).getProperty?.(prop); + if (!sym) continue; + const decl = sym.declarations?.[0]; + const dsf = decl?.getSourceFile(); + if (decl && dsf && isMarked(dsf.text.split("\n")[dsf.getLineAndCharacterOfPosition(decl.getStart()).line] ?? "")) continue; + findings.push({ rule: "removed", where: `${rel(sf)}:${lineOf(sf, s)}`, what: `${m.row} (${m.owner}) live: ${m.type}.${prop} is declared` }); + } + } else { + const members = type.isUnion() ? type.types : [type]; + for (const member of members) { + if (member.isStringLiteral() && m.members.includes(member.value)) { + findings.push({ rule: "removed", where: `${rel(sf)}:${lineOf(sf, s)}`, what: `${m.row} (${m.owner}) live: ${m.type} still has "${member.value}"` }); + } + } + } + } + } + const memberRules = REMOVED.filter((x) => x.kind === "member"); + const doorRule = REMOVED.find((x) => x.kind === "door"); + /** Whether `expr` resolves, by symbol, to core's `defineFlow`. */ + const isCoreDefineFlow = (expr) => (symbolOf(expr)?.declarations ?? []).some((d) => /\/packages\/core\/(src|dist)\/flow\/defineFlow\.(d\.)?ts$/.test(d.getSourceFile().fileName)); + /** The worker contract's keys: the shape `workerConfigSchema()` returns, read from its declaration. */ + const contractKeys = (() => { + for (const sf of ownFiles) { + if (!rel(sf).startsWith("packages/workforce/src/")) continue; + const decl = sf.statements.find((s) => ts.isFunctionDeclaration(s) && s.name?.text === "workerConfigSchema"); + if (decl === undefined) continue; + const shape = checker.getReturnTypeOfSignature(checker.getSignatureFromDeclaration(decl)).getProperty("shape"); + return shape === undefined ? [] : checker.getTypeOfSymbolAtLocation(shape, decl).getProperties().map((p) => p.name); + } + return []; + })(); + if (doorRule && contractKeys.length === 0) cantTell.push("removed: workerConfigSchema's keys weren't read, so a worker flow can't be told"); + /** Whether a `defineFlow` config's `configSchema` is typed with every key of the worker contract. */ + const isWorkerConfig = (config) => { + if (contractKeys.length === 0) return false; + const prop = config.properties.find((p) => (ts.isPropertyAssignment(p) || ts.isShorthandPropertyAssignment(p)) && p.name.getText() === "configSchema"); + if (prop === undefined) return false; + const shape = checker.getApparentType(checker.getTypeAtLocation(ts.isPropertyAssignment(prop) ? prop.initializer : prop.name)).getProperty("shape"); + if (shape === undefined) return false; + const keys = new Set(checker.getTypeOfSymbolAtLocation(shape, prop).getProperties().map((p) => p.name)); + return contractKeys.every((k) => keys.has(k)); + }; + /** An action's key: a literal name, or a computed one whose type is a string literal (`[RUN_ACTION]`). */ + const actionKey = (p, sf) => { + if (p.name === undefined) return undefined; + if (ts.isComputedPropertyName(p.name)) { + const t = checker.getTypeAtLocation(p.name.expression); + return t.isStringLiteral() ? t.value : undefined; + } + return p.name.getText(sf).replace(/["']/g, ""); + }; + const workerFlows = []; + + // ---- endings · read by type --------------------------------------------------------------- + // A task collection is any value whose type has the collection's lifecycle: claim, complete, + // fail and cancel. Its `complete` or `fail` called, or its `cancel` called with the caller's + // own claim (in any shape: `.call`, `.apply`, element access, destructured), is an ending; so + // is a cancel handed on (`.bind`, passed as a value), and a call of a pass-through helper. + const isCollection = (type) => { + const t = checker.getApparentType(checker.getNonNullableType(type)); + return ["claim", "complete", "fail", "cancel"].every((p) => t.getProperty(p) !== undefined); + }; + /** Whether a method's symbol is declared on a task collection interface (a `Pick` of one keeps it). */ + const declaredOnCollection = (name) => + (checker.getSymbolAtLocation(name)?.declarations ?? []).some((d) => { + for (let n = d.parent; n; n = n.parent) if (ts.isInterfaceDeclaration(n)) return /^TaskCollection/.test(n.name.text); + return false; + }); + const passesClaim = (arg) => { + if (arg === undefined) return false; + const t = checker.getApparentType(checker.getTypeAtLocation(arg)); + const claim = t.getProperty("claim"); + if (!claim) return false; + const ct = checker.getTypeOfSymbolAtLocation(claim, arg); + return !(ct.flags & ts.TypeFlags.Undefined) && !(ct.flags & ts.TypeFlags.Never); + }; + /** The name a function is known by: a declaration, a method, or a variable holding one (or, at module scope, a call's result). */ + const nameOfFunction = (n) => { + if ((ts.isFunctionDeclaration(n) || ts.isMethodDeclaration(n)) && n.name) return n.name.getText(); + if ((ts.isArrowFunction(n) || ts.isFunctionExpression(n)) && ts.isVariableDeclaration(n.parent) && ts.isIdentifier(n.parent.name)) return n.parent.name.text; + if (ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.initializer && ts.isCallExpression(n.initializer) && n.parent.parent && ts.isVariableStatement(n.parent.parent) && n.parent.parent.parent === n.getSourceFile()) return n.name.text; + return undefined; + }; + /** The nearest enclosing named function; anonymous ones are looked through. */ + const recorderOf = (node) => { + for (let n = node.parent; n; n = n.parent) { + const name = nameOfFunction(n); + if (name !== undefined) return name; + } + return "(module scope)"; + }; + // A recorder, and a pass-through, is keyed by name, so each key must name exactly one function: + // a second function of that name in the package would be read as the recorder. + const declared = new Map(); + const keyed = new Set([...Object.keys(RECORDERS), ...Object.keys(FORWARDERS), ...PASS_THROUGHS.map((p) => `orchestration:${p}`)]); + for (const sf of ownFiles) { + const visit = (node) => { + const name = nameOfFunction(node); + const key = name === undefined ? undefined : `${packageOf(rel(sf))}:${name}`; + if (key !== undefined && keyed.has(key)) declared.set(key, [...(declared.get(key) ?? []), `${rel(sf)}:${lineOf(sf, node)}`]); + ts.forEachChild(node, visit); + }; + visit(sf); + } + for (const key of keyed) { + const at = declared.get(key) ?? []; + if (at.length === 1) continue; + const what = `CAN'T TELL: ${key} names ${at.length} functions, so its writes can't be told from another's`; + cantTell.push(`endings: ${what}`); + findings.push({ rule: "endings", where: at.join(" and ") || MIGRATION_TABLE, what }); + } + /** The pass-through helper `expr` resolves to by symbol (a renamed import, a re-export, a namespace member, a local alias), if any. */ + const passThroughOf = (expr, depth = 0) => { + if (expr === undefined || depth > 8) return undefined; + if (ts.isParenthesizedExpression(expr) || ts.isAsExpression(expr) || ts.isNonNullExpression(expr)) return passThroughOf(expr.expression, depth + 1); + if (!ts.isIdentifier(expr) && !ts.isPropertyAccessExpression(expr)) return undefined; + for (const d of symbolOf(expr)?.declarations ?? []) { + if (ts.isFunctionDeclaration(d) && d.name && PASS_THROUGHS.includes(d.name.text) && packageOf(rel(d.getSourceFile())) === "orchestration") return d.name.text; + if (ts.isVariableDeclaration(d) && d.initializer) { + const via = passThroughOf(d.initializer, depth + 1); + if (via !== undefined) return via; + } + // `const { advisoryFail: f } = writes`: the property the binding reads. + if (ts.isBindingElement(d)) { + const prop = d.propertyName ?? d.name; + if (ts.isIdentifier(prop)) { + const via = (resolveAlias(checker.getSymbolAtLocation(prop))?.declarations ?? []).find( + (x) => ts.isFunctionDeclaration(x) && x.name && PASS_THROUGHS.includes(x.name.text) && packageOf(rel(x.getSourceFile())) === "orchestration", + ); + if (via !== undefined) return via.name.text; + } + } + } + return undefined; + }; + /** Whether `node` names a pass-through as a value (handed on, not called): not a callee, a declaration, an import or a type. */ + const passedOn = (node) => { + const p = node.parent; + if (ts.isIdentifier(node) && ts.isPropertyAccessExpression(p) && p.name === node) return false; + if (ts.isCallExpression(p) && p.expression === node) return false; + if (ts.isPropertyAccessExpression(p) && p.expression === node && ["call", "apply", "bind"].includes(p.name.text)) return false; + if (ts.isVariableDeclaration(p) && (p.initializer === node || p.name === node)) return false; + if (ts.isImportSpecifier(p) || ts.isExportSpecifier(p) || ts.isImportClause(p) || ts.isNamespaceImport(p) || ts.isBindingElement(p) || ts.isTypeQueryNode(p)) return false; + if ((ts.isFunctionDeclaration(p) || ts.isParameter(p)) && p.name === node) return false; + return true; + }; + const ENDS = ["complete", "fail"]; + const onCollection = (receiver, name) => isCollection(checker.getTypeAtLocation(receiver)) || (name !== undefined && declaredOnCollection(name)); + /** + * Whether `expr` is a task collection's `cancel` in a shape other than `x.cancel`: `x["cancel"]`, + * or a name destructured from a collection (`const { cancel: c } = x`). A cancel is an ending + * only under the caller's own claim, so the call's claim argument decides, as for `x.cancel(...)`. + */ + const isCancelRef = (expr) => { + if (ts.isElementAccessExpression(expr)) return ts.isStringLiteralLike(expr.argumentExpression) && expr.argumentExpression.text === "cancel" && isCollection(checker.getTypeAtLocation(expr.expression)); + if (!ts.isIdentifier(expr)) return false; + return (checker.getSymbolAtLocation(expr)?.declarations ?? []).some( + (d) => ts.isBindingElement(d) && ts.isObjectBindingPattern(d.parent) && (d.propertyName ?? d.name).getText() === "cancel" && isCollection(checker.getTypeAtLocation(d.parent)), + ); + }; + /** `x.cancel` or {@link isCancelRef}: any reference to a task collection's cancel. */ + const isAnyCancel = (expr) => (ts.isPropertyAccessExpression(expr) && expr.name.text === "cancel" && onCollection(expr.expression, expr.name)) || isCancelRef(expr); + /** + * The ending a call of a collection's cancel makes, through `.call`/`.apply` or a shape + * {@link isCancelRef} reads (`x.cancel(...)` itself is read below, with the removed members). + * `.apply` with an argument list it can't read counts, since its claim can't be told apart. + */ + const cancelCallEnding = (node, sf) => { + const callee = node.expression; + if (isCancelRef(callee)) return passesClaim(node.arguments[2]) ? `${callee.getText(sf)}() under the caller's own claim` : undefined; + if (!ts.isPropertyAccessExpression(callee) || !isAnyCancel(callee.expression)) return undefined; + const how = callee.name.text; + if (how === "call") return passesClaim(node.arguments[3]) ? `${callee.getText(sf)}() under the caller's own claim` : undefined; + if (how === "apply") { + const list = node.arguments[1]; + if (list !== undefined && ts.isArrayLiteralExpression(list)) return passesClaim(list.elements[2]) ? `${callee.getText(sf)}() under the caller's own claim` : undefined; + return `${callee.getText(sf)}() with arguments it can't read`; + } + return undefined; + }; + /** + * A collection's cancel handed on rather than called (`.bind`, passed as a value): what is + * done with it later can't be read, so it counts as an ending, as a handed-on complete or fail does. + */ + const cancelHandedOn = (node) => { + if (!isAnyCancel(node)) return false; + const p = node.parent; + if (ts.isCallExpression(p) && p.expression === node) return false; + if (ts.isPropertyAccessExpression(p) && p.expression === node && ["call", "apply"].includes(p.name.text)) return false; + if (ts.isIdentifier(node) && ts.isPropertyAccessExpression(p) && p.name === node) return false; + if ((ts.isVariableDeclaration(p) && p.name === node) || ts.isBindingElement(p)) return false; + return true; + }; + const recorders = {}; + for (const sf of ownFiles) { + const path = rel(sf); + const visit = (node) => { + let ending; + // The shapes that hand a collection's write on rather than make it: a `.bind`/`.call`/`.apply` + // receiver, an element access by a non-literal key, a property access not called directly. + let forwards = false; + if (ts.isPropertyAccessExpression(node) && ENDS.includes(node.name.text) && onCollection(node.expression, node.name)) { + ending = `${node.getText(sf)}`; + // Called, or called through `.call`/`.apply` (`ref.fail.call(ref, …)`), is a write made + // here; only a reference handed on (`.bind`, passed as a value) forwards. + const viaCall = ts.isPropertyAccessExpression(node.parent) && node.parent.expression === node && ["call", "apply"].includes(node.parent.name.text) && ts.isCallExpression(node.parent.parent) && node.parent.parent.expression === node.parent; + forwards = !((ts.isCallExpression(node.parent) && node.parent.expression === node) || viaCall); + } else if (ts.isElementAccessExpression(node) && isCollection(checker.getTypeAtLocation(node.expression))) { + const arg = node.argumentExpression; + if (!ts.isStringLiteralLike(arg) || ENDS.includes(arg.text)) ending = `${node.getText(sf)} (element access)`; + forwards = !ts.isStringLiteralLike(arg); + } else if ((ts.isIdentifier(node) || ts.isPropertyAccessExpression(node)) && passedOn(node) && passThroughOf(node) !== undefined) { + ending = `${node.getText(sf)} (${passThroughOf(node)}, handed on as a value)`; + } else if (ts.isBindingElement(node) && ts.isObjectBindingPattern(node.parent)) { + const prop = (node.propertyName ?? node.name).getText(sf); + if (ENDS.includes(prop) && isCollection(checker.getTypeAtLocation(node.parent))) ending = `${prop} destructured from a task collection`; + } else if ((ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) || ts.isIdentifier(node)) && cancelHandedOn(node)) { + ending = `${node.getText(sf)} (a task collection's cancel, handed on)`; + forwards = true; + } + if (ts.isCallExpression(node)) { + if (ts.isPropertyAccessExpression(node.expression)) { + const method = node.expression.name.text; + if (["cancel", ...memberRules.flatMap((m) => m.props)].includes(method) && onCollection(node.expression.expression, node.expression.name)) { + for (const m of memberRules) { + if (m.props.includes(method) && !isMarked(lineText(sf, node))) { + findings.push({ rule: "removed", where: `${path}:${lineOf(sf, node)}`, what: `${m.row} (${m.owner}) live: a task collection's ${method}() is called` }); + } + } + if (method === "cancel" && passesClaim(node.arguments[2])) ending = `${node.expression.getText(sf)}() under the caller's own claim`; + } + } + ending ??= cancelCallEnding(node, sf); + const helper = passThroughOf(node.expression); + if (helper !== undefined) { + if (helper !== "advisoryCancel" || passesClaim(node.arguments[3])) ending = `${node.expression.getText(sf)}() (${helper})`; + } else if (ts.isPropertyAccessExpression(node.expression) && ["call", "apply", "bind"].includes(node.expression.name.text)) { + const via = passThroughOf(node.expression.expression); + if (via !== undefined) ending = `${node.expression.getText(sf)}() (${via})`; + } + } + if (ending !== undefined) { + const fn = recorderOf(node); + const key = `${packageOf(path)}:${fn}`; + scanned.endings++; + if (key.startsWith("orchestration:") && PASS_THROUGHS.includes(fn)) { + // The helper's own write: its callers are read in its place. + } else if (key in FORWARDERS && forwards) { + // A collection wrapper handing its own complete or fail on: its callers are read in its + // place. A direct write inside it is still a finding. + } else if (key in RECORDERS) { + recorders[key] = (recorders[key] ?? 0) + 1; + } else if (!isMarked(lineText(sf, node))) { + findings.push({ rule: "endings", where: `${path}:${lineOf(sf, node)}`, what: `${ending} in ${key}, which is not a recorder` }); + } + } + // A worker flow's door: a call of core's `defineFlow` (by symbol) that declares the `respond` + // door or whose `configSchema` is typed with every key of Workforce's worker contract + // (`workerConfigSchema()`), however either is built. + let config = ts.isCallExpression(node) ? node.arguments[0] : undefined; + // `defineFlow({ ... } as never)`, as the coordinator's is: the config under its cast. + while (config !== undefined && (ts.isAsExpression(config) || ts.isParenthesizedExpression(config) || ts.isSatisfiesExpression?.(config))) config = config.expression; + if (doorRule && ts.isCallExpression(node) && isCoreDefineFlow(node.expression) && config !== undefined && ts.isObjectLiteralExpression(config)) { + const actions = config.properties.find((p) => ts.isPropertyAssignment(p) && p.name.getText(sf) === "actions"); + // The contract's one door, `respond` (worker-door.ts), marks a worker flow too: a configSchema + // typed only as some zod object (the coordinator's, from its turn's settings) carries no keys to read. + const hasRespond = actions !== undefined && ts.isObjectLiteralExpression(actions.initializer) && actions.initializer.properties.some((p) => actionKey(p, sf) === "respond"); + if ((hasRespond || isWorkerConfig(config)) && actions && ts.isObjectLiteralExpression(actions.initializer)) { + scanned.removedTyped++; + workerFlows.push(`${path}:${lineOf(sf, node)}`); + for (const p of actions.initializer.properties) { + const key = actionKey(p, sf); + if (key !== undefined && doorRule.names.includes(key) && !isMarked(lineText(sf, p))) { + findings.push({ rule: "removed", where: `${path}:${lineOf(sf, p)}`, what: `${doorRule.row} (${doorRule.owner}) live: a worker flow declares the door "${key}"` }); + } + } + } + } + ts.forEachChild(node, visit); + }; + visit(sf); + } + const unreadFlows = (doorRule?.floor ?? []).filter((f) => !workerFlows.some((w) => w.startsWith(`${f}:`))); + if (unreadFlows.length > 0) cantTell.push(`removed: the door rule read no worker flow in ${unreadFlows.join(", ")}, so it stopped reading the built-in flows`); + + return { + findings, + cantTell, + scanned, + rows, + tools, + recorders, + workerFlows, + files: { tracked: tracked.length, swept: swept.length, excluded, program: ownFiles.length }, + }; +} diff --git a/goals/workforce-coordination/runs-on-one-task/gaps.mts b/goals/workforce-coordination/runs-on-one-task/gaps.mts new file mode 100644 index 0000000000..68c26757f5 --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/gaps.mts @@ -0,0 +1,142 @@ +/** + * Plan row 4's map: each of the 31 in-scope rows of `docs/internal/design/workforce-coordination-gaps.md` + * (every phase 1 and phase 3 row), the child whose Linear scope line cites it, and the checks + * that read it. A row is matched by its target's opening words, so a row renamed or added on the + * page is a row this map doesn't hold, and the run says so. Each check names a run of this + * closure: `closure ` row 1's assertions, `sweep: …` the sweep, ` .test.ts` + * a regression test row 3 runs, or a goal path row 3 runs, then its legs and `(control …)`. + * A row passes only when every run it names ran in this closure run and passed (a goal and each + * control it names, as row 3 graded them; an assertion, as its leg's last attempt): a label + * naming no such run, or one not run here, fails, as a stale `by` does. Leg letters after a goal + * are where in it the row is read; the goal's own verdict covers them. + * + * Four rows hold a half other epics own (FIX-2056 D2, (f)); the carved half is printed beside + * its row and does not gate the closure. + */ +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { REPO_ROOT } from "../../lib/index.mts"; + +const GAPS = "docs/internal/design/workforce-coordination-gaps.md"; + +interface Row { + /** The row's target, its opening words. */ + target: string; + owners: string; + checks: string[]; + carved?: string; +} + +/** The 31 rows, in the page's order. */ +export const ROWS: Row[] = [ + { target: "Routed tasks; attempts a setting", owners: "FIX-2083, FIX-2080", checks: ["coordinators/routes-each-message-as-a-task a:tasks", "task-board/runs-by-its-filing-settings", "closure a:routed-listed"] }, + { target: "`onTaskSettled` in orchestration", owners: "FIX-2078", checks: ["task-board/every-ending-reaches-its-filer a", "closure a:one-settled, a:once"] }, + { target: "A failure is an ending delivered to the filer", owners: "FIX-2078", checks: ["task-board/every-ending-reaches-its-filer d", "closure c:one-failure-notice"], carved: "the Error Reporter's intake half is (f)'s: errorCapture stays the request route" }, + { target: "Every debt a row owes carries a due time", owners: "FIX-2078", checks: ["task-board/every-ending-reaches-its-filer b, e (control no-sweeper-payment)", "closure b:hears-in-due-time"] }, + { target: "A plan board and a delegation board", owners: "FIX-2076", checks: ["task-board/refuses-a-board-built-wrong", "task-board/a-worker-plans-its-own-steps"] }, + { target: "A plan board waits on a row it dispatched", owners: "FIX-2082", checks: ["task-board/waits-on-a-task-it-filed d (control drain-moves-on)", "closure a:drain-waits, a:answers-after-steps"] }, + { target: "A goal-seek loop needs a plan board", owners: "FIX-2076", checks: ["task-board/refuses-a-board-built-wrong b", "goal-seek-loop/replans-until-done (j3)"] }, + { target: "An ending that makes a row startable runs `dispatch`", owners: "FIX-2078", checks: ["task-board/every-ending-reaches-its-filer c"] }, + { target: "`respond`; `dispatch`", owners: "FIX-2084", checks: ["workers/roles-checked-at-hire", "sweep: the door matcher"] }, + { target: "`dispatch` runs one at a time per session", owners: "FIX-2078", checks: ["task-board/every-ending-reaches-its-filer c"] }, + { target: "The drain's last startable check runs after its repairs", owners: "FIX-2078, FIX-2096", checks: ["task-board/every-ending-reaches-its-filer c", "orchestration drain-pays-owed-cascade.test.ts"] }, + { target: "A task's request may wait on a task it filed, up to 5 waits deep", owners: "FIX-2082", checks: ["task-board/waits-on-a-task-it-filed b"] }, + { target: "A request waits on a task it filed, just now or earlier", owners: "FIX-2082", checks: ["task-board/waits-on-a-task-it-filed a", "closure j1:waits-in-request"] }, + { target: "A wait's deadline leaves the task running", owners: "FIX-2082", checks: ["task-board/waits-on-a-task-it-filed c (control deadline-cancels)"] }, + { target: "A request suspended by `waitForUser`", owners: "FIX-2077", checks: ["task-board/settles-when-its-work-is-over"], carved: "marking a task waiting on a person is FIX-2058's" }, + { target: "A message to an open task's session is conversation", owners: "FIX-2077", checks: ["task-board/settles-when-its-work-is-over"] }, + { target: "The row settles only after the request's background work ends", owners: "FIX-2077, FIX-2088", checks: ["task-board/settles-when-its-work-is-over a (control settle-at-return)", "task-board/background-error-fails-the-attempt"] }, + { target: "Splits use delegates and stay running", owners: "FIX-2077, FIX-2081", checks: ["task-board/settles-when-its-work-is-over", "workforce/split-to-roster", "closure a:ends-running"] }, + { target: "Delegates are authority; `all`", owners: "FIX-2081", checks: ["task-tools/five-tools-one-rule-per-field c (control roster-reach)"] }, + { target: "One open task per session; another stays pending", owners: "FIX-2080", checks: ["task-board/runs-by-its-filing-settings"] }, + { target: "Start: now, after named tasks, or held", owners: "FIX-2080", checks: ["task-board/runs-by-its-filing-settings"] }, + { target: "`updateTask`, one rule per field", owners: "FIX-2081", checks: ["task-tools/five-tools-one-rule-per-field b (control edit-any-state)"] }, + { target: "Recurring work is a schedule that files an ordinary task", owners: "FIX-2085", checks: ["scheduled-work/recurring-work-files-a-task"] }, + { target: "A dependency stays within one board", owners: "FIX-2080", checks: ["task-board/runs-by-its-filing-settings"] }, + { target: "Cancelling, retrying or failing a task cancels its open tasks", owners: "FIX-2080", checks: ["task-board/runs-by-its-filing-settings d (control no-cascade)"] }, + { target: "A failed or cancelled dependency cancels its dependents", owners: "FIX-2080", checks: ["task-board/runs-by-its-filing-settings d (control no-cascade)", "closure c:dependent-cancelled"] }, + { target: "Five task tools", owners: "FIX-2081, FIX-2083", checks: ["task-tools/five-tools-one-rule-per-field a", "coordinators/routes-each-message-as-a-task d:one-check", "sweep: the tools rule", "closure j1:tools"], carved: "`sendMessage` among the model's tools is FIX-2070's" }, + { target: "The task tools are public actions a screen calls as the user", owners: "FIX-2081", checks: ["task-tools/five-tools-one-rule-per-field", "closure a:routed-listed, j2:cancelled"], carved: "answering a question by public action is FIX-2058's" }, + { target: "A worker declares its roles", owners: "FIX-2084", checks: ["workers/roles-checked-at-hire a (control no-role-check)"] }, + { target: "`updateTask`'s `message`, until a task finishes", owners: "FIX-2086", checks: ["task-tools/steer-and-follow-up (controls drop-late-steer, reopen)", "closure j2:steered, j2:steer-shows"], carved: "answering a question with the message is FIX-2058's" }, + { target: "A finished task's output never changes", owners: "FIX-2086", checks: ["task-tools/steer-and-follow-up d (control reopen)", "closure a:follow-up, a:original-unchanged"] }, +]; + +/** The in-scope rows on the page: every phase 1 and phase 3 row of the differences table. */ +function pageRows(): string[] { + return readFileSync(join(REPO_ROOT, GAPS), "utf8") + .split("\n") + .filter((l) => /^\| (1 · One task|3 · Into a task) \| /.test(l)) + .map((l) => l.split("|")[2]!.trim()) + .filter((target) => target !== "FIX-2056 (one epic)"); +} + +/** What this closure run ran, and each run's verdict, for resolving a row's checks. */ +export interface Ran { + /** Row 1's assertion tags. */ + tags: readonly string[]; + /** Each walk leg run, and whether its last attempt passed (`j1:` and `j2:` are leg a's). */ + legs: ReadonlyMap; + /** The sweep's verdict, plain and `--control` both, if it ran. */ + sweep: boolean | undefined; + /** Row 3's goals (`goals/`), its controls (` GOAL_CONTROL=`) and its test files, each with whether it passed (a control: went red as documented). */ + checks: ReadonlyMap; +} + +/** Why one check label isn't a run that passed here, or undefined when it is. */ +function unresolved(label: string, ran: Ran): string | undefined { + if (label.startsWith("closure ")) { + for (const tag of label.slice("closure ".length).split(/,\s*/)) { + if (!ran.tags.includes(tag)) return `${tag} is no row-1 assertion`; + const leg = /^j[12]:/.test(tag) ? "a" : tag.split(":")[0]!; + const passed = ran.legs.get(leg); + if (passed === undefined) return `leg ${leg}, which holds ${tag}, didn't run`; + if (!passed) return `leg ${leg}, which holds ${tag}, failed`; + } + return undefined; + } + if (label.startsWith("sweep:")) return ran.sweep === undefined ? "the sweep didn't run" : ran.sweep ? undefined : "the sweep failed"; + const test = /^\S+ (\S+\.test\.ts)$/.exec(label)?.[1]; + if (test !== undefined) { + const hits = [...ran.checks].filter(([k]) => k.endsWith(`/${test}`)); + if (hits.length === 0) return `${test} didn't run`; + return hits.every(([, ok]) => ok) ? undefined : `${test} failed`; + } + const goal = label.split(" ")[0]!; + const passed = ran.checks.get(goal); + if (passed === undefined) return `${goal} didn't run`; + if (!passed) return `${goal} failed`; + for (const name of (/\(controls? ([^)]+)\)/.exec(label)?.[1] ?? "").split(/,\s*/).filter(Boolean)) { + const red = ran.checks.get(`${goal} GOAL_CONTROL=${name}`); + if (red === undefined) return `${goal}'s control ${name} didn't run`; + if (!red) return `${goal}'s control ${name} didn't go red as documented`; + } + return undefined; +} + +/** + * Print the map, each check with whether it ran and passed here. A page row this map doesn't + * hold, one with no check, and a check naming no run that passed in this closure run are each + * pushed as a failure. + */ +export function printGaps(report: string[], failures: string[], ran: Ran): void { + const page = pageRows(); + report.push(`row 4: ${page.length} in-scope gaps rows on the page, ${ROWS.length} mapped`); + let green = 0; + for (const target of page) { + const row = ROWS.find((r) => target.startsWith(r.target)); + if (row === undefined || row.checks.length === 0) { + failures.push(`gaps — no check reads the row "${target.slice(0, 90)}"`); + report.push(` UNMAPPED ${target.slice(0, 90)}`); + continue; + } + const misses = row.checks.flatMap((c) => { + const why = unresolved(c, ran); + return why === undefined ? [] : [`${c}: ${why}`]; + }); + if (misses.length === 0) green += 1; + else failures.push(`gaps — the row "${row.target.slice(0, 60)}" names checks that didn't pass in this run: ${misses.join("; ")}`); + report.push(` ${misses.length === 0 ? "PASS" : "FAIL"} ${row.owners.padEnd(18)} ${row.target.slice(0, 60).padEnd(60)} ← ${row.checks.join(" · ")}${row.carved ? ` [carved: ${row.carved}]` : ""}`); + } + report.push(`row 4: ${green} of ${page.length} rows with every check run and passed here`); +} diff --git a/goals/workforce-coordination/runs-on-one-task/goal.md b/goals/workforce-coordination/runs-on-one-task/goal.md new file mode 100644 index 0000000000..1b25a59797 --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/goal.md @@ -0,0 +1,51 @@ +# workforce-coordination › it runs on one task + +**Issue:** FIX-2087, the closure of epic FIX-2056. Its spec's goal: `specs/issues/FIX-2087/SPEC.md` → "The goal, and how we'll know it's met". + +**Outcome:** on one `main` commit with every other child merged, a person using a Workforce app on a real model hands out work, waits on it, steers it and hears how it ended through one task; every ending reaches its filer once, after a server restart and when a task fails out too; nothing records an ending except the claim gate or the drain; and no task tool beyond the five (and `answerTask` and `parkOnQuestion` until FIX-2058) is offered, now and in later changes. + +**Input:** + +- **The Lab** (`lab/fsdev.config.mts`): the concept's cast as `WORKER.md` files only (`cast.mts`, written at run time), on the built-in `agent` and coordinator flows, a SQLite store with durable execution, served by Shift Manager's own command. Alice signs in with her verified bearer and acts through the shipped clients. `desk.launch` (coordinator, routing everyone) hands to `desk.writer`, which asks `desk.researcher` for the fact and fact-checks it in a step of its own in a session of its own (`session: "new"`); `desk.chief` hands questions to `desk.editor`, which files for researcher and waits with `getTask`. The app adds no coordination code. The Lab notes the tools each model call was handed (`GOAL_TOOLS_LOG`), without changing them. +- **Held out, picked at run time:** the topic, the held-out word Alice asks for, the steer word, and the fact, which only researcher's file holds. + +**Signal:** each failure is tagged `:`, and each assertion is **structural** or **judgment** (`TAGS` in `run.mts`, printed with every failure; D1). + +- **a**, the concept's walk less the call and the Inbox: `a:routed-listed` launch's routed task for writer is in Alice's list; `a:files-research` writer files researcher's task; `a:ends-running` writer's task stays running past its first request and its research; `a:one-settled` one `onTaskSettled` per research ending starts writer again; `a:step-dispatched` writer files its fact-check as a step in a session of its own; `a:drain-waits` the task ends after that step; `a:answers-after-steps` writer gets a turn after the step ends; `a:lands-once` the draft lands once, under writer's name; `a:draft-content` with the held-out word and researcher's fact; `a:follow-up` Alice's follow-up from the finished task runs in its session and is answered by a turn of writer's after it was filed; `a:follow-up-differs` (judgment) that turn writes a new version, not the original word for word (when it does, the two lines read alike and are told apart by when each landed); `a:original-unchanged` the original reads byte-identical; `a:once` every ending reaches its filer once. +- **j1** (a team that hands out work): `j1:routed` (structural) launch routed Alice's message to editor, `j1:files` (judgment) editor filed for researcher, `j1:waits-in-request` editor reads the research's ending with `getTask` in the same request, `j1:read-once` and so no notice turn runs for it (an `onTaskSettled` request that finds the notice no longer owed, output `{ owed: false }`, runs no turn and isn't counted), `j1:heard-once` the ending is heard exactly once, by that read, `j1:answer`, `j1:tools` no task turn is offered a tool beyond the allow list and the roster's own two (`listDelegates`, `messageWorker`), whatever it is called, `j1:once`. +- **j2** (a team that runs a coordinator): `j2:steered` Alice's steer of an open routed task is taken, `j2:steer-shows` the draft carries the steer word, `j2:cancelled` her cancel of another ends it, `j2:cancel-heard-once` launch's session hears it once and no line lands. +- **b**: `b:routed` (structural), `b:files-research` (judgment) writer filed research before the kill; `b:killed` the server dies after researcher's ending is written and before it is sent (FIX-2078's kill point, reused); `b:hears-in-due-time` after a restart with no touch, writer hears it within the due time plus 2 s; `b:once`. +- **c**: researcher's model refuses every call. `c:routed` (structural), `c:files-dependent` (judgment), `c:fails-out`, `c:dependent-cancelled`, `c:one-failure-notice` writer hears one failure notice naming the dependent it cancelled, and none for the dependent; `c:writer-ends` (structural) writer's task ends, not left open; `c:once` its one line lands. +- **sweep** (`sweep.mjs`, the engine CI runs): 0 live lines and no CAN'T TELL (every allowed tool read, every recorder key naming one function), and `--control` reports exactly its plants. +- **children** (`children.mts`): every child's goal or check with its controls, the set read from Linear (when it can't be read, row 3 runs the set here and fails); plus the coordinators, hand-offs and FIX-2005 privacy goals, and the goal-seek loop. A control passes only when its goal exits 1, every tag its goal.md says it must fail has a `FAIL` bullet of its own, and the goal prints no diagnostic that a control of its own stayed green. The closure's own findings are children too once filed: each runs its regression tests and names, in `by`, the run here that grades it end to end (a goal another entry runs, a row-1 assertion, or the sweep). A `by` that names no such run fails. A child with an entry here that Linear doesn't list as blocking runs too, and is printed as such. +- **gaps** (`gaps.mts`): the 31 in-scope gaps rows, each mapped to the checks that read it. A row passes only when every check it names ran in the same closure run and passed: a goal or regression test row 3 ran, each control it names red as documented, a row-1 assertion whose leg passed, or the sweep. A label naming no such run, or one not run here, fails. It grades that each row's checks ran green, not that they reach every side of the row; the row's own wording is the claim they're read against. + +**Anti-game:** Alice's conversation and rows are read through `listTasks_tasks` and `getTask_tasks` as Alice; every other grade reads the store file, read-only; endings are counted from the filer's notices and requests. No fixture writes a row, a session or a notice. The kill is a real process exit at the delivery's send, and the restarted server gets no action. A structural FAIL on any attempt is a finding, never retried past (D1). + +**Model:** real: the goals library's default model through the gateway (`gatewayModel(goalModel())`, `openai/gpt-5.4-mini`), gated on a key that serves it (`keysServing`); without one the walk is blocked, not failed. The sweep and gaps parts are model-free. + +**Run:** `pnpm tsx goals/workforce-coordination/runs-on-one-task/run.mts`. `GOAL_PARTS=walk,sweep,children,gaps` (default all), `GOAL_LEGS=a,b,c`, `GOAL_ATTEMPTS=` (default 3, judgment misses only; 1 under a control). The children part runs every composed goal and takes hours. On demand only; the sweep alone runs in CI's Typecheck job (`node goals/workforce-coordination/runs-on-one-task/sweep.mjs`, and with `--control`). + +**Controls:** + +- `GOAL_CONTROL=no-sweeper-payment` (FIX-2078's, imported from its goal's `controls.mts`) must FAIL **`b:hears-in-due-time`**. +- `GOAL_CONTROL=no-cascade` (FIX-2080's, imported from its goal's `controls.mts`) must FAIL **`c:dependent-cancelled`**. +- Either control leaving its tag green fails the run as `control:broken`, so a broken control can't read as a pass. +- `node sweep.mjs --control` plants tools built through a renamed local alias, a template name, a sequencer and a generator under renamed imports, and a renamed `handler` import and a namespace import in sibling files; a removed name; a removed door brought back in the agent's worker flow and, under a computed key, the coordinator's (a worker flow is a core `defineFlow` declaring `respond` or with a `configSchema` typed by the worker contract, and both built-in flows must be read, or it can't tell); endings through a renamed variable, a claim-passing cancel (called, through `.call`, `.apply` and element access, destructured, and handed on by `.bind`; the same shapes without a claim must not be reported), a renamed, a namespace, a destructured and a passed-on pass-through, destructuring, element access and `.call`, and a direct write, and writes through `.call` and `.apply`, inside a collection wrapper; and a second function named like a recorder. It must report exactly those, and say it can't tell when the real capability's `createTask` isn't built with `handler`. `node sweep.mjs --at ` sweeps another commit's tree: today's `main` before the epic must FAIL. + +## Verdict log +| Date | Commit | Model | Verdict | Notes | +|------|--------|-------|---------|-------| +| 2026-10-23 | 14778344c (`main`, every other child merged) + this goal, uncommitted | vercel:openai/gpt-5.4-mini | FAIL | **a** stops on attempt 1, structural (D1), on both graded runs: `a:answers-after-steps` and `a:lands-once` (writer gets no turn after its `session: "new"` step; the task completes with its earlier answer, FIX-2110); `a:follow-up`, `a:once` (the follow-up runs in a new session, FIX-2111); `j1:read-once` (research ended 319 ms before editor's `getTask` wait, and a notice turn ran too, FIX-2112); `j2:steer-shows` on 3 of 3 runs (FIX-2113). Green: `a:routed-listed`, `a:files-research`, `a:ends-running`, `a:one-settled`, `a:step-dispatched`, `a:drain-waits`, `a:original-unchanged`, `j1:files`, `j1:waits-in-request`, `j1:answer`, `j1:tools` (createTask, getTask, listTasks, updateTask, answerTask, parkOnQuestion, reportProgress, listDelegates, messageWorker), `j1:once`, `j2:steered`, `j2:cancelled`, `j2:cancel-heard-once`. **b** PASS twice: heard 1012 and 857 ms past due. **c** PASS: research errored, its dependent cancelled, one failure notice naming it. **sweep** FAIL, 9 live lines, all published docs (FIX-2119); `--control` reports exactly its 4 plants | +| 2026-10-23 | same, `GOAL_CONTROL=no-cascade` | vercel:openai/gpt-5.4-mini | FAIL (expected) | `c:dependent-cancelled`: the dependent stayed pending; co-failure `c:one-failure-notice` (nothing was cancelled to name) | +| 2026-10-23 | same, `GOAL_CONTROL=no-sweeper-payment` | — | CONTROL BROKEN | the patch's anchor in `durability-sweeper.ts` changed with FIX-2082, so it applies to nothing (FIX-2118); `b:hears-in-due-time` has no red state until it is fixed | +| 2026-10-23 | `node sweep.mjs --at 96404d2` (`main` before the epic) | n/a | FAIL (expected) | 497 live lines: 8 removed tools offered, 482 removed surfaces, 7 ending writes outside a recorder (Harness Manager's refused run, `settleParent`, the coordinator's claim-passing cancel, `completeTask`/`failTask`) | +| 2026-10-23 | e07f360bb (`main`, every closure fix merged: FIX-2097, 2110 to 2122, 2124, 2126, 2127) merged into this branch, 7e07954b1 | vercel:openai/gpt-5.4-mini | FAIL | **a** PASS on attempt 2: attempt 1's only miss was `a:draft-content` (judgment: the model wrote the word capitalised); every structural assertion of a, j1 and j2 green on both attempts, `j1:read-once`, `j1:heard-once`, `j2:steer-shows`, `a:answers-after-steps`, `a:lands-once`, `a:follow-up`, `a:once` among them. **b** PASS: heard 999 ms past due. **c** FAIL then PASS on its rerun: `c:one-failure-notice` (structural) on run 1, the failure's notice named no dependent: the ending's own cascade missed the dependent, a touch cancelled it 2013 ms later, after the notice was sent, and writer's task stayed `in_progress` (no one told it); run 2 cancelled it 3 ms after the failure and passed (a product race, reported). **sweep** PASS: 7 tools, 18 ending writes, all recorders'; `--control` OK | +| 2026-10-23 | `node sweep.mjs --at 96404d2` (`main` before the epic) | n/a | CAN'T TELL (red, as required) | exit 3: the same 497 live lines (8 removed tools offered, 7 ending writes outside a recorder), and CAN'T TELL because createTask, getTask and reportProgress and six recorders don't exist yet there. The 2026-10-23 row above names this run's verdict FAIL; its exit was CAN'T TELL then too (the rules haven't changed) | +| 2026-10-23 | deaccbd61 and 1ba899d7f, children and gaps | as each goal names | FAIL | 27 children read from Linear, the closure's findings among them, each graded (`by`). Every child's check PASS and every control red as documented, except: `every-coordinator-is-a-worker-file` `b:after` (this goal, now committed, was unclassified in FIX-1792's census; classified, then PASS and its control red) and `hand-offs/survives-a-restart-and-runs-once` (the tree was dirty mid-run; rerun on 1ba899d7f: every child and control as expected, J4 `j:names-ticket` 3/3, KNOWN-OPEN FIX-2101). Gaps: 31 of 31 mapped | +| 2026-10-23 | round 3: `main` with FIX-2123, FIX-2135, FIX-2136 merged, 5e5ee0ea3 | vercel:openai/gpt-5.4-mini | FAIL, then goal fixed | **a** attempt 1 FAIL `a:one-settled` (structural): 2 onTaskSettled requests for 1 research ending, the second `{ owed: false }`, a duplicate delivery FIX-2123's claim turned away with no turn. Goal out of date: a, b and j2 now count the requests that admitted a notice, as j1 does. **b** PASS. **c** PASS 3 of 3 (the dependent cancelled 4 to 17 ms after the failure each time). **sweep** PASS, `--control` OK | +| 2026-10-23 | round 3, 455b3fb19, children and gaps | as each goal names | FAIL | 27 children from Linear plus FIX-2123, 2135, 2136 (children of FIX-2056 not blocking FIX-2087 in Linear): every check PASS, every control red as documented, except: `survives-a-restart-and-runs-once` J4 `j:names-ticket` 3/3 (KNOWN-OPEN FIX-2101), and FIX-2005's privacy leg FAIL m2/m3 on 1 of 2 runs (both talk requests `completed` with no answer, which landed later; rerun PASS). Gaps 31 of 31 | +| 2026-10-23 | round 3 after FIX-2138's merge, e51ac8be9 | vercel:openai/gpt-5.4-mini | PASS | **a** (with j1, j2) PASS on attempt 1, **b** PASS (1015 ms past due), **c** PASS 3 of 3, **sweep** PASS and `--control` OK, the privacy leg PASS; FIX-2138's regression test PASS | +| 2026-10-23 | round 4: `main` ce2282bd5 (FIX-2147, FIX-2149, FIX-2150) merged in, 4ec08e656 | vercel:openai/gpt-5.4-mini | FAIL, then goal fixed | **a** attempt 1 FAIL `a:lands-once`, `a:follow-up`, `a:once` (structural): writer's follow-up turn wrote the original draft word for word, so the two result lines read alike. Each line landed once, from its own request. Goal out of date: the new version is judgment now (`a:follow-up-differs`), and lines that read alike are told apart by when each landed | +| 2026-10-24 | round 4, 683c151f5, the whole plan | as each goal names | FAIL, then goals fixed | Walk, sweep and gaps PASS. Row 3, now requiring every tag a control must fail, read 5 controls as broken: `mailbox-left` (its tags are bracketed: the closure's reader, fixed), `no-follow-up` (stale since FIX-2115 and FIX-2122: Workforce maps the decision to a line itself; the control now patches those maps), `no-plan` and `no-self-choice` (the model, and best fit's evaluation, moved: 4 of 4 runs each; their goals now say so), `no-waker` (ungraded: the keeper ended before the asker's `getTask` parked, 4 of 4 standalone runs; red in P1.a's run) | +| 2026-10-24 | round 4, final, b6516a8a9, the whole plan, both controls, c ×3, the privacy leg ×5 | vercel:openai/gpt-5.4-mini | FAIL | **a** PASS on attempt 3 (judgment misses only: `a:draft-content`, then `j2:steer-shows`). **b** FAIL on attempt 1, structural, `b:hears-in-due-time` and `b:once` (D1, not retried): the kill caught writer's filing request still running; after the restart that request still reads `in_progress`, so each delivery of the research's notice is turned away for it (`noticeDeferred … whileRunning`), and writer never heard in the window. A product finding. 5 diagnostic reruns of leg b PASS (857 to 996 ms past due), each with writer's request ended before the kill. **c** PASS 3 of 3. **sweep** PASS (7 tools, 19 ending writes, 6 worker flows), `--control` OK (27 lines from 19 plants, floor CAN'T TELL). Controls `no-sweeper-payment` and `no-cascade` FAIL as they must. Row 3: 27 children from Linear plus 7 with a check here, every check PASS, all 33 controls red as documented, J4 `j:names-ticket` KNOWN-OPEN FIX-2101. Privacy leg PASS ×5 plus row 3's. Gaps 30 of 31 (the due-time row, on leg b) | diff --git a/goals/workforce-coordination/runs-on-one-task/lab/fsdev.config.mts b/goals/workforce-coordination/runs-on-one-task/lab/fsdev.config.mts new file mode 100644 index 0000000000..b3e1cd84b2 --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/lab/fsdev.config.mts @@ -0,0 +1,108 @@ +/** + * The closure's Lab: the concept's cast as `WORKER.md` files only, on the built-in flows, on the + * real engine and a SQLite store, served over HTTP by Shift Manager's own command. Alice and + * Bob sign in with their own verified bearers (`files-and-splits-down-the-chain`'s + * `people.mts`); a request with neither is refused. + * + * The tree is written by `run.mts` at run time (`GOAL_TREE`), so the topic, the held-out word + * and the researcher's fact are picked per run: `launch` on the built-in coordinator, routing + * everyone, whose one delegate is `writer`; `writer` and `researcher` on the built-in `agent` + * flow, writer's delegate being researcher. The app adds no coordination code: two flows, the + * tree, a store and a resolver. + * + * Read from the environment: `GOAL_STORE` (the store file), `GOAL_TREE` (the tree), + * `GOAL_REAL_MODEL` (the model `goal/real` names) and `GOAL_TOOLS_LOG` (where the tools each + * model call was handed are noted). `goal/broken` is a model that refuses every call, for leg + * c's researcher, so its task fails out. + * + * Durable, so it keeps wakes and runs the durability sweeper (leg b). + * + * Nothing here writes a row, a session or a notice. + */ +import { appendFileSync } from "node:fs"; +import * as engine from "@flow-state-dev/engine"; +import { sqliteStores } from "@flow-state-dev/store-sqlite"; +import * as workforce from "@flow-state-dev/workforce"; +import { readWorkforce } from "@flow-state-dev/workforce/loader"; +import { ORG, PEOPLE } from "../../../workers/files-and-splits-down-the-chain/lab/people.mts"; + +const STORE = process.env.GOAL_STORE ?? ""; +const TREE = process.env.GOAL_TREE ?? ""; +const REAL_MODEL = process.env.GOAL_REAL_MODEL ?? ""; +if (STORE === "" || TREE === "" || REAL_MODEL === "") throw new Error("GOAL_STORE, GOAL_TREE and GOAL_REAL_MODEL must each name something"); + +const tree = await readWorkforce(TREE); +if (tree.errors.length > 0) throw new Error(`the closure's tree did not load: ${JSON.stringify(tree.errors).slice(0, 600)}`); + +/** + * A model that refuses every call, after a while: leg c's researcher, whose task fails out once + * writer has filed the task that depends on it, as a slow outage would. + */ +const BROKEN_AFTER_MS = 12_000; +const refuse = async (): Promise => { + await new Promise((resolve) => setTimeout(resolve, BROKEN_AFTER_MS)); + throw new Error("the research service is down"); +}; +const broken = { modelId: "goal/broken", generate: refuse, generateStep: refuse }; + +/** + * The real model, noting the tools the engine handed each call (`GOAL_TOOLS_LOG`, one JSON line + * per call: who the turn is, from its instructions' first line, and the tool names). A record of + * what a turn was offered, never a change to it. + */ +const TOOLS_LOG = process.env.GOAL_TOOLS_LOG ?? ""; +const noted = (model: any) => { + const note = (options: any) => { + if (TOOLS_LOG === "") return; + const system = (options?.messages ?? []).find((m: any) => m?.role === "system"); + const who = /You are ([a-z]+\.[a-z]+)/.exec(typeof system?.content === "string" ? system.content : JSON.stringify(system?.content ?? ""))?.[1] ?? "?"; + appendFileSync(TOOLS_LOG, `${JSON.stringify({ who, tools: (options?.tools ?? []).map((t: any) => String(t?.name ?? "")), at: Date.now() })}\n`); + }; + return Object.assign(Object.create(model), { + generate: (options: any) => (note(options), model.generate(options)), + ...(typeof model.generateStep === "function" ? { generateStep: (options: any) => (note(options), model.generateStep(options)) } : {}), + ...(typeof model.streamStep === "function" ? { streamStep: (options: any) => (note(options), model.streamStep(options)) } : {}), + ...(typeof model.stream === "function" ? { stream: (options: any) => (note(options), model.stream(options)) } : {}), + }); +}; + +const real = engine.createModelResolver(); +const modelResolver = Object.assign( + (id: string, block?: string) => (id === "goal/broken" ? broken : noted((real as any)(id === "goal/real" ? REAL_MODEL : id, block))), + { resolveId: (id: string) => (id === "goal/real" ? REAL_MODEL : id) }, +); + +const wf = workforce as unknown as Record; +let flows: Record = {}; +const installation = wf.createWorkerInstallation({ standardWorkers: tree.workers, workerFlows: () => flows }); +flows = { + agent: wf.defineAgentWorkerFlow({ installation }), + coordinator: wf.defineCoordinatorFlow({ installation, routeModel: "goal/real" }), +}; +const copies = wf.hireWorkforce(installation, {}) as Array<{ id: string }>; + +const bearers = Object.values(PEOPLE).map((person) => + engine.createBearerSecretPrincipalResolver({ secret: person.bearer, principal: { userId: person.userId, orgId: ORG } }), +); +/** One secret per person; a request with none, or one nobody recognises, is refused. */ +const resolvePrincipal = async (context: unknown) => { + for (const verify of bearers) { + try { + const principal = await verify(context as never); + if (principal !== null) return principal; + break; + } catch (error) { + if (!(error instanceof engine.PrincipalResolutionError)) throw error; + } + } + throw new engine.PrincipalResolutionError("no verified bearer"); +}; + +export default engine.createFlowState({ + flows: Object.fromEntries(copies.map((copy) => [copy.id, copy])), + stores: { default: { primary: sqliteStores({ filename: STORE }) } }, + // Durable execution keeps wakes, so a notice owed when the server dies is paid when it is back (leg b). + durable: true, + modelResolver, + resolvePrincipal, +} as never); diff --git a/goals/workforce-coordination/runs-on-one-task/legs.mts b/goals/workforce-coordination/runs-on-one-task/legs.mts new file mode 100644 index 0000000000..7b1cf48561 --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/legs.mts @@ -0,0 +1,151 @@ +/** + * What Alice does in the closure's walk, over HTTP against the Lab served by Shift Manager's + * command, through the shipped clients. It only acts and waits; `run.mts` grades, from what + * Alice's actions answered and from the store file, read-only. + * + * Every wait is on the store: until a row the leg is about ends, then until nothing runs, then + * a grace for anything late (a second notice, a stray run). + */ +import { DatabaseSync } from "node:sqlite"; +import { PEOPLE } from "../../workers/files-and-splits-down-the-chain/lab/people.mts"; +import { connect, quiet, type Acted, type Shipped } from "../../workers/files-and-splits-down-the-chain/scripted.mts"; +import { allTaskRows, type StoredTask } from "../../coordinators/files-tasks-down-the-owners-chain/store.mts"; +import { CAST, type Cast } from "./cast.mts"; + +/** The wait after things go quiet, before the store is read. */ +export const GRACE_MS = 6_000; +const TERMINAL = new Set(["completed", "errored", "cancelled"]); +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + +/** Every row on a plan ledger (`plan//`), with its partition. */ +export function planRows(file: string): Array { + const db = new DatabaseSync(file, { readOnly: true }); + try { + db.exec("PRAGMA busy_timeout = 5000"); + return (db.prepare("SELECT resource_key, state FROM resource_state WHERE resource_key LIKE 'plan/%'").all() as Array<{ resource_key: string; state: string }>).map((row) => ({ + ...(JSON.parse(row.state) as StoredTask), + partition: decodeURIComponent(row.resource_key.split("/")[1] ?? ""), + })); + } finally { + db.close(); + } +} + +/** Wait until the row `pick` finds is terminal, then until the store is quiet, then a grace. */ +export async function settled(store: string, pick: (rows: StoredTask[]) => StoredTask | undefined, timeoutMs = 420_000): Promise { + const until = Date.now() + timeoutMs; + let row: StoredTask | undefined; + for (; Date.now() < until; await sleep(500)) { + row = pick(allTaskRows(store)); + if (row !== undefined && TERMINAL.has(row.status)) break; + } + await quiet(store, 300_000); + await sleep(GRACE_MS); + await quiet(store, 300_000); + return pick(allTaskRows(store)); +} + +/** The routed task a respond request filed, by its id's shape (`routed:::`). */ +export const routedFor = (requestId: string | null, delegate: string) => (rows: StoredTask[]) => + rows.find((r) => r.id.startsWith(`routed:${requestId}:`) && r.id.endsWith(`:${delegate}`)); + +/** Alice signed in to the Lab at `origin`. */ +export const alice = (shipped: Shipped, origin: string, store: string) => connect(shipped, origin, store, PEOPLE.alice); + +/** The message Alice sends launch. */ +export const askFor = (cast: Cast) => `Draft the launch post for ${cast.topic}. Use the word "${cast.word}".`; + +/** Leg a, the concept's walk less the call and the Inbox. */ +export interface WalkA { + conv: string; + turn: Acted; + /** Alice's list right after her message, and her read of the routed task before the follow-up. */ + listedAtOnce: Acted; + routed: StoredTask | undefined; + before: Acted | undefined; + followUp: Acted | undefined; + followUpRow: StoredTask | undefined; + after: Acted | undefined; + followUpRead: Acted | undefined; +} + +/** Leg a: Alice asks launch for a draft, waits for it, then files writer a follow-up from its task. */ +export async function walkA(o: { origin: string; store: string; shipped: Shipped; cast: Cast; say: (s: string) => void }): Promise { + const a = alice(o.shipped, o.origin, o.store); + const conv = await a.conversation("coordinator", CAST.launch); + o.say(`a: "${askFor(o.cast)}"`); + const turn = await a.act("coordinator", conv, "respond", { message: askFor(o.cast) }); + const listedAtOnce = await a.act("coordinator", conv, "listTasks_tasks", {}); + const routed = await settled(o.store, routedFor(turn.requestId, CAST.writer)); + if (routed === undefined || routed.status !== "completed") return { conv, turn, listedAtOnce, routed, before: undefined, followUp: undefined, followUpRow: undefined, after: undefined, followUpRead: undefined }; + const before = await a.act("coordinator", conv, "getTask_tasks", { id: routed.id }); + o.say(`a: the follow-up of ${routed.id}`); + const followUp = await a.act("coordinator", conv, "createTask_tasks", { + goal: `Write a new version of the post, half as long. Keep the word "${o.cast.word}".`, + followUpOf: routed.id, + }); + const fuId = (followUp.output as { taskId?: string } | undefined)?.taskId; + const followUpRow = fuId === undefined ? undefined : await settled(o.store, (rows) => rows.find((r) => r.id === fuId)); + const after = await a.act("coordinator", conv, "getTask_tasks", { id: routed.id }); + const followUpRead = fuId === undefined ? undefined : await a.act("coordinator", conv, "getTask_tasks", { id: fuId }); + return { conv, turn, listedAtOnce, routed, before, followUp, followUpRow, after, followUpRead }; +} + +/** Journey j1: Alice asks chief a question; editor files for researcher and waits in the same request. */ +export async function journeyHandsOut(o: { origin: string; store: string; shipped: Shipped; cast: Cast; say: (s: string) => void }) { + const a = alice(o.shipped, o.origin, o.store); + const conv = await a.conversation("coordinator", CAST.chief); + const message = `What is the battery life of ${o.cast.topic}?`; + o.say(`j1: "${message}"`); + const turn = await a.act("coordinator", conv, "respond", { message }); + const routed = await settled(o.store, routedFor(turn.requestId, CAST.editor)); + return { conv, turn, routed }; +} + +/** Journey j2: Alice steers one routed task while it waits on research, and cancels another. */ +export async function journeyCoordinator(o: { origin: string; store: string; shipped: Shipped; cast: Cast; say: (s: string) => void }) { + const a = alice(o.shipped, o.origin, o.store); + const steerConv = await a.conversation("coordinator", CAST.launch); + const cancelConv = await a.conversation("coordinator", CAST.launch); + const steerTurn = await a.act("coordinator", steerConv, "respond", { message: askFor(o.cast) }); + const cancelTurn = await a.act("coordinator", cancelConv, "respond", { message: `Draft a teaser for ${o.cast.topic}.` }); + // The steer lands while writer's task waits on its research: running, its filed work open. + let steer: Acted | undefined; + const steerAt = Date.now(); + for (const until = Date.now() + 180_000; Date.now() < until && steer === undefined; await sleep(250)) { + const rows = allTaskRows(o.store); + const routed = routedFor(steerTurn.requestId, CAST.writer)(rows); + if (routed === undefined || TERMINAL.has(routed.status)) continue; + if (!rows.some((r) => (r as { filedByTask?: string }).filedByTask === routed.id)) continue; + o.say(`j2: steering ${routed.id}`); + steer = await a.act("coordinator", steerConv, "updateTask_tasks", { id: routed.id, message: `Also use the word "${o.cast.steerWord}".` }); + } + let cancel: Acted | undefined; + const cancelled = routedFor(cancelTurn.requestId, CAST.writer)(allTaskRows(o.store)); + if (cancelled !== undefined) { + o.say(`j2: cancelling ${cancelled.id}`); + cancel = await a.act("coordinator", cancelConv, "updateTask_tasks", { id: cancelled.id, status: "cancelled" }); + } + const steered = await settled(o.store, routedFor(steerTurn.requestId, CAST.writer)); + const cancelledRow = await settled(o.store, routedFor(cancelTurn.requestId, CAST.writer), 60_000); + return { steerConv, cancelConv, steerTurn, cancelTurn, steer, steerWaitedMs: Date.now() - steerAt, cancel, steered, cancelledRow }; +} + +/** Leg b's first half: Alice's message on a Lab that dies once researcher's ending is written. */ +export async function walkUntilKilled(o: { origin: string; store: string; shipped: Shipped; cast: Cast; exited: Promise; say: (s: string) => void }) { + const a = alice(o.shipped, o.origin, o.store); + const conv = await a.conversation("coordinator", CAST.launch); + const turn = await a.act("coordinator", conv, "respond", { message: askFor(o.cast) }); + const died = await Promise.race([o.exited.then(() => true), sleep(300_000).then(() => false)]); + return { conv, turn, died, killedAt: Date.now() }; +} + +/** Leg c: Alice's message on a Lab whose researcher fails every call. */ +export async function walkFails(o: { origin: string; store: string; shipped: Shipped; cast: Cast; say: (s: string) => void }) { + const a = alice(o.shipped, o.origin, o.store); + const conv = await a.conversation("coordinator", CAST.launch); + o.say(`c: "${askFor(o.cast)}" (researcher's model refuses every call)`); + const turn = await a.act("coordinator", conv, "respond", { message: askFor(o.cast) }); + const routed = await settled(o.store, routedFor(turn.requestId, CAST.writer)); + return { conv, turn, routed }; +} diff --git a/goals/workforce-coordination/runs-on-one-task/rules.mjs b/goals/workforce-coordination/runs-on-one-task/rules.mjs new file mode 100644 index 0000000000..cf2140225d --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/rules.mjs @@ -0,0 +1,249 @@ +/** + * FIX-2087 task-surface rules. `engine.mjs` reads these for the goal and for CI, so the two + * give one answer on one commit (D2). + * + * {@link TOOLS_ALLOWED}: task tools the capability may declare. {@link REMOVED}: one matcher per + * name row of {@link MIGRATION_TABLE}. {@link BEHAVIOUR_ROWS}: the goal for a row with no name. + * A row with neither fails. {@link RECORDERS} may write an ending; {@link PASS_THROUGHS} and + * {@link FORWARDERS} are not recorders, and their callers are read instead. Each of those keys + * names exactly one function, or the sweep can't tell. A line that names a removed surface on + * purpose carries the `retired-name-refusal` marker. The lists change only with a reason a + * reviewer can read; FIX-2058, retiring `answerTask`, edits {@link TOOLS_ALLOWED}. + */ +import { REFUSAL_MARKER, ROOTS } from "../../vocabulary/one-word-per-thing/rules.mjs"; + +export { REFUSAL_MARKER, ROOTS }; + +/** The epic's migration table, read as it stands on the swept commit. */ +export const MIGRATION_TABLE = "specs/epics/FIX-2056/migration.md"; + +/** The task tools a turn may be offered. Anything else the capability declares fails. */ +export const TOOLS_ALLOWED = new Set([ + "createTask", "getTask", "listTasks", "updateTask", // FIX-2081: the filer's five + "reportProgress", // FIX-2081: the worker's progress checkpoint, one of the five + "answerTask", // FIX-2056 D2: kept until FIX-2058 retires it + "parkOnQuestion", // FIX-2056 D2: the worker's question park, kept until FIX-2058 +]); + +/** Code under the swept roots: the files the AST rules (tools, members, endings) read. */ +export const CODE = /^(packages|apps)\/.*\.(ts|tsx|mts|cts)$/; + +/** + * Test and fixture code. A test plays a worker or a board on purpose, and a refusal test names + * the surface it refuses, so the endings rule and the code matchers skip it; the behaviour it + * pins is graded by the children's goals. + */ +export const TEST_CODE = /(^|\/)(test|tests|__tests__|e2e|fixtures)\/|\.(test|spec|type-test)\.(ts|tsx|mts)$|\.d\.ts$|(^|\/)(stores\/testing|src\/testing)\//; + +/** Tracked files under ROOTS that are deliberately not swept by the removed rule, each with its reason. */ +export const EXCLUDE = [ + { path: /(^|\/)CHANGELOG\.md$/, why: "release history: records what shipped under the old names" }, + { + path: /^\.changeset\//, + why: "release notes: the migration table says each removal's changeset carries its row's line, so a changeset names the removed surface by design", + }, + { path: /^apps\/docs\/blog\//, why: "dated blog posts: history, not reference" }, + { path: /^goals\/workforce-coordination\/runs-on-one-task\//, why: "this sweep: its rules name every removed surface on purpose" }, + { + path: /^goals\//, + why: "goals: a goal drives the public surface, so one still driving a removed surface fails its own run (plan row 3); its verdict log is history, and a goal serving an older commit (GOAL_COMMIT) names that commit's surface by design", + }, + { path: TEST_CODE, why: "tests and fixtures: a test pins the refusal of what it names; the behaviour is graded by the children's goals" }, + { + path: /\.(png|jpe?g|gif|webp|ico|woff2?|ttf|otf|json|jsonl|ya?ml|lock|css|scss|html|txt|toml|sh|py|sql|snap|map|svg)$|(^|\/)\.[^/]+$|(^|\/)(LICENSE|Dockerfile)$/, + why: "no symbols in scope (data, assets, config, figures)", + }, + { path: /^(apps|packages)\/.*\.(md|mdx)$/, notIf: /^(apps\/docs\/(docs|guides)\/|packages\/[^/]+\/README\.md$)/, why: "markdown outside the published docs and package READMEs (fixtures, worker files, app notes)" }, +]; + +/** Code and the pages a user reads: where a removed name is live. */ +const SWEPT_TEXT = /^(packages|apps)\/.*\.(ts|tsx|mts|cts|js|mjs|cjs)$|^(apps\/docs\/(docs|guides)\/.*\.mdx?|packages\/[^/]+\/README\.md|docs\/architecture\/.*\.md)$/; +/** The pages a user reads. */ +const PAGES = /^(apps\/docs\/(docs|guides)\/.*\.mdx?|packages\/[^/]+\/README\.md|docs\/architecture\/.*\.md)$/; +/** Product code only (no tests): where a common word means the removed surface only in a declaration. */ +const PRODUCT = { files: CODE, notFiles: TEST_CODE }; + +const words = (...names) => new RegExp(`\\b(${names.join("|")})\\b`); + +/** + * One matcher per name row of the migration table. `row` is a phrase the engine finds in the + * row's "Removed" cell, so a row the table adds later with no matcher here fails the run. + * + * Kinds: + * - `text`: a line pattern over `files` (less `notFiles`). + * - `declared`: a property declared on an exported type (`type` in `module`), read by the + * TypeScript checker, so a renamed variable can't hide it. + * - `member`: a call of `name` on a value of that type, read by type. + * - `union`: a string member of an exported union type. + * - `door`: a worker flow's declared action (read by the goal, which builds the flows; the + * sweep reads the action keys of every worker flow file's `actions`). + */ +export const REMOVED = [ + // ---- tools and their `_tasks` actions (FIX-2081, FIX-2080, FIX-2083) ---- + // A tool's name means the tool where it is a tool's or an action's name: `named("x")`, a + // quoted `"x"` or `"x_"`, or a page's `x` tool. `addTask` is also the collection's + // method, which stays, so only those shapes are read for it. + { + row: "`addTask", + kind: "text", + files: SWEPT_TEXT, + // Only where the name is a tool's or an action's: the collection's `addTask` method stays. + pattern: /named\(\s*["']addTask["']|\b(name|toolName)\s*(:|===)\s*["'`]addTask["'`]|\baddTask_\w+|`addTask`\s+(tool|action)\b|\btool\s+`addTask`/, + owner: "FIX-2081", + }, + ...["dispatchTask", "readTask", "assignTask", "cancelTask", "completeTask", "failTask", "blockTask"].map((name) => ({ + row: `\`${name}`, + kind: "text", + files: SWEPT_TEXT, + pattern: new RegExp(`named\\(\\s*["']${name}["']|(?:`, never a file path, so moving a recorder costs no rule edit. + * Rule 18: the claim gate for a dispatched row, the drain for an inline one. Patterns keep + * their own boards' recorders (FIX-2056, "What stays as it is"), the drain there. + */ +export const RECORDERS = { + "orchestration:checkTask": "the claim gate: a dispatched task's request-end check fails the attempt (FIX-2077, FIX-2088)", + "orchestration:checkAnswered": "the claim gate: a dispatched task's request-end check settles it done (FIX-2077)", + "orchestration:settleAtRequestEnd": "the claim gate: the settle every request-end and recorder write passes through", + "orchestration:createRecordSuccess": "the drain's recorder: an inline row's result", + "orchestration:createRecordError": "the drain's recorder: an inline row's error (FIX-2090's refused run ends here)", + "orchestration:releaseRow": "the drain's recorder: a row whose recorder could not confirm its own write", + "patterns:createRecordArgument": "a pattern's own inline recorder (patterns stay)", + "patterns:createRecordContribution": "a pattern's own inline recorder (patterns stay)", + "patterns:routedSpecialists": "a pattern's own inline recorder (patterns stay)", +}; + +/** + * Collection wrappers: functions that build a task collection over another one and hand its + * `complete` or `fail` on (a bound method, or a method picked by name). Not recorders: the + * wrapper is itself a collection, so every call of its `complete` or `fail` is read in its place. + * Only the forwarding shapes are exempt; a direct `complete()`/`fail()` call inside one is a finding. + * Keyed like {@link RECORDERS}. + */ +export const FORWARDERS = { + "workforce:guarded": "the conversation board's ref: complete and fail bound through, deleting a finished top task's chain count", + "workforce:byRow": "the plan board: a write sent to the ledger holding the row (the plan's, or the board's)", + "workforce:notOnAStep": "the plan board: complete and fail refused on a step, else sent to the board", +}; + +/** + * Write helpers that pass an ending through: not recorders. Their own callers are read in + * their place, so a new caller of a helper is a finding like a direct write. + */ +export const PASS_THROUGHS = ["advisoryComplete", "advisoryFail", "advisoryCancel"]; diff --git a/goals/workforce-coordination/runs-on-one-task/run.mts b/goals/workforce-coordination/runs-on-one-task/run.mts new file mode 100644 index 0000000000..0a47723275 --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/run.mts @@ -0,0 +1,443 @@ +/** + * Goal check, FIX-2087: Workforce coordination runs on one task primitive. The closure of epic + * FIX-2056, run on one `main` commit with every other child merged. See goal.md. + * + * Run: pnpm tsx goals/workforce-coordination/runs-on-one-task/run.mts + */ +import { spawnSync } from "node:child_process"; +import { readFileSync, existsSync, rmSync } from "node:fs"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { REPO_ROOT, RUN_STAMP, gatewayModel, goalAttempts, goalModel, goalTmpDir, keysServing, runGoal, type Tell } from "../../lib/index.mts"; +import { assertPatched, describePatches, modulePatchEnv, type ModulePatch } from "../../lib/module-patch.mts"; +import { buildShiftManagerPages, startShiftManager, type ServedShiftManager } from "../../lib/shift-manager.mts"; +import { loadShipped, type Shipped } from "../../workers/files-and-splits-down-the-chain/scripted.mts"; +import { allTaskRows, items, requests, textOf, type StoredTask } from "../../coordinators/files-tasks-down-the-owners-chain/store.mts"; +import { KILL_AFTER_ENDING, PATCHES as ENDING_CONTROLS } from "../../task-board/every-ending-reaches-its-filer/controls.mts"; +import { PATCHES as FILING_CONTROLS } from "../../task-board/runs-by-its-filing-settings/controls.mts"; +import { CAST, pickCast, writeCast, type Cast } from "./cast.mts"; +import { journeyCoordinator, journeyHandsOut, planRows, walkA, walkFails, walkUntilKilled, type WalkA } from "./legs.mts"; +import { RAN, runChildren } from "./children.mts"; +import { printGaps } from "./gaps.mts"; + +const HERE = fileURLToPath(new URL(".", import.meta.url)); +const SCRATCH = goalTmpDir("one-task"); +const MODEL = gatewayModel(goalModel()); +/** The notice's due time after an ending (orchestration's `NOTICE_DUE_MS`). */ +const NOTICE_DUE_MS = 2_000; +const say = (s: string) => console.error(`[one-task ${new Date().toISOString().slice(11, 19)}] ${s}`); +const show = (value: unknown, n = 400) => JSON.stringify(value)?.slice(0, n); + +/** The task-surface sweep, the engine CI runs, as one JSON result. */ +interface Swept { + findings: Array<{ rule: string; where: string; what: string }>; + cantTell: string[]; + tools: string[]; + scanned: Record; + rows: Array<{ row: string; by: string }>; + allowed: string[]; +} +const sweepJson = (): Swept => JSON.parse(spawnSync(process.execPath, [join(HERE, "sweep.mjs"), "--json"], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }).stdout) as Swept; +let swept: Swept | undefined; +const allowedTools = () => new Set((swept ??= sweepJson()).allowed); +/** The tools a Lab worker's turn is handed that aren't task tools: the delegate read and the worker-to-worker message. */ +const NOT_TASK_TOOLS = new Set(["listDelegates", "messageWorker"]); + +// ---- D1: every assertion's kind --------------------------------------------------------------- + +type Kind = "structural" | "judgment"; +/** Each assertion and its kind. A structural FAIL is a finding on any attempt; a judgment FAIL may be retried. */ +export const TAGS: Record = { + "a:routed-listed": "structural", + "a:files-research": "judgment", + "a:ends-running": "structural", + "a:one-settled": "structural", + "a:step-dispatched": "judgment", + "a:drain-waits": "structural", + "a:answers-after-steps": "structural", + "a:lands-once": "structural", + "a:draft-content": "judgment", + "a:follow-up": "structural", + "a:follow-up-differs": "judgment", + "a:original-unchanged": "structural", + "a:once": "structural", + "j1:routed": "structural", + "j1:files": "judgment", + "j1:waits-in-request": "structural", + "j1:answer": "judgment", + "j1:tools": "structural", + "j1:read-once": "structural", + "j1:heard-once": "structural", + "j1:once": "structural", + "j2:steered": "structural", + "j2:steer-shows": "judgment", + "j2:cancelled": "structural", + "j2:cancel-heard-once": "structural", + "b:routed": "structural", + "b:files-research": "judgment", + "b:killed": "structural", + "b:hears-in-due-time": "structural", + "b:once": "structural", + "c:routed": "structural", + "c:files-dependent": "judgment", + "c:fails-out": "structural", + "c:dependent-cancelled": "structural", + "c:one-failure-notice": "structural", + "c:writer-ends": "structural", + "c:once": "structural", +}; +const tagOf = (failure: string) => failure.split(" — ")[0]!; +const kindOf = (failure: string): Kind => TAGS[tagOf(failure)] ?? "structural"; + +// ---- controls --------------------------------------------------------------------------------- + +interface Control { + leg: "b" | "c"; + mustFail: string; + patches: ModulePatch[]; +} +const CONTROLS: Record = { + "no-sweeper-payment": { leg: "b", mustFail: "b:hears-in-due-time", patches: ENDING_CONTROLS["no-sweeper-payment"]! }, + "no-cascade": { leg: "c", mustFail: "c:dependent-cancelled", patches: FILING_CONTROLS["no-cascade"]! }, +}; +const controlName = process.env.GOAL_CONTROL ?? ""; +if (controlName !== "" && CONTROLS[controlName] === undefined) { + console.error(`unknown GOAL_CONTROL "${controlName}"; known: ${Object.keys(CONTROLS).join(", ")}`); + process.exit(2); +} +const control = controlName === "" ? undefined : CONTROLS[controlName]; +const parts = new Set((process.env.GOAL_PARTS ?? (control !== undefined ? "walk" : "walk,sweep,children,gaps")).split(",").map((s) => s.trim())); +const walkLegs = new Set((process.env.GOAL_LEGS ?? (control !== undefined ? control.leg : "a,b,c")).split(",").map((s) => s.trim())); + +// ---- the Lab ---------------------------------------------------------------------------------- + +let pages: string | undefined; +/** Serve the closure's Lab over `store` and `tree`, with `patches` applied as it loads. */ +async function serve(label: string, store: string, tree: string, patches: ModulePatch[] = [], env: Record = {}): Promise<{ served: ServedShiftManager; mark: string }> { + pages ??= await buildShiftManagerPages(join(SCRATCH, "pages")); + const mark = join(SCRATCH, `patch-mark-${label}-${RUN_STAMP}`); + rmSync(mark, { force: true }); + const served = await startShiftManager({ + scratch: SCRATCH, + label, + config: join(HERE, "lab", "fsdev.config.mts"), + pages, + env: { + GOAL_STORE: store, + GOAL_TREE: tree, + GOAL_REAL_MODEL: MODEL, + GOAL_TOOLS_LOG: `${store}.tools.ndjson`, + ...(patches.length > 0 ? modulePatchEnv(patches, REPO_ROOT, mark) : {}), + ...env, + }, + timeoutMs: 180_000, + }); + return { served, mark }; +} + +// ---- store reads ------------------------------------------------------------------------------ + +const lines = (store: string, sessionId: string) => items(store, sessionId, "message").filter((m) => m.role === "assistant"); +const linesBy = (store: string, sessionId: string, agent: string) => lines(store, sessionId).filter((m) => m.agentName === agent); +const filedBy = (store: string, taskId: string) => allTaskRows(store).filter((r) => (r as { filedByTask?: string }).filedByTask === taskId); +const settledRequests = (store: string, sessionId: string | undefined) => requests(store).filter((r) => r.sessionId === sessionId && r.actionName === "onTaskSettled"); +/** + * The `onTaskSettled` requests that admitted a notice (`owed: true`), so ran the filer's turn or + * line. A notice delivered twice starts a second request, which finds it already taken (FIX-2123's + * claim) and runs nothing, output `{ owed: false }`: that is no second hearing. + */ +const admittedRequests = (store: string, sessionId: string | undefined) => settledRequests(store, sessionId).filter((r) => (r.output as { owed?: unknown } | undefined)?.owed === true); +const outputText = (output: unknown) => (typeof output === "string" ? output : JSON.stringify(output ?? "")); +const taskOf = (acted: { output?: any } | undefined) => acted?.output?.task ?? acted?.output; + +// ---- grading ---------------------------------------------------------------------------------- + +function gradeA(store: string, w: WalkA, cast: Cast, tell: Tell): string[] { + const f: string[] = []; + const R = w.routed; + const listed = (w.listedAtOnce.output?.tasks ?? []) as Array<{ id: string; assignee?: string }>; + if (!listed.some((t) => t.id.startsWith(`routed:${w.turn.requestId}:`) && t.assignee === CAST.writer)) { + f.push(`a:routed-listed — Alice's listTasks_tasks right after her message holds no routed task for desk.writer: ${show(w.listedAtOnce.output)}`); + } + if (R === undefined) return [...f, `a:ends-running — no routed task for desk.writer was filed`]; + const writerSession = R.run?.sessionId; + const research = tell.over("a · research rows writer filed", filedBy(store, R.id).filter((r) => r.assignee === CAST.researcher)); + if (research.length === 0) f.push(`a:files-research — writer filed nothing for desk.researcher`); + const work = requests(store).find((r) => r.sessionId === writerSession && r.actionName === "work"); + const lastEnd = Math.max(...research.map((r) => r.completedAt ?? r.updatedAt ?? Infinity)); + // Its first request ends with the research it filed open, and the task stays running past + // both: nothing settles it until a request after the research's ending. + if (research.length > 0 && !(work !== undefined && work.status === "completed" && (R.completedAt ?? Infinity) > Math.max(work.updatedAt, lastEnd))) { + f.push(`a:ends-running — wanted writer's task to stay running past its first request and its research: work ${show(work)}, research ended ${lastEnd}, task ${R.status} at ${R.completedAt}`); + } + const settled = settledRequests(store, writerSession); + const admitted = admittedRequests(store, writerSession); + if (research.length > 0 && admitted.length !== research.length) { + f.push(`a:one-settled — ${admitted.length} onTaskSettled request(s) admitted a notice in writer's task session for ${research.length} research ending(s) (${settled.length} in all, outputs ${show(settled.map((r) => r.output), 200)})`); + } + const steps = planRows(store).filter((r) => r.partition.startsWith(`${writerSession}~`) && (r as { session?: string }).session === "new"); + const step = steps[0]; + if (step === undefined) f.push(`a:step-dispatched — writer filed no step for itself in a session of its own`); + else { + if (step.status !== "completed" || step.run?.sessionId === writerSession) f.push(`a:step-dispatched — the fact-check step ${step.status}, run in ${step.run?.sessionId}`); + if (!((R.completedAt ?? 0) >= (step.completedAt ?? Infinity))) f.push(`a:drain-waits — writer's task ended at ${R.completedAt}, before its dispatched step at ${step.completedAt}`); + const answeredAfter = items(store, writerSession ?? "", "message").filter((m) => m.role === "assistant" && m.agentName === "agent-answer" && Number(m.ts ?? 0) > (step.completedAt ?? Infinity)); + if (answeredAfter.length === 0) { + f.push(`a:answers-after-steps — writer was given no turn after its dispatched step ended; its task completed with its earlier answer ${show(R.output, 120)}`); + } + } + const draft = outputText(R.output); + const fu = w.followUpRow; + // A line is matched to its ending by text. When the model writes the follow-up word for word + // like the original, the two lines read alike, so each is told apart by when it landed: the + // original's before the follow-up ended, the follow-up's after. + const fuEnded = fu?.status === "completed" && outputText(fu.output) === draft ? fu.completedAt : undefined; + const draftLines = linesBy(store, w.conv, CAST.writer).filter((m) => textOf(m) === draft && (fuEnded === undefined || Number(m.ts ?? 0) < fuEnded)); + if (R.status !== "completed" || draftLines.length !== 1) f.push(`a:lands-once — writer's task ${R.status}; ${draftLines.length} line(s) of its result in Alice's conversation`); + if (!draft.includes(cast.word) || !draft.includes(cast.factMark)) f.push(`a:draft-content — the draft lacks ${!draft.includes(cast.word) ? `the word "${cast.word}"` : ""} ${!draft.includes(cast.factMark) ? `researcher's fact (${cast.factMark})` : ""}: ${show(draft, 200)}`); + // The follow-up's result is a turn of writer's after it was filed, not the original's answer handed back. + const fuAnswered = fu === undefined ? [] : lines(store, writerSession ?? "").filter((m) => m.agentName === "agent-answer" && Number(m.ts ?? 0) >= (fu.createdAt ?? Infinity) && textOf(m) === outputText(fu.output)); + if (w.followUp === undefined) f.push(`a:follow-up — not filed: writer's task never completed`); + else if (fu === undefined || fu.status !== "completed" || (fu as { followUpOf?: string }).followUpOf !== R.id || !fu.partition.startsWith(w.conv) || fu.run?.sessionId !== writerSession || fuAnswered.length === 0) { + f.push(`a:follow-up — wanted a completed follow-up of ${R.id}, filed from Alice's conversation, run in writer's task session, answered by a turn of writer's after it was filed (${fuAnswered.length}): ${show(w.followUp.output)} ${show({ ...fu, output: outputText(fu?.output).slice(0, 80) })}`); + } else if (outputText(fu.output) === draft) { + f.push(`a:follow-up-differs — writer's follow-up turn wrote the original word for word: ${show(draft, 160)}`); + } + if (w.before !== undefined && JSON.stringify(taskOf(w.before)?.output) !== JSON.stringify(taskOf(w.after)?.output)) { + f.push(`a:original-unchanged — the original's output changed: ${show(taskOf(w.before)?.output, 120)} then ${show(taskOf(w.after)?.output, 120)}`); + } + for (const r of research) { + const heard = lines(store, writerSession ?? "").filter((m) => textOf(m).includes(`(${r.id})`)); + if (heard.length !== 1) f.push(`a:once — researcher's ending of ${r.id} reached writer ${heard.length} time(s)`); + } + if (fu?.status === "completed" && linesBy(store, w.conv, CAST.writer).filter((m) => textOf(m) === outputText(fu.output) && (fuEnded === undefined || Number(m.ts ?? 0) >= fuEnded)).length !== 1) f.push(`a:once — the follow-up's result did not land once in Alice's conversation`); + return f; +} + +function gradeJ1(store: string, j: Awaited>, cast: Cast, tell: Tell): string[] { + const f: string[] = []; + const E = j.routed; + if (E === undefined) return [`j1:routed — no routed task for desk.editor`]; + const editorSession = E.run?.sessionId; + const research = filedBy(store, E.id).filter((r) => r.assignee === CAST.researcher); + if (research.length === 0) f.push(`j1:files — editor filed nothing for desk.researcher`); + const works = requests(store).filter((r) => r.sessionId === editorSession && r.actionName === "work"); + const read = items(store, editorSession ?? "", "tool_output").filter( + (i) => i.toolCall?.name === "getTask" && works.some((w) => w.id === i.requestId) && JSON.stringify(i.output ?? "").includes('"status":"completed"'), + ); + if (research.length > 0 && (E.status !== "completed" || works.length !== 1 || read.length === 0)) { + f.push(`j1:waits-in-request — wanted editor's one work request to read the research's ending with getTask: task ${E.status}, ${works.length} work request(s), ${read.length} completed getTask read(s)`); + } + // An ending that lands before the getTask wait still starts an onTaskSettled request: nothing + // is waiting yet, so a notice is owed. After the wait reads it, that request finds the notice + // taken and returns `{ owed: false }` with no turn (FIX-2112). Only `owed: true` counts. + // goals/task-board/waits-on-a-task-it-filed counts the same way. + const notices = settledRequests(store, editorSession); + const heardBy = admittedRequests(store, editorSession); + if (research.length > 0 && read.length > 0 && heardBy.length > 0) { + const ended = research[0]!.completedAt ?? 0; + const asked = Math.min(...read.map((i) => Number(i.ts ?? Infinity))); + f.push(`j1:read-once — researcher's ending reached editor twice: its work request read it with getTask (${asked - ended} ms after the ending) and ${heardBy.length} onTaskSettled turn(s) ran for it too (${notices.length} request(s) in all)`); + } + // Discounting `{ owed: false }` must not pass a run that never read the ending, or one that + // admitted it twice. Every completed read counts, two in one request included. + if (research.length > 0 && (read.length !== 1 || heardBy.length !== 0)) { + f.push(`j1:heard-once — wanted researcher's ending heard exactly once, by editor's getTask read: ${read.length} completed read(s) in ${new Set(read.map((i) => i.requestId)).size} request(s), ${heardBy.length} notice turn(s); ${notices.length} onTaskSettled request(s), outputs ${show(notices.map((r) => r.output), 200)}`); + } + if (!outputText(E.output).includes(cast.factMark)) f.push(`j1:answer — editor's answer lacks researcher's fact: ${show(E.output, 160)}`); + const offered = tell.over( + "j1 · tool lists the engine handed a task turn", + readNdjson<{ who: string; tools: string[] }>(`${store}.tools.ndjson`).filter((c) => c.who === CAST.editor || c.who === CAST.writer), + ); + // Every tool offered is either on the allow list or one of the roster's own (NOT_TASK_TOOLS), + // so a new task tool fails here whatever it is called, not only one named like a task tool. + const allowed = allowedTools(); + const beyond = [...new Set(offered.flatMap((c) => c.tools))].filter((n) => !allowed.has(n) && !NOT_TASK_TOOLS.has(n)); + if (beyond.length > 0) f.push(`j1:tools — a task turn was offered ${beyond.join(", ")}, beyond the allow list and the roster's own tools`); + if (linesBy(store, j.conv, CAST.editor).filter((m) => textOf(m) === outputText(E.output)).length !== 1) f.push(`j1:once — editor's answer did not land once in Alice's conversation with chief`); + return f; +} + +function gradeJ2(store: string, j: Awaited>, cast: Cast): string[] { + const f: string[] = []; + if (j.steer === undefined || j.steer.output?.ok !== true) f.push(`j2:steered — Alice's steer of an open task was not taken: ${show(j.steer)}`); + if (!outputText(j.steered?.output).includes(cast.steerWord)) f.push(`j2:steer-shows — the steered draft lacks "${cast.steerWord}": ${show(j.steered?.output, 200)}`); + if (j.cancel === undefined || j.cancel.output?.ok !== true || j.cancelledRow?.status !== "cancelled") f.push(`j2:cancelled — Alice's cancel: ${show(j.cancel?.output)}, the row ${j.cancelledRow?.status}`); + // The coordinator hears the cancel once (its round closes on it); a routed task's cancel lands no line (coordinators.md). + const heard = admittedRequests(store, j.cancelConv); + const said = linesBy(store, j.cancelConv, CAST.writer); + if (j.cancelledRow?.status === "cancelled" && (heard.length !== 1 || said.length !== 0)) f.push(`j2:cancel-heard-once — ${heard.length} onTaskSettled in launch's conversation for the cancel, ${said.length} line(s) from writer: ${show(said.map(textOf))}`); + return f; +} + +function readNdjson(file: string): T[] { + return existsSync(file) ? readFileSync(file, "utf8").split("\n").filter(Boolean).map((l) => JSON.parse(l) as T) : []; +} + +// ---- the walk --------------------------------------------------------------------------------- + +/** Each walk leg run, and whether its last attempt passed: row 4 reads it. */ +const legPassed = new Map(); + +/** One leg's attempts under D1: retried only while every failure is a judgment miss. */ +async function attempts(leg: string, once: (attempt: number) => Promise, report: string[], failures: string[]): Promise { + legPassed.set(leg, false); + const max = control === undefined ? goalAttempts() : 1; + for (let attempt = 1; attempt <= max; attempt += 1) { + let got: string[]; + try { + got = await once(attempt); + } catch (error) { + got = [`${leg}:setup — ${error instanceof Error ? (error.stack ?? error.message).slice(0, 1200) : String(error)}`]; + } + const structural = got.filter((g) => kindOf(g) === "structural"); + report.push(`${leg} attempt ${attempt}/${max}: ${got.length === 0 ? "PASS" : `FAIL ${[...new Set(got.map((g) => `${tagOf(g)} (${kindOf(g)})`))].join(", ")}`}`); + for (const g of got) report.push(` ${g}`); + if (got.length === 0) { + legPassed.set(leg, true); + return; + } + if (structural.length > 0 || attempt === max) { + failures.push(...got); + return; + } + } +} + +async function walk(failures: string[], tell: Tell, report: string[]): Promise { + const keys = keysServing(MODEL); + if (!keys.some((k) => (process.env[k] ?? "") !== "")) { + failures.push(`walk:blocked — no key here serves ${MODEL}; set one of ${keys.join(", ")}`); + return; + } + const shipped: Shipped = await loadShipped(REPO_ROOT); + report.push(`model ${MODEL}`); + + if (walkLegs.has("a")) { + await attempts("a", async (attempt) => { + const cast = pickCast(); + const store = join(SCRATCH, `a-${attempt}-${RUN_STAMP}.sqlite`); + const tree = writeCast(join(SCRATCH, `tree-a-${attempt}-${RUN_STAMP}`), cast); + report.push(`a attempt ${attempt}: ${cast.topic}, word "${cast.word}", fact "${cast.fact}", steer "${cast.steerWord}", store ${store}`); + const { served } = await serve(`a-${attempt}`, store, tree); + try { + const w = await walkA({ origin: served.origin, store, shipped, cast, say }); + const j1 = await journeyHandsOut({ origin: served.origin, store, shipped, cast, say }); + const j2 = await journeyCoordinator({ origin: served.origin, store, shipped, cast, say }); + return [...gradeA(store, w, cast, tell), ...gradeJ1(store, j1, cast, tell), ...gradeJ2(store, j2, cast)]; + } finally { + await served.stop(); + } + }, report, failures); + } + + if (walkLegs.has("b")) { + await attempts("b", async (attempt) => { + const cast = pickCast(); + const store = join(SCRATCH, `b-${attempt}-${RUN_STAMP}.sqlite`); + const tree = writeCast(join(SCRATCH, `tree-b-${attempt}-${RUN_STAMP}`), cast); + const f: string[] = []; + const first = await serve(`b-${attempt}-killed`, store, tree, [KILL_AFTER_ENDING], { GOAL_KILL: "after-ending" }); + const k = await walkUntilKilled({ origin: first.served.origin, store, shipped, cast, exited: first.served.exited, say }); + if (!k.died) { + await first.served.stop(); + return [`b:killed — the server never died after an ending: nothing reached the delivery's send`]; + } + assertPatched([KILL_AFTER_ENDING], REPO_ROOT, first.mark); + const R = allTaskRows(store).find((r) => r.id.startsWith(`routed:${k.turn.requestId}:`)); + const research = R === undefined ? [] : filedBy(store, R.id).filter((r) => r.assignee === CAST.researcher); + const ended = research.find((r) => r.status === "completed"); + if (R === undefined) return [`b:routed — no routed task for desk.writer`]; + if (research.length === 0) return [`b:files-research — writer filed nothing for desk.researcher before the server died`]; + if (ended === undefined || !Object.keys(ended.metadata ?? {}).some((m) => m.startsWith("noticeOwed:"))) { + return [`b:killed — wanted researcher's ending written with its notice owed when the server died: ${show({ R: R?.status, research: research.map((r) => [r.status, Object.keys(r.metadata ?? {})]) })}`]; + } + const writerSession = R.run?.sessionId; + const before = admittedRequests(store, writerSession).length; + const patches = control?.leg === "b" ? control.patches : []; + const second = await serve(`b-${attempt}-restarted`, store, tree, patches); + const back = Date.now(); + try { + // No action on any board: only a wake can pay what is owed. + await new Promise((r) => setTimeout(r, NOTICE_DUE_MS + 10_000)); + if (patches.length > 0) assertPatched(patches, REPO_ROOT, second.mark); + const heard = admittedRequests(store, writerSession); + const due = Math.max(back, (ended.completedAt ?? back) + NOTICE_DUE_MS); + const at = heard[0]?.createdAt; + if (heard.length === 0 || at === undefined || at - due > 2_000) f.push(`b:hears-in-due-time — ${heard.length} onTaskSettled in writer's session after the restart; the first ${at === undefined ? "never" : `${at - due} ms past its due time`}`); + if (heard.length + before !== 1) f.push(`b:once — writer heard researcher's ending ${heard.length + before} time(s) (${before} before the kill)`); + report.push(`b attempt ${attempt}: killed with ${ended.id} ended and its notice owed; restarted ${back - k.killedAt} ms later; due ${due - back} ms after the restart; heard ${at === undefined ? "never" : `${at - due} ms past due`}`); + } finally { + await second.served.stop(); + } + return f; + }, report, failures); + } + + if (walkLegs.has("c")) { + await attempts("c", async (attempt) => { + const cast = pickCast(); + const store = join(SCRATCH, `c-${attempt}-${RUN_STAMP}.sqlite`); + const tree = writeCast(join(SCRATCH, `tree-c-${attempt}-${RUN_STAMP}`), cast, "fails"); + const patches = control?.leg === "c" ? control.patches : []; + const { served, mark } = await serve(`c-${attempt}`, store, tree, patches); + const f: string[] = []; + try { + const c = await walkFails({ origin: served.origin, store, shipped, cast, say }); + if (patches.length > 0) assertPatched(patches, REPO_ROOT, mark); + const R = c.routed; + if (R === undefined) return [`c:routed — no routed task for desk.writer`]; + const research = filedBy(store, R.id).filter((r) => r.assignee === CAST.researcher); + const dependent = research.find((r) => ((r as { deps?: string[] }).deps ?? []).length > 0); + const first = research.find((r) => r !== dependent && ((dependent as { deps?: string[] } | undefined)?.deps ?? []).includes(r.id)); + if (dependent === undefined || first === undefined) return [`c:files-dependent — wanted a research task and one depending on it: ${show(research.map((r) => [r.id, (r as any).deps]))}`]; + if (first.status !== "errored") f.push(`c:fails-out — the research task ended ${first.status}`); + if (dependent.status !== "cancelled") f.push(`c:dependent-cancelled — the dependent ended ${dependent.status}`); + const writerSession = R.run?.sessionId ?? ""; + const notices = lines(store, writerSession).filter((m) => textOf(m).includes(`(${first.id})`)); + const depNotices = lines(store, writerSession).filter((m) => textOf(m).includes(`(${dependent.id})`) && !textOf(m).includes(`(${first.id})`)); + if (notices.length !== 1 || !(textOf(notices[0]!).includes(dependent.id) || textOf(notices[0]!).includes(dependent.goal.slice(0, 30))) || depNotices.length !== 0) { + f.push(`c:one-failure-notice — ${notices.length} notice(s) of the failure, naming the dependent: ${notices[0] === undefined ? "-" : textOf(notices[0]).includes(dependent.id)}; ${depNotices.length} of the dependent: ${show(notices.map(textOf), 300)}`); + } + // Writer's task must end: one still open when the walk stops was never told how its research ended. + if (!["completed", "errored", "cancelled"].includes(R.status)) f.push(`c:writer-ends — writer's task is still ${R.status} when the walk stops`); + else if (linesBy(store, c.conv, CAST.writer).length !== 1) f.push(`c:once — writer's task ${R.status}; ${linesBy(store, c.conv, CAST.writer).length} line(s) in Alice's conversation`); + report.push(`c attempt ${attempt}: research ${first.status}, dependent ${dependent.status}, writer's task ${R.status}; notice ${show(notices.map(textOf), 240)}`); + } finally { + await served.stop(); + } + return f; + }, report, failures); + } +} + +// ---- the plan --------------------------------------------------------------------------------- + +await runGoal(async (failures, tell) => { + const report: string[] = []; + const head = spawnSync("git", ["-C", REPO_ROOT, "rev-parse", "HEAD"], { encoding: "utf8" }).stdout.trim(); + report.push(`commit ${head}${control === undefined ? "" : `, control ${controlName}:\n${describePatches(control.patches, REPO_ROOT)}`}`); + if (parts.has("walk")) await walk(failures, tell, report); + let sweptOk: boolean | undefined; + if (parts.has("sweep")) { + const plain = (swept ??= sweepJson()); + tell.over("sweep · task tools the capability declares", plain.tools); + for (const finding of plain.findings) failures.push(`sweep:${finding.rule} — ${finding.where} ${finding.what}`); + for (const reason of plain.cantTell) failures.push(`sweep:cant-tell — ${reason}`); + report.push(`sweep: ${plain.tools.length} tools, ${plain.scanned.endings} ending writes, ${plain.rows.length} migration rows; ${plain.findings.length} live lines`); + const ctl = spawnSync(process.execPath, [join(HERE, "sweep.mjs"), "--control"], { encoding: "utf8" }); + if (ctl.status !== 0) failures.push(`sweep:control — the plants were not reported exactly:\n${ctl.stdout.slice(-1500)}`); + report.push(`sweep --control: ${/CONTROL (OK|BROKEN).*/.exec(ctl.stdout)?.[0] ?? "no verdict"}`); + sweptOk = plain.findings.length === 0 && plain.cantTell.length === 0 && ctl.status === 0; + } + if (parts.has("children")) await runChildren(failures, report, say, Object.keys(TAGS)); + if (parts.has("gaps")) printGaps(report, failures, { tags: Object.keys(TAGS), legs: legPassed, sweep: sweptOk, checks: RAN }); + if (control !== undefined) { + const red = failures.filter((x) => tagOf(x) === control.mustFail); + report.push(red.length > 0 ? `control ${controlName}: ${control.mustFail} FAILed, as it must` : `control ${controlName}: ${control.mustFail} stayed green: the control is broken`); + // A broken control must not read as a pass, even when nothing else went red. + if (red.length === 0) failures.push(`control:broken — ${controlName} left ${control.mustFail} green, so it shows nothing`); + } + for (const line of report) console.error(line); + return { failures, evidence: `the closure's plan on ${head.slice(0, 9)}: ${[...parts].join(", ")}` }; +}); diff --git a/goals/workforce-coordination/runs-on-one-task/sweep.mjs b/goals/workforce-coordination/runs-on-one-task/sweep.mjs new file mode 100644 index 0000000000..66a92b4d1e --- /dev/null +++ b/goals/workforce-coordination/runs-on-one-task/sweep.mjs @@ -0,0 +1,264 @@ +#!/usr/bin/env node +/** + * FIX-2087 task-surface sweep: no task tool beyond the five (and `answerTask` and + * `parkOnQuestion` until FIX-2058), nothing the epic's migration table removes is live, and + * nothing records a task's ending except the claim gate or the drain. CI's Typecheck job runs + * it, and again with `--control`; `run.mts` runs the same engine as the goal's sweep leg. + * + * node goals/workforce-coordination/runs-on-one-task/sweep.mjs # report + verdict + * node goals/workforce-coordination/runs-on-one-task/sweep.mjs --rows # how each migration row is covered + * node goals/workforce-coordination/runs-on-one-task/sweep.mjs --control # every plant must be reported + * node goals/workforce-coordination/runs-on-one-task/sweep.mjs --json # the result as one JSON line (run.mts) + * + * Exit 0 = PASS, 1 = FAIL, 3 = CAN'T TELL (a rule read nothing, an allowed tool wasn't read, or a + * recorder's name names more than one function). + */ +import { execFileSync } from "node:child_process"; +import { mkdtempSync, readFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { sweepTaskSurface } from "./engine.mjs"; +import { MIGRATION_TABLE, ROOTS, TOOLS_ALLOWED } from "./rules.mjs"; + +const args = new Set(process.argv.slice(2)); +const repo = execFileSync("git", ["rev-parse", "--show-toplevel"], { encoding: "utf8" }).trim(); + +/** The capability's file, copied into plants so a planted tool is read the way the real ones are. */ +const CAPABILITY = "packages/orchestration/src/skills/task-tools-capability.ts"; +const COLLECTION = 'import type { TaskCollectionRef, TaskClaimTicket } from "@flow-state-dev/orchestration/tasks";\n'; + +/** + * The negative control: plants per rule, each in a file the sweep reads through the same path as + * a real one. Each must be reported `expect` times (default once) and nothing else may change. + * `cantTell` is how many CAN'T TELL lines a plant adds. + */ +function plantsOf(root) { + const capability = readFileSync(join(root, CAPABILITY), "utf8"); + const anchor = " const createTask = defineTool({"; + if (!capability.includes(anchor) || !capability.includes("const defineTool = ")) throw new Error(`the control can't plant: ${CAPABILITY} has no \`${anchor.trim()}\``); + // A copy of the capability with its local alias renamed, a sixth tool built with it, one with a + // template name, two built as a sequencer and a generator under renamed imports, and two tools + // imported from sibling files: one through a renamed import of `handler`, one through a + // namespace import. + const copy = + 'import { nudge } from "./__planted_side_tool__";\nimport { prod } from "./__planted_namespace_tool__";\n' + + 'import { sequencer as seq2, generator as gen2 } from "@flow-state-dev/core";\nexport const seqTask = seq2({ name: "seqTask" } as any);\nexport const genTask = gen2({ name: "genTask" } as any);\n' + + capability + .replace(anchor, ' const poke = defineTool({ name: named("pokeTask"), description: "Poke a task." } as any);\n const twice = defineTool({ name: `poke${"Twice"}` } as any);\n' + anchor) + .replaceAll("defineTool", "makeTool"); + return [ + { rule: "tools", path: "packages/orchestration/src/skills/__planted_capability__.ts", text: copy, expect: 4 }, + { + rule: "tools", + path: "packages/orchestration/src/skills/__planted_side_tool__.ts", + text: 'import { handler as defineIt } from "@flow-state-dev/core";\nexport const nudge = defineIt({ name: "nudgeTask" } as any);\n', + }, + { + rule: "tools", + path: "packages/orchestration/src/skills/__planted_namespace_tool__.ts", + text: 'import * as core from "@flow-state-dev/core";\nexport const prod = core.handler({ name: "prodTask" } as any);\n', + }, + { + rule: "removed", + path: "packages/workforce/src/__planted_removed__.ts", + text: "export const legacy = { tool: \"dispatchTask\" };\n", + }, + { + rule: "endings", + path: "packages/workforce/src/__planted_complete__.ts", + text: COLLECTION + "export async function wrapUp(rows: TaskCollectionRef, taskId: string, output: unknown) {\n const renamed = rows;\n await renamed.complete(taskId, output);\n}\n", + }, + { + rule: "endings", + path: "packages/workforce/src/__planted_cancel__.ts", + text: COLLECTION + "export async function giveUp(board: TaskCollectionRef, id: string, claim: TaskClaimTicket) {\n await board.cancel(id, \"done here\", { claim });\n}\n", + }, + { + rule: "endings", + path: "packages/orchestration/src/__planted_renamed_helper__.ts", + text: 'import { advisoryComplete as done } from "./tasks/collection/advisory-write-back";\nexport async function finish(ref: any, id: string) {\n await done(ref, id, "ok");\n}\n', + }, + { + rule: "endings", + path: "packages/orchestration/src/__planted_namespace_helper__.ts", + text: 'import * as writes from "./tasks/collection/advisory-write-back";\nexport async function flop(ref: any, id: string) {\n await writes.advisoryFail(ref, id, "no");\n}\n', + }, + { + rule: "endings", + path: "packages/workforce/src/__planted_destructured__.ts", + text: COLLECTION + "export async function wrapUp(rows: TaskCollectionRef, id: string) {\n const { complete } = rows;\n await complete(id, 1);\n}\n", + }, + { + rule: "endings", + path: "packages/workforce/src/__planted_element__.ts", + text: COLLECTION + 'export async function giveUp(rows: TaskCollectionRef, id: string) {\n await rows["fail"](id, "no");\n}\n', + }, + { + rule: "endings", + path: "packages/workforce/src/__planted_call__.ts", + text: COLLECTION + "export async function wrapUp(rows: TaskCollectionRef, id: string) {\n await rows.complete.call(rows, id, 1);\n}\n", + }, + { + // A claim-passing cancel in each indirect shape: `.call`, `.apply`, `.bind`, destructured, element access. + rule: "endings", + path: "packages/workforce/src/__planted_cancel_shapes__.ts", + text: + COLLECTION + + "export async function giveUp(board: TaskCollectionRef, id: string, claim: TaskClaimTicket) {\n" + + ' await board.cancel.call(board, id, "done", { claim });\n' + + ' await board.cancel.apply(board, [id, "done", { claim }]);\n' + + " const later = board.cancel.bind(board);\n" + + " const { cancel: stop } = board;\n" + + ' await stop(id, "done", { claim });\n' + + ' await board["cancel"](id, "done", { claim });\n' + + " return later;\n}\n", + expect: 5, + }, + { + // The same shapes with no claim are a plain cancel, not an ending: nothing is reported. + rule: "endings", + path: "packages/workforce/src/__planted_cancel_unclaimed__.ts", + text: + COLLECTION + + "export async function dropIt(board: TaskCollectionRef, id: string) {\n" + + ' await board.cancel.call(board, id, "not needed");\n' + + ' await board.cancel.apply(board, [id, "not needed"]);\n' + + " const { cancel: stop } = board;\n" + + ' await stop(id, "not needed");\n' + + ' await board["cancel"](id, "not needed");\n}\n', + expect: 0, + }, + ...doorPlants(root), + forwarderPlant(root), + { + rule: "endings", + path: "packages/orchestration/src/__planted_bound_helper__.ts", + text: 'import * as writes from "./tasks/collection/advisory-write-back";\nconst { advisoryFail: f } = writes;\nexport async function flop2(ref: any, id: string) {\n await f(ref, id, "no");\n}\n', + }, + { + rule: "endings", + path: "packages/orchestration/src/__planted_passed_helper__.ts", + text: 'import { advisoryComplete } from "./tasks/collection/advisory-write-back";\nexport async function later(run: (...a: any[]) => Promise, ref: any, id: string) {\n await run(ref, id, "ok");\n}\nexport const go = (ref: any, id: string) => later(advisoryComplete, ref, id);\n', + }, + { + // A second function named like a recorder: its write would be read as the recorder's. + rule: "endings", + path: "packages/orchestration/src/__planted_recorder__.ts", + text: COLLECTION + "export async function releaseRow(rows: TaskCollectionRef, id: string) {\n await rows.complete(id, 1);\n}\n", + cantTell: 1, + }, + ]; +} + +/** + * The floor's control: the real capability with `createTask` built by something other than + * `handler`. The tools rule then can't read an allowed name, and must say it can't tell. + */ +/** + * A removed door brought back in each built-in worker flow: `message` in the agent's (its config + * is `configSchema: settings` and `...bound`, so no text names the worker contract), and `run` + * under a computed key in the coordinator's (its config sits under `as never`). + */ +const AGENT_FLOW = "packages/workforce/src/agent-worker-flow.ts"; +const COORDINATOR_FLOW = "packages/workforce/src/coordinator/coordinator-flow.ts"; +function doorPlants(root) { + const agent = readFileSync(join(root, AGENT_FLOW), "utf8"); + const coordinator = readFileSync(join(root, COORDINATOR_FLOW), "utf8"); + const anchor = " [STANDING_RULES_ACTION]: standingRulesAction"; + if (agent.split(anchor).length !== 2 || coordinator.split(anchor).length !== 2) throw new Error(`the control can't plant: ${AGENT_FLOW} and ${COORDINATOR_FLOW} each need one \`${anchor.trim()}\``); + return [ + { rule: "removed", path: AGENT_FLOW, text: agent.replace(anchor, ` message: { block: userTurn } as any,\n${anchor}`) }, + { rule: "removed", path: COORDINATOR_FLOW, text: coordinator.replace(anchor, ` [("run") as "run"]: { block: door } as any,\n${anchor}`) }, + ]; +} + +/** Writes planted inside a collection wrapper: only its forwarding is exempt, so each is reported. */ +const BOARD = "packages/workforce/src/conversation-board/board.ts"; +function forwarderPlant(root) { + const board = readFileSync(join(root, BOARD), "utf8"); + const anchor = " const ending =\n (write:"; + if (!board.includes(anchor)) throw new Error(`the control can't plant: ${BOARD} has no \`const ending =\` in guarded`); + // A direct call, and calls through `.call` and `.apply`: each a write made in the forwarder, not a forwarding. + const writes = ' void ref.fail("planted", "a direct write in a forwarder");\n' + ' void ref.fail.call(ref, "planted", "a write through .call");\n' + ' void ref.complete.apply(ref, ["planted", 1] as any);\n'; + return { rule: "endings", path: BOARD, text: board.replace(anchor, writes + anchor), expect: 3 }; +} + +function floorPlant(root) { + const capability = readFileSync(join(root, CAPABILITY), "utf8"); + const anchor = "const createTask = defineTool({"; + if (!capability.includes(anchor)) throw new Error(`the control can't plant: ${CAPABILITY} has no \`${anchor}\``); + return [{ path: CAPABILITY, text: capability.replace(anchor, "const createTask = ((config: any) => config)({") }]; +} + +const started = Date.now(); +const control = args.has("--control"); +// `--at `: sweep another commit's tree (extracted with `git archive`), against this +// checkout's migration table, rules and plants: how today's main is shown to FAIL. +const atIndex = process.argv.indexOf("--at"); +const at = atIndex >= 0 ? process.argv[atIndex + 1] : undefined; +let root = repo; +let tracked; +if (at !== undefined) { + root = mkdtempSync(join(tmpdir(), "task-surface-")); + execFileSync("sh", ["-c", `git -C "${repo}" archive --format=tar "${at}" | tar -x -C "${root}"`]); + tracked = execFileSync("git", ["-C", repo, "ls-tree", "-r", "--name-only", at, "--", ...ROOTS], { encoding: "utf8" }).split("\n").filter(Boolean); +} +const table = at === undefined ? undefined : join(repo, MIGRATION_TABLE); +const plain = sweepTaskSurface({ repo: root, tracked, table }); +const PLANTS = control ? plantsOf(root) : []; +const result = control ? sweepTaskSurface({ repo: root, tracked, table, plants: PLANTS }) : plain; +const floor = control ? sweepTaskSurface({ repo: root, tracked, table, plants: floorPlant(root) }) : undefined; +const secs = ((Date.now() - started) / 1000).toFixed(1); +if (args.has("--json")) { + const { findings, cantTell, tools, scanned, rows } = result; + console.log(JSON.stringify({ findings, cantTell, tools, scanned, rows, allowed: [...TOOLS_ALLOWED] })); + process.exit(0); +} + +const head = at ?? execFileSync("git", ["rev-parse", "--short", "HEAD"], { cwd: repo, encoding: "utf8" }).trim(); +const excluded = [...result.files.excluded.values()].reduce((a, b) => a + b, 0); +console.log(`commit ${head} · ${secs}s`); +console.log(`files: ${result.files.tracked} tracked · ${result.files.swept} swept by the removed rule · ${excluded} excluded · ${result.files.program} in the program`); +for (const [why, n] of result.files.excluded) console.log(` excluded ${n} · ${why}`); +console.log(`tools offered (${result.tools.length}): ${result.tools.join(", ")}`); +console.log(`worker flows read for doors (${result.workerFlows.length}): ${result.workerFlows.join(", ")}`); +console.log(`ending writes: ${result.scanned.endings} · by recorder ${JSON.stringify(result.recorders)}`); +console.log(`removed: ${result.rows.length} migration rows · ${result.scanned.removedText} text hits (${result.scanned.marked} marked retired-name-refusal · ${result.scanned.kept} kept by reason) · ${result.scanned.removedTyped} typed reads`); +if (args.has("--rows")) for (const r of result.rows) console.log(` ${r.by.padEnd(28)} ${r.row.slice(0, 110)}`); + +const byRule = (rule) => result.findings.filter((f) => f.rule === rule); +if (control) { + const key = (f) => `${f.rule} ${f.where} ${f.what}`; + const before = new Set(plain.findings.map(key)); + const added = result.findings.filter((f) => !before.has(key(f))); + // A finding's place is `path:line`, or several joined by " and " (a key naming two functions). + const at = (f, p) => f.rule === p.rule && f.where.split(" and ").some((w) => w.startsWith(`${p.path}:`)); + const of = (p) => added.filter((f) => at(f, p)); + const missed = PLANTS.filter((p) => of(p).length !== (p.expect ?? 1)); + const stray = added.filter((f) => !PLANTS.some((p) => at(f, p))); + for (const p of PLANTS) console.log(` plant ${p.rule.padEnd(8)} ${p.path}: ${of(p).length} of ${p.expect ?? 1} reported${missed.includes(p) ? " MISSED" : ""}`); + for (const f of stray) console.log(` stray ${f.rule} ${f.where} ${f.what}`); + const wantCantTell = plain.cantTell.length + PLANTS.reduce((n, p) => n + (p.cantTell ?? 0), 0); + const floorOk = floor.cantTell.some((c) => c.startsWith("tools:") && c.includes("createTask")) && floor.findings.length === plain.findings.length; + console.log(` floor createTask built apart from handler: ${floorOk ? "CAN'T TELL, as it must" : `NOT REPORTED (${JSON.stringify(floor.cantTell)})`}`); + const expected = PLANTS.reduce((n, p) => n + (p.expect ?? 1), 0); + if (missed.length > 0 || stray.length > 0 || added.length !== expected || result.cantTell.length !== wantCantTell || !floorOk) { + console.log(`CONTROL BROKEN: ${missed.length} plant(s) not reported as expected, ${stray.length} other line(s) changed, ${added.length} added for ${expected} expected, ${result.cantTell.length} CAN'T TELL for ${wantCantTell}, floor ${floorOk ? "ok" : "missed"}`); + process.exit(1); + } + console.log(`CONTROL OK: the plain run's ${plain.findings.length} live lines, exactly the ${expected} planted ones from ${PLANTS.length} plants, and the floor`); + process.exit(0); +} + +if (result.scanned.tools === 0 || result.scanned.endings === 0 || result.rows.length === 0 || result.cantTell.length > 0) { + console.log(`CAN'T TELL — a rule read nothing (tools ${result.scanned.tools}, endings ${result.scanned.endings}, rows ${result.rows.length})${result.cantTell.map((c) => `\n ${c}`).join("")}`); + for (const f of result.findings) console.log(` ${f.rule.padEnd(8)} ${f.where} ${f.what}`); + process.exit(3); +} +if (result.findings.length === 0) { + console.log("PASS — no task tool beyond the allow list, nothing the migration table removes is live, every ending write is a recorder's"); + process.exit(0); +} +console.log(`FAIL — ${result.findings.length} live lines (tools ${byRule("tools").length} · removed ${byRule("removed").length} · endings ${byRule("endings").length})`); +for (const f of result.findings) console.log(` ${f.rule.padEnd(8)} ${f.where} ${f.what}`); +process.exit(1); diff --git a/specs/issues/FIX-1792/poc/inventory/check.mjs b/specs/issues/FIX-1792/poc/inventory/check.mjs index d6c505c10e..a3dcd00500 100644 --- a/specs/issues/FIX-1792/poc/inventory/check.mjs +++ b/specs/issues/FIX-1792/poc/inventory/check.mjs @@ -526,6 +526,7 @@ const GOALS = { "goals/shift-manager/one-person-runs-a-labs-projects-and-people": "CONVERT · after FIX-1793 retires its room legs, its MAILBOX.md key-list seam reads WORKER.md", "goals/task-run-link/it-names-the-run-working-each-task": "CONVERT · rows on a conversation's board", "goals/vocabulary/one-word-per-thing": "GUARD · FIX-1876's vocabulary sweep; it names post-to-mailbox only as a retired name it fails on", + "goals/workforce-coordination/runs-on-one-task": "GUARD · FIX-2087's closure; it names mailbox-left only as the control of this issue's goal check that it runs", "goals/workforce-conventions/a-mailbox-holds-the-work-a-seat-drains": "RETIRE · its subjects are gone (an unattended file board, a kind of its own, two boards on one mailbox); FIX-1791's and FIX-1794's goal checks and kitchen-sink-talk prove the rest", "goals/workforce-conventions/capabilities-come-from-files-alone": "EDIT · a generated module's empty mailboxKinds", "goals/workforce-conventions/code-comes-from-files-alone": "REWRITE · its flows/mailboxes leg goes with the slot",