@kabmo-labs/kabmo 0.38.0 → 0.38.1
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 +420 -419
- package/dist/client/static/js/{CsvGrid-90zfbn7f.js → CsvGrid-t1n057vw.js} +1 -1
- package/dist/client/static/js/{DocxView-vwq2rf16.js → DocxView-c9g6teks.js} +1 -1
- package/dist/client/static/js/{DrawioView-bvx7eqcz.js → DrawioView-gd14j54m.js} +1 -1
- package/dist/client/static/js/{ImageView-q6xfmbrq.js → ImageView-57f1ecdp.js} +1 -1
- package/dist/client/static/js/{LayoutWrapper-qatdaw3n.js → LayoutWrapper-x7srbpz1.js} +1 -1
- package/dist/client/static/js/{Modal-6jx1fvfb.js → Modal-0bn3f77b.js} +1 -1
- package/dist/client/static/js/{PdfView-tkv2d2zs.js → PdfView-cybz0xbj.js} +1 -1
- package/dist/client/static/js/{SessionsView-rbnecykz.js → SessionsView-gx5q68tp.js} +1 -1
- package/dist/client/static/js/{SheetView-t08g8kdr.js → SheetView-1kk5y326.js} +1 -1
- package/dist/client/static/js/{SvgPreview-9vh6mdcp.js → SvgPreview-mp4tk37p.js} +1 -1
- package/dist/client/static/js/{abnfDiagram-O67JEVCF-bxcn33e1.js → abnfDiagram-O67JEVCF-78qb3can.js} +1 -1
- package/dist/client/static/js/{architectureDiagram-NJMV4G6O-40ggw6e8.js → architectureDiagram-NJMV4G6O-n0fr4hb6.js} +1 -1
- package/dist/client/static/js/{blockDiagram-BEXU5L5S-k41cvjrd.js → blockDiagram-BEXU5L5S-2xemfrzh.js} +1 -1
- package/dist/client/static/js/{c4Diagram-YGBWAQC7-rdcmn6ac.js → c4Diagram-YGBWAQC7-zxfdx3eb.js} +1 -1
- package/dist/client/static/js/{classDiagram-v2-NBCMYWYE-txbmbkxv.js → classDiagram-v2-NBCMYWYE-z9sasy3k.js} +1 -1
- package/dist/client/static/js/{cose-bilkent-JH36ORCC-bxzh3qj1.js → cose-bilkent-JH36ORCC-9byp0czh.js} +1 -1
- package/dist/client/static/js/{cynefinDiagram-VND7K2PF-abx5343b.js → cynefinDiagram-VND7K2PF-gnm129fx.js} +1 -1
- package/dist/client/static/js/{dagre-6A5THRUB-baj278kt.js → dagre-6A5THRUB-sc5tfv1w.js} +1 -1
- package/dist/client/static/js/{diagram-22UHCM2B-vqvhdc5m.js → diagram-22UHCM2B-hq11eek7.js} +1 -1
- package/dist/client/static/js/{diagram-3UASUU5V-w328rfx0.js → diagram-3UASUU5V-7bk532gd.js} +1 -1
- package/dist/client/static/js/{diagram-ATOU4E4O-zzcmwnm8.js → diagram-ATOU4E4O-yt8wxttm.js} +1 -1
- package/dist/client/static/js/{diagram-CDSNMT55-8yw39mmj.js → diagram-CDSNMT55-n3978w6p.js} +1 -1
- package/dist/client/static/js/{diagram-MLGK6HIB-zgc386pv.js → diagram-MLGK6HIB-004x4gyw.js} +1 -1
- package/dist/client/static/js/{diagram-MPIPVDR6-efmvvscm.js → diagram-MPIPVDR6-498nt489.js} +1 -1
- package/dist/client/static/js/{dockerfile-bp5dre33.js → dockerfile-6hbcmkc8.js} +1 -1
- package/dist/client/static/js/{ebnfDiagram-ZINNZB2B-p66h0dzq.js → ebnfDiagram-ZINNZB2B-5fkmymqn.js} +1 -1
- package/dist/client/static/js/{elk-276RUBZZ-g0jjxcqd.js → elk-276RUBZZ-6xrfa2kd.js} +1 -1
- package/dist/client/static/js/{erDiagram-OPXOYQCR-39gat96z.js → erDiagram-OPXOYQCR-byz4zbkn.js} +1 -1
- package/dist/client/static/js/flowDiagram-KWPJA3E3-s3fbfwrz.js +1 -0
- package/dist/client/static/js/{ganttDiagram-FUAMR5RP-nsjv1zt4.js → ganttDiagram-FUAMR5RP-4fkw1k01.js} +1 -1
- package/dist/client/static/js/{gitGraphDiagram-X574FWY7-r4vnkcy3.js → gitGraphDiagram-X574FWY7-19g84yw8.js} +1 -1
- package/dist/client/static/js/{index-hzx81cjq.js → index-0v8b5pzb.js} +1 -1
- package/dist/client/static/js/{index-h8h5c2r8.js → index-1j1s91c4.js} +1 -1
- package/dist/client/static/js/{index-6ax2z70w.js → index-20vhkcb9.js} +1 -1
- package/dist/client/static/js/{index-st202rjv.js → index-3wpsr2bc.js} +7 -7
- package/dist/client/static/js/{index-73jwm4d5.js → index-3y6krykq.js} +1 -1
- package/dist/client/static/js/{index-1qszrvzk.js → index-4jbbdq6n.js} +1 -1
- package/dist/client/static/js/{index-tfsaj5d1.js → index-4qt2e84b.js} +1 -1
- package/dist/client/static/js/{index-da10qpns.js → index-57z8mtmx.js} +1 -1
- package/dist/client/static/js/{index-ha75kff0.js → index-5gn56236.js} +1 -1
- package/dist/client/static/js/{index-k9xsvab3.js → index-74scnfpk.js} +1 -1
- package/dist/client/static/js/{index-dkseg29t.js → index-7kafzae0.js} +2 -2
- package/dist/client/static/js/{index-ra38ggke.js → index-89a2702q.js} +1 -1
- package/dist/client/static/js/{index-tv61v3t3.js → index-8j90mdgz.js} +1 -1
- package/dist/client/static/js/{index-50z4ek83.js → index-baet2nv8.js} +1 -1
- package/dist/client/static/js/{index-a74k07z2.js → index-bhng2dax.js} +1 -1
- package/dist/client/static/js/{index-24yyj78e.js → index-cj9193d5.js} +1 -1
- package/dist/client/static/js/{index-5ebhb94w.js → index-dd4q54y0.js} +1 -1
- package/dist/client/static/js/{index-dfgdn3fj.js → index-e3w46vsc.js} +1 -1
- package/dist/client/static/js/{index-rgp69pa8.js → index-e99ec2f0.js} +1 -1
- package/dist/client/static/js/{index-2ndq1ngv.js → index-ehnztdd2.js} +1 -1
- package/dist/client/static/js/{index-rqrcw8nj.js → index-epbqzayj.js} +1 -1
- package/dist/client/static/js/{index-vnn7m9yp.js → index-fpf1jkwd.js} +1 -1
- package/dist/client/static/js/{index-vxd4gykh.js → index-g252205p.js} +1 -1
- package/dist/client/static/js/{index-11bdd235.js → index-gxtgyya0.js} +1 -1
- package/dist/client/static/js/{index-tzm90bsz.js → index-j314me09.js} +1 -1
- package/dist/client/static/js/{index-gwhx66f4.js → index-jbwns6sc.js} +1 -1
- package/dist/client/static/js/{index-c15r8jd3.js → index-kdgcbt17.js} +1 -1
- package/dist/client/static/js/{index-rf0p3v7q.js → index-m4bjsyj2.js} +1 -1
- package/dist/client/static/js/{index-2drjp9ks.js → index-pfwd0160.js} +1 -1
- package/dist/client/static/js/{index-e1pr3k50.js → index-qntytvt0.js} +1 -1
- package/dist/client/static/js/{index-z0cpp5bc.js → index-rgdhyry3.js} +1 -1
- package/dist/client/static/js/{index-9wtg6ywn.js → index-sjnnv1gd.js} +1 -1
- package/dist/client/static/js/{index-5y0bvq4q.js → index-zj45wj1x.js} +2 -2
- package/dist/client/static/js/{index-0kxqzafy.js → index-zw7qjx65.js} +1 -1
- package/dist/client/static/js/{infoDiagram-VRGFBTTK-e39pvm4j.js → infoDiagram-VRGFBTTK-aw6ter0a.js} +1 -1
- package/dist/client/static/js/{ishikawaDiagram-OU5B5YK6-rqhszgvp.js → ishikawaDiagram-OU5B5YK6-62gst5wh.js} +1 -1
- package/dist/client/static/js/{journeyDiagram-ZHPQQLJL-wgy5qrsv.js → journeyDiagram-ZHPQQLJL-6kgv5a4j.js} +1 -1
- package/dist/client/static/js/{kanban-definition-PNTS6WVX-b8bxn0h1.js → kanban-definition-PNTS6WVX-8p3515b7.js} +1 -1
- package/dist/client/static/js/{mermaid.core-vb5ehtre.js → mermaid.core-b6ga2y8y.js} +2 -2
- package/dist/client/static/js/{mindmap-definition-NLK3R4M7-t4p38s38.js → mindmap-definition-NLK3R4M7-na9e9b4a.js} +1 -1
- package/dist/client/static/js/one-dark-pro-xp6ssk4z.js +1 -0
- package/dist/client/static/js/one-light-xgab7b02.js +1 -0
- package/dist/client/static/js/{pegDiagram-GJSIUBJH-3gp4madx.js → pegDiagram-GJSIUBJH-d1hk7fnq.js} +1 -1
- package/dist/client/static/js/{pieDiagram-5QR66LMP-0cbm773h.js → pieDiagram-5QR66LMP-7b9f4x0y.js} +1 -1
- package/dist/client/static/js/{quadrantDiagram-O4NWA36T-jwg7fc9f.js → quadrantDiagram-O4NWA36T-q4wxgmhg.js} +1 -1
- package/dist/client/static/js/{railroadDiagram-XR7U4H2S-38xh691s.js → railroadDiagram-XR7U4H2S-jngq1qx4.js} +1 -1
- package/dist/client/static/js/{requirementDiagram-PLB6GJNP-gpddawxh.js → requirementDiagram-PLB6GJNP-6xph3b72.js} +1 -1
- package/dist/client/static/js/{rust-bx3ecv4n.js → rust-w6hen2h4.js} +1 -1
- package/dist/client/static/js/{sankeyDiagram-IPEJSGJF-s6t6bbhk.js → sankeyDiagram-IPEJSGJF-ncfzgdj8.js} +1 -1
- package/dist/client/static/js/{sequenceDiagram-PO4LG4MO-wkq51pmb.js → sequenceDiagram-PO4LG4MO-sqz0yfg0.js} +1 -1
- package/dist/client/static/js/{sizeCapture-INFHLROL-xtwexf8k.js → sizeCapture-INFHLROL-vt8qp04d.js} +1 -1
- package/dist/client/static/js/{stateDiagram-v2-GCMORJYK-jnwsqa7y.js → stateDiagram-v2-GCMORJYK-myjtrmfe.js} +1 -1
- package/dist/client/static/js/{swimlanes-2SLR337P-mnkkqcbx.js → swimlanes-2SLR337P-z9vx7z4j.js} +1 -1
- package/dist/client/static/js/{swimlanesDiagram-TC7HE7FX-6xmrz03n.js → swimlanesDiagram-TC7HE7FX-5vha7thx.js} +1 -1
- package/dist/client/static/js/{timeline-definition-EJHVYXUP-rxq6heqh.js → timeline-definition-EJHVYXUP-z815np57.js} +1 -1
- package/dist/client/static/js/{usecaseDiagram-POWQR4AR-dz94f57t.js → usecaseDiagram-POWQR4AR-06ntggng.js} +1 -1
- package/dist/client/static/js/{vennDiagram-UO4OBE2U-5mmvkhsq.js → vennDiagram-UO4OBE2U-nmtaf5rp.js} +1 -1
- package/dist/client/static/js/{wardleyDiagram-VNRHLVJA-dctdntpa.js → wardleyDiagram-VNRHLVJA-zgsxmak8.js} +1 -1
- package/dist/client/static/js/{xychartDiagram-PMCCYNJV-njyfrqkv.js → xychartDiagram-PMCCYNJV-8bg9qdra.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 -2
- 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 +32 -16
- package/src/server/lib/changes/capture.test.ts +48 -0
- 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 +51 -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
|
@@ -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,52 @@ 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
|
+
let entries;
|
|
98
|
+
try {
|
|
99
|
+
entries = await readdir(cwd, { withFileTypes: true });
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const dirty = new Map<string, string | null>();
|
|
104
|
+
let bytes = 0;
|
|
105
|
+
let truncated = false;
|
|
106
|
+
for (const entry of entries) {
|
|
107
|
+
if (!entry.isFile()) continue;
|
|
108
|
+
if (dirty.size >= SCAN_BUDGET_FILES) {
|
|
109
|
+
truncated = true;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
const absolute = path.join(cwd, entry.name);
|
|
113
|
+
const info = await stat(absolute).catch(() => null);
|
|
114
|
+
if (!info) continue;
|
|
115
|
+
if (bytes + info.size > DIR_SCAN_MAX_BYTES) {
|
|
116
|
+
truncated = true;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const text = await readText(absolute);
|
|
120
|
+
if (text === null || text.includes('\0')) continue;
|
|
121
|
+
bytes += info.size;
|
|
122
|
+
dirty.set(absolute, text);
|
|
123
|
+
}
|
|
124
|
+
return { root: cwd, dirty, truncated };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Git first (exact for tracked files), then the directory scan. */
|
|
128
|
+
export async function takeScan(cwd: string): Promise<GitScan | null> {
|
|
129
|
+
const git = await takeGitScan(cwd);
|
|
130
|
+
if (git) return git;
|
|
131
|
+
return takeDirScan(cwd);
|
|
132
|
+
}
|
|
133
|
+
|
|
84
134
|
/** The content at HEAD for a path that was clean; `null` when it did not exist. */
|
|
85
135
|
export async function readHeadContent(root: string, absolute: string): Promise<string | null> {
|
|
86
136
|
const relative = path.relative(root, absolute);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Which sessions share a working directory.
|
|
8
|
+
*
|
|
9
|
+
* The scan layer is the one place a concurrent session can be misattributed: a
|
|
10
|
+
* `git status` or a directory diff sees the other session's file too. Before a
|
|
11
|
+
* scan records a path, it asks this module for the OTHER sessions in the same
|
|
12
|
+
* cwd and skips a path one of them already recorded in the window.
|
|
13
|
+
*
|
|
14
|
+
* The cwd comes from each journal's `session` header, so the map is stable for
|
|
15
|
+
* a session's lifetime. It is cached by the `changes/` root's path + mtime: a
|
|
16
|
+
* new session directory changes the mtime, and a different data root changes
|
|
17
|
+
* the path.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import { changesRoot } from '@/server/lib/changes/paths.server';
|
|
23
|
+
import { parseJournalLine } from '@/server/lib/changes/records';
|
|
24
|
+
|
|
25
|
+
interface CwdCache {
|
|
26
|
+
root: string;
|
|
27
|
+
rootMtime: number;
|
|
28
|
+
byCwd: Map<string, string[]>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare global {
|
|
32
|
+
// eslint-disable-next-line no-var
|
|
33
|
+
var __kabmoSessionsByCwd: CwdCache | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Session ids whose journal header names `cwd`, excluding `excludeSessionId`. */
|
|
37
|
+
export async function sessionsInCwd(cwd: string, excludeSessionId: string): Promise<string[]> {
|
|
38
|
+
const root = changesRoot();
|
|
39
|
+
const info = await stat(root).catch(() => null);
|
|
40
|
+
if (!info) return [];
|
|
41
|
+
|
|
42
|
+
let cache = globalThis.__kabmoSessionsByCwd;
|
|
43
|
+
if (!cache || cache.root !== root || cache.rootMtime !== info.mtimeMs) {
|
|
44
|
+
cache = { root, rootMtime: info.mtimeMs, byCwd: await buildByCwd(root) };
|
|
45
|
+
globalThis.__kabmoSessionsByCwd = cache;
|
|
46
|
+
}
|
|
47
|
+
return (cache.byCwd.get(cwd) ?? []).filter((id) => id !== excludeSessionId);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function buildByCwd(root: string): Promise<Map<string, string[]>> {
|
|
51
|
+
const byCwd = new Map<string, string[]>();
|
|
52
|
+
const entries = await readdir(root, { withFileTypes: true }).catch(() => []);
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
if (!entry.isDirectory()) continue;
|
|
55
|
+
const file = path.join(root, entry.name, 'changes.jsonl');
|
|
56
|
+
// The header is the first line; a slice keeps a 20 MB journal cheap to read.
|
|
57
|
+
const head = await Bun.file(file).slice(0, 1024).text().catch(() => '');
|
|
58
|
+
const record = parseJournalLine(head.split('\n')[0] ?? '');
|
|
59
|
+
if (record?.type !== 'session') continue;
|
|
60
|
+
const list = byCwd.get(record.cwd) ?? [];
|
|
61
|
+
list.push(entry.name);
|
|
62
|
+
byCwd.set(record.cwd, list);
|
|
63
|
+
}
|
|
64
|
+
return byCwd;
|
|
65
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Which sessions share a cwd, from each journal's `session` header.
|
|
8
|
+
*
|
|
9
|
+
* The scan's cross-check depends on this: a path another session in the same
|
|
10
|
+
* cwd already recorded must not be attributed to the current command.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
|
|
14
|
+
import * as fsp from 'node:fs/promises';
|
|
15
|
+
import * as os from 'node:os';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
import { ChangeJournal } from '@/server/lib/changes/journal.server';
|
|
18
|
+
import { sessionsInCwd } from '@/server/lib/changes/sessions.server';
|
|
19
|
+
|
|
20
|
+
let dataDir = '';
|
|
21
|
+
let original: string | undefined;
|
|
22
|
+
|
|
23
|
+
beforeAll(async () => {
|
|
24
|
+
original = Bun.env.KABMO_DATA_DIR;
|
|
25
|
+
dataDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'kabmo-sessions-'));
|
|
26
|
+
Bun.env.KABMO_DATA_DIR = dataDir;
|
|
27
|
+
const a = new ChangeJournal('sess-a', '/repo');
|
|
28
|
+
const b = new ChangeJournal('sess-b', '/repo');
|
|
29
|
+
const c = new ChangeJournal('sess-c', '/other');
|
|
30
|
+
await a.recordNote({ at: 1, reason: 'opaque-command' });
|
|
31
|
+
await b.recordNote({ at: 1, reason: 'opaque-command' });
|
|
32
|
+
await c.recordNote({ at: 1, reason: 'opaque-command' });
|
|
33
|
+
await Promise.all([a.flush(), b.flush(), c.flush()]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterAll(async () => {
|
|
37
|
+
if (original === undefined) delete Bun.env.KABMO_DATA_DIR;
|
|
38
|
+
else Bun.env.KABMO_DATA_DIR = original;
|
|
39
|
+
await fsp.rm(dataDir, { recursive: true, force: true });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('sessionsInCwd', () => {
|
|
43
|
+
test('returns the other sessions in the same cwd', async () => {
|
|
44
|
+
expect((await sessionsInCwd('/repo', 'sess-a')).sort()).toEqual(['sess-b']);
|
|
45
|
+
expect((await sessionsInCwd('/repo', 'sess-x')).sort()).toEqual(['sess-a', 'sess-b']);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('does not mix a different cwd', async () => {
|
|
49
|
+
expect(await sessionsInCwd('/other', 'sess-a')).toEqual(['sess-c']);
|
|
50
|
+
expect(await sessionsInCwd('/none', 'sess-a')).toEqual([]);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import"./index-5y0bvq4q.js";import"./index-c15r8jd3.js";import{ah}from"./index-dfgdn3fj.js";export{ah as diagram};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{MA}from"./index-st202rjv.js";export{MA as default};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LA}from"./index-st202rjv.js";export{LA as default};
|