runner.py 258 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907
  1. """
  2. Agent Runner - Agent 执行引擎
  3. 核心职责:
  4. 1. 执行 Agent 任务(循环调用 LLM + 工具)
  5. 2. 记录执行轨迹(Trace + Messages + GoalTree)
  6. 3. 加载和注入技能(Skill)
  7. 4. 管理执行计划(GoalTree)
  8. 5. 支持续跑(continue)和回溯重跑(rewind)
  9. 参数分层:
  10. - Infrastructure: AgentRunner 构造时设置(trace_store, llm_call 等)
  11. - RunConfig: 每次 run 时指定(model, trace_id, after_sequence 等)
  12. - Messages: OpenAI SDK 格式的任务消息
  13. """
  14. import asyncio
  15. from copy import deepcopy
  16. import json
  17. import logging
  18. import os
  19. import uuid
  20. from dataclasses import dataclass, field
  21. from datetime import datetime
  22. from hashlib import sha256
  23. from typing import AsyncIterator, Optional, Dict, Any, List, Callable, Literal, Tuple, Union
  24. from cyber_agent.trace.models import Trace, Message
  25. from cyber_agent.trace.protocols import TraceStore
  26. from cyber_agent.trace.trace_id import generate_sub_trace_id
  27. from cyber_agent.trace.goal_models import GoalTree
  28. from cyber_agent.trace.compaction import (
  29. CompressionConfig,
  30. compress_completed_goals,
  31. estimate_tokens,
  32. needs_level2_compression,
  33. build_compression_prompt,
  34. )
  35. from cyber_agent.skill.models import Skill
  36. from cyber_agent.skill.skill_loader import load_skills_from_dir
  37. from cyber_agent.tools import ToolRegistry, get_tool_registry
  38. from cyber_agent.tools.approval import (
  39. ToolApprovalBatchV1,
  40. ToolApprovalCallV1,
  41. approval_grant,
  42. tool_argument_hash,
  43. )
  44. from cyber_agent.tools.builtin.knowledge import KnowledgeConfig
  45. from cyber_agent.core.memory import (
  46. MEMORY_IDENTITY_CONTEXT_KEY,
  47. MemoryConfig,
  48. compute_memory_identity,
  49. )
  50. from cyber_agent.core.dream import DreamScope
  51. from cyber_agent.core.run_snapshot import (
  52. RUN_CONFIG_SNAPSHOT_CONTEXT_KEY,
  53. RunConfigSnapshotError,
  54. RunConfigSnapshotV1,
  55. RunConfigSnapshotV2,
  56. load_run_config_snapshot,
  57. persist_run_config_snapshot,
  58. )
  59. from cyber_agent.core.agent_mode import (
  60. AGENT_MODE_CONTEXT_KEY,
  61. AgentMode,
  62. RECURSIVE_CAPABILITY_TOOLS_CONTEXT_KEY,
  63. RECURSIVE_CHILD_EXECUTION_MODE_CONTEXT_KEY,
  64. RECURSIVE_MAX_PARALLEL_CHILDREN_CONTEXT_KEY,
  65. apply_policy_to_context,
  66. assert_removed_config_absent,
  67. policy_from_context,
  68. CURRENT_RECURSIVE_REVISION,
  69. policy_from_environment,
  70. require_mutable_trace_policy,
  71. validate_recursive_child_execution,
  72. )
  73. from cyber_agent.core.task_protocol import (
  74. RootTaskAnchor,
  75. ensure_task_protocol,
  76. initialize_task_progress,
  77. new_task_protocol,
  78. task_progress_artifact_refs,
  79. task_progress_at_revision,
  80. task_progress_readiness_error,
  81. rewind_task_progress,
  82. protocol_error_report,
  83. rebuild_pending_replans,
  84. )
  85. from cyber_agent.core.task_protocol_service import TaskProtocolService
  86. from cyber_agent.core.context_policy import (
  87. canonical_json,
  88. ContextPolicyError,
  89. normalize_root_task_anchor,
  90. persist_root_task_anchor,
  91. prune_context_access,
  92. render_recursive_context,
  93. get_authorized_context_snapshot,
  94. require_matching_root_task_anchor,
  95. require_root_task_anchor,
  96. replace_context_access,
  97. )
  98. from cyber_agent.core.resource_budget import (
  99. RESOURCE_BUDGET_CONTEXT_KEY,
  100. ResourceBudget,
  101. ResourceBudgetController,
  102. ResourceBudgetExceeded,
  103. ResourceBudgetStateError,
  104. )
  105. from cyber_agent.core.artifacts import (
  106. ArtifactRef,
  107. ArtifactResolver,
  108. MaterialIssue,
  109. ValidationMaterial,
  110. extract_artifact_refs,
  111. material_chars,
  112. material_content_hash,
  113. resolve_artifact_refs,
  114. )
  115. from cyber_agent.core.validation import (
  116. ValidationCheck,
  117. ValidationCheckSpec,
  118. LLMValidator,
  119. ScopeValidationResult,
  120. ValidationPolicy,
  121. ValidationResult,
  122. ValidationRun,
  123. ValidationScope,
  124. ValidatorSettings,
  125. persist_validation_policy,
  126. require_validation_policy,
  127. )
  128. from cyber_agent.core.validator_web import (
  129. PageFetcher,
  130. SerperWebSearchProvider,
  131. ValidatorToolLimits,
  132. ValidatorToolSession,
  133. ValidatorWebSearchProvider,
  134. )
  135. from cyber_agent.core.prompts import (
  136. DEFAULT_SYSTEM_PREFIX,
  137. TRUNCATION_HINT,
  138. TOOL_INTERRUPTED_MESSAGE,
  139. AGENT_INTERRUPTED_SUMMARY,
  140. AGENT_CONTINUE_HINT_TEMPLATE,
  141. TASK_NAME_GENERATION_SYSTEM_PROMPT,
  142. TASK_NAME_FALLBACK,
  143. SUMMARY_HEADER_TEMPLATE,
  144. build_summary_header,
  145. build_tool_interrupted_message,
  146. build_agent_continue_hint,
  147. )
  148. logger = logging.getLogger(__name__)
  149. @dataclass
  150. class ContextUsage:
  151. """Context 使用情况"""
  152. trace_id: str
  153. message_count: int
  154. token_count: int
  155. max_tokens: int
  156. usage_percent: float
  157. image_count: int = 0
  158. @dataclass
  159. class SideBranchContext:
  160. """侧分支上下文(压缩/反思/知识评估)"""
  161. type: Literal["compression", "reflection", "knowledge_eval"]
  162. branch_id: str
  163. start_head_seq: int # 侧分支起点的 head_seq
  164. start_sequence: int # 侧分支第一条消息的 sequence
  165. start_history_length: int # 侧分支起点的 history 长度
  166. start_iteration: int # 旧 Trace 兼容字段;不再用于轮次预算
  167. max_turns: int = 5 # 最大轮次
  168. turns_used: int = 0 # 已持久化的 assistant 轮次
  169. def to_dict(self) -> Dict[str, Any]:
  170. """转换为字典(用于持久化和传递给工具)"""
  171. return {
  172. "type": self.type,
  173. "branch_id": self.branch_id,
  174. "start_head_seq": self.start_head_seq,
  175. "start_sequence": self.start_sequence,
  176. "start_iteration": self.start_iteration,
  177. "max_turns": self.max_turns,
  178. "turns_used": self.turns_used,
  179. "is_side_branch": True,
  180. "started_at": datetime.now().isoformat(),
  181. }
  182. # ===== 运行配置 =====
  183. @dataclass
  184. class RunConfig:
  185. """
  186. 运行参数 — 控制 Agent 如何执行
  187. 分为模型层参数(由上游 agent 或用户决定)和框架层参数(由系统注入)。
  188. """
  189. # --- 模型层参数 ---
  190. model: str = "gpt-4o"
  191. temperature: float = 0.3
  192. max_iterations: int = 200
  193. tools: Optional[List[str]] = None # None = 按 tool_groups 过滤;显式列表 = 精确指定
  194. tool_groups: Optional[List[str]] = field(default_factory=lambda: ["core"]) # 工具分组白名单;默认仅 core,项目按需追加
  195. exclude_tools: List[str] = field(default_factory=list) # 从 tools / tool_groups 结果中再排除的工具名(如远程 agent 禁用 agent/evaluate)
  196. side_branch_max_turns: int = 5 # 侧分支最大轮次(压缩/反思)
  197. goal_compression: Literal["none", "on_complete", "on_overflow"] = "on_overflow" # Goal 压缩模式
  198. # --- 强制侧分支(用于 API 手动触发或自动压缩流程)---
  199. # 使用列表作为侧分支队列,每次完成一个侧分支后 pop(0) 取下一个
  200. force_side_branch: Optional[List[Literal["compression", "reflection"]]] = None
  201. # --- 框架层参数 ---
  202. agent_type: str = "default"
  203. uid: Optional[str] = None
  204. system_prompt: Optional[str] = None # None = 从 skills 自动构建
  205. skills: Optional[List[str]] = None # 注入 system prompt 的 skill 名称列表;None = 按 preset 决定
  206. enable_memory: bool = True
  207. auto_execute_tools: bool = True
  208. name: Optional[str] = None # 显示名称(空则由 utility_llm 自动生成)
  209. enable_prompt_caching: bool = True # 启用 Anthropic Prompt Caching(仅 Claude 模型有效)
  210. parallel_tool_execution: bool = False # 是否启用并发 Tool Call 执行(慎用,需确保无资源冲突)
  211. child_execution_mode: Literal["sequential", "parallel"] = "sequential"
  212. max_parallel_children: int = 2
  213. root_task_anchor: Optional[RootTaskAnchor] = None
  214. # --- Trace 控制 ---
  215. trace_id: Optional[str] = None # None = 新建
  216. parent_trace_id: Optional[str] = None # 子 Agent 专用
  217. parent_goal_id: Optional[str] = None
  218. # --- 续跑控制 ---
  219. after_sequence: Optional[int] = None # 从哪条消息后续跑(message sequence)
  220. # --- 额外 LLM 参数(传给 llm_call 的 **kwargs)---
  221. extra_llm_params: Dict[str, Any] = field(default_factory=dict)
  222. # --- 自定义元数据上下文 ---
  223. context: Dict[str, Any] = field(default_factory=dict)
  224. # --- 研究流程控制 ---
  225. enable_research_flow: bool = True # 是否启用自动研究流程(知识检索→经验检索→调研→计划)
  226. # --- 知识管理配置 ---
  227. knowledge: KnowledgeConfig = field(default_factory=KnowledgeConfig)
  228. # --- Memory 配置(见 cyber_agent/docs/framework/runtime/memory.md) ---
  229. # None = 默认 Agent(无长期记忆);赋值 MemoryConfig 使该 Agent 成为 memory-bearing Agent
  230. memory: Optional["MemoryConfig"] = None
  231. # --- 一次性恢复动作(不进入持久化 RunConfigSnapshot) ---
  232. approval_batch_id: Optional[str] = None
  233. # --- ApplicationRuntime 固化身份(只由框架装配) ---
  234. application_ref: Any = None
  235. role_id: Optional[str] = None
  236. role_hash: Optional[str] = None
  237. effective_run_limits: Dict[str, Any] = field(default_factory=dict)
  238. def apply_snapshot(
  239. self,
  240. snapshot: RunConfigSnapshotV1 | RunConfigSnapshotV2,
  241. ) -> None:
  242. """Restore all persisted behavior fields while retaining invocation routing."""
  243. self.model = snapshot.model
  244. self.temperature = snapshot.temperature
  245. self.max_iterations = snapshot.max_iterations
  246. self.extra_llm_params = dict(snapshot.extra_llm_params)
  247. self.tools = list(snapshot.tools) if snapshot.tools is not None else None
  248. self.tool_groups = (
  249. list(snapshot.tool_groups) if snapshot.tool_groups is not None else None
  250. )
  251. self.exclude_tools = list(snapshot.exclude_tools)
  252. self.auto_execute_tools = snapshot.auto_execute_tools
  253. self.agent_type = snapshot.agent_type
  254. self.uid = snapshot.uid
  255. self.skills = list(snapshot.skills) if snapshot.skills is not None else None
  256. self.enable_memory = snapshot.enable_memory
  257. self.memory = MemoryConfig(**snapshot.memory) if snapshot.memory else None
  258. self.knowledge = KnowledgeConfig(**snapshot.knowledge)
  259. self.parallel_tool_execution = snapshot.parallel_tool_execution
  260. self.child_execution_mode = snapshot.child_execution_mode
  261. self.max_parallel_children = snapshot.max_parallel_children
  262. self.side_branch_max_turns = snapshot.side_branch_max_turns
  263. self.goal_compression = snapshot.goal_compression
  264. self.enable_prompt_caching = snapshot.enable_prompt_caching
  265. self.enable_research_flow = snapshot.enable_research_flow
  266. self.context = dict(snapshot.custom_context)
  267. if isinstance(snapshot, RunConfigSnapshotV2):
  268. self.application_ref = dict(snapshot.application_ref)
  269. self.role_id = snapshot.role_id
  270. self.role_hash = snapshot.role_hash
  271. self.effective_run_limits = dict(snapshot.effective_run_limits)
  272. # BUILTIN_TOOLS 硬编码列表已移除(2026-04)。
  273. # 工具可用性现在由 @tool(groups=[...]) 声明 + RunConfig.tool_groups 过滤控制。
  274. @dataclass
  275. class CallResult:
  276. """单次调用结果"""
  277. reply: str
  278. tool_calls: Optional[List[Dict]] = None
  279. trace_id: Optional[str] = None
  280. step_id: Optional[str] = None
  281. tokens: Optional[Dict[str, int]] = None
  282. cost: float = 0.0
  283. # ===== 执行引擎 =====
  284. CONTEXT_INJECTION_INTERVAL = 5 # 每 N 轮注入一次 GoalTree + Collaborators + IM 通知
  285. class AgentRunner:
  286. """
  287. Agent 执行引擎
  288. 支持三种运行模式(通过 RunConfig 区分):
  289. 1. 新建:trace_id=None
  290. 2. 续跑:trace_id=已有ID, after_sequence=None 或 == head
  291. 3. 回溯:trace_id=已有ID, after_sequence=N(N < head_sequence)
  292. """
  293. def __init__(
  294. self,
  295. trace_store: Optional[TraceStore] = None,
  296. tool_registry: Optional[ToolRegistry] = None,
  297. llm_call: Optional[Callable] = None,
  298. utility_llm_call: Optional[Callable] = None,
  299. skills_dir: Optional[str] = None,
  300. goal_tree: Optional[GoalTree] = None,
  301. debug: bool = False,
  302. logger_name: Optional[str] = None,
  303. validation_policy: Optional[ValidationPolicy] = None,
  304. validator_search_provider: Optional[ValidatorWebSearchProvider] = None,
  305. validator_page_fetcher: Optional[PageFetcher] = None,
  306. artifact_resolver: Optional[ArtifactResolver] = None,
  307. application_binding: Any = None,
  308. context_provider: Any = None,
  309. candidate_service: Any = None,
  310. event_service: Any = None,
  311. ):
  312. """
  313. 初始化 AgentRunner
  314. Args:
  315. trace_store: Trace 存储
  316. tool_registry: 工具注册表(默认使用全局注册表)
  317. llm_call: 主 LLM 调用函数
  318. utility_llm_call: 轻量 LLM(用于生成任务标题等),可选
  319. skills_dir: Skills 目录路径
  320. goal_tree: 初始 GoalTree(可选)
  321. debug: 保留参数(已废弃)
  322. logger_name: 自定义日志名称(如 "agents.knowledge_manager"),默认用模块名
  323. validation_policy: Recursive 根 Trace 固化的可信验收策略
  324. validator_search_provider: Validator 私有网页搜索适配器
  325. validator_page_fetcher: Validator 受控页面读取适配器
  326. artifact_resolver: Validator 只读产物解析器
  327. """
  328. self.trace_store = trace_store
  329. self.tools = tool_registry or get_tool_registry()
  330. self.llm_call = llm_call
  331. self.utility_llm_call = utility_llm_call
  332. self.skills_dir = skills_dir
  333. self.goal_tree = goal_tree
  334. self.debug = debug
  335. self.log = logging.getLogger(logger_name) if logger_name else logger
  336. self.validation_policy = validation_policy or ValidationPolicy()
  337. self.validator_search_provider = validator_search_provider
  338. self.validator_page_fetcher = validator_page_fetcher
  339. self.artifact_resolver = artifact_resolver
  340. self.application_binding = application_binding
  341. self.context_provider = context_provider
  342. self.candidate_service = candidate_service
  343. self.event_service = event_service
  344. self.stdin_check: Optional[Callable] = None # 由外部设置,用于子 agent 执行期间检查 stdin
  345. self._cancel_events: Dict[str, asyncio.Event] = {} # trace_id → cancel event
  346. self._recursive_active_traces: Dict[str, asyncio.Event] = {}
  347. self._active_children: Dict[str, set[str]] = {}
  348. self._active_parents: Dict[str, str] = {}
  349. # 知识保存跟踪(每个 trace 独立)
  350. self._saved_knowledge_ids: Dict[str, List[str]] = {} # trace_id → [knowledge_ids]
  351. # Context 使用跟踪
  352. self._context_warned: Dict[str, set] = {} # trace_id → {30, 50, 80} 已警告过的阈值
  353. self._context_usage: Dict[str, ContextUsage] = {} # trace_id → 当前用量快照
  354. # 图片优化缓存(避免重复处理)
  355. # key: 图片内容的 hash, value: {"downscaled": ..., "description": ...}
  356. self._image_opt_cache: Dict[str, Dict[str, Any]] = {}
  357. self.resource_budget = (
  358. ResourceBudgetController(trace_store) if trace_store else None
  359. )
  360. self.task_protocol_service = (
  361. TaskProtocolService(trace_store, event_service) if trace_store else None
  362. )
  363. # ===== 核心公开方法 =====
  364. def get_context_usage(self, trace_id: str) -> Optional[ContextUsage]:
  365. """获取指定 trace 的 context 使用情况"""
  366. return self._context_usage.get(trace_id)
  367. async def _resource_budget_for_trace(
  368. self,
  369. trace_id: str,
  370. ) -> tuple[str, ResourceBudget] | None:
  371. """解析本地 Trace 所属 Recursive 根树的不可变预算快照。
  372. 由模型调用、工具用量记账和子 Agent 创建入口调用;Legacy Trace 直接返回无预算。
  373. """
  374. if not self.trace_store:
  375. return None
  376. trace = await self.trace_store.get_trace(trace_id)
  377. if not trace or not policy_from_context(trace.context).requires_task_protocol:
  378. return None
  379. root_trace_id = trace.context.get("root_trace_id") or trace.trace_id
  380. root = trace if root_trace_id == trace.trace_id else await self.trace_store.get_trace(root_trace_id)
  381. if not root:
  382. raise ResourceBudgetStateError(
  383. f"Recursive root Trace not found: {root_trace_id}"
  384. )
  385. snapshot = root.context.get(RESOURCE_BUDGET_CONTEXT_KEY)
  386. if snapshot is None:
  387. raise ResourceBudgetStateError(
  388. "Recursive tree has no persisted resource budget; create a new trace"
  389. )
  390. return root_trace_id, ResourceBudget.from_dict(snapshot)
  391. async def call_recursive_llm(
  392. self,
  393. trace_id: str,
  394. *,
  395. purpose: Literal["ordinary", "root_validator"] = "ordinary",
  396. call: Optional[Callable] = None,
  397. fail_on_post_response_exhaustion: bool = False,
  398. **kwargs: Any,
  399. ) -> Dict[str, Any]:
  400. """Recursive 树中统一的 LLM 调用和资源记账入口。
  401. Agent 主循环、上下文压缩、图片描述和 Validator 共用;请求前预留次数,响应后记账。
  402. """
  403. llm = call or self.llm_call
  404. if not llm:
  405. raise ValueError("llm_call function not provided")
  406. resolved = await self._resource_budget_for_trace(trace_id)
  407. if resolved is None:
  408. return await llm(**kwargs)
  409. root_trace_id, budget = resolved
  410. if not self.resource_budget:
  411. raise ResourceBudgetStateError("ResourceBudgetController is unavailable")
  412. await self.resource_budget.reserve_llm_call(
  413. root_trace_id,
  414. budget,
  415. purpose=purpose,
  416. )
  417. result = await llm(**kwargs)
  418. try:
  419. await self.resource_budget.record_llm_usage(
  420. root_trace_id,
  421. budget,
  422. prompt_tokens=int(result.get("prompt_tokens", 0) or 0),
  423. completion_tokens=int(result.get("completion_tokens", 0) or 0),
  424. cost_usd=float(result.get("cost", 0) or 0),
  425. )
  426. except ResourceBudgetExceeded as exc:
  427. if fail_on_post_response_exhaustion:
  428. raise
  429. result = dict(result)
  430. result["_resource_budget_exceeded"] = exc.dimension
  431. return result
  432. async def record_recursive_tool_usage(
  433. self,
  434. trace_id: str,
  435. tool_usage: Dict[str, Any],
  436. ) -> None:
  437. """登记工具内部自行发起的模型用量。
  438. Agent 主循环在 Tool Result 携带 ``tool_usage`` 时调用,并计入同一棵 Recursive 树。
  439. """
  440. resolved = await self._resource_budget_for_trace(trace_id)
  441. if resolved is None:
  442. return
  443. root_trace_id, budget = resolved
  444. if not self.resource_budget:
  445. raise ResourceBudgetStateError("ResourceBudgetController is unavailable")
  446. await self.resource_budget.record_external_llm_usage(
  447. root_trace_id,
  448. budget,
  449. prompt_tokens=int(tool_usage.get("prompt_tokens", 0) or 0),
  450. completion_tokens=int(tool_usage.get("completion_tokens", 0) or 0),
  451. cost_usd=float(tool_usage.get("cost", 0) or 0),
  452. )
  453. async def record_recursive_validation_usage(
  454. self,
  455. trace_id: str,
  456. *,
  457. tool_calls: int = 0,
  458. material_chars_count: int = 0,
  459. provider_cost_usd: float = 0.0,
  460. operation_id: str | None = None,
  461. ) -> None:
  462. """把 Validator网页工具和真实材料字符计入同一棵树的预算。"""
  463. resolved = await self._resource_budget_for_trace(trace_id)
  464. if resolved is None:
  465. return
  466. root_trace_id, budget = resolved
  467. if not self.resource_budget:
  468. raise ResourceBudgetStateError("ResourceBudgetController is unavailable")
  469. await self.resource_budget.record_validation_usage(
  470. root_trace_id,
  471. budget,
  472. tool_calls=tool_calls,
  473. material_chars=material_chars_count,
  474. provider_cost_usd=provider_cost_usd,
  475. operation_id=operation_id,
  476. )
  477. async def validate_recursive_trace(
  478. self,
  479. evaluated_trace_id: str,
  480. *,
  481. scope: Optional[ValidationScope] = None,
  482. task_brief: Optional[Dict[str, Any]] = None,
  483. task_report: Optional[Dict[str, Any]] = None,
  484. completion_criteria: Optional[List[str]] = None,
  485. expected_outputs: Optional[List[str]] = None,
  486. candidate_output: Optional[str] = None,
  487. deterministic_failure: Optional[Dict[str, Any]] = None,
  488. root_validator: bool = False,
  489. candidate_ref: Any = None,
  490. ) -> ValidationRun:
  491. """编译Plan、解析材料、顺序运行Scope并持久化聚合缓存。"""
  492. if not self.trace_store or not self.llm_call:
  493. raise RuntimeError("Validator requires trace_store and llm_call")
  494. evaluated = await self.trace_store.get_trace(evaluated_trace_id)
  495. if not evaluated:
  496. raise ValueError(f"Trace not found: {evaluated_trace_id}")
  497. root_trace_id = evaluated.context.get("root_trace_id") or evaluated.trace_id
  498. root = (
  499. evaluated
  500. if root_trace_id == evaluated.trace_id
  501. else await self.trace_store.get_trace(root_trace_id)
  502. )
  503. if not root:
  504. raise ContextPolicyError(f"Recursive root Trace not found: {root_trace_id}")
  505. root_anchor = require_matching_root_task_anchor(
  506. root.context,
  507. evaluated.context,
  508. )
  509. policy, settings = require_validation_policy(root.context)
  510. state = ensure_task_protocol(evaluated.context)
  511. if candidate_ref is not None:
  512. from cyber_agent.application.candidate import CandidateRef
  513. candidate_ref = CandidateRef.model_validate(candidate_ref)
  514. if self.candidate_service is None:
  515. raise ValueError("Candidate validation requires CandidateService")
  516. if root_validator:
  517. raise ValueError("Candidate validation cannot be root validation")
  518. runtime_policy = policy_from_context(evaluated.context)
  519. authoritative_brief = state.get("task_brief")
  520. if authoritative_brief is not None:
  521. task_brief = authoritative_brief
  522. task_brief_version = int(state.get("task_brief_version", 0) or 0)
  523. progress_revision = (
  524. state.get("task_progress_head_revision")
  525. if root_validator or task_report is None
  526. else state.get("task_report_progress_revision")
  527. )
  528. task_progress = task_progress_at_revision(state, progress_revision)
  529. if runtime_policy.requires_task_progress and task_progress is None:
  530. raise ValueError("Recursive revision 3 validation requires TaskProgress")
  531. trajectory = await self.trace_store.get_main_path_messages(
  532. evaluated_trace_id,
  533. evaluated.head_sequence or evaluated.last_sequence,
  534. )
  535. refs: list[ArtifactRef] = []
  536. materials: list[ValidationMaterial] = []
  537. source_urls: list[str] = []
  538. material_issues: list[MaterialIssue] = []
  539. try:
  540. if isinstance(task_report, dict):
  541. refs.extend(extract_artifact_refs(task_report))
  542. source_urls = list(task_report.get("source_urls") or [])
  543. refs.extend(task_progress_artifact_refs(task_progress))
  544. if task_progress is not None:
  545. for item in (
  546. *task_progress.questions,
  547. *task_progress.blockers,
  548. *task_progress.findings,
  549. *task_progress.hypotheses,
  550. *task_progress.work_items,
  551. ):
  552. for ref in item.context_refs:
  553. snapshot = get_authorized_context_snapshot(
  554. evaluated.context,
  555. ref_id=ref.ref_id,
  556. version=ref.version,
  557. root_trace_id=root_trace_id,
  558. uid=evaluated.uid,
  559. )
  560. materials.append(ValidationMaterial(
  561. artifact_id=ref.ref_id,
  562. version=snapshot.version,
  563. content_hash=snapshot.version,
  564. kind=f"context.{snapshot.kind}",
  565. mime_type="application/json",
  566. root_trace_id=root_trace_id,
  567. uid=evaluated.uid,
  568. content=snapshot.content,
  569. ))
  570. if root_validator:
  571. for message in trajectory:
  572. if message.role == "tool" and isinstance(message.content, dict):
  573. refs.extend(extract_artifact_refs(message.content))
  574. except Exception as exc:
  575. material_issues.append(MaterialIssue(
  576. artifact_id="artifact_metadata",
  577. outcome="error",
  578. reason=f"Invalid artifact metadata: {exc}",
  579. ))
  580. unique_refs = {
  581. (item.artifact_id, item.version, item.content_hash): item for item in refs
  582. }
  583. resolved_materials, resolved_issues = await resolve_artifact_refs(
  584. list(unique_refs.values()),
  585. resolver=self.artifact_resolver,
  586. root_trace_id=root_trace_id,
  587. uid=evaluated.uid,
  588. )
  589. materials.extend(resolved_materials)
  590. material_issues.extend(resolved_issues)
  591. candidate_material = None
  592. if candidate_ref is not None:
  593. try:
  594. candidate_material = await self.candidate_service.resolve_for_validation(
  595. evaluated_trace_id,
  596. candidate_ref,
  597. )
  598. materials = [
  599. item for item in materials
  600. if not (
  601. item.artifact_id == candidate_material.artifact_id
  602. and item.version == candidate_material.version
  603. )
  604. ]
  605. materials.append(candidate_material)
  606. candidate_output = json.dumps(
  607. candidate_material.content,
  608. ensure_ascii=False,
  609. sort_keys=True,
  610. separators=(",", ":"),
  611. )
  612. except Exception as exc:
  613. material_issues.append(MaterialIssue(
  614. artifact_id=candidate_ref.artifact_ref.artifact_id,
  615. outcome="error",
  616. reason=f"Candidate material cannot be resolved: {exc}",
  617. scopes=["output"],
  618. ))
  619. total_material_chars = sum(material_chars(item) for item in materials)
  620. if deterministic_failure:
  621. material_issues.append(MaterialIssue(
  622. artifact_id="execution",
  623. outcome=deterministic_failure.get("outcome", "error"),
  624. reason=deterministic_failure.get("reason", "Task did not complete"),
  625. ))
  626. default_model = settings.validator_model or evaluated.model or ""
  627. root_model = settings.root_validator_model or default_model
  628. model_by_scope = {
  629. item: (root_model if item == "root" else default_model)
  630. for item in ("evidence", "hypothesis", "output", "task", "root")
  631. }
  632. if scope == "root" or root_validator:
  633. root_validator = True
  634. elif scope and task_brief is None:
  635. task_brief = {
  636. "completion_criteria": completion_criteria or [],
  637. "expected_outputs": expected_outputs or [],
  638. "validation_scopes": [] if scope == "task" else [scope],
  639. }
  640. validation_subject = None
  641. quality_specs: list[ValidationCheckSpec] = []
  642. quality_manifest: list[dict[str, Any]] = []
  643. fixed_checks: dict[str, ValidationCheck] = {}
  644. fixed_scope_errors: dict[ValidationScope, str] = {}
  645. quality_rules: tuple[Any, ...] = ()
  646. quality_materials: tuple[ValidationMaterial, ...] = ()
  647. if self.application_binding is not None:
  648. from cyber_agent.application.quality import ValidationSubject
  649. brief_payload = (
  650. task_brief.model_dump(mode="json")
  651. if hasattr(task_brief, "model_dump")
  652. else (task_brief or {})
  653. )
  654. requested_quality_scopes = (
  655. {"output"}
  656. if candidate_ref is not None
  657. else set(policy.effective_scopes(
  658. brief_payload.get("validation_scopes", []),
  659. root=root_validator,
  660. ))
  661. )
  662. validation_subject = ValidationSubject(
  663. subject_type=("candidate" if candidate_ref is not None else "trace"),
  664. trace_id=evaluated_trace_id,
  665. candidate_ref=candidate_ref,
  666. )
  667. quality_rules = tuple(
  668. rule
  669. for rule in self.application_binding.application.quality_rules
  670. if validation_subject.subject_type in rule.subject_types
  671. and rule.scope in requested_quality_scopes
  672. )
  673. if candidate_ref is not None and candidate_material is not None:
  674. quality_materials = (candidate_material,)
  675. elif candidate_ref is None:
  676. quality_materials = tuple(materials)
  677. if isinstance(task_report, dict):
  678. report_material = ValidationMaterial(
  679. artifact_id=f"trace:{evaluated_trace_id}:task_report",
  680. version=str(progress_revision or 0),
  681. content_hash=material_content_hash(task_report),
  682. kind="framework.task_report",
  683. mime_type="application/json",
  684. root_trace_id=root_trace_id,
  685. uid=evaluated.uid,
  686. content=task_report,
  687. )
  688. quality_materials = (*quality_materials, report_material)
  689. total_material_chars += material_chars(report_material)
  690. quality_specs = [
  691. ValidationCheckSpec(
  692. check_id=f"quality.{rule.rule_id}",
  693. scope=rule.scope,
  694. criterion=rule.criterion,
  695. method="deterministic",
  696. )
  697. for rule in quality_rules
  698. ]
  699. quality_manifest = [
  700. item.model_dump(mode="json") for item in quality_rules
  701. ]
  702. plan = policy.compile_plan(
  703. task_brief=task_brief,
  704. task_brief_version=task_brief_version,
  705. root_task_anchor=root_anchor,
  706. task_report=task_report,
  707. candidate_output=candidate_output,
  708. evaluated_head_sequence=evaluated.head_sequence or evaluated.last_sequence,
  709. materials=materials,
  710. material_issues=material_issues,
  711. model_by_scope=model_by_scope,
  712. root=root_validator,
  713. task_progress=task_progress,
  714. subject=validation_subject,
  715. quality_checks=quality_specs,
  716. quality_manifest=quality_manifest,
  717. )
  718. cached = state.get("task_report_validation")
  719. if candidate_ref is not None:
  720. candidate_cached = await self.candidate_service.cached_validation(
  721. evaluated_trace_id,
  722. candidate_ref,
  723. plan.plan_hash,
  724. )
  725. if candidate_cached is not None:
  726. aggregate = ValidationResult.model_validate(
  727. candidate_cached.validation_result
  728. )
  729. return ValidationRun(
  730. result=aggregate,
  731. trace_ids=[
  732. item.validator_trace_id
  733. for item in aggregate.scope_results
  734. ],
  735. cached=True,
  736. )
  737. cached = None
  738. validation_cache: dict[str, Any]
  739. resume_scope_results: list[ScopeValidationResult] = []
  740. if isinstance(cached, dict) and cached.get("plan_hash") == plan.plan_hash:
  741. validation_cache = cached
  742. try:
  743. aggregate_raw = cached.get("aggregate_result")
  744. if aggregate_raw:
  745. aggregate = ValidationResult.model_validate(aggregate_raw)
  746. if (
  747. aggregate.evaluated_trace_id == evaluated_trace_id
  748. and aggregate.plan_hash == plan.plan_hash
  749. ):
  750. return ValidationRun(
  751. result=aggregate,
  752. trace_ids=[
  753. item.validator_trace_id
  754. for item in aggregate.scope_results
  755. ],
  756. cached=True,
  757. )
  758. resume_scope_results = [
  759. ScopeValidationResult.model_validate(item)
  760. for item in cached.get("scope_results", [])
  761. ]
  762. except Exception:
  763. resume_scope_results = []
  764. else:
  765. validation_cache = {
  766. "validation_plan": plan.model_dump(mode="json"),
  767. "plan_hash": plan.plan_hash,
  768. "scope_results": [],
  769. "aggregate_result": None,
  770. "validated_at_sequence": plan.evaluated_head_sequence,
  771. "material_usage_recorded": total_material_chars == 0,
  772. }
  773. if candidate_ref is None:
  774. assert self.task_protocol_service is not None
  775. await self.task_protocol_service.mutate_state(
  776. evaluated_trace_id,
  777. lambda _trace, fresh_state: fresh_state.__setitem__(
  778. "task_report_validation",
  779. deepcopy(validation_cache),
  780. ),
  781. )
  782. candidate_checkpoint = None
  783. if candidate_ref is not None:
  784. candidate_checkpoint = await self.candidate_service.begin_validation_checkpoint(
  785. evaluated_trace_id,
  786. candidate_ref,
  787. plan=plan.model_dump(mode="json"),
  788. plan_hash=plan.plan_hash,
  789. validated_at_sequence=plan.evaluated_head_sequence,
  790. material_usage_recorded=total_material_chars == 0,
  791. )
  792. validation_cache = {
  793. "validation_plan": candidate_checkpoint.validation_plan,
  794. "plan_hash": candidate_checkpoint.plan_hash,
  795. "scope_results": list(candidate_checkpoint.scope_results),
  796. "aggregate_result": candidate_checkpoint.aggregate_result,
  797. "validated_at_sequence": candidate_checkpoint.validated_at_sequence,
  798. "material_usage_recorded": (
  799. candidate_checkpoint.material_usage_recorded
  800. ),
  801. }
  802. resume_scope_results = [
  803. ScopeValidationResult.model_validate(item)
  804. for item in candidate_checkpoint.scope_results
  805. ]
  806. if quality_rules:
  807. if candidate_checkpoint.quality_completed:
  808. fixed_checks = {
  809. item["check_id"]: ValidationCheck.model_validate(item)
  810. for item in candidate_checkpoint.fixed_checks
  811. }
  812. fixed_scope_errors = {
  813. key: value
  814. for key, value in candidate_checkpoint.fixed_scope_errors.items()
  815. }
  816. else:
  817. fixed_checks, fixed_scope_errors = await self._run_quality_checks(
  818. validation_subject,
  819. quality_materials,
  820. quality_rules,
  821. usage_operation_prefix=(
  822. f"candidate-validation:{candidate_ref.candidate_id}:"
  823. f"{candidate_ref.revision}:{plan.plan_hash}"
  824. ),
  825. )
  826. candidate_checkpoint = (
  827. await self.candidate_service.update_validation_checkpoint(
  828. evaluated_trace_id,
  829. candidate_ref,
  830. plan.plan_hash,
  831. fixed_checks=tuple(
  832. item.model_dump(mode="json")
  833. for item in fixed_checks.values()
  834. ),
  835. fixed_scope_errors={
  836. key: value
  837. for key, value in fixed_scope_errors.items()
  838. },
  839. quality_completed=True,
  840. )
  841. )
  842. if candidate_ref is None and quality_rules:
  843. fixed_checks, fixed_scope_errors = await self._run_quality_checks(
  844. validation_subject,
  845. quality_materials,
  846. quality_rules,
  847. usage_operation_prefix=(
  848. f"trace-validation:{evaluated_trace_id}:{plan.plan_hash}"
  849. ),
  850. )
  851. if (
  852. total_material_chars
  853. and not validation_cache.get("material_usage_recorded", False)
  854. ):
  855. await self.record_recursive_validation_usage(
  856. evaluated_trace_id,
  857. material_chars_count=total_material_chars,
  858. operation_id=(
  859. f"validation-materials:{evaluated_trace_id}:{plan.plan_hash}"
  860. ),
  861. )
  862. if candidate_ref is not None:
  863. validation_cache["material_usage_recorded"] = True
  864. candidate_checkpoint = (
  865. await self.candidate_service.update_validation_checkpoint(
  866. evaluated_trace_id,
  867. candidate_ref,
  868. plan.plan_hash,
  869. material_usage_recorded=True,
  870. )
  871. )
  872. else:
  873. assert self.task_protocol_service is not None
  874. def mark_material_usage(_trace, fresh_state):
  875. fresh_cache = fresh_state.get("task_report_validation")
  876. if (
  877. not isinstance(fresh_cache, dict)
  878. or fresh_cache.get("plan_hash") != plan.plan_hash
  879. ):
  880. raise ValueError(
  881. "Validation cache changed while recording materials"
  882. )
  883. fresh_cache["material_usage_recorded"] = True
  884. await self.task_protocol_service.mutate_state(
  885. evaluated_trace_id,
  886. mark_material_usage,
  887. )
  888. lineage_event = None
  889. if (
  890. evaluated.parent_trace_id
  891. and evaluated_trace_id not in self._recursive_active_traces
  892. ):
  893. lineage_event = self.register_recursive_child(
  894. evaluated.parent_trace_id,
  895. evaluated_trace_id,
  896. )
  897. async def validator_llm_call(**kwargs: Any) -> Dict[str, Any]:
  898. result = await self.call_recursive_llm(
  899. evaluated_trace_id,
  900. purpose="root_validator" if root_validator else "ordinary",
  901. **kwargs,
  902. )
  903. dimension = result.get("_resource_budget_exceeded")
  904. if dimension:
  905. raise RuntimeError(
  906. f"Validator exceeded tree resource budget: {dimension}"
  907. )
  908. return result
  909. provider: ValidatorWebSearchProvider | None
  910. if settings.search_provider == "disabled":
  911. provider = None
  912. else:
  913. provider = self.validator_search_provider or SerperWebSearchProvider()
  914. def tool_session_factory(
  915. validation_scope: ValidationScope,
  916. allowed_urls: set[str],
  917. validator_trace_id: str,
  918. ) -> ValidatorToolSession | None:
  919. limits_by_scope = {
  920. "evidence": ValidatorToolLimits(5, 10, 15),
  921. "hypothesis": ValidatorToolLimits(2, 4, 6),
  922. "root": ValidatorToolLimits(2, 5, 7),
  923. }
  924. limits = limits_by_scope.get(validation_scope)
  925. if limits is None:
  926. return None
  927. async def record_usage(
  928. tool_calls: int,
  929. chars: int,
  930. provider_cost_usd: float,
  931. ) -> None:
  932. await self.record_recursive_validation_usage(
  933. evaluated_trace_id,
  934. tool_calls=tool_calls,
  935. material_chars_count=chars,
  936. provider_cost_usd=provider_cost_usd,
  937. )
  938. session_kwargs: Dict[str, Any] = {}
  939. if self.validator_page_fetcher is not None:
  940. session_kwargs["page_fetcher"] = self.validator_page_fetcher
  941. return ValidatorToolSession(
  942. provider=provider,
  943. allowed_urls=allowed_urls,
  944. limits=limits,
  945. usage_recorder=record_usage,
  946. **session_kwargs,
  947. )
  948. validator = LLMValidator(
  949. llm_call=validator_llm_call,
  950. trace_store=self.trace_store,
  951. policy=policy,
  952. tool_session_factory=tool_session_factory,
  953. cancel_check=self.is_cancel_requested,
  954. trace_register=self.register_recursive_child,
  955. trace_release=self.release_recursive_trace,
  956. )
  957. try:
  958. async def persist_scope(result: ScopeValidationResult) -> None:
  959. if candidate_ref is not None:
  960. by_scope = {
  961. item.get("scope"): item
  962. for item in validation_cache.get("scope_results", [])
  963. if isinstance(item, dict)
  964. }
  965. by_scope[result.scope] = result.model_dump(mode="json")
  966. validation_cache["scope_results"] = [
  967. by_scope[item]
  968. for item in plan.effective_scopes
  969. if item in by_scope
  970. ]
  971. await self.candidate_service.update_validation_checkpoint(
  972. evaluated_trace_id,
  973. candidate_ref,
  974. plan.plan_hash,
  975. scope_results=tuple(validation_cache["scope_results"]),
  976. )
  977. return
  978. assert self.task_protocol_service is not None
  979. def persist_trace_scope(_trace, fresh_state):
  980. cache = fresh_state.get("task_report_validation")
  981. if (
  982. not isinstance(cache, dict)
  983. or cache.get("plan_hash") != plan.plan_hash
  984. ):
  985. raise ValueError(
  986. "Validation cache changed while scopes were running"
  987. )
  988. by_scope = {
  989. item.get("scope"): item
  990. for item in cache.get("scope_results", [])
  991. if isinstance(item, dict)
  992. }
  993. by_scope[result.scope] = result.model_dump(mode="json")
  994. cache["scope_results"] = [
  995. by_scope[item]
  996. for item in plan.effective_scopes
  997. if item in by_scope
  998. ]
  999. await self.task_protocol_service.mutate_state(
  1000. evaluated_trace_id,
  1001. persist_trace_scope,
  1002. )
  1003. run = await validator.validate_plan(
  1004. evaluated_trace=evaluated,
  1005. trajectory=trajectory,
  1006. plan=plan,
  1007. root_task_anchor=root_anchor,
  1008. task_brief=task_brief,
  1009. task_report=task_report,
  1010. task_progress=task_progress,
  1011. candidate_output=candidate_output,
  1012. materials=materials,
  1013. material_issues=material_issues,
  1014. model_by_scope=model_by_scope,
  1015. source_urls=source_urls,
  1016. resume_scope_results=resume_scope_results,
  1017. on_scope_result=persist_scope,
  1018. fixed_checks=fixed_checks,
  1019. fixed_scope_errors=fixed_scope_errors,
  1020. )
  1021. if candidate_ref is not None:
  1022. from cyber_agent.application.quality import CandidateValidationRecord
  1023. await self.candidate_service.record_validation(
  1024. evaluated_trace_id,
  1025. CandidateValidationRecord(
  1026. candidate_ref=candidate_ref,
  1027. plan_hash=plan.plan_hash,
  1028. validation_result=run.result.model_dump(mode="json"),
  1029. validated_at_sequence=plan.evaluated_head_sequence,
  1030. ),
  1031. )
  1032. return run
  1033. assert self.task_protocol_service is not None
  1034. def persist_aggregate(_trace, fresh_state):
  1035. cache = fresh_state.get("task_report_validation")
  1036. if (
  1037. not isinstance(cache, dict)
  1038. or cache.get("plan_hash") != plan.plan_hash
  1039. ):
  1040. raise ValueError("Validation cache changed before aggregation")
  1041. cache["aggregate_result"] = run.result.model_dump(mode="json")
  1042. cache["scope_results"] = [
  1043. item.model_dump(mode="json")
  1044. for item in run.result.scope_results
  1045. ]
  1046. await self.task_protocol_service.mutate_state(
  1047. evaluated_trace_id,
  1048. persist_aggregate,
  1049. )
  1050. if self.event_service is not None:
  1051. await self.event_service.emit_after_commit(
  1052. source_trace_id=evaluated_trace_id,
  1053. event_type="validation.completed",
  1054. event_key=(
  1055. f"validation.completed:trace:{evaluated_trace_id}:"
  1056. f"{plan.plan_hash}"
  1057. ),
  1058. effective_at_sequence=plan.evaluated_head_sequence,
  1059. payload={
  1060. "subject_type": "trace",
  1061. "validation_result": run.result.model_dump(mode="json"),
  1062. },
  1063. )
  1064. return run
  1065. finally:
  1066. if lineage_event is not None:
  1067. self.release_recursive_trace(evaluated_trace_id, lineage_event)
  1068. async def _run_quality_checks(
  1069. self,
  1070. subject: Any,
  1071. materials: tuple[ValidationMaterial, ...],
  1072. rules: tuple[Any, ...],
  1073. usage_operation_prefix: str,
  1074. ) -> tuple[dict[str, ValidationCheck], dict[ValidationScope, str]]:
  1075. checks: dict[str, ValidationCheck] = {}
  1076. errors: dict[ValidationScope, str] = {}
  1077. for scope in dict.fromkeys(rule.scope for rule in rules):
  1078. scoped_rules = tuple(rule for rule in rules if rule.scope == scope)
  1079. scoped_checks, scoped_errors = await self._run_quality_check_batch(
  1080. subject,
  1081. materials,
  1082. scoped_rules,
  1083. usage_operation_id=f"{usage_operation_prefix}:quality:{scope}",
  1084. )
  1085. checks.update(scoped_checks)
  1086. errors.update(scoped_errors)
  1087. return checks, errors
  1088. async def _run_quality_check_batch(
  1089. self,
  1090. subject: Any,
  1091. materials: tuple[ValidationMaterial, ...],
  1092. rules: tuple[Any, ...],
  1093. usage_operation_id: str,
  1094. ) -> tuple[dict[str, ValidationCheck], dict[ValidationScope, str]]:
  1095. """Execute one frozen quality batch and reject provider-shaped plans."""
  1096. from cyber_agent.application.quality import (
  1097. QualityCheckInput,
  1098. QualityCheckOutcome,
  1099. )
  1100. provider = self.application_binding.services.quality_provider
  1101. check_ids = {rule.rule_id: f"quality.{rule.rule_id}" for rule in rules}
  1102. def provider_error(reason: str):
  1103. return (
  1104. {
  1105. check_id: ValidationCheck(
  1106. check_id=check_id,
  1107. status="unknown",
  1108. issue=reason,
  1109. )
  1110. for check_id in check_ids.values()
  1111. },
  1112. {rule.scope: reason for rule in rules},
  1113. )
  1114. if provider is None:
  1115. return provider_error("QualityCheckProvider is unavailable")
  1116. if not materials:
  1117. return provider_error("QualityCheckProvider has no authorized material")
  1118. request = QualityCheckInput(
  1119. subject=subject,
  1120. rules=rules,
  1121. materials=materials,
  1122. )
  1123. try:
  1124. raw = await asyncio.wait_for(
  1125. provider.check(request),
  1126. timeout=max(rule.timeout_seconds for rule in rules),
  1127. )
  1128. outcomes = [QualityCheckOutcome.model_validate(item) for item in raw]
  1129. except Exception as exc:
  1130. return provider_error(f"QualityCheckProvider failed: {exc}")
  1131. returned = [(item.rule_id, item.rule_version) for item in outcomes]
  1132. expected = [(item.rule_id, item.version) for item in rules]
  1133. if len(returned) != len(set(returned)) or set(returned) != set(expected):
  1134. extras = sorted(set(returned) - set(expected))
  1135. missing = sorted(set(expected) - set(returned))
  1136. return provider_error(
  1137. "QualityCheckProvider results do not match the frozen rules: "
  1138. f"extras={extras}, missing={missing}"
  1139. )
  1140. by_rule = {item.rule_id: item for item in outcomes}
  1141. checks: dict[str, ValidationCheck] = {}
  1142. errors: dict[ValidationScope, str] = {}
  1143. for rule in rules:
  1144. outcome = by_rule[rule.rule_id]
  1145. check_id = check_ids[rule.rule_id]
  1146. checks[check_id] = ValidationCheck(
  1147. check_id=check_id,
  1148. status=("unknown" if outcome.status == "error" else outcome.status),
  1149. evidence_refs=list(outcome.evidence_refs),
  1150. issue=outcome.issue,
  1151. )
  1152. if outcome.status == "error":
  1153. errors[rule.scope] = outcome.issue or "Quality check failed"
  1154. try:
  1155. await self.record_recursive_validation_usage(
  1156. subject.trace_id,
  1157. tool_calls=len(rules),
  1158. operation_id=usage_operation_id,
  1159. )
  1160. except Exception as exc:
  1161. return provider_error(f"Quality validation budget failed: {exc}")
  1162. return checks, errors
  1163. async def dream(
  1164. self,
  1165. memory_config: MemoryConfig,
  1166. *,
  1167. uid: Optional[str],
  1168. agent_type: str,
  1169. reflect_model: str = "gpt-4o-mini",
  1170. dream_model: str = "gpt-4o",
  1171. ) -> "DreamReport":
  1172. """执行 dream(整理长期记忆)——外部调度入口。
  1173. Agent 主动调用走 dream 工具;外部调度(定时器、CLI)走这个方法。
  1174. Args:
  1175. memory_config: 记忆配置
  1176. uid/agent_type: 与 MemoryConfig 一起形成强制 Dream 身份边界
  1177. reflect_model: per-trace 反思模型
  1178. dream_model: 跨 trace 整合模型
  1179. """
  1180. from cyber_agent.core.dream import DreamScope, run_dream
  1181. if not self.trace_store or not self.llm_call:
  1182. raise RuntimeError("dream 需要 trace_store 和 llm_call 均已配置")
  1183. return await run_dream(
  1184. store=self.trace_store,
  1185. llm_call=self.llm_call,
  1186. memory_config=memory_config,
  1187. dream_scope=DreamScope(
  1188. uid=uid,
  1189. agent_type=agent_type,
  1190. memory_identity=compute_memory_identity(memory_config),
  1191. ),
  1192. reflect_model=reflect_model,
  1193. dream_model=dream_model,
  1194. )
  1195. async def run(
  1196. self,
  1197. messages: List[Dict],
  1198. config: Optional[RunConfig] = None,
  1199. inject_skills: Optional[List[str]] = None,
  1200. skill_recency_threshold: int = 10,
  1201. ) -> AsyncIterator[Union[Trace, Message]]:
  1202. """
  1203. Agent 模式执行(核心方法)
  1204. Args:
  1205. messages: OpenAI SDK 格式的输入消息
  1206. 新建: 初始任务消息 [{"role": "user", "content": "..."}]
  1207. 续跑: 追加的新消息
  1208. 回溯: 在插入点之后追加的消息
  1209. config: 运行配置
  1210. inject_skills: 本次调用需要指定注入的 skill 列表(skill 名称)
  1211. skill_recency_threshold: 最近 N 条消息内有该 skill 就不重复注入
  1212. Yields:
  1213. Union[Trace, Message]: Trace 对象(状态变化)或 Message 对象(执行过程)
  1214. """
  1215. if not self.llm_call:
  1216. raise ValueError("llm_call function not provided")
  1217. config = config or RunConfig()
  1218. trace = None
  1219. run_cancel_event: Optional[asyncio.Event] = None
  1220. try:
  1221. # Phase 1: PREPARE TRACE
  1222. trace, goal_tree, sequence = await self._prepare_trace(messages, config)
  1223. # 子 Trace 可能已在排队阶段收到停止信号,不能覆盖既有 Event。
  1224. run_cancel_event = self._cancel_events.setdefault(
  1225. trace.trace_id,
  1226. asyncio.Event(),
  1227. )
  1228. if policy_from_context(trace.context).mode is AgentMode.RECURSIVE:
  1229. self._recursive_active_traces[trace.trace_id] = run_cancel_event
  1230. if trace.parent_trace_id:
  1231. self._active_children.setdefault(
  1232. trace.parent_trace_id,
  1233. set(),
  1234. ).add(trace.trace_id)
  1235. self._active_parents[trace.trace_id] = trace.parent_trace_id
  1236. yield trace
  1237. # 检查是否有未完成的侧分支(用于用户追加消息场景)
  1238. side_branch_ctx_for_build: Optional[SideBranchContext] = None
  1239. if trace.context.get("active_side_branch") and messages:
  1240. side_branch_data = trace.context["active_side_branch"]
  1241. # 创建侧分支上下文(用于标记用户追加的消息)
  1242. side_branch_ctx_for_build = SideBranchContext(
  1243. type=side_branch_data["type"],
  1244. branch_id=side_branch_data["branch_id"],
  1245. start_head_seq=side_branch_data["start_head_seq"],
  1246. start_sequence=side_branch_data["start_sequence"],
  1247. start_history_length=0,
  1248. start_iteration=side_branch_data.get("start_iteration", 0),
  1249. max_turns=side_branch_data.get("max_turns", config.side_branch_max_turns),
  1250. turns_used=side_branch_data.get("turns_used", 0),
  1251. )
  1252. # Phase 2: BUILD HISTORY
  1253. history, sequence, created_messages, head_seq = await self._build_history(
  1254. trace.trace_id, messages, goal_tree, config, sequence, side_branch_ctx_for_build
  1255. )
  1256. # Update trace's head_sequence in memory
  1257. trace.head_sequence = head_seq
  1258. for msg in created_messages:
  1259. yield msg
  1260. # Phase 3: AGENT LOOP
  1261. async for event in self._agent_loop(
  1262. trace, history, goal_tree, config, sequence,
  1263. inject_skills=inject_skills,
  1264. skill_recency_threshold=skill_recency_threshold,
  1265. ):
  1266. yield event
  1267. except asyncio.CancelledError:
  1268. if trace and config.approval_batch_id:
  1269. await asyncio.shield(
  1270. self._mark_approval_execution_unknown(trace.trace_id)
  1271. )
  1272. raise
  1273. except Exception as e:
  1274. self.log.error(f"Agent run failed: {e}")
  1275. # Preparation rejections (for example, attempting to resume an
  1276. # immutable Validator Trace) must not rewrite the existing record.
  1277. tid = trace.trace_id if trace else None
  1278. if self.trace_store and tid:
  1279. approval_unknown = (
  1280. await self._mark_approval_execution_unknown(tid)
  1281. if config.approval_batch_id
  1282. else False
  1283. )
  1284. # 读取当前 last_sequence 作为 head_sequence,确保续跑时能加载完整历史
  1285. current = await self.trace_store.get_trace(tid)
  1286. head_seq = current.last_sequence if current else None
  1287. updates: Dict[str, Any] = {
  1288. "status": "failed",
  1289. "head_sequence": head_seq,
  1290. "error_message": (
  1291. "Tool execution outcome is unknown; automatic retry was refused"
  1292. if approval_unknown
  1293. else str(e)
  1294. ),
  1295. "completed_at": datetime.now(),
  1296. }
  1297. if isinstance(e, ResourceBudgetExceeded):
  1298. current_context = dict(current.context if current else {})
  1299. current_context["termination_reason"] = (
  1300. f"budget_exhausted:{e.dimension}"
  1301. )
  1302. updates["context"] = current_context
  1303. await self.trace_store.update_trace(
  1304. tid,
  1305. **updates,
  1306. )
  1307. trace_obj = await self.trace_store.get_trace(tid)
  1308. if trace_obj:
  1309. yield trace_obj
  1310. raise
  1311. finally:
  1312. if trace and run_cancel_event is not None:
  1313. self.release_recursive_trace(trace.trace_id, run_cancel_event)
  1314. async def run_result(
  1315. self,
  1316. messages: List[Dict],
  1317. config: Optional[RunConfig] = None,
  1318. on_event: Optional[Callable] = None,
  1319. inject_skills: Optional[List[str]] = None,
  1320. ) -> Dict[str, Any]:
  1321. """
  1322. 结果模式 — 消费 run(),返回结构化结果。
  1323. 主要用于 agent/evaluate 工具内部。
  1324. Args:
  1325. on_event: 可选回调,每个 Trace/Message 事件触发一次,用于实时输出子 Agent 执行过程。
  1326. inject_skills: 本次调用需要指定注入的 skill 列表(透传给 run())。
  1327. """
  1328. last_assistant_text = ""
  1329. final_trace: Optional[Trace] = None
  1330. async for item in self.run(messages=messages, config=config, inject_skills=inject_skills):
  1331. if on_event:
  1332. on_event(item)
  1333. if isinstance(item, Message) and item.role == "assistant":
  1334. content = item.content
  1335. text = ""
  1336. if isinstance(content, dict):
  1337. text = content.get("text", "") or ""
  1338. elif isinstance(content, str):
  1339. text = content
  1340. if text and text.strip():
  1341. last_assistant_text = text
  1342. elif isinstance(item, Trace):
  1343. final_trace = item
  1344. config = config or RunConfig()
  1345. if not final_trace and config.trace_id and self.trace_store:
  1346. final_trace = await self.trace_store.get_trace(config.trace_id)
  1347. status = final_trace.status if final_trace else "unknown"
  1348. error = final_trace.error_message if final_trace else None
  1349. summary = last_assistant_text
  1350. stopped_recursively = bool(
  1351. final_trace
  1352. and status == "stopped"
  1353. and policy_from_context(final_trace.context).mode is AgentMode.RECURSIVE
  1354. )
  1355. if not summary and stopped_recursively:
  1356. summary = "Agent execution stopped."
  1357. elif not summary and status == "waiting_confirmation":
  1358. summary = "Agent is waiting for local tool approval."
  1359. elif not summary:
  1360. status = "failed"
  1361. error = error or "Agent 没有产生 assistant 文本结果"
  1362. # 获取保存的知识 ID
  1363. trace_id = final_trace.trace_id if final_trace else config.trace_id
  1364. saved_knowledge_ids = self._saved_knowledge_ids.get(trace_id, [])
  1365. return {
  1366. "status": status,
  1367. "summary": summary,
  1368. "trace_id": trace_id,
  1369. "error": error,
  1370. "saved_knowledge_ids": saved_knowledge_ids, # 新增:返回保存的知识 ID
  1371. "stats": {
  1372. "total_messages": final_trace.total_messages if final_trace else 0,
  1373. "total_tokens": final_trace.total_tokens if final_trace else 0,
  1374. "total_cost": final_trace.total_cost if final_trace else 0.0,
  1375. },
  1376. }
  1377. async def stop(self, trace_id: str) -> bool:
  1378. """
  1379. 停止运行中的 Trace
  1380. Trace API 定位实际 Runner 后调用本方法;Legacy 只停当前 Trace,
  1381. Recursive 由 ``request_stop`` 把信号传给当前进程内已登记的子树。
  1382. Returns:
  1383. True 如果成功发送停止信号,False 如果该 trace 不在运行中
  1384. """
  1385. return self.request_stop(trace_id)
  1386. def request_stop(self, trace_id: str) -> bool:
  1387. """同步设置停止信号,供 API 与 stdin 回调共用。
  1388. Recursive Trace 会沿进程内父子登记表向下遍历,不影响父级或兄弟分支。
  1389. """
  1390. if trace_id not in self._cancel_events:
  1391. return False
  1392. if trace_id not in self._recursive_active_traces:
  1393. self._cancel_events[trace_id].set()
  1394. return True
  1395. pending = [trace_id]
  1396. visited = set()
  1397. while pending:
  1398. current = pending.pop()
  1399. if current in visited:
  1400. continue
  1401. visited.add(current)
  1402. event = self._cancel_events.get(current)
  1403. if event:
  1404. event.set()
  1405. pending.extend(tuple(self._active_children.get(current, ())))
  1406. return True
  1407. def register_recursive_child(
  1408. self,
  1409. parent_trace_id: str,
  1410. child_trace_id: str,
  1411. ) -> asyncio.Event:
  1412. """登记已创建但可能仍在排队的 Recursive 直属孩子。
  1413. ``agent`` 工具预创建孩子后、Runner 启动 Validator 前调用,使父级停止能传递到后代。
  1414. """
  1415. event = self._cancel_events.setdefault(child_trace_id, asyncio.Event())
  1416. self._recursive_active_traces[child_trace_id] = event
  1417. self._active_children.setdefault(parent_trace_id, set()).add(child_trace_id)
  1418. self._active_parents[child_trace_id] = parent_trace_id
  1419. parent_event = self._cancel_events.get(parent_trace_id)
  1420. if parent_event and parent_event.is_set():
  1421. event.set()
  1422. return event
  1423. def is_cancel_requested(self, trace_id: str) -> bool:
  1424. event = self._cancel_events.get(trace_id)
  1425. return bool(event and event.is_set())
  1426. def unregister_recursive_trace(
  1427. self,
  1428. trace_id: str,
  1429. event: Optional[asyncio.Event] = None,
  1430. ) -> None:
  1431. """幂等清理运行登记;event 防止旧运行误删续跑状态。"""
  1432. current_event = self._recursive_active_traces.get(trace_id)
  1433. if event is not None and current_event is not event:
  1434. return
  1435. self._recursive_active_traces.pop(trace_id, None)
  1436. parent_id = self._active_parents.pop(trace_id, None)
  1437. if parent_id:
  1438. children = self._active_children.get(parent_id)
  1439. if children:
  1440. children.discard(trace_id)
  1441. if not children:
  1442. self._active_children.pop(parent_id, None)
  1443. if not self._active_children.get(trace_id):
  1444. self._active_children.pop(trace_id, None)
  1445. def release_recursive_trace(
  1446. self,
  1447. trace_id: str,
  1448. event: Optional[asyncio.Event] = None,
  1449. ) -> None:
  1450. """完成一次运行并按对象身份释放取消 Event 与父子登记。"""
  1451. current_event = self._cancel_events.get(trace_id)
  1452. if event is not None and current_event is not event:
  1453. return
  1454. self.unregister_recursive_trace(trace_id, event)
  1455. if self._cancel_events.get(trace_id) is current_event:
  1456. self._cancel_events.pop(trace_id, None)
  1457. async def _mark_trace_stopped(
  1458. self,
  1459. trace_id: str,
  1460. head_sequence: Optional[int],
  1461. ) -> Optional[Trace]:
  1462. if not self.trace_store:
  1463. return None
  1464. await self.trace_store.update_trace(
  1465. trace_id,
  1466. status="stopped",
  1467. head_sequence=head_sequence,
  1468. completed_at=datetime.now(),
  1469. )
  1470. try:
  1471. from cyber_agent.trace.websocket import broadcast_trace_status_changed
  1472. await broadcast_trace_status_changed(trace_id, "stopped")
  1473. except Exception:
  1474. pass
  1475. return await self.trace_store.get_trace(trace_id)
  1476. # ===== 单次调用(保留)=====
  1477. async def call(
  1478. self,
  1479. messages: List[Dict],
  1480. model: str = "gpt-4o",
  1481. tools: Optional[List[str]] = None,
  1482. uid: Optional[str] = None,
  1483. trace: bool = True,
  1484. **kwargs
  1485. ) -> CallResult:
  1486. """
  1487. 单次 LLM 调用(无 Agent Loop)
  1488. """
  1489. if not self.llm_call:
  1490. raise ValueError("llm_call function not provided")
  1491. trace_id = None
  1492. message_id = None
  1493. tool_schemas = self._get_tool_schemas(tools)
  1494. if trace and self.trace_store:
  1495. trace_obj = Trace.create(mode="call", uid=uid, model=model, tools=tool_schemas, llm_params=kwargs)
  1496. trace_id = await self.trace_store.create_trace(trace_obj)
  1497. result = await self.llm_call(messages=messages, model=model, tools=tool_schemas, **kwargs)
  1498. if trace and self.trace_store and trace_id:
  1499. msg = Message.create(
  1500. trace_id=trace_id, role="assistant", sequence=1, goal_id=None,
  1501. content={"text": result.get("content", ""), "tool_calls": result.get("tool_calls")},
  1502. prompt_tokens=result.get("prompt_tokens", 0),
  1503. completion_tokens=result.get("completion_tokens", 0),
  1504. finish_reason=result.get("finish_reason"),
  1505. cost=result.get("cost", 0),
  1506. )
  1507. message_id = await self.trace_store.add_message(msg)
  1508. await self.trace_store.update_trace(trace_id, status="completed", completed_at=datetime.now())
  1509. return CallResult(
  1510. reply=result.get("content", ""),
  1511. tool_calls=result.get("tool_calls"),
  1512. trace_id=trace_id,
  1513. step_id=message_id,
  1514. tokens={"prompt": result.get("prompt_tokens", 0), "completion": result.get("completion_tokens", 0)},
  1515. cost=result.get("cost", 0)
  1516. )
  1517. # ===== Phase 1: PREPARE TRACE =====
  1518. async def _prepare_trace(
  1519. self,
  1520. messages: List[Dict],
  1521. config: RunConfig,
  1522. ) -> Tuple[Trace, Optional[GoalTree], int]:
  1523. """
  1524. 准备 Trace:为 ``run`` 选择新建、续跑或回溯路径。
  1525. 在 Agent 主循环前调用,并在任何 Trace 操作前校验已废弃的配置项。
  1526. Returns:
  1527. (trace, goal_tree, next_sequence)
  1528. """
  1529. assert_removed_config_absent()
  1530. if self.application_binding is not None and any(
  1531. message.get("role") == "system" for message in messages
  1532. ):
  1533. raise ValueError(
  1534. "Application runs do not allow caller-provided system messages"
  1535. )
  1536. if config.trace_id:
  1537. return await self._prepare_existing_trace(config)
  1538. else:
  1539. return await self._prepare_new_trace(messages, config)
  1540. @staticmethod
  1541. def _application_ref_dict(value: Any) -> dict[str, Any] | None:
  1542. if value is None:
  1543. return None
  1544. if hasattr(value, "model_dump"):
  1545. return value.model_dump(mode="json")
  1546. return dict(value) if isinstance(value, dict) else None
  1547. def _validate_application_config(self, config: RunConfig) -> None:
  1548. """Reject a new application RunConfig that diverges from its Binding."""
  1549. binding = self.application_binding
  1550. if binding is None:
  1551. raise ValueError("Application binding is unavailable")
  1552. expected_ref = binding.application_ref.model_dump(mode="json")
  1553. if self._application_ref_dict(config.application_ref) != expected_ref:
  1554. raise ValueError("RunConfig ApplicationRef does not match Runner binding")
  1555. if not config.role_id:
  1556. raise ValueError("Application role_id is required")
  1557. role = binding.role(config.role_id)
  1558. if config.role_hash != role.role_hash:
  1559. raise ValueError("RunConfig role hash does not match Runner binding")
  1560. if (
  1561. config.model != role.role.model
  1562. or config.temperature != role.role.temperature
  1563. or config.extra_llm_params != role.role.model_parameters
  1564. or not set(config.tools or []).issubset(set(role.tool_names))
  1565. or config.system_prompt != role.system_prompt
  1566. or config.skills != []
  1567. ):
  1568. raise ValueError("RunConfig behavior does not match the bound application role")
  1569. limits = dict(config.effective_run_limits)
  1570. if not limits:
  1571. raise ValueError("Application effective_run_limits are required")
  1572. allowed_limits = role.effective_limits.model_dump(mode="json")
  1573. for name, allowed in allowed_limits.items():
  1574. value = limits.get(name)
  1575. if value is None or value > allowed:
  1576. raise ValueError(
  1577. f"Application run limit exceeds the bound role: {name}"
  1578. )
  1579. if (
  1580. config.max_iterations != limits["max_iterations"]
  1581. or config.max_parallel_children != limits["max_parallel_children"]
  1582. ):
  1583. raise ValueError("RunConfig limits do not match effective_run_limits")
  1584. def _validate_application_snapshot(
  1585. self,
  1586. snapshot: RunConfigSnapshotV1 | RunConfigSnapshotV2,
  1587. trace: Trace,
  1588. ) -> None:
  1589. """Perform the Runner-side binding gate before any resume mutation."""
  1590. if isinstance(snapshot, RunConfigSnapshotV1) and not isinstance(
  1591. snapshot,
  1592. RunConfigSnapshotV2,
  1593. ):
  1594. if self.application_binding is not None:
  1595. raise ValueError("Application-bound Runner cannot resume a V1 Trace")
  1596. return
  1597. if self.application_binding is None:
  1598. raise ValueError(
  1599. "Application Trace requires an ApplicationRuntime-bound Runner"
  1600. )
  1601. expected_ref = self.application_binding.application_ref.model_dump(mode="json")
  1602. if snapshot.application_ref != expected_ref:
  1603. raise ValueError("ApplicationRef does not match Runner binding")
  1604. role = self.application_binding.role(snapshot.role_id)
  1605. if snapshot.role_hash != role.role_hash:
  1606. raise ValueError("Application role hash does not match Runner binding")
  1607. if (
  1608. trace.context.get("application_ref") != snapshot.application_ref
  1609. or trace.context.get("application_role_id") != snapshot.role_id
  1610. or trace.context.get("application_role_hash") != snapshot.role_hash
  1611. or trace.context.get("effective_run_limits")
  1612. != snapshot.effective_run_limits
  1613. ):
  1614. raise ValueError("Application snapshot does not match Trace context")
  1615. restored = RunConfig()
  1616. restored.apply_snapshot(snapshot)
  1617. restored.system_prompt = role.system_prompt
  1618. restored.skills = []
  1619. self._validate_application_config(restored)
  1620. async def _prepare_new_trace(
  1621. self,
  1622. messages: List[Dict],
  1623. config: RunConfig,
  1624. ) -> Tuple[Trace, Optional[GoalTree], int]:
  1625. """创建并持久化一个新根 Trace。
  1626. ``run`` 首次执行时调用;Recursive 会在此固化根任务锚点和树级预算。
  1627. """
  1628. if self.application_binding is not None:
  1629. self._validate_application_config(config)
  1630. elif config.application_ref is not None:
  1631. raise ValueError(
  1632. "Application runs require an ApplicationRuntime-bound Runner"
  1633. )
  1634. # 在任何标题生成/LLM 调用前完成模式校验。
  1635. policy = policy_from_environment(
  1636. recursive_revision=CURRENT_RECURSIVE_REVISION,
  1637. )
  1638. if policy.mode is AgentMode.RECURSIVE:
  1639. validate_recursive_child_execution(
  1640. config.child_execution_mode,
  1641. config.max_parallel_children,
  1642. )
  1643. if config.root_task_anchor is None:
  1644. raise ValueError(
  1645. "New Recursive root traces require root_task_anchor"
  1646. )
  1647. try:
  1648. root_task_anchor = normalize_root_task_anchor(
  1649. config.root_task_anchor
  1650. )
  1651. except ContextPolicyError as exc:
  1652. raise ValueError(str(exc)) from exc
  1653. deployment_budget = ResourceBudget.from_environment()
  1654. if self.application_binding is not None:
  1655. limits = config.effective_run_limits
  1656. budget = ResourceBudget(
  1657. enabled=deployment_budget.enabled,
  1658. max_total_agents=int(limits["max_total_agents"]),
  1659. max_llm_calls=int(limits["max_llm_calls"]),
  1660. max_total_tokens=int(limits["max_total_tokens"]),
  1661. max_total_cost_usd=float(limits["max_total_cost_usd"]),
  1662. max_duration_seconds=int(limits["max_duration_seconds"]),
  1663. reserved_final_calls=min(
  1664. deployment_budget.reserved_final_calls,
  1665. int(limits["max_llm_calls"]) - 1,
  1666. ),
  1667. max_validation_tool_calls=int(
  1668. limits["max_validation_tool_calls"]
  1669. ),
  1670. max_validation_material_chars=int(
  1671. limits["max_validation_material_chars"]
  1672. ),
  1673. )
  1674. else:
  1675. budget = deployment_budget
  1676. validator_settings = ValidatorSettings.from_environment()
  1677. trace_id = str(uuid.uuid4())
  1678. # 生成任务名称
  1679. task_name = config.name or (
  1680. self._fallback_task_name(messages)
  1681. if policy.mode is AgentMode.RECURSIVE
  1682. else await self._generate_task_name(messages)
  1683. )
  1684. # 准备工具 Schema
  1685. tool_schemas = self._get_tool_schemas(config.tools, config.tool_groups, config.exclude_tools)
  1686. trace_context = apply_policy_to_context(config.context, policy)
  1687. trace_context.setdefault("agent_depth", 0)
  1688. trace_context.setdefault("root_trace_id", trace_id)
  1689. memory_identity = (
  1690. compute_memory_identity(config.memory) if config.memory else None
  1691. )
  1692. if memory_identity is not None:
  1693. trace_context[MEMORY_IDENTITY_CONTEXT_KEY] = memory_identity
  1694. if self.application_binding is not None:
  1695. run_snapshot = RunConfigSnapshotV2.from_run_config(
  1696. config,
  1697. memory_identity=memory_identity,
  1698. )
  1699. trace_context.update({
  1700. "application_ref": run_snapshot.application_ref,
  1701. "application_role_id": run_snapshot.role_id,
  1702. "application_role_hash": run_snapshot.role_hash,
  1703. "effective_run_limits": run_snapshot.effective_run_limits,
  1704. })
  1705. else:
  1706. run_snapshot = RunConfigSnapshotV1.from_run_config(
  1707. config,
  1708. memory_identity=memory_identity,
  1709. )
  1710. persist_run_config_snapshot(trace_context, run_snapshot)
  1711. if policy.requires_task_protocol:
  1712. persist_root_task_anchor(trace_context, root_task_anchor)
  1713. persist_validation_policy(
  1714. trace_context,
  1715. self.validation_policy,
  1716. validator_settings,
  1717. )
  1718. trace_context[RESOURCE_BUDGET_CONTEXT_KEY] = budget.to_dict()
  1719. trace_context["task_protocol"] = new_task_protocol()
  1720. state = ensure_task_protocol(trace_context)
  1721. if policy.requires_task_progress:
  1722. initialize_task_progress(
  1723. state,
  1724. root_task_anchor_hash=trace_context.get(
  1725. "root_task_anchor_hash"
  1726. ),
  1727. )
  1728. replace_context_access(
  1729. trace_context,
  1730. [],
  1731. root_task_anchor=root_task_anchor,
  1732. task_brief=state.get("task_brief"),
  1733. )
  1734. if self.application_binding is not None and self.context_provider is not None:
  1735. from cyber_agent.application.context import load_application_context
  1736. from cyber_agent.application.ports import ContextRequest
  1737. await load_application_context(
  1738. self.application_binding,
  1739. trace_context,
  1740. ContextRequest(
  1741. application_ref=self.application_binding.application_ref,
  1742. root_trace_id=trace_id,
  1743. trace_id=trace_id,
  1744. uid=config.uid,
  1745. role_id=config.role_id,
  1746. task_brief=None,
  1747. task_brief_revision=0,
  1748. ),
  1749. root_task_anchor=root_task_anchor,
  1750. task_brief=None,
  1751. granted_at_sequence=0,
  1752. )
  1753. trace_obj = Trace(
  1754. trace_id=trace_id,
  1755. mode="agent",
  1756. task=task_name,
  1757. agent_type=config.agent_type,
  1758. parent_trace_id=config.parent_trace_id,
  1759. parent_goal_id=config.parent_goal_id,
  1760. uid=config.uid,
  1761. model=config.model,
  1762. tools=tool_schemas,
  1763. llm_params={"temperature": config.temperature, **config.extra_llm_params},
  1764. context=trace_context,
  1765. status="running",
  1766. )
  1767. goal_tree = self.goal_tree or GoalTree(mission=task_name)
  1768. if self.trace_store:
  1769. await self.trace_store.create_trace(trace_obj)
  1770. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  1771. assert self.resource_budget is not None
  1772. if policy.mode is AgentMode.RECURSIVE:
  1773. await self.resource_budget.initialize(
  1774. trace_id,
  1775. budget,
  1776. initial_agents=1,
  1777. )
  1778. return trace_obj, goal_tree, 1
  1779. async def _prepare_existing_trace(
  1780. self,
  1781. config: RunConfig,
  1782. ) -> Tuple[Trace, Optional[GoalTree], int]:
  1783. """加载已有 Trace,决定续跑或回溯。
  1784. ``run`` 携带 ``trace_id`` 时调用;Recursive 只信任持久化模式、预算和协议状态。
  1785. """
  1786. if not self.trace_store:
  1787. raise ValueError("trace_store required for continue/rewind")
  1788. trace_obj = await self.trace_store.get_trace(config.trace_id)
  1789. if not trace_obj:
  1790. raise ValueError(f"Trace not found: {config.trace_id}")
  1791. if (
  1792. trace_obj.agent_type == "validator"
  1793. or trace_obj.context.get("created_by_tool") == "validator"
  1794. ):
  1795. raise ValueError(
  1796. "Validator traces cannot be continued or rewound"
  1797. )
  1798. require_mutable_trace_policy(trace_obj.context)
  1799. # Resume/rewind behavior is always recovered from the immutable snapshot.
  1800. # Old Recursive traces cannot be reconstructed safely; Legacy traces get
  1801. # one explicitly-marked, conservative inferred snapshot for compatibility.
  1802. if RUN_CONFIG_SNAPSHOT_CONTEXT_KEY in trace_obj.context:
  1803. try:
  1804. snapshot = load_run_config_snapshot(trace_obj.context)
  1805. except RunConfigSnapshotError as exc:
  1806. raise ValueError(str(exc)) from exc
  1807. else:
  1808. existing_policy = policy_from_context(trace_obj.context)
  1809. if existing_policy.mode is AgentMode.RECURSIVE:
  1810. raise ValueError(
  1811. "This Recursive trace predates RunConfig snapshots; create a new trace"
  1812. )
  1813. inferred = RunConfig(
  1814. model=trace_obj.model or "gpt-4o",
  1815. temperature=float((trace_obj.llm_params or {}).get("temperature", 0.3)),
  1816. tools=[
  1817. item.get("function", {}).get("name")
  1818. for item in (trace_obj.tools or [])
  1819. if item.get("function", {}).get("name")
  1820. ],
  1821. tool_groups=None,
  1822. agent_type=trace_obj.agent_type or "default",
  1823. uid=trace_obj.uid,
  1824. extra_llm_params={
  1825. key: value
  1826. for key, value in (trace_obj.llm_params or {}).items()
  1827. if key != "temperature"
  1828. },
  1829. context={
  1830. "project_name": trace_obj.context.get("project_name")
  1831. } if trace_obj.context.get("project_name") else {},
  1832. )
  1833. snapshot = RunConfigSnapshotV1.from_run_config(
  1834. inferred,
  1835. memory_identity=None,
  1836. legacy_inferred=True,
  1837. )
  1838. persist_run_config_snapshot(trace_obj.context, snapshot)
  1839. await self.trace_store.update_trace(
  1840. trace_obj.trace_id,
  1841. context=trace_obj.context,
  1842. )
  1843. if snapshot.uid != trace_obj.uid or snapshot.agent_type != (trace_obj.agent_type or "default"):
  1844. raise ValueError("run config snapshot identity does not match Trace metadata")
  1845. self._validate_application_snapshot(snapshot, trace_obj)
  1846. config.apply_snapshot(snapshot)
  1847. if isinstance(snapshot, RunConfigSnapshotV2):
  1848. role = self.application_binding.role(snapshot.role_id)
  1849. config.system_prompt = role.system_prompt
  1850. config.skills = []
  1851. persisted_memory_identity = trace_obj.context.get(
  1852. MEMORY_IDENTITY_CONTEXT_KEY
  1853. )
  1854. if persisted_memory_identity != snapshot.memory_identity:
  1855. raise ValueError(
  1856. "run config snapshot memory identity does not match Trace context"
  1857. )
  1858. if config.memory is not None and (
  1859. compute_memory_identity(config.memory) != snapshot.memory_identity
  1860. ):
  1861. raise ValueError(
  1862. "restored MemoryConfig does not match the persisted memory identity"
  1863. )
  1864. # Historical traces predate AGENT_MODE. They resume in safe Legacy mode
  1865. # and the choice is persisted immediately; environment changes never
  1866. # mutate an existing tree.
  1867. if AGENT_MODE_CONTEXT_KEY not in trace_obj.context:
  1868. legacy_policy = policy_from_context(None)
  1869. trace_obj.context = apply_policy_to_context(trace_obj.context, legacy_policy)
  1870. await self.trace_store.update_trace(
  1871. config.trace_id,
  1872. context=trace_obj.context,
  1873. )
  1874. else:
  1875. policy = policy_from_context(trace_obj.context)
  1876. if policy.mode is AgentMode.RECURSIVE:
  1877. validate_recursive_child_execution(
  1878. config.child_execution_mode,
  1879. config.max_parallel_children,
  1880. )
  1881. if policy.requires_task_protocol:
  1882. root_trace_id = trace_obj.context.get("root_trace_id")
  1883. root = (
  1884. trace_obj
  1885. if root_trace_id == trace_obj.trace_id
  1886. else await self.trace_store.get_trace(root_trace_id)
  1887. )
  1888. if not root:
  1889. raise ValueError(
  1890. f"Recursive root Trace not found: {root_trace_id}"
  1891. )
  1892. try:
  1893. require_matching_root_task_anchor(
  1894. root.context,
  1895. trace_obj.context,
  1896. )
  1897. require_validation_policy(root.context)
  1898. except ContextPolicyError as exc:
  1899. raise ValueError(str(exc)) from exc
  1900. except ValueError as exc:
  1901. raise ValueError(str(exc)) from exc
  1902. if RESOURCE_BUDGET_CONTEXT_KEY not in root.context:
  1903. raise ValueError(
  1904. "This experimental Recursive trace predates tree resource "
  1905. "budgets; create a new trace"
  1906. )
  1907. ResourceBudget.from_dict(
  1908. root.context[RESOURCE_BUDGET_CONTEXT_KEY]
  1909. )
  1910. if root_trace_id == trace_obj.trace_id:
  1911. state = ensure_task_protocol(trace_obj.context)
  1912. if state["root_validation_attempts"] >= 2:
  1913. raise ValueError(
  1914. "Root task already used its two independent "
  1915. "validation attempts; create a new trace"
  1916. )
  1917. if policy.requires_task_progress:
  1918. assert self.task_protocol_service is not None
  1919. async with self.task_protocol_service.locked_trace(
  1920. trace_obj.trace_id
  1921. ) as fresh_trace:
  1922. state = ensure_task_protocol(fresh_trace.context)
  1923. had_report = state.get("task_report") is not None
  1924. report_progress_revision = state.get(
  1925. "task_report_progress_revision"
  1926. )
  1927. if had_report and report_progress_revision is None:
  1928. raise ValueError(
  1929. "TaskReport is missing its TaskProgress revision binding"
  1930. )
  1931. previous_head = state.get(
  1932. "task_progress_head_revision"
  1933. )
  1934. rewind_task_progress(
  1935. state,
  1936. fresh_trace.last_sequence,
  1937. )
  1938. if (
  1939. had_report
  1940. and state.get("task_report_progress_revision") is None
  1941. ):
  1942. raise ValueError(
  1943. "TaskReport references TaskProgress outside the "
  1944. "current revision ancestry"
  1945. )
  1946. if (
  1947. state.get("task_progress_head_revision")
  1948. != previous_head
  1949. ):
  1950. await self.trace_store.update_trace(
  1951. fresh_trace.trace_id,
  1952. context=fresh_trace.context,
  1953. )
  1954. trace_obj = fresh_trace
  1955. assert self.resource_budget is not None
  1956. await self.resource_budget.get_usage(root.trace_id)
  1957. goal_tree = await self.trace_store.get_goal_tree(config.trace_id)
  1958. if goal_tree is None:
  1959. # 防御性兜底:trace 存在但 goal.json 丢失时,创建空树
  1960. goal_tree = GoalTree(mission=trace_obj.task or "Agent task")
  1961. await self.trace_store.update_goal_tree(config.trace_id, goal_tree)
  1962. # 自动判断行为:after_sequence 为 None 或 == head → 续跑;< head → 回溯
  1963. after_seq = config.after_sequence
  1964. # 如果 after_seq > head_sequence,说明 generator 被强制关闭时 store 的
  1965. # head_sequence 未来得及更新(仍停在 Phase 2 写入的初始值)。
  1966. # 用 last_sequence 修正 head_sequence,确保续跑时能看到完整历史。
  1967. if after_seq is not None and after_seq > trace_obj.head_sequence:
  1968. trace_obj.head_sequence = trace_obj.last_sequence
  1969. await self.trace_store.update_trace(
  1970. config.trace_id, head_sequence=trace_obj.head_sequence
  1971. )
  1972. if after_seq is not None and after_seq < trace_obj.head_sequence:
  1973. # 回溯模式
  1974. sequence = await self._rewind(config.trace_id, after_seq, goal_tree)
  1975. else:
  1976. # 续跑模式:从 last_sequence + 1 开始
  1977. sequence = trace_obj.last_sequence + 1
  1978. # 状态置为 running
  1979. await self.trace_store.update_trace(
  1980. config.trace_id,
  1981. status="running",
  1982. completed_at=None,
  1983. )
  1984. trace_obj.status = "running"
  1985. # 广播状态变化给前端
  1986. try:
  1987. from cyber_agent.trace.websocket import broadcast_trace_status_changed
  1988. await broadcast_trace_status_changed(config.trace_id, "running")
  1989. except Exception:
  1990. pass
  1991. return trace_obj, goal_tree, sequence
  1992. # ===== Phase 2: BUILD HISTORY =====
  1993. async def _build_history(
  1994. self,
  1995. trace_id: str,
  1996. new_messages: List[Dict],
  1997. goal_tree: Optional[GoalTree],
  1998. config: RunConfig,
  1999. sequence: int,
  2000. side_branch_ctx: Optional[SideBranchContext] = None,
  2001. ) -> Tuple[List[Dict], int, List[Message], int]:
  2002. """
  2003. 构建完整的 LLM 消息历史
  2004. 1. 从 head_sequence 沿 parent chain 加载主路径消息(续跑/回溯场景)
  2005. 2. 构建 system prompt(新建时注入 skills)
  2006. 3. 新建时:在第一条 user message 末尾注入当前经验
  2007. 4. 追加 input messages(设置 parent_sequence 链接到当前 head)
  2008. 5. 如果在侧分支中,追加的消息自动标记为侧分支消息
  2009. Returns:
  2010. (history, next_sequence, created_messages, head_sequence)
  2011. created_messages: 本次新创建并持久化的 Message 列表,供 run() yield 给调用方
  2012. head_sequence: 当前主路径头节点的 sequence
  2013. """
  2014. history: List[Dict] = []
  2015. created_messages: List[Message] = []
  2016. head_seq: Optional[int] = None # 当前主路径的头节点 sequence
  2017. # 1. 加载已有 messages(通过主路径遍历)
  2018. if config.trace_id and self.trace_store:
  2019. trace_obj = await self.trace_store.get_trace(trace_id)
  2020. if trace_obj and trace_obj.head_sequence > 0:
  2021. main_path = await self.trace_store.get_main_path_messages(
  2022. trace_id, trace_obj.head_sequence
  2023. )
  2024. # An approved pending batch deliberately has orphaned calls;
  2025. # they are executed before the next model turn.
  2026. if not config.approval_batch_id:
  2027. main_path, sequence = await self._heal_orphaned_tool_calls(
  2028. main_path, trace_id, goal_tree, sequence,
  2029. )
  2030. history = [msg.to_llm_dict() for msg in main_path]
  2031. if main_path:
  2032. head_seq = main_path[-1].sequence
  2033. snapshot = load_run_config_snapshot(trace_obj.context)
  2034. if snapshot.system_prompt_hash:
  2035. persisted_system = next(
  2036. (
  2037. message.get("content")
  2038. for message in history
  2039. if message.get("role") == "system"
  2040. and isinstance(message.get("content"), str)
  2041. ),
  2042. None,
  2043. )
  2044. if persisted_system is None or sha256(
  2045. persisted_system.encode("utf-8")
  2046. ).hexdigest() != snapshot.system_prompt_hash:
  2047. raise ValueError("persisted system prompt does not match RunConfig snapshot")
  2048. current_trace = (
  2049. await self.trace_store.get_trace(trace_id)
  2050. if self.trace_store
  2051. else None
  2052. )
  2053. if (
  2054. current_trace
  2055. and current_trace.head_sequence == 0
  2056. and policy_from_context(current_trace.context).requires_task_protocol
  2057. ):
  2058. anchor = require_root_task_anchor(current_trace.context)
  2059. anchor_text = (
  2060. "# Root Task Anchor\n\n"
  2061. + canonical_json(anchor.model_dump(mode="json"))
  2062. )
  2063. anchored_messages = []
  2064. injected = False
  2065. for message in new_messages:
  2066. if not injected and message.get("role") == "user":
  2067. content = message.get("content") or ""
  2068. if isinstance(content, str):
  2069. anchored_content = f"{anchor_text}\n\n{content}"
  2070. elif isinstance(content, list):
  2071. anchored_content = [
  2072. {"type": "text", "text": anchor_text},
  2073. *content,
  2074. ]
  2075. else:
  2076. raise ValueError(
  2077. "Recursive root anchor requires text or multimodal user content"
  2078. )
  2079. anchored_messages.append({
  2080. **message,
  2081. "content": anchored_content,
  2082. })
  2083. injected = True
  2084. else:
  2085. anchored_messages.append(message)
  2086. if not injected:
  2087. anchored_messages.append({"role": "user", "content": anchor_text})
  2088. new_messages = anchored_messages
  2089. # 2. 构建/注入 skills 到 system prompt
  2090. has_system = any(m.get("role") == "system" for m in history)
  2091. has_system_in_new = any(m.get("role") == "system" for m in new_messages)
  2092. if not has_system:
  2093. if has_system_in_new:
  2094. # 入参消息已含 system,将 skills 注入其中(在 step 4 持久化之前)
  2095. augmented = []
  2096. for msg in new_messages:
  2097. if msg.get("role") == "system":
  2098. base = msg.get("content") or ""
  2099. enriched = await self._build_system_prompt(config, base_prompt=base)
  2100. augmented.append({**msg, "content": enriched or base})
  2101. else:
  2102. augmented.append(msg)
  2103. new_messages = augmented
  2104. else:
  2105. # 没有 system,自动构建并插入历史
  2106. system_prompt = await self._build_system_prompt(config)
  2107. if system_prompt:
  2108. history = [{"role": "system", "content": system_prompt}] + history
  2109. if self.trace_store:
  2110. system_msg = Message.create(
  2111. trace_id=trace_id, role="system", sequence=sequence,
  2112. goal_id=None, content=system_prompt,
  2113. parent_sequence=None, # system message 是 root
  2114. )
  2115. await self.trace_store.add_message(system_msg)
  2116. created_messages.append(system_msg)
  2117. head_seq = sequence
  2118. sequence += 1
  2119. # 3. 追加新 messages(设置 parent_sequence 链接到当前 head)
  2120. for msg_dict in new_messages:
  2121. history.append(msg_dict)
  2122. if self.trace_store:
  2123. # 如果在侧分支中,标记为侧分支消息
  2124. if side_branch_ctx:
  2125. stored_msg = Message.create(
  2126. trace_id=trace_id,
  2127. role=msg_dict["role"],
  2128. sequence=sequence,
  2129. goal_id=goal_tree.current_id if goal_tree else None,
  2130. parent_sequence=head_seq,
  2131. branch_type=side_branch_ctx.type,
  2132. branch_id=side_branch_ctx.branch_id,
  2133. content=msg_dict.get("content"),
  2134. )
  2135. self.log.info(f"用户在侧分支 {side_branch_ctx.type} 中追加消息")
  2136. else:
  2137. stored_msg = Message.from_llm_dict(
  2138. msg_dict, trace_id=trace_id, sequence=sequence,
  2139. goal_id=None, parent_sequence=head_seq,
  2140. )
  2141. await self.trace_store.add_message(stored_msg)
  2142. created_messages.append(stored_msg)
  2143. head_seq = sequence
  2144. sequence += 1
  2145. # 5. 更新 trace 的 head_sequence
  2146. if self.trace_store and head_seq is not None:
  2147. await self.trace_store.update_trace(trace_id, head_sequence=head_seq)
  2148. persisted = await self.trace_store.get_trace(trace_id)
  2149. system_content = next(
  2150. (
  2151. message.get("content")
  2152. for message in history
  2153. if message.get("role") == "system"
  2154. and isinstance(message.get("content"), str)
  2155. ),
  2156. None,
  2157. )
  2158. if persisted and system_content is not None:
  2159. snapshot = load_run_config_snapshot(persisted.context)
  2160. # Pre-created Recursive children enter through the resume path
  2161. # on their very first execution. Bind their actual persisted
  2162. # system prompt once, just like a newly-created root Trace.
  2163. if snapshot.system_prompt_hash is None:
  2164. snapshot = snapshot.model_copy(update={
  2165. "system_prompt_hash": sha256(
  2166. system_content.encode("utf-8")
  2167. ).hexdigest()
  2168. })
  2169. persist_run_config_snapshot(persisted.context, snapshot)
  2170. await self.trace_store.update_trace(
  2171. trace_id,
  2172. context=persisted.context,
  2173. )
  2174. return history, sequence, created_messages, head_seq or 0
  2175. # ===== Phase 3: AGENT LOOP =====
  2176. async def _manage_context_usage(
  2177. self,
  2178. trace_id: str,
  2179. history: List[Dict],
  2180. goal_tree: Optional[GoalTree],
  2181. config: RunConfig,
  2182. sequence: int,
  2183. head_seq: int,
  2184. ) -> Tuple[List[Dict], int, int, bool]:
  2185. """
  2186. 管理 context 用量:检查、预警、压缩
  2187. Returns:
  2188. (updated_history, new_head_seq, next_sequence, needs_enter_compression_branch)
  2189. """
  2190. compression_config = CompressionConfig()
  2191. token_count = estimate_tokens(history)
  2192. max_tokens = compression_config.get_max_tokens(config.model)
  2193. # 计算使用率
  2194. progress_pct = (token_count / max_tokens * 100) if max_tokens > 0 else 0
  2195. msg_count = len(history)
  2196. img_count = sum(
  2197. 1 for msg in history
  2198. if isinstance(msg.get("content"), list)
  2199. for part in msg["content"]
  2200. if isinstance(part, dict) and part.get("type") in ("image", "image_url")
  2201. )
  2202. # 更新 context usage 快照
  2203. self._context_usage[trace_id] = ContextUsage(
  2204. trace_id=trace_id,
  2205. message_count=msg_count,
  2206. token_count=token_count,
  2207. max_tokens=max_tokens,
  2208. usage_percent=progress_pct,
  2209. image_count=img_count,
  2210. )
  2211. # 阈值警告(30%, 50%, 80%)
  2212. if trace_id not in self._context_warned:
  2213. self._context_warned[trace_id] = set()
  2214. for threshold in [30, 50, 80]:
  2215. if progress_pct >= threshold and threshold not in self._context_warned[trace_id]:
  2216. self._context_warned[trace_id].add(threshold)
  2217. self.log.warning(
  2218. f"Context 使用率达到 {threshold}%: {token_count:,} / {max_tokens:,} tokens ({msg_count} 条消息)"
  2219. )
  2220. # 检查是否需要压缩(仅基于 token 数量)
  2221. needs_compression = token_count > max_tokens
  2222. if not needs_compression:
  2223. return history, head_seq, sequence, False
  2224. # 检查是否有待评估知识(压缩前必须先评估)
  2225. if self.trace_store and not config.force_side_branch:
  2226. pending = await self.trace_store.get_pending_knowledge_entries(trace_id)
  2227. if pending:
  2228. # 设置侧分支队列:反思 → 知识评估 → 压缩
  2229. # 反思放在前面,确保反思期间完成的 goal 产生的新知识也能在压缩前被评估
  2230. if config.knowledge.enable_extraction:
  2231. config.force_side_branch = ["reflection", "knowledge_eval", "compression"]
  2232. else:
  2233. config.force_side_branch = ["knowledge_eval", "compression"]
  2234. # 在 trace.context 中设置触发事件
  2235. trace = await self.trace_store.get_trace(trace_id)
  2236. if trace:
  2237. if not trace.context:
  2238. trace.context = {}
  2239. trace.context["knowledge_eval_trigger"] = "compression"
  2240. await self.trace_store.update_trace(trace_id, context=trace.context)
  2241. self.log.info(f"[Knowledge Eval] 压缩前触发知识评估,待评估: {len(pending)} 条")
  2242. return history, head_seq, sequence, True
  2243. # 知识提取:在任何压缩发生前,用完整 history 做反思(进入反思侧分支)
  2244. if config.knowledge.enable_extraction and not config.force_side_branch:
  2245. # 设置侧分支队列:先反思,再压缩
  2246. config.force_side_branch = ["reflection", "compression"]
  2247. return history, head_seq, sequence, True
  2248. # 以下为未启用反思、需要压缩的情况,直接进行level 1压缩,并检查是否需要进行level 2压缩(进入侧分支)
  2249. # Level 1 压缩:Goal 完成压缩
  2250. if config.goal_compression != "none" and self.trace_store and goal_tree:
  2251. if head_seq > 0:
  2252. main_path_msgs = await self.trace_store.get_main_path_messages(
  2253. trace_id, head_seq
  2254. )
  2255. compressed_msgs = compress_completed_goals(main_path_msgs, goal_tree)
  2256. if len(compressed_msgs) < len(main_path_msgs):
  2257. self.log.info(
  2258. "Level 1 压缩: %d -> %d 条消息",
  2259. len(main_path_msgs), len(compressed_msgs),
  2260. )
  2261. history = [msg.to_llm_dict() for msg in compressed_msgs]
  2262. else:
  2263. self.log.info(
  2264. "Level 1 压缩: 无可过滤消息 (%d 条全部保留)",
  2265. len(main_path_msgs),
  2266. )
  2267. elif needs_compression:
  2268. self.log.warning(
  2269. "Token 数 (%d) 超过阈值,但无法执行 Level 1 压缩(缺少 store 或 goal_tree,或 goal_compression=none)",
  2270. token_count,
  2271. )
  2272. # Level 2 压缩:检查 Level 1 后是否仍超阈值
  2273. # 注意:Level 1 压缩后需要重新优化图片并计算 token
  2274. optimized_history_after = await self._optimize_images(
  2275. history,
  2276. config.model,
  2277. trace_id=trace_id,
  2278. )
  2279. token_count_after = estimate_tokens(optimized_history_after)
  2280. needs_level2 = token_count_after > max_tokens
  2281. if needs_level2:
  2282. self.log.info(
  2283. "Level 1 后仍超阈值 (token=%d/%d),需要进入压缩侧分支",
  2284. token_count_after, max_tokens,
  2285. )
  2286. # 如果还没有设置侧分支(说明没有启用知识提取),直接进入压缩
  2287. if not config.force_side_branch:
  2288. config.force_side_branch = ["compression"]
  2289. # 返回标志,让主循环进入侧分支
  2290. return history, head_seq, sequence, True
  2291. # 压缩完成后,输出最终发给模型的消息列表
  2292. self.log.info("Level 1 压缩完成,发送给模型的消息列表:")
  2293. for idx, msg in enumerate(history):
  2294. role = msg.get("role", "unknown")
  2295. content = msg.get("content", "")
  2296. if isinstance(content, str):
  2297. preview = content[:100] + ("..." if len(content) > 100 else "")
  2298. elif isinstance(content, list):
  2299. preview = f"[{len(content)} blocks]"
  2300. else:
  2301. preview = str(content)[:100]
  2302. self.log.info(f" [{idx}] {role}: {preview}")
  2303. return history, head_seq, sequence, False
  2304. async def _build_knowledge_eval_prompt(
  2305. self,
  2306. trace_id: str,
  2307. goal_tree: Optional[GoalTree]
  2308. ) -> str:
  2309. """构建知识评估 prompt"""
  2310. if not self.trace_store:
  2311. return ""
  2312. pending = await self.trace_store.get_pending_knowledge_entries(trace_id)
  2313. if not pending:
  2314. return ""
  2315. # 获取mission
  2316. trace = await self.trace_store.get_trace(trace_id)
  2317. mission = trace.task if trace else "未知任务"
  2318. # 获取当前Goal
  2319. current_goal = goal_tree.find(goal_tree.current_id) if goal_tree and goal_tree.current_id else None
  2320. goal_desc = current_goal.description if current_goal else "无当前目标"
  2321. # 构建知识列表
  2322. knowledge_list = []
  2323. for idx, entry in enumerate(pending, 1):
  2324. knowledge_list.append(
  2325. f"### 知识 {idx}: {entry['knowledge_id']}\n"
  2326. f"- task: {entry['task']}\n"
  2327. f"- content: {entry['content']}\n"
  2328. f"- 注入于: sequence {entry['injected_at_sequence']}, goal {entry['goal_id']}"
  2329. )
  2330. prompt = f"""你是知识评估助手。请评估以下知识在本次任务执行中的实际效果。
  2331. ## 当前任务(Mission)
  2332. {mission}
  2333. ## 当前 Goal
  2334. {goal_desc}
  2335. ## 待评估知识列表
  2336. {chr(10).join(knowledge_list)}
  2337. ## 评估维度
  2338. 1. **helpfulness**: 知识内容是否对完成任务有实质帮助?
  2339. 2. **relevance**: 执行过程中是否体现了该知识的内容?
  2340. ## 评估分类
  2341. - irrelevant: task与当前任务无关
  2342. - unused: 相关但未使用
  2343. - helpful: 有帮助
  2344. - harmful: 有负面作用
  2345. - neutral: 无明显作用
  2346. ## 输出格式
  2347. 请直接输出评估结果,使用JSON格式:
  2348. {{
  2349. "evaluations": [
  2350. {{
  2351. "knowledge_id": "knowledge-xxx",
  2352. "eval_status": "helpful",
  2353. "reason": "1-2句评估理由"
  2354. }}
  2355. ]
  2356. }}
  2357. """
  2358. return prompt
  2359. async def _single_turn_compress(
  2360. self,
  2361. trace_id: str,
  2362. history: List[Dict],
  2363. goal_tree: Optional[GoalTree],
  2364. config: RunConfig,
  2365. ) -> str:
  2366. """单次 LLM 调用生成压缩摘要,返回 summary 文本"""
  2367. self.log.info("执行单次 LLM 压缩")
  2368. # 构建压缩 prompt(使用 SINGLE_TURN_PROMPT)
  2369. from cyber_agent.core.prompts import build_single_turn_prompt
  2370. goal_prompt = goal_tree.to_prompt(include_summary=True) if goal_tree else ""
  2371. compress_prompt = build_single_turn_prompt(goal_prompt)
  2372. compress_messages = list(history) + [
  2373. {"role": "user", "content": compress_prompt}
  2374. ]
  2375. # 应用 Prompt Caching
  2376. compress_messages = self._add_cache_control(
  2377. compress_messages, config.model, config.enable_prompt_caching
  2378. )
  2379. # 单次 LLM 调用(无工具)
  2380. result = await self.call_recursive_llm(
  2381. trace_id,
  2382. purpose="ordinary",
  2383. messages=compress_messages,
  2384. model=config.model,
  2385. tools=[], # 不提供工具
  2386. temperature=config.temperature,
  2387. fail_on_post_response_exhaustion=True,
  2388. **config.extra_llm_params,
  2389. )
  2390. summary_text = result.get("content", "").strip()
  2391. # 提取 [[SUMMARY]] 块
  2392. if "[[SUMMARY]]" in summary_text:
  2393. summary_text = summary_text[
  2394. summary_text.index("[[SUMMARY]]") + len("[[SUMMARY]]"):
  2395. ].strip()
  2396. return summary_text
  2397. @staticmethod
  2398. def _try_fix_json(s: str) -> Optional[dict]:
  2399. """尝试修复常见的 JSON 截断/格式问题,返回 dict 或 None"""
  2400. import re
  2401. fixed = s.strip()
  2402. # 1. 修复值中未转义的引号(如 "key": "he said "hello" to me")
  2403. # 策略:找到 key-value 模式中值字符串内部的裸引号并转义
  2404. def _fix_inner_quotes(text: str) -> str:
  2405. # 匹配 ": "..." 模式,修复值内部的未转义引号
  2406. result = []
  2407. i = 0
  2408. while i < len(text):
  2409. # 找到 ": " 后面的值字符串开头
  2410. if text[i] == '"':
  2411. # 找到这个引号对应的字符串结束位置
  2412. j = i + 1
  2413. while j < len(text):
  2414. if text[j] == '\\':
  2415. j += 2 # 跳过转义字符
  2416. continue
  2417. if text[j] == '"':
  2418. break
  2419. j += 1
  2420. # 检查引号后面是否是合法的 JSON 分隔符
  2421. if j < len(text):
  2422. after = j + 1
  2423. # 跳过空白
  2424. while after < len(text) and text[after] in ' \t\n\r':
  2425. after += 1
  2426. if after < len(text) and text[after] not in ':,}]\n\r':
  2427. # 这个引号不是真正的结束引号,继续往后找
  2428. # 找到下一个后面跟合法分隔符的引号
  2429. k = j + 1
  2430. found_end = False
  2431. while k < len(text):
  2432. if text[k] == '"':
  2433. peek = k + 1
  2434. while peek < len(text) and text[peek] in ' \t\n\r':
  2435. peek += 1
  2436. if peek >= len(text) or text[peek] in ':,}]':
  2437. # 这才是真正的结束引号,转义中间的引号
  2438. inner = text[i+1:k].replace('"', '\\"')
  2439. result.append('"' + inner + '"')
  2440. i = k + 1
  2441. found_end = True
  2442. break
  2443. k += 1
  2444. if found_end:
  2445. continue
  2446. result.append(text[i])
  2447. i += 1
  2448. return ''.join(result)
  2449. fixed = _fix_inner_quotes(fixed)
  2450. # 2. 去掉尾部多余逗号
  2451. fixed = re.sub(r',\s*([}\]])', r'\1', fixed)
  2452. # 3. 尝试补全截断的字符串和括号
  2453. for suffix in ['', '"', '"}', '"]', '"}]', '"}}']:
  2454. try:
  2455. attempt = fixed + suffix
  2456. open_braces = attempt.count('{') - attempt.count('}')
  2457. open_brackets = attempt.count('[') - attempt.count(']')
  2458. attempt += '}' * max(0, open_braces) + ']' * max(0, open_brackets)
  2459. result = json.loads(attempt)
  2460. if isinstance(result, dict):
  2461. self.log.info(f"[JSON Fix] 成功修复 JSON (suffix={repr(suffix)})")
  2462. return result
  2463. except json.JSONDecodeError:
  2464. continue
  2465. return None
  2466. async def _resume_approved_tool_batch(
  2467. self,
  2468. *,
  2469. trace: Trace,
  2470. history: List[Dict],
  2471. goal_tree: Optional[GoalTree],
  2472. config: RunConfig,
  2473. sequence: int,
  2474. head_seq: int,
  2475. side_branch_ctx: Optional[SideBranchContext],
  2476. runtime_tool_names: set[str],
  2477. ) -> Tuple[List[Dict], int, int]:
  2478. """Execute one fully-decided persisted approval batch exactly once."""
  2479. if not config.approval_batch_id:
  2480. return history, sequence, head_seq
  2481. if not self.trace_store or not hasattr(
  2482. self.trace_store, "get_tool_approval_batch"
  2483. ):
  2484. raise RuntimeError("TraceStore does not support tool approvals")
  2485. batch = await self.trace_store.get_tool_approval_batch(trace.trace_id)
  2486. if batch is None or batch.batch_id != config.approval_batch_id:
  2487. raise RuntimeError("approved tool batch was not found")
  2488. if batch.status != "decided":
  2489. raise RuntimeError(f"tool approval batch is not executable: {batch.status}")
  2490. if any(call.decision == "pending" for call in batch.calls):
  2491. raise RuntimeError("tool approval batch still has pending decisions")
  2492. if any(call.execution_status == "executing" for call in batch.calls):
  2493. batch.status = "execution_unknown"
  2494. for call in batch.calls:
  2495. if call.execution_status == "executing":
  2496. call.execution_status = "execution_unknown"
  2497. batch.updated_at = datetime.now().isoformat()
  2498. await self.trace_store.replace_tool_approval_batch(trace.trace_id, batch)
  2499. raise RuntimeError("tool execution outcome is unknown; refusing automatic retry")
  2500. batch.status = "executing"
  2501. batch.updated_at = datetime.now().isoformat()
  2502. await self.trace_store.replace_tool_approval_batch(trace.trace_id, batch)
  2503. current_goal_id = goal_tree.current_id if goal_tree and goal_tree.current_id else None
  2504. trigger_event = None
  2505. if side_branch_ctx and side_branch_ctx.type == "knowledge_eval":
  2506. trigger_event = (trace.context.get("active_side_branch") or {}).get(
  2507. "trigger_event",
  2508. "unknown",
  2509. )
  2510. for call in batch.calls:
  2511. if call.execution_status in {"executed", "rejected"}:
  2512. continue
  2513. if call.decision == "rejected":
  2514. tool_result: Any = json.dumps({
  2515. "status": "rejected",
  2516. "error": "Tool call rejected by the local user",
  2517. }, ensure_ascii=False)
  2518. call.execution_status = "rejected"
  2519. elif call.decision in {"approved", "auto_approved"}:
  2520. call.execution_status = "executing"
  2521. batch.updated_at = datetime.now().isoformat()
  2522. await self.trace_store.replace_tool_approval_batch(
  2523. trace.trace_id,
  2524. batch,
  2525. )
  2526. tool_result = await self.tools.execute(
  2527. call.tool_name,
  2528. call.effective_arguments,
  2529. uid=config.uid or "",
  2530. context=self._build_tool_context(
  2531. config=config,
  2532. trace=trace,
  2533. trace_id=trace.trace_id,
  2534. goal_id=current_goal_id,
  2535. goal_tree=goal_tree,
  2536. sequence=sequence,
  2537. tool_call_id=call.tool_call_id,
  2538. side_branch_ctx=side_branch_ctx,
  2539. trigger_event=trigger_event,
  2540. ),
  2541. allowed_tool_names=runtime_tool_names,
  2542. tool_call_id=call.tool_call_id,
  2543. approval_grant=approval_grant(batch, call),
  2544. )
  2545. else:
  2546. raise RuntimeError(f"unsupported tool approval decision: {call.decision}")
  2547. if isinstance(tool_result, str):
  2548. normalized = {"text": tool_result}
  2549. elif isinstance(tool_result, dict):
  2550. normalized = tool_result
  2551. else:
  2552. normalized = {"text": str(tool_result)}
  2553. tool_text = normalized.get("text", str(normalized))
  2554. tool_images = normalized.get("images", [])
  2555. artifact_refs = normalized.get("artifact_refs", [])
  2556. tool_content: Any = tool_text
  2557. if tool_images:
  2558. tool_content = [{"type": "text", "text": tool_text}]
  2559. for image in tool_images:
  2560. if image.get("type") == "base64" and image.get("data"):
  2561. media_type = image.get("media_type", "image/png")
  2562. tool_content.append({
  2563. "type": "image_url",
  2564. "image_url": {
  2565. "url": f"data:{media_type};base64,{image['data']}"
  2566. },
  2567. })
  2568. elif image.get("type") == "url" and image.get("url"):
  2569. tool_content.append({
  2570. "type": "image_url",
  2571. "image_url": {"url": image["url"]},
  2572. })
  2573. tool_msg = Message.create(
  2574. trace_id=trace.trace_id,
  2575. role="tool",
  2576. sequence=sequence,
  2577. goal_id=current_goal_id,
  2578. parent_sequence=head_seq,
  2579. tool_call_id=call.tool_call_id,
  2580. branch_type=side_branch_ctx.type if side_branch_ctx else None,
  2581. branch_id=side_branch_ctx.branch_id if side_branch_ctx else None,
  2582. content={
  2583. "tool_name": call.tool_name,
  2584. "result": tool_content,
  2585. "artifact_refs": artifact_refs,
  2586. },
  2587. )
  2588. await self.trace_store.add_message(tool_msg)
  2589. if tool_images and hasattr(self.trace_store, "write_message_attachment"):
  2590. import base64 as b64mod
  2591. for image in tool_images:
  2592. if image.get("data"):
  2593. await self.trace_store.write_message_attachment(
  2594. trace.trace_id,
  2595. tool_msg.message_id,
  2596. suffix=".png",
  2597. data=b64mod.b64decode(image["data"]),
  2598. )
  2599. break
  2600. call.result_message_id = tool_msg.message_id
  2601. if call.execution_status != "rejected":
  2602. call.execution_status = "executed"
  2603. batch.updated_at = datetime.now().isoformat()
  2604. await self.trace_store.replace_tool_approval_batch(trace.trace_id, batch)
  2605. history.append({
  2606. "role": "tool",
  2607. "tool_call_id": call.tool_call_id,
  2608. "name": call.tool_name,
  2609. "content": tool_content,
  2610. "_message_id": tool_msg.message_id,
  2611. })
  2612. head_seq = sequence
  2613. sequence += 1
  2614. batch.status = "completed"
  2615. batch.updated_at = datetime.now().isoformat()
  2616. await self.trace_store.replace_tool_approval_batch(trace.trace_id, batch)
  2617. await self.trace_store.update_trace(
  2618. trace.trace_id,
  2619. status="running",
  2620. head_sequence=head_seq,
  2621. )
  2622. config.approval_batch_id = None
  2623. return history, sequence, head_seq
  2624. async def _mark_approval_execution_unknown(self, trace_id: str) -> bool:
  2625. """Fail closed if an approved batch stopped after execution began."""
  2626. if not self.trace_store or not hasattr(
  2627. self.trace_store, "get_tool_approval_batch"
  2628. ):
  2629. return False
  2630. batch = await self.trace_store.get_tool_approval_batch(trace_id)
  2631. if batch is None or batch.status != "executing":
  2632. return False
  2633. batch.status = "execution_unknown"
  2634. batch.updated_at = datetime.now().isoformat()
  2635. for call in batch.calls:
  2636. if call.execution_status == "executing":
  2637. call.execution_status = "execution_unknown"
  2638. await self.trace_store.replace_tool_approval_batch(trace_id, batch)
  2639. await self.trace_store.update_trace(
  2640. trace_id,
  2641. status="failed",
  2642. error_message=(
  2643. "Tool execution outcome is unknown; automatic retry was refused"
  2644. ),
  2645. completed_at=datetime.now(),
  2646. )
  2647. return True
  2648. async def _agent_loop(
  2649. self,
  2650. trace: Trace,
  2651. history: List[Dict],
  2652. goal_tree: Optional[GoalTree],
  2653. config: RunConfig,
  2654. sequence: int,
  2655. inject_skills: Optional[List[str]] = None,
  2656. skill_recency_threshold: int = 10,
  2657. ) -> AsyncIterator[Union[Trace, Message]]:
  2658. """执行 Agent 的 ReAct 主循环。
  2659. ``run`` 在 Trace 准备后调用;Recursive 的预算、停止、工具门禁和根验收都在此串联。
  2660. """
  2661. trace_id = trace.trace_id
  2662. runtime_tool_names = self._get_runtime_tool_names(config, trace)
  2663. tool_schemas = self._get_runtime_tool_schemas(
  2664. config,
  2665. trace,
  2666. runtime_tool_names=runtime_tool_names,
  2667. )
  2668. completion_status = "completed"
  2669. # 当前主路径头节点的 sequence(用于设置 parent_sequence)
  2670. head_seq = trace.head_sequence
  2671. # 侧分支状态(None = 主路径)
  2672. side_branch_ctx: Optional[SideBranchContext] = None
  2673. # 检查是否有未完成的侧分支需要恢复
  2674. if trace.context.get("active_side_branch"):
  2675. side_branch_data = trace.context["active_side_branch"]
  2676. branch_id = side_branch_data["branch_id"]
  2677. start_sequence = side_branch_data["start_sequence"]
  2678. # 只恢复同一持久分支;sequence 范围会把相邻分支混进来。
  2679. if self.trace_store:
  2680. all_messages = await self.trace_store.get_trace_messages(trace_id)
  2681. side_messages = [
  2682. m for m in all_messages
  2683. if (
  2684. m.sequence >= start_sequence
  2685. and m.branch_id == branch_id
  2686. and m.branch_type == side_branch_data["type"]
  2687. and m.status == "active"
  2688. )
  2689. ]
  2690. reconstructed_turns = sum(
  2691. 1 for message in side_messages if message.role == "assistant"
  2692. )
  2693. persisted_turns = int(side_branch_data.get("turns_used", 0))
  2694. turns_used = max(persisted_turns, reconstructed_turns)
  2695. if turns_used != persisted_turns or "turns_used" not in side_branch_data:
  2696. side_branch_data["turns_used"] = turns_used
  2697. await self.trace_store.update_trace(
  2698. trace_id,
  2699. context=trace.context,
  2700. )
  2701. # 恢复侧分支上下文
  2702. side_branch_ctx = SideBranchContext(
  2703. type=side_branch_data["type"],
  2704. branch_id=branch_id,
  2705. start_head_seq=side_branch_data["start_head_seq"],
  2706. start_sequence=side_branch_data["start_sequence"],
  2707. start_history_length=0, # 稍后重新计算
  2708. start_iteration=side_branch_data.get("start_iteration", 0),
  2709. max_turns=side_branch_data.get("max_turns", config.side_branch_max_turns),
  2710. turns_used=int(turns_used),
  2711. )
  2712. self.log.info(
  2713. f"恢复未完成的侧分支: {side_branch_ctx.type}, "
  2714. f"max_turns={side_branch_ctx.max_turns}"
  2715. )
  2716. # 将侧分支消息追加到 history
  2717. for m in side_messages:
  2718. history.append(m.to_llm_dict())
  2719. # 重新计算 start_history_length
  2720. side_branch_ctx.start_history_length = len(history) - len(side_messages)
  2721. # The assistant Message is the durable turn record. A crash can
  2722. # happen after that Message is stored but before Trace.context is
  2723. # updated. If reconstruction shows the budget was already
  2724. # exhausted, fail the branch closed instead of buying one extra
  2725. # model turn after restart.
  2726. if side_branch_ctx.turns_used >= side_branch_ctx.max_turns:
  2727. self.log.warning(
  2728. "恢复的侧分支 %s 已用尽轮次 %d/%d,直接返回主路径",
  2729. side_branch_ctx.type,
  2730. side_branch_ctx.turns_used,
  2731. side_branch_ctx.max_turns,
  2732. )
  2733. main_path_messages = await self.trace_store.get_main_path_messages(
  2734. trace_id,
  2735. side_branch_ctx.start_head_seq,
  2736. )
  2737. history = [message.to_llm_dict() for message in main_path_messages]
  2738. head_seq = side_branch_ctx.start_head_seq
  2739. trace.context.pop("active_side_branch", None)
  2740. if config.force_side_branch:
  2741. if config.force_side_branch[0] == side_branch_ctx.type:
  2742. config.force_side_branch.pop(0)
  2743. if not config.force_side_branch:
  2744. config.force_side_branch = None
  2745. await self.trace_store.update_trace(
  2746. trace_id,
  2747. context=trace.context,
  2748. head_sequence=head_seq,
  2749. )
  2750. side_branch_ctx = None
  2751. if config.approval_batch_id:
  2752. history, sequence, head_seq = await self._resume_approved_tool_batch(
  2753. trace=trace,
  2754. history=history,
  2755. goal_tree=goal_tree,
  2756. config=config,
  2757. sequence=sequence,
  2758. head_seq=head_seq,
  2759. side_branch_ctx=side_branch_ctx,
  2760. runtime_tool_names=runtime_tool_names,
  2761. )
  2762. break_after_side_branch = False # 侧分支退出后是否 break 主循环
  2763. for iteration in range(config.max_iterations):
  2764. # 更新活动时间(表明trace正在活跃运行)
  2765. if self.trace_store:
  2766. await self.trace_store.update_trace(
  2767. trace_id,
  2768. last_activity_at=datetime.now()
  2769. )
  2770. # 检查取消信号
  2771. cancel_event = self._cancel_events.get(trace_id)
  2772. if cancel_event and cancel_event.is_set():
  2773. self.log.info(f"Trace {trace_id} stopped by user")
  2774. trace_obj = await self._mark_trace_stopped(trace_id, head_seq)
  2775. if trace_obj:
  2776. yield trace_obj
  2777. return
  2778. # 检查Goal完成触发的知识评估
  2779. if not side_branch_ctx and self.trace_store:
  2780. trace = await self.trace_store.get_trace(trace_id)
  2781. if trace and trace.context and trace.context.get("pending_knowledge_eval"):
  2782. # 清除标志
  2783. trace.context.pop("pending_knowledge_eval", None)
  2784. await self.trace_store.update_trace(trace_id, context=trace.context)
  2785. # 设置侧分支队列
  2786. config.force_side_branch = ["knowledge_eval"]
  2787. self.log.info("[Knowledge Eval] 检测到Goal完成触发,进入知识评估侧分支")
  2788. # Context 管理(仅主路径)
  2789. needs_enter_side_branch = False
  2790. if not side_branch_ctx:
  2791. # 侧分支退出后需要 break 主循环
  2792. if break_after_side_branch and not config.force_side_branch:
  2793. break
  2794. # 检查是否强制进入侧分支(API 手动触发或自动压缩流程)
  2795. if config.force_side_branch:
  2796. needs_enter_side_branch = True
  2797. self.log.info(f"强制进入侧分支: {config.force_side_branch}")
  2798. else:
  2799. # 正常的 context 管理逻辑
  2800. history, head_seq, sequence, needs_enter_side_branch = await self._manage_context_usage(
  2801. trace_id, history, goal_tree, config, sequence, head_seq
  2802. )
  2803. # 进入侧分支
  2804. if needs_enter_side_branch and not side_branch_ctx:
  2805. # 刷新 trace,获取 _manage_context_usage 可能写入 DB 的 knowledge_eval_trigger
  2806. if self.trace_store:
  2807. fresh = await self.trace_store.get_trace(trace_id)
  2808. if fresh:
  2809. trace = fresh
  2810. # 从队列中取出第一个侧分支类型
  2811. branch_type: Literal["compression", "reflection", "knowledge_eval"]
  2812. if config.force_side_branch and isinstance(config.force_side_branch, list) and len(config.force_side_branch) > 0:
  2813. branch_type = config.force_side_branch.pop(0) # type: ignore
  2814. self.log.info(f"从队列取出侧分支: {branch_type}, 剩余队列: {config.force_side_branch}")
  2815. elif config.knowledge.enable_extraction:
  2816. # 兼容旧的单值模式(如果 force_side_branch 是字符串)
  2817. branch_type = "reflection"
  2818. else:
  2819. # 自动触发:压缩
  2820. branch_type = "compression"
  2821. branch_id = f"{branch_type}_{uuid.uuid4().hex[:8]}"
  2822. side_branch_ctx = SideBranchContext(
  2823. type=branch_type,
  2824. branch_id=branch_id,
  2825. start_head_seq=head_seq,
  2826. start_sequence=sequence,
  2827. start_history_length=len(history),
  2828. start_iteration=iteration,
  2829. max_turns=config.side_branch_max_turns,
  2830. turns_used=0,
  2831. )
  2832. # 持久化侧分支状态
  2833. if self.trace_store:
  2834. # 获取触发事件(如果是 knowledge_eval 分支)
  2835. trigger_event = trace.context.get("knowledge_eval_trigger", "unknown") if branch_type == "knowledge_eval" else None
  2836. trace.context["active_side_branch"] = {
  2837. "type": side_branch_ctx.type,
  2838. "branch_id": side_branch_ctx.branch_id,
  2839. "start_head_seq": side_branch_ctx.start_head_seq,
  2840. "start_sequence": side_branch_ctx.start_sequence,
  2841. "start_iteration": side_branch_ctx.start_iteration,
  2842. "max_turns": side_branch_ctx.max_turns,
  2843. "turns_used": side_branch_ctx.turns_used,
  2844. "started_at": datetime.now().isoformat(),
  2845. }
  2846. # 如果是 knowledge_eval 分支,添加 trigger_event
  2847. if trigger_event:
  2848. trace.context["active_side_branch"]["trigger_event"] = trigger_event
  2849. # 清除触发事件标记
  2850. trace.context.pop("knowledge_eval_trigger", None)
  2851. await self.trace_store.update_trace(
  2852. trace_id,
  2853. context=trace.context
  2854. )
  2855. # 追加侧分支 prompt
  2856. if branch_type == "reflection":
  2857. # 完成场景用全局复盘 prompt,压缩场景用阶段性反思 prompt
  2858. if break_after_side_branch:
  2859. prompt = config.knowledge.get_completion_reflect_prompt()
  2860. else:
  2861. prompt = config.knowledge.get_reflect_prompt()
  2862. elif branch_type == "knowledge_eval":
  2863. prompt = await self._build_knowledge_eval_prompt(trace_id, goal_tree)
  2864. else: # compression
  2865. from cyber_agent.trace.compaction import build_compression_prompt
  2866. prompt = build_compression_prompt(goal_tree)
  2867. branch_user_msg = Message.create(
  2868. trace_id=trace_id,
  2869. role="user",
  2870. sequence=sequence,
  2871. parent_sequence=head_seq,
  2872. goal_id=goal_tree.current_id if goal_tree else None,
  2873. branch_type=branch_type,
  2874. branch_id=branch_id,
  2875. content=prompt,
  2876. )
  2877. if self.trace_store:
  2878. await self.trace_store.add_message(branch_user_msg)
  2879. history.append(branch_user_msg.to_llm_dict())
  2880. head_seq = sequence
  2881. sequence += 1
  2882. self.log.info(f"进入侧分支: {branch_type}, branch_id={branch_id}")
  2883. continue # 跳过本轮,下一轮开始侧分支
  2884. if self.trace_store:
  2885. fresh_trace = await self.trace_store.get_trace(trace_id)
  2886. if fresh_trace:
  2887. trace = fresh_trace
  2888. runtime_policy = policy_from_context(trace.context)
  2889. runtime_tool_names = self._get_runtime_tool_names(
  2890. config,
  2891. trace,
  2892. in_side_branch=side_branch_ctx is not None,
  2893. )
  2894. tool_schemas = self._get_runtime_tool_schemas(
  2895. config,
  2896. trace,
  2897. in_side_branch=side_branch_ctx is not None,
  2898. runtime_tool_names=runtime_tool_names,
  2899. )
  2900. dispatch_allowlist = (
  2901. runtime_tool_names
  2902. if runtime_policy.mode is AgentMode.RECURSIVE
  2903. else None
  2904. )
  2905. # 构建 LLM messages(注入上下文,移除内部字段)
  2906. llm_messages = [{k: v for k, v in msg.items() if not k.startswith("_")} for msg in history]
  2907. # 优化已处理的图片(分级处理:保留/压缩/描述)
  2908. llm_messages = await self._optimize_images(
  2909. llm_messages,
  2910. config.model,
  2911. trace_id=trace_id,
  2912. )
  2913. # 对历史消息应用 Prompt Caching
  2914. llm_messages = self._add_cache_control(
  2915. llm_messages,
  2916. config.model,
  2917. config.enable_prompt_caching
  2918. )
  2919. # 调用 LLM(等待完成后再检查 cancel 信号,不中断正在进行的调用)
  2920. result = await self.call_recursive_llm(
  2921. trace_id,
  2922. purpose="ordinary",
  2923. messages=llm_messages,
  2924. model=config.model,
  2925. tools=tool_schemas,
  2926. temperature=config.temperature,
  2927. **config.extra_llm_params,
  2928. )
  2929. if (
  2930. runtime_policy.mode is AgentMode.RECURSIVE
  2931. and self.is_cancel_requested(trace_id)
  2932. ):
  2933. trace_obj = await self._mark_trace_stopped(trace_id, head_seq)
  2934. if trace_obj:
  2935. yield trace_obj
  2936. return
  2937. response_content = result.get("content", "")
  2938. reasoning_content = result.get("reasoning_content", "")
  2939. tool_calls = result.get("tool_calls")
  2940. finish_reason = result.get("finish_reason")
  2941. prompt_tokens = result.get("prompt_tokens", 0)
  2942. completion_tokens = result.get("completion_tokens", 0)
  2943. step_cost = result.get("cost", 0)
  2944. cache_creation_tokens = result.get("cache_creation_tokens")
  2945. cache_read_tokens = result.get("cache_read_tokens")
  2946. budget_exceeded_dimension = result.get("_resource_budget_exceeded")
  2947. if budget_exceeded_dimension:
  2948. tool_calls = None
  2949. finish_reason = "budget_exhausted"
  2950. lifecycle_tools = {
  2951. "agent",
  2952. "submit_task_report",
  2953. "review_task_result",
  2954. "update_task_progress",
  2955. "manage_candidate",
  2956. }
  2957. lifecycle_call_count = sum(
  2958. 1 for tc in (tool_calls or [])
  2959. if tc.get("function", {}).get("name") in lifecycle_tools
  2960. )
  2961. protocol_batch_error = None
  2962. if (
  2963. runtime_policy.requires_task_protocol
  2964. and lifecycle_call_count
  2965. and len(tool_calls or []) != 1
  2966. ):
  2967. protocol_batch_error = (
  2968. "Recursive lifecycle tools must be the only tool call in an LLM turn"
  2969. )
  2970. runtime_protocol_state = (
  2971. ensure_task_protocol(trace.context)
  2972. if runtime_policy.requires_task_protocol
  2973. else None
  2974. )
  2975. protocol_lifecycle_required = bool(
  2976. runtime_protocol_state
  2977. and (
  2978. runtime_protocol_state["pending_reviews"]
  2979. or runtime_protocol_state["next_actions"]
  2980. )
  2981. )
  2982. # 周期性自动注入上下文(仅主路径)
  2983. if (
  2984. not side_branch_ctx
  2985. and not budget_exceeded_dimension
  2986. and iteration % CONTEXT_INJECTION_INTERVAL == 0
  2987. ):
  2988. # 检查是否已经调用了 get_current_context
  2989. if tool_calls:
  2990. has_context_call = any(
  2991. tc.get("function", {}).get("name") == "get_current_context"
  2992. for tc in tool_calls
  2993. )
  2994. else:
  2995. has_context_call = False
  2996. tool_calls = []
  2997. if (
  2998. not has_context_call
  2999. and not lifecycle_call_count
  3000. and not protocol_lifecycle_required
  3001. and "get_current_context" in runtime_tool_names
  3002. ):
  3003. # 手动添加 get_current_context 工具调用
  3004. context_call_id = f"call_context_{uuid.uuid4().hex[:8]}"
  3005. tool_calls.append({
  3006. "id": context_call_id,
  3007. "type": "function",
  3008. "function": {"name": "get_current_context", "arguments": "{}"}
  3009. })
  3010. self.log.info(f"[周期性注入] 自动添加 get_current_context 工具调用 (iteration={iteration})")
  3011. # Skill 指定注入(仅主路径,首轮 iteration==0 时执行)
  3012. if (
  3013. not side_branch_ctx
  3014. and not budget_exceeded_dimension
  3015. and inject_skills
  3016. and iteration == 0
  3017. and not lifecycle_call_count
  3018. and not protocol_lifecycle_required
  3019. and "skill" in runtime_tool_names
  3020. ):
  3021. skills_to_inject = self._check_skills_need_injection(
  3022. trace, inject_skills, history, skill_recency_threshold
  3023. )
  3024. if skills_to_inject:
  3025. if not tool_calls:
  3026. tool_calls = []
  3027. for skill_name in skills_to_inject:
  3028. skill_call_id = f"call_skill_{skill_name}_{uuid.uuid4().hex[:8]}"
  3029. tool_calls.append({
  3030. "id": skill_call_id,
  3031. "type": "function",
  3032. "function": {
  3033. "name": "skill",
  3034. "arguments": json.dumps({"skill_name": skill_name})
  3035. }
  3036. })
  3037. self.log.info(f"[Skill 指定注入] 自动添加 skill(\"{skill_name}\") 工具调用")
  3038. # 按需自动创建 root goal(仅主路径)
  3039. if not side_branch_ctx and goal_tree and not goal_tree.goals and tool_calls:
  3040. has_goal_call = any(
  3041. tc.get("function", {}).get("name") == "goal"
  3042. for tc in tool_calls
  3043. )
  3044. self.log.debug(f"[Auto Root Goal] Before tool execution: goal_tree.goals={len(goal_tree.goals)}, has_goal_call={has_goal_call}, tool_calls={[tc.get('function', {}).get('name') for tc in tool_calls]}")
  3045. if not has_goal_call:
  3046. mission = goal_tree.mission
  3047. root_desc = mission[:200] if len(mission) > 200 else mission
  3048. goal_tree.add_goals(
  3049. descriptions=[root_desc],
  3050. reasons=["系统自动创建:Agent 未显式创建目标"],
  3051. parent_id=None
  3052. )
  3053. if self.trace_store:
  3054. await self.trace_store.add_goal(trace_id, goal_tree.goals[0])
  3055. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  3056. self.log.info(f"自动创建 root goal: {goal_tree.goals[0].id}(未自动 focus,等待模型决定)")
  3057. else:
  3058. self.log.debug(f"[Auto Root Goal] 检测到 goal 工具调用,跳过自动创建")
  3059. # 获取当前 goal_id
  3060. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  3061. # 记录 assistant Message(parent_sequence 指向当前 head)
  3062. assistant_msg = Message.create(
  3063. trace_id=trace_id,
  3064. role="assistant",
  3065. sequence=sequence,
  3066. goal_id=current_goal_id,
  3067. parent_sequence=head_seq if head_seq > 0 else None,
  3068. branch_type=side_branch_ctx.type if side_branch_ctx else None,
  3069. branch_id=side_branch_ctx.branch_id if side_branch_ctx else None,
  3070. content={"text": response_content, "tool_calls": tool_calls, "reasoning_content": reasoning_content or None},
  3071. prompt_tokens=prompt_tokens,
  3072. completion_tokens=completion_tokens,
  3073. cache_creation_tokens=cache_creation_tokens,
  3074. cache_read_tokens=cache_read_tokens,
  3075. finish_reason=finish_reason,
  3076. cost=step_cost,
  3077. )
  3078. if self.trace_store:
  3079. await self.trace_store.add_message(assistant_msg)
  3080. # 记录模型使用
  3081. await self.trace_store.record_model_usage(
  3082. trace_id=trace_id,
  3083. sequence=sequence,
  3084. role="assistant",
  3085. model=config.model,
  3086. prompt_tokens=prompt_tokens,
  3087. completion_tokens=completion_tokens,
  3088. cache_read_tokens=cache_read_tokens or 0,
  3089. )
  3090. # 知识评估侧分支:即时检测并写入评估结果
  3091. if side_branch_ctx and side_branch_ctx.type == "knowledge_eval":
  3092. text = response_content if isinstance(response_content, str) else ""
  3093. eval_results = None
  3094. try:
  3095. eval_results = json.loads(text.strip())
  3096. if "evaluations" not in eval_results:
  3097. eval_results = None
  3098. except json.JSONDecodeError:
  3099. import re
  3100. json_match = re.search(r'```json\s*(\{.*?\})\s*```', text, re.DOTALL)
  3101. if json_match:
  3102. try:
  3103. eval_results = json.loads(json_match.group(1))
  3104. except json.JSONDecodeError:
  3105. pass
  3106. if not eval_results:
  3107. json_match = re.search(r'\{[^{]*"evaluations"[^}]*\[[^\]]*\][^}]*\}', text, re.DOTALL)
  3108. if json_match:
  3109. try:
  3110. eval_results = json.loads(json_match.group(0))
  3111. except json.JSONDecodeError:
  3112. pass
  3113. if eval_results and self.trace_store:
  3114. current_trace = await self.trace_store.get_trace(trace_id)
  3115. trigger_event = current_trace.context.get("active_side_branch", {}).get("trigger_event", "unknown")
  3116. for eval_item in eval_results.get("evaluations", []):
  3117. await self.trace_store.update_knowledge_evaluation(
  3118. trace_id=trace_id,
  3119. knowledge_id=eval_item["knowledge_id"],
  3120. eval_result={
  3121. "eval_status": eval_item["eval_status"],
  3122. "reason": eval_item.get("reason", "")
  3123. },
  3124. trigger_event=trigger_event
  3125. )
  3126. self.log.info(f"[Knowledge Eval] 已写入 {len(eval_results.get('evaluations', []))} 条评估结果")
  3127. # 一轮以成功持久化的侧分支 assistant 回复为准;先落盘计数,
  3128. # 再 yield,确保停止/重启不会重新获得已消费轮次。
  3129. if side_branch_ctx:
  3130. side_branch_ctx.turns_used += 1
  3131. active_branch = trace.context.get("active_side_branch")
  3132. if isinstance(active_branch, dict):
  3133. active_branch["turns_used"] = side_branch_ctx.turns_used
  3134. if self.trace_store:
  3135. await self.trace_store.update_trace(
  3136. trace_id,
  3137. context=trace.context,
  3138. )
  3139. yield assistant_msg
  3140. head_seq = sequence
  3141. sequence += 1
  3142. if budget_exceeded_dimension:
  3143. completion_status = "failed"
  3144. trace.context["termination_reason"] = (
  3145. f"budget_exhausted:{budget_exceeded_dimension}"
  3146. )
  3147. if self.trace_store:
  3148. await self.trace_store.update_trace(
  3149. trace_id,
  3150. context=trace.context,
  3151. error_message=trace.context["termination_reason"],
  3152. )
  3153. break
  3154. # 检查侧分支是否应该退出
  3155. if side_branch_ctx:
  3156. turns_in_branch = side_branch_ctx.turns_used
  3157. should_exit = turns_in_branch >= side_branch_ctx.max_turns or not tool_calls
  3158. if turns_in_branch >= side_branch_ctx.max_turns:
  3159. self.log.warning(
  3160. f"侧分支 {side_branch_ctx.type} 达到最大轮次 "
  3161. f"{side_branch_ctx.max_turns},强制退出"
  3162. )
  3163. if should_exit and side_branch_ctx.type == "compression":
  3164. # === 压缩侧分支退出(超时 + 正常完成统一处理)===
  3165. summary_text = ""
  3166. # 1. 从当前回复提取
  3167. if response_content:
  3168. if "[[SUMMARY]]" in response_content:
  3169. summary_text = response_content[
  3170. response_content.index("[[SUMMARY]]") + len("[[SUMMARY]]"):
  3171. ].strip()
  3172. elif response_content.strip():
  3173. summary_text = response_content.strip()
  3174. # 2. 从持久化存储按 sequence 范围查询
  3175. if not summary_text and self.trace_store:
  3176. all_messages = await self.trace_store.get_trace_messages(trace_id)
  3177. side_messages = [
  3178. m for m in all_messages
  3179. if (
  3180. m.sequence >= side_branch_ctx.start_sequence
  3181. and m.branch_id == side_branch_ctx.branch_id
  3182. and m.branch_type == side_branch_ctx.type
  3183. and m.status == "active"
  3184. )
  3185. ]
  3186. for msg in reversed(side_messages):
  3187. if msg.role == "assistant" and isinstance(msg.content, dict):
  3188. text = msg.content.get("text", "")
  3189. if "[[SUMMARY]]" in text:
  3190. summary_text = text[text.index("[[SUMMARY]]") + len("[[SUMMARY]]"):].strip()
  3191. break
  3192. elif text:
  3193. summary_text = text
  3194. break
  3195. # 3. 单次 LLM 调用
  3196. if not summary_text:
  3197. self.log.warning("侧分支未生成有效 summary,fallback 到单次 LLM 压缩")
  3198. pre_branch_history = history[:side_branch_ctx.start_history_length]
  3199. summary_text = await self._single_turn_compress(
  3200. trace_id, pre_branch_history, goal_tree, config,
  3201. )
  3202. # 创建主路径 summary 消息并重建 history
  3203. if summary_text:
  3204. # 清理侧分支指令,防止泄露到主分支
  3205. summary_text = summary_text.replace(
  3206. "**生成摘要后立即停止,不要继续执行原有任务。**", ""
  3207. ).strip()
  3208. from cyber_agent.core.prompts import build_summary_header
  3209. summary_content = build_summary_header(summary_text)
  3210. if goal_tree and goal_tree.goals:
  3211. goal_tree_detail = goal_tree.to_prompt(include_summary=True)
  3212. summary_content += f"\n\n## Current Plan\n\n{goal_tree_detail}"
  3213. # 找第一条 user message 的 sequence 作为 parent
  3214. # 续跑时 get_main_path_messages 沿 parent 链回溯,
  3215. # 指向 first_user 可以跳过所有被压缩的中间消息
  3216. first_user_seq = None
  3217. if self.trace_store:
  3218. all_msgs = await self.trace_store.get_trace_messages(trace_id)
  3219. for m in all_msgs:
  3220. if m.role == "user":
  3221. first_user_seq = m.sequence
  3222. break
  3223. summary_msg = Message.create(
  3224. trace_id=trace_id,
  3225. role="user",
  3226. sequence=sequence,
  3227. parent_sequence=first_user_seq,
  3228. branch_type=None,
  3229. content=summary_content,
  3230. )
  3231. if self.trace_store:
  3232. await self.trace_store.add_message(summary_msg)
  3233. history = self._rebuild_history_after_compression(
  3234. history, summary_msg.to_llm_dict(), label="压缩侧分支"
  3235. )
  3236. head_seq = sequence
  3237. sequence += 1
  3238. else:
  3239. self.log.error("所有压缩方案均未生成有效 summary,跳过压缩")
  3240. # 回退 history 到侧分支开始前,防止侧分支指令泄露到主分支
  3241. history = history[:side_branch_ctx.start_history_length]
  3242. head_seq = side_branch_ctx.start_head_seq
  3243. # 清理
  3244. trace.context.pop("active_side_branch", None)
  3245. config.force_side_branch = None
  3246. if self.trace_store:
  3247. await self.trace_store.update_trace(
  3248. trace_id, context=trace.context, head_sequence=head_seq,
  3249. )
  3250. side_branch_ctx = None
  3251. continue
  3252. elif should_exit and side_branch_ctx.type == "reflection":
  3253. # === 反思侧分支退出(超时 + 正常完成统一处理)===
  3254. self.log.info("反思侧分支退出")
  3255. # auto-commit hook:默认 pending 要等人工 review,
  3256. # 但 reflect_auto_commit=True 时视作全部 approved,直接批量 upload。
  3257. if (
  3258. self.trace_store
  3259. and getattr(config.knowledge, "reflect_auto_commit", False)
  3260. ):
  3261. try:
  3262. from cyber_agent.trace.extraction_review import auto_commit_branch
  3263. report = await auto_commit_branch(
  3264. self.trace_store,
  3265. trace_id,
  3266. side_branch_ctx.branch_id,
  3267. )
  3268. if report.committed or report.failed:
  3269. self.log.info(
  3270. f"[auto-commit] committed={len(report.committed)} "
  3271. f"failed={len(report.failed)} skipped={len(report.skipped)}"
  3272. )
  3273. except Exception as e:
  3274. self.log.error(f"[auto-commit] 反思分支自动提交失败: {e}")
  3275. # 恢复主路径
  3276. if self.trace_store:
  3277. main_path_messages = await self.trace_store.get_main_path_messages(
  3278. trace_id, side_branch_ctx.start_head_seq
  3279. )
  3280. history = [m.to_llm_dict() for m in main_path_messages]
  3281. head_seq = side_branch_ctx.start_head_seq
  3282. # 清理
  3283. trace.context.pop("active_side_branch", None)
  3284. if not config.force_side_branch or len(config.force_side_branch) == 0:
  3285. config.force_side_branch = None
  3286. self.log.info("反思完成,队列为空")
  3287. if self.trace_store:
  3288. await self.trace_store.update_trace(
  3289. trace_id, context=trace.context, head_sequence=head_seq,
  3290. )
  3291. side_branch_ctx = None
  3292. continue
  3293. elif should_exit and side_branch_ctx.type == "knowledge_eval":
  3294. # === 知识评估侧分支退出 ===
  3295. self.log.info("知识评估侧分支退出")
  3296. # 恢复主路径
  3297. if self.trace_store:
  3298. main_path_messages = await self.trace_store.get_main_path_messages(
  3299. trace_id, side_branch_ctx.start_head_seq
  3300. )
  3301. history = [m.to_llm_dict() for m in main_path_messages]
  3302. head_seq = side_branch_ctx.start_head_seq
  3303. # 清理
  3304. trace.context.pop("active_side_branch", None)
  3305. if not config.force_side_branch or len(config.force_side_branch) == 0:
  3306. config.force_side_branch = None
  3307. self.log.info("知识评估完成,队列为空")
  3308. if self.trace_store:
  3309. await self.trace_store.update_trace(
  3310. trace_id, context=trace.context, head_sequence=head_seq,
  3311. )
  3312. side_branch_ctx = None
  3313. continue
  3314. # 处理工具调用
  3315. # 截断兜底:finish_reason == "length" 说明响应被 max_tokens 截断,
  3316. # tool call 参数很可能不完整,不应执行,改为提示模型分批操作
  3317. if tool_calls and finish_reason == "length":
  3318. self.log.warning(
  3319. "[Runner] 响应被 max_tokens 截断,跳过 %d 个不完整的 tool calls",
  3320. len(tool_calls),
  3321. )
  3322. truncation_hint = TRUNCATION_HINT
  3323. history.append({
  3324. "role": "assistant",
  3325. "content": response_content,
  3326. "tool_calls": tool_calls,
  3327. })
  3328. # 为每个被截断的 tool call 返回错误结果
  3329. for tc in tool_calls:
  3330. history.append({
  3331. "role": "tool",
  3332. "tool_call_id": tc["id"],
  3333. "content": truncation_hint,
  3334. })
  3335. continue
  3336. if tool_calls and not config.approval_batch_id:
  3337. needs_confirmation = (
  3338. not config.auto_execute_tools
  3339. or self.tools.check_confirmation_required(tool_calls)
  3340. )
  3341. if needs_confirmation:
  3342. approval_calls: List[ToolApprovalCallV1] = []
  3343. for tool_call in tool_calls:
  3344. tool_name = tool_call.get("function", {}).get("name", "")
  3345. raw_arguments = tool_call.get("function", {}).get(
  3346. "arguments", {}
  3347. )
  3348. if isinstance(raw_arguments, str):
  3349. try:
  3350. parsed_arguments = (
  3351. json.loads(raw_arguments)
  3352. if raw_arguments.strip()
  3353. else {}
  3354. )
  3355. except json.JSONDecodeError:
  3356. parsed_arguments = {"_raw": raw_arguments}
  3357. elif isinstance(raw_arguments, dict):
  3358. parsed_arguments = dict(raw_arguments)
  3359. else:
  3360. parsed_arguments = {}
  3361. policy = self.tools.get_runtime_policy(tool_name)
  3362. requires_confirmation = (
  3363. not config.auto_execute_tools
  3364. or bool(policy.get("requires_confirmation"))
  3365. )
  3366. decision = (
  3367. "pending" if requires_confirmation else "auto_approved"
  3368. )
  3369. call_id = str(tool_call.get("id") or "")
  3370. approval_calls.append(ToolApprovalCallV1(
  3371. tool_call_id=call_id,
  3372. tool_name=tool_name,
  3373. original_arguments=parsed_arguments,
  3374. effective_arguments=parsed_arguments,
  3375. argument_hash=tool_argument_hash(
  3376. tool_call_id=call_id,
  3377. tool_name=tool_name,
  3378. arguments=parsed_arguments,
  3379. ),
  3380. editable_params=list(policy.get("editable_params", [])),
  3381. requires_confirmation=requires_confirmation,
  3382. decision=decision,
  3383. ))
  3384. batch = ToolApprovalBatchV1.create(
  3385. trace_id=trace_id,
  3386. assistant_message_id=assistant_msg.message_id,
  3387. assistant_sequence=assistant_msg.sequence,
  3388. calls=approval_calls,
  3389. )
  3390. if not self.trace_store or not hasattr(
  3391. self.trace_store, "replace_tool_approval_batch"
  3392. ):
  3393. raise RuntimeError(
  3394. "persistent TraceStore tool approval support is required"
  3395. )
  3396. await self.trace_store.replace_tool_approval_batch(
  3397. trace_id,
  3398. batch,
  3399. )
  3400. await self.trace_store.update_trace(
  3401. trace_id,
  3402. status="waiting_confirmation",
  3403. head_sequence=head_seq,
  3404. )
  3405. trace.status = "waiting_confirmation"
  3406. try:
  3407. from cyber_agent.trace.websocket import broadcast_trace_status_changed
  3408. await broadcast_trace_status_changed(
  3409. trace_id,
  3410. "waiting_confirmation",
  3411. )
  3412. except Exception:
  3413. pass
  3414. yield trace
  3415. return
  3416. if tool_calls and config.auto_execute_tools:
  3417. if (
  3418. runtime_policy.mode is AgentMode.RECURSIVE
  3419. and self.is_cancel_requested(trace_id)
  3420. ):
  3421. trace_obj = await self._mark_trace_stopped(trace_id, head_seq)
  3422. if trace_obj:
  3423. yield trace_obj
  3424. return
  3425. history.append({
  3426. "role": "assistant",
  3427. "content": response_content,
  3428. "tool_calls": tool_calls,
  3429. })
  3430. if config.parallel_tool_execution:
  3431. # === 并发执行 ===
  3432. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  3433. async def _execute_single_tool(tc: dict) -> tuple:
  3434. tool_name = tc["function"]["name"]
  3435. tool_args = tc["function"]["arguments"]
  3436. if protocol_batch_error:
  3437. return (
  3438. tc,
  3439. {},
  3440. json.dumps({
  3441. "status": "failed",
  3442. "error": protocol_batch_error,
  3443. }, ensure_ascii=False),
  3444. )
  3445. if isinstance(tool_args, str):
  3446. if not tool_args.strip():
  3447. tool_args = {}
  3448. else:
  3449. try:
  3450. tool_args = json.loads(tool_args)
  3451. except json.JSONDecodeError:
  3452. tool_args = self._try_fix_json(tool_args)
  3453. if tool_args is None:
  3454. self.log.warning(f"[Tool Call] JSON 解析失败: {tc['function']['arguments'][:200]}")
  3455. tc["function"]["arguments"] = json.dumps({"_error": "JSON parse failed", "_raw": tc["function"]["arguments"][:200]}, ensure_ascii=False)
  3456. return (tc, None, f"Error: 工具参数 JSON 格式错误,无法解析。原始参数: {tc['function']['arguments'][:200]}")
  3457. elif tool_args is None:
  3458. tool_args = {}
  3459. args_str = json.dumps(tool_args, ensure_ascii=False)
  3460. args_display = args_str[:100] + "..." if len(args_str) > 100 else args_str
  3461. self.log.info(f"[Tool Call] {tool_name}({args_display})")
  3462. trigger_event_for_tool = None
  3463. if side_branch_ctx and side_branch_ctx.type == "knowledge_eval" and self.trace_store:
  3464. current_trace = await self.trace_store.get_trace(trace_id)
  3465. if current_trace:
  3466. trigger_event_for_tool = current_trace.context.get("active_side_branch", {}).get("trigger_event", "unknown")
  3467. if tool_name in ("toolhub_call", "toolhub_search", "toolhub_health"):
  3468. try:
  3469. from cyber_agent.tools.builtin.toolhub import set_trace_context
  3470. set_trace_context(trace_id)
  3471. except ImportError:
  3472. pass
  3473. try:
  3474. tool_result = await self.tools.execute(
  3475. tool_name,
  3476. tool_args,
  3477. uid=config.uid or "",
  3478. context=self._build_tool_context(
  3479. config=config,
  3480. trace=trace,
  3481. trace_id=trace_id,
  3482. goal_id=current_goal_id,
  3483. goal_tree=goal_tree,
  3484. sequence=sequence,
  3485. tool_call_id=tc["id"],
  3486. side_branch_ctx=side_branch_ctx,
  3487. trigger_event=trigger_event_for_tool,
  3488. ),
  3489. allowed_tool_names=dispatch_allowlist,
  3490. )
  3491. return (tc, tool_args, tool_result)
  3492. except Exception as e:
  3493. import traceback
  3494. return (tc, tool_args, f"Error executing tool {tool_name}: {str(e)}\n{traceback.format_exc()}")
  3495. tasks = [_execute_single_tool(tc) for tc in tool_calls]
  3496. results = await asyncio.gather(*tasks)
  3497. for res in results:
  3498. tc, tool_args, tool_result = res
  3499. tool_name = tc["function"]["name"]
  3500. if tool_args is None:
  3501. history.append({"role": "tool", "tool_call_id": tc["id"], "name": tool_name, "content": tool_result})
  3502. yield Message.create(trace_id=trace_id, role="tool", sequence=sequence, parent_sequence=head_seq, tool_call_id=tc["id"], content=tool_result)
  3503. head_seq = sequence
  3504. sequence += 1
  3505. continue
  3506. if tool_name == "goal" and goal_tree:
  3507. self.log.debug(f"[Goal Tool] After execution: goal_tree.goals={len(goal_tree.goals)}, current_id={goal_tree.current_id}")
  3508. if tool_name == "upload_knowledge" and isinstance(tool_result, dict):
  3509. self.log.info(f"[Knowledge Tracking] 知识已上传")
  3510. if isinstance(tool_result, str):
  3511. tool_result = {"text": tool_result}
  3512. elif not isinstance(tool_result, dict):
  3513. tool_result = {"text": str(tool_result)}
  3514. tool_text = tool_result.get("text", str(tool_result))
  3515. tool_images = tool_result.get("images", [])
  3516. tool_usage = tool_result.get("tool_usage")
  3517. artifact_refs = tool_result.get("artifact_refs", [])
  3518. if tool_images:
  3519. tool_result_text = tool_text
  3520. tool_content_for_llm = [{"type": "text", "text": tool_text}]
  3521. for img in tool_images:
  3522. if img.get("type") == "base64" and img.get("data"):
  3523. media_type = img.get("media_type", "image/png")
  3524. tool_content_for_llm.append({"type": "image_url", "image_url": {"url": f"data:{media_type};base64,{img['data']}"}})
  3525. elif img.get("type") == "url" and img.get("url"):
  3526. tool_content_for_llm.append({"type": "image_url", "image_url": {"url": img["url"]}})
  3527. else:
  3528. tool_result_text = tool_text
  3529. tool_content_for_llm = tool_text
  3530. tool_msg = Message.create(trace_id=trace_id, role="tool", sequence=sequence, goal_id=current_goal_id, parent_sequence=head_seq, tool_call_id=tc["id"], branch_type=side_branch_ctx.type if side_branch_ctx else None, branch_id=side_branch_ctx.branch_id if side_branch_ctx else None, content={"tool_name": tool_name, "result": tool_content_for_llm, "artifact_refs": artifact_refs})
  3531. if self.trace_store:
  3532. await self.trace_store.add_message(tool_msg)
  3533. if tool_usage:
  3534. await self.trace_store.record_model_usage(trace_id=trace_id, sequence=sequence, role="tool", tool_name=tool_name, model=tool_usage.get("model"), prompt_tokens=tool_usage.get("prompt_tokens", 0), completion_tokens=tool_usage.get("completion_tokens", 0), cache_read_tokens=tool_usage.get("cache_read_tokens", 0))
  3535. await self.record_recursive_tool_usage(
  3536. trace_id,
  3537. tool_usage,
  3538. )
  3539. if tool_images:
  3540. import base64 as b64mod
  3541. for img in tool_images:
  3542. if img.get("data"):
  3543. await self.trace_store.write_message_attachment(
  3544. trace_id,
  3545. tool_msg.message_id,
  3546. suffix=".png",
  3547. data=b64mod.b64decode(img["data"]),
  3548. )
  3549. break
  3550. yield tool_msg
  3551. head_seq = sequence
  3552. sequence += 1
  3553. history.append({"role": "tool", "tool_call_id": tc["id"], "name": tool_name, "content": tool_content_for_llm, "_message_id": tool_msg.message_id})
  3554. if tool_name == "skill" and tc["id"].startswith("call_skill_"):
  3555. try:
  3556. skill_args = json.loads(tc["function"]["arguments"]) if isinstance(tc["function"]["arguments"], str) else tc["function"]["arguments"]
  3557. injected_skill_name = skill_args.get("skill_name", "")
  3558. if injected_skill_name:
  3559. await self._update_skill_injection_record(trace_id, trace, injected_skill_name, tool_msg.message_id, tool_msg.sequence)
  3560. self.log.info(f"[Skill 指定注入] 已记录 {injected_skill_name} → msg={tool_msg.message_id}")
  3561. except Exception as e:
  3562. self.log.warning(f"[Skill 指定注入] 记录追踪失败: {e}")
  3563. else:
  3564. for tc in tool_calls:
  3565. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  3566. tool_name = tc["function"]["name"]
  3567. tool_args = tc["function"]["arguments"]
  3568. if isinstance(tool_args, str):
  3569. if not tool_args.strip():
  3570. tool_args = {}
  3571. else:
  3572. try:
  3573. tool_args = json.loads(tool_args)
  3574. except json.JSONDecodeError:
  3575. # 尝试修复常见的截断/格式问题
  3576. tool_args = self._try_fix_json(tool_args)
  3577. if tool_args is None:
  3578. self.log.warning(f"[Tool Call] JSON 解析失败,跳过工具调用 {tool_name}: {tc['function']['arguments'][:200]}")
  3579. # 修复 history 中 assistant message 里的残缺 JSON,
  3580. # 避免 Qwen API 拒绝 "function.arguments must be in JSON format"
  3581. tc["function"]["arguments"] = json.dumps(
  3582. {"_error": "JSON parse failed", "_raw": tc["function"]["arguments"][:200]},
  3583. ensure_ascii=False,
  3584. )
  3585. history.append({
  3586. "role": "tool",
  3587. "tool_call_id": tc["id"],
  3588. "content": f"Error: 工具参数 JSON 格式错误,无法解析。请重新生成正确的 JSON 参数调用此工具。原始参数: {tc['function']['arguments'][:200]}",
  3589. })
  3590. # 注意:这里不 yield Message,因为缺少必需参数会导致错误
  3591. # yield Message 应该由 trace_store 统一管理
  3592. continue
  3593. elif tool_args is None:
  3594. tool_args = {}
  3595. # 记录工具调用(INFO 级别,显示参数)
  3596. args_str = json.dumps(tool_args, ensure_ascii=False)
  3597. args_display = args_str[:100] + "..." if len(args_str) > 100 else args_str
  3598. self.log.info(f"[Tool Call] {tool_name}({args_display})")
  3599. # 获取trigger_event(如果在knowledge_eval侧分支中)
  3600. trigger_event_for_tool = None
  3601. if side_branch_ctx and side_branch_ctx.type == "knowledge_eval" and self.trace_store:
  3602. current_trace = await self.trace_store.get_trace(trace_id)
  3603. if current_trace:
  3604. trigger_event_for_tool = current_trace.context.get("active_side_branch", {}).get("trigger_event", "unknown")
  3605. # 设置 trace_id 上下文供 toolhub 使用(图片保存到 outputs/{trace_id}/)
  3606. if tool_name in ("toolhub_call", "toolhub_search", "toolhub_health"):
  3607. try:
  3608. from cyber_agent.tools.builtin.toolhub import set_trace_context
  3609. set_trace_context(trace_id)
  3610. except ImportError:
  3611. pass
  3612. if protocol_batch_error:
  3613. tool_result = json.dumps({
  3614. "status": "failed",
  3615. "error": protocol_batch_error,
  3616. }, ensure_ascii=False)
  3617. else:
  3618. tool_result = await self.tools.execute(
  3619. tool_name,
  3620. tool_args,
  3621. uid=config.uid or "",
  3622. context=self._build_tool_context(
  3623. config=config,
  3624. trace=trace,
  3625. trace_id=trace_id,
  3626. goal_id=current_goal_id,
  3627. goal_tree=goal_tree,
  3628. sequence=sequence,
  3629. tool_call_id=tc["id"],
  3630. side_branch_ctx=side_branch_ctx,
  3631. trigger_event=trigger_event_for_tool,
  3632. ),
  3633. allowed_tool_names=dispatch_allowlist,
  3634. )
  3635. # 如果是 goal 工具,记录执行后的状态
  3636. if tool_name == "goal" and goal_tree:
  3637. self.log.debug(f"[Goal Tool] After execution: goal_tree.goals={len(goal_tree.goals)}, current_id={goal_tree.current_id}")
  3638. # 跟踪上传的知识(通过 upload_knowledge)
  3639. if tool_name == "upload_knowledge" and isinstance(tool_result, dict):
  3640. metadata = tool_result.get("metadata", {})
  3641. # upload_knowledge 返回的是统计信息,不是单个 knowledge_id
  3642. # 这里只记录上传动作,不跟踪具体 ID
  3643. self.log.info(f"[Knowledge Tracking] 知识已上传到 Knowledge Manager")
  3644. # --- 支持多模态工具反馈 ---
  3645. # execute() 返回 dict{"text","images","tool_usage"} 或 str
  3646. # 统一为dict格式
  3647. if isinstance(tool_result, str):
  3648. tool_result = {"text": tool_result}
  3649. tool_text = tool_result.get("text", str(tool_result))
  3650. tool_images = tool_result.get("images", [])
  3651. tool_usage = tool_result.get("tool_usage") # 新增:提取tool_usage
  3652. artifact_refs = tool_result.get("artifact_refs", [])
  3653. # 处理多模态消息
  3654. if tool_images:
  3655. tool_result_text = tool_text
  3656. # 构建多模态消息格式
  3657. tool_content_for_llm = [{"type": "text", "text": tool_text}]
  3658. for img in tool_images:
  3659. if img.get("type") == "base64" and img.get("data"):
  3660. media_type = img.get("media_type", "image/png")
  3661. tool_content_for_llm.append({
  3662. "type": "image_url",
  3663. "image_url": {
  3664. "url": f"data:{media_type};base64,{img['data']}"
  3665. }
  3666. })
  3667. elif img.get("type") == "url" and img.get("url"):
  3668. tool_content_for_llm.append({
  3669. "type": "image_url",
  3670. "image_url": {
  3671. "url": img["url"]
  3672. }
  3673. })
  3674. img_count = len(tool_content_for_llm) - 1 # 减去 text 块
  3675. print(f"[Runner] 多模态工具反馈: tool={tool_name}, images={img_count}, text_len={len(tool_result_text)}")
  3676. else:
  3677. tool_result_text = tool_text
  3678. tool_content_for_llm = tool_text
  3679. tool_msg = Message.create(
  3680. trace_id=trace_id,
  3681. role="tool",
  3682. sequence=sequence,
  3683. goal_id=current_goal_id,
  3684. parent_sequence=head_seq,
  3685. tool_call_id=tc["id"],
  3686. branch_type=side_branch_ctx.type if side_branch_ctx else None,
  3687. branch_id=side_branch_ctx.branch_id if side_branch_ctx else None,
  3688. # 存储完整内容:有图片时保留 list(含 image_url),纯文本时存字符串
  3689. content={"tool_name": tool_name, "result": tool_content_for_llm, "artifact_refs": artifact_refs},
  3690. )
  3691. if self.trace_store:
  3692. await self.trace_store.add_message(tool_msg)
  3693. # 记录工具的模型使用
  3694. if tool_usage:
  3695. await self.trace_store.record_model_usage(
  3696. trace_id=trace_id,
  3697. sequence=sequence,
  3698. role="tool",
  3699. tool_name=tool_name,
  3700. model=tool_usage.get("model"),
  3701. prompt_tokens=tool_usage.get("prompt_tokens", 0),
  3702. completion_tokens=tool_usage.get("completion_tokens", 0),
  3703. cache_read_tokens=tool_usage.get("cache_read_tokens", 0),
  3704. )
  3705. await self.record_recursive_tool_usage(
  3706. trace_id,
  3707. tool_usage,
  3708. )
  3709. # 截图单独存为同名 PNG 文件
  3710. if tool_images:
  3711. import base64 as b64mod
  3712. for img in tool_images:
  3713. if img.get("data"):
  3714. png_path = await self.trace_store.write_message_attachment(
  3715. trace_id,
  3716. tool_msg.message_id,
  3717. suffix=".png",
  3718. data=b64mod.b64decode(img["data"]),
  3719. )
  3720. print(f"[Runner] 截图已保存: {png_path.name}")
  3721. break # 只存第一张
  3722. # 如果在侧分支,tool_msg 已持久化(不需要额外维护)
  3723. yield tool_msg
  3724. head_seq = sequence
  3725. sequence += 1
  3726. history.append({
  3727. "role": "tool",
  3728. "tool_call_id": tc["id"],
  3729. "name": tool_name,
  3730. "content": tool_content_for_llm,
  3731. "_message_id": tool_msg.message_id,
  3732. })
  3733. # 更新 skill 注入追踪记录
  3734. if tool_name == "skill" and tc["id"].startswith("call_skill_"):
  3735. try:
  3736. skill_args = json.loads(tc["function"]["arguments"]) if isinstance(tc["function"]["arguments"], str) else tc["function"]["arguments"]
  3737. injected_skill_name = skill_args.get("skill_name", "")
  3738. if injected_skill_name:
  3739. await self._update_skill_injection_record(
  3740. trace_id, trace, injected_skill_name,
  3741. tool_msg.message_id, tool_msg.sequence,
  3742. )
  3743. self.log.info(f"[Skill 指定注入] 已记录 {injected_skill_name} → msg={tool_msg.message_id}")
  3744. except Exception as e:
  3745. self.log.warning(f"[Skill 指定注入] 记录追踪失败: {e}")
  3746. # on_complete 模式:goal(done=...) 后立即压缩该 goal 的消息
  3747. if (
  3748. not side_branch_ctx
  3749. and config.goal_compression == "on_complete"
  3750. and self.trace_store
  3751. and goal_tree
  3752. ):
  3753. has_goal_done = False
  3754. for tc in tool_calls:
  3755. if tc["function"]["name"] != "goal":
  3756. continue
  3757. try:
  3758. raw = tc["function"]["arguments"]
  3759. args = json.loads(raw) if isinstance(raw, str) and raw.strip() else {}
  3760. except (json.JSONDecodeError, TypeError):
  3761. args = {}
  3762. if args.get("done") is not None:
  3763. has_goal_done = True
  3764. break
  3765. if has_goal_done:
  3766. main_path_msgs = await self.trace_store.get_main_path_messages(
  3767. trace_id, head_seq
  3768. )
  3769. compressed_msgs = compress_completed_goals(main_path_msgs, goal_tree)
  3770. if len(compressed_msgs) < len(main_path_msgs):
  3771. self.log.info(
  3772. "on_complete 压缩: %d -> %d 条消息",
  3773. len(main_path_msgs), len(compressed_msgs),
  3774. )
  3775. history = [msg.to_llm_dict() for msg in compressed_msgs]
  3776. continue # 继续循环
  3777. # 无工具调用
  3778. # 如果在侧分支中,已经在上面处理过了(不会走到这里)
  3779. # 主路径无工具调用 → 任务完成,检查是否需要完成后反思或知识评估
  3780. if not side_branch_ctx and self.trace_store:
  3781. fresh_trace = await self.trace_store.get_trace(trace_id)
  3782. if fresh_trace:
  3783. trace = fresh_trace
  3784. policy = policy_from_context(trace.context)
  3785. if policy.requires_task_protocol:
  3786. assert self.task_protocol_service is not None
  3787. def apply_completion_gate(fresh, state):
  3788. missing_report = bool(
  3789. fresh.parent_trace_id
  3790. and state.get("task_report") is None
  3791. )
  3792. pending_reviews = bool(state["pending_reviews"])
  3793. pending_actions = bool(state["next_actions"])
  3794. progress_error = (
  3795. task_progress_readiness_error(state)
  3796. if policy.requires_task_progress
  3797. and not fresh.parent_trace_id
  3798. else None
  3799. )
  3800. blocked = bool(
  3801. missing_report
  3802. or pending_reviews
  3803. or pending_actions
  3804. or progress_error
  3805. )
  3806. attempts = state["protocol_correction_attempts"]
  3807. if blocked and attempts < 2:
  3808. state["protocol_correction_attempts"] = attempts + 1
  3809. elif blocked and missing_report:
  3810. report = protocol_error_report(
  3811. trace_id,
  3812. "No valid TaskReport after two correction attempts",
  3813. )
  3814. state["task_report"] = report.model_dump()
  3815. state["task_report_submitted_at_sequence"] = sequence
  3816. state["task_report_progress_revision"] = state.get(
  3817. "task_progress_head_revision"
  3818. )
  3819. return {
  3820. "missing_report": missing_report,
  3821. "pending_reviews": pending_reviews,
  3822. "pending_actions": pending_actions,
  3823. "progress_error": progress_error,
  3824. "attempts": attempts,
  3825. "blocked": blocked,
  3826. }
  3827. gate = await self.task_protocol_service.mutate_state(
  3828. trace_id,
  3829. apply_completion_gate,
  3830. )
  3831. missing_report = gate["missing_report"]
  3832. pending_reviews = gate["pending_reviews"]
  3833. pending_actions = gate["pending_actions"]
  3834. progress_error = gate["progress_error"]
  3835. if (
  3836. gate["blocked"]
  3837. ):
  3838. attempts = gate["attempts"]
  3839. if attempts < 2:
  3840. if pending_reviews:
  3841. required_action = (
  3842. "review every pending child report with "
  3843. "review_task_result"
  3844. )
  3845. elif pending_actions:
  3846. required_action = (
  3847. "execute the approved next action with agent"
  3848. )
  3849. elif missing_report:
  3850. required_action = (
  3851. "submit a valid TaskReport with submit_task_report"
  3852. )
  3853. else:
  3854. required_action = (
  3855. "update TaskProgress to a ready_to_submit snapshot "
  3856. f"({progress_error})"
  3857. )
  3858. history.append({
  3859. "role": "user",
  3860. "content": (
  3861. "Protocol gate: you cannot finish yet. You must "
  3862. f"{required_action}."
  3863. ),
  3864. })
  3865. continue
  3866. completion_status = "failed"
  3867. await self.trace_store.update_trace(
  3868. trace_id,
  3869. error_message="Recursive task protocol gate failed",
  3870. )
  3871. break
  3872. # 检查是否有待评估的知识
  3873. if not side_branch_ctx and self.trace_store:
  3874. pending = await self.trace_store.get_pending_knowledge_entries(trace_id)
  3875. if pending:
  3876. self.log.info(f"任务即将结束,但仍有 {len(pending)} 条知识未评估,强制触发评估")
  3877. config.force_side_branch = ["knowledge_eval"]
  3878. trace = await self.trace_store.get_trace(trace_id)
  3879. if trace:
  3880. trace.context["knowledge_eval_trigger"] = "task_completion"
  3881. await self.trace_store.update_trace(trace_id, context=trace.context)
  3882. continue
  3883. if not side_branch_ctx and config.knowledge.enable_completion_extraction and not break_after_side_branch:
  3884. config.force_side_branch = ["reflection"]
  3885. break_after_side_branch = True
  3886. self.log.info("任务完成,进入完成后反思侧分支")
  3887. continue
  3888. if (
  3889. not side_branch_ctx
  3890. and self.trace_store
  3891. and policy_from_context(trace.context).requires_task_protocol
  3892. and not trace.parent_trace_id
  3893. ):
  3894. state = ensure_task_protocol(trace.context)
  3895. if (
  3896. state["root_validation_attempts"] >= 2
  3897. ):
  3898. raise ValueError(
  3899. "Root task already used its two independent validation "
  3900. "attempts; create a new trace"
  3901. )
  3902. # The Validator must read the exact persisted main path that
  3903. # produced this candidate, including real Tool Results.
  3904. await self.trace_store.update_trace(
  3905. trace_id,
  3906. head_sequence=head_seq,
  3907. )
  3908. trace.head_sequence = head_seq
  3909. validation_run = await self.validate_recursive_trace(
  3910. trace_id,
  3911. scope="root",
  3912. completion_criteria=require_root_task_anchor(
  3913. trace.context
  3914. ).completion_criteria,
  3915. candidate_output=response_content,
  3916. root_validator=True,
  3917. )
  3918. assert self.task_protocol_service is not None
  3919. def record_root_validation(fresh, state):
  3920. validation_cache = state.get("task_report_validation") or {}
  3921. validation_record = {
  3922. **validation_run.result.model_dump(),
  3923. "validation_plan": validation_cache.get("validation_plan"),
  3924. "validated_at_sequence": head_seq,
  3925. }
  3926. state["root_validation_history"].append(validation_record)
  3927. state["root_validation_attempts"] += 1
  3928. state["root_validation_passed"] = (
  3929. validation_run.result.outcome == "passed"
  3930. )
  3931. return (
  3932. state["root_validation_passed"],
  3933. state["root_validation_attempts"],
  3934. fresh,
  3935. )
  3936. root_passed, root_attempts, trace = (
  3937. await self.task_protocol_service.mutate_state(
  3938. trace_id,
  3939. record_root_validation,
  3940. )
  3941. )
  3942. if not root_passed:
  3943. if root_attempts < 2:
  3944. validation_feedback = {
  3945. "role": "user",
  3946. "content": (
  3947. "Root validation did not pass. Revise the current "
  3948. "answer using this framework-owned ValidationResult:\n"
  3949. + json.dumps(
  3950. validation_run.result.model_dump(),
  3951. ensure_ascii=False,
  3952. )
  3953. ),
  3954. }
  3955. history.append(validation_feedback)
  3956. feedback_msg = Message.from_llm_dict(
  3957. validation_feedback,
  3958. trace_id=trace_id,
  3959. sequence=sequence,
  3960. goal_id=(goal_tree.current_id if goal_tree else None),
  3961. parent_sequence=head_seq,
  3962. )
  3963. await self.trace_store.add_message(feedback_msg)
  3964. head_seq = sequence
  3965. sequence += 1
  3966. await self.trace_store.update_trace(
  3967. trace_id,
  3968. head_sequence=head_seq,
  3969. )
  3970. trace.head_sequence = head_seq
  3971. if goal_tree and goal_tree.current_id:
  3972. goal = goal_tree.find(goal_tree.current_id)
  3973. if goal:
  3974. goal.status = "in_progress"
  3975. await self.trace_store.update_goal_tree(
  3976. trace_id,
  3977. goal_tree,
  3978. )
  3979. continue
  3980. completion_status = "failed"
  3981. await self.trace_store.update_trace(
  3982. trace_id,
  3983. error_message="Root task did not pass independent validation",
  3984. )
  3985. break
  3986. if (
  3987. policy_from_context(trace.context).mode is AgentMode.RECURSIVE
  3988. and self.is_cancel_requested(trace_id)
  3989. ):
  3990. trace_obj = await self._mark_trace_stopped(trace_id, head_seq)
  3991. if trace_obj:
  3992. yield trace_obj
  3993. return
  3994. # max_iterations 等非正常退出也必须经过同一完成门禁。
  3995. if self.trace_store:
  3996. fresh_trace = await self.trace_store.get_trace(trace_id)
  3997. if fresh_trace:
  3998. trace = fresh_trace
  3999. policy = policy_from_context(trace.context)
  4000. if policy.requires_task_protocol:
  4001. assert self.task_protocol_service is not None
  4002. def close_incomplete_protocol(fresh, state):
  4003. missing_report = bool(
  4004. fresh.parent_trace_id
  4005. and state.get("task_report") is None
  4006. )
  4007. if missing_report:
  4008. report = protocol_error_report(
  4009. trace_id,
  4010. "Agent loop ended without a valid TaskReport",
  4011. )
  4012. state["task_report"] = report.model_dump()
  4013. state["task_report_submitted_at_sequence"] = sequence
  4014. state["task_report_progress_revision"] = state.get(
  4015. "task_progress_head_revision"
  4016. )
  4017. return {
  4018. "missing_report": missing_report,
  4019. "pending_reviews": bool(state["pending_reviews"]),
  4020. "pending_actions": bool(state["next_actions"]),
  4021. "root_validation_passed": bool(
  4022. state.get("root_validation_passed")
  4023. ),
  4024. "is_root": not fresh.parent_trace_id,
  4025. }
  4026. closed = await self.task_protocol_service.mutate_state(
  4027. trace_id,
  4028. close_incomplete_protocol,
  4029. )
  4030. if closed["missing_report"]:
  4031. completion_status = "failed"
  4032. await self.trace_store.update_trace(
  4033. trace_id,
  4034. error_message="Recursive task protocol gate failed",
  4035. )
  4036. if closed["pending_reviews"]:
  4037. completion_status = "failed"
  4038. if closed["pending_actions"]:
  4039. completion_status = "failed"
  4040. if (
  4041. closed["is_root"]
  4042. and not closed["root_validation_passed"]
  4043. ):
  4044. completion_status = "failed"
  4045. # 清理 trace 相关的跟踪数据
  4046. self._context_warned.pop(trace_id, None)
  4047. self._context_usage.pop(trace_id, None)
  4048. self._saved_knowledge_ids.pop(trace_id, None)
  4049. # 更新 head_sequence 并完成 Trace
  4050. if self.trace_store:
  4051. await self.trace_store.update_trace(
  4052. trace_id,
  4053. status=completion_status,
  4054. head_sequence=head_seq,
  4055. completed_at=datetime.now(),
  4056. )
  4057. if self.event_service is not None:
  4058. await self.event_service.try_pump(
  4059. trace.context.get("root_trace_id") or trace_id
  4060. )
  4061. trace_obj = await self.trace_store.get_trace(trace_id)
  4062. if trace_obj:
  4063. yield trace_obj
  4064. # ===== 压缩辅助方法 =====
  4065. def _rebuild_history_after_compression(
  4066. self,
  4067. history: List[Dict],
  4068. summary_msg_dict: Dict,
  4069. label: str = "压缩",
  4070. ) -> List[Dict]:
  4071. """
  4072. 压缩后重建 history:system prompt + 第一条 user message + summary
  4073. Args:
  4074. history: 压缩前的 history
  4075. summary_msg_dict: summary 消息的 LLM dict
  4076. label: 日志标签
  4077. Returns:
  4078. 新的 history
  4079. """
  4080. system_msg = None
  4081. first_user_msg = None
  4082. for msg in history:
  4083. if msg.get("role") == "system" and not system_msg:
  4084. system_msg = msg
  4085. elif msg.get("role") == "user" and not first_user_msg:
  4086. first_user_msg = msg
  4087. if system_msg and first_user_msg:
  4088. break
  4089. new_history = []
  4090. if system_msg:
  4091. new_history.append(system_msg)
  4092. if first_user_msg:
  4093. new_history.append(first_user_msg)
  4094. new_history.append(summary_msg_dict)
  4095. self.log.info(f"{label}完成: {len(history)} → {len(new_history)} 条消息")
  4096. for idx, msg in enumerate(new_history):
  4097. role = msg.get("role", "unknown")
  4098. content = msg.get("content", "")
  4099. if isinstance(content, str):
  4100. preview = content
  4101. elif isinstance(content, list):
  4102. preview = f"[{len(content)} blocks]"
  4103. else:
  4104. preview = str(content)
  4105. self.log.info(f" {label}后[{idx}] {role}: {preview}")
  4106. return new_history
  4107. # ===== 回溯(Rewind)=====
  4108. async def _rewind(
  4109. self,
  4110. trace_id: str,
  4111. after_sequence: int,
  4112. goal_tree: Optional[GoalTree],
  4113. ) -> int:
  4114. """
  4115. 回溯 Trace:快照 GoalTree,重建干净树并设置 ``head_sequence``。
  4116. ``_prepare_existing_trace`` 判定为回溯时调用;Recursive 同时重建待审核和待重规划状态。
  4117. Returns:
  4118. 下一个可用的 sequence 号
  4119. """
  4120. if not self.trace_store:
  4121. raise ValueError("trace_store required for rewind")
  4122. # 1. 加载所有 messages(用于 safe cutoff 和 max sequence)
  4123. all_messages = await self.trace_store.get_trace_messages(trace_id)
  4124. if not all_messages:
  4125. return 1
  4126. # 2. 找到安全截断点(确保不截断在 tool_call 和 tool response 之间)
  4127. cutoff = self._find_safe_cutoff(all_messages, after_sequence)
  4128. if self.candidate_service is not None:
  4129. await self.candidate_service.assert_rewind_allowed(trace_id, cutoff)
  4130. rewind_operation_id = (
  4131. f"rewind:{trace_id}:"
  4132. f"{max((item.sequence for item in all_messages), default=0)}:{cutoff}"
  4133. )
  4134. rewind_payload: dict[str, Any] = {
  4135. "operation_id": rewind_operation_id,
  4136. "after_sequence": cutoff,
  4137. "head_sequence": cutoff,
  4138. }
  4139. # 3. 快照并重建 GoalTree
  4140. if goal_tree:
  4141. # 获取截断点消息的 created_at 作为时间界限
  4142. cutoff_msg = None
  4143. for msg in all_messages:
  4144. if msg.sequence == cutoff:
  4145. cutoff_msg = msg
  4146. break
  4147. cutoff_time = cutoff_msg.created_at if cutoff_msg else datetime.now()
  4148. rewind_payload["goal_tree_snapshot"] = goal_tree.to_dict()
  4149. # 按时间重建干净的 GoalTree
  4150. new_tree = goal_tree.rebuild_for_rewind(cutoff_time)
  4151. await self.trace_store.update_goal_tree(trace_id, new_tree)
  4152. # 更新内存中的引用
  4153. goal_tree.goals = new_tree.goals
  4154. goal_tree.current_id = new_tree.current_id
  4155. trace = await self.trace_store.get_trace(trace_id)
  4156. projected_statuses: dict[str, str] = {}
  4157. if trace and policy_from_context(trace.context).requires_task_protocol:
  4158. projected_statuses = await self._rewind_protocol_state(
  4159. trace_id,
  4160. cutoff,
  4161. rewind_operation_id,
  4162. )
  4163. else:
  4164. await self.trace_store.update_trace(trace_id, head_sequence=cutoff)
  4165. if goal_tree and projected_statuses:
  4166. for goal_id, status in projected_statuses.items():
  4167. goal = goal_tree.find(goal_id)
  4168. if goal:
  4169. goal.status = status
  4170. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  4171. # 4. 协议状态与 head_sequence 已在同一临界区落盘
  4172. await self.trace_store.append_event(trace_id, "rewind", rewind_payload)
  4173. if self.event_service is not None:
  4174. await self.event_service.emit_after_commit(
  4175. source_trace_id=trace_id,
  4176. event_type="run.rewound",
  4177. event_key=f"run.rewound:{rewind_operation_id}",
  4178. effective_at_sequence=cutoff,
  4179. payload={"after_sequence": cutoff},
  4180. )
  4181. # 5. 返回 next sequence(全局递增,不复用)
  4182. max_seq = max((m.sequence for m in all_messages), default=0)
  4183. return max_seq + 1
  4184. async def _rewind_protocol_state(
  4185. self,
  4186. trace_id: str,
  4187. cutoff: int,
  4188. rewind_operation_id: str,
  4189. ) -> dict[str, str]:
  4190. """Rebuild protocol state and move the Trace head in one lock domain."""
  4191. assert self.trace_store is not None
  4192. assert self.task_protocol_service is not None
  4193. async with self.task_protocol_service.locked_trace(trace_id) as trace:
  4194. state = ensure_task_protocol(trace.context)
  4195. submitted_at = state.get("task_report_submitted_at_sequence")
  4196. if isinstance(submitted_at, int) and submitted_at > cutoff:
  4197. if state.get("task_report"):
  4198. state["report_history"].append(state["task_report"])
  4199. state["task_report"] = None
  4200. state["task_report_submitted_at_sequence"] = None
  4201. state["task_report_progress_revision"] = None
  4202. state["task_report_validation"] = None
  4203. validation_cache = state.get("task_report_validation")
  4204. if (
  4205. isinstance(validation_cache, dict)
  4206. and int(validation_cache.get("validated_at_sequence", 0) or 0)
  4207. > cutoff
  4208. ):
  4209. state["task_report_validation"] = None
  4210. state["pending_reviews"] = {
  4211. child_id: entry
  4212. for child_id, entry in state["pending_reviews"].items()
  4213. if entry.get("received_at_sequence", 0) <= cutoff
  4214. }
  4215. reverted_reviews = [
  4216. review
  4217. for review in state["reviews"]
  4218. if review.get("reviewed_at_sequence", 0) > cutoff
  4219. ]
  4220. for review in reverted_reviews:
  4221. entry = review.get("pending_review")
  4222. child_id = review.get("child_trace_id")
  4223. if (
  4224. child_id
  4225. and isinstance(entry, dict)
  4226. and entry.get("received_at_sequence", 0) <= cutoff
  4227. ):
  4228. state["pending_reviews"][child_id] = entry
  4229. state["reviews"] = [
  4230. review
  4231. for review in state["reviews"]
  4232. if review.get("reviewed_at_sequence", 0) <= cutoff
  4233. ]
  4234. state["next_actions"] = [
  4235. action
  4236. for action in state["next_actions"]
  4237. if action.get("created_at_sequence", 0) <= cutoff
  4238. ]
  4239. while (
  4240. state.get("task_brief") is not None
  4241. and state.get("task_brief_effective_at_sequence", 0) > cutoff
  4242. and state.get("task_brief_history")
  4243. ):
  4244. previous = state["task_brief_history"].pop()
  4245. state["task_brief"] = previous["task_brief"]
  4246. state["task_brief_version"] = previous["version"]
  4247. state["task_brief_effective_at_sequence"] = previous.get(
  4248. "effective_at_sequence",
  4249. 0,
  4250. )
  4251. if policy_from_context(trace.context).requires_task_progress:
  4252. rewind_task_progress(state, cutoff)
  4253. state["pending_replans"] = rebuild_pending_replans(state)
  4254. root_history = [
  4255. item
  4256. for item in state.get("root_validation_history", [])
  4257. if int(
  4258. item.get(
  4259. "validated_at_sequence",
  4260. item.get("evaluated_at_sequence", 0),
  4261. )
  4262. or 0
  4263. )
  4264. <= cutoff
  4265. ]
  4266. state["root_validation_history"] = root_history
  4267. state["root_validation_attempts"] = len(root_history)
  4268. state["root_validation_passed"] = bool(
  4269. root_history and root_history[-1].get("outcome") == "passed"
  4270. )
  4271. state["protocol_correction_attempts"] = 0
  4272. prune_context_access(trace.context, cutoff)
  4273. operations = trace.context.setdefault("run_event_operations", [])
  4274. if not any(
  4275. item.get("operation_id") == rewind_operation_id
  4276. for item in operations
  4277. if isinstance(item, dict)
  4278. ):
  4279. operations.append({
  4280. "operation_id": rewind_operation_id,
  4281. "event_type": "run.rewound",
  4282. "effective_at_sequence": cutoff,
  4283. "payload": {"after_sequence": cutoff},
  4284. })
  4285. del operations[:-64]
  4286. await self.trace_store.update_trace(
  4287. trace_id,
  4288. context=trace.context,
  4289. head_sequence=cutoff,
  4290. )
  4291. projected_statuses: dict[str, str] = {}
  4292. for entry in state["pending_reviews"].values():
  4293. if entry.get("goal_id"):
  4294. projected_statuses[entry["goal_id"]] = "pending_review"
  4295. for action in state["next_actions"]:
  4296. goal_id = action.get("goal_id")
  4297. if goal_id and goal_id not in projected_statuses:
  4298. projected_statuses[goal_id] = "in_progress"
  4299. return projected_statuses
  4300. def _find_safe_cutoff(self, messages: List[Message], after_sequence: int) -> int:
  4301. """
  4302. 找到安全的截断点。
  4303. 如果 after_sequence 指向一条带 tool_calls 的 assistant message,
  4304. 则自动扩展到其所有对应的 tool response 之后。
  4305. """
  4306. cutoff = after_sequence
  4307. # 找到 after_sequence 对应的 message
  4308. target_msg = None
  4309. for msg in messages:
  4310. if msg.sequence == after_sequence:
  4311. target_msg = msg
  4312. break
  4313. if not target_msg:
  4314. return cutoff
  4315. # 如果是 assistant 且有 tool_calls,找到所有对应的 tool responses
  4316. if target_msg.role == "assistant":
  4317. content = target_msg.content
  4318. if isinstance(content, dict) and content.get("tool_calls"):
  4319. tool_call_ids = set()
  4320. for tc in content["tool_calls"]:
  4321. if isinstance(tc, dict) and tc.get("id"):
  4322. tool_call_ids.add(tc["id"])
  4323. # 找到这些 tool_call 对应的 tool messages
  4324. for msg in messages:
  4325. if (msg.role == "tool" and msg.tool_call_id
  4326. and msg.tool_call_id in tool_call_ids):
  4327. cutoff = max(cutoff, msg.sequence)
  4328. return cutoff
  4329. async def _heal_orphaned_tool_calls(
  4330. self,
  4331. messages: List[Message],
  4332. trace_id: str,
  4333. goal_tree: Optional[GoalTree],
  4334. sequence: int,
  4335. ) -> tuple:
  4336. """
  4337. 检测并修复消息历史中的 orphaned tool_calls。
  4338. 当 agent 被 stop/crash 中断时,可能有 assistant 的 tool_calls 没有对应的
  4339. tool results(包括多 tool_call 部分完成的情况)。直接发给 LLM 会导致 400。
  4340. 修复策略:为每个缺失的 tool_result 插入合成的"中断通知"消息,而非裁剪。
  4341. - 普通工具:简短中断提示
  4342. - agent/evaluate:包含 sub_trace_id、执行统计、continue_from 指引
  4343. 合成消息持久化到 store,确保幂等(下次续跑不再触发)。
  4344. Returns:
  4345. (healed_messages, next_sequence)
  4346. """
  4347. if not messages:
  4348. return messages, sequence
  4349. # 收集所有 tool_call IDs → (assistant_msg, tool_call_dict)
  4350. tc_map: Dict[str, tuple] = {}
  4351. result_ids: set = set()
  4352. for msg in messages:
  4353. if msg.role == "assistant":
  4354. content = msg.content
  4355. if isinstance(content, dict) and content.get("tool_calls"):
  4356. for tc in content["tool_calls"]:
  4357. tc_id = tc.get("id")
  4358. if tc_id:
  4359. tc_map[tc_id] = (msg, tc)
  4360. elif msg.role == "tool" and msg.tool_call_id:
  4361. result_ids.add(msg.tool_call_id)
  4362. orphaned_ids = [tc_id for tc_id in tc_map if tc_id not in result_ids]
  4363. if not orphaned_ids:
  4364. return messages, sequence
  4365. self.log.info(
  4366. "检测到 %d 个 orphaned tool_calls,生成合成中断通知",
  4367. len(orphaned_ids),
  4368. )
  4369. healed = list(messages)
  4370. head_seq = messages[-1].sequence
  4371. for tc_id in orphaned_ids:
  4372. assistant_msg, tc = tc_map[tc_id]
  4373. tool_name = tc.get("function", {}).get("name", "unknown")
  4374. if tool_name in ("agent", "evaluate"):
  4375. result_text = self._build_agent_interrupted_result(
  4376. tc, goal_tree, assistant_msg,
  4377. )
  4378. else:
  4379. result_text = build_tool_interrupted_message(tool_name)
  4380. synthetic_msg = Message.create(
  4381. trace_id=trace_id,
  4382. role="tool",
  4383. sequence=sequence,
  4384. goal_id=assistant_msg.goal_id,
  4385. parent_sequence=head_seq,
  4386. tool_call_id=tc_id,
  4387. content={"tool_name": tool_name, "result": result_text},
  4388. )
  4389. if self.trace_store:
  4390. await self.trace_store.add_message(synthetic_msg)
  4391. healed.append(synthetic_msg)
  4392. head_seq = sequence
  4393. sequence += 1
  4394. # 更新 trace head/last sequence
  4395. if self.trace_store:
  4396. await self.trace_store.update_trace(
  4397. trace_id,
  4398. head_sequence=head_seq,
  4399. last_sequence=max(head_seq, sequence - 1),
  4400. )
  4401. return healed, sequence
  4402. def _build_agent_interrupted_result(
  4403. self,
  4404. tc: Dict,
  4405. goal_tree: Optional[GoalTree],
  4406. assistant_msg: Message,
  4407. ) -> str:
  4408. """为中断的 agent/evaluate 工具调用构建合成结果(对齐正常返回值格式)"""
  4409. args_str = tc.get("function", {}).get("arguments", "{}")
  4410. try:
  4411. args = json.loads(args_str) if isinstance(args_str, str) else args_str
  4412. except json.JSONDecodeError:
  4413. args = {}
  4414. task = args.get("task", "未知任务")
  4415. if isinstance(task, list):
  4416. task = "; ".join(task)
  4417. tool_name = tc.get("function", {}).get("name", "agent")
  4418. mode = "evaluate" if tool_name == "evaluate" else "delegate"
  4419. # 从 goal_tree 查找 sub_trace 信息
  4420. sub_trace_id = None
  4421. stats = None
  4422. if goal_tree and assistant_msg.goal_id:
  4423. goal = goal_tree.find(assistant_msg.goal_id)
  4424. if goal and goal.sub_trace_ids:
  4425. first = goal.sub_trace_ids[0]
  4426. if isinstance(first, dict):
  4427. sub_trace_id = first.get("trace_id")
  4428. elif isinstance(first, str):
  4429. sub_trace_id = first
  4430. if goal.cumulative_stats:
  4431. s = goal.cumulative_stats
  4432. if s.message_count > 0:
  4433. stats = {
  4434. "message_count": s.message_count,
  4435. "total_tokens": s.total_tokens,
  4436. "total_cost": round(s.total_cost, 4),
  4437. }
  4438. result: Dict[str, Any] = {
  4439. "mode": mode,
  4440. "status": "interrupted",
  4441. "summary": AGENT_INTERRUPTED_SUMMARY,
  4442. "task": task,
  4443. }
  4444. if sub_trace_id:
  4445. result["sub_trace_id"] = sub_trace_id
  4446. result["hint"] = build_agent_continue_hint(sub_trace_id)
  4447. if stats:
  4448. result["stats"] = stats
  4449. return json.dumps(result, ensure_ascii=False, indent=2)
  4450. # ===== 上下文注入 =====
  4451. # ===== Skill 指定注入 =====
  4452. def _check_skills_need_injection(
  4453. self,
  4454. trace: Trace,
  4455. inject_skills: List[str],
  4456. history: List[Dict],
  4457. recency_threshold: int,
  4458. ) -> List[str]:
  4459. """
  4460. 检查哪些 skill 需要注入。
  4461. 通过 trace.context["injected_skills"] 中记录的 message_id
  4462. 检查是否仍在当前 history 的最近 recency_threshold 条消息中。
  4463. Returns:
  4464. 需要注入的 skill 名称列表
  4465. """
  4466. injected = (trace.context or {}).get("injected_skills", {})
  4467. # 收集 history 中最近 recency_threshold 条消息的 message_id
  4468. recent_msgs = history[-recency_threshold:] if recency_threshold > 0 else []
  4469. recent_ids = set()
  4470. for msg in recent_msgs:
  4471. mid = msg.get("message_id") or msg.get("_message_id")
  4472. if mid:
  4473. recent_ids.add(mid)
  4474. needs_inject = []
  4475. for skill_name in inject_skills:
  4476. record = injected.get(skill_name)
  4477. if not record:
  4478. needs_inject.append(skill_name)
  4479. continue
  4480. if record.get("message_id") not in recent_ids:
  4481. needs_inject.append(skill_name)
  4482. return needs_inject
  4483. async def _update_skill_injection_record(
  4484. self,
  4485. trace_id: str,
  4486. trace: Trace,
  4487. skill_name: str,
  4488. message_id: str,
  4489. sequence: int,
  4490. ):
  4491. """更新 trace.context 中的 skill 注入记录"""
  4492. if not trace.context:
  4493. trace.context = {}
  4494. if "injected_skills" not in trace.context:
  4495. trace.context["injected_skills"] = {}
  4496. trace.context["injected_skills"][skill_name] = {
  4497. "message_id": message_id,
  4498. "sequence": sequence,
  4499. }
  4500. if self.trace_store:
  4501. await self.trace_store.update_trace(trace_id, context=trace.context)
  4502. # ===== 上下文注入 =====
  4503. def _build_context_injection(
  4504. self,
  4505. trace: Trace,
  4506. goal_tree: Optional[GoalTree],
  4507. ) -> str:
  4508. """构建周期性注入的上下文(GoalTree + Active Collaborators + Focus 提醒 + IM 消息通知)"""
  4509. from datetime import datetime
  4510. parts = [f"## Current Time\n\n{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"]
  4511. if trace and policy_from_context(trace.context).requires_task_protocol:
  4512. parts.append(render_recursive_context(trace.context))
  4513. # GoalTree
  4514. if goal_tree and goal_tree.goals:
  4515. parts.append(f"## Current Plan\n\n{goal_tree.to_prompt()}")
  4516. if goal_tree.current_id:
  4517. # 检测 focus 在有子节点的父目标上:提醒模型 focus 到具体子目标
  4518. children = goal_tree.get_children(goal_tree.current_id)
  4519. pending_children = [c for c in children if c.status in ("pending", "in_progress")]
  4520. if pending_children:
  4521. child_ids = ", ".join(
  4522. goal_tree._generate_display_id(c) for c in pending_children[:3]
  4523. )
  4524. parts.append(
  4525. f"**提醒**:当前焦点在父目标上,建议用 `goal(focus=\"...\")` "
  4526. f"切换到具体子目标(如 {child_ids})再执行。"
  4527. )
  4528. else:
  4529. # 无焦点:提醒模型 focus
  4530. parts.append(
  4531. "**提醒**:当前没有焦点目标。请用 `goal(focus=\"...\")` 选择一个目标开始执行。"
  4532. )
  4533. # Active Collaborators
  4534. collaborators = trace.context.get("collaborators", [])
  4535. if collaborators:
  4536. lines = ["## Active Collaborators"]
  4537. for c in collaborators:
  4538. status_str = c.get("status", "unknown")
  4539. ctype = c.get("type", "agent")
  4540. summary = c.get("summary", "")
  4541. name = c.get("name", "unnamed")
  4542. lines.append(f"- {name} [{ctype}, {status_str}]: {summary}")
  4543. parts.append("\n".join(lines))
  4544. # IM 消息通知(Research Agent)
  4545. im_config = trace.context.get("im_config")
  4546. if im_config:
  4547. contact_id = im_config.get("contact_id")
  4548. chat_id = im_config.get("chat_id")
  4549. if contact_id and chat_id:
  4550. # 尝试导入 IM 模块并检查通知
  4551. try:
  4552. from cyber_agent.tools.builtin.im import chat as im_chat
  4553. notification = im_chat._notifications.get((contact_id, chat_id))
  4554. if notification:
  4555. count = notification.get("count", 0)
  4556. senders = notification.get("from", [])
  4557. senders_str = ", ".join(senders)
  4558. parts.append(
  4559. f"## IM 消息通知\n\n"
  4560. f"你有 {count} 条新消息,来自: {senders_str}\n"
  4561. f"使用 `im_receive_messages(contact_id=\"{contact_id}\", chat_id=\"{chat_id}\")` 查看消息内容。"
  4562. )
  4563. else:
  4564. parts.append("## IM 消息通知\n\n暂无新消息")
  4565. except (ImportError, AttributeError):
  4566. # IM 模块未加载或不可用
  4567. pass
  4568. # Knowledge Manager 队列状态
  4569. km_queue_size = trace.context.get("km_queue_size")
  4570. if km_queue_size is not None:
  4571. current_sender = trace.context.get("current_sender", "unknown")
  4572. if km_queue_size > 0:
  4573. parts.append(
  4574. f"## 消息队列状态\n\n"
  4575. f"当前处理: {current_sender} 的消息\n"
  4576. f"队列中还有 {km_queue_size} 条待处理消息"
  4577. )
  4578. else:
  4579. parts.append(
  4580. f"## 消息队列状态\n\n"
  4581. f"当前处理: {current_sender} 的消息\n"
  4582. f"队列为空,处理完本条消息后将进入休眠"
  4583. )
  4584. return "\n\n".join(parts)
  4585. # ===== 辅助方法 =====
  4586. async def _optimize_images(
  4587. self,
  4588. messages: List[Dict],
  4589. model: str,
  4590. *,
  4591. trace_id: Optional[str] = None,
  4592. ) -> List[Dict]:
  4593. """
  4594. 分级优化已处理的图片,节省 token
  4595. 策略(基于图片距离最后一条 assistant 的"轮次"):
  4596. 1. 最近 1-2 轮:保留原图
  4597. 2. 3-5 轮:降低分辨率和压缩(节省 token 但保留视觉信息)
  4598. 3. 5 轮以上:调用小模型生成文本描述 + 保留 URL
  4599. 处理结果会缓存,避免重复的 PIL 解码/编码和 LLM 调用。
  4600. Args:
  4601. messages: 原始消息列表
  4602. model: 当前使用的模型(用于选择描述生成模型)
  4603. Returns:
  4604. 优化后的消息列表(深拷贝)
  4605. """
  4606. if not messages:
  4607. return messages
  4608. # 找到最后一条 assistant message 的位置
  4609. last_assistant_idx = -1
  4610. for i in range(len(messages) - 1, -1, -1):
  4611. if messages[i].get("role") == "assistant":
  4612. last_assistant_idx = i
  4613. break
  4614. # 如果没有 assistant message,说明还没开始对话,不优化
  4615. if last_assistant_idx == -1:
  4616. return messages
  4617. # 统计从每个位置到最后一条 assistant 之间的 assistant 数量(作为"轮次")
  4618. assistant_count_after = [0] * len(messages)
  4619. count = 0
  4620. for i in range(len(messages) - 1, -1, -1):
  4621. assistant_count_after[i] = count
  4622. if messages[i].get("role") == "assistant":
  4623. count += 1
  4624. # 深拷贝避免修改原始数据
  4625. import copy
  4626. import hashlib
  4627. import asyncio
  4628. import base64 as b64mod
  4629. import httpx
  4630. import mimetypes
  4631. messages = copy.deepcopy(messages)
  4632. # 预处理:将所有 HTTP(S) URL 图片下载并转为 base64 data URL
  4633. # Qwen API 无法访问外部签名 URL(如 BFL、火山引擎 TOS),必须在本地转换
  4634. url_download_jobs = [] # [(msg_idx, block_idx, url)]
  4635. for i, msg in enumerate(messages):
  4636. if msg.get("role") != "tool":
  4637. continue
  4638. content = msg.get("content")
  4639. if not isinstance(content, list):
  4640. continue
  4641. for block_idx, block in enumerate(content):
  4642. if isinstance(block, dict) and block.get("type") == "image_url":
  4643. url = block.get("image_url", {}).get("url", "")
  4644. if url.startswith(("http://", "https://")):
  4645. url_download_jobs.append((i, block_idx, url))
  4646. if url_download_jobs:
  4647. async def _download_image_to_data_url(url: str) -> str | None:
  4648. try:
  4649. async with httpx.AsyncClient(timeout=60, trust_env=False) as client:
  4650. resp = await client.get(url)
  4651. resp.raise_for_status()
  4652. ct = resp.headers.get("content-type", "").split(";")[0].strip()
  4653. if not ct.startswith("image/"):
  4654. ct = mimetypes.guess_type(url.split("?")[0])[0] or "image/png"
  4655. b64 = b64mod.b64encode(resp.content).decode()
  4656. return f"data:{ct};base64,{b64}"
  4657. except Exception:
  4658. return None
  4659. results = await asyncio.gather(
  4660. *[_download_image_to_data_url(url) for _, _, url in url_download_jobs],
  4661. return_exceptions=True
  4662. )
  4663. converted = 0
  4664. for (msg_idx, block_idx, original_url), result in zip(url_download_jobs, results):
  4665. if isinstance(result, str) and result.startswith("data:"):
  4666. messages[msg_idx]["content"][block_idx]["image_url"]["url"] = result
  4667. converted += 1
  4668. if converted:
  4669. self.log.info(f"[Image Optimization] URL→base64 预转换: {converted}/{len(url_download_jobs)} 张")
  4670. # 统计优化情况
  4671. stats = {"kept": 0, "downscaled": 0, "described": 0, "cache_hit": 0}
  4672. # 收集需要降分辨率或尺寸补齐的图片(用于并发处理)
  4673. process_jobs = [] # [(msg_idx, block_idx, image_url, cache_key, max_size, cache_field)]
  4674. # 第一遍:扫描并收集需要处理的图片
  4675. for i in range(last_assistant_idx):
  4676. msg = messages[i]
  4677. if msg.get("role") != "tool":
  4678. continue
  4679. content = msg.get("content")
  4680. if not isinstance(content, list):
  4681. continue
  4682. rounds_ago = assistant_count_after[i]
  4683. for block_idx, block in enumerate(content):
  4684. if isinstance(block, dict) and block.get("type") == "image_url":
  4685. image_url_obj = block.get("image_url", {})
  4686. image_url = image_url_obj.get("url", "")
  4687. if image_url.startswith("data:"):
  4688. cache_key = hashlib.md5(image_url[:200].encode()).hexdigest()
  4689. else:
  4690. cache_key = hashlib.md5(image_url.encode()).hexdigest()
  4691. # 1-5 轮都需要检查尺寸
  4692. if rounds_ago <= 5:
  4693. cached = self._image_opt_cache.get(cache_key, {})
  4694. cache_field = "pad_only" if rounds_ago <= 2 else "downscaled"
  4695. if cache_field not in cached and image_url.startswith("data:"):
  4696. max_size = None if rounds_ago <= 2 else 512
  4697. process_jobs.append((i, block_idx, image_url, cache_key, max_size, cache_field))
  4698. # 并发处理所有尺寸任务
  4699. if process_jobs:
  4700. process_results = await asyncio.gather(
  4701. *[self._process_image_size(url, max_size=ms) for _, _, url, _, ms, _ in process_jobs],
  4702. return_exceptions=True
  4703. )
  4704. for (_, _, _, cache_key, _, cache_field), result in zip(process_jobs, process_results):
  4705. if not isinstance(result, Exception) and result is not None:
  4706. self._image_opt_cache.setdefault(cache_key, {})[cache_field] = result
  4707. # 第二遍:应用处理结果
  4708. for i in range(last_assistant_idx):
  4709. msg = messages[i]
  4710. if msg.get("role") != "tool":
  4711. continue
  4712. content = msg.get("content")
  4713. if not isinstance(content, list):
  4714. continue
  4715. # 计算这条消息距离最后一条 assistant 的"轮次"
  4716. rounds_ago = assistant_count_after[i]
  4717. # 处理每个 content block
  4718. new_content = []
  4719. for block in content:
  4720. if isinstance(block, dict) and block.get("type") == "image_url":
  4721. image_url_obj = block.get("image_url", {})
  4722. image_url = image_url_obj.get("url", "")
  4723. # 生成缓存 key(URL 图片用 URL 本身,base64 用前 64 字符 hash)
  4724. if image_url.startswith("data:"):
  4725. cache_key = hashlib.md5(image_url[:200].encode()).hexdigest()
  4726. else:
  4727. cache_key = hashlib.md5(image_url.encode()).hexdigest()
  4728. # 根据距离决定处理策略
  4729. if rounds_ago <= 2:
  4730. # 最近 1-2 轮:只补齐过小图片,保留原分辨率
  4731. cached = self._image_opt_cache.get(cache_key, {})
  4732. if "pad_only" in cached:
  4733. new_content.append({
  4734. "type": "image_url",
  4735. "image_url": {"url": cached["pad_only"]}
  4736. })
  4737. stats["kept"] += 1
  4738. stats["cache_hit"] += 1
  4739. elif image_url.startswith("data:"):
  4740. processed = await self._process_image_size(image_url, max_size=None)
  4741. if processed:
  4742. self._image_opt_cache.setdefault(cache_key, {})["pad_only"] = processed
  4743. new_content.append({
  4744. "type": "image_url",
  4745. "image_url": {"url": processed}
  4746. })
  4747. else:
  4748. new_content.append(block)
  4749. stats["kept"] += 1
  4750. else:
  4751. new_content.append(block)
  4752. stats["kept"] += 1
  4753. elif rounds_ago <= 5:
  4754. # 3-5 轮:降低分辨率(优先从缓存取)
  4755. cached = self._image_opt_cache.get(cache_key, {})
  4756. if "downscaled" in cached:
  4757. new_content.append({
  4758. "type": "image_url",
  4759. "image_url": {"url": cached["downscaled"]}
  4760. })
  4761. stats["downscaled"] += 1
  4762. stats["cache_hit"] += 1
  4763. elif image_url.startswith("data:"):
  4764. processed = await self._process_image_size(image_url, max_size=512)
  4765. if processed:
  4766. # 缓存结果
  4767. self._image_opt_cache.setdefault(cache_key, {})["downscaled"] = processed
  4768. new_content.append({
  4769. "type": "image_url",
  4770. "image_url": {"url": processed}
  4771. })
  4772. stats["downscaled"] += 1
  4773. else:
  4774. new_content.append(block)
  4775. stats["kept"] += 1
  4776. else:
  4777. # URL 图片:无法直接处理,保留原图
  4778. new_content.append(block)
  4779. stats["kept"] += 1
  4780. else:
  4781. # 5 轮以上:生成文本描述(优先从缓存取)
  4782. cached = self._image_opt_cache.get(cache_key, {})
  4783. if "description" in cached:
  4784. new_content.append(cached["description"])
  4785. stats["described"] += 1
  4786. stats["cache_hit"] += 1
  4787. else:
  4788. description = await self._generate_image_description(
  4789. image_url,
  4790. model,
  4791. trace_id=trace_id,
  4792. )
  4793. url_info = f" (URL: {image_url[:100]}...)" if not image_url.startswith("data:") else ""
  4794. desc_block = {
  4795. "type": "text",
  4796. "text": f"[Image description: {description}]{url_info}"
  4797. }
  4798. # 缓存结果
  4799. self._image_opt_cache.setdefault(cache_key, {})["description"] = desc_block
  4800. new_content.append(desc_block)
  4801. stats["described"] += 1
  4802. else:
  4803. new_content.append(block)
  4804. msg["content"] = new_content
  4805. # print(f"[Image Opt Check] 扫描到 {stats['kept'] + stats['downscaled'] + stats['described']} 张图片上下文")
  4806. if stats["downscaled"] > 0 or stats["described"] > 0:
  4807. self.log.info(
  4808. f"[Image Optimization] 保留 {stats['kept']} 张,"
  4809. f"降分辨率 {stats['downscaled']} 张,"
  4810. f"文本描述 {stats['described']} 张,"
  4811. f"缓存命中 {stats['cache_hit']} 次"
  4812. )
  4813. return messages
  4814. async def _process_image_size(self, base64_url: str, max_size: Optional[int] = 512, min_size: int = 11) -> Optional[str]:
  4815. """
  4816. 处理 base64 图片的尺寸:
  4817. - 若 max_size 不为 None 且大于该值,则等比例缩放
  4818. - 若任意一边小于 min_size,则补充白边 (Padding)
  4819. """
  4820. try:
  4821. from PIL import Image
  4822. import io
  4823. import base64
  4824. # 解析 base64 数据
  4825. if not base64_url.startswith("data:"):
  4826. return None
  4827. header, data = base64_url.split(",", 1)
  4828. media_type = header.split(";")[0].split(":")[1] # image/png
  4829. # 解码图片
  4830. img_data = base64.b64decode(data)
  4831. img = Image.open(io.BytesIO(img_data))
  4832. width, height = img.size
  4833. needs_downscale = max_size is not None and (width > max_size or height > max_size)
  4834. needs_pad = width < min_size or height < min_size
  4835. # 尺寸正常,无需处理
  4836. if not needs_downscale and not needs_pad:
  4837. return base64_url
  4838. new_width, new_height = width, height
  4839. # 1. 降分辨率
  4840. if needs_downscale:
  4841. if width > height:
  4842. new_width = max_size
  4843. new_height = int(height * max_size / width)
  4844. else:
  4845. new_height = max_size
  4846. new_width = int(width * max_size / height)
  4847. if (new_width, new_height) != (width, height):
  4848. img_resized = img.resize((new_width, new_height), Image.Resampling.BILINEAR)
  4849. else:
  4850. img_resized = img
  4851. # 2. 补齐白边 (Padding)
  4852. pad_width = max(new_width, min_size)
  4853. pad_height = max(new_height, min_size)
  4854. if pad_width > new_width or pad_height > new_height:
  4855. # 创建白色背景
  4856. padded_img = Image.new("RGBA" if img_resized.mode in ("RGBA", "P") else "RGB", (pad_width, pad_height), (255, 255, 255, 255))
  4857. offset_x = (pad_width - new_width) // 2
  4858. offset_y = (pad_height - new_height) // 2
  4859. padded_img.paste(img_resized, (offset_x, offset_y))
  4860. img_resized = padded_img
  4861. # 转换为 RGB(JPEG不支持 RGBA, P 等具有透明度或索引的模式)
  4862. if img_resized.mode != "RGB":
  4863. if img_resized.mode == "RGBA" or img_resized.mode == "P":
  4864. # Create a white background for transparent images
  4865. background = Image.new("RGB", img_resized.size, (255, 255, 255))
  4866. if img_resized.mode == "P" and "transparency" in img_resized.info:
  4867. img_resized = img_resized.convert("RGBA")
  4868. if img_resized.mode == "RGBA":
  4869. background.paste(img_resized, mask=img_resized.split()[3])
  4870. img_resized = background
  4871. img_resized = img_resized.convert("RGB")
  4872. # 重新编码为 JPEG(如果只是补齐没有缩放,可以稍微保留高点质量)
  4873. buffer = io.BytesIO()
  4874. quality = 60 if needs_downscale else 85
  4875. img_resized.save(buffer, format="JPEG", quality=quality, optimize=False)
  4876. new_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
  4877. return f"data:image/jpeg;base64,{new_data}"
  4878. except Exception as e:
  4879. self.log.warning(f"[Image Process] 处理图片尺寸失败: {e}")
  4880. return None
  4881. async def _generate_image_description(
  4882. self,
  4883. image_url: str,
  4884. current_model: str,
  4885. *,
  4886. trace_id: Optional[str] = None,
  4887. ) -> str:
  4888. """
  4889. 使用小模型生成图片的文本描述
  4890. Args:
  4891. image_url: 图片 URL(base64 或 http(s))
  4892. current_model: 当前使用的模型
  4893. Returns:
  4894. 图片描述文本
  4895. """
  4896. try:
  4897. # 使用 qwen-vl-max(通义千问视觉模型)生成描述
  4898. # 注意:qwen-vl 系列专门支持视觉输入
  4899. description_model = "qwen-vl-max"
  4900. # 构建描述请求
  4901. messages = [
  4902. {
  4903. "role": "user",
  4904. "content": [
  4905. {
  4906. "type": "image_url",
  4907. "image_url": {"url": image_url}
  4908. },
  4909. {
  4910. "type": "text",
  4911. "text": "请用 1-2 句话简洁描述这张图片的主要内容。"
  4912. }
  4913. ]
  4914. }
  4915. ]
  4916. # 调用 LLM
  4917. call_kwargs = {
  4918. "messages": messages,
  4919. "model": description_model,
  4920. "tools": None,
  4921. "temperature": 0.3,
  4922. }
  4923. result = (
  4924. await self.call_recursive_llm(
  4925. trace_id,
  4926. purpose="ordinary",
  4927. fail_on_post_response_exhaustion=True,
  4928. **call_kwargs,
  4929. )
  4930. if trace_id
  4931. else await self.llm_call(**call_kwargs)
  4932. )
  4933. description = result.get("content", "").strip()
  4934. return description if description else "图片内容"
  4935. except (ResourceBudgetExceeded, ResourceBudgetStateError):
  4936. raise
  4937. except Exception as e:
  4938. self.log.warning(f"[Image Description] 生成描述失败: {e}")
  4939. return "图片内容"
  4940. def _add_cache_control(
  4941. self,
  4942. messages: List[Dict],
  4943. model: str,
  4944. enable: bool
  4945. ) -> List[Dict]:
  4946. """
  4947. 为支持的模型添加 Prompt Caching 标记
  4948. 策略:固定位置 + 延迟缓存
  4949. 1. 如果有未处理的图片(最后一条 assistant 之后的 tool messages 中有图片),跳过缓存
  4950. 2. system message 添加缓存(如果足够长)
  4951. 3. 固定位置缓存点(20, 40, 60, 80),确保每个缓存点间隔 >= 1024 tokens
  4952. 4. 最多使用 4 个缓存点(含 system)
  4953. Args:
  4954. messages: 原始消息列表
  4955. model: 模型名称
  4956. enable: 是否启用缓存
  4957. Returns:
  4958. 添加了 cache_control 的消息列表(深拷贝)
  4959. """
  4960. if not enable:
  4961. return messages
  4962. # 只对 Claude 模型启用
  4963. if "claude" not in model.lower():
  4964. return messages
  4965. # 延迟缓存:检查是否有未处理的图片
  4966. last_assistant_idx = -1
  4967. for i in range(len(messages) - 1, -1, -1):
  4968. if messages[i].get("role") == "assistant":
  4969. last_assistant_idx = i
  4970. break
  4971. # 检查最后一条 assistant 之后是否有包含图片的 tool messages
  4972. has_unprocessed_images = False
  4973. if last_assistant_idx >= 0:
  4974. for i in range(last_assistant_idx + 1, len(messages)):
  4975. msg = messages[i]
  4976. if msg.get("role") == "tool":
  4977. content = msg.get("content")
  4978. if isinstance(content, list):
  4979. has_unprocessed_images = any(
  4980. isinstance(block, dict) and block.get("type") == "image_url"
  4981. for block in content
  4982. )
  4983. if has_unprocessed_images:
  4984. break
  4985. if has_unprocessed_images:
  4986. self.log.debug("[Cache] 检测到未处理的图片,延迟缓存建立")
  4987. return messages
  4988. # 深拷贝避免修改原始数据
  4989. import copy
  4990. messages = copy.deepcopy(messages)
  4991. # 策略 1: 为 system message 添加缓存
  4992. system_cached = False
  4993. for msg in messages:
  4994. if msg.get("role") == "system":
  4995. content = msg.get("content", "")
  4996. if isinstance(content, str) and len(content) > 1000:
  4997. msg["content"] = [{
  4998. "type": "text",
  4999. "text": content,
  5000. "cache_control": {"type": "ephemeral"}
  5001. }]
  5002. system_cached = True
  5003. self.log.debug(f"[Cache] 为 system message 添加缓存标记 (len={len(content)})")
  5004. break
  5005. # 策略 2: 固定位置缓存点
  5006. CACHE_INTERVAL = 20
  5007. MAX_POINTS = 3 if system_cached else 4
  5008. MIN_TOKENS = 1024
  5009. AVG_TOKENS_PER_MSG = 70
  5010. total_msgs = len(messages)
  5011. if total_msgs == 0:
  5012. return messages
  5013. cache_positions = []
  5014. last_cache_pos = 0
  5015. for i in range(1, MAX_POINTS + 1):
  5016. target_pos = i * CACHE_INTERVAL - 1 # 19, 39, 59, 79
  5017. if target_pos >= total_msgs:
  5018. break
  5019. # 从目标位置开始查找合适的 user/assistant 消息
  5020. for j in range(target_pos, total_msgs):
  5021. msg = messages[j]
  5022. if msg.get("role") not in ("user", "assistant"):
  5023. continue
  5024. content = msg.get("content", "")
  5025. if not content:
  5026. continue
  5027. # 检查 content 是否非空
  5028. is_valid = False
  5029. if isinstance(content, str):
  5030. is_valid = len(content) > 0
  5031. elif isinstance(content, list):
  5032. is_valid = any(
  5033. isinstance(block, dict) and
  5034. block.get("type") == "text" and
  5035. len(block.get("text", "")) > 0
  5036. for block in content
  5037. )
  5038. if not is_valid:
  5039. continue
  5040. # 检查 token 距离
  5041. msg_count = j - last_cache_pos
  5042. estimated_tokens = msg_count * AVG_TOKENS_PER_MSG
  5043. if estimated_tokens >= MIN_TOKENS:
  5044. cache_positions.append(j)
  5045. last_cache_pos = j
  5046. self.log.debug(f"[Cache] 在位置 {j} 添加缓存点 (估算 {estimated_tokens} tokens)")
  5047. break
  5048. # 应用缓存标记
  5049. for idx in cache_positions:
  5050. msg = messages[idx]
  5051. content = msg.get("content", "")
  5052. if isinstance(content, str):
  5053. msg["content"] = [{
  5054. "type": "text",
  5055. "text": content,
  5056. "cache_control": {"type": "ephemeral"}
  5057. }]
  5058. self.log.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  5059. elif isinstance(content, list):
  5060. # 在最后一个 text block 添加 cache_control
  5061. for block in reversed(content):
  5062. if isinstance(block, dict) and block.get("type") == "text":
  5063. block["cache_control"] = {"type": "ephemeral"}
  5064. self.log.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  5065. break
  5066. self.log.debug(
  5067. f"[Cache] 总消息: {total_msgs}, "
  5068. f"缓存点: {len(cache_positions)} at {cache_positions}"
  5069. )
  5070. return messages
  5071. def _get_configured_tool_names(
  5072. self,
  5073. tools: Optional[List[str]] = None,
  5074. tool_groups: Optional[List[str]] = None,
  5075. exclude_tools: Optional[List[str]] = None,
  5076. ) -> set[str]:
  5077. """解析 RunConfig 的基础工具能力集合。"""
  5078. if tools is not None:
  5079. # Explicit tools are an exact capability set, not an addition to
  5080. # the default groups.
  5081. tool_names = set(tools)
  5082. elif tool_groups is not None:
  5083. tool_names = set(self.tools.get_tool_names(groups=tool_groups))
  5084. else:
  5085. tool_names = set(self.tools.get_tool_names())
  5086. if exclude_tools:
  5087. tool_names -= set(exclude_tools)
  5088. return tool_names
  5089. def _get_tool_schemas(
  5090. self,
  5091. tools: Optional[List[str]] = None,
  5092. tool_groups: Optional[List[str]] = None,
  5093. exclude_tools: Optional[List[str]] = None,
  5094. ) -> List[Dict]:
  5095. """获取 RunConfig 基础能力对应的工具 Schema。"""
  5096. tool_names = self._get_configured_tool_names(
  5097. tools,
  5098. tool_groups,
  5099. exclude_tools,
  5100. )
  5101. return self.tools.get_schemas(list(tool_names))
  5102. def _get_runtime_tool_names(
  5103. self,
  5104. config: RunConfig,
  5105. trace: Trace,
  5106. *,
  5107. in_side_branch: bool = False,
  5108. ) -> set[str]:
  5109. """在 RunConfig 基础能力上应用 Recursive 协议状态门禁。
  5110. 主循环每轮调用,使待审核、待执行与报告阶段只暴露当前允许的工具。
  5111. """
  5112. tool_names = self._get_configured_tool_names(
  5113. config.tools,
  5114. config.tool_groups,
  5115. config.exclude_tools,
  5116. )
  5117. policy = policy_from_context(trace.context)
  5118. protocol_tools = {
  5119. "submit_task_report",
  5120. "review_task_result",
  5121. "update_task_progress",
  5122. "read_context_ref",
  5123. "manage_candidate",
  5124. }
  5125. if not policy.requires_task_protocol or in_side_branch:
  5126. available = tool_names - protocol_tools
  5127. if policy.requires_task_protocol:
  5128. available.discard("evaluate")
  5129. return available
  5130. if self.application_binding is not None and trace.context.get(
  5131. "application_ref"
  5132. ):
  5133. role = self.application_binding.role(
  5134. trace.context.get("application_role_id")
  5135. )
  5136. limits = trace.context.get("effective_run_limits") or {}
  5137. depth = int(trace.context.get("agent_depth", 0) or 0)
  5138. if (
  5139. not role.role.allowed_child_roles
  5140. or depth >= int(limits.get("max_depth", policy.max_depth))
  5141. ):
  5142. tool_names.discard("agent")
  5143. state = ensure_task_protocol(trace.context)
  5144. tool_names.discard("evaluate")
  5145. if state["pending_reviews"]:
  5146. return tool_names & {"review_task_result", "read_context_ref"}
  5147. if state["next_actions"]:
  5148. return tool_names & {"agent", "read_context_ref"}
  5149. tool_names.discard("review_task_result")
  5150. if self.candidate_service is None or state.get("task_report") is not None:
  5151. tool_names.discard("manage_candidate")
  5152. if not policy.requires_task_progress or state.get("task_report") is not None:
  5153. tool_names.discard("update_task_progress")
  5154. if not trace.parent_trace_id or state.get("task_report") is not None:
  5155. tool_names.discard("submit_task_report")
  5156. return tool_names
  5157. def _get_runtime_tool_schemas(
  5158. self,
  5159. config: RunConfig,
  5160. trace: Trace,
  5161. *,
  5162. in_side_branch: bool = False,
  5163. runtime_tool_names: Optional[set[str]] = None,
  5164. ) -> List[Dict]:
  5165. """按 Trace 持久化模式和当前协议状态生成工具 Schema。
  5166. 主循环把结果交给 LLM;Recursive revision 2 只允许受治理的本地
  5167. ``task_brief`` 委托,Legacy 和 Recursive revision 1 继续使用 ``task``。
  5168. """
  5169. tool_names = (
  5170. runtime_tool_names
  5171. if runtime_tool_names is not None
  5172. else self._get_runtime_tool_names(
  5173. config,
  5174. trace,
  5175. in_side_branch=in_side_branch,
  5176. )
  5177. )
  5178. schemas = deepcopy(self.tools.get_schemas(list(tool_names)))
  5179. policy = policy_from_context(trace.context)
  5180. for schema in schemas:
  5181. function = schema.get("function", {})
  5182. if function.get("name") != "agent":
  5183. continue
  5184. parameters = function.get("parameters", {})
  5185. properties = parameters.get("properties", {})
  5186. required = parameters.setdefault("required", [])
  5187. if policy.requires_task_protocol:
  5188. properties.pop("messages", None)
  5189. properties.pop("task", None)
  5190. if "task_brief" in properties:
  5191. properties["task_brief"]["description"] = (
  5192. "Required structured contract for Recursive local delegation. "
  5193. "remote_* agents are not supported in Recursive revision 3."
  5194. )
  5195. if "task" in required:
  5196. required.remove("task")
  5197. if "task_brief" in properties and "task_brief" not in required:
  5198. required.append("task_brief")
  5199. if self.application_binding is not None and trace.context.get(
  5200. "application_ref"
  5201. ):
  5202. role = self.application_binding.role(
  5203. trace.context.get("application_role_id")
  5204. )
  5205. properties.pop("skills", None)
  5206. if "skills" in required:
  5207. required.remove("skills")
  5208. if "agent_type" in properties:
  5209. properties["agent_type"]["enum"] = list(
  5210. role.role.allowed_child_roles
  5211. )
  5212. properties["agent_type"]["description"] = (
  5213. "Required application role for the direct child."
  5214. )
  5215. if "agent_type" not in required:
  5216. required.append("agent_type")
  5217. else:
  5218. properties.pop("task_brief", None)
  5219. if "task" in properties and "task" not in required:
  5220. required.append("task")
  5221. return schemas
  5222. def _build_tool_context(
  5223. self,
  5224. *,
  5225. config: RunConfig,
  5226. trace: Trace,
  5227. trace_id: str,
  5228. goal_id: Optional[str],
  5229. goal_tree: Optional[GoalTree],
  5230. sequence: int,
  5231. tool_call_id: str | None,
  5232. side_branch_ctx: Optional[SideBranchContext],
  5233. trigger_event: Optional[str],
  5234. ) -> Dict[str, Any]:
  5235. """构建 ToolRegistry 执行时注入的隐藏上下文。
  5236. 主循环在 dispatch 前调用;Recursive 权限快照和调度配置最后覆盖,不可伪造。
  5237. """
  5238. framework_context = {
  5239. "store": self.trace_store,
  5240. "task_protocol_service": self.task_protocol_service,
  5241. "candidate_service": self.candidate_service,
  5242. "event_service": self.event_service,
  5243. "trace_id": trace_id,
  5244. "goal_id": goal_id,
  5245. "runner": self,
  5246. "goal_tree": goal_tree,
  5247. "knowledge_config": config.knowledge,
  5248. "memory_config": config.memory,
  5249. "dream_scope": (
  5250. DreamScope(
  5251. uid=trace.uid,
  5252. agent_type=trace.agent_type or config.agent_type,
  5253. memory_identity=compute_memory_identity(config.memory),
  5254. )
  5255. if config.memory is not None
  5256. else None
  5257. ),
  5258. "sequence": sequence,
  5259. "tool_call_id": tool_call_id,
  5260. "side_branch": {
  5261. "type": side_branch_ctx.type,
  5262. "branch_id": side_branch_ctx.branch_id,
  5263. "is_side_branch": True,
  5264. "max_turns": side_branch_ctx.max_turns,
  5265. "trigger_event": trigger_event,
  5266. } if side_branch_ctx else None,
  5267. }
  5268. if policy_from_context(trace.context).mode is AgentMode.RECURSIVE:
  5269. context = {**(config.context or {}), **framework_context}
  5270. context.update({
  5271. RECURSIVE_CAPABILITY_TOOLS_CONTEXT_KEY: sorted(
  5272. self._get_configured_tool_names(
  5273. config.tools,
  5274. config.tool_groups,
  5275. config.exclude_tools,
  5276. )
  5277. ),
  5278. RECURSIVE_CHILD_EXECUTION_MODE_CONTEXT_KEY: config.child_execution_mode,
  5279. RECURSIVE_MAX_PARALLEL_CHILDREN_CONTEXT_KEY: config.max_parallel_children,
  5280. })
  5281. return context
  5282. # Framework-owned hidden values are authoritative in every mode. A
  5283. # caller-provided custom context may add page/business metadata, but it
  5284. # cannot replace the store, Trace identity or per-run MemoryConfig.
  5285. return {**(config.context or {}), **framework_context}
  5286. # 默认 system prompt 前缀(当 config.system_prompt 和前端都未提供 system message 时使用)
  5287. # 注意:此常量已迁移到 cyber_agent.core.prompts,这里保留引用以保持向后兼容
  5288. async def _build_system_prompt(self, config: RunConfig, base_prompt: Optional[str] = None) -> Optional[str]:
  5289. """构建 system prompt(注入 skills)
  5290. 优先级:
  5291. 1. base_prompt(来自消息)
  5292. 2. config.system_prompt(显式指定)
  5293. 3. preset.system_prompt(预设的完整 system prompt)
  5294. 4. 默认模板 + skills
  5295. Skills 注入优先级:
  5296. 1. config.skills 显式指定 → 按名称过滤
  5297. 2. config.skills 为 None → 查 preset 的默认 skills 列表
  5298. 3. preset 也无 skills(None)→ 加载全部(向后兼容)
  5299. Args:
  5300. base_prompt: 已有 system 内容(来自消息),
  5301. None 时使用 config.system_prompt 或 preset.system_prompt
  5302. """
  5303. from cyber_agent.core.presets import AGENT_PRESETS
  5304. # 确定 system_prompt 来源
  5305. if base_prompt is not None:
  5306. system_prompt = base_prompt
  5307. elif config.system_prompt is not None:
  5308. system_prompt = config.system_prompt
  5309. else:
  5310. # 尝试从 preset 获取 system_prompt
  5311. preset = AGENT_PRESETS.get(config.agent_type)
  5312. system_prompt = preset.system_prompt if preset and preset.system_prompt else None
  5313. # 确定要加载哪些 skills
  5314. skills_filter: Optional[List[str]] = config.skills
  5315. if skills_filter is None:
  5316. preset = AGENT_PRESETS.get(config.agent_type)
  5317. if preset is not None:
  5318. skills_filter = preset.skills # 可能仍为 None(加载全部)
  5319. # 加载并过滤
  5320. all_skills = load_skills_from_dir(self.skills_dir)
  5321. if skills_filter is not None:
  5322. skills = [s for s in all_skills if s.name in skills_filter]
  5323. else:
  5324. skills = all_skills
  5325. skills_text = self._format_skills(skills) if skills else ""
  5326. if system_prompt:
  5327. if skills_text:
  5328. system_prompt += f"\n\n## Skills\n{skills_text}"
  5329. else:
  5330. system_prompt = DEFAULT_SYSTEM_PREFIX
  5331. if skills_text:
  5332. system_prompt += f"\n\n## Skills\n{skills_text}"
  5333. if config.max_iterations and config.max_iterations > 0:
  5334. system_prompt += f"\n\n## Execution Constraint\n这是一项有严格步数限制的任务。你最多可以用 {config.max_iterations} 轮交互来解决问题。\n请务必【边查边写、随时存档】!每当你收集或得出一个有价值的独立结果(如收集到一个独立 Case),请立刻调用工具写入或追加到结果文件中,绝对不要等到所有任务都做完再最后一次性输出。这样即使触达步数上限被强制打断,你已经收集的成果也能安全保留!"
  5335. # Memory 注入(memory-bearing Agent)——在 system prompt 末尾追加
  5336. # 初版选择 system prompt 追加(见 cyber_agent/docs/framework/runtime/memory.md 待定问题 1)。
  5337. # 好处:run 启动一次性注入、所有后续轮次都能看到、与 skills 注入方式一致。
  5338. # 代价:若记忆文件很大会持续占 prompt tokens —— 待观察后决定是否切换方案。
  5339. if config.memory:
  5340. try:
  5341. from cyber_agent.core.memory import load_memory_files, format_memory_injection
  5342. files = load_memory_files(config.memory)
  5343. memory_text = format_memory_injection(files)
  5344. if memory_text:
  5345. system_prompt += f"\n\n{memory_text}"
  5346. except Exception as e:
  5347. self.log.warning(f"[Memory] 加载记忆失败,跳过注入: {e}")
  5348. return system_prompt
  5349. @staticmethod
  5350. def _task_text(messages: List[Dict]) -> str:
  5351. """Extract the same plain task text used by Legacy title generation."""
  5352. text_parts = []
  5353. for msg in messages:
  5354. content = msg.get("content", "")
  5355. if isinstance(content, str):
  5356. text_parts.append(content)
  5357. elif isinstance(content, list):
  5358. for part in content:
  5359. if isinstance(part, dict) and part.get("type") == "text":
  5360. text_parts.append(part.get("text", ""))
  5361. return " ".join(text_parts).strip()
  5362. @classmethod
  5363. def _fallback_task_name(cls, messages: List[Dict]) -> str:
  5364. """Build a deterministic title without spending an untracked LLM call."""
  5365. raw_text = cls._task_text(messages)
  5366. if not raw_text:
  5367. return TASK_NAME_FALLBACK
  5368. return raw_text[:50] + ("..." if len(raw_text) > 50 else "")
  5369. async def _generate_task_name(self, messages: List[Dict]) -> str:
  5370. """生成任务名称:优先使用 utility_llm,fallback 到文本截取"""
  5371. fallback = self._fallback_task_name(messages)
  5372. raw_text = self._task_text(messages)
  5373. # 尝试使用 utility_llm 生成标题
  5374. if self.utility_llm_call:
  5375. try:
  5376. result = await self.utility_llm_call(
  5377. messages=[
  5378. {"role": "system", "content": TASK_NAME_GENERATION_SYSTEM_PROMPT},
  5379. {"role": "user", "content": raw_text[:2000]},
  5380. ],
  5381. model="gpt-4o-mini", # 使用便宜模型
  5382. )
  5383. title = result.get("content", "").strip()
  5384. if title and len(title) < 100:
  5385. return title
  5386. except Exception:
  5387. pass
  5388. # Fallback: 截取前 50 字符
  5389. return fallback
  5390. def _format_skills(self, skills: List[Skill]) -> str:
  5391. if not skills:
  5392. return ""
  5393. return "\n\n".join(s.to_prompt_text() for s in skills)