Public: anyone can read this; the crew can change it.

Trusted sources fold in by themselves, in real time; everyone else waits for review #8954

Parked opened this in answer to
Closed without merging

mason/forge:mason/8937-trusted-sources-fold-in-by-themselves-in → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
20 files changed +979 −8
docs/design/audit.md +1
@@ -100,6 +100,7 @@ Machine routes are not pages and have no entry: `/audit.csv`,
100100 - `/settings/capacity` — `pages/capacity.ts` · how many runs and checks go at once · `/admin/limits`, `/settings/usage` · none seen.
101101 - `/settings/usage` — `pages/usage.ts` · route patterns by use, unused first · `/settings/telemetry`, `/board/returns` · none seen. Not about cost, despite the name.
102102 - `/settings/telemetry` — `pages/telemetry.ts` · which agents may read telemetry · `/settings/crew` (agent grants) · none seen.
103 - `/settings/trusted` — `pages/trusted.ts` · who may fold in without review, until when, and the kill switch · `/review`, `/settings/crew` · none seen.
103104 - `/settings/spend` — `pages/spend-tab.ts` · what the crew's runs spent, by credential, app, agent, issue and run kind · `/settings/subscriptions`, `/board/returns` · none seen. First tab of Usage (#2692).
104105 - `/settings/resilience` — `pages/domains.ts` · failure domains and their last drill · `/resilience` · none seen.
105106 - `/settings/backups` — `pages/backups.ts` (621 lines) · backups and restore · `/settings/resilience`, `/releases` · none seen.
docs/features/trusted-sources.md added +7
@@ -0,0 +1,7 @@
1 # Trusted sources (#8937)
2
3 Settings → Trusted sources lists who folds in without a person's review: an account **on a named forge**, until a date, set by whom. The same name on another forge is a stranger. Remove is one click; past its date an entry falls to review by itself, with a `trust.expiring` note 3 days before. Every change is an event (`trust.set`, `trust.removed`).
4
5 A trusted source's pull (recorded in `pull_source`, from a followed upstream's address) is admitted by the autonomy tick like the crew's own: its check, the boot proof and the walls still stand, then it lands by true merge with `Landed-from` / `Landed-via` trailers. Touching secrets, credentials, permissions, the contract or release machinery (`src/crew/fold_walls.ts`) still waits for a person. Everyone else is a guest: nothing runs with secrets and the pull waits on `/review` with Approve and Decline.
6
7 Work whose `Landed-via` already names this forge is not taken again, so forges following each other in a ring do not re-land it. The kill switch ("Stop all folding in") ends all of it at once; "Revert landings" opens a revert pull for everything one source landed.
src/actions/pulls.ts +10 −4
@@ -80,6 +80,7 @@ import {
8080 import { canWrite } from "../core/repos.ts";
8181 import { refreshPullHead as refreshHead } from "../core/following.ts";
8282 import { carryBranchThrough } from "../origins/push-through.ts";
83 import { ownHost, pullSourceOf, sourceTrailers } from "../core/trusted_sources.ts";
8384
8485 export interface OpenPullInput {
8586 title: string;
@@ -399,10 +400,14 @@ export async function mergePull(
399400 const asked = toldBy(app.db, pull);
400401 const requester =
401402 asked === null ? undefined : getUserByName(app.db, asked);
402 const trailers = landingTrailers(
403 requester ? commitIdentity(app.config, requester) : null,
404 lander,
405 );
403 const from = pullSourceOf(app.db, pull.id);
404 const trailers = [
405 landingTrailers(
406 requester ? commitIdentity(app.config, requester) : null,
407 lander,
408 ),
409 ...(from === undefined ? [] : [sourceTrailers(from, ownHost(app.config))]),
410 ].join("\n");
406411 const baseDir = repoDir(app.reposRoot, repo.ownerName, repo.name);
407412 const at = await pullDir(app.db, app.reposRoot, repo, pull);
408413 const merged = await landPull(
@@ -439,6 +444,7 @@ export async function mergePull(
439444 number: pull.number,
440445 strategy,
441446 mergeOid: merged.value.mergeOid,
447 ...(from === undefined ? {} : { source: `${from.account}@${from.host}` }),
442448 },
443449 });
444450 // A vision pull, landed by an admin, rewrites the direction's fenced block.
src/actions/trusted_sources.ts added +73
@@ -0,0 +1,73 @@
1 /**
2 * Changing who is trusted (#8937): the owner's list, the kill switch and
3 * the revert of one source's landings. Only a forge admin who is a person
4 * may; each change is an event, so it is audited and the feed shows it.
5 */
6 import type { App } from "../app.ts";
7 import { listEveryRepo } from "../core/repos.ts";
8 import {
9 resumeFoldIn,
10 stopFoldIn,
11 trustSource,
12 untrust,
13 type TrustError,
14 type TrustedSource,
15 } from "../core/trusted_sources.ts";
16 import type { User } from "../core/users.ts";
17 import { revertSource } from "../crew/fold_in.ts";
18 import { err, ok, type Result } from "../kernel/result.ts";
19
20 const mayChange = (actor: User | undefined): actor is User =>
21 actor !== undefined && actor.isAdmin && actor.kind !== "agent";
22
23 export function addTrust(
24 app: App,
25 actor: User | undefined,
26 input: { host: string; account: string; until: string },
27 ): Result<TrustedSource, TrustError | "not_found"> {
28 if (!mayChange(actor)) return err("not_found");
29 const made = trustSource(app.db, { ...input, setBy: actor.username });
30 if (typeof made === "string") return err(made);
31 app.bus.append({
32 kind: "trust.set",
33 actorId: actor.id,
34 payload: { account: made.account, host: made.host, until: made.until },
35 });
36 return ok(made);
37 }
38
39 export function removeTrust(app: App, actor: User | undefined, sourceId: string): Result<TrustedSource, "not_found"> {
40 if (!mayChange(actor)) return err("not_found");
41 const gone = untrust(app.db, sourceId);
42 if (gone === undefined) return err("not_found");
43 app.bus.append({ kind: "trust.removed", actorId: actor.id, payload: { account: gone.account, host: gone.host } });
44 return ok(gone);
45 }
46
47 /** The kill switch: all folding in stops at once, and resumes on one action too. */
48 export function setFoldInStopped(app: App, actor: User | undefined, stopped: boolean): Result<boolean, "not_found"> {
49 if (!mayChange(actor)) return err("not_found");
50 if (stopped) stopFoldIn(app.db, actor.username);
51 else resumeFoldIn(app.db);
52 app.bus.append({ kind: stopped ? "foldin.stopped" : "foldin.resumed", actorId: actor.id, payload: {} });
53 return ok(stopped);
54 }
55
56 /** Open a revert for everything one source landed, in every repository. */
57 export async function revertBySource(
58 app: App,
59 actor: User | undefined,
60 host: string,
61 account: string,
62 ): Promise<Result<{ opened: number; refused: number }, "not_found">> {
63 if (!mayChange(actor)) return err("not_found");
64 let opened = 0;
65 let refused = 0;
66 for (const repo of listEveryRepo(app.db)) {
67 const r = await revertSource(app, actor, repo, host, account);
68 opened += r.opened.length;
69 refused += r.refused.length;
70 }
71 app.bus.append({ kind: "trust.reverted", actorId: actor.id, payload: { account, host, opened, refused } });
72 return ok({ opened, refused });
73 }
src/actions/upstream_follow.ts +15
@@ -44,6 +44,7 @@ import { isSafeOid, isSafeRef } from "../git/guard.ts";
4444 import { aheadCount, mergeBase, mergeable, resolveOid, writeRef } from "../git/merge.ts";
4545 import { REMOTE_PROTOCOLS } from "../git/remote-env.ts";
4646 import { repoDir } from "../git/repos.ts";
47 import { ownHost, recordPullSource, sourceOfUrl, takeable, viaOf } from "../core/trusted_sources.ts";
4748 import { err, ok, type Result } from "../kernel/result.ts";
4849 import { providerForOrigin } from "../origins/credential.ts";
4950 import { redact, secretsIn } from "../origins/credentials.ts";
@@ -301,6 +302,12 @@ async function checkOnce(app: App, repo: Repo): Promise<Result<Checked, CheckErr
301302 return ok({ tip: staged, behind, outcome: "none" });
302303 }
303304
305 // Work that already landed here and came back round a ring of forges is not taken again (#8937).
306 const seenVia = viaOf((await runGitCapture(["log", "-1", "--format=%B", staged], { cwd: dir })).stdout);
307 if (!takeable(seenVia, ownHost(app.config))) {
308 noteCheck(app.db, repo.id, { at, tip: staged, behind: 0, error: "" });
309 return ok({ tip: staged, behind: 0, outcome: "none" });
310 }
304311 const merged = await mergeable(dir, main, staged);
305312 if (!merged.ok) {
306313 if (merged.error.kind !== "conflict") return failed("git");
@@ -323,19 +330,27 @@ async function checkOnce(app: App, repo: Repo): Promise<Result<Checked, CheckErr
323330 const known = pullForBranch(app.db, repo.id, name);
324331 const pending = follow.pendingId === null ? undefined : getIssueById(app.db, follow.pendingId);
325332 const open = pending !== undefined && pending.kind === "pull" && pending.state === "open";
333 // Where the work came from, so a landing can say, and a trusted source's folds in by itself.
334 const from = sourceOfUrl(follow.url);
335 const came = (pullId: string): void => {
336 if (from !== undefined) recordPullSource(app.db, { pullId, ...from, via: seenVia });
337 };
326338 let number: number;
327339 if (known !== undefined) {
340 came(known.id);
328341 // This tip was proposed already, whatever came of it: nothing new.
329342 number = known.number;
330343 } else if (open) {
331344 // The upstream moved on: the open pull follows it instead of a second opening.
332345 retargetPull(app.db, pending.id, name, staged);
346 came(pending.id);
333347 await writeRef(dir, `refs/pull/${pending.number}/head`, staged);
334348 updateIssueText(app.db, pending.id, text);
335349 number = pending.number;
336350 } else {
337351 const opened = await openPull(app, by, repo, { ...text, baseBranch: repo.defaultBranch, headBranch: name });
338352 if (!opened.ok) return failed("git", `The proposal could not be opened: ${opened.error}`);
353 came(opened.value.id);
339354 noteCheck(app.db, repo.id, { at, tip: staged, behind, error: "", pendingId: opened.value.id, opened: true });
340355 return ok({ tip: staged, behind, outcome: "proposed", pull: opened.value.number });
341356 }
src/core/event_audience.ts +3
@@ -169,6 +169,9 @@ export function classifiedAudience(kind: string): EventAudience | undefined {
169169 kind.startsWith("clog.") ||
170170 // What the throughput keeper did to lift a brake is the owner's too (`flow/keeper.ts`).
171171 kind.startsWith("keeper.") ||
172 // Who is trusted to fold in, and the switch that stops it, are the owner's (`actions/trusted_sources.ts`).
173 kind.startsWith("trust.") ||
174 kind.startsWith("foldin.") ||
172175 // Events the retired FLOW ONLY switch wrote stay the admins' to hear.
173176 kind === "flow_only.cleared" ||
174177 // A report from outside waits for a person who decides; its arrival is
src/core/migration-pins/20261103160000-trusted-sources.pin added +1
@@ -0,0 +1,1 @@
1 aacd80722a0817f6606620ae71f9ca407d0c0e51b01960fbcdab3f511a765e88
src/core/migrations/20261103160000-trusted-sources.ts added +32
@@ -0,0 +1,32 @@
1 // Trusted sources (#8937): who, on which forge, until when, set by whom;
2 // where each pull from another forge came from; and the one switch that
3 // stops all folding in. The first three entries are the owner's of 2026-11-02, for a month. Additive.
4 export default `
5 CREATE TABLE trusted_source (
6 id TEXT PRIMARY KEY,
7 host TEXT NOT NULL,
8 account TEXT NOT NULL,
9 until TEXT NOT NULL,
10 set_by TEXT NOT NULL,
11 created_at TEXT NOT NULL,
12 warned_at TEXT,
13 UNIQUE (host, account)
14 );
15 CREATE TABLE pull_source (
16 pull_id TEXT PRIMARY KEY,
17 host TEXT NOT NULL,
18 account TEXT NOT NULL,
19 via TEXT NOT NULL DEFAULT '',
20 received_at TEXT NOT NULL
21 );
22 CREATE INDEX pull_source_by_source ON pull_source (host, account);
23 CREATE TABLE fold_in_stop (
24 id INTEGER PRIMARY KEY CHECK (id = 1),
25 by TEXT NOT NULL,
26 at TEXT NOT NULL
27 );
28 INSERT INTO trusted_source (id, host, account, until, set_by, created_at) VALUES
29 ('01trusted0tdobbertin0espo', 'git.open-platform.sh', 'tdobbertin-espo', '2026-12-02T23:59:59.999Z', 'owner', '2026-11-02T00:00:00.000Z'),
30 ('01trusted0000jmoran00000', 'git.open-platform.sh', 'jmoran', '2026-12-02T23:59:59.999Z', 'owner', '2026-11-02T00:00:00.000Z'),
31 ('01trusted00000benjivers0', 'git.open-platform.sh', 'benjivers', '2026-12-02T23:59:59.999Z', 'owner', '2026-11-02T00:00:00.000Z');
32 `;
src/core/pull_trust.ts +3
@@ -22,6 +22,7 @@ import { getIssueById, getPull, type Pull } from "./pulls.ts";
2222 import { canWrite, getRepoById, onCrew, type Repo } from "./repos.ts";
2323 import { listReviews } from "./reviews.ts";
2424 import { runGrantedAt } from "./run_grant.ts";
25 import { foldsIn } from "./trusted_sources.ts";
2526 import { getUserById, type User } from "./users.ts";
2627
2728 const trusted = (db: Database, repo: Repo, user: User | undefined): boolean =>
@@ -40,6 +41,8 @@ export function pullMayRun(
4041 headOid: string,
4142 ): boolean {
4243 const proposed = isProposed(db, pull);
44 // A trusted source's code runs its checks like the crew's (#8937).
45 if (foldsIn(db, pull.id)) return true;
4346 if (!proposed && !pull.crossRepo && pull.originHeadRepo === null) return true;
4447 if (!proposed && pull.crossRepo && pull.originHeadRepo === null) {
4548 const author = getUserById(db, pull.authorId);
src/core/trusted_sources.ts added +231
@@ -0,0 +1,231 @@
1 /**
2 * Trusted sources (#8937): an account on a named forge, until a date, set by
3 * a person. An account name means nothing without its forge — the same name
4 * elsewhere is a stranger — so a source is always the pair. Trust ends by
5 * itself: past `until` a source is no longer listed as active and its work
6 * waits for review like anyone's. Pure data, no authz, no events;
7 * `actions/trusted_sources.ts` decides who may change it and audits it.
8 */
9 import type { Database } from "bun:sqlite";
10 import { id } from "../kernel/id.ts";
11
12 export interface TrustedSource {
13 readonly id: string;
14 readonly host: string;
15 readonly account: string;
16 readonly until: string;
17 readonly setBy: string;
18 readonly createdAt: string;
19 readonly warnedAt: string | null;
20 }
21
22 interface Row {
23 id: string;
24 host: string;
25 account: string;
26 until: string;
27 set_by: string;
28 created_at: string;
29 warned_at: string | null;
30 }
31
32 const toSource = (r: Row): TrustedSource => ({
33 id: r.id,
34 host: r.host,
35 account: r.account,
36 until: r.until,
37 setBy: r.set_by,
38 createdAt: r.created_at,
39 warnedAt: r.warned_at,
40 });
41
42 /** Days before expiry that the note is sent. */
43 export const WARN_DAYS = 3;
44 const DAY_MS = 86_400_000;
45
46 const HOST = /^[a-z0-9]([a-z0-9.-]{0,251}[a-z0-9])?(:\d{1,5})?$/;
47 const ACCOUNT = /^[a-z0-9][a-z0-9._-]{0,63}$/;
48
49 /** A forge's name as a source is kept: lowercase host, no scheme, no path. */
50 export function cleanHost(raw: string): string | undefined {
51 const text = raw.trim().toLowerCase();
52 const host = URL.parse(text)?.host || text.replace(/^[a-z]+:\/\//, "").replace(/\/.*$/, "");
53 return HOST.test(host) ? host : undefined;
54 }
55
56 export function cleanAccount(raw: string): string | undefined {
57 const name = raw.trim().replace(/^@/, "").toLowerCase();
58 return ACCOUNT.test(name) ? name : undefined;
59 }
60
61 export type TrustError = "bad_host" | "bad_account" | "bad_date" | "in_the_past";
62
63 /** Keep (or renew) a source. The end is a calendar date, read as the end of that UTC day. */
64 export function trustSource(
65 db: Database,
66 input: { host: string; account: string; until: string; setBy: string },
67 now: Date = new Date(),
68 ): TrustedSource | TrustError {
69 const host = cleanHost(input.host);
70 if (host === undefined) return "bad_host";
71 const account = cleanAccount(input.account);
72 if (account === undefined) return "bad_account";
73 if (!/^\d{4}-\d{2}-\d{2}$/.test(input.until)) return "bad_date";
74 const end = new Date(`${input.until}T23:59:59.999Z`);
75 if (Number.isNaN(end.getTime())) return "bad_date";
76 if (end.getTime() <= now.getTime()) return "in_the_past";
77 const until = end.toISOString();
78 db.query(
79 `INSERT INTO trusted_source (id, host, account, until, set_by, created_at)
80 VALUES (?1, ?2, ?3, ?4, ?5, ?6)
81 ON CONFLICT (host, account)
82 DO UPDATE SET until = excluded.until, set_by = excluded.set_by, warned_at = NULL`,
83 ).run(id(), host, account, until, input.setBy, now.toISOString());
84 return sourceOf(db, host, account)!;
85 }
86
87 export function sourceOf(db: Database, host: string, account: string): TrustedSource | undefined {
88 const row = db
89 .query<Row, [string, string]>("SELECT * FROM trusted_source WHERE host = ?1 AND account = ?2")
90 .get(host.toLowerCase(), account.toLowerCase());
91 return row === null ? undefined : toSource(row);
92 }
93
94 export function untrust(db: Database, sourceId: string): TrustedSource | undefined {
95 const row = db.query<Row, [string]>("SELECT * FROM trusted_source WHERE id = ?1").get(sourceId);
96 if (row === null) return undefined;
97 db.query("DELETE FROM trusted_source WHERE id = ?1").run(sourceId);
98 return toSource(row);
99 }
100
101 /** Every entry, expired ones included (the page shows them as lapsed), soonest end first. */
102 export function listTrusted(db: Database): TrustedSource[] {
103 return db.query<Row, []>("SELECT * FROM trusted_source ORDER BY until, host, account").all().map(toSource);
104 }
105
106 /** Whether the account on that forge is trusted at `now`. */
107 export function isTrusted(db: Database, host: string, account: string, now: Date = new Date()): boolean {
108 const source = sourceOf(db, host, account);
109 return source !== undefined && source.until > now.toISOString();
110 }
111
112 /** Entries that end within the warning window and have not been told about yet. */
113 export function dueWarning(db: Database, now: Date = new Date()): TrustedSource[] {
114 const soon = new Date(now.getTime() + WARN_DAYS * DAY_MS).toISOString();
115 return db
116 .query<Row, [string, string]>(
117 "SELECT * FROM trusted_source WHERE warned_at IS NULL AND until > ?1 AND until <= ?2",
118 )
119 .all(now.toISOString(), soon)
120 .map(toSource);
121 }
122
123 export function markWarned(db: Database, sourceId: string, now: Date = new Date()): void {
124 db.query("UPDATE trusted_source SET warned_at = ?2 WHERE id = ?1").run(sourceId, now.toISOString());
125 }
126
127 /** Whether the entry ends within the warning window (or already has). */
128 export const endsSoon = (s: TrustedSource, now: Date = new Date()): boolean =>
129 Date.parse(s.until) - now.getTime() <= WARN_DAYS * DAY_MS;
130
131 // --- the kill switch -------------------------------------------------------
132
133 export function foldInStopped(db: Database): { by: string; at: string } | undefined {
134 const row = db.query<{ by: string; at: string }, []>("SELECT by, at FROM fold_in_stop WHERE id = 1").get();
135 return row ?? undefined;
136 }
137
138 export function stopFoldIn(db: Database, by: string, now: Date = new Date()): void {
139 db.query("INSERT OR REPLACE INTO fold_in_stop (id, by, at) VALUES (1, ?1, ?2)").run(by, now.toISOString());
140 }
141
142 export function resumeFoldIn(db: Database): void {
143 db.query("DELETE FROM fold_in_stop WHERE id = 1").run();
144 }
145
146 // --- where a pull came from ------------------------------------------------
147
148 export interface PullSource {
149 readonly pullId: string;
150 readonly host: string;
151 readonly account: string;
152 /** Forges the work passed through before it reached this one, oldest first. */
153 readonly via: readonly string[];
154 }
155
156 export function recordPullSource(
157 db: Database,
158 input: { pullId: string; host: string; account: string; via: readonly string[] },
159 now: Date = new Date(),
160 ): void {
161 db.query(
162 `INSERT OR REPLACE INTO pull_source (pull_id, host, account, via, received_at)
163 VALUES (?1, ?2, ?3, ?4, ?5)`,
164 ).run(input.pullId, input.host.toLowerCase(), input.account.toLowerCase(), input.via.join(","), now.toISOString());
165 }
166
167 export function pullSourceOf(db: Database, pullId: string): PullSource | undefined {
168 const row = db
169 .query<{ host: string; account: string; via: string }, [string]>(
170 "SELECT host, account, via FROM pull_source WHERE pull_id = ?1",
171 )
172 .get(pullId);
173 return row === null
174 ? undefined
175 : { pullId, host: row.host, account: row.account, via: row.via === "" ? [] : row.via.split(",") };
176 }
177
178 /** Pull ids that came from one source, newest first. */
179 export function pullIdsFrom(db: Database, host: string, account: string): string[] {
180 return db
181 .query<{ pull_id: string }, [string, string]>(
182 "SELECT pull_id FROM pull_source WHERE host = ?1 AND account = ?2 ORDER BY received_at DESC",
183 )
184 .all(host.toLowerCase(), account.toLowerCase())
185 .map((r) => r.pull_id);
186 }
187
188 // --- a landing says where it came from -------------------------------------
189
190 /**
191 * Whether a pull's source is trusted right now and folding in is not
192 * stopped: the one question the crew's landing path asks. Trust skips the
193 * person's review and nothing else.
194 */
195 export function foldsIn(db: Database, pullId: string, now: Date = new Date()): boolean {
196 const from = pullSourceOf(db, pullId);
197 return from !== undefined && foldInStopped(db) === undefined && isTrusted(db, from.host, from.account, now);
198 }
199
200 /**
201 * The trailers a landing from another forge carries. `Landed-via` lists
202 * every forge the work passed through, this one last: a forge that finds
203 * itself in the list will not take the work again (`takeable`).
204 */
205 export function sourceTrailers(from: PullSource, selfHost: string): string {
206 const via = [...from.via, from.host, selfHost.toLowerCase()].filter((h, i, all) => all.indexOf(h) === i);
207 return `Landed-from: ${from.account}@${from.host}\nLanded-via: ${via.join(",")}`;
208 }
209
210 /** `Landed-via` read back from a commit message; empty when it has none. */
211 export function viaOf(message: string): string[] {
212 const line = /^Landed-via: ([a-z0-9.,:-]{1,500})$/m.exec(message);
213 return line === null ? [] : line[1]!.split(",").filter((h) => HOST.test(h));
214 }
215
216 /** Work that already passed through this forge is not taken in again: that is how a ring of forges stops. */
217 export const takeable = (via: readonly string[], selfHost: string): boolean => !via.includes(selfHost.toLowerCase());
218
219 /** This forge's own name as other forges' trust lists spell it. */
220 export function ownHost(config: { url?: string | undefined; host?: string | undefined }): string {
221 return (config.url === undefined ? undefined : URL.parse(config.url)?.host.toLowerCase()) ?? (config.host ?? "localhost").toLowerCase();
222 }
223
224 /** Who an upstream's address names: the forge, and the account that owns the repository (`https://host/account/name.git`). */
225 export function sourceOfUrl(url: string): { host: string; account: string } | undefined {
226 const parsed = URL.parse(url);
227 if (parsed === null || (parsed.protocol !== "https:" && parsed.protocol !== "http:")) return undefined;
228 const host = cleanHost(parsed.host);
229 const account = cleanAccount(parsed.pathname.split("/").filter((s) => s !== "")[0] ?? "");
230 return host === undefined || account === undefined ? undefined : { host, account };
231 }
src/crew/autonomy.ts +36
@@ -135,6 +135,9 @@ import {
135135 landsOnItsOwn,
136136 markReaders,
137137 } from "./direct.ts";
138 import { foldsIn } from "../core/trusted_sources.ts";
139 import { foldWall } from "./fold_walls.ts";
140 import { warnExpiring } from "./fold_in.ts";
138141 import { crewOffInPreview } from "./preview.ts";
139142 import { approvedOnHead, finishApproved, finishFields } from "./finishing.ts";
140143 import { recheckRedApproved } from "./red_approved.ts";
@@ -1314,6 +1317,12 @@ export async function autonomyTick(
13141317 } catch (error) {
13151318 log.error("stranded sweep failed", { error: String(error) });
13161319 }
1320 // Three days before a trusted source lapses, a note (#8937).
1321 try {
1322 warnExpiring(app, now);
1323 } catch (error) {
1324 log.error("trust note failed", { error: String(error) });
1325 }
13171326 for (const repo of listAutonomousRepos(app.db)) {
13181327 // An archived repository is put away: the crew starts nothing in it.
13191328 if (repo.archivedAt !== null) continue;
@@ -1954,6 +1963,9 @@ async function reviewed(
19541963 const seen = reviewSummary(app.db, pull.id, pull.headOid);
19551964 if (seen.changesRequested > 0) return "hold";
19561965 if (seen.approvals > 0) return "pass";
1966 // A trusted source's pull needs no person's review (#8937); its check, the
1967 // boot proof and the walls above still stood before this point.
1968 if (foldsIn(app.db, pull.id)) return "pass";
19571969 // An agent's pull ships only on an approving review — the crew's reviewer
19581970 // or a person's. With no reviewer, or a review that lapsed, it waits for
19591971 // a person (`waitsForPerson` puts it where a person looks); never by lapse.
@@ -2101,6 +2113,27 @@ async function admit(
21012113 }
21022114 return err("wall");
21032115 }
2116 // A trusted source's pull that touches secrets, credentials, permissions,
2117 // the contract or release machinery waits for a person like a guest's.
2118 const folded = await foldWall(app, repo, pull, baseTip);
2119 if (folded.length > 0) {
2120 const earlier = latestDecision(app.db, pull.id, "land");
2121 const told =
2122 earlier?.answer === "wall" &&
2123 (earlier.state as { head?: unknown } | null)?.head === pull.headOid;
2124 if (!told)
2125 recordDecision(app.db, {
2126 kind: "land",
2127 repoId: repo.id,
2128 issueId: pull.id,
2129 state: { head: pull.headOid, cells: folded, plain: folded, unsound: false },
2130 answer: "wall",
2131 p: 1,
2132 by: "wall",
2133 ms: 0,
2134 });
2135 return err("wall");
2136 }
21042137 // One gate for every pull, whoever wrote it: when the crew has a
21052138 // reviewer, a green head is queued for review and waits for an approval
21062139 // on that very head (a person's counts the same). A push is a new head,
@@ -2246,6 +2279,9 @@ export async function landingStrategy(
22462279 pull: Pull,
22472280 baseOid: string,
22482281 ): Promise<"merge" | "squash"> {
2282 // A trusted source's work lands as a true merge: its commits stay
2283 // ancestors of main and each author keeps their name (#8937).
2284 if (foldsIn(app.db, pull.id)) return "merge";
22492285 return (await carriesUpstream(
22502286 app,
22512287 repo,
src/crew/direct.ts +4
@@ -19,6 +19,7 @@ import { crewRole } from "../core/agents.ts";
1919 import { canRead, canWrite, type Repo } from "../core/repos.ts";
2020 import type { Issue } from "../core/issues.ts";
2121 import { isProposed } from "../core/pull_trust.ts";
22 import { foldsIn } from "../core/trusted_sources.ts";
2223 import { openPullsClosing, type Pull } from "../core/pulls.ts";
2324 import { getUserById, getUserByName, type User } from "../core/users.ts";
2425
@@ -224,6 +225,9 @@ export function unadoptedAmong(
224225 * crew, not a reader's code reach the default branch unread by a person.
225226 */
226227 export function landsOnItsOwn(db: Database, repo: Repo, pull: Pull): boolean {
228 // A trusted source's pull (#8937) lands on its checks, wall and boot proof
229 // alone; trust skips the person and nothing else.
230 if (foldsIn(db, pull.id)) return true;
227231 if (pull.originAuthor !== null) return false;
228232 // A child's code, whoever holds its token: a grant lets it run, a person's
229233 // review lands it.
src/crew/fold_in.ts added +110
@@ -0,0 +1,110 @@
1 /**
2 * Folding in (#8937): work from a trusted source flows to the default
3 * branch without a person's review, and without skipping anything else —
4 * its check, the boot proof and the walls still stand. This module is the
5 * rules that are not the merge queue's: how a branch from another forge
6 * becomes a pull, the note before a trust lapses, and the revert of one
7 * source's work. The sweep for what no event announced is the autonomy
8 * tick's own (`sweepGreen` admits a trusted source's pull like the crew's);
9 * the walls are in `fold_walls.ts`.
10 */
11 import type { App } from "../app.ts";
12 import { getIssueById } from "../core/issues.ts";
13 import { getPull, type Pull } from "../core/pulls.ts";
14 import type { Repo } from "../core/repos.ts";
15 import {
16 dueWarning,
17 foldInStopped,
18 isTrusted,
19 markWarned,
20 ownHost,
21 pullIdsFrom,
22 recordPullSource,
23 takeable,
24 } from "../core/trusted_sources.ts";
25 import { getUserById, type User } from "../core/users.ts";
26 import { runGitCapture } from "../git/env.ts";
27 import { isSafeRef } from "../git/guard.ts";
28 import { repoDir } from "../git/repos.ts";
29 import { err, ok, type Result } from "../kernel/result.ts";
30 import { openPull } from "../actions/pulls.ts";
31 import { revertPull } from "../actions/revert.ts";
32
33 export interface Incoming {
34 readonly host: string;
35 readonly account: string;
36 /** Forges the work passed through already (its `Landed-via` trailer). */
37 readonly via: readonly string[];
38 readonly branch: string;
39 readonly title: string;
40 readonly body?: string;
41 }
42
43 export type IntakeError = "stopped" | "not_trusted" | "loop" | "bad_branch" | "no_such_branch" | "not_opened";
44
45 /**
46 * A branch from a source on another forge becomes a pull on this one, with
47 * the source recorded so the landing can say where it came from. Only a
48 * trusted source's branch is taken here — everyone else's arrives as a
49 * guest pull through the contribute path — and work that already passed
50 * through this forge is refused, so a ring of forges cannot re-land it.
51 * The pull is opened as the person who turned autonomy on.
52 */
53 export async function takeIncoming(app: App, repo: Repo, from: Incoming): Promise<Result<Pull, IntakeError>> {
54 if (foldInStopped(app.db) !== undefined) return err("stopped");
55 if (!isTrusted(app.db, from.host, from.account)) return err("not_trusted");
56 if (!takeable(from.via, ownHost(app.config))) return err("loop");
57 if (!isSafeRef(from.branch)) return err("bad_branch");
58 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
59 const known = await runGitCapture(["rev-parse", "--verify", "--quiet", `refs/heads/${from.branch}`], { cwd: dir });
60 if (known.code !== 0) return err("no_such_branch");
61 const by = repo.autonomyBy === null ? undefined : getUserById(app.db, repo.autonomyBy);
62 if (by === undefined) return err("not_opened");
63 const opened = await openPull(app, by, repo, {
64 title: from.title,
65 body: `${from.body ?? ""}\n\nFolded in from ${from.account}@${from.host}.`.trim(),
66 baseBranch: repo.defaultBranch,
67 headBranch: from.branch,
68 });
69 if (!opened.ok) return err("not_opened");
70 recordPullSource(app.db, { pullId: opened.value.id, host: from.host, account: from.account, via: from.via });
71 return ok(opened.value);
72 }
73
74 /** The note 3 days before a trust ends: an event the owner's feed shows, once per entry. */
75 export function warnExpiring(app: App, now: Date = new Date()): number {
76 const due = dueWarning(app.db, now);
77 for (const source of due) {
78 app.bus.append({
79 kind: "trust.expiring",
80 payload: { account: source.account, host: source.host, until: source.until },
81 });
82 markWarned(app.db, source.id, now);
83 }
84 return due.length;
85 }
86
87 /**
88 * A bad landing reverts by source: every merged pull of one source in this
89 * repository becomes a revert pull, newest first, for a person to land.
90 * Which pulls were reverted, and which could not be.
91 */
92 export async function revertSource(
93 app: App,
94 actor: User,
95 repo: Repo,
96 host: string,
97 account: string,
98 ): Promise<{ opened: number[]; refused: number[] }> {
99 const opened: number[] = [];
100 const refused: number[] = [];
101 for (const id of pullIdsFrom(app.db, host, account)) {
102 const issue = getIssueById(app.db, id);
103 if (issue === undefined || issue.repoId !== repo.id) continue;
104 const pull = getPull(app.db, repo.id, issue.number);
105 if (pull === undefined || pull.mergedAt === null) continue;
106 const reverted = await revertPull(app, actor, repo, pull, `Reverts #${pull.number}, folded in from ${account}@${host}.`);
107 (reverted.ok ? opened : refused).push(pull.number);
108 }
109 return { opened, refused };
110 }
src/crew/fold_walls.ts added +50
@@ -0,0 +1,50 @@
1 /**
2 * The walls a trusted source's pull still stops at (#8937): secrets and
3 * credentials, permissions and the contract's walls, release machinery.
4 * Trust skips the person's review, never these.
5 */
6 import type { App } from "../app.ts";
7 import { pullDir, type Pull } from "../core/pulls.ts";
8 import type { Repo } from "../core/repos.ts";
9 import { foldsIn } from "../core/trusted_sources.ts";
10 import { runGitCapture } from "../git/env.ts";
11
12 /**
13 * Paths a person decides on whoever wrote them: secrets and credentials,
14 * who may do what, the contract's walls, and the machinery that releases.
15 * Each rule says why in the words a person reads on the card.
16 */
17 const WALLS: readonly { readonly test: RegExp; readonly why: string }[] = [
18 { test: /(^|\/)\.env([./]|$)|(^|\/)(secrets?|credentials?)([./_-]|$)|\.(pem|key|p12|pfx)$|(^|\/)id_(rsa|ed25519|ecdsa)/i, why: "secrets or credentials" },
19 { test: /^src\/(auth|origins\/credential|kernel\/secret|core\/(secret|tokens|keys|sessions|passkeys|run_token|run_grant|proposal_token))/, why: "secrets or credentials" },
20 { test: /^src\/(contract|core\/(roles|access_fingerprint|pull_trust|trusted_sources)|crew\/fold_in|http\/run_gate|actions\/(access|app_members|egress_grants|model_grant))/, why: "permissions or the contract's walls" },
21 { test: /^(src\/(release|keeper)\/|scripts\/(gate|check|cut-release|roll|dark-roll|rehearse-roll|roll-preflight)\.ts$|Dockerfile$|charts\/|infra\/|forge\.json$|package\.json$|bun\.lock$|bunfig\.toml$|\.github\/|src\/core\/migration-pins)/, why: "release machinery" },
22 ];
23
24 /** What the walls hold a set of files for, as plain sentences; empty when it touches none. */
25 export function wallsTouched(files: readonly string[]): string[] {
26 const reasons = new Set<string>();
27 for (const file of files) for (const w of WALLS) if (w.test.test(file)) reasons.add(w.why);
28 return [...reasons].map((why) => `a trusted source's pull touches ${why}`);
29 }
30
31 /** The files a pull changes against its base, or undefined when git could not say. */
32 export async function touchedFiles(dir: string, base: string, head: string): Promise<string[] | undefined> {
33 const out = await runGitCapture(["diff", "--name-only", "-z", "--no-renames", `${base}...${head}`, "--"], { cwd: dir });
34 if (out.code !== 0) return undefined;
35 return out.stdout.split("\0").filter((f) => f !== "");
36 }
37
38 /**
39 * What waits for a person on a trusted pull even though its source is
40 * trusted: the walls. Empty for a guest's pull (it waits for a person
41 * anyway) and for a trusted pull that touches none. A diff git cannot read
42 * is a wall too: unread is not clear.
43 */
44 export async function foldWall(app: App, repo: Repo, pull: Pull, baseTip: string): Promise<string[]> {
45 if (!foldsIn(app.db, pull.id)) return [];
46 const dir = await pullDir(app.db, app.reposRoot, repo, pull);
47 const files = await touchedFiles(dir, baseTip, pull.headOid);
48 return files === undefined ? ["a trusted source's pull could not be read"] : wallsTouched(files);
49 }
50
src/http/settings_routes.ts +6
@@ -15,6 +15,7 @@ import { capacityPage, capacitySet } from "../web/pages/capacity.ts";
1515 import { teamSet, teamsPage, teamsStream } from "../web/pages/teams.ts";
1616 import { telemetryPage, telemetrySet } from "../web/pages/telemetry.ts";
1717 import { flagsPage, flagsSet } from "../web/pages/flags.ts";
18 import { trustedAdd, trustedPage, trustedRemove, trustedRevert, trustedStop } from "../web/pages/trusted.ts";
1819 import { usagePage } from "../web/pages/usage.ts";
1920 import { spendPage } from "../web/pages/spend-tab.ts";
2021 import {
@@ -112,6 +113,11 @@ import { originFlowPage, originFlowPoll } from "../web/pages/origin-flow.ts";
112113 import { originMirrorSubmit, originReposPage } from "../web/pages/origin-repos.ts";
113114
114115 export function settingsRoutes(router: Router<Handler>, knownPatterns: () => readonly string[]): void {
116 router.add("GET", "/settings/trusted", trustedPage);
117 router.add("POST", "/settings/trusted", trustedAdd);
118 router.add("POST", "/settings/trusted/stop", trustedStop);
119 router.add("POST", "/settings/trusted/revert", trustedRevert);
120 router.add("POST", "/settings/trusted/:id/remove", trustedRemove);
115121 router.add("GET", "/settings/telemetry", telemetryPage);
116122 router.add("POST", "/settings/telemetry", telemetrySet);
117123 router.add("GET", "/settings", accountPage);
src/web/pages/origins.ts +7
@@ -93,6 +93,7 @@ export type AccountSection =
9393 | "spend"
9494 | "crew"
9595 | "accounts"
96 | "trusted"
9697 | "people"
9798 | "subscriptions"
9899 | "backups"
@@ -137,6 +138,12 @@ export function accountSections(
137138 icon: "bot" as const,
138139 current: current === "crew",
139140 },
141 {
142 label: "Trusted sources",
143 href: "/settings/trusted",
144 icon: "lock" as const,
145 current: current === "trusted",
146 },
140147 {
141148 label: "Subscriptions",
142149 href: "/settings/subscriptions",
src/web/pages/trusted.ts added +166
@@ -0,0 +1,166 @@
1 /**
2 * Settings → Trusted sources (#8937), for the owner: who may fold in without
3 * a person's review, until when; a one-click remove; the kill switch that
4 * stops all folding in; and the revert of one source's landings.
5 */
6 import { addTrust, removeTrust, revertBySource, setFoldInStopped } from "../../actions/trusted_sources.ts";
7 import {
8 endsSoon,
9 foldInStopped,
10 listTrusted,
11 type TrustError,
12 } from "../../core/trusted_sources.ts";
13 import type { Ctx } from "../../http/context.ts";
14 import { notFoundPage, settingsLayout, signInFirst } from "../chrome.ts";
15 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
16 import { field, readForm } from "../form.ts";
17 import { html } from "../html.ts";
18 import { flashTo, page } from "../layout.ts";
19 import { badge, btn, card, icon, row, time } from "../ui.ts";
20 import { accountSections } from "./origins.ts";
21
22 function owner(ctx: Ctx): Response | undefined {
23 if (!ctx.auth.user) return signInFirst(ctx);
24 return ctx.auth.user.isAdmin && ctx.auth.user.kind !== "agent" ? undefined : notFoundPage(ctx);
25 }
26
27 const WORDS: Record<TrustError | "not_found", string> = {
28 bad_host: "That is not a forge address.",
29 bad_account: "That is not an account name.",
30 bad_date: "Give the end as a date, year-month-day.",
31 in_the_past: "That date has passed.",
32 not_found: "Not found.",
33 };
34
35 // GET /settings/trusted
36 export function trustedPage(ctx: Ctx): Response {
37 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
38 const denied = owner(ctx);
39 if (denied) return denied;
40 const rendering = performance.now();
41 const now = new Date();
42 const stopped = foldInStopped(ctx.app.db);
43 const entries = listTrusted(ctx.app.db);
44 const body = html`${card(
45 html`<div class="card-body"><p class="muted small">
46 Work from a trusted source flows in as it appears and lands by true merge when its checks pass,
47 with no person's review. It never skips the check, the boot proof or the walls: a pull touching
48 secrets, credentials, permissions, the contract or release machinery still waits for you. Anyone
49 else is a guest, and their pull waits on <a href="/review">review</a>. An account name means
50 nothing without its forge.
51 </p>
52 <form method="post" action="/settings/trusted/stop" class="row-fields">
53 <input type="hidden" name="stop" value="${stopped ? "0" : "1"}" />
54 ${stopped
55 ? html`${badge("Folding in is stopped", { variant: "outline" })}
56 <span class="muted small">by ${stopped.by}, <time datetime="${stopped.at}">${stopped.at.slice(0, 10)}</time></span>
57 ${btn("Resume folding in", { size: "sm" })}`
58 : btn("Stop all folding in", { size: "sm", variant: "outline" })}
59 </form></div>`,
60 { head: html`${icon("lock")}<h2>Fold-in</h2>` },
61 )}
62 ${card(
63 html`<ul class="list setting-list">
64 ${entries.length === 0
65 ? html`<li class="muted small">No one is trusted. Everyone waits for review.</li>`
66 : entries.map((s) => {
67 const lapsed = s.until <= now.toISOString();
68 return row({
69 lead: icon("user"),
70 title: html`${s.account} <span class="muted small mono">${s.host}</span>`,
71 meta: html`${lapsed ? "Lapsed, waits for review" : "Until"} ${time(s.until)}${
72 !lapsed && endsSoon(s, now) ? " (ends soon)" : ""
73 } · set by ${s.setBy}`,
74 trail: html`<form class="row-fields" method="post" action="/settings/trusted/${s.id}/remove">
75 ${btn("Remove", { size: "sm", variant: "outline", label: `Remove ${s.account} on ${s.host}` })}
76 </form>
77 <form class="row-fields" method="post" action="/settings/trusted/revert">
78 <input type="hidden" name="host" value="${s.host}" />
79 <input type="hidden" name="account" value="${s.account}" />
80 ${btn("Revert landings", { size: "sm", variant: "outline", label: `Revert what ${s.account} on ${s.host} landed` })}
81 </form>`,
82 });
83 })}
84 </ul>`,
85 { head: html`${icon("users")}<h2>Trusted sources</h2>`, pad: false },
86 )}
87 ${card(
88 html`<form method="post" action="/settings/trusted" class="stack">
89 <label>Account <input class="input" name="account" required maxlength="64" autocomplete="off" /></label>
90 <label>Forge <input class="input" name="host" required maxlength="260" placeholder="git.open-platform.sh" autocomplete="off" /></label>
91 <label>Until <input class="input" name="until" type="date" required /></label>
92 ${btn("Trust", { icon: "check" })}
93 </form>`,
94 { head: html`${icon("plus")}<h2>Trust a source</h2>` },
95 )}`;
96 notePhase(ctx.request, "render", performance.now() - rendering);
97 return page({
98 ctx,
99 title: "Trusted sources",
100 body: html`<div class="page-head"><h1>Trusted sources</h1></div>
101 ${settingsLayout(accountSections("trusted", { admin: true }), body, "Account settings")}`,
102 });
103 }
104
105 // POST /settings/trusted
106 export async function trustedAdd(ctx: Ctx): Promise<Response> {
107 const denied = owner(ctx);
108 if (denied) return denied;
109 const form = await readForm(ctx, "admin");
110 if (form instanceof Response) return form;
111 const made = addTrust(ctx.app, ctx.auth.user, {
112 host: field(form, "host"),
113 account: field(form, "account"),
114 until: field(form, "until"),
115 });
116 return flashTo(
117 ctx,
118 "/settings/trusted",
119 made.ok
120 ? { message: `${made.value.account} on ${made.value.host} is trusted until ${made.value.until.slice(0, 10)}.` }
121 : { tone: "danger", message: WORDS[made.error] },
122 );
123 }
124
125 // POST /settings/trusted/:id/remove
126 export async function trustedRemove(ctx: Ctx): Promise<Response> {
127 const denied = owner(ctx);
128 if (denied) return denied;
129 const form = await readForm(ctx, "admin");
130 if (form instanceof Response) return form;
131 const gone = removeTrust(ctx.app, ctx.auth.user, ctx.params.id ?? "");
132 return flashTo(
133 ctx,
134 "/settings/trusted",
135 gone.ok
136 ? { message: `${gone.value.account} on ${gone.value.host} now waits for review.` }
137 : { tone: "danger", message: WORDS.not_found },
138 );
139 }
140
141 // POST /settings/trusted/stop
142 export async function trustedStop(ctx: Ctx): Promise<Response> {
143 const denied = owner(ctx);
144 if (denied) return denied;
145 const form = await readForm(ctx, "admin");
146 if (form instanceof Response) return form;
147 const stop = field(form, "stop") === "1";
148 setFoldInStopped(ctx.app, ctx.auth.user, stop);
149 return flashTo(ctx, "/settings/trusted", { message: stop ? "Folding in is stopped." : "Folding in resumed." });
150 }
151
152 // POST /settings/trusted/revert
153 export async function trustedRevert(ctx: Ctx): Promise<Response> {
154 const denied = owner(ctx);
155 if (denied) return denied;
156 const form = await readForm(ctx, "admin");
157 if (form instanceof Response) return form;
158 const done = await revertBySource(ctx.app, ctx.auth.user, field(form, "host"), field(form, "account"));
159 return flashTo(
160 ctx,
161 "/settings/trusted",
162 done.ok
163 ? { message: `${done.value.opened} revert pull${done.value.opened === 1 ? "" : "s"} opened for you to land.` }
164 : { tone: "danger", message: WORDS.not_found },
165 );
166 }
src/web/studio/review_act.ts +3 −3
@@ -44,8 +44,8 @@ export function verbsFor(a: Ask): readonly Verb[] {
4444 return a.pull !== undefined ? VERBS : a.issue !== undefined ? ["close", "reply"] : [];
4545 }
4646
47 /** The buttons and boxes under a row, a sibling of its link; `undefined` when it offers none. */
48 export function actRow(a: Ask): Html | undefined {
47 /** The buttons and boxes under a row (a guest's pull from another forge says Approve and Decline), a sibling of its link; `undefined` when it offers none. */
48 export function actRow(a: Ask, guest = false): Html | undefined {
4949 const verbs = verbsFor(a);
5050 const number = a.pull?.number ?? a.issue?.number;
5151 if (verbs.length === 0 || number === undefined) return undefined;
@@ -66,7 +66,7 @@ export function actRow(a: Ask): Html | undefined {
6666 const quick = verbs.filter((v) => v === "merge" || v === "close");
6767 return html`<li data-act-forms>
6868 <div class="form-actions">${quick.map((v) =>
69 form(v, btn(v === "merge" ? "Merge" : "Close")),
69 form(v, btn(guest ? (v === "merge" ? "Approve" : "Decline") : v === "merge" ? "Merge" : "Close")),
7070 )}</div>
7171 ${verbs.includes("changes") ? box("changes", "Request changes: what must change", "Request changes") : null}
7272 ${box("reply", "Reply", "Reply")}
src/web/studio/review_page.ts +2 −1
@@ -20,6 +20,7 @@ import { badge, empty, listCard, row, runStateBadge, time } from "../ui.ts";
2020 import { lookThumb, lookThumbPlain, thumbLook } from "../pages/looks.ts";
2121 import { stepOfStored } from "../../flow/path.ts";
2222 import { stepLink } from "./step_link.ts";
23 import { pullSourceOf } from "../../core/trusted_sources.ts";
2324 import { actRow, rowId } from "./review_act.ts";
2425 import { reviewSet } from "./review_set.ts";
2526 import { staleArchive } from "./review_stale.ts";
@@ -121,7 +122,7 @@ export function reviewPage(ctx: Ctx): Response {
121122 : stat === "failed"
122123 ? " · Diff size unavailable — open the pull to see it"
123124 : "";
124 const act = actRow(a);
125 const act = actRow(a, a.pull !== undefined && pullSourceOf(ctx.app.db, a.pull.id) !== undefined);
125126 const step = a.pull === undefined ? null : stepOfStored(ctx.app.db, a.repo, a.pull);
126127 return row({
127128 href,
test/trusted-sources.test.ts added +219
@@ -0,0 +1,219 @@
1 /** Trusted sources fold in by themselves (#8937): trust skips the person, nothing else. */
2 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { openPull } from "../src/actions/pulls.ts";
7 import { addTrust, removeTrust, setFoldInStopped } from "../src/actions/trusted_sources.ts";
8 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
9 import { createApp, type App } from "../src/app.ts";
10 import { pullMayRun } from "../src/core/pull_trust.ts";
11 import { getPull } from "../src/core/pulls.ts";
12 import type { Repo } from "../src/core/repos.ts";
13 import {
14 dueWarning,
15 foldsIn,
16 isTrusted,
17 listTrusted,
18 pullSourceOf,
19 recordPullSource,
20 sourceOfUrl,
21 sourceTrailers,
22 takeable,
23 trustSource,
24 viaOf,
25 } from "../src/core/trusted_sources.ts";
26 import type { User } from "../src/core/users.ts";
27 import { landsOnItsOwn } from "../src/crew/direct.ts";
28 import { warnExpiring } from "../src/crew/fold_in.ts";
29 import { wallsTouched } from "../src/crew/fold_walls.ts";
30 import { quietLogs } from "./helpers/globals.ts";
31 import { seedAdmin } from "./helpers/admin.ts";
32
33 quietLogs();
34
35 let dataDir = "";
36 let app: App;
37 let alice: User;
38 let repo: Repo;
39
40 beforeAll(async () => {
41 dataDir = await mkdtemp(join(tmpdir(), "forge-trusted-"));
42 app = createApp({ dataDir, port: 0, host: "forge.test" });
43 const made = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" });
44 if (!made.ok) throw new Error(made.error);
45 alice = made.value;
46 const r = await createRepo(app, alice, { name: "lib", visibility: "public" });
47 if (!r.ok) throw new Error(String(r.error));
48 repo = r.value;
49 const first = await commitToBranch(app, alice, repo, { message: "First", files: [{ path: "README.md", content: "hi\n" }] });
50 if (!first.ok) throw new Error(JSON.stringify(first.error));
51 });
52
53 afterAll(async () => {
54 app.close();
55 await rm(dataDir, { recursive: true, force: true });
56 });
57
58 const HOST = "git.open-platform.sh";
59
60 describe("the list", () => {
61 test("the owner's three are there until 2026-12-02, and the same name elsewhere is a stranger", () => {
62 const now = new Date("2026-11-03T00:00:00Z");
63 for (const who of ["tdobbertin-espo", "jmoran", "benjivers"]) {
64 expect(isTrusted(app.db, HOST, who, now)).toBe(true);
65 expect(isTrusted(app.db, "evil.example", who, now)).toBe(false);
66 }
67 expect(listTrusted(app.db).every((s) => s.until.startsWith("2026-12-02"))).toBe(true);
68 });
69
70 test("expiry falls to review on its own, with a note 3 days before, once", () => {
71 expect(isTrusted(app.db, HOST, "jmoran", new Date("2026-12-03T00:00:00Z"))).toBe(false);
72 expect(dueWarning(app.db, new Date("2026-11-20T00:00:00Z"))).toEqual([]);
73 expect(dueWarning(app.db, new Date("2026-11-30T00:00:00Z")).length).toBe(3);
74 expect(warnExpiring(app, new Date("2026-11-30T00:00:00Z"))).toBe(3);
75 expect(warnExpiring(app, new Date("2026-11-30T01:00:00Z"))).toBe(0);
76 expect(app.db.query("SELECT COUNT(*) AS n FROM event WHERE kind = 'trust.expiring'").get()).toEqual({ n: 3 });
77 });
78
79 test("a change is audited, only an admin person may make it, and input is validated", () => {
80 const bob = { ...alice, id: "x", isAdmin: false };
81 expect(addTrust(app, bob, { host: HOST, account: "zed", until: "2027-01-01" }).ok).toBe(false);
82 expect(addTrust(app, alice, { host: "no spaces", account: "zed", until: "2027-01-01" })).toEqual({ ok: false, error: "bad_host" });
83 expect(addTrust(app, alice, { host: HOST, account: "../x", until: "2027-01-01" })).toEqual({ ok: false, error: "bad_account" });
84 expect(addTrust(app, alice, { host: HOST, account: "zed", until: "soon" })).toEqual({ ok: false, error: "bad_date" });
85 expect(addTrust(app, alice, { host: HOST, account: "zed", until: "2020-01-01" })).toEqual({ ok: false, error: "in_the_past" });
86 const added = addTrust(app, alice, { host: `https://${HOST}/`, account: "@Zed", until: "2027-01-01" });
87 if (!added.ok) throw new Error(added.error);
88 expect(added.value).toMatchObject({ host: HOST, account: "zed", setBy: "alice" });
89 const removed = removeTrust(app, alice, added.value.id);
90 expect(removed.ok).toBe(true);
91 expect(isTrusted(app.db, HOST, "zed")).toBe(false);
92 const kinds = app.db.query<{ kind: string }, []>("SELECT kind FROM event WHERE kind LIKE 'trust.%'").all().map((e) => e.kind);
93 expect(kinds).toContain("trust.set");
94 expect(kinds).toContain("trust.removed");
95 });
96 });
97
98 describe("what trust skips, and what it never does", () => {
99 const push = async (branch: string, path: string) => {
100 const done = await commitToBranch(app, alice, repo, {
101 branch,
102 message: path,
103 files: [{ path, content: "x\n" }],
104 });
105 if (!done.ok) throw new Error(JSON.stringify(done.error));
106 return done.value.commitOid;
107 };
108 const pullFrom = async (branch: string, account: string, host = HOST) => {
109 await push(branch, `${branch}.md`);
110 const opened = await openPull(app, alice, repo, { title: branch, baseBranch: "main", headBranch: branch });
111 if (!opened.ok) throw new Error(String(opened.error));
112 recordPullSource(app.db, { pullId: opened.value.id, host, account, via: [] });
113 return getPull(app.db, repo.id, opened.value.number)!;
114 };
115
116 test("a trusted source's pull lands without a person, a guest's does not", async () => {
117 const trusted = await pullFrom("t/one", "jmoran");
118 const guest = await pullFrom("t/two", "jmoran", "other.example");
119 expect(foldsIn(app.db, trusted.id)).toBe(true);
120 expect(landsOnItsOwn(app.db, repo, trusted)).toBe(true);
121 expect(foldsIn(app.db, guest.id)).toBe(false);
122 expect(pullMayRun(app.db, repo, trusted, trusted.headOid)).toBe(true);
123 });
124
125 test("the kill switch stops all folding in, and resuming restores it", async () => {
126 const pull = await pullFrom("t/three", "benjivers");
127 expect(foldsIn(app.db, pull.id)).toBe(true);
128 expect(setFoldInStopped(app, alice, true).ok).toBe(true);
129 expect(foldsIn(app.db, pull.id)).toBe(false);
130 setFoldInStopped(app, alice, false);
131 expect(foldsIn(app.db, pull.id)).toBe(true);
132 });
133
134 test("an expired source's pull waits for review", async () => {
135 const pull = await pullFrom("t/four", "tdobbertin-espo");
136 expect(foldsIn(app.db, pull.id, new Date("2027-01-01T00:00:00Z"))).toBe(false);
137 });
138
139 const walls: [string, string][] = [
140 [".env", "secrets or credentials"],
141 ["config/secrets.json", "secrets or credentials"],
142 ["deploy/server.pem", "secrets or credentials"],
143 ["src/core/sessions.ts", "secrets or credentials"],
144 ["src/core/roles.ts", "permissions or the contract's walls"],
145 ["src/contract/walls.ts", "permissions or the contract's walls"],
146 ["src/core/pull_trust.ts", "permissions or the contract's walls"],
147 ["scripts/cut-release.ts", "release machinery"],
148 ["forge.json", "release machinery"],
149 ["Dockerfile", "release machinery"],
150 ];
151 for (const [file, why] of walls)
152 test(`a pull touching ${file} still waits for a person`, () => {
153 expect(wallsTouched([file])).toEqual([`a trusted source's pull touches ${why}`]);
154 });
155
156 test("ordinary files are no wall", () => {
157 expect(wallsTouched(["src/web/pages/home.ts", "README.md", "test/a.test.ts"])).toEqual([]);
158 });
159 });
160
161 describe("a ring of forges", () => {
162 test("work that passed through a forge is not taken by it again", () => {
163 const names = ["a.test", "b.test", "c.test"];
164 // A lands work by someone on x.test; B follows A, C follows B, A follows C.
165 let via: string[] = [];
166 let origin = "x.test";
167 const trail: string[] = [];
168 for (let hop = 0; hop < 6; hop++) {
169 const self = names[hop % 3]!;
170 if (!takeable(via, self)) break;
171 const trailers = sourceTrailers({ pullId: "p", host: origin, account: "jmoran", via }, self);
172 via = viaOf(`Merge\n\n${trailers}`);
173 origin = self;
174 trail.push(self);
175 }
176 expect(trail).toEqual(["a.test", "b.test", "c.test"]);
177 expect(via).toEqual(["x.test", "a.test", "b.test", "c.test"]);
178 });
179
180 test("a landing says where it came from", () => {
181 const text = sourceTrailers({ pullId: "p", host: HOST, account: "jmoran", via: [] }, "forge.test");
182 expect(text).toContain(`Landed-from: jmoran@${HOST}`);
183 expect(viaOf(text)).toEqual([HOST, "forge.test"]);
184 });
185
186 test("an upstream's address names its forge and account", () => {
187 expect(sourceOfUrl(`https://${HOST}/jmoran/app.git`)).toEqual({ host: HOST, account: "jmoran" });
188 expect(sourceOfUrl("ssh://x/y")).toBeUndefined();
189 });
190
191 test("a pull's source is read back", () => {
192 recordPullSource(app.db, { pullId: "zz", host: HOST, account: "jmoran", via: ["a.test"] });
193 expect(pullSourceOf(app.db, "zz")?.via).toEqual(["a.test"]);
194 expect(trustSource(app.db, { host: HOST, account: "q", until: "2020-01-01", setBy: "t" })).toBe("in_the_past");
195 });
196 });
197
198 describe("the settings page", () => {
199 test("lists the entries with a one-click remove and refuses a stranger", async () => {
200 const { serve } = await import("../src/http/server.ts");
201 const { createSession } = await import("../src/core/sessions.ts");
202 const servers = await serve(app);
203 try {
204 const anon = await fetch(`${servers.url}/settings/trusted`, { redirect: "manual" });
205 await anon.arrayBuffer();
206 expect(anon.status).not.toBe(200);
207 const res = await fetch(`${servers.url}/settings/trusted`, {
208 headers: { cookie: `forge_session=${createSession(app.db, alice.id).token}` },
209 });
210 const text = await res.text();
211 expect(res.status).toBe(200);
212 expect(text).toContain("tdobbertin-espo");
213 expect(text).toContain("/remove");
214 expect(text).toContain("Stop all folding in");
215 } finally {
216 servers.stop();
217 }
218 });
219 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs