feat: complete catalog-driven preprocessing
Publish documentation / publish (push) Successful in 2m12s
Publish documentation / publish (push) Successful in 2m12s
This commit is contained in:
@@ -90,6 +90,7 @@ export function resolveBinding(
|
||||
): ResolvedBinding {
|
||||
requireSupportedDescriptor(workspace);
|
||||
const descriptor = validateWorkspaceDescriptor(workspace);
|
||||
if (!descriptor.dwh) throw new Error("workspace database is not bound in the descriptor");
|
||||
const contract = buildInstallationContract(descriptor);
|
||||
const variables = contract.variables.filter((variable) => variable.role === role);
|
||||
const transportVariable = variables.find((variable) => variable.suffix === "TRANSPORT");
|
||||
@@ -165,7 +166,9 @@ export function resolveRuntimeBindings(
|
||||
const descriptor = validateWorkspaceDescriptor(workspace);
|
||||
|
||||
return {
|
||||
dwh: resolveBinding(descriptor, "DWH", env, secretRoots),
|
||||
dwh: descriptor.dwh
|
||||
? resolveBinding(descriptor, "DWH", env, secretRoots)
|
||||
: { transport: "postgres_direct", values: {}, missing: [] },
|
||||
evidence: resolveEvidenceBinding(descriptor, env, secretRoots),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -155,7 +155,9 @@ export function buildInstallationContract(workspace: WorkspaceDescriptor): Insta
|
||||
workspaceId: descriptor.workspace.id,
|
||||
namespace,
|
||||
variables: [
|
||||
...connectorVariables(namespace, descriptor.dwh.supported_transports),
|
||||
...(descriptor.dwh
|
||||
? connectorVariables(namespace, descriptor.dwh.supported_transports)
|
||||
: []),
|
||||
...evidenceVariables(namespace, descriptor),
|
||||
],
|
||||
};
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
import type { WorkspaceErrorCode } from "./types.js";
|
||||
import type { SemanticRuntimeConfig } from "./runtime-renderer.js";
|
||||
import type { AuthDiagnostics } from "../auth/diagnostics.js";
|
||||
import { workspaceVectorCollections } from "./vector-collections.js";
|
||||
|
||||
export interface Diagnostic {
|
||||
level: "error" | "warning" | "info";
|
||||
@@ -447,6 +448,9 @@ async function diagnoseValidatedWorkspace(
|
||||
|
||||
let activatable = true;
|
||||
if (!skipDwh) {
|
||||
if (!descriptor.dwh) {
|
||||
return { activatable: false, diagnostics: [diagnosticError("binding_missing", "dwh")] };
|
||||
}
|
||||
const dwhTimeout = boundedTimeout(descriptor.dwh.timeout_ms, timeoutMs);
|
||||
const dwhValues = bindings.dwh.values;
|
||||
const dwhField = (suffix: string) => bindingName(descriptor, suffix);
|
||||
@@ -514,20 +518,18 @@ async function diagnoseValidatedWorkspace(
|
||||
}
|
||||
|
||||
try {
|
||||
const vector = await withTimeout(timeoutMs, (signal) => adapters.inspectQdrant({
|
||||
baseUrl: semanticRuntime.internalQdrantUrl,
|
||||
collection: descriptor.workspace.id,
|
||||
timeoutMs,
|
||||
signal,
|
||||
}));
|
||||
const expected = {
|
||||
collection: descriptor.workspace.id,
|
||||
dimensions: semanticRuntime.internalEmbeddingDimensions,
|
||||
distance: "cosine",
|
||||
};
|
||||
if (vector.collection !== expected.collection
|
||||
|| vector.dimensions !== expected.dimensions
|
||||
|| vector.distance !== expected.distance) {
|
||||
const expectedCollections = Object.values(workspaceVectorCollections(descriptor.workspace.id));
|
||||
const vectors = await Promise.all(expectedCollections.map(async (collection) =>
|
||||
await withTimeout(timeoutMs, (signal) => adapters.inspectQdrant({
|
||||
baseUrl: semanticRuntime.internalQdrantUrl,
|
||||
collection,
|
||||
timeoutMs,
|
||||
signal,
|
||||
}))));
|
||||
if (vectors.some((vector, index) =>
|
||||
vector.collection !== expectedCollections[index]
|
||||
|| vector.dimensions !== semanticRuntime.internalEmbeddingDimensions
|
||||
|| vector.distance !== "cosine")) {
|
||||
diagnostics.push(diagnosticError("semantic_index_incompatible"));
|
||||
activatable = false;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createHash } from "node:crypto";
|
||||
import { normalize } from "node:path";
|
||||
|
||||
export interface CanonicalEffectiveConfig {
|
||||
schemaVersion: 2;
|
||||
schemaVersion: 3;
|
||||
dwh: CanonicalDwhConfig;
|
||||
vector: CanonicalVectorConfig;
|
||||
embedding: CanonicalEmbeddingConfig;
|
||||
@@ -21,7 +21,10 @@ export interface CanonicalDwhConfig {
|
||||
}
|
||||
|
||||
export interface CanonicalVectorConfig {
|
||||
collection: string;
|
||||
collections: {
|
||||
reference: string;
|
||||
memory: string;
|
||||
};
|
||||
dimensions: number;
|
||||
distance: string;
|
||||
}
|
||||
@@ -120,7 +123,10 @@ function buildVectorConfig(rendered: Record<string, unknown>): CanonicalVectorCo
|
||||
if (!vector) {
|
||||
throw new TypeError("effective config is missing vector resources");
|
||||
}
|
||||
const collection = requireString(vector, "collection");
|
||||
const collections = asRecord(vector.collections);
|
||||
if (!collections) {
|
||||
throw new TypeError("effective config is missing vector collections");
|
||||
}
|
||||
const semanticIndex = asRecord(rendered.semantic_index);
|
||||
const vectorStore = semanticIndex ? asRecord(semanticIndex.vector_store) : undefined;
|
||||
const dimensions = vectorStore
|
||||
@@ -129,7 +135,14 @@ function buildVectorConfig(rendered: Record<string, unknown>): CanonicalVectorCo
|
||||
const distance = vectorStore
|
||||
? requireString(vectorStore, "distance")
|
||||
: (optionalString(vector, "distance") ?? "cosine");
|
||||
return { collection, dimensions, distance };
|
||||
return {
|
||||
collections: {
|
||||
reference: requireString(collections, "reference"),
|
||||
memory: requireString(collections, "memory"),
|
||||
},
|
||||
dimensions,
|
||||
distance,
|
||||
};
|
||||
}
|
||||
|
||||
function buildEmbeddingConfig(rendered: Record<string, unknown>): CanonicalEmbeddingConfig {
|
||||
@@ -169,7 +182,7 @@ export function buildCanonicalEffectiveConfig(renderedConfig: unknown): Canonica
|
||||
throw new TypeError("effective config requires a rendered configuration object");
|
||||
}
|
||||
return {
|
||||
schemaVersion: 2,
|
||||
schemaVersion: 3,
|
||||
dwh: buildDwhConfig(rendered),
|
||||
vector: buildVectorConfig(rendered),
|
||||
embedding: buildEmbeddingConfig(rendered),
|
||||
@@ -220,3 +233,20 @@ export function configFingerprint(renderedConfig: unknown): string {
|
||||
export function inputFingerprint(workspaceId: string, renderedConfig: unknown): string {
|
||||
return sha256(effectiveConfigIdentity(workspaceId, renderedConfig));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fingerprint every non-Catalog input consumed by complete preprocessing. The immutable Git
|
||||
* revision covers the workspace descriptor and its revision-pinned Evidence tree; the effective
|
||||
* configuration identity covers the Catalog-derived DWH binding and semantic runtime contract.
|
||||
*/
|
||||
export function preprocessingInputFingerprint(
|
||||
workspaceId: string,
|
||||
workspaceRevision: string,
|
||||
renderedConfig: unknown,
|
||||
): string {
|
||||
return sha256(JSON.stringify({
|
||||
workspaceId,
|
||||
workspaceRevision,
|
||||
effectiveConfigIdentity: effectiveConfigIdentity(workspaceId, renderedConfig),
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -171,6 +171,14 @@ export async function continueEvidencePreprocessing(
|
||||
const policy = evidencePolicy(request.evidence, request.httpPrivateHostAllowlist);
|
||||
if (policy) return { ...policy, ...jobResult(request.job) };
|
||||
if (!request.job.completedStages.includes("evidence")) {
|
||||
const preflight = await deps.evidencePreflight();
|
||||
if (!preflight.ok) {
|
||||
return {
|
||||
status: "failed",
|
||||
code: preflight.code,
|
||||
...jobResult(request.job),
|
||||
};
|
||||
}
|
||||
return await runEvidenceStage(request, deps);
|
||||
}
|
||||
return { status: "unchanged", code: "ok", ...jobResult(request.job) };
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { readdirSync, readFileSync, rmSync, writeFileSync, mkdirSync } from "node:fs";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { renameSync, rmSync, writeFileSync, mkdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
continueEvidencePreprocessing,
|
||||
preprocessEvidence as runEvidencePreprocessing,
|
||||
type EvidencePreprocessingDependencies,
|
||||
type EvidencePreprocessingOutcome,
|
||||
} from "./evidence/preprocessing.js";
|
||||
import type { WorkspaceDescriptor } from "./schema.js";
|
||||
import {
|
||||
PreprocessingStateStore,
|
||||
type FkReviewRecord,
|
||||
type PreprocessingJobState,
|
||||
type SessionInventoryRow,
|
||||
} from "./preprocessing-state.js";
|
||||
import type { DeterministicRuntimeConfigLease } from "./runtime-config-lease.js";
|
||||
import { readAnnotationsSync } from "./annotations-sync.js";
|
||||
import { reconcileCollection } from "./qdrant-collection.js";
|
||||
import { buildCatalogMetadataSnapshot } from "../catalog/metadata-snapshot.js";
|
||||
import type { CatalogRepository } from "../catalog/types.js";
|
||||
|
||||
export interface WorkspaceOperationResult {
|
||||
schemaVersion: 1;
|
||||
@@ -27,7 +24,7 @@ export interface WorkspaceOperationResult {
|
||||
| "preprocessing_resume_mismatch" | "manual_review_required"
|
||||
| "evidence_materialization_required" | "effective_config_mismatch"
|
||||
| "semantic_index_incompatible" | "annotation_invalid"
|
||||
| "egress_policy_refused";
|
||||
| "egress_policy_refused" | "catalog_not_ready" | "preprocessing_clear_failed";
|
||||
workspaceId: string;
|
||||
workspaceRevision: string;
|
||||
descriptorBlob: string;
|
||||
@@ -69,7 +66,6 @@ export interface WorkspacePreprocessingServiceDeps {
|
||||
dataRoot: string;
|
||||
acquireActiveRuntime(workspaceId: string): Promise<ActiveRuntime>;
|
||||
runChild(request: ChildProcessRequest): Promise<ChildProcessResult>;
|
||||
listSessions(workspaceId: string): Promise<readonly SessionInventoryRow[]>;
|
||||
semanticPreflight(workspace: WorkspaceDescriptor): Promise<
|
||||
{ ok: true } | { ok: false; code: "workspace_not_activatable" | "semantic_index_incompatible" }
|
||||
>;
|
||||
@@ -77,7 +73,7 @@ export interface WorkspacePreprocessingServiceDeps {
|
||||
{ ok: true } | { ok: false; code: "workspace_not_activatable" | "semantic_index_incompatible" }
|
||||
>;
|
||||
httpPrivateHostAllowlist?: readonly string[];
|
||||
embeddingDimensions?: number;
|
||||
catalogRepository?: CatalogRepository;
|
||||
}
|
||||
|
||||
interface RunScope {
|
||||
@@ -86,10 +82,6 @@ interface RunScope {
|
||||
job: PreprocessingJobState;
|
||||
}
|
||||
|
||||
function digest(value: string | Buffer): string {
|
||||
return `sha256:${createHash("sha256").update(value).digest("hex")}`;
|
||||
}
|
||||
|
||||
function baseResult(
|
||||
runtime: ActiveRuntime,
|
||||
operation: string,
|
||||
@@ -116,41 +108,6 @@ function baseResult(
|
||||
export class WorkspacePreprocessingService {
|
||||
constructor(private readonly deps: WorkspacePreprocessingServiceDeps) {}
|
||||
|
||||
|
||||
async vectorInspect(options: { workspaceId: string }): Promise<WorkspaceOperationResult> {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
const collection = runtime.workspace.workspace.id;
|
||||
const res = await fetch(`${runtime.configLease.semanticQdrantUrl}/collections/${encodeURIComponent(collection)}`, { method: "GET" });
|
||||
if (!res.ok) return baseResult(runtime, "vector inspect", "failed", "semantic_index_incompatible", { warnings: ["collection unavailable"] });
|
||||
const body = await res.json() as any;
|
||||
const info = body?.result;
|
||||
const vectors = info?.config?.params?.vectors;
|
||||
return baseResult(runtime, "vector inspect", "succeeded", "ok", {
|
||||
counts: { dimensions: vectors?.size ?? 0 },
|
||||
warnings: [`collection=${collection} distance=${vectors?.distance ?? "unknown"}`],
|
||||
});
|
||||
}
|
||||
|
||||
async vectorRebuild(options: { workspaceId: string; collection?: string; confirm?: string; destroy?: boolean }): Promise<WorkspaceOperationResult> {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
const collection = runtime.workspace.workspace.id;
|
||||
if (options.collection !== collection || options.confirm !== collection || options.destroy !== true) {
|
||||
return baseResult(runtime, "vector rebuild", "failed", "semantic_index_incompatible", { warnings: ["rebuild requires exact confirmation and --destroy"] });
|
||||
}
|
||||
const q = `${runtime.configLease.semanticQdrantUrl}/collections/${encodeURIComponent(collection)}`;
|
||||
const del = await fetch(q, { method: "DELETE" });
|
||||
if (!del.ok && del.status !== 404) return baseResult(runtime, "vector rebuild", "failed", "semantic_index_incompatible", { warnings: ["collection delete failed"] });
|
||||
// Recreate the complete contract (dimensions + distance + the 8 required keyword indexes).
|
||||
const recreated = await reconcileCollection({
|
||||
baseUrl: runtime.configLease.semanticQdrantUrl,
|
||||
collection,
|
||||
dimensions: this.deps.embeddingDimensions ?? 1024,
|
||||
distance: "cosine",
|
||||
mode: "self_heal",
|
||||
});
|
||||
if (!recreated.ok) return baseResult(runtime, "vector rebuild", "failed", "semantic_index_incompatible", { warnings: ["collection recreate failed"] });
|
||||
return baseResult(runtime, "vector rebuild", "succeeded", "ok", { warnings: [`recreated collection=${collection}`] });
|
||||
}
|
||||
async inspect(options: { workspaceId: string }): Promise<WorkspaceOperationResult> {
|
||||
try {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
@@ -175,227 +132,151 @@ export class WorkspacePreprocessingService {
|
||||
}
|
||||
}
|
||||
|
||||
async preprocessDwh(options: { workspaceId: string; resumeRunId?: string }): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId, "preprocess dwh", options.resumeRunId);
|
||||
if (scope.job.completedStages.includes("dwh")) {
|
||||
return baseResult(scope.runtime, "preprocess dwh", "unchanged", "ok", {
|
||||
runId: scope.job.runId,
|
||||
childRuns: scope.job.childRuns,
|
||||
completedStages: [...scope.job.completedStages],
|
||||
});
|
||||
async run(options: { workspaceId: string }): Promise<WorkspaceOperationResult> {
|
||||
if (!this.deps.catalogRepository) {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
return baseResult(runtime, "preprocess run", "failed", "catalog_not_ready");
|
||||
}
|
||||
const payload = await this.runJsonStage(scope.runtime, [
|
||||
"preprocess", "dwh", "--steps", "introspect,lsh",
|
||||
...(scope.job.childRuns.dwh ? ["--resume", scope.job.childRuns.dwh] : []),
|
||||
"--json", "-c", "/dev/fd/3",
|
||||
]);
|
||||
const childRun = this.requireRunId(payload.run_id);
|
||||
scope.job.childRuns.dwh = childRun;
|
||||
if (!scope.job.completedStages.includes("dwh")) scope.job.completedStages.push("dwh");
|
||||
this.state(scope.runtime.workspaceId).writeJob(scope.job);
|
||||
return baseResult(scope.runtime, "preprocess dwh", "succeeded", "ok", {
|
||||
runId: scope.job.runId,
|
||||
childRuns: { ...scope.job.childRuns },
|
||||
completedStages: [...scope.job.completedStages],
|
||||
});
|
||||
return await this.runFromCatalog(options);
|
||||
}
|
||||
|
||||
async suggestFks(options: {
|
||||
workspaceId: string;
|
||||
fromSql?: ReadonlyArray<{ name: string; sql: string }>;
|
||||
assume?: readonly string[];
|
||||
resumeRunId?: string;
|
||||
}): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId, "schema suggest-fks", options.resumeRunId);
|
||||
return await this.runSuggestStage(scope, options.fromSql ?? [], options.assume ?? []);
|
||||
}
|
||||
|
||||
async checkSchema(options: {
|
||||
workspaceId: string;
|
||||
annotationsYaml?: string;
|
||||
reviewedCandidatesDigest?: string;
|
||||
}): Promise<WorkspaceOperationResult> {
|
||||
async clear(options: { workspaceId: string }): Promise<WorkspaceOperationResult> {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
const state = this.state(runtime.workspaceId);
|
||||
if ((options.annotationsYaml === undefined) !== (options.reviewedCandidatesDigest === undefined)) {
|
||||
return baseResult(runtime, "schema check", "failed", "annotation_invalid");
|
||||
const repository = this.deps.catalogRepository;
|
||||
if (!repository) return baseResult(runtime, "preprocess clear", "failed", "catalog_not_ready");
|
||||
const cleared = await repository.clearPreprocessing(runtime.workspaceId);
|
||||
if (cleared.kind !== "cleared") {
|
||||
return baseResult(
|
||||
runtime,
|
||||
"preprocess clear",
|
||||
"failed",
|
||||
cleared.kind === "already_running" ? "preprocessing_conflict" : "catalog_not_ready",
|
||||
);
|
||||
}
|
||||
if (options.reviewedCandidatesDigest === undefined) {
|
||||
const payload = await this.runJsonStage(runtime, ["schema", "check", "--json", "-c", "/dev/fd/3"]);
|
||||
return baseResult(runtime, "schema check", Number(payload.orphan_count ?? 0) === 0 ? "succeeded" : "failed", Number(payload.orphan_count ?? 0) === 0 ? "ok" : "annotation_invalid");
|
||||
}
|
||||
const reviewedCandidatesDigest = options.reviewedCandidatesDigest;
|
||||
const runId = this.findRunIdByCandidateDigest(state, reviewedCandidatesDigest);
|
||||
if (!runId) return baseResult(runtime, "schema check", "failed", "annotation_invalid");
|
||||
const request = await this.withStagedInputs(runtime.workspaceId, [
|
||||
{ flag: "--annotations", name: "annotations.yaml", contents: options.annotationsYaml! },
|
||||
], async (argv) => await this.runJsonStage(runtime, [
|
||||
"schema", "check", ...argv,
|
||||
"--reviewed-candidates", reviewedCandidatesDigest,
|
||||
"--json", "-c", "/dev/fd/3",
|
||||
]));
|
||||
if (request.reviewed_candidates_digest !== reviewedCandidatesDigest || typeof request.annotations_digest !== "string") {
|
||||
return baseResult(runtime, "schema check", "failed", "annotation_invalid", { runId });
|
||||
}
|
||||
// P5 supersedes the host-file FK review: schema check is read-only validation and never
|
||||
// records a review. Only `schema accept` records a human review for the curated Git blob.
|
||||
return baseResult(runtime, "schema check", "succeeded", "ok", { runId });
|
||||
}
|
||||
|
||||
async acceptSchema(options: { workspaceId: string; runId: string; yes?: boolean }): Promise<WorkspaceOperationResult> {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
const state = this.state(runtime.workspaceId);
|
||||
if (options.yes !== true) {
|
||||
return baseResult(runtime, "schema accept", "failed", "annotation_invalid", {
|
||||
runId: options.runId,
|
||||
warnings: ["accept requires --yes"],
|
||||
});
|
||||
}
|
||||
if (!/^[0-9a-f]{32}$/.test(options.runId)) {
|
||||
return baseResult(runtime, "schema accept", "failed", "annotation_invalid");
|
||||
}
|
||||
const candidate = state.readFkCandidates(options.runId);
|
||||
if (candidate === undefined) {
|
||||
return baseResult(runtime, "schema accept", "failed", "annotation_invalid", {
|
||||
runId: options.runId,
|
||||
warnings: ["candidate run is unavailable"],
|
||||
});
|
||||
}
|
||||
const synced = readAnnotationsSync(this.deps.dataRoot, runtime.workspaceId, runtime.workspaceRevision);
|
||||
if (synced === undefined || synced.contents.toString("utf8").trim() === "") {
|
||||
return baseResult(runtime, "schema accept", "failed", "annotation_invalid", {
|
||||
runId: options.runId,
|
||||
warnings: ["curated annotations are not synchronized"],
|
||||
});
|
||||
}
|
||||
// The harness parser validates the curated blob against the physical schema; the recorded
|
||||
// candidate digest must round-trip and the blob digest must match the synced destination.
|
||||
const payload = await this.runJsonStage(runtime, [
|
||||
"schema", "check", "--reviewed-candidates", candidate.digest, "--json", "-c", "/dev/fd/3",
|
||||
]);
|
||||
if (payload.annotations_digest !== synced.contentDigest
|
||||
|| payload.reviewed_candidates_digest !== candidate.digest
|
||||
|| Number(payload.orphan_count ?? 0) !== 0) {
|
||||
return baseResult(runtime, "schema accept", "failed", "annotation_invalid", { runId: options.runId });
|
||||
}
|
||||
const review = state.writeFkReview(options.runId, {
|
||||
reviewedCandidatesDigest: candidate.digest,
|
||||
annotationsDigest: synced.contentDigest,
|
||||
workspaceRevision: runtime.workspaceRevision,
|
||||
blobId: synced.blobId,
|
||||
const result = await this.deps.runChild({
|
||||
argv: ["preprocess", "clear", "--json", "-c", "/dev/fd/3"],
|
||||
configPath: runtime.configLease.path,
|
||||
});
|
||||
const job = state.readJob(options.runId);
|
||||
job.reviewDigest = review.digest;
|
||||
if (!job.completedStages.includes("fk_review")) job.completedStages.push("fk_review");
|
||||
state.writeJob(job);
|
||||
return baseResult(runtime, "schema accept", "succeeded", "ok", {
|
||||
runId: options.runId,
|
||||
completedStages: [...job.completedStages],
|
||||
artifactIdentities: [
|
||||
{ kind: "fk_review", digest: review.digest },
|
||||
{ kind: "annotations", digest: synced.contentDigest },
|
||||
],
|
||||
if (result.exitCode !== 0) {
|
||||
return baseResult(runtime, "preprocess clear", "failed", "preprocessing_clear_failed");
|
||||
}
|
||||
const payload = JSON.parse(result.stdout) as Record<string, unknown>;
|
||||
return baseResult(runtime, "preprocess clear", "succeeded", "ok", {
|
||||
counts: this.numberRecord(payload.counts),
|
||||
});
|
||||
}
|
||||
|
||||
async indexSchema(options: { workspaceId: string; resumeRunId?: string }): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId, "index-schema", options.resumeRunId);
|
||||
const semantic = await this.deps.semanticPreflight(scope.runtime.workspace);
|
||||
if (!semantic.ok) return baseResult(scope.runtime, "index-schema", "failed", semantic.code, { runId: scope.job.runId });
|
||||
if (scope.job.completedStages.includes("schema_index")) {
|
||||
return baseResult(scope.runtime, "index-schema", "unchanged", "ok", {
|
||||
runId: scope.job.runId,
|
||||
completedStages: [...scope.job.completedStages],
|
||||
});
|
||||
private async runFromCatalog(options: { workspaceId: string }): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId);
|
||||
const repository = this.deps.catalogRepository!;
|
||||
const fingerprint = scope.runtime.configLease.inputFingerprint;
|
||||
const started = await repository.beginPreprocessing(scope.runtime.workspaceId, fingerprint);
|
||||
if (started.kind !== "started") {
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
return baseResult(
|
||||
scope.runtime,
|
||||
"preprocess run",
|
||||
"failed",
|
||||
started.kind === "already_running" ? "preprocessing_conflict" : "catalog_not_ready",
|
||||
{ warnings: [`catalog=${started.kind}`] },
|
||||
);
|
||||
}
|
||||
const payload = await this.runJsonStage(scope.runtime, ["vector", "index-schema", "--json", "-c", "/dev/fd/3"]);
|
||||
const counts = this.numberRecord(payload.counts);
|
||||
scope.job.completedStages.push("schema_index");
|
||||
this.state(scope.runtime.workspaceId).writeJob(scope.job);
|
||||
return baseResult(scope.runtime, "index-schema", "succeeded", "ok", {
|
||||
runId: scope.job.runId,
|
||||
completedStages: [...scope.job.completedStages],
|
||||
counts,
|
||||
});
|
||||
}
|
||||
|
||||
async preprocessEvidence(options: { workspaceId: string; dryRun?: boolean; resumeRunId?: string }): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId, "preprocess evidence", options.resumeRunId);
|
||||
const outcome = await runEvidencePreprocessing(
|
||||
{
|
||||
evidence: scope.runtime.workspace.evidence,
|
||||
job: scope.job,
|
||||
dryRun: options.dryRun,
|
||||
httpPrivateHostAllowlist: this.deps.httpPrivateHostAllowlist,
|
||||
},
|
||||
this.evidenceDependencies(scope),
|
||||
);
|
||||
return this.evidenceResult(scope, "preprocess evidence", outcome);
|
||||
}
|
||||
|
||||
async run(options: { workspaceId: string; resumeRunId?: string }): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId, "preprocess run", options.resumeRunId);
|
||||
if (!scope.job.completedStages.includes("dwh")) {
|
||||
const payload = await this.runJsonStage(scope.runtime, [
|
||||
"preprocess", "dwh", "--steps", "introspect,lsh",
|
||||
...(scope.job.childRuns.dwh ? ["--resume", scope.job.childRuns.dwh] : []),
|
||||
"--json", "-c", "/dev/fd/3",
|
||||
]);
|
||||
scope.job.childRuns.dwh = this.requireRunId(payload.run_id);
|
||||
scope.job.completedStages.push("dwh");
|
||||
this.state(scope.runtime.workspaceId).writeJob(scope.job);
|
||||
}
|
||||
if (!scope.job.completedStages.includes("fk_suggest")) {
|
||||
const suggest = await this.runSuggestStage(scope, [], []);
|
||||
if (suggest.code === "manual_review_required") return suggest;
|
||||
}
|
||||
const candidate = this.state(scope.runtime.workspaceId).readFkCandidates(scope.job.runId);
|
||||
if (candidate && !scope.job.completedStages.includes("fk_review")) {
|
||||
// P5: continuation requires a review accepted for this candidate whose accepted blob digest
|
||||
// equals the current revision's synced annotations. A revision change (or a missing curated
|
||||
// blob) therefore records a new review checkpoint instead of silently reusing the old one.
|
||||
const accepted = this.findAcceptedReviewForDigest(scope.runtime.workspaceId, candidate.digest);
|
||||
const currentDigest = this.currentAnnotationsDigest(scope.runtime);
|
||||
if (accepted === undefined || currentDigest === undefined || accepted.annotationsDigest !== currentDigest) {
|
||||
return baseResult(scope.runtime, "preprocess run", "blocked", "manual_review_required", {
|
||||
runId: scope.job.runId,
|
||||
childRuns: { ...scope.job.childRuns },
|
||||
completedStages: [...scope.job.completedStages],
|
||||
artifactIdentities: [{ kind: "fk_candidates", digest: candidate.digest }],
|
||||
});
|
||||
const revision = started.database.metadataContentRevision;
|
||||
let finished = false;
|
||||
let failureCode = "catalog_snapshot_failed";
|
||||
try {
|
||||
const snapshot = await buildCatalogMetadataSnapshot(
|
||||
repository,
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
);
|
||||
const snapshotPath = this.publishCatalogSnapshot(
|
||||
scope.runtime.workspaceId,
|
||||
JSON.stringify(snapshot),
|
||||
);
|
||||
this.completeStages(scope, "catalog_snapshot");
|
||||
const semantic = await this.deps.semanticPreflight(scope.runtime.workspace);
|
||||
if (!semantic.ok) {
|
||||
await repository.finishPreprocessing(
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "failed", errorCode: semantic.code },
|
||||
);
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
finished = true;
|
||||
return baseResult(scope.runtime, "preprocess run", "failed", semantic.code);
|
||||
}
|
||||
scope.job.reviewDigest = accepted.reviewedCandidatesDigest;
|
||||
scope.job.completedStages.push("fk_review");
|
||||
this.state(scope.runtime.workspaceId).writeJob(scope.job);
|
||||
|
||||
failureCode = "schema_index_failed";
|
||||
const payload = await this.runJsonStage(scope.runtime, [
|
||||
"preprocess",
|
||||
"catalog",
|
||||
"--catalog-metadata",
|
||||
snapshotPath,
|
||||
"--json",
|
||||
"-c",
|
||||
"/dev/fd/3",
|
||||
]);
|
||||
this.completeStages(scope, "catalog_metadata", "lsh", "schema_index");
|
||||
failureCode = "evidence_preprocessing_failed";
|
||||
const outcome = await continueEvidencePreprocessing(
|
||||
{
|
||||
evidence: scope.runtime.workspace.evidence,
|
||||
job: scope.job,
|
||||
httpPrivateHostAllowlist: this.deps.httpPrivateHostAllowlist,
|
||||
priorCounts: this.numberRecord(payload.counts),
|
||||
},
|
||||
this.evidenceDependencies(scope),
|
||||
);
|
||||
if (!["succeeded", "unchanged"].includes(outcome.status)) {
|
||||
await repository.finishPreprocessing(
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "failed", errorCode: outcome.code },
|
||||
);
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
finished = true;
|
||||
return this.evidenceResult(scope, outcome);
|
||||
}
|
||||
// Evidence has been published. The only remaining operation is the atomic Catalog commit.
|
||||
this.completeStages(scope, "evidence");
|
||||
const completed = await repository.finishPreprocessing(
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "succeeded" },
|
||||
);
|
||||
if (!completed) throw new Error("catalog preprocessing completion lost its lease");
|
||||
scope.job.status = "succeeded";
|
||||
scope.state.writeJob(scope.job);
|
||||
finished = true;
|
||||
return this.evidenceResult(scope, outcome);
|
||||
} catch (error) {
|
||||
if (!finished) {
|
||||
await repository.finishPreprocessing(
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "failed", errorCode: failureCode },
|
||||
);
|
||||
}
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
throw error;
|
||||
}
|
||||
const semantic = await this.deps.semanticPreflight(scope.runtime.workspace);
|
||||
if (!semantic.ok) return baseResult(scope.runtime, "preprocess run", "failed", semantic.code, { runId: scope.job.runId });
|
||||
let schemaCounts: Record<string, number> | undefined;
|
||||
if (!scope.job.completedStages.includes("schema_index")) {
|
||||
const payload = await this.runJsonStage(scope.runtime, ["vector", "index-schema", "--json", "-c", "/dev/fd/3"]);
|
||||
scope.job.completedStages.push("schema_index");
|
||||
this.state(scope.runtime.workspaceId).writeJob(scope.job);
|
||||
schemaCounts = this.numberRecord(payload.counts);
|
||||
}
|
||||
const outcome = await continueEvidencePreprocessing(
|
||||
{
|
||||
evidence: scope.runtime.workspace.evidence,
|
||||
job: scope.job,
|
||||
httpPrivateHostAllowlist: this.deps.httpPrivateHostAllowlist,
|
||||
priorCounts: schemaCounts,
|
||||
},
|
||||
this.evidenceDependencies(scope),
|
||||
);
|
||||
return this.evidenceResult(scope, "preprocess run", outcome);
|
||||
}
|
||||
|
||||
private async startRun(workspaceId: string, operation: string, resumeRunId?: string): Promise<RunScope> {
|
||||
private async startRun(workspaceId: string): Promise<RunScope> {
|
||||
const runtime = await this.deps.acquireActiveRuntime(workspaceId);
|
||||
const state = this.state(runtime.workspaceId);
|
||||
await state.assertSessionInventoryCompatible(runtime.workspaceRevision, await this.deps.listSessions(runtime.workspaceId));
|
||||
const job = await state.beginJob({
|
||||
operation,
|
||||
runId: resumeRunId,
|
||||
operation: "preprocess run",
|
||||
workspaceRevision: runtime.workspaceRevision,
|
||||
descriptorBlob: runtime.descriptorBlob,
|
||||
catalogBlob: runtime.catalogBlob,
|
||||
@@ -411,6 +292,28 @@ export class WorkspacePreprocessingService {
|
||||
return new PreprocessingStateStore({ dataRoot: this.deps.dataRoot, workspaceId });
|
||||
}
|
||||
|
||||
private completeStages(scope: RunScope, ...stages: string[]): void {
|
||||
for (const stage of stages) {
|
||||
if (!scope.job.completedStages.includes(stage)) scope.job.completedStages.push(stage);
|
||||
}
|
||||
scope.state.writeJob(scope.job);
|
||||
}
|
||||
|
||||
private publishCatalogSnapshot(workspaceId: string, contents: string): string {
|
||||
const root = join(this.deps.dataRoot, "sessions", workspaceId, "preprocessing");
|
||||
mkdirSync(root, { recursive: true, mode: 0o700 });
|
||||
const target = join(root, "catalog-metadata.json");
|
||||
const staging = join(root, `.catalog-metadata-${randomBytes(6).toString("hex")}.json`);
|
||||
try {
|
||||
writeFileSync(staging, contents, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
||||
renameSync(staging, target);
|
||||
return target;
|
||||
} catch (error) {
|
||||
rmSync(staging, { force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private evidenceDependencies(scope: RunScope): EvidencePreprocessingDependencies {
|
||||
return {
|
||||
runStage: async (argv) => await this.runJsonStage(scope.runtime, argv),
|
||||
@@ -423,50 +326,10 @@ export class WorkspacePreprocessingService {
|
||||
|
||||
private evidenceResult(
|
||||
scope: RunScope,
|
||||
operation: "preprocess evidence" | "preprocess run",
|
||||
outcome: EvidencePreprocessingOutcome,
|
||||
): WorkspaceOperationResult {
|
||||
const { status, code, ...extra } = outcome;
|
||||
return baseResult(scope.runtime, operation, status, code, extra);
|
||||
}
|
||||
|
||||
private async runSuggestStage(
|
||||
scope: RunScope,
|
||||
fromSql: ReadonlyArray<{ name: string; sql: string }>,
|
||||
assume: readonly string[],
|
||||
): Promise<WorkspaceOperationResult> {
|
||||
const payload = await this.withStagedInputs(scope.runtime.workspaceId, fromSql.map((entry) => ({
|
||||
flag: "--from-sql",
|
||||
name: entry.name,
|
||||
contents: entry.sql,
|
||||
})), async (stagedArgv) => await this.runJsonStage(scope.runtime, [
|
||||
"schema", "suggest-fks", ...stagedArgv,
|
||||
...assume.flatMap((value) => ["--assume", value]),
|
||||
"--json", "-c", "/dev/fd/3",
|
||||
]));
|
||||
const candidateCount = Number(payload.candidate_count ?? 0);
|
||||
const candidateYaml = typeof payload.candidate_yaml === "string" ? payload.candidate_yaml : "";
|
||||
let artifactIdentities: Array<{ kind: string; digest: string }> | undefined;
|
||||
if (candidateCount > 0) {
|
||||
const persisted = this.state(scope.runtime.workspaceId).writeFkCandidates(scope.job.runId, candidateYaml);
|
||||
scope.job.candidateDigest = persisted.digest;
|
||||
artifactIdentities = [{ kind: "fk_candidates", digest: persisted.digest }];
|
||||
}
|
||||
if (!scope.job.completedStages.includes("fk_suggest")) scope.job.completedStages.push("fk_suggest");
|
||||
this.state(scope.runtime.workspaceId).writeJob(scope.job);
|
||||
const resultExtra = {
|
||||
runId: scope.job.runId,
|
||||
completedStages: [...scope.job.completedStages],
|
||||
...(artifactIdentities ? { artifactIdentities } : {}),
|
||||
...(candidateYaml.length > 0 ? { suggestedFksYaml: candidateYaml } : {}),
|
||||
};
|
||||
if (candidateCount > 0) {
|
||||
return baseResult(scope.runtime, scope.job.operation === "preprocess run" ? "preprocess run" : "schema suggest-fks", "blocked", "manual_review_required", {
|
||||
...resultExtra,
|
||||
childRuns: { ...scope.job.childRuns },
|
||||
});
|
||||
}
|
||||
return baseResult(scope.runtime, scope.job.operation === "preprocess run" ? "preprocess run" : "schema suggest-fks", "succeeded", "ok", resultExtra);
|
||||
return baseResult(scope.runtime, "preprocess run", status, code, extra);
|
||||
}
|
||||
|
||||
private async runJsonStage(runtime: ActiveRuntime, argv: string[]): Promise<Record<string, unknown>> {
|
||||
@@ -485,54 +348,5 @@ export class WorkspacePreprocessingService {
|
||||
return Object.fromEntries(Object.entries(value as Record<string, unknown>).map(([key, nested]) => [key, Number(nested)]));
|
||||
}
|
||||
|
||||
private async withStagedInputs<T>(
|
||||
workspaceId: string,
|
||||
inputs: ReadonlyArray<{ flag: string; name: string; contents: string }>,
|
||||
fn: (argv: string[]) => Promise<T>,
|
||||
): Promise<T> {
|
||||
if (inputs.length === 0) return await fn([]);
|
||||
const root = join(this.deps.dataRoot, "sessions", workspaceId, "preprocessing", `.stage-${randomBytes(6).toString("hex")}`);
|
||||
mkdirSync(root, { recursive: true, mode: 0o700 });
|
||||
const argv: string[] = [];
|
||||
const paths: string[] = [];
|
||||
try {
|
||||
for (const input of inputs) {
|
||||
const path = join(root, input.name);
|
||||
writeFileSync(path, input.contents, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
||||
paths.push(path);
|
||||
argv.push(input.flag, path);
|
||||
}
|
||||
return await fn(argv);
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
private currentAnnotationsDigest(runtime: ActiveRuntime): string | undefined {
|
||||
return readAnnotationsSync(this.deps.dataRoot, runtime.workspaceId, runtime.workspaceRevision)?.contentDigest;
|
||||
}
|
||||
|
||||
private findAcceptedReviewForDigest(
|
||||
workspaceId: string,
|
||||
digestValue: string,
|
||||
): FkReviewRecord | undefined {
|
||||
const state = this.state(workspaceId);
|
||||
for (const entry of readdirSync(state.fkReviewsDirectory(), { withFileTypes: true })) {
|
||||
if (!entry.isFile() || entry.isSymbolicLink() || !/^[0-9a-f]{32}\.json$/.test(entry.name)) continue;
|
||||
const runId = entry.name.slice(0, -".json".length);
|
||||
const review = state.readFkReview(runId);
|
||||
if (review && review.reviewedCandidatesDigest === digestValue) return review;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private findRunIdByCandidateDigest(state: PreprocessingStateStore, digestValue: string): string | undefined {
|
||||
for (const entry of readdirSync(state.fkCandidatesDirectory(), { withFileTypes: true })) {
|
||||
if (!entry.isFile() || entry.isSymbolicLink() || !/^[0-9a-f]{32}\.yaml$/.test(entry.name)) continue;
|
||||
const runId = entry.name.slice(0, -".yaml".length);
|
||||
if (state.readFkCandidates(runId)?.digest === digestValue) return runId;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -198,6 +198,7 @@ export class PreprocessingStateStore {
|
||||
runtimeConfigDirectory(): string { return join(this.root, "runtime-config"); }
|
||||
runtimeConfigManifestDirectory(): string { return join(this.root, "runtime-config-manifests"); }
|
||||
jobsDirectory(): string { return join(this.root, "jobs"); }
|
||||
latestJobPath(): string { return join(this.root, "latest-job.json"); }
|
||||
fkCandidatesDirectory(): string { return join(this.root, "fk-candidates"); }
|
||||
fkReviewsDirectory(): string { return join(this.root, "fk-reviews"); }
|
||||
jobPath(runId: string): string { return join(this.jobsDirectory(), `${validateRunId(runId)}.json`); }
|
||||
@@ -289,7 +290,7 @@ export class PreprocessingStateStore {
|
||||
"Workspace preprocessing resume no longer matches the pinned revision",
|
||||
);
|
||||
}
|
||||
return existing;
|
||||
return this.writeJob(existing);
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
||||
if (error instanceof PreprocessingStateError) throw error;
|
||||
@@ -318,19 +319,30 @@ export class PreprocessingStateStore {
|
||||
childRuns: {},
|
||||
status: "active",
|
||||
};
|
||||
writeAtomicFile(path, `${JSON.stringify(job)}
|
||||
`, 0o600);
|
||||
return job;
|
||||
return this.writeJob(job);
|
||||
}
|
||||
|
||||
readJob(runId: string): PreprocessingJobState {
|
||||
return decodeJob(JSON.parse(readTrustedFile(this.jobPath(runId))));
|
||||
}
|
||||
|
||||
readLatestJob(): PreprocessingJobState | undefined {
|
||||
try {
|
||||
return decodeJob(JSON.parse(readTrustedFile(this.latestJobPath())));
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
writeJob(job: PreprocessingJobState): PreprocessingJobState {
|
||||
this.ensureLayout();
|
||||
writeAtomicFile(this.jobPath(job.runId), `${JSON.stringify(job)}
|
||||
`, 0o600);
|
||||
const contents = `${JSON.stringify(job)}
|
||||
`;
|
||||
writeAtomicFile(this.jobPath(job.runId), contents, 0o600);
|
||||
// This fixed pointer is the sole status surface for operators. Per-run files remain an
|
||||
// internal resume mechanism and are never exposed as history.
|
||||
writeAtomicFile(this.latestJobPath(), contents, 0o600);
|
||||
return job;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import {
|
||||
canonicalEffectiveConfigJson,
|
||||
configFingerprint,
|
||||
effectiveConfigIdentity,
|
||||
inputFingerprint,
|
||||
preprocessingInputFingerprint,
|
||||
type CanonicalEffectiveConfig,
|
||||
} from "./effective-config.js";
|
||||
import { resolveRuntimeBindings, type RuntimeBindings } from "./bindings.js";
|
||||
@@ -41,6 +41,8 @@ import {
|
||||
} from "./runtime-renderer.js";
|
||||
import { parseWorkspaceYaml, validateOperationalWorkspace, type WorkspaceDescriptor } from "./schema.js";
|
||||
import type { WorkspaceRegistryConfig } from "./types.js";
|
||||
import { resolveCatalogRuntimeBinding } from "../catalog/runtime-binding.js";
|
||||
import type { WorkspaceDatabase } from "../catalog/types.js";
|
||||
|
||||
export interface RuntimeConfigLease {
|
||||
path: string;
|
||||
@@ -246,8 +248,6 @@ function readSnapshotWorkspace(snapshotPath: string): {
|
||||
function runtimePaths(
|
||||
dataRoot: string,
|
||||
workspaceId: string,
|
||||
workspaceRevision?: string,
|
||||
effectiveRelationshipsPath?: string,
|
||||
): RuntimePaths {
|
||||
if (!isAbsolute(dataRoot)) throw new Error("registry workspace runtime requires an absolute data root");
|
||||
const root = join(dataRoot, "sessions", workspaceId);
|
||||
@@ -256,12 +256,7 @@ function runtimePaths(
|
||||
artifacts: join(root, "artifacts"),
|
||||
indexes: join(root, "indexes"),
|
||||
memory: join(root, "memory"),
|
||||
...(workspaceRevision === undefined
|
||||
? {}
|
||||
: { annotations_root: join(dataRoot, "sessions", workspaceId, "revisions", workspaceRevision, "artifacts") }),
|
||||
...(effectiveRelationshipsPath === undefined
|
||||
? {}
|
||||
: { effective_relationships: effectiveRelationshipsPath }),
|
||||
catalog_metadata_snapshot: join(root, "preprocessing", "catalog-metadata.json"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -309,19 +304,32 @@ function renderWorkspaceRuntimeFromWorkspace(options: {
|
||||
dataRoot: string;
|
||||
secretRoots: readonly string[];
|
||||
semanticRuntime: SemanticRuntimeConfig;
|
||||
effectiveRelationshipsPath?: string;
|
||||
workspaceSecretStore?: WorkspaceSecretStore;
|
||||
catalogDatabase?: WorkspaceDatabase;
|
||||
}): RenderedWorkspaceRuntime {
|
||||
const secretLease = options.workspaceSecretStore === undefined
|
||||
if (options.catalogDatabase && !options.workspaceSecretStore) {
|
||||
throw new Error("Catalog runtime binding requires the workspace secret store");
|
||||
}
|
||||
const catalogLease = options.catalogDatabase === undefined
|
||||
? undefined
|
||||
: resolveCatalogRuntimeBinding({
|
||||
workspace: options.workspace,
|
||||
database: options.catalogDatabase,
|
||||
environment: process.env,
|
||||
secretRoots: options.secretRoots,
|
||||
secretStore: options.workspaceSecretStore!,
|
||||
});
|
||||
const runtimeWorkspace = catalogLease?.workspace ?? options.workspace;
|
||||
const secretLease = catalogLease !== undefined || options.workspaceSecretStore === undefined
|
||||
? undefined
|
||||
: resolveRuntimeBindingsWithWorkspaceSecrets(
|
||||
options.workspace,
|
||||
runtimeWorkspace,
|
||||
process.env,
|
||||
options.secretRoots,
|
||||
options.workspaceSecretStore,
|
||||
);
|
||||
const bindings = secretLease?.bindings
|
||||
?? resolveRuntimeBindings(options.workspace, process.env, options.secretRoots);
|
||||
const bindings = catalogLease?.bindings ?? secretLease?.bindings
|
||||
?? resolveRuntimeBindings(runtimeWorkspace, process.env, options.secretRoots);
|
||||
const overlay = installationOverlay(options.harnessDir, options.configPath);
|
||||
const context: RuntimeRenderContext = {
|
||||
workspaceId: options.workspaceId,
|
||||
@@ -334,32 +342,26 @@ function renderWorkspaceRuntimeFromWorkspace(options: {
|
||||
workspaceId: options.workspaceId,
|
||||
workspaceRevision: options.workspaceRevision,
|
||||
revisionContentRoot: options.revisionContentRoot,
|
||||
runtimePaths: runtimePaths(
|
||||
options.dataRoot,
|
||||
options.workspaceId,
|
||||
options.workspaceRevision,
|
||||
options.effectiveRelationshipsPath,
|
||||
),
|
||||
runtimePaths: runtimePaths(options.dataRoot, options.workspaceId),
|
||||
installationOverlay: overlay,
|
||||
bindings,
|
||||
bindingDigest: stableBindingDigest(bindings),
|
||||
semanticQdrantUrl: options.semanticRuntime.internalQdrantUrl,
|
||||
releaseSecrets: () => secretLease?.release(),
|
||||
releaseSecrets: () => {
|
||||
catalogLease?.release();
|
||||
secretLease?.release();
|
||||
},
|
||||
renderedConfig: renderRuntimeConfig(
|
||||
options.workspace,
|
||||
runtimeWorkspace,
|
||||
bindings,
|
||||
runtimePaths(
|
||||
options.dataRoot,
|
||||
options.workspaceId,
|
||||
options.workspaceRevision,
|
||||
options.effectiveRelationshipsPath,
|
||||
),
|
||||
runtimePaths(options.dataRoot, options.workspaceId),
|
||||
context,
|
||||
overlay,
|
||||
options.semanticRuntime,
|
||||
),
|
||||
};
|
||||
} catch (error) {
|
||||
catalogLease?.release();
|
||||
secretLease?.release();
|
||||
throw error;
|
||||
}
|
||||
@@ -372,8 +374,8 @@ export function renderWorkspaceRuntimeFromSnapshotPath(options: {
|
||||
dataRoot: string;
|
||||
secretRoots: readonly string[];
|
||||
semanticRuntime: SemanticRuntimeConfig;
|
||||
effectiveRelationshipsPath?: string;
|
||||
workspaceSecretStore?: WorkspaceSecretStore;
|
||||
catalogDatabase?: WorkspaceDatabase;
|
||||
}): RenderedWorkspaceRuntime {
|
||||
const snapshot = readSnapshotWorkspace(options.snapshotPath);
|
||||
return renderWorkspaceRuntimeFromWorkspace({
|
||||
@@ -386,8 +388,8 @@ export function renderWorkspaceRuntimeFromSnapshotPath(options: {
|
||||
dataRoot: options.dataRoot,
|
||||
secretRoots: options.secretRoots,
|
||||
semanticRuntime: options.semanticRuntime,
|
||||
effectiveRelationshipsPath: options.effectiveRelationshipsPath,
|
||||
workspaceSecretStore: options.workspaceSecretStore,
|
||||
catalogDatabase: options.catalogDatabase,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -401,6 +403,7 @@ export async function renderActiveWorkspaceRuntime(options: {
|
||||
secretRoots: readonly string[];
|
||||
semanticRuntime: SemanticRuntimeConfig;
|
||||
workspaceSecretStore?: WorkspaceSecretStore;
|
||||
catalogDatabase?: WorkspaceDatabase;
|
||||
}): Promise<ActiveRenderedWorkspaceRuntime> {
|
||||
// The persisted active state may reference host-side snapshot paths (written by another
|
||||
// process or installation). Read the active state directly and resolve the immutable snapshot
|
||||
@@ -431,6 +434,7 @@ export async function renderActiveWorkspaceRuntime(options: {
|
||||
secretRoots: options.secretRoots,
|
||||
semanticRuntime: options.semanticRuntime,
|
||||
workspaceSecretStore: options.workspaceSecretStore,
|
||||
catalogDatabase: options.catalogDatabase,
|
||||
});
|
||||
return {
|
||||
...rendered,
|
||||
@@ -480,6 +484,7 @@ export async function publishDeterministicRuntimeConfigLease(options: {
|
||||
secretRoots: readonly string[];
|
||||
semanticRuntime: SemanticRuntimeConfig;
|
||||
workspaceSecretStore?: WorkspaceSecretStore;
|
||||
catalogDatabase?: WorkspaceDatabase;
|
||||
}): Promise<DeterministicRuntimeConfigLease> {
|
||||
const rendered = await renderActiveWorkspaceRuntime(options);
|
||||
const publishedConfig = applyCollectionLifecycle(rendered.renderedConfig, "require_existing");
|
||||
@@ -487,7 +492,11 @@ export async function publishDeterministicRuntimeConfigLease(options: {
|
||||
const effectiveConfig = buildCanonicalEffectiveConfig(renderedConfigObject);
|
||||
const effectiveConfigIdentityValue = effectiveConfigIdentity(rendered.workspaceId, renderedConfigObject);
|
||||
const configFingerprintValue = configFingerprint(renderedConfigObject);
|
||||
const inputFingerprintValue = inputFingerprint(rendered.workspaceId, renderedConfigObject);
|
||||
const inputFingerprintValue = preprocessingInputFingerprint(
|
||||
rendered.workspaceId,
|
||||
rendered.workspaceRevision,
|
||||
renderedConfigObject,
|
||||
);
|
||||
const identitySuffix = inputFingerprintValue.slice(7, 23);
|
||||
|
||||
const preprocessingRoot = ensureTrustedDirectory(join(
|
||||
|
||||
@@ -3,6 +3,7 @@ import { stringify } from "yaml";
|
||||
import { buildInstallationContract } from "./contracts.js";
|
||||
import { validateWorkspaceDescriptor, type WorkspaceDescriptor } from "./schema.js";
|
||||
import type { ResolvedBinding, ResolvedEvidenceBinding, RuntimeBindings } from "./bindings.js";
|
||||
import { workspaceVectorCollections } from "./vector-collections.js";
|
||||
export type { RuntimeBindings } from "./bindings.js";
|
||||
|
||||
export interface RuntimePaths {
|
||||
@@ -10,10 +11,8 @@ export interface RuntimePaths {
|
||||
artifacts: string;
|
||||
indexes: string;
|
||||
memory: string;
|
||||
/** Revision-qualified root for curated FK annotations (P5); optional for legacy callers. */
|
||||
annotations_root?: string;
|
||||
/** Immutable Catalog projection used as the exclusive runtime relationship source. */
|
||||
effective_relationships?: string;
|
||||
/** Current backend-produced projection of the PostgreSQL Metadata Catalog. */
|
||||
catalog_metadata_snapshot?: string;
|
||||
}
|
||||
|
||||
export interface RuntimeIdentity {
|
||||
@@ -224,6 +223,7 @@ export function renderRuntimeConfig(
|
||||
): string {
|
||||
requireSupportedDescriptor(workspace);
|
||||
const descriptor = validateWorkspaceDescriptor(workspace);
|
||||
if (!descriptor.dwh) throw new Error("runtime configuration requires a Catalog database binding");
|
||||
const contract = buildInstallationContract(descriptor);
|
||||
const name = (role: "DWH" | "EVIDENCE", suffix: string) => {
|
||||
const variable = contract.variables.find((entry) => entry.role === role && entry.suffix === suffix);
|
||||
@@ -243,6 +243,7 @@ export function renderRuntimeConfig(
|
||||
}
|
||||
|
||||
const dwhIdentity = { database: descriptor.dwh.database, schema: descriptor.dwh.schema };
|
||||
const vectorCollections = workspaceVectorCollections(descriptor.workspace.id);
|
||||
const database = bindings.dwh.transport === "postgres_direct"
|
||||
? { ...directConnection(bindings.dwh, {
|
||||
host: name("DWH", "HOST"),
|
||||
@@ -268,7 +269,7 @@ export function renderRuntimeConfig(
|
||||
semantic_index: {
|
||||
vector_store: {
|
||||
engine: "qdrant",
|
||||
collection: descriptor.workspace.id,
|
||||
collections: vectorCollections,
|
||||
dimensions: semanticRuntime.internalEmbeddingDimensions,
|
||||
distance: "cosine",
|
||||
},
|
||||
@@ -284,7 +285,7 @@ export function renderRuntimeConfig(
|
||||
vector: {
|
||||
engine: "qdrant",
|
||||
base_url: semanticRuntime.internalQdrantUrl,
|
||||
collection: descriptor.workspace.id,
|
||||
collections: vectorCollections,
|
||||
},
|
||||
embeddings: {
|
||||
provider: "ollama_internal",
|
||||
|
||||
@@ -53,7 +53,8 @@ interface WorkspaceDwh {
|
||||
|
||||
interface WorkspaceBase {
|
||||
workspace: WorkspaceMetadata;
|
||||
dwh: WorkspaceDwh;
|
||||
/** Legacy connection block; current descriptors bind their database through PostgreSQL. */
|
||||
dwh?: WorkspaceDwh;
|
||||
diagnostics?: Pick<CanonicalDiagnostics, "dwh_rest">;
|
||||
}
|
||||
|
||||
@@ -328,7 +329,9 @@ function unique<T>(values: readonly T[], context: z.RefinementCtx, path: Propert
|
||||
}
|
||||
|
||||
function workspaceInvariants(workspace: any, context: z.RefinementCtx): void {
|
||||
unique(workspace.dwh.supported_transports, context, ["dwh", "supported_transports"]);
|
||||
if (workspace.dwh) {
|
||||
unique(workspace.dwh.supported_transports, context, ["dwh", "supported_transports"]);
|
||||
}
|
||||
|
||||
if (workspace.evidence?.source.type === "filesystem") {
|
||||
const expected = `${workspace.workspace.id}/evidence`;
|
||||
@@ -341,7 +344,8 @@ function workspaceInvariants(workspace: any, context: z.RefinementCtx): void {
|
||||
}
|
||||
}
|
||||
|
||||
if (workspace.diagnostics?.dwh_rest && !workspace.dwh.supported_transports.includes("rest_api")) {
|
||||
if (workspace.diagnostics?.dwh_rest
|
||||
&& !workspace.dwh?.supported_transports.includes("rest_api")) {
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["diagnostics", "dwh_rest"],
|
||||
@@ -351,7 +355,7 @@ function workspaceInvariants(workspace: any, context: z.RefinementCtx): void {
|
||||
}
|
||||
|
||||
const WorkspaceV4Schema = z.object({
|
||||
dwh: dwhSchema,
|
||||
dwh: dwhSchema.optional(),
|
||||
evidence: workspaceEvidenceSchema.optional(),
|
||||
diagnostics: z.object({
|
||||
dwh_rest: dwhRestDiagnostic.optional(),
|
||||
@@ -363,7 +367,7 @@ const WorkspaceV4Schema = z.object({
|
||||
}).strict().superRefine(workspaceInvariants);
|
||||
const WorkspaceDescriptorSchema = WorkspaceV4Schema;
|
||||
|
||||
export function parseWorkspaceYaml(source: string): WorkspaceDescriptor {
|
||||
function parseWorkspaceDocument(source: string): unknown {
|
||||
const documents = parseAllDocuments(source, { uniqueKeys: true });
|
||||
if (documents.length !== 1) throw new Error("Workspace YAML must contain exactly one document");
|
||||
const document = documents[0];
|
||||
@@ -371,10 +375,30 @@ export function parseWorkspaceYaml(source: string): WorkspaceDescriptor {
|
||||
throw new Error(`Invalid workspace YAML: ${[...document.errors, ...document.warnings]
|
||||
.map((error) => error.message).join("; ")}`);
|
||||
}
|
||||
return validateWorkspaceDescriptor(document.toJSON());
|
||||
return document.toJSON();
|
||||
}
|
||||
|
||||
/** Deterministically removes the two installation-owned v3 blocks without altering workspace data. */
|
||||
export function parseWorkspaceYaml(source: string): WorkspaceDescriptor {
|
||||
const value = parseWorkspaceDocument(source);
|
||||
assertAuthoredWorkspaceIsDatabaseFree(value);
|
||||
return validateWorkspaceDescriptor(value);
|
||||
}
|
||||
|
||||
/** Parses an ephemeral, generated core runtime descriptor that may contain a Catalog binding. */
|
||||
export function parseRuntimeWorkspaceYaml(source: string): WorkspaceDescriptor {
|
||||
return validateWorkspaceDescriptor(parseWorkspaceDocument(source));
|
||||
}
|
||||
|
||||
function assertAuthoredWorkspaceIsDatabaseFree(workspace: unknown): void {
|
||||
if (workspace !== null && typeof workspace === "object"
|
||||
&& ("dwh" in workspace || "diagnostics" in workspace)) {
|
||||
throw new Error(
|
||||
"Workspace YAML must not contain database configuration; use the PostgreSQL Metadata Catalog",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Build a database-free v4 descriptor; legacy database/configuration fields are not carried over. */
|
||||
export function migrateWorkspaceV3Yaml(source: string): string {
|
||||
const documents = parseAllDocuments(source, { uniqueKeys: true });
|
||||
if (documents.length !== 1) throw new Error("Workspace YAML must contain exactly one document");
|
||||
@@ -388,6 +412,8 @@ export function migrateWorkspaceV3Yaml(source: string): string {
|
||||
throw new Error("Workspace migration requires schema version 3");
|
||||
}
|
||||
metadata.schema_version = 4;
|
||||
delete value.dwh;
|
||||
delete value.diagnostics;
|
||||
delete value.semantic_index;
|
||||
delete value.llm_policy;
|
||||
return serializeWorkspaceYaml(validateWorkspaceDescriptor(value));
|
||||
@@ -423,6 +449,7 @@ export function resolveDiagnosticUrl(baseUrl: string, path: string): URL {
|
||||
|
||||
export function serializeWorkspaceYaml(workspace: CanonicalWorkspace): string {
|
||||
const canonical = validateOperationalWorkspace(workspace);
|
||||
assertAuthoredWorkspaceIsDatabaseFree(canonical);
|
||||
return stringify(canonical, { lineWidth: 0, sortMapEntries: true });
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,8 @@ function selectedTransport(
|
||||
descriptor: WorkspaceDescriptor,
|
||||
variables: readonly InstallationVariable[],
|
||||
env: NodeJS.ProcessEnv,
|
||||
): DwhTransport {
|
||||
): DwhTransport | undefined {
|
||||
if (!descriptor.dwh) return undefined;
|
||||
const transportVariable = variables.find(({ role, suffix }) => role === "DWH" && suffix === "TRANSPORT");
|
||||
const value = transportVariable === undefined ? undefined : env[transportVariable.name];
|
||||
return isTransport(value) && descriptor.dwh.supported_transports.includes(value)
|
||||
@@ -136,11 +137,14 @@ export function discoverWorkspaceSecretRequirements(
|
||||
const variables = buildInstallationContract(descriptor).variables;
|
||||
const transport = selectedTransport(descriptor, variables, env);
|
||||
const restHasNoAuthentication = transport === "rest_api" && descriptor.diagnostics?.dwh_rest?.auth === "none";
|
||||
const requiredDwh = new Set(restHasNoAuthentication ? [] : REQUIRED_DWH_SECRETS[transport]);
|
||||
const requiredDwh = new Set(
|
||||
transport === undefined || restHasNoAuthentication ? [] : REQUIRED_DWH_SECRETS[transport],
|
||||
);
|
||||
|
||||
const requirements: WorkspaceSecretRequirement[] = [];
|
||||
for (const variable of variables) {
|
||||
if (variable.role === "DWH") {
|
||||
if (transport === undefined) continue;
|
||||
if (variable.transports !== undefined && !variable.transports.includes(transport)) continue;
|
||||
const requirement = requirementFor(variable, requiredDwh.has(variable.suffix));
|
||||
if (requirement !== undefined && requirement.required) requirements.push(requirement);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
export interface WorkspaceVectorCollections {
|
||||
reference: string;
|
||||
memory: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Physical Qdrant namespaces owned by one workspace.
|
||||
*
|
||||
* Reference data is replaceable preprocessing output. Memory is durable runtime
|
||||
* state and deliberately has a separate lifecycle.
|
||||
*/
|
||||
export function workspaceVectorCollections(workspaceId: string): WorkspaceVectorCollections {
|
||||
if (!/^[a-z][a-z0-9-]{2,62}$/.test(workspaceId)) {
|
||||
throw new Error("workspace id is invalid");
|
||||
}
|
||||
return {
|
||||
reference: `${workspaceId}-reference`,
|
||||
memory: `${workspaceId}-memory`,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user