print.ts 209 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596
  1. // biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered
  2. import { feature } from 'bun:bundle'
  3. import { readFile, stat } from 'fs/promises'
  4. import { dirname } from 'path'
  5. import {
  6. downloadUserSettings,
  7. redownloadUserSettings,
  8. } from 'src/services/settingsSync/index.js'
  9. import { waitForRemoteManagedSettingsToLoad } from 'src/services/remoteManagedSettings/index.js'
  10. import { StructuredIO } from 'src/cli/structuredIO.js'
  11. import { RemoteIO } from 'src/cli/remoteIO.js'
  12. import {
  13. type Command,
  14. formatDescriptionWithSource,
  15. getCommandName,
  16. } from 'src/commands.js'
  17. import { createStreamlinedTransformer } from 'src/utils/streamlinedTransform.js'
  18. import { installStreamJsonStdoutGuard } from 'src/utils/streamJsonStdoutGuard.js'
  19. import type { ToolPermissionContext } from 'src/Tool.js'
  20. import type { ThinkingConfig } from 'src/utils/thinking.js'
  21. import { assembleToolPool, filterToolsByDenyRules } from 'src/tools.js'
  22. import uniqBy from 'lodash-es/uniqBy.js'
  23. import { uniq } from 'src/utils/array.js'
  24. import { mergeAndFilterTools } from 'src/utils/toolPool.js'
  25. import {
  26. logEvent,
  27. type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  28. } from 'src/services/analytics/index.js'
  29. import { getFeatureValue_CACHED_MAY_BE_STALE } from 'src/services/analytics/growthbook.js'
  30. import { logForDebugging } from 'src/utils/debug.js'
  31. import {
  32. logForDiagnosticsNoPII,
  33. withDiagnosticsTiming,
  34. } from 'src/utils/diagLogs.js'
  35. import { toolMatchesName, type Tool, type Tools } from 'src/Tool.js'
  36. import {
  37. type AgentDefinition,
  38. isBuiltInAgent,
  39. parseAgentsFromJson,
  40. } from 'src/tools/AgentTool/loadAgentsDir.js'
  41. import type { Message, NormalizedUserMessage } from 'src/types/message.js'
  42. import type { QueuedCommand } from 'src/types/textInputTypes.js'
  43. import {
  44. dequeue,
  45. dequeueAllMatching,
  46. enqueue,
  47. hasCommandsInQueue,
  48. peek,
  49. subscribeToCommandQueue,
  50. getCommandsByMaxPriority,
  51. } from 'src/utils/messageQueueManager.js'
  52. import { notifyCommandLifecycle } from 'src/utils/commandLifecycle.js'
  53. import {
  54. getSessionState,
  55. notifySessionStateChanged,
  56. notifySessionMetadataChanged,
  57. setPermissionModeChangedListener,
  58. type RequiresActionDetails,
  59. type SessionExternalMetadata,
  60. } from 'src/utils/sessionState.js'
  61. import { externalMetadataToAppState } from 'src/state/onChangeAppState.js'
  62. import { getInMemoryErrors, logError, logMCPDebug } from 'src/utils/log.js'
  63. import {
  64. writeToStdout,
  65. registerProcessOutputErrorHandlers,
  66. } from 'src/utils/process.js'
  67. import type { Stream } from 'src/utils/stream.js'
  68. import { EMPTY_USAGE } from 'src/services/api/logging.js'
  69. import {
  70. loadConversationForResume,
  71. type TurnInterruptionState,
  72. } from 'src/utils/conversationRecovery.js'
  73. import type {
  74. MCPServerConnection,
  75. McpSdkServerConfig,
  76. ScopedMcpServerConfig,
  77. } from 'src/services/mcp/types.js'
  78. import {
  79. ChannelMessageNotificationSchema,
  80. gateChannelServer,
  81. wrapChannelMessage,
  82. findChannelEntry,
  83. } from 'src/services/mcp/channelNotification.js'
  84. import {
  85. isChannelAllowlisted,
  86. isChannelsEnabled,
  87. } from 'src/services/mcp/channelAllowlist.js'
  88. import { parsePluginIdentifier } from 'src/utils/plugins/pluginIdentifier.js'
  89. import { validateUuid } from 'src/utils/uuid.js'
  90. import { fromArray } from 'src/utils/generators.js'
  91. import { ask } from 'src/QueryEngine.js'
  92. import type { PermissionPromptTool } from 'src/utils/queryHelpers.js'
  93. import {
  94. createFileStateCacheWithSizeLimit,
  95. mergeFileStateCaches,
  96. READ_FILE_STATE_CACHE_SIZE,
  97. } from 'src/utils/fileStateCache.js'
  98. import { expandPath } from 'src/utils/path.js'
  99. import { extractReadFilesFromMessages } from 'src/utils/queryHelpers.js'
  100. import { registerHookEventHandler } from 'src/utils/hooks/hookEvents.js'
  101. import { executeFilePersistence } from 'src/utils/filePersistence/filePersistence.js'
  102. import { finalizePendingAsyncHooks } from 'src/utils/hooks/AsyncHookRegistry.js'
  103. import {
  104. gracefulShutdown,
  105. gracefulShutdownSync,
  106. isShuttingDown,
  107. } from 'src/utils/gracefulShutdown.js'
  108. import { registerCleanup } from 'src/utils/cleanupRegistry.js'
  109. import { createIdleTimeoutManager } from 'src/utils/idleTimeout.js'
  110. import type {
  111. SDKStatus,
  112. ModelInfo,
  113. SDKMessage,
  114. SDKUserMessage,
  115. SDKUserMessageReplay,
  116. PermissionResult,
  117. McpServerConfigForProcessTransport,
  118. McpServerStatus,
  119. RewindFilesResult,
  120. } from 'src/entrypoints/agentSdkTypes.js'
  121. import type {
  122. StdoutMessage,
  123. SDKControlInitializeRequest,
  124. SDKControlInitializeResponse,
  125. SDKControlRequest,
  126. SDKControlResponse,
  127. SDKControlMcpSetServersResponse,
  128. SDKControlReloadPluginsResponse,
  129. } from 'src/entrypoints/sdk/controlTypes.js'
  130. import type { PermissionMode } from '@anthropic-ai/claude-agent-sdk'
  131. import type { PermissionMode as InternalPermissionMode } from 'src/types/permissions.js'
  132. import { cwd } from 'process'
  133. import { getCwd } from 'src/utils/cwd.js'
  134. import omit from 'lodash-es/omit.js'
  135. import reject from 'lodash-es/reject.js'
  136. import { isPolicyAllowed } from 'src/services/policyLimits/index.js'
  137. import type { ReplBridgeHandle } from 'src/bridge/replBridge.js'
  138. import { getRemoteSessionUrl } from 'src/constants/product.js'
  139. import { buildBridgeConnectUrl } from 'src/bridge/bridgeStatusUtil.js'
  140. import { extractInboundMessageFields } from 'src/bridge/inboundMessages.js'
  141. import { resolveAndPrepend } from 'src/bridge/inboundAttachments.js'
  142. import type { CanUseToolFn } from 'src/hooks/useCanUseTool.js'
  143. import { hasPermissionsToUseTool } from 'src/utils/permissions/permissions.js'
  144. import { safeParseJSON } from 'src/utils/json.js'
  145. import {
  146. outputSchema as permissionToolOutputSchema,
  147. permissionPromptToolResultToPermissionDecision,
  148. } from 'src/utils/permissions/PermissionPromptToolResultSchema.js'
  149. import { createAbortController } from 'src/utils/abortController.js'
  150. import { createCombinedAbortSignal } from 'src/utils/combinedAbortSignal.js'
  151. import { generateSessionTitle } from 'src/utils/sessionTitle.js'
  152. import { buildSideQuestionFallbackParams } from 'src/utils/queryContext.js'
  153. import { runSideQuestion } from 'src/utils/sideQuestion.js'
  154. import {
  155. processSessionStartHooks,
  156. processSetupHooks,
  157. takeInitialUserMessage,
  158. } from 'src/utils/sessionStart.js'
  159. import {
  160. DEFAULT_OUTPUT_STYLE_NAME,
  161. getAllOutputStyles,
  162. } from 'src/constants/outputStyles.js'
  163. import { TEAMMATE_MESSAGE_TAG, TICK_TAG } from 'src/constants/xml.js'
  164. import {
  165. getSettings_DEPRECATED,
  166. getSettingsWithSources,
  167. } from 'src/utils/settings/settings.js'
  168. import { settingsChangeDetector } from 'src/utils/settings/changeDetector.js'
  169. import { applySettingsChange } from 'src/utils/settings/applySettingsChange.js'
  170. import {
  171. isFastModeAvailable,
  172. isFastModeEnabled,
  173. isFastModeSupportedByModel,
  174. getFastModeState,
  175. } from 'src/utils/fastMode.js'
  176. import {
  177. isAutoModeGateEnabled,
  178. getAutoModeUnavailableNotification,
  179. getAutoModeUnavailableReason,
  180. isBypassPermissionsModeDisabled,
  181. transitionPermissionMode,
  182. } from 'src/utils/permissions/permissionSetup.js'
  183. import {
  184. tryGenerateSuggestion,
  185. logSuggestionOutcome,
  186. logSuggestionSuppressed,
  187. type PromptVariant,
  188. } from 'src/services/PromptSuggestion/promptSuggestion.js'
  189. import { getLastCacheSafeParams } from 'src/utils/forkedAgent.js'
  190. import { getAccountInformation } from 'src/utils/auth.js'
  191. import { OAuthService } from 'src/services/oauth/index.js'
  192. import { installOAuthTokens } from 'src/cli/handlers/auth.js'
  193. import { getAPIProvider } from 'src/utils/model/providers.js'
  194. import type { HookCallbackMatcher } from 'src/types/hooks.js'
  195. import { AwsAuthStatusManager } from 'src/utils/awsAuthStatusManager.js'
  196. import type { HookEvent } from 'src/entrypoints/agentSdkTypes.js'
  197. import {
  198. registerHookCallbacks,
  199. setInitJsonSchema,
  200. getInitJsonSchema,
  201. setSdkAgentProgressSummariesEnabled,
  202. } from 'src/bootstrap/state.js'
  203. import { createSyntheticOutputTool } from 'src/tools/SyntheticOutputTool/SyntheticOutputTool.js'
  204. import { parseSessionIdentifier } from 'src/utils/sessionUrl.js'
  205. import {
  206. hydrateRemoteSession,
  207. hydrateFromCCRv2InternalEvents,
  208. resetSessionFilePointer,
  209. doesMessageExistInSession,
  210. findUnresolvedToolUse,
  211. recordAttributionSnapshot,
  212. saveAgentSetting,
  213. saveMode,
  214. saveAiGeneratedTitle,
  215. restoreSessionMetadata,
  216. } from 'src/utils/sessionStorage.js'
  217. import { incrementPromptCount } from 'src/utils/commitAttribution.js'
  218. import {
  219. setupSdkMcpClients,
  220. connectToServer,
  221. clearServerCache,
  222. fetchToolsForClient,
  223. areMcpConfigsEqual,
  224. reconnectMcpServerImpl,
  225. } from 'src/services/mcp/client.js'
  226. import {
  227. filterMcpServersByPolicy,
  228. getMcpConfigByName,
  229. isMcpServerDisabled,
  230. setMcpServerEnabled,
  231. } from 'src/services/mcp/config.js'
  232. import {
  233. performMCPOAuthFlow,
  234. revokeServerTokens,
  235. } from 'src/services/mcp/auth.js'
  236. import {
  237. runElicitationHooks,
  238. runElicitationResultHooks,
  239. } from 'src/services/mcp/elicitationHandler.js'
  240. import { executeNotificationHooks } from 'src/utils/hooks.js'
  241. import {
  242. ElicitRequestSchema,
  243. ElicitationCompleteNotificationSchema,
  244. } from '@modelcontextprotocol/sdk/types.js'
  245. import { getMcpPrefix } from 'src/services/mcp/mcpStringUtils.js'
  246. import {
  247. commandBelongsToServer,
  248. filterToolsByServer,
  249. } from 'src/services/mcp/utils.js'
  250. import { setupVscodeSdkMcp } from 'src/services/mcp/vscodeSdkMcp.js'
  251. import { getAllMcpConfigs } from 'src/services/mcp/config.js'
  252. import {
  253. isQualifiedForGrove,
  254. checkGroveForNonInteractive,
  255. } from 'src/services/api/grove.js'
  256. import {
  257. toInternalMessages,
  258. toSDKRateLimitInfo,
  259. } from 'src/utils/messages/mappers.js'
  260. import { createModelSwitchBreadcrumbs } from 'src/utils/messages.js'
  261. import { collectContextData } from 'src/commands/context/context-noninteractive.js'
  262. import { LOCAL_COMMAND_STDOUT_TAG } from 'src/constants/xml.js'
  263. import {
  264. statusListeners,
  265. type ClaudeAILimits,
  266. } from 'src/services/claudeAiLimits.js'
  267. import {
  268. getDefaultMainLoopModel,
  269. getMainLoopModel,
  270. modelDisplayString,
  271. parseUserSpecifiedModel,
  272. } from 'src/utils/model/model.js'
  273. import { getModelOptions } from 'src/utils/model/modelOptions.js'
  274. import {
  275. modelSupportsEffort,
  276. modelSupportsMaxEffort,
  277. EFFORT_LEVELS,
  278. resolveAppliedEffort,
  279. } from 'src/utils/effort.js'
  280. import { modelSupportsAdaptiveThinking } from 'src/utils/thinking.js'
  281. import { modelSupportsAutoMode } from 'src/utils/betas.js'
  282. import { ensureModelStringsInitialized } from 'src/utils/model/modelStrings.js'
  283. import {
  284. getSessionId,
  285. setMainLoopModelOverride,
  286. setMainThreadAgentType,
  287. switchSession,
  288. isSessionPersistenceDisabled,
  289. getIsRemoteMode,
  290. getFlagSettingsInline,
  291. setFlagSettingsInline,
  292. getMainThreadAgentType,
  293. getAllowedChannels,
  294. setAllowedChannels,
  295. type ChannelEntry,
  296. } from 'src/bootstrap/state.js'
  297. import { runWithWorkload, WORKLOAD_CRON } from 'src/utils/workloadContext.js'
  298. import type { UUID } from 'crypto'
  299. import { randomUUID } from 'crypto'
  300. import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/messages.mjs'
  301. import type { AppState } from 'src/state/AppStateStore.js'
  302. import {
  303. fileHistoryRewind,
  304. fileHistoryCanRestore,
  305. fileHistoryEnabled,
  306. fileHistoryGetDiffStats,
  307. } from 'src/utils/fileHistory.js'
  308. import {
  309. restoreAgentFromSession,
  310. restoreSessionStateFromLog,
  311. } from 'src/utils/sessionRestore.js'
  312. import { SandboxManager } from 'src/utils/sandbox/sandbox-adapter.js'
  313. import {
  314. headlessProfilerStartTurn,
  315. headlessProfilerCheckpoint,
  316. logHeadlessProfilerTurn,
  317. } from 'src/utils/headlessProfiler.js'
  318. import {
  319. startQueryProfile,
  320. logQueryProfileReport,
  321. } from 'src/utils/queryProfiler.js'
  322. import { asSessionId } from 'src/types/ids.js'
  323. import { jsonStringify } from '../utils/slowOperations.js'
  324. import { skillChangeDetector } from '../utils/skills/skillChangeDetector.js'
  325. import { getCommands, clearCommandsCache } from '../commands.js'
  326. import {
  327. isBareMode,
  328. isEnvTruthy,
  329. isEnvDefinedFalsy,
  330. } from '../utils/envUtils.js'
  331. import { installPluginsForHeadless } from '../utils/plugins/headlessPluginInstall.js'
  332. import { refreshActivePlugins } from '../utils/plugins/refresh.js'
  333. import { loadAllPluginsCacheOnly } from '../utils/plugins/pluginLoader.js'
  334. import {
  335. isTeamLead,
  336. hasActiveInProcessTeammates,
  337. hasWorkingInProcessTeammates,
  338. waitForTeammatesToBecomeIdle,
  339. } from '../utils/teammate.js'
  340. import {
  341. readUnreadMessages,
  342. markMessagesAsRead,
  343. isShutdownApproved,
  344. } from '../utils/teammateMailbox.js'
  345. import { removeTeammateFromTeamFile } from '../utils/swarm/teamHelpers.js'
  346. import { unassignTeammateTasks } from '../utils/tasks.js'
  347. import { getRunningTasks } from '../utils/task/framework.js'
  348. import { isBackgroundTask } from '../tasks/types.js'
  349. import { stopTask } from '../tasks/stopTask.js'
  350. import { drainSdkEvents } from '../utils/sdkEventQueue.js'
  351. import { initializeGrowthBook } from '../services/analytics/growthbook.js'
  352. import { errorMessage, toError } from '../utils/errors.js'
  353. import { sleep } from '../utils/sleep.js'
  354. import { isExtractModeActive } from '../memdir/paths.js'
  355. // Dead code elimination: conditional imports
  356. /* eslint-disable @typescript-eslint/no-require-imports */
  357. const coordinatorModeModule = feature('COORDINATOR_MODE')
  358. ? (require('../coordinator/coordinatorMode.js') as typeof import('../coordinator/coordinatorMode.js'))
  359. : null
  360. const proactiveModule =
  361. feature('PROACTIVE') || feature('KAIROS')
  362. ? (require('../proactive/index.js') as typeof import('../proactive/index.js'))
  363. : null
  364. const cronSchedulerModule = require('../utils/cronScheduler.js') as typeof import('../utils/cronScheduler.js')
  365. const cronJitterConfigModule = require('../utils/cronJitterConfig.js') as typeof import('../utils/cronJitterConfig.js')
  366. const cronGate = require('../tools/ScheduleCronTool/prompt.js') as typeof import('../tools/ScheduleCronTool/prompt.js')
  367. const extractMemoriesModule = feature('EXTRACT_MEMORIES')
  368. ? (require('../services/extractMemories/extractMemories.js') as typeof import('../services/extractMemories/extractMemories.js'))
  369. : null
  370. /* eslint-enable @typescript-eslint/no-require-imports */
  371. const SHUTDOWN_TEAM_PROMPT = `<system-reminder>
  372. You are running in non-interactive mode and cannot return a response to the user until your team is shut down.
  373. You MUST shut down your team before preparing your final response:
  374. 1. Use requestShutdown to ask each team member to shut down gracefully
  375. 2. Wait for shutdown approvals
  376. 3. Use the cleanup operation to clean up the team
  377. 4. Only then provide your final response to the user
  378. The user cannot receive your response until the team is completely shut down.
  379. </system-reminder>
  380. Shut down your team and prepare your final response for the user.`
  381. // Track message UUIDs received during the current session runtime
  382. const MAX_RECEIVED_UUIDS = 10_000
  383. const receivedMessageUuids = new Set<UUID>()
  384. const receivedMessageUuidsOrder: UUID[] = []
  385. function trackReceivedMessageUuid(uuid: UUID): boolean {
  386. if (receivedMessageUuids.has(uuid)) {
  387. return false // duplicate
  388. }
  389. receivedMessageUuids.add(uuid)
  390. receivedMessageUuidsOrder.push(uuid)
  391. // Evict oldest entries when at capacity
  392. if (receivedMessageUuidsOrder.length > MAX_RECEIVED_UUIDS) {
  393. const toEvict = receivedMessageUuidsOrder.splice(
  394. 0,
  395. receivedMessageUuidsOrder.length - MAX_RECEIVED_UUIDS,
  396. )
  397. for (const old of toEvict) {
  398. receivedMessageUuids.delete(old)
  399. }
  400. }
  401. return true // new UUID
  402. }
  403. type PromptValue = string | ContentBlockParam[]
  404. function toBlocks(v: PromptValue): ContentBlockParam[] {
  405. return typeof v === 'string' ? [{ type: 'text', text: v }] : v
  406. }
  407. /**
  408. * Join prompt values from multiple queued commands into one. Strings are
  409. * newline-joined; if any value is a block array, all values are normalized
  410. * to blocks and concatenated.
  411. */
  412. export function joinPromptValues(values: PromptValue[]): PromptValue {
  413. if (values.length === 1) return values[0]!
  414. if (values.every(v => typeof v === 'string')) {
  415. return values.join('\n')
  416. }
  417. return values.flatMap(toBlocks)
  418. }
  419. /**
  420. * Whether `next` can be batched into the same ask() call as `head`. Only
  421. * prompt-mode commands batch, and only when the workload tag matches (so the
  422. * combined turn is attributed correctly) and the isMeta flag matches (so a
  423. * proactive tick can't merge into a user prompt and lose its hidden-in-
  424. * transcript marking when the head is spread over the merged command).
  425. */
  426. export function canBatchWith(
  427. head: QueuedCommand,
  428. next: QueuedCommand | undefined,
  429. ): boolean {
  430. return (
  431. next !== undefined &&
  432. next.mode === 'prompt' &&
  433. next.workload === head.workload &&
  434. next.isMeta === head.isMeta
  435. )
  436. }
  437. export async function runHeadless(
  438. inputPrompt: string | AsyncIterable<string>,
  439. getAppState: () => AppState,
  440. setAppState: (f: (prev: AppState) => AppState) => void,
  441. commands: Command[],
  442. tools: Tools,
  443. sdkMcpConfigs: Record<string, McpSdkServerConfig>,
  444. agents: AgentDefinition[],
  445. options: {
  446. continue: boolean | undefined
  447. resume: string | boolean | undefined
  448. resumeSessionAt: string | undefined
  449. verbose: boolean | undefined
  450. outputFormat: string | undefined
  451. jsonSchema: Record<string, unknown> | undefined
  452. permissionPromptToolName: string | undefined
  453. allowedTools: string[] | undefined
  454. thinkingConfig: ThinkingConfig | undefined
  455. maxTurns: number | undefined
  456. maxBudgetUsd: number | undefined
  457. taskBudget: { total: number } | undefined
  458. systemPrompt: string | undefined
  459. appendSystemPrompt: string | undefined
  460. userSpecifiedModel: string | undefined
  461. fallbackModel: string | undefined
  462. teleport: string | true | null | undefined
  463. sdkUrl: string | undefined
  464. replayUserMessages: boolean | undefined
  465. includePartialMessages: boolean | undefined
  466. forkSession: boolean | undefined
  467. rewindFiles: string | undefined
  468. enableAuthStatus: boolean | undefined
  469. agent: string | undefined
  470. workload: string | undefined
  471. setupTrigger?: 'init' | 'maintenance' | undefined
  472. sessionStartHooksPromise?: ReturnType<typeof processSessionStartHooks>
  473. setSDKStatus?: (status: SDKStatus) => void
  474. },
  475. ): Promise<void> {
  476. if (
  477. process.env.USER_TYPE === 'ant' &&
  478. isEnvTruthy(process.env.CLAUDE_CODE_EXIT_AFTER_FIRST_RENDER)
  479. ) {
  480. process.stderr.write(
  481. `\nStartup time: ${Math.round(process.uptime() * 1000)}ms\n`,
  482. )
  483. // eslint-disable-next-line custom-rules/no-process-exit
  484. process.exit(0)
  485. }
  486. // Fire user settings download now so it overlaps with the MCP/tool setup
  487. // below. Managed settings already started in main.tsx preAction; this gives
  488. // user settings a similar head start. The cached promise is joined in
  489. // installPluginsAndApplyMcpInBackground before plugin install reads
  490. // enabledPlugins.
  491. if (
  492. feature('DOWNLOAD_USER_SETTINGS') &&
  493. (isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) || getIsRemoteMode())
  494. ) {
  495. void downloadUserSettings()
  496. }
  497. // In headless mode there is no React tree, so the useSettingsChange hook
  498. // never runs. Subscribe directly so that settings changes (including
  499. // managed-settings / policy updates) are fully applied.
  500. settingsChangeDetector.subscribe(source => {
  501. applySettingsChange(source, setAppState)
  502. // In headless mode, also sync the denormalized fastMode field from
  503. // settings. The TUI manages fastMode via the UI so it skips this.
  504. if (isFastModeEnabled()) {
  505. setAppState(prev => {
  506. const s = prev.settings as Record<string, unknown>
  507. const fastMode = s.fastMode === true && !s.fastModePerSessionOptIn
  508. return { ...prev, fastMode }
  509. })
  510. }
  511. })
  512. // Proactive activation is now handled in main.tsx before getTools() so
  513. // SleepTool passes isEnabled() filtering. This fallback covers the case
  514. // where CLAUDE_CODE_PROACTIVE is set but main.tsx's check didn't fire
  515. // (e.g. env was injected by the SDK transport after argv parsing).
  516. if (
  517. (feature('PROACTIVE') || feature('KAIROS')) &&
  518. proactiveModule &&
  519. !proactiveModule.isProactiveActive() &&
  520. isEnvTruthy(process.env.CLAUDE_CODE_PROACTIVE)
  521. ) {
  522. proactiveModule.activateProactive('command')
  523. }
  524. // Periodically force a full GC to keep memory usage in check
  525. if (typeof Bun !== 'undefined') {
  526. const gcTimer = setInterval(Bun.gc, 1000)
  527. gcTimer.unref()
  528. }
  529. // Start headless profiler for first turn
  530. headlessProfilerStartTurn()
  531. headlessProfilerCheckpoint('runHeadless_entry')
  532. // Check Grove requirements for non-interactive consumer subscribers
  533. if (await isQualifiedForGrove()) {
  534. await checkGroveForNonInteractive()
  535. }
  536. headlessProfilerCheckpoint('after_grove_check')
  537. // Initialize GrowthBook so feature flags take effect in headless mode.
  538. // Without this, the disk cache is empty and all flags fall back to defaults.
  539. void initializeGrowthBook()
  540. if (options.resumeSessionAt && !options.resume) {
  541. process.stderr.write(`Error: --resume-session-at requires --resume\n`)
  542. gracefulShutdownSync(1)
  543. return
  544. }
  545. if (options.rewindFiles && !options.resume) {
  546. process.stderr.write(`Error: --rewind-files requires --resume\n`)
  547. gracefulShutdownSync(1)
  548. return
  549. }
  550. if (options.rewindFiles && inputPrompt) {
  551. process.stderr.write(
  552. `Error: --rewind-files is a standalone operation and cannot be used with a prompt\n`,
  553. )
  554. gracefulShutdownSync(1)
  555. return
  556. }
  557. const structuredIO = getStructuredIO(inputPrompt, options)
  558. // When emitting NDJSON for SDK clients, any stray write to stdout (debug
  559. // prints, dependency console.log, library banners) breaks the client's
  560. // line-by-line JSON parser. Install a guard that diverts non-JSON lines to
  561. // stderr so the stream stays clean. Must run before the first
  562. // structuredIO.write below.
  563. if (options.outputFormat === 'stream-json') {
  564. installStreamJsonStdoutGuard()
  565. }
  566. // #34044: if user explicitly set sandbox.enabled=true but deps are missing,
  567. // isSandboxingEnabled() returns false silently. Surface the reason so users
  568. // know their security config isn't being enforced.
  569. const sandboxUnavailableReason = SandboxManager.getSandboxUnavailableReason()
  570. if (sandboxUnavailableReason) {
  571. if (SandboxManager.isSandboxRequired()) {
  572. process.stderr.write(
  573. `\nError: sandbox required but unavailable: ${sandboxUnavailableReason}\n` +
  574. ` sandbox.failIfUnavailable is set — refusing to start without a working sandbox.\n\n`,
  575. )
  576. gracefulShutdownSync(1)
  577. return
  578. }
  579. process.stderr.write(
  580. `\n⚠ Sandbox disabled: ${sandboxUnavailableReason}\n` +
  581. ` Commands will run WITHOUT sandboxing. Network and filesystem restrictions will NOT be enforced.\n\n`,
  582. )
  583. } else if (SandboxManager.isSandboxingEnabled()) {
  584. // Initialize sandbox with a callback that forwards network permission
  585. // requests to the SDK host via the can_use_tool control_request protocol.
  586. // This must happen after structuredIO is created so we can send requests.
  587. try {
  588. await SandboxManager.initialize(structuredIO.createSandboxAskCallback())
  589. } catch (err) {
  590. process.stderr.write(`\n❌ Sandbox Error: ${errorMessage(err)}\n`)
  591. gracefulShutdownSync(1, 'other')
  592. return
  593. }
  594. }
  595. if (options.outputFormat === 'stream-json' && options.verbose) {
  596. registerHookEventHandler(event => {
  597. const message: StdoutMessage = (() => {
  598. switch (event.type) {
  599. case 'started':
  600. return {
  601. type: 'system' as const,
  602. subtype: 'hook_started' as const,
  603. hook_id: event.hookId,
  604. hook_name: event.hookName,
  605. hook_event: event.hookEvent,
  606. uuid: randomUUID(),
  607. session_id: getSessionId(),
  608. }
  609. case 'progress':
  610. return {
  611. type: 'system' as const,
  612. subtype: 'hook_progress' as const,
  613. hook_id: event.hookId,
  614. hook_name: event.hookName,
  615. hook_event: event.hookEvent,
  616. stdout: event.stdout,
  617. stderr: event.stderr,
  618. output: event.output,
  619. uuid: randomUUID(),
  620. session_id: getSessionId(),
  621. }
  622. case 'response':
  623. return {
  624. type: 'system' as const,
  625. subtype: 'hook_response' as const,
  626. hook_id: event.hookId,
  627. hook_name: event.hookName,
  628. hook_event: event.hookEvent,
  629. output: event.output,
  630. stdout: event.stdout,
  631. stderr: event.stderr,
  632. exit_code: event.exitCode,
  633. outcome: event.outcome,
  634. uuid: randomUUID(),
  635. session_id: getSessionId(),
  636. }
  637. }
  638. })()
  639. void structuredIO.write(message)
  640. })
  641. }
  642. if (options.setupTrigger) {
  643. await processSetupHooks(options.setupTrigger)
  644. }
  645. headlessProfilerCheckpoint('before_loadInitialMessages')
  646. const appState = getAppState()
  647. const {
  648. messages: initialMessages,
  649. turnInterruptionState,
  650. agentSetting: resumedAgentSetting,
  651. } = await loadInitialMessages(setAppState, {
  652. continue: options.continue,
  653. teleport: options.teleport,
  654. resume: options.resume,
  655. resumeSessionAt: options.resumeSessionAt,
  656. forkSession: options.forkSession,
  657. outputFormat: options.outputFormat,
  658. sessionStartHooksPromise: options.sessionStartHooksPromise,
  659. restoredWorkerState: structuredIO.restoredWorkerState,
  660. })
  661. // SessionStart hooks can emit initialUserMessage — the first user turn for
  662. // headless orchestrator sessions where stdin is empty and additionalContext
  663. // alone (an attachment, not a turn) would leave the REPL with nothing to
  664. // respond to. The hook promise is awaited inside loadInitialMessages, so the
  665. // module-level pending value is set by the time we get here.
  666. const hookInitialUserMessage = takeInitialUserMessage()
  667. if (hookInitialUserMessage) {
  668. structuredIO.prependUserMessage(hookInitialUserMessage)
  669. }
  670. // Restore agent setting from the resumed session (if not overridden by current --agent flag
  671. // or settings-based agent, which would already have set mainThreadAgentType in main.tsx)
  672. if (!options.agent && !getMainThreadAgentType() && resumedAgentSetting) {
  673. const { agentDefinition: restoredAgent } = restoreAgentFromSession(
  674. resumedAgentSetting,
  675. undefined,
  676. { activeAgents: agents, allAgents: agents },
  677. )
  678. if (restoredAgent) {
  679. setAppState(prev => ({ ...prev, agent: restoredAgent.agentType }))
  680. // Apply the agent's system prompt for non-built-in agents (mirrors main.tsx initial --agent path)
  681. if (!options.systemPrompt && !isBuiltInAgent(restoredAgent)) {
  682. const agentSystemPrompt = restoredAgent.getSystemPrompt()
  683. if (agentSystemPrompt) {
  684. options.systemPrompt = agentSystemPrompt
  685. }
  686. }
  687. // Re-persist agent setting so future resumes maintain the agent
  688. saveAgentSetting(restoredAgent.agentType)
  689. }
  690. }
  691. // gracefulShutdownSync schedules an async shutdown and sets process.exitCode.
  692. // If a loadInitialMessages error path triggered it, bail early to avoid
  693. // unnecessary work while the process winds down.
  694. if (initialMessages.length === 0 && process.exitCode !== undefined) {
  695. return
  696. }
  697. // Handle --rewind-files: restore filesystem and exit immediately
  698. if (options.rewindFiles) {
  699. // File history snapshots are only created for user messages,
  700. // so we require the target to be a user message
  701. const targetMessage = initialMessages.find(
  702. m => m.uuid === options.rewindFiles,
  703. )
  704. if (!targetMessage || targetMessage.type !== 'user') {
  705. process.stderr.write(
  706. `Error: --rewind-files requires a user message UUID, but ${options.rewindFiles} is not a user message in this session\n`,
  707. )
  708. gracefulShutdownSync(1)
  709. return
  710. }
  711. const currentAppState = getAppState()
  712. const result = await handleRewindFiles(
  713. options.rewindFiles as UUID,
  714. currentAppState,
  715. setAppState,
  716. false,
  717. )
  718. if (!result.canRewind) {
  719. process.stderr.write(`Error: ${result.error || 'Unexpected error'}\n`)
  720. gracefulShutdownSync(1)
  721. return
  722. }
  723. // Rewind complete - exit successfully
  724. process.stdout.write(
  725. `Files rewound to state at message ${options.rewindFiles}\n`,
  726. )
  727. gracefulShutdownSync(0)
  728. return
  729. }
  730. // Check if we need input prompt - skip if we're resuming with a valid session ID/JSONL file or using SDK URL
  731. const hasValidResumeSessionId =
  732. typeof options.resume === 'string' &&
  733. (Boolean(validateUuid(options.resume)) || options.resume.endsWith('.jsonl'))
  734. const isUsingSdkUrl = Boolean(options.sdkUrl)
  735. if (!inputPrompt && !hasValidResumeSessionId && !isUsingSdkUrl) {
  736. process.stderr.write(
  737. `Error: Input must be provided either through stdin or as a prompt argument when using --print\n`,
  738. )
  739. gracefulShutdownSync(1)
  740. return
  741. }
  742. if (options.outputFormat === 'stream-json' && !options.verbose) {
  743. process.stderr.write(
  744. 'Error: When using --print, --output-format=stream-json requires --verbose\n',
  745. )
  746. gracefulShutdownSync(1)
  747. return
  748. }
  749. // Filter out MCP tools that are in the deny list
  750. const allowedMcpTools = filterToolsByDenyRules(
  751. appState.mcp.tools,
  752. appState.toolPermissionContext,
  753. )
  754. let filteredTools = [...tools, ...allowedMcpTools]
  755. // When using SDK URL, always use stdio permission prompting to delegate to the SDK
  756. const effectivePermissionPromptToolName = options.sdkUrl
  757. ? 'stdio'
  758. : options.permissionPromptToolName
  759. // Callback for when a permission prompt is shown
  760. const onPermissionPrompt = (details: RequiresActionDetails) => {
  761. if (feature('COMMIT_ATTRIBUTION')) {
  762. setAppState(prev => ({
  763. ...prev,
  764. attribution: {
  765. ...prev.attribution,
  766. permissionPromptCount: prev.attribution.permissionPromptCount + 1,
  767. },
  768. }))
  769. }
  770. notifySessionStateChanged('requires_action', details)
  771. }
  772. const canUseTool = getCanUseToolFn(
  773. effectivePermissionPromptToolName,
  774. structuredIO,
  775. () => getAppState().mcp.tools,
  776. onPermissionPrompt,
  777. )
  778. if (options.permissionPromptToolName) {
  779. // Remove the permission prompt tool from the list of available tools.
  780. filteredTools = filteredTools.filter(
  781. tool => !toolMatchesName(tool, options.permissionPromptToolName!),
  782. )
  783. }
  784. // Install errors handlers to gracefully handle broken pipes (e.g., when parent process dies)
  785. registerProcessOutputErrorHandlers()
  786. headlessProfilerCheckpoint('after_loadInitialMessages')
  787. // Ensure model strings are initialized before generating model options.
  788. // For Bedrock users, this waits for the profile fetch to get correct region strings.
  789. await ensureModelStringsInitialized()
  790. headlessProfilerCheckpoint('after_modelStrings')
  791. // UDS inbox store registration is deferred until after `run` is defined
  792. // so we can pass `run` as the onEnqueue callback (see below).
  793. // Only `json` + `verbose` needs the full array (jsonStringify(messages) below).
  794. // For stream-json (SDK/CCR) and default text output, only the last message is
  795. // read for the exit code / final result. Avoid accumulating every message in
  796. // memory for the entire session.
  797. const needsFullArray = options.outputFormat === 'json' && options.verbose
  798. const messages: SDKMessage[] = []
  799. let lastMessage: SDKMessage | undefined
  800. // Streamlined mode transforms messages when CLAUDE_CODE_STREAMLINED_OUTPUT=true and using stream-json
  801. // Build flag gates this out of external builds; env var is the runtime opt-in for ant builds
  802. const transformToStreamlined =
  803. feature('STREAMLINED_OUTPUT') &&
  804. isEnvTruthy(process.env.CLAUDE_CODE_STREAMLINED_OUTPUT) &&
  805. options.outputFormat === 'stream-json'
  806. ? createStreamlinedTransformer()
  807. : null
  808. headlessProfilerCheckpoint('before_runHeadlessStreaming')
  809. for await (const message of runHeadlessStreaming(
  810. structuredIO,
  811. appState.mcp.clients,
  812. [...commands, ...appState.mcp.commands],
  813. filteredTools,
  814. initialMessages,
  815. canUseTool,
  816. sdkMcpConfigs,
  817. getAppState,
  818. setAppState,
  819. agents,
  820. options,
  821. turnInterruptionState,
  822. )) {
  823. if (transformToStreamlined) {
  824. // Streamlined mode: transform messages and stream immediately
  825. const transformed = transformToStreamlined(message)
  826. if (transformed) {
  827. await structuredIO.write(transformed)
  828. }
  829. } else if (options.outputFormat === 'stream-json' && options.verbose) {
  830. await structuredIO.write(message)
  831. }
  832. // Should not be getting control messages or stream events in non-stream mode.
  833. // Also filter out streamlined types since they're only produced by the transformer.
  834. // SDK-only system events are excluded so lastMessage stays at the result
  835. // (session_state_changed(idle) and any late task_notification drain after
  836. // result in the finally block).
  837. if (
  838. message.type !== 'control_response' &&
  839. message.type !== 'control_request' &&
  840. message.type !== 'control_cancel_request' &&
  841. !(
  842. message.type === 'system' &&
  843. (message.subtype === 'session_state_changed' ||
  844. message.subtype === 'task_notification' ||
  845. message.subtype === 'task_started' ||
  846. message.subtype === 'task_progress' ||
  847. message.subtype === 'post_turn_summary')
  848. ) &&
  849. message.type !== 'stream_event' &&
  850. message.type !== 'keep_alive' &&
  851. message.type !== 'streamlined_text' &&
  852. message.type !== 'streamlined_tool_use_summary' &&
  853. message.type !== 'prompt_suggestion'
  854. ) {
  855. if (needsFullArray) {
  856. messages.push(message)
  857. }
  858. lastMessage = message
  859. }
  860. }
  861. switch (options.outputFormat) {
  862. case 'json':
  863. if (!lastMessage || lastMessage.type !== 'result') {
  864. throw new Error('No messages returned')
  865. }
  866. if (options.verbose) {
  867. writeToStdout(jsonStringify(messages) + '\n')
  868. break
  869. }
  870. writeToStdout(jsonStringify(lastMessage) + '\n')
  871. break
  872. case 'stream-json':
  873. // already logged above
  874. break
  875. default:
  876. if (!lastMessage || lastMessage.type !== 'result') {
  877. throw new Error('No messages returned')
  878. }
  879. switch (lastMessage.subtype) {
  880. case 'success':
  881. writeToStdout(
  882. (lastMessage.result as string).endsWith('\n')
  883. ? (lastMessage.result as string)
  884. : (lastMessage.result as string) + '\n',
  885. )
  886. break
  887. case 'error_during_execution':
  888. writeToStdout(`Execution error`)
  889. break
  890. case 'error_max_turns':
  891. writeToStdout(`Error: Reached max turns (${options.maxTurns})`)
  892. break
  893. case 'error_max_budget_usd':
  894. writeToStdout(`Error: Exceeded USD budget (${options.maxBudgetUsd})`)
  895. break
  896. case 'error_max_structured_output_retries':
  897. writeToStdout(
  898. `Error: Failed to provide valid structured output after maximum retries`,
  899. )
  900. }
  901. }
  902. // Log headless latency metrics for the final turn
  903. logHeadlessProfilerTurn()
  904. // Drain any in-flight memory extraction before shutdown. The response is
  905. // already flushed above, so this adds no user-visible latency — it just
  906. // delays process exit so gracefulShutdownSync's 5s failsafe doesn't kill
  907. // the forked agent mid-flight. Gated by isExtractModeActive so the
  908. // tengu_slate_thimble flag controls non-interactive extraction end-to-end.
  909. if (feature('EXTRACT_MEMORIES') && isExtractModeActive()) {
  910. await extractMemoriesModule!.drainPendingExtraction()
  911. }
  912. gracefulShutdownSync(
  913. lastMessage?.type === 'result' && lastMessage?.is_error ? 1 : 0,
  914. )
  915. }
  916. function runHeadlessStreaming(
  917. structuredIO: StructuredIO,
  918. mcpClients: MCPServerConnection[],
  919. commands: Command[],
  920. tools: Tools,
  921. initialMessages: Message[],
  922. canUseTool: CanUseToolFn,
  923. sdkMcpConfigs: Record<string, McpSdkServerConfig>,
  924. getAppState: () => AppState,
  925. setAppState: (f: (prev: AppState) => AppState) => void,
  926. agents: AgentDefinition[],
  927. options: {
  928. verbose: boolean | undefined
  929. jsonSchema: Record<string, unknown> | undefined
  930. permissionPromptToolName: string | undefined
  931. allowedTools: string[] | undefined
  932. thinkingConfig: ThinkingConfig | undefined
  933. maxTurns: number | undefined
  934. maxBudgetUsd: number | undefined
  935. taskBudget: { total: number } | undefined
  936. systemPrompt: string | undefined
  937. appendSystemPrompt: string | undefined
  938. userSpecifiedModel: string | undefined
  939. fallbackModel: string | undefined
  940. replayUserMessages?: boolean | undefined
  941. includePartialMessages?: boolean | undefined
  942. enableAuthStatus?: boolean | undefined
  943. agent?: string | undefined
  944. setSDKStatus?: (status: SDKStatus) => void
  945. promptSuggestions?: boolean | undefined
  946. workload?: string | undefined
  947. },
  948. turnInterruptionState?: TurnInterruptionState,
  949. ): AsyncIterable<StdoutMessage> {
  950. let running = false
  951. let runPhase:
  952. | 'draining_commands'
  953. | 'waiting_for_agents'
  954. | 'finally_flush'
  955. | 'finally_post_flush'
  956. | undefined
  957. let inputClosed = false
  958. let shutdownPromptInjected = false
  959. let heldBackResult: StdoutMessage | null = null
  960. let abortController: AbortController | undefined
  961. // Same queue sendRequest() enqueues to — one FIFO for everything.
  962. const output = structuredIO.outbound
  963. // Ctrl+C in -p mode: abort the in-flight query, then shut down gracefully.
  964. // gracefulShutdown persists session state and flushes analytics, with a
  965. // failsafe timer that force-exits if cleanup hangs.
  966. const sigintHandler = () => {
  967. logForDiagnosticsNoPII('info', 'shutdown_signal', { signal: 'SIGINT' })
  968. if (abortController && !abortController.signal.aborted) {
  969. abortController.abort()
  970. }
  971. void gracefulShutdown(0)
  972. }
  973. process.on('SIGINT', sigintHandler)
  974. // Dump run()'s state at SIGTERM so a stuck session's healthsweep can name
  975. // the do/while(waitingForAgents) poll without reading the transcript.
  976. registerCleanup(async () => {
  977. const bg: Record<string, number> = {}
  978. for (const t of getRunningTasks(getAppState())) {
  979. if (isBackgroundTask(t)) bg[t.type] = (bg[t.type] ?? 0) + 1
  980. }
  981. logForDiagnosticsNoPII('info', 'run_state_at_shutdown', {
  982. run_active: running,
  983. run_phase: runPhase,
  984. worker_status: getSessionState(),
  985. internal_events_pending: structuredIO.internalEventsPending,
  986. bg_tasks: bg,
  987. })
  988. })
  989. // Wire the central onChangeAppState mode-diff hook to the SDK output stream.
  990. // This fires whenever ANY code path mutates toolPermissionContext.mode —
  991. // Shift+Tab, ExitPlanMode dialog, /plan slash command, rewind, bridge
  992. // set_permission_mode, the query loop, stop_task — rather than the two
  993. // paths that previously went through a bespoke wrapper.
  994. // The wrapper's body was fully redundant (it enqueued here AND called
  995. // notifySessionMetadataChanged, both of which onChangeAppState now covers);
  996. // keeping it would double-emit status messages.
  997. setPermissionModeChangedListener(newMode => {
  998. // Only emit for SDK-exposed modes.
  999. if (
  1000. newMode === 'default' ||
  1001. newMode === 'acceptEdits' ||
  1002. newMode === 'bypassPermissions' ||
  1003. newMode === 'plan' ||
  1004. newMode === (feature('TRANSCRIPT_CLASSIFIER') && 'auto') ||
  1005. newMode === 'dontAsk'
  1006. ) {
  1007. output.enqueue({
  1008. type: 'system',
  1009. subtype: 'status',
  1010. status: null,
  1011. permissionMode: newMode as PermissionMode,
  1012. uuid: randomUUID(),
  1013. session_id: getSessionId(),
  1014. })
  1015. }
  1016. })
  1017. // Prompt suggestion tracking (push model)
  1018. const suggestionState: {
  1019. abortController: AbortController | null
  1020. inflightPromise: Promise<void> | null
  1021. lastEmitted: {
  1022. text: string
  1023. emittedAt: number
  1024. promptId: PromptVariant
  1025. generationRequestId: string | null
  1026. } | null
  1027. pendingSuggestion: {
  1028. type: 'prompt_suggestion'
  1029. suggestion: string
  1030. uuid: UUID
  1031. session_id: string
  1032. } | null
  1033. pendingLastEmittedEntry: {
  1034. text: string
  1035. promptId: PromptVariant
  1036. generationRequestId: string | null
  1037. } | null
  1038. } = {
  1039. abortController: null,
  1040. inflightPromise: null,
  1041. lastEmitted: null,
  1042. pendingSuggestion: null,
  1043. pendingLastEmittedEntry: null,
  1044. }
  1045. // Set up AWS auth status listener if enabled
  1046. let unsubscribeAuthStatus: (() => void) | undefined
  1047. if (options.enableAuthStatus) {
  1048. const authStatusManager = AwsAuthStatusManager.getInstance()
  1049. unsubscribeAuthStatus = authStatusManager.subscribe(status => {
  1050. output.enqueue({
  1051. type: 'auth_status',
  1052. isAuthenticating: status.isAuthenticating,
  1053. output: status.output,
  1054. error: status.error,
  1055. uuid: randomUUID(),
  1056. session_id: getSessionId(),
  1057. })
  1058. })
  1059. }
  1060. // Set up rate limit status listener to emit SDKRateLimitEvent for all status changes.
  1061. // Emitting for all statuses (including 'allowed') ensures consumers can clear warnings
  1062. // when rate limits reset. The upstream emitStatusChange already deduplicates via isEqual.
  1063. const rateLimitListener = (limits: ClaudeAILimits) => {
  1064. const rateLimitInfo = toSDKRateLimitInfo(limits)
  1065. if (rateLimitInfo) {
  1066. output.enqueue({
  1067. type: 'rate_limit_event',
  1068. rate_limit_info: rateLimitInfo,
  1069. uuid: randomUUID(),
  1070. session_id: getSessionId(),
  1071. })
  1072. }
  1073. }
  1074. statusListeners.add(rateLimitListener)
  1075. // Messages for internal tracking, directly mutated by ask(). These messages
  1076. // include Assistant, User, Attachment, and Progress messages.
  1077. // TODO: Clean up this code to avoid passing around a mutable array.
  1078. const mutableMessages: Message[] = initialMessages
  1079. // Seed the readFileState cache from the transcript (content the model saw,
  1080. // with message timestamps) so getChangedFiles can detect external edits.
  1081. // This cache instance must persist across ask() calls, since the edit tool
  1082. // relies on this as a global state.
  1083. let readFileState = extractReadFilesFromMessages(
  1084. initialMessages,
  1085. cwd(),
  1086. READ_FILE_STATE_CACHE_SIZE,
  1087. )
  1088. // Client-supplied readFileState seeds (via seed_read_state control request).
  1089. // The stdin IIFE runs concurrently with ask() — a seed arriving mid-turn
  1090. // would be lost to ask()'s clone-then-replace (QueryEngine.ts finally block)
  1091. // if written directly into readFileState. Instead, seeds land here, merge
  1092. // into getReadFileCache's view (readFileState-wins-ties: seeds fill gaps),
  1093. // and are re-applied then CLEARED in setReadFileCache. One-shot: each seed
  1094. // survives exactly one clone-replace cycle, then becomes a regular
  1095. // readFileState entry subject to compact's clear like everything else.
  1096. const pendingSeeds = createFileStateCacheWithSizeLimit(
  1097. READ_FILE_STATE_CACHE_SIZE,
  1098. )
  1099. // Auto-resume interrupted turns on restart so CC continues from where it
  1100. // left off without requiring the SDK to re-send the prompt.
  1101. const resumeInterruptedTurnEnv =
  1102. process.env.CLAUDE_CODE_RESUME_INTERRUPTED_TURN
  1103. if (
  1104. turnInterruptionState &&
  1105. turnInterruptionState.kind !== 'none' &&
  1106. resumeInterruptedTurnEnv
  1107. ) {
  1108. logForDebugging(
  1109. `[print.ts] Auto-resuming interrupted turn (kind: ${turnInterruptionState.kind})`,
  1110. )
  1111. // Remove the interrupted message and its sentinel, then re-enqueue so
  1112. // the model sees it exactly once. For mid-turn interruptions, the
  1113. // deserialization layer transforms them into interrupted_prompt by
  1114. // appending a synthetic "Continue from where you left off." message.
  1115. removeInterruptedMessage(mutableMessages, turnInterruptionState.message)
  1116. enqueue({
  1117. mode: 'prompt',
  1118. value: turnInterruptionState.message.message.content,
  1119. uuid: randomUUID(),
  1120. })
  1121. }
  1122. const modelOptions = getModelOptions()
  1123. const modelInfos = modelOptions.map(option => {
  1124. const modelId = option.value === null ? 'default' : option.value
  1125. const resolvedModel =
  1126. modelId === 'default'
  1127. ? getDefaultMainLoopModel()
  1128. : parseUserSpecifiedModel(modelId)
  1129. const hasEffort = modelSupportsEffort(resolvedModel)
  1130. const hasAdaptiveThinking = modelSupportsAdaptiveThinking(resolvedModel)
  1131. const hasFastMode = isFastModeSupportedByModel(option.value)
  1132. const hasAutoMode = modelSupportsAutoMode(resolvedModel)
  1133. return {
  1134. name: modelId,
  1135. value: modelId,
  1136. displayName: option.label,
  1137. description: option.description,
  1138. ...(hasEffort && {
  1139. supportsEffort: true,
  1140. supportedEffortLevels: modelSupportsMaxEffort(resolvedModel)
  1141. ? [...EFFORT_LEVELS]
  1142. : EFFORT_LEVELS.filter(l => l !== 'max'),
  1143. }),
  1144. ...(hasAdaptiveThinking && { supportsAdaptiveThinking: true }),
  1145. ...(hasFastMode && { supportsFastMode: true }),
  1146. ...(hasAutoMode && { supportsAutoMode: true }),
  1147. }
  1148. })
  1149. let activeUserSpecifiedModel = options.userSpecifiedModel
  1150. function injectModelSwitchBreadcrumbs(
  1151. modelArg: string,
  1152. resolvedModel: string,
  1153. ): void {
  1154. const breadcrumbs = createModelSwitchBreadcrumbs(
  1155. modelArg,
  1156. modelDisplayString(resolvedModel),
  1157. )
  1158. mutableMessages.push(...breadcrumbs)
  1159. for (const crumb of breadcrumbs) {
  1160. if (
  1161. typeof crumb.message.content === 'string' &&
  1162. crumb.message.content.includes(`<${LOCAL_COMMAND_STDOUT_TAG}>`)
  1163. ) {
  1164. output.enqueue({
  1165. type: 'user',
  1166. content: crumb.message.content,
  1167. message: crumb.message,
  1168. session_id: getSessionId(),
  1169. parent_tool_use_id: null,
  1170. uuid: crumb.uuid,
  1171. timestamp: crumb.timestamp,
  1172. isReplay: true,
  1173. } as SDKUserMessageReplay)
  1174. }
  1175. }
  1176. }
  1177. // Cache SDK MCP clients to avoid reconnecting on each run
  1178. let sdkClients: MCPServerConnection[] = []
  1179. let sdkTools: Tools = []
  1180. // Track which MCP clients have had elicitation handlers registered
  1181. const elicitationRegistered = new Set<string>()
  1182. /**
  1183. * Register elicitation request/completion handlers on connected MCP clients
  1184. * that haven't been registered yet. SDK MCP servers are excluded because they
  1185. * route through SdkControlClientTransport. Hooks run first (matching REPL
  1186. * behavior); if no hook responds, the request is forwarded to the SDK
  1187. * consumer via the control protocol.
  1188. */
  1189. function registerElicitationHandlers(clients: MCPServerConnection[]): void {
  1190. for (const connection of clients) {
  1191. if (
  1192. connection.type !== 'connected' ||
  1193. elicitationRegistered.has(connection.name)
  1194. ) {
  1195. continue
  1196. }
  1197. // Skip SDK MCP servers — elicitation flows through SdkControlClientTransport
  1198. if (connection.config.type === 'sdk') {
  1199. continue
  1200. }
  1201. const serverName = connection.name
  1202. // Wrapped in try/catch because setRequestHandler throws if the client wasn't
  1203. // created with elicitation capability declared (e.g., SDK-created clients).
  1204. try {
  1205. connection.client.setRequestHandler(
  1206. ElicitRequestSchema,
  1207. async (request, extra) => {
  1208. logMCPDebug(
  1209. serverName,
  1210. `Elicitation request received in print mode: ${jsonStringify(request)}`,
  1211. )
  1212. const mode = request.params.mode === 'url' ? 'url' : 'form'
  1213. logEvent('tengu_mcp_elicitation_shown', {
  1214. mode: mode as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  1215. })
  1216. // Run elicitation hooks first — they can provide a response programmatically
  1217. const hookResponse = await runElicitationHooks(
  1218. serverName,
  1219. request.params,
  1220. extra.signal,
  1221. )
  1222. if (hookResponse) {
  1223. logMCPDebug(
  1224. serverName,
  1225. `Elicitation resolved by hook: ${jsonStringify(hookResponse)}`,
  1226. )
  1227. logEvent('tengu_mcp_elicitation_response', {
  1228. mode: mode as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  1229. action:
  1230. hookResponse.action as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  1231. })
  1232. return hookResponse
  1233. }
  1234. // Delegate to SDK consumer via control protocol
  1235. const url =
  1236. 'url' in request.params
  1237. ? (request.params.url as string)
  1238. : undefined
  1239. const requestedSchema =
  1240. 'requestedSchema' in request.params
  1241. ? (request.params.requestedSchema as
  1242. | Record<string, unknown>
  1243. | undefined)
  1244. : undefined
  1245. const elicitationId =
  1246. 'elicitationId' in request.params
  1247. ? (request.params.elicitationId as string | undefined)
  1248. : undefined
  1249. const rawResult = await structuredIO.handleElicitation(
  1250. serverName,
  1251. request.params.message,
  1252. requestedSchema,
  1253. extra.signal,
  1254. mode,
  1255. url,
  1256. elicitationId,
  1257. )
  1258. const result = await runElicitationResultHooks(
  1259. serverName,
  1260. rawResult,
  1261. extra.signal,
  1262. mode,
  1263. elicitationId,
  1264. )
  1265. logEvent('tengu_mcp_elicitation_response', {
  1266. mode: mode as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  1267. action:
  1268. result.action as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  1269. })
  1270. return result
  1271. },
  1272. )
  1273. // Surface completion notifications to SDK consumers (URL mode)
  1274. connection.client.setNotificationHandler(
  1275. ElicitationCompleteNotificationSchema,
  1276. notification => {
  1277. const { elicitationId } = notification.params
  1278. logMCPDebug(
  1279. serverName,
  1280. `Elicitation completion notification: ${elicitationId}`,
  1281. )
  1282. void executeNotificationHooks({
  1283. message: `MCP server "${serverName}" confirmed elicitation ${elicitationId} complete`,
  1284. notificationType: 'elicitation_complete',
  1285. })
  1286. output.enqueue({
  1287. type: 'system',
  1288. subtype: 'elicitation_complete',
  1289. mcp_server_name: serverName,
  1290. elicitation_id: elicitationId,
  1291. uuid: randomUUID(),
  1292. session_id: getSessionId(),
  1293. })
  1294. },
  1295. )
  1296. elicitationRegistered.add(serverName)
  1297. } catch {
  1298. // setRequestHandler throws if the client wasn't created with
  1299. // elicitation capability — skip silently
  1300. }
  1301. }
  1302. }
  1303. async function updateSdkMcp() {
  1304. // Check if SDK MCP servers need to be updated (new servers added or removed)
  1305. const currentServerNames = new Set(Object.keys(sdkMcpConfigs))
  1306. const connectedServerNames = new Set(sdkClients.map(c => c.name))
  1307. // Check if there are any differences (additions or removals)
  1308. const hasNewServers = Array.from(currentServerNames).some(
  1309. name => !connectedServerNames.has(name),
  1310. )
  1311. const hasRemovedServers = Array.from(connectedServerNames).some(
  1312. name => !currentServerNames.has(name),
  1313. )
  1314. // Check if any SDK clients are pending and need to be upgraded
  1315. const hasPendingSdkClients = sdkClients.some(c => c.type === 'pending')
  1316. // Check if any SDK clients failed their handshake and need to be retried.
  1317. // Without this, a client that lands in 'failed' (e.g. handshake timeout on
  1318. // a WS reconnect race) stays failed forever — its name satisfies the
  1319. // connectedServerNames diff but it contributes zero tools.
  1320. const hasFailedSdkClients = sdkClients.some(c => c.type === 'failed')
  1321. const haveServersChanged =
  1322. hasNewServers ||
  1323. hasRemovedServers ||
  1324. hasPendingSdkClients ||
  1325. hasFailedSdkClients
  1326. if (haveServersChanged) {
  1327. // Clean up removed servers
  1328. for (const client of sdkClients) {
  1329. if (!currentServerNames.has(client.name)) {
  1330. if (client.type === 'connected') {
  1331. await client.cleanup()
  1332. }
  1333. }
  1334. }
  1335. // Re-initialize all SDK MCP servers with current config
  1336. const sdkSetup = await setupSdkMcpClients(
  1337. sdkMcpConfigs,
  1338. (serverName, message) =>
  1339. structuredIO.sendMcpMessage(serverName, message),
  1340. )
  1341. sdkClients = sdkSetup.clients
  1342. sdkTools = sdkSetup.tools
  1343. // Store SDK MCP tools in appState so subagents can access them via
  1344. // assembleToolPool. Only tools are stored here — SDK clients are already
  1345. // merged separately in the query loop (allMcpClients) and mcp_status handler.
  1346. // Use both old (connectedServerNames) and new (currentServerNames) to remove
  1347. // stale SDK tools when servers are added or removed.
  1348. const allSdkNames = uniq([...connectedServerNames, ...currentServerNames])
  1349. setAppState(prev => ({
  1350. ...prev,
  1351. mcp: {
  1352. ...prev.mcp,
  1353. tools: [
  1354. ...prev.mcp.tools.filter(
  1355. t =>
  1356. !allSdkNames.some(name =>
  1357. t.name.startsWith(getMcpPrefix(name)),
  1358. ),
  1359. ),
  1360. ...sdkTools,
  1361. ],
  1362. },
  1363. }))
  1364. // Set up the special internal VSCode MCP server if necessary.
  1365. setupVscodeSdkMcp(sdkClients)
  1366. }
  1367. }
  1368. void updateSdkMcp()
  1369. // State for dynamically added MCP servers (via mcp_set_servers control message)
  1370. // These are separate from SDK MCP servers and support all transport types
  1371. let dynamicMcpState: DynamicMcpState = {
  1372. clients: [],
  1373. tools: [],
  1374. configs: {},
  1375. }
  1376. // Shared tool assembly for ask() and the get_context_usage control request.
  1377. // Closes over the mutable sdkTools/dynamicMcpState bindings so both call
  1378. // sites see late-connecting servers.
  1379. const buildAllTools = (appState: AppState): Tools => {
  1380. const assembledTools = assembleToolPool(
  1381. appState.toolPermissionContext,
  1382. appState.mcp.tools,
  1383. )
  1384. let allTools = uniqBy(
  1385. mergeAndFilterTools(
  1386. [...tools, ...sdkTools, ...dynamicMcpState.tools],
  1387. assembledTools,
  1388. appState.toolPermissionContext.mode,
  1389. ),
  1390. 'name',
  1391. )
  1392. if (options.permissionPromptToolName) {
  1393. allTools = allTools.filter(
  1394. tool => !toolMatchesName(tool, options.permissionPromptToolName!),
  1395. )
  1396. }
  1397. const initJsonSchema = getInitJsonSchema()
  1398. if (initJsonSchema && !options.jsonSchema) {
  1399. const syntheticOutputResult = createSyntheticOutputTool(initJsonSchema)
  1400. if ('tool' in syntheticOutputResult) {
  1401. allTools = [...allTools, syntheticOutputResult.tool]
  1402. }
  1403. }
  1404. return allTools
  1405. }
  1406. // Bridge handle for remote-control (SDK control message).
  1407. // Mirrors the REPL's useReplBridge hook: the handle is created when
  1408. // `remote_control` is enabled and torn down when disabled.
  1409. let bridgeHandle: ReplBridgeHandle | null = null
  1410. // Cursor into mutableMessages — tracks how far we've forwarded.
  1411. // Same index-based diff as useReplBridge's lastWrittenIndexRef.
  1412. let bridgeLastForwardedIndex = 0
  1413. // Forward new messages from mutableMessages to the bridge.
  1414. // Called incrementally during each turn (so claude.ai sees progress
  1415. // and stays alive during permission waits) and again after the turn.
  1416. //
  1417. // writeMessages has its own UUID-based dedup (initialMessageUUIDs,
  1418. // recentPostedUUIDs) — the index cursor here is a pre-filter to avoid
  1419. // O(n) re-scanning of already-sent messages on every call.
  1420. function forwardMessagesToBridge(): void {
  1421. if (!bridgeHandle) return
  1422. // Guard against mutableMessages shrinking (compaction truncates it).
  1423. const startIndex = Math.min(
  1424. bridgeLastForwardedIndex,
  1425. mutableMessages.length,
  1426. )
  1427. const newMessages = mutableMessages
  1428. .slice(startIndex)
  1429. .filter(m => m.type === 'user' || m.type === 'assistant')
  1430. bridgeLastForwardedIndex = mutableMessages.length
  1431. if (newMessages.length > 0) {
  1432. bridgeHandle.writeMessages(newMessages)
  1433. }
  1434. }
  1435. // Helper to apply MCP server changes - used by both mcp_set_servers control message
  1436. // and background plugin installation.
  1437. // NOTE: Nested function required - mutates closure state (sdkMcpConfigs, sdkClients, etc.)
  1438. let mcpChangesPromise: Promise<{
  1439. response: SDKControlMcpSetServersResponse
  1440. sdkServersChanged: boolean
  1441. }> = Promise.resolve({
  1442. response: {
  1443. added: [] as string[],
  1444. removed: [] as string[],
  1445. errors: {} as Record<string, string>,
  1446. },
  1447. sdkServersChanged: false,
  1448. })
  1449. function applyMcpServerChanges(
  1450. servers: Record<string, McpServerConfigForProcessTransport>,
  1451. ): Promise<{
  1452. response: SDKControlMcpSetServersResponse
  1453. sdkServersChanged: boolean
  1454. }> {
  1455. // Serialize calls to prevent race conditions between concurrent callers
  1456. // (background plugin install and mcp_set_servers control messages)
  1457. const doWork = async (): Promise<{
  1458. response: SDKControlMcpSetServersResponse
  1459. sdkServersChanged: boolean
  1460. }> => {
  1461. const oldSdkClientNames = new Set(sdkClients.map(c => c.name))
  1462. const result = await handleMcpSetServers(
  1463. servers,
  1464. { configs: sdkMcpConfigs, clients: sdkClients, tools: sdkTools },
  1465. dynamicMcpState,
  1466. setAppState,
  1467. )
  1468. // Update SDK state (need to mutate sdkMcpConfigs since it's shared)
  1469. for (const key of Object.keys(sdkMcpConfigs)) {
  1470. delete sdkMcpConfigs[key]
  1471. }
  1472. Object.assign(sdkMcpConfigs, result.newSdkState.configs)
  1473. sdkClients = result.newSdkState.clients
  1474. sdkTools = result.newSdkState.tools
  1475. dynamicMcpState = result.newDynamicState
  1476. // Keep appState.mcp.tools in sync so subagents can see SDK MCP tools.
  1477. // Use both old and new SDK client names to remove stale tools.
  1478. if (result.sdkServersChanged) {
  1479. const newSdkClientNames = new Set(sdkClients.map(c => c.name))
  1480. const allSdkNames = uniq([...oldSdkClientNames, ...newSdkClientNames])
  1481. setAppState(prev => ({
  1482. ...prev,
  1483. mcp: {
  1484. ...prev.mcp,
  1485. tools: [
  1486. ...prev.mcp.tools.filter(
  1487. t =>
  1488. !allSdkNames.some(name =>
  1489. t.name.startsWith(getMcpPrefix(name)),
  1490. ),
  1491. ),
  1492. ...sdkTools,
  1493. ],
  1494. },
  1495. }))
  1496. }
  1497. return {
  1498. response: result.response,
  1499. sdkServersChanged: result.sdkServersChanged,
  1500. }
  1501. }
  1502. mcpChangesPromise = mcpChangesPromise.then(doWork, doWork)
  1503. return mcpChangesPromise
  1504. }
  1505. // Build McpServerStatus[] for control responses. Shared by mcp_status and
  1506. // reload_plugins handlers. Reads closure state: sdkClients, dynamicMcpState.
  1507. function buildMcpServerStatuses(): McpServerStatus[] {
  1508. const currentAppState = getAppState()
  1509. const currentMcpClients = currentAppState.mcp.clients
  1510. const allMcpTools = uniqBy(
  1511. [...currentAppState.mcp.tools, ...dynamicMcpState.tools],
  1512. 'name',
  1513. )
  1514. const existingNames = new Set([
  1515. ...currentMcpClients.map(c => c.name),
  1516. ...sdkClients.map(c => c.name),
  1517. ])
  1518. return [
  1519. ...currentMcpClients,
  1520. ...sdkClients,
  1521. ...dynamicMcpState.clients.filter(c => !existingNames.has(c.name)),
  1522. ].map(connection => {
  1523. let config
  1524. if (
  1525. connection.config.type === 'sse' ||
  1526. connection.config.type === 'http'
  1527. ) {
  1528. config = {
  1529. type: connection.config.type,
  1530. url: connection.config.url,
  1531. headers: connection.config.headers,
  1532. oauth: connection.config.oauth,
  1533. }
  1534. } else if (connection.config.type === 'claudeai-proxy') {
  1535. config = {
  1536. type: 'claudeai-proxy' as const,
  1537. url: connection.config.url,
  1538. id: connection.config.id,
  1539. }
  1540. } else if (
  1541. connection.config.type === 'stdio' ||
  1542. connection.config.type === undefined
  1543. ) {
  1544. const stdioConfig = connection.config as { command: string; args: string[] }
  1545. config = {
  1546. type: 'stdio' as const,
  1547. command: stdioConfig.command,
  1548. args: stdioConfig.args,
  1549. }
  1550. }
  1551. const serverTools =
  1552. connection.type === 'connected'
  1553. ? filterToolsByServer(allMcpTools, connection.name).map(tool => ({
  1554. name: tool.mcpInfo?.toolName ?? tool.name,
  1555. annotations: {
  1556. readOnly: tool.isReadOnly({}) || undefined,
  1557. destructive: tool.isDestructive?.({}) || undefined,
  1558. openWorld: tool.isOpenWorld?.({}) || undefined,
  1559. },
  1560. }))
  1561. : undefined
  1562. // Capabilities passthrough with allowlist pre-filter. The IDE reads
  1563. // experimental['claude/channel'] to decide whether to show the
  1564. // Enable-channel prompt — only echo it if channel_enable would
  1565. // actually pass the allowlist. Not a security boundary (the
  1566. // handler re-runs the full gate); just avoids dead buttons.
  1567. let capabilities: { experimental?: Record<string, unknown> } | undefined
  1568. if (
  1569. (feature('KAIROS') || feature('KAIROS_CHANNELS')) &&
  1570. connection.type === 'connected' &&
  1571. connection.capabilities.experimental
  1572. ) {
  1573. const exp = { ...connection.capabilities.experimental }
  1574. if (
  1575. exp['claude/channel'] &&
  1576. (!isChannelsEnabled() ||
  1577. !isChannelAllowlisted(connection.config.pluginSource))
  1578. ) {
  1579. delete exp['claude/channel']
  1580. }
  1581. if (Object.keys(exp).length > 0) {
  1582. capabilities = { experimental: exp }
  1583. }
  1584. }
  1585. return {
  1586. name: connection.name,
  1587. status: connection.type as McpServerStatus['status'],
  1588. serverInfo:
  1589. connection.type === 'connected' ? connection.serverInfo : undefined,
  1590. error: connection.type === 'failed' ? connection.error : undefined,
  1591. config,
  1592. scope: connection.config.scope,
  1593. tools: serverTools,
  1594. capabilities,
  1595. }
  1596. }) as McpServerStatus[]
  1597. }
  1598. // NOTE: Nested function required - needs closure access to applyMcpServerChanges and updateSdkMcp
  1599. async function installPluginsAndApplyMcpInBackground(): Promise<void> {
  1600. try {
  1601. // Join point for user settings (fired at runHeadless entry) and managed
  1602. // settings (fired in main.tsx preAction). downloadUserSettings() caches
  1603. // its promise so this awaits the same in-flight request.
  1604. await Promise.all([
  1605. feature('DOWNLOAD_USER_SETTINGS') &&
  1606. (isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) || getIsRemoteMode())
  1607. ? withDiagnosticsTiming('headless_user_settings_download', () =>
  1608. downloadUserSettings(),
  1609. )
  1610. : Promise.resolve(),
  1611. withDiagnosticsTiming('headless_managed_settings_wait', () =>
  1612. waitForRemoteManagedSettingsToLoad(),
  1613. ),
  1614. ])
  1615. const pluginsInstalled = await installPluginsForHeadless()
  1616. if (pluginsInstalled) {
  1617. await applyPluginMcpDiff()
  1618. }
  1619. } catch (error) {
  1620. logError(error)
  1621. }
  1622. }
  1623. // Background plugin installation for all headless users
  1624. // Installs marketplaces from extraKnownMarketplaces and missing enabled plugins
  1625. // CLAUDE_CODE_SYNC_PLUGIN_INSTALL=true: resolved in run() before the first
  1626. // query so plugins are guaranteed available on the first ask().
  1627. let pluginInstallPromise: Promise<void> | null = null
  1628. // --bare / SIMPLE: skip plugin install. Scripted calls don't add plugins
  1629. // mid-session; the next interactive run reconciles.
  1630. if (!isBareMode()) {
  1631. if (isEnvTruthy(process.env.CLAUDE_CODE_SYNC_PLUGIN_INSTALL)) {
  1632. pluginInstallPromise = installPluginsAndApplyMcpInBackground()
  1633. } else {
  1634. void installPluginsAndApplyMcpInBackground()
  1635. }
  1636. }
  1637. // Idle timeout management
  1638. const idleTimeout = createIdleTimeoutManager(() => !running)
  1639. // Mutable commands and agents for hot reloading
  1640. let currentCommands = commands
  1641. let currentAgents = agents
  1642. // Clear all plugin-related caches, reload commands/agents/hooks.
  1643. // Called after CLAUDE_CODE_SYNC_PLUGIN_INSTALL completes (before first query)
  1644. // and after non-sync background install finishes.
  1645. // refreshActivePlugins calls clearAllCaches() which is required because
  1646. // loadAllPlugins() may have run during main.tsx startup BEFORE managed
  1647. // settings were fetched. Without clearing, getCommands() would rebuild
  1648. // from a stale plugin list.
  1649. async function refreshPluginState(): Promise<void> {
  1650. // refreshActivePlugins handles the full cache sweep (clearAllCaches),
  1651. // reloads all plugin component loaders, writes AppState.plugins +
  1652. // AppState.agentDefinitions, registers hooks, and bumps mcp.pluginReconnectKey.
  1653. const { agentDefinitions: freshAgentDefs } =
  1654. await refreshActivePlugins(setAppState)
  1655. // Headless-specific: currentCommands/currentAgents are local mutable refs
  1656. // captured by the query loop (REPL uses AppState instead). getCommands is
  1657. // fresh because refreshActivePlugins cleared its cache.
  1658. currentCommands = await getCommands(cwd())
  1659. // Preserve SDK-provided agents (--agents CLI flag or SDK initialize
  1660. // control_request) — both inject via parseAgentsFromJson with
  1661. // source='flagSettings'. loadMarkdownFilesForSubdir never assigns this
  1662. // source, so it cleanly discriminates "injected, not disk-loadable".
  1663. //
  1664. // The previous filter used a negative set-diff (!freshAgentTypes.has(a))
  1665. // which also matched plugin agents that were in the poisoned initial
  1666. // currentAgents but correctly excluded from freshAgentDefs after managed
  1667. // settings applied — leaking policy-blocked agents into the init message.
  1668. // See gh-23085: isBridgeEnabled() at Commander-definition time poisoned
  1669. // the settings cache before setEligibility(true) ran.
  1670. const sdkAgents = currentAgents.filter(a => a.source === 'flagSettings')
  1671. currentAgents = [...freshAgentDefs.allAgents, ...sdkAgents]
  1672. }
  1673. // Re-diff MCP configs after plugin state changes. Filters to
  1674. // process-transport-supported types and carries SDK-mode servers through
  1675. // so applyMcpServerChanges' diff doesn't close their transports.
  1676. // Nested: needs closure access to sdkMcpConfigs, applyMcpServerChanges,
  1677. // updateSdkMcp.
  1678. async function applyPluginMcpDiff(): Promise<void> {
  1679. const { servers: newConfigs } = await getAllMcpConfigs()
  1680. const supportedConfigs: Record<string, McpServerConfigForProcessTransport> =
  1681. {}
  1682. for (const [name, config] of Object.entries(newConfigs)) {
  1683. const type = config.type
  1684. if (
  1685. type === undefined ||
  1686. type === 'stdio' ||
  1687. type === 'sse' ||
  1688. type === 'http' ||
  1689. type === 'sdk'
  1690. ) {
  1691. supportedConfigs[name] = config as McpServerConfigForProcessTransport
  1692. }
  1693. }
  1694. for (const [name, config] of Object.entries(sdkMcpConfigs)) {
  1695. if (config.type === 'sdk' && !(name in supportedConfigs)) {
  1696. supportedConfigs[name] = config as unknown as McpServerConfigForProcessTransport
  1697. }
  1698. }
  1699. const { response, sdkServersChanged } =
  1700. await applyMcpServerChanges(supportedConfigs)
  1701. if (sdkServersChanged) {
  1702. void updateSdkMcp()
  1703. }
  1704. logForDebugging(
  1705. `Headless MCP refresh: added=${response.added.length}, removed=${response.removed.length}`,
  1706. )
  1707. }
  1708. // Subscribe to skill changes for hot reloading
  1709. const unsubscribeSkillChanges = skillChangeDetector.subscribe(() => {
  1710. clearCommandsCache()
  1711. void getCommands(cwd()).then(newCommands => {
  1712. currentCommands = newCommands
  1713. })
  1714. })
  1715. // Proactive mode: schedule a tick to keep the model looping autonomously.
  1716. // setTimeout(0) yields to the event loop so pending stdin messages
  1717. // (interrupts, user messages) are processed before the tick fires.
  1718. const scheduleProactiveTick =
  1719. feature('PROACTIVE') || feature('KAIROS')
  1720. ? () => {
  1721. setTimeout(() => {
  1722. if (
  1723. !proactiveModule?.isProactiveActive() ||
  1724. proactiveModule.isProactivePaused() ||
  1725. inputClosed
  1726. ) {
  1727. return
  1728. }
  1729. const tickContent = `<${TICK_TAG}>${new Date().toLocaleTimeString()}</${TICK_TAG}>`
  1730. enqueue({
  1731. mode: 'prompt' as const,
  1732. value: tickContent,
  1733. uuid: randomUUID(),
  1734. priority: 'later',
  1735. isMeta: true,
  1736. })
  1737. void run()
  1738. }, 0)
  1739. }
  1740. : undefined
  1741. // Abort the current operation when a 'now' priority message arrives.
  1742. subscribeToCommandQueue(() => {
  1743. if (abortController && getCommandsByMaxPriority('now').length > 0) {
  1744. abortController.abort('interrupt')
  1745. }
  1746. })
  1747. const run = async () => {
  1748. if (running) {
  1749. return
  1750. }
  1751. running = true
  1752. runPhase = undefined
  1753. notifySessionStateChanged('running')
  1754. idleTimeout.stop()
  1755. headlessProfilerCheckpoint('run_entry')
  1756. // TODO(custom-tool-refactor): Should move to the init message, like browser
  1757. await updateSdkMcp()
  1758. headlessProfilerCheckpoint('after_updateSdkMcp')
  1759. // Resolve deferred plugin installation (CLAUDE_CODE_SYNC_PLUGIN_INSTALL).
  1760. // The promise was started eagerly so installation overlaps with other init.
  1761. // Awaiting here guarantees plugins are available before the first ask().
  1762. // If CLAUDE_CODE_SYNC_PLUGIN_INSTALL_TIMEOUT_MS is set, races against that
  1763. // deadline and proceeds without plugins on timeout (logging an error).
  1764. if (pluginInstallPromise) {
  1765. const timeoutMs = parseInt(
  1766. process.env.CLAUDE_CODE_SYNC_PLUGIN_INSTALL_TIMEOUT_MS || '',
  1767. 10,
  1768. )
  1769. if (timeoutMs > 0) {
  1770. const timeout = sleep(timeoutMs).then(() => 'timeout' as const)
  1771. const result = await Promise.race([pluginInstallPromise, timeout])
  1772. if (result === 'timeout') {
  1773. logError(
  1774. new Error(
  1775. `CLAUDE_CODE_SYNC_PLUGIN_INSTALL: plugin installation timed out after ${timeoutMs}ms`,
  1776. ),
  1777. )
  1778. logEvent('tengu_sync_plugin_install_timeout', {
  1779. timeout_ms: timeoutMs,
  1780. })
  1781. }
  1782. } else {
  1783. await pluginInstallPromise
  1784. }
  1785. pluginInstallPromise = null
  1786. // Refresh commands, agents, and hooks now that plugins are installed
  1787. await refreshPluginState()
  1788. // Set up hot-reload for plugin hooks now that the initial install is done.
  1789. // In sync-install mode, setup.ts skips this to avoid racing with the install.
  1790. const { setupPluginHookHotReload } = await import(
  1791. '../utils/plugins/loadPluginHooks.js'
  1792. )
  1793. setupPluginHookHotReload()
  1794. }
  1795. // Only main-thread commands (agentId===undefined) — subagent
  1796. // notifications are drained by the subagent's mid-turn gate in query.ts.
  1797. // Defined outside the try block so it's accessible in the post-finally
  1798. // queue re-checks at the bottom of run().
  1799. const isMainThread = (cmd: QueuedCommand) => cmd.agentId === undefined
  1800. try {
  1801. let command: QueuedCommand | undefined
  1802. let waitingForAgents = false
  1803. // Extract command processing into a named function for the do-while pattern.
  1804. // Drains the queue, batching consecutive prompt-mode commands into one
  1805. // ask() call so messages that queued up during a long turn coalesce
  1806. // into a single follow-up turn instead of N separate turns.
  1807. const drainCommandQueue = async () => {
  1808. while ((command = dequeue(isMainThread))) {
  1809. if (
  1810. command.mode !== 'prompt' &&
  1811. command.mode !== 'orphaned-permission' &&
  1812. command.mode !== 'task-notification'
  1813. ) {
  1814. throw new Error(
  1815. 'only prompt commands are supported in streaming mode',
  1816. )
  1817. }
  1818. // Non-prompt commands (task-notification, orphaned-permission) carry
  1819. // side effects or orphanedPermission state, so they process singly.
  1820. // Prompt commands greedily collect followers with matching workload.
  1821. const batch: QueuedCommand[] = [command]
  1822. if (command.mode === 'prompt') {
  1823. while (canBatchWith(command, peek(isMainThread))) {
  1824. batch.push(dequeue(isMainThread)!)
  1825. }
  1826. if (batch.length > 1) {
  1827. command = {
  1828. ...command,
  1829. value: joinPromptValues(batch.map(c => c.value)),
  1830. uuid: batch.findLast(c => c.uuid)?.uuid ?? command.uuid,
  1831. }
  1832. }
  1833. }
  1834. const batchUuids = batch.map(c => c.uuid).filter(u => u !== undefined)
  1835. // QueryEngine will emit a replay for command.uuid (the last uuid in
  1836. // the batch) via its messagesToAck path. Emit replays here for the
  1837. // rest so consumers that track per-uuid delivery (clank's
  1838. // asyncMessages footer, CCR) see an ack for every message they sent,
  1839. // not just the one that survived the merge.
  1840. if (options.replayUserMessages && batch.length > 1) {
  1841. for (const c of batch) {
  1842. if (c.uuid && c.uuid !== command.uuid) {
  1843. output.enqueue({
  1844. type: 'user',
  1845. content: c.value,
  1846. message: { role: 'user', content: c.value },
  1847. session_id: getSessionId(),
  1848. parent_tool_use_id: null,
  1849. uuid: c.uuid as string,
  1850. isReplay: true,
  1851. } as SDKUserMessageReplay)
  1852. }
  1853. }
  1854. }
  1855. // Combine all MCP clients. appState.mcp is populated incrementally
  1856. // per-server by main.tsx (mirrors useManageMCPConnections). Reading
  1857. // fresh per-command means late-connecting servers are visible on the
  1858. // next turn. registerElicitationHandlers is idempotent (tracking set).
  1859. const appState = getAppState()
  1860. const allMcpClients = [
  1861. ...appState.mcp.clients,
  1862. ...sdkClients,
  1863. ...dynamicMcpState.clients,
  1864. ]
  1865. registerElicitationHandlers(allMcpClients)
  1866. // Channel handlers for servers allowlisted via --channels at
  1867. // construction time (or enableChannel() mid-session). Runs every
  1868. // turn like registerElicitationHandlers — idempotent per-client
  1869. // (setNotificationHandler replaces, not stacks) and no-ops for
  1870. // non-allowlisted servers (one feature-flag check).
  1871. for (const client of allMcpClients) {
  1872. reregisterChannelHandlerAfterReconnect(client)
  1873. }
  1874. const allTools = buildAllTools(appState)
  1875. for (const uuid of batchUuids) {
  1876. notifyCommandLifecycle(uuid, 'started')
  1877. }
  1878. // Task notifications arrive when background agents complete.
  1879. // Emit an SDK system event for SDK consumers, then fall through
  1880. // to ask() so the model sees the agent result and can act on it.
  1881. // This matches TUI behavior where useQueueProcessor always feeds
  1882. // notifications to the model regardless of coordinator mode.
  1883. if (command.mode === 'task-notification') {
  1884. const notificationText =
  1885. typeof command.value === 'string' ? command.value : ''
  1886. // Parse the XML-formatted notification
  1887. const taskIdMatch = notificationText.match(
  1888. /<task-id>([^<]+)<\/task-id>/,
  1889. )
  1890. const toolUseIdMatch = notificationText.match(
  1891. /<tool-use-id>([^<]+)<\/tool-use-id>/,
  1892. )
  1893. const outputFileMatch = notificationText.match(
  1894. /<output-file>([^<]+)<\/output-file>/,
  1895. )
  1896. const statusMatch = notificationText.match(
  1897. /<status>([^<]+)<\/status>/,
  1898. )
  1899. const summaryMatch = notificationText.match(
  1900. /<summary>([^<]+)<\/summary>/,
  1901. )
  1902. const isValidStatus = (
  1903. s: string | undefined,
  1904. ): s is 'completed' | 'failed' | 'stopped' | 'killed' =>
  1905. s === 'completed' ||
  1906. s === 'failed' ||
  1907. s === 'stopped' ||
  1908. s === 'killed'
  1909. const rawStatus = statusMatch?.[1]
  1910. const status = isValidStatus(rawStatus)
  1911. ? rawStatus === 'killed'
  1912. ? 'stopped'
  1913. : rawStatus
  1914. : 'completed'
  1915. const usageMatch = notificationText.match(
  1916. /<usage>([\s\S]*?)<\/usage>/,
  1917. )
  1918. const usageContent = usageMatch?.[1] ?? ''
  1919. const totalTokensMatch = usageContent.match(
  1920. /<total_tokens>(\d+)<\/total_tokens>/,
  1921. )
  1922. const toolUsesMatch = usageContent.match(
  1923. /<tool_uses>(\d+)<\/tool_uses>/,
  1924. )
  1925. const durationMsMatch = usageContent.match(
  1926. /<duration_ms>(\d+)<\/duration_ms>/,
  1927. )
  1928. // Only emit a task_notification SDK event when a <status> tag is
  1929. // present — that means this is a terminal notification (completed/
  1930. // failed/stopped). Stream events from enqueueStreamEvent carry no
  1931. // <status> (they're progress pings); emitting them here would
  1932. // default to 'completed' and falsely close the task for SDK
  1933. // consumers. Terminal bookends are now emitted directly via
  1934. // emitTaskTerminatedSdk, so skipping statusless events is safe.
  1935. if (statusMatch) {
  1936. output.enqueue({
  1937. type: 'system',
  1938. subtype: 'task_notification',
  1939. task_id: taskIdMatch?.[1] ?? '',
  1940. tool_use_id: toolUseIdMatch?.[1],
  1941. status,
  1942. output_file: outputFileMatch?.[1] ?? '',
  1943. summary: summaryMatch?.[1] ?? '',
  1944. usage:
  1945. totalTokensMatch && toolUsesMatch
  1946. ? {
  1947. total_tokens: parseInt(totalTokensMatch[1]!, 10),
  1948. tool_uses: parseInt(toolUsesMatch[1]!, 10),
  1949. duration_ms: durationMsMatch
  1950. ? parseInt(durationMsMatch[1]!, 10)
  1951. : 0,
  1952. }
  1953. : undefined,
  1954. session_id: getSessionId(),
  1955. uuid: randomUUID(),
  1956. })
  1957. }
  1958. // No continue -- fall through to ask() so the model processes the result
  1959. }
  1960. const input = command.value
  1961. if (structuredIO instanceof RemoteIO && command.mode === 'prompt') {
  1962. logEvent('tengu_bridge_message_received', {
  1963. is_repl: false,
  1964. })
  1965. }
  1966. // Abort any in-flight suggestion generation and track acceptance
  1967. suggestionState.abortController?.abort()
  1968. suggestionState.abortController = null
  1969. suggestionState.pendingSuggestion = null
  1970. suggestionState.pendingLastEmittedEntry = null
  1971. if (suggestionState.lastEmitted) {
  1972. if (command.mode === 'prompt') {
  1973. // SDK user messages enqueue ContentBlockParam[], not a plain string
  1974. const inputText =
  1975. typeof input === 'string'
  1976. ? input
  1977. : (
  1978. input.find(b => b.type === 'text') as
  1979. | { type: 'text'; text: string }
  1980. | undefined
  1981. )?.text
  1982. if (typeof inputText === 'string') {
  1983. logSuggestionOutcome(
  1984. suggestionState.lastEmitted.text,
  1985. inputText,
  1986. suggestionState.lastEmitted.emittedAt,
  1987. suggestionState.lastEmitted.promptId,
  1988. suggestionState.lastEmitted.generationRequestId,
  1989. )
  1990. }
  1991. suggestionState.lastEmitted = null
  1992. }
  1993. }
  1994. abortController = createAbortController()
  1995. const turnStartTime = feature('FILE_PERSISTENCE')
  1996. ? Date.now()
  1997. : undefined
  1998. headlessProfilerCheckpoint('before_ask')
  1999. startQueryProfile()
  2000. // Per-iteration ALS context so bg agents spawned inside ask()
  2001. // inherit workload across their detached awaits. In-process cron
  2002. // stamps cmd.workload; the SDK --workload flag is options.workload.
  2003. // const-capture: TS loses `while ((command = dequeue()))` narrowing
  2004. // inside the closure.
  2005. const cmd = command
  2006. await runWithWorkload(cmd.workload ?? options.workload, async () => {
  2007. for await (const message of ask({
  2008. commands: uniqBy(
  2009. [...currentCommands, ...appState.mcp.commands],
  2010. 'name',
  2011. ),
  2012. prompt: input,
  2013. promptUuid: cmd.uuid,
  2014. isMeta: cmd.isMeta,
  2015. cwd: cwd(),
  2016. tools: allTools,
  2017. verbose: options.verbose,
  2018. mcpClients: allMcpClients,
  2019. thinkingConfig: options.thinkingConfig,
  2020. maxTurns: options.maxTurns,
  2021. maxBudgetUsd: options.maxBudgetUsd,
  2022. taskBudget: options.taskBudget,
  2023. canUseTool,
  2024. userSpecifiedModel: activeUserSpecifiedModel,
  2025. fallbackModel: options.fallbackModel,
  2026. jsonSchema: getInitJsonSchema() ?? options.jsonSchema,
  2027. mutableMessages,
  2028. getReadFileCache: () =>
  2029. pendingSeeds.size === 0
  2030. ? readFileState
  2031. : mergeFileStateCaches(readFileState, pendingSeeds),
  2032. setReadFileCache: cache => {
  2033. readFileState = cache
  2034. for (const [path, seed] of pendingSeeds.entries()) {
  2035. const existing = readFileState.get(path)
  2036. if (!existing || seed.timestamp > existing.timestamp) {
  2037. readFileState.set(path, seed)
  2038. }
  2039. }
  2040. pendingSeeds.clear()
  2041. },
  2042. customSystemPrompt: options.systemPrompt,
  2043. appendSystemPrompt: options.appendSystemPrompt,
  2044. getAppState,
  2045. setAppState,
  2046. abortController,
  2047. replayUserMessages: options.replayUserMessages,
  2048. includePartialMessages: options.includePartialMessages,
  2049. handleElicitation: (serverName, params, elicitSignal) =>
  2050. structuredIO.handleElicitation(
  2051. serverName,
  2052. params.message,
  2053. undefined,
  2054. elicitSignal,
  2055. params.mode,
  2056. params.url,
  2057. 'elicitationId' in params ? params.elicitationId : undefined,
  2058. ),
  2059. agents: currentAgents,
  2060. orphanedPermission: cmd.orphanedPermission,
  2061. setSDKStatus: status => {
  2062. output.enqueue({
  2063. type: 'system',
  2064. subtype: 'status',
  2065. status,
  2066. session_id: getSessionId(),
  2067. uuid: randomUUID(),
  2068. })
  2069. },
  2070. })) {
  2071. // Forward messages to bridge incrementally (mid-turn) so
  2072. // claude.ai sees progress and the connection stays alive
  2073. // while blocked on permission requests.
  2074. forwardMessagesToBridge()
  2075. if (message.type === 'result') {
  2076. // Flush pending SDK events so they appear before result on the stream.
  2077. for (const event of drainSdkEvents()) {
  2078. output.enqueue(event)
  2079. }
  2080. // Hold-back: don't emit result while background agents are running
  2081. const currentState = getAppState()
  2082. if (
  2083. getRunningTasks(currentState).some(
  2084. t =>
  2085. (t.type === 'local_agent' ||
  2086. t.type === 'local_workflow') &&
  2087. isBackgroundTask(t),
  2088. )
  2089. ) {
  2090. heldBackResult = message
  2091. } else {
  2092. heldBackResult = null
  2093. output.enqueue(message)
  2094. }
  2095. } else {
  2096. // Flush SDK events (task_started, task_progress) so background
  2097. // agent progress is streamed in real-time, not batched until result.
  2098. for (const event of drainSdkEvents()) {
  2099. output.enqueue(event)
  2100. }
  2101. output.enqueue(message)
  2102. }
  2103. }
  2104. }) // end runWithWorkload
  2105. for (const uuid of batchUuids) {
  2106. notifyCommandLifecycle(uuid, 'completed')
  2107. }
  2108. // Forward messages to bridge after each turn
  2109. forwardMessagesToBridge()
  2110. bridgeHandle?.sendResult()
  2111. if (feature('FILE_PERSISTENCE') && turnStartTime !== undefined) {
  2112. void executeFilePersistence(
  2113. { turnStartTime } as import('src/utils/filePersistence/types.js').TurnStartTime,
  2114. abortController.signal,
  2115. result => {
  2116. output.enqueue({
  2117. type: 'system' as const,
  2118. subtype: 'files_persisted' as const,
  2119. files: (result as any).persistedFiles,
  2120. failed: (result as any).failedFiles,
  2121. processed_at: new Date().toISOString(),
  2122. uuid: randomUUID(),
  2123. session_id: getSessionId(),
  2124. })
  2125. },
  2126. )
  2127. }
  2128. // Generate and emit prompt suggestion for SDK consumers
  2129. if (
  2130. options.promptSuggestions &&
  2131. !isEnvDefinedFalsy(process.env.CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION)
  2132. ) {
  2133. // TS narrows suggestionState to never in the while loop body;
  2134. // cast via unknown to reset narrowing.
  2135. const state = suggestionState as unknown as typeof suggestionState
  2136. state.abortController?.abort()
  2137. const localAbort = new AbortController()
  2138. suggestionState.abortController = localAbort
  2139. const cacheSafeParams = getLastCacheSafeParams()
  2140. if (!cacheSafeParams) {
  2141. logSuggestionSuppressed(
  2142. 'sdk_no_params',
  2143. undefined,
  2144. undefined,
  2145. 'sdk',
  2146. )
  2147. } else {
  2148. // Use a ref object so the IIFE's finally can compare against its own
  2149. // promise without a self-reference (which upsets TypeScript's flow analysis).
  2150. const ref: { promise: Promise<void> | null } = { promise: null }
  2151. ref.promise = (async () => {
  2152. try {
  2153. const result = await tryGenerateSuggestion(
  2154. localAbort,
  2155. mutableMessages,
  2156. getAppState,
  2157. cacheSafeParams,
  2158. 'sdk',
  2159. )
  2160. if (!result || localAbort.signal.aborted) return
  2161. const suggestionMsg = {
  2162. type: 'prompt_suggestion' as const,
  2163. suggestion: result.suggestion,
  2164. uuid: randomUUID(),
  2165. session_id: getSessionId(),
  2166. }
  2167. const lastEmittedEntry = {
  2168. text: result.suggestion,
  2169. emittedAt: Date.now(),
  2170. promptId: result.promptId,
  2171. generationRequestId: result.generationRequestId,
  2172. }
  2173. // Defer emission if the result is being held for background agents,
  2174. // so that prompt_suggestion always arrives after result.
  2175. // Only set lastEmitted when the suggestion is actually delivered
  2176. // to the consumer; deferred suggestions may be discarded before
  2177. // delivery if a new command arrives first.
  2178. if (heldBackResult) {
  2179. suggestionState.pendingSuggestion = suggestionMsg
  2180. suggestionState.pendingLastEmittedEntry = {
  2181. text: lastEmittedEntry.text,
  2182. promptId: lastEmittedEntry.promptId,
  2183. generationRequestId: lastEmittedEntry.generationRequestId,
  2184. }
  2185. } else {
  2186. suggestionState.lastEmitted = lastEmittedEntry
  2187. output.enqueue(suggestionMsg)
  2188. }
  2189. } catch (error) {
  2190. if (
  2191. error instanceof Error &&
  2192. (error.name === 'AbortError' ||
  2193. error.name === 'APIUserAbortError')
  2194. ) {
  2195. logSuggestionSuppressed(
  2196. 'aborted',
  2197. undefined,
  2198. undefined,
  2199. 'sdk',
  2200. )
  2201. return
  2202. }
  2203. logError(toError(error))
  2204. } finally {
  2205. if (suggestionState.inflightPromise === ref.promise) {
  2206. suggestionState.inflightPromise = null
  2207. }
  2208. }
  2209. })()
  2210. suggestionState.inflightPromise = ref.promise
  2211. }
  2212. }
  2213. // Log headless profiler metrics for this turn and start next turn
  2214. logHeadlessProfilerTurn()
  2215. logQueryProfileReport()
  2216. headlessProfilerStartTurn()
  2217. }
  2218. }
  2219. // Use a do-while loop to drain commands and then wait for any
  2220. // background agents that are still running. When agents complete,
  2221. // their notifications are enqueued and the loop re-drains.
  2222. do {
  2223. // Drain SDK events (task_started, task_progress) before command queue
  2224. // so progress events precede task_notification on the stream.
  2225. for (const event of drainSdkEvents()) {
  2226. output.enqueue(event)
  2227. }
  2228. runPhase = 'draining_commands'
  2229. await drainCommandQueue()
  2230. // Check for running background tasks before exiting.
  2231. // Exclude in_process_teammate — teammates are long-lived by design
  2232. // (status: 'running' for their whole lifetime, cleaned up by the
  2233. // shutdown protocol, not by transitioning to 'completed'). Waiting
  2234. // on them here loops forever (gh-30008). Same exclusion already
  2235. // exists at useBackgroundTaskNavigation.ts:55 for the same reason;
  2236. // L1839 above is already narrower (type === 'local_agent') so it
  2237. // doesn't hit this.
  2238. waitingForAgents = false
  2239. {
  2240. const state = getAppState()
  2241. const hasRunningBg = getRunningTasks(state).some(
  2242. t => isBackgroundTask(t) && t.type !== 'in_process_teammate',
  2243. )
  2244. const hasMainThreadQueued = peek(isMainThread) !== undefined
  2245. if (hasRunningBg || hasMainThreadQueued) {
  2246. waitingForAgents = true
  2247. if (!hasMainThreadQueued) {
  2248. runPhase = 'waiting_for_agents'
  2249. // No commands ready yet, wait for tasks to complete
  2250. await sleep(100)
  2251. }
  2252. // Loop back to drain any newly queued commands
  2253. }
  2254. }
  2255. } while (waitingForAgents)
  2256. if (heldBackResult) {
  2257. output.enqueue(heldBackResult)
  2258. heldBackResult = null
  2259. if (suggestionState.pendingSuggestion) {
  2260. output.enqueue(suggestionState.pendingSuggestion)
  2261. // Now that the suggestion is actually delivered, record it for acceptance tracking
  2262. if (suggestionState.pendingLastEmittedEntry) {
  2263. suggestionState.lastEmitted = {
  2264. ...suggestionState.pendingLastEmittedEntry,
  2265. emittedAt: Date.now(),
  2266. }
  2267. suggestionState.pendingLastEmittedEntry = null
  2268. }
  2269. suggestionState.pendingSuggestion = null
  2270. }
  2271. }
  2272. } catch (error) {
  2273. // Emit error result message before shutting down
  2274. // Write directly to structuredIO to ensure immediate delivery
  2275. try {
  2276. await structuredIO.write({
  2277. type: 'result',
  2278. subtype: 'error_during_execution',
  2279. duration_ms: 0,
  2280. duration_api_ms: 0,
  2281. is_error: true,
  2282. num_turns: 0,
  2283. stop_reason: null,
  2284. session_id: getSessionId(),
  2285. total_cost_usd: 0,
  2286. usage: EMPTY_USAGE,
  2287. modelUsage: {},
  2288. permission_denials: [],
  2289. uuid: randomUUID(),
  2290. errors: [
  2291. errorMessage(error),
  2292. ...getInMemoryErrors().map(_ => _.error),
  2293. ],
  2294. })
  2295. } catch {
  2296. // If we can't emit the error result, continue with shutdown anyway
  2297. }
  2298. suggestionState.abortController?.abort()
  2299. gracefulShutdownSync(1)
  2300. return
  2301. } finally {
  2302. runPhase = 'finally_flush'
  2303. // Flush pending internal events before going idle
  2304. await structuredIO.flushInternalEvents()
  2305. runPhase = 'finally_post_flush'
  2306. if (!isShuttingDown()) {
  2307. notifySessionStateChanged('idle')
  2308. // Drain so the idle session_state_changed SDK event (plus any
  2309. // terminal task_notification bookends emitted during bg-agent
  2310. // teardown) reach the output stream before we block on the next
  2311. // command. The do-while drain above only runs while
  2312. // waitingForAgents; once we're here the next drain would be the
  2313. // top of the next run(), which won't come if input is idle.
  2314. for (const event of drainSdkEvents()) {
  2315. output.enqueue(event)
  2316. }
  2317. }
  2318. running = false
  2319. // Start idle timer when we finish processing and are waiting for input
  2320. idleTimeout.start()
  2321. }
  2322. // Proactive tick: if proactive is active and queue is empty, inject a tick
  2323. if (
  2324. (feature('PROACTIVE') || feature('KAIROS')) &&
  2325. proactiveModule?.isProactiveActive() &&
  2326. !proactiveModule.isProactivePaused()
  2327. ) {
  2328. if (peek(isMainThread) === undefined && !inputClosed) {
  2329. scheduleProactiveTick!()
  2330. return
  2331. }
  2332. }
  2333. // Re-check the queue after releasing the mutex. A message may have
  2334. // arrived (and called run()) between the last dequeue() returning
  2335. // undefined and `running = false` above. In that case the caller
  2336. // saw `running === true` and returned immediately, leaving the
  2337. // message stranded in the queue with no one to process it.
  2338. if (peek(isMainThread) !== undefined) {
  2339. void run()
  2340. return
  2341. }
  2342. // Check for unread teammate messages and process them
  2343. // This mirrors what useInboxPoller does in interactive REPL mode
  2344. // Poll until no more messages (teammates may still be working)
  2345. {
  2346. const currentAppState = getAppState()
  2347. const teamContext = currentAppState.teamContext
  2348. if (teamContext && isTeamLead(teamContext)) {
  2349. const agentName = 'team-lead'
  2350. // Poll for messages while teammates are active
  2351. // This is needed because teammates may send messages while we're waiting
  2352. // Keep polling until the team is shut down
  2353. const POLL_INTERVAL_MS = 500
  2354. while (true) {
  2355. // Check if teammates are still active
  2356. const refreshedState = getAppState()
  2357. const hasActiveTeammates =
  2358. hasActiveInProcessTeammates(refreshedState) ||
  2359. (refreshedState.teamContext &&
  2360. Object.keys(refreshedState.teamContext.teammates).length > 0)
  2361. if (!hasActiveTeammates) {
  2362. logForDebugging(
  2363. '[print.ts] No more active teammates, stopping poll',
  2364. )
  2365. break
  2366. }
  2367. const unread = await readUnreadMessages(
  2368. agentName,
  2369. refreshedState.teamContext?.teamName,
  2370. )
  2371. if (unread.length > 0) {
  2372. logForDebugging(
  2373. `[print.ts] Team-lead found ${unread.length} unread messages`,
  2374. )
  2375. // Mark as read immediately to avoid duplicate processing
  2376. await markMessagesAsRead(
  2377. agentName,
  2378. refreshedState.teamContext?.teamName,
  2379. )
  2380. // Process shutdown_approved messages - remove teammates from team file
  2381. // This mirrors what useInboxPoller does in interactive mode (lines 546-606)
  2382. const teamName = refreshedState.teamContext?.teamName
  2383. for (const m of unread) {
  2384. const shutdownApproval = isShutdownApproved(m.text)
  2385. if (shutdownApproval && teamName) {
  2386. const teammateToRemove = shutdownApproval.from
  2387. logForDebugging(
  2388. `[print.ts] Processing shutdown_approved from ${teammateToRemove}`,
  2389. )
  2390. // Find the teammate ID by name
  2391. const teammateId = refreshedState.teamContext?.teammates
  2392. ? Object.entries(refreshedState.teamContext.teammates).find(
  2393. ([, t]) => t.name === teammateToRemove,
  2394. )?.[0]
  2395. : undefined
  2396. if (teammateId) {
  2397. // Remove from team file
  2398. removeTeammateFromTeamFile(teamName, {
  2399. agentId: teammateId,
  2400. name: teammateToRemove,
  2401. })
  2402. logForDebugging(
  2403. `[print.ts] Removed ${teammateToRemove} from team file`,
  2404. )
  2405. // Unassign tasks owned by this teammate
  2406. await unassignTeammateTasks(
  2407. teamName,
  2408. teammateId,
  2409. teammateToRemove,
  2410. 'shutdown',
  2411. )
  2412. // Remove from teamContext in AppState
  2413. setAppState(prev => {
  2414. if (!prev.teamContext?.teammates) return prev
  2415. if (!(teammateId in prev.teamContext.teammates)) return prev
  2416. const { [teammateId]: _, ...remainingTeammates } =
  2417. prev.teamContext.teammates
  2418. return {
  2419. ...prev,
  2420. teamContext: {
  2421. ...prev.teamContext,
  2422. teammates: remainingTeammates,
  2423. },
  2424. }
  2425. })
  2426. }
  2427. }
  2428. }
  2429. // Format messages same as useInboxPoller
  2430. const formatted = unread
  2431. .map(
  2432. (m: { from: string; text: string; color?: string }) =>
  2433. `<${TEAMMATE_MESSAGE_TAG} teammate_id="${m.from}"${m.color ? ` color="${m.color}"` : ''}>\n${m.text}\n</${TEAMMATE_MESSAGE_TAG}>`,
  2434. )
  2435. .join('\n\n')
  2436. // Enqueue and process
  2437. enqueue({
  2438. mode: 'prompt',
  2439. value: formatted,
  2440. uuid: randomUUID(),
  2441. })
  2442. void run()
  2443. return // run() will come back here after processing
  2444. }
  2445. // No messages - check if we need to prompt for shutdown
  2446. // If input is closed and teammates are active, inject shutdown prompt once
  2447. if (inputClosed && !shutdownPromptInjected) {
  2448. shutdownPromptInjected = true
  2449. logForDebugging(
  2450. '[print.ts] Input closed with active teammates, injecting shutdown prompt',
  2451. )
  2452. enqueue({
  2453. mode: 'prompt',
  2454. value: SHUTDOWN_TEAM_PROMPT,
  2455. uuid: randomUUID(),
  2456. })
  2457. void run()
  2458. return // run() will come back here after processing
  2459. }
  2460. // Wait and check again
  2461. await sleep(POLL_INTERVAL_MS)
  2462. }
  2463. }
  2464. }
  2465. if (inputClosed) {
  2466. // Check for active swarm that needs shutdown
  2467. const hasActiveSwarm = await (async () => {
  2468. // Wait for any working in-process team members to finish
  2469. const currentAppState = getAppState()
  2470. if (hasWorkingInProcessTeammates(currentAppState)) {
  2471. await waitForTeammatesToBecomeIdle(setAppState, currentAppState)
  2472. }
  2473. // Re-fetch state after potential wait
  2474. const refreshedAppState = getAppState()
  2475. const refreshedTeamContext = refreshedAppState.teamContext
  2476. const hasTeamMembersNotCleanedUp =
  2477. refreshedTeamContext &&
  2478. Object.keys(refreshedTeamContext.teammates).length > 0
  2479. return (
  2480. hasTeamMembersNotCleanedUp ||
  2481. hasActiveInProcessTeammates(refreshedAppState)
  2482. )
  2483. })()
  2484. if (hasActiveSwarm) {
  2485. // Team members are idle or pane-based - inject prompt to shut down team
  2486. enqueue({
  2487. mode: 'prompt',
  2488. value: SHUTDOWN_TEAM_PROMPT,
  2489. uuid: randomUUID(),
  2490. })
  2491. void run()
  2492. } else {
  2493. // Wait for any in-flight push suggestion before closing the output stream.
  2494. if (suggestionState.inflightPromise) {
  2495. await Promise.race([suggestionState.inflightPromise, sleep(5000)])
  2496. }
  2497. suggestionState.abortController?.abort()
  2498. suggestionState.abortController = null
  2499. await finalizePendingAsyncHooks()
  2500. unsubscribeSkillChanges()
  2501. unsubscribeAuthStatus?.()
  2502. statusListeners.delete(rateLimitListener)
  2503. output.done()
  2504. }
  2505. }
  2506. }
  2507. // Set up UDS inbox callback so the query loop is kicked off
  2508. // when a message arrives via the UDS socket in headless mode.
  2509. if (feature('UDS_INBOX')) {
  2510. /* eslint-disable @typescript-eslint/no-require-imports */
  2511. const { setOnEnqueue } = require('../utils/udsMessaging.js')
  2512. /* eslint-enable @typescript-eslint/no-require-imports */
  2513. setOnEnqueue(() => {
  2514. if (!inputClosed) {
  2515. void run()
  2516. }
  2517. })
  2518. }
  2519. // Cron scheduler: runs scheduled_tasks.json tasks in SDK/-p mode.
  2520. // Mirrors REPL's useScheduledTasks hook. Fired prompts enqueue + kick
  2521. // off run() directly — unlike REPL, there's no queue subscriber here
  2522. // that drains on enqueue while idle. The run() mutex makes this safe
  2523. // during an active turn: the call no-ops and the post-run recheck at
  2524. // the end of run() picks up the queued command.
  2525. let cronScheduler: import('../utils/cronScheduler.js').CronScheduler | null =
  2526. null
  2527. if (
  2528. cronGate.isKairosCronEnabled()
  2529. ) {
  2530. cronScheduler = cronSchedulerModule.createCronScheduler({
  2531. onFire: prompt => {
  2532. if (inputClosed) return
  2533. enqueue({
  2534. mode: 'prompt',
  2535. value: prompt,
  2536. uuid: randomUUID(),
  2537. priority: 'later',
  2538. // System-generated — matches useScheduledTasks.ts REPL equivalent.
  2539. // Without this, messages.ts metaProp eval is {} → prompt leaks
  2540. // into visible transcript when cron fires mid-turn in -p mode.
  2541. isMeta: true,
  2542. // Threaded to cc_workload= in the billing-header attribution block
  2543. // so the API can serve cron requests at lower QoS. drainCommandQueue
  2544. // reads this per-iteration and hoists it into bootstrap state for
  2545. // the ask() call.
  2546. workload: WORKLOAD_CRON,
  2547. })
  2548. void run()
  2549. },
  2550. isLoading: () => running || inputClosed,
  2551. getJitterConfig: cronJitterConfigModule?.getCronJitterConfig,
  2552. isKilled: () => !cronGate?.isKairosCronEnabled(),
  2553. })
  2554. cronScheduler.start()
  2555. }
  2556. const sendControlResponseSuccess = function (
  2557. message: SDKControlRequest,
  2558. response?: Record<string, unknown>,
  2559. ) {
  2560. output.enqueue({
  2561. type: 'control_response',
  2562. response: {
  2563. subtype: 'success',
  2564. request_id: message.request_id,
  2565. response: response,
  2566. },
  2567. })
  2568. }
  2569. const sendControlResponseError = function (
  2570. message: SDKControlRequest,
  2571. errorMessage: string,
  2572. ) {
  2573. output.enqueue({
  2574. type: 'control_response',
  2575. response: {
  2576. subtype: 'error',
  2577. request_id: message.request_id,
  2578. error: errorMessage,
  2579. },
  2580. })
  2581. }
  2582. // Handle unexpected permission responses by looking up the unresolved tool
  2583. // call in the transcript and executing it
  2584. const handledOrphanedToolUseIds = new Set<string>()
  2585. structuredIO.setUnexpectedResponseCallback(async message => {
  2586. await handleOrphanedPermissionResponse({
  2587. message,
  2588. setAppState,
  2589. handledToolUseIds: handledOrphanedToolUseIds,
  2590. onEnqueued: () => {
  2591. // The first message of a session might be the orphaned permission
  2592. // check rather than a user prompt, so kick off the loop.
  2593. void run()
  2594. },
  2595. })
  2596. })
  2597. // Track active OAuth flows per server so we can abort a previous flow
  2598. // when a new mcp_authenticate request arrives for the same server.
  2599. const activeOAuthFlows = new Map<string, AbortController>()
  2600. // Track manual callback URL submit functions for active OAuth flows.
  2601. // Used when localhost is not reachable (e.g., browser-based IDEs).
  2602. const oauthCallbackSubmitters = new Map<
  2603. string,
  2604. (callbackUrl: string) => void
  2605. >()
  2606. // Track servers where the manual callback was actually invoked (so the
  2607. // automatic reconnect path knows to skip — the extension will reconnect).
  2608. const oauthManualCallbackUsed = new Set<string>()
  2609. // Track OAuth auth-only promises so mcp_oauth_callback_url can await
  2610. // token exchange completion. Reconnect is handled separately by the
  2611. // extension via handleAuthDone → mcp_reconnect.
  2612. const oauthAuthPromises = new Map<string, Promise<void>>()
  2613. // In-flight Anthropic OAuth flow (claude_authenticate). Single-slot: a
  2614. // second authenticate request cleans up the first. The service holds the
  2615. // PKCE verifier + localhost listener; the promise settles after
  2616. // installOAuthTokens — after it resolves, the in-process memoized token
  2617. // cache is already cleared and the next API call picks up the new creds.
  2618. let claudeOAuth: {
  2619. service: OAuthService
  2620. flow: Promise<void>
  2621. } | null = null
  2622. // This is essentially spawning a parallel async task- we have two
  2623. // running in parallel- one reading from stdin and adding to the
  2624. // queue to be processed and another reading from the queue,
  2625. // processing and returning the result of the generation.
  2626. // The process is complete when the input stream completes and
  2627. // the last generation of the queue has complete.
  2628. void (async () => {
  2629. let initialized = false
  2630. logForDiagnosticsNoPII('info', 'cli_message_loop_started')
  2631. for await (const message of structuredIO.structuredInput) {
  2632. // Non-user events are handled inline (no queue). started→completed in
  2633. // the same tick carries no information, so only fire completed.
  2634. // control_response is reported by StructuredIO.processLine (which also
  2635. // sees orphans that never yield here).
  2636. const eventId = 'uuid' in message ? message.uuid : undefined
  2637. if (
  2638. eventId &&
  2639. message.type !== 'user' &&
  2640. message.type !== 'control_response'
  2641. ) {
  2642. notifyCommandLifecycle(eventId, 'completed')
  2643. }
  2644. if (message.type === 'control_request') {
  2645. if (message.request.subtype === 'interrupt') {
  2646. // Track escapes for attribution (ant-only feature)
  2647. if (feature('COMMIT_ATTRIBUTION')) {
  2648. setAppState(prev => ({
  2649. ...prev,
  2650. attribution: {
  2651. ...prev.attribution,
  2652. escapeCount: prev.attribution.escapeCount + 1,
  2653. },
  2654. }))
  2655. }
  2656. if (abortController) {
  2657. abortController.abort()
  2658. }
  2659. suggestionState.abortController?.abort()
  2660. suggestionState.abortController = null
  2661. suggestionState.lastEmitted = null
  2662. suggestionState.pendingSuggestion = null
  2663. sendControlResponseSuccess(message)
  2664. } else if (message.request.subtype === 'end_session') {
  2665. logForDebugging(
  2666. `[print.ts] end_session received, reason=${message.request.reason ?? 'unspecified'}`,
  2667. )
  2668. if (abortController) {
  2669. abortController.abort()
  2670. }
  2671. suggestionState.abortController?.abort()
  2672. suggestionState.abortController = null
  2673. suggestionState.lastEmitted = null
  2674. suggestionState.pendingSuggestion = null
  2675. sendControlResponseSuccess(message)
  2676. break // exits for-await → falls through to inputClosed=true drain below
  2677. } else if (message.request.subtype === 'initialize') {
  2678. // SDK MCP server names from the initialize message
  2679. // Populated by both browser and ProcessTransport sessions
  2680. if (
  2681. message.request.sdkMcpServers &&
  2682. message.request.sdkMcpServers.length > 0
  2683. ) {
  2684. for (const serverName of message.request.sdkMcpServers) {
  2685. // Create placeholder config for SDK MCP servers
  2686. // The actual server connection is managed by the SDK Query class
  2687. sdkMcpConfigs[serverName] = {
  2688. type: 'sdk',
  2689. name: serverName,
  2690. }
  2691. }
  2692. }
  2693. await handleInitializeRequest(
  2694. message.request,
  2695. message.request_id,
  2696. initialized,
  2697. output,
  2698. commands,
  2699. modelInfos,
  2700. structuredIO,
  2701. !!options.enableAuthStatus,
  2702. options,
  2703. agents,
  2704. getAppState,
  2705. )
  2706. // Enable prompt suggestions in AppState when SDK consumer opts in.
  2707. // shouldEnablePromptSuggestion() returns false for non-interactive
  2708. // sessions, but the SDK consumer explicitly requested suggestions.
  2709. if (message.request.promptSuggestions) {
  2710. setAppState(prev => {
  2711. if (prev.promptSuggestionEnabled) return prev
  2712. return { ...prev, promptSuggestionEnabled: true }
  2713. })
  2714. }
  2715. if (
  2716. message.request.agentProgressSummaries &&
  2717. getFeatureValue_CACHED_MAY_BE_STALE('tengu_slate_prism', true)
  2718. ) {
  2719. setSdkAgentProgressSummariesEnabled(true)
  2720. }
  2721. initialized = true
  2722. // If the auto-resume logic pre-enqueued a command, drain it now
  2723. // that initialize has set up systemPrompt, agents, hooks, etc.
  2724. if (hasCommandsInQueue()) {
  2725. void run()
  2726. }
  2727. } else if (message.request.subtype === 'set_permission_mode') {
  2728. const m = message.request // for typescript (TODO: use readonly types to avoid this)
  2729. setAppState(prev => ({
  2730. ...prev,
  2731. toolPermissionContext: handleSetPermissionMode(
  2732. m,
  2733. message.request_id,
  2734. prev.toolPermissionContext,
  2735. output,
  2736. ),
  2737. isUltraplanMode: m.ultraplan ?? prev.isUltraplanMode,
  2738. }))
  2739. // handleSetPermissionMode sends the control_response; the
  2740. // notifySessionMetadataChanged that used to follow here is
  2741. // now fired by onChangeAppState (with externalized mode name).
  2742. } else if (message.request.subtype === 'set_model') {
  2743. const requestedModel = message.request.model ?? 'default'
  2744. const model =
  2745. requestedModel === 'default'
  2746. ? getDefaultMainLoopModel()
  2747. : requestedModel
  2748. activeUserSpecifiedModel = model
  2749. setMainLoopModelOverride(model)
  2750. notifySessionMetadataChanged({ model })
  2751. injectModelSwitchBreadcrumbs(requestedModel, model)
  2752. sendControlResponseSuccess(message)
  2753. } else if (message.request.subtype === 'set_max_thinking_tokens') {
  2754. if (message.request.max_thinking_tokens === null) {
  2755. options.thinkingConfig = undefined
  2756. } else if (message.request.max_thinking_tokens === 0) {
  2757. options.thinkingConfig = { type: 'disabled' }
  2758. } else {
  2759. options.thinkingConfig = {
  2760. type: 'enabled',
  2761. budgetTokens: message.request.max_thinking_tokens,
  2762. }
  2763. }
  2764. sendControlResponseSuccess(message)
  2765. } else if (message.request.subtype === 'mcp_status') {
  2766. sendControlResponseSuccess(message, {
  2767. mcpServers: buildMcpServerStatuses(),
  2768. })
  2769. } else if (message.request.subtype === 'get_context_usage') {
  2770. try {
  2771. const appState = getAppState()
  2772. const data = await collectContextData({
  2773. messages: mutableMessages,
  2774. getAppState,
  2775. options: {
  2776. mainLoopModel: getMainLoopModel(),
  2777. tools: buildAllTools(appState),
  2778. agentDefinitions: appState.agentDefinitions,
  2779. customSystemPrompt: options.systemPrompt,
  2780. appendSystemPrompt: options.appendSystemPrompt,
  2781. },
  2782. })
  2783. sendControlResponseSuccess(message, { ...data })
  2784. } catch (error) {
  2785. sendControlResponseError(message, errorMessage(error))
  2786. }
  2787. } else if (message.request.subtype === 'mcp_message') {
  2788. // Handle MCP notifications from SDK servers
  2789. const mcpRequest = message.request
  2790. const sdkClient = sdkClients.find(
  2791. client => client.name === mcpRequest.server_name,
  2792. )
  2793. // Check client exists - dynamically added SDK servers may have
  2794. // placeholder clients with null client until updateSdkMcp() runs
  2795. if (
  2796. sdkClient &&
  2797. sdkClient.type === 'connected' &&
  2798. sdkClient.client?.transport?.onmessage
  2799. ) {
  2800. sdkClient.client.transport.onmessage(mcpRequest.message)
  2801. }
  2802. sendControlResponseSuccess(message)
  2803. } else if (message.request.subtype === 'rewind_files') {
  2804. const appState = getAppState()
  2805. const result = await handleRewindFiles(
  2806. message.request.user_message_id as UUID,
  2807. appState,
  2808. setAppState,
  2809. message.request.dry_run ?? false,
  2810. )
  2811. if (result.canRewind || message.request.dry_run) {
  2812. sendControlResponseSuccess(message, result)
  2813. } else {
  2814. sendControlResponseError(
  2815. message,
  2816. (result.error as string) ?? 'Unexpected error',
  2817. )
  2818. }
  2819. } else if (message.request.subtype === 'cancel_async_message') {
  2820. const targetUuid = message.request.message_uuid
  2821. const removed = dequeueAllMatching(cmd => cmd.uuid === targetUuid)
  2822. sendControlResponseSuccess(message, {
  2823. cancelled: removed.length > 0,
  2824. })
  2825. } else if (message.request.subtype === 'seed_read_state') {
  2826. // Client observed a Read that was later removed from context (e.g.
  2827. // by snip), so transcript-based seeding missed it. Queued into
  2828. // pendingSeeds; applied at the next clone-replace boundary.
  2829. try {
  2830. // expandPath: all other readFileState writers normalize (~, relative,
  2831. // session cwd vs process cwd). FileEditTool looks up by expandPath'd
  2832. // key — a verbatim client path would miss.
  2833. const normalizedPath = expandPath(message.request.path)
  2834. // Check disk mtime before reading content. If the file changed
  2835. // since the client's observation, readFile would return C_current
  2836. // but we'd store it with the client's M_observed — getChangedFiles
  2837. // then sees disk > cache.timestamp, re-reads, diffs C_current vs
  2838. // C_current = empty, emits no attachment, and the model is never
  2839. // told about the C_observed → C_current change. Skipping the seed
  2840. // makes Edit fail "file not read yet" → forces a fresh Read.
  2841. // Math.floor matches FileReadTool and getFileModificationTime.
  2842. const diskMtime = Math.floor((await stat(normalizedPath)).mtimeMs)
  2843. if (diskMtime <= message.request.mtime) {
  2844. const raw = await readFile(normalizedPath, 'utf-8')
  2845. // Strip BOM + normalize CRLF→LF to match readFileInRange and
  2846. // readFileSyncWithMetadata. FileEditTool's content-compare
  2847. // fallback (for Windows mtime bumps without content change)
  2848. // compares against LF-normalized disk reads.
  2849. const content = (
  2850. raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw
  2851. ).replaceAll('\r\n', '\n')
  2852. pendingSeeds.set(normalizedPath, {
  2853. content,
  2854. timestamp: diskMtime,
  2855. offset: undefined,
  2856. limit: undefined,
  2857. })
  2858. }
  2859. } catch {
  2860. // ENOENT etc — skip seeding but still succeed
  2861. }
  2862. sendControlResponseSuccess(message)
  2863. } else if (message.request.subtype === 'mcp_set_servers') {
  2864. const { response, sdkServersChanged } = await applyMcpServerChanges(
  2865. message.request.servers,
  2866. )
  2867. sendControlResponseSuccess(message, response)
  2868. // Connect SDK servers AFTER response to avoid deadlock
  2869. if (sdkServersChanged) {
  2870. void updateSdkMcp()
  2871. }
  2872. } else if (message.request.subtype === 'reload_plugins') {
  2873. try {
  2874. if (
  2875. feature('DOWNLOAD_USER_SETTINGS') &&
  2876. (isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) || getIsRemoteMode())
  2877. ) {
  2878. // Re-pull user settings so enabledPlugins pushed from the
  2879. // user's local CLI take effect before the cache sweep.
  2880. const applied = await redownloadUserSettings()
  2881. if (applied) {
  2882. settingsChangeDetector.notifyChange('userSettings')
  2883. }
  2884. }
  2885. const r = await refreshActivePlugins(setAppState)
  2886. const sdkAgents = currentAgents.filter(
  2887. a => a.source === 'flagSettings',
  2888. )
  2889. currentAgents = [...r.agentDefinitions.allAgents, ...sdkAgents]
  2890. // Reload succeeded — gather response data best-effort so a
  2891. // read failure doesn't mask the successful state change.
  2892. // allSettled so one failure doesn't discard the others.
  2893. let plugins: SDKControlReloadPluginsResponse['plugins'] = []
  2894. const [cmdsR, mcpR, pluginsR] = await Promise.allSettled([
  2895. getCommands(cwd()),
  2896. applyPluginMcpDiff(),
  2897. loadAllPluginsCacheOnly(),
  2898. ])
  2899. if (cmdsR.status === 'fulfilled') {
  2900. currentCommands = cmdsR.value
  2901. } else {
  2902. logError(cmdsR.reason)
  2903. }
  2904. if (mcpR.status === 'rejected') {
  2905. logError(mcpR.reason)
  2906. }
  2907. if (pluginsR.status === 'fulfilled') {
  2908. plugins = pluginsR.value.enabled.map(p => ({
  2909. name: p.name,
  2910. path: p.path,
  2911. source: p.source,
  2912. }))
  2913. } else {
  2914. logError(pluginsR.reason)
  2915. }
  2916. sendControlResponseSuccess(message, {
  2917. commands: currentCommands
  2918. .filter(cmd => cmd.userInvocable !== false)
  2919. .map(cmd => ({
  2920. name: getCommandName(cmd),
  2921. description: formatDescriptionWithSource(cmd),
  2922. argumentHint: cmd.argumentHint || '',
  2923. })),
  2924. agents: currentAgents.map(a => ({
  2925. name: a.agentType,
  2926. description: a.whenToUse,
  2927. model: a.model === 'inherit' ? undefined : a.model,
  2928. })),
  2929. plugins,
  2930. mcpServers: buildMcpServerStatuses(),
  2931. error_count: r.error_count,
  2932. } satisfies SDKControlReloadPluginsResponse)
  2933. } catch (error) {
  2934. sendControlResponseError(message, errorMessage(error))
  2935. }
  2936. } else if (message.request.subtype === 'mcp_reconnect') {
  2937. const currentAppState = getAppState()
  2938. const { serverName } = message.request
  2939. elicitationRegistered.delete(serverName)
  2940. // Config-existence gate must cover the SAME sources as the
  2941. // operations below. SDK-injected servers (query({mcpServers:{...}}))
  2942. // and dynamically-added servers were missing here, so
  2943. // toggleMcpServer/reconnect returned "Server not found" even though
  2944. // the disconnect/reconnect would have worked (gh-31339 / CC-314).
  2945. const config =
  2946. getMcpConfigByName(serverName) ??
  2947. mcpClients.find(c => c.name === serverName)?.config ??
  2948. sdkClients.find(c => c.name === serverName)?.config ??
  2949. dynamicMcpState.clients.find(c => c.name === serverName)?.config ??
  2950. currentAppState.mcp.clients.find(c => c.name === serverName)
  2951. ?.config ??
  2952. null
  2953. if (!config) {
  2954. sendControlResponseError(message, `Server not found: ${serverName}`)
  2955. } else {
  2956. const result = await reconnectMcpServerImpl(serverName, config)
  2957. // Update appState.mcp with the new client, tools, commands, and resources
  2958. const prefix = getMcpPrefix(serverName)
  2959. setAppState(prev => ({
  2960. ...prev,
  2961. mcp: {
  2962. ...prev.mcp,
  2963. clients: prev.mcp.clients.map(c =>
  2964. c.name === serverName ? result.client : c,
  2965. ),
  2966. tools: [
  2967. ...reject(prev.mcp.tools, t => t.name?.startsWith(prefix)),
  2968. ...result.tools,
  2969. ],
  2970. commands: [
  2971. ...reject(prev.mcp.commands, c =>
  2972. commandBelongsToServer(c, serverName),
  2973. ),
  2974. ...result.commands,
  2975. ],
  2976. resources:
  2977. result.resources && result.resources.length > 0
  2978. ? { ...prev.mcp.resources, [serverName]: result.resources }
  2979. : omit(prev.mcp.resources, serverName),
  2980. },
  2981. }))
  2982. // Also update dynamicMcpState so run() picks up the new tools
  2983. // on the next turn (run() reads dynamicMcpState, not appState)
  2984. dynamicMcpState = {
  2985. ...dynamicMcpState,
  2986. clients: [
  2987. ...dynamicMcpState.clients.filter(c => c.name !== serverName),
  2988. result.client,
  2989. ],
  2990. tools: [
  2991. ...dynamicMcpState.tools.filter(
  2992. t => !t.name?.startsWith(prefix),
  2993. ),
  2994. ...result.tools,
  2995. ],
  2996. }
  2997. if (result.client.type === 'connected') {
  2998. registerElicitationHandlers([result.client])
  2999. reregisterChannelHandlerAfterReconnect(result.client)
  3000. sendControlResponseSuccess(message)
  3001. } else {
  3002. const errorMessage =
  3003. result.client.type === 'failed'
  3004. ? (result.client.error ?? 'Connection failed')
  3005. : `Server status: ${result.client.type}`
  3006. sendControlResponseError(message, errorMessage)
  3007. }
  3008. }
  3009. } else if (message.request.subtype === 'mcp_toggle') {
  3010. const currentAppState = getAppState()
  3011. const { serverName, enabled } = message.request
  3012. elicitationRegistered.delete(serverName)
  3013. // Gate must match the client-lookup spread below (which
  3014. // includes sdkClients and dynamicMcpState.clients). Same fix as
  3015. // mcp_reconnect above (gh-31339 / CC-314).
  3016. const config =
  3017. getMcpConfigByName(serverName) ??
  3018. mcpClients.find(c => c.name === serverName)?.config ??
  3019. sdkClients.find(c => c.name === serverName)?.config ??
  3020. dynamicMcpState.clients.find(c => c.name === serverName)?.config ??
  3021. currentAppState.mcp.clients.find(c => c.name === serverName)
  3022. ?.config ??
  3023. null
  3024. if (!config) {
  3025. sendControlResponseError(message, `Server not found: ${serverName}`)
  3026. } else if (!enabled) {
  3027. // Disabling: persist + disconnect (matches TUI toggleMcpServer behavior)
  3028. setMcpServerEnabled(serverName, false)
  3029. const client = [
  3030. ...mcpClients,
  3031. ...sdkClients,
  3032. ...dynamicMcpState.clients,
  3033. ...currentAppState.mcp.clients,
  3034. ].find(c => c.name === serverName)
  3035. if (client && client.type === 'connected') {
  3036. await clearServerCache(serverName, config)
  3037. }
  3038. // Update appState.mcp to reflect disabled status and remove tools/commands/resources
  3039. const prefix = getMcpPrefix(serverName)
  3040. setAppState(prev => ({
  3041. ...prev,
  3042. mcp: {
  3043. ...prev.mcp,
  3044. clients: prev.mcp.clients.map(c =>
  3045. c.name === serverName
  3046. ? { name: serverName, type: 'disabled' as const, config }
  3047. : c,
  3048. ),
  3049. tools: reject(prev.mcp.tools, t => t.name?.startsWith(prefix)),
  3050. commands: reject(prev.mcp.commands, c =>
  3051. commandBelongsToServer(c, serverName),
  3052. ),
  3053. resources: omit(prev.mcp.resources, serverName),
  3054. },
  3055. }))
  3056. sendControlResponseSuccess(message)
  3057. } else {
  3058. // Enabling: persist + reconnect
  3059. setMcpServerEnabled(serverName, true)
  3060. const result = await reconnectMcpServerImpl(serverName, config)
  3061. // Update appState.mcp with the new client, tools, commands, and resources
  3062. // This ensures the LLM sees updated tools after enabling the server
  3063. const prefix = getMcpPrefix(serverName)
  3064. setAppState(prev => ({
  3065. ...prev,
  3066. mcp: {
  3067. ...prev.mcp,
  3068. clients: prev.mcp.clients.map(c =>
  3069. c.name === serverName ? result.client : c,
  3070. ),
  3071. tools: [
  3072. ...reject(prev.mcp.tools, t => t.name?.startsWith(prefix)),
  3073. ...result.tools,
  3074. ],
  3075. commands: [
  3076. ...reject(prev.mcp.commands, c =>
  3077. commandBelongsToServer(c, serverName),
  3078. ),
  3079. ...result.commands,
  3080. ],
  3081. resources:
  3082. result.resources && result.resources.length > 0
  3083. ? { ...prev.mcp.resources, [serverName]: result.resources }
  3084. : omit(prev.mcp.resources, serverName),
  3085. },
  3086. }))
  3087. if (result.client.type === 'connected') {
  3088. registerElicitationHandlers([result.client])
  3089. reregisterChannelHandlerAfterReconnect(result.client)
  3090. sendControlResponseSuccess(message)
  3091. } else {
  3092. const errorMessage =
  3093. result.client.type === 'failed'
  3094. ? (result.client.error ?? 'Connection failed')
  3095. : `Server status: ${result.client.type}`
  3096. sendControlResponseError(message, errorMessage)
  3097. }
  3098. }
  3099. } else if (message.request.subtype === 'channel_enable') {
  3100. const currentAppState = getAppState()
  3101. handleChannelEnable(
  3102. message.request_id,
  3103. message.request.serverName,
  3104. // Pool spread matches mcp_status — all three client sources.
  3105. [
  3106. ...currentAppState.mcp.clients,
  3107. ...sdkClients,
  3108. ...dynamicMcpState.clients,
  3109. ],
  3110. output,
  3111. )
  3112. } else if (message.request.subtype === 'mcp_authenticate') {
  3113. const { serverName } = message.request
  3114. const currentAppState = getAppState()
  3115. const config =
  3116. getMcpConfigByName(serverName) ??
  3117. mcpClients.find(c => c.name === serverName)?.config ??
  3118. currentAppState.mcp.clients.find(c => c.name === serverName)
  3119. ?.config ??
  3120. null
  3121. if (!config) {
  3122. sendControlResponseError(message, `Server not found: ${serverName}`)
  3123. } else if (config.type !== 'sse' && config.type !== 'http') {
  3124. sendControlResponseError(
  3125. message,
  3126. `Server type "${config.type}" does not support OAuth authentication`,
  3127. )
  3128. } else {
  3129. try {
  3130. // Abort any previous in-flight OAuth flow for this server
  3131. activeOAuthFlows.get(serverName)?.abort()
  3132. const controller = new AbortController()
  3133. activeOAuthFlows.set(serverName, controller)
  3134. // Capture the auth URL from the callback
  3135. let resolveAuthUrl: (url: string) => void
  3136. const authUrlPromise = new Promise<string>(resolve => {
  3137. resolveAuthUrl = resolve
  3138. })
  3139. // Start the OAuth flow in the background
  3140. const oauthPromise = performMCPOAuthFlow(
  3141. serverName,
  3142. config,
  3143. url => resolveAuthUrl!(url),
  3144. controller.signal,
  3145. {
  3146. skipBrowserOpen: true,
  3147. onWaitingForCallback: submit => {
  3148. oauthCallbackSubmitters.set(serverName, submit)
  3149. },
  3150. },
  3151. )
  3152. // Wait for the auth URL (or the flow to complete without needing redirect)
  3153. const authUrl = await Promise.race([
  3154. authUrlPromise,
  3155. oauthPromise.then(() => null as string | null),
  3156. ])
  3157. if (authUrl) {
  3158. sendControlResponseSuccess(message, {
  3159. authUrl,
  3160. requiresUserAction: true,
  3161. })
  3162. } else {
  3163. sendControlResponseSuccess(message, {
  3164. requiresUserAction: false,
  3165. })
  3166. }
  3167. // Store auth-only promise for mcp_oauth_callback_url handler.
  3168. // Don't swallow errors — the callback handler needs to detect
  3169. // auth failures and report them to the caller.
  3170. oauthAuthPromises.set(serverName, oauthPromise)
  3171. // Handle background completion — reconnect after auth.
  3172. // When manual callback is used, skip the reconnect here;
  3173. // the extension's handleAuthDone → mcp_reconnect handles it
  3174. // (which also updates dynamicMcpState for tool registration).
  3175. const fullFlowPromise = oauthPromise
  3176. .then(async () => {
  3177. // Don't reconnect if the server was disabled during the OAuth flow
  3178. if (isMcpServerDisabled(serverName)) {
  3179. return
  3180. }
  3181. // Skip reconnect if the manual callback path was used —
  3182. // handleAuthDone will do it via mcp_reconnect (which
  3183. // updates dynamicMcpState for tool registration).
  3184. if (oauthManualCallbackUsed.has(serverName)) {
  3185. return
  3186. }
  3187. // Reconnect the server after successful auth
  3188. const result = await reconnectMcpServerImpl(
  3189. serverName,
  3190. config,
  3191. )
  3192. const prefix = getMcpPrefix(serverName)
  3193. setAppState(prev => ({
  3194. ...prev,
  3195. mcp: {
  3196. ...prev.mcp,
  3197. clients: prev.mcp.clients.map(c =>
  3198. c.name === serverName ? result.client : c,
  3199. ),
  3200. tools: [
  3201. ...reject(prev.mcp.tools, t =>
  3202. t.name?.startsWith(prefix),
  3203. ),
  3204. ...result.tools,
  3205. ],
  3206. commands: [
  3207. ...reject(prev.mcp.commands, c =>
  3208. commandBelongsToServer(c, serverName),
  3209. ),
  3210. ...result.commands,
  3211. ],
  3212. resources:
  3213. result.resources && result.resources.length > 0
  3214. ? {
  3215. ...prev.mcp.resources,
  3216. [serverName]: result.resources,
  3217. }
  3218. : omit(prev.mcp.resources, serverName),
  3219. },
  3220. }))
  3221. // Also update dynamicMcpState so run() picks up the new tools
  3222. // on the next turn (run() reads dynamicMcpState, not appState)
  3223. dynamicMcpState = {
  3224. ...dynamicMcpState,
  3225. clients: [
  3226. ...dynamicMcpState.clients.filter(
  3227. c => c.name !== serverName,
  3228. ),
  3229. result.client,
  3230. ],
  3231. tools: [
  3232. ...dynamicMcpState.tools.filter(
  3233. t => !t.name?.startsWith(prefix),
  3234. ),
  3235. ...result.tools,
  3236. ],
  3237. }
  3238. })
  3239. .catch(error => {
  3240. logForDebugging(
  3241. `MCP OAuth failed for ${serverName}: ${error}`,
  3242. { level: 'error' },
  3243. )
  3244. })
  3245. .finally(() => {
  3246. // Clean up only if this is still the active flow
  3247. if (activeOAuthFlows.get(serverName) === controller) {
  3248. activeOAuthFlows.delete(serverName)
  3249. oauthCallbackSubmitters.delete(serverName)
  3250. oauthManualCallbackUsed.delete(serverName)
  3251. oauthAuthPromises.delete(serverName)
  3252. }
  3253. })
  3254. void fullFlowPromise
  3255. } catch (error) {
  3256. sendControlResponseError(message, errorMessage(error))
  3257. }
  3258. }
  3259. } else if (message.request.subtype === 'mcp_oauth_callback_url') {
  3260. const { serverName, callbackUrl } = message.request
  3261. const submit = oauthCallbackSubmitters.get(serverName)
  3262. if (submit) {
  3263. // Validate the callback URL before submitting. The submit
  3264. // callback in auth.ts silently ignores URLs missing a code
  3265. // param, which would leave the auth promise unresolved and
  3266. // block the control message loop until timeout.
  3267. let hasCodeOrError = false
  3268. try {
  3269. const parsed = new URL(callbackUrl)
  3270. hasCodeOrError =
  3271. parsed.searchParams.has('code') ||
  3272. parsed.searchParams.has('error')
  3273. } catch {
  3274. // Invalid URL
  3275. }
  3276. if (!hasCodeOrError) {
  3277. sendControlResponseError(
  3278. message,
  3279. 'Invalid callback URL: missing authorization code. Please paste the full redirect URL including the code parameter.',
  3280. )
  3281. } else {
  3282. oauthManualCallbackUsed.add(serverName)
  3283. submit(callbackUrl)
  3284. // Wait for auth (token exchange) to complete before responding.
  3285. // Reconnect is handled by the extension via handleAuthDone →
  3286. // mcp_reconnect (which updates dynamicMcpState for tools).
  3287. const authPromise = oauthAuthPromises.get(serverName)
  3288. if (authPromise) {
  3289. try {
  3290. await authPromise
  3291. sendControlResponseSuccess(message)
  3292. } catch (error) {
  3293. sendControlResponseError(
  3294. message,
  3295. error instanceof Error
  3296. ? error.message
  3297. : 'OAuth authentication failed',
  3298. )
  3299. }
  3300. } else {
  3301. sendControlResponseSuccess(message)
  3302. }
  3303. }
  3304. } else {
  3305. sendControlResponseError(
  3306. message,
  3307. `No active OAuth flow for server: ${serverName}`,
  3308. )
  3309. }
  3310. } else if (message.request.subtype === 'claude_authenticate') {
  3311. // Anthropic OAuth over the control channel. The SDK client owns
  3312. // the user's browser (we're headless in -p mode); we hand back
  3313. // both URLs and wait. Automatic URL → localhost listener catches
  3314. // the redirect if the browser is on this host; manual URL → the
  3315. // success page shows "code#state" for claude_oauth_callback.
  3316. const { loginWithClaudeAi } = message.request
  3317. // Clean up any prior flow. cleanup() closes the localhost listener
  3318. // and nulls the manual resolver. The prior `flow` promise is left
  3319. // pending (AuthCodeListener.close() does not reject) but its object
  3320. // graph becomes unreachable once the server handle is released and
  3321. // is GC'd — no fd or port is held.
  3322. claudeOAuth?.service.cleanup()
  3323. logEvent('tengu_oauth_flow_start', {
  3324. loginWithClaudeAi: loginWithClaudeAi ?? true,
  3325. })
  3326. const service = new OAuthService()
  3327. let urlResolver!: (urls: {
  3328. manualUrl: string
  3329. automaticUrl: string
  3330. }) => void
  3331. const urlPromise = new Promise<{
  3332. manualUrl: string
  3333. automaticUrl: string
  3334. }>(resolve => {
  3335. urlResolver = resolve
  3336. })
  3337. const flow = service
  3338. .startOAuthFlow(
  3339. async (manualUrl, automaticUrl) => {
  3340. // automaticUrl is always defined when skipBrowserOpen is set;
  3341. // the signature is optional only for the existing single-arg callers.
  3342. urlResolver({ manualUrl, automaticUrl: automaticUrl! })
  3343. },
  3344. {
  3345. loginWithClaudeAi: loginWithClaudeAi ?? true,
  3346. skipBrowserOpen: true,
  3347. },
  3348. )
  3349. .then(async tokens => {
  3350. // installOAuthTokens: performLogout (clear stale state) →
  3351. // store profile → saveOAuthTokensIfNeeded → clearOAuthTokenCache
  3352. // → clearAuthRelatedCaches. After this resolves, the memoized
  3353. // getClaudeAIOAuthTokens in this process is invalidated; the
  3354. // next API call re-reads keychain/file and works. No respawn.
  3355. await installOAuthTokens(tokens)
  3356. logEvent('tengu_oauth_success', {
  3357. loginWithClaudeAi: loginWithClaudeAi ?? true,
  3358. })
  3359. })
  3360. .finally(() => {
  3361. service.cleanup()
  3362. if (claudeOAuth?.service === service) {
  3363. claudeOAuth = null
  3364. }
  3365. })
  3366. claudeOAuth = { service, flow }
  3367. // Attach the rejection handler before awaiting so a synchronous
  3368. // startOAuthFlow failure doesn't surface as an unhandled rejection.
  3369. // The claude_oauth_callback handler re-awaits flow for the manual
  3370. // path and surfaces the real error to the client.
  3371. void flow.catch(err =>
  3372. logForDebugging(`claude_authenticate flow ended: ${err}`, {
  3373. level: 'info',
  3374. }),
  3375. )
  3376. try {
  3377. // Race against flow: if startOAuthFlow rejects before calling
  3378. // the authURLHandler (e.g. AuthCodeListener.start() fails with
  3379. // EACCES or fd exhaustion), urlPromise would pend forever and
  3380. // wedge the stdin loop. flow resolving first is unreachable in
  3381. // practice (it's suspended on the same urls we're waiting for).
  3382. const { manualUrl, automaticUrl } = await Promise.race([
  3383. urlPromise,
  3384. flow.then(() => {
  3385. throw new Error(
  3386. 'OAuth flow completed without producing auth URLs',
  3387. )
  3388. }),
  3389. ])
  3390. sendControlResponseSuccess(message, {
  3391. manualUrl,
  3392. automaticUrl,
  3393. })
  3394. } catch (error) {
  3395. sendControlResponseError(message, errorMessage(error))
  3396. }
  3397. } else if (
  3398. message.request.subtype === 'claude_oauth_callback' ||
  3399. message.request.subtype === 'claude_oauth_wait_for_completion'
  3400. ) {
  3401. if (!claudeOAuth) {
  3402. sendControlResponseError(
  3403. message,
  3404. 'No active claude_authenticate flow',
  3405. )
  3406. } else {
  3407. // Inject the manual code synchronously — must happen in stdin
  3408. // message order so a subsequent claude_authenticate doesn't
  3409. // replace the service before this code lands.
  3410. if (message.request.subtype === 'claude_oauth_callback') {
  3411. claudeOAuth.service.handleManualAuthCodeInput({
  3412. authorizationCode: message.request.authorizationCode,
  3413. state: message.request.state,
  3414. })
  3415. }
  3416. // Detach the await — the stdin reader is serial and blocking
  3417. // here deadlocks claude_oauth_wait_for_completion: flow may
  3418. // only resolve via a future claude_oauth_callback on stdin,
  3419. // which can't be read while we're parked. Capture the binding;
  3420. // claudeOAuth is nulled in flow's own .finally.
  3421. const { flow } = claudeOAuth
  3422. void flow.then(
  3423. () => {
  3424. const accountInfo = getAccountInformation()
  3425. sendControlResponseSuccess(message, {
  3426. account: {
  3427. email: accountInfo?.email,
  3428. organization: accountInfo?.organization,
  3429. subscriptionType: accountInfo?.subscription,
  3430. tokenSource: accountInfo?.tokenSource,
  3431. apiKeySource: accountInfo?.apiKeySource,
  3432. apiProvider: getAPIProvider(),
  3433. },
  3434. })
  3435. },
  3436. (error: unknown) =>
  3437. sendControlResponseError(message, errorMessage(error)),
  3438. )
  3439. }
  3440. } else if (message.request.subtype === 'mcp_clear_auth') {
  3441. const { serverName } = message.request
  3442. const currentAppState = getAppState()
  3443. const config =
  3444. getMcpConfigByName(serverName) ??
  3445. mcpClients.find(c => c.name === serverName)?.config ??
  3446. currentAppState.mcp.clients.find(c => c.name === serverName)
  3447. ?.config ??
  3448. null
  3449. if (!config) {
  3450. sendControlResponseError(message, `Server not found: ${serverName}`)
  3451. } else if (config.type !== 'sse' && config.type !== 'http') {
  3452. sendControlResponseError(
  3453. message,
  3454. `Cannot clear auth for server type "${config.type}"`,
  3455. )
  3456. } else {
  3457. await revokeServerTokens(serverName, config)
  3458. const result = await reconnectMcpServerImpl(serverName, config)
  3459. const prefix = getMcpPrefix(serverName)
  3460. setAppState(prev => ({
  3461. ...prev,
  3462. mcp: {
  3463. ...prev.mcp,
  3464. clients: prev.mcp.clients.map(c =>
  3465. c.name === serverName ? result.client : c,
  3466. ),
  3467. tools: [
  3468. ...reject(prev.mcp.tools, t => t.name?.startsWith(prefix)),
  3469. ...result.tools,
  3470. ],
  3471. commands: [
  3472. ...reject(prev.mcp.commands, c =>
  3473. commandBelongsToServer(c, serverName),
  3474. ),
  3475. ...result.commands,
  3476. ],
  3477. resources:
  3478. result.resources && result.resources.length > 0
  3479. ? {
  3480. ...prev.mcp.resources,
  3481. [serverName]: result.resources,
  3482. }
  3483. : omit(prev.mcp.resources, serverName),
  3484. },
  3485. }))
  3486. sendControlResponseSuccess(message, {})
  3487. }
  3488. } else if (message.request.subtype === 'apply_flag_settings') {
  3489. // Snapshot the current model before applying — we need to detect
  3490. // model switches so we can inject breadcrumbs and notify listeners.
  3491. const prevModel = getMainLoopModel()
  3492. // Merge the provided settings into the in-memory flag settings
  3493. const existing = getFlagSettingsInline() ?? {}
  3494. const incoming = message.request.settings
  3495. // Shallow-merge top-level keys; getSettingsForSource handles
  3496. // the deep merge with file-based flag settings via mergeWith.
  3497. // JSON serialization drops `undefined`, so callers use `null`
  3498. // to signal "clear this key". Convert nulls to deletions so
  3499. // SettingsSchema().safeParse() doesn't reject the whole object
  3500. // (z.string().optional() accepts string | undefined, not null).
  3501. const merged = { ...existing, ...incoming }
  3502. for (const key of Object.keys(merged)) {
  3503. if (merged[key as keyof typeof merged] === null) {
  3504. delete merged[key as keyof typeof merged]
  3505. }
  3506. }
  3507. setFlagSettingsInline(merged)
  3508. // Route through notifyChange so fanOut() resets the settings cache
  3509. // before listeners run. The subscriber at :392 calls
  3510. // applySettingsChange for us. Pre-#20625 this was a direct
  3511. // applySettingsChange() call that relied on its own internal reset —
  3512. // now that the reset is centralized in fanOut, a direct call here
  3513. // would read stale cached settings and silently drop the update.
  3514. // Bonus: going through notifyChange also tells the other subscribers
  3515. // (loadPluginHooks, sandbox-adapter) about the change, which the
  3516. // previous direct call skipped.
  3517. settingsChangeDetector.notifyChange('flagSettings')
  3518. // If the incoming settings include a model change, update the
  3519. // override so getMainLoopModel() reflects it. The override has
  3520. // higher priority than the settings cascade in
  3521. // getUserSpecifiedModelSetting(), so without this update,
  3522. // getMainLoopModel() returns the stale override and the model
  3523. // change is silently ignored (matching set_model at :2811).
  3524. if ('model' in incoming) {
  3525. if (incoming.model != null) {
  3526. setMainLoopModelOverride(String(incoming.model))
  3527. } else {
  3528. setMainLoopModelOverride(undefined)
  3529. }
  3530. }
  3531. // If the model changed, inject breadcrumbs so the model sees the
  3532. // mid-conversation switch, and notify metadata listeners (CCR).
  3533. const newModel = getMainLoopModel()
  3534. if (newModel !== prevModel) {
  3535. activeUserSpecifiedModel = newModel
  3536. const modelArg = incoming.model ? String(incoming.model) : 'default'
  3537. notifySessionMetadataChanged({ model: newModel })
  3538. injectModelSwitchBreadcrumbs(modelArg, newModel)
  3539. }
  3540. sendControlResponseSuccess(message)
  3541. } else if (message.request.subtype === 'get_settings') {
  3542. const currentAppState = getAppState()
  3543. const model = getMainLoopModel()
  3544. // modelSupportsEffort gate matches claude.ts — applied.effort must
  3545. // mirror what actually goes to the API, not just what's configured.
  3546. const effort = modelSupportsEffort(model)
  3547. ? resolveAppliedEffort(model, currentAppState.effortValue)
  3548. : undefined
  3549. sendControlResponseSuccess(message, {
  3550. ...getSettingsWithSources(),
  3551. applied: {
  3552. model,
  3553. // Numeric effort (ant-only) → null; SDK schema is string-level only.
  3554. effort: typeof effort === 'string' ? effort : null,
  3555. },
  3556. })
  3557. } else if (message.request.subtype === 'stop_task') {
  3558. const { task_id: taskId } = message.request
  3559. try {
  3560. await stopTask(taskId, {
  3561. getAppState,
  3562. setAppState,
  3563. })
  3564. sendControlResponseSuccess(message, {})
  3565. } catch (error) {
  3566. sendControlResponseError(message, errorMessage(error))
  3567. }
  3568. } else if (message.request.subtype === 'generate_session_title') {
  3569. // Fire-and-forget so the Haiku call does not block the stdin loop
  3570. // (which would delay processing of subsequent user messages /
  3571. // interrupts for the duration of the API roundtrip).
  3572. const { description, persist } = message.request
  3573. // Reuse the live controller only if it has not already been aborted
  3574. // (e.g. by interrupt()); an aborted signal would cause queryHaiku to
  3575. // immediately throw APIUserAbortError → {title: null}.
  3576. const titleSignal = (
  3577. abortController && !abortController.signal.aborted
  3578. ? abortController
  3579. : createAbortController()
  3580. ).signal
  3581. void (async () => {
  3582. try {
  3583. const title = await generateSessionTitle(description, titleSignal)
  3584. if (title && persist) {
  3585. try {
  3586. saveAiGeneratedTitle(getSessionId() as UUID, title)
  3587. } catch (e) {
  3588. logError(e)
  3589. }
  3590. }
  3591. sendControlResponseSuccess(message, { title })
  3592. } catch (e) {
  3593. // Unreachable in practice — generateSessionTitle wraps its
  3594. // own body and returns null, saveAiGeneratedTitle is wrapped
  3595. // above. Propagate (not swallow) so unexpected failures are
  3596. // visible to the SDK caller (hostComms.ts catches and logs).
  3597. sendControlResponseError(message, errorMessage(e))
  3598. }
  3599. })()
  3600. } else if (message.request.subtype === 'side_question') {
  3601. // Same fire-and-forget pattern as generate_session_title above —
  3602. // the forked agent's API roundtrip must not block the stdin loop.
  3603. //
  3604. // The snapshot captured by stopHooks (for querySource === 'sdk')
  3605. // holds the exact systemPrompt/userContext/systemContext/messages
  3606. // sent on the last main-thread turn. Reusing them gives a byte-
  3607. // identical prefix → prompt cache hit.
  3608. //
  3609. // Fallback (resume before first turn completes — no snapshot yet):
  3610. // rebuild from scratch. buildSideQuestionFallbackParams mirrors
  3611. // QueryEngine.ts:ask()'s system prompt assembly (including
  3612. // --system-prompt / --append-system-prompt) so the rebuilt prefix
  3613. // matches in the common case. May still miss the cache for
  3614. // coordinator mode or memory-mechanics extras — acceptable, the
  3615. // alternative is the side question failing entirely.
  3616. const { question } = message.request
  3617. void (async () => {
  3618. try {
  3619. const saved = getLastCacheSafeParams()
  3620. const cacheSafeParams = saved
  3621. ? {
  3622. ...saved,
  3623. // If the last turn was interrupted, the snapshot holds an
  3624. // already-aborted controller; createChildAbortController in
  3625. // createSubagentContext would propagate it and the fork
  3626. // would die before sending a request. The controller is
  3627. // not part of the cache key — swapping in a fresh one is
  3628. // safe. Same guard as generate_session_title above.
  3629. toolUseContext: {
  3630. ...saved.toolUseContext,
  3631. abortController: createAbortController(),
  3632. },
  3633. }
  3634. : await buildSideQuestionFallbackParams({
  3635. tools: buildAllTools(getAppState()),
  3636. commands: currentCommands,
  3637. mcpClients: [
  3638. ...getAppState().mcp.clients,
  3639. ...sdkClients,
  3640. ...dynamicMcpState.clients,
  3641. ],
  3642. messages: mutableMessages,
  3643. readFileState,
  3644. getAppState,
  3645. setAppState,
  3646. customSystemPrompt: options.systemPrompt,
  3647. appendSystemPrompt: options.appendSystemPrompt,
  3648. thinkingConfig: options.thinkingConfig,
  3649. agents: currentAgents,
  3650. })
  3651. const result = await runSideQuestion({
  3652. question,
  3653. cacheSafeParams,
  3654. })
  3655. sendControlResponseSuccess(message, { response: result.response })
  3656. } catch (e) {
  3657. sendControlResponseError(message, errorMessage(e))
  3658. }
  3659. })()
  3660. } else if (
  3661. (feature('PROACTIVE') || feature('KAIROS')) &&
  3662. (message.request as { subtype: string }).subtype === 'set_proactive'
  3663. ) {
  3664. const req = message.request as unknown as {
  3665. subtype: string
  3666. enabled: boolean
  3667. }
  3668. if (req.enabled) {
  3669. if (!proactiveModule!.isProactiveActive()) {
  3670. proactiveModule!.activateProactive('command')
  3671. scheduleProactiveTick!()
  3672. }
  3673. } else {
  3674. proactiveModule!.deactivateProactive()
  3675. }
  3676. sendControlResponseSuccess(message)
  3677. } else if (message.request.subtype === 'remote_control') {
  3678. if (message.request.enabled) {
  3679. if (bridgeHandle) {
  3680. // Already connected
  3681. sendControlResponseSuccess(message, {
  3682. session_url: getRemoteSessionUrl(
  3683. bridgeHandle.bridgeSessionId,
  3684. bridgeHandle.sessionIngressUrl,
  3685. ),
  3686. connect_url: buildBridgeConnectUrl(
  3687. bridgeHandle.environmentId,
  3688. bridgeHandle.sessionIngressUrl,
  3689. ),
  3690. environment_id: bridgeHandle.environmentId,
  3691. })
  3692. } else {
  3693. // initReplBridge surfaces gate-failure reasons via
  3694. // onStateChange('failed', detail) before returning null.
  3695. // Capture so the control-response error is actionable
  3696. // ("/login", "disabled by your organization's policy", etc.)
  3697. // instead of a generic "initialization failed".
  3698. let bridgeFailureDetail: string | undefined
  3699. try {
  3700. const { initReplBridge } = await import(
  3701. 'src/bridge/initReplBridge.js'
  3702. )
  3703. const handle = await initReplBridge({
  3704. onInboundMessage(msg) {
  3705. const fields = extractInboundMessageFields(msg)
  3706. if (!fields) return
  3707. const { content, uuid } = fields
  3708. enqueue({
  3709. value: content,
  3710. mode: 'prompt' as const,
  3711. uuid,
  3712. skipSlashCommands: true,
  3713. })
  3714. void run()
  3715. },
  3716. onPermissionResponse(response) {
  3717. // Forward bridge permission responses into the
  3718. // stdin processing loop so they resolve pending
  3719. // permission requests from the SDK consumer.
  3720. structuredIO.injectControlResponse(response)
  3721. },
  3722. onInterrupt() {
  3723. abortController?.abort()
  3724. },
  3725. onSetModel(model) {
  3726. const resolved =
  3727. model === 'default' ? getDefaultMainLoopModel() : model
  3728. activeUserSpecifiedModel = resolved
  3729. setMainLoopModelOverride(resolved)
  3730. },
  3731. onSetMaxThinkingTokens(maxTokens) {
  3732. if (maxTokens === null) {
  3733. options.thinkingConfig = undefined
  3734. } else if (maxTokens === 0) {
  3735. options.thinkingConfig = { type: 'disabled' }
  3736. } else {
  3737. options.thinkingConfig = {
  3738. type: 'enabled',
  3739. budgetTokens: maxTokens,
  3740. }
  3741. }
  3742. },
  3743. onStateChange(state, detail) {
  3744. if (state === 'failed') {
  3745. bridgeFailureDetail = detail
  3746. }
  3747. logForDebugging(
  3748. `[bridge:sdk] State change: ${state}${detail ? ` — ${detail}` : ''}`,
  3749. )
  3750. output.enqueue({
  3751. type: 'system' as StdoutMessage['type'],
  3752. subtype: 'bridge_state' as string,
  3753. state,
  3754. detail,
  3755. uuid: randomUUID(),
  3756. session_id: getSessionId(),
  3757. } as StdoutMessage)
  3758. },
  3759. initialMessages:
  3760. mutableMessages.length > 0 ? mutableMessages : undefined,
  3761. })
  3762. if (!handle) {
  3763. sendControlResponseError(
  3764. message,
  3765. bridgeFailureDetail ??
  3766. 'Remote Control initialization failed',
  3767. )
  3768. } else {
  3769. bridgeHandle = handle
  3770. bridgeLastForwardedIndex = mutableMessages.length
  3771. // Forward permission requests to the bridge
  3772. structuredIO.setOnControlRequestSent(request => {
  3773. handle.sendControlRequest(request)
  3774. })
  3775. // Cancel stale bridge permission prompts when the SDK
  3776. // consumer resolves a can_use_tool request first.
  3777. structuredIO.setOnControlRequestResolved(requestId => {
  3778. handle.sendControlCancelRequest(requestId)
  3779. })
  3780. sendControlResponseSuccess(message, {
  3781. session_url: getRemoteSessionUrl(
  3782. handle.bridgeSessionId,
  3783. handle.sessionIngressUrl,
  3784. ),
  3785. connect_url: buildBridgeConnectUrl(
  3786. handle.environmentId,
  3787. handle.sessionIngressUrl,
  3788. ),
  3789. environment_id: handle.environmentId,
  3790. })
  3791. }
  3792. } catch (err) {
  3793. sendControlResponseError(message, errorMessage(err))
  3794. }
  3795. }
  3796. } else {
  3797. // Disable
  3798. if (bridgeHandle) {
  3799. structuredIO.setOnControlRequestSent(undefined)
  3800. structuredIO.setOnControlRequestResolved(undefined)
  3801. await bridgeHandle.teardown()
  3802. bridgeHandle = null
  3803. }
  3804. sendControlResponseSuccess(message)
  3805. }
  3806. } else {
  3807. // Unknown control request subtype — send an error response so
  3808. // the caller doesn't hang waiting for a reply that never comes.
  3809. sendControlResponseError(
  3810. message,
  3811. `Unsupported control request subtype: ${(message.request as { subtype: string }).subtype}`,
  3812. )
  3813. }
  3814. continue
  3815. } else if (message.type === 'control_response') {
  3816. // Replay control_response messages when replay mode is enabled
  3817. if (options.replayUserMessages) {
  3818. output.enqueue(message)
  3819. }
  3820. continue
  3821. } else if (message.type === 'keep_alive') {
  3822. // Silently ignore keep-alive messages
  3823. continue
  3824. } else if (message.type === 'update_environment_variables') {
  3825. // Handled in structuredIO.ts, but TypeScript needs the type guard
  3826. continue
  3827. } else if (message.type === 'assistant' || message.type === 'system') {
  3828. // History replay from bridge: inject into mutableMessages as
  3829. // conversation context so the model sees prior turns.
  3830. const internalMsgs = toInternalMessages([message])
  3831. mutableMessages.push(...internalMsgs)
  3832. // Echo assistant messages back so CCR displays them
  3833. if (message.type === 'assistant' && options.replayUserMessages) {
  3834. output.enqueue(message)
  3835. }
  3836. continue
  3837. }
  3838. // After handling control, keep-alive, env-var, assistant, and system
  3839. // messages above, only user messages should remain.
  3840. if (message.type !== 'user') {
  3841. continue
  3842. }
  3843. // First prompt message implicitly initializes if not already done.
  3844. initialized = true
  3845. // Check for duplicate user message - skip if already processed
  3846. if (message.uuid) {
  3847. const sessionId = getSessionId() as UUID
  3848. const existsInSession = await doesMessageExistInSession(
  3849. sessionId,
  3850. message.uuid,
  3851. )
  3852. // Check both historical duplicates (from file) and runtime duplicates (this session)
  3853. if (existsInSession || receivedMessageUuids.has(message.uuid)) {
  3854. logForDebugging(`Skipping duplicate user message: ${message.uuid}`)
  3855. // Send acknowledgment for duplicate message if replay mode is enabled
  3856. if (options.replayUserMessages) {
  3857. logForDebugging(
  3858. `Sending acknowledgment for duplicate user message: ${message.uuid}`,
  3859. )
  3860. output.enqueue({
  3861. type: 'user',
  3862. content: message.message?.content ?? '',
  3863. message: message.message,
  3864. session_id: sessionId,
  3865. parent_tool_use_id: null,
  3866. uuid: message.uuid,
  3867. timestamp: message.timestamp,
  3868. isReplay: true,
  3869. } as unknown as SDKUserMessageReplay)
  3870. }
  3871. // Historical dup = transcript already has this turn's output, so it
  3872. // ran but its lifecycle was never closed (interrupted before ack).
  3873. // Runtime dups don't need this — the original enqueue path closes them.
  3874. if (existsInSession) {
  3875. notifyCommandLifecycle(message.uuid, 'completed')
  3876. }
  3877. // Don't enqueue duplicate messages for execution
  3878. continue
  3879. }
  3880. // Track this UUID to prevent runtime duplicates
  3881. trackReceivedMessageUuid(message.uuid)
  3882. }
  3883. enqueue({
  3884. mode: 'prompt' as const,
  3885. // file_attachments rides the protobuf catchall from the web composer.
  3886. // Same-ref no-op when absent (no 'file_attachments' key).
  3887. value: await resolveAndPrepend(message, message.message.content),
  3888. uuid: message.uuid,
  3889. priority: message.priority,
  3890. })
  3891. // Increment prompt count for attribution tracking and save snapshot
  3892. // The snapshot persists promptCount so it survives compaction
  3893. if (feature('COMMIT_ATTRIBUTION')) {
  3894. setAppState(prev => ({
  3895. ...prev,
  3896. attribution: incrementPromptCount(prev.attribution, snapshot => {
  3897. void recordAttributionSnapshot(snapshot).catch(error => {
  3898. logForDebugging(`Attribution: Failed to save snapshot: ${error}`)
  3899. })
  3900. }),
  3901. }))
  3902. }
  3903. void run()
  3904. }
  3905. inputClosed = true
  3906. cronScheduler?.stop()
  3907. if (!running) {
  3908. // If a push-suggestion is in-flight, wait for it to emit before closing
  3909. // the output stream (5 s safety timeout to prevent hanging).
  3910. if (suggestionState.inflightPromise) {
  3911. await Promise.race([suggestionState.inflightPromise, sleep(5000)])
  3912. }
  3913. suggestionState.abortController?.abort()
  3914. suggestionState.abortController = null
  3915. await finalizePendingAsyncHooks()
  3916. unsubscribeSkillChanges()
  3917. unsubscribeAuthStatus?.()
  3918. statusListeners.delete(rateLimitListener)
  3919. output.done()
  3920. }
  3921. })()
  3922. return output
  3923. }
  3924. /**
  3925. * Creates a CanUseToolFn that incorporates a custom permission prompt tool.
  3926. * This function converts the permissionPromptTool into a CanUseToolFn that can be used in ask.tsx
  3927. */
  3928. export function createCanUseToolWithPermissionPrompt(
  3929. permissionPromptTool: PermissionPromptTool,
  3930. ): CanUseToolFn {
  3931. const canUseTool: CanUseToolFn = async (
  3932. tool,
  3933. input,
  3934. toolUseContext,
  3935. assistantMessage,
  3936. toolUseId,
  3937. forceDecision,
  3938. ) => {
  3939. const mainPermissionResult =
  3940. forceDecision ??
  3941. (await hasPermissionsToUseTool(
  3942. tool,
  3943. input,
  3944. toolUseContext,
  3945. assistantMessage,
  3946. toolUseId,
  3947. ))
  3948. // If the tool is allowed or denied, return the result
  3949. if (
  3950. mainPermissionResult.behavior === 'allow' ||
  3951. mainPermissionResult.behavior === 'deny'
  3952. ) {
  3953. return mainPermissionResult
  3954. }
  3955. // Race the permission prompt tool against the abort signal.
  3956. //
  3957. // Why we need this: The permission prompt tool may block indefinitely waiting
  3958. // for user input (e.g., via stdin or a UI dialog). If the user triggers an
  3959. // interrupt (Ctrl+C), we need to detect it even while the tool is blocked.
  3960. // Without this race, the abort check would only run AFTER the tool completes,
  3961. // which may never happen if the tool is waiting for input that will never come.
  3962. //
  3963. // The second check (combinedSignal.aborted) handles a race condition where
  3964. // abort fires after Promise.race resolves but before we reach this check.
  3965. const { signal: combinedSignal, cleanup: cleanupAbortListener } =
  3966. createCombinedAbortSignal(toolUseContext.abortController.signal)
  3967. // Check if already aborted before starting the race
  3968. if (combinedSignal.aborted) {
  3969. cleanupAbortListener()
  3970. return {
  3971. behavior: 'deny',
  3972. message: 'Permission prompt was aborted.',
  3973. decisionReason: {
  3974. type: 'permissionPromptTool' as const,
  3975. permissionPromptToolName: tool.name,
  3976. toolResult: undefined,
  3977. },
  3978. }
  3979. }
  3980. const abortPromise = new Promise<'aborted'>(resolve => {
  3981. combinedSignal.addEventListener('abort', () => resolve('aborted'), {
  3982. once: true,
  3983. })
  3984. })
  3985. const toolCallPromise = permissionPromptTool.call(
  3986. {
  3987. tool_name: tool.name,
  3988. input,
  3989. tool_use_id: toolUseId,
  3990. },
  3991. toolUseContext,
  3992. canUseTool,
  3993. assistantMessage,
  3994. )
  3995. const raceResult = await Promise.race([toolCallPromise, abortPromise])
  3996. cleanupAbortListener()
  3997. if (raceResult === 'aborted' || combinedSignal.aborted) {
  3998. return {
  3999. behavior: 'deny',
  4000. message: 'Permission prompt was aborted.',
  4001. decisionReason: {
  4002. type: 'permissionPromptTool' as const,
  4003. permissionPromptToolName: tool.name,
  4004. toolResult: undefined,
  4005. },
  4006. }
  4007. }
  4008. // TypeScript narrowing: after the abort check, raceResult must be ToolResult
  4009. const result = raceResult as Awaited<typeof toolCallPromise>
  4010. const permissionToolResultBlockParam =
  4011. permissionPromptTool.mapToolResultToToolResultBlockParam(result.data, '1')
  4012. if (
  4013. !permissionToolResultBlockParam.content ||
  4014. !Array.isArray(permissionToolResultBlockParam.content) ||
  4015. !permissionToolResultBlockParam.content[0] ||
  4016. permissionToolResultBlockParam.content[0].type !== 'text' ||
  4017. typeof permissionToolResultBlockParam.content[0].text !== 'string'
  4018. ) {
  4019. throw new Error(
  4020. 'Permission prompt tool returned an invalid result. Expected a single text block param with type="text" and a string text value.',
  4021. )
  4022. }
  4023. return permissionPromptToolResultToPermissionDecision(
  4024. permissionToolOutputSchema().parse(
  4025. safeParseJSON(permissionToolResultBlockParam.content[0].text),
  4026. ),
  4027. permissionPromptTool,
  4028. input,
  4029. toolUseContext,
  4030. )
  4031. }
  4032. return canUseTool
  4033. }
  4034. // Exported for testing — regression: this used to crash at construction when
  4035. // getMcpTools() was empty (before per-server connects populated appState).
  4036. export function getCanUseToolFn(
  4037. permissionPromptToolName: string | undefined,
  4038. structuredIO: StructuredIO,
  4039. getMcpTools: () => Tool[],
  4040. onPermissionPrompt?: (details: RequiresActionDetails) => void,
  4041. ): CanUseToolFn {
  4042. if (permissionPromptToolName === 'stdio') {
  4043. return structuredIO.createCanUseTool(onPermissionPrompt)
  4044. }
  4045. if (!permissionPromptToolName) {
  4046. return async (
  4047. tool,
  4048. input,
  4049. toolUseContext,
  4050. assistantMessage,
  4051. toolUseId,
  4052. forceDecision,
  4053. ) =>
  4054. forceDecision ??
  4055. (await hasPermissionsToUseTool(
  4056. tool,
  4057. input,
  4058. toolUseContext,
  4059. assistantMessage,
  4060. toolUseId,
  4061. ))
  4062. }
  4063. // Lazy lookup: MCP connects are per-server incremental in print mode, so
  4064. // the tool may not be in appState yet at init time. Resolve on first call
  4065. // (first permission prompt), by which point connects have had time to finish.
  4066. let resolved: CanUseToolFn | null = null
  4067. return async (
  4068. tool,
  4069. input,
  4070. toolUseContext,
  4071. assistantMessage,
  4072. toolUseId,
  4073. forceDecision,
  4074. ) => {
  4075. if (!resolved) {
  4076. const mcpTools = getMcpTools()
  4077. const permissionPromptTool = mcpTools.find(t =>
  4078. toolMatchesName(t, permissionPromptToolName),
  4079. ) as PermissionPromptTool | undefined
  4080. if (!permissionPromptTool) {
  4081. const error = `Error: MCP tool ${permissionPromptToolName} (passed via --permission-prompt-tool) not found. Available MCP tools: ${mcpTools.map(t => t.name).join(', ') || 'none'}`
  4082. process.stderr.write(`${error}\n`)
  4083. gracefulShutdownSync(1)
  4084. throw new Error(error)
  4085. }
  4086. if (!permissionPromptTool.inputJSONSchema) {
  4087. const error = `Error: tool ${permissionPromptToolName} (passed via --permission-prompt-tool) must be an MCP tool`
  4088. process.stderr.write(`${error}\n`)
  4089. gracefulShutdownSync(1)
  4090. throw new Error(error)
  4091. }
  4092. resolved = createCanUseToolWithPermissionPrompt(permissionPromptTool)
  4093. }
  4094. return resolved(
  4095. tool,
  4096. input,
  4097. toolUseContext,
  4098. assistantMessage,
  4099. toolUseId,
  4100. forceDecision,
  4101. )
  4102. }
  4103. }
  4104. async function handleInitializeRequest(
  4105. request: SDKControlInitializeRequest,
  4106. requestId: string,
  4107. initialized: boolean,
  4108. output: Stream<StdoutMessage>,
  4109. commands: Command[],
  4110. modelInfos: ModelInfo[],
  4111. structuredIO: StructuredIO,
  4112. enableAuthStatus: boolean,
  4113. options: {
  4114. systemPrompt: string | undefined
  4115. appendSystemPrompt: string | undefined
  4116. agent?: string | undefined
  4117. userSpecifiedModel?: string | undefined
  4118. [key: string]: unknown
  4119. },
  4120. agents: AgentDefinition[],
  4121. getAppState: () => AppState,
  4122. ): Promise<void> {
  4123. if (initialized) {
  4124. output.enqueue({
  4125. type: 'control_response',
  4126. response: {
  4127. subtype: 'error',
  4128. error: 'Already initialized',
  4129. request_id: requestId,
  4130. pending_permission_requests:
  4131. structuredIO.getPendingPermissionRequests(),
  4132. },
  4133. })
  4134. return
  4135. }
  4136. // Apply systemPrompt/appendSystemPrompt from stdin to avoid ARG_MAX limits
  4137. if (request.systemPrompt !== undefined) {
  4138. options.systemPrompt = request.systemPrompt
  4139. }
  4140. if (request.appendSystemPrompt !== undefined) {
  4141. options.appendSystemPrompt = request.appendSystemPrompt
  4142. }
  4143. if (request.promptSuggestions !== undefined) {
  4144. options.promptSuggestions = request.promptSuggestions
  4145. }
  4146. // Merge agents from stdin to avoid ARG_MAX limits
  4147. if (request.agents) {
  4148. const stdinAgents = parseAgentsFromJson(request.agents, 'flagSettings')
  4149. agents.push(...stdinAgents)
  4150. }
  4151. // Re-evaluate main thread agent after SDK agents are merged
  4152. // This allows --agent to reference agents defined via SDK
  4153. if (options.agent) {
  4154. // If main.tsx already found this agent (filesystem-defined), it already
  4155. // applied systemPrompt/model/initialPrompt. Skip to avoid double-apply.
  4156. const alreadyResolved = getMainThreadAgentType() === options.agent
  4157. const mainThreadAgent = agents.find(a => a.agentType === options.agent)
  4158. if (mainThreadAgent && !alreadyResolved) {
  4159. // Update the main thread agent type in bootstrap state
  4160. setMainThreadAgentType(mainThreadAgent.agentType)
  4161. // Apply the agent's system prompt if user hasn't specified a custom one
  4162. // SDK agents are always custom agents (not built-in), so getSystemPrompt() takes no args
  4163. if (!options.systemPrompt && !isBuiltInAgent(mainThreadAgent)) {
  4164. const agentSystemPrompt = mainThreadAgent.getSystemPrompt()
  4165. if (agentSystemPrompt) {
  4166. options.systemPrompt = agentSystemPrompt
  4167. }
  4168. }
  4169. // Apply the agent's model if user didn't specify one and agent has a model
  4170. if (
  4171. !options.userSpecifiedModel &&
  4172. mainThreadAgent.model &&
  4173. mainThreadAgent.model !== 'inherit'
  4174. ) {
  4175. const agentModel = parseUserSpecifiedModel(mainThreadAgent.model)
  4176. setMainLoopModelOverride(agentModel)
  4177. }
  4178. // SDK-defined agents arrive via init, so main.tsx's lookup missed them.
  4179. if (mainThreadAgent.initialPrompt) {
  4180. structuredIO.prependUserMessage(mainThreadAgent.initialPrompt)
  4181. }
  4182. } else if (mainThreadAgent?.initialPrompt) {
  4183. // Filesystem-defined agent (alreadyResolved by main.tsx). main.tsx
  4184. // handles initialPrompt for the string inputPrompt case, but when
  4185. // inputPrompt is an AsyncIterable (SDK stream-json), it can't
  4186. // concatenate — fall back to prependUserMessage here.
  4187. structuredIO.prependUserMessage(mainThreadAgent.initialPrompt)
  4188. }
  4189. }
  4190. const settings = getSettings_DEPRECATED()
  4191. const outputStyle = settings?.outputStyle || DEFAULT_OUTPUT_STYLE_NAME
  4192. const availableOutputStyles = await getAllOutputStyles(getCwd())
  4193. // Get account information
  4194. const accountInfo = getAccountInformation()
  4195. if (request.hooks) {
  4196. const hooks: Partial<Record<HookEvent, HookCallbackMatcher[]>> = {}
  4197. for (const [event, matchers] of Object.entries(request.hooks) as [string, Array<{ hookCallbackIds: string[]; timeout?: number; matcher?: string }>][]) {
  4198. hooks[event as HookEvent] = matchers.map(matcher => {
  4199. const callbacks = matcher.hookCallbackIds.map(callbackId => {
  4200. return structuredIO.createHookCallback(callbackId, matcher.timeout)
  4201. })
  4202. return {
  4203. matcher: matcher.matcher,
  4204. hooks: callbacks,
  4205. }
  4206. })
  4207. }
  4208. registerHookCallbacks(hooks)
  4209. }
  4210. if (request.jsonSchema) {
  4211. setInitJsonSchema(request.jsonSchema)
  4212. }
  4213. const initResponse: SDKControlInitializeResponse = {
  4214. commands: commands
  4215. .filter(cmd => cmd.userInvocable !== false)
  4216. .map(cmd => ({
  4217. name: getCommandName(cmd),
  4218. description: formatDescriptionWithSource(cmd),
  4219. argumentHint: cmd.argumentHint || '',
  4220. })),
  4221. agents: agents.map(agent => ({
  4222. name: agent.agentType,
  4223. description: agent.whenToUse,
  4224. // 'inherit' is an internal sentinel; normalize to undefined for the public API
  4225. model: agent.model === 'inherit' ? undefined : agent.model,
  4226. })),
  4227. output_style: outputStyle,
  4228. available_output_styles: Object.keys(availableOutputStyles),
  4229. models: modelInfos,
  4230. account: {
  4231. email: accountInfo?.email,
  4232. organization: accountInfo?.organization,
  4233. subscriptionType: accountInfo?.subscription,
  4234. tokenSource: accountInfo?.tokenSource,
  4235. apiKeySource: accountInfo?.apiKeySource,
  4236. // getAccountInformation() returns undefined under 3P providers, so the
  4237. // other fields are all absent. apiProvider disambiguates "not logged
  4238. // in" (firstParty + tokenSource:none) from "3P, login not applicable".
  4239. apiProvider: getAPIProvider(),
  4240. },
  4241. pid: process.pid,
  4242. }
  4243. if (isFastModeEnabled() && isFastModeAvailable()) {
  4244. const appState = getAppState()
  4245. initResponse.fast_mode_state = getFastModeState(
  4246. options.userSpecifiedModel ?? null,
  4247. appState.fastMode,
  4248. )
  4249. }
  4250. output.enqueue({
  4251. type: 'control_response',
  4252. response: {
  4253. subtype: 'success',
  4254. request_id: requestId,
  4255. response: initResponse,
  4256. },
  4257. })
  4258. // After the initialize message, check the auth status-
  4259. // This will get notified of changes, but we also want to send the
  4260. // initial state.
  4261. if (enableAuthStatus) {
  4262. const authStatusManager = AwsAuthStatusManager.getInstance()
  4263. const status = authStatusManager.getStatus()
  4264. if (status) {
  4265. output.enqueue({
  4266. type: 'auth_status',
  4267. isAuthenticating: status.isAuthenticating,
  4268. output: status.output,
  4269. error: status.error,
  4270. uuid: randomUUID(),
  4271. session_id: getSessionId(),
  4272. })
  4273. }
  4274. }
  4275. }
  4276. async function handleRewindFiles(
  4277. userMessageId: UUID,
  4278. appState: AppState,
  4279. setAppState: (updater: (prev: AppState) => AppState) => void,
  4280. dryRun: boolean,
  4281. ): Promise<RewindFilesResult> {
  4282. if (!fileHistoryEnabled()) {
  4283. return { canRewind: false, error: 'File rewinding is not enabled.', filesChanged: [] }
  4284. }
  4285. if (!fileHistoryCanRestore(appState.fileHistory, userMessageId)) {
  4286. return {
  4287. canRewind: false,
  4288. error: 'No file checkpoint found for this message.',
  4289. filesChanged: [],
  4290. }
  4291. }
  4292. if (dryRun) {
  4293. const diffStats = await fileHistoryGetDiffStats(
  4294. appState.fileHistory,
  4295. userMessageId,
  4296. )
  4297. return {
  4298. canRewind: true,
  4299. filesChanged: diffStats?.filesChanged,
  4300. insertions: diffStats?.insertions,
  4301. deletions: diffStats?.deletions,
  4302. }
  4303. }
  4304. try {
  4305. await fileHistoryRewind(
  4306. updater =>
  4307. setAppState(prev => ({
  4308. ...prev,
  4309. fileHistory: updater(prev.fileHistory),
  4310. })),
  4311. userMessageId,
  4312. )
  4313. } catch (error) {
  4314. return {
  4315. canRewind: false,
  4316. error: `Failed to rewind: ${errorMessage(error)}`,
  4317. filesChanged: [],
  4318. }
  4319. }
  4320. return { canRewind: true, filesChanged: [] }
  4321. }
  4322. function handleSetPermissionMode(
  4323. request: { mode: InternalPermissionMode },
  4324. requestId: string,
  4325. toolPermissionContext: ToolPermissionContext,
  4326. output: Stream<StdoutMessage>,
  4327. ): ToolPermissionContext {
  4328. // Check if trying to switch to bypassPermissions mode
  4329. if (request.mode === 'bypassPermissions') {
  4330. if (isBypassPermissionsModeDisabled()) {
  4331. output.enqueue({
  4332. type: 'control_response',
  4333. response: {
  4334. subtype: 'error',
  4335. request_id: requestId,
  4336. error:
  4337. 'Cannot set permission mode to bypassPermissions because it is disabled by settings or configuration',
  4338. },
  4339. })
  4340. return toolPermissionContext
  4341. }
  4342. if (!toolPermissionContext.isBypassPermissionsModeAvailable) {
  4343. output.enqueue({
  4344. type: 'control_response',
  4345. response: {
  4346. subtype: 'error',
  4347. request_id: requestId,
  4348. error:
  4349. 'Cannot set permission mode to bypassPermissions because the session was not launched with --dangerously-skip-permissions',
  4350. },
  4351. })
  4352. return toolPermissionContext
  4353. }
  4354. }
  4355. // Check if trying to switch to auto mode without the classifier gate
  4356. if (
  4357. feature('TRANSCRIPT_CLASSIFIER') &&
  4358. request.mode === 'auto' &&
  4359. !isAutoModeGateEnabled()
  4360. ) {
  4361. const reason = getAutoModeUnavailableReason()
  4362. output.enqueue({
  4363. type: 'control_response',
  4364. response: {
  4365. subtype: 'error',
  4366. request_id: requestId,
  4367. error: reason
  4368. ? `Cannot set permission mode to auto: ${getAutoModeUnavailableNotification(reason)}`
  4369. : 'Cannot set permission mode to auto',
  4370. },
  4371. })
  4372. return toolPermissionContext
  4373. }
  4374. // Allow the mode switch
  4375. output.enqueue({
  4376. type: 'control_response',
  4377. response: {
  4378. subtype: 'success',
  4379. request_id: requestId,
  4380. response: {
  4381. mode: request.mode,
  4382. },
  4383. },
  4384. })
  4385. return {
  4386. ...transitionPermissionMode(
  4387. toolPermissionContext.mode,
  4388. request.mode,
  4389. toolPermissionContext,
  4390. ),
  4391. mode: request.mode,
  4392. }
  4393. }
  4394. /**
  4395. * IDE-triggered channel enable. Derives the ChannelEntry from the connection's
  4396. * pluginSource (IDE can't spoof kind/marketplace — we only take the server
  4397. * name), appends it to session allowedChannels, and runs the full gate. On
  4398. * gate failure, rolls back the append. On success, registers a notification
  4399. * handler that enqueues channel messages at priority:'next' — drainCommandQueue
  4400. * picks them up between turns.
  4401. *
  4402. * Intentionally does NOT register the claude/channel/permission handler that
  4403. * useManageMCPConnections sets up for interactive mode. That handler resolves
  4404. * a pending dialog inside handleInteractivePermission — but print.ts never
  4405. * calls handleInteractivePermission. When SDK permission lands on 'ask', it
  4406. * goes to the consumer's canUseTool callback over stdio; there is no CLI-side
  4407. * dialog for a remote "yes tbxkq" to resolve. If an IDE wants channel-relayed
  4408. * tool approval, that's IDE-side plumbing against its own pending-map. (Also
  4409. * gated separately by tengu_harbor_permissions — not yet shipping on
  4410. * interactive either.)
  4411. */
  4412. function handleChannelEnable(
  4413. requestId: string,
  4414. serverName: string,
  4415. connectionPool: readonly MCPServerConnection[],
  4416. output: Stream<StdoutMessage>,
  4417. ): void {
  4418. const respondError = (error: string) =>
  4419. output.enqueue({
  4420. type: 'control_response',
  4421. response: { subtype: 'error', request_id: requestId, error },
  4422. })
  4423. if (!(feature('KAIROS') || feature('KAIROS_CHANNELS'))) {
  4424. return respondError('channels feature not available in this build')
  4425. }
  4426. // Only a 'connected' client has .capabilities and .client to register the
  4427. // handler on. The pool spread at the call site matches mcp_status.
  4428. const connection = connectionPool.find(
  4429. c => c.name === serverName && c.type === 'connected',
  4430. )
  4431. if (!connection || connection.type !== 'connected') {
  4432. return respondError(`server ${serverName} is not connected`)
  4433. }
  4434. const pluginSource = connection.config.pluginSource
  4435. const parsed = pluginSource ? parsePluginIdentifier(pluginSource) : undefined
  4436. if (!parsed?.marketplace) {
  4437. // No pluginSource or @-less source — can never pass the {plugin,
  4438. // marketplace}-keyed allowlist. Short-circuit with the same reason the
  4439. // gate would produce.
  4440. return respondError(
  4441. `server ${serverName} is not plugin-sourced; channel_enable requires a marketplace plugin`,
  4442. )
  4443. }
  4444. const entry: ChannelEntry = {
  4445. kind: 'plugin',
  4446. name: parsed.name,
  4447. marketplace: parsed.marketplace,
  4448. }
  4449. // Idempotency: don't double-append on repeat enable.
  4450. const prior = getAllowedChannels()
  4451. const already = prior.some(
  4452. e =>
  4453. e.kind === 'plugin' &&
  4454. e.name === entry.name &&
  4455. e.marketplace === entry.marketplace,
  4456. )
  4457. if (!already) setAllowedChannels([...prior, entry])
  4458. const gate = gateChannelServer(
  4459. serverName,
  4460. connection.capabilities,
  4461. pluginSource,
  4462. )
  4463. if (gate.action === 'skip') {
  4464. // Rollback — only remove the entry we appended.
  4465. if (!already) setAllowedChannels(prior)
  4466. return respondError(gate.reason)
  4467. }
  4468. const pluginId =
  4469. `${entry.name}@${entry.marketplace}` as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
  4470. logMCPDebug(serverName, 'Channel notifications registered')
  4471. logEvent('tengu_mcp_channel_enable', { plugin: pluginId })
  4472. // Identical enqueue shape to the interactive register block in
  4473. // useManageMCPConnections. drainCommandQueue processes it between turns —
  4474. // channel messages queue at priority 'next' and are seen by the model on
  4475. // the turn after they arrive.
  4476. connection.client.setNotificationHandler(
  4477. ChannelMessageNotificationSchema(),
  4478. async notification => {
  4479. const { content, meta } = notification.params
  4480. logMCPDebug(
  4481. serverName,
  4482. `notifications/claude/channel: ${content.slice(0, 80)}`,
  4483. )
  4484. logEvent('tengu_mcp_channel_message', {
  4485. content_length: content.length,
  4486. meta_key_count: Object.keys(meta ?? {}).length,
  4487. entry_kind:
  4488. 'plugin' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  4489. is_dev: false,
  4490. plugin: pluginId,
  4491. })
  4492. enqueue({
  4493. mode: 'prompt',
  4494. value: wrapChannelMessage(serverName, content, meta),
  4495. priority: 'next',
  4496. isMeta: true,
  4497. origin: { kind: 'channel', server: serverName } as unknown as string,
  4498. skipSlashCommands: true,
  4499. })
  4500. },
  4501. )
  4502. output.enqueue({
  4503. type: 'control_response',
  4504. response: {
  4505. subtype: 'success',
  4506. request_id: requestId,
  4507. response: undefined,
  4508. },
  4509. })
  4510. }
  4511. /**
  4512. * Re-register the channel notification handler after mcp_reconnect /
  4513. * mcp_toggle creates a new client. handleChannelEnable bound the handler to
  4514. * the OLD client object; allowedChannels survives the reconnect but the
  4515. * handler binding does not. Without this, channel messages silently drop
  4516. * after a reconnect while the IDE still believes the channel is live.
  4517. *
  4518. * Mirrors the interactive CLI's onConnectionAttempt in
  4519. * useManageMCPConnections, which re-gates on every new connection. Paired
  4520. * with registerElicitationHandlers at the same call sites.
  4521. *
  4522. * No-op if the server was never channel-enabled: gateChannelServer calls
  4523. * findChannelEntry internally and returns skip/session for an unlisted
  4524. * server, so reconnecting a non-channel MCP server costs one feature-flag
  4525. * check.
  4526. */
  4527. function reregisterChannelHandlerAfterReconnect(
  4528. connection: MCPServerConnection,
  4529. ): void {
  4530. if (!(feature('KAIROS') || feature('KAIROS_CHANNELS'))) return
  4531. if (connection.type !== 'connected') return
  4532. const gate = gateChannelServer(
  4533. connection.name,
  4534. connection.capabilities,
  4535. connection.config.pluginSource,
  4536. )
  4537. if (gate.action !== 'register') return
  4538. const entry = findChannelEntry(connection.name, getAllowedChannels())
  4539. const pluginId =
  4540. entry?.kind === 'plugin'
  4541. ? (`${entry.name}@${entry.marketplace}` as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
  4542. : undefined
  4543. logMCPDebug(
  4544. connection.name,
  4545. 'Channel notifications re-registered after reconnect',
  4546. )
  4547. connection.client.setNotificationHandler(
  4548. ChannelMessageNotificationSchema(),
  4549. async notification => {
  4550. const { content, meta } = notification.params
  4551. logMCPDebug(
  4552. connection.name,
  4553. `notifications/claude/channel: ${content.slice(0, 80)}`,
  4554. )
  4555. logEvent('tengu_mcp_channel_message', {
  4556. content_length: content.length,
  4557. meta_key_count: Object.keys(meta ?? {}).length,
  4558. entry_kind:
  4559. entry?.kind as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
  4560. is_dev: entry?.dev ?? false,
  4561. plugin: pluginId,
  4562. })
  4563. enqueue({
  4564. mode: 'prompt',
  4565. value: wrapChannelMessage(connection.name, content, meta),
  4566. priority: 'next',
  4567. isMeta: true,
  4568. origin: { kind: 'channel', server: connection.name } as unknown as string,
  4569. skipSlashCommands: true,
  4570. })
  4571. },
  4572. )
  4573. }
  4574. /**
  4575. * Emits an error message in the correct format based on outputFormat.
  4576. * When using stream-json, writes JSON to stdout; otherwise writes plain text to stderr.
  4577. */
  4578. function emitLoadError(
  4579. message: string,
  4580. outputFormat: string | undefined,
  4581. ): void {
  4582. if (outputFormat === 'stream-json') {
  4583. const errorResult = {
  4584. type: 'result',
  4585. subtype: 'error_during_execution',
  4586. duration_ms: 0,
  4587. duration_api_ms: 0,
  4588. is_error: true,
  4589. num_turns: 0,
  4590. stop_reason: null,
  4591. session_id: getSessionId(),
  4592. total_cost_usd: 0,
  4593. usage: EMPTY_USAGE,
  4594. modelUsage: {},
  4595. permission_denials: [],
  4596. uuid: randomUUID(),
  4597. errors: [message],
  4598. }
  4599. process.stdout.write(jsonStringify(errorResult) + '\n')
  4600. } else {
  4601. process.stderr.write(message + '\n')
  4602. }
  4603. }
  4604. /**
  4605. * Removes an interrupted user message and its synthetic assistant sentinel
  4606. * from the message array. Used during gateway-triggered restarts to clean up
  4607. * the message history before re-enqueuing the interrupted prompt.
  4608. *
  4609. * @internal Exported for testing
  4610. */
  4611. export function removeInterruptedMessage(
  4612. messages: Message[],
  4613. interruptedUserMessage: NormalizedUserMessage,
  4614. ): void {
  4615. const idx = messages.findIndex(m => m.uuid === interruptedUserMessage.uuid)
  4616. if (idx !== -1) {
  4617. // Remove the user message and the sentinel that immediately follows it.
  4618. // splice safely handles the case where idx is the last element.
  4619. messages.splice(idx, 2)
  4620. }
  4621. }
  4622. type LoadInitialMessagesResult = {
  4623. messages: Message[]
  4624. turnInterruptionState?: TurnInterruptionState
  4625. agentSetting?: string
  4626. }
  4627. async function loadInitialMessages(
  4628. setAppState: (f: (prev: AppState) => AppState) => void,
  4629. options: {
  4630. continue: boolean | undefined
  4631. teleport: string | true | null | undefined
  4632. resume: string | boolean | undefined
  4633. resumeSessionAt: string | undefined
  4634. forkSession: boolean | undefined
  4635. outputFormat: string | undefined
  4636. sessionStartHooksPromise?: ReturnType<typeof processSessionStartHooks>
  4637. restoredWorkerState: Promise<SessionExternalMetadata | null>
  4638. },
  4639. ): Promise<LoadInitialMessagesResult> {
  4640. const persistSession = !isSessionPersistenceDisabled()
  4641. // Handle continue in print mode
  4642. if (options.continue) {
  4643. try {
  4644. logEvent('tengu_continue_print', {})
  4645. const result = await loadConversationForResume(
  4646. undefined /* sessionId */,
  4647. undefined /* file path */,
  4648. )
  4649. if (result) {
  4650. // Match coordinator mode to the resumed session's mode
  4651. if (feature('COORDINATOR_MODE') && coordinatorModeModule) {
  4652. const warning = coordinatorModeModule.matchSessionMode(result.mode)
  4653. if (warning) {
  4654. process.stderr.write(warning + '\n')
  4655. // Refresh agent definitions to reflect the mode switch
  4656. const {
  4657. getAgentDefinitionsWithOverrides,
  4658. getActiveAgentsFromList,
  4659. } =
  4660. // eslint-disable-next-line @typescript-eslint/no-require-imports
  4661. require('../tools/AgentTool/loadAgentsDir.js') as typeof import('../tools/AgentTool/loadAgentsDir.js')
  4662. getAgentDefinitionsWithOverrides.cache.clear?.()
  4663. const freshAgentDefs = await getAgentDefinitionsWithOverrides(
  4664. getCwd(),
  4665. )
  4666. setAppState(prev => ({
  4667. ...prev,
  4668. agentDefinitions: {
  4669. ...freshAgentDefs,
  4670. allAgents: freshAgentDefs.allAgents,
  4671. activeAgents: getActiveAgentsFromList(freshAgentDefs.allAgents),
  4672. },
  4673. }))
  4674. }
  4675. }
  4676. // Reuse the resumed session's ID
  4677. if (!options.forkSession) {
  4678. if (result.sessionId) {
  4679. switchSession(
  4680. asSessionId(result.sessionId),
  4681. result.fullPath ? dirname(result.fullPath) : null,
  4682. )
  4683. if (persistSession) {
  4684. await resetSessionFilePointer()
  4685. }
  4686. }
  4687. }
  4688. restoreSessionStateFromLog(result, setAppState)
  4689. // Restore session metadata so it's re-appended on exit via reAppendSessionMetadata
  4690. restoreSessionMetadata(
  4691. options.forkSession
  4692. ? { ...result, worktreeSession: undefined }
  4693. : result,
  4694. )
  4695. // Write mode entry for the resumed session
  4696. if (feature('COORDINATOR_MODE') && coordinatorModeModule) {
  4697. saveMode(
  4698. coordinatorModeModule.isCoordinatorMode()
  4699. ? 'coordinator'
  4700. : 'normal',
  4701. )
  4702. }
  4703. return {
  4704. messages: result.messages,
  4705. turnInterruptionState: result.turnInterruptionState,
  4706. agentSetting: result.agentSetting,
  4707. }
  4708. }
  4709. } catch (error) {
  4710. logError(error)
  4711. gracefulShutdownSync(1)
  4712. return { messages: [] }
  4713. }
  4714. }
  4715. // Handle teleport in print mode
  4716. if (options.teleport) {
  4717. try {
  4718. if (!isPolicyAllowed('allow_remote_sessions')) {
  4719. throw new Error(
  4720. "Remote sessions are disabled by your organization's policy.",
  4721. )
  4722. }
  4723. logEvent('tengu_teleport_print', {})
  4724. if (typeof options.teleport !== 'string') {
  4725. throw new Error('No session ID provided for teleport')
  4726. }
  4727. const {
  4728. checkOutTeleportedSessionBranch,
  4729. processMessagesForTeleportResume,
  4730. teleportResumeCodeSession,
  4731. validateGitState,
  4732. } = await import('src/utils/teleport.js')
  4733. await validateGitState()
  4734. const teleportResult = await teleportResumeCodeSession(options.teleport)
  4735. const { branchError } = await checkOutTeleportedSessionBranch(
  4736. teleportResult.branch,
  4737. )
  4738. return {
  4739. messages: processMessagesForTeleportResume(
  4740. teleportResult.log,
  4741. branchError,
  4742. ),
  4743. }
  4744. } catch (error) {
  4745. logError(error)
  4746. gracefulShutdownSync(1)
  4747. return { messages: [] }
  4748. }
  4749. }
  4750. // Handle resume in print mode (accepts session ID or URL)
  4751. // URLs are [ANT-ONLY]
  4752. if (options.resume) {
  4753. try {
  4754. logEvent('tengu_resume_print', {})
  4755. // In print mode - we require a valid session ID, JSONL file or URL
  4756. const parsedSessionId = parseSessionIdentifier(
  4757. typeof options.resume === 'string' ? options.resume : '',
  4758. )
  4759. if (!parsedSessionId) {
  4760. let errorMessage =
  4761. 'Error: --resume requires a valid session ID when used with --print. Usage: claude -p --resume <session-id>'
  4762. if (typeof options.resume === 'string') {
  4763. errorMessage += `. Session IDs must be in UUID format (e.g., 550e8400-e29b-41d4-a716-446655440000). Provided value "${options.resume}" is not a valid UUID`
  4764. }
  4765. emitLoadError(errorMessage, options.outputFormat)
  4766. gracefulShutdownSync(1)
  4767. return { messages: [] }
  4768. }
  4769. // Hydrate local transcript from remote before loading
  4770. if (isEnvTruthy(process.env.CLAUDE_CODE_USE_CCR_V2)) {
  4771. // Await restore alongside hydration so SSE catchup lands on
  4772. // restored state, not a fresh default.
  4773. const [, metadata] = await Promise.all([
  4774. hydrateFromCCRv2InternalEvents(parsedSessionId.sessionId),
  4775. options.restoredWorkerState,
  4776. ])
  4777. if (metadata) {
  4778. setAppState(externalMetadataToAppState(metadata))
  4779. if (typeof metadata.model === 'string') {
  4780. setMainLoopModelOverride(metadata.model)
  4781. }
  4782. }
  4783. } else if (
  4784. parsedSessionId.isUrl &&
  4785. parsedSessionId.ingressUrl &&
  4786. isEnvTruthy(process.env.ENABLE_SESSION_PERSISTENCE)
  4787. ) {
  4788. // v1: fetch session logs from Session Ingress
  4789. await hydrateRemoteSession(
  4790. parsedSessionId.sessionId,
  4791. parsedSessionId.ingressUrl,
  4792. )
  4793. }
  4794. // Load the conversation with the specified session ID
  4795. const result = await loadConversationForResume(
  4796. parsedSessionId.sessionId,
  4797. parsedSessionId.jsonlFile || undefined,
  4798. )
  4799. // hydrateFromCCRv2InternalEvents writes an empty transcript file for
  4800. // fresh sessions (writeFile(sessionFile, '') with zero events), so
  4801. // loadConversationForResume returns {messages: []} not null. Treat
  4802. // empty the same as null so SessionStart still fires.
  4803. if (!result || result.messages.length === 0) {
  4804. // For URL-based or CCR v2 resume, start with empty session (it was hydrated but empty)
  4805. if (
  4806. parsedSessionId.isUrl ||
  4807. isEnvTruthy(process.env.CLAUDE_CODE_USE_CCR_V2)
  4808. ) {
  4809. // Execute SessionStart hooks for startup since we're starting a new session
  4810. return {
  4811. messages: await (options.sessionStartHooksPromise ??
  4812. processSessionStartHooks('startup')),
  4813. }
  4814. } else {
  4815. emitLoadError(
  4816. `No conversation found with session ID: ${parsedSessionId.sessionId}`,
  4817. options.outputFormat,
  4818. )
  4819. gracefulShutdownSync(1)
  4820. return { messages: [] }
  4821. }
  4822. }
  4823. // Handle resumeSessionAt feature
  4824. if (options.resumeSessionAt) {
  4825. const index = result.messages.findIndex(
  4826. m => m.uuid === options.resumeSessionAt,
  4827. )
  4828. if (index < 0) {
  4829. emitLoadError(
  4830. `No message found with message.uuid of: ${options.resumeSessionAt}`,
  4831. options.outputFormat,
  4832. )
  4833. gracefulShutdownSync(1)
  4834. return { messages: [] }
  4835. }
  4836. result.messages = index >= 0 ? result.messages.slice(0, index + 1) : []
  4837. }
  4838. // Match coordinator mode to the resumed session's mode
  4839. if (feature('COORDINATOR_MODE') && coordinatorModeModule) {
  4840. const warning = coordinatorModeModule.matchSessionMode(result.mode)
  4841. if (warning) {
  4842. process.stderr.write(warning + '\n')
  4843. // Refresh agent definitions to reflect the mode switch
  4844. const { getAgentDefinitionsWithOverrides, getActiveAgentsFromList } =
  4845. // eslint-disable-next-line @typescript-eslint/no-require-imports
  4846. require('../tools/AgentTool/loadAgentsDir.js') as typeof import('../tools/AgentTool/loadAgentsDir.js')
  4847. getAgentDefinitionsWithOverrides.cache.clear?.()
  4848. const freshAgentDefs = await getAgentDefinitionsWithOverrides(
  4849. getCwd(),
  4850. )
  4851. setAppState(prev => ({
  4852. ...prev,
  4853. agentDefinitions: {
  4854. ...freshAgentDefs,
  4855. allAgents: freshAgentDefs.allAgents,
  4856. activeAgents: getActiveAgentsFromList(freshAgentDefs.allAgents),
  4857. },
  4858. }))
  4859. }
  4860. }
  4861. // Reuse the resumed session's ID
  4862. if (!options.forkSession && result.sessionId) {
  4863. switchSession(
  4864. asSessionId(result.sessionId),
  4865. result.fullPath ? dirname(result.fullPath) : null,
  4866. )
  4867. if (persistSession) {
  4868. await resetSessionFilePointer()
  4869. }
  4870. }
  4871. restoreSessionStateFromLog(result, setAppState)
  4872. // Restore session metadata so it's re-appended on exit via reAppendSessionMetadata
  4873. restoreSessionMetadata(
  4874. options.forkSession
  4875. ? { ...result, worktreeSession: undefined }
  4876. : result,
  4877. )
  4878. // Write mode entry for the resumed session
  4879. if (feature('COORDINATOR_MODE') && coordinatorModeModule) {
  4880. saveMode(
  4881. coordinatorModeModule.isCoordinatorMode() ? 'coordinator' : 'normal',
  4882. )
  4883. }
  4884. return {
  4885. messages: result.messages,
  4886. turnInterruptionState: result.turnInterruptionState,
  4887. agentSetting: result.agentSetting,
  4888. }
  4889. } catch (error) {
  4890. logError(error)
  4891. const errorMessage =
  4892. error instanceof Error
  4893. ? `Failed to resume session: ${error.message}`
  4894. : 'Failed to resume session with --print mode'
  4895. emitLoadError(errorMessage, options.outputFormat)
  4896. gracefulShutdownSync(1)
  4897. return { messages: [] }
  4898. }
  4899. }
  4900. // Join the SessionStart hooks promise kicked in main.tsx (or run fresh if
  4901. // it wasn't kicked — e.g. --continue with no prior session falls through
  4902. // here with sessionStartHooksPromise undefined because main.tsx guards on continue)
  4903. return {
  4904. messages: await (options.sessionStartHooksPromise ??
  4905. processSessionStartHooks('startup')),
  4906. }
  4907. }
  4908. function getStructuredIO(
  4909. inputPrompt: string | AsyncIterable<string>,
  4910. options: {
  4911. sdkUrl: string | undefined
  4912. replayUserMessages?: boolean
  4913. },
  4914. ): StructuredIO {
  4915. let inputStream: AsyncIterable<string>
  4916. if (typeof inputPrompt === 'string') {
  4917. if (inputPrompt.trim() !== '') {
  4918. // Normalize to a streaming input.
  4919. inputStream = fromArray([
  4920. jsonStringify({
  4921. type: 'user',
  4922. content: inputPrompt,
  4923. uuid: '',
  4924. session_id: '',
  4925. message: {
  4926. role: 'user',
  4927. content: inputPrompt,
  4928. },
  4929. parent_tool_use_id: null,
  4930. } satisfies SDKUserMessage),
  4931. ])
  4932. } else {
  4933. // Empty string - create empty stream
  4934. inputStream = fromArray([])
  4935. }
  4936. } else {
  4937. inputStream = inputPrompt
  4938. }
  4939. // Use RemoteIO if sdkUrl is provided, otherwise use regular StructuredIO
  4940. return options.sdkUrl
  4941. ? new RemoteIO(options.sdkUrl, inputStream, options.replayUserMessages)
  4942. : new StructuredIO(inputStream, options.replayUserMessages)
  4943. }
  4944. /**
  4945. * Handles unexpected permission responses by looking up the unresolved tool
  4946. * call in the transcript and enqueuing it for execution.
  4947. *
  4948. * Returns true if a permission was enqueued, false otherwise.
  4949. */
  4950. export async function handleOrphanedPermissionResponse({
  4951. message,
  4952. setAppState,
  4953. onEnqueued,
  4954. handledToolUseIds,
  4955. }: {
  4956. message: SDKControlResponse
  4957. setAppState: (f: (prev: AppState) => AppState) => void
  4958. onEnqueued?: () => void
  4959. handledToolUseIds: Set<string>
  4960. }): Promise<boolean> {
  4961. const responseInner = message.response as { subtype?: string; response?: Record<string, unknown>; request_id?: string } | undefined
  4962. if (
  4963. responseInner?.subtype === 'success' &&
  4964. responseInner.response?.toolUseID &&
  4965. typeof responseInner.response.toolUseID === 'string'
  4966. ) {
  4967. const permissionResult = responseInner.response as PermissionResult & { toolUseID?: string }
  4968. const toolUseID = permissionResult.toolUseID
  4969. if (!toolUseID) {
  4970. return false
  4971. }
  4972. logForDebugging(
  4973. `handleOrphanedPermissionResponse: received orphaned control_response for toolUseID=${toolUseID} request_id=${responseInner.request_id}`,
  4974. )
  4975. // Prevent re-processing the same orphaned tool_use. Without this guard,
  4976. // duplicate control_response deliveries (e.g. from WebSocket reconnect)
  4977. // cause the same tool to be executed multiple times, producing duplicate
  4978. // tool_use IDs in the messages array and a 400 error from the API.
  4979. // Once corrupted, every retry accumulates more duplicates.
  4980. if (handledToolUseIds.has(toolUseID)) {
  4981. logForDebugging(
  4982. `handleOrphanedPermissionResponse: skipping duplicate orphaned permission for toolUseID=${toolUseID} (already handled)`,
  4983. )
  4984. return false
  4985. }
  4986. const assistantMessage = await findUnresolvedToolUse(toolUseID)
  4987. if (!assistantMessage) {
  4988. logForDebugging(
  4989. `handleOrphanedPermissionResponse: no unresolved tool_use found for toolUseID=${toolUseID} (already resolved in transcript)`,
  4990. )
  4991. return false
  4992. }
  4993. handledToolUseIds.add(toolUseID)
  4994. logForDebugging(
  4995. `handleOrphanedPermissionResponse: enqueuing orphaned permission for toolUseID=${toolUseID} messageID=${assistantMessage.message.id}`,
  4996. )
  4997. enqueue({
  4998. mode: 'orphaned-permission' as const,
  4999. value: [],
  5000. orphanedPermission: {
  5001. permissionResult,
  5002. assistantMessage,
  5003. },
  5004. })
  5005. onEnqueued?.()
  5006. return true
  5007. }
  5008. return false
  5009. }
  5010. export type DynamicMcpState = {
  5011. clients: MCPServerConnection[]
  5012. tools: Tools
  5013. configs: Record<string, ScopedMcpServerConfig>
  5014. }
  5015. /**
  5016. * Converts a process transport config to a scoped config.
  5017. * The types are structurally compatible, so we just add the scope.
  5018. */
  5019. function toScopedConfig(
  5020. config: McpServerConfigForProcessTransport,
  5021. ): ScopedMcpServerConfig {
  5022. // McpServerConfigForProcessTransport is a subset of McpServerConfig
  5023. // (it excludes IDE-specific types like sse-ide and ws-ide)
  5024. // Adding scope makes it a valid ScopedMcpServerConfig
  5025. return { ...config, scope: 'dynamic' } as ScopedMcpServerConfig
  5026. }
  5027. /**
  5028. * State for SDK MCP servers that run in the SDK process.
  5029. */
  5030. export type SdkMcpState = {
  5031. configs: Record<string, McpSdkServerConfig>
  5032. clients: MCPServerConnection[]
  5033. tools: Tools
  5034. }
  5035. /**
  5036. * Result of handleMcpSetServers - contains new state and response data.
  5037. */
  5038. export type McpSetServersResult = {
  5039. response: SDKControlMcpSetServersResponse
  5040. newSdkState: SdkMcpState
  5041. newDynamicState: DynamicMcpState
  5042. sdkServersChanged: boolean
  5043. }
  5044. /**
  5045. * Handles mcp_set_servers requests by processing both SDK and process-based servers.
  5046. * SDK servers run in the SDK process; process-based servers are spawned by the CLI.
  5047. *
  5048. * Applies enterprise allowedMcpServers/deniedMcpServers policy — same filter as
  5049. * --mcp-config (see filterMcpServersByPolicy call in main.tsx). Without this,
  5050. * SDK V2 Query.setMcpServers() was a second policy bypass vector. Blocked servers
  5051. * are reported in response.errors so the SDK consumer knows why they weren't added.
  5052. */
  5053. export async function handleMcpSetServers(
  5054. servers: Record<string, McpServerConfigForProcessTransport>,
  5055. sdkState: SdkMcpState,
  5056. dynamicState: DynamicMcpState,
  5057. setAppState: (f: (prev: AppState) => AppState) => void,
  5058. ): Promise<McpSetServersResult> {
  5059. // Enforce enterprise MCP policy on process-based servers (stdio/http/sse).
  5060. // Mirrors the --mcp-config filter in main.tsx — both user-controlled injection
  5061. // paths must have the same gate. type:'sdk' servers are exempt (SDK-managed,
  5062. // CLI never spawns/connects for them — see filterMcpServersByPolicy jsdoc).
  5063. // Blocked servers go into response.errors so the SDK caller sees why.
  5064. const { allowed: allowedServers, blocked } = filterMcpServersByPolicy(servers)
  5065. const policyErrors: Record<string, string> = {}
  5066. for (const name of blocked) {
  5067. policyErrors[name] =
  5068. 'Blocked by enterprise policy (allowedMcpServers/deniedMcpServers)'
  5069. }
  5070. // Separate SDK servers from process-based servers
  5071. const sdkServers: Record<string, McpSdkServerConfig> = {}
  5072. const processServers: Record<string, McpServerConfigForProcessTransport> = {}
  5073. for (const [name, config] of Object.entries(allowedServers)) {
  5074. if ((config.type as string) === 'sdk') {
  5075. sdkServers[name] = config as unknown as McpSdkServerConfig
  5076. } else {
  5077. processServers[name] = config
  5078. }
  5079. }
  5080. // Handle SDK servers
  5081. const currentSdkNames = new Set(Object.keys(sdkState.configs))
  5082. const newSdkNames = new Set(Object.keys(sdkServers))
  5083. const sdkAdded: string[] = []
  5084. const sdkRemoved: string[] = []
  5085. const newSdkConfigs = { ...sdkState.configs }
  5086. let newSdkClients = [...sdkState.clients]
  5087. let newSdkTools = [...sdkState.tools]
  5088. // Remove SDK servers no longer in desired state
  5089. for (const name of currentSdkNames) {
  5090. if (!newSdkNames.has(name)) {
  5091. const client = newSdkClients.find(c => c.name === name)
  5092. if (client && client.type === 'connected') {
  5093. await client.cleanup()
  5094. }
  5095. newSdkClients = newSdkClients.filter(c => c.name !== name)
  5096. const prefix = `mcp__${name}__`
  5097. newSdkTools = newSdkTools.filter(t => !t.name.startsWith(prefix))
  5098. delete newSdkConfigs[name]
  5099. sdkRemoved.push(name)
  5100. }
  5101. }
  5102. // Add new SDK servers as pending - they'll be upgraded to connected
  5103. // when updateSdkMcp() runs on the next query
  5104. for (const [name, config] of Object.entries(sdkServers)) {
  5105. if (!currentSdkNames.has(name)) {
  5106. newSdkConfigs[name] = config
  5107. const pendingClient: MCPServerConnection = {
  5108. type: 'pending',
  5109. name,
  5110. config: { ...config, scope: 'dynamic' as const },
  5111. }
  5112. newSdkClients = [...newSdkClients, pendingClient]
  5113. sdkAdded.push(name)
  5114. }
  5115. }
  5116. // Handle process-based servers
  5117. const processResult = await reconcileMcpServers(
  5118. processServers,
  5119. dynamicState,
  5120. setAppState,
  5121. )
  5122. return {
  5123. response: {
  5124. added: [...sdkAdded, ...processResult.response.added],
  5125. removed: [...sdkRemoved, ...processResult.response.removed],
  5126. errors: { ...policyErrors, ...processResult.response.errors },
  5127. },
  5128. newSdkState: {
  5129. configs: newSdkConfigs,
  5130. clients: newSdkClients,
  5131. tools: newSdkTools,
  5132. },
  5133. newDynamicState: processResult.newState,
  5134. sdkServersChanged: sdkAdded.length > 0 || sdkRemoved.length > 0,
  5135. }
  5136. }
  5137. /**
  5138. * Reconciles the current set of dynamic MCP servers with a new desired state.
  5139. * Handles additions, removals, and config changes.
  5140. */
  5141. export async function reconcileMcpServers(
  5142. desiredConfigs: Record<string, McpServerConfigForProcessTransport>,
  5143. currentState: DynamicMcpState,
  5144. setAppState: (f: (prev: AppState) => AppState) => void,
  5145. ): Promise<{
  5146. response: SDKControlMcpSetServersResponse
  5147. newState: DynamicMcpState
  5148. }> {
  5149. const currentNames = new Set(Object.keys(currentState.configs))
  5150. const desiredNames = new Set(Object.keys(desiredConfigs))
  5151. const toRemove = [...currentNames].filter(n => !desiredNames.has(n))
  5152. const toAdd = [...desiredNames].filter(n => !currentNames.has(n))
  5153. // Check for config changes (same name, different config)
  5154. const toCheck = [...currentNames].filter(n => desiredNames.has(n))
  5155. const toReplace = toCheck.filter(name => {
  5156. const currentConfig = currentState.configs[name]
  5157. const desiredConfigRaw = desiredConfigs[name]
  5158. if (!currentConfig || !desiredConfigRaw) return true
  5159. const desiredConfig = toScopedConfig(desiredConfigRaw)
  5160. return !areMcpConfigsEqual(currentConfig, desiredConfig)
  5161. })
  5162. const removed: string[] = []
  5163. const added: string[] = []
  5164. const errors: Record<string, string> = {}
  5165. let newClients = [...currentState.clients]
  5166. let newTools = [...currentState.tools]
  5167. // Remove old servers (including ones being replaced)
  5168. for (const name of [...toRemove, ...toReplace]) {
  5169. const client = newClients.find(c => c.name === name)
  5170. const config = currentState.configs[name]
  5171. if (client && config) {
  5172. if (client.type === 'connected') {
  5173. try {
  5174. await client.cleanup()
  5175. } catch (e) {
  5176. logError(e)
  5177. }
  5178. }
  5179. // Clear the memoization cache
  5180. await clearServerCache(name, config)
  5181. }
  5182. // Remove tools from this server
  5183. const prefix = `mcp__${name}__`
  5184. newTools = newTools.filter(t => !t.name.startsWith(prefix))
  5185. // Remove from clients list
  5186. newClients = newClients.filter(c => c.name !== name)
  5187. // Track removal (only for actually removed, not replaced)
  5188. if (toRemove.includes(name)) {
  5189. removed.push(name)
  5190. }
  5191. }
  5192. // Add new servers (including replacements)
  5193. for (const name of [...toAdd, ...toReplace]) {
  5194. const config = desiredConfigs[name]
  5195. if (!config) continue
  5196. const scopedConfig = toScopedConfig(config)
  5197. // SDK servers are managed by the SDK process, not the CLI.
  5198. // Just track them without trying to connect.
  5199. if ((config.type as string) === 'sdk') {
  5200. added.push(name)
  5201. continue
  5202. }
  5203. try {
  5204. const client = await connectToServer(name, scopedConfig)
  5205. newClients.push(client)
  5206. if (client.type === 'connected') {
  5207. const serverTools = await fetchToolsForClient(client)
  5208. newTools.push(...serverTools)
  5209. } else if (client.type === 'failed') {
  5210. errors[name] = client.error || 'Connection failed'
  5211. }
  5212. added.push(name)
  5213. } catch (e) {
  5214. const err = toError(e)
  5215. errors[name] = err.message
  5216. logError(err)
  5217. }
  5218. }
  5219. // Build new configs
  5220. const newConfigs: Record<string, ScopedMcpServerConfig> = {}
  5221. for (const name of desiredNames) {
  5222. const config = desiredConfigs[name]
  5223. if (config) {
  5224. newConfigs[name] = toScopedConfig(config)
  5225. }
  5226. }
  5227. const newState: DynamicMcpState = {
  5228. clients: newClients,
  5229. tools: newTools,
  5230. configs: newConfigs,
  5231. }
  5232. // Update AppState with the new tools
  5233. setAppState(prev => {
  5234. // Get all dynamic server names (current + new)
  5235. const allDynamicServerNames = new Set([
  5236. ...Object.keys(currentState.configs),
  5237. ...Object.keys(newConfigs),
  5238. ])
  5239. // Remove old dynamic tools
  5240. const nonDynamicTools = prev.mcp.tools.filter(t => {
  5241. for (const serverName of allDynamicServerNames) {
  5242. if (t.name.startsWith(`mcp__${serverName}__`)) {
  5243. return false
  5244. }
  5245. }
  5246. return true
  5247. })
  5248. // Remove old dynamic clients
  5249. const nonDynamicClients = prev.mcp.clients.filter(c => {
  5250. return !allDynamicServerNames.has(c.name)
  5251. })
  5252. return {
  5253. ...prev,
  5254. mcp: {
  5255. ...prev.mcp,
  5256. tools: [...nonDynamicTools, ...newTools],
  5257. clients: [...nonDynamicClients, ...newClients],
  5258. },
  5259. }
  5260. })
  5261. return {
  5262. response: { added, removed, errors },
  5263. newState,
  5264. }
  5265. }