Public: anyone can read this; the crew can change it.
The week's cap holds the crew's own work only: what a person sends starts regardless #16323
Check · Review · Land · Live done — Landed and live in production.
talk/cap-directed → main 0 commits
11 files
changed +281
−12
docs/CREW.md +5 −2
| @@ -484,8 +484,11 @@ The same read is a second door: the MCP `vision` tool takes an optional `number` | ||
| 484 | 484 | The Weekly budget is the share of the week the crew may use. The forge reads |
| 485 | 485 | the percent of the week the provider reports as used (`subscription_measure`, |
| 486 | 486 | noted from the session's `rate_limit_event`) and, once it reaches that share, |
| 487 | starts nothing until the reset (`src/crew/week.ts`); reviews and a person's | |
| 488 | assignments wait too. Below the share nothing slows the crew, and at 100% only | |
| 487 | starts nothing of the crew's own until the reset (`src/crew/week.ts`): its picks, | |
| 488 | reviews, feedback and check runs. Work a person sends starts regardless (#15734): a run | |
| 489 | whose cause came from a person's own request (`personActing`, `core/acting_via.ts`: a | |
| 490 | page, a token, Talk's agent for them, a terminal key) is marked `run.sent`. Every review stays | |
| 491 | under the cap, even of a pull a person opened, until the owner says otherwise. Below the share nothing slows the crew, and at 100% only | |
| 489 | 492 | the provider's own refusal stops it (that pauses runs with its reset time, |
| 490 | 493 | `src/crew/limits.ts`). No dollar cap is inferred, and an unmeasured week holds |
| 491 | 494 | nothing. Home, Spend and the knobs line say it plainly: `25% of the week used, |
docs/features/crew-week.md +1
| @@ -12,3 +12,4 @@ row: The week is one budget for the whole crew | ||
| 12 | 12 | - Home and the terminal print the crew's number: "the week allows the whole crew N of M slots". |
| 13 | 13 | - Picks made earlier in the same tick in other repositories count against the budget before their runs show, so a burst across repositories cannot pass the crew's floor. |
| 14 | 14 | - Work a person assigns starts through the dispatcher, which does not read `repoPace`, so it is not held by the crew's budget. |
| 15 | - At the dispatcher's start the week's cap holds only the crew's own runs: a run a person sent (`run.sent`, set where a person's own request makes it) starts past the cap, and waits only at the provider's own limit, all of the week (#15734). Proof: `test/week-cap-directed.test.ts`. |
src/actions/issues.ts +5 −2
| @@ -57,6 +57,7 @@ import { isStructural, pillarsOf } from "../core/pillars.ts"; | ||
| 57 | 57 | import { closesFromBody, getPull } from "../core/pulls.ts"; |
| 58 | 58 | import { canRead, canWrite, type Repo } from "../core/repos.ts"; |
| 59 | 59 | import { getUserById, type User } from "../core/users.ts"; |
| 60 | import { personActing } from "../core/acting_via.ts"; | |
| 60 | 61 | import { |
| 61 | 62 | directorsAmong, |
| 62 | 63 | mayDirectCrew, |
| @@ -400,6 +401,8 @@ export function assignIssue( | ||
| 400 | 401 | ...(variantGroup ? { variantGroup } : {}), |
| 401 | 402 | // A person's assignment says so; an event without it (queued before the mark) is a pick. |
| 402 | 403 | ...(pick ? { pick: true } : { person: true }), |
| 404 | // Made in a person's own request: theirs to send (`personActing`), not the crew's. | |
| 405 | ...(!pick && personActing(writer.value) ? { sent: true } : {}), | |
| 403 | 406 | }, |
| 404 | 407 | }); |
| 405 | 408 | return ok(updated); |
| @@ -431,7 +434,7 @@ export function addComment( | ||
| 431 | 434 | kind: `${issue.kind}.commented`, |
| 432 | 435 | repoId: repo.id, |
| 433 | 436 | actorId: user.value.id, |
| 434 | payload: { number: issue.number, body, commentId: added.value.id }, | |
| 437 | payload: { number: issue.number, body, commentId: added.value.id, ...(personActing(user.value) ? { sent: true } : {}) }, | |
| 435 | 438 | }); |
| 436 | 439 | return added; |
| 437 | 440 | } |
| @@ -990,7 +993,7 @@ export async function addCommentThrough( | ||
| 990 | 993 | kind: `${issue.kind}.commented`, |
| 991 | 994 | repoId: repo.id, |
| 992 | 995 | actorId: user.value.id, |
| 993 | payload: { number: issue.number, via: "origin" }, | |
| 996 | payload: { number: issue.number, via: "origin", ...(personActing(user.value) ? { sent: true } : {}) }, | |
| 994 | 997 | }); |
| 995 | 998 | return ok( |
| 996 | 999 | listComments(app.db, issue.id).find((c) => c.id === localId) ?? |
src/actions/pulls.ts +5 −1
| @@ -77,6 +77,7 @@ import { missingHomeLooks, touchesStylesheet } from "../looks/style_looks.ts"; | ||
| 77 | 77 | import { grantRun } from "../core/run_grant.ts"; |
| 78 | 78 | import { commitIdentity } from "../core/commit_identity.ts"; |
| 79 | 79 | import { getUserById, getUserByName, type User } from "../core/users.ts"; |
| 80 | import { personActing } from "../core/acting_via.ts"; | |
| 80 | 81 | import { squashCredits } from "../git/coauthors.ts"; |
| 81 | 82 | import { resolveOid, type MergeStrategy } from "../git/merge.ts"; |
| 82 | 83 | import { bareTitle, fromKeptFork, landingKind } from "../core/landing_kind.ts"; |
| @@ -485,7 +486,7 @@ function postLine( | ||
| 485 | 486 | kind: "pull.reviewed", |
| 486 | 487 | repoId: pull.repoId, |
| 487 | 488 | actorId: user.id, |
| 488 | payload: { number: pull.number, path: line.path, line: line.line }, | |
| 489 | payload: { number: pull.number, path: line.path, line: line.line, ...(personActing(user) ? { sent: true } : {}) }, | |
| 489 | 490 | }); |
| 490 | 491 | return added; |
| 491 | 492 | } |
| @@ -819,6 +820,7 @@ export async function submitReview( | ||
| 819 | 820 | actorId: user.id, |
| 820 | 821 | payload: { |
| 821 | 822 | number: pull.number, |
| 823 | ...(personActing(user) ? { sent: true } : {}), | |
| 822 | 824 | state: input.state, |
| 823 | 825 | via: "origin", |
| 824 | 826 | ...(stored ? { reviewId: stored.id } : {}), |
| @@ -887,6 +889,7 @@ export async function submitReview( | ||
| 887 | 889 | actorId: user.id, |
| 888 | 890 | payload: { |
| 889 | 891 | number: pull.number, |
| 892 | ...(personActing(user) ? { sent: true } : {}), | |
| 890 | 893 | state: input.state, |
| 891 | 894 | body: (input.body ?? "").trim(), |
| 892 | 895 | reviewId: recorded.value.id, |
| @@ -1073,6 +1076,7 @@ export async function approveHere( | ||
| 1073 | 1076 | actorId: user.id, |
| 1074 | 1077 | payload: { |
| 1075 | 1078 | number: pull.number, |
| 1079 | ...(personActing(user) ? { sent: true } : {}), | |
| 1076 | 1080 | state: "approved", |
| 1077 | 1081 | via: "here", |
| 1078 | 1082 | reviewId: recorded.value.id, |
src/core/acting_via.ts +7
| @@ -38,5 +38,12 @@ export const currentDoor = (actorId: string): string | undefined => { | ||
| 38 | 38 | return acting !== undefined && acting.userId === actorId ? acting.door : undefined; |
| 39 | 39 | }; |
| 40 | 40 | |
| 41 | /** | |
| 42 | * Whether `actor` is a person acting now, in their own request (a page, a token, Talk's agent for | |
| 43 | * them, a terminal session): what they do there is theirs to send, not the crew's own work. | |
| 44 | */ | |
| 45 | export const personActing = (actor: { readonly id: string; readonly kind: string }): boolean => | |
| 46 | actor.kind === "user" && store.getStore()?.userId === actor.id; | |
| 47 | ||
| 41 | 48 | /** The words that follow the person's name on the trail. */ |
| 42 | 49 | export const VIA_WORDS = "via the forge's agent"; |
src/core/migration-pins/20261124151500-run-sent.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | bd50c24a4aa81a5f3e5d703f141e588ec67d1d71e6d988a78aa1c72346bf7f73 |
src/core/migrations/20261124151500-run-sent.ts added +5
| @@ -0,0 +1,5 @@ | ||
| 1 | // A run a person sent (#15734): set where a person's own action makes it (an assignment, a comment or a | |
| 2 | // review through a door, Talk, a re-run); the week's cap holds only runs without it. Additive. | |
| 3 | export default ` | |
| 4 | ALTER TABLE run ADD COLUMN sent INTEGER NOT NULL DEFAULT 0; | |
| 5 | `; |
src/core/runs.ts +9 −2
| @@ -130,6 +130,8 @@ export interface Run { | ||
| 130 | 130 | readonly combinedOid: string; |
| 131 | 131 | /** The variant group this run belongs to; null for an ordinary run. */ |
| 132 | 132 | readonly variantGroup: string | null; |
| 133 | /** A person sent it (`core/acting_via.ts`): the week's cap does not hold it. */ | |
| 134 | readonly sent: boolean; | |
| 133 | 135 | readonly createdAt: string; |
| 134 | 136 | readonly startedAt: string | null; |
| 135 | 137 | /** When this deploy's environment started serving — set once, at the swap-in. */ |
| @@ -163,6 +165,7 @@ interface RunRow { | ||
| 163 | 165 | pushed: number | null; |
| 164 | 166 | sandbox_enforced: number | null; |
| 165 | 167 | sandbox_note: string; |
| 168 | sent: number; | |
| 166 | 169 | check_detail: string; |
| 167 | 170 | base_oid: string; |
| 168 | 171 | combined_oid: string; |
| @@ -208,6 +211,7 @@ const toRun = (r: RunRow): Run => ({ | ||
| 208 | 211 | baseOid: r.base_oid, |
| 209 | 212 | combinedOid: r.combined_oid, |
| 210 | 213 | variantGroup: r.variant_group, |
| 214 | sent: r.sent === 1, | |
| 211 | 215 | createdAt: r.created_at, |
| 212 | 216 | startedAt: r.started_at, |
| 213 | 217 | servingAt: r.serving_at, |
| @@ -232,14 +236,16 @@ export interface CreateRunInput { | ||
| 232 | 236 | summary?: string | undefined; |
| 233 | 237 | /** The variant group the run belongs to, when it is one of several tries. */ |
| 234 | 238 | variantGroup?: string | undefined; |
| 239 | /** A person sent it: the week's cap does not hold it. */ | |
| 240 | sent?: boolean | undefined; | |
| 235 | 241 | } |
| 236 | 242 | |
| 237 | 243 | export function createRun(db: Database, input: CreateRunInput): Run { |
| 238 | 244 | const runId = id(); |
| 239 | 245 | db.query( |
| 240 | 246 | `INSERT INTO run (id, repo_id, kind, issue_id, env, commit_oid, branch, actor_id, reason, |
| 241 | not_before, attempt, summary, variant_group, interruptions) | |
| 242 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)`, | |
| 247 | not_before, attempt, summary, variant_group, interruptions, sent) | |
| 248 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15)`, | |
| 243 | 249 | ).run( |
| 244 | 250 | runId, |
| 245 | 251 | input.repoId, |
| @@ -255,6 +261,7 @@ export function createRun(db: Database, input: CreateRunInput): Run { | ||
| 255 | 261 | input.summary ?? "", |
| 256 | 262 | input.variantGroup ?? null, |
| 257 | 263 | input.interruptions ?? 0, |
| 264 | input.sent === true ? 1 : 0, | |
| 258 | 265 | ); |
| 259 | 266 | return getRun(db, runId)!; |
| 260 | 267 | } |
src/crew/dispatch.ts +21 −5
| @@ -16,6 +16,7 @@ import { TEMP_FULL } from "../checks/disk_full.ts"; | ||
| 16 | 16 | import { freshPullFor } from "./fresh_pull.ts"; |
| 17 | 17 | import { betCapHold } from "./bet_cap_hold.ts"; |
| 18 | 18 | import { weekHold } from "./week_hold.ts"; |
| 19 | import { personActing } from "../core/acting_via.ts"; | |
| 19 | 20 | import { weekFor, weekSpent } from "./week_read.ts"; |
| 20 | 21 | import { redStreak } from "./red_thrice.ts"; |
| 21 | 22 | import { twinNote, twinOf, type Twin } from "./twin.ts"; |
| @@ -490,7 +491,7 @@ export function queueAgentRun( | ||
| 490 | 491 | agent: Agent, |
| 491 | 492 | reason: RunReason, |
| 492 | 493 | actorId?: string, |
| 493 | again?: { notBefore: string; attempt: number; summary: string; interruptions?: number }, | |
| 494 | again?: { notBefore: string; attempt: number; summary: string; interruptions?: number; sent?: boolean }, | |
| 494 | 495 | variantGroup?: string, |
| 495 | 496 | retry = false, |
| 496 | 497 | /** |
| @@ -498,6 +499,8 @@ export function queueAgentRun( | ||
| 498 | 499 | * reviews). |
| 499 | 500 | */ |
| 500 | 501 | own = false, |
| 502 | /** A person sent it, by an event a person's request wrote (`personActing`). */ | |
| 503 | sent = false, | |
| 501 | 504 | ): Run | undefined { |
| 502 | 505 | // A suspended or removed owner's repositories run nothing, whoever asks. |
| 503 | 506 | if (!accountActive(app.db, repo.ownerId) || isContributionFork(app.db, repo.id)) return undefined; |
| @@ -552,12 +555,17 @@ export function queueAgentRun( | ||
| 552 | 555 | // actions that start one by name refuse out loud before they get here. |
| 553 | 556 | const asker = actorId === undefined ? undefined : getUserById(app.db, actorId); |
| 554 | 557 | if (asker !== undefined && lacks(app.db, asker, "run_crew")) return undefined; |
| 558 | // A run a person sent: by an event their request wrote, in their own request now (a re-run, a | |
| 559 | // proposal taken), or carried on from one they sent. The week's cap holds only the crew's own | |
| 560 | // work; a review stays under it whoever opened the pull, until the owner says otherwise. | |
| 561 | const directed = sent || again?.sent === true || (asker !== undefined && personActing(asker)); | |
| 555 | 562 | // The week's share gates every run the crew starts on its own, not only |
| 556 | 563 | // the picks: a review, a feedback run or a conflict run past the week's share |
| 557 | 564 | // is money the person did not agree to. A person's own assignment is not |
| 558 | 565 | // the crew's. |
| 559 | 566 | if ( |
| 560 | 567 | reason !== "assigned" && |
| 568 | !directed && | |
| 561 | 569 | repo.autonomy === "autonomous" && |
| 562 | 570 | weekSpent(app, repo, new Date()) |
| 563 | 571 | ) { |
| @@ -666,6 +674,7 @@ export function queueAgentRun( | ||
| 666 | 674 | // A run queued again (a restart, a pause, the sweep) stays in the |
| 667 | 675 | // group its issue was started in. |
| 668 | 676 | ...(group ? { variantGroup: group } : {}), |
| 677 | sent: directed, | |
| 669 | 678 | }); |
| 670 | 679 | }); |
| 671 | 680 | if (run !== undefined) announceRun(app, run, actorId); |
| @@ -751,6 +760,9 @@ export function route(app: App, event: ForgeEvent): void { | ||
| 751 | 760 | typeof event.payload["variantGroup"] === "string" |
| 752 | 761 | ? event.payload["variantGroup"] |
| 753 | 762 | : undefined, |
| 763 | false, | |
| 764 | false, | |
| 765 | event.payload["sent"] === true, | |
| 754 | 766 | ); |
| 755 | 767 | return; |
| 756 | 768 | } |
| @@ -841,7 +853,7 @@ export function route(app: App, event: ForgeEvent): void { | ||
| 841 | 853 | markHeard(app.db, commentId, active.id); |
| 842 | 854 | return; |
| 843 | 855 | } |
| 844 | queueAgentRun(app, repo, issue, agent, "feedback", actor.id); | |
| 856 | queueAgentRun(app, repo, issue, agent, "feedback", actor.id, undefined, undefined, false, false, event.payload["sent"] === true); | |
| 845 | 857 | return; |
| 846 | 858 | } |
| 847 | 859 | case "run.finished": { |
| @@ -1538,6 +1550,7 @@ export function requeue(app: App, run: Run, why: string, interrupted: boolean, n | ||
| 1538 | 1550 | attempt: run.attempt + 1, |
| 1539 | 1551 | interruptions, |
| 1540 | 1552 | summary: `${why}; carries on where it stopped`, |
| 1553 | sent: run.sent, | |
| 1541 | 1554 | }, |
| 1542 | 1555 | ); |
| 1543 | 1556 | log.info("run queued again", { run: run.id, reason: task.reason, why }); |
| @@ -1564,6 +1577,7 @@ function resumeLater(app: App, run: Run, pause: Pause): void { | ||
| 1564 | 1577 | summary: |
| 1565 | 1578 | pause.summary ?? |
| 1566 | 1579 | `paused on the provider's limit; resumes ${whenText(new Date(pause.resumeAt))}`, |
| 1580 | sent: run.sent, | |
| 1567 | 1581 | }, |
| 1568 | 1582 | ); |
| 1569 | 1583 | log.info("run paused", { |
| @@ -1760,11 +1774,13 @@ export function pump( | ||
| 1760 | 1774 | } |
| 1761 | 1775 | continue; |
| 1762 | 1776 | } |
| 1763 | // The week's ceiling holds every path that starts a run in an autonomous | |
| 1764 | // repository: assigned, review, feedback and check runs as much as picks (#8873). | |
| 1777 | // The week's ceiling holds every run the crew starts on its own in an autonomous repository: | |
| 1778 | // picks, reviews, feedback and check runs (#8873). A run a person sent passes it, short of the | |
| 1779 | // provider's own limit: the owner's "that cap should only affect autonomous work". | |
| 1765 | 1780 | if (home !== undefined && home.autonomy === "autonomous") { |
| 1766 | 1781 | if (!weeks.has(home.id)) weeks.set(home.id, weekFor(app, home, new Date())); |
| 1767 | const paced = weekHold(weeks.get(home.id)!); | |
| 1782 | const week = weeks.get(home.id)!; | |
| 1783 | const paced = fresh.sent && week !== null && week.usedPct < 100 ? null : weekHold(week); | |
| 1768 | 1784 | if (paced !== null) { |
| 1769 | 1785 | held.set(fresh.id, "pace"); |
| 1770 | 1786 | if (!noted.has(fresh.id)) { |
src/tui/forge.ts +14
| @@ -45,6 +45,7 @@ import type { Command, PaletteItem, PaletteSearch } from "./app/palette.ts"; | ||
| 45 | 45 | import { G } from "./term/screen.ts"; |
| 46 | 46 | import { fileIcon, icon, type IconName } from "./ui/icons.ts"; |
| 47 | 47 | import type { SessionOpts, Terminal } from "./term/terminal.ts"; |
| 48 | import { actingVia } from "../core/acting_via.ts"; | |
| 48 | 49 | import { Session, type TuiIdentity } from "./session.ts"; |
| 49 | 50 | import { |
| 50 | 51 | ISSUE_MARK, |
| @@ -979,8 +980,21 @@ export function createForgeApp( | ||
| 979 | 980 | options: ForgeAppOptions, |
| 980 | 981 | ): { app: App; session: Session } { |
| 981 | 982 | const session = new Session(forge, identity); |
| 983 | // Each key a person presses runs as them (`core/acting_via.ts`): what it sends is theirs. | |
| 984 | const user = session.user; | |
| 985 | const terminal: Terminal = | |
| 986 | user === undefined | |
| 987 | ? options.terminal | |
| 988 | : new Proxy(options.terminal, { | |
| 989 | get(t, k) { | |
| 990 | if (k === "onInput") return (cb: (chunk: Uint8Array) => void) => t.onInput((chunk) => actingVia({ userId: user.id, door: "ssh" }, () => cb(chunk))); | |
| 991 | const v: unknown = Reflect.get(t, k); | |
| 992 | return typeof v === "function" ? (v as (...a: unknown[]) => unknown).bind(t) : v; | |
| 993 | }, | |
| 994 | }); | |
| 982 | 995 | const app = new App({ |
| 983 | 996 | ...options, |
| 997 | terminal, | |
| 984 | 998 | theme: session.theme(), |
| 985 | 999 | prefs: session.terminalPrefs(), |
| 986 | 1000 | root: forgeRoot(session), |
test/week-cap-directed.test.ts added +208
| @@ -0,0 +1,208 @@ | ||
| 1 | /** | |
| 2 | * The week's cap holds the crew's own work only (#15734, the owner: "that cap should only affect | |
| 3 | * autonomous work. i should still be able to send work off if that is my intention"). A run a person | |
| 4 | * sent, from their own request, starts past the cap; the crew's own waits with today's sentence; at | |
| 5 | * the provider's own limit (all of the week) both wait. The terminal acts as its person too. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { addToCrew } from "../src/actions/crew.ts"; | |
| 9 | import { assignIssue, openIssue } from "../src/actions/issues.ts"; | |
| 10 | import { | |
| 11 | commitToBranch, | |
| 12 | createRepo, | |
| 13 | updateRepoSettings, | |
| 14 | } from "../src/actions/repos.ts"; | |
| 15 | import { actingVia, personActing } from "../src/core/acting_via.ts"; | |
| 16 | import { noteMeasure } from "../src/core/measure.ts"; | |
| 17 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 18 | import { createRun, listRuns, type Run } from "../src/core/runs.ts"; | |
| 19 | import { setHeadroom } from "../src/core/standing.ts"; | |
| 20 | import { addSubscription } from "../src/core/subscriptions.ts"; | |
| 21 | import { forgetWeek } from "../src/crew/week_read.ts"; | |
| 22 | import { readRunLog } from "../src/runs/log.ts"; | |
| 23 | import type { Screen } from "../src/tui/app/app.ts"; | |
| 24 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 25 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 26 | import { | |
| 27 | startStandInCrew, | |
| 28 | stopStandInCrew, | |
| 29 | type StandInCrew, | |
| 30 | } from "./helpers/crew-standin.ts"; | |
| 31 | import { settleApp } from "./helpers/tui.ts"; | |
| 32 | import { pollUntil } from "./helpers/poll.ts"; | |
| 33 | ||
| 34 | const DAY = 86_400_000; | |
| 35 | /** Each test's own timeout; every wait in it fits inside, with room to spare. */ | |
| 36 | const TEST_MS = 30_000; | |
| 37 | /** One wait's deadline, inside TEST_MS, so a slow tick fails as this test, by name. */ | |
| 38 | const WAIT_MS = 8_000; | |
| 39 | ||
| 40 | /** Wait until `done` holds, or fail at the deadline: the pump's tick is what we wait on. */ | |
| 41 | const until = (done: () => boolean, what: string): Promise<void> => pollUntil(done, what, WAIT_MS); | |
| 42 | ||
| 43 | let standIn: StandInCrew; | |
| 44 | let repo: Repo; | |
| 45 | let measure: (used: number) => void; | |
| 46 | ||
| 47 | beforeAll(async () => { | |
| 48 | standIn = await startStandInCrew({ tickMs: 50 }); | |
| 49 | const { app, alice } = standIn; | |
| 50 | const made = await createRepo(app, alice, { | |
| 51 | name: "arena", | |
| 52 | visibility: "private", | |
| 53 | }); | |
| 54 | if (!made.ok) throw new Error(String(made.error)); | |
| 55 | const seeded = await commitToBranch(app, alice, made.value, { | |
| 56 | message: "Start", | |
| 57 | files: [{ path: "README.md", content: "# arena\n" }], | |
| 58 | }); | |
| 59 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 60 | expect(addToCrew(app, alice, made.value, "bot").ok).toBe(true); | |
| 61 | const sub = addSubscription(app.db, standIn.dataDir, { | |
| 62 | name: "main", | |
| 63 | token: "sk-ant-oat01-week-cap-directed", | |
| 64 | }); | |
| 65 | if (!sub.ok) throw new Error(sub.error); | |
| 66 | setHeadroom(app.db, sub.value.id, 0); | |
| 67 | app.db | |
| 68 | .query( | |
| 69 | "INSERT INTO subscription_grant (scope_id, agent_id, subscription_id) VALUES (?1, '', ?2)", | |
| 70 | ) | |
| 71 | .run(made.value.id, sub.value.id); | |
| 72 | const now = Date.now(); | |
| 73 | measure = (used: number) => { | |
| 74 | noteMeasure(app.db, sub.value.id, { | |
| 75 | window: "week", | |
| 76 | usedPct: used, | |
| 77 | resetsAt: new Date(now + 3.5 * DAY).toISOString(), | |
| 78 | observedAt: new Date(now).toISOString(), | |
| 79 | }); | |
| 80 | forgetWeek(); | |
| 81 | }; | |
| 82 | // Past the crew's share: 95% used, the crew may use 90%. | |
| 83 | measure(95); | |
| 84 | const on = await updateRepoSettings(app, alice, made.value, { | |
| 85 | autonomy: "autonomous", | |
| 86 | autonomyDirection: "Ship.", | |
| 87 | autonomySlots: 4, | |
| 88 | autonomyBy: alice.id, | |
| 89 | }); | |
| 90 | expect(on.ok).toBe(true); | |
| 91 | repo = getRepoById(app.db, made.value.id)!; | |
| 92 | }, TEST_MS); | |
| 93 | afterAll(() => stopStandInCrew(standIn), TEST_MS); | |
| 94 | ||
| 95 | const runOf = (issueId: string): Run | undefined => | |
| 96 | listRuns(standIn.app.db, repo.id, { kind: "agent", limit: 20 }).find( | |
| 97 | (r) => r.issueId === issueId, | |
| 98 | ); | |
| 99 | const logOf = (issueId: string): string => { | |
| 100 | const run = runOf(issueId); | |
| 101 | return run === undefined | |
| 102 | ? "" | |
| 103 | : readRunLog(standIn.app.config.dataDir, run.id).lines.join("\n"); | |
| 104 | }; | |
| 105 | const opened = (title: string) => { | |
| 106 | const issue = openIssue(standIn.app, standIn.alice, repo, { | |
| 107 | title, | |
| 108 | body: "Do it.", | |
| 109 | }); | |
| 110 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 111 | return issue.value; | |
| 112 | }; | |
| 113 | ||
| 114 | test( | |
| 115 | "past the cap, a run a person sent starts and the crew's own waits with today's sentence", | |
| 116 | async () => { | |
| 117 | const { app, alice, bot } = standIn; | |
| 118 | // The crew's own: the same assignment, made by the forge outside any person's request. | |
| 119 | const own = opened("The crew's pick"); | |
| 120 | assignIssue(app, alice, repo, own, bot); | |
| 121 | // A person's: made in their own request (a page, Talk, a token), as `core/acting_via.ts` says. | |
| 122 | const sent = opened("Sent by a person"); | |
| 123 | actingVia({ userId: alice.id, door: "session" }, () => | |
| 124 | assignIssue(app, alice, repo, sent, bot), | |
| 125 | ); | |
| 126 | expect(personActing(alice)).toBe(false); | |
| 127 | await until( | |
| 128 | () => runOf(sent.id) !== undefined && runOf(sent.id)!.state !== "queued", | |
| 129 | "the person's run to start", | |
| 130 | ); | |
| 131 | expect(runOf(sent.id)!.sent).toBe(true); | |
| 132 | await until( | |
| 133 | () => | |
| 134 | logOf(own.id).includes( | |
| 135 | "queued, not started: 95% of the week used, the crew may use up to", | |
| 136 | ), | |
| 137 | "the crew's own run to log its hold", | |
| 138 | ); | |
| 139 | expect([runOf(own.id)!.state, runOf(own.id)!.sent]).toEqual([ | |
| 140 | "queued", | |
| 141 | false, | |
| 142 | ]); | |
| 143 | }, | |
| 144 | TEST_MS, | |
| 145 | ); | |
| 146 | ||
| 147 | test("a review stays under the cap even of a pull a person opened: it waits at 95% of 90%", async () => { | |
| 148 | const { app, alice } = standIn; | |
| 149 | const pull = openIssue(app, alice, repo, { title: "A person's pull", body: "b" }); | |
| 150 | if (!pull.ok) throw new Error(String(pull.error)); | |
| 151 | app.db.run("UPDATE issue SET kind = 'pull' WHERE id = ?1", [pull.value.id]); | |
| 152 | const review = createRun(app.db, { repoId: repo.id, kind: "agent", issueId: pull.value.id, reason: "review", actorId: standIn.bot.id }); | |
| 153 | await until(() => readRunLog(app.config.dataDir, review.id).lines.join("\n").includes("queued, not started: 95% of the week used"), "the review to log its hold"); | |
| 154 | expect([runOf(pull.value.id)!.state, runOf(pull.value.id)!.sent]).toEqual(["queued", false]); | |
| 155 | }, TEST_MS); | |
| 156 | ||
| 157 | test( | |
| 158 | "at the provider's own limit both wait, the person's run too", | |
| 159 | async () => { | |
| 160 | const { app, alice, bot } = standIn; | |
| 161 | measure(100); | |
| 162 | const sent = opened("Sent at the limit"); | |
| 163 | actingVia({ userId: alice.id, door: "session" }, () => | |
| 164 | assignIssue(app, alice, repo, sent, bot), | |
| 165 | ); | |
| 166 | await until( | |
| 167 | () => | |
| 168 | logOf(sent.id).includes("queued, not started: 100% of the week used"), | |
| 169 | "the person's run to wait at the limit", | |
| 170 | ); | |
| 171 | expect(runOf(sent.id)!.state).toBe("queued"); | |
| 172 | measure(95); | |
| 173 | }, | |
| 174 | TEST_MS, | |
| 175 | ); | |
| 176 | ||
| 177 | test( | |
| 178 | "the terminal acts as its person: a key's work is theirs to send", | |
| 179 | async () => { | |
| 180 | const { app, alice } = standIn; | |
| 181 | const terminal = headlessTerminal(100, 30); | |
| 182 | const { app: tui } = createForgeApp( | |
| 183 | app, | |
| 184 | { | |
| 185 | sshUsername: alice.username, | |
| 186 | remoteAddress: "127.0.0.1", | |
| 187 | fingerprint: "SHA256:test", | |
| 188 | user: alice, | |
| 189 | }, | |
| 190 | { terminal, exitHooks: false }, | |
| 191 | ); | |
| 192 | tui.start(); | |
| 193 | let seen: boolean | undefined; | |
| 194 | const probe: Screen = { | |
| 195 | title: "probe", | |
| 196 | keys: [], | |
| 197 | render() {}, | |
| 198 | key: () => ((seen = personActing(alice)), true), | |
| 199 | }; | |
| 200 | tui.push(probe); | |
| 201 | terminal.feed("x"); | |
| 202 | await settleApp(tui); | |
| 203 | expect(seen).toBe(true); | |
| 204 | // Outside a key, nobody acts. | |
| 205 | expect(personActing(alice)).toBe(false); | |
| 206 | }, | |
| 207 | TEST_MS, | |
| 208 | ); |
new above
offline