agent-rulebook 0.2.0 → 0.3.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 (181) hide show
  1. package/README.md +58 -4
  2. package/dist/commands/dashboard.d.ts +7 -0
  3. package/dist/commands/dashboard.js +42 -0
  4. package/dist/commands/dashboard.js.map +1 -0
  5. package/dist/commands/diagnostics.d.ts +2 -0
  6. package/dist/commands/diagnostics.js +38 -0
  7. package/dist/commands/diagnostics.js.map +1 -0
  8. package/dist/commands/home.d.ts +2 -0
  9. package/dist/commands/home.js +59 -0
  10. package/dist/commands/home.js.map +1 -0
  11. package/dist/commands/import.d.ts +2 -0
  12. package/dist/commands/import.js +34 -0
  13. package/dist/commands/import.js.map +1 -0
  14. package/dist/commands/pending.d.ts +2 -0
  15. package/dist/commands/pending.js +27 -0
  16. package/dist/commands/pending.js.map +1 -0
  17. package/dist/commands/resolution.js +25 -11
  18. package/dist/commands/resolution.js.map +1 -1
  19. package/dist/commands/stats-renderer.d.ts +3 -0
  20. package/dist/commands/stats-renderer.js +76 -0
  21. package/dist/commands/stats-renderer.js.map +1 -0
  22. package/dist/commands/stats.d.ts +4 -0
  23. package/dist/commands/stats.js +107 -0
  24. package/dist/commands/stats.js.map +1 -0
  25. package/dist/commands/status.d.ts +2 -0
  26. package/dist/commands/status.js +17 -0
  27. package/dist/commands/status.js.map +1 -0
  28. package/dist/commands/sync.js +1 -1
  29. package/dist/commands/sync.js.map +1 -1
  30. package/dist/commands/usage.d.ts +2 -0
  31. package/dist/commands/usage.js +25 -0
  32. package/dist/commands/usage.js.map +1 -0
  33. package/dist/core/codex-call-parser.d.ts +10 -0
  34. package/dist/core/codex-call-parser.js +97 -0
  35. package/dist/core/codex-call-parser.js.map +1 -0
  36. package/dist/core/codex-usage-import.d.ts +24 -0
  37. package/dist/core/codex-usage-import.js +259 -0
  38. package/dist/core/codex-usage-import.js.map +1 -0
  39. package/dist/core/diagnostic-cluster.d.ts +37 -0
  40. package/dist/core/diagnostic-cluster.js +20 -0
  41. package/dist/core/diagnostic-cluster.js.map +1 -0
  42. package/dist/core/environment-normalization.d.ts +21 -0
  43. package/dist/core/environment-normalization.js +42 -0
  44. package/dist/core/environment-normalization.js.map +1 -0
  45. package/dist/core/index.d.ts +1 -0
  46. package/dist/core/index.js +1 -0
  47. package/dist/core/index.js.map +1 -1
  48. package/dist/core/invocation-diagnostics.d.ts +40 -0
  49. package/dist/core/invocation-diagnostics.js +91 -0
  50. package/dist/core/invocation-diagnostics.js.map +1 -0
  51. package/dist/core/legacy-candidate.d.ts +159 -0
  52. package/dist/core/legacy-candidate.js +73 -0
  53. package/dist/core/legacy-candidate.js.map +1 -0
  54. package/dist/core/legacy-import-apply.d.ts +215 -0
  55. package/dist/core/legacy-import-apply.js +172 -0
  56. package/dist/core/legacy-import-apply.js.map +1 -0
  57. package/dist/core/legacy-import-manifest.d.ts +197 -0
  58. package/dist/core/legacy-import-manifest.js +63 -0
  59. package/dist/core/legacy-import-manifest.js.map +1 -0
  60. package/dist/core/legacy-scan.d.ts +71 -0
  61. package/dist/core/legacy-scan.js +99 -0
  62. package/dist/core/legacy-scan.js.map +1 -0
  63. package/dist/core/pending-replay.d.ts +4 -0
  64. package/dist/core/pending-replay.js +96 -0
  65. package/dist/core/pending-replay.js.map +1 -0
  66. package/dist/core/resolution-applicability.d.ts +1 -0
  67. package/dist/core/resolution-applicability.js +28 -11
  68. package/dist/core/resolution-applicability.js.map +1 -1
  69. package/dist/core/resolution-ranker.js +9 -2
  70. package/dist/core/resolution-ranker.js.map +1 -1
  71. package/dist/core/resolution-search-types.d.ts +9 -0
  72. package/dist/core/statistics-data.d.ts +98 -0
  73. package/dist/core/statistics-data.js +53 -0
  74. package/dist/core/statistics-data.js.map +1 -0
  75. package/dist/core/statistics-pages.d.ts +3 -0
  76. package/dist/core/statistics-pages.js +107 -0
  77. package/dist/core/statistics-pages.js.map +1 -0
  78. package/dist/core/statistics-query.d.ts +86 -0
  79. package/dist/core/statistics-query.js +51 -0
  80. package/dist/core/statistics-query.js.map +1 -0
  81. package/dist/core/statistics-types.d.ts +286 -0
  82. package/dist/core/statistics-types.js +2 -0
  83. package/dist/core/statistics-types.js.map +1 -0
  84. package/dist/core/statistics.d.ts +5 -0
  85. package/dist/core/statistics.js +101 -0
  86. package/dist/core/statistics.js.map +1 -0
  87. package/dist/core/sync-status.d.ts +53 -0
  88. package/dist/core/sync-status.js +85 -0
  89. package/dist/core/sync-status.js.map +1 -0
  90. package/dist/core/usage-context.d.ts +15 -0
  91. package/dist/core/usage-context.js +22 -0
  92. package/dist/core/usage-context.js.map +1 -0
  93. package/dist/core/usage-observer.d.ts +36 -0
  94. package/dist/core/usage-observer.js +112 -0
  95. package/dist/core/usage-observer.js.map +1 -0
  96. package/dist/core/usage-schema.d.ts +113 -0
  97. package/dist/core/usage-schema.js +33 -0
  98. package/dist/core/usage-schema.js.map +1 -0
  99. package/dist/dashboard/public/api.js +41 -0
  100. package/dist/dashboard/public/app.js +111 -0
  101. package/dist/dashboard/public/components/detail-panel.js +21 -0
  102. package/dist/dashboard/public/components/index.js +216 -0
  103. package/dist/dashboard/public/components/input-diff.js +15 -0
  104. package/dist/dashboard/public/diagnostic-detail.js +25 -0
  105. package/dist/dashboard/public/diagnostics.js +46 -0
  106. package/dist/dashboard/public/index.html +28 -0
  107. package/dist/dashboard/public/knowledge.js +57 -0
  108. package/dist/dashboard/public/overview.js +101 -0
  109. package/dist/dashboard/public/solution-detail.js +42 -0
  110. package/dist/dashboard/public/styles.css +205 -0
  111. package/dist/dashboard/server.d.ts +10 -0
  112. package/dist/dashboard/server.js +133 -0
  113. package/dist/dashboard/server.js.map +1 -0
  114. package/dist/index.d.ts +4 -0
  115. package/dist/index.js +3 -0
  116. package/dist/index.js.map +1 -1
  117. package/dist/mcp/observed-transport.d.ts +20 -0
  118. package/dist/mcp/observed-transport.js +99 -0
  119. package/dist/mcp/observed-transport.js.map +1 -0
  120. package/dist/mcp/resolution-change.d.ts +1 -1
  121. package/dist/mcp/resolution-change.js +24 -9
  122. package/dist/mcp/resolution-change.js.map +1 -1
  123. package/dist/mcp/resolutions-commit-change-tool.d.ts +5 -0
  124. package/dist/mcp/resolutions-commit-change-tool.js +22 -4
  125. package/dist/mcp/resolutions-commit-change-tool.js.map +1 -1
  126. package/dist/mcp/resolutions-find-tool.d.ts +1 -2
  127. package/dist/mcp/resolutions-find-tool.js +43 -8
  128. package/dist/mcp/resolutions-find-tool.js.map +1 -1
  129. package/dist/mcp/resolutions-list-facets-tool.d.ts +1 -1
  130. package/dist/mcp/resolutions-list-facets-tool.js +16 -2
  131. package/dist/mcp/resolutions-list-facets-tool.js.map +1 -1
  132. package/dist/mcp/resolutions-report-attempt-tool.d.ts +1 -0
  133. package/dist/mcp/resolutions-report-attempt-tool.js +44 -55
  134. package/dist/mcp/resolutions-report-attempt-tool.js.map +1 -1
  135. package/dist/mcp/schemas.d.ts +299 -14
  136. package/dist/mcp/schemas.js +31 -10
  137. package/dist/mcp/schemas.js.map +1 -1
  138. package/dist/mcp/server.js +20 -5
  139. package/dist/mcp/server.js.map +1 -1
  140. package/dist/mcp/stats-get-tool.d.ts +99 -0
  141. package/dist/mcp/stats-get-tool.js +17 -0
  142. package/dist/mcp/stats-get-tool.js.map +1 -0
  143. package/dist/mcp/tool-call.d.ts +99 -0
  144. package/dist/mcp/tool-call.js +20 -0
  145. package/dist/mcp/tool-call.js.map +1 -0
  146. package/dist/mcp/tool-output.d.ts +9 -0
  147. package/dist/mcp/tool-output.js +22 -0
  148. package/dist/mcp/tool-output.js.map +1 -1
  149. package/dist/mcp/tools.d.ts +182 -27
  150. package/dist/mcp/tools.js +9 -2
  151. package/dist/mcp/tools.js.map +1 -1
  152. package/dist/program.js +16 -0
  153. package/dist/program.js.map +1 -1
  154. package/dist/storage/canonical-write-lock.d.ts +2 -0
  155. package/dist/storage/canonical-write-lock.js +32 -0
  156. package/dist/storage/canonical-write-lock.js.map +1 -0
  157. package/dist/storage/diagnostic-store.d.ts +21 -0
  158. package/dist/storage/diagnostic-store.js +50 -0
  159. package/dist/storage/diagnostic-store.js.map +1 -0
  160. package/dist/storage/import-manifest.d.ts +6 -0
  161. package/dist/storage/import-manifest.js +41 -0
  162. package/dist/storage/import-manifest.js.map +1 -0
  163. package/dist/storage/pending-store.d.ts +329 -0
  164. package/dist/storage/pending-store.js +164 -0
  165. package/dist/storage/pending-store.js.map +1 -0
  166. package/dist/storage/resolution-candidate-service.js +2 -1
  167. package/dist/storage/resolution-candidate-service.js.map +1 -1
  168. package/dist/storage/resolution-change-transaction.d.ts +41 -3
  169. package/dist/storage/resolution-change-transaction.js +101 -12
  170. package/dist/storage/resolution-change-transaction.js.map +1 -1
  171. package/dist/storage/resolution-home.d.ts +2 -1
  172. package/dist/storage/resolution-home.js +5 -2
  173. package/dist/storage/resolution-home.js.map +1 -1
  174. package/dist/storage/resolution-index-check.js +3 -2
  175. package/dist/storage/resolution-index-check.js.map +1 -1
  176. package/dist/storage/resolution-indexer.js +3 -1
  177. package/dist/storage/resolution-indexer.js.map +1 -1
  178. package/dist/storage/usage-store.d.ts +80 -0
  179. package/dist/storage/usage-store.js +239 -0
  180. package/dist/storage/usage-store.js.map +1 -0
  181. package/package.json +11 -4
@@ -0,0 +1,159 @@
1
+ import { z } from 'zod';
2
+ import { type LegacyScanEntry } from './legacy-scan.js';
3
+ import type { ResolutionDocument } from './resolution-schema.js';
4
+ export declare const ReviewedLegacyDraftSchema: z.ZodObject<{
5
+ problem: z.ZodObject<{
6
+ schema_version: z.ZodLiteral<3>;
7
+ kind: z.ZodLiteral<"problem">;
8
+ id: z.ZodString;
9
+ title: z.ZodString;
10
+ observations: z.ZodObject<{
11
+ symptoms: z.ZodArray<z.ZodString>;
12
+ error_signatures: z.ZodDefault<z.ZodArray<z.ZodString>>;
13
+ symbols: z.ZodDefault<z.ZodArray<z.ZodString>>;
14
+ operations: z.ZodDefault<z.ZodArray<z.ZodString>>;
15
+ expected: z.ZodString;
16
+ observed: z.ZodString;
17
+ }, z.core.$strict>;
18
+ hypotheses: z.ZodObject<{
19
+ suspected_causes: z.ZodDefault<z.ZodArray<z.ZodString>>;
20
+ components: z.ZodDefault<z.ZodArray<z.ZodString>>;
21
+ }, z.core.$strict>;
22
+ environment: z.ZodObject<{
23
+ languages: z.ZodDefault<z.ZodArray<z.ZodString>>;
24
+ platforms: z.ZodDefault<z.ZodArray<z.ZodString>>;
25
+ runtimes: z.ZodDefault<z.ZodArray<z.ZodString>>;
26
+ tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
27
+ }, z.core.$strict>;
28
+ created_at: z.ZodISODateTime;
29
+ }, z.core.$strict>;
30
+ solution: z.ZodObject<{
31
+ schema_version: z.ZodLiteral<3>;
32
+ kind: z.ZodLiteral<"solution">;
33
+ id: z.ZodString;
34
+ problem_id: z.ZodString;
35
+ revision: z.ZodNumber;
36
+ title: z.ZodString;
37
+ cause: z.ZodString;
38
+ applicable_if: z.ZodArray<z.ZodString>;
39
+ reject_if: z.ZodDefault<z.ZodArray<z.ZodString>>;
40
+ inspections: z.ZodArray<z.ZodString>;
41
+ actions: z.ZodArray<z.ZodString>;
42
+ verification: z.ZodArray<z.ZodString>;
43
+ safety: z.ZodArray<z.ZodString>;
44
+ source_evidence: z.ZodDefault<z.ZodArray<z.ZodObject<{
45
+ id: z.ZodString;
46
+ project_key: z.ZodString;
47
+ repository_fingerprint: z.ZodString;
48
+ revision: z.ZodDiscriminatedUnion<[z.ZodObject<{
49
+ kind: z.ZodLiteral<"git_commit">;
50
+ value: z.ZodString;
51
+ }, z.core.$strict>, z.ZodObject<{
52
+ kind: z.ZodLiteral<"working_tree_hash">;
53
+ value: z.ZodString;
54
+ }, z.core.$strict>], "kind">;
55
+ relative_path: z.ZodString;
56
+ symbol: z.ZodOptional<z.ZodString>;
57
+ content_hash: z.ZodString;
58
+ tests: z.ZodDefault<z.ZodArray<z.ZodString>>;
59
+ }, z.core.$strict>>>;
60
+ prior_solution: z.ZodOptional<z.ZodObject<{
61
+ id: z.ZodString;
62
+ revision: z.ZodNumber;
63
+ relation: z.ZodEnum<{
64
+ refines: "refines";
65
+ supersedes: "supersedes";
66
+ }>;
67
+ }, z.core.$strict>>;
68
+ created_at: z.ZodISODateTime;
69
+ }, z.core.$strict>;
70
+ conflict_evidence: z.ZodOptional<z.ZodArray<z.ZodString>>;
71
+ }, z.core.$strict>;
72
+ export declare const LegacyCandidateSchema: z.ZodObject<{
73
+ candidate_id: z.ZodString;
74
+ content_hash: z.ZodString;
75
+ verification_status: z.ZodLiteral<"historical_only">;
76
+ historical: z.ZodObject<{
77
+ status: z.ZodString;
78
+ verified_at: z.ZodOptional<z.ZodString>;
79
+ cause_statement: z.ZodString;
80
+ }, z.core.$strip>;
81
+ draft: z.ZodObject<{
82
+ problem: z.ZodObject<{
83
+ schema_version: z.ZodOptional<z.ZodLiteral<3>>;
84
+ kind: z.ZodOptional<z.ZodLiteral<"problem">>;
85
+ id: z.ZodOptional<z.ZodString>;
86
+ title: z.ZodOptional<z.ZodString>;
87
+ hypotheses: z.ZodOptional<z.ZodObject<{
88
+ suspected_causes: z.ZodDefault<z.ZodArray<z.ZodString>>;
89
+ components: z.ZodDefault<z.ZodArray<z.ZodString>>;
90
+ }, z.core.$strict>>;
91
+ environment: z.ZodOptional<z.ZodObject<{
92
+ languages: z.ZodDefault<z.ZodArray<z.ZodString>>;
93
+ platforms: z.ZodDefault<z.ZodArray<z.ZodString>>;
94
+ runtimes: z.ZodDefault<z.ZodArray<z.ZodString>>;
95
+ tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
96
+ }, z.core.$strict>>;
97
+ created_at: z.ZodOptional<z.ZodISODateTime>;
98
+ observations: z.ZodObject<{
99
+ symptoms: z.ZodOptional<z.ZodArray<z.ZodString>>;
100
+ error_signatures: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodString>>>;
101
+ symbols: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodString>>>;
102
+ operations: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodString>>>;
103
+ expected: z.ZodOptional<z.ZodString>;
104
+ observed: z.ZodOptional<z.ZodString>;
105
+ }, z.core.$strict>;
106
+ }, z.core.$strict>;
107
+ solution: z.ZodObject<{
108
+ schema_version: z.ZodOptional<z.ZodLiteral<3>>;
109
+ kind: z.ZodOptional<z.ZodLiteral<"solution">>;
110
+ id: z.ZodOptional<z.ZodString>;
111
+ problem_id: z.ZodOptional<z.ZodString>;
112
+ revision: z.ZodOptional<z.ZodNumber>;
113
+ title: z.ZodOptional<z.ZodString>;
114
+ cause: z.ZodOptional<z.ZodString>;
115
+ applicable_if: z.ZodOptional<z.ZodArray<z.ZodString>>;
116
+ reject_if: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodString>>>;
117
+ inspections: z.ZodOptional<z.ZodArray<z.ZodString>>;
118
+ actions: z.ZodOptional<z.ZodArray<z.ZodString>>;
119
+ verification: z.ZodOptional<z.ZodArray<z.ZodString>>;
120
+ safety: z.ZodOptional<z.ZodArray<z.ZodString>>;
121
+ source_evidence: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodObject<{
122
+ id: z.ZodString;
123
+ project_key: z.ZodString;
124
+ repository_fingerprint: z.ZodString;
125
+ revision: z.ZodDiscriminatedUnion<[z.ZodObject<{
126
+ kind: z.ZodLiteral<"git_commit">;
127
+ value: z.ZodString;
128
+ }, z.core.$strict>, z.ZodObject<{
129
+ kind: z.ZodLiteral<"working_tree_hash">;
130
+ value: z.ZodString;
131
+ }, z.core.$strict>], "kind">;
132
+ relative_path: z.ZodString;
133
+ symbol: z.ZodOptional<z.ZodString>;
134
+ content_hash: z.ZodString;
135
+ tests: z.ZodDefault<z.ZodArray<z.ZodString>>;
136
+ }, z.core.$strict>>>>;
137
+ prior_solution: z.ZodOptional<z.ZodOptional<z.ZodObject<{
138
+ id: z.ZodString;
139
+ revision: z.ZodNumber;
140
+ relation: z.ZodEnum<{
141
+ refines: "refines";
142
+ supersedes: "supersedes";
143
+ }>;
144
+ }, z.core.$strict>>>;
145
+ created_at: z.ZodOptional<z.ZodISODateTime>;
146
+ }, z.core.$strict>;
147
+ }, z.core.$strict>;
148
+ missing_fields: z.ZodArray<z.ZodString>;
149
+ reuse_priority: z.ZodEnum<{
150
+ high: "high";
151
+ normal: "normal";
152
+ }>;
153
+ conflict_evidence: z.ZodArray<z.ZodString>;
154
+ relation: z.ZodString;
155
+ related_solution_handles: z.ZodArray<z.ZodString>;
156
+ }, z.core.$strict>;
157
+ export type LegacyCandidate = z.infer<typeof LegacyCandidateSchema>;
158
+ export declare function buildLegacyCandidate(entry: LegacyScanEntry, commit: string, canonical: ResolutionDocument[], reviewed?: z.infer<typeof ReviewedLegacyDraftSchema>): LegacyCandidate;
159
+ export declare function stableImportJson(value: unknown): string;
@@ -0,0 +1,73 @@
1
+ import { z } from 'zod';
2
+ import { ProblemSchema } from './problem-schema.js';
3
+ import { SolutionSchema } from './solution-schema.js';
4
+ import { fingerprintProblem, normalizeSearchText } from './problem-fingerprint.js';
5
+ import { classifyResolutionRelation } from './resolution-relation.js';
6
+ import { importHash } from './legacy-scan.js';
7
+ export const ReviewedLegacyDraftSchema = z.object({ problem: ProblemSchema, solution: SolutionSchema, conflict_evidence: z.array(z.string().min(1)).optional() }).strict();
8
+ const LegacyDraftSchema = z.object({ problem: ProblemSchema.partial().extend({ observations: ProblemSchema.shape.observations.partial() }), solution: SolutionSchema.partial() }).strict();
9
+ export const LegacyCandidateSchema = z.object({
10
+ candidate_id: z.string().regex(/^candidate-[a-f0-9]{64}$/), content_hash: z.string().regex(/^sha256:[a-f0-9]{64}$/),
11
+ verification_status: z.literal('historical_only'),
12
+ historical: z.object({ status: z.string(), verified_at: z.string().optional(), cause_statement: z.string() }),
13
+ draft: LegacyDraftSchema,
14
+ missing_fields: z.array(z.string()), reuse_priority: z.enum(['high', 'normal']),
15
+ conflict_evidence: z.array(z.string()),
16
+ relation: z.string(), related_solution_handles: z.array(z.string()),
17
+ }).strict();
18
+ export function buildLegacyCandidate(entry, commit, canonical, reviewed) {
19
+ const legacy = entry.legacy;
20
+ if (!legacy)
21
+ throw new Error('legacy_source_not_parsed');
22
+ const suffix = importHash(`${commit}:${entry.blob_sha}:${entry.entry_id}`).slice(7);
23
+ const problemId = `problem-import-${suffix}`;
24
+ const solutionId = `solution-import-${suffix}`;
25
+ const created = new Date(legacy.created_at);
26
+ const createdAt = Number.isFinite(created.valueOf()) ? created.toISOString() : undefined;
27
+ const rawDraft = reviewed ? {
28
+ problem: { ...reviewed.problem, id: problemId }, solution: { ...reviewed.solution, id: solutionId, problem_id: problemId, revision: 1 },
29
+ } : {
30
+ problem: { schema_version: 3, kind: 'problem', id: problemId, title: legacy.title, observations: { symptoms: [legacy.summary], error_signatures: legacy.error_signature ? [legacy.error_signature] : [], observed: legacy.mistake }, environment: { languages: legacy.language ? [legacy.language] : [], platforms: legacy.platforms, tools: legacy.tools, runtimes: [] }, ...(createdAt ? { created_at: createdAt } : {}) },
31
+ solution: { schema_version: 3, kind: 'solution', id: solutionId, problem_id: problemId, revision: 1, title: legacy.title, actions: [legacy.fix], reject_if: legacy.anti_conditions, source_evidence: [], ...(createdAt ? { created_at: createdAt } : {}) },
32
+ };
33
+ // Hash the same schema-normalized representation that the reader validates.
34
+ const draft = LegacyDraftSchema.parse(rawDraft);
35
+ const missing_fields = [];
36
+ const problem = ProblemSchema.safeParse(draft.problem);
37
+ const solution = SolutionSchema.safeParse(draft.solution);
38
+ if (!problem.success)
39
+ missing_fields.push(...problem.error.issues.map((issue) => `problem.${issue.path.join('.')}`));
40
+ if (!solution.success)
41
+ missing_fields.push(...solution.error.issues.map((issue) => `solution.${issue.path.join('.')}`));
42
+ const relations = problem.success && solution.success ? compareDraft(problem.data, solution.data, canonical) : { relation: 'needs_review', handles: [] };
43
+ const conflict_evidence = reviewed?.conflict_evidence ?? [];
44
+ const content_hash = importHash(stableImportJson({ draft, conflict_evidence }));
45
+ return { candidate_id: `candidate-${importHash(`${commit}:${entry.blob_sha}:${stableImportJson(normalizeSemantic({ draft, conflict_evidence }))}`).slice(7)}`, content_hash, verification_status: 'historical_only', historical: { status: legacy.status, ...(legacy.verified_at ? { verified_at: legacy.verified_at } : {}), cause_statement: legacy.mistake }, draft, missing_fields: [...new Set(missing_fields)].sort(), reuse_priority: legacy.rule_scope !== 'project' && legacy.confidence === 'high' ? 'high' : 'normal', conflict_evidence, relation: conflict_evidence.length ? 'contradicts_solution' : relations.relation, related_solution_handles: relations.handles };
46
+ }
47
+ function compareDraft(problem, solution, canonical) {
48
+ const same = canonical.filter((item) => item.kind === 'problem' && fingerprintProblem(item) === fingerprintProblem(problem));
49
+ const candidates = canonical.filter((item) => item.kind === 'solution' && same.some((p) => p.id === item.problem_id));
50
+ if (!candidates.length)
51
+ return { relation: same.length ? 'same_problem_alternative' : 'new_problem', handles: [] };
52
+ const compared = candidates.map((candidate) => ({ handle: `${candidate.id}@${candidate.revision}`, relation: classifyResolutionRelation({ sameProblem: true, sameCause: normalizeSearchText(candidate.cause) === normalizeSearchText(solution.cause), sameStrategy: sameTexts(candidate.actions, solution.actions), sameApplicability: sameTexts(candidate.applicable_if, solution.applicable_if) && sameTexts(candidate.reject_if, solution.reject_if), hasTarget: false, replacementVerified: false, conflictEvidence: [], analogousEvidence: [], weakTextOverlap: false }) }));
53
+ const exact = compared.filter(({ relation }) => relation === 'exact_solution');
54
+ return { relation: exact.length ? 'exact_solution' : 'same_problem_alternative', handles: (exact.length ? exact : compared).map(({ handle }) => handle) };
55
+ }
56
+ function sameTexts(a, b) { return JSON.stringify(a.map(normalizeSearchText).sort()) === JSON.stringify(b.map(normalizeSearchText).sort()); }
57
+ function normalizeSemantic(value) {
58
+ if (typeof value === 'string')
59
+ return normalizeSearchText(value);
60
+ if (Array.isArray(value))
61
+ return value.map(normalizeSemantic);
62
+ if (value && typeof value === 'object')
63
+ return Object.fromEntries(Object.entries(value).filter(([key]) => !['id', 'problem_id', 'created_at'].includes(key)).map(([key, item]) => [key, normalizeSemantic(item)]));
64
+ return value;
65
+ }
66
+ export function stableImportJson(value) {
67
+ if (Array.isArray(value))
68
+ return `[${value.map(stableImportJson).join(',')}]`;
69
+ if (value && typeof value === 'object')
70
+ return `{${Object.entries(value).filter(([, item]) => item !== undefined).sort(([a], [b]) => a.localeCompare(b)).map(([key, item]) => `${JSON.stringify(key)}:${stableImportJson(item)}`).join(',')}}`;
71
+ return JSON.stringify(value);
72
+ }
73
+ //# sourceMappingURL=legacy-candidate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"legacy-candidate.js","sourceRoot":"","sources":["../../src/core/legacy-candidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,aAAa,EAAgB,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAE,cAAc,EAAiB,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACnF,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AACtE,OAAO,EAAE,UAAU,EAAwB,MAAM,kBAAkB,CAAC;AAGpE,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,cAAc,EAAE,iBAAiB,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC;AAC3K,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,OAAO,EAAE,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,aAAa,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,QAAQ,EAAE,cAAc,CAAC,OAAO,EAAE,EAAE,CAAC,C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C,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IACnN,OAAO,KAAK,CAAC;AACf,CAAC;AACD,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,KAAK,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IAC9E,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IAC/O,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,215 @@
1
+ import { z } from 'zod';
2
+ import { type LegacyManifest } from './legacy-scan.js';
3
+ export declare const LegacyVerificationSchema: z.ZodObject<{
4
+ source_commit: z.ZodString;
5
+ items: z.ZodArray<z.ZodObject<{
6
+ entry_id: z.ZodString;
7
+ candidate_hash: z.ZodString;
8
+ verified_at: z.ZodISODateTime;
9
+ command: z.ZodString;
10
+ environment: z.ZodObject<{
11
+ languages: z.ZodDefault<z.ZodArray<z.ZodString>>;
12
+ platforms: z.ZodDefault<z.ZodArray<z.ZodString>>;
13
+ runtimes: z.ZodDefault<z.ZodArray<z.ZodString>>;
14
+ tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
15
+ }, z.core.$strict>;
16
+ observed: z.ZodString;
17
+ }, z.core.$strict>>;
18
+ }, z.core.$strict>;
19
+ declare const PlanSchema: z.ZodObject<{
20
+ status: z.ZodLiteral<"preview">;
21
+ source_commit: z.ZodString;
22
+ manifest_hash: z.ZodString;
23
+ evidence_hash: z.ZodString;
24
+ canonical_state_hash: z.ZodString;
25
+ selected_ids: z.ZodArray<z.ZodString>;
26
+ state_after: z.ZodArray<z.ZodString>;
27
+ items: z.ZodArray<z.ZodObject<{
28
+ entry_id: z.ZodString;
29
+ request: z.ZodObject<{
30
+ usage_context: z.ZodOptional<z.ZodObject<{
31
+ investigation_id: z.ZodOptional<z.ZodString>;
32
+ invocation_id: z.ZodOptional<z.ZodString>;
33
+ retry_of: z.ZodOptional<z.ZodUUID>;
34
+ project_key: z.ZodOptional<z.ZodString>;
35
+ supplied_agent: z.ZodOptional<z.ZodString>;
36
+ supplied_model: z.ZodOptional<z.ZodString>;
37
+ }, z.core.$strict>>;
38
+ payload: z.ZodObject<{
39
+ problem: z.ZodObject<{
40
+ schema_version: z.ZodLiteral<3>;
41
+ kind: z.ZodLiteral<"problem">;
42
+ id: z.ZodString;
43
+ title: z.ZodString;
44
+ observations: z.ZodObject<{
45
+ symptoms: z.ZodArray<z.ZodString>;
46
+ error_signatures: z.ZodDefault<z.ZodArray<z.ZodString>>;
47
+ symbols: z.ZodDefault<z.ZodArray<z.ZodString>>;
48
+ operations: z.ZodDefault<z.ZodArray<z.ZodString>>;
49
+ expected: z.ZodString;
50
+ observed: z.ZodString;
51
+ }, z.core.$strict>;
52
+ hypotheses: z.ZodObject<{
53
+ suspected_causes: z.ZodDefault<z.ZodArray<z.ZodString>>;
54
+ components: z.ZodDefault<z.ZodArray<z.ZodString>>;
55
+ }, z.core.$strict>;
56
+ environment: z.ZodObject<{
57
+ languages: z.ZodDefault<z.ZodArray<z.ZodString>>;
58
+ platforms: z.ZodDefault<z.ZodArray<z.ZodString>>;
59
+ runtimes: z.ZodDefault<z.ZodArray<z.ZodString>>;
60
+ tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
61
+ }, z.core.$strict>;
62
+ created_at: z.ZodISODateTime;
63
+ }, z.core.$strict>;
64
+ solution: z.ZodObject<{
65
+ schema_version: z.ZodLiteral<3>;
66
+ kind: z.ZodLiteral<"solution">;
67
+ id: z.ZodString;
68
+ problem_id: z.ZodString;
69
+ revision: z.ZodNumber;
70
+ title: z.ZodString;
71
+ cause: z.ZodString;
72
+ applicable_if: z.ZodArray<z.ZodString>;
73
+ reject_if: z.ZodDefault<z.ZodArray<z.ZodString>>;
74
+ inspections: z.ZodArray<z.ZodString>;
75
+ actions: z.ZodArray<z.ZodString>;
76
+ verification: z.ZodArray<z.ZodString>;
77
+ safety: z.ZodArray<z.ZodString>;
78
+ source_evidence: z.ZodDefault<z.ZodArray<z.ZodObject<{
79
+ id: z.ZodString;
80
+ project_key: z.ZodString;
81
+ repository_fingerprint: z.ZodString;
82
+ revision: z.ZodDiscriminatedUnion<[z.ZodObject<{
83
+ kind: z.ZodLiteral<"git_commit">;
84
+ value: z.ZodString;
85
+ }, z.core.$strict>, z.ZodObject<{
86
+ kind: z.ZodLiteral<"working_tree_hash">;
87
+ value: z.ZodString;
88
+ }, z.core.$strict>], "kind">;
89
+ relative_path: z.ZodString;
90
+ symbol: z.ZodOptional<z.ZodString>;
91
+ content_hash: z.ZodString;
92
+ tests: z.ZodDefault<z.ZodArray<z.ZodString>>;
93
+ }, z.core.$strict>>>;
94
+ prior_solution: z.ZodOptional<z.ZodObject<{
95
+ id: z.ZodString;
96
+ revision: z.ZodNumber;
97
+ relation: z.ZodEnum<{
98
+ refines: "refines";
99
+ supersedes: "supersedes";
100
+ }>;
101
+ }, z.core.$strict>>;
102
+ created_at: z.ZodISODateTime;
103
+ }, z.core.$strict>;
104
+ problem_fingerprint: z.ZodString;
105
+ project_key: z.ZodOptional<z.ZodString>;
106
+ target_solution_handle: z.ZodOptional<z.ZodString>;
107
+ verification: z.ZodObject<{
108
+ recurrence_risk: z.ZodOptional<z.ZodEnum<{
109
+ unknown: "unknown";
110
+ low: "low";
111
+ high: "high";
112
+ medium: "medium";
113
+ }>>;
114
+ value: z.ZodOptional<z.ZodEnum<{
115
+ unknown: "unknown";
116
+ low: "low";
117
+ high: "high";
118
+ medium: "medium";
119
+ }>>;
120
+ verified_at: z.ZodISODateTime;
121
+ evidence: z.ZodArray<z.ZodObject<{
122
+ kind: z.ZodEnum<{
123
+ focused_test: "focused_test";
124
+ integration_test: "integration_test";
125
+ manual_verification: "manual_verification";
126
+ observation: "observation";
127
+ }>;
128
+ handle: z.ZodString;
129
+ summary: z.ZodString;
130
+ }, z.core.$strict>>;
131
+ }, z.core.$strict>;
132
+ relation_evidence: z.ZodObject<{
133
+ replacement_verified: z.ZodBoolean;
134
+ conflict: z.ZodArray<z.ZodString>;
135
+ analogous: z.ZodArray<z.ZodString>;
136
+ weak_text_overlap: z.ZodOptional<z.ZodBoolean>;
137
+ }, z.core.$strict>;
138
+ }, z.core.$strict>;
139
+ payload_hash: z.ZodString;
140
+ state_hash: z.ZodString;
141
+ relation: z.ZodEnum<{
142
+ exact_solution: "exact_solution";
143
+ same_problem_alternative: "same_problem_alternative";
144
+ refines_solution: "refines_solution";
145
+ supersedes_solution: "supersedes_solution";
146
+ contradicts_solution: "contradicts_solution";
147
+ analogous_pattern: "analogous_pattern";
148
+ weak_overlap: "weak_overlap";
149
+ new_problem: "new_problem";
150
+ }>;
151
+ pending_verification: z.ZodOptional<z.ZodObject<{
152
+ environment: z.ZodObject<{
153
+ languages: z.ZodDefault<z.ZodArray<z.ZodString>>;
154
+ platforms: z.ZodDefault<z.ZodArray<z.ZodString>>;
155
+ runtimes: z.ZodDefault<z.ZodArray<z.ZodString>>;
156
+ tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
157
+ }, z.core.$strict>;
158
+ sources: z.ZodArray<z.ZodObject<{
159
+ role: z.ZodEnum<{
160
+ code: "code";
161
+ environment: "environment";
162
+ test: "test";
163
+ }>;
164
+ locator: z.ZodObject<{
165
+ id: z.ZodString;
166
+ project_key: z.ZodString;
167
+ repository_fingerprint: z.ZodString;
168
+ revision: z.ZodDiscriminatedUnion<[z.ZodObject<{
169
+ kind: z.ZodLiteral<"git_commit">;
170
+ value: z.ZodString;
171
+ }, z.core.$strict>, z.ZodObject<{
172
+ kind: z.ZodLiteral<"working_tree_hash">;
173
+ value: z.ZodString;
174
+ }, z.core.$strict>], "kind">;
175
+ relative_path: z.ZodString;
176
+ symbol: z.ZodOptional<z.ZodString>;
177
+ content_hash: z.ZodString;
178
+ tests: z.ZodDefault<z.ZodArray<z.ZodString>>;
179
+ }, z.core.$strict>;
180
+ }, z.core.$strict>>;
181
+ }, z.core.$strict>>;
182
+ }, z.core.$strict>;
183
+ solution_handle: z.ZodString;
184
+ }, z.core.$strict>>;
185
+ plan_hash: z.ZodString;
186
+ }, z.core.$strict>;
187
+ export type LegacyApplyPlan = z.infer<typeof PlanSchema>;
188
+ /** Semantic identity excludes mutable progress/receipts so a selected batch can resume. */
189
+ export declare function legacyManifestHash(manifest: LegacyManifest): string;
190
+ export declare function previewLegacyImport(path: string, home: string, input: unknown): Promise<LegacyApplyPlan>;
191
+ export declare function executeLegacyImport(path: string, home: string, input: unknown, preview: unknown, hooks?: {
192
+ afterCommit?: (entryId: string) => Promise<void>;
193
+ }): Promise<{
194
+ imported: number;
195
+ duplicate: number;
196
+ remaining: number;
197
+ canonical_counts: {
198
+ problems: number;
199
+ solutions: number;
200
+ outcomes: number;
201
+ };
202
+ index: {
203
+ status: "ok";
204
+ };
205
+ searchable: number;
206
+ receipts: ({
207
+ receipt_id: string;
208
+ solution_handle: string;
209
+ verified_at: string;
210
+ } | undefined)[];
211
+ sync_pending?: boolean | undefined;
212
+ status: string;
213
+ canonical_write: boolean;
214
+ }>;
215
+ export {};
@@ -0,0 +1,172 @@
1
+ import Database from 'better-sqlite3';
2
+ import { join } from 'node:path';
3
+ import { z } from 'zod';
4
+ import { readImportManifest, withImportManifestLock, writeImportManifest } from '../storage/import-manifest.js';
5
+ import { loadCanonicalResolutions } from '../storage/resolution-indexer.js';
6
+ import { readChangeReceipt, withResolutionTransaction } from '../storage/resolution-change-transaction.js';
7
+ import { checkResolutionIndex } from '../storage/resolution-index-check.js';
8
+ import { hashResolutionChangePayload, prepareResolutionChange } from '../mcp/resolution-change.js';
9
+ import { commitResolutionChangeLocked } from '../mcp/resolutions-commit-change-tool.js';
10
+ import { McpResolutionsCommitChangeInputSchema, McpResolutionsPrepareChangeInputSchema } from '../mcp/schemas.js';
11
+ import { computeResolutionSemanticChecksum } from './resolution-semantic-checksum.js';
12
+ import { fingerprintProblem } from './problem-fingerprint.js';
13
+ import { ProblemSchema, ResolutionEnvironmentSchema } from './problem-schema.js';
14
+ import { SolutionSchema } from './solution-schema.js';
15
+ import { hasUnsafeImportContent, importHash } from './legacy-scan.js';
16
+ import { stableImportJson } from './legacy-candidate.js';
17
+ const hash = z.string().regex(/^sha256:[a-f0-9]{64}$/);
18
+ export const LegacyVerificationSchema = z.object({
19
+ source_commit: z.string().regex(/^[a-f0-9]{40,64}$/),
20
+ items: z.array(z.object({ entry_id: z.string().regex(/^[a-f0-9]{64}$/), candidate_hash: hash, verified_at: z.iso.datetime(), command: z.string().trim().min(1).max(500), environment: ResolutionEnvironmentSchema, observed: z.string().trim().min(1).max(1500) }).strict()).min(1).max(500),
21
+ }).strict();
22
+ const PlanSchema = z.object({
23
+ status: z.literal('preview'), source_commit: z.string(), manifest_hash: hash, evidence_hash: hash, canonical_state_hash: hash,
24
+ selected_ids: z.array(z.string()), state_after: z.array(hash),
25
+ items: z.array(z.object({ entry_id: z.string(), request: McpResolutionsCommitChangeInputSchema, solution_handle: z.string() }).strict()),
26
+ plan_hash: hash,
27
+ }).strict();
28
+ /** Semantic identity excludes mutable progress/receipts so a selected batch can resume. */
29
+ export function legacyManifestHash(manifest) {
30
+ return importHash(stableImportJson({ source_commit: manifest.source_commit, source_checksum: manifest.source_checksum, entries: manifest.entries.map((entry) => ({ entry_id: entry.entry_id, blob_sha: entry.blob_sha, content_hash: entry.content_hash, candidate: entry.candidate ? { candidate_id: entry.candidate.candidate_id, content_hash: entry.candidate.content_hash } : null })) }));
31
+ }
32
+ export async function previewLegacyImport(path, home, input) {
33
+ const manifest = await readImportManifest(path);
34
+ const evidence = validateEvidence(manifest, input);
35
+ let virtual = (await loadCanonicalResolutions(home)).map(({ document }) => document);
36
+ const canonical_state_hash = computeResolutionSemanticChecksum(virtual);
37
+ const items = [];
38
+ const state_after = [];
39
+ for (const verified of evidence.items) {
40
+ const payload = payloadFor(manifest, verified);
41
+ const prepared = await prepareResolutionChange(payload, home, virtual);
42
+ if (!prepared.eligible)
43
+ throw new Error(prepared.code ?? 'import_preflight_blocked');
44
+ virtual = mergeDocuments(virtual, prepared.documents);
45
+ items.push({ entry_id: verified.entry_id, solution_handle: prepared.solution_handle, request: { payload: prepared.normalized_payload, payload_hash: prepared.payload_hash, state_hash: prepared.state_hash, relation: prepared.relation } });
46
+ state_after.push(computeResolutionSemanticChecksum(virtual));
47
+ }
48
+ const plan = { status: 'preview', source_commit: manifest.source_commit, manifest_hash: legacyManifestHash(manifest), evidence_hash: importHash(stableImportJson(evidence)), canonical_state_hash, selected_ids: evidence.items.map(({ entry_id }) => entry_id), state_after, items };
49
+ return { ...plan, plan_hash: importHash(stableImportJson(plan)) };
50
+ }
51
+ export async function executeLegacyImport(path, home, input, preview, hooks = {}) {
52
+ const plan = PlanSchema.parse(preview);
53
+ const { plan_hash, ...body } = plan;
54
+ if (plan_hash !== importHash(stableImportJson(body)))
55
+ throw new Error('import_preview_changed');
56
+ return withImportManifestLock(path, home, () => withResolutionTransaction(home, async () => {
57
+ const manifest = await readImportManifest(path);
58
+ const evidence = validateEvidence(manifest, input);
59
+ if (manifest.source_commit !== plan.source_commit || legacyManifestHash(manifest) !== plan.manifest_hash || importHash(stableImportJson(evidence)) !== plan.evidence_hash || stableImportJson(evidence.items.map(({ entry_id }) => entry_id)) !== stableImportJson(plan.selected_ids))
60
+ throw new Error('import_source_or_evidence_changed');
61
+ if (plan.items.length !== evidence.items.length || plan.state_after.length !== plan.items.length)
62
+ throw new Error('import_preview_incomplete');
63
+ for (let i = 0; i < plan.items.length; i++) {
64
+ const item = plan.items[i];
65
+ const original = payloadFor(manifest, evidence.items[i]);
66
+ const rebound = { ...original, problem: item.request.payload.problem, solution: { ...original.solution, id: item.request.payload.solution.id, problem_id: item.request.payload.solution.problem_id, revision: item.request.payload.solution.revision } };
67
+ if (item.entry_id !== evidence.items[i].entry_id || fingerprintProblem(item.request.payload.problem) !== original.problem_fingerprint || hashResolutionChangePayload(rebound) !== item.request.payload_hash || hashResolutionChangePayload(item.request.payload) !== item.request.payload_hash || item.solution_handle !== `${item.request.payload.solution.id}@${item.request.payload.solution.revision}`)
68
+ throw new Error('import_preview_payload_changed');
69
+ }
70
+ let virtual = (await loadCanonicalResolutions(home)).map(({ document }) => document);
71
+ let prefix = 0;
72
+ for (const item of plan.items) {
73
+ if (!await readChangeReceipt(home, item.request.payload_hash))
74
+ break;
75
+ prefix++;
76
+ }
77
+ if (computeResolutionSemanticChecksum(virtual) !== (prefix ? plan.state_after[prefix - 1] : plan.canonical_state_hash))
78
+ throw new Error('canonical_state_changed');
79
+ // Recheck every remaining member before the first new canonical write, including intra-batch collisions.
80
+ for (let i = prefix; i < plan.items.length; i++) {
81
+ const item = plan.items[i];
82
+ if (item.entry_id !== evidence.items[i].entry_id)
83
+ throw new Error('import_selection_changed');
84
+ const prepared = await prepareResolutionChange(payloadFor(manifest, evidence.items[i]), home, virtual);
85
+ if (!prepared.eligible || prepared.payload_hash !== item.request.payload_hash || prepared.relation !== item.request.relation || prepared.state_hash !== item.request.state_hash || stableImportJson(prepared.normalized_payload) !== stableImportJson(item.request.payload))
86
+ throw new Error(prepared.code ?? 'import_proposal_changed');
87
+ virtual = mergeDocuments(virtual, prepared.documents);
88
+ if (computeResolutionSemanticChecksum(virtual) !== plan.state_after[i])
89
+ throw new Error('import_preview_changed');
90
+ }
91
+ let newWrites = 0;
92
+ for (let i = 0; i < plan.items.length; i++) {
93
+ const item = plan.items[i];
94
+ const entry = manifest.entries.find(({ entry_id }) => entry_id === item.entry_id);
95
+ if (i >= prefix) {
96
+ const response = await commitResolutionChangeLocked(item.request, { rulebookHome: home });
97
+ const block = response.content[0];
98
+ if (block.type !== 'text')
99
+ throw new Error('import_invalid_receipt');
100
+ const result = JSON.parse(block.text);
101
+ if (!['committed', 'already_committed'].includes(result.status))
102
+ throw new Error(result.code ?? 'import_commit_blocked');
103
+ if (result.canonical_write)
104
+ newWrites++;
105
+ await hooks.afterCommit?.(item.entry_id);
106
+ }
107
+ const receipt = await readChangeReceipt(home, item.request.payload_hash);
108
+ if (!receipt)
109
+ throw new Error('import_receipt_unavailable');
110
+ entry.status = item.request.relation === 'exact_solution' ? 'duplicate' : 'imported';
111
+ entry.reason = 'current_verification_committed';
112
+ entry.import_receipt = { receipt_id: receipt.receipt_id, solution_handle: item.solution_handle, verified_at: evidence.items[i].verified_at };
113
+ await writeImportManifest(path, home, manifest);
114
+ }
115
+ const index = await checkResolutionIndex(home);
116
+ if (index.status !== 'ok')
117
+ throw new Error('import_index_not_ready');
118
+ const database = new Database(join(home, 'index/resolutions.sqlite'), { readonly: true, fileMustExist: true });
119
+ try {
120
+ for (const item of plan.items) {
121
+ const [id, revision] = item.solution_handle.split('@');
122
+ if (!database.prepare('SELECT id FROM solutions WHERE id = ? AND revision = ?').get(id, Number(revision)))
123
+ throw new Error('import_not_searchable');
124
+ }
125
+ }
126
+ finally {
127
+ database.close();
128
+ }
129
+ const actual = (await loadCanonicalResolutions(home)).map(({ document }) => document);
130
+ if (computeResolutionSemanticChecksum(actual) !== plan.state_after.at(-1))
131
+ throw new Error('import_reconciliation_failed');
132
+ return { status: 'applied', canonical_write: newWrites > 0, ...(newWrites ? { sync_pending: true } : {}), imported: plan.items.filter(({ request }) => request.relation !== 'exact_solution').length, duplicate: plan.items.filter(({ request }) => request.relation === 'exact_solution').length, remaining: manifest.entries.filter((entry) => !entry.import_receipt && entry.status !== 'discarded').length, canonical_counts: counts(actual), index: { status: index.status }, searchable: plan.items.length, receipts: plan.items.map((item) => manifest.entries.find(({ entry_id }) => entry_id === item.entry_id).import_receipt) };
133
+ }));
134
+ }
135
+ function validateEvidence(manifest, input) {
136
+ const evidence = LegacyVerificationSchema.parse(input);
137
+ if (hasUnsafeImportContent(evidence))
138
+ throw new Error('unsafe_import_evidence');
139
+ if (evidence.source_commit !== manifest.source_commit || new Set(evidence.items.map(({ entry_id }) => entry_id)).size !== evidence.items.length)
140
+ throw new Error('import_selection_mismatch');
141
+ for (const item of evidence.items) {
142
+ const entry = manifest.entries.find(({ entry_id }) => entry_id === item.entry_id);
143
+ if (!entry?.candidate || entry.disposition !== 'parsed' || entry.status === 'discarded' || entry.candidate.content_hash !== item.candidate_hash)
144
+ throw new Error('candidate_hash_mismatch');
145
+ if (entry.candidate.missing_fields.length || entry.candidate.conflict_evidence.length)
146
+ throw new Error('candidate_needs_review');
147
+ if (Date.parse(item.verified_at) > Date.now() + 300_000)
148
+ throw new Error('verification_time_in_future');
149
+ if (stableImportJson(item.environment) !== stableImportJson(entry.candidate.draft.problem.environment))
150
+ throw new Error('verification_environment_mismatch');
151
+ }
152
+ return evidence;
153
+ }
154
+ function payloadFor(manifest, item) {
155
+ const candidate = manifest.entries.find(({ entry_id }) => entry_id === item.entry_id).candidate;
156
+ const problem = ProblemSchema.parse(candidate.draft.problem);
157
+ const solution = SolutionSchema.parse(candidate.draft.solution);
158
+ return McpResolutionsPrepareChangeInputSchema.parse({ problem, solution, problem_fingerprint: fingerprintProblem(problem), verification: { verified_at: item.verified_at, evidence: [{ kind: 'integration_test', handle: `import/${item.entry_id}`, summary: `${item.command}: ${item.observed}` }] }, relation_evidence: { replacement_verified: false, conflict: candidate.conflict_evidence, analogous: [] } });
159
+ }
160
+ function mergeDocuments(existing, added) {
161
+ const merged = [...existing];
162
+ for (const document of added) {
163
+ const previous = merged.find((item) => item.kind === document.kind && item.id === document.id && (item.kind !== 'solution' || document.kind !== 'solution' || item.revision === document.revision));
164
+ if (previous && stableImportJson(previous) !== stableImportJson(document))
165
+ throw new Error('import_path_collision');
166
+ if (!previous)
167
+ merged.push(document);
168
+ }
169
+ return merged;
170
+ }
171
+ function counts(documents) { return { problems: documents.filter(({ kind }) => kind === 'problem').length, solutions: documents.filter(({ kind }) => kind === 'solution').length, outcomes: documents.filter(({ kind }) => kind === 'outcome').length }; }
172
+ //# sourceMappingURL=legacy-import-apply.js.map