@tiangong-lca/foundry 0.1.16 → 0.1.18

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 (114) hide show
  1. package/README.md +23 -7
  2. package/docs/architecture.md +19 -5
  3. package/docs/foundry-task-contracts.md +27 -3
  4. package/docs/package-distribution-contract.md +47 -15
  5. package/docs/public-runtime-contract.md +77 -5
  6. package/docs/runtime-context-contract.md +21 -5
  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 +351 -137
  10. package/package-dist/scripts/foundry-facade.d.ts +5 -0
  11. package/package-dist/scripts/foundry-facade.js +258 -16
  12. package/package-dist/scripts/foundry-runtime.d.ts +1 -1
  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 +16 -6
  17. package/package-dist/scripts/lib/finalize-owners/post-authoring.d.ts +2 -1
  18. package/package-dist/scripts/lib/finalize-owners/post-authoring.js +28 -1
  19. package/package-dist/scripts/lib/foundry-assessment-producer.d.ts +6 -0
  20. package/package-dist/scripts/lib/foundry-assessment-producer.js +167 -0
  21. package/package-dist/scripts/lib/foundry-command-metadata.js +6 -1
  22. package/package-dist/scripts/lib/foundry-decision-owners.d.ts +1 -1
  23. package/package-dist/scripts/lib/foundry-decision-owners.js +15 -7
  24. package/package-dist/scripts/lib/foundry-execution-admission.js +4 -1
  25. package/package-dist/scripts/lib/foundry-finalize-owners.js +64 -17
  26. package/package-dist/scripts/lib/foundry-identity-stage-input.d.ts +24 -0
  27. package/package-dist/scripts/lib/foundry-identity-stage-input.js +122 -0
  28. package/package-dist/scripts/lib/foundry-identity-stage-state.d.ts +17 -0
  29. package/package-dist/scripts/lib/foundry-identity-stage-state.js +48 -0
  30. package/package-dist/scripts/lib/foundry-installed-entry.d.ts +2 -0
  31. package/package-dist/scripts/lib/foundry-installed-entry.js +173 -0
  32. package/package-dist/scripts/lib/foundry-managed-adoption.d.ts +12 -0
  33. package/package-dist/scripts/lib/foundry-managed-adoption.js +108 -0
  34. package/package-dist/scripts/lib/foundry-managed-authentication.d.ts +4 -0
  35. package/package-dist/scripts/lib/foundry-managed-authentication.js +28 -0
  36. package/package-dist/scripts/lib/foundry-managed-host.js +14 -2
  37. package/package-dist/scripts/lib/foundry-migration-transfer-io.d.ts +2 -0
  38. package/package-dist/scripts/lib/foundry-migration-transfer-io.js +27 -3
  39. package/package-dist/scripts/lib/foundry-operation-result.d.ts +1 -1
  40. package/package-dist/scripts/lib/foundry-operation-result.js +1 -0
  41. package/package-dist/scripts/lib/foundry-package-contract.d.ts +4 -4
  42. package/package-dist/scripts/lib/foundry-package-contract.js +10 -2
  43. package/package-dist/scripts/lib/foundry-preparation-cli-validation.d.ts +22 -0
  44. package/package-dist/scripts/lib/foundry-preparation-cli-validation.js +213 -0
  45. package/package-dist/scripts/lib/foundry-quality-review-context.d.ts +3 -0
  46. package/package-dist/scripts/lib/foundry-quality-review-context.js +18 -0
  47. package/package-dist/scripts/lib/foundry-quality-review-input.d.ts +48 -0
  48. package/package-dist/scripts/lib/foundry-quality-review-input.js +284 -0
  49. package/package-dist/scripts/lib/foundry-quality-review-lineage.d.ts +13 -0
  50. package/package-dist/scripts/lib/foundry-quality-review-lineage.js +123 -0
  51. package/package-dist/scripts/lib/foundry-quality-review.d.ts +112 -0
  52. package/package-dist/scripts/lib/foundry-quality-review.js +487 -0
  53. package/package-dist/scripts/lib/foundry-runtime-identity.js +3 -1
  54. package/package-dist/scripts/lib/foundry-runtime-qualification.js +2 -2
  55. package/package-dist/scripts/lib/foundry-runtime-utils.js +1 -1
  56. package/package-dist/scripts/lib/foundry-task-authorization.d.ts +2 -1
  57. package/package-dist/scripts/lib/foundry-task-authorization.js +32 -11
  58. package/package-dist/scripts/lib/foundry-task-registration.js +4 -5
  59. package/package-dist/scripts/lib/foundry-task-runtime-adoption-write.d.ts +28 -0
  60. package/package-dist/scripts/lib/foundry-task-runtime-adoption-write.js +250 -0
  61. package/package-dist/scripts/lib/foundry-task-runtime-adoption.d.ts +60 -0
  62. package/package-dist/scripts/lib/foundry-task-runtime-adoption.js +549 -0
  63. package/package-dist/scripts/lib/foundry-task-store.d.ts +4 -1
  64. package/package-dist/scripts/lib/foundry-task-store.js +113 -4
  65. package/package-dist/scripts/lib/foundry-task-types.d.ts +2 -1
  66. package/package-dist/scripts/lib/foundry-validation-reference-context.d.ts +8 -0
  67. package/package-dist/scripts/lib/foundry-validation-reference-context.js +115 -0
  68. package/package-dist/scripts/lib/foundry-workflow-assessment.d.ts +1 -0
  69. package/package-dist/scripts/lib/foundry-workflow-assessment.js +149 -18
  70. package/package-dist/scripts/lib/foundry-workflow-authorization.js +17 -4
  71. package/package-dist/scripts/lib/foundry-workflow-child-failure.d.ts +44 -0
  72. package/package-dist/scripts/lib/foundry-workflow-child-failure.js +439 -0
  73. package/package-dist/scripts/lib/foundry-workflow-context.js +37 -8
  74. package/package-dist/scripts/lib/foundry-workflow-finalize.js +37 -0
  75. package/package-dist/scripts/lib/foundry-workflow-identity-recovery.d.ts +6 -0
  76. package/package-dist/scripts/lib/foundry-workflow-identity-recovery.js +450 -0
  77. package/package-dist/scripts/lib/foundry-workflow-identity-stage.d.ts +9 -0
  78. package/package-dist/scripts/lib/foundry-workflow-identity-stage.js +676 -0
  79. package/package-dist/scripts/lib/foundry-workflow-identity.js +10 -1
  80. package/package-dist/scripts/lib/foundry-workflow-io.d.ts +7 -1
  81. package/package-dist/scripts/lib/foundry-workflow-io.js +79 -13
  82. package/package-dist/scripts/lib/foundry-workflow-semantic.js +27 -13
  83. package/package-dist/scripts/lib/foundry-workflow-state.d.ts +4 -0
  84. package/package-dist/scripts/lib/foundry-workflow-state.js +175 -22
  85. package/package-dist/scripts/lib/identity-preflight-artifacts.d.ts +4 -7
  86. package/package-dist/scripts/lib/identity-preflight-artifacts.js +18 -5
  87. package/package-dist/scripts/lib/identity-preflight-proof.d.ts +5 -0
  88. package/package-dist/scripts/lib/identity-preflight-proof.js +73 -1
  89. package/package-dist/scripts/lib/identity-preflight-recovery-proof.d.ts +78 -0
  90. package/package-dist/scripts/lib/identity-preflight-recovery-proof.js +415 -0
  91. package/package-dist/scripts/lib/import-curation/curation-cleanup.d.ts +9 -1
  92. package/package-dist/scripts/lib/import-curation/curation-cleanup.js +39 -2
  93. package/package-dist/scripts/lib/import-curation/curation-gate.d.ts +6 -1
  94. package/package-dist/scripts/lib/import-curation/curation-gate.js +48 -2
  95. package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
  96. package/package-dist/scripts/lib/import-curation/internal/workflow-identity-preflight.js +11 -2
  97. package/package-dist/scripts/lib/import-curation/internal/workflow-reference-closure.js +4 -1
  98. package/package-dist/scripts/lib/tidas-adapter.js +38 -15
  99. package/package-dist/scripts/lib/tidas-process-flow-context.d.ts +12 -0
  100. package/package-dist/scripts/lib/tidas-process-flow-context.js +96 -0
  101. package/package-dist/scripts/package-entry.js +2 -3
  102. package/package-dist/scripts/public-api.d.ts +5 -0
  103. package/package-dist/scripts/public-api.js +2 -0
  104. package/package-dist/scripts/runtime-entry.d.ts +2 -0
  105. package/package-dist/scripts/runtime-entry.js +42 -15
  106. package/package.json +6 -2
  107. package/specs/schemas/foundry-identity-stage-input.schema.json +47 -0
  108. package/specs/schemas/foundry-managed-adoption.schema.json +40 -0
  109. package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
  110. package/specs/schemas/foundry-operation-result.schema.json +1 -0
  111. package/specs/schemas/foundry-package-descriptor.schema.json +6 -2
  112. package/specs/schemas/foundry-quality-review-draft.schema.json +271 -0
  113. package/specs/schemas/foundry-quality-review-input.schema.json +271 -0
  114. package/specs/schemas/runtime-qualification.schema.json +1 -1
@@ -0,0 +1,676 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { withBatchRunLock } from "@tiangong-lca/cli/batch";
5
+ import { assertCliRuntimeMatches } from "@tiangong-lca/cli/runtime";
6
+ import { captureFoundryInput, FoundryContextError, readFoundryInput, resolveFoundryOutput, } from "./foundry-runtime-context.js";
7
+ import { assertQualifiedFoundryRuntime, } from "./foundry-runtime-qualification.js";
8
+ import { assertVerifiedFoundryIdentity, verifyFoundryRuntimeIdentity, } from "./foundry-runtime-identity.js";
9
+ import { createFoundryAuthenticationEnvironment } from "./foundry-authentication-environment.js";
10
+ import { createFoundryIdentityOwners } from "./foundry-identity-owners.js";
11
+ import { inspectOwnerExecutions } from "./foundry-owner-execution-store.js";
12
+ import { currentFoundryInteractionState, currentFoundryQuestions, currentFoundryInvestigations, } from "./foundry-interaction-input.js";
13
+ import { readSelectedSemanticBytes } from "./foundry-semantic-input.js";
14
+ import { assertSelectedFoundryIdentityStageInput, } from "./foundry-identity-stage-input.js";
15
+ import { runFoundryTaskOperation, withFoundryTaskMetadata } from "./foundry-task-store.js";
16
+ import { registerWorkflowStageFiles } from "./foundry-workflow-io.js";
17
+ import { currentWorkflowState, readWorkflowArtifact, workflowObject, } from "./foundry-workflow-state.js";
18
+ import { resolveInstalledTiangongLcaCliPackage } from "./foundry-runtime-utils.js";
19
+ import { datasetIdentity } from "./import-curation/internal/dataset-payload.js";
20
+ import { readRows } from "./import-curation/internal/runtime-io.js";
21
+ import { createFileArtifactFact, createFoundryCommandSpec } from "./foundry-command-spec.js";
22
+ import { createIdentityPreflightBinding, parseFreshIntentBoundAuthReceipt, sha256Json, sha256Text, stableJson, validateBoundExecutionManifest, validateIdentityPreflightExecution, } from "./identity-preflight-proof.js";
23
+ const prepareCommand = "dataset-workflow-identity-stage-prepare";
24
+ const dispatchCommand = "dataset-workflow-identity-stage-dispatch";
25
+ const sensitiveCommands = new Set([
26
+ "dataset-workflow-finalize",
27
+ "dataset-workflow-authorization",
28
+ "dataset-workflow-native-contract",
29
+ "dataset-workflow-execution-prepare",
30
+ "dataset-workflow-execution-result",
31
+ "dataset-workflow-execution-consume",
32
+ "dataset-workflow-execution-observation",
33
+ ]);
34
+ function reject(reason) {
35
+ throw new FoundryContextError("identity_stage_input_invalid", `Explicit read-only identity stage cannot be admitted (${reason}).`);
36
+ }
37
+ function key(target) {
38
+ return JSON.stringify([target.dataset_type, target.dataset_id, target.dataset_version]);
39
+ }
40
+ function readJson(file) {
41
+ const fact = captureFoundryInput(file);
42
+ return workflowObject(JSON.parse(readSelectedSemanticBytes(fact).toString("utf8")));
43
+ }
44
+ function jsonLines(file) {
45
+ const text = readSelectedSemanticBytes(captureFoundryInput(file)).toString("utf8");
46
+ if (text && !text.endsWith("\n"))
47
+ reject("incomplete-index");
48
+ return text
49
+ .split("\n")
50
+ .filter(Boolean)
51
+ .map((line) => workflowObject(JSON.parse(line)));
52
+ }
53
+ function selectedBytes(selected) {
54
+ const content = readSelectedSemanticBytes(selected.descriptor);
55
+ if (stableJson(JSON.parse(content.toString("utf8"))) !== stableJson(selected.value))
56
+ reject("descriptor-binding");
57
+ return content;
58
+ }
59
+ function assertNativeUnprepared(context, entries) {
60
+ // The existing native owner validates all consumed, legacy and UNKNOWN attempt state first.
61
+ const execution = inspectOwnerExecutions(context, entries);
62
+ if (execution.requests.length ||
63
+ execution.consumed.size ||
64
+ execution.pending.length ||
65
+ entries.some((entry) => sensitiveCommands.has(entry.command)) ||
66
+ fs.existsSync(resolveFoundryOutput(context, "authorization.json")))
67
+ reject("native-owner-history-requires-original-recovery");
68
+ }
69
+ /** An explicit new read-only execution. Its local transactions only prepare or capture evidence. */
70
+ export async function runExplicitFoundryIdentityStage(context, qualified, entries, selected, authentication = { mode: "oauth" }, options = {}) {
71
+ options.signal?.throwIfAborted();
72
+ assertQualifiedFoundryRuntime(context, qualified);
73
+ assertSelectedFoundryIdentityStageInput(context, selected);
74
+ selectedBytes(selected);
75
+ const intent = selected.value;
76
+ if (intent.task_id !== context.taskId ||
77
+ intent.actor_id !== context.actorId ||
78
+ !context.accountIntent)
79
+ reject("task-actor-account");
80
+ // Same lock names and sorted order as native dispatch and runtime adoption.
81
+ const scopes = [
82
+ ...new Set([
83
+ ...["flow", "process"].map((type) => sha256Json({ task: context.taskId, type })),
84
+ sha256Json({ task: context.taskId, type: "explicit-identity-stage" }),
85
+ ]),
86
+ ].sort();
87
+ const locked = (at) => at === scopes.length
88
+ ? runLocked(context, qualified, entries, selected, authentication, options.signal)
89
+ : withBatchRunLock({
90
+ runPath: resolveFoundryOutput(context, `owner-locks/${context.taskId}-${scopes[at]}.json`, "state"),
91
+ identity: { task: context.taskId, scope: scopes[at] },
92
+ reason: "Foundry explicit read-only identity stage",
93
+ }, () => locked(at + 1));
94
+ return locked(0);
95
+ }
96
+ async function runLocked(context, qualified, prefix, selected, authentication, signal) {
97
+ const intent = selected.value;
98
+ const intentSha256 = sha256Json(intent);
99
+ const stageId = sha256Json({
100
+ task: context.taskId,
101
+ actor: context.actorId,
102
+ intent_id: intent.intent_id,
103
+ });
104
+ const output = resolveFoundryOutput(context, `outputs/identity-stage/${stageId}`);
105
+ const prepareFile = path.join(output, "prepared", "identity-stage-preparation.json");
106
+ let rowsFile = "";
107
+ let predecessorFile = "";
108
+ let targets = [];
109
+ const validateCurrent = (index) => {
110
+ signal?.throwIfAborted();
111
+ assertQualifiedFoundryRuntime(context, qualified);
112
+ assertSelectedFoundryIdentityStageInput(context, selected);
113
+ selectedBytes(selected);
114
+ for (const fact of context.inputs)
115
+ readFoundryInput(context, fact.path);
116
+ if (index.length < prefix.length ||
117
+ prefix.some((entry, at) => entry.record_sha256 !== index[at].record_sha256))
118
+ reject("retained-index-prefix");
119
+ assertNativeUnprepared(context, index);
120
+ const interaction = currentFoundryInteractionState(context, index);
121
+ if (interaction) {
122
+ const questions = currentFoundryQuestions(interaction.state);
123
+ const investigations = currentFoundryInvestigations(interaction.state);
124
+ if (questions.length || investigations.length)
125
+ throw new FoundryContextError(questions.length ? "interaction_decision_pending" : "interaction_investigation_pending", "Resolve the current question or investigation before identity preflight.");
126
+ }
127
+ const state = currentWorkflowState(context, index), rows = state.rows;
128
+ if (!rows ||
129
+ rows.entry.sha256 !== intent.rows_report_sha256 ||
130
+ (!state.assessment && !state.retainedAssessment))
131
+ reject("current-rows-assessment");
132
+ const predecessor = index.find((entry) => entry.command === "dataset-workflow-identity" &&
133
+ path.basename(entry.path) === "foundry-identity.json" &&
134
+ entry.sha256 === intent.predecessor_identity_sha256);
135
+ if (!predecessor)
136
+ reject("indexed-predecessor");
137
+ const old = readWorkflowArtifact(context, predecessor);
138
+ const predecessorAccount = workflowObject(old.value.account);
139
+ if (old.value.status !== "blocked" ||
140
+ old.value.rows_report !== rows.file ||
141
+ old.value.explicit_new_stage === true ||
142
+ predecessorAccount.project_ref !== context.accountIntent.projectRef ||
143
+ predecessorAccount.user_id !== context.accountIntent.userId)
144
+ reject("blocked-original-predecessor");
145
+ rowsFile = rows.file;
146
+ predecessorFile = old.file;
147
+ const roster = rows.value.sets
148
+ .filter((set) => set.type === "flow" || set.type === "process")
149
+ .flatMap((set) => {
150
+ readFoundryInput(context, set.file);
151
+ const values = readRows(set.file);
152
+ if (values.length !== set.count)
153
+ reject("registered-row-count");
154
+ return values.map((row) => {
155
+ const identity = datasetIdentity(row, 0, set.type);
156
+ return {
157
+ dataset_type: set.type,
158
+ dataset_id: identity.id,
159
+ dataset_version: identity.version,
160
+ source_row_sha256: sha256Json(row),
161
+ source_file: set.file,
162
+ };
163
+ });
164
+ });
165
+ if (!roster.length ||
166
+ new Set(roster.map(key)).size !== roster.length ||
167
+ intent.targets.length !== roster.length ||
168
+ new Set(intent.targets.map(key)).size !== intent.targets.length ||
169
+ intent.targets.some((target) => !roster.some((row) => key(row) === key(target) && row.source_row_sha256 === target.source_row_sha256)))
170
+ reject("exact-current-flow-process-roster");
171
+ targets = roster;
172
+ for (const entry of index.filter((item) => item.command === prepareCommand &&
173
+ path.basename(item.path) === "identity-stage-preparation.json")) {
174
+ const retained = readWorkflowArtifact(context, entry).value;
175
+ if (retained.intent_id === intent.intent_id && retained.intent_sha256 !== intentSha256)
176
+ reject("intent-already-bound");
177
+ if (retained.stage_id === stageId &&
178
+ retained.qualification_sha256 !== qualified.qualification_sha256)
179
+ reject("intent-runtime-changed");
180
+ if (retained.stage_id === stageId && retained.authentication_mode !== authentication.mode)
181
+ reject("intent-authentication-mode-changed");
182
+ if (retained.stage_id !== stageId &&
183
+ retained.rows_report_sha256 === intent.rows_report_sha256 &&
184
+ retained.predecessor_identity_sha256 === intent.predecessor_identity_sha256)
185
+ reject("overlapping-retained-stage");
186
+ }
187
+ if (state.identity?.entry.sha256 !== intent.predecessor_identity_sha256 &&
188
+ state.identity?.value.stage_id !== stageId)
189
+ reject("current-predecessor-changed");
190
+ };
191
+ await withFoundryTaskMetadata(context, (_, index) => validateCurrent(index));
192
+ const environment = createFoundryAuthenticationEnvironment(authentication, context.accountIntent.sessionReference, process.env);
193
+ environment.FOUNDRY_VERIFIED_PROJECT_REF = context.accountIntent.projectRef;
194
+ environment.FOUNDRY_VERIFIED_USER_ID = context.accountIntent.userId;
195
+ fs.mkdirSync(output, { recursive: true, mode: 0o700 });
196
+ try {
197
+ const prepare = await runFoundryTaskOperation(context, {
198
+ command: prepareCommand,
199
+ options: {
200
+ stage_id: stageId,
201
+ intent_sha256: intentSha256,
202
+ rows_report_sha256: intent.rows_report_sha256,
203
+ predecessor_identity_sha256: intent.predecessor_identity_sha256,
204
+ },
205
+ validateCurrent,
206
+ }, (operation) => {
207
+ const identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
208
+ const cli = assertCliRuntimeMatches(qualified.cli.expectation);
209
+ const installed = resolveInstalledTiangongLcaCliPackage();
210
+ const root = path.dirname(prepareFile);
211
+ operation.writeText(path.join(root, "selected-intent.json"), selectedBytes(selected));
212
+ operation.writeJson(path.join(root, "identity-receipt.json"), identity.receipt);
213
+ operation.writeJson(path.join(root, "roster.json"), targets);
214
+ operation.writeJson(path.join(root, "runtime-cli-inventory.json"), {
215
+ qualification: qualified,
216
+ cli: {
217
+ root: installed.packageRoot,
218
+ bin: captureFoundryInput(installed.binPath),
219
+ files: cli.files,
220
+ },
221
+ executable: captureFoundryInput(process.execPath),
222
+ });
223
+ const preparedTargets = [];
224
+ for (const target of targets) {
225
+ const type = target.dataset_type;
226
+ // The published owner names outputs by ID. A separate version-bound parent prevents collisions.
227
+ const outDir = path.join(root, "targets", sha256Json(target));
228
+ const sourceIndex = path.join(outDir, "source-context.jsonl");
229
+ const rowFile = path.join(outDir, "selected-row.jsonl");
230
+ const row = readRows(target.source_file).find((value) => sha256Json(value) === target.source_row_sha256);
231
+ if (!row)
232
+ reject("prepared-source-row");
233
+ operation.writeText(rowFile, JSON.stringify(row) + "\n");
234
+ operation.writeText(sourceIndex, JSON.stringify({ ...target, source_file: rowFile }) + "\n");
235
+ const owner = createFoundryIdentityOwners(context, qualified, type, {
236
+ environment,
237
+ cwd: root,
238
+ });
239
+ const built = workflowObject(owner.invoke(() => owner.preflight.runDatasetIdentityPreflightRequestsBuild({
240
+ type,
241
+ rowsFile: rowFile,
242
+ sourceIndex,
243
+ outDir,
244
+ })));
245
+ const indexFile = resolveFoundryOutput(context, path.resolve(context.assetRoot, String(workflowObject(built.files).identity_preflight_requests)));
246
+ const audited = workflowObject(owner.invoke(() => owner.preflight.runDatasetIdentityPreflightQueryAudit({
247
+ index: indexFile,
248
+ outDir: path.join(outDir, "query-audit"),
249
+ })));
250
+ if (built.status !== "ready" || audited.status !== "passed")
251
+ reject(`request-query-audit:${JSON.stringify({ prepare: built.blockers, audit: audited.blockers })}`);
252
+ const indexRows = jsonLines(indexFile);
253
+ if (indexRows.length !== 1 ||
254
+ key(indexRows[0]) !== key(target))
255
+ reject("prepared-roster-count");
256
+ const admittedRow = {
257
+ ...indexRows[0],
258
+ relevant_input_hashes: {
259
+ registered_rows: captureFoundryInput(target.source_file).sha256,
260
+ source_row: target.source_row_sha256,
261
+ },
262
+ };
263
+ const admittedIndex = path.join(outDir, "admitted-identity-preflight-requests.jsonl");
264
+ operation.writeText(admittedIndex, JSON.stringify(admittedRow) + "\n");
265
+ preparedTargets.push({
266
+ ...target,
267
+ owner_source_file: rowFile,
268
+ index: admittedIndex,
269
+ row: admittedRow,
270
+ });
271
+ }
272
+ registerWorkflowStageFiles(context, operation, root);
273
+ const report = {
274
+ schema: "tiangong-foundry.identity-stage-preparation.v1",
275
+ stage_id: stageId,
276
+ intent_id: intent.intent_id,
277
+ descriptor_sha256: selected.descriptor.sha256,
278
+ intent_sha256: intentSha256,
279
+ task_id: context.taskId,
280
+ actor_id: context.actorId,
281
+ rows_report: rowsFile,
282
+ rows_report_sha256: intent.rows_report_sha256,
283
+ predecessor_identity: {
284
+ path: predecessorFile,
285
+ sha256: intent.predecessor_identity_sha256,
286
+ },
287
+ predecessor_identity_sha256: intent.predecessor_identity_sha256,
288
+ qualification_sha256: qualified.qualification_sha256,
289
+ authentication_mode: identity.mode,
290
+ targets: preparedTargets,
291
+ account: {
292
+ project_ref: context.accountIntent.projectRef,
293
+ user_id: context.accountIntent.userId,
294
+ },
295
+ prepared_at_utc: operation.nowIso(),
296
+ };
297
+ operation.writeJson(prepareFile, report);
298
+ return report;
299
+ });
300
+ const preparedTargets = prepare.targets;
301
+ if (!Array.isArray(preparedTargets) || preparedTargets.length !== targets.length)
302
+ reject("retained-preparation");
303
+ const accepted = [];
304
+ const blockers = [];
305
+ let invocations = 0;
306
+ let thisInvocation = 0;
307
+ for (const target of preparedTargets) {
308
+ signal?.throwIfAborted();
309
+ const targetId = sha256Json({ stage_id: stageId, target });
310
+ const targetRoot = path.join(output, "dispatch", targetId), claimFile = path.join(targetRoot, "dispatch.json");
311
+ const authFile = path.join(targetRoot, "identity-receipt.json"), runDir = path.join(targetRoot, "run");
312
+ let newlyClaimed = false;
313
+ let identity = null;
314
+ const claim = await runFoundryTaskOperation(context, {
315
+ command: dispatchCommand,
316
+ options: { stage_id: stageId, target_id: targetId, intent_sha256: intentSha256 },
317
+ validateCurrent(index) {
318
+ validateCurrent(index);
319
+ // Verify after expensive locked metadata; serialized receipts never extend permission.
320
+ if (!fs.existsSync(claimFile))
321
+ identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
322
+ },
323
+ }, (operation) => {
324
+ if (fs.existsSync(claimFile)) {
325
+ // An orphan file without its completed native receipt cannot prove dispatch admission.
326
+ const orphaned = {
327
+ schema: "tiangong-foundry.identity-stage-orphan-observation.v1",
328
+ stage_id: stageId,
329
+ target_id: targetId,
330
+ target,
331
+ claim: captureFoundryInput(claimFile),
332
+ admission_status: "ORPHANED_CLAIM_UNPROVEN",
333
+ };
334
+ operation.writeJson(path.join(targetRoot, "orphaned-claim.json"), orphaned);
335
+ return orphaned;
336
+ }
337
+ if (!identity)
338
+ reject("fresh-dispatch-identity");
339
+ assertVerifiedFoundryIdentity(context, identity, qualified);
340
+ operation.writeJson(authFile, identity.receipt);
341
+ const value = {
342
+ schema: "tiangong-foundry.identity-stage-dispatch.v1",
343
+ stage_id: stageId,
344
+ target_id: targetId,
345
+ intent_id: intent.intent_id,
346
+ target,
347
+ auth_receipt: captureFoundryInput(authFile),
348
+ qualification_sha256: qualified.qualification_sha256,
349
+ authentication_mode: identity.mode,
350
+ max_attempts: 1,
351
+ dispatch_state: "CLAIMED_OUTCOME_UNPROVEN",
352
+ claimed_at_utc: new Date().toISOString(),
353
+ };
354
+ operation.writeJson(claimFile, value);
355
+ newlyClaimed = true;
356
+ return value;
357
+ });
358
+ if (newlyClaimed) {
359
+ signal?.throwIfAborted();
360
+ if (!identity)
361
+ reject("fresh-dispatch-identity");
362
+ assertVerifiedFoundryIdentity(context, identity, qualified);
363
+ // Keep CLI startup independent of digest paths and the workspace's .env.
364
+ const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "foundry-identity-stage-"));
365
+ try {
366
+ fs.chmodSync(temporary, 0o700);
367
+ const owner = createFoundryIdentityOwners(context, qualified, target.dataset_type, {
368
+ environment,
369
+ cwd: temporary,
370
+ });
371
+ try {
372
+ if (thisInvocation !== null)
373
+ thisInvocation += 1;
374
+ owner.invoke(() => owner.preflight.runDatasetIdentityPreflightRun({
375
+ index: target.index,
376
+ outDir: runDir,
377
+ authReceipt: authFile,
378
+ expectedProjectRef: context.accountIntent.projectRef,
379
+ expectedUserId: context.accountIntent.userId,
380
+ maxAttempts: 1,
381
+ timeoutMs: 60_000,
382
+ authReceiptMaxAgeMs: 60_000,
383
+ }));
384
+ }
385
+ catch {
386
+ // Retained logs may prove the original result; no new invocation follows this claim.
387
+ }
388
+ }
389
+ finally {
390
+ fs.rmSync(temporary, { recursive: true, force: true });
391
+ }
392
+ }
393
+ signal?.throwIfAborted();
394
+ await withFoundryTaskMetadata(context, (_, index) => validateCurrent(index));
395
+ try {
396
+ const run = acceptRetainedTarget(context, qualified, target, claim, authFile, runDir, authentication.mode);
397
+ accepted.push({ target, run, runDir });
398
+ if (invocations !== null)
399
+ invocations += 1;
400
+ }
401
+ catch (error) {
402
+ invocations = null;
403
+ if (newlyClaimed)
404
+ thisInvocation = null;
405
+ blockers.push({
406
+ code: "identity_stage_outcome_unproven",
407
+ target,
408
+ disposition: "UNKNOWN_DO_NOT_REPLAY",
409
+ reason: error instanceof Error ? error.message : "missing-execution-evidence",
410
+ });
411
+ // A partial stage cannot evade this claim through another intent or launch later targets.
412
+ break;
413
+ }
414
+ }
415
+ const resultId = sha256Json({
416
+ accepted: accepted.map(({ target, run }) => ({ target: key(target), run: sha256Json(run) })),
417
+ blockers,
418
+ });
419
+ const resultRoot = path.join(output, "results", resultId);
420
+ const report = await runFoundryTaskOperation(context, {
421
+ command: "dataset-workflow-identity",
422
+ options: {
423
+ stage_id: stageId,
424
+ result_id: resultId,
425
+ explicit_new_stage: true,
426
+ intent_sha256: intentSha256,
427
+ },
428
+ validateCurrent,
429
+ }, (operation) => {
430
+ // Partial raw outputs remain retained but unadopted. Only completed exact executions are indexed.
431
+ const retainedDirectories = [];
432
+ for (const { target, runDir } of accepted) {
433
+ retainedDirectories.push(runDir, resolveFoundryOutput(context, path.resolve(context.assetRoot, String(target.row.output_dir))));
434
+ }
435
+ registerWorkflowStageFiles(context, operation, retainedDirectories);
436
+ const combined = path.join(resultRoot, "identity-preflight-requests.jsonl");
437
+ operation.writeText(combined, accepted.map(({ target }) => JSON.stringify(target.row)).join("\n") +
438
+ (accepted.length ? "\n" : ""));
439
+ const sets = [];
440
+ const grouped = new Map();
441
+ for (const item of accepted) {
442
+ const groupKey = JSON.stringify([item.target.dataset_type, item.target.source_file]);
443
+ grouped.set(groupKey, [...(grouped.get(groupKey) ?? []), item]);
444
+ }
445
+ for (const [groupKey, items] of grouped) {
446
+ const groupRoot = path.join(resultRoot, "sets", sha256Text(groupKey));
447
+ const index = path.join(groupRoot, "identity-preflight-requests.jsonl");
448
+ operation.writeText(index, items.map(({ target }) => JSON.stringify(target.row)).join("\n") + "\n");
449
+ const summary = {
450
+ schema: "tiangong-foundry.identity-stage-set.v1",
451
+ remote_write_mode: "read-only",
452
+ status: items.some(({ run }) => run.status === "completed_with_identity_findings")
453
+ ? "completed_with_identity_findings"
454
+ : "completed",
455
+ type: items[0].target.dataset_type,
456
+ rows: items[0].target.source_file,
457
+ index,
458
+ owner_runs: items.map(({ run }) => path.resolve(context.assetRoot, String(workflowObject(run.files).report))),
459
+ };
460
+ const summaryFile = path.join(groupRoot, "identity-stage-set.json");
461
+ operation.writeJson(summaryFile, summary);
462
+ sets.push({
463
+ type: summary.type,
464
+ rows: summary.rows,
465
+ index,
466
+ report: summaryFile,
467
+ status: summary.status,
468
+ });
469
+ }
470
+ const report = {
471
+ schema: "tiangong-foundry.identity-stage.v1",
472
+ status: blockers.length ? "blocked" : "completed",
473
+ mode: "explicit-new-read-only-stage",
474
+ explicit_new_stage: true,
475
+ new_cli_execution: accepted.length ? true : invocations === null ? null : false,
476
+ stage_id: stageId,
477
+ intent_id: intent.intent_id,
478
+ task_id: context.taskId,
479
+ actor_id: context.actorId,
480
+ rows_report: rowsFile,
481
+ owner_base: context.assetRoot,
482
+ predecessor_identity: prepare.predecessor_identity,
483
+ account: prepare.account,
484
+ qualification_sha256: qualified.qualification_sha256,
485
+ authentication_mode: authentication.mode,
486
+ checked_at_utc: operation.nowIso(),
487
+ index: combined,
488
+ counts: {
489
+ admitted_targets: preparedTargets.length,
490
+ accepted_targets: accepted.length,
491
+ cli_invocations: invocations,
492
+ underlying_retrievals: null,
493
+ },
494
+ sets,
495
+ blockers,
496
+ };
497
+ operation.writeJson(path.join(resultRoot, "foundry-identity.json"), report);
498
+ return report;
499
+ });
500
+ return {
501
+ ...report,
502
+ this_invocation: { cli_invocations: thisInvocation, underlying_retrievals: null },
503
+ };
504
+ }
505
+ finally {
506
+ delete environment.TIANGONG_LCA_ACCESS_TOKEN;
507
+ }
508
+ }
509
+ function acceptRetainedTarget(context, qualified, target, claim, authFile, runDir, authenticationMode) {
510
+ assertQualifiedFoundryRuntime(context, qualified);
511
+ if (claim.admission_status === "ORPHANED_CLAIM_UNPROVEN")
512
+ reject("orphaned-claim-unproven");
513
+ const originalMode = claim.authentication_mode;
514
+ if ((originalMode !== "oauth" && originalMode !== "headless") ||
515
+ originalMode !== authenticationMode)
516
+ reject("retained-authentication-mode-changed");
517
+ if (claim.qualification_sha256 !== qualified.qualification_sha256 ||
518
+ stableJson(claim.target) !== stableJson(target) ||
519
+ claim.max_attempts !== 1)
520
+ reject("retained-claim-binding");
521
+ const authFact = captureFoundryInput(authFile);
522
+ if (stableJson(authFact) !== stableJson(claim.auth_receipt))
523
+ reject("retained-authentication-receipt");
524
+ const receipt = parseFreshIntentBoundAuthReceipt(readJson(authFile), {
525
+ nowMs: Date.parse(String(claim.claimed_at_utc)),
526
+ maxAgeMs: 60_000,
527
+ expectedProjectRef: context.accountIntent.projectRef,
528
+ expectedUserId: context.accountIntent.userId,
529
+ sessionMode: originalMode,
530
+ });
531
+ const runFile = path.join(runDir, "dataset-identity-preflight-run-report.json"), run = readJson(runFile);
532
+ const resolve = (value) => {
533
+ if (typeof value !== "string")
534
+ reject("retained-locator");
535
+ return resolveFoundryOutput(context, path.resolve(context.assetRoot, value));
536
+ };
537
+ if (resolve(workflowObject(run.files).report) !== resolve(runFile))
538
+ reject("retained-run-report-path");
539
+ if (!Array.isArray(run.results) ||
540
+ run.results.length !== 1 ||
541
+ !["completed", "completed_with_identity_findings"].includes(String(run.status)) ||
542
+ run.command !== "dataset-identity-preflight-run" ||
543
+ run.remote_write_mode !== "read-only" ||
544
+ resolve(run.index_file) !== target.index)
545
+ reject("retained-owner-result");
546
+ const result = workflowObject(run.results[0]), options = workflowObject(run.runtime_options), cli = workflowObject(options.cli);
547
+ const auth = workflowObject(options.auth_receipt), installed = resolveInstalledTiangongLcaCliPackage();
548
+ if (result.status !== "completed" ||
549
+ result.attempt !== 1 ||
550
+ result.attempts !== 1 ||
551
+ result.failure_code !== null ||
552
+ options.max_attempts !== 1 ||
553
+ options.timeout_ms !== 60_000 ||
554
+ options.retry_failed !== null ||
555
+ options.result_cache_dir !== null ||
556
+ cli.executable !== process.execPath ||
557
+ stableJson(cli.args_prefix) !== stableJson([installed.binPath]) ||
558
+ cli.package !== `${installed.packageName}@${installed.packageVersion}` ||
559
+ resolve(auth.file) !== authFile ||
560
+ auth.receipt_scope_sha256 !== receipt.receipt_scope_sha256 ||
561
+ auth.project_ref !== receipt.project.project_ref ||
562
+ auth.user_id !== receipt.identity.user_id)
563
+ reject("retained-owner-runtime-account-attempt");
564
+ if (key(result) !== key(target))
565
+ reject("retained-target-identity");
566
+ if (stableJson(jsonLines(resolve(workflowObject(run.files).results))) !== stableJson(run.results))
567
+ reject("retained-results-bytes");
568
+ const requestFile = resolve(target.row.request_file), reportFile = resolve(target.row.expected_report_file), outputDir = resolve(target.row.output_dir);
569
+ const requestText = fs.readFileSync(requestFile, "utf8"), request = readJson(requestFile);
570
+ if (target.row.request_bytes_sha256 !== sha256Text(requestText) ||
571
+ target.row.request_json_sha256 !== sha256Text(JSON.stringify(request)) ||
572
+ target.row.target_sha256 !== sha256Text(JSON.stringify(request.target)) ||
573
+ resolve(target.row.source_file) !== target.owner_source_file)
574
+ reject("retained-request-bytes");
575
+ const argv = [
576
+ target.dataset_type,
577
+ "identity-preflight",
578
+ "--input",
579
+ requestFile,
580
+ "--out-dir",
581
+ outputDir,
582
+ "--json",
583
+ "--timeout-ms",
584
+ "60000",
585
+ ];
586
+ const spec = createFoundryCommandSpec({
587
+ executable: process.execPath,
588
+ argv: [installed.binPath, ...argv],
589
+ binding: {
590
+ artifacts: [
591
+ createFileArtifactFact({
592
+ role: "identity_preflight_request",
593
+ path: String(target.row.request_file),
594
+ filePath: requestFile,
595
+ }),
596
+ ],
597
+ },
598
+ });
599
+ if (result.executable !== process.execPath ||
600
+ result.cli_package !== cli.package ||
601
+ stableJson(result.cli_args) !== stableJson(argv) ||
602
+ stableJson(result.command_spec) !== stableJson(spec) ||
603
+ resolve(result.request_file) !== requestFile ||
604
+ resolve(result.report_file) !== reportFile)
605
+ reject("retained-command-spec");
606
+ const binding = createIdentityPreflightBinding({
607
+ datasetType: target.dataset_type,
608
+ datasetId: target.dataset_id,
609
+ datasetVersion: target.dataset_version,
610
+ targetSha256: String(target.row.target_sha256),
611
+ requestText,
612
+ semanticArgv: [target.dataset_type, "identity-preflight", "--json", "--timeout-ms", "60000"],
613
+ cli: {
614
+ packageName: installed.packageName,
615
+ packageVersion: installed.packageVersion,
616
+ packageIntegrity: `sha256-${sha256Text(fs.readFileSync(installed.binPath, "utf8"))}`,
617
+ },
618
+ authReceipt: receipt,
619
+ relevantInputHashes: {
620
+ ...workflowObject(target.row.relevant_input_hashes ?? {}),
621
+ source_file: captureFoundryInput(target.owner_source_file).sha256,
622
+ },
623
+ });
624
+ if (result.binding_sha256 !== binding.binding_sha256)
625
+ reject("retained-execution-binding");
626
+ const reportText = fs.readFileSync(reportFile, "utf8");
627
+ const report = workflowObject(JSON.parse(reportText));
628
+ if (report.schema_version !== 1 ||
629
+ report.kind !== target.dataset_type ||
630
+ workflowObject(report.target).id !== target.dataset_id ||
631
+ workflowObject(report.target).version !== target.dataset_version ||
632
+ report.input_path !== requestFile ||
633
+ report.out_dir !== outputDir ||
634
+ workflowObject(report.files).identity_decision !== reportFile ||
635
+ Object.hasOwn(report, "error") ||
636
+ (Object.hasOwn(report, "errors") && (!Array.isArray(report.errors) || report.errors.length)) ||
637
+ result.report_status !== report.status ||
638
+ result.decision !== (report.decision ?? null) ||
639
+ result.signal != null ||
640
+ fs.readFileSync(resolve(result.stderr_log), "utf8") !== "" ||
641
+ typeof result.cli_exit_code !== "number" ||
642
+ ![0, 1].includes(result.cli_exit_code))
643
+ reject("retained-report-identity-path-outcome");
644
+ const manifest = readJson(resolve(result.execution_manifest_file));
645
+ const completedAt = Date.parse(String(manifest.completed_at_utc));
646
+ const generatedAt = Date.parse(String(report.generated_at_utc));
647
+ const runCompletedAt = Date.parse(String(run.generated_at_utc));
648
+ if (!Number.isFinite(completedAt) ||
649
+ !Number.isFinite(generatedAt) ||
650
+ !Number.isFinite(runCompletedAt) ||
651
+ generatedAt < Date.parse(String(claim.claimed_at_utc)) ||
652
+ generatedAt > completedAt ||
653
+ completedAt < Date.parse(String(claim.claimed_at_utc)) ||
654
+ completedAt > runCompletedAt)
655
+ reject("retained-execution-time");
656
+ const checked = validateIdentityPreflightExecution({
657
+ binding,
658
+ exitCode: Number(result.cli_exit_code),
659
+ stdoutText: fs.readFileSync(resolve(result.stdout_log), "utf8"),
660
+ diskReportText: reportText,
661
+ startedAtMs: Date.parse(String(claim.claimed_at_utc)),
662
+ diskReportMtimeMs: fs.statSync(reportFile).mtimeMs,
663
+ completedAtUtc: String(manifest.completed_at_utc),
664
+ requestFile,
665
+ outputDir,
666
+ reportFile,
667
+ stderrText: fs.readFileSync(resolve(result.stderr_log), "utf8"),
668
+ signal: result.signal == null ? null : String(result.signal),
669
+ });
670
+ if (!checked.ok)
671
+ reject(checked.code);
672
+ if (!validateBoundExecutionManifest(manifest, binding, reportText).ok ||
673
+ stableJson(manifest) !== stableJson(checked.manifest))
674
+ reject("retained-execution-manifest");
675
+ return run;
676
+ }