@cnzgray/dsh-mattpocock-workflow 0.4.0 → 0.5.1
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/client.js +267 -66
- package/lib/index.js +1155 -440
- package/package.json +1 -1
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 = 6;
|
|
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);
|
|
@@ -429,6 +692,16 @@ function repositorySlug(remote) {
|
|
|
429
692
|
if (!REPOSITORY_SEGMENT.test(owner) || !REPOSITORY_SEGMENT.test(name2)) return void 0;
|
|
430
693
|
return `${owner}/${name2}`;
|
|
431
694
|
}
|
|
695
|
+
function parseRepositorySegment(value) {
|
|
696
|
+
if (value.includes("://") || value.includes("@")) return void 0;
|
|
697
|
+
const segments = value.split("/");
|
|
698
|
+
if (segments.length !== 2) return void 0;
|
|
699
|
+
const [owner, name2] = segments;
|
|
700
|
+
if (owner === void 0 || name2 === void 0) return void 0;
|
|
701
|
+
if (!REPOSITORY_SEGMENT.test(owner) || !REPOSITORY_SEGMENT.test(name2)) return void 0;
|
|
702
|
+
if (owner === "." || owner === ".." || name2 === "." || name2 === "..") return void 0;
|
|
703
|
+
return { owner, name: name2 };
|
|
704
|
+
}
|
|
432
705
|
function buildAddress(host, path) {
|
|
433
706
|
if (host === void 0 || path === void 0) return void 0;
|
|
434
707
|
const normalizedHost = normalizeHost(host);
|
|
@@ -517,15 +790,22 @@ function chooseBackend(address, glabHosts, teaHosts = []) {
|
|
|
517
790
|
// src/core/tracker-error.ts
|
|
518
791
|
var TrackerError = class extends Error {
|
|
519
792
|
code;
|
|
793
|
+
/**
|
|
794
|
+
* 服务端要求的重试等待(毫秒);限流类错误才有,
|
|
795
|
+
* 调用方据此决定冷却时长,缺省时按失败次数指数退避。
|
|
796
|
+
*/
|
|
797
|
+
retryAfterMs;
|
|
520
798
|
/**
|
|
521
799
|
* @param code - 客户端可据以给出操作提示的错误码。
|
|
522
800
|
* @param message - 面向用户的失败原因。
|
|
523
801
|
* @param options - 原始错误,保留调用链。
|
|
802
|
+
* @param retryAfterMs - 服务端要求的重试等待(毫秒)。
|
|
524
803
|
*/
|
|
525
|
-
constructor(code, message, options) {
|
|
804
|
+
constructor(code, message, options, retryAfterMs) {
|
|
526
805
|
super(message, options);
|
|
527
806
|
this.name = "TrackerError";
|
|
528
807
|
this.code = code;
|
|
808
|
+
this.retryAfterMs = retryAfterMs;
|
|
529
809
|
}
|
|
530
810
|
};
|
|
531
811
|
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;
|
|
@@ -704,12 +984,21 @@ function parseDependencies(body) {
|
|
|
704
984
|
for (const dependency of payload) {
|
|
705
985
|
if (!isRecord(dependency)) continue;
|
|
706
986
|
const number = dependency.number;
|
|
707
|
-
if (typeof number === "number" && Number.isInteger(number) && number > 0)
|
|
987
|
+
if (typeof number === "number" && Number.isInteger(number) && number > 0) {
|
|
988
|
+
const repository = readDependencyRepository(dependency.repository);
|
|
989
|
+
refs.push(repository === void 0 ? { number } : { number, repository });
|
|
990
|
+
}
|
|
708
991
|
if (dependency.state === "closed") continue;
|
|
709
992
|
open += 1;
|
|
710
993
|
}
|
|
711
994
|
return { received: payload.length, open, refs };
|
|
712
995
|
}
|
|
996
|
+
function readDependencyRepository(value) {
|
|
997
|
+
if (!isRecord(value)) return void 0;
|
|
998
|
+
const fullName = value.full_name;
|
|
999
|
+
if (typeof fullName !== "string") return void 0;
|
|
1000
|
+
return parseRepositorySegment(fullName) === void 0 ? void 0 : fullName;
|
|
1001
|
+
}
|
|
713
1002
|
function readStatus(stderr) {
|
|
714
1003
|
const match = STATUS_LINE.exec(stderr);
|
|
715
1004
|
if (match === null) return 200;
|
|
@@ -1039,7 +1328,7 @@ function parseNativeBlockers(output) {
|
|
|
1039
1328
|
if (!isRecord(link)) continue;
|
|
1040
1329
|
if (link.link_type !== "is_blocked_by") continue;
|
|
1041
1330
|
const iid = link.iid;
|
|
1042
|
-
if (typeof iid === "number" && Number.isInteger(iid) && iid > 0) refs.push(iid);
|
|
1331
|
+
if (typeof iid === "number" && Number.isInteger(iid) && iid > 0) refs.push({ number: iid });
|
|
1043
1332
|
if (link.state === "closed") continue;
|
|
1044
1333
|
open += 1;
|
|
1045
1334
|
}
|
|
@@ -1614,6 +1903,7 @@ var TRIAGE_LABELS = [
|
|
|
1614
1903
|
var RPC_PATH = "/api/dsh-mattpocock-workflow";
|
|
1615
1904
|
var SNAPSHOT_PAGE_SIZE = 100;
|
|
1616
1905
|
var SNAPSHOT_MAX_WINDOW = 2e3;
|
|
1906
|
+
var SNAPSHOT_DELTA_MAX_ROWS = 1e3;
|
|
1617
1907
|
var RPC_METHODS = {
|
|
1618
1908
|
/** 读缓存;没有缓存时拉取一次。阻塞信息可能尚未定稿。 */
|
|
1619
1909
|
snapshotGet: "snapshot.get",
|
|
@@ -1655,6 +1945,8 @@ var ERROR_CODES = {
|
|
|
1655
1945
|
fetchFailed: "fetch-failed",
|
|
1656
1946
|
/** 编号不存在或工单已被删除。 */
|
|
1657
1947
|
ticketNotFound: "ticket-not-found",
|
|
1948
|
+
/** 后端拒绝了请求(限流);携带服务端要求的重试等待时按它退避。 */
|
|
1949
|
+
rateLimited: "rate-limited",
|
|
1658
1950
|
/** 当前后端不提供所请求的详情或写能力。 */
|
|
1659
1951
|
capabilityUnsupported: "capability-unsupported",
|
|
1660
1952
|
invalidParams: "invalid-params",
|
|
@@ -1703,8 +1995,10 @@ function hasDescriptionBlockers(description) {
|
|
|
1703
1995
|
function countDescriptionBlockers(description, states) {
|
|
1704
1996
|
return countOpenBlockers(descriptionBlockerNumbers(description), states);
|
|
1705
1997
|
}
|
|
1706
|
-
function remoteBlockerRefs(
|
|
1707
|
-
return
|
|
1998
|
+
function remoteBlockerRefs(refs, repository) {
|
|
1999
|
+
return refs.map(
|
|
2000
|
+
(ref) => ref.repository === void 0 || ref.repository === repository ? `#${String(ref.number)}` : `${ref.repository}#${String(ref.number)}`
|
|
2001
|
+
);
|
|
1708
2002
|
}
|
|
1709
2003
|
function resolveOpenBlockers(input) {
|
|
1710
2004
|
if (input.ticket.state !== "open" || input.ticket.number === null) {
|
|
@@ -1720,7 +2014,7 @@ function resolveOpenBlockers(input) {
|
|
|
1720
2014
|
const numbers = descriptionBlockerNumbers(input.text);
|
|
1721
2015
|
return {
|
|
1722
2016
|
openBlockers: countOpenBlockers(numbers, input.states),
|
|
1723
|
-
blockedBy: remoteBlockerRefs(numbers)
|
|
2017
|
+
blockedBy: remoteBlockerRefs(numbers.map((number) => ({ number })))
|
|
1724
2018
|
};
|
|
1725
2019
|
}
|
|
1726
2020
|
function preambleOf2(description) {
|
|
@@ -1898,7 +2192,7 @@ function invalidPermission(message) {
|
|
|
1898
2192
|
// src/adapters/gitea/detail.ts
|
|
1899
2193
|
async function readGiteaDetail(deps, workspaceRoot, reference) {
|
|
1900
2194
|
const number = requireTicketNumber(reference);
|
|
1901
|
-
const repository = await readRepository(deps.run, workspaceRoot);
|
|
2195
|
+
const repository = reference.repository === void 0 ? await readRepository(deps.run, workspaceRoot) : repositoryOf(reference.repository);
|
|
1902
2196
|
const issue = await readIssue(deps, workspaceRoot, repository, number);
|
|
1903
2197
|
const comments = await readComments(deps, workspaceRoot, repository, number);
|
|
1904
2198
|
return {
|
|
@@ -1917,6 +2211,13 @@ async function readGiteaDetail(deps, workspaceRoot, reference) {
|
|
|
1917
2211
|
updatedAt: issue.updatedAt
|
|
1918
2212
|
};
|
|
1919
2213
|
}
|
|
2214
|
+
function repositoryOf(segment) {
|
|
2215
|
+
const parsed = parseRepositorySegment(segment);
|
|
2216
|
+
if (parsed === void 0) {
|
|
2217
|
+
throw new TrackerError("fetch-failed", "\u8DE8\u4ED3\u5E93\u5F15\u7528\u7684\u4ED3\u5E93\u6BB5\u4E0D\u5408\u6CD5\uFF0C\u9700\u8981 owner/name\u3002");
|
|
2218
|
+
}
|
|
2219
|
+
return { ...parsed, avatarBaseUrl: "" };
|
|
2220
|
+
}
|
|
1920
2221
|
async function applyGiteaChanges(deps, workspaceRoot, reference, patch) {
|
|
1921
2222
|
const number = requireTicketNumber(reference);
|
|
1922
2223
|
const repository = await readRepository(deps.run, workspaceRoot);
|
|
@@ -2159,7 +2460,7 @@ function invalidOptions(message) {
|
|
|
2159
2460
|
|
|
2160
2461
|
// src/adapters/gitea/tracker.ts
|
|
2161
2462
|
var DEFAULT_PAGE_SIZE = 50;
|
|
2162
|
-
var DEFAULT_CONCURRENCY =
|
|
2463
|
+
var DEFAULT_CONCURRENCY = 8;
|
|
2163
2464
|
var MAX_PAGES2 = 200;
|
|
2164
2465
|
function createGiteaTracker(deps) {
|
|
2165
2466
|
const pageSize = normalizePositive(deps.pageSize ?? DEFAULT_PAGE_SIZE, DEFAULT_PAGE_SIZE);
|
|
@@ -2175,23 +2476,12 @@ function createGiteaTracker(deps) {
|
|
|
2175
2476
|
const repository = await readRepository(deps.run, workspaceRoot);
|
|
2176
2477
|
const issues = await readIssues(deps, workspaceRoot, repository, pageSize, void 0, window);
|
|
2177
2478
|
const viewer = await viewerTask;
|
|
2178
|
-
const states =
|
|
2179
|
-
for (const issue of issues.items) {
|
|
2180
|
-
if (issue.ticket.number !== null) states.set(issue.ticket.number, issue.ticket.state);
|
|
2181
|
-
}
|
|
2182
|
-
const native = await readNativeBlockers(
|
|
2183
|
-
deps,
|
|
2184
|
-
workspaceRoot,
|
|
2185
|
-
repository,
|
|
2186
|
-
issues.items,
|
|
2187
|
-
pageSize,
|
|
2188
|
-
concurrency
|
|
2189
|
-
);
|
|
2479
|
+
const states = statesOf(issues.items);
|
|
2190
2480
|
const tickets = issues.items.map((issue) => ({
|
|
2191
2481
|
...issue.ticket,
|
|
2192
2482
|
...resolveOpenBlockers({
|
|
2193
2483
|
ticket: issue.ticket,
|
|
2194
|
-
nativeBlockers:
|
|
2484
|
+
nativeBlockers: void 0,
|
|
2195
2485
|
text: issue.body,
|
|
2196
2486
|
states
|
|
2197
2487
|
})
|
|
@@ -2209,7 +2499,10 @@ function createGiteaTracker(deps) {
|
|
|
2209
2499
|
};
|
|
2210
2500
|
},
|
|
2211
2501
|
async fetchChanges(workspaceRoot, since) {
|
|
2212
|
-
return readGiteaChanges(deps, workspaceRoot, pageSize, since
|
|
2502
|
+
return readGiteaChanges(deps, workspaceRoot, pageSize, since);
|
|
2503
|
+
},
|
|
2504
|
+
async resolveBlockers(workspaceRoot, targets) {
|
|
2505
|
+
return readNativeResolution(deps, workspaceRoot, targets, pageSize, concurrency);
|
|
2213
2506
|
},
|
|
2214
2507
|
fetchDetail: (workspaceRoot, reference) => readGiteaDetail(detailDeps, workspaceRoot, reference),
|
|
2215
2508
|
updateTicket: (workspaceRoot, reference, patch) => applyGiteaChanges(detailDeps, workspaceRoot, reference, patch),
|
|
@@ -2226,7 +2519,7 @@ async function readGiteaTotal(deps, workspaceRoot, repository) {
|
|
|
2226
2519
|
}
|
|
2227
2520
|
return response.total;
|
|
2228
2521
|
}
|
|
2229
|
-
async function readGiteaChanges(deps, workspaceRoot, pageSize, since
|
|
2522
|
+
async function readGiteaChanges(deps, workspaceRoot, pageSize, since) {
|
|
2230
2523
|
if (!Number.isFinite(since)) {
|
|
2231
2524
|
throw new TrackerError("fetch-failed", "\u589E\u91CF\u7A97\u53E3\u7684\u8D77\u70B9\u4E0D\u662F\u6709\u6548\u65F6\u95F4\u6233\u3002");
|
|
2232
2525
|
}
|
|
@@ -2238,23 +2531,12 @@ async function readGiteaChanges(deps, workspaceRoot, pageSize, since, concurrenc
|
|
|
2238
2531
|
const total = await readGiteaTotal(deps, workspaceRoot, repository);
|
|
2239
2532
|
const issues = await readIssues(deps, workspaceRoot, repository, pageSize, since);
|
|
2240
2533
|
const viewer = await viewerTask;
|
|
2241
|
-
const states =
|
|
2242
|
-
for (const issue of issues.items) {
|
|
2243
|
-
if (issue.ticket.number !== null) states.set(issue.ticket.number, issue.ticket.state);
|
|
2244
|
-
}
|
|
2245
|
-
const native = await readNativeBlockers(
|
|
2246
|
-
deps,
|
|
2247
|
-
workspaceRoot,
|
|
2248
|
-
repository,
|
|
2249
|
-
issues.items,
|
|
2250
|
-
pageSize,
|
|
2251
|
-
concurrency
|
|
2252
|
-
);
|
|
2534
|
+
const states = statesOf(issues.items);
|
|
2253
2535
|
const tickets = issues.items.map((issue) => ({
|
|
2254
2536
|
...issue.ticket,
|
|
2255
2537
|
...resolveOpenBlockers({
|
|
2256
2538
|
ticket: issue.ticket,
|
|
2257
|
-
nativeBlockers:
|
|
2539
|
+
nativeBlockers: void 0,
|
|
2258
2540
|
text: issue.body,
|
|
2259
2541
|
states
|
|
2260
2542
|
})
|
|
@@ -2297,27 +2579,45 @@ async function readIssues(deps, workspaceRoot, repository, pageSize, since, wind
|
|
|
2297
2579
|
truncated
|
|
2298
2580
|
};
|
|
2299
2581
|
}
|
|
2300
|
-
async function
|
|
2301
|
-
const open =
|
|
2302
|
-
(
|
|
2582
|
+
async function readNativeResolution(deps, workspaceRoot, targets, pageSize, concurrency) {
|
|
2583
|
+
const open = targets.filter(
|
|
2584
|
+
(target) => target.number !== null
|
|
2303
2585
|
);
|
|
2304
|
-
if (open.length === 0) return
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2586
|
+
if (open.length === 0) return emptyResolution();
|
|
2587
|
+
const repository = await readRepository(deps.run, workspaceRoot);
|
|
2588
|
+
const settled = await mapWithConcurrency(
|
|
2589
|
+
open,
|
|
2590
|
+
concurrency,
|
|
2591
|
+
async (target) => {
|
|
2592
|
+
try {
|
|
2593
|
+
const blocked2 = await readDependencies(
|
|
2594
|
+
deps,
|
|
2595
|
+
workspaceRoot,
|
|
2596
|
+
repository,
|
|
2597
|
+
target.number,
|
|
2598
|
+
pageSize
|
|
2599
|
+
);
|
|
2600
|
+
return { ref: target.ref, outcome: "ok", blocked: blocked2 };
|
|
2601
|
+
} catch (error) {
|
|
2602
|
+
if (error instanceof DependenciesUnavailable) {
|
|
2603
|
+
return { ref: target.ref, outcome: "unavailable" };
|
|
2604
|
+
}
|
|
2605
|
+
return { ref: target.ref, outcome: "failed" };
|
|
2606
|
+
}
|
|
2607
|
+
}
|
|
2608
|
+
);
|
|
2609
|
+
if (settled.some((entry) => entry.outcome === "unavailable")) return emptyResolution();
|
|
2610
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2611
|
+
const blockedBy = /* @__PURE__ */ new Map();
|
|
2612
|
+
const repositoryName = `${repository.owner}/${repository.name}`;
|
|
2613
|
+
for (const entry of settled) {
|
|
2614
|
+
if (entry.outcome !== "ok") continue;
|
|
2615
|
+
counts.set(entry.ref, entry.blocked.open);
|
|
2616
|
+
if (entry.blocked.refs.length > 0) {
|
|
2617
|
+
blockedBy.set(entry.ref, remoteBlockerRefs(entry.blocked.refs, repositoryName));
|
|
2618
|
+
}
|
|
2320
2619
|
}
|
|
2620
|
+
return { open: counts, blockedBy };
|
|
2321
2621
|
}
|
|
2322
2622
|
async function readDependencies(deps, workspaceRoot, repository, index, pageSize) {
|
|
2323
2623
|
let open = 0;
|
|
@@ -2344,6 +2644,16 @@ async function readDependencies(deps, workspaceRoot, repository, index, pageSize
|
|
|
2344
2644
|
}
|
|
2345
2645
|
var DependenciesUnavailable = class extends Error {
|
|
2346
2646
|
};
|
|
2647
|
+
function emptyResolution() {
|
|
2648
|
+
return { open: /* @__PURE__ */ new Map(), blockedBy: /* @__PURE__ */ new Map() };
|
|
2649
|
+
}
|
|
2650
|
+
function statesOf(issues) {
|
|
2651
|
+
const states = /* @__PURE__ */ new Map();
|
|
2652
|
+
for (const issue of issues) {
|
|
2653
|
+
if (issue.ticket.number !== null) states.set(issue.ticket.number, issue.ticket.state);
|
|
2654
|
+
}
|
|
2655
|
+
return states;
|
|
2656
|
+
}
|
|
2347
2657
|
function issuesEndpoint(repository, pageSize, page, since) {
|
|
2348
2658
|
const window = since === void 0 ? "" : `&since=${new Date(since).toISOString()}`;
|
|
2349
2659
|
return `/repos/${repository.owner}/${repository.name}/issues?state=all&type=issues&sort=recentupdate&page=${String(page)}&limit=${String(pageSize)}${window}`;
|
|
@@ -2415,6 +2725,15 @@ function mapGhError(error) {
|
|
|
2415
2725
|
cause: error
|
|
2416
2726
|
});
|
|
2417
2727
|
}
|
|
2728
|
+
const rateLimit = rateLimitOf(error.stderr);
|
|
2729
|
+
if (rateLimit !== null) {
|
|
2730
|
+
return new TrackerError(
|
|
2731
|
+
"rate-limited",
|
|
2732
|
+
`GitHub \u9650\u6D41\u4E2D\uFF0C\u7A0D\u540E\u4F1A\u81EA\u52A8\u6062\u590D\uFF1A${rateLimit}`,
|
|
2733
|
+
{ cause: error },
|
|
2734
|
+
retryAfterMsOf(error.stderr)
|
|
2735
|
+
);
|
|
2736
|
+
}
|
|
2418
2737
|
return new TrackerError("fetch-failed", `\u8BFB\u53D6\u5DE5\u5355\u5931\u8D25\uFF1A${commandFailureDetail(error)}`, {
|
|
2419
2738
|
cause: error
|
|
2420
2739
|
});
|
|
@@ -2426,6 +2745,18 @@ function isAuthenticationFailure(stderr) {
|
|
|
2426
2745
|
stderr
|
|
2427
2746
|
);
|
|
2428
2747
|
}
|
|
2748
|
+
var RATE_LIMITED = /rate limit|too many requests|HTTP 429/i;
|
|
2749
|
+
function rateLimitOf(stderr) {
|
|
2750
|
+
if (!RATE_LIMITED.test(stderr)) return null;
|
|
2751
|
+
const firstLine = stderr.split("\n").map((line) => line.trim()).find((line) => line.length > 0);
|
|
2752
|
+
return firstLine ?? "\u5DF2\u89E6\u53D1\u9650\u6D41\u3002";
|
|
2753
|
+
}
|
|
2754
|
+
function retryAfterMsOf(stderr) {
|
|
2755
|
+
const match = /retry[ -]?after[^0-9]*(\d{1,4})\s*s?\b/i.exec(stderr);
|
|
2756
|
+
if (match === null) return void 0;
|
|
2757
|
+
const seconds = Number(match[1]);
|
|
2758
|
+
return Number.isFinite(seconds) && seconds > 0 ? seconds * 1e3 : void 0;
|
|
2759
|
+
}
|
|
2429
2760
|
|
|
2430
2761
|
// src/adapters/github/delete.ts
|
|
2431
2762
|
var NOT_FOUND4 = /Could not resolve to an Issue|HTTP 404|404 Not Found|Not Found/i;
|
|
@@ -2625,6 +2956,15 @@ function parseIssueGraphql(output, repository) {
|
|
|
2625
2956
|
}
|
|
2626
2957
|
return { total, tickets, viewer: readViewer2(payload), ...readPageCursor(issues) };
|
|
2627
2958
|
}
|
|
2959
|
+
function parseIssuesTotal(output) {
|
|
2960
|
+
const payload = parseJson6(output, "gh \u8FD4\u56DE\u7684\u4E0D\u662F\u6709\u6548 JSON");
|
|
2961
|
+
const issues = readIssues2(readRepository3(payload));
|
|
2962
|
+
const total = issues.totalCount;
|
|
2963
|
+
if (typeof total !== "number" || !Number.isInteger(total) || total < 0) {
|
|
2964
|
+
throw invalid3("\u4ED3\u5E93\u7684 issues.totalCount \u4E0D\u662F\u975E\u8D1F\u6574\u6570");
|
|
2965
|
+
}
|
|
2966
|
+
return total;
|
|
2967
|
+
}
|
|
2628
2968
|
function readViewer2(payload) {
|
|
2629
2969
|
if (!isRecord(payload)) return null;
|
|
2630
2970
|
const data = payload.data;
|
|
@@ -2844,6 +3184,7 @@ function invalidOptions2(message) {
|
|
|
2844
3184
|
|
|
2845
3185
|
// src/adapters/github/tracker.ts
|
|
2846
3186
|
var DEFAULT_LIMIT = 100;
|
|
3187
|
+
var DEFAULT_CHANGES_PAGE_LIMIT = 5;
|
|
2847
3188
|
var ISSUE_NODES = `nodes {
|
|
2848
3189
|
number
|
|
2849
3190
|
title
|
|
@@ -2869,6 +3210,22 @@ var ISSUE_QUERY = `query ($owner: String!, $name: String!, $limit: Int!, $after:
|
|
|
2869
3210
|
}
|
|
2870
3211
|
}
|
|
2871
3212
|
}`;
|
|
3213
|
+
var ISSUE_CHANGES_QUERY = `query ($owner: String!, $name: String!, $limit: Int!, $after: String, $since: DateTime!) {
|
|
3214
|
+
viewer { login }
|
|
3215
|
+
repository(owner: $owner, name: $name) {
|
|
3216
|
+
nameWithOwner
|
|
3217
|
+
issues(first: $limit, after: $after, orderBy: { field: UPDATED_AT, direction: DESC }, filterBy: { since: $since }) {
|
|
3218
|
+
totalCount
|
|
3219
|
+
pageInfo { hasNextPage endCursor }
|
|
3220
|
+
${ISSUE_NODES}
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
}`;
|
|
3224
|
+
var ISSUE_TOTAL_QUERY = `query ($owner: String!, $name: String!) {
|
|
3225
|
+
repository(owner: $owner, name: $name) {
|
|
3226
|
+
issues(first: 1) { totalCount }
|
|
3227
|
+
}
|
|
3228
|
+
}`;
|
|
2872
3229
|
function createGithubTracker(deps) {
|
|
2873
3230
|
const limit = normalizeLimit(deps.limit ?? DEFAULT_LIMIT);
|
|
2874
3231
|
return {
|
|
@@ -2889,6 +3246,21 @@ function createGithubTracker(deps) {
|
|
|
2889
3246
|
windowLimit: window
|
|
2890
3247
|
};
|
|
2891
3248
|
},
|
|
3249
|
+
async fetchChanges(workspaceRoot, since) {
|
|
3250
|
+
if (!Number.isFinite(since)) {
|
|
3251
|
+
throw new TrackerError("fetch-failed", "\u589E\u91CF\u7A97\u53E3\u7684\u8D77\u70B9\u4E0D\u662F\u6709\u6548\u65F6\u95F4\u6233\u3002");
|
|
3252
|
+
}
|
|
3253
|
+
const repository = await readRepository2(deps.run, workspaceRoot);
|
|
3254
|
+
const total = await readIssuesTotal(deps, workspaceRoot, repository);
|
|
3255
|
+
const changed = await readChangedTickets(deps, workspaceRoot, repository, limit, since);
|
|
3256
|
+
return {
|
|
3257
|
+
tickets: changed.tickets,
|
|
3258
|
+
hierarchy: "native",
|
|
3259
|
+
truncated: changed.truncated,
|
|
3260
|
+
total,
|
|
3261
|
+
viewer: changed.viewer
|
|
3262
|
+
};
|
|
3263
|
+
},
|
|
2892
3264
|
fetchDetail: (workspaceRoot, reference) => readGithubDetail(deps, workspaceRoot, reference),
|
|
2893
3265
|
updateTicket: (workspaceRoot, reference, patch) => applyGithubChanges(deps, workspaceRoot, reference, patch),
|
|
2894
3266
|
fetchOptions: (workspaceRoot) => readGithubOptions(deps, workspaceRoot),
|
|
@@ -2920,6 +3292,55 @@ async function readTickets2(deps, workspaceRoot, repository, pageSize, window) {
|
|
|
2920
3292
|
const windowed = tickets.slice(0, window);
|
|
2921
3293
|
return { total, tickets: windowed, viewer, hasNextPage: false, endCursor: null };
|
|
2922
3294
|
}
|
|
3295
|
+
async function readIssuesTotal(deps, workspaceRoot, repository) {
|
|
3296
|
+
const args = [
|
|
3297
|
+
"api",
|
|
3298
|
+
"graphql",
|
|
3299
|
+
"-f",
|
|
3300
|
+
`query=${ISSUE_TOTAL_QUERY}`,
|
|
3301
|
+
"-f",
|
|
3302
|
+
`owner=${repository.owner}`,
|
|
3303
|
+
"-f",
|
|
3304
|
+
`name=${repository.name}`
|
|
3305
|
+
];
|
|
3306
|
+
return parseIssuesTotal(await callGh(deps.run, workspaceRoot, args));
|
|
3307
|
+
}
|
|
3308
|
+
async function readChangedTickets(deps, workspaceRoot, repository, pageSize, since) {
|
|
3309
|
+
const tickets = [];
|
|
3310
|
+
let viewer = null;
|
|
3311
|
+
let cursor = null;
|
|
3312
|
+
for (let page = 1; page <= DEFAULT_CHANGES_PAGE_LIMIT; page += 1) {
|
|
3313
|
+
const parsed = await readChangesPage(deps, workspaceRoot, repository, pageSize, cursor, since);
|
|
3314
|
+
viewer = parsed.viewer ?? viewer;
|
|
3315
|
+
tickets.push(...parsed.tickets);
|
|
3316
|
+
if (!parsed.hasNextPage || parsed.endCursor === null || parsed.tickets.length === 0) {
|
|
3317
|
+
return { tickets, viewer, truncated: false };
|
|
3318
|
+
}
|
|
3319
|
+
cursor = parsed.endCursor;
|
|
3320
|
+
if (page === DEFAULT_CHANGES_PAGE_LIMIT) {
|
|
3321
|
+
return { tickets, viewer, truncated: true };
|
|
3322
|
+
}
|
|
3323
|
+
}
|
|
3324
|
+
return { tickets, viewer, truncated: false };
|
|
3325
|
+
}
|
|
3326
|
+
async function readChangesPage(deps, workspaceRoot, repository, limit, after, since) {
|
|
3327
|
+
const args = [
|
|
3328
|
+
"api",
|
|
3329
|
+
"graphql",
|
|
3330
|
+
"-f",
|
|
3331
|
+
`query=${ISSUE_CHANGES_QUERY}`,
|
|
3332
|
+
"-f",
|
|
3333
|
+
`owner=${repository.owner}`,
|
|
3334
|
+
"-f",
|
|
3335
|
+
`name=${repository.name}`,
|
|
3336
|
+
"-F",
|
|
3337
|
+
`limit=${String(limit)}`,
|
|
3338
|
+
"-f",
|
|
3339
|
+
`since=${new Date(since).toISOString()}`,
|
|
3340
|
+
...after === null ? [] : ["-f", `after=${after}`]
|
|
3341
|
+
];
|
|
3342
|
+
return parseIssueGraphql(await callGh(deps.run, workspaceRoot, args), repositoryLabel(repository));
|
|
3343
|
+
}
|
|
2923
3344
|
async function readTicketsPage(deps, workspaceRoot, repository, limit, after) {
|
|
2924
3345
|
const args = [
|
|
2925
3346
|
"api",
|
|
@@ -3479,7 +3900,7 @@ function createGitlabTracker(deps) {
|
|
|
3479
3900
|
const open = targets.filter(
|
|
3480
3901
|
(target) => target.number !== null
|
|
3481
3902
|
);
|
|
3482
|
-
if (open.length === 0) return
|
|
3903
|
+
if (open.length === 0) return emptyResolution2();
|
|
3483
3904
|
const hint = hints.get(workspaceRoot) ?? {
|
|
3484
3905
|
project: await readProjectAddress(deps.run, workspaceRoot),
|
|
3485
3906
|
listSupportsNative: false,
|
|
@@ -3492,7 +3913,7 @@ function createGitlabTracker(deps) {
|
|
|
3492
3913
|
hint,
|
|
3493
3914
|
open
|
|
3494
3915
|
);
|
|
3495
|
-
if (!supported) return
|
|
3916
|
+
if (!supported) return emptyResolution2();
|
|
3496
3917
|
const counts = await readBlockedByCounts(deps, workspaceRoot, hint.project.path, open);
|
|
3497
3918
|
const native = /* @__PURE__ */ new Map();
|
|
3498
3919
|
for (const target of open) {
|
|
@@ -3517,7 +3938,7 @@ function createGitlabTracker(deps) {
|
|
|
3517
3938
|
fetchAttachment: (workspaceRoot, path) => readGitlabAttachment(deps, workspaceRoot, path)
|
|
3518
3939
|
};
|
|
3519
3940
|
}
|
|
3520
|
-
function
|
|
3941
|
+
function emptyResolution2() {
|
|
3521
3942
|
return { open: /* @__PURE__ */ new Map(), blockedBy: /* @__PURE__ */ new Map() };
|
|
3522
3943
|
}
|
|
3523
3944
|
async function readSnapshot(deps, hints, workspaceRoot, pageSize, windowLimit) {
|
|
@@ -4223,9 +4644,13 @@ var TICKET_ACTIONS = {
|
|
|
4223
4644
|
|
|
4224
4645
|
// src/host/env-check-service.ts
|
|
4225
4646
|
var TRACKER_DOC = "docs/agents/issue-tracker.md";
|
|
4647
|
+
var ENV_CHECK_CACHE_TTL_MS = 10 * 60 * 1e3;
|
|
4648
|
+
var ENV_CHECK_BACKOFF_START_MS = 8e3;
|
|
4649
|
+
var ENV_CHECK_BACKOFF_MAX_MS = 5 * 60 * 1e3;
|
|
4226
4650
|
var SKILL_DIRECTORIES = [".dsh/skills", ".agents/skills"];
|
|
4227
4651
|
function createEnvCheckService(deps) {
|
|
4228
4652
|
const pending = /* @__PURE__ */ new Map();
|
|
4653
|
+
const cache = /* @__PURE__ */ new Map();
|
|
4229
4654
|
return {
|
|
4230
4655
|
handle(method, params) {
|
|
4231
4656
|
if (method !== RPC_METHODS.envCheck) {
|
|
@@ -4239,7 +4664,38 @@ function createEnvCheckService(deps) {
|
|
|
4239
4664
|
}
|
|
4240
4665
|
const running = pending.get(workspaceRoot);
|
|
4241
4666
|
if (running !== void 0) return running;
|
|
4242
|
-
const
|
|
4667
|
+
const now = deps.now();
|
|
4668
|
+
const state = cache.get(workspaceRoot);
|
|
4669
|
+
const cached = state?.success;
|
|
4670
|
+
if (cached !== void 0 && cached !== null && now < cached.expiresAt) {
|
|
4671
|
+
const task2 = serveCached(deps, workspaceRoot, cached).then((outcome) => {
|
|
4672
|
+
cache.set(workspaceRoot, nextCacheState(state, outcome, deps.now()));
|
|
4673
|
+
return outcome.result;
|
|
4674
|
+
});
|
|
4675
|
+
pending.set(workspaceRoot, task2);
|
|
4676
|
+
void task2.then(
|
|
4677
|
+
() => {
|
|
4678
|
+
pending.delete(workspaceRoot);
|
|
4679
|
+
},
|
|
4680
|
+
() => {
|
|
4681
|
+
pending.delete(workspaceRoot);
|
|
4682
|
+
}
|
|
4683
|
+
);
|
|
4684
|
+
return task2;
|
|
4685
|
+
}
|
|
4686
|
+
if (state !== void 0 && state.failures > 0 && state.lastFailure !== null) {
|
|
4687
|
+
const backoff = Math.min(
|
|
4688
|
+
ENV_CHECK_BACKOFF_START_MS * 2 ** (state.failures - 1),
|
|
4689
|
+
ENV_CHECK_BACKOFF_MAX_MS
|
|
4690
|
+
);
|
|
4691
|
+
if (now - state.lastRunAt < backoff) {
|
|
4692
|
+
return Promise.resolve({ ok: true, result: state.lastFailure });
|
|
4693
|
+
}
|
|
4694
|
+
}
|
|
4695
|
+
const task = runCheck(deps, workspaceRoot).then((outcome) => {
|
|
4696
|
+
cache.set(workspaceRoot, nextCacheState(state, outcome, deps.now()));
|
|
4697
|
+
return outcome.result;
|
|
4698
|
+
});
|
|
4243
4699
|
pending.set(workspaceRoot, task);
|
|
4244
4700
|
void task.then(
|
|
4245
4701
|
() => {
|
|
@@ -4253,6 +4709,41 @@ function createEnvCheckService(deps) {
|
|
|
4253
4709
|
}
|
|
4254
4710
|
};
|
|
4255
4711
|
}
|
|
4712
|
+
async function serveCached(deps, workspaceRoot, cached) {
|
|
4713
|
+
const probe = await probeBackend(deps, workspaceRoot);
|
|
4714
|
+
const identity = identityOf(probe);
|
|
4715
|
+
if (identity !== null && identity === cached.identity) {
|
|
4716
|
+
return { result: { ok: true, result: cached.report }, identity: cached.identity };
|
|
4717
|
+
}
|
|
4718
|
+
return runCheck(deps, workspaceRoot);
|
|
4719
|
+
}
|
|
4720
|
+
function nextCacheState(previous, outcome, now) {
|
|
4721
|
+
const report = outcome.result.ok ? outcome.result.result : null;
|
|
4722
|
+
if (report === null) return previous ?? emptyCache(now);
|
|
4723
|
+
const failed = report.items.some((item) => item.status === "fail");
|
|
4724
|
+
if (!failed) {
|
|
4725
|
+
return {
|
|
4726
|
+
success: outcome.identity === null ? null : { report, identity: outcome.identity, expiresAt: now + ENV_CHECK_CACHE_TTL_MS },
|
|
4727
|
+
failures: 0,
|
|
4728
|
+
lastRunAt: now,
|
|
4729
|
+
lastFailure: null
|
|
4730
|
+
};
|
|
4731
|
+
}
|
|
4732
|
+
return {
|
|
4733
|
+
success: previous?.success ?? null,
|
|
4734
|
+
failures: (previous?.failures ?? 0) + 1,
|
|
4735
|
+
lastRunAt: now,
|
|
4736
|
+
lastFailure: report
|
|
4737
|
+
};
|
|
4738
|
+
}
|
|
4739
|
+
function emptyCache(now) {
|
|
4740
|
+
return { success: null, failures: 0, lastRunAt: now, lastFailure: null };
|
|
4741
|
+
}
|
|
4742
|
+
function identityOf(probe) {
|
|
4743
|
+
if (probe.outcome.status !== "pass") return null;
|
|
4744
|
+
const kind = probe.outcome.choice.kind;
|
|
4745
|
+
return `${kind}@${kind === "markdown" ? "" : probe.remote?.path ?? ""}`;
|
|
4746
|
+
}
|
|
4256
4747
|
async function runCheck(deps, workspaceRoot) {
|
|
4257
4748
|
const startedAt = deps.now();
|
|
4258
4749
|
try {
|
|
@@ -4266,13 +4757,16 @@ async function runCheck(deps, workspaceRoot) {
|
|
|
4266
4757
|
capabilities: deps.capabilities()
|
|
4267
4758
|
};
|
|
4268
4759
|
logSummary(deps, report, startedAt);
|
|
4269
|
-
return { ok: true, result: report };
|
|
4760
|
+
return { result: { ok: true, result: report }, identity: collected.identity };
|
|
4270
4761
|
} catch {
|
|
4271
4762
|
deps.log("warn", LOG_EVENTS.envCheck, {
|
|
4272
4763
|
result: "error",
|
|
4273
4764
|
durationMs: Math.max(0, deps.now() - startedAt)
|
|
4274
4765
|
});
|
|
4275
|
-
return
|
|
4766
|
+
return {
|
|
4767
|
+
result: failure(ERROR_CODES.internal, "\u73AF\u5883\u68C0\u67E5\u65F6\u53D1\u751F\u672A\u9884\u671F\u7684\u9519\u8BEF\u3002"),
|
|
4768
|
+
identity: null
|
|
4769
|
+
};
|
|
4276
4770
|
}
|
|
4277
4771
|
}
|
|
4278
4772
|
async function collectFacts(deps, workspaceRoot) {
|
|
@@ -4300,7 +4794,8 @@ async function collectFacts(deps, workspaceRoot) {
|
|
|
4300
4794
|
skills,
|
|
4301
4795
|
setupSkill: PANEL_SKILLS.setup
|
|
4302
4796
|
},
|
|
4303
|
-
repository: decided === null || decided === "markdown" ? null : backend.remote?.path ?? null
|
|
4797
|
+
repository: decided === null || decided === "markdown" ? null : backend.remote?.path ?? null,
|
|
4798
|
+
identity: identityOf(backend)
|
|
4304
4799
|
};
|
|
4305
4800
|
}
|
|
4306
4801
|
async function probeLocalIssues(deps, workspaceRoot) {
|
|
@@ -4578,53 +5073,6 @@ function classifyTickets(tickets, names) {
|
|
|
4578
5073
|
}));
|
|
4579
5074
|
}
|
|
4580
5075
|
|
|
4581
|
-
// src/core/merge.ts
|
|
4582
|
-
function mergeTickets(base, changes, windowLimit) {
|
|
4583
|
-
const positions = /* @__PURE__ */ new Map();
|
|
4584
|
-
const merged = [];
|
|
4585
|
-
for (const ticket of base) {
|
|
4586
|
-
if (!positions.has(ticket.ref)) positions.set(ticket.ref, merged.length);
|
|
4587
|
-
merged.push(ticket);
|
|
4588
|
-
}
|
|
4589
|
-
for (const change of changes) {
|
|
4590
|
-
const at = positions.get(change.ref);
|
|
4591
|
-
if (at === void 0) {
|
|
4592
|
-
positions.set(change.ref, merged.length);
|
|
4593
|
-
merged.push(change);
|
|
4594
|
-
continue;
|
|
4595
|
-
}
|
|
4596
|
-
const existing = merged[at];
|
|
4597
|
-
if (existing !== void 0 && isNewer(change, existing)) merged[at] = change;
|
|
4598
|
-
}
|
|
4599
|
-
if (windowLimit === void 0) return merged;
|
|
4600
|
-
return trimToWindow(merged, windowLimit);
|
|
4601
|
-
}
|
|
4602
|
-
function isNewer(candidate, current) {
|
|
4603
|
-
const left = timestamp(candidate.updatedAt);
|
|
4604
|
-
const right = timestamp(current.updatedAt);
|
|
4605
|
-
if (left === void 0 || right === void 0) return candidate.updatedAt >= current.updatedAt;
|
|
4606
|
-
return left >= right;
|
|
4607
|
-
}
|
|
4608
|
-
function timestamp(value) {
|
|
4609
|
-
const parsed = Date.parse(value);
|
|
4610
|
-
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
4611
|
-
}
|
|
4612
|
-
function trimToWindow(tickets, limit) {
|
|
4613
|
-
if (tickets.length <= limit) return [...tickets];
|
|
4614
|
-
const sorted = [...tickets].sort((left, right) => {
|
|
4615
|
-
const byTime = compareTime(left.updatedAt, right.updatedAt);
|
|
4616
|
-
if (byTime !== 0) return byTime;
|
|
4617
|
-
return left.ref.localeCompare(right.ref);
|
|
4618
|
-
});
|
|
4619
|
-
return sorted.slice(0, limit);
|
|
4620
|
-
}
|
|
4621
|
-
function compareTime(left, right) {
|
|
4622
|
-
const leftTime = timestamp(left);
|
|
4623
|
-
const rightTime = timestamp(right);
|
|
4624
|
-
if (leftTime === void 0 || rightTime === void 0) return right.localeCompare(left);
|
|
4625
|
-
return rightTime - leftTime;
|
|
4626
|
-
}
|
|
4627
|
-
|
|
4628
5076
|
// src/core/skill-action.ts
|
|
4629
5077
|
function decideSkillAction(ticket, names) {
|
|
4630
5078
|
const status = classify(ticket, names);
|
|
@@ -4650,6 +5098,45 @@ function blocked(reason) {
|
|
|
4650
5098
|
return { skillAction: "none", skillActionReason: reason };
|
|
4651
5099
|
}
|
|
4652
5100
|
|
|
5101
|
+
// src/host/refresh-guard.ts
|
|
5102
|
+
function createRefreshGuard(options = {}) {
|
|
5103
|
+
const baseMs = options.baseMs ?? 5e3;
|
|
5104
|
+
const maxMs = options.maxMs ?? 3e5;
|
|
5105
|
+
const states = /* @__PURE__ */ new Map();
|
|
5106
|
+
function stateOf(workspaceRoot) {
|
|
5107
|
+
let state = states.get(workspaceRoot);
|
|
5108
|
+
if (state === void 0) {
|
|
5109
|
+
state = { failuresSinceSuccess: 0, coolingUntil: 0 };
|
|
5110
|
+
states.set(workspaceRoot, state);
|
|
5111
|
+
}
|
|
5112
|
+
return state;
|
|
5113
|
+
}
|
|
5114
|
+
return {
|
|
5115
|
+
blockedUntil(workspaceRoot, at) {
|
|
5116
|
+
const state = states.get(workspaceRoot);
|
|
5117
|
+
if (state === void 0 || state.coolingUntil <= at) return null;
|
|
5118
|
+
return state.coolingUntil;
|
|
5119
|
+
},
|
|
5120
|
+
noteSuccess(workspaceRoot) {
|
|
5121
|
+
const state = stateOf(workspaceRoot);
|
|
5122
|
+
state.failuresSinceSuccess = 0;
|
|
5123
|
+
state.coolingUntil = 0;
|
|
5124
|
+
},
|
|
5125
|
+
noteFailure(workspaceRoot, at, retryAfterMs) {
|
|
5126
|
+
const state = stateOf(workspaceRoot);
|
|
5127
|
+
state.failuresSinceSuccess += 1;
|
|
5128
|
+
const backoffMs = Math.min(baseMs * 2 ** Math.min(state.failuresSinceSuccess - 1, 16), maxMs);
|
|
5129
|
+
const waitMs = retryAfterMs !== void 0 && retryAfterMs > 0 ? Math.min(retryAfterMs, maxMs) : backoffMs;
|
|
5130
|
+
state.coolingUntil = at + waitMs;
|
|
5131
|
+
return state.coolingUntil;
|
|
5132
|
+
},
|
|
5133
|
+
view(workspaceRoot) {
|
|
5134
|
+
const state = states.get(workspaceRoot);
|
|
5135
|
+
return state === void 0 ? { failuresSinceSuccess: 0, coolingUntil: 0 } : { ...state };
|
|
5136
|
+
}
|
|
5137
|
+
};
|
|
5138
|
+
}
|
|
5139
|
+
|
|
4653
5140
|
// src/host/rpc-params.ts
|
|
4654
5141
|
function readWorkspaceRoot2(params) {
|
|
4655
5142
|
if (typeof params !== "object" || params === null) return void 0;
|
|
@@ -4668,70 +5155,75 @@ function readTicketParams(params) {
|
|
|
4668
5155
|
if (number !== null && !(typeof number === "number" && Number.isInteger(number) && number > 0)) {
|
|
4669
5156
|
return void 0;
|
|
4670
5157
|
}
|
|
4671
|
-
|
|
5158
|
+
let repository;
|
|
5159
|
+
if ("repository" in params && params.repository !== void 0) {
|
|
5160
|
+
const value = params.repository;
|
|
5161
|
+
if (typeof value !== "string" || parseRepositorySegment(value) === void 0) return void 0;
|
|
5162
|
+
repository = value;
|
|
5163
|
+
}
|
|
5164
|
+
return { workspaceRoot, ref, number, ...repository === void 0 ? {} : { repository } };
|
|
4672
5165
|
}
|
|
4673
5166
|
|
|
4674
5167
|
// src/host/snapshot-error.ts
|
|
4675
5168
|
function mapSnapshotError(error) {
|
|
4676
|
-
if (error instanceof TrackerError)
|
|
5169
|
+
if (error instanceof TrackerError) {
|
|
5170
|
+
return {
|
|
5171
|
+
code: error.code,
|
|
5172
|
+
message: error.message,
|
|
5173
|
+
...error.retryAfterMs !== void 0 ? { retryAfterMs: error.retryAfterMs } : {}
|
|
5174
|
+
};
|
|
5175
|
+
}
|
|
4677
5176
|
return { code: ERROR_CODES.internal, message: "\u8BFB\u53D6\u5DE5\u5355\u65F6\u53D1\u751F\u672A\u9884\u671F\u7684\u9519\u8BEF\u3002" };
|
|
4678
5177
|
}
|
|
4679
5178
|
function snapshotFailure(code, message) {
|
|
4680
5179
|
return { ok: false, error: { code, message } };
|
|
4681
5180
|
}
|
|
4682
5181
|
|
|
4683
|
-
// src/
|
|
4684
|
-
function
|
|
4685
|
-
const
|
|
4686
|
-
const
|
|
4687
|
-
const
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
function mergePulled(current, raw) {
|
|
4691
|
-
if (current === void 0) return raw;
|
|
4692
|
-
if (current.raw.repository !== raw.repository) return raw;
|
|
4693
|
-
const existingWindow = current.raw.windowLimit ?? current.raw.tickets.length;
|
|
4694
|
-
const pulledWindow = raw.windowLimit ?? raw.tickets.length;
|
|
4695
|
-
const window = Math.max(existingWindow, pulledWindow);
|
|
4696
|
-
const shrunk = raw.tickets.length < current.raw.tickets.length;
|
|
4697
|
-
if (!shrunk && window <= pulledWindow) return raw;
|
|
4698
|
-
const tickets = mergeTickets(current.raw.tickets, raw.tickets, window);
|
|
4699
|
-
return { ...raw, loaded: tickets.length, tickets, windowLimit: window };
|
|
4700
|
-
}
|
|
4701
|
-
async function loadMore(host, workspaceRoot) {
|
|
4702
|
-
const initial = await host.getSnapshot(workspaceRoot);
|
|
4703
|
-
if (!initial.ok) return initial;
|
|
4704
|
-
const entry = host.cache.get(workspaceRoot);
|
|
4705
|
-
if (entry === void 0) return initial;
|
|
4706
|
-
const windowLimit = entry.raw.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
4707
|
-
if (entry.raw.tickets.length >= entry.raw.total || windowLimit >= SNAPSHOT_MAX_WINDOW) {
|
|
4708
|
-
return { ok: true, result: entry.snapshot };
|
|
5182
|
+
// src/core/merge.ts
|
|
5183
|
+
function mergeTickets(base, changes, windowLimit) {
|
|
5184
|
+
const positions = /* @__PURE__ */ new Map();
|
|
5185
|
+
const merged = [];
|
|
5186
|
+
for (const ticket of base) {
|
|
5187
|
+
if (!positions.has(ticket.ref)) positions.set(ticket.ref, merged.length);
|
|
5188
|
+
merged.push(ticket);
|
|
4709
5189
|
}
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4716
|
-
|
|
4717
|
-
const
|
|
4718
|
-
|
|
4719
|
-
workspaceRoot,
|
|
4720
|
-
resolved,
|
|
4721
|
-
{ ...raw, viewer, windowLimit: nextWindow },
|
|
4722
|
-
startedAt
|
|
4723
|
-
);
|
|
4724
|
-
} catch (error) {
|
|
4725
|
-
const mapped = mapSnapshotError(error);
|
|
4726
|
-
host.log("warn", LOG_EVENTS.snapshot, {
|
|
4727
|
-
result: "error",
|
|
4728
|
-
...kind === void 0 ? {} : { backend: kind },
|
|
4729
|
-
stage: SNAPSHOT_STAGES.list,
|
|
4730
|
-
code: mapped.code,
|
|
4731
|
-
durationMs: Math.max(0, host.now() - startedAt)
|
|
4732
|
-
});
|
|
4733
|
-
return snapshotFailure(mapped.code, mapped.message);
|
|
5190
|
+
for (const change of changes) {
|
|
5191
|
+
const at = positions.get(change.ref);
|
|
5192
|
+
if (at === void 0) {
|
|
5193
|
+
positions.set(change.ref, merged.length);
|
|
5194
|
+
merged.push(change);
|
|
5195
|
+
continue;
|
|
5196
|
+
}
|
|
5197
|
+
const existing = merged[at];
|
|
5198
|
+
if (existing !== void 0 && isNewer(change, existing)) merged[at] = change;
|
|
4734
5199
|
}
|
|
5200
|
+
if (windowLimit === void 0) return merged;
|
|
5201
|
+
return trimToWindow(merged, windowLimit);
|
|
5202
|
+
}
|
|
5203
|
+
function isNewer(candidate, current) {
|
|
5204
|
+
const left = timestamp(candidate.updatedAt);
|
|
5205
|
+
const right = timestamp(current.updatedAt);
|
|
5206
|
+
if (left === void 0 || right === void 0) return candidate.updatedAt >= current.updatedAt;
|
|
5207
|
+
return left >= right;
|
|
5208
|
+
}
|
|
5209
|
+
function timestamp(value) {
|
|
5210
|
+
const parsed = Date.parse(value);
|
|
5211
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
5212
|
+
}
|
|
5213
|
+
function trimToWindow(tickets, limit) {
|
|
5214
|
+
if (tickets.length <= limit) return [...tickets];
|
|
5215
|
+
const sorted = [...tickets].sort((left, right) => {
|
|
5216
|
+
const byTime = compareTime(left.updatedAt, right.updatedAt);
|
|
5217
|
+
if (byTime !== 0) return byTime;
|
|
5218
|
+
return left.ref.localeCompare(right.ref);
|
|
5219
|
+
});
|
|
5220
|
+
return sorted.slice(0, limit);
|
|
5221
|
+
}
|
|
5222
|
+
function compareTime(left, right) {
|
|
5223
|
+
const leftTime = timestamp(left);
|
|
5224
|
+
const rightTime = timestamp(right);
|
|
5225
|
+
if (leftTime === void 0 || rightTime === void 0) return right.localeCompare(left);
|
|
5226
|
+
return rightTime - leftTime;
|
|
4735
5227
|
}
|
|
4736
5228
|
|
|
4737
5229
|
// src/core/metrics.ts
|
|
@@ -5055,11 +5547,13 @@ async function removeStored(deps, workspaceRoot) {
|
|
|
5055
5547
|
} catch {
|
|
5056
5548
|
}
|
|
5057
5549
|
}
|
|
5058
|
-
async function writeStored(deps, workspaceRoot, resolved, raw) {
|
|
5550
|
+
async function writeStored(deps, workspaceRoot, resolved, raw, metrics) {
|
|
5059
5551
|
const store = deps.store;
|
|
5060
|
-
if (store === void 0) return;
|
|
5552
|
+
if (store === void 0) return null;
|
|
5553
|
+
const coversAll = raw.tickets.length >= raw.total;
|
|
5061
5554
|
const entry = {
|
|
5062
5555
|
version: SNAPSHOT_STORE_VERSION,
|
|
5556
|
+
libraryVersion: 0,
|
|
5063
5557
|
workspaceRoot,
|
|
5064
5558
|
backend: resolved.kind,
|
|
5065
5559
|
repository: raw.repository,
|
|
@@ -5069,12 +5563,13 @@ async function writeStored(deps, workspaceRoot, resolved, raw) {
|
|
|
5069
5563
|
fetchedAt: raw.fetchedAt,
|
|
5070
5564
|
loaded: raw.tickets.length,
|
|
5071
5565
|
total: raw.total,
|
|
5072
|
-
...raw.windowLimit === void 0 ? {} : { windowLimit: raw.windowLimit },
|
|
5566
|
+
...coversAll || raw.windowLimit === void 0 ? {} : { windowLimit: raw.windowLimit },
|
|
5073
5567
|
hierarchy: raw.hierarchy,
|
|
5568
|
+
metrics,
|
|
5074
5569
|
tickets: raw.tickets
|
|
5075
5570
|
};
|
|
5076
5571
|
try {
|
|
5077
|
-
await store.write(entry);
|
|
5572
|
+
return await store.write(entry);
|
|
5078
5573
|
} catch (error) {
|
|
5079
5574
|
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5080
5575
|
result: "error",
|
|
@@ -5083,9 +5578,10 @@ async function writeStored(deps, workspaceRoot, resolved, raw) {
|
|
|
5083
5578
|
code: ERROR_CODES.internal,
|
|
5084
5579
|
...error instanceof Error ? { reason: error.message } : {}
|
|
5085
5580
|
});
|
|
5581
|
+
return null;
|
|
5086
5582
|
}
|
|
5087
5583
|
}
|
|
5088
|
-
function buildSnapshot(kind, repositoryHost, raw, tickets, blockersFinalized, metrics, source, stale, refreshError) {
|
|
5584
|
+
function buildSnapshot(kind, repositoryHost, raw, tickets, blockersFinalized, metrics, source, stale, refreshError, libraryVersion) {
|
|
5089
5585
|
return {
|
|
5090
5586
|
backend: kind,
|
|
5091
5587
|
repository: raw.repository,
|
|
@@ -5096,37 +5592,40 @@ function buildSnapshot(kind, repositoryHost, raw, tickets, blockersFinalized, me
|
|
|
5096
5592
|
total: raw.total,
|
|
5097
5593
|
windowLimit: raw.windowLimit ?? tickets.length,
|
|
5098
5594
|
hierarchy: raw.hierarchy,
|
|
5595
|
+
libraryVersion,
|
|
5099
5596
|
blockersFinalized,
|
|
5100
5597
|
metrics,
|
|
5101
5598
|
source,
|
|
5102
5599
|
stale,
|
|
5103
5600
|
refreshError,
|
|
5104
|
-
tickets: tickets.map(
|
|
5105
|
-
|
|
5106
|
-
|
|
5107
|
-
|
|
5108
|
-
|
|
5109
|
-
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5115
|
-
|
|
5116
|
-
|
|
5117
|
-
|
|
5118
|
-
|
|
5119
|
-
|
|
5120
|
-
|
|
5121
|
-
|
|
5122
|
-
|
|
5123
|
-
}
|
|
5601
|
+
tickets: tickets.map(toTicketSnapshot)
|
|
5602
|
+
};
|
|
5603
|
+
}
|
|
5604
|
+
function toTicketSnapshot(ticket) {
|
|
5605
|
+
return {
|
|
5606
|
+
ref: ticket.ref,
|
|
5607
|
+
number: ticket.number,
|
|
5608
|
+
title: ticket.title,
|
|
5609
|
+
state: ticket.state,
|
|
5610
|
+
category: ticket.category,
|
|
5611
|
+
status: ticket.status,
|
|
5612
|
+
labels: ticket.labels,
|
|
5613
|
+
author: ticket.author,
|
|
5614
|
+
assignees: ticket.assignees,
|
|
5615
|
+
skillAction: ticket.skillAction,
|
|
5616
|
+
skillActionReason: ticket.skillActionReason,
|
|
5617
|
+
createdAt: ticket.createdAt,
|
|
5618
|
+
updatedAt: ticket.updatedAt,
|
|
5619
|
+
openBlockers: ticket.openBlockers,
|
|
5620
|
+
...ticket.closedAt === void 0 ? {} : { closedAt: ticket.closedAt },
|
|
5621
|
+
...ticket.url === void 0 ? {} : { url: ticket.url },
|
|
5622
|
+
...ticket.blockedBy === void 0 ? {} : { blockedBy: ticket.blockedBy },
|
|
5623
|
+
...ticket.parent === void 0 ? {} : { parent: ticket.parent }
|
|
5124
5624
|
};
|
|
5125
5625
|
}
|
|
5126
5626
|
function buildStoredSnapshot(deps, stored, state) {
|
|
5127
5627
|
const raw = rawOf(stored);
|
|
5128
5628
|
const tickets = withSkillActions(classifyTickets(raw.tickets, deps.labels), deps.labels);
|
|
5129
|
-
const metrics = metricsOf(deps, stored.backend, raw, tickets, false);
|
|
5130
5629
|
return {
|
|
5131
5630
|
backend: stored.backend,
|
|
5132
5631
|
repository: raw.repository,
|
|
@@ -5137,31 +5636,13 @@ function buildStoredSnapshot(deps, stored, state) {
|
|
|
5137
5636
|
total: raw.total,
|
|
5138
5637
|
windowLimit: raw.windowLimit ?? SNAPSHOT_PAGE_SIZE,
|
|
5139
5638
|
hierarchy: stored.hierarchy,
|
|
5639
|
+
libraryVersion: stored.libraryVersion,
|
|
5140
5640
|
blockersFinalized: false,
|
|
5141
|
-
metrics,
|
|
5641
|
+
metrics: stored.metrics,
|
|
5142
5642
|
source: "cache",
|
|
5143
5643
|
stale: state.stale,
|
|
5144
5644
|
refreshError: state.refreshError,
|
|
5145
|
-
tickets: tickets.map(
|
|
5146
|
-
ref: ticket.ref,
|
|
5147
|
-
number: ticket.number,
|
|
5148
|
-
title: ticket.title,
|
|
5149
|
-
state: ticket.state,
|
|
5150
|
-
category: ticket.category,
|
|
5151
|
-
status: ticket.status,
|
|
5152
|
-
labels: ticket.labels,
|
|
5153
|
-
author: ticket.author,
|
|
5154
|
-
assignees: ticket.assignees,
|
|
5155
|
-
skillAction: ticket.skillAction,
|
|
5156
|
-
skillActionReason: ticket.skillActionReason,
|
|
5157
|
-
createdAt: ticket.createdAt,
|
|
5158
|
-
updatedAt: ticket.updatedAt,
|
|
5159
|
-
openBlockers: ticket.openBlockers,
|
|
5160
|
-
...ticket.closedAt === void 0 ? {} : { closedAt: ticket.closedAt },
|
|
5161
|
-
...ticket.url === void 0 ? {} : { url: ticket.url },
|
|
5162
|
-
...ticket.blockedBy === void 0 ? {} : { blockedBy: ticket.blockedBy },
|
|
5163
|
-
...ticket.parent === void 0 ? {} : { parent: ticket.parent }
|
|
5164
|
-
}))
|
|
5645
|
+
tickets: tickets.map(toTicketSnapshot)
|
|
5165
5646
|
};
|
|
5166
5647
|
}
|
|
5167
5648
|
function storedEntry(stored, resolved, snapshot) {
|
|
@@ -5249,11 +5730,297 @@ function latestWrite(entries) {
|
|
|
5249
5730
|
return latest;
|
|
5250
5731
|
}
|
|
5251
5732
|
|
|
5252
|
-
// src/host/snapshot-
|
|
5733
|
+
// src/host/snapshot-window.ts
|
|
5734
|
+
function windowFor(current, base) {
|
|
5735
|
+
const cached = current?.raw.windowLimit ?? 0;
|
|
5736
|
+
const stored = base?.windowLimit ?? 0;
|
|
5737
|
+
const loaded = current?.raw.tickets.length ?? 0;
|
|
5738
|
+
return Math.min(SNAPSHOT_MAX_WINDOW, Math.max(cached, stored, loaded, SNAPSHOT_PAGE_SIZE));
|
|
5739
|
+
}
|
|
5740
|
+
function mergePulled(current, raw) {
|
|
5741
|
+
if (current === void 0) return raw;
|
|
5742
|
+
if (current.raw.repository !== raw.repository) return raw;
|
|
5743
|
+
const existingWindow = current.raw.windowLimit ?? current.raw.tickets.length;
|
|
5744
|
+
const pulledWindow = raw.windowLimit ?? raw.tickets.length;
|
|
5745
|
+
const window = Math.max(existingWindow, pulledWindow);
|
|
5746
|
+
const shrunk = raw.tickets.length < current.raw.tickets.length;
|
|
5747
|
+
if (!shrunk && window <= pulledWindow) return raw;
|
|
5748
|
+
const tickets = mergeTickets(current.raw.tickets, raw.tickets, window);
|
|
5749
|
+
return { ...raw, loaded: tickets.length, tickets, windowLimit: window };
|
|
5750
|
+
}
|
|
5751
|
+
async function loadMore(host, workspaceRoot) {
|
|
5752
|
+
const initial = await host.getSnapshot(workspaceRoot);
|
|
5753
|
+
if (!initial.ok) return initial;
|
|
5754
|
+
const entry = host.cache.get(workspaceRoot);
|
|
5755
|
+
if (entry === void 0) return initial;
|
|
5756
|
+
const windowLimit = entry.raw.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
5757
|
+
if (entry.raw.tickets.length >= entry.raw.total || windowLimit >= SNAPSHOT_MAX_WINDOW) {
|
|
5758
|
+
return { ok: true, result: entry.snapshot };
|
|
5759
|
+
}
|
|
5760
|
+
const nextWindow = Math.min(windowLimit + SNAPSHOT_PAGE_SIZE, SNAPSHOT_MAX_WINDOW);
|
|
5761
|
+
const startedAt = host.now();
|
|
5762
|
+
let kind;
|
|
5763
|
+
try {
|
|
5764
|
+
const resolved = await host.resolve(workspaceRoot);
|
|
5765
|
+
kind = resolved.kind;
|
|
5766
|
+
const raw = await resolved.tracker.fetchSnapshot(workspaceRoot, nextWindow);
|
|
5767
|
+
const viewer = raw.viewer ?? (entry.raw.repository === raw.repository ? entry.raw.viewer : null);
|
|
5768
|
+
return await host.publish(
|
|
5769
|
+
workspaceRoot,
|
|
5770
|
+
resolved,
|
|
5771
|
+
{ ...raw, viewer, windowLimit: nextWindow },
|
|
5772
|
+
startedAt
|
|
5773
|
+
);
|
|
5774
|
+
} catch (error) {
|
|
5775
|
+
const mapped = mapSnapshotError(error);
|
|
5776
|
+
host.log("warn", LOG_EVENTS.snapshot, {
|
|
5777
|
+
result: "error",
|
|
5778
|
+
...kind === void 0 ? {} : { backend: kind },
|
|
5779
|
+
stage: SNAPSHOT_STAGES.list,
|
|
5780
|
+
code: mapped.code,
|
|
5781
|
+
durationMs: Math.max(0, host.now() - startedAt)
|
|
5782
|
+
});
|
|
5783
|
+
return snapshotFailure(mapped.code, mapped.message);
|
|
5784
|
+
}
|
|
5785
|
+
}
|
|
5786
|
+
|
|
5787
|
+
// src/host/snapshot-refresh.ts
|
|
5253
5788
|
var INCREMENTAL_OVERLAP_MS = 6e4;
|
|
5789
|
+
async function refreshSnapshot(host, workspaceRoot, full, ifVersion) {
|
|
5790
|
+
const startedAt = host.now();
|
|
5791
|
+
if (!full) {
|
|
5792
|
+
const cooling = coolingReject(host, workspaceRoot, startedAt);
|
|
5793
|
+
if (cooling !== null) return cooling;
|
|
5794
|
+
}
|
|
5795
|
+
const failWith = (mapped) => {
|
|
5796
|
+
host.guard.noteFailure(workspaceRoot, startedAt, mapped.retryAfterMs);
|
|
5797
|
+
return keepOldSnapshot(host, workspaceRoot, mapped, startedAt);
|
|
5798
|
+
};
|
|
5799
|
+
let resolved;
|
|
5800
|
+
try {
|
|
5801
|
+
resolved = await host.resolve(workspaceRoot);
|
|
5802
|
+
} catch (error) {
|
|
5803
|
+
return failWith(mapSnapshotError(error));
|
|
5804
|
+
}
|
|
5805
|
+
const stored = await readStored(host, workspaceRoot);
|
|
5806
|
+
const base = stored?.repositoryKey === repositoryKey(resolved.kind, resolved.address) ? stored : void 0;
|
|
5807
|
+
const tracker = resolved.tracker;
|
|
5808
|
+
let raw;
|
|
5809
|
+
let mode = "full";
|
|
5810
|
+
if (base !== void 0 && !full && tracker.fetchChanges !== void 0 && // 对账以「全量覆盖」为前提:窗口截断的后端看不到窗口外的删除。
|
|
5811
|
+
base.windowLimit === void 0) {
|
|
5812
|
+
const outcome = await pullIncremental(host, workspaceRoot, resolved, base, startedAt);
|
|
5813
|
+
if (outcome.kind === "failure") {
|
|
5814
|
+
return failWith(outcome.error);
|
|
5815
|
+
}
|
|
5816
|
+
if (outcome.kind === "merged") {
|
|
5817
|
+
raw = outcome.raw;
|
|
5818
|
+
mode = "incremental";
|
|
5819
|
+
}
|
|
5820
|
+
}
|
|
5821
|
+
if (raw === void 0) {
|
|
5822
|
+
try {
|
|
5823
|
+
const fetched = await tracker.fetchSnapshot(
|
|
5824
|
+
workspaceRoot,
|
|
5825
|
+
windowFor(host.cache.get(workspaceRoot), base)
|
|
5826
|
+
);
|
|
5827
|
+
const current = host.cache.get(workspaceRoot);
|
|
5828
|
+
const hostName = resolved.kind === "markdown" ? null : resolved.address?.host ?? null;
|
|
5829
|
+
const sameRepository = current?.kind === resolved.kind && current.repositoryHost === hostName && current.raw.repository === fetched.repository;
|
|
5830
|
+
const previousViewer = (sameRepository ? current.raw.viewer : null) ?? base?.viewer ?? null;
|
|
5831
|
+
raw = fetched.viewer === null ? { ...fetched, viewer: previousViewer } : fetched;
|
|
5832
|
+
} catch (error) {
|
|
5833
|
+
return failWith(mapSnapshotError(error));
|
|
5834
|
+
}
|
|
5835
|
+
}
|
|
5836
|
+
if (base !== void 0 && !ticketsDiffer(base, raw)) {
|
|
5837
|
+
return respondUnchanged(host, workspaceRoot, resolved, base, raw, ifVersion, startedAt);
|
|
5838
|
+
}
|
|
5839
|
+
const published = await host.publish(workspaceRoot, resolved, raw, startedAt, mode);
|
|
5840
|
+
if (!published.ok) {
|
|
5841
|
+
host.guard.noteFailure(workspaceRoot, host.now());
|
|
5842
|
+
return published;
|
|
5843
|
+
}
|
|
5844
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5845
|
+
return shapeDelta(host, workspaceRoot, published.result, ifVersion);
|
|
5846
|
+
}
|
|
5847
|
+
function coolingReject(host, workspaceRoot, startedAt) {
|
|
5848
|
+
const coolingUntil = host.guard.blockedUntil(workspaceRoot, startedAt);
|
|
5849
|
+
if (coolingUntil === null) return null;
|
|
5850
|
+
return keepOldSnapshot(
|
|
5851
|
+
host,
|
|
5852
|
+
workspaceRoot,
|
|
5853
|
+
{
|
|
5854
|
+
code: ERROR_CODES.fetchFailed,
|
|
5855
|
+
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"
|
|
5856
|
+
},
|
|
5857
|
+
startedAt
|
|
5858
|
+
);
|
|
5859
|
+
}
|
|
5860
|
+
async function pullIncremental(host, workspaceRoot, resolved, base, startedAt) {
|
|
5861
|
+
const tracker = resolved.tracker;
|
|
5862
|
+
if (tracker.fetchChanges === void 0) return { kind: "fallback" };
|
|
5863
|
+
let changes;
|
|
5864
|
+
try {
|
|
5865
|
+
changes = await tracker.fetchChanges(workspaceRoot, base.fetchedAt - INCREMENTAL_OVERLAP_MS);
|
|
5866
|
+
} catch (error) {
|
|
5867
|
+
const mapped = mapSnapshotError(error);
|
|
5868
|
+
return { kind: "failure", error: mapped };
|
|
5869
|
+
}
|
|
5870
|
+
if (changes.truncated) {
|
|
5871
|
+
logIncrementalFallback(host, resolved.kind, "truncated", startedAt);
|
|
5872
|
+
return { kind: "fallback" };
|
|
5873
|
+
}
|
|
5874
|
+
const total = changes.total;
|
|
5875
|
+
if (!Number.isInteger(total) || total < 0) {
|
|
5876
|
+
logIncrementalFallback(host, resolved.kind, "invalid-total", startedAt);
|
|
5877
|
+
return { kind: "fallback" };
|
|
5878
|
+
}
|
|
5879
|
+
if (changes.hierarchy !== base.hierarchy) {
|
|
5880
|
+
logIncrementalFallback(host, resolved.kind, "hierarchy", startedAt);
|
|
5881
|
+
return { kind: "fallback" };
|
|
5882
|
+
}
|
|
5883
|
+
const merged = mergeTickets(base.tickets, changes.tickets);
|
|
5884
|
+
if (merged.length > total) {
|
|
5885
|
+
logIncrementalFallback(host, resolved.kind, "drift", startedAt);
|
|
5886
|
+
return { kind: "fallback" };
|
|
5887
|
+
}
|
|
5888
|
+
return {
|
|
5889
|
+
kind: "merged",
|
|
5890
|
+
raw: {
|
|
5891
|
+
repository: base.repository,
|
|
5892
|
+
fetchedAt: host.now(),
|
|
5893
|
+
loaded: merged.length,
|
|
5894
|
+
total,
|
|
5895
|
+
hierarchy: base.hierarchy,
|
|
5896
|
+
tickets: merged,
|
|
5897
|
+
// 后端这次查了身份就用新值;没查(省略)时沿用基准数据里的身份。
|
|
5898
|
+
viewer: changes.viewer === void 0 ? base.viewer : changes.viewer,
|
|
5899
|
+
windowLimit: Math.max(base.tickets.length, SNAPSHOT_PAGE_SIZE)
|
|
5900
|
+
}
|
|
5901
|
+
};
|
|
5902
|
+
}
|
|
5903
|
+
function logIncrementalFallback(deps, backend, reason, startedAt) {
|
|
5904
|
+
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5905
|
+
result: "fallback",
|
|
5906
|
+
backend,
|
|
5907
|
+
stage: SNAPSHOT_STAGES.list,
|
|
5908
|
+
reason,
|
|
5909
|
+
durationMs: Math.max(0, deps.now() - startedAt)
|
|
5910
|
+
});
|
|
5911
|
+
}
|
|
5912
|
+
function ticketsDiffer(base, raw) {
|
|
5913
|
+
if (base.total !== raw.total || base.viewer !== raw.viewer) return true;
|
|
5914
|
+
return JSON.stringify(base.tickets) !== JSON.stringify(raw.tickets);
|
|
5915
|
+
}
|
|
5916
|
+
async function respondUnchanged(host, workspaceRoot, resolved, base, raw, ifVersion, startedAt) {
|
|
5917
|
+
if (ifVersion !== void 0 && ifVersion === base.libraryVersion) {
|
|
5918
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5919
|
+
return {
|
|
5920
|
+
ok: true,
|
|
5921
|
+
result: { kind: "not-modified", version: base.libraryVersion, checkedAt: host.now() }
|
|
5922
|
+
};
|
|
5923
|
+
}
|
|
5924
|
+
if (ifVersion !== void 0 && ifVersion < base.libraryVersion) {
|
|
5925
|
+
const changes = await host.store?.changesSince(workspaceRoot, ifVersion);
|
|
5926
|
+
if (changes !== void 0) {
|
|
5927
|
+
const delta = buildDelta(host, base.libraryVersion, raw.fetchedAt, changes, base);
|
|
5928
|
+
if (delta !== void 0) {
|
|
5929
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5930
|
+
return { ok: true, result: delta };
|
|
5931
|
+
}
|
|
5932
|
+
}
|
|
5933
|
+
}
|
|
5934
|
+
const published = await host.publish(workspaceRoot, resolved, raw, startedAt, "full", {
|
|
5935
|
+
skipWrite: true,
|
|
5936
|
+
versionFallback: base.libraryVersion
|
|
5937
|
+
});
|
|
5938
|
+
if (!published.ok) {
|
|
5939
|
+
host.guard.noteFailure(workspaceRoot, host.now());
|
|
5940
|
+
return published;
|
|
5941
|
+
}
|
|
5942
|
+
host.guard.noteSuccess(workspaceRoot);
|
|
5943
|
+
return { ok: true, result: { kind: "full", snapshot: published.result } };
|
|
5944
|
+
}
|
|
5945
|
+
async function shapeDelta(host, workspaceRoot, snapshot, ifVersion) {
|
|
5946
|
+
const full = { kind: "full", snapshot };
|
|
5947
|
+
if (ifVersion === void 0 || snapshot.libraryVersion === null) return { ok: true, result: full };
|
|
5948
|
+
if (ifVersion === snapshot.libraryVersion) return { ok: true, result: full };
|
|
5949
|
+
const changes = await host.store?.changesSince(workspaceRoot, ifVersion);
|
|
5950
|
+
if (changes === void 0) return { ok: true, result: full };
|
|
5951
|
+
const delta = buildDelta(
|
|
5952
|
+
host,
|
|
5953
|
+
snapshot.libraryVersion,
|
|
5954
|
+
snapshot.fetchedAt,
|
|
5955
|
+
changes,
|
|
5956
|
+
void 0,
|
|
5957
|
+
snapshot
|
|
5958
|
+
);
|
|
5959
|
+
if (delta === void 0) return { ok: true, result: full };
|
|
5960
|
+
return { ok: true, result: delta };
|
|
5961
|
+
}
|
|
5962
|
+
function buildDelta(host, version, fetchedAt, changes, base, snapshot) {
|
|
5963
|
+
if (changes.tickets.length + changes.removed.length > SNAPSHOT_DELTA_MAX_ROWS) return void 0;
|
|
5964
|
+
const metrics = snapshot?.metrics ?? base?.metrics;
|
|
5965
|
+
const viewer = snapshot?.viewer ?? base?.viewer ?? null;
|
|
5966
|
+
const total = snapshot?.total ?? base?.total ?? 0;
|
|
5967
|
+
const loaded = snapshot?.loaded ?? base?.tickets.length ?? 0;
|
|
5968
|
+
const windowLimit = snapshot?.windowLimit ?? base?.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
5969
|
+
if (metrics === void 0) return void 0;
|
|
5970
|
+
const rows = withSkillActions(classifyTickets(changes.tickets, host.labels), host.labels).map(
|
|
5971
|
+
toTicketSnapshot
|
|
5972
|
+
);
|
|
5973
|
+
const anyOpen = base?.tickets.some((ticket) => ticket.state === "open") ?? false;
|
|
5974
|
+
const blockersFinalized = snapshot?.blockersFinalized ?? !anyOpen;
|
|
5975
|
+
return {
|
|
5976
|
+
kind: "changes",
|
|
5977
|
+
version,
|
|
5978
|
+
fetchedAt,
|
|
5979
|
+
tickets: rows,
|
|
5980
|
+
removed: changes.removed,
|
|
5981
|
+
metrics,
|
|
5982
|
+
viewer,
|
|
5983
|
+
total,
|
|
5984
|
+
loaded,
|
|
5985
|
+
windowLimit,
|
|
5986
|
+
blockersFinalized
|
|
5987
|
+
};
|
|
5988
|
+
}
|
|
5989
|
+
async function keepOldSnapshot(host, workspaceRoot, mapped, startedAt) {
|
|
5990
|
+
const refreshError = { code: mapped.code, message: mapped.message };
|
|
5991
|
+
const cached = host.cache.get(workspaceRoot);
|
|
5992
|
+
if (cached !== void 0) {
|
|
5993
|
+
const snapshot2 = { ...cached.snapshot, stale: true, refreshError };
|
|
5994
|
+
cached.snapshot = snapshot2;
|
|
5995
|
+
host.log("warn", LOG_EVENTS.snapshot, {
|
|
5996
|
+
result: "error",
|
|
5997
|
+
backend: cached.kind,
|
|
5998
|
+
stage: SNAPSHOT_STAGES.list,
|
|
5999
|
+
code: mapped.code,
|
|
6000
|
+
durationMs: Math.max(0, host.now() - startedAt)
|
|
6001
|
+
});
|
|
6002
|
+
return { ok: true, result: { kind: "full", snapshot: snapshot2 } };
|
|
6003
|
+
}
|
|
6004
|
+
const stored = await readStored(host, workspaceRoot);
|
|
6005
|
+
if (stored === void 0) return { ok: false, error: refreshError };
|
|
6006
|
+
const snapshot = buildStoredSnapshot(host, stored, { stale: true, refreshError });
|
|
6007
|
+
host.cache.set(workspaceRoot, storedEntry(stored, null, snapshot));
|
|
6008
|
+
host.log("warn", LOG_EVENTS.snapshot, {
|
|
6009
|
+
result: "error",
|
|
6010
|
+
backend: stored.backend,
|
|
6011
|
+
stage: SNAPSHOT_STAGES.list,
|
|
6012
|
+
code: mapped.code,
|
|
6013
|
+
durationMs: Math.max(0, host.now() - startedAt)
|
|
6014
|
+
});
|
|
6015
|
+
return { ok: true, result: { kind: "full", snapshot } };
|
|
6016
|
+
}
|
|
6017
|
+
|
|
6018
|
+
// src/host/snapshot-service.ts
|
|
5254
6019
|
function createSnapshotService(deps) {
|
|
5255
6020
|
const cache = /* @__PURE__ */ new Map();
|
|
5256
6021
|
const loading = /* @__PURE__ */ new Map();
|
|
6022
|
+
const refreshing = /* @__PURE__ */ new Map();
|
|
6023
|
+
const guard = createRefreshGuard();
|
|
5257
6024
|
const more = {
|
|
5258
6025
|
resolve: deps.resolve,
|
|
5259
6026
|
now: deps.now,
|
|
@@ -5262,6 +6029,16 @@ function createSnapshotService(deps) {
|
|
|
5262
6029
|
getSnapshot: (workspaceRoot) => getSnapshot(deps, cache, loading, workspaceRoot),
|
|
5263
6030
|
publish: (workspaceRoot, resolved, raw, startedAt) => publish(deps, cache, workspaceRoot, resolved, raw, startedAt, "more")
|
|
5264
6031
|
};
|
|
6032
|
+
const refreshHost = {
|
|
6033
|
+
resolve: deps.resolve,
|
|
6034
|
+
now: deps.now,
|
|
6035
|
+
labels: deps.labels,
|
|
6036
|
+
log: deps.log,
|
|
6037
|
+
...deps.store === void 0 ? {} : { store: deps.store },
|
|
6038
|
+
cache,
|
|
6039
|
+
guard,
|
|
6040
|
+
publish: (workspaceRoot, resolved, raw, startedAt, mode, options) => publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mode, options)
|
|
6041
|
+
};
|
|
5265
6042
|
return {
|
|
5266
6043
|
handle(method, params) {
|
|
5267
6044
|
if (!isSnapshotMethod(method)) {
|
|
@@ -5279,10 +6056,29 @@ function createSnapshotService(deps) {
|
|
|
5279
6056
|
if (method === RPC_METHODS.snapshotResolveBlockers) {
|
|
5280
6057
|
return resolveBlockers(deps, cache, loading, workspaceRoot);
|
|
5281
6058
|
}
|
|
5282
|
-
|
|
5283
|
-
|
|
6059
|
+
return loadMore(more, workspaceRoot);
|
|
6060
|
+
},
|
|
6061
|
+
refresh(params) {
|
|
6062
|
+
const workspaceRoot = readWorkspaceRoot2(params);
|
|
6063
|
+
if (workspaceRoot === void 0) {
|
|
6064
|
+
return Promise.resolve(
|
|
6065
|
+
snapshotFailure(ERROR_CODES.invalidParams, "\u7F3A\u5C11\u975E\u7A7A\u7684 workspaceRoot \u53C2\u6570\u3002")
|
|
6066
|
+
);
|
|
5284
6067
|
}
|
|
5285
|
-
|
|
6068
|
+
const inflight = refreshing.get(workspaceRoot);
|
|
6069
|
+
if (inflight !== void 0) return inflight;
|
|
6070
|
+
const task = refreshSnapshot(
|
|
6071
|
+
refreshHost,
|
|
6072
|
+
workspaceRoot,
|
|
6073
|
+
readFullFlag(params),
|
|
6074
|
+
readIfVersion(params)
|
|
6075
|
+
);
|
|
6076
|
+
refreshing.set(workspaceRoot, task);
|
|
6077
|
+
const clear = () => {
|
|
6078
|
+
if (refreshing.get(workspaceRoot) === task) refreshing.delete(workspaceRoot);
|
|
6079
|
+
};
|
|
6080
|
+
void task.then(clear, clear);
|
|
6081
|
+
return task;
|
|
5286
6082
|
},
|
|
5287
6083
|
cache(params) {
|
|
5288
6084
|
if (readWorkspaceRoot2(params) === void 0) {
|
|
@@ -5296,11 +6092,17 @@ function createSnapshotService(deps) {
|
|
|
5296
6092
|
};
|
|
5297
6093
|
}
|
|
5298
6094
|
function isSnapshotMethod(method) {
|
|
5299
|
-
return method === RPC_METHODS.snapshotGet || method === RPC_METHODS.
|
|
6095
|
+
return method === RPC_METHODS.snapshotGet || method === RPC_METHODS.snapshotResolveBlockers || method === RPC_METHODS.snapshotMore;
|
|
5300
6096
|
}
|
|
5301
6097
|
function readFullFlag(params) {
|
|
5302
6098
|
return isRecord(params) && params.full === true;
|
|
5303
6099
|
}
|
|
6100
|
+
function readIfVersion(params) {
|
|
6101
|
+
if (!isRecord(params)) return void 0;
|
|
6102
|
+
const value = params.ifVersion;
|
|
6103
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) return void 0;
|
|
6104
|
+
return value;
|
|
6105
|
+
}
|
|
5304
6106
|
function getSnapshot(deps, cache, loading, workspaceRoot) {
|
|
5305
6107
|
const cached = cache.get(workspaceRoot);
|
|
5306
6108
|
if (cached !== void 0) return Promise.resolve({ ok: true, result: cached.snapshot });
|
|
@@ -5309,7 +6111,7 @@ function getSnapshot(deps, cache, loading, workspaceRoot) {
|
|
|
5309
6111
|
function loadWorkspace(deps, cache, loading, workspaceRoot) {
|
|
5310
6112
|
const running = loading.get(workspaceRoot);
|
|
5311
6113
|
if (running !== void 0) return running;
|
|
5312
|
-
const task =
|
|
6114
|
+
const task = readWorkspace2(deps, cache, workspaceRoot);
|
|
5313
6115
|
loading.set(workspaceRoot, task);
|
|
5314
6116
|
void task.then(
|
|
5315
6117
|
() => {
|
|
@@ -5321,7 +6123,7 @@ function loadWorkspace(deps, cache, loading, workspaceRoot) {
|
|
|
5321
6123
|
);
|
|
5322
6124
|
return task;
|
|
5323
6125
|
}
|
|
5324
|
-
async function
|
|
6126
|
+
async function readWorkspace2(deps, cache, workspaceRoot) {
|
|
5325
6127
|
const stored = await readStored(deps, workspaceRoot);
|
|
5326
6128
|
if (stored !== void 0) {
|
|
5327
6129
|
const served = await serveStored(deps, cache, workspaceRoot, stored);
|
|
@@ -5354,87 +6156,6 @@ async function serveStored(deps, cache, workspaceRoot, stored) {
|
|
|
5354
6156
|
cache.set(workspaceRoot, storedEntry(stored, resolved, snapshot));
|
|
5355
6157
|
return { ok: true, result: snapshot };
|
|
5356
6158
|
}
|
|
5357
|
-
async function refreshSnapshot(deps, cache, workspaceRoot, full) {
|
|
5358
|
-
const startedAt = deps.now();
|
|
5359
|
-
let resolved;
|
|
5360
|
-
try {
|
|
5361
|
-
resolved = await deps.resolve(workspaceRoot);
|
|
5362
|
-
} catch (error) {
|
|
5363
|
-
return await keepOldSnapshot(deps, cache, workspaceRoot, mapSnapshotError(error), startedAt);
|
|
5364
|
-
}
|
|
5365
|
-
const stored = await readStored(deps, workspaceRoot);
|
|
5366
|
-
const base = stored?.repositoryKey === repositoryKey(resolved.kind, resolved.address) ? stored : void 0;
|
|
5367
|
-
const tracker = resolved.tracker;
|
|
5368
|
-
if (base !== void 0 && !full && tracker.fetchChanges !== void 0 && // 对账以「全量覆盖」为前提:窗口截断的后端看不到窗口外的删除。
|
|
5369
|
-
base.windowLimit === void 0) {
|
|
5370
|
-
const merged = await mergeIncremental(deps, cache, workspaceRoot, resolved, base, startedAt);
|
|
5371
|
-
if (merged !== void 0) return merged;
|
|
5372
|
-
}
|
|
5373
|
-
try {
|
|
5374
|
-
const raw = await tracker.fetchSnapshot(
|
|
5375
|
-
workspaceRoot,
|
|
5376
|
-
windowFor(cache.get(workspaceRoot), base)
|
|
5377
|
-
);
|
|
5378
|
-
const current = cache.get(workspaceRoot);
|
|
5379
|
-
const host = resolved.kind === "markdown" ? null : resolved.address?.host ?? null;
|
|
5380
|
-
const sameRepository = current?.kind === resolved.kind && current.repositoryHost === host && current.raw.repository === raw.repository;
|
|
5381
|
-
const previousViewer = (sameRepository ? current.raw.viewer : null) ?? base?.viewer ?? null;
|
|
5382
|
-
const withViewer = raw.viewer === null ? { ...raw, viewer: previousViewer } : raw;
|
|
5383
|
-
return await publish(deps, cache, workspaceRoot, resolved, withViewer, startedAt, "full");
|
|
5384
|
-
} catch (error) {
|
|
5385
|
-
return await keepOldSnapshot(deps, cache, workspaceRoot, mapSnapshotError(error), startedAt);
|
|
5386
|
-
}
|
|
5387
|
-
}
|
|
5388
|
-
async function mergeIncremental(deps, cache, workspaceRoot, resolved, base, startedAt) {
|
|
5389
|
-
const tracker = resolved.tracker;
|
|
5390
|
-
if (tracker.fetchChanges === void 0) return void 0;
|
|
5391
|
-
let changes;
|
|
5392
|
-
try {
|
|
5393
|
-
changes = await tracker.fetchChanges(workspaceRoot, base.fetchedAt - INCREMENTAL_OVERLAP_MS);
|
|
5394
|
-
} catch (error) {
|
|
5395
|
-
return await keepOldSnapshot(deps, cache, workspaceRoot, mapSnapshotError(error), startedAt);
|
|
5396
|
-
}
|
|
5397
|
-
if (changes.truncated) {
|
|
5398
|
-
logIncrementalFallback(deps, resolved.kind, "truncated", startedAt);
|
|
5399
|
-
return void 0;
|
|
5400
|
-
}
|
|
5401
|
-
const total = changes.total;
|
|
5402
|
-
if (!Number.isInteger(total) || total < 0) {
|
|
5403
|
-
logIncrementalFallback(deps, resolved.kind, "invalid-total", startedAt);
|
|
5404
|
-
return void 0;
|
|
5405
|
-
}
|
|
5406
|
-
if (changes.hierarchy !== base.hierarchy) {
|
|
5407
|
-
logIncrementalFallback(deps, resolved.kind, "hierarchy", startedAt);
|
|
5408
|
-
return void 0;
|
|
5409
|
-
}
|
|
5410
|
-
const windowLimit = base.windowLimit ?? SNAPSHOT_PAGE_SIZE;
|
|
5411
|
-
const merged = mergeTickets(base.tickets, changes.tickets, windowLimit);
|
|
5412
|
-
if (merged.length > total) {
|
|
5413
|
-
logIncrementalFallback(deps, resolved.kind, "drift", startedAt);
|
|
5414
|
-
return void 0;
|
|
5415
|
-
}
|
|
5416
|
-
const raw = {
|
|
5417
|
-
repository: base.repository,
|
|
5418
|
-
fetchedAt: deps.now(),
|
|
5419
|
-
loaded: merged.length,
|
|
5420
|
-
total,
|
|
5421
|
-
hierarchy: base.hierarchy,
|
|
5422
|
-
tickets: merged,
|
|
5423
|
-
// 后端这次查了身份就用新值;没查(省略)时沿用基准数据里的身份。
|
|
5424
|
-
viewer: changes.viewer === void 0 ? base.viewer : changes.viewer,
|
|
5425
|
-
windowLimit
|
|
5426
|
-
};
|
|
5427
|
-
return await publish(deps, cache, workspaceRoot, resolved, raw, startedAt, "incremental");
|
|
5428
|
-
}
|
|
5429
|
-
function logIncrementalFallback(deps, backend, reason, startedAt) {
|
|
5430
|
-
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5431
|
-
result: "fallback",
|
|
5432
|
-
backend,
|
|
5433
|
-
stage: SNAPSHOT_STAGES.list,
|
|
5434
|
-
reason,
|
|
5435
|
-
durationMs: Math.max(0, deps.now() - startedAt)
|
|
5436
|
-
});
|
|
5437
|
-
}
|
|
5438
6159
|
async function fetchAndCache(deps, cache, workspaceRoot) {
|
|
5439
6160
|
const startedAt = deps.now();
|
|
5440
6161
|
let kind;
|
|
@@ -5455,7 +6176,7 @@ async function fetchAndCache(deps, cache, workspaceRoot) {
|
|
|
5455
6176
|
return snapshotFailure(mapped.code, mapped.message);
|
|
5456
6177
|
}
|
|
5457
6178
|
}
|
|
5458
|
-
async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mode) {
|
|
6179
|
+
async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mode, options) {
|
|
5459
6180
|
const repositoryHost = resolved.kind === "markdown" ? null : resolved.address?.host ?? null;
|
|
5460
6181
|
const current = cache.get(workspaceRoot);
|
|
5461
6182
|
const merged = mergePulled(current, raw);
|
|
@@ -5463,6 +6184,7 @@ async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mod
|
|
|
5463
6184
|
const resolver = resolved.tracker.resolveBlockers ?? null;
|
|
5464
6185
|
const finalized = resolver === null || !tickets.some((ticket) => ticket.state === "open");
|
|
5465
6186
|
const metrics = metricsOf(deps, resolved.kind, merged, tickets, finalized);
|
|
6187
|
+
const libraryVersion = options?.skipWrite ? options.versionFallback ?? null : await writeStored(deps, workspaceRoot, resolved, merged, metrics);
|
|
5466
6188
|
const snapshot = buildSnapshot(
|
|
5467
6189
|
resolved.kind,
|
|
5468
6190
|
repositoryHost,
|
|
@@ -5472,7 +6194,8 @@ async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mod
|
|
|
5472
6194
|
metrics,
|
|
5473
6195
|
"live",
|
|
5474
6196
|
false,
|
|
5475
|
-
null
|
|
6197
|
+
null,
|
|
6198
|
+
libraryVersion
|
|
5476
6199
|
);
|
|
5477
6200
|
cache.set(workspaceRoot, {
|
|
5478
6201
|
kind: resolved.kind,
|
|
@@ -5482,45 +6205,18 @@ async function publish(deps, cache, workspaceRoot, resolved, raw, startedAt, mod
|
|
|
5482
6205
|
resolver,
|
|
5483
6206
|
pending: null
|
|
5484
6207
|
});
|
|
5485
|
-
|
|
5486
|
-
|
|
5487
|
-
|
|
5488
|
-
|
|
5489
|
-
stage: SNAPSHOT_STAGES.list,
|
|
5490
|
-
durationMs: Math.max(0, deps.now() - startedAt),
|
|
5491
|
-
repository: snapshot.repository,
|
|
5492
|
-
loaded: snapshot.loaded,
|
|
5493
|
-
total: snapshot.total,
|
|
5494
|
-
mode
|
|
5495
|
-
});
|
|
5496
|
-
return { ok: true, result: snapshot };
|
|
5497
|
-
}
|
|
5498
|
-
async function keepOldSnapshot(deps, cache, workspaceRoot, mapped, startedAt) {
|
|
5499
|
-
const refreshError = { code: mapped.code, message: mapped.message };
|
|
5500
|
-
const cached = cache.get(workspaceRoot);
|
|
5501
|
-
if (cached !== void 0) {
|
|
5502
|
-
const snapshot2 = { ...cached.snapshot, stale: true, refreshError };
|
|
5503
|
-
cached.snapshot = snapshot2;
|
|
5504
|
-
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5505
|
-
result: "error",
|
|
5506
|
-
backend: cached.kind,
|
|
6208
|
+
if (!options?.skipWrite) {
|
|
6209
|
+
deps.log("info", LOG_EVENTS.snapshot, {
|
|
6210
|
+
result: "ok",
|
|
6211
|
+
backend: resolved.kind,
|
|
5507
6212
|
stage: SNAPSHOT_STAGES.list,
|
|
5508
|
-
|
|
5509
|
-
|
|
6213
|
+
durationMs: Math.max(0, deps.now() - startedAt),
|
|
6214
|
+
repository: snapshot.repository,
|
|
6215
|
+
loaded: snapshot.loaded,
|
|
6216
|
+
total: snapshot.total,
|
|
6217
|
+
mode
|
|
5510
6218
|
});
|
|
5511
|
-
return { ok: true, result: snapshot2 };
|
|
5512
6219
|
}
|
|
5513
|
-
const stored = await readStored(deps, workspaceRoot);
|
|
5514
|
-
if (stored === void 0) return snapshotFailure(mapped.code, mapped.message);
|
|
5515
|
-
const snapshot = buildStoredSnapshot(deps, stored, { stale: true, refreshError });
|
|
5516
|
-
cache.set(workspaceRoot, storedEntry(stored, null, snapshot));
|
|
5517
|
-
deps.log("warn", LOG_EVENTS.snapshot, {
|
|
5518
|
-
result: "error",
|
|
5519
|
-
backend: stored.backend,
|
|
5520
|
-
stage: SNAPSHOT_STAGES.list,
|
|
5521
|
-
code: mapped.code,
|
|
5522
|
-
durationMs: Math.max(0, deps.now() - startedAt)
|
|
5523
|
-
});
|
|
5524
6220
|
return { ok: true, result: snapshot };
|
|
5525
6221
|
}
|
|
5526
6222
|
function resolveBlockers(deps, cache, loading, workspaceRoot) {
|
|
@@ -5562,7 +6258,8 @@ async function finalize(deps, cache, workspaceRoot, entry, resolver) {
|
|
|
5562
6258
|
metrics,
|
|
5563
6259
|
entry.snapshot.source,
|
|
5564
6260
|
entry.snapshot.stale,
|
|
5565
|
-
entry.snapshot.refreshError
|
|
6261
|
+
entry.snapshot.refreshError,
|
|
6262
|
+
entry.snapshot.libraryVersion
|
|
5566
6263
|
);
|
|
5567
6264
|
} catch (error) {
|
|
5568
6265
|
failure4 = mapSnapshotError(error);
|
|
@@ -5626,12 +6323,19 @@ async function getDetail(deps, params) {
|
|
|
5626
6323
|
if (fetchDetail === void 0) {
|
|
5627
6324
|
return unsupported(kind, "\u8BFB\u53D6\u5DE5\u5355\u8BE6\u60C5");
|
|
5628
6325
|
}
|
|
6326
|
+
const crossRepository = target.repository !== void 0;
|
|
6327
|
+
if (crossRepository && kind !== "gitea") {
|
|
6328
|
+
return unsupported(kind, "\u6253\u5F00\u8DE8\u4ED3\u5E93\u5DE5\u5355");
|
|
6329
|
+
}
|
|
5629
6330
|
const [detail, canDelete] = await Promise.all([
|
|
5630
6331
|
fetchDetail(target.workspaceRoot, referenceOf(target)),
|
|
5631
|
-
readCanDelete(resolved.tracker, target.workspaceRoot)
|
|
6332
|
+
crossRepository ? Promise.resolve(false) : readCanDelete(resolved.tracker, target.workspaceRoot)
|
|
5632
6333
|
]);
|
|
5633
6334
|
logResult(deps, "info", kind, "get", startedAt);
|
|
5634
|
-
return {
|
|
6335
|
+
return {
|
|
6336
|
+
ok: true,
|
|
6337
|
+
result: withCapabilities(kind, resolved.tracker, detail, canDelete, crossRepository)
|
|
6338
|
+
};
|
|
5635
6339
|
} catch (error) {
|
|
5636
6340
|
const mapped = mapError(error);
|
|
5637
6341
|
logResult(deps, "warn", kind, "get", startedAt, mapped.code);
|
|
@@ -5656,6 +6360,9 @@ async function update(deps, params) {
|
|
|
5656
6360
|
if (updateTicket === void 0) {
|
|
5657
6361
|
return unsupported(kind, "\u4FEE\u6539\u5DE5\u5355\u5C5E\u6027");
|
|
5658
6362
|
}
|
|
6363
|
+
if (target.repository !== void 0) {
|
|
6364
|
+
return unsupported(kind, "\u4FEE\u6539\u8DE8\u4ED3\u5E93\u5DE5\u5355");
|
|
6365
|
+
}
|
|
5659
6366
|
await updateTicket(target.workspaceRoot, referenceOf(target), patch);
|
|
5660
6367
|
await syncCachedTicket(deps, target.workspaceRoot, target.ref, patch);
|
|
5661
6368
|
logResult(deps, "info", kind, "update", startedAt);
|
|
@@ -5706,6 +6413,9 @@ async function remove(deps, params) {
|
|
|
5706
6413
|
if (deleteTicket === void 0) {
|
|
5707
6414
|
return unsupported(kind, "\u5220\u9664\u5DE5\u5355");
|
|
5708
6415
|
}
|
|
6416
|
+
if (target.repository !== void 0) {
|
|
6417
|
+
return unsupported(kind, "\u5220\u9664\u8DE8\u4ED3\u5E93\u5DE5\u5355");
|
|
6418
|
+
}
|
|
5709
6419
|
if (!await readCanDelete(resolved.tracker, target.workspaceRoot)) {
|
|
5710
6420
|
const code = ERROR_CODES.fetchFailed;
|
|
5711
6421
|
logResult(deps, "warn", kind, "delete", startedAt, code);
|
|
@@ -5768,14 +6478,14 @@ async function removeCachedTicket(deps, workspaceRoot, ref) {
|
|
|
5768
6478
|
} catch {
|
|
5769
6479
|
}
|
|
5770
6480
|
}
|
|
5771
|
-
function withCapabilities(kind, tracker, detail, canDelete) {
|
|
5772
|
-
const writable = tracker.updateTicket !== void 0;
|
|
6481
|
+
function withCapabilities(kind, tracker, detail, canDelete, readOnly) {
|
|
6482
|
+
const writable = !readOnly && tracker.updateTicket !== void 0;
|
|
5773
6483
|
const capabilities = {
|
|
5774
6484
|
labels: writable,
|
|
5775
6485
|
assignees: writable,
|
|
5776
6486
|
state: writable,
|
|
5777
6487
|
link: detail.url !== null,
|
|
5778
|
-
delete: canDelete
|
|
6488
|
+
delete: !readOnly && canDelete
|
|
5779
6489
|
};
|
|
5780
6490
|
return { ...detail, backend: kind, capabilities };
|
|
5781
6491
|
}
|
|
@@ -5828,7 +6538,11 @@ function readField(params, key) {
|
|
|
5828
6538
|
return key in params ? params[key] : void 0;
|
|
5829
6539
|
}
|
|
5830
6540
|
function referenceOf(target) {
|
|
5831
|
-
return {
|
|
6541
|
+
return {
|
|
6542
|
+
ref: target.ref,
|
|
6543
|
+
number: target.number,
|
|
6544
|
+
...target.repository === void 0 ? {} : { repository: target.repository }
|
|
6545
|
+
};
|
|
5832
6546
|
}
|
|
5833
6547
|
function unsupported(kind, action) {
|
|
5834
6548
|
return failure3(
|
|
@@ -6220,6 +6934,7 @@ function createRpcHandler(scope, run, timer, log) {
|
|
|
6220
6934
|
handle: (method, params) => {
|
|
6221
6935
|
if (method === RPC_METHODS.envCheck) return envCheck.handle(method, params);
|
|
6222
6936
|
if (method === RPC_METHODS.snapshotCache) return snapshot.cache(params);
|
|
6937
|
+
if (method === RPC_METHODS.snapshotRefresh) return snapshot.refresh(params);
|
|
6223
6938
|
if (method === RPC_METHODS.ticketGet || method === RPC_METHODS.ticketUpdate || method === RPC_METHODS.ticketOptions || method === RPC_METHODS.ticketDelete || method === RPC_METHODS.ticketAttachment) {
|
|
6224
6939
|
return ticket.handle(method, params);
|
|
6225
6940
|
}
|