Public: anyone can read this; the crew can change it.
A standing rule per repository: decisions here go to me, the decider, or the decider telling me the unsure ones #1736
scout/forge:scout/1605-a-standing-rule-per-repository-decisions → main 6 commits fork 2 threads · 2 unresolved
18 files
changed +729
−10
README.md +18
| @@ -267,6 +267,24 @@ configured leaves it with you. Undo for a day (`/api/v1/asks/decide/<id>/undo`, | ||
| 267 | 267 | `asks_undecide`, `U`). Each tap is a paid call, so one card is handed over |
| 268 | 268 | once in ten minutes. |
| 269 | 269 | |
| 270 | **The standing rule** says who answers a repository's hand-overs without a | |
| 271 | tap, three ways: `me` (the default: they wait for you), `decider` (the sweep | |
| 272 | asks the decider about each parked card and its word stands, whatever its | |
| 273 | confidence), or `decider_tell` (the decider's sure set-asides, 70% or more, | |
| 274 | take the card off your board; the unsure ones stay with you). The tap on the | |
| 275 | card follows the same rule. A shelved repository, one whose autonomy is `off`, | |
| 276 | has its parked cards closed by rule with no model call whenever the rule is | |
| 277 | not `me`, so turning the crew off on a ruled repository closes what it had | |
| 278 | parked. The sweep spends at most five model calls a tick and never re-asks a | |
| 279 | card already answered or undone. Only a repository admin who is a person sets | |
| 280 | it, never an agent. Set it in the repository's settings (Autonomy → Decisions | |
| 281 | here); over the API with `PATCH /api/v1/repos/<owner>/<name>` | |
| 282 | `{"decisionsBy": "me" | "decider" | "decider_tell"}` (admin token; it is in the | |
| 283 | repository JSON too); over MCP with `decisions_by` (`repo`; without `by` it | |
| 284 | says how the rule is set, with `by` it plans unless `dryRun: false`); in the | |
| 285 | terminal with `R` in the repository's workspace, which cycles me → decider → | |
| 286 | decider_tell. | |
| 287 | ||
| 270 | 288 | ## On your machine |
| 271 | 289 | |
| 272 | 290 | The forge holds the truth; `forge` keeps a copy of the parts you work on, the |
docs/MCP.md +1
| @@ -38,6 +38,7 @@ for a tunnel and a forge that sleeps. | ||
| 38 | 38 | | `health` | how this forge is: the version and commit it runs, draining or not, the rollout and its incidents | | |
| 39 | 39 | | `audit` | who can do what, per app, read from its contract.json in git: yes, own or no per door, and whether outsiders may direct its crew (`crewOutsiders`) | | |
| 40 | 40 | | `outsiders` | `{ repo, on?, dryRun? }`: whether people who cannot write may direct the crew; with `on`, planned first (`dryRun` is true unless `false`); set only by the app's admin with an admin token, never an agent | yes | |
| 41 | | `decisions_by` | `{ repo, by?, dryRun? }`: the standing rule for a repository's hand-overs (`me`, `decider`, `decider_tell`); with `by`, planned first (`dryRun` is true unless `false`); set only by the app's admin with an admin token, never an agent | yes | | |
| 41 | 42 | | `probe` | knock on a running app's doors as nobody, a user and an admin; the ones that disobey its contract | | |
| 42 | 43 | | `following` | where a fork stands against what it follows, and what the crew is doing about it | | |
| 43 | 44 | | `spawn` | `{ name, apps?, crew?, dryRun? }`: a child forge, planned first (`dryRun` is true unless `false`); an admin's, and an `ro` token may plan but not make | yes | |
scripts/gate.ts +2
| @@ -165,6 +165,8 @@ export const GATE: readonly string[] = [ | ||
| 165 | 165 | "test/review-wait.test.ts", |
| 166 | 166 | "test/asks-bulk.test.ts", |
| 167 | 167 | "test/hand-decider.test.ts", |
| 168 | "test/decisions-by-doors.test.ts", | |
| 169 | "test/standing-rule.test.ts", | |
| 168 | 170 | "test/asks-expiry.test.ts", |
| 169 | 171 | "test/asks-rank.test.ts", |
| 170 | 172 | "test/plan-pillars.test.ts", |
src/actions/asks_decide.ts +1 −1
| @@ -84,7 +84,7 @@ export async function decideAsk( | ||
| 84 | 84 | ); |
| 85 | 85 | handing.add(issue.id); |
| 86 | 86 | try { |
| 87 | const { decision } = await letDeciderHandle(app, actor, repo, issue); | |
| 87 | const { decision } = await letDeciderHandle(app, actor, repo, issue, repo.decisionsBy); | |
| 88 | 88 | return ok(describe(ask, decision)); |
| 89 | 89 | } finally { |
| 90 | 90 | handing.delete(issue.id); |
src/actions/repos.ts +38 −1
| @@ -23,6 +23,7 @@ import { | ||
| 23 | 23 | getSealedFork, |
| 24 | 24 | updateRepoSettings as writeSettings, |
| 25 | 25 | type CreateRepoError, |
| 26 | type DecisionsBy, | |
| 26 | 27 | type ForkRepoError, |
| 27 | 28 | type Repo, |
| 28 | 29 | type RepoSettings, |
| @@ -246,7 +247,11 @@ export async function updateRepoSettings( | ||
| 246 | 247 | if (!admin.ok) return admin; |
| 247 | 248 | // Who the crew obeys is a person's call: an agent never widens it, even |
| 248 | 249 | // one on a team that administers the repository (`crew/direct.ts`). |
| 249 | if (settings.crewOutsiders !== undefined && admin.value.kind === "agent") | |
| 250 | if ( | |
| 251 | (settings.crewOutsiders !== undefined || | |
| 252 | settings.decisionsBy !== undefined) && | |
| 253 | admin.value.kind === "agent" | |
| 254 | ) | |
| 250 | 255 | return err("not_found"); |
| 251 | 256 | // The work on a sealed report stays private until it merges into its |
| 252 | 257 | // base, whoever owns it (`core/sealed.ts`). |
| @@ -270,6 +275,7 @@ export async function updateRepoSettings( | ||
| 270 | 275 | "requireReview", |
| 271 | 276 | "requireChecks", |
| 272 | 277 | "crewOutsiders", |
| 278 | "decisionsBy", | |
| 273 | 279 | ] as const |
| 274 | 280 | ).filter((k) => settings[k] !== undefined && settings[k] !== repo[k]); |
| 275 | 281 | app.bus.append({ |
| @@ -321,6 +327,37 @@ export async function setCrewOutsiders( | ||
| 321 | 327 | return ok({ on: updated.value.crewOutsiders, was: repo.crewOutsiders, dryRun }); |
| 322 | 328 | } |
| 323 | 329 | |
| 330 | export interface DecisionsAnswer { | |
| 331 | /** Who answers the repository's decisions, after this call. */ | |
| 332 | readonly by: DecisionsBy; | |
| 333 | /** Who did before it. */ | |
| 334 | readonly was: DecisionsBy; | |
| 335 | /** Nothing changed: this was the plan. */ | |
| 336 | readonly dryRun: boolean; | |
| 337 | } | |
| 338 | ||
| 339 | /** | |
| 340 | * The standing rule for the decisions a repository asks of its owner, for | |
| 341 | * the MCP and the terminal — the web form's choice. A repository admin's, | |
| 342 | * never an agent's; a dry run answers what would change and changes nothing. | |
| 343 | */ | |
| 344 | export async function setDecisionsBy( | |
| 345 | app: App, | |
| 346 | actor: User | undefined, | |
| 347 | repo: Repo, | |
| 348 | by: DecisionsBy, | |
| 349 | dryRun: boolean, | |
| 350 | ): Promise<Result<DecisionsAnswer, "not_found">> { | |
| 351 | const admin = requireRepoAdmin(app, actor, repo); | |
| 352 | if (!admin.ok || admin.value.kind === "agent") return err("not_found"); | |
| 353 | if (dryRun) return ok({ by, was: repo.decisionsBy, dryRun }); | |
| 354 | const updated = await updateRepoSettings(app, actor, repo, { | |
| 355 | decisionsBy: by, | |
| 356 | }); | |
| 357 | if (!updated.ok) return err("not_found"); | |
| 358 | return ok({ by: updated.value.decisionsBy, was: repo.decisionsBy, dryRun }); | |
| 359 | } | |
| 360 | ||
| 324 | 361 | export async function deleteRepo( |
| 325 | 362 | app: App, |
| 326 | 363 | actor: User | undefined, |
src/core/repos.ts +13 −1
| @@ -116,6 +116,8 @@ export interface Repo { | ||
| 116 | 116 | * default; on a public repository it hands strangers the owner's budget. |
| 117 | 117 | */ |
| 118 | 118 | readonly crewOutsiders: boolean; |
| 119 | /** The standing rule for decisions here: `me`, `decider`, or `decider_tell`. */ | |
| 120 | readonly decisionsBy: DecisionsBy; | |
| 119 | 121 | /** The issue the planner is given, when the map has run dry. */ |
| 120 | 122 | readonly directionIssueId: string | null; |
| 121 | 123 | /** The repo this was forked from, or null. */ |
| @@ -160,6 +162,7 @@ interface RepoRow { | ||
| 160 | 162 | autonomy_slots: number; |
| 161 | 163 | autonomy_by: string | null; |
| 162 | 164 | crew_outsiders: number; |
| 165 | decisions_by: string; | |
| 163 | 166 | direction_issue_id: string | null; |
| 164 | 167 | fork_parent_id: string | null; |
| 165 | 168 | sealed_for: string | null; |
| @@ -197,6 +200,7 @@ function toRepo(row: RepoRow): Repo { | ||
| 197 | 200 | autonomySlots: row.autonomy_slots, |
| 198 | 201 | autonomyBy: row.autonomy_by, |
| 199 | 202 | crewOutsiders: row.crew_outsiders === 1, |
| 203 | decisionsBy: isDecisionsBy(row.decisions_by) ? row.decisions_by : "me", | |
| 200 | 204 | directionIssueId: row.direction_issue_id, |
| 201 | 205 | forkParentId: row.fork_parent_id, |
| 202 | 206 | sealedFor: row.sealed_for, |
| @@ -1007,6 +1011,12 @@ export function canAdmin( | ||
| 1007 | 1011 | return accessAllows(orgAccessFor(db, repo, user), "admin"); |
| 1008 | 1012 | } |
| 1009 | 1013 | |
| 1014 | /** Who answers the decisions a repository asks of its owner. */ | |
| 1015 | export const DECISIONS_BY = ["me", "decider", "decider_tell"] as const; | |
| 1016 | export type DecisionsBy = (typeof DECISIONS_BY)[number]; | |
| 1017 | export const isDecisionsBy = (v: unknown): v is DecisionsBy => | |
| 1018 | typeof v === "string" && (DECISIONS_BY as readonly string[]).includes(v); | |
| 1019 | ||
| 1010 | 1020 | export interface RepoSettings { |
| 1011 | 1021 | visibility?: "public" | "private"; |
| 1012 | 1022 | description?: string; |
| @@ -1025,6 +1035,7 @@ export interface RepoSettings { | ||
| 1025 | 1035 | autonomyBy?: string | null; |
| 1026 | 1036 | /** Whether someone who cannot write here may start or steer a run. */ |
| 1027 | 1037 | crewOutsiders?: boolean; |
| 1038 | decisionsBy?: DecisionsBy; | |
| 1028 | 1039 | } |
| 1029 | 1040 | |
| 1030 | 1041 | /** Update the mutable repo fields, leaving unspecified ones untouched. */ |
| @@ -1037,7 +1048,7 @@ export function updateRepoSettings( | ||
| 1037 | 1048 | `UPDATE repo SET visibility = ?1, description = ?2, protect_default = ?3, |
| 1038 | 1049 | default_branch = ?4, require_review = ?5, require_checks = ?7, |
| 1039 | 1050 | autonomy = ?8, autonomy_direction = ?9, autonomy_ceiling_usd = ?10, |
| 1040 | autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13 | |
| 1051 | autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13, decisions_by = ?14 | |
| 1041 | 1052 | WHERE id = ?6`, |
| 1042 | 1053 | ).run( |
| 1043 | 1054 | settings.visibility ?? repo.visibility, |
| @@ -1053,6 +1064,7 @@ export function updateRepoSettings( | ||
| 1053 | 1064 | settings.autonomySlots ?? repo.autonomySlots, |
| 1054 | 1065 | settings.autonomyBy === undefined ? repo.autonomyBy : settings.autonomyBy, |
| 1055 | 1066 | (settings.crewOutsiders ?? repo.crewOutsiders) ? 1 : 0, |
| 1067 | settings.decisionsBy ?? repo.decisionsBy, | |
| 1056 | 1068 | ); |
| 1057 | 1069 | return getRepoById(db, repo.id)!; |
| 1058 | 1070 | } |
src/core/schema.ts +6
| @@ -1735,6 +1735,12 @@ export const migrations: readonly string[] = [ | ||
| 1735 | 1735 | placed_at TEXT NOT NULL |
| 1736 | 1736 | ); |
| 1737 | 1737 | `, |
| 1738 | // 85 — a standing rule per repository (#1605): who answers the decisions | |
| 1739 | // it asks of its owner. \`me\` is what every repository did before. | |
| 1740 | ` | |
| 1741 | ALTER TABLE repo ADD COLUMN decisions_by TEXT NOT NULL DEFAULT 'me' | |
| 1742 | CHECK (decisions_by IN ('me', 'decider', 'decider_tell')); | |
| 1743 | `, | |
| 1738 | 1744 | ]; |
| 1739 | 1745 | |
| 1740 | 1746 | /** |
src/crew/handover.ts +45 −3
| @@ -28,7 +28,7 @@ import { | ||
| 28 | 28 | } from "../core/interactions.ts"; |
| 29 | 29 | import type { Issue } from "../core/issues.ts"; |
| 30 | 30 | import { isPinned } from "../core/pins.ts"; |
| 31 | import type { Repo } from "../core/repos.ts"; | |
| 31 | import type { DecisionsBy, Repo } from "../core/repos.ts"; | |
| 32 | 32 | import type { User } from "../core/users.ts"; |
| 33 | 33 | import { spentTodayForRepos } from "./autonomy.ts"; |
| 34 | 34 | import { certain, decideOr, type Answers, type Questions } from "./decide.ts"; |
| @@ -120,6 +120,7 @@ export async function letDeciderHandle( | ||
| 120 | 120 | owner: User, |
| 121 | 121 | repo: Repo, |
| 122 | 122 | issue: Issue, |
| 123 | mode: DecisionsBy = "decider_tell", | |
| 123 | 124 | ): Promise<Handled> { |
| 124 | 125 | const context = handContext(app, owner, repo, issue); |
| 125 | 126 | const rule = (): Answers<typeof QUESTIONS> => ({ |
| @@ -132,10 +133,14 @@ export async function letDeciderHandle( | ||
| 132 | 133 | rule, |
| 133 | 134 | ); |
| 134 | 135 | const answer = decided.answers.hand; |
| 135 | const sure = answer.choice === SET_ASIDE && answer.p >= SURE; | |
| 136 | // Ruled "decider", its word stands; otherwise only a sure one does. | |
| 137 | const sure = | |
| 138 | answer.choice === SET_ASIDE && (mode === "decider" || answer.p >= SURE); | |
| 136 | 139 | const percent = Math.round(answer.p * 100); |
| 137 | 140 | const reason = sure |
| 138 | ? `Set aside, ${percent}% sure: nothing in the direction, the budget or your past decisions here asks for it now.` | |
| 141 | ? mode === "decider" && answer.p < SURE | |
| 142 | ? `Set aside, ${percent}% sure: this repository is ruled "decider", so its word stands.` | |
| 143 | : `Set aside, ${percent}% sure: nothing in the direction, the budget or your past decisions here asks for it now.` | |
| 139 | 144 | : decided.by === "rule" |
| 140 | 145 | ? "No decider answered, so it stays with you." |
| 141 | 146 | : `Unsure (${percent}% for ${answer.choice === SET_ASIDE ? "setting it aside" : "asking you"}), so it stays with you.`; |
| @@ -206,3 +211,40 @@ export function forReader(d: Decision, reader: User | undefined): Decision { | ||
| 206 | 211 | state: { ...rest, repository: { name: repository?.name } }, |
| 207 | 212 | }; |
| 208 | 213 | } |
| 214 | ||
| 215 | /** | |
| 216 | * A hand-over on a shelved repository — its crew is off, so nothing will | |
| 217 | * pick the issue up again — closes without a model call: the rule answers, | |
| 218 | * in the ledger, with the same context and the same undo. | |
| 219 | */ | |
| 220 | export function setAsideShelved( | |
| 221 | app: App, | |
| 222 | owner: User, | |
| 223 | repo: Repo, | |
| 224 | issue: Issue, | |
| 225 | ): Handled { | |
| 226 | const context = handContext(app, owner, repo, issue); | |
| 227 | const decision = recordDecision(app.db, { | |
| 228 | kind: "hand", | |
| 229 | repoId: repo.id, | |
| 230 | issueId: issue.id, | |
| 231 | state: { | |
| 232 | ...context, | |
| 233 | reason: "Set aside: the crew is off here, so nothing will pick it up.", | |
| 234 | actor: owner.id, | |
| 235 | }, | |
| 236 | answer: SET_ASIDE, | |
| 237 | p: 1, | |
| 238 | by: "rule", | |
| 239 | ms: 0, | |
| 240 | }); | |
| 241 | recordInteraction(app.db, { | |
| 242 | viewerId: owner.id, | |
| 243 | itemId: itemOf(issue), | |
| 244 | kind: "dismissed", | |
| 245 | repoId: repo.id, | |
| 246 | actorId: owner.id, | |
| 247 | }); | |
| 248 | setOutcome(app.db, decision.id, "set_aside"); | |
| 249 | return { decision: getDecision(app.db, decision.id)!, setAside: true }; | |
| 250 | } |
src/crew/standing.ts added +51
| @@ -0,0 +1,51 @@ | ||
| 1 | /** | |
| 2 | * The standing rule: a repository ruled `decider` or `decider_tell` has its | |
| 3 | * hand-overs answered without a person tapping. A sweep on the asks tick | |
| 4 | * looks at each parked card of such a repository once per park: a shelved | |
| 5 | * repository's closes by rule with no model call, any other goes to the | |
| 6 | * decider (one small-model call), and a card already answered since it was | |
| 7 | * parked is left alone — an unsure one waits for its owner, an undone one | |
| 8 | * stays undone. | |
| 9 | */ | |
| 10 | ||
| 11 | import type { App } from "../app.ts"; | |
| 12 | import { latestDecision } from "../core/decisions.ts"; | |
| 13 | import { askDismissals } from "../core/interactions.ts"; | |
| 14 | import { getIssueById } from "../core/issues.ts"; | |
| 15 | import { listEveryRepo } from "../core/repos.ts"; | |
| 16 | import { getUserById } from "../core/users.ts"; | |
| 17 | import { breakerParked } from "./breaker.ts"; | |
| 18 | import { letDeciderHandle, setAsideShelved } from "./handover.ts"; | |
| 19 | ||
| 20 | /** Model calls one sweep may make: a flood of parks is not a flood of spend. */ | |
| 21 | export const SWEEP_CALLS = 5; | |
| 22 | ||
| 23 | export interface Swept { | |
| 24 | readonly byRule: number; | |
| 25 | readonly byDecider: number; | |
| 26 | } | |
| 27 | ||
| 28 | export async function sweepStanding(app: App): Promise<Swept> { | |
| 29 | let byRule = 0; | |
| 30 | let byDecider = 0; | |
| 31 | const ruled = listEveryRepo(app.db).filter((r) => r.decisionsBy !== "me"); | |
| 32 | if (ruled.length === 0) return { byRule, byDecider }; | |
| 33 | const byId = new Map(ruled.map((r) => [r.id, r])); | |
| 34 | for (const p of breakerParked(app.db, [...byId.keys()])) { | |
| 35 | const repo = byId.get(p.repoId)!; | |
| 36 | const issue = getIssueById(app.db, p.issueId); | |
| 37 | const owner = getUserById(app.db, repo.ownerId); | |
| 38 | if (issue === undefined || owner === undefined) continue; | |
| 39 | const last = latestDecision(app.db, issue.id, "hand"); | |
| 40 | if (last !== undefined && last.createdAt >= p.at) continue; | |
| 41 | if (askDismissals(app.db, owner.id).has(`ask:parked:${issue.id}`)) continue; | |
| 42 | if (repo.autonomy === "off") { | |
| 43 | setAsideShelved(app, owner, repo, issue); | |
| 44 | byRule++; | |
| 45 | } else if (app.config.decider !== undefined && byDecider < SWEEP_CALLS) { | |
| 46 | await letDeciderHandle(app, owner, repo, issue, repo.decisionsBy); | |
| 47 | byDecider++; | |
| 48 | } | |
| 49 | } | |
| 50 | return { byRule, byDecider }; | |
| 51 | } |
src/http/api.ts +11
| @@ -142,6 +142,7 @@ import { audienceFilter } from "../core/event_audience.ts"; | ||
| 142 | 142 | import { |
| 143 | 143 | forkParentFor, |
| 144 | 144 | getReposByIds, |
| 145 | isDecisionsBy, | |
| 145 | 146 | } from "../core/repos.ts"; |
| 146 | 147 | import { |
| 147 | 148 | commentCounts, |
| @@ -1731,6 +1732,12 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1731 | 1732 | const repo = loadRepo(ctx); |
| 1732 | 1733 | if (!repo) return notFound(); |
| 1733 | 1734 | const input = await body(ctx.request); |
| 1735 | if (input.decisionsBy !== undefined && !isDecisionsBy(input.decisionsBy)) | |
| 1736 | return apiError( | |
| 1737 | "bad_decisions_by", | |
| 1738 | 422, | |
| 1739 | "decisionsBy is me, decider or decider_tell", | |
| 1740 | ); | |
| 1734 | 1741 | const updated = await updateRepoSettings(ctx.app, ctx.auth.user, repo, { |
| 1735 | 1742 | ...(input.visibility === "public" || input.visibility === "private" |
| 1736 | 1743 | ? { visibility: input.visibility } |
| @@ -1754,6 +1761,10 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1754 | 1761 | ...(typeof input.crewOutsiders === "boolean" |
| 1755 | 1762 | ? { crewOutsiders: input.crewOutsiders } |
| 1756 | 1763 | : {}), |
| 1764 | // The standing rule for its decisions: also an admin's, never an agent's. | |
| 1765 | ...(isDecisionsBy(input.decisionsBy) | |
| 1766 | ? { decisionsBy: input.decisionsBy } | |
| 1767 | : {}), | |
| 1757 | 1768 | }); |
| 1758 | 1769 | if (!updated.ok) return respondAccess(updated.error, 422); |
| 1759 | 1770 | return json(repoJson(ctx, updated.value)); |
src/http/mcp.ts +44 −2
| @@ -84,6 +84,7 @@ import { | ||
| 84 | 84 | canWrite, |
| 85 | 85 | forkParentFor, |
| 86 | 86 | getRepo, |
| 87 | isDecisionsBy, | |
| 87 | 88 | ownerFactRepoIds, |
| 88 | 89 | type Repo, |
| 89 | 90 | } from "../core/repos.ts"; |
| @@ -100,7 +101,7 @@ import { | ||
| 100 | 101 | directionNote, |
| 101 | 102 | OUTSIDERS_NOTE, |
| 102 | 103 | } from "../crew/direct.ts"; |
| 103 | import { setCrewOutsiders } from "../actions/repos.ts"; | |
| 104 | import { setCrewOutsiders, setDecisionsBy } from "../actions/repos.ts"; | |
| 104 | 105 | import { mapJson, marksFor } from "../web/pages/map.ts"; |
| 105 | 106 | import { undoFleetOp } from "../actions/fleet.ts"; |
| 106 | 107 | import { applyLimb, isLimbAction, previewLimb } from "../actions/limbs.ts"; |
| @@ -587,7 +588,9 @@ const TOOLS: readonly Tool[] = [ | ||
| 587 | 588 | "Let the decider answer a hand-over (a parked issue in `asks`): `item` is the row's `parked:<id>` item. The decider sees the direction, whether you pinned or muted the app, the day's spend against the ceiling, your last ten decisions there and the card's own evidence, and answers set_aside or ask_owner with a reason and a confidence. Only a sure set_aside (70%+) takes the card off your board; anything else stays with you. The answer and the context it saw are in the decision ledger. One card is handed over once in ten minutes. Undo a set_aside within a day with `asks_undecide`.", |
| 588 | 589 | inputSchema: { |
| 589 | 590 | type: "object", |
| 590 | properties: { item: { type: "string", maxLength: 80 } }, | |
| 591 | properties: { | |
| 592 | item: { type: "string", maxLength: 80 }, | |
| 593 | }, | |
| 591 | 594 | required: ["item"], |
| 592 | 595 | }, |
| 593 | 596 | writes: true, |
| @@ -856,6 +859,45 @@ const TOOLS: readonly Tool[] = [ | ||
| 856 | 859 | }); |
| 857 | 860 | }, |
| 858 | 861 | }, |
| 862 | { | |
| 863 | name: "decisions_by", | |
| 864 | description: | |
| 865 | "The standing rule for the decisions a repository asks of its owner (parked hand-over cards): `me` (they wait for the owner), `decider` (the decider answers each, and a repository whose crew is off closes its parked cards by rule, with no model call), or `decider_tell` (the decider answers, and the owner is told about the ones it is unsure of). Without `by`, says how it is set. With `by`, it plans — `dryRun` is true unless you say false — and only an admin of the app, with an admin token and never as an agent, sets it. Each change is recorded in the app's activity.", | |
| 866 | inputSchema: { | |
| 867 | type: "object", | |
| 868 | properties: { | |
| 869 | repo: REPO_ARG, | |
| 870 | by: { type: "string", enum: ["me", "decider", "decider_tell"] }, | |
| 871 | dryRun: { | |
| 872 | type: "boolean", | |
| 873 | description: "Plan only (the default). Send false to set it.", | |
| 874 | }, | |
| 875 | }, | |
| 876 | required: ["repo"], | |
| 877 | }, | |
| 878 | writes: true, | |
| 879 | dryByDefault: true, | |
| 880 | async run(ctx, user, args) { | |
| 881 | const repo = repoArg(ctx, user, args); | |
| 882 | if (isAnswer(repo)) return repo; | |
| 883 | const by = args["by"]; | |
| 884 | if (by === undefined) return good({ by: repo.decisionsBy }); | |
| 885 | if (!isDecisionsBy(by)) | |
| 886 | return bad("bad_action", "`by` is me, decider or decider_tell"); | |
| 887 | const dryRun = args["dryRun"] !== false; | |
| 888 | if (!dryRun && !scopeAllows(ctx.auth.scope ?? "ro", "admin")) | |
| 889 | return bad("forbidden", "setting the standing rule needs an admin token"); | |
| 890 | const set = await setDecisionsBy(ctx.app, user, repo, by, dryRun); | |
| 891 | if (!set.ok) | |
| 892 | return bad("forbidden", "only an admin of this app sets its standing rule"); | |
| 893 | return good({ | |
| 894 | ...set.value, | |
| 895 | says: dryRun | |
| 896 | ? `This is the plan; nothing changed. Decisions go by "${set.value.was}" now; send dryRun: false to make it "${set.value.by}".` | |
| 897 | : `Decisions here now go by "${set.value.by}".`, | |
| 898 | }); | |
| 899 | }, | |
| 900 | }, | |
| 859 | 901 | { |
| 860 | 902 | name: "probe", |
| 861 | 903 | description: |
src/main.ts +7
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | * environment-only — a handful of variables, all with sane defaults. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { sweepStanding } from "./crew/standing.ts"; | |
| 7 | 8 | import { releaseEnv } from "./release/env.ts"; |
| 8 | 9 | import { createApp } from "./app.ts"; |
| 9 | 10 | import { reconcile } from "./core/reconcile.ts"; |
| @@ -354,6 +355,12 @@ runSync(app); // one pass on boot: a forge that was down has fetching to do | ||
| 354 | 355 | // expired: what asked each person, and how it ended. |
| 355 | 356 | const tickAsks = async (): Promise<void> => { |
| 356 | 357 | await sweepStaleAsks(app); |
| 358 | // Repositories ruled "decider" have their hand-overs answered here. | |
| 359 | try { | |
| 360 | await sweepStanding(app); | |
| 361 | } catch (error) { | |
| 362 | app.log.error("standing rule sweep failed", { error: String(error) }); | |
| 363 | } | |
| 357 | 364 | try { |
| 358 | 365 | recordAskLedger(app.db); |
| 359 | 366 | } catch (error) { |
src/tui/screens/workspace.ts +21 −1
| @@ -39,7 +39,8 @@ import { err, ok } from "../../kernel/result.ts"; | ||
| 39 | 39 | import { createBranch } from "../../actions/branches.ts"; |
| 40 | 40 | import { pinRepo, syncRepoNow, unpinRepo } from "../../actions/origins.ts"; |
| 41 | 41 | import { isPinned } from "../../core/pins.ts"; |
| 42 | import { canAdmin, type Repo } from "../../core/repos.ts"; | |
| 42 | import { setDecisionsBy } from "../../actions/repos.ts"; | |
| 43 | import { canAdmin, DECISIONS_BY, getRepoById, type Repo } from "../../core/repos.ts"; | |
| 43 | 44 | import type { TreeEntry } from "../../git/read.ts"; |
| 44 | 45 | import type { RepoFacts, RepoSource } from "../../view/source.ts"; |
| 45 | 46 | import { detectLanguage, highlightLines } from "../../web/highlight.ts"; |
| @@ -1283,6 +1284,9 @@ export function workspaceScreen( | ||
| 1283 | 1284 | binding(["y"], "copy path"), |
| 1284 | 1285 | binding(["i", "p", "C"], "issues / pulls / commits"), |
| 1285 | 1286 | binding(["I"], "about this repository"), |
| 1287 | ...(repo !== undefined && admin() | |
| 1288 | ? [binding(["R"], `decisions by ${(getRepoById(f.db, repo.id) ?? repo).decisionsBy} (cycle)`)] | |
| 1289 | : []), | |
| 1286 | 1290 | ...liveBindings(src), |
| 1287 | 1291 | ...(liveSide?.mirroredAs === undefined |
| 1288 | 1292 | ? [] |
| @@ -1670,6 +1674,22 @@ export function workspaceScreen( | ||
| 1670 | 1674 | ); |
| 1671 | 1675 | return true; |
| 1672 | 1676 | } |
| 1677 | if (plain && e.key === "R" && repo !== undefined && admin()) { | |
| 1678 | // `repo` was read when the screen opened; the rule is read afresh. | |
| 1679 | const target = getRepoById(f.db, repo.id) ?? repo; | |
| 1680 | const next = | |
| 1681 | DECISIONS_BY[(DECISIONS_BY.indexOf(target.decisionsBy) + 1) % DECISIONS_BY.length]!; | |
| 1682 | runAsync(ctx, async () => { | |
| 1683 | const done = await setDecisionsBy(f.app, f.user, target, next, false); | |
| 1684 | if (!done.ok) { | |
| 1685 | ctx.toast("only a person who administers this sets its standing rule", "danger"); | |
| 1686 | return; | |
| 1687 | } | |
| 1688 | reload(); | |
| 1689 | ctx.toast(`decisions here go by ${next}`, "success"); | |
| 1690 | }); | |
| 1691 | return true; | |
| 1692 | } | |
| 1673 | 1693 | if (plain && e.key === "r") { |
| 1674 | 1694 | cache.clear(); |
| 1675 | 1695 | runAsync(ctx, reload); |
src/web/pages/settings.ts +9 −1
| @@ -39,7 +39,7 @@ import { | ||
| 39 | 39 | } from "../../core/app_env.ts"; |
| 40 | 40 | import { addToCrew, listCrew, removeFromCrew } from "../../actions/crew.ts"; |
| 41 | 41 | import { getAgentByName, isCrewRole, setCrewRole } from "../../core/agents.ts"; |
| 42 | import { isAutonomy } from "../../core/repos.ts"; | |
| 42 | import { isAutonomy, isDecisionsBy } from "../../core/repos.ts"; | |
| 43 | 43 | import { |
| 44 | 44 | createLabel, |
| 45 | 45 | createWebhook, |
| @@ -1759,12 +1759,20 @@ export async function autonomySettingsUpdate(ctx: Ctx): Promise<Response> { | ||
| 1759 | 1759 | message: |
| 1760 | 1760 | "Mode is off, assisted or autonomous; the ceiling is a number of dollars; slots are 1 to 8.", |
| 1761 | 1761 | }); |
| 1762 | const rule = field(form, "decisions"); | |
| 1763 | const decisionsBy = rule === "" ? repo.decisionsBy : rule; | |
| 1764 | if (!isDecisionsBy(decisionsBy)) | |
| 1765 | return flashTo(ctx, `${base}/settings#autonomy`, { | |
| 1766 | tone: "danger", | |
| 1767 | message: "Decisions here go to you, the decider, or the decider telling you.", | |
| 1768 | }); | |
| 1762 | 1769 | const updated = await updateRepoSettings(ctx.app, ctx.auth.user, repo, { |
| 1763 | 1770 | autonomy: mode, |
| 1764 | 1771 | autonomyDirection: field(form, "direction").trim().slice(0, 2000), |
| 1765 | 1772 | autonomyCeilingUsd: ceiling, |
| 1766 | 1773 | autonomySlots: slots, |
| 1767 | 1774 | autonomyBy: mode === "off" ? null : (ctx.auth.user?.id ?? null), |
| 1775 | decisionsBy, | |
| 1768 | 1776 | }); |
| 1769 | 1777 | return flashTo(ctx, `${base}/settings#autonomy`, { |
| 1770 | 1778 | ...(updated.ok |
src/web/studio/crew.ts +18
| @@ -326,6 +326,24 @@ ${repo.autonomyDirection}</textarea | ||
| 326 | 326 | map is only what people write.`} |
| 327 | 327 | </p> |
| 328 | 328 | </div> |
| 329 | ${selectField({ | |
| 330 | label: "Decisions here", | |
| 331 | name: "decisions", | |
| 332 | hint: "Who answers the hand-overs this asks of you. The decider answers in the ledger with its reason and context; ‘tell me’ sets a card aside only when it is sure and leaves the unsure ones on your board. A shelved repository (autonomy off) has its parked cards closed by rule, with no model call, when decisions go to the decider. Needs a decider set up on this forge.", | |
| 333 | options: [ | |
| 334 | { value: "me", label: "Me", selected: repo.decisionsBy === "me" }, | |
| 335 | { | |
| 336 | value: "decider", | |
| 337 | label: "The decider", | |
| 338 | selected: repo.decisionsBy === "decider", | |
| 339 | }, | |
| 340 | { | |
| 341 | value: "decider_tell", | |
| 342 | label: "The decider, and tell me the ones it was unsure of", | |
| 343 | selected: repo.decisionsBy === "decider_tell", | |
| 344 | }, | |
| 345 | ], | |
| 346 | })} | |
| 329 | 347 | ${field({ |
| 330 | 348 | label: "Ceiling per day", |
| 331 | 349 | name: "ceiling", |
test/decisions-by-doors.test.ts added +260
| @@ -0,0 +1,260 @@ | ||
| 1 | /** | |
| 2 | * The standing rule for a repository's decisions is reachable beyond the web | |
| 3 | * form (#1605): the JSON API's repository settings, an MCP tool that plans by | |
| 4 | * default, and a key in the terminal workspace. Only an admin who is not an | |
| 5 | * agent sets it. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, 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 { createApp, type App } from "../src/app.ts"; | |
| 12 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 13 | import type { Agent } from "../src/core/agents.ts"; | |
| 14 | import { listEvents } from "../src/core/events.ts"; | |
| 15 | import { | |
| 16 | addTeamMember, | |
| 17 | createOrg, | |
| 18 | createTeam, | |
| 19 | listTeams, | |
| 20 | OWNERS_TEAM, | |
| 21 | } from "../src/core/orgs.ts"; | |
| 22 | import { createRepo, getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 23 | import { createToken } from "../src/core/tokens.ts"; | |
| 24 | import { createUser, type User } from "../src/core/users.ts"; | |
| 25 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 26 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 27 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 28 | import { quietLogs } from "./helpers/globals.ts"; | |
| 29 | import { settleApp } from "./helpers/tui.ts"; | |
| 30 | ||
| 31 | quietLogs(); | |
| 32 | ||
| 33 | let dataDir: string; | |
| 34 | let app: App; | |
| 35 | let servers: Servers; | |
| 36 | let founder: User; | |
| 37 | let dev: User; | |
| 38 | let mallory: User; | |
| 39 | let bot: Agent; | |
| 40 | let repo: Repo; | |
| 41 | const admin: Record<string, string> = {}; | |
| 42 | ||
| 43 | const ok = <T>( | |
| 44 | r: { ok: true; value: T } | { ok: false; error: unknown }, | |
| 45 | ): T => { | |
| 46 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 47 | return r.value; | |
| 48 | }; | |
| 49 | const rule = (): string => getRepoById(app.db, repo.id)!.decisionsBy; | |
| 50 | ||
| 51 | const patch = (token: string, body: unknown) => | |
| 52 | fetch(`${servers.url}/api/v1/repos/acme/svc`, { | |
| 53 | method: "PATCH", | |
| 54 | headers: { | |
| 55 | authorization: `Bearer ${token}`, | |
| 56 | "content-type": "application/json", | |
| 57 | }, | |
| 58 | body: JSON.stringify(body), | |
| 59 | }); | |
| 60 | ||
| 61 | async function mcp( | |
| 62 | token: string, | |
| 63 | name: string, | |
| 64 | args: unknown, | |
| 65 | ): Promise<{ error?: string; value?: any }> { | |
| 66 | const res = await fetch(`${servers.url}/mcp`, { | |
| 67 | method: "POST", | |
| 68 | headers: { | |
| 69 | "content-type": "application/json", | |
| 70 | accept: "application/json, text/event-stream", | |
| 71 | authorization: `Bearer ${token}`, | |
| 72 | }, | |
| 73 | body: JSON.stringify({ | |
| 74 | jsonrpc: "2.0", | |
| 75 | id: 1, | |
| 76 | method: "tools/call", | |
| 77 | params: { name, arguments: args }, | |
| 78 | }), | |
| 79 | }); | |
| 80 | const body = (await res.json()) as any; | |
| 81 | const text: string = body.result.content[0].text; | |
| 82 | return body.result.isError === true | |
| 83 | ? { error: text } | |
| 84 | : { value: JSON.parse(text) }; | |
| 85 | } | |
| 86 | ||
| 87 | beforeAll(async () => { | |
| 88 | dataDir = await mkdtemp(join(tmpdir(), "forge-decisions-by-")); | |
| 89 | app = createApp({ dataDir, port: 0 }); | |
| 90 | servers = await serve(app); | |
| 91 | const make = async (username: string) => | |
| 92 | ok( | |
| 93 | await createUser(app.db, { | |
| 94 | trusted: true, | |
| 95 | username, | |
| 96 | email: `${username}@forge.local`, | |
| 97 | password: "a-good-passphrase", | |
| 98 | adminIfFirst: true, | |
| 99 | }), | |
| 100 | ); | |
| 101 | founder = await make("founder"); | |
| 102 | dev = await make("dev"); | |
| 103 | mallory = await make("mallory"); | |
| 104 | const org = ok(createOrg(app.db, "acme", founder.id)); | |
| 105 | const writers = ok(createTeam(app.db, org.id, "Writers", "write")); | |
| 106 | addTeamMember(app.db, writers, dev.id); | |
| 107 | repo = ok( | |
| 108 | await createRepo(app.db, app.reposRoot, { | |
| 109 | ownerId: org.id, | |
| 110 | name: "svc", | |
| 111 | visibility: "public", | |
| 112 | }), | |
| 113 | ); | |
| 114 | bot = ok(await createAgent(app, founder, { username: "bot" })); | |
| 115 | expect(addToCrew(app, founder, repo, "bot").ok).toBe(true); | |
| 116 | // The hardest agent to refuse: on the team that administers the repository. | |
| 117 | const owners = listTeams(app.db, org.id).find((t) => t.name === OWNERS_TEAM)!; | |
| 118 | addTeamMember(app.db, owners, bot.user.id); | |
| 119 | for (const u of [founder, dev, mallory, bot.user]) | |
| 120 | admin[u.username] = createToken(app.db, u.id, "admin", "admin").token; | |
| 121 | }, 30_000); | |
| 122 | ||
| 123 | afterAll(async () => { | |
| 124 | servers?.stop(); | |
| 125 | app?.close(); | |
| 126 | await rm(dataDir, { recursive: true, force: true }); | |
| 127 | }); | |
| 128 | ||
| 129 | ||
| 130 | describe("the standing rule over the API", () => { | |
| 131 | test("anyone but an admin who is a person is refused", async () => { | |
| 132 | for (const who of ["dev", "mallory", "bot"]) { | |
| 133 | const res = await patch(admin[who]!, { decisionsBy: "decider" }); | |
| 134 | expect(res.status).toBe(404); | |
| 135 | expect(rule()).toBe("me"); | |
| 136 | } | |
| 137 | }); | |
| 138 | ||
| 139 | test("the admin sets it and reads it back; a stranger value is refused", async () => { | |
| 140 | const set = await patch(admin["founder"]!, { decisionsBy: "decider_tell" }); | |
| 141 | expect(set.status).toBe(200); | |
| 142 | expect(((await set.json()) as { decisionsBy: string }).decisionsBy).toBe("decider_tell"); | |
| 143 | expect(rule()).toBe("decider_tell"); | |
| 144 | expect((await patch(admin["founder"]!, { decisionsBy: "anyone" })).status).toBe(422); | |
| 145 | expect((await patch(admin["founder"]!, { description: "x" })).status).toBe(200); | |
| 146 | expect(rule()).toBe("decider_tell"); | |
| 147 | const read = await fetch(`${servers.url}/api/v1/repos/acme/svc`, { | |
| 148 | headers: { authorization: `Bearer ${admin["mallory"]}` }, | |
| 149 | }); | |
| 150 | expect(((await read.json()) as { decisionsBy: string }).decisionsBy).toBe("decider_tell"); | |
| 151 | await patch(admin["founder"]!, { decisionsBy: "me" }); | |
| 152 | }); | |
| 153 | }); | |
| 154 | ||
| 155 | describe("the standing rule over MCP", () => { | |
| 156 | test("without `by` it says how it is set; with `by` it plans unless told otherwise", async () => { | |
| 157 | const said = await mcp(admin["mallory"]!, "decisions_by", { repo: "acme/svc" }); | |
| 158 | expect(said.value.by).toBe("me"); | |
| 159 | const planned = await mcp(admin["founder"]!, "decisions_by", { | |
| 160 | repo: "acme/svc", | |
| 161 | by: "decider", | |
| 162 | }); | |
| 163 | expect(planned.value).toMatchObject({ dryRun: true, by: "decider", was: "me" }); | |
| 164 | expect(rule()).toBe("me"); | |
| 165 | }); | |
| 166 | ||
| 167 | test("a writer, a reader and an agent cannot set it, nor plan it; a bad value is refused", async () => { | |
| 168 | for (const who of ["dev", "mallory", "bot"]) | |
| 169 | for (const dryRun of [true, false]) { | |
| 170 | const tried = await mcp(admin[who]!, "decisions_by", { | |
| 171 | repo: "acme/svc", | |
| 172 | by: "decider", | |
| 173 | dryRun, | |
| 174 | }); | |
| 175 | expect(tried.error).toContain("forbidden"); | |
| 176 | expect(rule()).toBe("me"); | |
| 177 | } | |
| 178 | const odd = await mcp(admin["founder"]!, "decisions_by", { repo: "acme/svc", by: "x" }); | |
| 179 | expect(odd.error).toContain("me, decider or decider_tell"); | |
| 180 | }); | |
| 181 | ||
| 182 | test("the admin needs an admin token to set it", async () => { | |
| 183 | const rw = createToken(app.db, founder.id, "rw", "rw").token; | |
| 184 | const refused = await mcp(rw, "decisions_by", { repo: "acme/svc", by: "decider", dryRun: false }); | |
| 185 | expect(refused.error).toContain("forbidden"); | |
| 186 | expect(rule()).toBe("me"); | |
| 187 | const set = await mcp(admin["founder"]!, "decisions_by", { | |
| 188 | repo: "acme/svc", | |
| 189 | by: "decider", | |
| 190 | dryRun: false, | |
| 191 | }); | |
| 192 | expect(set.value).toMatchObject({ dryRun: false, by: "decider", was: "me" }); | |
| 193 | expect(rule()).toBe("decider"); | |
| 194 | }); | |
| 195 | ||
| 196 | test("asks_decide no longer takes a `by`", async () => { | |
| 197 | const tools = await fetch(`${servers.url}/mcp`, { | |
| 198 | method: "POST", | |
| 199 | headers: { | |
| 200 | "content-type": "application/json", | |
| 201 | accept: "application/json, text/event-stream", | |
| 202 | authorization: `Bearer ${admin["founder"]}`, | |
| 203 | }, | |
| 204 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }), | |
| 205 | }); | |
| 206 | const list = ((await tools.json()) as any).result.tools as any[]; | |
| 207 | expect(list.find((t) => t.name === "asks_decide").inputSchema.properties.by).toBeUndefined(); | |
| 208 | }); | |
| 209 | }); | |
| 210 | ||
| 211 | describe("the standing rule in the terminal", () => { | |
| 212 | test("R cycles it for an admin who is a person, and says so", async () => { | |
| 213 | getRepoById(app.db, repo.id); | |
| 214 | app.db.run("UPDATE repo SET decisions_by = 'me' WHERE id = ?1", [repo.id]); | |
| 215 | const terminal = headlessTerminal(160, 40); | |
| 216 | const { app: tui } = createForgeApp( | |
| 217 | app, | |
| 218 | { sshUsername: "founder", remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user: founder }, | |
| 219 | { terminal, exitHooks: false }, | |
| 220 | ); | |
| 221 | tui.start(); | |
| 222 | const press = async (...keys: string[]) => { | |
| 223 | for (const key of keys) { | |
| 224 | tui.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 225 | await settleApp(tui); | |
| 226 | } | |
| 227 | }; | |
| 228 | await press("/"); | |
| 229 | for (const ch of "svc") await press(ch); | |
| 230 | await press("enter", "enter"); | |
| 231 | await press("R"); | |
| 232 | expect(rule()).toBe("decider"); | |
| 233 | expect(tui.screen.toText()).toContain("decisions here go by decider"); | |
| 234 | await press("R"); | |
| 235 | expect(rule()).toBe("decider_tell"); | |
| 236 | tui.stop(); | |
| 237 | }); | |
| 238 | ||
| 239 | test("a reader's R changes nothing", async () => { | |
| 240 | app.db.run("UPDATE repo SET decisions_by = 'me' WHERE id = ?1", [repo.id]); | |
| 241 | const terminal = headlessTerminal(160, 40); | |
| 242 | const { app: tui } = createForgeApp( | |
| 243 | app, | |
| 244 | { sshUsername: "mallory", remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user: mallory }, | |
| 245 | { terminal, exitHooks: false }, | |
| 246 | ); | |
| 247 | tui.start(); | |
| 248 | const press = async (...keys: string[]) => { | |
| 249 | for (const key of keys) { | |
| 250 | tui.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 251 | await settleApp(tui); | |
| 252 | } | |
| 253 | }; | |
| 254 | await press("/"); | |
| 255 | for (const ch of "svc") await press(ch); | |
| 256 | await press("enter", "enter", "R"); | |
| 257 | expect(rule()).toBe("me"); | |
| 258 | tui.stop(); | |
| 259 | }); | |
| 260 | }); |
test/mcp.test.ts +1
| @@ -175,6 +175,7 @@ describe("the forge over MCP", () => { | ||
| 175 | 175 | "health", |
| 176 | 176 | "audit", |
| 177 | 177 | "outsiders", |
| 178 | "decisions_by", | |
| 178 | 179 | "probe", |
| 179 | 180 | "following", |
| 180 | 181 | "spawn", |
test/standing-rule.test.ts added +183
| @@ -0,0 +1,183 @@ | ||
| 1 | /** | |
| 2 | * A standing rule per repository: hand-overs on a repository ruled | |
| 3 | * "decider" are answered by the sweep without a person; a shelved one | |
| 4 | * closes with no model call; "decider_tell" leaves the unsure ones; the | |
| 5 | * settings form saves the rule; a manual tap follows it. | |
| 6 | */ | |
| 7 | ||
| 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { addComment, openIssue } from "../src/actions/issues.ts"; | |
| 14 | import { decideAsk } from "../src/actions/asks_decide.ts"; | |
| 15 | import { createRepo } from "../src/actions/repos.ts"; | |
| 16 | import { listDecisions } from "../src/core/decisions.ts"; | |
| 17 | import type { Issue } from "../src/core/issues.ts"; | |
| 18 | import { getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts"; | |
| 19 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 20 | import { BREAKER_NOTE } from "../src/crew/breaker.ts"; | |
| 21 | import type { Decider } from "../src/crew/decide.ts"; | |
| 22 | import { undoHandled } from "../src/crew/handover.ts"; | |
| 23 | import { sweepStanding } from "../src/crew/standing.ts"; | |
| 24 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 25 | import { askLinesFor } from "../src/web/studio/asks.ts"; | |
| 26 | import { quietLogs } from "./helpers/globals.ts"; | |
| 27 | ||
| 28 | quietLogs(); | |
| 29 | ||
| 30 | describe("a standing rule per repository", () => { | |
| 31 | let dataDir = ""; | |
| 32 | let app: App; | |
| 33 | let servers: Servers; | |
| 34 | let alice: User; | |
| 35 | let repo: Repo; | |
| 36 | let cookie = ""; | |
| 37 | let calls = 0; | |
| 38 | let says = 0.9; | |
| 39 | ||
| 40 | const standIn: Decider = { | |
| 41 | name: "stand-in", | |
| 42 | async decide(_state, questions) { | |
| 43 | calls++; | |
| 44 | const out: Record<string, any> = {}; | |
| 45 | for (const [key, q] of Object.entries(questions)) { | |
| 46 | const [a, b] = q.options as [string, string]; | |
| 47 | out[key] = { choice: a, p: says, probabilities: { [a]: says, [b]: 1 - says } }; | |
| 48 | } | |
| 49 | return out as any; | |
| 50 | }, | |
| 51 | }; | |
| 52 | ||
| 53 | const rule = (by: "me" | "decider" | "decider_tell", autonomy: "off" | "assisted" = "assisted") => { | |
| 54 | repo = updateRepoSettings(app.db, repo, { decisionsBy: by, autonomy }); | |
| 55 | }; | |
| 56 | const parked = (title: string): Issue => { | |
| 57 | const made = openIssue(app, alice, repo, { title }); | |
| 58 | if (!made.ok) throw new Error(String(made.error)); | |
| 59 | expect(addComment(app, alice, repo, made.value, BREAKER_NOTE).ok).toBe(true); | |
| 60 | return made.value; | |
| 61 | }; | |
| 62 | const asking = (issue: Issue): boolean => | |
| 63 | askLinesFor(app.db, alice).some((r) => r.items.includes(`parked:${issue.id}`)); | |
| 64 | ||
| 65 | beforeAll(async () => { | |
| 66 | dataDir = await mkdtemp(join(tmpdir(), "forge-standing-")); | |
| 67 | app = createApp({ dataDir, port: 0 }); | |
| 68 | servers = await serve(app); | |
| 69 | const made = await createUser(app.db, { | |
| 70 | trusted: true, | |
| 71 | username: "alice", | |
| 72 | email: "[email protected]", | |
| 73 | password: "a-long-standing-passphrase", | |
| 74 | }); | |
| 75 | if (!made.ok) throw new Error("no alice"); | |
| 76 | const signin = await fetch(`${servers.url}/signin`, { | |
| 77 | method: "POST", | |
| 78 | body: new URLSearchParams({ username: "alice", password: "a-long-standing-passphrase" }), | |
| 79 | redirect: "manual", | |
| 80 | }); | |
| 81 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 82 | alice = getUserByName(app.db, "alice")!; | |
| 83 | const r = await createRepo(app, alice, { name: "shelf", visibility: "public" }); | |
| 84 | if (!r.ok) throw new Error(String(r.error)); | |
| 85 | repo = r.value; | |
| 86 | app.config.decider = standIn; | |
| 87 | }, 30_000); | |
| 88 | ||
| 89 | afterAll(async () => { | |
| 90 | await servers.stop(); | |
| 91 | await rm(dataDir, { recursive: true, force: true }); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a repository ruled me is left alone", async () => { | |
| 95 | rule("me"); | |
| 96 | const issue = parked("mine"); | |
| 97 | const before = calls; | |
| 98 | await sweepStanding(app); | |
| 99 | expect(asking(issue)).toBe(true); | |
| 100 | expect(calls).toBe(before); | |
| 101 | }); | |
| 102 | ||
| 103 | test("ruled decider, a hand-over is answered without a person, once", async () => { | |
| 104 | rule("decider"); | |
| 105 | says = 0.9; | |
| 106 | const issue = parked("old idea"); | |
| 107 | // The card left over from the "me" test is the rule's too, now. | |
| 108 | const swept = await sweepStanding(app); | |
| 109 | expect(swept.byDecider).toBe(2); | |
| 110 | expect(asking(issue)).toBe(false); | |
| 111 | const row = listDecisions(app.db, repo.id, { kind: "hand" }).find((d) => d.issueId === issue.id)!; | |
| 112 | expect(row.by).toBe("stand-in"); | |
| 113 | expect(row.outcome).toBe("set_aside"); | |
| 114 | const after = calls; | |
| 115 | await sweepStanding(app); | |
| 116 | expect(calls).toBe(after); | |
| 117 | }); | |
| 118 | ||
| 119 | test("decider_tell leaves an unsure one with the owner; decider's word stands", async () => { | |
| 120 | says = 0.55; | |
| 121 | rule("decider_tell"); | |
| 122 | const unsure = parked("hard call"); | |
| 123 | await sweepStanding(app); | |
| 124 | expect(asking(unsure)).toBe(true); | |
| 125 | const row = listDecisions(app.db, repo.id, { kind: "hand" }).find((d) => d.issueId === unsure.id)!; | |
| 126 | expect(row.outcome).toBe("asked"); | |
| 127 | ||
| 128 | rule("decider"); | |
| 129 | const standing = parked("settled"); | |
| 130 | await sweepStanding(app); | |
| 131 | expect(asking(standing)).toBe(false); | |
| 132 | // The unsure one was already answered: it is not asked again. | |
| 133 | expect(asking(unsure)).toBe(true); | |
| 134 | says = 0.9; | |
| 135 | }); | |
| 136 | ||
| 137 | test("a shelved repository's hand-over closes with no model call, and undo restores it", async () => { | |
| 138 | rule("decider_tell", "off"); | |
| 139 | const issue = parked("shelved"); | |
| 140 | const before = calls; | |
| 141 | const swept = await sweepStanding(app); | |
| 142 | expect(swept.byRule).toBeGreaterThanOrEqual(1); | |
| 143 | expect(calls).toBe(before); | |
| 144 | expect(asking(issue)).toBe(false); | |
| 145 | const row = listDecisions(app.db, repo.id, { kind: "hand" }).find((d) => d.issueId === issue.id)!; | |
| 146 | expect(row.by).toBe("rule"); | |
| 147 | expect((row.state as any).evidence.title).toBe("shelved"); | |
| 148 | expect(undoHandled(app, alice, row.id)).toMatchObject({ outcome: "undone" }); | |
| 149 | expect(asking(issue)).toBe(true); | |
| 150 | await sweepStanding(app); | |
| 151 | expect(asking(issue)).toBe(true); | |
| 152 | }); | |
| 153 | ||
| 154 | test("the settings form saves the rule and refuses a made-up one", async () => { | |
| 155 | const post = (decisions: string) => | |
| 156 | fetch(`${servers.url}/alice/shelf/settings/autonomy`, { | |
| 157 | method: "POST", | |
| 158 | headers: { cookie, origin: servers.url }, | |
| 159 | body: new URLSearchParams({ autonomy: "assisted", ceiling: "20", slots: "1", direction: "", decisions }), | |
| 160 | redirect: "manual", | |
| 161 | }); | |
| 162 | expect((await post("decider")).status).toBe(303); | |
| 163 | expect(getRepoById(app.db, repo.id)!.decisionsBy).toBe("decider"); | |
| 164 | await post("nobody"); | |
| 165 | expect(getRepoById(app.db, repo.id)!.decisionsBy).toBe("decider"); | |
| 166 | const page = await (await fetch(`${servers.url}/alice/shelf/settings`, { headers: { cookie } })).text(); | |
| 167 | expect(page).toContain("Decisions here"); | |
| 168 | }); | |
| 169 | ||
| 170 | test("a manual tap follows the repository's rule, as the sweep does", async () => { | |
| 171 | const tap = async (title: string) => { | |
| 172 | const issue = parked(title); | |
| 173 | const done = await decideAsk(app, alice, `parked:${issue.id}`); | |
| 174 | expect(done.ok).toBe(true); | |
| 175 | return issue; | |
| 176 | }; | |
| 177 | says = 0.6; | |
| 178 | rule("decider_tell"); | |
| 179 | expect(asking(await tap("tap cautious"))).toBe(true); | |
| 180 | rule("decider"); | |
| 181 | expect(asking(await tap("tap ruled"))).toBe(false); | |
| 182 | }); | |
| 183 | }); |
new above
offline
autonomy === "off"is what counts as "shelved" here. That is reasonable, but the README and the settings hint should say so plainly, so an owner who turns the crew off knows that parked cards on a repository ruled decider will close by themselves.