groundswell 0.0.1

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 (120) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.claude/system_prompts/task-breakdown.md +100 -0
  3. package/PRPs/001-hierarchical-workflow-engine.md +2438 -0
  4. package/PRPs/PRDs/001-hierarchical-workflow-engine.md +543 -0
  5. package/PRPs/PRDs/002-agent-prompt.md +390 -0
  6. package/PRPs/PRDs/003-agent-prompt.md +943 -0
  7. package/PRPs/PRDs/004-agent-prompt.md +1136 -0
  8. package/PRPs/PRDs/tasks-001.json +492 -0
  9. package/PRPs/README.md +83 -0
  10. package/PRPs/templates/prp_base.md +222 -0
  11. package/README.md +218 -0
  12. package/docs/agent.md +422 -0
  13. package/docs/prompt.md +419 -0
  14. package/docs/workflow.md +600 -0
  15. package/examples/README.md +244 -0
  16. package/examples/examples/01-basic-workflow.ts +100 -0
  17. package/examples/examples/02-decorator-options.ts +217 -0
  18. package/examples/examples/03-parent-child.ts +241 -0
  19. package/examples/examples/04-observers-debugger.ts +340 -0
  20. package/examples/examples/05-error-handling.ts +387 -0
  21. package/examples/examples/06-concurrent-tasks.ts +352 -0
  22. package/examples/examples/07-agent-loops.ts +432 -0
  23. package/examples/examples/08-sdk-features.ts +667 -0
  24. package/examples/examples/09-reflection.ts +573 -0
  25. package/examples/examples/10-introspection.ts +550 -0
  26. package/examples/index.ts +143 -0
  27. package/examples/utils/helpers.ts +57 -0
  28. package/llms_full.txt +5890 -0
  29. package/package.json +63 -0
  30. package/plan/P1P2/PRP.md +527 -0
  31. package/plan/P1P2/research/LRU_CACHE_BEST_PRACTICES.md +1929 -0
  32. package/plan/P1P2/research/LRU_CACHE_CODE_PATTERNS.md +857 -0
  33. package/plan/P1P2/research/LRU_CACHE_INTEGRATION_GUIDE.md +738 -0
  34. package/plan/P1P2/research/LRU_CACHE_RESEARCH_INDEX.md +424 -0
  35. package/plan/P1P2/research/REFLECTION_INDEX.md +291 -0
  36. package/plan/P1P2/research/REFLECTION_RESEARCH_REPORT.md +1342 -0
  37. package/plan/P1P2/research/RESEARCH_SUMMARY.md +342 -0
  38. package/plan/P1P2/research/anthropic-sdk.md +174 -0
  39. package/plan/P1P2/research/async-local-storage.md +200 -0
  40. package/plan/P1P2/research/reflection-code-patterns.md +1205 -0
  41. package/plan/P1P2/research/reflection-decision-matrix.md +421 -0
  42. package/plan/P1P2/research/reflection-implementation-guide.md +1341 -0
  43. package/plan/P1P2/research/reflection-integration-guide.md +834 -0
  44. package/plan/P1P2/research/reflection-patterns.md +1468 -0
  45. package/plan/P1P2/research/reflection-quick-reference.md +558 -0
  46. package/plan/P1P2/research/zod-schema.md +152 -0
  47. package/plan/P3P4/PRP.md +1388 -0
  48. package/plan/P3P4/research/caching-lru.md +116 -0
  49. package/plan/P3P4/research/introspection-tools.md +177 -0
  50. package/plan/P3P4/research/reflection-patterns.md +117 -0
  51. package/plan/P4P5/PRP.md +1136 -0
  52. package/plan/P4P5/research/RESEARCH_SUMMARY.md +151 -0
  53. package/plan/architecture/external_deps.md +358 -0
  54. package/plan/architecture/system_context.md +242 -0
  55. package/plan/backlog.json +867 -0
  56. package/plan/research/INTROSPECTION_RESEARCH_SUMMARY.md +378 -0
  57. package/plan/research/README-INTROSPECTION.md +352 -0
  58. package/plan/research/agent-introspection-patterns.md +1085 -0
  59. package/plan/research/introspection-security-guide.md +928 -0
  60. package/plan/research/introspection-tool-examples.md +875 -0
  61. package/scripts/generate-llms-full.ts +206 -0
  62. package/src/__tests__/integration/agent-workflow.test.ts +256 -0
  63. package/src/__tests__/integration/tree-mirroring.test.ts +114 -0
  64. package/src/__tests__/unit/agent.test.ts +169 -0
  65. package/src/__tests__/unit/cache-key.test.ts +182 -0
  66. package/src/__tests__/unit/cache.test.ts +172 -0
  67. package/src/__tests__/unit/context.test.ts +138 -0
  68. package/src/__tests__/unit/decorators.test.ts +100 -0
  69. package/src/__tests__/unit/introspection-tools.test.ts +277 -0
  70. package/src/__tests__/unit/prompt.test.ts +135 -0
  71. package/src/__tests__/unit/reflection.test.ts +210 -0
  72. package/src/__tests__/unit/tree-debugger.test.ts +85 -0
  73. package/src/__tests__/unit/workflow.test.ts +81 -0
  74. package/src/cache/cache-key.ts +244 -0
  75. package/src/cache/cache.ts +236 -0
  76. package/src/cache/index.ts +8 -0
  77. package/src/core/agent.ts +573 -0
  78. package/src/core/context.ts +119 -0
  79. package/src/core/event-tree.ts +260 -0
  80. package/src/core/factory.ts +123 -0
  81. package/src/core/index.ts +17 -0
  82. package/src/core/logger.ts +87 -0
  83. package/src/core/mcp-handler.ts +184 -0
  84. package/src/core/prompt.ts +150 -0
  85. package/src/core/workflow-context.ts +349 -0
  86. package/src/core/workflow.ts +302 -0
  87. package/src/debugger/index.ts +1 -0
  88. package/src/debugger/tree-debugger.ts +210 -0
  89. package/src/decorators/index.ts +3 -0
  90. package/src/decorators/observed-state.ts +95 -0
  91. package/src/decorators/step.ts +139 -0
  92. package/src/decorators/task.ts +96 -0
  93. package/src/examples/index.ts +2 -0
  94. package/src/examples/tdd-orchestrator.ts +65 -0
  95. package/src/examples/test-cycle-workflow.ts +64 -0
  96. package/src/index.ts +140 -0
  97. package/src/reflection/index.ts +5 -0
  98. package/src/reflection/reflection.ts +407 -0
  99. package/src/tools/index.ts +36 -0
  100. package/src/tools/introspection.ts +464 -0
  101. package/src/types/agent.ts +90 -0
  102. package/src/types/decorators.ts +25 -0
  103. package/src/types/error-strategy.ts +13 -0
  104. package/src/types/error.ts +20 -0
  105. package/src/types/events.ts +74 -0
  106. package/src/types/index.ts +55 -0
  107. package/src/types/logging.ts +24 -0
  108. package/src/types/observer.ts +18 -0
  109. package/src/types/prompt.ts +40 -0
  110. package/src/types/reflection.ts +117 -0
  111. package/src/types/sdk-primitives.ts +128 -0
  112. package/src/types/snapshot.ts +14 -0
  113. package/src/types/workflow-context.ts +163 -0
  114. package/src/types/workflow.ts +37 -0
  115. package/src/utils/id.ts +11 -0
  116. package/src/utils/index.ts +3 -0
  117. package/src/utils/observable.ts +77 -0
  118. package/tasks.json +0 -0
  119. package/tsconfig.json +22 -0
  120. package/vitest.config.ts +16 -0
@@ -0,0 +1,390 @@
1
+ # **📘 Product Requirements Document (PRD)**
2
+
3
+ ### **Groundswell Orchestration Framework — Source of Truth v1.0**
4
+
5
+ ### **Unified Workflow · Agent · Prompt System**
6
+
7
+ ---
8
+
9
+ # **1. Purpose**
10
+
11
+ This PRD defines the core architecture and feature requirements for the Groundswell orchestration framework. The system provides a fully composable, hierarchical execution environment for workflows, agents, prompts, and dynamically created subflows. It ensures complete observability, strict determinism of structure, dynamic control, and seamless integration with Anthropic’s Agent SDK—including tools, MCPs, hooks, skills, and environment variables.
12
+
13
+ This document is the single source of truth for all functional, structural, and behavioral requirements.
14
+
15
+ ---
16
+
17
+ # **2. System Goals**
18
+
19
+ 1. Full composability of Workflows, Agents, Prompts in arbitrary JavaScript control flow.
20
+ 2. Rich hierarchical observability with an automatically assembled event tree.
21
+ 3. Full compatibility with Anthropic’s Agent SDK (tools, MCPs, hooks, skills, streaming).
22
+ 4. Agents, Prompts, and Workflows that can be instantiated once and used anywhere.
23
+ 5. Runtime dynamic construction of new workflows, agents, and prompts.
24
+ 6. Dynamic context revision (replace or update prompts mid-session).
25
+ 7. Reflection support at Workflow, Agent, and Prompt levels.
26
+ 8. Hierarchical automatic “mounting” similar to React component trees.
27
+ 9. Deterministic logging, traceability, and data inspection across the entire hierarchy.
28
+ 10. Ability for agents to inspect and manipulate their own position in the hierarchy using special tools.
29
+
30
+ ---
31
+
32
+ # **3. Core Entities**
33
+
34
+ The system consists of three primary entities:
35
+
36
+ ```
37
+ Workflow → contains Steps → Steps contain AgentRuns and Subflows
38
+ Agent → executes Prompts via Anthropic’s Agent SDK
39
+ Prompt → immutable declarative definition of a single agent call
40
+ ```
41
+
42
+ Entities are fully independent, instantiable, reusable, serializable, and composable.
43
+
44
+ ---
45
+
46
+ # **4. Workflow Specification**
47
+
48
+ ## **4.1 Workflow Type**
49
+
50
+ ```ts
51
+ interface WorkflowConfig {
52
+ name?: string;
53
+ enableReflection?: boolean;
54
+ }
55
+ ```
56
+
57
+ ## **4.2 Workflow Instance**
58
+
59
+ ```ts
60
+ class Workflow {
61
+ constructor(config: WorkflowConfig, executor: (ctx: WorkflowContext) => Promise<any>);
62
+ run(): Promise<WorkflowResult>;
63
+ }
64
+ ```
65
+
66
+ * Workflows can be nested dynamically.
67
+ * A workflow always reports into the nearest ancestor workflow’s event tree.
68
+ * A workflow has a root node automatically mounted into the hierarchy.
69
+
70
+ ## **4.3 Workflow Context**
71
+
72
+ ```ts
73
+ interface WorkflowContext {
74
+ workflowId: string;
75
+ parentWorkflowId?: string;
76
+ step(name: string, fn: () => Promise<any>): Promise<any>;
77
+ spawnWorkflow(wf: Workflow): Promise<any>;
78
+ eventTree: EventTreeHandle;
79
+ reflection: ReflectionAPI;
80
+ }
81
+ ```
82
+
83
+ ### Requirements:
84
+
85
+ * `step()` may be called anywhere inside arbitrary JavaScript logic (loops, conditionals, async flows).
86
+ * `spawnWorkflow()` inserts a child workflow under the current workflow in the hierarchy.
87
+ * All events produced under a `step` are automatically nested.
88
+
89
+ ## **4.4 Reflection (Workflow Level)**
90
+
91
+ If enabled:
92
+
93
+ * Any step failure triggers a reflection pass.
94
+ * A reflection prompt is automatically created unless overridden by the user.
95
+ * Workflow-level reflection may be disabled or enabled globally.
96
+
97
+ ---
98
+
99
+ # **5. Agent Specification**
100
+
101
+ Agents are lightweight wrappers around Anthropic’s Agent SDK.
102
+ All Agent configuration properties correspond directly to Anthropic SDK properties.
103
+
104
+ ## **5.1 Agent Type**
105
+
106
+ ```ts
107
+ interface AgentConfig {
108
+ name?: string;
109
+
110
+ // Anthropic SDK properties
111
+ system?: string;
112
+ tools?: Tool[];
113
+ mcps?: MCPServer[];
114
+ skills?: Skill[];
115
+ hooks?: AgentHooks; // lifecycle hooks FROM the SDK, not custom
116
+ env?: Record<string, string>;
117
+
118
+ enableReflection?: boolean;
119
+ enableCache?: boolean;
120
+ }
121
+ ```
122
+
123
+ ### Rules:
124
+
125
+ * Every property in AgentConfig maps 1:1 to Anthropic SDK.
126
+ * No custom hook types are invented.
127
+ * Tools, MCPs, Skills, Hooks are passed through unchanged.
128
+ * All environment variables must be passed through untouched.
129
+
130
+ ## **5.2 Agent Instance**
131
+
132
+ ```ts
133
+ class Agent {
134
+ constructor(config: AgentConfig);
135
+
136
+ prompt<T>(prompt: Prompt<T>, overrides?: PromptOverrides): Promise<T>;
137
+ reflect<T>(prompt: Prompt<T>, overrides?: PromptOverrides): Promise<T>;
138
+ }
139
+ ```
140
+
141
+ ## **5.3 Prompt Overrides**
142
+
143
+ ```ts
144
+ interface PromptOverrides {
145
+ system?: string;
146
+ tools?: Tool[];
147
+ mcps?: MCPServer[];
148
+ skills?: Skill[];
149
+ hooks?: AgentHooks;
150
+ env?: Record<string, string>;
151
+
152
+ temperature?: number;
153
+ maxTokens?: number;
154
+ stop?: string[];
155
+ disableCache?: boolean;
156
+
157
+ enableReflection?: boolean;
158
+ }
159
+ ```
160
+
161
+ ### Rules:
162
+
163
+ * Prompt overrides take precedence over Agent config.
164
+ * Anything that exists in AgentConfig can be overridden at the prompt level.
165
+
166
+ ---
167
+
168
+ # **6. Prompt Specification**
169
+
170
+ Prompts define the content and expected output shape.
171
+
172
+ ## **6.1 Prompt Type**
173
+
174
+ ```ts
175
+ class Prompt<T> {
176
+ readonly user: string; // user messages
177
+ readonly data: Record<string, any>; // structured data injected
178
+ readonly responseFormat: ZodSchema<T>;
179
+
180
+ readonly systemOverride?: string;
181
+ readonly toolsOverride?: Tool[];
182
+ readonly mcpsOverride?: MCPServer[];
183
+ readonly skillsOverride?: Skill[];
184
+ readonly hooksOverride?: AgentHooks;
185
+
186
+ readonly enableReflection?: boolean;
187
+
188
+ constructor(config: PromptConfig<T>);
189
+ }
190
+ ```
191
+
192
+ ## **6.2 PromptConfig**
193
+
194
+ ```ts
195
+ interface PromptConfig<T> {
196
+ user: string;
197
+ data?: Record<string, any>;
198
+ responseFormat: ZodSchema<T>;
199
+
200
+ system?: string;
201
+ tools?: Tool[];
202
+ mcps?: MCPServer[];
203
+ skills?: Skill[];
204
+ hooks?: AgentHooks;
205
+
206
+ enableReflection?: boolean;
207
+ }
208
+ ```
209
+
210
+ ### Rules:
211
+
212
+ * Prompts are immutable.
213
+ * Prompts may override any Agent-level value that corresponds to Anthropic SDK fields.
214
+ * Reflection can be enabled specifically for a single Prompt.
215
+
216
+ ---
217
+
218
+ # **7. Hierarchy Specification**
219
+
220
+ ## **7.1 Automatic Mounting**
221
+
222
+ Whenever:
223
+
224
+ * a Workflow runs another Workflow
225
+ * an Agent executes a Prompt
226
+ * a Prompt triggers tools or MCP events
227
+ * a reflection flow is produced
228
+ * a dynamic subflow is created by an agent
229
+
230
+ The system must automatically:
231
+
232
+ 1. Identify the current parent node.
233
+ 2. Create a new child node.
234
+ 3. Attach it in the event tree.
235
+ 4. Propagate the lineage upward.
236
+
237
+ Zero user plumbing is allowed.
238
+ The hierarchy must behave like a React tree: mounting determines placement.
239
+
240
+ ---
241
+
242
+ # **8. Event System**
243
+
244
+ ## **8.1 Requirements**
245
+
246
+ * Every event from Anthropic SDK tools, MCP streams, agent streams, prompts, and workflow steps must be captured.
247
+ * The entire event tree must be assembled automatically.
248
+ * The tree must be directly queryable as a complete object.
249
+
250
+ ## **8.2 Event Structure**
251
+
252
+ ```ts
253
+ interface EventNode {
254
+ id: string;
255
+ type: EventType;
256
+ timestamp: number;
257
+
258
+ name?: string;
259
+ payload?: any;
260
+ metrics?: EventMetrics;
261
+
262
+ parentId?: string;
263
+ children: EventNode[];
264
+ }
265
+ ```
266
+
267
+ ## **8.3 Queryable Event Tree API**
268
+
269
+ ```ts
270
+ interface EventTreeHandle {
271
+ root: EventNode;
272
+ getNode(id: string): EventNode | undefined;
273
+ getChildren(id: string): EventNode[];
274
+ getAncestors(id: string): EventNode[];
275
+ toJSON(): EventNode;
276
+ }
277
+ ```
278
+
279
+ ---
280
+
281
+ # **9. Caching Specification**
282
+
283
+ ## **9.1 Cache Key**
284
+
285
+ SHA-256 of deterministic JSON encoding of:
286
+
287
+ ```
288
+ user message
289
+ data
290
+ system value
291
+ model settings
292
+ temperature
293
+ tools
294
+ mcps
295
+ skills
296
+ schema version/hash
297
+ ```
298
+
299
+ ## **9.2 Cache API**
300
+
301
+ ```ts
302
+ interface Cache {
303
+ get(key: string): Promise<any | undefined>;
304
+ set(key: string, value: any): Promise<void>;
305
+ bust(key: string): Promise<void>;
306
+ bustPrefix(prefix: string): Promise<void>;
307
+ }
308
+ ```
309
+
310
+ Default: in-memory LRU.
311
+
312
+ ---
313
+
314
+ # **10. Dynamic Behavior Requirements**
315
+
316
+ ## **10.1 Dynamic Workflow/Agent/Prompt Creation**
317
+
318
+ Agents must be able to:
319
+
320
+ * create new Workflows
321
+ * create new Agents
322
+ * create new Prompts
323
+ * spawn them
324
+ * attach them into the hierarchy automatically
325
+
326
+ No additional user code besides calling the workflow/agent/prompt constructors.
327
+
328
+ ## **10.2 Dynamic Context Revision**
329
+
330
+ Users must be able to:
331
+
332
+ * send a yes/no or clarifying prompt
333
+ * inspect the result
334
+ * replace the previous prompt with a new prompt
335
+ * continue the chain without forking the tree incorrectly
336
+
337
+ ---
338
+
339
+ # **11. Agent Introspection & Control Tools**
340
+
341
+ A standard set of tools must be provided to agents:
342
+
343
+ * inspect current workflow node
344
+ * read full ancestor chain
345
+ * list sibling or child nodes
346
+ * inspect prior outputs
347
+ * inspect cache status
348
+ * request workflow mount points
349
+ * request spawning of new workflows or agents
350
+
351
+ These are standard tools implemented using Anthropic SDK conventions.
352
+
353
+ ---
354
+
355
+ # **12. Examples Directory**
356
+
357
+ A complete examples directory must include:
358
+
359
+ 1. Basic workflow with steps
360
+ 2. Workflow calling Agents with different prompts
361
+ 3. A single Agent used across many workflows
362
+ 4. Workflow inside a workflow
363
+ 5. Agent spawning a new Workflow dynamically
364
+ 6. Dynamic prompt replacement based on output conditions
365
+ 7. Loops calling multiple agents repeatedly with full observability
366
+ 8. Tools, MCPs, hooks, skills usage examples
367
+ 9. Reflection at each level (Workflow, Agent, Prompt)
368
+ 10. Introspection tools demo
369
+
370
+ All examples must be minimal, isolated, and canonical.
371
+
372
+ ---
373
+
374
+ # **13. Completion Criteria (MVP)**
375
+
376
+ The MVP is complete when:
377
+
378
+ 1. The hierarchy system mounts everything automatically.
379
+ 2. Workflows, Agents, and Prompts are fully composable in JS control flow.
380
+ 3. All Anthropic SDK fields pass through cleanly.
381
+ 4. Event tree is complete and queryable.
382
+ 5. Dynamic workflows are attachable anywhere.
383
+ 6. Reflection works at all three levels.
384
+ 7. Cache works with overrides and schema hashing.
385
+ 8. Introspection tools work.
386
+ 9. Examples directory is complete.
387
+
388
+ ---
389
+
390
+ # **End of PRD**