/*
RFC-0565: Config loading, validation, and creation for werkstatt.dht.json.
The config file is workshop-local (gitignored) and created by dht.node.init.
It stores the DHT bind address, bootstrap nodes, replication factor, and
timeout parameters.
- Do not implement DHT routing logic — that lives in node.ts.
- Do not implement command handlers — those live in init.ts, lookup.ts, etc.
- Config is created with sane defaults when absent — the node is usable without manual setup.
- RFC-0565: initial implementation — config loading, validation, and creation.
- 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.
*/
import { join } from "node:path";
import { dhtConfigSchema, type DHTConfig } from "./types.ts";
import { createDefaultIO, type WorkspaceIO } from "@warpgogol/werkstatt-shared/kernel/workspace-io";
const CONFIG_FILENAME = "werkstatt.dht.json";
const DEFAULT_BIND_ADDR = "0.0.0.0:7947";
const DEFAULT_REPLICATION_FACTOR = 5;
const DEFAULT_LOOKUP_TIMEOUT_MS = 5000;
const DEFAULT_CACHE_TTL_MS = 300000;
export function validateDhtConfig(config: unknown): DHTConfig {
return dhtConfigSchema.parse(config);
}
export async function loadDhtConfig(
workspaceRoot: string,
io: WorkspaceIO = createDefaultIO().io,
): Promise {
const configPath = join(workspaceRoot, CONFIG_FILENAME);
const raw = await io.readFile(configPath);
const parsed: unknown = JSON.parse(raw);
return validateDhtConfig(parsed);
}
export async function createDhtConfig(
workspaceRoot: string,
options: { bind?: string; bootstrap?: string[] },
io: WorkspaceIO = createDefaultIO().io,
): Promise {
const config: DHTConfig = {
bindAddr: options.bind ?? DEFAULT_BIND_ADDR,
bootstrapNodes: options.bootstrap ?? [],
replicationFactor: DEFAULT_REPLICATION_FACTOR,
lookupTimeoutMs: DEFAULT_LOOKUP_TIMEOUT_MS,
cacheTtlMs: DEFAULT_CACHE_TTL_MS,
};
const validated = validateDhtConfig(config);
const configPath = join(workspaceRoot, CONFIG_FILENAME);
await io.writeFile(configPath, JSON.stringify(validated, null, 2) + "\n");
return validated;
}
export async function loadOrCreateDhtConfig(
workspaceRoot: string,
options?: { bind?: string; bootstrap?: string[] },
): Promise {
try {
return await loadDhtConfig(workspaceRoot);
} catch {
if (!options) {
throw new Error(`dht config: no ${CONFIG_FILENAME} found and no options provided`);
}
return await createDhtConfig(workspaceRoot, options);
}
}
export { CONFIG_FILENAME };