archctx-contracts 0.17.1 → 0.18.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.
Files changed (112) hide show
  1. package/fixtures/valid/archctx-capabilities.json +2 -2
  2. package/fixtures/valid/product-version-manifest.json +4 -10
  3. package/package.json +4 -1
  4. package/schemas/repo/refactor-decision.schema.json +307 -37
  5. package/schemas/runtime/archctx-capabilities.schema.json +144 -20
  6. package/schemas/runtime/architecture-debt-delta.schema.json +58 -19
  7. package/schemas/runtime/architecture-debt.schema.json +42 -42
  8. package/schemas/runtime/architecture-event.schema.json +1 -1
  9. package/schemas/runtime/architecture-history.schema.json +354 -0
  10. package/schemas/runtime/capability-dependencies.schema.json +266 -59
  11. package/schemas/runtime/cli-check.schema.json +209 -0
  12. package/schemas/runtime/cli-help.schema.json +28 -0
  13. package/schemas/runtime/dependency-constraint-evaluation.schema.json +67 -68
  14. package/schemas/runtime/docs-anchors.schema.json +262 -67
  15. package/schemas/runtime/doctor-code-facts.schema.json +49 -0
  16. package/schemas/runtime/evidence-binding.schema.json +1 -1
  17. package/schemas/runtime/flow-selector-check.schema.json +179 -46
  18. package/schemas/runtime/hook-debt-checkpoint.schema.json +12 -12
  19. package/schemas/runtime/index-seed.schema.json +56 -0
  20. package/schemas/runtime/path-context.schema.json +120 -0
  21. package/schemas/runtime/product-version-manifest.schema.json +142 -54
  22. package/schemas/runtime/projection-slice.schema.json +34 -34
  23. package/schemas/runtime/recommendation-v3.schema.json +372 -1
  24. package/schemas/runtime/ref-impact-batch-request.schema.json +20 -7
  25. package/schemas/runtime/ref-impact-batch.schema.json +83 -13
  26. package/schemas/runtime/ref-impact.schema.json +68 -61
  27. package/schemas/runtime/refactor-scan-v2.schema.json +1392 -0
  28. package/schemas/runtime/refactor-simulate.schema.json +553 -0
  29. package/schemas/runtime/refactor-verify-v2.schema.json +172 -0
  30. package/schemas/runtime/resolve-path.schema.json +69 -0
  31. package/schemas/runtime/runtime-recommendation-show.schema.json +531 -2
  32. package/schemas/runtime/runtime-refactor-scan.schema.json +498 -2
  33. package/schemas/runtime/runtime-verify.schema.json +274 -56
  34. package/schemas/runtime/state-machine-check.schema.json +357 -89
  35. package/schemas/runtime/structural-observation-payload.schema.json +372 -1
  36. package/schemas/runtime/sync-check.schema.json +77 -0
  37. package/src/architecture-debt.ts +125 -38
  38. package/src/architecture-history.ts +168 -42
  39. package/src/architecture.ts +100 -0
  40. package/src/cli-read.ts +228 -0
  41. package/src/docs-anchors.ts +105 -50
  42. package/src/explorer-occurrence-ports.ts +175 -0
  43. package/src/explorer-query-ports.ts +270 -0
  44. package/src/failure.ts +4 -0
  45. package/src/index-seed.ts +42 -0
  46. package/src/index.ts +5 -0
  47. package/src/ledger-records.ts +262 -0
  48. package/src/ledger.ts +27 -566
  49. package/src/ownership-dependencies.ts +115 -5
  50. package/src/ports.ts +6 -541
  51. package/src/product-version.ts +74 -55
  52. package/src/projection-apply-types.ts +216 -0
  53. package/src/projection-apply-validators.ts +355 -0
  54. package/src/projection-capabilities.ts +181 -0
  55. package/src/projection-constants.ts +317 -0
  56. package/src/projection-manifest-types.ts +298 -0
  57. package/src/projection-manifest.ts +12 -299
  58. package/src/projection-recovery-validators.ts +229 -0
  59. package/src/projection-request-validators.ts +326 -0
  60. package/src/projection-types.ts +365 -0
  61. package/src/projection-validation-helpers.ts +15 -0
  62. package/src/projection.ts +7 -1689
  63. package/src/ref-impact.ts +225 -33
  64. package/src/refactor-constants.ts +213 -0
  65. package/src/refactor-decision-validators.ts +351 -0
  66. package/src/refactor-evidence-types.ts +286 -0
  67. package/src/refactor-observation-validators.ts +359 -0
  68. package/src/refactor-resolution-validators.ts +229 -0
  69. package/src/refactor-snapshot-validators.ts +368 -0
  70. package/src/refactor-types.ts +270 -0
  71. package/src/refactor-v2.ts +554 -0
  72. package/src/refactor-validation-helpers.ts +199 -0
  73. package/src/refactor.ts +7 -1849
  74. package/src/review-guards.ts +3 -23
  75. package/src/runtime-verify.ts +87 -0
  76. package/src/schema-kit.ts +187 -0
  77. package/src/schema-registry.ts +73 -0
  78. package/src/schema.ts +15 -4
  79. package/src/state-machine-schemas.ts +192 -0
  80. package/src/state-machine-types.ts +188 -0
  81. package/src/state-machine.ts +9 -189
  82. package/fixtures/boundary/agent-job-expired.json +0 -33
  83. package/fixtures/invalid/adapter-fidelity-reverse-sync.json +0 -9
  84. package/fixtures/invalid/agent-job-direct-mutation.json +0 -32
  85. package/fixtures/invalid/cloud-egress-envelope-private-field.json +0 -12
  86. package/fixtures/invalid/investigation-report-direct-mutation.json +0 -38
  87. package/fixtures/invalid/investigation-report-missing-proposed-delta.json +0 -19
  88. package/fixtures/invalid/notification-event-code-field.json +0 -11
  89. package/fixtures/valid/adapter-fidelity.json +0 -9
  90. package/fixtures/valid/agent-job.json +0 -33
  91. package/fixtures/valid/architecture-candidate-delta-policy.json +0 -45
  92. package/fixtures/valid/architecture-candidate-delta.json +0 -188
  93. package/fixtures/valid/chatgpt-ga-tool.json +0 -9
  94. package/fixtures/valid/cloud-egress-envelope.json +0 -11
  95. package/fixtures/valid/entitlement.json +0 -12
  96. package/fixtures/valid/investigation-report.json +0 -38
  97. package/fixtures/valid/likec4-mapping.json +0 -14
  98. package/fixtures/valid/notification-event.json +0 -10
  99. package/fixtures/valid/notification-provider.json +0 -12
  100. package/fixtures/valid/structurizr-mapping.json +0 -15
  101. package/schemas/cloud/cloud-egress-envelope.schema.json +0 -19
  102. package/schemas/cloud/entitlement.schema.json +0 -24
  103. package/schemas/integrations/adapter-fidelity.schema.json +0 -21
  104. package/schemas/integrations/chatgpt-ga-tool.schema.json +0 -25
  105. package/schemas/integrations/likec4-mapping.schema.json +0 -32
  106. package/schemas/integrations/structurizr-mapping.schema.json +0 -33
  107. package/schemas/runtime/agent-job.schema.json +0 -63
  108. package/schemas/runtime/architecture-candidate-delta-policy.schema.json +0 -129
  109. package/schemas/runtime/architecture-candidate-delta.schema.json +0 -386
  110. package/schemas/runtime/investigation-report.schema.json +0 -119
  111. package/schemas/runtime/notification-event.schema.json +0 -18
  112. package/schemas/runtime/notification-provider.schema.json +0 -26
@@ -0,0 +1,554 @@
1
+ import {
2
+ REFACTOR_NAMESPACE_PATTERN,
3
+ REFACTOR_OBSERVATION_KINDS,
4
+ REFACTOR_DETECTOR_EXTRA_KINDS,
5
+ REFACTOR_KILL_LIST_SYMBOL_TARGET_REGEX,
6
+ formatRefactorKillListSymbolTarget,
7
+ parseRefactorKillListSymbolTarget,
8
+ isValidRefactorKillListSymbolTarget
9
+ } from "./refactor";
10
+ import type {
11
+ RefactorEvidenceCoChangePairV1,
12
+ RefactorEvidenceDirectionViolationV1,
13
+ RefactorEvidenceImportEdgeV1,
14
+ RefactorEvidenceLowCohesionClusterV1,
15
+ RefactorEvidenceLowCohesionFileV1,
16
+ RefactorEvidencePairDirectionV1,
17
+ RefactorEvidencePathV1,
18
+ RefactorEvidenceUnresolvedImportV1,
19
+ RefactorEvidenceUnstableTargetV1,
20
+ RefactorObservationEvidenceV1
21
+ } from "./refactor-evidence-types";
22
+ import {
23
+ AnyOf,
24
+ BoundedStringSchema,
25
+ Const,
26
+ CountSchema,
27
+ GitCommitShaSchema,
28
+ NonEmptyStringSchema,
29
+ NullableType,
30
+ OneOf,
31
+ RawEnum,
32
+ RecordOf,
33
+ Ref,
34
+ RepoPathSchema,
35
+ SchemaDocument,
36
+ Sha256DigestSchema,
37
+ StrictObject,
38
+ StringEnum,
39
+ Type,
40
+ UniqueStringArray,
41
+ type Static
42
+ } from "./schema-kit";
43
+
44
+ export {
45
+ REFACTOR_KILL_LIST_SYMBOL_TARGET_REGEX,
46
+ formatRefactorKillListSymbolTarget,
47
+ parseRefactorKillListSymbolTarget,
48
+ isValidRefactorKillListSymbolTarget
49
+ };
50
+
51
+ export const REFACTOR_SCAN_V2_SCHEMA_VERSION = "archcontext.refactor-scan/v2" as const;
52
+ export const REFACTOR_VERIFY_V2_SCHEMA_VERSION = "archcontext.refactor-verify/v2" as const;
53
+ export const REFACTOR_SIMULATION_REQUEST_SCHEMA_VERSION = "archcontext.refactor-simulation-request/v1" as const;
54
+ export const REFACTOR_SIMULATE_V1_SCHEMA_VERSION = "archcontext.runtime-refactor-simulate/v1" as const;
55
+
56
+ export const V2_REFACTOR_DETECTOR_KINDS = [
57
+ "file-cycle",
58
+ "component-cycle",
59
+ "god-file",
60
+ "low-cohesion-god-file",
61
+ "entrypoint-bypass",
62
+ "undeclared-dependency",
63
+ "purity-violation",
64
+ "dead-public-export",
65
+ "unstable-dependency",
66
+ "shotgun-surgery-hotspot",
67
+ "co-change-coupling"
68
+ ] as const;
69
+ export type V2RefactorDetectorKind = (typeof V2_REFACTOR_DETECTOR_KINDS)[number];
70
+
71
+ export const ALL_REFACTOR_DECISION_KINDS = [
72
+ ...REFACTOR_OBSERVATION_KINDS,
73
+ ...REFACTOR_DETECTOR_EXTRA_KINDS
74
+ ].sort() as readonly (
75
+ | (typeof REFACTOR_OBSERVATION_KINDS)[number]
76
+ | (typeof REFACTOR_DETECTOR_EXTRA_KINDS)[number]
77
+ )[];
78
+
79
+ const RefactorDecisionScopeSchema = OneOf([
80
+ StrictObject({ kind: Const("repository") }),
81
+ StrictObject({ kind: Const("node"), nodeId: NonEmptyStringSchema }),
82
+ StrictObject({ kind: Const("paths"), paths: UniqueStringArray(RepoPathSchema, { minItems: 1 }) })
83
+ ]);
84
+
85
+ const RefactorDecisionOutcomeSchema = StrictObject({
86
+ outcomeId: NonEmptyStringSchema,
87
+ metric: NonEmptyStringSchema,
88
+ subjectSelectorId: NonEmptyStringSchema,
89
+ nodeId: NullableType("string"),
90
+ memberNodeIds: Type.Optional(UniqueStringArray(NonEmptyStringSchema, { minItems: 2 })),
91
+ operator: StringEnum(["absent", "equals", "greater_than", "less_than", "present"] as const),
92
+ value: AnyOf([Type.Number(), Type.Null()]),
93
+ required: Type.Boolean()
94
+ });
95
+
96
+ export const ISO_TIMESTAMP_REGEX = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,9})?(Z|[+-]\\d{2}:\\d{2})$";
97
+ export const GRAPH_LINE_TEXT_REGEX = "^\\S(?:[^\\u0000-\\u001f\\u007f]*\\S)?$";
98
+
99
+ const ObservationMetricValueSchema = AnyOf([
100
+ Type.Null(),
101
+ Type.Number()
102
+ ]);
103
+
104
+ export const RefactorKillListTargetV2Schema = OneOf([
105
+ StrictObject({
106
+ kind: Const("path"),
107
+ target: RepoPathSchema,
108
+ required: Type.Boolean()
109
+ }),
110
+ StrictObject({
111
+ kind: Const("relation"),
112
+ target: NonEmptyStringSchema,
113
+ required: Type.Boolean()
114
+ }),
115
+ StrictObject({
116
+ kind: Const("symbol"),
117
+ target: Type.String({ pattern: REFACTOR_KILL_LIST_SYMBOL_TARGET_REGEX }),
118
+ required: Type.Boolean()
119
+ })
120
+ ]);
121
+ export type RefactorKillListTargetV2 = Static<typeof RefactorKillListTargetV2Schema>;
122
+
123
+ export const RefactorDecisionFileBodySchema = StrictObject({
124
+ schemaVersion: Const("archcontext.refactor-decision/v1"),
125
+ namespace: Type.String({ pattern: REFACTOR_NAMESPACE_PATTERN.source }),
126
+ recommendationId: Type.String({ pattern: "^recommendation\\.[a-f0-9]{16}$" }),
127
+ fingerprint: Sha256DigestSchema,
128
+ fingerprintSchemaVersion: Const("archcontext.recommendation-fingerprint/v3"),
129
+ category: StringEnum(["refactor_proposal", "structural_observation"] as const),
130
+ kind: AnyOf([
131
+ Type.Null(),
132
+ StringEnum(ALL_REFACTOR_DECISION_KINDS as unknown as readonly [string, ...string[]])
133
+ ]),
134
+ subject: StrictObject({
135
+ selectorKind: StringEnum(["node", "repository"] as const),
136
+ stableKey: Type.String({ minLength: 1, maxLength: 512, pattern: GRAPH_LINE_TEXT_REGEX }),
137
+ subjectSelectorId: Type.String({ pattern: "^subject\\.(node|repository)\\.[a-f0-9]{16}$" })
138
+ }),
139
+ affectedNodeIds: UniqueStringArray(NonEmptyStringSchema, { maxItems: 32 }),
140
+ affectedNodeCount: CountSchema,
141
+ regressesFrom: AnyOf([
142
+ Type.Null(),
143
+ Type.String({ pattern: "^recommendation\\.[a-f0-9]{16}$" })
144
+ ]),
145
+ status: StringEnum([
146
+ "acknowledged",
147
+ "accepted",
148
+ "deferred",
149
+ "rejected",
150
+ "resolved",
151
+ "waived"
152
+ ] as const),
153
+ observationScopeDigest: Type.Optional(
154
+ Type.String({
155
+ pattern: "^sha256:[a-f0-9]{64}$",
156
+ description: "Scope of the measured observation metrics. Absent for repository baselines."
157
+ })
158
+ ),
159
+ observationMetrics: AnyOf([
160
+ Type.Null(),
161
+ Type.Unsafe<Record<string, number | null>>({
162
+ type: "object",
163
+ additionalProperties: ObservationMetricValueSchema
164
+ })
165
+ ]),
166
+ reason: Type.String({ minLength: 1, maxLength: 1024, pattern: GRAPH_LINE_TEXT_REGEX }),
167
+ actor: StrictObject({
168
+ kind: StringEnum([
169
+ "cli",
170
+ "daemon",
171
+ "developer",
172
+ "hook",
173
+ "mcp",
174
+ "migration",
175
+ "subagent",
176
+ "system"
177
+ ] as const),
178
+ id: Type.String({ minLength: 1, maxLength: 128, pattern: GRAPH_LINE_TEXT_REGEX })
179
+ }),
180
+ decidedAt: Type.String({ pattern: ISO_TIMESTAMP_REGEX }),
181
+ provenance: StrictObject({
182
+ headSha: GitCommitShaSchema,
183
+ assessmentDigest: Sha256DigestSchema,
184
+ baselineSnapshotDigest: Sha256DigestSchema
185
+ }),
186
+ snapshotDigest: Type.Optional(Sha256DigestSchema),
187
+ evidenceDigest: Type.Optional(Sha256DigestSchema),
188
+ resolutionDigest: Type.Optional(Sha256DigestSchema),
189
+ scope: Type.Optional(RefactorDecisionScopeSchema),
190
+ importNodeLimit: Type.Optional(Type.Integer({ minimum: 1 })),
191
+ targetOutcomes: Type.Optional(Type.Array(RefactorDecisionOutcomeSchema)),
192
+ killList: Type.Optional(Type.Array(RefactorKillListTargetV2Schema))
193
+ });
194
+
195
+ export const RefactorDecisionFileSchema = SchemaDocument({
196
+ $id: "https://archctx.repoharness.com/schemas/repo/refactor-decision.schema.json",
197
+ title: "RefactorDecision",
198
+ description:
199
+ "The latest human decision for one refactor recommendation generation, stored at .archcontext/refactor-decisions/<recommendationId>.json (ADR-0046). Identity that needs the core hashers (recommendationId from fingerprint and regressesFrom, the namespace-scoped subject selector) is checked by the runtime reader.",
200
+ schema: RefactorDecisionFileBodySchema
201
+ });
202
+ export type RefactorDecisionFileV1 = Static<typeof RefactorDecisionFileSchema>;
203
+
204
+ const EvidenceImportEdgeSchema = StrictObject({
205
+ fromPath: RepoPathSchema,
206
+ fromLine: NullableType("integer", { minimum: 1 }),
207
+ toPath: RepoPathSchema,
208
+ specifier: NonEmptyStringSchema,
209
+ fromNodeId: NonEmptyStringSchema,
210
+ toNodeId: NonEmptyStringSchema
211
+ });
212
+
213
+ const EvidencePairDirectionSchema = StrictObject({
214
+ fromNodeId: NonEmptyStringSchema,
215
+ toNodeId: NonEmptyStringSchema,
216
+ totalCount: CountSchema,
217
+ sampledCount: CountSchema
218
+ });
219
+
220
+ const EvidenceDirectionViolationSchema = StrictObject({
221
+ constraintId: NonEmptyStringSchema,
222
+ fromPath: RepoPathSchema,
223
+ fromLine: NullableType("integer", { minimum: 1 }),
224
+ toPath: RepoPathSchema,
225
+ specifier: NonEmptyStringSchema,
226
+ fromNodeId: NonEmptyStringSchema,
227
+ toNodeId: NonEmptyStringSchema
228
+ });
229
+
230
+ const EvidencePathEntrySchema = StrictObject({
231
+ path: RepoPathSchema,
232
+ candidateOwnerNodeIds: UniqueStringArray(NonEmptyStringSchema),
233
+ candidateOwnerCount: CountSchema
234
+ });
235
+
236
+ const EvidenceUnresolvedImportSchema = StrictObject({
237
+ fromPath: RepoPathSchema,
238
+ fromLine: NullableType("integer", { minimum: 1 }),
239
+ specifier: NonEmptyStringSchema
240
+ });
241
+
242
+ const EvidenceLowCohesionClusterSchema = StrictObject({
243
+ symbols: UniqueStringArray(NonEmptyStringSchema),
244
+ startLine: Type.Integer({ minimum: 1 }),
245
+ endLine: Type.Integer({ minimum: 1 }),
246
+ intraClusterEdges: CountSchema,
247
+ crossClusterEdges: CountSchema
248
+ });
249
+
250
+ const EvidenceLowCohesionFileSchema = StrictObject({
251
+ path: RepoPathSchema,
252
+ lineCount: CountSchema,
253
+ topLevelSymbolCount: CountSchema,
254
+ clusterCount: CountSchema,
255
+ clusters: Type.Array(Ref<RefactorEvidenceLowCohesionClusterV1>("#/$defs/evidenceLowCohesionCluster"))
256
+ });
257
+
258
+ const EvidenceUnstableTargetSchema = StrictObject({
259
+ targetNodeId: NonEmptyStringSchema,
260
+ targetInstability: Type.Number({ minimum: 0, maximum: 1 }),
261
+ delta: Type.Number({ minimum: -1, maximum: 1 }),
262
+ edgeCount: Type.Integer({ minimum: 1 })
263
+ });
264
+
265
+ const EvidenceCoChangePairSchema = StrictObject({
266
+ pathA: RepoPathSchema,
267
+ pathB: RepoPathSchema,
268
+ coChangeCount: Type.Integer({ minimum: 1 }),
269
+ jaccard: Type.Number({ minimum: 0, maximum: 1 })
270
+ });
271
+
272
+ const ObservationEvidenceSchema = OneOf([
273
+ StrictObject({
274
+ kind: Const("cycle"),
275
+ memberNodeIds: UniqueStringArray(NonEmptyStringSchema),
276
+ directions: Type.Array(Ref<RefactorEvidencePairDirectionV1>("#/$defs/evidencePairDirection")),
277
+ edges: Type.Array(Ref<RefactorEvidenceImportEdgeV1>("#/$defs/evidenceImportEdge")),
278
+ totalCount: CountSchema,
279
+ truncated: Type.Boolean()
280
+ }),
281
+ StrictObject({
282
+ kind: Const("residual-cycle"),
283
+ memberNodeIds: UniqueStringArray(NonEmptyStringSchema),
284
+ edges: Type.Array(Ref<RefactorEvidenceImportEdgeV1>("#/$defs/evidenceImportEdge")),
285
+ totalCount: CountSchema,
286
+ truncated: Type.Boolean()
287
+ }),
288
+ StrictObject({
289
+ kind: Const("direction-violation"),
290
+ constraintIds: UniqueStringArray(NonEmptyStringSchema),
291
+ constraintCount: CountSchema,
292
+ violations: Type.Array(Ref<RefactorEvidenceDirectionViolationV1>("#/$defs/evidenceDirectionViolation")),
293
+ totalCount: CountSchema,
294
+ truncated: Type.Boolean()
295
+ }),
296
+ StrictObject({
297
+ kind: StringEnum(["ownership-ambiguous", "undeclared-footprint", "unowned-paths"] as const),
298
+ paths: Type.Array(Ref<RefactorEvidencePathV1>("#/$defs/evidencePathEntry")),
299
+ totalCount: CountSchema,
300
+ truncated: Type.Boolean()
301
+ }),
302
+ StrictObject({
303
+ kind: Const("evidence-gap"),
304
+ coverage: StringEnum(["complete", "partial", "unknown"] as const),
305
+ reasonCodes: UniqueStringArray(BoundedStringSchema),
306
+ unresolvedImports: Type.Array(Ref<RefactorEvidenceUnresolvedImportV1>("#/$defs/evidenceUnresolvedImport")),
307
+ totalCount: CountSchema,
308
+ truncated: Type.Boolean()
309
+ }),
310
+ StrictObject({
311
+ kind: Const("low-cohesion-god-file"),
312
+ files: Type.Array(Ref<RefactorEvidenceLowCohesionFileV1>("#/$defs/evidenceLowCohesionFile")),
313
+ totalCount: CountSchema,
314
+ truncated: Type.Boolean()
315
+ }),
316
+ StrictObject({
317
+ kind: Const("entrypoint-bypass"),
318
+ declaredEntrypointPaths: UniqueStringArray(RepoPathSchema),
319
+ edges: Type.Array(Ref<RefactorEvidenceImportEdgeV1>("#/$defs/evidenceImportEdge")),
320
+ totalCount: CountSchema,
321
+ truncated: Type.Boolean()
322
+ }),
323
+ StrictObject({
324
+ kind: Const("unstable-dependency"),
325
+ sourceFanIn: CountSchema,
326
+ sourceInstability: Type.Number({ minimum: 0, maximum: 1 }),
327
+ targets: Type.Array(Ref<RefactorEvidenceUnstableTargetV1>("#/$defs/evidenceUnstableTarget")),
328
+ totalCount: CountSchema,
329
+ truncated: Type.Boolean()
330
+ }),
331
+ StrictObject({
332
+ kind: Const("co-change-coupling"),
333
+ memberNodeIds: UniqueStringArray(NonEmptyStringSchema),
334
+ coverageStatus: StringEnum(["complete", "shallow", "insufficient-history", "unavailable"] as const),
335
+ windowLimit: Type.Integer({ minimum: 1 }),
336
+ observedCommitCount: CountSchema,
337
+ pairs: Type.Array(Ref<RefactorEvidenceCoChangePairV1>("#/$defs/evidenceCoChangePair")),
338
+ totalCount: CountSchema,
339
+ truncated: Type.Boolean()
340
+ })
341
+ ]);
342
+
343
+ export const RefactorScanFindingV2Schema = StrictObject({
344
+ recommendationId: Type.String({ pattern: "^recommendation\\.[a-f0-9]{16}$" }),
345
+ fingerprint: Sha256DigestSchema,
346
+ detector: StringEnum(V2_REFACTOR_DETECTOR_KINDS),
347
+ severity: RawEnum(["notice", "warning", "error", "critical"] as const),
348
+ blocking: Type.Boolean(),
349
+ capabilityId: NullableType("string", { minLength: 1 }),
350
+ affectedNodeIds: UniqueStringArray(NonEmptyStringSchema),
351
+ affectedPaths: UniqueStringArray(RepoPathSchema),
352
+ killList: Type.Array(RefactorKillListTargetV2Schema),
353
+ metrics: RecordOf(AnyOf([Type.Number(), Type.Null(), Type.Boolean()])),
354
+ evidence: StrictObject({
355
+ summary: BoundedStringSchema,
356
+ coChangeSupport: Type.Optional(Type.Integer({ minimum: 0 })),
357
+ coChangeCoverageOk: Type.Optional(Type.Boolean()),
358
+ thinDirectionEdgeCount: Type.Optional(Type.Integer({ minimum: 0 })),
359
+ sccSize: Type.Optional(Type.Integer({ minimum: 0 })),
360
+ instabilitySource: Type.Optional(Type.Number({ minimum: 0, maximum: 1 })),
361
+ instabilityTarget: Type.Optional(Type.Number({ minimum: 0, maximum: 1 })),
362
+ testCallerCoverage: Type.Optional(NullableType("number", { minimum: 0, maximum: 1 })),
363
+ lcomScore: Type.Optional(Type.Number({ minimum: 0 })),
364
+ splitClusterCount: Type.Optional(Type.Integer({ minimum: 0 })),
365
+ observation: Type.Optional(Ref<RefactorObservationEvidenceV1>("#/$defs/observationEvidence"))
366
+ })
367
+ });
368
+ export type RefactorScanFindingV2 = Static<typeof RefactorScanFindingV2Schema>;
369
+
370
+ export const RefactorScanV2BodySchema = StrictObject({
371
+ schemaVersion: Const(REFACTOR_SCAN_V2_SCHEMA_VERSION),
372
+ scope: OneOf([
373
+ StrictObject({ kind: Const("repository") }),
374
+ StrictObject({ kind: Const("node"), nodeId: NonEmptyStringSchema }),
375
+ StrictObject({ kind: Const("paths"), paths: UniqueStringArray(RepoPathSchema, { minItems: 1 }) })
376
+ ]),
377
+ headSha: GitCommitShaSchema,
378
+ snapshotDigest: Sha256DigestSchema,
379
+ importNodeLimit: Type.Optional(Type.Integer({ minimum: 1 })),
380
+ coverage: StrictObject({
381
+ imports: StringEnum(["complete", "partial", "unavailable"] as const),
382
+ gitHistory: StringEnum(["complete", "shallow", "unavailable"] as const),
383
+ reasonCodes: UniqueStringArray(BoundedStringSchema)
384
+ }),
385
+ findings: Type.Array(RefactorScanFindingV2Schema),
386
+ suppressed: Type.Array(
387
+ StrictObject({
388
+ recommendationId: Type.String({ pattern: "^recommendation\\.[a-f0-9]{16}$" }),
389
+ detector: StringEnum(V2_REFACTOR_DETECTOR_KINDS),
390
+ decisionStatus: StringEnum(["deferred", "rejected", "waived", "resolved"] as const),
391
+ reason: BoundedStringSchema
392
+ })
393
+ )
394
+ });
395
+
396
+ export const RefactorScanV2Schema = SchemaDocument({
397
+ $id: "https://archcontext.dev/schemas/runtime/refactor-scan-v2.schema.json",
398
+ title: "RefactorScanV2",
399
+ description: "v2 Stage 5 deterministic refactor radar output for `archctx scan --json` (#643).",
400
+ schema: RefactorScanV2BodySchema,
401
+ $defs: {
402
+ evidenceImportEdge: EvidenceImportEdgeSchema,
403
+ evidencePairDirection: EvidencePairDirectionSchema,
404
+ evidenceDirectionViolation: EvidenceDirectionViolationSchema,
405
+ evidencePathEntry: EvidencePathEntrySchema,
406
+ evidenceUnresolvedImport: EvidenceUnresolvedImportSchema,
407
+ evidenceLowCohesionCluster: EvidenceLowCohesionClusterSchema,
408
+ evidenceLowCohesionFile: EvidenceLowCohesionFileSchema,
409
+ evidenceUnstableTarget: EvidenceUnstableTargetSchema,
410
+ evidenceCoChangePair: EvidenceCoChangePairSchema,
411
+ observationEvidence: ObservationEvidenceSchema
412
+ }
413
+ });
414
+ export type RefactorScanV2 = Static<typeof RefactorScanV2Schema>;
415
+
416
+ export const RefactorVerifyV2BodySchema = StrictObject({
417
+ schemaVersion: Const(REFACTOR_VERIFY_V2_SCHEMA_VERSION),
418
+ recommendationId: Type.String({ pattern: "^recommendation\\.[a-f0-9]{16}$" }),
419
+ headSha: GitCommitShaSchema,
420
+ snapshotDigest: Sha256DigestSchema,
421
+ disposition: StringEnum([
422
+ "resolved",
423
+ "partially_resolved",
424
+ "unresolved",
425
+ "regressed",
426
+ "stale"
427
+ ] as const),
428
+ resolutionDigest: Sha256DigestSchema,
429
+ killListChecks: Type.Array(
430
+ StrictObject({
431
+ kind: StringEnum(["path", "relation", "symbol"] as const),
432
+ target: NonEmptyStringSchema,
433
+ required: Type.Boolean(),
434
+ status: StringEnum(["removed", "still-present", "uncheckable"] as const)
435
+ })
436
+ ),
437
+ metricChecks: Type.Array(
438
+ StrictObject({
439
+ metric: NonEmptyStringSchema,
440
+ before: AnyOf([Type.Number(), Type.Null()]),
441
+ after: AnyOf([Type.Number(), Type.Null()]),
442
+ satisfied: Type.Boolean()
443
+ })
444
+ ),
445
+ blockingReasons: UniqueStringArray(BoundedStringSchema),
446
+ historicalReceipt: Type.Optional(
447
+ StrictObject({
448
+ status: StringEnum(["acknowledged", "accepted", "deferred", "rejected", "resolved", "waived"] as const),
449
+ headSha: GitCommitShaSchema,
450
+ resolutionDigest: NullableType("string", { pattern: "^sha256:[a-f0-9]{64}$" })
451
+ })
452
+ )
453
+ });
454
+
455
+ export const RefactorVerifyV2Schema = SchemaDocument({
456
+ $id: "https://archcontext.dev/schemas/runtime/refactor-verify-v2.schema.json",
457
+ title: "RefactorVerifyV2",
458
+ description: "v2 Stage 5 attested resolution verification output for `archctx verify <id> --json` (#643).",
459
+ schema: RefactorVerifyV2BodySchema
460
+ });
461
+ export type RefactorVerifyV2 = Static<typeof RefactorVerifyV2Schema>;
462
+
463
+ export const RefactorSimulationMoveSchema = StrictObject({
464
+ path: RepoPathSchema,
465
+ toNodeId: NonEmptyStringSchema,
466
+ symbols: Type.Optional(UniqueStringArray(NonEmptyStringSchema, { minItems: 1 }))
467
+ });
468
+ export type RefactorSimulationMoveV1 = Static<typeof RefactorSimulationMoveSchema>;
469
+
470
+ export interface RefactorSimulationRequestV1 {
471
+ schemaVersion?: typeof REFACTOR_SIMULATION_REQUEST_SCHEMA_VERSION;
472
+ moves?: RefactorSimulationMoveV1[];
473
+ targetSubject?: string;
474
+ expectedHeadSha?: string;
475
+ expectedWorktreeDigest?: string;
476
+ importNodeLimit?: number;
477
+ }
478
+
479
+ const RefactorSimulationMetricsSnapshotSchema = StrictObject({
480
+ pairThinDirectionEdgeCount: CountSchema,
481
+ crossModuleEdgeCount: CountSchema,
482
+ crossModuleCycleCount: CountSchema,
483
+ residualCycleCount: CountSchema,
484
+ directionViolationCount: CountSchema
485
+ });
486
+
487
+ const RefactorSimulationMetricsDeltaValuesSchema = StrictObject({
488
+ pairThinDirectionEdgeCount: Type.Integer(),
489
+ crossModuleEdgeCount: Type.Integer(),
490
+ crossModuleCycleCount: Type.Integer(),
491
+ residualCycleCount: Type.Integer(),
492
+ directionViolationCount: Type.Integer()
493
+ });
494
+
495
+ const RefactorSimulationViolationSchema = StrictObject({
496
+ constraintId: NonEmptyStringSchema,
497
+ fromPath: RepoPathSchema,
498
+ toPath: RepoPathSchema,
499
+ fromNode: NonEmptyStringSchema,
500
+ toNode: NonEmptyStringSchema,
501
+ severity: RawEnum(["notice", "warning", "error", "critical"] as const),
502
+ fromLayer: Type.Optional(NonEmptyStringSchema),
503
+ toLayer: Type.Optional(NonEmptyStringSchema)
504
+ });
505
+
506
+ const RefactorSimulationCutCandidateSchema = StrictObject({
507
+ strategy: StringEnum(["move-source-file", "move-target-file", "extract-shared-leaf", "invert-interface"] as const),
508
+ measured: Type.Boolean(),
509
+ measurementStatus: StringEnum(["measured", "unmeasured"] as const),
510
+ summary: BoundedStringSchema,
511
+ moves: Type.Array(RefactorSimulationMoveSchema),
512
+ thinDirectionEdgeDelta: AnyOf([Type.Integer(), Type.Null()]),
513
+ crossModuleCycleDelta: AnyOf([Type.Integer(), Type.Null()]),
514
+ directionViolationDelta: AnyOf([Type.Integer(), Type.Null()]),
515
+ newlyIntroducedViolationCount: AnyOf([CountSchema, Type.Null()]),
516
+ breaksPair: AnyOf([Type.Boolean(), Type.Null()]),
517
+ caveats: UniqueStringArray(BoundedStringSchema)
518
+ });
519
+
520
+ export const RefactorSimulateV1BodySchema = StrictObject({
521
+ schemaVersion: Const(REFACTOR_SIMULATE_V1_SCHEMA_VERSION),
522
+ beforeSnapshotDigest: Sha256DigestSchema,
523
+ modelDigest: Sha256DigestSchema,
524
+ codeFactsCoverage: StringEnum(["complete", "partial", "unknown"] as const),
525
+ moves: Type.Array(RefactorSimulationMoveSchema),
526
+ targetSubject: AnyOf([Type.String({ minLength: 1 }), Type.Null()]),
527
+ measurementStatus: StringEnum(["measured", "unmeasured"] as const),
528
+ unmeasuredReasons: UniqueStringArray(BoundedStringSchema),
529
+ metricsDelta: StrictObject({
530
+ before: RefactorSimulationMetricsSnapshotSchema,
531
+ after: AnyOf([RefactorSimulationMetricsSnapshotSchema, Type.Null()]),
532
+ delta: AnyOf([RefactorSimulationMetricsDeltaValuesSchema, Type.Null()])
533
+ }),
534
+ newlyIntroducedCycles: Type.Array(NonEmptyStringSchema),
535
+ resolvedCycles: Type.Array(NonEmptyStringSchema),
536
+ newlyIntroducedViolations: Type.Array(RefactorSimulationViolationSchema),
537
+ cutCandidates: Type.Array(RefactorSimulationCutCandidateSchema),
538
+ simulationDigest: Sha256DigestSchema,
539
+ privacy: StrictObject({
540
+ writes: Const("none"),
541
+ rawSourcePersisted: Const(false),
542
+ rawDiffPersisted: Const(false),
543
+ promptPersisted: Const(false)
544
+ })
545
+ });
546
+
547
+ export const RefactorSimulateV1Schema = SchemaDocument({
548
+ $id: "https://archcontext.dev/schemas/runtime/refactor-simulate.schema.json",
549
+ title: "RefactorSimulateV1",
550
+ description: "Read-only move/cut impact simulation preview (#638, #643 Phase 2).",
551
+ schema: RefactorSimulateV1BodySchema
552
+ });
553
+ export type RefactorSimulateV1 = Static<typeof RefactorSimulateV1Schema>;
554
+ export type RefactorSimulationResultV1 = RefactorSimulateV1;