@tiangong-lca/foundry 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +13 -11
  2. package/docs/architecture.md +12 -10
  3. package/docs/foundry-task-contracts.md +16 -4
  4. package/docs/import-profiles/bafu/constraints.md +1 -1
  5. package/docs/import-profiles/bafu/profile.md +1 -1
  6. package/docs/import-profiles/worldsteel/constraints.md +3 -3
  7. package/docs/import-profiles/worldsteel/import-coverage.md +3 -3
  8. package/docs/import-profiles/worldsteel/import-plan.md +4 -4
  9. package/docs/import-profiles/worldsteel/profile.md +3 -3
  10. package/docs/package-distribution-contract.md +11 -9
  11. package/docs/public-runtime-contract.md +31 -9
  12. package/docs/runtime-context-contract.md +7 -5
  13. package/docs/task-authorization-contract.md +5 -3
  14. package/package-dist/assets/foundry-package-descriptor.json +300 -154
  15. package/package-dist/scripts/foundry-facade.d.ts +1 -0
  16. package/package-dist/scripts/foundry-facade.js +233 -28
  17. package/package-dist/scripts/foundry-runtime.d.ts +14 -3
  18. package/package-dist/scripts/foundry-runtime.js +2 -2
  19. package/package-dist/scripts/lib/finalize-owners/commit-handoff.js +3 -3
  20. package/package-dist/scripts/lib/finalize-owners/{native-insert-closeout.d.ts → native-draft-closeout.d.ts} +1 -1
  21. package/package-dist/scripts/lib/finalize-owners/{native-insert-closeout.js → native-draft-closeout.js} +10 -6
  22. package/package-dist/scripts/lib/finalize-owners/{native-insert-handoff.d.ts → native-draft-handoff.d.ts} +7 -5
  23. package/package-dist/scripts/lib/finalize-owners/{native-insert-handoff.js → native-draft-handoff.js} +30 -11
  24. package/package-dist/scripts/lib/finalize-owners/post-write-closeout.js +2 -2
  25. package/package-dist/scripts/lib/foundry-authorization-input.d.ts +10 -1
  26. package/package-dist/scripts/lib/foundry-authorization-input.js +27 -3
  27. package/package-dist/scripts/lib/foundry-command-metadata.js +25 -2
  28. package/package-dist/scripts/lib/foundry-command-registry.d.ts +4 -4
  29. package/package-dist/scripts/lib/foundry-command-registry.js +3 -0
  30. package/package-dist/scripts/lib/foundry-execution-admission.js +3 -3
  31. package/package-dist/scripts/lib/foundry-facade-store.js +56 -6
  32. package/package-dist/scripts/lib/foundry-interaction-input.d.ts +19 -0
  33. package/package-dist/scripts/lib/foundry-interaction-input.js +327 -0
  34. package/package-dist/scripts/lib/foundry-interaction-projection.d.ts +22 -0
  35. package/package-dist/scripts/lib/foundry-interaction-projection.js +81 -0
  36. package/package-dist/scripts/lib/foundry-interaction-types.d.ts +17 -0
  37. package/package-dist/scripts/lib/foundry-interaction-types.js +4 -0
  38. package/package-dist/scripts/lib/foundry-owner-execution-store.d.ts +33 -16
  39. package/package-dist/scripts/lib/foundry-owner-execution-store.js +66 -5
  40. package/package-dist/scripts/lib/foundry-owner-readback.d.ts +3 -0
  41. package/package-dist/scripts/lib/foundry-owner-readback.js +101 -27
  42. package/package-dist/scripts/lib/foundry-package-contract.d.ts +4 -4
  43. package/package-dist/scripts/lib/foundry-package-contract.js +4 -2
  44. package/package-dist/scripts/lib/foundry-repair-execution.d.ts +11 -0
  45. package/package-dist/scripts/lib/foundry-repair-execution.js +70 -0
  46. package/package-dist/scripts/lib/foundry-repair-handoff.d.ts +16 -0
  47. package/package-dist/scripts/lib/foundry-repair-handoff.js +108 -0
  48. package/package-dist/scripts/lib/foundry-repair-preflight.d.ts +96 -0
  49. package/package-dist/scripts/lib/foundry-repair-preflight.js +313 -0
  50. package/package-dist/scripts/lib/foundry-repair-reference-binding.d.ts +26 -0
  51. package/package-dist/scripts/lib/foundry-repair-reference-binding.js +120 -0
  52. package/package-dist/scripts/lib/foundry-repair-reference-proof.d.ts +63 -0
  53. package/package-dist/scripts/lib/foundry-repair-reference-proof.js +329 -0
  54. package/package-dist/scripts/lib/foundry-repair-scope.d.ts +76 -0
  55. package/package-dist/scripts/lib/foundry-repair-scope.js +304 -0
  56. package/package-dist/scripts/lib/foundry-runtime-command-policy.js +1 -0
  57. package/package-dist/scripts/lib/foundry-runtime-qualification.js +2 -2
  58. package/package-dist/scripts/lib/foundry-runtime-utils.d.ts +3 -0
  59. package/package-dist/scripts/lib/foundry-runtime-utils.js +6 -1
  60. package/package-dist/scripts/lib/foundry-semantic-input.d.ts +2 -0
  61. package/package-dist/scripts/lib/foundry-semantic-input.js +29 -2
  62. package/package-dist/scripts/lib/foundry-semantic-interaction.d.ts +12 -0
  63. package/package-dist/scripts/lib/foundry-semantic-interaction.js +38 -0
  64. package/package-dist/scripts/lib/foundry-successor-predecessor.d.ts +36 -0
  65. package/package-dist/scripts/lib/foundry-successor-predecessor.js +399 -0
  66. package/package-dist/scripts/lib/foundry-task-registration.js +64 -3
  67. package/package-dist/scripts/lib/foundry-task-start-spec.d.ts +14 -0
  68. package/package-dist/scripts/lib/foundry-task-start-spec.js +60 -1
  69. package/package-dist/scripts/lib/foundry-task-store.d.ts +13 -1
  70. package/package-dist/scripts/lib/foundry-task-store.js +23 -12
  71. package/package-dist/scripts/lib/foundry-task-types.d.ts +22 -1
  72. package/package-dist/scripts/lib/foundry-tidas-source.d.ts +15 -0
  73. package/package-dist/scripts/lib/foundry-tidas-source.js +90 -0
  74. package/package-dist/scripts/lib/foundry-workflow-assessment.d.ts +7 -1
  75. package/package-dist/scripts/lib/foundry-workflow-assessment.js +127 -6
  76. package/package-dist/scripts/lib/foundry-workflow-authorization.d.ts +2 -1
  77. package/package-dist/scripts/lib/foundry-workflow-authorization.js +194 -26
  78. package/package-dist/scripts/lib/foundry-workflow-context.js +4 -0
  79. package/package-dist/scripts/lib/foundry-workflow-execution.js +6 -1
  80. package/package-dist/scripts/lib/foundry-workflow-finalize.js +7 -4
  81. package/package-dist/scripts/lib/foundry-workflow-interaction.d.ts +4 -0
  82. package/package-dist/scripts/lib/foundry-workflow-interaction.js +63 -0
  83. package/package-dist/scripts/lib/foundry-workflow-repair.d.ts +36 -0
  84. package/package-dist/scripts/lib/foundry-workflow-repair.js +580 -0
  85. package/package-dist/scripts/lib/foundry-workflow-semantic.js +21 -1
  86. package/package-dist/scripts/lib/foundry-workflow-state.d.ts +9 -1
  87. package/package-dist/scripts/lib/foundry-workflow-state.js +239 -39
  88. package/package-dist/scripts/lib/import-curation/curation-cleanup.js +39 -8
  89. package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
  90. package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.d.ts +13 -2
  91. package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.js +100 -29
  92. package/package-dist/scripts/lib/import-curation/internal/workflow-authoring-tasks.js +1 -2
  93. package/package-dist/scripts/lib/import-curation/internal/workflow-patch-collect.js +2 -3
  94. package/package-dist/scripts/lib/import-curation/internal/workflow-queue-context.js +8 -8
  95. package/package-dist/scripts/lib/import-curation/internal/workflow-semantic-actions.js +12 -2
  96. package/package-dist/scripts/public-api.d.ts +2 -0
  97. package/package-dist/scripts/public-api.js +1 -0
  98. package/package-dist/scripts/runtime-entry.js +9 -1
  99. package/package.json +3 -2
  100. package/specs/import-profiles.json +1 -1
  101. package/specs/schemas/foundry-authorization-input.schema.json +45 -2
  102. package/specs/schemas/foundry-interaction-input.schema.json +173 -0
  103. package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
  104. package/specs/schemas/foundry-package-descriptor.schema.json +3 -2
  105. package/specs/schemas/foundry-semantic-input.schema.json +12 -1
  106. package/specs/schemas/foundry-task-start.schema.json +275 -19
  107. package/specs/schemas/runtime-qualification.schema.json +1 -1
@@ -1,6 +1,18 @@
1
1
  import { type FoundryRuntimeContext, type FoundryInputFact } from "./foundry-runtime-context.ts";
2
2
  import type { JsonRecord, ArtifactEntry, LoadedTask, FoundryTaskOptions, FoundryTaskOperation } from "./foundry-task-types.ts";
3
3
  export type { FoundryTaskOptions, FoundryTaskOperation, FoundryTaskJob, } from "./foundry-task-types.ts";
4
+ /**
5
+ * The synchronous verified snapshot of one task: registered job, bound account intent, hash-chained
6
+ * artifact index and re-verified frozen sources. It is the same verification the locked metadata
7
+ * transaction performs, exposed for read-only projections that cannot await; callers that read a
8
+ * *foreign* task synchronously must re-check the snapshot's stability themselves.
9
+ */
10
+ export declare function readVerifiedTaskSnapshot(context: FoundryRuntimeContext, options?: {
11
+ verifyCommands?: readonly string[];
12
+ }): {
13
+ readonly task: LoadedTask;
14
+ readonly index: readonly ArtifactEntry[];
15
+ };
4
16
  export declare function withFoundryTaskMetadata<T>(context: FoundryRuntimeContext, inspect: (task: LoadedTask, index: readonly ArtifactEntry[]) => T, options?: {
5
17
  verifyCommands?: readonly string[];
6
18
  }): Promise<T>;
@@ -11,7 +23,7 @@ export declare function assertFoundryTaskInputLineage(context: FoundryRuntimeCon
11
23
  }>>;
12
24
  /** Short local preparation transaction. Remote mutation/batch execution must not use this replay path. */
13
25
  export declare function runFoundryTaskOperation(context: FoundryRuntimeContext, input: {
14
- command: "dataset-curation-cleanup" | "dataset-tidas-import" | "dataset-context-pack" | "dataset-workflow-rows" | "dataset-workflow-assessment" | "dataset-workflow-identity" | "dataset-workflow-finalize" | "dataset-workflow-authorization" | "dataset-workflow-native-contract" | "dataset-workflow-reference-input" | "dataset-workflow-execution-prepare" | "dataset-workflow-execution-result" | "dataset-workflow-execution-consume" | "dataset-workflow-execution-observation" | "dataset-workflow-reference-verify" | "dataset-semantic-apply";
26
+ command: "dataset-curation-cleanup" | "dataset-tidas-import" | "dataset-context-pack" | "dataset-workflow-rows" | "dataset-workflow-assessment" | "dataset-workflow-identity" | "dataset-workflow-finalize" | "dataset-workflow-authorization" | "dataset-workflow-native-contract" | "dataset-workflow-repair-preflight" | "dataset-workflow-reference-input" | "dataset-workflow-execution-prepare" | "dataset-workflow-execution-result" | "dataset-workflow-execution-consume" | "dataset-workflow-execution-observation" | "dataset-workflow-reference-verify" | "dataset-workflow-interaction" | "dataset-semantic-apply";
15
27
  options: JsonRecord;
16
28
  task?: FoundryTaskOptions;
17
29
  validateCurrent?: (index: readonly ArtifactEntry[]) => void;
@@ -6,21 +6,32 @@ import { captureFoundryInput, resolveFoundryInputPath, resolveFoundryOutput, wri
6
6
  import { sha256Json } from "./identity-preflight-proof.js";
7
7
  import { fail, object, exact, timestamp, bytes, digest, taskPath, relative, readTaskBytes, readTaskJson, reference, facts, sameFact, shaPattern, maxIndexBytes, } from "./foundry-task-io.js";
8
8
  import { requiredTask, createTask, loadTask, bindAccountIntent, } from "./foundry-task-registration.js";
9
+ /**
10
+ * The synchronous verified snapshot of one task: registered job, bound account intent, hash-chained
11
+ * artifact index and re-verified frozen sources. It is the same verification the locked metadata
12
+ * transaction performs, exposed for read-only projections that cannot await; callers that read a
13
+ * *foreign* task synchronously must re-check the snapshot's stability themselves.
14
+ */
15
+ export function readVerifiedTaskSnapshot(context, options = {}) {
16
+ requiredTask(context);
17
+ const task = loadTask(context, {});
18
+ bindAccountIntent(context);
19
+ const index = readIndex(context);
20
+ const extra = index
21
+ .filter((entry) => options.verifyCommands?.includes(entry.command))
22
+ .map((entry) => ({
23
+ path: taskPath(context, entry.path),
24
+ bytes: entry.bytes,
25
+ sha256: entry.sha256,
26
+ }));
27
+ verifyInputs(context, task, index, [...context.inputs, ...extra]);
28
+ return { task, index };
29
+ }
9
30
  export async function withFoundryTaskMetadata(context, inspect, options = {}) {
10
31
  requiredTask(context);
11
32
  const inspectCurrent = () => {
12
- const task = loadTask(context, {});
13
- bindAccountIntent(context);
14
- const index = readIndex(context);
15
- const extra = index
16
- .filter((entry) => options.verifyCommands?.includes(entry.command))
17
- .map((entry) => ({
18
- path: taskPath(context, entry.path),
19
- bytes: entry.bytes,
20
- sha256: entry.sha256,
21
- }));
22
- verifyInputs(context, task, index, [...context.inputs, ...extra]);
23
- return inspect(task, index);
33
+ const snapshot = readVerifiedTaskSnapshot(context, options);
34
+ return inspect(snapshot.task, snapshot.index);
24
35
  };
25
36
  if (context.workspaceAccess === "read")
26
37
  return inspectCurrent();
@@ -1,6 +1,6 @@
1
1
  import type { FoundryInputFact } from "./foundry-runtime-context.ts";
2
2
  export type JsonRecord = Record<string, unknown>;
3
- export type Lane = "external-dataset-curated-import" | "source-evidence-dataset-development";
3
+ export type Lane = "external-dataset-curated-import" | "source-evidence-dataset-development" | "existing-owner-draft-repair";
4
4
  export interface FileReference {
5
5
  path: string;
6
6
  sha256: string;
@@ -29,6 +29,17 @@ export interface FoundryTaskJob {
29
29
  remote_state_code: 0;
30
30
  };
31
31
  created_at_utc: string;
32
+ /** Present only for the existing-owner-draft-repair lane; absent bytes stay byte-identical. */
33
+ repair?: Readonly<{
34
+ kind: string;
35
+ contract: string;
36
+ before: string;
37
+ candidate: string;
38
+ predecessor: Readonly<{
39
+ task_id: string;
40
+ receipt_sha256: string;
41
+ }> | null;
42
+ }>;
32
43
  }
33
44
  export interface SourceManifest {
34
45
  schema: "tiangong-foundry.source-manifest.v2";
@@ -43,6 +54,16 @@ export interface FoundryTaskOptions {
43
54
  requestId?: string;
44
55
  targetEntities?: string[];
45
56
  seed?: JsonRecord;
57
+ repair?: Readonly<{
58
+ kind: string;
59
+ contract: string;
60
+ before: string;
61
+ candidate: string;
62
+ predecessor: Readonly<{
63
+ task_id: string;
64
+ receipt_sha256: string;
65
+ }> | null;
66
+ }>;
46
67
  }
47
68
  export interface ArtifactFact extends FoundryInputFact {
48
69
  path: string;
@@ -0,0 +1,15 @@
1
+ export declare const TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA: "tiangong-lca.cli-tidas-spec-source.v1";
2
+ export type TiangongLcaCliTidasSchemaSource = Readonly<{
3
+ schema: typeof TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA;
4
+ spec_repository: string;
5
+ spec_commit: string;
6
+ spec_version: string;
7
+ source_repository: string;
8
+ source_commit: string;
9
+ manifest_sha256: string;
10
+ schemas: readonly {
11
+ name: string;
12
+ sha256: string;
13
+ }[];
14
+ }>;
15
+ export declare function resolveTiangongLcaCliTidasSource(sourceManifestPath: string, schemaDir: string): TiangongLcaCliTidasSchemaSource;
@@ -0,0 +1,90 @@
1
+ import { createHash } from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ const cliPackageName = "@tiangong-lca/cli";
5
+ export const TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA = "tiangong-lca.cli-tidas-spec-source.v1";
6
+ const expectedSource = Object.freeze({
7
+ schema: TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA,
8
+ spec_repository: "tiangong-lca/tidas-spec",
9
+ spec_commit: "d4cb089c753ffd20b173db2e56fb553a364f48f4",
10
+ spec_version: "0.2.1",
11
+ source_repository: "https://github.com/tiangong-lca/tidas-toolkit",
12
+ source_commit: "9c0d8b1c8ceb1841074f5bc6de5fbb7fcc9318f5",
13
+ manifest_sha256: "16d4d5950496544b50cd6b1570789930da104af313a325bb9eaa466507ec6bb5",
14
+ });
15
+ function isRecord(value) {
16
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
17
+ }
18
+ function sha256File(filePath) {
19
+ return createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
20
+ }
21
+ function parseSource(value) {
22
+ const keys = [
23
+ "manifest_sha256",
24
+ "schema",
25
+ "schemas",
26
+ "source_commit",
27
+ "source_repository",
28
+ "spec_commit",
29
+ "spec_repository",
30
+ "spec_version",
31
+ ];
32
+ if (Object.keys(value).length !== keys.length ||
33
+ keys.some((key) => !Object.hasOwn(value, key)) ||
34
+ value.schema !== expectedSource.schema ||
35
+ value.spec_repository !== expectedSource.spec_repository ||
36
+ value.spec_commit !== expectedSource.spec_commit ||
37
+ value.spec_version !== expectedSource.spec_version ||
38
+ value.source_repository !== expectedSource.source_repository ||
39
+ value.source_commit !== expectedSource.source_commit ||
40
+ value.manifest_sha256 !== expectedSource.manifest_sha256)
41
+ throw new Error(`Installed ${cliPackageName} TIDAS source manifest does not match the approved spec 0.2.1 source identity.`);
42
+ if (!Array.isArray(value.schemas) || value.schemas.length !== 18)
43
+ throw new Error(`Installed ${cliPackageName} TIDAS source manifest must enumerate exactly 18 schemas.`);
44
+ const schemas = value.schemas.map((entry, index) => {
45
+ if (!isRecord(entry) ||
46
+ Object.keys(entry).length !== 2 ||
47
+ typeof entry.name !== "string" ||
48
+ !/^tidas_[a-z0-9_]+\.json$/u.test(entry.name) ||
49
+ typeof entry.sha256 !== "string" ||
50
+ !/^[0-9a-f]{64}$/u.test(entry.sha256))
51
+ throw new Error(`Installed ${cliPackageName} TIDAS source manifest has an invalid schema entry at index ${index}.`);
52
+ return Object.freeze({ name: entry.name, sha256: entry.sha256 });
53
+ });
54
+ if (new Set(schemas.map((entry) => entry.name)).size !== schemas.length)
55
+ throw new Error(`Installed ${cliPackageName} TIDAS source manifest schema entries must be unique.`);
56
+ return Object.freeze({
57
+ schema: expectedSource.schema,
58
+ spec_repository: expectedSource.spec_repository,
59
+ spec_commit: expectedSource.spec_commit,
60
+ spec_version: expectedSource.spec_version,
61
+ source_repository: expectedSource.source_repository,
62
+ source_commit: expectedSource.source_commit,
63
+ manifest_sha256: expectedSource.manifest_sha256,
64
+ schemas: Object.freeze(schemas),
65
+ });
66
+ }
67
+ export function resolveTiangongLcaCliTidasSource(sourceManifestPath, schemaDir) {
68
+ let source;
69
+ try {
70
+ source = parseSource(JSON.parse(fs.readFileSync(sourceManifestPath, "utf8")));
71
+ }
72
+ catch (error) {
73
+ if (error instanceof Error && error.message.includes("TIDAS source manifest"))
74
+ throw error;
75
+ throw new Error(`Installed ${cliPackageName} TIDAS source manifest is unreadable at ${sourceManifestPath}.`, { cause: error });
76
+ }
77
+ const actualEntries = fs.readdirSync(schemaDir, { withFileTypes: true });
78
+ if (actualEntries.some((entry) => !entry.isFile()))
79
+ throw new Error(`Installed ${cliPackageName} TIDAS schema directory contains a non-file entry.`);
80
+ const actualNames = actualEntries.map((entry) => entry.name).sort();
81
+ const expectedNames = source.schemas.map((entry) => entry.name).sort();
82
+ if (JSON.stringify(actualNames) !== JSON.stringify(expectedNames))
83
+ throw new Error(`Installed ${cliPackageName} TIDAS schema files do not match its approved source manifest.`);
84
+ for (const entry of source.schemas) {
85
+ const filePath = path.join(schemaDir, entry.name);
86
+ if (sha256File(filePath) !== entry.sha256)
87
+ throw new Error(`Installed ${cliPackageName} TIDAS schema hash mismatch for ${entry.name}.`);
88
+ }
89
+ return source;
90
+ }
@@ -1,3 +1,9 @@
1
1
  import { type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
2
2
  import { type QualifiedFoundryRuntime } from "./foundry-runtime-qualification.ts";
3
- export declare function assessFoundryWorkflowRows(context: FoundryRuntimeContext, qualified: QualifiedFoundryRuntime, rowsReport: string, contextReports: readonly string[], identityReport?: string): Promise<import("./foundry-task-types.ts").JsonRecord>;
3
+ export interface FoundryAssessmentOptions {
4
+ /** Omit to retain the original all-set operation for direct callers. */
5
+ scopeType?: string;
6
+ previousAssessment?: string;
7
+ interactionSha256?: string | null;
8
+ }
9
+ export declare function assessFoundryWorkflowRows(context: FoundryRuntimeContext, qualified: QualifiedFoundryRuntime, rowsReport: string, contextReports: readonly string[], identityReport?: string, options?: FoundryAssessmentOptions): Promise<import("./foundry-task-types.ts").JsonRecord>;
@@ -10,22 +10,64 @@ import { runDatasetCurationGate } from "./import-curation/curation-gate.js";
10
10
  import { runDatasetAuthoringTaskBuild } from "./import-curation/authoring-packages.js";
11
11
  import { routeFoundryDecisionAction } from "./foundry-decision-routing.js";
12
12
  import { prepareFoundryDecisionWork } from "./foundry-workflow-decisions.js";
13
+ import { currentWorkflowState, workflowAssessmentQueueScopeSha256, } from "./foundry-workflow-state.js";
14
+ import { currentFoundryInteractionState } from "./foundry-interaction-input.js";
15
+ import { applicableFoundryInteractionDigest, applicableFoundryInteractionProjection, } from "./foundry-interaction-projection.js";
13
16
  import { createWorkflowStageDirectory, createWorkflowDirectory, registerWorkflowStageFiles, runWorkflowLocalCli, } from "./foundry-workflow-io.js";
14
17
  function record(value) {
15
18
  if (!value || typeof value !== "object" || Array.isArray(value))
16
19
  throw new FoundryContextError("workflow_report_invalid", "Stage metadata must be an object.");
17
20
  return value;
18
21
  }
19
- export function assessFoundryWorkflowRows(context, qualified, rowsReport, contextReports, identityReport) {
22
+ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contextReports, identityReport, options = {}) {
20
23
  assertQualifiedFoundryRuntime(context, qualified);
21
24
  resolveFoundryAsset(context, "specs/prewrite-content-policy.json");
22
25
  resolveFoundryAsset(context, "specs/import-profiles.json");
26
+ const previous = options.previousAssessment
27
+ ? captureFoundryInput(options.previousAssessment)
28
+ : null;
29
+ if (previous)
30
+ readFoundryInput(context, previous.path);
31
+ if (options.previousAssessment && !options.scopeType)
32
+ throw new FoundryContextError("workflow_assessment_invalid", "An assessment predecessor requires a selected row set.");
33
+ let selectedState = null;
34
+ let selectedInteraction = null;
23
35
  return runFoundryTaskOperation(context, {
24
36
  command: "dataset-workflow-assessment",
25
37
  options: {
26
38
  rows_report: rowsReport,
27
39
  context_reports: contextReports,
28
40
  identity_report: identityReport ?? null,
41
+ ...(options.scopeType
42
+ ? {
43
+ scope_type: options.scopeType,
44
+ previous_assessment_sha256: previous?.sha256 ?? null,
45
+ }
46
+ : {}),
47
+ ...(options.interactionSha256 ? { interaction_sha256: options.interactionSha256 } : {}),
48
+ },
49
+ validateCurrent(index) {
50
+ const state = currentWorkflowState(context, index);
51
+ selectedState = state;
52
+ selectedInteraction = currentFoundryInteractionState(context, index);
53
+ if (state.rows?.file !== rowsReport)
54
+ throw new FoundryContextError("workflow_rows_changed", "Current rows changed before assessment.");
55
+ if ((state.identity?.value.status === "completed" ? state.identity.file : null) !==
56
+ (identityReport ?? null))
57
+ throw new FoundryContextError("workflow_identity_changed", "Current identity evidence changed before assessment.");
58
+ if (state.interactionSha256 !== (options.interactionSha256 ?? null))
59
+ throw new FoundryContextError("workflow_interaction_changed", "Current task decisions changed before assessment.");
60
+ if (!options.scopeType)
61
+ return;
62
+ const current = state.assessment;
63
+ if (current?.file === (previous?.path ?? null) || (!current && !previous))
64
+ return;
65
+ // A concurrent identical operation can have completed before this caller obtains the lock.
66
+ // The receipt path below will replay that exact result, without advancing another scope.
67
+ if (current?.value.previous_assessment === (previous?.path ?? null) &&
68
+ current.value.assessed_type === options.scopeType)
69
+ return;
70
+ throw new FoundryContextError("workflow_assessment_changed", "Assessment progress changed before this row set was checked.");
29
71
  },
30
72
  }, (operation) => {
31
73
  for (const input of context.inputs)
@@ -42,6 +84,7 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
42
84
  !Array.isArray(identity.sets)))
43
85
  throw new FoundryContextError("workflow_identity_invalid", "Identity preflight must bind these current rows.");
44
86
  const contracts = new Map();
87
+ const contractShaByType = new Map();
45
88
  const retainedReports = (key) => {
46
89
  const value = rows[key] ?? [];
47
90
  if (!Array.isArray(value) || value.some((file) => typeof file !== "string"))
@@ -58,6 +101,7 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
58
101
  if (value.status !== "completed" || typeof value.type !== "string")
59
102
  throw new FoundryContextError("workflow_context_invalid", "A contract pack is incomplete.");
60
103
  contracts.set(value.type, record(value.files));
104
+ contractShaByType.set(value.type, captureFoundryInput(file).sha256);
61
105
  }
62
106
  const output = createWorkflowStageDirectory(context, operation, "assessment");
63
107
  fs.mkdirSync(resolveFoundryOutput(context, "tmp"), { recursive: true, mode: 0o700 });
@@ -70,9 +114,48 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
70
114
  throw new FoundryContextError("runtime_tidas_unqualified", "Selected native bytes changed.");
71
115
  const environment = createFoundryIsolatedChildEnvironment({ tempRoot: temporary });
72
116
  const selectedSets = rows.sets.map(record);
117
+ for (const set of selectedSets) {
118
+ if (typeof set.type !== "string" || typeof set.file !== "string")
119
+ throw new FoundryContextError("workflow_rows_invalid", "A row set is invalid.");
120
+ readFoundryInput(context, set.file);
121
+ }
122
+ const previousReport = previous
123
+ ? record(JSON.parse(readFoundryInput(context, previous.path).toString("utf8")))
124
+ : null;
125
+ const previousSets = selectedState?.assessment?.value.sets ?? [];
126
+ if (options.scopeType) {
127
+ if ((previousReport &&
128
+ (previousReport.schema !== "tiangong-foundry.assessment-stage.v1" ||
129
+ previousReport.owner_base !== context.assetRoot ||
130
+ !["in_progress", "completed"].includes(String(previousReport.status)))) ||
131
+ (previous && selectedState?.assessment?.file !== previous.path) ||
132
+ (!previous && selectedState?.assessment) ||
133
+ previousSets.length >= selectedSets.length ||
134
+ !selectedState?.assessmentRemainingTypes.includes(options.scopeType))
135
+ throw new FoundryContextError("workflow_assessment_invalid", "Select the next row set against the current registered assessment progress.");
136
+ }
137
+ for (const set of previousSets) {
138
+ for (const key of [
139
+ "rows",
140
+ "schema_report",
141
+ "qa_report",
142
+ "curation_report",
143
+ "authoring_manifest",
144
+ "interaction_context",
145
+ ]) {
146
+ const file = set[key];
147
+ if (file !== undefined)
148
+ readFoundryInput(context, String(file));
149
+ }
150
+ }
151
+ const queueScopeSha256 = workflowAssessmentQueueScopeSha256(selectedSets.map((set) => ({
152
+ type: String(set.type),
153
+ file: String(set.file),
154
+ count: Number(set.count),
155
+ })));
73
156
  const processes = selectedSets.find((set) => set.type === "process");
74
157
  let queueDir;
75
- if (processes) {
158
+ if (processes && (!options.scopeType || ["flow", "process"].includes(options.scopeType))) {
76
159
  if (typeof processes.file !== "string")
77
160
  throw new FoundryContextError("workflow_rows_invalid", "Process rows are missing.");
78
161
  queueDir = path.join(output, "queue");
@@ -97,8 +180,11 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
97
180
  }
98
181
  runWorkflowLocalCli(context, qualified, temporary, args);
99
182
  }
100
- const assessed = [];
101
- for (const candidate of rows.sets) {
183
+ const assessed = previousSets.map(record);
184
+ const toAssess = options.scopeType
185
+ ? [selectedSets.find((set) => set.type === options.scopeType)]
186
+ : selectedSets;
187
+ for (const candidate of toAssess) {
102
188
  const set = record(candidate);
103
189
  if (typeof set.type !== "string" ||
104
190
  typeof set.file !== "string" ||
@@ -195,6 +281,20 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
195
281
  includeExecutionCommands: false,
196
282
  },
197
283
  });
284
+ const interactionDigest = applicableFoundryInteractionDigest(selectedInteraction?.state ?? null, set.type);
285
+ const interactionContext = selectedInteraction
286
+ ? path.join(output, set.type, "interaction-context.json")
287
+ : null;
288
+ if (interactionContext && selectedInteraction) {
289
+ const projected = applicableFoundryInteractionProjection(selectedInteraction.state, set.type);
290
+ operation.writeJson(interactionContext, {
291
+ schema: projected.schema,
292
+ dataset_type: set.type,
293
+ source_state_sha256: selectedInteraction.entry.sha256,
294
+ decisions: projected.decisions,
295
+ ai_assumptions: projected.ai_assumptions,
296
+ });
297
+ }
198
298
  assessed.push({
199
299
  type: set.type,
200
300
  rows: set.file,
@@ -207,19 +307,40 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
207
307
  authoring_status: authoring.status,
208
308
  authoring_counts: authoring.counts,
209
309
  decisions: decisionWork,
310
+ ...(options.scopeType || selectedInteraction
311
+ ? { context_report_sha256: contractShaByType.get(set.type) }
312
+ : {}),
313
+ ...(queueScopeSha256 &&
314
+ ["flow", "process"].includes(set.type) &&
315
+ (options.scopeType || selectedInteraction)
316
+ ? { queue_scope_sha256: queueScopeSha256 }
317
+ : {}),
318
+ ...(interactionDigest ? { interaction_digest: interactionDigest } : {}),
319
+ ...(interactionContext ? { interaction_context: interactionContext } : {}),
210
320
  });
211
321
  }
322
+ const orderedAssessed = selectedSets
323
+ .map((set) => assessed.find((item) => item.type === set.type))
324
+ .filter((set) => Boolean(set));
212
325
  for (const input of context.inputs)
213
326
  readFoundryInput(context, input.path);
214
327
  assertQualifiedFoundryRuntime(context, qualified);
215
328
  registerWorkflowStageFiles(context, operation, output);
216
329
  const report = {
217
330
  schema: "tiangong-foundry.assessment-stage.v1",
218
- status: "completed",
331
+ status: orderedAssessed.length === selectedSets.length ? "completed" : "in_progress",
219
332
  owner_base: context.assetRoot,
220
333
  rows_report: rowsReport,
221
334
  identity_report: identityReport ?? null,
222
- sets: assessed,
335
+ sets: orderedAssessed,
336
+ ...(options.scopeType
337
+ ? {
338
+ previous_assessment: previous?.path ?? null,
339
+ previous_assessment_sha256: previous?.sha256 ?? null,
340
+ assessed_type: options.scopeType,
341
+ }
342
+ : {}),
343
+ ...(options.interactionSha256 ? { interaction_sha256: options.interactionSha256 } : {}),
223
344
  };
224
345
  operation.writeJson(path.join(output, "foundry-assessment.json"), report);
225
346
  return report;
@@ -11,13 +11,14 @@ export declare function recordFoundryWorkflowAuthorization(context: FoundryRunti
11
11
  scope: Record<string, unknown>;
12
12
  finalizationSha256: string;
13
13
  datasetType: string;
14
- inputKind: "current_rows" | "final_rows";
14
+ inputKind: "current_rows" | "final_rows" | "repair_rows";
15
15
  registration: {
16
16
  authorization_sha256: string;
17
17
  pointer_sha256: string;
18
18
  };
19
19
  submissionSha256: string;
20
20
  executionContract?: FoundryInputFact;
21
+ repairPreparation?: FoundryInputFact;
21
22
  operationOptions: Record<string, unknown>;
22
23
  validateCurrent: (index: readonly ArtifactEntry[]) => void;
23
24
  }): Promise<import("./foundry-task-types.ts").JsonRecord>;