dsh-github-copilot 0.4.0-alpha.99 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/AGENTS.md +96 -16
  2. package/CHANGELOG.md +211 -0
  3. package/CONTRIBUTING.md +9 -0
  4. package/README.md +106 -18
  5. package/README.zh.md +97 -18
  6. package/deployment-baseline.json +227 -8
  7. package/docs/account-management-experience.md +143 -0
  8. package/docs/auto-high-cost.md +154 -0
  9. package/docs/auto-iteration-review.json +22 -0
  10. package/docs/auto-review-sampling.md +60 -0
  11. package/docs/auto-task-routing.md +8 -6
  12. package/docs/automatic-model-routing.md +33 -5
  13. package/docs/copilot-accounts.md +376 -0
  14. package/docs/copilot-compaction.md +23 -2
  15. package/docs/copilot-usage.md +24 -4
  16. package/docs/evidence-driven-iteration.md +86 -0
  17. package/docs/images/copilot-accounts-credits.png +0 -0
  18. package/docs/images/copilot-accounts.png +0 -0
  19. package/docs/images/copilot-current-provenance.json +39 -0
  20. package/docs/images/copilot-model-preferences.png +0 -0
  21. package/docs/images/copilot-search-routing.png +0 -0
  22. package/docs/images/copilot-session-continuation.png +0 -0
  23. package/docs/manual-compaction-recovery.md +50 -7
  24. package/docs/mockups/copilot-account-dropdown.html +71 -0
  25. package/docs/mockups/copilot-account-management.html +393 -0
  26. package/docs/model-compatibility-acceptance.md +34 -16
  27. package/docs/official-first-020-rc2.md +1 -0
  28. package/docs/plugin-diagnostics.md +452 -0
  29. package/docs/session-continuation.md +141 -0
  30. package/docs/session-search-routing.md +1 -1
  31. package/docs/single-route-migration.md +5 -4
  32. package/lib/client.js +4415 -846
  33. package/lib/client.js.map +1 -1
  34. package/lib/diagnostics-analysis-io.js +78 -0
  35. package/lib/diagnostics-analysis.js +425 -0
  36. package/lib/diagnostics-types-FVf6zjob.js +182 -0
  37. package/lib/index.js +10356 -6020
  38. package/lib/manual-compaction-recovery.js +163 -38
  39. package/lib/remote.js +593 -34
  40. package/lib/routed-web.js +1 -1
  41. package/lib/{search-routing-jzNYveyK.js → search-routing-CV2hs8as.js} +139 -139
  42. package/lib/types/account-dropdown.d.ts +22 -0
  43. package/lib/types/account-model-auth.d.ts +1 -1
  44. package/lib/types/authorization-code-clipboard.d.ts +4 -0
  45. package/lib/types/authorization-controller.d.ts +2 -0
  46. package/lib/types/auto-allocation-card.d.ts +9 -0
  47. package/lib/types/auto-allocation-evidence.d.ts +38 -0
  48. package/lib/types/auto-allocation.d.ts +24 -0
  49. package/lib/types/auto-model-host.d.ts +2 -0
  50. package/lib/types/auto-model-routing.d.ts +6 -2
  51. package/lib/types/auto-review-sampling.d.ts +6 -0
  52. package/lib/types/auto-task-classifier.d.ts +1 -1
  53. package/lib/types/background-compaction.d.ts +2 -2
  54. package/lib/types/client.d.ts +19 -3
  55. package/lib/types/compaction-continuation-ui.d.ts +29 -0
  56. package/lib/types/compaction-continuation.d.ts +48 -0
  57. package/lib/types/compaction-input-estimate.d.ts +24 -0
  58. package/lib/types/compaction-replay.d.ts +18 -0
  59. package/lib/types/composer-notice-style.d.ts +7 -0
  60. package/lib/types/config.d.ts +42 -2
  61. package/lib/types/context-evidence-ui.d.ts +18 -1
  62. package/lib/types/context-evidence.d.ts +152 -4
  63. package/lib/types/copilot-account-presentation.d.ts +7 -0
  64. package/lib/types/copilot-accounts-card.d.ts +43 -0
  65. package/lib/types/copilot-accounts-host.d.ts +93 -0
  66. package/lib/types/copilot-accounts-remote.d.ts +85 -0
  67. package/lib/types/copilot-accounts-types.d.ts +43 -0
  68. package/lib/types/copilot-auth.d.ts +3 -1
  69. package/lib/types/copilot-usage-card.d.ts +23 -0
  70. package/lib/types/copilot-usage-host.d.ts +9 -1
  71. package/lib/types/copilot-usage-remote.d.ts +2 -1
  72. package/lib/types/copilot-usage-types.d.ts +1 -0
  73. package/lib/types/diagnostics-analysis-io.d.ts +23 -0
  74. package/lib/types/diagnostics-analysis.d.ts +295 -0
  75. package/lib/types/diagnostics-card.d.ts +11 -0
  76. package/lib/types/diagnostics-client.d.ts +13 -0
  77. package/lib/types/diagnostics-collector.d.ts +36 -0
  78. package/lib/types/diagnostics-host.d.ts +72 -0
  79. package/lib/types/diagnostics-remote.d.ts +109 -0
  80. package/lib/types/diagnostics-types.d.ts +431 -0
  81. package/lib/types/manual-compaction-recovery.d.ts +18 -3
  82. package/lib/types/model-preferences-card.d.ts +1 -1
  83. package/lib/types/native-settings-style.d.ts +10 -0
  84. package/lib/types/parent-model-follow-card.d.ts +2 -11
  85. package/lib/types/preview-provider.d.ts +4 -0
  86. package/lib/types/preview-route.d.ts +7 -5
  87. package/lib/types/remote.d.ts +9 -6
  88. package/lib/types/replay-recovery-host.d.ts +2 -2
  89. package/lib/types/replay-recovery-ui.d.ts +3 -1
  90. package/lib/types/request-body-evidence.d.ts +3 -1
  91. package/lib/types/responses-replay-compat.d.ts +2 -1
  92. package/lib/types/responses-request-compression.d.ts +33 -0
  93. package/lib/types/session-accounts-controller.d.ts +20 -0
  94. package/lib/types/session-accounts-host.d.ts +33 -0
  95. package/lib/types/session-accounts-remote.d.ts +114 -0
  96. package/lib/types/session-accounts.d.ts +38 -0
  97. package/lib/types/session-continuation-host.d.ts +45 -0
  98. package/lib/types/session-continuation-remote.d.ts +22 -0
  99. package/lib/types/session-continuation-types.d.ts +62 -0
  100. package/lib/types/session-continuation-ui.d.ts +49 -0
  101. package/lib/types/session-continuation.d.ts +14 -0
  102. package/lib/types/turn-request-models.d.ts +55 -0
  103. package/lib/types/turn-selection-card.d.ts +15 -1
  104. package/lib/types/turn-selection-host.d.ts +23 -0
  105. package/lib/types/turn-selection-remote.d.ts +4 -0
  106. package/lib/types/turn-selection.d.ts +19 -4
  107. package/package.json +19 -3
  108. package/scripts/analyze-diagnostics.mjs +91 -0
  109. package/scripts/iteration-review.mjs +53 -0
@@ -0,0 +1,55 @@
1
+ import { z } from 'zod';
2
+ import type { Context } from '@deepseek-ai/cordis';
3
+ export declare const TURN_REQUEST_MODELS = "githubCopilotTurnRequestModels";
4
+ export declare const RequestedModelsSchema: z.ZodObject<{
5
+ routes: z.ZodArray<z.ZodObject<{
6
+ provider: z.ZodString;
7
+ model: z.ZodString;
8
+ }, z.core.$strict>>;
9
+ incomplete: z.ZodBoolean;
10
+ }, z.core.$strict>;
11
+ export type RequestedModels = z.infer<typeof RequestedModelsSchema>;
12
+ export declare const TurnRequestModelsStateSchema: z.ZodObject<{
13
+ active: z.ZodNullable<z.ZodObject<{
14
+ turn: z.ZodNumber;
15
+ step: z.ZodNullable<z.ZodNumber>;
16
+ }, z.core.$strict>>;
17
+ turns: z.ZodArray<z.ZodObject<{
18
+ routes: z.ZodArray<z.ZodObject<{
19
+ provider: z.ZodString;
20
+ model: z.ZodString;
21
+ }, z.core.$strict>>;
22
+ incomplete: z.ZodBoolean;
23
+ turn: z.ZodNumber;
24
+ }, z.core.$strict>>;
25
+ }, z.core.$strict>;
26
+ type State = z.infer<typeof TurnRequestModelsStateSchema>;
27
+ declare module '@deepseek-ai/dsh-session-projection/types' {
28
+ interface SessionProjectionStateMap {
29
+ githubCopilotTurnRequestModels: State;
30
+ }
31
+ }
32
+ export declare function foldTurnRequestModels(state: State, event: unknown): State;
33
+ export declare const turnRequestModelsDefinition: {
34
+ key: "githubCopilotTurnRequestModels";
35
+ stateVersion: number;
36
+ stateSchema: z.ZodObject<{
37
+ active: z.ZodNullable<z.ZodObject<{
38
+ turn: z.ZodNumber;
39
+ step: z.ZodNullable<z.ZodNumber>;
40
+ }, z.core.$strict>>;
41
+ turns: z.ZodArray<z.ZodObject<{
42
+ routes: z.ZodArray<z.ZodObject<{
43
+ provider: z.ZodString;
44
+ model: z.ZodString;
45
+ }, z.core.$strict>>;
46
+ incomplete: z.ZodBoolean;
47
+ turn: z.ZodNumber;
48
+ }, z.core.$strict>>;
49
+ }, z.core.$strict>;
50
+ init: () => State;
51
+ apply: typeof foldTurnRequestModels;
52
+ };
53
+ export declare function installTurnRequestModels(ctx: Context): void;
54
+ export {};
55
+ //# sourceMappingURL=turn-request-models.d.ts.map
@@ -1,10 +1,24 @@
1
1
  import type { ReactElement } from 'react';
2
2
  import type { TurnSelection } from './turn-selection.ts';
3
- export declare function TurnSelectionCard({ selection, locale, incomplete, readState, retry }: {
3
+ import type { TurnAccountView } from './session-accounts-remote.ts';
4
+ import type { RecordedModel } from './turn-model-provenance.ts';
5
+ export interface TurnModelEvidence {
6
+ readonly kind: 'recorded' | 'requested' | 'unknown';
7
+ readonly routes: readonly RecordedModel[];
8
+ readonly incomplete: boolean;
9
+ }
10
+ export declare function TurnSelectionCard({ selection, locale, incomplete, readState, retry, account, accountFailed, retryAccount, models, modelsFailed, retryModels, allocationEvidence }: {
4
11
  selection: TurnSelection;
5
12
  locale?: string;
6
13
  incomplete?: boolean;
7
14
  readState?: 'loading' | 'ready' | 'failed';
8
15
  retry?: () => void;
16
+ account?: TurnAccountView;
17
+ accountFailed?: boolean;
18
+ retryAccount?: () => void;
19
+ models?: TurnModelEvidence;
20
+ modelsFailed?: boolean;
21
+ retryModels?: () => void;
22
+ allocationEvidence?: ReactElement;
9
23
  }): ReactElement;
10
24
  //# sourceMappingURL=turn-selection-card.d.ts.map
@@ -2,10 +2,33 @@ import type { Context } from '@deepseek-ai/cordis';
2
2
  import type { Agent } from '@deepseek-ai/dsh-agent';
3
3
  import { TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol';
4
4
  import type { TurnSelection, TurnSelectionStore } from './turn-selection.ts';
5
+ import type { RequestedModels } from './turn-request-models.ts';
5
6
  /** Public Host binding for the existing bounded, runtime-only decision store. */
6
7
  export declare class TurnSelectionController extends TypertRemoteService {
7
8
  private readonly selections;
8
9
  constructor(ctx: Context, selections: TurnSelectionStore);
9
10
  get(agent: Agent, turn: number): TurnSelection;
11
+ allocationSummary(agent: Agent): {
12
+ policyVersion: "high-cost-v1";
13
+ scope: "viewed-session-host-lifetime";
14
+ status: "observed" | "not-collected";
15
+ retainedDecisions: number;
16
+ noFitDecisions: number;
17
+ rowsTruncated: boolean;
18
+ completeHistory: false;
19
+ observationStart: number | null;
20
+ observationEnd: number | null;
21
+ rows: {
22
+ modelId: string;
23
+ category: "unknown" | "powerful" | "versatile" | "lightweight";
24
+ highCost: boolean;
25
+ previous: boolean;
26
+ weight: number;
27
+ opportunities: number;
28
+ expectedSelections: number;
29
+ selections: number;
30
+ }[];
31
+ };
32
+ requestedModels(agent: Agent, turn: number): RequestedModels;
10
33
  }
11
34
  //# sourceMappingURL=turn-selection-host.d.ts.map
@@ -1,10 +1,14 @@
1
1
  import type { RemoteResult, TypertRemoteContribution } from '@deepseek-ai/dsh-typert-protocol';
2
2
  import type { TurnSelection } from './turn-selection.ts';
3
+ import type { RequestedModels } from './turn-request-models.ts';
4
+ import type { AutoAllocationSummary } from './auto-allocation-evidence.ts';
3
5
  export { TurnSelectionSchema } from './turn-selection.ts';
4
6
  declare module '@deepseek-ai/dsh-typert-protocol' {
5
7
  interface TypertRemoteNamespaceMap {
6
8
  githubCopilotTurnSelection: {
7
9
  get(agentId: string, turn: number): Promise<RemoteResult<TurnSelection>>;
10
+ requestedModels(agentId: string, turn: number): Promise<RemoteResult<RequestedModels>>;
11
+ allocationSummary(agentId: string): Promise<RemoteResult<AutoAllocationSummary>>;
8
12
  };
9
13
  }
10
14
  }
@@ -1,6 +1,7 @@
1
1
  import type { AutoModelPreference } from './copilot-identity.ts';
2
2
  import { z } from 'zod';
3
3
  import type { AutoSelectionExplanation } from './auto-model-routing.ts';
4
+ import type { AutoAllocationSummary } from './auto-allocation-evidence.ts';
4
5
  export declare const TurnSelectionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
5
6
  mode: z.ZodLiteral<"unknown">;
6
7
  }, z.core.$strict>, z.ZodObject<{
@@ -8,8 +9,8 @@ export declare const TurnSelectionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
8
9
  }, z.core.$strict>, z.ZodObject<{
9
10
  mode: z.ZodLiteral<"auto">;
10
11
  preference: z.ZodEnum<{
11
- efficiency: "efficiency";
12
12
  balance: "balance";
13
+ efficiency: "efficiency";
13
14
  intelligence: "intelligence";
14
15
  }>;
15
16
  reason: z.ZodEnum<{
@@ -42,12 +43,12 @@ export declare const TurnSelectionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
42
43
  semantic: "semantic";
43
44
  }>;
44
45
  diagnostic: z.ZodOptional<z.ZodEnum<{
46
+ disabled: "disabled";
45
47
  unavailable: "unavailable";
48
+ failed: "failed";
46
49
  "context-omitted": "context-omitted";
47
- disabled: "disabled";
48
50
  "invalid-result": "invalid-result";
49
51
  timeout: "timeout";
50
- failed: "failed";
51
52
  }>>;
52
53
  semantic: z.ZodOptional<z.ZodObject<{
53
54
  modelId: z.ZodOptional<z.ZodString>;
@@ -81,19 +82,31 @@ export declare const TurnSelectionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
81
82
  lightweight: "lightweight";
82
83
  }>;
83
84
  selectedCategory: z.ZodEnum<{
85
+ unknown: "unknown";
84
86
  powerful: "powerful";
85
87
  versatile: "versatile";
86
88
  lightweight: "lightweight";
87
- unknown: "unknown";
88
89
  }>;
89
90
  categoryCandidateCount: z.ZodNumber;
90
91
  method: z.ZodEnum<{
91
92
  continuity: "continuity";
92
93
  "equal-distribution": "equal-distribution";
94
+ "weighted-distribution": "weighted-distribution";
93
95
  "only-candidate": "only-candidate";
94
96
  "no-fit": "no-fit";
95
97
  }>;
96
98
  fallback: z.ZodBoolean;
99
+ allocation: z.ZodOptional<z.ZodObject<{
100
+ policyVersion: z.ZodLiteral<"high-cost-v1">;
101
+ selectedModelId: z.ZodString;
102
+ candidates: z.ZodArray<z.ZodObject<{
103
+ modelId: z.ZodString;
104
+ highCost: z.ZodBoolean;
105
+ previous: z.ZodBoolean;
106
+ weight: z.ZodNumber;
107
+ expectedShare: z.ZodNumber;
108
+ }, z.core.$strict>>;
109
+ }, z.core.$strict>>;
97
110
  }, z.core.$strict>>;
98
111
  }, z.core.$strict>], "mode">;
99
112
  export type TurnSelection = {
@@ -111,8 +124,10 @@ export type TurnSelection = {
111
124
  /** Ephemeral decision evidence only; never session content, model usage or credentials. */
112
125
  export declare class TurnSelectionStore {
113
126
  private readonly agents;
127
+ private timestamps;
114
128
  record(agent: object, turn: number, selection: TurnSelection): void;
115
129
  get(agent: object, turn: number): TurnSelection;
130
+ allocationSummary(agent: object): AutoAllocationSummary;
116
131
  remove(agent: object): void;
117
132
  clear(): void;
118
133
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "dsh-github-copilot",
3
3
  "description": "DSH companion for GitHub Copilot sign-in, account-aware model profiles, tool compatibility, and provider-hosted search.",
4
- "version": "0.4.0-alpha.99",
4
+ "version": "0.4.0",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "registry": "https://registry.npmjs.org/",
8
- "tag": "alpha"
8
+ "tag": "latest"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
@@ -54,6 +54,7 @@
54
54
  "AGENTS.md",
55
55
  "docs/agent-readiness.md",
56
56
  "docs/model-compatibility-acceptance.md",
57
+ "docs/auto-review-sampling.md",
57
58
  "docs/single-route-migration.md",
58
59
  "docs/session-search-routing.md",
59
60
  "docs/npm-distribution.md",
@@ -65,8 +66,19 @@
65
66
  "docs/copilot-compaction.md",
66
67
  "docs/manual-compaction-recovery.md",
67
68
  "docs/copilot-usage.md",
69
+ "docs/copilot-accounts.md",
70
+ "docs/session-continuation.md",
71
+ "docs/account-management-experience.md",
72
+ "docs/mockups/copilot-account-management.html",
73
+ "docs/mockups/copilot-account-dropdown.html",
68
74
  "docs/automatic-model-routing.md",
69
75
  "docs/auto-task-routing.md",
76
+ "docs/auto-high-cost.md",
77
+ "docs/evidence-driven-iteration.md",
78
+ "docs/plugin-diagnostics.md",
79
+ "docs/auto-iteration-review.json",
80
+ "scripts/analyze-diagnostics.mjs",
81
+ "scripts/iteration-review.mjs",
70
82
  "docs/image-input-compatibility.md",
71
83
  "output/auto-model-mockup/index.html",
72
84
  "docs/images/dual-model-provenance.json",
@@ -141,7 +153,7 @@
141
153
  "dependencies": {
142
154
  "@earendil-works/pi-ai": "0.87.1",
143
155
  "undici": "6.28.1",
144
- "zod": "^4.4.3"
156
+ "zod": "4.6.5"
145
157
  },
146
158
  "devDependencies": {
147
159
  "@deepseek-ai/cordis": "~4.0.4",
@@ -171,6 +183,9 @@
171
183
  "@deepseek-ai/dsh-session-format-catalog": "0.2.0-rc.2",
172
184
  "@deepseek-ai/dsh-session-projection": "0.2.0-rc.2",
173
185
  "@deepseek-ai/dsh-settings": "0.2.0-rc.2",
186
+ "@deepseek-ai/dsh-storage": "0.2.0-rc.2",
187
+ "@deepseek-ai/dsh-storage-domain": "0.2.0-rc.2",
188
+ "@deepseek-ai/dsh-storage-json": "0.2.0-rc.2",
174
189
  "@deepseek-ai/dsh-system-prompt": "0.2.0-rc.2",
175
190
  "@deepseek-ai/dsh-tool-web": "0.2.0-rc.2",
176
191
  "@deepseek-ai/dsh-tools": "0.2.0-rc.2",
@@ -218,6 +233,7 @@
218
233
  "typecheck": "tsc -p tsconfig.json --noEmit",
219
234
  "typecheck:tests": "tsc -p tsconfig.tests.json",
220
235
  "test:scripts": "node --test \"tests/scripts/*.test.mjs\"",
236
+ "diagnostics:analyze": "node scripts/analyze-diagnostics.mjs",
221
237
  "agent:describe": "node scripts/agent.mjs describe --json",
222
238
  "agent:doctor": "node scripts/agent.mjs doctor --json",
223
239
  "agent:plan": "node scripts/agent.mjs plan",
@@ -0,0 +1,91 @@
1
+ import { open } from 'node:fs/promises'
2
+ import { basename, isAbsolute, parse, resolve } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
+ import {
5
+ analyzeReviewedView, analyzeSnapshot, diagnosticsUnitName, parsePersistedUnit, parseReviewedView,
6
+ } from '../lib/diagnostics-analysis.js'
7
+ import { readBoundedDiagnosticsFile } from '../lib/diagnostics-analysis-io.js'
8
+
9
+ const usage = 'Usage: node scripts/analyze-diagnostics.mjs --input <absolute-file> --mode persisted-unit --profile-name <name> --output <new-local-file>\n'
10
+ + ' or: node scripts/analyze-diagnostics.mjs --input <absolute-file> --mode reviewed-view --output <new-local-file>'
11
+ const supportedErrors = new Set([
12
+ 'INVALID_PROFILE', 'INVALID_UNIT', 'INVALID_SNAPSHOT', 'DUPLICATE_AGGREGATE',
13
+ 'INVALID_OPTIONS', 'INVALID_VIEW', 'INVALID_ARGUMENTS', 'INPUT_LIMIT', 'INVALID_JSON', 'INVALID_PATH',
14
+ 'SYMLINK_REJECTED', 'NOT_REGULAR_FILE', 'FILE_CHANGED', 'FILE_READ_FAILED', 'INVALID_UTF8',
15
+ 'INVALID_REPORT', 'OUTPUT_UNAVAILABLE',
16
+ ])
17
+
18
+ function parseArguments(args) {
19
+ const values = new Map()
20
+ for (let index = 0; index < args.length; index += 2) {
21
+ const key = args[index]
22
+ const value = args[index + 1]
23
+ if (!['--input', '--mode', '--profile-name', '--output'].includes(key)
24
+ || !value || value.startsWith('--') || values.has(key)) {
25
+ throw new Error('COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_ARGUMENTS')
26
+ }
27
+ values.set(key, value)
28
+ }
29
+ const mode = values.get('--mode')
30
+ if (!values.has('--input') || !values.has('--output')
31
+ || !['persisted-unit', 'reviewed-view'].includes(mode)
32
+ || (mode === 'persisted-unit') !== values.has('--profile-name')) {
33
+ throw new Error('COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_ARGUMENTS')
34
+ }
35
+ const input = values.get('--input')
36
+ const output = values.get('--output')
37
+ if (!isAbsolute(input) || !isAbsolute(output))
38
+ throw new Error('COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_PATH')
39
+ const inputPath = resolve(input)
40
+ const outputPath = resolve(output)
41
+ const comparable = value => process.platform === 'win32' ? value.toLowerCase() : value
42
+ if (comparable(inputPath) === comparable(outputPath)
43
+ || (process.platform === 'win32' && parse(outputPath).root.startsWith('\\\\'))) {
44
+ throw new Error('COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_PATH')
45
+ }
46
+ return {
47
+ inputPath, outputPath, mode,
48
+ profileName: values.get('--profile-name'),
49
+ }
50
+ }
51
+
52
+ function errorCode(error) {
53
+ const prefix = 'COPILOT_DIAGNOSTICS_ANALYSIS_'
54
+ if (error instanceof Error && error.message.startsWith(prefix)) {
55
+ const candidate = error.message.slice(prefix.length)
56
+ if (supportedErrors.has(candidate)) return candidate
57
+ }
58
+ return 'ANALYSIS_FAILED'
59
+ }
60
+
61
+ async function main(args) {
62
+ if (args.length === 1 && args[0] === '--help') {
63
+ process.stdout.write(`${usage}\n`)
64
+ return
65
+ }
66
+ const options = parseArguments(args)
67
+ const text = await readBoundedDiagnosticsFile(options.inputPath)
68
+ const observedAt = Date.now()
69
+ let report
70
+ if (options.mode === 'persisted-unit') {
71
+ const unitName = diagnosticsUnitName(options.profileName)
72
+ if (basename(options.inputPath) !== `${unitName}.json`)
73
+ throw new Error('COPILOT_DIAGNOSTICS_ANALYSIS_INVALID_UNIT')
74
+ const snapshot = parsePersistedUnit(text, unitName)
75
+ report = analyzeSnapshot(snapshot, { source: 'persisted-snapshot', observedAt })
76
+ } else {
77
+ report = analyzeReviewedView(parseReviewedView(text), observedAt)
78
+ }
79
+ let handle
80
+ try { handle = await open(options.outputPath, 'wx') } catch {
81
+ throw new Error('COPILOT_DIAGNOSTICS_ANALYSIS_OUTPUT_UNAVAILABLE')
82
+ }
83
+ try { await handle.writeFile(`${JSON.stringify(report, null, 2)}\n`, 'utf8') } finally { await handle.close() }
84
+ }
85
+
86
+ if (process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
87
+ main(process.argv.slice(2)).catch(error => {
88
+ process.stderr.write(`COPILOT_DIAGNOSTICS_ANALYSIS_${errorCode(error)}\n`)
89
+ process.exitCode = 1
90
+ })
91
+ }
@@ -0,0 +1,53 @@
1
+ import { open } from 'node:fs/promises'
2
+ import { resolve } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
+
5
+ export function validateIterationReview(value) {
6
+ const require = (condition, message) => { if (!condition) throw new Error(`Iteration review: ${message}`) }
7
+ const object = value => value !== null && typeof value === 'object' && !Array.isArray(value)
8
+ const keys = (value, allowed) => require(object(value) && Object.keys(value).every(key => allowed.includes(key)), 'unknown or invalid fields')
9
+ const text = value => typeof value === 'string' && value.trim().length > 0 && value.length <= 2000
10
+ keys(value, ['schemaVersion', 'feature', 'status', 'policyVersion', 'observation', 'previousReview', 'findings', 'decision', 'validation', 'followUp'])
11
+ require(value.schemaVersion === 1 && value.feature === 'autorouting', 'unsupported version or feature')
12
+ require(['observed', 'not-collected', 'insufficient-sample', 'unavailable'].includes(value.status), 'explicit evidence status required')
13
+ require(text(value.policyVersion), 'policy version required')
14
+ keys(value.observation, ['start', 'end', 'sampleCount', 'limitations'])
15
+ const { start, end, sampleCount, limitations } = value.observation
16
+ require(Number.isSafeInteger(sampleCount) && sampleCount >= 0, 'invalid sample count')
17
+ require(text(limitations), 'observation limitations required')
18
+ if (value.status === 'observed' || value.status === 'insufficient-sample') {
19
+ require(typeof start === 'string' && typeof end === 'string'
20
+ && Number.isFinite(Date.parse(start)) && Number.isFinite(Date.parse(end))
21
+ && Date.parse(start) <= Date.parse(end), 'valid observation window required')
22
+ require(sampleCount > 0, 'observed evidence needs samples')
23
+ } else require(start === null && end === null && sampleCount === 0, 'missing evidence cannot claim a window')
24
+ require(value.previousReview === null || text(value.previousReview), 'invalid prior review reference')
25
+ for (const field of ['findings', 'decision', 'validation']) require(text(value[field]), `${field} required`)
26
+ keys(value.followUp, ['owner', 'trigger', 'metric', 'action'])
27
+ for (const field of ['owner', 'trigger', 'metric', 'action']) require(text(value.followUp[field]), `follow-up ${field} required`)
28
+ return { schemaVersion: 1, ok: true, feature: value.feature, evidenceStatus: value.status,
29
+ sampleCount, reviewDocumentOnly: true, evidenceTruthVerified: false }
30
+ }
31
+
32
+ export async function main(args = process.argv.slice(2)) {
33
+ try {
34
+ if (args.length !== 2 || args[0] !== '--report') throw new Error('Invalid arguments')
35
+ const handle = await open(resolve(args[1]), 'r')
36
+ let value
37
+ try {
38
+ const info = await handle.stat()
39
+ if (!info.isFile() || info.size > 32768) throw new Error('Invalid report file')
40
+ const bytes = Buffer.alloc(32769)
41
+ const { bytesRead } = await handle.read(bytes, 0, bytes.length, 0)
42
+ if (bytesRead > 32768) throw new Error('Report exceeds 32 KiB')
43
+ value = JSON.parse(bytes.subarray(0, bytesRead).toString('utf8'))
44
+ } finally { await handle.close() }
45
+ const review = validateIterationReview(value)
46
+ console.log(JSON.stringify({ ...review, report: value }))
47
+ return 0
48
+ } catch {
49
+ console.error('ITERATION_REVIEW_INVALID: provide a bounded report matching docs/evidence-driven-iteration.md')
50
+ return 1
51
+ }
52
+ }
53
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) process.exitCode = await main()