@app-vitals/agent-admin 1.32.0 → 1.34.0
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.
- package/openapi.platform.json +473 -1
- package/package.json +2 -2
- package/src/admin-boot.ts +12 -0
- package/src/agents-api.smoke.test.ts +496 -0
- package/src/agents-api.ts +314 -3
- package/src/github-reconcile-wiring.ts +22 -0
- package/src/lost-access-removal.ts +333 -0
- package/src/lost-access-removal.unit.test.ts +467 -0
- package/src/reconcile-planner.ts +26 -11
- package/src/repo-removal-open-work.ts +64 -0
- package/src/repo-removal-open-work.unit.test.ts +105 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import { describe, expect, it, test } from "bun:test";
|
|
2
|
+
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
3
|
+
import type { InstallationReposPort } from "./installation-repos-service.ts";
|
|
4
|
+
import {
|
|
5
|
+
LOST_ACCESS_AUDIT_PLAN_HASH,
|
|
6
|
+
type LostAccessPlanInput,
|
|
7
|
+
LostAccessRemovalService,
|
|
8
|
+
NOT_PRESENT_REASON,
|
|
9
|
+
planLostAccessRemoval,
|
|
10
|
+
STILL_PUSHABLE_REASON,
|
|
11
|
+
} from "./lost-access-removal.ts";
|
|
12
|
+
import type {
|
|
13
|
+
ReconcileAgentRecord,
|
|
14
|
+
ReconcileAuditEntry,
|
|
15
|
+
} from "./reconcile-apply-service.ts";
|
|
16
|
+
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
17
|
+
import { repoLostAccessBlockReason } from "./repo-removal-open-work.ts";
|
|
18
|
+
import {
|
|
19
|
+
type OrgAccessResult,
|
|
20
|
+
SELECTED_REPOS_REASON,
|
|
21
|
+
type UserRepoAccessResult,
|
|
22
|
+
} from "./user-repo-access-resolver.ts";
|
|
23
|
+
|
|
24
|
+
const repo = (fullName: string, canPush: boolean) => ({
|
|
25
|
+
fullName,
|
|
26
|
+
canPush,
|
|
27
|
+
level: canPush ? ("write" as const) : ("read" as const),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const org = (
|
|
31
|
+
name: string,
|
|
32
|
+
repos: ReturnType<typeof repo>[],
|
|
33
|
+
extra: Partial<OrgAccessResult> = {},
|
|
34
|
+
): OrgAccessResult => ({
|
|
35
|
+
org: name,
|
|
36
|
+
status: "ok",
|
|
37
|
+
repos,
|
|
38
|
+
unresolved: 0,
|
|
39
|
+
...extra,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const access = (
|
|
43
|
+
orgs: OrgAccessResult[],
|
|
44
|
+
extra: Partial<UserRepoAccessResult> = {},
|
|
45
|
+
): UserRepoAccessResult => ({
|
|
46
|
+
username: "octo",
|
|
47
|
+
outcome: "ok",
|
|
48
|
+
userVerified: true,
|
|
49
|
+
orgs,
|
|
50
|
+
unresolved: 0,
|
|
51
|
+
complete: orgs.every((o) => o.status === "ok"),
|
|
52
|
+
checkedAt: "2026-11-11T00:00:00.000Z",
|
|
53
|
+
...extra,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const ACME = org("acme", [
|
|
57
|
+
repo("acme/a", true),
|
|
58
|
+
repo("acme/b", false),
|
|
59
|
+
repo("acme/c", false),
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
const base = (
|
|
63
|
+
over: Partial<LostAccessPlanInput> = {},
|
|
64
|
+
): LostAccessPlanInput => ({
|
|
65
|
+
username: "octo",
|
|
66
|
+
access: access([ACME]),
|
|
67
|
+
agentRepos: ["acme/a", "acme/b"],
|
|
68
|
+
installationRepos: { acme: ["acme/a", "acme/b", "acme/c"] },
|
|
69
|
+
...over,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("planLostAccessRemoval", () => {
|
|
73
|
+
test("removes repos the user cannot push to from installation and agent repos", () => {
|
|
74
|
+
const p = planLostAccessRemoval(base());
|
|
75
|
+
expect(p.skipped).toBe(false);
|
|
76
|
+
expect(p.repos).toEqual(["acme/b", "acme/c"]);
|
|
77
|
+
expect(p.installationRemoves).toEqual(["acme/b", "acme/c"]);
|
|
78
|
+
expect(p.configRemoves).toEqual(["acme/b"]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("never adds: a pushable repo missing from the agent stays missing", () => {
|
|
82
|
+
const p = planLostAccessRemoval(
|
|
83
|
+
base({ agentRepos: [], installationRepos: { acme: ["acme/a"] } }),
|
|
84
|
+
);
|
|
85
|
+
expect(p.repos).toEqual([]);
|
|
86
|
+
expect(p.configRemoves).toEqual([]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a repo not listed in a complete org listing counts as no access", () => {
|
|
90
|
+
const p = planLostAccessRemoval(
|
|
91
|
+
base({ agentRepos: ["acme/gone"], installationRepos: {} }),
|
|
92
|
+
);
|
|
93
|
+
expect(p.configRemoves).toEqual(["acme/gone"]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("push checks ignore case", () => {
|
|
97
|
+
const p = planLostAccessRemoval(
|
|
98
|
+
base({ agentRepos: ["ACME/A"], installationRepos: { acme: ["Acme/a"] } }),
|
|
99
|
+
);
|
|
100
|
+
expect(p.repos).toEqual([]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test.each([
|
|
104
|
+
["partial", { status: "partial" as const, unresolved: 1 }],
|
|
105
|
+
["failed", { status: "rate_limited" as const }],
|
|
106
|
+
[
|
|
107
|
+
"selected-only",
|
|
108
|
+
{ status: "error" as const, reason: SELECTED_REPOS_REASON },
|
|
109
|
+
],
|
|
110
|
+
])("skips a %s org and reports it", (_label, extra) => {
|
|
111
|
+
const p = planLostAccessRemoval(
|
|
112
|
+
base({ access: access([org("acme", [repo("acme/a", true)], extra)]) }),
|
|
113
|
+
);
|
|
114
|
+
expect(p.repos).toEqual([]);
|
|
115
|
+
expect(p.skippedOrgs).toHaveLength(1);
|
|
116
|
+
expect(p.skippedOrgs[0]?.org).toBe("acme");
|
|
117
|
+
expect(p.warnings[0]).toContain("acme");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("selected-only reason matches the reconcile planner wording", () => {
|
|
121
|
+
const p = planLostAccessRemoval(
|
|
122
|
+
base({
|
|
123
|
+
access: access([org("acme", [], { reason: SELECTED_REPOS_REASON })]),
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
expect(p.skippedOrgs[0]?.reason).toContain("selected repositories");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("an org missing from the access result is skipped, not emptied", () => {
|
|
130
|
+
const p = planLostAccessRemoval(
|
|
131
|
+
base({
|
|
132
|
+
agentRepos: ["other/x"],
|
|
133
|
+
installationRepos: { other: ["other/x"] },
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
expect(p.repos).toEqual([]);
|
|
137
|
+
expect(p.skippedOrgs).toEqual([
|
|
138
|
+
{ org: "other", reason: "org was not checked" },
|
|
139
|
+
]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test.each([
|
|
143
|
+
["unbound", { username: " " }, "no GitHub username"],
|
|
144
|
+
[
|
|
145
|
+
"not found",
|
|
146
|
+
{ access: access([ACME], { outcome: "user_not_found" }) },
|
|
147
|
+
"not found",
|
|
148
|
+
],
|
|
149
|
+
[
|
|
150
|
+
"unverified",
|
|
151
|
+
{ access: access([ACME], { userVerified: false }) },
|
|
152
|
+
"verified",
|
|
153
|
+
],
|
|
154
|
+
["unresolved", { access: null }, "not resolved"],
|
|
155
|
+
])("a %s user skips the whole plan", (_l, over, reason) => {
|
|
156
|
+
const p = planLostAccessRemoval(base(over as Partial<LostAccessPlanInput>));
|
|
157
|
+
expect(p.skipped).toBe(true);
|
|
158
|
+
expect(p.skipReason).toContain(reason);
|
|
159
|
+
expect(p.repos).toEqual([]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("requested limits candidates and reports the ones left alone", () => {
|
|
163
|
+
const p = planLostAccessRemoval(
|
|
164
|
+
base({
|
|
165
|
+
agentRepos: ["acme/a", "acme/b", "other/x"],
|
|
166
|
+
requested: ["acme/a", "acme/b", "acme/zzz", "other/x"],
|
|
167
|
+
installationRepos: { acme: ["acme/a", "acme/b", "acme/c"] },
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
expect(p.repos).toEqual(["acme/b"]);
|
|
171
|
+
expect(p.installationRemoves).toEqual(["acme/b"]);
|
|
172
|
+
expect(p.notRemoved).toEqual([
|
|
173
|
+
{ repo: "acme/a", reason: STILL_PUSHABLE_REASON },
|
|
174
|
+
{ repo: "acme/zzz", reason: NOT_PRESENT_REASON },
|
|
175
|
+
{
|
|
176
|
+
repo: "other/x",
|
|
177
|
+
reason: "removals skipped for org: org was not checked",
|
|
178
|
+
},
|
|
179
|
+
]);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// ─── Service ────────────────────────────────────────────────────────────────
|
|
184
|
+
|
|
185
|
+
function harness(
|
|
186
|
+
opts: {
|
|
187
|
+
agent?: Partial<ReconcileAgentRecord> | null;
|
|
188
|
+
access?: Awaited<ReturnType<GithubAccessPreviewFn>>;
|
|
189
|
+
installed?: { org: string; repos: string[] }[];
|
|
190
|
+
removeFails?: boolean;
|
|
191
|
+
openWork?: Partial<OpenWorkPort> | null;
|
|
192
|
+
} = {},
|
|
193
|
+
) {
|
|
194
|
+
const calls: unknown[][] = [];
|
|
195
|
+
const audits: ReconcileAuditEntry[] = [];
|
|
196
|
+
let repos = opts.agent?.repos ?? ["acme/a", "acme/b"];
|
|
197
|
+
const agent: ReconcileAgentRecord | null =
|
|
198
|
+
opts.agent === null
|
|
199
|
+
? null
|
|
200
|
+
: {
|
|
201
|
+
id: "ag",
|
|
202
|
+
githubUsername: "octo",
|
|
203
|
+
appId: 7,
|
|
204
|
+
...opts.agent,
|
|
205
|
+
repos,
|
|
206
|
+
};
|
|
207
|
+
const installation: Pick<
|
|
208
|
+
InstallationReposPort,
|
|
209
|
+
"listInstalled" | "removeRepos"
|
|
210
|
+
> = {
|
|
211
|
+
listInstalled: async (appId) => {
|
|
212
|
+
calls.push(["listInstalled", appId]);
|
|
213
|
+
return (
|
|
214
|
+
opts.installed ?? [
|
|
215
|
+
{ org: "acme", repos: ["acme/a", "acme/b", "acme/c"] },
|
|
216
|
+
]
|
|
217
|
+
);
|
|
218
|
+
},
|
|
219
|
+
removeRepos: async (appId, o, r) => {
|
|
220
|
+
calls.push(["removeRepos", appId, o, [...r]]);
|
|
221
|
+
if (opts.removeFails) throw new Error("github says no");
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
const openWork: OpenWorkPort | undefined =
|
|
225
|
+
opts.openWork === null
|
|
226
|
+
? undefined
|
|
227
|
+
: {
|
|
228
|
+
listOpenWork: async (...a) => {
|
|
229
|
+
calls.push(["list", ...a]);
|
|
230
|
+
return [{ id: "T-1", repo: "acme/b", title: "fix" }];
|
|
231
|
+
},
|
|
232
|
+
blockOpenWork: async (...a) => {
|
|
233
|
+
calls.push(["block", ...a]);
|
|
234
|
+
},
|
|
235
|
+
...opts.openWork,
|
|
236
|
+
};
|
|
237
|
+
const svc = new LostAccessRemovalService({
|
|
238
|
+
agents: {
|
|
239
|
+
get: async () => (agent ? { ...agent, repos } : null),
|
|
240
|
+
setRepos: async (_id, next) => {
|
|
241
|
+
calls.push(["setRepos", [...next]]);
|
|
242
|
+
repos = next;
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
access: async () => opts.access ?? { status: "ok", access: access([ACME]) },
|
|
246
|
+
installation,
|
|
247
|
+
audit: {
|
|
248
|
+
write: async (e) => {
|
|
249
|
+
calls.push(["audit"]);
|
|
250
|
+
audits.push(e);
|
|
251
|
+
return `audit-${audits.length}`;
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
...(openWork ? { openWork } : {}),
|
|
255
|
+
});
|
|
256
|
+
return { svc, calls, audits, repos: () => repos };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const removeInput = (repos: string[], extra = {}) => ({
|
|
260
|
+
agentId: "ag",
|
|
261
|
+
caller: "root",
|
|
262
|
+
repos,
|
|
263
|
+
...extra,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe("LostAccessRemovalService.preview", () => {
|
|
267
|
+
it("returns the repos that would be removed plus their open work, changing nothing", async () => {
|
|
268
|
+
const { svc, calls, audits } = harness();
|
|
269
|
+
const r = await svc.preview("ag");
|
|
270
|
+
expect(r.status).toBe("previewed");
|
|
271
|
+
if (r.status !== "previewed") return;
|
|
272
|
+
expect(r.plan.repos).toEqual(["acme/b", "acme/c"]);
|
|
273
|
+
expect(r.openWork).toEqual([{ id: "T-1", repo: "acme/b", title: "fix" }]);
|
|
274
|
+
expect(calls.map((c) => c[0])).toEqual(["listInstalled", "list"]);
|
|
275
|
+
expect(audits).toEqual([]);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("a failing open-work port is a warning", async () => {
|
|
279
|
+
const { svc } = harness({
|
|
280
|
+
openWork: {
|
|
281
|
+
listOpenWork: async () => {
|
|
282
|
+
throw new Error("store down");
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
const r = await svc.preview("ag");
|
|
287
|
+
if (r.status !== "previewed") throw new Error(r.status);
|
|
288
|
+
expect(r.openWork).toEqual([]);
|
|
289
|
+
expect(r.warnings.join()).toContain("store down");
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("is skipped for an unbound user with no GitHub calls", async () => {
|
|
293
|
+
const { svc, calls } = harness({ agent: { githubUsername: null } });
|
|
294
|
+
expect(await svc.preview("ag")).toMatchObject({ status: "skipped" });
|
|
295
|
+
expect(calls).toEqual([]);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("is skipped for unconfigured Apps and for a missing GH_APP_ID", async () => {
|
|
299
|
+
expect(
|
|
300
|
+
await harness({ access: { status: "unconfigured" } }).svc.preview("ag"),
|
|
301
|
+
).toEqual({ status: "skipped", reason: "GitHub Apps are not configured" });
|
|
302
|
+
const noApp = await harness({ agent: { appId: undefined } }).svc.preview(
|
|
303
|
+
"ag",
|
|
304
|
+
);
|
|
305
|
+
expect(noApp).toMatchObject({ status: "skipped" });
|
|
306
|
+
expect(JSON.stringify(noApp)).toContain("GH_APP_ID");
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("agent_not_found for an unknown agent", async () => {
|
|
310
|
+
expect(await harness({ agent: null }).svc.preview("ag")).toEqual({
|
|
311
|
+
status: "agent_not_found",
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
describe("LostAccessRemovalService.remove", () => {
|
|
317
|
+
it("removes installation first, then agent repos, then audits, then blocks", async () => {
|
|
318
|
+
const { svc, calls, audits, repos } = harness();
|
|
319
|
+
const r = await svc.remove(removeInput(["acme/b", "acme/c"]));
|
|
320
|
+
expect(r).toMatchObject({
|
|
321
|
+
status: "applied",
|
|
322
|
+
auditId: "audit-1",
|
|
323
|
+
warnings: [],
|
|
324
|
+
});
|
|
325
|
+
expect(calls).toEqual([
|
|
326
|
+
["listInstalled", 7],
|
|
327
|
+
["removeRepos", 7, "acme", ["acme/b", "acme/c"]],
|
|
328
|
+
["setRepos", ["acme/a"]],
|
|
329
|
+
["audit"],
|
|
330
|
+
["block", "ag", ["acme/b"], repoLostAccessBlockReason("acme/b")],
|
|
331
|
+
["block", "ag", ["acme/c"], repoLostAccessBlockReason("acme/c")],
|
|
332
|
+
]);
|
|
333
|
+
expect(repos()).toEqual(["acme/a"]);
|
|
334
|
+
expect(audits).toEqual([
|
|
335
|
+
{
|
|
336
|
+
agentId: "ag",
|
|
337
|
+
caller: "root",
|
|
338
|
+
planHash: LOST_ACCESS_AUDIT_PLAN_HASH,
|
|
339
|
+
diff: {
|
|
340
|
+
installationAdds: [],
|
|
341
|
+
installationRemoves: ["acme/b", "acme/c"],
|
|
342
|
+
configAdds: [],
|
|
343
|
+
configRemoves: ["acme/b"],
|
|
344
|
+
},
|
|
345
|
+
result: { status: "applied" },
|
|
346
|
+
},
|
|
347
|
+
]);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("leaves a named repo the user can still push to alone and reports it", async () => {
|
|
351
|
+
const { svc, repos } = harness();
|
|
352
|
+
const r = await svc.remove(removeInput(["acme/a", "acme/b"]));
|
|
353
|
+
if (r.status !== "applied") throw new Error(r.status);
|
|
354
|
+
expect(r.plan.repos).toEqual(["acme/b"]);
|
|
355
|
+
expect(r.plan.notRemoved).toEqual([
|
|
356
|
+
{ repo: "acme/a", reason: STILL_PUSHABLE_REASON },
|
|
357
|
+
]);
|
|
358
|
+
expect(repos()).toEqual(["acme/a"]);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("noop with no audit when nothing named still lacks access", async () => {
|
|
362
|
+
const { svc, calls, audits } = harness();
|
|
363
|
+
const r = await svc.remove(removeInput(["acme/a"]));
|
|
364
|
+
expect(r.status).toBe("noop");
|
|
365
|
+
expect(audits).toEqual([]);
|
|
366
|
+
expect(calls.map((c) => c[0])).toEqual(["listInstalled"]);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("a skipped call changes nothing and writes no audit row", async () => {
|
|
370
|
+
for (const h of [
|
|
371
|
+
harness({ agent: { githubUsername: null } }),
|
|
372
|
+
harness({
|
|
373
|
+
access: {
|
|
374
|
+
status: "ok",
|
|
375
|
+
access: access([ACME], { userVerified: false }),
|
|
376
|
+
},
|
|
377
|
+
}),
|
|
378
|
+
harness({
|
|
379
|
+
access: {
|
|
380
|
+
status: "user_not_found",
|
|
381
|
+
access: access([], { outcome: "user_not_found" }),
|
|
382
|
+
},
|
|
383
|
+
}),
|
|
384
|
+
]) {
|
|
385
|
+
const r = await h.svc.remove(removeInput(["acme/b"]));
|
|
386
|
+
expect(r.status).toBe("skipped");
|
|
387
|
+
expect(h.audits).toEqual([]);
|
|
388
|
+
expect(h.calls.filter((c) => c[0] !== "listInstalled")).toEqual([]);
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("skipped orgs are reported and nothing there is removed", async () => {
|
|
393
|
+
const { svc, audits } = harness({
|
|
394
|
+
access: {
|
|
395
|
+
status: "ok",
|
|
396
|
+
access: access([
|
|
397
|
+
org("acme", [], { reason: SELECTED_REPOS_REASON, status: "error" }),
|
|
398
|
+
]),
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
const r = await svc.remove(removeInput(["acme/b"]));
|
|
402
|
+
expect(r.status).toBe("noop");
|
|
403
|
+
if (r.status !== "noop") return;
|
|
404
|
+
expect(r.plan.skippedOrgs[0]?.org).toBe("acme");
|
|
405
|
+
expect(audits).toEqual([]);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("more than 10 removals need confirmRemovals", async () => {
|
|
409
|
+
const many = Array.from({ length: 11 }, (_, i) => `acme/r${i}`);
|
|
410
|
+
const h = harness({
|
|
411
|
+
agent: { repos: many },
|
|
412
|
+
installed: [{ org: "acme", repos: many }],
|
|
413
|
+
});
|
|
414
|
+
const r = await h.svc.remove(removeInput(many));
|
|
415
|
+
expect(r).toMatchObject({ status: "removal_cap", removals: 11 });
|
|
416
|
+
expect(h.audits).toEqual([]);
|
|
417
|
+
expect(h.repos()).toEqual(many);
|
|
418
|
+
const ok = await h.svc.remove(removeInput(many, { confirmRemovals: true }));
|
|
419
|
+
expect(ok.status).toBe("applied");
|
|
420
|
+
expect(h.repos()).toEqual([]);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("an open-work port failure is a warning, not a failed call", async () => {
|
|
424
|
+
const { svc, audits } = harness({
|
|
425
|
+
openWork: {
|
|
426
|
+
blockOpenWork: async () => {
|
|
427
|
+
throw new Error("store down");
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
});
|
|
431
|
+
const r = await svc.remove(removeInput(["acme/b"]));
|
|
432
|
+
expect(r.status).toBe("applied");
|
|
433
|
+
if (r.status !== "applied") return;
|
|
434
|
+
expect(r.warnings[0]).toContain("acme/b");
|
|
435
|
+
expect(r.warnings[0]).toContain("store down");
|
|
436
|
+
expect(audits).toHaveLength(1);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("works without an open-work port", async () => {
|
|
440
|
+
const r = await harness({ openWork: null }).svc.remove(
|
|
441
|
+
removeInput(["acme/b"]),
|
|
442
|
+
);
|
|
443
|
+
expect(r).toMatchObject({ status: "applied", warnings: [] });
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it("a GitHub failure writes a failed audit with only what landed and blocks nothing", async () => {
|
|
447
|
+
const { svc, calls, audits, repos } = harness({ removeFails: true });
|
|
448
|
+
const r = await svc.remove(removeInput(["acme/b"]));
|
|
449
|
+
expect(r).toMatchObject({ status: "failed", auditId: "audit-1" });
|
|
450
|
+
expect(repos()).toEqual(["acme/a", "acme/b"]);
|
|
451
|
+
expect(audits[0]?.result).toEqual({
|
|
452
|
+
status: "failed",
|
|
453
|
+
error: "github says no",
|
|
454
|
+
});
|
|
455
|
+
expect(audits[0]?.diff.installationRemoves).toEqual([]);
|
|
456
|
+
expect(audits[0]?.diff.configRemoves).toEqual([]);
|
|
457
|
+
expect(calls.some((c) => c[0] === "block")).toBe(false);
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it("removes an agent repo even when the App is not installed on its org", async () => {
|
|
461
|
+
const { svc, calls, repos } = harness({ installed: [] });
|
|
462
|
+
const r = await svc.remove(removeInput(["acme/b"]));
|
|
463
|
+
expect(r.status).toBe("applied");
|
|
464
|
+
expect(calls.some((c) => c[0] === "removeRepos")).toBe(false);
|
|
465
|
+
expect(repos()).toEqual(["acme/a"]);
|
|
466
|
+
});
|
|
467
|
+
});
|
package/src/reconcile-planner.ts
CHANGED
|
@@ -84,7 +84,13 @@ export function repoBasename(fullName: string): string {
|
|
|
84
84
|
|
|
85
85
|
const sorted = (xs: Iterable<string>): string[] => [...xs].sort();
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Why removals in one org must be skipped (unknown is never none): the org
|
|
89
|
+
* was not checked, its listing is partial or failed, or the lookup App is
|
|
90
|
+
* installed on selected repositories only. Undefined when the listing is
|
|
91
|
+
* complete. Shared with the lost-access removal (IRM-1.3).
|
|
92
|
+
*/
|
|
93
|
+
export function orgSkipReason(
|
|
88
94
|
result: OrgAccessResult | undefined,
|
|
89
95
|
): string | undefined {
|
|
90
96
|
if (!result) return "org was not checked";
|
|
@@ -148,18 +154,27 @@ function skippedPlan(username: string, reason: string): ReconcilePlan {
|
|
|
148
154
|
});
|
|
149
155
|
}
|
|
150
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Why a whole plan must be skipped: no bound user, access not resolved, user
|
|
159
|
+
* not found or not verified. Undefined when the access result is usable.
|
|
160
|
+
* Shared with the lost-access removal (IRM-1.3).
|
|
161
|
+
*/
|
|
162
|
+
export function accessSkipReason(
|
|
163
|
+
username: string | null | undefined,
|
|
164
|
+
access: UserRepoAccessResult | null,
|
|
165
|
+
): string | undefined {
|
|
166
|
+
if (!username?.trim()) return "no GitHub username bound to the agent";
|
|
167
|
+
if (!access) return "access was not resolved";
|
|
168
|
+
if (access.outcome === "user_not_found") return "GitHub user not found";
|
|
169
|
+
if (!access.userVerified) return "GitHub user could not be verified";
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
151
173
|
export function planReconcile(input: ReconcilePlanInput): ReconcilePlan {
|
|
152
174
|
const username = input.username?.trim() ?? "";
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const access = input.access;
|
|
156
|
-
if (!access) return skippedPlan(username, "access was not resolved");
|
|
157
|
-
if (access.outcome === "user_not_found") {
|
|
158
|
-
return skippedPlan(username, "GitHub user not found");
|
|
159
|
-
}
|
|
160
|
-
if (!access.userVerified) {
|
|
161
|
-
return skippedPlan(username, "GitHub user could not be verified");
|
|
162
|
-
}
|
|
175
|
+
const skipReason = accessSkipReason(username, input.access);
|
|
176
|
+
if (skipReason) return skippedPlan(username, skipReason);
|
|
177
|
+
const access = input.access as UserRepoAccessResult;
|
|
163
178
|
|
|
164
179
|
const orgResults = new Map(access.orgs.map((o) => [o.org, o]));
|
|
165
180
|
const pushable = new Set<string>();
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/repo-removal-open-work.ts
|
|
3
|
+
* Shared preview + block logic for open work on a repo removed from an agent's
|
|
4
|
+
* own repos (IRM-1.7; reused by IRM-1.3). Both helpers are no-ops without a
|
|
5
|
+
* port, and a port failure is returned as a warning, never thrown, so the
|
|
6
|
+
* removal itself always succeeds. Re-adding a repo never unblocks anything.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { OpenWorkItem, OpenWorkPort } from "./reconcile-open-work.ts";
|
|
10
|
+
|
|
11
|
+
export function repoRemovedFromAgentBlockReason(repo: string): string {
|
|
12
|
+
return `repo ${repo} was removed from the agent's repos`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** IRM-1.3: reason for work on a repo the agent's bound user can no longer push to. */
|
|
16
|
+
export function repoLostAccessBlockReason(repo: string): string {
|
|
17
|
+
return `repo ${repo} was removed from the agent because its bound GitHub user lost push access`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface RepoRemovalPreview {
|
|
21
|
+
openWork: OpenWorkItem[];
|
|
22
|
+
warnings: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Read-only: the open work that removing `repos` would block. */
|
|
26
|
+
export async function previewRepoRemovalWork(
|
|
27
|
+
port: OpenWorkPort | undefined,
|
|
28
|
+
agentId: string,
|
|
29
|
+
repos: readonly string[],
|
|
30
|
+
): Promise<RepoRemovalPreview> {
|
|
31
|
+
if (!port || repos.length === 0) return { openWork: [], warnings: [] };
|
|
32
|
+
try {
|
|
33
|
+
return { openWork: await port.listOpenWork(agentId, repos), warnings: [] };
|
|
34
|
+
} catch (err) {
|
|
35
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
36
|
+
return {
|
|
37
|
+
openWork: [],
|
|
38
|
+
warnings: [`open work could not be listed: ${msg}`],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Blocks the agent's open work on each removed repo; returns warnings.
|
|
45
|
+
* `reason` builds the human-facing block reason per repo.
|
|
46
|
+
*/
|
|
47
|
+
export async function blockRepoRemovalWork(
|
|
48
|
+
port: OpenWorkPort | undefined,
|
|
49
|
+
agentId: string,
|
|
50
|
+
repos: readonly string[],
|
|
51
|
+
reason: (repo: string) => string = repoRemovedFromAgentBlockReason,
|
|
52
|
+
): Promise<string[]> {
|
|
53
|
+
if (!port) return [];
|
|
54
|
+
const warnings: string[] = [];
|
|
55
|
+
for (const repo of repos) {
|
|
56
|
+
try {
|
|
57
|
+
await port.blockOpenWork(agentId, [repo], reason(repo));
|
|
58
|
+
} catch (err) {
|
|
59
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
60
|
+
warnings.push(`open work for ${repo} was not blocked: ${msg}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return warnings;
|
|
64
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
3
|
+
import {
|
|
4
|
+
blockRepoRemovalWork,
|
|
5
|
+
previewRepoRemovalWork,
|
|
6
|
+
repoLostAccessBlockReason,
|
|
7
|
+
repoRemovedFromAgentBlockReason,
|
|
8
|
+
} from "./repo-removal-open-work.ts";
|
|
9
|
+
|
|
10
|
+
const ITEMS = [{ id: "T-1", repo: "a/one", title: "t" }];
|
|
11
|
+
|
|
12
|
+
function port(over: Partial<OpenWorkPort> = {}) {
|
|
13
|
+
const calls: unknown[][] = [];
|
|
14
|
+
const p: OpenWorkPort = {
|
|
15
|
+
listOpenWork: async (...a) => {
|
|
16
|
+
calls.push(["list", ...a]);
|
|
17
|
+
return ITEMS;
|
|
18
|
+
},
|
|
19
|
+
blockOpenWork: async (...a) => {
|
|
20
|
+
calls.push(["block", ...a]);
|
|
21
|
+
},
|
|
22
|
+
...over,
|
|
23
|
+
};
|
|
24
|
+
return { p, calls };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("previewRepoRemovalWork", () => {
|
|
28
|
+
it("lists open work from the port", async () => {
|
|
29
|
+
const { p } = port();
|
|
30
|
+
expect(await previewRepoRemovalWork(p, "ag", ["a/one"])).toEqual({
|
|
31
|
+
openWork: ITEMS,
|
|
32
|
+
warnings: [],
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
it("is empty without a port", async () => {
|
|
36
|
+
expect(await previewRepoRemovalWork(undefined, "ag", ["a/one"])).toEqual({
|
|
37
|
+
openWork: [],
|
|
38
|
+
warnings: [],
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
it("turns a port failure into a warning", async () => {
|
|
42
|
+
const { p } = port({
|
|
43
|
+
listOpenWork: async () => {
|
|
44
|
+
throw new Error("down");
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
const r = await previewRepoRemovalWork(p, "ag", ["a/one"]);
|
|
48
|
+
expect(r.openWork).toEqual([]);
|
|
49
|
+
expect(r.warnings[0]).toContain("down");
|
|
50
|
+
});
|
|
51
|
+
it("never blocks", async () => {
|
|
52
|
+
const { p, calls } = port();
|
|
53
|
+
await previewRepoRemovalWork(p, "ag", ["a/one"]);
|
|
54
|
+
expect(calls.some((c) => c[0] === "block")).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("blockRepoRemovalWork", () => {
|
|
59
|
+
it("blocks each repo with a reason naming it", async () => {
|
|
60
|
+
const { p, calls } = port();
|
|
61
|
+
expect(await blockRepoRemovalWork(p, "ag", ["a/one", "b/two"])).toEqual([]);
|
|
62
|
+
expect(calls).toEqual([
|
|
63
|
+
["block", "ag", ["a/one"], repoRemovedFromAgentBlockReason("a/one")],
|
|
64
|
+
["block", "ag", ["b/two"], repoRemovedFromAgentBlockReason("b/two")],
|
|
65
|
+
]);
|
|
66
|
+
expect(repoRemovedFromAgentBlockReason("a/one")).toContain("a/one");
|
|
67
|
+
});
|
|
68
|
+
it("does nothing without a port", async () => {
|
|
69
|
+
expect(await blockRepoRemovalWork(undefined, "ag", ["a/one"])).toEqual([]);
|
|
70
|
+
});
|
|
71
|
+
it("warns on port failure and continues with the next repo", async () => {
|
|
72
|
+
let n = 0;
|
|
73
|
+
const { p } = port({
|
|
74
|
+
blockOpenWork: async () => {
|
|
75
|
+
if (n++ === 0) throw new Error("boom");
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
const w = await blockRepoRemovalWork(p, "ag", ["a/one", "b/two"]);
|
|
79
|
+
expect(w).toHaveLength(1);
|
|
80
|
+
expect(w[0]).toContain("a/one");
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("blockRepoRemovalWork with a lost-access reason (IRM-1.3)", () => {
|
|
85
|
+
it("names the repo and that the bound user lost access", () => {
|
|
86
|
+
const r = repoLostAccessBlockReason("a/one");
|
|
87
|
+
expect(r).toContain("a/one");
|
|
88
|
+
expect(r).toContain("lost");
|
|
89
|
+
expect(r).toContain("access");
|
|
90
|
+
expect(r).not.toBe(repoRemovedFromAgentBlockReason("a/one"));
|
|
91
|
+
});
|
|
92
|
+
it("blocks each repo with the given reason", async () => {
|
|
93
|
+
const { p, calls } = port();
|
|
94
|
+
await blockRepoRemovalWork(
|
|
95
|
+
p,
|
|
96
|
+
"ag",
|
|
97
|
+
["a/one", "b/two"],
|
|
98
|
+
repoLostAccessBlockReason,
|
|
99
|
+
);
|
|
100
|
+
expect(calls).toEqual([
|
|
101
|
+
["block", "ag", ["a/one"], repoLostAccessBlockReason("a/one")],
|
|
102
|
+
["block", "ag", ["b/two"], repoLostAccessBlockReason("b/two")],
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
105
|
+
});
|