1414 * physical stream keep the same graph rather than one copy each.
1515 * - **Incremental reading.** Records are read a chunk at a time, split at the
1616 * newline byte and decoded per line, so multi-byte text split across a chunk
17- * boundary reads exactly as a whole-file read did. A history past the
18- * supported length is refused by a sentence naming the limit .
17+ * boundary reads exactly as a whole-file read did. The decoded limit applies
18+ * to each record independently .
1919 * - **Exact bytes.** The Sidekick's prefix bindings and the single-file-to-pair
2020 * upgrade measure and copy the file's own bytes through the same reader.
2121 * - **Bounded reading of a long value.** What the screen says a binding is
@@ -51,12 +51,7 @@ import type { ReplRenderer } from "../src/repl/renderer.ts";
5151import type { ReplTerminalSize } from "../src/repl/terminal.ts" ;
5252import { committedContext , useMeasuringRenderer } from "./fixtures/repl/presentation.ts" ;
5353import { ReplHost } from "../src/repl/host.ts" ;
54- import {
55- HISTORY_LIMIT ,
56- openExecutionFile ,
57- readRecords ,
58- RetainedReplStream ,
59- } from "../src/repl/journal.ts" ;
54+ import { openExecutionFile , readRecords , RetainedReplStream } from "../src/repl/journal.ts" ;
6055import { EntrySegmentStream , ROOT_IMPORT } from "../src/repl/entries.ts" ;
6156import { projectRepl } from "../src/repl/model.ts" ;
6257import { READ_CHUNK } from "../src/repl/record-file.ts" ;
@@ -420,25 +415,23 @@ describe("#992 — a history is read a record at a time", () => {
420415 ) ;
421416 } ) ;
422417
423- it ( "H3: the limit is in UTF-16 code units with newlines, and refusing it names it " , function * ( ) {
418+ it ( "H3: the decoded limit applies to each record, including its newline " , function * ( ) {
424419 const directory = yield * useTempDirectory ( "xmd-limit-" ) ;
425420 const path = join ( directory , "limit.jsonl" ) ;
426- // Three-byte characters, so a byte count and a code-unit count disagree and
427- // a limit that confused them would refuse a file that fits.
428- const text = line ( record ( "one" , "—" . repeat ( 400 ) ) ) + line ( record ( "two" , "x" . repeat ( 300 ) ) ) ;
421+ const short = line ( record ( "one" , "x" . repeat ( 300 ) ) ) ;
422+ const long = line ( record ( "two" , "—" . repeat ( 400 ) ) ) ;
423+ const text = short + long ;
429424 yield * until ( writeFile ( path , text ) ) ;
430- expect ( bytesOf ( text ) ) . toBeGreaterThan ( text . length ) ;
425+ expect ( bytesOf ( long ) ) . toBeGreaterThan ( long . length ) ;
426+ expect ( short . length ) . toBeLessThan ( long . length ) ;
431427
432- expect ( yield * readRecords ( path , text . length ) ) . toEqual ( wholeFile ( text ) ) ;
433- const refused = yield * refusal ( ( ) => readRecords ( path , text . length - 1 ) ) ;
428+ expect ( yield * readRecords ( path , long . length ) ) . toEqual ( wholeFile ( text ) ) ;
429+ const refused = yield * refusal ( ( ) => readRecords ( path , long . length - 1 ) ) ;
434430 expect ( refused ) . toBe (
435- `this execution's history exceeds the supported limit of ` +
436- `${ ( text . length - 1 ) . toLocaleString ( "en-US" ) } UTF-16 code units. Start a new execution ` +
437- "with `xmd repl` for further work; this history was left unchanged." ,
431+ `this execution's history holds a record that exceeds ` +
432+ `${ ( long . length - 1 ) . toLocaleString ( "en-US" ) } UTF-16 code units (record 2).` ,
438433 ) ;
439- // Refusing leaves the history exactly as it was.
440434 expect ( yield * until ( readFile ( path , "utf8" ) ) ) . toBe ( text ) ;
441- expect ( HISTORY_LIMIT ) . toBe ( 0x1fffffe8 ) ;
442435 } ) ;
443436} ) ;
444437
@@ -461,6 +454,40 @@ function linesOf(text: string): string[] {
461454 . map ( ( each ) => `${ each } \n` ) ;
462455}
463456
457+ describe ( "#992 — Sidekick histories are read one record at a time" , ( ) => {
458+ it ( "reads a UTF-8 character split between chunks and preserves record order" , function * ( ) {
459+ const directory = yield * useTempDirectory ( "xmd-sidekick-chunks-" ) ;
460+ const path = join ( directory , SIDEKICK_FILE ) ;
461+ const owner = { kind : "owner" , version : 1 , execution : "chat" } ;
462+ const ready = { kind : "ready" } ;
463+ const prefix = `${ JSON . stringify ( owner ) } \n${ JSON . stringify ( ready ) } \n` ;
464+ const conversation = {
465+ kind : "conversation" ,
466+ conversation : "chat" ,
467+ phase : "prepared" ,
468+ agent : "MARK" ,
469+ } ;
470+ const at = prefix . length + JSON . stringify ( conversation ) . indexOf ( "MARK" ) ;
471+ conversation . agent = "x" . repeat ( READ_CHUNK - at - 1 ) + "😀" ;
472+ yield * until ( writeFile ( path , `${ prefix } ${ JSON . stringify ( conversation ) } \n` ) ) ;
473+ expect ( yield * readSidekickRecords ( path ) ) . toEqual ( [ owner , ready , conversation ] ) ;
474+ } ) ;
475+
476+ it ( "preserves the refusal for an unfinished tail after a malformed record" , function * ( ) {
477+ const directory = yield * useTempDirectory ( "xmd-sidekick-tail-" ) ;
478+ const path = join ( directory , SIDEKICK_FILE ) ;
479+ yield * until ( writeFile ( path , '{"kind":"ready"}\n{"secret":"do not repeat"}\n{' ) ) ;
480+ expect ( yield * refusal ( ( ) => readSidekickRecords ( path ) ) ) . toBe (
481+ "this execution's chat history ends in a record that was never finished writing." ,
482+ ) ;
483+
484+ yield * until ( appendFile ( path , "}\n" ) ) ;
485+ expect ( yield * refusal ( ( ) => readSidekickRecords ( path ) ) ) . toBe (
486+ "this execution's chat history holds a record this version cannot read (record 2)." ,
487+ ) ;
488+ } ) ;
489+ } ) ;
490+
464491describe ( "#992 — Sidekick prefixes and the upgrade use the file's own bytes" , ( ) => {
465492 it ( "H4: a bound and a measured prefix are the digest of exactly those bytes" , function * ( ) {
466493 const directory = yield * useTempDirectory ( "xmd-prefix-" ) ;
@@ -507,7 +534,8 @@ describe("#992 — Sidekick prefixes and the upgrade use the file's own bytes",
507534 const reordered =
508535 '{"result":{"status":"ok","value":"— reordered"},"coroutineId":"root",' +
509536 '"description":{"name":"reordered","type":"test_effect"},"type":"yield"}\n' ;
510- const text = longHistory ( ) + reordered ;
537+ const largeRecord = line ( record ( "large" , "x" . repeat ( 5 * READ_CHUNK ) ) ) ;
538+ const text = longHistory ( ) + largeRecord + reordered ;
511539 const legacy = join ( root , "long-legacy.jsonl" ) ;
512540 yield * until ( writeFile ( legacy , text ) ) ;
513541 const repository = pairedReplRepository ( root ) ;
0 commit comments