@app-vitals/agent-admin 1.35.1 → 1.36.1

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/src/agents-api.ts CHANGED
@@ -166,7 +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
+ import type { GithubSyncService } from "./github-sync.ts";
170
170
  import type { OpenWorkPort } from "./reconcile-open-work.ts";
171
171
  import {
172
172
  blockRepoRemovalWork,
@@ -277,23 +277,17 @@ export interface AdminDeps {
277
277
  */
278
278
  openWork?: OpenWorkPort;
279
279
  /**
280
- * IRM-1.2: backs the admin-only /agents/:id/github-installation/repos
281
- * routes. Optional (additive); absent means the enterprise Apps are not
282
- * wired and the routes answer 503.
280
+ * IRM-1.2: backs the admin-only GET /agents/:id/github-installation/repos
281
+ * route. Optional (additive); absent means the enterprise Apps are not
282
+ * wired and the route answers 503.
283
283
  */
284
- githubInstallationRepos?: Pick<
285
- InstallationReposService,
286
- "list" | "add" | "remove"
287
- >;
284
+ githubInstallationRepos?: Pick<InstallationReposService, "list">;
288
285
  /**
289
- * IRM-1.3: backs the admin-only /agents/:id/github-lost-access/{preview,remove}
286
+ * IRM-3.1: backs the admin-only /agents/:id/github-sync/{preview,apply}
290
287
  * routes. Optional (additive); absent means the enterprise Apps are not
291
288
  * wired and the routes answer 503.
292
289
  */
293
- githubLostAccessRemoval?: Pick<
294
- LostAccessRemovalService,
295
- "preview" | "remove"
296
- >;
290
+ githubSync?: Pick<GithubSyncService, "preview" | "apply">;
297
291
  /**
298
292
  * Retained solely as a pass-through to deleteAgentFully() (DELETE /agents/:id),
299
293
  * which has its own internal prisma.agent/prisma.agentEnv access — out of
@@ -630,15 +624,6 @@ const RepoFullNameSchema = z
630
624
  .string()
631
625
  .regex(/^[^/\s]+\/[^/\s]+$/, "must be owner/repo");
632
626
 
633
- const InstallationReposBodySchema = z
634
- .object({
635
- repos: z.array(RepoFullNameSchema).min(1).max(100).openapi({
636
- description:
637
- "Repos (owner/repo) to change on the agent App's installation.",
638
- }),
639
- })
640
- .openapi("GithubInstallationReposBody");
641
-
642
627
  const GithubInstallationReposListSchema = z
643
628
  .object({
644
629
  appId: z.number().int(),
@@ -659,14 +644,6 @@ const GithubInstallationReposListSchema = z
659
644
  })
660
645
  .openapi("GithubInstallationReposList");
661
646
 
662
- const GithubInstallationReposChangeSchema = z
663
- .object({
664
- status: z.enum(["applied"]),
665
- auditId: z.string(),
666
- repos: z.array(z.string()),
667
- })
668
- .openapi("GithubInstallationReposChange");
669
-
670
647
  const OpenWorkItemSchema = z.object({
671
648
  id: z.string(),
672
649
  repo: z.string(),
@@ -677,62 +654,76 @@ const SkippedOrgsSchema = z.array(
677
654
  z.object({ org: z.string(), reason: z.string() }),
678
655
  );
679
656
 
680
- const GithubLostAccessPreviewSchema = z
657
+ const GithubSyncPreviewSchema = z
681
658
  .object({
682
659
  status: z.enum(["previewed", "skipped"]).openapi({
683
660
  description:
684
661
  "`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.",
685
662
  }),
686
663
  reason: z.string().optional(),
687
- repos: z.array(z.string()).optional().openapi({
664
+ appAdds: z.array(z.string()).optional().openapi({
665
+ description:
666
+ "Repos the bound user can push to, in orgs the agent App is installed on with a complete access listing, that are not on the installation. No cap.",
667
+ }),
668
+ appRemoves: z.array(z.string()).optional().openapi({
669
+ description: "Installation repos the bound user can no longer push to.",
670
+ }),
671
+ agentRemoves: z.array(z.string()).optional().openapi({
688
672
  description:
689
- "Every repo the bound user can no longer push to that would be removed from the installation or the agent's repos.",
673
+ "Configured agent repos the bound user can no longer push to. The agent's repos are never added to.",
690
674
  }),
691
- installationRemoves: z.array(z.string()).optional(),
692
- configRemoves: z.array(z.string()).optional(),
693
675
  skippedOrgs: SkippedOrgsSchema.optional().openapi({
694
676
  description:
695
- "Orgs whose access listing is missing, partial or failed, or whose lookup App is installed on selected repositories only; nothing there is removed.",
677
+ "Orgs whose access listing is missing, partial or failed, or whose lookup App is not installed or installed on selected repositories only; nothing there is added or removed.",
696
678
  }),
697
679
  openWork: z.array(OpenWorkItemSchema).optional().openapi({
698
680
  description:
699
- "The agent's open tasks and PRs on those repos that removing them would block; empty when no open-work port is wired.",
681
+ "The agent's open tasks and PRs on the removed repos that the removals would block; empty when no open-work port is wired.",
700
682
  }),
701
683
  warnings: z.array(z.string()).optional(),
702
684
  })
703
- .openapi("GithubLostAccessPreview");
685
+ .openapi("GithubSyncPreview");
704
686
 
705
- const GithubLostAccessRemoveBodySchema = z
687
+ const GithubSyncApplyBodySchema = z
706
688
  .object({
707
- repos: z.array(RepoFullNameSchema).min(1).max(100).openapi({
689
+ appAdds: z.array(RepoFullNameSchema).max(500).default([]).openapi({
708
690
  description:
709
- "The repos (owner/repo) the admin confirmed from the preview. Each is re-checked; only those the user still cannot push to are removed.",
691
+ "Repos (owner/repo) the admin confirmed to add to the agent App's installation.",
692
+ }),
693
+ appRemoves: z.array(RepoFullNameSchema).max(500).default([]).openapi({
694
+ description:
695
+ "Repos the admin confirmed to remove from the agent App's installation.",
696
+ }),
697
+ agentRemoves: z.array(RepoFullNameSchema).max(500).default([]).openapi({
698
+ description:
699
+ "Repos the admin confirmed to remove from the agent's repos.",
710
700
  }),
711
701
  confirmRemovals: z.boolean().optional().openapi({
712
- description: "Required when more than 10 repos would be removed.",
702
+ description:
703
+ "Required when more than 10 distinct repos would be removed.",
713
704
  }),
714
705
  })
715
- .openapi("GithubLostAccessRemoveBody");
706
+ .openapi("GithubSyncApplyBody");
716
707
 
717
- const GithubLostAccessRemoveResultSchema = z
708
+ const GithubSyncApplyResultSchema = z
718
709
  .object({
719
710
  status: z.enum(["applied", "noop", "skipped"]),
720
711
  reason: z.string().optional(),
721
712
  auditId: z.string().optional(),
722
- repos: z.array(z.string()).optional(),
723
- installationRemoves: z.array(z.string()).optional(),
724
- configRemoves: z.array(z.string()).optional(),
725
- notRemoved: z
713
+ appAdds: z.array(z.string()).optional(),
714
+ appRemoves: z.array(z.string()).optional(),
715
+ agentRemoves: z.array(z.string()).optional(),
716
+ notApplied: z
726
717
  .array(z.object({ repo: z.string(), reason: z.string() }))
727
718
  .optional()
728
719
  .openapi({
729
720
  description:
730
- "Named repos left alone: the user can still push, the org was skipped, or the repo is on neither the installation nor the agent.",
721
+ "Confirmed repos left alone: access changed since the preview, the org was skipped, or the repo is not where the change expected it.",
731
722
  }),
732
723
  skippedOrgs: SkippedOrgsSchema.optional(),
733
724
  warnings: z.array(z.string()).optional(),
734
725
  })
735
- .openapi("GithubLostAccessRemoveResult");
726
+ .openapi("GithubSyncApplyResult");
736
727
 
737
728
  // ─── Route definitions ──────────────────────────────────────────────────────────
738
729
 
@@ -1688,59 +1679,7 @@ const listInstallationReposRoute = createRoute({
1688
1679
  },
1689
1680
  });
1690
1681
 
1691
- const addInstallationReposRoute = createRoute({
1692
- method: "post",
1693
- path: "/agents/{id}/github-installation/repos",
1694
- summary: "Add repos to the agent App's installation",
1695
- description:
1696
- "Platform-admin only. Grants the agent's own GitHub App installation access to the named repos; the agent's configured repos are not changed. Writes one audit entry (GitHubReconcileAudit, planHash `direct:add`) recording the caller and repos. 404 unknown agent; 422 no GH_APP_ID; 409 when the App is not installed on a repo's org (nothing is changed or audited); 502 when GitHub refuses (a failed audit entry is written).",
1697
- request: {
1698
- params: AgentIdParamSchema,
1699
- body: {
1700
- required: true,
1701
- content: { "application/json": { schema: InstallationReposBodySchema } },
1702
- },
1703
- },
1704
- responses: {
1705
- 200: {
1706
- description: "Repos added",
1707
- content: {
1708
- "application/json": { schema: GithubInstallationReposChangeSchema },
1709
- },
1710
- },
1711
- ...installationReposErrors,
1712
- 409: { description: "Agent App not installed on the org", ...jsonError },
1713
- 502: { description: "GitHub unavailable or refused", ...jsonError },
1714
- },
1715
- });
1716
-
1717
- const removeInstallationReposRoute = createRoute({
1718
- method: "post",
1719
- path: "/agents/{id}/github-installation/repos/remove",
1720
- summary: "Remove repos from the agent App's installation",
1721
- description:
1722
- "Platform-admin only. Revokes the agent's own GitHub App installation access to the named repos; the agent's configured repos are not changed. Writes one audit entry (GitHubReconcileAudit, planHash `direct:remove`) recording the caller and repos. Same errors as the add route.",
1723
- request: {
1724
- params: AgentIdParamSchema,
1725
- body: {
1726
- required: true,
1727
- content: { "application/json": { schema: InstallationReposBodySchema } },
1728
- },
1729
- },
1730
- responses: {
1731
- 200: {
1732
- description: "Repos removed",
1733
- content: {
1734
- "application/json": { schema: GithubInstallationReposChangeSchema },
1735
- },
1736
- },
1737
- ...installationReposErrors,
1738
- 409: { description: "Agent App not installed on the org", ...jsonError },
1739
- 502: { description: "GitHub unavailable or refused", ...jsonError },
1740
- },
1741
- });
1742
-
1743
- const lostAccessErrors = {
1682
+ const syncErrors = {
1744
1683
  401: { description: "Unauthorized", ...jsonError },
1745
1684
  403: { description: "Platform admin required", ...jsonError },
1746
1685
  404: { description: "Agent not found", ...jsonError },
@@ -1748,47 +1687,41 @@ const lostAccessErrors = {
1748
1687
  503: { description: "GitHub Apps are not configured", ...jsonError },
1749
1688
  };
1750
1689
 
1751
- const githubLostAccessPreviewRoute = createRoute({
1690
+ const githubSyncPreviewRoute = createRoute({
1752
1691
  method: "get",
1753
- path: "/agents/{id}/github-lost-access/preview",
1754
- summary: "Preview removing the repos the bound user can no longer push to",
1692
+ path: "/agents/{id}/github-sync/preview",
1693
+ summary: "Preview syncing the agent's GitHub access with its bound user",
1755
1694
  description:
1756
- "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.",
1695
+ "Platform-admin only (session members, ops keys and agent keys get 403). Read-only: nothing is changed or audited. Lists (1) repos to add to the agent App's installation, (2) installation repos to remove and (3) agent repos to remove, based on what the bound githubUsername can push to, plus the agent's open tasks and PRs the removals would block (via the optional open-work port; a port failure is a warning). The agent's repos are never added to. Orgs whose access listing is missing, partial or failed, or whose lookup App is not installed or installed on selected repositories only, are skipped (adds included) 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.",
1757
1696
  request: { params: AgentIdParamSchema },
1758
1697
  responses: {
1759
1698
  200: {
1760
- description: "Lost-access removal preview",
1761
- content: {
1762
- "application/json": { schema: GithubLostAccessPreviewSchema },
1763
- },
1699
+ description: "Sync preview",
1700
+ content: { "application/json": { schema: GithubSyncPreviewSchema } },
1764
1701
  },
1765
- ...lostAccessErrors,
1702
+ ...syncErrors,
1766
1703
  },
1767
1704
  });
1768
1705
 
1769
- const githubLostAccessRemoveRoute = createRoute({
1706
+ const githubSyncApplyRoute = createRoute({
1770
1707
  method: "post",
1771
- path: "/agents/{id}/github-lost-access/remove",
1772
- summary: "Remove the confirmed repos the bound user can no longer push to",
1708
+ path: "/agents/{id}/github-sync/apply",
1709
+ summary: "Apply the confirmed sync changes",
1773
1710
  description:
1774
- "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).",
1711
+ "Platform-admin only. Takes the exact lists the admin confirmed from the preview, re-checks each against the bound user's current access, and applies only what still qualifies: installation adds, installation removes, then agent-repo removes. Writes one audit entry (GitHubReconcileAudit, planHash `sync:apply`) and blocks the agent's open work on the removed repos through the optional open-work port (a failure there is a warning). Confirmed repos left alone are reported in `notApplied`. Same skip rules as the preview: a skipped call (200 `status: skipped`) changes and audits nothing; `noop` when nothing confirmed still applies. 422 when more than 10 repos would be removed without `confirmRemovals: true`. 502 when GitHub refuses (a failed audit entry records only what landed).",
1775
1712
  request: {
1776
1713
  params: AgentIdParamSchema,
1777
1714
  body: {
1778
1715
  required: true,
1779
- content: {
1780
- "application/json": { schema: GithubLostAccessRemoveBodySchema },
1781
- },
1716
+ content: { "application/json": { schema: GithubSyncApplyBodySchema } },
1782
1717
  },
1783
1718
  },
1784
1719
  responses: {
1785
1720
  200: {
1786
- description: "Removal result",
1787
- content: {
1788
- "application/json": { schema: GithubLostAccessRemoveResultSchema },
1789
- },
1721
+ description: "Sync result",
1722
+ content: { "application/json": { schema: GithubSyncApplyResultSchema } },
1790
1723
  },
1791
- ...lostAccessErrors,
1724
+ ...syncErrors,
1792
1725
  422: { description: "Removal cap needs confirmRemovals", ...jsonError },
1793
1726
  },
1794
1727
  });
@@ -3295,7 +3228,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
3295
3228
  }) => {
3296
3229
  if (c.get("isAdmin") !== true) {
3297
3230
  throw new ForbiddenError(
3298
- "Admin access required to manage installation repos",
3231
+ "Admin access required to list installation repos",
3299
3232
  );
3300
3233
  }
3301
3234
  const svc = deps.githubInstallationRepos;
@@ -3306,16 +3239,12 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
3306
3239
  agentId: string,
3307
3240
  result:
3308
3241
  | { status: "agent_not_found" }
3309
- | { status: "no_app_id"; reason: string }
3310
- | { status: "not_installed"; reason: string },
3242
+ | { status: "no_app_id"; reason: string },
3311
3243
  ): never => {
3312
3244
  if (result.status === "agent_not_found") {
3313
3245
  throw new NotFoundError(`agent ${agentId} not found`);
3314
3246
  }
3315
- if (result.status === "no_app_id") {
3316
- throw new UnprocessableEntityError(result.reason);
3317
- }
3318
- throw new ConflictError(result.reason);
3247
+ throw new UnprocessableEntityError(result.reason);
3319
3248
  };
3320
3249
 
3321
3250
  app.openapi(listInstallationReposRoute, async (c) => {
@@ -3326,77 +3255,19 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
3326
3255
  return c.json({ appId: result.appId, orgs: result.orgs }, 200);
3327
3256
  });
3328
3257
 
3329
- const changeInstallationRepos = async (
3330
- kind: "add" | "remove",
3331
- c: {
3332
- get(k: "isAdmin"): boolean | undefined;
3333
- get(k: "caller"): { name: string } | undefined;
3334
- },
3335
- agentId: string,
3336
- repos: string[],
3337
- ) => {
3338
- const svc = installationReposService(c);
3339
- const caller = c.get("caller")?.name ?? "anonymous";
3340
- const result = await svc[kind](agentId, caller, repos);
3341
- if (
3342
- result.status === "agent_not_found" ||
3343
- result.status === "no_app_id" ||
3344
- result.status === "not_installed"
3345
- ) {
3346
- return installationFailure(agentId, result);
3347
- }
3348
- console.log(
3349
- `[agents-api] github installation repos ${kind} ${result.status}: agent=${agentId} audit=${result.auditId} caller=${caller}`,
3350
- );
3351
- if (result.status === "failed") {
3352
- throw new BadGatewayError(
3353
- `GitHub refused the ${kind}; audit ${result.auditId} records the failure: ${result.error}`,
3354
- );
3355
- }
3356
- return {
3357
- status: "applied" as const,
3358
- auditId: result.auditId,
3359
- repos: result.repos,
3360
- };
3361
- };
3362
- app.openapi(addInstallationReposRoute, async (c) =>
3363
- c.json(
3364
- await changeInstallationRepos(
3365
- "add",
3366
- c,
3367
- c.req.valid("param").id,
3368
- c.req.valid("json").repos,
3369
- ),
3370
- 200,
3371
- ),
3372
- );
3373
- app.openapi(removeInstallationReposRoute, async (c) =>
3374
- c.json(
3375
- await changeInstallationRepos(
3376
- "remove",
3377
- c,
3378
- c.req.valid("param").id,
3379
- c.req.valid("json").repos,
3380
- ),
3381
- 200,
3382
- ),
3383
- );
3384
-
3385
- // /agents/:id/github-lost-access/{preview,remove} — IRM-1.3. Platform admin
3386
- // only; responses carry repo names and ids, never tokens.
3387
- const lostAccessService = (c: { get(k: "isAdmin"): boolean | undefined }) => {
3258
+ // /agents/:id/github-sync/{preview,apply} — IRM-3.1. Platform admin only;
3259
+ // responses carry repo names and ids, never tokens.
3260
+ const syncService = (c: { get(k: "isAdmin"): boolean | undefined }) => {
3388
3261
  if (c.get("isAdmin") !== true) {
3389
- throw new ForbiddenError(
3390
- "Admin access required to remove repos the bound user lost access to",
3391
- );
3262
+ throw new ForbiddenError("Admin access required to sync GitHub repos");
3392
3263
  }
3393
- const svc = deps.githubLostAccessRemoval;
3264
+ const svc = deps.githubSync;
3394
3265
  if (!svc) throw new ApiError(503, GITHUB_APPS_UNCONFIGURED);
3395
3266
  return svc;
3396
3267
  };
3397
3268
 
3398
- app.openapi(githubLostAccessPreviewRoute, async (c) => {
3399
- const svc = lostAccessService(c);
3269
+ app.openapi(githubSyncPreviewRoute, async (c) => {
3270
+ const svc = syncService(c);
3400
3271
  const { id: agentId } = c.req.valid("param");
3401
3272
  const result = await svc.preview(agentId);
3402
3273
  if (result.status === "agent_not_found") {
@@ -3409,9 +3280,9 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
3409
3280
  return c.json(
3410
3281
  {
3411
3282
  status: "previewed" as const,
3412
- repos: plan.repos,
3413
- installationRemoves: plan.installationRemoves,
3414
- configRemoves: plan.configRemoves,
3283
+ appAdds: plan.appAdds,
3284
+ appRemoves: plan.appRemoves,
3285
+ agentRemoves: plan.agentRemoves,
3415
3286
  skippedOrgs: plan.skippedOrgs,
3416
3287
  openWork: result.openWork,
3417
3288
  warnings: result.warnings,
@@ -3420,15 +3291,17 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
3420
3291
  );
3421
3292
  });
3422
3293
 
3423
- app.openapi(githubLostAccessRemoveRoute, async (c) => {
3424
- const svc = lostAccessService(c);
3294
+ app.openapi(githubSyncApplyRoute, async (c) => {
3295
+ const svc = syncService(c);
3425
3296
  const { id: agentId } = c.req.valid("param");
3426
3297
  const body = c.req.valid("json");
3427
3298
  const caller = c.get("caller")?.name ?? "anonymous";
3428
- const result = await svc.remove({
3299
+ const result = await svc.apply({
3429
3300
  agentId,
3430
3301
  caller,
3431
- repos: body.repos,
3302
+ appAdds: body.appAdds,
3303
+ appRemoves: body.appRemoves,
3304
+ agentRemoves: body.agentRemoves,
3432
3305
  confirmRemovals: body.confirmRemovals,
3433
3306
  });
3434
3307
  switch (result.status) {
@@ -3445,27 +3318,27 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
3445
3318
  );
3446
3319
  case "failed":
3447
3320
  console.log(
3448
- `[agents-api] github lost-access removal failed: agent=${agentId} audit=${result.auditId} caller=${caller}`,
3321
+ `[agents-api] github sync failed: agent=${agentId} audit=${result.auditId} caller=${caller}`,
3449
3322
  );
3450
3323
  throw new BadGatewayError(
3451
- `GitHub refused the removal; audit ${result.auditId} records the failure: ${result.error}`,
3324
+ `GitHub refused the sync; audit ${result.auditId} records the failure: ${result.error}`,
3452
3325
  );
3453
3326
  case "noop":
3454
3327
  case "applied": {
3455
3328
  const { plan } = result;
3456
3329
  if (result.status === "applied") {
3457
3330
  console.log(
3458
- `[agents-api] github lost-access removal applied: agent=${agentId} audit=${result.auditId} caller=${caller}`,
3331
+ `[agents-api] github sync applied: agent=${agentId} audit=${result.auditId} caller=${caller}`,
3459
3332
  );
3460
3333
  }
3461
3334
  return c.json(
3462
3335
  {
3463
3336
  status: result.status,
3464
3337
  ...(result.status === "applied" ? { auditId: result.auditId } : {}),
3465
- repos: plan.repos,
3466
- installationRemoves: plan.installationRemoves,
3467
- configRemoves: plan.configRemoves,
3468
- notRemoved: plan.notRemoved,
3338
+ appAdds: plan.appAdds,
3339
+ appRemoves: plan.appRemoves,
3340
+ agentRemoves: plan.agentRemoves,
3341
+ notApplied: plan.notApplied,
3469
3342
  skippedOrgs: plan.skippedOrgs,
3470
3343
  warnings:
3471
3344
  result.status === "applied"
@@ -0,0 +1,26 @@
1
+ [
2
+ {
3
+ "id": 1000001,
4
+ "app_slug": "example-agent-app",
5
+ "client_id": "Iv1.0000000000000000",
6
+ "account": { "login": "example-org" },
7
+ "repository_selection": "selected",
8
+ "repositories_url": "https://api.github.com/enterprises/example-ent/apps/organizations/example-org/installations/1000001/repositories",
9
+ "permissions": { "contents": "write", "metadata": "read" },
10
+ "events": ["push"],
11
+ "created_at": "2026-01-01T00:00:00Z",
12
+ "updated_at": "2026-01-02T00:00:00Z"
13
+ },
14
+ {
15
+ "id": 1000002,
16
+ "app_slug": "example-other-app",
17
+ "client_id": "Iv1.1111111111111111",
18
+ "account": { "login": "example-org" },
19
+ "repository_selection": "all",
20
+ "repositories_url": "https://api.github.com/enterprises/example-ent/apps/organizations/example-org/installations/1000002/repositories",
21
+ "permissions": { "metadata": "read" },
22
+ "events": [],
23
+ "created_at": "2026-01-01T00:00:00Z",
24
+ "updated_at": "2026-01-02T00:00:00Z"
25
+ }
26
+ ]
@@ -16,6 +16,7 @@
16
16
  * Auth: admin key or admin session (every other caller class gets 403).
17
17
  */
18
18
 
19
+ import { CLIENT_ID_RE, SLUG_RE } from "./installation-ports.ts";
19
20
  import { createPrivateKey } from "node:crypto";
20
21
  import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
21
22
  import { HTTPException } from "hono/http-exception";
@@ -84,9 +85,6 @@ const REQUIRED_PERMISSION: Record<
84
85
  lookup: { name: "metadata", levels: ["read", "write"] },
85
86
  };
86
87
 
87
- const SLUG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
88
- /** GitHub App client ids look like Iv1.<hex> / Iv23li<alnum>; no path or query chars. */
89
- const CLIENT_ID_RE = /^[A-Za-z0-9._-]{1,64}$/;
90
88
  const ORG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
91
89
 
92
90
  // ─── Schemas ──────────────────────────────────────────────────────────────────
@@ -142,7 +142,12 @@ export interface InstallableOrganization {
142
142
 
143
143
  export interface AppInstallation {
144
144
  id: number;
145
- appId: number;
145
+ /**
146
+ * GitHub's enterprise-organization-installation schema omits app_id, so this
147
+ * is undefined for installations listed through the enterprise API; match by
148
+ * clientId or appSlug instead.
149
+ */
150
+ appId?: number;
146
151
  appSlug: string;
147
152
  clientId?: string;
148
153
  accountLogin: string;
@@ -504,7 +509,7 @@ export class GithubEnterpriseClient {
504
509
 
505
510
  interface RawInstallation {
506
511
  id: number;
507
- app_id: number;
512
+ app_id?: number;
508
513
  app_slug: string;
509
514
  client_id?: string;
510
515
  account?: { login?: string } | null;
@@ -521,10 +526,10 @@ function toRepository(r: RawRepository): AccessibleRepository {
521
526
  return { id: r.id, name: r.name, fullName: r.full_name };
522
527
  }
523
528
 
524
- function toInstallation(r: RawInstallation): AppInstallation {
529
+ export function toInstallation(r: RawInstallation): AppInstallation {
525
530
  return {
526
531
  id: r.id,
527
- appId: r.app_id,
532
+ ...(r.app_id === undefined ? {} : { appId: r.app_id }),
528
533
  appSlug: r.app_slug,
529
534
  ...(r.client_id ? { clientId: r.client_id } : {}),
530
535
  accountLogin: r.account?.login ?? "",
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * admin/src/github-installation-wiring.ts
3
- * Composition of the installation repos and lost-access removal services for
3
+ * Composition of the installation repos list and GitHub Sync services for
4
4
  * admin boot, from the stored installer App and an optional host-supplied
5
5
  * OpenWorkPort. The agent App id comes from each agent's own stored
6
6
  * GH_APP_ID; without an open-work port nothing is listed or blocked.
@@ -23,7 +23,7 @@ import {
23
23
  InstallationReposService,
24
24
  type InstallationReposPort,
25
25
  } from "./installation-repos-service.ts";
26
- import { LostAccessRemovalService } from "./lost-access-removal.ts";
26
+ import { GithubSyncService } from "./github-sync.ts";
27
27
  import type { OpenWorkPort } from "./reconcile-open-work.ts";
28
28
 
29
29
  export interface GithubInstallationWiringDeps {
@@ -85,8 +85,7 @@ export interface InstallationReposWiringDeps
85
85
 
86
86
  /**
87
87
  * The `githubInstallationRepos` dep for createAdminApp (IRM-1.2): direct
88
- * list/add/remove on the agent App's installation, audited in the
89
- * GitHubReconcileAudit table.
88
+ * list the repos on the agent App's installation.
90
89
  */
91
90
  export function createGithubInstallationRepos(
92
91
  deps: InstallationReposWiringDeps,
@@ -98,21 +97,20 @@ export function createGithubInstallationRepos(
98
97
  installation:
99
98
  deps.installationRepos ??
100
99
  createInstallationReposPort(createInstallationPort(client), client),
101
- audit: new PrismaInstallationAudit(deps.prisma),
102
100
  });
103
101
  }
104
102
 
105
103
  /**
106
- * The `githubLostAccessRemoval` dep for createAdminApp (IRM-1.3): removes the
107
- * repos the bound user can no longer push to from the agent App's
108
- * installation and the agent's repos, blocking open work through the
109
- * optional host OpenWorkPort.
104
+ * The `githubSync` dep for createAdminApp (IRM-3.1): installation adds and
105
+ * removes plus agent-repo removals for the repos the bound user can or can no
106
+ * longer push to, audited once and blocking open work through the optional
107
+ * host OpenWorkPort.
110
108
  */
111
- export function createGithubLostAccessRemoval(
109
+ export function createGithubSync(
112
110
  deps: InstallationReposWiringDeps,
113
- ): LostAccessRemovalService {
111
+ ): GithubSyncService {
114
112
  const client = lazyInstallerClient(deps);
115
- return new LostAccessRemovalService({
113
+ return new GithubSyncService({
116
114
  agents: new PrismaInstallationAgents(deps.prisma, deps.agentEnv),
117
115
  access: deps.access,
118
116
  installation: