toolroll 0.9.75 → 0.9.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/agent-fence.js +8 -0
  3. package/dist/approval-authority.js +6 -2
  4. package/dist/approval-rules-ui.d.ts +11 -0
  5. package/dist/approval-rules-ui.js +13 -1
  6. package/dist/batch-checks.js +3 -1
  7. package/dist/builder.js +1 -1
  8. package/dist/coding-workspace.js +7 -5
  9. package/dist/contracts/console-api.d.ts +1 -0
  10. package/dist/contracts/console-api.js +1 -0
  11. package/dist/deploy-gate.d.ts +3 -0
  12. package/dist/deploy-gate.js +3 -2
  13. package/dist/desktop-update.d.ts +1 -1
  14. package/dist/discover.js +6 -1
  15. package/dist/first-run.js +3 -1
  16. package/dist/git-incident.d.ts +49 -0
  17. package/dist/git-incident.js +69 -0
  18. package/dist/git-views.d.ts +20 -0
  19. package/dist/git-views.js +120 -0
  20. package/dist/grant.js +3 -1
  21. package/dist/operate.js +7 -1
  22. package/dist/planner.js +5 -1
  23. package/dist/project-knowledge.js +1 -1
  24. package/dist/project-learning.js +22 -2
  25. package/dist/pull-request-flow.js +4 -0
  26. package/dist/release-adapters.d.ts +24 -2
  27. package/dist/release-adapters.js +136 -10
  28. package/dist/release-authority.d.ts +46 -0
  29. package/dist/release-authority.js +64 -0
  30. package/dist/release-auto.d.ts +153 -0
  31. package/dist/release-auto.js +496 -0
  32. package/dist/release-cli.js +56 -2
  33. package/dist/release-coverage.js +1 -1
  34. package/dist/release.d.ts +105 -1
  35. package/dist/release.js +229 -9
  36. package/dist/repository-context.js +4 -6
  37. package/dist/result-follow-ups.js +3 -1
  38. package/dist/scope.d.ts +31 -2
  39. package/dist/scope.js +44 -3
  40. package/dist/scout.js +5 -1
  41. package/dist/server/render-tasks.d.ts +10 -0
  42. package/dist/server/render-tasks.js +23 -13
  43. package/dist/server/route-table.js +3 -0
  44. package/dist/server/settings.js +29 -2
  45. package/dist/server/task-views.js +8 -0
  46. package/dist/store.d.ts +1 -1
  47. package/dist/store.js +19 -4
  48. package/dist/task-base.js +1 -1
  49. package/dist/teardown-git.d.ts +38 -10
  50. package/dist/teardown-git.js +894 -108
  51. package/dist/test-infra.d.ts +26 -0
  52. package/dist/test-infra.js +321 -0
  53. package/dist/worktree.d.ts +4 -0
  54. package/dist/worktree.js +13 -7
  55. package/package.json +1 -1
package/dist/operate.js CHANGED
@@ -224,6 +224,8 @@ import { scout as scoutTask } from "./scout.js";
224
224
  import { profileDigestOf } from "./scope.js";
225
225
  import { PROVIDER_KEY_ENV, clearProviderKey, keyStatus, readAuthMode, readAuthModeStrict, readProviderKey, saveProviderKey, setAuthMode, verifyProviderKey, verdictWords } from "./keys.js";
226
226
  import { run, terminateLiveProviders, run as execRun } from "./exec.js";
227
+ import { hardenedGit } from "./teardown-git.js";
228
+ import { noticeGitIncidents } from "./git-incident.js";
227
229
  import { containmentNotice, containmentStatus, currentContainment, describeContainment, resolveContainment } from "./containment.js";
228
230
  import { readPulls } from "./pulls.js";
229
231
  import { startMaintenance } from "./maintenance.js";
@@ -2220,7 +2222,7 @@ function flowIoOf(context, pool, git, base) {
2220
2222
  replies: { fetch: context.flowTriggerIo?.fetch ?? fetch, dir: context.flowTriggerIo?.dir ?? dir,
2221
2223
  ...(context.flowTriggerIo?.mail === undefined ? {} : { mail: context.flowTriggerIo.mail }) },
2222
2224
  // Check and update steps run outside a model: an approved command in a fresh copy of the card's work, or a comment on the issue it came from.
2223
- steps: { gh: context.flowStepIo?.gh ?? run, git: context.flowStepIo?.git ?? git, shell: context.flowStepIo?.shell ?? run, fetch: context.flowStepIo?.fetch ?? fetch,
2225
+ steps: { gh: context.flowStepIo?.gh ?? run, git: context.flowStepIo?.git ?? (git === run ? hardenedGit(run) : git), shell: context.flowStepIo?.shell ?? run, fetch: context.flowStepIo?.fetch ?? fetch,
2224
2226
  dir: context.flowStepIo?.dir ?? dir, scratch: context.flowStepIo?.scratch ?? join(pool, "flow-checks"), base,
2225
2227
  ...(context.evidenceRoot === undefined ? {} : { evidenceRoot: context.evidenceRoot }) },
2226
2228
  ...(context.evidenceRoot === undefined ? {} : { evidenceRoot: context.evidenceRoot }),
@@ -3905,6 +3907,8 @@ async function decideCommand(positional, flags, context) {
3905
3907
  */
3906
3908
  async function startConsole(options) {
3907
3909
  const { context } = options;
3910
+ // A git incident (teardown-git.ts) raises an attention notification in this console's outbox.
3911
+ noticeGitIncidents(context.store, context.clock);
3908
3912
  const server = createDecisionServer({
3909
3913
  store: context.store,
3910
3914
  // Sign-in, integration and model checks, and the tidy-ups no page visit does (docs/plans/get-side-effects.md).
@@ -6041,6 +6045,8 @@ let staleStatusesRepaired = false;
6041
6045
  async function runWatchLoop(args) {
6042
6046
  const { context, flags, runner, token, repo, progress } = args;
6043
6047
  const { store } = context;
6048
+ // A git incident in this worker's builds raises an attention notification in its outbox (once per store).
6049
+ noticeGitIncidents(store, context.clock);
6044
6050
  const tickEveryMs = Number(text(flags, "tick-every") ?? 60_000);
6045
6051
  const bridgeEveryMs = Number(text(flags, "bridge-every") ?? 45_000);
6046
6052
  const reconcileEveryMs = Number(text(flags, "reconcile-every") ?? 5 * 60_000);
package/dist/planner.js CHANGED
@@ -20,6 +20,7 @@ import { unlinkSync } from "node:fs";
20
20
  import { homedir } from "node:os";
21
21
  import { join } from "node:path";
22
22
  import { run } from "./exec.js";
23
+ import { checkoutGit } from "./teardown-git.js";
23
24
  import { auditOf, isProviderId } from "./provider.js";
24
25
  import { currentClaim, heartbeat } from "./claim.js";
25
26
  import { heartbeat as runnerHeartbeat } from "./runner.js";
@@ -491,7 +492,10 @@ function draftedOutcome(root, runId, plan, repairRunId = null) {
491
492
  return { ok: true, drafted: { plan, artifact, repairRunId } };
492
493
  }
493
494
  export async function plan(store, request) {
494
- const { taskId, taskRef, runner, worktree, branch, now, agent, git = run, timeoutMs = DEFAULT_PLAN_TIMEOUT_MS, maxTurns = DEFAULT_PLAN_TURNS, } = request;
495
+ const { taskId, taskRef, runner, worktree, branch, now, agent, git: givenGit = run, timeoutMs = DEFAULT_PLAN_TIMEOUT_MS, maxTurns = DEFAULT_PLAN_TURNS, } = request;
496
+ // The plane's git on the plan's checkout (agent-written, and its repository shared with every build) runs hardened, against
497
+ // a frozen view of the repository's config and info (teardown-git.ts checkoutGit); an injected runner is used as given.
498
+ const git = givenGit === run ? checkoutGit(run, "build", { repo: store.getWorktree(worktree)?.repo ?? null }) : givenGit;
495
499
  // An ACP agent works with full access inside the fence: it builds and repairs, and is never handed a plan (routing refuses it first).
496
500
  if (request.provider !== undefined && isAcpProvider(request.provider)) {
497
501
  return { ok: false, kind: "failure", reason: "provider-unsupported", message: `${ACP_AGENTS[request.provider].name} builds and repairs only — configure the planner to claude, codex or gemini` };
@@ -35,7 +35,7 @@ CREATE TRIGGER IF NOT EXISTS knowledge_snapshot_no_update BEFORE UPDATE ON knowl
35
35
  CREATE TRIGGER IF NOT EXISTS knowledge_snapshot_no_delete BEFORE DELETE ON knowledge_snapshot BEGIN SELECT RAISE(ABORT,'Knowledge context is immutable'); END;
36
36
  `;
37
37
  const EMPTY = { instructions: '', references: [] };
38
- const git = (repo, args) => execFileSync('git', ['--no-optional-locks', '-C', repo, ...args], { encoding: 'utf8', maxBuffer: 200_000, stdio: ['ignore', 'pipe', 'pipe'] }).trimEnd();
38
+ const git = (repo, args) => execFileSync('git', ['--no-optional-locks', '--no-lazy-fetch', '-c', 'core.fsmonitor=false', '-c', 'core.hooksPath=/dev/null', '-C', repo, ...args], { encoding: 'utf8', maxBuffer: 200_000, stdio: ['ignore', 'pipe', 'pipe'] }).trimEnd();
39
39
  function clean(value, bytes) {
40
40
  if (Buffer.byteLength(value) > bytes || /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f\ufffd]/u.test(value))
41
41
  throw Error('This text is too long or is not readable text.');
@@ -1,5 +1,6 @@
1
1
  /** Optional, project-local advice. Core review and approval never depend on it. */
2
2
  import { inReadOnlyRequest } from "./read-only-request.js";
3
+ import { checkoutGitSync } from './teardown-git.js';
3
4
  import { createHash } from 'node:crypto';
4
5
  import { execFileSync } from 'node:child_process';
5
6
  import { closeSync, constants as fsConstants, existsSync, fstatSync, lstatSync, openSync, readSync, realpathSync, statSync } from 'node:fs';
@@ -52,7 +53,8 @@ const git = (repo, args, seen) => {
52
53
  if (known !== undefined)
53
54
  return known;
54
55
  // Bounded, and no hook or fsmonitor program: it also runs from the console's background pass.
55
- const out = execFileSync('git', ['--no-optional-locks', '-c', 'core.fsmonitor=false', '-c', 'core.hooksPath=/dev/null', '-C', repo, ...args], { encoding: 'utf8', maxBuffer: 2 * 1024 * 1024, timeout: 10_000, stdio: ['ignore', 'pipe', 'pipe'] }).trimEnd();
56
+ // No lazy fetch either: a partial clone's fetch runs whatever transport command the shared config names.
57
+ const out = execFileSync('git', ['--no-optional-locks', '--no-lazy-fetch', '-c', 'core.fsmonitor=false', '-c', 'core.hooksPath=/dev/null', '-C', repo, ...args], { encoding: 'utf8', maxBuffer: 2 * 1024 * 1024, timeout: 10_000, stdio: ['ignore', 'pipe', 'pipe'] }).trimEnd();
56
58
  seen?.set(key, out);
57
59
  return out;
58
60
  };
@@ -542,6 +544,24 @@ export function learningContext(store, root, runId, phase, now = new Date(), val
542
544
  // advice may inform its proposal; only an approval can authorize a build.
543
545
  const unscopedPlan = phase === 'plan' && scope === null && !run.scopeDigest;
544
546
  const eligible = [];
547
+ // The checkout's status, read once for every lesson (one command on one checkout): a hardened read costs a
548
+ // private view and several git processes, and this runs inside the transaction. A failed read fails every lesson
549
+ // that reaches it, as each read alone would have.
550
+ let status = null;
551
+ const statusLines = (cwd) => {
552
+ status ??= (() => {
553
+ // A build's checkout: status runs filters, so the hardened read (teardown-git.ts checkoutGitSync).
554
+ try {
555
+ return { lines: checkoutGitSync(cwd, ['status', '--porcelain', '--untracked-files=all'], { timeoutMs: 10_000, maxBuffer: 2 * 1024 * 1024 }).trimEnd().split('\n') };
556
+ }
557
+ catch (error) {
558
+ return { error };
559
+ }
560
+ })();
561
+ if ('error' in status)
562
+ throw status.error;
563
+ return status.lines;
564
+ };
545
565
  if (policy && (paths.length > 0 || unscopedPlan)) {
546
566
  for (const row of store.handle.prepare("SELECT * FROM project_lesson WHERE repo=? AND identity IN (SELECT value FROM json_each(?)) AND status='adopted' ORDER BY id DESC LIMIT 100").all(repo, carried)) {
547
567
  try {
@@ -552,7 +572,7 @@ export function learningContext(store, root, runId, phase, now = new Date(), val
552
572
  if (l.payload.environment !== environmentFingerprint(store, repo) || learningFingerprint(repo, head, l.payload.paths, seen) !== l.payload.fingerprint)
553
573
  continue;
554
574
  const cwd = run.worktree ?? repo;
555
- const dirty = git(cwd, ['status', '--porcelain', '--untracked-files=all'], seen).split('\n').some(line => { const p = line.slice(3); return configPath(p) || l.payload.paths.includes(p); });
575
+ const dirty = statusLines(cwd).some(line => { const p = line.slice(3); return configPath(p) || l.payload.paths.includes(p); });
556
576
  if (!dirty)
557
577
  eligible.push(l);
558
578
  }
@@ -25,6 +25,7 @@ import { authenticateApprover, personProof } from "./scope.js";
25
25
  import { BRANCH_PREFIX, headWithin } from "./names.js";
26
26
  import { NIGHTLY_CHECK_ONLY, RELEASE_MERGES_ONLY, nightlyTaskRef, releaseTaskRef } from "./store.js";
27
27
  import { belongsToRelease } from "./release-authority.js";
28
+ import { autoReleaseSealed } from "./release-auto.js";
28
29
  import { fullCheckGate, requestFollowUpChecks } from "./result-follow-ups.js";
29
30
  export const MERGE_METHODS = ["squash", "merge", "rebase"];
30
31
  /** CI revisions one task may file across its revisions before a person is asked instead. */
@@ -402,6 +403,9 @@ export async function mergePullRequest(store, input) {
402
403
  // the merge at its lease's epoch and checks the exact pull request: never here, by hand or automatically.
403
404
  if (mergedTask !== null && belongsToRelease(store, mergedTask))
404
405
  return refuse("release", "This result belongs to a release; it merges only through `toolroll release`.");
406
+ // A release task the plane approved (or is checking) automatically (auto-approve-green.md) never merges here either.
407
+ if (mergedTask !== null && autoReleaseSealed(store, mergedTask))
408
+ return refuse("release", "This result was approved automatically for a release; it merges only through `toolroll release`.");
405
409
  const publication = store.publicationForRun(input.runId);
406
410
  const follow = publication === null ? null : followOf(store, publication.id);
407
411
  if (publication === null || follow === null || publication.prNumber === null)
@@ -4,8 +4,9 @@
4
4
  * Homebrew tap through `gh`. Each call is bounded; none asks for or stores a credential — `gh`, npm and Toolroll use
5
5
  * the sign-ins this machine already has.
6
6
  */
7
- import type { GateState, Installed, ReleaseAdapters } from "./release.js";
7
+ import type { CheckRun, GateState, Installed, ReleaseAdapters } from "./release.js";
8
8
  import { Store } from "./store.js";
9
+ import { type CiObservation } from "./release-auto.js";
9
10
  import { type BuildRunner } from "./release-carry.js";
10
11
  import { type ReleaseRepo, type ReleaseTools } from "./release-git.js";
11
12
  export { RELEASE_PATH, RELEASE_REMOTE, releaseConfigProblem, releaseGitArgs, releaseGitEnv, releaseRemote, releaseTools, type ReleaseRepo, type ReleaseTools } from "./release-git.js";
@@ -81,4 +82,25 @@ export declare function bumpFormula(text: string, url: string, sha256: string):
81
82
  * The release's authority record through a store (release-once.md): what `toolroll release` reads and writes for its
82
83
  * lease, its carries and its operations. `open` hands a store for each call and `done` lets it go.
83
84
  */
84
- export declare function storeReleaseAdapter(open: () => Store, done: (store: Store) => void, now: () => Date, runner?: BuildRunner): NonNullable<ReleaseAdapters["release"]>;
85
+ export declare function storeReleaseAdapter(open: () => Store, done: (store: Store) => void, now: () => Date, runner?: BuildRunner,
86
+ /** Releases that approve themselves (auto-approve-green.md): the plane's own look at CI on a commit, and where the
87
+ * check receipts are kept. Absent: no automatic approval, every release asks a person. */
88
+ auto?: AutoObservers): NonNullable<ReleaseAdapters["release"]>;
89
+ /** What the plane itself reads for an automatic release approval. */
90
+ export type AutoObservers = {
91
+ observe: (sha: string) => Promise<CiObservation>;
92
+ /** GitHub's merge base of `sha` with the branch `base` of the recorded repository. */
93
+ mergeBase: (base: string, sha: string) => Promise<string>;
94
+ evidenceRoot: string;
95
+ };
96
+ /**
97
+ * Every check run GitHub holds for one commit — every attempt, not only the latest (`filter=all`; GitHub's default
98
+ * lists only the latest of each), every page — with its id, app, check suite and the commit it is filed under, oldest
99
+ * first. A row that doesn't parse stops the read: a partial answer is never taken for the whole.
100
+ */
101
+ export declare function readCheckRuns(run: Exec, gh: string, repo: string, sha: string): Promise<CheckRun[]>;
102
+ /** GitHub lists check runs from at most the latest 1000 check suites of a commit: the history is whole only below that. */
103
+ export declare const CHECK_SUITE_CEILING = 1000;
104
+ export declare function readSuitesComplete(run: Exec, gh: string, repo: string, sha: string): Promise<boolean>;
105
+ /** GitHub's merge base of `sha` with branch `base` (the compare API), read by the plane. */
106
+ export declare function readMergeBase(run: Exec, gh: string, repo: string, base: string, sha: string): Promise<string>;
@@ -19,7 +19,8 @@ import { BEFORE_SWAP, gateOwner } from "./release-gate.js";
19
19
  import { findCliLinks, switchCliLinks } from "./release-links.js";
20
20
  import { databasePath, finishReleaseOperation, requireFreshReleaseApproval, openStore, releaseAuthorityOfTask, releaseCarries, releaseOperations, renewReleaseLease, reserveRelease, runReleaseOperation, Store, releaseIssuerGone, takeReleaseLease, } from "./store.js";
21
21
  import { acquireDeploymentLock } from "./deploy-lock.js";
22
- import { liveReleaseOf } from "./scope.js";
22
+ import { autoReleaseApprovalOf, liveReleaseOf } from "./scope.js";
23
+ import { autoPublishHeld, holdAutoPublish, authorizeAutoCheck, giveUpAutoApproval, noteAutoCheckFailed, noteAutoRelease, releaseAutoApproves, sealAutoGreen } from "./release-auto.js";
23
24
  import { proveCarry } from "./release-carry.js";
24
25
  import { randomBytes, randomUUID } from "node:crypto";
25
26
  import { deploymentGate } from "./deploy-gate.js";
@@ -236,6 +237,14 @@ export function releaseAdapters(options) {
236
237
  }
237
238
  };
238
239
  const refused = (answer) => `${String(answer["message"] ?? answer["reason"] ?? "refused")}`;
240
+ /** gh for every CI and merge-base read (auto-approve-green.md): found only in the system's own folders, never the
241
+ * caller's PATH (a test hands its own). */
242
+ const ciGh = options.tools?.gh ?? releaseTools("").gh;
243
+ const observeCi = async (sha) => {
244
+ const observedAt = (options.now ?? (() => new Date()))().toISOString();
245
+ const runs = (await readCheckRuns(run, ciGh, releaseRepo().slug, sha)).map(one => ({ id: one.id ?? null, name: one.name, status: one.status, conclusion: one.conclusion, app: one.app ?? null, suite: one.suite ?? null, head: one.head ?? null, workflow: one.workflow ?? null }));
246
+ return { head: sha, observedAt, runs, complete: await readSuitesComplete(run, ciGh, releaseRepo().slug, sha) };
247
+ };
239
248
  const database = options.database ?? databasePath(process.env, homedir());
240
249
  let recorded = options.releaseRepo ?? null;
241
250
  /** The recorded repository, read once (chat-management.md §0a.2). */
@@ -284,7 +293,7 @@ export function releaseAdapters(options) {
284
293
  return { ok: false, message: "The plane's database isn't here, so the candidate can't be proven." };
285
294
  const db = new (sqlite().DatabaseSync)(database, { readOnly: true });
286
295
  try {
287
- return deploymentGate(new Store(db), { runId, evidenceRoot: join(dirname(database), "evidence"), now: (options.now ?? (() => new Date()))() });
296
+ return deploymentGate(new Store(db), { runId, evidenceRoot: join(dirname(database), "evidence"), now: (options.now ?? (() => new Date()))(), releaseFlow: true });
288
297
  }
289
298
  catch (error) {
290
299
  return { ok: false, message: `The candidate can't be proven: ${error.message}` };
@@ -360,7 +369,7 @@ export function releaseAdapters(options) {
360
369
  rmSync(extracted.dir, { recursive: true, force: true });
361
370
  }
362
371
  };
363
- const release = storeReleaseAdapter(() => openStore(database), store => store.close(), options.now ?? (() => new Date()));
372
+ const release = storeReleaseAdapter(() => openStore(database), store => store.close(), options.now ?? (() => new Date()), undefined, { observe: sha => observeCi(sha), mergeBase: (base, sha) => readMergeBase(run, ciGh, releaseRepo().slug, base, sha), evidenceRoot: join(dirname(database), "evidence") });
364
373
  return {
365
374
  ...(existsSync(database) ? { release } : {}),
366
375
  now: options.now ?? (() => new Date()),
@@ -559,12 +568,9 @@ export function releaseAdapters(options) {
559
568
  }));
560
569
  return prs.find(one => one.state === "open") ?? prs.filter(one => one.state === "merged").sort((a, b) => b.number - a.number)[0] ?? null;
561
570
  },
562
- async checks(repo, sha) {
563
- const out = await must(gh("api", "--paginate", `repos/${repo}/commits/${sha}/check-runs?per_page=100`, "--jq", ".check_runs[] | {id, name, status, conclusion, app: .app.slug}"), "gh api check-runs");
564
- const runs = out.split("\n").filter(Boolean).map(line => object(JSON.parse(line)));
565
- // Oldest first, so a rerun's attempt is the last one listed for its name.
566
- return runs.sort((a, b) => Number(a["id"]) - Number(b["id"])).map((one) => ({ name: String(one["name"]), status: String(one["status"]), conclusion: textOr(one["conclusion"]), app: textOr(one["app"]) }));
567
- },
571
+ // Every attempt (filter=all), with its app, check suite and commit: the release judges each job by its latest attempt.
572
+ async checks(repo, sha) { return readCheckRuns(run, ciGh, repo, sha); },
573
+ async complete(repo, sha) { return readSuitesComplete(run, ciGh, repo, sha); },
568
574
  async merge(repo, pr, head, message) {
569
575
  // The squash commit's message lists every commit that landed after the approval (release-once.md).
570
576
  const merged = object(await ghJson("gh api pulls merge", "api", "-X", "PUT", `repos/${repo}/pulls/${pr}/merge`, "-f", "merge_method=squash", "-f", `sha=${head}`, ...(message === undefined ? [] : ["-f", `commit_message=${message}`])));
@@ -728,7 +734,10 @@ function personAnswerOf(value) {
728
734
  * The release's authority record through a store (release-once.md): what `toolroll release` reads and writes for its
729
735
  * lease, its carries and its operations. `open` hands a store for each call and `done` lets it go.
730
736
  */
731
- export function storeReleaseAdapter(open, done, now, runner) {
737
+ export function storeReleaseAdapter(open, done, now, runner,
738
+ /** Releases that approve themselves (auto-approve-green.md): the plane's own look at CI on a commit, and where the
739
+ * check receipts are kept. Absent: no automatic approval, every release asks a person. */
740
+ auto) {
732
741
  const withStore = (body) => { const store = open(); try {
733
742
  return body(store);
734
743
  }
@@ -736,6 +745,7 @@ export function storeReleaseAdapter(open, done, now, runner) {
736
745
  done(store);
737
746
  } };
738
747
  return {
748
+ ...(auto === undefined ? {} : { auto: autoReleaseAdapter(withStore, now, auto) }),
739
749
  async authority(taskId) {
740
750
  return withStore(store => {
741
751
  const found = releaseAuthorityOfTask(store, taskId);
@@ -777,3 +787,119 @@ export function storeReleaseAdapter(open, done, now, runner) {
777
787
  },
778
788
  };
779
789
  }
790
+ /** The plane's automatic release approval through a store (release-auto.ts): what `toolroll release` asks. It hands the
791
+ * plane ids only; the plane reads CI itself (`observe`) right before it seals. */
792
+ function autoReleaseAdapter(withStore, now, auto) {
793
+ return {
794
+ async setting(taskId) {
795
+ return withStore(store => { try {
796
+ const ref = store.lookupRef(taskId);
797
+ return ref?.repo != null && releaseAutoApproves(store, ref.repo);
798
+ }
799
+ catch {
800
+ return false;
801
+ } });
802
+ },
803
+ async authorizeCheck(input) {
804
+ // The comparison base for what the release changes: GitHub's own merge base of the candidate with the release's
805
+ // base branch (its signed terms), read by the plane — never a task branch or a build's recorded base.
806
+ const branch = withStore(store => store.getScope(input.taskId)?.releaseTerms?.base ?? null);
807
+ if (branch === null)
808
+ return { ok: false, why: "only a release task is approved automatically" };
809
+ let base;
810
+ try {
811
+ base = await auto.mergeBase(branch, input.candidate);
812
+ }
813
+ catch (error) {
814
+ return { ok: false, why: `GitHub couldn't say where ${input.candidate.slice(0, 7)} left ${branch} (${String(error?.message ?? error).slice(0, 160)})` };
815
+ }
816
+ return withStore(store => { const answer = authorizeAutoCheck(store, { ...input, base, now: now() }); return answer.ok ? { ok: true } : answer; });
817
+ },
818
+ async checking(taskId) {
819
+ return withStore(store => { const answer = autoReleaseApprovalOf(store, taskId, "auto-check"); return answer.ok ? { ok: true } : { ok: false, why: answer.why }; });
820
+ },
821
+ async approve(input) {
822
+ let ci = null, problem = "";
823
+ // A short network failure is asked again a few times; GitHub not answering is never a pass.
824
+ for (let attempt = 0; attempt < 3 && ci === null; attempt++) {
825
+ try {
826
+ ci = await auto.observe(input.candidate);
827
+ }
828
+ catch (error) {
829
+ problem = String(error?.message ?? error).slice(0, 200);
830
+ }
831
+ }
832
+ if (ci === null) {
833
+ const why = `GitHub CI on ${input.candidate.slice(0, 7)} couldn't be read (${problem})`;
834
+ withStore(store => giveUpAutoApproval(store, { taskId: input.taskId, why, now: now() }));
835
+ return { ok: false, why };
836
+ }
837
+ const observed = ci;
838
+ return withStore(store => sealAutoGreen(store, { ...input, ci: observed, evidenceRoot: auto.evidenceRoot, now: now() }));
839
+ },
840
+ async giveUp(taskId, why) { withStore(store => giveUpAutoApproval(store, { taskId, why, now: now() })); },
841
+ async checkFailed(taskId, why) { withStore(store => noteAutoCheckFailed(store, { taskId, why, now: now() })); },
842
+ async released(taskId, tag) { withStore(store => noteAutoRelease(store, { taskId, tag, now: now() })); },
843
+ async holdPublish(taskId, why) { withStore(store => holdAutoPublish(store, { taskId, why, now: now() })); },
844
+ async publishHeld(taskId) { return withStore(store => autoPublishHeld(store, taskId)); },
845
+ };
846
+ }
847
+ /** The environment every CI read runs gh with: no caller PATH, host, config folder or proxy override. */
848
+ function ciEnv() {
849
+ const kept = Object.fromEntries(["HOME", "LANG", "LC_ALL", "TMPDIR", "USER", "LOGNAME", "GH_TOKEN", "GITHUB_TOKEN"].flatMap(name => process.env[name] === undefined ? [] : [[name, process.env[name]]]));
850
+ return { ...kept, PATH: "/usr/bin:/bin", GH_HOST: "github.com", GH_PROMPT_DISABLED: "1", GH_NO_UPDATE_NOTIFIER: "1" };
851
+ }
852
+ /**
853
+ * Every check run GitHub holds for one commit — every attempt, not only the latest (`filter=all`; GitHub's default
854
+ * lists only the latest of each), every page — with its id, app, check suite and the commit it is filed under, oldest
855
+ * first. A row that doesn't parse stops the read: a partial answer is never taken for the whole.
856
+ */
857
+ export async function readCheckRuns(run, gh, repo, sha) {
858
+ if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo) || !/^[0-9a-f]{40}$/.test(sha))
859
+ throw new Error("not a repository and commit GitHub can be asked about");
860
+ const out = await must(run(gh, ["api", "--hostname", "github.com", "--paginate", `repos/${repo}/commits/${sha}/check-runs?filter=all&per_page=100`,
861
+ "--jq", ".check_runs[] | {id, name, status, conclusion, app: .app.slug, suite: .check_suite.id, head: .head_sha}"], { cwd: "/", env: ciEnv() }), "gh api check-runs");
862
+ // Which workflow each check suite is a run of (GitHub's workflow-run API): a newer run of a workflow supersedes its
863
+ // older runs on this commit (release.ts prChecksVerdict).
864
+ const flows = await must(run(gh, ["api", "--hostname", "github.com", "--paginate", `repos/${repo}/actions/runs?head_sha=${sha}&per_page=100`,
865
+ "--jq", ".workflow_runs[] | {suite: .check_suite_id, workflow: .workflow_id, head: .head_sha}"], { cwd: "/", env: ciEnv() }), "gh api actions/runs");
866
+ const workflowOf = new Map();
867
+ for (const line of flows.split("\n").filter(Boolean)) {
868
+ const one = object(JSON.parse(line));
869
+ if (!Number.isSafeInteger(one["suite"]) || !Number.isSafeInteger(one["workflow"]) || one["head"] !== sha)
870
+ throw new Error("GitHub answered a workflow run this can't read");
871
+ const known = workflowOf.get(Number(one["suite"]));
872
+ if (known !== undefined && known !== Number(one["workflow"]))
873
+ throw new Error("GitHub named two workflows for one check suite");
874
+ workflowOf.set(Number(one["suite"]), Number(one["workflow"]));
875
+ }
876
+ const runs = out.split("\n").filter(Boolean).map(line => {
877
+ const one = object(JSON.parse(line));
878
+ if (!Number.isSafeInteger(one["id"]) || typeof one["name"] !== "string" || typeof one["status"] !== "string")
879
+ throw new Error("GitHub answered a check run this can't read");
880
+ const suite = Number.isSafeInteger(one["suite"]) ? Number(one["suite"]) : null;
881
+ return { id: Number(one["id"]), name: one["name"], status: one["status"], conclusion: textOr(one["conclusion"]), app: textOr(one["app"]),
882
+ suite, head: textOr(one["head"]), workflow: suite === null ? null : workflowOf.get(suite) ?? null };
883
+ });
884
+ if (new Set(runs.map(one => one.id)).size !== runs.length)
885
+ throw new Error("GitHub answered the same check run twice");
886
+ return runs.sort((a, b) => a.id - b.id);
887
+ }
888
+ /** GitHub lists check runs from at most the latest 1000 check suites of a commit: the history is whole only below that. */
889
+ export const CHECK_SUITE_CEILING = 1000;
890
+ export async function readSuitesComplete(run, gh, repo, sha) {
891
+ if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo) || !/^[0-9a-f]{40}$/.test(sha))
892
+ return false;
893
+ const out = await run(gh, ["api", "--hostname", "github.com", `repos/${repo}/commits/${sha}/check-suites?per_page=1`, "--jq", ".total_count"], { cwd: "/", env: ciEnv() });
894
+ const total = Number(out.stdout.trim());
895
+ return out.code === 0 && out.stdout.trim() !== "" && Number.isSafeInteger(total) && total >= 0 && total < CHECK_SUITE_CEILING;
896
+ }
897
+ /** GitHub's merge base of `sha` with branch `base` (the compare API), read by the plane. */
898
+ export async function readMergeBase(run, gh, repo, base, sha) {
899
+ if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo) || !/^[0-9a-f]{40}$/.test(sha) || !/^[A-Za-z0-9._/-]+$/.test(base) || base.includes(".."))
900
+ throw new Error("not a repository, branch and commit GitHub can be asked about");
901
+ const out = (await must(run(gh, ["api", "--hostname", "github.com", `repos/${repo}/compare/${base}...${sha}`, "--jq", ".merge_base_commit.sha"], { cwd: "/", env: ciEnv() }), "gh api compare")).trim();
902
+ if (!/^[0-9a-f]{40}$/.test(out))
903
+ throw new Error("GitHub named no merge base");
904
+ return out;
905
+ }
@@ -116,3 +116,49 @@ export declare function outstandingReleaseOperations(store: Store, authority: nu
116
116
  * superseded by the new one — but never while one of its operations is unsettled, and never once its merge started.
117
117
  */
118
118
  export declare function releaseSealProblem(store: Store, terms: ReleaseTerms): string | null;
119
+ /** "Release approval: Ask me / Approve automatically when checks pass": a project setting, newest choice wins (a ledger
120
+ * row each); `ask` by default. Turning it on is a person's credential act (a password typed now); off is one click. */
121
+ export declare const RELEASE_APPROVAL_ACTION = "release approval changed";
122
+ /** The plane's record of what proved an automatic seal (`auto-check` or `auto-green`): one ledger row per seal, its
123
+ * id named by the seal's `mode_digest` as `auto:<id>`. Only release-auto.ts writes it. */
124
+ export declare const AUTO_EVIDENCE_ACTION = "release auto-approved";
125
+ /** An automatic seal taken back for good (the setting lowered, a refusal): its outcome names the seal's `auto:<id>`, and
126
+ * that evidence never stands again. */
127
+ export declare const AUTO_WITHDRAWN_ACTION = "release auto-approval withdrawn";
128
+ export type ReleaseApprovalSetting = {
129
+ auto: boolean;
130
+ setBy: string | null;
131
+ at: string | null;
132
+ };
133
+ /** Read live at every use. Fail closed: a missing, unreadable or unknown row is `ask`. */
134
+ export declare function releaseApprovalSetting(store: Store, repo: string): ReleaseApprovalSetting;
135
+ export declare const releaseAutoApproves: (store: Store, repo: string) => boolean;
136
+ /** SQL: the newest setting row for `repo` (an SQL expression) says `auto`. */
137
+ export declare const releaseAutoSql: (repo: string) => string;
138
+ /**
139
+ * SQL over a `task_scope` alias: its automatic seal (`auto-check` or `auto-green`) stands on its own evidence row — this
140
+ * task, this kind, `mode_digest = 'auto:<row id>'`, the row's digest and candidate exactly the sealed ones — on a release
141
+ * task (a candidate and release terms signed in), while the project's setting is on. The approver's standing is read
142
+ * beside it (approval-authority.ts).
143
+ */
144
+ export declare const autoSealSql: (scope: string, repo: string) => string;
145
+ /** The evidence an automatic seal names, read strictly; null when the row is missing or doesn't match the seal. */
146
+ export type AutoEvidence = {
147
+ id: number;
148
+ kind: "auto-check" | "auto-green";
149
+ taskId: string;
150
+ digest: string;
151
+ candidate: string;
152
+ at: string;
153
+ by: string;
154
+ /** auto-green only: the build whose check passed, its receipt, the CI check runs observed green and when, the base. */
155
+ run: number | null;
156
+ receipt: string | null;
157
+ ci: {
158
+ id: number;
159
+ name: string;
160
+ }[];
161
+ observedAt: string | null;
162
+ base: string | null;
163
+ };
164
+ export declare function autoEvidenceOf(store: Store, taskId: string, modeDigest: string | null | undefined, kind: "auto-check" | "auto-green"): AutoEvidence | null;
@@ -133,3 +133,67 @@ export function releaseSealProblem(store, terms) {
133
133
  }
134
134
  return null;
135
135
  }
136
+ // ------------------------------------------------------------------ automatic release approval (auto-approve-green.md)
137
+ /** "Release approval: Ask me / Approve automatically when checks pass": a project setting, newest choice wins (a ledger
138
+ * row each); `ask` by default. Turning it on is a person's credential act (a password typed now); off is one click. */
139
+ export const RELEASE_APPROVAL_ACTION = "release approval changed";
140
+ /** The plane's record of what proved an automatic seal (`auto-check` or `auto-green`): one ledger row per seal, its
141
+ * id named by the seal's `mode_digest` as `auto:<id>`. Only release-auto.ts writes it. */
142
+ export const AUTO_EVIDENCE_ACTION = "release auto-approved";
143
+ /** An automatic seal taken back for good (the setting lowered, a refusal): its outcome names the seal's `auto:<id>`, and
144
+ * that evidence never stands again. */
145
+ export const AUTO_WITHDRAWN_ACTION = "release auto-approval withdrawn";
146
+ /** Read live at every use. Fail closed: a missing, unreadable or unknown row is `ask`. */
147
+ export function releaseApprovalSetting(store, repo) {
148
+ let row;
149
+ try {
150
+ row = store.handle.prepare("SELECT actor, at, outcome FROM action_ledger WHERE repo = ? AND task_id IS NULL AND action = ? ORDER BY id DESC LIMIT 1").get(repo, RELEASE_APPROVAL_ACTION);
151
+ }
152
+ catch {
153
+ return { auto: false, setBy: null, at: null };
154
+ }
155
+ if (row === undefined || (row["outcome"] !== "auto" && row["outcome"] !== "ask"))
156
+ return { auto: false, setBy: null, at: null };
157
+ return { auto: row["outcome"] === "auto", setBy: String(row["actor"]), at: String(row["at"]) };
158
+ }
159
+ export const releaseAutoApproves = (store, repo) => releaseApprovalSetting(store, repo).auto;
160
+ /** SQL: the newest setting row for `repo` (an SQL expression) says `auto`. */
161
+ export const releaseAutoSql = (repo) => `COALESCE((SELECT ra_set.outcome = 'auto' FROM action_ledger ra_set WHERE ra_set.repo = ${repo} AND ra_set.task_id IS NULL
162
+ AND ra_set.action = '${RELEASE_APPROVAL_ACTION}' ORDER BY ra_set.id DESC LIMIT 1), 0)`;
163
+ /**
164
+ * SQL over a `task_scope` alias: its automatic seal (`auto-check` or `auto-green`) stands on its own evidence row — this
165
+ * task, this kind, `mode_digest = 'auto:<row id>'`, the row's digest and candidate exactly the sealed ones — on a release
166
+ * task (a candidate and release terms signed in), while the project's setting is on. The approver's standing is read
167
+ * beside it (approval-authority.ts).
168
+ */
169
+ export const autoSealSql = (scope, repo) => `(${scope}.candidate IS NOT NULL AND ${scope}.release_terms_json IS NOT NULL AND ${releaseAutoSql(repo)}
170
+ AND EXISTS (SELECT 1 FROM action_ledger ae WHERE ae.task_id = ${scope}.task_id AND ae.action = '${AUTO_EVIDENCE_ACTION}'
171
+ AND ae.outcome = ${scope}.approval_basis AND ${scope}.mode_digest = 'auto:' || ae.id AND json_valid(ae.detail)
172
+ AND json_extract(ae.detail, '$.v') = 1 AND json_extract(ae.detail, '$.digest') = ${scope}.approved_digest
173
+ AND json_extract(ae.detail, '$.candidate') = ${scope}.candidate)
174
+ AND NOT EXISTS (SELECT 1 FROM action_ledger aw WHERE aw.task_id = ${scope}.task_id AND aw.action = '${AUTO_WITHDRAWN_ACTION}' AND aw.outcome = ${scope}.mode_digest))`;
175
+ export function autoEvidenceOf(store, taskId, modeDigest, kind) {
176
+ const match = /^auto:([1-9][0-9]{0,15})$/.exec(modeDigest ?? "");
177
+ if (match === null)
178
+ return null;
179
+ const row = store.handle.prepare("SELECT id, at, actor, task_id, outcome, detail FROM action_ledger WHERE id = ? AND action = ?").get(Number(match[1]), AUTO_EVIDENCE_ACTION);
180
+ if (row === undefined || row["task_id"] !== taskId || row["outcome"] !== kind)
181
+ return null;
182
+ if (store.handle.prepare("SELECT 1 AS hit FROM action_ledger WHERE task_id = ? AND action = ? AND outcome = ?").get(taskId, AUTO_WITHDRAWN_ACTION, `auto:${match[1]}`) !== undefined)
183
+ return null;
184
+ let detail;
185
+ try {
186
+ detail = JSON.parse(String(row["detail"]));
187
+ }
188
+ catch {
189
+ return null;
190
+ }
191
+ if (detail === null || typeof detail !== "object" || detail["v"] !== 1 || typeof detail["digest"] !== "string" || typeof detail["candidate"] !== "string")
192
+ return null;
193
+ const ci = Array.isArray(detail["ci"]) ? detail["ci"].filter((one) => one !== null && typeof one === "object" && Number.isSafeInteger(one.id) && typeof one.name === "string") : [];
194
+ const textOr = (value) => typeof value === "string" ? value : null;
195
+ return {
196
+ id: Number(row["id"]), kind, taskId, digest: detail["digest"], candidate: detail["candidate"], at: String(row["at"]), by: String(detail["by"] ?? row["actor"]),
197
+ run: Number.isSafeInteger(detail["run"]) ? Number(detail["run"]) : null, receipt: textOr(detail["receipt"]), ci, observedAt: textOr(detail["observedAt"]), base: textOr(detail["base"]),
198
+ };
199
+ }