@app-vitals/agent-admin 1.33.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 +368 -0
- package/package.json +2 -2
- package/src/admin-boot.ts +9 -0
- package/src/agents-api.smoke.test.ts +277 -1
- package/src/agents-api.ts +226 -0
- 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 +11 -6
- package/src/repo-removal-open-work.unit.test.ts +24 -0
|
@@ -26,6 +26,7 @@ import { createAdminApp, parseAdminApiKeys } from "./agents-api.ts";
|
|
|
26
26
|
import { PLATFORM_OPS_ROUTES } from "./api-auth.ts";
|
|
27
27
|
import {
|
|
28
28
|
createGithubInstallationRepos,
|
|
29
|
+
createGithubLostAccessRemoval,
|
|
29
30
|
createGithubReconcile,
|
|
30
31
|
} from "./github-reconcile-wiring.ts";
|
|
31
32
|
import {
|
|
@@ -35,7 +36,10 @@ import {
|
|
|
35
36
|
fakePrisma,
|
|
36
37
|
} from "./github-reconcile-wiring.unit.test.ts";
|
|
37
38
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
38
|
-
import {
|
|
39
|
+
import {
|
|
40
|
+
repoLostAccessBlockReason,
|
|
41
|
+
repoRemovedFromAgentBlockReason,
|
|
42
|
+
} from "./repo-removal-open-work.ts";
|
|
39
43
|
import type { SpecialtyType } from "./specialty-types.ts";
|
|
40
44
|
import { identityCrypto } from "./token-crypto.ts";
|
|
41
45
|
|
|
@@ -7776,3 +7780,275 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7776
7780
|
expect((await app.request(url(), { headers: asAdmin })).status).toBe(503);
|
|
7777
7781
|
});
|
|
7778
7782
|
});
|
|
7783
|
+
|
|
7784
|
+
// ─── IRM-1.3: /agents/:id/github-lost-access ──────────────────────────────────
|
|
7785
|
+
|
|
7786
|
+
describe("admin API — remove repos the bound user lost access to (IRM-1.3)", () => {
|
|
7787
|
+
const OPS = "irm13-ops-token";
|
|
7788
|
+
const AGENT_KEY = "irm13-agent-token";
|
|
7789
|
+
|
|
7790
|
+
function setup(
|
|
7791
|
+
opts: {
|
|
7792
|
+
repos?: string[];
|
|
7793
|
+
installed?: string[];
|
|
7794
|
+
username?: string | null;
|
|
7795
|
+
known?: boolean;
|
|
7796
|
+
openWork?: Partial<OpenWorkPort> | null;
|
|
7797
|
+
access?: AdminDeps["githubAccessPreview"];
|
|
7798
|
+
} = {},
|
|
7799
|
+
) {
|
|
7800
|
+
const state = {
|
|
7801
|
+
repos: [...(opts.repos ?? ["acme/api", "acme/old"])],
|
|
7802
|
+
installed: [...(opts.installed ?? ["acme/api", "acme/old", "acme/gone"])],
|
|
7803
|
+
};
|
|
7804
|
+
const audits: Array<Record<string, unknown>> = [];
|
|
7805
|
+
const calls: unknown[][] = [];
|
|
7806
|
+
const prisma = {
|
|
7807
|
+
agent: {
|
|
7808
|
+
findUnique: async () =>
|
|
7809
|
+
opts.known === false
|
|
7810
|
+
? null
|
|
7811
|
+
: {
|
|
7812
|
+
id: AGENT_ID,
|
|
7813
|
+
repos: [...state.repos],
|
|
7814
|
+
githubUsername:
|
|
7815
|
+
opts.username === undefined ? "dev" : opts.username,
|
|
7816
|
+
},
|
|
7817
|
+
update: async (args: { data: { repos: string[] } }) => {
|
|
7818
|
+
calls.push(["setRepos", args.data.repos]);
|
|
7819
|
+
state.repos = args.data.repos;
|
|
7820
|
+
},
|
|
7821
|
+
},
|
|
7822
|
+
gitHubReconcileAudit: {
|
|
7823
|
+
create: async (args: { data: Record<string, unknown> }) => {
|
|
7824
|
+
calls.push(["audit"]);
|
|
7825
|
+
audits.push(args.data);
|
|
7826
|
+
return { id: `audit-${audits.length}` };
|
|
7827
|
+
},
|
|
7828
|
+
},
|
|
7829
|
+
} as unknown as Parameters<
|
|
7830
|
+
typeof createGithubLostAccessRemoval
|
|
7831
|
+
>[0]["prisma"];
|
|
7832
|
+
const openWork: OpenWorkPort | undefined =
|
|
7833
|
+
opts.openWork === null
|
|
7834
|
+
? undefined
|
|
7835
|
+
: {
|
|
7836
|
+
listOpenWork: async (...a) => {
|
|
7837
|
+
calls.push(["list", ...a]);
|
|
7838
|
+
return [{ id: "T-1", repo: "acme/old", title: "fix it" }];
|
|
7839
|
+
},
|
|
7840
|
+
blockOpenWork: async (...a) => {
|
|
7841
|
+
calls.push(["block", ...a]);
|
|
7842
|
+
},
|
|
7843
|
+
...opts.openWork,
|
|
7844
|
+
};
|
|
7845
|
+
const githubLostAccessRemoval = createGithubLostAccessRemoval({
|
|
7846
|
+
prisma,
|
|
7847
|
+
appService: { get: async () => null, getPrivateKey: async () => null },
|
|
7848
|
+
access: opts.access ?? accessFor(["acme/api"]),
|
|
7849
|
+
agentEnv: envWith({ GH_APP_ID: "42" }),
|
|
7850
|
+
installationRepos: {
|
|
7851
|
+
listInstalled: async () => [
|
|
7852
|
+
{ org: "acme", repos: [...state.installed] },
|
|
7853
|
+
],
|
|
7854
|
+
listRepos: async () => ({ acme: [...state.installed] }),
|
|
7855
|
+
addRepos: async () => {},
|
|
7856
|
+
removeRepos: async (_appId, org, repos) => {
|
|
7857
|
+
calls.push(["removeRepos", org, [...repos]]);
|
|
7858
|
+
state.installed = state.installed.filter((r) => !repos.includes(r));
|
|
7859
|
+
},
|
|
7860
|
+
},
|
|
7861
|
+
...(openWork ? { openWork } : {}),
|
|
7862
|
+
});
|
|
7863
|
+
const base = makeMockDeps();
|
|
7864
|
+
const app = createAdminApp({
|
|
7865
|
+
...base,
|
|
7866
|
+
adminApiKeys: parseAdminApiKeys(
|
|
7867
|
+
`root:${ADMIN_API_KEY}:*,operator:${OPS}:platform.ops`,
|
|
7868
|
+
),
|
|
7869
|
+
agentTokenService: {
|
|
7870
|
+
...base.agentTokenService,
|
|
7871
|
+
validate: async (t: string) =>
|
|
7872
|
+
t === AGENT_KEY ? { agentId: AGENT_ID } : null,
|
|
7873
|
+
},
|
|
7874
|
+
githubLostAccessRemoval,
|
|
7875
|
+
});
|
|
7876
|
+
return { app, audits, calls, state };
|
|
7877
|
+
}
|
|
7878
|
+
|
|
7879
|
+
const asAdmin = { Authorization: `Bearer ${ADMIN_API_KEY}` };
|
|
7880
|
+
const previewUrl = `/agents/${AGENT_ID}/github-lost-access/preview`;
|
|
7881
|
+
const removeUrl = `/agents/${AGENT_ID}/github-lost-access/remove`;
|
|
7882
|
+
const remove = (
|
|
7883
|
+
app: ReturnType<typeof createAdminApp>,
|
|
7884
|
+
body: unknown,
|
|
7885
|
+
headers: Record<string, string> = asAdmin,
|
|
7886
|
+
) =>
|
|
7887
|
+
app.request(removeUrl, {
|
|
7888
|
+
method: "POST",
|
|
7889
|
+
headers: { "Content-Type": "application/json", ...headers },
|
|
7890
|
+
body: JSON.stringify(body),
|
|
7891
|
+
});
|
|
7892
|
+
|
|
7893
|
+
it("preview lists the repos that would be removed and their open work, changing nothing", async () => {
|
|
7894
|
+
const { app, audits, calls, state } = setup();
|
|
7895
|
+
const res = await app.request(previewUrl, { headers: asAdmin });
|
|
7896
|
+
expect(res.status).toBe(200);
|
|
7897
|
+
expect(await res.json()).toEqual({
|
|
7898
|
+
status: "previewed",
|
|
7899
|
+
repos: ["acme/gone", "acme/old"],
|
|
7900
|
+
installationRemoves: ["acme/gone", "acme/old"],
|
|
7901
|
+
configRemoves: ["acme/old"],
|
|
7902
|
+
skippedOrgs: [],
|
|
7903
|
+
openWork: [{ id: "T-1", repo: "acme/old", title: "fix it" }],
|
|
7904
|
+
warnings: [],
|
|
7905
|
+
});
|
|
7906
|
+
expect(audits).toEqual([]);
|
|
7907
|
+
expect(calls.map((c) => c[0])).toEqual(["list"]);
|
|
7908
|
+
expect(state.repos).toEqual(["acme/api", "acme/old"]);
|
|
7909
|
+
});
|
|
7910
|
+
|
|
7911
|
+
it("preview reports a failing open-work port as a warning", async () => {
|
|
7912
|
+
const { app } = setup({
|
|
7913
|
+
openWork: {
|
|
7914
|
+
listOpenWork: async () => {
|
|
7915
|
+
throw new Error("store down");
|
|
7916
|
+
},
|
|
7917
|
+
},
|
|
7918
|
+
});
|
|
7919
|
+
const body = (await (
|
|
7920
|
+
await app.request(previewUrl, { headers: asAdmin })
|
|
7921
|
+
).json()) as { openWork: unknown[]; warnings: string[] };
|
|
7922
|
+
expect(body.openWork).toEqual([]);
|
|
7923
|
+
expect(body.warnings.join()).toContain("store down");
|
|
7924
|
+
});
|
|
7925
|
+
|
|
7926
|
+
it("remove applies the confirmed repos in order, audits once, and blocks open work", async () => {
|
|
7927
|
+
const { app, audits, calls, state } = setup();
|
|
7928
|
+
const res = await remove(app, { repos: ["acme/old", "acme/api"] });
|
|
7929
|
+
expect(res.status).toBe(200);
|
|
7930
|
+
expect(await res.json()).toEqual({
|
|
7931
|
+
status: "applied",
|
|
7932
|
+
auditId: "audit-1",
|
|
7933
|
+
repos: ["acme/old"],
|
|
7934
|
+
installationRemoves: ["acme/old"],
|
|
7935
|
+
configRemoves: ["acme/old"],
|
|
7936
|
+
notRemoved: [
|
|
7937
|
+
{
|
|
7938
|
+
repo: "acme/api",
|
|
7939
|
+
reason: "the bound user can still push to this repo",
|
|
7940
|
+
},
|
|
7941
|
+
],
|
|
7942
|
+
skippedOrgs: [],
|
|
7943
|
+
warnings: [],
|
|
7944
|
+
});
|
|
7945
|
+
expect(calls).toEqual([
|
|
7946
|
+
["removeRepos", "acme", ["acme/old"]],
|
|
7947
|
+
["setRepos", ["acme/api"]],
|
|
7948
|
+
["audit"],
|
|
7949
|
+
["block", AGENT_ID, ["acme/old"], repoLostAccessBlockReason("acme/old")],
|
|
7950
|
+
]);
|
|
7951
|
+
expect(state.repos).toEqual(["acme/api"]);
|
|
7952
|
+
expect(state.installed).toEqual(["acme/api", "acme/gone"]);
|
|
7953
|
+
expect(audits).toHaveLength(1);
|
|
7954
|
+
expect(audits[0]).toMatchObject({
|
|
7955
|
+
agentId: AGENT_ID,
|
|
7956
|
+
caller: "root",
|
|
7957
|
+
planHash: "lost-access:remove",
|
|
7958
|
+
result: { status: "applied" },
|
|
7959
|
+
});
|
|
7960
|
+
});
|
|
7961
|
+
|
|
7962
|
+
it("a failing open-work port is a warning, not a failed call", async () => {
|
|
7963
|
+
const { app, state, audits } = setup({
|
|
7964
|
+
openWork: {
|
|
7965
|
+
blockOpenWork: async () => {
|
|
7966
|
+
throw new Error("store down");
|
|
7967
|
+
},
|
|
7968
|
+
},
|
|
7969
|
+
});
|
|
7970
|
+
const res = await remove(app, { repos: ["acme/old"] });
|
|
7971
|
+
expect(res.status).toBe(200);
|
|
7972
|
+
const body = (await res.json()) as { status: string; warnings: string[] };
|
|
7973
|
+
expect(body.status).toBe("applied");
|
|
7974
|
+
expect(body.warnings[0]).toContain("acme/old");
|
|
7975
|
+
expect(body.warnings[0]).toContain("store down");
|
|
7976
|
+
expect(state.repos).toEqual(["acme/api"]);
|
|
7977
|
+
expect(audits).toHaveLength(1);
|
|
7978
|
+
});
|
|
7979
|
+
|
|
7980
|
+
it("skipped for an unbound user: nothing changed, no audit", async () => {
|
|
7981
|
+
const { app, audits, calls } = setup({ username: null });
|
|
7982
|
+
const res = await remove(app, { repos: ["acme/old"] });
|
|
7983
|
+
expect(res.status).toBe(200);
|
|
7984
|
+
expect(await res.json()).toMatchObject({ status: "skipped" });
|
|
7985
|
+
expect((await app.request(previewUrl, { headers: asAdmin })).status).toBe(
|
|
7986
|
+
200,
|
|
7987
|
+
);
|
|
7988
|
+
expect(audits).toEqual([]);
|
|
7989
|
+
expect(calls).toEqual([]);
|
|
7990
|
+
});
|
|
7991
|
+
|
|
7992
|
+
it("skipped for an unverified user: nothing changed, no audit", async () => {
|
|
7993
|
+
const verified = accessFor(["acme/api"]);
|
|
7994
|
+
const { app, audits, calls } = setup({
|
|
7995
|
+
access: async (u) => {
|
|
7996
|
+
const r = await verified(u);
|
|
7997
|
+
return r.status === "ok"
|
|
7998
|
+
? { ...r, access: { ...r.access, userVerified: false } }
|
|
7999
|
+
: r;
|
|
8000
|
+
},
|
|
8001
|
+
});
|
|
8002
|
+
const res = await remove(app, { repos: ["acme/old"] });
|
|
8003
|
+
const body = (await res.json()) as { status: string; reason: string };
|
|
8004
|
+
expect(body.status).toBe("skipped");
|
|
8005
|
+
expect(body.reason).toContain("verified");
|
|
8006
|
+
expect(audits).toEqual([]);
|
|
8007
|
+
expect(calls).toEqual([]);
|
|
8008
|
+
});
|
|
8009
|
+
|
|
8010
|
+
it("422 for more than 10 removals without confirmRemovals; nothing changed", async () => {
|
|
8011
|
+
const many = Array.from({ length: 11 }, (_, i) => `acme/r${i}`);
|
|
8012
|
+
const { app, audits, state } = setup({ repos: many, installed: many });
|
|
8013
|
+
const res = await remove(app, { repos: many });
|
|
8014
|
+
expect(res.status).toBe(422);
|
|
8015
|
+
expect(JSON.stringify(await res.json())).toContain("confirmRemovals");
|
|
8016
|
+
expect(audits).toEqual([]);
|
|
8017
|
+
expect(state.repos).toEqual(many);
|
|
8018
|
+
const ok = await remove(app, { repos: many, confirmRemovals: true });
|
|
8019
|
+
expect(ok.status).toBe(200);
|
|
8020
|
+
expect(state.repos).toEqual([]);
|
|
8021
|
+
});
|
|
8022
|
+
|
|
8023
|
+
it("noop when nothing named still lacks access; no audit", async () => {
|
|
8024
|
+
const { app, audits } = setup();
|
|
8025
|
+
const res = await remove(app, { repos: ["acme/api"] });
|
|
8026
|
+
expect(res.status).toBe(200);
|
|
8027
|
+
expect(await res.json()).toMatchObject({ status: "noop", repos: [] });
|
|
8028
|
+
expect(audits).toEqual([]);
|
|
8029
|
+
});
|
|
8030
|
+
|
|
8031
|
+
it("403 for ops and agent keys; 404 unknown agent; 400 bad body; 503 unwired", async () => {
|
|
8032
|
+
const { app, audits } = setup();
|
|
8033
|
+
for (const key of [OPS, AGENT_KEY]) {
|
|
8034
|
+
const h = { Authorization: `Bearer ${key}` };
|
|
8035
|
+
expect((await app.request(previewUrl, { headers: h })).status).toBe(403);
|
|
8036
|
+
expect((await remove(app, { repos: ["acme/old"] }, h)).status).toBe(403);
|
|
8037
|
+
}
|
|
8038
|
+
expect((await remove(app, { repos: [] })).status).toBe(400);
|
|
8039
|
+
expect((await remove(app, { repos: ["nope"] })).status).toBe(400);
|
|
8040
|
+
expect(audits).toEqual([]);
|
|
8041
|
+
const unknown = setup({ known: false }).app;
|
|
8042
|
+
expect(
|
|
8043
|
+
(await unknown.request(previewUrl, { headers: asAdmin })).status,
|
|
8044
|
+
).toBe(404);
|
|
8045
|
+
expect((await remove(unknown, { repos: ["acme/old"] })).status).toBe(404);
|
|
8046
|
+
const unwired = createAdminApp({
|
|
8047
|
+
...makeMockDeps(),
|
|
8048
|
+
adminApiKeys: parseAdminApiKeys(`root:${ADMIN_API_KEY}:*`),
|
|
8049
|
+
});
|
|
8050
|
+
expect(
|
|
8051
|
+
(await unwired.request(previewUrl, { headers: asAdmin })).status,
|
|
8052
|
+
).toBe(503);
|
|
8053
|
+
});
|
|
8054
|
+
});
|
package/src/agents-api.ts
CHANGED
|
@@ -166,6 +166,7 @@ import {
|
|
|
166
166
|
UpsertChatTokenDailyBodySchema,
|
|
167
167
|
} from "./openapi-schemas.ts";
|
|
168
168
|
import type { InstallationReposService } from "./installation-repos-service.ts";
|
|
169
|
+
import type { LostAccessRemovalService } from "./lost-access-removal.ts";
|
|
169
170
|
import type { ReconcileApplyService } from "./reconcile-apply-service.ts";
|
|
170
171
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
171
172
|
import {
|
|
@@ -291,6 +292,15 @@ export interface AdminDeps {
|
|
|
291
292
|
InstallationReposService,
|
|
292
293
|
"list" | "add" | "remove"
|
|
293
294
|
>;
|
|
295
|
+
/**
|
|
296
|
+
* IRM-1.3: backs the admin-only /agents/:id/github-lost-access/{preview,remove}
|
|
297
|
+
* routes. Optional (additive); absent means the enterprise Apps are not
|
|
298
|
+
* wired and the routes answer 503.
|
|
299
|
+
*/
|
|
300
|
+
githubLostAccessRemoval?: Pick<
|
|
301
|
+
LostAccessRemovalService,
|
|
302
|
+
"preview" | "remove"
|
|
303
|
+
>;
|
|
294
304
|
/**
|
|
295
305
|
* Retained solely as a pass-through to deleteAgentFully() (DELETE /agents/:id),
|
|
296
306
|
* which has its own internal prisma.agent/prisma.agentEnv access — out of
|
|
@@ -739,6 +749,73 @@ const GithubInstallationReposChangeSchema = z
|
|
|
739
749
|
})
|
|
740
750
|
.openapi("GithubInstallationReposChange");
|
|
741
751
|
|
|
752
|
+
const OpenWorkItemSchema = z.object({
|
|
753
|
+
id: z.string(),
|
|
754
|
+
repo: z.string(),
|
|
755
|
+
title: z.string().optional(),
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
const SkippedOrgsSchema = z.array(
|
|
759
|
+
z.object({ org: z.string(), reason: z.string() }),
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
const GithubLostAccessPreviewSchema = z
|
|
763
|
+
.object({
|
|
764
|
+
status: z.enum(["previewed", "skipped"]).openapi({
|
|
765
|
+
description:
|
|
766
|
+
"`skipped` (not an error) when no GitHub username is bound, the user is not found or unverified, the agent has no GH_APP_ID, or the Apps are not configured; `reason` says why.",
|
|
767
|
+
}),
|
|
768
|
+
reason: z.string().optional(),
|
|
769
|
+
repos: z.array(z.string()).optional().openapi({
|
|
770
|
+
description:
|
|
771
|
+
"Every repo the bound user can no longer push to that would be removed from the installation or the agent's repos.",
|
|
772
|
+
}),
|
|
773
|
+
installationRemoves: z.array(z.string()).optional(),
|
|
774
|
+
configRemoves: z.array(z.string()).optional(),
|
|
775
|
+
skippedOrgs: SkippedOrgsSchema.optional().openapi({
|
|
776
|
+
description:
|
|
777
|
+
"Orgs whose access listing is missing, partial or failed, or whose lookup App is installed on selected repositories only; nothing there is removed.",
|
|
778
|
+
}),
|
|
779
|
+
openWork: z.array(OpenWorkItemSchema).optional().openapi({
|
|
780
|
+
description:
|
|
781
|
+
"The agent's open tasks and PRs on those repos that removing them would block; empty when no open-work port is wired.",
|
|
782
|
+
}),
|
|
783
|
+
warnings: z.array(z.string()).optional(),
|
|
784
|
+
})
|
|
785
|
+
.openapi("GithubLostAccessPreview");
|
|
786
|
+
|
|
787
|
+
const GithubLostAccessRemoveBodySchema = z
|
|
788
|
+
.object({
|
|
789
|
+
repos: z.array(RepoFullNameSchema).min(1).max(100).openapi({
|
|
790
|
+
description:
|
|
791
|
+
"The repos (owner/repo) the admin confirmed from the preview. Each is re-checked; only those the user still cannot push to are removed.",
|
|
792
|
+
}),
|
|
793
|
+
confirmRemovals: z.boolean().optional().openapi({
|
|
794
|
+
description: "Required when more than 10 repos would be removed.",
|
|
795
|
+
}),
|
|
796
|
+
})
|
|
797
|
+
.openapi("GithubLostAccessRemoveBody");
|
|
798
|
+
|
|
799
|
+
const GithubLostAccessRemoveResultSchema = z
|
|
800
|
+
.object({
|
|
801
|
+
status: z.enum(["applied", "noop", "skipped"]),
|
|
802
|
+
reason: z.string().optional(),
|
|
803
|
+
auditId: z.string().optional(),
|
|
804
|
+
repos: z.array(z.string()).optional(),
|
|
805
|
+
installationRemoves: z.array(z.string()).optional(),
|
|
806
|
+
configRemoves: z.array(z.string()).optional(),
|
|
807
|
+
notRemoved: z
|
|
808
|
+
.array(z.object({ repo: z.string(), reason: z.string() }))
|
|
809
|
+
.optional()
|
|
810
|
+
.openapi({
|
|
811
|
+
description:
|
|
812
|
+
"Named repos left alone: the user can still push, the org was skipped, or the repo is on neither the installation nor the agent.",
|
|
813
|
+
}),
|
|
814
|
+
skippedOrgs: SkippedOrgsSchema.optional(),
|
|
815
|
+
warnings: z.array(z.string()).optional(),
|
|
816
|
+
})
|
|
817
|
+
.openapi("GithubLostAccessRemoveResult");
|
|
818
|
+
|
|
742
819
|
// ─── Route definitions ──────────────────────────────────────────────────────────
|
|
743
820
|
|
|
744
821
|
const reconcileAgentsRoute = createRoute({
|
|
@@ -1803,6 +1880,59 @@ const removeInstallationReposRoute = createRoute({
|
|
|
1803
1880
|
},
|
|
1804
1881
|
});
|
|
1805
1882
|
|
|
1883
|
+
const lostAccessErrors = {
|
|
1884
|
+
401: { description: "Unauthorized", ...jsonError },
|
|
1885
|
+
403: { description: "Platform admin required", ...jsonError },
|
|
1886
|
+
404: { description: "Agent not found", ...jsonError },
|
|
1887
|
+
502: { description: "GitHub unavailable or refused", ...jsonError },
|
|
1888
|
+
503: { description: "GitHub Apps are not configured", ...jsonError },
|
|
1889
|
+
};
|
|
1890
|
+
|
|
1891
|
+
const githubLostAccessPreviewRoute = createRoute({
|
|
1892
|
+
method: "get",
|
|
1893
|
+
path: "/agents/{id}/github-lost-access/preview",
|
|
1894
|
+
summary: "Preview removing the repos the bound user can no longer push to",
|
|
1895
|
+
description:
|
|
1896
|
+
"Platform-admin only (session members, ops keys and agent keys get 403). Read-only: nothing is changed or audited. Lists the repos on the agent App's installation or in the agent's repos that the bound githubUsername can no longer push to, and the agent's open tasks and PRs on them (via the optional open-work port; a port failure is a warning). Orgs whose access listing is missing, partial or failed, or whose lookup App is installed on selected repositories only, are skipped and reported. 200 with `status: skipped` when the user is unbound, not found or unverified, the agent has no GH_APP_ID, or the Apps are not configured.",
|
|
1897
|
+
request: { params: AgentIdParamSchema },
|
|
1898
|
+
responses: {
|
|
1899
|
+
200: {
|
|
1900
|
+
description: "Lost-access removal preview",
|
|
1901
|
+
content: {
|
|
1902
|
+
"application/json": { schema: GithubLostAccessPreviewSchema },
|
|
1903
|
+
},
|
|
1904
|
+
},
|
|
1905
|
+
...lostAccessErrors,
|
|
1906
|
+
},
|
|
1907
|
+
});
|
|
1908
|
+
|
|
1909
|
+
const githubLostAccessRemoveRoute = createRoute({
|
|
1910
|
+
method: "post",
|
|
1911
|
+
path: "/agents/{id}/github-lost-access/remove",
|
|
1912
|
+
summary: "Remove the confirmed repos the bound user can no longer push to",
|
|
1913
|
+
description:
|
|
1914
|
+
"Platform-admin only. Never adds repos. Takes the repos the admin confirmed from the preview, re-checks each against the bound user's current access, and removes only those still without push access: first from the agent App's installation, then from the agent's repos; then writes one audit entry (GitHubReconcileAudit, planHash `lost-access:remove`) and blocks the agent's open work on the removed repos through the optional open-work port (a failure there is a warning). Named repos left alone are reported in `notRemoved`. Same skip rules as the preview: a skipped call (200 `status: skipped`) changes and audits nothing; `noop` when nothing named still lacks access. 422 when more than 10 repos would be removed without `confirmRemovals: true`. 502 when GitHub refuses (a failed audit entry records only what landed).",
|
|
1915
|
+
request: {
|
|
1916
|
+
params: AgentIdParamSchema,
|
|
1917
|
+
body: {
|
|
1918
|
+
required: true,
|
|
1919
|
+
content: {
|
|
1920
|
+
"application/json": { schema: GithubLostAccessRemoveBodySchema },
|
|
1921
|
+
},
|
|
1922
|
+
},
|
|
1923
|
+
},
|
|
1924
|
+
responses: {
|
|
1925
|
+
200: {
|
|
1926
|
+
description: "Removal result",
|
|
1927
|
+
content: {
|
|
1928
|
+
"application/json": { schema: GithubLostAccessRemoveResultSchema },
|
|
1929
|
+
},
|
|
1930
|
+
},
|
|
1931
|
+
...lostAccessErrors,
|
|
1932
|
+
422: { description: "Removal cap needs confirmRemovals", ...jsonError },
|
|
1933
|
+
},
|
|
1934
|
+
});
|
|
1935
|
+
|
|
1806
1936
|
/** `from`/`to` ISO datetime bounds shared by the cron-run report routes. */
|
|
1807
1937
|
export const cronRunStatsQuerySchema = z
|
|
1808
1938
|
.object({
|
|
@@ -3516,6 +3646,102 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3516
3646
|
),
|
|
3517
3647
|
);
|
|
3518
3648
|
|
|
3649
|
+
// /agents/:id/github-lost-access/{preview,remove} — IRM-1.3. Platform admin
|
|
3650
|
+
// only; responses carry repo names and ids, never tokens.
|
|
3651
|
+
const lostAccessService = (c: { get(k: "isAdmin"): boolean | undefined }) => {
|
|
3652
|
+
if (c.get("isAdmin") !== true) {
|
|
3653
|
+
throw new ForbiddenError(
|
|
3654
|
+
"Admin access required to remove repos the bound user lost access to",
|
|
3655
|
+
);
|
|
3656
|
+
}
|
|
3657
|
+
const svc = deps.githubLostAccessRemoval;
|
|
3658
|
+
if (!svc) throw new ApiError(503, GITHUB_RECONCILE_UNCONFIGURED);
|
|
3659
|
+
return svc;
|
|
3660
|
+
};
|
|
3661
|
+
|
|
3662
|
+
app.openapi(githubLostAccessPreviewRoute, async (c) => {
|
|
3663
|
+
const svc = lostAccessService(c);
|
|
3664
|
+
const { id: agentId } = c.req.valid("param");
|
|
3665
|
+
const result = await svc.preview(agentId);
|
|
3666
|
+
if (result.status === "agent_not_found") {
|
|
3667
|
+
throw new NotFoundError(`agent ${agentId} not found`);
|
|
3668
|
+
}
|
|
3669
|
+
if (result.status === "skipped") {
|
|
3670
|
+
return c.json({ status: "skipped" as const, reason: result.reason }, 200);
|
|
3671
|
+
}
|
|
3672
|
+
const { plan } = result;
|
|
3673
|
+
return c.json(
|
|
3674
|
+
{
|
|
3675
|
+
status: "previewed" as const,
|
|
3676
|
+
repos: plan.repos,
|
|
3677
|
+
installationRemoves: plan.installationRemoves,
|
|
3678
|
+
configRemoves: plan.configRemoves,
|
|
3679
|
+
skippedOrgs: plan.skippedOrgs,
|
|
3680
|
+
openWork: result.openWork,
|
|
3681
|
+
warnings: result.warnings,
|
|
3682
|
+
},
|
|
3683
|
+
200,
|
|
3684
|
+
);
|
|
3685
|
+
});
|
|
3686
|
+
|
|
3687
|
+
app.openapi(githubLostAccessRemoveRoute, async (c) => {
|
|
3688
|
+
const svc = lostAccessService(c);
|
|
3689
|
+
const { id: agentId } = c.req.valid("param");
|
|
3690
|
+
const body = c.req.valid("json");
|
|
3691
|
+
const caller = c.get("caller")?.name ?? "anonymous";
|
|
3692
|
+
const result = await svc.remove({
|
|
3693
|
+
agentId,
|
|
3694
|
+
caller,
|
|
3695
|
+
repos: body.repos,
|
|
3696
|
+
confirmRemovals: body.confirmRemovals,
|
|
3697
|
+
});
|
|
3698
|
+
switch (result.status) {
|
|
3699
|
+
case "agent_not_found":
|
|
3700
|
+
throw new NotFoundError(`agent ${agentId} not found`);
|
|
3701
|
+
case "skipped":
|
|
3702
|
+
return c.json(
|
|
3703
|
+
{ status: "skipped" as const, reason: result.reason },
|
|
3704
|
+
200,
|
|
3705
|
+
);
|
|
3706
|
+
case "removal_cap":
|
|
3707
|
+
throw new UnprocessableEntityError(
|
|
3708
|
+
`${result.removals} repos would be removed; resend with confirmRemovals: true to apply`,
|
|
3709
|
+
);
|
|
3710
|
+
case "failed":
|
|
3711
|
+
console.log(
|
|
3712
|
+
`[agents-api] github lost-access removal failed: agent=${agentId} audit=${result.auditId} caller=${caller}`,
|
|
3713
|
+
);
|
|
3714
|
+
throw new BadGatewayError(
|
|
3715
|
+
`GitHub refused the removal; audit ${result.auditId} records the failure: ${result.error}`,
|
|
3716
|
+
);
|
|
3717
|
+
case "noop":
|
|
3718
|
+
case "applied": {
|
|
3719
|
+
const { plan } = result;
|
|
3720
|
+
if (result.status === "applied") {
|
|
3721
|
+
console.log(
|
|
3722
|
+
`[agents-api] github lost-access removal applied: agent=${agentId} audit=${result.auditId} caller=${caller}`,
|
|
3723
|
+
);
|
|
3724
|
+
}
|
|
3725
|
+
return c.json(
|
|
3726
|
+
{
|
|
3727
|
+
status: result.status,
|
|
3728
|
+
...(result.status === "applied" ? { auditId: result.auditId } : {}),
|
|
3729
|
+
repos: plan.repos,
|
|
3730
|
+
installationRemoves: plan.installationRemoves,
|
|
3731
|
+
configRemoves: plan.configRemoves,
|
|
3732
|
+
notRemoved: plan.notRemoved,
|
|
3733
|
+
skippedOrgs: plan.skippedOrgs,
|
|
3734
|
+
warnings:
|
|
3735
|
+
result.status === "applied"
|
|
3736
|
+
? [...plan.warnings, ...result.warnings]
|
|
3737
|
+
: plan.warnings,
|
|
3738
|
+
},
|
|
3739
|
+
200,
|
|
3740
|
+
);
|
|
3741
|
+
}
|
|
3742
|
+
}
|
|
3743
|
+
});
|
|
3744
|
+
|
|
3519
3745
|
// GET /agents/:id/github-installations — latest snapshot, 404 if none yet
|
|
3520
3746
|
app.openapi(getGitHubInstallationsRoute, async (c) => {
|
|
3521
3747
|
const { id: agentId } = c.req.valid("param");
|
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
InstallationReposService,
|
|
26
26
|
type InstallationReposPort,
|
|
27
27
|
} from "./installation-repos-service.ts";
|
|
28
|
+
import { LostAccessRemovalService } from "./lost-access-removal.ts";
|
|
28
29
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
29
30
|
|
|
30
31
|
export interface GithubReconcileWiringDeps {
|
|
@@ -120,3 +121,24 @@ export function createGithubInstallationRepos(
|
|
|
120
121
|
audit: new PrismaReconcileAudit(deps.prisma),
|
|
121
122
|
});
|
|
122
123
|
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The `githubLostAccessRemoval` dep for createAdminApp (IRM-1.3): removes the
|
|
127
|
+
* repos the bound user can no longer push to from the agent App's
|
|
128
|
+
* installation and the agent's repos, blocking open work through the
|
|
129
|
+
* optional host OpenWorkPort.
|
|
130
|
+
*/
|
|
131
|
+
export function createGithubLostAccessRemoval(
|
|
132
|
+
deps: InstallationReposWiringDeps,
|
|
133
|
+
): LostAccessRemovalService {
|
|
134
|
+
const client = lazyInstallerClient(deps);
|
|
135
|
+
return new LostAccessRemovalService({
|
|
136
|
+
agents: new PrismaReconcileAgents(deps.prisma, deps.agentEnv),
|
|
137
|
+
access: deps.access,
|
|
138
|
+
installation:
|
|
139
|
+
deps.installationRepos ??
|
|
140
|
+
createInstallationReposPort(createInstallationPort(client), client),
|
|
141
|
+
audit: new PrismaReconcileAudit(deps.prisma),
|
|
142
|
+
...(deps.openWork ? { openWork: deps.openWork } : {}),
|
|
143
|
+
});
|
|
144
|
+
}
|