@app-vitals/agent-admin 1.129.1 → 1.130.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.
@@ -24,6 +24,11 @@
24
24
  * switching it in-app fails on large orgs (GitHub HTTP 400, 100 implicit
25
25
  * removals max). Sync skips it with ALL_REPOS_INSTALLATION_REASON.
26
26
  *
27
+ * An agent can have several Apps. Each org is routed to the App already
28
+ * installed on it (the lowest-priority one when more than one is); adds and
29
+ * removals on that org go through that App's installation. An org with no
30
+ * installation of any of the agent's Apps is reported, never auto-routed.
31
+ *
27
32
  * Order: installation adds, installation removals,
28
33
  * agent-repo removals, one audit entry, then the agent's open work on the
29
34
  * removed repos is blocked through the optional OpenWorkPort (a port failure is
@@ -46,6 +51,9 @@ import {
46
51
  type CachedOrgInput,
47
52
  } from "./github-sync-cached-access.ts";
48
53
  import {
54
+ type AgentAppRef,
55
+ type AppIdentity,
56
+ agentApps,
49
57
  type InstallationAgentRecord,
50
58
  type InstallationAgentsPort,
51
59
  type InstallationAuditPort,
@@ -416,6 +424,8 @@ export type GithubSyncApplyResult =
416
424
  interface PlannedSync {
417
425
  plan: GithubSyncPlan;
418
426
  installationRepos: Record<string, string[]>;
427
+ /** The App routed to each installed org (key: lower-case org login). */
428
+ orgApps: Map<string, AgentAppRef>;
419
429
  selections?: Record<string, "all" | "selected" | null>;
420
430
  }
421
431
 
@@ -521,18 +531,26 @@ export class GithubSyncService {
521
531
  return { status: "removal_cap", plan, removals };
522
532
  }
523
533
 
524
- const appId = agent.appId as number;
525
- const identity = identityOf(agent);
534
+ const route = (org: string): { appId: number; identity: AppIdentity } => {
535
+ const app = planned.orgApps.get(lower(org));
536
+ if (!app) throw new Error(`no agent App is installed on org ${org}`);
537
+ return {
538
+ appId: app.appId,
539
+ identity: identityOf({ ...app, accountId: agent.accountId }),
540
+ };
541
+ };
526
542
  const added: string[] = [];
527
543
  const removed: string[] = [];
528
544
  let configDone = false;
529
545
  let error: string | undefined;
530
546
  try {
531
547
  for (const [org, repos] of groupByOrg(plan.appAdds)) {
548
+ const { appId, identity } = route(org);
532
549
  await this.deps.installation.addRepos(appId, org, repos, identity);
533
550
  added.push(...repos);
534
551
  }
535
552
  for (const [org, repos] of groupByOrg(plan.appRemoves)) {
553
+ const { appId, identity } = route(org);
536
554
  await this.deps.installation.removeRepos(appId, org, repos, identity);
537
555
  removed.push(...repos);
538
556
  }
@@ -647,6 +665,7 @@ export class GithubSyncService {
647
665
  agent: InstallationAgentRecord,
648
666
  username: string,
649
667
  installed: { org: string; repos: string[] }[],
668
+ orgApps: ReadonlyMap<string, AgentAppRef>,
650
669
  ): Promise<{
651
670
  access: UserRepoAccessResult;
652
671
  archivedRepos: string[];
@@ -656,16 +675,24 @@ export class GithubSyncService {
656
675
  const accountId = agent.accountId || DEPLOYMENT_WIDE_ACCOUNT_KEY;
657
676
  const now = (this.deps.now ?? (() => new Date()))();
658
677
  const maxAgeMs = this.deps.maxAgeMs ?? DEFAULT_SYNC_MAX_AGE_MS;
659
- let agentSnaps: Awaited<
678
+ // Each org's snapshot is the one crawled for the App routed to it.
679
+ const agentSnaps: Awaited<
660
680
  ReturnType<InstallationCachePort["listAgentSnapshots"]>
661
681
  > = [];
662
682
  let lookup: CachedLookupOrg[] = [];
663
683
  let readFailed = false;
664
684
  try {
665
- agentSnaps = await cache.listAgentSnapshots(
666
- accountId,
667
- String(agent.appId),
668
- );
685
+ for (const app of agentApps(agent)) {
686
+ const snaps = await cache.listAgentSnapshots(
687
+ accountId,
688
+ String(app.appId),
689
+ );
690
+ agentSnaps.push(
691
+ ...snaps.filter(
692
+ (s) => orgApps.get(s.org.toLowerCase())?.appId === app.appId,
693
+ ),
694
+ );
695
+ }
669
696
  if (this.deps.lookupRepos)
670
697
  lookup = await this.deps.lookupRepos(agent.accountId);
671
698
  } catch {
@@ -768,6 +795,7 @@ export class GithubSyncService {
768
795
  const none = (plan: GithubSyncPlan): PlannedSync => ({
769
796
  plan,
770
797
  installationRepos: {},
798
+ orgApps: new Map(),
771
799
  });
772
800
  const username = agent.githubUsername?.trim();
773
801
  if (!username) {
@@ -793,21 +821,37 @@ export class GithubSyncService {
793
821
  if (skip) return none(skippedPlan(skip));
794
822
  liveAccess = preview.access;
795
823
  }
796
- if (agent.appId === undefined) {
824
+ const apps = agentApps(agent);
825
+ if (apps.length === 0) {
797
826
  return none(skippedPlan(MISSING_APP_ID_REASON));
798
827
  }
799
- const installed = await this.deps.installation.listInstalled(
800
- agent.appId,
801
- identityOf(agent),
802
- );
803
- if (installed.status === "none") {
828
+ // Route each org to the first (lowest-priority) App installed on it.
829
+ const orgApps = new Map<string, AgentAppRef>();
830
+ const installedOrgs = new Map<string, { org: string; repos: string[] }>();
831
+ let anyFound = false;
832
+ for (const app of apps) {
833
+ const found = await this.deps.installation.listInstalled(
834
+ app.appId,
835
+ identityOf({ ...app, accountId: agent.accountId }),
836
+ );
837
+ if (found.status === "none") continue;
838
+ anyFound = true;
839
+ for (const o of found.orgs) {
840
+ const k = lower(o.org);
841
+ if (orgApps.has(k)) continue;
842
+ orgApps.set(k, app);
843
+ installedOrgs.set(k, { org: o.org, repos: o.repos });
844
+ }
845
+ }
846
+ if (!anyFound) {
804
847
  return none(skippedPlan(NO_INSTALLATION_REASON));
805
848
  }
849
+ const installed = { orgs: [...installedOrgs.values()] };
806
850
  // The live resolver reports no archived flag, so only the cached path
807
851
  // leaves archived repos alone.
808
852
  const cached = liveAccess
809
853
  ? undefined
810
- : await this.cachedAccess(agent, username, installed.orgs);
854
+ : await this.cachedAccess(agent, username, installed.orgs, orgApps);
811
855
  // The live path never reads the crawl cache, so every selection is unknown.
812
856
  const selections = cached?.selections;
813
857
  const installationRepos = Object.fromEntries(
@@ -835,6 +879,7 @@ export class GithubSyncService {
835
879
  return {
836
880
  plan,
837
881
  installationRepos,
882
+ orgApps,
838
883
  ...(selections ? { selections } : {}),
839
884
  };
840
885
  }