@@ -55,10 +55,10 @@ const isChangeRequestCloseCommand = (command: string) =>
5555const COMMAND_START =
5656 "(?:^|[;&|({`\\n]|\\b(?:do|then|else)\\s)\\s*(?:(?:\\w+=\\S*|env|time|nohup|timeout\\s+\\S+)\\s+)*" ;
5757const CREATE_COMMANDS = sourceControlDefinitions . definitions . flatMap (
58- ( { changeRequestCreateCommand : command } ) =>
58+ ( { kind , changeRequestCreateCommand : command } ) =>
5959 command === undefined
6060 ? [ ]
61- : [ { command, run : new RegExp ( COMMAND_START + command . source , "gu" ) } ] ,
61+ : [ { kind , command, run : new RegExp ( COMMAND_START + command . source , "gu" ) } ] ,
6262) ;
6363const createCommandCount = ( code : string ) =>
6464 CREATE_COMMANDS . reduce ( ( count , { run } ) => count + [ ...code . matchAll ( run ) ] . length , 0 ) ;
@@ -69,8 +69,11 @@ const SHELL_WRAPPER = /^\s*(?:\S*\/)?(?:ba|z|da|k)?sh\s+-l?c\s+(?=["'])([\s\S]*)
6969const SHELL_WORD_PIECE = / " ( (?: \\ [ \s \S ] | [ ^ " \\ ] ) * ) " | ' ( [ ^ ' ] * ) ' | \\ ( [ \s \S ] ) | ( \S ) / gu;
7070// Heredoc bodies, quoted strings and comments are text a command carries, not commands it runs.
7171const HEREDOC_BODY = / < < - ? [ \t ] * ( [ ' " ] ? ) ( \w + ) \1[ ^ \n ] * \n [ \s \S ] * ?(?: \n [ \t ] * \2[ \t ] * (? = \n | $ ) | $ ) / gu;
72- const QUOTED_OR_COMMENT = / ' [ ^ ' ] * ' | " (?: \\ [ \s \S ] | [ ^ " \\ ] ) * " | (?< = ^ | \s ) # [ ^ \n ] * / gmu;
72+ const QUOTED_OR_COMMENT = / ' [ ^ ' ] * ' | " (?: \\ [ \s \S ] | [ ^ " \\ ] ) * " | (?< = ^ | [ \s ; & | ( ] ) # [ ^ \n ] * / gmu;
7373const COMMAND_SEPARATOR = / & & | \| \| | [ ; | \n ] / u;
74+ // Commands that print other change requests' URLs, so output beside them proves nothing.
75+ const LISTS_CHANGE_REQUESTS =
76+ / \b (?: g h \s + (?: p r \s + (?: l i s t | s t a t u s ) | s e a r c h \s + p r s | a p i ) | g l a b \s + (?: m r \s + l i s t | a p i ) ) \b / u;
7477const URL_LINE = / ^ [ \t ] * ( h t t p s ? : \/ \/ \S + ) [ \t ] * $ / gmu;
7578
7679/** The commands a shell input runs, without the text it carries. */
@@ -104,10 +107,16 @@ function changeRequestsCreatedBy(input: string, output: string) {
104107 . split ( COMMAND_SEPARATOR )
105108 . filter ( ( part ) => createCommandCount ( part ) === 0 )
106109 . join ( "\n" ) ;
110+ if ( LISTS_CHANGE_REQUESTS . test ( otherCommands ) ) return [ ] ;
111+ const creators = new Set (
112+ CREATE_COMMANDS . filter ( ( { run } ) => code . match ( run ) !== null ) . map ( ( { kind } ) => kind ) ,
113+ ) ;
107114 const created = new Map < string , ThreadPullRequestKey & { readonly url : string } > ( ) ;
108115 for ( const [ , url = "" ] of output . matchAll ( URL_LINE ) ) {
109- const link = sourceControlDefinitions . parseChangeRequestUrl ( url ) ?. link ;
110- if ( link === undefined ) continue ;
116+ const parsed = sourceControlDefinitions . parseChangeRequestUrl ( url ) ;
117+ // Only the host whose create command ran can have printed a new change request.
118+ if ( parsed === null || ! creators . has ( parsed . definition . kind ) ) continue ;
119+ const { link } = parsed ;
111120 const named = new RegExp ( `(?:^|(?<=[\\s#/=(]))${ link . number } (?=$|[\\s);&|/])` , "u" ) ;
112121 if ( named . test ( otherCommands ) ) continue ;
113122 const key = normalizeThreadPullRequestKey ( link ) ;
@@ -568,30 +577,32 @@ export const make = Effect.gen(function* () {
568577 ) ;
569578
570579 // An agent that opens a pull request from a shell gets it linked as link_pull_request would.
571- const linkCreatedChangeRequests = ( threadId : ThreadId , item : CommandExecution ) =>
572- isOrchestrationV2WorkActive ( item . status )
573- ? Effect . void
574- : Effect . forEach (
575- changeRequestsCreatedBy ( item . input , item . output ?? "" ) ,
576- ( created ) =>
577- engine
578- . dispatch ( {
579- type : "thread.pull-request.link" ,
580- // Stable per command, so a repeated settlement cannot relink one the user unlinked.
581- commandId : CommandId . make (
582- `server:pr-created-link:${ threadId } :${ item . id } :${ threadPullRequestKeyOf ( created ) } ` ,
583- ) ,
584- threadId,
585- ...created ,
586- source : "created" ,
587- } )
588- . pipe (
589- Effect . catchCause (
590- logSkipped ( "linking a pull request a command created failed" , { threadId } ) ,
591- ) ,
580+ const linkCreatedChangeRequests = Effect . fn ( "PullRequestSyncReactor.linkCreatedChangeRequests" ) (
581+ function * ( threadId : ThreadId , item : CommandExecution ) {
582+ if ( isOrchestrationV2WorkActive ( item . status ) ) return ;
583+ yield * Effect . forEach (
584+ changeRequestsCreatedBy ( item . input , item . output ?? "" ) ,
585+ ( created ) =>
586+ engine
587+ . dispatch ( {
588+ type : "thread.pull-request.link" ,
589+ // Stable per command, so a repeated settlement cannot relink one the user unlinked.
590+ commandId : CommandId . make (
591+ `server:pr-created-link:${ threadId } :${ item . id } :${ threadPullRequestKeyOf ( created ) } ` ,
592592 ) ,
593- { discard : true } ,
594- ) ;
593+ threadId,
594+ ...created ,
595+ source : "created" ,
596+ } )
597+ . pipe (
598+ Effect . catchCause (
599+ logSkipped ( "linking a pull request a command created failed" , { threadId } ) ,
600+ ) ,
601+ ) ,
602+ { discard : true } ,
603+ ) ;
604+ } ,
605+ ) ;
595606
596607 const start : PullRequestSyncReactor [ "Service" ] [ "start" ] = Effect . fn (
597608 "PullRequestSyncReactor.start" ,
0 commit comments