@app-vitals/agent-admin 1.20.0 → 1.21.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.
@@ -3675,6 +3675,13 @@
3675
3675
  "null"
3676
3676
  ]
3677
3677
  },
3678
+ "agentAppReason": {
3679
+ "type": [
3680
+ "string",
3681
+ "null"
3682
+ ],
3683
+ "description": "Why the agent's App cannot be installed through the enterprise API (private App with no stored GH_APP_CLIENT_ID, or no App at all). Null when it can; present only with agentId."
3684
+ },
3678
3685
  "orgs": {
3679
3686
  "type": "array",
3680
3687
  "items": {
@@ -7293,7 +7300,7 @@
7293
7300
  "github-enterprise"
7294
7301
  ],
7295
7302
  "summary": "Install an agent's App on an organization",
7296
- "description": "Admin-only. Resolves the agent's stored GH_APP_SLUG, looks the App up through the public GET /apps/{slug} to get its client id, and installs it on the org with the installer App. 422 for an agent with no slug and for a private App (not visible to the public lookup); 409 when the installer App is not configured.",
7303
+ "description": "Admin-only. Uses the agent's stored GH_APP_CLIENT_ID when set; otherwise resolves the stored GH_APP_SLUG and looks the App up through the public GET /apps/{slug} to get its client id. Installs it on the org with the installer App. 422 for an agent with neither a stored client id nor a slug, and for a private App (not visible to the public lookup) with no stored client id; 409 when the installer App is not configured.",
7297
7304
  "parameters": [
7298
7305
  {
7299
7306
  "schema": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@app-vitals/agent-admin",
3
- "version": "1.20.0",
3
+ "version": "1.21.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@anthropic-ai/sdk": "0.135.0",
45
- "@app-vitals/agent-lib": "1.20.0",
45
+ "@app-vitals/agent-lib": "1.21.0",
46
46
  "@hono/zod-openapi": "^1.0.0",
47
47
  "@prisma/adapter-pg": "^7.10.0",
48
48
  "@prisma/client": "^7.10.0",
@@ -42,6 +42,8 @@ export interface GheOrg {
42
42
  export interface GheOrgs {
43
43
  configured: boolean;
44
44
  agentAppSlug?: string | null;
45
+ /** Why the agent's App cannot be installed (e.g. private, no client id). */
46
+ agentAppReason?: string | null;
45
47
  orgs: GheOrg[];
46
48
  }
47
49
 
@@ -177,7 +179,10 @@ function orgsCard(opts: GithubEnterpriseSettingsPageOpts): string {
177
179
  </tr>`;
178
180
  })
179
181
  .join("\n");
180
- body = `<table class="data-table">
182
+ const reason = opts.orgs?.agentAppReason
183
+ ? `<div class="alert alert-error">${escapeHtml(opts.orgs.agentAppReason)}</div>`
184
+ : "";
185
+ body = `${reason}<table class="data-table">
181
186
  <thead><tr><th>Organization</th><th>Lookup app</th><th>Agent app</th><th></th></tr></thead>
182
187
  <tbody>${rows}</tbody>
183
188
  </table>`;
@@ -159,6 +159,29 @@ describe("renderGithubEnterpriseSettingsPage", () => {
159
159
  expect(html).toContain("Installed");
160
160
  });
161
161
 
162
+ test("check agent shows the private-App reason, escaped", () => {
163
+ const html = renderGithubEnterpriseSettingsPage({
164
+ ...base,
165
+ agentId: "agent-1",
166
+ status: { configured: true, installer: app(), lookup: null },
167
+ orgs: {
168
+ configured: true,
169
+ agentAppSlug: "agent-app",
170
+ agentAppReason: "the agent's App 'agent-app' is private <b>",
171
+ orgs: [
172
+ {
173
+ id: 1,
174
+ login: "org-a",
175
+ lookupAppInstalled: null,
176
+ agentAppInstalled: false,
177
+ },
178
+ ],
179
+ },
180
+ });
181
+ expect(html).toContain("is private &lt;b&gt;");
182
+ expect(html).not.toContain("private <b>");
183
+ });
184
+
162
185
  test("org list failure and top-level error/notice are escaped messages", () => {
163
186
  const html = renderGithubEnterpriseSettingsPage({
164
187
  ...base,
@@ -207,6 +207,45 @@ describeOrSkip("github-enterprise API (integration)", () => {
207
207
  expect(await res.json()).toMatchObject({ created: true, org: "org-1" });
208
208
  });
209
209
 
210
+ it("install prefers the stored GH_APP_CLIENT_ID and skips the public lookup", async () => {
211
+ const agent = await prisma.agent.create({ data: { name: "Ghe Agent" } });
212
+ await new AgentEnvService(prisma, identityCrypto).upsert(agent.id, {
213
+ GH_APP_SLUG: "example-agent-app",
214
+ GH_APP_CLIENT_ID: "Iv1.targetapp0001",
215
+ });
216
+ // No "GET /apps/..." route: a public lookup would throw.
217
+ const app = build({
218
+ "POST /enterprises/acme/apps/organizations/org-1/installations":
219
+ "install_created",
220
+ });
221
+ await put(app);
222
+ const res = await app.request("/github-enterprise/orgs/org-1/install", {
223
+ method: "POST",
224
+ headers: { ...ADMIN, "Content-Type": "application/json" },
225
+ body: JSON.stringify({ agentId: agent.id }),
226
+ });
227
+ expect(res.status).toBe(200);
228
+ expect(await res.json()).toMatchObject({ created: true, org: "org-1" });
229
+ });
230
+
231
+ it("install refuses a private App when no client id is stored", async () => {
232
+ const agent = await prisma.agent.create({ data: { name: "Ghe Agent" } });
233
+ await new AgentEnvService(prisma, identityCrypto).upsert(agent.id, {
234
+ GH_APP_SLUG: "example-agent-app",
235
+ });
236
+ const app = build({
237
+ "GET /apps/example-agent-app": "public_app_private_404",
238
+ });
239
+ await put(app);
240
+ const res = await app.request("/github-enterprise/orgs/org-1/install", {
241
+ method: "POST",
242
+ headers: { ...ADMIN, "Content-Type": "application/json" },
243
+ body: JSON.stringify({ agentId: agent.id }),
244
+ });
245
+ expect(res.status).toBe(422);
246
+ expect(((await res.json()) as { error: string }).error).toMatch(/private/i);
247
+ });
248
+
210
249
  it("isolation: no agent-facing path returns the stored key", async () => {
211
250
  const agent = await prisma.agent.create({ data: { name: "Iso Agent" } });
212
251
  const envService = new AgentEnvService(prisma, identityCrypto);
@@ -452,6 +452,53 @@ describe("GET orgs", () => {
452
452
  });
453
453
  });
454
454
 
455
+ it("check agent reports the private-App reason when no client id is stored", async () => {
456
+ const h = build({
457
+ routes: {
458
+ [ORGS]: "installable_orgs_success",
459
+ [INSTS]: "org_installations_success",
460
+ "GET /enterprises/acme/apps/organizations/org-2/installations":
461
+ "installable_orgs_empty",
462
+ "GET /apps/example-agent-app": "public_app_private_404",
463
+ },
464
+ });
465
+ await seed(h);
466
+ const res = await h.app.request("/github-enterprise/orgs?agentId=agent-1", {
467
+ headers: ADMIN,
468
+ });
469
+ expect(res.status).toBe(200);
470
+ const body = (await res.json()) as { agentAppReason: string | null };
471
+ expect(body.agentAppReason).toMatch(/private/i);
472
+ });
473
+
474
+ it("check agent has no reason when a client id is stored (no public lookup)", async () => {
475
+ const h = build({
476
+ agents: {
477
+ "agent-1": {
478
+ GH_APP_SLUG: "example-agent-app",
479
+ GH_APP_CLIENT_ID: "Iv1.targetapp0001",
480
+ },
481
+ },
482
+ routes: {
483
+ [ORGS]: "installable_orgs_success",
484
+ [INSTS]: "org_installations_success",
485
+ "GET /enterprises/acme/apps/organizations/org-2/installations":
486
+ "installable_orgs_empty",
487
+ },
488
+ });
489
+ await seed(h);
490
+ const res = await h.app.request("/github-enterprise/orgs?agentId=agent-1", {
491
+ headers: ADMIN,
492
+ });
493
+ expect(res.status).toBe(200);
494
+ expect(
495
+ ((await res.json()) as { agentAppReason: string | null }).agentAppReason,
496
+ ).toBeNull();
497
+ expect(h.calls.some((k) => k.includes("/apps/example-agent-app"))).toBe(
498
+ false,
499
+ );
500
+ });
501
+
455
502
  it("without agentId or lookup app the flags are null", async () => {
456
503
  const h = build({
457
504
  routes: {
@@ -519,6 +566,34 @@ describe("POST install", () => {
519
566
  expect(h.calls).not.toContain(INSTALL);
520
567
  });
521
568
 
569
+ it("uses the agent's stored GH_APP_CLIENT_ID and skips the public lookup", async () => {
570
+ const h = build({
571
+ agents: {
572
+ "agent-1": {
573
+ GH_APP_SLUG: "example-agent-app",
574
+ GH_APP_CLIENT_ID: "Iv1.targetapp0001",
575
+ },
576
+ },
577
+ routes: { [INSTALL]: "install_created" },
578
+ });
579
+ await seed(h);
580
+ const res = await post(h, { agentId: "agent-1" });
581
+ expect(res.status).toBe(200);
582
+ expect(h.calls).toContain(INSTALL);
583
+ expect(h.calls.some((k) => k.includes("/apps/example-agent-app"))).toBe(
584
+ false,
585
+ );
586
+ });
587
+
588
+ it("installs a slug-less agent that has a stored client id", async () => {
589
+ const h = build({
590
+ agents: { "agent-1": { GH_APP_CLIENT_ID: "Iv1.targetapp0001" } },
591
+ routes: { [INSTALL]: "install_created" },
592
+ });
593
+ await seed(h);
594
+ expect((await post(h, { agentId: "agent-1" })).status).toBe(200);
595
+ });
596
+
522
597
  it("refuses an App with no client id", async () => {
523
598
  const h = build({
524
599
  routes: { "GET /apps/example-agent-app": "public_app_no_client_id" },
@@ -84,6 +84,8 @@ const REQUIRED_PERMISSION: Record<
84
84
  };
85
85
 
86
86
  const SLUG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
87
+ /** GitHub App client ids look like Iv1.<hex> / Iv23li<alnum>; no path or query chars. */
88
+ const CLIENT_ID_RE = /^[A-Za-z0-9._-]{1,64}$/;
87
89
  const ORG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
88
90
 
89
91
  // ─── Schemas ──────────────────────────────────────────────────────────────────
@@ -178,6 +180,14 @@ const OrgsResultSchema = z
178
180
  .object({
179
181
  configured: z.boolean(),
180
182
  agentAppSlug: z.string().nullable().optional(),
183
+ agentAppReason: z
184
+ .string()
185
+ .nullable()
186
+ .optional()
187
+ .openapi({
188
+ description:
189
+ "Why the agent's App cannot be installed through the enterprise API (private App with no stored GH_APP_CLIENT_ID, or no App at all). Null when it can; present only with agentId.",
190
+ }),
181
191
  orgs: z.array(
182
192
  z.object({
183
193
  id: z.number(),
@@ -339,7 +349,7 @@ const installRoute = createRoute({
339
349
  tags,
340
350
  summary: "Install an agent's App on an organization",
341
351
  description:
342
- "Admin-only. Resolves the agent's stored GH_APP_SLUG, looks the App up through the public GET /apps/{slug} to get its client id, and installs it on the org with the installer App. 422 for an agent with no slug and for a private App (not visible to the public lookup); 409 when the installer App is not configured.",
352
+ "Admin-only. Uses the agent's stored GH_APP_CLIENT_ID when set; otherwise resolves the stored GH_APP_SLUG and looks the App up through the public GET /apps/{slug} to get its client id. Installs it on the org with the installer App. 422 for an agent with neither a stored client id nor a slug, and for a private App (not visible to the public lookup) with no stored client id; 409 when the installer App is not configured.",
343
353
  request: {
344
354
  params: OrgParamSchema,
345
355
  body: { content: { "application/json": { schema: InstallBodySchema } } },
@@ -567,21 +577,54 @@ export function createGithubEnterpriseApp(
567
577
  });
568
578
  };
569
579
 
570
- /** Agent's stored App slug: null when absent/invalid. 404 when the agent is unknown. */
571
- const agentSlug = async (
580
+ /**
581
+ * Agent's stored App slug (null when absent/invalid) and stored client id
582
+ * (null when absent/malformed). 404 when the agent is unknown.
583
+ */
584
+ const agentApp = async (
572
585
  agentId: string,
573
- ): Promise<{ slug: string | null; invalid: boolean }> => {
586
+ ): Promise<{
587
+ slug: string | null;
588
+ invalid: boolean;
589
+ clientId: string | null;
590
+ }> => {
574
591
  if (!(await deps.agentService.getSummary(agentId))) {
575
592
  throw new NotFoundError(`agent ${agentId} not found`);
576
593
  }
577
- const slug = (await deps.agentEnvService.getByAgentId(agentId))?.env
578
- .GH_APP_SLUG;
579
- if (!slug) return { slug: null, invalid: false };
594
+ const env = (await deps.agentEnvService.getByAgentId(agentId))?.env;
595
+ const storedClientId = env?.GH_APP_CLIENT_ID?.trim();
596
+ const clientId =
597
+ storedClientId && CLIENT_ID_RE.test(storedClientId)
598
+ ? storedClientId
599
+ : null;
600
+ const slug = env?.GH_APP_SLUG;
601
+ if (!slug) return { slug: null, invalid: false, clientId };
580
602
  return SLUG_RE.test(slug)
581
- ? { slug, invalid: false }
582
- : { slug: null, invalid: true };
603
+ ? { slug, invalid: false, clientId }
604
+ : { slug: null, invalid: true, clientId };
583
605
  };
584
606
 
607
+ /** Public-lookup client id; reason is set when the App is private (404). */
608
+ const publicClientId = async (
609
+ slug: string,
610
+ ): Promise<{ clientId: string | null; privateApp: boolean }> => {
611
+ try {
612
+ const app = await fetchPublicApp(slug, {
613
+ apiBase: deps.apiBase,
614
+ fetchFn: deps.fetchFn,
615
+ });
616
+ return { clientId: app.clientId, privateApp: false };
617
+ } catch (err) {
618
+ if (err instanceof GithubEnterpriseError && err.kind === "not_found") {
619
+ return { clientId: null, privateApp: true };
620
+ }
621
+ throw err;
622
+ }
623
+ };
624
+
625
+ const privateAppMessage = (slug: string) =>
626
+ `the agent's App '${slug}' is private (not visible to the public lookup) and cannot be installed through the enterprise API; store its GH_APP_CLIENT_ID on the agent`;
627
+
585
628
  app.openapi(statusRoute, async (c) => {
586
629
  requireAdmin(c);
587
630
  const rows = await appService.list();
@@ -712,7 +755,25 @@ export function createGithubEnterpriseApp(
712
755
  if (!installer) return c.json({ configured: false, orgs: [] }, 200);
713
756
 
714
757
  let agentAppSlug: string | null = null;
715
- if (agentId) agentAppSlug = (await agentSlug(agentId)).slug;
758
+ let agentAppReason: string | null = null;
759
+ if (agentId) {
760
+ const agent = await agentApp(agentId);
761
+ agentAppSlug = agent.slug;
762
+ if (!agent.clientId) {
763
+ if (!agent.slug) {
764
+ agentAppReason =
765
+ "the agent has no GH_APP_SLUG or GH_APP_CLIENT_ID; it has no GitHub App to install";
766
+ } else {
767
+ try {
768
+ if ((await publicClientId(agent.slug)).privateApp) {
769
+ agentAppReason = privateAppMessage(agent.slug);
770
+ }
771
+ } catch {
772
+ // The reason is advisory; a lookup failure must not fail the listing.
773
+ }
774
+ }
775
+ }
776
+ }
716
777
  const lookup = await appService.get("lookup");
717
778
 
718
779
  const client = await clientFor(installer);
@@ -739,7 +800,7 @@ export function createGithubEnterpriseApp(
739
800
  return c.json(
740
801
  {
741
802
  configured: true,
742
- ...(agentId ? { agentAppSlug } : {}),
803
+ ...(agentId ? { agentAppSlug, agentAppReason } : {}),
743
804
  orgs: result,
744
805
  },
745
806
  200,
@@ -755,38 +816,29 @@ export function createGithubEnterpriseApp(
755
816
  if (!installer) {
756
817
  throw new ConflictError("the installer App is not configured");
757
818
  }
758
- const { slug, invalid } = await agentSlug(body.agentId);
759
- if (invalid) {
760
- throw new UnprocessableEntityError(
761
- "the agent's stored GH_APP_SLUG is not a valid GitHub App slug",
762
- );
763
- }
764
- if (!slug) {
765
- throw new UnprocessableEntityError(
766
- "the agent has no GH_APP_SLUG; it has no GitHub App to install",
767
- );
768
- }
769
-
770
- let clientId: string | null;
771
- try {
772
- clientId = (
773
- await fetchPublicApp(slug, {
774
- apiBase: deps.apiBase,
775
- fetchFn: deps.fetchFn,
776
- })
777
- ).clientId;
778
- } catch (err) {
779
- if (err instanceof GithubEnterpriseError && err.kind === "not_found") {
819
+ const agent = await agentApp(body.agentId);
820
+ let clientId = agent.clientId;
821
+ if (!clientId) {
822
+ if (agent.invalid) {
780
823
  throw new UnprocessableEntityError(
781
- `the agent's App '${slug}' is private (not visible to the public lookup) and cannot be installed through the enterprise API`,
824
+ "the agent's stored GH_APP_SLUG is not a valid GitHub App slug",
825
+ );
826
+ }
827
+ if (!agent.slug) {
828
+ throw new UnprocessableEntityError(
829
+ "the agent has no GH_APP_SLUG or GH_APP_CLIENT_ID; it has no GitHub App to install",
830
+ );
831
+ }
832
+ const lookedUp = await publicClientId(agent.slug);
833
+ if (lookedUp.privateApp) {
834
+ throw new UnprocessableEntityError(privateAppMessage(agent.slug));
835
+ }
836
+ clientId = lookedUp.clientId;
837
+ if (!clientId) {
838
+ throw new UnprocessableEntityError(
839
+ `the agent's App '${agent.slug}' did not expose a client id and cannot be installed`,
782
840
  );
783
841
  }
784
- throw err;
785
- }
786
- if (!clientId) {
787
- throw new UnprocessableEntityError(
788
- `the agent's App '${slug}' did not expose a client id and cannot be installed`,
789
- );
790
842
  }
791
843
 
792
844
  const client = await clientFor(installer);