@app-vitals/agent-admin 1.35.0 → 1.36.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.
Files changed (36) hide show
  1. package/openapi.platform.json +136 -766
  2. package/package.json +2 -2
  3. package/src/admin-boot.ts +4 -15
  4. package/src/admin-extension.ts +3 -3
  5. package/src/admin-ui-github-identity.smoke.test.ts +24 -116
  6. package/src/admin-ui-installation-repos-pages.ts +198 -231
  7. package/src/admin-ui-installation-repos-pages.unit.test.ts +199 -0
  8. package/src/admin-ui-installation-repos.smoke.test.ts +166 -173
  9. package/src/admin-ui-pages.ts +2 -2
  10. package/src/admin-ui.ts +103 -171
  11. package/src/agents-api.smoke.test.ts +195 -642
  12. package/src/agents-api.ts +86 -477
  13. package/src/github-enterprise-isolation.unit.test.ts +4 -4
  14. package/src/{github-reconcile-wiring.ts → github-installation-wiring.ts} +24 -46
  15. package/src/github-installation-wiring.unit.test.ts +115 -0
  16. package/src/github-installation.fixtures.ts +76 -0
  17. package/src/github-sync.ts +407 -0
  18. package/src/{lost-access-removal.unit.test.ts → github-sync.unit.test.ts} +220 -115
  19. package/src/installation-ports.integration.test.ts +89 -0
  20. package/src/installation-ports.ts +207 -0
  21. package/src/installation-ports.unit.test.ts +51 -0
  22. package/src/installation-repos-service.ts +10 -105
  23. package/src/installation-repos-service.unit.test.ts +5 -90
  24. package/src/reconcile-open-work.ts +5 -7
  25. package/src/repo-access-rules.ts +58 -0
  26. package/src/repo-access-rules.unit.test.ts +98 -0
  27. package/src/admin-ui-github-reconcile-pages.ts +0 -208
  28. package/src/admin-ui-github-reconcile-pages.unit.test.ts +0 -226
  29. package/src/github-reconcile-routes.integration.test.ts +0 -206
  30. package/src/github-reconcile-wiring.unit.test.ts +0 -170
  31. package/src/lost-access-removal.ts +0 -333
  32. package/src/reconcile-apply-service.integration.test.ts +0 -271
  33. package/src/reconcile-apply-service.ts +0 -439
  34. package/src/reconcile-apply-service.unit.test.ts +0 -279
  35. package/src/reconcile-planner.ts +0 -264
  36. package/src/reconcile-planner.unit.test.ts +0 -274
@@ -17,7 +17,7 @@ import {
17
17
  renderGithubUsernameRow,
18
18
  } from "./admin-ui-github-identity-pages.ts";
19
19
  import {
20
- renderInstallationReposCard,
20
+ renderGithubSyncCard,
21
21
  renderUncoveredBadge,
22
22
  uncoveredRepos,
23
23
  } from "./admin-ui-installation-repos-pages.ts";
@@ -1855,7 +1855,7 @@ export function renderAgentDetailPage(
1855
1855
  ${identityRow("Federation rule ID", agent.anthropicFederationRuleId, true)}
1856
1856
  ${showGithubIdentity ? renderGithubUsernameRow(agent.id, agent.githubUsername) : ""}
1857
1857
  </tbody></table>
1858
- </div>${showGithubIdentity ? renderGithubAccessCard(agent.id) : ""}${showGithubIdentity && "GH_APP_ID" in envVars ? renderInstallationReposCard(agent.id) : ""}`;
1858
+ </div>${showGithubIdentity ? renderGithubAccessCard(agent.id) : ""}${showGithubIdentity ? renderGithubSyncCard(agent.id, { hasAppId: "GH_APP_ID" in envVars, githubUsername: agent.githubUsername }) : ""}`;
1859
1859
 
1860
1860
  // Only agents with a GitHub App can make API-signed commits. Checked state
1861
1861
  // is the SHIPWRIGHT_SIGNED_COMMITS env row; when the box is hidden the field
package/src/admin-ui.ts CHANGED
@@ -57,24 +57,17 @@ import {
57
57
  renderGithubAccessError,
58
58
  renderGithubAccessResult,
59
59
  } from "./admin-ui-github-identity-pages.ts";
60
- import {
61
- type GithubReconcileApplyView,
62
- type GithubReconcilePlanView,
63
- renderGithubReconcileApplyResult,
64
- renderGithubReconcilePlanResult,
65
- } from "./admin-ui-github-reconcile-pages.ts";
66
60
  import {
67
61
  actuallyBlockedWork,
62
+ type GithubSyncApplyView,
63
+ type GithubSyncPreviewView,
68
64
  type InstallationReposView,
69
- type LostAccessPreviewView,
70
- type LostAccessRemoveView,
71
65
  type OpenWorkView,
72
66
  type RepoRemovalPreviewView,
73
- renderInstallationChangeResult,
74
67
  renderInstallationReposBody,
75
68
  renderInstallationReposError,
76
- renderLostAccessPreview,
77
- renderLostAccessResult,
69
+ renderSyncConfirm,
70
+ renderSyncResult,
78
71
  renderRepoRemovalConfirm,
79
72
  renderRepoRemovalResult,
80
73
  } from "./admin-ui-installation-repos-pages.ts";
@@ -1835,8 +1828,10 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
1835
1828
  }
1836
1829
  };
1837
1830
 
1838
- // True only for a platform admin when the enterprise GitHub apps are
1839
- // configured (GHE-1.3 status) and the agents API seam is wired.
1831
+ // True only for a platform admin when BOTH the installer and the lookup
1832
+ // enterprise GitHub Apps are configured (GHE-1.3 status) and the agents API
1833
+ // seam is wired. One role alone is not enough: the access preview and Sync
1834
+ // both need the pair.
1840
1835
  const githubEnterpriseConfigured = async (
1841
1836
  c: Context<AdminUIEnv>,
1842
1837
  ): Promise<boolean> => {
@@ -1848,8 +1843,15 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
1848
1843
  );
1849
1844
  if (!res?.ok) return false;
1850
1845
  try {
1846
+ const status = (await res.json()) as {
1847
+ configured?: unknown;
1848
+ installer?: unknown;
1849
+ lookup?: unknown;
1850
+ };
1851
1851
  return (
1852
- ((await res.json()) as { configured?: unknown }).configured === true
1852
+ status.configured === true &&
1853
+ status.installer != null &&
1854
+ status.lookup != null
1853
1855
  );
1854
1856
  } catch {
1855
1857
  return false;
@@ -1924,84 +1926,8 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
1924
1926
  );
1925
1927
  });
1926
1928
 
1927
- // GHE-3.5: thin proxies over the GHE-3.4 plan/apply routes. They return
1928
- // HTML fragments for the Reconcile card; the agents API enforces admin too.
1929
- const reconcileProxy = async (
1930
- c: Context<AdminUIEnv>,
1931
- step: "plan" | "apply",
1932
- ): Promise<{ res: Response | null; body: Record<string, unknown> }> => {
1933
- let body: Record<string, unknown> = {};
1934
- try {
1935
- const parsed = await c.req.json();
1936
- if (parsed && typeof parsed === "object") body = parsed;
1937
- } catch {
1938
- // empty body
1939
- }
1940
- const res = await callApi(
1941
- c,
1942
- agentsApi as NonNullable<typeof agentsApi>,
1943
- `/agents/${encodeURIComponent(c.req.param("id") ?? "")}/github-reconcile/${step}`,
1944
- {
1945
- method: "POST",
1946
- headers: { "content-type": "application/json" },
1947
- body: JSON.stringify(body),
1948
- },
1949
- );
1950
- return { res, body };
1951
- };
1952
- const reconcileError = async (res: Response | null) => {
1953
- let message = "The reconcile request failed.";
1954
- try {
1955
- const j = (await res?.json()) as { error?: unknown };
1956
- if (typeof j.error === "string" && j.error) message = j.error;
1957
- } catch {
1958
- // keep the generic message
1959
- }
1960
- return html(renderGithubAccessError(message), {
1961
- status: res && res.status >= 400 ? res.status : 502,
1962
- });
1963
- };
1964
-
1965
- app.post(
1966
- "/admin/agents/:id/github-reconcile/plan",
1967
- requireAuth,
1968
- async (c) => {
1969
- if (!c.var.isAdmin || !agentsApi) {
1970
- return new Response("Forbidden", { status: 403 });
1971
- }
1972
- const { res, body } = await reconcileProxy(c, "plan");
1973
- if (!res?.ok) return reconcileError(res);
1974
- const adds = Array.isArray(body.addRepos)
1975
- ? body.addRepos.filter((r): r is string => typeof r === "string")
1976
- : [];
1977
- return html(
1978
- renderGithubReconcilePlanResult(
1979
- (await res.json()) as GithubReconcilePlanView,
1980
- adds,
1981
- ),
1982
- );
1983
- },
1984
- );
1985
-
1986
- app.post(
1987
- "/admin/agents/:id/github-reconcile/apply",
1988
- requireAuth,
1989
- async (c) => {
1990
- if (!c.var.isAdmin || !agentsApi) {
1991
- return new Response("Forbidden", { status: 403 });
1992
- }
1993
- const { res } = await reconcileProxy(c, "apply");
1994
- if (!res?.ok) return reconcileError(res);
1995
- return html(
1996
- renderGithubReconcileApplyResult(
1997
- (await res.json()) as GithubReconcileApplyView,
1998
- ),
1999
- );
2000
- },
2001
- );
2002
-
2003
- // IRM-1.4: thin proxies over the installation repos (IRM-1.2) and
2004
- // lost-access (IRM-1.3) routes, plus the repo-removal preview (IRM-1.7).
1929
+ // IRM-1.4: thin proxies over the installation repos list (IRM-1.2) and
1930
+ // the repo-removal preview (IRM-1.7).
2005
1931
  // They return HTML fragments for the Installation repos card and the agent's
2006
1932
  // repos list; the agents API enforces platform-admin too.
2007
1933
  const apiErrorFragment = async (res: Response | null, fallback: string) => {
@@ -2026,14 +1952,6 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2026
1952
  }
2027
1953
  return {} as Record<string, unknown>;
2028
1954
  };
2029
- const stringList = (v: unknown): string[] =>
2030
- Array.isArray(v) ? v.filter((r): r is string => typeof r === "string") : [];
2031
- const postJson = (c: Context<AdminUIEnv>, path: string, body: unknown) =>
2032
- callApi(c, agentsApi as NonNullable<typeof agentsApi>, path, {
2033
- method: "POST",
2034
- headers: { "content-type": "application/json" },
2035
- body: JSON.stringify(body),
2036
- });
2037
1955
  const apiId = (c: Context<AdminUIEnv>) =>
2038
1956
  encodeURIComponent(c.req.param("id") ?? "");
2039
1957
 
@@ -2041,7 +1959,6 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2041
1959
  if (!c.var.isAdmin || !agentsApi) {
2042
1960
  return new Response("Forbidden", { status: 403 });
2043
1961
  }
2044
- const agentId = c.req.param("id") ?? "";
2045
1962
  const res = await callApi(
2046
1963
  c,
2047
1964
  agentsApi,
@@ -2050,101 +1967,116 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2050
1967
  if (!res?.ok) {
2051
1968
  return apiErrorFragment(res, "Could not load the installation repos.");
2052
1969
  }
2053
- const agent = await agentService.getDetail(agentId);
2054
- return html(
2055
- renderInstallationReposBody(
2056
- (await res.json()) as InstallationReposView,
2057
- agent?.githubUsername,
2058
- ),
2059
- );
2060
- });
2061
-
2062
- for (const kind of ["add", "remove"] as const) {
2063
- app.post(
2064
- `/admin/agents/:id/github-installation/repos/${kind}`,
2065
- requireAuth,
2066
- async (c) => {
2067
- if (!c.var.isAdmin || !agentsApi) {
2068
- return new Response("Forbidden", { status: 403 });
2069
- }
2070
- const repos = stringList((await readBody(c)).repos);
2071
- const res = await postJson(
2072
- c,
2073
- `/agents/${apiId(c)}/github-installation/repos${kind === "add" ? "" : "/remove"}`,
2074
- { repos },
2075
- );
2076
- if (!res?.ok) {
2077
- return apiErrorFragment(res, `Could not ${kind} the repos.`);
2078
- }
2079
- return html(
2080
- renderInstallationChangeResult(
2081
- kind,
2082
- (await res.json()) as { auditId?: string; repos?: string[] },
2083
- ),
2084
- );
2085
- },
2086
- );
2087
- }
2088
-
2089
- app.get(
2090
- "/admin/agents/:id/github-lost-access/preview",
2091
- requireAuth,
2092
- async (c) => {
2093
- if (!c.var.isAdmin || !agentsApi) {
2094
- return new Response("Forbidden", { status: 403 });
2095
- }
2096
- const res = await callApi(
1970
+ const view = (await res.json()) as InstallationReposView;
1971
+ // No installation matched the numeric GH_APP_ID. If the agent still has an
1972
+ // installation by slug (the Installations card), the id is probably wrong.
1973
+ let appIdMismatch = false;
1974
+ if (view.orgs.length === 0) {
1975
+ const snap = await callApi(
2097
1976
  c,
2098
1977
  agentsApi,
2099
- `/agents/${apiId(c)}/github-lost-access/preview`,
1978
+ `/agents/${apiId(c)}/github-installations`,
2100
1979
  );
2101
- if (!res?.ok) {
2102
- return apiErrorFragment(res, "Could not preview the removal.");
1980
+ if (snap?.ok) {
1981
+ try {
1982
+ const j = (await snap.json()) as {
1983
+ snapshot?: { installations?: unknown[] };
1984
+ };
1985
+ appIdMismatch = (j.snapshot?.installations?.length ?? 0) > 0;
1986
+ } catch {
1987
+ // unknown: not reported as a mismatch
1988
+ }
2103
1989
  }
2104
- return html(
2105
- renderLostAccessPreview((await res.json()) as LostAccessPreviewView),
2106
- );
2107
- },
2108
- );
1990
+ }
1991
+ return html(renderInstallationReposBody(view, { appIdMismatch }));
1992
+ });
1993
+
1994
+ // IRM-3.2: the single Sync confirm (over the IRM-3.1 preview) and its apply.
1995
+ app.get("/admin/agents/:id/github-sync/confirm", requireAuth, async (c) => {
1996
+ if (!c.var.isAdmin || !agentsApi) {
1997
+ return new Response("Forbidden", { status: 403 });
1998
+ }
1999
+ const res = await callApi(
2000
+ c,
2001
+ agentsApi,
2002
+ `/agents/${apiId(c)}/github-sync/preview`,
2003
+ );
2004
+ if (!res?.ok) return apiErrorFragment(res, "Could not preview the sync.");
2005
+ return html(
2006
+ renderSyncConfirm(
2007
+ c.req.param("id") ?? "",
2008
+ (await res.json()) as GithubSyncPreviewView,
2009
+ ),
2010
+ );
2011
+ });
2109
2012
 
2110
2013
  app.post(
2111
- "/admin/agents/:id/github-lost-access/remove",
2014
+ "/admin/agents/:id/github-sync/apply-confirmed",
2112
2015
  requireAuth,
2113
2016
  async (c) => {
2114
2017
  if (!c.var.isAdmin || !agentsApi) {
2115
2018
  return new Response("Forbidden", { status: 403 });
2116
2019
  }
2117
2020
  const body = await readBody(c);
2118
- const repos = stringList(body.repos);
2119
- // The open work the removal will block, read just before it happens so
2120
- // the result can show what was actually blocked.
2021
+ const list = (v: unknown): string[] | null =>
2022
+ v === undefined
2023
+ ? []
2024
+ : Array.isArray(v) &&
2025
+ v.every((r) => typeof r === "string" && isOrgRepo(r))
2026
+ ? (v as string[])
2027
+ : null;
2028
+ const appAdds = list(body.appAdds);
2029
+ const appRemoves = list(body.appRemoves);
2030
+ const agentRemoves = list(body.agentRemoves);
2031
+ if (!appAdds || !appRemoves || !agentRemoves) {
2032
+ return html(renderInstallationReposError(400, "Invalid repo list."), {
2033
+ status: 400,
2034
+ });
2035
+ }
2036
+ // The open work seen now, to report what the removals blocked.
2121
2037
  let seen: OpenWorkView[] = [];
2122
2038
  const pre = await callApi(
2123
2039
  c,
2124
2040
  agentsApi,
2125
- `/agents/${apiId(c)}/github-lost-access/preview`,
2041
+ `/agents/${apiId(c)}/github-sync/preview`,
2126
2042
  );
2127
2043
  if (pre?.ok) {
2128
2044
  try {
2129
- seen = ((await pre.json()) as LostAccessPreviewView).openWork ?? [];
2045
+ seen = ((await pre.json()) as GithubSyncPreviewView).openWork ?? [];
2130
2046
  } catch {
2131
2047
  // no preview: nothing is claimed as blocked
2132
2048
  }
2133
2049
  }
2134
- const res = await postJson(
2050
+ const res = await callApi(
2135
2051
  c,
2136
- `/agents/${apiId(c)}/github-lost-access/remove`,
2137
- { repos, confirmRemovals: body.confirmRemovals === true },
2052
+ agentsApi,
2053
+ `/agents/${apiId(c)}/github-sync/apply`,
2054
+ {
2055
+ method: "POST",
2056
+ headers: { "content-type": "application/json" },
2057
+ body: JSON.stringify({
2058
+ appAdds,
2059
+ appRemoves,
2060
+ agentRemoves,
2061
+ ...(body.confirmRemovals === true ? { confirmRemovals: true } : {}),
2062
+ }),
2063
+ },
2064
+ );
2065
+ if (!res?.ok) return apiErrorFragment(res, "Could not apply the sync.");
2066
+ const result = (await res.json()) as GithubSyncApplyView;
2067
+ const warnings = result.warnings ?? [];
2068
+ return html(
2069
+ renderSyncResult(
2070
+ result,
2071
+ result.status === "applied"
2072
+ ? actuallyBlockedWork(
2073
+ seen,
2074
+ [...(result.appRemoves ?? []), ...(result.agentRemoves ?? [])],
2075
+ warnings,
2076
+ )
2077
+ : [],
2078
+ ),
2138
2079
  );
2139
- if (!res?.ok) {
2140
- return apiErrorFragment(res, "Could not remove the repos.");
2141
- }
2142
- const result = (await res.json()) as LostAccessRemoveView;
2143
- const blocked =
2144
- result.status === "applied"
2145
- ? actuallyBlockedWork(seen, result.repos ?? [], result.warnings ?? [])
2146
- : [];
2147
- return html(renderLostAccessResult(result, blocked));
2148
2080
  },
2149
2081
  );
2150
2082