@aitne/daemon 0.1.10 → 0.1.11

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 (305) hide show
  1. package/dist/adapters/adapter-watchdog.d.ts +70 -0
  2. package/dist/adapters/adapter-watchdog.js +115 -0
  3. package/dist/adapters/discord.d.ts +17 -1
  4. package/dist/adapters/discord.js +33 -0
  5. package/dist/adapters/notification-manager.d.ts +27 -1
  6. package/dist/adapters/notification-manager.js +54 -39
  7. package/dist/adapters/slack-adapter.d.ts +26 -1
  8. package/dist/adapters/slack-adapter.js +41 -0
  9. package/dist/adapters/telegram-adapter.d.ts +18 -1
  10. package/dist/adapters/telegram-adapter.js +41 -2
  11. package/dist/adapters/types.d.ts +20 -0
  12. package/dist/adapters/whatsapp-adapter.d.ts +26 -7
  13. package/dist/adapters/whatsapp-adapter.js +74 -21
  14. package/dist/api/env-writer.js +8 -5
  15. package/dist/api/helpers/agent-errors-registry.d.ts +5 -5
  16. package/dist/api/helpers/agent-errors-registry.js +5 -5
  17. package/dist/api/routes/agent.js +33 -12
  18. package/dist/api/routes/agents/index.js +75 -16
  19. package/dist/api/routes/agents/views.d.ts +37 -2
  20. package/dist/api/routes/agents/views.js +64 -2
  21. package/dist/api/routes/background-task.d.ts +22 -0
  22. package/dist/api/routes/background-task.js +338 -0
  23. package/dist/api/routes/browser-history.js +9 -1
  24. package/dist/api/routes/context/permissions.js +3 -2
  25. package/dist/api/routes/context/snapshots.js +0 -3
  26. package/dist/api/routes/context/write.js +3 -17
  27. package/dist/api/routes/dashboard/config.js +48 -12
  28. package/dist/api/routes/dashboard/cost-approvals.js +66 -0
  29. package/dist/api/routes/dashboard/notifications.js +9 -9
  30. package/dist/api/routes/integrations/crud-patch.js +5 -1
  31. package/dist/api/routes/integrations-reconcile.js +2 -2
  32. package/dist/api/routes/notion.d.ts +1 -1
  33. package/dist/api/routes/observations.js +7 -7
  34. package/dist/api/routes/obsidian.d.ts +1 -1
  35. package/dist/api/routes/receipts.js +5 -1
  36. package/dist/api/routes/setup-migrate.js +1 -1
  37. package/dist/api/routes/setup.js +1 -1
  38. package/dist/api/routes/task-flows.d.ts +1 -1
  39. package/dist/api/routes/task-flows.js +1 -1
  40. package/dist/api/routes/tuning.d.ts +29 -0
  41. package/dist/api/routes/tuning.js +304 -0
  42. package/dist/api/server.d.ts +44 -16
  43. package/dist/api/server.js +9 -0
  44. package/dist/bootstrap/adapters.d.ts +19 -0
  45. package/dist/bootstrap/adapters.js +61 -0
  46. package/dist/bootstrap/api.d.ts +5 -3
  47. package/dist/bootstrap/api.js +45 -13
  48. package/dist/bootstrap/catchup.d.ts +1 -1
  49. package/dist/bootstrap/catchup.js +11 -11
  50. package/dist/bootstrap/event-pipeline.d.ts +11 -0
  51. package/dist/bootstrap/event-pipeline.js +245 -7
  52. package/dist/bootstrap/observers.js +9 -6
  53. package/dist/bootstrap/schedule-helpers.d.ts +104 -6
  54. package/dist/bootstrap/schedule-helpers.js +172 -19
  55. package/dist/config.js +26 -12
  56. package/dist/core/agent-core.d.ts +33 -1
  57. package/dist/core/agent-core.js +36 -1
  58. package/dist/core/agents/activity-scan-cadence.d.ts +103 -0
  59. package/dist/core/agents/activity-scan-cadence.js +127 -0
  60. package/dist/core/agents/agent-route-override.d.ts +53 -0
  61. package/dist/core/agents/agent-route-override.js +69 -0
  62. package/dist/core/agents/builtin-registry.d.ts +51 -14
  63. package/dist/core/agents/builtin-registry.js +92 -15
  64. package/dist/core/agents/config-gate-reconcile.d.ts +38 -0
  65. package/dist/core/agents/config-gate-reconcile.js +51 -0
  66. package/dist/core/agents/cron-substitute.d.ts +1 -1
  67. package/dist/core/agents/cron-substitute.js +1 -1
  68. package/dist/core/agents/custom-routine-migration.d.ts +60 -0
  69. package/dist/core/agents/custom-routine-migration.js +149 -0
  70. package/dist/core/agents/firing-blocked.d.ts +1 -1
  71. package/dist/core/agents/hourly-cadence.d.ts +102 -0
  72. package/dist/core/agents/hourly-cadence.js +126 -0
  73. package/dist/core/agents/loader-boot.js +23 -0
  74. package/dist/core/agents/loader.d.ts +19 -0
  75. package/dist/core/agents/loader.js +34 -2
  76. package/dist/core/agents/override-merge.d.ts +1 -1
  77. package/dist/core/agents/override-merge.js +9 -1
  78. package/dist/core/agents/recurrence-convert.d.ts +1 -1
  79. package/dist/core/agents/recurrence-convert.js +1 -1
  80. package/dist/core/agents/recurring-schedule-adapter.js +8 -0
  81. package/dist/core/alerts.js +6 -6
  82. package/dist/core/backends/auth-health-monitor.d.ts +2 -2
  83. package/dist/core/backends/auth-health-monitor.js +1 -1
  84. package/dist/core/backends/backend-router.d.ts +27 -1
  85. package/dist/core/backends/backend-router.js +165 -1
  86. package/dist/core/backends/claude-code-core.d.ts +71 -31
  87. package/dist/core/backends/claude-code-core.js +282 -54
  88. package/dist/core/backends/cli-quota-guards.d.ts +29 -1
  89. package/dist/core/backends/cli-quota-guards.js +40 -5
  90. package/dist/core/backends/codex-core.d.ts +6 -0
  91. package/dist/core/backends/codex-core.js +22 -6
  92. package/dist/core/backends/failure-spend.d.ts +58 -0
  93. package/dist/core/backends/failure-spend.js +137 -0
  94. package/dist/core/backends/gemini-cli-core.d.ts +6 -0
  95. package/dist/core/backends/gemini-cli-core.js +25 -6
  96. package/dist/core/backends/model-registry.d.ts +1 -1
  97. package/dist/core/backends/model-registry.js +4 -4
  98. package/dist/core/backends/opencode-core.d.ts +1 -1
  99. package/dist/core/backends/opencode-core.js +5 -5
  100. package/dist/core/backends/plan-presets.js +39 -15
  101. package/dist/core/bang-commands/commands-cost.js +3 -1
  102. package/dist/core/bang-commands/commands-report.js +4 -3
  103. package/dist/core/bang-commands/commands-research.js +4 -1
  104. package/dist/core/bang-commands/commands-revert-tuning.d.ts +18 -0
  105. package/dist/core/bang-commands/commands-revert-tuning.js +63 -0
  106. package/dist/core/bang-commands/commands-stop-start.js +3 -3
  107. package/dist/core/bang-commands/commands-task-control.d.ts +19 -0
  108. package/dist/core/bang-commands/commands-task-control.js +147 -0
  109. package/dist/core/bang-commands/commands-wiki.js +5 -5
  110. package/dist/core/bang-commands/index.d.ts +2 -0
  111. package/dist/core/bang-commands/index.js +12 -0
  112. package/dist/core/bang-commands/registry.d.ts +12 -0
  113. package/dist/core/browser-history/research-cluster-fanout.d.ts +28 -14
  114. package/dist/core/browser-history/research-cluster-fanout.js +39 -16
  115. package/dist/core/channel-timeline.d.ts +5 -1
  116. package/dist/core/channel-timeline.js +13 -0
  117. package/dist/core/context/index-reconciler.js +5 -2
  118. package/dist/core/context/policy-index-reconciler.d.ts +6 -4
  119. package/dist/core/context/policy-index-runner.js +25 -6
  120. package/dist/core/context-builder-calendar.js +10 -2
  121. package/dist/core/context-builder-conversation.d.ts +8 -1
  122. package/dist/core/context-builder-conversation.js +41 -7
  123. package/dist/core/context-builder-yesterday.js +4 -3
  124. package/dist/core/context-builder.d.ts +7 -2
  125. package/dist/core/context-builder.js +62 -20
  126. package/dist/core/context-file-serializer.d.ts +1 -1
  127. package/dist/core/context-file-serializer.js +1 -1
  128. package/dist/core/context-health.js +2 -2
  129. package/dist/core/context-paths.d.ts +1 -1
  130. package/dist/core/context-paths.js +1 -1
  131. package/dist/core/context-validation/prepare-write.js +1 -1
  132. package/dist/core/context-validation/routine-rulebook.d.ts +1 -1
  133. package/dist/core/context-vault-aliases.d.ts +0 -13
  134. package/dist/core/context-vault-aliases.js +37 -0
  135. package/dist/core/custom-routines.d.ts +99 -0
  136. package/dist/core/custom-routines.js +187 -0
  137. package/dist/core/daemon-api-cli.js +49 -0
  138. package/dist/core/day-boundary.d.ts +46 -0
  139. package/dist/core/day-boundary.js +40 -0
  140. package/dist/core/dispatcher-activity-scan.d.ts +221 -0
  141. package/dist/core/dispatcher-activity-scan.js +775 -0
  142. package/dist/core/dispatcher-error-handling.d.ts +6 -11
  143. package/dist/core/dispatcher-error-handling.js +38 -62
  144. package/dist/core/dispatcher-hourly-check.js +6 -1
  145. package/dist/core/dispatcher-message-handler.d.ts +10 -0
  146. package/dist/core/dispatcher-message-handler.js +17 -0
  147. package/dist/core/dispatcher-morning-routine.d.ts +6 -6
  148. package/dist/core/dispatcher-morning-routine.js +13 -13
  149. package/dist/core/dispatcher-result-processor.d.ts +33 -0
  150. package/dist/core/dispatcher-result-processor.js +167 -11
  151. package/dist/core/dispatcher-scheduled-background-task.d.ts +42 -0
  152. package/dist/core/dispatcher-scheduled-background-task.js +89 -0
  153. package/dist/core/dispatcher-scheduled-tasks.d.ts +63 -1
  154. package/dist/core/dispatcher-scheduled-tasks.js +213 -6
  155. package/dist/core/dispatcher-task-delivery.d.ts +105 -0
  156. package/dist/core/dispatcher-task-delivery.js +555 -0
  157. package/dist/core/dispatcher-types.d.ts +48 -9
  158. package/dist/core/dispatcher-types.js +3 -3
  159. package/dist/core/dispatcher.d.ts +112 -31
  160. package/dist/core/dispatcher.js +284 -59
  161. package/dist/core/dm-freshness-metrics.d.ts +1 -1
  162. package/dist/core/drift-effects.js +2 -2
  163. package/dist/core/feedback/consolidation-prep.js +17 -5
  164. package/dist/core/feedback/eviction-scorer.js +6 -2
  165. package/dist/core/feedback/lesson-format.js +9 -4
  166. package/dist/core/feedback/lesson-injection.d.ts +1 -1
  167. package/dist/core/feedback/lesson-injection.js +17 -2
  168. package/dist/core/feedback/lesson-store-overview.d.ts +8 -4
  169. package/dist/core/feedback/lesson-store-overview.js +8 -4
  170. package/dist/core/feedback/regeneralization-prep.js +29 -16
  171. package/dist/core/feedback/self-performance-prep.d.ts +186 -0
  172. package/dist/core/feedback/self-performance-prep.js +541 -0
  173. package/dist/core/feedback/tuning-actuator.d.ts +198 -0
  174. package/dist/core/feedback/tuning-actuator.js +432 -0
  175. package/dist/core/feedback/tuning-recommender.d.ts +247 -0
  176. package/dist/core/feedback/tuning-recommender.js +580 -0
  177. package/dist/core/feedback/tuning-revert-monitor.d.ts +90 -0
  178. package/dist/core/feedback/tuning-revert-monitor.js +213 -0
  179. package/dist/core/health-monitor.d.ts +6 -0
  180. package/dist/core/health-monitor.js +1 -1
  181. package/dist/core/injection-policy.d.ts +4 -4
  182. package/dist/core/injection-policy.js +4 -4
  183. package/dist/core/integration-main-backend.js +4 -0
  184. package/dist/core/management-md.d.ts +2 -2
  185. package/dist/core/management-md.js +51 -13
  186. package/dist/core/morning/orchestrator.d.ts +2 -2
  187. package/dist/core/morning/orchestrator.js +2 -2
  188. package/dist/core/notification-gate.d.ts +64 -0
  189. package/dist/core/notification-gate.js +51 -0
  190. package/dist/core/notification-rate-limit.d.ts +40 -0
  191. package/dist/core/notification-rate-limit.js +50 -0
  192. package/dist/core/policy-files.d.ts +1 -1
  193. package/dist/core/policy-files.js +2 -2
  194. package/dist/core/pre-pass-freshness.d.ts +4 -4
  195. package/dist/core/retention.d.ts +5 -0
  196. package/dist/core/retention.js +20 -4
  197. package/dist/core/review-context.d.ts +1 -1
  198. package/dist/core/review-context.js +10 -5
  199. package/dist/core/roadmap-write-lock.d.ts +2 -1
  200. package/dist/core/roadmap-write-lock.js +15 -10
  201. package/dist/core/routine-acquisition-plan.d.ts +47 -1
  202. package/dist/core/routine-acquisition-plan.js +78 -20
  203. package/dist/core/routine-fetch-window-retry.js +7 -4
  204. package/dist/core/routine-fetch-window-runner.d.ts +39 -3
  205. package/dist/core/routine-fetch-window-runner.js +264 -13
  206. package/dist/core/routine-windows.d.ts +2 -2
  207. package/dist/core/routine-windows.js +8 -5
  208. package/dist/core/scheduler.d.ts +175 -16
  209. package/dist/core/scheduler.js +559 -102
  210. package/dist/core/signal-detector.d.ts +12 -0
  211. package/dist/core/signal-detector.js +53 -9
  212. package/dist/core/skills-compiler-denied-tools.js +2 -2
  213. package/dist/core/skills-compiler-skill-index.d.ts +2 -2
  214. package/dist/core/skills-compiler-skill-index.js +2 -2
  215. package/dist/core/skills-compiler-variants.d.ts +1 -1
  216. package/dist/core/skills-compiler-variants.js +8 -0
  217. package/dist/core/skills-compiler.d.ts +29 -26
  218. package/dist/core/skills-compiler.js +117 -81
  219. package/dist/core/skills-manifest.d.ts +37 -0
  220. package/dist/core/skills-manifest.js +73 -2
  221. package/dist/core/sleep-inhibitor.d.ts +79 -0
  222. package/dist/core/sleep-inhibitor.js +132 -0
  223. package/dist/core/slim-system-prompt-loader.d.ts +77 -0
  224. package/dist/core/slim-system-prompt-loader.js +141 -0
  225. package/dist/core/spawn-gates.d.ts +126 -0
  226. package/dist/core/spawn-gates.js +180 -0
  227. package/dist/core/today-direct-writer.d.ts +2 -2
  228. package/dist/core/today-direct-writer.js +1 -1
  229. package/dist/core/today-write-lock.d.ts +4 -2
  230. package/dist/core/today-write-lock.js +30 -20
  231. package/dist/core/wake-detector.d.ts +55 -0
  232. package/dist/core/wake-detector.js +80 -0
  233. package/dist/core/wiki/compile-lock.d.ts +1 -1
  234. package/dist/core/wiki/compile-lock.js +1 -1
  235. package/dist/core/workdir.js +15 -6
  236. package/dist/db/activity-scan-signals.d.ts +77 -0
  237. package/dist/db/activity-scan-signals.js +378 -0
  238. package/dist/db/agents-store.d.ts +28 -0
  239. package/dist/db/agents-store.js +62 -0
  240. package/dist/db/background-task-clarifications-store.d.ts +81 -0
  241. package/dist/db/background-task-clarifications-store.js +152 -0
  242. package/dist/db/background-task-store.d.ts +207 -0
  243. package/dist/db/background-task-store.js +380 -0
  244. package/dist/db/browser-history-store.d.ts +39 -6
  245. package/dist/db/browser-history-store.js +51 -7
  246. package/dist/db/browser-task-clarifications-store.d.ts +12 -0
  247. package/dist/db/browser-task-clarifications-store.js +35 -5
  248. package/dist/db/browser-task-store.d.ts +3 -0
  249. package/dist/db/browser-task-store.js +29 -4
  250. package/dist/db/deferred-dm.d.ts +86 -0
  251. package/dist/db/deferred-dm.js +199 -0
  252. package/dist/db/migrations.js +330 -0
  253. package/dist/db/observations.d.ts +2 -2
  254. package/dist/db/observations.js +3 -3
  255. package/dist/db/schema.js +217 -16
  256. package/dist/db/voice-transcripts-store.d.ts +1 -1
  257. package/dist/index.js +86 -29
  258. package/dist/messaging/browser-task-mcp-notifier.d.ts +12 -70
  259. package/dist/messaging/browser-task-mcp-notifier.js +30 -151
  260. package/dist/messaging/browser-task-screenshot-attachment.d.ts +15 -0
  261. package/dist/messaging/browser-task-screenshot-attachment.js +63 -0
  262. package/dist/observers/delegated-sync-worker.d.ts +6 -6
  263. package/dist/observers/delegated-sync-worker.js +10 -10
  264. package/dist/observers/git-delegated-cron.d.ts +1 -1
  265. package/dist/observers/git-delegated-cron.js +2 -2
  266. package/dist/observers/github-poller-classifier.d.ts +3 -3
  267. package/dist/observers/github-poller-classifier.js +3 -3
  268. package/dist/observers/imminent-event-scheduler.d.ts +1 -1
  269. package/dist/observers/imminent-event-scheduler.js +1 -1
  270. package/dist/observers/mail-poller.d.ts +1 -0
  271. package/dist/observers/mail-poller.js +42 -3
  272. package/dist/observers/observation-summarizer/summarizer-client.d.ts +2 -2
  273. package/dist/observers/observation-summarizer/summarizer-client.js +2 -2
  274. package/dist/observers/observation-summarizer/worker.d.ts +2 -2
  275. package/dist/observers/observation-summarizer/worker.js +4 -4
  276. package/dist/observers/obsidian-watcher.d.ts +1 -1
  277. package/dist/observers/obsidian-watcher.js +1 -1
  278. package/dist/safety/agent-write-tracker.d.ts +4 -4
  279. package/dist/safety/agent-write-tracker.js +4 -4
  280. package/dist/safety/audit.d.ts +43 -5
  281. package/dist/safety/audit.js +86 -18
  282. package/dist/safety/risk-classifier.d.ts +6 -0
  283. package/dist/safety/risk-classifier.js +75 -11
  284. package/dist/scheduler/activity-scan-gate.d.ts +86 -0
  285. package/dist/scheduler/activity-scan-gate.js +132 -0
  286. package/dist/services/background-task/background-task-budget.d.ts +80 -0
  287. package/dist/services/background-task/background-task-budget.js +91 -0
  288. package/dist/services/background-task/background-task-driver.d.ts +105 -0
  289. package/dist/services/background-task/background-task-driver.js +416 -0
  290. package/dist/services/background-task/background-task-runner.d.ts +96 -0
  291. package/dist/services/background-task/background-task-runner.js +673 -0
  292. package/dist/services/background-task/background-task-tools.d.ts +84 -0
  293. package/dist/services/background-task/background-task-tools.js +247 -0
  294. package/dist/services/background-task/background-task-transition-events.d.ts +43 -0
  295. package/dist/services/background-task/background-task-transition-events.js +54 -0
  296. package/dist/services/browser-history/automation/egress-denylist.d.ts +1 -1
  297. package/dist/services/browser-history/automation/egress-denylist.js +16 -6
  298. package/dist/services/browser-history/managed-chromium/sandbox-launcher.js +0 -1
  299. package/dist/services/browser-task/browser-task-runner.js +53 -8
  300. package/dist/services/observations-batch.d.ts +1 -1
  301. package/dist/services/observations-batch.js +2 -2
  302. package/dist/settings/runtime-settings.d.ts +38 -11
  303. package/dist/settings/runtime-settings.js +203 -40
  304. package/dist/settings/settings-store.js +11 -3
  305. package/package.json +4 -4
@@ -0,0 +1,247 @@
1
+ /**
2
+ * Self-Tuning Review Cycle — Recommend stage (SELF_TUNING_REVIEW_CYCLE_DESIGN.md
3
+ * §3.2, Phase 2).
4
+ *
5
+ * The daemon-side, deterministic Recommend step ($0 — LLM tokens buy judgment
6
+ * only, P1). On the weekly-review dispatch it consumes the same
7
+ * {@link SelfPerformanceData} the Measure stage (§3.1) already gathered —
8
+ * current + baseline window = the 14-day evidence span the v1 rules read —
9
+ * and emits at most {@link MAX_RECOMMENDATIONS_PER_CYCLE} bounded
10
+ * `TuningRecommendation`s, rendered as one `<tuning_recommendations>` block
11
+ * for the weekly session's Phase 3c verdict step (§3.3).
12
+ *
13
+ * Guards live in code, not prompt (§3.2): per-rule ladders with hard
14
+ * floors/caps, hysteresis (no re-proposal of a key changed < 14 days ago;
15
+ * 28 days after a revert), minimum sample sizes, and the max-3 ranking by
16
+ * estimated $ impact. Numeric *enforcement* stays where it already lives —
17
+ * `runtimeSettingsSchema` + `env-writer.ts:NUMERIC_RANGE` behind the
18
+ * `applyConfigUpdates` chokepoint (P4); the `bounds` field on a
19
+ * recommendation documents the rule's own ladder, it is not a third copy of
20
+ * the schema bounds.
21
+ *
22
+ * The shadow period (§7): recommendations are generated, persisted under
23
+ * {@link TUNING_PENDING_CYCLE_STATE_KEY}, and verdicted via
24
+ * `POST /api/tuning/verdicts`. While `selfTuningEnabled` is `false` (the
25
+ * shipped default) nothing is actuated; once flipped, the Phase 3 actuator
26
+ * (`tuning-actuator.ts`) applies `apply` verdicts through the config
27
+ * chokepoint. Verdict ids are single-use: each weekly cycle overwrites the
28
+ * pending blob, expiring the prior cycle's ids (§3.4).
29
+ */
30
+ import type Database from "better-sqlite3";
31
+ import type { LessonStoreUtilization, SelfPerformanceData, SelfTuningLedgerEntry } from "./self-performance-prep.js";
32
+ /**
33
+ * §3.4 — runtime_state key for the current cycle's pending recommendations
34
+ * + verdicts. Deliberately uses a `.` separator, NOT the
35
+ * `SELF_TUNING_LEDGER_PREFIX` (`self_tuning:`) namespace — the Measure
36
+ * stage's `gatherLedger` does a `LIKE 'self_tuning:%'` scan and must never
37
+ * pick the pending blob up as a phantom ledger entry.
38
+ */
39
+ export declare const TUNING_PENDING_CYCLE_STATE_KEY = "self_tuning.pending_cycle";
40
+ /** §3.2 — max recommendations per weekly cycle, ranked by estimated $ impact. */
41
+ export declare const MAX_RECOMMENDATIONS_PER_CYCLE = 3;
42
+ /** §3.2 — no re-proposal of a key changed less than this many days ago. */
43
+ export declare const TUNING_HYSTERESIS_DAYS = 14;
44
+ /** §3.4 — extended cool-down after an auto-revert, so apply→revert can't flap. */
45
+ export declare const TUNING_REVERT_COOLDOWN_DAYS = 28;
46
+ export declare const R1_KNOB = "activityScanPrePassFreshnessMinutes";
47
+ export declare const R1_EMPTY_RATE_STEP_UP = 0.7;
48
+ export declare const R1_EMPTY_RATE_STEP_DOWN = 0.2;
49
+ export declare const R1_MIN_RUNS = 10;
50
+ /**
51
+ * §3.2 — the freshness ladder. Step up = smallest notch above the current
52
+ * value (cap 480); step down = largest notch below it (floor 120). Today's
53
+ * schema caps the knob at 240 — the 360/480 notches become appliable when
54
+ * Phase 3 widens `.max()` to 480 (§6); in the Phase 2 shadow period they are
55
+ * recorded-and-judged only, so proposing them is safe.
56
+ */
57
+ export declare const R1_FRESHNESS_NOTCHES: readonly [120, 240, 360, 480];
58
+ export declare const R2_IGNORED_RATE = 0.6;
59
+ export declare const R2_MIN_SENT = 5;
60
+ /**
61
+ * The loop's own DM channel — apply notices ("Reply `!revert tuning` to
62
+ * undo") and auto-revert notices land in `notification_log` under this
63
+ * type. R2 must never propose demoting it: the per-change DM is the D1/D6
64
+ * safety invariant (daemon-sent, mandatory, deliberately not a tunable
65
+ * notification surface), so an owner who lets those DMs sit unreacted
66
+ * would otherwise have the loop spend one of its max-3 weekly slots
67
+ * recommending that its own safety channel go quiet.
68
+ */
69
+ export declare const SELF_TUNING_NOTIFICATION_TYPE = "self_tuning";
70
+ export declare const R3_KNOB = "activityScanLowSignalPendingCeiling";
71
+ export declare const R3_LOW_NOVELTY_SHARE = 0.5;
72
+ /** Minimum stage-3 escalations over 14d before the share is meaningful. */
73
+ export declare const R3_MIN_STAGE3 = 4;
74
+ /**
75
+ * Conservative ladder for the silent-skip band. The schema allows up to 20;
76
+ * the rule never proposes past 8 — a wider band is an operator decision.
77
+ */
78
+ export declare const R3_CEILING_NOTCHES: readonly [2, 4, 8];
79
+ export declare const R5_KNOB = "feedbackLessonMaxBytesGlobal";
80
+ export declare const R5_UTILIZATION_THRESHOLD = 0.9;
81
+ export declare const R5_MEDIAN_EV_CEILING = 1;
82
+ /** Floor for the R5 step-down; matches the per-agent default cap. */
83
+ export declare const R5_MIN_BYTES = 4096;
84
+ /** R5 proposes a 25% reduction, rounded down to a 1 KiB multiple. */
85
+ export declare const R5_STEP_FACTOR = 0.75;
86
+ export type TuningRuleId = "R1" | "R2" | "R3" | "R4" | "R5";
87
+ /**
88
+ * What the Actuate stage would touch. `config` → `applyConfigUpdates`
89
+ * chokepoint (Phase 3); `lesson` → consumed as task-flow guidance via the
90
+ * existing lesson loop (R2 v1 — no per-type knob exists yet, §3.2);
91
+ * `schedule` → `recurring_schedules.enabled` flag (R4, propose-only in v1).
92
+ */
93
+ export type TuningActuator = "config" | "lesson" | "schedule";
94
+ export interface TuningRecommendation {
95
+ /** Daemon-generated, single-use id: `<cycleId>:<rule>:<key>`. */
96
+ id: string;
97
+ rule: TuningRuleId;
98
+ actuator: TuningActuator;
99
+ /** Config knob name, `notification:<type>`, or `recurring_schedules:<id>`. */
100
+ key: string;
101
+ currentValue: number | string;
102
+ proposedValue: number | string;
103
+ /**
104
+ * The rule's own ladder floor/cap — informational for the judge. Numeric
105
+ * enforcement stays in `runtimeSettingsSchema` / `env-writer.ts` (P4).
106
+ */
107
+ bounds: {
108
+ min: number;
109
+ max: number;
110
+ } | null;
111
+ /** One-line, number-bearing evidence string (≤ {@link MAX_EVIDENCE_CHARS}). */
112
+ evidence: string;
113
+ /** Deterministic heuristic, used only for the max-3 ranking. */
114
+ estWeeklySavingUsd: number;
115
+ }
116
+ export type TuningVerdict = "apply" | "reject" | "defer";
117
+ export interface TuningVerdictRecord {
118
+ verdict: TuningVerdict;
119
+ reason: string;
120
+ recordedAt: string;
121
+ }
122
+ export interface PendingTuningCycle {
123
+ /** ISO date of the generating weekly run — doubles as the id namespace. */
124
+ cycleId: string;
125
+ generatedAt: string;
126
+ recommendations: TuningRecommendation[];
127
+ /** Verdicts recorded so far, keyed by recommendation id. */
128
+ verdicts: Record<string, TuningVerdictRecord>;
129
+ }
130
+ /** R4 input row — gathered by {@link gatherFailingRecurringSchedules}. */
131
+ export interface FailingRecurringSchedule {
132
+ id: number;
133
+ taskType: string;
134
+ description: string | null;
135
+ /** Length of the trailing all-failed streak (= the qualifying window, 3). */
136
+ lastFailures: number;
137
+ }
138
+ export interface TuningKnobValues {
139
+ activityScanPrePassFreshnessMinutes: number;
140
+ activityScanLowSignalPendingCeiling: number;
141
+ feedbackLessonMaxBytesGlobal: number;
142
+ }
143
+ export interface TuningRecommenderInput {
144
+ /** Measure-stage output; current + baseline = the 14-day evidence span. */
145
+ data: SelfPerformanceData;
146
+ knobs: TuningKnobValues;
147
+ /** §3.5 store-side byte pressure; only the global `agent` scope feeds R5. */
148
+ lessonStores?: ReadonlyArray<LessonStoreUtilization>;
149
+ failingSchedules?: ReadonlyArray<FailingRecurringSchedule>;
150
+ now: Date;
151
+ }
152
+ export declare const MAX_EVIDENCE_CHARS = 200;
153
+ /**
154
+ * §3.2 hysteresis guard. A key with a ledger entry applied within
155
+ * {@link TUNING_HYSTERESIS_DAYS} (or reverted within
156
+ * {@link TUNING_REVERT_COOLDOWN_DAYS}) is off the table this cycle. A ledger
157
+ * entry whose timestamps fail to parse blocks conservatively.
158
+ */
159
+ export declare function isKeyInCooldown(ledger: ReadonlyArray<SelfTuningLedgerEntry>, key: string, now: Date): boolean;
160
+ /** Smallest notch strictly above `current`, or null at/above the cap. */
161
+ export declare function stepUpNotch(notches: ReadonlyArray<number>, current: number): number | null;
162
+ /** Largest notch strictly below `current`, or null at/below the floor. */
163
+ export declare function stepDownNotch(notches: ReadonlyArray<number>, current: number): number | null;
164
+ /**
165
+ * The rule table (§3.2). Pure: every input is passed in; the only
166
+ * non-determinism allowed is the caller's `now`. Applies the code-side
167
+ * guards — hysteresis against the ledger, per-rule minimum samples, and the
168
+ * max-3 ranking by estimated weekly $ impact (ties: rule order, then key).
169
+ */
170
+ export declare function buildTuningRecommendations(input: TuningRecommenderInput): TuningRecommendation[];
171
+ /** Cycle id = the generating run's UTC date (`YYYY-MM-DD`). */
172
+ export declare function cycleIdForDate(now: Date): string;
173
+ /**
174
+ * Wrap a recommendation set as the persisted pending-cycle blob. Written to
175
+ * `runtime_state` even when empty — overwriting is what expires the previous
176
+ * cycle's single-use ids (§3.4).
177
+ *
178
+ * Same-day regeneration (a weekly-review re-run via `!run` / crash retry)
179
+ * produces the SAME cycle id and — for any rule still firing on the same
180
+ * key — the same recommendation ids. Those ids are not expired (§3.4 expiry
181
+ * is the *next* weekly cycle), so verdicts already recorded against them
182
+ * carry forward: without this, the regenerated blob's empty `verdicts` map
183
+ * would silently reopen judged ids, and the re-run session's re-POST would
184
+ * record fresh verdicts — double-posting the rejection `self_critique`
185
+ * signals the route's per-id idempotency exists to prevent. Verdicts for
186
+ * ids the regenerated set no longer contains are dropped (the evidence
187
+ * that produced them is gone); a different-day cycle starts clean.
188
+ */
189
+ export declare function createPendingTuningCycle(recommendations: ReadonlyArray<TuningRecommendation>, generatedAtIso: string, previousCycle?: PendingTuningCycle | null): PendingTuningCycle;
190
+ /** Trailing settled-run window R4 inspects per recurring row. */
191
+ export declare const R4_FAILURE_STREAK = 3;
192
+ /**
193
+ * R4 input — enabled `recurring_schedules` rows whose last
194
+ * {@link R4_FAILURE_STREAK} *settled* materialized runs (`completed` /
195
+ * `failed` / `skipped`; pending and running rows are not evidence) all
196
+ * failed. SQL over `agent_schedule` grouped by `recurring_schedule_id` —
197
+ * no new columns (§3.2). A `skipped` run breaks the streak deliberately:
198
+ * the rule targets "fires and fails every time", not gate-skipped rows.
199
+ */
200
+ export declare function gatherFailingRecurringSchedules(db: Database.Database): FailingRecurringSchedule[];
201
+ export interface RenderTuningRecommendationsOptions {
202
+ /**
203
+ * Phase 3 — `shadow` (default) means verdicts are recorded but never
204
+ * actuated; `live` means the daemon applies `apply` verdicts through the
205
+ * config chokepoint (`selfTuningEnabled=true`). The attribute is what the
206
+ * weekly task-flow's Phase 3c branches on, so the prompt never has to
207
+ * guess the flag state.
208
+ */
209
+ mode?: "shadow" | "live";
210
+ }
211
+ /**
212
+ * Render the `<tuning_recommendations>` block for the weekly session's
213
+ * Phase 3c verdict step. Returns `null` when the cycle holds no
214
+ * recommendations — the design requires zero bytes in that case (§3.2).
215
+ * Output is bounded by construction: ≤ {@link MAX_RECOMMENDATIONS_PER_CYCLE}
216
+ * rows with ≤ {@link MAX_EVIDENCE_CHARS}-char evidence strings.
217
+ *
218
+ * Carried-forward verdicts (a same-day re-run regenerates the same ids, and
219
+ * `createPendingTuningCycle` preserves verdicts already recorded against
220
+ * them) surface as a `verdict` attribute on the row, so the re-run session
221
+ * skips already-judged rows instead of re-POSTing them — the route's per-id
222
+ * idempotency would absorb the duplicates, but not the wasted judgment
223
+ * tokens.
224
+ */
225
+ export declare function renderTuningRecommendationsBlock(cycle: PendingTuningCycle, opts?: RenderTuningRecommendationsOptions): string | null;
226
+ export interface VerdictEntry {
227
+ id: string;
228
+ verdict: TuningVerdict;
229
+ reason: string;
230
+ }
231
+ export type VerdictStatus = "recorded" | "duplicate" | "conflict";
232
+ export interface VerdictApplicationResult {
233
+ cycle: PendingTuningCycle;
234
+ results: Array<{
235
+ id: string;
236
+ status: VerdictStatus;
237
+ }>;
238
+ }
239
+ /**
240
+ * Record verdicts onto a pending cycle, idempotently per id (§3.4): a
241
+ * retried POST with the same verdict is a `duplicate` no-op; a different
242
+ * verdict for an already-verdicted id is a `conflict` — first verdict wins
243
+ * (re-judging a recommendation mid-cycle is not a supported operation).
244
+ * Callers must have validated every id against `cycle.recommendations`
245
+ * first; an unknown id here is a programming error and throws.
246
+ */
247
+ export declare function applyVerdictsToCycle(cycle: PendingTuningCycle, entries: ReadonlyArray<VerdictEntry>, nowIso: string): VerdictApplicationResult;