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,116 @@
1
+ # LRU Cache Research Summary
2
+
3
+ ## Recommended Stack
4
+
5
+ ```json
6
+ {
7
+ "dependencies": {
8
+ "lru-cache": "^10.0.0"
9
+ }
10
+ }
11
+ ```
12
+
13
+ ## Key Patterns
14
+
15
+ ### 1. Deterministic Cache Key Generation
16
+
17
+ ```typescript
18
+ import { createHash } from 'node:crypto';
19
+
20
+ function deterministicStringify(value: unknown): string {
21
+ const seen = new WeakSet<object>();
22
+
23
+ function stringify(val: unknown): string {
24
+ if (val === null) return 'null';
25
+ if (typeof val === 'string') return JSON.stringify(val);
26
+ if (typeof val === 'number' || typeof val === 'boolean') return String(val);
27
+ if (typeof val === 'undefined') return 'undefined';
28
+
29
+ if (typeof val === 'object') {
30
+ if (seen.has(val as object)) {
31
+ throw new TypeError('Converting circular structure to JSON');
32
+ }
33
+ seen.add(val as object);
34
+
35
+ let result: string;
36
+ if (Array.isArray(val)) {
37
+ result = '[' + val.map(stringify).join(',') + ']';
38
+ } else {
39
+ const keys = Object.keys(val as Record<string, unknown>).sort();
40
+ const pairs = keys.map(k =>
41
+ JSON.stringify(k) + ':' + stringify((val as Record<string, unknown>)[k])
42
+ );
43
+ result = '{' + pairs.join(',') + '}';
44
+ }
45
+
46
+ seen.delete(val as object);
47
+ return result;
48
+ }
49
+
50
+ return String(val);
51
+ }
52
+
53
+ return stringify(value);
54
+ }
55
+
56
+ function generateCacheKey(data: unknown): string {
57
+ const hash = createHash('sha256');
58
+ const serialized = deterministicStringify(data);
59
+ hash.update(serialized, 'utf8');
60
+ return hash.digest('hex');
61
+ }
62
+ ```
63
+
64
+ ### 2. LRU Cache Implementation with lru-cache
65
+
66
+ ```typescript
67
+ import LRU from 'lru-cache';
68
+
69
+ const cache = new LRU<string, CacheValue>({
70
+ max: 1000, // Maximum number of items
71
+ maxSize: 52428800, // 50MB memory limit
72
+ sizeCalculation: (entry, key) => {
73
+ return JSON.stringify(entry).length;
74
+ },
75
+ ttl: 1000 * 60 * 60, // 1 hour default TTL
76
+ updateAgeOnGet: true,
77
+ allowStale: false
78
+ });
79
+ ```
80
+
81
+ ### 3. Cache Interface (PRD Section 9.2)
82
+
83
+ ```typescript
84
+ interface Cache {
85
+ get(key: string): Promise<any | undefined>;
86
+ set(key: string, value: any): Promise<void>;
87
+ bust(key: string): Promise<void>;
88
+ bustPrefix(prefix: string): Promise<void>;
89
+ }
90
+ ```
91
+
92
+ ### 4. Cache Key Fields (PRD Section 9.1)
93
+
94
+ SHA-256 of deterministic JSON encoding of:
95
+ - user message
96
+ - data
97
+ - system value
98
+ - model settings
99
+ - temperature
100
+ - tools (names sorted)
101
+ - mcps (names sorted)
102
+ - skills (names sorted)
103
+ - schema version/hash (from Zod responseFormat._def)
104
+
105
+ ## Gotchas
106
+
107
+ 1. **Non-deterministic JSON.stringify** - Must sort keys!
108
+ 2. **Memory exhaustion** - Set both `max` and `maxSize`
109
+ 3. **Stale data** - Include model version in cache key
110
+ 4. **Circular references** - Handle with WeakSet tracking
111
+
112
+ ## Sources
113
+
114
+ - https://www.npmjs.com/package/lru-cache
115
+ - https://nodejs.org/api/crypto.html
116
+ - https://www.npmjs.com/package/json-stringify-deterministic
@@ -0,0 +1,177 @@
1
+ # Agent Introspection Tools Research
2
+
3
+ ## Overview (PRD Section 11)
4
+
5
+ Standard tools that allow agents to inspect and manipulate their position in the workflow hierarchy.
6
+
7
+ ## Tool Definitions (Anthropic Tool Format)
8
+
9
+ ### 1. inspect_current_node
10
+
11
+ ```typescript
12
+ const inspectCurrentNode: Tool = {
13
+ name: 'inspect_current_node',
14
+ description: 'Get information about the current workflow node including ID, name, status, and parent reference',
15
+ input_schema: {
16
+ type: 'object',
17
+ properties: {},
18
+ required: []
19
+ }
20
+ };
21
+
22
+ // Handler returns:
23
+ interface CurrentNodeInfo {
24
+ id: string;
25
+ name: string;
26
+ status: WorkflowStatus;
27
+ parentId?: string;
28
+ parentName?: string;
29
+ childCount: number;
30
+ depth: number;
31
+ }
32
+ ```
33
+
34
+ ### 2. read_ancestor_chain
35
+
36
+ ```typescript
37
+ const readAncestorChain: Tool = {
38
+ name: 'read_ancestor_chain',
39
+ description: 'Get all ancestor nodes from current position up to the root workflow',
40
+ input_schema: {
41
+ type: 'object',
42
+ properties: {
43
+ maxDepth: {
44
+ type: 'number',
45
+ description: 'Maximum ancestors to return (default: all)'
46
+ }
47
+ },
48
+ required: []
49
+ }
50
+ };
51
+
52
+ // Handler returns:
53
+ interface AncestorInfo {
54
+ ancestors: Array<{
55
+ id: string;
56
+ name: string;
57
+ status: WorkflowStatus;
58
+ depth: number;
59
+ }>;
60
+ totalDepth: number;
61
+ }
62
+ ```
63
+
64
+ ### 3. list_siblings_children
65
+
66
+ ```typescript
67
+ const listSiblingsChildren: Tool = {
68
+ name: 'list_siblings_children',
69
+ description: 'List sibling or child nodes relative to current position',
70
+ input_schema: {
71
+ type: 'object',
72
+ properties: {
73
+ type: {
74
+ type: 'string',
75
+ enum: ['siblings', 'children'],
76
+ description: 'Type of nodes to list'
77
+ }
78
+ },
79
+ required: ['type']
80
+ }
81
+ };
82
+ ```
83
+
84
+ ### 4. inspect_prior_outputs
85
+
86
+ ```typescript
87
+ const inspectPriorOutputs: Tool = {
88
+ name: 'inspect_prior_outputs',
89
+ description: 'Get outputs from prior steps or prompt executions',
90
+ input_schema: {
91
+ type: 'object',
92
+ properties: {
93
+ nodeId: {
94
+ type: 'string',
95
+ description: 'Specific node ID to inspect (default: most recent)'
96
+ },
97
+ count: {
98
+ type: 'number',
99
+ description: 'Number of prior outputs to return (default: 1)'
100
+ }
101
+ },
102
+ required: []
103
+ }
104
+ };
105
+ ```
106
+
107
+ ### 5. inspect_cache_status
108
+
109
+ ```typescript
110
+ const inspectCacheStatus: Tool = {
111
+ name: 'inspect_cache_status',
112
+ description: 'Check if a prompt response is cached',
113
+ input_schema: {
114
+ type: 'object',
115
+ properties: {
116
+ promptHash: {
117
+ type: 'string',
118
+ description: 'Hash of the prompt to check'
119
+ }
120
+ },
121
+ required: ['promptHash']
122
+ }
123
+ };
124
+ ```
125
+
126
+ ### 6. request_spawn_workflow
127
+
128
+ ```typescript
129
+ const requestSpawnWorkflow: Tool = {
130
+ name: 'request_spawn_workflow',
131
+ description: 'Request to spawn a new child workflow dynamically',
132
+ input_schema: {
133
+ type: 'object',
134
+ properties: {
135
+ name: {
136
+ type: 'string',
137
+ description: 'Name for the new workflow'
138
+ },
139
+ description: {
140
+ type: 'string',
141
+ description: 'Description of what the workflow should do'
142
+ }
143
+ },
144
+ required: ['name', 'description']
145
+ }
146
+ };
147
+ ```
148
+
149
+ ## Security Considerations
150
+
151
+ 1. **Read-only by default**: Most introspection tools should be read-only
152
+ 2. **Sanitize sensitive data**: Remove API keys, credentials from output
153
+ 3. **Limit spawning**: request_spawn_workflow should have rate limits
154
+ 4. **Depth limits**: Prevent infinite recursion in hierarchy traversal
155
+
156
+ ## Tool Bundle Export
157
+
158
+ ```typescript
159
+ export const INTROSPECTION_TOOLS: Tool[] = [
160
+ inspectCurrentNode,
161
+ readAncestorChain,
162
+ listSiblingsChildren,
163
+ inspectPriorOutputs,
164
+ inspectCacheStatus,
165
+ requestSpawnWorkflow
166
+ ];
167
+ ```
168
+
169
+ ## Usage Example
170
+
171
+ ```typescript
172
+ const agent = new Agent({
173
+ name: 'IntrospectiveAgent',
174
+ tools: [...INTROSPECTION_TOOLS, ...customTools],
175
+ system: 'You can inspect your position in the workflow hierarchy using the introspection tools.'
176
+ });
177
+ ```
@@ -0,0 +1,117 @@
1
+ # Reflection Patterns Research
2
+
3
+ ## What is Reflection in AI Agent Contexts
4
+
5
+ Reflection is a self-correction mechanism where an AI agent:
6
+ 1. Evaluates its own output
7
+ 2. Identifies errors or improvements
8
+ 3. Revises its approach based on feedback
9
+
10
+ ## Levels of Reflection (PRD Section 4.4)
11
+
12
+ ### 1. Workflow-Level Reflection
13
+ - Triggered when a step fails
14
+ - Has access to entire workflow state
15
+ - Can retry failed steps with modified approach
16
+
17
+ ### 2. Agent-Level Reflection
18
+ - Triggered via Agent.reflect() method
19
+ - Adds reflection system prompt prefix
20
+ - Reviews reasoning before final answer
21
+
22
+ ### 3. Prompt-Level Reflection
23
+ - Enabled per-prompt via enableReflection flag
24
+ - Validates response against schema
25
+ - Retries with error context if validation fails
26
+
27
+ ## Implementation Patterns
28
+
29
+ ### Reflection Prompt Template
30
+
31
+ ```typescript
32
+ const REFLECTION_PROMPT = `Before answering, reflect on your reasoning step by step:
33
+ 1. What is the core problem/question?
34
+ 2. What approaches could solve this?
35
+ 3. What are potential errors or edge cases?
36
+ 4. Review your answer for accuracy.
37
+
38
+ Then provide your final answer.`;
39
+ ```
40
+
41
+ ### Auto-Retry with Reflection
42
+
43
+ ```typescript
44
+ async function executeWithReflection<T>(
45
+ agent: Agent,
46
+ prompt: Prompt<T>,
47
+ maxAttempts: number = 3
48
+ ): Promise<T> {
49
+ let lastError: Error | null = null;
50
+
51
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
52
+ try {
53
+ const result = await agent.prompt(prompt);
54
+ return result;
55
+ } catch (error) {
56
+ lastError = error as Error;
57
+
58
+ if (attempt < maxAttempts) {
59
+ // Create reflection prompt with error context
60
+ const reflectionPrompt = prompt.withData({
61
+ previousError: lastError.message,
62
+ attempt: attempt,
63
+ instruction: 'Reflect on the previous error and try again.'
64
+ });
65
+
66
+ // Emit reflection event
67
+ emitEvent({
68
+ type: 'reflectionStart',
69
+ level: 'prompt',
70
+ node: currentNode
71
+ });
72
+ }
73
+ }
74
+ }
75
+
76
+ throw lastError;
77
+ }
78
+ ```
79
+
80
+ ### ReflectionAPI Interface (PRD Section 4.3)
81
+
82
+ ```typescript
83
+ interface ReflectionAPI {
84
+ isEnabled(): boolean;
85
+ triggerReflection(reason?: string): Promise<void>;
86
+ getReflectionHistory(): ReflectionEntry[];
87
+ }
88
+
89
+ interface ReflectionEntry {
90
+ timestamp: number;
91
+ level: 'workflow' | 'agent' | 'prompt';
92
+ reason: string;
93
+ originalError?: Error;
94
+ resolution: 'retry' | 'skip' | 'abort';
95
+ success: boolean;
96
+ }
97
+ ```
98
+
99
+ ## Best Practices
100
+
101
+ 1. **Maximum reflection attempts**: 2-3 attempts before failure
102
+ 2. **When NOT to reflect**:
103
+ - Rate limit errors (wait and retry instead)
104
+ - Authentication errors (cannot self-fix)
105
+ - Quota exceeded (needs external action)
106
+ 3. **Performance considerations**:
107
+ - Each reflection doubles token usage
108
+ - Cache reflection results when possible
109
+ 4. **State capture**: Always snapshot state before reflection for debugging
110
+
111
+ ## Integration with Workflow Events
112
+
113
+ ```typescript
114
+ // Events to emit during reflection
115
+ { type: 'reflectionStart'; level: 'workflow' | 'agent' | 'prompt'; node: WorkflowNode }
116
+ { type: 'reflectionEnd'; level: 'workflow' | 'agent' | 'prompt'; success: boolean; node: WorkflowNode }
117
+ ```