@bakapiano/ccsm 0.10.3 → 0.11.0
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/CLAUDE.md +475 -475
- package/README.md +190 -190
- package/bin/ccsm.js +194 -194
- package/lib/cliSessionWatcher.js +249 -249
- package/lib/config.js +185 -185
- package/lib/folders.js +96 -96
- package/lib/localCliSessions.js +489 -177
- package/lib/persistedSessions.js +134 -134
- package/lib/webTerminal.js +208 -208
- package/lib/workspace.js +230 -255
- package/package.json +57 -57
- package/public/css/base.css +99 -99
- package/public/css/cards.css +183 -183
- package/public/css/feedback.css +303 -303
- package/public/css/forms.css +405 -405
- package/public/css/layout.css +160 -160
- package/public/css/modal.css +190 -183
- package/public/css/responsive.css +10 -10
- package/public/css/sidebar.css +616 -601
- package/public/css/terminals.css +294 -294
- package/public/css/tokens.css +81 -79
- package/public/css/wco.css +98 -98
- package/public/css/widgets.css +1596 -1375
- package/public/index.html +105 -103
- package/public/js/api.js +272 -260
- package/public/js/components/AdoptModal.js +343 -171
- package/public/js/components/App.js +35 -35
- package/public/js/components/DirectoryPicker.js +203 -203
- package/public/js/components/EntityFormModal.js +105 -105
- package/public/js/components/Modal.js +51 -51
- package/public/js/components/OfflineBanner.js +93 -93
- package/public/js/components/PageTitleBar.js +13 -13
- package/public/js/components/Picker.js +179 -179
- package/public/js/components/Popover.js +55 -55
- package/public/js/components/Sidebar.js +270 -270
- package/public/js/components/TerminalView.js +298 -298
- package/public/js/components/useDragSort.js +67 -67
- package/public/js/dialog.js +67 -67
- package/public/js/icons.js +177 -177
- package/public/js/main.js +140 -140
- package/public/js/pages/AboutPage.js +165 -165
- package/public/js/pages/ConfigurePage.js +475 -487
- package/public/js/pages/LaunchPage.js +369 -369
- package/public/js/pages/SessionsPage.js +97 -97
- package/public/js/state.js +231 -231
- package/public/manifest.webmanifest +15 -15
- package/scripts/install.js +137 -137
- package/server.js +1126 -1117
package/lib/cliSessionWatcher.js
CHANGED
|
@@ -1,249 +1,249 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Captures the upstream CLI's session id (claude / codex / copilot) so
|
|
4
|
-
// ccsm can later spawn `<cli> --resume <uuid>` and reattach to the same
|
|
5
|
-
// conversation precisely.
|
|
6
|
-
//
|
|
7
|
-
// Approach (poll-based, deliberately):
|
|
8
|
-
// - fs.watch is unreliable on Windows for in-place content writes.
|
|
9
|
-
// - CLIs reuse existing transcripts in the same cwd when they can —
|
|
10
|
-
// there's no "new file appears" signal to wait on.
|
|
11
|
-
// - Instead we poll the per-CLI transcript dir every POLL_MS, find
|
|
12
|
-
// candidates whose mtime > spawnAt, read each one's cwd field, and
|
|
13
|
-
// if exactly one matches our spawn cwd that's the session id.
|
|
14
|
-
// - Window expires after WINDOW_MS — CLIs only persist after the first
|
|
15
|
-
// user message, so this needs to be generous.
|
|
16
|
-
//
|
|
17
|
-
// Per-CLI profile shape:
|
|
18
|
-
// dirFor(cwd) → directory to poll
|
|
19
|
-
// entryType → 'file' (claude/codex) or 'dir' (copilot —
|
|
20
|
-
// each child dir = one session)
|
|
21
|
-
// recursive → for 'file' mode only; walk subdirs too
|
|
22
|
-
// filePattern → regex an entry's basename must match
|
|
23
|
-
// parseId(basename) → extract the upstream session id from name
|
|
24
|
-
// (returns null to skip)
|
|
25
|
-
// readCwd(entryPath) → async; returns the cwd recorded inside the
|
|
26
|
-
// session, or null if not yet readable.
|
|
27
|
-
|
|
28
|
-
const fs = require('node:fs');
|
|
29
|
-
const fsp = require('node:fs/promises');
|
|
30
|
-
const path = require('node:path');
|
|
31
|
-
const os = require('node:os');
|
|
32
|
-
const readline = require('node:readline');
|
|
33
|
-
|
|
34
|
-
const POLL_MS = 1_500;
|
|
35
|
-
const WINDOW_MS = 5 * 60_000;
|
|
36
|
-
|
|
37
|
-
const profiles = {
|
|
38
|
-
claude: {
|
|
39
|
-
dirFor: (cwd) => path.join(os.homedir(), '.claude', 'projects', claudeSlug(cwd)),
|
|
40
|
-
entryType: 'file',
|
|
41
|
-
filePattern: /\.jsonl$/i,
|
|
42
|
-
parseId: (filename) => filename.replace(/\.jsonl$/i, ''),
|
|
43
|
-
readCwd: (filepath) => firstJsonField(filepath, 'cwd', 12),
|
|
44
|
-
},
|
|
45
|
-
codex: {
|
|
46
|
-
dirFor: () => path.join(os.homedir(), '.codex', 'sessions'),
|
|
47
|
-
entryType: 'file',
|
|
48
|
-
recursive: true,
|
|
49
|
-
filePattern: /\.jsonl$/i,
|
|
50
|
-
parseId: (filename) => {
|
|
51
|
-
const m = filename.match(/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i);
|
|
52
|
-
return m ? m[1] : null;
|
|
53
|
-
},
|
|
54
|
-
readCwd: (filepath) => firstJsonField(filepath, 'cwd', 12),
|
|
55
|
-
},
|
|
56
|
-
copilot: {
|
|
57
|
-
// ~/.copilot/session-state/<uuid>/ with workspace.yaml + events.jsonl
|
|
58
|
-
// Each session is a directory, not a single file.
|
|
59
|
-
dirFor: () => path.join(os.homedir(), '.copilot', 'session-state'),
|
|
60
|
-
entryType: 'dir',
|
|
61
|
-
// Subdir name is the uuid; tolerate any non-empty name (copilot uses
|
|
62
|
-
// standard uuid4 but we'd rather not be strict).
|
|
63
|
-
parseId: (dirname) => /^[0-9a-f-]+$/i.test(dirname) ? dirname : null,
|
|
64
|
-
readCwd: async (dirpath) => {
|
|
65
|
-
// workspace.yaml has plain `cwd: <path>` on its own line — quick
|
|
66
|
-
// regex parse, no YAML dep.
|
|
67
|
-
const yaml = path.join(dirpath, 'workspace.yaml');
|
|
68
|
-
try {
|
|
69
|
-
const txt = await fsp.readFile(yaml, 'utf8');
|
|
70
|
-
const m = txt.match(/^\s*cwd\s*:\s*(.+?)\s*$/m);
|
|
71
|
-
return m ? m[1].trim() : null;
|
|
72
|
-
} catch {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
function captureSessionId({ cliType, cwd, onCapture, onTimeout, windowMs = WINDOW_MS }) {
|
|
80
|
-
const profile = profiles[cliType];
|
|
81
|
-
if (!profile) return () => {};
|
|
82
|
-
const dir = profile.dirFor(cwd);
|
|
83
|
-
const spawnAt = Date.now();
|
|
84
|
-
console.log(`[cliSessionWatcher] start ${cliType} dir=${dir} cwd=${cwd}`);
|
|
85
|
-
|
|
86
|
-
let stopped = false;
|
|
87
|
-
let captured = false;
|
|
88
|
-
let pollTimer = null;
|
|
89
|
-
let expireTimer = null;
|
|
90
|
-
// Track entries we've already proven aren't ours so we don't re-read them.
|
|
91
|
-
// Only used for "wrong cwd recorded inside the file" — that's stable.
|
|
92
|
-
// We do NOT cache mtime-based rejections: a stale file can be re-touched
|
|
93
|
-
// by the CLI later (claude appends to existing transcripts) and we want
|
|
94
|
-
// to re-evaluate it then.
|
|
95
|
-
const rejected = new Set();
|
|
96
|
-
|
|
97
|
-
const cleanup = () => {
|
|
98
|
-
if (stopped) return;
|
|
99
|
-
stopped = true;
|
|
100
|
-
if (pollTimer) clearTimeout(pollTimer);
|
|
101
|
-
if (expireTimer) clearTimeout(expireTimer);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const finish = (sessionId) => {
|
|
105
|
-
if (stopped) return;
|
|
106
|
-
captured = true;
|
|
107
|
-
cleanup();
|
|
108
|
-
console.log(`[cliSessionWatcher] captured ${cliType} ${sessionId}`);
|
|
109
|
-
try { onCapture?.(sessionId); } catch (e) { console.error('[cliSessionWatcher] onCapture:', e); }
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const onExpire = () => {
|
|
113
|
-
if (stopped || captured) return;
|
|
114
|
-
cleanup();
|
|
115
|
-
console.warn(`[cliSessionWatcher] timeout ${cliType} (no transcript in ${Math.round(windowMs / 1000)}s) cwd=${cwd}`);
|
|
116
|
-
try { onTimeout?.(); } catch (e) { console.error('[cliSessionWatcher] onTimeout:', e); }
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
const poll = async () => {
|
|
120
|
-
if (stopped) return;
|
|
121
|
-
try {
|
|
122
|
-
const entries = await listEntries(dir, profile);
|
|
123
|
-
console.log(`[cliSessionWatcher] poll: ${entries.length} entries, rejected=${rejected.size}`);
|
|
124
|
-
const candidates = [];
|
|
125
|
-
for (const entryPath of entries) {
|
|
126
|
-
const base = path.basename(entryPath);
|
|
127
|
-
if (rejected.has(entryPath)) continue;
|
|
128
|
-
if (profile.filePattern && !profile.filePattern.test(base)) continue;
|
|
129
|
-
const id = profile.parseId(base);
|
|
130
|
-
if (!id) continue;
|
|
131
|
-
let st;
|
|
132
|
-
try { st = await fsp.stat(entryPath); } catch { continue; }
|
|
133
|
-
// Mtime gate is re-evaluated every poll: don't memoise it. If the
|
|
134
|
-
// CLI later re-touches an old transcript (claude appends to the
|
|
135
|
-
// existing one for the cwd), this poll will pick it up.
|
|
136
|
-
if (st.mtimeMs < spawnAt - 2000) continue;
|
|
137
|
-
candidates.push({ entryPath, id, mtime: st.mtimeMs });
|
|
138
|
-
}
|
|
139
|
-
const matched = [];
|
|
140
|
-
for (const c of candidates) {
|
|
141
|
-
const cwdFromEntry = await profile.readCwd(c.entryPath);
|
|
142
|
-
if (cwdFromEntry == null) continue; // not enough data yet
|
|
143
|
-
if (!samePath(cwdFromEntry, cwd)) { rejected.add(c.entryPath); continue; }
|
|
144
|
-
matched.push(c);
|
|
145
|
-
}
|
|
146
|
-
if (matched.length === 1) {
|
|
147
|
-
finish(matched[0].id);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
if (matched.length > 1) {
|
|
151
|
-
console.warn(`[cliSessionWatcher] ambiguous: ${matched.length} candidates for ${cwd} — skipping capture`);
|
|
152
|
-
cleanup();
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
} catch (e) {
|
|
156
|
-
console.error('[cliSessionWatcher] poll:', e.message);
|
|
157
|
-
}
|
|
158
|
-
if (!stopped) pollTimer = setTimeout(poll, POLL_MS);
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
(async () => {
|
|
162
|
-
try { await fsp.mkdir(dir, { recursive: true }); } catch {}
|
|
163
|
-
if (stopped) return;
|
|
164
|
-
expireTimer = setTimeout(onExpire, windowMs);
|
|
165
|
-
poll();
|
|
166
|
-
})();
|
|
167
|
-
|
|
168
|
-
return cleanup;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
module.exports = { captureSessionId };
|
|
172
|
-
|
|
173
|
-
// ── helpers ─────────────────────────────────────────────────────────
|
|
174
|
-
|
|
175
|
-
async function listEntries(root, profile) {
|
|
176
|
-
if (profile.entryType === 'dir') {
|
|
177
|
-
let names;
|
|
178
|
-
try { names = await fsp.readdir(root, { withFileTypes: true }); }
|
|
179
|
-
catch { return []; }
|
|
180
|
-
return names.filter((e) => e.isDirectory()).map((e) => path.join(root, e.name));
|
|
181
|
-
}
|
|
182
|
-
// file mode
|
|
183
|
-
if (profile.recursive) return listAllFiles(root);
|
|
184
|
-
try {
|
|
185
|
-
const names = await fsp.readdir(root);
|
|
186
|
-
return names.map((n) => path.join(root, n));
|
|
187
|
-
} catch {
|
|
188
|
-
return [];
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function claudeSlug(cwd) {
|
|
193
|
-
return cwd.replace(/[:\\\/]/g, '-');
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function samePath(a, b) {
|
|
197
|
-
if (!a || !b) return false;
|
|
198
|
-
const norm = (p) => path.resolve(p).replace(/[\\\/]+$/, '').toLowerCase();
|
|
199
|
-
return norm(a) === norm(b);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async function firstJsonField(filepath, field, maxLines) {
|
|
203
|
-
return new Promise((resolve) => {
|
|
204
|
-
let stream;
|
|
205
|
-
try { stream = fs.createReadStream(filepath, { encoding: 'utf8' }); }
|
|
206
|
-
catch { resolve(null); return; }
|
|
207
|
-
const rl = readline.createInterface({ input: stream });
|
|
208
|
-
let count = 0;
|
|
209
|
-
// rl.close() synchronously emits 'close' on some platforms, which would
|
|
210
|
-
// re-enter done(null) and clobber the value resolve. Guard against that.
|
|
211
|
-
let settled = false;
|
|
212
|
-
const done = (v) => {
|
|
213
|
-
if (settled) return;
|
|
214
|
-
settled = true;
|
|
215
|
-
try { rl.close(); } catch {}
|
|
216
|
-
try { stream.destroy(); } catch {}
|
|
217
|
-
resolve(v);
|
|
218
|
-
};
|
|
219
|
-
rl.on('line', (line) => {
|
|
220
|
-
count++;
|
|
221
|
-
try {
|
|
222
|
-
const obj = JSON.parse(line);
|
|
223
|
-
if (obj && Object.prototype.hasOwnProperty.call(obj, field)) {
|
|
224
|
-
done(obj[field]);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
} catch {}
|
|
228
|
-
if (count >= maxLines) done(null);
|
|
229
|
-
});
|
|
230
|
-
rl.on('close', () => done(null));
|
|
231
|
-
rl.on('error', () => done(null));
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
async function listAllFiles(root) {
|
|
236
|
-
const out = [];
|
|
237
|
-
const walk = async (dir) => {
|
|
238
|
-
let entries;
|
|
239
|
-
try { entries = await fsp.readdir(dir, { withFileTypes: true }); }
|
|
240
|
-
catch { return; }
|
|
241
|
-
for (const e of entries) {
|
|
242
|
-
const p = path.join(dir, e.name);
|
|
243
|
-
if (e.isDirectory()) await walk(p);
|
|
244
|
-
else out.push(p);
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
await walk(root);
|
|
248
|
-
return out;
|
|
249
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Captures the upstream CLI's session id (claude / codex / copilot) so
|
|
4
|
+
// ccsm can later spawn `<cli> --resume <uuid>` and reattach to the same
|
|
5
|
+
// conversation precisely.
|
|
6
|
+
//
|
|
7
|
+
// Approach (poll-based, deliberately):
|
|
8
|
+
// - fs.watch is unreliable on Windows for in-place content writes.
|
|
9
|
+
// - CLIs reuse existing transcripts in the same cwd when they can —
|
|
10
|
+
// there's no "new file appears" signal to wait on.
|
|
11
|
+
// - Instead we poll the per-CLI transcript dir every POLL_MS, find
|
|
12
|
+
// candidates whose mtime > spawnAt, read each one's cwd field, and
|
|
13
|
+
// if exactly one matches our spawn cwd that's the session id.
|
|
14
|
+
// - Window expires after WINDOW_MS — CLIs only persist after the first
|
|
15
|
+
// user message, so this needs to be generous.
|
|
16
|
+
//
|
|
17
|
+
// Per-CLI profile shape:
|
|
18
|
+
// dirFor(cwd) → directory to poll
|
|
19
|
+
// entryType → 'file' (claude/codex) or 'dir' (copilot —
|
|
20
|
+
// each child dir = one session)
|
|
21
|
+
// recursive → for 'file' mode only; walk subdirs too
|
|
22
|
+
// filePattern → regex an entry's basename must match
|
|
23
|
+
// parseId(basename) → extract the upstream session id from name
|
|
24
|
+
// (returns null to skip)
|
|
25
|
+
// readCwd(entryPath) → async; returns the cwd recorded inside the
|
|
26
|
+
// session, or null if not yet readable.
|
|
27
|
+
|
|
28
|
+
const fs = require('node:fs');
|
|
29
|
+
const fsp = require('node:fs/promises');
|
|
30
|
+
const path = require('node:path');
|
|
31
|
+
const os = require('node:os');
|
|
32
|
+
const readline = require('node:readline');
|
|
33
|
+
|
|
34
|
+
const POLL_MS = 1_500;
|
|
35
|
+
const WINDOW_MS = 5 * 60_000;
|
|
36
|
+
|
|
37
|
+
const profiles = {
|
|
38
|
+
claude: {
|
|
39
|
+
dirFor: (cwd) => path.join(os.homedir(), '.claude', 'projects', claudeSlug(cwd)),
|
|
40
|
+
entryType: 'file',
|
|
41
|
+
filePattern: /\.jsonl$/i,
|
|
42
|
+
parseId: (filename) => filename.replace(/\.jsonl$/i, ''),
|
|
43
|
+
readCwd: (filepath) => firstJsonField(filepath, 'cwd', 12),
|
|
44
|
+
},
|
|
45
|
+
codex: {
|
|
46
|
+
dirFor: () => path.join(os.homedir(), '.codex', 'sessions'),
|
|
47
|
+
entryType: 'file',
|
|
48
|
+
recursive: true,
|
|
49
|
+
filePattern: /\.jsonl$/i,
|
|
50
|
+
parseId: (filename) => {
|
|
51
|
+
const m = filename.match(/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i);
|
|
52
|
+
return m ? m[1] : null;
|
|
53
|
+
},
|
|
54
|
+
readCwd: (filepath) => firstJsonField(filepath, 'cwd', 12),
|
|
55
|
+
},
|
|
56
|
+
copilot: {
|
|
57
|
+
// ~/.copilot/session-state/<uuid>/ with workspace.yaml + events.jsonl
|
|
58
|
+
// Each session is a directory, not a single file.
|
|
59
|
+
dirFor: () => path.join(os.homedir(), '.copilot', 'session-state'),
|
|
60
|
+
entryType: 'dir',
|
|
61
|
+
// Subdir name is the uuid; tolerate any non-empty name (copilot uses
|
|
62
|
+
// standard uuid4 but we'd rather not be strict).
|
|
63
|
+
parseId: (dirname) => /^[0-9a-f-]+$/i.test(dirname) ? dirname : null,
|
|
64
|
+
readCwd: async (dirpath) => {
|
|
65
|
+
// workspace.yaml has plain `cwd: <path>` on its own line — quick
|
|
66
|
+
// regex parse, no YAML dep.
|
|
67
|
+
const yaml = path.join(dirpath, 'workspace.yaml');
|
|
68
|
+
try {
|
|
69
|
+
const txt = await fsp.readFile(yaml, 'utf8');
|
|
70
|
+
const m = txt.match(/^\s*cwd\s*:\s*(.+?)\s*$/m);
|
|
71
|
+
return m ? m[1].trim() : null;
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function captureSessionId({ cliType, cwd, onCapture, onTimeout, windowMs = WINDOW_MS }) {
|
|
80
|
+
const profile = profiles[cliType];
|
|
81
|
+
if (!profile) return () => {};
|
|
82
|
+
const dir = profile.dirFor(cwd);
|
|
83
|
+
const spawnAt = Date.now();
|
|
84
|
+
console.log(`[cliSessionWatcher] start ${cliType} dir=${dir} cwd=${cwd}`);
|
|
85
|
+
|
|
86
|
+
let stopped = false;
|
|
87
|
+
let captured = false;
|
|
88
|
+
let pollTimer = null;
|
|
89
|
+
let expireTimer = null;
|
|
90
|
+
// Track entries we've already proven aren't ours so we don't re-read them.
|
|
91
|
+
// Only used for "wrong cwd recorded inside the file" — that's stable.
|
|
92
|
+
// We do NOT cache mtime-based rejections: a stale file can be re-touched
|
|
93
|
+
// by the CLI later (claude appends to existing transcripts) and we want
|
|
94
|
+
// to re-evaluate it then.
|
|
95
|
+
const rejected = new Set();
|
|
96
|
+
|
|
97
|
+
const cleanup = () => {
|
|
98
|
+
if (stopped) return;
|
|
99
|
+
stopped = true;
|
|
100
|
+
if (pollTimer) clearTimeout(pollTimer);
|
|
101
|
+
if (expireTimer) clearTimeout(expireTimer);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const finish = (sessionId) => {
|
|
105
|
+
if (stopped) return;
|
|
106
|
+
captured = true;
|
|
107
|
+
cleanup();
|
|
108
|
+
console.log(`[cliSessionWatcher] captured ${cliType} ${sessionId}`);
|
|
109
|
+
try { onCapture?.(sessionId); } catch (e) { console.error('[cliSessionWatcher] onCapture:', e); }
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const onExpire = () => {
|
|
113
|
+
if (stopped || captured) return;
|
|
114
|
+
cleanup();
|
|
115
|
+
console.warn(`[cliSessionWatcher] timeout ${cliType} (no transcript in ${Math.round(windowMs / 1000)}s) cwd=${cwd}`);
|
|
116
|
+
try { onTimeout?.(); } catch (e) { console.error('[cliSessionWatcher] onTimeout:', e); }
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const poll = async () => {
|
|
120
|
+
if (stopped) return;
|
|
121
|
+
try {
|
|
122
|
+
const entries = await listEntries(dir, profile);
|
|
123
|
+
console.log(`[cliSessionWatcher] poll: ${entries.length} entries, rejected=${rejected.size}`);
|
|
124
|
+
const candidates = [];
|
|
125
|
+
for (const entryPath of entries) {
|
|
126
|
+
const base = path.basename(entryPath);
|
|
127
|
+
if (rejected.has(entryPath)) continue;
|
|
128
|
+
if (profile.filePattern && !profile.filePattern.test(base)) continue;
|
|
129
|
+
const id = profile.parseId(base);
|
|
130
|
+
if (!id) continue;
|
|
131
|
+
let st;
|
|
132
|
+
try { st = await fsp.stat(entryPath); } catch { continue; }
|
|
133
|
+
// Mtime gate is re-evaluated every poll: don't memoise it. If the
|
|
134
|
+
// CLI later re-touches an old transcript (claude appends to the
|
|
135
|
+
// existing one for the cwd), this poll will pick it up.
|
|
136
|
+
if (st.mtimeMs < spawnAt - 2000) continue;
|
|
137
|
+
candidates.push({ entryPath, id, mtime: st.mtimeMs });
|
|
138
|
+
}
|
|
139
|
+
const matched = [];
|
|
140
|
+
for (const c of candidates) {
|
|
141
|
+
const cwdFromEntry = await profile.readCwd(c.entryPath);
|
|
142
|
+
if (cwdFromEntry == null) continue; // not enough data yet
|
|
143
|
+
if (!samePath(cwdFromEntry, cwd)) { rejected.add(c.entryPath); continue; }
|
|
144
|
+
matched.push(c);
|
|
145
|
+
}
|
|
146
|
+
if (matched.length === 1) {
|
|
147
|
+
finish(matched[0].id);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (matched.length > 1) {
|
|
151
|
+
console.warn(`[cliSessionWatcher] ambiguous: ${matched.length} candidates for ${cwd} — skipping capture`);
|
|
152
|
+
cleanup();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
} catch (e) {
|
|
156
|
+
console.error('[cliSessionWatcher] poll:', e.message);
|
|
157
|
+
}
|
|
158
|
+
if (!stopped) pollTimer = setTimeout(poll, POLL_MS);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
(async () => {
|
|
162
|
+
try { await fsp.mkdir(dir, { recursive: true }); } catch {}
|
|
163
|
+
if (stopped) return;
|
|
164
|
+
expireTimer = setTimeout(onExpire, windowMs);
|
|
165
|
+
poll();
|
|
166
|
+
})();
|
|
167
|
+
|
|
168
|
+
return cleanup;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
module.exports = { captureSessionId };
|
|
172
|
+
|
|
173
|
+
// ── helpers ─────────────────────────────────────────────────────────
|
|
174
|
+
|
|
175
|
+
async function listEntries(root, profile) {
|
|
176
|
+
if (profile.entryType === 'dir') {
|
|
177
|
+
let names;
|
|
178
|
+
try { names = await fsp.readdir(root, { withFileTypes: true }); }
|
|
179
|
+
catch { return []; }
|
|
180
|
+
return names.filter((e) => e.isDirectory()).map((e) => path.join(root, e.name));
|
|
181
|
+
}
|
|
182
|
+
// file mode
|
|
183
|
+
if (profile.recursive) return listAllFiles(root);
|
|
184
|
+
try {
|
|
185
|
+
const names = await fsp.readdir(root);
|
|
186
|
+
return names.map((n) => path.join(root, n));
|
|
187
|
+
} catch {
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function claudeSlug(cwd) {
|
|
193
|
+
return cwd.replace(/[:\\\/]/g, '-');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function samePath(a, b) {
|
|
197
|
+
if (!a || !b) return false;
|
|
198
|
+
const norm = (p) => path.resolve(p).replace(/[\\\/]+$/, '').toLowerCase();
|
|
199
|
+
return norm(a) === norm(b);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function firstJsonField(filepath, field, maxLines) {
|
|
203
|
+
return new Promise((resolve) => {
|
|
204
|
+
let stream;
|
|
205
|
+
try { stream = fs.createReadStream(filepath, { encoding: 'utf8' }); }
|
|
206
|
+
catch { resolve(null); return; }
|
|
207
|
+
const rl = readline.createInterface({ input: stream });
|
|
208
|
+
let count = 0;
|
|
209
|
+
// rl.close() synchronously emits 'close' on some platforms, which would
|
|
210
|
+
// re-enter done(null) and clobber the value resolve. Guard against that.
|
|
211
|
+
let settled = false;
|
|
212
|
+
const done = (v) => {
|
|
213
|
+
if (settled) return;
|
|
214
|
+
settled = true;
|
|
215
|
+
try { rl.close(); } catch {}
|
|
216
|
+
try { stream.destroy(); } catch {}
|
|
217
|
+
resolve(v);
|
|
218
|
+
};
|
|
219
|
+
rl.on('line', (line) => {
|
|
220
|
+
count++;
|
|
221
|
+
try {
|
|
222
|
+
const obj = JSON.parse(line);
|
|
223
|
+
if (obj && Object.prototype.hasOwnProperty.call(obj, field)) {
|
|
224
|
+
done(obj[field]);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
} catch {}
|
|
228
|
+
if (count >= maxLines) done(null);
|
|
229
|
+
});
|
|
230
|
+
rl.on('close', () => done(null));
|
|
231
|
+
rl.on('error', () => done(null));
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async function listAllFiles(root) {
|
|
236
|
+
const out = [];
|
|
237
|
+
const walk = async (dir) => {
|
|
238
|
+
let entries;
|
|
239
|
+
try { entries = await fsp.readdir(dir, { withFileTypes: true }); }
|
|
240
|
+
catch { return; }
|
|
241
|
+
for (const e of entries) {
|
|
242
|
+
const p = path.join(dir, e.name);
|
|
243
|
+
if (e.isDirectory()) await walk(p);
|
|
244
|
+
else out.push(p);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
await walk(root);
|
|
248
|
+
return out;
|
|
249
|
+
}
|