@tiangong-lca/foundry 0.1.9 → 0.1.10
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/README.md +10 -8
- package/docs/architecture.md +9 -7
- package/docs/foundry-task-contracts.md +10 -4
- package/docs/import-profiles/bafu/constraints.md +1 -1
- package/docs/import-profiles/bafu/profile.md +1 -1
- package/docs/import-profiles/worldsteel/constraints.md +3 -3
- package/docs/import-profiles/worldsteel/import-coverage.md +3 -3
- package/docs/import-profiles/worldsteel/import-plan.md +4 -4
- package/docs/import-profiles/worldsteel/profile.md +3 -3
- package/docs/package-distribution-contract.md +9 -9
- package/docs/public-runtime-contract.md +17 -7
- package/docs/runtime-context-contract.md +5 -5
- package/docs/task-authorization-contract.md +3 -3
- package/package-dist/assets/foundry-package-descriptor.json +218 -128
- package/package-dist/scripts/foundry-facade.js +71 -0
- package/package-dist/scripts/foundry-runtime.d.ts +12 -2
- package/package-dist/scripts/lib/finalize-owners/commit-handoff.js +3 -3
- package/package-dist/scripts/lib/finalize-owners/{native-insert-closeout.d.ts → native-draft-closeout.d.ts} +1 -1
- package/package-dist/scripts/lib/finalize-owners/{native-insert-closeout.js → native-draft-closeout.js} +10 -6
- package/package-dist/scripts/lib/finalize-owners/{native-insert-handoff.d.ts → native-draft-handoff.d.ts} +7 -5
- package/package-dist/scripts/lib/finalize-owners/{native-insert-handoff.js → native-draft-handoff.js} +30 -11
- package/package-dist/scripts/lib/finalize-owners/post-write-closeout.js +2 -2
- package/package-dist/scripts/lib/foundry-authorization-input.d.ts +10 -1
- package/package-dist/scripts/lib/foundry-authorization-input.js +27 -3
- package/package-dist/scripts/lib/foundry-command-metadata.js +25 -2
- package/package-dist/scripts/lib/foundry-command-registry.d.ts +4 -4
- package/package-dist/scripts/lib/foundry-command-registry.js +3 -0
- package/package-dist/scripts/lib/foundry-execution-admission.js +3 -3
- package/package-dist/scripts/lib/foundry-facade-store.js +56 -6
- package/package-dist/scripts/lib/foundry-owner-execution-store.d.ts +33 -16
- package/package-dist/scripts/lib/foundry-owner-execution-store.js +32 -3
- package/package-dist/scripts/lib/foundry-owner-readback.d.ts +3 -0
- package/package-dist/scripts/lib/foundry-owner-readback.js +101 -27
- 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-repair-execution.d.ts +11 -0
- package/package-dist/scripts/lib/foundry-repair-execution.js +70 -0
- package/package-dist/scripts/lib/foundry-repair-handoff.d.ts +16 -0
- package/package-dist/scripts/lib/foundry-repair-handoff.js +108 -0
- package/package-dist/scripts/lib/foundry-repair-preflight.d.ts +96 -0
- package/package-dist/scripts/lib/foundry-repair-preflight.js +313 -0
- package/package-dist/scripts/lib/foundry-repair-reference-binding.d.ts +26 -0
- package/package-dist/scripts/lib/foundry-repair-reference-binding.js +120 -0
- package/package-dist/scripts/lib/foundry-repair-reference-proof.d.ts +63 -0
- package/package-dist/scripts/lib/foundry-repair-reference-proof.js +329 -0
- package/package-dist/scripts/lib/foundry-repair-scope.d.ts +76 -0
- package/package-dist/scripts/lib/foundry-repair-scope.js +304 -0
- package/package-dist/scripts/lib/foundry-runtime-command-policy.js +1 -0
- package/package-dist/scripts/lib/foundry-runtime-qualification.js +2 -2
- package/package-dist/scripts/lib/foundry-runtime-utils.d.ts +3 -0
- package/package-dist/scripts/lib/foundry-runtime-utils.js +6 -1
- package/package-dist/scripts/lib/foundry-successor-predecessor.d.ts +36 -0
- package/package-dist/scripts/lib/foundry-successor-predecessor.js +399 -0
- package/package-dist/scripts/lib/foundry-task-registration.js +64 -3
- package/package-dist/scripts/lib/foundry-task-start-spec.d.ts +3 -0
- package/package-dist/scripts/lib/foundry-task-start-spec.js +24 -1
- package/package-dist/scripts/lib/foundry-task-store.d.ts +13 -1
- package/package-dist/scripts/lib/foundry-task-store.js +23 -12
- package/package-dist/scripts/lib/foundry-task-types.d.ts +22 -1
- package/package-dist/scripts/lib/foundry-tidas-source.d.ts +15 -0
- package/package-dist/scripts/lib/foundry-tidas-source.js +90 -0
- package/package-dist/scripts/lib/foundry-workflow-authorization.d.ts +2 -1
- package/package-dist/scripts/lib/foundry-workflow-authorization.js +194 -26
- package/package-dist/scripts/lib/foundry-workflow-context.js +4 -0
- package/package-dist/scripts/lib/foundry-workflow-execution.js +6 -1
- package/package-dist/scripts/lib/foundry-workflow-repair.d.ts +36 -0
- package/package-dist/scripts/lib/foundry-workflow-repair.js +580 -0
- package/package-dist/scripts/lib/foundry-workflow-state.js +66 -18
- package/package-dist/scripts/lib/import-curation/curation-cleanup.js +39 -8
- package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
- package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.d.ts +13 -2
- package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.js +100 -29
- package/package-dist/scripts/lib/import-curation/internal/workflow-authoring-tasks.js +1 -2
- package/package-dist/scripts/lib/import-curation/internal/workflow-patch-collect.js +2 -3
- package/package-dist/scripts/lib/import-curation/internal/workflow-queue-context.js +8 -8
- package/package-dist/scripts/lib/import-curation/internal/workflow-semantic-actions.js +12 -2
- package/package.json +2 -2
- package/specs/import-profiles.json +1 -1
- package/specs/schemas/foundry-authorization-input.schema.json +45 -2
- package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
- package/specs/schemas/foundry-package-descriptor.schema.json +2 -2
- package/specs/schemas/foundry-task-start.schema.json +200 -19
- package/specs/schemas/runtime-qualification.schema.json +1 -1
|
@@ -6,21 +6,32 @@ import { captureFoundryInput, resolveFoundryInputPath, resolveFoundryOutput, wri
|
|
|
6
6
|
import { sha256Json } from "./identity-preflight-proof.js";
|
|
7
7
|
import { fail, object, exact, timestamp, bytes, digest, taskPath, relative, readTaskBytes, readTaskJson, reference, facts, sameFact, shaPattern, maxIndexBytes, } from "./foundry-task-io.js";
|
|
8
8
|
import { requiredTask, createTask, loadTask, bindAccountIntent, } from "./foundry-task-registration.js";
|
|
9
|
+
/**
|
|
10
|
+
* The synchronous verified snapshot of one task: registered job, bound account intent, hash-chained
|
|
11
|
+
* artifact index and re-verified frozen sources. It is the same verification the locked metadata
|
|
12
|
+
* transaction performs, exposed for read-only projections that cannot await; callers that read a
|
|
13
|
+
* *foreign* task synchronously must re-check the snapshot's stability themselves.
|
|
14
|
+
*/
|
|
15
|
+
export function readVerifiedTaskSnapshot(context, options = {}) {
|
|
16
|
+
requiredTask(context);
|
|
17
|
+
const task = loadTask(context, {});
|
|
18
|
+
bindAccountIntent(context);
|
|
19
|
+
const index = readIndex(context);
|
|
20
|
+
const extra = index
|
|
21
|
+
.filter((entry) => options.verifyCommands?.includes(entry.command))
|
|
22
|
+
.map((entry) => ({
|
|
23
|
+
path: taskPath(context, entry.path),
|
|
24
|
+
bytes: entry.bytes,
|
|
25
|
+
sha256: entry.sha256,
|
|
26
|
+
}));
|
|
27
|
+
verifyInputs(context, task, index, [...context.inputs, ...extra]);
|
|
28
|
+
return { task, index };
|
|
29
|
+
}
|
|
9
30
|
export async function withFoundryTaskMetadata(context, inspect, options = {}) {
|
|
10
31
|
requiredTask(context);
|
|
11
32
|
const inspectCurrent = () => {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const index = readIndex(context);
|
|
15
|
-
const extra = index
|
|
16
|
-
.filter((entry) => options.verifyCommands?.includes(entry.command))
|
|
17
|
-
.map((entry) => ({
|
|
18
|
-
path: taskPath(context, entry.path),
|
|
19
|
-
bytes: entry.bytes,
|
|
20
|
-
sha256: entry.sha256,
|
|
21
|
-
}));
|
|
22
|
-
verifyInputs(context, task, index, [...context.inputs, ...extra]);
|
|
23
|
-
return inspect(task, index);
|
|
33
|
+
const snapshot = readVerifiedTaskSnapshot(context, options);
|
|
34
|
+
return inspect(snapshot.task, snapshot.index);
|
|
24
35
|
};
|
|
25
36
|
if (context.workspaceAccess === "read")
|
|
26
37
|
return inspectCurrent();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { FoundryInputFact } from "./foundry-runtime-context.ts";
|
|
2
2
|
export type JsonRecord = Record<string, unknown>;
|
|
3
|
-
export type Lane = "external-dataset-curated-import" | "source-evidence-dataset-development";
|
|
3
|
+
export type Lane = "external-dataset-curated-import" | "source-evidence-dataset-development" | "existing-owner-draft-repair";
|
|
4
4
|
export interface FileReference {
|
|
5
5
|
path: string;
|
|
6
6
|
sha256: string;
|
|
@@ -29,6 +29,17 @@ export interface FoundryTaskJob {
|
|
|
29
29
|
remote_state_code: 0;
|
|
30
30
|
};
|
|
31
31
|
created_at_utc: string;
|
|
32
|
+
/** Present only for the existing-owner-draft-repair lane; absent bytes stay byte-identical. */
|
|
33
|
+
repair?: Readonly<{
|
|
34
|
+
kind: string;
|
|
35
|
+
contract: string;
|
|
36
|
+
before: string;
|
|
37
|
+
candidate: string;
|
|
38
|
+
predecessor: Readonly<{
|
|
39
|
+
task_id: string;
|
|
40
|
+
receipt_sha256: string;
|
|
41
|
+
}> | null;
|
|
42
|
+
}>;
|
|
32
43
|
}
|
|
33
44
|
export interface SourceManifest {
|
|
34
45
|
schema: "tiangong-foundry.source-manifest.v2";
|
|
@@ -43,6 +54,16 @@ export interface FoundryTaskOptions {
|
|
|
43
54
|
requestId?: string;
|
|
44
55
|
targetEntities?: string[];
|
|
45
56
|
seed?: JsonRecord;
|
|
57
|
+
repair?: Readonly<{
|
|
58
|
+
kind: string;
|
|
59
|
+
contract: string;
|
|
60
|
+
before: string;
|
|
61
|
+
candidate: string;
|
|
62
|
+
predecessor: Readonly<{
|
|
63
|
+
task_id: string;
|
|
64
|
+
receipt_sha256: string;
|
|
65
|
+
}> | null;
|
|
66
|
+
}>;
|
|
46
67
|
}
|
|
47
68
|
export interface ArtifactFact extends FoundryInputFact {
|
|
48
69
|
path: string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA: "tiangong-lca.cli-tidas-spec-source.v1";
|
|
2
|
+
export type TiangongLcaCliTidasSchemaSource = Readonly<{
|
|
3
|
+
schema: typeof TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA;
|
|
4
|
+
spec_repository: string;
|
|
5
|
+
spec_commit: string;
|
|
6
|
+
spec_version: string;
|
|
7
|
+
source_repository: string;
|
|
8
|
+
source_commit: string;
|
|
9
|
+
manifest_sha256: string;
|
|
10
|
+
schemas: readonly {
|
|
11
|
+
name: string;
|
|
12
|
+
sha256: string;
|
|
13
|
+
}[];
|
|
14
|
+
}>;
|
|
15
|
+
export declare function resolveTiangongLcaCliTidasSource(sourceManifestPath: string, schemaDir: string): TiangongLcaCliTidasSchemaSource;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
const cliPackageName = "@tiangong-lca/cli";
|
|
5
|
+
export const TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA = "tiangong-lca.cli-tidas-spec-source.v1";
|
|
6
|
+
const expectedSource = Object.freeze({
|
|
7
|
+
schema: TIANGONG_LCA_CLI_TIDAS_SOURCE_SCHEMA,
|
|
8
|
+
spec_repository: "tiangong-lca/tidas-spec",
|
|
9
|
+
spec_commit: "d4cb089c753ffd20b173db2e56fb553a364f48f4",
|
|
10
|
+
spec_version: "0.2.1",
|
|
11
|
+
source_repository: "https://github.com/tiangong-lca/tidas-toolkit",
|
|
12
|
+
source_commit: "9c0d8b1c8ceb1841074f5bc6de5fbb7fcc9318f5",
|
|
13
|
+
manifest_sha256: "16d4d5950496544b50cd6b1570789930da104af313a325bb9eaa466507ec6bb5",
|
|
14
|
+
});
|
|
15
|
+
function isRecord(value) {
|
|
16
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
function sha256File(filePath) {
|
|
19
|
+
return createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
|
|
20
|
+
}
|
|
21
|
+
function parseSource(value) {
|
|
22
|
+
const keys = [
|
|
23
|
+
"manifest_sha256",
|
|
24
|
+
"schema",
|
|
25
|
+
"schemas",
|
|
26
|
+
"source_commit",
|
|
27
|
+
"source_repository",
|
|
28
|
+
"spec_commit",
|
|
29
|
+
"spec_repository",
|
|
30
|
+
"spec_version",
|
|
31
|
+
];
|
|
32
|
+
if (Object.keys(value).length !== keys.length ||
|
|
33
|
+
keys.some((key) => !Object.hasOwn(value, key)) ||
|
|
34
|
+
value.schema !== expectedSource.schema ||
|
|
35
|
+
value.spec_repository !== expectedSource.spec_repository ||
|
|
36
|
+
value.spec_commit !== expectedSource.spec_commit ||
|
|
37
|
+
value.spec_version !== expectedSource.spec_version ||
|
|
38
|
+
value.source_repository !== expectedSource.source_repository ||
|
|
39
|
+
value.source_commit !== expectedSource.source_commit ||
|
|
40
|
+
value.manifest_sha256 !== expectedSource.manifest_sha256)
|
|
41
|
+
throw new Error(`Installed ${cliPackageName} TIDAS source manifest does not match the approved spec 0.2.1 source identity.`);
|
|
42
|
+
if (!Array.isArray(value.schemas) || value.schemas.length !== 18)
|
|
43
|
+
throw new Error(`Installed ${cliPackageName} TIDAS source manifest must enumerate exactly 18 schemas.`);
|
|
44
|
+
const schemas = value.schemas.map((entry, index) => {
|
|
45
|
+
if (!isRecord(entry) ||
|
|
46
|
+
Object.keys(entry).length !== 2 ||
|
|
47
|
+
typeof entry.name !== "string" ||
|
|
48
|
+
!/^tidas_[a-z0-9_]+\.json$/u.test(entry.name) ||
|
|
49
|
+
typeof entry.sha256 !== "string" ||
|
|
50
|
+
!/^[0-9a-f]{64}$/u.test(entry.sha256))
|
|
51
|
+
throw new Error(`Installed ${cliPackageName} TIDAS source manifest has an invalid schema entry at index ${index}.`);
|
|
52
|
+
return Object.freeze({ name: entry.name, sha256: entry.sha256 });
|
|
53
|
+
});
|
|
54
|
+
if (new Set(schemas.map((entry) => entry.name)).size !== schemas.length)
|
|
55
|
+
throw new Error(`Installed ${cliPackageName} TIDAS source manifest schema entries must be unique.`);
|
|
56
|
+
return Object.freeze({
|
|
57
|
+
schema: expectedSource.schema,
|
|
58
|
+
spec_repository: expectedSource.spec_repository,
|
|
59
|
+
spec_commit: expectedSource.spec_commit,
|
|
60
|
+
spec_version: expectedSource.spec_version,
|
|
61
|
+
source_repository: expectedSource.source_repository,
|
|
62
|
+
source_commit: expectedSource.source_commit,
|
|
63
|
+
manifest_sha256: expectedSource.manifest_sha256,
|
|
64
|
+
schemas: Object.freeze(schemas),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
export function resolveTiangongLcaCliTidasSource(sourceManifestPath, schemaDir) {
|
|
68
|
+
let source;
|
|
69
|
+
try {
|
|
70
|
+
source = parseSource(JSON.parse(fs.readFileSync(sourceManifestPath, "utf8")));
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error instanceof Error && error.message.includes("TIDAS source manifest"))
|
|
74
|
+
throw error;
|
|
75
|
+
throw new Error(`Installed ${cliPackageName} TIDAS source manifest is unreadable at ${sourceManifestPath}.`, { cause: error });
|
|
76
|
+
}
|
|
77
|
+
const actualEntries = fs.readdirSync(schemaDir, { withFileTypes: true });
|
|
78
|
+
if (actualEntries.some((entry) => !entry.isFile()))
|
|
79
|
+
throw new Error(`Installed ${cliPackageName} TIDAS schema directory contains a non-file entry.`);
|
|
80
|
+
const actualNames = actualEntries.map((entry) => entry.name).sort();
|
|
81
|
+
const expectedNames = source.schemas.map((entry) => entry.name).sort();
|
|
82
|
+
if (JSON.stringify(actualNames) !== JSON.stringify(expectedNames))
|
|
83
|
+
throw new Error(`Installed ${cliPackageName} TIDAS schema files do not match its approved source manifest.`);
|
|
84
|
+
for (const entry of source.schemas) {
|
|
85
|
+
const filePath = path.join(schemaDir, entry.name);
|
|
86
|
+
if (sha256File(filePath) !== entry.sha256)
|
|
87
|
+
throw new Error(`Installed ${cliPackageName} TIDAS schema hash mismatch for ${entry.name}.`);
|
|
88
|
+
}
|
|
89
|
+
return source;
|
|
90
|
+
}
|
|
@@ -11,13 +11,14 @@ export declare function recordFoundryWorkflowAuthorization(context: FoundryRunti
|
|
|
11
11
|
scope: Record<string, unknown>;
|
|
12
12
|
finalizationSha256: string;
|
|
13
13
|
datasetType: string;
|
|
14
|
-
inputKind: "current_rows" | "final_rows";
|
|
14
|
+
inputKind: "current_rows" | "final_rows" | "repair_rows";
|
|
15
15
|
registration: {
|
|
16
16
|
authorization_sha256: string;
|
|
17
17
|
pointer_sha256: string;
|
|
18
18
|
};
|
|
19
19
|
submissionSha256: string;
|
|
20
20
|
executionContract?: FoundryInputFact;
|
|
21
|
+
repairPreparation?: FoundryInputFact;
|
|
21
22
|
operationOptions: Record<string, unknown>;
|
|
22
23
|
validateCurrent: (index: readonly ArtifactEntry[]) => void;
|
|
23
24
|
}): Promise<import("./foundry-task-types.ts").JsonRecord>;
|
|
@@ -3,7 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { assertSelectedAuthorizationInput, } from "./foundry-authorization-input.js";
|
|
5
5
|
import { readSelectedSemanticBytes } from "./foundry-semantic-input.js";
|
|
6
|
-
import { FoundryContextError, readFoundryInput, captureFoundryInput, resolveFoundryOutput, } from "./foundry-runtime-context.js";
|
|
6
|
+
import { FoundryContextError, readFoundryInput, captureFoundryInput, resolveFoundryInputPath, resolveFoundryOutput, } from "./foundry-runtime-context.js";
|
|
7
7
|
import { assertQualifiedFoundryRuntime, } from "./foundry-runtime-qualification.js";
|
|
8
8
|
import { currentWorkflowState, workflowObject } from "./foundry-workflow-state.js";
|
|
9
9
|
import { runFoundryTaskOperation } from "./foundry-task-store.js";
|
|
@@ -15,11 +15,16 @@ import { createFoundryExecutionCapsule } from "./foundry-execution-admission.js"
|
|
|
15
15
|
import { registerWorkflowStageFiles } from "./foundry-workflow-io.js";
|
|
16
16
|
import { createFoundryCommandSpec, createFileArtifactFact } from "./foundry-command-spec.js";
|
|
17
17
|
import { parseFoundryCommandSpec } from "@tiangong-lca/cli/command-spec";
|
|
18
|
-
import {
|
|
18
|
+
import { readNativeDraftHandoff } from "./finalize-owners/native-draft-handoff.js";
|
|
19
|
+
import { foundryRepairSelection, readFoundryRepairPreparation } from "./foundry-workflow-repair.js";
|
|
20
|
+
import { createFoundryRepairHandoffPlan } from "./foundry-repair-handoff.js";
|
|
21
|
+
import { readTaskJson } from "./foundry-task-io.js";
|
|
19
22
|
export async function authorizeFoundryWorkflow(context, qualified, entries, selected, authentication = { mode: "oauth" }) {
|
|
20
23
|
assertSelectedAuthorizationInput(selected);
|
|
21
24
|
assertQualifiedFoundryRuntime(context, qualified);
|
|
22
25
|
const state = currentWorkflowState(context, entries), spec = selected.spec;
|
|
26
|
+
if (spec.input_kind === "repair_rows")
|
|
27
|
+
return authorizeFoundryRepair(context, qualified, entries, selected, authentication, state);
|
|
23
28
|
const finalization = state.finalization;
|
|
24
29
|
if (state.authorization?.value.submission_sha256 === selected.descriptor.sha256)
|
|
25
30
|
return state.authorization.value;
|
|
@@ -39,7 +44,7 @@ export async function authorizeFoundryWorkflow(context, qualified, entries, sele
|
|
|
39
44
|
const identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
|
|
40
45
|
if (selected.executionContract) {
|
|
41
46
|
try {
|
|
42
|
-
|
|
47
|
+
readNativeDraftHandoff({
|
|
43
48
|
contractFile: selected.executionContract.path,
|
|
44
49
|
rowsFile: inputFile,
|
|
45
50
|
datasetType: spec.dataset_type,
|
|
@@ -50,7 +55,7 @@ export async function authorizeFoundryWorkflow(context, qualified, entries, sele
|
|
|
50
55
|
});
|
|
51
56
|
}
|
|
52
57
|
catch {
|
|
53
|
-
throw new FoundryContextError("authorization_execution_contract_invalid", "Native
|
|
58
|
+
throw new FoundryContextError("authorization_execution_contract_invalid", "Native draft contract must bind the current final rows, owner, project and draft state before authorization.");
|
|
54
59
|
}
|
|
55
60
|
}
|
|
56
61
|
const grant = JSON.parse(readSelectedSemanticBytes(selected.grant).toString("utf8"));
|
|
@@ -92,6 +97,153 @@ export async function authorizeFoundryWorkflow(context, qualified, entries, sele
|
|
|
92
97
|
},
|
|
93
98
|
});
|
|
94
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* The repair authority. A repair approval names the registered preparation report, never a
|
|
102
|
+
* finalization: the report digest is the scope authority, the scope must be a prepared dispatchable
|
|
103
|
+
* metadata repair of the locked generic profile, and the signed native contract must still bind the
|
|
104
|
+
* immutable candidate rows and the exact owner account. No successor or closeout is produced here.
|
|
105
|
+
*/
|
|
106
|
+
async function authorizeFoundryRepair(context, qualified, entries, selected, authentication, state) {
|
|
107
|
+
const spec = selected.spec;
|
|
108
|
+
const preparationFact = selected.repairPreparation;
|
|
109
|
+
if (state.authorization?.value.submission_sha256 === selected.descriptor.sha256)
|
|
110
|
+
return state.authorization.value;
|
|
111
|
+
const preparation = readFoundryRepairPreparation(context, entries);
|
|
112
|
+
if (!preparation)
|
|
113
|
+
throw new FoundryContextError("authorization_repair_preparation_missing", "A repair approval requires the current registered preparation report.");
|
|
114
|
+
if (preparation.entry.sha256 !== spec.finalization_sha256 ||
|
|
115
|
+
preparation.entry.sha256 !== preparationFact.sha256 ||
|
|
116
|
+
preparation.entry.bytes !== preparationFact.bytes)
|
|
117
|
+
throw new FoundryContextError("authorization_repair_preparation_mismatch", "Approval must select the registered preparation report bytes of this scope.");
|
|
118
|
+
const value = preparation.value;
|
|
119
|
+
if (value.status !== "prepared")
|
|
120
|
+
throw new FoundryContextError("authorization_repair_not_prepared", "Only a prepared dispatchable repair may be authorized; a no-change or blocked preparation carries no write authority.");
|
|
121
|
+
if (value.publication_ready !== false || value.remote_writes !== 0)
|
|
122
|
+
throw new FoundryContextError("authorization_repair_not_prepared", "Repair evidence must remain a non-publishing, zero-write preparation.");
|
|
123
|
+
const scope = workflowObject(value.scope);
|
|
124
|
+
const reportedActions = Array.isArray(scope.actions) ? scope.actions.map(workflowObject) : [];
|
|
125
|
+
if (scope.status !== "dispatchable" || !reportedActions.length)
|
|
126
|
+
throw new FoundryContextError("authorization_repair_scope_not_dispatchable", "A repair approval requires a dispatchable scope with at least one action.");
|
|
127
|
+
const job = readTaskJson(context, "foundry-job.json");
|
|
128
|
+
if (job.target_profile !== "generic")
|
|
129
|
+
throw new FoundryContextError("authorization_repair_profile_unsupported", "The repair lane admits only the generic profile.");
|
|
130
|
+
const selection = foundryRepairSelection(context);
|
|
131
|
+
const candidateFile = resolveFoundryInputPath(context, selection.candidate);
|
|
132
|
+
const beforeFile = resolveFoundryInputPath(context, selection.before);
|
|
133
|
+
const contractFile = selected.executionContract.path;
|
|
134
|
+
let native;
|
|
135
|
+
try {
|
|
136
|
+
native = readNativeDraftHandoff({
|
|
137
|
+
contractFile,
|
|
138
|
+
rowsFile: candidateFile,
|
|
139
|
+
datasetType: spec.dataset_type,
|
|
140
|
+
targetUserId: context.accountIntent.userId,
|
|
141
|
+
verifiedProjectRef: context.accountIntent.projectRef,
|
|
142
|
+
stateCode: "0",
|
|
143
|
+
relativePath: (file) => path.relative(context.workspaceRoot, file),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
throw new FoundryContextError("authorization_execution_contract_invalid", "Native draft contract must bind the current repair candidate rows, owner, project and draft state.");
|
|
148
|
+
}
|
|
149
|
+
const reportInputs = workflowObject(value.inputs);
|
|
150
|
+
const sameFileBytes = (fact, file) => {
|
|
151
|
+
const item = workflowObject(fact);
|
|
152
|
+
const current = captureFoundryInput(file);
|
|
153
|
+
return item.sha256 === current.sha256 && item.bytes === current.bytes;
|
|
154
|
+
};
|
|
155
|
+
if (workflowObject(value.contract).canonical_sha256 !== native.canonical_sha256 ||
|
|
156
|
+
scope.contract_sha256 !== native.canonical_sha256 ||
|
|
157
|
+
!sameFileBytes(reportInputs.contract, contractFile) ||
|
|
158
|
+
!sameFileBytes(reportInputs.before, beforeFile) ||
|
|
159
|
+
!sameFileBytes(reportInputs.candidate, candidateFile) ||
|
|
160
|
+
workflowObject(reportInputs.candidate).sha256 !== spec.input_sha256)
|
|
161
|
+
throw new FoundryContextError("authorization_repair_scope_mismatch", "The registered preparation must bind the exact selected contract, before and candidate bytes.");
|
|
162
|
+
for (const [index, action] of native.contract.actions.entries()) {
|
|
163
|
+
const reported = reportedActions[index];
|
|
164
|
+
if (reportedActions.length !== native.contract.actions.length ||
|
|
165
|
+
reported.action_id !== action.action_id ||
|
|
166
|
+
reported.table !== action.table ||
|
|
167
|
+
reported.id !== action.id ||
|
|
168
|
+
reported.version !== action.version ||
|
|
169
|
+
reported.before_sha256 !== action.before_sha256 ||
|
|
170
|
+
reported.desired_sha256 !== action.desired_sha256)
|
|
171
|
+
throw new FoundryContextError("authorization_repair_action_mismatch", "Registered repair actions must equal the signed native contract actions.");
|
|
172
|
+
}
|
|
173
|
+
const identity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
|
|
174
|
+
const grant = JSON.parse(readSelectedSemanticBytes(selected.grant).toString("utf8"));
|
|
175
|
+
const current = (index) => {
|
|
176
|
+
assertSelectedAuthorizationInput(selected);
|
|
177
|
+
const again = readFoundryRepairPreparation(context, index);
|
|
178
|
+
if (!again ||
|
|
179
|
+
again.entry.sha256 !== preparation.entry.sha256 ||
|
|
180
|
+
again.entry.bytes !== preparation.entry.bytes)
|
|
181
|
+
throw new FoundryContextError("authorization_repair_preparation_changed", "Repair evidence changed before approval activation.");
|
|
182
|
+
};
|
|
183
|
+
const registration = await registerFoundryTaskAuthorization(context, identity, {
|
|
184
|
+
inputFile: candidateFile,
|
|
185
|
+
grant,
|
|
186
|
+
evidence: spec.evidence.map((item, index) => ({
|
|
187
|
+
id: item.id,
|
|
188
|
+
kind: item.kind,
|
|
189
|
+
file: selected.evidence[index],
|
|
190
|
+
})),
|
|
191
|
+
expectedPreviousSha256: spec.expected_previous_sha256,
|
|
192
|
+
validateCurrent: (_, index) => current(index),
|
|
193
|
+
}, qualified);
|
|
194
|
+
const authorization = await loadFoundryTaskAuthorization(context, identity, candidateFile, qualified);
|
|
195
|
+
return recordFoundryWorkflowAuthorization(context, qualified, identity, authorization, authentication, {
|
|
196
|
+
inputFile: candidateFile,
|
|
197
|
+
approvedInputFile: candidateFile,
|
|
198
|
+
scope: { type: spec.dataset_type, status: scope.status, actions: reportedActions },
|
|
199
|
+
finalizationSha256: spec.finalization_sha256,
|
|
200
|
+
datasetType: spec.dataset_type,
|
|
201
|
+
inputKind: spec.input_kind,
|
|
202
|
+
registration,
|
|
203
|
+
submissionSha256: selected.descriptor.sha256,
|
|
204
|
+
executionContract: selected.executionContract,
|
|
205
|
+
repairPreparation: preparationFact,
|
|
206
|
+
validateCurrent: current,
|
|
207
|
+
operationOptions: {
|
|
208
|
+
submission: selected.descriptor,
|
|
209
|
+
grant: selected.grant,
|
|
210
|
+
evidence: selected.evidence,
|
|
211
|
+
repair_preparation: selected.repairPreparation,
|
|
212
|
+
finalization: spec.finalization_sha256,
|
|
213
|
+
authorization: registration.authorization_sha256,
|
|
214
|
+
execution_contract: selected.executionContract,
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Re-bind every emitted handoff command to the host-selected execution inputs: the final-row fact is
|
|
220
|
+
* re-derived from the approved file and every other bound artifact is resolved against the asset root.
|
|
221
|
+
* The commands keep their exact argv, so only the artifact paths are normalized.
|
|
222
|
+
*/
|
|
223
|
+
function bindHandoffCommands(context, handoff, inputFile) {
|
|
224
|
+
const commands = workflowObject(handoff.commands);
|
|
225
|
+
for (const key of ["commit", "post_write_verify"]) {
|
|
226
|
+
if (!commands[key])
|
|
227
|
+
continue;
|
|
228
|
+
const command = parseFoundryCommandSpec(commands[key]);
|
|
229
|
+
commands[key] = createFoundryCommandSpec({
|
|
230
|
+
executable: command.executable,
|
|
231
|
+
argv: [...command.argv],
|
|
232
|
+
binding: {
|
|
233
|
+
artifacts: [
|
|
234
|
+
createFileArtifactFact({ role: "final_rows", path: inputFile, filePath: inputFile }),
|
|
235
|
+
...command.binding.artifacts
|
|
236
|
+
.filter((artifact) => artifact.role !== "final_rows")
|
|
237
|
+
.map((artifact) => ({
|
|
238
|
+
...artifact,
|
|
239
|
+
path: path.resolve(context.assetRoot, artifact.path),
|
|
240
|
+
})),
|
|
241
|
+
],
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
return handoff;
|
|
246
|
+
}
|
|
95
247
|
export async function recordFoundryWorkflowAuthorization(context, qualified, identity, authorization, authentication, request) {
|
|
96
248
|
const { inputFile, scope, registration } = request;
|
|
97
249
|
const nonce = randomUUID(), output = resolveFoundryOutput(context, `outputs/authorization/${nonce}`);
|
|
@@ -124,29 +276,9 @@ export async function recordFoundryWorkflowAuthorization(context, qualified, ide
|
|
|
124
276
|
? { executionContractFile: request.executionContract.path }
|
|
125
277
|
: {}),
|
|
126
278
|
}));
|
|
127
|
-
|
|
128
|
-
for (const key of ["commit", "post_write_verify"]) {
|
|
129
|
-
if (!commands[key])
|
|
130
|
-
continue;
|
|
131
|
-
const command = parseFoundryCommandSpec(commands[key]);
|
|
132
|
-
commands[key] = createFoundryCommandSpec({
|
|
133
|
-
executable: command.executable,
|
|
134
|
-
argv: [...command.argv],
|
|
135
|
-
binding: {
|
|
136
|
-
artifacts: [
|
|
137
|
-
createFileArtifactFact({ role: "final_rows", path: inputFile, filePath: inputFile }),
|
|
138
|
-
...command.binding.artifacts
|
|
139
|
-
.filter((artifact) => artifact.role !== "final_rows")
|
|
140
|
-
.map((artifact) => ({
|
|
141
|
-
...artifact,
|
|
142
|
-
path: path.resolve(context.assetRoot, artifact.path),
|
|
143
|
-
})),
|
|
144
|
-
],
|
|
145
|
-
},
|
|
146
|
-
});
|
|
147
|
-
}
|
|
279
|
+
handoff = bindHandoffCommands(context, handoff, inputFile);
|
|
148
280
|
fs.writeFileSync(path.join(output, "handoff", "dataset-commit-handoff-plan.json"), JSON.stringify(handoff, null, 2) + "\n");
|
|
149
|
-
const command = commands.commit;
|
|
281
|
+
const command = workflowObject(handoff.commands).commit;
|
|
150
282
|
if (command) {
|
|
151
283
|
const requiredActions = authorization.allowed_actions.filter((action) => request.datasetType === "flow"
|
|
152
284
|
? action === "elementary_flow_write" || action === "elementary_flow_create_new"
|
|
@@ -174,6 +306,39 @@ export async function recordFoundryWorkflowAuthorization(context, qualified, ide
|
|
|
174
306
|
});
|
|
175
307
|
}
|
|
176
308
|
}
|
|
309
|
+
if (request.inputKind === "repair_rows") {
|
|
310
|
+
// The repair scope has no finalization: its write plan is the reviewed native save-draft
|
|
311
|
+
// handoff over the immutable candidate rows and the selected execution contract. The capsule
|
|
312
|
+
// seals exactly that command, and the owner execution request stays a separate later stage.
|
|
313
|
+
const handoffDir = path.join(output, "handoff");
|
|
314
|
+
fs.mkdirSync(handoffDir, { recursive: true, mode: 0o700 });
|
|
315
|
+
handoff = bindHandoffCommands(context, createFoundryRepairHandoffPlan({
|
|
316
|
+
context,
|
|
317
|
+
candidateFile: inputFile,
|
|
318
|
+
contractFile: request.executionContract.path,
|
|
319
|
+
outDir: handoffDir,
|
|
320
|
+
}), inputFile);
|
|
321
|
+
fs.writeFileSync(path.join(handoffDir, "dataset-commit-handoff-plan.json"), JSON.stringify(handoff, null, 2) + "\n");
|
|
322
|
+
const command = workflowObject(handoff.commands).commit;
|
|
323
|
+
if (command) {
|
|
324
|
+
try {
|
|
325
|
+
assertVerifiedFoundryIdentity(context, identity, qualified);
|
|
326
|
+
}
|
|
327
|
+
catch (error) {
|
|
328
|
+
if (!(error instanceof FoundryContextError) || error.code !== "identity_receipt_stale")
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
const sealingIdentity = verifyFoundryRuntimeIdentity(context, authentication, process.env, qualified);
|
|
332
|
+
capsule = await createFoundryExecutionCapsule(context, qualified, sealingIdentity, {
|
|
333
|
+
command: "dataset-commit-handoff-plan",
|
|
334
|
+
approvedInputFile: request.approvedInputFile,
|
|
335
|
+
finalRowsFile: inputFile,
|
|
336
|
+
commandSpec: command,
|
|
337
|
+
requiredActions: [],
|
|
338
|
+
requiredQaWaivers: [],
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
177
342
|
return await runFoundryTaskOperation(context, {
|
|
178
343
|
command: "dataset-workflow-authorization",
|
|
179
344
|
options: request.operationOptions,
|
|
@@ -198,6 +363,9 @@ export async function recordFoundryWorkflowAuthorization(context, qualified, ide
|
|
|
198
363
|
capsule,
|
|
199
364
|
handoff,
|
|
200
365
|
submission_sha256: request.submissionSha256,
|
|
366
|
+
...(request.repairPreparation
|
|
367
|
+
? { repair_preparation: { ...request.repairPreparation } }
|
|
368
|
+
: {}),
|
|
201
369
|
};
|
|
202
370
|
operation.writeJson(path.join(output, "foundry-authorization.json"), result);
|
|
203
371
|
return result;
|
|
@@ -86,6 +86,10 @@ export async function prepareFoundryWorkflowContext(context, qualified, selected
|
|
|
86
86
|
const report = {
|
|
87
87
|
schema: "tiangong-foundry.context-stage.v1",
|
|
88
88
|
status: "completed",
|
|
89
|
+
cli: {
|
|
90
|
+
package: cli.packageSpec,
|
|
91
|
+
tidas_spec_source: cli.tidasSpecSource,
|
|
92
|
+
},
|
|
89
93
|
reports,
|
|
90
94
|
files,
|
|
91
95
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
+
import { assertFoundryRepairExecution } from "./foundry-repair-execution.js";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { spawnSync } from "node:child_process";
|
|
4
5
|
import { randomUUID } from "node:crypto";
|
|
@@ -29,7 +30,10 @@ export async function executeFoundryOwnerScope(context, qualified, entries, requ
|
|
|
29
30
|
reason: "Foundry owner execution and readback",
|
|
30
31
|
}, async () => {
|
|
31
32
|
assertQualifiedFoundryRuntime(context, qualified);
|
|
32
|
-
const inspected = await withFoundryTaskMetadata(context, (_, index) =>
|
|
33
|
+
const inspected = await withFoundryTaskMetadata(context, (_, index) => {
|
|
34
|
+
assertFoundryRepairExecution(context, index, request);
|
|
35
|
+
return inspectOwnerExecutions(context, index);
|
|
36
|
+
});
|
|
33
37
|
const consumed = inspected.consumed.get(request.scope_id);
|
|
34
38
|
if (consumed && consumed.entry.sha256 !== requested.entry.sha256)
|
|
35
39
|
throw new FoundryContextError("execution_request_changed", "A consumed scope must recover its original request.");
|
|
@@ -77,6 +81,7 @@ export async function executeFoundryOwnerScope(context, qualified, entries, requ
|
|
|
77
81
|
lastRead = await readbackFoundryOwner(context, qualified, request, authentication, commitReport);
|
|
78
82
|
return lastRead.proof.status === "verified" ? lastRead : null;
|
|
79
83
|
};
|
|
84
|
+
await withFoundryTaskMetadata(context, (_, index) => assertFoundryRepairExecution(context, index, request));
|
|
80
85
|
const batch = await runBoundedBatch({
|
|
81
86
|
contract: request.contract,
|
|
82
87
|
items: [request],
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
|
|
2
|
+
import { type QualifiedFoundryRuntime } from "./foundry-runtime-qualification.ts";
|
|
3
|
+
import { type FoundryAuthentication } from "./foundry-authentication-environment.ts";
|
|
4
|
+
import type { ArtifactEntry } from "./foundry-task-types.ts";
|
|
5
|
+
type JsonRecord = Record<string, unknown>;
|
|
6
|
+
/** The task-store command this owner registers under; the facade owns invoking it. */
|
|
7
|
+
export declare const FOUNDRY_REPAIR_PREFLIGHT_COMMAND: "dataset-workflow-repair-preflight";
|
|
8
|
+
export declare const FOUNDRY_REPAIR_REPORT_NAME = "foundry-repair-preflight.json";
|
|
9
|
+
export declare const FOUNDRY_REPAIR_JOB_SCHEMA = "tiangong-foundry.repair-preparation.v1";
|
|
10
|
+
export interface FoundryRepairSelectionFacts {
|
|
11
|
+
readonly kind: string;
|
|
12
|
+
readonly contract: string;
|
|
13
|
+
readonly before: string;
|
|
14
|
+
readonly candidate: string;
|
|
15
|
+
}
|
|
16
|
+
export interface FoundryRepairPreparationRead {
|
|
17
|
+
readonly entry: ArtifactEntry;
|
|
18
|
+
readonly file: string;
|
|
19
|
+
readonly value: JsonRecord;
|
|
20
|
+
}
|
|
21
|
+
/** The immutable repair selection of the current task job. */
|
|
22
|
+
export declare function foundryRepairSelection(context: FoundryRuntimeContext): FoundryRepairSelectionFacts;
|
|
23
|
+
/**
|
|
24
|
+
* Read the already-registered preparation report for the current task scope. The report and every
|
|
25
|
+
* CLI evidence file it references must exist in the same producer index with exact bytes and hash; a
|
|
26
|
+
* report that names this scope but fails verification is rejected rather than ignored.
|
|
27
|
+
*/
|
|
28
|
+
export declare function readFoundryRepairPreparation(context: FoundryRuntimeContext, entries: readonly ArtifactEntry[]): FoundryRepairPreparationRead | null;
|
|
29
|
+
/**
|
|
30
|
+
* Prepare one repair through the qualified owner CLI and register the result as trusted producer
|
|
31
|
+
* evidence. Remote reads run outside the task metadata lock; registration happens inside the store's
|
|
32
|
+
* operation (which re-verifies inputs and producers), and a repeated preparation returns the
|
|
33
|
+
* already-registered report without issuing another remote call.
|
|
34
|
+
*/
|
|
35
|
+
export declare function prepareFoundryRepair(context: FoundryRuntimeContext, qualified: QualifiedFoundryRuntime, _entries: readonly ArtifactEntry[], authentication: FoundryAuthentication): Promise<JsonRecord>;
|
|
36
|
+
export {};
|