-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path10-code-council.vvm
More file actions
301 lines (237 loc) · 8.47 KB
/
10-code-council.vvm
File metadata and controls
301 lines (237 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# VVM Example 10: Code Council
# A comprehensive code review system with 9 specialized reviewers.
# Each reviewer analyzes code from a different perspective and grades A-F.
# All reviews run in parallel, then synthesize into a unified report.
#
# Demonstrates:
# - Derived agents via .with()
# - Parallel execution via pmap
# - Multi-perspective analysis
# - Synthesis pattern
# Base reviewer - all specialists derive from this
agent base_reviewer(
model="sonnet",
prompt="""You are a senior code reviewer. Analyze ONLY the provided code.
Rules:
- Be specific: cite line numbers
- Suggest fixes: show improved code
- Grade A-F based on your focus area
- Only report findings with confidence >= 80%
Output format:
## Grade: [A-F]
## Summary
[1-2 sentences]
## Findings
### Finding 1: [Title] (Confidence: X%)
**Line:** N
**Issue:** [Description]
**Fix:** [Code suggestion]
"""
)
# === Architecture Reviewers ===
coupling_analyzer = base_reviewer.with(
prompt="""You analyze code for HIGH COHESION and LOW COUPLING.
Detect:
- Content coupling: accessing internals of other modules
- Common coupling: shared global/mutable state
- Control coupling: passing flags that control behavior
- Stamp coupling: passing more data than needed
- Circular dependencies
- Fat interfaces (Interface Segregation violations)
Good: modules with single purpose, minimal dependencies, clear interfaces.
Bad: modules reaching into each other, global state, god objects."""
)
fcis_analyzer = base_reviewer.with(
prompt="""You analyze code for FUNCTIONAL CORE, IMPERATIVE SHELL pattern.
Detect:
- Business logic mixed with I/O (database, network, filesystem)
- Side effects in "pure" functions (logging, time, random)
- Non-determinism in core (Date.now(), Math.random(), UUID)
- Exceptions thrown in business logic (should return Result/Either)
Key question: "Can this function be tested WITHOUT mocks?"
Good: pure functions for logic, thin shell for I/O orchestration.
Bad: async/await deep in business logic, db calls in calculations."""
)
simplicity_analyzer = base_reviewer.with(
prompt="""You analyze code using RICH HICKEY'S SIMPLICITY principles.
Core concepts:
- Simple != Easy (simple means "not intertwined")
- Values over state (immutable data, no in-place mutation)
- Functions over methods (stateless transformations)
- Data over objects (plain records, not actors)
- Explicit over implicit (no hidden dependencies)
Detect COMPLECTED concerns:
- State + Identity tangled together
- What + How mixed (should be declarative)
- What + When mixed (logic + scheduling)
- Hidden dependencies and globals
Output the "Rich Hickey Grade" - would this code make Rich proud?"""
)
# === Quality Reviewers ===
dry_analyzer = base_reviewer.with(
prompt="""You detect DRY (Don't Repeat Yourself) violations.
Detect:
- Identical code blocks (3+ lines repeated)
- Copy-pasted functions with minor variations
- Repeated validation logic
- Duplicated error handling patterns
- Magic numbers/strings repeated inline
Rule of Three: Flag when pattern appears 3+ times, or 2 times if substantial.
Provide:
- Locations of duplication
- Suggested extraction/abstraction
- Estimated lines saved"""
)
fail_fast_analyzer = base_reviewer.with(
prompt="""You detect ERROR HANDLING issues. Philosophy: "Fail fast, fail loud."
Detect:
- Silent error swallowing: empty catch blocks, catch-and-ignore
- Defensive fallbacks hiding bugs: ?? defaultValue everywhere
- Workarounds: HACK/TODO/WORKAROUND comments
- Catch-all handlers: catch(e) { return null }
- Infinite retry without limits
- try? / unwrap_or without logging
Errors should SURFACE immediately, not be hidden.
Workarounds are technical debt - fix the root cause.
Bad: catch (e) { results.push({ success: false }) }
Good: catch (e) { logger.error(e); throw e }"""
)
srp_analyzer = base_reviewer.with(
prompt="""You analyze SINGLE RESPONSIBILITY PRINCIPLE.
"A module should have one, and only one, reason to change."
Detect:
- God classes: doing everything
- Kitchen-sink functions: parse + validate + calculate + save + notify
- Mixed abstraction levels: HTTP handling + business logic + SQL
- Utility dumping grounds: unrelated functions grouped together
Tests:
- Name test: Can you name it with ONE noun? (Not Manager, Handler, Utils)
- Stakeholder test: Who would request changes? Should be ONE answer.
- Description test: Can you describe it without "and"?
Signs: >50 line functions, >10 method classes, many unrelated imports."""
)
type_analyzer = base_reviewer.with(
prompt="""You analyze TYPE STRICTNESS. Goal: make illegal states unrepresentable.
Detect:
- any / interface{} / Any usage
- Missing null checks, implicit nullability
- Loose object types (should be discriminated unions)
- Primitive obsession: string IDs instead of branded types
- Force unwrap: .unwrap(), !, as Type
- Weak error types: string errors instead of typed errors
Good: OrderId type, Result<T, E>, discriminated unions
Bad: any, string for everything, implicit null"""
)
# === Correctness Reviewers ===
logic_reviewer = base_reviewer.with(
prompt="""You review ALGORITHM CORRECTNESS.
Detect:
- Off-by-one errors (< vs <=, starting index)
- Incorrect loop bounds
- Wrong comparison operators
- Missing base cases in recursion
- Incorrect state transitions
- Violated invariants
- Integer overflow potential
- Race conditions in concurrent code
Check: Does the logic actually do what it claims to do?"""
)
edge_case_reviewer = base_reviewer.with(
prompt="""You find UNHANDLED EDGE CASES.
Check for handling of:
- Empty collections: [], {}, ""
- Null/undefined inputs
- Zero and negative numbers
- Very large numbers (overflow)
- Single-element collections
- Duplicate values
- Unicode and special characters
- Whitespace-only strings
- Concurrent access / race conditions
- Resource exhaustion
For each edge case found, suggest a guard or validation."""
)
# === Synthesizer ===
agent synthesizer(
model="opus",
prompt="""You synthesize multiple code reviews into a UNIFIED REPORT.
Your job:
1. Aggregate individual grades into an overall grade
2. Prioritize findings by severity (Critical > Warning > Suggestion)
3. Remove duplicate findings across reviewers
4. Produce an actionable summary
Be concise but comprehensive. The goal is a report someone would actually use."""
)
# === The Code Under Review ===
# This example code has intentional issues for reviewers to find:
# - Silent error swallowing (catch block)
# - Mixed I/O with business logic (FCIS violation)
# - No type safety (any-like behavior)
# - SRP violation (one function does everything)
# - Missing edge case handling (empty orders)
code = """
async function processOrders(orders) {
const results = [];
for (const order of orders) {
try {
const user = await db.users.find(order.userId);
const total = order.items.reduce((sum, item) => sum + item.price, 0);
const tax = total * 0.1;
await db.orders.update(order.id, { total: total + tax, status: 'processed' });
await sendEmail(user.email, 'Order processed', { orderId: order.id });
results.push({ orderId: order.id, success: true });
} catch (e) {
results.push({ orderId: order.id, success: false });
}
}
return results;
}
"""
# === Run All Reviews in Parallel ===
def review(reviewer):
return @reviewer `Review this code:
\`\`\`typescript
{code}
\`\`\`
Analyze from your specialized perspective. Provide grade, summary, and findings.`(code)
reviewers = [
coupling_analyzer,
fcis_analyzer,
simplicity_analyzer,
dry_analyzer,
fail_fast_analyzer,
srp_analyzer,
type_analyzer,
logic_reviewer,
edge_case_reviewer
]
# pmap runs all 9 reviews concurrently
reviews = pmap(reviewers, review)
# === Synthesize Final Report ===
final_review = @synthesizer `Synthesize these 9 specialized code reviews into a unified report:
{reviews}
Output format:
# Code Council Review
## Overall Grade: [A-F]
[Brief overall assessment]
## Critical Issues (must fix)
[Findings that could cause bugs, security issues, or data loss]
## Warnings (should fix)
[Findings that impact maintainability, reliability, or performance]
## Suggestions (nice to have)
[Improvements for code quality and best practices]
## Individual Reviewer Grades
| Reviewer | Grade | Key Finding |
|----------|-------|-------------|
| Coupling | ? | ... |
| FCIS | ? | ... |
| Simplicity | ? | ... |
| DRY | ? | ... |
| Fail-Fast | ? | ... |
| SRP | ? | ... |
| Types | ? | ... |
| Logic | ? | ... |
| Edge Cases | ? | ... |
## Recommended Refactor
[Show improved version of the code addressing the top issues]`(reviews)
export final_review