@tiangong-lca/foundry 0.1.18 → 0.1.20

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 (116) hide show
  1. package/README.md +5 -3
  2. package/docs/architecture.md +5 -3
  3. package/docs/foundry-task-contracts.md +2 -0
  4. package/docs/package-distribution-contract.md +10 -8
  5. package/docs/public-runtime-contract.md +53 -3
  6. package/docs/runtime-context-contract.md +7 -3
  7. package/docs/task-authorization-contract.md +2 -0
  8. package/package-dist/assets/foundry-package-descriptor.json +374 -136
  9. package/package-dist/scripts/foundry-facade.d.ts +6 -0
  10. package/package-dist/scripts/foundry-facade.js +38 -0
  11. package/package-dist/scripts/foundry-runtime.d.ts +3 -2
  12. package/package-dist/scripts/lib/decision-owners/identity-preflight.js +3 -1
  13. package/package-dist/scripts/lib/final-delivery-manifest.d.ts +40 -0
  14. package/package-dist/scripts/lib/final-delivery-manifest.js +145 -0
  15. package/package-dist/scripts/lib/final-delivery-review.d.ts +37 -0
  16. package/package-dist/scripts/lib/final-delivery-review.js +108 -0
  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/native-draft-handoff.js +4 -3
  20. package/package-dist/scripts/lib/finalize-owners/post-authoring.d.ts +9 -0
  21. package/package-dist/scripts/lib/finalize-owners/post-authoring.js +37 -8
  22. package/package-dist/scripts/lib/foundry-authentication-environment.js +1 -1
  23. package/package-dist/scripts/lib/foundry-curation-queue.d.ts +73 -0
  24. package/package-dist/scripts/lib/foundry-curation-queue.js +261 -0
  25. package/package-dist/scripts/lib/foundry-edge-draft-admission.d.ts +17 -0
  26. package/package-dist/scripts/lib/foundry-edge-draft-admission.js +94 -0
  27. package/package-dist/scripts/lib/foundry-edge-identity-preflight.d.ts +47 -0
  28. package/package-dist/scripts/lib/foundry-edge-identity-preflight.js +268 -0
  29. package/package-dist/scripts/lib/foundry-edge-oauth.d.ts +4 -0
  30. package/package-dist/scripts/lib/foundry-edge-oauth.js +154 -0
  31. package/package-dist/scripts/lib/foundry-edge-reference-intent.d.ts +51 -0
  32. package/package-dist/scripts/lib/foundry-edge-reference-intent.js +184 -0
  33. package/package-dist/scripts/lib/foundry-edge-repair-preparation.d.ts +24 -0
  34. package/package-dist/scripts/lib/foundry-edge-repair-preparation.js +209 -0
  35. package/package-dist/scripts/lib/foundry-edge-session.d.ts +32 -0
  36. package/package-dist/scripts/lib/foundry-edge-session.js +203 -0
  37. package/package-dist/scripts/lib/foundry-edge-support-plan.d.ts +69 -0
  38. package/package-dist/scripts/lib/foundry-edge-support-plan.js +245 -0
  39. package/package-dist/scripts/lib/foundry-edge-support-planning.d.ts +115 -0
  40. package/package-dist/scripts/lib/foundry-edge-support-planning.js +259 -0
  41. package/package-dist/scripts/lib/foundry-edge-support.d.ts +19 -0
  42. package/package-dist/scripts/lib/foundry-edge-support.js +188 -0
  43. package/package-dist/scripts/lib/foundry-execution-admission.d.ts +4 -2
  44. package/package-dist/scripts/lib/foundry-execution-admission.js +24 -2
  45. package/package-dist/scripts/lib/foundry-finalize-edge-identity.d.ts +30 -0
  46. package/package-dist/scripts/lib/foundry-finalize-edge-identity.js +102 -0
  47. package/package-dist/scripts/lib/foundry-finalize-owners.d.ts +1 -0
  48. package/package-dist/scripts/lib/foundry-finalize-owners.js +68 -2
  49. package/package-dist/scripts/lib/foundry-interaction-input.js +2 -2
  50. package/package-dist/scripts/lib/foundry-managed-authentication.js +1 -1
  51. package/package-dist/scripts/lib/foundry-operation-result.d.ts +1 -1
  52. package/package-dist/scripts/lib/foundry-operation-result.js +1 -0
  53. package/package-dist/scripts/lib/foundry-owner-execution-store.d.ts +16 -16
  54. package/package-dist/scripts/lib/foundry-owner-execution-store.js +34 -7
  55. package/package-dist/scripts/lib/foundry-owner-readback.d.ts +21 -0
  56. package/package-dist/scripts/lib/foundry-owner-readback.js +61 -0
  57. package/package-dist/scripts/lib/foundry-package-contract.d.ts +4 -4
  58. package/package-dist/scripts/lib/foundry-package-contract.js +8 -2
  59. package/package-dist/scripts/lib/foundry-preparation-cli-validation.js +71 -0
  60. package/package-dist/scripts/lib/foundry-process-mass-balance.d.ts +64 -0
  61. package/package-dist/scripts/lib/foundry-process-mass-balance.js +224 -0
  62. package/package-dist/scripts/lib/foundry-process-qa.d.ts +19 -0
  63. package/package-dist/scripts/lib/foundry-process-qa.js +423 -0
  64. package/package-dist/scripts/lib/foundry-repair-census.d.ts +82 -0
  65. package/package-dist/scripts/lib/foundry-repair-census.js +574 -0
  66. package/package-dist/scripts/lib/foundry-repair-execution.js +13 -2
  67. package/package-dist/scripts/lib/foundry-repair-scope.d.ts +15 -2
  68. package/package-dist/scripts/lib/foundry-repair-scope.js +62 -10
  69. package/package-dist/scripts/lib/foundry-runtime-environment.d.ts +2 -0
  70. package/package-dist/scripts/lib/foundry-runtime-environment.js +18 -0
  71. package/package-dist/scripts/lib/foundry-runtime-identity.d.ts +1 -0
  72. package/package-dist/scripts/lib/foundry-runtime-identity.js +19 -1
  73. package/package-dist/scripts/lib/foundry-runtime-qualification.js +2 -2
  74. package/package-dist/scripts/lib/foundry-runtime-utils.d.ts +1 -0
  75. package/package-dist/scripts/lib/foundry-runtime-utils.js +1 -1
  76. package/package-dist/scripts/lib/foundry-scientific-repair.d.ts +38 -0
  77. package/package-dist/scripts/lib/foundry-scientific-repair.js +438 -0
  78. package/package-dist/scripts/lib/foundry-semantic-input.js +1 -1
  79. package/package-dist/scripts/lib/foundry-task-input-preparation.d.ts +4 -0
  80. package/package-dist/scripts/lib/foundry-task-input-preparation.js +160 -0
  81. package/package-dist/scripts/lib/foundry-task-registration.js +19 -1
  82. package/package-dist/scripts/lib/foundry-task-types.d.ts +2 -0
  83. package/package-dist/scripts/lib/foundry-typed-authoring-patch.d.ts +76 -0
  84. package/package-dist/scripts/lib/foundry-typed-authoring-patch.js +237 -0
  85. package/package-dist/scripts/lib/foundry-typed-context.d.ts +11 -0
  86. package/package-dist/scripts/lib/foundry-typed-context.js +79 -0
  87. package/package-dist/scripts/lib/foundry-typed-flow-qa.d.ts +166 -0
  88. package/package-dist/scripts/lib/foundry-typed-flow-qa.js +226 -0
  89. package/package-dist/scripts/lib/foundry-typed-model-write.d.ts +6 -0
  90. package/package-dist/scripts/lib/foundry-typed-model-write.js +59 -0
  91. package/package-dist/scripts/lib/foundry-workflow-assessment.js +67 -29
  92. package/package-dist/scripts/lib/foundry-workflow-authorization.js +82 -1
  93. package/package-dist/scripts/lib/foundry-workflow-context.js +59 -18
  94. package/package-dist/scripts/lib/foundry-workflow-execution.js +57 -5
  95. package/package-dist/scripts/lib/foundry-workflow-finalize.js +27 -3
  96. package/package-dist/scripts/lib/foundry-workflow-identity-stage.js +202 -161
  97. package/package-dist/scripts/lib/foundry-workflow-identity.js +16 -10
  98. package/package-dist/scripts/lib/foundry-workflow-io.js +27 -6
  99. package/package-dist/scripts/lib/foundry-workflow-reference-verify.js +102 -29
  100. package/package-dist/scripts/lib/foundry-workflow-repair.d.ts +5 -0
  101. package/package-dist/scripts/lib/foundry-workflow-repair.js +204 -2
  102. package/package-dist/scripts/lib/foundry-workflow-semantic.js +41 -21
  103. package/package-dist/scripts/lib/identity-preflight-proof.d.ts +1 -0
  104. package/package-dist/scripts/lib/identity-preflight-proof.js +19 -12
  105. package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
  106. package/package-dist/scripts/public-api.d.ts +6 -0
  107. package/package-dist/scripts/runtime-entry.js +15 -6
  108. package/package.json +5 -2
  109. package/specs/schemas/foundry-interaction-draft.schema.json +323 -0
  110. package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
  111. package/specs/schemas/foundry-operation-result.schema.json +1 -0
  112. package/specs/schemas/foundry-package-descriptor.schema.json +5 -2
  113. package/specs/schemas/foundry-scientific-repair.schema.json +66 -0
  114. package/specs/schemas/foundry-semantic-draft.schema.json +50 -0
  115. package/specs/schemas/foundry-task-start.schema.json +10 -2
  116. package/specs/schemas/runtime-qualification.schema.json +1 -1
@@ -19,8 +19,15 @@ import { workflowObject } from "./foundry-workflow-state.js";
19
19
  import { canonicalPayloadSha256 } from "./post-write-root-proof.js";
20
20
  import { summarizeSameIdentityCommitFailures } from "./same-identity-commit-recovery.js";
21
21
  import { readRows } from "./import-curation/internal/runtime-io.js";
22
+ import { verifySelectedEdgeDraftPlan } from "./foundry-edge-draft-admission.js";
23
+ import { isEdgeSupportPlan } from "./foundry-edge-support-plan.js";
24
+ import { createFoundryEdgeActor } from "./foundry-edge-session.js";
25
+ import { dispatchFoundryEdgeSupport, verifyFoundryEdgeSupport } from "./foundry-edge-support.js";
26
+ import { createFoundryTypedModelDraft } from "./foundry-typed-model-write.js";
27
+ import { verifyScientificRepairBeforeDispatch } from "./foundry-workflow-repair.js";
22
28
  export async function executeFoundryOwnerScope(context, qualified, entries, requested, authentication = { mode: "oauth" }) {
23
29
  const request = requested.request;
30
+ const edgePlan = isEdgeSupportPlan(request.content.commit) ? request.content.commit : null;
24
31
  if (!entries.some((entry) => entry.path === requested.entry.path && entry.sha256 === requested.entry.sha256))
25
32
  throw new FoundryContextError("execution_request_unregistered", "Select a registered owner execution request.");
26
33
  const lock = resolveFoundryOutput(context, `owner-locks/${context.taskId}-${request.scope_id}.json`, "state");
@@ -49,6 +56,7 @@ export async function executeFoundryOwnerScope(context, qualified, entries, requ
49
56
  throw new FoundryContextError("execution_observation_invalid", "Dispatch observation belongs to another request.");
50
57
  let identity = null;
51
58
  let admission = null;
59
+ let edgeDispatch = null;
52
60
  if (!consumed) {
53
61
  identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
54
62
  admission = await rehydrateFoundryExecutionAdmission(context, qualified, identity, {
@@ -61,10 +69,24 @@ export async function executeFoundryOwnerScope(context, qualified, entries, requ
61
69
  await assertFoundryExecutionAdmission(context, qualified, identity, admission);
62
70
  if (observation)
63
71
  throw new FoundryContextError("execution_observation_invalid", "Existing dispatch evidence cannot be treated as unattempted.");
64
- const out = commandSpecOptionValue(request.content.commit, "--out-dir");
65
- resolveFoundryOutput(context, out);
66
- if (fs.existsSync(out) && fs.readdirSync(out).length)
67
- throw new FoundryContextError("execution_output_not_empty", "An unattempted command cannot adopt existing output artifacts.");
72
+ if (edgePlan) {
73
+ if (edgePlan.update)
74
+ await verifyScientificRepairBeforeDispatch(context, qualified, authentication, requested.entry.sha256);
75
+ const rows = verifySelectedEdgeDraftPlan(context, edgePlan, captureFoundryInput(request.content.input.path), request.policy);
76
+ const actor = await createFoundryEdgeActor(context, qualified, authentication);
77
+ const before = await verifyFoundryEdgeSupport(actor, edgePlan, "before");
78
+ if (before.blockers.length)
79
+ throw new FoundryContextError("edge_support_before_conflict", "The Edge draft identity is already in use or a required dependency cannot be verified.");
80
+ identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
81
+ await assertFoundryExecutionAdmission(context, qualified, identity, admission);
82
+ edgeDispatch = { actor, rows, before };
83
+ }
84
+ else {
85
+ const out = commandSpecOptionValue(request.content.commit, "--out-dir");
86
+ resolveFoundryOutput(context, out);
87
+ if (fs.existsSync(out) && fs.readdirSync(out).length)
88
+ throw new FoundryContextError("execution_output_not_empty", "An unattempted command cannot adopt existing output artifacts.");
89
+ }
68
90
  }
69
91
  const attemptStore = ownerAttemptStore(context, request.scope_id, {
70
92
  path: requested.file,
@@ -107,6 +129,34 @@ export async function executeFoundryOwnerScope(context, qualified, entries, requ
107
129
  if (!identity || !admission)
108
130
  throw new FoundryContextError("execution_admission_required", "Mutation requires fresh process-local admission.");
109
131
  const command = request.content.commit;
132
+ if (isEdgeSupportPlan(command)) {
133
+ if (!edgeDispatch)
134
+ throw new FoundryContextError("execution_admission_required", "Edge creation requires fresh verified draft admission.");
135
+ writeFoundryArtifact(context, path.join(observationDir, "edge-before.json"), JSON.stringify(edgeDispatch.before, null, 2) + "\n");
136
+ let acknowledgements = null;
137
+ const { actor, rows } = edgeDispatch;
138
+ try {
139
+ acknowledgements = await dispatchFoundryEdgeSupport(actor, command, rows, (root, content) => createFoundryTypedModelDraft(context, qualified, authentication, actor.receipt, requested.entry.sha256, root, content));
140
+ }
141
+ catch {
142
+ // The durable owner attempt was consumed before the first request. A lost
143
+ // response can only be settled by independent exact readback.
144
+ }
145
+ observation = {
146
+ schema: "tiangong-foundry.dispatch-observation.v1",
147
+ request_sha256: requested.entry.sha256,
148
+ disposition: acknowledgements ? "confirmed" : "unknown",
149
+ edge_plan_sha256: command.sha256,
150
+ acknowledgements,
151
+ };
152
+ writeFoundryArtifact(context, observationFile, JSON.stringify(observation, null, 2) + "\n");
153
+ if (!acknowledgements)
154
+ throw new Error("Edge dispatch requires exact readback.");
155
+ const verified = await readback();
156
+ if (!verified)
157
+ throw new Error("Edge drafts are not independently verified.");
158
+ return verified.proof;
159
+ }
110
160
  const environment = createFoundryAuthenticationEnvironment(authentication, context.accountIntent?.sessionReference, process.env);
111
161
  environment.FOUNDRY_VERIFIED_PROJECT_REF = request.policy.project_ref;
112
162
  environment.FOUNDRY_VERIFIED_USER_ID = request.policy.user_id;
@@ -224,7 +274,9 @@ export async function executeFoundryOwnerScope(context, qualified, entries, requ
224
274
  if (!observationRegistered) {
225
275
  if (fs.existsSync(observationDir))
226
276
  registerWorkflowStageFiles(context, operation, observationDir);
227
- const commitDir = commandSpecOptionValue(request.content.commit, "--out-dir");
277
+ const commitDir = edgePlan
278
+ ? null
279
+ : commandSpecOptionValue(request.content.commit, "--out-dir");
228
280
  if (commitDir && fs.existsSync(commitDir))
229
281
  registerWorkflowStageFiles(context, operation, commitDir);
230
282
  }
@@ -7,6 +7,8 @@ import { verifyFoundryRuntimeIdentity, } from "./foundry-runtime-identity.js";
7
7
  import { createFoundryAuthenticationEnvironment } from "./foundry-authentication-environment.js";
8
8
  import { createFoundryIsolatedChildEnvironment, copyFoundryIsolatedExecutable, } from "./foundry-runtime-environment.js";
9
9
  import { WorkflowChildFailure } from "./foundry-workflow-child-failure.js";
10
+ import { createFoundryIdentityOwners } from "./foundry-identity-owners.js";
11
+ import { refreshFoundryFinalizeIdentity } from "./foundry-finalize-edge-identity.js";
10
12
  import { createFoundryFinalizeOwners } from "./foundry-finalize-owners.js";
11
13
  import { currentWorkflowState, readWorkflowArtifact, workflowObject, } from "./foundry-workflow-state.js";
12
14
  import { runFoundryTaskOperation, withFoundryTaskMetadata } from "./foundry-task-store.js";
@@ -139,13 +141,15 @@ export async function finalizeFoundryWorkflow(context, qualified, entries, authe
139
141
  tidasExecutable,
140
142
  receiptFile,
141
143
  datasetType: set.type,
144
+ outputRoot: path.join(output, set.type),
142
145
  });
143
146
  const contract = contracts.get(set.type);
144
147
  if (!contract)
145
148
  throw new FoundryContextError("workflow_context_required", "Finalization requires the current owner contract pack.");
146
149
  const needsIdentity = ["flow", "process"].includes(set.type);
147
150
  let identityIndex = state.identity?.value.index;
148
- if (needsIdentity && typeof identityIndex === "string") {
151
+ const typed = qualified.cli.expectation.package_version === "0.4.0";
152
+ if (!typed && needsIdentity && typeof identityIndex === "string") {
149
153
  const prepared = workflowObject(owners.preflight.runDatasetIdentityPreflightRequestsBuild({
150
154
  type: set.type,
151
155
  rowsFile: set.file,
@@ -165,7 +169,7 @@ export async function finalizeFoundryWorkflow(context, qualified, entries, authe
165
169
  throw new FoundryContextError("finalize_identity_inputs_invalid", "Finalization identity index could not preserve dependency evidence.");
166
170
  identityIndex = path.resolve(context.assetRoot, mergedFile);
167
171
  }
168
- const result = workflowObject(owners.invoke(() => owners.finalize.runDatasetPostAuthoringFinalize({
172
+ const finalizeOptions = {
169
173
  type: ["unitgroup", "flowproperty"].includes(set.type) ? "support" : set.type,
170
174
  rowsFile: set.file,
171
175
  ...referenceOptions.get(set.type),
@@ -205,7 +209,27 @@ export async function finalizeFoundryWorkflow(context, qualified, entries, authe
205
209
  verifyRemote: Boolean(context.accountIntent),
206
210
  remoteStateCode: "0",
207
211
  remoteRootPolicy: "candidate",
208
- })));
212
+ };
213
+ const stages = owners.finalize.finalizeStages(finalizeOptions, typed);
214
+ let finished = owners.invoke(() => stages.next());
215
+ if (!finished.done) {
216
+ if ((set.type !== "flow" && set.type !== "process") || typeof identityIndex !== "string")
217
+ throw new FoundryContextError("finalize_identity_inputs_invalid", "Finalization requires a selected Flow or Process identity index.");
218
+ const identityOwner = createFoundryIdentityOwners(context, qualified, set.type, {
219
+ environment,
220
+ cwd: temporary,
221
+ });
222
+ const refreshed = await refreshFoundryFinalizeIdentity(context, qualified, authentication, identityOwner, {
223
+ type: set.type,
224
+ rowsFile: finished.value.rowsFile,
225
+ outDir: finished.value.outDir,
226
+ index: identityIndex,
227
+ });
228
+ finished = owners.invoke(() => stages.next(refreshed));
229
+ }
230
+ if (!finished.done)
231
+ throw new FoundryContextError("finalize_identity_inputs_invalid", "Unexpected additional identity stage.");
232
+ const result = workflowObject(finished.value);
209
233
  const files = workflowObject(result.files);
210
234
  sets.push({
211
235
  type: set.type,
@@ -7,6 +7,7 @@ import { captureFoundryInput, FoundryContextError, readFoundryInput, resolveFoun
7
7
  import { assertQualifiedFoundryRuntime, } from "./foundry-runtime-qualification.js";
8
8
  import { assertVerifiedFoundryIdentity, verifyFoundryRuntimeIdentity, } from "./foundry-runtime-identity.js";
9
9
  import { createFoundryAuthenticationEnvironment } from "./foundry-authentication-environment.js";
10
+ import { readRetainedFoundryEdgeIdentity, runFoundryEdgeIdentityPreflight, } from "./foundry-edge-identity-preflight.js";
10
11
  import { createFoundryIdentityOwners } from "./foundry-identity-owners.js";
11
12
  import { inspectOwnerExecutions } from "./foundry-owner-execution-store.js";
12
13
  import { currentFoundryInteractionState, currentFoundryQuestions, currentFoundryInvestigations, } from "./foundry-interaction-input.js";
@@ -188,118 +189,129 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
188
189
  state.identity?.value.stage_id !== stageId)
189
190
  reject("current-predecessor-changed");
190
191
  };
191
- await withFoundryTaskMetadata(context, (_, index) => validateCurrent(index));
192
+ const retainedStageFile = (file, command) => withFoundryTaskMetadata(context, (_, index) => {
193
+ validateCurrent(index);
194
+ const entry = index.findLast((item) => item.command === command && path.resolve(context.taskRoot, item.path) === file);
195
+ return entry ? readWorkflowArtifact(context, entry).value : null;
196
+ });
192
197
  const environment = createFoundryAuthenticationEnvironment(authentication, context.accountIntent.sessionReference, process.env);
193
198
  environment.FOUNDRY_VERIFIED_PROJECT_REF = context.accountIntent.projectRef;
194
199
  environment.FOUNDRY_VERIFIED_USER_ID = context.accountIntent.userId;
195
200
  fs.mkdirSync(output, { recursive: true, mode: 0o700 });
196
201
  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,
202
+ const prepare = (await retainedStageFile(prepareFile, prepareCommand)) ??
203
+ (await runFoundryTaskOperation(context, {
204
+ command: prepareCommand,
205
+ options: {
206
+ stage_id: stageId,
207
+ intent_sha256: intentSha256,
208
+ rows_report_sha256: intent.rows_report_sha256,
209
+ predecessor_identity_sha256: intent.predecessor_identity_sha256,
220
210
  },
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,
211
+ validateCurrent,
212
+ }, (operation) => {
213
+ const identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
214
+ const cli = assertCliRuntimeMatches(qualified.cli.expectation);
215
+ const installed = resolveInstalledTiangongLcaCliPackage();
216
+ const root = path.dirname(prepareFile);
217
+ operation.writeText(path.join(root, "selected-intent.json"), selectedBytes(selected));
218
+ operation.writeJson(path.join(root, "identity-receipt.json"), identity.receipt);
219
+ operation.writeJson(path.join(root, "roster.json"), targets);
220
+ operation.writeJson(path.join(root, "runtime-cli-inventory.json"), {
221
+ qualification: qualified,
222
+ cli: {
223
+ root: installed.packageRoot,
224
+ bin: captureFoundryInput(installed.binPath),
225
+ files: cli.files,
226
+ },
227
+ executable: captureFoundryInput(process.execPath),
238
228
  });
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,
229
+ const preparedTargets = [];
230
+ for (const target of targets) {
231
+ const type = target.dataset_type;
232
+ // The published owner names outputs by ID. A separate version-bound parent prevents collisions.
233
+ const outDir = path.join(root, "targets", sha256Json(target));
234
+ const sourceIndex = path.join(outDir, "source-context.jsonl");
235
+ const rowFile = path.join(outDir, "selected-row.jsonl");
236
+ const row = readRows(target.source_file).find((value) => sha256Json(value) === target.source_row_sha256);
237
+ if (!row)
238
+ reject("prepared-source-row");
239
+ operation.writeText(rowFile, JSON.stringify(row) + "\n");
240
+ operation.writeText(sourceIndex, JSON.stringify({ ...target, source_file: rowFile }) + "\n");
241
+ const owner = createFoundryIdentityOwners(context, qualified, type, {
242
+ environment,
243
+ cwd: root,
244
+ });
245
+ const built = workflowObject(owner.invoke(() => owner.preflight.runDatasetIdentityPreflightRequestsBuild({
246
+ type,
247
+ rowsFile: rowFile,
248
+ sourceIndex,
249
+ outDir,
250
+ })));
251
+ const indexFile = resolveFoundryOutput(context, path.resolve(context.assetRoot, String(workflowObject(built.files).identity_preflight_requests)));
252
+ const audited = workflowObject(owner.invoke(() => owner.preflight.runDatasetIdentityPreflightQueryAudit({
253
+ index: indexFile,
254
+ outDir: path.join(outDir, "query-audit"),
255
+ })));
256
+ if (built.status !== "ready" || audited.status !== "passed")
257
+ reject(`request-query-audit:${JSON.stringify({ prepare: built.blockers, audit: audited.blockers })}`);
258
+ const indexRows = jsonLines(indexFile);
259
+ if (indexRows.length !== 1 ||
260
+ key(indexRows[0]) !== key(target))
261
+ reject("prepared-roster-count");
262
+ const admittedRow = {
263
+ ...indexRows[0],
264
+ relevant_input_hashes: {
265
+ registered_rows: captureFoundryInput(target.source_file).sha256,
266
+ source_row: target.source_row_sha256,
267
+ },
268
+ };
269
+ const admittedIndex = path.join(outDir, "admitted-identity-preflight-requests.jsonl");
270
+ operation.writeText(admittedIndex, JSON.stringify(admittedRow) + "\n");
271
+ preparedTargets.push({
272
+ ...target,
273
+ owner_source_file: rowFile,
274
+ index: admittedIndex,
275
+ row: admittedRow,
276
+ });
277
+ }
278
+ registerWorkflowStageFiles(context, operation, root);
279
+ const report = {
280
+ schema: "tiangong-foundry.identity-stage-preparation.v1",
281
+ stage_id: stageId,
282
+ intent_id: intent.intent_id,
283
+ descriptor_sha256: selected.descriptor.sha256,
284
+ intent_sha256: intentSha256,
285
+ task_id: context.taskId,
286
+ actor_id: context.actorId,
287
+ rows_report: rowsFile,
288
+ rows_report_sha256: intent.rows_report_sha256,
289
+ predecessor_identity: {
290
+ path: predecessorFile,
291
+ sha256: intent.predecessor_identity_sha256,
261
292
  },
293
+ predecessor_identity_sha256: intent.predecessor_identity_sha256,
294
+ qualification_sha256: qualified.qualification_sha256,
295
+ authentication_mode: identity.mode,
296
+ targets: preparedTargets,
297
+ account: {
298
+ project_ref: context.accountIntent.projectRef,
299
+ user_id: context.accountIntent.userId,
300
+ },
301
+ prepared_at_utc: operation.nowIso(),
262
302
  };
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
- });
303
+ operation.writeJson(prepareFile, report);
304
+ return report;
305
+ }));
300
306
  const preparedTargets = prepare.targets;
301
- if (!Array.isArray(preparedTargets) || preparedTargets.length !== targets.length)
307
+ if (prepare.schema !== "tiangong-foundry.identity-stage-preparation.v1" ||
308
+ prepare.stage_id !== stageId ||
309
+ prepare.intent_sha256 !== intentSha256 ||
310
+ prepare.qualification_sha256 !== qualified.qualification_sha256 ||
311
+ !Array.isArray(preparedTargets) ||
312
+ preparedTargets.length !== targets.length)
302
313
  reject("retained-preparation");
314
+ const usesEdge = qualified.cli.expectation.package_version === "0.4.0";
303
315
  const accepted = [];
304
316
  const blockers = [];
305
317
  let invocations = 0;
@@ -311,55 +323,57 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
311
323
  const authFile = path.join(targetRoot, "identity-receipt.json"), runDir = path.join(targetRoot, "run");
312
324
  let newlyClaimed = false;
313
325
  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",
326
+ const claim = (await retainedStageFile(claimFile, dispatchCommand)) ??
327
+ (await runFoundryTaskOperation(context, {
328
+ command: dispatchCommand,
329
+ options: { stage_id: stageId, target_id: targetId, intent_sha256: intentSha256 },
330
+ validateCurrent(index) {
331
+ validateCurrent(index);
332
+ // Verify after expensive locked metadata; serialized receipts never extend permission.
333
+ if (!fs.existsSync(claimFile))
334
+ identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
335
+ },
336
+ }, (operation) => {
337
+ if (fs.existsSync(claimFile)) {
338
+ // An orphan file without its completed native receipt cannot prove dispatch admission.
339
+ const orphaned = {
340
+ schema: "tiangong-foundry.identity-stage-orphan-observation.v1",
341
+ stage_id: stageId,
342
+ target_id: targetId,
343
+ target,
344
+ claim: captureFoundryInput(claimFile),
345
+ admission_status: "ORPHANED_CLAIM_UNPROVEN",
346
+ };
347
+ operation.writeJson(path.join(targetRoot, "orphaned-claim.json"), orphaned);
348
+ return orphaned;
349
+ }
350
+ if (!identity)
351
+ reject("fresh-dispatch-identity");
352
+ assertVerifiedFoundryIdentity(context, identity, qualified);
353
+ operation.writeJson(authFile, identity.receipt);
354
+ const value = {
355
+ schema: "tiangong-foundry.identity-stage-dispatch.v1",
328
356
  stage_id: stageId,
329
357
  target_id: targetId,
358
+ intent_id: intent.intent_id,
330
359
  target,
331
- claim: captureFoundryInput(claimFile),
332
- admission_status: "ORPHANED_CLAIM_UNPROVEN",
360
+ auth_receipt: captureFoundryInput(authFile),
361
+ qualification_sha256: qualified.qualification_sha256,
362
+ authentication_mode: identity.mode,
363
+ max_attempts: 1,
364
+ dispatch_state: "CLAIMED_OUTCOME_UNPROVEN",
365
+ claimed_at_utc: new Date().toISOString(),
333
366
  };
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
- });
367
+ operation.writeJson(claimFile, value);
368
+ newlyClaimed = true;
369
+ return value;
370
+ }));
358
371
  if (newlyClaimed) {
359
372
  signal?.throwIfAborted();
360
- if (!identity)
373
+ const fresh = identity;
374
+ if (!fresh)
361
375
  reject("fresh-dispatch-identity");
362
- assertVerifiedFoundryIdentity(context, identity, qualified);
376
+ assertVerifiedFoundryIdentity(context, fresh, qualified);
363
377
  // Keep CLI startup independent of digest paths and the workspace's .env.
364
378
  const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "foundry-identity-stage-"));
365
379
  try {
@@ -371,16 +385,24 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
371
385
  try {
372
386
  if (thisInvocation !== null)
373
387
  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
- }));
388
+ if (usesEdge)
389
+ await runFoundryEdgeIdentityPreflight(context, qualified, authentication, target.dataset_type, target.index, runDir, {
390
+ dispatchSha256: sha256Json(claim),
391
+ sourceSha256: captureFoundryInput(target.owner_source_file).sha256,
392
+ claimedAt: String(claim.claimed_at_utc),
393
+ receipt: fresh.receipt,
394
+ });
395
+ else
396
+ owner.invoke(() => owner.preflight.runDatasetIdentityPreflightRun({
397
+ index: target.index,
398
+ outDir: runDir,
399
+ authReceipt: authFile,
400
+ expectedProjectRef: context.accountIntent.projectRef,
401
+ expectedUserId: context.accountIntent.userId,
402
+ maxAttempts: 1,
403
+ timeoutMs: 60_000,
404
+ authReceiptMaxAgeMs: 60_000,
405
+ }));
384
406
  }
385
407
  catch {
386
408
  // Retained logs may prove the original result; no new invocation follows this claim.
@@ -394,7 +416,7 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
394
416
  await withFoundryTaskMetadata(context, (_, index) => validateCurrent(index));
395
417
  try {
396
418
  const run = acceptRetainedTarget(context, qualified, target, claim, authFile, runDir, authentication.mode);
397
- accepted.push({ target, run, runDir });
419
+ accepted.push({ target, ...run, runDir });
398
420
  if (invocations !== null)
399
421
  invocations += 1;
400
422
  }
@@ -434,8 +456,7 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
434
456
  }
435
457
  registerWorkflowStageFiles(context, operation, retainedDirectories);
436
458
  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" : ""));
459
+ operation.writeText(combined, accepted.map(({ row }) => JSON.stringify(row)).join("\n") + (accepted.length ? "\n" : ""));
439
460
  const sets = [];
440
461
  const grouped = new Map();
441
462
  for (const item of accepted) {
@@ -445,7 +466,7 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
445
466
  for (const [groupKey, items] of grouped) {
446
467
  const groupRoot = path.join(resultRoot, "sets", sha256Text(groupKey));
447
468
  const index = path.join(groupRoot, "identity-preflight-requests.jsonl");
448
- operation.writeText(index, items.map(({ target }) => JSON.stringify(target.row)).join("\n") + "\n");
469
+ operation.writeText(index, items.map(({ row }) => JSON.stringify(row)).join("\n") + "\n");
449
470
  const summary = {
450
471
  schema: "tiangong-foundry.identity-stage-set.v1",
451
472
  remote_write_mode: "read-only",
@@ -472,7 +493,14 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
472
493
  status: blockers.length ? "blocked" : "completed",
473
494
  mode: "explicit-new-read-only-stage",
474
495
  explicit_new_stage: true,
475
- new_cli_execution: accepted.length ? true : invocations === null ? null : false,
496
+ new_cli_execution: usesEdge
497
+ ? false
498
+ : accepted.length
499
+ ? true
500
+ : invocations === null
501
+ ? null
502
+ : false,
503
+ ...(usesEdge ? { execution_transport: "edge" } : {}),
476
504
  stage_id: stageId,
477
505
  intent_id: intent.intent_id,
478
506
  task_id: context.taskId,
@@ -488,7 +516,8 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
488
516
  counts: {
489
517
  admitted_targets: preparedTargets.length,
490
518
  accepted_targets: accepted.length,
491
- cli_invocations: invocations,
519
+ cli_invocations: usesEdge ? 0 : invocations,
520
+ ...(usesEdge ? { edge_invocations: invocations } : {}),
492
521
  underlying_retrievals: null,
493
522
  },
494
523
  sets,
@@ -499,7 +528,11 @@ async function runLocked(context, qualified, prefix, selected, authentication, s
499
528
  });
500
529
  return {
501
530
  ...report,
502
- this_invocation: { cli_invocations: thisInvocation, underlying_retrievals: null },
531
+ this_invocation: {
532
+ cli_invocations: usesEdge ? 0 : thisInvocation,
533
+ ...(usesEdge ? { edge_invocations: thisInvocation } : {}),
534
+ underlying_retrievals: null,
535
+ },
503
536
  };
504
537
  }
505
538
  finally {
@@ -528,6 +561,14 @@ function acceptRetainedTarget(context, qualified, target, claim, authFile, runDi
528
561
  expectedUserId: context.accountIntent.userId,
529
562
  sessionMode: originalMode,
530
563
  });
564
+ if (qualified.cli.expectation.package_version === "0.4.0") {
565
+ return readRetainedFoundryEdgeIdentity(context, target.dataset_type, target.row, runDir, originalMode, {
566
+ dispatchSha256: sha256Json(claim),
567
+ sourceSha256: captureFoundryInput(target.owner_source_file).sha256,
568
+ claimedAt: String(claim.claimed_at_utc),
569
+ receipt,
570
+ });
571
+ }
531
572
  const runFile = path.join(runDir, "dataset-identity-preflight-run-report.json"), run = readJson(runFile);
532
573
  const resolve = (value) => {
533
574
  if (typeof value !== "string")
@@ -672,5 +713,5 @@ function acceptRetainedTarget(context, qualified, target, claim, authFile, runDi
672
713
  if (!validateBoundExecutionManifest(manifest, binding, reportText).ok ||
673
714
  stableJson(manifest) !== stableJson(checked.manifest))
674
715
  reject("retained-execution-manifest");
675
- return run;
716
+ return { run, row: target.row };
676
717
  }