Public: anyone can read this; the crew can change it.
Knobs: one panel on the crew page with five plain dials, each showing its live effect, editable in place #10476
scout/forge:scout/10461-knobs-one-panel-on-the-crew-page-with-fi → main 4 commits fork
26 files
changed +623
−74
docs/features/knobs.md added +12
| @@ -0,0 +1,12 @@ | ||
| 1 | # Knobs | |
| 2 | ||
| 3 | Five dials for the crew, on **Settings › Crew** (admin), in the terminal's palette (`Knobs`) and in the MCP `knobs` tool, in the same words (#10461). | |
| 4 | ||
| 5 | 1. **Speed**: crew runs at once (`crew.concurrency`). The pace floor (half, at least 1) and the merge-queue depth are derived from it. | |
| 6 | 2. **Weekly budget**: percent of the week to use (`week.use`, default 90); the rest is the reserve the pacer keeps. | |
| 7 | 3. **Polish share**: percent of the week's spend polish may take (`polishShare`, applied to every repository the crew works). The small-issue slot share is fixed at 30% and the polish run cap at 60 minutes: no longer dials. | |
| 8 | 4. **Autonomy**: per repository, `off`, `ask` (plans need a nod, decisions come to you) or `go` (the decider answers, plans open). | |
| 9 | 5. **Feedback first**: always on; shown, not tuned. | |
| 10 | ||
| 11 | A change applies on the next pump and leaves the event the setting's own page leaves (`capacity.changed`, `repo.settings_updated`). | |
| 12 | Not done: the per-repository dollar ceiling (`autonomyCeilingUsd`) and `reserve.ts`'s two-per-slot reserve still stand. |
scripts/unreached-allow.json −1
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | "src/flow/review_rounds.ts", |
| 16 | 16 | "src/flow/shared_red.ts", |
| 17 | 17 | "src/flow/stale_fold.ts", |
| 18 | "src/kernel/why_now.ts", | |
| 19 | 18 | "src/spawn/pod.ts", |
| 20 | 19 | "src/tui/gallery.ts", |
| 21 | 20 | "src/tui/ui/qr.ts", |
src/actions/knobs.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** | |
| 2 | * Turning a knob (#10461), an admin's: each one writes through the setting | |
| 3 | * the forge already keeps and leaves the same event that setting's own page | |
| 4 | * leaves (`capacity.changed`, `repo.settings_updated`), so the feed says who | |
| 5 | * turned what. The next pump reads the change; nothing restarts. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { changeCapacity } from "./capacity.ts"; | |
| 10 | import { updateRepoSettings } from "./repos.ts"; | |
| 11 | import { getRepo } from "../core/repos.ts"; | |
| 12 | import { AUTONOMY_WORDS, knobRepos, type AutonomyWord, type KnobId } from "../crew/knobs.ts"; | |
| 13 | import { validPolishShare } from "../crew/small_share.ts"; | |
| 14 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 15 | import type { User } from "../core/users.ts"; | |
| 16 | ||
| 17 | export type KnobError = "not_found" | "no_such_knob" | "invalid_value"; | |
| 18 | ||
| 19 | const WORDS: Record<AutonomyWord, { autonomy: "off" | "autonomous"; plansNeedNod: boolean; decisionsBy: "me" | "decider" }> = { | |
| 20 | off: { autonomy: "off", plansNeedNod: true, decisionsBy: "me" }, | |
| 21 | ask: { autonomy: "autonomous", plansNeedNod: true, decisionsBy: "me" }, | |
| 22 | go: { autonomy: "autonomous", plansNeedNod: false, decisionsBy: "decider" }, | |
| 23 | }; | |
| 24 | ||
| 25 | export async function setKnob( | |
| 26 | app: App, | |
| 27 | actor: User | undefined, | |
| 28 | knob: string, | |
| 29 | value: string, | |
| 30 | repoPath?: string, | |
| 31 | ): Promise<Result<{ knob: KnobId; value: string }, KnobError>> { | |
| 32 | if (actor?.isAdmin !== true || actor.kind === "agent") return err("not_found"); | |
| 33 | const v = value.trim(); | |
| 34 | if (knob === "speed" || knob === "budget") { | |
| 35 | const changed = changeCapacity(app, actor, knob === "speed" ? "crew.concurrency" : "week.use", v); | |
| 36 | if (!changed.ok) return err(changed.error === "not_found" ? "not_found" : "invalid_value"); | |
| 37 | return ok({ knob, value: v }); | |
| 38 | } | |
| 39 | if (knob === "polish") { | |
| 40 | const n = /^\d+$/.test(v) ? Number(v) : Number.NaN; | |
| 41 | if (!validPolishShare(n)) return err("invalid_value"); | |
| 42 | for (const repo of knobRepos(app).filter((r) => r.autonomy !== "off")) { | |
| 43 | const done = await updateRepoSettings(app, actor, repo, { polishShare: n }); | |
| 44 | if (!done.ok) return err("invalid_value"); | |
| 45 | } | |
| 46 | return ok({ knob, value: v }); | |
| 47 | } | |
| 48 | if (knob === "autonomy") { | |
| 49 | if (!(AUTONOMY_WORDS as readonly string[]).includes(v)) return err("invalid_value"); | |
| 50 | const [owner, name] = (repoPath ?? "").split("/"); | |
| 51 | const repo = owner === undefined || name === undefined ? undefined : getRepo(app.db, owner, name); | |
| 52 | if (repo === undefined) return err("not_found"); | |
| 53 | const done = await updateRepoSettings(app, actor, repo, WORDS[v as AutonomyWord]); | |
| 54 | return done.ok ? ok({ knob, value: v }) : err("invalid_value"); | |
| 55 | } | |
| 56 | return err("no_such_knob"); | |
| 57 | } |
src/core/capacity.ts +29 −42
| @@ -19,17 +19,14 @@ export type CapacityKey = | ||
| 19 | 19 | | "crew.concurrency" |
| 20 | 20 | | "checks.concurrency" |
| 21 | 21 | | "checks.shards" |
| 22 | | "queue.depth" | |
| 23 | 22 | | "queue.batch" |
| 23 | | "week.use" | |
| 24 | 24 | | "children.max" |
| 25 | 25 | | "cells.egress" |
| 26 | 26 | | "children.modelEgress" |
| 27 | 27 | | "children.registryEgress" |
| 28 | 28 | | "guard_new_accounts" |
| 29 | | "signin.edge" | |
| 30 | | "slots.smallShare" | |
| 31 | | "polish.capMinutes" | |
| 32 | | "pace.floor"; | |
| 29 | | "signin.edge"; | |
| 33 | 30 | |
| 34 | 31 | interface Definition { |
| 35 | 32 | /** What the settings page and the API call it. */ |
| @@ -71,13 +68,6 @@ export const CAPACITY: Record<CapacityKey, Definition> = { | ||
| 71 | 68 | sized: (cpus) => shardsForCpus(cpus), |
| 72 | 69 | help: "How many parallel shards a check's test run uses (handed to checks and crew shells as FORGE_CHECK_SHARDS). Empty: the pod's CPUs, at most 4 — and, in a check, no more than its cell holds.", |
| 73 | 70 | }, |
| 74 | "queue.depth": { | |
| 75 | label: "Merge queue candidates at once", | |
| 76 | env: "FORGE_QUEUE_DEPTH", | |
| 77 | kind: { type: "count", min: 1, max: 4 }, | |
| 78 | fallback: "2", | |
| 79 | help: "How many merge-queue candidates of one repository gate at once, each on top of the one before: while one gates on main the next gates on it, and lands right after it if both are green. 1 gates one candidate at a time. A gate beyond the first takes a free check slot only, never one of the two kept for pull checks.", | |
| 80 | }, | |
| 81 | 71 | "queue.batch": { |
| 82 | 72 | label: "Merge queue batch size", |
| 83 | 73 | env: "FORGE_QUEUE_BATCH", |
| @@ -85,6 +75,13 @@ export const CAPACITY: Record<CapacityKey, Definition> = { | ||
| 85 | 75 | fallback: "8", |
| 86 | 76 | help: "The most pulls one merge-queue candidate carries. A bigger batch lands more pulls per green gate; a red one costs more search gates to find its culprit (one bad pull in 16 is found in two rounds, at most √16 = 4 gates each). The queue starts each line here, halves the batch after a red gate and doubles it back after two green gates in a row, never past this. Read on every sweep.", |
| 87 | 77 | }, |
| 78 | "week.use": { | |
| 79 | label: "Share of the week to use (%)", | |
| 80 | env: "FORGE_WEEK_USE", | |
| 81 | kind: { type: "count", min: 10, max: 100 }, | |
| 82 | fallback: "90", | |
| 83 | help: "The weekly budget knob: how much of the week's subscription the crew may spend, evenly to the reset. The rest is kept for conversations and a person's own requests; the crew stops at it. Read on every pump.", | |
| 84 | }, | |
| 88 | 85 | "children.max": { |
| 89 | 86 | label: "Forges this one may send", |
| 90 | 87 | env: "FORGE_CHILDREN_MAX", |
| @@ -127,27 +124,6 @@ export const CAPACITY: Record<CapacityKey, Definition> = { | ||
| 127 | 124 | fallback: "on", |
| 128 | 125 | help: "on reads the platform edge's identity headers (when the forge has an edge secret) and offers its sign-in; off ignores them on every door, so people sign in with the forge's own methods — password and passkey. Accounts the edge made keep their names but have no password until they set one. Takes effect on the next request.", |
| 129 | 126 | }, |
| 130 | "slots.smallShare": { | |
| 131 | label: "Builder slots small issues may hold (%)", | |
| 132 | env: "FORGE_SLOTS_SMALL_SHARE", | |
| 133 | kind: { type: "count", min: 0, max: 100 }, | |
| 134 | fallback: "30", | |
| 135 | help: "The most of a repository's builder slots that tests-only and small-addition issues (\"A test pins …\", or a label test, tests, tests-only or small) hold at once, as a percent rounded up: 30 lets 2 of 5 slots go to them. At the cap a small issue is skipped and the slot goes to the next issue in the Next up order; a ranked small issue is still taken. 100 lifts the cap; 0 starts none. Read on every pick.", | |
| 136 | }, | |
| 137 | "polish.capMinutes": { | |
| 138 | label: "Run time a polish issue may take (minutes)", | |
| 139 | env: "FORGE_POLISH_CAP_MINUTES", | |
| 140 | kind: { type: "count", min: 1, max: 1440 }, | |
| 141 | fallback: "60", | |
| 142 | help: "An issue labelled polish is small work: a builder run on it that has gone this many minutes is stopped, not extended, and the issue gets one comment saying what is left. Read on every tick.", | |
| 143 | }, | |
| 144 | "pace.floor": { | |
| 145 | label: "Slots the pace always leaves reviews and assigned work", | |
| 146 | env: "FORGE_PACE_FLOOR", | |
| 147 | kind: { type: "count", min: 1, max: 16 }, | |
| 148 | fallback: "2", | |
| 149 | help: "While the week's pace allows any slots, this many reviews and issues a person assigned may run at once, apart from the crew's own picks, however hard the pace slows them; also the fewest slots a fast burn slows a repository to. At the reserve's wall everything waits. Read on every pump.", | |
| 150 | }, | |
| 151 | 127 | }; |
| 152 | 128 | |
| 153 | 129 | export const CAPACITY_KEYS = Object.keys(CAPACITY) as CapacityKey[]; |
| @@ -365,9 +341,20 @@ export function capacityCount( | ||
| 365 | 341 | return Number.isInteger(n) && n > 0 ? n : fallback; |
| 366 | 342 | } |
| 367 | 343 | |
| 368 | /** The slots the pace leaves reviews and assigned work (`pace.floor`). */ | |
| 344 | /** Speed: the crew runs at once now (`crew.concurrency`), the one number the derived dials below follow. */ | |
| 345 | export function speed(db: Database): number { | |
| 346 | const n = Number(capacityValue(db, "crew.concurrency")); | |
| 347 | return Number.isInteger(n) && n > 0 ? n : 2; | |
| 348 | } | |
| 349 | ||
| 350 | /** The slots the pace leaves reviews and assigned work: half of Speed, at least 1 (derived, #10461). */ | |
| 369 | 351 | export function paceFloor(db: Database): number { |
| 370 | return Number(capacityValue(db, "pace.floor")); | |
| 352 | return Math.max(1, Math.floor(speed(db) / 2)); | |
| 353 | } | |
| 354 | ||
| 355 | /** The share of the week the crew may use, in percent (`week.use`); the rest is the reserve. */ | |
| 356 | export function weekUse(db: Database): number { | |
| 357 | return Number(capacityValue(db, "week.use")); | |
| 371 | 358 | } |
| 372 | 359 | |
| 373 | 360 | /** How many hosted children this forge may hold now; read on every use. */ |
| @@ -408,7 +395,7 @@ export const BUILD_HOSTS: readonly string[] = [ | ||
| 408 | 395 | |
| 409 | 396 | /** How many merge-queue candidates of one repository gate at once (#4053). */ |
| 410 | 397 | export function queueDepth(db: Database): number { |
| 411 | return Number(capacityValue(db, "queue.depth")); | |
| 398 | return Math.min(4, Math.max(1, Math.ceil(speed(db) / 2))); | |
| 412 | 399 | } |
| 413 | 400 | |
| 414 | 401 | /** The most pulls one merge-queue candidate carries (#6674); the line adapts below it. */ |
| @@ -416,14 +403,14 @@ export function queueBatch(db: Database): number { | ||
| 416 | 403 | return Number(capacityValue(db, "queue.batch")); |
| 417 | 404 | } |
| 418 | 405 | |
| 419 | /** The percent of builder slots small issues may hold (#7571). */ | |
| 420 | export function smallShare(db: Database): number { | |
| 421 | return Number(capacityValue(db, "slots.smallShare")); | |
| 406 | /** The percent of builder slots small issues may hold: fixed, no longer a dial (#10461); the polish share is the dial. */ | |
| 407 | export function smallShare(_db: Database): number { | |
| 408 | return 30; | |
| 422 | 409 | } |
| 423 | 410 | |
| 424 | /** The minutes of run time a polish issue may take before its run is stopped (#8852). */ | |
| 425 | export function polishCapMinutes(db: Database): number { | |
| 426 | return Number(capacityValue(db, "polish.capMinutes")); | |
| 411 | /** The minutes of run time a polish issue may take before its run is stopped: fixed, no longer a dial (#10461). */ | |
| 412 | export function polishCapMinutes(_db: Database): number { | |
| 413 | return 60; | |
| 427 | 414 | } |
| 428 | 415 | |
| 429 | 416 | /** Whether the platform edge's identity is read at all; on every request. */ |
src/crew/knobs.ts added +141
| @@ -0,0 +1,141 @@ | ||
| 1 | /** | |
| 2 | * The five knobs (#10461): Speed, Weekly budget, Polish share, Autonomy per | |
| 3 | * repository, Feedback first. Plain data out: one row each with its name, a | |
| 4 | * line on what it does, the value, a control kind and its live effect. The | |
| 5 | * crew page, the terminal and the MCP `knobs` tool print these same rows; | |
| 6 | * `actions/knobs.ts` is the one place that turns them. Everything else | |
| 7 | * (pace floor, queue depth, the reserve) is derived from them or fixed. | |
| 8 | */ | |
| 9 | ||
| 10 | import type { App } from "../app.ts"; | |
| 11 | import { capacityValue, paceFloor, queueDepth, speed } from "../core/capacity.ts"; | |
| 12 | import { labelsForIssues } from "../core/issues.ts"; | |
| 13 | import { getRepoById, type Repo } from "../core/repos.ts"; | |
| 14 | import { listActive } from "../core/runs.ts"; | |
| 15 | import { waitingOnPeople } from "./people_first.ts"; | |
| 16 | import { POLISH_SHARE_DEFAULT, isPolish, polishSpend } from "./small_share.ts"; | |
| 17 | import { budgetEffect } from "./week_read.ts"; | |
| 18 | ||
| 19 | export type KnobId = "speed" | "budget" | "polish" | "autonomy" | "feedback"; | |
| 20 | export type AutonomyWord = "off" | "ask" | "go"; | |
| 21 | export const AUTONOMY_WORDS: readonly AutonomyWord[] = ["off", "ask", "go"]; | |
| 22 | ||
| 23 | export interface KnobRow { | |
| 24 | readonly id: KnobId; | |
| 25 | readonly name: string; | |
| 26 | readonly does: string; | |
| 27 | /** The value as typed back: a number, a percent's number, or one of the words. */ | |
| 28 | readonly value: string; | |
| 29 | readonly control: "number" | "percent" | "words" | "fixed"; | |
| 30 | readonly min?: number; | |
| 31 | readonly max?: number; | |
| 32 | readonly words?: readonly string[]; | |
| 33 | /** What it does right now, in a line. */ | |
| 34 | readonly effect: string; | |
| 35 | /** For Autonomy: the repository (`owner/name`) this row is for. */ | |
| 36 | readonly repo?: string; | |
| 37 | /** Derived dials this knob drives, shown read-only. */ | |
| 38 | readonly derived?: string; | |
| 39 | } | |
| 40 | ||
| 41 | /** A repository's autonomy in the knob's three words. */ | |
| 42 | export function autonomyWord(repo: Repo): AutonomyWord { | |
| 43 | if (repo.autonomy === "off") return "off"; | |
| 44 | return !repo.plansNeedNod && repo.decisionsBy !== "me" ? "go" : "ask"; | |
| 45 | } | |
| 46 | ||
| 47 | function runsLine(app: App): string { | |
| 48 | const active = listActive(app.db, "agent"); | |
| 49 | const running = active.filter((r) => r.state === "running").length; | |
| 50 | return `${running} running · ${active.length - running} queued`; | |
| 51 | } | |
| 52 | ||
| 53 | function polishWaiting(app: App, repos: readonly Repo[]): number { | |
| 54 | let n = 0; | |
| 55 | for (const repo of repos) { | |
| 56 | const rows = app.db | |
| 57 | .query<{ id: string; title: string }, [string]>("SELECT id, title FROM issue WHERE repo_id = ?1 AND state = 'open' AND assignee_id IS NULL") | |
| 58 | .all(repo.id); | |
| 59 | const labels = labelsForIssues(app.db, rows.map((r) => r.id)); | |
| 60 | n += rows.filter((r) => isPolish(r.title, (labels.get(r.id) ?? []).map((l) => l.name))).length; | |
| 61 | } | |
| 62 | return n; | |
| 63 | } | |
| 64 | ||
| 65 | /** The repositories the Autonomy knob lists: awake ones, those with the crew on first. */ | |
| 66 | export function knobRepos(app: App, limit = 12): Repo[] { | |
| 67 | return app.db | |
| 68 | .query<{ id: string }, [number]>("SELECT id FROM repo WHERE archived_at IS NULL ORDER BY autonomy = 'off', name LIMIT ?1") | |
| 69 | .all(limit) | |
| 70 | .flatMap((r) => getRepoById(app.db, r.id) ?? []); | |
| 71 | } | |
| 72 | ||
| 73 | /** The five knobs now (Autonomy is one row per repository). */ | |
| 74 | export function readKnobs(app: App, now: Date = new Date()): KnobRow[] { | |
| 75 | const repos = knobRepos(app); | |
| 76 | const live = repos.filter((r) => r.autonomy !== "off"); | |
| 77 | const share = live[0]?.polishShare ?? POLISH_SHARE_DEFAULT; | |
| 78 | const spend = live[0] === undefined ? undefined : polishSpend(app.db, live[0].id, share, now); | |
| 79 | const waiting = waitingOnPeople(app.db, listActive(app.db, "agent").filter((r) => r.state === "queued")); | |
| 80 | const hhmm = waiting.since === null ? "" : ` since ${waiting.since.slice(11, 16)}`; | |
| 81 | const rows: KnobRow[] = [ | |
| 82 | { | |
| 83 | id: "speed", | |
| 84 | name: "Speed", | |
| 85 | does: "How many crew runs go at once.", | |
| 86 | value: String(speed(app.db)), | |
| 87 | control: "number", | |
| 88 | min: 1, | |
| 89 | max: 16, | |
| 90 | effect: runsLine(app), | |
| 91 | derived: `pace floor ${paceFloor(app.db)} · merge queue depth ${queueDepth(app.db)} (derived)`, | |
| 92 | }, | |
| 93 | { | |
| 94 | id: "budget", | |
| 95 | name: "Weekly budget", | |
| 96 | does: "How much of the week's subscription to use, evenly to the reset.", | |
| 97 | value: capacityValue(app.db, "week.use"), | |
| 98 | control: "percent", | |
| 99 | min: 10, | |
| 100 | max: 100, | |
| 101 | effect: budgetEffect(app, now), | |
| 102 | }, | |
| 103 | { | |
| 104 | id: "polish", | |
| 105 | name: "Polish share", | |
| 106 | does: "The share of the week's spend polish may take.", | |
| 107 | value: String(share), | |
| 108 | control: "percent", | |
| 109 | min: 0, | |
| 110 | max: 50, | |
| 111 | effect: `polish ${spend === undefined ? 0 : Math.round(spend.percent)}% of this week's spend · ${polishWaiting(app, live)} polish issues waiting`, | |
| 112 | }, | |
| 113 | ]; | |
| 114 | for (const repo of repos) { | |
| 115 | const word = autonomyWord(repo); | |
| 116 | rows.push({ | |
| 117 | id: "autonomy", | |
| 118 | name: "Autonomy", | |
| 119 | does: "off: the crew leaves it alone · ask: plans need a nod, decisions come to you · go: the decider answers, plans open.", | |
| 120 | value: word, | |
| 121 | control: "words", | |
| 122 | words: AUTONOMY_WORDS, | |
| 123 | repo: `${repo.ownerName}/${repo.name}`, | |
| 124 | effect: word === "off" ? "off · the crew does not work here" : `${word} · ${repo.autonomySlots} builder slots`, | |
| 125 | }); | |
| 126 | } | |
| 127 | rows.push({ | |
| 128 | id: "feedback", | |
| 129 | name: "Feedback first", | |
| 130 | does: "A person's comment, assignment or note from an app starts ahead of everything. Always on.", | |
| 131 | value: "on", | |
| 132 | control: "fixed", | |
| 133 | effect: waiting.count === 0 ? "nothing from a person waiting" : `${waiting.count} waiting${hhmm}`, | |
| 134 | }); | |
| 135 | return rows; | |
| 136 | } | |
| 137 | ||
| 138 | /** The knobs as the plain lines the terminal and the MCP print. */ | |
| 139 | export function knobLines(rows: readonly KnobRow[]): string[] { | |
| 140 | return rows.map((r) => `${r.name}${r.repo === undefined ? "" : ` (${r.repo})`}: ${r.value}${r.control === "percent" ? "%" : ""} — ${r.effect}`); | |
| 141 | } |
src/crew/people_first.ts +15
| @@ -55,3 +55,18 @@ export function peopleFirst( | ||
| 55 | 55 | if (first.length === 0 || first.length === queued.length) return [...queued]; |
| 56 | 56 | return [...first, ...queued.filter((r) => !personal(r))]; |
| 57 | 57 | } |
| 58 | ||
| 59 | /** | |
| 60 | * The queued runs that carry a person's ask and the time the oldest one | |
| 61 | * began waiting (the Feedback-first knob's live effect, #10461): a person's | |
| 62 | * comment, a note from an app, or work a person assigned. | |
| 63 | */ | |
| 64 | export function waitingOnPeople(db: Database, queued: readonly Run[]): { count: number; since: string | null } { | |
| 65 | const fromApp = fromAppIssues(db, queued.flatMap((r) => (r.issueId === null ? [] : [r.issueId]))); | |
| 66 | const spoke = people(db, queued.flatMap((r) => (r.actorId !== null && (r.reason === "feedback" || r.reason === "assigned") ? [r.actorId] : []))); | |
| 67 | const mine = queued.filter( | |
| 68 | (r) => (r.issueId !== null && fromApp.has(r.issueId)) || ((r.reason === "feedback" || r.reason === "assigned") && r.actorId !== null && spoke.has(r.actorId)), | |
| 69 | ); | |
| 70 | const since = mine.map((r) => r.createdAt).filter((t): t is string => typeof t === "string").sort()[0] ?? null; | |
| 71 | return { count: mine.length, since }; | |
| 72 | } |
src/crew/week.ts +3 −1
| @@ -70,6 +70,8 @@ export interface WeekInput { | ||
| 70 | 70 | * is a meter too coarse to read, not a crew that is behind (#10440). |
| 71 | 71 | */ |
| 72 | 72 | readonly crewRan?: boolean; |
| 73 | /** The share of the week kept for people (1 − the weekly budget knob); `WEEK.reserve` when absent. */ | |
| 74 | readonly reserve?: number; | |
| 73 | 75 | } |
| 74 | 76 | |
| 75 | 77 | export interface WeekDecision { |
| @@ -116,7 +118,7 @@ export function weekDecision(input: WeekInput): WeekDecision | null { | ||
| 116 | 118 | const hours = left / 3_600_000; |
| 117 | 119 | const capPct = Math.max(0, 100 - meter.keptPct); |
| 118 | 120 | const used = 100 - meter.freePct; |
| 119 | const wall = capPct * (1 - WEEK.reserve); | |
| 121 | const wall = capPct * (1 - (input.reserve ?? WEEK.reserve)); | |
| 120 | 122 | const targetPctPerHour = Math.max(0, capPct - used) / hours; |
| 121 | 123 | |
| 122 | 124 | // Allowed: what the crew may still spend before the reserve, over the hours left. |
src/crew/week_read.ts +20 −3
| @@ -10,7 +10,7 @@ import { measureFor } from "../core/measure.ts"; | ||
| 10 | 10 | import { getHeadroom, WINDOW_MS } from "../core/standing.ts"; |
| 11 | 11 | import { grantedAccounts, PLATFORM } from "../core/subscriptions.ts"; |
| 12 | 12 | import { burnFrom, noteReading, readingsSince } from "./week_burn.ts"; |
| 13 | import { belowCeiling, capacityCount, capacityValue, paceFloor } from "../core/capacity.ts"; | |
| 13 | import { belowCeiling, capacityCount, capacityValue, paceFloor, weekUse } from "../core/capacity.ts"; | |
| 14 | 14 | import { efficiencyWords, WEEK, weekDecision, type Efficiency, type WeekDecision, type WeekMeter } from "./week.ts"; |
| 15 | 15 | |
| 16 | 16 | /** The week as the provider's meter reads it: a meter, or null while no account's limit is known. */ |
| @@ -132,7 +132,7 @@ export function weekFor( | ||
| 132 | 132 | seen: { idleSlots: number; startable: number; gated: number } = { idleSlots: 0, startable: 0, gated: 0 }, |
| 133 | 133 | ): WeekDecision | null { |
| 134 | 134 | const week = weekOf(app, repo, now); |
| 135 | return weekDecision({ ...week, now, ceiling: repo.autonomySlots, floorSlots: paceFloor(app.db), ...seen }); | |
| 135 | return weekDecision({ ...week, now, ceiling: repo.autonomySlots, floorSlots: paceFloor(app.db), reserve: 1 - weekUse(app.db) / 100, ...seen }); | |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** The key of the accounts a repository draws its week from. */ |
| @@ -158,7 +158,7 @@ export function crewCeiling(app: App, repo: Repo): number { | ||
| 158 | 158 | */ |
| 159 | 159 | export function crewWeekFor(app: App, repo: Repo, now: Date): WeekDecision | null { |
| 160 | 160 | const week = weekOf(app, repo, now); |
| 161 | return weekDecision({ ...week, now, ceiling: crewCeiling(app, repo), floorSlots: paceFloor(app.db), idleSlots: 0, startable: 0, gated: 0 }); | |
| 161 | return weekDecision({ ...week, now, ceiling: crewCeiling(app, repo), floorSlots: paceFloor(app.db), reserve: 1 - weekUse(app.db) / 100, idleSlots: 0, startable: 0, gated: 0 }); | |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | /** A sentence saying the crew's cap is lowered, so pacing is not mistaken for it; empty at the ceiling. */ |
| @@ -240,3 +240,20 @@ export function weekBlock(app: App, now: Date = new Date()): string[] { | ||
| 240 | 240 | const words = weekWords(app, now, crew !== null); |
| 241 | 241 | return [...(crew === null ? [] : [crew]), ...(words === null ? [] : [words])]; |
| 242 | 242 | } |
| 243 | ||
| 244 | /** | |
| 245 | * The weekly-budget knob's live effect (#10461): `25% used, 73% of the time | |
| 246 | * left · on pace · lasts to the reset Sat 02:00 UTC` — the busiest repository's | |
| 247 | * meter, the same week `weekWords` states. Plain words while it is unmeasured. | |
| 248 | */ | |
| 249 | export function budgetEffect(app: App, now: Date = new Date()): string { | |
| 250 | const words = busiest(app, now, (repo, week) => { | |
| 251 | const meter = weekOf(app, repo, now).meter; | |
| 252 | if (meter === null) return null; | |
| 253 | const used = Math.round(Math.min(100, Math.max(0, 100 - meter.freePct))); | |
| 254 | const left = Math.round(Math.min(100, Math.max(0, ((meter.resetsAt.getTime() - now.getTime()) / WINDOW_MS.week) * 100))); | |
| 255 | const lasts = week.runsOutAt === null ? `lasts to ${dayTime(meter.resetsAt)}` : `runs out ${dayTime(week.runsOutAt)}`; | |
| 256 | return `${used}% used, ${left}% of the time left · ${week.status} · ${lasts}`; | |
| 257 | }); | |
| 258 | return words ?? "the week is not measured yet · the crew holds at a few slots until it is"; | |
| 259 | } |
src/http/mcp/tools/index.ts +1
| @@ -85,6 +85,7 @@ export { tool as issue_teams } from "./issue_teams.ts"; | ||
| 85 | 85 | export { tool as issues } from "./issues.ts"; |
| 86 | 86 | export { tool as iterations } from "./iterations.ts"; |
| 87 | 87 | export { tool as keep_plan } from "./keep_plan.ts"; |
| 88 | export { tool as knobs } from "./knobs.ts"; | |
| 88 | 89 | export { tool as labels } from "./labels.ts"; |
| 89 | 90 | export { tool as land_past_gate } from "./land_past_gate.ts"; |
| 90 | 91 | export { tool as lift } from "./lift.ts"; |
src/http/mcp/tools/knobs.ts added +37
| @@ -0,0 +1,37 @@ | ||
| 1 | import { setKnob } from "../../../actions/knobs.ts"; | |
| 2 | import { knobLines, readKnobs } from "../../../crew/knobs.ts"; | |
| 3 | import { scopeAllows } from "../../../core/tokens.ts"; | |
| 4 | import { good, bad, type Tool } from "../shared.ts"; | |
| 5 | ||
| 6 | export const tool: Tool = { | |
| 7 | name: "knobs", | |
| 8 | doc: "`{ knob?, value?, repo?, dryRun? }` (forge admin): the crew's five knobs in the page's words, each with its live effect: `speed` (runs at once, 1-16), `budget` (percent of the week to use, 10-100), `polish` (percent share, 0-50), `autonomy` (`off`, `ask` or `go`, with `repo` as `owner/name`), and `feedback` (always on). With `knob` and `value`, planned first (`dryRun` is true unless `false`); applying needs an admin-scope token", | |
| 9 | description: "The crew's five knobs and what each does now. With `knob` and `value`, plans.", | |
| 10 | inputSchema: { | |
| 11 | type: "object", | |
| 12 | properties: { | |
| 13 | knob: { type: "string", enum: ["speed", "budget", "polish", "autonomy"] }, | |
| 14 | value: { type: "string", maxLength: 16 }, | |
| 15 | repo: { type: "string", maxLength: 100 }, | |
| 16 | dryRun: { type: "boolean" }, | |
| 17 | }, | |
| 18 | }, | |
| 19 | writes: true, | |
| 20 | dryByDefault: true, | |
| 21 | async run(ctx, user, args) { | |
| 22 | if (user?.isAdmin !== true || user.kind === "agent") return bad("not_found", "no such tool here"); | |
| 23 | const knob = typeof args["knob"] === "string" ? args["knob"] : undefined; | |
| 24 | if (knob === undefined) { | |
| 25 | const rows = readKnobs(ctx.app); | |
| 26 | return good({ knobs: rows, says: knobLines(rows) }); | |
| 27 | } | |
| 28 | const value = typeof args["value"] === "string" ? args["value"] : ""; | |
| 29 | const repo = typeof args["repo"] === "string" ? args["repo"] : undefined; | |
| 30 | if (args["dryRun"] !== false) | |
| 31 | return good({ says: `Would set ${knob}${repo === undefined ? "" : ` for ${repo}`} to ${value}. Send dryRun: false to change it.` }); | |
| 32 | if (!scopeAllows(ctx.auth.scope ?? "ro", "admin")) return bad("forbidden", "turning a knob needs an admin-scope token"); | |
| 33 | const set = await setKnob(ctx.app, user, knob, value, repo); | |
| 34 | if (!set.ok) return bad(set.error, "That knob or value is not one the knobs take."); | |
| 35 | return good({ says: `${knob}: ${set.value.value}, from the next pump on.`, ...set.value }); | |
| 36 | }, | |
| 37 | }; |
src/http/settings_routes.ts +2
| @@ -11,6 +11,7 @@ import type { Handler } from "./context.ts"; | ||
| 11 | 11 | import type { Router } from "./router.ts"; |
| 12 | 12 | import { parentLinkStart } from "./rp.ts"; |
| 13 | 13 | import { accessPage } from "../web/pages/access.ts"; |
| 14 | import { knobSet } from "../web/pages/knobs.ts"; | |
| 14 | 15 | import { capacityPage, capacitySet } from "../web/pages/capacity.ts"; |
| 15 | 16 | import { teamPage, teamSet, teamsPage, teamsStream } from "../web/pages/teams.ts"; |
| 16 | 17 | import { telemetryPage, telemetrySet } from "../web/pages/telemetry.ts"; |
| @@ -247,6 +248,7 @@ export function settingsRoutes(router: Router<Handler>, knownPatterns: () => rea | ||
| 247 | 248 | router.add("POST", "/settings/accounts/:username/retire", retireAct); |
| 248 | 249 | router.add("POST", "/settings/accounts/:username/verify", accountAct("verify")); |
| 249 | 250 | router.add("GET", "/settings/crew", crewPage); |
| 251 | router.add("POST", "/settings/crew/knobs", knobSet); | |
| 250 | 252 | router.add("POST", "/settings/crew", agentCreate); |
| 251 | 253 | router.add("GET", "/settings/teams", teamsPage); |
| 252 | 254 | router.add("GET", "/settings/teams/stream", teamsStream); |
src/tui/screens/entries.ts +2
| @@ -21,6 +21,7 @@ import { entry as cloud } from "./cloud.ts"; | ||
| 21 | 21 | import { entry as connections } from "./connections.ts"; |
| 22 | 22 | import { entry as crewHandover } from "./crewhandover.ts"; |
| 23 | 23 | import { entry as crewSplits } from "./crewsplits.ts"; |
| 24 | import { entry as knobs } from "./knobs.ts"; | |
| 24 | 25 | import { entry as displays } from "./displays.ts"; |
| 25 | 26 | import { entry as domains } from "./domains.ts"; |
| 26 | 27 | import { entry as edgeOnly } from "./edgeonly.ts"; |
| @@ -97,6 +98,7 @@ export const ENTRIES: readonly PaletteEntry[] = [ | ||
| 97 | 98 | audit, |
| 98 | 99 | secrets, |
| 99 | 100 | crewSplits, |
| 101 | knobs, | |
| 100 | 102 | crewHandover, |
| 101 | 103 | scorecard, |
| 102 | 104 | modelGrantEntry, |
src/tui/screens/knobs.ts added +42
| @@ -0,0 +1,42 @@ | ||
| 1 | /** | |
| 2 | * The crew's five knobs in the terminal (#10461), a forge admin's: the same | |
| 3 | * rows the crew page and the MCP `knobs` tool print (`readKnobs`), read once | |
| 4 | * when the screen opens. Turning one is the crew page's or the tool's. | |
| 5 | */ | |
| 6 | ||
| 7 | import { knobLines, readKnobs } from "../../crew/knobs.ts"; | |
| 8 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 9 | import { binding } from "../app/keys.ts"; | |
| 10 | import { style } from "../term/style.ts"; | |
| 11 | import { truncate } from "../term/width.ts"; | |
| 12 | import type { Session } from "../session.ts"; | |
| 13 | import { fill, len, split } from "../ui/layout.ts"; | |
| 14 | import { ListState, drawList, type ListRow } from "./common.ts"; | |
| 15 | import type { PaletteEntry } from "./entries.ts"; | |
| 16 | ||
| 17 | export function knobsScreen(f: Session, ctx: Ctx): Screen { | |
| 18 | const state = new ListState(); | |
| 19 | const allowed = f.user?.isAdmin === true; | |
| 20 | const rows: ListRow[] = allowed ? knobLines(readKnobs(f.app)).map((title) => ({ title })) : []; | |
| 21 | return { | |
| 22 | title: "Knobs", | |
| 23 | keys: [binding(["j", "k"], "move cursor")], | |
| 24 | render(area, s) { | |
| 25 | const c = ctx.theme.c; | |
| 26 | const [head, list] = split(area, "v", [len(1), fill(1)]); | |
| 27 | const text = allowed ? "Five dials for the crew; turn them on the crew page or with the knobs tool." : "The knobs are a forge admin's."; | |
| 28 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 29 | drawList(s, list!, c, state, rows); | |
| 30 | }, | |
| 31 | key: (e) => state.key(e, rows.length), | |
| 32 | }; | |
| 33 | } | |
| 34 | ||
| 35 | export const entry: PaletteEntry = { | |
| 36 | id: "knobs", | |
| 37 | title: "Knobs", | |
| 38 | hint: "speed, weekly budget, polish, autonomy, feedback first", | |
| 39 | after: ["crew-scorecard", "usage"], | |
| 40 | offered: (session) => session.user?.isAdmin === true, | |
| 41 | run: (session, ctx) => ctx.push(knobsScreen(session, ctx)), | |
| 42 | }; |
src/web/pages/crew.ts +2
| @@ -37,6 +37,7 @@ import { | ||
| 37 | 37 | type SplitScorecard, |
| 38 | 38 | } from "../../core/scorecard.ts"; |
| 39 | 39 | import { keeperDays, type KeeperDay } from "../../flow/keeper_rate.ts"; |
| 40 | import { knobsCard } from "./knobs.ts"; | |
| 40 | 41 | import { decideChange, readReview } from "../../actions/model_review.ts"; |
| 41 | 42 | import { wastedDays, type WastedDay } from "../../core/wasted_days.ts"; |
| 42 | 43 | import { modelDays, type ModelDay } from "../../core/model_days.ts"; |
| @@ -602,6 +603,7 @@ function crewView( | ||
| 602 | 603 | error !== undefined && /username/i.test(error) ? error : undefined; |
| 603 | 604 | const handover = crewHandover(ctx.app.db); |
| 604 | 605 | return html`${crewReadiness(ctx)} |
| 606 | ${knobsCard(ctx)} | |
| 605 | 607 | ${when(spending.length > 0, () => |
| 606 | 608 | spendingCard(spending, forgeCeiling(ctx.app.config) ?? undefined), |
| 607 | 609 | )} |
src/web/pages/knobs.ts added +67
| @@ -0,0 +1,67 @@ | ||
| 1 | /** | |
| 2 | * The knobs card on the crew page (#10461): one row per knob with its name, | |
| 3 | * what it does, its value, its live effect and the control right there. A | |
| 4 | * change posts to `knobSet`, which turns it through `actions/knobs.ts` and | |
| 5 | * leaves the event the setting's own page leaves. | |
| 6 | */ | |
| 7 | ||
| 8 | import { setKnob } from "../../actions/knobs.ts"; | |
| 9 | import { readKnobs, type KnobRow } from "../../crew/knobs.ts"; | |
| 10 | import type { Ctx } from "../../http/context.ts"; | |
| 11 | import { notFoundPage, signInFirst } from "../chrome.ts"; | |
| 12 | import { field, readForm } from "../form.ts"; | |
| 13 | import { html, type Html } from "../html.ts"; | |
| 14 | import { flashTo } from "../layout.ts"; | |
| 15 | import { btn } from "../ui.ts"; | |
| 16 | ||
| 17 | function control(r: KnobRow): Html { | |
| 18 | if (r.control === "fixed") return html`<span class="badge">${r.value}</span>`; | |
| 19 | const label = `${r.name}${r.repo === undefined ? "" : ` for ${r.repo}`}`; | |
| 20 | const input = | |
| 21 | r.control === "words" | |
| 22 | ? html`<select class="input" name="value" aria-label="${label}">${(r.words ?? []).map( | |
| 23 | (w) => html`<option value="${w}" ${w === r.value ? "selected" : ""}>${w}</option>`, | |
| 24 | )}</select>` | |
| 25 | : html`<input class="input mono" type="number" name="value" value="${r.value}" min="${r.min ?? 0}" max="${r.max ?? 100}" aria-label="${label}" style="width:5rem">${r.control === "percent" ? html` %` : html``}`; | |
| 26 | return html`<form method="post" action="/settings/crew/knobs" class="row-actions"> | |
| 27 | <input type="hidden" name="knob" value="${r.id}"> | |
| 28 | <input type="hidden" name="repo" value="${r.repo ?? ""}"> | |
| 29 | ${input} ${btn("Set", { variant: "primary" })} | |
| 30 | </form>`; | |
| 31 | } | |
| 32 | ||
| 33 | /** The card: five knobs, the autonomy one per repository. */ | |
| 34 | export function knobsCard(ctx: Ctx): Html { | |
| 35 | const rows = readKnobs(ctx.app); | |
| 36 | return html`<section class="settings-block" id="knobs"> | |
| 37 | <h2>Knobs</h2> | |
| 38 | <p class="muted">Five dials for the crew. A change applies on the next pump.</p> | |
| 39 | ${rows.map( | |
| 40 | (r) => html`<div class="knob" data-knob="${r.id}"> | |
| 41 | <div><strong>${r.name}</strong>${r.repo === undefined ? html`` : html` <span class="mono">${r.repo}</span>`} | |
| 42 | <div class="muted small">${r.does}</div> | |
| 43 | <div class="small">${r.effect}</div> | |
| 44 | ${r.derived === undefined ? html`` : html`<div class="muted small">${r.derived}</div>`}</div> | |
| 45 | ${control(r)} | |
| 46 | </div>`, | |
| 47 | )} | |
| 48 | </section>`; | |
| 49 | } | |
| 50 | ||
| 51 | // POST /settings/crew/knobs | |
| 52 | export async function knobSet(ctx: Ctx): Promise<Response> { | |
| 53 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 54 | if (!ctx.auth.user.isAdmin) return notFoundPage(ctx); | |
| 55 | const form = await readForm(ctx, "admin"); | |
| 56 | if (form instanceof Response) return form; | |
| 57 | const knob = field(form, "knob"); | |
| 58 | const set = await setKnob(ctx.app, ctx.auth.user, knob, field(form, "value"), field(form, "repo")); | |
| 59 | if (!set.ok && set.error === "not_found") return notFoundPage(ctx); | |
| 60 | return flashTo( | |
| 61 | ctx, | |
| 62 | "/settings/crew", | |
| 63 | set.ok | |
| 64 | ? { message: `${knob}: ${set.value.value}, from the next pump on.` } | |
| 65 | : { tone: "danger", message: "That value is outside what this knob takes." }, | |
| 66 | ); | |
| 67 | } |
test/child-quota.test.ts +1 −1
| @@ -101,7 +101,7 @@ describe("child quota", () => { | ||
| 101 | 101 | |
| 102 | 102 | test("room for small but not large is refused only for large", async () => { |
| 103 | 103 | clear(); |
| 104 | process.env["FORGE_CHILD_MEMORY_MIB"] = "2000"; | |
| 104 | process.env["FORGE_CHILD_MEMORY_MIB"] = "4200"; | |
| 105 | 105 | expect(quotaRefusal(dir, "small")).toBeUndefined(); |
| 106 | 106 | expect(quotaRefusal(dir, "large")).toContain("memory limit"); |
| 107 | 107 | }); |
test/conversation-confirm.test.ts +1 −1
| @@ -123,7 +123,7 @@ describe("the one list of what asks first", () => { | ||
| 123 | 123 | "access", "account_verify", "app_members", "app_notes", "asks_act", "asks_decide", "asks_proposal", "asks_undecide", "atlas", |
| 124 | 124 | "assign", "attach_look", "bet", "board_pins", "board_write", "branches", "capacity", "valves", "cloud_accounts", "close", "confirm_wait", |
| 125 | 125 | "crew", "decisions_by", "detach", "economics", "edit", "email_verify", "feed_chips", "feed_dismiss", "feed_like", "flag_set", "flag_winner", |
| 126 | "follow_mode", "fork", "front_page", "hold", "issue_teams", "keep_plan", "labels", "lift", "limb", "maintenance", "make_it_yours", "map", "merge", "new_app", | |
| 126 | "follow_mode", "fork", "front_page", "hold", "issue_teams", "keep_plan", "knobs", "labels", "lift", "limb", "maintenance", "make_it_yours", "map", "merge", "new_app", | |
| 127 | 127 | "next_up", "week", "notify", "open_pull", "org_create", "origin_apps", "pins", "plan", "profile", "propose_to_parent_forge", "repo_labels", "reports", |
| 128 | 128 | "restore_drill", "review", "say", "signup", "snapshot_now", "spend", "steer", "sync", "take_back", "take_updates", |
| 129 | 129 | "teams", "tell", "threads", "trail", "upstream", "variant", "wait_for", "watch", "whoami_can", "write_file", |
test/knobs-page.test.ts added +54
| @@ -0,0 +1,54 @@ | ||
| 1 | /** The knobs card on the crew page (#10461): five rows, edited in place by a same-origin POST. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { capacityValue } from "../src/core/capacity.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 10 | ||
| 11 | let dataDir = ""; | |
| 12 | let app: App, servers: Servers, bearer = ""; | |
| 13 | ||
| 14 | beforeAll(async () => { | |
| 15 | dataDir = await mkdtemp(join(tmpdir(), "forge-knobs-page-")); | |
| 16 | app = createApp({ dataDir, port: 0 }); | |
| 17 | servers = await serve(app); | |
| 18 | const admin = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 19 | if (!admin.ok) throw new Error(admin.error); | |
| 20 | const minted = await fetch(`${servers.url}/api/v1/tokens`, { | |
| 21 | method: "POST", | |
| 22 | headers: { "content-type": "application/json" }, | |
| 23 | body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }), | |
| 24 | }); | |
| 25 | bearer = ((await minted.json()) as { token: string }).token; | |
| 26 | }); | |
| 27 | afterAll(async () => { | |
| 28 | servers.stop(); | |
| 29 | app.close(); | |
| 30 | await rm(dataDir, { recursive: true, force: true }); | |
| 31 | }); | |
| 32 | ||
| 33 | const post = (origin: string, body: Record<string, string>) => | |
| 34 | fetch(`${servers.url}/settings/crew/knobs`, { | |
| 35 | method: "POST", | |
| 36 | redirect: "manual", | |
| 37 | headers: { authorization: `Bearer ${bearer}`, "content-type": "application/x-www-form-urlencoded", origin }, | |
| 38 | body: new URLSearchParams(body), | |
| 39 | }); | |
| 40 | ||
| 41 | test("the crew page shows the knobs, and a same-origin post turns one", async () => { | |
| 42 | const res = await fetch(`${servers.url}/settings/crew`, { headers: { authorization: `Bearer ${bearer}` } }); | |
| 43 | const body = await res.text(); | |
| 44 | for (const id of ["speed", "budget", "polish", "feedback"]) expect(body).toContain(`data-knob="${id}"`); | |
| 45 | expect(body).toContain("nothing from a person waiting"); | |
| 46 | const done = await post(servers.url, { knob: "speed", value: "3", repo: "" }); | |
| 47 | expect(done.status).toBe(303); | |
| 48 | expect(capacityValue(app.db, "crew.concurrency")).toBe("3"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("another origin changes nothing", async () => { | |
| 52 | await post("https://evil.example", { knob: "speed", value: "5", repo: "" }); | |
| 53 | expect(capacityValue(app.db, "crew.concurrency")).toBe("3"); | |
| 54 | }); |
test/knobs.test.ts added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | /** | |
| 2 | * The five knobs (#10461): read in plain words, turned in place, and each | |
| 3 | * turn read by the next pump; the dials they drive are derived, the two | |
| 4 | * dials they fold in are gone. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { setKnob } from "../src/actions/knobs.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { CAPACITY_KEYS, capacityCount, paceFloor, queueDepth, setCapacity, smallShare, weekUse } from "../src/core/capacity.ts"; | |
| 15 | import { listEvents } from "../src/core/events.ts"; | |
| 16 | import { getRepoById } from "../src/core/repos.ts"; | |
| 17 | import { createUser, type User } from "../src/core/users.ts"; | |
| 18 | import { knobLines, readKnobs } from "../src/crew/knobs.ts"; | |
| 19 | import { weekDecision } from "../src/crew/week.ts"; | |
| 20 | import { tool } from "../src/http/mcp/tools/knobs.ts"; | |
| 21 | import { quietLogs } from "./helpers/globals.ts"; | |
| 22 | ||
| 23 | quietLogs(); | |
| 24 | ||
| 25 | let dir: string; | |
| 26 | let app: App; | |
| 27 | let admin: User; | |
| 28 | let bob: User; | |
| 29 | let repoId: string; | |
| 30 | ||
| 31 | beforeAll(async () => { | |
| 32 | dir = await mkdtemp(join(tmpdir(), "forge-knobs-")); | |
| 33 | app = createApp({ dataDir: dir, port: 0 }); | |
| 34 | const mk = async (username: string, first = false) => { | |
| 35 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first }); | |
| 36 | if (!r.ok) throw new Error(r.error); | |
| 37 | return r.value; | |
| 38 | }; | |
| 39 | admin = await mk("carol", true); | |
| 40 | bob = await mk("bob"); | |
| 41 | const made = await createRepo(app, admin, { name: "site", visibility: "private" }); | |
| 42 | if (!made.ok) throw new Error(String(made.error)); | |
| 43 | repoId = made.value.id; | |
| 44 | }, 30_000); | |
| 45 | afterAll(async () => { | |
| 46 | app.close(); | |
| 47 | await rm(dir, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | test("the panel has Speed, Weekly budget, Polish share, Autonomy per repository and Feedback first, each with an effect", () => { | |
| 51 | const rows = readKnobs(app); | |
| 52 | expect(rows.map((r) => r.id)).toEqual(["speed", "budget", "polish", "autonomy", "feedback"]); | |
| 53 | for (const r of rows) { | |
| 54 | expect(r.does.length).toBeGreaterThan(0); | |
| 55 | expect(r.effect.length).toBeGreaterThan(0); | |
| 56 | } | |
| 57 | expect(rows[0]!.effect).toMatch(/\d+ running · \d+ queued/); | |
| 58 | expect(rows[4]!.effect).toBe("nothing from a person waiting"); | |
| 59 | expect(knobLines(rows)).toHaveLength(5); | |
| 60 | }); | |
| 61 | ||
| 62 | test("Speed 4 → 2 is read by the next pump, and the dials it drives follow", async () => { | |
| 63 | setCapacity(app.db, "crew.concurrency", "4", admin.id); | |
| 64 | expect([paceFloor(app.db), queueDepth(app.db)]).toEqual([2, 2]); | |
| 65 | expect((await setKnob(app, admin, "speed", "2")).ok).toBe(true); | |
| 66 | // The pump asks exactly this, with the number it booted on as the fallback. | |
| 67 | expect(capacityCount(app.db, "crew.concurrency", 4)).toBe(2); | |
| 68 | expect([paceFloor(app.db), queueDepth(app.db)]).toEqual([1, 1]); | |
| 69 | expect(readKnobs(app)[0]!.value).toBe("2"); | |
| 70 | expect((await setKnob(app, admin, "speed", "99")).ok).toBe(false); | |
| 71 | expect(listEvents(app.db, {}).some((e) => e.kind === "capacity.changed")).toBe(true); | |
| 72 | }); | |
| 73 | ||
| 74 | test("the folded and derived dials are gone from capacity; smallShare is fixed", () => { | |
| 75 | for (const key of ["slots.smallShare", "polish.capMinutes", "pace.floor", "queue.depth"]) expect(CAPACITY_KEYS as string[]).not.toContain(key); | |
| 76 | expect(smallShare(app.db)).toBe(30); | |
| 77 | }); | |
| 78 | ||
| 79 | test("Weekly budget sets the reserve the pacer holds back", () => { | |
| 80 | expect(weekUse(app.db)).toBe(90); | |
| 81 | const week = (reserve: number) => | |
| 82 | weekDecision({ | |
| 83 | meter: { freePct: 20, keptPct: 0, burnPctPerHour: 1, resetsAt: new Date(Date.now() + 3 * 86_400_000) }, | |
| 84 | now: new Date(), | |
| 85 | ceiling: 4, | |
| 86 | floorSlots: 1, | |
| 87 | idleSlots: 0, | |
| 88 | startable: 0, | |
| 89 | gated: 0, | |
| 90 | reserve, | |
| 91 | })!; | |
| 92 | expect(week(0.1).slots).toBeGreaterThan(0); | |
| 93 | expect(week(0.9).slots).toBe(0); | |
| 94 | }); | |
| 95 | ||
| 96 | test("Autonomy turns ask and go into the repository's settings; Polish share sets its share", async () => { | |
| 97 | expect((await setKnob(app, admin, "autonomy", "ask", "carol/site")).ok).toBe(true); | |
| 98 | let repo = getRepoById(app.db, repoId)!; | |
| 99 | expect([repo.autonomy, repo.plansNeedNod, repo.decisionsBy]).toEqual(["autonomous", true, "me"]); | |
| 100 | expect((await setKnob(app, admin, "autonomy", "go", "carol/site")).ok).toBe(true); | |
| 101 | repo = getRepoById(app.db, repoId)!; | |
| 102 | expect([repo.plansNeedNod, repo.decisionsBy]).toEqual([false, "decider"]); | |
| 103 | expect(readKnobs(app).find((r) => r.id === "autonomy" && r.repo === "carol/site")!.value).toBe("go"); | |
| 104 | expect((await setKnob(app, admin, "polish", "20")).ok).toBe(true); | |
| 105 | expect(getRepoById(app.db, repoId)!.polishShare).toBe(20); | |
| 106 | expect((await setKnob(app, admin, "polish", "70")).ok).toBe(false); | |
| 107 | expect((await setKnob(app, admin, "autonomy", "maybe", "carol/site")).ok).toBe(false); | |
| 108 | expect(listEvents(app.db, {}).some((e) => e.kind === "repo.settings_updated")).toBe(true); | |
| 109 | expect((await setKnob(app, admin, "autonomy", "off", "carol/site")).ok).toBe(true); | |
| 110 | }); | |
| 111 | ||
| 112 | test("only a forge admin turns a knob, and the MCP tool says the same words", async () => { | |
| 113 | expect((await setKnob(app, bob, "speed", "3")).ok).toBe(false); | |
| 114 | const ctx = { app, auth: { scope: "admin" } } as never; | |
| 115 | const read = (await tool.run(ctx, admin, {})) as { content?: unknown }; | |
| 116 | expect(JSON.stringify(read)).toContain("Weekly budget"); | |
| 117 | const dry = await tool.run(ctx, admin, { knob: "speed", value: "3" }); | |
| 118 | expect(JSON.stringify(dry)).toContain("Would set speed"); | |
| 119 | expect(JSON.stringify(await tool.run(ctx, bob, {}))).toContain("not_found"); | |
| 120 | }); |
test/mcp.test.ts +1
| @@ -247,6 +247,7 @@ describe("the forge over MCP", () => { | ||
| 247 | 247 | "issues", |
| 248 | 248 | "iterations", |
| 249 | 249 | "keep_plan", |
| 250 | "knobs", | |
| 250 | 251 | "orgs", |
| 251 | 252 | "now", |
| 252 | 253 | "open_pull", |
test/pace-floor.test.ts +1 −1
| @@ -23,7 +23,7 @@ let standIn: StandInCrew; | ||
| 23 | 23 | |
| 24 | 24 | beforeAll(async () => { |
| 25 | 25 | standIn = await startStandInCrew({ tickMs: 50 }); |
| 26 | setCapacity(standIn.app.db, "crew.concurrency", "10", standIn.alice.id); | |
| 26 | setCapacity(standIn.app.db, "crew.concurrency", "5", standIn.alice.id); | |
| 27 | 27 | }, 30_000); |
| 28 | 28 | afterAll(() => stopStandInCrew(standIn)); |
| 29 | 29 |
test/palette-roster.test.ts +2
| @@ -118,6 +118,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 118 | 118 | ["trusted-hosts", "Trusted hosts", "internal hosts an origin may reach"], |
| 119 | 119 | ["usage", "Usage", "the forge's spend and headroom; an admin also sees every route"], |
| 120 | 120 | ["crew-scorecard", "Crew scorecard", "agent runs per model and per prompt version"], |
| 121 | ["knobs", "Knobs", "speed, weekly budget, polish, autonomy, feedback first"], | |
| 121 | 122 | ["crew-handover", "Crew hand-overs", "issues parked twice, and the last restore drill"], |
| 122 | 123 | ["scorecard", "Crew scorecard per agent", "each agent's pulls landed, review rounds, red checks, reverts, time to land"], |
| 123 | 124 | ["forge-spend", "Forge spend", "today and the last 24 hours, headroom per subscription"], |
| @@ -207,6 +208,7 @@ test("a site-admin agent is offered the roster without the person-only entries", | ||
| 207 | 208 | ["secrets", "Secrets", "sealed secrets by name; set or remove one"], |
| 208 | 209 | ["usage", "Usage", "the forge's spend and headroom; an admin also sees every route"], |
| 209 | 210 | ["crew-scorecard", "Crew scorecard", "agent runs per model and per prompt version"], |
| 211 | ["knobs", "Knobs", "speed, weekly budget, polish, autonomy, feedback first"], | |
| 210 | 212 | ["crew-handover", "Crew hand-overs", "issues parked twice, and the last restore drill"], |
| 211 | 213 | ["scorecard", "Crew scorecard per agent", "each agent's pulls landed, review rounds, red checks, reverts, time to land"], |
| 212 | 214 | ["forge-spend", "Forge spend", "today and the last 24 hours, headroom per subscription"], |
test/tui-capacity.test.ts +4 −4
| @@ -81,7 +81,7 @@ describe("tui capacity", () => { | ||
| 81 | 81 | await h.press(); |
| 82 | 82 | expect(h.text()).toContain("Crew runs at once"); |
| 83 | 83 | expect(h.text()).toContain("takes 1–16"); |
| 84 | expect(h.text()).toContain("Merge queue candidates at once"); | |
| 84 | expect(h.text()).toContain("Share of the week to use"); | |
| 85 | 85 | expect(h.text()).not.toContain("read-only"); |
| 86 | 86 | h.stop(); |
| 87 | 87 | }); |
| @@ -134,14 +134,14 @@ describe("tui capacity", () => { | ||
| 134 | 134 | h.stop(); |
| 135 | 135 | }); |
| 136 | 136 | |
| 137 | test("queue.depth, which the page sets, changes after a confirm", async () => { | |
| 137 | test("queue.batch, which the page sets, changes after a confirm", async () => { | |
| 138 | 138 | const h = boot(carol); |
| 139 | for (let i = 0; i < CAPACITY_KEYS.indexOf("queue.depth"); i++) await h.press("j"); | |
| 139 | for (let i = 0; i < CAPACITY_KEYS.indexOf("queue.batch"); i++) await h.press("j"); | |
| 140 | 140 | await h.press("e"); |
| 141 | 141 | for (let i = 0; i < 4; i++) await h.press("backspace"); |
| 142 | 142 | await h.press("4", "enter"); |
| 143 | 143 | await h.press("left", "enter"); |
| 144 | expect(capacityValue(app.db, "queue.depth")).toBe("4"); | |
| 144 | expect(capacityValue(app.db, "queue.batch")).toBe("4"); | |
| 145 | 145 | h.stop(); |
| 146 | 146 | }); |
| 147 | 147 |
test/week-pump.test.ts +1 −1
| @@ -78,7 +78,7 @@ test("builds fill the pace slots, yet a review or assigned run still starts; a t | ||
| 78 | 78 | if (!sub.ok) throw new Error(sub.error); |
| 79 | 79 | setHeadroom(app.db, sub.value.id, 0); |
| 80 | 80 | app.db.query("INSERT INTO subscription_grant (scope_id, agent_id, subscription_id) VALUES (?1, '', ?2)").run(made.value.id, sub.value.id); |
| 81 | setCapacity(app.db, "crew.concurrency", "10", alice.id); | |
| 81 | setCapacity(app.db, "crew.concurrency", "5", alice.id); | |
| 82 | 82 | const now = Date.now(); |
| 83 | 83 | noteMeasure(app.db, sub.value.id, { window: "week", usedPct: 70, resetsAt: new Date(now + 3.5 * DAY).toISOString(), observedAt: new Date(now).toISOString() }); |
| 84 | 84 | // 30% free now, 36% an hour ago: 6 points an hour, far over what is allowed, so the pace is the floor of 2 slots. |
test/week-wiring.test.ts +4 −2
| @@ -19,7 +19,7 @@ import { recordUsage } from "../src/core/usage.ts"; | ||
| 19 | 19 | import { createUser, type User } from "../src/core/users.ts"; |
| 20 | 20 | import { driveRepo, repoPace } from "../src/crew/autonomy.ts"; |
| 21 | 21 | import { changeCapacity } from "../src/actions/capacity.ts"; |
| 22 | import { capacityDefault } from "../src/core/capacity.ts"; | |
| 22 | import { bootDefault, noteBooted } from "../src/core/capacity.ts"; | |
| 23 | 23 | import { crewLineWords, forgetWeek, weekWords } from "../src/crew/week_read.ts"; |
| 24 | 24 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 25 | 25 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| @@ -117,6 +117,8 @@ afterAll(async () => { | ||
| 117 | 117 | }); |
| 118 | 118 | |
| 119 | 119 | beforeEach(() => forgetWeek()); |
| 120 | // The floor is half of Speed (#10461): tests state the Speed they expect, not the machine's. | |
| 121 | beforeAll(() => noteBooted(app.db, "crew.concurrency", "4")); | |
| 120 | 122 | // A failed expectation must not leave the lowered crew concurrency for later tests. |
| 121 | 123 | afterEach(() => { |
| 122 | 124 | changeCapacity(app, alice, "crew.concurrency", null); |
| @@ -175,7 +177,7 @@ test("home and the terminal print the same sentence, the terminal for an admin", | ||
| 175 | 177 | |
| 176 | 178 | test("the pace sentence says when crew concurrency is below its ceiling, and not at it", () => { |
| 177 | 179 | week(20); |
| 178 | const ceiling = Number(capacityDefault("crew.concurrency")); | |
| 180 | const ceiling = Number(bootDefault(app.db, "crew.concurrency")); | |
| 179 | 181 | expect(changeCapacity(app, alice, "crew.concurrency", String(ceiling)).ok).toBe(true); |
| 180 | 182 | expect(weekWords(app, NOW)!).not.toContain("below its ceiling"); |
| 181 | 183 | expect(changeCapacity(app, alice, "crew.concurrency", "1").ok).toBe(true); |
new above
offline