refactor: remove workspace migration utilities
This commit is contained in:
@@ -1,213 +0,0 @@
|
||||
import { lstat, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
||||
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { parseAllDocuments, stringify } from "yaml";
|
||||
import { parseWorkspaceYaml, validateOperationalWorkspace, type WorkspaceDescriptor, type WorkspaceV3 } from "./schema.js";
|
||||
|
||||
export interface LegacyMigrationResult {
|
||||
source: string;
|
||||
workspace: WorkspaceV3;
|
||||
}
|
||||
|
||||
export interface LegacyMigrationOptions {
|
||||
/** Immutable repository identifier, normally derived from the input filename by the CLI. */
|
||||
id: string;
|
||||
/** Required Qdrant collection name for the migrated schema-v3 descriptor. */
|
||||
collection: string;
|
||||
}
|
||||
|
||||
type LegacyRecord = Record<string, unknown>;
|
||||
|
||||
const workspaceId = /^[a-z][a-z0-9-]{2,62}$/;
|
||||
const identifier = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
||||
|
||||
function record(value: unknown): LegacyRecord | undefined {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value)
|
||||
? value as LegacyRecord
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function literalIdentifier(value: unknown): string | undefined {
|
||||
return typeof value === "string" && identifier.test(value) ? value : undefined;
|
||||
}
|
||||
|
||||
function literalText(value: unknown): string | undefined {
|
||||
return typeof value === "string" && value.trim() === value && value.length > 0 && !value.includes("${")
|
||||
? value
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function literalPort(value: unknown): number | undefined {
|
||||
if (typeof value === "number" && Number.isInteger(value) && value > 0 && value <= 65_535) return value;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function titleFor(id: string): string {
|
||||
return id.split("-").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
||||
}
|
||||
|
||||
function sourceDocument(source: string): LegacyRecord {
|
||||
const documents = parseAllDocuments(source, { uniqueKeys: true });
|
||||
if (documents.length !== 1 || documents[0].errors.length > 0) {
|
||||
throw new Error("legacy workspace YAML must contain exactly one valid document");
|
||||
}
|
||||
const parsed = record(documents[0].toJSON());
|
||||
if (!parsed) throw new Error("legacy workspace YAML must contain an object");
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function dwhFrom(source: LegacyRecord): { section: LegacyRecord; transport: "postgres_direct" | "rest_api" } {
|
||||
const dwh = record(source.dwh);
|
||||
const database = record(source.database);
|
||||
if (dwh) {
|
||||
const type = literalText(dwh.type);
|
||||
return { section: record(dwh.connection) ?? record(dwh.database) ?? dwh, transport: type === "postgres_direct" ? "postgres_direct" : "rest_api" };
|
||||
}
|
||||
if (database) {
|
||||
return { section: database, transport: literalText(database.transport) === "direct" ? "postgres_direct" : "rest_api" };
|
||||
}
|
||||
return { section: {}, transport: "rest_api" };
|
||||
}
|
||||
|
||||
function vectorFrom(source: LegacyRecord): {
|
||||
section: LegacyRecord; transport: "pgvector_direct" | "rest_api"; writer: boolean;
|
||||
} {
|
||||
const vectors = record(source.vectors);
|
||||
if (vectors) {
|
||||
const type = literalText(vectors.type);
|
||||
const direct = record(vectors.direct);
|
||||
return {
|
||||
section: type === "pgvector_direct" ? record(vectors.connection) ?? record(vectors.reader) ?? direct ?? {} : direct ?? {},
|
||||
transport: type === "pgvector_direct" ? "pgvector_direct" : "rest_api",
|
||||
writer: record(vectors.writer) !== undefined,
|
||||
};
|
||||
}
|
||||
const vectorDb = record(source.vector_db);
|
||||
return { section: vectorDb ?? {}, transport: "pgvector_direct", writer: record(source.vector_write_rest) !== undefined };
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a legacy runtime descriptor into a schema-v3 registry descriptor.
|
||||
* Runtime YAMLs mix shared metadata with `${ENV}` bindings and omit internal semantic identity,
|
||||
* so the operator must explicitly choose the target Qdrant collection during migration.
|
||||
*/
|
||||
export function migrateLegacyWorkspace(source: string, options: LegacyMigrationOptions): LegacyMigrationResult {
|
||||
if (!workspaceId.test(options.id)) throw new Error("legacy workspace ID is invalid");
|
||||
const collection = typeof options.collection === "string" && workspaceId.test(options.collection)
|
||||
? options.collection
|
||||
: undefined;
|
||||
if (collection === undefined) throw new Error("legacy migration requires an explicit target collection");
|
||||
const legacy = sourceDocument(source);
|
||||
const language = legacy.language === "it" ? "it" : "en";
|
||||
const { section: dwh, transport: dwhTransport } = dwhFrom(legacy);
|
||||
const { section: vector } = vectorFrom(legacy);
|
||||
const embedding = record(legacy.embeddings) ?? {};
|
||||
const dwhDatabase = literalIdentifier(dwh.database) ?? "legacy_dwh";
|
||||
const dwhSchema = literalIdentifier(dwh.schema) ?? "public";
|
||||
void vector;
|
||||
void embedding;
|
||||
const workspace = validateOperationalWorkspace({
|
||||
workspace: {
|
||||
schema_version: 3,
|
||||
id: options.id,
|
||||
name: titleFor(options.id),
|
||||
language,
|
||||
},
|
||||
dwh: {
|
||||
engine: "postgres",
|
||||
database: dwhDatabase,
|
||||
schema: dwhSchema,
|
||||
supported_transports: [dwhTransport],
|
||||
...(literalPort(dwh.port) === undefined ? {} : { port: literalPort(dwh.port) }),
|
||||
},
|
||||
semantic_index: {
|
||||
vector_store: {
|
||||
engine: "qdrant",
|
||||
collection,
|
||||
dimensions: 1024,
|
||||
distance: "cosine",
|
||||
},
|
||||
embedding: {
|
||||
provider: "ollama_internal",
|
||||
model: "qwen3-embedding:0.6b",
|
||||
dimensions: 1024,
|
||||
},
|
||||
},
|
||||
llm_policy: { allowed: ["zai/glm-5.2"] },
|
||||
});
|
||||
const rendered = stringify(workspace, { lineWidth: 0, sortMapEntries: true });
|
||||
return { source: rendered, workspace };
|
||||
}
|
||||
|
||||
function destinationFor(repositoryRoot: string, id: string): string {
|
||||
if (!isAbsolute(repositoryRoot)) throw new Error("migration output root must be absolute");
|
||||
if (!workspaceId.test(id)) throw new Error("legacy workspace ID is invalid");
|
||||
return join(repositoryRoot, "workspaces", `${id}.yaml`);
|
||||
}
|
||||
|
||||
/** Safely adds a migrated descriptor without replacing a previous operator-reviewed migration. */
|
||||
export async function writeMigratedWorkspace(result: LegacyMigrationResult, repositoryRoot: string): Promise<string> {
|
||||
const destination = destinationFor(repositoryRoot, result.workspace.workspace.id);
|
||||
const directory = dirname(destination);
|
||||
await mkdir(directory, { recursive: true, mode: 0o700 });
|
||||
try {
|
||||
await lstat(destination);
|
||||
throw new Error("migrated workspace already exists");
|
||||
} catch (error) {
|
||||
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") throw error;
|
||||
}
|
||||
const temporary = join(directory, `.${result.workspace.workspace.id}.${process.pid}.${Date.now()}.tmp`);
|
||||
try {
|
||||
await writeFile(temporary, result.source, { encoding: "utf8", mode: 0o600, flag: "wx" });
|
||||
await rename(temporary, destination);
|
||||
} catch (error) {
|
||||
await rm(temporary, { force: true });
|
||||
throw error;
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
|
||||
function parseCliArguments(argv: readonly string[]): { input: string; output: string; id?: string; collection: string } {
|
||||
if (argv.length !== 6 && argv.length !== 8) {
|
||||
throw new Error("usage: migrate-legacy --input <legacy-workspace.yaml> --output <repository-root> --collection <qdrant-collection> [--id <workspace-id>]");
|
||||
}
|
||||
const options = new Map<string, string>();
|
||||
for (let index = 0; index < argv.length; index += 2) {
|
||||
const flag = argv[index];
|
||||
const value = argv[index + 1];
|
||||
if ((flag !== "--input" && flag !== "--output" && flag !== "--id" && flag !== "--collection") || value === undefined || options.has(flag)) {
|
||||
throw new Error("usage: migrate-legacy --input <legacy-workspace.yaml> --output <repository-root> --collection <qdrant-collection> [--id <workspace-id>]");
|
||||
}
|
||||
options.set(flag, value);
|
||||
}
|
||||
const input = options.get("--input");
|
||||
const output = options.get("--output");
|
||||
const id = options.get("--id");
|
||||
const collection = options.get("--collection");
|
||||
if (input === undefined || output === undefined || collection === undefined) {
|
||||
throw new Error("usage: migrate-legacy --input <legacy-workspace.yaml> --output <repository-root> --collection <qdrant-collection> [--id <workspace-id>]");
|
||||
}
|
||||
if (!isAbsolute(input) || !isAbsolute(output)) {
|
||||
throw new Error("migration input and output paths must be absolute");
|
||||
}
|
||||
if (id !== undefined && !workspaceId.test(id)) throw new Error("legacy workspace ID is invalid");
|
||||
if (!workspaceId.test(collection)) {
|
||||
throw new Error("legacy migration requires an explicit target collection");
|
||||
}
|
||||
return { input, output, id, collection };
|
||||
}
|
||||
|
||||
export async function main(argv = process.argv.slice(2)): Promise<void> {
|
||||
const { input, output, id: explicitId, collection } = parseCliArguments(argv);
|
||||
const id = explicitId ?? basename(input, ".yaml");
|
||||
const result = migrateLegacyWorkspace(await readFile(input, "utf8"), { id, collection });
|
||||
const destination = await writeMigratedWorkspace(result, output);
|
||||
process.stdout.write(`${destination}\n`);
|
||||
}
|
||||
|
||||
if (process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||||
main().catch((error: unknown) => {
|
||||
process.stderr.write(`${error instanceof Error ? error.message : "migration failed"}\n`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import {
|
||||
validateOperationalWorkspace,
|
||||
type CanonicalDiagnostics,
|
||||
type DwhTransport,
|
||||
type WorkspaceV3,
|
||||
} from "./schema.js";
|
||||
|
||||
/** Legacy input exists only at the migration boundary and is never an accepted runtime descriptor. */
|
||||
interface WorkspaceV2MigrationInput {
|
||||
workspace: {
|
||||
schema_version: 2;
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
language: "en" | "it";
|
||||
};
|
||||
dwh: {
|
||||
engine: "postgres";
|
||||
database: string;
|
||||
schema: string;
|
||||
port?: number;
|
||||
timeout_ms?: number;
|
||||
supported_transports: DwhTransport[];
|
||||
};
|
||||
semantic_index: unknown;
|
||||
llm_policy: {
|
||||
default?: `${string}/${string}`;
|
||||
allowed: `${string}/${string}`[];
|
||||
};
|
||||
diagnostics?: CanonicalDiagnostics;
|
||||
}
|
||||
|
||||
export function migrateWorkspaceV2ToV3(
|
||||
legacy: WorkspaceV2MigrationInput,
|
||||
collection: string,
|
||||
): WorkspaceV3 {
|
||||
return validateOperationalWorkspace({
|
||||
workspace: { ...legacy.workspace, schema_version: 3 },
|
||||
dwh: legacy.dwh,
|
||||
semantic_index: {
|
||||
vector_store: {
|
||||
engine: "qdrant",
|
||||
collection,
|
||||
dimensions: 1024,
|
||||
distance: "cosine",
|
||||
},
|
||||
embedding: {
|
||||
provider: "ollama_internal",
|
||||
model: "qwen3-embedding:0.6b",
|
||||
dimensions: 1024,
|
||||
},
|
||||
},
|
||||
llm_policy: legacy.llm_policy,
|
||||
...(legacy.diagnostics?.dwh_rest
|
||||
? { diagnostics: { dwh_rest: legacy.diagnostics.dwh_rest } }
|
||||
: {}),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user