feat: atomic revision-qualified annotations sync with ownership manifest (P5)
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import {
|
||||
closeSync,
|
||||
constants as fsConstants,
|
||||
fchmodSync,
|
||||
fstatSync,
|
||||
fsyncSync,
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
openSync,
|
||||
readFileSync,
|
||||
renameSync,
|
||||
unlinkSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { dirname, isAbsolute, join } from "node:path";
|
||||
import { parseAnnotationsYaml } from "./annotations.js";
|
||||
|
||||
export interface AnnotationsSyncInput {
|
||||
dataRoot: string;
|
||||
workspaceId: string;
|
||||
commit: string;
|
||||
blobId: string;
|
||||
contents: Buffer;
|
||||
}
|
||||
|
||||
export interface AnnotationsSyncResult {
|
||||
path: string;
|
||||
manifestPath: string;
|
||||
contentDigest: string;
|
||||
}
|
||||
|
||||
interface AnnotationsOwnershipManifest {
|
||||
workspace: string;
|
||||
commit: string;
|
||||
blobId: string;
|
||||
contentDigest: string;
|
||||
destination: string;
|
||||
}
|
||||
|
||||
export function annotationsSyncRoot(dataRoot: string, workspaceId: string, commit: string): string {
|
||||
return join(dataRoot, "sessions", workspaceId, "revisions", commit, "artifacts");
|
||||
}
|
||||
|
||||
function sha256(value: Buffer | string): string {
|
||||
return `sha256:${createHash("sha256").update(value).digest("hex")}`;
|
||||
}
|
||||
|
||||
function ensureDirectory(path: string): void {
|
||||
mkdirSync(path, { recursive: true, mode: 0o700 });
|
||||
const entry = lstatSync(path);
|
||||
if (!entry.isDirectory() || entry.isSymbolicLink()) {
|
||||
throw new Error("annotations sync directory is unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
function syncDirectory(directory: string): void {
|
||||
if (process.platform === "win32") return;
|
||||
const fd = openSync(directory, "r");
|
||||
try { fsyncSync(fd); } finally { closeSync(fd); }
|
||||
}
|
||||
|
||||
function writeAtomicFile(path: string, contents: string | Buffer, mode: number): void {
|
||||
ensureDirectory(dirname(path));
|
||||
const staging = `${path}.tmp-${process.pid}-${Date.now()}-${randomBytes(6).toString("hex")}`;
|
||||
const fd = openSync(
|
||||
staging,
|
||||
fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_NOFOLLOW,
|
||||
0o600,
|
||||
);
|
||||
let closed = false;
|
||||
try {
|
||||
writeFileSync(fd, contents);
|
||||
fsyncSync(fd);
|
||||
fchmodSync(fd, mode);
|
||||
closeSync(fd);
|
||||
closed = true;
|
||||
renameSync(staging, path);
|
||||
syncDirectory(dirname(path));
|
||||
} catch (error) {
|
||||
if (!closed) try { closeSync(fd); } catch { /* preserve original failure */ }
|
||||
try { unlinkSync(staging); } catch { /* best effort */ }
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function readTrustedFile(path: string): Buffer {
|
||||
const entry = lstatSync(path);
|
||||
if (!entry.isFile() || entry.isSymbolicLink()) throw new Error("annotations sync file is invalid");
|
||||
const fd = openSync(path, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
|
||||
try {
|
||||
const before = fstatSync(fd);
|
||||
if (!before.isFile() || before.nlink !== 1) throw new Error("annotations sync file is invalid");
|
||||
const contents = readFileSync(fd);
|
||||
const after = fstatSync(fd);
|
||||
if (before.dev !== after.dev || before.ino !== after.ino || before.size !== after.size || before.nlink !== after.nlink) {
|
||||
throw new Error("annotations sync file changed while reading");
|
||||
}
|
||||
return contents;
|
||||
} finally {
|
||||
closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
function parseManifest(source: string): AnnotationsOwnershipManifest {
|
||||
const parsed = JSON.parse(source) as Record<string, unknown>;
|
||||
if (
|
||||
typeof parsed.workspace !== "string"
|
||||
|| typeof parsed.commit !== "string"
|
||||
|| typeof parsed.blobId !== "string"
|
||||
|| typeof parsed.contentDigest !== "string"
|
||||
|| typeof parsed.destination !== "string"
|
||||
) {
|
||||
throw new Error("annotations ownership manifest is invalid");
|
||||
}
|
||||
return parsed as unknown as AnnotationsOwnershipManifest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically synchronize a curated annotation blob to its immutable revision-qualified runtime
|
||||
* root and write an adjacent ownership manifest. Idempotent: an existing destination is re-verified
|
||||
* against the exact blob/digest and fails closed on any mismatch. Never follows symlinks.
|
||||
*/
|
||||
export function syncAnnotations(input: AnnotationsSyncInput): AnnotationsSyncResult {
|
||||
if (!isAbsolute(input.dataRoot)) throw new Error("annotations sync data root must be absolute");
|
||||
if (!/^[a-z][a-z0-9-]{2,62}$/.test(input.workspaceId) || input.workspaceId === "workspace-docs") {
|
||||
throw new Error("annotations sync workspace id is invalid");
|
||||
}
|
||||
if (!/^[0-9a-f]{40}$/.test(input.commit)) throw new Error("annotations sync commit is invalid");
|
||||
if (!/^[0-9a-f]{40}$/.test(input.blobId)) throw new Error("annotations sync blob id is invalid");
|
||||
parseAnnotationsYaml(input.contents.toString("utf8"));
|
||||
|
||||
const root = annotationsSyncRoot(input.dataRoot, input.workspaceId, input.commit);
|
||||
const directory = join(root, "mschema");
|
||||
const path = join(directory, "annotations.yaml");
|
||||
const manifestPath = join(directory, "annotations.ownership.json");
|
||||
const contentDigest = sha256(input.contents);
|
||||
const manifest: AnnotationsOwnershipManifest = {
|
||||
workspace: input.workspaceId,
|
||||
commit: input.commit,
|
||||
blobId: input.blobId,
|
||||
contentDigest,
|
||||
destination: path,
|
||||
};
|
||||
|
||||
let existingContents: Buffer | undefined;
|
||||
try {
|
||||
existingContents = readTrustedFile(path);
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
||||
existingContents = undefined;
|
||||
}
|
||||
|
||||
if (existingContents !== undefined) {
|
||||
if (sha256(existingContents) !== contentDigest) {
|
||||
throw new Error("annotations sync destination does not match the pinned revision");
|
||||
}
|
||||
const existingManifest = parseManifest(readTrustedFile(manifestPath).toString("utf8"));
|
||||
if (
|
||||
existingManifest.workspace !== manifest.workspace
|
||||
|| existingManifest.commit !== manifest.commit
|
||||
|| existingManifest.blobId !== manifest.blobId
|
||||
|| existingManifest.contentDigest !== manifest.contentDigest
|
||||
|| existingManifest.destination !== manifest.destination
|
||||
) {
|
||||
throw new Error("annotations ownership manifest does not match the pinned revision");
|
||||
}
|
||||
return { path, manifestPath, contentDigest };
|
||||
}
|
||||
|
||||
writeAtomicFile(path, input.contents, 0o400);
|
||||
writeAtomicFile(manifestPath, `${JSON.stringify(manifest)}\n`, 0o600);
|
||||
return { path, manifestPath, contentDigest };
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { mkdir, readdir, readFile, rename, rm, writeFile } from "node:fs/promise
|
||||
import { isAbsolute, join } from "node:path";
|
||||
import { buildInstallationContract, renderWorkspaceDocs } from "./contracts.js";
|
||||
import { parseAnnotationsYaml } from "./annotations.js";
|
||||
import { syncAnnotations } from "./annotations-sync.js";
|
||||
import { assertCatalogMatchesDescriptor, parseWorkspaceCatalogYaml, type WorkspaceCatalog, type WorkspaceCatalogEntry } from "./catalog.js";
|
||||
import {
|
||||
GitWorkspaceRepository,
|
||||
@@ -585,6 +586,15 @@ export class WorkspaceRegistry {
|
||||
const annotations = await this.repository.annotationsObject(safeHead, id);
|
||||
if (annotations !== undefined) {
|
||||
parseAnnotationsYaml(annotations.contents.toString("utf8"));
|
||||
if (this.config.dataRoot !== undefined) {
|
||||
syncAnnotations({
|
||||
dataRoot: this.config.dataRoot,
|
||||
workspaceId: id,
|
||||
commit: safeHead,
|
||||
blobId: annotations.blobId,
|
||||
contents: annotations.contents,
|
||||
});
|
||||
}
|
||||
}
|
||||
const collection = workspace.semantic_index.vector_store.collection;
|
||||
const owner = collectionOwners.get(collection);
|
||||
|
||||
@@ -8,6 +8,8 @@ export interface WorkspaceRegistryConfig {
|
||||
secretRoots: readonly string[];
|
||||
maxImportBytes: number;
|
||||
maxImportEntries: number;
|
||||
/** Absolute runtime data root; when set, activation also syncs curated annotations per revision. */
|
||||
dataRoot?: string;
|
||||
}
|
||||
|
||||
export type WorkspaceErrorCode =
|
||||
|
||||
Reference in New Issue
Block a user