refactor: make workspace descriptors schema v3 only
This commit is contained in:
@@ -34,8 +34,8 @@ export interface CanonicalDiagnostics {
|
||||
embedding?: RestDiagnosticRequest & { response: { model: string; dimensions: string } };
|
||||
}
|
||||
|
||||
interface WorkspaceMetadata<Version extends 1 | 2 | 3> {
|
||||
schema_version: Version;
|
||||
interface WorkspaceMetadata {
|
||||
schema_version: 3;
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
@@ -51,50 +51,22 @@ interface WorkspaceDwh {
|
||||
supported_transports: DwhTransport[];
|
||||
}
|
||||
|
||||
interface VectorStore {
|
||||
engine: "pgvector";
|
||||
collection: string;
|
||||
dimensions: number;
|
||||
distance: "cosine" | "l2" | "inner_product";
|
||||
port?: number;
|
||||
timeout_ms?: number;
|
||||
supported_transports: VectorTransport[];
|
||||
}
|
||||
|
||||
interface SemanticIndex<TVectorStore extends VectorStore> {
|
||||
vector_store: TVectorStore;
|
||||
vector_writer?: Record<string, never>;
|
||||
embedding: {
|
||||
provider: "ollama_compatible" | "openai_compatible";
|
||||
model: string;
|
||||
dimensions: number;
|
||||
timeout_ms?: number;
|
||||
};
|
||||
}
|
||||
|
||||
interface WorkspaceBase<Version extends 1 | 2 | 3, TVectorStore, TDiagnostics = CanonicalDiagnostics> {
|
||||
workspace: WorkspaceMetadata<Version>;
|
||||
interface WorkspaceBase<TVectorStore> {
|
||||
workspace: WorkspaceMetadata;
|
||||
dwh: WorkspaceDwh;
|
||||
semantic_index: {
|
||||
vector_store: TVectorStore;
|
||||
vector_writer?: Record<string, never>;
|
||||
embedding: Version extends 3 ? {
|
||||
embedding: {
|
||||
provider: "ollama_internal";
|
||||
model: "qwen3-embedding:0.6b";
|
||||
dimensions: 1024;
|
||||
timeout_ms?: number;
|
||||
} : {
|
||||
provider: "ollama_compatible" | "openai_compatible";
|
||||
model: string;
|
||||
dimensions: number;
|
||||
timeout_ms?: number;
|
||||
};
|
||||
};
|
||||
llm_policy: {
|
||||
default?: `${string}/${string}`;
|
||||
allowed: `${string}/${string}`[];
|
||||
};
|
||||
diagnostics?: TDiagnostics;
|
||||
diagnostics?: Pick<CanonicalDiagnostics, "dwh_rest">;
|
||||
}
|
||||
|
||||
interface QdrantVectorStore {
|
||||
@@ -147,17 +119,12 @@ export interface WorkspaceEvidence {
|
||||
policy: EvidencePolicy;
|
||||
}
|
||||
|
||||
export interface WorkspaceV3 extends WorkspaceBase<3, QdrantVectorStore> {
|
||||
export interface WorkspaceV3 extends WorkspaceBase<QdrantVectorStore> {
|
||||
evidence?: WorkspaceEvidence;
|
||||
}
|
||||
export interface WorkspaceV2 extends WorkspaceBase<2, VectorStore & { database: string; schema: string }> {}
|
||||
|
||||
/** A readable, non-operational v1 descriptor. It must be explicitly migrated before use. */
|
||||
export interface WorkspaceV1 extends WorkspaceBase<1, VectorStore & { database?: string; schema?: string }> {}
|
||||
|
||||
export type CanonicalWorkspace = WorkspaceV3;
|
||||
export type LegacyWorkspace = WorkspaceV1 | WorkspaceV2;
|
||||
export type WorkspaceDescriptor = WorkspaceV1 | WorkspaceV2 | WorkspaceV3;
|
||||
export type WorkspaceDescriptor = WorkspaceV3;
|
||||
|
||||
const workspaceId = z.string().regex(/^[a-z][a-z0-9-]{2,62}$/, {
|
||||
message: "workspace id must match ^[a-z][a-z0-9-]{2,62}$",
|
||||
@@ -165,7 +132,6 @@ const workspaceId = z.string().regex(/^[a-z][a-z0-9-]{2,62}$/, {
|
||||
const identifier = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/, {
|
||||
message: "database identifiers must start with a letter or underscore",
|
||||
});
|
||||
const dimensions = z.number().int().positive().max(32_768);
|
||||
const port = z.number().int().min(1).max(65_535);
|
||||
const timeoutMs = z.number().int().positive();
|
||||
const modelReference = z.string().regex(/^[^/\s]+\/[^/\s]+$/, {
|
||||
@@ -190,29 +156,6 @@ const restDiagnosticRequest = z.object({
|
||||
const dwhRestDiagnostic = restDiagnosticRequest.extend({
|
||||
response: z.object({ database: responseField, schema: responseField }).strict(),
|
||||
}).strict();
|
||||
const vectorMetadataDiagnostic = restDiagnosticRequest.extend({
|
||||
response: z.object({
|
||||
collection: responseField,
|
||||
dimensions: responseField,
|
||||
distance: responseField,
|
||||
}).strict(),
|
||||
}).strict();
|
||||
const reversibleVectorProbe = restDiagnosticRequest.extend({
|
||||
method: z.literal("POST"),
|
||||
auth: z.enum(["bearer", "x-api-key"]),
|
||||
response: z.object({ operation: responseField }).strict(),
|
||||
}).strict();
|
||||
const embeddingDiagnostic = restDiagnosticRequest.extend({
|
||||
response: z.object({ model: responseField, dimensions: responseField }).strict(),
|
||||
}).strict();
|
||||
const diagnosticsSchema = z.object({
|
||||
dwh_rest: dwhRestDiagnostic.optional(),
|
||||
vector_rest: z.object({
|
||||
metadata: vectorMetadataDiagnostic,
|
||||
reversible_probe: reversibleVectorProbe.optional(),
|
||||
}).strict().optional(),
|
||||
embedding: embeddingDiagnostic.optional(),
|
||||
}).strict().optional();
|
||||
|
||||
const dwhSchema = z.object({
|
||||
engine: z.literal("postgres"),
|
||||
@@ -222,36 +165,11 @@ const dwhSchema = z.object({
|
||||
timeout_ms: timeoutMs.optional(),
|
||||
supported_transports: z.array(z.enum(DWH_TRANSPORTS)).min(1),
|
||||
}).strict();
|
||||
const embeddingSchema = z.object({
|
||||
provider: z.enum(["ollama_compatible", "openai_compatible"]),
|
||||
model: z.string().trim().min(1),
|
||||
dimensions,
|
||||
timeout_ms: timeoutMs.optional(),
|
||||
}).strict();
|
||||
const internalEmbeddingSchema = z.object({
|
||||
provider: z.literal("ollama_internal"),
|
||||
model: z.literal("qwen3-embedding:0.6b"),
|
||||
dimensions: z.literal(1024),
|
||||
}).strict();
|
||||
const vectorStoreShape = {
|
||||
engine: z.literal("pgvector"),
|
||||
collection: identifier,
|
||||
dimensions,
|
||||
distance: z.enum(["cosine", "l2", "inner_product"]),
|
||||
port: port.optional(),
|
||||
timeout_ms: timeoutMs.optional(),
|
||||
supported_transports: z.array(z.enum(VECTOR_TRANSPORTS)).min(1),
|
||||
};
|
||||
const legacyVectorStoreSchema = z.object({
|
||||
...vectorStoreShape,
|
||||
database: identifier.optional(),
|
||||
schema: identifier.optional(),
|
||||
}).strict();
|
||||
const canonicalVectorStoreSchema = z.object({
|
||||
...vectorStoreShape,
|
||||
database: identifier,
|
||||
schema: identifier,
|
||||
}).strict();
|
||||
const qdrantVectorStoreSchema = z.object({
|
||||
engine: z.literal("qdrant"),
|
||||
collection: workspaceId,
|
||||
@@ -420,13 +338,6 @@ 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 ("supported_transports" in workspace.semantic_index.vector_store) {
|
||||
unique(
|
||||
workspace.semantic_index.vector_store.supported_transports,
|
||||
context,
|
||||
["semantic_index", "vector_store", "supported_transports"],
|
||||
);
|
||||
}
|
||||
unique(workspace.llm_policy.allowed, context, ["llm_policy", "allowed"]);
|
||||
|
||||
if (workspace.evidence?.source.type === "filesystem") {
|
||||
@@ -461,50 +372,8 @@ function workspaceInvariants(workspace: any, context: z.RefinementCtx): void {
|
||||
message: "diagnostics.dwh_rest requires dwh rest_api transport support",
|
||||
});
|
||||
}
|
||||
if (
|
||||
workspace.diagnostics?.vector_rest
|
||||
&& (
|
||||
!("supported_transports" in workspace.semantic_index.vector_store)
|
||||
|| !workspace.semantic_index.vector_store.supported_transports.includes("rest_api")
|
||||
)
|
||||
) {
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["diagnostics", "vector_rest"],
|
||||
message: "diagnostics.vector_rest requires vector_store rest_api transport support",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const workspaceShape = {
|
||||
dwh: dwhSchema,
|
||||
llm_policy: llmPolicySchema,
|
||||
diagnostics: diagnosticsSchema,
|
||||
};
|
||||
const WorkspaceV1Schema = z.object({
|
||||
...workspaceShape,
|
||||
workspace: z.object({
|
||||
schema_version: z.literal(1), id: workspaceId, name: z.string().trim().min(1),
|
||||
description: z.string().trim().min(1).optional(), language: z.enum(["en", "it"]),
|
||||
}).strict(),
|
||||
semantic_index: z.object({
|
||||
vector_store: legacyVectorStoreSchema,
|
||||
vector_writer: z.object({}).strict().optional(),
|
||||
embedding: embeddingSchema,
|
||||
}).strict(),
|
||||
}).strict().superRefine(workspaceInvariants);
|
||||
const WorkspaceV2Schema = z.object({
|
||||
...workspaceShape,
|
||||
workspace: z.object({
|
||||
schema_version: z.literal(2), id: workspaceId, name: z.string().trim().min(1),
|
||||
description: z.string().trim().min(1).optional(), language: z.enum(["en", "it"]),
|
||||
}).strict(),
|
||||
semantic_index: z.object({
|
||||
vector_store: canonicalVectorStoreSchema,
|
||||
vector_writer: z.object({}).strict().optional(),
|
||||
embedding: embeddingSchema,
|
||||
}).strict(),
|
||||
}).strict().superRefine(workspaceInvariants);
|
||||
const WorkspaceV3Schema = z.object({
|
||||
dwh: dwhSchema,
|
||||
llm_policy: llmPolicySchema,
|
||||
@@ -521,7 +390,7 @@ const WorkspaceV3Schema = z.object({
|
||||
embedding: internalEmbeddingSchema,
|
||||
}).strict(),
|
||||
}).strict().superRefine(workspaceInvariants);
|
||||
const WorkspaceDescriptorSchema = z.union([WorkspaceV3Schema, WorkspaceV2Schema, WorkspaceV1Schema]);
|
||||
const WorkspaceDescriptorSchema = WorkspaceV3Schema;
|
||||
|
||||
export function parseWorkspaceYaml(source: string): WorkspaceDescriptor {
|
||||
const documents = parseAllDocuments(source, { uniqueKeys: true });
|
||||
@@ -538,46 +407,16 @@ export function validateWorkspaceDescriptor(workspace: unknown): WorkspaceDescri
|
||||
return WorkspaceDescriptorSchema.parse(workspace) as WorkspaceDescriptor;
|
||||
}
|
||||
|
||||
export function isCanonicalWorkspace(workspace: WorkspaceDescriptor): workspace is CanonicalWorkspace {
|
||||
return workspace.workspace.schema_version === 3;
|
||||
export function isCanonicalWorkspace(workspace: unknown): workspace is CanonicalWorkspace {
|
||||
return WorkspaceDescriptorSchema.safeParse(workspace).success;
|
||||
}
|
||||
|
||||
export function isOperationalWorkspace(workspace: WorkspaceDescriptor): workspace is WorkspaceV3 {
|
||||
return workspace.workspace.schema_version === 3;
|
||||
export function isOperationalWorkspace(workspace: unknown): workspace is WorkspaceV3 {
|
||||
return WorkspaceDescriptorSchema.safeParse(workspace).success;
|
||||
}
|
||||
|
||||
/** Rejects readable v1 descriptors at every operational boundary until a caller migrates them. */
|
||||
export function validateCanonicalWorkspace(workspace: unknown): CanonicalWorkspace {
|
||||
return validateOperationalWorkspace(workspace);
|
||||
}
|
||||
|
||||
/** Rejects readable v1/v2 descriptors at every operational boundary until a caller migrates them. */
|
||||
export function validateOperationalWorkspace(workspace: unknown): WorkspaceV3 {
|
||||
const descriptor = validateWorkspaceDescriptor(workspace);
|
||||
if (!isOperationalWorkspace(descriptor)) {
|
||||
throw new Error("Workspace descriptor requires explicit migration to schema version 3");
|
||||
}
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicitly upgrades a readable v1 descriptor. The caller must supply vector identity; the
|
||||
* transformer never derives it from DWH identity, even where both services share a database.
|
||||
*/
|
||||
export function migrateWorkspaceV1ToV2(
|
||||
workspace: WorkspaceV1,
|
||||
vectorIdentity: { database: string; schema: string },
|
||||
): WorkspaceV2 {
|
||||
const legacy = WorkspaceV1Schema.parse(workspace) as WorkspaceV1;
|
||||
const identity = z.object({ database: identifier, schema: identifier }).strict().parse(vectorIdentity);
|
||||
return WorkspaceV2Schema.parse({
|
||||
...legacy,
|
||||
workspace: { ...legacy.workspace, schema_version: 2 },
|
||||
semantic_index: {
|
||||
...legacy.semantic_index,
|
||||
vector_store: { ...legacy.semantic_index.vector_store, ...identity },
|
||||
},
|
||||
}) as WorkspaceV2;
|
||||
return validateWorkspaceDescriptor(workspace);
|
||||
}
|
||||
|
||||
/** Builds a request URL only after rejecting values that can leave the declared service origin. */
|
||||
|
||||
Reference in New Issue
Block a user