@tiangong-lca/foundry 0.1.10 → 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 (44) hide show
  1. package/README.md +6 -6
  2. package/docs/architecture.md +6 -6
  3. package/docs/foundry-task-contracts.md +9 -3
  4. package/docs/package-distribution-contract.md +6 -4
  5. package/docs/public-runtime-contract.md +17 -5
  6. package/docs/runtime-context-contract.md +5 -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 +165 -31
  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 +127 -6
  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
@@ -0,0 +1,327 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { createHash } from "node:crypto";
4
+ import { captureFoundryInput, FoundryContextError, } from "./foundry-runtime-context.js";
5
+ import { assertNotFoundrySessionFile, migrationCredentialPath } from "./foundry-private-path.js";
6
+ import { readWorkflowArtifact } from "./foundry-workflow-state.js";
7
+ import { FOUNDRY_INTERACTION_INPUT_SCHEMA, FOUNDRY_INTERACTION_STATE_SCHEMA, FOUNDRY_INTERACTION_COMMAND, FOUNDRY_INTERACTION_REPORT, } from "./foundry-interaction-types.js";
8
+ export { FOUNDRY_INTERACTION_INPUT_SCHEMA, FOUNDRY_INTERACTION_STATE_SCHEMA, FOUNDRY_INTERACTION_COMMAND, FOUNDRY_INTERACTION_REPORT, } from "./foundry-interaction-types.js";
9
+ const idPattern = /^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$/u;
10
+ const taskPattern = /^task-[0-9a-f]{64}-r\d{4}$/u;
11
+ const shaPattern = /^[0-9a-f]{64}$/u;
12
+ const datasetTypes = new Set([
13
+ "contact",
14
+ "source",
15
+ "support",
16
+ "flow",
17
+ "flowproperty",
18
+ "unitgroup",
19
+ "process",
20
+ "lifecyclemodel",
21
+ ]);
22
+ const selectedInputs = new WeakSet();
23
+ function invalid(message) {
24
+ throw new FoundryContextError("interaction_input_invalid", message);
25
+ }
26
+ function record(value) {
27
+ if (!value || typeof value !== "object" || Array.isArray(value))
28
+ invalid("Interaction input must be an object.");
29
+ return value;
30
+ }
31
+ function exact(value, keys) {
32
+ if (Object.keys(value).length !== keys.length || keys.some((key) => !Object.hasOwn(value, key)))
33
+ invalid("Interaction input has missing or unsupported fields.");
34
+ }
35
+ function bounded(value, label, limit = 4_096) {
36
+ if (typeof value !== "string" || !value.trim() || value.length > limit || /[\0\r\n]/u.test(value))
37
+ invalid(`${label} must be concise, nonempty single-line text.`);
38
+ return value;
39
+ }
40
+ function rawAnswer(value) {
41
+ if (typeof value !== "string" || !value.trim() || value.length > 16_384 || value.includes("\0"))
42
+ invalid("Raw answer must be bounded nonempty text.");
43
+ return value;
44
+ }
45
+ function identifier(value, label) {
46
+ if (typeof value !== "string" || !idPattern.test(value))
47
+ invalid(`${label} is invalid.`);
48
+ return value;
49
+ }
50
+ function evidence(value) {
51
+ if (!Array.isArray(value) ||
52
+ value.length > 16 ||
53
+ value.some((sha) => typeof sha !== "string" || !shaPattern.test(sha)))
54
+ invalid("Evidence must contain at most 16 SHA-256 references.");
55
+ if (new Set(value).size !== value.length)
56
+ invalid("Evidence references must be unique.");
57
+ return Object.freeze([...value]);
58
+ }
59
+ function datasetType(value) {
60
+ if (value !== null && (typeof value !== "string" || !datasetTypes.has(value)))
61
+ invalid("Question or assumption dataset type is invalid.");
62
+ return value;
63
+ }
64
+ function event(value) {
65
+ const item = record(value);
66
+ if (item.kind === "question") {
67
+ exact(item, [
68
+ "kind",
69
+ "id",
70
+ "dataset_type",
71
+ "missing",
72
+ "impact",
73
+ "recommendation",
74
+ "ask",
75
+ "choices",
76
+ "evidence_sha256",
77
+ "supersedes",
78
+ ]);
79
+ if (!Array.isArray(item.choices) || item.choices.length > 4)
80
+ invalid("A question may offer at most four short choices.");
81
+ const choices = item.choices.map((choice) => bounded(choice, "Choice", 240));
82
+ if (new Set(choices).size !== choices.length)
83
+ invalid("Question choices must be unique.");
84
+ return Object.freeze({
85
+ kind: "question",
86
+ id: identifier(item.id, "Question id"),
87
+ dataset_type: datasetType(item.dataset_type),
88
+ missing: bounded(item.missing, "Missing information", 1_200),
89
+ impact: bounded(item.impact, "Impact", 1_200),
90
+ recommendation: bounded(item.recommendation, "Recommendation", 1_200),
91
+ ask: bounded(item.ask, "Question", 1_200),
92
+ choices: Object.freeze(choices),
93
+ evidence_sha256: evidence(item.evidence_sha256),
94
+ supersedes: item.supersedes === null ? null : identifier(item.supersedes, "Superseded question"),
95
+ });
96
+ }
97
+ if (item.kind === "answer") {
98
+ exact(item, [
99
+ "kind",
100
+ "question_id",
101
+ "decision_id",
102
+ "supersedes_decision_id",
103
+ "raw_answer",
104
+ "adopted_decision",
105
+ "disposition",
106
+ "evidence_sha256",
107
+ ]);
108
+ if (item.disposition !== "decided" && item.disposition !== "investigate")
109
+ invalid("Answer disposition must be decided or investigate.");
110
+ const adopted = item.adopted_decision === null ? null : bounded(item.adopted_decision, "Adopted decision");
111
+ if ((item.disposition === "decided") !== (adopted !== null))
112
+ invalid("Investigate-first answers cannot silently adopt a decision.");
113
+ return Object.freeze({
114
+ kind: "answer",
115
+ question_id: identifier(item.question_id, "Question id"),
116
+ decision_id: identifier(item.decision_id, "Decision id"),
117
+ supersedes_decision_id: item.supersedes_decision_id === null
118
+ ? null
119
+ : identifier(item.supersedes_decision_id, "Superseded decision id"),
120
+ raw_answer: rawAnswer(item.raw_answer),
121
+ adopted_decision: adopted,
122
+ disposition: item.disposition,
123
+ evidence_sha256: evidence(item.evidence_sha256),
124
+ });
125
+ }
126
+ if (item.kind === "assumption") {
127
+ exact(item, [
128
+ "kind",
129
+ "id",
130
+ "dataset_type",
131
+ "statement",
132
+ "impact",
133
+ "evidence_sha256",
134
+ "supersedes",
135
+ ]);
136
+ return Object.freeze({
137
+ kind: "assumption",
138
+ id: identifier(item.id, "Assumption id"),
139
+ dataset_type: datasetType(item.dataset_type),
140
+ statement: bounded(item.statement, "Assumption"),
141
+ impact: bounded(item.impact, "Assumption impact"),
142
+ evidence_sha256: evidence(item.evidence_sha256),
143
+ supersedes: item.supersedes === null ? null : identifier(item.supersedes, "Superseded assumption"),
144
+ });
145
+ }
146
+ return invalid("Interaction event kind is unsupported.");
147
+ }
148
+ export function parseFoundryInteractionInput(value) {
149
+ const item = record(value);
150
+ exact(item, ["schema", "task_id", "actor_id", "expected_state_sha256", "events"]);
151
+ if (item.schema !== FOUNDRY_INTERACTION_INPUT_SCHEMA ||
152
+ typeof item.task_id !== "string" ||
153
+ !taskPattern.test(item.task_id) ||
154
+ typeof item.actor_id !== "string" ||
155
+ !idPattern.test(item.actor_id) ||
156
+ (item.expected_state_sha256 !== null &&
157
+ (typeof item.expected_state_sha256 !== "string" ||
158
+ !shaPattern.test(item.expected_state_sha256))) ||
159
+ !Array.isArray(item.events) ||
160
+ !item.events.length ||
161
+ item.events.length > 16)
162
+ invalid("Interaction input needs one task, actor, current-state digest and 1-16 events.");
163
+ return Object.freeze({
164
+ schema: FOUNDRY_INTERACTION_INPUT_SCHEMA,
165
+ task_id: item.task_id,
166
+ actor_id: item.actor_id,
167
+ expected_state_sha256: item.expected_state_sha256,
168
+ events: Object.freeze(item.events.map(event)),
169
+ });
170
+ }
171
+ export function advanceFoundryInteractionState(previous, input, currentSha256, availableEvidence, allowedTypes) {
172
+ if (input.expected_state_sha256 !== currentSha256)
173
+ invalid("Interaction state changed; reload the current question and submit against its digest.");
174
+ if (previous && (previous.task_id !== input.task_id || previous.actor_id !== input.actor_id))
175
+ invalid("Interaction state belongs to another task or actor.");
176
+ const events = [...(previous?.events ?? [])];
177
+ if (events.length + input.events.length > 1_000)
178
+ invalid("Interaction history exceeds its bounded size.");
179
+ const questions = new Map();
180
+ const assumptions = new Map();
181
+ const supersededAssumptions = new Set();
182
+ const decisions = new Set();
183
+ const answers = new Map();
184
+ const supersededQuestions = new Set();
185
+ for (const existing of events) {
186
+ if (existing.kind === "question") {
187
+ questions.set(String(existing.id), existing);
188
+ if (existing.supersedes)
189
+ supersededQuestions.add(String(existing.supersedes));
190
+ }
191
+ else if (existing.kind === "answer") {
192
+ decisions.add(String(existing.decision_id));
193
+ answers.set(String(existing.question_id), existing);
194
+ }
195
+ else if (existing.kind === "assumption") {
196
+ assumptions.set(String(existing.id), existing);
197
+ if (existing.supersedes)
198
+ supersededAssumptions.add(String(existing.supersedes));
199
+ }
200
+ }
201
+ for (const item of input.events) {
202
+ const type = item.dataset_type;
203
+ if (typeof type === "string" && !allowedTypes.has(type))
204
+ invalid("Interaction scope is outside this task's target entity types.");
205
+ for (const sha of item.evidence_sha256)
206
+ if (!availableEvidence.has(sha))
207
+ invalid("Interaction evidence is not a registered source or artifact.");
208
+ if (item.kind === "question") {
209
+ const id = String(item.id);
210
+ if (questions.has(id))
211
+ invalid("Duplicate question id; reuse the current question instead.");
212
+ if (item.supersedes) {
213
+ const prior = questions.get(String(item.supersedes));
214
+ if (!prior ||
215
+ supersededQuestions.has(String(item.supersedes)) ||
216
+ prior.dataset_type !== item.dataset_type)
217
+ invalid("A replacement question must name one current question in the same scope.");
218
+ supersededQuestions.add(String(item.supersedes));
219
+ }
220
+ if ([...questions.values()].some((prior) => !supersededQuestions.has(String(prior.id)) &&
221
+ prior.dataset_type === item.dataset_type &&
222
+ prior.missing === item.missing &&
223
+ prior.ask === item.ask))
224
+ invalid("Duplicate current question; use its existing id and answer.");
225
+ questions.set(id, item);
226
+ }
227
+ else if (item.kind === "answer") {
228
+ if (!questions.has(String(item.question_id)) ||
229
+ supersededQuestions.has(String(item.question_id)))
230
+ invalid("Answer must refer to one current registered question.");
231
+ if (decisions.has(String(item.decision_id)))
232
+ invalid("Duplicate decision id.");
233
+ const previousAnswer = answers.get(String(item.question_id));
234
+ if ((previousAnswer && item.supersedes_decision_id !== previousAnswer.decision_id) ||
235
+ (!previousAnswer && item.supersedes_decision_id !== null))
236
+ invalid("A corrected answer must name the exact prior decision for this question.");
237
+ decisions.add(String(item.decision_id));
238
+ answers.set(String(item.question_id), item);
239
+ }
240
+ else if (item.kind === "assumption") {
241
+ if (assumptions.has(String(item.id)))
242
+ invalid("Duplicate assumption id.");
243
+ if (item.supersedes) {
244
+ const prior = assumptions.get(String(item.supersedes));
245
+ if (!prior ||
246
+ prior.dataset_type !== item.dataset_type ||
247
+ supersededAssumptions.has(String(item.supersedes)))
248
+ invalid("A replacement assumption must name one current assumption in the same scope.");
249
+ supersededAssumptions.add(String(item.supersedes));
250
+ }
251
+ assumptions.set(String(item.id), item);
252
+ }
253
+ events.push(item);
254
+ }
255
+ const next = Object.freeze({
256
+ schema: FOUNDRY_INTERACTION_STATE_SCHEMA,
257
+ task_id: input.task_id,
258
+ actor_id: input.actor_id,
259
+ events: Object.freeze(events),
260
+ });
261
+ if (Buffer.byteLength(`${JSON.stringify(next, null, 2)}\n`) > 8 * 1024 * 1024)
262
+ invalid("Interaction history exceeds the task artifact byte limit.");
263
+ return next;
264
+ }
265
+ export function readSelectedFoundryInteractionBytes(fact) {
266
+ const fd = fs.openSync(fact.path, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
267
+ try {
268
+ const stat = fs.fstatSync(fd);
269
+ if (!stat.isFile() || stat.size !== fact.bytes || stat.size > 1024 * 1024)
270
+ invalid("Interaction input must be a bounded regular file.");
271
+ const bytes = fs.readFileSync(fd);
272
+ if (bytes.length !== fact.bytes ||
273
+ createHash("sha256").update(bytes).digest("hex") !== fact.sha256)
274
+ invalid("Interaction input changed after selection.");
275
+ return bytes;
276
+ }
277
+ finally {
278
+ fs.closeSync(fd);
279
+ }
280
+ }
281
+ export function selectFoundryInteractionInput(context, file) {
282
+ if (!file || file.length > 4096 || /[\0\r\n]/u.test(file))
283
+ invalid("Interaction input path is invalid.");
284
+ const target = path.resolve(context.workspaceRoot, file);
285
+ if (migrationCredentialPath(path.relative(context.workspaceRoot, target)))
286
+ invalid("Credential paths cannot be interaction inputs.");
287
+ assertNotFoundrySessionFile(target, context.accountIntent?.sessionReference);
288
+ const stat = fs.lstatSync(target);
289
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024 * 1024)
290
+ invalid("Interaction input must be a bounded regular file.");
291
+ const descriptor = captureFoundryInput(target);
292
+ let spec;
293
+ try {
294
+ spec = parseFoundryInteractionInput(JSON.parse(readSelectedFoundryInteractionBytes(descriptor).toString("utf8")));
295
+ }
296
+ catch (error) {
297
+ if (error instanceof FoundryContextError && error.code !== "interaction_input_invalid")
298
+ throw error;
299
+ invalid("Interaction input must be complete valid JSON.");
300
+ }
301
+ if (spec.task_id !== context.taskId || spec.actor_id !== context.actorId)
302
+ invalid("Interaction task or actor differs from this invocation.");
303
+ const selected = Object.freeze({ spec, descriptor: Object.freeze(descriptor) });
304
+ selectedInputs.add(selected);
305
+ return selected;
306
+ }
307
+ export function assertSelectedFoundryInteractionInput(input) {
308
+ if (!selectedInputs.has(input))
309
+ invalid("Select interaction input through the current invocation.");
310
+ }
311
+ export function currentFoundryInteractionState(context, entries) {
312
+ for (const entry of [...entries].reverse()) {
313
+ if (entry.command !== FOUNDRY_INTERACTION_COMMAND ||
314
+ path.basename(entry.path) !== FOUNDRY_INTERACTION_REPORT)
315
+ continue;
316
+ const value = readWorkflowArtifact(context, entry).value;
317
+ if (value.schema !== FOUNDRY_INTERACTION_STATE_SCHEMA ||
318
+ value.task_id !== context.taskId ||
319
+ value.actor_id !== context.actorId ||
320
+ !Array.isArray(value.events) ||
321
+ value.events.length > 1_000)
322
+ invalid("Registered interaction state is invalid.");
323
+ return { entry, state: value };
324
+ }
325
+ return null;
326
+ }
327
+ export { applicableFoundryInteractionDigest, applicableFoundryInteractionProjection, currentFoundryAssumptions, currentFoundryAssumptionsForType, currentFoundryDecisions, currentFoundryDecisionsForType, currentFoundryInvestigations, currentFoundryQuestions, } from "./foundry-interaction-projection.js";
@@ -0,0 +1,22 @@
1
+ import type { FoundryInteractionState } from "./foundry-interaction-types.ts";
2
+ type Item = Readonly<Record<string, unknown>>;
3
+ export declare function foundryInteractionScopeApplies(scope: unknown, type: string): boolean;
4
+ export declare function currentFoundryQuestions(state: FoundryInteractionState): Item[];
5
+ export declare function currentFoundryInvestigations(state: FoundryInteractionState): Item[];
6
+ export declare function currentFoundryDecisions(state: FoundryInteractionState): Item[];
7
+ export declare function currentFoundryDecisionsForType(state: FoundryInteractionState, type: string): Item[];
8
+ export declare function currentFoundryAssumptions(state: FoundryInteractionState): Item[];
9
+ export declare function currentFoundryAssumptionsForType(state: FoundryInteractionState, type: string): Item[];
10
+ export declare function applicableFoundryInteractionProjection(state: FoundryInteractionState, type: string): {
11
+ schema: string;
12
+ dataset_type: string;
13
+ pending_questions: Readonly<Record<string, unknown>>[];
14
+ investigations: Readonly<Record<string, unknown>>[];
15
+ decisions: {
16
+ question: Readonly<Record<string, unknown>> | undefined;
17
+ answer: Readonly<Record<string, unknown>>;
18
+ }[];
19
+ ai_assumptions: Readonly<Record<string, unknown>>[];
20
+ };
21
+ export declare function applicableFoundryInteractionDigest(state: FoundryInteractionState | null, type: string): string | null;
22
+ export {};
@@ -0,0 +1,81 @@
1
+ import { sha256Json } from "./identity-preflight-proof.js";
2
+ const supportTypes = new Set(["contact", "source", "unitgroup", "flowproperty"]);
3
+ export function foundryInteractionScopeApplies(scope, type) {
4
+ return scope === null || scope === type || (scope === "support" && supportTypes.has(type));
5
+ }
6
+ function current(state) {
7
+ const questions = new Map();
8
+ const answers = new Map();
9
+ const assumptions = new Map();
10
+ const supersededQuestions = new Set();
11
+ const supersededAssumptions = new Set();
12
+ for (const item of state.events) {
13
+ if (item.kind === "question") {
14
+ questions.set(String(item.id), item);
15
+ if (item.supersedes)
16
+ supersededQuestions.add(String(item.supersedes));
17
+ }
18
+ else if (item.kind === "answer") {
19
+ answers.set(String(item.question_id), item);
20
+ }
21
+ else if (item.kind === "assumption") {
22
+ assumptions.set(String(item.id), item);
23
+ if (item.supersedes)
24
+ supersededAssumptions.add(String(item.supersedes));
25
+ }
26
+ }
27
+ return { questions, answers, assumptions, supersededQuestions, supersededAssumptions };
28
+ }
29
+ export function currentFoundryQuestions(state) {
30
+ const { questions, answers, supersededQuestions } = current(state);
31
+ return [...questions.values()].filter((question) => !supersededQuestions.has(String(question.id)) && !answers.has(String(question.id)));
32
+ }
33
+ export function currentFoundryInvestigations(state) {
34
+ const { questions, answers, supersededQuestions } = current(state);
35
+ return [...questions.values()].filter((question) => !supersededQuestions.has(String(question.id)) &&
36
+ answers.get(String(question.id))?.disposition === "investigate");
37
+ }
38
+ export function currentFoundryDecisions(state) {
39
+ const { answers, supersededQuestions } = current(state);
40
+ return [...answers.values()].filter((answer) => answer.disposition === "decided" && !supersededQuestions.has(String(answer.question_id)));
41
+ }
42
+ export function currentFoundryDecisionsForType(state, type) {
43
+ const { questions } = current(state);
44
+ return currentFoundryDecisions(state).filter((answer) => {
45
+ const scope = questions.get(String(answer.question_id))?.dataset_type;
46
+ return foundryInteractionScopeApplies(scope, type);
47
+ });
48
+ }
49
+ export function currentFoundryAssumptions(state) {
50
+ const { assumptions, supersededAssumptions } = current(state);
51
+ return [...assumptions.values()].filter((assumption) => !supersededAssumptions.has(String(assumption.id)));
52
+ }
53
+ export function currentFoundryAssumptionsForType(state, type) {
54
+ return currentFoundryAssumptions(state).filter((assumption) => foundryInteractionScopeApplies(assumption.dataset_type, type));
55
+ }
56
+ export function applicableFoundryInteractionProjection(state, type) {
57
+ const { questions } = current(state);
58
+ const applies = (item) => foundryInteractionScopeApplies(item.dataset_type, type);
59
+ return {
60
+ schema: "tiangong-foundry.interaction-context.v1",
61
+ dataset_type: type,
62
+ pending_questions: currentFoundryQuestions(state).filter(applies),
63
+ investigations: currentFoundryInvestigations(state).filter(applies),
64
+ decisions: currentFoundryDecisionsForType(state, type).map((answer) => ({
65
+ question: questions.get(String(answer.question_id)),
66
+ answer,
67
+ })),
68
+ ai_assumptions: currentFoundryAssumptionsForType(state, type),
69
+ };
70
+ }
71
+ export function applicableFoundryInteractionDigest(state, type) {
72
+ if (!state)
73
+ return null;
74
+ const projected = applicableFoundryInteractionProjection(state, type);
75
+ if (!projected.pending_questions.length &&
76
+ !projected.investigations.length &&
77
+ !projected.decisions.length &&
78
+ !projected.ai_assumptions.length)
79
+ return null;
80
+ return sha256Json(projected);
81
+ }
@@ -0,0 +1,17 @@
1
+ export declare const FOUNDRY_INTERACTION_INPUT_SCHEMA: "tiangong-foundry.interaction-input.v1";
2
+ export declare const FOUNDRY_INTERACTION_STATE_SCHEMA: "tiangong-foundry.interaction-state.v1";
3
+ export declare const FOUNDRY_INTERACTION_COMMAND: "dataset-workflow-interaction";
4
+ export declare const FOUNDRY_INTERACTION_REPORT: "interaction-state.json";
5
+ export interface FoundryInteractionInput {
6
+ readonly schema: typeof FOUNDRY_INTERACTION_INPUT_SCHEMA;
7
+ readonly task_id: string;
8
+ readonly actor_id: string;
9
+ readonly expected_state_sha256: string | null;
10
+ readonly events: readonly Readonly<Record<string, unknown>>[];
11
+ }
12
+ export interface FoundryInteractionState {
13
+ readonly schema: typeof FOUNDRY_INTERACTION_STATE_SCHEMA;
14
+ readonly task_id: string;
15
+ readonly actor_id: string;
16
+ readonly events: readonly Readonly<Record<string, unknown>>[];
17
+ }
@@ -0,0 +1,4 @@
1
+ export const FOUNDRY_INTERACTION_INPUT_SCHEMA = "tiangong-foundry.interaction-input.v1";
2
+ export const FOUNDRY_INTERACTION_STATE_SCHEMA = "tiangong-foundry.interaction-state.v1";
3
+ export const FOUNDRY_INTERACTION_COMMAND = "dataset-workflow-interaction";
4
+ export const FOUNDRY_INTERACTION_REPORT = "interaction-state.json";
@@ -8,6 +8,22 @@ import { captureFoundryInput, FoundryContextError, readFoundryInput, resolveFoun
8
8
  import { currentWorkflowState, readWorkflowArtifact, workflowObject, } from "./foundry-workflow-state.js";
9
9
  import { runFoundryTaskOperation } from "./foundry-task-store.js";
10
10
  import { sha256Json } from "./identity-preflight-proof.js";
11
+ import { currentFoundryInteractionState } from "./foundry-interaction-input.js";
12
+ import { currentFoundryQuestions, currentFoundryInvestigations, foundryInteractionScopeApplies, } from "./foundry-interaction-projection.js";
13
+ function assertOwnerInteractionReady(context, entries, datasetType, repairScope) {
14
+ if (repairScope)
15
+ return;
16
+ const state = currentWorkflowState(context, entries);
17
+ if (!state.assessmentComplete)
18
+ throw new FoundryContextError("execution_assessment_incomplete", "Owner execution requires complete current assessment coverage.");
19
+ const interaction = currentFoundryInteractionState(context, entries);
20
+ if (interaction &&
21
+ [
22
+ ...currentFoundryQuestions(interaction.state),
23
+ ...currentFoundryInvestigations(interaction.state),
24
+ ].some((question) => foundryInteractionScopeApplies(question.dataset_type, datasetType)))
25
+ throw new FoundryContextError("interaction_decision_pending", "An applicable question or investigation remains unresolved before owner execution.");
26
+ }
11
27
  /**
12
28
  * The finalization and mutation evidence a sealed owner scope must retain. Only the registered repair
13
29
  * scope — the repair kind whose handoff declares `repair_scope: true` — has neither; any other sealed
@@ -85,12 +101,15 @@ export async function prepareFoundryOwnerExecution(context, entries) {
85
101
  if (!approval || approval.value.status !== "sealed")
86
102
  throw new FoundryContextError("execution_approval_required", "A current sealed approval is required.");
87
103
  const request = buildRequest(context, approval);
104
+ assertOwnerInteractionReady(context, entries, request.policy.dataset_type, request.policy.repair_scope === true);
88
105
  return runFoundryTaskOperation(context, {
89
106
  command: "dataset-workflow-execution-prepare",
90
107
  options: { authorization: approval.entry.sha256 },
91
108
  validateCurrent(index) {
92
- if (currentWorkflowState(context, index).authorization?.entry.sha256 !== approval.entry.sha256)
109
+ const latest = currentWorkflowState(context, index);
110
+ if (latest.authorization?.entry.sha256 !== approval.entry.sha256)
93
111
  throw new FoundryContextError("execution_approval_changed", "Approval changed before execution preparation.");
112
+ assertOwnerInteractionReady(context, index, request.policy.dataset_type, request.policy.repair_scope === true);
94
113
  },
95
114
  }, (operation) => {
96
115
  operation.writeJson(`outputs/execution-requests/${request.scope_id}/${approval.entry.sha256}/owner-execution-request.json`, request);
@@ -196,7 +215,20 @@ export async function markOwnerAttemptConsumed(context, artifact) {
196
215
  item_contract: artifact.request.item_contract,
197
216
  attempts: 1,
198
217
  };
199
- return runFoundryTaskOperation(context, { command: "dataset-workflow-execution-consume", options: { request: artifact.entry.sha256 } }, (operation) => {
218
+ return runFoundryTaskOperation(context, {
219
+ command: "dataset-workflow-execution-consume",
220
+ options: { request: artifact.entry.sha256 },
221
+ validateCurrent(index) {
222
+ const current = readOwnerExecutionRequests(context, index);
223
+ if (!current.some((item) => item.entry.sha256 === artifact.entry.sha256))
224
+ throw new FoundryContextError("execution_request_changed", "The owner request changed before its attempt was consumed.");
225
+ assertOwnerInteractionReady(context, index, artifact.request.policy.dataset_type, artifact.request.policy.repair_scope === true);
226
+ if (artifact.request.policy.repair_scope !== true &&
227
+ currentWorkflowState(context, index).authorization?.entry.sha256 !==
228
+ artifact.request.content.authorization)
229
+ throw new FoundryContextError("execution_approval_changed", "Approval changed before the owner attempt was consumed.");
230
+ },
231
+ }, (operation) => {
200
232
  operation.writeJson(`attempts/owner-v1/${artifact.request.scope_id}/consumed.json`, value);
201
233
  return value;
202
234
  });
@@ -1,15 +1,15 @@
1
1
  export declare const FOUNDRY_PACKAGE_DESCRIPTOR_SCHEMA: "tiangong-foundry.package-descriptor.v1";
2
2
  export declare const foundryPackageDescriptorPath: "package-dist/assets/foundry-package-descriptor.json";
3
- export declare const foundryPackageStaticFiles: readonly ["package-dist/", "specs/import-profiles.json", "specs/prewrite-content-policy.json", "specs/canonical-support/flow-properties-unit-groups.json", "specs/schemas/authorization-derivation.schema.json", "specs/schemas/execution-context.schema.json", "specs/schemas/foundry-facade-request-index.schema.json", "specs/schemas/foundry-operation-result.schema.json", "specs/schemas/foundry-package-descriptor.schema.json", "specs/schemas/foundry-task-start.schema.json", "specs/schemas/foundry-semantic-input.schema.json", "specs/schemas/foundry-authorization-input.schema.json", "specs/schemas/foundry-reference-input.schema.json", "specs/schemas/foundry-workspace-migration-plan.schema.json", "specs/schemas/foundry-workspace-migration-transfer-plan.schema.json", "specs/schemas/foundry-migration-transfer-receipt.schema.json", "specs/schemas/foundry-workspace-migration-pending.schema.json", "specs/schemas/foundry-migration-adoption-plan.schema.json", "specs/schemas/foundry-migration-activation.schema.json", "specs/schemas/foundry-workspace-v2.schema.json", "specs/schemas/foundry-runtime-selection.schema.json", "specs/schemas/foundry-managed-runtime.schema.json", "specs/schemas/runtime-qualification.schema.json", "specs/schemas/task-authorization.schema.json", "specs/schemas/tidas-runtime-expectation.schema.json", "docs/architecture.md", "docs/package-distribution-contract.md", "docs/foundry-task-contracts.md", "docs/public-runtime-contract.md", "docs/runtime-context-contract.md", "docs/workspace-migration-contract.md", "docs/safety-policy.md", "docs/task-authorization-contract.md", "docs/import-profiles/bafu/constraints.md", "docs/import-profiles/bafu/leaf-process-classification-authoring.md", "docs/import-profiles/bafu/profile.md", "docs/import-profiles/uslci/constraints.md", "docs/import-profiles/uslci/profile.md", "docs/uslci-import-runbook.md", "docs/import-profiles/worldsteel/constraints.md", "docs/import-profiles/worldsteel/import-coverage.md", "docs/import-profiles/worldsteel/import-plan.md", "docs/import-profiles/worldsteel/profile.md", "README.md", "LICENSE"];
3
+ export declare const foundryPackageStaticFiles: readonly ["package-dist/", "specs/import-profiles.json", "specs/prewrite-content-policy.json", "specs/canonical-support/flow-properties-unit-groups.json", "specs/schemas/authorization-derivation.schema.json", "specs/schemas/execution-context.schema.json", "specs/schemas/foundry-facade-request-index.schema.json", "specs/schemas/foundry-operation-result.schema.json", "specs/schemas/foundry-package-descriptor.schema.json", "specs/schemas/foundry-task-start.schema.json", "specs/schemas/foundry-semantic-input.schema.json", "specs/schemas/foundry-interaction-input.schema.json", "specs/schemas/foundry-authorization-input.schema.json", "specs/schemas/foundry-reference-input.schema.json", "specs/schemas/foundry-workspace-migration-plan.schema.json", "specs/schemas/foundry-workspace-migration-transfer-plan.schema.json", "specs/schemas/foundry-migration-transfer-receipt.schema.json", "specs/schemas/foundry-workspace-migration-pending.schema.json", "specs/schemas/foundry-migration-adoption-plan.schema.json", "specs/schemas/foundry-migration-activation.schema.json", "specs/schemas/foundry-workspace-v2.schema.json", "specs/schemas/foundry-runtime-selection.schema.json", "specs/schemas/foundry-managed-runtime.schema.json", "specs/schemas/runtime-qualification.schema.json", "specs/schemas/task-authorization.schema.json", "specs/schemas/tidas-runtime-expectation.schema.json", "docs/architecture.md", "docs/package-distribution-contract.md", "docs/foundry-task-contracts.md", "docs/public-runtime-contract.md", "docs/runtime-context-contract.md", "docs/workspace-migration-contract.md", "docs/safety-policy.md", "docs/task-authorization-contract.md", "docs/import-profiles/bafu/constraints.md", "docs/import-profiles/bafu/leaf-process-classification-authoring.md", "docs/import-profiles/bafu/profile.md", "docs/import-profiles/uslci/constraints.md", "docs/import-profiles/uslci/profile.md", "docs/uslci-import-runbook.md", "docs/import-profiles/worldsteel/constraints.md", "docs/import-profiles/worldsteel/import-coverage.md", "docs/import-profiles/worldsteel/import-plan.md", "docs/import-profiles/worldsteel/profile.md", "README.md", "LICENSE"];
4
4
  declare const packageName = "@tiangong-lca/foundry";
5
- declare const packageVersion = "0.1.10";
5
+ declare const packageVersion = "0.1.11";
6
6
  declare const cliPackageName = "@tiangong-lca/cli";
7
7
  declare const cliPackageVersion = "0.1.19";
8
8
  declare const packageBin = "package-dist/scripts/package-entry.js";
9
9
  declare const packageApi = "package-dist/scripts/public-api.js";
10
10
  declare const packageTypes = "package-dist/scripts/public-api.d.ts";
11
11
  declare const supportedPlatforms: readonly ["linux-x64", "linux-arm64", "darwin-arm64", "win32-x64"];
12
- declare const protocolSchemas: readonly ["tiangong-foundry.operation-result.v1", "tiangong-foundry.task-start.v1", "tiangong-foundry.semantic-input.v1", "tiangong-foundry.authorization-input.v1", "tiangong-foundry.reference-input.v1", "tiangong-foundry.facade-request-index.v1", "tiangong-foundry.workspace-migration-plan.v1", "tiangong-foundry.workspace-migration-transfer-plan.v2", "tiangong-foundry.migration-transfer-receipt.v1", "tiangong-foundry.workspace-migration-pending.v1", "tiangong-foundry.migration-adoption-plan.v1", "tiangong-foundry.migration-activation.v1", "tiangong-foundry.workspace.v2", "tiangong-foundry.workspace-runtime-selection.v1", "tiangong-foundry.managed-runtime.v1", "tiangong-foundry.runtime-qualification.v1", "tiangong-foundry.tidas-runtime-expectation.v1", "tiangong-foundry.execution-context.v1", "tiangong-foundry.authorization-derivation.v1", "tiangong-foundry.task-authorization.v1"];
12
+ declare const protocolSchemas: readonly ["tiangong-foundry.operation-result.v1", "tiangong-foundry.task-start.v1", "tiangong-foundry.semantic-input.v1", "tiangong-foundry.interaction-input.v1", "tiangong-foundry.authorization-input.v1", "tiangong-foundry.reference-input.v1", "tiangong-foundry.facade-request-index.v1", "tiangong-foundry.workspace-migration-plan.v1", "tiangong-foundry.workspace-migration-transfer-plan.v2", "tiangong-foundry.migration-transfer-receipt.v1", "tiangong-foundry.workspace-migration-pending.v1", "tiangong-foundry.migration-adoption-plan.v1", "tiangong-foundry.migration-activation.v1", "tiangong-foundry.workspace.v2", "tiangong-foundry.workspace-runtime-selection.v1", "tiangong-foundry.managed-runtime.v1", "tiangong-foundry.runtime-qualification.v1", "tiangong-foundry.tidas-runtime-expectation.v1", "tiangong-foundry.execution-context.v1", "tiangong-foundry.authorization-derivation.v1", "tiangong-foundry.task-authorization.v1"];
13
13
  export interface FoundryPackageFileFact {
14
14
  readonly path: string;
15
15
  readonly bytes: number;
@@ -16,6 +16,7 @@ export const foundryPackageStaticFiles = Object.freeze([
16
16
  "specs/schemas/foundry-package-descriptor.schema.json",
17
17
  "specs/schemas/foundry-task-start.schema.json",
18
18
  "specs/schemas/foundry-semantic-input.schema.json",
19
+ "specs/schemas/foundry-interaction-input.schema.json",
19
20
  "specs/schemas/foundry-authorization-input.schema.json",
20
21
  "specs/schemas/foundry-reference-input.schema.json",
21
22
  "specs/schemas/foundry-workspace-migration-plan.schema.json",
@@ -52,7 +53,7 @@ export const foundryPackageStaticFiles = Object.freeze([
52
53
  "LICENSE",
53
54
  ]);
54
55
  const packageName = "@tiangong-lca/foundry";
55
- const packageVersion = "0.1.10";
56
+ const packageVersion = "0.1.11";
56
57
  const packageDescription = "Control plane for TianGong LCA external dataset import and TIDAS authoring work.";
57
58
  const cliPackageName = "@tiangong-lca/cli";
58
59
  const cliPackageVersion = "0.1.19";
@@ -69,6 +70,7 @@ const protocolSchemas = Object.freeze([
69
70
  "tiangong-foundry.operation-result.v1",
70
71
  "tiangong-foundry.task-start.v1",
71
72
  "tiangong-foundry.semantic-input.v1",
73
+ "tiangong-foundry.interaction-input.v1",
72
74
  "tiangong-foundry.authorization-input.v1",
73
75
  "tiangong-foundry.reference-input.v1",
74
76
  "tiangong-foundry.facade-request-index.v1",
@@ -5,6 +5,7 @@ export interface SemanticSubmission {
5
5
  readonly authoring_task_sha256: string;
6
6
  readonly file: string;
7
7
  readonly sha256: string;
8
+ readonly decision_ids?: readonly string[];
8
9
  }
9
10
  export type SemanticPatchInput = SemanticSubmission & {
10
11
  readonly kind: "patch";
@@ -17,6 +18,7 @@ export interface FoundrySemanticInput {
17
18
  task_id: string;
18
19
  actor_id: string;
19
20
  assessment_sha256: string;
21
+ interaction_sha256?: string;
20
22
  submissions: readonly SemanticSubmission[];
21
23
  }
22
24
  export interface SelectedSemanticInput {