@@ -713,6 +713,24 @@ const programUse = (program: string, names: (statement: string) => boolean) => {
713713 return use ;
714714} ;
715715
716+ /** The `splitTop` parts that can run: `false && …` and `true || …` skip what follows that operator. */
717+ const liveParts = ( script : string ) => {
718+ const parts = splitTop ( script ) ;
719+ const live : Array < Part > = [ ] ;
720+ let dead : string | undefined ;
721+ for ( const [ i , part ] of parts . entries ( ) ) {
722+ if ( dead !== undefined && parts [ i - 1 ] ?. op === dead ) continue ;
723+ dead = undefined ;
724+ if (
725+ ( / ^ f a l s e $ / . test ( part . text ) && part . op === "&&" ) ||
726+ ( / ^ (?: t r u e | : ) $ / . test ( part . text ) && part . op === "||" )
727+ )
728+ dead = part . op ;
729+ else live . push ( part ) ;
730+ }
731+ return live ;
732+ } ;
733+
716734type Language = "python" | "js" ;
717735/** The language of an interpreter's inline program that `inlineProgramUses` reads. */
718736const programLanguage = ( head : string ) : Language | undefined =>
@@ -722,6 +740,16 @@ const programLanguage = (head: string): Language | undefined =>
722740 ? "js"
723741 : undefined ;
724742
743+ /** A written script's language: its extension, its shebang, or the interpreter that runs it. */
744+ const scriptLanguage = ( path : string , source : string , stage : string ) : Language | undefined => {
745+ const shebang = / ^ # ! ( .* ) / . exec ( source ) ?. [ 1 ] ?? "" ;
746+ if ( / \. p y $ / . test ( path ) || / \b p y t h o n 3 ? \b / . test ( shebang ) ) return "python" ;
747+ if ( / \. [ c m ] ? [ j t ] s x ? $ / . test ( path ) || / \b (?: n o d e | d e n o | b u n | t s x ) \b / . test ( shebang ) ) return "js" ;
748+ return programLanguage (
749+ withoutAssignments ( stage ) . replace ( / ^ (?: (?: s u d o | e n v ) (?: \s + - \S + ) * \s + ) + / , "" ) ,
750+ ) ;
751+ } ;
752+
725753const PROGRAM_COMMENT : Readonly < Record < Language , RegExp > > = {
726754 python : / # [ ^ \n ] * / y,
727755 js : / \/ \/ [ ^ \n ] * | \/ \* [ \s \S ] * ?(?: \* \/ | $ ) / y,
@@ -1279,7 +1307,8 @@ const noteWrites = (call: SecretRun["calls"][number], scripts: Map<string, strin
12791307 else if ( scripts . has ( path ) && old ) scripts . set ( path , scripts . get ( path ) ! . replace ( old , next ) ) ;
12801308 }
12811309 if ( SHELL . has ( call . tool ) && call . tool !== "exec" )
1282- for ( const { text } of splitTop ( inputString ( call . input , "command" ) ) ) noteHeredoc ( text , scripts ) ;
1310+ for ( const { text } of liveParts ( inputString ( call . input , "command" ) ) )
1311+ noteHeredoc ( text , scripts ) ;
12831312} ;
12841313/** The scripts the agent had written before each call. */
12851314const scriptsBefore = ( calls : SecretRun [ "calls" ] ) : Array < Scripts > => {
@@ -1333,7 +1362,7 @@ export const scriptSources = (calls: SecretRun["calls"]): Array<Array<string>> =
13331362 if ( ! SHELL . has ( call . tool ) || call . tool === "exec" ) return [ ] ;
13341363 const scripts = new Map ( before [ index ] ) ;
13351364 const ran = new Set < string > ( ) ;
1336- for ( const { text } of splitTop ( inputString ( call . input , "command" ) ) ) {
1365+ for ( const { text } of liveParts ( inputString ( call . input , "command" ) ) ) {
13371366 for ( const stage of splitPipe ( text ) )
13381367 for ( const path of runsScripts ( stage , scripts ) ) ran . add ( scripts . get ( path ) ! ) ;
13391368 noteHeredoc ( text , scripts ) ;
@@ -1450,8 +1479,8 @@ export const classifyFileUse = (
14501479 // A written script that reads the file by its id prints it, writes it to a file, or passes it on
14511480 // (a consume, at the command and the statement that receives it).
14521481 const fileIds = files . map ( fileId ) ;
1453- const scriptVerdict = ( source : string , stage : string ) : FileUse => {
1454- const language = programLanguage ( withoutAssignments ( stage ) ) ;
1482+ const scriptVerdict = ( path : string , source : string , stage : string ) : FileUse => {
1483+ const language = scriptLanguage ( path , source , stage ) ;
14551484 const use = programUse (
14561485 language === undefined ? source : maskProgram ( source , language , true ) ,
14571486 ( statement ) => fileIds . some ( ( id ) => statement . includes ( id ) ) ,
@@ -1543,17 +1572,7 @@ export const classifyFileUse = (
15431572 } ;
15441573
15451574 const visit = ( script : string ) => {
1546- let dead : string | undefined ;
1547- const parts = splitTop ( script ) ;
1548- for ( const [ i , { text : part , op } ] of parts . entries ( ) ) {
1549- const before = parts [ i - 1 ] ?. op ;
1550- if ( dead !== undefined && before === dead ) continue ;
1551- dead = undefined ;
1552- // `false && …` and `true || …` never run what follows on that operator.
1553- if ( ( / ^ f a l s e $ / . test ( part ) && op === "&&" ) || ( / ^ (?: t r u e | : ) $ / . test ( part ) && op === "||" ) ) {
1554- dead = op ;
1555- continue ;
1556- }
1575+ for ( const { text : part } of liveParts ( script ) ) {
15571576 const set = / ^ s e t \s + ( .* ) $ / . exec ( part ) ?. [ 1 ] ;
15581577 if ( set !== undefined ) {
15591578 // set -x, set -euxo pipefail and set -o xtrace turn tracing on; their + forms turn it off.
@@ -1597,7 +1616,7 @@ export const classifyFileUse = (
15971616 continue ;
15981617 }
15991618 }
1600- const use = scriptVerdict ( source , stage ) ;
1619+ const use = scriptVerdict ( path , source , stage ) ;
16011620 if ( use . verdict === "print (script prints the value)" && routed ) flow ( after , own , stage ) ;
16021621 else uses . push ( use ) ;
16031622 }
0 commit comments