@kabmo-labs/kabmo 0.38.0 → 0.38.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/dist/client/index.html +1 -1
- package/dist/client/index.js +421 -420
- package/dist/client/static/js/{CsvGrid-90zfbn7f.js → CsvGrid-4dpsfdrf.js} +1 -1
- package/dist/client/static/js/{DocxView-vwq2rf16.js → DocxView-crdgc0jd.js} +1 -1
- package/dist/client/static/js/{DrawioView-bvx7eqcz.js → DrawioView-1ts9y0yk.js} +1 -1
- package/dist/client/static/js/{ImageView-q6xfmbrq.js → ImageView-w2hyp129.js} +1 -1
- package/dist/client/static/js/{LayoutWrapper-qatdaw3n.js → LayoutWrapper-pdvfkncm.js} +1 -1
- package/dist/client/static/js/{Modal-6jx1fvfb.js → Modal-2sx1tgqr.js} +1 -1
- package/dist/client/static/js/{PdfView-tkv2d2zs.js → PdfView-72c123a9.js} +1 -1
- package/dist/client/static/js/{SessionsView-rbnecykz.js → SessionsView-a8921ysr.js} +1 -1
- package/dist/client/static/js/{SheetView-t08g8kdr.js → SheetView-8xh69wgk.js} +1 -1
- package/dist/client/static/js/{SvgPreview-9vh6mdcp.js → SvgPreview-z3fm87h6.js} +1 -1
- package/dist/client/static/js/{abnfDiagram-O67JEVCF-bxcn33e1.js → abnfDiagram-O67JEVCF-25r2p3vh.js} +1 -1
- package/dist/client/static/js/{architectureDiagram-NJMV4G6O-40ggw6e8.js → architectureDiagram-NJMV4G6O-z3n19xwk.js} +1 -1
- package/dist/client/static/js/{blockDiagram-BEXU5L5S-k41cvjrd.js → blockDiagram-BEXU5L5S-vzamgz9s.js} +1 -1
- package/dist/client/static/js/{c4Diagram-YGBWAQC7-rdcmn6ac.js → c4Diagram-YGBWAQC7-jejafgn0.js} +1 -1
- package/dist/client/static/js/{classDiagram-v2-NBCMYWYE-txbmbkxv.js → classDiagram-v2-NBCMYWYE-qfa6mj0q.js} +1 -1
- package/dist/client/static/js/{cose-bilkent-JH36ORCC-bxzh3qj1.js → cose-bilkent-JH36ORCC-9vzdgmk1.js} +1 -1
- package/dist/client/static/js/{cynefinDiagram-VND7K2PF-abx5343b.js → cynefinDiagram-VND7K2PF-a3k3w1t5.js} +1 -1
- package/dist/client/static/js/{dagre-6A5THRUB-baj278kt.js → dagre-6A5THRUB-gf04dvxj.js} +1 -1
- package/dist/client/static/js/{diagram-22UHCM2B-vqvhdc5m.js → diagram-22UHCM2B-a9e0a684.js} +1 -1
- package/dist/client/static/js/{diagram-3UASUU5V-w328rfx0.js → diagram-3UASUU5V-fb73gfjd.js} +1 -1
- package/dist/client/static/js/{diagram-ATOU4E4O-zzcmwnm8.js → diagram-ATOU4E4O-yb9n3t30.js} +1 -1
- package/dist/client/static/js/{diagram-CDSNMT55-8yw39mmj.js → diagram-CDSNMT55-adhbhw28.js} +1 -1
- package/dist/client/static/js/{diagram-MLGK6HIB-zgc386pv.js → diagram-MLGK6HIB-v4yxyb8r.js} +1 -1
- package/dist/client/static/js/{diagram-MPIPVDR6-efmvvscm.js → diagram-MPIPVDR6-94ttsqy2.js} +1 -1
- package/dist/client/static/js/{dockerfile-bp5dre33.js → dockerfile-mrranatf.js} +1 -1
- package/dist/client/static/js/{ebnfDiagram-ZINNZB2B-p66h0dzq.js → ebnfDiagram-ZINNZB2B-jj4f3pyq.js} +1 -1
- package/dist/client/static/js/{elk-276RUBZZ-g0jjxcqd.js → elk-276RUBZZ-mtnkhyte.js} +1 -1
- package/dist/client/static/js/{erDiagram-OPXOYQCR-39gat96z.js → erDiagram-OPXOYQCR-18qdkhm5.js} +1 -1
- package/dist/client/static/js/flowDiagram-KWPJA3E3-zmz4grtx.js +1 -0
- package/dist/client/static/js/{ganttDiagram-FUAMR5RP-nsjv1zt4.js → ganttDiagram-FUAMR5RP-vz28h061.js} +1 -1
- package/dist/client/static/js/{gitGraphDiagram-X574FWY7-r4vnkcy3.js → gitGraphDiagram-X574FWY7-cgq3r4rz.js} +1 -1
- package/dist/client/static/js/{index-73jwm4d5.js → index-12p63902.js} +1 -1
- package/dist/client/static/js/{index-h8h5c2r8.js → index-1c7294vk.js} +1 -1
- package/dist/client/static/js/{index-9wtg6ywn.js → index-2xb54jt3.js} +1 -1
- package/dist/client/static/js/{index-da10qpns.js → index-4hsxaa87.js} +1 -1
- package/dist/client/static/js/{index-5y0bvq4q.js → index-5v9v41ap.js} +2 -2
- package/dist/client/static/js/{index-rgp69pa8.js → index-6d5qj0kb.js} +1 -1
- package/dist/client/static/js/{index-11bdd235.js → index-6j9h1fa7.js} +1 -1
- package/dist/client/static/js/{index-dkseg29t.js → index-6k2x281j.js} +2 -2
- package/dist/client/static/js/{index-tzm90bsz.js → index-6rt7gs1j.js} +1 -1
- package/dist/client/static/js/{index-5ebhb94w.js → index-737w2mqf.js} +1 -1
- package/dist/client/static/js/{index-c15r8jd3.js → index-95t1hwr8.js} +1 -1
- package/dist/client/static/js/{index-vnn7m9yp.js → index-a5ptb956.js} +1 -1
- package/dist/client/static/js/{index-2drjp9ks.js → index-aa6qdgmv.js} +1 -1
- package/dist/client/static/js/{index-gwhx66f4.js → index-ajtd1q1h.js} +1 -1
- package/dist/client/static/js/{index-rf0p3v7q.js → index-arvqarv3.js} +1 -1
- package/dist/client/static/js/{index-ra38ggke.js → index-bgcp21ed.js} +1 -1
- package/dist/client/static/js/{index-rqrcw8nj.js → index-d146s9g3.js} +1 -1
- package/dist/client/static/js/{index-st202rjv.js → index-d8kjsgpt.js} +7 -7
- package/dist/client/static/js/{index-z0cpp5bc.js → index-dr7nhha9.js} +1 -1
- package/dist/client/static/js/{index-6ax2z70w.js → index-enxfdvdq.js} +1 -1
- package/dist/client/static/js/{index-dfgdn3fj.js → index-fqhpxzyb.js} +1 -1
- package/dist/client/static/js/{index-24yyj78e.js → index-gbdvr4rt.js} +1 -1
- package/dist/client/static/js/{index-50z4ek83.js → index-hp8kfk9z.js} +1 -1
- package/dist/client/static/js/{index-ha75kff0.js → index-kf9t3mzv.js} +1 -1
- package/dist/client/static/js/{index-hzx81cjq.js → index-naz3px72.js} +1 -1
- package/dist/client/static/js/{index-1qszrvzk.js → index-njqpkcvw.js} +1 -1
- package/dist/client/static/js/{index-e1pr3k50.js → index-qxb8k6py.js} +1 -1
- package/dist/client/static/js/{index-a74k07z2.js → index-rtd9frp1.js} +1 -1
- package/dist/client/static/js/{index-2ndq1ngv.js → index-s16h2nmv.js} +1 -1
- package/dist/client/static/js/{index-k9xsvab3.js → index-s910y31q.js} +1 -1
- package/dist/client/static/js/{index-tv61v3t3.js → index-we2ezmn7.js} +1 -1
- package/dist/client/static/js/{index-0kxqzafy.js → index-y5y7smet.js} +1 -1
- package/dist/client/static/js/{index-tfsaj5d1.js → index-z72wf7q4.js} +1 -1
- package/dist/client/static/js/{index-vxd4gykh.js → index-zdbvm01f.js} +1 -1
- package/dist/client/static/js/{infoDiagram-VRGFBTTK-e39pvm4j.js → infoDiagram-VRGFBTTK-jjwckbr9.js} +1 -1
- package/dist/client/static/js/{ishikawaDiagram-OU5B5YK6-rqhszgvp.js → ishikawaDiagram-OU5B5YK6-qzk5gejf.js} +1 -1
- package/dist/client/static/js/{journeyDiagram-ZHPQQLJL-wgy5qrsv.js → journeyDiagram-ZHPQQLJL-maccpg3q.js} +1 -1
- package/dist/client/static/js/{kanban-definition-PNTS6WVX-b8bxn0h1.js → kanban-definition-PNTS6WVX-xv2azgws.js} +1 -1
- package/dist/client/static/js/{mermaid.core-vb5ehtre.js → mermaid.core-e4ys760s.js} +2 -2
- package/dist/client/static/js/{mindmap-definition-NLK3R4M7-t4p38s38.js → mindmap-definition-NLK3R4M7-b779wz81.js} +1 -1
- package/dist/client/static/js/one-dark-pro-kckm8epj.js +1 -0
- package/dist/client/static/js/one-light-0v7rg9a7.js +1 -0
- package/dist/client/static/js/{pegDiagram-GJSIUBJH-3gp4madx.js → pegDiagram-GJSIUBJH-8sgdmhnw.js} +1 -1
- package/dist/client/static/js/{pieDiagram-5QR66LMP-0cbm773h.js → pieDiagram-5QR66LMP-nz6n95d6.js} +1 -1
- package/dist/client/static/js/{quadrantDiagram-O4NWA36T-jwg7fc9f.js → quadrantDiagram-O4NWA36T-47dpygk5.js} +1 -1
- package/dist/client/static/js/{railroadDiagram-XR7U4H2S-38xh691s.js → railroadDiagram-XR7U4H2S-wv0bvwve.js} +1 -1
- package/dist/client/static/js/{requirementDiagram-PLB6GJNP-gpddawxh.js → requirementDiagram-PLB6GJNP-w3r8r837.js} +1 -1
- package/dist/client/static/js/{rust-bx3ecv4n.js → rust-42c4h7e4.js} +1 -1
- package/dist/client/static/js/{sankeyDiagram-IPEJSGJF-s6t6bbhk.js → sankeyDiagram-IPEJSGJF-t55rjgad.js} +1 -1
- package/dist/client/static/js/{sequenceDiagram-PO4LG4MO-wkq51pmb.js → sequenceDiagram-PO4LG4MO-yfwjwy37.js} +1 -1
- package/dist/client/static/js/{sizeCapture-INFHLROL-xtwexf8k.js → sizeCapture-INFHLROL-nq6hy7ce.js} +1 -1
- package/dist/client/static/js/{stateDiagram-v2-GCMORJYK-jnwsqa7y.js → stateDiagram-v2-GCMORJYK-4xy0atqh.js} +1 -1
- package/dist/client/static/js/{swimlanes-2SLR337P-mnkkqcbx.js → swimlanes-2SLR337P-np47cy1q.js} +1 -1
- package/dist/client/static/js/{swimlanesDiagram-TC7HE7FX-6xmrz03n.js → swimlanesDiagram-TC7HE7FX-56rk9kxv.js} +1 -1
- package/dist/client/static/js/{timeline-definition-EJHVYXUP-rxq6heqh.js → timeline-definition-EJHVYXUP-rb0gnm35.js} +1 -1
- package/dist/client/static/js/{usecaseDiagram-POWQR4AR-dz94f57t.js → usecaseDiagram-POWQR4AR-gatkznan.js} +1 -1
- package/dist/client/static/js/{vennDiagram-UO4OBE2U-5mmvkhsq.js → vennDiagram-UO4OBE2U-r3f5xjjq.js} +1 -1
- package/dist/client/static/js/{wardleyDiagram-VNRHLVJA-dctdntpa.js → wardleyDiagram-VNRHLVJA-tpass65t.js} +1 -1
- package/dist/client/static/js/{xychartDiagram-PMCCYNJV-njyfrqkv.js → xychartDiagram-PMCCYNJV-8xkh67cw.js} +1 -1
- package/package.json +1 -1
- package/src/server/lib/changes/capture-bash.server.ts +69 -4
- package/src/server/lib/changes/capture-fs.server.ts +2 -10
- package/src/server/lib/changes/capture-races.test.ts +107 -0
- package/src/server/lib/changes/capture-scan.test.ts +157 -0
- package/src/server/lib/changes/capture-test-util.ts +1 -1
- package/src/server/lib/changes/capture-types.ts +3 -1
- package/src/server/lib/changes/capture-write.server.ts +50 -0
- package/src/server/lib/changes/capture.server.ts +33 -18
- package/src/server/lib/changes/capture.test.ts +48 -0
- package/src/server/lib/changes/classify-command.test.ts +20 -0
- package/src/server/lib/changes/classify-command.ts +46 -23
- package/src/server/lib/changes/classify-git.ts +12 -11
- package/src/server/lib/changes/classify-tokenize.ts +9 -1
- package/src/server/lib/changes/journal.server.ts +3 -1
- package/src/server/lib/changes/omp-fallback.server.ts +48 -0
- package/src/server/lib/changes/omp-fallback.test.ts +75 -0
- package/src/server/lib/changes/reader.server.ts +28 -0
- package/src/server/lib/changes/scan.server.ts +56 -1
- package/src/server/lib/changes/sessions.server.ts +65 -0
- package/src/server/lib/changes/sessions.test.ts +52 -0
- package/dist/client/static/js/flowDiagram-KWPJA3E3-f6jpcqyq.js +0 -1
- package/dist/client/static/js/one-dark-pro-dhybn1ff.js +0 -1
- package/dist/client/static/js/one-light-bqg77ay0.js +0 -1
|
@@ -21,21 +21,30 @@ import * as fsp from 'node:fs/promises';
|
|
|
21
21
|
import * as os from 'node:os';
|
|
22
22
|
import * as path from 'node:path';
|
|
23
23
|
import { makeCapture, records, REPO } from '@/server/lib/changes/capture-test-util';
|
|
24
|
+
import { ChangeJournal } from '@/server/lib/changes/journal.server';
|
|
24
25
|
import { readSessionChangeDiff } from '@/server/lib/changes/reader.server';
|
|
25
26
|
|
|
26
27
|
let dataDir = '';
|
|
28
|
+
let agentDir = '';
|
|
27
29
|
let original: string | undefined;
|
|
30
|
+
let originalAgent: string | undefined;
|
|
28
31
|
|
|
29
32
|
beforeAll(async () => {
|
|
30
33
|
original = Bun.env.KABMO_DATA_DIR;
|
|
34
|
+
originalAgent = Bun.env.PI_CODING_AGENT_DIR;
|
|
31
35
|
dataDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'kabmo-changes-capture-'));
|
|
36
|
+
agentDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'kabmo-changes-capture-agent-'));
|
|
32
37
|
Bun.env.KABMO_DATA_DIR = dataDir;
|
|
38
|
+
Bun.env.PI_CODING_AGENT_DIR = agentDir;
|
|
33
39
|
});
|
|
34
40
|
|
|
35
41
|
afterAll(async () => {
|
|
36
42
|
if (original === undefined) delete Bun.env.KABMO_DATA_DIR;
|
|
37
43
|
else Bun.env.KABMO_DATA_DIR = original;
|
|
44
|
+
if (originalAgent === undefined) delete Bun.env.PI_CODING_AGENT_DIR;
|
|
45
|
+
else Bun.env.PI_CODING_AGENT_DIR = originalAgent;
|
|
38
46
|
await fsp.rm(dataDir, { recursive: true, force: true });
|
|
47
|
+
await fsp.rm(agentDir, { recursive: true, force: true });
|
|
39
48
|
});
|
|
40
49
|
|
|
41
50
|
const WINDOW_END = Date.now() + 60_000;
|
|
@@ -221,6 +230,45 @@ describe('scan pass', () => {
|
|
|
221
230
|
});
|
|
222
231
|
});
|
|
223
232
|
|
|
233
|
+
describe('cross-session scan', () => {
|
|
234
|
+
test('a scan skips a path another session in the same cwd recorded', async () => {
|
|
235
|
+
const other = new ChangeJournal('cap-other', REPO);
|
|
236
|
+
await other.recordChange({
|
|
237
|
+
at: Date.now(), agent: 'Main', tool: 'edit', op: 'update', path: at('file4.txt'),
|
|
238
|
+
displayPath: 'file4.txt', before: 'a', after: 'b', source: 'tool',
|
|
239
|
+
});
|
|
240
|
+
await other.flush();
|
|
241
|
+
|
|
242
|
+
const files = new Map<string, string>();
|
|
243
|
+
const scans = [
|
|
244
|
+
{ root: REPO, dirty: new Map<string, string | null>(), truncated: false },
|
|
245
|
+
{ root: REPO, dirty: new Map([[at('file4.txt'), 'python']]), truncated: false },
|
|
246
|
+
];
|
|
247
|
+
const capture = makeCapture('cap-cross', files, {
|
|
248
|
+
takeScan: async () => scans.shift() ?? null,
|
|
249
|
+
readHead: async () => null,
|
|
250
|
+
});
|
|
251
|
+
capture.onFrame({
|
|
252
|
+
type: 'tool_execution_start',
|
|
253
|
+
toolCallId: 'c1',
|
|
254
|
+
toolName: 'bash',
|
|
255
|
+
args: { command: "python3 - <<'PY'\nopen('file4.txt','w').write('python')\nPY" },
|
|
256
|
+
});
|
|
257
|
+
await capture.flush();
|
|
258
|
+
files.set(at('file4.txt'), 'python');
|
|
259
|
+
capture.onFrame({
|
|
260
|
+
type: 'tool_execution_end',
|
|
261
|
+
toolCallId: 'c1',
|
|
262
|
+
toolName: 'bash',
|
|
263
|
+
result: { details: { trace: { status: 0, units: [{ nodes: [{ k: 'simple', argv: ['python3', '-'], st: 0 }] }] } } },
|
|
264
|
+
});
|
|
265
|
+
await capture.flush();
|
|
266
|
+
|
|
267
|
+
const lines = await records('cap-cross');
|
|
268
|
+
expect(lines.some((line) => line.type === 'change' && line.path === at('file4.txt'))).toBe(false);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
224
272
|
describe('subagents', () => {
|
|
225
273
|
test('a child edit is attributed to the child', async () => {
|
|
226
274
|
const files = new Map([[at('f.ts'), 'old']]);
|
|
@@ -140,6 +140,26 @@ describe('classifyCommand', () => {
|
|
|
140
140
|
expect(ops(result)).toEqual([`update:${at('f')}`]);
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
+
test('a newline separates commands', () => {
|
|
144
|
+
const result = plan("printf 'x' > a.txt\npython3 - <<'PY'\nprint('hi')\nPY\ntouch b.txt");
|
|
145
|
+
expect(ops(result)).toEqual([`update:${at('a.txt')}`, `update:${at('b.txt')}`]);
|
|
146
|
+
expect(result.scan).toBe(true);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('a scan verb later in a chain still triggers the scan', () => {
|
|
150
|
+
const result = plan("printf 'x' > a.txt && python3 - <<'PY'\nopen('b.txt','w').write('y')\nPY");
|
|
151
|
+
expect(result.scan).toBe(true);
|
|
152
|
+
expect(result.opaque).toBe(true);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('a test runner does not trigger the scan', () => {
|
|
156
|
+
expect(plan('bun test').scan).toBe(false);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('git reset triggers the scan', () => {
|
|
160
|
+
expect(plan('git reset --hard').scan).toBe(true);
|
|
161
|
+
});
|
|
162
|
+
|
|
143
163
|
test('a chain is the union of its commands', () => {
|
|
144
164
|
const result = plan('rm a && echo hi > b');
|
|
145
165
|
expect(ops(result)).toEqual([`delete:${at('a')}`, `update:${at('b')}`]);
|
|
@@ -61,6 +61,8 @@ export interface CommandPlan {
|
|
|
61
61
|
opaque: boolean;
|
|
62
62
|
/** Every command is a known read-only verb and there is no output redirect. */
|
|
63
63
|
readOnly: boolean;
|
|
64
|
+
/** An opaque command whose effect a filesystem scan can reconcile. */
|
|
65
|
+
scan: boolean;
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
/** Verbs that never write a file. */
|
|
@@ -95,6 +97,21 @@ const OPAQUE: Record<string, true> = {
|
|
|
95
97
|
watch: true,
|
|
96
98
|
};
|
|
97
99
|
|
|
100
|
+
/** Opaque verbs whose effect a filesystem scan can reconcile. */
|
|
101
|
+
const SCAN_VERBS: Record<string, true> = {
|
|
102
|
+
make: true, cargo: true, go: true, sh: true, bash: true, zsh: true,
|
|
103
|
+
python: true, python3: true, node: true, deno: true, xargs: true,
|
|
104
|
+
sudo: true, eval: true, source: true, '.': true,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** Package managers scan only for the subcommands that write the tree. */
|
|
108
|
+
const SCAN_PACKAGE_VERBS: Record<string, true> = {
|
|
109
|
+
npm: true, pnpm: true, yarn: true, bun: true,
|
|
110
|
+
};
|
|
111
|
+
const SCAN_PACKAGE_SUBCOMMANDS: Record<string, true> = {
|
|
112
|
+
install: true, i: true, add: true, remove: true, uninstall: true,
|
|
113
|
+
};
|
|
114
|
+
|
|
98
115
|
/** Classify one already-parsed `argv` (the shell trace's `simple` node). */
|
|
99
116
|
export function classifyArgv(argv: readonly string[], cwd: string, home: string): CommandPlan {
|
|
100
117
|
return classifySimple({ words: [...argv], redirects: [] }, cwd, home);
|
|
@@ -105,6 +122,7 @@ export function classifyCommand(script: string, cwd: string, home: string): Comm
|
|
|
105
122
|
const mutations: CommandMutation[] = [];
|
|
106
123
|
let opaque = false;
|
|
107
124
|
let readOnly = true;
|
|
125
|
+
let scan = false;
|
|
108
126
|
let currentCwd = cwd;
|
|
109
127
|
for (const command of splitCommands(tokenizeCommand(stripHeredocs(script)))) {
|
|
110
128
|
const verb = command.words[0] ? path.basename(command.words[0]) : '';
|
|
@@ -121,14 +139,16 @@ export function classifyCommand(script: string, cwd: string, home: string): Comm
|
|
|
121
139
|
mutations.push(...result.mutations);
|
|
122
140
|
if (result.opaque) opaque = true;
|
|
123
141
|
if (!result.readOnly) readOnly = false;
|
|
142
|
+
if (result.scan) scan = true;
|
|
124
143
|
}
|
|
125
|
-
return { mutations: dedupe(mutations), opaque, readOnly };
|
|
144
|
+
return { mutations: dedupe(mutations), opaque, readOnly, scan };
|
|
126
145
|
}
|
|
127
146
|
|
|
128
147
|
function classifySimple(command: SimpleCommand, cwd: string, home: string): CommandPlan {
|
|
129
148
|
const mutations: CommandMutation[] = [];
|
|
130
149
|
let opaque = false;
|
|
131
150
|
let readOnly = true;
|
|
151
|
+
let scan = false;
|
|
132
152
|
|
|
133
153
|
const add = (op: MutationOp, raw: string, from?: string): void => {
|
|
134
154
|
// `/dev/null` and friends are redirect sinks, not working-tree mutations.
|
|
@@ -147,35 +167,37 @@ function classifySimple(command: SimpleCommand, cwd: string, home: string): Comm
|
|
|
147
167
|
}
|
|
148
168
|
|
|
149
169
|
const argv = command.words;
|
|
150
|
-
if (argv.length === 0) return { mutations, opaque, readOnly };
|
|
170
|
+
if (argv.length === 0) return { mutations, opaque, readOnly, scan };
|
|
151
171
|
const verb = path.basename(argv[0]);
|
|
152
172
|
const args = argv.slice(1);
|
|
153
173
|
|
|
154
|
-
if (READ_ONLY[verb] === true) return { mutations, opaque, readOnly };
|
|
174
|
+
if (READ_ONLY[verb] === true) return { mutations, opaque, readOnly, scan };
|
|
175
|
+
if (SCAN_VERBS[verb] === true) scan = true;
|
|
176
|
+
if (SCAN_PACKAGE_VERBS[verb] === true && SCAN_PACKAGE_SUBCOMMANDS[args[0] ?? ''] === true) scan = true;
|
|
155
177
|
|
|
156
178
|
if (verb === 'sed' || verb === 'gsed' || verb === 'perl') {
|
|
157
179
|
const inPlace = args.some((arg) => arg === '-i' || arg.startsWith('-i') || arg === '--in-place' || arg.startsWith('--in-place='));
|
|
158
|
-
if (!inPlace) return { mutations, opaque, readOnly };
|
|
180
|
+
if (!inPlace) return { mutations, opaque, readOnly, scan };
|
|
159
181
|
for (const file of scriptFiles(args)) add('update', file);
|
|
160
|
-
return { mutations, opaque, readOnly };
|
|
182
|
+
return { mutations, opaque, readOnly, scan };
|
|
161
183
|
}
|
|
162
184
|
if (verb === 'awk' || verb === 'gawk') {
|
|
163
185
|
const index = args.findIndex((arg) => arg === '-i' || arg === 'inplace');
|
|
164
|
-
if (index === -1) return { mutations, opaque, readOnly };
|
|
186
|
+
if (index === -1) return { mutations, opaque, readOnly, scan };
|
|
165
187
|
// `-i inplace` is two tokens; the program follows, then the files.
|
|
166
188
|
const programAt = args[index] === '-i' && args[index + 1] === 'inplace' ? index + 2 : index + 1;
|
|
167
189
|
for (const file of args.slice(programAt + 1).filter((arg) => !arg.startsWith('-'))) add('update', file);
|
|
168
|
-
return { mutations, opaque, readOnly };
|
|
190
|
+
return { mutations, opaque, readOnly, scan };
|
|
169
191
|
}
|
|
170
192
|
if (verb === 'rm' || verb === 'rmdir' || verb === 'unlink') {
|
|
171
193
|
for (const file of nonFlagArgs(args)) add('delete', file);
|
|
172
|
-
return { mutations, opaque, readOnly };
|
|
194
|
+
return { mutations, opaque, readOnly, scan };
|
|
173
195
|
}
|
|
174
196
|
if (verb === 'mv') {
|
|
175
197
|
const files = nonFlagArgs(args);
|
|
176
198
|
if (files.length < 2) {
|
|
177
199
|
opaque = true;
|
|
178
|
-
return { mutations, opaque, readOnly };
|
|
200
|
+
return { mutations, opaque, readOnly, scan };
|
|
179
201
|
}
|
|
180
202
|
const dest = files[files.length - 1];
|
|
181
203
|
const sources = files.slice(0, -1);
|
|
@@ -190,13 +212,13 @@ function classifySimple(command: SimpleCommand, cwd: string, home: string): Comm
|
|
|
190
212
|
mutations.push({ op: 'rename', path: to, from });
|
|
191
213
|
readOnly = false;
|
|
192
214
|
}
|
|
193
|
-
return { mutations, opaque, readOnly };
|
|
215
|
+
return { mutations, opaque, readOnly, scan };
|
|
194
216
|
}
|
|
195
217
|
if (verb === 'cp') {
|
|
196
218
|
const files = nonFlagArgs(args);
|
|
197
219
|
if (files.length < 2) {
|
|
198
220
|
opaque = true;
|
|
199
|
-
return { mutations, opaque, readOnly };
|
|
221
|
+
return { mutations, opaque, readOnly, scan };
|
|
200
222
|
}
|
|
201
223
|
const dest = files[files.length - 1];
|
|
202
224
|
const sources = files.slice(0, -1);
|
|
@@ -210,63 +232,64 @@ function classifySimple(command: SimpleCommand, cwd: string, home: string): Comm
|
|
|
210
232
|
mutations.push({ op: 'update', path: to });
|
|
211
233
|
readOnly = false;
|
|
212
234
|
}
|
|
213
|
-
return { mutations, opaque, readOnly };
|
|
235
|
+
return { mutations, opaque, readOnly, scan };
|
|
214
236
|
}
|
|
215
237
|
if (verb === 'tee' || verb === 'touch') {
|
|
216
238
|
for (const file of nonFlagArgs(args)) add('update', file);
|
|
217
|
-
return { mutations, opaque, readOnly };
|
|
239
|
+
return { mutations, opaque, readOnly, scan };
|
|
218
240
|
}
|
|
219
241
|
if (verb === 'truncate') {
|
|
220
242
|
for (const file of truncateFiles(args)) add('update', file);
|
|
221
|
-
return { mutations, opaque, readOnly };
|
|
243
|
+
return { mutations, opaque, readOnly, scan };
|
|
222
244
|
}
|
|
223
245
|
if (verb === 'install' || verb === 'ln') {
|
|
224
246
|
const files = nonFlagArgs(args);
|
|
225
247
|
const file = files[files.length - 1];
|
|
226
248
|
if (file) add(verb === 'ln' ? 'create' : 'update', file);
|
|
227
249
|
else opaque = true;
|
|
228
|
-
return { mutations, opaque, readOnly };
|
|
250
|
+
return { mutations, opaque, readOnly, scan };
|
|
229
251
|
}
|
|
230
252
|
if (verb === 'dd') {
|
|
231
253
|
for (const arg of args) {
|
|
232
254
|
if (arg.startsWith('of=')) add('update', arg.slice(3));
|
|
233
255
|
}
|
|
234
|
-
return { mutations, opaque, readOnly };
|
|
256
|
+
return { mutations, opaque, readOnly, scan };
|
|
235
257
|
}
|
|
236
258
|
if (verb === 'sort') {
|
|
237
259
|
const out = flagValue(args, '-o') ?? flagValue(args, '--output');
|
|
238
260
|
if (out) add('update', out);
|
|
239
|
-
return { mutations, opaque, readOnly };
|
|
261
|
+
return { mutations, opaque, readOnly, scan };
|
|
240
262
|
}
|
|
241
263
|
if (verb === 'uniq') {
|
|
242
264
|
const files = nonFlagArgs(args);
|
|
243
265
|
if (files.length >= 2) add('update', files[1]);
|
|
244
|
-
return { mutations, opaque, readOnly };
|
|
266
|
+
return { mutations, opaque, readOnly, scan };
|
|
245
267
|
}
|
|
246
268
|
if (verb === 'curl' || verb === 'wget') {
|
|
247
269
|
const out = flagValue(args, '-o') ?? flagValue(args, '--output') ?? flagValue(args, '-O');
|
|
248
270
|
if (out) add('update', out);
|
|
249
|
-
return { mutations, opaque, readOnly };
|
|
271
|
+
return { mutations, opaque, readOnly, scan };
|
|
250
272
|
}
|
|
251
273
|
if (verb === 'find') {
|
|
252
274
|
const mutating = args.some((arg) => arg === '-delete' || arg === '-exec' || arg === '-execdir' || arg === '-ok' || arg === '-okdir');
|
|
253
275
|
if (mutating) {
|
|
254
276
|
opaque = true;
|
|
255
277
|
readOnly = false;
|
|
278
|
+
scan = true;
|
|
256
279
|
}
|
|
257
|
-
return { mutations, opaque, readOnly };
|
|
280
|
+
return { mutations, opaque, readOnly, scan };
|
|
258
281
|
}
|
|
259
282
|
if (verb === 'git') {
|
|
260
|
-
return classifyGit(args, cwd, home, mutations, opaque, readOnly);
|
|
283
|
+
return classifyGit(args, cwd, home, mutations, opaque, readOnly, scan);
|
|
261
284
|
}
|
|
262
285
|
if (OPAQUE[verb] === true) {
|
|
263
286
|
opaque = true;
|
|
264
287
|
readOnly = false;
|
|
265
|
-
return { mutations, opaque, readOnly };
|
|
288
|
+
return { mutations, opaque, readOnly, scan };
|
|
266
289
|
}
|
|
267
290
|
|
|
268
291
|
// An unknown verb is not assumed harmless.
|
|
269
292
|
opaque = true;
|
|
270
293
|
readOnly = false;
|
|
271
|
-
return { mutations, opaque, readOnly };
|
|
294
|
+
return { mutations, opaque, readOnly, scan };
|
|
272
295
|
}
|
|
@@ -41,10 +41,11 @@ export function classifyGit(
|
|
|
41
41
|
mutations: CommandMutation[],
|
|
42
42
|
opaque: boolean,
|
|
43
43
|
readOnly: boolean,
|
|
44
|
+
scan: boolean,
|
|
44
45
|
): CommandPlan {
|
|
45
46
|
const sub = args[0];
|
|
46
|
-
if (!sub) return { mutations, opaque, readOnly };
|
|
47
|
-
if (GIT_READ_ONLY[sub] === true) return { mutations, opaque, readOnly };
|
|
47
|
+
if (!sub) return { mutations, opaque, readOnly, scan };
|
|
48
|
+
if (GIT_READ_ONLY[sub] === true) return { mutations, opaque, readOnly, scan };
|
|
48
49
|
|
|
49
50
|
const add = (op: MutationOp, raw: string, from?: string): void => {
|
|
50
51
|
if (isSpecialFile(raw)) return;
|
|
@@ -59,30 +60,30 @@ export function classifyGit(
|
|
|
59
60
|
|
|
60
61
|
if (sub === 'mv') {
|
|
61
62
|
const files = nonFlagArgs(args.slice(1));
|
|
62
|
-
if (files.length < 2) return { mutations, opaque: true, readOnly: false };
|
|
63
|
+
if (files.length < 2) return { mutations, opaque: true, readOnly: false, scan: true };
|
|
63
64
|
const from = resolvePath(cwd, home, files[0]);
|
|
64
65
|
const to = resolvePath(cwd, home, files[files.length - 1]);
|
|
65
|
-
if (!from || !to) return { mutations, opaque: true, readOnly: false };
|
|
66
|
+
if (!from || !to) return { mutations, opaque: true, readOnly: false, scan: true };
|
|
66
67
|
mutations.push({ op: 'rename', path: to, from });
|
|
67
|
-
return { mutations, opaque, readOnly: false };
|
|
68
|
+
return { mutations, opaque, readOnly: false, scan };
|
|
68
69
|
}
|
|
69
70
|
if (sub === 'rm') {
|
|
70
71
|
for (const file of nonFlagArgs(args.slice(1))) add('delete', file);
|
|
71
|
-
return { mutations, opaque, readOnly };
|
|
72
|
+
return { mutations, opaque, readOnly, scan };
|
|
72
73
|
}
|
|
73
74
|
if (sub === 'checkout' || sub === 'restore') {
|
|
74
75
|
const separator = args.indexOf('--');
|
|
75
76
|
if (separator !== -1) {
|
|
76
77
|
for (const file of args.slice(separator + 1)) add('update', file);
|
|
77
|
-
return { mutations, opaque, readOnly };
|
|
78
|
+
return { mutations, opaque, readOnly, scan };
|
|
78
79
|
}
|
|
79
80
|
if (sub === 'restore') {
|
|
80
81
|
for (const file of nonFlagArgs(args.slice(1))) add('update', file);
|
|
81
|
-
return { mutations, opaque, readOnly };
|
|
82
|
+
return { mutations, opaque, readOnly, scan };
|
|
82
83
|
}
|
|
83
|
-
return { mutations, opaque: true, readOnly: false };
|
|
84
|
+
return { mutations, opaque: true, readOnly: false, scan: true };
|
|
84
85
|
}
|
|
85
|
-
if (GIT_OPAQUE[sub] === true) return { mutations, opaque: true, readOnly: false };
|
|
86
|
+
if (GIT_OPAQUE[sub] === true) return { mutations, opaque: true, readOnly: false, scan: true };
|
|
86
87
|
|
|
87
|
-
return { mutations, opaque: true, readOnly: false };
|
|
88
|
+
return { mutations, opaque: true, readOnly: false, scan: true };
|
|
88
89
|
}
|
|
@@ -41,10 +41,18 @@ export function tokenizeCommand(script: string): Token[] {
|
|
|
41
41
|
|
|
42
42
|
for (let i = 0; i < script.length; i++) {
|
|
43
43
|
const ch = script[i];
|
|
44
|
-
if (ch === ' ' || ch === '\t' || ch === '\
|
|
44
|
+
if (ch === ' ' || ch === '\t' || ch === '\r') {
|
|
45
45
|
flush();
|
|
46
46
|
continue;
|
|
47
47
|
}
|
|
48
|
+
// A newline is a command separator, not whitespace: `printf > a` and
|
|
49
|
+
// `python3 - <<PY` on separate lines are two commands, and merging them
|
|
50
|
+
// made the second one invisible to the classifier.
|
|
51
|
+
if (ch === '\n') {
|
|
52
|
+
flush();
|
|
53
|
+
tokens.push({ kind: 'op', value: ';' });
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
48
56
|
if (ch === '#' && !hasWord) {
|
|
49
57
|
while (i < script.length && script[i] !== '\n') i++;
|
|
50
58
|
continue;
|
|
@@ -60,6 +60,8 @@ export interface ChangeInput {
|
|
|
60
60
|
source: JournalChangeRecord['source'];
|
|
61
61
|
command?: string;
|
|
62
62
|
exit?: number;
|
|
63
|
+
/** Why a side is `undefined`; defaults to the blob cap. */
|
|
64
|
+
unavailableReason?: 'too-large' | 'snapshot-race';
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
export class ChangeJournal {
|
|
@@ -96,7 +98,7 @@ export class ChangeJournal {
|
|
|
96
98
|
type: 'note',
|
|
97
99
|
at: input.at,
|
|
98
100
|
...(input.toolCallId ? { toolCallId: input.toolCallId } : {}),
|
|
99
|
-
reason: 'too-large',
|
|
101
|
+
reason: input.unavailableReason ?? 'too-large',
|
|
100
102
|
detail: input.path,
|
|
101
103
|
});
|
|
102
104
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The omp journal as a fallback for a raced `write` snapshot.
|
|
8
|
+
*
|
|
9
|
+
* omp records `write`/`edit` before/after blobs itself, and it reads the file
|
|
10
|
+
* inside the tool — before the write — so its `before` is exact where Kabmo's
|
|
11
|
+
* start-frame snapshot can lose the race. When a `write`'s before and after are
|
|
12
|
+
* equal, the capture asks this module for the omp record of the same path near
|
|
13
|
+
* the call's start time and reads its `before` blob.
|
|
14
|
+
*
|
|
15
|
+
* A missing record answers `undefined` (unknown), not `null` (absent): the
|
|
16
|
+
* caller must not turn "I could not find it" into "the file did not exist".
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { readBlob } from '@/server/lib/changes/blobs.server';
|
|
21
|
+
import { readSessionChangeIndex } from '@/server/lib/omp/session/changes';
|
|
22
|
+
import { findSessionFileById } from '@/server/lib/omp/session/locator';
|
|
23
|
+
import { siblingDirForSession } from '@/server/lib/omp/subagent/history/paths';
|
|
24
|
+
|
|
25
|
+
/** How far the omp record's write time may sit from the call's start frame. */
|
|
26
|
+
const MATCH_WINDOW_MS = 10_000;
|
|
27
|
+
|
|
28
|
+
export async function readOmpBefore(
|
|
29
|
+
sessionId: string,
|
|
30
|
+
absolute: string,
|
|
31
|
+
at: number,
|
|
32
|
+
): Promise<string | null | undefined> {
|
|
33
|
+
const records = await readSessionChangeIndex(sessionId);
|
|
34
|
+
let best: { before: string | null | undefined; delta: number } | null = null;
|
|
35
|
+
for (const record of records) {
|
|
36
|
+
if (record.path !== absolute) continue;
|
|
37
|
+
if (record.before === undefined) continue;
|
|
38
|
+
const delta = Math.abs(record.at - at);
|
|
39
|
+
if (delta > MATCH_WINDOW_MS) continue;
|
|
40
|
+
if (!best || delta < best.delta) best = { before: record.before, delta };
|
|
41
|
+
}
|
|
42
|
+
if (!best) return undefined;
|
|
43
|
+
if (best.before === null) return null;
|
|
44
|
+
|
|
45
|
+
const sessionFile = await findSessionFileById(sessionId);
|
|
46
|
+
if (!sessionFile) return undefined;
|
|
47
|
+
return readBlob(path.join(siblingDirForSession(sessionFile), 'blobs'), best.before);
|
|
48
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The omp journal as the `write` race fallback.
|
|
8
|
+
*
|
|
9
|
+
* omp reads the file inside the tool, before the write, so its `before` blob is
|
|
10
|
+
* exact where Kabmo's start-frame snapshot can lose the race. The three answers
|
|
11
|
+
* matter: a string (the before content), `null` (the file did not exist), and
|
|
12
|
+
* `undefined` (no matching record — the caller must not read that as absent).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
|
|
16
|
+
import * as fsp from 'node:fs/promises';
|
|
17
|
+
import * as os from 'node:os';
|
|
18
|
+
import * as path from 'node:path';
|
|
19
|
+
import { readOmpBefore } from '@/server/lib/changes/omp-fallback.server';
|
|
20
|
+
import { clearSessionFileCaches } from '@/server/lib/omp/session/files';
|
|
21
|
+
|
|
22
|
+
const SESSION = 'omp-fallback-1';
|
|
23
|
+
let agentDir = '';
|
|
24
|
+
let original: string | undefined;
|
|
25
|
+
|
|
26
|
+
beforeAll(async () => {
|
|
27
|
+
original = Bun.env.PI_CODING_AGENT_DIR;
|
|
28
|
+
agentDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'kabmo-omp-fallback-'));
|
|
29
|
+
Bun.env.PI_CODING_AGENT_DIR = agentDir;
|
|
30
|
+
clearSessionFileCaches();
|
|
31
|
+
|
|
32
|
+
const projectDir = path.join(agentDir, 'sessions', '-repo');
|
|
33
|
+
await fsp.mkdir(projectDir, { recursive: true });
|
|
34
|
+
const sessionFile = path.join(projectDir, `2026-01-01T00-00-00-000Z_${SESSION}.jsonl`);
|
|
35
|
+
await fsp.writeFile(
|
|
36
|
+
sessionFile,
|
|
37
|
+
`${JSON.stringify({ type: 'session', version: 3, id: SESSION, timestamp: '2026-01-01T00:00:00.000Z', cwd: '/repo' })}\n`,
|
|
38
|
+
);
|
|
39
|
+
const journalDir = sessionFile.slice(0, -'.jsonl'.length);
|
|
40
|
+
await fsp.mkdir(path.join(journalDir, 'blobs'), { recursive: true });
|
|
41
|
+
await fsp.writeFile(path.join(journalDir, 'blobs', 'b1'), Bun.gzipSync(Buffer.from('old', 'utf8')));
|
|
42
|
+
await fsp.writeFile(path.join(journalDir, 'blobs', 'b2'), Bun.gzipSync(Buffer.from('new', 'utf8')));
|
|
43
|
+
await fsp.writeFile(
|
|
44
|
+
path.join(journalDir, 'changes.jsonl'),
|
|
45
|
+
[
|
|
46
|
+
JSON.stringify({ type: 'change', at: 1000, agent: 'Main', tool: 'write', path: '/repo/a.ts', before: 'b1', after: 'b2' }),
|
|
47
|
+
JSON.stringify({ type: 'change', at: 2000, agent: 'Main', tool: 'write', path: '/repo/new.ts', before: null, after: 'b2' }),
|
|
48
|
+
].join('\n') + '\n',
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterAll(async () => {
|
|
53
|
+
if (original === undefined) delete Bun.env.PI_CODING_AGENT_DIR;
|
|
54
|
+
else Bun.env.PI_CODING_AGENT_DIR = original;
|
|
55
|
+
clearSessionFileCaches();
|
|
56
|
+
await fsp.rm(agentDir, { recursive: true, force: true });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('readOmpBefore', () => {
|
|
60
|
+
test('returns the before content of the matching record', async () => {
|
|
61
|
+
expect(await readOmpBefore(SESSION, '/repo/a.ts', 1000)).toBe('old');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('returns null when the file did not exist', async () => {
|
|
65
|
+
expect(await readOmpBefore(SESSION, '/repo/new.ts', 2000)).toBeNull();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('returns undefined outside the match window', async () => {
|
|
69
|
+
expect(await readOmpBefore(SESSION, '/repo/a.ts', 20_000)).toBeUndefined();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('returns undefined for a path with no record', async () => {
|
|
73
|
+
expect(await readOmpBefore(SESSION, '/repo/missing.ts', 1000)).toBeUndefined();
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -215,6 +215,34 @@ async function resolveMergedBlob(
|
|
|
215
215
|
return dir ? readBlob(dir, key) : undefined;
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
+
/** The last record for one path, for the scan's same-content dedupe. */
|
|
219
|
+
export async function readSessionChangeLast(
|
|
220
|
+
sessionId: string,
|
|
221
|
+
absolute: string,
|
|
222
|
+
): Promise<{ at: number; after: string | null | undefined } | null> {
|
|
223
|
+
const records = await readJournalRecords(sessionId);
|
|
224
|
+
let last: JournalChangeRecord | null = null;
|
|
225
|
+
for (const record of records) {
|
|
226
|
+
if (record.path !== absolute) continue;
|
|
227
|
+
if (!last || record.at >= last.at) last = record;
|
|
228
|
+
}
|
|
229
|
+
return last ? { at: last.at, after: last.after } : null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The paths one session recorded inside `[from, to]`; the scan's cross-check. */
|
|
233
|
+
export async function readSessionChangePaths(
|
|
234
|
+
sessionId: string,
|
|
235
|
+
from: number,
|
|
236
|
+
to: number,
|
|
237
|
+
): Promise<Set<string>> {
|
|
238
|
+
const records = await readJournalRecords(sessionId);
|
|
239
|
+
const paths = new Set<string>();
|
|
240
|
+
for (const record of records) {
|
|
241
|
+
if (record.at >= from && record.at <= to) paths.add(record.path);
|
|
242
|
+
}
|
|
243
|
+
return paths;
|
|
244
|
+
}
|
|
245
|
+
|
|
218
246
|
/** Every change record of one session, oldest first, with display paths. */
|
|
219
247
|
export async function readSessionChangeIndex(sessionId: string): Promise<SessionChangeRecord[]> {
|
|
220
248
|
const records = await readMergedRecords(sessionId, false);
|
|
@@ -18,11 +18,13 @@
|
|
|
18
18
|
* there, answers `null` and the caller writes a note instead of a guess.
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
21
22
|
import path from 'node:path';
|
|
22
23
|
import { childEnv } from '@/server/lib/fs/child-env';
|
|
23
24
|
|
|
24
25
|
const SCAN_BUDGET_FILES = 200;
|
|
25
26
|
const GIT_TIMEOUT_MS = 5_000;
|
|
27
|
+
const DIR_SCAN_MAX_BYTES = 10 * 1024 * 1024;
|
|
26
28
|
|
|
27
29
|
export interface GitScan {
|
|
28
30
|
root: string;
|
|
@@ -69,7 +71,9 @@ async function readText(absolute: string): Promise<string | null> {
|
|
|
69
71
|
export async function takeGitScan(cwd: string): Promise<GitScan | null> {
|
|
70
72
|
const root = (await runGit(cwd, ['rev-parse', '--show-toplevel']))?.trim();
|
|
71
73
|
if (!root) return null;
|
|
72
|
-
|
|
74
|
+
// `--no-optional-locks` keeps a concurrent scan from taking the index lock:
|
|
75
|
+
// two sessions in one repo must not make each other's `git status` fail.
|
|
76
|
+
const status = await runGit(root, ['status', '--porcelain=v1', '-z', '--no-optional-locks']);
|
|
73
77
|
if (status === null) return null;
|
|
74
78
|
|
|
75
79
|
const entries = parseStatus(status);
|
|
@@ -81,6 +85,57 @@ export async function takeGitScan(cwd: string): Promise<GitScan | null> {
|
|
|
81
85
|
return { root, dirty, truncated: entries.length > SCAN_BUDGET_FILES };
|
|
82
86
|
}
|
|
83
87
|
|
|
88
|
+
/**
|
|
89
|
+
* A non-git fallback: the cwd's own files and their content, bounded.
|
|
90
|
+
*
|
|
91
|
+
* A `python3 script.py` in a temp directory writes files no `argv` names, and
|
|
92
|
+
* there is no git index to ask. The directory scan is the only reconciliation
|
|
93
|
+
* available there; it is bounded to 200 files and 10 MB, and a binary file is
|
|
94
|
+
* skipped rather than journaled as a text diff.
|
|
95
|
+
*/
|
|
96
|
+
export async function takeDirScan(cwd: string): Promise<GitScan | null> {
|
|
97
|
+
const files: string[] = [];
|
|
98
|
+
let truncated = await walkFiles(cwd, files, SCAN_BUDGET_FILES);
|
|
99
|
+
const dirty = new Map<string, string | null>();
|
|
100
|
+
let bytes = 0;
|
|
101
|
+
for (const absolute of files) {
|
|
102
|
+
const info = await stat(absolute).catch(() => null);
|
|
103
|
+
if (!info) continue;
|
|
104
|
+
if (bytes + info.size > DIR_SCAN_MAX_BYTES) {
|
|
105
|
+
truncated = true;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const text = await readText(absolute);
|
|
109
|
+
if (text === null || text.includes('\0')) continue;
|
|
110
|
+
bytes += info.size;
|
|
111
|
+
dirty.set(absolute, text);
|
|
112
|
+
}
|
|
113
|
+
return { root: cwd, dirty, truncated };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Recursive file walk, bounded; `node_modules` and `.git` are skipped. */
|
|
117
|
+
async function walkFiles(dir: string, out: string[], budget: number): Promise<boolean> {
|
|
118
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
119
|
+
for (const entry of entries) {
|
|
120
|
+
if (out.length >= budget) return true;
|
|
121
|
+
if (entry.name === 'node_modules' || entry.name === '.git') continue;
|
|
122
|
+
const absolute = path.join(dir, entry.name);
|
|
123
|
+
if (entry.isDirectory()) {
|
|
124
|
+
if (await walkFiles(absolute, out, budget)) return true;
|
|
125
|
+
} else if (entry.isFile()) {
|
|
126
|
+
out.push(absolute);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Git first (exact for tracked files), then the directory scan. */
|
|
133
|
+
export async function takeScan(cwd: string): Promise<GitScan | null> {
|
|
134
|
+
const git = await takeGitScan(cwd);
|
|
135
|
+
if (git) return git;
|
|
136
|
+
return takeDirScan(cwd);
|
|
137
|
+
}
|
|
138
|
+
|
|
84
139
|
/** The content at HEAD for a path that was clean; `null` when it did not exist. */
|
|
85
140
|
export async function readHeadContent(root: string, absolute: string): Promise<string | null> {
|
|
86
141
|
const relative = path.relative(root, absolute);
|