Skip to content

Commit 34da00d

Browse files
committed
Merge branch 'feature/faster-imports'
2 parents 1aa9f97 + 425e6a8 commit 34da00d

21 files changed

+1063
-27
lines changed

AGENTIC.md

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
# Agentic Development: Multi-Agent Coordination for Complex Software Engineering
2+
3+
## Overview
4+
5+
This document describes a proven methodology for coordinating multiple AI agents to solve complex software engineering problems through focused, iterative collaboration. The approach emphasizes empirical validation, controlled scope, and systematic knowledge transfer between specialized agents.
6+
7+
## Core Principles
8+
9+
### 1. **Focused Agent Specialization**
10+
Each agent receives a narrow, well-defined scope with minimal context to prevent:
11+
- Scope creep beyond intended functionality
12+
- Over-engineering of solutions
13+
- Analysis paralysis from too much information
14+
- Conflicting approaches within single implementations
15+
16+
**Example**: Schema Design Agent receives only requirements and foundation context, not implementation details or testing frameworks.
17+
18+
### 2. **Empirical Validation Over Theory**
19+
Every optimization or change must be validated with real data and measurements before acceptance:
20+
- Theoretical calculations can be completely wrong (our case: predicted 52% reduction, got 1,342% increase)
21+
- Real-world testing reveals hidden overhead and complexity
22+
- Performance improvements must be demonstrated, not assumed
23+
- Failed approaches provide valuable learning for future iterations
24+
25+
### 3. **Structured Knowledge Transfer**
26+
Information flows between agents through standardized handoff files rather than shared memory:
27+
- Prevents information overload
28+
- Creates clear dependency chains
29+
- Enables quality gates between phases
30+
- Allows stopping/redirecting at any decision point
31+
32+
## Architecture
33+
34+
### Phase-Based Execution Model
35+
36+
```
37+
Phase 1: Foundation
38+
├── Agent 1A: Schema Design → tmp/agent_handoff_schema.md
39+
├── Agent 1B: Recovery Logic → tmp/agent_handoff_recovery.md
40+
└── Agent 1C: Verification → tmp/verification_foundation.md
41+
42+
Phase 2: Pilot Implementation
43+
├── Agent 2A: Step Analysis → tmp/agent_handoff_step_analysis.md
44+
├── Agent 2B: Conversion → tmp/agent_handoff_converted_procedure.md
45+
├── Agent 2C: Testing → tmp/agent_handoff_pilot_test.md
46+
└── Agent 2D: Verification → tmp/verification_pilot.md
47+
48+
Phase 3: Production Deployment
49+
├── Agent 3A: Migration Creation
50+
├── Agent 3B: Test Framework
51+
└── Agent 3C: Final Review
52+
```
53+
54+
### Context Management System
55+
56+
#### **Global Context Files**
57+
- `tmp/implementation_status.md` - Overall progress and decisions
58+
- `tmp/current_migration_context.md` - Technical requirements and constraints
59+
- `tmp/verification_checklist.md` - Quality gates and success criteria
60+
61+
#### **Agent Handoff Files**
62+
- **Input Context**: Previous agent outputs + focused technical requirements
63+
- **Output Specification**: Structured deliverables for next agent
64+
- **Scope Boundaries**: Clear limitations on what agent should/shouldn't do
65+
- **Success Criteria**: Measurable outcomes required for phase completion
66+
67+
#### **Verification Files**
68+
- **Quality Assessment**: Pass/fail evaluation of completed work
69+
- **Issue Identification**: Specific problems with remediation paths
70+
- **Integration Analysis**: Compatibility with existing systems
71+
- **Decision Framework**: Go/no-go recommendations with evidence
72+
73+
## Implementation Methodology
74+
75+
### Agent Task Design
76+
77+
#### **Information Scoping Strategy**
78+
```markdown
79+
AGENT TASK TEMPLATE:
80+
81+
**Mission**: Single focused objective
82+
**Context**: Read only relevant tmp/handoff files
83+
**Focused Task**: 3-4 specific deliverables
84+
**Technical Constraints**: Hard boundaries and limitations
85+
**Output**: Structured deliverable in tmp/agent_handoff_*.md
86+
**Success Criteria**: Measurable outcomes
87+
**Scope Boundaries**: What NOT to do
88+
```
89+
90+
#### **Quality Control Mechanisms**
91+
92+
1. **Verification Agents**: Dedicated agents that only assess quality, don't create
93+
2. **Empirical Testing**: All performance claims validated with real data
94+
3. **Integration Gates**: Compatibility verification at each phase boundary
95+
4. **Rollback Capability**: Clear path to previous working state
96+
97+
### Context Management Patterns
98+
99+
#### **Selective Context Access**
100+
Agents access broader context through search rather than full consumption:
101+
```
102+
Agent Receives:
103+
├── Direct Handoff: tmp/agent_handoff_previous.md (always read)
104+
├── Global Context: tmp/implementation_status.md (always read)
105+
├── Focused Context: tmp/current_migration_context.md (always read)
106+
└── Search-Based Access: Query broader context when specific info needed
107+
```
108+
109+
**Search Patterns Used**:
110+
- **Technical Details**: "Find batch size settings" → discovers analysis_batch_size = 32768
111+
- **Implementation Patterns**: "Find UPDATE operations" → locates specific SQL patterns
112+
- **Integration Points**: "Find worker scheduling" → identifies admin.import_job_* functions
113+
- **Error Handling**: "Find error propagation" → discovers existing error patterns
114+
115+
#### **Context Handoff Patterns**
116+
117+
**Sequential Handoff**: Agent B reads Agent A's output file and builds upon it:
118+
```
119+
Agent 1A: Schema Design
120+
↓ (tmp/agent_handoff_schema.md)
121+
Agent 1B: Recovery Logic (reads schema + searches for recovery patterns)
122+
↓ (tmp/agent_handoff_recovery.md)
123+
Agent 1C: Verification (reads both + searches for integration requirements)
124+
```
125+
126+
**Parallel Execution**: Multiple agents work independently but can search shared context:
127+
```
128+
Agent 2A: Analysis ──→ tmp/agent_handoff_step_analysis.md
129+
↓ (searches: "current procedure implementations")
130+
Agent 2B: Conversion ←─ (searches: "existing API patterns")
131+
Agent 2C: Testing ←── (searches: "current job data for testing")
132+
133+
Agent 2D: Verification (reads all outputs + searches for production patterns)
134+
```
135+
136+
**Convergence Points**: Verification agents integrate multiple streams:
137+
```
138+
Foundation Components → Verification Agent → Go/No-Go Decision
139+
Implementation Assets → Verification Agent → Production Readiness
140+
Test Results + Code → Verification Agent → Deployment Recommendation
141+
```
142+
143+
#### **Strategic Context Oversight**
144+
Dedicated coordination agent periodically reviews entire accumulated context:
145+
```
146+
Context Oversight Agent:
147+
├── Reviews: All tmp/agent_handoff_*.md files
148+
├── Reviews: All tmp/verification_*.md files
149+
├── Reviews: tmp/implementation_status.md timeline
150+
├── Searches: Codebase for consistency with agent outputs
151+
└── Outputs: tmp/strategic_context_review.md with:
152+
├── Consistency Assessment
153+
├── Gap Identification
154+
├── Strategic Recommendations
155+
└── Course Corrections
156+
```
157+
158+
**Oversight Triggers**:
159+
- After each phase completion
160+
- When contradictions detected between agent outputs
161+
- When performance targets not being met
162+
- When scope expansion beyond original goals detected
163+
164+
## Benefits Demonstrated
165+
166+
### **Prevented Production Disasters**
167+
- Theoretical optimization showed 52% improvement in analysis
168+
- Empirical testing revealed 1,342% performance degradation
169+
- Verification agent provided definitive no-go recommendation
170+
- Saved organization from catastrophic production deployment
171+
172+
### **Maintained Solution Quality**
173+
- Each agent delivered focused, high-quality output within scope
174+
- No single agent became overwhelmed by entire problem complexity
175+
- Clear decision points prevented continued investment in failed approaches
176+
- Reusable components (testing framework, schema designs) created for future iterations
177+
178+
### **Enabled Rapid Pivoting**
179+
- Clear phase boundaries allowed stopping after pilot failure
180+
- Knowledge accumulated in structured format enabled alternative approaches
181+
- Testing infrastructure reusable for validating different optimization strategies
182+
- Lessons learned documented for future optimization attempts
183+
184+
## Success Factors
185+
186+
### **Controlled Information Flow**
187+
- Agents receive minimum viable context for their specific task
188+
- Prevents analysis paralysis and scope creep
189+
- Enables focused problem-solving within defined boundaries
190+
- Reduces cognitive load on individual agents
191+
192+
### **Selective Context Access**
193+
- Agents can search and access broader context when needed, but don't read everything
194+
- Each agent identifies and reads only relevant portions of accumulated knowledge
195+
- Search-based context retrieval prevents information overload while maintaining access to necessary details
196+
- Agents cite specific sources (file names, line numbers) when referencing broader context
197+
198+
**Benefits Demonstrated**:
199+
- Agent 2A found actual batch sizes (32,768) by searching codebase, correcting Agent 1B's assumptions (1,000)
200+
- Agent 2B located exact UPDATE patterns by searching migration files, enabling precise optimization
201+
- Agent 2C found real job data (3,924 processing rows) for empirical testing rather than creating synthetic data
202+
- Verification agents could cross-reference claims against actual codebase implementation
203+
204+
### **Empirical Validation Requirement**
205+
- All performance claims must be measured with real data
206+
- Theoretical calculations validated against actual system behavior
207+
- Failed optimizations caught before production deployment
208+
- Testing infrastructure becomes reusable asset
209+
210+
### **Structured Decision Points**
211+
- Clear go/no-go criteria at each phase
212+
- Evidence-based recommendations from verification agents
213+
- Ability to stop/redirect without losing accumulated work
214+
- Quality gates prevent poor decisions from propagating
215+
216+
### **Modular Architecture**
217+
- Each phase builds on previous phase outputs
218+
- Components can be reused across different approaches
219+
- Failed implementations don't invalidate entire framework
220+
- Knowledge accumulates in structured, transferrable format
221+
222+
### **Strategic Context Oversight**
223+
- Dedicated coordination agent periodically reviews entire context for consistency
224+
- Identifies contradictions, gaps, or misalignments across agent outputs
225+
- Ensures global coherence while maintaining individual agent focus
226+
- Provides strategic redirection when accumulated knowledge suggests better approaches
227+
228+
## Anti-Patterns Avoided
229+
230+
### **Single Agent Overwhelm**
231+
- ❌ One agent trying to solve entire complex problem
232+
- ✅ Multiple specialized agents with focused scopes
233+
234+
### **Theoretical Optimization**
235+
- ❌ Assuming performance improvements based on calculations
236+
- ✅ Requiring empirical validation with real data
237+
238+
### **Scope Creep**
239+
- ❌ Agents expanding beyond defined responsibilities
240+
- ✅ Clear boundaries and handoff specifications
241+
242+
### **Integration Surprises**
243+
- ❌ Discovering incompatibilities at final deployment
244+
- ✅ Verification agents checking integration at each phase
245+
246+
### **Context Information Overload**
247+
- ❌ Agents reading all accumulated context and getting overwhelmed
248+
- ✅ Selective context access through search-based retrieval
249+
250+
### **Context Inconsistency**
251+
- ❌ Agent outputs contradicting each other without detection
252+
- ✅ Strategic oversight agent ensuring global coherence
253+
254+
## Lessons Learned
255+
256+
### **Simplicity Often Wins**
257+
Complex optimizations (UNLOGGED tables, additional tracking) can create more overhead than they eliminate. Simple approaches (batch size increases, existing hot-patches) often provide better results with lower risk.
258+
259+
### **Testing Infrastructure is Valuable**
260+
Even failed optimizations can produce valuable testing frameworks and measurement tools that enable future successful optimization attempts.
261+
262+
### **Agent Coordination Scales**
263+
The methodology successfully coordinated 8 specialized agents across 3 phases, with clear deliverables and decision points. This demonstrates scalability to larger, more complex problems.
264+
265+
### **Early Failure is Success**
266+
Catching a failed optimization in pilot phase (rather than production) represents successful risk management and validates the methodology's effectiveness.
267+
268+
## Applicability
269+
270+
This methodology applies to complex software engineering problems that:
271+
- Require multiple technical disciplines (schema design, performance optimization, testing, deployment)
272+
- Have high consequences for failure (production systems, performance-critical applications)
273+
- Benefit from iterative development with validation gates
274+
- Need systematic knowledge transfer between solution phases
275+
- Require empirical validation of theoretical improvements
276+
277+
The approach provides a structured framework for agent coordination that maintains solution quality while preventing common pitfalls of AI-assisted development.

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"ts-node": "^10.9.2",
100100
"typescript": "^5.9.3"
101101
},
102-
"packageManager": "pnpm@10.28.0+sha512.05df71d1421f21399e053fde567cea34d446fa02c76571441bfc1c7956e98e363088982d940465fd34480d4d90a0668bc12362f8aa88000a64e83d0b0e47be48",
102+
"packageManager": "pnpm@10.28.1+sha512.7d7dbbca9e99447b7c3bf7a73286afaaf6be99251eb9498baefa7d406892f67b879adb3a1d7e687fc4ccc1a388c7175fbaae567a26ab44d1067b54fcb0d6a316",
103103
"pnpm": {
104104
"onlyBuiltDependencies": [
105105
"sharp",

cli/src/manage.cr

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,18 @@ module Statbus
192192
db_mem_limit : String
193193

194194
# Type-safe derived memory configuration structure
195+
# All memory settings are derived from DB_MEM_LIMIT for consistent scaling.
196+
# See tmp/db-memory-todo.md for tuning rationale.
195197
record DbMemoryEnv,
196198
db_shm_size : String,
197199
db_mem_limit : String,
198200
db_mem_reservation : String,
199201
db_shared_buffers : String,
200202
db_maintenance_work_mem : String,
201203
db_effective_cache_size : String,
202-
db_work_mem : String
204+
db_work_mem : String,
205+
db_temp_buffers : String,
206+
db_wal_buffers : String
203207

204208
# Configuration values that are derived from other settings
205209
record DerivedEnv,
@@ -396,12 +400,28 @@ module Statbus
396400
end
397401

398402
# Calculate derived memory values based on DB_MEM_LIMIT
399-
# See tmp/db-memory-todo.md for rationale.
403+
# All PostgreSQL memory settings scale from this single source of truth.
404+
# See tmp/db-memory-todo.md for detailed rationale.
405+
#
406+
# Memory allocation strategy (for 4GB total):
407+
# shared_buffers: 25% = 1GB (main buffer cache)
408+
# effective_cache_size: 75% = 3GB (planner hint for OS cache)
409+
# maintenance_work_mem: 25% = 1GB (VACUUM, CREATE INDEX, etc.)
410+
# work_mem: 1% = 40MB (per-operation sorts/hashes, multiplied by queries)
411+
# temp_buffers: 12.5% = 512MB (temporary tables, min 256MB for imports)
412+
# wal_buffers: ~1.5% = 64MB (WAL write buffering, min 16MB, max 256MB)
413+
#
400414
mem_limit_mb = parse_mem_size_to_mb(config.db_mem_limit)
401415
shared_buffers_mb = (mem_limit_mb * 0.25).to_i64
402416
maintenance_work_mem_mb = (mem_limit_mb * 0.25).to_i64
403417
effective_cache_size_mb = (mem_limit_mb * 0.75).to_i64
404418
work_mem_mb = Math.max(4_i64, mem_limit_mb // 100) # Min 4MB for safety
419+
# temp_buffers: ~12.5% of memory, min 256MB for import temp tables
420+
# Must be set at server startup (can't be changed after temp tables are accessed)
421+
temp_buffers_mb = Math.max(256_i64, mem_limit_mb // 8)
422+
# wal_buffers: ~1.5% of memory, clamped between 16MB and 256MB
423+
# Larger values reduce disk I/O by buffering more WAL before write
424+
wal_buffers_mb = Math.min(256_i64, Math.max(16_i64, (mem_limit_mb * 0.015).to_i64))
405425
reservation_mb = (mem_limit_mb // 2).to_i64
406426

407427
db_mem = DbMemoryEnv.new(
@@ -411,7 +431,9 @@ module Statbus
411431
db_shared_buffers: format_mb_for_pg(shared_buffers_mb),
412432
db_maintenance_work_mem: format_mb_for_pg(maintenance_work_mem_mb),
413433
db_effective_cache_size: format_mb_for_pg(effective_cache_size_mb),
414-
db_work_mem: format_mb_for_pg(work_mem_mb)
434+
db_work_mem: format_mb_for_pg(work_mem_mb),
435+
db_temp_buffers: format_mb_for_pg(temp_buffers_mb),
436+
db_wal_buffers: format_mb_for_pg(wal_buffers_mb)
415437
)
416438

417439
# Calculate derived values
@@ -606,14 +628,16 @@ module Statbus
606628

607629
# PostgreSQL memory configuration
608630
# These control the docker container resource limits and postgresql.conf settings.
609-
# They are derived from DB_MEM_LIMIT in .env.config.
631+
# All values are derived from DB_MEM_LIMIT in .env.config (single source of truth).
610632
env.set("DB_MEM_LIMIT", db_mem.db_mem_limit)
611633
env.set("DB_SHM_SIZE", db_mem.db_shm_size)
612634
env.set("DB_MEM_RESERVATION", db_mem.db_mem_reservation)
613635
env.set("DB_SHARED_BUFFERS", db_mem.db_shared_buffers)
614636
env.set("DB_MAINTENANCE_WORK_MEM", db_mem.db_maintenance_work_mem)
615637
env.set("DB_EFFECTIVE_CACHE_SIZE", db_mem.db_effective_cache_size)
616638
env.set("DB_WORK_MEM", db_mem.db_work_mem)
639+
env.set("DB_TEMP_BUFFERS", db_mem.db_temp_buffers)
640+
env.set("DB_WAL_BUFFERS", db_mem.db_wal_buffers)
617641

618642
env.set("ACCESS_JWT_EXPIRY", config.access_jwt_expiry)
619643
env.set("REFRESH_JWT_EXPIRY", config.refresh_jwt_expiry)

0 commit comments

Comments
 (0)