@cnzgray/dsh-mattpocock-workflow 0.3.0 → 0.5.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/lib/index.js
CHANGED
|
@@ -33,11 +33,12 @@ function isRecord(value) {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// src/core/snapshot-store.ts
|
|
36
|
-
var SNAPSHOT_STORE_VERSION =
|
|
36
|
+
var SNAPSHOT_STORE_VERSION = 5;
|
|
37
37
|
var BACKENDS = ["github", "gitlab", "gitea", "markdown"];
|
|
38
38
|
function serializeSnapshotStoreEntry(entry) {
|
|
39
39
|
return JSON.stringify({
|
|
40
40
|
version: entry.version,
|
|
41
|
+
libraryVersion: entry.libraryVersion,
|
|
41
42
|
workspaceRoot: entry.workspaceRoot,
|
|
42
43
|
backend: entry.backend,
|
|
43
44
|
repository: entry.repository,
|
|
@@ -49,17 +50,21 @@ function serializeSnapshotStoreEntry(entry) {
|
|
|
49
50
|
total: entry.total,
|
|
50
51
|
...entry.windowLimit === void 0 ? {} : { windowLimit: entry.windowLimit },
|
|
51
52
|
hierarchy: entry.hierarchy,
|
|
53
|
+
metrics: entry.metrics,
|
|
52
54
|
tickets: entry.tickets
|
|
53
55
|
});
|
|
54
56
|
}
|
|
55
|
-
function
|
|
57
|
+
function parseSnapshotTicket(raw) {
|
|
56
58
|
let payload;
|
|
57
59
|
try {
|
|
58
60
|
payload = JSON.parse(raw);
|
|
59
61
|
} catch {
|
|
60
62
|
return void 0;
|
|
61
63
|
}
|
|
62
|
-
return
|
|
64
|
+
return readTicket(payload);
|
|
65
|
+
}
|
|
66
|
+
function readSnapshotStoreEntry(value) {
|
|
67
|
+
return readEntry(value);
|
|
63
68
|
}
|
|
64
69
|
function readEntry(value) {
|
|
65
70
|
if (!isRecord(value)) return void 0;
|
|
@@ -85,10 +90,17 @@ function readEntry(value) {
|
|
|
85
90
|
}
|
|
86
91
|
const hierarchy = readHierarchy(value.hierarchy);
|
|
87
92
|
if (hierarchy === void 0) return void 0;
|
|
93
|
+
const libraryVersion = value.libraryVersion;
|
|
94
|
+
if (typeof libraryVersion !== "number" || !Number.isInteger(libraryVersion) || libraryVersion < 0) {
|
|
95
|
+
return void 0;
|
|
96
|
+
}
|
|
97
|
+
const metrics = readMetrics(value.metrics);
|
|
98
|
+
if (metrics === void 0) return void 0;
|
|
88
99
|
const tickets = readTickets(value.tickets);
|
|
89
100
|
if (tickets === void 0) return void 0;
|
|
90
101
|
return {
|
|
91
102
|
version: SNAPSHOT_STORE_VERSION,
|
|
103
|
+
libraryVersion,
|
|
92
104
|
workspaceRoot,
|
|
93
105
|
backend,
|
|
94
106
|
repository,
|
|
@@ -100,6 +112,7 @@ function readEntry(value) {
|
|
|
100
112
|
total,
|
|
101
113
|
...windowLimit === void 0 ? {} : { windowLimit },
|
|
102
114
|
hierarchy,
|
|
115
|
+
metrics,
|
|
103
116
|
tickets
|
|
104
117
|
};
|
|
105
118
|
}
|
|
@@ -166,6 +179,38 @@ function readHierarchy(value) {
|
|
|
166
179
|
if (value === "native" || value === "convention" || value === "none") return value;
|
|
167
180
|
return void 0;
|
|
168
181
|
}
|
|
182
|
+
function readMetrics(value) {
|
|
183
|
+
if (!isRecord(value)) return void 0;
|
|
184
|
+
for (const key of ["openBacklog", "readyCapacity", "blockedRatio", "needsTriage", "stalled"]) {
|
|
185
|
+
if (typeof value[key] !== "number" || !Number.isFinite(value[key])) return void 0;
|
|
186
|
+
}
|
|
187
|
+
for (const key of [
|
|
188
|
+
"staleCount",
|
|
189
|
+
"staleRatio",
|
|
190
|
+
"oldestUpdateDays",
|
|
191
|
+
"longestBlockerChain",
|
|
192
|
+
"openedLast30Days",
|
|
193
|
+
"closedLast30Days",
|
|
194
|
+
"netFlow"
|
|
195
|
+
]) {
|
|
196
|
+
if (readMetricValue(value[key]) === void 0) return void 0;
|
|
197
|
+
}
|
|
198
|
+
const coverage = value.coverage;
|
|
199
|
+
if (!isRecord(coverage) || typeof coverage.truncated !== "boolean") return void 0;
|
|
200
|
+
const pressure = value.pressure;
|
|
201
|
+
if (!isRecord(pressure) || typeof pressure.tier !== "string") return void 0;
|
|
202
|
+
if (!Array.isArray(pressure.dimensions)) return void 0;
|
|
203
|
+
if (!Array.isArray(value.attention)) return void 0;
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
function readMetricValue(value) {
|
|
207
|
+
if (!isRecord(value)) return void 0;
|
|
208
|
+
if (value.value !== null && (typeof value.value !== "number" || !Number.isFinite(value.value))) {
|
|
209
|
+
return void 0;
|
|
210
|
+
}
|
|
211
|
+
if (value.gap !== null && typeof value.gap !== "string") return void 0;
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
169
214
|
function readAuthor(value) {
|
|
170
215
|
if (value === null || value === void 0) return null;
|
|
171
216
|
if (!isRecord(value)) return void 0;
|
|
@@ -206,123 +251,320 @@ function readStringArray(value) {
|
|
|
206
251
|
// src/adapters/fs/snapshot-store.ts
|
|
207
252
|
var SNAPSHOT_STORE_TTL_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
208
253
|
var SNAPSHOT_STORE_MAX_ENTRIES = 20;
|
|
254
|
+
var SNAPSHOT_CHANGELOG_LIMIT = 200;
|
|
209
255
|
var DIRECTORY_NAME = "dsh-mattpocock-workflow";
|
|
210
256
|
function createNodeSnapshotStore(deps = {}) {
|
|
211
257
|
const env = deps.env ?? process.env;
|
|
212
258
|
const root = resolveRoot(deps, env);
|
|
213
259
|
const display = resolveDisplay(deps, env);
|
|
214
|
-
return {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return [];
|
|
223
|
-
}
|
|
224
|
-
const entries = [];
|
|
225
|
-
for (const name2 of names) {
|
|
226
|
-
if (!name2.endsWith(".json")) continue;
|
|
227
|
-
try {
|
|
228
|
-
const entry = parseSnapshotStoreEntry(await readFile(join(root, name2), "utf8"));
|
|
260
|
+
return {
|
|
261
|
+
read: (workspaceRoot) => readWorkspace(dirFor(root, workspaceRoot)),
|
|
262
|
+
write: (entry) => commitEntry(dirFor(root, entry.workspaceRoot), entry),
|
|
263
|
+
remove: (workspaceRoot) => rm(dirFor(root, workspaceRoot), { recursive: true, force: true }).then(() => void 0),
|
|
264
|
+
async list() {
|
|
265
|
+
const entries = [];
|
|
266
|
+
for (const meta of await listMetas(root)) {
|
|
267
|
+
const entry = await readWorkspace(dirFor(root, meta.entry.workspaceRoot));
|
|
229
268
|
if (entry !== void 0) entries.push(entry);
|
|
230
|
-
} catch {
|
|
231
269
|
}
|
|
232
|
-
|
|
233
|
-
return entries;
|
|
234
|
-
};
|
|
235
|
-
return {
|
|
236
|
-
read: (workspaceRoot) => readEntryAt(root, workspaceRoot),
|
|
237
|
-
async write(entry) {
|
|
238
|
-
await writeEntryAt(root, entry);
|
|
270
|
+
return entries;
|
|
239
271
|
},
|
|
240
|
-
async remove(workspaceRoot) {
|
|
241
|
-
await rm(fileFor(root, workspaceRoot), { force: true });
|
|
242
|
-
},
|
|
243
|
-
list,
|
|
244
272
|
async clear() {
|
|
245
|
-
const
|
|
246
|
-
for (const
|
|
247
|
-
await rm(
|
|
273
|
+
const metas = await listMetas(root);
|
|
274
|
+
for (const meta of metas) {
|
|
275
|
+
await rm(dirFor(root, meta.entry.workspaceRoot), { recursive: true, force: true });
|
|
248
276
|
}
|
|
249
|
-
return
|
|
277
|
+
return metas.length + await sweepLegacyFiles(root);
|
|
250
278
|
},
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if (gone === true) {
|
|
258
|
-
await rm(fileFor(root, entry.workspaceRoot), { force: true });
|
|
259
|
-
removed += 1;
|
|
260
|
-
continue;
|
|
261
|
-
}
|
|
262
|
-
if (gone === false && now - entry.fetchedAt > SNAPSHOT_STORE_TTL_MS) {
|
|
263
|
-
await rm(fileFor(root, entry.workspaceRoot), { force: true });
|
|
264
|
-
removed += 1;
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
kept.push(entry);
|
|
268
|
-
}
|
|
269
|
-
if (kept.length > SNAPSHOT_STORE_MAX_ENTRIES) {
|
|
270
|
-
kept.sort((left, right) => left.fetchedAt - right.fetchedAt);
|
|
271
|
-
for (const entry of kept.slice(0, kept.length - SNAPSHOT_STORE_MAX_ENTRIES)) {
|
|
272
|
-
await rm(fileFor(root, entry.workspaceRoot), { force: true });
|
|
273
|
-
removed += 1;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return removed;
|
|
277
|
-
}
|
|
279
|
+
prune: (now) => pruneDirs(root, now),
|
|
280
|
+
patchTicket: (workspaceRoot, ref, patch) => patchTicketAt(root, workspaceRoot, ref, patch),
|
|
281
|
+
removeTicket: (workspaceRoot, ref) => removeTicketAt(root, workspaceRoot, ref),
|
|
282
|
+
version: async (workspaceRoot) => (await readMetaAt(dirFor(root, workspaceRoot)))?.entry.libraryVersion ?? null,
|
|
283
|
+
changesSince: (workspaceRoot, afterVersion) => changesSinceAt(root, workspaceRoot, afterVersion),
|
|
284
|
+
location: () => display
|
|
278
285
|
};
|
|
279
286
|
}
|
|
280
|
-
function
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
287
|
+
async function listMetas(root) {
|
|
288
|
+
let names;
|
|
289
|
+
try {
|
|
290
|
+
names = await readdir(root);
|
|
291
|
+
} catch {
|
|
292
|
+
return [];
|
|
293
|
+
}
|
|
294
|
+
const metas = [];
|
|
295
|
+
for (const name2 of names) {
|
|
296
|
+
const meta = await readMetaAt(join(root, name2));
|
|
297
|
+
if (meta !== void 0) metas.push(meta);
|
|
298
|
+
}
|
|
299
|
+
return metas;
|
|
300
|
+
}
|
|
301
|
+
async function sweepLegacyFiles(root) {
|
|
302
|
+
let names;
|
|
303
|
+
try {
|
|
304
|
+
names = await readdir(root);
|
|
305
|
+
} catch {
|
|
306
|
+
return 0;
|
|
307
|
+
}
|
|
308
|
+
let removed = 0;
|
|
309
|
+
for (const name2 of names) {
|
|
310
|
+
if (!name2.endsWith(".json")) continue;
|
|
311
|
+
const path = join(root, name2);
|
|
312
|
+
if ((await stat2(path)).isDirectory()) continue;
|
|
313
|
+
await rm(path, { force: true });
|
|
314
|
+
removed += 1;
|
|
315
|
+
}
|
|
316
|
+
return removed;
|
|
317
|
+
}
|
|
318
|
+
async function pruneDirs(root, now) {
|
|
319
|
+
let removed = await sweepLegacyFiles(root);
|
|
320
|
+
for (const meta of await listMetas(root)) {
|
|
321
|
+
const workspaceRoot = meta.entry.workspaceRoot;
|
|
322
|
+
const gone = await isWorkspaceGone(workspaceRoot);
|
|
323
|
+
if (gone === true) {
|
|
324
|
+
await rm(dirFor(root, workspaceRoot), { recursive: true, force: true });
|
|
325
|
+
removed += 1;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (gone === false && now - meta.entry.fetchedAt > SNAPSHOT_STORE_TTL_MS) {
|
|
329
|
+
await rm(dirFor(root, workspaceRoot), { recursive: true, force: true });
|
|
330
|
+
removed += 1;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const kept = await listMetas(root);
|
|
334
|
+
if (kept.length > SNAPSHOT_STORE_MAX_ENTRIES) {
|
|
335
|
+
const sorted = [...kept].sort((left, right) => left.entry.fetchedAt - right.entry.fetchedAt);
|
|
336
|
+
for (const meta of sorted.slice(0, kept.length - SNAPSHOT_STORE_MAX_ENTRIES)) {
|
|
337
|
+
await rm(dirFor(root, meta.entry.workspaceRoot), { recursive: true, force: true });
|
|
338
|
+
removed += 1;
|
|
306
339
|
}
|
|
340
|
+
}
|
|
341
|
+
return removed;
|
|
342
|
+
}
|
|
343
|
+
async function patchTicketAt(root, workspaceRoot, ref, patch) {
|
|
344
|
+
const dir = dirFor(root, workspaceRoot);
|
|
345
|
+
const meta = await readMetaAt(dir);
|
|
346
|
+
if (meta === void 0) return false;
|
|
347
|
+
const item = meta.manifest.find((entry) => entry.ref === ref);
|
|
348
|
+
if (item === void 0) return false;
|
|
349
|
+
const ticket = await readTicketAt(dir, item);
|
|
350
|
+
if (ticket === void 0) return false;
|
|
351
|
+
const updated = {
|
|
352
|
+
...ticket,
|
|
353
|
+
...patch.labels === void 0 ? {} : { labels: patch.labels },
|
|
354
|
+
...patch.state === void 0 ? {} : { state: patch.state }
|
|
307
355
|
};
|
|
356
|
+
const sha = digest(JSON.stringify(updated));
|
|
357
|
+
await writeAtomic(join(ticketsDirOf(dir), item.file), JSON.stringify(updated));
|
|
358
|
+
await appendLog(dir, meta.entry.libraryVersion + 1, [ref], []);
|
|
359
|
+
await writeMeta(dir, { ...meta.entry, libraryVersion: meta.entry.libraryVersion + 1 }, [
|
|
360
|
+
...meta.manifest.filter((entry) => entry.ref !== ref),
|
|
361
|
+
{ ...item, sha }
|
|
362
|
+
]);
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
async function removeTicketAt(root, workspaceRoot, ref) {
|
|
366
|
+
const dir = dirFor(root, workspaceRoot);
|
|
367
|
+
const meta = await readMetaAt(dir);
|
|
368
|
+
if (meta === void 0) return false;
|
|
369
|
+
if (!meta.manifest.some((entry) => entry.ref === ref)) return false;
|
|
370
|
+
const newVersion = meta.entry.libraryVersion + 1;
|
|
371
|
+
await appendLog(dir, newVersion, [], [ref]);
|
|
372
|
+
await writeAtomic(tombstonesPathOf(dir), JSON.stringify([ref]));
|
|
373
|
+
await writeMeta(
|
|
374
|
+
dir,
|
|
375
|
+
{ ...meta.entry, libraryVersion: newVersion },
|
|
376
|
+
meta.manifest.filter((entry) => entry.ref !== ref)
|
|
377
|
+
);
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
async function changesSinceAt(root, workspaceRoot, afterVersion) {
|
|
381
|
+
const dir = dirFor(root, workspaceRoot);
|
|
382
|
+
const meta = await readMetaAt(dir);
|
|
383
|
+
if (meta === void 0) return void 0;
|
|
384
|
+
if (!Number.isInteger(afterVersion) || afterVersion < 0) return void 0;
|
|
385
|
+
const current = meta.entry.libraryVersion;
|
|
386
|
+
if (afterVersion > current) return void 0;
|
|
387
|
+
if (afterVersion === current) return { version: current, tickets: [], removed: [] };
|
|
388
|
+
const log = await readLogAt(dir);
|
|
389
|
+
if (log === null) return void 0;
|
|
390
|
+
const oldest = log.length === 0 ? current : log[0]?.version ?? current;
|
|
391
|
+
if (afterVersion < oldest - 1) return void 0;
|
|
392
|
+
const upserted = /* @__PURE__ */ new Set();
|
|
393
|
+
const removed = /* @__PURE__ */ new Set();
|
|
394
|
+
for (const entry of log) {
|
|
395
|
+
if (entry.version <= afterVersion) continue;
|
|
396
|
+
for (const ref of entry.upserted) {
|
|
397
|
+
upserted.add(ref);
|
|
398
|
+
removed.delete(ref);
|
|
399
|
+
}
|
|
400
|
+
for (const ref of entry.removed) {
|
|
401
|
+
removed.add(ref);
|
|
402
|
+
upserted.delete(ref);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
const byRef = new Map(meta.manifest.map((item) => [item.ref, item]));
|
|
406
|
+
const tickets = [];
|
|
407
|
+
for (const ref of upserted) {
|
|
408
|
+
const item = byRef.get(ref);
|
|
409
|
+
if (item === void 0) return void 0;
|
|
410
|
+
const ticket = await readTicketAt(dir, item);
|
|
411
|
+
if (ticket === void 0) return void 0;
|
|
412
|
+
tickets.push(ticket);
|
|
413
|
+
}
|
|
414
|
+
return { version: current, tickets, removed: [...removed] };
|
|
415
|
+
}
|
|
416
|
+
async function readWorkspace(dir) {
|
|
417
|
+
const meta = await readMetaAt(dir);
|
|
418
|
+
if (meta === void 0) return void 0;
|
|
419
|
+
const tickets = [];
|
|
420
|
+
for (const item of meta.manifest) {
|
|
421
|
+
const ticket = await readTicketAt(dir, item);
|
|
422
|
+
if (ticket === void 0) return void 0;
|
|
423
|
+
tickets.push(ticket);
|
|
424
|
+
}
|
|
425
|
+
return { ...meta.entry, tickets };
|
|
426
|
+
}
|
|
427
|
+
async function commitEntry(dir, entry) {
|
|
428
|
+
const old = await readMetaAt(dir);
|
|
429
|
+
const newVersion = (old?.entry.libraryVersion ?? 0) + 1;
|
|
430
|
+
const previous = new Map((old?.manifest ?? []).map((item) => [item.ref, item]));
|
|
431
|
+
const nextRefs = /* @__PURE__ */ new Set();
|
|
432
|
+
const manifest = [];
|
|
433
|
+
const upserted = [];
|
|
434
|
+
for (const ticket of entry.tickets) {
|
|
435
|
+
const file = ticketFileFor(ticket.ref);
|
|
436
|
+
const sha = digest(JSON.stringify(ticket));
|
|
437
|
+
const prior = previous.get(ticket.ref);
|
|
438
|
+
const unchanged = prior?.sha === sha && prior.file === file && await ticketFileExists(dir, prior.file);
|
|
439
|
+
if (!unchanged) {
|
|
440
|
+
upserted.push({ ref: ticket.ref, file, sha });
|
|
441
|
+
}
|
|
442
|
+
nextRefs.add(ticket.ref);
|
|
443
|
+
manifest.push({ ref: ticket.ref, file, sha });
|
|
444
|
+
}
|
|
445
|
+
const removed = (old?.manifest ?? []).filter((item) => !nextRefs.has(item.ref)).map((item) => item.ref);
|
|
446
|
+
await mkdir(ticketsDirOf(dir), { recursive: true, mode: 448 });
|
|
447
|
+
for (const item of upserted) {
|
|
448
|
+
const ticket = entry.tickets.find((candidate) => candidate.ref === item.ref);
|
|
449
|
+
if (ticket === void 0) continue;
|
|
450
|
+
await writeAtomic(join(ticketsDirOf(dir), item.file), JSON.stringify(ticket));
|
|
451
|
+
}
|
|
452
|
+
if (upserted.length > 0 || removed.length > 0) {
|
|
453
|
+
const log = await readLogAt(dir) ?? [];
|
|
454
|
+
log.push({ version: newVersion, upserted: upserted.map((item) => item.ref), removed });
|
|
455
|
+
while (log.length > SNAPSHOT_CHANGELOG_LIMIT) log.shift();
|
|
456
|
+
await writeAtomic(logPathOf(dir), JSON.stringify(log));
|
|
457
|
+
}
|
|
458
|
+
await writeAtomic(tombstonesPathOf(dir), JSON.stringify(removed));
|
|
459
|
+
const live = new Set(manifest.map((item) => item.file));
|
|
460
|
+
for (const ref of removed) live.add(ticketFileFor(ref));
|
|
461
|
+
await sweepOrphans(dir, live);
|
|
462
|
+
const stored = { ...entry, libraryVersion: newVersion };
|
|
463
|
+
await writeMeta(dir, stored, manifest);
|
|
464
|
+
return newVersion;
|
|
465
|
+
}
|
|
466
|
+
async function readMetaAt(dir) {
|
|
467
|
+
let raw;
|
|
468
|
+
try {
|
|
469
|
+
raw = await readFile(metaPathOf(dir), "utf8");
|
|
470
|
+
} catch {
|
|
471
|
+
return void 0;
|
|
472
|
+
}
|
|
473
|
+
let payload;
|
|
474
|
+
try {
|
|
475
|
+
payload = JSON.parse(raw);
|
|
476
|
+
} catch {
|
|
477
|
+
return void 0;
|
|
478
|
+
}
|
|
479
|
+
if (!isRecord(payload)) return void 0;
|
|
480
|
+
const entry = readSnapshotStoreEntry(payload);
|
|
481
|
+
if (entry === void 0) return void 0;
|
|
482
|
+
const manifest = readManifest(payload.manifest);
|
|
483
|
+
if (manifest === void 0) return void 0;
|
|
484
|
+
return { entry, manifest };
|
|
308
485
|
}
|
|
309
|
-
function
|
|
310
|
-
|
|
486
|
+
function readManifest(value) {
|
|
487
|
+
if (!Array.isArray(value)) return void 0;
|
|
488
|
+
const manifest = [];
|
|
489
|
+
for (const item of value) {
|
|
490
|
+
if (!isRecord(item)) return void 0;
|
|
491
|
+
if (typeof item.ref !== "string" || item.ref.length === 0) return void 0;
|
|
492
|
+
if (typeof item.file !== "string" || item.file.length === 0) return void 0;
|
|
493
|
+
if (typeof item.sha !== "string" || item.sha.length === 0) return void 0;
|
|
494
|
+
manifest.push({ ref: item.ref, file: item.file, sha: item.sha });
|
|
495
|
+
}
|
|
496
|
+
return manifest;
|
|
311
497
|
}
|
|
312
|
-
async function
|
|
498
|
+
async function readTicketAt(dir, item) {
|
|
313
499
|
try {
|
|
314
|
-
|
|
315
|
-
return parseSnapshotStoreEntry(raw);
|
|
500
|
+
return parseSnapshotTicket(await readFile(join(ticketsDirOf(dir), item.file), "utf8"));
|
|
316
501
|
} catch {
|
|
317
502
|
return void 0;
|
|
318
503
|
}
|
|
319
504
|
}
|
|
320
|
-
async function
|
|
321
|
-
|
|
322
|
-
|
|
505
|
+
async function ticketFileExists(dir, file) {
|
|
506
|
+
try {
|
|
507
|
+
await stat2(join(ticketsDirOf(dir), file));
|
|
508
|
+
return true;
|
|
509
|
+
} catch {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
async function readLogAt(dir) {
|
|
514
|
+
let raw;
|
|
515
|
+
try {
|
|
516
|
+
raw = await readFile(logPathOf(dir), "utf8");
|
|
517
|
+
} catch {
|
|
518
|
+
return [];
|
|
519
|
+
}
|
|
520
|
+
let payload;
|
|
521
|
+
try {
|
|
522
|
+
payload = JSON.parse(raw);
|
|
523
|
+
} catch {
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
if (!Array.isArray(payload)) return null;
|
|
527
|
+
const log = [];
|
|
528
|
+
for (const item of payload) {
|
|
529
|
+
if (!isRecord(item)) return null;
|
|
530
|
+
if (typeof item.version !== "number" || !Number.isInteger(item.version)) return null;
|
|
531
|
+
if (!Array.isArray(item.upserted) || !item.upserted.every((ref) => typeof ref === "string")) {
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
if (!Array.isArray(item.removed) || !item.removed.every((ref) => typeof ref === "string")) {
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
log.push({ version: item.version, upserted: item.upserted, removed: item.removed });
|
|
538
|
+
}
|
|
539
|
+
return log;
|
|
540
|
+
}
|
|
541
|
+
async function appendLog(dir, version, upserted, removed) {
|
|
542
|
+
const log = await readLogAt(dir) ?? [];
|
|
543
|
+
log.push({ version, upserted, removed });
|
|
544
|
+
while (log.length > SNAPSHOT_CHANGELOG_LIMIT) log.shift();
|
|
545
|
+
await writeAtomic(logPathOf(dir), JSON.stringify(log));
|
|
546
|
+
}
|
|
547
|
+
async function writeMeta(dir, entry, manifest) {
|
|
548
|
+
const fields = JSON.parse(serializeSnapshotStoreEntry(entry));
|
|
549
|
+
const payload = { ...fields, manifest };
|
|
550
|
+
await writeAtomic(metaPathOf(dir), JSON.stringify(payload));
|
|
551
|
+
}
|
|
552
|
+
async function sweepOrphans(dir, live) {
|
|
553
|
+
let names;
|
|
554
|
+
try {
|
|
555
|
+
names = await readdir(ticketsDirOf(dir));
|
|
556
|
+
} catch {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
for (const name2 of names) {
|
|
560
|
+
if (!live.has(name2)) await rm(join(ticketsDirOf(dir), name2), { force: true });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
async function writeAtomic(target, content) {
|
|
564
|
+
await mkdir(join(target, ".."), { recursive: true, mode: 448 }).catch(() => void 0);
|
|
323
565
|
const temporary = `${target}.${String(process.pid)}.tmp`;
|
|
324
566
|
try {
|
|
325
|
-
await writeFile(temporary,
|
|
567
|
+
await writeFile(temporary, content, { mode: 384 });
|
|
326
568
|
await rename(temporary, target);
|
|
327
569
|
await chmod(target, 384);
|
|
328
570
|
} catch (error) {
|
|
@@ -330,6 +572,30 @@ async function writeEntryAt(root, entry) {
|
|
|
330
572
|
throw error;
|
|
331
573
|
}
|
|
332
574
|
}
|
|
575
|
+
function digest(content) {
|
|
576
|
+
return createHash("sha1").update(content).digest("hex");
|
|
577
|
+
}
|
|
578
|
+
function dirFor(root, workspaceRoot) {
|
|
579
|
+
return join(root, keyOf(workspaceRoot));
|
|
580
|
+
}
|
|
581
|
+
function keyOf(workspaceRoot) {
|
|
582
|
+
return createHash("sha256").update(workspaceRoot).digest("hex").slice(0, 16);
|
|
583
|
+
}
|
|
584
|
+
function ticketFileFor(ref) {
|
|
585
|
+
return `${createHash("sha256").update(ref).digest("hex").slice(0, 16)}.json`;
|
|
586
|
+
}
|
|
587
|
+
function metaPathOf(dir) {
|
|
588
|
+
return join(dir, "meta.json");
|
|
589
|
+
}
|
|
590
|
+
function ticketsDirOf(dir) {
|
|
591
|
+
return join(dir, "tickets");
|
|
592
|
+
}
|
|
593
|
+
function tombstonesPathOf(dir) {
|
|
594
|
+
return join(dir, "tombstones.json");
|
|
595
|
+
}
|
|
596
|
+
function logPathOf(dir) {
|
|
597
|
+
return join(dir, "log.json");
|
|
598
|
+
}
|
|
333
599
|
async function isWorkspaceGone(workspaceRoot) {
|
|
334
600
|
try {
|
|
335
601
|
await stat2(workspaceRoot);
|
|
@@ -340,9 +606,6 @@ async function isWorkspaceGone(workspaceRoot) {
|
|
|
340
606
|
return false;
|
|
341
607
|
}
|
|
342
608
|
}
|
|
343
|
-
function keyOf(workspaceRoot) {
|
|
344
|
-
return createHash("sha256").update(workspaceRoot).digest("hex").slice(0, 16);
|
|
345
|
-
}
|
|
346
609
|
function resolveRoot(deps, env) {
|
|
347
610
|
if (deps.root !== void 0 && deps.root.length > 0) return deps.root;
|
|
348
611
|
const home = resolveHome(deps, env);
|
|
@@ -517,15 +780,22 @@ function chooseBackend(address, glabHosts, teaHosts = []) {
|
|
|
517
780
|
// src/core/tracker-error.ts
|
|
518
781
|
var TrackerError = class extends Error {
|
|
519
782
|
code;
|
|
783
|
+
/**
|
|
784
|
+
* 服务端要求的重试等待(毫秒);限流类错误才有,
|
|
785
|
+
* 调用方据此决定冷却时长,缺省时按失败次数指数退避。
|
|
786
|
+
*/
|
|
787
|
+
retryAfterMs;
|
|
520
788
|
/**
|
|
521
789
|
* @param code - 客户端可据以给出操作提示的错误码。
|
|
522
790
|
* @param message - 面向用户的失败原因。
|
|
523
791
|
* @param options - 原始错误,保留调用链。
|
|
792
|
+
* @param retryAfterMs - 服务端要求的重试等待(毫秒)。
|
|
524
793
|
*/
|
|
525
|
-
constructor(code, message, options) {
|
|
794
|
+
constructor(code, message, options, retryAfterMs) {
|
|
526
795
|
super(message, options);
|
|
527
796
|
this.name = "TrackerError";
|
|
528
797
|
this.code = code;
|
|
798
|
+
this.retryAfterMs = retryAfterMs;
|
|
529
799
|
}
|
|
530
800
|
};
|
|
531
801
|
var NETWORK_FAILURE = /dial tcp|no such host|i\/o timeout|timeout|network is unreachable|connection refused|connection reset|TLS handshake|proxyconnect|unexpected EOF/i;
|
|
@@ -680,6 +950,7 @@ var AUTHOR_FIELDS = {
|
|
|
680
950
|
displayName: "full_name",
|
|
681
951
|
avatarUrl: "avatar_url"
|
|
682
952
|
};
|
|
953
|
+
var PART_OF_LINE = /^part of #(\d+)$/i;
|
|
683
954
|
var STATUS_LINE = /^HTTP\/\S+\s+(\d{3})/m;
|
|
684
955
|
var TOTAL_LINE = /^x-total-count:\s*(\d+)\s*$/im;
|
|
685
956
|
function parseApiResponse(stdout, stderr) {
|
|
@@ -731,6 +1002,8 @@ function parseIssue(value, index, avatarBaseUrl2) {
|
|
|
731
1002
|
const state = readState2(value.state, number);
|
|
732
1003
|
const closedAt = readClosedAt(value.closed_at, number);
|
|
733
1004
|
const url = readWebUrl(value.html_url);
|
|
1005
|
+
const body = typeof value.body === "string" ? value.body : "";
|
|
1006
|
+
const parent = readParent(body, number);
|
|
734
1007
|
return {
|
|
735
1008
|
ticket: {
|
|
736
1009
|
ref: `#${String(number)}`,
|
|
@@ -745,11 +1018,21 @@ function parseIssue(value, index, avatarBaseUrl2) {
|
|
|
745
1018
|
openBlockers: 0,
|
|
746
1019
|
blockedBy: [],
|
|
747
1020
|
...closedAt === void 0 ? {} : { closedAt },
|
|
748
|
-
...url === void 0 ? {} : { url }
|
|
1021
|
+
...url === void 0 ? {} : { url },
|
|
1022
|
+
...parent === void 0 ? {} : { parent }
|
|
749
1023
|
},
|
|
750
|
-
body
|
|
1024
|
+
body
|
|
751
1025
|
};
|
|
752
1026
|
}
|
|
1027
|
+
function readParent(body, number) {
|
|
1028
|
+
const first = body.split(/\r?\n/).find((line) => line.trim().length > 0);
|
|
1029
|
+
if (first === void 0) return void 0;
|
|
1030
|
+
const match = PART_OF_LINE.exec(first.trim());
|
|
1031
|
+
if (match === null) return void 0;
|
|
1032
|
+
const parent = Number(match[1]);
|
|
1033
|
+
if (!Number.isSafeInteger(parent) || parent <= 0 || parent === number) return void 0;
|
|
1034
|
+
return `#${String(parent)}`;
|
|
1035
|
+
}
|
|
753
1036
|
function readState2(value, number) {
|
|
754
1037
|
if (value === "open") return "open";
|
|
755
1038
|
if (value === "closed") return "closed";
|
|
@@ -1601,6 +1884,7 @@ var TRIAGE_LABELS = [
|
|
|
1601
1884
|
var RPC_PATH = "/api/dsh-mattpocock-workflow";
|
|
1602
1885
|
var SNAPSHOT_PAGE_SIZE = 100;
|
|
1603
1886
|
var SNAPSHOT_MAX_WINDOW = 2e3;
|
|
1887
|
+
var SNAPSHOT_DELTA_MAX_ROWS = 1e3;
|
|
1604
1888
|
var RPC_METHODS = {
|
|
1605
1889
|
/** 读缓存;没有缓存时拉取一次。阻塞信息可能尚未定稿。 */
|
|
1606
1890
|
snapshotGet: "snapshot.get",
|
|
@@ -1642,6 +1926,8 @@ var ERROR_CODES = {
|
|
|
1642
1926
|
fetchFailed: "fetch-failed",
|
|
1643
1927
|
/** 编号不存在或工单已被删除。 */
|
|
1644
1928
|
ticketNotFound: "ticket-not-found",
|
|
1929
|
+
/** 后端拒绝了请求(限流);携带服务端要求的重试等待时按它退避。 */
|
|
1930
|
+
rateLimited: "rate-limited",
|
|
1645
1931
|
/** 当前后端不提供所请求的详情或写能力。 */
|
|
1646
1932
|
capabilityUnsupported: "capability-unsupported",
|
|
1647
1933
|
invalidParams: "invalid-params",
|
|
@@ -1948,17 +2234,61 @@ async function readIssue(deps, workspaceRoot, repository, number) {
|
|
|
1948
2234
|
ensureDetailSuccess(response, "\u8BFB\u53D6 Gitea \u5DE5\u5355\u8BE6\u60C5");
|
|
1949
2235
|
return parseIssue3(response.body);
|
|
1950
2236
|
}
|
|
2237
|
+
var MAX_COMMENT_PAGES = 200;
|
|
1951
2238
|
async function readComments(deps, workspaceRoot, repository, number) {
|
|
1952
|
-
const
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
const
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
2239
|
+
const collected = /* @__PURE__ */ new Map();
|
|
2240
|
+
const absorb = (entries) => {
|
|
2241
|
+
let added = 0;
|
|
2242
|
+
for (const entry of entries) {
|
|
2243
|
+
const key = commentKey(entry);
|
|
2244
|
+
if (collected.has(key)) continue;
|
|
2245
|
+
collected.set(key, entry.comment);
|
|
2246
|
+
added += 1;
|
|
2247
|
+
}
|
|
2248
|
+
return added;
|
|
2249
|
+
};
|
|
2250
|
+
const finish = () => sortCommentsByTime([...collected.values()]);
|
|
2251
|
+
const first = await readCommentsPage(deps, workspaceRoot, repository, number, 1);
|
|
2252
|
+
absorb(first.entries);
|
|
2253
|
+
if (first.total !== void 0 && collected.size >= first.total) return finish();
|
|
2254
|
+
if (first.entries.length < deps.pageSize) return finish();
|
|
2255
|
+
const second = await readCommentsPage(deps, workspaceRoot, repository, number, 2);
|
|
2256
|
+
if (absorb(second.entries) === 0) return finish();
|
|
2257
|
+
const total = first.total;
|
|
2258
|
+
if (total === void 0) {
|
|
2259
|
+
for (let page = 3; page <= MAX_COMMENT_PAGES; page += 1) {
|
|
2260
|
+
const next = await readCommentsPage(deps, workspaceRoot, repository, number, page);
|
|
2261
|
+
const added = absorb(next.entries);
|
|
2262
|
+
if (added === 0) break;
|
|
2263
|
+
if (next.total !== void 0 && collected.size >= next.total) break;
|
|
2264
|
+
if (next.entries.length < deps.pageSize) break;
|
|
2265
|
+
}
|
|
2266
|
+
return finish();
|
|
2267
|
+
}
|
|
2268
|
+
const lastPage = Math.min(Math.ceil(total / deps.pageSize), MAX_COMMENT_PAGES);
|
|
2269
|
+
const rest = Array.from({ length: Math.max(0, lastPage - 2) }, (_, index) => index + 3);
|
|
2270
|
+
const pages = await mapWithConcurrency(
|
|
2271
|
+
rest,
|
|
2272
|
+
deps.concurrency,
|
|
2273
|
+
(page) => readCommentsPage(deps, workspaceRoot, repository, number, page)
|
|
2274
|
+
);
|
|
2275
|
+
for (const page of pages) absorb(page.entries);
|
|
2276
|
+
return finish();
|
|
2277
|
+
}
|
|
2278
|
+
async function readCommentsPage(deps, workspaceRoot, repository, number, page) {
|
|
2279
|
+
const endpoint = `/repos/${repository.owner}/${repository.name}/issues/${String(number)}/comments?page=${String(page)}&limit=${String(deps.pageSize)}`;
|
|
2280
|
+
const response = await callGiteaApi(deps.run, workspaceRoot, endpoint);
|
|
2281
|
+
ensureDetailSuccess(response, "\u8BFB\u53D6 Gitea \u5DE5\u5355\u8BC4\u8BBA");
|
|
2282
|
+
return { entries: parseCommentsPage(response.body), total: response.total };
|
|
2283
|
+
}
|
|
2284
|
+
function commentKey(entry) {
|
|
2285
|
+
if (entry.id !== null) return `id\0${String(entry.id)}`;
|
|
2286
|
+
return [
|
|
2287
|
+
"content",
|
|
2288
|
+
entry.comment.createdAt ?? "",
|
|
2289
|
+
entry.comment.author ?? "",
|
|
2290
|
+
entry.comment.body
|
|
2291
|
+
].join("\0");
|
|
1962
2292
|
}
|
|
1963
2293
|
async function readCurrentLogin(run, workspaceRoot) {
|
|
1964
2294
|
const response = await callGiteaApi(run, workspaceRoot, "/user");
|
|
@@ -2001,9 +2331,12 @@ function parseCommentsPage(body) {
|
|
|
2001
2331
|
const text = typeof entry.body === "string" ? entry.body : "";
|
|
2002
2332
|
if (text.length === 0) continue;
|
|
2003
2333
|
comments.push({
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2334
|
+
id: typeof entry.id === "number" ? entry.id : null,
|
|
2335
|
+
comment: {
|
|
2336
|
+
author: isRecord(entry.user) ? readOptionalString(entry.user.login) : null,
|
|
2337
|
+
body: text,
|
|
2338
|
+
createdAt: readOptionalString(entry.created_at)
|
|
2339
|
+
}
|
|
2007
2340
|
});
|
|
2008
2341
|
}
|
|
2009
2342
|
return comments;
|
|
@@ -2099,7 +2432,7 @@ function invalidOptions(message) {
|
|
|
2099
2432
|
|
|
2100
2433
|
// src/adapters/gitea/tracker.ts
|
|
2101
2434
|
var DEFAULT_PAGE_SIZE = 50;
|
|
2102
|
-
var DEFAULT_CONCURRENCY =
|
|
2435
|
+
var DEFAULT_CONCURRENCY = 8;
|
|
2103
2436
|
var MAX_PAGES2 = 200;
|
|
2104
2437
|
function createGiteaTracker(deps) {
|
|
2105
2438
|
const pageSize = normalizePositive(deps.pageSize ?? DEFAULT_PAGE_SIZE, DEFAULT_PAGE_SIZE);
|
|
@@ -2107,7 +2440,7 @@ function createGiteaTracker(deps) {
|
|
|
2107
2440
|
deps.concurrency ?? DEFAULT_CONCURRENCY,
|
|
2108
2441
|
DEFAULT_CONCURRENCY
|
|
2109
2442
|
);
|
|
2110
|
-
const detailDeps = { run: deps.run, pageSize };
|
|
2443
|
+
const detailDeps = { run: deps.run, pageSize, concurrency };
|
|
2111
2444
|
return {
|
|
2112
2445
|
async fetchSnapshot(workspaceRoot, windowLimit) {
|
|
2113
2446
|
const window = windowLimit === void 0 ? void 0 : Math.max(1, Math.trunc(windowLimit));
|
|
@@ -2115,23 +2448,12 @@ function createGiteaTracker(deps) {
|
|
|
2115
2448
|
const repository = await readRepository(deps.run, workspaceRoot);
|
|
2116
2449
|
const issues = await readIssues(deps, workspaceRoot, repository, pageSize, void 0, window);
|
|
2117
2450
|
const viewer = await viewerTask;
|
|
2118
|
-
const states =
|
|
2119
|
-
for (const issue of issues.items) {
|
|
2120
|
-
if (issue.ticket.number !== null) states.set(issue.ticket.number, issue.ticket.state);
|
|
2121
|
-
}
|
|
2122
|
-
const native = await readNativeBlockers(
|
|
2123
|
-
deps,
|
|
2124
|
-
workspaceRoot,
|
|
2125
|
-
repository,
|
|
2126
|
-
issues.items,
|
|
2127
|
-
pageSize,
|
|
2128
|
-
concurrency
|
|
2129
|
-
);
|
|
2451
|
+
const states = statesOf(issues.items);
|
|
2130
2452
|
const tickets = issues.items.map((issue) => ({
|
|
2131
2453
|
...issue.ticket,
|
|
2132
2454
|
...resolveOpenBlockers({
|
|
2133
2455
|
ticket: issue.ticket,
|
|
2134
|
-
nativeBlockers:
|
|
2456
|
+
nativeBlockers: void 0,
|
|
2135
2457
|
text: issue.body,
|
|
2136
2458
|
states
|
|
2137
2459
|
})
|
|
@@ -2141,15 +2463,18 @@ function createGiteaTracker(deps) {
|
|
|
2141
2463
|
fetchedAt: deps.now(),
|
|
2142
2464
|
loaded: tickets.length,
|
|
2143
2465
|
total: Math.max(issues.total ?? tickets.length, tickets.length),
|
|
2144
|
-
// Gitea
|
|
2145
|
-
hierarchy: "
|
|
2466
|
+
// Gitea 没有原生子工单;父级来自子票正文首行的 `Part of #<n>` 约定。
|
|
2467
|
+
hierarchy: "convention",
|
|
2146
2468
|
tickets,
|
|
2147
2469
|
viewer,
|
|
2148
2470
|
...window === void 0 ? {} : { windowLimit: window }
|
|
2149
2471
|
};
|
|
2150
2472
|
},
|
|
2151
2473
|
async fetchChanges(workspaceRoot, since) {
|
|
2152
|
-
return readGiteaChanges(deps, workspaceRoot, pageSize, since
|
|
2474
|
+
return readGiteaChanges(deps, workspaceRoot, pageSize, since);
|
|
2475
|
+
},
|
|
2476
|
+
async resolveBlockers(workspaceRoot, targets) {
|
|
2477
|
+
return readNativeResolution(deps, workspaceRoot, targets, pageSize, concurrency);
|
|
2153
2478
|
},
|
|
2154
2479
|
fetchDetail: (workspaceRoot, reference) => readGiteaDetail(detailDeps, workspaceRoot, reference),
|
|
2155
2480
|
updateTicket: (workspaceRoot, reference, patch) => applyGiteaChanges(detailDeps, workspaceRoot, reference, patch),
|
|
@@ -2166,7 +2491,7 @@ async function readGiteaTotal(deps, workspaceRoot, repository) {
|
|
|
2166
2491
|
}
|
|
2167
2492
|
return response.total;
|
|
2168
2493
|
}
|
|
2169
|
-
async function readGiteaChanges(deps, workspaceRoot, pageSize, since
|
|
2494
|
+
async function readGiteaChanges(deps, workspaceRoot, pageSize, since) {
|
|
2170
2495
|
if (!Number.isFinite(since)) {
|
|
2171
2496
|
throw new TrackerError("fetch-failed", "\u589E\u91CF\u7A97\u53E3\u7684\u8D77\u70B9\u4E0D\u662F\u6709\u6548\u65F6\u95F4\u6233\u3002");
|
|
2172
2497
|
}
|
|
@@ -2178,30 +2503,19 @@ async function readGiteaChanges(deps, workspaceRoot, pageSize, since, concurrenc
|
|
|
2178
2503
|
const total = await readGiteaTotal(deps, workspaceRoot, repository);
|
|
2179
2504
|
const issues = await readIssues(deps, workspaceRoot, repository, pageSize, since);
|
|
2180
2505
|
const viewer = await viewerTask;
|
|
2181
|
-
const states =
|
|
2182
|
-
for (const issue of issues.items) {
|
|
2183
|
-
if (issue.ticket.number !== null) states.set(issue.ticket.number, issue.ticket.state);
|
|
2184
|
-
}
|
|
2185
|
-
const native = await readNativeBlockers(
|
|
2186
|
-
deps,
|
|
2187
|
-
workspaceRoot,
|
|
2188
|
-
repository,
|
|
2189
|
-
issues.items,
|
|
2190
|
-
pageSize,
|
|
2191
|
-
concurrency
|
|
2192
|
-
);
|
|
2506
|
+
const states = statesOf(issues.items);
|
|
2193
2507
|
const tickets = issues.items.map((issue) => ({
|
|
2194
2508
|
...issue.ticket,
|
|
2195
2509
|
...resolveOpenBlockers({
|
|
2196
2510
|
ticket: issue.ticket,
|
|
2197
|
-
nativeBlockers:
|
|
2511
|
+
nativeBlockers: void 0,
|
|
2198
2512
|
text: issue.body,
|
|
2199
2513
|
states
|
|
2200
2514
|
})
|
|
2201
2515
|
}));
|
|
2202
2516
|
return {
|
|
2203
2517
|
tickets,
|
|
2204
|
-
hierarchy: "
|
|
2518
|
+
hierarchy: "convention",
|
|
2205
2519
|
truncated: issues.truncated,
|
|
2206
2520
|
total,
|
|
2207
2521
|
...viewer === void 0 ? {} : { viewer }
|
|
@@ -2237,27 +2551,44 @@ async function readIssues(deps, workspaceRoot, repository, pageSize, since, wind
|
|
|
2237
2551
|
truncated
|
|
2238
2552
|
};
|
|
2239
2553
|
}
|
|
2240
|
-
async function
|
|
2241
|
-
const open =
|
|
2242
|
-
(
|
|
2554
|
+
async function readNativeResolution(deps, workspaceRoot, targets, pageSize, concurrency) {
|
|
2555
|
+
const open = targets.filter(
|
|
2556
|
+
(target) => target.number !== null
|
|
2243
2557
|
);
|
|
2244
|
-
if (open.length === 0) return
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2558
|
+
if (open.length === 0) return emptyResolution();
|
|
2559
|
+
const repository = await readRepository(deps.run, workspaceRoot);
|
|
2560
|
+
const settled = await mapWithConcurrency(
|
|
2561
|
+
open,
|
|
2562
|
+
concurrency,
|
|
2563
|
+
async (target) => {
|
|
2564
|
+
try {
|
|
2565
|
+
const blocked2 = await readDependencies(
|
|
2566
|
+
deps,
|
|
2567
|
+
workspaceRoot,
|
|
2568
|
+
repository,
|
|
2569
|
+
target.number,
|
|
2570
|
+
pageSize
|
|
2571
|
+
);
|
|
2572
|
+
return { ref: target.ref, outcome: "ok", blocked: blocked2 };
|
|
2573
|
+
} catch (error) {
|
|
2574
|
+
if (error instanceof DependenciesUnavailable) {
|
|
2575
|
+
return { ref: target.ref, outcome: "unavailable" };
|
|
2576
|
+
}
|
|
2577
|
+
return { ref: target.ref, outcome: "failed" };
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
);
|
|
2581
|
+
if (settled.some((entry) => entry.outcome === "unavailable")) return emptyResolution();
|
|
2582
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2583
|
+
const blockedBy = /* @__PURE__ */ new Map();
|
|
2584
|
+
for (const entry of settled) {
|
|
2585
|
+
if (entry.outcome !== "ok") continue;
|
|
2586
|
+
counts.set(entry.ref, entry.blocked.open);
|
|
2587
|
+
if (entry.blocked.refs.length > 0) {
|
|
2588
|
+
blockedBy.set(entry.ref, remoteBlockerRefs(entry.blocked.refs));
|
|
2589
|
+
}
|
|
2260
2590
|
}
|
|
2591
|
+
return { open: counts, blockedBy };
|
|
2261
2592
|
}
|
|
2262
2593
|
async function readDependencies(deps, workspaceRoot, repository, index, pageSize) {
|
|
2263
2594
|
let open = 0;
|
|
@@ -2284,6 +2615,16 @@ async function readDependencies(deps, workspaceRoot, repository, index, pageSize
|
|
|
2284
2615
|
}
|
|
2285
2616
|
var DependenciesUnavailable = class extends Error {
|
|
2286
2617
|
};
|
|
2618
|
+
function emptyResolution() {
|
|
2619
|
+
return { open: /* @__PURE__ */ new Map(), blockedBy: /* @__PURE__ */ new Map() };
|
|
2620
|
+
}
|
|
2621
|
+
function statesOf(issues) {
|
|
2622
|
+
const states = /* @__PURE__ */ new Map();
|
|
2623
|
+
for (const issue of issues) {
|
|
2624
|
+
if (issue.ticket.number !== null) states.set(issue.ticket.number, issue.ticket.state);
|
|
2625
|
+
}
|
|
2626
|
+
return states;
|
|
2627
|
+
}
|
|
2287
2628
|
function issuesEndpoint(repository, pageSize, page, since) {
|
|
2288
2629
|
const window = since === void 0 ? "" : `&since=${new Date(since).toISOString()}`;
|
|
2289
2630
|
return `/repos/${repository.owner}/${repository.name}/issues?state=all&type=issues&sort=recentupdate&page=${String(page)}&limit=${String(pageSize)}${window}`;
|
|
@@ -2355,6 +2696,15 @@ function mapGhError(error) {
|
|
|
2355
2696
|
cause: error
|
|
2356
2697
|
});
|
|
2357
2698
|
}
|
|
2699
|
+
const rateLimit = rateLimitOf(error.stderr);
|
|
2700
|
+
if (rateLimit !== null) {
|
|
2701
|
+
return new TrackerError(
|
|
2702
|
+
"rate-limited",
|
|
2703
|
+
`GitHub \u9650\u6D41\u4E2D\uFF0C\u7A0D\u540E\u4F1A\u81EA\u52A8\u6062\u590D\uFF1A${rateLimit}`,
|
|
2704
|
+
{ cause: error },
|
|
2705
|
+
retryAfterMsOf(error.stderr)
|
|
2706
|
+
);
|
|
2707
|
+
}
|
|
2358
2708
|
return new TrackerError("fetch-failed", `\u8BFB\u53D6\u5DE5\u5355\u5931\u8D25\uFF1A${commandFailureDetail(error)}`, {
|
|
2359
2709
|
cause: error
|
|
2360
2710
|
});
|
|
@@ -2366,6 +2716,18 @@ function isAuthenticationFailure(stderr) {
|
|
|
2366
2716
|
stderr
|
|
2367
2717
|
);
|
|
2368
2718
|
}
|
|
2719
|
+
var RATE_LIMITED = /rate limit|too many requests|HTTP 429/i;
|
|
2720
|
+
function rateLimitOf(stderr) {
|
|
2721
|
+
if (!RATE_LIMITED.test(stderr)) return null;
|
|
2722
|
+
const firstLine = stderr.split("\n").map((line) => line.trim()).find((line) => line.length > 0);
|
|
2723
|
+
return firstLine ?? "\u5DF2\u89E6\u53D1\u9650\u6D41\u3002";
|
|
2724
|
+
}
|
|
2725
|
+
function retryAfterMsOf(stderr) {
|
|
2726
|
+
const match = /retry[ -]?after[^0-9]*(\d{1,4})\s*s?\b/i.exec(stderr);
|
|
2727
|
+
if (match === null) return void 0;
|
|
2728
|
+
const seconds = Number(match[1]);
|
|
2729
|
+
return Number.isFinite(seconds) && seconds > 0 ? seconds * 1e3 : void 0;
|
|
2730
|
+
}
|
|
2369
2731
|
|
|
2370
2732
|
// src/adapters/github/delete.ts
|
|
2371
2733
|
var NOT_FOUND4 = /Could not resolve to an Issue|HTTP 404|404 Not Found|Not Found/i;
|
|
@@ -2565,6 +2927,15 @@ function parseIssueGraphql(output, repository) {
|
|
|
2565
2927
|
}
|
|
2566
2928
|
return { total, tickets, viewer: readViewer2(payload), ...readPageCursor(issues) };
|
|
2567
2929
|
}
|
|
2930
|
+
function parseIssuesTotal(output) {
|
|
2931
|
+
const payload = parseJson6(output, "gh \u8FD4\u56DE\u7684\u4E0D\u662F\u6709\u6548 JSON");
|
|
2932
|
+
const issues = readIssues2(readRepository3(payload));
|
|
2933
|
+
const total = issues.totalCount;
|
|
2934
|
+
if (typeof total !== "number" || !Number.isInteger(total) || total < 0) {
|
|
2935
|
+
throw invalid3("\u4ED3\u5E93\u7684 issues.totalCount \u4E0D\u662F\u975E\u8D1F\u6574\u6570");
|
|
2936
|
+
}
|
|
2937
|
+
return total;
|
|
2938
|
+
}
|
|
2568
2939
|
function readViewer2(payload) {
|
|
2569
2940
|
if (!isRecord(payload)) return null;
|
|
2570
2941
|
const data = payload.data;
|
|
@@ -2600,7 +2971,7 @@ function parseIssueNode(value, index, repository) {
|
|
|
2600
2971
|
const state = readState6(value.state, number);
|
|
2601
2972
|
const closedAt = readClosedAt3(value.closedAt, number);
|
|
2602
2973
|
const url = readWebUrl(value.url);
|
|
2603
|
-
const parent =
|
|
2974
|
+
const parent = readParent2(value.parent, number, repository);
|
|
2604
2975
|
return {
|
|
2605
2976
|
ref: `#${String(number)}`,
|
|
2606
2977
|
number,
|
|
@@ -2617,7 +2988,7 @@ function parseIssueNode(value, index, repository) {
|
|
|
2617
2988
|
...parent === void 0 ? {} : { parent }
|
|
2618
2989
|
};
|
|
2619
2990
|
}
|
|
2620
|
-
function
|
|
2991
|
+
function readParent2(value, number, repository) {
|
|
2621
2992
|
if (value === void 0 || value === null) return void 0;
|
|
2622
2993
|
if (!isRecord(value)) throw invalid3(`\u7B2C ${String(number)} \u5F20\u7968\u7684\u7236\u7968\u4E0D\u662F\u5BF9\u8C61`);
|
|
2623
2994
|
const parentNumber = value.number;
|
|
@@ -2784,6 +3155,7 @@ function invalidOptions2(message) {
|
|
|
2784
3155
|
|
|
2785
3156
|
// src/adapters/github/tracker.ts
|
|
2786
3157
|
var DEFAULT_LIMIT = 100;
|
|
3158
|
+
var DEFAULT_CHANGES_PAGE_LIMIT = 5;
|
|
2787
3159
|
var ISSUE_NODES = `nodes {
|
|
2788
3160
|
number
|
|
2789
3161
|
title
|
|
@@ -2809,6 +3181,22 @@ var ISSUE_QUERY = `query ($owner: String!, $name: String!, $limit: Int!, $after:
|
|
|
2809
3181
|
}
|
|
2810
3182
|
}
|
|
2811
3183
|
}`;
|
|
3184
|
+
var ISSUE_CHANGES_QUERY = `query ($owner: String!, $name: String!, $limit: Int!, $after: String, $since: DateTime!) {
|
|
3185
|
+
viewer { login }
|
|
3186
|
+
repository(owner: $owner, name: $name) {
|
|
3187
|
+
nameWithOwner
|
|
3188
|
+
issues(first: $limit, after: $after, orderBy: { field: UPDATED_AT, direction: DESC }, filterBy: { since: $since }) {
|
|
3189
|
+
totalCount
|
|
3190
|
+
pageInfo { hasNextPage endCursor }
|
|
3191
|
+
${ISSUE_NODES}
|
|
3192
|
+
}
|
|
3193
|
+
}
|
|
3194
|
+
}`;
|
|
3195
|
+
var ISSUE_TOTAL_QUERY = `query ($owner: String!, $name: String!) {
|
|
3196
|
+
repository(owner: $owner, name: $name) {
|
|
3197
|
+
issues(first: 1) { totalCount }
|
|
3198
|
+
}
|
|
3199
|
+
}`;
|
|
2812
3200
|
function createGithubTracker(deps) {
|
|
2813
3201
|
const limit = normalizeLimit(deps.limit ?? DEFAULT_LIMIT);
|
|
2814
3202
|
return {
|
|
@@ -2829,6 +3217,21 @@ function createGithubTracker(deps) {
|
|
|
2829
3217
|
windowLimit: window
|
|
2830
3218
|
};
|
|
2831
3219
|
},
|
|
3220
|
+
async fetchChanges(workspaceRoot, since) {
|
|
3221
|
+
if (!Number.isFinite(since)) {
|
|
3222
|
+
throw new TrackerError("fetch-failed", "\u589E\u91CF\u7A97\u53E3\u7684\u8D77\u70B9\u4E0D\u662F\u6709\u6548\u65F6\u95F4\u6233\u3002");
|
|
3223
|
+
}
|
|
3224
|
+
const repository = await readRepository2(deps.run, workspaceRoot);
|
|
3225
|
+
const total = await readIssuesTotal(deps, workspaceRoot, repository);
|
|
3226
|
+
const changed = await readChangedTickets(deps, workspaceRoot, repository, limit, since);
|
|
3227
|
+
return {
|
|
3228
|
+
tickets: changed.tickets,
|
|
3229
|
+
hierarchy: "native",
|
|
3230
|
+
truncated: changed.truncated,
|
|
3231
|
+
total,
|
|
3232
|
+
viewer: changed.viewer
|
|
3233
|
+
};
|
|
3234
|
+
},
|
|
2832
3235
|
fetchDetail: (workspaceRoot, reference) => readGithubDetail(deps, workspaceRoot, reference),
|
|
2833
3236
|
updateTicket: (workspaceRoot, reference, patch) => applyGithubChanges(deps, workspaceRoot, reference, patch),
|
|
2834
3237
|
fetchOptions: (workspaceRoot) => readGithubOptions(deps, workspaceRoot),
|
|
@@ -2860,6 +3263,55 @@ async function readTickets2(deps, workspaceRoot, repository, pageSize, window) {
|
|
|
2860
3263
|
const windowed = tickets.slice(0, window);
|
|
2861
3264
|
return { total, tickets: windowed, viewer, hasNextPage: false, endCursor: null };
|
|
2862
3265
|
}
|
|
3266
|
+
async function readIssuesTotal(deps, workspaceRoot, repository) {
|
|
3267
|
+
const args = [
|
|
3268
|
+
"api",
|
|
3269
|
+
"graphql",
|
|
3270
|
+
"-f",
|
|
3271
|
+
`query=${ISSUE_TOTAL_QUERY}`,
|
|
3272
|
+
"-f",
|
|
3273
|
+
`owner=${repository.owner}`,
|
|
3274
|
+
"-f",
|
|
3275
|
+
`name=${repository.name}`
|
|
3276
|
+
];
|
|
3277
|
+
return parseIssuesTotal(await callGh(deps.run, workspaceRoot, args));
|
|
3278
|
+
}
|
|
3279
|
+
async function readChangedTickets(deps, workspaceRoot, repository, pageSize, since) {
|
|
3280
|
+
const tickets = [];
|
|
3281
|
+
let viewer = null;
|
|
3282
|
+
let cursor = null;
|
|
3283
|
+
for (let page = 1; page <= DEFAULT_CHANGES_PAGE_LIMIT; page += 1) {
|
|
3284
|
+
const parsed = await readChangesPage(deps, workspaceRoot, repository, pageSize, cursor, since);
|
|
3285
|
+
viewer = parsed.viewer ?? viewer;
|
|
3286
|
+
tickets.push(...parsed.tickets);
|
|
3287
|
+
if (!parsed.hasNextPage || parsed.endCursor === null || parsed.tickets.length === 0) {
|
|
3288
|
+
return { tickets, viewer, truncated: false };
|
|
3289
|
+
}
|
|
3290
|
+
cursor = parsed.endCursor;
|
|
3291
|
+
if (page === DEFAULT_CHANGES_PAGE_LIMIT) {
|
|
3292
|
+
return { tickets, viewer, truncated: true };
|
|
3293
|
+
}
|
|
3294
|
+
}
|
|
3295
|
+
return { tickets, viewer, truncated: false };
|
|
3296
|
+
}
|
|
3297
|
+
async function readChangesPage(deps, workspaceRoot, repository, limit, after, since) {
|
|
3298
|
+
const args = [
|
|
3299
|
+
"api",
|
|
3300
|
+
"graphql",
|
|
3301
|
+
"-f",
|
|
3302
|
+
`query=${ISSUE_CHANGES_QUERY}`,
|
|
3303
|
+
"-f",
|
|
3304
|
+
`owner=${repository.owner}`,
|
|
3305
|
+
"-f",
|
|
3306
|
+
`name=${repository.name}`,
|
|
3307
|
+
"-F",
|
|
3308
|
+
`limit=${String(limit)}`,
|
|
3309
|
+
"-f",
|
|
3310
|
+
`since=${new Date(since).toISOString()}`,
|
|
3311
|
+
...after === null ? [] : ["-f", `after=${after}`]
|
|
3312
|
+
];
|
|
3313
|
+
return parseIssueGraphql(await callGh(deps.run, workspaceRoot, args), repositoryLabel(repository));
|
|
3314
|
+
}
|
|
2863
3315
|
async function readTicketsPage(deps, workspaceRoot, repository, limit, after) {
|
|
2864
3316
|
const args = [
|
|
2865
3317
|
"api",
|
|
@@ -3259,7 +3711,7 @@ function parseWorkItemHierarchy(output) {
|
|
|
3259
3711
|
for (const node of nodes) {
|
|
3260
3712
|
if (!isRecord(node)) throw invalid4("\u5DE5\u4F5C\u9879\u5C42\u7EA7\u8282\u70B9\u4E0D\u662F\u5BF9\u8C61");
|
|
3261
3713
|
const iid = readIid2(node.iid);
|
|
3262
|
-
const parent =
|
|
3714
|
+
const parent = readParent3(node.widgets);
|
|
3263
3715
|
if (parent !== void 0) parents.set(iid, parent);
|
|
3264
3716
|
}
|
|
3265
3717
|
return parents;
|
|
@@ -3287,7 +3739,7 @@ function hierarchyQuery(batch) {
|
|
|
3287
3739
|
const iids = batch.map((iid) => `"${String(iid)}"`).join(", ");
|
|
3288
3740
|
return `query ($project: ID!) { project(fullPath: $project) { workItems(iids: [${iids}], first: ${String(batch.length)}) { nodes { iid widgets { type ... on WorkItemWidgetHierarchy { parent { iid workItemType { name } } } } } } } }`;
|
|
3289
3741
|
}
|
|
3290
|
-
function
|
|
3742
|
+
function readParent3(value) {
|
|
3291
3743
|
if (!Array.isArray(value)) return void 0;
|
|
3292
3744
|
for (const widget of value) {
|
|
3293
3745
|
if (!isRecord(widget)) continue;
|
|
@@ -3419,7 +3871,7 @@ function createGitlabTracker(deps) {
|
|
|
3419
3871
|
const open = targets.filter(
|
|
3420
3872
|
(target) => target.number !== null
|
|
3421
3873
|
);
|
|
3422
|
-
if (open.length === 0) return
|
|
3874
|
+
if (open.length === 0) return emptyResolution2();
|
|
3423
3875
|
const hint = hints.get(workspaceRoot) ?? {
|
|
3424
3876
|
project: await readProjectAddress(deps.run, workspaceRoot),
|
|
3425
3877
|
listSupportsNative: false,
|
|
@@ -3432,7 +3884,7 @@ function createGitlabTracker(deps) {
|
|
|
3432
3884
|
hint,
|
|
3433
3885
|
open
|
|
3434
3886
|
);
|
|
3435
|
-
if (!supported) return
|
|
3887
|
+
if (!supported) return emptyResolution2();
|
|
3436
3888
|
const counts = await readBlockedByCounts(deps, workspaceRoot, hint.project.path, open);
|
|
3437
3889
|
const native = /* @__PURE__ */ new Map();
|
|
3438
3890
|
for (const target of open) {
|
|
@@ -3457,7 +3909,7 @@ function createGitlabTracker(deps) {
|
|
|
3457
3909
|
fetchAttachment: (workspaceRoot, path) => readGitlabAttachment(deps, workspaceRoot, path)
|
|
3458
3910
|
};
|
|
3459
3911
|
}
|
|
3460
|
-
function
|
|
3912
|
+
function emptyResolution2() {
|
|
3461
3913
|
return { open: /* @__PURE__ */ new Map(), blockedBy: /* @__PURE__ */ new Map() };
|
|
3462
3914
|
}
|
|
3463
3915
|
async function readSnapshot(deps, hints, workspaceRoot, pageSize, windowLimit) {
|
|
@@ -4163,9 +4615,13 @@ var TICKET_ACTIONS = {
|
|
|
4163
4615
|
|
|
4164
4616
|
// src/host/env-check-service.ts
|
|
4165
4617
|
var TRACKER_DOC = "docs/agents/issue-tracker.md";
|
|
4618
|
+
var ENV_CHECK_CACHE_TTL_MS = 10 * 60 * 1e3;
|
|
4619
|
+
var ENV_CHECK_BACKOFF_START_MS = 8e3;
|
|
4620
|
+
var ENV_CHECK_BACKOFF_MAX_MS = 5 * 60 * 1e3;
|
|
4166
4621
|
var SKILL_DIRECTORIES = [".dsh/skills", ".agents/skills"];
|
|
4167
4622
|
function createEnvCheckService(deps) {
|
|
4168
4623
|
const pending = /* @__PURE__ */ new Map();
|
|
4624
|
+
const cache = /* @__PURE__ */ new Map();
|
|
4169
4625
|
return {
|
|
4170
4626
|
handle(method, params) {
|
|
4171
4627
|
if (method !== RPC_METHODS.envCheck) {
|
|
@@ -4179,7 +4635,38 @@ function createEnvCheckService(deps) {
|
|
|
4179
4635
|
}
|
|
4180
4636
|
const running = pending.get(workspaceRoot);
|
|
4181
4637
|
if (running !== void 0) return running;
|
|
4182
|
-
const
|
|
4638
|
+
const now = deps.now();
|
|
4639
|
+
const state = cache.get(workspaceRoot);
|
|
4640
|
+
const cached = state?.success;
|
|
4641
|
+
if (cached !== void 0 && cached !== null && now < cached.expiresAt) {
|
|
4642
|
+
const task2 = serveCached(deps, workspaceRoot, cached).then((outcome) => {
|
|
4643
|
+
cache.set(workspaceRoot, nextCacheState(state, outcome, deps.now()));
|
|
4644
|
+
return outcome.result;
|
|
4645
|
+
});
|
|
4646
|
+
pending.set(workspaceRoot, task2);
|
|
4647
|
+
void task2.then(
|
|
4648
|
+
() => {
|
|
4649
|
+
pending.delete(workspaceRoot);
|
|
4650
|
+
},
|
|
4651
|
+
() => {
|
|
4652
|
+
pending.delete(workspaceRoot);
|
|
4653
|
+
}
|
|
4654
|
+
);
|
|
4655
|
+
return task2;
|
|
4656
|
+
}
|
|
4657
|
+
if (state !== void 0 && state.failures > 0 && state.lastFailure !== null) {
|
|
4658
|
+
const backoff = Math.min(
|
|
4659
|
+
ENV_CHECK_BACKOFF_START_MS * 2 ** (state.failures - 1),
|
|
4660
|
+
ENV_CHECK_BACKOFF_MAX_MS
|
|
4661
|
+
);
|
|
4662
|
+
if (now - state.lastRunAt < backoff) {
|
|
4663
|
+
return Promise.resolve({ ok: true, result: state.lastFailure });
|
|
4664
|
+
}
|
|
4665
|
+
}
|
|
4666
|
+
const task = runCheck(deps, workspaceRoot).then((outcome) => {
|
|
4667
|
+
cache.set(workspaceRoot, nextCacheState(state, outcome, deps.now()));
|
|
4668
|
+
return outcome.result;
|
|
4669
|
+
});
|
|
4183
4670
|
pending.set(workspaceRoot, task);
|
|
4184
4671
|
void task.then(
|
|
4185
4672
|
() => {
|
|
@@ -4193,26 +4680,64 @@ function createEnvCheckService(deps) {
|
|
|
4193
4680
|
}
|
|
4194
4681
|
};
|
|
4195
4682
|
}
|
|
4196
|
-
async function
|
|
4197
|
-
const
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4683
|
+
async function serveCached(deps, workspaceRoot, cached) {
|
|
4684
|
+
const probe = await probeBackend(deps, workspaceRoot);
|
|
4685
|
+
const identity = identityOf(probe);
|
|
4686
|
+
if (identity !== null && identity === cached.identity) {
|
|
4687
|
+
return { result: { ok: true, result: cached.report }, identity: cached.identity };
|
|
4688
|
+
}
|
|
4689
|
+
return runCheck(deps, workspaceRoot);
|
|
4690
|
+
}
|
|
4691
|
+
function nextCacheState(previous, outcome, now) {
|
|
4692
|
+
const report = outcome.result.ok ? outcome.result.result : null;
|
|
4693
|
+
if (report === null) return previous ?? emptyCache(now);
|
|
4694
|
+
const failed = report.items.some((item) => item.status === "fail");
|
|
4695
|
+
if (!failed) {
|
|
4696
|
+
return {
|
|
4697
|
+
success: outcome.identity === null ? null : { report, identity: outcome.identity, expiresAt: now + ENV_CHECK_CACHE_TTL_MS },
|
|
4698
|
+
failures: 0,
|
|
4699
|
+
lastRunAt: now,
|
|
4700
|
+
lastFailure: null
|
|
4701
|
+
};
|
|
4702
|
+
}
|
|
4703
|
+
return {
|
|
4704
|
+
success: previous?.success ?? null,
|
|
4705
|
+
failures: (previous?.failures ?? 0) + 1,
|
|
4706
|
+
lastRunAt: now,
|
|
4707
|
+
lastFailure: report
|
|
4708
|
+
};
|
|
4709
|
+
}
|
|
4710
|
+
function emptyCache(now) {
|
|
4711
|
+
return { success: null, failures: 0, lastRunAt: now, lastFailure: null };
|
|
4712
|
+
}
|
|
4713
|
+
function identityOf(probe) {
|
|
4714
|
+
if (probe.outcome.status !== "pass") return null;
|
|
4715
|
+
const kind = probe.outcome.choice.kind;
|
|
4716
|
+
return `${kind}@${kind === "markdown" ? "" : probe.remote?.path ?? ""}`;
|
|
4717
|
+
}
|
|
4718
|
+
async function runCheck(deps, workspaceRoot) {
|
|
4719
|
+
const startedAt = deps.now();
|
|
4720
|
+
try {
|
|
4721
|
+
const collected = await collectFacts(deps, workspaceRoot);
|
|
4722
|
+
const items = buildEnvCheckItems(collected.facts);
|
|
4723
|
+
const report = {
|
|
4724
|
+
checkedAt: startedAt,
|
|
4725
|
+
backend: collected.facts.backend.status === "pass" ? collected.facts.backend.choice.kind : null,
|
|
4726
|
+
repository: collected.repository,
|
|
4205
4727
|
items,
|
|
4206
4728
|
capabilities: deps.capabilities()
|
|
4207
4729
|
};
|
|
4208
4730
|
logSummary(deps, report, startedAt);
|
|
4209
|
-
return { ok: true, result: report };
|
|
4731
|
+
return { result: { ok: true, result: report }, identity: collected.identity };
|
|
4210
4732
|
} catch {
|
|
4211
4733
|
deps.log("warn", LOG_EVENTS.envCheck, {
|
|
4212
4734
|
result: "error",
|
|
4213
4735
|
durationMs: Math.max(0, deps.now() - startedAt)
|
|
4214
4736
|
});
|
|
4215
|
-
return
|
|
4737
|
+
return {
|
|
4738
|
+
result: failure(ERROR_CODES.internal, "\u73AF\u5883\u68C0\u67E5\u65F6\u53D1\u751F\u672A\u9884\u671F\u7684\u9519\u8BEF\u3002"),
|
|
4739
|
+
identity: null
|
|
4740
|
+
};
|
|
4216
4741
|
}
|
|
4217
4742
|
}
|
|
4218
4743
|
async function collectFacts(deps, workspaceRoot) {
|
|
@@ -4240,7 +4765,8 @@ async function collectFacts(deps, workspaceRoot) {
|
|
|
4240
4765
|
skills,
|
|
4241
4766
|
setupSkill: PANEL_SKILLS.setup
|
|
4242
4767
|
},
|
|
4243
|
-
repository: decided === null || decided === "markdown" ? null : backend.remote?.path ?? null
|
|
4768
|
+
repository: decided === null || decided === "markdown" ? null : backend.remote?.path ?? null,
|
|
4769
|
+
identity: identityOf(backend)
|
|
4244
4770
|
};
|
|
4245
4771
|
}
|
|
4246
4772
|
async function probeLocalIssues(deps, workspaceRoot) {
|
|
@@ -4518,53 +5044,6 @@ function classifyTickets(tickets, names) {
|
|
|
4518
5044
|
}));
|
|
4519
5045
|
}
|
|
4520
5046
|
|
|
4521
|
-
// src/core/merge.ts
|
|
4522
|
-
function mergeTickets(base, changes, windowLimit) {
|
|
4523
|
-
const positions = /* @__PURE__ */ new Map();
|
|
4524
|
-
const merged = [];
|
|
4525
|
-
for (const ticket of base) {
|
|
4526
|
-
if (!positions.has(ticket.ref)) positions.set(ticket.ref, merged.length);
|
|
4527
|
-
merged.push(ticket);
|
|
4528
|
-
}
|
|
4529
|
-
for (const change of changes) {
|
|
4530
|
-
const at = positions.get(change.ref);
|
|
4531
|
-
if (at === void 0) {
|
|
4532
|
-
positions.set(change.ref, merged.length);
|
|
4533
|
-
merged.push(change);
|
|
4534
|
-
continue;
|
|
4535
|
-
}
|
|
4536
|
-
const existing = merged[at];
|
|
4537
|
-
if (existing !== void 0 && isNewer(change, existing)) merged[at] = change;
|
|
4538
|
-
}
|
|
4539
|
-
if (windowLimit === void 0) return merged;
|
|
4540
|
-
return trimToWindow(merged, windowLimit);
|
|
4541
|
-
}
|
|
4542
|
-
function isNewer(candidate, current) {
|
|
4543
|
-
const left = timestamp(candidate.updatedAt);
|
|
4544
|
-
const right = timestamp(current.updatedAt);
|
|
4545
|
-
if (left === void 0 || right === void 0) return candidate.updatedAt >= current.updatedAt;
|
|
4546
|
-
return left >= right;
|
|
4547
|
-
}
|
|
4548
|
-
function timestamp(value) {
|
|
4549
|
-
const parsed = Date.parse(value);
|
|
4550
|
-
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
4551
|
-
}
|
|
4552
|
-
function trimToWindow(tickets, limit) {
|
|
4553
|
-
if (tickets.length <= limit) return [...tickets];
|
|
4554
|
-
const sorted = [...tickets].sort((left, right) => {
|
|
4555
|
-
const byTime = compareTime(left.updatedAt, right.updatedAt);
|
|
4556
|
-
if (byTime !== 0) return byTime;
|
|
4557
|
-
return left.ref.localeCompare(right.ref);
|
|
4558
|
-
});
|
|
4559
|
-
return sorted.slice(0, limit);
|
|
4560
|
-
}
|
|
4561
|
-
function compareTime(left, right) {
|
|
4562
|
-
const leftTime = timestamp(left);
|
|
4563
|
-
const rightTime = timestamp(right);
|
|
4564
|
-
if (leftTime === void 0 || rightTime === void 0) return right.localeCompare(left);
|
|
4565
|
-
return rightTime - leftTime;
|
|
4566
|
-
}
|
|
4567
|
-
|
|
4568
5047
|
// src/core/skill-action.ts
|
|
4569
5048
|
function decideSkillAction(ticket, names) {
|
|
4570
5049
|
const status = classify(ticket, names);
|
|
@@ -4590,6 +5069,45 @@ function blocked(reason) {
|
|
|
4590
5069
|
return { skillAction: "none", skillActionReason: reason };
|
|
4591
5070
|
}
|
|
4592
5071
|
|
|
5072
|
+
// src/host/refresh-guard.ts
|
|
5073
|
+
function createRefreshGuard(options = {}) {
|
|
5074
|
+
const baseMs = options.baseMs ?? 5e3;
|
|
5075
|
+
const maxMs = options.maxMs ?? 3e5;
|
|
5076
|
+
const states = /* @__PURE__ */ new Map();
|
|
5077
|
+
function stateOf(workspaceRoot) {
|
|
5078
|
+
let state = states.get(workspaceRoot);
|
|
5079
|
+
if (state === void 0) {
|
|
5080
|
+
state = { failuresSinceSuccess: 0, coolingUntil: 0 };
|
|
5081
|
+
states.set(workspaceRoot, state);
|
|
5082
|
+
}
|
|
5083
|
+
return state;
|
|
5084
|
+
}
|
|
5085
|
+
return {
|
|
5086
|
+
blockedUntil(workspaceRoot, at) {
|
|
5087
|
+
const state = states.get(workspaceRoot);
|
|
5088
|
+
if (state === void 0 || state.coolingUntil <= at) return null;
|
|
5089
|
+
return state.coolingUntil;
|
|
5090
|
+
},
|
|
5091
|
+
noteSuccess(workspaceRoot) {
|
|
5092
|
+
const state = stateOf(workspaceRoot);
|
|
5093
|
+
state.failuresSinceSuccess = 0;
|
|
5094
|
+
state.coolingUntil = 0;
|
|
5095
|
+
},
|
|
5096
|
+
noteFailure(workspaceRoot, at, retryAfterMs) {
|
|
5097
|
+
const state = stateOf(workspaceRoot);
|
|
5098
|
+
state.failuresSinceSuccess += 1;
|
|
5099
|
+
const backoffMs = Math.min(baseMs * 2 ** Math.min(state.failuresSinceSuccess - 1, 16), maxMs);
|
|
5100
|
+
const waitMs = retryAfterMs !== void 0 && retryAfterMs > 0 ? Math.min(retryAfterMs, maxMs) : backoffMs;
|
|
5101
|
+
state.coolingUntil = at + waitMs;
|
|
5102
|
+
return state.coolingUntil;
|
|
5103
|
+
},
|
|
5104
|
+
view(workspaceRoot) {
|
|
5105
|
+
const state = states.get(workspaceRoot);
|
|
5106
|
+
return state === void 0 ? { failuresSinceSuccess: 0, coolingUntil: 0 } : { ...state };
|
|
5107
|
+
}
|
|
5108
|
+
};
|
|
5109
|
+
}
|
|
5110
|
+
|
|
4593
5111
|
// src/host/rpc-params.ts
|
|
4594
5112
|
function readWorkspaceRoot2(params) {
|
|
4595
5113
|
if (typeof params !== "object" || params === null) return void 0;
|
|
@@ -4613,65 +5131,64 @@ function readTicketParams(params) {
|
|
|
4613
5131
|
|
|
4614
5132
|
// src/host/snapshot-error.ts
|
|
4615
5133
|
function mapSnapshotError(error) {
|
|
4616
|
-
if (error instanceof TrackerError)
|
|
5134
|
+
if (error instanceof TrackerError) {
|
|
5135
|
+
return {
|
|
5136
|
+
code: error.code,
|
|
5137
|
+
message: error.message,
|
|
5138
|
+
...error.retryAfterMs !== void 0 ? { retryAfterMs: error.retryAfterMs } : {}
|
|
5139
|
+
};
|
|
5140
|
+
}
|
|
4617
5141
|
return { code: ERROR_CODES.internal, message: "\u8BFB\u53D6\u5DE5\u5355\u65F6\u53D1\u751F\u672A\u9884\u671F\u7684\u9519\u8BEF\u3002" };
|
|
4618
5142
|
}
|
|
4619
5143
|
function snapshotFailure(code, message) {
|
|
4620
5144
|
return { ok: false, error: { code, message } };
|
|
4621
5145
|
}
|
|
4622
5146
|
|
|
4623
|
-
// src/
|
|
4624
|
-
function
|
|
4625
|
-
const
|
|
4626
|
-
const
|
|
4627
|
-
const
|
|
4628
|
-
|
|
4629
|
-
|
|
4630
|
-
function mergePulled(current, raw) {
|
|
4631
|
-
if (current === void 0) return raw;
|
|
4632
|
-
if (current.raw.repository !== raw.repository) return raw;
|
|
4633
|
-
const existingWindow = current.raw.windowLimit ?? current.raw.tickets.length;
|
|
4634
|
-
const pulledWindow = raw.windowLimit ?? raw.tickets.length;
|
|
4635
|
-
const window = Math.max(existingWindow, pulledWindow);
|
|
4636
|
-
const shrunk = raw.tickets.length < current.raw.tickets.length;
|
|
4637
|
-
if (!shrunk && window <= pulledWindow) return raw;
|
|
4638
|
-
const tickets = mergeTickets(current.raw.tickets, raw.tickets, window);
|
|
4639
|
-
return { ...raw, loaded: tickets.length, tickets, windowLimit: window };
|
|
4640
|
-
}
|
|
4641
|
-
async function loadMore(host, workspaceRoot) {
|
|
4642
|
-
const initial = await host.getSnapshot(workspaceRoot);
|
|
4643
|
-
if (!initial.ok) return initial;
|
|
4644
|
-
const entry = host.cache.get(workspaceRoot);
|
|
4645
|
-
if (entry === void 0) return initial;
|
|
4646
|
-
const windowLimit = entry.raw.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
4647
|
-
if (entry.raw.tickets.length >= entry.raw.total || windowLimit >= SNAPSHOT_MAX_WINDOW) {
|
|
4648
|
-
return { ok: true, result: entry.snapshot };
|
|
5147
|
+
// src/core/merge.ts
|
|
5148
|
+
function mergeTickets(base, changes, windowLimit) {
|
|
5149
|
+
const positions = /* @__PURE__ */ new Map();
|
|
5150
|
+
const merged = [];
|
|
5151
|
+
for (const ticket of base) {
|
|
5152
|
+
if (!positions.has(ticket.ref)) positions.set(ticket.ref, merged.length);
|
|
5153
|
+
merged.push(ticket);
|
|
4649
5154
|
}
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
const
|
|
4658
|
-
|
|
4659
|
-
workspaceRoot,
|
|
4660
|
-
resolved,
|
|
4661
|
-
{ ...raw, viewer, windowLimit: nextWindow },
|
|
4662
|
-
startedAt
|
|
4663
|
-
);
|
|
4664
|
-
} catch (error) {
|
|
4665
|
-
const mapped = mapSnapshotError(error);
|
|
4666
|
-
host.log("warn", LOG_EVENTS.snapshot, {
|
|
4667
|
-
result: "error",
|
|
4668
|
-
...kind === void 0 ? {} : { backend: kind },
|
|
4669
|
-
stage: SNAPSHOT_STAGES.list,
|
|
4670
|
-
code: mapped.code,
|
|
4671
|
-
durationMs: Math.max(0, host.now() - startedAt)
|
|
4672
|
-
});
|
|
4673
|
-
return snapshotFailure(mapped.code, mapped.message);
|
|
5155
|
+
for (const change of changes) {
|
|
5156
|
+
const at = positions.get(change.ref);
|
|
5157
|
+
if (at === void 0) {
|
|
5158
|
+
positions.set(change.ref, merged.length);
|
|
5159
|
+
merged.push(change);
|
|
5160
|
+
continue;
|
|
5161
|
+
}
|
|
5162
|
+
const existing = merged[at];
|
|
5163
|
+
if (existing !== void 0 && isNewer(change, existing)) merged[at] = change;
|
|
4674
5164
|
}
|
|
5165
|
+
if (windowLimit === void 0) return merged;
|
|
5166
|
+
return trimToWindow(merged, windowLimit);
|
|
5167
|
+
}
|
|
5168
|
+
function isNewer(candidate, current) {
|
|
5169
|
+
const left = timestamp(candidate.updatedAt);
|
|
5170
|
+
const right = timestamp(current.updatedAt);
|
|
5171
|
+
if (left === void 0 || right === void 0) return candidate.updatedAt >= current.updatedAt;
|
|
5172
|
+
return left >= right;
|
|
5173
|
+
}
|
|
5174
|
+
function timestamp(value) {
|
|
5175
|
+
const parsed = Date.parse(value);
|
|
5176
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
5177
|
+
}
|
|
5178
|
+
function trimToWindow(tickets, limit) {
|
|
5179
|
+
if (tickets.length <= limit) return [...tickets];
|
|
5180
|
+
const sorted = [...tickets].sort((left, right) => {
|
|
5181
|
+
const byTime = compareTime(left.updatedAt, right.updatedAt);
|
|
5182
|
+
if (byTime !== 0) return byTime;
|
|
5183
|
+
return left.ref.localeCompare(right.ref);
|
|
5184
|
+
});
|
|
5185
|
+
return sorted.slice(0, limit);
|
|
5186
|
+
}
|
|
5187
|
+
function compareTime(left, right) {
|
|
5188
|
+
const leftTime = timestamp(left);
|
|
5189
|
+
const rightTime = timestamp(right);
|
|
5190
|
+
if (leftTime === void 0 || rightTime === void 0) return right.localeCompare(left);
|
|
5191
|
+
return rightTime - leftTime;
|
|
4675
5192
|
}
|
|
4676
5193
|
|
|
4677
5194
|
// src/core/metrics.ts
|
|
@@ -4995,11 +5512,13 @@ async function removeStored(deps, workspaceRoot) {
|
|
|
4995
5512
|
} catch {
|
|
4996
5513
|
}
|
|
4997
5514
|
}
|
|
4998
|
-
async function writeStored(deps, workspaceRoot, resolved, raw) {
|
|
5515
|
+
async function writeStored(deps, workspaceRoot, resolved, raw, metrics) {
|
|
4999
5516
|
const store = deps.store;
|
|
5000
|
-
if (store === void 0) return;
|
|
5517
|
+
if (store === void 0) return null;
|
|
5518
|
+
const coversAll = raw.tickets.length >= raw.total;
|
|
5001
5519
|
const entry = {
|
|
5002
5520
|
version: SNAPSHOT_STORE_VERSION,
|
|
5521
|
+
libraryVersion: 0,
|
|
5003
5522
|
workspaceRoot,
|
|
5004
5523
|
backend: resolved.kind,
|
|
5005
5524
|
repository: raw.repository,
|
|
@@ -5009,12 +5528,13 @@ async function writeStored(deps, workspaceRoot, resolved, raw) {
|
|
|
5009
5528
|
fetchedAt: raw.fetchedAt,
|
|
5010
5529
|
loaded: raw.tickets.length,
|
|
5011
5530
|
total: raw.total,
|
|
5012
|
-
...raw.windowLimit === void 0 ? {} : { windowLimit: raw.windowLimit },
|
|
5531
|
+
...coversAll || raw.windowLimit === void 0 ? {} : { windowLimit: raw.windowLimit },
|
|
5013
5532
|
hierarchy: raw.hierarchy,
|
|
5533
|
+
metrics,
|
|
5014
5534
|
tickets: raw.tickets
|
|
5015
5535
|
};
|
|
5016
5536
|
try {
|
|
5017
|
-
await store.write(entry);
|
|
5537
|
+
return await store.write(entry);
|
|
5018
5538
|
} catch (error) {
|
|
5019
5539
|
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5020
5540
|
result: "error",
|
|
@@ -5023,9 +5543,10 @@ async function writeStored(deps, workspaceRoot, resolved, raw) {
|
|
|
5023
5543
|
code: ERROR_CODES.internal,
|
|
5024
5544
|
...error instanceof Error ? { reason: error.message } : {}
|
|
5025
5545
|
});
|
|
5546
|
+
return null;
|
|
5026
5547
|
}
|
|
5027
5548
|
}
|
|
5028
|
-
function buildSnapshot(kind, repositoryHost, raw, tickets, blockersFinalized, metrics, source, stale, refreshError) {
|
|
5549
|
+
function buildSnapshot(kind, repositoryHost, raw, tickets, blockersFinalized, metrics, source, stale, refreshError, libraryVersion) {
|
|
5029
5550
|
return {
|
|
5030
5551
|
backend: kind,
|
|
5031
5552
|
repository: raw.repository,
|
|
@@ -5036,37 +5557,40 @@ function buildSnapshot(kind, repositoryHost, raw, tickets, blockersFinalized, me
|
|
|
5036
5557
|
total: raw.total,
|
|
5037
5558
|
windowLimit: raw.windowLimit ?? tickets.length,
|
|
5038
5559
|
hierarchy: raw.hierarchy,
|
|
5560
|
+
libraryVersion,
|
|
5039
5561
|
blockersFinalized,
|
|
5040
5562
|
metrics,
|
|
5041
5563
|
source,
|
|
5042
5564
|
stale,
|
|
5043
5565
|
refreshError,
|
|
5044
|
-
tickets: tickets.map(
|
|
5045
|
-
|
|
5046
|
-
|
|
5047
|
-
|
|
5048
|
-
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
|
|
5060
|
-
|
|
5061
|
-
|
|
5062
|
-
|
|
5063
|
-
}
|
|
5566
|
+
tickets: tickets.map(toTicketSnapshot)
|
|
5567
|
+
};
|
|
5568
|
+
}
|
|
5569
|
+
function toTicketSnapshot(ticket) {
|
|
5570
|
+
return {
|
|
5571
|
+
ref: ticket.ref,
|
|
5572
|
+
number: ticket.number,
|
|
5573
|
+
title: ticket.title,
|
|
5574
|
+
state: ticket.state,
|
|
5575
|
+
category: ticket.category,
|
|
5576
|
+
status: ticket.status,
|
|
5577
|
+
labels: ticket.labels,
|
|
5578
|
+
author: ticket.author,
|
|
5579
|
+
assignees: ticket.assignees,
|
|
5580
|
+
skillAction: ticket.skillAction,
|
|
5581
|
+
skillActionReason: ticket.skillActionReason,
|
|
5582
|
+
createdAt: ticket.createdAt,
|
|
5583
|
+
updatedAt: ticket.updatedAt,
|
|
5584
|
+
openBlockers: ticket.openBlockers,
|
|
5585
|
+
...ticket.closedAt === void 0 ? {} : { closedAt: ticket.closedAt },
|
|
5586
|
+
...ticket.url === void 0 ? {} : { url: ticket.url },
|
|
5587
|
+
...ticket.blockedBy === void 0 ? {} : { blockedBy: ticket.blockedBy },
|
|
5588
|
+
...ticket.parent === void 0 ? {} : { parent: ticket.parent }
|
|
5064
5589
|
};
|
|
5065
5590
|
}
|
|
5066
5591
|
function buildStoredSnapshot(deps, stored, state) {
|
|
5067
5592
|
const raw = rawOf(stored);
|
|
5068
5593
|
const tickets = withSkillActions(classifyTickets(raw.tickets, deps.labels), deps.labels);
|
|
5069
|
-
const metrics = metricsOf(deps, stored.backend, raw, tickets, false);
|
|
5070
5594
|
return {
|
|
5071
5595
|
backend: stored.backend,
|
|
5072
5596
|
repository: raw.repository,
|
|
@@ -5077,31 +5601,13 @@ function buildStoredSnapshot(deps, stored, state) {
|
|
|
5077
5601
|
total: raw.total,
|
|
5078
5602
|
windowLimit: raw.windowLimit ?? SNAPSHOT_PAGE_SIZE,
|
|
5079
5603
|
hierarchy: stored.hierarchy,
|
|
5604
|
+
libraryVersion: stored.libraryVersion,
|
|
5080
5605
|
blockersFinalized: false,
|
|
5081
|
-
metrics,
|
|
5606
|
+
metrics: stored.metrics,
|
|
5082
5607
|
source: "cache",
|
|
5083
5608
|
stale: state.stale,
|
|
5084
5609
|
refreshError: state.refreshError,
|
|
5085
|
-
tickets: tickets.map(
|
|
5086
|
-
ref: ticket.ref,
|
|
5087
|
-
number: ticket.number,
|
|
5088
|
-
title: ticket.title,
|
|
5089
|
-
state: ticket.state,
|
|
5090
|
-
category: ticket.category,
|
|
5091
|
-
status: ticket.status,
|
|
5092
|
-
labels: ticket.labels,
|
|
5093
|
-
author: ticket.author,
|
|
5094
|
-
assignees: ticket.assignees,
|
|
5095
|
-
skillAction: ticket.skillAction,
|
|
5096
|
-
skillActionReason: ticket.skillActionReason,
|
|
5097
|
-
createdAt: ticket.createdAt,
|
|
5098
|
-
updatedAt: ticket.updatedAt,
|
|
5099
|
-
openBlockers: ticket.openBlockers,
|
|
5100
|
-
...ticket.closedAt === void 0 ? {} : { closedAt: ticket.closedAt },
|
|
5101
|
-
...ticket.url === void 0 ? {} : { url: ticket.url },
|
|
5102
|
-
...ticket.blockedBy === void 0 ? {} : { blockedBy: ticket.blockedBy },
|
|
5103
|
-
...ticket.parent === void 0 ? {} : { parent: ticket.parent }
|
|
5104
|
-
}))
|
|
5610
|
+
tickets: tickets.map(toTicketSnapshot)
|
|
5105
5611
|
};
|
|
5106
5612
|
}
|
|
5107
5613
|
function storedEntry(stored, resolved, snapshot) {
|
|
@@ -5189,11 +5695,297 @@ function latestWrite(entries) {
|
|
|
5189
5695
|
return latest;
|
|
5190
5696
|
}
|
|
5191
5697
|
|
|
5192
|
-
// src/host/snapshot-
|
|
5698
|
+
// src/host/snapshot-window.ts
|
|
5699
|
+
function windowFor(current, base) {
|
|
5700
|
+
const cached = current?.raw.windowLimit ?? 0;
|
|
5701
|
+
const stored = base?.windowLimit ?? 0;
|
|
5702
|
+
const loaded = current?.raw.tickets.length ?? 0;
|
|
5703
|
+
return Math.min(SNAPSHOT_MAX_WINDOW, Math.max(cached, stored, loaded, SNAPSHOT_PAGE_SIZE));
|
|
5704
|
+
}
|
|
5705
|
+
function mergePulled(current, raw) {
|
|
5706
|
+
if (current === void 0) return raw;
|
|
5707
|
+
if (current.raw.repository !== raw.repository) return raw;
|
|
5708
|
+
const existingWindow = current.raw.windowLimit ?? current.raw.tickets.length;
|
|
5709
|
+
const pulledWindow = raw.windowLimit ?? raw.tickets.length;
|
|
5710
|
+
const window = Math.max(existingWindow, pulledWindow);
|
|
5711
|
+
const shrunk = raw.tickets.length < current.raw.tickets.length;
|
|
5712
|
+
if (!shrunk && window <= pulledWindow) return raw;
|
|
5713
|
+
const tickets = mergeTickets(current.raw.tickets, raw.tickets, window);
|
|
5714
|
+
return { ...raw, loaded: tickets.length, tickets, windowLimit: window };
|
|
5715
|
+
}
|
|
5716
|
+
async function loadMore(host, workspaceRoot) {
|
|
5717
|
+
const initial = await host.getSnapshot(workspaceRoot);
|
|
5718
|
+
if (!initial.ok) return initial;
|
|
5719
|
+
const entry = host.cache.get(workspaceRoot);
|
|
5720
|
+
if (entry === void 0) return initial;
|
|
5721
|
+
const windowLimit = entry.raw.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
5722
|
+
if (entry.raw.tickets.length >= entry.raw.total || windowLimit >= SNAPSHOT_MAX_WINDOW) {
|
|
5723
|
+
return { ok: true, result: entry.snapshot };
|
|
5724
|
+
}
|
|
5725
|
+
const nextWindow = Math.min(windowLimit + SNAPSHOT_PAGE_SIZE, SNAPSHOT_MAX_WINDOW);
|
|
5726
|
+
const startedAt = host.now();
|
|
5727
|
+
let kind;
|
|
5728
|
+
try {
|
|
5729
|
+
const resolved = await host.resolve(workspaceRoot);
|
|
5730
|
+
kind = resolved.kind;
|
|
5731
|
+
const raw = await resolved.tracker.fetchSnapshot(workspaceRoot, nextWindow);
|
|
5732
|
+
const viewer = raw.viewer ?? (entry.raw.repository === raw.repository ? entry.raw.viewer : null);
|
|
5733
|
+
return await host.publish(
|
|
5734
|
+
workspaceRoot,
|
|
5735
|
+
resolved,
|
|
5736
|
+
{ ...raw, viewer, windowLimit: nextWindow },
|
|
5737
|
+
startedAt
|
|
5738
|
+
);
|
|
5739
|
+
} catch (error) {
|
|
5740
|
+
const mapped = mapSnapshotError(error);
|
|
5741
|
+
host.log("warn", LOG_EVENTS.snapshot, {
|
|
5742
|
+
result: "error",
|
|
5743
|
+
...kind === void 0 ? {} : { backend: kind },
|
|
5744
|
+
stage: SNAPSHOT_STAGES.list,
|
|
5745
|
+
code: mapped.code,
|
|
5746
|
+
durationMs: Math.max(0, host.now() - startedAt)
|
|
5747
|
+
});
|
|
5748
|
+
return snapshotFailure(mapped.code, mapped.message);
|
|
5749
|
+
}
|
|
5750
|
+
}
|
|
5751
|
+
|
|
5752
|
+
// src/host/snapshot-refresh.ts
|
|
5193
5753
|
var INCREMENTAL_OVERLAP_MS = 6e4;
|
|
5754
|
+
async function refreshSnapshot(host, workspaceRoot, full, ifVersion) {
|
|
5755
|
+
const startedAt = host.now();
|
|
5756
|
+
if (!full) {
|
|
5757
|
+
const cooling = coolingReject(host, workspaceRoot, startedAt);
|
|
5758
|
+
if (cooling !== null) return cooling;
|
|
5759
|
+
}
|
|
5760
|
+
const failWith = (mapped) => {
|
|
5761
|
+
host.guard.noteFailure(workspaceRoot, startedAt, mapped.retryAfterMs);
|
|
5762
|
+
return keepOldSnapshot(host, workspaceRoot, mapped, startedAt);
|
|
5763
|
+
};
|
|
5764
|
+
let resolved;
|
|
5765
|
+
try {
|
|
5766
|
+
resolved = await host.resolve(workspaceRoot);
|
|
5767
|
+
} catch (error) {
|
|
5768
|
+
return failWith(mapSnapshotError(error));
|
|
5769
|
+
}
|
|
5770
|
+
const stored = await readStored(host, workspaceRoot);
|
|
5771
|
+
const base = stored?.repositoryKey === repositoryKey(resolved.kind, resolved.address) ? stored : void 0;
|
|
5772
|
+
const tracker = resolved.tracker;
|
|
5773
|
+
let raw;
|
|
5774
|
+
let mode = "full";
|
|
5775
|
+
if (base !== void 0 && !full && tracker.fetchChanges !== void 0 && // 对账以「全量覆盖」为前提:窗口截断的后端看不到窗口外的删除。
|
|
5776
|
+
base.windowLimit === void 0) {
|
|
5777
|
+
const outcome = await pullIncremental(host, workspaceRoot, resolved, base, startedAt);
|
|
5778
|
+
if (outcome.kind === "failure") {
|
|
5779
|
+
return failWith(outcome.error);
|
|
5780
|
+
}
|
|
5781
|
+
if (outcome.kind === "merged") {
|
|
5782
|
+
raw = outcome.raw;
|
|
5783
|
+
mode = "incremental";
|
|
5784
|
+
}
|
|
5785
|
+
}
|
|
5786
|
+
if (raw === void 0) {
|
|
5787
|
+
try {
|
|
5788
|
+
const fetched = await tracker.fetchSnapshot(
|
|
5789
|
+
workspaceRoot,
|
|
5790
|
+
windowFor(host.cache.get(workspaceRoot), base)
|
|
5791
|
+
);
|
|
5792
|
+
const current = host.cache.get(workspaceRoot);
|
|
5793
|
+
const hostName = resolved.kind === "markdown" ? null : resolved.address?.host ?? null;
|
|
5794
|
+
const sameRepository = current?.kind === resolved.kind && current.repositoryHost === hostName && current.raw.repository === fetched.repository;
|
|
5795
|
+
const previousViewer = (sameRepository ? current.raw.viewer : null) ?? base?.viewer ?? null;
|
|
5796
|
+
raw = fetched.viewer === null ? { ...fetched, viewer: previousViewer } : fetched;
|
|
5797
|
+
} catch (error) {
|
|
5798
|
+
return failWith(mapSnapshotError(error));
|
|
5799
|
+
}
|
|
5800
|
+
}
|
|
5801
|
+
if (base !== void 0 && !ticketsDiffer(base, raw)) {
|
|
5802
|
+
return respondUnchanged(host, workspaceRoot, resolved, base, raw, ifVersion, startedAt);
|
|
5803
|
+
}
|
|
5804
|
+
const published = await host.publish(workspaceRoot, resolved, raw, startedAt, mode);
|
|
5805
|
+
if (!published.ok) {
|
|
5806
|
+
host.guard.noteFailure(workspaceRoot, host.now());
|
|
5807
|
+
return published;
|
|
5808
|
+
}
|
|
5809
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5810
|
+
return shapeDelta(host, workspaceRoot, published.result, ifVersion);
|
|
5811
|
+
}
|
|
5812
|
+
function coolingReject(host, workspaceRoot, startedAt) {
|
|
5813
|
+
const coolingUntil = host.guard.blockedUntil(workspaceRoot, startedAt);
|
|
5814
|
+
if (coolingUntil === null) return null;
|
|
5815
|
+
return keepOldSnapshot(
|
|
5816
|
+
host,
|
|
5817
|
+
workspaceRoot,
|
|
5818
|
+
{
|
|
5819
|
+
code: ERROR_CODES.fetchFailed,
|
|
5820
|
+
message: "\u5237\u65B0\u5DF2\u6682\u505C\uFF1A\u8FDE\u7EED\u5931\u8D25\u540E\u6B63\u5728\u9000\u907F\uFF0C\u7A0D\u540E\u4F1A\u81EA\u52A8\u6062\u590D\uFF0C\u663E\u793A\u7684\u662F\u672C\u5730\u7F13\u5B58\u3002"
|
|
5821
|
+
},
|
|
5822
|
+
startedAt
|
|
5823
|
+
);
|
|
5824
|
+
}
|
|
5825
|
+
async function pullIncremental(host, workspaceRoot, resolved, base, startedAt) {
|
|
5826
|
+
const tracker = resolved.tracker;
|
|
5827
|
+
if (tracker.fetchChanges === void 0) return { kind: "fallback" };
|
|
5828
|
+
let changes;
|
|
5829
|
+
try {
|
|
5830
|
+
changes = await tracker.fetchChanges(workspaceRoot, base.fetchedAt - INCREMENTAL_OVERLAP_MS);
|
|
5831
|
+
} catch (error) {
|
|
5832
|
+
const mapped = mapSnapshotError(error);
|
|
5833
|
+
return { kind: "failure", error: mapped };
|
|
5834
|
+
}
|
|
5835
|
+
if (changes.truncated) {
|
|
5836
|
+
logIncrementalFallback(host, resolved.kind, "truncated", startedAt);
|
|
5837
|
+
return { kind: "fallback" };
|
|
5838
|
+
}
|
|
5839
|
+
const total = changes.total;
|
|
5840
|
+
if (!Number.isInteger(total) || total < 0) {
|
|
5841
|
+
logIncrementalFallback(host, resolved.kind, "invalid-total", startedAt);
|
|
5842
|
+
return { kind: "fallback" };
|
|
5843
|
+
}
|
|
5844
|
+
if (changes.hierarchy !== base.hierarchy) {
|
|
5845
|
+
logIncrementalFallback(host, resolved.kind, "hierarchy", startedAt);
|
|
5846
|
+
return { kind: "fallback" };
|
|
5847
|
+
}
|
|
5848
|
+
const merged = mergeTickets(base.tickets, changes.tickets);
|
|
5849
|
+
if (merged.length > total) {
|
|
5850
|
+
logIncrementalFallback(host, resolved.kind, "drift", startedAt);
|
|
5851
|
+
return { kind: "fallback" };
|
|
5852
|
+
}
|
|
5853
|
+
return {
|
|
5854
|
+
kind: "merged",
|
|
5855
|
+
raw: {
|
|
5856
|
+
repository: base.repository,
|
|
5857
|
+
fetchedAt: host.now(),
|
|
5858
|
+
loaded: merged.length,
|
|
5859
|
+
total,
|
|
5860
|
+
hierarchy: base.hierarchy,
|
|
5861
|
+
tickets: merged,
|
|
5862
|
+
// 后端这次查了身份就用新值;没查(省略)时沿用基准数据里的身份。
|
|
5863
|
+
viewer: changes.viewer === void 0 ? base.viewer : changes.viewer,
|
|
5864
|
+
windowLimit: Math.max(base.tickets.length, SNAPSHOT_PAGE_SIZE)
|
|
5865
|
+
}
|
|
5866
|
+
};
|
|
5867
|
+
}
|
|
5868
|
+
function logIncrementalFallback(deps, backend, reason, startedAt) {
|
|
5869
|
+
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5870
|
+
result: "fallback",
|
|
5871
|
+
backend,
|
|
5872
|
+
stage: SNAPSHOT_STAGES.list,
|
|
5873
|
+
reason,
|
|
5874
|
+
durationMs: Math.max(0, deps.now() - startedAt)
|
|
5875
|
+
});
|
|
5876
|
+
}
|
|
5877
|
+
function ticketsDiffer(base, raw) {
|
|
5878
|
+
if (base.total !== raw.total || base.viewer !== raw.viewer) return true;
|
|
5879
|
+
return JSON.stringify(base.tickets) !== JSON.stringify(raw.tickets);
|
|
5880
|
+
}
|
|
5881
|
+
async function respondUnchanged(host, workspaceRoot, resolved, base, raw, ifVersion, startedAt) {
|
|
5882
|
+
if (ifVersion !== void 0 && ifVersion === base.libraryVersion) {
|
|
5883
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5884
|
+
return {
|
|
5885
|
+
ok: true,
|
|
5886
|
+
result: { kind: "not-modified", version: base.libraryVersion, checkedAt: host.now() }
|
|
5887
|
+
};
|
|
5888
|
+
}
|
|
5889
|
+
if (ifVersion !== void 0 && ifVersion < base.libraryVersion) {
|
|
5890
|
+
const changes = await host.store?.changesSince(workspaceRoot, ifVersion);
|
|
5891
|
+
if (changes !== void 0) {
|
|
5892
|
+
const delta = buildDelta(host, base.libraryVersion, raw.fetchedAt, changes, base);
|
|
5893
|
+
if (delta !== void 0) {
|
|
5894
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5895
|
+
return { ok: true, result: delta };
|
|
5896
|
+
}
|
|
5897
|
+
}
|
|
5898
|
+
}
|
|
5899
|
+
const published = await host.publish(workspaceRoot, resolved, raw, startedAt, "full", {
|
|
5900
|
+
skipWrite: true,
|
|
5901
|
+
versionFallback: base.libraryVersion
|
|
5902
|
+
});
|
|
5903
|
+
if (!published.ok) {
|
|
5904
|
+
host.guard.noteFailure(workspaceRoot, host.now());
|
|
5905
|
+
return published;
|
|
5906
|
+
}
|
|
5907
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5908
|
+
return { ok: true, result: { kind: "full", snapshot: published.result } };
|
|
5909
|
+
}
|
|
5910
|
+
async function shapeDelta(host, workspaceRoot, snapshot, ifVersion) {
|
|
5911
|
+
const full = { kind: "full", snapshot };
|
|
5912
|
+
if (ifVersion === void 0 || snapshot.libraryVersion === null) return { ok: true, result: full };
|
|
5913
|
+
if (ifVersion === snapshot.libraryVersion) return { ok: true, result: full };
|
|
5914
|
+
const changes = await host.store?.changesSince(workspaceRoot, ifVersion);
|
|
5915
|
+
if (changes === void 0) return { ok: true, result: full };
|
|
5916
|
+
const delta = buildDelta(
|
|
5917
|
+
host,
|
|
5918
|
+
snapshot.libraryVersion,
|
|
5919
|
+
snapshot.fetchedAt,
|
|
5920
|
+
changes,
|
|
5921
|
+
void 0,
|
|
5922
|
+
snapshot
|
|
5923
|
+
);
|
|
5924
|
+
if (delta === void 0) return { ok: true, result: full };
|
|
5925
|
+
return { ok: true, result: delta };
|
|
5926
|
+
}
|
|
5927
|
+
function buildDelta(host, version, fetchedAt, changes, base, snapshot) {
|
|
5928
|
+
if (changes.tickets.length + changes.removed.length > SNAPSHOT_DELTA_MAX_ROWS) return void 0;
|
|
5929
|
+
const metrics = snapshot?.metrics ?? base?.metrics;
|
|
5930
|
+
const viewer = snapshot?.viewer ?? base?.viewer ?? null;
|
|
5931
|
+
const total = snapshot?.total ?? base?.total ?? 0;
|
|
5932
|
+
const loaded = snapshot?.loaded ?? base?.tickets.length ?? 0;
|
|
5933
|
+
const windowLimit = snapshot?.windowLimit ?? base?.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
5934
|
+
if (metrics === void 0) return void 0;
|
|
5935
|
+
const rows = withSkillActions(classifyTickets(changes.tickets, host.labels), host.labels).map(
|
|
5936
|
+
toTicketSnapshot
|
|
5937
|
+
);
|
|
5938
|
+
const anyOpen = base?.tickets.some((ticket) => ticket.state === "open") ?? false;
|
|
5939
|
+
const blockersFinalized = snapshot?.blockersFinalized ?? !anyOpen;
|
|
5940
|
+
return {
|
|
5941
|
+
kind: "changes",
|
|
5942
|
+
version,
|
|
5943
|
+
fetchedAt,
|
|
5944
|
+
tickets: rows,
|
|
5945
|
+
removed: changes.removed,
|
|
5946
|
+
metrics,
|
|
5947
|
+
viewer,
|
|
5948
|
+
total,
|
|
5949
|
+
loaded,
|
|
5950
|
+
windowLimit,
|
|
5951
|
+
blockersFinalized
|
|
5952
|
+
};
|
|
5953
|
+
}
|
|
5954
|
+
async function keepOldSnapshot(host, workspaceRoot, mapped, startedAt) {
|
|
5955
|
+
const refreshError = { code: mapped.code, message: mapped.message };
|
|
5956
|
+
const cached = host.cache.get(workspaceRoot);
|
|
5957
|
+
if (cached !== void 0) {
|
|
5958
|
+
const snapshot2 = { ...cached.snapshot, stale: true, refreshError };
|
|
5959
|
+
cached.snapshot = snapshot2;
|
|
5960
|
+
host.log("warn", LOG_EVENTS.snapshot, {
|
|
5961
|
+
result: "error",
|
|
5962
|
+
backend: cached.kind,
|
|
5963
|
+
stage: SNAPSHOT_STAGES.list,
|
|
5964
|
+
code: mapped.code,
|
|
5965
|
+
durationMs: Math.max(0, host.now() - startedAt)
|
|
5966
|
+
});
|
|
5967
|
+
return { ok: true, result: { kind: "full", snapshot: snapshot2 } };
|
|
5968
|
+
}
|
|
5969
|
+
const stored = await readStored(host, workspaceRoot);
|
|
5970
|
+
if (stored === void 0) return { ok: false, error: refreshError };
|
|
5971
|
+
const snapshot = buildStoredSnapshot(host, stored, { stale: true, refreshError });
|
|
5972
|
+
host.cache.set(workspaceRoot, storedEntry(stored, null, snapshot));
|
|
5973
|
+
host.log("warn", LOG_EVENTS.snapshot, {
|
|
5974
|
+
result: "error",
|
|
5975
|
+
backend: stored.backend,
|
|
5976
|
+
stage: SNAPSHOT_STAGES.list,
|
|
5977
|
+
code: mapped.code,
|
|
5978
|
+
durationMs: Math.max(0, host.now() - startedAt)
|
|
5979
|
+
});
|
|
5980
|
+
return { ok: true, result: { kind: "full", snapshot } };
|
|
5981
|
+
}
|
|
5982
|
+
|
|
5983
|
+
// src/host/snapshot-service.ts
|
|
5194
5984
|
function createSnapshotService(deps) {
|
|
5195
5985
|
const cache = /* @__PURE__ */ new Map();
|
|
5196
5986
|
const loading = /* @__PURE__ */ new Map();
|
|
5987
|
+
const refreshing = /* @__PURE__ */ new Map();
|
|
5988
|
+
const guard = createRefreshGuard();
|
|
5197
5989
|
const more = {
|
|
5198
5990
|
resolve: deps.resolve,
|
|
5199
5991
|
now: deps.now,
|
|
@@ -5202,6 +5994,16 @@ function createSnapshotService(deps) {
|
|
|
5202
5994
|
getSnapshot: (workspaceRoot) => getSnapshot(deps, cache, loading, workspaceRoot),
|
|
5203
5995
|
publish: (workspaceRoot, resolved, raw, startedAt) => publish(deps, cache, workspaceRoot, resolved, raw, startedAt, "more")
|
|
5204
5996
|
};
|
|
5997
|
+
const refreshHost = {
|
|
5998
|
+
resolve: deps.resolve,
|
|
5999
|
+
now: deps.now,
|
|
6000
|
+
labels: deps.labels,
|
|
6001
|
+
log: deps.log,
|
|
6002
|
+
...deps.store === void 0 ? {} : { store: deps.store },
|
|
6003
|
+
cache,
|
|
6004
|
+
guard,
|
|
6005
|
+
publish: (workspaceRoot, resolved, raw, startedAt, mode, options) => publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mode, options)
|
|
6006
|
+
};
|
|
5205
6007
|
return {
|
|
5206
6008
|
handle(method, params) {
|
|
5207
6009
|
if (!isSnapshotMethod(method)) {
|
|
@@ -5219,10 +6021,29 @@ function createSnapshotService(deps) {
|
|
|
5219
6021
|
if (method === RPC_METHODS.snapshotResolveBlockers) {
|
|
5220
6022
|
return resolveBlockers(deps, cache, loading, workspaceRoot);
|
|
5221
6023
|
}
|
|
5222
|
-
|
|
5223
|
-
|
|
6024
|
+
return loadMore(more, workspaceRoot);
|
|
6025
|
+
},
|
|
6026
|
+
refresh(params) {
|
|
6027
|
+
const workspaceRoot = readWorkspaceRoot2(params);
|
|
6028
|
+
if (workspaceRoot === void 0) {
|
|
6029
|
+
return Promise.resolve(
|
|
6030
|
+
snapshotFailure(ERROR_CODES.invalidParams, "\u7F3A\u5C11\u975E\u7A7A\u7684 workspaceRoot \u53C2\u6570\u3002")
|
|
6031
|
+
);
|
|
5224
6032
|
}
|
|
5225
|
-
|
|
6033
|
+
const inflight = refreshing.get(workspaceRoot);
|
|
6034
|
+
if (inflight !== void 0) return inflight;
|
|
6035
|
+
const task = refreshSnapshot(
|
|
6036
|
+
refreshHost,
|
|
6037
|
+
workspaceRoot,
|
|
6038
|
+
readFullFlag(params),
|
|
6039
|
+
readIfVersion(params)
|
|
6040
|
+
);
|
|
6041
|
+
refreshing.set(workspaceRoot, task);
|
|
6042
|
+
const clear = () => {
|
|
6043
|
+
if (refreshing.get(workspaceRoot) === task) refreshing.delete(workspaceRoot);
|
|
6044
|
+
};
|
|
6045
|
+
void task.then(clear, clear);
|
|
6046
|
+
return task;
|
|
5226
6047
|
},
|
|
5227
6048
|
cache(params) {
|
|
5228
6049
|
if (readWorkspaceRoot2(params) === void 0) {
|
|
@@ -5236,11 +6057,17 @@ function createSnapshotService(deps) {
|
|
|
5236
6057
|
};
|
|
5237
6058
|
}
|
|
5238
6059
|
function isSnapshotMethod(method) {
|
|
5239
|
-
return method === RPC_METHODS.snapshotGet || method === RPC_METHODS.
|
|
6060
|
+
return method === RPC_METHODS.snapshotGet || method === RPC_METHODS.snapshotResolveBlockers || method === RPC_METHODS.snapshotMore;
|
|
5240
6061
|
}
|
|
5241
6062
|
function readFullFlag(params) {
|
|
5242
6063
|
return isRecord(params) && params.full === true;
|
|
5243
6064
|
}
|
|
6065
|
+
function readIfVersion(params) {
|
|
6066
|
+
if (!isRecord(params)) return void 0;
|
|
6067
|
+
const value = params.ifVersion;
|
|
6068
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) return void 0;
|
|
6069
|
+
return value;
|
|
6070
|
+
}
|
|
5244
6071
|
function getSnapshot(deps, cache, loading, workspaceRoot) {
|
|
5245
6072
|
const cached = cache.get(workspaceRoot);
|
|
5246
6073
|
if (cached !== void 0) return Promise.resolve({ ok: true, result: cached.snapshot });
|
|
@@ -5249,7 +6076,7 @@ function getSnapshot(deps, cache, loading, workspaceRoot) {
|
|
|
5249
6076
|
function loadWorkspace(deps, cache, loading, workspaceRoot) {
|
|
5250
6077
|
const running = loading.get(workspaceRoot);
|
|
5251
6078
|
if (running !== void 0) return running;
|
|
5252
|
-
const task =
|
|
6079
|
+
const task = readWorkspace2(deps, cache, workspaceRoot);
|
|
5253
6080
|
loading.set(workspaceRoot, task);
|
|
5254
6081
|
void task.then(
|
|
5255
6082
|
() => {
|
|
@@ -5261,7 +6088,7 @@ function loadWorkspace(deps, cache, loading, workspaceRoot) {
|
|
|
5261
6088
|
);
|
|
5262
6089
|
return task;
|
|
5263
6090
|
}
|
|
5264
|
-
async function
|
|
6091
|
+
async function readWorkspace2(deps, cache, workspaceRoot) {
|
|
5265
6092
|
const stored = await readStored(deps, workspaceRoot);
|
|
5266
6093
|
if (stored !== void 0) {
|
|
5267
6094
|
const served = await serveStored(deps, cache, workspaceRoot, stored);
|
|
@@ -5294,87 +6121,6 @@ async function serveStored(deps, cache, workspaceRoot, stored) {
|
|
|
5294
6121
|
cache.set(workspaceRoot, storedEntry(stored, resolved, snapshot));
|
|
5295
6122
|
return { ok: true, result: snapshot };
|
|
5296
6123
|
}
|
|
5297
|
-
async function refreshSnapshot(deps, cache, workspaceRoot, full) {
|
|
5298
|
-
const startedAt = deps.now();
|
|
5299
|
-
let resolved;
|
|
5300
|
-
try {
|
|
5301
|
-
resolved = await deps.resolve(workspaceRoot);
|
|
5302
|
-
} catch (error) {
|
|
5303
|
-
return await keepOldSnapshot(deps, cache, workspaceRoot, mapSnapshotError(error), startedAt);
|
|
5304
|
-
}
|
|
5305
|
-
const stored = await readStored(deps, workspaceRoot);
|
|
5306
|
-
const base = stored?.repositoryKey === repositoryKey(resolved.kind, resolved.address) ? stored : void 0;
|
|
5307
|
-
const tracker = resolved.tracker;
|
|
5308
|
-
if (base !== void 0 && !full && tracker.fetchChanges !== void 0 && // 对账以「全量覆盖」为前提:窗口截断的后端看不到窗口外的删除。
|
|
5309
|
-
base.windowLimit === void 0) {
|
|
5310
|
-
const merged = await mergeIncremental(deps, cache, workspaceRoot, resolved, base, startedAt);
|
|
5311
|
-
if (merged !== void 0) return merged;
|
|
5312
|
-
}
|
|
5313
|
-
try {
|
|
5314
|
-
const raw = await tracker.fetchSnapshot(
|
|
5315
|
-
workspaceRoot,
|
|
5316
|
-
windowFor(cache.get(workspaceRoot), base)
|
|
5317
|
-
);
|
|
5318
|
-
const current = cache.get(workspaceRoot);
|
|
5319
|
-
const host = resolved.kind === "markdown" ? null : resolved.address?.host ?? null;
|
|
5320
|
-
const sameRepository = current?.kind === resolved.kind && current.repositoryHost === host && current.raw.repository === raw.repository;
|
|
5321
|
-
const previousViewer = (sameRepository ? current.raw.viewer : null) ?? base?.viewer ?? null;
|
|
5322
|
-
const withViewer = raw.viewer === null ? { ...raw, viewer: previousViewer } : raw;
|
|
5323
|
-
return await publish(deps, cache, workspaceRoot, resolved, withViewer, startedAt, "full");
|
|
5324
|
-
} catch (error) {
|
|
5325
|
-
return await keepOldSnapshot(deps, cache, workspaceRoot, mapSnapshotError(error), startedAt);
|
|
5326
|
-
}
|
|
5327
|
-
}
|
|
5328
|
-
async function mergeIncremental(deps, cache, workspaceRoot, resolved, base, startedAt) {
|
|
5329
|
-
const tracker = resolved.tracker;
|
|
5330
|
-
if (tracker.fetchChanges === void 0) return void 0;
|
|
5331
|
-
let changes;
|
|
5332
|
-
try {
|
|
5333
|
-
changes = await tracker.fetchChanges(workspaceRoot, base.fetchedAt - INCREMENTAL_OVERLAP_MS);
|
|
5334
|
-
} catch (error) {
|
|
5335
|
-
return await keepOldSnapshot(deps, cache, workspaceRoot, mapSnapshotError(error), startedAt);
|
|
5336
|
-
}
|
|
5337
|
-
if (changes.truncated) {
|
|
5338
|
-
logIncrementalFallback(deps, resolved.kind, "truncated", startedAt);
|
|
5339
|
-
return void 0;
|
|
5340
|
-
}
|
|
5341
|
-
const total = changes.total;
|
|
5342
|
-
if (!Number.isInteger(total) || total < 0) {
|
|
5343
|
-
logIncrementalFallback(deps, resolved.kind, "invalid-total", startedAt);
|
|
5344
|
-
return void 0;
|
|
5345
|
-
}
|
|
5346
|
-
if (changes.hierarchy !== base.hierarchy) {
|
|
5347
|
-
logIncrementalFallback(deps, resolved.kind, "hierarchy", startedAt);
|
|
5348
|
-
return void 0;
|
|
5349
|
-
}
|
|
5350
|
-
const windowLimit = base.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
5351
|
-
const merged = mergeTickets(base.tickets, changes.tickets, windowLimit);
|
|
5352
|
-
if (merged.length > total) {
|
|
5353
|
-
logIncrementalFallback(deps, resolved.kind, "drift", startedAt);
|
|
5354
|
-
return void 0;
|
|
5355
|
-
}
|
|
5356
|
-
const raw = {
|
|
5357
|
-
repository: base.repository,
|
|
5358
|
-
fetchedAt: deps.now(),
|
|
5359
|
-
loaded: merged.length,
|
|
5360
|
-
total,
|
|
5361
|
-
hierarchy: base.hierarchy,
|
|
5362
|
-
tickets: merged,
|
|
5363
|
-
// 后端这次查了身份就用新值;没查(省略)时沿用基准数据里的身份。
|
|
5364
|
-
viewer: changes.viewer === void 0 ? base.viewer : changes.viewer,
|
|
5365
|
-
windowLimit
|
|
5366
|
-
};
|
|
5367
|
-
return await publish(deps, cache, workspaceRoot, resolved, raw, startedAt, "incremental");
|
|
5368
|
-
}
|
|
5369
|
-
function logIncrementalFallback(deps, backend, reason, startedAt) {
|
|
5370
|
-
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5371
|
-
result: "fallback",
|
|
5372
|
-
backend,
|
|
5373
|
-
stage: SNAPSHOT_STAGES.list,
|
|
5374
|
-
reason,
|
|
5375
|
-
durationMs: Math.max(0, deps.now() - startedAt)
|
|
5376
|
-
});
|
|
5377
|
-
}
|
|
5378
6124
|
async function fetchAndCache(deps, cache, workspaceRoot) {
|
|
5379
6125
|
const startedAt = deps.now();
|
|
5380
6126
|
let kind;
|
|
@@ -5395,7 +6141,7 @@ async function fetchAndCache(deps, cache, workspaceRoot) {
|
|
|
5395
6141
|
return snapshotFailure(mapped.code, mapped.message);
|
|
5396
6142
|
}
|
|
5397
6143
|
}
|
|
5398
|
-
async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mode) {
|
|
6144
|
+
async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mode, options) {
|
|
5399
6145
|
const repositoryHost = resolved.kind === "markdown" ? null : resolved.address?.host ?? null;
|
|
5400
6146
|
const current = cache.get(workspaceRoot);
|
|
5401
6147
|
const merged = mergePulled(current, raw);
|
|
@@ -5403,6 +6149,7 @@ async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mod
|
|
|
5403
6149
|
const resolver = resolved.tracker.resolveBlockers ?? null;
|
|
5404
6150
|
const finalized = resolver === null || !tickets.some((ticket) => ticket.state === "open");
|
|
5405
6151
|
const metrics = metricsOf(deps, resolved.kind, merged, tickets, finalized);
|
|
6152
|
+
const libraryVersion = options?.skipWrite ? options.versionFallback ?? null : await writeStored(deps, workspaceRoot, resolved, merged, metrics);
|
|
5406
6153
|
const snapshot = buildSnapshot(
|
|
5407
6154
|
resolved.kind,
|
|
5408
6155
|
repositoryHost,
|
|
@@ -5412,7 +6159,8 @@ async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mod
|
|
|
5412
6159
|
metrics,
|
|
5413
6160
|
"live",
|
|
5414
6161
|
false,
|
|
5415
|
-
null
|
|
6162
|
+
null,
|
|
6163
|
+
libraryVersion
|
|
5416
6164
|
);
|
|
5417
6165
|
cache.set(workspaceRoot, {
|
|
5418
6166
|
kind: resolved.kind,
|
|
@@ -5422,45 +6170,18 @@ async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mod
|
|
|
5422
6170
|
resolver,
|
|
5423
6171
|
pending: null
|
|
5424
6172
|
});
|
|
5425
|
-
|
|
5426
|
-
|
|
5427
|
-
|
|
5428
|
-
|
|
5429
|
-
stage: SNAPSHOT_STAGES.list,
|
|
5430
|
-
durationMs: Math.max(0, deps.now() - startedAt),
|
|
5431
|
-
repository: snapshot.repository,
|
|
5432
|
-
loaded: snapshot.loaded,
|
|
5433
|
-
total: snapshot.total,
|
|
5434
|
-
mode
|
|
5435
|
-
});
|
|
5436
|
-
return { ok: true, result: snapshot };
|
|
5437
|
-
}
|
|
5438
|
-
async function keepOldSnapshot(deps, cache, workspaceRoot, mapped, startedAt) {
|
|
5439
|
-
const refreshError = { code: mapped.code, message: mapped.message };
|
|
5440
|
-
const cached = cache.get(workspaceRoot);
|
|
5441
|
-
if (cached !== void 0) {
|
|
5442
|
-
const snapshot2 = { ...cached.snapshot, stale: true, refreshError };
|
|
5443
|
-
cached.snapshot = snapshot2;
|
|
5444
|
-
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5445
|
-
result: "error",
|
|
5446
|
-
backend: cached.kind,
|
|
6173
|
+
if (!options?.skipWrite) {
|
|
6174
|
+
deps.log("info", LOG_EVENTS.snapshot, {
|
|
6175
|
+
result: "ok",
|
|
6176
|
+
backend: resolved.kind,
|
|
5447
6177
|
stage: SNAPSHOT_STAGES.list,
|
|
5448
|
-
|
|
5449
|
-
|
|
6178
|
+
durationMs: Math.max(0, deps.now() - startedAt),
|
|
6179
|
+
repository: snapshot.repository,
|
|
6180
|
+
loaded: snapshot.loaded,
|
|
6181
|
+
total: snapshot.total,
|
|
6182
|
+
mode
|
|
5450
6183
|
});
|
|
5451
|
-
return { ok: true, result: snapshot2 };
|
|
5452
6184
|
}
|
|
5453
|
-
const stored = await readStored(deps, workspaceRoot);
|
|
5454
|
-
if (stored === void 0) return snapshotFailure(mapped.code, mapped.message);
|
|
5455
|
-
const snapshot = buildStoredSnapshot(deps, stored, { stale: true, refreshError });
|
|
5456
|
-
cache.set(workspaceRoot, storedEntry(stored, null, snapshot));
|
|
5457
|
-
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5458
|
-
result: "error",
|
|
5459
|
-
backend: stored.backend,
|
|
5460
|
-
stage: SNAPSHOT_STAGES.list,
|
|
5461
|
-
code: mapped.code,
|
|
5462
|
-
durationMs: Math.max(0, deps.now() - startedAt)
|
|
5463
|
-
});
|
|
5464
6185
|
return { ok: true, result: snapshot };
|
|
5465
6186
|
}
|
|
5466
6187
|
function resolveBlockers(deps, cache, loading, workspaceRoot) {
|
|
@@ -5502,7 +6223,8 @@ async function finalize(deps, cache, workspaceRoot, entry, resolver) {
|
|
|
5502
6223
|
metrics,
|
|
5503
6224
|
entry.snapshot.source,
|
|
5504
6225
|
entry.snapshot.stale,
|
|
5505
|
-
entry.snapshot.refreshError
|
|
6226
|
+
entry.snapshot.refreshError,
|
|
6227
|
+
entry.snapshot.libraryVersion
|
|
5506
6228
|
);
|
|
5507
6229
|
} catch (error) {
|
|
5508
6230
|
failure4 = mapSnapshotError(error);
|
|
@@ -6160,6 +6882,7 @@ function createRpcHandler(scope, run, timer, log) {
|
|
|
6160
6882
|
handle: (method, params) => {
|
|
6161
6883
|
if (method === RPC_METHODS.envCheck) return envCheck.handle(method, params);
|
|
6162
6884
|
if (method === RPC_METHODS.snapshotCache) return snapshot.cache(params);
|
|
6885
|
+
if (method === RPC_METHODS.snapshotRefresh) return snapshot.refresh(params);
|
|
6163
6886
|
if (method === RPC_METHODS.ticketGet || method === RPC_METHODS.ticketUpdate || method === RPC_METHODS.ticketOptions || method === RPC_METHODS.ticketDelete || method === RPC_METHODS.ticketAttachment) {
|
|
6164
6887
|
return ticket.handle(method, params);
|
|
6165
6888
|
}
|