@buoy-gg/agent-core 7.0.40 → 7.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/lib/commonjs/blocks/receipts.js +1 -761
  2. package/lib/commonjs/blocks/runId.js +1 -31
  3. package/lib/commonjs/blocks/types.js +1 -140
  4. package/lib/commonjs/blocks/uiTool.js +6 -1405
  5. package/lib/commonjs/catalog/catalog.g.js +5 -4207
  6. package/lib/commonjs/catalog/catalog.source.json +656 -12
  7. package/lib/commonjs/catalog/catalog.types.g.js +1 -44
  8. package/lib/commonjs/catalog/normalizeParams.js +2 -332
  9. package/lib/commonjs/catalog/signature.js +1 -68
  10. package/lib/commonjs/catalog/snapshotReads.js +1 -263
  11. package/lib/commonjs/catalog/toProviderTools.js +2 -145
  12. package/lib/commonjs/catalog/validateParams.js +1 -129
  13. package/lib/commonjs/context/buildContextPack.js +1 -489
  14. package/lib/commonjs/effects/digest.js +1 -34
  15. package/lib/commonjs/effects/ledger.js +1 -325
  16. package/lib/commonjs/engine/askGate.js +1 -287
  17. package/lib/commonjs/engine/effectFor.js +1 -176
  18. package/lib/commonjs/engine/evidence.js +3 -111
  19. package/lib/commonjs/engine/historyBudget.js +1 -363
  20. package/lib/commonjs/engine/retrieve.js +1 -214
  21. package/lib/commonjs/engine/runAgentTurn.js +8 -1803
  22. package/lib/commonjs/engine/systemPrompt.js +54 -163
  23. package/lib/commonjs/engine/textToolCalls.js +1 -276
  24. package/lib/commonjs/engine/tokenCalibration.js +1 -81
  25. package/lib/commonjs/engine/verify.js +4 -297
  26. package/lib/commonjs/index.js +1 -384
  27. package/lib/commonjs/policy/labels.js +1 -319
  28. package/lib/commonjs/policy/policy.js +1 -148
  29. package/lib/commonjs/policy/redact.js +1 -172
  30. package/lib/commonjs/providers/anthropic.js +3 -445
  31. package/lib/commonjs/providers/openai.js +4 -324
  32. package/lib/commonjs/providers/problem.js +1 -98
  33. package/lib/commonjs/providers/sse.js +1 -240
  34. package/lib/commonjs/providers/streamTimer.js +1 -123
  35. package/lib/commonjs/providers/transport.js +1 -123
  36. package/lib/commonjs/providers/types.js +1 -6
  37. package/lib/commonjs/providers/xhrStream.js +1 -212
  38. package/lib/commonjs/realNow.js +1 -0
  39. package/lib/commonjs/session.js +1 -393
  40. package/lib/commonjs/types.js +0 -1
  41. package/lib/module/blocks/receipts.js +1 -755
  42. package/lib/module/blocks/runId.js +1 -26
  43. package/lib/module/blocks/types.js +1 -134
  44. package/lib/module/blocks/uiTool.js +6 -1396
  45. package/lib/module/catalog/catalog.g.js +5 -4203
  46. package/lib/module/catalog/catalog.source.json +656 -12
  47. package/lib/module/catalog/catalog.types.g.js +1 -40
  48. package/lib/module/catalog/normalizeParams.js +2 -326
  49. package/lib/module/catalog/signature.js +1 -63
  50. package/lib/module/catalog/snapshotReads.js +1 -259
  51. package/lib/module/catalog/toProviderTools.js +2 -139
  52. package/lib/module/catalog/validateParams.js +1 -124
  53. package/lib/module/context/buildContextPack.js +1 -485
  54. package/lib/module/effects/digest.js +1 -29
  55. package/lib/module/effects/ledger.js +1 -319
  56. package/lib/module/engine/askGate.js +1 -280
  57. package/lib/module/engine/effectFor.js +1 -173
  58. package/lib/module/engine/evidence.js +3 -105
  59. package/lib/module/engine/historyBudget.js +1 -355
  60. package/lib/module/engine/retrieve.js +1 -210
  61. package/lib/module/engine/runAgentTurn.js +8 -1794
  62. package/lib/module/engine/systemPrompt.js +54 -157
  63. package/lib/module/engine/textToolCalls.js +1 -270
  64. package/lib/module/engine/tokenCalibration.js +1 -75
  65. package/lib/module/engine/verify.js +4 -289
  66. package/lib/module/index.js +1 -35
  67. package/lib/module/policy/labels.js +1 -312
  68. package/lib/module/policy/policy.js +1 -142
  69. package/lib/module/policy/redact.js +1 -165
  70. package/lib/module/providers/anthropic.js +3 -441
  71. package/lib/module/providers/openai.js +4 -320
  72. package/lib/module/providers/problem.js +1 -92
  73. package/lib/module/providers/sse.js +1 -231
  74. package/lib/module/providers/streamTimer.js +1 -118
  75. package/lib/module/providers/transport.js +1 -119
  76. package/lib/module/providers/types.js +1 -4
  77. package/lib/module/providers/xhrStream.js +1 -207
  78. package/lib/module/realNow.js +1 -0
  79. package/lib/module/session.js +1 -369
  80. package/lib/module/types.js +0 -1
  81. package/lib/typescript/catalog/catalog.g.d.ts +3 -3
  82. package/lib/typescript/catalog/catalog.types.g.d.ts +7 -4
  83. package/lib/typescript/effects/ledger.d.ts +16 -1
  84. package/lib/typescript/providers/problem.d.ts +0 -20
  85. package/lib/typescript/providers/streamTimer.d.ts +0 -24
  86. package/lib/typescript/realNow.d.ts +18 -0
  87. package/lib/web/index.mjs +152 -0
  88. package/package.json +24 -3
  89. package/lib/commonjs/blocks/receipts.js.map +0 -1
  90. package/lib/commonjs/blocks/runId.js.map +0 -1
  91. package/lib/commonjs/blocks/types.js.map +0 -1
  92. package/lib/commonjs/blocks/uiTool.js.map +0 -1
  93. package/lib/commonjs/catalog/catalog.g.js.map +0 -1
  94. package/lib/commonjs/catalog/catalog.types.g.js.map +0 -1
  95. package/lib/commonjs/catalog/normalizeParams.js.map +0 -1
  96. package/lib/commonjs/catalog/signature.js.map +0 -1
  97. package/lib/commonjs/catalog/snapshotReads.js.map +0 -1
  98. package/lib/commonjs/catalog/toProviderTools.js.map +0 -1
  99. package/lib/commonjs/catalog/validateParams.js.map +0 -1
  100. package/lib/commonjs/context/buildContextPack.js.map +0 -1
  101. package/lib/commonjs/effects/digest.js.map +0 -1
  102. package/lib/commonjs/effects/ledger.js.map +0 -1
  103. package/lib/commonjs/engine/askGate.js.map +0 -1
  104. package/lib/commonjs/engine/effectFor.js.map +0 -1
  105. package/lib/commonjs/engine/evidence.js.map +0 -1
  106. package/lib/commonjs/engine/historyBudget.js.map +0 -1
  107. package/lib/commonjs/engine/retrieve.js.map +0 -1
  108. package/lib/commonjs/engine/runAgentTurn.js.map +0 -1
  109. package/lib/commonjs/engine/systemPrompt.js.map +0 -1
  110. package/lib/commonjs/engine/textToolCalls.js.map +0 -1
  111. package/lib/commonjs/engine/tokenCalibration.js.map +0 -1
  112. package/lib/commonjs/engine/verify.js.map +0 -1
  113. package/lib/commonjs/index.js.map +0 -1
  114. package/lib/commonjs/policy/labels.js.map +0 -1
  115. package/lib/commonjs/policy/policy.js.map +0 -1
  116. package/lib/commonjs/policy/redact.js.map +0 -1
  117. package/lib/commonjs/providers/anthropic.js.map +0 -1
  118. package/lib/commonjs/providers/openai.js.map +0 -1
  119. package/lib/commonjs/providers/problem.js.map +0 -1
  120. package/lib/commonjs/providers/sse.js.map +0 -1
  121. package/lib/commonjs/providers/streamTimer.js.map +0 -1
  122. package/lib/commonjs/providers/transport.js.map +0 -1
  123. package/lib/commonjs/providers/types.js.map +0 -1
  124. package/lib/commonjs/providers/xhrStream.js.map +0 -1
  125. package/lib/commonjs/session.js.map +0 -1
  126. package/lib/commonjs/types.js.map +0 -1
  127. package/lib/module/blocks/receipts.js.map +0 -1
  128. package/lib/module/blocks/runId.js.map +0 -1
  129. package/lib/module/blocks/types.js.map +0 -1
  130. package/lib/module/blocks/uiTool.js.map +0 -1
  131. package/lib/module/catalog/catalog.g.js.map +0 -1
  132. package/lib/module/catalog/catalog.types.g.js.map +0 -1
  133. package/lib/module/catalog/normalizeParams.js.map +0 -1
  134. package/lib/module/catalog/signature.js.map +0 -1
  135. package/lib/module/catalog/snapshotReads.js.map +0 -1
  136. package/lib/module/catalog/toProviderTools.js.map +0 -1
  137. package/lib/module/catalog/validateParams.js.map +0 -1
  138. package/lib/module/context/buildContextPack.js.map +0 -1
  139. package/lib/module/effects/digest.js.map +0 -1
  140. package/lib/module/effects/ledger.js.map +0 -1
  141. package/lib/module/engine/askGate.js.map +0 -1
  142. package/lib/module/engine/effectFor.js.map +0 -1
  143. package/lib/module/engine/evidence.js.map +0 -1
  144. package/lib/module/engine/historyBudget.js.map +0 -1
  145. package/lib/module/engine/retrieve.js.map +0 -1
  146. package/lib/module/engine/runAgentTurn.js.map +0 -1
  147. package/lib/module/engine/systemPrompt.js.map +0 -1
  148. package/lib/module/engine/textToolCalls.js.map +0 -1
  149. package/lib/module/engine/tokenCalibration.js.map +0 -1
  150. package/lib/module/engine/verify.js.map +0 -1
  151. package/lib/module/index.js.map +0 -1
  152. package/lib/module/policy/labels.js.map +0 -1
  153. package/lib/module/policy/policy.js.map +0 -1
  154. package/lib/module/policy/redact.js.map +0 -1
  155. package/lib/module/providers/anthropic.js.map +0 -1
  156. package/lib/module/providers/openai.js.map +0 -1
  157. package/lib/module/providers/problem.js.map +0 -1
  158. package/lib/module/providers/sse.js.map +0 -1
  159. package/lib/module/providers/streamTimer.js.map +0 -1
  160. package/lib/module/providers/transport.js.map +0 -1
  161. package/lib/module/providers/types.js.map +0 -1
  162. package/lib/module/providers/xhrStream.js.map +0 -1
  163. package/lib/module/session.js.map +0 -1
  164. package/lib/module/types.js.map +0 -1
  165. package/lib/typescript/blocks/receipts.d.ts.map +0 -1
  166. package/lib/typescript/blocks/runId.d.ts.map +0 -1
  167. package/lib/typescript/blocks/types.d.ts.map +0 -1
  168. package/lib/typescript/blocks/uiTool.d.ts.map +0 -1
  169. package/lib/typescript/catalog/catalog.g.d.ts.map +0 -1
  170. package/lib/typescript/catalog/catalog.types.g.d.ts.map +0 -1
  171. package/lib/typescript/catalog/normalizeParams.d.ts.map +0 -1
  172. package/lib/typescript/catalog/signature.d.ts.map +0 -1
  173. package/lib/typescript/catalog/snapshotReads.d.ts.map +0 -1
  174. package/lib/typescript/catalog/toProviderTools.d.ts.map +0 -1
  175. package/lib/typescript/catalog/validateParams.d.ts.map +0 -1
  176. package/lib/typescript/context/buildContextPack.d.ts.map +0 -1
  177. package/lib/typescript/effects/digest.d.ts.map +0 -1
  178. package/lib/typescript/effects/ledger.d.ts.map +0 -1
  179. package/lib/typescript/engine/askGate.d.ts.map +0 -1
  180. package/lib/typescript/engine/effectFor.d.ts.map +0 -1
  181. package/lib/typescript/engine/evidence.d.ts.map +0 -1
  182. package/lib/typescript/engine/historyBudget.d.ts.map +0 -1
  183. package/lib/typescript/engine/retrieve.d.ts.map +0 -1
  184. package/lib/typescript/engine/runAgentTurn.d.ts.map +0 -1
  185. package/lib/typescript/engine/systemPrompt.d.ts.map +0 -1
  186. package/lib/typescript/engine/textToolCalls.d.ts.map +0 -1
  187. package/lib/typescript/engine/tokenCalibration.d.ts.map +0 -1
  188. package/lib/typescript/engine/verify.d.ts.map +0 -1
  189. package/lib/typescript/index.d.ts.map +0 -1
  190. package/lib/typescript/policy/labels.d.ts.map +0 -1
  191. package/lib/typescript/policy/policy.d.ts.map +0 -1
  192. package/lib/typescript/policy/redact.d.ts.map +0 -1
  193. package/lib/typescript/providers/anthropic.d.ts.map +0 -1
  194. package/lib/typescript/providers/openai.d.ts.map +0 -1
  195. package/lib/typescript/providers/problem.d.ts.map +0 -1
  196. package/lib/typescript/providers/sse.d.ts.map +0 -1
  197. package/lib/typescript/providers/streamTimer.d.ts.map +0 -1
  198. package/lib/typescript/providers/transport.d.ts.map +0 -1
  199. package/lib/typescript/providers/types.d.ts.map +0 -1
  200. package/lib/typescript/providers/xhrStream.d.ts.map +0 -1
  201. package/lib/typescript/session.d.ts.map +0 -1
  202. package/lib/typescript/types.d.ts.map +0 -1
@@ -1,1807 +1,12 @@
1
- "use strict";
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"digestOf",{enumerable:true,get:function(){return _digest.digestOf}});exports.readTargetDigest=readTargetDigest;exports.runAgentTurn=runAgentTurn;exports.runGatedAction=runGatedAction;var _catalog=require("../catalog/catalog.g");var _validateParams=require("../catalog/validateParams");var _normalizeParams=require("../catalog/normalizeParams");var _toProviderTools=require("../catalog/toProviderTools");var _snapshotReads=require("../catalog/snapshotReads");var _ledger=require("../effects/ledger");var _policy=require("../policy/policy");var _redact=require("../policy/redact");var _effectFor=require("./effectFor");var _digest=require("../effects/digest");var _uiTool=require("../blocks/uiTool");var _receipts=require("../blocks/receipts");var _types=require("../blocks/types");var _askGate=require("./askGate");var _textToolCalls=require("./textToolCalls");var _historyBudget=require("./historyBudget");var _tokenCalibration=require("./tokenCalibration");var _evidence=require("./evidence");var _retrieve=require("./retrieve");var _verify=require("./verify");var _realNow=require("../realNow");const MAX_RESULT_CHARS=24e3;const MAX_TRACE_RESULT_CHARS=2e3;const LOOP_REPEATS=3;function callSignature(t){const r=n=>{if(n===null||typeof n!=="object")return n;if(Array.isArray(n))return n.map(r);const l=n;return Object.keys(l).sort().reduce((c,b)=>(c[b]=r(l[b]),c),{})};return t.map(n=>`${n.name}:${JSON.stringify(r(n.input??{}))}`).join("|")}const LOOP_WARNING="[Buoy] You have now made this exact call three times in a row and gotten the same thing back. Repeating it again will not change the answer. Read the result you already have, then either try a different tool or different arguments, or tell the user plainly what you could not get and what you tried.";const DEFAULT_MAX_STEPS=12;const DEFAULT_TURN_MS=18e4;const THROTTLE_RETRIES=2;const OVERFLOW_RETRIES=1;const RETRY_BASE_MS=1e3;const RETRY_JITTER_MS=250;const RETRY_AFTER_CAP_MS=3e4;function sleep(t,r){return new Promise(n=>{if(r?.aborted)return n();const l=(0,_realNow.realSetTimeout)(c,t);function c(){(0,_realNow.realClearTimeout)(l);r?.removeEventListener("abort",c);n()}r?.addEventListener("abort",c,{once:true})})}function readAnswer(t){if(typeof t==="boolean")return{approved:t,trust:false};const r=t.reason?.trim();return{approved:t.approved,...r?{reason:r}:{},trust:t.trust===true&&t.approved}}function declinedResult(t){return t?`The user declined this change and said: "${t}". Do not retry it as proposed; take their note into account and, if a different change would fit, propose that instead.`:"The user declined this change. Do not retry it; ask what they would prefer."}function findDescriptor(t,r,n){return t.find(l=>l.toolId===r)?.actions.find(l=>l.action===n)}const URL_PROVENANCE_REJECTION="Rejected: every image URL must be one you read from a tool result in THIS conversation (images.list, a storage value, a response body). Never invent or remember URLs \u2014 read them first, then show them.";const droppedImageNote=t=>` NOTE: ${t} image ${t===1?"URL was":"URLs were"} dropped \u2014 they were not read from a tool result in this conversation, so the card the user is looking at has no picture there. Do not tell them it does; read the URLs and show it again, or say the artwork is missing.`;function fullText(t){try{return JSON.stringify(t??null)}catch{return String(t)}}function encodeResult(t,r){if(t.length<=MAX_RESULT_CHARS)return t;return`${t.slice(0,MAX_RESULT_CHARS)}${(0,_evidence.truncationMarker)(t.length,r)}`}function summarise(t){if(t===null||t===void 0)return"Done";if(Array.isArray(t))return countOf(t.length);if(typeof t==="boolean")return t?"Done":"Failed";if(typeof t==="object"){const r=t;if(r.ok===false)return typeof r.error==="string"?r.error:"Failed";if(r.found===false)return"Not found";if(r.tapped===false)return"Nothing to tap";if(r.active===false)return"Saved, but inactive in this build";for(const l of["returned","count","total"]){const c=r[l];if(typeof c==="number")return countOf(c)}const n=Object.values(r).filter(Array.isArray);if(n.length===1)return countOf(n[0].length);return"Done"}return String(t).slice(0,80)}function countOf(t){return t===1?"1 item":`${t} items`}function reportsFailure(t){return typeof t==="object"&&t!==null&&t.ok===false}async function*runAgentTurn(t){const{provider:r,catalog:n,system:l,systemVolatile:c,model:b,maxTokens:m,dispatch:u,readSnapshot:y,storeKeyOwners:H,ledger:v,policy:q,isRelease:P,requestApproval:L,signal:E,evidence:U,trusted:w,procedures:D}=t;const j=new Map;const $=t.calibration??new _tokenCalibration.TokenCalibration;const p=[...t.messages];const te=(0,_toProviderTools.toProviderTools)(n,{availableToolIds:t.availableToolIds,availableActions:t.availableActions,isRelease:P});te.push((0,_uiTool.uiProviderTool)());const ue=q.maxSteps??DEFAULT_MAX_STEPS;const Oe=v.generation;const fe=l.length+(c?.length??0)+te.reduce((Q,C)=>Q+C.description.length+JSON.stringify(C.inputSchema).length,0);const pe=()=>fe+$.chars(m);let K=(0,_realNow.realNow)()+DEFAULT_TURN_MS;const oe={throttled:0,overflow:0};let V=_historyBudget.MAX_HISTORY_TOKENS;const re=t.seenUrls??new Set;const qe=/https?:\/\/[^\s"'\\)\]}>]+/g;let he=false;const B=[];let ye=false;const me=[];let ge=false;let be=false;for(let Q=0;Q<ue;Q++){if(E?.aborted){yield{type:"done",stopReason:"stopped"};return p}if((0,_realNow.realNow)()>K){yield{type:"block",block:{id:`cap${(0,_realNow.realNow)()}`,kind:"notice",tone:"warning",text:`This is taking too long \u2014 stopped after ${Math.round(DEFAULT_TURN_MS/1e3)}s. What it found so far is above.`,actions:[{label:"Continue",primary:true,send:"Continue where you left off."}]}};yield{type:"done",stopReason:"time-cap"};return p}const C=(0,_historyBudget.budgetForRequest)(p,pe(),$.chars(V),$.charsPerToken,v.liveCallIds());if(C.droppedRounds>0){p.length=0;p.push(...C.messages);yield{type:"history-trimmed",droppedRounds:C.droppedRounds,droppedPinned:C.droppedPinned}}else if(C.messages!==p){p.length=0;p.push(...C.messages)}let _="";let ke=false;const z=e=>{const o=he&&!ke;ke=true;he=true;return o?{type:"text",delta:e,breakBefore:true}:{type:"text",delta:e}};let k=[];const ve=new Set;let N="";let ne=true;let G=[];let X=false;let Z;let F;for(;;){_="";k=[];N="";ne=true;G=[];X=false;Z=void 0;F=void 0;let e;const o=fe+p.reduce((s,I)=>s+(0,_historyBudget.messageSize)(I),0);for await(const s of r.send({messages:p,system:l,systemVolatile:c,tools:te,model:b,maxTokens:m,signal:E})){if(s.type==="text"){_+=s.delta;if(!ne){yield z(s.delta)}else if((0,_textToolCalls.couldBeToolCallEnvelope)(_)){N+=s.delta}else{ne=false;N="";yield z(_)}}else if(s.type==="tool-call"){k.push(s.call)}else if(s.type==="thinking"){G.push(s.block);if(s.block.type==="thinking"&&s.block.thinking.trim()){yield{type:"reasoning",text:s.block.thinking}}}else if(s.type==="error"){if(s.kind==="truncated"||s.kind==="timeout"){F={message:s.message}}else if(s.problem&&(s.problem.kind==="throttled"||s.problem.kind==="overflow")&&_===""&&k.length===0&&G.length===0){e={problem:s.problem,message:s.message}}else{yield{type:"error",message:s.message};X=true}}else if(s.type==="done"){Z=s.outcome;if(s.usage){$.observe(o,(0,_tokenCalibration.promptTokensOf)(s.usage,r.protocol));yield{type:"usage",...s.usage,model:s.model}}}}if(!e)break;const i=e.problem.kind;const f=1+(i==="throttled"?THROTTLE_RETRIES:OVERFLOW_RETRIES);const d=oe[i];let A=i==="throttled"?Math.min(e.problem.retryAfterMs??RETRY_BASE_MS*2**d,RETRY_AFTER_CAP_MS)+Math.floor(Math.random()*RETRY_JITTER_MS):0;let S=d>=f-1||E?.aborted===true||(0,_realNow.realNow)()+A>K;if(!S&&i==="overflow"){V=Math.floor(V/2);const s=p.reduce((M,O)=>M+(0,_historyBudget.messageSize)(O),0);const I=(0,_historyBudget.budgetForRequest)(p,pe(),$.chars(V),$.charsPerToken,v.liveCallIds());const W=I.messages.reduce((M,O)=>M+(0,_historyBudget.messageSize)(O),0);if(W>=s){S=true}else{p.length=0;p.push(...I.messages);if(I.droppedRounds>0)yield{type:"history-trimmed",droppedRounds:I.droppedRounds,droppedPinned:I.droppedPinned}}}if(S){const s=i==="throttled"?d>0?`The AI endpoint is rate-limiting requests \u2014 tried ${d+1} times.`:"The AI endpoint is rate-limiting requests.":"This conversation is too large for the AI endpoint and there was nothing older to trim. Start a new conversation, or ask a shorter question.";yield{type:"error",message:`${s} (${e.message})`};X=true;break}oe[i]+=1;yield{type:"retrying",reason:i,attempt:oe[i]+1,maxAttempts:f,inMs:A};if(A>0)await sleep(A,E);if(E?.aborted){yield{type:"done",stopReason:"stopped"};return p}}if(X){if(N)yield z(N);yield{type:"done",stopReason:"error"};return p}if(!F&&Z!=="completed"){F={message:Z==="output-limited"?k.length?"The model ran out of room mid-plan, so nothing was run. What it found so far is above.":"The model ran out of room before finishing this answer.":"The answer ended before the endpoint said it was finished, so nothing was run."}}if(F){if(N)yield z(N);if(_.trim())p.push({role:"assistant",text:_});yield{type:"block",block:{id:`cut${(0,_realNow.realNow)()}`,kind:"notice",tone:"warning",text:F.message,actions:[{label:"Continue",primary:true,send:"Continue where you left off."}]}};yield{type:"done",stopReason:"incomplete"};return p}if(N){const e=k.length===0?(0,_textToolCalls.parseTextToolCalls)(N,n):void 0;if(e){for(const o of e.calls){k.push(o);ve.add(o.id)}_=e.reply;if(e.reply)yield z(e.reply)}else{yield z(N)}N=""}p.push({role:"assistant",text:_,toolCalls:k.length?k:void 0,thinking:G.length?G:void 0});if(_.trim())me.push(_.trim());if(k.length===0){const e=me.join("\n\n");const o=(0,_askGate.synthesizeShortfallBlock)({text:e,attempted:be});if(o)yield{type:"block",block:o};const i=ge||o?null:(0,_askGate.synthesizeAskBlock)(e);if(i){yield{type:"block",block:i};yield{type:"awaiting-user",blockId:i.id}}yield{type:"done",stopReason:i?"awaiting-user":"answered"};return p}const g=[];let we=false;const Te=new Map;if(k.length>1){const e=k.every(o=>{if(o.name===_uiTool.UI_TOOL_NAME)return false;const i=(0,_toProviderTools.fromProviderToolName)(o.name,n);if(!i||i==="ask-buoy")return false;const f=o.input?.action;if(typeof f!=="string"||f===_snapshotReads.SNAPSHOT_ACTION)return false;const d=findDescriptor(n,i,f);if(!d||d.effect!=="read")return false;return(0,_policy.decide)({descriptor:d,toolId:i,policy:q,isRelease:P}).verdict==="allow"});if(e){for(const o of k){const i=(0,_toProviderTools.fromProviderToolName)(o.name,n);const f=String(o.input.action);const d={...o.input.params??{}};const A=findDescriptor(n,i,f);const S=(0,_normalizeParams.normalizeParams)(i,f,d);const s=(0,_validateParams.applyDefaults)(S.params,A.params);if(!(0,_validateParams.validateParams)(s,A.params).ok)continue;const I=Promise.resolve(u(i,f,s)).catch(W=>{throw W});I.catch(()=>{});Te.set(o.id,I)}}}let ie=null;const Pe=e=>{const o=(0,_toProviderTools.fromProviderToolName)(e.name,n)??e.name;const i=typeof e.input?.action==="string"?e.input.action:"";const f=i?findDescriptor(n,o,i):void 0;const d=e.input?.params??{};return[{type:"tool-start",id:e.id,toolId:o,action:i,label:f?(0,_policy.describeCall)(o,f,d):`${o}.${i}`,description:f?.summary??"",params:d,effect:f?.effect??"read"},{type:"tool-end",id:e.id,ok:false,summary:"stopped",durationMs:0}]};const Re=k.map(e=>{if(e.name===_uiTool.UI_TOOL_NAME)return void 0;const o=(0,_toProviderTools.fromProviderToolName)(e.name,n);const i=typeof e.input?.action==="string"?e.input.action:void 0;if(!o||!i)return void 0;const f=findDescriptor(n,o,i);if(!f)return void 0;const d=e.input.params??{};const A=(0,_validateParams.applyDefaults)((0,_normalizeParams.normalizeParams)(o,i,d).params,f.params);if(!(0,_validateParams.validateParams)(A,f.params).ok)return void 0;const S=(0,_policy.decide)({descriptor:f,toolId:o,policy:q,isRelease:P});return{call:e,toolId:o,action:i,descriptor:f,params:A,label:(0,_policy.describeCall)(o,f,A),mutates:f.effect!=="read",gated:S.verdict==="needs-approval",reason:S.verdict==="needs-approval"?S.reason:""}});const xe=Re.some(e=>e?.gated);const x=new Map;let Ae=false;let Ie=false;async function*Me(){Ae=true;for(const e of Re){if(!e?.gated||x.has(e.call.id))continue;if(w?.has(`${e.toolId}.${e.action}`)){x.set(e.call.id,true);continue}yield{type:"approval-required",id:e.call.id,toolId:e.toolId,action:e.action,label:e.label,description:e.descriptor.summary,reason:e.reason};const o=(0,_realNow.realNow)();const i=await readTargetDigest({toolId:e.toolId,action:e.action,params:e.params,dispatch:u,catalog:n});const f=readAnswer(w?.has(`${e.toolId}.${e.action}`)?true:L?await L({id:e.call.id,toolId:e.toolId,action:e.action,label:e.label,description:e.descriptor.summary,reason:e.reason,params:e.params,targetDigest:i}):false);K+=(0,_realNow.realNow)()-o;const d=f.approved;x.set(e.call.id,d);if(f.trust)w?.add(`${e.toolId}.${e.action}`);if(f.reason)j.set(e.call.id,f.reason);if(!d){Ie=true;return}}}for(const e of k){if(E?.aborted){if(e.name!==_uiTool.UI_TOOL_NAME){for(const a of Pe(e))yield a}g.push({toolCallId:e.id,content:"Not run \u2014 the user pressed Stop before this call started.",isError:true});continue}if(e.name===_uiTool.UI_TOOL_NAME){const a=(0,_uiTool.buildUiBlock)(e.input);if(!a.ok||!a.block){g.push({toolCallId:e.id,content:`Invalid ${_uiTool.UI_TOOL_NAME} block:
2
+ ${a.errors.join("\n")}
2
3
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "digestOf", {
7
- enumerable: true,
8
- get: function () {
9
- return _digest.digestOf;
10
- }
11
- });
12
- exports.readTargetDigest = readTargetDigest;
13
- exports.runAgentTurn = runAgentTurn;
14
- exports.runGatedAction = runGatedAction;
15
- var _catalog = require("../catalog/catalog.g");
16
- var _validateParams = require("../catalog/validateParams");
17
- var _normalizeParams = require("../catalog/normalizeParams");
18
- var _toProviderTools = require("../catalog/toProviderTools");
19
- var _snapshotReads = require("../catalog/snapshotReads");
20
- var _ledger = require("../effects/ledger");
21
- var _policy = require("../policy/policy");
22
- var _redact = require("../policy/redact");
23
- var _effectFor = require("./effectFor");
24
- var _digest = require("../effects/digest");
25
- var _uiTool = require("../blocks/uiTool");
26
- var _receipts = require("../blocks/receipts");
27
- var _types = require("../blocks/types");
28
- var _askGate = require("./askGate");
29
- var _textToolCalls = require("./textToolCalls");
30
- var _historyBudget = require("./historyBudget");
31
- var _tokenCalibration = require("./tokenCalibration");
32
- var _evidence = require("./evidence");
33
- var _retrieve = require("./retrieve");
34
- var _verify = require("./verify");
35
- /**
36
- * One turn: user sentence in, tool calls and an answer out.
37
- *
38
- * The loop is deliberately explicit rather than clever, because every step of
39
- * it encodes something that bites otherwise:
40
- *
41
- * - Validation happens BEFORE dispatch and a failure comes back as a readable
42
- * tool_result, not an exception. Buoy's adapters validate nothing, so a
43
- * wrong shape becomes an undefined read deep inside a tool — and an LLM
44
- * omitting `replace` on `zustand.setState` replaces the entire store.
45
- * - `app.reloadApp` is TERMINAL. It returns before the reload fires and then
46
- * kills the JS realm; anything queued behind it dies with the conversation.
47
- * - Results are truncated before they are sent. `dispatchToolAction` bypasses
48
- * the 8MB wire cap that protects the broker path, so an in-app agent can
49
- * receive a 50MB object and then try to feed it to a token-metered model.
50
- * - Steps and wall-clock are capped, because a model that misreads a result
51
- * will otherwise retry the same call until the user closes the sheet.
52
- */
4
+ Resend ONLY the corrected block. Do not rewrite your answer \u2014 whatever you already said this turn has been shown to the user, and saying it again repeats it on their screen.`,isError:true});continue}let h=0;if(a.block.kind==="imageGrid"){const T=a.block.images.filter(R=>re.has(R.url));h=a.block.images.length-T.length;if(T.length===0){g.push({toolCallId:e.id,content:URL_PROVENANCE_REJECTION,isError:true});continue}a.block={...a.block,images:T}}if(a.block.kind==="list"){const T=a.block.items.map(R=>{if(!R.image||re.has(R.image))return R;h+=1;return{...R,image:void 0}});a.block={...a.block,items:T}}yield{type:"block",block:a.block,forToolCallId:e.id};if(a.block.kind==="actions"||a.block.kind==="suggestions")ge=true;if((0,_types.isAskBlock)(a.block)){ie=a.block.id;g.push({toolCallId:e.id,content:"Shown. Waiting for the user's answer \u2014 it will arrive as their next message. Do not guess it."})}else{g.push({toolCallId:e.id,content:"Shown to the user. Don't repeat its contents in text."+(h>0?droppedImageNote(h):"")})}continue}const o=(0,_toProviderTools.fromProviderToolName)(e.name,n);const i=e.input.action;const f=(a,h)=>[{type:"tool-start",id:e.id,toolId:o??e.name,action:i??"",label:a,description:"",params:e.input.params??{},effect:"read"},{type:"tool-end",id:e.id,ok:false,summary:h,durationMs:0}];if(!o){for(const a of f(e.name,"No such tool"))yield a;g.push({toolCallId:e.id,content:`There is no tool called "${e.name}". Use one of the tools you were given.`,isError:true});continue}if(!i){for(const a of f(e.name,"No action given"))yield a;g.push({toolCallId:e.id,content:`"${e.name}" needs an "action" \u2014 it is required, and it names which of this tool's actions to run. See the action list in the tool's description.`,isError:true});continue}const d=findDescriptor(n,o,i);if(!d){const a=n.find(h=>h.toolId===o)?.actions.map(h=>h.action).join(", ");for(const h of f(`${o}.${i}`,"No such action"))yield h;g.push({toolCallId:e.id,content:`"${i}" is not an action on ${o}.${a?` Valid actions: ${a}.`:""}`,isError:true});continue}const A=e.input.params??{};const S=(0,_normalizeParams.normalizeParams)(o,i,A);const s=(0,_validateParams.applyDefaults)(S.params,d.params);const I=(0,_validateParams.validateParams)(s,d.params);if(!I.ok){g.push({toolCallId:e.id,content:`Invalid parameters for ${o}.${i}:
5
+ ${I.errors.join("\n")}
53
6
 
54
- /** Beyond this a tool result costs more in tokens than it can possibly be worth. */
55
- const MAX_RESULT_CHARS = 24_000;
56
- /**
57
- * The result kept for the trace. Far smaller than what the MODEL gets — this
58
- * one is read by a person in a chat bubble or a pasted JSON file, and a 24k
59
- * store dump per step makes the trace unreadable rather than more useful.
60
- */
61
- const MAX_TRACE_RESULT_CHARS = 2_000;
62
- /**
63
- * How many identical rounds before the model is told it is looping.
64
- *
65
- * `maxSteps` alone was the whole answer, and it is the wrong shape: a model
66
- * that misread a result retries the same call until the cap, then the turn
67
- * ends with nothing — the user watches twelve identical rows go by and gets
68
- * no answer. The cap is a backstop, not feedback. Telling the model what it
69
- * is doing gives it the chance to change approach, and costs one sentence.
70
- *
71
- * Signature is name + action + arguments, key-sorted so `{b,a}` and `{a,b}`
72
- * are the same call. Three, not two: a legitimate retry after a transient
73
- * failure is normal, and warning on it would be noise.
74
- */
75
- const LOOP_REPEATS = 3;
76
- function callSignature(calls) {
77
- const sort = v => {
78
- if (v === null || typeof v !== "object") return v;
79
- if (Array.isArray(v)) return v.map(sort);
80
- const o = v;
81
- return Object.keys(o).sort().reduce((a, k) => (a[k] = sort(o[k]), a), {});
82
- };
83
- return calls.map(c => `${c.name}:${JSON.stringify(sort(c.input ?? {}))}`).join("|");
84
- }
85
- const LOOP_WARNING = "[Buoy] You have now made this exact call three times in a row and gotten the same thing back. Repeating it again will not change the answer. Read the result you already have, then either try a different tool or different arguments, or tell the user plainly what you could not get and what you tried.";
86
- const DEFAULT_MAX_STEPS = 12;
87
- const DEFAULT_TURN_MS = 180_000;
7
+ Expected schema:
8
+ ${JSON.stringify(d.params)}`,isError:true});continue}const W=(0,_normalizeParams.aliasNote)(S.notes);const M=(0,_policy.describeCall)(o,d,s);const O=(0,_policy.decide)({descriptor:d,toolId:o,policy:q,isRelease:P});const J=a=>[{type:"tool-start",id:e.id,toolId:o,action:i,label:M,description:d.summary,params:s,effect:d.effect},{type:"tool-end",id:e.id,ok:false,summary:a,durationMs:0}];if(O.verdict==="refuse"){for(const a of J("refused"))yield a;g.push({toolCallId:e.id,content:O.reason,isError:true});continue}const se=O.verdict==="needs-approval";if(xe&&!Ae&&(d.effect!=="read"||se)){yield*Me()}if(Ie&&(d.effect!=="read"||se)){for(const a of J("declined"))yield a;g.push({toolCallId:e.id,content:x.get(e.id)===false?declinedResult(j.get(e.id)):"Not run \u2014 the user declined another change in this same batch, so none of it was applied. Ask what they would prefer before proposing it again.",isError:true});continue}if(se){let a;if(x.has(e.id)){a=x.get(e.id)}else if(w?.has(`${o}.${i}`)){a=true;x.set(e.id,true)}else{yield{type:"approval-required",id:e.id,toolId:o,action:i,label:M,description:d.summary,reason:O.reason};const h=(0,_realNow.realNow)();const T=await readTargetDigest({toolId:o,action:i,params:s,dispatch:u,catalog:n});const R=readAnswer(L?await L({id:e.id,toolId:o,action:i,label:M,description:d.summary,reason:O.reason,params:s,targetDigest:T}):false);K+=(0,_realNow.realNow)()-h;a=R.approved;x.set(e.id,a);if(R.trust)w?.add(`${o}.${i}`);if(R.reason)j.set(e.id,R.reason)}if(!a){for(const h of J("declined"))yield h;g.push({toolCallId:e.id,content:declinedResult(j.get(e.id)),isError:true});continue}}if(_askGate.ACQUISITION_CALLS.has(`${o}.${i}`))be=true;yield{type:"tool-start",id:e.id,toolId:o,action:i,label:M,description:d.summary,params:s,effect:d.effect};const Se=(0,_realNow.realNow)();const _e=await captureBefore(d,o,s,u);const ae=await captureStoreState(d,o,s,u);if(E?.aborted){for(const a of J("stopped"))yield a;g.push({toolCallId:e.id,content:"Not run \u2014 the user pressed Stop before this call started.",isError:true});continue}const Ne=(0,_policy.decide)({descriptor:d,toolId:o,policy:q,isRelease:P});if(Ne.verdict==="refuse"){for(const a of J("refused"))yield a;g.push({toolCallId:e.id,content:Ne.reason,isError:true});continue}try{const a=i===_snapshotReads.SNAPSHOT_ACTION?(0,_snapshotReads.projectSnapshot)(o,await requireSnapshot(y,o),s):o==="ask-buoy"?await runAskBuoyAction(i,v,u,U,s,D):await(Te.get(e.id)??u(o,i,s));const h=(0,_redact.redact)((0,_redact.stripSelfTraffic)(a));const T=reportsFailure(a);const R=fullText(h);const de=U&&!T&&!(o==="ask-buoy"&&i==="retrieve")?U.stash({toolId:o,action:i,params:s,capturedAt:(0,_realNow.realNow)(),text:R}):void 0;const Y=encodeResult(R,de);const Le=Y.length>MAX_TRACE_RESULT_CHARS?`${Y.slice(0,MAX_TRACE_RESULT_CHARS)}\u2026`:Y;yield{type:"tool-end",id:e.id,ok:!T,summary:summarise(a),durationMs:(0,_realNow.realNow)()-Se,result:Le};const le=ae===void 0||T?void 0:await captureStoreState(d,o,s,u);if(d.effect!=="read"&&o!=="ask-buoy"&&!T){const ce=v.noteExternalRevert(o,i,s);const $e=(0,_effectFor.effectFor)(o,d,s,a,_e,{before:ae,after:le});if($e&&!ce){const Ce=v.record(o,i,$e,Oe);if(Ce)Ce.callId=e.id}}const Ee=T?void 0:(0,_receipts.projectReceipt)({toolId:o,action:i,params:s,result:h,before:ae??_e,after:le,effect:d.effect});if(Ee)yield{type:"block",block:Ee,forToolCallId:e.id};const ee=d.effect!=="read"&&!T?await(0,_verify.verifyOutcome)({toolId:o,action:i,params:s,result:a,dispatch:u,signal:E,after:le}):void 0;if(ee)yield{type:"tool-verified",id:e.id,verification:ee};for(const ce of Y.match(qe)??[])re.add(ce);g.push({toolCallId:e.id,content:Y+ownedKeyNote(o,d,s,H)+W+(ee?(0,_verify.verificationTrailer)(ee):"")+(ve.has(e.id)?_textToolCalls.SALVAGE_NOTE:""),...de?{ref:de}:{}});if(o==="app"&&(i==="reloadApp"||i==="reload")){we=true}}catch(a){const h=a instanceof Error?a.message:String(a);yield{type:"tool-end",id:e.id,ok:false,summary:h,durationMs:(0,_realNow.realNow)()-Se};g.push({toolCallId:e.id,content:`Failed: ${h}`,isError:true})}}if(k.length){B.push(callSignature(k));if(B.length>LOOP_REPEATS)B.shift();const e=!ye&&B.length===LOOP_REPEATS&&B.every(o=>o===B[0]);if(e){ye=true;const o=g[g.length-1];if(o)o.content=`${o.content}
88
9
 
89
- /**
90
- * Retrying a model request — narrowly.
91
- *
92
- * A 429 from a shared gateway, a 529 / `overloaded_error` from Anthropic, or
93
- * a "prompt is too long" 400 used to end the turn with an error card and Try
94
- * again — and Try again re-sends the QUESTION, restarting an investigation
95
- * that may already have written things. All three fail before a model has
96
- * generated anything, so asking again is safe: nothing runs twice, nothing
97
- * is billed twice. The rule that makes it safe is enforced, not assumed —
98
- * a request is only retried when its stream produced NO text, tool call or
99
- * thinking; once anything has come back, the existing no-replay handling
100
- * stands (see providers/transport.ts on consumed-exactly-once).
101
- *
102
- * Throttled: up to two retries, 1 s then 2 s (plus jitter), a `Retry-After`
103
- * winning when the endpoint sent one. Overflow: one retry, with the history
104
- * ceiling halved first — the proactive budget is chars ÷ 4 and the provider
105
- * just said that was optimistic. Both wait inside the turn deadline and stop
106
- * on the user's Stop. Small numbers on purpose: this is a phone, not a
107
- * server queue.
108
- */
109
- const THROTTLE_RETRIES = 2;
110
- const OVERFLOW_RETRIES = 1;
111
- const RETRY_BASE_MS = 1_000;
112
- const RETRY_JITTER_MS = 250;
113
- const RETRY_AFTER_CAP_MS = 30_000;
114
- function sleep(ms, signal) {
115
- return new Promise(resolve => {
116
- if (signal?.aborted) return resolve();
117
- const t = setTimeout(done, ms);
118
- function done() {
119
- clearTimeout(t);
120
- signal?.removeEventListener("abort", done);
121
- resolve();
122
- }
123
- signal?.addEventListener("abort", done, {
124
- once: true
125
- });
126
- });
127
- }
10
+ ${LOOP_WARNING}`}}p.push({role:"tool-results",results:g});if(ie){yield{type:"awaiting-user",blockId:ie};yield{type:"done",stopReason:"awaiting-user"};return p}if(we){yield{type:"done",stopReason:"realm-died"};return p}}yield{type:"block",block:{id:`cap${(0,_realNow.realNow)()}`,kind:"notice",tone:"warning",text:`Stopped after ${ue} steps to avoid looping.`,actions:[{label:"Continue",primary:true,send:"Continue where you left off."}]}};yield{type:"done",stopReason:"step-cap"};return p}function ownedKeyNote(t,r,n,l){if(t!=="storage"||r.effect==="read")return"";if(!l||typeof n.key!=="string")return"";const c=l[n.key];if(!c)return"";return`
128
11
 
129
- /**
130
- * Why the turn ended — the machine-readable half of the notices. The text in
131
- * the notice blocks is unchanged (the eval classifier pins it); this is for a
132
- * host, the bank and the desktop, which used to have to grep the prose.
133
- */
134
-
135
- /** Normalise an answer to its parts, so both call sites read one shape. */
136
- function readAnswer(answer) {
137
- if (typeof answer === "boolean") return {
138
- approved: answer,
139
- trust: false
140
- };
141
- const reason = answer.reason?.trim();
142
- return {
143
- approved: answer.approved,
144
- ...(reason ? {
145
- reason
146
- } : {}),
147
- trust: answer.trust === true && answer.approved
148
- };
149
- }
150
-
151
- /** What the model is told when the user says no — with their note, when they left one. */
152
- function declinedResult(reason) {
153
- return reason ? `The user declined this change and said: "${reason}". Do not retry it as proposed; take their note into account and, if a different change would fit, propose that instead.` : "The user declined this change. Do not retry it; ask what they would prefer.";
154
- }
155
- function findDescriptor(catalog, toolId, action) {
156
- return catalog.find(t => t.toolId === toolId)?.actions.find(a => a.action === action);
157
- }
158
-
159
- /**
160
- * Why a picture did not render, in the model's own terms.
161
- *
162
- * Two messages, not one, because they answer different questions: the first
163
- * is "your block was refused", the second is "your block was shown but with
164
- * holes in it". The literal opening of the rejection is pinned by the
165
- * trajectory classifier (evals/ask-buoy/lib/trajectory.ts) — keep it.
166
- */
167
- const URL_PROVENANCE_REJECTION = "Rejected: every image URL must be one you read from a tool result in THIS conversation (images.list, a storage value, a response body). Never invent or remember URLs — read them first, then show them.";
168
- const droppedImageNote = n => ` NOTE: ${n} image ${n === 1 ? "URL was" : "URLs were"} dropped — they were not read from a tool result in this conversation, so the card the user is looking at has no picture there. Do not tell them it does; read the URLs and show it again, or say the artwork is missing.`;
169
-
170
- /** The whole result as text — what the evidence store keeps. */
171
- function fullText(value) {
172
- try {
173
- return JSON.stringify(value ?? null);
174
- } catch {
175
- return String(value);
176
- }
177
- }
178
-
179
- /** What the model is sent: the text, cut at the cap with a marker that names where the rest is. */
180
- function encodeResult(text, ref) {
181
- if (text.length <= MAX_RESULT_CHARS) return text;
182
- return `${text.slice(0, MAX_RESULT_CHARS)}${(0, _evidence.truncationMarker)(text.length, ref)}`;
183
- }
184
-
185
- /** A short line for the transcript row, so the UI never renders raw payloads. */
186
- /**
187
- * The trailing status word on a transcript row — "12 items", "Not found",
188
- * "Done". Written for the person watching, not the model (which gets the full
189
- * result): a list of JSON key names ("found, storeName, …") told a QA tester
190
- * nothing, so this reads the few result shapes that carry meaning and says
191
- * "Done" for the rest.
192
- */
193
- function summarise(value) {
194
- if (value === null || value === undefined) return "Done";
195
- if (Array.isArray(value)) return countOf(value.length);
196
- if (typeof value === "boolean") return value ? "Done" : "Failed";
197
- if (typeof value === "object") {
198
- const rec = value;
199
- if (rec.ok === false) return typeof rec.error === "string" ? rec.error : "Failed";
200
- if (rec.found === false) return "Not found";
201
- if (rec.tapped === false) return "Nothing to tap";
202
- if (rec.active === false) return "Saved, but inactive in this build";
203
- for (const key of ["returned", "count", "total"]) {
204
- const n = rec[key];
205
- if (typeof n === "number") return countOf(n);
206
- }
207
- // `{requests:[…]}` → "12 items", but a snapshot with several lists
208
- // (`{stack, routes, recentNavigations}`) has no single honest count.
209
- const lists = Object.values(rec).filter(Array.isArray);
210
- if (lists.length === 1) return countOf(lists[0].length);
211
- return "Done";
212
- }
213
- return String(value).slice(0, 80);
214
- }
215
- function countOf(n) {
216
- return n === 1 ? "1 item" : `${n} items`;
217
- }
218
-
219
- /**
220
- * A call that came back reporting its own failure.
221
- *
222
- * Buoy adapters RESOLVE with `{ok:false, error}` rather than throwing, so the
223
- * dispatch's try/catch never fires and a refused write looked like a completed
224
- * one: the row read "Done", and — much worse — the effect ledger recorded a
225
- * change that had not happened, so the changes bar offered to undo nothing.
226
- * Caught on device: a typed-edit refusal ("qty is a number, 'many' is text")
227
- * left the bag untouched and the bar claiming one permanent change.
228
- *
229
- * `ok === false` is the same signal `summarise` above already trusts to turn a
230
- * result into the row's text, so this reads it no more liberally than the UI
231
- * already does.
232
- */
233
- function reportsFailure(value) {
234
- return typeof value === "object" && value !== null && value.ok === false;
235
- }
236
- async function* runAgentTurn(input) {
237
- const {
238
- provider,
239
- catalog,
240
- system,
241
- systemVolatile,
242
- model,
243
- maxTokens,
244
- dispatch,
245
- readSnapshot,
246
- storeKeyOwners,
247
- ledger,
248
- policy,
249
- isRelease,
250
- requestApproval,
251
- signal,
252
- evidence,
253
- trusted,
254
- procedures
255
- } = input;
256
- /** Notes the user left with a decline, by call id — see declinedResult. */
257
- const declineReasons = new Map();
258
- const calibration = input.calibration ?? new _tokenCalibration.TokenCalibration();
259
- const messages = [...input.messages];
260
- const tools = (0, _toProviderTools.toProviderTools)(catalog, {
261
- availableToolIds: input.availableToolIds,
262
- availableActions: input.availableActions,
263
- isRelease
264
- });
265
- tools.push((0, _uiTool.uiProviderTool)());
266
- const maxSteps = policy.maxSteps ?? DEFAULT_MAX_STEPS;
267
- /**
268
- * The conversation this turn belongs to. Handed back on every `record` so a
269
- * dispatch that settles after the user started a new conversation lands in
270
- * the app (nothing can pull it back) but not in the new chat's undo bar.
271
- */
272
- const ledgerGeneration = ledger.generation;
273
- /**
274
- * What every request costs before a single message is added: the tool
275
- * definitions, the system prompt, and the room the model needs to reply.
276
- *
277
- * Measured, not guessed — the tool block alone is ~32k characters for a
278
- * typical install and ~94k with all 24 tools available, which is far too
279
- * much to leave out of a budget. `maxTokens` is multiplied by four because
280
- * the budget is in characters.
281
- */
282
- const fixedChars = system.length + (systemVolatile?.length ?? 0) + tools.reduce((n, t) => n + t.description.length + JSON.stringify(t.inputSchema).length, 0);
283
- /** The reserve at the ratio believed RIGHT NOW — it moves as usage reports come in. */
284
- const requestReserve = () => fixedChars + calibration.chars(maxTokens);
285
- /**
286
- * Caps the AGENT's wall-clock, not the user's. Time spent parked on an
287
- * approval sheet is pushed onto the deadline as it is spent (see the
288
- * `requestApproval` await below) — otherwise a user who takes three minutes
289
- * to tap Approve gets the change applied and then "this is taking too long"
290
- * for the turn that applied it.
291
- */
292
- let deadline = Date.now() + DEFAULT_TURN_MS;
293
- /** Retries spent this turn, per kind — the budget is per turn, not per step. */
294
- const retries = {
295
- throttled: 0,
296
- overflow: 0
297
- };
298
- /** Tightened after an overflow; see budgetForRequest's `cap`. In tokens, so a calibration change re-scales it. */
299
- let historyCapTokens = _historyBudget.MAX_HISTORY_TOKENS;
300
-
301
- // Every http(s) URL that appeared in a tool result in this CONVERSATION.
302
- // The prompt tells the model image URLs must come from here; this Set is
303
- // the enforcement — a model-remembered or injected URL in an image block is
304
- // both a fabrication vector and a data-exfil beacon (the GET carries
305
- // whatever is encoded into the URL). Session-owned when the caller passes
306
- // one; see RunTurnInput.seenUrls for why it is not per-turn.
307
- const seenUrls = input.seenUrls ?? new Set();
308
- const URL_RE = /https?:\/\/[^\s"'\\)\]}>]+/g;
309
-
310
- /** Whether any text has gone out THIS TURN — see `breakBefore`. */
311
- let answerStarted = false;
312
-
313
- /**
314
- * The last few rounds' call signatures. See LOOP_REPEATS — a run of
315
- * identical rounds gets one warning appended to the results, once, so the
316
- * model is told rather than silently capped.
317
- */
318
- const recentSignatures = [];
319
- let loopWarned = false;
320
-
321
- /**
322
- * Everything the model SAID this turn, in order. Read once, at the end, by
323
- * the ask gate — a question asked in round one and left hanging is the same
324
- * dead end as one asked in the last round. See askGate.ts.
325
- */
326
- const spoken = [];
327
- /**
328
- * Whether the user already has something to tap. An `actions` or
329
- * `suggestions` block is the model doing this job itself, and a second card
330
- * asking the same thing underneath it is worse than none.
331
- *
332
- * Note this does NOT suppress the shortfall gate below — that one asks
333
- * whether the user can tap the GAP, which unrelated chips do not answer.
334
- */
335
- let offeredTap = false;
336
- /**
337
- * Whether this turn actually went and looked — navigated, described the
338
- * screen, tapped something, refetched. The shortfall gate stays quiet when
339
- * it did: an agent that tried and came up short has earned its answer.
340
- */
341
- let attemptedAcquisition = false;
342
- for (let step = 0; step < maxSteps; step++) {
343
- if (signal?.aborted) {
344
- yield {
345
- type: "done",
346
- stopReason: "stopped"
347
- };
348
- return messages;
349
- }
350
- if (Date.now() > deadline) {
351
- // Same shape as the step cap below. This used to be an `error`, which
352
- // drew the failed-turn card with Try again — and Try again re-sends the
353
- // question, restarting the whole investigation that just ran out of
354
- // time. Continue picks up with everything so far still in history.
355
- yield {
356
- type: "block",
357
- block: {
358
- id: `cap${Date.now()}`,
359
- kind: "notice",
360
- tone: "warning",
361
- // Keeps the "This is taking too long" prefix: the eval classifier
362
- // (evals/ask-buoy/lib/trajectory.ts) tells a time-out from a step
363
- // cap by it.
364
- text: `This is taking too long — stopped after ${Math.round(DEFAULT_TURN_MS / 1000)}s. What it found so far is above.`,
365
- actions: [{
366
- label: "Continue",
367
- primary: true,
368
- send: "Continue where you left off."
369
- }]
370
- }
371
- };
372
- yield {
373
- type: "done",
374
- stopReason: "time-cap"
375
- };
376
- return messages;
377
- }
378
-
379
- /**
380
- * BUDGET, EVERY REQUEST — not once per turn.
381
- *
382
- * The session trims when a turn opens, and that used to be the only
383
- * check. But a turn is not one request: each step appends an assistant
384
- * message and a tool-results message, and a single result can be 24,000
385
- * characters. A twelve-step investigation could therefore add six figures
386
- * of history AFTER the only check had run, and the turn died on the
387
- * provider's context limit with an opaque error — in exactly the long
388
- * sessions the tool is for.
389
- *
390
- * The current round is never touched: it is the question being answered.
391
- */
392
- const budgeted = (0, _historyBudget.budgetForRequest)(messages, requestReserve(), calibration.chars(historyCapTokens), calibration.charsPerToken, ledger.liveCallIds());
393
- if (budgeted.droppedRounds > 0) {
394
- messages.length = 0;
395
- messages.push(...budgeted.messages);
396
- yield {
397
- type: "history-trimmed",
398
- droppedRounds: budgeted.droppedRounds,
399
- droppedPinned: budgeted.droppedPinned
400
- };
401
- } else if (budgeted.messages !== messages) {
402
- messages.length = 0;
403
- messages.push(...budgeted.messages);
404
- }
405
- let text = "";
406
- /** See `breakBefore`: set once the first text of THIS round has gone out. */
407
- let textThisRound = false;
408
- const textEvent = delta => {
409
- // Only when this round opens a NEW paragraph of an answer that has
410
- // already started. A turn whose first words arrive in round three has
411
- // nothing to be separated from, and marking that would put the flag on
412
- // events where it means nothing.
413
- const opensNewRound = answerStarted && !textThisRound;
414
- textThisRound = true;
415
- answerStarted = true;
416
- return opensNewRound ? {
417
- type: "text",
418
- delta,
419
- breakBefore: true
420
- } : {
421
- type: "text",
422
- delta
423
- };
424
- };
425
- let calls = [];
426
- /**
427
- * Recovered from text rather than emitted as calls — see textToolCalls.ts.
428
- * Tracked by id so each one's result can tell the model to stop doing that;
429
- * they are otherwise ordinary calls and get every check the others get.
430
- */
431
- const salvagedIds = new Set();
432
- /**
433
- * Text withheld from the screen while it could still be a tool-call
434
- * envelope. Streamed text cannot be un-shown, so the choice has to be made
435
- * before it leaves — and a model that writes its call as JSON must not
436
- * have that JSON become the answer the user reads.
437
- */
438
- let held = "";
439
- let holding = true;
440
- // Carried, never read: Anthropic requires the turn's thinking blocks back
441
- // verbatim with its tool results. See providers/anthropic.ts note 5.
442
- let thinking = [];
443
- let failed = false;
444
- /**
445
- * What the provider said about how the stream ended.
446
- *
447
- * Undefined means it never said — a bare EOF, or a host-supplied provider
448
- * whose generator simply returned. Both are treated as incomplete, so the
449
- * fail-safe direction is the default rather than something each adapter
450
- * has to remember to opt into.
451
- */
452
- let outcome;
453
- /** Set for the two kinds that are recoverable rather than a hard failure. */
454
- let incomplete;
455
-
456
- /**
457
- * THE REQUEST, with its retries. One request per pass; a pass that fails
458
- * before the model generated anything may be sent again (see
459
- * THROTTLE_RETRIES). Everything the pass accumulated is reset first —
460
- * there is nothing to keep, by the rule that made the retry safe.
461
- */
462
- for (;;) {
463
- text = "";
464
- calls = [];
465
- held = "";
466
- holding = true;
467
- thinking = [];
468
- failed = false;
469
- outcome = undefined;
470
- incomplete = undefined;
471
- /** The failure that ended this pass, when it is one the engine may retry. */
472
- let retryable;
473
- /** What this pass sends, in characters — the numerator of the calibration sample. */
474
- const sentChars = fixedChars + messages.reduce((n, m) => n + (0, _historyBudget.messageSize)(m), 0);
475
- for await (const ev of provider.send({
476
- messages,
477
- system,
478
- systemVolatile,
479
- tools,
480
- model,
481
- maxTokens,
482
- signal
483
- })) {
484
- if (ev.type === "text") {
485
- text += ev.delta;
486
- if (!holding) {
487
- yield textEvent(ev.delta);
488
- } else if ((0, _textToolCalls.couldBeToolCallEnvelope)(text)) {
489
- held += ev.delta;
490
- } else {
491
- // Not an envelope after all. Release everything at once and stream
492
- // the rest as usual — the reader loses nothing but a few characters
493
- // of latency at the very start of the answer.
494
- holding = false;
495
- held = "";
496
- yield textEvent(text);
497
- }
498
- } else if (ev.type === "tool-call") {
499
- calls.push(ev.call);
500
- } else if (ev.type === "thinking") {
501
- thinking.push(ev.block);
502
- // Surfaced as well as carried: the block goes back to the provider
503
- // verbatim (note above), and the readable half goes to the UI so a
504
- // tester can see WHY a turn did what it did. Redacted blocks have no
505
- // readable half and are carried only.
506
- if (ev.block.type === "thinking" && ev.block.thinking.trim()) {
507
- yield {
508
- type: "reasoning",
509
- text: ev.block.thinking
510
- };
511
- }
512
- } else if (ev.type === "error") {
513
- if (ev.kind === "truncated" || ev.kind === "timeout") {
514
- // Not a failure the user did anything about, and not one Try again
515
- // can fix — re-sending the QUESTION restarts an investigation that
516
- // may already have written things. Held back from the error card
517
- // (which is what draws Try again) and handled below as a recoverable
518
- // stop with a Continue button.
519
- incomplete = {
520
- message: ev.message
521
- };
522
- } else if (ev.problem && (ev.problem.kind === "throttled" || ev.problem.kind === "overflow") && text === "" && calls.length === 0 && thinking.length === 0) {
523
- // Nothing generated, and a kind that a wait or a trim can fix.
524
- // Decided after the stream closes; the adapter returns on error.
525
- retryable = {
526
- problem: ev.problem,
527
- message: ev.message
528
- };
529
- } else {
530
- // A mid-stream error can arrive on an already-committed 200.
531
- yield {
532
- type: "error",
533
- message: ev.message
534
- };
535
- failed = true;
536
- }
537
- } else if (ev.type === "done") {
538
- outcome = ev.outcome;
539
- if (ev.usage) {
540
- // The provider just counted this prompt. One sample per request
541
- // keeps the chars↔tokens ratio honest for the NEXT budget.
542
- calibration.observe(sentChars, (0, _tokenCalibration.promptTokensOf)(ev.usage, provider.protocol));
543
- // Surfaced so a host can meter cost per seat. Parsed for a long time;
544
- // went nowhere anyone could see.
545
- yield {
546
- type: "usage",
547
- ...ev.usage,
548
- model: ev.model
549
- };
550
- }
551
- }
552
- }
553
- if (!retryable) break;
554
- const kind = retryable.problem.kind;
555
- const maxAttempts = 1 + (kind === "throttled" ? THROTTLE_RETRIES : OVERFLOW_RETRIES);
556
- const spent = retries[kind];
557
- let waitMs = kind === "throttled" ? Math.min(retryable.problem.retryAfterMs ?? RETRY_BASE_MS * 2 ** spent, RETRY_AFTER_CAP_MS) + Math.floor(Math.random() * RETRY_JITTER_MS) : 0;
558
- let giveUp = spent >= maxAttempts - 1 || signal?.aborted === true || Date.now() + waitMs > deadline;
559
- if (!giveUp && kind === "overflow") {
560
- // Halve the ceiling and trim again. If that changes nothing — the
561
- // current round alone is over the limit — a retry would only repeat
562
- // the refusal, so report it instead.
563
- historyCapTokens = Math.floor(historyCapTokens / 2);
564
- const before = messages.reduce((n, m) => n + (0, _historyBudget.messageSize)(m), 0);
565
- const again = (0, _historyBudget.budgetForRequest)(messages, requestReserve(), calibration.chars(historyCapTokens), calibration.charsPerToken, ledger.liveCallIds());
566
- const after = again.messages.reduce((n, m) => n + (0, _historyBudget.messageSize)(m), 0);
567
- if (after >= before) {
568
- giveUp = true;
569
- } else {
570
- messages.length = 0;
571
- messages.push(...again.messages);
572
- if (again.droppedRounds > 0) yield {
573
- type: "history-trimmed",
574
- droppedRounds: again.droppedRounds,
575
- droppedPinned: again.droppedPinned
576
- };
577
- }
578
- }
579
- if (giveUp) {
580
- // Plain words first; the endpoint's own text after, for whoever files
581
- // the bug. A user reading "prompt is too long: 213000 tokens" has no
582
- // move; "start a new conversation" is one.
583
- const plain = kind === "throttled" ? spent > 0 ? `The AI endpoint is rate-limiting requests — tried ${spent + 1} times.` : "The AI endpoint is rate-limiting requests." : "This conversation is too large for the AI endpoint and there was nothing older to trim. Start a new conversation, or ask a shorter question.";
584
- yield {
585
- type: "error",
586
- message: `${plain} (${retryable.message})`
587
- };
588
- failed = true;
589
- break;
590
- }
591
- retries[kind] += 1;
592
- yield {
593
- type: "retrying",
594
- reason: kind,
595
- attempt: retries[kind] + 1,
596
- maxAttempts,
597
- inMs: waitMs
598
- };
599
- if (waitMs > 0) await sleep(waitMs, signal);
600
- if (signal?.aborted) {
601
- yield {
602
- type: "done",
603
- stopReason: "stopped"
604
- };
605
- return messages;
606
- }
607
- }
608
- if (failed) {
609
- // Whatever was withheld is the model's own words on the way to an error.
610
- // Show it rather than swallowing it.
611
- if (held) yield textEvent(held);
612
- yield {
613
- type: "done",
614
- stopReason: "error"
615
- };
616
- return messages;
617
- }
618
-
619
- /**
620
- * THE COMPLETION GATE. Nothing below this runs a tool unless the provider
621
- * said the response was whole.
622
- *
623
- * The failure it exists for is not theoretical and does not look like a
624
- * failure: a connection cut after the model emitted
625
- * `{"key":"cart","value":[]}` leaves valid JSON, a plausible-looking plan,
626
- * and no error anywhere. The old code parsed those arguments, dispatched
627
- * the write, sent the result back and carried on — a real mutation from
628
- * half a sentence. Bare EOF is not consent.
629
- *
630
- * `output-limited` is the same shape from a different cause: the model hit
631
- * max_tokens partway through planning. What it managed to SAY is real and
632
- * is shown; what it was part-way through DOING is not run.
633
- */
634
- if (!incomplete && outcome !== "completed") {
635
- incomplete = {
636
- message: outcome === "output-limited" ? calls.length ? "The model ran out of room mid-plan, so nothing was run. What it found so far is above." : "The model ran out of room before finishing this answer." : "The answer ended before the endpoint said it was finished, so nothing was run."
637
- };
638
- }
639
- if (incomplete) {
640
- // Held text is the model's own words on the way out. Show it: the user
641
- // watched it arrive and hiding it now reads as the app losing work.
642
- if (held) yield textEvent(held);
643
- /**
644
- * Text only — never the tool calls, and never the thinking.
645
- *
646
- * An assistant turn carrying `tool_use` with no matching `tool_result`
647
- * is an instant 400 on the next request, and these calls are exactly the
648
- * ones that must not be answered. Keeping the text preserves what the
649
- * user can see on screen for whatever comes next.
650
- */
651
- if (text.trim()) messages.push({
652
- role: "assistant",
653
- text
654
- });
655
- yield {
656
- type: "block",
657
- block: {
658
- id: `cut${Date.now()}`,
659
- kind: "notice",
660
- tone: "warning",
661
- text: incomplete.message,
662
- actions: [{
663
- label: "Continue",
664
- primary: true,
665
- send: "Continue where you left off."
666
- }]
667
- }
668
- };
669
- yield {
670
- type: "done",
671
- stopReason: "incomplete"
672
- };
673
- return messages;
674
- }
675
- if (held) {
676
- // A model that wrote its call out as JSON instead of calling it. Run it,
677
- // and show its `reply` — never the blob. See textToolCalls.ts.
678
- const salvaged = calls.length === 0 ? (0, _textToolCalls.parseTextToolCalls)(held, catalog) : undefined;
679
- if (salvaged) {
680
- for (const call of salvaged.calls) {
681
- calls.push(call);
682
- salvagedIds.add(call.id);
683
- }
684
- text = salvaged.reply;
685
- if (salvaged.reply) yield textEvent(salvaged.reply);
686
- } else {
687
- yield textEvent(held);
688
- }
689
- held = "";
690
- }
691
- messages.push({
692
- role: "assistant",
693
- text,
694
- toolCalls: calls.length ? calls : undefined,
695
- thinking: thinking.length ? thinking : undefined
696
- });
697
- if (text.trim()) spoken.push(text.trim());
698
- if (calls.length === 0) {
699
- // The turn is over and the model wrote its own words. Two ways those
700
- // words end badly, and they are different failures — see askGate.ts.
701
- const said = spoken.join("\n\n");
702
-
703
- // 1. It answered a smaller question than it was asked and handed the
704
- // rest back. Fires even when chips were offered: the question is
705
- // whether the user can tap the GAP, not whether they can tap.
706
- const gap = (0, _askGate.synthesizeShortfallBlock)({
707
- text: said,
708
- attempted: attemptedAcquisition
709
- });
710
- if (gap) yield {
711
- type: "block",
712
- block: gap
713
- };
714
-
715
- // 2. It ended waiting on the user in prose. Suppressed once anything
716
- // tappable is on screen, the gap button included.
717
- const asked = offeredTap || gap ? null : (0, _askGate.synthesizeAskBlock)(said);
718
- if (asked) {
719
- yield {
720
- type: "block",
721
- block: asked
722
- };
723
- yield {
724
- type: "awaiting-user",
725
- blockId: asked.id
726
- };
727
- }
728
- yield {
729
- type: "done",
730
- stopReason: asked ? "awaiting-user" : "answered"
731
- };
732
- return messages;
733
- }
734
- const results = [];
735
- let realmWillDie = false;
736
-
737
- /**
738
- * Reads, already in flight.
739
- *
740
- * The loop below stays strictly serial — every yield, every ledger entry
741
- * and every approval keeps its order — but a round of independent READS
742
- * used to pay each round trip end to end. The prompt tells the model to
743
- * chain reads freely ("reading is cheap; do it"), and a three-read round
744
- * cost three times the device latency for no reason.
745
- *
746
- * ONLY when the whole batch is safe to overlap: every call a catalog read,
747
- * none needing approval, no `buoy_ui`, nothing snapshot-only or
748
- * session-local. One write, one approval or one unknown action in the
749
- * batch and nothing is prefetched — the mixed case is where ordering
750
- * actually matters, and it is not worth the risk for the latency.
751
- */
752
- const prefetch = new Map();
753
- if (calls.length > 1) {
754
- const safe = calls.every(c => {
755
- if (c.name === _uiTool.UI_TOOL_NAME) return false;
756
- const id = (0, _toProviderTools.fromProviderToolName)(c.name, catalog);
757
- if (!id || id === "ask-buoy") return false;
758
- const act = c.input?.action;
759
- if (typeof act !== "string" || act === _snapshotReads.SNAPSHOT_ACTION) return false;
760
- const d = findDescriptor(catalog, id, act);
761
- if (!d || d.effect !== "read") return false;
762
- return (0, _policy.decide)({
763
- descriptor: d,
764
- toolId: id,
765
- policy,
766
- isRelease
767
- }).verdict === "allow";
768
- });
769
- if (safe) {
770
- for (const c of calls) {
771
- const id = (0, _toProviderTools.fromProviderToolName)(c.name, catalog);
772
- const act = String(c.input.action);
773
- const raw = {
774
- ...(c.input.params ?? {})
775
- };
776
- const d = findDescriptor(catalog, id, act);
777
- const norm = (0, _normalizeParams.normalizeParams)(id, act, raw);
778
- const withDefaults = (0, _validateParams.applyDefaults)(norm.params, d.params);
779
- if (!(0, _validateParams.validateParams)(withDefaults, d.params).ok) continue;
780
- // Rejections are swallowed here and re-awaited in the loop, where
781
- // they are turned into the same tool_result they always were.
782
- const p = Promise.resolve(dispatch(id, act, withDefaults)).catch(e => {
783
- throw e;
784
- });
785
- p.catch(() => {});
786
- prefetch.set(c.id, p);
787
- }
788
- }
789
- }
790
- let awaiting = null;
791
-
792
- /**
793
- * A call Stop got to first — as a row, so the transcript says which ones.
794
- *
795
- * Resolved leniently: the call has not been validated yet, and a label
796
- * that falls back to `tool.action` is better than no row for a call the
797
- * user needs to know did not run.
798
- */
799
- const stoppedStep = call => {
800
- const toolId = (0, _toProviderTools.fromProviderToolName)(call.name, catalog) ?? call.name;
801
- const action = typeof call.input?.action === "string" ? call.input.action : "";
802
- const descriptor = action ? findDescriptor(catalog, toolId, action) : undefined;
803
- const params = call.input?.params ?? {};
804
- return [{
805
- type: "tool-start",
806
- id: call.id,
807
- toolId,
808
- action,
809
- label: descriptor ? (0, _policy.describeCall)(toolId, descriptor, params) : `${toolId}.${action}`,
810
- description: descriptor?.summary ?? "",
811
- params,
812
- effect: descriptor?.effect ?? "read"
813
- }, {
814
- type: "tool-end",
815
- id: call.id,
816
- ok: false,
817
- summary: "stopped",
818
- durationMs: 0
819
- }];
820
- };
821
-
822
- /**
823
- * THE BATCH BARRIER — what the whole batch will do, decided before any of
824
- * it does anything.
825
- *
826
- * A model answers with several calls at once, and they used to run one at
827
- * a time with each approval asked when its own call came up. So
828
- * `[set the flag, delete the cache]` wrote the flag, THEN asked about the
829
- * delete — and a user who declined had already changed the app, with the
830
- * bubble reading "changed the flag" directly above "left the cache alone".
831
- * Declining is supposed to mean the plan does not happen.
832
- *
833
- * The fix is not to ask again later or to refuse afterwards, neither of
834
- * which can unrun a write. It is to ask FIRST: every gated call in the
835
- * batch is put to the user before the batch's first mutation dispatches,
836
- * so consent is given with the whole plan visible.
837
- *
838
- * Resolution here is pure and duplicates the loop's own — deliberately.
839
- * Anything it cannot resolve (an unknown tool, a bad shape) comes back as
840
- * neither mutating nor gated, so the loop reports it exactly as it always
841
- * did and the barrier simply does not fire. Failing back to today's
842
- * behaviour is the right failure for a safety gate to have.
843
- */
844
- const planned = calls.map(call => {
845
- if (call.name === _uiTool.UI_TOOL_NAME) return undefined;
846
- const toolId = (0, _toProviderTools.fromProviderToolName)(call.name, catalog);
847
- const action = typeof call.input?.action === "string" ? call.input.action : undefined;
848
- if (!toolId || !action) return undefined;
849
- const descriptor = findDescriptor(catalog, toolId, action);
850
- if (!descriptor) return undefined;
851
- const raw = call.input.params ?? {};
852
- const params = (0, _validateParams.applyDefaults)((0, _normalizeParams.normalizeParams)(toolId, action, raw).params, descriptor.params);
853
- if (!(0, _validateParams.validateParams)(params, descriptor.params).ok) return undefined;
854
- const verdict = (0, _policy.decide)({
855
- descriptor,
856
- toolId,
857
- policy,
858
- isRelease
859
- });
860
- return {
861
- call,
862
- toolId,
863
- action,
864
- descriptor,
865
- params,
866
- label: (0, _policy.describeCall)(toolId, descriptor, params),
867
- mutates: descriptor.effect !== "read",
868
- gated: verdict.verdict === "needs-approval",
869
- reason: verdict.verdict === "needs-approval" ? verdict.reason : ""
870
- };
871
- });
872
- const gatedInBatch = planned.some(p => p?.gated);
873
- /** Decisions the barrier has already taken, so no call is asked twice. */
874
- const decided = new Map();
875
- let barrierRun = false;
876
- let batchDeclined = false;
877
-
878
- /** Put every gated call in the batch to the user, in the order written. */
879
- async function* runBarrier() {
880
- barrierRun = true;
881
- for (const p of planned) {
882
- if (!p?.gated || decided.has(p.call.id)) continue;
883
- if (trusted?.has(`${p.toolId}.${p.action}`)) {
884
- // Waived for this conversation: runs like any allowed write, no card.
885
- decided.set(p.call.id, true);
886
- continue;
887
- }
888
- yield {
889
- type: "approval-required",
890
- id: p.call.id,
891
- toolId: p.toolId,
892
- action: p.action,
893
- label: p.label,
894
- description: p.descriptor.summary,
895
- reason: p.reason
896
- };
897
- const askedAt = Date.now();
898
- const targetDigest = await readTargetDigest({
899
- toolId: p.toolId,
900
- action: p.action,
901
- params: p.params,
902
- dispatch,
903
- catalog
904
- });
905
- const answer = readAnswer(trusted?.has(`${p.toolId}.${p.action}`) ? true : requestApproval ? await requestApproval({
906
- id: p.call.id,
907
- toolId: p.toolId,
908
- action: p.action,
909
- label: p.label,
910
- description: p.descriptor.summary,
911
- reason: p.reason,
912
- params: p.params,
913
- targetDigest
914
- }) : false);
915
- // The user's deliberation is not the agent's runtime.
916
- deadline += Date.now() - askedAt;
917
- const approved = answer.approved;
918
- decided.set(p.call.id, approved);
919
- if (answer.trust) trusted?.add(`${p.toolId}.${p.action}`);
920
- if (answer.reason) declineReasons.set(p.call.id, answer.reason);
921
- if (!approved) {
922
- // One refusal ends the PLAN, not just the call. The remaining
923
- // changes were proposed together and nothing has run yet, so
924
- // carrying on with the rest would apply half of something the user
925
- // has just turned down.
926
- batchDeclined = true;
927
- return;
928
- }
929
- }
930
- }
931
- for (const call of calls) {
932
- /**
933
- * Stop was pressed while this batch was running.
934
- *
935
- * The abort signal used to be read only at the top of a ROUND, so a
936
- * response carrying three calls ran all three after the tap — and the
937
- * bubble then said "Stopped." over two writes the user believed it had
938
- * prevented. Each call now checks before it starts. A dispatch already
939
- * in flight is left to finish (nothing can pull a store write back out
940
- * of an adapter) and keeps its receipt; everything after it is reported
941
- * as not run, each as its own row, so the transcript says exactly which
942
- * changes landed. A result is still pushed for every call — the provider
943
- * requires one per tool_use or the next request is rejected.
944
- */
945
- if (signal?.aborted) {
946
- // A `buoy_ui` card that was never shown is not a step; no row for it.
947
- if (call.name !== _uiTool.UI_TOOL_NAME) {
948
- for (const ev of stoppedStep(call)) yield ev;
949
- }
950
- results.push({
951
- toolCallId: call.id,
952
- content: "Not run — the user pressed Stop before this call started.",
953
- isError: true
954
- });
955
- continue;
956
- }
957
-
958
- // `buoy_ui`: show or ask. Validated like any action; an Ask block ends
959
- // the turn after this batch — the answer comes back as a user message.
960
- if (call.name === _uiTool.UI_TOOL_NAME) {
961
- const built = (0, _uiTool.buildUiBlock)(call.input);
962
- if (!built.ok || !built.block) {
963
- results.push({
964
- toolCallId: call.id,
965
- /**
966
- * The trailing sentence is the expensive half.
967
- *
968
- * A model that gets a block bounced rewrites its WHOLE answer on
969
- * the retry, and one assistant bubble spans every round of a turn
970
- * (see `breakBefore`), so the user reads the same paragraph again
971
- * — measured at four near-identical closings from three schema
972
- * misses in a row on one live turn. The block is wrong; the
973
- * sentence under it was fine.
974
- */
975
- content: `Invalid ${_uiTool.UI_TOOL_NAME} block:\n${built.errors.join("\n")}\n\nResend ONLY the corrected block. Do not rewrite your answer — whatever you already said this turn has been shown to the user, and saying it again repeats it on their screen.`,
976
- isError: true
977
- });
978
- continue;
979
- }
980
- // Provenance for pictures, ENFORCED: an image URL the model did not
981
- // read from a tool result this turn does not render. Live or nothing.
982
- // Dropping pictures SILENTLY is its own bug: the model then writes
983
- // "here they are, with artwork" over a card that has none, and the
984
- // user is told something untrue about their own screen. Every drop
985
- // comes back in the tool result so the next sentence can be honest.
986
- let droppedImages = 0;
987
- if (built.block.kind === "imageGrid") {
988
- const kept = built.block.images.filter(img => seenUrls.has(img.url));
989
- droppedImages = built.block.images.length - kept.length;
990
- if (kept.length === 0) {
991
- results.push({
992
- toolCallId: call.id,
993
- content: URL_PROVENANCE_REJECTION,
994
- isError: true
995
- });
996
- continue;
997
- }
998
- built.block = {
999
- ...built.block,
1000
- images: kept
1001
- };
1002
- }
1003
- if (built.block.kind === "list") {
1004
- const items = built.block.items.map(item => {
1005
- if (!item.image || seenUrls.has(item.image)) return item;
1006
- droppedImages += 1;
1007
- return {
1008
- ...item,
1009
- image: undefined
1010
- };
1011
- });
1012
- built.block = {
1013
- ...built.block,
1014
- items
1015
- };
1016
- }
1017
- yield {
1018
- type: "block",
1019
- block: built.block,
1020
- forToolCallId: call.id
1021
- };
1022
- if (built.block.kind === "actions" || built.block.kind === "suggestions") offeredTap = true;
1023
- if ((0, _types.isAskBlock)(built.block)) {
1024
- awaiting = built.block.id;
1025
- results.push({
1026
- toolCallId: call.id,
1027
- content: "Shown. Waiting for the user's answer — it will arrive as their next message. Do not guess it."
1028
- });
1029
- } else {
1030
- results.push({
1031
- toolCallId: call.id,
1032
- content: "Shown to the user. Don't repeat its contents in text." + (droppedImages > 0 ? droppedImageNote(droppedImages) : "")
1033
- });
1034
- }
1035
- continue;
1036
- }
1037
- const toolId = (0, _toProviderTools.fromProviderToolName)(call.name, catalog);
1038
- const action = call.input.action;
1039
-
1040
- /**
1041
- * A call that never reached a tool at all.
1042
- *
1043
- * Same rule as a refusal: the model made a move, the move went nowhere,
1044
- * and a transcript that hides it leaves the reader watching the agent
1045
- * change its mind for no visible reason. Caught on device — a model
1046
- * guessed three navigation tool names that do not exist, spent 41
1047
- * seconds doing it, and the only trace was a sentence it chose to write.
1048
- * `read` because nothing was touched.
1049
- */
1050
- const deadCall = (label, summary) => [{
1051
- type: "tool-start",
1052
- id: call.id,
1053
- toolId: toolId ?? call.name,
1054
- action: action ?? "",
1055
- label,
1056
- description: "",
1057
- params: call.input.params ?? {},
1058
- effect: "read"
1059
- }, {
1060
- type: "tool-end",
1061
- id: call.id,
1062
- ok: false,
1063
- summary,
1064
- durationMs: 0
1065
- }];
1066
-
1067
- // Two different mistakes, kept apart on purpose. Merging them tells a
1068
- // model that forgot `action` that the TOOL does not exist, and it stops
1069
- // reaching for a tool that was fine — the same distinction
1070
- // `dispatchToolAction` draws between an unknown tool and an unknown
1071
- // action, for the same reason.
1072
- if (!toolId) {
1073
- for (const ev of deadCall(call.name, "No such tool")) yield ev;
1074
- results.push({
1075
- toolCallId: call.id,
1076
- content: `There is no tool called "${call.name}". Use one of the tools you were given.`,
1077
- isError: true
1078
- });
1079
- continue;
1080
- }
1081
- if (!action) {
1082
- for (const ev of deadCall(call.name, "No action given")) yield ev;
1083
- results.push({
1084
- toolCallId: call.id,
1085
- content: `"${call.name}" needs an "action" — it is required, and it names which of this tool's actions to run. See the action list in the tool's description.`,
1086
- isError: true
1087
- });
1088
- continue;
1089
- }
1090
- const descriptor = findDescriptor(catalog, toolId, action);
1091
- if (!descriptor) {
1092
- // Name the alternatives. Without them a model that guessed an action
1093
- // has nowhere to go but guess again, and the next guess is no better
1094
- // informed than the last.
1095
- const known = catalog.find(t => t.toolId === toolId)?.actions.map(a => a.action).join(", ");
1096
- for (const ev of deadCall(`${toolId}.${action}`, "No such action")) yield ev;
1097
- results.push({
1098
- toolCallId: call.id,
1099
- content: `"${action}" is not an action on ${toolId}.${known ? ` Valid actions: ${known}.` : ""}`,
1100
- isError: true
1101
- });
1102
- continue;
1103
- }
1104
- const raw = call.input.params ?? {};
1105
- // Accept the parameter-name misses every model makes (queryKey for
1106
- // queryHash, requestId for id, flat rule fields, …) before validation,
1107
- // so a mechanical alias never costs a turn. See normalizeParams.ts.
1108
- const normalized = (0, _normalizeParams.normalizeParams)(toolId, action, raw);
1109
- const params = (0, _validateParams.applyDefaults)(normalized.params, descriptor.params);
1110
- const check = (0, _validateParams.validateParams)(params, descriptor.params);
1111
- if (!check.ok) {
1112
- results.push({
1113
- toolCallId: call.id,
1114
- content: `Invalid parameters for ${toolId}.${action}:\n${check.errors.join("\n")}\n\nExpected schema:\n${JSON.stringify(descriptor.params)}`,
1115
- isError: true
1116
- });
1117
- continue;
1118
- }
1119
- const aliasTrailer = (0, _normalizeParams.aliasNote)(normalized.notes);
1120
- const label = (0, _policy.describeCall)(toolId, descriptor, params);
1121
- const verdict = (0, _policy.decide)({
1122
- descriptor,
1123
- toolId,
1124
- policy,
1125
- isRelease
1126
- });
1127
-
1128
- // A step the user can SEE, for a call that never ran. `tool-end` alone
1129
- // updates a row that was never opened, so a refusal used to leave no
1130
- // trace in the transcript at all — the model simply changed its mind
1131
- // between one sentence and the next.
1132
- const unrunStep = summary => [{
1133
- type: "tool-start",
1134
- id: call.id,
1135
- toolId,
1136
- action,
1137
- label,
1138
- description: descriptor.summary,
1139
- params,
1140
- effect: descriptor.effect
1141
- }, {
1142
- type: "tool-end",
1143
- id: call.id,
1144
- ok: false,
1145
- summary,
1146
- durationMs: 0
1147
- }];
1148
- if (verdict.verdict === "refuse") {
1149
- for (const ev of unrunStep("refused")) yield ev;
1150
- results.push({
1151
- toolCallId: call.id,
1152
- content: verdict.reason,
1153
- isError: true
1154
- });
1155
- continue;
1156
- }
1157
-
1158
- /**
1159
- * Nothing in this batch changes anything until every card in it has been
1160
- * answered. See `runBarrier` — this is the line that makes a decline
1161
- * mean "the plan does not happen" rather than "the rest of the plan does
1162
- * not happen".
1163
- */
1164
- const gatedHere = verdict.verdict === "needs-approval";
1165
- if (gatedInBatch && !barrierRun && (descriptor.effect !== "read" || gatedHere)) {
1166
- yield* runBarrier();
1167
- }
1168
- if (batchDeclined && (descriptor.effect !== "read" || gatedHere)) {
1169
- for (const ev of unrunStep("declined")) yield ev;
1170
- results.push({
1171
- toolCallId: call.id,
1172
- content: decided.get(call.id) === false ? declinedResult(declineReasons.get(call.id)) : "Not run — the user declined another change in this same batch, so none of it was applied. Ask what they would prefer before proposing it again.",
1173
- isError: true
1174
- });
1175
- continue;
1176
- }
1177
- if (gatedHere) {
1178
- let approved;
1179
- if (decided.has(call.id)) {
1180
- // The barrier already put this card up, before the batch's first
1181
- // mutation. Asking again would be the same question twice.
1182
- approved = decided.get(call.id);
1183
- } else if (trusted?.has(`${toolId}.${action}`)) {
1184
- // Waived for this conversation — see RunTurnInput.trusted.
1185
- approved = true;
1186
- decided.set(call.id, true);
1187
- } else {
1188
- // The barrier could not resolve this call (see `planned`), so it was
1189
- // never offered. Ask here, exactly as this always did — a gate that
1190
- // fails closed by silently declining would be worse than one that
1191
- // asks late.
1192
- yield {
1193
- type: "approval-required",
1194
- id: call.id,
1195
- toolId,
1196
- action,
1197
- label,
1198
- description: descriptor.summary,
1199
- reason: verdict.reason
1200
- };
1201
- const askedAt = Date.now();
1202
- // What the write is about to replace, as of NOW. Only a restored
1203
- // card ever reads it back — see readTargetDigest.
1204
- const targetDigest = await readTargetDigest({
1205
- toolId,
1206
- action,
1207
- params,
1208
- dispatch,
1209
- catalog
1210
- });
1211
- const answer = readAnswer(requestApproval ? await requestApproval({
1212
- id: call.id,
1213
- toolId,
1214
- action,
1215
- label,
1216
- description: descriptor.summary,
1217
- reason: verdict.reason,
1218
- params,
1219
- targetDigest
1220
- }) : false);
1221
- // The user's deliberation is not the agent's runtime. Give the clock
1222
- // back before anything else can trip the deadline check.
1223
- deadline += Date.now() - askedAt;
1224
- approved = answer.approved;
1225
- decided.set(call.id, approved);
1226
- if (answer.trust) trusted?.add(`${toolId}.${action}`);
1227
- if (answer.reason) declineReasons.set(call.id, answer.reason);
1228
- }
1229
- if (!approved) {
1230
- // The row is the whole point here. Without it the bubble read as a
1231
- // contradiction — "Bumped the line from 5 to 9." straight into "I
1232
- // left it as it was" — with nothing on screen to say a change had
1233
- // been proposed and turned down.
1234
- for (const ev of unrunStep("declined")) yield ev;
1235
- results.push({
1236
- toolCallId: call.id,
1237
- content: declinedResult(declineReasons.get(call.id)),
1238
- isError: true
1239
- });
1240
- continue;
1241
- }
1242
- }
1243
- if (_askGate.ACQUISITION_CALLS.has(`${toolId}.${action}`)) attemptedAcquisition = true;
1244
- yield {
1245
- type: "tool-start",
1246
- id: call.id,
1247
- toolId,
1248
- action,
1249
- label,
1250
- description: descriptor.summary,
1251
- params,
1252
- effect: descriptor.effect
1253
- };
1254
- const startedAt = Date.now();
1255
-
1256
- // Capture the prior value BEFORE a storage write, so undo can restore it
1257
- // exactly. This is the one thing that makes storage reversible here when
1258
- // the Scenarios engine has to treat it as permanent.
1259
- const before = await captureBefore(descriptor, toolId, params, dispatch);
1260
- const stateBefore = await captureStoreState(descriptor, toolId, params, dispatch);
1261
-
1262
- /**
1263
- * THE FINAL GUARD. Everything above this line happened in the past.
1264
- *
1265
- * `decide()` ran before the approval card went up, and between there and
1266
- * here are three awaits: the target read, the person deciding, and two
1267
- * pre-reads. Each one yields the thread, and a person can do a lot in
1268
- * that gap — press Stop, or flip the read-only toggle in the settings
1269
- * sheet while the card is still on screen. Both were reproduced: the
1270
- * write went ahead on the verdict it captured before they touched
1271
- * anything, which is precisely the moment the product promises it will
1272
- * not. `live` is mutated in place by the session (see `refreshLive`), so
1273
- * asking again reads the toggles as they are NOW.
1274
- *
1275
- * Below this there is no `await` before `dispatch` is CALLED. That is
1276
- * load-bearing: an await here would reopen the same gap one line lower.
1277
- */
1278
- if (signal?.aborted) {
1279
- for (const ev of unrunStep("stopped")) yield ev;
1280
- results.push({
1281
- toolCallId: call.id,
1282
- content: "Not run — the user pressed Stop before this call started.",
1283
- isError: true
1284
- });
1285
- continue;
1286
- }
1287
- const stillAllowed = (0, _policy.decide)({
1288
- descriptor,
1289
- toolId,
1290
- policy,
1291
- isRelease
1292
- });
1293
- if (stillAllowed.verdict === "refuse") {
1294
- // `needs-approval` is NOT re-refused: reaching here means the tap
1295
- // already happened, and asking twice for one call is its own bug.
1296
- for (const ev of unrunStep("refused")) yield ev;
1297
- results.push({
1298
- toolCallId: call.id,
1299
- content: stillAllowed.reason,
1300
- isError: true
1301
- });
1302
- continue;
1303
- }
1304
- try {
1305
- // `getSnapshot` is reserved: it is not an adapter action, so it goes to
1306
- // the snapshot reader and is trimmed to the fields worth sending. See
1307
- // catalog/snapshotReads.ts. `ask-buoy.*` is the session itself — the
1308
- // ledger the model asks about lives HERE, not behind an adapter, so
1309
- // routing it through dispatch would only work on a device and would
1310
- // record the undo as a fresh effect.
1311
- const result = action === _snapshotReads.SNAPSHOT_ACTION ? (0, _snapshotReads.projectSnapshot)(toolId, await requireSnapshot(readSnapshot, toolId), params) : toolId === "ask-buoy" ? await runAskBuoyAction(action, ledger, dispatch, evidence, params, procedures) : await (prefetch.get(call.id) ?? dispatch(toolId, action, params));
1312
- const cleaned = (0, _redact.redact)((0, _redact.stripSelfTraffic)(result));
1313
- const refused = reportsFailure(result);
1314
-
1315
- // Kept in full — after redaction, never before — so the model can go
1316
- // back to it. Not a retrieve's own result (a retrieve of a retrieve is
1317
- // a loop), and not a refusal (nothing to go back to).
1318
- const text = fullText(cleaned);
1319
- const ref = evidence && !refused && !(toolId === "ask-buoy" && action === "retrieve") ? evidence.stash({
1320
- toolId,
1321
- action,
1322
- params,
1323
- capturedAt: Date.now(),
1324
- text
1325
- }) : undefined;
1326
- const encodedResult = encodeResult(text, ref);
1327
- const traceResult = encodedResult.length > MAX_TRACE_RESULT_CHARS ? `${encodedResult.slice(0, MAX_TRACE_RESULT_CHARS)}…` : encodedResult;
1328
- yield {
1329
- type: "tool-end",
1330
- id: call.id,
1331
- ok: !refused,
1332
- summary: summarise(result),
1333
- durationMs: Date.now() - startedAt,
1334
- result: traceResult
1335
- };
1336
-
1337
- // The receipt: what the user sees of this result, built from the
1338
- // same redacted data the model gets. See blocks/receipts.ts.
1339
- //
1340
- // The store is read back a second time so the diff shows what the
1341
- // write ACTUALLY did rather than what it asked for — the two differ on
1342
- // every `path` edit and every keyed list edit, and the request-shaped
1343
- // version reported untouched fields as deleted.
1344
- const stateAfter = stateBefore === undefined || refused ? undefined : await captureStoreState(descriptor, toolId, params, dispatch);
1345
- if (descriptor.effect !== "read" && toolId !== "ask-buoy" && !refused) {
1346
- // A delete of something we created IS the undo, whoever asked for
1347
- // it — mark the matching entry undone instead of leaving the bar
1348
- // promising an undo against a rule that no longer exists.
1349
- ledger.noteExternalRevert(toolId, action, params);
1350
- // Recorded AFTER the read-back so a cache edit's entry can carry both
1351
- // halves: the data to restore and a fingerprint of what the write
1352
- // left behind, which is how undo knows whether the app has since
1353
- // replaced it. See effectFor / ledger "query-write".
1354
- const fx = (0, _effectFor.effectFor)(toolId, descriptor, params, result, before, {
1355
- before: stateBefore,
1356
- after: stateAfter
1357
- });
1358
- if (fx) {
1359
- const entry = ledger.record(toolId, action, fx, ledgerGeneration);
1360
- // Ties the change to its round, so the budget keeps that round
1361
- // while the change is live. See LedgerEntry.callId.
1362
- if (entry) entry.callId = call.id;
1363
- }
1364
- }
1365
- // No receipt for a call that changed nothing: a "what changed" card
1366
- // under a refusal is the same lie as the ledger entry, drawn bigger.
1367
- const receipt = refused ? undefined : (0, _receipts.projectReceipt)({
1368
- toolId,
1369
- action,
1370
- params,
1371
- result: cleaned,
1372
- before: stateBefore ?? before,
1373
- after: stateAfter,
1374
- effect: descriptor.effect
1375
- });
1376
- if (receipt) yield {
1377
- type: "block",
1378
- block: receipt,
1379
- forToolCallId: call.id
1380
- };
1381
- /**
1382
- * THE OUTCOME CHECK. A write's own `{ok:true}` says the adapter
1383
- * accepted it; this reads the app back and says whether the requested
1384
- * state is actually there. Only for actions that have a verifier, only
1385
- * for writes that were not refused, and never itself a write. Its
1386
- * verdict rides in the tool result as a trailer the model reads, and
1387
- * on a second tool-end so the row says "verified" / "unverified" /
1388
- * "check failed". See engine/verify.ts for the three meanings.
1389
- */
1390
- const verification = descriptor.effect !== "read" && !refused ? await (0, _verify.verifyOutcome)({
1391
- toolId,
1392
- action,
1393
- params,
1394
- result,
1395
- dispatch,
1396
- signal,
1397
- after: stateAfter
1398
- }) : undefined;
1399
- if (verification) yield {
1400
- type: "tool-verified",
1401
- id: call.id,
1402
- verification
1403
- };
1404
- for (const url of encodedResult.match(URL_RE) ?? []) seenUrls.add(url);
1405
- results.push({
1406
- toolCallId: call.id,
1407
- content: encodedResult + ownedKeyNote(toolId, descriptor, params, storeKeyOwners) + aliasTrailer + (verification ? (0, _verify.verificationTrailer)(verification) : "") + (salvagedIds.has(call.id) ? _textToolCalls.SALVAGE_NOTE : ""),
1408
- ...(ref ? {
1409
- ref
1410
- } : {})
1411
- });
1412
- if (toolId === "app" && (action === "reloadApp" || action === "reload")) {
1413
- realmWillDie = true;
1414
- }
1415
- } catch (err) {
1416
- const message = err instanceof Error ? err.message : String(err);
1417
- yield {
1418
- type: "tool-end",
1419
- id: call.id,
1420
- ok: false,
1421
- summary: message,
1422
- durationMs: Date.now() - startedAt
1423
- };
1424
- results.push({
1425
- toolCallId: call.id,
1426
- content: `Failed: ${message}`,
1427
- isError: true
1428
- });
1429
- }
1430
- }
1431
-
1432
- // Looping? Say so, once, on the LAST result of this round — so it arrives
1433
- // attached to the thing being repeated rather than as a free-floating
1434
- // user turn, and the model reads it before deciding what to do next.
1435
- if (calls.length) {
1436
- recentSignatures.push(callSignature(calls));
1437
- if (recentSignatures.length > LOOP_REPEATS) recentSignatures.shift();
1438
- const looping = !loopWarned && recentSignatures.length === LOOP_REPEATS && recentSignatures.every(sig => sig === recentSignatures[0]);
1439
- if (looping) {
1440
- loopWarned = true;
1441
- const last = results[results.length - 1];
1442
- if (last) last.content = `${last.content}\n\n${LOOP_WARNING}`;
1443
- }
1444
- }
1445
- messages.push({
1446
- role: "tool-results",
1447
- results
1448
- });
1449
- if (awaiting) {
1450
- yield {
1451
- type: "awaiting-user",
1452
- blockId: awaiting
1453
- };
1454
- yield {
1455
- type: "done",
1456
- stopReason: "awaiting-user"
1457
- };
1458
- return messages;
1459
- }
1460
- if (realmWillDie) {
1461
- // Anything after this dies with the JS realm. Stop cleanly instead of
1462
- // sending a request whose answer can never arrive.
1463
- yield {
1464
- type: "done",
1465
- stopReason: "realm-died"
1466
- };
1467
- return messages;
1468
- }
1469
- }
1470
- yield {
1471
- type: "block",
1472
- block: {
1473
- id: `cap${Date.now()}`,
1474
- kind: "notice",
1475
- tone: "warning",
1476
- text: `Stopped after ${maxSteps} steps to avoid looping.`,
1477
- actions: [{
1478
- label: "Continue",
1479
- primary: true,
1480
- send: "Continue where you left off."
1481
- }]
1482
- }
1483
- };
1484
- yield {
1485
- type: "done",
1486
- stopReason: "step-cap"
1487
- };
1488
- return messages;
1489
- }
1490
-
1491
- /**
1492
- * A factual postscript for a storage write that landed on a store's saved copy.
1493
- *
1494
- * This is a STATEMENT ABOUT WHAT HAPPENED, not a policy reminder — the same
1495
- * kind of thing the release refusals say, and the reason it belongs in the
1496
- * result rather than the system prompt. The write really did succeed and the
1497
- * app really did not change, and only the engine is in a position to know
1498
- * both at once.
1499
- *
1500
- * Deliberately not a refusal. Writing the on-disk value on purpose — "make it
1501
- * this on next launch" — is legitimate, and a hard block would make that
1502
- * impossible. Saying so leaves the model free to continue, correct itself with
1503
- * `zustand.rehydrate`, or explain the distinction to the user.
1504
- */
1505
- function ownedKeyNote(toolId, descriptor, params, storeKeyOwners) {
1506
- if (toolId !== "storage" || descriptor.effect === "read") return "";
1507
- if (!storeKeyOwners || typeof params.key !== "string") return "";
1508
- const storeName = storeKeyOwners[params.key];
1509
- if (!storeName) return "";
1510
- return `\n\n[Buoy] This wrote to disk, but "${params.key}" is the saved copy of the live "${storeName}" store, so nothing on screen has changed — the app read that key once at startup and has held the state in memory since. Call zustand.rehydrate({"storeName":"${storeName}"}) to make the app pick this up, or use zustand.setState next time to change the store directly. If you meant to set the value for the app's next launch, this is already done and no further action is needed.`;
1511
- }
1512
- /**
1513
- * Fingerprint the value a write is about to replace.
1514
- *
1515
- * Built for the approval card that outlives its turn: the app crashed with
1516
- * "Set bag line L-01 qty to 14" still asking, relaunched, and the card came
1517
- * back — and Allow wrote qty 14 against whatever the bag was NOW, maybe a
1518
- * different account, maybe a line that no longer existed. A hash of the
1519
- * params alone would not catch that (same label, same params, different
1520
- * world). So the card carries a digest of the TARGET at ask time, and a
1521
- * restored Allow re-reads and compares before it writes. Undefined when the
1522
- * target cannot be read — then the card can only warn, not check.
1523
- */
1524
- async function readTargetDigest(input) {
1525
- const descriptor = findDescriptor(input.catalog ?? _catalog.CATALOG, input.toolId, input.action);
1526
- if (!descriptor || descriptor.effect === "read") return undefined;
1527
- const store = await captureStoreState(descriptor, input.toolId, input.params, input.dispatch);
1528
- if (store !== undefined) return (0, _digest.digestOf)(store);
1529
- const before = await captureBefore(descriptor, input.toolId, input.params, input.dispatch);
1530
- if (before !== undefined) return (0, _digest.digestOf)(before);
1531
- return undefined;
1532
- }
1533
- /**
1534
- * One human-initiated action through EVERY gate the model's calls go through:
1535
- * catalog lookup, param validation, policy, pre-read, effect ledger.
1536
- *
1537
- * This exists for block buttons. The `buoy_ui` schema promises the model that
1538
- * a dispatch button runs "with the same policy and undo as if you called it",
1539
- * and the first implementation called `dispatchToolAction` raw — no deny list,
1540
- * no release refusal, no ledger entry. One tap on a model-authored button was
1541
- * an invisible, ungoverned, unundoable mutation. Route every button through
1542
- * here so the promise is true.
1543
- *
1544
- * `needs-approval` is treated as approved: the human's tap on the button IS
1545
- * the approval. Deny lists, read-only mode and release refusals still stand —
1546
- * a button must never reach further than the model could.
1547
- */
1548
- async function runGatedAction(input) {
1549
- const {
1550
- toolId,
1551
- action,
1552
- dispatch,
1553
- ledger,
1554
- policy,
1555
- isRelease
1556
- } = input;
1557
- const catalog = input.catalog ?? _catalog.CATALOG;
1558
- const descriptor = findDescriptor(catalog, toolId, action);
1559
- if (!descriptor) {
1560
- return {
1561
- ok: false,
1562
- error: `${toolId}.${action} is not in Buoy's catalog.`
1563
- };
1564
- }
1565
- const normalized = (0, _normalizeParams.normalizeParams)(toolId, action, input.params ?? {});
1566
- const params = (0, _validateParams.applyDefaults)(normalized.params, descriptor.params);
1567
- const check = (0, _validateParams.validateParams)(params, descriptor.params);
1568
- if (!check.ok) {
1569
- return {
1570
- ok: false,
1571
- error: check.errors.join("\n")
1572
- };
1573
- }
1574
- const verdict = (0, _policy.decide)({
1575
- descriptor,
1576
- toolId,
1577
- policy,
1578
- isRelease
1579
- });
1580
- if (verdict.verdict === "refuse") {
1581
- return {
1582
- ok: false,
1583
- error: verdict.reason
1584
- };
1585
- }
1586
- const before = await captureBefore(descriptor, toolId, params, dispatch);
1587
- /**
1588
- * The same read-back the model's path takes. It was missing here, and the
1589
- * gap was invisible: a `setQueryData` through a block button or a restored
1590
- * approval card recorded a ledger entry with no prior value, so Undo had
1591
- * nothing to put back and the concurrent-writer check had no fingerprint to
1592
- * compare. The bar said "1 undoable" over a change that could not be undone.
1593
- */
1594
- const stateBefore = await captureStoreState(descriptor, toolId, params, dispatch);
1595
-
1596
- // Asked again after the pre-reads, for the same reason the model's path asks
1597
- // again: those are awaits, and read-only can be switched on inside one.
1598
- const stillAllowed = (0, _policy.decide)({
1599
- descriptor,
1600
- toolId,
1601
- policy,
1602
- isRelease
1603
- });
1604
- if (stillAllowed.verdict === "refuse") {
1605
- return {
1606
- ok: false,
1607
- error: stillAllowed.reason
1608
- };
1609
- }
1610
- try {
1611
- const result = toolId === "ask-buoy" ? await runAskBuoyAction(action, ledger, dispatch, undefined, params) : await dispatch(toolId, action, params);
1612
-
1613
- // Same rule as the model's path: a resolved `{ok:false}` is a refusal, not
1614
- // a change. Without this a block button (or an approval answered after a
1615
- // reload) put a phantom entry in the undo bar too.
1616
- if (reportsFailure(result)) {
1617
- const err = result.error;
1618
- return {
1619
- ok: false,
1620
- error: typeof err === "string" ? err : "The tool refused this."
1621
- };
1622
- }
1623
- if (descriptor.effect !== "read" && toolId !== "ask-buoy") {
1624
- ledger.noteExternalRevert(toolId, action, params);
1625
- // Read back AFTER the write, exactly as the model's path does — the two
1626
- // halves together are what make a cache edit undoable.
1627
- const stateAfter = await captureStoreState(descriptor, toolId, params, dispatch);
1628
- const fx = (0, _effectFor.effectFor)(toolId, descriptor, params, result, before, {
1629
- before: stateBefore,
1630
- after: stateAfter
1631
- });
1632
- if (fx) ledger.record(toolId, action, fx);
1633
- }
1634
- return {
1635
- ok: true,
1636
- result
1637
- };
1638
- } catch (err) {
1639
- return {
1640
- ok: false,
1641
- error: err instanceof Error ? err.message : String(err)
1642
- };
1643
- }
1644
- }
1645
-
1646
- /**
1647
- * The `ask-buoy` catalog tool, served by the engine itself. `listChanges` and
1648
- * `undoAll` operate on THIS session's ledger — the same one the changes bar
1649
- * renders — so the model's undo and the human's undo are one mechanism. The
1650
- * adapter's same-named actions remain for desktop/MCP, where the ledger is
1651
- * reached over the broker instead.
1652
- */
1653
- async function runAskBuoyAction(action, ledger, dispatch, evidence, params, procedures) {
1654
- if (action === "retrieve") {
1655
- return (0, _retrieve.retrieveEvidence)(evidence, params ?? {});
1656
- }
1657
- if (action === "openProcedure") {
1658
- const id = typeof params?.id === "string" ? params.id : "";
1659
- const found = procedures?.find(p => p.id === id);
1660
- if (!found) {
1661
- const ids = (procedures ?? []).map(p => p.id);
1662
- return {
1663
- ok: false,
1664
- error: ids.length ? `No procedure "${id}". This app's procedures: ${ids.join(", ")}.` : "This app has no procedures written for it."
1665
- };
1666
- }
1667
- return {
1668
- id: found.id,
1669
- title: found.title,
1670
- ...(found.version ? {
1671
- version: found.version
1672
- } : {}),
1673
- ...(found.requires?.length ? {
1674
- requires: found.requires
1675
- } : {}),
1676
- body: found.body,
1677
- note: "Follow these steps with the ordinary tools. Every write still goes through the same policy, approval and undo as any other call; a step that is refused stays refused."
1678
- };
1679
- }
1680
- if (action === "listChanges") {
1681
- const changes = ledger.list().filter(e => !e.undoneAt).map(e => ({
1682
- toolId: e.toolId,
1683
- action: e.action,
1684
- kind: e.effect.kind,
1685
- label: e.effect.label,
1686
- reversible: (0, _ledger.isReversible)(e.effect)
1687
- }));
1688
- return {
1689
- changes,
1690
- returned: changes.length
1691
- };
1692
- }
1693
- if (action === "undoAll") {
1694
- const outcome = await ledger.revertAll(dispatch);
1695
- return {
1696
- ok: outcome.failed.length === 0,
1697
- ...outcome
1698
- };
1699
- }
1700
- throw new Error(`Unknown ask-buoy action "${action}"`);
1701
- }
1702
-
1703
- /** Fail loudly rather than hand the model an empty object it will believe. */
1704
- async function requireSnapshot(readSnapshot, toolId) {
1705
- if (!readSnapshot) {
1706
- throw new Error(`This app cannot read ${toolId}'s snapshot — Buoy's tool registry is not published here.`);
1707
- }
1708
- return await readSnapshot(toolId);
1709
- }
1710
-
1711
- /**
1712
- * Read a storage key's current value so the write can be reversed.
1713
- *
1714
- * This pre-read is the ONLY reason storage is reversible here. The adapter's
1715
- * `async.*` / `mmkv.*` proxies all return void, so neither the prior value nor
1716
- * the store event id that `timeTravel.undo` needs ever reaches a caller — and
1717
- * that event only exists while something holds capture open. Reading first is
1718
- * what turns "we don't know what was there" into a real undo.
1719
- *
1720
- * Deliberately NOT gated on `descriptor.undo`: the catalog's undo claims were
1721
- * audited down to almost nothing precisely because they were unsound. The
1722
- * ledger's reversibility comes from this read, not from a catalog promise.
1723
- */
1724
- /**
1725
- * The prior state for a store write, so the receipt can show a real diff.
1726
- * Zustand only for now: `getStoreState` is cheap and the write params carry
1727
- * the after-state, so the diff is exact without the store's cooperation.
1728
- */
1729
- /**
1730
- * Read a zustand store whole, for the before/after halves of a write receipt.
1731
- * Called on both sides of the dispatch — see the receipt above.
1732
- */
1733
- async function captureStoreState(descriptor, toolId, params, dispatch) {
1734
- if (toolId === "zustand" && descriptor.action === "setState") {
1735
- if (typeof params.storeName !== "string") return undefined;
1736
- try {
1737
- return await dispatch(toolId, "getStoreState", {
1738
- storeName: params.storeName
1739
- });
1740
- } catch {
1741
- return undefined;
1742
- }
1743
- }
1744
- /**
1745
- * The same read-both-sides trick for the CACHE.
1746
- *
1747
- * A zustand edit produced a "what changed" card and the identical edit to a
1748
- * query produced nothing — for the write the prompt calls "THE way to change
1749
- * what a server-backed screen shows". The QA tester's confirmation that the
1750
- * right field moved was missing from the most-used tool in the product.
1751
- */
1752
- if (toolId === "query" && descriptor.action === "setQueryData") {
1753
- // hashQueryKey, not JSON.stringify: React Query SORTS object keys when it
1754
- // hashes, so a key carrying `{query:"",category:null}` hashes as
1755
- // `{"category":null,"query":""}`. Stringifying in insertion order produced
1756
- // a hash that matched nothing, the read came back empty, and the card
1757
- // silently did not draw.
1758
- const queryHash = typeof params.queryHash === "string" ? params.queryHash : Array.isArray(params.queryKey) ? (0, _normalizeParams.hashQueryKey)(params.queryKey) : undefined;
1759
- if (!queryHash) return undefined;
1760
- try {
1761
- return await dispatch(toolId, "getQueryData", {
1762
- queryHash
1763
- });
1764
- } catch {
1765
- return undefined;
1766
- }
1767
- }
1768
- return undefined;
1769
- }
1770
- async function captureBefore(descriptor, toolId, params, dispatch) {
1771
- if (descriptor.effect === "read") return undefined;
1772
- if (toolId !== "storage" || typeof params.key !== "string") return undefined;
1773
- const readAction = descriptor.action.startsWith("async.") ? "async.getItem" : descriptor.action.startsWith("mmkv.") ? "mmkv.get" : undefined;
1774
- // secure.* is excluded on purpose: a key registered with
1775
- // `requireAuthentication` cannot be read without a biometric prompt, so a
1776
- // pre-read would either fail or pop a Face ID sheet mid-conversation.
1777
- if (!readAction) return undefined;
1778
-
1779
- // MMKV IS PER-INSTANCE, and `mmkv.get` does not throw when the id is missing
1780
- // — it returns `{found:false, reason:"missing instanceId/key"}`. Reading with
1781
- // the key alone therefore produced a `before` that LOOKED captured and was
1782
- // empty, which the ledger then read as "the key did not exist" and undid by
1783
- // deleting it. Without an instance there is nothing to capture, so say so.
1784
- const readParams = {
1785
- key: params.key
1786
- };
1787
- if (readAction === "mmkv.get") {
1788
- if (typeof params.instanceId !== "string") return undefined;
1789
- readParams.instanceId = params.instanceId;
1790
- }
1791
- try {
1792
- const current = await dispatch(toolId, readAction, readParams);
1793
- const rec = current && typeof current === "object" ? current : undefined;
1794
- // An explicit "I could not read that" is not a captured empty value.
1795
- if (rec?.found === false) return undefined;
1796
- const value = rec ? rec.value : current;
1797
- return {
1798
- had: value !== undefined && value !== null,
1799
- value: value ?? undefined
1800
- };
1801
- } catch {
1802
- // A failed pre-read must not block the write the user asked for; it just
1803
- // means this one is not reversible, which the ledger will say.
1804
- return undefined;
1805
- }
1806
- }
1807
- //# sourceMappingURL=runAgentTurn.js.map
12
+ [Buoy] This wrote to disk, but "${n.key}" is the saved copy of the live "${c}" store, so nothing on screen has changed \u2014 the app read that key once at startup and has held the state in memory since. Call zustand.rehydrate({"storeName":"${c}"}) to make the app pick this up, or use zustand.setState next time to change the store directly. If you meant to set the value for the app's next launch, this is already done and no further action is needed.`}async function readTargetDigest(t){const r=findDescriptor(t.catalog??_catalog.CATALOG,t.toolId,t.action);if(!r||r.effect==="read")return void 0;const n=await captureStoreState(r,t.toolId,t.params,t.dispatch);if(n!==void 0)return(0,_digest.digestOf)(n);const l=await captureBefore(r,t.toolId,t.params,t.dispatch);if(l!==void 0)return(0,_digest.digestOf)(l);return void 0}async function runGatedAction(t){const{toolId:r,action:n,dispatch:l,ledger:c,policy:b,isRelease:m}=t;const u=t.catalog??_catalog.CATALOG;const y=findDescriptor(u,r,n);if(!y){return{ok:false,error:`${r}.${n} is not in Buoy's catalog.`}}const H=(0,_normalizeParams.normalizeParams)(r,n,t.params??{});const v=(0,_validateParams.applyDefaults)(H.params,y.params);const q=(0,_validateParams.validateParams)(v,y.params);if(!q.ok){return{ok:false,error:q.errors.join("\n")}}const P=(0,_policy.decide)({descriptor:y,toolId:r,policy:b,isRelease:m});if(P.verdict==="refuse"){return{ok:false,error:P.reason}}const L=await captureBefore(y,r,v,l);const E=await captureStoreState(y,r,v,l);const U=(0,_policy.decide)({descriptor:y,toolId:r,policy:b,isRelease:m});if(U.verdict==="refuse"){return{ok:false,error:U.reason}}try{const w=r==="ask-buoy"?await runAskBuoyAction(n,c,l,void 0,v):await l(r,n,v);if(reportsFailure(w)){const D=w.error;return{ok:false,error:typeof D==="string"?D:"The tool refused this."}}if(y.effect!=="read"&&r!=="ask-buoy"){const D=c.noteExternalRevert(r,n,v);const j=await captureStoreState(y,r,v,l);const $=(0,_effectFor.effectFor)(r,y,v,w,L,{before:E,after:j});if($&&!D)c.record(r,n,$)}return{ok:true,result:w}}catch(w){return{ok:false,error:w instanceof Error?w.message:String(w)}}}async function runAskBuoyAction(t,r,n,l,c,b){if(t==="retrieve"){return(0,_retrieve.retrieveEvidence)(l,c??{})}if(t==="openProcedure"){const m=typeof c?.id==="string"?c.id:"";const u=b?.find(y=>y.id===m);if(!u){const y=(b??[]).map(H=>H.id);return{ok:false,error:y.length?`No procedure "${m}". This app's procedures: ${y.join(", ")}.`:"This app has no procedures written for it."}}return{id:u.id,title:u.title,...u.version?{version:u.version}:{},...u.requires?.length?{requires:u.requires}:{},body:u.body,note:"Follow these steps with the ordinary tools. Every write still goes through the same policy, approval and undo as any other call; a step that is refused stays refused."}}if(t==="listChanges"){const m=r.list().filter(u=>!u.undoneAt).map(u=>({toolId:u.toolId,action:u.action,kind:u.effect.kind,label:u.effect.label,reversible:(0,_ledger.isReversible)(u.effect)}));return{changes:m,returned:m.length}}if(t==="undoAll"){const m=await r.revertAll(n);return{ok:m.failed.length===0,...m}}throw new Error(`Unknown ask-buoy action "${t}"`)}async function requireSnapshot(t,r){if(!t){throw new Error(`This app cannot read ${r}'s snapshot \u2014 Buoy's tool registry is not published here.`)}return await t(r)}async function captureStoreState(t,r,n,l){if(r==="zustand"&&t.action==="setState"){if(typeof n.storeName!=="string")return void 0;try{return await l(r,"getStoreState",{storeName:n.storeName})}catch{return void 0}}if(r==="query"&&t.action==="setQueryData"){const c=typeof n.queryHash==="string"?n.queryHash:Array.isArray(n.queryKey)?(0,_normalizeParams.hashQueryKey)(n.queryKey):void 0;if(!c)return void 0;try{return await l(r,"getQueryData",{queryHash:c})}catch{return void 0}}return void 0}async function captureBefore(t,r,n,l){if(t.effect==="read")return void 0;if(r==="network")return captureRuleSwitch(t,n,l);if(r!=="storage"||typeof n.key!=="string")return void 0;const c=t.action.startsWith("async.")?"async.getItem":t.action.startsWith("mmkv.")?"mmkv.get":void 0;if(!c)return void 0;const b={key:n.key};if(c==="mmkv.get"){if(typeof n.instanceId!=="string")return void 0;b.instanceId=n.instanceId}try{const m=await l(r,c,b);const u=m&&typeof m==="object"?m:void 0;if(u?.found===false)return void 0;const y=u?u.value:m;return{had:y!==void 0&&y!==null,value:y??void 0}}catch{return void 0}}async function captureRuleSwitch(t,r,n){if(t.action!=="setOverrideRuleEnabled"&&t.action!=="setOverridesEnabled"){return void 0}try{const l=await n("network","listOverrideRules",{});if(!l||typeof l!=="object")return void 0;if(t.action==="setOverridesEnabled"){return{had:true,value:String(l.enabled!==false)}}const c=(l.rules??[]).find(b=>b&&b.id===r.id);if(!c)return void 0;return{had:true,value:String(c.enabled!==false)}}catch{return void 0}}