ccdeck 4.0.4 → 4.2.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.
Files changed (32) hide show
  1. package/dist/web/assets/AccountBridge-C6OFH_Ju.js +1 -0
  2. package/dist/web/assets/{AccountSection-HmlI65gN.js → AccountSection-BMuTp9CV.js} +1 -1
  3. package/dist/web/assets/{BrowserWatchModal-DwY9hX-u.js → BrowserWatchModal-CxD5MH0G.js} +1 -1
  4. package/dist/web/assets/{FeedbackConversations-6S1QZR6S.js → FeedbackConversations-E11nU0s8.js} +1 -1
  5. package/dist/web/assets/{IntegrationModal-a8CAd22m.js → IntegrationModal-BGAiSkJt.js} +1 -1
  6. package/dist/web/assets/{IntegrationSkeleton-C2RiqqMA.js → IntegrationSkeleton-DAKXKuLF.js} +1 -1
  7. package/dist/web/assets/{RadioPanel-CyJsFSkf.js → RadioPanel-1NM6GOif.js} +1 -1
  8. package/dist/web/assets/StageShell-F-zWWpCp.js +12 -0
  9. package/dist/web/assets/{TrafficRadar-CEbp0Yi7.js → TrafficRadar-DHu0CnOc.js} +1 -1
  10. package/dist/web/assets/{UsageHistoryModal-i16x6suY.js → UsageHistoryModal-3rOC8oBZ.js} +1 -1
  11. package/dist/web/assets/{app-C0usT8Zy.js → app-CXxFM7fY.js} +61 -61
  12. package/dist/web/assets/{index-BQWffZlx.js → index-B0kLYrYF.js} +1 -1
  13. package/dist/web/assets/{inspector-status-pane-DD3QOXE6.js → inspector-status-pane-BvSOWD4r.js} +1 -1
  14. package/dist/web/assets/screens-B53mAUFb.js +16 -0
  15. package/dist/web/assets/screens-C6vVzMK-.css +1 -0
  16. package/dist/web/assets/stage-board-BmcdRHaG.js +2 -0
  17. package/dist/web/assets/{use-account-x7fuCA6S.js → use-account-DaZBTcqA.js} +1 -1
  18. package/dist/web/index.html +2 -2
  19. package/package.json +1 -1
  20. package/release-notes.json +38 -0
  21. package/src/server/codex-live-engine.mjs +15 -2
  22. package/src/server/codex-live-handoff.mjs +27 -0
  23. package/src/server/codex-translate.mjs +51 -9
  24. package/src/server/memory-metrics.mjs +19 -30
  25. package/src/server/session-transcript-routes.mjs +186 -35
  26. package/src/server/transcript-turns.mjs +641 -41
  27. package/src/server/tui-codex.mjs +3 -0
  28. package/dist/web/assets/AccountBridge-Cx7no9kK.js +0 -1
  29. package/dist/web/assets/StageShell-DXcdcjPI.js +0 -4
  30. package/dist/web/assets/screens-DAn5OlXe.js +0 -12
  31. package/dist/web/assets/screens-DUniTTbC.css +0 -1
  32. package/dist/web/assets/stage-board-kUkV8N-4.js +0 -2
@@ -1,4 +1,5 @@
1
- // GET /api/session/transcript?session=<id>&provider=<claude|codex>.
1
+ // GET /api/session/transcript?session=<id>&provider=<claude|codex>
2
+ // [&since=<version>] or [&before=<cursor>[&after=<cursor>]].
2
3
  // Chat reads one transcript already tracked by the Claude output watch or
3
4
  // the Codex rollout watcher. Requests without provider use Claude.
4
5
  //
@@ -29,13 +30,30 @@
29
30
  // file. Cut at the front, the partial first line is dropped and the answer says
30
31
  // `complete: false`. A line longer than the window gives no turn; the read is
31
32
  // never grown to fit it.
33
+ //
34
+ // LIVE WITHOUT A WATCHER. Each answer carries `version`, a token for the file
35
+ // as it stands (which file, how long, when written), opaque to the page. A page
36
+ // that sends it back as `since` is told `unchanged: true` after one lstat when
37
+ // the file has not moved: nothing is opened, read or parsed, so asking often
38
+ // costs one stat.
39
+ //
40
+ // PAGED BACK ON REQUEST. When a window did not reach the file's start it
41
+ // carries `older`, a cursor for the byte its first kept line starts at.
42
+ // `before=<cursor>` answers the window that ends there, with an `older` of its
43
+ // own until the start; `after=<cursor>` with it stops that window at a line the
44
+ // page already holds, which is how a page keeping older turns fills the stretch
45
+ // the live window slid past. Pages never split a line, and the turn cap falls
46
+ // on whole lines. A cursor is held to the file as it is now: an offset inside
47
+ // it, at the start of a line, of the same file (device and inode). Anything
48
+ // else is a 400 with a short reason, never a path.
32
49
  import { constants } from "node:fs";
50
+ import { createHash } from "node:crypto";
33
51
  import { basename, dirname, relative, sep } from "node:path";
34
52
  import { lstat as fsLstat, open as fsOpen, realpath as fsRealpath } from "node:fs/promises";
35
53
  import { send } from "./http-io.mjs";
36
54
  import { claudeTranscriptRoots, isClaudeTranscriptPath } from "./transcript-gate.mjs";
37
55
  import { outputWatch } from "./session-tracking.mjs";
38
- import { codexTranscriptWindow, transcriptWindow } from "./transcript-turns.mjs";
56
+ import { codexTranscriptWindow, MAX_TURNS, transcriptWindow } from "./transcript-turns.mjs";
39
57
  import { rolloutNameId } from "./codex-dir.mjs";
40
58
  import { codexProfileSessionDirs } from "./codex-profiles.mjs";
41
59
  import { codexTranscriptPath } from "./codex-watch.mjs";
@@ -45,20 +63,56 @@ export const SESSION_TRANSCRIPT_ROUTE = "/api/session/transcript";
45
63
  /** The most bytes read from the end of a transcript. */
46
64
  export const READ_WINDOW = 512 * 1024;
47
65
 
66
+ /** How far past an older window's end is read for the answers to its last
67
+ * calls: a call at its end is answered in the line after, which is the next
68
+ * window's. Only lines that bring no turn of their own are taken. */
69
+ export const LOOKAHEAD = 64 * 1024;
70
+
48
71
  const UUID = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i;
49
72
  const SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
73
+ const TOKEN = "[A-Za-z0-9_-]{22}";
74
+ const VERSION = new RegExp(`^${TOKEN}$`);
75
+ const CURSOR = new RegExp(`^([1-9][0-9]{0,15})\\.(${TOKEN})$`);
76
+ const KEYS = new Set(["session", "provider", "continuation", "since", "before", "after"]);
50
77
 
51
78
  const loopback = req => ["127.0.0.1", "::1", "::ffff:127.0.0.1"].includes(req.socket.remoteAddress);
52
79
 
80
+ const digest = text => createHash("sha256").update(text).digest("base64url").slice(0, 22);
81
+ /** Which file this is: the same until it is replaced. Hashed, so the page never sees an inode. */
82
+ const fileTag = stat => digest(`file:${stat.dev}:${stat.ino}`);
83
+ /** The file as it stands: moves whenever it grows or is written again. */
84
+ const versionOf = stat => digest(`version:${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}`);
85
+ const cursorText = (at, tag) => `${at}.${tag}`;
86
+
87
+ /** A cursor's offset and file tag, or null for one of the wrong shape. */
88
+ function cursorOf(text) {
89
+ const found = CURSOR.exec(text ?? "");
90
+ if (!found) return null;
91
+ const at = Number(found[1]);
92
+ return Number.isSafeInteger(at) ? { at, tag: found[2] } : null;
93
+ }
94
+
95
+ const BAD_SESSION = { reason: "bad_session" };
96
+
97
+ /** The request's parts, or `{ reason }` for one refused before anything is looked up. */
53
98
  function transcriptQuery(params) {
54
99
  const keys = [...params.keys()];
55
- if (new Set(keys).size !== keys.length || keys.some(key => key !== "session" && key !== "provider" && key !== "continuation")) return null;
100
+ if (new Set(keys).size !== keys.length || keys.some(key => !KEYS.has(key))) return BAD_SESSION;
56
101
  const session = params.get("session");
57
102
  const provider = params.get("provider") ?? "claude";
58
- if (!SESSION_ID.test(session ?? "") || (provider !== "claude" && provider !== "codex")) return null;
103
+ if (!SESSION_ID.test(session ?? "") || (provider !== "claude" && provider !== "codex")) return BAD_SESSION;
59
104
  const continuation = params.has("continuation");
60
- if (continuation && (params.get("continuation") !== "1" || provider !== "claude" || !UUID.test(session))) return null;
61
- return { session, provider, continuation };
105
+ if (continuation && (params.get("continuation") !== "1" || provider !== "claude" || !UUID.test(session))) return BAD_SESSION;
106
+ const since = params.get("since");
107
+ if (since !== null && (continuation || !VERSION.test(since))) return { reason: "bad_since" };
108
+ if (!params.has("before") && !params.has("after")) return { session, provider, continuation, since, end: null, floor: null };
109
+ // An older window: `before` where it ends, `after` (optional) a line it stops at, both of one file.
110
+ const end = cursorOf(params.get("before"));
111
+ const floor = params.has("after") ? cursorOf(params.get("after")) : null;
112
+ if (!end || continuation || since !== null || (params.has("after") && (!floor || floor.tag !== end.tag || floor.at >= end.at))) {
113
+ return { reason: "bad_cursor" };
114
+ }
115
+ return { session, provider, continuation, since, end, floor };
62
116
  }
63
117
 
64
118
  function isCodexTranscriptPath(path, roots, session) {
@@ -68,15 +122,65 @@ function isCodexTranscriptPath(path, roots, session) {
68
122
 
69
123
  const unknown = res => send(res, 404, { ok: false, reason: "unknown_session" });
70
124
 
125
+ /** The bytes [from, to) of an open file; fewer when it shrank under the read. */
126
+ async function readRange(handle, from, to) {
127
+ const want = Math.max(0, to - from);
128
+ const buf = Buffer.alloc(want);
129
+ let got = 0;
130
+ while (got < want) {
131
+ const { bytesRead } = await handle.read(buf, got, want - got, from + got);
132
+ if (bytesRead === 0) break;
133
+ got += bytesRead;
134
+ }
135
+ return buf.subarray(0, got);
136
+ }
137
+
138
+ /** Whether byte `at` starts a line: the file's start, or just after a newline. */
139
+ async function startsLine(handle, at) {
140
+ if (at === 0) return true;
141
+ const byte = await readRange(handle, at - 1, at);
142
+ return byte.length === 1 && byte[0] === 0x0a;
143
+ }
144
+
71
145
  /**
72
- * The tail of the file at `path` as lines, or null for a file this route will not read.
146
+ * The lines of `buf`, read from byte `from`, with the byte each starts at. With
147
+ * `cut`, the partial first line is dropped. The last entry is what follows the
148
+ * last newline: "" or a line still being written. Split on the newline byte, so
149
+ * an offset is exact whatever the text holds.
150
+ */
151
+ function splitLines(buf, from, cut) {
152
+ const lines = [];
153
+ const at = [];
154
+ let start = 0;
155
+ if (cut) {
156
+ const nl = buf.indexOf(0x0a);
157
+ if (nl < 0) return { lines, at };
158
+ start = nl + 1;
159
+ }
160
+ for (;;) {
161
+ const nl = buf.indexOf(0x0a, start);
162
+ lines.push(buf.toString("utf8", start, nl < 0 ? buf.length : nl));
163
+ at.push(from + start);
164
+ if (nl < 0) return { lines, at };
165
+ start = nl + 1;
166
+ }
167
+ }
168
+
169
+ const STALE = { stale: true };
170
+
171
+ /**
172
+ * One window of the file at `path`, or null for a file this route will not read.
73
173
  * `lstat` first (a symlink is refused), then opened read-only and non-blocking, and the
74
174
  * handle's own `fstat` must be the same regular file `lstat` saw, so a swap in between
75
- * is refused too.
175
+ * is refused too. `{ unchanged }` when the file still has the version the page holds;
176
+ * `{ stale }` for a cursor that does not fit the file as it is now.
76
177
  */
77
- async function readTail(path, io) {
178
+ async function readWindow(path, io, want) {
78
179
  const before = await io.lstat(path, { bigint: true });
79
180
  if (!before.isFile()) return null;
181
+ // The page holds this file as it stands: one stat answers it. Nothing new is
182
+ // said, so this runs ahead of the real-path check the read itself needs.
183
+ if (want.since && versionOf(before) === want.since) return { unchanged: true };
80
184
  // Where the file really is, after every link on the way, must still be in a
81
185
  // transcript root (themselves resolved: ~/.claude is often a link).
82
186
  const real = await io.realpath(path);
@@ -90,27 +194,60 @@ async function readTail(path, io) {
90
194
  const after = await handle.stat({ bigint: true });
91
195
  if (!after.isFile() || after.dev !== before.dev || after.ino !== before.ino) return null;
92
196
  const size = Number(after.size);
93
- const from = Math.max(0, size - READ_WINDOW);
94
- const want = size - from;
95
- const buf = Buffer.alloc(want);
96
- let got = 0;
97
- while (got < want) {
98
- const { bytesRead } = await handle.read(buf, got, want - got, from + got);
99
- if (bytesRead === 0) break;
100
- got += bytesRead;
101
- }
102
- let text = buf.toString("utf8", 0, got);
103
- const complete = from === 0;
104
- if (!complete) {
105
- const nl = text.indexOf("\n");
106
- text = nl < 0 ? "" : text.slice(nl + 1);
197
+ const tag = fileTag(after);
198
+ // The live window ends at the file's end; an older one where its cursor
199
+ // says, and stops at its `after` line or at the start.
200
+ let end = size;
201
+ let floor = 0;
202
+ if (want.end) {
203
+ if (want.end.tag !== tag || want.end.at > size || !(await startsLine(handle, want.end.at))) return STALE;
204
+ if (want.floor && !(await startsLine(handle, want.floor.at))) return STALE;
205
+ end = want.end.at;
206
+ floor = want.floor?.at ?? 0;
107
207
  }
108
- return { complete, lines: text.split("\n") };
208
+ const from = Math.max(floor, end - READ_WINDOW);
209
+ const cut = from > floor;
210
+ const { lines, at } = splitLines(await readRange(handle, from, end), from, cut);
211
+ // Whole lines after an older window, for the answers to its last calls.
212
+ const ahead = want.end && end < size
213
+ ? splitLines(await readRange(handle, end, Math.min(size, end + LOOKAHEAD)), end, false).lines.slice(0, -1)
214
+ : [];
215
+ return { lines, at, ahead, cut, floor, end, tag, version: versionOf(after) };
109
216
  } finally {
110
217
  await handle.close();
111
218
  }
112
219
  }
113
220
 
221
+ /**
222
+ * The turns of the last whole lines that fit under the turn cap, and the index of
223
+ * the first line kept. Each line's own turns are counted to find where the cap
224
+ * falls, so a line is never split between two pages. `ahead` are lines after the
225
+ * window that bring no turn of their own (a tool's answer, a command's output):
226
+ * read with it, so a call at its end has its answer.
227
+ */
228
+ export function pageOf(lines, windowOf, ahead = []) {
229
+ const whole = windowOf(ahead.length ? [...lines, ...ahead] : lines);
230
+ if (!whole.cut) return { turns: whole.turns, start: 0, cut: false };
231
+ let count = 0;
232
+ let start = lines.length;
233
+ while (start > 0) {
234
+ const own = windowOf([lines[start - 1]]).turns.length;
235
+ // The newest line with a turn is kept even when it alone is over the cap.
236
+ if (count > 0 && count + own > MAX_TURNS) break;
237
+ count += own;
238
+ start -= 1;
239
+ }
240
+ const kept = windowOf([...lines.slice(start), ...ahead]);
241
+ return { turns: kept.turns, start, cut: start > 0 || kept.cut };
242
+ }
243
+
244
+ /** Nothing in an older window still runs: a call with no answer in it was answered later. */
245
+ function settled(turn) {
246
+ if (!turn.running) return turn;
247
+ const { running: _, ...rest } = turn;
248
+ return rest;
249
+ }
250
+
114
251
  /** Claude writes this event in the original transcript when /background changes its UUID. */
115
252
  function continuedSession(lines, source) {
116
253
  for (let i = lines.length - 1; i >= 0; i -= 1) {
@@ -127,25 +264,25 @@ export async function handleSessionTranscript(req, res, io = {}) {
127
264
  if (!loopback(req)) return send(res, 403, { ok: false, error: "A conversation can be read on this computer only." });
128
265
  if (req.method !== "GET") return send(res, 405, { ok: false, error: "Use GET." });
129
266
  const query = transcriptQuery(new URL(req.url, "http://localhost").searchParams);
130
- if (!query) return send(res, 400, { ok: false, reason: "bad_session" });
267
+ if (query.reason) return send(res, 400, { ok: false, reason: query.reason });
131
268
  const { session, provider, continuation } = query;
132
269
  const codex = provider === "codex";
133
270
  const pathOf = io.pathOf ?? (codex ? codexTranscriptPath : sid => outputWatch.pathOf(sid));
134
271
  const allowed = codex ? (path, roots) => isCodexTranscriptPath(path, roots, session) : isClaudeTranscriptPath;
135
272
  const realpath = io.realpath ?? fsRealpath;
136
- let tail;
273
+ let read;
137
274
  let next = null;
138
275
  try {
139
276
  const path = pathOf(session);
140
277
  if (typeof path !== "string") return unknown(res);
141
278
  const roots = await (io.roots ?? (codex ? codexProfileSessionDirs : claudeTranscriptRoots))();
142
279
  if (!(io.isTranscriptPath ?? allowed)(path, roots)) return unknown(res);
143
- tail = await readTail(path, {
280
+ read = await readWindow(path, {
144
281
  lstat: io.lstat ?? fsLstat, open: io.open ?? fsOpen,
145
282
  realpath, roots, allowed,
146
- });
147
- if (continuation && tail && basename(path) === `${session}.jsonl`) {
148
- const candidate = continuedSession(tail.lines, session);
283
+ }, query);
284
+ if (continuation && read?.lines && basename(path) === `${session}.jsonl`) {
285
+ const candidate = continuedSession(read.lines, session);
149
286
  const destination = candidate && pathOf(candidate);
150
287
  // An idle background worker may be attachable before writing a transcript.
151
288
  // The pane verifies the backend's live target and the session's project.
@@ -158,10 +295,24 @@ export async function handleSessionTranscript(req, res, io = {}) {
158
295
  }
159
296
  }
160
297
  } catch { return unknown(res); }
161
- if (!tail) return unknown(res);
298
+ if (!read) return unknown(res);
299
+ if (read.unchanged) return send(res, 200, { ok: true, unchanged: true });
300
+ if (read.stale) return send(res, 400, { ok: false, reason: "stale_cursor" });
162
301
  if (continuation) return send(res, 200, { ok: true, sessionId: next });
163
- const { turns, cut } = (codex ? codexTranscriptWindow : transcriptWindow)(tail.lines);
164
- // Not complete when the byte window began after the file's start, or when the
165
- // turn cap dropped turns from the front.
166
- return send(res, 200, { ok: true, complete: tail.complete && !cut, turns });
302
+ const windowOf = codex ? codexTranscriptWindow : transcriptWindow;
303
+ const ahead = [];
304
+ for (const line of read.ahead) {
305
+ if (windowOf([line]).turns.length) break;
306
+ ahead.push(line);
307
+ }
308
+ const page = pageOf(read.lines, windowOf, ahead);
309
+ // Where the kept lines start: a cursor for what comes before them, when that
310
+ // is past the window's floor. One that would not move back is not given, so a
311
+ // line longer than the window ends the paging rather than repeating it.
312
+ const startAt = page.start < read.at.length ? read.at[page.start] : read.end;
313
+ const older = startAt > read.floor && startAt < read.end ? cursorText(startAt, read.tag) : null;
314
+ const turns = query.end ? page.turns.map(settled) : page.turns;
315
+ // Not complete when the byte window began after its floor, or when the turn
316
+ // cap dropped lines from the front.
317
+ return send(res, 200, { ok: true, complete: !read.cut && !page.cut, turns, version: read.version, ...(older ? { older } : {}) });
167
318
  }