@@ -24,6 +24,7 @@ import * as Option from "effect/Option";
2424import * as PlatformError from "effect/PlatformError" ;
2525import * as Path from "effect/Path" ;
2626import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
27+ import * as Queue from "effect/Queue" ;
2728import * as Ref from "effect/Ref" ;
2829import * as Schema from "effect/Schema" ;
2930import * as Scope from "effect/Scope" ;
@@ -317,6 +318,73 @@ const createManager = (
317318 } ) ,
318319 ) ;
319320
321+ // Holds each 250 ms persist debounce and records every history write attempt.
322+ const createPersistFixture = Effect . gen ( function * ( ) {
323+ const fs = yield * FileSystem . FileSystem ;
324+ const clock = yield * Clock . Clock ;
325+ const debounces = yield * Queue . unbounded < Deferred . Deferred < void > > ( ) ;
326+ const attempts : Array < string > = [ ] ;
327+ let beforeNextWrite : Effect . Effect < void , PlatformError . PlatformError > = Effect . void ;
328+
329+ const gatedClock : Clock . Clock = {
330+ currentTimeMillisUnsafe : ( ) => clock . currentTimeMillisUnsafe ( ) ,
331+ currentTimeMillis : clock . currentTimeMillis ,
332+ currentTimeNanosUnsafe : ( ) => clock . currentTimeNanosUnsafe ( ) ,
333+ currentTimeNanos : clock . currentTimeNanos ,
334+ monotonicTimeNanosUnsafe : ( ) => clock . monotonicTimeNanosUnsafe ( ) ,
335+ monotonicTimeNanos : clock . monotonicTimeNanos ,
336+ sleep : ( duration ) =>
337+ Duration . toMillis ( duration ) === 250
338+ ? Effect . gen ( function * ( ) {
339+ const release = yield * Deferred . make < void > ( ) ;
340+ yield * Queue . offer ( debounces , release ) ;
341+ yield * Deferred . await ( release ) ;
342+ } )
343+ : clock . sleep ( duration ) ,
344+ } ;
345+ const recording = FileSystem . FileSystem . of ( {
346+ ...fs ,
347+ writeFileString : ( filePath , data , options ) => {
348+ attempts . push ( data ) ;
349+ const before = beforeNextWrite ;
350+ beforeNextWrite = Effect . void ;
351+ return Effect . andThen ( before , fs . writeFileString ( filePath , data , options ) ) ;
352+ } ,
353+ } ) ;
354+ const { manager, ptyAdapter, logsDir } = yield * createManager ( ) . pipe (
355+ Effect . provideService ( FileSystem . FileSystem , recording ) ,
356+ Effect . provideService ( Clock . Clock , gatedClock ) ,
357+ ) ;
358+
359+ const emitOutput = ( processIndex : number , data : string ) =>
360+ Effect . gen ( function * ( ) {
361+ const persistQueued = yield * Deferred . make < void > ( ) ;
362+ const unsubscribe = yield * manager . subscribe ( ( event ) =>
363+ event . type === "output" && event . data === data
364+ ? Deferred . succeed ( persistQueued , undefined ) . pipe ( Effect . asVoid )
365+ : Effect . void ,
366+ ) ;
367+ ptyAdapter . processes [ processIndex ] ! . emitData ( data ) ;
368+ yield * Deferred . await ( persistQueued ) ;
369+ unsubscribe ( ) ;
370+ } ) ;
371+ const releaseNextDebounce = Effect . flatMap ( Queue . take ( debounces ) , ( release ) =>
372+ Deferred . succeed ( release , undefined ) ,
373+ ) ;
374+
375+ return {
376+ manager,
377+ attempts,
378+ emitOutput,
379+ releaseNextDebounce,
380+ nextDebounce : Queue . take ( debounces ) ,
381+ readHistory : Effect . flatMap ( historyLogPath ( logsDir ) , ( filePath ) => fs . readFileString ( filePath ) ) ,
382+ setBeforeNextWrite : ( effect : Effect . Effect < void , PlatformError . PlatformError > ) => {
383+ beforeNextWrite = effect ;
384+ } ,
385+ } ;
386+ } ) ;
387+
320388const layerWithHostPlatform = ( platform : NodeJS . Platform ) =>
321389 Layer . succeed ( HostProcess . Platform , platform ) ;
322390
@@ -1501,6 +1569,117 @@ it.layer(
15011569 } ) ,
15021570 ) ;
15031571
1572+ it . effect ( "skips a background history write the previous write already covered" , ( ) =>
1573+ Effect . gen ( function * ( ) {
1574+ const { manager, attempts, emitOutput, releaseNextDebounce, nextDebounce, readHistory } =
1575+ yield * createPersistFixture ;
1576+ yield * manager . open ( openInput ( ) ) ;
1577+ yield * emitOutput ( 0 , "first\n" ) ;
1578+ const firstDebounce = yield * nextDebounce ;
1579+ yield * emitOutput ( 0 , "second\n" ) ;
1580+ yield * Deferred . succeed ( firstDebounce , undefined ) ;
1581+ yield * releaseNextDebounce ;
1582+ yield * manager . close ( { threadId : "thread-1" } ) ;
1583+
1584+ // The final attempt is close's forced write of an unchanged revision.
1585+ expect ( attempts ) . toEqual ( [ "first\nsecond\n" , "first\nsecond\n" ] ) ;
1586+ expect ( yield * readHistory ) . toBe ( "first\nsecond\n" ) ;
1587+ } ) ,
1588+ ) ;
1589+
1590+ it . effect ( "persists output that arrives while a history write is in flight" , ( ) =>
1591+ Effect . gen ( function * ( ) {
1592+ const fixture = yield * createPersistFixture ;
1593+ const { manager, attempts, emitOutput, releaseNextDebounce, readHistory } = fixture ;
1594+ const writeStarted = yield * Deferred . make < void > ( ) ;
1595+ const releaseWrite = yield * Deferred . make < void > ( ) ;
1596+ fixture . setBeforeNextWrite (
1597+ Deferred . succeed ( writeStarted , undefined ) . pipe (
1598+ Effect . andThen ( Deferred . await ( releaseWrite ) ) ,
1599+ ) ,
1600+ ) ;
1601+ yield * manager . open ( openInput ( ) ) ;
1602+ yield * emitOutput ( 0 , "first\n" ) ;
1603+ yield * releaseNextDebounce ;
1604+ yield * Deferred . await ( writeStarted ) ;
1605+ yield * emitOutput ( 0 , "second\n" ) ;
1606+ yield * Deferred . succeed ( releaseWrite , undefined ) ;
1607+ yield * releaseNextDebounce ;
1608+ yield * manager . close ( { threadId : "thread-1" } ) ;
1609+
1610+ expect ( attempts ) . toEqual ( [ "first\n" , "first\nsecond\n" , "first\nsecond\n" ] ) ;
1611+ expect ( yield * readHistory ) . toBe ( "first\nsecond\n" ) ;
1612+ } ) ,
1613+ ) ;
1614+
1615+ it . effect ( "retries a coalesced background write after the first attempt fails" , ( ) =>
1616+ Effect . gen ( function * ( ) {
1617+ const fixture = yield * createPersistFixture ;
1618+ const { manager, attempts, emitOutput, releaseNextDebounce, nextDebounce, readHistory } =
1619+ fixture ;
1620+ fixture . setBeforeNextWrite (
1621+ Effect . fail (
1622+ PlatformError . systemError ( {
1623+ _tag : "Unknown" ,
1624+ module : "FileSystem" ,
1625+ method : "writeFileString" ,
1626+ cause : { code : "ENOSPC" } ,
1627+ } ) ,
1628+ ) ,
1629+ ) ;
1630+ yield * manager . open ( openInput ( ) ) ;
1631+ yield * emitOutput ( 0 , "first\n" ) ;
1632+ const firstDebounce = yield * nextDebounce ;
1633+ yield * emitOutput ( 0 , "second\n" ) ;
1634+ yield * Deferred . succeed ( firstDebounce , undefined ) ;
1635+ yield * releaseNextDebounce ;
1636+ // Close blocks on the worker, so its forced write is always the last attempt.
1637+ yield * manager . close ( { threadId : "thread-1" } ) ;
1638+
1639+ expect ( attempts ) . toEqual ( [ "first\nsecond\n" , "first\nsecond\n" , "first\nsecond\n" ] ) ;
1640+ expect ( yield * readHistory ) . toBe ( "first\nsecond\n" ) ;
1641+ } ) ,
1642+ ) ;
1643+
1644+ it . effect ( "writes a clear that lands while a redundant background pass is pending" , ( ) =>
1645+ Effect . gen ( function * ( ) {
1646+ const { manager, attempts, emitOutput, nextDebounce, readHistory } =
1647+ yield * createPersistFixture ;
1648+ yield * manager . open ( openInput ( ) ) ;
1649+ yield * emitOutput ( 0 , "first\n" ) ;
1650+ const firstDebounce = yield * nextDebounce ;
1651+ yield * emitOutput ( 0 , "second\n" ) ;
1652+ yield * Deferred . succeed ( firstDebounce , undefined ) ;
1653+ const pendingDebounce = yield * nextDebounce ;
1654+ const clearing = yield * Effect . forkChild (
1655+ manager . clear ( { threadId : "thread-1" , terminalId : DEFAULT_TERMINAL_ID } ) ,
1656+ ) ;
1657+ yield * Deferred . succeed ( pendingDebounce , undefined ) ;
1658+ yield * Fiber . join ( clearing ) ;
1659+
1660+ expect ( attempts . at ( - 1 ) ) . toBe ( "" ) ;
1661+ expect ( yield * readHistory ) . toBe ( "" ) ;
1662+ } ) ,
1663+ ) ;
1664+
1665+ it . effect ( "persists a reopened terminal's history under the same id" , ( ) =>
1666+ Effect . gen ( function * ( ) {
1667+ const { manager, attempts, emitOutput, releaseNextDebounce, readHistory } =
1668+ yield * createPersistFixture ;
1669+ yield * manager . open ( openInput ( ) ) ;
1670+ yield * emitOutput ( 0 , "first\n" ) ;
1671+ yield * releaseNextDebounce ;
1672+ yield * manager . close ( { threadId : "thread-1" } ) ;
1673+ yield * manager . open ( openInput ( ) ) ;
1674+ yield * emitOutput ( 1 , "second\n" ) ;
1675+ yield * releaseNextDebounce ;
1676+ yield * manager . close ( { threadId : "thread-1" } ) ;
1677+
1678+ expect ( attempts ) . toEqual ( [ "first\n" , "first\n" , "first\nsecond\n" , "first\nsecond\n" ] ) ;
1679+ expect ( yield * readHistory ) . toBe ( "first\nsecond\n" ) ;
1680+ } ) ,
1681+ ) ;
1682+
15041683 it . effect ( "migrates legacy transcript filenames to terminal-scoped history path on open" , ( ) =>
15051684 Effect . gen ( function * ( ) {
15061685 const { manager, logsDir } = yield * createManager ( ) ;
0 commit comments