@@ -1424,6 +1424,162 @@ it.effect("retries a failed workspace preparation on the same run", () => {
14241424 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
14251425} ) ;
14261426
1427+ it . effect . each ( [ "fails" , "is interrupted" ] as const ) (
1428+ "holds a message queued during setup when the workspace preparation %s" ,
1429+ ( ending ) =>
1430+ Effect . gen ( function * ( ) {
1431+ const fetchEntered = yield * Deferred . make < void > ( ) ;
1432+ const allowFetch = yield * Deferred . make < void > ( ) ;
1433+ let fetchFailures = 1 ;
1434+ const harness = makeHarness ( {
1435+ fetchRemote : ( ) =>
1436+ fetchFailures -- <= 0
1437+ ? Effect . void
1438+ : Deferred . succeed ( fetchEntered , undefined ) . pipe (
1439+ Effect . andThen ( Deferred . await ( allowFetch ) ) ,
1440+ Effect . andThen (
1441+ Effect . fail (
1442+ new GitCommandError ( {
1443+ operation : "GitVcsDriver.fetchRemote" ,
1444+ command : "git" ,
1445+ cwd : project . workspaceRoot ,
1446+ detail : "Git could not reach the remote." ,
1447+ exitCode : 128 ,
1448+ } ) ,
1449+ ) ,
1450+ ) ,
1451+ ) ,
1452+ } ) ;
1453+ yield * Effect . gen ( function * ( ) {
1454+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1455+ const threads = yield * ThreadManagement . ThreadManagementService ;
1456+ const launched = yield * launches . launch (
1457+ launchInput ( {
1458+ command : `command:launch:queued-setup-${ ending } ` ,
1459+ thread : `thread:launch:queued-setup-${ ending } ` ,
1460+ message : "First message" ,
1461+ workspace : { type : "worktree" , baseRef : "main" , startFromOrigin : true } ,
1462+ } ) ,
1463+ ) ;
1464+ yield * Deferred . await ( fetchEntered ) ;
1465+ const queued = yield * threads . sendToThread ( {
1466+ projectId,
1467+ commandId : CommandId . make ( `command:launch:queued-setup-${ ending } :follow-up` ) ,
1468+ threadId : launched . threadId ,
1469+ messageId : MessageId . make ( `message:launch:queued-setup-${ ending } :follow-up` ) ,
1470+ text : "Sent during setup" ,
1471+ attachments : [ ] ,
1472+ mode : "queue" ,
1473+ createdBy : "user" ,
1474+ creationSource : "web" ,
1475+ } ) ;
1476+ assert . equal ( queued . delivery , "queued" ) ;
1477+ if ( ending === "fails" ) {
1478+ yield * Deferred . succeed ( allowFetch , undefined ) ;
1479+ } else {
1480+ // An agent stop does not ask to hold the queue.
1481+ yield * threads . dispatch ( {
1482+ type : "run.interrupt" ,
1483+ commandId : CommandId . make ( "command:launch:queued-setup-interrupt" ) ,
1484+ threadId : launched . threadId ,
1485+ runId : launched . projection . runs [ 0 ] ! . id ,
1486+ } ) ;
1487+ }
1488+
1489+ // Without its worktree the follow-up would start in the project checkout.
1490+ yield * threads . streamStoredEventsFrom ( { threadId : launched . threadId } ) . pipe (
1491+ Stream . filter (
1492+ ( stored ) =>
1493+ stored . event . type === "run.updated" && stored . event . payload . queueHeld === true ,
1494+ ) ,
1495+ Stream . runHead ,
1496+ ) ;
1497+ const projection = yield * threads . getThreadProjection ( launched . threadId ) ;
1498+ assert . equal ( projection . runs . at ( - 1 ) ?. status , "queued" ) ;
1499+ assert . equal ( projection . thread . worktreePath , null ) ;
1500+
1501+ if ( ending === "fails" ) {
1502+ // A successful retry lets the held message follow the first turn again.
1503+ yield * launches . retryPreparation ( {
1504+ commandId : CommandId . make ( "command:launch:queued-setup-retry" ) ,
1505+ threadId : launched . threadId ,
1506+ runId : launched . projection . runs [ 0 ] ! . id ,
1507+ } ) ;
1508+ const retried = yield * threads . getThreadProjection ( launched . threadId ) ;
1509+ assert . equal ( retried . runs . at ( - 1 ) ?. queueHeld , false ) ;
1510+ }
1511+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1512+ } ) ,
1513+ ) ;
1514+
1515+ it . effect . each ( [ "failed" , "interrupted" ] as const ) (
1516+ "holds a message queued during setup that arrives after the setup %s" ,
1517+ ( ending ) => {
1518+ let fetchEntered : Deferred . Deferred < void > | null = null ;
1519+ const harness = makeHarness ( {
1520+ fetchRemote : ( ) =>
1521+ ending === "interrupted"
1522+ ? Deferred . succeed ( fetchEntered ! , undefined ) . pipe ( Effect . andThen ( Effect . never ) )
1523+ : Effect . fail (
1524+ new GitCommandError ( {
1525+ operation : "GitVcsDriver.fetchRemote" ,
1526+ command : "git" ,
1527+ cwd : project . workspaceRoot ,
1528+ detail : "Git could not reach the remote." ,
1529+ exitCode : 128 ,
1530+ } ) ,
1531+ ) ,
1532+ } ) ;
1533+ return Effect . gen ( function * ( ) {
1534+ fetchEntered = yield * Deferred . make < void > ( ) ;
1535+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1536+ const threads = yield * ThreadManagement . ThreadManagementService ;
1537+ const launched = yield * launches . launch (
1538+ launchInput ( {
1539+ command : `command:launch:late-queued-follow-up-${ ending } ` ,
1540+ thread : `thread:launch:late-queued-follow-up-${ ending } ` ,
1541+ message : "First message" ,
1542+ workspace : { type : "worktree" , baseRef : "main" , startFromOrigin : true } ,
1543+ } ) ,
1544+ ) ;
1545+ if ( ending === "interrupted" ) {
1546+ yield * Deferred . await ( fetchEntered ) ;
1547+ // An agent stop does not ask to hold the queue.
1548+ yield * threads . dispatch ( {
1549+ type : "run.interrupt" ,
1550+ commandId : CommandId . make ( "command:launch:late-queued-follow-up:interrupt" ) ,
1551+ threadId : launched . threadId ,
1552+ runId : launched . projection . runs [ 0 ] ! . id ,
1553+ } ) ;
1554+ }
1555+ yield * threads . streamStoredEventsFrom ( { threadId : launched . threadId } ) . pipe (
1556+ Stream . filter (
1557+ ( stored ) => stored . event . type === "run.updated" && stored . event . payload . status === ending ,
1558+ ) ,
1559+ Stream . runHead ,
1560+ ) ;
1561+
1562+ // Queued while setup ran, delivered only after it failed: it must not start
1563+ // in the project checkout.
1564+ const queued = yield * threads . sendToThread ( {
1565+ projectId,
1566+ commandId : CommandId . make ( `command:launch:late-queued-follow-up-${ ending } :send` ) ,
1567+ threadId : launched . threadId ,
1568+ messageId : MessageId . make ( `message:launch:late-queued-follow-up-${ ending } :send` ) ,
1569+ text : "Sent during setup" ,
1570+ attachments : [ ] ,
1571+ mode : "queue" ,
1572+ createdBy : "user" ,
1573+ creationSource : "web" ,
1574+ } ) ;
1575+ assert . equal ( queued . delivery , "queued" ) ;
1576+ const projection = yield * threads . getThreadProjection ( launched . threadId ) ;
1577+ assert . equal ( projection . runs . at ( - 1 ) ?. status , "queued" ) ;
1578+ assert . equal ( projection . runs . at ( - 1 ) ?. queueHeld , true ) ;
1579+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1580+ } ,
1581+ ) ;
1582+
14271583it . effect ( "a retry reuses a recorded worktree without undoing its branch rename" , ( ) => {
14281584 let setupFailures = 1 ;
14291585 const harness = makeHarness ( {
0 commit comments