@adhdev/session-host-core 1.0.60-rc.66 → 1.0.60-rc.67

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/index.d.mts CHANGED
@@ -456,6 +456,47 @@ declare class SessionRingBuffer {
456
456
  text: string;
457
457
  }): void;
458
458
  private trim;
459
+ /**
460
+ * TRIM-BOUNDARY repair. Eviction drops whole chunks, and a chunk boundary is
461
+ * a PTY read boundary — an arbitrary byte offset with no relationship to the
462
+ * structure of the stream. So the chunk that becomes the new oldest can begin
463
+ * partway through something the sender wrote atomically, and `snapshot()`
464
+ * joins from exactly there.
465
+ *
466
+ * The consumer is the browser terminal: "Load older terminal output" asks
467
+ * with `sinceSeq: 0`, which makes the daemon skip the emulator viewport and
468
+ * hand this raw text straight to xterm (see `mergeRuntimeSnapshot`). xterm
469
+ * then parses a stream that starts mid-token, which is how the reported
470
+ * screenshot got orphaned `.` and `5` glyphs floating above the output and a
471
+ * large blank band at the top:
472
+ *
473
+ * drop 5 chars -> "[HClaude Code v2.1.220…" the CSI introducer is gone,
474
+ * so `[H` prints literally
475
+ * drop 12 chars -> "2mClaude Code v2.1.220…" half an SGR prints literally
476
+ * drop 46 chars -> the leading `\x1b[2J\x1b[H` never arrives, so the screen
477
+ * is never cleared/homed and row placement collapses
478
+ * byte cut -> a torn 3-byte Hangul sequence decodes to U+FFFD
479
+ *
480
+ * So walk the head of the new oldest chunk forward to the first offset that
481
+ * is safe to start parsing at, and drop the partial prefix. Losing a few
482
+ * bytes of already-evicted context is strictly better than injecting literal
483
+ * garbage into the viewport.
484
+ *
485
+ * Two independent boundary classes have to be handled, and neither subsumes
486
+ * the other:
487
+ *
488
+ * 1. Character encoding. Chunks are JS strings, so a torn multi-byte UTF-8
489
+ * sequence has already decayed into U+FFFD (or, for astral characters, a
490
+ * lone surrogate) by the time it gets here. This mirrors the protection
491
+ * `createLineParser` grew for the IPC socket path in
492
+ * `ipc-line-parser-utf8.test.ts`; that layer can hold bytes back and
493
+ * re-join them because it owns both sides of the split, whereas here the
494
+ * other half is already gone, so dropping is the only repair available.
495
+ * 2. Escape sequences. A CSI/OSC/SS3 can be cut anywhere, and unlike the
496
+ * encoding case the leftover bytes are all perfectly printable — which is
497
+ * precisely why the corruption is visible rather than silent.
498
+ */
499
+ private healHead;
459
500
  }
460
501
 
461
502
  declare class SessionHostRegistry {
package/dist/index.d.ts CHANGED
@@ -456,6 +456,47 @@ declare class SessionRingBuffer {
456
456
  text: string;
457
457
  }): void;
458
458
  private trim;
459
+ /**
460
+ * TRIM-BOUNDARY repair. Eviction drops whole chunks, and a chunk boundary is
461
+ * a PTY read boundary — an arbitrary byte offset with no relationship to the
462
+ * structure of the stream. So the chunk that becomes the new oldest can begin
463
+ * partway through something the sender wrote atomically, and `snapshot()`
464
+ * joins from exactly there.
465
+ *
466
+ * The consumer is the browser terminal: "Load older terminal output" asks
467
+ * with `sinceSeq: 0`, which makes the daemon skip the emulator viewport and
468
+ * hand this raw text straight to xterm (see `mergeRuntimeSnapshot`). xterm
469
+ * then parses a stream that starts mid-token, which is how the reported
470
+ * screenshot got orphaned `.` and `5` glyphs floating above the output and a
471
+ * large blank band at the top:
472
+ *
473
+ * drop 5 chars -> "[HClaude Code v2.1.220…" the CSI introducer is gone,
474
+ * so `[H` prints literally
475
+ * drop 12 chars -> "2mClaude Code v2.1.220…" half an SGR prints literally
476
+ * drop 46 chars -> the leading `\x1b[2J\x1b[H` never arrives, so the screen
477
+ * is never cleared/homed and row placement collapses
478
+ * byte cut -> a torn 3-byte Hangul sequence decodes to U+FFFD
479
+ *
480
+ * So walk the head of the new oldest chunk forward to the first offset that
481
+ * is safe to start parsing at, and drop the partial prefix. Losing a few
482
+ * bytes of already-evicted context is strictly better than injecting literal
483
+ * garbage into the viewport.
484
+ *
485
+ * Two independent boundary classes have to be handled, and neither subsumes
486
+ * the other:
487
+ *
488
+ * 1. Character encoding. Chunks are JS strings, so a torn multi-byte UTF-8
489
+ * sequence has already decayed into U+FFFD (or, for astral characters, a
490
+ * lone surrogate) by the time it gets here. This mirrors the protection
491
+ * `createLineParser` grew for the IPC socket path in
492
+ * `ipc-line-parser-utf8.test.ts`; that layer can hold bytes back and
493
+ * re-join them because it owns both sides of the split, whereas here the
494
+ * other half is already gone, so dropping is the only repair available.
495
+ * 2. Escape sequences. A CSI/OSC/SS3 can be cut anywhere, and unlike the
496
+ * encoding case the leftover bytes are all perfectly printable — which is
497
+ * precisely why the corruption is visible rather than silent.
498
+ */
499
+ private healHead;
459
500
  }
460
501
 
461
502
  declare class SessionHostRegistry {
package/dist/index.js CHANGED
@@ -181,13 +181,112 @@ var SessionRingBuffer = class {
181
181
  this.trim();
182
182
  }
183
183
  trim() {
184
+ let evicted = false;
184
185
  while (this.totalBytes > this.maxBytes && this.chunks.length > 1) {
185
186
  const removed = this.chunks.shift();
186
187
  if (!removed) break;
187
188
  this.totalBytes -= removed.bytes;
189
+ evicted = true;
188
190
  }
191
+ if (evicted) this.healHead();
192
+ }
193
+ /**
194
+ * TRIM-BOUNDARY repair. Eviction drops whole chunks, and a chunk boundary is
195
+ * a PTY read boundary — an arbitrary byte offset with no relationship to the
196
+ * structure of the stream. So the chunk that becomes the new oldest can begin
197
+ * partway through something the sender wrote atomically, and `snapshot()`
198
+ * joins from exactly there.
199
+ *
200
+ * The consumer is the browser terminal: "Load older terminal output" asks
201
+ * with `sinceSeq: 0`, which makes the daemon skip the emulator viewport and
202
+ * hand this raw text straight to xterm (see `mergeRuntimeSnapshot`). xterm
203
+ * then parses a stream that starts mid-token, which is how the reported
204
+ * screenshot got orphaned `.` and `5` glyphs floating above the output and a
205
+ * large blank band at the top:
206
+ *
207
+ * drop 5 chars -> "[HClaude Code v2.1.220…" the CSI introducer is gone,
208
+ * so `[H` prints literally
209
+ * drop 12 chars -> "2mClaude Code v2.1.220…" half an SGR prints literally
210
+ * drop 46 chars -> the leading `\x1b[2J\x1b[H` never arrives, so the screen
211
+ * is never cleared/homed and row placement collapses
212
+ * byte cut -> a torn 3-byte Hangul sequence decodes to U+FFFD
213
+ *
214
+ * So walk the head of the new oldest chunk forward to the first offset that
215
+ * is safe to start parsing at, and drop the partial prefix. Losing a few
216
+ * bytes of already-evicted context is strictly better than injecting literal
217
+ * garbage into the viewport.
218
+ *
219
+ * Two independent boundary classes have to be handled, and neither subsumes
220
+ * the other:
221
+ *
222
+ * 1. Character encoding. Chunks are JS strings, so a torn multi-byte UTF-8
223
+ * sequence has already decayed into U+FFFD (or, for astral characters, a
224
+ * lone surrogate) by the time it gets here. This mirrors the protection
225
+ * `createLineParser` grew for the IPC socket path in
226
+ * `ipc-line-parser-utf8.test.ts`; that layer can hold bytes back and
227
+ * re-join them because it owns both sides of the split, whereas here the
228
+ * other half is already gone, so dropping is the only repair available.
229
+ * 2. Escape sequences. A CSI/OSC/SS3 can be cut anywhere, and unlike the
230
+ * encoding case the leftover bytes are all perfectly printable — which is
231
+ * precisely why the corruption is visible rather than silent.
232
+ */
233
+ healHead() {
234
+ const head = this.chunks[0];
235
+ if (!head) return;
236
+ const repaired = stripDanglingPrefix(head.data);
237
+ if (repaired === head.data) return;
238
+ const bytes = Buffer.byteLength(repaired, "utf8");
239
+ this.totalBytes -= head.bytes - bytes;
240
+ head.data = repaired;
241
+ head.bytes = bytes;
189
242
  }
190
243
  };
244
+ var CSI_FINAL = /[\x40-\x7e]/;
245
+ var CSI_COMMON_FINAL = /[ABCDEFGHJKSTLMPX@mnchlsurdfgqit]/;
246
+ function stripDanglingPrefix(text) {
247
+ let i = 0;
248
+ while (i < text.length) {
249
+ const code = text.charCodeAt(i);
250
+ if (code === 65533) {
251
+ i += 1;
252
+ continue;
253
+ }
254
+ if (code >= 55296 && code <= 56319) {
255
+ const next = text.charCodeAt(i + 1);
256
+ if (next >= 56320 && next <= 57343) break;
257
+ i += 1;
258
+ continue;
259
+ }
260
+ if (code >= 56320 && code <= 57343) {
261
+ i += 1;
262
+ continue;
263
+ }
264
+ break;
265
+ }
266
+ const rest = text.slice(i);
267
+ const danglingLength = danglingEscapeTailLength(rest);
268
+ return danglingLength > 0 ? rest.slice(danglingLength) : rest;
269
+ }
270
+ function danglingEscapeTailLength(text) {
271
+ if (!text || text.charCodeAt(0) === 27) return 0;
272
+ let i = 0;
273
+ const hasIntroducer = text[0] === "[" || text[0] === "]";
274
+ if (hasIntroducer) i = 1;
275
+ const start = i;
276
+ while (i < text.length && /[0-9;?:<=>]/.test(text[i])) i += 1;
277
+ if (i >= text.length) return 0;
278
+ if (text[0] === "]") {
279
+ const bel = text.indexOf("\x07");
280
+ if (bel >= 0) return bel + 1;
281
+ return 0;
282
+ }
283
+ if (!CSI_FINAL.test(text[i])) return 0;
284
+ if (!hasIntroducer) {
285
+ if (i === start) return 0;
286
+ if (!CSI_COMMON_FINAL.test(text[i])) return 0;
287
+ }
288
+ return i + 1;
289
+ }
191
290
 
192
291
  // src/defaults.ts
193
292
  var DEFAULT_SESSION_HOST_COLS = 80;