@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,433 @@
1
+ /**
2
+ * `GeneCatalog` - the per-process, in-memory search index over an explicit list of scopes
3
+ * (gene-bank spec 7.7, 8.2, 9.3, I-4, I-13, I-24).
4
+ *
5
+ * ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
6
+ *
7
+ * WHAT IT IS. A `GeneSearchIndex` (pure, in `tui/model/gene/search.ts`) fed from the derived index
8
+ * files of the scopes it is told about: `index/snapshot.json` + `index/journal.jsonl`. An agent
9
+ * process loads ONLY the scopes the session is authorised to see (user, the current project, the
10
+ * team scope when configured, this session's task scope) - another project's genes are never in
11
+ * memory, so no filter bug can leak them (11.1). The UI manager builds its own catalog for the
12
+ * scopes it browses.
13
+ *
14
+ * WHAT IT IS NOT. It never writes a gene file, a snapshot or a journal (I-4). Compaction is
15
+ * performed by whoever holds the library lease, through an `IndexWriter` handed to `compact()`.
16
+ *
17
+ * FAIL-OPEN (I-24). `catchUp()` never rejects. While a scope is rescanned, or before the first
18
+ * load has finished, `search()` answers with an EMPTY result flagged
19
+ * `degraded: 'index_rebuilding'` rather than a stale or partial one. A scope whose files cannot
20
+ * be read at all contributes nothing and the result carries `index_unavailable`.
21
+ *
22
+ * CHANGE NOTIFICATION. `onChange` fires when the VISIBLE gene set (published rows of the loaded
23
+ * scopes) changed during a catch-up AFTER the first load. The service uses it to mark the prompt
24
+ * context dirty for the NEXT user turn; nothing mid-run ever changes the system prompt (9.3).
25
+ */
26
+ import { scopeId, isValidScope } from '@aragon-agent/tui/model/gene/ids';
27
+ import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
28
+ import { VEC_ALGO } from '@aragon-agent/tui/model/gene/hashvec';
29
+ import { GeneSearchIndex } from '@aragon-agent/tui/model/gene/search';
30
+ import { reconcileScope, emptyScopeReconcile } from './catalog-reconcile.js';
31
+ import { StatsCache } from './catalog-stats.js';
32
+ import { FS_IO, indexPaths, newScopeState, syncScope, } from './catalog-sync.js';
33
+ import { indexFile } from './paths.js';
34
+ import { emptyCounts } from './store-port.js';
35
+ const nextTick = () => new Promise((resolve) => setImmediate(resolve));
36
+ function defaultClock() {
37
+ const perf = globalThis.performance;
38
+ return perf === undefined ? () => Date.now() : () => perf.now();
39
+ }
40
+ function emptyReport() {
41
+ return { changed: false, tailed: [], reloaded: [], rebuilt: [], failed: [], applied: 0 };
42
+ }
43
+ function degradedEmpty(reason) {
44
+ return { hits: [], total: 0, visible: 0, filtered: {}, belowThreshold: 0, degraded: reason };
45
+ }
46
+ export class GeneCatalog {
47
+ /** Exposed for `planGeneContext`, which needs the index itself, not a search call. */
48
+ index;
49
+ reader;
50
+ io;
51
+ vecAlgo;
52
+ onError;
53
+ stats;
54
+ env;
55
+ states = new Map();
56
+ rebuilding = new Set();
57
+ listeners = new Set();
58
+ scopeList = [];
59
+ pending = null;
60
+ queue = Promise.resolve();
61
+ clockScale = 1;
62
+ mutations = 0;
63
+ disposed = false;
64
+ constructor(options) {
65
+ this.reader = options.reader;
66
+ this.io = options.io ?? FS_IO;
67
+ this.vecAlgo = options.vecAlgo ?? VEC_ALGO;
68
+ this.onError = options.onError ?? (() => undefined);
69
+ const clock = options.clock ?? defaultClock();
70
+ // The index checks its budget against `clock()`. Scaling the clock is how a hint lookup gets
71
+ // a smaller budget than a tool search without a second index: the search is synchronous, so
72
+ // the scale cannot change while a run is in progress.
73
+ this.index = new GeneSearchIndex({ clock: () => clock() * this.clockScale });
74
+ this.stats = new StatsCache({
75
+ reader: this.reader,
76
+ io: this.io,
77
+ now: options.now ?? Date.now,
78
+ ttlMs: options.statsTtlMs ?? 30_000,
79
+ });
80
+ this.env = {
81
+ reader: this.reader,
82
+ io: this.io,
83
+ index: this.index,
84
+ vecAlgo: this.vecAlgo,
85
+ yieldEvery: options.yieldEvery ?? 200,
86
+ yieldTo: options.yieldTo ?? nextTick,
87
+ setRebuilding: (sid, on) => {
88
+ if (on)
89
+ this.rebuilding.add(sid);
90
+ else
91
+ this.rebuilding.delete(sid);
92
+ },
93
+ touched: () => {
94
+ this.mutations += 1;
95
+ },
96
+ };
97
+ }
98
+ // ----- scopes ------------------------------------------------------------
99
+ /**
100
+ * Replace the list of scopes this catalog serves. Rows of a scope that left the list are
101
+ * dropped at once (so a `/cwd` change cannot leave the old project's genes searchable); a new
102
+ * scope is loaded by the next `catchUp()` and meanwhile reads as `index_rebuilding`.
103
+ */
104
+ setScopes(scopes) {
105
+ const wanted = new Map();
106
+ for (const scope of scopes) {
107
+ if (isValidScope(scope))
108
+ wanted.set(scopeId(scope), scope);
109
+ }
110
+ for (const [sid, state] of this.states) {
111
+ if (wanted.has(sid))
112
+ continue;
113
+ for (const id of [...state.rowIds])
114
+ this.index.remove(id);
115
+ this.states.delete(sid);
116
+ this.rebuilding.delete(sid);
117
+ this.stats.forget(sid);
118
+ this.mutations += 1;
119
+ }
120
+ for (const [sid, scope] of wanted) {
121
+ if (!this.states.has(sid))
122
+ this.states.set(sid, newScopeState(scope));
123
+ }
124
+ this.scopeList = [...wanted.values()];
125
+ }
126
+ scopes() {
127
+ return this.scopeList;
128
+ }
129
+ /** Incremented by every change to the index or the scope list; cheap cache key for views. */
130
+ version() {
131
+ return this.mutations;
132
+ }
133
+ // ----- state -------------------------------------------------------------
134
+ /** Every wanted scope finished its first sync (a scope that failed has SETTLED, as empty). */
135
+ isLoaded() {
136
+ for (const state of this.states.values())
137
+ if (!state.loaded && !state.failed)
138
+ return false;
139
+ return true;
140
+ }
141
+ state() {
142
+ if (this.rebuilding.size > 0)
143
+ return 'rebuilding';
144
+ return this.isLoaded() ? 'ready' : 'loading';
145
+ }
146
+ failureFlag() {
147
+ for (const state of this.states.values())
148
+ if (state.failed)
149
+ return 'index_unavailable';
150
+ return undefined;
151
+ }
152
+ // ----- catch-up ----------------------------------------------------------
153
+ /**
154
+ * Bring every scope up to date. O(1) (two `stat`s per scope) when nothing changed. Concurrent
155
+ * callers share one pass; a scope added while a pass was running gets its own pass afterwards.
156
+ * Never rejects.
157
+ */
158
+ catchUp() {
159
+ if (this.pending !== null) {
160
+ return this.pending.then((report) => (this.isLoaded() ? report : this.catchUp()));
161
+ }
162
+ const run = this.exclusive(() => this.pass('auto')).finally(() => {
163
+ if (this.pending === run)
164
+ this.pending = null;
165
+ });
166
+ this.pending = run;
167
+ return run;
168
+ }
169
+ /** Forget the derived files and rescan the heads of every scope (or one). Never rejects. */
170
+ rebuild(scope) {
171
+ const only = scope === undefined ? undefined : scopeId(scope);
172
+ return this.exclusive(() => this.pass('rebuild', only));
173
+ }
174
+ exclusive(job) {
175
+ const run = this.queue.then(job, job);
176
+ this.queue = run.then(() => undefined, () => undefined);
177
+ return run;
178
+ }
179
+ async pass(mode, only) {
180
+ const report = emptyReport();
181
+ const targets = [...this.states.values()].filter((state) => only === undefined || state.sid === only);
182
+ const outcomes = await Promise.all(targets.map((state) => this.syncOne(state, mode)));
183
+ outcomes.forEach((outcome, i) => {
184
+ const sid = targets[i].sid;
185
+ if (outcome === null) {
186
+ report.failed.push(sid);
187
+ return;
188
+ }
189
+ this.tally(report, sid, outcome);
190
+ // A scope's very first load is not a "change": nobody has seen the previous state.
191
+ if (outcome.visibleChanged && !outcome.initial)
192
+ report.changed = true;
193
+ });
194
+ if (report.changed)
195
+ this.fire();
196
+ await this.refreshStats();
197
+ return report;
198
+ }
199
+ tally(report, sid, outcome) {
200
+ report.applied += outcome.applied;
201
+ if (outcome.kind === 'tailed')
202
+ report.tailed.push(sid);
203
+ if (outcome.kind === 'reloaded')
204
+ report.reloaded.push(sid);
205
+ if (outcome.kind === 'rebuilt')
206
+ report.rebuilt.push(sid);
207
+ }
208
+ async syncOne(state, mode) {
209
+ try {
210
+ return await syncScope(this.env, state, mode);
211
+ }
212
+ catch (error) {
213
+ state.failed = true;
214
+ this.onError('catalog.sync', error);
215
+ return null;
216
+ }
217
+ }
218
+ /** The first pass awaits the statistics (so the first ranking has them); later ones refresh. */
219
+ async refreshStats() {
220
+ if (!this.stats.hasWarmed()) {
221
+ await this.stats.warm(this.scopeList, scopeId);
222
+ return;
223
+ }
224
+ if (this.stats.isStale())
225
+ void this.stats.warm(this.scopeList, scopeId);
226
+ }
227
+ /** Resolves when the statistics of the loaded scopes are cached. Never rejects. */
228
+ warmStats() {
229
+ return this.stats.warm(this.scopeList, scopeId);
230
+ }
231
+ // ----- search ------------------------------------------------------------
232
+ /**
233
+ * Synchronous, never throws. `budgetMs` shrinks the CPU budget for latency-critical callers
234
+ * (the hint engine passes 8). While the catalog cannot answer honestly the result is empty and
235
+ * flagged, which every consumer renders as a polite one-liner.
236
+ */
237
+ search(opts, caller, budgetMs) {
238
+ if (this.state() !== 'ready')
239
+ return degradedEmpty('index_rebuilding');
240
+ this.clockScale =
241
+ typeof budgetMs === 'number' && budgetMs > 0 ? GENE_LIMITS.searchBudgetMs / budgetMs : 1;
242
+ try {
243
+ const result = this.index.search(opts, caller, this.statsOf);
244
+ const flag = this.failureFlag();
245
+ if (flag !== undefined && result.degraded === undefined)
246
+ result.degraded = flag;
247
+ return result;
248
+ }
249
+ finally {
250
+ this.clockScale = 1;
251
+ }
252
+ }
253
+ /** Score breakdown for the `explain` views: numbers and field names only. */
254
+ explain(row, opts, caller) {
255
+ return this.index.explain(row, opts, caller, this.statsOf(row.id));
256
+ }
257
+ /** The `StatsFn` the planner and the searcher take; reads the cache, never the disk. */
258
+ statsOf = (id) => {
259
+ const row = this.index.get(id);
260
+ return row === undefined ? undefined : this.stats.get(scopeId(row.scope), id);
261
+ };
262
+ get(id) {
263
+ return this.index.get(id);
264
+ }
265
+ rows() {
266
+ return this.index.rows();
267
+ }
268
+ size() {
269
+ return this.index.size();
270
+ }
271
+ // ----- counts ------------------------------------------------------------
272
+ /** Recomputed from the in-memory rows of one scope (`index/counts.json` is the writer's). */
273
+ countsFor(scope) {
274
+ const counts = emptyCounts(0);
275
+ const state = this.states.get(scopeId(scope));
276
+ for (const id of state?.rowIds ?? []) {
277
+ const row = this.index.get(id);
278
+ if (row === undefined)
279
+ continue;
280
+ if (row.status === 'candidate')
281
+ counts.candidates += 1;
282
+ if (row.status !== 'published')
283
+ continue;
284
+ counts.published[row.type] += 1;
285
+ if (row.pinned)
286
+ counts.pinned[row.type] += 1;
287
+ }
288
+ return counts;
289
+ }
290
+ /** `index/counts.json` when present and well-formed, else the recomputed view. */
291
+ async readCounts(scope) {
292
+ try {
293
+ const path = indexFile(scope, 'counts.json', this.reader.ctx);
294
+ const stored = path === null ? null : await this.io.readJson(path);
295
+ if (stored !== null && stored.schema === 1 && stored.published !== undefined)
296
+ return stored;
297
+ }
298
+ catch (error) {
299
+ this.onError('catalog.readCounts', error);
300
+ }
301
+ return this.countsFor(scope);
302
+ }
303
+ /** Published rows per type, candidates and pinned, over every loaded scope. */
304
+ libraryCounts() {
305
+ const totals = { knowledge: 0, tool: 0, skill: 0, candidates: 0, pinned: 0 };
306
+ for (const row of this.index.rows()) {
307
+ if (row.status === 'candidate')
308
+ totals.candidates += 1;
309
+ if (row.status !== 'published')
310
+ continue;
311
+ totals[row.type] += 1;
312
+ if (row.pinned)
313
+ totals.pinned += 1;
314
+ }
315
+ return totals;
316
+ }
317
+ // ----- reconcile / compact -----------------------------------------------
318
+ /**
319
+ * Compare head files with the index and repair differences from the heads. `deep` adds the
320
+ * stat pass (RV-8) and belongs to the lease holder; a session start does the names only.
321
+ */
322
+ reconcile(opts = {}) {
323
+ return this.exclusive(() => this.reconcileAll(opts.deep === true));
324
+ }
325
+ async reconcileAll(deep) {
326
+ const total = {
327
+ scopes: 0,
328
+ added: 0,
329
+ removed: 0,
330
+ repaired: 0,
331
+ statChecked: 0,
332
+ truncated: false,
333
+ changed: false,
334
+ };
335
+ for (const state of this.states.values()) {
336
+ let out = emptyScopeReconcile();
337
+ try {
338
+ out = await reconcileScope(this.env, state, deep);
339
+ }
340
+ catch (error) {
341
+ this.onError('catalog.reconcile', error);
342
+ continue;
343
+ }
344
+ total.scopes += 1;
345
+ total.added += out.added;
346
+ total.removed += out.removed;
347
+ total.repaired += out.repaired;
348
+ total.statChecked += out.statChecked;
349
+ total.truncated = total.truncated || out.truncated;
350
+ total.changed = total.changed || out.visibleChanged;
351
+ }
352
+ if (total.changed)
353
+ this.fire();
354
+ return total;
355
+ }
356
+ /**
357
+ * Fold the journal into a fresh snapshot through `writer`. Only scopes over the size / line
358
+ * thresholds are touched unless `force`. The catalog first catches up, so the rows it hands over
359
+ * include every journal line up to `includedOffset`.
360
+ */
361
+ async compact(writer, opts = {}) {
362
+ await this.catchUp();
363
+ return this.exclusive(() => this.compactAll(writer, opts.force === true));
364
+ }
365
+ async compactAll(writer, force) {
366
+ const report = { compacted: [], skipped: [] };
367
+ for (const state of this.states.values()) {
368
+ const due = state.journalSize >= GENE_LIMITS.journalCompactBytes ||
369
+ state.journalLines >= GENE_LIMITS.journalCompactLines;
370
+ if (!state.loaded || (!force && !due)) {
371
+ report.skipped.push(state.sid);
372
+ continue;
373
+ }
374
+ const ok = await this.compactOne(writer, state);
375
+ (ok ? report.compacted : report.skipped).push(state.sid);
376
+ }
377
+ return report;
378
+ }
379
+ async compactOne(writer, state) {
380
+ try {
381
+ const rows = [...state.rowIds]
382
+ .map((id) => this.index.get(id))
383
+ .filter((r) => r !== undefined);
384
+ const gen = await writer.compactScope({
385
+ scope: state.scope,
386
+ gen: Math.max(state.gen, 0) + 1,
387
+ vecAlgo: this.vecAlgo,
388
+ rows,
389
+ includedOffset: state.journalOffset,
390
+ });
391
+ if (gen === null)
392
+ return false;
393
+ state.gen = gen;
394
+ state.journalOffset = 0;
395
+ state.journalSize = 0;
396
+ state.journalLines = 0;
397
+ const paths = indexPaths(this.env, state.scope);
398
+ state.snapStamp = paths === null ? null : await this.io.stat(paths.snapshot);
399
+ return true;
400
+ }
401
+ catch (error) {
402
+ this.onError('catalog.compact', error);
403
+ return false;
404
+ }
405
+ }
406
+ /** Test aid: the snapshot generation currently loaded for a scope (-1 = rebuilt / none). */
407
+ generationOf(scope) {
408
+ return this.states.get(scopeId(scope))?.gen ?? -1;
409
+ }
410
+ // ----- change events -----------------------------------------------------
411
+ onChange(listener) {
412
+ this.listeners.add(listener);
413
+ return () => {
414
+ this.listeners.delete(listener);
415
+ };
416
+ }
417
+ fire() {
418
+ if (this.disposed)
419
+ return;
420
+ for (const listener of [...this.listeners]) {
421
+ try {
422
+ listener();
423
+ }
424
+ catch {
425
+ // One broken subscriber must not stop the others, and must not fail a catch-up.
426
+ }
427
+ }
428
+ }
429
+ dispose() {
430
+ this.disposed = true;
431
+ this.listeners.clear();
432
+ }
433
+ }
@@ -0,0 +1,50 @@
1
+ import type { Command } from 'commander';
2
+ import type { GeneRowView } from '@aragon-agent/tui/model/gene/port';
3
+ import type { GeneScope, GeneStatus, GeneType } from '@aragon-agent/tui/model/gene/types';
4
+ import { type GeneManager } from './manager.js';
5
+ export interface GeneCliOptions {
6
+ type?: string;
7
+ scope?: string;
8
+ status?: string;
9
+ json?: boolean;
10
+ tier?: string;
11
+ asCandidates?: boolean;
12
+ window?: string;
13
+ repair?: boolean;
14
+ force?: boolean;
15
+ lang?: string;
16
+ yes?: boolean;
17
+ limit?: string;
18
+ project?: boolean;
19
+ out?: string;
20
+ }
21
+ /** Everything a test replaces; production passes nothing. */
22
+ export interface GeneCliDeps {
23
+ /** Library root override. */
24
+ root?: string;
25
+ cwd?: string;
26
+ out?: (text: string) => void;
27
+ err?: (text: string) => void;
28
+ /** Use this manager instead of building one (tests that need a probe seam). */
29
+ manager?: GeneManager;
30
+ }
31
+ export declare function registerGeneCliCommand(program: Command): void;
32
+ /** The stable JSON shape of a row. */
33
+ export interface GeneRowJson {
34
+ id: string;
35
+ type: GeneType;
36
+ scope: GeneScope;
37
+ status: GeneStatus;
38
+ health: string;
39
+ trust: string;
40
+ class: string;
41
+ pinned: boolean;
42
+ version: number;
43
+ name: string;
44
+ purpose: string;
45
+ trigger: string;
46
+ updatedAt: number;
47
+ }
48
+ export declare function rowJson(row: GeneRowView): GeneRowJson;
49
+ /** Run one `aragon gene` subcommand and return its exit code. Never throws. */
50
+ export declare function runGeneCliCommand(subcommand: string, args: string[], opts: GeneCliOptions, deps?: GeneCliDeps): Promise<number>;