Files
devclaw-gitea/lib/audit.ts
Lauren ten Hoor a359ffed34 feat(migration): implement workspace layout migration and testing
- Added `migrate-layout.ts` to handle migration from old workspace layouts to the new `devclaw/` structure.
- Introduced `migrate-layout.test.ts` for comprehensive tests covering various migration scenarios.
- Updated `workspace.ts` to ensure default files are created post-migration, including `workflow.yaml` and role-specific prompts.
- Refactored role instruction handling to accommodate new directory structure.
- Enhanced project registration to scaffold prompt files in the new `devclaw/projects/<project>/prompts/` directory.
- Adjusted setup tool descriptions and logic to reflect changes in file structure.
- Updated templates to align with the new workflow configuration and role instructions.
2026-02-15 20:19:09 +08:00

49 lines
1.5 KiB
TypeScript

/**
* Append-only NDJSON audit logging.
* Every tool call automatically logs — no manual action needed from agents.
* Automatically truncates log to keep only last 250 lines.
*/
import { appendFile, mkdir, readFile, writeFile } from "node:fs/promises";
import { join, dirname } from "node:path";
import { DATA_DIR } from "./setup/migrate-layout.js";
const MAX_LOG_LINES = 50;
export async function log(
workspaceDir: string,
event: string,
data: Record<string, unknown>,
): Promise<void> {
const filePath = join(workspaceDir, DATA_DIR, "log", "audit.log");
const entry = JSON.stringify({
ts: new Date().toISOString(),
event,
...data,
});
try {
await appendFile(filePath, entry + "\n");
await truncateIfNeeded(filePath);
} catch (err: unknown) {
// If directory doesn't exist, create it and retry
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
await mkdir(dirname(filePath), { recursive: true });
await appendFile(filePath, entry + "\n");
}
// Audit logging should never break the tool — silently ignore other errors
}
}
async function truncateIfNeeded(filePath: string): Promise<void> {
try {
const content = await readFile(filePath, "utf-8");
const lines = content.split("\n").filter((line) => line.length > 0);
if (lines.length > MAX_LOG_LINES) {
const keptLines = lines.slice(-MAX_LOG_LINES);
await writeFile(filePath, keptLines.join("\n") + "\n", "utf-8");
}
} catch {
// Silently ignore truncation errors — log remains intact
}
}