codehere 2.1.0-rc.2 → 2.1.0-rc.4
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 +7 -6
- package/package.json +2 -2
- package/src/audit.js +64 -21
- package/src/brain.js +172 -47
- package/src/commands.js +24 -2
- package/src/gitpath.js +47 -0
- package/src/hooks.js +489 -45
- package/src/noskip.js +590 -0
- package/src/signals.js +651 -23
package/src/hooks.js
CHANGED
|
@@ -2,17 +2,20 @@
|
|
|
2
2
|
// Every handler fails open: if anything goes wrong, it exits 0 and the agent carries on as if
|
|
3
3
|
// Codehere were not installed.
|
|
4
4
|
import fs from 'node:fs';
|
|
5
|
+
import os from 'node:os';
|
|
5
6
|
import path from 'node:path';
|
|
6
7
|
import crypto from 'node:crypto';
|
|
7
8
|
import { spawnSync } from 'node:child_process';
|
|
8
9
|
import {
|
|
9
10
|
findRoot, paths, readConfig, readState, writeState, session, readRules, renderDoc,
|
|
10
|
-
listDocs, git,
|
|
11
|
-
removedAt, expireDrafts, draftFile, saveRecord, readPlain, ownPath, teamSnapshot, teamMark, keepSaved, isMap,
|
|
11
|
+
listDocs, git, gitRead, gitPaths, isGitRepo, hasGit, me, isApproved, readApprovals, printable, commitId, statusFiles, gitHashes, noTick,
|
|
12
|
+
removedAt, expireDrafts, draftFile, saveRecord, readPlain, readOwn, ownPath, teamSnapshot, teamMark, keepSaved, isMap,
|
|
12
13
|
} from './brain.js';
|
|
13
14
|
import {
|
|
14
|
-
responseText, signature, errorLines, similarity, tamperFindings, failingTest, lowInfoPrompt, vagueFeature, testCounts, redact, plainYes,
|
|
15
|
+
responseText, signature, errorLines, similarity, tamperFindings, failingTest, lowInfoPrompt, vagueFeature, testCounts, redact, plainYes, isTestFile, isCheckFile, isTestCode, isCiFile, ciFileIn,
|
|
15
16
|
} from './signals.js';
|
|
17
|
+
import { languageOf } from './noskip.js';
|
|
18
|
+
import { unquotePath, quotePath } from './gitpath.js';
|
|
16
19
|
|
|
17
20
|
function readStdin() {
|
|
18
21
|
try {
|
|
@@ -113,19 +116,35 @@ function sessionStart(root, input, agent) {
|
|
|
113
116
|
const state = readState(root);
|
|
114
117
|
const s = session(state, input.session_id);
|
|
115
118
|
s.transcript = input.transcript_path || s.transcript;
|
|
116
|
-
|
|
119
|
+
const fresh = !input.source || ['startup', 'clear', 'new'].includes(input.source);
|
|
120
|
+
const key = input.session_id || 'default';
|
|
121
|
+
if (isGitRepo(root) && !s.baseline && s.record === undefined && !fresh) {
|
|
122
|
+
// Resumed or compacted, but state.json lost the entry (pushed out by newer sessions, or written over by another
|
|
123
|
+
// session's check): a snapshot taken now would start from the agent's own changes (round 9). The record written
|
|
124
|
+
// at the session's real start is used if it is still there; otherwise this session has none, and no ✓.
|
|
125
|
+
const name = recordName(key);
|
|
126
|
+
s.record = fs.existsSync(recordFile(root, { record: name })) ? name : null;
|
|
127
|
+
adoptStart(root, s);
|
|
128
|
+
} else if (isGitRepo(root) && !s.baseline && s.record === undefined) {
|
|
117
129
|
// Remember what was already changed before this session, so we only
|
|
118
130
|
// ever react to what the agent changes now.
|
|
119
131
|
s.startHead = git(root, 'rev-parse HEAD') || null;
|
|
120
132
|
s.baseline = dirtySnapshot(root);
|
|
121
133
|
s.settings = settingsSnapshot(root);
|
|
134
|
+
s.gitSettings = gitSettingsSnapshot(root);
|
|
122
135
|
s.team = teamSnapshot(root);
|
|
123
136
|
s.teamHead = s.startHead;
|
|
137
|
+
keepRecord(root, state, s, key);
|
|
124
138
|
} else if (input.source === 'resume') {
|
|
125
|
-
// While the session was closed only the developer or a pull could change settings.
|
|
139
|
+
// While the session was closed only the developer or a pull could change settings. The record of the
|
|
140
|
+
// test files is kept from the start: a change git hides must not become the new normal (round 6, M4).
|
|
126
141
|
s.settings = settingsSnapshot(root);
|
|
142
|
+
s.gitSettings = gitSettingsSnapshot(root);
|
|
127
143
|
s.team = teamSnapshot(root);
|
|
128
144
|
s.teamHead = git(root, 'rev-parse HEAD') || null;
|
|
145
|
+
// a session started before Codehere kept a record: a record taken now would start from the agent's changes
|
|
146
|
+
// (round 8, M1), so this session has none, and no ✓
|
|
147
|
+
if (s.record === undefined) s.record = null;
|
|
129
148
|
}
|
|
130
149
|
|
|
131
150
|
const out = [];
|
|
@@ -173,7 +192,7 @@ function sessionStart(root, input, agent) {
|
|
|
173
192
|
);
|
|
174
193
|
}
|
|
175
194
|
|
|
176
|
-
writeState(root, state);
|
|
195
|
+
writeState(root, state, [input.session_id || 'default'], fresh ? ['lastSession', 'pendingBrief'] : ['lastSession']);
|
|
177
196
|
// Rules, briefs and help cards come from the repo: none of it may show the agent Codehere's tick.
|
|
178
197
|
const text = noTick(out.join('\n'), '[tick]'); // "[tick]" keeps the meaning of a spec that asks for one
|
|
179
198
|
// Copilot reads only JSON here; Claude Code and Codex take the plain text
|
|
@@ -196,14 +215,20 @@ function briefFile(root, file) {
|
|
|
196
215
|
|
|
197
216
|
const IGNORE = (f) => ['.codehere/', '.claude/', '.codex/', '.github/copilot/', '.github/hooks/'].some((d) => f.startsWith(d));
|
|
198
217
|
|
|
218
|
+
// Null when git cannot list the changes.
|
|
199
219
|
export function dirtySnapshot(root) {
|
|
200
|
-
const
|
|
201
|
-
|
|
220
|
+
const listed = statusFiles(root);
|
|
221
|
+
if (listed == null) return null;
|
|
222
|
+
const files = listed.filter((f) => !IGNORE(f));
|
|
223
|
+
const hashes = gitHashes(root, files, { filters: false }); // no filter program of the repo's runs inside the hook
|
|
224
|
+
if (hashes == null) return null; // a hash git could not give is never read as "unchanged" (round 9)
|
|
202
225
|
return Object.fromEntries(files.map((f, i) => [f, hashes[i] || 'x']));
|
|
203
226
|
}
|
|
204
227
|
|
|
228
|
+
// Null when git cannot list them: the finish check then says so instead of staying silent.
|
|
205
229
|
function changedThisSession(root, s) {
|
|
206
230
|
const now = dirtySnapshot(root);
|
|
231
|
+
if (now == null) return null;
|
|
207
232
|
const before = s.baseline || null;
|
|
208
233
|
const changed = new Set();
|
|
209
234
|
if (!before) {
|
|
@@ -214,7 +239,9 @@ function changedThisSession(root, s) {
|
|
|
214
239
|
}
|
|
215
240
|
}
|
|
216
241
|
if (commitId(s.startHead)) {
|
|
217
|
-
|
|
242
|
+
const committed = gitRead(root, ['diff', s.startHead, 'HEAD', '--name-only', '--no-renames', '--relative', '-z']);
|
|
243
|
+
if (committed == null) return null;
|
|
244
|
+
committed
|
|
218
245
|
.split('\0')
|
|
219
246
|
.filter((f) => f && !IGNORE(f))
|
|
220
247
|
.forEach((f) => changed.add(f));
|
|
@@ -277,7 +304,7 @@ function teamChanged(root, s) {
|
|
|
277
304
|
const diff = [...new Set([...Object.keys(now), ...Object.keys(s.team)])].filter((f) => now[f] !== s.team[f]);
|
|
278
305
|
if (!diff.length) return [];
|
|
279
306
|
const nfc = (f) => f.normalize('NFC');
|
|
280
|
-
const dirty = new Set(statusFiles(root).map(nfc));
|
|
307
|
+
const dirty = new Set((statusFiles(root) || []).map(nfc));
|
|
281
308
|
let byCommit = null;
|
|
282
309
|
const committed = (f) => {
|
|
283
310
|
if (!byCommit) {
|
|
@@ -295,6 +322,239 @@ function teamChanged(root, s) {
|
|
|
295
322
|
return diff.filter((f) => dirty.has(nfc(f)) || !committed(f)).sort();
|
|
296
323
|
}
|
|
297
324
|
|
|
325
|
+
// Git settings that make git show a changed file as unchanged: a clean filter rewrites a file before git
|
|
326
|
+
// reads it, an fsmonitor hook tells git which files changed. Every other diff setting is overridden by
|
|
327
|
+
// PLAIN_DIFF. Kept at session start, from every config scope and the attributes files outside the work tree;
|
|
328
|
+
// in an attributes file only the lines that name a filter or define a macro, which can carry one.
|
|
329
|
+
const FILTER_ATTR = /\bfilter\b|^\s*\[attr\]/;
|
|
330
|
+
const filterLines = (text) => text.split('\n').filter((l) => FILTER_ATTR.test(l)).join('\n');
|
|
331
|
+
function gitSettingsSnapshot(root) {
|
|
332
|
+
const snap = {};
|
|
333
|
+
// every value of a name, from every scope: Codehere's own core.fsmonitor=false comes last, after the repo's
|
|
334
|
+
const values = {};
|
|
335
|
+
for (const l of git(root, ['config', '--get-regexp', '^(filter\\.|core\\.fsmonitor)'], true).split('\n')) {
|
|
336
|
+
if (l) (values[`git config ${l.split(' ')[0]}`] ||= []).push(l);
|
|
337
|
+
}
|
|
338
|
+
for (const [k, ls] of Object.entries(values)) snap[k] = crypto.createHash('sha256').update(ls.join('\n')).digest('hex');
|
|
339
|
+
const xdg = process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config');
|
|
340
|
+
const files = [git(root, ['rev-parse', '--git-path', 'info/attributes']), git(root, ['config', '--path', 'core.attributesFile']) || path.join(xdg, 'git', 'attributes')];
|
|
341
|
+
for (const f of files.filter(Boolean)) {
|
|
342
|
+
// git follows a link here, which readPlain does not: a link counts by its target, so swapping one in is seen
|
|
343
|
+
let text = null;
|
|
344
|
+
try {
|
|
345
|
+
text = filterLines(readPlain(path.resolve(root, f), 'utf8', 1024 * 1024));
|
|
346
|
+
} catch (e) {
|
|
347
|
+
if (e.code === 'ENOTPLAIN') text = `not a plain file: ${fs.lstatSync(path.resolve(root, f)).isSymbolicLink() ? fs.readlinkSync(path.resolve(root, f)) : ''}`;
|
|
348
|
+
}
|
|
349
|
+
if (text) snap[f.startsWith('.git/') ? f : path.resolve(root, f)] = crypto.createHash('sha256').update(text).digest('hex');
|
|
350
|
+
}
|
|
351
|
+
return snap;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// The git settings above that changed since session start, and .gitattributes files changed this session
|
|
355
|
+
// that name a filter.
|
|
356
|
+
function gitSettingsChanged(root, s, changed) {
|
|
357
|
+
const out = [];
|
|
358
|
+
if (s.gitSettings) {
|
|
359
|
+
const now = gitSettingsSnapshot(root);
|
|
360
|
+
for (const k of new Set([...Object.keys(now), ...Object.keys(s.gitSettings)])) if (now[k] !== s.gitSettings[k]) out.push(noTick(k));
|
|
361
|
+
}
|
|
362
|
+
for (const f of changed) {
|
|
363
|
+
if (path.basename(f) !== '.gitattributes') continue;
|
|
364
|
+
let text = '';
|
|
365
|
+
try {
|
|
366
|
+
text = readPlain(path.join(root, f), 'utf8', 1024 * 1024);
|
|
367
|
+
} catch {}
|
|
368
|
+
// only a filter line the session added or changed: a repo that already uses LFS is edited honestly (round 6, m2)
|
|
369
|
+
const before = commitId(s.startHead) ? (gitRead(root, ['show', `${s.startHead}:./${f}`]) ?? '') : '';
|
|
370
|
+
if (filterLines(text) !== filterLines(before)) out.push(noTick(f));
|
|
371
|
+
}
|
|
372
|
+
return out.sort();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ---------- Codehere's own record of the test files ----------
|
|
376
|
+
// Each test file, test config, test setup and CI file by its bytes, kept at session start without asking git
|
|
377
|
+
// and compared at the stop (docs/finish-check-promise.md). Git can be told to show a changed file as unchanged
|
|
378
|
+
// or to diff the index instead of the file (index flags, replace objects, filters, core.bare: cold review, L13
|
|
379
|
+
// rounds 6 and 7); this record cannot. Ids are git blob ids worked out here, so git can diff two versions by id.
|
|
380
|
+
// Not walked: .git, .codehere and node_modules; a folder holding its own .git (a nested repo or a worktree) or a
|
|
381
|
+
// pyvenv.cfg (a virtual environment), and site-packages; and the folders git ignored at session start. A repo
|
|
382
|
+
// with more entries than RAW_LIMIT gets no record, and so no ✓. Each session's record is its own file in
|
|
383
|
+
// .codehere/local/records/, so state.json stays small (round 7, finding 9).
|
|
384
|
+
const NEVER_WALK = new Set(['.git', '.codehere', 'node_modules', 'site-packages']);
|
|
385
|
+
const RAW_LIMIT = 200000;
|
|
386
|
+
const blobId = (algo, bytes) => crypto.createHash(algo).update(`blob ${bytes.length}\0`).update(bytes).digest('hex');
|
|
387
|
+
const isBlobId = (x) => typeof x === 'string' && /^[0-9a-f]{40}([0-9a-f]{24})?$/.test(x);
|
|
388
|
+
const statKey = (st) => `${st.ino}:${st.ctimeMs}:${st.mtimeMs}:${st.size}`;
|
|
389
|
+
function fileId(file, algo, stats, r, follow = false) {
|
|
390
|
+
try {
|
|
391
|
+
const st = (follow ? fs.statSync : fs.lstatSync)(file);
|
|
392
|
+
if (stats) stats[r] = statKey(st);
|
|
393
|
+
if (!algo) return null;
|
|
394
|
+
if (st.isSymbolicLink()) return `link:${fs.readlinkSync(file)}`;
|
|
395
|
+
if (!st.isFile()) return 'not a file';
|
|
396
|
+
if (st.size > 64 * 1024 * 1024) return `size:${st.size}:${st.mtimeMs}`;
|
|
397
|
+
return blobId(algo, fs.readFileSync(file));
|
|
398
|
+
} catch {
|
|
399
|
+
return 'unreadable';
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
// The check files' ids (algo given), their stat lines into stats (when given), or null over RAW_LIMIT.
|
|
403
|
+
// keep: folders that held a recorded file; a .git or pyvenv.cfg made in one mid-session does not stop the walk (round 8, B1)
|
|
404
|
+
// up: from root to the repo's top ('../..') when Codehere is set up in a subfolder: the parents' test configs and
|
|
405
|
+
// setup files, and the top's CI files, are recorded too, by their path from root (round 11)
|
|
406
|
+
export function rawSnapshot(root, algo, { skip = [], stats = null, keep = new Set(), up = '' } = {}) {
|
|
407
|
+
const files = {};
|
|
408
|
+
const skipped = new Set(skip);
|
|
409
|
+
let seen = 0;
|
|
410
|
+
const walk = (dir, rel) => {
|
|
411
|
+
let entries = [];
|
|
412
|
+
try {
|
|
413
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
414
|
+
} catch {}
|
|
415
|
+
if (rel && !keep.has(rel) && entries.some((e) => e.name === '.git' || e.name === 'pyvenv.cfg')) return true;
|
|
416
|
+
for (const e of entries) {
|
|
417
|
+
if (++seen > RAW_LIMIT) return false;
|
|
418
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
419
|
+
if (e.isDirectory()) {
|
|
420
|
+
if (!NEVER_WALK.has(e.name) && !skipped.has(r) && !walk(path.join(dir, e.name), r)) return false;
|
|
421
|
+
} else if (isCheckFile(r) && !skipped.has(r) && !(up && isCiFile(r))) files[r] = fileId(path.join(dir, e.name), algo, stats, r);
|
|
422
|
+
}
|
|
423
|
+
return true;
|
|
424
|
+
};
|
|
425
|
+
// skip: folders git ignored at session start, and test code it ignored by its own name (round 12); up: in a
|
|
426
|
+
// subfolder setup, the package's own .github/workflows is not the repo's CI (round 12)
|
|
427
|
+
if (!walk(root, '')) return null;
|
|
428
|
+
const levels = /^\.\.(\/\.\.)*$/.test(up) ? up.split('/').length : 0;
|
|
429
|
+
const entries = (dir) => {
|
|
430
|
+
try {
|
|
431
|
+
return fs.readdirSync(dir, { withFileTypes: true });
|
|
432
|
+
} catch {
|
|
433
|
+
return [];
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
for (let k = 1; k <= levels; k++) {
|
|
437
|
+
const pre = '../'.repeat(k);
|
|
438
|
+
const dir = path.resolve(root, pre);
|
|
439
|
+
// a parent config that is a link counts by the bytes of the file it points to (round 12)
|
|
440
|
+
for (const e of entries(dir)) if ((e.isFile() || e.isSymbolicLink()) && isCheckFile(pre + e.name) && !isTestCode(pre + e.name)) files[pre + e.name] = fileId(path.join(dir, e.name), algo, stats, pre + e.name, true);
|
|
441
|
+
if (k < levels) continue;
|
|
442
|
+
for (const sub of ['.github/workflows', '.circleci']) {
|
|
443
|
+
for (const e of entries(path.join(dir, sub))) {
|
|
444
|
+
const r = `${pre}${sub}/${e.name}`;
|
|
445
|
+
if (e.isFile() && isCiFile(r)) files[r] = fileId(path.join(dir, sub, e.name), algo, stats, r);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return files;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const recordName = (key) => crypto.createHash('sha256').update(String(key)).digest('hex').slice(0, 16);
|
|
453
|
+
const recordFile = (root, s) => (typeof s.record === 'string' && /^[0-9a-f]{16}$/.test(s.record) ? path.join(paths(root).local, 'records', `${s.record}.json`) : null);
|
|
454
|
+
export function readRecord(root, s) {
|
|
455
|
+
const file = recordFile(root, s);
|
|
456
|
+
try {
|
|
457
|
+
const r = file && JSON.parse(readOwn(root, file, 'utf8', 256 * 1024 * 1024));
|
|
458
|
+
if (r && (r.algo === 'sha1' || r.algo === 'sha256') && isMap(r.ids) && Array.isArray(r.skip)) return r;
|
|
459
|
+
} catch {}
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// A session found again by its record takes the start commit and snapshot kept with it (round 11).
|
|
464
|
+
function adoptStart(root, s) {
|
|
465
|
+
const r = readRecord(root, s);
|
|
466
|
+
if (r && commitId(r.startHead)) s.startHead = r.startHead;
|
|
467
|
+
if (r && isMap(r.baseline)) s.baseline = r.baseline;
|
|
468
|
+
// and the snapshots of its settings and team files, so a change to the agent's hooks is still reported (round 12)
|
|
469
|
+
for (const k of ['settings', 'gitSettings', 'team']) if (r && isMap(r[k])) s[k] = r[k];
|
|
470
|
+
if (r && commitId(r.teamHead)) s.teamHead = r.teamHead;
|
|
471
|
+
if (r && typeof r.startedAt === 'string') s.startedAt = r.startedAt;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// The record, and the start version of each file written into git's object store when it is not there yet (a
|
|
475
|
+
// file changed or untracked at session start), so a diff from it can be built at the stop. Records of sessions
|
|
476
|
+
// no longer in state.json are removed.
|
|
477
|
+
function keepRecord(root, state, s, key) {
|
|
478
|
+
const algo = git(root, ['rev-parse', '--show-object-format']) === 'sha256' ? 'sha256' : 'sha1';
|
|
479
|
+
const ignored = (gitRead(root, ['ls-files', '-z', '--others', '--ignored', '--exclude-standard', '--directory']) ?? '').split('\0');
|
|
480
|
+
const skip = ignored.filter((f) => f.endsWith('/')).map((f) => f.slice(0, -1)).slice(0, 5000);
|
|
481
|
+
let up = '';
|
|
482
|
+
try {
|
|
483
|
+
up = path.relative(fs.realpathSync(root), fs.realpathSync(git(root, ['rev-parse', '--show-toplevel']) || root)).split(path.sep).join('/');
|
|
484
|
+
} catch {}
|
|
485
|
+
const first = rawSnapshot(root, algo, { skip, up });
|
|
486
|
+
// test code git ignores by its own name is build output, as an ignored folder is (round 12)
|
|
487
|
+
const ignoredByName = first ? new Set((gitRead(root, ['check-ignore', '-z', '--stdin'], Object.keys(first).filter(isTestCode).join('\0') + '\0') ?? '').split('\0').filter(Boolean)) : new Set();
|
|
488
|
+
for (const f of ignoredByName) skip.push(f), delete first[f];
|
|
489
|
+
const ids = first;
|
|
490
|
+
s.record = recordName(key);
|
|
491
|
+
delete s.raw; // kept in state.json before round 7
|
|
492
|
+
delete s.rawAlgo;
|
|
493
|
+
const dir = path.join(paths(root).local, 'records');
|
|
494
|
+
const file = recordFile(root, s);
|
|
495
|
+
if (!ids || !ownPath(root, file)) return void (s.record = null);
|
|
496
|
+
try {
|
|
497
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
498
|
+
const tmp = `${file}.${crypto.randomBytes(6).toString('hex')}.tmp`;
|
|
499
|
+
// the session's start commit and snapshot too, so a session state.json loses keeps them (round 11)
|
|
500
|
+
const kept = { startHead: s.startHead || null, baseline: s.baseline || null, settings: s.settings || null, gitSettings: s.gitSettings || null, team: s.team || null, teamHead: s.teamHead || null, startedAt: s.startedAt || null };
|
|
501
|
+
fs.writeFileSync(tmp, JSON.stringify({ algo, ids, skip, up, ...kept }), { flag: 'wx' });
|
|
502
|
+
fs.renameSync(tmp, file);
|
|
503
|
+
// a record no session in state.json names, and older than an hour: a session starting at the same moment
|
|
504
|
+
// may not be in this copy of the state yet (round 8, m2)
|
|
505
|
+
const named = new Set(Object.values(state.sessions).map((x) => `${x.record}.json`));
|
|
506
|
+
for (const n of fs.readdirSync(dir)) {
|
|
507
|
+
const at = path.join(dir, n);
|
|
508
|
+
const st = fs.lstatSync(at, { throwIfNoEntry: false }); // another session's temporary file can go meanwhile
|
|
509
|
+
if (st && !named.has(n) && Date.now() - st.mtimeMs > 3600 * 1000) fs.rmSync(at, { force: true });
|
|
510
|
+
}
|
|
511
|
+
} catch {
|
|
512
|
+
s.record = null;
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const blobs = Object.entries(ids).filter(([f, id]) => isBlobId(id) && !f.includes('\n'));
|
|
516
|
+
if (!blobs.length) return;
|
|
517
|
+
const out = gitRead(root, ['cat-file', '--batch-check'], blobs.map(([, id]) => id).join('\n') + '\n');
|
|
518
|
+
const missing = new Set((out || '').split('\n').filter((l) => l.endsWith(' missing')).map((l) => l.split(' ')[0]));
|
|
519
|
+
const files = blobs.filter(([, id]) => missing.has(id)).map(([f]) => path.resolve(root, f));
|
|
520
|
+
if (files.length) gitRead(root, ['hash-object', '-w', '--no-filters', '--stdin-paths'], files.join('\n') + '\n');
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// The diff of one file from its version at session start (a blob id, or null for a file that did not exist then)
|
|
524
|
+
// to the file as it is now, built by git from the two blobs: no index flag, filter or replace object is asked.
|
|
525
|
+
// A file is read whole. Null when it cannot be built.
|
|
526
|
+
const ONE_MIB = 1024 * 1024;
|
|
527
|
+
function builtDiff(root, file, from, algo, gone = false, known = null) {
|
|
528
|
+
const abs = path.join(root, file);
|
|
529
|
+
let st = null;
|
|
530
|
+
try {
|
|
531
|
+
if (!gone) st = (file.startsWith('../') ? fs.statSync : fs.lstatSync)(abs); // a parent config may be a link (round 12)
|
|
532
|
+
} catch {}
|
|
533
|
+
if ((st && !st.isFile()) || (!st && !from) || (from != null && !isBlobId(from)) || file.includes('\n')) return null;
|
|
534
|
+
const head = `diff --git ${quotePath(`a/${file}`)} ${quotePath(`b/${file}`)}\n--- ${from ? quotePath(`a/${file}`) : '/dev/null'}\n+++ ${st ? quotePath(`b/${file}`) : '/dev/null'}\n`;
|
|
535
|
+
const status = `${from ? (st ? 'M' : 'D') : 'A'}\t${quotePath(file)}`;
|
|
536
|
+
if (!from && st.size <= ONE_MIB) {
|
|
537
|
+
// a small new file: every line is added, no git needed; one that grew past the limit goes through git
|
|
538
|
+
let bytes = null;
|
|
539
|
+
try {
|
|
540
|
+
bytes = readPlain(abs, null, ONE_MIB + 1);
|
|
541
|
+
} catch {}
|
|
542
|
+
if (bytes && bytes.length <= ONE_MIB) {
|
|
543
|
+
const lines = bytes.toString('utf8').split('\n');
|
|
544
|
+
if (lines.at(-1) === '') lines.pop();
|
|
545
|
+
return { status, diff: head + (lines.length ? `@@ -0,0 +1,${lines.length} @@\n${lines.map((l) => `+${l}`).join('\n')}` : '') };
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
const empty = blobId(algo, Buffer.alloc(0));
|
|
549
|
+
const now = !st ? empty : known || (gitRead(root, ['hash-object', '-w', '--no-filters', '--stdin-paths'], abs + '\n') ?? '').trim();
|
|
550
|
+
if (!isBlobId(now)) return null;
|
|
551
|
+
const out = gitRead(root, ['diff', '-U0', from || empty, now]);
|
|
552
|
+
if (out == null) return null;
|
|
553
|
+
// git names the two blobs: the file's own path goes in their place
|
|
554
|
+
const at = out.startsWith('@@') ? 0 : out.indexOf('\n@@') + 1;
|
|
555
|
+
return { status, diff: head + (at > 0 || out.startsWith('@@') ? out.slice(at) : '') };
|
|
556
|
+
}
|
|
557
|
+
|
|
298
558
|
function settingsChanged(root, s) {
|
|
299
559
|
if (!s.settings) return []; // no snapshot from session start: cannot tell
|
|
300
560
|
const now = settingsSnapshot(root);
|
|
@@ -428,7 +688,7 @@ function bindDraft(root, input, command, text) {
|
|
|
428
688
|
const exact = !!file && fs.existsSync(path.dirname(file)) && fs.readdirSync(path.dirname(file)).includes(`${m[1]}.json`);
|
|
429
689
|
const rel = path.relative(root, file || '').toLowerCase();
|
|
430
690
|
const tracked = git(root, ['ls-files', '-z']).split('\0').some((x) => x.toLowerCase() === rel); // .CODEHERE/... too
|
|
431
|
-
if (!callOk || !exact || tracked) return 'refused';
|
|
691
|
+
if (!callOk || !exact || tracked || !ownPath(root, file)) return 'refused';
|
|
432
692
|
const content = readPlain(file, null);
|
|
433
693
|
let rec = null;
|
|
434
694
|
try {
|
|
@@ -448,7 +708,7 @@ function bindDraft(root, input, command, text) {
|
|
|
448
708
|
}
|
|
449
709
|
s.drafts = [...(Array.isArray(s.drafts) ? s.drafts : []), entry];
|
|
450
710
|
delete s.lastSendBack; // any send-back came before this draft, so it cannot be the answer to it
|
|
451
|
-
return writeState(root, state) ? { proof: entry.proof || null } : 'refused'; // a binding that was not kept is not one
|
|
711
|
+
return writeState(root, state, [input.session_id || 'default']) ? { proof: entry.proof || null } : 'refused'; // a binding that was not kept is not one
|
|
452
712
|
}
|
|
453
713
|
|
|
454
714
|
// The lines the CLI printed for this record (commands.js draft()), each indented by two spaces.
|
|
@@ -524,7 +784,7 @@ function lessonText(root, doc) {
|
|
|
524
784
|
const files = json(m.files);
|
|
525
785
|
const hashes = json(m.file_hashes);
|
|
526
786
|
if (Array.isArray(files) && files.length && files.every((f) => typeof f === 'string')) {
|
|
527
|
-
const now = gitHashes(root, files);
|
|
787
|
+
const now = gitHashes(root, files) || [];
|
|
528
788
|
const changed = !Array.isArray(hashes) || files.some((f, i) => !now[i] || now[i] !== hashes[i]);
|
|
529
789
|
out.push(`Files ${files.join(', ')} · ${changed ? 'changed' : 'unchanged'} since the fix`);
|
|
530
790
|
}
|
|
@@ -553,11 +813,11 @@ function lessonProofFor(root, sig, proofCommand, changed, test) {
|
|
|
553
813
|
proof_command: proofCommand,
|
|
554
814
|
proof_exit: 0,
|
|
555
815
|
proof_commit: git(root, ['rev-parse', '--short', 'HEAD']),
|
|
556
|
-
proof_dirty: statusFiles(root).some((f) => !IGNORE(f)),
|
|
816
|
+
proof_dirty: (statusFiles(root) ?? ['?']).some((f) => !IGNORE(f)), // unknown counts as dirty
|
|
557
817
|
});
|
|
558
818
|
}
|
|
559
819
|
proof.files = JSON.stringify(changed);
|
|
560
|
-
proof.file_hashes = JSON.stringify(gitHashes(root, changed));
|
|
820
|
+
proof.file_hashes = JSON.stringify(gitHashes(root, changed) || changed.map(() => null));
|
|
561
821
|
return proof;
|
|
562
822
|
}
|
|
563
823
|
|
|
@@ -586,7 +846,7 @@ function postTool(root, input, agent) {
|
|
|
586
846
|
const s = state.sessions[input.session_id || 'default'];
|
|
587
847
|
if (s && s.lastFailure && s.lastFailure.command === command) {
|
|
588
848
|
s.lastFailure = null;
|
|
589
|
-
writeState(root, state);
|
|
849
|
+
writeState(root, state, [input.session_id || 'default']);
|
|
590
850
|
}
|
|
591
851
|
quiet();
|
|
592
852
|
}
|
|
@@ -605,12 +865,12 @@ function postTool(root, input, agent) {
|
|
|
605
865
|
|
|
606
866
|
const lesson = matchLesson(root, sig, s.shownLessons, test);
|
|
607
867
|
if (lesson) s.shownLessons.push(lesson.id);
|
|
608
|
-
writeState(root, state);
|
|
868
|
+
writeState(root, state, [input.session_id || 'default']);
|
|
609
869
|
|
|
610
870
|
const threshold = cfg.loopThreshold;
|
|
611
871
|
if (count === threshold || count === threshold + 2) {
|
|
612
872
|
s.loopsStopped = (s.loopsStopped || 0) + 1;
|
|
613
|
-
writeState(root, state);
|
|
873
|
+
writeState(root, state, [input.session_id || 'default']);
|
|
614
874
|
const msg = [
|
|
615
875
|
`Codehere: the same error has come back ${count} times this session:`,
|
|
616
876
|
` ${sig}`,
|
|
@@ -672,11 +932,12 @@ const short = (t) => (t.length > 60 ? `${t.slice(0, 60)}…` : t);
|
|
|
672
932
|
function decideDrafts(root, state, s, prompt) {
|
|
673
933
|
const notes = [];
|
|
674
934
|
const lines = [];
|
|
935
|
+
const moved = []; // other open sessions whose team snapshot a save moved: written back with this one
|
|
675
936
|
// Copilot passes a send-back to this hook as if the person typed it: that is not their answer
|
|
676
937
|
// used once: a later message that happens to start the same way is the person's
|
|
677
938
|
const skip = s.lastSendBack && prompt.startsWith(s.lastSendBack);
|
|
678
939
|
delete s.lastSendBack;
|
|
679
|
-
if (skip) return { notes, lines };
|
|
940
|
+
if (skip) return { notes, lines, moved };
|
|
680
941
|
// since this session's previous prompt, or its start: an older draft is not this answer's
|
|
681
942
|
const since = Date.parse(s.lastPromptAt || s.startedAt || '') || Date.now();
|
|
682
943
|
s.lastPromptAt = new Date().toISOString();
|
|
@@ -685,6 +946,7 @@ function decideDrafts(root, state, s, prompt) {
|
|
|
685
946
|
let proofBack = null;
|
|
686
947
|
for (const d of bound) {
|
|
687
948
|
const file = draftFile(root, d.id);
|
|
949
|
+
if (!ownPath(root, file)) continue;
|
|
688
950
|
// Taken by a rename, which only one of two hooks running at the same moment can win (two unlinks
|
|
689
951
|
// of one file both succeeded in a test on macOS); the other finds it gone and saves nothing.
|
|
690
952
|
let content = null;
|
|
@@ -720,7 +982,7 @@ function decideDrafts(root, state, s, prompt) {
|
|
|
720
982
|
// a saved rule is not a settings change, unless rules.md had already changed this session
|
|
721
983
|
if (rec.kind === 'rule' && s.settings && s.settings[RULES] === before) s.settings[RULES] = settingsSnapshot(root)[RULES];
|
|
722
984
|
const teamAfter = teamSnapshot(root);
|
|
723
|
-
for (const x of Object.
|
|
985
|
+
for (const [k, x] of Object.entries(state.sessions)) if (keepSaved(x, teamBefore, teamAfter)) moved.push(k); // every open session: the save is not its agent's either
|
|
724
986
|
notes.push(`Codehere saved the draft after the user's yes: ${saved.file}${saved.lesson ? `, and the team lesson ${saved.lesson}` : ''}. Tell them it is saved. Teammates get it once the user commits and pushes it.`);
|
|
725
987
|
// the agent's own words: never a tick in a codehere line (DESIGN.md §1)
|
|
726
988
|
lines.push(`codehere › saved after your yes · ${rec.kind} · ${short(noTick(gist(rec)))}`);
|
|
@@ -737,7 +999,7 @@ function decideDrafts(root, state, s, prompt) {
|
|
|
737
999
|
s.drafts = [];
|
|
738
1000
|
if (proofBack) s.pendingLesson = proofBack;
|
|
739
1001
|
else delete s.pendingLesson;
|
|
740
|
-
return { notes, lines };
|
|
1002
|
+
return { notes, lines, moved };
|
|
741
1003
|
}
|
|
742
1004
|
|
|
743
1005
|
// An unbound draft made since then: one bound in any session is that session's to decide.
|
|
@@ -805,7 +1067,7 @@ function promptSubmit(root, input) {
|
|
|
805
1067
|
].join('\n');
|
|
806
1068
|
}
|
|
807
1069
|
|
|
808
|
-
writeState(root, state);
|
|
1070
|
+
writeState(root, state, [input.session_id || 'default', ...decided.moved]);
|
|
809
1071
|
const context = [...decided.notes, ...(msg ? [msg.slice(0, 1100)] : [])].join('\n\n');
|
|
810
1072
|
if (context || decided.lines.length) {
|
|
811
1073
|
emitJson({
|
|
@@ -900,8 +1162,13 @@ function block(reason, message) {
|
|
|
900
1162
|
function stop(root, input) {
|
|
901
1163
|
const cfg = readConfig(root);
|
|
902
1164
|
const state = readState(root);
|
|
903
|
-
const
|
|
1165
|
+
const sid = input.session_id || 'default';
|
|
1166
|
+
const s = session(state, sid);
|
|
904
1167
|
const mayBlock = (s.stopBlocks || 0) < 3; // never trap the agent: after 3 blocks, only observe
|
|
1168
|
+
// Only this session is written from this copy; every other one is taken as it is on disk then, so sessions that
|
|
1169
|
+
// started or changed while this check ran (it can run the tests for minutes) are kept as they are (rounds 9 and 10).
|
|
1170
|
+
const top = ['lastSession']; // top-level fields this check sets: lastGreen and episodes only after a proven run
|
|
1171
|
+
const save = () => writeState(root, state, [sid], top);
|
|
905
1172
|
// The first line of each reason is kept: Copilot passes a send-back to the prompt hook as if the
|
|
906
1173
|
// person typed it, and the prompt hook must not take it as their answer to a draft.
|
|
907
1174
|
// A send-back that cannot be counted is not sent: without the count the 3-block cap would never
|
|
@@ -910,20 +1177,59 @@ function stop(root, input) {
|
|
|
910
1177
|
// the test result is not hidden behind it.
|
|
911
1178
|
const sendAgentBack = (reason, message, carryOn = false) => {
|
|
912
1179
|
s.lastSendBack = reason.split('\n')[0];
|
|
913
|
-
if (
|
|
1180
|
+
if (save()) block(reason, message);
|
|
914
1181
|
const found = reason.split('\n').filter((l) => l.startsWith('- ')).map((l) => l.slice(2));
|
|
915
1182
|
const why = `codehere ✕ ${noTick([s.lastSendBack, ...found].join(' ')).slice(0, 160)} · not sent back · Codehere could not save its state in .codehere/local`;
|
|
916
1183
|
if (carryOn) return void lines.push(why);
|
|
917
1184
|
emitJson({ systemMessage: [String(message || '').replace(/ · agent sent back · \d+ of 3/, ''), printable(why)].filter(Boolean).join('\n') });
|
|
918
1185
|
};
|
|
919
1186
|
|
|
920
|
-
if (!
|
|
1187
|
+
if (!hasGit()) emitJson({ systemMessage: 'codehere ⊘ git not found · the finish check did not run' });
|
|
1188
|
+
// A session that started in a repo is checked blind when git no longer reads the folder as one (core.bare, a
|
|
1189
|
+
// moved .git): the record still says what changed (rounds 6 and 8). A folder git could not read at session start
|
|
1190
|
+
// stays quiet, as it did before (round 8, m3).
|
|
1191
|
+
// An entry state.json lost has its record found by the session's id, as at a compaction (rounds 9 and 10).
|
|
1192
|
+
if (s.record === undefined && !s.baseline && !commitId(s.startHead)) {
|
|
1193
|
+
const name = recordName(sid);
|
|
1194
|
+
s.record = fs.existsSync(recordFile(root, { record: name })) ? name : null;
|
|
1195
|
+
adoptStart(root, s);
|
|
1196
|
+
}
|
|
1197
|
+
// A session with no start Codehere knows (no start commit, no snapshot, no record) cannot be told to have changed
|
|
1198
|
+
// nothing: it is checked, and says there is no record, instead of staying silent (round 10).
|
|
1199
|
+
const known = Boolean(commitId(s.startHead) || s.baseline || s.record);
|
|
1200
|
+
if (!isGitRepo(root) && !known) quiet();
|
|
921
1201
|
const base = commitId(s.startHead) || 'HEAD';
|
|
922
|
-
const
|
|
923
|
-
const
|
|
1202
|
+
const listed = changedThisSession(root, s);
|
|
1203
|
+
const blind = listed == null; // git could not list the changes: never read that as "nothing changed"
|
|
1204
|
+
const changed = listed || [];
|
|
1205
|
+
const nfc = (f) => f.normalize('NFC');
|
|
1206
|
+
// the test files whose bytes changed since session start, by Codehere's own record; none to compare means no ✓
|
|
1207
|
+
const record = readRecord(root, s);
|
|
1208
|
+
const rawStats = {}; // when each file was last touched, to see one change while the tests run
|
|
1209
|
+
const held = new Set(record ? Object.keys(record.ids).flatMap((f) => f.split('/').slice(0, -1).map((_, i, p) => p.slice(0, i + 1).join('/'))) : []);
|
|
1210
|
+
const up = typeof record?.up === 'string' ? record.up : '';
|
|
1211
|
+
const isCi = ciFileIn(up);
|
|
1212
|
+
const rawNow = record ? rawSnapshot(root, record.algo, { skip: record.skip, stats: rawStats, keep: held, up }) : null;
|
|
1213
|
+
// A new file in a folder git ignores now is a build or an install (tests copied, package files, a config copied: rounds
|
|
1214
|
+
// 8 to 10), and so is new test code git ignores by its own name (a build writing next to the sources: round 11): not
|
|
1215
|
+
// the agent's, before the test run and during it alike. A new config or setup file git ignores by its own name (a
|
|
1216
|
+
// pytest.ini put in .git/info/exclude) is still read.
|
|
1217
|
+
const ignored = (paths) => new Set((paths.length ? gitRead(root, ['check-ignore', '-z', '--stdin'], paths.join('\0') + '\0') : '')?.split('\0').filter(Boolean));
|
|
1218
|
+
const letOffNew = (files) => {
|
|
1219
|
+
const dirs = [...new Set(files.map((f) => path.posix.dirname(f)).filter((d) => d !== '.'))];
|
|
1220
|
+
const folders = new Set([...ignored(dirs.map((d) => `${d}/`))].map((d) => d.replace(/\/$/, '')));
|
|
1221
|
+
const byName = ignored(files.filter(isTestCode));
|
|
1222
|
+
return new Set(files.filter((f) => folders.has(path.posix.dirname(f)) || byName.has(f)));
|
|
1223
|
+
};
|
|
1224
|
+
if (rawNow) for (const f of letOffNew(Object.keys(rawNow).filter((x) => !(x in record.ids)))) delete rawNow[f], delete rawStats[f];
|
|
1225
|
+
const noRecord = !rawNow;
|
|
1226
|
+
const rawStart = new Map(record ? Object.entries(record.ids).map(([f, id]) => [nfc(f), id]) : []);
|
|
1227
|
+
const rawChanged = rawNow ? [...new Set([...Object.keys(rawNow), ...Object.keys(record.ids)])].filter((f) => rawNow[f] !== record.ids[f]) : [];
|
|
1228
|
+
const gitSettings = gitSettingsChanged(root, s, changed);
|
|
1229
|
+
const settings = [...settingsChanged(root, s), ...gitSettings];
|
|
924
1230
|
const removed = removedAt(root);
|
|
925
1231
|
const team = teamChanged(root, s);
|
|
926
|
-
if (!changed.length && !settings.length && !removed && !team.length) quiet(); // the agent changed nothing this session: stay silent
|
|
1232
|
+
if (known && !blind && !changed.length && !rawChanged.length && !settings.length && !removed && !team.length) quiet(); // the agent changed nothing this session: stay silent
|
|
927
1233
|
|
|
928
1234
|
// Lines for the developer. Each is escaped as a whole, so text from the repo cannot fake a
|
|
929
1235
|
// line, and text from the repo never carries a tick (noTick).
|
|
@@ -944,22 +1250,144 @@ function stop(root, input) {
|
|
|
944
1250
|
quiet();
|
|
945
1251
|
}
|
|
946
1252
|
// only lessons or help cards changed: the person gets the line, and the tests are not run
|
|
947
|
-
if (!changed.length && !settings.length && !removed) report();
|
|
1253
|
+
if (known && !blind && !changed.length && !rawChanged.length && !settings.length && !removed) report();
|
|
948
1254
|
|
|
949
1255
|
// 1. tampering with tests or checks (only in files changed this session)
|
|
950
1256
|
let findings = [];
|
|
951
|
-
let unread =
|
|
952
|
-
if (changed.length) {
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
1257
|
+
let unread = blind; // the session's diff could not be read, so weakened tests were not checked: never proven
|
|
1258
|
+
if (changed.length || rawChanged.length) {
|
|
1259
|
+
// With a record, test files are read through it alone: git's diff of a test file can be of the index (an index
|
|
1260
|
+
// flag on a staged file) or of a replaced commit, and git can hide the change altogether (rounds 6 and 7).
|
|
1261
|
+
// Other files are read through git.
|
|
1262
|
+
const recorded = (f) => rawNow != null && isCheckFile(f);
|
|
1263
|
+
const gitFiles = changed.filter((f) => !recorded(f));
|
|
1264
|
+
let nameStatus = changed.length ? gitPaths(root, ['diff', base, '--relative', '--name-status'], changed) : '';
|
|
1265
|
+
let diff = gitFiles.length ? gitPaths(root, ['diff', base, '--relative', '-U0'], gitFiles) : '';
|
|
1266
|
+
unread = blind || diff === null || nameStatus === null;
|
|
1267
|
+
// git's statuses are kept for other files, and for a rename of a test file the record saw go (the record alone
|
|
1268
|
+
// cannot tell a rename from a deletion and an addition); a test file's other statuses come from the record
|
|
1269
|
+
const rawGone = new Set(rawChanged.map(nfc));
|
|
1270
|
+
const nowSet = new Set(Object.keys(rawNow || {}).map(nfc));
|
|
1271
|
+
const renamedFrom = new Map(); // new path -> its old path, for a test file renamed this session
|
|
1272
|
+
const movedAway = new Set(); // old paths of those renames: their content is read at the new path
|
|
1273
|
+
const kept = [];
|
|
1274
|
+
for (const line of String(nameStatus || '').split('\n')) {
|
|
1275
|
+
const [status, a, b] = line.split('\t');
|
|
1276
|
+
if (!status || !a) continue;
|
|
1277
|
+
const [from, to] = [unquotePath(a), b && unquotePath(b)];
|
|
1278
|
+
if (/^R/.test(status) && to && recorded(from)) {
|
|
1279
|
+
if (!rawGone.has(nfc(from))) continue;
|
|
1280
|
+
// a rename git takes from the index onto a file that is gone, or onto a file with its own start version, is a
|
|
1281
|
+
// deletion: the new file is read from its own start (round 8, B3 and B4)
|
|
1282
|
+
if (recorded(to) && (!nowSet.has(nfc(to)) || rawStart.has(nfc(to)))) continue;
|
|
1283
|
+
kept.push(line);
|
|
1284
|
+
if (!(isCi(from) && !isCi(to))) movedAway.add(nfc(from)); // a CI file moved out: its lines read as removed (round 11)
|
|
1285
|
+
if (recorded(to)) renamedFrom.set(nfc(to), nfc(from));
|
|
1286
|
+
} else if (!recorded(from) && !(to && recorded(to))) kept.push(line);
|
|
1287
|
+
}
|
|
1288
|
+
// a test moved without git (plain mv) is one deleted and one new file: paired as git pairs renames, by the same
|
|
1289
|
+
// bytes or mostly the same lines, so an honest move is not a deletion and a moved test is read from its start
|
|
1290
|
+
// (round 8, M4)
|
|
1291
|
+
if (rawNow) {
|
|
1292
|
+
// by the same bytes, any check file and any number of them (round 9); by shared lines, up to 500 (a config
|
|
1293
|
+
// rewritten as it moves, jest.config.js to .mjs, too: round 10; tests moved with their imports fixed: round 11)
|
|
1294
|
+
const gone = rawChanged.filter((f) => f in record.ids && !(f in rawNow) && !movedAway.has(nfc(f)));
|
|
1295
|
+
const added = rawChanged.filter((f) => !(f in record.ids) && f in rawNow && !renamedFrom.has(nfc(f)));
|
|
1296
|
+
const byId = new Map();
|
|
1297
|
+
for (const g of gone) byId.set(record.ids[g], [...(byId.get(record.ids[g]) || []), g]);
|
|
1298
|
+
const lineSet = (text) => new Set(String(text ?? '').split('\n').map((l) => l.trim()).filter(Boolean));
|
|
1299
|
+
const startLines = new Map();
|
|
1300
|
+
const firstGone = gone.slice(0, 500);
|
|
1301
|
+
let compared = 0;
|
|
1302
|
+
for (const a of added) {
|
|
1303
|
+
let best = (byId.get(rawNow[a]) || []).find((g) => !movedAway.has(nfc(g))) ?? null;
|
|
1304
|
+
let now = null;
|
|
1305
|
+
if (!best && compared++ < 500) {
|
|
1306
|
+
try {
|
|
1307
|
+
now = lineSet(readPlain(path.join(root, a), 'utf8', 1024 * 1024));
|
|
1308
|
+
} catch {}
|
|
1309
|
+
}
|
|
1310
|
+
for (let i = 0, score = 0.5; now && i < firstGone.length; i++) {
|
|
1311
|
+
const g = firstGone[i];
|
|
1312
|
+
if (movedAway.has(nfc(g))) continue;
|
|
1313
|
+
if (!startLines.has(g)) startLines.set(g, isBlobId(record.ids[g]) ? lineSet(gitRead(root, ['cat-file', 'blob', record.ids[g]])) : new Set());
|
|
1314
|
+
const was = startLines.get(g);
|
|
1315
|
+
const both = [...now].filter((l) => was.has(l)).length;
|
|
1316
|
+
const j = now.size + was.size ? both / (now.size + was.size - both) : 0;
|
|
1317
|
+
if (j >= score) [best, score] = [g, j];
|
|
1318
|
+
}
|
|
1319
|
+
if (!best) continue;
|
|
1320
|
+
kept.push(`R100\t${quotePath(best)}\t${quotePath(a)}`);
|
|
1321
|
+
movedAway.add(nfc(best));
|
|
1322
|
+
renamedFrom.set(nfc(a), nfc(best));
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
nameStatus = kept.join('\n');
|
|
1326
|
+
const inStatus = new Set(kept.flatMap((l) => l.split('\t').slice(1)).map((f) => nfc(unquotePath(f))));
|
|
1327
|
+
// Files git's diff does not hold, read through a diff Codehere builds: new code the agent left untracked (git
|
|
1328
|
+
// diff never shows it), and every test file whose bytes changed, from its start version in the record.
|
|
1329
|
+
const build = new Map(); // file -> its version at session start: a blob id, or null when it is new
|
|
1330
|
+
if (changed.length && !blind) {
|
|
1331
|
+
const untracked = (gitPaths(root, ['ls-files', '-z', '--others', '--exclude-standard'], changed) ?? '').split('\0').filter(Boolean);
|
|
1332
|
+
let n = 0;
|
|
1333
|
+
for (const f of untracked) {
|
|
1334
|
+
if (recorded(f) || inStatus.has(nfc(f)) || !(isCheckFile(f) || languageOf(f) != null)) continue;
|
|
1335
|
+
if (++n > 2000) break;
|
|
1336
|
+
if (!(s.baseline && f in s.baseline)) build.set(f, null); // already untracked at session start: as before
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
for (const f of rawChanged) {
|
|
1340
|
+
if (movedAway.has(nfc(f))) continue;
|
|
1341
|
+
build.set(f, rawStart.get(renamedFrom.get(nfc(f)) ?? nfc(f)) ?? null);
|
|
1342
|
+
}
|
|
1343
|
+
// the bytes now of every file built from a start version, worked out here as the walk did; one git call writes
|
|
1344
|
+
// them all into the object store (two git calls per file took 45 s for 3,000 files: round 10)
|
|
1345
|
+
const nowIds = new Map();
|
|
1346
|
+
if (rawNow) {
|
|
1347
|
+
const write = [...build].filter(([f, from]) => from && f in rawNow && rawNow[f] !== from && isBlobId(rawNow[f]) && !f.includes('\n')).map(([f]) => f);
|
|
1348
|
+
if (write.length && gitRead(root, ['hash-object', '-w', '--no-filters', '--stdin-paths'], write.map((f) => path.resolve(root, f)).join('\n') + '\n') != null) for (const f of write) nowIds.set(f, rawNow[f]);
|
|
1349
|
+
}
|
|
1350
|
+
for (const [f, from] of build) {
|
|
1351
|
+
if (from && rawNow && rawNow[f] === from) continue; // moved without a change: nothing to read
|
|
1352
|
+
const built = builtDiff(root, f, from, record ? record.algo : 'sha1', recorded(f) && !(f in rawNow), nowIds.get(f)); // not walked now: gone, even behind a link (round 8, B2)
|
|
1353
|
+
if (built) {
|
|
1354
|
+
if (!inStatus.has(nfc(f))) nameStatus = `${nameStatus}\n${built.status}`;
|
|
1355
|
+
diff = `${diff || ''}\n${built.diff}`;
|
|
1356
|
+
} else if (isCheckFile(f)) unread = true; // a test file changed and its change could not be read: never proven
|
|
1357
|
+
}
|
|
1358
|
+
// a test file as it is now, to see what a swallowing catch holds: plain files only, up to 1 MiB
|
|
1359
|
+
const read = (f, max = 1024 * 1024) => {
|
|
1360
|
+
try {
|
|
1361
|
+
return readPlain(path.join(root, f), 'utf8', max);
|
|
1362
|
+
} catch {
|
|
1363
|
+
return null;
|
|
1364
|
+
}
|
|
1365
|
+
};
|
|
1366
|
+
// the repo's test files, read only when the diff returns a literal under a condition: up to 400
|
|
1367
|
+
// files, 256 KiB each and 4 MiB in all
|
|
1368
|
+
let testList = null;
|
|
1369
|
+
const testFiles = () => (testList ||= git(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard'], true).split('\0').filter(isTestFile));
|
|
1370
|
+
const tests = () => {
|
|
1371
|
+
const out = [];
|
|
1372
|
+
let total = 0;
|
|
1373
|
+
for (const f of testFiles().slice(0, 400)) {
|
|
1374
|
+
const text = read(f, 256 * 1024);
|
|
1375
|
+
if (text == null) continue;
|
|
1376
|
+
if ((total += text.length) > 4 * 1024 * 1024) break;
|
|
1377
|
+
out.push(`${f}\0${text}`);
|
|
1378
|
+
}
|
|
1379
|
+
return out;
|
|
1380
|
+
};
|
|
1381
|
+
// a lone test moved to another extension is a migration only if tests the repo had at session start use it
|
|
1382
|
+
let baseList = null;
|
|
1383
|
+
const baseTests = () => (baseList ||= (commitId(s.startHead) ? git(root, ['ls-tree', '-r', '-z', '--name-only', s.startHead], true) : '').split('\0').filter(isTestFile));
|
|
1384
|
+
findings = tamperFindings(nameStatus || '', diff || '', { read, tests, testFiles: baseTests, command: cfg.testCommand, up });
|
|
957
1385
|
}
|
|
958
1386
|
const key = findings.join('|');
|
|
959
1387
|
if (findings.length && s.tamperReported !== key && mayBlock) {
|
|
960
1388
|
s.tamperReported = key;
|
|
961
1389
|
s.stopBlocks++;
|
|
962
|
-
|
|
1390
|
+
save();
|
|
963
1391
|
sendAgentBack(
|
|
964
1392
|
[
|
|
965
1393
|
'Codehere found changes that weaken tests or checks:',
|
|
@@ -967,7 +1395,7 @@ function stop(root, input) {
|
|
|
967
1395
|
'Restore them and fix the code instead.',
|
|
968
1396
|
'If the change is truly intended, tell the user in one line why, then finish.',
|
|
969
1397
|
].join('\n'),
|
|
970
|
-
|
|
1398
|
+
[printable(`codehere ✕ a check was weakened · ${noTick(findings.join(' · '))} · agent sent back · ${s.stopBlocks} of 3`), message()].filter(Boolean).join('\n'),
|
|
971
1399
|
true
|
|
972
1400
|
);
|
|
973
1401
|
}
|
|
@@ -980,15 +1408,15 @@ function stop(root, input) {
|
|
|
980
1408
|
if (sendBack && s.settingsReported !== settingsKey && mayBlock) {
|
|
981
1409
|
s.settingsReported = settingsKey;
|
|
982
1410
|
s.stopBlocks++;
|
|
983
|
-
|
|
1411
|
+
save();
|
|
984
1412
|
sendAgentBack(
|
|
985
1413
|
[
|
|
986
|
-
'Codehere
|
|
1414
|
+
'Codehere, hook or git settings changed during this session:',
|
|
987
1415
|
...settings.map((f) => `- ${f}`),
|
|
988
1416
|
'If you changed them and the user did not ask for it, undo your change.',
|
|
989
1417
|
'Then tell the user in one line what changed and why, and finish.',
|
|
990
1418
|
].join('\n'),
|
|
991
|
-
|
|
1419
|
+
message(),
|
|
992
1420
|
true
|
|
993
1421
|
);
|
|
994
1422
|
}
|
|
@@ -1009,17 +1437,25 @@ function stop(root, input) {
|
|
|
1009
1437
|
let proven = false; // passed here, now, with intact tests and an unchanged config
|
|
1010
1438
|
if (cfg.verifyOnStop && approved) {
|
|
1011
1439
|
const t = runTests(root, cfg);
|
|
1012
|
-
//
|
|
1440
|
+
// A test file touched since the record was read at the start of this check: what ran may not be what was read
|
|
1441
|
+
// (a process the agent left running, or a git hook, swapping files during the check: round 7, finding 2).
|
|
1442
|
+
// A file's change time cannot be set back, so a swap and a swap back are both seen.
|
|
1443
|
+
const after = {};
|
|
1444
|
+
if (rawNow) rawSnapshot(root, null, { skip: record.skip, stats: after, keep: held, up });
|
|
1445
|
+
let touched = rawNow ? [...new Set([...Object.keys(after), ...Object.keys(rawStats)])].filter((f) => after[f] !== rawStats[f]).sort() : [];
|
|
1446
|
+
const appeared = letOffNew(touched.filter((f) => !(f in record.ids))); // made during the check, by a build
|
|
1447
|
+
touched = touched.filter((f) => !appeared.has(f));
|
|
1448
|
+
// saves made while the tests ran moved snapshots on disk, and this hook writes its own session back
|
|
1013
1449
|
const disk = readState(root).sessions || {};
|
|
1014
1450
|
for (const [k, x] of Object.entries(state.sessions)) if (isMap(disk[k]?.team)) Object.assign(x, { team: disk[k].team, teamHead: disk[k].teamHead });
|
|
1015
1451
|
if (!t.ok && !mayBlock) {
|
|
1016
1452
|
lines.push(`codehere ✕ ${cmd} · ${runResult(t)} · not sent back, limit of 3 reached`);
|
|
1017
|
-
|
|
1453
|
+
save();
|
|
1018
1454
|
report();
|
|
1019
1455
|
}
|
|
1020
1456
|
if (!t.ok) {
|
|
1021
1457
|
s.stopBlocks++;
|
|
1022
|
-
|
|
1458
|
+
save();
|
|
1023
1459
|
const tail = t.output.split('\n').slice(-25).join('\n');
|
|
1024
1460
|
sendAgentBack(
|
|
1025
1461
|
[
|
|
@@ -1034,14 +1470,22 @@ function stop(root, input) {
|
|
|
1034
1470
|
}
|
|
1035
1471
|
// ✓ only for intact evidence (DESIGN.md §1): not after a weakened test, nor after the
|
|
1036
1472
|
// config that names the test command changed.
|
|
1037
|
-
|
|
1473
|
+
// a run where the runner's own count says no test passed proves nothing (every test skipped, or none found)
|
|
1474
|
+
const counts = t.ok ? testCounts(t.output) : null;
|
|
1475
|
+
const nonePassed = Boolean(counts && !counts.passed);
|
|
1476
|
+
proven = !findings.length && !configChanged && !unread && !gitSettings.length && !noRecord && !touched.length && !nonePassed;
|
|
1038
1477
|
if (findings.length) passLine = `codehere ✕ ${cmd} passed, but a check was weakened · ${noTick(findings.join(' · '))}`;
|
|
1039
1478
|
else if (configChanged) passLine = `codehere ✕ ${cmd} passed, but .codehere/config.json changed during this session`;
|
|
1040
1479
|
else if (unread) passLine = `codehere ✕ ${cmd} passed, but this session's diff could not be read · weakened tests not checked`;
|
|
1480
|
+
else if (gitSettings.length) passLine = `codehere ✕ ${cmd} passed, but git settings that change what a diff shows changed during this session`;
|
|
1481
|
+
else if (noRecord) passLine = `codehere ✕ ${cmd} passed, but Codehere has no record of the test files from session start · weakened tests may be missed`;
|
|
1482
|
+
else if (touched.length) passLine = `codehere ✕ ${cmd} passed, but test files or configs changed while Codehere ran the check · ${noTick(touched.slice(0, 3).join(' · '))}${touched.length > 3 ? ` · and ${touched.length - 3} more` : ''}`;
|
|
1483
|
+
else if (nonePassed) passLine = `codehere ✕ ${cmd} · 0 passed · no test ran`;
|
|
1041
1484
|
else passLine = `codehere ✓ ${cmd} · ${runResult(t)} · re-run here · ${t.seconds.toFixed(1)} s`;
|
|
1042
1485
|
if (proven) {
|
|
1043
1486
|
state.lastGreen = { commit: git(root, 'rev-parse HEAD'), at: new Date().toISOString() };
|
|
1044
1487
|
recordGreen(state, s);
|
|
1488
|
+
top.push('lastGreen', 'episodes');
|
|
1045
1489
|
}
|
|
1046
1490
|
lines.unshift(passLine);
|
|
1047
1491
|
}
|
|
@@ -1072,6 +1516,6 @@ function stop(root, input) {
|
|
|
1072
1516
|
} else lines.push('codehere › repeated error solved · not offered as a lesson · limit of 3 reached');
|
|
1073
1517
|
}
|
|
1074
1518
|
|
|
1075
|
-
|
|
1519
|
+
save();
|
|
1076
1520
|
report();
|
|
1077
1521
|
}
|