4141import re
4242import subprocess
4343import threading
44+ import time
4445from pathlib import Path
46+ from types import SimpleNamespace
4547
4648import httpx
4749
4850from app .core import ai , learned
4951from app .core .db import session_scope
5052from app .core .models import (Finding , FindingAnalysis , FindingKind ,
51- FindingState , Severity , Waiver , utcnow )
53+ FindingState , Severity , UserRole , Waiver , utcnow )
5254
5355log = logging .getLogger ("gcr.analysis" )
5456
@@ -591,7 +593,7 @@ def _parse(text: str) -> tuple:
591593# --------------------------------------------------------------------- run
592594
593595def run (finding_id : int , user , force : bool = False , wait : bool = False ,
594- learn : bool = True , requeue : bool = True ) -> dict :
596+ learn : bool = True , requeue : bool = True , paced : bool = False ) -> dict :
595597 """Ask about one finding and keep the answer. -> {ok, detail, id}
596598
597599 The caller has already checked the person may see the finding and may ask.
@@ -600,6 +602,7 @@ def run(finding_id: int, user, force: bool = False, wait: bool = False,
600602 refusing, which is what a bulk run wants. learn=False keeps the answer
601603 to this one finding. requeue=False leaves the findings of a rule this
602604 answer retires at their restored grade instead of asking about each.
605+ paced keeps any batch this starts under the automatic review's rate.
603606 """
604607 with _busy_lock :
605608 if finding_id in _busy :
@@ -612,7 +615,7 @@ def run(finding_id: int, user, force: bool = False, wait: bool = False,
612615 return {"ok" : False ,
613616 "detail" : "Two validations are already running. Try again in a moment." }
614617 try :
615- return _run (finding_id , user , force , learn , requeue )
618+ return _run (finding_id , user , force , learn , requeue , paced )
616619 finally :
617620 with _busy_lock :
618621 _busy .discard (finding_id )
@@ -647,7 +650,7 @@ def _answer_from_rule(db, row, user) -> dict:
647650
648651
649652def _run (finding_id : int , user , force : bool = False , learn : bool = True ,
650- requeue : bool = True ) -> dict :
653+ requeue : bool = True , paced : bool = False ) -> dict :
651654 from app .core import rbac
652655
653656 with session_scope () as db :
@@ -715,7 +718,8 @@ def _run(finding_id: int, user, force: bool = False, learn: bool = True,
715718
716719 # Started after the commit, so the batch reads the grades put back.
717720 if others and requeue :
718- number = start_batch (others , user , force = True , learn = False , requeue = False )
721+ number = start_batch (others , user , force = True , learn = False , requeue = False ,
722+ paced = paced )
719723 outcome += (f" The { len (others )} other finding{ '' if len (others ) == 1 else 's' } "
720724 f"it graded { 'was' if len (others ) == 1 else 'were' } put back and "
721725 f"queued for { 'its' if len (others ) == 1 else 'their' } own reading "
@@ -869,15 +873,16 @@ def rule_only(finding_id: int, user) -> dict:
869873
870874
871875def start_batch (finding_ids : list , user , rules_only : bool = False , force : bool = False ,
872- learn : bool = True , requeue : bool = True ) -> int :
876+ learn : bool = True , requeue : bool = True , paced : bool = False ) -> int :
873877 """Validate many findings in the background. -> the batch number.
874878
875879 Ordered so findings about the same thing sit together: the same
876880 repository, kind and rule or package. The first of a group goes to the
877881 model, and when what it learns covers the rest they are answered from the
878882 rule without a model call, which is where a bulk run saves its tokens.
879883 force asks the model about every one, past any rule, which is how the
880- findings of a retired rule each get their own reading.
884+ findings of a retired rule each get their own reading. paced spaces the
885+ questions out to the automatic review's rate.
881886 """
882887 with _batch_lock :
883888 _batch_seq [0 ] += 1
@@ -888,12 +893,12 @@ def start_batch(finding_ids: list, user, rules_only: bool = False, force: bool =
888893 "done" : 0 , "asked" : 0 , "from_rules" : 0 , "lowered" : 0 ,
889894 "skipped_protected" : 0 , "skipped_done" : 0 , "errors" : 0 ,
890895 "tokens" : 0 , "rules_learned" : 0 , "skipped_no_rule" : 0 ,
891- "rules_only" : rules_only , "results" : []}
896+ "rules_only" : rules_only , "paced" : paced , " results" : []}
892897 # Keep the twenty newest; a batch is a progress display, not a record.
893898 for old in sorted (_batches )[:- 20 ]:
894899 _batches .pop (old , None )
895900 worker = threading .Thread (target = _batch , args = (number , list (finding_ids ), user , rules_only ,
896- force , learn , requeue ),
901+ force , learn , requeue , paced ),
897902 name = f"ai-batch-{ number } " , daemon = True )
898903 worker .start ()
899904 return number
@@ -921,7 +926,8 @@ def _note(number: int, **change) -> None:
921926
922927
923928def _batch (number : int , finding_ids : list , user , rules_only : bool = False ,
924- force : bool = False , learn : bool = True , requeue : bool = True ) -> None :
929+ force : bool = False , learn : bool = True , requeue : bool = True ,
930+ paced : bool = False ) -> None :
925931 from app .core import rbac
926932 from app .core .models import AIGrade , LearnedRule
927933
@@ -934,6 +940,11 @@ def _batch(number: int, finding_ids: list, user, rules_only: bool = False,
934940 (f .package_name if f .kind == FindingKind .DEPENDENCY else f .rule ) or "" ,
935941 - learned .RANK .get (f .severity .value , 0 ), f .id ))
936942 plan = []
943+ # A finding a rule already covers is answered without a request,
944+ # so it does not wait its turn. A rule learned partway through
945+ # can only make the pacing slower than it needs to be.
946+ rules = learned .load_rules (db ) if paced and not force else []
947+ per_minute = int (ai .config (db ).get ("ai.auto_review_per_minute" ) or 0 )
937948 for f in order :
938949 why = learned .protected (f )
939950 if why :
@@ -944,7 +955,8 @@ def _batch(number: int, finding_ids: list, user, rules_only: bool = False,
944955 if done is not None :
945956 plan .append ((f .id , f .vuln_id , "done" ))
946957 continue
947- plan .append ((f .id , f .vuln_id , "ask" ))
958+ covered = bool (rules ) and learned .match (rules , f ) is not None
959+ plan .append ((f .id , f .vuln_id , "rule" if covered else "ask" ))
948960 rules_before = db .query (LearnedRule ).count ()
949961 for finding_id , vuln_id , step in plan :
950962 label = vuln_id or str (finding_id )
@@ -957,10 +969,12 @@ def _batch(number: int, finding_ids: list, user, rules_only: bool = False,
957969 _note (number , done = 1 , skipped_done = 1 ,
958970 result = f"{ label } : already validated for this code" )
959971 continue
972+ if paced and step == "ask" and not rules_only :
973+ _wait_turn (per_minute )
960974 try :
961975 result = (rule_only (finding_id , user ) if rules_only
962976 else run (finding_id , user , force = force , wait = True ,
963- learn = learn , requeue = requeue ))
977+ learn = learn , requeue = requeue , paced = paced ))
964978 except Exception : # one bad finding must not stop the rest
965979 log .exception ("bulk validation of %s failed" , finding_id )
966980 result = {"ok" : False , "detail" : "failed; see the log" }
@@ -985,3 +999,72 @@ def _batch(number: int, finding_ids: list, user, rules_only: bool = False,
985999 with _batch_lock :
9861000 if number in _batches :
9871001 _batches [number ]["finished" ] = utcnow ()
1002+
1003+
1004+ # ------------------------------------------------------------ automatic
1005+
1006+ # Who the automatic review says it is. It reads what a scan found for
1007+ # everybody, so it sees what an administrator sees, and its name is on every
1008+ # answer it leaves so nobody mistakes it for a person.
1009+ AUTO_REVIEWER = SimpleNamespace (username = "auto-review" , role = UserRole .ADMIN ,
1010+ disabled = False )
1011+
1012+ # One clock for every paced batch. Two scans finishing close together start
1013+ # two batches, and each keeping to the rate on its own would be twice it.
1014+ _pace_lock = threading .Lock ()
1015+ _next_turn = [0.0 ]
1016+
1017+
1018+ def _wait_turn (per_minute : int ) -> None :
1019+ """Hold until the next question may go. 0 or less never waits."""
1020+ if per_minute <= 0 :
1021+ return
1022+ gap = 60.0 / per_minute
1023+ with _pace_lock :
1024+ now = time .monotonic ()
1025+ turn = max (now , _next_turn [0 ])
1026+ _next_turn [0 ] = turn + gap
1027+ if turn > now :
1028+ time .sleep (turn - now )
1029+
1030+
1031+ def auto_review (finding_ids ) -> int :
1032+ """Hand a scan's new findings to the model, when that is switched on.
1033+ -> the batch number, or 0 when nothing was started.
1034+
1035+ Worst first, up to the per scan limit, so a first scan of a large estate
1036+ reads the criticals and stops instead of running up a bill for every low
1037+ there is. The rest are still there to pick from the findings list.
1038+ Never raises: a scan is not worth failing over this.
1039+ """
1040+ from app .core .audit import record
1041+
1042+ ids = sorted ({int (i ) for i in finding_ids or []})
1043+ if not ids :
1044+ return 0
1045+ try :
1046+ with session_scope () as db :
1047+ cfg = ai .config (db )
1048+ if not cfg .get ("ai.auto_review" ) or not ai .enabled (cfg ) or ai .missing (cfg ):
1049+ return 0
1050+ limit = int (cfg .get ("ai.auto_review_max" ) or 0 )
1051+ rows = (db .query (Finding .id , Finding .severity )
1052+ .filter (Finding .id .in_ (ids )).all ())
1053+ rows .sort (key = lambda r : (- learned .RANK .get (learned ._value (r .severity ), 0 ), r .id ))
1054+ chosen = [r .id for r in rows ]
1055+ left = 0
1056+ if limit and len (chosen ) > limit :
1057+ left = len (chosen ) - limit
1058+ chosen = chosen [:limit ]
1059+ if not chosen :
1060+ return 0
1061+ number = start_batch (chosen , AUTO_REVIEWER , paced = True )
1062+ record (db , "ai.auto_review" , actor = AUTO_REVIEWER .username ,
1063+ detail = (f"batch { number } : { len (chosen )} new finding(s) queued at "
1064+ f"{ cfg .get ('ai.auto_review_per_minute' )} a minute"
1065+ + (f", { left } over the per scan limit left for a person"
1066+ if left else "" )))
1067+ return number
1068+ except Exception : # noqa: BLE001
1069+ log .exception ("could not start the automatic review" )
1070+ return 0
0 commit comments