/*
run-init — establishes integrity tracking for managed files by creating manifests and initial state.
- Do not perform file content parsing; focus solely on metadata and integrity tracking.
- Do not manage the orchestration of file transport or configuration settings.
- Do not handle user input validation beyond the initial policy file check.
- RFC-1097: step 6 — compass.migrate codemod run
Mechanical v1 to v2 header migration across the workspace: 942 files rewritten — CHANGE_SUMMARY windows collapsed into history, forbidden v1 blocks stripped, KEY_DECISIONS seeded from @ai-invariant comments (5 files) or TODO placeholders (103 files), blocks reordered to canonical order.
- RFC-1097: sweep — werkstatt-engine clean
Sweep batch 4: 73 Compass headers on headerless engine files (certification, component-runtime, isolation, evolution, testing), real KEY_DECISIONS on 75 files (kernel, cache, dht, swim, gitmesh, runtime), ~80 purpose expansions (CONTRACT-02/PURPOSE-02), non-goals on 13 CONTRACT-03 files, CS-07 history literal fix repo-wide (253 files). Policy: .template.ts/.template.astro excludedPaths. werkstatt-engine now 0 diagnostics.
*/
/**
* Initialize integrity tracking by registering all managed files.
* Creates manifests, entities registry, and path bindings from scratch.
*/
import path from "node:path";
import { byteHashFile } from "@warpgogol/werkstatt-engine/fingerprint";
import { discoverManagedFiles, groupFilesByDirectory } from "./discover.ts";
import { getFileHistory, getFileRevisionFromHistory } from "./git.ts";
import { writeJsonFile } from "./json.ts";
import {
createEmptyManifest,
getManifestFileName,
saveDirectoryManifest,
upsertManifestRecord,
} from "./manifests.ts";
import { deletedLogPath } from "./paths.ts";
import { saveEntitiesById, savePathsCurrent } from "./registry.ts";
import { v7 as uuidv7 } from "uuid";
import { ensurePolicyFile } from "./policy.ts";
import type { DirectoryManifest, EntitiesById, PathsCurrent } from "./types.ts";
export async function runInit(args: { cwd: string }): Promise {
const { cwd } = args;
await ensurePolicyFile(cwd);
const files = await discoverManagedFiles(cwd);
const grouped = await groupFilesByDirectory(files);
const manifests = new Map();
const entities: EntitiesById = {};
const paths: PathsCurrent = {};
for (const file of files) {
const absPath = path.join(cwd, file);
const entityId = uuidv7();
const hash = await byteHashFile(absPath);
const [history, revision] = await Promise.all([
getFileHistory(cwd, file),
getFileRevisionFromHistory(cwd, file),
]);
const now = new Date().toISOString();
const record = {
entityId,
createdAt: history.createdAt ?? now,
updatedAt: history.updatedAt ?? now,
revision,
contentHash: hash,
gitSha: history.lastCommitSha ?? "unknown",
status: "active" as const,
};
const repoDir = path.posix.dirname(file);
const fileName = getManifestFileName(file);
const manifest = manifests.get(repoDir) ?? createEmptyManifest(repoDir);
upsertManifestRecord(manifest, fileName, record);
manifests.set(repoDir, manifest);
entities[entityId] = {
currentPath: file,
firstPath: file,
createdAt: record.createdAt,
updatedAt: record.updatedAt,
revision: record.revision,
contentHash: record.contentHash,
gitSha: record.gitSha,
status: "active",
moves: [],
};
paths[file] = entityId;
}
for (const repoDir of grouped.keys()) {
const manifest = manifests.get(repoDir);
if (manifest) await saveDirectoryManifest(cwd, manifest);
}
await saveEntitiesById(cwd, entities);
await savePathsCurrent(cwd, paths);
await writeJsonFile(deletedLogPath(cwd), []);
console.log("");
console.log("Integrity init");
console.log(` managed files ${files.length}`);
console.log(` managed directories ${grouped.size}`);
console.log(` registered entities ${Object.keys(entities).length}`);
}