@tiangong-lca/foundry 0.1.17 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +23 -7
  2. package/docs/architecture.md +15 -3
  3. package/docs/foundry-task-contracts.md +27 -3
  4. package/docs/package-distribution-contract.md +45 -13
  5. package/docs/public-runtime-contract.md +85 -5
  6. package/docs/runtime-context-contract.md +22 -4
  7. package/docs/task-authorization-contract.md +15 -3
  8. package/docs/workspace-migration-contract.md +5 -3
  9. package/package-dist/assets/foundry-package-descriptor.json +541 -177
  10. package/package-dist/scripts/foundry-facade.d.ts +5 -0
  11. package/package-dist/scripts/foundry-facade.js +261 -16
  12. package/package-dist/scripts/foundry-runtime.d.ts +2 -2
  13. package/package-dist/scripts/lib/bundle-sample-utils.d.ts +4 -4
  14. package/package-dist/scripts/lib/bundle-source-context.d.ts +6 -4
  15. package/package-dist/scripts/lib/bundle-source-context.js +10 -8
  16. package/package-dist/scripts/lib/decision-owners/identity-preflight.js +18 -6
  17. package/package-dist/scripts/lib/finalize-owners/cli-wrappers.d.ts +72 -1
  18. package/package-dist/scripts/lib/finalize-owners/cli-wrappers.js +123 -3
  19. package/package-dist/scripts/lib/finalize-owners/post-authoring.d.ts +11 -1
  20. package/package-dist/scripts/lib/finalize-owners/post-authoring.js +65 -9
  21. package/package-dist/scripts/lib/foundry-assessment-producer.d.ts +6 -0
  22. package/package-dist/scripts/lib/foundry-assessment-producer.js +167 -0
  23. package/package-dist/scripts/lib/foundry-authentication-environment.js +1 -1
  24. package/package-dist/scripts/lib/foundry-command-metadata.js +6 -1
  25. package/package-dist/scripts/lib/foundry-curation-queue.d.ts +73 -0
  26. package/package-dist/scripts/lib/foundry-curation-queue.js +261 -0
  27. package/package-dist/scripts/lib/foundry-decision-owners.d.ts +1 -1
  28. package/package-dist/scripts/lib/foundry-decision-owners.js +15 -7
  29. package/package-dist/scripts/lib/foundry-edge-draft-admission.d.ts +17 -0
  30. package/package-dist/scripts/lib/foundry-edge-draft-admission.js +94 -0
  31. package/package-dist/scripts/lib/foundry-edge-identity-preflight.d.ts +47 -0
  32. package/package-dist/scripts/lib/foundry-edge-identity-preflight.js +268 -0
  33. package/package-dist/scripts/lib/foundry-edge-oauth.d.ts +4 -0
  34. package/package-dist/scripts/lib/foundry-edge-oauth.js +154 -0
  35. package/package-dist/scripts/lib/foundry-edge-reference-intent.d.ts +51 -0
  36. package/package-dist/scripts/lib/foundry-edge-reference-intent.js +184 -0
  37. package/package-dist/scripts/lib/foundry-edge-repair-preparation.d.ts +21 -0
  38. package/package-dist/scripts/lib/foundry-edge-repair-preparation.js +120 -0
  39. package/package-dist/scripts/lib/foundry-edge-session.d.ts +15 -0
  40. package/package-dist/scripts/lib/foundry-edge-session.js +69 -0
  41. package/package-dist/scripts/lib/foundry-edge-support-plan.d.ts +69 -0
  42. package/package-dist/scripts/lib/foundry-edge-support-plan.js +245 -0
  43. package/package-dist/scripts/lib/foundry-edge-support-planning.d.ts +115 -0
  44. package/package-dist/scripts/lib/foundry-edge-support-planning.js +259 -0
  45. package/package-dist/scripts/lib/foundry-edge-support.d.ts +19 -0
  46. package/package-dist/scripts/lib/foundry-edge-support.js +188 -0
  47. package/package-dist/scripts/lib/foundry-execution-admission.d.ts +4 -2
  48. package/package-dist/scripts/lib/foundry-execution-admission.js +28 -3
  49. package/package-dist/scripts/lib/foundry-finalize-edge-identity.d.ts +30 -0
  50. package/package-dist/scripts/lib/foundry-finalize-edge-identity.js +102 -0
  51. package/package-dist/scripts/lib/foundry-finalize-owners.d.ts +1 -0
  52. package/package-dist/scripts/lib/foundry-finalize-owners.js +126 -13
  53. package/package-dist/scripts/lib/foundry-identity-stage-input.d.ts +24 -0
  54. package/package-dist/scripts/lib/foundry-identity-stage-input.js +122 -0
  55. package/package-dist/scripts/lib/foundry-identity-stage-state.d.ts +17 -0
  56. package/package-dist/scripts/lib/foundry-identity-stage-state.js +48 -0
  57. package/package-dist/scripts/lib/foundry-installed-entry.d.ts +2 -0
  58. package/package-dist/scripts/lib/foundry-installed-entry.js +173 -0
  59. package/package-dist/scripts/lib/foundry-managed-adoption.d.ts +12 -0
  60. package/package-dist/scripts/lib/foundry-managed-adoption.js +108 -0
  61. package/package-dist/scripts/lib/foundry-managed-authentication.d.ts +4 -0
  62. package/package-dist/scripts/lib/foundry-managed-authentication.js +28 -0
  63. package/package-dist/scripts/lib/foundry-managed-host.js +14 -2
  64. package/package-dist/scripts/lib/foundry-migration-transfer-io.d.ts +2 -0
  65. package/package-dist/scripts/lib/foundry-migration-transfer-io.js +27 -3
  66. package/package-dist/scripts/lib/foundry-operation-result.d.ts +1 -1
  67. package/package-dist/scripts/lib/foundry-operation-result.js +1 -0
  68. package/package-dist/scripts/lib/foundry-owner-execution-store.d.ts +16 -16
  69. package/package-dist/scripts/lib/foundry-owner-execution-store.js +34 -7
  70. package/package-dist/scripts/lib/foundry-owner-readback.d.ts +20 -0
  71. package/package-dist/scripts/lib/foundry-owner-readback.js +42 -0
  72. package/package-dist/scripts/lib/foundry-package-contract.d.ts +4 -4
  73. package/package-dist/scripts/lib/foundry-package-contract.js +10 -2
  74. package/package-dist/scripts/lib/foundry-preparation-cli-validation.d.ts +2 -0
  75. package/package-dist/scripts/lib/foundry-preparation-cli-validation.js +96 -3
  76. package/package-dist/scripts/lib/foundry-process-mass-balance.d.ts +64 -0
  77. package/package-dist/scripts/lib/foundry-process-mass-balance.js +224 -0
  78. package/package-dist/scripts/lib/foundry-process-qa.d.ts +19 -0
  79. package/package-dist/scripts/lib/foundry-process-qa.js +423 -0
  80. package/package-dist/scripts/lib/foundry-quality-review-context.d.ts +3 -0
  81. package/package-dist/scripts/lib/foundry-quality-review-context.js +18 -0
  82. package/package-dist/scripts/lib/foundry-quality-review-input.d.ts +48 -0
  83. package/package-dist/scripts/lib/foundry-quality-review-input.js +284 -0
  84. package/package-dist/scripts/lib/foundry-quality-review-lineage.d.ts +13 -0
  85. package/package-dist/scripts/lib/foundry-quality-review-lineage.js +123 -0
  86. package/package-dist/scripts/lib/foundry-quality-review.d.ts +112 -0
  87. package/package-dist/scripts/lib/foundry-quality-review.js +487 -0
  88. package/package-dist/scripts/lib/foundry-repair-execution.js +13 -2
  89. package/package-dist/scripts/lib/foundry-runtime-environment.d.ts +2 -0
  90. package/package-dist/scripts/lib/foundry-runtime-environment.js +18 -0
  91. package/package-dist/scripts/lib/foundry-runtime-identity.d.ts +1 -0
  92. package/package-dist/scripts/lib/foundry-runtime-identity.js +22 -2
  93. package/package-dist/scripts/lib/foundry-runtime-qualification.js +2 -2
  94. package/package-dist/scripts/lib/foundry-runtime-utils.d.ts +1 -0
  95. package/package-dist/scripts/lib/foundry-runtime-utils.js +1 -1
  96. package/package-dist/scripts/lib/foundry-task-authorization.d.ts +2 -1
  97. package/package-dist/scripts/lib/foundry-task-authorization.js +32 -11
  98. package/package-dist/scripts/lib/foundry-task-registration.js +4 -5
  99. package/package-dist/scripts/lib/foundry-task-runtime-adoption-write.d.ts +28 -0
  100. package/package-dist/scripts/lib/foundry-task-runtime-adoption-write.js +250 -0
  101. package/package-dist/scripts/lib/foundry-task-runtime-adoption.d.ts +60 -0
  102. package/package-dist/scripts/lib/foundry-task-runtime-adoption.js +549 -0
  103. package/package-dist/scripts/lib/foundry-task-store.d.ts +4 -1
  104. package/package-dist/scripts/lib/foundry-task-store.js +113 -4
  105. package/package-dist/scripts/lib/foundry-task-types.d.ts +2 -1
  106. package/package-dist/scripts/lib/foundry-typed-authoring-patch.d.ts +76 -0
  107. package/package-dist/scripts/lib/foundry-typed-authoring-patch.js +237 -0
  108. package/package-dist/scripts/lib/foundry-typed-context.d.ts +11 -0
  109. package/package-dist/scripts/lib/foundry-typed-context.js +79 -0
  110. package/package-dist/scripts/lib/foundry-typed-flow-qa.d.ts +166 -0
  111. package/package-dist/scripts/lib/foundry-typed-flow-qa.js +226 -0
  112. package/package-dist/scripts/lib/foundry-typed-model-write.d.ts +6 -0
  113. package/package-dist/scripts/lib/foundry-typed-model-write.js +59 -0
  114. package/package-dist/scripts/lib/foundry-validation-reference-context.d.ts +8 -0
  115. package/package-dist/scripts/lib/foundry-validation-reference-context.js +115 -0
  116. package/package-dist/scripts/lib/foundry-workflow-assessment.d.ts +1 -0
  117. package/package-dist/scripts/lib/foundry-workflow-assessment.js +165 -34
  118. package/package-dist/scripts/lib/foundry-workflow-authorization.js +99 -5
  119. package/package-dist/scripts/lib/foundry-workflow-child-failure.d.ts +44 -0
  120. package/package-dist/scripts/lib/foundry-workflow-child-failure.js +439 -0
  121. package/package-dist/scripts/lib/foundry-workflow-context.js +92 -22
  122. package/package-dist/scripts/lib/foundry-workflow-execution.js +54 -5
  123. package/package-dist/scripts/lib/foundry-workflow-finalize.js +64 -3
  124. package/package-dist/scripts/lib/foundry-workflow-identity-recovery.d.ts +6 -0
  125. package/package-dist/scripts/lib/foundry-workflow-identity-recovery.js +450 -0
  126. package/package-dist/scripts/lib/foundry-workflow-identity-stage.d.ts +9 -0
  127. package/package-dist/scripts/lib/foundry-workflow-identity-stage.js +717 -0
  128. package/package-dist/scripts/lib/foundry-workflow-identity.js +26 -11
  129. package/package-dist/scripts/lib/foundry-workflow-io.d.ts +7 -1
  130. package/package-dist/scripts/lib/foundry-workflow-io.js +102 -16
  131. package/package-dist/scripts/lib/foundry-workflow-reference-verify.js +102 -29
  132. package/package-dist/scripts/lib/foundry-workflow-repair.js +53 -2
  133. package/package-dist/scripts/lib/foundry-workflow-semantic.js +65 -31
  134. package/package-dist/scripts/lib/foundry-workflow-state.d.ts +4 -0
  135. package/package-dist/scripts/lib/foundry-workflow-state.js +165 -22
  136. package/package-dist/scripts/lib/identity-preflight-artifacts.d.ts +4 -7
  137. package/package-dist/scripts/lib/identity-preflight-artifacts.js +18 -5
  138. package/package-dist/scripts/lib/identity-preflight-proof.d.ts +6 -0
  139. package/package-dist/scripts/lib/identity-preflight-proof.js +92 -13
  140. package/package-dist/scripts/lib/identity-preflight-recovery-proof.d.ts +78 -0
  141. package/package-dist/scripts/lib/identity-preflight-recovery-proof.js +415 -0
  142. package/package-dist/scripts/lib/import-curation/curation-cleanup.d.ts +9 -1
  143. package/package-dist/scripts/lib/import-curation/curation-cleanup.js +39 -2
  144. package/package-dist/scripts/lib/import-curation/curation-gate.d.ts +1 -0
  145. package/package-dist/scripts/lib/import-curation/curation-gate.js +19 -1
  146. package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
  147. package/package-dist/scripts/lib/import-curation/internal/workflow-identity-preflight.js +11 -2
  148. package/package-dist/scripts/lib/import-curation/internal/workflow-reference-closure.js +4 -1
  149. package/package-dist/scripts/package-entry.js +2 -3
  150. package/package-dist/scripts/public-api.d.ts +5 -0
  151. package/package-dist/scripts/public-api.js +2 -0
  152. package/package-dist/scripts/runtime-entry.d.ts +2 -0
  153. package/package-dist/scripts/runtime-entry.js +42 -15
  154. package/package.json +6 -2
  155. package/specs/schemas/foundry-identity-stage-input.schema.json +47 -0
  156. package/specs/schemas/foundry-managed-adoption.schema.json +40 -0
  157. package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
  158. package/specs/schemas/foundry-operation-result.schema.json +1 -0
  159. package/specs/schemas/foundry-package-descriptor.schema.json +6 -2
  160. package/specs/schemas/foundry-quality-review-draft.schema.json +271 -0
  161. package/specs/schemas/foundry-quality-review-input.schema.json +271 -0
  162. package/specs/schemas/runtime-qualification.schema.json +1 -1
@@ -0,0 +1,439 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { FoundryContextError, resolveFoundryOutput, } from "./foundry-runtime-context.js";
5
+ import { bytes, digest, exact, facts, object, readTaskBytes, reference, shaPattern, timestamp, } from "./foundry-task-io.js";
6
+ import { sha256Json } from "./identity-preflight-proof.js";
7
+ export const workflowFailureCommand = "dataset-workflow-failure";
8
+ export const workflowFailureFile = "workflow-child-failure.json";
9
+ const kinds = [
10
+ "spawn_error",
11
+ "signal",
12
+ "exit_code",
13
+ "empty_stdout",
14
+ "malformed_json",
15
+ "invalid_report",
16
+ "stage_rejected",
17
+ ];
18
+ export const workflowFailureArtifactLimit = 64 * 1024;
19
+ export const workflowFailurePreviewLimit = 4096;
20
+ const previewLimit = workflowFailurePreviewLimit;
21
+ const argvPreviewBudget = 16 * 1024;
22
+ const evidenceBudget = 48 * 1024;
23
+ const truncatableFields = [
24
+ "invocation.argv",
25
+ "stdout.preview",
26
+ "stderr.preview",
27
+ "child.error_message",
28
+ "rejection.code",
29
+ "rejection.message",
30
+ "invocation.executable",
31
+ "invocation.cli_path",
32
+ "invocation.cwd",
33
+ ];
34
+ function utf8Prefix(value, budget) {
35
+ let used = 0, end = 0;
36
+ for (const character of value) {
37
+ const length = Buffer.byteLength(character);
38
+ if (used + length > budget)
39
+ break;
40
+ used += length;
41
+ end += character.length;
42
+ }
43
+ return value.slice(0, end);
44
+ }
45
+ function renderPreview(value) {
46
+ // Facts hash the complete stream, while display work never scans its unbounded tail.
47
+ const prefix = utf8Prefix(value, previewLimit * 2);
48
+ const clean = prefix
49
+ .split("")
50
+ .filter((character) => {
51
+ const code = character.charCodeAt(0);
52
+ return (code >= 32 && code !== 127) || [9, 10, 13].includes(code);
53
+ })
54
+ .join("");
55
+ const redact = (text) => text
56
+ .replace(/(?:Bearer|Basic)\s+[A-Za-z0-9._~+/=-]+/giu, "[redacted authorization]")
57
+ .replace(/\beyJ[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]*){0,2}/gu, "[redacted token]")
58
+ .replace(/([a-z][a-z0-9+.-]*:\/\/)[^\s/@]+:[^\s/@]+(?:@|$)/giu, "$1[redacted]@")
59
+ .replace(/(["']?(?:[a-z0-9_]*(?:token|secret|password|passwd|key|cookie|session|credential|auth)[a-z0-9_]*)["']?\s*[:=]\s*)(?:"(?:[^"\\]|\\.)*"|'[^']*'|[^\s,;}]+)/giu, "$1[redacted]");
60
+ const redacted = redact(clean);
61
+ const preview = utf8Prefix(redacted, previewLimit);
62
+ // Cropping can shorten a redaction placeholder; render that bounded suffix consistently.
63
+ const renderedAgain = redact(preview);
64
+ const rendered = utf8Prefix(renderedAgain, previewLimit);
65
+ return {
66
+ preview: rendered,
67
+ truncated: prefix.length < value.length ||
68
+ preview.length < redacted.length ||
69
+ rendered.length < renderedAgain.length,
70
+ };
71
+ }
72
+ function safePreview(value) {
73
+ return renderPreview(value).preview;
74
+ }
75
+ function diagnostic(value) {
76
+ const raw = Buffer.from(value);
77
+ const rendered = renderPreview(value);
78
+ return {
79
+ bytes: raw.length,
80
+ sha256: digest(raw),
81
+ preview: rendered.preview,
82
+ truncated: raw.length > previewLimit || rendered.truncated,
83
+ };
84
+ }
85
+ function previewArgv(argv) {
86
+ const preview = [];
87
+ let cost = 2, truncated = false;
88
+ for (const argument of argv) {
89
+ const rendered = renderPreview(argument);
90
+ const value = rendered.preview;
91
+ // Include escaping, commas/newlines and more indentation than the final receipt uses.
92
+ const nextCost = Buffer.byteLength(JSON.stringify(value)) + 16;
93
+ if (cost + nextCost > argvPreviewBudget) {
94
+ truncated = true;
95
+ break;
96
+ }
97
+ cost += nextCost;
98
+ preview.push(value);
99
+ if (Buffer.byteLength(argument) > previewLimit || rendered.truncated)
100
+ truncated = true;
101
+ }
102
+ return { argv: Object.freeze(preview), argv_count: argv.length, argv_truncated: truncated };
103
+ }
104
+ function boundFailureRecord(record, limit) {
105
+ const failure = object(record.failure), invocation = object(failure.invocation);
106
+ const truncated = new Set(Array.isArray(failure.truncated_fields) ? failure.truncated_fields.map(String) : []);
107
+ const sources = object(failure.preview_sources);
108
+ let content = bytes(record);
109
+ const mark = (field) => {
110
+ truncated.add(field);
111
+ failure.truncated_fields = [...truncated];
112
+ };
113
+ const argv = Array.isArray(invocation.argv) ? [...invocation.argv] : [];
114
+ while (content.length > limit && argv.length) {
115
+ argv.splice(Math.floor(argv.length / 2));
116
+ invocation.argv = [...argv];
117
+ invocation.argv_truncated = true;
118
+ mark("invocation.argv");
119
+ content = bytes(record);
120
+ }
121
+ for (const field of truncatableFields.filter((field) => field !== "invocation.argv")) {
122
+ if (content.length <= limit)
123
+ break;
124
+ const [owner, key] = field.split(".");
125
+ if (failure[owner] === null)
126
+ continue;
127
+ const container = object(failure[owner]), original = container[key];
128
+ if (typeof original !== "string")
129
+ continue;
130
+ if (!["stdout.preview", "stderr.preview"].includes(field) && !Object.hasOwn(sources, field))
131
+ sources[field] = {
132
+ bytes: Buffer.byteLength(original),
133
+ sha256: digest(Buffer.from(original)),
134
+ };
135
+ let budget = Math.floor(Buffer.byteLength(original) / 2);
136
+ do {
137
+ const cropped = utf8Prefix(original, budget);
138
+ container[key] = owner === "invocation" ? cropped : safePreview(cropped);
139
+ mark(field);
140
+ if (owner === "stdout" || owner === "stderr")
141
+ container.truncated = true;
142
+ content = bytes(record);
143
+ budget = Math.floor(budget / 2);
144
+ } while (content.length > limit && container[key] !== "");
145
+ }
146
+ if (content.length > limit)
147
+ throw new FoundryContextError("workflow_failure_limit", "The fixed task failure identity exceeds its bounded protocol.");
148
+ return content;
149
+ }
150
+ /** Bound the complete persisted wrapper, including JSON escaping, indentation and task fields. */
151
+ export function serializeWorkflowFailureResult(result) {
152
+ const record = object(JSON.parse(bytes(result).toString("utf8")));
153
+ return boundFailureRecord(record, workflowFailureArtifactLimit);
154
+ }
155
+ export class WorkflowChildFailure extends FoundryContextError {
156
+ evidence;
157
+ retainedArtifact;
158
+ constructor(evidence) {
159
+ super("workflow_child_failed", "The original local CLI call failed; its task-bound diagnostic receipt is retained and requires a reviewed task revision.");
160
+ this.evidence = evidence;
161
+ }
162
+ }
163
+ export function bindWorkflowChildExecution(context, qualified, cliPath, argv, cwd = context.workspaceRoot, executable = process.execPath) {
164
+ return Object.freeze({
165
+ execution_id: randomUUID(),
166
+ started_at_utc: new Date().toISOString(),
167
+ invocation: Object.freeze({
168
+ executable,
169
+ executable_sha256: createHash("sha256").update(fs.readFileSync(executable)).digest("hex"),
170
+ cli_path: cliPath,
171
+ cli_sha256: createHash("sha256").update(fs.readFileSync(cliPath)).digest("hex"),
172
+ ...previewArgv(argv),
173
+ argv_sha256: sha256Json(argv),
174
+ cwd,
175
+ cwd_relative: path.relative(context.workspaceRoot, cwd),
176
+ cwd_sha256: digest(Buffer.from(cwd)),
177
+ qualification_sha256: qualified.qualification_sha256,
178
+ }),
179
+ });
180
+ }
181
+ export function workflowChildFailure(execution, child, kind, rejection) {
182
+ const error = child.error;
183
+ const evidence = {
184
+ schema: "tiangong-foundry.workflow-child-failure.v1",
185
+ ...execution,
186
+ finished_at_utc: new Date().toISOString(),
187
+ kind,
188
+ truncated_fields: [],
189
+ preview_sources: {},
190
+ rejection: rejection instanceof Error
191
+ ? {
192
+ code: "code" in rejection && typeof rejection.code === "string"
193
+ ? safePreview(rejection.code)
194
+ : null,
195
+ message: safePreview(rejection.message),
196
+ }
197
+ : null,
198
+ child: {
199
+ pid: Number.isSafeInteger(child.pid) ? child.pid : null,
200
+ exit_code: child.status,
201
+ signal: child.signal,
202
+ error_name: error && /^[A-Za-z][A-Za-z0-9]{0,63}$/u.test(error.name) ? error.name : null,
203
+ error_code: error &&
204
+ "code" in error &&
205
+ typeof error.code === "string" &&
206
+ /^[A-Z0-9_]{1,64}$/u.test(error.code)
207
+ ? error.code
208
+ : null,
209
+ error_message: error ? safePreview(error.message) : null,
210
+ },
211
+ stdout: diagnostic(child.stdout ?? ""),
212
+ stderr: diagnostic(child.stderr ?? ""),
213
+ };
214
+ const sources = evidence.preview_sources;
215
+ const note = (field, value) => {
216
+ if (Buffer.byteLength(value) > previewLimit || renderPreview(value).truncated) {
217
+ evidence.truncated_fields.push(field);
218
+ sources[field] = { bytes: Buffer.byteLength(value), sha256: digest(Buffer.from(value)) };
219
+ }
220
+ };
221
+ if (execution.invocation.argv_truncated)
222
+ evidence.truncated_fields.push("invocation.argv");
223
+ if (error)
224
+ note("child.error_message", error.message);
225
+ if (rejection instanceof Error) {
226
+ note("rejection.message", rejection.message);
227
+ if ("code" in rejection && typeof rejection.code === "string")
228
+ note("rejection.code", rejection.code);
229
+ }
230
+ // Reserve space for the actual task wrapper; the writer additionally measures that wrapper.
231
+ const bounded = boundFailureRecord(object(JSON.parse(bytes({ failure: evidence }).toString("utf8"))), evidenceBudget);
232
+ return new WorkflowChildFailure(object(object(JSON.parse(bounded.toString("utf8"))).failure));
233
+ }
234
+ /** Read-only proof of a failed local operation. Failure artifacts never enter producer lineage. */
235
+ export function readWorkflowChildFailure(context, entries) {
236
+ const entry = entries.findLast((candidate) => candidate.command === workflowFailureCommand);
237
+ if (!entry)
238
+ return null;
239
+ const invalid = () => {
240
+ throw new FoundryContextError("workflow_failure_receipt_invalid", "The original failed operation evidence is missing, changed or inconsistent; do not rerun it.");
241
+ };
242
+ try {
243
+ if (entry.path !== `evidence/failures/${entry.operation_id}/${workflowFailureFile}` ||
244
+ entry.receipt.path !== `checkpoints/${entry.operation_id}.json`)
245
+ invalid();
246
+ const resultBytes = readTaskBytes(context, entry.path, workflowFailureArtifactLimit);
247
+ if (resultBytes.length !== entry.bytes || digest(resultBytes) !== entry.sha256)
248
+ invalid();
249
+ const result = object(JSON.parse(resultBytes.toString("utf8")));
250
+ exact(result, [
251
+ "schema",
252
+ "status",
253
+ "task_id",
254
+ "operation_id",
255
+ "command",
256
+ "input_scope_sha256",
257
+ "failure",
258
+ ]);
259
+ if (result.schema !== "tiangong-foundry.workflow-operation-failure.v1" ||
260
+ result.status !== "failed" ||
261
+ result.task_id !== context.taskId ||
262
+ result.operation_id !== entry.operation_id ||
263
+ result.input_scope_sha256 !== entry.input_scope_sha256 ||
264
+ typeof result.command !== "string")
265
+ invalid();
266
+ const receiptBytes = readTaskBytes(context, entry.receipt.path);
267
+ if (digest(receiptBytes) !== entry.receipt.sha256)
268
+ invalid();
269
+ const receipt = object(JSON.parse(receiptBytes.toString("utf8")));
270
+ exact(receipt, ["schema", "mode", "status", "plan", "job_sha256", "outputs", "result"]);
271
+ const output = facts(receipt.outputs);
272
+ if (receipt.schema !== "tiangong-foundry.operation-receipt.v1" ||
273
+ receipt.mode !== "deterministic-local" ||
274
+ receipt.status !== "failed" ||
275
+ output.length !== 1 ||
276
+ output[0].path !== entry.path ||
277
+ output[0].sha256 !== entry.sha256 ||
278
+ output[0].bytes !== entry.bytes ||
279
+ sha256Json(receipt.result) !== sha256Json({ path: entry.path, sha256: entry.sha256 }))
280
+ invalid();
281
+ const planRef = reference(receipt.plan), planBytes = readTaskBytes(context, planRef.path);
282
+ if (planRef.path !== `checkpoints/${entry.operation_id}.plan.json` ||
283
+ digest(planBytes) !== planRef.sha256)
284
+ invalid();
285
+ const plan = object(JSON.parse(planBytes.toString("utf8")));
286
+ exact(plan, [
287
+ "schema",
288
+ "operation_id",
289
+ "job_sha256",
290
+ "command",
291
+ "options_sha256",
292
+ "input_scope_sha256",
293
+ "inputs",
294
+ "created_at_utc",
295
+ ]);
296
+ const inputs = facts(plan.inputs);
297
+ const job = digest(readTaskBytes(context, "foundry-job.json"));
298
+ if (plan.schema !== "tiangong-foundry.operation-plan.v1" ||
299
+ plan.operation_id !== entry.operation_id ||
300
+ plan.command !== result.command ||
301
+ plan.job_sha256 !== job ||
302
+ receipt.job_sha256 !== job ||
303
+ sha256Json(inputs) !== entry.input_scope_sha256 ||
304
+ plan.input_scope_sha256 !== entry.input_scope_sha256 ||
305
+ !timestamp(plan.created_at_utc) ||
306
+ sha256Json({
307
+ job,
308
+ command: plan.command,
309
+ input_scope: entry.input_scope_sha256,
310
+ options: plan.options_sha256,
311
+ }) !== entry.operation_id)
312
+ invalid();
313
+ const failure = object(result.failure);
314
+ exact(failure, [
315
+ "schema",
316
+ "execution_id",
317
+ "started_at_utc",
318
+ "finished_at_utc",
319
+ "kind",
320
+ "truncated_fields",
321
+ "preview_sources",
322
+ "rejection",
323
+ "invocation",
324
+ "child",
325
+ "stdout",
326
+ "stderr",
327
+ ]);
328
+ if (failure.rejection !== null) {
329
+ const rejection = object(failure.rejection);
330
+ exact(rejection, ["code", "message"]);
331
+ if (!(rejection.code === null ||
332
+ (typeof rejection.code === "string" &&
333
+ Buffer.byteLength(rejection.code) <= previewLimit &&
334
+ safePreview(rejection.code) === rejection.code)) ||
335
+ typeof rejection.message !== "string" ||
336
+ Buffer.byteLength(rejection.message) > previewLimit ||
337
+ safePreview(rejection.message) !== rejection.message)
338
+ invalid();
339
+ }
340
+ const invocation = object(failure.invocation), child = object(failure.child);
341
+ exact(invocation, [
342
+ "executable",
343
+ "executable_sha256",
344
+ "cli_path",
345
+ "cli_sha256",
346
+ "argv",
347
+ "argv_count",
348
+ "argv_truncated",
349
+ "argv_sha256",
350
+ "cwd",
351
+ "cwd_relative",
352
+ "cwd_sha256",
353
+ "qualification_sha256",
354
+ ]);
355
+ exact(child, ["pid", "exit_code", "signal", "error_name", "error_code", "error_message"]);
356
+ if (!Array.isArray(failure.truncated_fields) ||
357
+ failure.truncated_fields.some((field) => typeof field !== "string" || !truncatableFields.includes(field)))
358
+ invalid();
359
+ const truncatedFields = Array.isArray(failure.truncated_fields)
360
+ ? failure.truncated_fields
361
+ : invalid();
362
+ const sources = object(failure.preview_sources);
363
+ for (const [field, value] of Object.entries(sources)) {
364
+ if (!truncatableFields.includes(field))
365
+ invalid();
366
+ const source = object(value);
367
+ exact(source, ["bytes", "sha256"]);
368
+ if (!Number.isSafeInteger(source.bytes) ||
369
+ Number(source.bytes) < 0 ||
370
+ typeof source.sha256 !== "string" ||
371
+ !shaPattern.test(source.sha256))
372
+ invalid();
373
+ }
374
+ const cwdRelative = typeof invocation.cwd_relative === "string" ? invocation.cwd_relative : invalid();
375
+ if (typeof invocation.cwd !== "string" ||
376
+ typeof invocation.cwd_sha256 !== "string" ||
377
+ !shaPattern.test(invocation.cwd_sha256))
378
+ invalid();
379
+ const cwd = path.resolve(context.workspaceRoot, cwdRelative);
380
+ if (digest(Buffer.from(cwd)) !== invocation.cwd_sha256 ||
381
+ (!truncatedFields.includes("invocation.cwd") && invocation.cwd !== cwd))
382
+ invalid();
383
+ if (cwd !== context.workspaceRoot) {
384
+ const relative = path.relative(context.tempRoot, resolveFoundryOutput(context, cwd));
385
+ if (!relative ||
386
+ relative === ".." ||
387
+ relative.startsWith(`..${path.sep}`) ||
388
+ path.isAbsolute(relative))
389
+ invalid();
390
+ }
391
+ if (failure.schema !== "tiangong-foundry.workflow-child-failure.v1" ||
392
+ typeof failure.kind !== "string" ||
393
+ !kinds.includes(failure.kind) ||
394
+ typeof failure.execution_id !== "string" ||
395
+ !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u.test(failure.execution_id) ||
396
+ !timestamp(failure.started_at_utc) ||
397
+ !timestamp(failure.finished_at_utc) ||
398
+ !Number.isSafeInteger(invocation.argv_count) ||
399
+ Number(invocation.argv_count) < 0 ||
400
+ typeof invocation.argv_truncated !== "boolean" ||
401
+ !Array.isArray(invocation.argv) ||
402
+ Number(invocation.argv_count) < invocation.argv.length ||
403
+ (Number(invocation.argv_count) > invocation.argv.length && !invocation.argv_truncated) ||
404
+ invocation.argv.some((arg) => typeof arg !== "string" || Buffer.byteLength(arg) > previewLimit) ||
405
+ ![child.signal, child.error_name, child.error_code, child.error_message].every((value) => value === null ||
406
+ (typeof value === "string" &&
407
+ Buffer.byteLength(value) <= previewLimit &&
408
+ safePreview(value) === value)) ||
409
+ (failure.kind === "signal" && child.signal === null) ||
410
+ (failure.kind === "spawn_error" && child.error_message === null) ||
411
+ typeof invocation.cli_path !== "string" ||
412
+ typeof invocation.executable !== "string" ||
413
+ ![
414
+ invocation.argv_sha256,
415
+ invocation.cli_sha256,
416
+ invocation.executable_sha256,
417
+ invocation.qualification_sha256,
418
+ ].every((hash) => typeof hash === "string" && shaPattern.test(hash)) ||
419
+ ![child.pid, child.exit_code].every((value) => value === null || Number.isSafeInteger(value)))
420
+ invalid();
421
+ for (const name of ["stdout", "stderr"]) {
422
+ const detail = object(failure[name]);
423
+ exact(detail, ["bytes", "sha256", "preview", "truncated"]);
424
+ if (!Number.isSafeInteger(detail.bytes) ||
425
+ Number(detail.bytes) < 0 ||
426
+ typeof detail.sha256 !== "string" ||
427
+ !shaPattern.test(detail.sha256) ||
428
+ typeof detail.preview !== "string" ||
429
+ Buffer.byteLength(detail.preview) > previewLimit ||
430
+ safePreview(detail.preview) !== detail.preview ||
431
+ typeof detail.truncated !== "boolean")
432
+ invalid();
433
+ }
434
+ return { entry, result, failure };
435
+ }
436
+ catch {
437
+ return invalid();
438
+ }
439
+ }
@@ -3,11 +3,14 @@ import path from "node:path";
3
3
  import { spawnSync } from "node:child_process";
4
4
  import { FoundryContextError, readFoundryInput, resolveFoundryOutput, } from "./foundry-runtime-context.js";
5
5
  import { assertQualifiedFoundryRuntime, } from "./foundry-runtime-qualification.js";
6
- import { createFoundryIsolatedChildEnvironment } from "./foundry-runtime-environment.js";
6
+ import { withFoundryIsolatedLocalCliEnvironment } from "./foundry-runtime-environment.js";
7
7
  import { resolveInstalledTiangongLcaCliPackage } from "./foundry-runtime-utils.js";
8
8
  import { runFoundryTaskOperation } from "./foundry-task-store.js";
9
9
  import { supportedDatasetTypes } from "./import-curation/internal/dataset-types.js";
10
+ import { bindWorkflowChildExecution, workflowChildFailure, } from "./foundry-workflow-child-failure.js";
10
11
  import { createWorkflowDirectory } from "./foundry-workflow-io.js";
12
+ import { readFoundryTaskRuntimeAdoption } from "./foundry-task-runtime-adoption.js";
13
+ import { createTypedContextCommand, projectTypedContext } from "./foundry-typed-context.js";
11
14
  export async function prepareFoundryWorkflowContext(context, qualified, selectedTypes) {
12
15
  assertQualifiedFoundryRuntime(context, qualified);
13
16
  if (!selectedTypes.length || selectedTypes.some((type) => !supportedDatasetTypes.has(type)))
@@ -15,7 +18,16 @@ export async function prepareFoundryWorkflowContext(context, qualified, selected
15
18
  const types = [
16
19
  ...new Set(selectedTypes.flatMap((type) => type === "support" ? ["contact", "source", "flowproperty", "unitgroup"] : [type])),
17
20
  ].sort();
18
- return runFoundryTaskOperation(context, { command: "dataset-context-pack", options: { types, profile: "ai-import" } }, (operation) => {
21
+ const typed = qualified.cli.expectation.package_version === "0.4.0";
22
+ const adoption = readFoundryTaskRuntimeAdoption(context);
23
+ return runFoundryTaskOperation(context, {
24
+ command: "dataset-context-pack",
25
+ options: {
26
+ types,
27
+ profile: typed ? "default" : "ai-import",
28
+ ...(adoption ? { runtime_adoption_plan_sha256: adoption.plan_sha256 } : {}),
29
+ },
30
+ }, (operation) => {
19
31
  assertQualifiedFoundryRuntime(context, qualified);
20
32
  for (const input of context.inputs)
21
33
  readFoundryInput(context, input.path);
@@ -29,39 +41,97 @@ export async function prepareFoundryWorkflowContext(context, qualified, selected
29
41
  const temporary = createWorkflowDirectory(context, path.join(context.tempRoot, "context-"));
30
42
  try {
31
43
  const cli = resolveInstalledTiangongLcaCliPackage();
32
- const environment = createFoundryIsolatedChildEnvironment({ tempRoot: temporary });
33
44
  const reports = [];
34
45
  for (const type of types) {
35
46
  assertQualifiedFoundryRuntime(context, qualified);
36
47
  const destination = path.join(output, type);
37
- const child = spawnSync(process.execPath, [
38
- cli.binPath,
39
- "dataset",
40
- "context-pack",
41
- "--type",
42
- type,
43
- "--profile",
44
- "ai-import",
45
- "--out-dir",
46
- destination,
47
- "--json",
48
- ], {
48
+ const outputDirectory = path.join(destination, "outputs");
49
+ const reportPath = path.join(outputDirectory, "contract-report.json");
50
+ const resultPath = path.join(outputDirectory, "contract-result.json");
51
+ if (typed)
52
+ fs.mkdirSync(outputDirectory, { recursive: true, mode: 0o700 });
53
+ const argv = typed
54
+ ? createTypedContextCommand(type, resultPath)
55
+ : [
56
+ "dataset",
57
+ "context-pack",
58
+ "--type",
59
+ type,
60
+ "--profile",
61
+ "ai-import",
62
+ "--out-dir",
63
+ destination,
64
+ "--json",
65
+ ];
66
+ const execution = bindWorkflowChildExecution(context, qualified, cli.binPath, argv);
67
+ const child = withFoundryIsolatedLocalCliEnvironment(temporary, (env) => spawnSync(process.execPath, [cli.binPath, ...argv], {
49
68
  cwd: context.workspaceRoot,
50
- env: environment,
69
+ env,
51
70
  encoding: "utf8",
52
71
  shell: false,
53
72
  maxBuffer: 8 * 1024 * 1024,
54
73
  timeout: 120_000,
55
- });
56
- if (child.error || child.signal || child.status !== 0 || child.stderr)
57
- throw new FoundryContextError("workflow_context_failed", `CLI context preparation failed for ${type}.`);
58
- const value = JSON.parse(child.stdout);
74
+ }));
75
+ const kind = child.error
76
+ ? "spawn_error"
77
+ : child.signal
78
+ ? "signal"
79
+ : child.status !== 0
80
+ ? "exit_code"
81
+ : !child.stdout.trim()
82
+ ? "empty_stdout"
83
+ : child.stderr
84
+ ? "stage_rejected"
85
+ : null;
86
+ if (kind)
87
+ throw workflowChildFailure(execution, child, kind);
88
+ let value;
89
+ try {
90
+ value = JSON.parse(child.stdout);
91
+ }
92
+ catch {
93
+ throw workflowChildFailure(execution, child, "malformed_json");
94
+ }
59
95
  if (!value ||
60
96
  typeof value !== "object" ||
97
+ Array.isArray(value) ||
61
98
  !("status" in value) ||
62
99
  value.status !== "completed")
63
- throw new FoundryContextError("workflow_context_invalid", "CLI returned no completed context pack.");
64
- reports.push({ type, report: path.join(destination, "outputs/contract-report.json") });
100
+ throw workflowChildFailure(execution, child, "invalid_report");
101
+ if (typed) {
102
+ let projected;
103
+ try {
104
+ projected = projectTypedContext(type, resultPath, value, fs.readFileSync(resultPath));
105
+ }
106
+ catch (error) {
107
+ throw workflowChildFailure(execution, child, "invalid_report", error);
108
+ }
109
+ const manifestPath = path.join(outputDirectory, "contract-manifest.json");
110
+ const files = {
111
+ manifest: manifestPath,
112
+ result: resultPath,
113
+ report: reportPath,
114
+ };
115
+ for (const { key, name, text } of projected.files) {
116
+ const file = path.join(outputDirectory, name);
117
+ fs.writeFileSync(file, text, { flag: "wx", mode: 0o600 });
118
+ files[key] = file;
119
+ }
120
+ fs.writeFileSync(manifestPath, JSON.stringify(projected.manifest, null, 2), {
121
+ flag: "wx",
122
+ mode: 0o600,
123
+ });
124
+ fs.writeFileSync(reportPath, JSON.stringify({
125
+ schema_version: 1,
126
+ status: "completed",
127
+ type,
128
+ profile: projected.profile,
129
+ source: projected.source,
130
+ manifest: projected.manifest,
131
+ files,
132
+ }), { flag: "wx", mode: 0o600 });
133
+ }
134
+ reports.push({ type, report: reportPath });
65
135
  }
66
136
  const files = [];
67
137
  const register = (directory) => {