@arcticdata/dsh-datarc-assistant 26.39.0 → 26.39.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -10
- package/api-operations.js +404 -0
- package/client.js +452 -87
- package/cordis.patch.yml +4 -4
- package/entry/SKILL.md +34 -0
- package/icon.png +0 -0
- package/index.js +35 -47
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/native-api.mjs +326 -0
- package/native-contracts.js +195 -0
- package/native-execution.js +184 -0
- package/native-runner.mjs +107 -0
- package/native-state.js +67 -0
- package/native-tools.js +241 -0
- package/operations.js +1260 -0
- package/package.json +65 -51
- package/{skills → runtime}/datarc-access-permissions/SKILL.md +30 -30
- package/{skills → runtime}/datarc-access-permissions/references/api.md +31 -31
- package/{skills → runtime}/datarc-access-permissions/references/write-order.md +16 -16
- package/{skills → runtime}/datarc-analysis-report/SKILL.md +125 -125
- package/{skills → runtime}/datarc-analysis-report/agents/openai.yaml +6 -6
- package/{skills → runtime}/datarc-analysis-report/references/analyst-frameworks.md +215 -215
- package/{skills → runtime}/datarc-analysis-report/references/api-contracts.md +478 -478
- package/{skills → runtime}/datarc-analysis-report/references/direct-card-fallback-design.md +66 -66
- package/{skills → runtime}/datarc-analysis-report/references/human-in-the-loop.md +142 -142
- package/{skills → runtime}/datarc-analysis-report/references/loop-personalization-policy.md +138 -138
- package/{skills → runtime}/datarc-analysis-report/references/manifest-example.json +779 -779
- package/{skills → runtime}/datarc-analysis-report/references/narrative-schema.md +160 -160
- package/{skills → runtime}/datarc-analysis-report/references/question-syntax.md +151 -151
- package/{skills → runtime}/datarc-analysis-report/scripts/run_analysis_report.py +2225 -2225
- package/{skills → runtime}/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -1914
- package/{skills → runtime}/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -5295
- package/{skills → runtime}/datarc-data-search/SKILL.md +243 -243
- package/{skills → runtime}/datarc-data-search/scripts/calculate.py +116 -116
- package/{skills → runtime}/datarc-data-search/scripts/data-search.mjs +628 -628
- package/{skills/datarc-dataseek-search → runtime/datarc-data-search}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-data-search/scripts/search-result.mjs +211 -211
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/SKILL.md +59 -59
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -36
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -30
- package/{skills/datarc-data-search → runtime/datarc-dataseek-batch-evaluation}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -1020
- package/{skills → runtime}/datarc-dataseek-search/SKILL.md +96 -96
- package/{skills/datarc-dataseek-batch-evaluation → runtime/datarc-dataseek-search}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -257
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/SKILL.md +44 -44
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/evals/evals.json +165 -165
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/dataset.md +77 -77
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -93
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/interventions.md +64 -64
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -47
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/rollback.md +11 -11
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -95
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -29
- package/{skills → runtime}/datarc-dataseek-testset-generation/SKILL.md +47 -47
- package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -526
- package/{skills → runtime}/datarc-datasource/SKILL.md +97 -97
- package/{skills → runtime}/datarc-oauth/SKILL.md +50 -50
- package/{skills → runtime}/datarc-oauth/scripts/auth.mjs +192 -192
- package/{skills → runtime}/datarc-oauth/scripts/credentials.mjs +237 -237
- package/{skills → runtime}/datarc-report-add-card/SKILL.md +90 -90
- package/{skills → runtime}/datarc-report-add-card/agents/openai.yaml +4 -4
- package/runtime/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/{skills → runtime}/datarc-report-add-card/scripts/report-card.mjs +439 -439
- package/{skills → runtime}/datarc-semantic-alias/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-alias/scripts/alias-plan.mjs +422 -422
- package/{skills → runtime}/datarc-semantic-alias/scripts/alias.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-calibration/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -683
- package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-core/SKILL.md +12 -12
- package/{skills → runtime}/datarc-semantic-core/scripts/artifact-journal.mjs +853 -830
- package/{skills → runtime}/datarc-semantic-core/scripts/canonical.mjs +48 -48
- package/{skills → runtime}/datarc-semantic-core/scripts/case-model.mjs +608 -608
- package/{skills → runtime}/datarc-semantic-core/scripts/cli.mjs +180 -180
- package/{skills → runtime}/datarc-semantic-core/scripts/datarc-client.mjs +180 -180
- package/{skills → runtime}/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -57
- package/{skills → runtime}/datarc-semantic-core/scripts/interventions.mjs +693 -693
- package/{skills → runtime}/datarc-semantic-core/scripts/live-resolution.mjs +305 -305
- package/{skills → runtime}/datarc-semantic-core/scripts/model-scope.mjs +49 -49
- package/{skills → runtime}/datarc-semantic-core/scripts/reproduction.mjs +330 -330
- package/{skills → runtime}/datarc-semantic-core/scripts/semantic-cli.mjs +799 -740
- package/{skills → runtime}/datarc-semantic-core/scripts/shared.mjs +312 -312
- package/{skills → runtime}/datarc-semantic-core/scripts/verification.mjs +1111 -1014
- package/{skills → runtime}/datarc-semantic-dataset/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -749
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -813
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-knowledge/SKILL.md +38 -38
- package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -61
- package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-generation.json +16 -16
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -347
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -848
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -31
- package/{skills → runtime}/datarc-semantic-prompt/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt-plan.mjs +598 -447
- package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt.mjs +15 -15
- package/{skills → runtime}/datarc-semantic-synonym/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -688
- package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym.mjs +11 -11
- package/{skills → runtime}/datarc-viz-analysis-report/SKILL.md +108 -108
- package/{skills → runtime}/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -8
- package/{skills → runtime}/datarc-viz-analysis-report/assets/echarts.min.js +45 -45
- package/{skills → runtime}/datarc-viz-analysis-report/references/api-contracts.md +163 -163
- package/{skills → runtime}/datarc-viz-analysis-report/references/derivation-log.md +157 -157
- package/{skills → runtime}/datarc-viz-analysis-report/references/evidence-framework.md +76 -76
- package/{skills → runtime}/datarc-viz-analysis-report/references/html-template.md +92 -92
- package/{skills → runtime}/datarc-viz-analysis-report/references/provenance-format.md +56 -56
- package/{skills → runtime}/datarc-viz-analysis-report/references/question-syntax.md +151 -151
- package/{skills → runtime}/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -1708
- package/{skills → runtime}/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -964
- package/{skills → runtime}/datarc-workspace-configuration/SKILL.md +59 -59
- package/{skills → runtime}/datarc-workspace-configuration/references/artifacts.md +75 -75
- package/{skills → runtime}/datarc-workspace-configuration/references/verification.md +20 -20
- package/{skills → runtime}/datarc-workspace-configuration/scripts/configuration.mjs +722 -662
- package/{skills → runtime}/datarc-workspace-context/SKILL.md +43 -43
- package/{skills → runtime}/datarc-workspace-context/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-context/scripts/datarc-client.mjs +31 -31
- package/{skills → runtime}/datarc-workspace-context/scripts/query-display.mjs +114 -114
- package/{skills → runtime}/datarc-workspace-context/scripts/workspace-context.mjs +58 -58
- package/{skills → runtime}/datarc-workspace-custom-tables/SKILL.md +89 -89
- package/{skills → runtime}/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -165
- package/{skills → runtime}/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -601
- package/{skills → runtime}/datarc-workspace-keywords/SKILL.md +197 -197
- package/{skills → runtime}/datarc-workspace-keywords/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-keywords/references/api-fields.md +79 -79
- package/{skills → runtime}/datarc-workspace-keywords/references/derived-measures.md +177 -177
- package/{skills → runtime}/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -145
- package/{skills → runtime}/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -2421
- package/{skills → runtime}/datarc-workspace-membership/SKILL.md +29 -29
- package/{skills → runtime}/datarc-workspace-membership/references/api.md +19 -19
- package/{skills → runtime}/datarc-workspace-modeling/SKILL.md +209 -209
- package/{skills → runtime}/datarc-workspace-modeling/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-modeling/references/join-preview.md +38 -38
- package/{skills → runtime}/datarc-workspace-modeling/references/model-plan.md +300 -300
- package/{skills → runtime}/datarc-workspace-modeling/references/model-tags.md +95 -95
- package/{skills → runtime}/datarc-workspace-modeling/references/modeling-contract.json +29 -29
- package/{skills → runtime}/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -2423
- package/{skills → runtime}/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -137
- package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -238
- package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -1261
- package/skills-provider.js +51 -0
- package/workflow-index.json +434 -0
- package/icon.svg +0 -4
- package/skills/datarc-report-add-card/scripts/datarc-client.mjs +0 -464
- package/skills-index.json +0 -194
|
@@ -1,830 +1,853 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { chmod, open, readFile, writeFile } from "node:fs/promises";
|
|
3
|
-
import { dirname, resolve } from "node:path";
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
canonicalArtifactValue,
|
|
7
|
-
hashArtifactValue,
|
|
8
|
-
sameCanonical,
|
|
9
|
-
withoutId,
|
|
10
|
-
} from "./canonical.mjs";
|
|
11
|
-
import { DatarcError } from "./datarc-client.mjs";
|
|
12
|
-
|
|
13
|
-
const ARTIFACT_JOURNAL = "datarc-semantic-debugging-artifact-v1";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
command,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
await
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
await
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
"
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
reserved.
|
|
129
|
-
reserved.
|
|
130
|
-
reserved.
|
|
131
|
-
reserved.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
reserved.
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
"
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
.
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
value.payload
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
let
|
|
214
|
-
let
|
|
215
|
-
let
|
|
216
|
-
let
|
|
217
|
-
let
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
value
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
record
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
record.journalId
|
|
268
|
-
record.
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if (
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
!
|
|
300
|
-
!
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
!
|
|
313
|
-
!
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
previousState
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
return
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
payload
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
!payload
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
(typeof value === "
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
previous.
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
validEnvelopeHash(
|
|
424
|
-
|
|
425
|
-
plan.
|
|
426
|
-
|
|
427
|
-
receipt.
|
|
428
|
-
receipt.completedSteps.
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
"
|
|
441
|
-
"
|
|
442
|
-
"
|
|
443
|
-
"
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
(bundle.
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
!
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
validEnvelopeHash(
|
|
462
|
-
|
|
463
|
-
plan.
|
|
464
|
-
|
|
465
|
-
(
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
receipt.completedSteps.
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
Array.isArray(baseline
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
value
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
if (payload.status === "
|
|
511
|
-
if (payload.status ===
|
|
512
|
-
return
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
const
|
|
518
|
-
const
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
if (
|
|
523
|
-
return
|
|
524
|
-
|
|
525
|
-
(plan === undefined || plan.
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
plan
|
|
535
|
-
|
|
536
|
-
plan.planHash &&
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
receipt
|
|
547
|
-
|
|
548
|
-
receipt.planHash &&
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
(receipt
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
if (
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
pendingIndex,
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
!
|
|
584
|
-
|
|
585
|
-
actual.
|
|
586
|
-
|
|
587
|
-
!
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
actual.
|
|
595
|
-
actual.
|
|
596
|
-
|
|
597
|
-
sameCanonical(
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
actual.
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
actual.
|
|
609
|
-
actual.
|
|
610
|
-
actual.
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
if (
|
|
614
|
-
|
|
615
|
-
!
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
"
|
|
637
|
-
"
|
|
638
|
-
"
|
|
639
|
-
"
|
|
640
|
-
"
|
|
641
|
-
"
|
|
642
|
-
"
|
|
643
|
-
"
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
if (
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
planned.
|
|
661
|
-
planned.
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
return
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
planned.
|
|
690
|
-
|
|
691
|
-
actual.
|
|
692
|
-
actual.
|
|
693
|
-
actual.
|
|
694
|
-
|
|
695
|
-
sameCanonical(actual.
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
seen.
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
if (
|
|
743
|
-
|
|
744
|
-
!
|
|
745
|
-
|
|
746
|
-
typeof
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
if (
|
|
765
|
-
if (
|
|
766
|
-
if (
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
if (
|
|
776
|
-
if (growth === 0
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
pending?.
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
await
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
!
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { chmod, open, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
canonicalArtifactValue,
|
|
7
|
+
hashArtifactValue,
|
|
8
|
+
sameCanonical,
|
|
9
|
+
withoutId,
|
|
10
|
+
} from "./canonical.mjs";
|
|
11
|
+
import { DatarcError } from "./datarc-client.mjs";
|
|
12
|
+
|
|
13
|
+
const ARTIFACT_JOURNAL = "datarc-semantic-debugging-artifact-v1";
|
|
14
|
+
let directorySyncWarningEmitted = false;
|
|
15
|
+
|
|
16
|
+
export function createCommandArtifactIdentity({
|
|
17
|
+
command,
|
|
18
|
+
workspaceId,
|
|
19
|
+
request,
|
|
20
|
+
input = request,
|
|
21
|
+
}) {
|
|
22
|
+
const canonicalRequest = canonicalArtifactValue(request);
|
|
23
|
+
return {
|
|
24
|
+
journalType: "command",
|
|
25
|
+
command,
|
|
26
|
+
...(workspaceId === undefined ? {} : { workspaceId }),
|
|
27
|
+
inputHash: hashArtifactValue(input),
|
|
28
|
+
request: canonicalRequest,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function completeCommandArtifact(identity, result) {
|
|
33
|
+
return {
|
|
34
|
+
...identity,
|
|
35
|
+
status: "completed",
|
|
36
|
+
result,
|
|
37
|
+
resultHash: hashArtifactValue(result),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function writeArtifact(path, value) {
|
|
42
|
+
if (typeof path !== "string" || !path)
|
|
43
|
+
throw new DatarcError("--output 需要文件路径", "INVALID_ARGUMENT");
|
|
44
|
+
const artifact = resolve(path);
|
|
45
|
+
try {
|
|
46
|
+
await writeFile(artifact, `${JSON.stringify(value, null, 2)}\n`, {
|
|
47
|
+
encoding: "utf8",
|
|
48
|
+
flag: "wx",
|
|
49
|
+
mode: 0o600,
|
|
50
|
+
});
|
|
51
|
+
await chmod(artifact, 0o600);
|
|
52
|
+
} catch {
|
|
53
|
+
throw new DatarcError("无法安全写入 artifact", "ARTIFACT_WRITE_FAILED", {
|
|
54
|
+
artifact,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return artifact;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function reserveArtifact(
|
|
61
|
+
path,
|
|
62
|
+
value,
|
|
63
|
+
{ openArtifact = open, openDirectory = open, journalId = randomUUID() } = {},
|
|
64
|
+
) {
|
|
65
|
+
if (typeof path !== "string" || !path)
|
|
66
|
+
throw new DatarcError("--output 需要文件路径", "INVALID_ARGUMENT");
|
|
67
|
+
const artifact = resolve(path);
|
|
68
|
+
let handle;
|
|
69
|
+
try {
|
|
70
|
+
handle = await openArtifact(artifact, "wx", 0o600);
|
|
71
|
+
await handle.chmod(0o600);
|
|
72
|
+
const reserved = {
|
|
73
|
+
artifact,
|
|
74
|
+
handle,
|
|
75
|
+
offset: 0,
|
|
76
|
+
sequence: 0,
|
|
77
|
+
journalId,
|
|
78
|
+
rootPlan: value.plan,
|
|
79
|
+
previousChecksum: null,
|
|
80
|
+
lastDurableValue: undefined,
|
|
81
|
+
syncAmbiguous: false,
|
|
82
|
+
failed: false,
|
|
83
|
+
};
|
|
84
|
+
await appendReservedArtifact(reserved, value);
|
|
85
|
+
await syncArtifactDirectory(artifact, openDirectory);
|
|
86
|
+
return reserved;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
await handle?.close();
|
|
89
|
+
if (error?.details?.reloadRequired === true) throw error;
|
|
90
|
+
throw new DatarcError("无法安全保留 artifact", "ARTIFACT_WRITE_FAILED", {
|
|
91
|
+
artifact,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function appendReservedArtifact(reserved, value) {
|
|
97
|
+
if (reserved.failed)
|
|
98
|
+
throw new DatarcError(
|
|
99
|
+
"artifact journal is unavailable",
|
|
100
|
+
"ARTIFACT_WRITE_FAILED",
|
|
101
|
+
{
|
|
102
|
+
artifact: reserved.artifact,
|
|
103
|
+
},
|
|
104
|
+
);
|
|
105
|
+
if (
|
|
106
|
+
!validateArtifactPayloadTransition(
|
|
107
|
+
reserved.lastDurableValue,
|
|
108
|
+
value,
|
|
109
|
+
reserved.rootPlan,
|
|
110
|
+
)
|
|
111
|
+
) {
|
|
112
|
+
reserved.failed = true;
|
|
113
|
+
throw new DatarcError(
|
|
114
|
+
"artifact journal transition is invalid",
|
|
115
|
+
"ARTIFACT_WRITE_FAILED",
|
|
116
|
+
{ artifact: reserved.artifact },
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const content = encodeArtifactRecord(reserved.sequence, value, {
|
|
120
|
+
journalId: reserved.journalId,
|
|
121
|
+
previousChecksum: reserved.previousChecksum,
|
|
122
|
+
});
|
|
123
|
+
const checksum = JSON.parse(content.toString("utf8")).checksum;
|
|
124
|
+
let writeComplete = false;
|
|
125
|
+
try {
|
|
126
|
+
await writeAll(reserved.handle, content, reserved.offset);
|
|
127
|
+
writeComplete = true;
|
|
128
|
+
await reserved.handle.sync();
|
|
129
|
+
reserved.offset += content.length;
|
|
130
|
+
reserved.sequence += 1;
|
|
131
|
+
reserved.previousChecksum = checksum;
|
|
132
|
+
reserved.lastDurableValue = structuredClone(value);
|
|
133
|
+
} catch {
|
|
134
|
+
reserved.failed = true;
|
|
135
|
+
reserved.syncAmbiguous = writeComplete;
|
|
136
|
+
throw new DatarcError("无法安全写入 artifact", "ARTIFACT_WRITE_FAILED", {
|
|
137
|
+
artifact: reserved.artifact,
|
|
138
|
+
...(writeComplete ? { reloadRequired: true } : {}),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function readJson(path) {
|
|
144
|
+
const text = path === "-" ? await readStdin() : await readFile(path, "utf8");
|
|
145
|
+
try {
|
|
146
|
+
return parseJsonOrArtifactJournal(text);
|
|
147
|
+
} catch {
|
|
148
|
+
throw new DatarcError("输入不是有效 JSON", "INVALID_JSON");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function encodeArtifactRecord(
|
|
153
|
+
sequence,
|
|
154
|
+
payload,
|
|
155
|
+
{ journalId, previousChecksum = null } = {},
|
|
156
|
+
) {
|
|
157
|
+
if (typeof journalId !== "string" || !journalId)
|
|
158
|
+
throw new DatarcError(
|
|
159
|
+
"artifact journal id is required",
|
|
160
|
+
"INVALID_ARTIFACT",
|
|
161
|
+
);
|
|
162
|
+
const body = {
|
|
163
|
+
journal: ARTIFACT_JOURNAL,
|
|
164
|
+
journalId,
|
|
165
|
+
sequence,
|
|
166
|
+
previousChecksum,
|
|
167
|
+
payload,
|
|
168
|
+
};
|
|
169
|
+
const checksum = createHash("sha256")
|
|
170
|
+
.update(JSON.stringify(body))
|
|
171
|
+
.digest("hex");
|
|
172
|
+
return Buffer.from(`${JSON.stringify({ ...body, checksum })}\n`, "utf8");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function validateArtifactPayloadTransition(previous, next, rootPlan) {
|
|
176
|
+
return validArtifactPayloadTransition(previous, next, rootPlan);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function validateFinalBundle(bundle) {
|
|
180
|
+
return validFinalBundle(bundle);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function validateRollbackBundle(bundle) {
|
|
184
|
+
return validRollbackBundle(bundle);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function parseJsonOrArtifactJournal(text) {
|
|
188
|
+
try {
|
|
189
|
+
const value = JSON.parse(text);
|
|
190
|
+
if (!isArtifactRecord(value)) return value;
|
|
191
|
+
if (
|
|
192
|
+
!validArtifactRecord(value, {
|
|
193
|
+
sequence: 0,
|
|
194
|
+
journalId: value.journalId,
|
|
195
|
+
previousChecksum: null,
|
|
196
|
+
}) ||
|
|
197
|
+
!validArtifactPayloadTransition(
|
|
198
|
+
undefined,
|
|
199
|
+
value.payload,
|
|
200
|
+
value.payload?.plan,
|
|
201
|
+
)
|
|
202
|
+
)
|
|
203
|
+
throw new Error("invalid artifact record");
|
|
204
|
+
return artifactPayloadReadValue(value.payload);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
const journalValue = lastArtifactJournalValue(text);
|
|
207
|
+
if (journalValue.found) return artifactPayloadReadValue(journalValue.value);
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function lastArtifactJournalValue(text) {
|
|
213
|
+
let value;
|
|
214
|
+
let expectedSequence = 0;
|
|
215
|
+
let journalId;
|
|
216
|
+
let previousChecksum = null;
|
|
217
|
+
let rootPlan;
|
|
218
|
+
let found = false;
|
|
219
|
+
for (const line of text.split("\n")) {
|
|
220
|
+
if (!line) continue;
|
|
221
|
+
let record;
|
|
222
|
+
try {
|
|
223
|
+
record = JSON.parse(line);
|
|
224
|
+
} catch {
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
if (
|
|
228
|
+
!validArtifactRecord(record, {
|
|
229
|
+
sequence: expectedSequence,
|
|
230
|
+
journalId: journalId ?? record.journalId,
|
|
231
|
+
previousChecksum,
|
|
232
|
+
}) ||
|
|
233
|
+
!validArtifactPayloadTransition(
|
|
234
|
+
value,
|
|
235
|
+
record.payload,
|
|
236
|
+
rootPlan ?? record.payload?.plan,
|
|
237
|
+
)
|
|
238
|
+
)
|
|
239
|
+
break;
|
|
240
|
+
value = record.payload;
|
|
241
|
+
journalId = record.journalId;
|
|
242
|
+
previousChecksum = record.checksum;
|
|
243
|
+
rootPlan ??= record.payload.plan;
|
|
244
|
+
expectedSequence += 1;
|
|
245
|
+
found = true;
|
|
246
|
+
}
|
|
247
|
+
return { found, value };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function isArtifactRecord(value) {
|
|
251
|
+
return (
|
|
252
|
+
value &&
|
|
253
|
+
typeof value === "object" &&
|
|
254
|
+
!Array.isArray(value) &&
|
|
255
|
+
value.journal === ARTIFACT_JOURNAL
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function validArtifactRecord(
|
|
260
|
+
record,
|
|
261
|
+
{ sequence, journalId, previousChecksum },
|
|
262
|
+
) {
|
|
263
|
+
if (
|
|
264
|
+
!isArtifactRecord(record) ||
|
|
265
|
+
record.sequence !== sequence ||
|
|
266
|
+
typeof record.journalId !== "string" ||
|
|
267
|
+
!record.journalId ||
|
|
268
|
+
record.journalId !== journalId ||
|
|
269
|
+
record.previousChecksum !== previousChecksum ||
|
|
270
|
+
!Object.hasOwn(record, "payload") ||
|
|
271
|
+
typeof record.checksum !== "string"
|
|
272
|
+
)
|
|
273
|
+
return false;
|
|
274
|
+
const body = {
|
|
275
|
+
journal: record.journal,
|
|
276
|
+
journalId: record.journalId,
|
|
277
|
+
sequence: record.sequence,
|
|
278
|
+
previousChecksum: record.previousChecksum,
|
|
279
|
+
payload: record.payload,
|
|
280
|
+
};
|
|
281
|
+
return (
|
|
282
|
+
createHash("sha256").update(JSON.stringify(body)).digest("hex") ===
|
|
283
|
+
record.checksum
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function validArtifactPayloadTransition(previous, next, rootPlan) {
|
|
288
|
+
if (isCommandArtifactPayload(previous) || isCommandArtifactPayload(next))
|
|
289
|
+
return validCommandArtifactPayloadTransition(previous, next);
|
|
290
|
+
return validApplyArtifactPayloadTransition(previous, next, rootPlan);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function validCommandArtifactPayloadTransition(previous, next) {
|
|
294
|
+
const nextState = commandArtifactPayloadState(next);
|
|
295
|
+
if (!nextState || !validCommandArtifactPayload(next)) return false;
|
|
296
|
+
if (previous === undefined) return nextState === "reserved";
|
|
297
|
+
const previousState = commandArtifactPayloadState(previous);
|
|
298
|
+
if (
|
|
299
|
+
!previousState ||
|
|
300
|
+
!validCommandArtifactPayload(previous) ||
|
|
301
|
+
!sameCommandArtifactIdentity(previous, next) ||
|
|
302
|
+
previousState === "completed"
|
|
303
|
+
)
|
|
304
|
+
return false;
|
|
305
|
+
if (previousState === "reserved") return nextState === "running";
|
|
306
|
+
return nextState === "running" || nextState === "completed";
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function validApplyArtifactPayloadTransition(previous, next, rootPlan) {
|
|
310
|
+
const nextState = artifactPayloadState(next);
|
|
311
|
+
if (
|
|
312
|
+
!nextState ||
|
|
313
|
+
!validPlanIdentity(rootPlan) ||
|
|
314
|
+
!validPayloadIdentity(next) ||
|
|
315
|
+
(next.plan !== undefined && !sameCanonical(next.plan, rootPlan))
|
|
316
|
+
)
|
|
317
|
+
return false;
|
|
318
|
+
if (previous === undefined) return nextState === "reserved";
|
|
319
|
+
const previousState = artifactPayloadState(previous);
|
|
320
|
+
if (
|
|
321
|
+
!previousState ||
|
|
322
|
+
previousState === "final" ||
|
|
323
|
+
nextState === "reserved" ||
|
|
324
|
+
(previousState === "reserved" && nextState !== "applying") ||
|
|
325
|
+
artifactWorkspaceId(previous) !== artifactWorkspaceId(next) ||
|
|
326
|
+
artifactPlanHash(previous) !== artifactPlanHash(next)
|
|
327
|
+
)
|
|
328
|
+
return false;
|
|
329
|
+
if (nextState === "final" && next.receipt.pendingStep !== undefined)
|
|
330
|
+
return false;
|
|
331
|
+
if (
|
|
332
|
+
nextState === "final" &&
|
|
333
|
+
next.receipt.completedSteps.length !== rootPlan.steps.length
|
|
334
|
+
)
|
|
335
|
+
return false;
|
|
336
|
+
if (nextState === "final" && !validFinalBundle(next)) return false;
|
|
337
|
+
return (
|
|
338
|
+
validReceiptPlanBinding(next.receipt, rootPlan) &&
|
|
339
|
+
validReceiptLineage(previous.receipt, next.receipt)
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function isCommandArtifactPayload(payload) {
|
|
344
|
+
return (
|
|
345
|
+
payload &&
|
|
346
|
+
typeof payload === "object" &&
|
|
347
|
+
!Array.isArray(payload) &&
|
|
348
|
+
payload.journalType === "command"
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function commandArtifactPayloadState(payload) {
|
|
353
|
+
if (!isCommandArtifactPayload(payload)) return null;
|
|
354
|
+
return new Set(["reserved", "running", "completed"]).has(payload.status)
|
|
355
|
+
? payload.status
|
|
356
|
+
: null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function validCommandArtifactPayload(payload) {
|
|
360
|
+
const state = commandArtifactPayloadState(payload);
|
|
361
|
+
if (
|
|
362
|
+
!state ||
|
|
363
|
+
typeof payload.command !== "string" ||
|
|
364
|
+
!payload.command.trim() ||
|
|
365
|
+
typeof payload.inputHash !== "string" ||
|
|
366
|
+
!/^[a-f0-9]{64}$/u.test(payload.inputHash) ||
|
|
367
|
+
!Object.hasOwn(payload, "request") ||
|
|
368
|
+
!payload.request ||
|
|
369
|
+
typeof payload.request !== "object" ||
|
|
370
|
+
Array.isArray(payload.request) ||
|
|
371
|
+
(Object.hasOwn(payload, "workspaceId") &&
|
|
372
|
+
!validCommandWorkspaceId(payload.workspaceId))
|
|
373
|
+
)
|
|
374
|
+
return false;
|
|
375
|
+
if (state !== "completed")
|
|
376
|
+
return (
|
|
377
|
+
!Object.hasOwn(payload, "result") && !Object.hasOwn(payload, "resultHash")
|
|
378
|
+
);
|
|
379
|
+
return (
|
|
380
|
+
Object.hasOwn(payload, "result") &&
|
|
381
|
+
typeof payload.resultHash === "string" &&
|
|
382
|
+
/^[a-f0-9]{64}$/u.test(payload.resultHash) &&
|
|
383
|
+
payload.resultHash === hashArtifactValue(payload.result)
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function validCommandWorkspaceId(value) {
|
|
388
|
+
return (
|
|
389
|
+
(typeof value === "string" && Boolean(value.trim())) ||
|
|
390
|
+
(typeof value === "number" && Number.isFinite(value))
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function sameCommandArtifactIdentity(previous, next) {
|
|
395
|
+
return (
|
|
396
|
+
previous.command === next.command &&
|
|
397
|
+
previous.inputHash === next.inputHash &&
|
|
398
|
+
Object.hasOwn(previous, "workspaceId") ===
|
|
399
|
+
Object.hasOwn(next, "workspaceId") &&
|
|
400
|
+
previous.workspaceId === next.workspaceId &&
|
|
401
|
+
sameCanonical(previous.request, next.request)
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function artifactPayloadReadValue(payload) {
|
|
406
|
+
return isCommandArtifactPayload(payload) && payload.status === "completed"
|
|
407
|
+
? payload.result
|
|
408
|
+
: payload;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function validFinalBundle(bundle) {
|
|
412
|
+
if (!bundle || typeof bundle !== "object" || Array.isArray(bundle))
|
|
413
|
+
return false;
|
|
414
|
+
const plan = bundle.plan;
|
|
415
|
+
const receipt = bundle.receipt;
|
|
416
|
+
return Boolean(
|
|
417
|
+
!hasNonFinalMarker(bundle) &&
|
|
418
|
+
validFinalBaseline(bundle.baseline) &&
|
|
419
|
+
validTimestamp(bundle.mutationStartedAt) &&
|
|
420
|
+
validPlanIdentity(plan) &&
|
|
421
|
+
validReceiptShape(receipt) &&
|
|
422
|
+
!hasNonFinalMarker(receipt) &&
|
|
423
|
+
validEnvelopeHash(plan, "planHash") &&
|
|
424
|
+
validEnvelopeHash(receipt, "receiptHash") &&
|
|
425
|
+
plan.workspaceId === receipt.workspaceId &&
|
|
426
|
+
plan.planHash === receipt.planHash &&
|
|
427
|
+
receipt.pendingStep === undefined &&
|
|
428
|
+
receipt.completedSteps.length === plan.steps.length &&
|
|
429
|
+
receipt.completedSteps.every(
|
|
430
|
+
(step) => step.state === undefined && !hasNonFinalMarker(step),
|
|
431
|
+
) &&
|
|
432
|
+
validReceiptPlanBinding(receipt, plan),
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function validRollbackBundle(bundle) {
|
|
437
|
+
if (!bundle || typeof bundle !== "object" || Array.isArray(bundle))
|
|
438
|
+
return false;
|
|
439
|
+
const allowed = new Set([
|
|
440
|
+
"baseline",
|
|
441
|
+
"mutationStartedAt",
|
|
442
|
+
"plan",
|
|
443
|
+
"receipt",
|
|
444
|
+
"status",
|
|
445
|
+
]);
|
|
446
|
+
if (
|
|
447
|
+
Object.keys(bundle).some((field) => !allowed.has(field)) ||
|
|
448
|
+
(bundle.status !== undefined && bundle.status !== "applying") ||
|
|
449
|
+
["partial", "uncertain", "reloadRequired"].some((field) =>
|
|
450
|
+
Object.hasOwn(bundle, field),
|
|
451
|
+
) ||
|
|
452
|
+
!validFinalBaseline(bundle.baseline) ||
|
|
453
|
+
!validTimestamp(bundle.mutationStartedAt)
|
|
454
|
+
)
|
|
455
|
+
return false;
|
|
456
|
+
const plan = bundle.plan;
|
|
457
|
+
const receipt = bundle.receipt;
|
|
458
|
+
return Boolean(
|
|
459
|
+
validPlanIdentity(plan) &&
|
|
460
|
+
validReceiptShape(receipt) &&
|
|
461
|
+
validEnvelopeHash(plan, "planHash") &&
|
|
462
|
+
validEnvelopeHash(receipt, "receiptHash") &&
|
|
463
|
+
plan.workspaceId === receipt.workspaceId &&
|
|
464
|
+
plan.planHash === receipt.planHash &&
|
|
465
|
+
!hasNonFinalMarker(receipt) &&
|
|
466
|
+
(bundle.status === "applying" ||
|
|
467
|
+
(receipt.pendingStep === undefined &&
|
|
468
|
+
receipt.completedSteps.length === plan.steps.length &&
|
|
469
|
+
receipt.completedSteps.every(
|
|
470
|
+
(step) => step.state === undefined && !hasNonFinalMarker(step),
|
|
471
|
+
))) &&
|
|
472
|
+
validReceiptPlanBinding(receipt, plan),
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function validFinalBaseline(baseline) {
|
|
477
|
+
return Boolean(
|
|
478
|
+
baseline &&
|
|
479
|
+
typeof baseline === "object" &&
|
|
480
|
+
!Array.isArray(baseline) &&
|
|
481
|
+
Array.isArray(baseline.baselineTaskUids) &&
|
|
482
|
+
validTimestamp(baseline.capturedAt),
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function validTimestamp(value) {
|
|
487
|
+
return (
|
|
488
|
+
typeof value === "string" &&
|
|
489
|
+
value.trim() &&
|
|
490
|
+
Number.isFinite(Date.parse(value))
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function hasNonFinalMarker(value) {
|
|
495
|
+
return ["status", "partial", "uncertain", "reloadRequired"].some((field) =>
|
|
496
|
+
Object.hasOwn(value ?? {}, field),
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function validEnvelopeHash(value, field) {
|
|
501
|
+
if (typeof value?.[field] !== "string" || !value[field]) return false;
|
|
502
|
+
const body = structuredClone(value);
|
|
503
|
+
delete body[field];
|
|
504
|
+
return value[field] === hashArtifactValue(body);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function artifactPayloadState(payload) {
|
|
508
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
509
|
+
return null;
|
|
510
|
+
if (payload.status === "reserved") return "reserved";
|
|
511
|
+
if (payload.status === "applying") return "applying";
|
|
512
|
+
if (payload.status === undefined && payload.receipt) return "final";
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function validPayloadIdentity(payload) {
|
|
517
|
+
const state = artifactPayloadState(payload);
|
|
518
|
+
const plan = payload.plan;
|
|
519
|
+
const receipt = payload.receipt;
|
|
520
|
+
if (state === "reserved")
|
|
521
|
+
return validPlanIdentity(plan) && receipt === undefined;
|
|
522
|
+
if (!validReceiptShape(receipt)) return false;
|
|
523
|
+
if (plan !== undefined && !validPlanIdentity(plan)) return false;
|
|
524
|
+
return (
|
|
525
|
+
(plan === undefined || plan.workspaceId === receipt.workspaceId) &&
|
|
526
|
+
(plan === undefined || plan.planHash === receipt.planHash)
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function validPlanIdentity(plan) {
|
|
531
|
+
return (
|
|
532
|
+
plan &&
|
|
533
|
+
typeof plan === "object" &&
|
|
534
|
+
!Array.isArray(plan) &&
|
|
535
|
+
plan.workspaceId !== undefined &&
|
|
536
|
+
typeof plan.planHash === "string" &&
|
|
537
|
+
plan.planHash &&
|
|
538
|
+
Array.isArray(plan.steps)
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function validReceiptShape(receipt) {
|
|
543
|
+
return (
|
|
544
|
+
receipt &&
|
|
545
|
+
typeof receipt === "object" &&
|
|
546
|
+
!Array.isArray(receipt) &&
|
|
547
|
+
receipt.workspaceId !== undefined &&
|
|
548
|
+
typeof receipt.planHash === "string" &&
|
|
549
|
+
receipt.planHash &&
|
|
550
|
+
Array.isArray(receipt.completedSteps) &&
|
|
551
|
+
!Object.hasOwn(receipt, "pendingSteps") &&
|
|
552
|
+
(receipt.pendingStep === undefined ||
|
|
553
|
+
(receipt.pendingStep &&
|
|
554
|
+
typeof receipt.pendingStep === "object" &&
|
|
555
|
+
!Array.isArray(receipt.pendingStep)))
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function validReceiptPlanBinding(receipt, plan) {
|
|
560
|
+
if (receipt.completedSteps.length > plan.steps.length) return false;
|
|
561
|
+
if (
|
|
562
|
+
!receipt.completedSteps.every((step, index) =>
|
|
563
|
+
validBoundReceiptStep(step, plan.steps[index], index, false),
|
|
564
|
+
)
|
|
565
|
+
)
|
|
566
|
+
return false;
|
|
567
|
+
if (receipt.pendingStep === undefined) return true;
|
|
568
|
+
const pendingIndex = receipt.completedSteps.length;
|
|
569
|
+
return (
|
|
570
|
+
pendingIndex < plan.steps.length &&
|
|
571
|
+
validBoundReceiptStep(
|
|
572
|
+
receipt.pendingStep,
|
|
573
|
+
plan.steps[pendingIndex],
|
|
574
|
+
pendingIndex,
|
|
575
|
+
true,
|
|
576
|
+
)
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function validBoundReceiptStep(actual, planned, index, pending) {
|
|
581
|
+
const generatedIdentity = hasResolvedGeneratedIdentity(actual, planned);
|
|
582
|
+
if (
|
|
583
|
+
!actual ||
|
|
584
|
+
!planned ||
|
|
585
|
+
actual.planStepIndex !== index ||
|
|
586
|
+
actual.planStepHash !== hashArtifactValue(planned) ||
|
|
587
|
+
!sameCanonical(stepStaticBody(actual), stepStaticBody(planned)) ||
|
|
588
|
+
!validRuntimeEndpoint(actual, planned)
|
|
589
|
+
)
|
|
590
|
+
return false;
|
|
591
|
+
const allowedStates = plannedPossibleStates(planned);
|
|
592
|
+
if (pending)
|
|
593
|
+
return (
|
|
594
|
+
actual.state === "uncertain" &&
|
|
595
|
+
actual.operation === planned.operation &&
|
|
596
|
+
actual.endpoint === planned.endpoint &&
|
|
597
|
+
sameCanonical(actual.after, planned.after) &&
|
|
598
|
+
sameCanonical(
|
|
599
|
+
actual.possibleAfter,
|
|
600
|
+
uniqueCanonicalStates(allowedStates),
|
|
601
|
+
) &&
|
|
602
|
+
actual.identityResolved === (planned.before !== null) &&
|
|
603
|
+
actual.rollbackRecreate === undefined
|
|
604
|
+
);
|
|
605
|
+
if (actual.state === undefined)
|
|
606
|
+
return (
|
|
607
|
+
actual.operation === planned.operation &&
|
|
608
|
+
runtimeStateMatches(actual.after, planned.after, generatedIdentity) &&
|
|
609
|
+
actual.possibleAfter === undefined &&
|
|
610
|
+
actual.identityResolved === undefined &&
|
|
611
|
+
actual.rollbackRecreate === undefined
|
|
612
|
+
);
|
|
613
|
+
if (!new Set(["uncertain", "verified"]).has(actual.state)) return false;
|
|
614
|
+
if (
|
|
615
|
+
!validPartialOperation(actual, planned) ||
|
|
616
|
+
!allowedStates.some((state) =>
|
|
617
|
+
runtimeStateMatches(actual.after, state, generatedIdentity),
|
|
618
|
+
) ||
|
|
619
|
+
!validPartialPossibleAfter(
|
|
620
|
+
actual.possibleAfter,
|
|
621
|
+
allowedStates,
|
|
622
|
+
generatedIdentity,
|
|
623
|
+
) ||
|
|
624
|
+
!validPartialIdentity(actual, planned)
|
|
625
|
+
)
|
|
626
|
+
return false;
|
|
627
|
+
return (
|
|
628
|
+
actual.rollbackRecreate === undefined ||
|
|
629
|
+
(actual.rollbackRecreate === true && planned.operation === "REPLACE")
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function stepStaticBody(step) {
|
|
634
|
+
const result = structuredClone(step);
|
|
635
|
+
for (const field of [
|
|
636
|
+
"after",
|
|
637
|
+
"endpoint",
|
|
638
|
+
"identityResolved",
|
|
639
|
+
"operation",
|
|
640
|
+
"planStepHash",
|
|
641
|
+
"planStepIndex",
|
|
642
|
+
"possibleAfter",
|
|
643
|
+
"rollbackRecreate",
|
|
644
|
+
"state",
|
|
645
|
+
])
|
|
646
|
+
delete result[field];
|
|
647
|
+
return result;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function validRuntimeEndpoint(actual, planned) {
|
|
651
|
+
if (actual.endpoint === planned.endpoint) return true;
|
|
652
|
+
return hasResolvedGeneratedIdentity(actual, planned);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function hasResolvedGeneratedIdentity(actual, planned) {
|
|
656
|
+
if (!new Set(["POST", "REPLACE"]).has(planned.operation)) return false;
|
|
657
|
+
if (planned.after?.id !== undefined) return false;
|
|
658
|
+
const id = actual.after?.id;
|
|
659
|
+
if (
|
|
660
|
+
planned.kind === "semantic_calibration" &&
|
|
661
|
+
planned.operation === "POST" &&
|
|
662
|
+
planned.before !== null &&
|
|
663
|
+
id !== undefined &&
|
|
664
|
+
actual.endpoint === planned.endpoint
|
|
665
|
+
)
|
|
666
|
+
return true;
|
|
667
|
+
return (
|
|
668
|
+
id !== undefined &&
|
|
669
|
+
planned.endpoint.endsWith("/") &&
|
|
670
|
+
actual.endpoint === `${planned.endpoint}${id}/`
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
function validPartialOperation(actual, planned) {
|
|
675
|
+
return (
|
|
676
|
+
actual.operation === planned.operation ||
|
|
677
|
+
(planned.kind === "semantic_calibration" &&
|
|
678
|
+
planned.operation === "REPLACE" &&
|
|
679
|
+
actual.operation === "DELETE")
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function validPartialIdentity(actual, planned) {
|
|
684
|
+
const expected =
|
|
685
|
+
planned.before !== null || actual.endpoint !== planned.endpoint;
|
|
686
|
+
if (actual.identityResolved === expected) return true;
|
|
687
|
+
return (
|
|
688
|
+
actual.identityResolved === false &&
|
|
689
|
+
planned.kind === "semantic_calibration" &&
|
|
690
|
+
planned.operation === "REPLACE" &&
|
|
691
|
+
actual.operation === "REPLACE" &&
|
|
692
|
+
actual.endpoint === planned.endpoint &&
|
|
693
|
+
actual.state === "uncertain" &&
|
|
694
|
+
actual.rollbackRecreate === true &&
|
|
695
|
+
sameCanonical(actual.after, planned.after) &&
|
|
696
|
+
sameCanonical(actual.possibleAfter, [null, withoutId(planned.after)])
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function validPartialPossibleAfter(
|
|
701
|
+
actualStates,
|
|
702
|
+
allowedStates,
|
|
703
|
+
generatedIdentity,
|
|
704
|
+
) {
|
|
705
|
+
if (!Array.isArray(actualStates) || actualStates.length === 0) return false;
|
|
706
|
+
let previousIndex = -1;
|
|
707
|
+
for (const actual of actualStates) {
|
|
708
|
+
const index = allowedStates.findIndex(
|
|
709
|
+
(allowed, candidateIndex) =>
|
|
710
|
+
candidateIndex > previousIndex &&
|
|
711
|
+
runtimeStateMatches(actual, allowed, generatedIdentity),
|
|
712
|
+
);
|
|
713
|
+
if (index === -1) return false;
|
|
714
|
+
previousIndex = index;
|
|
715
|
+
}
|
|
716
|
+
return true;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function plannedPossibleStates(step) {
|
|
720
|
+
if (step.kind === "semantic_calibration" && step.operation === "POST")
|
|
721
|
+
return step.before === null
|
|
722
|
+
? [null, withoutId(step.after)]
|
|
723
|
+
: [withoutId(step.before), withoutId(step.after)];
|
|
724
|
+
if (step.operation === "REPLACE")
|
|
725
|
+
return [withoutId(step.before), null, withoutId(step.after)];
|
|
726
|
+
if (step.kind === "knowledge" && step.before === null && !step.after.active)
|
|
727
|
+
return [null, { ...step.after, active: true }, step.after];
|
|
728
|
+
return [step.before, step.after];
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function uniqueCanonicalStates(states) {
|
|
732
|
+
const seen = new Set();
|
|
733
|
+
return states.filter((state) => {
|
|
734
|
+
const key = JSON.stringify(canonicalArtifactValue(state));
|
|
735
|
+
if (seen.has(key)) return false;
|
|
736
|
+
seen.add(key);
|
|
737
|
+
return true;
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function runtimeStateMatches(actual, expected, generatedIdentity = false) {
|
|
742
|
+
if (actual === null || expected === null) return actual === expected;
|
|
743
|
+
if (
|
|
744
|
+
!actual ||
|
|
745
|
+
!expected ||
|
|
746
|
+
typeof actual !== "object" ||
|
|
747
|
+
typeof expected !== "object"
|
|
748
|
+
)
|
|
749
|
+
return sameCanonical(actual, expected);
|
|
750
|
+
const normalized = structuredClone(actual);
|
|
751
|
+
if (generatedIdentity && !Object.hasOwn(expected, "id")) delete normalized.id;
|
|
752
|
+
return sameCanonical(normalized, expected);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function artifactWorkspaceId(payload) {
|
|
756
|
+
return payload.receipt?.workspaceId ?? payload.plan?.workspaceId;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
function artifactPlanHash(payload) {
|
|
760
|
+
return payload.receipt?.planHash ?? payload.plan?.planHash;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function validReceiptLineage(previous, next) {
|
|
764
|
+
if (!validReceiptShape(next)) return false;
|
|
765
|
+
if (previous === undefined) return next.completedSteps.length === 0;
|
|
766
|
+
if (!validReceiptShape(previous)) return false;
|
|
767
|
+
if (
|
|
768
|
+
next.completedSteps.length < previous.completedSteps.length ||
|
|
769
|
+
!previous.completedSteps.every((step, index) =>
|
|
770
|
+
sameCanonical(step, next.completedSteps[index]),
|
|
771
|
+
)
|
|
772
|
+
)
|
|
773
|
+
return false;
|
|
774
|
+
const growth = next.completedSteps.length - previous.completedSteps.length;
|
|
775
|
+
if (growth > 1) return false;
|
|
776
|
+
if (previous.pendingStep === undefined) return growth === 0;
|
|
777
|
+
if (growth === 0)
|
|
778
|
+
return (
|
|
779
|
+
next.pendingStep !== undefined &&
|
|
780
|
+
sameCanonical(previous.pendingStep, next.pendingStep)
|
|
781
|
+
);
|
|
782
|
+
return (
|
|
783
|
+
next.pendingStep === undefined &&
|
|
784
|
+
sameStepLineage(
|
|
785
|
+
previous.pendingStep,
|
|
786
|
+
next.completedSteps[next.completedSteps.length - 1],
|
|
787
|
+
)
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
function sameStepLineage(pending, completed) {
|
|
792
|
+
return (
|
|
793
|
+
pending?.planStepIndex === completed?.planStepIndex &&
|
|
794
|
+
pending?.planStepHash === completed?.planStepHash
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
async function syncArtifactDirectory(artifact, openDirectory) {
|
|
799
|
+
let handle;
|
|
800
|
+
try {
|
|
801
|
+
handle = await openDirectory(dirname(artifact), "r");
|
|
802
|
+
try {
|
|
803
|
+
await handle.sync();
|
|
804
|
+
} catch (error) {
|
|
805
|
+
// reserveArtifact has already completed its exclusive file write and fsync.
|
|
806
|
+
// Only the native Windows directory-fsync limitation may lose this extra
|
|
807
|
+
// directory-entry durability guarantee; all other I/O errors stay fatal.
|
|
808
|
+
if (
|
|
809
|
+
process.platform !== "win32" ||
|
|
810
|
+
openDirectory !== open ||
|
|
811
|
+
error?.code !== "EPERM" ||
|
|
812
|
+
error?.errno !== -4048 ||
|
|
813
|
+
error?.syscall !== "fsync" ||
|
|
814
|
+
!(await handle.stat()).isDirectory()
|
|
815
|
+
)
|
|
816
|
+
throw error;
|
|
817
|
+
if (!directorySyncWarningEmitted) {
|
|
818
|
+
directorySyncWarningEmitted = true;
|
|
819
|
+
process.emitWarning(
|
|
820
|
+
"Windows已同步工件文件,但不支持目录项fsync;目录项的断电持久性无法保证。请保留并核对工件,不要自动重放写入。",
|
|
821
|
+
{ code: "DATARC_DIRECTORY_SYNC_UNAVAILABLE" },
|
|
822
|
+
);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
} finally {
|
|
826
|
+
await handle?.close();
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
async function writeAll(handle, content, position) {
|
|
831
|
+
let offset = 0;
|
|
832
|
+
while (offset < content.length) {
|
|
833
|
+
const { bytesWritten } = await handle.write(
|
|
834
|
+
content,
|
|
835
|
+
offset,
|
|
836
|
+
content.length - offset,
|
|
837
|
+
position + offset,
|
|
838
|
+
);
|
|
839
|
+
if (
|
|
840
|
+
!Number.isInteger(bytesWritten) ||
|
|
841
|
+
bytesWritten <= 0 ||
|
|
842
|
+
bytesWritten > content.length - offset
|
|
843
|
+
)
|
|
844
|
+
throw new Error("artifact write made no progress");
|
|
845
|
+
offset += bytesWritten;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
async function readStdin() {
|
|
850
|
+
const chunks = [];
|
|
851
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
852
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
853
|
+
}
|