dsh-github-copilot 0.4.0-alpha.128 → 0.4.0-alpha.130

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.
@@ -0,0 +1,78 @@
1
+ import { lstat, open } from "node:fs/promises";
2
+ import { isAbsolute } from "node:path";
3
+ import { TextDecoder } from "node:util";
4
+ //#region lib/types/diagnostics-analysis-io.js
5
+ const DIAGNOSTICS_ANALYSIS_MAX_INPUT_BYTES = 8388608;
6
+ const fileSystem = {
7
+ lstat,
8
+ open
9
+ };
10
+ function failure(code) {
11
+ throw new Error(`COPILOT_DIAGNOSTICS_ANALYSIS_${code}`);
12
+ }
13
+ function sameFile(left, right) {
14
+ return left.dev === right.dev && left.ino === right.ino;
15
+ }
16
+ function sameContentState(left, right) {
17
+ return sameFile(left, right) && left.size === right.size && left.mtimeMs === right.mtimeMs;
18
+ }
19
+ async function readBoundedDiagnosticsFile(filePath, io = fileSystem) {
20
+ if (!isAbsolute(filePath)) failure("INVALID_PATH");
21
+ let pathBefore;
22
+ try {
23
+ pathBefore = await io.lstat(filePath);
24
+ } catch {
25
+ failure("FILE_READ_FAILED");
26
+ }
27
+ if (pathBefore.isSymbolicLink()) failure("SYMLINK_REJECTED");
28
+ if (!pathBefore.isFile()) failure("NOT_REGULAR_FILE");
29
+ if (pathBefore.size > 8388608) failure("INPUT_LIMIT");
30
+ let handle;
31
+ try {
32
+ handle = await io.open(filePath, "r");
33
+ } catch {
34
+ failure("FILE_READ_FAILED");
35
+ }
36
+ let text;
37
+ try {
38
+ const before = await handle.stat();
39
+ if (!before.isFile()) failure("NOT_REGULAR_FILE");
40
+ if (before.size > 8388608) failure("INPUT_LIMIT");
41
+ if (!sameContentState(pathBefore, before)) failure("FILE_CHANGED");
42
+ const bytes = Buffer.alloc(8388609);
43
+ let offset = 0;
44
+ while (offset < bytes.length) {
45
+ const { bytesRead } = await handle.read(bytes, offset, bytes.length - offset, offset);
46
+ if (bytesRead === 0) break;
47
+ offset += bytesRead;
48
+ }
49
+ const after = await handle.stat();
50
+ let pathAfter;
51
+ try {
52
+ pathAfter = await io.lstat(filePath);
53
+ } catch {
54
+ failure("FILE_CHANGED");
55
+ }
56
+ if (pathAfter.isSymbolicLink()) failure("SYMLINK_REJECTED");
57
+ if (!pathAfter.isFile() || !after.isFile()) failure("NOT_REGULAR_FILE");
58
+ if (offset > 8388608 || before.size > 8388608 || after.size > 8388608) failure("INPUT_LIMIT");
59
+ if (offset !== before.size || !sameContentState(before, after) || !sameContentState(pathBefore, pathAfter) || !sameFile(pathAfter, after)) failure("FILE_CHANGED");
60
+ try {
61
+ text = new TextDecoder("utf-8", { fatal: true }).decode(bytes.subarray(0, offset));
62
+ } catch {
63
+ failure("INVALID_UTF8");
64
+ }
65
+ } catch (error) {
66
+ if (error instanceof Error && error.message.startsWith("COPILOT_DIAGNOSTICS_ANALYSIS_")) throw error;
67
+ failure("FILE_READ_FAILED");
68
+ } finally {
69
+ try {
70
+ await handle.close();
71
+ } catch {
72
+ failure("FILE_READ_FAILED");
73
+ }
74
+ }
75
+ return text;
76
+ }
77
+ //#endregion
78
+ export { DIAGNOSTICS_ANALYSIS_MAX_INPUT_BYTES, readBoundedDiagnosticsFile };
@@ -0,0 +1,425 @@
1
+ import { a as DiagnosticsPendingSchema, c as DiagnosticsViewSchema, n as DIAGNOSTICS_OPERATIONS, r as DIAGNOSTICS_REASONS, s as DiagnosticsSnapshotSchema } from "./diagnostics-types-FVf6zjob.js";
2
+ import { z } from "zod";
3
+ //#region lib/types/diagnostics-analysis.js
4
+ const RETENTION_MS = 12096e5;
5
+ const terminals = [
6
+ "success",
7
+ "no-op",
8
+ "cancelled",
9
+ "policy-rejected",
10
+ "revoked",
11
+ "environment-fault",
12
+ "failed",
13
+ "unknown",
14
+ "interrupted"
15
+ ];
16
+ const gaps = [
17
+ "persisted-only",
18
+ "live-enabled-state-unknown",
19
+ "live-pending-state-unknown",
20
+ "unflushed-tail-unknown",
21
+ "stored-pending-is-not-live",
22
+ "no-retained-operation-observations",
23
+ "account-switch-not-observed",
24
+ "compaction-not-observed",
25
+ "stale-snapshot",
26
+ "expired-in-stored-snapshot",
27
+ "host-dropped",
28
+ "client-dropped",
29
+ "client-unconfirmed",
30
+ "retention-or-capacity-eviction",
31
+ "collector-saturated",
32
+ "interrupted-observations",
33
+ "clock-discontinuity",
34
+ "recorded-pause",
35
+ "unpersisted-observations",
36
+ "collection-paused",
37
+ "storage-not-ready",
38
+ "analysis-count-overflow",
39
+ "no-cross-layer-correlation",
40
+ "no-unique-impact",
41
+ "hour-boundary-censoring",
42
+ "no-collection-coverage-clock",
43
+ "no-calibrated-alert-thresholds",
44
+ "no-baseline-trend",
45
+ "no-root-cause-proof"
46
+ ];
47
+ const intervals = [
48
+ {
49
+ lowerMs: 0,
50
+ upperExclusiveMs: 100
51
+ },
52
+ {
53
+ lowerMs: 100,
54
+ upperExclusiveMs: 500
55
+ },
56
+ {
57
+ lowerMs: 500,
58
+ upperExclusiveMs: 1e3
59
+ },
60
+ {
61
+ lowerMs: 1e3,
62
+ upperExclusiveMs: 5e3
63
+ },
64
+ {
65
+ lowerMs: 5e3,
66
+ upperExclusiveMs: 15e3
67
+ },
68
+ {
69
+ lowerMs: 15e3,
70
+ upperExclusiveMs: 6e4
71
+ },
72
+ {
73
+ lowerMs: 6e4,
74
+ upperExclusiveMs: 3e5
75
+ },
76
+ {
77
+ lowerMs: 3e5,
78
+ upperExclusiveMs: null
79
+ }
80
+ ];
81
+ const CountSchema = z.object({
82
+ value: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).nullable(),
83
+ overflow: z.boolean()
84
+ }).strict().refine((value) => value.overflow === (value.value === null));
85
+ const outcomeSchema = z.object({
86
+ success: CountSchema,
87
+ "no-op": CountSchema,
88
+ cancelled: CountSchema,
89
+ "policy-rejected": CountSchema,
90
+ revoked: CountSchema,
91
+ "environment-fault": CountSchema,
92
+ failed: CountSchema,
93
+ unknown: CountSchema,
94
+ interrupted: CountSchema
95
+ }).strict();
96
+ const intervalSchema = z.object({
97
+ lowerMs: z.number().int().nonnegative(),
98
+ upperExclusiveMs: z.number().int().positive().nullable(),
99
+ count: CountSchema
100
+ }).strict().refine((value) => value.upperExclusiveMs === null || value.lowerMs < value.upperExclusiveMs);
101
+ const populationSchema = z.object({
102
+ version: z.string().max(64).regex(/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/),
103
+ layer: z.enum(["client", "host"]),
104
+ operation: z.enum(DIAGNOSTICS_OPERATIONS),
105
+ successMeaning: z.enum([
106
+ "owner-terminal-observation-only",
107
+ "matching-checkpoint-and-end-observed",
108
+ "summary-call-fulfilled-not-checkpoint-commit"
109
+ ]),
110
+ firstHour: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
111
+ lastHour: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
112
+ started: CountSchema,
113
+ terminalObservations: CountSchema,
114
+ outcomes: outcomeSchema,
115
+ terminalLatency: z.array(intervalSchema).length(8),
116
+ stageAgeObservations: z.array(intervalSchema).length(8),
117
+ pendingSamples: CountSchema,
118
+ pendingSampleAges: z.array(intervalSchema).length(8),
119
+ stageObservations: CountSchema,
120
+ startsMinusTerminals: z.null(),
121
+ exactOperationFailureRate: z.null()
122
+ }).strict().refine((value) => value.firstHour <= value.lastHour).refine((value) => value.successMeaning === (value.operation === "compaction" ? "matching-checkpoint-and-end-observed" : value.operation === "compaction-summary" ? "summary-call-fulfilled-not-checkpoint-commit" : "owner-terminal-observation-only"));
123
+ const candidateSchema = z.object({
124
+ fingerprint: z.string().max(256).regex(/^coverage-1\|(?:client|host)\|[a-z-]+\|(?:failed|environment-fault|unknown)\|[A-Za-z0-9_-]+$/),
125
+ category: z.enum(["recorded-failure", "unknown-outcome"]),
126
+ layer: z.enum(["client", "host"]),
127
+ operation: z.enum(DIAGNOSTICS_OPERATIONS),
128
+ metric: z.enum([
129
+ "failed",
130
+ "environment-fault",
131
+ "unknown"
132
+ ]),
133
+ reason: z.enum(DIAGNOSTICS_REASONS),
134
+ versions: z.array(z.string().max(64).regex(/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/)).max(4096),
135
+ firstHour: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
136
+ lastHour: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
137
+ observations: CountSchema,
138
+ regressionProven: z.literal(false),
139
+ persistentFaultProven: z.literal(false),
140
+ rootCause: z.literal("unknown")
141
+ }).strict().refine((value) => value.firstHour <= value.lastHour).refine((value) => value.fingerprint === `coverage-1|${value.layer}|${value.operation}|${value.metric}|${value.reason}`).refine((value) => value.category === (value.metric === "unknown" ? "unknown-outcome" : "recorded-failure"));
142
+ const collectionSchema = z.object({
143
+ enabled: z.boolean().nullable(),
144
+ state: z.enum([
145
+ "loading",
146
+ "ready",
147
+ "unavailable",
148
+ "error"
149
+ ]).nullable(),
150
+ diagnostic: z.enum([
151
+ "none",
152
+ "storage-unavailable",
153
+ "storage-invalid",
154
+ "storage-write-failed",
155
+ "profile-unavailable",
156
+ "settings-unavailable",
157
+ "settings-conflict",
158
+ "client-report-failed",
159
+ "client-report-revoked",
160
+ "closed"
161
+ ]).nullable(),
162
+ dirty: z.boolean().nullable(),
163
+ persistedAt: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).nullable()
164
+ }).strict();
165
+ const DiagnosticsAnalysisReportSchema = z.object({
166
+ reportVersion: z.literal(1),
167
+ coverageVersion: z.literal(1),
168
+ interpretation: z.literal("descriptive-observations-only"),
169
+ source: z.enum(["reviewed-view", "persisted-snapshot"]),
170
+ observedAt: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
171
+ snapshotAt: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
172
+ ageMs: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).nullable(),
173
+ epoch: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
174
+ coverageComplete: z.literal(false),
175
+ effectivenessVerified: z.literal(false),
176
+ gaps: z.array(z.enum(gaps)).max(gaps.length),
177
+ collection: collectionSchema,
178
+ counters: z.object({
179
+ dropped: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
180
+ clientDropped: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
181
+ clientUnconfirmed: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
182
+ evicted: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
183
+ saturated: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
184
+ interrupted: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER)
185
+ }).strict(),
186
+ populations: z.array(populationSchema).max(4096),
187
+ pendingAtSnapshot: z.array(DiagnosticsPendingSchema).max(128),
188
+ investigationCandidates: z.array(candidateSchema).max(4096)
189
+ }).strict().refine((value) => new Set(value.gaps).size === value.gaps.length);
190
+ function sum(values) {
191
+ const total = values.reduce((value, next) => value + BigInt(next), 0n);
192
+ return total > BigInt(Number.MAX_SAFE_INTEGER) ? {
193
+ value: null,
194
+ overflow: true
195
+ } : {
196
+ value: Number(total),
197
+ overflow: false
198
+ };
199
+ }
200
+ function parseSnapshot(input) {
201
+ const parsed = DiagnosticsSnapshotSchema.safeParse(input);
202
+ if (!parsed.success) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_SNAPSHOT");
203
+ const tuples = /* @__PURE__ */ new Set();
204
+ for (const row of parsed.data.rows) {
205
+ const tuple = [
206
+ row.hour,
207
+ row.version,
208
+ row.layer,
209
+ row.operation,
210
+ row.stage,
211
+ row.metric,
212
+ row.reason,
213
+ row.bucket
214
+ ].join("|");
215
+ if (tuples.has(tuple)) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_DUPLICATE_AGGREGATE");
216
+ tuples.add(tuple);
217
+ }
218
+ return parsed.data;
219
+ }
220
+ function parseJsonText(text) {
221
+ if (Buffer.byteLength(text, "utf8") > 8388608) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INPUT_LIMIT");
222
+ try {
223
+ return JSON.parse(text);
224
+ } catch {
225
+ throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_JSON");
226
+ }
227
+ }
228
+ function order(left, right) {
229
+ return left < right ? -1 : left > right ? 1 : 0;
230
+ }
231
+ function diagnosticsUnitName(profileName) {
232
+ if (!/^[a-zA-Z0-9_-]{1,48}$/.test(profileName)) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_PROFILE");
233
+ return `github_copilot_diagnostics_${Buffer.from(profileName, "ascii").toString("hex")}`;
234
+ }
235
+ function parseReviewedView(text) {
236
+ const parsed = DiagnosticsViewSchema.safeParse(parseJsonText(text));
237
+ if (!parsed.success) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_VIEW");
238
+ return parsed.data;
239
+ }
240
+ function parsePersistedUnit(text, expectedUnit) {
241
+ if (!/^github_copilot_diagnostics_(?:[a-f0-9]{2}){1,48}$/.test(expectedUnit)) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_UNIT");
242
+ const input = parseJsonText(text);
243
+ if (typeof input !== "object" || input === null || Array.isArray(input) || Object.keys(input).sort().join(",") !== "global,tables,unit" || !("unit" in input) || typeof input.unit !== "object" || input.unit === null || Array.isArray(input.unit) || Object.keys(input.unit).sort().join(",") !== "name,version" || !("name" in input.unit) || input.unit.name !== expectedUnit || !("version" in input.unit) || input.unit.version !== 1 || !("tables" in input) || typeof input.tables !== "object" || input.tables === null || Array.isArray(input.tables) || Object.keys(input.tables).length !== 0 || !("global" in input)) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_UNIT");
244
+ return parseSnapshot(input.global);
245
+ }
246
+ function analyzeSnapshot(input, options) {
247
+ if (!Number.isSafeInteger(options.observedAt) || options.observedAt < 0 || !["reviewed-view", "persisted-snapshot"].includes(options.source) || options.source === "reviewed-view" !== (options.view !== void 0)) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_OPTIONS");
248
+ const snapshot = parseSnapshot(input);
249
+ const rows = [...snapshot.rows].sort((a, b) => order(a.version, b.version) || order(a.layer, b.layer) || order(a.operation, b.operation) || a.hour - b.hour || order(a.stage, b.stage) || order(a.metric, b.metric) || order(a.reason, b.reason) || a.bucket - b.bucket);
250
+ const groups = /* @__PURE__ */ new Map();
251
+ for (const row of rows) {
252
+ if (row.operation === "collection") continue;
253
+ const key = `${row.version}|${row.layer}|${row.operation}`;
254
+ const population = groups.get(key) ?? [];
255
+ population.push(row);
256
+ groups.set(key, population);
257
+ }
258
+ const populations = [...groups.values()].map((population) => {
259
+ const first = population[0];
260
+ const terminalRows = population.filter((row) => terminals.some((metric) => metric === row.metric));
261
+ const outcomes = Object.fromEntries(terminals.map((metric) => [metric, sum(population.filter((row) => row.metric === metric).map((row) => row.count))]));
262
+ return {
263
+ version: first.version,
264
+ layer: first.layer,
265
+ operation: first.operation,
266
+ successMeaning: first.operation === "compaction" ? "matching-checkpoint-and-end-observed" : first.operation === "compaction-summary" ? "summary-call-fulfilled-not-checkpoint-commit" : "owner-terminal-observation-only",
267
+ firstHour: Math.min(...population.map((row) => row.hour)),
268
+ lastHour: Math.max(...population.map((row) => row.hour)),
269
+ started: sum(population.filter((row) => row.metric === "started").map((row) => row.count)),
270
+ terminalObservations: sum(terminalRows.map((row) => row.count)),
271
+ outcomes,
272
+ terminalLatency: intervals.map((interval, bucket) => ({
273
+ ...interval,
274
+ count: sum(terminalRows.filter((row) => row.bucket === bucket).map((row) => row.count))
275
+ })),
276
+ stageAgeObservations: intervals.map((interval, bucket) => ({
277
+ ...interval,
278
+ count: sum(population.filter((row) => row.metric === "stage" && row.bucket === bucket).map((row) => row.count))
279
+ })),
280
+ pendingSamples: sum(population.filter((row) => row.metric === "pending-sample").map((row) => row.count)),
281
+ pendingSampleAges: intervals.map((interval, bucket) => ({
282
+ ...interval,
283
+ count: sum(population.filter((row) => row.metric === "pending-sample" && row.bucket === bucket).map((row) => row.count))
284
+ })),
285
+ stageObservations: sum(population.filter((row) => row.metric === "stage").map((row) => row.count)),
286
+ startsMinusTerminals: null,
287
+ exactOperationFailureRate: null
288
+ };
289
+ });
290
+ const candidates = /* @__PURE__ */ new Map();
291
+ for (const row of rows) {
292
+ if (row.metric !== "failed" && row.metric !== "environment-fault" && row.metric !== "unknown" || row.operation === "collection") continue;
293
+ const metric = row.metric;
294
+ const fingerprint = `coverage-1|${row.layer}|${row.operation}|${metric}|${row.reason}`;
295
+ const candidate = candidates.get(fingerprint) ?? {
296
+ fingerprint,
297
+ category: metric === "unknown" ? "unknown-outcome" : "recorded-failure",
298
+ layer: row.layer,
299
+ operation: row.operation,
300
+ metric,
301
+ reason: row.reason,
302
+ versions: [],
303
+ hours: [],
304
+ counts: []
305
+ };
306
+ candidate.versions.push(row.version);
307
+ candidate.hours.push(row.hour);
308
+ candidate.counts.push(row.count);
309
+ candidates.set(fingerprint, candidate);
310
+ }
311
+ const gapSet = /* @__PURE__ */ new Set([
312
+ "no-cross-layer-correlation",
313
+ "no-unique-impact",
314
+ "hour-boundary-censoring",
315
+ "no-collection-coverage-clock",
316
+ "no-calibrated-alert-thresholds",
317
+ "no-baseline-trend",
318
+ "no-root-cause-proof"
319
+ ]);
320
+ const view = options.view;
321
+ if (options.source === "persisted-snapshot") {
322
+ gapSet.add("persisted-only");
323
+ gapSet.add("live-enabled-state-unknown");
324
+ gapSet.add("live-pending-state-unknown");
325
+ gapSet.add("unflushed-tail-unknown");
326
+ if (snapshot.pending.length) gapSet.add("stored-pending-is-not-live");
327
+ }
328
+ if (!populations.length) gapSet.add("no-retained-operation-observations");
329
+ if (!populations.some((row) => [
330
+ "account-global-switch",
331
+ "account-session-select",
332
+ "account-session-inherit"
333
+ ].includes(row.operation))) gapSet.add("account-switch-not-observed");
334
+ if (!populations.some((row) => row.operation === "compaction" || row.operation === "compaction-summary")) gapSet.add("compaction-not-observed");
335
+ if (snapshot.updatedAt <= options.observedAt - RETENTION_MS) gapSet.add("stale-snapshot");
336
+ if (snapshot.rows.some((row) => row.hour <= options.observedAt - RETENTION_MS)) gapSet.add("expired-in-stored-snapshot");
337
+ if (snapshot.dropped) gapSet.add("host-dropped");
338
+ if (snapshot.clientDropped) gapSet.add("client-dropped");
339
+ if (snapshot.clientUnconfirmed) gapSet.add("client-unconfirmed");
340
+ if (snapshot.evicted) gapSet.add("retention-or-capacity-eviction");
341
+ if (snapshot.saturated) gapSet.add("collector-saturated");
342
+ if (snapshot.interrupted || snapshot.rows.some((row) => row.metric === "interrupted")) gapSet.add("interrupted-observations");
343
+ if (snapshot.rows.some((row) => row.metric === "clock-discontinuity") || snapshot.updatedAt > options.observedAt || snapshot.rows.some((row) => row.hour > options.observedAt)) gapSet.add("clock-discontinuity");
344
+ if (snapshot.rows.some((row) => row.metric === "paused")) gapSet.add("recorded-pause");
345
+ if (view?.dirty) gapSet.add("unpersisted-observations");
346
+ if (view && !view.enabled) gapSet.add("collection-paused");
347
+ if (view && view.state !== "ready") gapSet.add("storage-not-ready");
348
+ const report = {
349
+ reportVersion: 1,
350
+ coverageVersion: 1,
351
+ interpretation: "descriptive-observations-only",
352
+ source: options.source,
353
+ observedAt: options.observedAt,
354
+ snapshotAt: snapshot.updatedAt,
355
+ ageMs: snapshot.updatedAt <= options.observedAt ? options.observedAt - snapshot.updatedAt : null,
356
+ epoch: snapshot.epoch,
357
+ coverageComplete: false,
358
+ effectivenessVerified: false,
359
+ gaps: [...gapSet],
360
+ collection: {
361
+ enabled: view?.enabled ?? null,
362
+ state: view?.state ?? null,
363
+ diagnostic: view?.diagnostic ?? null,
364
+ dirty: view?.dirty ?? null,
365
+ persistedAt: view?.persistedAt ?? null
366
+ },
367
+ counters: {
368
+ dropped: snapshot.dropped,
369
+ clientDropped: snapshot.clientDropped,
370
+ clientUnconfirmed: snapshot.clientUnconfirmed,
371
+ evicted: snapshot.evicted,
372
+ saturated: snapshot.saturated,
373
+ interrupted: snapshot.interrupted
374
+ },
375
+ populations,
376
+ pendingAtSnapshot: snapshot.pending,
377
+ investigationCandidates: [...candidates.values()].map((candidate) => ({
378
+ fingerprint: candidate.fingerprint,
379
+ category: candidate.category,
380
+ layer: candidate.layer,
381
+ operation: candidate.operation,
382
+ metric: candidate.metric,
383
+ reason: candidate.reason,
384
+ versions: [...new Set(candidate.versions)].sort(order),
385
+ firstHour: Math.min(...candidate.hours),
386
+ lastHour: Math.max(...candidate.hours),
387
+ observations: sum(candidate.counts),
388
+ regressionProven: false,
389
+ persistentFaultProven: false,
390
+ rootCause: "unknown"
391
+ }))
392
+ };
393
+ const parsed = DiagnosticsAnalysisReportSchema.safeParse(report);
394
+ if (!parsed.success) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_REPORT");
395
+ if (populations.some((population) => [
396
+ population.started,
397
+ population.terminalObservations,
398
+ population.stageObservations,
399
+ population.pendingSamples,
400
+ ...Object.values(population.outcomes),
401
+ ...population.terminalLatency.map((bin) => bin.count),
402
+ ...population.stageAgeObservations.map((bin) => bin.count),
403
+ ...population.pendingSampleAges.map((bin) => bin.count)
404
+ ].some((count) => count.overflow)) || report.investigationCandidates.some((candidate) => candidate.observations.overflow)) gapSet.add("analysis-count-overflow");
405
+ if (gapSet.has("analysis-count-overflow")) {
406
+ const corrected = DiagnosticsAnalysisReportSchema.safeParse({
407
+ ...parsed.data,
408
+ gaps: [...gapSet]
409
+ });
410
+ if (!corrected.success) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_REPORT");
411
+ return corrected.data;
412
+ }
413
+ return parsed.data;
414
+ }
415
+ function analyzeReviewedView(view, observedAt) {
416
+ const parsed = DiagnosticsViewSchema.safeParse(view);
417
+ if (!parsed.success) throw new Error("COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_VIEW");
418
+ return analyzeSnapshot(parsed.data.snapshot, {
419
+ source: "reviewed-view",
420
+ observedAt,
421
+ view: parsed.data
422
+ });
423
+ }
424
+ //#endregion
425
+ export { DiagnosticsAnalysisReportSchema, analyzeReviewedView, analyzeSnapshot, diagnosticsUnitName, parsePersistedUnit, parseReviewedView };
@@ -0,0 +1,182 @@
1
+ import { z } from "zod";
2
+ //#region lib/types/diagnostics-types.js
3
+ const DIAGNOSTICS_OPERATIONS = [
4
+ "account-global-switch",
5
+ "account-session-select",
6
+ "account-session-inherit",
7
+ "identity-read",
8
+ "identity-flight",
9
+ "compaction",
10
+ "compaction-summary",
11
+ "collection"
12
+ ];
13
+ const DIAGNOSTICS_STAGES = [
14
+ "admitted",
15
+ "rpc-invoked",
16
+ "host-received",
17
+ "identity-validation",
18
+ "model-validation",
19
+ "cas",
20
+ "readback",
21
+ "response-received",
22
+ "decoded",
23
+ "client-settled",
24
+ "summary-attempt",
25
+ "checkpoint-committed",
26
+ "ended",
27
+ "cache-hit",
28
+ "joined-flight",
29
+ "cooldown",
30
+ "dispatched"
31
+ ];
32
+ const DIAGNOSTICS_METRICS = [
33
+ "started",
34
+ "stage",
35
+ "success",
36
+ "no-op",
37
+ "cancelled",
38
+ "policy-rejected",
39
+ "revoked",
40
+ "environment-fault",
41
+ "failed",
42
+ "unknown",
43
+ "interrupted",
44
+ "dropped",
45
+ "paused",
46
+ "expired",
47
+ "clock-discontinuity",
48
+ "pending-sample"
49
+ ];
50
+ const DIAGNOSTICS_REASONS = [
51
+ "none",
52
+ "unknown",
53
+ "rpc-unavailable",
54
+ "decode-invalid",
55
+ "COPILOT_ACCOUNTS_BUSY",
56
+ "COPILOT_ACCOUNTS_CONFLICT",
57
+ "COPILOT_ACCOUNTS_CHANGED",
58
+ "COPILOT_ACCOUNTS_COMMIT_UNCERTAIN",
59
+ "COPILOT_ACCOUNTS_IDENTITY_TIMEOUT",
60
+ "COPILOT_ACCOUNTS_IDENTITY_UNAVAILABLE",
61
+ "COPILOT_ACCOUNTS_MODELS_FAILED",
62
+ "COPILOT_ACCOUNTS_SETTINGS_UNAVAILABLE",
63
+ "COPILOT_ACCOUNTS_IDENTITY_TLS",
64
+ "COPILOT_ACCOUNTS_IDENTITY_NETWORK",
65
+ "COPILOT_ACCOUNTS_IDENTITY_RATE_LIMITED",
66
+ "COPILOT_ACCOUNTS_IDENTITY_AUTH_REJECTED",
67
+ "COPILOT_ACCOUNTS_IDENTITY_HTTP_ERROR",
68
+ "COPILOT_ACCOUNTS_IDENTITY_INVALID",
69
+ "COPILOT_SESSION_ACCOUNTS_UNAVAILABLE",
70
+ "COPILOT_SESSION_ACCOUNTS_SETTINGS_UNAVAILABLE",
71
+ "COPILOT_SESSION_ACCOUNTS_COMMIT_UNCERTAIN",
72
+ "COPILOT_RESPONSES_REPLAY_SCOPE_MISMATCH",
73
+ "CONTEXT_WINDOW_EXCEEDED",
74
+ "COPILOT_MANUAL_RECOVERY_FIXED_PREFIX",
75
+ "COPILOT_MANUAL_RECOVERY_INDIVISIBLE",
76
+ "COPILOT_MANUAL_RECOVERY_CALL_LIMIT",
77
+ "COPILOT_MANUAL_RECOVERY_EMPTY_SUMMARY",
78
+ "COPILOT_MANUAL_RECOVERY_UNBALANCED"
79
+ ];
80
+ const DIAGNOSTICS_MAX_ROWS = 4096;
81
+ const DIAGNOSTICS_MAX_BYTES = 2097152;
82
+ const DIAGNOSTICS_RETENTION_MS = 12096e5;
83
+ const integer = z.number().int().min(0).max(Number.MAX_SAFE_INTEGER);
84
+ const version = z.string().max(64).regex(/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/);
85
+ const DiagnosticsRowSchema = z.object({
86
+ hour: integer.multipleOf(36e5),
87
+ version,
88
+ layer: z.enum(["client", "host"]),
89
+ operation: z.enum(DIAGNOSTICS_OPERATIONS),
90
+ stage: z.enum(DIAGNOSTICS_STAGES),
91
+ metric: z.enum(DIAGNOSTICS_METRICS),
92
+ reason: z.enum(DIAGNOSTICS_REASONS),
93
+ bucket: z.number().int().min(0).max(7),
94
+ count: integer.min(1)
95
+ }).strict();
96
+ const DiagnosticsPendingSchema = z.object({
97
+ version,
98
+ operation: z.enum(DIAGNOSTICS_OPERATIONS),
99
+ stage: z.enum(DIAGNOSTICS_STAGES),
100
+ bucket: z.number().int().min(0).max(7),
101
+ count: integer.min(1).max(128)
102
+ }).strict();
103
+ const DiagnosticsSnapshotSchema = z.object({
104
+ schemaVersion: z.literal(1),
105
+ coverageVersion: z.literal(1),
106
+ epoch: integer,
107
+ updatedAt: integer,
108
+ rows: z.array(DiagnosticsRowSchema).max(DIAGNOSTICS_MAX_ROWS),
109
+ pending: z.array(DiagnosticsPendingSchema).max(128),
110
+ dropped: integer,
111
+ clientDropped: integer,
112
+ clientUnconfirmed: integer,
113
+ saturated: integer,
114
+ evicted: integer,
115
+ interrupted: integer
116
+ }).strict().refine((value) => value.pending.reduce((sum, row) => sum + row.count, 0) <= 128, "Diagnostics pending capacity exceeded").refine((value) => JSON.stringify(value).length <= DIAGNOSTICS_MAX_BYTES, "Diagnostics capacity exceeded");
117
+ const DiagnosticsViewSchema = z.object({
118
+ enabled: z.boolean(),
119
+ state: z.enum([
120
+ "loading",
121
+ "ready",
122
+ "unavailable",
123
+ "error"
124
+ ]),
125
+ diagnostic: z.enum([
126
+ "none",
127
+ "storage-unavailable",
128
+ "storage-invalid",
129
+ "storage-write-failed",
130
+ "profile-unavailable",
131
+ "settings-unavailable",
132
+ "settings-conflict",
133
+ "client-report-failed",
134
+ "client-report-revoked",
135
+ "closed"
136
+ ]),
137
+ persistedAt: integer.optional(),
138
+ dirty: z.boolean(),
139
+ snapshot: DiagnosticsSnapshotSchema
140
+ }).strict();
141
+ function diagnosticsBucket(ms) {
142
+ return [
143
+ 100,
144
+ 500,
145
+ 1e3,
146
+ 5e3,
147
+ 15e3,
148
+ 6e4,
149
+ 3e5
150
+ ].findIndex((bound) => ms < bound) === -1 ? 7 : [
151
+ 100,
152
+ 500,
153
+ 1e3,
154
+ 5e3,
155
+ 15e3,
156
+ 6e4,
157
+ 3e5
158
+ ].findIndex((bound) => ms < bound);
159
+ }
160
+ function diagnosticsReason(error) {
161
+ const seen = /* @__PURE__ */ new Set();
162
+ for (let depth = 0; depth < 8 && error instanceof Error && !seen.has(error); depth++) {
163
+ seen.add(error);
164
+ const nativeCode = "code" in error ? error.code : void 0;
165
+ const typed = DIAGNOSTICS_REASONS.find((code) => code !== "none" && code !== "unknown" && code === nativeCode);
166
+ if (typed !== void 0) return typed;
167
+ const message = error.message.slice(0, 4096);
168
+ for (const code of DIAGNOSTICS_REASONS) if (code.startsWith("COPILOT_") || code === "CONTEXT_WINDOW_EXCEEDED") {
169
+ if (new RegExp(`(?:^|[^A-Za-z0-9_])${code}(?:$|[^A-Za-z0-9_])`).test(message)) return code;
170
+ }
171
+ error = error.cause;
172
+ }
173
+ return "unknown";
174
+ }
175
+ function diagnosticsOutcome(reason) {
176
+ if (reason === "COPILOT_ACCOUNTS_BUSY" || reason === "COPILOT_ACCOUNTS_CONFLICT") return "policy-rejected";
177
+ if (reason === "COPILOT_ACCOUNTS_CHANGED") return "revoked";
178
+ if (reason.endsWith("SETTINGS_UNAVAILABLE") || reason === "COPILOT_SESSION_ACCOUNTS_UNAVAILABLE") return "environment-fault";
179
+ return "failed";
180
+ }
181
+ //#endregion
182
+ export { DiagnosticsPendingSchema as a, DiagnosticsViewSchema as c, diagnosticsReason as d, DIAGNOSTICS_RETENTION_MS as i, diagnosticsBucket as l, DIAGNOSTICS_OPERATIONS as n, DiagnosticsRowSchema as o, DIAGNOSTICS_REASONS as r, DiagnosticsSnapshotSchema as s, DIAGNOSTICS_MAX_ROWS as t, diagnosticsOutcome as u };