-
Notifications
You must be signed in to change notification settings - Fork 59
Cortex Code Generation
geoffrey fernald edited this page Feb 1, 2026
·
1 revision
Cortex V2 provides rich context for AI code generation, ensuring generated code follows established patterns and avoids known anti-patterns.
When generating code, Cortex V2 provides:
- Relevant patterns β How similar code is written
- Tribal knowledge β Team conventions and preferences
- Constraints β Rules that must be followed
- Anti-patterns β What to avoid
- Provenance tracking β What influenced the generation
interface GenerationContext {
target: GenerationTarget;
patterns: PatternContext[];
tribal: TribalContext[];
constraints: ConstraintContext[];
antipatterns: AntipatternContext[];
provenance: ProvenanceTracker;
}Finds relevant patterns for the generation target:
const patterns = await patternGatherer.gather({
targetFile: 'src/api/users.ts',
intent: 'add_feature',
focus: 'user CRUD'
});
// Returns patterns like:
// - REST controller pattern
// - Error handling pattern
// - Validation patternFinds team conventions:
const tribal = await tribalGatherer.gather({
targetFile: 'src/api/users.ts',
categories: ['naming', 'style', 'architecture']
});
// Returns knowledge like:
// - "Use camelCase for function names"
// - "Controllers should be thin"
// - "Always validate input at boundaries"Finds rules that must be followed:
const constraints = await constraintGatherer.gather({
targetFile: 'src/api/users.ts'
});
// Returns constraints like:
// - "All API routes must use authentication middleware"
// - "Database queries must use parameterized queries"Finds what to avoid:
const antipatterns = await antipatternGatherer.gather({
targetFile: 'src/api/users.ts'
});
// Returns anti-patterns like:
// - "Don't use string concatenation for SQL"
// - "Avoid synchronous file operations"Track what memories influenced generated code:
interface CodeProvenance {
generatedAt: Date;
targetFile: string;
influencingMemories: InfluencingMemory[];
confidenceScore: number;
}
interface InfluencingMemory {
memoryId: string;
influence: 'pattern' | 'constraint' | 'tribal' | 'antipattern';
weight: number;
excerpt: string;
}const code = await generator.generate(context);
const withProvenance = provenanceTracker.addComments(code, provenance);
// Result:
// /**
// * @drift-provenance
// * - Pattern: REST controller (mem_abc123)
// * - Constraint: Auth required (mem_def456)
// */
// export async function createUser(req, res) { ... }Validate generated code against patterns:
const validation = await validator.validate(generatedCode, {
targetFile: 'src/api/users.ts',
strictMode: true
});
// Returns:
// {
// valid: false,
// violations: [
// { rule: 'auth-required', message: 'Missing auth middleware' }
// ],
// suggestions: [
// { fix: 'Add @RequireAuth decorator', confidence: 0.9 }
// ]
// }const context = await cortex.getGenerationContext({
targetFile: 'src/api/users.ts',
intent: 'add_feature',
focus: 'create user endpoint',
maxTokens: 3000
});const result = await cortex.validateGenerated(code, {
targetFile: 'src/api/users.ts',
checkPatterns: true,
checkConstraints: true,
checkAntipatterns: true
});// After code is accepted/rejected
await cortex.recordOutcome({
generatedCode: code,
outcome: 'accepted', // or 'modified', 'rejected'
modifications: diff, // if modified
provenance: provenance
});{
"intent": "add_feature",
"focus": "user authentication",
"question": "How do I add a new auth endpoint?"
}{
"code": "export async function createUser(...) { ... }",
"targetFile": "src/api/users.ts",
"kind": "function"
}{
"file": "src/api/users.ts",
"content": "// full file content",
"strictMode": true
}Generated code outcomes feed back into learning:
βββββββββββββββββββ
β Generate Code β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β User Reviews β
β - Accept β
β - Modify β
β - Reject β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Record Outcome β
β - Update conf. β
β - Learn from β
β modificationsβ
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Improve Future β
β Generations β
βββββββββββββββββββ
- Always get context β Don't generate blind
- Check constraints β Validate before presenting
- Track provenance β Know what influenced code
- Record outcomes β Enable learning from results
- Use strict mode β For critical code paths
- Cortex V2 Overview
- Memory Setup Wizard
- Memory CLI
- Universal Memory Types
- Learning System
- Token Efficiency
- Causal Graphs
- Code Generation
- Predictive Retrieval
- Architecture
- Call Graph Analysis
- Impact Analysis
- Security Analysis
- Data Boundaries
- Test Topology
- Coupling Analysis
- Error Handling Analysis
- Wrappers Detection
- Environment Variables
- Constants Analysis
- Styling DNA
- Constraints
- Contracts
- Decision Mining
- Speculative Execution
- Watch Mode
- Trends Analysis
- Projects Management
- Package Context
- Monorepo Support
- Reports & Export
- Dashboard
- 10 Languages
- 21 Frameworks
- 16 ORMs
- 400+ Detectors
- 50+ MCP Tools
- 60+ CLI Commands
- 23 Memory Types