@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
@@ -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";
@@ -3,7 +3,18 @@ import { type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
3
3
  import { readWorkflowArtifact } from "./foundry-workflow-state.ts";
4
4
  import type { ArtifactEntry } from "./foundry-task-types.ts";
5
5
  export type OwnerExecutionRequest = ReturnType<typeof buildRequest>;
6
+ /**
7
+ * The finalization and mutation evidence a sealed owner scope must retain. Only the registered repair
8
+ * scope — the repair kind whose handoff declares `repair_scope: true` — has neither; any other sealed
9
+ * scope that lost one is refused rather than prepared.
10
+ */
11
+ export declare function ownerExecutionScopeFiles(assetRoot: string, authorizationValue: Record<string, unknown>, handoff: Record<string, unknown>): Readonly<{
12
+ finalize_file: string | null;
13
+ mutation_file: string | null;
14
+ }>;
6
15
  declare function buildRequest(context: FoundryRuntimeContext, authorization: ReturnType<typeof readWorkflowArtifact>): {
16
+ finalize_file: string | null;
17
+ mutation_file: string | null;
7
18
  schema: "tiangong-foundry.owner-execution-request.v1";
8
19
  scope_id: string;
9
20
  content: {
@@ -21,6 +32,7 @@ declare function buildRequest(context: FoundryRuntimeContext, authorization: Ret
21
32
  state_code: number;
22
33
  dataset_type: string;
23
34
  account_mode: string;
35
+ repair_scope?: true | undefined;
24
36
  };
25
37
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
26
38
  task: string;
@@ -32,8 +44,6 @@ declare function buildRequest(context: FoundryRuntimeContext, authorization: Ret
32
44
  policy_sha256: string;
33
45
  }>;
34
46
  handoff_file: string;
35
- finalize_file: string;
36
- mutation_file: string;
37
47
  };
38
48
  export declare function prepareFoundryOwnerExecution(context: FoundryRuntimeContext, entries: readonly ArtifactEntry[]): Promise<import("./foundry-task-types.ts").JsonRecord>;
39
49
  export declare function readOwnerExecutionRequests(context: FoundryRuntimeContext, entries: readonly ArtifactEntry[]): {
@@ -41,6 +51,8 @@ export declare function readOwnerExecutionRequests(context: FoundryRuntimeContex
41
51
  file: string;
42
52
  value: Record<string, unknown>;
43
53
  request: {
54
+ finalize_file: string | null;
55
+ mutation_file: string | null;
44
56
  schema: "tiangong-foundry.owner-execution-request.v1";
45
57
  scope_id: string;
46
58
  content: {
@@ -58,6 +70,7 @@ export declare function readOwnerExecutionRequests(context: FoundryRuntimeContex
58
70
  state_code: number;
59
71
  dataset_type: string;
60
72
  account_mode: string;
73
+ repair_scope?: true | undefined;
61
74
  };
62
75
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
63
76
  task: string;
@@ -69,8 +82,6 @@ export declare function readOwnerExecutionRequests(context: FoundryRuntimeContex
69
82
  policy_sha256: string;
70
83
  }>;
71
84
  handoff_file: string;
72
- finalize_file: string;
73
- mutation_file: string;
74
85
  };
75
86
  }[];
76
87
  export declare function ownerAttemptStore(context: FoundryRuntimeContext, scopeId: string, request: {
@@ -92,6 +103,8 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
92
103
  file: string;
93
104
  value: Record<string, unknown>;
94
105
  request: {
106
+ finalize_file: string | null;
107
+ mutation_file: string | null;
95
108
  schema: "tiangong-foundry.owner-execution-request.v1";
96
109
  scope_id: string;
97
110
  content: {
@@ -109,6 +122,7 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
109
122
  state_code: number;
110
123
  dataset_type: string;
111
124
  account_mode: string;
125
+ repair_scope?: true | undefined;
112
126
  };
113
127
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
114
128
  task: string;
@@ -120,8 +134,6 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
120
134
  policy_sha256: string;
121
135
  }>;
122
136
  handoff_file: string;
123
- finalize_file: string;
124
- mutation_file: string;
125
137
  };
126
138
  }[];
127
139
  consumed: Map<string, {
@@ -129,6 +141,8 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
129
141
  file: string;
130
142
  value: Record<string, unknown>;
131
143
  request: {
144
+ finalize_file: string | null;
145
+ mutation_file: string | null;
132
146
  schema: "tiangong-foundry.owner-execution-request.v1";
133
147
  scope_id: string;
134
148
  content: {
@@ -146,6 +160,7 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
146
160
  state_code: number;
147
161
  dataset_type: string;
148
162
  account_mode: string;
163
+ repair_scope?: true | undefined;
149
164
  };
150
165
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
151
166
  task: string;
@@ -157,8 +172,6 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
157
172
  policy_sha256: string;
158
173
  }>;
159
174
  handoff_file: string;
160
- finalize_file: string;
161
- mutation_file: string;
162
175
  };
163
176
  }>;
164
177
  verified: Map<string, import("./foundry-workflow-state.ts").WorkflowArtifact<Record<string, unknown>>>;
@@ -167,6 +180,8 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
167
180
  file: string;
168
181
  value: Record<string, unknown>;
169
182
  request: {
183
+ finalize_file: string | null;
184
+ mutation_file: string | null;
170
185
  schema: "tiangong-foundry.owner-execution-request.v1";
171
186
  scope_id: string;
172
187
  content: {
@@ -184,6 +199,7 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
184
199
  state_code: number;
185
200
  dataset_type: string;
186
201
  account_mode: string;
202
+ repair_scope?: true | undefined;
187
203
  };
188
204
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
189
205
  task: string;
@@ -195,8 +211,6 @@ export declare function inspectOwnerExecutions(context: FoundryRuntimeContext, e
195
211
  policy_sha256: string;
196
212
  }>;
197
213
  handoff_file: string;
198
- finalize_file: string;
199
- mutation_file: string;
200
214
  };
201
215
  }[];
202
216
  };
@@ -206,6 +220,8 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
206
220
  file: string;
207
221
  value: Record<string, unknown>;
208
222
  request: {
223
+ finalize_file: string | null;
224
+ mutation_file: string | null;
209
225
  schema: "tiangong-foundry.owner-execution-request.v1";
210
226
  scope_id: string;
211
227
  content: {
@@ -223,6 +239,7 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
223
239
  state_code: number;
224
240
  dataset_type: string;
225
241
  account_mode: string;
242
+ repair_scope?: true | undefined;
226
243
  };
227
244
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
228
245
  task: string;
@@ -234,8 +251,6 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
234
251
  policy_sha256: string;
235
252
  }>;
236
253
  handoff_file: string;
237
- finalize_file: string;
238
- mutation_file: string;
239
254
  };
240
255
  }[];
241
256
  consumed: Map<string, {
@@ -243,6 +258,8 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
243
258
  file: string;
244
259
  value: Record<string, unknown>;
245
260
  request: {
261
+ finalize_file: string | null;
262
+ mutation_file: string | null;
246
263
  schema: "tiangong-foundry.owner-execution-request.v1";
247
264
  scope_id: string;
248
265
  content: {
@@ -260,6 +277,7 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
260
277
  state_code: number;
261
278
  dataset_type: string;
262
279
  account_mode: string;
280
+ repair_scope?: true | undefined;
263
281
  };
264
282
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
265
283
  task: string;
@@ -271,8 +289,6 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
271
289
  policy_sha256: string;
272
290
  }>;
273
291
  handoff_file: string;
274
- finalize_file: string;
275
- mutation_file: string;
276
292
  };
277
293
  }>;
278
294
  verified: Map<string, import("./foundry-workflow-state.ts").WorkflowArtifact<Record<string, unknown>>>;
@@ -281,6 +297,8 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
281
297
  file: string;
282
298
  value: Record<string, unknown>;
283
299
  request: {
300
+ finalize_file: string | null;
301
+ mutation_file: string | null;
284
302
  schema: "tiangong-foundry.owner-execution-request.v1";
285
303
  scope_id: string;
286
304
  content: {
@@ -298,6 +316,7 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
298
316
  state_code: number;
299
317
  dataset_type: string;
300
318
  account_mode: string;
319
+ repair_scope?: true | undefined;
301
320
  };
302
321
  contract: import("@tiangong-lca/cli/batch").BatchContract<{
303
322
  task: string;
@@ -309,8 +328,6 @@ export declare function completedOwnerScopes(context: FoundryRuntimeContext, ent
309
328
  policy_sha256: string;
310
329
  }>;
311
330
  handoff_file: string;
312
- finalize_file: string;
313
- mutation_file: string;
314
331
  };
315
332
  }[];
316
333
  completed: Map<string, Record<string, unknown>>;