@app-vitals/agent-admin 1.80.0 → 1.82.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/openapi.platform.json +48 -2
- package/package.json +2 -2
- package/src/admin-boot.boot-order.unit.test.ts +11 -0
- package/src/admin-boot.ts +42 -2
- package/src/admin-ui-installation-repos-pages.ts +89 -25
- package/src/admin-ui-installation-repos.smoke.test.ts +71 -0
- package/src/admin-ui-pages.ts +13 -1
- package/src/admin-ui.ts +16 -4
- package/src/agents-api.smoke.test.ts +85 -5
- package/src/agents-api.ts +32 -3
- package/src/github-access-backfill.ts +9 -1
- package/src/github-access-backfill.unit.test.ts +16 -0
- package/src/github-agent-installation-crawler.ts +4 -1
- package/src/github-agent-installation-crawler.unit.test.ts +14 -0
- package/src/github-enterprise-apps.integration.test.ts +11 -0
- package/src/github-enterprise-apps.ts +10 -0
- package/src/github-installation-wiring.ts +86 -4
- package/src/github-installation-wiring.unit.test.ts +8 -4
- package/src/github-installation.fixtures.ts +12 -1
- package/src/github-repo-cache.integration.test.ts +14 -0
- package/src/github-repo-cache.ts +30 -0
- package/src/github-repo-crawler.ts +6 -0
- package/src/github-repo-crawler.unit.test.ts +36 -0
- package/src/github-repo-refresh-discovery.ts +241 -0
- package/src/github-repo-refresh-discovery.unit.test.ts +237 -0
- package/src/github-repo-refresher.integration.test.ts +179 -0
- package/src/github-repo-refresher.ts +640 -0
- package/src/github-repo-refresher.unit.test.ts +735 -0
- package/src/installation-repos-service.ts +174 -50
- package/src/installation-repos-service.unit.test.ts +157 -52
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-repo-refresher.unit.test.ts
|
|
3
|
+
* The GLC-1.4 refresh tick against an in-memory store and a fake clock:
|
|
4
|
+
* per-org priority (missing before expired, staleness cap), the per-org
|
|
5
|
+
* lease, the in-process overlap guard, the time budget, rate-limit skips and
|
|
6
|
+
* failure isolation.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { beforeEach, describe, expect, it, spyOn } from "bun:test";
|
|
10
|
+
import type {
|
|
11
|
+
AccessProbe,
|
|
12
|
+
AccessRow,
|
|
13
|
+
BackfillUser,
|
|
14
|
+
} from "./github-access-backfill.ts";
|
|
15
|
+
import type {
|
|
16
|
+
AccessInput,
|
|
17
|
+
SnapshotKey,
|
|
18
|
+
SnapshotProgress,
|
|
19
|
+
} from "./github-repo-cache.ts";
|
|
20
|
+
import {
|
|
21
|
+
DEFAULT_REFRESH_INTERVAL_MS,
|
|
22
|
+
DEFAULT_REFRESH_STALENESS_CAP_MS,
|
|
23
|
+
DEFAULT_REFRESH_TIME_BUDGET_MS,
|
|
24
|
+
DEFAULT_REFRESH_TTL_MS,
|
|
25
|
+
GithubRepoRefresher,
|
|
26
|
+
planOrgWork,
|
|
27
|
+
type RefreshSnapshot,
|
|
28
|
+
type RefreshStore,
|
|
29
|
+
type RefreshTarget,
|
|
30
|
+
resolveRefreshConfig,
|
|
31
|
+
type StepOutcome,
|
|
32
|
+
} from "./github-repo-refresher.ts";
|
|
33
|
+
|
|
34
|
+
const T0 = new Date("2026-11-20T12:00:00Z");
|
|
35
|
+
const MIN = 60_000;
|
|
36
|
+
const HOUR = 60 * MIN;
|
|
37
|
+
const TTL = HOUR;
|
|
38
|
+
const CAP = 6 * HOUR;
|
|
39
|
+
const BUDGET = 30_000;
|
|
40
|
+
|
|
41
|
+
class ManualClock {
|
|
42
|
+
t = T0.getTime();
|
|
43
|
+
now(): Date {
|
|
44
|
+
return new Date(this.t);
|
|
45
|
+
}
|
|
46
|
+
advance(ms: number): void {
|
|
47
|
+
this.t += ms;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const keyStr = (k: SnapshotKey) =>
|
|
52
|
+
`${k.accountId}|${k.appKind}|${k.appRef}|${k.org}`;
|
|
53
|
+
|
|
54
|
+
type Snap = RefreshSnapshot & {
|
|
55
|
+
leaseHolder: string | null;
|
|
56
|
+
leasedUntil: Date | null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
class MemoryStore implements RefreshStore {
|
|
60
|
+
snaps = new Map<string, Snap>();
|
|
61
|
+
repos = new Map<string, string[]>();
|
|
62
|
+
access = new Map<string, AccessRow & { username: string }>();
|
|
63
|
+
released: string[] = [];
|
|
64
|
+
private seq = 0;
|
|
65
|
+
|
|
66
|
+
constructor(private readonly clock: ManualClock) {}
|
|
67
|
+
|
|
68
|
+
async getSnapshot(key: SnapshotKey) {
|
|
69
|
+
const s = this.snaps.get(keyStr(key));
|
|
70
|
+
return s ? { ...s } : null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async upsertSnapshot(key: SnapshotKey, progress: SnapshotProgress = {}) {
|
|
74
|
+
const k = keyStr(key);
|
|
75
|
+
const base: Snap = this.snaps.get(k) ?? {
|
|
76
|
+
id: `snap${++this.seq}`,
|
|
77
|
+
completedAt: null,
|
|
78
|
+
crawlStartedAt: null,
|
|
79
|
+
nextAttemptAt: null,
|
|
80
|
+
rateLimitResetAt: null,
|
|
81
|
+
lastTickAt: null,
|
|
82
|
+
lastError: null,
|
|
83
|
+
leaseHolder: null,
|
|
84
|
+
leasedUntil: null,
|
|
85
|
+
};
|
|
86
|
+
const next = { ...base } as Record<string, unknown>;
|
|
87
|
+
for (const [f, v] of Object.entries(progress)) {
|
|
88
|
+
if (v !== undefined) next[f] = v;
|
|
89
|
+
}
|
|
90
|
+
this.snaps.set(k, next as unknown as Snap);
|
|
91
|
+
return { ...(next as unknown as Snap) };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
byId(id: string): Snap | undefined {
|
|
95
|
+
return [...this.snaps.values()].find((s) => s.id === id);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async acquireLease(id: string, holder: string, ttlMs: number) {
|
|
99
|
+
const s = this.byId(id);
|
|
100
|
+
if (!s) return false;
|
|
101
|
+
const now = this.clock.now();
|
|
102
|
+
if (s.leasedUntil && s.leasedUntil > now) return false;
|
|
103
|
+
s.leaseHolder = holder;
|
|
104
|
+
s.leasedUntil = new Date(now.getTime() + ttlMs);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async releaseLease(id: string, holder: string) {
|
|
109
|
+
const s = this.byId(id);
|
|
110
|
+
if (s && s.leaseHolder === holder) {
|
|
111
|
+
s.leaseHolder = null;
|
|
112
|
+
s.leasedUntil = null;
|
|
113
|
+
this.released.push(id);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async listRepoFullNames(id: string) {
|
|
118
|
+
return [...(this.repos.get(id) ?? [])].sort();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async getAccessRows(q: {
|
|
122
|
+
accountId: string;
|
|
123
|
+
org: string;
|
|
124
|
+
username: string;
|
|
125
|
+
repos: string[];
|
|
126
|
+
}) {
|
|
127
|
+
return q.repos.flatMap((r) => {
|
|
128
|
+
const row = this.access.get(`${q.username}/${r}`);
|
|
129
|
+
return row ? [row] : [];
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async upsertAccess(i: AccessInput) {
|
|
134
|
+
this.access.set(`${i.username}/${i.repo}`, {
|
|
135
|
+
username: i.username,
|
|
136
|
+
repo: i.repo,
|
|
137
|
+
status: i.status,
|
|
138
|
+
checkedAt: this.clock.now(),
|
|
139
|
+
attempts: i.attempts ?? 0,
|
|
140
|
+
nextAttemptAt: i.nextAttemptAt ?? null,
|
|
141
|
+
});
|
|
142
|
+
return i;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Test helper: an ok access row checked at `checkedAt`. */
|
|
146
|
+
setOk(username: string, repo: string, checkedAt: Date) {
|
|
147
|
+
this.access.set(`${username}/${repo}`, {
|
|
148
|
+
username,
|
|
149
|
+
repo,
|
|
150
|
+
status: "ok",
|
|
151
|
+
checkedAt,
|
|
152
|
+
attempts: 0,
|
|
153
|
+
nextAttemptAt: null,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const lookupKey = (org: string): SnapshotKey => ({
|
|
159
|
+
accountId: "acc",
|
|
160
|
+
appKind: "lookup",
|
|
161
|
+
appRef: "555",
|
|
162
|
+
org,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
let clock: ManualClock;
|
|
166
|
+
let store: MemoryStore;
|
|
167
|
+
let events: string[];
|
|
168
|
+
|
|
169
|
+
/** A fake crawl: on "complete" it writes `repos` and marks the snapshot done. */
|
|
170
|
+
function fakeCrawl(
|
|
171
|
+
key: SnapshotKey,
|
|
172
|
+
repos: string[],
|
|
173
|
+
opts: {
|
|
174
|
+
outcome?: StepOutcome["outcome"];
|
|
175
|
+
costMs?: number;
|
|
176
|
+
throws?: Error;
|
|
177
|
+
rateLimitResetAt?: Date;
|
|
178
|
+
} = {},
|
|
179
|
+
) {
|
|
180
|
+
return async (ctx: { shouldStop: () => boolean }): Promise<StepOutcome> => {
|
|
181
|
+
events.push(`crawl:${key.org}`);
|
|
182
|
+
if (opts.costMs) clock.advance(opts.costMs);
|
|
183
|
+
if (opts.throws) throw opts.throws;
|
|
184
|
+
const outcome = opts.outcome ?? "complete";
|
|
185
|
+
const snap = await store.upsertSnapshot(key, { lastTickAt: clock.now() });
|
|
186
|
+
if (outcome === "complete") {
|
|
187
|
+
store.repos.set(snap.id, repos);
|
|
188
|
+
await store.upsertSnapshot(key, {
|
|
189
|
+
crawlStartedAt: clock.now(),
|
|
190
|
+
completedAt: clock.now(),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
if (outcome === "rate_limited") {
|
|
194
|
+
await store.upsertSnapshot(key, {
|
|
195
|
+
rateLimitResetAt: opts.rateLimitResetAt ?? null,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
void ctx;
|
|
199
|
+
return opts.rateLimitResetAt
|
|
200
|
+
? { outcome, rateLimitResetAt: opts.rateLimitResetAt }
|
|
201
|
+
: { outcome };
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function probe(costMs = 0): AccessProbe {
|
|
206
|
+
return {
|
|
207
|
+
async getCollaboratorPermissionWithRateLimit(org, repo, user) {
|
|
208
|
+
events.push(`probe:${org}/${repo}@${user}`);
|
|
209
|
+
if (costMs) clock.advance(costMs);
|
|
210
|
+
return {
|
|
211
|
+
role: "write",
|
|
212
|
+
rateLimit: { remaining: 4000, resetAt: null, retryAfterSeconds: null },
|
|
213
|
+
};
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function target(
|
|
219
|
+
org: string,
|
|
220
|
+
over: Partial<RefreshTarget> & {
|
|
221
|
+
repos?: string[];
|
|
222
|
+
users?: BackfillUser[];
|
|
223
|
+
crawlOpts?: Parameters<typeof fakeCrawl>[2];
|
|
224
|
+
} = {},
|
|
225
|
+
): RefreshTarget {
|
|
226
|
+
const key = over.key ?? lookupKey(org);
|
|
227
|
+
const repos = over.repos ?? [`${org}/a`, `${org}/b`];
|
|
228
|
+
return {
|
|
229
|
+
key,
|
|
230
|
+
crawl: over.crawl ?? fakeCrawl(key, repos, over.crawlOpts),
|
|
231
|
+
...(over.users
|
|
232
|
+
? { access: { probe: probe(), users: over.users } }
|
|
233
|
+
: over.access
|
|
234
|
+
? { access: over.access }
|
|
235
|
+
: {}),
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Seed a completed crawl at `completedAt` with `repos`. */
|
|
240
|
+
async function seedCrawl(key: SnapshotKey, repos: string[], completedAt: Date) {
|
|
241
|
+
const snap = await store.upsertSnapshot(key, {
|
|
242
|
+
crawlStartedAt: completedAt,
|
|
243
|
+
completedAt,
|
|
244
|
+
lastTickAt: completedAt,
|
|
245
|
+
});
|
|
246
|
+
store.repos.set(snap.id, repos);
|
|
247
|
+
return snap;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function refresher(
|
|
251
|
+
targets: RefreshTarget[] | (() => Promise<RefreshTarget[]>),
|
|
252
|
+
over: Partial<ConstructorParameters<typeof GithubRepoRefresher>[0]> = {},
|
|
253
|
+
) {
|
|
254
|
+
return new GithubRepoRefresher({
|
|
255
|
+
discovery: {
|
|
256
|
+
listTargets:
|
|
257
|
+
typeof targets === "function" ? targets : async () => targets,
|
|
258
|
+
},
|
|
259
|
+
store,
|
|
260
|
+
clock,
|
|
261
|
+
config: {
|
|
262
|
+
intervalMs: MIN,
|
|
263
|
+
ttlMs: TTL,
|
|
264
|
+
stalenessCapMs: CAP,
|
|
265
|
+
timeBudgetMs: BUDGET,
|
|
266
|
+
},
|
|
267
|
+
holder: "worker-1",
|
|
268
|
+
log: () => {},
|
|
269
|
+
...over,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
beforeEach(() => {
|
|
274
|
+
clock = new ManualClock();
|
|
275
|
+
store = new MemoryStore(clock);
|
|
276
|
+
events = [];
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// ─── resolveRefreshConfig ────────────────────────────────────────────────────
|
|
280
|
+
|
|
281
|
+
describe("resolveRefreshConfig", () => {
|
|
282
|
+
it("defaults when unset, blank, non-numeric or non-positive", () => {
|
|
283
|
+
const defaults = {
|
|
284
|
+
intervalMs: DEFAULT_REFRESH_INTERVAL_MS,
|
|
285
|
+
ttlMs: DEFAULT_REFRESH_TTL_MS,
|
|
286
|
+
stalenessCapMs: DEFAULT_REFRESH_STALENESS_CAP_MS,
|
|
287
|
+
timeBudgetMs: DEFAULT_REFRESH_TIME_BUDGET_MS,
|
|
288
|
+
};
|
|
289
|
+
expect(resolveRefreshConfig({})).toEqual(defaults);
|
|
290
|
+
expect(
|
|
291
|
+
resolveRefreshConfig({
|
|
292
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_INTERVAL_MS: " ",
|
|
293
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_TTL_MS: "abc",
|
|
294
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_STALENESS_CAP_MS: "0",
|
|
295
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_TIME_BUDGET_MS: "-5",
|
|
296
|
+
}),
|
|
297
|
+
).toEqual(defaults);
|
|
298
|
+
expect(DEFAULT_REFRESH_INTERVAL_MS).toBeLessThan(DEFAULT_REFRESH_TTL_MS);
|
|
299
|
+
expect(DEFAULT_REFRESH_TIME_BUDGET_MS).toBeLessThan(
|
|
300
|
+
DEFAULT_REFRESH_INTERVAL_MS,
|
|
301
|
+
);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("reads each value and never lets the staleness cap undercut the TTL", () => {
|
|
305
|
+
expect(
|
|
306
|
+
resolveRefreshConfig({
|
|
307
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_INTERVAL_MS: "30000",
|
|
308
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_TTL_MS: "7200000",
|
|
309
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_STALENESS_CAP_MS: "60000",
|
|
310
|
+
SHIPWRIGHT_ADMIN_GITHUB_REFRESH_TIME_BUDGET_MS: "20000",
|
|
311
|
+
}),
|
|
312
|
+
).toEqual({
|
|
313
|
+
intervalMs: 30_000,
|
|
314
|
+
ttlMs: 7_200_000,
|
|
315
|
+
stalenessCapMs: 7_200_000,
|
|
316
|
+
timeBudgetMs: 20_000,
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// ─── planOrgWork ─────────────────────────────────────────────────────────────
|
|
322
|
+
|
|
323
|
+
describe("planOrgWork", () => {
|
|
324
|
+
const snap = (over: Partial<RefreshSnapshot> = {}): RefreshSnapshot => ({
|
|
325
|
+
id: "s",
|
|
326
|
+
completedAt: null,
|
|
327
|
+
crawlStartedAt: null,
|
|
328
|
+
nextAttemptAt: null,
|
|
329
|
+
rateLimitResetAt: null,
|
|
330
|
+
lastTickAt: null,
|
|
331
|
+
lastError: null,
|
|
332
|
+
...over,
|
|
333
|
+
});
|
|
334
|
+
const plan = (
|
|
335
|
+
s: RefreshSnapshot,
|
|
336
|
+
access?: { missing: number; expired: number; staleRepos: string[] },
|
|
337
|
+
) =>
|
|
338
|
+
planOrgWork({
|
|
339
|
+
snapshot: s,
|
|
340
|
+
access,
|
|
341
|
+
now: T0,
|
|
342
|
+
ttlMs: TTL,
|
|
343
|
+
stalenessCapMs: CAP,
|
|
344
|
+
});
|
|
345
|
+
const ago = (ms: number) => new Date(T0.getTime() - ms);
|
|
346
|
+
const none = { missing: 0, expired: 0, staleRepos: [] };
|
|
347
|
+
|
|
348
|
+
it("crawls a never-crawled org", () => {
|
|
349
|
+
expect(plan(snap())).toEqual([{ kind: "crawl", reason: "missing" }]);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("treats an incomplete list as missing and runs it before missing access", () => {
|
|
353
|
+
expect(
|
|
354
|
+
plan(snap({ crawlStartedAt: ago(MIN) }), { ...none, missing: 3 }),
|
|
355
|
+
).toEqual([
|
|
356
|
+
{ kind: "crawl", reason: "missing" },
|
|
357
|
+
{ kind: "access", reason: "missing" },
|
|
358
|
+
]);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("holds expired work while anything is missing", () => {
|
|
362
|
+
expect(
|
|
363
|
+
plan(snap({ completedAt: ago(2 * HOUR) }), {
|
|
364
|
+
missing: 2,
|
|
365
|
+
expired: 5,
|
|
366
|
+
staleRepos: [],
|
|
367
|
+
}),
|
|
368
|
+
).toEqual([{ kind: "access", reason: "missing" }]);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("runs expired work only when nothing is missing", () => {
|
|
372
|
+
expect(
|
|
373
|
+
plan(snap({ completedAt: ago(2 * HOUR) }), { ...none, expired: 1 }),
|
|
374
|
+
).toEqual([
|
|
375
|
+
{ kind: "crawl", reason: "expired" },
|
|
376
|
+
{ kind: "access", reason: "expired" },
|
|
377
|
+
]);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("does nothing when everything is fresh", () => {
|
|
381
|
+
expect(plan(snap({ completedAt: ago(MIN) }), none)).toEqual([]);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("forces a crawl past the staleness cap even while backfill is pending", () => {
|
|
385
|
+
expect(
|
|
386
|
+
plan(snap({ completedAt: ago(CAP + 1) }), { ...none, missing: 9 }),
|
|
387
|
+
).toEqual([
|
|
388
|
+
{ kind: "crawl", reason: "stale_cap" },
|
|
389
|
+
{ kind: "access", reason: "missing" },
|
|
390
|
+
]);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("forces access rows past the staleness cap ahead of missing access", () => {
|
|
394
|
+
expect(
|
|
395
|
+
plan(snap({ completedAt: ago(MIN) }), {
|
|
396
|
+
missing: 4,
|
|
397
|
+
expired: 2,
|
|
398
|
+
staleRepos: ["old"],
|
|
399
|
+
}),
|
|
400
|
+
).toEqual([
|
|
401
|
+
{ kind: "access", reason: "stale_cap", repos: ["old"] },
|
|
402
|
+
{ kind: "access", reason: "missing" },
|
|
403
|
+
]);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("labels capped work stale_cap even when nothing is missing", () => {
|
|
407
|
+
expect(
|
|
408
|
+
plan(snap({ completedAt: ago(CAP + 1) }), {
|
|
409
|
+
missing: 0,
|
|
410
|
+
expired: 3,
|
|
411
|
+
staleRepos: ["old"],
|
|
412
|
+
}),
|
|
413
|
+
).toEqual([
|
|
414
|
+
{ kind: "crawl", reason: "stale_cap" },
|
|
415
|
+
{ kind: "access", reason: "stale_cap", repos: ["old"] },
|
|
416
|
+
{ kind: "access", reason: "expired" },
|
|
417
|
+
]);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("skips a crawl still backing off, which then does not hold expired access", () => {
|
|
421
|
+
expect(
|
|
422
|
+
plan(
|
|
423
|
+
snap({
|
|
424
|
+
crawlStartedAt: ago(MIN),
|
|
425
|
+
nextAttemptAt: new Date(T0.getTime() + MIN),
|
|
426
|
+
}),
|
|
427
|
+
{ ...none, expired: 1 },
|
|
428
|
+
),
|
|
429
|
+
).toEqual([{ kind: "access", reason: "expired" }]);
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
// ─── tick ────────────────────────────────────────────────────────────────────
|
|
434
|
+
|
|
435
|
+
describe("GithubRepoRefresher.tick", () => {
|
|
436
|
+
it("runs missing work before expired: crawl, unanswered pairs, then expired", async () => {
|
|
437
|
+
const t = target("acme", {
|
|
438
|
+
repos: ["acme/a", "acme/b", "acme/c"],
|
|
439
|
+
users: [{ username: "octocat" }],
|
|
440
|
+
});
|
|
441
|
+
// An in-progress crawl (incomplete list) and an expired answer for "a".
|
|
442
|
+
await store.upsertSnapshot(t.key, { crawlStartedAt: clock.now() });
|
|
443
|
+
clock.advance(2 * HOUR);
|
|
444
|
+
store.setOk("octocat", "a", new Date(T0.getTime() - HOUR));
|
|
445
|
+
const res = await refresher([t]).tick();
|
|
446
|
+
// The expired row for "a" waits until no pair is missing.
|
|
447
|
+
expect(events).toEqual([
|
|
448
|
+
"crawl:acme",
|
|
449
|
+
"probe:acme/b@octocat",
|
|
450
|
+
"probe:acme/c@octocat",
|
|
451
|
+
"probe:acme/a@octocat",
|
|
452
|
+
]);
|
|
453
|
+
expect(res.targets[0]?.steps).toEqual([
|
|
454
|
+
"crawl:missing",
|
|
455
|
+
"access:missing",
|
|
456
|
+
"access:expired",
|
|
457
|
+
]);
|
|
458
|
+
|
|
459
|
+
// Later: nothing missing, so the expired list and rows are refreshed.
|
|
460
|
+
events = [];
|
|
461
|
+
clock.advance(2 * HOUR);
|
|
462
|
+
await refresher([t]).tick();
|
|
463
|
+
expect(events).toEqual([
|
|
464
|
+
"crawl:acme",
|
|
465
|
+
"probe:acme/a@octocat",
|
|
466
|
+
"probe:acme/b@octocat",
|
|
467
|
+
"probe:acme/c@octocat",
|
|
468
|
+
]);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it("the staleness cap forces a refresh while backfill is still pending", async () => {
|
|
472
|
+
const t = target("acme", {
|
|
473
|
+
repos: ["acme/a", "acme/b", "acme/new"],
|
|
474
|
+
access: {
|
|
475
|
+
// Each probe eats the whole budget: backfill never finishes in a tick.
|
|
476
|
+
probe: probe(BUDGET),
|
|
477
|
+
users: [{ username: "octocat" }],
|
|
478
|
+
},
|
|
479
|
+
});
|
|
480
|
+
await seedCrawl(t.key, ["acme/a", "acme/b"], T0);
|
|
481
|
+
const r = refresher([t]);
|
|
482
|
+
|
|
483
|
+
// Within the TTL... then expired, but pending backfill holds the crawl.
|
|
484
|
+
clock.advance(2 * HOUR);
|
|
485
|
+
await r.tick();
|
|
486
|
+
expect(events).toEqual(["probe:acme/a@octocat"]);
|
|
487
|
+
events = [];
|
|
488
|
+
clock.advance(MIN);
|
|
489
|
+
await r.tick();
|
|
490
|
+
expect(events).toEqual(["probe:acme/b@octocat"]);
|
|
491
|
+
|
|
492
|
+
// Past the cap the crawl runs first even though a pair is still missing.
|
|
493
|
+
store.access.delete("octocat/b");
|
|
494
|
+
events = [];
|
|
495
|
+
clock.advance(CAP);
|
|
496
|
+
await r.tick();
|
|
497
|
+
expect(events[0]).toBe("crawl:acme");
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
it("an org past the cap with nothing missing is not starved by another org's backfill", async () => {
|
|
501
|
+
// "busy" never finishes its crawl and eats the whole budget each tick.
|
|
502
|
+
const busy = target("busy", {
|
|
503
|
+
crawlOpts: { outcome: "in_progress", costMs: BUDGET },
|
|
504
|
+
});
|
|
505
|
+
const capped = target("capped");
|
|
506
|
+
await seedCrawl(capped.key, ["capped/a"], T0);
|
|
507
|
+
clock.advance(CAP + 1);
|
|
508
|
+
const r = refresher([busy, capped]);
|
|
509
|
+
|
|
510
|
+
// Tick 1: busy has never ticked, so it goes first and spends the budget.
|
|
511
|
+
const first = await r.tick();
|
|
512
|
+
expect(events).toEqual(["crawl:busy"]);
|
|
513
|
+
expect(first.targets.find((s) => s.key.org === "capped")?.state).toBe(
|
|
514
|
+
"deferred",
|
|
515
|
+
);
|
|
516
|
+
|
|
517
|
+
// Tick 2: the capped org ranks with missing work and has waited longest.
|
|
518
|
+
events = [];
|
|
519
|
+
clock.advance(MIN);
|
|
520
|
+
const second = await r.tick();
|
|
521
|
+
expect(events[0]).toBe("crawl:capped");
|
|
522
|
+
expect(
|
|
523
|
+
second.targets.find((s) => s.key.org === "capped")?.steps,
|
|
524
|
+
).toEqual(["crawl:stale_cap"]);
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it("skips an org whose lease another worker holds", async () => {
|
|
528
|
+
const t = target("acme");
|
|
529
|
+
const snap = await store.upsertSnapshot(t.key);
|
|
530
|
+
expect(await store.acquireLease(snap.id, "worker-2", 10 * MIN)).toBe(true);
|
|
531
|
+
const res = await refresher([t]).tick();
|
|
532
|
+
expect(events).toEqual([]);
|
|
533
|
+
expect(res.targets[0]?.state).toBe("leased_elsewhere");
|
|
534
|
+
|
|
535
|
+
clock.advance(11 * MIN);
|
|
536
|
+
const again = await refresher([t]).tick();
|
|
537
|
+
expect(events).toEqual(["crawl:acme"]);
|
|
538
|
+
expect(again.targets[0]?.state).toBe("worked");
|
|
539
|
+
expect(store.byId(snap.id)?.leaseHolder).toBeNull();
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
it("an overlapping tick in the same process is a no-op", async () => {
|
|
543
|
+
let release: () => void = () => {};
|
|
544
|
+
const gate = new Promise<void>((r) => {
|
|
545
|
+
release = r;
|
|
546
|
+
});
|
|
547
|
+
const t = target("acme", {
|
|
548
|
+
crawl: async () => {
|
|
549
|
+
events.push("crawl:acme");
|
|
550
|
+
await gate;
|
|
551
|
+
return { outcome: "in_progress" };
|
|
552
|
+
},
|
|
553
|
+
});
|
|
554
|
+
const r = refresher([t]);
|
|
555
|
+
const first = r.tick();
|
|
556
|
+
await Bun.sleep(0);
|
|
557
|
+
const second = await r.tick();
|
|
558
|
+
expect(second.skipped).toBe("overlap");
|
|
559
|
+
release();
|
|
560
|
+
await first;
|
|
561
|
+
expect(events).toEqual(["crawl:acme"]);
|
|
562
|
+
expect(r.status().running).toBe(false);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("skips an org inside its rate-limit window and resumes after the reset", async () => {
|
|
566
|
+
const resetAt = new Date(T0.getTime() + 10 * MIN);
|
|
567
|
+
const t = target("acme");
|
|
568
|
+
await store.upsertSnapshot(t.key, { rateLimitResetAt: resetAt });
|
|
569
|
+
const r = refresher([t]);
|
|
570
|
+
const res = await r.tick();
|
|
571
|
+
expect(events).toEqual([]);
|
|
572
|
+
expect(res.targets[0]).toMatchObject({
|
|
573
|
+
state: "rate_limited",
|
|
574
|
+
resumeAt: resetAt,
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
clock.advance(10 * MIN);
|
|
578
|
+
await r.tick();
|
|
579
|
+
expect(events).toEqual(["crawl:acme"]);
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
it("stops an org's remaining steps once a step hits a rate limit", async () => {
|
|
583
|
+
const resetAt = new Date(T0.getTime() + 5 * MIN);
|
|
584
|
+
const t = target("acme", {
|
|
585
|
+
users: [{ username: "octocat" }],
|
|
586
|
+
crawlOpts: { outcome: "rate_limited", rateLimitResetAt: resetAt },
|
|
587
|
+
});
|
|
588
|
+
await store.upsertSnapshot(t.key, { crawlStartedAt: T0 });
|
|
589
|
+
const res = await refresher([t]).tick();
|
|
590
|
+
expect(events).toEqual(["crawl:acme"]);
|
|
591
|
+
expect(res.targets[0]).toMatchObject({
|
|
592
|
+
state: "rate_limited",
|
|
593
|
+
resumeAt: resetAt,
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
it("one org failing never blocks the others, and its lease is released", async () => {
|
|
598
|
+
const bad = target("bad", { crawlOpts: { throws: new Error("boom") } });
|
|
599
|
+
const good = target("good");
|
|
600
|
+
const res = await refresher([bad, good]).tick();
|
|
601
|
+
expect(events).toEqual(["crawl:bad", "crawl:good"]);
|
|
602
|
+
const byOrg = Object.fromEntries(res.targets.map((s) => [s.key.org, s]));
|
|
603
|
+
expect(byOrg.bad).toMatchObject({ state: "error", error: "boom" });
|
|
604
|
+
expect(byOrg.good?.state).toBe("worked");
|
|
605
|
+
const badSnap = await store.getSnapshot(bad.key);
|
|
606
|
+
expect(store.byId(badSnap?.id ?? "")?.leaseHolder).toBeNull();
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
it("logs a target error with its key and message", async () => {
|
|
610
|
+
const errors = spyOn(console, "error").mockImplementation(() => {});
|
|
611
|
+
try {
|
|
612
|
+
const bad = target("bad", { crawlOpts: { throws: new Error("boom") } });
|
|
613
|
+
await refresher([bad]).tick();
|
|
614
|
+
expect(errors).toHaveBeenCalledWith(
|
|
615
|
+
"[github-repo-refresher] target acc|lookup|555|bad failed:",
|
|
616
|
+
"boom",
|
|
617
|
+
);
|
|
618
|
+
} finally {
|
|
619
|
+
errors.mockRestore();
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
it("stops starting work once the time budget is spent", async () => {
|
|
624
|
+
const a = target("a", { crawlOpts: { costMs: BUDGET } });
|
|
625
|
+
const b = target("b");
|
|
626
|
+
const seen: boolean[] = [];
|
|
627
|
+
const c: RefreshTarget = {
|
|
628
|
+
key: lookupKey("c"),
|
|
629
|
+
crawl: async (ctx) => {
|
|
630
|
+
seen.push(ctx.shouldStop());
|
|
631
|
+
return { outcome: "in_progress" };
|
|
632
|
+
},
|
|
633
|
+
};
|
|
634
|
+
const res = await refresher([a, b, c]).tick();
|
|
635
|
+
expect(events).toEqual(["crawl:a"]);
|
|
636
|
+
expect(seen).toEqual([]);
|
|
637
|
+
const states = res.targets.map((s) => `${s.key.org}:${s.state}`);
|
|
638
|
+
expect(states).toEqual(["a:worked", "b:deferred", "c:deferred"]);
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
it("hands each step a shouldStop that turns true at the deadline", async () => {
|
|
642
|
+
const seen: boolean[] = [];
|
|
643
|
+
const t: RefreshTarget = {
|
|
644
|
+
key: lookupKey("acme"),
|
|
645
|
+
crawl: async (ctx) => {
|
|
646
|
+
seen.push(ctx.shouldStop());
|
|
647
|
+
clock.advance(BUDGET);
|
|
648
|
+
seen.push(ctx.shouldStop());
|
|
649
|
+
return { outcome: "in_progress" };
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
await refresher([t]).tick();
|
|
653
|
+
expect(seen).toEqual([false, true]);
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it("works orgs with missing work before expired-only ones, least recently ticked first", async () => {
|
|
657
|
+
const expired = target("expired");
|
|
658
|
+
await seedCrawl(expired.key, [], new Date(T0.getTime() - 2 * HOUR));
|
|
659
|
+
const missingOld = target("missing-old");
|
|
660
|
+
await store.upsertSnapshot(missingOld.key, {
|
|
661
|
+
crawlStartedAt: T0,
|
|
662
|
+
lastTickAt: new Date(T0.getTime() - HOUR),
|
|
663
|
+
});
|
|
664
|
+
const missingNew = target("missing-new");
|
|
665
|
+
await store.upsertSnapshot(missingNew.key, {
|
|
666
|
+
crawlStartedAt: T0,
|
|
667
|
+
lastTickAt: T0,
|
|
668
|
+
});
|
|
669
|
+
const fresh = target("fresh");
|
|
670
|
+
await seedCrawl(fresh.key, [], T0);
|
|
671
|
+
const res = await refresher([
|
|
672
|
+
expired,
|
|
673
|
+
missingNew,
|
|
674
|
+
fresh,
|
|
675
|
+
missingOld,
|
|
676
|
+
]).tick();
|
|
677
|
+
expect(events).toEqual([
|
|
678
|
+
"crawl:missing-old",
|
|
679
|
+
"crawl:missing-new",
|
|
680
|
+
"crawl:expired",
|
|
681
|
+
]);
|
|
682
|
+
const freshStatus = res.targets.find((s) => s.key.org === "fresh");
|
|
683
|
+
expect(freshStatus?.state).toBe("idle");
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
it("writes lastTickAt on each worked snapshot and on the refresher status", async () => {
|
|
687
|
+
const t = target("acme", {
|
|
688
|
+
crawl: async () => {
|
|
689
|
+
clock.advance(1000);
|
|
690
|
+
return { outcome: "in_progress" };
|
|
691
|
+
},
|
|
692
|
+
});
|
|
693
|
+
const r = refresher([t]);
|
|
694
|
+
expect(r.status().lastTickAt).toBeNull();
|
|
695
|
+
await r.tick();
|
|
696
|
+
expect((await store.getSnapshot(t.key))?.lastTickAt).toEqual(
|
|
697
|
+
new Date(T0.getTime() + 1000),
|
|
698
|
+
);
|
|
699
|
+
const s = r.status();
|
|
700
|
+
expect(s.lastTickAt).toEqual(T0);
|
|
701
|
+
expect(s.targets[keyStr(t.key)]).toMatchObject({
|
|
702
|
+
state: "worked",
|
|
703
|
+
steps: ["crawl:missing"],
|
|
704
|
+
outcome: "in_progress",
|
|
705
|
+
});
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
it("never throws when discovery fails", async () => {
|
|
709
|
+
const r = refresher(async () => {
|
|
710
|
+
throw new Error("no installer");
|
|
711
|
+
});
|
|
712
|
+
const res = await r.tick();
|
|
713
|
+
expect(res.targets).toEqual([]);
|
|
714
|
+
expect(res.error).toBe("no installer");
|
|
715
|
+
expect(r.status().lastTickAt).toEqual(T0);
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it("caches discovery between ticks and refreshes it after the interval", async () => {
|
|
719
|
+
let calls = 0;
|
|
720
|
+
const r = refresher(
|
|
721
|
+
async () => {
|
|
722
|
+
calls++;
|
|
723
|
+
return [];
|
|
724
|
+
},
|
|
725
|
+
{ discoveryIntervalMs: 10 * MIN },
|
|
726
|
+
);
|
|
727
|
+
await r.tick();
|
|
728
|
+
clock.advance(MIN);
|
|
729
|
+
await r.tick();
|
|
730
|
+
expect(calls).toBe(1);
|
|
731
|
+
clock.advance(10 * MIN);
|
|
732
|
+
await r.tick();
|
|
733
|
+
expect(calls).toBe(2);
|
|
734
|
+
});
|
|
735
|
+
});
|