@aragon-agent/cli 0.6.54 → 0.6.56

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 (346) hide show
  1. package/CHANGELOG.md +2912 -2838
  2. package/README.md +4226 -4042
  3. package/dist/agent/controller.d.ts +119 -0
  4. package/dist/agent/controller.js +365 -2
  5. package/dist/agent/governance-wiring.js +8 -1
  6. package/dist/agent/redteam-prompt.d.ts +47 -0
  7. package/dist/agent/redteam-prompt.js +161 -0
  8. package/dist/agent/redteam-wiring.d.ts +159 -0
  9. package/dist/agent/redteam-wiring.js +349 -0
  10. package/dist/agent/system-prompt.d.ts +39 -0
  11. package/dist/agent/system-prompt.js +18 -0
  12. package/dist/app/App.d.ts +10 -1
  13. package/dist/app/App.js +233 -24
  14. package/dist/app/use-redteam-mode.d.ts +56 -0
  15. package/dist/app/use-redteam-mode.js +121 -0
  16. package/dist/cli.js +82 -4
  17. package/dist/commands/builtins.js +43 -1
  18. package/dist/commands/catalog.js +5 -0
  19. package/dist/commands/index-build.js +2 -1
  20. package/dist/commands/redteam-command.d.ts +22 -0
  21. package/dist/commands/redteam-command.js +141 -0
  22. package/dist/commands/registry.d.ts +8 -0
  23. package/dist/config/app-paths.d.ts +10 -0
  24. package/dist/config/app-paths.js +12 -0
  25. package/dist/config/cli-commands.d.ts +29 -0
  26. package/dist/config/cli-commands.js +76 -1
  27. package/dist/config/env.d.ts +3 -1
  28. package/dist/config/env.js +23 -1
  29. package/dist/config/load.d.ts +22 -0
  30. package/dist/config/load.js +22 -2
  31. package/dist/config/redteam-launch.d.ts +77 -0
  32. package/dist/config/redteam-launch.js +58 -0
  33. package/dist/config/store.js +21 -1
  34. package/dist/config/ui-state.d.ts +44 -0
  35. package/dist/config/ui-state.js +38 -0
  36. package/dist/exec/bridge/server.d.ts +7 -0
  37. package/dist/exec/bridge/server.js +6 -2
  38. package/dist/exec/cli-commands.js +4 -0
  39. package/dist/exec/index.js +2 -0
  40. package/dist/exec/options.d.ts +11 -0
  41. package/dist/exec/options.js +21 -1
  42. package/dist/gene/block.d.ts +33 -0
  43. package/dist/gene/block.js +51 -0
  44. package/dist/gene/capacity.d.ts +74 -0
  45. package/dist/gene/capacity.js +169 -0
  46. package/dist/gene/catalog-reconcile.d.ts +13 -0
  47. package/dist/gene/catalog-reconcile.js +105 -0
  48. package/dist/gene/catalog-stats.d.ts +27 -0
  49. package/dist/gene/catalog-stats.js +89 -0
  50. package/dist/gene/catalog-sync.d.ts +82 -0
  51. package/dist/gene/catalog-sync.js +324 -0
  52. package/dist/gene/catalog.d.ts +160 -0
  53. package/dist/gene/catalog.js +433 -0
  54. package/dist/gene/cli-commands.d.ts +50 -0
  55. package/dist/gene/cli-commands.js +500 -0
  56. package/dist/gene/command-report.d.ts +40 -0
  57. package/dist/gene/command-report.js +246 -0
  58. package/dist/gene/command.d.ts +29 -0
  59. package/dist/gene/command.js +561 -0
  60. package/dist/gene/commit-apply-types.d.ts +13 -0
  61. package/dist/gene/commit-apply-types.js +1 -0
  62. package/dist/gene/commit-compound.d.ts +34 -0
  63. package/dist/gene/commit-compound.js +212 -0
  64. package/dist/gene/commit-counts.d.ts +41 -0
  65. package/dist/gene/commit-counts.js +70 -0
  66. package/dist/gene/commit-exec.d.ts +42 -0
  67. package/dist/gene/commit-exec.js +134 -0
  68. package/dist/gene/commit-head.d.ts +52 -0
  69. package/dist/gene/commit-head.js +97 -0
  70. package/dist/gene/commit-plan.d.ts +95 -0
  71. package/dist/gene/commit-plan.js +68 -0
  72. package/dist/gene/commit-prepare.d.ts +63 -0
  73. package/dist/gene/commit-prepare.js +159 -0
  74. package/dist/gene/commit-types.d.ts +234 -0
  75. package/dist/gene/commit-types.js +3 -0
  76. package/dist/gene/curator/agent.d.ts +137 -0
  77. package/dist/gene/curator/agent.js +203 -0
  78. package/dist/gene/curator/budget.d.ts +69 -0
  79. package/dist/gene/curator/budget.js +131 -0
  80. package/dist/gene/curator/conflicts.d.ts +54 -0
  81. package/dist/gene/curator/conflicts.js +143 -0
  82. package/dist/gene/curator/curator.d.ts +170 -0
  83. package/dist/gene/curator/curator.js +617 -0
  84. package/dist/gene/curator/deps.d.ts +34 -0
  85. package/dist/gene/curator/deps.js +48 -0
  86. package/dist/gene/curator/deterministic.d.ts +62 -0
  87. package/dist/gene/curator/deterministic.js +287 -0
  88. package/dist/gene/curator/executors.d.ts +36 -0
  89. package/dist/gene/curator/executors.js +379 -0
  90. package/dist/gene/curator/lanes.d.ts +52 -0
  91. package/dist/gene/curator/lanes.js +99 -0
  92. package/dist/gene/curator/lease.d.ts +76 -0
  93. package/dist/gene/curator/lease.js +171 -0
  94. package/dist/gene/curator/library.d.ts +36 -0
  95. package/dist/gene/curator/library.js +169 -0
  96. package/dist/gene/curator/lifecycle.d.ts +52 -0
  97. package/dist/gene/curator/lifecycle.js +109 -0
  98. package/dist/gene/curator/maintenance.d.ts +71 -0
  99. package/dist/gene/curator/maintenance.js +213 -0
  100. package/dist/gene/curator/probation.d.ts +51 -0
  101. package/dist/gene/curator/probation.js +71 -0
  102. package/dist/gene/curator/probe.d.ts +131 -0
  103. package/dist/gene/curator/probe.js +534 -0
  104. package/dist/gene/curator/prompt.d.ts +18 -0
  105. package/dist/gene/curator/prompt.js +128 -0
  106. package/dist/gene/curator/publish.d.ts +64 -0
  107. package/dist/gene/curator/publish.js +113 -0
  108. package/dist/gene/curator/recheck.d.ts +55 -0
  109. package/dist/gene/curator/recheck.js +103 -0
  110. package/dist/gene/curator/run-events.d.ts +63 -0
  111. package/dist/gene/curator/run-events.js +55 -0
  112. package/dist/gene/curator/similarity.d.ts +13 -0
  113. package/dist/gene/curator/similarity.js +46 -0
  114. package/dist/gene/curator/switches.d.ts +78 -0
  115. package/dist/gene/curator/switches.js +146 -0
  116. package/dist/gene/curator/task-context.d.ts +93 -0
  117. package/dist/gene/curator/task-context.js +63 -0
  118. package/dist/gene/curator/task-runner.d.ts +56 -0
  119. package/dist/gene/curator/task-runner.js +257 -0
  120. package/dist/gene/curator/tool-support.d.ts +112 -0
  121. package/dist/gene/curator/tool-support.js +197 -0
  122. package/dist/gene/curator/toolkit.d.ts +49 -0
  123. package/dist/gene/curator/toolkit.js +104 -0
  124. package/dist/gene/curator/tools-read.d.ts +4 -0
  125. package/dist/gene/curator/tools-read.js +177 -0
  126. package/dist/gene/curator/tools-release.d.ts +73 -0
  127. package/dist/gene/curator/tools-release.js +287 -0
  128. package/dist/gene/curator/tools-verify.d.ts +47 -0
  129. package/dist/gene/curator/tools-verify.js +202 -0
  130. package/dist/gene/curator/tools-write.d.ts +26 -0
  131. package/dist/gene/curator/tools-write.js +394 -0
  132. package/dist/gene/curator/tools.d.ts +23 -0
  133. package/dist/gene/curator/tools.js +21 -0
  134. package/dist/gene/curator/validate/common.d.ts +87 -0
  135. package/dist/gene/curator/validate/common.js +132 -0
  136. package/dist/gene/curator/validate/evidence-tokens.d.ts +36 -0
  137. package/dist/gene/curator/validate/evidence-tokens.js +139 -0
  138. package/dist/gene/curator/validate/index.d.ts +23 -0
  139. package/dist/gene/curator/validate/index.js +62 -0
  140. package/dist/gene/curator/validate/knowledge.d.ts +25 -0
  141. package/dist/gene/curator/validate/knowledge.js +216 -0
  142. package/dist/gene/curator/validate/live-check.d.ts +14 -0
  143. package/dist/gene/curator/validate/live-check.js +144 -0
  144. package/dist/gene/curator/validate/ports.d.ts +67 -0
  145. package/dist/gene/curator/validate/ports.js +1 -0
  146. package/dist/gene/curator/validate/prompts.d.ts +41 -0
  147. package/dist/gene/curator/validate/prompts.js +161 -0
  148. package/dist/gene/curator/validate/skill.d.ts +15 -0
  149. package/dist/gene/curator/validate/skill.js +251 -0
  150. package/dist/gene/curator/validate/tool.d.ts +6 -0
  151. package/dist/gene/curator/validate/tool.js +217 -0
  152. package/dist/gene/curator/validate/types.d.ts +90 -0
  153. package/dist/gene/curator/validate/types.js +1 -0
  154. package/dist/gene/curator/validator-ports.d.ts +38 -0
  155. package/dist/gene/curator/validator-ports.js +29 -0
  156. package/dist/gene/diag.d.ts +209 -0
  157. package/dist/gene/diag.js +549 -0
  158. package/dist/gene/env.d.ts +10 -0
  159. package/dist/gene/env.js +214 -0
  160. package/dist/gene/eval-recorder.d.ts +84 -0
  161. package/dist/gene/eval-recorder.js +222 -0
  162. package/dist/gene/explain-transport.d.ts +3 -0
  163. package/dist/gene/explain-transport.js +39 -0
  164. package/dist/gene/explain.d.ts +96 -0
  165. package/dist/gene/explain.js +293 -0
  166. package/dist/gene/fs.d.ts +39 -0
  167. package/dist/gene/fs.js +139 -0
  168. package/dist/gene/fsck.d.ts +68 -0
  169. package/dist/gene/fsck.js +134 -0
  170. package/dist/gene/gc-library-sweeps.d.ts +9 -0
  171. package/dist/gene/gc-library-sweeps.js +220 -0
  172. package/dist/gene/gc-rejected-set.d.ts +15 -0
  173. package/dist/gene/gc-rejected-set.js +63 -0
  174. package/dist/gene/gc-safe.d.ts +12 -0
  175. package/dist/gene/gc-safe.js +74 -0
  176. package/dist/gene/gc-scope-sweeps.d.ts +18 -0
  177. package/dist/gene/gc-scope-sweeps.js +297 -0
  178. package/dist/gene/gc-types.d.ts +55 -0
  179. package/dist/gene/gc-types.js +26 -0
  180. package/dist/gene/gc.d.ts +42 -0
  181. package/dist/gene/gc.js +129 -0
  182. package/dist/gene/gene-errors.d.ts +23 -0
  183. package/dist/gene/gene-errors.js +29 -0
  184. package/dist/gene/hints.d.ts +91 -0
  185. package/dist/gene/hints.js +394 -0
  186. package/dist/gene/hub.d.ts +149 -0
  187. package/dist/gene/hub.js +371 -0
  188. package/dist/gene/import-plan.d.ts +80 -0
  189. package/dist/gene/import-plan.js +294 -0
  190. package/dist/gene/import-proposal.d.ts +23 -0
  191. package/dist/gene/import-proposal.js +101 -0
  192. package/dist/gene/import-skill-port.d.ts +6 -0
  193. package/dist/gene/import-skill-port.js +53 -0
  194. package/dist/gene/import-sources.d.ts +48 -0
  195. package/dist/gene/import-sources.js +140 -0
  196. package/dist/gene/index-writer.d.ts +36 -0
  197. package/dist/gene/index-writer.js +101 -0
  198. package/dist/gene/learn/learn-call.d.ts +63 -0
  199. package/dist/gene/learn/learn-call.js +157 -0
  200. package/dist/gene/learn/learn-prompt.d.ts +51 -0
  201. package/dist/gene/learn/learn-prompt.js +245 -0
  202. package/dist/gene/learn/learner.d.ts +181 -0
  203. package/dist/gene/learn/learner.js +426 -0
  204. package/dist/gene/learn/ledger.d.ts +220 -0
  205. package/dist/gene/learn/ledger.js +394 -0
  206. package/dist/gene/learn/proposal-build.d.ts +83 -0
  207. package/dist/gene/learn/proposal-build.js +211 -0
  208. package/dist/gene/learn/proposal-guard.d.ts +43 -0
  209. package/dist/gene/learn/proposal-guard.js +37 -0
  210. package/dist/gene/learn/signals-port.d.ts +47 -0
  211. package/dist/gene/learn/signals-port.js +86 -0
  212. package/dist/gene/maintenance-paths.d.ts +7 -0
  213. package/dist/gene/maintenance-paths.js +20 -0
  214. package/dist/gene/maintenance-probation.d.ts +33 -0
  215. package/dist/gene/maintenance-probation.js +127 -0
  216. package/dist/gene/maintenance-proposals.d.ts +24 -0
  217. package/dist/gene/maintenance-proposals.js +68 -0
  218. package/dist/gene/maintenance-runner.d.ts +92 -0
  219. package/dist/gene/maintenance-runner.js +262 -0
  220. package/dist/gene/maintenance-tasks.d.ts +37 -0
  221. package/dist/gene/maintenance-tasks.js +95 -0
  222. package/dist/gene/manager-actions.d.ts +12 -0
  223. package/dist/gene/manager-actions.js +279 -0
  224. package/dist/gene/manager-approve.d.ts +29 -0
  225. package/dist/gene/manager-approve.js +84 -0
  226. package/dist/gene/manager-context.d.ts +90 -0
  227. package/dist/gene/manager-context.js +201 -0
  228. package/dist/gene/manager-detail.d.ts +17 -0
  229. package/dist/gene/manager-detail.js +313 -0
  230. package/dist/gene/manager-gc.d.ts +13 -0
  231. package/dist/gene/manager-gc.js +81 -0
  232. package/dist/gene/manager-library.d.ts +39 -0
  233. package/dist/gene/manager-library.js +90 -0
  234. package/dist/gene/manager-list.d.ts +27 -0
  235. package/dist/gene/manager-list.js +170 -0
  236. package/dist/gene/manager-queue.d.ts +16 -0
  237. package/dist/gene/manager-queue.js +124 -0
  238. package/dist/gene/manager-stats.d.ts +28 -0
  239. package/dist/gene/manager-stats.js +53 -0
  240. package/dist/gene/manager-view.d.ts +27 -0
  241. package/dist/gene/manager-view.js +124 -0
  242. package/dist/gene/manager.d.ts +56 -0
  243. package/dist/gene/manager.js +179 -0
  244. package/dist/gene/orphan-scopes.d.ts +37 -0
  245. package/dist/gene/orphan-scopes.js +150 -0
  246. package/dist/gene/paths.d.ts +73 -0
  247. package/dist/gene/paths.js +239 -0
  248. package/dist/gene/plan-content.d.ts +73 -0
  249. package/dist/gene/plan-content.js +522 -0
  250. package/dist/gene/plan-lifecycle.d.ts +29 -0
  251. package/dist/gene/plan-lifecycle.js +310 -0
  252. package/dist/gene/policy.d.ts +71 -0
  253. package/dist/gene/policy.js +313 -0
  254. package/dist/gene/project-learning.d.ts +25 -0
  255. package/dist/gene/project-learning.js +54 -0
  256. package/dist/gene/propagation.d.ts +48 -0
  257. package/dist/gene/propagation.js +45 -0
  258. package/dist/gene/queue-lock.d.ts +22 -0
  259. package/dist/gene/queue-lock.js +99 -0
  260. package/dist/gene/queue-record.d.ts +57 -0
  261. package/dist/gene/queue-record.js +145 -0
  262. package/dist/gene/queue.d.ts +235 -0
  263. package/dist/gene/queue.js +764 -0
  264. package/dist/gene/read.d.ts +61 -0
  265. package/dist/gene/read.js +252 -0
  266. package/dist/gene/runlog.d.ts +101 -0
  267. package/dist/gene/runlog.js +404 -0
  268. package/dist/gene/schema-lite.d.ts +24 -0
  269. package/dist/gene/schema-lite.js +159 -0
  270. package/dist/gene/scope-guard.d.ts +23 -0
  271. package/dist/gene/scope-guard.js +9 -0
  272. package/dist/gene/scope-lock.d.ts +48 -0
  273. package/dist/gene/scope-lock.js +222 -0
  274. package/dist/gene/scope-verify.d.ts +63 -0
  275. package/dist/gene/scope-verify.js +123 -0
  276. package/dist/gene/secrets.d.ts +43 -0
  277. package/dist/gene/secrets.js +161 -0
  278. package/dist/gene/service-context.d.ts +43 -0
  279. package/dist/gene/service-context.js +79 -0
  280. package/dist/gene/service-prepare.d.ts +25 -0
  281. package/dist/gene/service-prepare.js +219 -0
  282. package/dist/gene/service-types.d.ts +196 -0
  283. package/dist/gene/service-types.js +1 -0
  284. package/dist/gene/service-view.d.ts +41 -0
  285. package/dist/gene/service-view.js +69 -0
  286. package/dist/gene/service.d.ts +114 -0
  287. package/dist/gene/service.js +761 -0
  288. package/dist/gene/session-start.d.ts +36 -0
  289. package/dist/gene/session-start.js +212 -0
  290. package/dist/gene/store-port.d.ts +88 -0
  291. package/dist/gene/store-port.js +10 -0
  292. package/dist/gene/store-rows.d.ts +36 -0
  293. package/dist/gene/store-rows.js +72 -0
  294. package/dist/gene/store.d.ts +125 -0
  295. package/dist/gene/store.js +622 -0
  296. package/dist/gene/tombstones.d.ts +47 -0
  297. package/dist/gene/tombstones.js +61 -0
  298. package/dist/gene/tool-runner.d.ts +47 -0
  299. package/dist/gene/tool-runner.js +154 -0
  300. package/dist/gene/tool-screen.d.ts +41 -0
  301. package/dist/gene/tool-screen.js +227 -0
  302. package/dist/gene/tools.d.ts +112 -0
  303. package/dist/gene/tools.js +539 -0
  304. package/dist/gene/transfer.d.ts +85 -0
  305. package/dist/gene/transfer.js +433 -0
  306. package/dist/gene/ui-hints.d.ts +15 -0
  307. package/dist/gene/ui-hints.js +67 -0
  308. package/dist/gene/usage-fold-cursor.d.ts +24 -0
  309. package/dist/gene/usage-fold-cursor.js +115 -0
  310. package/dist/gene/usage-fold.d.ts +49 -0
  311. package/dist/gene/usage-fold.js +397 -0
  312. package/dist/gene/usage.d.ts +67 -0
  313. package/dist/gene/usage.js +231 -0
  314. package/dist/gene/version-service.d.ts +118 -0
  315. package/dist/gene/version-service.js +453 -0
  316. package/dist/gene/wiring-events.d.ts +43 -0
  317. package/dist/gene/wiring-events.js +113 -0
  318. package/dist/gene/wiring-models.d.ts +46 -0
  319. package/dist/gene/wiring-models.js +81 -0
  320. package/dist/gene/wiring.d.ts +200 -0
  321. package/dist/gene/wiring.js +764 -0
  322. package/dist/governance/prompts/orchestrator-block.js +3 -2
  323. package/dist/host/controller-factory.d.ts +8 -0
  324. package/dist/host/controller-factory.js +7 -0
  325. package/dist/host/gene.d.ts +26 -0
  326. package/dist/host/gene.js +24 -0
  327. package/dist/host/index.d.ts +1 -0
  328. package/dist/host/index.js +1 -0
  329. package/dist/session/checkpoint.d.ts +15 -0
  330. package/dist/session/checkpoint.js +18 -0
  331. package/dist/session/persist.d.ts +21 -0
  332. package/dist/session/persist.js +10 -0
  333. package/dist/session/startup-session.d.ts +11 -2
  334. package/dist/session/startup-session.js +12 -2
  335. package/dist/session/validate-session.js +31 -0
  336. package/dist/team/runtime.d.ts +16 -0
  337. package/dist/team/runtime.js +29 -0
  338. package/dist/team/subagent.d.ts +55 -0
  339. package/dist/team/subagent.js +46 -2
  340. package/dist/tools/index.d.ts +60 -2
  341. package/dist/tools/index.js +68 -10
  342. package/package.json +3 -3
  343. package/skills/project-indexer/SKILL.md +275 -147
  344. package/skills/project-indexer/templates/artifact-hygiene-template.md +14 -0
  345. package/skills/project-indexer/templates/config-template.md +12 -0
  346. package/skills/project-indexer/templates/index-template.md +4 -0
@@ -0,0 +1,161 @@
1
+ /**
2
+ * The `<redteam_mode>` system-prompt block and the session banner
3
+ * (red-team-security-mode 4.6).
4
+ *
5
+ * ASCII ONLY AND ENGLISH, AND BOTH ARE HARD CONSTRAINTS: `agent/**` is inside
6
+ * `glyphs.test.ts::inScope` (a single non-ASCII byte turns the suite red), and the
7
+ * requirement is explicit that this mode's prompt is English.
8
+ *
9
+ * WHERE IT GOES AND WHY (D-7). `buildSystemPrompt` splices it immediately AFTER the project
10
+ * guidance and BEFORE memory / skills, because this block declares who authorized the session
11
+ * and where the boundary is - it is the frame that explains the scale of every capability
12
+ * block after it. It also has to sit after the base prompt's environment and tool list, since
13
+ * the scope and the target sit on top of the real working directory and the real toolset.
14
+ *
15
+ * WHAT IT DOES NOT SAY. It does not say "maximum permission". `confirmTools` is `false` by
16
+ * default, so for most sessions this mode changes no permission at all (RV-1); a prompt that
17
+ * claimed otherwise would be the same untruth as v1's disclaimer clause 2, one layer down.
18
+ * The `What this mode changes` section therefore states the narrow fact and names the four
19
+ * things that are still refused.
20
+ *
21
+ * VERSIONS: `REDTEAM_BLOCK_VERSION` for the lead, `REDTEAM_CHILD_BLOCK_VERSION` for a
22
+ * subagent, so a wording change is greppable from a behaviour report.
23
+ */
24
+ import { REDTEAM_BLOCK_VERSION, REDTEAM_CHILD_BLOCK_VERSION, } from '@aragon-agent/tui/model/redteam/limits';
25
+ import { clipScopeForPrompt } from '@aragon-agent/tui/model/redteam/scope';
26
+ import { redTeamBannerLines } from '@aragon-agent/tui/ui/layout/redteam-banner';
27
+ /** The scope line as it is injected: normalized once more, and clipped to the budget. */
28
+ function scopeLine(scope) {
29
+ if (scope === null) {
30
+ return '- Engagement scope: not declared. Establish it with the user (one ask_user round,'
31
+ + ' at most three questions) before you touch anything that is not a read.';
32
+ }
33
+ return `- Engagement scope: ${clipScopeForPrompt(scope)}.`;
34
+ }
35
+ /** The posture note, appended before the closing tag when a stricter posture is active. */
36
+ function postureNote(params) {
37
+ if (!params.paused)
38
+ return [];
39
+ const label = params.postureLabel && params.postureLabel.length > 0
40
+ ? params.postureLabel
41
+ : 'A stricter posture';
42
+ return [
43
+ '',
44
+ `Posture note: ${label} is active in this turn. Plan mode still refuses writes and the`,
45
+ 'governed Orchestrator is still hard-gated, so the authorization above applies to',
46
+ 'analysis and reporting only. Do not request a write path until the posture changes.',
47
+ ];
48
+ }
49
+ const AUTHORIZATION_LINES = [
50
+ '- Treat that scope as the only authorized target set. Anything outside it is out of scope,',
51
+ ' even when it is reachable from inside it.',
52
+ '- If a request would act on a target outside the scope, say so in one sentence, name the',
53
+ ' boundary, and ask the user to confirm or extend the scope. Ask once; do not lecture.',
54
+ '- The waiver is the user\'s assertion of authorization. Do not re-argue it every turn and',
55
+ ' do not repeat a disclaimer the user has already accepted.',
56
+ ];
57
+ const CHANGE_LINES = [
58
+ 'What this mode changes',
59
+ '- The confirmation prompt before write_file, edit_file and bash is OFF in this session.',
60
+ ' If it was already off, this mode grants no permission you did not already have. A',
61
+ ' destructive command runs the first time you emit it: prefer read-only discovery, and',
62
+ ' copy before you overwrite.',
63
+ '- Nothing else is loosened. Plan-mode write refusals, the governed Orchestrator\'s hard gate,',
64
+ ' the workspace boundary, the skill tool-policy ceiling and every tool contract are',
65
+ ' unchanged.',
66
+ ];
67
+ const RULES_LINES = [
68
+ 'Rules of engagement',
69
+ '1. State the scope at the start of the work: target, environment (lab / staging / production)',
70
+ ' and the property you were asked to prove.',
71
+ '2. Least impact. Prove a finding with the smallest action that is convincing: a benign marker',
72
+ ' (one file, one line of output, one account) beats a full exploit chain.',
73
+ '3. No persistence and no exfiltration. Do not install backdoors, cron jobs, service accounts',
74
+ ' or credentials, and do not copy real user data or secrets out of the target. When you find',
75
+ ' a secret, report its location and type, never its value.',
76
+ '4. Clean up. Revert what you changed and say what you reverted.',
77
+ '5. Evidence. For each finding record where it is, how to reproduce it, what an attacker gains,',
78
+ ' how to fix it, and a severity (critical / high / medium / low / info) with one sentence of',
79
+ ' reasoning.',
80
+ '6. Honest failure. A negative result is a result: say what you tested and what you ruled out.',
81
+ ];
82
+ const METHOD_LINES = [
83
+ 'Method',
84
+ '- Recon before assumptions: read the code, configuration and infrastructure manifests that',
85
+ ' are in scope before attacking anything.',
86
+ '- Prefer a static read to a live probe, and a local reproduction to a remote one.',
87
+ '- An exploit or fuzz harness belongs in the workspace under one clearly named directory, kept',
88
+ ' minimal and marked as engagement material.',
89
+ '- Threat-model the change: name the asset, the attacker, the entry point and the trust',
90
+ ' boundary being crossed.',
91
+ ];
92
+ const DELIVERABLE_LINES = [
93
+ 'Deliverable',
94
+ '- Lead with findings, most severe first, then reproduction, then the fix.',
95
+ '- A finding without a reproduction is a hypothesis; label it as one.',
96
+ '- Close with the residual risk you did not test.',
97
+ ];
98
+ const CLOSING_LINES = [
99
+ 'This mode is a permission posture, not a legal authorization. The user is responsible for',
100
+ 'having the right to test what they point you at.',
101
+ ];
102
+ /**
103
+ * The block. TOTAL: every combination of `scope` / `paused` / `role` produces exactly one
104
+ * string with exactly one opening and one closing tag (T-P1 / I-8).
105
+ */
106
+ export function buildRedTeamBlock(params) {
107
+ const child = params.role === 'child';
108
+ const version = child ? REDTEAM_CHILD_BLOCK_VERSION : REDTEAM_BLOCK_VERSION;
109
+ const intro = child
110
+ ? 'You are a subagent of a session operating in RED TEAM / SECURITY mode: an explicitly'
111
+ + ' authorized security engagement.'
112
+ : 'You are operating in RED TEAM / SECURITY mode: an explicitly authorized security'
113
+ + ' engagement.';
114
+ // A CHILD DROPS TWO AUTHORIZATION ITEMS (3 and 4): it cannot ask the user to extend a scope
115
+ // (it has no user), and it must not re-argue a waiver it did not see.
116
+ const authorization = child ? AUTHORIZATION_LINES.slice(0, 2) : AUTHORIZATION_LINES;
117
+ const method = child ? METHOD_LINES.slice(0, METHOD_LINES.length - 2) : METHOD_LINES;
118
+ return [
119
+ `<redteam_mode version="${version}">`,
120
+ intro,
121
+ // THE ONE SENTENCE v1 HAD HERE THAT IS GONE (RV-1): "this mode holds the widest permission
122
+ // the runtime offers". With `confirmTools: false` by default it does not, and a prompt
123
+ // that says so teaches the model to overstate what it is allowed to do. The facts are in
124
+ // `What this mode changes` below, in the conditional form.
125
+ 'The user has accepted the liability waiver for this session. Work as an',
126
+ 'offensive-security professional, not as a generic assistant.',
127
+ '',
128
+ 'Authorization',
129
+ scopeLine(params.scope),
130
+ ...authorization,
131
+ '',
132
+ ...CHANGE_LINES,
133
+ '',
134
+ ...RULES_LINES,
135
+ '',
136
+ ...method,
137
+ '',
138
+ ...DELIVERABLE_LINES,
139
+ ...(child
140
+ ? [
141
+ '',
142
+ 'You share the lead session\'s permission posture: confirmations are off for your tool',
143
+ 'calls too, and every rule above binds you exactly as it binds the lead.',
144
+ ]
145
+ : []),
146
+ ...postureNote(params),
147
+ '',
148
+ ...CLOSING_LINES,
149
+ '</redteam_mode>',
150
+ ].join('\n');
151
+ }
152
+ /**
153
+ * The three-row session banner (4.6), as plain lines for the `notice` entry.
154
+ *
155
+ * THE CONFIRMATION SENTENCE IS CONDITIONAL (RV-1 / D-19): the banner is the third place a
156
+ * user could be told something that did not happen, so it states which of the three
157
+ * situations this session is actually in.
158
+ */
159
+ export function buildRedTeamBanner(scope, confirmations) {
160
+ return redTeamBannerLines({ scope, confirmations });
161
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * RedTeamWiring - the stateful half of red team / security mode (4.4).
3
+ *
4
+ * ASCII ONLY (`agent/` is inside the glyph scanner's scope).
5
+ *
6
+ * The controller is far over the repo's size limit, so the mode's state and rules live here and
7
+ * the controller only forwards, the same shape as `TeamModeWiring` and `GovernanceWiring`. The
8
+ * DECISIONS are not here either: `planRedTeam` (tui/model/redteam/redteam-mode.ts) is a pure
9
+ * function, and `redTeamPaused` is the ONE definition of "a stricter posture holds this back"
10
+ * (I-10). This class holds the four things a pure function cannot: `armed`, `scope`, `pending`
11
+ * and the subscription that tells a surface to repaint.
12
+ *
13
+ * THE PERMISSION SURFACE IS ONE METHOD (I-5): `bypassesConfirmation()`. It is read by exactly
14
+ * two call sites - `tools/index.ts`'s confirm wrapper (the lead) and `team/subagent.ts` (a
15
+ * child, through a predicate the controller passes down). Nothing else may consult it, and a
16
+ * later feature that makes plan mode or governance read it would be turning a liability mode
17
+ * into a back door (R-4).
18
+ *
19
+ * THE WAIVER IS READ, NEVER INFERRED (I-3 / I-4): `waiverVersion()` comes from `state.json`
20
+ * through an injected reader, and nothing in `config.json` or the environment can express
21
+ * "accepted".
22
+ */
23
+ import type { CliConfig } from '@aragon-agent/tui/model/config/schema';
24
+ import { type RedTeamOutcome, type RedTeamPosture, type RedTeamRequest } from '@aragon-agent/tui/model/redteam/redteam-mode';
25
+ import type { RedTeamLogEvent, RedTeamView } from '@aragon-agent/tui/model/redteam/types';
26
+ /** What a `--no-redteam` / `ARAGON_REDTEAM=0` / `redteam.enabled=false` session looks like. */
27
+ export declare const UNAVAILABLE_RED_TEAM_VIEW: RedTeamView;
28
+ export interface RedTeamWiringDeps {
29
+ /** Live config: the four `redteam.*` keys are read through it on every question. */
30
+ getConfig(): CliConfig;
31
+ /**
32
+ * The chain exists for this launch: `--no-redteam` / `ARAGON_REDTEAM=0` are already folded
33
+ * into `redteam.enabled` by the config layer, so the controller passes that one predicate
34
+ * rather than three flags (the `--no-redteam` table in 4.8).
35
+ */
36
+ isAvailable(): boolean;
37
+ /** The accepted waiver version from `state.json` (0 = never). */
38
+ readWaiverVersion(): number;
39
+ /** Write an acceptance and return what landed on disk. */
40
+ saveWaiverVersion(version: number): number;
41
+ /** The live posture: `paused` is derived from it by `redTeamPaused` and nowhere else. */
42
+ getPosture(): RedTeamPosture;
43
+ /** A turn or a compaction is in flight (arming is deferred, disarming is not - D-12). */
44
+ isRunning(): boolean;
45
+ /** Re-render the system prompt (the controller's single rebuild funnel). */
46
+ rebuildPrompt(): void;
47
+ log(event: RedTeamLogEvent): void;
48
+ /** The clause-set version this build asks for; injectable for the version-scope tests. */
49
+ disclaimerVersion?: number;
50
+ }
51
+ export type ScopeSetResult = {
52
+ ok: true;
53
+ scope: string | null;
54
+ } | {
55
+ ok: false;
56
+ reason: 'too-long' | 'multi-line';
57
+ };
58
+ export declare class RedTeamWiring {
59
+ private readonly deps;
60
+ private armed;
61
+ private scope;
62
+ private pending;
63
+ private view;
64
+ private lastEmitted;
65
+ private readonly listeners;
66
+ constructor(deps: RedTeamWiringDeps);
67
+ /** The disclaimer version this build's clause set carries. */
68
+ get disclaimerVersion(): number;
69
+ /** Does this session have the chain at all? Config, flag and env all fold in here. */
70
+ available(): boolean;
71
+ /** The cached view: recomputed when something actually changes, never during render. */
72
+ getView(): RedTeamView;
73
+ /** Take the baseline view once the controller is fully built. */
74
+ prime(): void;
75
+ /**
76
+ * Plan and carry out one request. The outcome tells the caller what to say; a
77
+ * `refusal: 'disclaimer'` is also the caller's cue to open the waiver overlay (4.3).
78
+ */
79
+ request(req: RedTeamRequest): RedTeamOutcome;
80
+ /** Land a deferred change at `agent_end`. `null` = nothing was pending or it was a no-op. */
81
+ applyPending(): RedTeamOutcome | null;
82
+ /** A posture change can only move `paused`; the prompt rebuild is the caller's job. */
83
+ onPostureChange(): void;
84
+ /** The engagement scope, or `null`. Never persisted, never logged (D-9). */
85
+ getScope(): string | null;
86
+ /**
87
+ * Set or clear the scope. Refuses rather than truncating (4.6 step 4): a user who typed 300
88
+ * cells must not be told they set 200.
89
+ */
90
+ setScope(line: string): ScopeSetResult;
91
+ /**
92
+ * THE ONE PREDICATE THE CONFIRM GATE READS (I-5).
93
+ *
94
+ * Effective AND configured: `paused` and `bypassConfirm: false` both read false here, so an
95
+ * operator can have "red team prompt plus ordinary confirmations" (4.5 point 4).
96
+ */
97
+ bypassesConfirmation(): boolean;
98
+ /** The lead's `<redteam_mode>` block, or `''` when the mode is off (I-1 byte-identity). */
99
+ block(): string;
100
+ /** The subagent's trimmed block. Same arming condition, so a child can never out-run the lead. */
101
+ childBlock(): string;
102
+ /**
103
+ * Accept the waiver and REPORT WHAT WAS WRITTEN, read back from disk.
104
+ *
105
+ * `askEveryArm` re-askings go through this same call, which is what makes a re-acceptance
106
+ * idempotent: the recorded version does not move (RV-21).
107
+ */
108
+ acceptWaiver(): number;
109
+ /** The waiver is current for this build (the fact `/redteam status` reports). */
110
+ waiverAccepted(): boolean;
111
+ /** Whether the state machine should ask again before arming. */
112
+ private needsWaiver;
113
+ /**
114
+ * Adopt the state a saved session carried (4.10 / I-12).
115
+ *
116
+ * TWO OUTCOMES AND BOTH ARE REPORTED: a current waiver restores `armed` and reprints the
117
+ * banner (the user has to know what posture they are in), and a stale waiver leaves the mode
118
+ * OFF with a notice that says why and how to fix it. The scope is never restored - it belongs
119
+ * to one engagement (D-9).
120
+ */
121
+ restoreFromSession(saved: {
122
+ armed?: boolean;
123
+ waiverVersion?: number;
124
+ }): {
125
+ armed: boolean;
126
+ notice: string | null;
127
+ };
128
+ /** Arm directly, bypassing the state machine: the `--redteam` launch path, already checked. */
129
+ armForLaunch(): void;
130
+ /** The armed/disarmed split for the session file (4.10). */
131
+ isArmed(): boolean;
132
+ subscribe(listener: () => void): () => void;
133
+ /** Recompute the view and tell subscribers only when it really changed. */
134
+ emitIfChanged(): void;
135
+ /** The scope's LENGTH for a log line, or `null` when none is set. Never the text (D-9). */
136
+ private scopeCells;
137
+ private config;
138
+ private state;
139
+ private execute;
140
+ private computeView;
141
+ }
142
+ /**
143
+ * The waiver READER, behind a guard that has a real job.
144
+ *
145
+ * `state.json` is the only source of an acceptance (I-3) and `getRedTeamWaiverVersion` is total.
146
+ * The case this defends is the other one: several App-level tests replace `config/ui-state.js`
147
+ * with a PARTIAL mock, and a named import the mock does not provide is `undefined` at call time
148
+ * - which would crash the controller CONSTRUCTOR for a feature those tests are not about.
149
+ *
150
+ * An unreadable version reads as `0` ("never accepted"), which is the safe direction: it can
151
+ * never arm a session, only fail to.
152
+ */
153
+ export declare function readWaiverVersionPort(read: () => number): number;
154
+ /** The same guard on the write side; it returns what the store reports, or 0. */
155
+ export declare function writeWaiverVersionPort(write: (version: number) => number, version: number): number;
156
+ /** The transcript banner rows for an armed session (4.6). */
157
+ export declare function bannerConfirmations(config: {
158
+ bypassConfirm?: boolean;
159
+ }, confirmTools: boolean): 'off' | 'on' | 'none';
@@ -0,0 +1,349 @@
1
+ /**
2
+ * RedTeamWiring - the stateful half of red team / security mode (4.4).
3
+ *
4
+ * ASCII ONLY (`agent/` is inside the glyph scanner's scope).
5
+ *
6
+ * The controller is far over the repo's size limit, so the mode's state and rules live here and
7
+ * the controller only forwards, the same shape as `TeamModeWiring` and `GovernanceWiring`. The
8
+ * DECISIONS are not here either: `planRedTeam` (tui/model/redteam/redteam-mode.ts) is a pure
9
+ * function, and `redTeamPaused` is the ONE definition of "a stricter posture holds this back"
10
+ * (I-10). This class holds the four things a pure function cannot: `armed`, `scope`, `pending`
11
+ * and the subscription that tells a surface to repaint.
12
+ *
13
+ * THE PERMISSION SURFACE IS ONE METHOD (I-5): `bypassesConfirmation()`. It is read by exactly
14
+ * two call sites - `tools/index.ts`'s confirm wrapper (the lead) and `team/subagent.ts` (a
15
+ * child, through a predicate the controller passes down). Nothing else may consult it, and a
16
+ * later feature that makes plan mode or governance read it would be turning a liability mode
17
+ * into a back door (R-4).
18
+ *
19
+ * THE WAIVER IS READ, NEVER INFERRED (I-3 / I-4): `waiverVersion()` comes from `state.json`
20
+ * through an injected reader, and nothing in `config.json` or the environment can express
21
+ * "accepted".
22
+ */
23
+ import { REDTEAM_DISCLAIMER_VERSION } from '@aragon-agent/tui/model/redteam/limits';
24
+ import { applyPendingRedTeam, planRedTeam, redTeamPaused, } from '@aragon-agent/tui/model/redteam/redteam-mode';
25
+ import { normalizeScope, scopeCells } from '@aragon-agent/tui/model/redteam/scope';
26
+ import { REDTEAM_NOTICE } from '@aragon-agent/tui/ui/redteam-copy';
27
+ import { buildRedTeamBlock } from './redteam-prompt.js';
28
+ /** What a `--no-redteam` / `ARAGON_REDTEAM=0` / `redteam.enabled=false` session looks like. */
29
+ export const UNAVAILABLE_RED_TEAM_VIEW = Object.freeze({
30
+ available: false,
31
+ armed: false,
32
+ paused: false,
33
+ effective: 'off',
34
+ scope: null,
35
+ bypassesConfirm: false,
36
+ waiverAccepted: false,
37
+ pending: null,
38
+ });
39
+ export class RedTeamWiring {
40
+ deps;
41
+ armed = false;
42
+ scope = null;
43
+ pending = null;
44
+ view = null;
45
+ lastEmitted = null;
46
+ listeners = new Set();
47
+ constructor(deps) {
48
+ this.deps = deps;
49
+ }
50
+ /** The disclaimer version this build's clause set carries. */
51
+ get disclaimerVersion() {
52
+ return this.deps.disclaimerVersion ?? REDTEAM_DISCLAIMER_VERSION;
53
+ }
54
+ /** Does this session have the chain at all? Config, flag and env all fold in here. */
55
+ available() {
56
+ if (!this.deps.isAvailable())
57
+ return false;
58
+ return this.config().enabled !== false;
59
+ }
60
+ /** The cached view: recomputed when something actually changes, never during render. */
61
+ getView() {
62
+ if (this.view === null)
63
+ this.prime();
64
+ return this.view;
65
+ }
66
+ /** Take the baseline view once the controller is fully built. */
67
+ prime() {
68
+ this.view = this.computeView();
69
+ this.lastEmitted = JSON.stringify(this.view);
70
+ }
71
+ /**
72
+ * Plan and carry out one request. The outcome tells the caller what to say; a
73
+ * `refusal: 'disclaimer'` is also the caller's cue to open the waiver overlay (4.3).
74
+ */
75
+ request(req) {
76
+ const plan = planRedTeam(this.state(), req);
77
+ this.execute(plan.action);
78
+ this.emitIfChanged();
79
+ return plan.outcome;
80
+ }
81
+ /** Land a deferred change at `agent_end`. `null` = nothing was pending or it was a no-op. */
82
+ applyPending() {
83
+ const plan = applyPendingRedTeam(this.state());
84
+ if (!plan)
85
+ return null;
86
+ this.execute(plan.action);
87
+ this.emitIfChanged();
88
+ return plan.outcome;
89
+ }
90
+ /** A posture change can only move `paused`; the prompt rebuild is the caller's job. */
91
+ onPostureChange() {
92
+ this.emitIfChanged();
93
+ }
94
+ /** The engagement scope, or `null`. Never persisted, never logged (D-9). */
95
+ getScope() {
96
+ return this.scope;
97
+ }
98
+ /**
99
+ * Set or clear the scope. Refuses rather than truncating (4.6 step 4): a user who typed 300
100
+ * cells must not be told they set 200.
101
+ */
102
+ setScope(line) {
103
+ const normalized = normalizeScope(line);
104
+ if (!normalized.ok)
105
+ return { ok: false, reason: normalized.reason };
106
+ this.scope = normalized.scope.length > 0 ? normalized.scope : null;
107
+ // THE LENGTH, NEVER THE TEXT (R-12): a target name can itself be sensitive.
108
+ this.deps.log({ type: 'redteam.scope_set', cells: scopeCells(this.scope ?? '') });
109
+ this.deps.rebuildPrompt();
110
+ this.emitIfChanged();
111
+ return { ok: true, scope: this.scope };
112
+ }
113
+ /**
114
+ * THE ONE PREDICATE THE CONFIRM GATE READS (I-5).
115
+ *
116
+ * Effective AND configured: `paused` and `bypassConfirm: false` both read false here, so an
117
+ * operator can have "red team prompt plus ordinary confirmations" (4.5 point 4).
118
+ */
119
+ bypassesConfirmation() {
120
+ if (!this.available() || !this.armed)
121
+ return false;
122
+ if (redTeamPaused(this.deps.getPosture()))
123
+ return false;
124
+ return this.config().bypassConfirm !== false;
125
+ }
126
+ /** The lead's `<redteam_mode>` block, or `''` when the mode is off (I-1 byte-identity). */
127
+ block() {
128
+ if (!this.available() || !this.armed)
129
+ return '';
130
+ return buildRedTeamBlock({
131
+ scope: this.scope,
132
+ paused: redTeamPaused(this.deps.getPosture()),
133
+ postureLabel: postureLabel(this.deps.getPosture()),
134
+ role: 'lead',
135
+ });
136
+ }
137
+ /** The subagent's trimmed block. Same arming condition, so a child can never out-run the lead. */
138
+ childBlock() {
139
+ if (!this.available() || !this.armed)
140
+ return '';
141
+ return buildRedTeamBlock({
142
+ scope: this.scope,
143
+ paused: redTeamPaused(this.deps.getPosture()),
144
+ postureLabel: postureLabel(this.deps.getPosture()),
145
+ role: 'child',
146
+ });
147
+ }
148
+ /**
149
+ * Accept the waiver and REPORT WHAT WAS WRITTEN, read back from disk.
150
+ *
151
+ * `askEveryArm` re-askings go through this same call, which is what makes a re-acceptance
152
+ * idempotent: the recorded version does not move (RV-21).
153
+ */
154
+ acceptWaiver() {
155
+ const landed = this.deps.saveWaiverVersion(this.disclaimerVersion);
156
+ this.deps.log({ type: 'redteam.waiver_accepted', version: landed });
157
+ this.emitIfChanged();
158
+ return landed;
159
+ }
160
+ /** The waiver is current for this build (the fact `/redteam status` reports). */
161
+ waiverAccepted() {
162
+ return this.deps.readWaiverVersion() >= this.disclaimerVersion;
163
+ }
164
+ /** Whether the state machine should ask again before arming. */
165
+ needsWaiver() {
166
+ if (this.config().askEveryArm === true)
167
+ return true;
168
+ return !this.waiverAccepted();
169
+ }
170
+ /**
171
+ * Adopt the state a saved session carried (4.10 / I-12).
172
+ *
173
+ * TWO OUTCOMES AND BOTH ARE REPORTED: a current waiver restores `armed` and reprints the
174
+ * banner (the user has to know what posture they are in), and a stale waiver leaves the mode
175
+ * OFF with a notice that says why and how to fix it. The scope is never restored - it belongs
176
+ * to one engagement (D-9).
177
+ */
178
+ restoreFromSession(saved) {
179
+ const wanted = saved.armed === true;
180
+ const fileVersion = typeof saved.waiverVersion === 'number' ? saved.waiverVersion : 0;
181
+ if (!wanted || !this.available())
182
+ return { armed: false, notice: null };
183
+ // STALE MEANS THE FILE IS OLDER THAN THE CLAUSE SET THIS BUILD ASKS FOR (RV-20 / I-12).
184
+ // A file armed under version 1 must not silently re-arm under version 2: the user signed
185
+ // six clauses they can no longer see, so the mode comes back OFF and says why.
186
+ if (fileVersion < this.disclaimerVersion) {
187
+ this.deps.log({ type: 'redteam.resume_restored', armed: false, reason: 'stale_waiver' });
188
+ return { armed: false, notice: REDTEAM_NOTICE.resumeStaleWaiver };
189
+ }
190
+ // AND THE MACHINE HAS TO AGREE (I-3). A session file is a document the user can copy
191
+ // between machines; the acceptance it names is a MACHINE fact (`state.json`), and without a
192
+ // current record here there is no waiver to restore - only a file that says there was one.
193
+ if (!this.waiverAccepted()) {
194
+ this.deps.log({ type: 'redteam.resume_restored', armed: false, reason: 'no_record' });
195
+ return { armed: false, notice: REDTEAM_NOTICE.resumeNoRecord };
196
+ }
197
+ this.armed = true;
198
+ this.pending = null;
199
+ this.deps.log({ type: 'redteam.resume_restored', armed: true, reason: 'ok' });
200
+ this.deps.rebuildPrompt();
201
+ this.emitIfChanged();
202
+ return { armed: true, notice: REDTEAM_NOTICE.resumeRestored };
203
+ }
204
+ /** Arm directly, bypassing the state machine: the `--redteam` launch path, already checked. */
205
+ armForLaunch() {
206
+ if (!this.available())
207
+ return;
208
+ this.armed = true;
209
+ this.pending = null;
210
+ this.deps.log({ type: 'redteam.arm', scopeCells: this.scopeCells(), deferred: false });
211
+ this.deps.rebuildPrompt();
212
+ this.emitIfChanged();
213
+ }
214
+ /** The armed/disarmed split for the session file (4.10). */
215
+ isArmed() {
216
+ return this.armed;
217
+ }
218
+ subscribe(listener) {
219
+ this.listeners.add(listener);
220
+ return () => { this.listeners.delete(listener); };
221
+ }
222
+ /** Recompute the view and tell subscribers only when it really changed. */
223
+ emitIfChanged() {
224
+ this.view = this.computeView();
225
+ const key = JSON.stringify(this.view);
226
+ if (key === this.lastEmitted)
227
+ return;
228
+ this.lastEmitted = key;
229
+ for (const listener of [...this.listeners]) {
230
+ try {
231
+ listener();
232
+ }
233
+ catch { /* a repaint hook must not break a mode change */ }
234
+ }
235
+ }
236
+ /** The scope's LENGTH for a log line, or `null` when none is set. Never the text (D-9). */
237
+ scopeCells() {
238
+ return this.scope === null ? null : scopeCells(this.scope);
239
+ }
240
+ config() {
241
+ return this.deps.getConfig().redteam ?? DEFAULT_RED_TEAM_CONFIG;
242
+ }
243
+ state() {
244
+ return {
245
+ available: this.available(),
246
+ armed: this.armed,
247
+ waiverAccepted: !this.needsWaiver(),
248
+ running: this.deps.isRunning(),
249
+ posture: this.deps.getPosture(),
250
+ pending: this.pending,
251
+ };
252
+ }
253
+ execute(action) {
254
+ switch (action.type) {
255
+ case 'none':
256
+ return;
257
+ case 'arm':
258
+ this.armed = true;
259
+ this.pending = null;
260
+ this.deps.log({ type: 'redteam.arm', scopeCells: this.scopeCells(), deferred: false });
261
+ this.deps.rebuildPrompt();
262
+ return;
263
+ case 'disarm':
264
+ if (!this.armed && this.pending === null)
265
+ return;
266
+ this.armed = false;
267
+ this.pending = null;
268
+ this.deps.log({ type: 'redteam.disarm' });
269
+ this.deps.rebuildPrompt();
270
+ return;
271
+ case 'defer':
272
+ // NO prompt rebuild: this turn's tool array and system prompt are already assembled,
273
+ // and rewriting them mid-turn is exactly what deferring avoids (D-12).
274
+ this.pending = action.target;
275
+ return;
276
+ }
277
+ }
278
+ computeView() {
279
+ const available = this.available();
280
+ const armed = available && this.armed;
281
+ const paused = armed && redTeamPaused(this.deps.getPosture());
282
+ return {
283
+ available,
284
+ armed,
285
+ paused,
286
+ effective: paused ? 'paused' : armed ? 'armed' : 'off',
287
+ scope: this.scope,
288
+ bypassesConfirm: armed && !paused && this.config().bypassConfirm !== false,
289
+ waiverAccepted: this.waiverAccepted(),
290
+ pending: this.pending,
291
+ };
292
+ }
293
+ }
294
+ /**
295
+ * The waiver READER, behind a guard that has a real job.
296
+ *
297
+ * `state.json` is the only source of an acceptance (I-3) and `getRedTeamWaiverVersion` is total.
298
+ * The case this defends is the other one: several App-level tests replace `config/ui-state.js`
299
+ * with a PARTIAL mock, and a named import the mock does not provide is `undefined` at call time
300
+ * - which would crash the controller CONSTRUCTOR for a feature those tests are not about.
301
+ *
302
+ * An unreadable version reads as `0` ("never accepted"), which is the safe direction: it can
303
+ * never arm a session, only fail to.
304
+ */
305
+ export function readWaiverVersionPort(read) {
306
+ if (typeof read !== 'function')
307
+ return 0;
308
+ try {
309
+ const version = read();
310
+ return Number.isFinite(version) && version > 0 ? Math.floor(version) : 0;
311
+ }
312
+ catch {
313
+ return 0;
314
+ }
315
+ }
316
+ /** The same guard on the write side; it returns what the store reports, or 0. */
317
+ export function writeWaiverVersionPort(write, version) {
318
+ if (typeof write !== 'function')
319
+ return 0;
320
+ try {
321
+ const landed = write(version);
322
+ return Number.isFinite(landed) ? Math.floor(landed) : 0;
323
+ }
324
+ catch {
325
+ return 0;
326
+ }
327
+ }
328
+ /** The section a hand-built `CliConfig` fixture may omit; identical to the schema default. */
329
+ const DEFAULT_RED_TEAM_CONFIG = {
330
+ enabled: true,
331
+ bypassConfirm: true,
332
+ banner: true,
333
+ askEveryArm: false,
334
+ };
335
+ /** The posture's NAME, for the prompt note. It never decides anything (I-10). */
336
+ function postureLabel(posture) {
337
+ const parts = [];
338
+ if (posture.mode === 'plan')
339
+ parts.push('PLAN');
340
+ if (posture.governOn)
341
+ parts.push('GOVERN');
342
+ return parts.length > 0 ? parts.join('+') : undefined;
343
+ }
344
+ /** The transcript banner rows for an armed session (4.6). */
345
+ export function bannerConfirmations(config, confirmTools) {
346
+ if (!confirmTools)
347
+ return 'none';
348
+ return config.bypassConfirm === false ? 'on' : 'off';
349
+ }