@tiangong-lca/foundry 0.1.17 → 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/README.md +23 -7
- package/docs/architecture.md +15 -3
- package/docs/foundry-task-contracts.md +27 -3
- package/docs/package-distribution-contract.md +45 -13
- package/docs/public-runtime-contract.md +85 -5
- package/docs/runtime-context-contract.md +22 -4
- package/docs/task-authorization-contract.md +15 -3
- package/docs/workspace-migration-contract.md +5 -3
- package/package-dist/assets/foundry-package-descriptor.json +541 -177
- package/package-dist/scripts/foundry-facade.d.ts +5 -0
- package/package-dist/scripts/foundry-facade.js +261 -16
- package/package-dist/scripts/foundry-runtime.d.ts +2 -2
- package/package-dist/scripts/lib/bundle-sample-utils.d.ts +4 -4
- package/package-dist/scripts/lib/bundle-source-context.d.ts +6 -4
- package/package-dist/scripts/lib/bundle-source-context.js +10 -8
- package/package-dist/scripts/lib/decision-owners/identity-preflight.js +18 -6
- 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 +11 -1
- package/package-dist/scripts/lib/finalize-owners/post-authoring.js +65 -9
- package/package-dist/scripts/lib/foundry-assessment-producer.d.ts +6 -0
- package/package-dist/scripts/lib/foundry-assessment-producer.js +167 -0
- package/package-dist/scripts/lib/foundry-authentication-environment.js +1 -1
- package/package-dist/scripts/lib/foundry-command-metadata.js +6 -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-decision-owners.d.ts +1 -1
- package/package-dist/scripts/lib/foundry-decision-owners.js +15 -7
- 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 +28 -3
- 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 +126 -13
- package/package-dist/scripts/lib/foundry-identity-stage-input.d.ts +24 -0
- package/package-dist/scripts/lib/foundry-identity-stage-input.js +122 -0
- package/package-dist/scripts/lib/foundry-identity-stage-state.d.ts +17 -0
- package/package-dist/scripts/lib/foundry-identity-stage-state.js +48 -0
- package/package-dist/scripts/lib/foundry-installed-entry.d.ts +2 -0
- package/package-dist/scripts/lib/foundry-installed-entry.js +173 -0
- package/package-dist/scripts/lib/foundry-managed-adoption.d.ts +12 -0
- package/package-dist/scripts/lib/foundry-managed-adoption.js +108 -0
- package/package-dist/scripts/lib/foundry-managed-authentication.d.ts +4 -0
- package/package-dist/scripts/lib/foundry-managed-authentication.js +28 -0
- package/package-dist/scripts/lib/foundry-managed-host.js +14 -2
- package/package-dist/scripts/lib/foundry-migration-transfer-io.d.ts +2 -0
- package/package-dist/scripts/lib/foundry-migration-transfer-io.js +27 -3
- package/package-dist/scripts/lib/foundry-operation-result.d.ts +1 -1
- package/package-dist/scripts/lib/foundry-operation-result.js +1 -0
- 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 +4 -4
- package/package-dist/scripts/lib/foundry-package-contract.js +10 -2
- package/package-dist/scripts/lib/foundry-preparation-cli-validation.d.ts +2 -0
- package/package-dist/scripts/lib/foundry-preparation-cli-validation.js +96 -3
- 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-quality-review-context.d.ts +3 -0
- package/package-dist/scripts/lib/foundry-quality-review-context.js +18 -0
- package/package-dist/scripts/lib/foundry-quality-review-input.d.ts +48 -0
- package/package-dist/scripts/lib/foundry-quality-review-input.js +284 -0
- package/package-dist/scripts/lib/foundry-quality-review-lineage.d.ts +13 -0
- package/package-dist/scripts/lib/foundry-quality-review-lineage.js +123 -0
- package/package-dist/scripts/lib/foundry-quality-review.d.ts +112 -0
- package/package-dist/scripts/lib/foundry-quality-review.js +487 -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 +22 -2
- 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-task-authorization.d.ts +2 -1
- package/package-dist/scripts/lib/foundry-task-authorization.js +32 -11
- package/package-dist/scripts/lib/foundry-task-registration.js +4 -5
- package/package-dist/scripts/lib/foundry-task-runtime-adoption-write.d.ts +28 -0
- package/package-dist/scripts/lib/foundry-task-runtime-adoption-write.js +250 -0
- package/package-dist/scripts/lib/foundry-task-runtime-adoption.d.ts +60 -0
- package/package-dist/scripts/lib/foundry-task-runtime-adoption.js +549 -0
- package/package-dist/scripts/lib/foundry-task-store.d.ts +4 -1
- package/package-dist/scripts/lib/foundry-task-store.js +113 -4
- package/package-dist/scripts/lib/foundry-task-types.d.ts +2 -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-validation-reference-context.d.ts +8 -0
- package/package-dist/scripts/lib/foundry-validation-reference-context.js +115 -0
- package/package-dist/scripts/lib/foundry-workflow-assessment.d.ts +1 -0
- package/package-dist/scripts/lib/foundry-workflow-assessment.js +165 -34
- package/package-dist/scripts/lib/foundry-workflow-authorization.js +99 -5
- package/package-dist/scripts/lib/foundry-workflow-child-failure.d.ts +44 -0
- package/package-dist/scripts/lib/foundry-workflow-child-failure.js +439 -0
- package/package-dist/scripts/lib/foundry-workflow-context.js +92 -22
- package/package-dist/scripts/lib/foundry-workflow-execution.js +54 -5
- package/package-dist/scripts/lib/foundry-workflow-finalize.js +64 -3
- package/package-dist/scripts/lib/foundry-workflow-identity-recovery.d.ts +6 -0
- package/package-dist/scripts/lib/foundry-workflow-identity-recovery.js +450 -0
- package/package-dist/scripts/lib/foundry-workflow-identity-stage.d.ts +9 -0
- package/package-dist/scripts/lib/foundry-workflow-identity-stage.js +717 -0
- package/package-dist/scripts/lib/foundry-workflow-identity.js +26 -11
- package/package-dist/scripts/lib/foundry-workflow-io.d.ts +7 -1
- package/package-dist/scripts/lib/foundry-workflow-io.js +102 -16
- 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 +65 -31
- package/package-dist/scripts/lib/foundry-workflow-state.d.ts +4 -0
- package/package-dist/scripts/lib/foundry-workflow-state.js +165 -22
- package/package-dist/scripts/lib/identity-preflight-artifacts.d.ts +4 -7
- package/package-dist/scripts/lib/identity-preflight-artifacts.js +18 -5
- package/package-dist/scripts/lib/identity-preflight-proof.d.ts +6 -0
- package/package-dist/scripts/lib/identity-preflight-proof.js +92 -13
- package/package-dist/scripts/lib/identity-preflight-recovery-proof.d.ts +78 -0
- package/package-dist/scripts/lib/identity-preflight-recovery-proof.js +415 -0
- package/package-dist/scripts/lib/import-curation/curation-cleanup.d.ts +9 -1
- package/package-dist/scripts/lib/import-curation/curation-cleanup.js +39 -2
- package/package-dist/scripts/lib/import-curation/curation-gate.d.ts +1 -0
- package/package-dist/scripts/lib/import-curation/curation-gate.js +19 -1
- package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +1 -1
- package/package-dist/scripts/lib/import-curation/internal/workflow-identity-preflight.js +11 -2
- package/package-dist/scripts/lib/import-curation/internal/workflow-reference-closure.js +4 -1
- package/package-dist/scripts/package-entry.js +2 -3
- package/package-dist/scripts/public-api.d.ts +5 -0
- package/package-dist/scripts/public-api.js +2 -0
- package/package-dist/scripts/runtime-entry.d.ts +2 -0
- package/package-dist/scripts/runtime-entry.js +42 -15
- package/package.json +6 -2
- package/specs/schemas/foundry-identity-stage-input.schema.json +47 -0
- package/specs/schemas/foundry-managed-adoption.schema.json +40 -0
- package/specs/schemas/foundry-managed-runtime.schema.json +1 -1
- package/specs/schemas/foundry-operation-result.schema.json +1 -0
- package/specs/schemas/foundry-package-descriptor.schema.json +6 -2
- package/specs/schemas/foundry-quality-review-draft.schema.json +271 -0
- package/specs/schemas/foundry-quality-review-input.schema.json +271 -0
- package/specs/schemas/runtime-qualification.schema.json +1 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { captureFoundryInput, FoundryContextError, } from "./foundry-runtime-context.js";
|
|
4
|
+
import { assertNotFoundrySessionFile, migrationCredentialPath } from "./foundry-private-path.js";
|
|
5
|
+
import { readSelectedSemanticBytes } from "./foundry-semantic-input.js";
|
|
6
|
+
import { sha256Json } from "./identity-preflight-proof.js";
|
|
7
|
+
export const FOUNDRY_IDENTITY_STAGE_INPUT_SCHEMA = "tiangong-foundry.identity-stage-input.v1";
|
|
8
|
+
const selectedInputs = new WeakMap();
|
|
9
|
+
const sha = /^[0-9a-f]{64}$/u;
|
|
10
|
+
const token = /^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,255}$/u;
|
|
11
|
+
function invalid(message) {
|
|
12
|
+
throw new FoundryContextError("identity_stage_input_invalid", message);
|
|
13
|
+
}
|
|
14
|
+
function record(value) {
|
|
15
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
16
|
+
invalid("Explicit identity stage input must be an object.");
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function exact(value, keys) {
|
|
20
|
+
if (Object.keys(value).length !== keys.length || keys.some((key) => !Object.hasOwn(value, key)))
|
|
21
|
+
invalid("Explicit identity stage has missing or unsupported fields.");
|
|
22
|
+
}
|
|
23
|
+
export function parseFoundryIdentityStageInput(value) {
|
|
24
|
+
const data = record(value);
|
|
25
|
+
exact(data, [
|
|
26
|
+
"schema",
|
|
27
|
+
"intent_id",
|
|
28
|
+
"task_id",
|
|
29
|
+
"actor_id",
|
|
30
|
+
"rows_report_sha256",
|
|
31
|
+
"predecessor_identity_sha256",
|
|
32
|
+
"targets",
|
|
33
|
+
]);
|
|
34
|
+
if (data.schema !== FOUNDRY_IDENTITY_STAGE_INPUT_SCHEMA ||
|
|
35
|
+
typeof data.intent_id !== "string" ||
|
|
36
|
+
!token.test(data.intent_id) ||
|
|
37
|
+
typeof data.task_id !== "string" ||
|
|
38
|
+
!/^task-[0-9a-f]{64}-r[0-9]{4}$/u.test(data.task_id) ||
|
|
39
|
+
typeof data.actor_id !== "string" ||
|
|
40
|
+
!token.test(data.actor_id) ||
|
|
41
|
+
typeof data.rows_report_sha256 !== "string" ||
|
|
42
|
+
!sha.test(data.rows_report_sha256) ||
|
|
43
|
+
typeof data.predecessor_identity_sha256 !== "string" ||
|
|
44
|
+
!sha.test(data.predecessor_identity_sha256) ||
|
|
45
|
+
!Array.isArray(data.targets) ||
|
|
46
|
+
!data.targets.length ||
|
|
47
|
+
data.targets.length > 64)
|
|
48
|
+
invalid("Explicit identity stage needs one stable intent, original task/actor and bounded exact roster.");
|
|
49
|
+
const targets = data.targets.map((raw) => {
|
|
50
|
+
const item = record(raw);
|
|
51
|
+
exact(item, ["dataset_type", "dataset_id", "dataset_version", "source_row_sha256"]);
|
|
52
|
+
if ((item.dataset_type !== "flow" && item.dataset_type !== "process") ||
|
|
53
|
+
typeof item.dataset_id !== "string" ||
|
|
54
|
+
!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu.test(item.dataset_id) ||
|
|
55
|
+
typeof item.dataset_version !== "string" ||
|
|
56
|
+
!/^[0-9]{2}\.[0-9]{2}\.[0-9]{3}$/u.test(item.dataset_version) ||
|
|
57
|
+
typeof item.source_row_sha256 !== "string" ||
|
|
58
|
+
!sha.test(item.source_row_sha256))
|
|
59
|
+
invalid("Readonly identity targets must select exact Flow/Process identities and unchanged registered row hashes.");
|
|
60
|
+
return Object.freeze({
|
|
61
|
+
dataset_type: item.dataset_type,
|
|
62
|
+
dataset_id: item.dataset_id,
|
|
63
|
+
dataset_version: item.dataset_version,
|
|
64
|
+
source_row_sha256: item.source_row_sha256,
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
if (new Set(targets.map((item) => `${item.dataset_type}:${item.dataset_id}:${item.dataset_version}`)).size !== targets.length)
|
|
68
|
+
invalid("Explicit identity stage target roster contains duplicates.");
|
|
69
|
+
return Object.freeze({
|
|
70
|
+
schema: FOUNDRY_IDENTITY_STAGE_INPUT_SCHEMA,
|
|
71
|
+
intent_id: data.intent_id,
|
|
72
|
+
task_id: data.task_id,
|
|
73
|
+
actor_id: data.actor_id,
|
|
74
|
+
rows_report_sha256: data.rows_report_sha256,
|
|
75
|
+
predecessor_identity_sha256: data.predecessor_identity_sha256,
|
|
76
|
+
targets: Object.freeze(targets),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function contextBinding(context) {
|
|
80
|
+
return sha256Json({
|
|
81
|
+
workspace: context.workspaceId,
|
|
82
|
+
task: context.taskId,
|
|
83
|
+
actor: context.actorId,
|
|
84
|
+
project: context.accountIntent?.projectRef ?? null,
|
|
85
|
+
user: context.accountIntent?.userId ?? null,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export function selectFoundryIdentityStageInput(context, file) {
|
|
89
|
+
if (!file || file.length > 4096 || /[\0\r\n]/u.test(file))
|
|
90
|
+
invalid("Explicit identity input path is invalid.");
|
|
91
|
+
const target = path.resolve(context.workspaceRoot, file);
|
|
92
|
+
if (migrationCredentialPath(path.relative(context.workspaceRoot, target)))
|
|
93
|
+
invalid("Credential paths cannot select an identity stage.");
|
|
94
|
+
assertNotFoundrySessionFile(target, context.accountIntent?.sessionReference);
|
|
95
|
+
let stat;
|
|
96
|
+
try {
|
|
97
|
+
stat = fs.lstatSync(target);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
invalid("Explicit identity input must select an existing regular file.");
|
|
101
|
+
}
|
|
102
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024 * 1024)
|
|
103
|
+
invalid("Explicit identity input must be a bounded regular file.");
|
|
104
|
+
const descriptor = Object.freeze(captureFoundryInput(target));
|
|
105
|
+
let value;
|
|
106
|
+
try {
|
|
107
|
+
value = parseFoundryIdentityStageInput(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(readSelectedSemanticBytes(descriptor))));
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
invalid("Select a complete supported explicit identity-stage input.");
|
|
111
|
+
}
|
|
112
|
+
if (value.task_id !== context.taskId || value.actor_id !== context.actorId)
|
|
113
|
+
invalid("Explicit identity input belongs to another task or actor.");
|
|
114
|
+
const selected = Object.freeze({ descriptor, value });
|
|
115
|
+
selectedInputs.set(selected, contextBinding(context));
|
|
116
|
+
return selected;
|
|
117
|
+
}
|
|
118
|
+
export function assertSelectedFoundryIdentityStageInput(context, selected) {
|
|
119
|
+
if (!selectedInputs.has(selected) || selectedInputs.get(selected) !== contextBinding(context))
|
|
120
|
+
invalid("Explicit identity intent requires the independently selected current task/account context.");
|
|
121
|
+
readSelectedSemanticBytes(selected.descriptor);
|
|
122
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type FoundryRuntimeContext } from "./foundry-runtime-context.ts";
|
|
2
|
+
import type { ArtifactEntry } from "./foundry-task-types.ts";
|
|
3
|
+
/** Native indexed admissions remain visible even when result capture was interrupted. */
|
|
4
|
+
export declare function pendingFoundryIdentityStage(context: FoundryRuntimeContext, entries: readonly ArtifactEntry[]): Readonly<{
|
|
5
|
+
stage_id: string;
|
|
6
|
+
intent_id: unknown;
|
|
7
|
+
preparation: {
|
|
8
|
+
path: string;
|
|
9
|
+
bytes: number;
|
|
10
|
+
sha256: string;
|
|
11
|
+
};
|
|
12
|
+
indexed_dispatch_artifacts: number;
|
|
13
|
+
disposition: string;
|
|
14
|
+
new_cli_execution: null;
|
|
15
|
+
result_registered: boolean;
|
|
16
|
+
}> | null;
|
|
17
|
+
export declare function assertNoPendingFoundryIdentityStage(context: FoundryRuntimeContext, entries: readonly ArtifactEntry[]): void;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { FoundryContextError } from "./foundry-runtime-context.js";
|
|
3
|
+
import { readWorkflowArtifact } from "./foundry-workflow-state.js";
|
|
4
|
+
/** Native indexed admissions remain visible even when result capture was interrupted. */
|
|
5
|
+
export function pendingFoundryIdentityStage(context, entries) {
|
|
6
|
+
for (const entry of [...entries].reverse()) {
|
|
7
|
+
if (entry.command !== "dataset-workflow-identity-stage-prepare" ||
|
|
8
|
+
path.basename(entry.path) !== "identity-stage-preparation.json")
|
|
9
|
+
continue;
|
|
10
|
+
const prepared = readWorkflowArtifact(context, entry);
|
|
11
|
+
const stageId = prepared.value.stage_id;
|
|
12
|
+
if (prepared.value.schema !== "tiangong-foundry.identity-stage-preparation.v1" ||
|
|
13
|
+
typeof stageId !== "string" ||
|
|
14
|
+
!/^[a-f0-9]{64}$/u.test(stageId) ||
|
|
15
|
+
prepared.value.task_id !== context.taskId ||
|
|
16
|
+
prepared.value.actor_id !== context.actorId)
|
|
17
|
+
throw new FoundryContextError("identity_stage_unproven", "Indexed read-only stage admission is invalid; preserve its evidence without replay.");
|
|
18
|
+
const resultEntry = entries.findLast((candidate) => {
|
|
19
|
+
if (candidate.command !== "dataset-workflow-identity" ||
|
|
20
|
+
path.basename(candidate.path) !== "foundry-identity.json" ||
|
|
21
|
+
candidate.sequence <= entry.sequence)
|
|
22
|
+
return false;
|
|
23
|
+
return readWorkflowArtifact(context, candidate).value.stage_id === stageId;
|
|
24
|
+
});
|
|
25
|
+
const result = resultEntry ? readWorkflowArtifact(context, resultEntry).value : null;
|
|
26
|
+
if (result?.schema === "tiangong-foundry.identity-stage.v1" &&
|
|
27
|
+
result.explicit_new_stage === true &&
|
|
28
|
+
result.status === "completed")
|
|
29
|
+
continue;
|
|
30
|
+
const claims = entries.filter((candidate) => candidate.command === "dataset-workflow-identity-stage-dispatch" &&
|
|
31
|
+
candidate.sequence > entry.sequence &&
|
|
32
|
+
candidate.path.startsWith(path.dirname(path.dirname(entry.path)) + "/"));
|
|
33
|
+
return Object.freeze({
|
|
34
|
+
stage_id: stageId,
|
|
35
|
+
intent_id: prepared.value.intent_id,
|
|
36
|
+
preparation: { path: prepared.file, bytes: entry.bytes, sha256: entry.sha256 },
|
|
37
|
+
indexed_dispatch_artifacts: claims.length,
|
|
38
|
+
disposition: claims.length ? "CLAIMED_OUTCOME_UNPROVEN" : "PREPARED_OUTCOME_UNPROVEN",
|
|
39
|
+
new_cli_execution: null,
|
|
40
|
+
result_registered: result !== null,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
export function assertNoPendingFoundryIdentityStage(context, entries) {
|
|
46
|
+
if (pendingFoundryIdentityStage(context, entries))
|
|
47
|
+
throw new FoundryContextError("identity_stage_unproven", "The indexed explicit read-only stage has no proven completed result. Inspect its admission and retained claims; ordinary resume cannot dispatch another query.");
|
|
48
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { executeRuntimeLaunch, inspectRuntimeComponents, loadTrustedRuntimeManifest, RUNTIME_HOST_CONTEXT_PROTOCOL, } from "@tiangong-lca/cli/runtime";
|
|
5
|
+
import { runFoundryPublicCommand } from "../runtime-entry.js";
|
|
6
|
+
import { receiveFoundryManagedHost } from "./foundry-managed-host.js";
|
|
7
|
+
import { FoundryContextError } from "./foundry-runtime-error.js";
|
|
8
|
+
import { transferHash, transferRead } from "./foundry-migration-transfer-io.js";
|
|
9
|
+
const help = `Usage: tiangong-foundry runtime describe --json [--require-protocol <schema>]
|
|
10
|
+
tiangong-foundry <workspace|task|doctor> <public arguments>
|
|
11
|
+
|
|
12
|
+
Configure the installed entry with TIANGONG_FOUNDRY_MANIFEST and
|
|
13
|
+
TIANGONG_FOUNDRY_MANIFEST_SHA256 from a verified Foundry release.
|
|
14
|
+
TIANGONG_FOUNDRY_CACHE_DIR selects an existing absolute component cache.
|
|
15
|
+
TIANGONG_FOUNDRY_ENTRY optionally selects a declared launch (default: foundry).
|
|
16
|
+
|
|
17
|
+
runtime describe reads local identity, protocols and cache readiness without
|
|
18
|
+
authentication, downloads or workspace changes. Missing configuration/assets or
|
|
19
|
+
unsupported required protocols exit nonzero. Install the selected release with
|
|
20
|
+
the existing CLI runtime ensure command, then repeat runtime describe.
|
|
21
|
+
|
|
22
|
+
Configured operations use the selected verified managed runtime. Continue an
|
|
23
|
+
existing task through its returned executable/argv/cwd to retain its binding.
|
|
24
|
+
`;
|
|
25
|
+
function fail(code, message) {
|
|
26
|
+
throw new FoundryContextError(code, message);
|
|
27
|
+
}
|
|
28
|
+
function configuration(env) {
|
|
29
|
+
const manifest = env.TIANGONG_FOUNDRY_MANIFEST;
|
|
30
|
+
const digest = env.TIANGONG_FOUNDRY_MANIFEST_SHA256;
|
|
31
|
+
const cacheDir = env.TIANGONG_FOUNDRY_CACHE_DIR;
|
|
32
|
+
const entry = env.TIANGONG_FOUNDRY_ENTRY ?? "foundry";
|
|
33
|
+
if ([manifest, digest, cacheDir, env.TIANGONG_FOUNDRY_ENTRY].every((item) => item === undefined))
|
|
34
|
+
return null;
|
|
35
|
+
if (!manifest || !path.isAbsolute(manifest) || !digest || !/^[a-f0-9]{64}$/u.test(digest))
|
|
36
|
+
fail("runtime_configuration_invalid", "Set an absolute TIANGONG_FOUNDRY_MANIFEST and its independently verified TIANGONG_FOUNDRY_MANIFEST_SHA256 together.");
|
|
37
|
+
if (cacheDir !== undefined && (!cacheDir || !path.isAbsolute(cacheDir)))
|
|
38
|
+
fail("runtime_configuration_invalid", "TIANGONG_FOUNDRY_CACHE_DIR must be an absolute directory.");
|
|
39
|
+
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/u.test(entry))
|
|
40
|
+
fail("runtime_configuration_invalid", "TIANGONG_FOUNDRY_ENTRY must name a declared launch.");
|
|
41
|
+
if (!fs.existsSync(manifest))
|
|
42
|
+
fail("runtime_manifest_missing", "The configured manifest is missing. Install the selected release manifest before invoking Foundry.");
|
|
43
|
+
const trusted = loadTrustedRuntimeManifest(manifest, digest);
|
|
44
|
+
if (trusted.manifest.product.id !== "tiangong-foundry")
|
|
45
|
+
fail("runtime_product_unsupported", "The configured manifest must declare the tiangong-foundry product.");
|
|
46
|
+
const report = inspectRuntimeComponents(trusted, { cacheDir });
|
|
47
|
+
const launch = trusted.manifest.launches.find((item) => item.id === entry && item.platform === report.platform);
|
|
48
|
+
if (!launch ||
|
|
49
|
+
launch.context_protocol !== RUNTIME_HOST_CONTEXT_PROTOCOL ||
|
|
50
|
+
launch.argv.length !== 1 ||
|
|
51
|
+
!("component" in launch.argv[0]))
|
|
52
|
+
fail("runtime_launch_unsupported", "Select a declared Foundry managed launch with the runtime host protocol.");
|
|
53
|
+
return { trusted, report, launch, cacheDir, entry, manifest };
|
|
54
|
+
}
|
|
55
|
+
function describe(selection, required) {
|
|
56
|
+
const { trusted, report, launch } = selection;
|
|
57
|
+
const application = launch.argv[0];
|
|
58
|
+
if (!("component" in application))
|
|
59
|
+
fail("runtime_launch_unsupported", "Managed launch requires a package entry.");
|
|
60
|
+
let cliVersion = null;
|
|
61
|
+
let protocols = [];
|
|
62
|
+
if (report.status === "ready") {
|
|
63
|
+
const component = trusted.manifest.components.find((item) => item.id === application.component && item.platform === report.platform);
|
|
64
|
+
const installed = report.components.find((item) => item.id === application.component);
|
|
65
|
+
const descriptorPath = path.posix.join(path.posix.dirname(application.path), "../assets/foundry-package-descriptor.json");
|
|
66
|
+
const fact = component.files.find((item) => item.path === descriptorPath);
|
|
67
|
+
if (!fact)
|
|
68
|
+
fail("runtime_descriptor_missing", "Selected release does not bind a public Foundry package descriptor.");
|
|
69
|
+
const bytes = transferRead(path.join(installed.root, descriptorPath), 8 * 1024 * 1024);
|
|
70
|
+
if (bytes.length !== fact.bytes || transferHash(bytes) !== fact.sha256)
|
|
71
|
+
fail("runtime_descriptor_changed", "Selected package descriptor differs from the verified manifest.");
|
|
72
|
+
const value = JSON.parse(bytes.toString("utf8"));
|
|
73
|
+
if (value?.schema !== "tiangong-foundry.package-descriptor.v1" ||
|
|
74
|
+
value.package?.name !== "@tiangong-lca/foundry" ||
|
|
75
|
+
value.package.version !== trusted.manifest.product.version ||
|
|
76
|
+
value.package.cli_dependency?.name !== "@tiangong-lca/cli" ||
|
|
77
|
+
typeof value.package.cli_dependency.version !== "string" ||
|
|
78
|
+
!Array.isArray(value.runtime?.protocol_schemas) ||
|
|
79
|
+
!value.runtime.protocol_schemas.every((item) => typeof item === "string"))
|
|
80
|
+
fail("runtime_descriptor_unsupported", "Selected release has an unsupported or inconsistent package descriptor.");
|
|
81
|
+
cliVersion = value.package.cli_dependency.version;
|
|
82
|
+
protocols = value.runtime.protocol_schemas;
|
|
83
|
+
}
|
|
84
|
+
const missing = required.filter((protocol) => !protocols.includes(protocol));
|
|
85
|
+
const status = report.status === "ready" && missing.length ? "unsupported" : report.status;
|
|
86
|
+
return {
|
|
87
|
+
schema: "tiangong-foundry.runtime-description.v1",
|
|
88
|
+
status,
|
|
89
|
+
product: trusted.manifest.product,
|
|
90
|
+
cli_version: cliVersion,
|
|
91
|
+
manifest_sha256: trusted.sha256,
|
|
92
|
+
entry: selection.entry,
|
|
93
|
+
platform: report.platform,
|
|
94
|
+
components: report.components,
|
|
95
|
+
protocols,
|
|
96
|
+
protocol_assessment: report.status === "ready" ? "checked" : "not_checked",
|
|
97
|
+
unsupported_protocols: report.status === "ready" ? missing : [],
|
|
98
|
+
next: status === "missing"
|
|
99
|
+
? "Install the selected components with tiangong-lca runtime ensure using this manifest and digest."
|
|
100
|
+
: status === "ready"
|
|
101
|
+
? "Use the public task commands; retain returned actions for existing tasks."
|
|
102
|
+
: "Select a compatible verified release or repair its installation, then repeat runtime describe.",
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Deployment selection happens before managed IPC. Existing returned actions enter IPC directly. */
|
|
106
|
+
export async function runFoundryInstalledCommand(argv = process.argv) {
|
|
107
|
+
if (typeof process.send === "function") {
|
|
108
|
+
await runFoundryPublicCommand(argv, {}, receiveFoundryManagedHost);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const args = argv.slice(2);
|
|
112
|
+
if ((args.length === 1 && ["--help", "-h"].includes(args[0])) ||
|
|
113
|
+
(args[0] === "runtime" && args.includes("--help"))) {
|
|
114
|
+
process.stdout.write(help);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const controller = new AbortController();
|
|
118
|
+
const interrupt = () => controller.abort();
|
|
119
|
+
try {
|
|
120
|
+
const selected = configuration(process.env);
|
|
121
|
+
if (args[0] === "runtime") {
|
|
122
|
+
if (args[1] !== "describe")
|
|
123
|
+
fail("runtime_operation_unsupported", "Use tiangong-foundry runtime describe --json.");
|
|
124
|
+
const { values } = parseArgs({
|
|
125
|
+
args: args.slice(2),
|
|
126
|
+
strict: true,
|
|
127
|
+
allowPositionals: false,
|
|
128
|
+
options: {
|
|
129
|
+
json: { type: "boolean" },
|
|
130
|
+
"require-protocol": { type: "string", multiple: true },
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
if (!values.json)
|
|
134
|
+
fail("argument_json_required", "runtime describe requires --json.");
|
|
135
|
+
if (!selected)
|
|
136
|
+
fail("runtime_not_configured", "Configure TIANGONG_FOUNDRY_MANIFEST and TIANGONG_FOUNDRY_MANIFEST_SHA256 from the installed release, then repeat runtime describe --json.");
|
|
137
|
+
const result = describe(selected, values["require-protocol"] ?? []);
|
|
138
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
139
|
+
process.exitCode = result.status === "ready" ? 0 : 4;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (!selected) {
|
|
143
|
+
await runFoundryPublicCommand(argv, {}, receiveFoundryManagedHost);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (selected.report.status !== "ready")
|
|
147
|
+
fail("runtime_not_ready", "Selected runtime components are missing or invalid. Inspect tiangong-foundry runtime describe --json, then install the selected release with runtime ensure.");
|
|
148
|
+
process.once("SIGINT", interrupt);
|
|
149
|
+
process.once("SIGTERM", interrupt);
|
|
150
|
+
const result = await executeRuntimeLaunch(selected.trusted, {
|
|
151
|
+
cacheDir: selected.cacheDir,
|
|
152
|
+
entry: selected.entry,
|
|
153
|
+
cwd: process.cwd(),
|
|
154
|
+
argv: args,
|
|
155
|
+
env: process.env,
|
|
156
|
+
signal: controller.signal,
|
|
157
|
+
fetchImpl: async () => fail("runtime_installation_required", "A selected component disappeared before launch. Install it explicitly with runtime ensure, then inspect readiness before continuing."),
|
|
158
|
+
});
|
|
159
|
+
process.stdout.write(result.stdout);
|
|
160
|
+
process.stderr.write(result.stderr);
|
|
161
|
+
if (result.error && !result.stderr)
|
|
162
|
+
process.stderr.write("Foundry managed launch failed; inspect the selected runtime before recovery.\n");
|
|
163
|
+
process.exitCode = controller.signal.aborted ? 130 : result.error ? 1 : (result.status ?? 1);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
process.stdout.write(`${JSON.stringify({ schema: "tiangong-foundry.runtime-description.v1", status: "blocked", error: { code: error instanceof FoundryContextError ? error.code : "runtime_selection_failed", message: error instanceof FoundryContextError ? error.message : "Runtime selection failed. Check configuration, manifest integrity and installed components with runtime describe --json." } })}\n`);
|
|
167
|
+
process.exitCode = 4;
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
process.removeListener("SIGINT", interrupt);
|
|
171
|
+
process.removeListener("SIGTERM", interrupt);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type RuntimeHostContext, type RuntimeLaunch, type TrustedRuntimeManifest } from "@tiangong-lca/cli/runtime";
|
|
2
|
+
import { type TrustedFoundryRuntimeAdoptionQualification } from "./foundry-task-runtime-adoption.ts";
|
|
3
|
+
export declare const FOUNDRY_MANAGED_ADOPTION_SCHEMA: "tiangong-foundry.managed-adoption.v1";
|
|
4
|
+
export declare const FOUNDRY_MANAGED_ADOPTION_PATH: "metadata/foundry-adoption.json";
|
|
5
|
+
type Reference = RuntimeLaunch["executable"];
|
|
6
|
+
export interface ManagedFoundryAdoption {
|
|
7
|
+
readonly manifest: TrustedRuntimeManifest;
|
|
8
|
+
readonly qualification?: TrustedFoundryRuntimeAdoptionQualification;
|
|
9
|
+
}
|
|
10
|
+
/** Admit only an independently trusted carrier; task, environment and argv bytes are not inputs. */
|
|
11
|
+
export declare function prepareFoundryManagedAdoption(context: RuntimeHostContext, read: (reference: Reference, limit: number) => Uint8Array, target?: Reference | null): ManagedFoundryAdoption | null;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { RUNTIME_HOST_CONTEXT_PROTOCOL, copyTrustedRuntimeManifestBytes, trustRuntimeManifest, } from "@tiangong-lca/cli/runtime";
|
|
2
|
+
import { createFoundryRuntimeAdoptionQualification, } from "./foundry-task-runtime-adoption.js";
|
|
3
|
+
import { FoundryContextError } from "./foundry-runtime-error.js";
|
|
4
|
+
import { transferHash } from "./foundry-migration-transfer-io.js";
|
|
5
|
+
import { sha256Json } from "./identity-preflight-proof.js";
|
|
6
|
+
export const FOUNDRY_MANAGED_ADOPTION_SCHEMA = "tiangong-foundry.managed-adoption.v1";
|
|
7
|
+
export const FOUNDRY_MANAGED_ADOPTION_PATH = "metadata/foundry-adoption.json";
|
|
8
|
+
const protocolPrefix = "tiangong-foundry.managed-adoption.";
|
|
9
|
+
function fail(message) {
|
|
10
|
+
throw new FoundryContextError("managed_runtime_invalid", message);
|
|
11
|
+
}
|
|
12
|
+
function object(value, keys) {
|
|
13
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
14
|
+
fail("Managed adoption control metadata must be an object.");
|
|
15
|
+
const record = value;
|
|
16
|
+
if (Object.keys(record).length !== keys.length || keys.some((key) => !Object.hasOwn(record, key)))
|
|
17
|
+
fail("Managed adoption control metadata has missing or unsupported fields.");
|
|
18
|
+
return record;
|
|
19
|
+
}
|
|
20
|
+
/** Admit only an independently trusted carrier; task, environment and argv bytes are not inputs. */
|
|
21
|
+
export function prepareFoundryManagedAdoption(context, read, target = null) {
|
|
22
|
+
// The public CLI's brand prevents a serialized carrier from issuing host authority.
|
|
23
|
+
copyTrustedRuntimeManifestBytes(context.manifest);
|
|
24
|
+
const carrier = context.manifest.manifest;
|
|
25
|
+
const controls = carrier.components.filter((component) => component.protocols.some((protocol) => protocol.startsWith(protocolPrefix)) ||
|
|
26
|
+
component.files.some((file) => file.path === FOUNDRY_MANAGED_ADOPTION_PATH));
|
|
27
|
+
if (!controls.length)
|
|
28
|
+
return null;
|
|
29
|
+
if (controls.length !== 1)
|
|
30
|
+
fail("Managed adoption requires exactly one control component.");
|
|
31
|
+
const control = controls[0];
|
|
32
|
+
const protocols = control.protocols.filter((protocol) => protocol.startsWith(protocolPrefix));
|
|
33
|
+
if (protocols.some((protocol) => protocol !== FOUNDRY_MANAGED_ADOPTION_SCHEMA))
|
|
34
|
+
throw new FoundryContextError("managed_runtime_unsupported", "This Foundry package cannot read the selected managed adoption protocol.");
|
|
35
|
+
if (protocols.length !== 1 ||
|
|
36
|
+
control.protocols.length !== 1 ||
|
|
37
|
+
control.platform !== context.host.platform ||
|
|
38
|
+
control.files.some((file) => file.mode !== 0o644))
|
|
39
|
+
fail("Managed adoption control must use one supported protocol and non-executable files.");
|
|
40
|
+
if (target !== null)
|
|
41
|
+
fail("Managed adoption cannot be mixed with a rollback target policy.");
|
|
42
|
+
if (carrier.launches.some((launch) => launch.executable.component === control.id ||
|
|
43
|
+
launch.argv.some((argument) => "component" in argument && argument.component === control.id)))
|
|
44
|
+
fail("Managed adoption control cannot be referenced by a runtime launch.");
|
|
45
|
+
const reference = (value) => {
|
|
46
|
+
const item = object(value, ["component", "path"]);
|
|
47
|
+
if (item.component !== control.id ||
|
|
48
|
+
typeof item.path !== "string" ||
|
|
49
|
+
!control.files.some((file) => file.path === item.path))
|
|
50
|
+
fail("Managed adoption references must name files in the same control component.");
|
|
51
|
+
return { component: control.id, path: item.path };
|
|
52
|
+
};
|
|
53
|
+
const bound = (ref, limit) => {
|
|
54
|
+
const fact = control.files.find((file) => file.path === ref.path);
|
|
55
|
+
if (ref.component !== control.id || !fact || fact.bytes > limit)
|
|
56
|
+
fail("Managed adoption control file is missing or exceeds its inventory bound.");
|
|
57
|
+
const data = Buffer.from(read(ref, limit));
|
|
58
|
+
if (data.length !== fact.bytes || transferHash(data) !== fact.sha256)
|
|
59
|
+
fail("Managed adoption control bytes changed after inventory admission.");
|
|
60
|
+
return { data, sha256: fact.sha256 };
|
|
61
|
+
};
|
|
62
|
+
const metadata = object(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bound({ component: control.id, path: FOUNDRY_MANAGED_ADOPTION_PATH }, 256 * 1024).data)), ["schema", "platform", "successor_manifest", "launches"]);
|
|
63
|
+
if (metadata.schema !== FOUNDRY_MANAGED_ADOPTION_SCHEMA ||
|
|
64
|
+
metadata.platform !== context.host.platform)
|
|
65
|
+
fail("Managed adoption control metadata must match its protocol and current platform.");
|
|
66
|
+
const successorFile = bound(reference(metadata.successor_manifest), 32 * 1024 * 1024);
|
|
67
|
+
const manifest = trustRuntimeManifest(successorFile.data, successorFile.sha256);
|
|
68
|
+
const { components: runtimeComponents, ...runtimeFields } = manifest.manifest;
|
|
69
|
+
const { components: carrierComponents, ...carrierFields } = carrier;
|
|
70
|
+
if (runtimeComponents.some((component) => component.id === control.id) ||
|
|
71
|
+
sha256Json(runtimeFields) !== sha256Json(carrierFields) ||
|
|
72
|
+
sha256Json(runtimeComponents) !==
|
|
73
|
+
sha256Json(carrierComponents.filter((component) => component !== control)))
|
|
74
|
+
fail("Managed adoption carrier must preserve every runtime component and execution binding.");
|
|
75
|
+
if (!Array.isArray(metadata.launches) ||
|
|
76
|
+
!metadata.launches.length ||
|
|
77
|
+
metadata.launches.length > 16)
|
|
78
|
+
fail("Managed adoption requires bounded explicit launch policies.");
|
|
79
|
+
const seen = new Set();
|
|
80
|
+
const policies = metadata.launches.map((value) => {
|
|
81
|
+
const item = object(value, ["id", "qualification"]);
|
|
82
|
+
if (typeof item.id !== "string" ||
|
|
83
|
+
!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/u.test(item.id) ||
|
|
84
|
+
seen.has(item.id) ||
|
|
85
|
+
!carrier.launches.some((launch) => launch.platform === context.host.platform &&
|
|
86
|
+
launch.id === item.id &&
|
|
87
|
+
launch.context_protocol === RUNTIME_HOST_CONTEXT_PROTOCOL))
|
|
88
|
+
fail("Managed adoption policies require unique declared managed launch ids.");
|
|
89
|
+
seen.add(item.id);
|
|
90
|
+
return {
|
|
91
|
+
id: item.id,
|
|
92
|
+
qualification: item.qualification === null ? null : reference(item.qualification),
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
const selected = policies.find((policy) => policy.id === context.entry);
|
|
96
|
+
if (!selected)
|
|
97
|
+
fail("The selected managed launch requires an explicit adoption policy.");
|
|
98
|
+
if (selected.qualification === null)
|
|
99
|
+
return Object.freeze({ manifest });
|
|
100
|
+
const qualificationFile = bound(selected.qualification, 8 * 1024 * 1024);
|
|
101
|
+
const qualification = createFoundryRuntimeAdoptionQualification({
|
|
102
|
+
bytes: qualificationFile.data,
|
|
103
|
+
expectedSha256: qualificationFile.sha256,
|
|
104
|
+
});
|
|
105
|
+
if (qualification.value.successor_manifest_sha256 !== manifest.sha256)
|
|
106
|
+
fail("Managed adoption qualification must bind the exact successor runtime manifest.");
|
|
107
|
+
return Object.freeze({ manifest, qualification });
|
|
108
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { RuntimeLaunch } from "@tiangong-lca/cli/runtime";
|
|
2
|
+
import type { FoundryAuthentication } from "./foundry-authentication-environment.ts";
|
|
3
|
+
/** The verified carrier and CLI launch policy select this existing credential channel. */
|
|
4
|
+
export declare function createManagedFoundryAuthentication(environment: RuntimeLaunch["environment"], carrierAdmitted: boolean, source: NodeJS.ProcessEnv): FoundryAuthentication | undefined;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { FoundryContextError } from "./foundry-runtime-error.js";
|
|
2
|
+
/** The verified carrier and CLI launch policy select this existing credential channel. */
|
|
3
|
+
export function createManagedFoundryAuthentication(environment, carrierAdmitted, source) {
|
|
4
|
+
if (!carrierAdmitted || environment !== "cli-auth")
|
|
5
|
+
return undefined;
|
|
6
|
+
const mode = source.TIANGONG_LCA_AUTH_MODE || "oauth";
|
|
7
|
+
if (mode === "access_token" || mode === "access-token") {
|
|
8
|
+
const accessToken = source.TIANGONG_LCA_ACCESS_TOKEN;
|
|
9
|
+
const apiBaseUrl = source.TIANGONG_LCA_API_BASE_URL;
|
|
10
|
+
const publishableKey = source.TIANGONG_LCA_SUPABASE_PUBLISHABLE_KEY;
|
|
11
|
+
if (!accessToken || !apiBaseUrl || !publishableKey)
|
|
12
|
+
throw new FoundryContextError("managed_authentication_invalid", "Managed headless authentication requires the existing CLI explicit public target and process-only token.");
|
|
13
|
+
return Object.freeze({ mode: "headless", accessToken, apiBaseUrl, publishableKey });
|
|
14
|
+
}
|
|
15
|
+
if (mode !== "oauth")
|
|
16
|
+
throw new FoundryContextError("managed_authentication_invalid", "The selected managed authentication mode is unsupported.");
|
|
17
|
+
const fields = [
|
|
18
|
+
["apiBaseUrl", "TIANGONG_LCA_API_BASE_URL"],
|
|
19
|
+
["publishableKey", "TIANGONG_LCA_SUPABASE_PUBLISHABLE_KEY"],
|
|
20
|
+
["oauthClientId", "TIANGONG_LCA_OAUTH_CLIENT_ID"],
|
|
21
|
+
["oauthRedirectUri", "TIANGONG_LCA_OAUTH_REDIRECT_URI"],
|
|
22
|
+
];
|
|
23
|
+
const configuration = {};
|
|
24
|
+
for (const [key, variable] of fields)
|
|
25
|
+
if (source[variable])
|
|
26
|
+
configuration[key] = source[variable];
|
|
27
|
+
return Object.freeze({ mode: "oauth", configuration: Object.freeze(configuration) });
|
|
28
|
+
}
|
|
@@ -2,6 +2,8 @@ import fs from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { RUNTIME_HOST_CONTEXT_PROTOCOL, assertCliRuntimeMatches, inspectRuntimeComponents, receiveRuntimeHostContext, trustRuntimeManifest, } from "@tiangong-lca/cli/runtime";
|
|
4
4
|
import { createManagedFoundryActionProjector } from "./foundry-managed-actions.js";
|
|
5
|
+
import { prepareFoundryManagedAdoption } from "./foundry-managed-adoption.js";
|
|
6
|
+
import { createManagedFoundryAuthentication } from "./foundry-managed-authentication.js";
|
|
5
7
|
import { FoundryContextError } from "./foundry-runtime-error.js";
|
|
6
8
|
import { describeFoundryRuntime } from "./foundry-runtime-paths.js";
|
|
7
9
|
import { assertFoundryPackage } from "./foundry-package-contract.js";
|
|
@@ -129,20 +131,30 @@ function prepare(context) {
|
|
|
129
131
|
const policy = policies.find((item) => item.id === context.entry);
|
|
130
132
|
if (!policy)
|
|
131
133
|
fail("The selected launch has no Foundry access policy.");
|
|
134
|
+
const adoption = prepareFoundryManagedAdoption(context, bytes, policy.target);
|
|
135
|
+
if (adoption &&
|
|
136
|
+
inspectRuntimeComponents(adoption.manifest, {
|
|
137
|
+
cacheDir: context.cacheDir,
|
|
138
|
+
host: context.host,
|
|
139
|
+
}).status !== "ready")
|
|
140
|
+
fail("The independently bound successor runtime changed before Foundry admission.");
|
|
141
|
+
const workspaceManifest = adoption?.manifest ?? context.manifest;
|
|
132
142
|
const target = policy.target
|
|
133
143
|
? trustRuntimeManifest(bytes(policy.target, 32 * 1024 * 1024), file(policy.target).fact.sha256)
|
|
134
|
-
:
|
|
144
|
+
: workspaceManifest;
|
|
135
145
|
if (target.manifest.product.id !== "tiangong-foundry")
|
|
136
146
|
fail("Runtime selection targets must be Foundry manifests.");
|
|
137
147
|
return Object.freeze({
|
|
138
148
|
projectNextActions: createManagedFoundryActionProjector(context, observedCli, runtime.entryPath),
|
|
139
|
-
workspaceAccess: Object.freeze({ manifest:
|
|
149
|
+
workspaceAccess: Object.freeze({ manifest: workspaceManifest, access: policy.access }),
|
|
140
150
|
runtimeSelection: Object.freeze({
|
|
141
151
|
cliExpectation: Object.freeze(cli),
|
|
142
152
|
tidasExpectation,
|
|
143
153
|
tidasExecutable: tidasFile.absolute,
|
|
144
154
|
}),
|
|
145
155
|
runtimeTarget: target,
|
|
156
|
+
runtimeAdoptionQualification: adoption?.qualification,
|
|
157
|
+
authentication: createManagedFoundryAuthentication(selected.environment, adoption !== null, process.env),
|
|
146
158
|
runtimeManager: Object.freeze({ cacheDir: context.cacheDir, host: context.host }),
|
|
147
159
|
cacheBase: path.join(context.cacheDir, "foundry-workspaces"),
|
|
148
160
|
});
|
|
@@ -6,6 +6,8 @@ export declare function transferPath(root: string, relative: string): string;
|
|
|
6
6
|
export declare function transferRead(file: string, limit?: number): Buffer;
|
|
7
7
|
export declare function transferWriteOnce(root: string, relative: string, bytes: Buffer, scratchRelative?: string): void;
|
|
8
8
|
export declare function transferFileFact(file: string): FoundryInputFact;
|
|
9
|
+
/** Verify retained inventory content without constructing an unused canonical locator. */
|
|
10
|
+
export declare function transferFileContentFact(file: string): Pick<FoundryInputFact, "bytes" | "sha256">;
|
|
9
11
|
export declare function transferCopy(root: string, relative: string, source: FoundryInputFact, scratch: string): void;
|
|
10
12
|
export declare function transferTree(root: string): {
|
|
11
13
|
files: string[];
|
|
@@ -99,7 +99,20 @@ export function transferWriteOnce(root, relative, bytes, scratchRelative) {
|
|
|
99
99
|
fs.unlinkSync(temp);
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
-
|
|
102
|
+
function transferCanonicalPath(file) {
|
|
103
|
+
// Canonical retained paths can avoid the interpreted absolute-ancestor walk.
|
|
104
|
+
// Other spellings and native failures retain the existing resolver behavior.
|
|
105
|
+
try {
|
|
106
|
+
const resolved = fs.realpathSync.native(file);
|
|
107
|
+
if (resolved === path.resolve(file))
|
|
108
|
+
return resolved;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// The generic resolver also supports filesystems without native resolution.
|
|
112
|
+
}
|
|
113
|
+
return fs.realpathSync(file);
|
|
114
|
+
}
|
|
115
|
+
function transferVerifiedFile(file, capture) {
|
|
103
116
|
const before = fs.lstatSync(file, { bigint: true });
|
|
104
117
|
if (!before.isFile() || before.isSymbolicLink() || before.size > 64n * 1024n * 1024n)
|
|
105
118
|
transferFail("migration_file_invalid", "Transfer file must be bounded and regular.");
|
|
@@ -108,7 +121,7 @@ export function transferFileFact(file) {
|
|
|
108
121
|
const opened = fs.fstatSync(fd, { bigint: true });
|
|
109
122
|
if (opened.ino !== before.ino || opened.dev !== before.dev)
|
|
110
123
|
transferFail("migration_source_changed", "Transfer file identity changed.");
|
|
111
|
-
const hash = createHash("sha256"), buffer = Buffer.alloc(1024 * 1024);
|
|
124
|
+
const hash = createHash("sha256"), buffer = Buffer.alloc(Math.max(1, Math.min(1024 * 1024, Number(before.size))));
|
|
112
125
|
let bytes = 0;
|
|
113
126
|
while (true) {
|
|
114
127
|
const count = fs.readSync(fd, buffer, 0, buffer.length, null);
|
|
@@ -129,12 +142,23 @@ export function transferFileFact(file) {
|
|
|
129
142
|
linked.ino !== before.ino ||
|
|
130
143
|
linked.dev !== before.dev)
|
|
131
144
|
transferFail("migration_source_changed", "Transfer file changed during hashing.");
|
|
132
|
-
return
|
|
145
|
+
return capture(bytes, hash);
|
|
133
146
|
}
|
|
134
147
|
finally {
|
|
135
148
|
fs.closeSync(fd);
|
|
136
149
|
}
|
|
137
150
|
}
|
|
151
|
+
export function transferFileFact(file) {
|
|
152
|
+
return transferVerifiedFile(file, (bytes, hash) => ({
|
|
153
|
+
path: transferCanonicalPath(file),
|
|
154
|
+
bytes,
|
|
155
|
+
sha256: hash.digest("hex"),
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
/** Verify retained inventory content without constructing an unused canonical locator. */
|
|
159
|
+
export function transferFileContentFact(file) {
|
|
160
|
+
return transferVerifiedFile(file, (bytes, hash) => ({ bytes, sha256: hash.digest("hex") }));
|
|
161
|
+
}
|
|
138
162
|
export function transferCopy(root, relative, source, scratch) {
|
|
139
163
|
const file = transferPath(root, relative);
|
|
140
164
|
if (fs.existsSync(file)) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export declare const FOUNDRY_OPERATION_RESULT_SCHEMA: "tiangong-foundry.operation-result.v1";
|
|
2
2
|
export declare const foundryOperationStatuses: readonly ["ready", "running", "needs_auth", "needs_input", "blocked", "completed", "failed"];
|
|
3
3
|
export declare const foundryOperationPermissionStates: readonly ["not_required", "required", "granted", "invalid"];
|
|
4
|
-
export declare const foundryPublicOperations: readonly ["workspace.init", "doctor", "task.start", "task.status", "task.resume", "workspace.migrate"];
|
|
4
|
+
export declare const foundryPublicOperations: readonly ["workspace.init", "doctor", "task.start", "task.status", "task.resume", "task.adopt-runtime", "workspace.migrate"];
|
|
5
5
|
export type FoundryOperationStatus = (typeof foundryOperationStatuses)[number];
|
|
6
6
|
export type FoundryOperationPermissionState = (typeof foundryOperationPermissionStates)[number];
|
|
7
7
|
export type FoundryPublicOperation = "unknown" | (typeof foundryPublicOperations)[number];
|