@armadra/agent 0.0.0-stage → 0.2.1

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 (407) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/LICENSE +21 -0
  3. package/README.md +426 -2
  4. package/dist/agent/agent.d.ts +56 -0
  5. package/dist/agent/agent.js +129 -0
  6. package/dist/agent/loop.d.ts +51 -0
  7. package/dist/agent/loop.js +158 -0
  8. package/dist/agent/queue.d.ts +25 -0
  9. package/dist/agent/queue.js +55 -0
  10. package/dist/agent/retry.d.ts +22 -0
  11. package/dist/agent/retry.js +92 -0
  12. package/dist/agent/schema.d.ts +27 -0
  13. package/dist/agent/schema.js +186 -0
  14. package/dist/agent/session-cache.d.ts +100 -0
  15. package/dist/agent/session-cache.js +511 -0
  16. package/dist/agent/session-compaction.d.ts +49 -0
  17. package/dist/agent/session-compaction.js +251 -0
  18. package/dist/agent/session-core.d.ts +103 -0
  19. package/dist/agent/session-core.js +7 -0
  20. package/dist/agent/session-run.d.ts +55 -0
  21. package/dist/agent/session-run.js +246 -0
  22. package/dist/agent/session-state.d.ts +39 -0
  23. package/dist/agent/session-state.js +118 -0
  24. package/dist/agent/session-subagent.d.ts +54 -0
  25. package/dist/agent/session-subagent.js +157 -0
  26. package/dist/agent/session-sync.d.ts +20 -0
  27. package/dist/agent/session-sync.js +104 -0
  28. package/dist/agent/session-tools.d.ts +24 -0
  29. package/dist/agent/session-tools.js +277 -0
  30. package/dist/agent/session.d.ts +117 -0
  31. package/dist/agent/session.js +484 -0
  32. package/dist/agent/system-prompt.d.ts +49 -0
  33. package/dist/agent/system-prompt.js +121 -0
  34. package/dist/agent/tool-runner.d.ts +64 -0
  35. package/dist/agent/tool-runner.js +309 -0
  36. package/dist/agent/transform.d.ts +26 -0
  37. package/dist/agent/transform.js +171 -0
  38. package/dist/agent/types.d.ts +378 -0
  39. package/dist/agent/types.js +21 -0
  40. package/dist/ai/apis/anthropic-messages.d.ts +26 -0
  41. package/dist/ai/apis/anthropic-messages.js +214 -0
  42. package/dist/ai/apis/anthropic-request.d.ts +42 -0
  43. package/dist/ai/apis/anthropic-request.js +282 -0
  44. package/dist/ai/apis/api.d.ts +28 -0
  45. package/dist/ai/apis/api.js +102 -0
  46. package/dist/ai/apis/cache-params.d.ts +48 -0
  47. package/dist/ai/apis/cache-params.js +163 -0
  48. package/dist/ai/apis/google-generative-ai.d.ts +32 -0
  49. package/dist/ai/apis/google-generative-ai.js +249 -0
  50. package/dist/ai/apis/google-request.d.ts +40 -0
  51. package/dist/ai/apis/google-request.js +255 -0
  52. package/dist/ai/apis/openai-compat.d.ts +35 -0
  53. package/dist/ai/apis/openai-compat.js +142 -0
  54. package/dist/ai/apis/openai-completions.d.ts +28 -0
  55. package/dist/ai/apis/openai-completions.js +246 -0
  56. package/dist/ai/apis/openai-request.d.ts +35 -0
  57. package/dist/ai/apis/openai-request.js +303 -0
  58. package/dist/ai/apis/openai-responses-request.d.ts +52 -0
  59. package/dist/ai/apis/openai-responses-request.js +266 -0
  60. package/dist/ai/apis/openai-responses.d.ts +28 -0
  61. package/dist/ai/apis/openai-responses.js +303 -0
  62. package/dist/ai/apis/shared.d.ts +56 -0
  63. package/dist/ai/apis/shared.js +179 -0
  64. package/dist/ai/cache/economics.d.ts +24 -0
  65. package/dist/ai/cache/economics.js +69 -0
  66. package/dist/ai/cache/fingerprint.d.ts +17 -0
  67. package/dist/ai/cache/fingerprint.js +39 -0
  68. package/dist/ai/cache/miss.d.ts +49 -0
  69. package/dist/ai/cache/miss.js +96 -0
  70. package/dist/ai/cache/reporting.d.ts +39 -0
  71. package/dist/ai/cache/reporting.js +102 -0
  72. package/dist/ai/cache/types.d.ts +94 -0
  73. package/dist/ai/cache/types.js +19 -0
  74. package/dist/ai/cache/warmer.d.ts +94 -0
  75. package/dist/ai/cache/warmer.js +224 -0
  76. package/dist/ai/context.d.ts +35 -0
  77. package/dist/ai/context.js +114 -0
  78. package/dist/ai/cost.d.ts +21 -0
  79. package/dist/ai/cost.js +60 -0
  80. package/dist/ai/event-stream.d.ts +33 -0
  81. package/dist/ai/event-stream.js +101 -0
  82. package/dist/ai/fake/fake-provider.d.ts +68 -0
  83. package/dist/ai/fake/fake-provider.js +278 -0
  84. package/dist/ai/fake/fake-script.d.ts +69 -0
  85. package/dist/ai/fake/fake-script.js +142 -0
  86. package/dist/ai/http.d.ts +54 -0
  87. package/dist/ai/http.js +212 -0
  88. package/dist/ai/json-partial.d.ts +17 -0
  89. package/dist/ai/json-partial.js +243 -0
  90. package/dist/ai/overflow.d.ts +22 -0
  91. package/dist/ai/overflow.js +61 -0
  92. package/dist/ai/providers/auth.d.ts +71 -0
  93. package/dist/ai/providers/auth.js +202 -0
  94. package/dist/ai/providers/builtin.d.ts +23 -0
  95. package/dist/ai/providers/builtin.js +148 -0
  96. package/dist/ai/providers/catalog-data.d.ts +5 -0
  97. package/dist/ai/providers/catalog-data.js +19 -0
  98. package/dist/ai/providers/catalog.d.ts +28 -0
  99. package/dist/ai/providers/catalog.js +166 -0
  100. package/dist/ai/providers/registry.d.ts +83 -0
  101. package/dist/ai/providers/registry.js +305 -0
  102. package/dist/ai/sse.d.ts +32 -0
  103. package/dist/ai/sse.js +140 -0
  104. package/dist/ai/thinking.d.ts +42 -0
  105. package/dist/ai/thinking.js +101 -0
  106. package/dist/ai/types.d.ts +428 -0
  107. package/dist/ai/types.js +19 -0
  108. package/dist/bundle/ama-sandbox.cjs +324 -0
  109. package/dist/bundle/ama.cjs +25770 -0
  110. package/dist/bundle.d.ts +9 -0
  111. package/dist/bundle.js +15 -0
  112. package/dist/cli/args.d.ts +82 -0
  113. package/dist/cli/args.js +381 -0
  114. package/dist/cli/bootstrap.d.ts +18 -0
  115. package/dist/cli/bootstrap.js +398 -0
  116. package/dist/cli/compose-providers.d.ts +37 -0
  117. package/dist/cli/compose-providers.js +104 -0
  118. package/dist/cli/compose-session.d.ts +69 -0
  119. package/dist/cli/compose-session.js +370 -0
  120. package/dist/cli/compose-store.d.ts +24 -0
  121. package/dist/cli/compose-store.js +125 -0
  122. package/dist/cli/compose.d.ts +106 -0
  123. package/dist/cli/compose.js +206 -0
  124. package/dist/cli/default-model.d.ts +16 -0
  125. package/dist/cli/default-model.js +24 -0
  126. package/dist/cli/deps.d.ts +205 -0
  127. package/dist/cli/deps.js +12 -0
  128. package/dist/cli/exit-codes.d.ts +29 -0
  129. package/dist/cli/exit-codes.js +40 -0
  130. package/dist/cli/main.d.ts +26 -0
  131. package/dist/cli/main.js +165 -0
  132. package/dist/cli/runtime.d.ts +90 -0
  133. package/dist/cli/runtime.js +8 -0
  134. package/dist/cli/startup-screen.d.ts +15 -0
  135. package/dist/cli/startup-screen.js +56 -0
  136. package/dist/cli/startup-steps.d.ts +35 -0
  137. package/dist/cli/startup-steps.js +197 -0
  138. package/dist/cli/subcommands/auth.d.ts +12 -0
  139. package/dist/cli/subcommands/auth.js +91 -0
  140. package/dist/cli/subcommands/config.d.ts +44 -0
  141. package/dist/cli/subcommands/config.js +201 -0
  142. package/dist/cli/subcommands/context.d.ts +21 -0
  143. package/dist/cli/subcommands/context.js +41 -0
  144. package/dist/cli/subcommands/doctor.d.ts +11 -0
  145. package/dist/cli/subcommands/doctor.js +246 -0
  146. package/dist/cli/subcommands/models-cache-probe.d.ts +34 -0
  147. package/dist/cli/subcommands/models-cache-probe.js +190 -0
  148. package/dist/cli/subcommands/models-discover.d.ts +40 -0
  149. package/dist/cli/subcommands/models-discover.js +204 -0
  150. package/dist/cli/subcommands/models.d.ts +40 -0
  151. package/dist/cli/subcommands/models.js +144 -0
  152. package/dist/cli/subcommands/sessions.d.ts +11 -0
  153. package/dist/cli/subcommands/sessions.js +100 -0
  154. package/dist/codemode/capability.d.ts +37 -0
  155. package/dist/codemode/capability.js +55 -0
  156. package/dist/codemode/declarations.d.ts +44 -0
  157. package/dist/codemode/declarations.js +111 -0
  158. package/dist/codemode/host-side.d.ts +59 -0
  159. package/dist/codemode/host-side.js +253 -0
  160. package/dist/codemode/modes.d.ts +23 -0
  161. package/dist/codemode/modes.js +40 -0
  162. package/dist/codemode/protocol.d.ts +91 -0
  163. package/dist/codemode/protocol.js +114 -0
  164. package/dist/codemode/sandbox-entry.d.ts +25 -0
  165. package/dist/codemode/sandbox-entry.js +323 -0
  166. package/dist/codemode/store.d.ts +26 -0
  167. package/dist/codemode/store.js +53 -0
  168. package/dist/codemode/tool.d.ts +105 -0
  169. package/dist/codemode/tool.js +283 -0
  170. package/dist/compaction/branch-summary.d.ts +30 -0
  171. package/dist/compaction/branch-summary.js +79 -0
  172. package/dist/compaction/breaker.d.ts +32 -0
  173. package/dist/compaction/breaker.js +65 -0
  174. package/dist/compaction/cut-point.d.ts +23 -0
  175. package/dist/compaction/cut-point.js +70 -0
  176. package/dist/compaction/estimate.d.ts +30 -0
  177. package/dist/compaction/estimate.js +114 -0
  178. package/dist/compaction/prune-tier.d.ts +31 -0
  179. package/dist/compaction/prune-tier.js +83 -0
  180. package/dist/compaction/serialize.d.ts +20 -0
  181. package/dist/compaction/serialize.js +115 -0
  182. package/dist/compaction/summarize-tier.d.ts +111 -0
  183. package/dist/compaction/summarize-tier.js +305 -0
  184. package/dist/config/auth-file.d.ts +39 -0
  185. package/dist/config/auth-file.js +92 -0
  186. package/dist/config/context-files.d.ts +30 -0
  187. package/dist/config/context-files.js +77 -0
  188. package/dist/config/load.d.ts +45 -0
  189. package/dist/config/load.js +97 -0
  190. package/dist/config/merge.d.ts +73 -0
  191. package/dist/config/merge.js +248 -0
  192. package/dist/config/paths.d.ts +38 -0
  193. package/dist/config/paths.js +108 -0
  194. package/dist/config/profile.d.ts +27 -0
  195. package/dist/config/profile.js +65 -0
  196. package/dist/config/schema.d.ts +34 -0
  197. package/dist/config/schema.js +413 -0
  198. package/dist/config/trust.d.ts +42 -0
  199. package/dist/config/trust.js +122 -0
  200. package/dist/config/types.d.ts +172 -0
  201. package/dist/config/types.js +27 -0
  202. package/dist/config/write.d.ts +10 -0
  203. package/dist/config/write.js +25 -0
  204. package/dist/errors.d.ts +28 -0
  205. package/dist/errors.js +30 -0
  206. package/dist/hooks/config.d.ts +49 -0
  207. package/dist/hooks/config.js +102 -0
  208. package/dist/hooks/dispatcher.d.ts +41 -0
  209. package/dist/hooks/dispatcher.js +104 -0
  210. package/dist/hooks/matcher.d.ts +22 -0
  211. package/dist/hooks/matcher.js +108 -0
  212. package/dist/hooks/protocol.d.ts +33 -0
  213. package/dist/hooks/protocol.js +240 -0
  214. package/dist/hooks/runner.d.ts +30 -0
  215. package/dist/hooks/runner.js +143 -0
  216. package/dist/hooks/types.d.ts +147 -0
  217. package/dist/hooks/types.js +20 -0
  218. package/dist/host/api-impl.d.ts +75 -0
  219. package/dist/host/api-impl.js +173 -0
  220. package/dist/host/loader.d.ts +31 -0
  221. package/dist/host/loader.js +147 -0
  222. package/dist/host/types.d.ts +169 -0
  223. package/dist/host/types.js +14 -0
  224. package/dist/host.d.ts +5 -0
  225. package/dist/host.js +4 -0
  226. package/dist/index.d.ts +41 -0
  227. package/dist/index.js +28 -0
  228. package/dist/modes/commands-core.d.ts +44 -0
  229. package/dist/modes/commands-core.js +171 -0
  230. package/dist/modes/interactive/approval-dialog.d.ts +48 -0
  231. package/dist/modes/interactive/approval-dialog.js +202 -0
  232. package/dist/modes/interactive/commands.d.ts +41 -0
  233. package/dist/modes/interactive/commands.js +202 -0
  234. package/dist/modes/interactive/completion.d.ts +53 -0
  235. package/dist/modes/interactive/completion.js +157 -0
  236. package/dist/modes/interactive/interactive-mode.d.ts +46 -0
  237. package/dist/modes/interactive/interactive-mode.js +487 -0
  238. package/dist/modes/interactive/key-dispatch.d.ts +29 -0
  239. package/dist/modes/interactive/key-dispatch.js +107 -0
  240. package/dist/modes/interactive/line/line-editor.d.ts +50 -0
  241. package/dist/modes/interactive/line/line-editor.js +232 -0
  242. package/dist/modes/interactive/line/line-mode.d.ts +20 -0
  243. package/dist/modes/interactive/line/line-mode.js +216 -0
  244. package/dist/modes/interactive/line/line-render.d.ts +35 -0
  245. package/dist/modes/interactive/line/line-render.js +171 -0
  246. package/dist/modes/interactive/line/paste-state.d.ts +26 -0
  247. package/dist/modes/interactive/line/paste-state.js +72 -0
  248. package/dist/modes/interactive/message-view.d.ts +77 -0
  249. package/dist/modes/interactive/message-view.js +269 -0
  250. package/dist/modes/interactive/pickers.d.ts +42 -0
  251. package/dist/modes/interactive/pickers.js +115 -0
  252. package/dist/modes/interactive/startup-ui.d.ts +28 -0
  253. package/dist/modes/interactive/startup-ui.js +238 -0
  254. package/dist/modes/interactive/status-bar.d.ts +48 -0
  255. package/dist/modes/interactive/status-bar.js +139 -0
  256. package/dist/modes/interactive/tool-view.d.ts +95 -0
  257. package/dist/modes/interactive/tool-view.js +344 -0
  258. package/dist/modes/print/json-event.d.ts +15 -0
  259. package/dist/modes/print/json-event.js +39 -0
  260. package/dist/modes/print/print-mode.d.ts +15 -0
  261. package/dist/modes/print/print-mode.js +96 -0
  262. package/dist/modes/rpc/commands.d.ts +40 -0
  263. package/dist/modes/rpc/commands.js +258 -0
  264. package/dist/modes/rpc/jsonl.d.ts +16 -0
  265. package/dist/modes/rpc/jsonl.js +55 -0
  266. package/dist/modes/rpc/rpc-mode.d.ts +22 -0
  267. package/dist/modes/rpc/rpc-mode.js +153 -0
  268. package/dist/modes/session-report.d.ts +55 -0
  269. package/dist/modes/session-report.js +278 -0
  270. package/dist/modes/shared.d.ts +13 -0
  271. package/dist/modes/shared.js +52 -0
  272. package/dist/modes/startup-ui-text.d.ts +16 -0
  273. package/dist/modes/startup-ui-text.js +103 -0
  274. package/dist/permissions/broker.d.ts +47 -0
  275. package/dist/permissions/broker.js +107 -0
  276. package/dist/permissions/dangerous.d.ts +47 -0
  277. package/dist/permissions/dangerous.js +370 -0
  278. package/dist/permissions/pipeline.d.ts +51 -0
  279. package/dist/permissions/pipeline.js +181 -0
  280. package/dist/permissions/preview.d.ts +43 -0
  281. package/dist/permissions/preview.js +463 -0
  282. package/dist/permissions/rules.d.ts +58 -0
  283. package/dist/permissions/rules.js +249 -0
  284. package/dist/permissions/types.d.ts +100 -0
  285. package/dist/permissions/types.js +18 -0
  286. package/dist/rpc.d.ts +171 -0
  287. package/dist/rpc.js +13 -0
  288. package/dist/sdk.d.ts +98 -0
  289. package/dist/sdk.js +195 -0
  290. package/dist/session/manager.d.ts +70 -0
  291. package/dist/session/manager.js +278 -0
  292. package/dist/session/migrate.d.ts +19 -0
  293. package/dist/session/migrate.js +53 -0
  294. package/dist/session/projection.d.ts +50 -0
  295. package/dist/session/projection.js +180 -0
  296. package/dist/session/store.d.ts +47 -0
  297. package/dist/session/store.js +210 -0
  298. package/dist/session/tree.d.ts +21 -0
  299. package/dist/session/tree.js +88 -0
  300. package/dist/session/types.d.ts +199 -0
  301. package/dist/session/types.js +18 -0
  302. package/dist/skills/discover.d.ts +60 -0
  303. package/dist/skills/discover.js +160 -0
  304. package/dist/skills/expand.d.ts +35 -0
  305. package/dist/skills/expand.js +46 -0
  306. package/dist/skills/frontmatter.d.ts +27 -0
  307. package/dist/skills/frontmatter.js +157 -0
  308. package/dist/skills/index-prompt.d.ts +13 -0
  309. package/dist/skills/index-prompt.js +42 -0
  310. package/dist/skills/templates.d.ts +52 -0
  311. package/dist/skills/templates.js +177 -0
  312. package/dist/tools/bash.d.ts +49 -0
  313. package/dist/tools/bash.js +222 -0
  314. package/dist/tools/edit-fuzzy.d.ts +36 -0
  315. package/dist/tools/edit-fuzzy.js +129 -0
  316. package/dist/tools/edit.d.ts +38 -0
  317. package/dist/tools/edit.js +241 -0
  318. package/dist/tools/file-mutex.d.ts +8 -0
  319. package/dist/tools/file-mutex.js +29 -0
  320. package/dist/tools/glob.d.ts +44 -0
  321. package/dist/tools/glob.js +217 -0
  322. package/dist/tools/grep.d.ts +39 -0
  323. package/dist/tools/grep.js +179 -0
  324. package/dist/tools/ignore.d.ts +40 -0
  325. package/dist/tools/ignore.js +174 -0
  326. package/dist/tools/ls.d.ts +21 -0
  327. package/dist/tools/ls.js +96 -0
  328. package/dist/tools/output-accumulator.d.ts +48 -0
  329. package/dist/tools/output-accumulator.js +106 -0
  330. package/dist/tools/paths.d.ts +20 -0
  331. package/dist/tools/paths.js +58 -0
  332. package/dist/tools/presets.d.ts +71 -0
  333. package/dist/tools/presets.js +128 -0
  334. package/dist/tools/process-tree.d.ts +35 -0
  335. package/dist/tools/process-tree.js +125 -0
  336. package/dist/tools/read.d.ts +28 -0
  337. package/dist/tools/read.js +181 -0
  338. package/dist/tools/registry.d.ts +43 -0
  339. package/dist/tools/registry.js +106 -0
  340. package/dist/tools/shell.d.ts +38 -0
  341. package/dist/tools/shell.js +91 -0
  342. package/dist/tools/task.d.ts +29 -0
  343. package/dist/tools/task.js +107 -0
  344. package/dist/tools/todo.d.ts +28 -0
  345. package/dist/tools/todo.js +107 -0
  346. package/dist/tools/truncate.d.ts +44 -0
  347. package/dist/tools/truncate.js +175 -0
  348. package/dist/tools/types.d.ts +135 -0
  349. package/dist/tools/types.js +19 -0
  350. package/dist/tools/write.d.ts +20 -0
  351. package/dist/tools/write.js +84 -0
  352. package/dist/tui/ansi.d.ts +29 -0
  353. package/dist/tui/ansi.js +420 -0
  354. package/dist/tui/component.d.ts +38 -0
  355. package/dist/tui/component.js +13 -0
  356. package/dist/tui/components/box.d.ts +26 -0
  357. package/dist/tui/components/box.js +65 -0
  358. package/dist/tui/components/container.d.ts +14 -0
  359. package/dist/tui/components/container.js +39 -0
  360. package/dist/tui/components/editor-buffer.d.ts +67 -0
  361. package/dist/tui/components/editor-buffer.js +300 -0
  362. package/dist/tui/components/editor-paste.d.ts +29 -0
  363. package/dist/tui/components/editor-paste.js +64 -0
  364. package/dist/tui/components/editor.d.ts +114 -0
  365. package/dist/tui/components/editor.js +530 -0
  366. package/dist/tui/components/key-value.d.ts +29 -0
  367. package/dist/tui/components/key-value.js +48 -0
  368. package/dist/tui/components/loader.d.ts +35 -0
  369. package/dist/tui/components/loader.js +71 -0
  370. package/dist/tui/components/markdown.d.ts +90 -0
  371. package/dist/tui/components/markdown.js +462 -0
  372. package/dist/tui/components/meter.d.ts +29 -0
  373. package/dist/tui/components/meter.js +47 -0
  374. package/dist/tui/components/overlay.d.ts +24 -0
  375. package/dist/tui/components/overlay.js +52 -0
  376. package/dist/tui/components/select-list.d.ts +57 -0
  377. package/dist/tui/components/select-list.js +179 -0
  378. package/dist/tui/components/spacer.d.ts +11 -0
  379. package/dist/tui/components/spacer.js +16 -0
  380. package/dist/tui/components/text.d.ts +30 -0
  381. package/dist/tui/components/text.js +65 -0
  382. package/dist/tui/keybindings.d.ts +66 -0
  383. package/dist/tui/keybindings.js +120 -0
  384. package/dist/tui/keys.d.ts +38 -0
  385. package/dist/tui/keys.js +275 -0
  386. package/dist/tui/stdin-buffer.d.ts +41 -0
  387. package/dist/tui/stdin-buffer.js +206 -0
  388. package/dist/tui/terminal.d.ts +79 -0
  389. package/dist/tui/terminal.js +153 -0
  390. package/dist/tui/theme.d.ts +31 -0
  391. package/dist/tui/theme.js +199 -0
  392. package/dist/tui/tui.d.ts +103 -0
  393. package/dist/tui/tui.js +363 -0
  394. package/dist/tui/vt-screen.d.ts +46 -0
  395. package/dist/tui/vt-screen.js +251 -0
  396. package/dist/tui.d.ts +30 -0
  397. package/dist/tui.js +31 -0
  398. package/dist/version.d.ts +8 -0
  399. package/dist/version.js +24 -0
  400. package/docs/codemode.md +102 -0
  401. package/docs/hooks.md +196 -0
  402. package/docs/host-api.md +161 -0
  403. package/docs/providers.md +316 -0
  404. package/docs/rpc.md +253 -0
  405. package/docs/session-format.md +119 -0
  406. package/docs/tui.md +168 -0
  407. package/package.json +101 -4
@@ -0,0 +1,34 @@
1
+ /**
2
+ * 配置文件校验(设计 §10、§6.1、§7.3)。[B5]
3
+ *
4
+ * 形状定义在 config/types.ts(B0)与 hooks/types.ts(HookConfig);这里只做校验并再导出。
5
+ * 校验结果是诊断列表:`severity: "error"` 拒绝该文件(启动退出码 3),`"warning"` 只提示
6
+ * (未知字段等)。`path` 是 JSON 指针风格的字段路径(`permission.mode`、`hooks.PreToolUse[0]`)。
7
+ */
8
+ import type { AmaConfig, AuthFile, ProfileFile, TrustFile } from "./types.js";
9
+ import type { HookConfig } from "../hooks/types.js";
10
+ export type { AmaConfig, AuthFile, ProfileFile, TrustFile, TrustEntry, ProviderConfig, PermissionConfig, CompactionConfig, RetryConfig, ToolsConfig, ToolsPreset, CodemodeConfig, CodemodeMode, HooksSettings, UiConfig, SkillsConfig, CacheConfig, ModelConfig, ModelOverride, } from "./types.js";
11
+ export { CONFIG_FILE_VERSION } from "./types.js";
12
+ export type { HookConfig } from "../hooks/types.js";
13
+ export interface Diagnostic {
14
+ severity: "error" | "warning";
15
+ /** 字段路径;文件级问题为 ""。 */
16
+ path: string;
17
+ message: string;
18
+ }
19
+ export declare const THINKING_LEVELS: readonly ["off", "minimal", "low", "medium", "high", "xhigh"];
20
+ export declare const HOOK_TIMEOUT_MAX_MS = 600000;
21
+ export declare function hasErrors(diagnostics: readonly Diagnostic[]): boolean;
22
+ export declare function validateConfig(value: unknown): Diagnostic[];
23
+ export declare function validateAuthFile(value: unknown): Diagnostic[];
24
+ export declare const PROFILE_PATH_FIELDS: readonly ["host", "hooksFile", "authFile", "sessionDir", "config"];
25
+ export declare const PROFILE_PATH_LIST_FIELDS: readonly ["instructions", "skillDirs", "promptDirs"];
26
+ export declare function validateProfile(value: unknown): Diagnostic[];
27
+ export declare function validateTrustFile(value: unknown): Diagnostic[];
28
+ export declare function validateHookConfig(value: unknown): Diagnostic[];
29
+ /** 校验通过后的类型断言(调用方保证 `hasErrors()` 为 false)。 */
30
+ export declare function asConfig(value: unknown): AmaConfig;
31
+ export declare function asAuthFile(value: unknown): AuthFile;
32
+ export declare function asProfile(value: unknown): ProfileFile;
33
+ export declare function asTrustFile(value: unknown): TrustFile;
34
+ export declare function asHookConfig(value: unknown): HookConfig;
@@ -0,0 +1,413 @@
1
+ /**
2
+ * 配置文件校验(设计 §10、§6.1、§7.3)。[B5]
3
+ *
4
+ * 形状定义在 config/types.ts(B0)与 hooks/types.ts(HookConfig);这里只做校验并再导出。
5
+ * 校验结果是诊断列表:`severity: "error"` 拒绝该文件(启动退出码 3),`"warning"` 只提示
6
+ * (未知字段等)。`path` 是 JSON 指针风格的字段路径(`permission.mode`、`hooks.PreToolUse[0]`)。
7
+ */
8
+ import { HOOK_EVENTS } from "../hooks/types.js";
9
+ import { PERMISSION_MODES_STRICT_FIRST } from "../permissions/types.js";
10
+ import { WARMING_MODES } from "../ai/cache/types.js";
11
+ import { CACHE_RETENTIONS, CODEMODE_MODES, TOOLS_PRESETS_STRICT_FIRST } from "./types.js";
12
+ export { CONFIG_FILE_VERSION } from "./types.js";
13
+ export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh"];
14
+ export const HOOK_TIMEOUT_MAX_MS = 600_000;
15
+ class Checker {
16
+ diagnostics = [];
17
+ error(path, message) {
18
+ this.diagnostics.push({ severity: "error", path, message });
19
+ }
20
+ warn(path, message) {
21
+ this.diagnostics.push({ severity: "warning", path, message });
22
+ }
23
+ object(value, path) {
24
+ if (typeof value === "object" && value !== null && !Array.isArray(value))
25
+ return true;
26
+ this.error(path, "应为对象");
27
+ return false;
28
+ }
29
+ /** 未知字段只警告(前向兼容)。 */
30
+ keys(value, path, allowed) {
31
+ for (const key of Object.keys(value)) {
32
+ if (!allowed.includes(key))
33
+ this.warn(join(path, key), "未知字段,已忽略");
34
+ }
35
+ }
36
+ version(value, path) {
37
+ if (value["version"] !== 1)
38
+ this.error(join(path, "version"), "version 必须为 1");
39
+ }
40
+ string(value, key, path, required = false) {
41
+ const v = value[key];
42
+ if (v === undefined) {
43
+ if (required)
44
+ this.error(join(path, key), "缺少必填字符串");
45
+ return;
46
+ }
47
+ if (typeof v !== "string")
48
+ this.error(join(path, key), "应为字符串");
49
+ }
50
+ boolean(value, key, path) {
51
+ const v = value[key];
52
+ if (v !== undefined && typeof v !== "boolean")
53
+ this.error(join(path, key), "应为布尔值");
54
+ }
55
+ number(value, key, path, min = 0, max = Number.MAX_SAFE_INTEGER) {
56
+ const v = value[key];
57
+ if (v === undefined)
58
+ return;
59
+ if (typeof v !== "number" || !Number.isFinite(v)) {
60
+ this.error(join(path, key), "应为数字");
61
+ }
62
+ else if (v < min || v > max) {
63
+ this.error(join(path, key), `应在 ${min}–${max} 之间`);
64
+ }
65
+ }
66
+ oneOf(value, key, path, choices) {
67
+ const v = value[key];
68
+ if (v === undefined)
69
+ return;
70
+ if (typeof v !== "string" || !choices.includes(v)) {
71
+ this.error(join(path, key), `取值应为 ${choices.join(" | ")}`);
72
+ }
73
+ }
74
+ stringArray(value, key, path) {
75
+ const v = value[key];
76
+ if (v === undefined)
77
+ return;
78
+ if (!Array.isArray(v) || v.some((item) => typeof item !== "string")) {
79
+ this.error(join(path, key), "应为字符串数组");
80
+ }
81
+ }
82
+ stringRecord(value, key, path) {
83
+ const v = value[key];
84
+ if (v === undefined)
85
+ return;
86
+ if (!this.object(v, join(path, key)))
87
+ return;
88
+ for (const [k, item] of Object.entries(v)) {
89
+ if (typeof item !== "string")
90
+ this.error(join(join(path, key), k), "应为字符串");
91
+ }
92
+ }
93
+ }
94
+ function join(path, key) {
95
+ if (typeof key === "number")
96
+ return `${path}[${key}]`;
97
+ return path === "" ? key : `${path}.${key}`;
98
+ }
99
+ export function hasErrors(diagnostics) {
100
+ return diagnostics.some((d) => d.severity === "error");
101
+ }
102
+ // ---------------------------------------------------------------------------
103
+ // config.json
104
+ // ---------------------------------------------------------------------------
105
+ const CONFIG_KEYS = [
106
+ "version",
107
+ "defaultModel",
108
+ "thinkingLevel",
109
+ "providers",
110
+ "permission",
111
+ "compaction",
112
+ "retry",
113
+ "tools",
114
+ "codemode",
115
+ "hooks",
116
+ "ui",
117
+ "skills",
118
+ "cache",
119
+ "$schema",
120
+ ];
121
+ const PROVIDER_KEYS = [
122
+ "name",
123
+ "api",
124
+ "baseUrl",
125
+ "apiKey",
126
+ "envKeys",
127
+ "authHeader",
128
+ "headers",
129
+ "compat",
130
+ "requiresApiKey",
131
+ "models",
132
+ "modelOverrides",
133
+ ];
134
+ /** 第三波 §1.3 的缓存兼容开关(其余 compat 字段按协议各异,只查是对象)。 */
135
+ const CACHE_COMPAT_FLAGS = [
136
+ "sendPromptCacheKey",
137
+ "sendSessionAffinityHeaders",
138
+ "supportsLongCacheRetention",
139
+ "supportsExplicitPromptCacheMode",
140
+ ];
141
+ function checkModels(c, value, path) {
142
+ if (value === undefined)
143
+ return;
144
+ if (!Array.isArray(value)) {
145
+ c.error(path, "应为数组");
146
+ return;
147
+ }
148
+ value.forEach((item, index) => {
149
+ const p = join(path, index);
150
+ if (!c.object(item, p))
151
+ return;
152
+ c.string(item, "id", p, true);
153
+ c.string(item, "name", p);
154
+ c.number(item, "contextWindow", p, 1);
155
+ c.number(item, "maxTokens", p, 1);
156
+ c.boolean(item, "reasoning", p);
157
+ c.string(item, "api", p);
158
+ c.string(item, "baseUrl", p);
159
+ c.stringRecord(item, "headers", p);
160
+ const promptCache = item["promptCache"];
161
+ if (promptCache !== undefined && c.object(promptCache, join(p, "promptCache"))) {
162
+ const pp = join(p, "promptCache");
163
+ c.keys(promptCache, pp, ["short", "long", "minTokens"]);
164
+ for (const key of ["short", "long", "minTokens"])
165
+ c.number(promptCache, key, pp);
166
+ }
167
+ const input = item["input"];
168
+ if (input !== undefined &&
169
+ (!Array.isArray(input) || input.some((m) => m !== "text" && m !== "image"))) {
170
+ c.error(join(p, "input"), `应为 ("text" | "image")[]`);
171
+ }
172
+ });
173
+ }
174
+ function checkProvider(c, value, path) {
175
+ if (!c.object(value, path))
176
+ return;
177
+ c.keys(value, path, PROVIDER_KEYS);
178
+ for (const key of ["name", "api", "baseUrl", "apiKey"])
179
+ c.string(value, key, path);
180
+ c.stringArray(value, "envKeys", path);
181
+ c.stringRecord(value, "headers", path);
182
+ c.boolean(value, "requiresApiKey", path);
183
+ const compat = value["compat"];
184
+ if (compat !== undefined && c.object(compat, join(path, "compat"))) {
185
+ const cp = join(path, "compat");
186
+ for (const key of CACHE_COMPAT_FLAGS)
187
+ c.boolean(compat, key, cp);
188
+ c.oneOf(compat, "cacheReporting", cp, ["auto", "silent", "reported"]);
189
+ }
190
+ if (value["authHeader"] !== undefined)
191
+ c.object(value["authHeader"], join(path, "authHeader"));
192
+ checkModels(c, value["models"], join(path, "models"));
193
+ checkModels(c, value["modelOverrides"], join(path, "modelOverrides"));
194
+ }
195
+ function checkSection(c, config, key, allowed, body) {
196
+ const section = config[key];
197
+ if (section === undefined)
198
+ return;
199
+ if (!c.object(section, key))
200
+ return;
201
+ c.keys(section, key, allowed);
202
+ body(section, key);
203
+ }
204
+ export function validateConfig(value) {
205
+ const c = new Checker();
206
+ if (!c.object(value, ""))
207
+ return c.diagnostics;
208
+ c.version(value, "");
209
+ c.keys(value, "", CONFIG_KEYS);
210
+ c.string(value, "defaultModel", "");
211
+ c.oneOf(value, "thinkingLevel", "", THINKING_LEVELS);
212
+ const providers = value["providers"];
213
+ if (providers !== undefined && c.object(providers, "providers")) {
214
+ for (const [id, provider] of Object.entries(providers)) {
215
+ checkProvider(c, provider, join("providers", id));
216
+ }
217
+ }
218
+ checkSection(c, value, "permission", ["mode", "allow", "deny", "builtinDeny"], (s, p) => {
219
+ c.oneOf(s, "mode", p, PERMISSION_MODES_STRICT_FIRST);
220
+ c.stringArray(s, "allow", p);
221
+ c.stringArray(s, "deny", p);
222
+ const builtinDeny = s["builtinDeny"];
223
+ if (builtinDeny !== undefined &&
224
+ typeof builtinDeny !== "boolean" &&
225
+ (!Array.isArray(builtinDeny) || builtinDeny.some((item) => typeof item !== "string"))) {
226
+ c.error(join(p, "builtinDeny"), "应为布尔值或字符串数组");
227
+ }
228
+ });
229
+ checkSection(c, value, "compaction", ["enabled", "reserveTokens", "keepRecentTokens"], (s, p) => {
230
+ c.boolean(s, "enabled", p);
231
+ c.number(s, "reserveTokens", p);
232
+ c.number(s, "keepRecentTokens", p);
233
+ });
234
+ checkSection(c, value, "retry", ["enabled", "maxRetries", "baseDelayMs", "maxDelayMs"], (s, p) => {
235
+ c.boolean(s, "enabled", p);
236
+ c.number(s, "maxRetries", p, 0, 100);
237
+ c.number(s, "baseDelayMs", p);
238
+ c.number(s, "maxDelayMs", p);
239
+ });
240
+ checkSection(c, value, "tools", ["preset", "default", "maxToolResultChars", "bashTimeoutMs", "disabled"], (s, p) => {
241
+ c.oneOf(s, "preset", p, TOOLS_PRESETS_STRICT_FIRST);
242
+ c.stringArray(s, "default", p);
243
+ c.number(s, "maxToolResultChars", p, 1);
244
+ c.number(s, "bashTimeoutMs", p, 1);
245
+ c.stringArray(s, "disabled", p);
246
+ });
247
+ checkSection(c, value, "codemode", ["mode", "inlineBudget", "requireStrict"], (s, p) => {
248
+ c.oneOf(s, "mode", p, CODEMODE_MODES);
249
+ c.number(s, "inlineBudget", p, 0);
250
+ c.boolean(s, "requireStrict", p);
251
+ });
252
+ checkSection(c, value, "hooks", ["timeoutMs"], (s, p) => {
253
+ c.number(s, "timeoutMs", p, 1, HOOK_TIMEOUT_MAX_MS);
254
+ });
255
+ checkSection(c, value, "ui", ["theme", "markdown", "showThinking", "tuiMode", "quietStartup"], (s, p) => {
256
+ c.oneOf(s, "theme", p, ["dark", "light"]);
257
+ c.boolean(s, "markdown", p);
258
+ c.oneOf(s, "showThinking", p, ["full", "collapsed", "hidden"]);
259
+ c.oneOf(s, "tuiMode", p, ["regular"]);
260
+ c.oneOf(s, "quietStartup", p, ["normal", "header", "silent"]);
261
+ });
262
+ checkSection(c, value, "skills", ["dirs"], (s, p) => c.stringArray(s, "dirs", p));
263
+ checkSection(c, value, "cache", ["warming", "retention", "minSavingsUsd", "missNotices", "warmSubagents"], (s, p) => {
264
+ c.oneOf(s, "warming", p, WARMING_MODES);
265
+ c.oneOf(s, "retention", p, CACHE_RETENTIONS);
266
+ c.number(s, "minSavingsUsd", p, 0);
267
+ c.boolean(s, "missNotices", p);
268
+ c.boolean(s, "warmSubagents", p);
269
+ });
270
+ return c.diagnostics;
271
+ }
272
+ // ---------------------------------------------------------------------------
273
+ // auth.json / profile.json / trust.json / hooks.json
274
+ // ---------------------------------------------------------------------------
275
+ export function validateAuthFile(value) {
276
+ const c = new Checker();
277
+ if (!c.object(value, ""))
278
+ return c.diagnostics;
279
+ c.version(value, "");
280
+ c.keys(value, "", ["version", "providers"]);
281
+ const providers = value["providers"];
282
+ if (providers === undefined) {
283
+ c.error("providers", "缺少 providers");
284
+ return c.diagnostics;
285
+ }
286
+ if (!c.object(providers, "providers"))
287
+ return c.diagnostics;
288
+ for (const [id, entry] of Object.entries(providers)) {
289
+ const p = join("providers", id);
290
+ if (!c.object(entry, p))
291
+ continue;
292
+ c.keys(entry, p, ["apiKey", "env", "baseUrl"]);
293
+ c.string(entry, "apiKey", p, true);
294
+ c.stringRecord(entry, "env", p);
295
+ c.string(entry, "baseUrl", p);
296
+ }
297
+ return c.diagnostics;
298
+ }
299
+ export const PROFILE_PATH_FIELDS = [
300
+ "host",
301
+ "hooksFile",
302
+ "authFile",
303
+ "sessionDir",
304
+ "config",
305
+ ];
306
+ export const PROFILE_PATH_LIST_FIELDS = ["instructions", "skillDirs", "promptDirs"];
307
+ export function validateProfile(value) {
308
+ const c = new Checker();
309
+ if (!c.object(value, ""))
310
+ return c.diagnostics;
311
+ c.version(value, "");
312
+ c.keys(value, "", [
313
+ "version",
314
+ ...PROFILE_PATH_FIELDS,
315
+ ...PROFILE_PATH_LIST_FIELDS,
316
+ "authEnv",
317
+ "trustProject",
318
+ "$schema",
319
+ ]);
320
+ for (const key of PROFILE_PATH_FIELDS)
321
+ c.string(value, key, "");
322
+ for (const key of PROFILE_PATH_LIST_FIELDS)
323
+ c.stringArray(value, key, "");
324
+ c.boolean(value, "authEnv", "");
325
+ c.boolean(value, "trustProject", "");
326
+ return c.diagnostics;
327
+ }
328
+ export function validateTrustFile(value) {
329
+ const c = new Checker();
330
+ if (!c.object(value, ""))
331
+ return c.diagnostics;
332
+ c.version(value, "");
333
+ const entries = value["entries"];
334
+ if (!Array.isArray(entries)) {
335
+ c.error("entries", "应为数组");
336
+ return c.diagnostics;
337
+ }
338
+ entries.forEach((entry, index) => {
339
+ const p = join("entries", index);
340
+ if (!c.object(entry, p))
341
+ return;
342
+ c.string(entry, "path", p, true);
343
+ c.string(entry, "at", p, true);
344
+ if (typeof entry["trusted"] !== "boolean")
345
+ c.error(join(p, "trusted"), "应为布尔值");
346
+ });
347
+ return c.diagnostics;
348
+ }
349
+ export function validateHookConfig(value) {
350
+ const c = new Checker();
351
+ if (!c.object(value, ""))
352
+ return c.diagnostics;
353
+ c.version(value, "");
354
+ c.keys(value, "", ["version", "hooks", "$schema"]);
355
+ const hooks = value["hooks"];
356
+ if (hooks === undefined)
357
+ return c.diagnostics;
358
+ if (!c.object(hooks, "hooks"))
359
+ return c.diagnostics;
360
+ for (const [event, groups] of Object.entries(hooks)) {
361
+ const p = join("hooks", event);
362
+ if (!HOOK_EVENTS.includes(event)) {
363
+ c.error(p, `未知事件(可用:${HOOK_EVENTS.join(", ")})`);
364
+ continue;
365
+ }
366
+ if (!Array.isArray(groups)) {
367
+ c.error(p, "应为数组");
368
+ continue;
369
+ }
370
+ groups.forEach((group, gi) => {
371
+ const gp = join(p, gi);
372
+ if (!c.object(group, gp))
373
+ return;
374
+ c.keys(group, gp, ["matcher", "hooks"]);
375
+ c.string(group, "matcher", gp);
376
+ const commands = group["hooks"];
377
+ if (!Array.isArray(commands)) {
378
+ c.error(join(gp, "hooks"), "应为数组");
379
+ return;
380
+ }
381
+ commands.forEach((command, ci) => {
382
+ const cp = join(join(gp, "hooks"), ci);
383
+ if (!c.object(command, cp))
384
+ return;
385
+ c.keys(command, cp, ["type", "command", "timeoutMs"]);
386
+ if (command["type"] !== "command")
387
+ c.error(join(cp, "type"), `type 必须为 "command"`);
388
+ c.string(command, "command", cp, true);
389
+ if (typeof command["command"] === "string" && command["command"].trim() === "") {
390
+ c.error(join(cp, "command"), "命令为空");
391
+ }
392
+ c.number(command, "timeoutMs", cp, 1, HOOK_TIMEOUT_MAX_MS);
393
+ });
394
+ });
395
+ }
396
+ return c.diagnostics;
397
+ }
398
+ /** 校验通过后的类型断言(调用方保证 `hasErrors()` 为 false)。 */
399
+ export function asConfig(value) {
400
+ return value;
401
+ }
402
+ export function asAuthFile(value) {
403
+ return value;
404
+ }
405
+ export function asProfile(value) {
406
+ return value;
407
+ }
408
+ export function asTrustFile(value) {
409
+ return value;
410
+ }
411
+ export function asHookConfig(value) {
412
+ return value;
413
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * 项目信任(设计 §6.1「信任边界」、§7.3)。[B5]
3
+ *
4
+ * - 需要信任:`.ama/hooks.json`、`.ama/skills/`、`.ama/prompts/`、祖先 `.agents/skills/`。
5
+ * 不需要:AGENTS.md、`.ama/config.json`(它只能收紧)。
6
+ * - 决策顺序:`--trust` / `--no-trust` → profile `trustProject: true` → trust.json 中最近祖先的
7
+ * 记录 → 交互模式询问(一次,可记住)→ 非交互缺省不信任。
8
+ * - trust.json 只在用户级目录;写入为原子替换。
9
+ */
10
+ import type { TrustState } from "../cli/runtime.js";
11
+ import type { TrustEntry, TrustFile } from "./types.js";
12
+ export type { TrustState } from "../cli/runtime.js";
13
+ export declare function trustFilePath(configDir: string): string;
14
+ /** 读 trust.json;不存在 → 空表。语法 / 字段错误抛 StartupError(3)。 */
15
+ export declare function readTrustFile(configDir: string): TrustFile;
16
+ /** `dir` 是否等于 `ancestor` 或在其下。 */
17
+ export declare function isWithin(dir: string, ancestor: string, platform?: NodeJS.Platform): boolean;
18
+ /** 最近祖先(路径最长)的记录。 */
19
+ export declare function findTrustEntry(entries: readonly TrustEntry[], cwd: string, platform?: NodeJS.Platform): TrustEntry | undefined;
20
+ /** 记录信任决策(同路径覆盖)。 */
21
+ export declare function recordTrust(configDir: string, path: string, trusted: boolean, now?: Date): void;
22
+ /** cwd 下需要信任才能加载的资源(为空则无需询问)。 */
23
+ export declare function trustGatedResources(cwd: string): string[];
24
+ /** cwd 自身与全部祖先(由内向外)。 */
25
+ export declare function ancestorsOf(cwd: string): string[];
26
+ export interface TrustPromptAnswer {
27
+ trusted: boolean;
28
+ /** 写入 trust.json。 */
29
+ remember: boolean;
30
+ }
31
+ export interface DecideTrustInput {
32
+ cwd: string;
33
+ configDir: string;
34
+ /** `--trust` → true、`--no-trust` → false。 */
35
+ flag?: boolean | undefined;
36
+ /** profile.trustProject。 */
37
+ profileTrust?: boolean | undefined;
38
+ interactive: boolean;
39
+ /** 交互询问;不提供或非交互时跳过。 */
40
+ prompt?: ((cwd: string, resources: readonly string[]) => Promise<TrustPromptAnswer>) | undefined;
41
+ }
42
+ export declare function decideTrust(input: DecideTrustInput): Promise<TrustState>;
@@ -0,0 +1,122 @@
1
+ /**
2
+ * 项目信任(设计 §6.1「信任边界」、§7.3)。[B5]
3
+ *
4
+ * - 需要信任:`.ama/hooks.json`、`.ama/skills/`、`.ama/prompts/`、祖先 `.agents/skills/`。
5
+ * 不需要:AGENTS.md、`.ama/config.json`(它只能收紧)。
6
+ * - 决策顺序:`--trust` / `--no-trust` → profile `trustProject: true` → trust.json 中最近祖先的
7
+ * 记录 → 交互模式询问(一次,可记住)→ 非交互缺省不信任。
8
+ * - trust.json 只在用户级目录;写入为原子替换。
9
+ */
10
+ import { existsSync, mkdirSync, renameSync, statSync, writeFileSync } from "node:fs";
11
+ import { dirname, join, resolve, sep } from "node:path";
12
+ import { loadConfigFile } from "./load.js";
13
+ import { PROJECT_DIR, TRUST_FILE } from "./paths.js";
14
+ import { CONFIG_FILE_VERSION } from "./types.js";
15
+ export function trustFilePath(configDir) {
16
+ return join(configDir, TRUST_FILE);
17
+ }
18
+ /** 读 trust.json;不存在 → 空表。语法 / 字段错误抛 StartupError(3)。 */
19
+ export function readTrustFile(configDir) {
20
+ const loaded = loadConfigFile("trust", trustFilePath(configDir));
21
+ return loaded?.value ?? { version: CONFIG_FILE_VERSION, entries: [] };
22
+ }
23
+ function normalize(path, platform) {
24
+ const abs = resolve(path);
25
+ const trimmed = abs.length > 1 && abs.endsWith(sep) ? abs.slice(0, -1) : abs;
26
+ return platform === "win32" || platform === "darwin" ? trimmed.toLowerCase() : trimmed;
27
+ }
28
+ /** `dir` 是否等于 `ancestor` 或在其下。 */
29
+ export function isWithin(dir, ancestor, platform = process.platform) {
30
+ const d = normalize(dir, platform);
31
+ const a = normalize(ancestor, platform);
32
+ if (d === a)
33
+ return true;
34
+ const prefix = a.endsWith(sep) ? a : a + sep;
35
+ return d.startsWith(prefix);
36
+ }
37
+ /** 最近祖先(路径最长)的记录。 */
38
+ export function findTrustEntry(entries, cwd, platform = process.platform) {
39
+ let best;
40
+ for (const entry of entries) {
41
+ if (!isWithin(cwd, entry.path, platform))
42
+ continue;
43
+ if (best === undefined ||
44
+ normalize(entry.path, platform).length >= normalize(best.path, platform).length) {
45
+ best = entry;
46
+ }
47
+ }
48
+ return best;
49
+ }
50
+ /** 记录信任决策(同路径覆盖)。 */
51
+ export function recordTrust(configDir, path, trusted, now = new Date()) {
52
+ const file = readTrustFile(configDir);
53
+ const abs = resolve(path);
54
+ const entries = file.entries.filter((e) => normalize(e.path, process.platform) !== normalize(abs, process.platform));
55
+ entries.push({ path: abs, trusted, at: now.toISOString() });
56
+ const target = trustFilePath(configDir);
57
+ mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
58
+ const tmp = `${target}.${process.pid}.tmp`;
59
+ writeFileSync(tmp, `${JSON.stringify({ version: CONFIG_FILE_VERSION, entries }, null, 2)}\n`, {
60
+ mode: 0o600,
61
+ });
62
+ renameSync(tmp, target);
63
+ }
64
+ function isDir(path) {
65
+ try {
66
+ return statSync(path).isDirectory();
67
+ }
68
+ catch {
69
+ return false;
70
+ }
71
+ }
72
+ /** cwd 下需要信任才能加载的资源(为空则无需询问)。 */
73
+ export function trustGatedResources(cwd) {
74
+ const found = [];
75
+ const project = join(cwd, PROJECT_DIR);
76
+ if (existsSync(join(project, "hooks.json")))
77
+ found.push(join(project, "hooks.json"));
78
+ for (const sub of ["skills", "prompts"]) {
79
+ if (isDir(join(project, sub)))
80
+ found.push(join(project, sub));
81
+ }
82
+ for (const dir of ancestorsOf(cwd)) {
83
+ const skills = join(dir, ".agents", "skills");
84
+ if (isDir(skills))
85
+ found.push(skills);
86
+ }
87
+ return found;
88
+ }
89
+ /** cwd 自身与全部祖先(由内向外)。 */
90
+ export function ancestorsOf(cwd) {
91
+ const result = [];
92
+ let current = resolve(cwd);
93
+ for (;;) {
94
+ result.push(current);
95
+ const parent = dirname(current);
96
+ if (parent === current)
97
+ break;
98
+ current = parent;
99
+ }
100
+ return result;
101
+ }
102
+ export async function decideTrust(input) {
103
+ if (input.flag !== undefined)
104
+ return { trusted: input.flag, source: "flag" };
105
+ if (input.profileTrust === true)
106
+ return { trusted: true, source: "profile" };
107
+ const entry = findTrustEntry(readTrustFile(input.configDir).entries, input.cwd);
108
+ if (entry !== undefined) {
109
+ return { trusted: entry.trusted, source: "trust-file", matchedPath: entry.path };
110
+ }
111
+ if (input.interactive && input.prompt !== undefined) {
112
+ const resources = trustGatedResources(input.cwd);
113
+ // 没有需要信任的资源就不打扰用户;不信任对结果没有影响。
114
+ if (resources.length === 0)
115
+ return { trusted: false, source: "default" };
116
+ const answer = await input.prompt(input.cwd, resources);
117
+ if (answer.remember)
118
+ recordTrust(input.configDir, input.cwd, answer.trusted);
119
+ return { trusted: answer.trusted, source: "prompt" };
120
+ }
121
+ return { trusted: false, source: "default" };
122
+ }