@@ -58,49 +58,62 @@ const CREATE_COMMANDS = sourceControlDefinitions.definitions.flatMap(
5858 ( { changeRequestCreateCommand : command } ) =>
5959 command === undefined
6060 ? [ ]
61- : [ { command, run : new RegExp ( COMMAND_START + command . source , "u " ) } ] ,
61+ : [ { command, run : new RegExp ( COMMAND_START + command . source , "gu " ) } ] ,
6262) ;
63- const runsChangeRequestCreateCommand = ( script : string ) =>
64- CREATE_COMMANDS . some ( ( { run } ) => run . test ( script ) ) ;
65-
66- // Codex runs each command as `/bin/zsh -lc "<script>"`.
67- const SHELL_WRAPPER = / ^ \s * (?: \S * \/ ) ? (?: b a | z | d a | k ) ? s h \s + - l ? c \s + ( [ " ' ] ) ( [ \s \S ] * ) \1\s * $ / u;
68- // A heredoc body is text a command writes, such as a pull request body, not commands it runs.
63+ const createCommandCount = ( code : string ) =>
64+ CREATE_COMMANDS . reduce ( ( count , { run } ) => count + [ ...code . matchAll ( run ) ] . length , 0 ) ;
65+
66+ // Codex runs each command as `/bin/zsh -lc <script>`, quoting the script in pieces such as
67+ // `"a "'`b`'" c"`.
68+ const SHELL_WRAPPER = / ^ \s * (?: \S * \/ ) ? (?: b a | z | d a | k ) ? s h \s + - l ? c \s + (? = [ " ' ] ) ( [ \s \S ] * ) $ / u;
69+ const SHELL_WORD_PIECE = / " ( (?: \\ [ \s \S ] | [ ^ " \\ ] ) * ) " | ' ( [ ^ ' ] * ) ' | \\ ( [ \s \S ] ) | ( \S ) / gu;
70+ // Heredoc bodies, quoted strings and comments are text a command carries, not commands it runs.
6971const 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;
7073const COMMAND_SEPARATOR = / & & | \| \| | [ ; | \n ] / u;
7174const URL_LINE = / ^ [ \t ] * ( h t t p s ? : \/ \/ \S + ) [ \t ] * $ / gmu;
7275
73- function shellScript ( input : string ) : string {
74- const [ , quote , script ] = SHELL_WRAPPER . exec ( input ) ?? [ ] ;
75- if ( script === undefined ) return input ;
76- return quote === '"' ? script . replace ( / \\ ( [ " \\ $ ` ] ) / gu, "$1" ) : script ;
76+ /** The commands a shell input runs, without the text it carries. */
77+ function shellCode ( input : string ) : string {
78+ const [ , wrapped ] = SHELL_WRAPPER . exec ( input ) ?? [ ] ;
79+ const script =
80+ wrapped === undefined
81+ ? input
82+ : Array . from (
83+ wrapped . matchAll ( SHELL_WORD_PIECE ) ,
84+ ( [ , double , single , escaped , bare ] ) =>
85+ double ?. replace ( / \\ ( [ " \\ $ ` ] ) / gu, "$1" ) ?? single ?? escaped ?? bare ,
86+ ) . join ( "" ) ;
87+ return script . replace ( HEREDOC_BODY , "<<" ) . replace ( QUOTED_OR_COMMENT , "''" ) ;
7788}
7889
7990type CommandExecution = Extract < OrchestrationV2TurnItem , { readonly type : "command_execution" } > ;
8091
8192/**
8293 * The change requests a shell command opened: URLs on lines of their own in the output of a
8394 * command that runs a host's create command. One the command also names, as `gh pr view 12`
84- * does, existed before it ran.
95+ * does, existed before it ran. More URLs than create commands means something else printed
96+ * some, such as `gh pr list`, so none are taken.
8597 */
8698function changeRequestsCreatedBy ( input : string , output : string ) {
8799 if ( ! CREATE_COMMANDS . some ( ( { command } ) => command . test ( input ) ) ) return [ ] ;
88- const script = shellScript ( input ) . replace ( HEREDOC_BODY , "<<" ) ;
89- if ( ! runsChangeRequestCreateCommand ( script ) ) return [ ] ;
90- const otherCommands = script
100+ const code = shellCode ( input ) ;
101+ const creates = createCommandCount ( code ) ;
102+ if ( creates === 0 ) return [ ] ;
103+ const otherCommands = code
91104 . split ( COMMAND_SEPARATOR )
92- . filter ( ( part ) => ! runsChangeRequestCreateCommand ( part ) )
105+ . filter ( ( part ) => createCommandCount ( part ) === 0 )
93106 . join ( "\n" ) ;
94107 const created = new Map < string , ThreadPullRequestKey & { readonly url : string } > ( ) ;
95108 for ( const [ , url = "" ] of output . matchAll ( URL_LINE ) ) {
96109 const link = sourceControlDefinitions . parseChangeRequestUrl ( url ) ?. link ;
97110 if ( link === undefined ) continue ;
98- const named = new RegExp ( `(?:^|(?<=[\\s#/=('" ]))${ link . number } (?=$|[\\s'" );&|/])` , "u" ) ;
111+ const named = new RegExp ( `(?:^|(?<=[\\s#/=(]))${ link . number } (?=$|[\\s);&|/])` , "u" ) ;
99112 if ( named . test ( otherCommands ) ) continue ;
100113 const key = normalizeThreadPullRequestKey ( link ) ;
101114 created . set ( threadPullRequestKeyOf ( key ) , { ...key , url } ) ;
102115 }
103- return [ ...created . values ( ) ] ;
116+ return created . size > creates ? [ ] : [ ...created . values ( ) ] ;
104117}
105118
106119const isPullRequestProviderError = Schema . is ( PullRequestProviderError ) ;
0 commit comments