@vellumai/assistant 0.8.7-dev.202606052232.2ddc989 → 0.8.8

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 (262) hide show
  1. package/bun.lock +2 -2
  2. package/docs/plugins.md +832 -0
  3. package/examples/plugins/echo/README.md +60 -61
  4. package/examples/plugins/echo/package.json +2 -1
  5. package/examples/plugins/echo/register.ts +143 -0
  6. package/node_modules/@vellumai/skill-host-contracts/src/skill-host.ts +6 -7
  7. package/openapi.yaml +5 -15
  8. package/package.json +2 -2
  9. package/src/__tests__/agent-loop-exit-reason.test.ts +56 -3
  10. package/src/__tests__/anthropic-provider.test.ts +1 -1
  11. package/src/__tests__/app-control-flow.test.ts +1 -1
  12. package/src/__tests__/app-dir-path-guard.test.ts +0 -1
  13. package/src/__tests__/approval-routes-http.test.ts +1 -4
  14. package/src/__tests__/channel-approval-routes.test.ts +1 -1
  15. package/src/__tests__/channel-approvals.test.ts +1 -1
  16. package/src/__tests__/circuit-breaker-pipeline.test.ts +405 -0
  17. package/src/__tests__/compaction-pipeline.test.ts +210 -0
  18. package/src/__tests__/compaction-timeout-recovery.test.ts +251 -0
  19. package/src/__tests__/conversation-agent-loop-disk-pressure.test.ts +3 -0
  20. package/src/__tests__/conversation-agent-loop-inference-profile.test.ts +3 -0
  21. package/src/__tests__/conversation-agent-loop-overflow.test.ts +7 -3
  22. package/src/__tests__/conversation-agent-loop.test.ts +39 -42
  23. package/src/__tests__/conversation-clean-command.test.ts +2 -5
  24. package/src/__tests__/conversation-provider-retry-repair.test.ts +5 -4
  25. package/src/__tests__/conversation-runtime-assembly.test.ts +71 -140
  26. package/src/__tests__/conversation-runtime-workspace.test.ts +27 -108
  27. package/src/__tests__/conversation-starter-routes.test.ts +6 -14
  28. package/src/__tests__/conversation-workspace-cache-state.test.ts +16 -17
  29. package/src/__tests__/conversation-workspace-injection.test.ts +1 -61
  30. package/src/__tests__/conversation-workspace-tool-tracking.test.ts +6 -7
  31. package/src/__tests__/db-acp-history.test.ts +0 -101
  32. package/src/__tests__/dynamic-page-surface.test.ts +0 -31
  33. package/src/__tests__/file-write-tool.test.ts +0 -63
  34. package/src/__tests__/gateway-only-guard.test.ts +2 -12
  35. package/src/__tests__/guardian-grant-minting.test.ts +1 -1
  36. package/src/__tests__/guardian-routing-invariants.test.ts +4 -2
  37. package/src/__tests__/handlers-user-message-approval-consumption.test.ts +1 -1
  38. package/src/__tests__/heartbeat-disk-pressure.test.ts +0 -1
  39. package/src/__tests__/heartbeat-service.test.ts +0 -1
  40. package/src/__tests__/host-app-control-routes.test.ts +1 -1
  41. package/src/__tests__/host-cu-routes-targeted.test.ts +3 -3
  42. package/src/__tests__/injector-background-turn.test.ts +1 -1
  43. package/src/__tests__/injector-chain.test.ts +6 -34
  44. package/src/__tests__/injector-disk-pressure.test.ts +34 -77
  45. package/src/__tests__/injector-document-comments.test.ts +1 -1
  46. package/src/__tests__/list-messages-hidden-metadata.test.ts +0 -38
  47. package/src/__tests__/memory-v2-static-injector.test.ts +1 -1
  48. package/src/__tests__/{overflow-reduction-loop.test.ts → overflow-reduce-pipeline.test.ts} +284 -64
  49. package/src/__tests__/pipeline-runner.test.ts +554 -0
  50. package/src/__tests__/plugin-api-shim.test.ts +6 -3
  51. package/src/__tests__/plugin-bootstrap.test.ts +23 -12
  52. package/src/__tests__/plugin-registry.test.ts +49 -3
  53. package/src/__tests__/plugin-types.test.ts +70 -0
  54. package/src/__tests__/reaction-persistence.test.ts +1 -1
  55. package/src/__tests__/send-endpoint-busy.test.ts +1 -4
  56. package/src/__tests__/skill-feature-flags-integration.test.ts +0 -33
  57. package/src/__tests__/subagent-call-site-routing.test.ts +1 -1
  58. package/src/__tests__/subagent-fork-notifications.test.ts +3 -1
  59. package/src/__tests__/subagent-fork-spawn.test.ts +1 -1
  60. package/src/__tests__/subagent-manager-notify.test.ts +3 -1
  61. package/src/__tests__/subagent-notify-parent.test.ts +3 -1
  62. package/src/__tests__/subagent-spawn-tool-fork.test.ts +1 -1
  63. package/src/__tests__/user-plugin-loader.test.ts +286 -54
  64. package/src/acp/__tests__/client-handler.test.ts +0 -40
  65. package/src/acp/__tests__/prepare-agent-env.test.ts +0 -137
  66. package/src/acp/__tests__/session-manager-persistence.test.ts +28 -95
  67. package/src/acp/agent-process.ts +1 -61
  68. package/src/acp/client-handler.ts +0 -31
  69. package/src/acp/prepare-agent-env.ts +29 -83
  70. package/src/acp/resolve-agent.test.ts +7 -320
  71. package/src/acp/resolve-agent.ts +18 -182
  72. package/src/acp/session-manager.ts +73 -495
  73. package/src/acp/types.ts +0 -8
  74. package/src/agent/compaction-circuit.ts +102 -60
  75. package/src/agent/loop.ts +59 -32
  76. package/src/api/responses/conversation-message.ts +1 -7
  77. package/src/approvals/guardian-request-resolvers.ts +1 -1
  78. package/src/background-wake/next-wake.ts +0 -1
  79. package/src/config/__tests__/feature-flag-registry-guard.test.ts +2 -2
  80. package/src/config/acp-defaults.test.ts +0 -10
  81. package/src/config/acp-defaults.ts +0 -6
  82. package/src/config/bundled-skills/acp/SKILL.md +31 -83
  83. package/src/config/bundled-skills/acp/TOOLS.json +4 -4
  84. package/src/config/bundled-skills/app-builder/SKILL.md +381 -224
  85. package/src/config/bundled-skills/app-builder/TOOLS.json +0 -29
  86. package/src/config/bundled-skills/document-editor/SKILL.md +23 -28
  87. package/src/config/bundled-skills/document-editor/TOOLS.json +1 -1
  88. package/src/config/bundled-tool-registry.ts +0 -2
  89. package/src/config/feature-flag-registry.json +5 -14
  90. package/src/config/schemas/heartbeat.ts +0 -9
  91. package/src/context/strip-injections.ts +2 -8
  92. package/src/context/window-manager.ts +1 -2
  93. package/src/daemon/conversation-agent-loop-handlers.ts +11 -0
  94. package/src/daemon/conversation-agent-loop.ts +279 -62
  95. package/src/daemon/conversation-runtime-assembly.ts +69 -106
  96. package/src/daemon/conversation-store.ts +90 -9
  97. package/src/daemon/conversation-workspace.ts +0 -17
  98. package/src/daemon/conversation.ts +6 -0
  99. package/src/daemon/external-plugins-bootstrap.ts +11 -11
  100. package/src/daemon/handlers/conversations.ts +1 -3
  101. package/src/daemon/handlers/skills.ts +1 -4
  102. package/src/daemon/lifecycle.ts +0 -21
  103. package/src/daemon/server.ts +0 -2
  104. package/src/heartbeat/__tests__/heartbeat-service.test.ts +0 -3
  105. package/src/heartbeat/heartbeat-run-store.ts +1 -23
  106. package/src/heartbeat/heartbeat-service.ts +0 -26
  107. package/src/ipc/__tests__/browser-ipc.test.ts +1 -1
  108. package/src/ipc/__tests__/ui-request-route.test.ts +3 -3
  109. package/src/ipc/skill-routes/__tests__/memory.test.ts +0 -15
  110. package/src/ipc/skill-routes/memory.ts +2 -4
  111. package/src/memory/conversation-starter-checkpoints.ts +0 -1
  112. package/src/memory/db-init.ts +0 -2
  113. package/src/memory/job-handlers/conversation-starters.ts +2 -13
  114. package/src/memory/jobs-worker.ts +1 -1
  115. package/src/memory/migrations/index.ts +0 -1
  116. package/src/memory/schema/acp.ts +0 -4
  117. package/src/memory/v2/__tests__/consolidation-job.test.ts +3 -3
  118. package/src/memory/v2/consolidation-job.ts +4 -13
  119. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/assign.test.ts +4 -4
  120. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/live-integration.test.ts +4 -4
  121. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/maintain-job.test.ts +5 -5
  122. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/orchestrate.test.ts +3 -3
  123. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/reconcile.test.ts +2 -2
  124. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/render-injection.test.ts +1 -1
  125. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/router.test.ts +3 -3
  126. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/selection-log-store.test.ts +8 -8
  127. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/selector.test.ts +3 -3
  128. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/shadow-plugin.test.ts +12 -12
  129. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/assign.ts +5 -5
  130. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/capabilities.ts +2 -2
  131. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/maintain-job.ts +8 -8
  132. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/page-content.ts +2 -2
  133. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/provider-blocks.ts +1 -1
  134. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/reconcile.ts +3 -3
  135. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/render-injection.ts +1 -1
  136. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/router.ts +3 -3
  137. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/selection-log-store.ts +4 -4
  138. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/selector.ts +4 -4
  139. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/shadow-plugin.ts +90 -28
  140. package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/tree.ts +1 -1
  141. package/src/plugin-api/index.ts +5 -0
  142. package/src/plugins/defaults/circuit-breaker/middlewares/circuitBreaker.ts +93 -0
  143. package/src/plugins/defaults/{memory-v3-shadow → circuit-breaker}/package.json +2 -2
  144. package/src/plugins/defaults/circuit-breaker/register.ts +39 -0
  145. package/src/plugins/defaults/compaction/middlewares/compaction.ts +25 -0
  146. package/src/plugins/defaults/compaction/package.json +1 -1
  147. package/src/plugins/defaults/compaction/register.ts +19 -8
  148. package/src/plugins/defaults/compaction/terminal.ts +73 -0
  149. package/src/plugins/defaults/index.ts +5 -3
  150. package/src/plugins/defaults/{memory-retrieval/injectors.ts → injectors/register.ts} +7 -45
  151. package/src/plugins/defaults/memory-retrieval/hooks/post-compact.ts +7 -11
  152. package/src/plugins/defaults/memory-retrieval/injector-chain.ts +2 -2
  153. package/src/plugins/defaults/overflow-reduce/middlewares/overflowReduce.ts +126 -0
  154. package/src/plugins/defaults/overflow-reduce/package.json +15 -0
  155. package/src/plugins/defaults/overflow-reduce/register.ts +42 -0
  156. package/src/plugins/external-api.ts +2 -2
  157. package/src/plugins/pipeline.ts +293 -6
  158. package/src/plugins/registry.ts +37 -9
  159. package/src/plugins/types.ts +336 -32
  160. package/src/plugins/user-loader.ts +127 -30
  161. package/src/proactive-artifact/aux-message-injector.ts +1 -1
  162. package/src/proactive-artifact/job.test.ts +1 -1
  163. package/src/prompts/__tests__/system-prompt.test.ts +0 -6
  164. package/src/prompts/templates/BOOTSTRAP-ACTIVATION-RAIL.md +2 -4
  165. package/src/runtime/__tests__/agent-wake.test.ts +5 -5
  166. package/src/runtime/__tests__/interactive-ui.test.ts +1 -1
  167. package/src/runtime/agent-wake.ts +3 -0
  168. package/src/runtime/assistant-event-hub.ts +1 -1
  169. package/src/runtime/channel-approvals.ts +1 -1
  170. package/src/runtime/interactive-ui.ts +1 -1
  171. package/src/runtime/routes/__tests__/acp-routes.test.ts +55 -283
  172. package/src/runtime/routes/__tests__/conversation-list-routes.test.ts +1 -1
  173. package/src/runtime/routes/__tests__/surface-action-routes.test.ts +4 -5
  174. package/src/runtime/routes/__tests__/surface-content-routes.test.ts +1 -4
  175. package/src/runtime/routes/acp-routes.test.ts +25 -89
  176. package/src/runtime/routes/acp-routes.ts +29 -81
  177. package/src/runtime/routes/approval-routes.ts +1 -1
  178. package/src/runtime/routes/browser-routes.ts +1 -1
  179. package/src/runtime/routes/browser-tabs-routes.ts +10 -6
  180. package/src/runtime/routes/conversation-cli-routes.ts +1 -1
  181. package/src/runtime/routes/conversation-list-routes.ts +1 -1
  182. package/src/runtime/routes/conversation-query-routes.ts +1 -1
  183. package/src/runtime/routes/conversation-routes.ts +2 -15
  184. package/src/runtime/routes/conversation-starter-routes.ts +7 -13
  185. package/src/runtime/routes/conversations-import-routes.ts +7 -24
  186. package/src/runtime/routes/host-app-control-routes.ts +1 -1
  187. package/src/runtime/routes/host-cu-routes.ts +1 -1
  188. package/src/runtime/routes/identity-routes.ts +3 -18
  189. package/src/runtime/routes/inbound-message-handler.ts +1 -1
  190. package/src/runtime/routes/memory-v3-routes.ts +6 -16
  191. package/src/runtime/routes/playground/helpers.ts +1 -1
  192. package/src/runtime/routes/surface-conversation-resolver.ts +3 -4
  193. package/src/runtime/routes/work-items-routes.ts +4 -2
  194. package/src/runtime/services/conversation-serializer.ts +1 -1
  195. package/src/signals/cancel.ts +4 -2
  196. package/src/subagent/manager.ts +5 -17
  197. package/src/tools/acp/list-agents.test.ts +1 -7
  198. package/src/tools/acp/spawn.test.ts +55 -158
  199. package/src/tools/acp/spawn.ts +72 -47
  200. package/src/tools/acp/steer.test.ts +8 -105
  201. package/src/tools/acp/steer.ts +17 -48
  202. package/src/tools/apps/executors.ts +8 -13
  203. package/src/tools/filesystem/write.ts +0 -34
  204. package/src/tools/subagent/spawn.ts +4 -2
  205. package/src/tools/ui-surface/definitions.ts +4 -25
  206. package/src/workspace/migrations/051-seed-conversation-summarization-callsite.ts +5 -4
  207. package/src/workspace/migrations/097-enable-adaptive-thinking-managed-profiles.ts +45 -69
  208. package/examples/plugins/echo/hooks/post-tool-use.ts +0 -18
  209. package/examples/plugins/echo/hooks/stop.ts +0 -16
  210. package/examples/plugins/echo/hooks/user-prompt-submit.ts +0 -18
  211. package/examples/plugins/echo/src/emit.ts +0 -19
  212. package/src/__tests__/compaction-circuit.test.ts +0 -258
  213. package/src/__tests__/compaction-direct.test.ts +0 -132
  214. package/src/__tests__/conversations-import-system-filter.test.ts +0 -101
  215. package/src/acp/__tests__/agent-process.test.ts +0 -161
  216. package/src/acp/__tests__/helpers/acp-history-db.ts +0 -82
  217. package/src/acp/__tests__/helpers/exec-file-stub.ts +0 -101
  218. package/src/acp/__tests__/session-manager-resume.test.ts +0 -736
  219. package/src/acp/auto-install.test.ts +0 -196
  220. package/src/acp/auto-install.ts +0 -177
  221. package/src/acp/feature-gate.test.ts +0 -48
  222. package/src/acp/feature-gate.ts +0 -34
  223. package/src/acp/resume-hint.ts +0 -25
  224. package/src/config/bundled-skills/app-builder/references/DESIGN_SYSTEM.md +0 -48
  225. package/src/config/bundled-skills/app-builder/references/RESPONSIVE.md +0 -57
  226. package/src/config/bundled-skills/app-builder/references/SLIDES.md +0 -38
  227. package/src/config/bundled-skills/app-builder/tools/app-list.ts +0 -62
  228. package/src/daemon/conversation-registry.ts +0 -159
  229. package/src/daemon/overflow-reduction-loop.ts +0 -230
  230. package/src/memory/migrations/272-acp-session-history-cwd.ts +0 -36
  231. package/src/plugins/defaults/compaction/compact.ts +0 -59
  232. package/src/plugins/defaults/memory-v3-shadow/hooks/post-compact.ts +0 -14
  233. package/src/plugins/defaults/memory-v3-shadow/hooks/user-prompt-submit.ts +0 -19
  234. package/src/plugins/defaults/memory-v3-shadow/injector.ts +0 -75
  235. package/src/plugins/defaults/memory-v3-shadow/register.ts +0 -26
  236. package/src/tools/acp/context.ts +0 -20
  237. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/capabilities.test.ts +0 -0
  238. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/core.test.ts +0 -0
  239. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/fixtures/eval-turns.json +0 -0
  240. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/fixtures/live-turns.json +0 -0
  241. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/health.test.ts +0 -0
  242. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/needle.test.ts +0 -0
  243. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/provider-blocks.test.ts +0 -0
  244. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/snapshot.test.ts +0 -0
  245. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/tree.test.ts +0 -0
  246. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/types.test.ts +0 -0
  247. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/working-set-eviction.test.ts +0 -0
  248. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/__tests__/working-set-skeleton.test.ts +0 -0
  249. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/core.ts +0 -0
  250. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/data/README.md +0 -0
  251. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/data/assignments.json +0 -0
  252. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/data/core.json +0 -0
  253. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/data/leaves/domain-a/topic-x.md +0 -0
  254. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/data/leaves/domain-a/topic-y.md +0 -0
  255. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/data/leaves/domain-b/topic-z.md +0 -0
  256. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/health.ts +0 -0
  257. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/llm-retry.ts +0 -0
  258. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/needle.ts +0 -0
  259. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/orchestrate.ts +0 -0
  260. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/snapshot.ts +0 -0
  261. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/types.ts +0 -0
  262. /package/src/{plugins/defaults/memory-v3-shadow → memory/v3}/working-set.ts +0 -0
@@ -0,0 +1,405 @@
1
+ /**
2
+ * Tests for the `circuitBreaker` plugin pipeline.
3
+ *
4
+ * The default plugin (`plugins/defaults/circuit-breaker/register.ts`) replaces the
5
+ * inline compaction circuit-breaker logic that previously lived in
6
+ * `daemon/conversation-agent-loop.ts`. These tests exercise the default
7
+ * plugin through the pipeline runner and assert the threshold (3 consecutive
8
+ * failures) and cooldown (1 hour) exactly match the legacy behavior.
9
+ *
10
+ * Coverage mirrors the eight scenarios the deleted
11
+ * `compaction-circuit-breaker.test.ts` exercised before the wrap:
12
+ * (a) counter increments on each failure outcome
13
+ * (b) circuit opens after exactly 3 consecutive failures
14
+ * (c) successful compaction resets counter and clears the circuit
15
+ * (d) decision.open reflects state and cooldown expiry
16
+ * (d) open circuit admits force:true (exercised at the call site; this
17
+ * file asserts decision.open is true while the breaker is tripped)
18
+ * (e) circuit re-opens after cooldown expiry when 3 more failures
19
+ * accumulate (guards the stale-timestamp regression)
20
+ * (f) callers skip tracking on undefined summaryFailed so early returns
21
+ * don't reset the counter (documented from the caller's perspective)
22
+ * (g) open→closed transition emits `compaction_circuit_closed` exactly once
23
+ * (h) closed→closed transition emits nothing
24
+ */
25
+
26
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
27
+
28
+ import type { ServerMessage } from "../daemon/message-protocol.js";
29
+ import type { TrustContext } from "../daemon/trust-context.js";
30
+ import {
31
+ COMPACTION_CIRCUIT_COOLDOWN_MS,
32
+ COMPACTION_CIRCUIT_FAILURE_THRESHOLD,
33
+ } from "../plugins/defaults/circuit-breaker/middlewares/circuitBreaker.js";
34
+ import { defaultCircuitBreakerPlugin } from "../plugins/defaults/circuit-breaker/register.js";
35
+ import { runPipeline } from "../plugins/pipeline.js";
36
+ import {
37
+ getMiddlewaresFor,
38
+ registerPlugin,
39
+ resetPluginRegistryForTests,
40
+ } from "../plugins/registry.js";
41
+ import type {
42
+ CircuitBreakerArgs,
43
+ CircuitBreakerResult,
44
+ TurnContext,
45
+ } from "../plugins/types.js";
46
+
47
+ // ─── Fixtures ───────────────────────────────────────────────────────────────
48
+
49
+ interface BreakerState {
50
+ readonly conversationId: string;
51
+ consecutiveCompactionFailures: number;
52
+ compactionCircuitOpenUntil: number | null;
53
+ }
54
+
55
+ function makeState(conversationId = "conv-breaker-test"): BreakerState {
56
+ return {
57
+ conversationId,
58
+ consecutiveCompactionFailures: 0,
59
+ compactionCircuitOpenUntil: null,
60
+ };
61
+ }
62
+
63
+ function collectEvents(): {
64
+ events: ServerMessage[];
65
+ onEvent: (msg: ServerMessage) => void;
66
+ } {
67
+ const events: ServerMessage[] = [];
68
+ return { events, onEvent: (msg) => events.push(msg) };
69
+ }
70
+
71
+ const trust: TrustContext = {
72
+ sourceChannel: "vellum",
73
+ trustClass: "guardian",
74
+ };
75
+
76
+ function makeTurnCtx(conversationId = "conv-breaker-test"): TurnContext {
77
+ return {
78
+ requestId: "req-test",
79
+ conversationId,
80
+ turnIndex: 0,
81
+ trust,
82
+ };
83
+ }
84
+
85
+ /**
86
+ * Run the `circuitBreaker` pipeline through the registered plugin chain.
87
+ * Mirrors how `conversation-agent-loop.ts` invokes it, with the same
88
+ * terminal fallback used in production.
89
+ */
90
+ async function runCircuit(
91
+ args: CircuitBreakerArgs,
92
+ ctx: TurnContext = makeTurnCtx(args.state.conversationId),
93
+ ): Promise<CircuitBreakerResult> {
94
+ return runPipeline<CircuitBreakerArgs, CircuitBreakerResult>(
95
+ "circuitBreaker",
96
+ getMiddlewaresFor("circuitBreaker"),
97
+ async (terminalArgs) => {
98
+ const openUntil = terminalArgs.state.compactionCircuitOpenUntil;
99
+ const now = Date.now();
100
+ if (openUntil !== null && now < openUntil) {
101
+ return { open: true, cooldownRemainingMs: openUntil - now };
102
+ }
103
+ return { open: false };
104
+ },
105
+ args,
106
+ ctx,
107
+ 500,
108
+ );
109
+ }
110
+
111
+ describe("circuit-breaker pipeline", () => {
112
+ let originalDateNow: () => number;
113
+
114
+ beforeEach(() => {
115
+ resetPluginRegistryForTests();
116
+ registerPlugin(defaultCircuitBreakerPlugin);
117
+ originalDateNow = Date.now;
118
+ });
119
+
120
+ afterEach(() => {
121
+ Date.now = originalDateNow;
122
+ });
123
+
124
+ test("threshold and cooldown match legacy constants exactly", () => {
125
+ // Sanity — the plugin must expose the same constants the legacy inline
126
+ // helpers used. Any drift would silently change user-visible behavior.
127
+ expect(COMPACTION_CIRCUIT_FAILURE_THRESHOLD).toBe(3);
128
+ expect(COMPACTION_CIRCUIT_COOLDOWN_MS).toBe(60 * 60 * 1000);
129
+ });
130
+
131
+ test("(a) counter increments on each failure outcome", async () => {
132
+ const state = makeState();
133
+ const { onEvent, events } = collectEvents();
134
+
135
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
136
+ expect(state.consecutiveCompactionFailures).toBe(1);
137
+ expect(state.compactionCircuitOpenUntil).toBeNull();
138
+ expect(events).toHaveLength(0);
139
+
140
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
141
+ expect(state.consecutiveCompactionFailures).toBe(2);
142
+ expect(state.compactionCircuitOpenUntil).toBeNull();
143
+ expect(events).toHaveLength(0);
144
+ });
145
+
146
+ test("(b) circuit opens after exactly 3 consecutive failures", async () => {
147
+ const fixedNow = 1_700_000_000_000;
148
+ Date.now = () => fixedNow;
149
+
150
+ const state = makeState();
151
+ const { onEvent, events } = collectEvents();
152
+
153
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
154
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
155
+ // Two failures — circuit still closed.
156
+ expect(state.compactionCircuitOpenUntil).toBeNull();
157
+ expect(events).toHaveLength(0);
158
+
159
+ const third = await runCircuit({
160
+ key: "k",
161
+ outcome: "failure",
162
+ state,
163
+ onEvent,
164
+ });
165
+ // Third failure — circuit trips and fires the event exactly once.
166
+ expect(state.consecutiveCompactionFailures).toBe(3);
167
+ expect(state.compactionCircuitOpenUntil).toBe(fixedNow + 60 * 60 * 1000);
168
+ expect(third.open).toBe(true);
169
+ expect(third.cooldownRemainingMs).toBe(60 * 60 * 1000);
170
+ expect(events).toHaveLength(1);
171
+ expect(events[0]).toEqual({
172
+ type: "compaction_circuit_open",
173
+ conversationId: state.conversationId,
174
+ reason: "3_consecutive_failures",
175
+ openUntil: fixedNow + 60 * 60 * 1000,
176
+ });
177
+
178
+ // Further failures do not re-fire the event while the circuit is open.
179
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
180
+ expect(state.consecutiveCompactionFailures).toBe(4);
181
+ expect(events).toHaveLength(1);
182
+ });
183
+
184
+ test("(c) successful outcome resets counter and clears circuit", async () => {
185
+ const fixedNow = 1_700_000_000_000;
186
+ Date.now = () => fixedNow;
187
+
188
+ const state = makeState();
189
+ const { onEvent } = collectEvents();
190
+
191
+ // Trip the breaker.
192
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
193
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
194
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
195
+ expect(state.compactionCircuitOpenUntil).not.toBeNull();
196
+
197
+ // Success resets state.
198
+ await runCircuit({ key: "k", outcome: "success", state, onEvent });
199
+ expect(state.consecutiveCompactionFailures).toBe(0);
200
+ expect(state.compactionCircuitOpenUntil).toBeNull();
201
+ });
202
+
203
+ test("(d) decision.open reflects state and expiry", async () => {
204
+ const fixedNow = 1_700_000_000_000;
205
+ Date.now = () => fixedNow;
206
+
207
+ const state = makeState();
208
+ const { onEvent } = collectEvents();
209
+
210
+ // Query-only on a fresh state: closed, no cooldown.
211
+ const preQuery = await runCircuit({ key: "k", state, onEvent });
212
+ expect(preQuery.open).toBe(false);
213
+ expect(preQuery.cooldownRemainingMs).toBeUndefined();
214
+
215
+ // Trip the breaker.
216
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
217
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
218
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
219
+
220
+ // Query-only while open: open + non-zero cooldown.
221
+ const openQuery = await runCircuit({ key: "k", state, onEvent });
222
+ expect(openQuery.open).toBe(true);
223
+ expect(openQuery.cooldownRemainingMs).toBe(60 * 60 * 1000);
224
+
225
+ // After cooldown expires the breaker reports closed again, even without
226
+ // an explicit reset — the open-until timestamp is the only source of
227
+ // truth for the gate.
228
+ Date.now = () => fixedNow + 60 * 60 * 1000 + 1;
229
+ const postCooldown = await runCircuit({ key: "k", state, onEvent });
230
+ expect(postCooldown.open).toBe(false);
231
+ expect(postCooldown.cooldownRemainingMs).toBeUndefined();
232
+ });
233
+
234
+ test("(e) circuit re-opens after cooldown expiry when 3 more failures accumulate", async () => {
235
+ // Regression: before the fix in the legacy helper, opening the breaker a
236
+ // second time required `compactionCircuitOpenUntil === null`. Once a
237
+ // cooldown expired, the decision correctly reported "closed" but the
238
+ // stale past-timestamp stayed on the state, so the next 3-strike window
239
+ // couldn't trip a new cooldown. The default plugin must treat any
240
+ // expired timestamp the same as null.
241
+ const t0 = 1_700_000_000_000;
242
+ Date.now = () => t0;
243
+
244
+ const state = makeState();
245
+ const { onEvent, events } = collectEvents();
246
+
247
+ // Trip the breaker the first time.
248
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
249
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
250
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
251
+ expect(state.compactionCircuitOpenUntil).toBe(t0 + 60 * 60 * 1000);
252
+ expect(events).toHaveLength(1);
253
+
254
+ // Advance past the cooldown window. Manually reset the counter — in
255
+ // production this happens when a subsequent `maybeCompact` call succeeds
256
+ // (outcome: "success") after the cooldown elapses, but the bug
257
+ // manifests even when the counter is reset: the stale
258
+ // `compactionCircuitOpenUntil` is what breaks re-opening.
259
+ const t1 = t0 + 60 * 60 * 1000 + 1;
260
+ Date.now = () => t1;
261
+ const postCooldown = await runCircuit({ key: "k", state, onEvent });
262
+ expect(postCooldown.open).toBe(false);
263
+ state.consecutiveCompactionFailures = 0;
264
+ // `compactionCircuitOpenUntil` is deliberately left as the old
265
+ // timestamp to reproduce the bug condition.
266
+ expect(state.compactionCircuitOpenUntil).toBe(t0 + 60 * 60 * 1000);
267
+
268
+ // Three more failures must trip a fresh cooldown even though the old
269
+ // timestamp is still set.
270
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
271
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
272
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
273
+ expect(state.consecutiveCompactionFailures).toBe(3);
274
+ expect(state.compactionCircuitOpenUntil).toBe(t1 + 60 * 60 * 1000);
275
+ expect(events).toHaveLength(2);
276
+ expect(events[1]).toEqual({
277
+ type: "compaction_circuit_open",
278
+ conversationId: state.conversationId,
279
+ reason: "3_consecutive_failures",
280
+ openUntil: t1 + 60 * 60 * 1000,
281
+ });
282
+ });
283
+
284
+ test("(f) callers must skip tracking on undefined summaryFailed so early returns don't reset the counter", async () => {
285
+ // Regression: `maybeCompact()` returns `summaryFailed: undefined` on
286
+ // early-return paths (no eligible messages, below threshold, cooldown
287
+ // active, truncation-only). Callers guard with `summaryFailed !==
288
+ // undefined` at every call site — this test asserts that a query-only
289
+ // pipeline invocation (no `outcome`) preserves the counter.
290
+ const state = makeState();
291
+ const { onEvent } = collectEvents();
292
+
293
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
294
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
295
+ expect(state.consecutiveCompactionFailures).toBe(2);
296
+
297
+ // Query-only — should NOT touch the counter.
298
+ await runCircuit({ key: "k", state, onEvent });
299
+ expect(state.consecutiveCompactionFailures).toBe(2);
300
+
301
+ // A third real failure then trips the breaker as expected.
302
+ await runCircuit({ key: "k", outcome: "failure", state, onEvent });
303
+ expect(state.consecutiveCompactionFailures).toBe(3);
304
+ expect(state.compactionCircuitOpenUntil).not.toBeNull();
305
+ });
306
+
307
+ test("(g) open→closed transition emits compaction_circuit_closed exactly once", async () => {
308
+ // Regression: before this fix in the legacy helper, the reset branch
309
+ // silently cleared `compactionCircuitOpenUntil` without notifying the
310
+ // client. The Swift banner set from `compaction_circuit_open` would
311
+ // stay visible until the original `openUntil` deadline (up to 1h),
312
+ // misrepresenting the live state. The default plugin emits
313
+ // `compaction_circuit_closed` on the open→closed transition so the
314
+ // banner dismisses immediately.
315
+ const fixedNow = 1_700_000_000_000;
316
+ Date.now = () => fixedNow;
317
+
318
+ const state = makeState();
319
+ const { onEvent, events } = collectEvents();
320
+
321
+ // Force the circuit into the open state directly — the emitted-event
322
+ // transition logic is what we're testing, not the tripping path.
323
+ state.compactionCircuitOpenUntil = fixedNow + 60 * 60 * 1000;
324
+ state.consecutiveCompactionFailures = 3;
325
+
326
+ await runCircuit({ key: "k", outcome: "success", state, onEvent });
327
+
328
+ expect(state.consecutiveCompactionFailures).toBe(0);
329
+ expect(state.compactionCircuitOpenUntil).toBeNull();
330
+ expect(events).toHaveLength(1);
331
+ expect(events[0]).toEqual({
332
+ type: "compaction_circuit_closed",
333
+ conversationId: state.conversationId,
334
+ });
335
+ });
336
+
337
+ test("(h) successful outcome against an already-closed circuit emits no event", async () => {
338
+ // Emitting `compaction_circuit_closed` on every successful compaction
339
+ // would spam the client (the breaker is closed in the common case).
340
+ // Only the open→closed transition is meaningful.
341
+ const state = makeState();
342
+ const { onEvent, events } = collectEvents();
343
+
344
+ expect(state.compactionCircuitOpenUntil).toBeNull();
345
+ await runCircuit({ key: "k", outcome: "success", state, onEvent });
346
+ expect(state.compactionCircuitOpenUntil).toBeNull();
347
+ expect(events).toHaveLength(0);
348
+
349
+ // A second successful outcome while still closed — still no event.
350
+ await runCircuit({ key: "k", outcome: "success", state, onEvent });
351
+ expect(events).toHaveLength(0);
352
+ });
353
+
354
+ test("omitting onEvent still updates state without emitting", async () => {
355
+ // `onEvent` is optional in `CircuitBreakerArgs`. When omitted the plugin
356
+ // must still mutate the state container correctly — the only missing
357
+ // side effect is the transition notification.
358
+ const state = makeState();
359
+
360
+ for (let i = 0; i < 3; i++) {
361
+ await runCircuit({ key: "k", outcome: "failure", state });
362
+ }
363
+ expect(state.consecutiveCompactionFailures).toBe(3);
364
+ expect(state.compactionCircuitOpenUntil).not.toBeNull();
365
+
366
+ await runCircuit({ key: "k", outcome: "success", state });
367
+ expect(state.consecutiveCompactionFailures).toBe(0);
368
+ expect(state.compactionCircuitOpenUntil).toBeNull();
369
+ });
370
+
371
+ test("pipeline runner applies registered middleware in registration order", async () => {
372
+ // A second plugin registered after the default can observe args/result
373
+ // around the default's behavior. This proves the pipeline composes both
374
+ // middlewares rather than short-circuiting on the default alone.
375
+ const seen: string[] = [];
376
+ registerPlugin({
377
+ manifest: {
378
+ name: "observer",
379
+ version: "0.0.1",
380
+ },
381
+ middleware: {
382
+ circuitBreaker: async (args, next) => {
383
+ seen.push(`before:${args.outcome ?? "query"}`);
384
+ const res = await next(args);
385
+ seen.push(`after:${res.open ? "open" : "closed"}`);
386
+ return res;
387
+ },
388
+ },
389
+ });
390
+
391
+ const state = makeState();
392
+ await runCircuit({ key: "k", outcome: "failure", state });
393
+ await runCircuit({ key: "k", outcome: "failure", state });
394
+ await runCircuit({ key: "k", outcome: "failure", state });
395
+
396
+ expect(seen).toEqual([
397
+ "before:failure",
398
+ "after:closed",
399
+ "before:failure",
400
+ "after:closed",
401
+ "before:failure",
402
+ "after:open",
403
+ ]);
404
+ });
405
+ });
@@ -0,0 +1,210 @@
1
+ /**
2
+ * Tests for the `compaction` plugin pipeline (PR 25).
3
+ *
4
+ * Covers:
5
+ * - Default plugin delegates to the manager's `maybeCompact` and returns the
6
+ * same `ContextWindowResult` object the manager produced.
7
+ * - A custom plugin layered on top can short-circuit before the terminal is
8
+ * reached and return a different summary, demonstrating that the pipeline
9
+ * slot is observable and replaceable without patching the manager.
10
+ *
11
+ * The tests drive `runPipeline` directly rather than going through the full
12
+ * orchestrator — the integration path (conversation-agent-loop) is exercised
13
+ * by `conversation-agent-loop-overflow.test.ts`, which must continue to pass
14
+ * as the acceptance criterion for this PR.
15
+ */
16
+
17
+ import { describe, expect, test } from "bun:test";
18
+
19
+ import type { TrustContext } from "../daemon/trust-context.js";
20
+ import {
21
+ DEFAULT_COMPACTION_PLUGIN_NAME,
22
+ defaultCompactionTerminal,
23
+ } from "../plugins/defaults/compaction/terminal.js";
24
+ import { runPipeline } from "../plugins/pipeline.js";
25
+ import {
26
+ type CompactionArgs,
27
+ type CompactionResult,
28
+ type Middleware,
29
+ PluginExecutionError,
30
+ type TurnContext,
31
+ } from "../plugins/types.js";
32
+
33
+ type ContextWindowResultShape = {
34
+ compacted: boolean;
35
+ summaryText: string;
36
+ messages: unknown[];
37
+ previousEstimatedInputTokens: number;
38
+ estimatedInputTokens: number;
39
+ maxInputTokens: number;
40
+ thresholdTokens: number;
41
+ compactedMessages: number;
42
+ compactedPersistedMessages: number;
43
+ summaryCalls: number;
44
+ summaryInputTokens: number;
45
+ summaryOutputTokens: number;
46
+ summaryModel: string;
47
+ reason?: string;
48
+ };
49
+
50
+ const trust: TrustContext = {
51
+ sourceChannel: "vellum",
52
+ trustClass: "guardian",
53
+ };
54
+
55
+ function makeTurnCtx(manager: {
56
+ maybeCompact: (...args: unknown[]) => Promise<unknown>;
57
+ }): TurnContext {
58
+ return {
59
+ requestId: "req-compaction-test",
60
+ conversationId: "conv-compaction-test",
61
+ turnIndex: 0,
62
+ trust,
63
+ // `TurnContext.contextWindowManager` is a typed optional field; the
64
+ // default compaction plugin reads it directly without a cast.
65
+ contextWindowManager:
66
+ manager as unknown as TurnContext["contextWindowManager"],
67
+ };
68
+ }
69
+
70
+ function makeResult(
71
+ overrides: Partial<ContextWindowResultShape> = {},
72
+ ): ContextWindowResultShape {
73
+ return {
74
+ compacted: true,
75
+ summaryText: "default-summary",
76
+ messages: [],
77
+ previousEstimatedInputTokens: 1000,
78
+ estimatedInputTokens: 100,
79
+ maxInputTokens: 100000,
80
+ thresholdTokens: 80000,
81
+ compactedMessages: 3,
82
+ compactedPersistedMessages: 3,
83
+ summaryCalls: 1,
84
+ summaryInputTokens: 500,
85
+ summaryOutputTokens: 120,
86
+ summaryModel: "default-model",
87
+ ...overrides,
88
+ };
89
+ }
90
+
91
+ describe("compaction pipeline", () => {
92
+ test("default plugin delegates to the manager and returns its result unchanged", async () => {
93
+ const observed: {
94
+ messages: unknown;
95
+ signal: unknown;
96
+ options: unknown;
97
+ }[] = [];
98
+ const expected = makeResult({
99
+ summaryText: "manager-summary",
100
+ compactedMessages: 7,
101
+ });
102
+ const manager = {
103
+ maybeCompact: async (
104
+ messages: unknown,
105
+ signal: unknown,
106
+ options: unknown,
107
+ ) => {
108
+ observed.push({ messages, signal, options });
109
+ return expected;
110
+ },
111
+ };
112
+ const turnCtx = makeTurnCtx(manager);
113
+ const args: CompactionArgs = {
114
+ messages: [{ role: "user", content: "hi" }],
115
+ signal: new AbortController().signal,
116
+ options: { precomputedEstimate: 1234 },
117
+ };
118
+
119
+ // No middleware registered — the runner invokes the terminal directly.
120
+ const result = (await runPipeline<CompactionArgs, CompactionResult>(
121
+ "compaction",
122
+ [],
123
+ (innerArgs) => defaultCompactionTerminal(innerArgs, turnCtx),
124
+ args,
125
+ turnCtx,
126
+ 30000,
127
+ )) as ContextWindowResultShape;
128
+
129
+ // Terminal forwarded args verbatim to the manager — except for
130
+ // `signal`, which the pipeline runner replaces with a signal linked
131
+ // to its internal timeout controller. The linked signal must forward
132
+ // caller-originated aborts, which is verified in the dedicated
133
+ // pipeline-runner abort-propagation tests.
134
+ expect(observed).toHaveLength(1);
135
+ expect(observed[0]!.messages).toBe(args.messages);
136
+ expect(observed[0]!.signal).toBeInstanceOf(AbortSignal);
137
+ expect(observed[0]!.options).toBe(args.options);
138
+
139
+ // Returned result is the manager's object, unmodified — no wrapping
140
+ // or shape transformation is allowed in the default path.
141
+ expect(result).toBe(expected);
142
+ expect(result.summaryText).toBe("manager-summary");
143
+ expect(result.compactedMessages).toBe(7);
144
+ });
145
+
146
+ test("custom plugin short-circuits to a different summary without touching the manager", async () => {
147
+ let managerCallCount = 0;
148
+ const manager = {
149
+ maybeCompact: async () => {
150
+ managerCallCount++;
151
+ return makeResult({ summaryText: "should-not-run" });
152
+ },
153
+ };
154
+ const turnCtx = makeTurnCtx(manager);
155
+
156
+ const custom: Middleware<CompactionArgs, CompactionResult> =
157
+ async function customCompaction(_args, _next, _ctx) {
158
+ // Short-circuit — omit the `next` call so the terminal never fires.
159
+ return makeResult({
160
+ summaryText: "custom-plugin-summary",
161
+ compactedMessages: 0,
162
+ summaryCalls: 0,
163
+ reason: "short-circuited by custom plugin",
164
+ });
165
+ };
166
+
167
+ const args: CompactionArgs = {
168
+ messages: [],
169
+ signal: undefined,
170
+ options: undefined,
171
+ };
172
+
173
+ const result = (await runPipeline<CompactionArgs, CompactionResult>(
174
+ "compaction",
175
+ [custom],
176
+ (innerArgs) => defaultCompactionTerminal(innerArgs, turnCtx),
177
+ args,
178
+ turnCtx,
179
+ 30000,
180
+ )) as ContextWindowResultShape;
181
+
182
+ expect(managerCallCount).toBe(0);
183
+ expect(result.summaryText).toBe("custom-plugin-summary");
184
+ expect(result.reason).toBe("short-circuited by custom plugin");
185
+ });
186
+
187
+ test("default terminal surfaces a PluginExecutionError when the manager is missing", async () => {
188
+ // Build a turn context without the extension field so the default
189
+ // terminal's lenient read fails — this guards against a future refactor
190
+ // that removes the handle-attach helper in the orchestrator.
191
+ const turnCtxWithoutManager: TurnContext = {
192
+ requestId: "req-missing",
193
+ conversationId: "conv-missing",
194
+ turnIndex: 0,
195
+ trust,
196
+ };
197
+ const args: CompactionArgs = {
198
+ messages: [],
199
+ signal: undefined,
200
+ options: undefined,
201
+ };
202
+
203
+ await expect(
204
+ defaultCompactionTerminal(args, turnCtxWithoutManager),
205
+ ).rejects.toThrow(PluginExecutionError);
206
+ await expect(
207
+ defaultCompactionTerminal(args, turnCtxWithoutManager),
208
+ ).rejects.toThrow(DEFAULT_COMPACTION_PLUGIN_NAME);
209
+ });
210
+ });