@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
@@ -1,8 +1,14 @@
1
1
  import { spawnSync } from "node:child_process";
2
+ import fs from "node:fs";
3
+ import os from "node:os";
2
4
  import path from "node:path";
3
5
  import process from "node:process";
6
+ import { buildFoundryCurationQueue } from "../foundry-curation-queue.js";
7
+ import { applyTypedAuthoringPatch } from "../foundry-typed-authoring-patch.js";
8
+ import { runTypedDatasetQa } from "../foundry-typed-flow-qa.js";
9
+ import { createFoundryIsolatedChildEnvironment } from "../foundry-runtime-environment.js";
4
10
  export function createCliWrapperCommands({ executionEnvironment = process.env, executionCwd, appendOption, appendRepeatedOptions, repoRoot, resolveTiangongLcaCliCommand, resolveTiangongLcaCliBin, }) {
5
- function runJsonCli(cliArgs, errorMessage) {
11
+ function runJsonCli(cliArgs, errorMessage, temporary) {
6
12
  const cli = resolveTiangongLcaCliCommand
7
13
  ? resolveTiangongLcaCliCommand()
8
14
  : {
@@ -13,8 +19,10 @@ export function createCliWrapperCommands({ executionEnvironment = process.env, e
13
19
  };
14
20
  const spawnArgs = [...cli.args, ...cliArgs];
15
21
  const result = spawnSync(cli.command, spawnArgs, {
16
- cwd: executionCwd ?? repoRoot,
17
- env: executionEnvironment,
22
+ cwd: temporary ?? executionCwd ?? repoRoot,
23
+ env: temporary
24
+ ? createFoundryIsolatedChildEnvironment({ tempRoot: temporary })
25
+ : executionEnvironment,
18
26
  encoding: "utf8",
19
27
  // Mega-scope stages emit large JSON payloads on stdout; the 1MB spawnSync
20
28
  // default overflows with ENOBUFS. Cap below V8's max string length.
@@ -46,6 +54,57 @@ export function createCliWrapperCommands({ executionEnvironment = process.env, e
46
54
  stderr: result.stderr || "",
47
55
  };
48
56
  }
57
+ const localPath = (name) => path.resolve(executionCwd ?? repoRoot, String(name));
58
+ function writeLocalText(file, value) {
59
+ fs.mkdirSync(path.dirname(file), { recursive: true });
60
+ fs.writeFileSync(file, value, { mode: 0o600 });
61
+ }
62
+ const localStage = (temporary) => ({
63
+ workspace: temporary,
64
+ readInput: fs.readFileSync,
65
+ readRowsFile: (file) => fs.readFileSync(file, "utf8"),
66
+ readResult(file) {
67
+ if (!fs.lstatSync(file).isFile())
68
+ throw new Error("Expected a regular typed CLI result file.");
69
+ return fs.readFileSync(file);
70
+ },
71
+ writeJson: (file, value) => writeLocalText(file, JSON.stringify(value, null, 2) + "\n"),
72
+ run(args) {
73
+ const { exitCode, report } = runJsonCli(args, "Typed CLI stage did not emit JSON.", temporary);
74
+ return { exit: exitCode, report, validate: (check) => check() };
75
+ },
76
+ });
77
+ function runDatasetQaStage(stage, argv) {
78
+ const qaType = argv[0] === "qa" ? argv[1] : null;
79
+ if (qaType !== "flow" && qaType !== "process" && qaType !== "lifecyclemodel")
80
+ return null;
81
+ const cli = resolveTiangongLcaCliCommand?.();
82
+ if (cli?.package_version !== "0.4.0")
83
+ return null;
84
+ const rowsFile = localPath(argv[argv.indexOf("--rows-file") + 1]);
85
+ const directory = localPath(argv[argv.indexOf("--out-dir") + 1]);
86
+ const referenceFiles = argv.flatMap((value, index) => value === "--reference-rows-file" ? [localPath(argv[index + 1])] : []);
87
+ const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "foundry-local-qa-"));
88
+ try {
89
+ const result = runTypedDatasetQa(qaType, rowsFile, directory, { referenceFiles }, localStage(temporary));
90
+ return {
91
+ stage,
92
+ exit_code: result.exit || (qaType !== "process" && result.report.findings.length) ? 2 : 0,
93
+ report: result.report,
94
+ report_file: result.reportFile,
95
+ ...(qaType === "process"
96
+ ? { cli_validation: { report: result.validationReportFile, exit: result.exit } }
97
+ : {}),
98
+ status: result.report.status,
99
+ command: cli.command,
100
+ args: [...cli.args, "dataset", qaType === "lifecyclemodel" ? "model" : qaType, "validate"],
101
+ stderr: "",
102
+ };
103
+ }
104
+ finally {
105
+ fs.rmSync(temporary, { recursive: true, force: true });
106
+ }
107
+ }
49
108
  function runDatasetCurationQueueBuild(options) {
50
109
  if (options.help) {
51
110
  return {
@@ -73,6 +132,33 @@ export function createCliWrapperCommands({ executionEnvironment = process.env, e
73
132
  appendRepeatedOptions(cliArgs, "--exclude-process-id", options.excludeProcessId || options.excludeProcessIds);
74
133
  appendOption(cliArgs, "--process-limit", options.processLimit);
75
134
  appendOption(cliArgs, "--out-dir", outDir);
135
+ const cliVersion = resolveTiangongLcaCliCommand?.().package_version;
136
+ if (cliVersion === "0.4.0") {
137
+ const cwd = executionCwd ?? repoRoot;
138
+ const file = (name) => path.resolve(cwd, name);
139
+ const repeated = (flag) => cliArgs.flatMap((name, index) => (name === flag ? [cliArgs[index + 1]] : []));
140
+ const processLimit = repeated("--process-limit")[0];
141
+ const limit = processLimit === undefined ? undefined : Number.parseInt(processLimit, 10);
142
+ if (limit !== undefined && (!Number.isInteger(limit) || limit < 1))
143
+ throw new Error("--process-limit must be a positive integer.");
144
+ const [processesFile] = repeated("--processes");
145
+ if (!processesFile)
146
+ throw new Error("--processes requires a Process rows file.");
147
+ const [flowsFile] = repeated("--flows");
148
+ const report = buildFoundryCurationQueue({
149
+ processes: file(processesFile),
150
+ flows: flowsFile ? file(flowsFile) : null,
151
+ support: repeated("--support").map(file),
152
+ external: repeated("--external-flow-ref").map(file),
153
+ excludeProcessIds: repeated("--exclude-process-id"),
154
+ processLimit: limit,
155
+ outDir: file(String(outDir)),
156
+ });
157
+ return {
158
+ ...report,
159
+ foundry_wrapper: { exit_code: report.blockers.length ? 1 : 0, owner: "foundry" },
160
+ };
161
+ }
76
162
  const { cliBin, cli, spawnArgs, exitCode, report, stderr } = runJsonCli(cliArgs, "tiangong-lca dataset curation-queue build did not emit JSON.");
77
163
  return {
78
164
  ...report,
@@ -110,6 +196,39 @@ export function createCliWrapperCommands({ executionEnvironment = process.env, e
110
196
  const patch = options.patch || options.patchFile || options.patches || options.suggestions;
111
197
  const outDir = (options.outDir || ".foundry/workspaces/dataset-patch-apply");
112
198
  const out = options.out || options.outFile || path.join(outDir, "patched-rows.jsonl");
199
+ if (resolveTiangongLcaCliCommand?.().package_version === "0.4.0") {
200
+ if (!input || !patch)
201
+ throw new Error("Dataset patch apply requires --input and --patch.");
202
+ const packageDir = options.authoringPackageDir || options.authoringPackagesDir;
203
+ const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "foundry-local-patch-"));
204
+ const io = localStage(temporary);
205
+ try {
206
+ const result = applyTypedAuthoringPatch({
207
+ input: localPath(input),
208
+ patch: localPath(patch),
209
+ output: localPath(out),
210
+ directory: localPath(outDir),
211
+ packageDirectory: packageDir ? localPath(packageDir) : undefined,
212
+ requirePackage: options.requireAuthoringPackage === true ||
213
+ options.requireAuthoringPackage === "true",
214
+ requireClosure: options.requireActionItemClosure === true ||
215
+ options.requireActionItemClosure === "true",
216
+ writeText: writeLocalText,
217
+ writeJson: io.writeJson,
218
+ }, io);
219
+ return {
220
+ ...result.report,
221
+ foundry_wrapper: {
222
+ exit_code: result.exit,
223
+ owner: "tiangong-lca-cli",
224
+ remote_write_mode: "read-only",
225
+ },
226
+ };
227
+ }
228
+ finally {
229
+ fs.rmSync(temporary, { recursive: true, force: true });
230
+ }
231
+ }
113
232
  const cliArgs = ["dataset", "patch", "apply", "--json"];
114
233
  appendOption(cliArgs, "--input", input);
115
234
  appendOption(cliArgs, "--patch", patch);
@@ -142,5 +261,6 @@ export function createCliWrapperCommands({ executionEnvironment = process.env, e
142
261
  return {
143
262
  runDatasetCurationQueueBuild,
144
263
  runDatasetPatchApply,
264
+ runDatasetQaStage,
145
265
  };
146
266
  }
@@ -81,9 +81,10 @@ export function readNativeDraftHandoff(input) {
81
81
  token(action.id) !== id ||
82
82
  token(action.version) !== version ||
83
83
  identity.id !== id ||
84
- identity.version !== version ||
85
- action.desired_sha256 !== sha256Json(identity.payload))
86
- fail("Contract action does not bind the exact final payload, identity and draft operation.");
84
+ identity.version !== version)
85
+ fail("Contract action does not bind the exact identity and draft operation.");
86
+ if (action.desired_sha256 !== sha256Json(identity.payload))
87
+ fail("Action desired_sha256 must match the canonical digest of the parsed dataset payload. File selection uses the SHA-256 of raw file bytes.");
87
88
  if (!Array.isArray(action.dependency_action_ids))
88
89
  fail("Action dependencies must be explicit.");
89
90
  const dependencies = action.dependency_action_ids.map(token);
@@ -104,6 +104,10 @@ interface FinalizeStage extends JsonRecord {
104
104
  stderr?: string;
105
105
  report?: FinalizeStage;
106
106
  report_file?: string | null;
107
+ cli_validation?: {
108
+ report: string;
109
+ exit: number;
110
+ };
107
111
  rust_report?: unknown;
108
112
  binary_version?: unknown;
109
113
  counts?: CountMap;
@@ -231,5 +235,10 @@ interface FinalizeDependencies {
231
235
  }
232
236
  export declare function createPostAuthoringFinalizeCommands({ appendOption, applyCanonicalSupportRewrites, applyIdentityReferenceRewrites, asText, profileFor, blockersFromLocationAuditStage, buildLibraryContactPayload, booleanOption, compactStageReport, cloneJson, contactGlobalReference, datasetIdentity, datasetRowsFileStem, ensureArray, externalizeUnresolvedProcessFlowExchanges, fileExists, normalizedList, nowIso, postAuthoringPrewriteGateBlockers, processSourceReferenceRows, profileFullContextRequirement, repoRelativeMaybe, loadCanonicalSupportCache, repoRelativePath, repoRoot, reportFileFromCliStage, readRowsFile, readReferenceBytes, repairTrueSourceClassification, repairTrueSourceDescription, repairTrueSourceIdentity, resolveRepoPath, rewriteCanonicalSourceReferences, rewriteContactReferences, rewriteTrueSourceReferenceDescriptions, runDatasetCommitHandoffPlan, runDatasetCurationCleanup, runDatasetCurationGate, runDatasetMutationManifest, runFinalizeAutoCurationQueue, runFinalizeIdentityPreflightStage, runTidasRowsValidation, runTiangongJsonStage, skippedPrewriteStage, sourceReferenceSemanticBlockers, sourceReferenceRewritesFileForRowsFile, sourceSemanticSummary, unique, writeFinalizeImportLedger, writeJson, writeJsonLines, }: FinalizeDependencies): {
233
237
  runDatasetPostAuthoringFinalize: (options: FinalizeOptions) => FinalizeStage;
238
+ finalizeStages: (options: FinalizeOptions, asynchronousIdentity: boolean) => Generator<{
239
+ rowsFile: string;
240
+ outDir: string;
241
+ options: FinalizeOptions;
242
+ }, FinalizeStage, FinalizeStage>;
234
243
  };
235
244
  export {};
@@ -623,7 +623,7 @@ export function createPostAuthoringFinalizeCommands({ appendOption, applyCanonic
623
623
  output_rows_file: repoRelativePath(outputRowsFile),
624
624
  };
625
625
  }
626
- function runDatasetPostAuthoringFinalize(options) {
626
+ function* finalizeStages(options, asynchronousIdentity) {
627
627
  const finalizeStartedAtUtc = nowIso();
628
628
  const finalizeStartedAtMs = Date.now();
629
629
  const finalizeTimings = [];
@@ -1038,14 +1038,27 @@ export function createPostAuthoringFinalizeCommands({ appendOption, applyCanonic
1038
1038
  if (sourceContactSupportFinalizeRequested) {
1039
1039
  sourceContactSupportFinalize = timeStage("source_contact_support_finalize", finalizeSourceContactSupport);
1040
1040
  }
1041
- const identityPreflightRunStage = timeStage("identity_preflight_run", () => runFinalizeIdentityPreflightStage({
1041
+ const identityRequest = {
1042
1042
  rowsFile: cleanedRowsFile,
1043
1043
  outDir,
1044
- options: {
1045
- ...options,
1046
- type: datasetType,
1047
- },
1048
- }));
1044
+ options: { ...options, type: datasetType },
1045
+ };
1046
+ let identityPreflightRunStage;
1047
+ if (asynchronousIdentity && booleanOption(options.runIdentityPreflight)) {
1048
+ const started_at_utc = nowIso();
1049
+ const startedAt = Date.now();
1050
+ identityPreflightRunStage = yield identityRequest;
1051
+ finalizeTimings.push({
1052
+ stage: "identity_preflight_run",
1053
+ status: stageStatus(identityPreflightRunStage),
1054
+ started_at_utc,
1055
+ finished_at_utc: nowIso(),
1056
+ duration_ms: Date.now() - startedAt,
1057
+ });
1058
+ }
1059
+ else {
1060
+ identityPreflightRunStage = timeStage("identity_preflight_run", () => runFinalizeIdentityPreflightStage(identityRequest));
1061
+ }
1049
1062
  const curationQueueStage = timeStage("curation_queue", () => runFinalizeAutoCurationQueue({
1050
1063
  datasetType,
1051
1064
  rowsFile,
@@ -1151,6 +1164,7 @@ export function createPostAuthoringFinalizeCommands({ appendOption, applyCanonic
1151
1164
  const curationGate = timeStage("post_authoring_curation_gate", () => requiresCurationGate
1152
1165
  ? runDatasetCurationGate({
1153
1166
  repoRoot,
1167
+ cliValidation: qaStage.cli_validation,
1154
1168
  options: {
1155
1169
  ...options,
1156
1170
  type: datasetType,
@@ -1314,6 +1328,15 @@ export function createPostAuthoringFinalizeCommands({ appendOption, applyCanonic
1314
1328
  if (prewriteGateReady) {
1315
1329
  stage.report_file = reportFileFromCliStage(stage, ["files.report"], path.join(remoteOutDir, "outputs", "remote-verification-report.json"));
1316
1330
  remoteVerifyReportFile = stage.report_file;
1331
+ if (stage.report?.schema === "tiangong-foundry.edge-support-precommit.v1" &&
1332
+ stage.status !== "passed_remote_verification") {
1333
+ prewriteGateBlockers.push({
1334
+ code: "edge_precommit_verification_blocked",
1335
+ stage: "remote_verify_precommit",
1336
+ message: "Resolve the exact remote identity and dependency checks before requesting write approval.",
1337
+ report: repoRelativeMaybe(remoteVerifyReportFile),
1338
+ });
1339
+ }
1317
1340
  }
1318
1341
  return stage;
1319
1342
  });
@@ -1756,5 +1779,11 @@ export function createPostAuthoringFinalizeCommands({ appendOption, applyCanonic
1756
1779
  },
1757
1780
  };
1758
1781
  }
1759
- return { runDatasetPostAuthoringFinalize };
1782
+ function runDatasetPostAuthoringFinalize(options) {
1783
+ const result = finalizeStages(options, false).next();
1784
+ if (!result.done)
1785
+ throw new Error("Unexpected asynchronous identity stage.");
1786
+ return result.value;
1787
+ }
1788
+ return { runDatasetPostAuthoringFinalize, finalizeStages };
1760
1789
  }
@@ -45,7 +45,7 @@ export function createFoundryAuthenticationEnvironment(authentication, sessionRe
45
45
  else {
46
46
  if (!authentication.accessToken || !authentication.apiBaseUrl || !authentication.publishableKey)
47
47
  reject("headless_target_required", "Headless mode requires the existing CLI explicit target and process-only actor token.");
48
- environment.TIANGONG_LCA_AUTH_MODE = "access_token";
48
+ environment.TIANGONG_LCA_AUTH_MODE = "access-token";
49
49
  environment.TIANGONG_LCA_ACCESS_TOKEN = authentication.accessToken;
50
50
  environment.TIANGONG_LCA_API_BASE_URL = authentication.apiBaseUrl;
51
51
  environment.TIANGONG_LCA_SUPABASE_PUBLISHABLE_KEY = authentication.publishableKey;
@@ -0,0 +1,73 @@
1
+ type Entity = "support" | "flow" | "process";
2
+ type QueueOptions = {
3
+ processes: string;
4
+ flows?: string | null;
5
+ support?: readonly string[];
6
+ external?: readonly string[];
7
+ excludeProcessIds?: readonly string[];
8
+ processLimit?: number;
9
+ outDir: string;
10
+ readInput?: (file: string) => Buffer;
11
+ };
12
+ export declare function buildFoundryCurationQueue(options: QueueOptions): {
13
+ schema_version: number;
14
+ producer: string;
15
+ generated_at_utc: string;
16
+ status: string;
17
+ out_dir: string;
18
+ inputs: {
19
+ processes: string;
20
+ flows: string | null;
21
+ support: string[];
22
+ external_flow_refs: string[];
23
+ };
24
+ counts: {
25
+ support_rows: number;
26
+ flow_rows: number;
27
+ process_rows: number;
28
+ external_flow_refs: number;
29
+ tasks: number;
30
+ blockers: number;
31
+ };
32
+ hashes: {
33
+ inputs: Record<string, string>;
34
+ task_order: string;
35
+ };
36
+ files: {
37
+ manifest: string;
38
+ tasks: string;
39
+ blockers: string;
40
+ locks: string;
41
+ };
42
+ tasks: {
43
+ schema_version: number;
44
+ entity_type: Entity;
45
+ task_id: string;
46
+ entity_id: string;
47
+ version: string;
48
+ lock_key: string;
49
+ depends_on: string[];
50
+ work_dir: string;
51
+ input_rows_file: string;
52
+ checkpoint_file: string;
53
+ run_plan_file: string;
54
+ closure_file: string;
55
+ }[];
56
+ blockers: {
57
+ schema_version: number;
58
+ code: string;
59
+ severity: string;
60
+ entity_type: string;
61
+ entity_id: string;
62
+ version: string;
63
+ message: string;
64
+ details: {
65
+ missing_flow_refs: {
66
+ id: string;
67
+ version: string | null;
68
+ path: string;
69
+ }[];
70
+ };
71
+ }[];
72
+ };
73
+ export {};
@@ -0,0 +1,261 @@
1
+ import { createHash } from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { FoundryContextError } from "./foundry-runtime-error.js";
5
+ import { datasetIdentity, detectDatasetType, unwrapDatasetPayload, } from "./import-curation/internal/dataset-payload.js";
6
+ import { asText, readRows } from "./import-curation/internal/runtime-io.js";
7
+ import { collectCommonOtherTraceEntries } from "./import-curation/internal/trace-summary.js";
8
+ import { collectDatasetReferences } from "./import-curation/internal/workflow-reference-closure.js";
9
+ const layout = {
10
+ support: { dir: "supports", stages: ["identity", "schema", "qa_or_profile", "checkpoint"] },
11
+ flow: { dir: "flows", stages: ["identity", "name_plan", "schema", "qa", "checkpoint"] },
12
+ process: {
13
+ dir: "processes",
14
+ stages: [
15
+ "dependency_closure",
16
+ "reference_refresh",
17
+ "required_fields",
18
+ "schema",
19
+ "qa",
20
+ "curation",
21
+ "remote_dry_run",
22
+ "readback",
23
+ ],
24
+ },
25
+ };
26
+ function sha256(data) {
27
+ return createHash("sha256").update(data).digest("hex");
28
+ }
29
+ function record(value) {
30
+ return value && typeof value === "object" && !Array.isArray(value)
31
+ ? value
32
+ : {};
33
+ }
34
+ function invalid(message) {
35
+ throw new FoundryContextError("workflow_queue_invalid", message);
36
+ }
37
+ function writeText(file, contents) {
38
+ fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
39
+ fs.writeFileSync(file, contents, { mode: 0o600, flag: "wx" });
40
+ }
41
+ function writeJson(file, value) {
42
+ writeText(file, JSON.stringify(value, null, 2) + "\n");
43
+ }
44
+ function writeRows(file, rows) {
45
+ writeText(file, rows.map((row) => JSON.stringify(row) + "\n").join(""));
46
+ }
47
+ const versionKey = (id, version) => JSON.stringify([id, version]);
48
+ const refPath = (pointer) => pointer.replace(/^\/+|\/+$/gu, "").replace(/\//gu, ".");
49
+ export function buildFoundryCurationQueue(options) {
50
+ const outDir = path.resolve(options.outDir);
51
+ const files = {
52
+ processes: path.resolve(options.processes),
53
+ flows: options.flows ? path.resolve(options.flows) : null,
54
+ support: (options.support ?? []).map((file) => path.resolve(file)),
55
+ external_flow_refs: (options.external ?? []).map((file) => path.resolve(file)),
56
+ };
57
+ const hashes = {};
58
+ const read = (file) => {
59
+ const bytes = (options.readInput ?? fs.readFileSync)(file);
60
+ hashes[file] = sha256(bytes);
61
+ return readRows(file, () => bytes.toString("utf8"));
62
+ };
63
+ const taskIds = new Set();
64
+ const workDirs = new Set();
65
+ const excluded = new Set(options.excludeProcessIds);
66
+ const entities = (file, type) => {
67
+ const selected = read(file).flatMap((row, index) => {
68
+ const kind = type === "support" ? detectDatasetType(unwrapDatasetPayload(row, "support")) : type;
69
+ if (!kind)
70
+ return invalid("Support queue rows need a recognized dataset type.");
71
+ const identity = datasetIdentity(row, index, kind);
72
+ if (!identity.id || !identity.version || /^row-\d+$/u.test(identity.id))
73
+ return invalid("Queue rows need a stable identity.");
74
+ return type === "process" && excluded.has(identity.id) ? [] : [{ row, index, identity }];
75
+ });
76
+ return (type === "process" ? selected.slice(0, options.processLimit) : selected).map(({ row, index, identity: { id, version, payload } }) => {
77
+ const taskId = `${type}:${id}@${version}`;
78
+ const token = (value) => value.replace(/[^A-Za-z0-9._-]+/gu, "_");
79
+ const workDir = path.join(outDir, "entities", layout[type].dir, `${token(id)}__${token(version)}`);
80
+ if (taskIds.has(taskId) || workDirs.has(workDir))
81
+ return invalid("The queue has conflicting rows for one task or path.");
82
+ taskIds.add(taskId);
83
+ workDirs.add(workDir);
84
+ const task = {
85
+ schema_version: 1,
86
+ entity_type: type,
87
+ task_id: taskId,
88
+ entity_id: id,
89
+ version,
90
+ lock_key: taskId,
91
+ depends_on: [],
92
+ work_dir: workDir,
93
+ input_rows_file: path.join(workDir, "input.jsonl"),
94
+ checkpoint_file: path.join(workDir, "checkpoint.json"),
95
+ run_plan_file: path.join(workDir, "entity-run-plan.json"),
96
+ closure_file: path.join(workDir, "closure.json"),
97
+ };
98
+ return { task, row, payload, source: { file, index } };
99
+ });
100
+ };
101
+ const support = files.support.flatMap((file) => entities(file, "support"));
102
+ const flows = files.flows ? entities(files.flows, "flow") : [];
103
+ const processes = entities(files.processes, "process");
104
+ const externalRows = files.external_flow_refs.flatMap(read);
105
+ const external = new Set(externalRows.map((row) => {
106
+ const source = record(row);
107
+ const id = asText(source.id ?? source.dataset_id ?? source.uuid);
108
+ if (!id)
109
+ return invalid("External Flow evidence requires a stable identity.");
110
+ return versionKey(id, asText(source.version) || null);
111
+ }));
112
+ const byFlowVersion = new Map(flows.map(({ task }) => [versionKey(task.entity_id, task.version), task]));
113
+ const flowCounts = new Map();
114
+ const firstByFlow = new Map();
115
+ for (const { task } of flows) {
116
+ flowCounts.set(task.entity_id, (flowCounts.get(task.entity_id) ?? 0) + 1);
117
+ if (!firstByFlow.has(task.entity_id))
118
+ firstByFlow.set(task.entity_id, task);
119
+ }
120
+ const blockers = [];
121
+ const all = [...support, ...flows, ...processes];
122
+ for (const item of all) {
123
+ const { task, row, payload, source } = item;
124
+ const localTasks = [], externalRefs = [], deferredRefs = [], unresolvedRefs = [];
125
+ if (task.entity_type === "process") {
126
+ const deferred = new Map(collectCommonOtherTraceEntries(payload, "tiangongfoundry:unresolvedTrace")
127
+ .map(record)
128
+ .filter((trace) => (trace.action_item_code ?? trace.actionItemCode) ===
129
+ "elementary_flow_identity_manual_review")
130
+ .map((trace) => [
131
+ JSON.stringify([
132
+ asText(trace.reference_id ?? trace.referenceId ?? trace.ref_object_id),
133
+ asText(trace.reference_version ?? trace.referenceVersion ?? trace.version) || null,
134
+ refPath(asText(trace.blocked_path ?? trace.blockedPath ?? trace.path)),
135
+ ]),
136
+ trace,
137
+ ]));
138
+ const missing = [];
139
+ const refs = collectDatasetReferences(payload).filter((ref) => ref.table === "flows");
140
+ for (const ref of refs) {
141
+ const version = ref.version || null;
142
+ const key = versionKey(ref.id, version);
143
+ const location = refPath(ref.path);
144
+ const target = version
145
+ ? byFlowVersion.get(key)
146
+ : flowCounts.get(ref.id) === 1
147
+ ? firstByFlow.get(ref.id)
148
+ : undefined;
149
+ const base = { entity_type: "flow", entity_id: ref.id, version, ref_path: location };
150
+ const trace = deferred.get(JSON.stringify([ref.id, version, location]));
151
+ if (target) {
152
+ localTasks.push({ ...base, version: target.version, task_id: target.task_id });
153
+ }
154
+ else if (version && external.has(key)) {
155
+ externalRefs.push(base);
156
+ }
157
+ else if (trace) {
158
+ deferredRefs.push({
159
+ ...base,
160
+ action_item_code: "elementary_flow_identity_manual_review",
161
+ reason: asText(trace.reason) || null,
162
+ });
163
+ }
164
+ else {
165
+ unresolvedRefs.push(base);
166
+ missing.push({ id: ref.id, version, path: location });
167
+ }
168
+ }
169
+ if (missing.length)
170
+ blockers.push({
171
+ schema_version: 1,
172
+ code: "process_flow_reference_unresolved",
173
+ severity: "blocker",
174
+ entity_type: "process",
175
+ entity_id: task.entity_id,
176
+ version: task.version,
177
+ message: "Process Flow dependencies require one local task or exact external Flow evidence.",
178
+ details: { missing_flow_refs: missing },
179
+ });
180
+ }
181
+ task.depends_on = [...new Set(localTasks.map((entry) => entry.task_id))].sort();
182
+ writeRows(task.input_rows_file, [row]);
183
+ writeJson(task.closure_file, {
184
+ schema_version: 1,
185
+ entity_type: task.entity_type,
186
+ entity_id: task.entity_id,
187
+ version: task.version,
188
+ source,
189
+ dependencies: {
190
+ local_tasks: localTasks,
191
+ external_refs: externalRefs,
192
+ deferred_refs: deferredRefs,
193
+ unresolved_refs: unresolvedRefs,
194
+ },
195
+ });
196
+ writeJson(task.run_plan_file, {
197
+ schema_version: 1,
198
+ task_id: task.task_id,
199
+ entity_type: task.entity_type,
200
+ entity_id: task.entity_id,
201
+ version: task.version,
202
+ input_rows_file: task.input_rows_file,
203
+ checkpoint_file: task.checkpoint_file,
204
+ stages: layout[task.entity_type].stages.map((id) => ({
205
+ id,
206
+ status: "pending",
207
+ checkpoint_file: path.join(task.work_dir, "checkpoints", `${id}.json`),
208
+ })),
209
+ ai_authoring_policy: {
210
+ output_only: "structured_patch_or_build_plan",
211
+ deterministic_apply_required: true,
212
+ remote_write_allowed: false,
213
+ },
214
+ });
215
+ }
216
+ const tasks = all.map(({ task }) => task);
217
+ const output = path.join(outDir, "outputs");
218
+ const artifacts = {
219
+ manifest: path.join(output, "curation-queue-manifest.json"),
220
+ tasks: path.join(output, "curation-queue-tasks.jsonl"),
221
+ blockers: path.join(output, "curation-queue-blockers.jsonl"),
222
+ locks: path.join(output, "curation-queue-locks.json"),
223
+ };
224
+ const generated = new Date().toISOString();
225
+ const report = {
226
+ schema_version: 1,
227
+ producer: "foundry",
228
+ generated_at_utc: generated,
229
+ status: blockers.length ? "blocked" : "ready",
230
+ out_dir: outDir,
231
+ inputs: files,
232
+ counts: {
233
+ support_rows: support.length,
234
+ flow_rows: flows.length,
235
+ process_rows: processes.length,
236
+ external_flow_refs: externalRows.length,
237
+ tasks: tasks.length,
238
+ blockers: blockers.length,
239
+ },
240
+ hashes: { inputs: hashes, task_order: sha256(tasks.map((task) => task.task_id).join("\n")) },
241
+ files: artifacts,
242
+ tasks,
243
+ blockers,
244
+ };
245
+ writeRows(artifacts.tasks, tasks);
246
+ writeRows(artifacts.blockers, blockers);
247
+ writeJson(artifacts.locks, {
248
+ schema_version: 1,
249
+ generated_at_utc: generated,
250
+ locks: tasks.map(({ task_id, entity_type, entity_id, version, lock_key }) => ({
251
+ task_id,
252
+ entity_type,
253
+ entity_id,
254
+ version,
255
+ lock_key,
256
+ status: "available",
257
+ })),
258
+ });
259
+ writeJson(artifacts.manifest, report);
260
+ return report;
261
+ }
@@ -0,0 +1,17 @@
1
+ import { type FoundryInputFact, type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
2
+ import { type EdgeSupportPlan, type EdgeSupportTarget } from "./foundry-edge-support-plan.ts";
3
+ export declare function verifySelectedEdgeDraftPlan(context: FoundryRuntimeContext, plan: EdgeSupportPlan, approved: FoundryInputFact, target: EdgeSupportTarget): {
4
+ root: {
5
+ table: string;
6
+ id: string;
7
+ version: string;
8
+ payload_sha256: string;
9
+ references: {
10
+ table: string;
11
+ id: string;
12
+ version: string;
13
+ }[];
14
+ };
15
+ content: Record<string, unknown>;
16
+ beforeContent?: Record<string, unknown> | undefined;
17
+ }[];