groundswell 0.0.1 → 0.0.3

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 (451) hide show
  1. package/CHANGELOG.md +188 -0
  2. package/README.md +99 -5
  3. package/dist/__tests__/adversarial/attachChild-performance.test.d.ts +16 -0
  4. package/dist/__tests__/adversarial/attachChild-performance.test.d.ts.map +1 -0
  5. package/dist/__tests__/adversarial/attachChild-performance.test.js +187 -0
  6. package/dist/__tests__/adversarial/attachChild-performance.test.js.map +1 -0
  7. package/dist/__tests__/adversarial/circular-reference.test.d.ts +13 -0
  8. package/dist/__tests__/adversarial/circular-reference.test.d.ts.map +1 -0
  9. package/dist/__tests__/adversarial/circular-reference.test.js +92 -0
  10. package/dist/__tests__/adversarial/circular-reference.test.js.map +1 -0
  11. package/dist/__tests__/adversarial/complex-circular-reference.test.d.ts +16 -0
  12. package/dist/__tests__/adversarial/complex-circular-reference.test.d.ts.map +1 -0
  13. package/dist/__tests__/adversarial/complex-circular-reference.test.js +127 -0
  14. package/dist/__tests__/adversarial/complex-circular-reference.test.js.map +1 -0
  15. package/dist/__tests__/adversarial/concurrent-task-failures.test.d.ts +21 -0
  16. package/dist/__tests__/adversarial/concurrent-task-failures.test.d.ts.map +1 -0
  17. package/dist/__tests__/adversarial/concurrent-task-failures.test.js +667 -0
  18. package/dist/__tests__/adversarial/concurrent-task-failures.test.js.map +1 -0
  19. package/dist/__tests__/adversarial/deep-analysis.test.d.ts +6 -0
  20. package/dist/__tests__/adversarial/deep-analysis.test.d.ts.map +1 -0
  21. package/dist/__tests__/adversarial/deep-analysis.test.js +877 -0
  22. package/dist/__tests__/adversarial/deep-analysis.test.js.map +1 -0
  23. package/dist/__tests__/adversarial/deep-hierarchy-stress.test.d.ts +13 -0
  24. package/dist/__tests__/adversarial/deep-hierarchy-stress.test.d.ts.map +1 -0
  25. package/dist/__tests__/adversarial/deep-hierarchy-stress.test.js +186 -0
  26. package/dist/__tests__/adversarial/deep-hierarchy-stress.test.js.map +1 -0
  27. package/dist/__tests__/adversarial/e2e-prd-validation.test.d.ts +6 -0
  28. package/dist/__tests__/adversarial/e2e-prd-validation.test.d.ts.map +1 -0
  29. package/dist/__tests__/adversarial/e2e-prd-validation.test.js +626 -0
  30. package/dist/__tests__/adversarial/e2e-prd-validation.test.js.map +1 -0
  31. package/dist/__tests__/adversarial/edge-case.test.d.ts +6 -0
  32. package/dist/__tests__/adversarial/edge-case.test.d.ts.map +1 -0
  33. package/dist/__tests__/adversarial/edge-case.test.js +857 -0
  34. package/dist/__tests__/adversarial/edge-case.test.js.map +1 -0
  35. package/dist/__tests__/adversarial/error-merge-strategy.test.d.ts +20 -0
  36. package/dist/__tests__/adversarial/error-merge-strategy.test.d.ts.map +1 -0
  37. package/dist/__tests__/adversarial/error-merge-strategy.test.js +907 -0
  38. package/dist/__tests__/adversarial/error-merge-strategy.test.js.map +1 -0
  39. package/dist/__tests__/adversarial/incremental-performance.test.d.ts +2 -0
  40. package/dist/__tests__/adversarial/incremental-performance.test.d.ts.map +1 -0
  41. package/dist/__tests__/adversarial/incremental-performance.test.js +113 -0
  42. package/dist/__tests__/adversarial/incremental-performance.test.js.map +1 -0
  43. package/dist/__tests__/adversarial/node-map-update-benchmarks.test.d.ts +22 -0
  44. package/dist/__tests__/adversarial/node-map-update-benchmarks.test.d.ts.map +1 -0
  45. package/dist/__tests__/adversarial/node-map-update-benchmarks.test.js +383 -0
  46. package/dist/__tests__/adversarial/node-map-update-benchmarks.test.js.map +1 -0
  47. package/dist/__tests__/adversarial/observer-propagation.test.d.ts +21 -0
  48. package/dist/__tests__/adversarial/observer-propagation.test.d.ts.map +1 -0
  49. package/dist/__tests__/adversarial/observer-propagation.test.js +404 -0
  50. package/dist/__tests__/adversarial/observer-propagation.test.js.map +1 -0
  51. package/dist/__tests__/adversarial/parent-validation.test.d.ts +13 -0
  52. package/dist/__tests__/adversarial/parent-validation.test.d.ts.map +1 -0
  53. package/dist/__tests__/adversarial/parent-validation.test.js +128 -0
  54. package/dist/__tests__/adversarial/parent-validation.test.js.map +1 -0
  55. package/dist/__tests__/adversarial/prd-12-2-compliance.test.d.ts +20 -0
  56. package/dist/__tests__/adversarial/prd-12-2-compliance.test.d.ts.map +1 -0
  57. package/dist/__tests__/adversarial/prd-12-2-compliance.test.js +482 -0
  58. package/dist/__tests__/adversarial/prd-12-2-compliance.test.js.map +1 -0
  59. package/dist/__tests__/adversarial/prd-compliance.test.d.ts +6 -0
  60. package/dist/__tests__/adversarial/prd-compliance.test.d.ts.map +1 -0
  61. package/dist/__tests__/adversarial/prd-compliance.test.js +886 -0
  62. package/dist/__tests__/adversarial/prd-compliance.test.js.map +1 -0
  63. package/dist/__tests__/compatibility/backward-compatibility.test.d.ts +22 -0
  64. package/dist/__tests__/compatibility/backward-compatibility.test.d.ts.map +1 -0
  65. package/dist/__tests__/compatibility/backward-compatibility.test.js +1843 -0
  66. package/dist/__tests__/compatibility/backward-compatibility.test.js.map +1 -0
  67. package/dist/__tests__/helpers/index.d.ts +10 -0
  68. package/dist/__tests__/helpers/index.d.ts.map +1 -0
  69. package/dist/__tests__/helpers/index.js +10 -0
  70. package/dist/__tests__/helpers/index.js.map +1 -0
  71. package/dist/__tests__/helpers/tree-verification.d.ts +90 -0
  72. package/dist/__tests__/helpers/tree-verification.d.ts.map +1 -0
  73. package/dist/__tests__/helpers/tree-verification.js +202 -0
  74. package/dist/__tests__/helpers/tree-verification.js.map +1 -0
  75. package/dist/__tests__/integration/agent-workflow.test.d.ts +2 -0
  76. package/dist/__tests__/integration/agent-workflow.test.d.ts.map +1 -0
  77. package/dist/__tests__/integration/agent-workflow.test.js +256 -0
  78. package/dist/__tests__/integration/agent-workflow.test.js.map +1 -0
  79. package/dist/__tests__/integration/bidirectional-consistency.test.d.ts +14 -0
  80. package/dist/__tests__/integration/bidirectional-consistency.test.d.ts.map +1 -0
  81. package/dist/__tests__/integration/bidirectional-consistency.test.js +668 -0
  82. package/dist/__tests__/integration/bidirectional-consistency.test.js.map +1 -0
  83. package/dist/__tests__/integration/observer-logging.test.d.ts +2 -0
  84. package/dist/__tests__/integration/observer-logging.test.d.ts.map +1 -0
  85. package/dist/__tests__/integration/observer-logging.test.js +517 -0
  86. package/dist/__tests__/integration/observer-logging.test.js.map +1 -0
  87. package/dist/__tests__/integration/tree-mirroring.test.d.ts +2 -0
  88. package/dist/__tests__/integration/tree-mirroring.test.d.ts.map +1 -0
  89. package/dist/__tests__/integration/tree-mirroring.test.js +117 -0
  90. package/dist/__tests__/integration/tree-mirroring.test.js.map +1 -0
  91. package/dist/__tests__/integration/workflow-reparenting.test.d.ts +12 -0
  92. package/dist/__tests__/integration/workflow-reparenting.test.d.ts.map +1 -0
  93. package/dist/__tests__/integration/workflow-reparenting.test.js +239 -0
  94. package/dist/__tests__/integration/workflow-reparenting.test.js.map +1 -0
  95. package/dist/__tests__/unit/agent.test.d.ts +2 -0
  96. package/dist/__tests__/unit/agent.test.d.ts.map +1 -0
  97. package/dist/__tests__/unit/agent.test.js +143 -0
  98. package/dist/__tests__/unit/agent.test.js.map +1 -0
  99. package/dist/__tests__/unit/cache-key.test.d.ts +5 -0
  100. package/dist/__tests__/unit/cache-key.test.d.ts.map +1 -0
  101. package/dist/__tests__/unit/cache-key.test.js +145 -0
  102. package/dist/__tests__/unit/cache-key.test.js.map +1 -0
  103. package/dist/__tests__/unit/cache.test.d.ts +5 -0
  104. package/dist/__tests__/unit/cache.test.d.ts.map +1 -0
  105. package/dist/__tests__/unit/cache.test.js +132 -0
  106. package/dist/__tests__/unit/cache.test.js.map +1 -0
  107. package/dist/__tests__/unit/context.test.d.ts +2 -0
  108. package/dist/__tests__/unit/context.test.d.ts.map +1 -0
  109. package/dist/__tests__/unit/context.test.js +220 -0
  110. package/dist/__tests__/unit/context.test.js.map +1 -0
  111. package/dist/__tests__/unit/decorators.test.d.ts +2 -0
  112. package/dist/__tests__/unit/decorators.test.d.ts.map +1 -0
  113. package/dist/__tests__/unit/decorators.test.js +162 -0
  114. package/dist/__tests__/unit/decorators.test.js.map +1 -0
  115. package/dist/__tests__/unit/introspection-tools.test.d.ts +5 -0
  116. package/dist/__tests__/unit/introspection-tools.test.d.ts.map +1 -0
  117. package/dist/__tests__/unit/introspection-tools.test.js +191 -0
  118. package/dist/__tests__/unit/introspection-tools.test.js.map +1 -0
  119. package/dist/__tests__/unit/logger.test.d.ts +2 -0
  120. package/dist/__tests__/unit/logger.test.d.ts.map +1 -0
  121. package/dist/__tests__/unit/logger.test.js +241 -0
  122. package/dist/__tests__/unit/logger.test.js.map +1 -0
  123. package/dist/__tests__/unit/observable.test.d.ts +2 -0
  124. package/dist/__tests__/unit/observable.test.d.ts.map +1 -0
  125. package/dist/__tests__/unit/observable.test.js +251 -0
  126. package/dist/__tests__/unit/observable.test.js.map +1 -0
  127. package/dist/__tests__/unit/prompt.test.d.ts +2 -0
  128. package/dist/__tests__/unit/prompt.test.d.ts.map +1 -0
  129. package/dist/__tests__/unit/prompt.test.js +113 -0
  130. package/dist/__tests__/unit/prompt.test.js.map +1 -0
  131. package/dist/__tests__/unit/reflection.test.d.ts +5 -0
  132. package/dist/__tests__/unit/reflection.test.d.ts.map +1 -0
  133. package/dist/__tests__/unit/reflection.test.js +160 -0
  134. package/dist/__tests__/unit/reflection.test.js.map +1 -0
  135. package/dist/__tests__/unit/tree-debugger-incremental.test.d.ts +2 -0
  136. package/dist/__tests__/unit/tree-debugger-incremental.test.d.ts.map +1 -0
  137. package/dist/__tests__/unit/tree-debugger-incremental.test.js +136 -0
  138. package/dist/__tests__/unit/tree-debugger-incremental.test.js.map +1 -0
  139. package/dist/__tests__/unit/tree-debugger.test.d.ts +2 -0
  140. package/dist/__tests__/unit/tree-debugger.test.d.ts.map +1 -0
  141. package/dist/__tests__/unit/tree-debugger.test.js +69 -0
  142. package/dist/__tests__/unit/tree-debugger.test.js.map +1 -0
  143. package/dist/__tests__/unit/utils/workflow-error-utils.test.d.ts +2 -0
  144. package/dist/__tests__/unit/utils/workflow-error-utils.test.d.ts.map +1 -0
  145. package/dist/__tests__/unit/utils/workflow-error-utils.test.js +154 -0
  146. package/dist/__tests__/unit/utils/workflow-error-utils.test.js.map +1 -0
  147. package/dist/__tests__/unit/workflow-detachChild.test.d.ts +2 -0
  148. package/dist/__tests__/unit/workflow-detachChild.test.d.ts.map +1 -0
  149. package/dist/__tests__/unit/workflow-detachChild.test.js +76 -0
  150. package/dist/__tests__/unit/workflow-detachChild.test.js.map +1 -0
  151. package/dist/__tests__/unit/workflow-emitEvent-childDetached.test.d.ts +2 -0
  152. package/dist/__tests__/unit/workflow-emitEvent-childDetached.test.d.ts.map +1 -0
  153. package/dist/__tests__/unit/workflow-emitEvent-childDetached.test.js +122 -0
  154. package/dist/__tests__/unit/workflow-emitEvent-childDetached.test.js.map +1 -0
  155. package/dist/__tests__/unit/workflow-isDescendantOf.test.d.ts +2 -0
  156. package/dist/__tests__/unit/workflow-isDescendantOf.test.d.ts.map +1 -0
  157. package/dist/__tests__/unit/workflow-isDescendantOf.test.js +140 -0
  158. package/dist/__tests__/unit/workflow-isDescendantOf.test.js.map +1 -0
  159. package/dist/__tests__/unit/workflow.test.d.ts +2 -0
  160. package/dist/__tests__/unit/workflow.test.d.ts.map +1 -0
  161. package/dist/__tests__/unit/workflow.test.js +330 -0
  162. package/dist/__tests__/unit/workflow.test.js.map +1 -0
  163. package/dist/cache/cache-key.d.ts +66 -0
  164. package/dist/cache/cache-key.d.ts.map +1 -0
  165. package/dist/cache/cache-key.js +195 -0
  166. package/dist/cache/cache-key.js.map +1 -0
  167. package/dist/cache/cache.d.ts +104 -0
  168. package/dist/cache/cache.d.ts.map +1 -0
  169. package/dist/cache/cache.js +179 -0
  170. package/dist/cache/cache.js.map +1 -0
  171. package/{src/cache/index.ts → dist/cache/index.d.ts} +1 -1
  172. package/dist/cache/index.d.ts.map +1 -0
  173. package/dist/cache/index.js +6 -0
  174. package/dist/cache/index.js.map +1 -0
  175. package/dist/core/agent.d.ts +112 -0
  176. package/dist/core/agent.d.ts.map +1 -0
  177. package/dist/core/agent.js +426 -0
  178. package/dist/core/agent.js.map +1 -0
  179. package/{src/core/context.ts → dist/core/context.d.ts} +16 -67
  180. package/dist/core/context.d.ts.map +1 -0
  181. package/dist/core/context.js +80 -0
  182. package/dist/core/context.js.map +1 -0
  183. package/dist/core/event-tree.d.ts +72 -0
  184. package/dist/core/event-tree.d.ts.map +1 -0
  185. package/dist/core/event-tree.js +211 -0
  186. package/dist/core/event-tree.js.map +1 -0
  187. package/{src/core/factory.ts → dist/core/factory.d.ts} +6 -27
  188. package/dist/core/factory.d.ts.map +1 -0
  189. package/dist/core/factory.js +110 -0
  190. package/dist/core/factory.js.map +1 -0
  191. package/{src/core/index.ts → dist/core/index.d.ts} +2 -10
  192. package/dist/core/index.d.ts.map +1 -0
  193. package/dist/core/index.js +9 -0
  194. package/dist/core/index.js.map +1 -0
  195. package/dist/core/logger.d.ts +50 -0
  196. package/dist/core/logger.d.ts.map +1 -0
  197. package/dist/core/logger.js +91 -0
  198. package/dist/core/logger.js.map +1 -0
  199. package/dist/core/mcp-handler.d.ts +69 -0
  200. package/dist/core/mcp-handler.d.ts.map +1 -0
  201. package/dist/core/mcp-handler.js +143 -0
  202. package/dist/core/mcp-handler.js.map +1 -0
  203. package/dist/core/prompt.d.ts +80 -0
  204. package/dist/core/prompt.d.ts.map +1 -0
  205. package/dist/core/prompt.js +120 -0
  206. package/dist/core/prompt.js.map +1 -0
  207. package/dist/core/workflow-context.d.ts +57 -0
  208. package/dist/core/workflow-context.d.ts.map +1 -0
  209. package/dist/core/workflow-context.js +263 -0
  210. package/dist/core/workflow-context.js.map +1 -0
  211. package/dist/core/workflow.d.ts +241 -0
  212. package/dist/core/workflow.d.ts.map +1 -0
  213. package/dist/core/workflow.js +464 -0
  214. package/dist/core/workflow.js.map +1 -0
  215. package/dist/debugger/index.d.ts +2 -0
  216. package/dist/debugger/index.d.ts.map +1 -0
  217. package/{src/debugger/index.ts → dist/debugger/index.js} +1 -0
  218. package/dist/debugger/index.js.map +1 -0
  219. package/dist/debugger/tree-debugger.d.ts +71 -0
  220. package/dist/debugger/tree-debugger.d.ts.map +1 -0
  221. package/dist/debugger/tree-debugger.js +198 -0
  222. package/dist/debugger/tree-debugger.js.map +1 -0
  223. package/dist/decorators/index.d.ts +4 -0
  224. package/dist/decorators/index.d.ts.map +1 -0
  225. package/{src/decorators/index.ts → dist/decorators/index.js} +1 -0
  226. package/dist/decorators/index.js.map +1 -0
  227. package/dist/decorators/observed-state.d.ts +32 -0
  228. package/dist/decorators/observed-state.d.ts.map +1 -0
  229. package/dist/decorators/observed-state.js +79 -0
  230. package/dist/decorators/observed-state.js.map +1 -0
  231. package/dist/decorators/step.d.ts +15 -0
  232. package/dist/decorators/step.d.ts.map +1 -0
  233. package/dist/decorators/step.js +110 -0
  234. package/dist/decorators/step.js.map +1 -0
  235. package/dist/decorators/task.d.ts +50 -0
  236. package/dist/decorators/task.d.ts.map +1 -0
  237. package/dist/decorators/task.js +118 -0
  238. package/dist/decorators/task.js.map +1 -0
  239. package/dist/examples/index.d.ts +3 -0
  240. package/dist/examples/index.d.ts.map +1 -0
  241. package/{src/examples/index.ts → dist/examples/index.js} +1 -0
  242. package/dist/examples/index.js.map +1 -0
  243. package/dist/examples/tdd-orchestrator.d.ts +15 -0
  244. package/dist/examples/tdd-orchestrator.d.ts.map +1 -0
  245. package/dist/examples/tdd-orchestrator.js +121 -0
  246. package/dist/examples/tdd-orchestrator.js.map +1 -0
  247. package/dist/examples/test-cycle-workflow.d.ts +14 -0
  248. package/dist/examples/test-cycle-workflow.d.ts.map +1 -0
  249. package/dist/examples/test-cycle-workflow.js +116 -0
  250. package/dist/examples/test-cycle-workflow.js.map +1 -0
  251. package/dist/index.d.ts +27 -0
  252. package/dist/index.d.ts.map +1 -0
  253. package/dist/index.js +40 -0
  254. package/dist/index.js.map +1 -0
  255. package/dist/reflection/index.d.ts +5 -0
  256. package/dist/reflection/index.d.ts.map +1 -0
  257. package/{src/reflection/index.ts → dist/reflection/index.js} +1 -1
  258. package/dist/reflection/index.js.map +1 -0
  259. package/dist/reflection/reflection.d.ts +84 -0
  260. package/dist/reflection/reflection.d.ts.map +1 -0
  261. package/dist/reflection/reflection.js +329 -0
  262. package/dist/reflection/reflection.js.map +1 -0
  263. package/dist/tools/index.d.ts +6 -0
  264. package/dist/tools/index.d.ts.map +1 -0
  265. package/dist/tools/index.js +11 -0
  266. package/dist/tools/index.js.map +1 -0
  267. package/dist/tools/introspection.d.ts +165 -0
  268. package/dist/tools/introspection.d.ts.map +1 -0
  269. package/dist/tools/introspection.js +324 -0
  270. package/dist/tools/introspection.js.map +1 -0
  271. package/dist/types/agent.d.ts +66 -0
  272. package/dist/types/agent.d.ts.map +1 -0
  273. package/dist/types/agent.js +6 -0
  274. package/dist/types/agent.js.map +1 -0
  275. package/dist/types/decorators.d.ts +31 -0
  276. package/dist/types/decorators.d.ts.map +1 -0
  277. package/dist/types/decorators.js +2 -0
  278. package/dist/types/decorators.js.map +1 -0
  279. package/dist/types/error-strategy.d.ts +13 -0
  280. package/dist/types/error-strategy.d.ts.map +1 -0
  281. package/dist/types/error-strategy.js +2 -0
  282. package/dist/types/error-strategy.js.map +1 -0
  283. package/dist/types/error.d.ts +20 -0
  284. package/dist/types/error.d.ts.map +1 -0
  285. package/dist/types/error.js +2 -0
  286. package/dist/types/error.js.map +1 -0
  287. package/dist/types/events.d.ts +87 -0
  288. package/dist/types/events.d.ts.map +1 -0
  289. package/dist/types/events.js +2 -0
  290. package/dist/types/events.js.map +1 -0
  291. package/dist/types/index.d.ts +15 -0
  292. package/dist/types/index.d.ts.map +1 -0
  293. package/dist/types/index.js +2 -0
  294. package/dist/types/index.js.map +1 -0
  295. package/dist/types/logging.d.ts +24 -0
  296. package/dist/types/logging.d.ts.map +1 -0
  297. package/dist/types/logging.js +2 -0
  298. package/dist/types/logging.js.map +1 -0
  299. package/dist/types/observer.d.ts +18 -0
  300. package/dist/types/observer.d.ts.map +1 -0
  301. package/dist/types/observer.js +2 -0
  302. package/dist/types/observer.js.map +1 -0
  303. package/dist/types/prompt.d.ts +31 -0
  304. package/dist/types/prompt.d.ts.map +1 -0
  305. package/dist/types/prompt.js +6 -0
  306. package/dist/types/prompt.js.map +1 -0
  307. package/dist/types/reflection.d.ts +96 -0
  308. package/dist/types/reflection.d.ts.map +1 -0
  309. package/dist/types/reflection.js +24 -0
  310. package/dist/types/reflection.js.map +1 -0
  311. package/dist/types/sdk-primitives.d.ts +118 -0
  312. package/dist/types/sdk-primitives.d.ts.map +1 -0
  313. package/dist/types/sdk-primitives.js +6 -0
  314. package/dist/types/sdk-primitives.js.map +1 -0
  315. package/{src/types/snapshot.ts → dist/types/snapshot.d.ts} +5 -5
  316. package/dist/types/snapshot.d.ts.map +1 -0
  317. package/dist/types/snapshot.js +2 -0
  318. package/dist/types/snapshot.js.map +1 -0
  319. package/dist/types/workflow-context.d.ts +139 -0
  320. package/dist/types/workflow-context.d.ts.map +1 -0
  321. package/dist/types/workflow-context.js +8 -0
  322. package/dist/types/workflow-context.js.map +1 -0
  323. package/dist/types/workflow.d.ts +30 -0
  324. package/dist/types/workflow.d.ts.map +1 -0
  325. package/dist/types/workflow.js +2 -0
  326. package/dist/types/workflow.js.map +1 -0
  327. package/dist/utils/id.d.ts +6 -0
  328. package/dist/utils/id.d.ts.map +1 -0
  329. package/dist/utils/id.js +12 -0
  330. package/dist/utils/id.js.map +1 -0
  331. package/{src/utils/index.ts → dist/utils/index.d.ts} +2 -0
  332. package/dist/utils/index.d.ts.map +1 -0
  333. package/dist/utils/index.js +4 -0
  334. package/dist/utils/index.js.map +1 -0
  335. package/dist/utils/observable.d.ts +54 -0
  336. package/dist/utils/observable.d.ts.map +1 -0
  337. package/dist/utils/observable.js +82 -0
  338. package/dist/utils/observable.js.map +1 -0
  339. package/dist/utils/workflow-error-utils.d.ts +22 -0
  340. package/dist/utils/workflow-error-utils.d.ts.map +1 -0
  341. package/dist/utils/workflow-error-utils.js +45 -0
  342. package/dist/utils/workflow-error-utils.js.map +1 -0
  343. package/package.json +7 -2
  344. package/.claude/settings.local.json +0 -9
  345. package/.claude/system_prompts/task-breakdown.md +0 -100
  346. package/PRPs/001-hierarchical-workflow-engine.md +0 -2438
  347. package/PRPs/PRDs/001-hierarchical-workflow-engine.md +0 -543
  348. package/PRPs/PRDs/002-agent-prompt.md +0 -390
  349. package/PRPs/PRDs/003-agent-prompt.md +0 -943
  350. package/PRPs/PRDs/004-agent-prompt.md +0 -1136
  351. package/PRPs/PRDs/tasks-001.json +0 -492
  352. package/PRPs/README.md +0 -83
  353. package/PRPs/templates/prp_base.md +0 -222
  354. package/docs/agent.md +0 -422
  355. package/docs/prompt.md +0 -419
  356. package/docs/workflow.md +0 -600
  357. package/examples/README.md +0 -244
  358. package/examples/examples/01-basic-workflow.ts +0 -100
  359. package/examples/examples/02-decorator-options.ts +0 -217
  360. package/examples/examples/03-parent-child.ts +0 -241
  361. package/examples/examples/04-observers-debugger.ts +0 -340
  362. package/examples/examples/05-error-handling.ts +0 -387
  363. package/examples/examples/06-concurrent-tasks.ts +0 -352
  364. package/examples/examples/07-agent-loops.ts +0 -432
  365. package/examples/examples/08-sdk-features.ts +0 -667
  366. package/examples/examples/09-reflection.ts +0 -573
  367. package/examples/examples/10-introspection.ts +0 -550
  368. package/examples/index.ts +0 -143
  369. package/examples/utils/helpers.ts +0 -57
  370. package/llms_full.txt +0 -5890
  371. package/plan/P1P2/PRP.md +0 -527
  372. package/plan/P1P2/research/LRU_CACHE_BEST_PRACTICES.md +0 -1929
  373. package/plan/P1P2/research/LRU_CACHE_CODE_PATTERNS.md +0 -857
  374. package/plan/P1P2/research/LRU_CACHE_INTEGRATION_GUIDE.md +0 -738
  375. package/plan/P1P2/research/LRU_CACHE_RESEARCH_INDEX.md +0 -424
  376. package/plan/P1P2/research/REFLECTION_INDEX.md +0 -291
  377. package/plan/P1P2/research/REFLECTION_RESEARCH_REPORT.md +0 -1342
  378. package/plan/P1P2/research/RESEARCH_SUMMARY.md +0 -342
  379. package/plan/P1P2/research/anthropic-sdk.md +0 -174
  380. package/plan/P1P2/research/async-local-storage.md +0 -200
  381. package/plan/P1P2/research/reflection-code-patterns.md +0 -1205
  382. package/plan/P1P2/research/reflection-decision-matrix.md +0 -421
  383. package/plan/P1P2/research/reflection-implementation-guide.md +0 -1341
  384. package/plan/P1P2/research/reflection-integration-guide.md +0 -834
  385. package/plan/P1P2/research/reflection-patterns.md +0 -1468
  386. package/plan/P1P2/research/reflection-quick-reference.md +0 -558
  387. package/plan/P1P2/research/zod-schema.md +0 -152
  388. package/plan/P3P4/PRP.md +0 -1388
  389. package/plan/P3P4/research/caching-lru.md +0 -116
  390. package/plan/P3P4/research/introspection-tools.md +0 -177
  391. package/plan/P3P4/research/reflection-patterns.md +0 -117
  392. package/plan/P4P5/PRP.md +0 -1136
  393. package/plan/P4P5/research/RESEARCH_SUMMARY.md +0 -151
  394. package/plan/architecture/external_deps.md +0 -358
  395. package/plan/architecture/system_context.md +0 -242
  396. package/plan/backlog.json +0 -867
  397. package/plan/research/INTROSPECTION_RESEARCH_SUMMARY.md +0 -378
  398. package/plan/research/README-INTROSPECTION.md +0 -352
  399. package/plan/research/agent-introspection-patterns.md +0 -1085
  400. package/plan/research/introspection-security-guide.md +0 -928
  401. package/plan/research/introspection-tool-examples.md +0 -875
  402. package/scripts/generate-llms-full.ts +0 -206
  403. package/src/__tests__/integration/agent-workflow.test.ts +0 -256
  404. package/src/__tests__/integration/tree-mirroring.test.ts +0 -114
  405. package/src/__tests__/unit/agent.test.ts +0 -169
  406. package/src/__tests__/unit/cache-key.test.ts +0 -182
  407. package/src/__tests__/unit/cache.test.ts +0 -172
  408. package/src/__tests__/unit/context.test.ts +0 -138
  409. package/src/__tests__/unit/decorators.test.ts +0 -100
  410. package/src/__tests__/unit/introspection-tools.test.ts +0 -277
  411. package/src/__tests__/unit/prompt.test.ts +0 -135
  412. package/src/__tests__/unit/reflection.test.ts +0 -210
  413. package/src/__tests__/unit/tree-debugger.test.ts +0 -85
  414. package/src/__tests__/unit/workflow.test.ts +0 -81
  415. package/src/cache/cache-key.ts +0 -244
  416. package/src/cache/cache.ts +0 -236
  417. package/src/core/agent.ts +0 -573
  418. package/src/core/event-tree.ts +0 -260
  419. package/src/core/logger.ts +0 -87
  420. package/src/core/mcp-handler.ts +0 -184
  421. package/src/core/prompt.ts +0 -150
  422. package/src/core/workflow-context.ts +0 -349
  423. package/src/core/workflow.ts +0 -302
  424. package/src/debugger/tree-debugger.ts +0 -210
  425. package/src/decorators/observed-state.ts +0 -95
  426. package/src/decorators/step.ts +0 -139
  427. package/src/decorators/task.ts +0 -96
  428. package/src/examples/tdd-orchestrator.ts +0 -65
  429. package/src/examples/test-cycle-workflow.ts +0 -64
  430. package/src/index.ts +0 -140
  431. package/src/reflection/reflection.ts +0 -407
  432. package/src/tools/index.ts +0 -36
  433. package/src/tools/introspection.ts +0 -464
  434. package/src/types/agent.ts +0 -90
  435. package/src/types/decorators.ts +0 -25
  436. package/src/types/error-strategy.ts +0 -13
  437. package/src/types/error.ts +0 -20
  438. package/src/types/events.ts +0 -74
  439. package/src/types/index.ts +0 -55
  440. package/src/types/logging.ts +0 -24
  441. package/src/types/observer.ts +0 -18
  442. package/src/types/prompt.ts +0 -40
  443. package/src/types/reflection.ts +0 -117
  444. package/src/types/sdk-primitives.ts +0 -128
  445. package/src/types/workflow-context.ts +0 -163
  446. package/src/types/workflow.ts +0 -37
  447. package/src/utils/id.ts +0 -11
  448. package/src/utils/observable.ts +0 -77
  449. package/tasks.json +0 -0
  450. package/tsconfig.json +0 -22
  451. package/vitest.config.ts +0 -16
@@ -1,834 +0,0 @@
1
- # Reflection Integration Guide for Groundswell Agent Framework
2
-
3
- This guide explains how to integrate reflection patterns into the Groundswell workflow orchestration engine.
4
-
5
- ## Architecture Overview
6
-
7
- Groundswell's hierarchical workflow engine provides the perfect foundation for reflection:
8
-
9
- ```
10
- Workflow
11
- ├── Step 1 (with reflection capability)
12
- ├── Step 2 (with reflection capability)
13
- └── Step 3 (with reflection capability)
14
- └── Child Workflow
15
- ├── Sub-Step 1 (with reflection capability)
16
- └── Sub-Step 2 (with reflection capability)
17
- ```
18
-
19
- Each step/task can have reflection configured independently, allowing fine-grained control over when and how reflection occurs.
20
-
21
- ## Core Integration Points
22
-
23
- ### 1. **Step-Level Reflection**
24
-
25
- Add reflection configuration to the `@Step` decorator:
26
-
27
- ```typescript
28
- interface StepReflectionConfig {
29
- enabled: boolean;
30
- trigger: "always" | "on-error" | "low-confidence";
31
- maxAttempts: number;
32
- validationRules?: ValidationRule[];
33
- confidenceThreshold?: number;
34
- }
35
-
36
- @Step({
37
- name: "GenerateCode",
38
- reflection: {
39
- enabled: true,
40
- trigger: "on-error",
41
- maxAttempts: 3,
42
- validationRules: [
43
- { name: "syntax", validate: (output) => checkSyntax(output) },
44
- { name: "hasTests", validate: (output) => output.includes("test") }
45
- ]
46
- }
47
- })
48
- async generateCode(requirement: string): Promise<string> {
49
- // Implementation
50
- }
51
- ```
52
-
53
- ### 2. **Task-Level Reflection**
54
-
55
- Configure reflection at the task/workflow level:
56
-
57
- ```typescript
58
- @Task({
59
- name: "CodeReviewWorkflow",
60
- reflection: {
61
- enabled: true,
62
- trigger: "on-error",
63
- maxAttempts: 3,
64
- stateCapture: true, // Capture state before/after
65
- errorCategories: {
66
- transient: { retries: 3, backoff: "exponential" },
67
- logical: { retries: 2, backoff: "linear" },
68
- invalid: { retries: 0, escalate: true }
69
- }
70
- }
71
- })
72
- async reviewCode() {
73
- // Implementation
74
- }
75
- ```
76
-
77
- ### 3. **Workflow-Level Reflection**
78
-
79
- Configure reflection at the orchestration level:
80
-
81
- ```typescript
82
- class TestCycleWorkflow {
83
- reflection: WorkflowReflectionConfig = {
84
- enabled: true,
85
- trigger: "on-workflow-error",
86
- maxRounds: 3,
87
- evaluationCriteria: {
88
- allTestsPassed: true,
89
- coverageAbove: 85,
90
- noBlockingIssues: true
91
- },
92
- multiAgentCritique: {
93
- enabled: true,
94
- models: ["claude-opus-4.5", "claude-opus-4.5"] // Generator + Critic
95
- }
96
- };
97
-
98
- async execute() {
99
- // Multi-level workflow with reflection
100
- }
101
- }
102
- ```
103
-
104
- ## Implementation Patterns for Groundswell
105
-
106
- ### Pattern 1: Automatic Retry with Error Reflection
107
-
108
- ```typescript
109
- import { Step, Task, WorkflowContext } from "@groundswell/core";
110
-
111
- @Step({
112
- name: "ExecuteWithReflection",
113
- reflection: {
114
- enabled: true,
115
- trigger: "on-error",
116
- maxAttempts: 3
117
- }
118
- })
119
- async executeWithReflection(
120
- ctx: WorkflowContext,
121
- input: string
122
- ): Promise<string> {
123
- const state = {
124
- attempt: 0,
125
- lastError: null,
126
- attempts: [] as Array<{ output: string; error: string | null }>
127
- };
128
-
129
- while (state.attempt < 3) {
130
- state.attempt++;
131
-
132
- try {
133
- const output = await this.attemptExecution(input, state.attempts);
134
-
135
- // Emit success event
136
- ctx.emit({
137
- type: "step:success",
138
- step: "ExecuteWithReflection",
139
- attempt: state.attempt,
140
- output
141
- });
142
-
143
- return output;
144
- } catch (error) {
145
- state.lastError = error;
146
- state.attempts.push({ output: "", error: String(error) });
147
-
148
- // Emit error event for observability
149
- ctx.emit({
150
- type: "step:error",
151
- step: "ExecuteWithReflection",
152
- attempt: state.attempt,
153
- error: String(error)
154
- });
155
-
156
- // Trigger reflection if not last attempt
157
- if (state.attempt < 3) {
158
- const reflection = await this.reflect(
159
- input,
160
- String(error),
161
- state.attempts
162
- );
163
-
164
- ctx.emit({
165
- type: "step:reflection",
166
- step: "ExecuteWithReflection",
167
- attempt: state.attempt,
168
- reflection
169
- });
170
- }
171
- }
172
- }
173
-
174
- throw new Error(
175
- `Step failed after 3 attempts. Last error: ${state.lastError}`
176
- );
177
- }
178
-
179
- private async attemptExecution(
180
- input: string,
181
- history: Array<{ output: string; error: string | null }>
182
- ): Promise<string> {
183
- const historyContext = history.length > 0
184
- ? `Previous attempts failed with: ${history
185
- .map((h) => h.error)
186
- .join("; ")}`
187
- : "";
188
-
189
- const response = await client.messages.create({
190
- model: "claude-opus-4.5",
191
- max_tokens: 1024,
192
- messages: [
193
- {
194
- role: "user",
195
- content: `${input}\n${historyContext}`,
196
- },
197
- ],
198
- });
199
-
200
- return response.content[0].text || "";
201
- }
202
-
203
- private async reflect(
204
- input: string,
205
- error: string,
206
- attempts: Array<{ output: string; error: string | null }>
207
- ): Promise<string> {
208
- const response = await client.messages.create({
209
- model: "claude-opus-4.5",
210
- max_tokens: 400,
211
- messages: [
212
- {
213
- role: "user",
214
- content: `Task: ${input}
215
-
216
- Failed with: ${error}
217
-
218
- What should we try differently?`,
219
- },
220
- ],
221
- });
222
-
223
- return response.content[0].text || "";
224
- }
225
- ```
226
-
227
- ### Pattern 2: Validation-Driven Reflection
228
-
229
- ```typescript
230
- @Step({
231
- name: "GenerateAndValidate",
232
- reflection: {
233
- enabled: true,
234
- trigger: "on-validation-failure",
235
- maxAttempts: 3,
236
- validationRules: [
237
- { name: "required_fields", validate: (output) => hasRequiredFields(output) },
238
- { name: "type_safety", validate: (output) => isTypeValid(output) },
239
- { name: "business_rules", validate: (output) => meetsBusinessRules(output) }
240
- ]
241
- }
242
- })
243
- async generateAndValidate(
244
- ctx: WorkflowContext,
245
- requirement: string
246
- ): Promise<string> {
247
- const validationRules = [
248
- { name: "required_fields", validate: (output: string) => hasRequiredFields(output) },
249
- { name: "type_safety", validate: (output: string) => isTypeValid(output) },
250
- { name: "business_rules", validate: (output: string) => meetsBusinessRules(output) }
251
- ];
252
-
253
- for (let attempt = 1; attempt <= 3; attempt++) {
254
- const output = await this.generate(requirement);
255
-
256
- const violations = this.validate(output, validationRules);
257
-
258
- if (violations.length === 0) {
259
- ctx.emit({
260
- type: "step:validation-passed",
261
- step: "GenerateAndValidate",
262
- attempt
263
- });
264
- return output;
265
- }
266
-
267
- ctx.emit({
268
- type: "step:validation-failed",
269
- step: "GenerateAndValidate",
270
- attempt,
271
- violations: violations.map((v) => v.name)
272
- });
273
-
274
- if (attempt < 3) {
275
- const reflection = await this.reflectOnViolations(
276
- requirement,
277
- output,
278
- violations
279
- );
280
-
281
- ctx.emit({
282
- type: "step:reflection",
283
- step: "GenerateAndValidate",
284
- attempt,
285
- reflection,
286
- violationCount: violations.length
287
- });
288
- }
289
- }
290
-
291
- throw new Error("Validation failed after 3 attempts");
292
- }
293
-
294
- private validate(
295
- output: string,
296
- rules: Array<{ name: string; validate: (output: string) => boolean }>
297
- ): Array<{ name: string; description: string }> {
298
- const violations = [];
299
- for (const rule of rules) {
300
- if (!rule.validate(output)) {
301
- violations.push({
302
- name: rule.name,
303
- description: `Failed: ${rule.name}`
304
- });
305
- }
306
- }
307
- return violations;
308
- }
309
-
310
- private async reflectOnViolations(
311
- requirement: string,
312
- output: string,
313
- violations: Array<{ name: string; description: string }>
314
- ): Promise<string> {
315
- const response = await client.messages.create({
316
- model: "claude-opus-4.5",
317
- max_tokens: 400,
318
- messages: [
319
- {
320
- role: "user",
321
- content: `Requirement: ${requirement}
322
-
323
- Your output violated these rules:
324
- ${violations.map((v) => `- ${v.description}`).join("\n")}
325
-
326
- Output: ${output}
327
-
328
- How would you fix these violations?`,
329
- },
330
- ],
331
- });
332
-
333
- return response.content[0].text || "";
334
- }
335
- ```
336
-
337
- ### Pattern 3: Confidence-Based Reflection
338
-
339
- ```typescript
340
- interface ConfidenceOutput {
341
- content: string;
342
- confidence: number;
343
- uncertaintyAreas: string[];
344
- }
345
-
346
- @Step({
347
- name: "HighConfidenceGeneration",
348
- reflection: {
349
- enabled: true,
350
- trigger: "low-confidence",
351
- confidenceThreshold: 0.75,
352
- maxAttempts: 2
353
- }
354
- })
355
- async highConfidenceGeneration(
356
- ctx: WorkflowContext,
357
- task: string
358
- ): Promise<string> {
359
- // First pass with confidence assessment
360
- const firstPass = await this.generateWithConfidence(task);
361
-
362
- ctx.emit({
363
- type: "step:confidence-assessment",
364
- step: "HighConfidenceGeneration",
365
- confidence: firstPass.confidence,
366
- uncertaintyAreas: firstPass.uncertaintyAreas
367
- });
368
-
369
- if (firstPass.confidence >= 0.75) {
370
- return firstPass.content;
371
- }
372
-
373
- // Low confidence, reflect and improve
374
- const improved = await this.reflectOnUncertainty(
375
- task,
376
- firstPass
377
- );
378
-
379
- ctx.emit({
380
- type: "step:low-confidence-reflection",
381
- step: "HighConfidenceGeneration",
382
- initialConfidence: firstPass.confidence,
383
- reflection: improved
384
- });
385
-
386
- return improved;
387
- }
388
-
389
- private async generateWithConfidence(task: string): Promise<ConfidenceOutput> {
390
- const response = await client.messages.create({
391
- model: "claude-opus-4.5",
392
- max_tokens: 1200,
393
- messages: [
394
- {
395
- role: "user",
396
- content: `${task}
397
-
398
- Assess your own confidence (respond with JSON after your answer):
399
- {"confidence": 0-1, "uncertaintyAreas": ["area1", "area2"]}`,
400
- },
401
- ],
402
- });
403
-
404
- const text = response.content[0].text || "";
405
- const jsonMatch = text.match(/\{[\s\S]*\}$/);
406
-
407
- if (jsonMatch) {
408
- const meta = JSON.parse(jsonMatch[0]);
409
- return {
410
- content: text.substring(0, text.lastIndexOf("{")),
411
- confidence: meta.confidence,
412
- uncertaintyAreas: meta.uncertaintyAreas || []
413
- };
414
- }
415
-
416
- return {
417
- content: text,
418
- confidence: 0.5,
419
- uncertaintyAreas: []
420
- };
421
- }
422
-
423
- private async reflectOnUncertainty(
424
- task: string,
425
- output: ConfidenceOutput
426
- ): Promise<string> {
427
- const response = await client.messages.create({
428
- model: "claude-opus-4.5",
429
- max_tokens: 1024,
430
- messages: [
431
- {
432
- role: "user",
433
- content: `Task: ${task}
434
-
435
- Your response (confidence: ${output.confidence}):
436
- ${output.content}
437
-
438
- You identified uncertainty in: ${output.uncertaintyAreas.join(", ")}
439
-
440
- Provide an improved response that addresses these uncertainty areas with more detail and evidence.`,
441
- },
442
- ],
443
- });
444
-
445
- return response.content[0].text || "";
446
- }
447
- ```
448
-
449
- ### Pattern 4: Multi-Agent Reflection in Workflow
450
-
451
- ```typescript
452
- @Task({
453
- name: "ReviewAndIterateWorkflow",
454
- reflection: {
455
- enabled: true,
456
- multiAgentCritique: {
457
- enabled: true,
458
- generatorModel: "claude-opus-4.5",
459
- criticModel: "claude-opus-4.5",
460
- maxRounds: 3
461
- }
462
- }
463
- })
464
- class ReviewAndIterateWorkflow {
465
- @Step()
466
- async generateOutput(ctx: WorkflowContext, requirement: string): Promise<string> {
467
- const response = await client.messages.create({
468
- model: "claude-opus-4.5",
469
- max_tokens: 1024,
470
- messages: [{ role: "user", content: requirement }],
471
- });
472
- return response.content[0].text || "";
473
- }
474
-
475
- @Step()
476
- async criticReview(
477
- ctx: WorkflowContext,
478
- output: string,
479
- requirement: string
480
- ): Promise<{
481
- isSatisfactory: boolean;
482
- feedback: string;
483
- suggestions: string[];
484
- }> {
485
- const response = await client.messages.create({
486
- model: "claude-opus-4.5",
487
- max_tokens: 600,
488
- messages: [
489
- {
490
- role: "user",
491
- content: `Requirement: ${requirement}
492
-
493
- Response to review:
494
- ${output}
495
-
496
- Provide JSON critique:
497
- {"isSatisfactory": boolean, "feedback": string, "suggestions": ["s1", "s2"]}`,
498
- },
499
- ],
500
- });
501
-
502
- const text = response.content[0].text || "{}";
503
- return JSON.parse(text);
504
- }
505
-
506
- @Step()
507
- async improveBasedOnCritique(
508
- ctx: WorkflowContext,
509
- originalOutput: string,
510
- requirement: string,
511
- critique: { feedback: string; suggestions: string[] }
512
- ): Promise<string> {
513
- const response = await client.messages.create({
514
- model: "claude-opus-4.5",
515
- max_tokens: 1024,
516
- messages: [
517
- {
518
- role: "user",
519
- content: `Requirement: ${requirement}
520
-
521
- Original: ${originalOutput}
522
-
523
- Critique: ${critique.feedback}
524
-
525
- Suggestions: ${critique.suggestions.join("; ")}
526
-
527
- Provide improved version.`,
528
- },
529
- ],
530
- });
531
-
532
- return response.content[0].text || "";
533
- }
534
-
535
- async execute(ctx: WorkflowContext, requirement: string): Promise<string> {
536
- let output = await this.generateOutput(ctx, requirement);
537
-
538
- for (let round = 1; round <= 3; round++) {
539
- const critique = await this.criticReview(ctx, output, requirement);
540
-
541
- ctx.emit({
542
- type: "workflow:critique",
543
- round,
544
- isSatisfactory: critique.isSatisfactory,
545
- feedback: critique.feedback
546
- });
547
-
548
- if (critique.isSatisfactory) {
549
- return output;
550
- }
551
-
552
- output = await this.improveBasedOnCritique(
553
- ctx,
554
- output,
555
- requirement,
556
- critique
557
- );
558
- }
559
-
560
- return output;
561
- }
562
- }
563
- ```
564
-
565
- ## State Capture & Observability
566
-
567
- ### State Capture Before Reflection
568
-
569
- ```typescript
570
- interface ReflectionSnapshot {
571
- timestamp: Date;
572
- stepName: string;
573
- attemptNumber: number;
574
- input: any;
575
- output: string;
576
- error: Error | null;
577
- validationViolations: string[];
578
- executionTimeMs: number;
579
- }
580
-
581
- async function captureStateBeforeReflection(
582
- ctx: WorkflowContext,
583
- step: string,
584
- attempt: number,
585
- input: any,
586
- output: string,
587
- error: Error | null,
588
- violations: string[],
589
- executionTimeMs: number
590
- ): Promise<void> {
591
- const snapshot: ReflectionSnapshot = {
592
- timestamp: new Date(),
593
- stepName: step,
594
- attemptNumber: attempt,
595
- input,
596
- output,
597
- error,
598
- validationViolations: violations,
599
- executionTimeMs
600
- };
601
-
602
- // Store in context for downstream access
603
- ctx.state.reflectionHistory = ctx.state.reflectionHistory || [];
604
- ctx.state.reflectionHistory.push(snapshot);
605
-
606
- // Emit event for observability
607
- ctx.emit({
608
- type: "reflection:state-captured",
609
- snapshot
610
- });
611
- }
612
- ```
613
-
614
- ### Event-Based Observability
615
-
616
- ```typescript
617
- // In your workflow/step implementation
618
- ctx.on("step:error", (event) => {
619
- console.log(`Step ${event.step} failed on attempt ${event.attempt}`);
620
- console.log(`Error: ${event.error}`);
621
- });
622
-
623
- ctx.on("step:reflection", (event) => {
624
- console.log(`Reflection triggered for ${event.step}`);
625
- console.log(`Reflection content: ${event.reflection}`);
626
- });
627
-
628
- ctx.on("reflection:state-captured", (event) => {
629
- console.log(`State captured for reflection at ${event.snapshot.timestamp}`);
630
- console.log(`Violations: ${event.snapshot.validationViolations.join(", ")}`);
631
- });
632
-
633
- ctx.on("step:success", (event) => {
634
- console.log(`Step ${event.step} succeeded on attempt ${event.attempt}`);
635
- });
636
- ```
637
-
638
- ## Configuration Best Practices
639
-
640
- ### Development Configuration
641
-
642
- ```typescript
643
- const devConfig = {
644
- reflection: {
645
- enabled: true,
646
- verbose: true,
647
- captureFullState: true,
648
- maxAttempts: 5, // More attempts for testing
649
- logAllAttempts: true,
650
- saveReflectionHistory: true
651
- }
652
- };
653
- ```
654
-
655
- ### Production Configuration
656
-
657
- ```typescript
658
- const prodConfig = {
659
- reflection: {
660
- enabled: true,
661
- verbose: false,
662
- captureFullState: false,
663
- maxAttempts: 3, // Fewer attempts, lower cost
664
- logAllAttempts: false,
665
- saveReflectionHistory: false, // Save only on error
666
- confidenceThreshold: 0.8, // Higher threshold
667
- maxTokensPerReflection: 300,
668
- timeoutMs: 5000
669
- }
670
- };
671
- ```
672
-
673
- ## Cost Optimization
674
-
675
- ### Cost Calculation
676
-
677
- ```
678
- Base Cost = Input Tokens + Output Tokens
679
- Reflection Cost = Base Cost * (Number of Attempts - 1)
680
-
681
- Example:
682
- - Initial: 500 input + 500 output = 1000 tokens
683
- - Reflection (2 retries): 1000 * 2 = 2000 tokens
684
- - Total: 3000 tokens = 3x cost
685
-
686
- Rule of thumb: Each reflection attempt multiplies cost by ~2x
687
- ```
688
-
689
- ### Strategies to Reduce Cost
690
-
691
- 1. **Use cheaper models for reflection**:
692
- ```typescript
693
- const response = await client.messages.create({
694
- model: state.attempt === 1
695
- ? "claude-opus-4.5" // First attempt with capable model
696
- : "claude-haiku-4.5", // Reflection with cheaper model
697
- max_tokens: 1024,
698
- messages
699
- });
700
- ```
701
-
702
- 2. **Limit reflection scope**:
703
- ```typescript
704
- const response = await client.messages.create({
705
- model: "claude-opus-4.5",
706
- max_tokens: state.attempt === 1
707
- ? 2048 // First attempt, more tokens
708
- : 400, // Reflection, fewer tokens
709
- messages
710
- });
711
- ```
712
-
713
- 3. **Confidence-based triggering**:
714
- ```typescript
715
- if (output.confidence > 0.85) {
716
- return output; // Skip reflection if confident
717
- }
718
- ```
719
-
720
- ## Monitoring & Metrics
721
-
722
- ### Key Metrics to Track
723
-
724
- ```typescript
725
- interface ReflectionMetrics {
726
- stepName: string;
727
- totalAttempts: number;
728
- successOnFirstAttempt: number; // % that succeed without reflection
729
- reflectionEffectiveness: number; // % improved by reflection
730
- averageReflectionTime: number; // ms
731
- totalTokensSpent: number;
732
- costMultiplier: number; // How much more expensive than baseline
733
- }
734
-
735
- function calculateMetrics(history: ReflectionSnapshot[]): ReflectionMetrics {
736
- const total = history.length;
737
- const successful = history.filter((h) => h.error === null).length;
738
- const successPercent = successful === 0 ? 0 : 1;
739
-
740
- return {
741
- stepName: history[0]?.stepName || "unknown",
742
- totalAttempts: total,
743
- successOnFirstAttempt: history.filter((h) => h.attemptNumber === 1).length,
744
- reflectionEffectiveness: successPercent,
745
- averageReflectionTime: 0, // Calculated from execution times
746
- totalTokensSpent: 0, // Calculated from usage
747
- costMultiplier: total // More attempts = higher multiplier
748
- };
749
- }
750
- ```
751
-
752
- ## Testing Reflection
753
-
754
- ### Unit Test Pattern
755
-
756
- ```typescript
757
- describe("Step with Reflection", () => {
758
- it("should succeed on first attempt", async () => {
759
- const output = await step.execute(ctx, "simple task");
760
- expect(output).toBeDefined();
761
- expect(ctx.getEventCount("step:error")).toBe(0);
762
- });
763
-
764
- it("should retry on error", async () => {
765
- // Mock to fail first, succeed second
766
- const attempts: number[] = [];
767
- step.generate = jest.fn(async () => {
768
- attempts.push(1);
769
- if (attempts.length === 1) throw new Error("First attempt fails");
770
- return "success";
771
- });
772
-
773
- const output = await step.executeWithReflection(ctx, "task");
774
- expect(output).toBe("success");
775
- expect(attempts.length).toBe(2);
776
- });
777
-
778
- it("should fail after max attempts", async () => {
779
- step.generate = jest.fn(async () => {
780
- throw new Error("Always fails");
781
- });
782
-
783
- await expect(step.executeWithReflection(ctx, "task")).rejects.toThrow();
784
- });
785
- });
786
- ```
787
-
788
- ## Troubleshooting
789
-
790
- ### Infinite Reflection Loops
791
-
792
- **Problem**: Reflection keeps suggesting same fix without making progress
793
-
794
- **Solution**:
795
- ```typescript
796
- // Add state deduplication
797
- const previousOutputs = new Set();
798
- const output = await generate();
799
-
800
- if (previousOutputs.has(output)) {
801
- throw new Error("Stuck in loop, breaking out");
802
- }
803
- previousOutputs.add(output);
804
- ```
805
-
806
- ### Excessive Token Usage
807
-
808
- **Problem**: Reflection consuming too many tokens
809
-
810
- **Solution**:
811
- ```typescript
812
- // Cap tokens per step
813
- const maxTokensPerStep = 2000;
814
- const maxTokensPerAttempt = maxTokensPerStep / maxAttempts;
815
-
816
- messages.max_tokens = Math.min(
817
- 1024,
818
- maxTokensPerAttempt - tokensSoFar
819
- );
820
- ```
821
-
822
- ### Reflection Not Helping
823
-
824
- **Problem**: Output quality not improving with reflection
825
-
826
- **Solution**:
827
- ```typescript
828
- // Track effectiveness
829
- if (attempt > 1 && newOutput.quality <= previousOutput.quality) {
830
- // Reflection didn't help, return original
831
- return previousOutput;
832
- }
833
- ```
834
-