@tiangong-lca/foundry 0.1.9 → 0.1.11
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 +13 -11
- package/docs/architecture.md +12 -10
- package/docs/foundry-task-contracts.md +16 -4
- package/docs/import-profiles/bafu/constraints.md +1 -1
- package/docs/import-profiles/bafu/profile.md +1 -1
- package/docs/import-profiles/worldsteel/constraints.md +3 -3
- package/docs/import-profiles/worldsteel/import-coverage.md +3 -3
- package/docs/import-profiles/worldsteel/import-plan.md +4 -4
- package/docs/import-profiles/worldsteel/profile.md +3 -3
- package/docs/package-distribution-contract.md +11 -9
- package/docs/public-runtime-contract.md +31 -9
- package/docs/runtime-context-contract.md +7 -5
- package/docs/task-authorization-contract.md +5 -3
- package/package-dist/assets/foundry-package-descriptor.json +300 -154
- package/package-dist/scripts/foundry-facade.d.ts +1 -0
- package/package-dist/scripts/foundry-facade.js +233 -28
- package/package-dist/scripts/foundry-runtime.d.ts +14 -3
- package/package-dist/scripts/foundry-runtime.js +2 -2
- package/package-dist/scripts/lib/finalize-owners/commit-handoff.js +3 -3
- package/package-dist/scripts/lib/finalize-owners/{native-insert-closeout.d.ts → native-draft-closeout.d.ts} +1 -1
- package/package-dist/scripts/lib/finalize-owners/{native-insert-closeout.js → native-draft-closeout.js} +10 -6
- package/package-dist/scripts/lib/finalize-owners/{native-insert-handoff.d.ts → native-draft-handoff.d.ts} +7 -5
- package/package-dist/scripts/lib/finalize-owners/{native-insert-handoff.js → native-draft-handoff.js} +30 -11
- package/package-dist/scripts/lib/finalize-owners/post-write-closeout.js +2 -2
- package/package-dist/scripts/lib/foundry-authorization-input.d.ts +10 -1
- package/package-dist/scripts/lib/foundry-authorization-input.js +27 -3
- package/package-dist/scripts/lib/foundry-command-metadata.js +25 -2
- package/package-dist/scripts/lib/foundry-command-registry.d.ts +4 -4
- package/package-dist/scripts/lib/foundry-command-registry.js +3 -0
- package/package-dist/scripts/lib/foundry-execution-admission.js +3 -3
- package/package-dist/scripts/lib/foundry-facade-store.js +56 -6
- package/package-dist/scripts/lib/foundry-interaction-input.d.ts +19 -0
- package/package-dist/scripts/lib/foundry-interaction-input.js +327 -0
- package/package-dist/scripts/lib/foundry-interaction-projection.d.ts +22 -0
- package/package-dist/scripts/lib/foundry-interaction-projection.js +81 -0
- package/package-dist/scripts/lib/foundry-interaction-types.d.ts +17 -0
- package/package-dist/scripts/lib/foundry-interaction-types.js +4 -0
- package/package-dist/scripts/lib/foundry-owner-execution-store.d.ts +33 -16
- package/package-dist/scripts/lib/foundry-owner-execution-store.js +66 -5
- package/package-dist/scripts/lib/foundry-owner-readback.d.ts +3 -0
- package/package-dist/scripts/lib/foundry-owner-readback.js +101 -27
- package/package-dist/scripts/lib/foundry-package-contract.d.ts +4 -4
- package/package-dist/scripts/lib/foundry-package-contract.js +4 -2
- package/package-dist/scripts/lib/foundry-repair-execution.d.ts +11 -0
- package/package-dist/scripts/lib/foundry-repair-execution.js +70 -0
- package/package-dist/scripts/lib/foundry-repair-handoff.d.ts +16 -0
- package/package-dist/scripts/lib/foundry-repair-handoff.js +108 -0
- package/package-dist/scripts/lib/foundry-repair-preflight.d.ts +96 -0
- package/package-dist/scripts/lib/foundry-repair-preflight.js +313 -0
- package/package-dist/scripts/lib/foundry-repair-reference-binding.d.ts +26 -0
- package/package-dist/scripts/lib/foundry-repair-reference-binding.js +120 -0
- package/package-dist/scripts/lib/foundry-repair-reference-proof.d.ts +63 -0
- package/package-dist/scripts/lib/foundry-repair-reference-proof.js +329 -0
- package/package-dist/scripts/lib/foundry-repair-scope.d.ts +76 -0
- package/package-dist/scripts/lib/foundry-repair-scope.js +304 -0
- package/package-dist/scripts/lib/foundry-runtime-command-policy.js +1 -0
- package/package-dist/scripts/lib/foundry-runtime-qualification.js +2 -2
- package/package-dist/scripts/lib/foundry-runtime-utils.d.ts +3 -0
- package/package-dist/scripts/lib/foundry-runtime-utils.js +6 -1
- package/package-dist/scripts/lib/foundry-semantic-input.d.ts +2 -0
- package/package-dist/scripts/lib/foundry-semantic-input.js +29 -2
- package/package-dist/scripts/lib/foundry-semantic-interaction.d.ts +12 -0
- package/package-dist/scripts/lib/foundry-semantic-interaction.js +38 -0
- package/package-dist/scripts/lib/foundry-successor-predecessor.d.ts +36 -0
- package/package-dist/scripts/lib/foundry-successor-predecessor.js +399 -0
- package/package-dist/scripts/lib/foundry-task-registration.js +64 -3
- package/package-dist/scripts/lib/foundry-task-start-spec.d.ts +14 -0
- package/package-dist/scripts/lib/foundry-task-start-spec.js +60 -1
- package/package-dist/scripts/lib/foundry-task-store.d.ts +13 -1
- package/package-dist/scripts/lib/foundry-task-store.js +23 -12
- package/package-dist/scripts/lib/foundry-task-types.d.ts +22 -1
- package/package-dist/scripts/lib/foundry-tidas-source.d.ts +15 -0
- package/package-dist/scripts/lib/foundry-tidas-source.js +90 -0
- package/package-dist/scripts/lib/foundry-workflow-assessment.d.ts +7 -1
- package/package-dist/scripts/lib/foundry-workflow-assessment.js +127 -6
- package/package-dist/scripts/lib/foundry-workflow-authorization.d.ts +2 -1
- package/package-dist/scripts/lib/foundry-workflow-authorization.js +194 -26
- package/package-dist/scripts/lib/foundry-workflow-context.js +4 -0
- package/package-dist/scripts/lib/foundry-workflow-execution.js +6 -1
- package/package-dist/scripts/lib/foundry-workflow-finalize.js +7 -4
- package/package-dist/scripts/lib/foundry-workflow-interaction.d.ts +4 -0
- package/package-dist/scripts/lib/foundry-workflow-interaction.js +63 -0
- package/package-dist/scripts/lib/foundry-workflow-repair.d.ts +36 -0
- package/package-dist/scripts/lib/foundry-workflow-repair.js +580 -0
- package/package-dist/scripts/lib/foundry-workflow-semantic.js +21 -1
- package/package-dist/scripts/lib/foundry-workflow-state.d.ts +9 -1
- package/package-dist/scripts/lib/foundry-workflow-state.js +239 -39
- package/package-dist/scripts/lib/import-curation/curation-cleanup.js +39 -8
- package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
- package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.d.ts +13 -2
- package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.js +100 -29
- package/package-dist/scripts/lib/import-curation/internal/workflow-authoring-tasks.js +1 -2
- package/package-dist/scripts/lib/import-curation/internal/workflow-patch-collect.js +2 -3
- package/package-dist/scripts/lib/import-curation/internal/workflow-queue-context.js +8 -8
- package/package-dist/scripts/lib/import-curation/internal/workflow-semantic-actions.js +12 -2
- package/package-dist/scripts/public-api.d.ts +2 -0
- package/package-dist/scripts/public-api.js +1 -0
- package/package-dist/scripts/runtime-entry.js +9 -1
- package/package.json +3 -2
- package/specs/import-profiles.json +1 -1
- package/specs/schemas/foundry-authorization-input.schema.json +45 -2
- package/specs/schemas/foundry-interaction-input.schema.json +173 -0
- package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
- package/specs/schemas/foundry-package-descriptor.schema.json +3 -2
- package/specs/schemas/foundry-semantic-input.schema.json +12 -1
- package/specs/schemas/foundry-task-start.schema.json +275 -19
- package/specs/schemas/runtime-qualification.schema.json +1 -1
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { FoundryContextError } from "./foundry-runtime-context.js";
|
|
3
|
+
import { sha256Json, sha256Text } from "./identity-preflight-proof.js";
|
|
4
|
+
import { datasetIdentity } from "./import-curation/internal/dataset-payload.js";
|
|
5
|
+
import { readRows } from "./import-curation/internal/runtime-io.js";
|
|
6
|
+
import { readNativeDraftHandoff } from "./finalize-owners/native-draft-handoff.js";
|
|
7
|
+
/** Phase 1 admits only Process, and only the two reviewed administrative display-text regions. */
|
|
8
|
+
export const FOUNDRY_REPAIR_SELECTION_KIND = "existing-owner-draft-metadata";
|
|
9
|
+
export const FOUNDRY_REPAIR_ALLOWED_DATASET_TYPE = "process";
|
|
10
|
+
export const FOUNDRY_REPAIR_EDIT_PATHS = Object.freeze([
|
|
11
|
+
"processDataSet.modellingAndValidation.dataSourcesTreatmentAndRepresentativeness" +
|
|
12
|
+
".referenceToDataSource.*.common:shortDescription.*.#text",
|
|
13
|
+
"processDataSet.administrativeInformation.publicationAndOwnership" +
|
|
14
|
+
".common:referenceToOwnershipOfDataSet.*.common:shortDescription.*.#text",
|
|
15
|
+
]);
|
|
16
|
+
/**
|
|
17
|
+
* A repair touching any of these surfaces can change provider selection or scientific content, so it
|
|
18
|
+
* requires a content-bound provider-impact diagnostic that this repository does not produce yet.
|
|
19
|
+
*/
|
|
20
|
+
export const FOUNDRY_REPAIR_SHARED_SCIENCE_PREFIXES = Object.freeze([
|
|
21
|
+
"processDataSet.exchanges",
|
|
22
|
+
"processDataSet.quantitativeReference",
|
|
23
|
+
"processDataSet.modellingAndValidation.dataSourcesTreatmentAndRepresentativeness.annualSupplyOrProductionVolume",
|
|
24
|
+
"processDataSet.modellingAndValidation.LCIMethod",
|
|
25
|
+
"processDataSet.processInformation.dataSetInformation.name",
|
|
26
|
+
"processDataSet.processInformation.dataSetInformation.classificationInformation",
|
|
27
|
+
"processDataSet.processInformation.dataSetInformation.common:other",
|
|
28
|
+
"processDataSet.processInformation.geography",
|
|
29
|
+
"processDataSet.administrativeInformation.publicationAndOwnership.common:dataSetVersion",
|
|
30
|
+
"flowDataSet",
|
|
31
|
+
"flowPropertyDataSet",
|
|
32
|
+
"unitGroupDataSet",
|
|
33
|
+
]);
|
|
34
|
+
const idPattern = /^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$/u;
|
|
35
|
+
const shaPattern = /^[0-9a-f]{64}$/u;
|
|
36
|
+
const blankTextSentinel = "FoundryRepairBlankLanguageText";
|
|
37
|
+
const dataSourcePath = [
|
|
38
|
+
"processDataSet",
|
|
39
|
+
"modellingAndValidation",
|
|
40
|
+
"dataSourcesTreatmentAndRepresentativeness",
|
|
41
|
+
];
|
|
42
|
+
const ownershipPath = [
|
|
43
|
+
"processDataSet",
|
|
44
|
+
"administrativeInformation",
|
|
45
|
+
"publicationAndOwnership",
|
|
46
|
+
];
|
|
47
|
+
function fail(code, message) {
|
|
48
|
+
throw new FoundryContextError(code, message);
|
|
49
|
+
}
|
|
50
|
+
function isRecord(value) {
|
|
51
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
52
|
+
}
|
|
53
|
+
function record(value, code, label) {
|
|
54
|
+
if (!isRecord(value))
|
|
55
|
+
fail(code, `${label} must be an object.`);
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
function exact(value, keys, code, label) {
|
|
59
|
+
if (Object.keys(value).length !== keys.length || keys.some((key) => !Object.hasOwn(value, key)))
|
|
60
|
+
fail(code, `${label} has missing or unsupported fields.`);
|
|
61
|
+
}
|
|
62
|
+
function nonEmptyText(value, code, label) {
|
|
63
|
+
if (typeof value !== "string" || !value.trim())
|
|
64
|
+
fail(code, `${label} must be a non-empty string.`);
|
|
65
|
+
return value.trim();
|
|
66
|
+
}
|
|
67
|
+
function selectionPath(value, code, label) {
|
|
68
|
+
if (typeof value !== "string" ||
|
|
69
|
+
!value ||
|
|
70
|
+
value.length > 4_096 ||
|
|
71
|
+
/[\0\r\n]/u.test(value) ||
|
|
72
|
+
/^\.env(?:\.|$)/iu.test(value.split(/[\\/]/u).pop() ?? ""))
|
|
73
|
+
fail(code, `${label} must be a bounded non-credential file path.`);
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
/** Parse the optional repair selection of a task-start spec against the independently selected sources. */
|
|
77
|
+
export function parseFoundryRepairSelection(value, sources, resolveSelection = (file) => file) {
|
|
78
|
+
const item = record(value, "task_spec_repair_invalid", "Repair selection");
|
|
79
|
+
exact(item, ["kind", "contract", "before", "candidate", "predecessor"], "task_spec_repair_invalid", "Repair selection");
|
|
80
|
+
if (item.kind !== FOUNDRY_REPAIR_SELECTION_KIND)
|
|
81
|
+
fail("task_spec_repair_invalid", "Repair selection kind is unsupported.");
|
|
82
|
+
const contract = resolveSelection(selectionPath(item.contract, "task_spec_repair_invalid", "Repair contract"));
|
|
83
|
+
const before = resolveSelection(selectionPath(item.before, "task_spec_repair_invalid", "Repair before rows"));
|
|
84
|
+
const candidate = resolveSelection(selectionPath(item.candidate, "task_spec_repair_invalid", "Repair candidate rows"));
|
|
85
|
+
if (new Set([contract, before, candidate]).size !== 3)
|
|
86
|
+
fail("task_spec_repair_invalid", "Repair selections must name three distinct files.");
|
|
87
|
+
for (const selected of [contract, before, candidate])
|
|
88
|
+
if (!sources.some((source) => source.path === selected))
|
|
89
|
+
fail("task_spec_repair_invalid", "Repair contract, before rows and candidate rows must each be independently selected sources.");
|
|
90
|
+
let predecessor = null;
|
|
91
|
+
if (item.predecessor !== null) {
|
|
92
|
+
const bound = record(item.predecessor, "task_spec_repair_invalid", "Repair predecessor");
|
|
93
|
+
exact(bound, ["task_id", "receipt_sha256"], "task_spec_repair_invalid", "Repair predecessor");
|
|
94
|
+
const taskId = nonEmptyText(bound.task_id, "task_spec_repair_invalid", "Predecessor task id");
|
|
95
|
+
const receipt = nonEmptyText(bound.receipt_sha256, "task_spec_repair_invalid", "Predecessor receipt hash");
|
|
96
|
+
if (!idPattern.test(taskId) || !shaPattern.test(receipt))
|
|
97
|
+
fail("task_spec_repair_invalid", "Repair predecessor binding is malformed.");
|
|
98
|
+
predecessor = Object.freeze({ task_id: taskId, receipt_sha256: receipt });
|
|
99
|
+
}
|
|
100
|
+
return Object.freeze({
|
|
101
|
+
kind: FOUNDRY_REPAIR_SELECTION_KIND,
|
|
102
|
+
contract,
|
|
103
|
+
before,
|
|
104
|
+
candidate,
|
|
105
|
+
predecessor,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function navigate(value, path) {
|
|
109
|
+
let current = value;
|
|
110
|
+
for (const segment of path) {
|
|
111
|
+
if (!isRecord(current) || !Object.hasOwn(current, segment))
|
|
112
|
+
return null;
|
|
113
|
+
current = current[segment];
|
|
114
|
+
}
|
|
115
|
+
return current ?? null;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Replace every allowlisted language-node `#text` with one sentinel in a clone of the payload, so that
|
|
119
|
+
* a structure-preserving text-only edit of those nodes disappears while every other difference -
|
|
120
|
+
* values, structure, node count and order, language tags, ids and URIs - survives the canonical hash.
|
|
121
|
+
*/
|
|
122
|
+
export function blankRepairEditableText(payload) {
|
|
123
|
+
const copy = structuredClone(payload);
|
|
124
|
+
const description = (value) => {
|
|
125
|
+
if (Array.isArray(value))
|
|
126
|
+
return value.map(description);
|
|
127
|
+
return isRecord(value) && typeof value["#text"] === "string"
|
|
128
|
+
? { ...value, "#text": blankTextSentinel }
|
|
129
|
+
: value;
|
|
130
|
+
};
|
|
131
|
+
const reference = (value) => {
|
|
132
|
+
if (Array.isArray(value))
|
|
133
|
+
return value.map(reference);
|
|
134
|
+
return isRecord(value) && Object.hasOwn(value, "common:shortDescription")
|
|
135
|
+
? { ...value, "common:shortDescription": description(value["common:shortDescription"]) }
|
|
136
|
+
: value;
|
|
137
|
+
};
|
|
138
|
+
for (const [regionPath, key] of [
|
|
139
|
+
[dataSourcePath, "referenceToDataSource"],
|
|
140
|
+
[ownershipPath, "common:referenceToOwnershipOfDataSet"],
|
|
141
|
+
]) {
|
|
142
|
+
const holder = navigate(copy, regionPath);
|
|
143
|
+
if (!isRecord(holder))
|
|
144
|
+
continue;
|
|
145
|
+
if (Object.hasOwn(holder, key))
|
|
146
|
+
holder[key] = reference(holder[key]);
|
|
147
|
+
}
|
|
148
|
+
return copy;
|
|
149
|
+
}
|
|
150
|
+
/** Collect every leaf path where two payloads differ. Values are never included. */
|
|
151
|
+
export function collectRepairChangedPaths(before, after) {
|
|
152
|
+
const changed = [];
|
|
153
|
+
const walk = (left, right, path) => {
|
|
154
|
+
if (changed.length > 2_000)
|
|
155
|
+
return;
|
|
156
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
157
|
+
if (!Array.isArray(left) || !Array.isArray(right)) {
|
|
158
|
+
changed.push(path.join("."));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const leftItems = Array.isArray(left) ? left : [];
|
|
162
|
+
const rightItems = Array.isArray(right) ? right : [];
|
|
163
|
+
if (leftItems.length !== rightItems.length) {
|
|
164
|
+
changed.push([...path, "<length>"].join("."));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
leftItems.forEach((item, index) => walk(item, rightItems[index], [...path, String(index)]));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (isRecord(left) || isRecord(right)) {
|
|
171
|
+
if (!isRecord(left) || !isRecord(right)) {
|
|
172
|
+
changed.push(path.join("."));
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const leftRecord = isRecord(left) ? left : {};
|
|
176
|
+
const rightRecord = isRecord(right) ? right : {};
|
|
177
|
+
const keys = [...new Set([...Object.keys(leftRecord), ...Object.keys(rightRecord)])].sort();
|
|
178
|
+
for (const key of keys)
|
|
179
|
+
if (!Object.hasOwn(leftRecord, key) || !Object.hasOwn(rightRecord, key))
|
|
180
|
+
changed.push([...path, key].join("."));
|
|
181
|
+
else
|
|
182
|
+
walk(leftRecord[key], rightRecord[key], [...path, key]);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (sha256Json(left ?? null) !== sha256Json(right ?? null))
|
|
186
|
+
changed.push(path.join("."));
|
|
187
|
+
};
|
|
188
|
+
walk(before, after, []);
|
|
189
|
+
return changed;
|
|
190
|
+
}
|
|
191
|
+
function isSharedSciencePath(path) {
|
|
192
|
+
return FOUNDRY_REPAIR_SHARED_SCIENCE_PREFIXES.some((prefix) => path.startsWith(prefix));
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Decide whether a before/candidate payload pair stays inside the phase-1 metadata allowlist: only the
|
|
196
|
+
* existing language nodes' `#text` inside the two reviewed regions may change. Everything else blocks,
|
|
197
|
+
* and a shared-science change reports the missing provider-impact diagnostic instead.
|
|
198
|
+
*/
|
|
199
|
+
export function classifyRepairMetadataDiff(before, after) {
|
|
200
|
+
const changedPaths = collectRepairChangedPaths(before, after);
|
|
201
|
+
if (!changedPaths.length)
|
|
202
|
+
return { allowed: true, blockers: [] };
|
|
203
|
+
const structurePreserved = sha256Json(blankRepairEditableText(before)) === sha256Json(blankRepairEditableText(after));
|
|
204
|
+
if (structurePreserved)
|
|
205
|
+
return { allowed: true, blockers: [] };
|
|
206
|
+
const blockers = changedPaths.map((path) => ({
|
|
207
|
+
path,
|
|
208
|
+
reason: (isSharedSciencePath(path)
|
|
209
|
+
? "provider_impact_diagnostics_missing"
|
|
210
|
+
: "unsupported_path"),
|
|
211
|
+
}));
|
|
212
|
+
return blockers.length
|
|
213
|
+
? { allowed: false, blockers }
|
|
214
|
+
: { allowed: false, blockers: [{ path: "<unclassified>", reason: "structure_change" }] };
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Validate one repair task: the CLI execution contract, the CLI-fresh before rows and the candidate rows
|
|
218
|
+
* must agree on the exact same Process identities, every action must be a save_draft against an existing
|
|
219
|
+
* row, and the whole diff must stay inside the metadata allowlist. Nothing is dispatched here.
|
|
220
|
+
*/
|
|
221
|
+
export function readFoundryRepairScope(input) {
|
|
222
|
+
if (input.datasetType !== FOUNDRY_REPAIR_ALLOWED_DATASET_TYPE)
|
|
223
|
+
fail("repair_scope_unsupported", "Phase 1 admits only Process owner-draft metadata repair; other dataset types stay blocked.");
|
|
224
|
+
if (input.stateCode !== "0")
|
|
225
|
+
fail("repair_scope_unsupported", "Repair admits only state_code=0 owner drafts.");
|
|
226
|
+
const native = readNativeDraftHandoff({
|
|
227
|
+
contractFile: input.contractFile,
|
|
228
|
+
rowsFile: input.candidateFile,
|
|
229
|
+
datasetType: input.datasetType,
|
|
230
|
+
targetUserId: input.targetUserId,
|
|
231
|
+
verifiedProjectRef: input.verifiedProjectRef,
|
|
232
|
+
stateCode: input.stateCode,
|
|
233
|
+
relativePath: input.relativePath,
|
|
234
|
+
});
|
|
235
|
+
for (const action of native.contract.actions)
|
|
236
|
+
if (action.expected_operation !== "save_draft" || !action.before_sha256)
|
|
237
|
+
fail("repair_insert_not_allowed", "Repair actions must be save_draft against an existing owner draft; insert is never allowed.");
|
|
238
|
+
const payloadsOf = (file, expectedBytesSha256) => {
|
|
239
|
+
const text = fs.readFileSync(file, "utf8");
|
|
240
|
+
if (expectedBytesSha256 && sha256Text(text) !== expectedBytesSha256)
|
|
241
|
+
fail("repair_candidate_changed", "Candidate bytes changed between contract admission and metadata comparison.");
|
|
242
|
+
const rows = readRows(file, () => text);
|
|
243
|
+
const byIdentity = new Map();
|
|
244
|
+
rows.forEach((row, index) => {
|
|
245
|
+
const identity = datasetIdentity(row, index, input.datasetType);
|
|
246
|
+
const key = JSON.stringify([identity.id, identity.version]);
|
|
247
|
+
if (byIdentity.has(key))
|
|
248
|
+
fail("repair_scope_identity_mismatch", "Repair rows contain duplicate dataset identities.");
|
|
249
|
+
byIdentity.set(key, identity.payload);
|
|
250
|
+
});
|
|
251
|
+
return byIdentity;
|
|
252
|
+
};
|
|
253
|
+
const beforeByIdentity = payloadsOf(input.beforeFile);
|
|
254
|
+
const candidateByIdentity = payloadsOf(input.candidateFile, native.rows_sha256);
|
|
255
|
+
if (beforeByIdentity.size !== candidateByIdentity.size ||
|
|
256
|
+
[...beforeByIdentity.keys()].some((key) => !candidateByIdentity.has(key)))
|
|
257
|
+
fail("repair_scope_identity_mismatch", "Repair candidate rows must cover exactly the selected before identities; versions are never minted.");
|
|
258
|
+
const changedPaths = [];
|
|
259
|
+
const actions = native.contract.actions.map((action) => {
|
|
260
|
+
const key = JSON.stringify([action.id, action.version]);
|
|
261
|
+
const beforePayload = beforeByIdentity.get(key);
|
|
262
|
+
if (!beforePayload)
|
|
263
|
+
fail("repair_scope_identity_mismatch", "Every repair action must name an existing draft present in the selected before rows.");
|
|
264
|
+
if (sha256Json(beforePayload) !== action.before_sha256)
|
|
265
|
+
fail("repair_before_content_mismatch", "The contract before hash must equal the canonical hash of the selected before payload.");
|
|
266
|
+
const candidatePayload = candidateByIdentity.get(key);
|
|
267
|
+
if (sha256Json(candidatePayload) === sha256Json(beforePayload))
|
|
268
|
+
return {
|
|
269
|
+
action_id: action.action_id,
|
|
270
|
+
table: action.table,
|
|
271
|
+
id: action.id,
|
|
272
|
+
version: action.version,
|
|
273
|
+
before_sha256: action.before_sha256,
|
|
274
|
+
desired_sha256: action.desired_sha256,
|
|
275
|
+
};
|
|
276
|
+
const changed = collectRepairChangedPaths(beforePayload, candidatePayload);
|
|
277
|
+
const verdict = classifyRepairMetadataDiff(beforePayload, candidatePayload);
|
|
278
|
+
if (!verdict.allowed) {
|
|
279
|
+
fail(verdict.blockers.some((blocker) => blocker.reason === "provider_impact_diagnostics_missing")
|
|
280
|
+
? "repair_provider_impact_diagnostics_missing"
|
|
281
|
+
: "repair_metadata_scope_blocked", `Repair diff is outside the phase-1 metadata allowlist: ${verdict.blockers
|
|
282
|
+
.slice(0, 8)
|
|
283
|
+
.map((item) => item.path)
|
|
284
|
+
.join(", ")}`);
|
|
285
|
+
}
|
|
286
|
+
changedPaths.push(...changed);
|
|
287
|
+
return {
|
|
288
|
+
action_id: action.action_id,
|
|
289
|
+
table: action.table,
|
|
290
|
+
id: action.id,
|
|
291
|
+
version: action.version,
|
|
292
|
+
before_sha256: action.before_sha256,
|
|
293
|
+
desired_sha256: action.desired_sha256,
|
|
294
|
+
};
|
|
295
|
+
});
|
|
296
|
+
return Object.freeze({
|
|
297
|
+
kind: FOUNDRY_REPAIR_SELECTION_KIND,
|
|
298
|
+
status: changedPaths.length ? "dispatchable" : "satisfied",
|
|
299
|
+
contract_sha256: native.canonical_sha256,
|
|
300
|
+
rows_sha256: native.rows_sha256,
|
|
301
|
+
changed_paths: Object.freeze(changedPaths),
|
|
302
|
+
actions: Object.freeze(actions),
|
|
303
|
+
});
|
|
304
|
+
}
|
|
@@ -27,6 +27,7 @@ const taskStages = new Set([
|
|
|
27
27
|
"execution-capsule-admit",
|
|
28
28
|
"dataset-incremental-change-set-compose",
|
|
29
29
|
"dataset-topology-convergence-compose",
|
|
30
|
+
"final-delivery-promote",
|
|
30
31
|
"dataset-curation-queue-build",
|
|
31
32
|
"dataset-curation-gate",
|
|
32
33
|
"dataset-authoring-plan",
|
|
@@ -157,8 +157,8 @@ export function qualifyFoundryRuntime(context, options) {
|
|
|
157
157
|
catch {
|
|
158
158
|
return fail("runtime_cli_unqualified", "CLI runtime bytes do not match the independently selected C1 expectation.");
|
|
159
159
|
}
|
|
160
|
-
if (cli.package.version !== "0.1.
|
|
161
|
-
fail("runtime_cli_unqualified", "Foundry requires the exact CLI 0.1.
|
|
160
|
+
if (cli.package.version !== "0.1.19" || cli.platform !== context.platform)
|
|
161
|
+
fail("runtime_cli_unqualified", "Foundry requires the exact CLI 0.1.19 C1 runtime on the selected platform.");
|
|
162
162
|
const cliExpectation = Object.freeze({ ...options.cliExpectation });
|
|
163
163
|
if (cliExpectation.schema !== CLI_RUNTIME_EXPECTATION_SCHEMA)
|
|
164
164
|
fail("runtime_cli_unqualified", "CLI runtime expectation is not the supported schema.");
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type TiangongLcaCliTidasSchemaSource } from "./foundry-tidas-source.ts";
|
|
1
2
|
export interface InstalledTiangongLcaCliPackage {
|
|
2
3
|
packageName: string;
|
|
3
4
|
packageVersion: string;
|
|
@@ -7,6 +8,8 @@ export interface InstalledTiangongLcaCliPackage {
|
|
|
7
8
|
binName: string;
|
|
8
9
|
binPath: string;
|
|
9
10
|
schemaDir: string;
|
|
11
|
+
sourceManifestPath: string;
|
|
12
|
+
tidasSpecSource: TiangongLcaCliTidasSchemaSource;
|
|
10
13
|
}
|
|
11
14
|
export interface TiangongLcaCliRuntimeCommand {
|
|
12
15
|
command: string;
|
|
@@ -6,9 +6,10 @@ import path from "node:path";
|
|
|
6
6
|
import process from "node:process";
|
|
7
7
|
import { runtimeEnvFilePolicyDisabled, runtimeEnvFilePolicyKey, } from "./foundry-runtime-environment.js";
|
|
8
8
|
import { resolveFoundryRuntimePaths } from "./foundry-runtime-paths.js";
|
|
9
|
+
import { resolveTiangongLcaCliTidasSource, } from "./foundry-tidas-source.js";
|
|
9
10
|
const require = createRequire(import.meta.url);
|
|
10
11
|
const tiangongLcaCliPackageName = "@tiangong-lca/cli";
|
|
11
|
-
const tiangongLcaCliPackageVersion = "0.1.
|
|
12
|
+
const tiangongLcaCliPackageVersion = "0.1.19";
|
|
12
13
|
const tiangongLcaCliBinName = "tiangong-lca";
|
|
13
14
|
const { repoRoot: foundryRepoRoot } = resolveFoundryRuntimePaths(import.meta.url);
|
|
14
15
|
function isRecord(value) {
|
|
@@ -42,12 +43,14 @@ export function resolveInstalledTiangongLcaCliPackage() {
|
|
|
42
43
|
const packageRoot = path.dirname(packageJsonPath);
|
|
43
44
|
const binPath = path.resolve(packageRoot, binEntry);
|
|
44
45
|
const schemaDir = path.join(packageRoot, "assets", "tidas-schemas");
|
|
46
|
+
const sourceManifestPath = path.join(packageRoot, "assets", "tidas-spec-source.json");
|
|
45
47
|
if (!fs.existsSync(binPath) || !fs.statSync(binPath).isFile()) {
|
|
46
48
|
throw new Error(`Installed ${tiangongLcaCliPackageName} bin is missing at ${binPath}.`);
|
|
47
49
|
}
|
|
48
50
|
if (!fs.existsSync(schemaDir) || !fs.statSync(schemaDir).isDirectory()) {
|
|
49
51
|
throw new Error(`Installed ${tiangongLcaCliPackageName} schema assets are missing at ${schemaDir}.`);
|
|
50
52
|
}
|
|
53
|
+
const tidasSpecSource = resolveTiangongLcaCliTidasSource(sourceManifestPath, schemaDir);
|
|
51
54
|
return {
|
|
52
55
|
packageName: packageJson.name,
|
|
53
56
|
packageVersion: packageJson.version,
|
|
@@ -57,6 +60,8 @@ export function resolveInstalledTiangongLcaCliPackage() {
|
|
|
57
60
|
binName: tiangongLcaCliBinName,
|
|
58
61
|
binPath,
|
|
59
62
|
schemaDir,
|
|
63
|
+
sourceManifestPath,
|
|
64
|
+
tidasSpecSource,
|
|
60
65
|
};
|
|
61
66
|
}
|
|
62
67
|
function shellQuoteCommandToken(value) {
|
|
@@ -5,6 +5,7 @@ export interface SemanticSubmission {
|
|
|
5
5
|
readonly authoring_task_sha256: string;
|
|
6
6
|
readonly file: string;
|
|
7
7
|
readonly sha256: string;
|
|
8
|
+
readonly decision_ids?: readonly string[];
|
|
8
9
|
}
|
|
9
10
|
export type SemanticPatchInput = SemanticSubmission & {
|
|
10
11
|
readonly kind: "patch";
|
|
@@ -17,6 +18,7 @@ export interface FoundrySemanticInput {
|
|
|
17
18
|
task_id: string;
|
|
18
19
|
actor_id: string;
|
|
19
20
|
assessment_sha256: string;
|
|
21
|
+
interaction_sha256?: string;
|
|
20
22
|
submissions: readonly SemanticSubmission[];
|
|
21
23
|
}
|
|
22
24
|
export interface SelectedSemanticInput {
|
|
@@ -16,7 +16,14 @@ function exact(value, keys) {
|
|
|
16
16
|
}
|
|
17
17
|
export function parseFoundrySemanticInput(value) {
|
|
18
18
|
const data = workflowObject(value);
|
|
19
|
-
exact(data, [
|
|
19
|
+
exact(data, [
|
|
20
|
+
"schema",
|
|
21
|
+
"task_id",
|
|
22
|
+
"actor_id",
|
|
23
|
+
"assessment_sha256",
|
|
24
|
+
"submissions",
|
|
25
|
+
...(Object.hasOwn(data, "interaction_sha256") ? ["interaction_sha256"] : []),
|
|
26
|
+
]);
|
|
20
27
|
if (data.schema !== FOUNDRY_SEMANTIC_INPUT_SCHEMA ||
|
|
21
28
|
typeof data.task_id !== "string" ||
|
|
22
29
|
!/^task-[0-9a-f]{64}-r\d{4}$/u.test(data.task_id) ||
|
|
@@ -24,13 +31,21 @@ export function parseFoundrySemanticInput(value) {
|
|
|
24
31
|
!/^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$/u.test(data.actor_id) ||
|
|
25
32
|
typeof data.assessment_sha256 !== "string" ||
|
|
26
33
|
!sha.test(data.assessment_sha256) ||
|
|
34
|
+
(Object.hasOwn(data, "interaction_sha256") &&
|
|
35
|
+
(typeof data.interaction_sha256 !== "string" || !sha.test(data.interaction_sha256))) ||
|
|
27
36
|
!Array.isArray(data.submissions) ||
|
|
28
37
|
!data.submissions.length ||
|
|
29
38
|
data.submissions.length > 1000)
|
|
30
39
|
invalid("Semantic input must bind a task, actor, assessment and bounded submission selection.");
|
|
31
40
|
const submissions = data.submissions.map((item) => {
|
|
32
41
|
const part = workflowObject(item);
|
|
33
|
-
exact(part, [
|
|
42
|
+
exact(part, [
|
|
43
|
+
"kind",
|
|
44
|
+
"authoring_task_sha256",
|
|
45
|
+
"file",
|
|
46
|
+
"sha256",
|
|
47
|
+
...(Object.hasOwn(part, "decision_ids") ? ["decision_ids"] : []),
|
|
48
|
+
]);
|
|
34
49
|
if ((part.kind !== "patch" &&
|
|
35
50
|
part.kind !== "classification" &&
|
|
36
51
|
part.kind !== "location" &&
|
|
@@ -44,11 +59,20 @@ export function parseFoundrySemanticInput(value) {
|
|
|
44
59
|
part.file.length > 4096 ||
|
|
45
60
|
/[\0\r\n]/u.test(part.file))
|
|
46
61
|
invalid("A semantic submission reference is invalid.");
|
|
62
|
+
if (Object.hasOwn(part, "decision_ids") &&
|
|
63
|
+
(!Array.isArray(part.decision_ids) ||
|
|
64
|
+
part.decision_ids.length > 100 ||
|
|
65
|
+
part.decision_ids.some((id) => typeof id !== "string" || !/^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$/u.test(id)) ||
|
|
66
|
+
new Set(part.decision_ids).size !== part.decision_ids.length))
|
|
67
|
+
invalid("Semantic decision ids must be unique bounded identifiers.");
|
|
47
68
|
return Object.freeze({
|
|
48
69
|
kind: part.kind,
|
|
49
70
|
authoring_task_sha256: part.authoring_task_sha256,
|
|
50
71
|
file: part.file,
|
|
51
72
|
sha256: part.sha256,
|
|
73
|
+
...(Object.hasOwn(part, "decision_ids")
|
|
74
|
+
? { decision_ids: Object.freeze([...part.decision_ids]) }
|
|
75
|
+
: {}),
|
|
52
76
|
});
|
|
53
77
|
});
|
|
54
78
|
if (new Set(submissions.map((part) => part.authoring_task_sha256)).size !== submissions.length)
|
|
@@ -58,6 +82,9 @@ export function parseFoundrySemanticInput(value) {
|
|
|
58
82
|
task_id: data.task_id,
|
|
59
83
|
actor_id: data.actor_id,
|
|
60
84
|
assessment_sha256: data.assessment_sha256,
|
|
85
|
+
...(Object.hasOwn(data, "interaction_sha256")
|
|
86
|
+
? { interaction_sha256: data.interaction_sha256 }
|
|
87
|
+
: {}),
|
|
61
88
|
submissions: Object.freeze(submissions),
|
|
62
89
|
});
|
|
63
90
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FoundrySemanticInput } from "./foundry-semantic-input.ts";
|
|
2
|
+
import type { FoundryInteractionState } from "./foundry-interaction-input.ts";
|
|
3
|
+
export interface SemanticInteractionScope {
|
|
4
|
+
readonly work_item_sha256: string;
|
|
5
|
+
readonly dataset_type: string;
|
|
6
|
+
readonly decision_ids: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
/** Bind a semantic proposal to the latest applicable user choices without treating them as data proof. */
|
|
9
|
+
export declare function verifyFoundrySemanticInteraction(input: FoundrySemanticInput, interaction: {
|
|
10
|
+
readonly sha256: string;
|
|
11
|
+
readonly state: FoundryInteractionState;
|
|
12
|
+
} | null, selectedScopes: ReadonlyMap<string, string>): readonly SemanticInteractionScope[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { FoundryContextError } from "./foundry-runtime-context.js";
|
|
2
|
+
import { currentFoundryDecisionsForType, currentFoundryQuestions, currentFoundryInvestigations, foundryInteractionScopeApplies, } from "./foundry-interaction-projection.js";
|
|
3
|
+
function fail(message) {
|
|
4
|
+
throw new FoundryContextError("semantic_interaction_invalid", message);
|
|
5
|
+
}
|
|
6
|
+
/** Bind a semantic proposal to the latest applicable user choices without treating them as data proof. */
|
|
7
|
+
export function verifyFoundrySemanticInteraction(input, interaction, selectedScopes) {
|
|
8
|
+
if ((input.interaction_sha256 ?? null) !== (interaction?.sha256 ?? null))
|
|
9
|
+
fail("Semantic input must name the current registered interaction state.");
|
|
10
|
+
const result = [];
|
|
11
|
+
for (const part of input.submissions) {
|
|
12
|
+
const type = selectedScopes.get(part.authoring_task_sha256);
|
|
13
|
+
if (!type)
|
|
14
|
+
fail("Semantic work is not in the current assessed scope.");
|
|
15
|
+
const expected = interaction
|
|
16
|
+
? currentFoundryDecisionsForType(interaction.state, type).map((decision) => String(decision.decision_id))
|
|
17
|
+
: [];
|
|
18
|
+
const actual = part.decision_ids ?? [];
|
|
19
|
+
if (expected.length !== actual.length ||
|
|
20
|
+
expected.some((id) => !actual.includes(id)) ||
|
|
21
|
+
actual.some((id) => !expected.includes(id)))
|
|
22
|
+
fail("Semantic work must name exactly the current applicable user decision ids.");
|
|
23
|
+
if (interaction) {
|
|
24
|
+
const unresolved = [
|
|
25
|
+
...currentFoundryQuestions(interaction.state),
|
|
26
|
+
...currentFoundryInvestigations(interaction.state),
|
|
27
|
+
];
|
|
28
|
+
if (unresolved.some((item) => foundryInteractionScopeApplies(item.dataset_type, type)))
|
|
29
|
+
fail("Resolve or investigate the current decision gap before applying dependent semantic work.");
|
|
30
|
+
}
|
|
31
|
+
result.push(Object.freeze({
|
|
32
|
+
work_item_sha256: part.authoring_task_sha256,
|
|
33
|
+
dataset_type: type,
|
|
34
|
+
decision_ids: Object.freeze([...expected]),
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
return Object.freeze(result);
|
|
38
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type FoundryInputFact, type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
|
|
2
|
+
import type { FoundryTaskStartSpec } from "./foundry-task-start-spec.ts";
|
|
3
|
+
export interface FoundrySuccessorLink {
|
|
4
|
+
readonly task_id: string;
|
|
5
|
+
readonly spec: FoundryTaskStartSpec;
|
|
6
|
+
readonly inputs: readonly FoundryInputFact[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The strict reader of one failed native commit report. It never trusts Foundry's own counters: the
|
|
10
|
+
* CLI's machine report must prove that nothing was executed and no CLI attempt was consumed, that
|
|
11
|
+
* every row is a definite failure, and that input, contract and every action are bound to the exact
|
|
12
|
+
* admitted scope. Returns the refusal reason, or null when the report qualifies.
|
|
13
|
+
*/
|
|
14
|
+
export declare function noDispatchCommitReportProblem(value: unknown, expected: {
|
|
15
|
+
readonly candidate_path: string;
|
|
16
|
+
readonly contract_sha256: string;
|
|
17
|
+
readonly execution_id: string;
|
|
18
|
+
readonly actions: readonly {
|
|
19
|
+
readonly action_id: string;
|
|
20
|
+
readonly table: string;
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly version: string;
|
|
23
|
+
readonly desired_sha256: string;
|
|
24
|
+
}[];
|
|
25
|
+
}): string | null;
|
|
26
|
+
/**
|
|
27
|
+
* Admit one successor revision over a predecessor whose only terminal evidence is a CLI failure that
|
|
28
|
+
* provably dispatched nothing. Every fact is re-derived from the predecessor's own hash-chained index
|
|
29
|
+
* on every call; nothing is cached, and the successor may never name one of the predecessor's files.
|
|
30
|
+
*/
|
|
31
|
+
export declare function assertFoundryNoDispatchSuccessor(input: {
|
|
32
|
+
context: FoundryRuntimeContext;
|
|
33
|
+
predecessorTaskId: string;
|
|
34
|
+
predecessorInputs: readonly FoundryInputFact[];
|
|
35
|
+
successor: FoundrySuccessorLink;
|
|
36
|
+
}): void;
|