@nathapp/nax-agent 0.1.0

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 (398) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +21 -0
  3. package/README.md +49 -0
  4. package/dist/coding-tools/coding-tool-bash.d.ts +46 -0
  5. package/dist/coding-tools/coding-tool-bash.js +44 -0
  6. package/dist/coding-tools/coding-tool-extras.d.ts +35 -0
  7. package/dist/coding-tools/coding-tool-extras.js +56 -0
  8. package/dist/coding-tools/coding-tool-sandbox.d.ts +74 -0
  9. package/dist/coding-tools/coding-tool-sandbox.js +215 -0
  10. package/dist/coding-tools/coding-tool-support.d.ts +132 -0
  11. package/dist/coding-tools/coding-tool-support.js +159 -0
  12. package/dist/coding-tools/universal-coding-tools.d.ts +11 -0
  13. package/dist/coding-tools/universal-coding-tools.js +5 -0
  14. package/dist/command-interceptor/index.d.ts +55 -0
  15. package/dist/command-interceptor/index.js +60 -0
  16. package/dist/command-interceptor/shell.d.ts +36 -0
  17. package/dist/command-interceptor/shell.js +137 -0
  18. package/dist/command-safety/build.d.ts +19 -0
  19. package/dist/command-safety/build.js +47 -0
  20. package/dist/command-safety/guard.d.ts +32 -0
  21. package/dist/command-safety/guard.js +179 -0
  22. package/dist/command-safety/identifiers.d.ts +24 -0
  23. package/dist/command-safety/identifiers.js +20 -0
  24. package/dist/command-safety/index.d.ts +17 -0
  25. package/dist/command-safety/index.js +11 -0
  26. package/dist/command-safety/questions.d.ts +19 -0
  27. package/dist/command-safety/questions.js +60 -0
  28. package/dist/command-safety/row.d.ts +2 -0
  29. package/dist/command-safety/row.js +14 -0
  30. package/dist/command-safety/rule-scorer.d.ts +9 -0
  31. package/dist/command-safety/rule-scorer.js +391 -0
  32. package/dist/command-safety/shadow.d.ts +33 -0
  33. package/dist/command-safety/shadow.js +188 -0
  34. package/dist/command-safety/systemone-client.d.ts +16 -0
  35. package/dist/command-safety/systemone-client.js +92 -0
  36. package/dist/command-safety/tap.d.ts +37 -0
  37. package/dist/command-safety/tap.js +77 -0
  38. package/dist/command-safety/temp-only.d.ts +6 -0
  39. package/dist/command-safety/temp-only.js +215 -0
  40. package/dist/command-safety/tmp-write.d.ts +2 -0
  41. package/dist/command-safety/tmp-write.js +112 -0
  42. package/dist/command-safety/types.d.ts +185 -0
  43. package/dist/command-safety/types.js +21 -0
  44. package/dist/config/bash-approval.d.ts +33 -0
  45. package/dist/config/bash-approval.js +30 -0
  46. package/dist/config/catalog-overrides.d.ts +94 -0
  47. package/dist/config/catalog-overrides.js +7 -0
  48. package/dist/config/native-agent/index.d.ts +8 -0
  49. package/dist/config/native-agent/index.js +8 -0
  50. package/dist/config/schemas-sandbox.d.ts +31 -0
  51. package/dist/config/schemas-sandbox.js +45 -0
  52. package/dist/cost/core/index.d.ts +8 -0
  53. package/dist/cost/core/index.js +7 -0
  54. package/dist/cost/estimate.d.ts +34 -0
  55. package/dist/cost/estimate.js +65 -0
  56. package/dist/cost/model-spec.d.ts +28 -0
  57. package/dist/cost/model-spec.js +29 -0
  58. package/dist/cost/standard-types.d.ts +9 -0
  59. package/dist/cost/standard-types.js +1 -0
  60. package/dist/cost/usage-math.d.ts +18 -0
  61. package/dist/cost/usage-math.js +46 -0
  62. package/dist/index.d.ts +26 -0
  63. package/dist/index.js +21 -0
  64. package/dist/infra/agent-logger.d.ts +22 -0
  65. package/dist/infra/agent-logger.js +19 -0
  66. package/dist/infra/credentials-config.d.ts +20 -0
  67. package/dist/infra/credentials-config.js +15 -0
  68. package/dist/infra/errors.d.ts +9 -0
  69. package/dist/infra/errors.js +11 -0
  70. package/dist/infra/index.d.ts +7 -0
  71. package/dist/infra/index.js +7 -0
  72. package/dist/infra/nax-error.d.ts +10 -0
  73. package/dist/infra/nax-error.js +16 -0
  74. package/dist/infra/spin-breaker/hash.d.ts +2 -0
  75. package/dist/infra/spin-breaker/hash.js +5 -0
  76. package/dist/infra/spin-breaker/index.d.ts +120 -0
  77. package/dist/infra/spin-breaker/index.js +340 -0
  78. package/dist/internal/agent-output-env.d.ts +44 -0
  79. package/dist/internal/agent-output-env.js +57 -0
  80. package/dist/internal/argv-exec.d.ts +58 -0
  81. package/dist/internal/argv-exec.js +224 -0
  82. package/dist/internal/bounded-io.d.ts +24 -0
  83. package/dist/internal/bounded-io.js +51 -0
  84. package/dist/internal/command-spec/index.d.ts +29 -0
  85. package/dist/internal/command-spec/index.js +36 -0
  86. package/dist/internal/describe-value-type.d.ts +8 -0
  87. package/dist/internal/describe-value-type.js +14 -0
  88. package/dist/internal/exec-framing.d.ts +18 -0
  89. package/dist/internal/exec-framing.js +22 -0
  90. package/dist/internal/file-lock.d.ts +91 -0
  91. package/dist/internal/file-lock.js +212 -0
  92. package/dist/internal/file-size.d.ts +2 -0
  93. package/dist/internal/file-size.js +12 -0
  94. package/dist/internal/git-add.d.ts +43 -0
  95. package/dist/internal/git-add.js +59 -0
  96. package/dist/internal/git-env.d.ts +59 -0
  97. package/dist/internal/git-env.js +93 -0
  98. package/dist/internal/git-exec.d.ts +58 -0
  99. package/dist/internal/git-exec.js +115 -0
  100. package/dist/internal/path-file-lock.d.ts +23 -0
  101. package/dist/internal/path-file-lock.js +25 -0
  102. package/dist/internal/process-alive.d.ts +42 -0
  103. package/dist/internal/process-alive.js +56 -0
  104. package/dist/internal/process-kill.d.ts +27 -0
  105. package/dist/internal/process-kill.js +54 -0
  106. package/dist/internal/realpath.d.ts +29 -0
  107. package/dist/internal/realpath.js +52 -0
  108. package/dist/internal/redact.d.ts +40 -0
  109. package/dist/internal/redact.js +161 -0
  110. package/dist/internal/shell-quote.d.ts +2 -0
  111. package/dist/internal/shell-quote.js +4 -0
  112. package/dist/internal/sort.d.ts +20 -0
  113. package/dist/internal/sort.js +20 -0
  114. package/dist/internal/strip-control-chars.d.ts +19 -0
  115. package/dist/internal/strip-control-chars.js +38 -0
  116. package/dist/internal/thenable.d.ts +10 -0
  117. package/dist/internal/thenable.js +15 -0
  118. package/dist/internal.d.ts +91 -0
  119. package/dist/internal.js +91 -0
  120. package/dist/native/adapter-deps.d.ts +46 -0
  121. package/dist/native/adapter-deps.js +43 -0
  122. package/dist/native/auth-types.d.ts +70 -0
  123. package/dist/native/auth-types.js +13 -0
  124. package/dist/native/auth.d.ts +133 -0
  125. package/dist/native/auth.js +362 -0
  126. package/dist/native/client.d.ts +50 -0
  127. package/dist/native/client.js +174 -0
  128. package/dist/native/complete.d.ts +33 -0
  129. package/dist/native/complete.js +88 -0
  130. package/dist/native/credentials/chained-store.d.ts +30 -0
  131. package/dist/native/credentials/chained-store.js +68 -0
  132. package/dist/native/credentials/change-guard.d.ts +51 -0
  133. package/dist/native/credentials/change-guard.js +136 -0
  134. package/dist/native/credentials/exec-source.d.ts +50 -0
  135. package/dist/native/credentials/exec-source.js +222 -0
  136. package/dist/native/credentials/fingerprint.d.ts +26 -0
  137. package/dist/native/credentials/fingerprint.js +174 -0
  138. package/dist/native/credentials/helper-process.d.ts +79 -0
  139. package/dist/native/credentials/helper-process.js +259 -0
  140. package/dist/native/credentials/helper-protocol.d.ts +36 -0
  141. package/dist/native/credentials/helper-protocol.js +79 -0
  142. package/dist/native/credentials/index.d.ts +60 -0
  143. package/dist/native/credentials/index.js +184 -0
  144. package/dist/native/errors.d.ts +34 -0
  145. package/dist/native/errors.js +109 -0
  146. package/dist/native/index.d.ts +25 -0
  147. package/dist/native/index.js +23 -0
  148. package/dist/native/model-resolver.d.ts +57 -0
  149. package/dist/native/model-resolver.js +73 -0
  150. package/dist/native/models.d.ts +116 -0
  151. package/dist/native/models.js +183 -0
  152. package/dist/native/session/ask-human.d.ts +15 -0
  153. package/dist/native/session/ask-human.js +22 -0
  154. package/dist/native/session/compaction.d.ts +94 -0
  155. package/dist/native/session/compaction.js +170 -0
  156. package/dist/native/session/handle-invalid-tool-call.d.ts +111 -0
  157. package/dist/native/session/handle-invalid-tool-call.js +159 -0
  158. package/dist/native/session/loop-events/cache-boundary.d.ts +11 -0
  159. package/dist/native/session/loop-events/cache-boundary.js +29 -0
  160. package/dist/native/session/loop-events/external-handler.d.ts +38 -0
  161. package/dist/native/session/loop-events/external-handler.js +193 -0
  162. package/dist/native/session/loop-events/index.d.ts +16 -0
  163. package/dist/native/session/loop-events/index.js +15 -0
  164. package/dist/native/session/loop-events/payload-guard.d.ts +9 -0
  165. package/dist/native/session/loop-events/payload-guard.js +31 -0
  166. package/dist/native/session/loop-events/registry.d.ts +84 -0
  167. package/dist/native/session/loop-events/registry.js +305 -0
  168. package/dist/native/session/loop-events/types.d.ts +251 -0
  169. package/dist/native/session/loop-events/types.js +14 -0
  170. package/dist/native/session/loop-handlers.d.ts +73 -0
  171. package/dist/native/session/loop-handlers.js +137 -0
  172. package/dist/native/session/nudge.d.ts +29 -0
  173. package/dist/native/session/nudge.js +35 -0
  174. package/dist/native/session/rate-provenance.d.ts +17 -0
  175. package/dist/native/session/rate-provenance.js +39 -0
  176. package/dist/native/session/session.d.ts +139 -0
  177. package/dist/native/session/session.js +208 -0
  178. package/dist/native/session/tool-input-exemplar.d.ts +2 -0
  179. package/dist/native/session/tool-input-exemplar.js +66 -0
  180. package/dist/native/session/tool-input-validation.d.ts +15 -0
  181. package/dist/native/session/tool-input-validation.js +146 -0
  182. package/dist/native/session/tool-mapping.d.ts +18 -0
  183. package/dist/native/session/tool-mapping.js +27 -0
  184. package/dist/native/session/tool-result.d.ts +37 -0
  185. package/dist/native/session/tool-result.js +25 -0
  186. package/dist/native/session/transcript-store.d.ts +77 -0
  187. package/dist/native/session/transcript-store.js +200 -0
  188. package/dist/native/session/truncation-handler.d.ts +47 -0
  189. package/dist/native/session/truncation-handler.js +83 -0
  190. package/dist/native/session/turn-accumulator.d.ts +45 -0
  191. package/dist/native/session/turn-accumulator.js +67 -0
  192. package/dist/native/session/turn-ask-human.d.ts +23 -0
  193. package/dist/native/session/turn-ask-human.js +41 -0
  194. package/dist/native/session/turn-compaction-step.d.ts +74 -0
  195. package/dist/native/session/turn-compaction-step.js +128 -0
  196. package/dist/native/session/turn-complete-step.d.ts +77 -0
  197. package/dist/native/session/turn-complete-step.js +159 -0
  198. package/dist/native/session/turn-end-event.d.ts +3 -0
  199. package/dist/native/session/turn-end-event.js +17 -0
  200. package/dist/native/session/turn-events.d.ts +63 -0
  201. package/dist/native/session/turn-events.js +48 -0
  202. package/dist/native/session/turn-loop-round-trip.d.ts +116 -0
  203. package/dist/native/session/turn-loop-round-trip.js +318 -0
  204. package/dist/native/session/turn-loop.d.ts +23 -0
  205. package/dist/native/session/turn-loop.js +250 -0
  206. package/dist/native/session/turn-result.d.ts +39 -0
  207. package/dist/native/session/turn-result.js +64 -0
  208. package/dist/native/session/turn-retry.d.ts +120 -0
  209. package/dist/native/session/turn-retry.js +182 -0
  210. package/dist/native/session/turn-tool-batch.d.ts +61 -0
  211. package/dist/native/session/turn-tool-batch.js +259 -0
  212. package/dist/native/session/turn-types.d.ts +164 -0
  213. package/dist/native/session/turn-types.js +36 -0
  214. package/dist/native/session-adapter.d.ts +56 -0
  215. package/dist/native/session-adapter.js +369 -0
  216. package/dist/native/session-affinity.d.ts +36 -0
  217. package/dist/native/session-affinity.js +43 -0
  218. package/dist/native/tier-providers.d.ts +18 -0
  219. package/dist/native/tier-providers.js +20 -0
  220. package/dist/permissions/approval-audit.d.ts +10 -0
  221. package/dist/permissions/approval-audit.js +22 -0
  222. package/dist/permissions/approvals-link.d.ts +20 -0
  223. package/dist/permissions/approvals-link.js +62 -0
  224. package/dist/permissions/approvals-store.d.ts +168 -0
  225. package/dist/permissions/approvals-store.js +259 -0
  226. package/dist/permissions/approvals-taint.d.ts +33 -0
  227. package/dist/permissions/approvals-taint.js +138 -0
  228. package/dist/permissions/ask-chain.d.ts +69 -0
  229. package/dist/permissions/ask-chain.js +32 -0
  230. package/dist/permissions/ask.d.ts +23 -0
  231. package/dist/permissions/ask.js +25 -0
  232. package/dist/permissions/bash-lex.d.ts +59 -0
  233. package/dist/permissions/bash-lex.js +279 -0
  234. package/dist/permissions/grammar.d.ts +12 -0
  235. package/dist/permissions/grammar.js +24 -0
  236. package/dist/permissions/index.d.ts +11 -0
  237. package/dist/permissions/index.js +9 -0
  238. package/dist/permissions/secret-spans.d.ts +19 -0
  239. package/dist/permissions/secret-spans.js +88 -0
  240. package/dist/permissions/types.d.ts +85 -0
  241. package/dist/permissions/types.js +1 -0
  242. package/dist/runtime/index.d.ts +3 -0
  243. package/dist/runtime/index.js +2 -0
  244. package/dist/runtime/node-glob.d.ts +3 -0
  245. package/dist/runtime/node-glob.js +83 -0
  246. package/dist/runtime/node-runtime.d.ts +3 -0
  247. package/dist/runtime/node-runtime.js +126 -0
  248. package/dist/runtime/slot.d.ts +11 -0
  249. package/dist/runtime/slot.js +17 -0
  250. package/dist/runtime/types.d.ts +51 -0
  251. package/dist/runtime/types.js +1 -0
  252. package/dist/runtime/which.d.ts +8 -0
  253. package/dist/runtime/which.js +32 -0
  254. package/dist/sandbox/argv-quote.d.ts +11 -0
  255. package/dist/sandbox/argv-quote.js +13 -0
  256. package/dist/sandbox/defaults.d.ts +27 -0
  257. package/dist/sandbox/defaults.js +47 -0
  258. package/dist/sandbox/git-guards.d.ts +64 -0
  259. package/dist/sandbox/git-guards.js +161 -0
  260. package/dist/sandbox/index.d.ts +11 -0
  261. package/dist/sandbox/index.js +10 -0
  262. package/dist/sandbox/launcher.d.ts +37 -0
  263. package/dist/sandbox/launcher.js +160 -0
  264. package/dist/sandbox/messages.d.ts +17 -0
  265. package/dist/sandbox/messages.js +46 -0
  266. package/dist/sandbox/policy-builder.d.ts +44 -0
  267. package/dist/sandbox/policy-builder.js +149 -0
  268. package/dist/sandbox/policy-inputs.d.ts +53 -0
  269. package/dist/sandbox/policy-inputs.js +76 -0
  270. package/dist/sandbox/probe.d.ts +8 -0
  271. package/dist/sandbox/probe.js +66 -0
  272. package/dist/sandbox/registry.d.ts +17 -0
  273. package/dist/sandbox/registry.js +57 -0
  274. package/dist/sandbox/session-tmp.d.ts +20 -0
  275. package/dist/sandbox/session-tmp.js +95 -0
  276. package/dist/sandbox/srt-backend.d.ts +21 -0
  277. package/dist/sandbox/srt-backend.js +99 -0
  278. package/dist/sandbox/types.d.ts +102 -0
  279. package/dist/sandbox/types.js +1 -0
  280. package/dist/session/adapter-failure.d.ts +60 -0
  281. package/dist/session/adapter-failure.js +11 -0
  282. package/dist/session/agent-stream-events.d.ts +103 -0
  283. package/dist/session/agent-stream-events.js +6 -0
  284. package/dist/session/interaction-handler.d.ts +53 -0
  285. package/dist/session/interaction-handler.js +2 -0
  286. package/dist/session/no-op-interaction-handler.d.ts +9 -0
  287. package/dist/session/no-op-interaction-handler.js +11 -0
  288. package/dist/session/protocol-types.d.ts +12 -0
  289. package/dist/session/protocol-types.js +1 -0
  290. package/dist/session/session-types.d.ts +456 -0
  291. package/dist/session/session-types.js +64 -0
  292. package/dist/session/tool-descriptor.d.ts +23 -0
  293. package/dist/session/tool-descriptor.js +5 -0
  294. package/dist/session/turn-deadline.d.ts +17 -0
  295. package/dist/session/turn-deadline.js +23 -0
  296. package/dist/tools/ask-request.d.ts +35 -0
  297. package/dist/tools/ask-request.js +57 -0
  298. package/dist/tools/bash-cwd.d.ts +82 -0
  299. package/dist/tools/bash-cwd.js +55 -0
  300. package/dist/tools/bash.d.ts +69 -0
  301. package/dist/tools/bash.js +283 -0
  302. package/dist/tools/delete.d.ts +57 -0
  303. package/dist/tools/delete.js +154 -0
  304. package/dist/tools/denial-redirect.d.ts +42 -0
  305. package/dist/tools/denial-redirect.js +266 -0
  306. package/dist/tools/deny-paths.d.ts +35 -0
  307. package/dist/tools/deny-paths.js +76 -0
  308. package/dist/tools/edit-region.d.ts +50 -0
  309. package/dist/tools/edit-region.js +90 -0
  310. package/dist/tools/edit.d.ts +25 -0
  311. package/dist/tools/edit.js +114 -0
  312. package/dist/tools/exec-allowlist-text.d.ts +18 -0
  313. package/dist/tools/exec-allowlist-text.js +24 -0
  314. package/dist/tools/exec-guard.d.ts +69 -0
  315. package/dist/tools/exec-guard.js +151 -0
  316. package/dist/tools/git-commit.d.ts +59 -0
  317. package/dist/tools/git-commit.js +220 -0
  318. package/dist/tools/git-flags/index.d.ts +10 -0
  319. package/dist/tools/git-flags/index.js +10 -0
  320. package/dist/tools/git.d.ts +88 -0
  321. package/dist/tools/git.js +397 -0
  322. package/dist/tools/glob.d.ts +29 -0
  323. package/dist/tools/glob.js +153 -0
  324. package/dist/tools/grep.d.ts +21 -0
  325. package/dist/tools/grep.js +162 -0
  326. package/dist/tools/index.d.ts +36 -0
  327. package/dist/tools/index.js +28 -0
  328. package/dist/tools/narrow-grants.d.ts +19 -0
  329. package/dist/tools/narrow-grants.js +54 -0
  330. package/dist/tools/nax-owned-writes.d.ts +135 -0
  331. package/dist/tools/nax-owned-writes.js +259 -0
  332. package/dist/tools/package-managers-table.d.ts +146 -0
  333. package/dist/tools/package-managers-table.js +325 -0
  334. package/dist/tools/package-managers-types.d.ts +36 -0
  335. package/dist/tools/package-managers-types.js +6 -0
  336. package/dist/tools/package-managers.d.ts +85 -0
  337. package/dist/tools/package-managers.js +391 -0
  338. package/dist/tools/path-list.d.ts +38 -0
  339. package/dist/tools/path-list.js +62 -0
  340. package/dist/tools/policy-bash-raw.d.ts +19 -0
  341. package/dist/tools/policy-bash-raw.js +210 -0
  342. package/dist/tools/policy-bash.d.ts +64 -0
  343. package/dist/tools/policy-bash.js +232 -0
  344. package/dist/tools/policy-command-branch.d.ts +32 -0
  345. package/dist/tools/policy-command-branch.js +63 -0
  346. package/dist/tools/policy-input.d.ts +2 -0
  347. package/dist/tools/policy-input.js +10 -0
  348. package/dist/tools/policy-match.d.ts +75 -0
  349. package/dist/tools/policy-match.js +117 -0
  350. package/dist/tools/policy-paths-branch.d.ts +97 -0
  351. package/dist/tools/policy-paths-branch.js +314 -0
  352. package/dist/tools/policy.d.ts +65 -0
  353. package/dist/tools/policy.js +232 -0
  354. package/dist/tools/protected-paths.d.ts +26 -0
  355. package/dist/tools/protected-paths.js +8 -0
  356. package/dist/tools/provider-adapt.d.ts +4 -0
  357. package/dist/tools/provider-adapt.js +32 -0
  358. package/dist/tools/provider-advertise.d.ts +39 -0
  359. package/dist/tools/provider-advertise.js +50 -0
  360. package/dist/tools/provider-grants.d.ts +36 -0
  361. package/dist/tools/provider-grants.js +72 -0
  362. package/dist/tools/provider-sanitize.d.ts +19 -0
  363. package/dist/tools/provider-sanitize.js +53 -0
  364. package/dist/tools/provider-types.d.ts +41 -0
  365. package/dist/tools/provider-types.js +26 -0
  366. package/dist/tools/read-continuation.d.ts +147 -0
  367. package/dist/tools/read-continuation.js +176 -0
  368. package/dist/tools/read-file.d.ts +32 -0
  369. package/dist/tools/read-file.js +85 -0
  370. package/dist/tools/read.d.ts +25 -0
  371. package/dist/tools/read.js +182 -0
  372. package/dist/tools/registry.d.ts +125 -0
  373. package/dist/tools/registry.js +67 -0
  374. package/dist/tools/request-capability.d.ts +13 -0
  375. package/dist/tools/request-capability.js +23 -0
  376. package/dist/tools/run-command-exec.d.ts +14 -0
  377. package/dist/tools/run-command-exec.js +161 -0
  378. package/dist/tools/run-command.d.ts +107 -0
  379. package/dist/tools/run-command.js +398 -0
  380. package/dist/tools/runtime-calltool.d.ts +146 -0
  381. package/dist/tools/runtime-calltool.js +223 -0
  382. package/dist/tools/runtime.d.ts +149 -0
  383. package/dist/tools/runtime.js +345 -0
  384. package/dist/tools/scratchpad.d.ts +20 -0
  385. package/dist/tools/scratchpad.js +197 -0
  386. package/dist/tools/spill.d.ts +81 -0
  387. package/dist/tools/spill.js +251 -0
  388. package/dist/tools/tool-audit.d.ts +171 -0
  389. package/dist/tools/tool-audit.js +183 -0
  390. package/dist/tools/truncate.d.ts +98 -0
  391. package/dist/tools/truncate.js +297 -0
  392. package/dist/tools/types.d.ts +145 -0
  393. package/dist/tools/types.js +24 -0
  394. package/dist/tools/verb-denial-argv-hint.d.ts +35 -0
  395. package/dist/tools/verb-denial-argv-hint.js +40 -0
  396. package/dist/tools/write.d.ts +9 -0
  397. package/dist/tools/write.js +45 -0
  398. package/package.json +54 -0
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The turn's tail: the warnings emitted once the loop has exited, and the
3
+ * `TurnResult` assembled from what the loop accumulated.
4
+ *
5
+ * Extracted from `turn-loop.ts`, which keeps the `saveTranscript` call that
6
+ * sits between the two: a write failure there fails the turn, unlike the
7
+ * best-effort save in the catch, and that distinction belongs with the control
8
+ * flow rather than in a result builder.
9
+ */
10
+ import { getSafeLogger } from "#src/infra/index";
11
+ export function logTurnTailWarnings(args) {
12
+ const { sessionName, completedNormally, spinStopped, roundTrips, timedOut, spinBreaker, invalidCallHalt } = args;
13
+ // Parity with acp/adapter.ts:555, which warns in exactly this situation. A
14
+ // native turn that stops here is indistinguishable from a finished one
15
+ // without this line plus the `turnIncomplete` fact below.
16
+ if (!completedNormally) {
17
+ getSafeLogger()?.warn("native-adapter", "turn ended with tool calls outstanding", {
18
+ sessionName,
19
+ roundTrips,
20
+ timedOut,
21
+ ...(invalidCallHalt !== undefined ? { invalidCallBudgetExceeded: true } : {}),
22
+ });
23
+ }
24
+ if (spinStopped) {
25
+ getSafeLogger()?.error("native-adapter", "turn ended by the spin breaker", {
26
+ sessionName,
27
+ roundTrips,
28
+ ...spinBreaker?.summary(),
29
+ });
30
+ }
31
+ // nax#2200: the budget halt used to leave no trace but the generic line
32
+ // above, so a verifier that lost its verdict to three `refs: null` calls read
33
+ // as a timeout. Name the tool and the property that was rejected.
34
+ if (invalidCallHalt !== undefined) {
35
+ getSafeLogger()?.error("native-adapter", "turn ended by the invalid tool call budget", {
36
+ sessionName,
37
+ roundTrips,
38
+ tool: invalidCallHalt.tool,
39
+ property: invalidCallHalt.property,
40
+ expected: invalidCallHalt.expected,
41
+ actual: invalidCallHalt.actual,
42
+ });
43
+ }
44
+ }
45
+ export function buildTurnResult(args) {
46
+ const { output, usage, roundTrips, codingTools, codingToolsCalled, completedNormally, timedOut, spinStopped, invalidCallHalt, interactions, pricingSource, } = args;
47
+ const rates = usage.rates();
48
+ return {
49
+ output,
50
+ tokenUsage: usage.tokens(),
51
+ estimatedCostUsd: usage.costUsd(),
52
+ internalRoundTrips: roundTrips,
53
+ ...(codingTools.length > 0 ? { codingToolUse: { advertised: codingTools.length, called: codingToolsCalled } } : {}),
54
+ ...(completedNormally ? {} : { turnIncomplete: true }),
55
+ ...(timedOut ? { timedOut: true } : {}),
56
+ ...(spinStopped ? { spinStopped: true } : {}),
57
+ ...(invalidCallHalt !== undefined
58
+ ? { invalidCallBudgetExceeded: true, invalidToolCall: invalidCallHalt }
59
+ : {}),
60
+ ...(interactions.length > 0 ? { interactions } : {}),
61
+ ...(pricingSource !== undefined ? { pricingSource } : {}),
62
+ ...(rates !== undefined ? { rates } : {}),
63
+ };
64
+ }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Bounded transport-fault retry for the native turn loop (nax#1870).
3
+ *
4
+ * A mid-stream provider stall (OpenRouter's "Upstream idle timeout exceeded",
5
+ * a bare 502/503) surfaces to `runNativeTurn` as a throw from `deps.complete`.
6
+ * Retrying it here is safe, and safe nowhere else:
7
+ *
8
+ * - `@nathapp/nax-ai`'s own retry (protocols/retry.ts, same repo family as
9
+ * this file — its doc comments are this file's model) is gated on
10
+ * `emitted`: the moment any event is yielded, retry is off for that
11
+ * attempt, because retrying past a `usage` event would double-bill. A
12
+ * mid-stream stall is therefore structurally out of its reach.
13
+ * - A run-kind op's declarative `retry` (`sendWithParseRetry`,
14
+ * src/operations/call.ts) has no try/catch around the call at all — a
15
+ * transport throw propagates straight past it, untouched.
16
+ * - The turn loop can, cheaply: on a throw from `deps.complete`, `messages`
17
+ * is unchanged (the failed response was never appended) and no tool from
18
+ * that round trip has executed (tools run only after `complete` returns),
19
+ * so an immediate re-issue costs one round trip against the alternative of
20
+ * discarding the whole session.
21
+ *
22
+ * This is the native counterpart to `agent.acp.promptRetries`: acpx's
23
+ * spawned process absorbs a provider stall before nax ever observes it;
24
+ * native has no such process, so nax is the harness that must.
25
+ *
26
+ * Detection is structural, matching turn-loop.ts's `isContextOverflow` and
27
+ * adapter-deps.ts's `isProtocolStreamError`: nax-ai's error class is not
28
+ * importable outside src/agents/native/ (check-nax-ai-imports.ts), so only
29
+ * the shape — a `protocolError.kind` string — is inspected, never the class.
30
+ */
31
+ /** Total call attempts including the first (2 = one retry, 3 = two retries). */
32
+ export interface TurnRetryConfig {
33
+ readonly maxAttempts: number;
34
+ /** Base delay for exponential backoff with jitter, in ms, when the provider gives no retryAfter. */
35
+ readonly baseDelayMs: number;
36
+ }
37
+ /** The minimal shape this module reads off a thrown protocol fault. */
38
+ interface RetryableProtocolError {
39
+ readonly protocolError: {
40
+ readonly kind: string;
41
+ readonly message: string;
42
+ /** Seconds, when the provider signals one (nax-ai types.ts ProtocolError.retryAfter). */
43
+ readonly retryAfter?: number;
44
+ readonly status?: number;
45
+ readonly cause?: unknown;
46
+ };
47
+ }
48
+ export declare function isRetryableTransportFault(err: unknown): err is RetryableProtocolError;
49
+ /** Whether another attempt is allowed at all, independent of the error's kind. */
50
+ export declare function canAttemptTurnRetry(retryIndex: number, config: TurnRetryConfig): boolean;
51
+ /**
52
+ * How long to wait before the next attempt. The provider's own `retryAfter`
53
+ * wins when set — it knows its own recovery time better than a guessed
54
+ * backoff does — otherwise equal-jitter exponential backoff from
55
+ * `baseDelayMs`.
56
+ *
57
+ * `remainingMs` caps whatever that produces. A 503 can advertise a recovery
58
+ * window far longer than the turn has left, and sleeping it out would spend
59
+ * wall clock the budget has already declared gone — the attempt after it
60
+ * aborts immediately anyway, because TurnDeadline.remainingMs() clamps to 0
61
+ * and session-adapter.ts arms the call's AbortController with it. Absent means
62
+ * the turn is unbounded (TurnDeadline's UNBOUNDED), so nothing caps the wait.
63
+ */
64
+ export declare function turnRetryDelayMs(err: RetryableProtocolError, retryIndex: number, config: TurnRetryConfig, random?: () => number, remainingMs?: number): number;
65
+ /**
66
+ * Whether the wait is worth taking at all.
67
+ *
68
+ * turnRetryDelayMs clamps to `remainingMs`, which alone produces the worst
69
+ * outcome available: a provider advertising 300s against 30s of budget sleeps
70
+ * the 30s, re-issues, and aborts immediately, having spent the remaining
71
+ * wall clock to learn nothing. Refusing outright surfaces the failure while
72
+ * budget remains for the layer above to act on it.
73
+ *
74
+ * An absent `remainingMs` means the turn is unbounded (TurnDeadline's
75
+ * UNBOUNDED), so there is no budget to exceed and the wait is always allowed.
76
+ */
77
+ export declare function turnRetryFitsBudget(delayMs: number, remainingMs: number | undefined): boolean;
78
+ /**
79
+ * A sleep that resolves early, and rejects, when `signal` fires — mirrors
80
+ * nax-ai's retry.ts `abortableSleep` so a pending backoff cannot outlive the
81
+ * caller's own cancellation and go on to start a request nobody wants.
82
+ */
83
+ export declare function abortableSleep(ms: number, sleep: (ms: number) => Promise<void>, signal?: AbortSignal): Promise<void>;
84
+ /** The real timer. Named and exported so the only production wiring of it is one reference. */
85
+ export declare function realSleep(ms: number): Promise<void>;
86
+ export interface TurnRetryDeps<T> {
87
+ /** Re-issues the round trip. Called once per retry, never for the triggering attempt. */
88
+ readonly attempt: () => Promise<T>;
89
+ readonly config: TurnRetryConfig;
90
+ /** Whole-turn wall-clock budget. Absent means unbounded, matching TurnDeps.deadline. */
91
+ readonly deadline?: {
92
+ expired(): boolean;
93
+ remainingMs(): number | undefined;
94
+ };
95
+ readonly signal?: AbortSignal;
96
+ /**
97
+ * The sleep used for backoff. Deliberately REQUIRED and without a default:
98
+ * a real timer here is production wiring (turn-loop.ts passes `realSleep`),
99
+ * and leaving it optional let a test omit it and silently wait out a real
100
+ * multi-second backoff. Required makes that a compile error instead.
101
+ */
102
+ readonly sleep: (ms: number) => Promise<void>;
103
+ /** Injectable jitter source, for deterministic tests. Defaults to Math.random. */
104
+ readonly random?: () => number;
105
+ /** Fired once per retry, before the backoff delay, with a 1-based retry number. */
106
+ readonly onRetry?: (retryNumber: number, delayMs: number, fault: RetryableProtocolError) => void;
107
+ }
108
+ /**
109
+ * Retries `attempt` while the thrown error is a transport/overloaded fault,
110
+ * bounded by `config.maxAttempts`, the deadline and the abort signal.
111
+ *
112
+ * Exhaustion, a non-retryable error, an expired deadline or an aborted
113
+ * signal all rethrow the triggering error EXACTLY as thrown — never wrapped
114
+ * or mutated. `runNativeTurn` records failure usage against the error's own
115
+ * identity in a WeakMap (`readNativeTurnFailureUsage`), and adapter-deps.ts's
116
+ * `isProtocolStreamError` guard depends on the error keeping its own shape;
117
+ * both would silently break if this function rethrew a new object.
118
+ */
119
+ export declare function retryTransportFault<T>(firstError: unknown, deps: TurnRetryDeps<T>): Promise<T>;
120
+ export {};
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Bounded transport-fault retry for the native turn loop (nax#1870).
3
+ *
4
+ * A mid-stream provider stall (OpenRouter's "Upstream idle timeout exceeded",
5
+ * a bare 502/503) surfaces to `runNativeTurn` as a throw from `deps.complete`.
6
+ * Retrying it here is safe, and safe nowhere else:
7
+ *
8
+ * - `@nathapp/nax-ai`'s own retry (protocols/retry.ts, same repo family as
9
+ * this file — its doc comments are this file's model) is gated on
10
+ * `emitted`: the moment any event is yielded, retry is off for that
11
+ * attempt, because retrying past a `usage` event would double-bill. A
12
+ * mid-stream stall is therefore structurally out of its reach.
13
+ * - A run-kind op's declarative `retry` (`sendWithParseRetry`,
14
+ * src/operations/call.ts) has no try/catch around the call at all — a
15
+ * transport throw propagates straight past it, untouched.
16
+ * - The turn loop can, cheaply: on a throw from `deps.complete`, `messages`
17
+ * is unchanged (the failed response was never appended) and no tool from
18
+ * that round trip has executed (tools run only after `complete` returns),
19
+ * so an immediate re-issue costs one round trip against the alternative of
20
+ * discarding the whole session.
21
+ *
22
+ * This is the native counterpart to `agent.acp.promptRetries`: acpx's
23
+ * spawned process absorbs a provider stall before nax ever observes it;
24
+ * native has no such process, so nax is the harness that must.
25
+ *
26
+ * Detection is structural, matching turn-loop.ts's `isContextOverflow` and
27
+ * adapter-deps.ts's `isProtocolStreamError`: nax-ai's error class is not
28
+ * importable outside src/agents/native/ (check-nax-ai-imports.ts), so only
29
+ * the shape — a `protocolError.kind` string — is inspected, never the class.
30
+ */
31
+ import { credentialFaultCode } from "../errors.js";
32
+ /**
33
+ * Kinds this module retries. "auth" and "bad-request" are terminal --
34
+ * retrying cannot help. "context-overflow" keeps its existing, dedicated
35
+ * compaction-retry path in turn-loop.ts and must never be handled here.
36
+ *
37
+ * "rate-limit" belongs here, not at the manager. It was previously excluded
38
+ * and deferred to the manager-tier defaultRetryStrategy, but that handler sits
39
+ * inside runWithFallback's "swap declined" branch, and on the native transport
40
+ * the swap gate accepts rather than declines -- so control left the branch
41
+ * before reaching it. The wait is also cheapest here: on a throw from
42
+ * deps.complete the messages array is unchanged and no tool has executed, so a
43
+ * re-issue costs one round trip rather than a whole hop and a fresh session.
44
+ */
45
+ const RETRYABLE_KINDS = new Set(["transport", "overloaded", "rate-limit"]);
46
+ export function isRetryableTransportFault(err) {
47
+ if (typeof err !== "object" || err === null || !("protocolError" in err))
48
+ return false;
49
+ const { protocolError } = err;
50
+ if (typeof protocolError?.kind !== "string" || !RETRYABLE_KINDS.has(protocolError.kind))
51
+ return false;
52
+ return credentialFaultCode({ kind: protocolError.kind, cause: protocolError.cause }) === undefined;
53
+ }
54
+ /** Whether another attempt is allowed at all, independent of the error's kind. */
55
+ export function canAttemptTurnRetry(retryIndex, config) {
56
+ return retryIndex < config.maxAttempts - 1;
57
+ }
58
+ /**
59
+ * Equal jitter (AWS's "Exponential Backoff And Jitter"): half of
60
+ * `base * 2^retryIndex` fixed, half uniformly random, so concurrent retries
61
+ * spread out instead of synchronising on the same step while still keeping a
62
+ * floor. Full jitter's [0, ceiling] range was rejected: its lower bound lets
63
+ * a retry re-issue almost instantly against a provider that has just stalled
64
+ * or reported itself overloaded, which is the one thing backoff exists to
65
+ * avoid. nax-ai's own retry.ts omits jitter entirely because it hands
66
+ * concurrency policy to its consumer — this module IS that consumer, so the
67
+ * jitter belongs here.
68
+ */
69
+ function backoffMs(retryIndex, baseDelayMs, random) {
70
+ const half = (baseDelayMs * 2 ** retryIndex) / 2;
71
+ return Math.floor(half + random() * half);
72
+ }
73
+ /**
74
+ * How long to wait before the next attempt. The provider's own `retryAfter`
75
+ * wins when set — it knows its own recovery time better than a guessed
76
+ * backoff does — otherwise equal-jitter exponential backoff from
77
+ * `baseDelayMs`.
78
+ *
79
+ * `remainingMs` caps whatever that produces. A 503 can advertise a recovery
80
+ * window far longer than the turn has left, and sleeping it out would spend
81
+ * wall clock the budget has already declared gone — the attempt after it
82
+ * aborts immediately anyway, because TurnDeadline.remainingMs() clamps to 0
83
+ * and session-adapter.ts arms the call's AbortController with it. Absent means
84
+ * the turn is unbounded (TurnDeadline's UNBOUNDED), so nothing caps the wait.
85
+ */
86
+ export function turnRetryDelayMs(err, retryIndex, config, random = Math.random, remainingMs) {
87
+ const { retryAfter } = err.protocolError;
88
+ const delayMs = retryAfter !== undefined ? retryAfter * 1000 : backoffMs(retryIndex, config.baseDelayMs, random);
89
+ return remainingMs === undefined ? delayMs : Math.min(delayMs, remainingMs);
90
+ }
91
+ /**
92
+ * Whether the wait is worth taking at all.
93
+ *
94
+ * turnRetryDelayMs clamps to `remainingMs`, which alone produces the worst
95
+ * outcome available: a provider advertising 300s against 30s of budget sleeps
96
+ * the 30s, re-issues, and aborts immediately, having spent the remaining
97
+ * wall clock to learn nothing. Refusing outright surfaces the failure while
98
+ * budget remains for the layer above to act on it.
99
+ *
100
+ * An absent `remainingMs` means the turn is unbounded (TurnDeadline's
101
+ * UNBOUNDED), so there is no budget to exceed and the wait is always allowed.
102
+ */
103
+ export function turnRetryFitsBudget(delayMs, remainingMs) {
104
+ return remainingMs === undefined || delayMs <= remainingMs;
105
+ }
106
+ function abortError(signal) {
107
+ return signal.reason ?? new DOMException("Aborted", "AbortError");
108
+ }
109
+ /**
110
+ * A sleep that resolves early, and rejects, when `signal` fires — mirrors
111
+ * nax-ai's retry.ts `abortableSleep` so a pending backoff cannot outlive the
112
+ * caller's own cancellation and go on to start a request nobody wants.
113
+ */
114
+ export function abortableSleep(ms, sleep, signal) {
115
+ if (signal === undefined)
116
+ return sleep(ms);
117
+ if (signal.aborted)
118
+ return Promise.reject(abortError(signal));
119
+ return new Promise((resolve, reject) => {
120
+ const onAbort = () => reject(abortError(signal));
121
+ signal.addEventListener("abort", onAbort, { once: true });
122
+ sleep(ms).then(() => {
123
+ signal.removeEventListener("abort", onAbort);
124
+ resolve();
125
+ }, reject);
126
+ });
127
+ }
128
+ /** The real timer. Named and exported so the only production wiring of it is one reference. */
129
+ export function realSleep(ms) {
130
+ return new Promise((resolve) => setTimeout(resolve, ms));
131
+ }
132
+ /**
133
+ * Retries `attempt` while the thrown error is a transport/overloaded fault,
134
+ * bounded by `config.maxAttempts`, the deadline and the abort signal.
135
+ *
136
+ * Exhaustion, a non-retryable error, an expired deadline or an aborted
137
+ * signal all rethrow the triggering error EXACTLY as thrown — never wrapped
138
+ * or mutated. `runNativeTurn` records failure usage against the error's own
139
+ * identity in a WeakMap (`readNativeTurnFailureUsage`), and adapter-deps.ts's
140
+ * `isProtocolStreamError` guard depends on the error keeping its own shape;
141
+ * both would silently break if this function rethrew a new object.
142
+ */
143
+ export async function retryTransportFault(firstError, deps) {
144
+ const { sleep } = deps;
145
+ const random = deps.random ?? Math.random;
146
+ let err = firstError;
147
+ let retryIndex = 0;
148
+ for (;;) {
149
+ if (!isRetryableTransportFault(err))
150
+ throw err;
151
+ if (!canAttemptTurnRetry(retryIndex, deps.config))
152
+ throw err;
153
+ if (deps.deadline?.expired() === true || deps.signal?.aborted === true)
154
+ throw err;
155
+ const fault = err;
156
+ // Computed WITHOUT the remainingMs clamp: the value that decides whether the
157
+ // wait is affordable must be the provider's own, not one already reduced to
158
+ // fit. Clamping alone spends the whole remaining budget on a wait that cannot
159
+ // succeed, and the attempt after it aborts immediately.
160
+ const remainingMs = deps.deadline?.remainingMs();
161
+ const delayMs = turnRetryDelayMs(fault, retryIndex, deps.config, random);
162
+ if (!turnRetryFitsBudget(delayMs, remainingMs))
163
+ throw fault;
164
+ deps.onRetry?.(retryIndex + 1, delayMs, fault);
165
+ try {
166
+ await abortableSleep(delayMs, sleep, deps.signal);
167
+ }
168
+ catch {
169
+ // The abort during backoff IS the reason to stop; the transport fault
170
+ // that triggered the retry is still the honest cause to surface, not
171
+ // the abort itself.
172
+ throw fault;
173
+ }
174
+ try {
175
+ return await deps.attempt();
176
+ }
177
+ catch (nextErr) {
178
+ err = nextErr;
179
+ retryIndex += 1;
180
+ }
181
+ }
182
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * The per-batch tool-call loop of the native turn loop.
3
+ *
4
+ * One assistant message may carry several tool calls; this module answers every
5
+ * one of them, applying the six distinct rules the loop has accumulated. It is
6
+ * the largest move out of `turn-loop.ts` and the one with the most invariants,
7
+ * so it is extracted unchanged: the rules are the point, not the plumbing.
8
+ *
9
+ * The batch owns a mutable copy of the transcript for its duration and returns
10
+ * the final array; `interactions` and `codingToolsCalled` accumulate locally and
11
+ * come back as NEW entries, so the caller appends rather than the batch
12
+ * mutating turn-lifetime arrays it does not own.
13
+ *
14
+ * `spinWarned` is a snapshot: it is set by the spin breaker's `onSpinStop`
15
+ * callback during this batch only in tandem with a `terminate` outcome, which
16
+ * breaks the batch immediately — so no later call in the same batch needs the
17
+ * flipped value. The batch reports the resulting `spinStopped` back out.
18
+ */
19
+ import type { ToolCall, ToolDefinition } from "@nathapp/nax-ai";
20
+ import type { SpinBreaker } from "#src/infra/spin-breaker/index";
21
+ import type { InteractionExchange, SendTurnOpts } from "#src/session/session-types";
22
+ import type { TranscriptMessage as NativeTranscriptMessage } from "./compaction.ts";
23
+ import { type InvalidCallBudget } from "./handle-invalid-tool-call.ts";
24
+ import type { LoopEventRegistry } from "./loop-events/index.ts";
25
+ import type { TurnDeps } from "./turn-types.ts";
26
+ export interface ToolBatchResult {
27
+ readonly messages: readonly NativeTranscriptMessage[];
28
+ /** Only the exchanges recorded in THIS batch; the caller appends them. */
29
+ readonly interactions: readonly InteractionExchange[];
30
+ /** Only the coding tools called in THIS batch; the caller appends them. */
31
+ readonly codingToolsCalled: readonly string[];
32
+ /** The caller breaks out of the while loop on either. */
33
+ readonly spinStopped: boolean;
34
+ readonly budgetExceeded: boolean;
35
+ /**
36
+ * US-002: the turn signal (`deps.signal`) was aborted during this batch.
37
+ * True means the batch answered this-and-later calls with the synthetic
38
+ * "Not run: the turn was cancelled." result and stopped dispatching; the
39
+ * loop throws the abort reason instead of continuing to another round trip.
40
+ * False (never aborted) preserves the pre-feature shape exactly.
41
+ */
42
+ readonly cancelled: boolean;
43
+ }
44
+ export interface ToolBatchArgs {
45
+ readonly messages: readonly NativeTranscriptMessage[];
46
+ readonly toolCalls: readonly ToolCall[];
47
+ readonly tools: readonly ToolDefinition[];
48
+ readonly codingToolNames: ReadonlySet<string>;
49
+ readonly roundTrips: number;
50
+ readonly opts: SendTurnOpts;
51
+ readonly deps: TurnDeps;
52
+ readonly loopEvents: LoopEventRegistry;
53
+ readonly invalidCallBudget: InvalidCallBudget;
54
+ readonly spinBreaker: SpinBreaker | undefined;
55
+ readonly maxInteractions: number;
56
+ /** Snapshot of the loop's `spinWarned` at batch dispatch. */
57
+ readonly spinWarned: boolean;
58
+ /** Exchanges already recorded this turn, so the ask_human budget is turn-lifetime, not per-batch. */
59
+ readonly interactionsSoFar: number;
60
+ }
61
+ export declare function runToolBatch(args: ToolBatchArgs): Promise<ToolBatchResult>;