@@ -332,17 +332,22 @@ function runUpdated(
332332 } ;
333333}
334334
335- function commandRan ( threadId : ThreadId , input : string ) : OrchestrationV2DomainEvent {
335+ function commandRan (
336+ threadId : ThreadId ,
337+ input : string ,
338+ output ?: string ,
339+ name = "command" ,
340+ ) : OrchestrationV2DomainEvent {
336341 const runId = RunId . make ( `run:${ threadId } :1` ) ;
337342 const at = DateTime . makeUnsafe ( NOW ) ;
338343 return {
339344 type : "turn-item.updated" ,
340- id : EventId . make ( `event:${ threadId } :command ` ) ,
345+ id : EventId . make ( `event:${ threadId } :${ name } ` ) ,
341346 threadId,
342347 runId,
343348 occurredAt : at ,
344349 payload : {
345- id : TurnItemId . make ( `item:${ threadId } :command ` ) ,
350+ id : TurnItemId . make ( `item:${ threadId } :${ name } ` ) ,
346351 threadId,
347352 runId,
348353 nodeId : null ,
@@ -358,6 +363,7 @@ function commandRan(threadId: ThreadId, input: string): OrchestrationV2DomainEve
358363 updatedAt : at ,
359364 type : "command_execution" ,
360365 input,
366+ ...( output === undefined ? { } : { output } ) ,
361367 } ,
362368 } ;
363369}
@@ -466,6 +472,29 @@ const requestAndApply = Effect.fn("requestPullRequestSyncHarness")(function* (
466472 yield * applyRecordedSyncs ( fixture ) ;
467473} ) ;
468474
475+ /** A thread with an open pull request whose merge command marks the end of earlier events. */
476+ const MERGING_THREAD = ThreadId . make ( "merging" ) ;
477+
478+ /** The link commands `events` dispatch, read once the reactor has handled them all. */
479+ const linksFrom = Effect . fn ( "linksFrom" ) ( function * (
480+ fixture : Harness ,
481+ reactor : PullRequestSyncReactor . PullRequestSyncReactor [ "Service" ] ,
482+ events : ReadonlyArray < OrchestrationV2DomainEvent > ,
483+ ) {
484+ yield * Ref . set ( fixture . linkCommands , [ ] ) ;
485+ yield * Queue . offerAll ( fixture . domainEvents , [
486+ ...events ,
487+ commandRan ( MERGING_THREAD , "gh pr merge 7" , undefined , "merge" ) ,
488+ ] ) ;
489+ yield * Queue . take ( fixture . snapshotReads ) ;
490+ yield * Queue . take ( fixture . snapshotReads ) ;
491+ yield * reactor . drain ;
492+ return yield * Ref . get ( fixture . linkCommands ) ;
493+ } ) ;
494+
495+ const mergingSnapshot = ( ) =>
496+ makeSnapshot ( [ makeThread ( MERGING_THREAD , { pullRequests : [ makeLink ( 7 , { state : "open" } ) ] } ) ] ) ;
497+
469498describe ( "PullRequestSyncReactor" , ( ) => {
470499 it . effect ( "retries a failed stack read after the summary becomes terminal" , ( ) =>
471500 Effect . scoped (
@@ -1480,4 +1509,183 @@ describe("PullRequestSyncReactor", () => {
14801509 } ) ,
14811510 ) ,
14821511 ) ;
1512+
1513+ it . effect . each ( [
1514+ {
1515+ name : "a plain create" ,
1516+ input :
1517+ 'gh pr create --base main --head feature --title "fix: 7 things" --body-file /tmp/body.md' ,
1518+ output : "https://github.com/owner/repository/pull/124\n" ,
1519+ linked : [ 124 ] ,
1520+ } ,
1521+ {
1522+ name : "a Codex shell wrapper after a push" ,
1523+ input : '/bin/zsh -lc "git push -u origin feature 2>&1 | tail -2 && gh pr create --fill"' ,
1524+ output : [
1525+ "remote: Create a pull request for 'feature' on GitHub by visiting:" ,
1526+ "remote: https://github.com/owner/repository/pull/new/feature" ,
1527+ "https://github.com/owner/repository/pull/124" ,
1528+ ] . join ( "\n" ) ,
1529+ linked : [ 124 ] ,
1530+ } ,
1531+ {
1532+ name : "a Codex script quoted in pieces around a heredoc" ,
1533+ input :
1534+ "/bin/zsh -lc \"cat > /tmp/body.md <<'EOF'\nSet the \"'`hidden`'\" flag.\nEOF\ngh pr create --fill --body-file /tmp/body.md\"" ,
1535+ output : "https://github.com/owner/repository/pull/62\n" ,
1536+ linked : [ 62 ] ,
1537+ } ,
1538+ {
1539+ name : "an older pull request read in the same command" ,
1540+ input : "gh pr view 120 --json url --jq .url && gh pr create --fill" ,
1541+ output :
1542+ "https://github.com/owner/repository/pull/120\nhttps://github.com/owner/repository/pull/124" ,
1543+ linked : [ 124 ] ,
1544+ } ,
1545+ {
1546+ name : "both layers of a stack" ,
1547+ input : "gh pr create --base main --head a --fill && gh pr create --base a --head b --fill" ,
1548+ output :
1549+ "https://github.com/owner/repository/pull/124\nhttps://github.com/owner/repository/pull/125" ,
1550+ linked : [ 124 , 125 ] ,
1551+ } ,
1552+ {
1553+ name : "the first pull request of a new repository" ,
1554+ input : "cargo test 2>&1 | tail -1 && timeout 60 gh pr create -R owner/repository --fill" ,
1555+ output : "test result: ok\nhttps://github.com/owner/repository/pull/1" ,
1556+ linked : [ 1 ] ,
1557+ } ,
1558+ {
1559+ name : "a GitLab merge request" ,
1560+ input : "glab mr create --fill --yes" ,
1561+ output :
1562+ "!3 Fix the thing (feature)\n https://gitlab.com/owner/repository/-/merge_requests/3\n" ,
1563+ linked : [ 3 ] ,
1564+ } ,
1565+ {
1566+ name : "a new pull request whose number appears in the create command" ,
1567+ input : "timeout 60 gh pr create --fill" ,
1568+ output : "https://github.com/owner/repository/pull/60" ,
1569+ linked : [ 60 ] ,
1570+ } ,
1571+ {
1572+ name : "a create subcommand that does not end its shell word" ,
1573+ input : "gh pr create#typo; gh pr view --json url --jq .url" ,
1574+ output : "https://github.com/owner/repository/pull/124" ,
1575+ linked : [ ] ,
1576+ } ,
1577+ {
1578+ name : "a create after an empty heredoc" ,
1579+ input : "cat > /tmp/notes.md <<'EOF'\nEOF\ngh pr create --fill" ,
1580+ output : "https://github.com/owner/repository/pull/124" ,
1581+ linked : [ 124 ] ,
1582+ } ,
1583+ {
1584+ name : "a read with no create" ,
1585+ input : "gh pr view 124 --json url --jq .url" ,
1586+ output : "https://github.com/owner/repository/pull/124" ,
1587+ linked : [ ] ,
1588+ } ,
1589+ {
1590+ name : "a create command quoted in another program's query" ,
1591+ input : `sqlite3 audit.db "select output from items where input like '%gh pr create%'"` ,
1592+ output : "https://github.com/owner/repository/pull/124" ,
1593+ linked : [ ] ,
1594+ } ,
1595+ {
1596+ name : "a create command with a separator inside quotes" ,
1597+ input : "echo 'example: gh pr create; gh pr create --fill' && gh pr view --json url --jq .url" ,
1598+ output : "https://github.com/owner/repository/pull/124" ,
1599+ linked : [ ] ,
1600+ } ,
1601+ {
1602+ name : "more URLs than create commands" ,
1603+ input : "gh pr view --json url --jq .url && gh pr create --fill" ,
1604+ output :
1605+ "https://github.com/owner/repository/pull/120\nhttps://github.com/owner/repository/pull/124" ,
1606+ linked : [ ] ,
1607+ } ,
1608+ {
1609+ name : "a create command only in heredoc text" ,
1610+ input :
1611+ "cat > notes.md <<'EOF'\nOpen it with gh pr create.\nEOF\ngh pr list --json url --jq '.[].url'" ,
1612+ output : "https://github.com/owner/repository/pull/124" ,
1613+ linked : [ ] ,
1614+ } ,
1615+ {
1616+ name : "a create command after a comment that starts at a separator" ,
1617+ input : "gh pr view --json url --jq .url;# note; gh pr create --fill" ,
1618+ output : "https://github.com/owner/repository/pull/124" ,
1619+ linked : [ ] ,
1620+ } ,
1621+ {
1622+ name : "a create beside a command that lists other pull requests" ,
1623+ input : "gh pr list --json url --jq '.[].url' && gh pr create --fill > /dev/null" ,
1624+ output : "https://github.com/owner/repository/pull/120" ,
1625+ linked : [ ] ,
1626+ } ,
1627+ {
1628+ name : "another host's URL beside a create command" ,
1629+ input : "gh pr create --fill" ,
1630+ output : "https://gitlab.com/owner/repository/-/merge_requests/3" ,
1631+ linked : [ ] ,
1632+ } ,
1633+ {
1634+ name : "an existing pull request quoted in another command" ,
1635+ input :
1636+ "printf '%s\\n' 'https://github.com/owner/repository/pull/120'; gh pr create --fill > /dev/null" ,
1637+ output : "https://github.com/owner/repository/pull/120" ,
1638+ linked : [ ] ,
1639+ } ,
1640+ ] ) ( "links what the command created: $name" , ( { input, output, linked } ) =>
1641+ Effect . scoped (
1642+ Effect . gen ( function * ( ) {
1643+ yield * TestClock . setTime ( Date . parse ( NOW ) ) ;
1644+ const fixture = yield * makeHarness ( { snapshot : mergingSnapshot ( ) } ) ;
1645+
1646+ yield * Effect . gen ( function * ( ) {
1647+ const reactor = yield * startAndSweep ( fixture ) ;
1648+ const child = ThreadId . make ( "delegated-child" ) ;
1649+ const links = yield * linksFrom ( fixture , reactor , [ commandRan ( child , input , output ) ] ) ;
1650+
1651+ assert . deepStrictEqual (
1652+ links . map ( ( command ) => [ command . threadId , command . number , command . source ] ) ,
1653+ linked . map ( ( number ) => [ child , number , "created" ] ) ,
1654+ ) ;
1655+ } ) . pipe ( Effect . provide ( fixture . layer ) ) ;
1656+ } ) ,
1657+ ) ,
1658+ ) ;
1659+
1660+ it . effect ( "links a created pull request once its command settles, with one command id" , ( ) =>
1661+ Effect . scoped (
1662+ Effect . gen ( function * ( ) {
1663+ yield * TestClock . setTime ( Date . parse ( NOW ) ) ;
1664+ const fixture = yield * makeHarness ( { snapshot : mergingSnapshot ( ) } ) ;
1665+
1666+ yield * Effect . gen ( function * ( ) {
1667+ const reactor = yield * startAndSweep ( fixture ) ;
1668+ const settled = commandRan (
1669+ ThreadId . make ( "agent" ) ,
1670+ "gh pr create --fill" ,
1671+ "https://github.com/owner/repository/pull/124" ,
1672+ ) ;
1673+ assert . strictEqual ( settled . type , "turn-item.updated" ) ;
1674+ if ( settled . type !== "turn-item.updated" ) return ;
1675+
1676+ const links = yield * linksFrom ( fixture , reactor , [
1677+ { ...settled , payload : { ...settled . payload , status : "running" } } ,
1678+ settled ,
1679+ settled ,
1680+ ] ) ;
1681+
1682+ assert . deepStrictEqual (
1683+ links . map ( ( command ) => command . number ) ,
1684+ [ 124 , 124 ] ,
1685+ ) ;
1686+ assert . strictEqual ( links [ 0 ] ! . commandId , links [ 1 ] ! . commandId ) ;
1687+ } ) . pipe ( Effect . provide ( fixture . layer ) ) ;
1688+ } ) ,
1689+ ) ,
1690+ ) ;
14831691} ) ;
0 commit comments