9898_PROJECTED_PROMPT_PATTERNS = tuple (
9999 pattern for pattern , _confidence in (* COMPILED_P3_PATTERNS , * COMPILED_P4_PATTERNS )
100100)
101- # Removing line breaks can give the existing wildcard patterns a much longer
102- # search space. Interrupt the regex itself, not just work between matches.
101+ # Reconstructed projections can give wildcard patterns a much longer search
102+ # space. Interrupt the regex itself, not just work between matches.
103103_MULTILINE_PROMPT_PATTERN_SECONDS = 0.25
104104_MULTILINE_PROMPT_PATTERNS = tuple (
105105 regex .compile (pattern .pattern , regex .ASCII | regex .IGNORECASE | regex .MULTILINE )
@@ -682,9 +682,9 @@ def _projected_prompt_injection_line(
682682 else (view .text ,)
683683 )
684684 for projected_text in projected_texts :
685- for pattern in _PROJECTED_PROMPT_PATTERNS :
686- budget . check_runtime ()
687- match = pattern . search ( projected_text )
685+ matching_text = _multiline_prompt_matching_text ( projected_text , budget )
686+ for pattern in _MULTILINE_PROMPT_PATTERNS :
687+ match = next ( _timed_prompt_matches ( pattern , matching_text , budget ), None )
688688 if match is None :
689689 continue
690690 reconstructed_gaps = view .reconstructed_source_spans (match .start (), match .end ())
@@ -705,9 +705,9 @@ def _projected_prompt_injection_line(
705705 )
706706 candidate_offsets = tuple (point [0 ] for point in join_points )
707707 for projected_text in irregular_texts :
708- for pattern in _PROJECTED_PROMPT_PATTERNS :
709- budget . check_runtime ()
710- for match in pattern . finditer ( projected_text ):
708+ matching_text = _multiline_prompt_matching_text ( projected_text , budget )
709+ for pattern in _MULTILINE_PROMPT_PATTERNS :
710+ for match in _timed_prompt_matches ( pattern , matching_text , budget ):
711711 budget .check_runtime ()
712712 point_index = bisect_right (candidate_offsets , match .start ())
713713 if (
@@ -759,6 +759,57 @@ def _multiline_prompt_matching_text(text: str, budget: _ArtifactIntegrityBudget)
759759 return "" .join (parts )
760760
761761
762+ def _timed_prompt_matches (
763+ pattern : regex .Pattern [str ],
764+ text : str ,
765+ budget : _ArtifactIntegrityBudget ,
766+ ) -> Iterator [regex .Match [str ]]:
767+ """Apply the same interruptible matching budget to every prompt projection."""
768+ matching_seconds = 0.0
769+ matching_limit = _MULTILINE_PROMPT_PATTERN_SECONDS
770+ terminal_timeout = False
771+ start = 0
772+ skip_empty = False
773+ while True :
774+ budget .check_runtime ()
775+ remaining = transitive_remaining_seconds (budget .state )
776+ if remaining is not None :
777+ workflow_limit = matching_seconds + max (0.0 , remaining )
778+ # A per-pattern cap ends this artifact; workflow exhaustion remains terminal.
779+ if workflow_limit <= matching_limit :
780+ matching_limit = workflow_limit
781+ terminal_timeout = True
782+ timeout = matching_limit - matching_seconds
783+ if timeout <= 0 :
784+ raise _ArtifactIntegrityResourceLimitError (
785+ LedgerReason .RUNTIME_LIMIT ,
786+ {"observed_seconds" : matching_seconds , "limit_seconds" : matching_limit },
787+ terminal = terminal_timeout ,
788+ )
789+ started_at = time .thread_time ()
790+ expired = False
791+ try :
792+ # Restart across yields so consumer work never uses the match budget.
793+ matches = pattern .finditer (text , pos = start , timeout = timeout , concurrent = False )
794+ if skip_empty :
795+ next (matches , None )
796+ match = next (matches , None )
797+ except TimeoutError :
798+ expired = True
799+ finally :
800+ matching_seconds += max (0.0 , time .thread_time () - started_at )
801+ budget .check_runtime ()
802+ if expired :
803+ # regex counts process CPU, including native work in other threads.
804+ # Retry only the same search; the thread and workflow budgets still bound it.
805+ continue
806+ if match is None :
807+ return
808+ start = match .end ()
809+ skip_empty = match .start () == start
810+ yield match
811+
812+
762813def _multiline_prompt_injection_line (
763814 content : str ,
764815 budget : _ArtifactIntegrityBudget ,
@@ -770,49 +821,27 @@ def _multiline_prompt_injection_line(
770821 matching_text = _multiline_prompt_matching_text (view .text , budget )
771822 first_offset : int | None = None
772823 for pattern in _MULTILINE_PROMPT_PATTERNS :
773- budget .check_runtime ()
774- remaining = transitive_remaining_seconds (budget .state )
775- timeout = _MULTILINE_PROMPT_PATTERN_SECONDS
776- terminal_timeout = False
777- if remaining is not None :
778- terminal_timeout = remaining <= timeout
779- timeout = min (timeout , max (0.0 , remaining ))
780- started_at = time .monotonic ()
781824 reconstruction_index = 0
782- try :
783- # Keep this short, interruptible search on the current thread.
784- # Releasing the GIL lets another analyzer consume its wall-clock
785- # allowance and turn ordinary prose into a false timeout.
786- for match in pattern .finditer (matching_text , timeout = timeout , concurrent = False ):
825+ for match in _timed_prompt_matches (pattern , matching_text , budget ):
826+ # Matches and reconstruction spans are both ordered. Advance
827+ # once per span, including ordinary matches before a spaced
828+ # instruction, instead of rescanning all provenance per match.
829+ while (
830+ reconstruction_index < len (view .reconstructions )
831+ and view .reconstructions [reconstruction_index ].derived_end <= match .start () + 1
832+ ):
787833 budget .check_runtime ()
788- # Matches and reconstruction spans are both ordered. Advance
789- # once per span, including ordinary matches before a spaced
790- # instruction, instead of rescanning all provenance per match.
791- while (
792- reconstruction_index < len (view .reconstructions )
793- and view .reconstructions [reconstruction_index ].derived_end <= match .start () + 1
794- ):
795- budget .check_runtime ()
796- reconstruction_index += 1
797- if reconstruction_index == len (view .reconstructions ):
798- break
799- reconstruction = view .reconstructions [reconstruction_index ]
800- right = max (match .start () + 1 , reconstruction .derived_start + 1 )
801- if right < min (match .end (), reconstruction .derived_end ):
802- source_offset = view .source_offset (right - 1 ) + 1
803- if first_offset is None or source_offset < first_offset :
804- first_offset = source_offset
805- # Later matches cannot precede this pattern's first gap.
806- break
807- except TimeoutError as exc :
808- raise _ArtifactIntegrityResourceLimitError (
809- LedgerReason .RUNTIME_LIMIT ,
810- {
811- "observed_seconds" : max (0.0 , time .monotonic () - started_at ),
812- "limit_seconds" : timeout ,
813- },
814- terminal = terminal_timeout ,
815- ) from exc
834+ reconstruction_index += 1
835+ if reconstruction_index == len (view .reconstructions ):
836+ break
837+ reconstruction = view .reconstructions [reconstruction_index ]
838+ right = max (match .start () + 1 , reconstruction .derived_start + 1 )
839+ if right < min (match .end (), reconstruction .derived_end ):
840+ source_offset = view .source_offset (right - 1 ) + 1
841+ if first_offset is None or source_offset < first_offset :
842+ first_offset = source_offset
843+ # Later matches cannot precede this pattern's first gap.
844+ break
816845 return get_line_number (content , first_offset ) if first_offset is not None else None
817846
818847
0 commit comments