codehere 2.0.4 → 2.1.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -14
- package/bin/codehere.js +11 -1
- package/package.json +1 -1
- package/src/audit.js +163 -0
- package/src/brain.js +298 -13
- package/src/commands.js +187 -65
- package/src/hooks.js +417 -64
- package/src/signals.js +49 -19
package/src/brain.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import os from 'node:os';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
+
import crypto from 'node:crypto';
|
|
6
7
|
import { execSync, execFileSync } from 'node:child_process';
|
|
7
8
|
|
|
8
9
|
export function findRoot(start = process.cwd()) {
|
|
@@ -26,13 +27,15 @@ export function paths(root) {
|
|
|
26
27
|
briefs: path.join(base, 'briefs'),
|
|
27
28
|
local: path.join(base, 'local'),
|
|
28
29
|
state: path.join(base, 'local', 'state.json'),
|
|
30
|
+
drafts: path.join(base, 'local', 'drafts'),
|
|
29
31
|
};
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
// No folder is made through a committed link (init refuses one outright).
|
|
32
35
|
export function ensureDirs(root) {
|
|
33
36
|
const p = paths(root);
|
|
34
37
|
for (const d of [p.base, p.lessons, p.help, p.briefs, p.local]) {
|
|
35
|
-
fs.mkdirSync(d, { recursive: true });
|
|
38
|
+
if (ownPath(root, d)) fs.mkdirSync(d, { recursive: true });
|
|
36
39
|
}
|
|
37
40
|
return p;
|
|
38
41
|
}
|
|
@@ -46,34 +49,79 @@ const DEFAULT_CONFIG = {
|
|
|
46
49
|
gemmaModel: 'gemma4:e4b',
|
|
47
50
|
};
|
|
48
51
|
|
|
52
|
+
// What a teammate can commit is read only as a plain file: git can commit a symlink (mode 120000),
|
|
53
|
+
// and one to /dev/zero would hang the hook that reads it. The file is opened without following a
|
|
54
|
+
// final link and without blocking, checked on the open file, and read up to max bytes: a linked
|
|
55
|
+
// folder can still reach a file such as /proc/self/pagemap, which says it is plain and never ends.
|
|
56
|
+
// encoding null gives a Buffer.
|
|
57
|
+
export function readPlain(file, encoding = 'utf8', max = 8 * 1024 * 1024) {
|
|
58
|
+
const notPlain = () => Object.assign(new Error(`not a plain file: ${file}`), { code: 'ENOTPLAIN' });
|
|
59
|
+
let fd;
|
|
60
|
+
try {
|
|
61
|
+
fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
throw e.code === 'ELOOP' ? notPlain() : e;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
if (!fs.fstatSync(fd).isFile()) throw notPlain();
|
|
67
|
+
const chunks = [];
|
|
68
|
+
const buf = Buffer.alloc(64 * 1024);
|
|
69
|
+
for (let total = 0, n; total < max && (n = fs.readSync(fd, buf, 0, Math.min(buf.length, max - total), null)) > 0; total += n) {
|
|
70
|
+
chunks.push(Buffer.from(buf.subarray(0, n)));
|
|
71
|
+
}
|
|
72
|
+
const data = Buffer.concat(chunks);
|
|
73
|
+
return encoding ? data.toString(encoding) : data;
|
|
74
|
+
} finally {
|
|
75
|
+
fs.closeSync(fd);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
49
79
|
export function readConfig(root) {
|
|
50
80
|
const p = paths(root);
|
|
51
81
|
try {
|
|
52
|
-
return { ...DEFAULT_CONFIG, ...JSON.parse(
|
|
82
|
+
return { ...DEFAULT_CONFIG, ...JSON.parse(readPlain(p.config)) };
|
|
53
83
|
} catch {
|
|
54
84
|
return { ...DEFAULT_CONFIG };
|
|
55
85
|
}
|
|
56
86
|
}
|
|
57
87
|
|
|
58
88
|
export function writeConfig(root, cfg) {
|
|
59
|
-
|
|
89
|
+
writePlain(root, paths(root).config, JSON.stringify(cfg, null, 2) + '\n');
|
|
60
90
|
}
|
|
61
91
|
|
|
62
92
|
// ---------- local session state ----------
|
|
63
93
|
|
|
64
94
|
export function readState(root) {
|
|
65
95
|
try {
|
|
66
|
-
const state = JSON.parse(
|
|
96
|
+
const state = JSON.parse(readPlain(paths(root).state));
|
|
67
97
|
// state.json can arrive through git: a file of the wrong shape counts as empty
|
|
68
98
|
if (state && state.sessions && typeof state.sessions === 'object') return state;
|
|
69
99
|
} catch {}
|
|
70
100
|
return { sessions: {}, lastSession: null, lastGreen: null, pendingBrief: null };
|
|
71
101
|
}
|
|
72
102
|
|
|
103
|
+
// Written to a temporary file and renamed into place: a reader never sees half a file (which
|
|
104
|
+
// readState would take as empty, erasing every session), and a committed symlink is replaced,
|
|
105
|
+
// not followed.
|
|
106
|
+
// Nothing is written when local/ is a committed symlink, and the temporary file has a random name
|
|
107
|
+
// made with exclusive create, so no planted link at that name is followed. Never throws: false when
|
|
108
|
+
// nothing was saved, and a caller that needs the state kept (the finish check's send-back count)
|
|
109
|
+
// then holds back, while what it reports still goes out.
|
|
73
110
|
export function writeState(root, state) {
|
|
74
111
|
const p = paths(root);
|
|
75
|
-
|
|
76
|
-
|
|
112
|
+
if (!ownPath(root, p.local)) return false;
|
|
113
|
+
const tmp = `${p.state}.${crypto.randomBytes(6).toString('hex')}.tmp`;
|
|
114
|
+
try {
|
|
115
|
+
fs.mkdirSync(p.local, { recursive: true });
|
|
116
|
+
fs.writeFileSync(tmp, JSON.stringify(state, null, 2), { flag: 'wx' });
|
|
117
|
+
fs.renameSync(tmp, p.state);
|
|
118
|
+
return true;
|
|
119
|
+
} catch {
|
|
120
|
+
try {
|
|
121
|
+
fs.rmSync(tmp, { force: true });
|
|
122
|
+
} catch {} // local/ may be a file: there is no temporary file to remove
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
77
125
|
}
|
|
78
126
|
|
|
79
127
|
export function session(state, id) {
|
|
@@ -128,10 +176,14 @@ export function listDocs(dir) {
|
|
|
128
176
|
.readdirSync(dir)
|
|
129
177
|
.filter((f) => f.endsWith('.md'))
|
|
130
178
|
.sort()
|
|
131
|
-
.
|
|
179
|
+
.flatMap((f) => {
|
|
132
180
|
const file = path.join(dir, f);
|
|
133
|
-
|
|
134
|
-
|
|
181
|
+
try {
|
|
182
|
+
const { meta, body } = parseDoc(readPlain(file));
|
|
183
|
+
return [{ id: f.replace(/\.md$/, ''), file, meta, body }];
|
|
184
|
+
} catch {
|
|
185
|
+
return []; // a link or a folder named like a lesson or card
|
|
186
|
+
}
|
|
135
187
|
});
|
|
136
188
|
}
|
|
137
189
|
|
|
@@ -159,9 +211,13 @@ export function today() {
|
|
|
159
211
|
|
|
160
212
|
export function readRules(root) {
|
|
161
213
|
const file = paths(root).rules;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
214
|
+
let text = '';
|
|
215
|
+
try {
|
|
216
|
+
text = readPlain(file);
|
|
217
|
+
} catch {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
return text
|
|
165
221
|
.split('\n')
|
|
166
222
|
.filter((l) => l.startsWith('- '))
|
|
167
223
|
.map((l) => l.slice(2).trim());
|
|
@@ -334,7 +390,7 @@ export function gitHashes(root, files) {
|
|
|
334
390
|
// A tick is Codehere's mark for a run it made on this machine (DESIGN.md §1). Text from the
|
|
335
391
|
// repo or from a teammate never brings its own.
|
|
336
392
|
export function noTick(text, mark = '?') {
|
|
337
|
-
return String(text).replace(/[
|
|
393
|
+
return String(text).replace(/[✓✔☑✅√🗸🗹\u{10102}\u{1FBB1}]/gu, mark);
|
|
338
394
|
}
|
|
339
395
|
|
|
340
396
|
// A commit id read back from state.json, or null. The state file can arrive through git,
|
|
@@ -350,3 +406,232 @@ export function isGitRepo(root) {
|
|
|
350
406
|
export function me(root) {
|
|
351
407
|
return git(root, 'config user.name') || process.env.USER || 'someone';
|
|
352
408
|
}
|
|
409
|
+
|
|
410
|
+
// ---------- records: what rule, lesson, help and reply save ----------
|
|
411
|
+
// A writing command builds its record once. Typed by the developer it is saved at once; run by an
|
|
412
|
+
// agent it becomes a draft, which the prompt hook saves only after the person's plain yes.
|
|
413
|
+
|
|
414
|
+
// True when no part of p below root is a symlink, so a write to p lands at p. A teammate can
|
|
415
|
+
// commit a symlink anywhere in .codehere/, dangling or not; git writes it on pull.
|
|
416
|
+
export function ownPath(root, p) {
|
|
417
|
+
let at = path.resolve(root);
|
|
418
|
+
for (const part of path.relative(at, path.resolve(p)).split(path.sep)) {
|
|
419
|
+
if (!part || part === '..') return false;
|
|
420
|
+
at = path.join(at, part);
|
|
421
|
+
try {
|
|
422
|
+
if (fs.lstatSync(at).isSymbolicLink()) return false;
|
|
423
|
+
} catch (e) {
|
|
424
|
+
return e.code === 'ENOENT'; // from here on nothing exists yet, and mkdir makes plain folders
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// A write into the repo that a committed symlink cannot redirect: no link on the path (the final
|
|
431
|
+
// name refused without a race by O_NOFOLLOW). mode 'w' replaces, 'a' appends.
|
|
432
|
+
const linkedError = (root, file) => Object.assign(new Error(`${path.relative(root, file)} is a link or under one, so Codehere will not write it.`), { code: 'ENOTPLAIN' });
|
|
433
|
+
export function writePlain(root, file, data, mode = 'w') {
|
|
434
|
+
if (!ownPath(root, file)) throw linkedError(root, file);
|
|
435
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
436
|
+
const { O_WRONLY, O_CREAT, O_TRUNC, O_APPEND, O_NOFOLLOW } = fs.constants;
|
|
437
|
+
const fd = fs.openSync(file, O_WRONLY | O_CREAT | O_NOFOLLOW | (mode === 'a' ? O_APPEND : O_TRUNC), 0o666); // the umask applies, as with writeFileSync
|
|
438
|
+
try {
|
|
439
|
+
fs.writeFileSync(fd, data);
|
|
440
|
+
} finally {
|
|
441
|
+
fs.closeSync(fd);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
export function removePlain(root, file) {
|
|
445
|
+
if (!ownPath(root, file)) throw linkedError(root, file);
|
|
446
|
+
fs.rmSync(file, { force: true });
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// A new file, never over an existing one or through a link: name.md, then name-2.md, ...
|
|
450
|
+
// 'wx' fails on any existing path, a dangling symlink included, and on a race with another save.
|
|
451
|
+
function writeNew(dir, name, text) {
|
|
452
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
453
|
+
for (let i = 1; ; i++) {
|
|
454
|
+
const base = i === 1 ? name : `${name}-${i}`;
|
|
455
|
+
try {
|
|
456
|
+
fs.writeFileSync(path.join(dir, `${base}.md`), text(base), { flag: 'wx' });
|
|
457
|
+
return path.join(dir, `${base}.md`);
|
|
458
|
+
} catch (e) {
|
|
459
|
+
if (e.code !== 'EEXIST') throw e;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const day = (d) => (/^\d{4}-\d\d-\d\d$/.test(String(d)) ? d : today());
|
|
465
|
+
const line = (s) => String(s).replace(/\s*[\r\n]+\s*/g, ' ');
|
|
466
|
+
// The front matter a record may set. Proof fields come only from the finish check's proof, never
|
|
467
|
+
// from a record: a draft is a file the agent could have edited.
|
|
468
|
+
const LESSON_KEYS = ['title', 'signature', 'author', 'date', 'test', 'from'];
|
|
469
|
+
const HELP_KEYS = ['id', 'author', 'date', 'status', 'signature', 'test'];
|
|
470
|
+
const PROOF_KEYS = ['signature', 'test', 'state', 'proof_command', 'proof_exit', 'proof_commit', 'proof_dirty', 'files', 'file_hashes'];
|
|
471
|
+
const pick = (obj, keys) => Object.fromEntries(keys.filter((k) => obj && obj[k] != null).map((k) => [k, line(obj[k])]));
|
|
472
|
+
const isText = (s) => typeof s === 'string';
|
|
473
|
+
|
|
474
|
+
// The folder comes from the kind and the file name is built here from slugs and a checked date,
|
|
475
|
+
// so no record field becomes a path. Never throws or exits: what cannot be saved comes back as
|
|
476
|
+
// { error }, for the CLI to print and for a hook to tell the agent.
|
|
477
|
+
export function saveRecord(root, rec, proof = null) {
|
|
478
|
+
try {
|
|
479
|
+
return save(root, rec || {}, proof);
|
|
480
|
+
} catch (e) {
|
|
481
|
+
return { error: `Codehere could not save it: ${e.message}` };
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export const isMap = (x) => !!x && typeof x === 'object' && !Array.isArray(x);
|
|
486
|
+
|
|
487
|
+
// Lessons and help cards as hashes by path, for the finish check: one the agent writes itself is
|
|
488
|
+
// listed for the person. A link is not followed, and a linked folder is not read: it stands for
|
|
489
|
+
// itself, so swapping a folder for a link is a change too.
|
|
490
|
+
export function teamSnapshot(root) {
|
|
491
|
+
const snap = {};
|
|
492
|
+
for (const d of ['.codehere/lessons', '.codehere/help']) {
|
|
493
|
+
const dir = path.join(root, d);
|
|
494
|
+
if (!ownPath(root, dir)) {
|
|
495
|
+
let target = '';
|
|
496
|
+
try {
|
|
497
|
+
target = fs.readlinkSync(dir);
|
|
498
|
+
} catch {}
|
|
499
|
+
snap[d] = `link:${target}`;
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
let names = [];
|
|
503
|
+
try {
|
|
504
|
+
names = fs.readdirSync(dir);
|
|
505
|
+
} catch {}
|
|
506
|
+
for (const n of names) {
|
|
507
|
+
const f = path.join(dir, n);
|
|
508
|
+
try {
|
|
509
|
+
const st = fs.lstatSync(f);
|
|
510
|
+
const text = st.isSymbolicLink() ? `link:${fs.readlinkSync(f)}` : st.isFile() ? readPlain(f, null) : null;
|
|
511
|
+
if (text != null) snap[`${d}/${n}`] = crypto.createHash('sha256').update(text).digest('hex');
|
|
512
|
+
} catch {}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return snap;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Before a save: the team files' hashes, and which files git sees as changed.
|
|
519
|
+
export function teamMark(root) {
|
|
520
|
+
return { hashes: teamSnapshot(root), dirty: new Set(statusFiles(root).map((f) => f.normalize('NFC'))) };
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// A save Codehere made is not the agent's change: what it changed moves into the session's
|
|
524
|
+
// snapshot, unless that file had an uncommitted change made earlier in the session, which stays
|
|
525
|
+
// visible. A change that came committed (a pull) is not the agent's. True if anything moved.
|
|
526
|
+
export function keepSaved(s, mark, after) {
|
|
527
|
+
let moved = false;
|
|
528
|
+
if (!s || !isMap(s.team)) return moved;
|
|
529
|
+
const before = mark.hashes;
|
|
530
|
+
for (const f of new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
531
|
+
if (before[f] === after[f] || (s.team[f] !== before[f] && mark.dirty.has(f.normalize('NFC')))) continue;
|
|
532
|
+
if (after[f] === undefined) delete s.team[f];
|
|
533
|
+
else s.team[f] = after[f];
|
|
534
|
+
moved = true;
|
|
535
|
+
}
|
|
536
|
+
return moved;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function save(root, rec, proof) {
|
|
540
|
+
const p = paths(root);
|
|
541
|
+
const rel = (f) => path.relative(root, f);
|
|
542
|
+
const linked = (f) => (ownPath(root, f) ? null : { error: `Codehere: ${rel(f)} is a link, so nothing was saved.` });
|
|
543
|
+
const bad = { error: 'Codehere: this draft is not one Codehere wrote, so nothing was saved.' };
|
|
544
|
+
if (rec.kind === 'rule') {
|
|
545
|
+
if (!isText(rec.line) || !rec.line.startsWith('- ')) return bad;
|
|
546
|
+
if (linked(p.rules)) return linked(p.rules);
|
|
547
|
+
fs.appendFileSync(p.rules, `${line(rec.line)}\n`);
|
|
548
|
+
return { file: rel(p.rules) };
|
|
549
|
+
}
|
|
550
|
+
if (rec.kind === 'lesson') {
|
|
551
|
+
// with a proof, the error's signature and failing test come from it alone, present or not
|
|
552
|
+
const meta = { ...pick(rec.meta, proof ? LESSON_KEYS.filter((k) => k !== 'signature' && k !== 'test') : LESSON_KEYS), ...pick(proof, PROOF_KEYS) };
|
|
553
|
+
if (!meta.title || !isText(rec.body)) return bad;
|
|
554
|
+
if (linked(p.lessons)) return linked(p.lessons);
|
|
555
|
+
return { file: rel(writeNew(p.lessons, `${day(meta.date)}-${slug(meta.title)}`, () => renderDoc(meta, rec.body))) };
|
|
556
|
+
}
|
|
557
|
+
if (rec.kind === 'help') {
|
|
558
|
+
const meta = pick(rec.meta, HELP_KEYS);
|
|
559
|
+
if (!meta.id || !isText(rec.text)) return bad;
|
|
560
|
+
if (linked(p.help)) return linked(p.help);
|
|
561
|
+
return { file: rel(writeNew(p.help, slug(meta.id, 80), (id) => renderDoc({ ...meta, id, status: 'open' }, rec.text))) };
|
|
562
|
+
}
|
|
563
|
+
if (rec.kind !== 'reply' || !isText(rec.card) || !isText(rec.line) || !rec.line.startsWith('- ')) return bad;
|
|
564
|
+
const lessonMeta = rec.lesson ? pick(rec.lesson.meta, LESSON_KEYS) : null;
|
|
565
|
+
if (rec.lesson && (!lessonMeta.title || !isText(rec.lesson.body))) return bad;
|
|
566
|
+
// a reply goes into the card as it is now (a pull may have added replies since the draft),
|
|
567
|
+
// and only into the card the draft named
|
|
568
|
+
const d = listDocs(p.help).find((x) => x.id === rec.card);
|
|
569
|
+
if (!d) return { error: `No help card "${rec.card}" any more, so the reply was not saved.` };
|
|
570
|
+
if (linked(d.file) || (rec.lesson && linked(p.lessons))) return linked(d.file) || linked(p.lessons);
|
|
571
|
+
const { meta, body } = parseDoc(readPlain(d.file));
|
|
572
|
+
let lesson = null;
|
|
573
|
+
if (rec.lesson) {
|
|
574
|
+
meta.status = 'resolved';
|
|
575
|
+
meta.resolvedBy = lessonMeta.author;
|
|
576
|
+
lesson = rel(writeNew(p.lessons, `${day(lessonMeta.date)}-${slug(lessonMeta.title)}`, () => renderDoc(lessonMeta, rec.lesson.body)));
|
|
577
|
+
}
|
|
578
|
+
const replies = body.includes('## Replies') ? body.trimEnd() : `${body.trimEnd()}\n\n## Replies`;
|
|
579
|
+
fs.writeFileSync(d.file, renderDoc(meta, `${replies}\n${line(rec.line)}\n`));
|
|
580
|
+
return { file: rel(d.file), lesson, author: meta.author };
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// null when .codehere/local/drafts/ is not a plain folder in this repo: a committed symlink must
|
|
584
|
+
// not move drafts elsewhere, nor let expiry delete files there; a committed file must not be mistaken
|
|
585
|
+
// for the folder.
|
|
586
|
+
function draftsHere(root) {
|
|
587
|
+
const dir = paths(root).drafts;
|
|
588
|
+
if (!ownPath(root, dir)) return null;
|
|
589
|
+
try {
|
|
590
|
+
return fs.statSync(dir).isDirectory() ? dir : null;
|
|
591
|
+
} catch (e) {
|
|
592
|
+
return e.code === 'ENOENT' ? dir : null;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// The id is new each time, so a draft file a teammate planted through git is never reused.
|
|
597
|
+
export function writeDraft(root, rec) {
|
|
598
|
+
const dir = draftsHere(root);
|
|
599
|
+
if (!dir) return null;
|
|
600
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
601
|
+
for (;;) {
|
|
602
|
+
const id = crypto.randomBytes(4).toString('hex');
|
|
603
|
+
try {
|
|
604
|
+
fs.writeFileSync(path.join(dir, `${id}.json`), JSON.stringify({ id, created: new Date().toISOString(), ...rec }, null, 2) + '\n', { flag: 'wx' });
|
|
605
|
+
return id;
|
|
606
|
+
} catch (e) {
|
|
607
|
+
if (e.code !== 'EEXIST') throw e;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
export function draftFile(root, id) {
|
|
613
|
+
const dir = draftsHere(root);
|
|
614
|
+
return dir && /^[0-9a-f]{8}$/.test(String(id)) ? path.join(dir, `${id}.json`) : null;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Runs at every session start, so it never throws: whatever a teammate committed under drafts/,
|
|
618
|
+
// session start goes on. Only plain files named like drafts are removed.
|
|
619
|
+
export function expireDrafts(root, days = 7) {
|
|
620
|
+
try {
|
|
621
|
+
const dir = draftsHere(root);
|
|
622
|
+
if (!dir || !fs.existsSync(dir) || !fs.lstatSync(dir).isDirectory()) return;
|
|
623
|
+
const cutoff = Date.now() - days * 864e5;
|
|
624
|
+
for (const f of fs.readdirSync(dir).filter((x) => /^[0-9a-f]{8}\.json$/.test(x))) {
|
|
625
|
+
const file = path.join(dir, f);
|
|
626
|
+
try {
|
|
627
|
+
if (!fs.lstatSync(file).isFile()) continue;
|
|
628
|
+
// the older of its own date and the file's: a date written into the file cannot keep it forever
|
|
629
|
+
let created = NaN;
|
|
630
|
+
try {
|
|
631
|
+
created = Date.parse(JSON.parse(fs.readFileSync(file, 'utf8')).created);
|
|
632
|
+
} catch {}
|
|
633
|
+
if (!(Math.min(created, fs.lstatSync(file).mtimeMs) >= cutoff)) fs.rmSync(file, { force: true });
|
|
634
|
+
} catch {}
|
|
635
|
+
}
|
|
636
|
+
} catch {}
|
|
637
|
+
}
|