@app-vitals/agent-admin 1.76.0 → 1.78.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/package.json +2 -2
- package/src/fixtures/github-agent-installation-cassette.json +29 -0
- package/src/fixtures/github-lookup-cassette.json +65 -0
- package/src/github-agent-installation-crawler.integration.test.ts +262 -0
- package/src/github-agent-installation-crawler.ts +179 -0
- package/src/github-agent-installation-crawler.unit.test.ts +343 -0
- package/src/github-enterprise-client-page.unit.test.ts +100 -0
- package/src/github-enterprise-client.ts +46 -0
- package/src/github-installation-wiring.ts +6 -2
- package/src/github-lookup-client.integration.test.ts +90 -1
- package/src/github-lookup-client.ts +156 -6
- package/src/github-lookup-client.unit.test.ts +147 -2
- package/src/github-repo-cache.ts +8 -0
- package/src/github-repo-crawler.integration.test.ts +244 -0
- package/src/github-repo-crawler.ts +273 -0
- package/src/github-repo-crawler.unit.test.ts +452 -0
- package/src/installation-ports.ts +14 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-repo-crawler.ts
|
|
3
|
+
* Resumable, rate-limit-aware crawl of an org's GitHub repositories into the
|
|
4
|
+
* repo cache (GLC-1.2). The engine is generic over a PageSource port; the
|
|
5
|
+
* lookup-App source (lookupRepoPageSource) adapts GithubLookupClient.
|
|
6
|
+
*
|
|
7
|
+
* One call to crawlRepos() is one run:
|
|
8
|
+
* - Fresh start (no crawl in progress, or the last one completed or was
|
|
9
|
+
* abandoned): crawlStartedAt = now, completedAt = null, from page 1.
|
|
10
|
+
* - Resume: keep crawlStartedAt, start at max(1, crawlPage - 1). The overlap
|
|
11
|
+
* page catches rows that shifted back a page between runs; GitHub's order
|
|
12
|
+
* is not documented, so nothing here relies on it.
|
|
13
|
+
* - Each page is upserted (seenAt = now) and crawlPage saved as it lands.
|
|
14
|
+
* - On the last page, the distinct rows seen this crawl (seenAt >=
|
|
15
|
+
* crawlStartedAt) are compared with total_count. Equal: completedAt is set
|
|
16
|
+
* and the generation sweep drops rows from earlier crawls. Unequal: the
|
|
17
|
+
* crawl is not trusted, completedAt stays unset, and the next run starts
|
|
18
|
+
* over from page 1. A partial crawl never sweeps.
|
|
19
|
+
* - A rate limit records rateLimitResetAt and ends the run cleanly (no
|
|
20
|
+
* attempts/lastError); runs before that time do not fetch. Other source
|
|
21
|
+
* errors record lastError, bump attempts and set a backoff nextAttemptAt.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type { Clock } from "./clock.ts";
|
|
25
|
+
import { GithubEnterpriseError } from "./github-enterprise-client.ts";
|
|
26
|
+
import {
|
|
27
|
+
type GithubLookupClient,
|
|
28
|
+
GithubLookupRateLimitError,
|
|
29
|
+
rateLimitResetAt,
|
|
30
|
+
} from "./github-lookup-client.ts";
|
|
31
|
+
import type {
|
|
32
|
+
CachedRepoInput,
|
|
33
|
+
SnapshotKey,
|
|
34
|
+
SnapshotProgress,
|
|
35
|
+
} from "./github-repo-cache.ts";
|
|
36
|
+
|
|
37
|
+
/** One page of the listing being crawled. */
|
|
38
|
+
export interface RepoPage {
|
|
39
|
+
repos: CachedRepoInput[];
|
|
40
|
+
/** The source's total row count for the whole listing. */
|
|
41
|
+
totalCount: number;
|
|
42
|
+
/** Whether a further page exists. An empty page is always the last. */
|
|
43
|
+
hasNext: boolean;
|
|
44
|
+
/** Quota state reported with this page; remaining 0 ends the run. */
|
|
45
|
+
rateLimit?: { remaining: number | null; resetAt: Date | null };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Port: fetch page `page` (1-based). A rate limit is signalled by throwing a
|
|
50
|
+
* GithubEnterpriseError of kind "rate_limit" (GithubLookupRateLimitError
|
|
51
|
+
* carries the exact resetAt). Implementations must time out their fetches.
|
|
52
|
+
*/
|
|
53
|
+
export interface PageSource {
|
|
54
|
+
fetchPage(page: number): Promise<RepoPage>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The snapshot fields the engine reads. */
|
|
58
|
+
export interface CrawlSnapshot {
|
|
59
|
+
id: string;
|
|
60
|
+
crawlPage: number;
|
|
61
|
+
crawlStartedAt: Date | null;
|
|
62
|
+
completedAt: Date | null;
|
|
63
|
+
totalCount: number | null;
|
|
64
|
+
lastError: string | null;
|
|
65
|
+
attempts: number;
|
|
66
|
+
nextAttemptAt: Date | null;
|
|
67
|
+
rateLimitResetAt: Date | null;
|
|
68
|
+
lastTickAt: Date | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Port: the storage the engine needs. GitHubRepoCacheService satisfies it. */
|
|
72
|
+
export interface CrawlStore {
|
|
73
|
+
getSnapshot(key: SnapshotKey): Promise<CrawlSnapshot | null>;
|
|
74
|
+
upsertSnapshot(
|
|
75
|
+
key: SnapshotKey,
|
|
76
|
+
progress?: SnapshotProgress,
|
|
77
|
+
): Promise<CrawlSnapshot>;
|
|
78
|
+
upsertRepos(
|
|
79
|
+
snapshotId: string,
|
|
80
|
+
repos: CachedRepoInput[],
|
|
81
|
+
seenAt?: Date,
|
|
82
|
+
): Promise<number>;
|
|
83
|
+
countReposSeenSince(snapshotId: string, since: Date): Promise<number>;
|
|
84
|
+
sweepGeneration(snapshotId: string): Promise<number>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export type CrawlOutcome =
|
|
88
|
+
/** Last page reached and seen rows matched total_count; swept. */
|
|
89
|
+
| "complete"
|
|
90
|
+
/** Last page reached but the counts disagreed; next run restarts. */
|
|
91
|
+
| "incomplete"
|
|
92
|
+
/** Page budget used up; next run resumes. */
|
|
93
|
+
| "in_progress"
|
|
94
|
+
/** Stopped on a rate limit; next run resumes after rateLimitResetAt. */
|
|
95
|
+
| "rate_limited"
|
|
96
|
+
/** A source error; recorded on the snapshot. */
|
|
97
|
+
| "failed";
|
|
98
|
+
|
|
99
|
+
export interface CrawlResult {
|
|
100
|
+
outcome: CrawlOutcome;
|
|
101
|
+
pagesFetched: number;
|
|
102
|
+
/** Rows deleted by the generation sweep (complete only). */
|
|
103
|
+
swept?: number;
|
|
104
|
+
rateLimitResetAt?: Date;
|
|
105
|
+
error?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface CrawlOptions {
|
|
109
|
+
key: SnapshotKey;
|
|
110
|
+
source: PageSource;
|
|
111
|
+
store: CrawlStore;
|
|
112
|
+
clock: Clock;
|
|
113
|
+
/** Most pages to fetch in this run. Default 1000. */
|
|
114
|
+
maxPages?: number;
|
|
115
|
+
/** Delay before retrying after the n-th consecutive failure. */
|
|
116
|
+
backoffMs?: (attempts: number) => number;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const DEFAULT_MAX_PAGES = 1000;
|
|
120
|
+
const MAX_ERROR_LENGTH = 500;
|
|
121
|
+
|
|
122
|
+
/** 1m, 2m, 4m, ... capped at 1h. */
|
|
123
|
+
export function defaultCrawlBackoffMs(attempts: number): number {
|
|
124
|
+
return Math.min(60_000 * 2 ** Math.max(0, attempts - 1), 3_600_000);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function crawlRepos(opts: CrawlOptions): Promise<CrawlResult> {
|
|
128
|
+
const { key, source, store, clock } = opts;
|
|
129
|
+
const maxPages = opts.maxPages ?? DEFAULT_MAX_PAGES;
|
|
130
|
+
const backoffMs = opts.backoffMs ?? defaultCrawlBackoffMs;
|
|
131
|
+
|
|
132
|
+
let snap =
|
|
133
|
+
(await store.getSnapshot(key)) ?? (await store.upsertSnapshot(key));
|
|
134
|
+
|
|
135
|
+
const resetAt = snap.rateLimitResetAt;
|
|
136
|
+
if (resetAt && resetAt.getTime() > clock.now().getTime()) {
|
|
137
|
+
return {
|
|
138
|
+
outcome: "rate_limited",
|
|
139
|
+
pagesFetched: 0,
|
|
140
|
+
rateLimitResetAt: resetAt,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const fresh =
|
|
145
|
+
snap.crawlStartedAt === null ||
|
|
146
|
+
snap.completedAt !== null ||
|
|
147
|
+
snap.crawlPage < 1;
|
|
148
|
+
if (fresh) {
|
|
149
|
+
snap = await store.upsertSnapshot(key, {
|
|
150
|
+
crawlStartedAt: clock.now(),
|
|
151
|
+
completedAt: null,
|
|
152
|
+
crawlPage: 0,
|
|
153
|
+
totalCount: null,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
const crawlStartedAt = snap.crawlStartedAt as Date;
|
|
157
|
+
const startPage = fresh ? 1 : Math.max(1, snap.crawlPage - 1);
|
|
158
|
+
|
|
159
|
+
let pagesFetched = 0;
|
|
160
|
+
for (let page = startPage; pagesFetched < maxPages; page++) {
|
|
161
|
+
let result: RepoPage;
|
|
162
|
+
try {
|
|
163
|
+
result = await source.fetchPage(page);
|
|
164
|
+
} catch (err) {
|
|
165
|
+
return recordError(err);
|
|
166
|
+
}
|
|
167
|
+
pagesFetched++;
|
|
168
|
+
|
|
169
|
+
await store.upsertRepos(snap.id, result.repos, clock.now());
|
|
170
|
+
await store.upsertSnapshot(key, {
|
|
171
|
+
crawlPage: page,
|
|
172
|
+
totalCount: result.totalCount,
|
|
173
|
+
rateLimitResetAt: null,
|
|
174
|
+
lastTickAt: clock.now(),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const last = !result.hasNext || result.repos.length === 0;
|
|
178
|
+
if (last) return finish(result.totalCount);
|
|
179
|
+
|
|
180
|
+
const rl = result.rateLimit;
|
|
181
|
+
if (rl && rl.remaining === 0) {
|
|
182
|
+
const at = rateLimitResetAt(
|
|
183
|
+
{ remaining: 0, resetAt: rl.resetAt, retryAfterSeconds: null },
|
|
184
|
+
clock.now(),
|
|
185
|
+
);
|
|
186
|
+
await store.upsertSnapshot(key, { rateLimitResetAt: at });
|
|
187
|
+
return { outcome: "rate_limited", pagesFetched, rateLimitResetAt: at };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { outcome: "in_progress", pagesFetched };
|
|
191
|
+
|
|
192
|
+
async function finish(totalCount: number): Promise<CrawlResult> {
|
|
193
|
+
const seen = await store.countReposSeenSince(snap.id, crawlStartedAt);
|
|
194
|
+
if (seen !== totalCount) {
|
|
195
|
+
const error = `total_count mismatch: saw ${seen} repos this crawl, GitHub reported ${totalCount}`;
|
|
196
|
+
await store.upsertSnapshot(key, {
|
|
197
|
+
crawlPage: 0,
|
|
198
|
+
completedAt: null,
|
|
199
|
+
lastError: error,
|
|
200
|
+
lastTickAt: clock.now(),
|
|
201
|
+
});
|
|
202
|
+
return { outcome: "incomplete", pagesFetched, error };
|
|
203
|
+
}
|
|
204
|
+
await store.upsertSnapshot(key, {
|
|
205
|
+
completedAt: clock.now(),
|
|
206
|
+
lastError: null,
|
|
207
|
+
attempts: 0,
|
|
208
|
+
nextAttemptAt: null,
|
|
209
|
+
lastTickAt: clock.now(),
|
|
210
|
+
});
|
|
211
|
+
const swept = await store.sweepGeneration(snap.id);
|
|
212
|
+
return { outcome: "complete", pagesFetched, swept };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function recordError(err: unknown): Promise<CrawlResult> {
|
|
216
|
+
const now = clock.now();
|
|
217
|
+
if (err instanceof GithubEnterpriseError && err.kind === "rate_limit") {
|
|
218
|
+
const at =
|
|
219
|
+
err instanceof GithubLookupRateLimitError
|
|
220
|
+
? err.resetAt
|
|
221
|
+
: rateLimitResetAt(
|
|
222
|
+
{
|
|
223
|
+
remaining: null,
|
|
224
|
+
resetAt: null,
|
|
225
|
+
retryAfterSeconds: err.retryAfterSeconds ?? null,
|
|
226
|
+
},
|
|
227
|
+
now,
|
|
228
|
+
);
|
|
229
|
+
await store.upsertSnapshot(key, {
|
|
230
|
+
rateLimitResetAt: at,
|
|
231
|
+
lastTickAt: now,
|
|
232
|
+
});
|
|
233
|
+
return { outcome: "rate_limited", pagesFetched, rateLimitResetAt: at };
|
|
234
|
+
}
|
|
235
|
+
const error = (err instanceof Error ? err.message : String(err)).slice(
|
|
236
|
+
0,
|
|
237
|
+
MAX_ERROR_LENGTH,
|
|
238
|
+
);
|
|
239
|
+
const attempts = snap.attempts + 1;
|
|
240
|
+
await store.upsertSnapshot(key, {
|
|
241
|
+
lastError: error,
|
|
242
|
+
attempts,
|
|
243
|
+
nextAttemptAt: new Date(now.getTime() + backoffMs(attempts)),
|
|
244
|
+
lastTickAt: now,
|
|
245
|
+
});
|
|
246
|
+
return { outcome: "failed", pagesFetched, error };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** PageSource over the lookup App's GET /installation/repositories for `org`. */
|
|
251
|
+
export function lookupRepoPageSource(
|
|
252
|
+
client: Pick<GithubLookupClient, "listInstallationRepositoriesPage">,
|
|
253
|
+
org: string,
|
|
254
|
+
): PageSource {
|
|
255
|
+
return {
|
|
256
|
+
async fetchPage(page: number): Promise<RepoPage> {
|
|
257
|
+
const res = await client.listInstallationRepositoriesPage(org, page);
|
|
258
|
+
return {
|
|
259
|
+
repos: res.repositories.map((r) => ({
|
|
260
|
+
githubId: r.id,
|
|
261
|
+
fullName: r.fullName,
|
|
262
|
+
archived: r.archived,
|
|
263
|
+
})),
|
|
264
|
+
totalCount: res.totalCount,
|
|
265
|
+
hasNext: res.hasNext,
|
|
266
|
+
rateLimit: {
|
|
267
|
+
remaining: res.rateLimit.remaining,
|
|
268
|
+
resetAt: res.rateLimit.resetAt,
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
}
|