@@ -1676,73 +1676,72 @@ describe("MuseAdapterV2", () => {
16761676 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
16771677 ) ;
16781678
1679+ const runActive = ( ) =>
1680+ new MspError ( {
1681+ code : - 32030 ,
1682+ message : "session/compact command rejected: run_active" ,
1683+ data : { kind : "commandRejected" , reason : "run_active" , retryable : false } ,
1684+ } ) ;
1685+ const compactionItem = ( itemId : string ) => ( {
1686+ item : {
1687+ itemId,
1688+ turnId : "wake-1" ,
1689+ kind : "compaction" ,
1690+ status : "completed" ,
1691+ outcome : "compacted" ,
1692+ revision : 1 ,
1693+ } ,
1694+ } ) ;
1695+
1696+ it . effect ( "compacts when a held Muse run ends before its run_active rejection arrives" , ( ) =>
1697+ Effect . gen ( function * ( ) {
1698+ const fake = yield * makeFakeMuse ( ) ;
1699+ const harness = yield * makeHeldWakeHarness ( fake ) ;
1700+ const rejection = pendingPromise < Record < string , unknown > > ( ) ;
1701+ fake . queueResponse ( "session/compact" , rejection . promise ) ;
1702+ const compacting = yield * harness . runtime . compactThread ! (
1703+ yield * turnInput ( harness . providerThread ) ,
1704+ ) . pipe ( Effect . forkScoped ) ;
1705+ yield * fake . takeCall ( "session/compact" ) ;
1706+ yield * fake . emit ( "turn/completed" , { turnId : "wake-1" , terminal : "completed" } ) ;
1707+ rejection . reject ( runActive ( ) ) ;
1708+ yield * Fiber . join ( compacting ) ;
1709+ yield * fake . takeCall ( "session/compact" ) ;
1710+ yield * fake . emit ( "item/completed" , compactionItem ( "compaction-1" ) ) ;
1711+ assert . strictEqual ( ( yield * harness . takeEvent ( "turn.terminal" ) ) . status , "completed" ) ;
1712+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1713+ ) ;
1714+
16791715 it . effect . each ( [
1680- { when : "while the run is still going" , endBeforeRejection : false } ,
1681- { when : "after the run already ended" , endBeforeRejection : true } ,
1716+ { asked : "before compaction" , run : "wake-1" } ,
1717+ { asked : "while compaction waits" , run : "wake-1" } ,
1718+ { asked : "while compaction waits" , run : "wake-2" } ,
16821719 ] as const ) (
1683- "compacts once a held Muse run ends when the rejection arrives $when " ,
1684- ( { endBeforeRejection } ) =>
1720+ "shows $run's approval raised $asked so compaction retries once runs end " ,
1721+ ( { asked , run } ) =>
16851722 Effect . gen ( function * ( ) {
16861723 const fake = yield * makeFakeMuse ( ) ;
16871724 const harness = yield * makeHeldWakeHarness ( fake ) ;
1688- const rejection = pendingPromise < Record < string , unknown > > ( ) ;
1689- fake . queueResponse ( "session/compact" , rejection . promise ) ;
1690- const compacting = yield * harness . runtime . compactThread ! (
1691- yield * turnInput ( harness . providerThread ) ,
1692- ) . pipe ( Effect . forkScoped ) ;
1725+ if ( asked === "before compaction" ) {
1726+ // An earlier automatic compaction must stay with the wake, not end this turn.
1727+ yield * fake . emit ( "item/completed" , compactionItem ( "auto-compaction" ) ) ;
1728+ yield * fake . emit ( "approval/requested" , approval ( run ) ) ;
1729+ // Native events are processed in order; this one marks the two above as buffered.
1730+ yield * fake . emit ( "session/contextUsage" , { usedTokens : 1 } ) ;
1731+ yield * harness . takeEvent (
1732+ "provider_thread.updated" ,
1733+ ( event ) => event . providerThread . contextUsage ?. usedTokens === 1 ,
1734+ ) ;
1735+ }
1736+ fake . queueResponse ( "session/compact" , runActive ( ) ) ;
1737+ yield * harness . runtime . compactThread ! ( yield * turnInput ( harness . providerThread ) ) ;
16931738 const first = yield * fake . takeCall ( "session/compact" ) ;
1694- if ( endBeforeRejection )
1739+ if ( run === "wake-2" ) {
1740+ yield * fake . emit ( "turn/started" , { turnId : "wake-2" } ) ;
16951741 yield * fake . emit ( "turn/completed" , { turnId : "wake-1" , terminal : "completed" } ) ;
1696- rejection . reject (
1697- new MspError ( {
1698- code : - 32030 ,
1699- message : "session/compact command rejected: run_active" ,
1700- data : { kind : "commandRejected" , reason : "run_active" , retryable : false } ,
1701- } ) ,
1702- ) ;
1703- yield * Fiber . join ( compacting ) ;
1704- if ( ! endBeforeRejection )
1705- yield * fake . emit ( "turn/completed" , { turnId : "wake-1" , terminal : "completed" } ) ;
1706- const retry = yield * fake . takeCall ( "session/compact" ) ;
1707- assert . notStrictEqual ( retry . commandId , first . commandId ) ;
1708- // The compaction runs under its own target run, not the command ID.
1709- yield * fake . emit ( "item/completed" , {
1710- item : {
1711- itemId : "compaction-1" ,
1712- turnId : "wake-1" ,
1713- kind : "compaction" ,
1714- status : "completed" ,
1715- outcome : "compacted" ,
1716- revision : 1 ,
1717- } ,
1718- } ) ;
1719- const terminal = yield * harness . takeEvent ( "turn.terminal" ) ;
1720- assert . strictEqual ( terminal . status , "completed" ) ;
1721- assert . strictEqual ( terminal . threadDisposition , "reusable" ) ;
1722- yield * startConversation ( harness , fake , 2 ) ;
1723- } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1724- ) ;
1725-
1726- it . effect . each ( [ "before compaction" , "while compaction waits" ] as const ) (
1727- "shows a held Muse run's approval raised $0 so compaction can retry" ,
1728- ( asked ) =>
1729- Effect . gen ( function * ( ) {
1730- const fake = yield * makeFakeMuse ( ) ;
1731- const harness = yield * makeHeldWakeHarness ( fake ) ;
1732- if ( asked === "before compaction" )
1733- yield * fake . emit ( "approval/requested" , approval ( "wake-1" ) ) ;
1734- fake . queueResponse (
1735- "session/compact" ,
1736- new MspError ( {
1737- code : - 32030 ,
1738- message : "session/compact command rejected: run_active" ,
1739- data : { kind : "commandRejected" , reason : "run_active" , retryable : false } ,
1740- } ) ,
1741- ) ;
1742- yield * harness . runtime . compactThread ! ( yield * turnInput ( harness . providerThread ) ) ;
1743- yield * fake . takeCall ( "session/compact" ) ;
1742+ }
17441743 if ( asked === "while compaction waits" )
1745- yield * fake . emit ( "approval/requested" , approval ( "wake-1" ) ) ;
1744+ yield * fake . emit ( "approval/requested" , approval ( run ) ) ;
17461745 const shown = yield * harness . takeEvent (
17471746 "runtime_request.updated" ,
17481747 ( event ) => event . runtimeRequest . status === "pending" ,
@@ -1752,9 +1751,17 @@ describe("MuseAdapterV2", () => {
17521751 decision : "accept" ,
17531752 } ) ;
17541753 yield * fake . takeCall ( "approval/decide" ) ;
1755- yield * fake . emit ( "approval/resolved" , { approvalId : "approval-1" , turnId : "wake-1" } ) ;
1756- yield * fake . emit ( "turn/completed" , { turnId : "wake-1" , terminal : "completed" } ) ;
1757- yield * fake . takeCall ( "session/compact" ) ;
1754+ yield * fake . emit ( "approval/resolved" , { approvalId : "approval-1" , turnId : run } ) ;
1755+ yield * fake . emit ( "turn/completed" , { turnId : run , terminal : "completed" } ) ;
1756+ const retry = yield * fake . takeCall ( "session/compact" ) ;
1757+ // Command IDs are idempotency keys, so a reused one would replay the rejection.
1758+ assert . notStrictEqual ( retry . commandId , first . commandId ) ;
1759+ // The compaction runs under its own target run, not the command ID.
1760+ yield * fake . emit ( "item/completed" , compactionItem ( "compaction-1" ) ) ;
1761+ const terminal = yield * harness . takeEvent ( "turn.terminal" ) ;
1762+ assert . strictEqual ( terminal . status , "completed" ) ;
1763+ assert . strictEqual ( terminal . threadDisposition , "reusable" ) ;
1764+ yield * startConversation ( harness , fake , 2 ) ;
17581765 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
17591766 ) ;
17601767
@@ -1787,13 +1794,7 @@ describe("MuseAdapterV2", () => {
17871794 yield * fake . emit ( "turn/started" , { turnId : "wake-1" } ) ;
17881795 yield * Deferred . await ( offered ) ;
17891796 }
1790- admission . reject (
1791- new MspError ( {
1792- code : - 32030 ,
1793- message : "session/compact command rejected: run_active" ,
1794- data : { kind : "commandRejected" , reason : "run_active" , retryable : false } ,
1795- } ) ,
1796- ) ;
1797+ admission . reject ( runActive ( ) ) ;
17971798 yield * Fiber . join ( compacting ) ;
17981799 const running = yield * harness . takeEvent (
17991800 "provider_turn.updated" ,
@@ -1835,36 +1836,6 @@ describe("MuseAdapterV2", () => {
18351836 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
18361837 ) ;
18371838
1838- it . effect ( "lets Muse admit compaction when a resumed session reports a stale active turn" , ( ) =>
1839- Effect . gen ( function * ( ) {
1840- const fake = yield * makeFakeMuse ( ) ;
1841- // muse-code-sdk#121: resume after compaction reports a finished turn as running.
1842- fake . queueResponse ( "session/resume" , {
1843- session : {
1844- sessionId : "saved-session" ,
1845- modelId : MODEL ,
1846- activeTurnId : "finished-turn" ,
1847- status : "running" ,
1848- } ,
1849- history : { items : [ ] } ,
1850- } ) ;
1851- const harness = yield * makeHarness ( fake , INSTANCE_ID , "saved-session" ) ;
1852- yield * harness . runtime . compactThread ! ( yield * turnInput ( harness . providerThread ) ) ;
1853- yield * fake . takeCall ( "session/compact" ) ;
1854- yield * fake . emit ( "item/completed" , {
1855- item : {
1856- itemId : "compaction-1" ,
1857- turnId : "compaction-target-run" ,
1858- kind : "compaction" ,
1859- status : "completed" ,
1860- outcome : "compacted" ,
1861- revision : 1 ,
1862- } ,
1863- } ) ;
1864- assert . strictEqual ( ( yield * harness . takeEvent ( "turn.terminal" ) ) . status , "completed" ) ;
1865- } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1866- ) ;
1867-
18681839 it . effect ( "projects native todos with stable identity and removes cancelled steps" , ( ) =>
18691840 Effect . gen ( function * ( ) {
18701841 const fake = yield * makeFakeMuse ( ) ;
0 commit comments