@tiangong-lca/foundry 0.1.10 → 0.1.12

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 (44) hide show
  1. package/README.md +6 -6
  2. package/docs/architecture.md +6 -6
  3. package/docs/foundry-task-contracts.md +11 -3
  4. package/docs/package-distribution-contract.md +6 -4
  5. package/docs/public-runtime-contract.md +21 -5
  6. package/docs/runtime-context-contract.md +7 -3
  7. package/docs/task-authorization-contract.md +5 -3
  8. package/package-dist/assets/foundry-package-descriptor.json +120 -64
  9. package/package-dist/scripts/foundry-facade.d.ts +1 -0
  10. package/package-dist/scripts/foundry-facade.js +261 -36
  11. package/package-dist/scripts/foundry-runtime.d.ts +2 -1
  12. package/package-dist/scripts/foundry-runtime.js +2 -2
  13. package/package-dist/scripts/lib/foundry-interaction-input.d.ts +19 -0
  14. package/package-dist/scripts/lib/foundry-interaction-input.js +327 -0
  15. package/package-dist/scripts/lib/foundry-interaction-projection.d.ts +22 -0
  16. package/package-dist/scripts/lib/foundry-interaction-projection.js +81 -0
  17. package/package-dist/scripts/lib/foundry-interaction-types.d.ts +17 -0
  18. package/package-dist/scripts/lib/foundry-interaction-types.js +4 -0
  19. package/package-dist/scripts/lib/foundry-owner-execution-store.js +34 -2
  20. package/package-dist/scripts/lib/foundry-package-contract.d.ts +3 -3
  21. package/package-dist/scripts/lib/foundry-package-contract.js +3 -1
  22. package/package-dist/scripts/lib/foundry-semantic-input.d.ts +2 -0
  23. package/package-dist/scripts/lib/foundry-semantic-input.js +29 -2
  24. package/package-dist/scripts/lib/foundry-semantic-interaction.d.ts +12 -0
  25. package/package-dist/scripts/lib/foundry-semantic-interaction.js +38 -0
  26. package/package-dist/scripts/lib/foundry-task-start-spec.d.ts +11 -0
  27. package/package-dist/scripts/lib/foundry-task-start-spec.js +36 -0
  28. package/package-dist/scripts/lib/foundry-task-store.d.ts +1 -1
  29. package/package-dist/scripts/lib/foundry-workflow-assessment.d.ts +7 -1
  30. package/package-dist/scripts/lib/foundry-workflow-assessment.js +301 -8
  31. package/package-dist/scripts/lib/foundry-workflow-finalize.js +7 -4
  32. package/package-dist/scripts/lib/foundry-workflow-interaction.d.ts +4 -0
  33. package/package-dist/scripts/lib/foundry-workflow-interaction.js +63 -0
  34. package/package-dist/scripts/lib/foundry-workflow-semantic.js +21 -1
  35. package/package-dist/scripts/lib/foundry-workflow-state.d.ts +9 -1
  36. package/package-dist/scripts/lib/foundry-workflow-state.js +173 -21
  37. package/package-dist/scripts/public-api.d.ts +2 -0
  38. package/package-dist/scripts/public-api.js +1 -0
  39. package/package-dist/scripts/runtime-entry.js +9 -1
  40. package/package.json +2 -1
  41. package/specs/schemas/foundry-interaction-input.schema.json +173 -0
  42. package/specs/schemas/foundry-package-descriptor.schema.json +2 -1
  43. package/specs/schemas/foundry-semantic-input.schema.json +12 -1
  44. package/specs/schemas/foundry-task-start.schema.json +75 -0
@@ -16,7 +16,14 @@ function exact(value, keys) {
16
16
  }
17
17
  export function parseFoundrySemanticInput(value) {
18
18
  const data = workflowObject(value);
19
- exact(data, ["schema", "task_id", "actor_id", "assessment_sha256", "submissions"]);
19
+ exact(data, [
20
+ "schema",
21
+ "task_id",
22
+ "actor_id",
23
+ "assessment_sha256",
24
+ "submissions",
25
+ ...(Object.hasOwn(data, "interaction_sha256") ? ["interaction_sha256"] : []),
26
+ ]);
20
27
  if (data.schema !== FOUNDRY_SEMANTIC_INPUT_SCHEMA ||
21
28
  typeof data.task_id !== "string" ||
22
29
  !/^task-[0-9a-f]{64}-r\d{4}$/u.test(data.task_id) ||
@@ -24,13 +31,21 @@ export function parseFoundrySemanticInput(value) {
24
31
  !/^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$/u.test(data.actor_id) ||
25
32
  typeof data.assessment_sha256 !== "string" ||
26
33
  !sha.test(data.assessment_sha256) ||
34
+ (Object.hasOwn(data, "interaction_sha256") &&
35
+ (typeof data.interaction_sha256 !== "string" || !sha.test(data.interaction_sha256))) ||
27
36
  !Array.isArray(data.submissions) ||
28
37
  !data.submissions.length ||
29
38
  data.submissions.length > 1000)
30
39
  invalid("Semantic input must bind a task, actor, assessment and bounded submission selection.");
31
40
  const submissions = data.submissions.map((item) => {
32
41
  const part = workflowObject(item);
33
- exact(part, ["kind", "authoring_task_sha256", "file", "sha256"]);
42
+ exact(part, [
43
+ "kind",
44
+ "authoring_task_sha256",
45
+ "file",
46
+ "sha256",
47
+ ...(Object.hasOwn(part, "decision_ids") ? ["decision_ids"] : []),
48
+ ]);
34
49
  if ((part.kind !== "patch" &&
35
50
  part.kind !== "classification" &&
36
51
  part.kind !== "location" &&
@@ -44,11 +59,20 @@ export function parseFoundrySemanticInput(value) {
44
59
  part.file.length > 4096 ||
45
60
  /[\0\r\n]/u.test(part.file))
46
61
  invalid("A semantic submission reference is invalid.");
62
+ if (Object.hasOwn(part, "decision_ids") &&
63
+ (!Array.isArray(part.decision_ids) ||
64
+ part.decision_ids.length > 100 ||
65
+ part.decision_ids.some((id) => typeof id !== "string" || !/^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$/u.test(id)) ||
66
+ new Set(part.decision_ids).size !== part.decision_ids.length))
67
+ invalid("Semantic decision ids must be unique bounded identifiers.");
47
68
  return Object.freeze({
48
69
  kind: part.kind,
49
70
  authoring_task_sha256: part.authoring_task_sha256,
50
71
  file: part.file,
51
72
  sha256: part.sha256,
73
+ ...(Object.hasOwn(part, "decision_ids")
74
+ ? { decision_ids: Object.freeze([...part.decision_ids]) }
75
+ : {}),
52
76
  });
53
77
  });
54
78
  if (new Set(submissions.map((part) => part.authoring_task_sha256)).size !== submissions.length)
@@ -58,6 +82,9 @@ export function parseFoundrySemanticInput(value) {
58
82
  task_id: data.task_id,
59
83
  actor_id: data.actor_id,
60
84
  assessment_sha256: data.assessment_sha256,
85
+ ...(Object.hasOwn(data, "interaction_sha256")
86
+ ? { interaction_sha256: data.interaction_sha256 }
87
+ : {}),
61
88
  submissions: Object.freeze(submissions),
62
89
  });
63
90
  }
@@ -0,0 +1,12 @@
1
+ import type { FoundrySemanticInput } from "./foundry-semantic-input.ts";
2
+ import type { FoundryInteractionState } from "./foundry-interaction-input.ts";
3
+ export interface SemanticInteractionScope {
4
+ readonly work_item_sha256: string;
5
+ readonly dataset_type: string;
6
+ readonly decision_ids: readonly string[];
7
+ }
8
+ /** Bind a semantic proposal to the latest applicable user choices without treating them as data proof. */
9
+ export declare function verifyFoundrySemanticInteraction(input: FoundrySemanticInput, interaction: {
10
+ readonly sha256: string;
11
+ readonly state: FoundryInteractionState;
12
+ } | null, selectedScopes: ReadonlyMap<string, string>): readonly SemanticInteractionScope[];
@@ -0,0 +1,38 @@
1
+ import { FoundryContextError } from "./foundry-runtime-context.js";
2
+ import { currentFoundryDecisionsForType, currentFoundryQuestions, currentFoundryInvestigations, foundryInteractionScopeApplies, } from "./foundry-interaction-projection.js";
3
+ function fail(message) {
4
+ throw new FoundryContextError("semantic_interaction_invalid", message);
5
+ }
6
+ /** Bind a semantic proposal to the latest applicable user choices without treating them as data proof. */
7
+ export function verifyFoundrySemanticInteraction(input, interaction, selectedScopes) {
8
+ if ((input.interaction_sha256 ?? null) !== (interaction?.sha256 ?? null))
9
+ fail("Semantic input must name the current registered interaction state.");
10
+ const result = [];
11
+ for (const part of input.submissions) {
12
+ const type = selectedScopes.get(part.authoring_task_sha256);
13
+ if (!type)
14
+ fail("Semantic work is not in the current assessed scope.");
15
+ const expected = interaction
16
+ ? currentFoundryDecisionsForType(interaction.state, type).map((decision) => String(decision.decision_id))
17
+ : [];
18
+ const actual = part.decision_ids ?? [];
19
+ if (expected.length !== actual.length ||
20
+ expected.some((id) => !actual.includes(id)) ||
21
+ actual.some((id) => !expected.includes(id)))
22
+ fail("Semantic work must name exactly the current applicable user decision ids.");
23
+ if (interaction) {
24
+ const unresolved = [
25
+ ...currentFoundryQuestions(interaction.state),
26
+ ...currentFoundryInvestigations(interaction.state),
27
+ ];
28
+ if (unresolved.some((item) => foundryInteractionScopeApplies(item.dataset_type, type)))
29
+ fail("Resolve or investigate the current decision gap before applying dependent semantic work.");
30
+ }
31
+ result.push(Object.freeze({
32
+ work_item_sha256: part.authoring_task_sha256,
33
+ dataset_type: type,
34
+ decision_ids: Object.freeze([...expected]),
35
+ }));
36
+ }
37
+ return Object.freeze(result);
38
+ }
@@ -18,6 +18,15 @@ interface CleanupPreparation {
18
18
  readonly source_input: string | null;
19
19
  readonly output_directory: string;
20
20
  }
21
+ export interface FoundryTaskBrief {
22
+ readonly original_request: string | null;
23
+ readonly goal: string;
24
+ readonly intended_use: string | null;
25
+ readonly scope: string | null;
26
+ readonly deliverables: readonly string[];
27
+ readonly user_constraints: readonly string[];
28
+ readonly ai_assumptions: readonly string[];
29
+ }
21
30
  export interface FoundryTaskStartSpec {
22
31
  readonly schema: typeof FOUNDRY_TASK_START_SPEC_SCHEMA;
23
32
  readonly request_id: string;
@@ -29,6 +38,8 @@ export interface FoundryTaskStartSpec {
29
38
  readonly seed: Readonly<SourceSelection> | null;
30
39
  readonly account_intent: Readonly<AccountIntentSelection> | null;
31
40
  readonly preparation: Readonly<CleanupPreparation> | null;
41
+ /** Omitted for legacy requests, preserving their normalized bytes and fingerprints. */
42
+ readonly brief?: Readonly<FoundryTaskBrief>;
32
43
  /** Present only for the existing-owner-draft-repair lane, so v1 fingerprints never change. */
33
44
  readonly repair?: Readonly<FoundryRepairSelection>;
34
45
  }
@@ -96,6 +96,40 @@ function preparation(value, sources) {
96
96
  output_directory: item.output_directory,
97
97
  });
98
98
  }
99
+ function briefText(value, label, maxLength) {
100
+ if (typeof value !== "string" || !value.trim() || Array.from(value).length > maxLength)
101
+ fail("task_spec_brief_invalid", `${label} must be a bounded nonblank string.`);
102
+ return value;
103
+ }
104
+ function nullableBriefText(value, label, maxLength) {
105
+ return value === null ? null : briefText(value, label, maxLength);
106
+ }
107
+ function briefItems(value, label) {
108
+ if (!Array.isArray(value) || value.length > 32)
109
+ fail("task_spec_brief_invalid", `${label} must be a bounded string list.`);
110
+ return Object.freeze(value.map((item) => briefText(item, `${label} item`, 2_048)));
111
+ }
112
+ function taskBrief(value) {
113
+ const item = record(value, "Task brief");
114
+ exact(item, [
115
+ "original_request",
116
+ "goal",
117
+ "intended_use",
118
+ "scope",
119
+ "deliverables",
120
+ "user_constraints",
121
+ "ai_assumptions",
122
+ ], "Task brief");
123
+ return Object.freeze({
124
+ original_request: nullableBriefText(item.original_request, "Original request", 16_384),
125
+ goal: briefText(item.goal, "Task goal", 4_096),
126
+ intended_use: nullableBriefText(item.intended_use, "Intended use", 4_096),
127
+ scope: nullableBriefText(item.scope, "Task scope", 4_096),
128
+ deliverables: briefItems(item.deliverables, "Deliverables"),
129
+ user_constraints: briefItems(item.user_constraints, "User constraints"),
130
+ ai_assumptions: briefItems(item.ai_assumptions, "AI assumptions"),
131
+ });
132
+ }
99
133
  export function parseFoundryTaskStartSpec(value) {
100
134
  const item = record(value, "Task-start spec");
101
135
  exact(item, [
@@ -109,6 +143,7 @@ export function parseFoundryTaskStartSpec(value) {
109
143
  "seed",
110
144
  "account_intent",
111
145
  "preparation",
146
+ ...(Object.hasOwn(item, "brief") ? ["brief"] : []),
112
147
  ...(Object.hasOwn(item, "repair") ? ["repair"] : []),
113
148
  ], "Task-start spec");
114
149
  if (item.schema !== FOUNDRY_TASK_START_SPEC_SCHEMA ||
@@ -167,6 +202,7 @@ export function parseFoundryTaskStartSpec(value) {
167
202
  seed,
168
203
  account_intent: account(item.account_intent),
169
204
  preparation: preparation(item.preparation, sources),
205
+ ...(Object.hasOwn(item, "brief") ? { brief: taskBrief(item.brief) } : {}),
170
206
  ...(repair ? { repair } : {}),
171
207
  };
172
208
  return Object.freeze(spec);
@@ -23,7 +23,7 @@ export declare function assertFoundryTaskInputLineage(context: FoundryRuntimeCon
23
23
  }>>;
24
24
  /** Short local preparation transaction. Remote mutation/batch execution must not use this replay path. */
25
25
  export declare function runFoundryTaskOperation(context: FoundryRuntimeContext, input: {
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-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";
27
27
  options: JsonRecord;
28
28
  task?: FoundryTaskOptions;
29
29
  validateCurrent?: (index: readonly ArtifactEntry[]) => void;
@@ -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>;
@@ -1,5 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ import { createHash } from "node:crypto";
3
4
  import { FoundryContextError, readFoundryInput, resolveFoundryAsset, resolveFoundryOutput, captureFoundryInput, } from "./foundry-runtime-context.js";
4
5
  import { assertQualifiedFoundryRuntime, } from "./foundry-runtime-qualification.js";
5
6
  import { copyFoundryIsolatedExecutable } from "./foundry-runtime-environment.js";
@@ -10,22 +11,235 @@ import { runDatasetCurationGate } from "./import-curation/curation-gate.js";
10
11
  import { runDatasetAuthoringTaskBuild } from "./import-curation/authoring-packages.js";
11
12
  import { routeFoundryDecisionAction } from "./foundry-decision-routing.js";
12
13
  import { prepareFoundryDecisionWork } from "./foundry-workflow-decisions.js";
13
- import { createWorkflowStageDirectory, createWorkflowDirectory, registerWorkflowStageFiles, runWorkflowLocalCli, } from "./foundry-workflow-io.js";
14
+ import { currentWorkflowState, workflowAssessmentQueueScopeSha256, } from "./foundry-workflow-state.js";
15
+ import { currentFoundryInteractionState } from "./foundry-interaction-input.js";
16
+ import { applicableFoundryInteractionDigest, applicableFoundryInteractionProjection, } from "./foundry-interaction-projection.js";
17
+ import { createWorkflowStageDirectory, createWorkflowDirectory, registerWorkflowStageFiles, runWorkflowLocalCli, runWorkflowLocalCliResult, } from "./foundry-workflow-io.js";
14
18
  function record(value) {
15
19
  if (!value || typeof value !== "object" || Array.isArray(value))
16
20
  throw new FoundryContextError("workflow_report_invalid", "Stage metadata must be an object.");
17
21
  return value;
18
22
  }
19
- export function assessFoundryWorkflowRows(context, qualified, rowsReport, contextReports, identityReport) {
23
+ function assertCurrentQueueBuild(context, queueDir, result, sets) {
24
+ const invalid = () => {
25
+ throw new FoundryContextError("workflow_queue_invalid", "The local curation queue report does not match current rows and bounded output files.");
26
+ };
27
+ const object = (value) => value && typeof value === "object" && !Array.isArray(value)
28
+ ? value
29
+ : invalid();
30
+ const exactJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
31
+ const expectedPath = (value, expected) => value === expected;
32
+ const outputFile = (value, expected) => {
33
+ if (!expectedPath(value, expected))
34
+ return invalid();
35
+ try {
36
+ const fact = captureFoundryInput(expected);
37
+ if (fact.path !== expected || fact.bytes > 32 * 1024 * 1024)
38
+ return invalid();
39
+ const bytes = fs.readFileSync(expected);
40
+ if (bytes.length !== fact.bytes ||
41
+ createHash("sha256").update(bytes).digest("hex") !== fact.sha256)
42
+ return invalid();
43
+ return bytes.toString("utf8");
44
+ }
45
+ catch {
46
+ return invalid();
47
+ }
48
+ };
49
+ const jsonFile = (value, expected) => {
50
+ try {
51
+ return JSON.parse(outputFile(value, expected));
52
+ }
53
+ catch {
54
+ return invalid();
55
+ }
56
+ };
57
+ const jsonLines = (value, expected) => {
58
+ try {
59
+ return outputFile(value, expected)
60
+ .split(/\r?\n/u)
61
+ .filter((line) => line.length > 0)
62
+ .map((line) => JSON.parse(line));
63
+ }
64
+ catch {
65
+ return invalid();
66
+ }
67
+ };
68
+ try {
69
+ if (fs.realpathSync(queueDir) !== queueDir)
70
+ invalid();
71
+ }
72
+ catch {
73
+ invalid();
74
+ }
75
+ const report = result.report;
76
+ if (report.schema_version !== 1 ||
77
+ !((result.exit === 0 && report.status === "ready") ||
78
+ (result.exit === 1 && report.status === "blocked")) ||
79
+ !expectedPath(report.out_dir, queueDir))
80
+ invalid();
81
+ const typedSets = sets.map((set) => {
82
+ if (typeof set.type !== "string" ||
83
+ typeof set.file !== "string" ||
84
+ typeof set.count !== "number" ||
85
+ !Number.isSafeInteger(set.count) ||
86
+ set.count < 0)
87
+ return invalid();
88
+ return { type: set.type, file: set.file, count: set.count };
89
+ });
90
+ const processes = typedSets.find((set) => set.type === "process");
91
+ const flows = typedSets.find((set) => set.type === "flow");
92
+ const support = typedSets.filter((set) => ["contact", "source", "unitgroup", "flowproperty"].includes(set.type));
93
+ if (!processes)
94
+ return invalid();
95
+ const selected = [processes, ...(flows ? [flows] : []), ...support];
96
+ const inputs = object(report.inputs);
97
+ const supportPaths = support.map((set) => set.file);
98
+ if (!expectedPath(inputs.processes, processes.file) ||
99
+ (flows ? !expectedPath(inputs.flows, flows.file) : inputs.flows !== null) ||
100
+ !exactJson(inputs.support, supportPaths) ||
101
+ !exactJson(inputs.external_flow_refs, []))
102
+ invalid();
103
+ const expectedHashes = new Map(selected.map((set) => {
104
+ const file = set.file;
105
+ readFoundryInput(context, file);
106
+ return [file, captureFoundryInput(file).sha256];
107
+ }));
108
+ const hashes = object(object(report.hashes).inputs);
109
+ const taskRecords = Array.isArray(report.tasks) ? report.tasks.map(object) : invalid();
110
+ const taskIds = taskRecords.map((task) => {
111
+ if (typeof task.task_id !== "string" || !task.task_id)
112
+ return invalid();
113
+ return task.task_id;
114
+ });
115
+ if (!exactJson(Object.keys(hashes).sort(), [...expectedHashes.keys()].sort()) ||
116
+ [...expectedHashes].some(([file, sha256]) => hashes[file] !== sha256) ||
117
+ object(report.hashes).task_order !==
118
+ createHash("sha256").update(taskIds.join("\n")).digest("hex"))
119
+ invalid();
120
+ const counts = object(report.counts);
121
+ const supportCount = support.reduce((total, set) => total + set.count, 0);
122
+ // This call supplies no process filters; the CLI emits one task and lock per selected row.
123
+ const expectedTasks = processes.count + (flows?.count ?? 0) + supportCount;
124
+ const blockers = report.blockers;
125
+ const tasks = report.tasks;
126
+ if (!Array.isArray(blockers) || !Array.isArray(tasks))
127
+ return invalid();
128
+ for (const candidate of blockers) {
129
+ const blocker = object(candidate);
130
+ if (blocker.schema_version !== 1 ||
131
+ blocker.severity !== "blocker" ||
132
+ typeof blocker.code !== "string" ||
133
+ !blocker.code ||
134
+ typeof blocker.message !== "string" ||
135
+ !blocker.message)
136
+ invalid();
137
+ if (blocker.code === "process_flow_reference_unresolved") {
138
+ const refs = object(blocker.details).missing_flow_refs;
139
+ if (!Array.isArray(refs) || !refs.length)
140
+ return invalid();
141
+ for (const candidateRef of refs) {
142
+ const ref = object(candidateRef);
143
+ if (typeof ref.id !== "string" ||
144
+ !ref.id ||
145
+ !(ref.version === null || (typeof ref.version === "string" && ref.version)) ||
146
+ typeof ref.path !== "string" ||
147
+ !ref.path)
148
+ invalid();
149
+ }
150
+ }
151
+ }
152
+ if (counts.process_rows !== processes.count ||
153
+ counts.flow_rows !== (flows?.count ?? 0) ||
154
+ counts.support_rows !== supportCount ||
155
+ counts.external_flow_refs !== 0 ||
156
+ counts.tasks !== expectedTasks ||
157
+ counts.blockers !== blockers.length ||
158
+ tasks.length !== expectedTasks ||
159
+ (report.status === "blocked") !== blockers.length > 0)
160
+ invalid();
161
+ const files = object(report.files);
162
+ const outputs = path.join(queueDir, "outputs");
163
+ const manifest = jsonFile(files.manifest, path.join(outputs, "curation-queue-manifest.json"));
164
+ const taskRows = jsonLines(files.tasks, path.join(outputs, "curation-queue-tasks.jsonl"));
165
+ const blockerRows = jsonLines(files.blockers, path.join(outputs, "curation-queue-blockers.jsonl"));
166
+ const locks = object(jsonFile(files.locks, path.join(outputs, "curation-queue-locks.json")));
167
+ if (!Array.isArray(locks.locks))
168
+ return invalid();
169
+ if (!exactJson(manifest, report) ||
170
+ !exactJson(taskRows, tasks) ||
171
+ !exactJson(blockerRows, blockers) ||
172
+ locks.schema_version !== 1 ||
173
+ locks.locks.length !== tasks.length)
174
+ invalid();
175
+ for (let index = 0; index < taskRecords.length; index += 1) {
176
+ const task = taskRecords[index], lock = object(locks.locks[index]);
177
+ for (const key of ["task_id", "entity_type", "entity_id", "version", "lock_key"])
178
+ if (task[key] !== lock[key])
179
+ invalid();
180
+ }
181
+ for (const candidate of tasks) {
182
+ const task = object(candidate);
183
+ for (const key of ["input_rows_file", "closure_file", "run_plan_file"]) {
184
+ const file = task[key];
185
+ if (typeof file !== "string" || file !== path.resolve(file))
186
+ return invalid();
187
+ const resolved = path.resolve(file);
188
+ if (!resolved.startsWith(`${queueDir}${path.sep}`))
189
+ invalid();
190
+ outputFile(file, resolved);
191
+ }
192
+ }
193
+ }
194
+ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contextReports, identityReport, options = {}) {
20
195
  assertQualifiedFoundryRuntime(context, qualified);
21
196
  resolveFoundryAsset(context, "specs/prewrite-content-policy.json");
22
197
  resolveFoundryAsset(context, "specs/import-profiles.json");
198
+ const previous = options.previousAssessment
199
+ ? captureFoundryInput(options.previousAssessment)
200
+ : null;
201
+ if (previous)
202
+ readFoundryInput(context, previous.path);
203
+ if (options.previousAssessment && !options.scopeType)
204
+ throw new FoundryContextError("workflow_assessment_invalid", "An assessment predecessor requires a selected row set.");
205
+ let selectedState = null;
206
+ let selectedInteraction = null;
23
207
  return runFoundryTaskOperation(context, {
24
208
  command: "dataset-workflow-assessment",
25
209
  options: {
26
210
  rows_report: rowsReport,
27
211
  context_reports: contextReports,
28
212
  identity_report: identityReport ?? null,
213
+ ...(options.scopeType
214
+ ? {
215
+ scope_type: options.scopeType,
216
+ previous_assessment_sha256: previous?.sha256 ?? null,
217
+ }
218
+ : {}),
219
+ ...(options.interactionSha256 ? { interaction_sha256: options.interactionSha256 } : {}),
220
+ },
221
+ validateCurrent(index) {
222
+ const state = currentWorkflowState(context, index);
223
+ selectedState = state;
224
+ selectedInteraction = currentFoundryInteractionState(context, index);
225
+ if (state.rows?.file !== rowsReport)
226
+ throw new FoundryContextError("workflow_rows_changed", "Current rows changed before assessment.");
227
+ if ((state.identity?.value.status === "completed" ? state.identity.file : null) !==
228
+ (identityReport ?? null))
229
+ throw new FoundryContextError("workflow_identity_changed", "Current identity evidence changed before assessment.");
230
+ if (state.interactionSha256 !== (options.interactionSha256 ?? null))
231
+ throw new FoundryContextError("workflow_interaction_changed", "Current task decisions changed before assessment.");
232
+ if (!options.scopeType)
233
+ return;
234
+ const current = state.assessment;
235
+ if (current?.file === (previous?.path ?? null) || (!current && !previous))
236
+ return;
237
+ // A concurrent identical operation can have completed before this caller obtains the lock.
238
+ // The receipt path below will replay that exact result, without advancing another scope.
239
+ if (current?.value.previous_assessment === (previous?.path ?? null) &&
240
+ current.value.assessed_type === options.scopeType)
241
+ return;
242
+ throw new FoundryContextError("workflow_assessment_changed", "Assessment progress changed before this row set was checked.");
29
243
  },
30
244
  }, (operation) => {
31
245
  for (const input of context.inputs)
@@ -42,6 +256,7 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
42
256
  !Array.isArray(identity.sets)))
43
257
  throw new FoundryContextError("workflow_identity_invalid", "Identity preflight must bind these current rows.");
44
258
  const contracts = new Map();
259
+ const contractShaByType = new Map();
45
260
  const retainedReports = (key) => {
46
261
  const value = rows[key] ?? [];
47
262
  if (!Array.isArray(value) || value.some((file) => typeof file !== "string"))
@@ -58,6 +273,7 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
58
273
  if (value.status !== "completed" || typeof value.type !== "string")
59
274
  throw new FoundryContextError("workflow_context_invalid", "A contract pack is incomplete.");
60
275
  contracts.set(value.type, record(value.files));
276
+ contractShaByType.set(value.type, captureFoundryInput(file).sha256);
61
277
  }
62
278
  const output = createWorkflowStageDirectory(context, operation, "assessment");
63
279
  fs.mkdirSync(resolveFoundryOutput(context, "tmp"), { recursive: true, mode: 0o700 });
@@ -70,9 +286,48 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
70
286
  throw new FoundryContextError("runtime_tidas_unqualified", "Selected native bytes changed.");
71
287
  const environment = createFoundryIsolatedChildEnvironment({ tempRoot: temporary });
72
288
  const selectedSets = rows.sets.map(record);
289
+ for (const set of selectedSets) {
290
+ if (typeof set.type !== "string" || typeof set.file !== "string")
291
+ throw new FoundryContextError("workflow_rows_invalid", "A row set is invalid.");
292
+ readFoundryInput(context, set.file);
293
+ }
294
+ const previousReport = previous
295
+ ? record(JSON.parse(readFoundryInput(context, previous.path).toString("utf8")))
296
+ : null;
297
+ const previousSets = selectedState?.assessment?.value.sets ?? [];
298
+ if (options.scopeType) {
299
+ if ((previousReport &&
300
+ (previousReport.schema !== "tiangong-foundry.assessment-stage.v1" ||
301
+ previousReport.owner_base !== context.assetRoot ||
302
+ !["in_progress", "completed"].includes(String(previousReport.status)))) ||
303
+ (previous && selectedState?.assessment?.file !== previous.path) ||
304
+ (!previous && selectedState?.assessment) ||
305
+ previousSets.length >= selectedSets.length ||
306
+ !selectedState?.assessmentRemainingTypes.includes(options.scopeType))
307
+ throw new FoundryContextError("workflow_assessment_invalid", "Select the next row set against the current registered assessment progress.");
308
+ }
309
+ for (const set of previousSets) {
310
+ for (const key of [
311
+ "rows",
312
+ "schema_report",
313
+ "qa_report",
314
+ "curation_report",
315
+ "authoring_manifest",
316
+ "interaction_context",
317
+ ]) {
318
+ const file = set[key];
319
+ if (file !== undefined)
320
+ readFoundryInput(context, String(file));
321
+ }
322
+ }
323
+ const queueScopeSha256 = workflowAssessmentQueueScopeSha256(selectedSets.map((set) => ({
324
+ type: String(set.type),
325
+ file: String(set.file),
326
+ count: Number(set.count),
327
+ })));
73
328
  const processes = selectedSets.find((set) => set.type === "process");
74
329
  let queueDir;
75
- if (processes) {
330
+ if (processes && (!options.scopeType || ["flow", "process"].includes(options.scopeType))) {
76
331
  if (typeof processes.file !== "string")
77
332
  throw new FoundryContextError("workflow_rows_invalid", "Process rows are missing.");
78
333
  queueDir = path.join(output, "queue");
@@ -95,10 +350,13 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
95
350
  else if (["contact", "source", "unitgroup", "flowproperty"].includes(String(set.type)))
96
351
  args.push("--support", set.file);
97
352
  }
98
- runWorkflowLocalCli(context, qualified, temporary, args);
353
+ assertCurrentQueueBuild(context, queueDir, runWorkflowLocalCliResult(context, qualified, temporary, args), selectedSets);
99
354
  }
100
- const assessed = [];
101
- for (const candidate of rows.sets) {
355
+ const assessed = previousSets.map(record);
356
+ const toAssess = options.scopeType
357
+ ? [selectedSets.find((set) => set.type === options.scopeType)]
358
+ : selectedSets;
359
+ for (const candidate of toAssess) {
102
360
  const set = record(candidate);
103
361
  if (typeof set.type !== "string" ||
104
362
  typeof set.file !== "string" ||
@@ -195,6 +453,20 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
195
453
  includeExecutionCommands: false,
196
454
  },
197
455
  });
456
+ const interactionDigest = applicableFoundryInteractionDigest(selectedInteraction?.state ?? null, set.type);
457
+ const interactionContext = selectedInteraction
458
+ ? path.join(output, set.type, "interaction-context.json")
459
+ : null;
460
+ if (interactionContext && selectedInteraction) {
461
+ const projected = applicableFoundryInteractionProjection(selectedInteraction.state, set.type);
462
+ operation.writeJson(interactionContext, {
463
+ schema: projected.schema,
464
+ dataset_type: set.type,
465
+ source_state_sha256: selectedInteraction.entry.sha256,
466
+ decisions: projected.decisions,
467
+ ai_assumptions: projected.ai_assumptions,
468
+ });
469
+ }
198
470
  assessed.push({
199
471
  type: set.type,
200
472
  rows: set.file,
@@ -207,19 +479,40 @@ export function assessFoundryWorkflowRows(context, qualified, rowsReport, contex
207
479
  authoring_status: authoring.status,
208
480
  authoring_counts: authoring.counts,
209
481
  decisions: decisionWork,
482
+ ...(options.scopeType || selectedInteraction
483
+ ? { context_report_sha256: contractShaByType.get(set.type) }
484
+ : {}),
485
+ ...(queueScopeSha256 &&
486
+ ["flow", "process"].includes(set.type) &&
487
+ (options.scopeType || selectedInteraction)
488
+ ? { queue_scope_sha256: queueScopeSha256 }
489
+ : {}),
490
+ ...(interactionDigest ? { interaction_digest: interactionDigest } : {}),
491
+ ...(interactionContext ? { interaction_context: interactionContext } : {}),
210
492
  });
211
493
  }
494
+ const orderedAssessed = selectedSets
495
+ .map((set) => assessed.find((item) => item.type === set.type))
496
+ .filter((set) => Boolean(set));
212
497
  for (const input of context.inputs)
213
498
  readFoundryInput(context, input.path);
214
499
  assertQualifiedFoundryRuntime(context, qualified);
215
500
  registerWorkflowStageFiles(context, operation, output);
216
501
  const report = {
217
502
  schema: "tiangong-foundry.assessment-stage.v1",
218
- status: "completed",
503
+ status: orderedAssessed.length === selectedSets.length ? "completed" : "in_progress",
219
504
  owner_base: context.assetRoot,
220
505
  rows_report: rowsReport,
221
506
  identity_report: identityReport ?? null,
222
- sets: assessed,
507
+ sets: orderedAssessed,
508
+ ...(options.scopeType
509
+ ? {
510
+ previous_assessment: previous?.path ?? null,
511
+ previous_assessment_sha256: previous?.sha256 ?? null,
512
+ assessed_type: options.scopeType,
513
+ }
514
+ : {}),
515
+ ...(options.interactionSha256 ? { interaction_sha256: options.interactionSha256 } : {}),
223
516
  };
224
517
  operation.writeJson(path.join(output, "foundry-assessment.json"), report);
225
518
  return report;
@@ -17,7 +17,7 @@ import { createHash } from "node:crypto";
17
17
  export async function finalizeFoundryWorkflow(context, qualified, entries, authentication = { mode: "oauth" }, approval, executionProgress) {
18
18
  assertQualifiedFoundryRuntime(context, qualified);
19
19
  const state = currentWorkflowState(context, entries), rows = state.rows;
20
- if (!rows || !state.assessment)
20
+ if (!rows || !state.assessment || !state.assessmentComplete)
21
21
  throw new FoundryContextError("workflow_assessment_required", "Assess the current rows before finalization.");
22
22
  const referenceOptions = new Map();
23
23
  for (const set of rows.value.sets) {
@@ -266,10 +266,13 @@ export async function finalizeFoundryWorkflow(context, qualified, entries, authe
266
266
  : {}),
267
267
  },
268
268
  validateCurrent(index) {
269
- if (currentWorkflowState(context, index).rows?.entry.sha256 !== rows.entry.sha256)
269
+ const current = currentWorkflowState(context, index);
270
+ if (current.rows?.entry.sha256 !== rows.entry.sha256)
270
271
  throw new FoundryContextError("workflow_rows_changed", "Rows changed during finalization.");
271
- if (currentWorkflowState(context, index).referenceInputsSha256 !==
272
- state.referenceInputsSha256)
272
+ if (!current.assessmentComplete ||
273
+ current.assessment?.entry.sha256 !== state.assessment?.entry.sha256)
274
+ throw new FoundryContextError("workflow_assessment_changed", "Assessment changed during finalization.");
275
+ if (current.referenceInputsSha256 !== state.referenceInputsSha256)
273
276
  throw new FoundryContextError("reference_input_changed", "Reference selection changed during finalization.");
274
277
  if (authorization &&
275
278
  (createHash("sha256")
@@ -0,0 +1,4 @@
1
+ import { type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
2
+ import type { ArtifactEntry } from "./foundry-task-types.ts";
3
+ import { type SelectedFoundryInteractionInput } from "./foundry-interaction-input.ts";
4
+ export declare function recordFoundryInteractionInput(context: FoundryRuntimeContext, entries: readonly ArtifactEntry[], selected: SelectedFoundryInteractionInput, targetEntities: readonly string[]): Promise<Record<string, unknown>>;