@tiangong-lca/foundry 0.1.18 → 0.1.19
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.
- package/docs/package-distribution-contract.md +5 -5
- package/docs/public-runtime-contract.md +12 -0
- package/docs/runtime-context-contract.md +2 -0
- package/package-dist/assets/foundry-package-descriptor.json +268 -98
- package/package-dist/scripts/foundry-facade.js +3 -0
- package/package-dist/scripts/foundry-runtime.d.ts +1 -1
- package/package-dist/scripts/lib/decision-owners/identity-preflight.js +3 -1
- package/package-dist/scripts/lib/finalize-owners/cli-wrappers.d.ts +72 -1
- package/package-dist/scripts/lib/finalize-owners/cli-wrappers.js +123 -3
- package/package-dist/scripts/lib/finalize-owners/post-authoring.d.ts +9 -0
- package/package-dist/scripts/lib/finalize-owners/post-authoring.js +37 -8
- package/package-dist/scripts/lib/foundry-authentication-environment.js +1 -1
- package/package-dist/scripts/lib/foundry-curation-queue.d.ts +73 -0
- package/package-dist/scripts/lib/foundry-curation-queue.js +261 -0
- package/package-dist/scripts/lib/foundry-edge-draft-admission.d.ts +17 -0
- package/package-dist/scripts/lib/foundry-edge-draft-admission.js +94 -0
- package/package-dist/scripts/lib/foundry-edge-identity-preflight.d.ts +47 -0
- package/package-dist/scripts/lib/foundry-edge-identity-preflight.js +268 -0
- package/package-dist/scripts/lib/foundry-edge-oauth.d.ts +4 -0
- package/package-dist/scripts/lib/foundry-edge-oauth.js +154 -0
- package/package-dist/scripts/lib/foundry-edge-reference-intent.d.ts +51 -0
- package/package-dist/scripts/lib/foundry-edge-reference-intent.js +184 -0
- package/package-dist/scripts/lib/foundry-edge-repair-preparation.d.ts +21 -0
- package/package-dist/scripts/lib/foundry-edge-repair-preparation.js +120 -0
- package/package-dist/scripts/lib/foundry-edge-session.d.ts +15 -0
- package/package-dist/scripts/lib/foundry-edge-session.js +69 -0
- package/package-dist/scripts/lib/foundry-edge-support-plan.d.ts +69 -0
- package/package-dist/scripts/lib/foundry-edge-support-plan.js +245 -0
- package/package-dist/scripts/lib/foundry-edge-support-planning.d.ts +115 -0
- package/package-dist/scripts/lib/foundry-edge-support-planning.js +259 -0
- package/package-dist/scripts/lib/foundry-edge-support.d.ts +19 -0
- package/package-dist/scripts/lib/foundry-edge-support.js +188 -0
- package/package-dist/scripts/lib/foundry-execution-admission.d.ts +4 -2
- package/package-dist/scripts/lib/foundry-execution-admission.js +24 -2
- package/package-dist/scripts/lib/foundry-finalize-edge-identity.d.ts +30 -0
- package/package-dist/scripts/lib/foundry-finalize-edge-identity.js +102 -0
- package/package-dist/scripts/lib/foundry-finalize-owners.d.ts +1 -0
- package/package-dist/scripts/lib/foundry-finalize-owners.js +68 -2
- package/package-dist/scripts/lib/foundry-managed-authentication.js +1 -1
- package/package-dist/scripts/lib/foundry-owner-execution-store.d.ts +16 -16
- package/package-dist/scripts/lib/foundry-owner-execution-store.js +34 -7
- package/package-dist/scripts/lib/foundry-owner-readback.d.ts +20 -0
- package/package-dist/scripts/lib/foundry-owner-readback.js +42 -0
- package/package-dist/scripts/lib/foundry-package-contract.d.ts +2 -2
- package/package-dist/scripts/lib/foundry-package-contract.js +2 -2
- package/package-dist/scripts/lib/foundry-preparation-cli-validation.js +71 -0
- package/package-dist/scripts/lib/foundry-process-mass-balance.d.ts +64 -0
- package/package-dist/scripts/lib/foundry-process-mass-balance.js +224 -0
- package/package-dist/scripts/lib/foundry-process-qa.d.ts +19 -0
- package/package-dist/scripts/lib/foundry-process-qa.js +423 -0
- package/package-dist/scripts/lib/foundry-repair-execution.js +13 -2
- package/package-dist/scripts/lib/foundry-runtime-environment.d.ts +2 -0
- package/package-dist/scripts/lib/foundry-runtime-environment.js +18 -0
- package/package-dist/scripts/lib/foundry-runtime-identity.d.ts +1 -0
- package/package-dist/scripts/lib/foundry-runtime-identity.js +19 -1
- package/package-dist/scripts/lib/foundry-runtime-qualification.js +2 -2
- package/package-dist/scripts/lib/foundry-runtime-utils.d.ts +1 -0
- package/package-dist/scripts/lib/foundry-runtime-utils.js +1 -1
- package/package-dist/scripts/lib/foundry-typed-authoring-patch.d.ts +76 -0
- package/package-dist/scripts/lib/foundry-typed-authoring-patch.js +237 -0
- package/package-dist/scripts/lib/foundry-typed-context.d.ts +11 -0
- package/package-dist/scripts/lib/foundry-typed-context.js +79 -0
- package/package-dist/scripts/lib/foundry-typed-flow-qa.d.ts +166 -0
- package/package-dist/scripts/lib/foundry-typed-flow-qa.js +226 -0
- package/package-dist/scripts/lib/foundry-typed-model-write.d.ts +6 -0
- package/package-dist/scripts/lib/foundry-typed-model-write.js +59 -0
- package/package-dist/scripts/lib/foundry-workflow-assessment.js +67 -29
- package/package-dist/scripts/lib/foundry-workflow-authorization.js +82 -1
- package/package-dist/scripts/lib/foundry-workflow-context.js +59 -18
- package/package-dist/scripts/lib/foundry-workflow-execution.js +54 -5
- package/package-dist/scripts/lib/foundry-workflow-finalize.js +27 -3
- package/package-dist/scripts/lib/foundry-workflow-identity-stage.js +202 -161
- package/package-dist/scripts/lib/foundry-workflow-identity.js +16 -10
- package/package-dist/scripts/lib/foundry-workflow-io.js +27 -6
- package/package-dist/scripts/lib/foundry-workflow-reference-verify.js +102 -29
- package/package-dist/scripts/lib/foundry-workflow-repair.js +53 -2
- package/package-dist/scripts/lib/foundry-workflow-semantic.js +41 -21
- package/package-dist/scripts/lib/identity-preflight-proof.d.ts +1 -0
- package/package-dist/scripts/lib/identity-preflight-proof.js +19 -12
- package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
- package/package.json +2 -2
- package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
- package/specs/schemas/foundry-package-descriptor.schema.json +2 -2
- package/specs/schemas/runtime-qualification.schema.json +1 -1
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { FoundryContextError } from "./foundry-runtime-error.js";
|
|
5
|
+
import { datasetIdentity, detectDatasetType, unwrapDatasetPayload, } from "./import-curation/internal/dataset-payload.js";
|
|
6
|
+
import { asText, readRows } from "./import-curation/internal/runtime-io.js";
|
|
7
|
+
import { collectCommonOtherTraceEntries } from "./import-curation/internal/trace-summary.js";
|
|
8
|
+
import { collectDatasetReferences } from "./import-curation/internal/workflow-reference-closure.js";
|
|
9
|
+
const layout = {
|
|
10
|
+
support: { dir: "supports", stages: ["identity", "schema", "qa_or_profile", "checkpoint"] },
|
|
11
|
+
flow: { dir: "flows", stages: ["identity", "name_plan", "schema", "qa", "checkpoint"] },
|
|
12
|
+
process: {
|
|
13
|
+
dir: "processes",
|
|
14
|
+
stages: [
|
|
15
|
+
"dependency_closure",
|
|
16
|
+
"reference_refresh",
|
|
17
|
+
"required_fields",
|
|
18
|
+
"schema",
|
|
19
|
+
"qa",
|
|
20
|
+
"curation",
|
|
21
|
+
"remote_dry_run",
|
|
22
|
+
"readback",
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
function sha256(data) {
|
|
27
|
+
return createHash("sha256").update(data).digest("hex");
|
|
28
|
+
}
|
|
29
|
+
function record(value) {
|
|
30
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
31
|
+
? value
|
|
32
|
+
: {};
|
|
33
|
+
}
|
|
34
|
+
function invalid(message) {
|
|
35
|
+
throw new FoundryContextError("workflow_queue_invalid", message);
|
|
36
|
+
}
|
|
37
|
+
function writeText(file, contents) {
|
|
38
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
39
|
+
fs.writeFileSync(file, contents, { mode: 0o600, flag: "wx" });
|
|
40
|
+
}
|
|
41
|
+
function writeJson(file, value) {
|
|
42
|
+
writeText(file, JSON.stringify(value, null, 2) + "\n");
|
|
43
|
+
}
|
|
44
|
+
function writeRows(file, rows) {
|
|
45
|
+
writeText(file, rows.map((row) => JSON.stringify(row) + "\n").join(""));
|
|
46
|
+
}
|
|
47
|
+
const versionKey = (id, version) => JSON.stringify([id, version]);
|
|
48
|
+
const refPath = (pointer) => pointer.replace(/^\/+|\/+$/gu, "").replace(/\//gu, ".");
|
|
49
|
+
export function buildFoundryCurationQueue(options) {
|
|
50
|
+
const outDir = path.resolve(options.outDir);
|
|
51
|
+
const files = {
|
|
52
|
+
processes: path.resolve(options.processes),
|
|
53
|
+
flows: options.flows ? path.resolve(options.flows) : null,
|
|
54
|
+
support: (options.support ?? []).map((file) => path.resolve(file)),
|
|
55
|
+
external_flow_refs: (options.external ?? []).map((file) => path.resolve(file)),
|
|
56
|
+
};
|
|
57
|
+
const hashes = {};
|
|
58
|
+
const read = (file) => {
|
|
59
|
+
const bytes = (options.readInput ?? fs.readFileSync)(file);
|
|
60
|
+
hashes[file] = sha256(bytes);
|
|
61
|
+
return readRows(file, () => bytes.toString("utf8"));
|
|
62
|
+
};
|
|
63
|
+
const taskIds = new Set();
|
|
64
|
+
const workDirs = new Set();
|
|
65
|
+
const excluded = new Set(options.excludeProcessIds);
|
|
66
|
+
const entities = (file, type) => {
|
|
67
|
+
const selected = read(file).flatMap((row, index) => {
|
|
68
|
+
const kind = type === "support" ? detectDatasetType(unwrapDatasetPayload(row, "support")) : type;
|
|
69
|
+
if (!kind)
|
|
70
|
+
return invalid("Support queue rows need a recognized dataset type.");
|
|
71
|
+
const identity = datasetIdentity(row, index, kind);
|
|
72
|
+
if (!identity.id || !identity.version || /^row-\d+$/u.test(identity.id))
|
|
73
|
+
return invalid("Queue rows need a stable identity.");
|
|
74
|
+
return type === "process" && excluded.has(identity.id) ? [] : [{ row, index, identity }];
|
|
75
|
+
});
|
|
76
|
+
return (type === "process" ? selected.slice(0, options.processLimit) : selected).map(({ row, index, identity: { id, version, payload } }) => {
|
|
77
|
+
const taskId = `${type}:${id}@${version}`;
|
|
78
|
+
const token = (value) => value.replace(/[^A-Za-z0-9._-]+/gu, "_");
|
|
79
|
+
const workDir = path.join(outDir, "entities", layout[type].dir, `${token(id)}__${token(version)}`);
|
|
80
|
+
if (taskIds.has(taskId) || workDirs.has(workDir))
|
|
81
|
+
return invalid("The queue has conflicting rows for one task or path.");
|
|
82
|
+
taskIds.add(taskId);
|
|
83
|
+
workDirs.add(workDir);
|
|
84
|
+
const task = {
|
|
85
|
+
schema_version: 1,
|
|
86
|
+
entity_type: type,
|
|
87
|
+
task_id: taskId,
|
|
88
|
+
entity_id: id,
|
|
89
|
+
version,
|
|
90
|
+
lock_key: taskId,
|
|
91
|
+
depends_on: [],
|
|
92
|
+
work_dir: workDir,
|
|
93
|
+
input_rows_file: path.join(workDir, "input.jsonl"),
|
|
94
|
+
checkpoint_file: path.join(workDir, "checkpoint.json"),
|
|
95
|
+
run_plan_file: path.join(workDir, "entity-run-plan.json"),
|
|
96
|
+
closure_file: path.join(workDir, "closure.json"),
|
|
97
|
+
};
|
|
98
|
+
return { task, row, payload, source: { file, index } };
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const support = files.support.flatMap((file) => entities(file, "support"));
|
|
102
|
+
const flows = files.flows ? entities(files.flows, "flow") : [];
|
|
103
|
+
const processes = entities(files.processes, "process");
|
|
104
|
+
const externalRows = files.external_flow_refs.flatMap(read);
|
|
105
|
+
const external = new Set(externalRows.map((row) => {
|
|
106
|
+
const source = record(row);
|
|
107
|
+
const id = asText(source.id ?? source.dataset_id ?? source.uuid);
|
|
108
|
+
if (!id)
|
|
109
|
+
return invalid("External Flow evidence requires a stable identity.");
|
|
110
|
+
return versionKey(id, asText(source.version) || null);
|
|
111
|
+
}));
|
|
112
|
+
const byFlowVersion = new Map(flows.map(({ task }) => [versionKey(task.entity_id, task.version), task]));
|
|
113
|
+
const flowCounts = new Map();
|
|
114
|
+
const firstByFlow = new Map();
|
|
115
|
+
for (const { task } of flows) {
|
|
116
|
+
flowCounts.set(task.entity_id, (flowCounts.get(task.entity_id) ?? 0) + 1);
|
|
117
|
+
if (!firstByFlow.has(task.entity_id))
|
|
118
|
+
firstByFlow.set(task.entity_id, task);
|
|
119
|
+
}
|
|
120
|
+
const blockers = [];
|
|
121
|
+
const all = [...support, ...flows, ...processes];
|
|
122
|
+
for (const item of all) {
|
|
123
|
+
const { task, row, payload, source } = item;
|
|
124
|
+
const localTasks = [], externalRefs = [], deferredRefs = [], unresolvedRefs = [];
|
|
125
|
+
if (task.entity_type === "process") {
|
|
126
|
+
const deferred = new Map(collectCommonOtherTraceEntries(payload, "tiangongfoundry:unresolvedTrace")
|
|
127
|
+
.map(record)
|
|
128
|
+
.filter((trace) => (trace.action_item_code ?? trace.actionItemCode) ===
|
|
129
|
+
"elementary_flow_identity_manual_review")
|
|
130
|
+
.map((trace) => [
|
|
131
|
+
JSON.stringify([
|
|
132
|
+
asText(trace.reference_id ?? trace.referenceId ?? trace.ref_object_id),
|
|
133
|
+
asText(trace.reference_version ?? trace.referenceVersion ?? trace.version) || null,
|
|
134
|
+
refPath(asText(trace.blocked_path ?? trace.blockedPath ?? trace.path)),
|
|
135
|
+
]),
|
|
136
|
+
trace,
|
|
137
|
+
]));
|
|
138
|
+
const missing = [];
|
|
139
|
+
const refs = collectDatasetReferences(payload).filter((ref) => ref.table === "flows");
|
|
140
|
+
for (const ref of refs) {
|
|
141
|
+
const version = ref.version || null;
|
|
142
|
+
const key = versionKey(ref.id, version);
|
|
143
|
+
const location = refPath(ref.path);
|
|
144
|
+
const target = version
|
|
145
|
+
? byFlowVersion.get(key)
|
|
146
|
+
: flowCounts.get(ref.id) === 1
|
|
147
|
+
? firstByFlow.get(ref.id)
|
|
148
|
+
: undefined;
|
|
149
|
+
const base = { entity_type: "flow", entity_id: ref.id, version, ref_path: location };
|
|
150
|
+
const trace = deferred.get(JSON.stringify([ref.id, version, location]));
|
|
151
|
+
if (target) {
|
|
152
|
+
localTasks.push({ ...base, version: target.version, task_id: target.task_id });
|
|
153
|
+
}
|
|
154
|
+
else if (version && external.has(key)) {
|
|
155
|
+
externalRefs.push(base);
|
|
156
|
+
}
|
|
157
|
+
else if (trace) {
|
|
158
|
+
deferredRefs.push({
|
|
159
|
+
...base,
|
|
160
|
+
action_item_code: "elementary_flow_identity_manual_review",
|
|
161
|
+
reason: asText(trace.reason) || null,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
unresolvedRefs.push(base);
|
|
166
|
+
missing.push({ id: ref.id, version, path: location });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (missing.length)
|
|
170
|
+
blockers.push({
|
|
171
|
+
schema_version: 1,
|
|
172
|
+
code: "process_flow_reference_unresolved",
|
|
173
|
+
severity: "blocker",
|
|
174
|
+
entity_type: "process",
|
|
175
|
+
entity_id: task.entity_id,
|
|
176
|
+
version: task.version,
|
|
177
|
+
message: "Process Flow dependencies require one local task or exact external Flow evidence.",
|
|
178
|
+
details: { missing_flow_refs: missing },
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
task.depends_on = [...new Set(localTasks.map((entry) => entry.task_id))].sort();
|
|
182
|
+
writeRows(task.input_rows_file, [row]);
|
|
183
|
+
writeJson(task.closure_file, {
|
|
184
|
+
schema_version: 1,
|
|
185
|
+
entity_type: task.entity_type,
|
|
186
|
+
entity_id: task.entity_id,
|
|
187
|
+
version: task.version,
|
|
188
|
+
source,
|
|
189
|
+
dependencies: {
|
|
190
|
+
local_tasks: localTasks,
|
|
191
|
+
external_refs: externalRefs,
|
|
192
|
+
deferred_refs: deferredRefs,
|
|
193
|
+
unresolved_refs: unresolvedRefs,
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
writeJson(task.run_plan_file, {
|
|
197
|
+
schema_version: 1,
|
|
198
|
+
task_id: task.task_id,
|
|
199
|
+
entity_type: task.entity_type,
|
|
200
|
+
entity_id: task.entity_id,
|
|
201
|
+
version: task.version,
|
|
202
|
+
input_rows_file: task.input_rows_file,
|
|
203
|
+
checkpoint_file: task.checkpoint_file,
|
|
204
|
+
stages: layout[task.entity_type].stages.map((id) => ({
|
|
205
|
+
id,
|
|
206
|
+
status: "pending",
|
|
207
|
+
checkpoint_file: path.join(task.work_dir, "checkpoints", `${id}.json`),
|
|
208
|
+
})),
|
|
209
|
+
ai_authoring_policy: {
|
|
210
|
+
output_only: "structured_patch_or_build_plan",
|
|
211
|
+
deterministic_apply_required: true,
|
|
212
|
+
remote_write_allowed: false,
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
const tasks = all.map(({ task }) => task);
|
|
217
|
+
const output = path.join(outDir, "outputs");
|
|
218
|
+
const artifacts = {
|
|
219
|
+
manifest: path.join(output, "curation-queue-manifest.json"),
|
|
220
|
+
tasks: path.join(output, "curation-queue-tasks.jsonl"),
|
|
221
|
+
blockers: path.join(output, "curation-queue-blockers.jsonl"),
|
|
222
|
+
locks: path.join(output, "curation-queue-locks.json"),
|
|
223
|
+
};
|
|
224
|
+
const generated = new Date().toISOString();
|
|
225
|
+
const report = {
|
|
226
|
+
schema_version: 1,
|
|
227
|
+
producer: "foundry",
|
|
228
|
+
generated_at_utc: generated,
|
|
229
|
+
status: blockers.length ? "blocked" : "ready",
|
|
230
|
+
out_dir: outDir,
|
|
231
|
+
inputs: files,
|
|
232
|
+
counts: {
|
|
233
|
+
support_rows: support.length,
|
|
234
|
+
flow_rows: flows.length,
|
|
235
|
+
process_rows: processes.length,
|
|
236
|
+
external_flow_refs: externalRows.length,
|
|
237
|
+
tasks: tasks.length,
|
|
238
|
+
blockers: blockers.length,
|
|
239
|
+
},
|
|
240
|
+
hashes: { inputs: hashes, task_order: sha256(tasks.map((task) => task.task_id).join("\n")) },
|
|
241
|
+
files: artifacts,
|
|
242
|
+
tasks,
|
|
243
|
+
blockers,
|
|
244
|
+
};
|
|
245
|
+
writeRows(artifacts.tasks, tasks);
|
|
246
|
+
writeRows(artifacts.blockers, blockers);
|
|
247
|
+
writeJson(artifacts.locks, {
|
|
248
|
+
schema_version: 1,
|
|
249
|
+
generated_at_utc: generated,
|
|
250
|
+
locks: tasks.map(({ task_id, entity_type, entity_id, version, lock_key }) => ({
|
|
251
|
+
task_id,
|
|
252
|
+
entity_type,
|
|
253
|
+
entity_id,
|
|
254
|
+
version,
|
|
255
|
+
lock_key,
|
|
256
|
+
status: "available",
|
|
257
|
+
})),
|
|
258
|
+
});
|
|
259
|
+
writeJson(artifacts.manifest, report);
|
|
260
|
+
return report;
|
|
261
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type FoundryInputFact, type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
|
|
2
|
+
import { type EdgeSupportPlan, type EdgeSupportTarget } from "./foundry-edge-support-plan.ts";
|
|
3
|
+
export declare function verifySelectedEdgeDraftPlan(context: FoundryRuntimeContext, plan: EdgeSupportPlan, approved: FoundryInputFact, target: EdgeSupportTarget): {
|
|
4
|
+
root: {
|
|
5
|
+
table: string;
|
|
6
|
+
id: string;
|
|
7
|
+
version: string;
|
|
8
|
+
payload_sha256: string;
|
|
9
|
+
references: {
|
|
10
|
+
table: string;
|
|
11
|
+
id: string;
|
|
12
|
+
version: string;
|
|
13
|
+
}[];
|
|
14
|
+
};
|
|
15
|
+
content: Record<string, unknown>;
|
|
16
|
+
beforeContent?: Record<string, unknown> | undefined;
|
|
17
|
+
}[];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { captureFoundryInput, FoundryContextError, readFoundryInput, } from "./foundry-runtime-context.js";
|
|
2
|
+
import { verifyEdgeSupportPlan, } from "./foundry-edge-support-plan.js";
|
|
3
|
+
import { sha256Json } from "./identity-preflight-proof.js";
|
|
4
|
+
import { readRows } from "./import-curation/internal/runtime-io.js";
|
|
5
|
+
import { readNativeDraftHandoff } from "./finalize-owners/native-draft-handoff.js";
|
|
6
|
+
import { workflowObject } from "./foundry-workflow-state.js";
|
|
7
|
+
import { readEdgeReferenceIntent } from "./foundry-edge-reference-intent.js";
|
|
8
|
+
export function verifySelectedEdgeDraftPlan(context, plan, approved, target) {
|
|
9
|
+
const read = (file) => readFoundryInput(context, file).toString("utf8");
|
|
10
|
+
const evidenceFile = plan.reference_evidence?.path;
|
|
11
|
+
const report = evidenceFile ? JSON.parse(read(evidenceFile)) : null;
|
|
12
|
+
const evidence = evidenceFile ? { fact: captureFoundryInput(evidenceFile), report } : undefined;
|
|
13
|
+
const rows = readRows(approved.path, read);
|
|
14
|
+
const recordedIntent = evidence && workflowObject(evidence.report).reference_intent;
|
|
15
|
+
if (recordedIntent || plan.reference_intent) {
|
|
16
|
+
const intent = plan.reference_intent?.[0];
|
|
17
|
+
if (!recordedIntent || !intent)
|
|
18
|
+
throw new FoundryContextError("execution_reference_evidence_unbound", "The sealed Edge plan must carry the original explicit reference intent.");
|
|
19
|
+
const current = readEdgeReferenceIntent(context, intent.path, plan.dataset_type, target, rows);
|
|
20
|
+
if (sha256Json(current.binding) !== sha256Json(recordedIntent))
|
|
21
|
+
throw new FoundryContextError("execution_reference_evidence_unbound", "The reference intent, reviews or consumers differ from the sealed precommit evidence.");
|
|
22
|
+
}
|
|
23
|
+
const update = plan.update;
|
|
24
|
+
const insert = plan.insert_contract;
|
|
25
|
+
if (insert) {
|
|
26
|
+
read(insert.path);
|
|
27
|
+
const native = readNativeDraftHandoff({
|
|
28
|
+
contractFile: insert.path,
|
|
29
|
+
rowsFile: approved.path,
|
|
30
|
+
datasetType: plan.dataset_type,
|
|
31
|
+
verifiedProjectRef: target.project_ref,
|
|
32
|
+
targetUserId: target.user_id,
|
|
33
|
+
stateCode: "0",
|
|
34
|
+
relativePath: (file) => file,
|
|
35
|
+
});
|
|
36
|
+
if (native.metadata.operation !== "insert" ||
|
|
37
|
+
sha256Json(captureFoundryInput(insert.path)) !== sha256Json(insert))
|
|
38
|
+
throw new FoundryContextError("edge_draft_contract_mismatch", "Explicit Edge insertion requires the original exact insert-only contract.");
|
|
39
|
+
}
|
|
40
|
+
if (update) {
|
|
41
|
+
const invalid = (detail) => new FoundryContextError("repair_edge_scope_changed", detail);
|
|
42
|
+
for (const fact of [update.before, update.contract, update.preparation]) {
|
|
43
|
+
read(fact.path);
|
|
44
|
+
const current = captureFoundryInput(fact.path);
|
|
45
|
+
if (current.path !== fact.path ||
|
|
46
|
+
current.bytes !== fact.bytes ||
|
|
47
|
+
current.sha256 !== fact.sha256)
|
|
48
|
+
throw invalid("A selected update input changed after the plan was sealed.");
|
|
49
|
+
}
|
|
50
|
+
const preparation = workflowObject(JSON.parse(read(update.preparation.path)));
|
|
51
|
+
const inputs = workflowObject(preparation.inputs);
|
|
52
|
+
const preparedContract = workflowObject(inputs.contract);
|
|
53
|
+
const evidence = workflowObject(preparation.evidence);
|
|
54
|
+
const preflight = Array.isArray(evidence.detail)
|
|
55
|
+
? evidence.detail.map(workflowObject).find((item) => item.role === "edge_preflight")
|
|
56
|
+
: undefined;
|
|
57
|
+
const rules = workflowObject(preflight?.report ?? {}).rule_verification;
|
|
58
|
+
const native = readNativeDraftHandoff({
|
|
59
|
+
contractFile: update.contract.path,
|
|
60
|
+
rowsFile: approved.path,
|
|
61
|
+
datasetType: "process",
|
|
62
|
+
verifiedProjectRef: target.project_ref,
|
|
63
|
+
targetUserId: target.user_id,
|
|
64
|
+
stateCode: "0",
|
|
65
|
+
relativePath: (file) => file,
|
|
66
|
+
});
|
|
67
|
+
const validPreparation = {
|
|
68
|
+
schema: preparation.schema === "tiangong-foundry.repair-preparation.v1",
|
|
69
|
+
readiness: preparation.status === "prepared" &&
|
|
70
|
+
preparation.remote_writes === 0 &&
|
|
71
|
+
preparation.publication_ready === false,
|
|
72
|
+
before: sha256Json(inputs.before) === sha256Json(update.before),
|
|
73
|
+
candidate: sha256Json(inputs.candidate) === sha256Json(captureFoundryInput(approved.path)),
|
|
74
|
+
contract: preparedContract.sha256 === update.contract.sha256 &&
|
|
75
|
+
preparedContract.bytes === update.contract.bytes,
|
|
76
|
+
scope: workflowObject(preparation.scope).contract_sha256 === native.canonical_sha256,
|
|
77
|
+
validation: Boolean(rules) && sha256Json(rules) === sha256Json(update.rule_verification),
|
|
78
|
+
};
|
|
79
|
+
const missing = Object.entries(validPreparation).filter(([, valid]) => !valid);
|
|
80
|
+
if (missing.length)
|
|
81
|
+
throw invalid(`Invalid guarded update preparation fields: ${missing.map(([key]) => key).join(", ")}.`);
|
|
82
|
+
if (native.contract.actions.length !== plan.roots.length ||
|
|
83
|
+
native.contract.actions.some((action) => {
|
|
84
|
+
const root = plan.roots.find((item) => item.id === action.id && item.version === action.version);
|
|
85
|
+
return (action.expected_operation !== "save_draft" ||
|
|
86
|
+
!root ||
|
|
87
|
+
action.table !== root.table ||
|
|
88
|
+
action.before_sha256 !== root.before_sha256 ||
|
|
89
|
+
action.desired_sha256 !== root.payload_sha256);
|
|
90
|
+
}))
|
|
91
|
+
throw invalid("The native update actions differ from the sealed exact draft writes.");
|
|
92
|
+
}
|
|
93
|
+
return verifyEdgeSupportPlan(plan, captureFoundryInput(approved.path), target, rows, evidence, update && { binding: update, rows: readRows(update.before.path, read) }, insert);
|
|
94
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
|
|
2
|
+
import type { QualifiedFoundryRuntime } from "./foundry-runtime-qualification.ts";
|
|
3
|
+
import type { FoundryAuthentication } from "./foundry-runtime-identity.ts";
|
|
4
|
+
import { type IdentityPreflightBindingInput } from "./identity-preflight-proof.ts";
|
|
5
|
+
export declare function validateEdgeIdentityQuery(type: "flow" | "process", rowValue: unknown, requestText: string): {
|
|
6
|
+
readonly endpoint: "flow_hybrid_search" | "process_hybrid_search";
|
|
7
|
+
readonly body: {
|
|
8
|
+
query: unknown;
|
|
9
|
+
filter?: {} | undefined;
|
|
10
|
+
match_count: unknown;
|
|
11
|
+
page_size: unknown;
|
|
12
|
+
data_source: unknown;
|
|
13
|
+
match_threshold: unknown;
|
|
14
|
+
lexical_weight: unknown;
|
|
15
|
+
semantic_weight: unknown;
|
|
16
|
+
rrf_k: unknown;
|
|
17
|
+
};
|
|
18
|
+
readonly target: import("./import-curation/internal/dataset-payload.ts").DatasetIdentity;
|
|
19
|
+
};
|
|
20
|
+
export declare function edgeIdentityCandidates(type: "flow" | "process", result: unknown, limit: number): {
|
|
21
|
+
id: string;
|
|
22
|
+
version: string;
|
|
23
|
+
content: Record<string, unknown>;
|
|
24
|
+
score: {} | null;
|
|
25
|
+
}[];
|
|
26
|
+
type Claim = {
|
|
27
|
+
dispatchSha256: string;
|
|
28
|
+
sourceSha256: string;
|
|
29
|
+
claimedAt: string;
|
|
30
|
+
receipt: NonNullable<IdentityPreflightBindingInput["authReceipt"]>;
|
|
31
|
+
};
|
|
32
|
+
/** Run the already-audited bounded query and hand the decision to Foundry's existing review lane. */
|
|
33
|
+
export declare function runFoundryEdgeIdentityPreflight(context: FoundryRuntimeContext, qualified: QualifiedFoundryRuntime, authentication: FoundryAuthentication, type: "flow" | "process", indexFile: string, outDir: string, claim?: Claim): Promise<{
|
|
34
|
+
schema: string;
|
|
35
|
+
status: string;
|
|
36
|
+
producer: string;
|
|
37
|
+
index: string;
|
|
38
|
+
files: {
|
|
39
|
+
report: string;
|
|
40
|
+
};
|
|
41
|
+
}>;
|
|
42
|
+
/** Adopt one original Edge observation only when the persisted dispatch and all response evidence agree. */
|
|
43
|
+
export declare function readRetainedFoundryEdgeIdentity(context: FoundryRuntimeContext, type: "flow" | "process", row: Record<string, unknown>, runDir: string, mode: FoundryAuthentication["mode"], claim: Claim): {
|
|
44
|
+
run: Record<string, unknown>;
|
|
45
|
+
row: Record<string, unknown>;
|
|
46
|
+
};
|
|
47
|
+
export {};
|