forked from cobusgreyling/loop-engineering
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappend-run-log.mjs
More file actions
48 lines (41 loc) · 1.43 KB
/
Copy pathappend-run-log.mjs
File metadata and controls
48 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env node
/**
* Append one JSON run entry to loop-run-log.md and prune entries older than 30 days.
* Usage: node scripts/append-run-log.mjs '<json-object>' [path-to-log]
*/
import { readFile, writeFile } from 'node:fs/promises';
const MARKER = '<!-- Loop appends below this line -->';
const MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
const entryJson = process.argv[2];
const logPath = process.argv[3] || 'loop-run-log.md';
if (!entryJson) {
console.error('Usage: node scripts/append-run-log.mjs \'<json>\' [loop-run-log.md]');
process.exit(1);
}
const entry = JSON.parse(entryJson);
const content = await readFile(logPath, 'utf8');
const markerAt = content.indexOf(MARKER);
if (markerAt === -1) {
console.error(`Marker not found in ${logPath}`);
process.exit(1);
}
const before = content.slice(0, markerAt + MARKER.length);
const after = content.slice(markerAt + MARKER.length);
const now = Date.now();
const kept = [];
for (const line of after.split('\n')) {
const trimmed = line.trim();
if (!trimmed.startsWith('{')) continue;
try {
const obj = JSON.parse(trimmed);
const t = new Date(obj.run_id).getTime();
if (!Number.isNaN(t) && now - t <= MAX_AGE_MS) {
kept.push(trimmed);
}
} catch {
// skip malformed lines
}
}
kept.push(JSON.stringify(entry));
await writeFile(logPath, `${before}\n\n${kept.join('\n')}\n`);
console.log(`Appended run ${entry.run_id} (${kept.length} entries within 30d window)`);