Files
Profa 2253d05618 feat: flatten project structure — archive v2, promote v3 to root
Remove the nested v3/ subdirectory by moving all active source code,
tests, configs, and assets directly to the project root. Archive v2/
(dead code, 847MB) to v2-archive.tar.gz.

Key changes:
- Move v3/src/ → src/, v3/tests/ → tests/, v3/packages/ → packages/
- Move v3/tsconfig.json, v3/vitest.config.ts to root
- Merge v3/package.json into root package.json (ESM, scripts, deps, exports)
- Merge v3/scripts/ into scripts/, v3/implementation/ into docs/implementation/
- Update all CI workflows to remove "cd v3 &&" and v3/ path prefixes
- Fix all init installer path resolution (3-level-up → 2-level-up)
- Fix hooks to use "node ./dist/cli/bundle.js" instead of bare "aqe"
- Update settings.json ADR/DDD directories to docs/implementation/
- Fix helper scripts (statusline, adr-compliance, ddd-tracker, guidance-hooks)
- Update 58 agent definitions: aqe/v3/ → aqe/ memory namespaces
- Update 15 guidance shards: v3/src/ → src/ path references
- Fix sync interfaces: remove v3/ and v2/ path references
- Fix test path calculations (process.cwd() + '..' no longer needed)
- Fix flaky perf test threshold (500ms → 1000ms for CI environments)
- Fix complexity-analyzer test to match ADR-051 keyword-based eligibility

Build passes, 17,901 tests pass, CLI and MCP bundles verified.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 11:35:41 +00:00

310 lines
8.3 KiB
Markdown

# Code Intelligence Domain
## Bounded Context Overview
**Domain**: Code Intelligence
**Responsibility**: Knowledge Graph construction, semantic search, and impact analysis
**Location**: `src/domains/code-intelligence/`
The Code Intelligence domain builds and maintains a Knowledge Graph of the codebase, enabling O(log n) semantic search via HNSW indexing and accurate change impact analysis.
## Ubiquitous Language
| Term | Definition |
|------|------------|
| **Knowledge Graph** | Graph representation of code relationships |
| **Semantic Search** | Natural language code search |
| **Impact Analysis** | Determining affected code from changes |
| **Dependency Map** | Graph of code dependencies |
| **Index** | Searchable representation of codebase |
| **KG Node** | Entity in the Knowledge Graph |
| **KG Edge** | Relationship between nodes |
## Domain Model
### Aggregates
#### IndexResult (Aggregate Root)
Result of codebase indexing operation.
```typescript
interface IndexResult {
filesIndexed: number;
nodesCreated: number;
edgesCreated: number;
duration: number;
errors: IndexError[];
}
```
#### ImpactAnalysis (Aggregate Root)
Complete change impact assessment.
```typescript
interface ImpactAnalysis {
directImpact: ImpactedFile[];
transitiveImpact: ImpactedFile[];
impactedTests: string[];
riskLevel: Severity;
recommendations: string[];
}
```
### Entities
#### DependencyNode
Node in the dependency graph.
```typescript
interface DependencyNode {
id: string;
path: string;
type: 'module' | 'class' | 'function' | 'file';
inDegree: number;
outDegree: number;
}
```
#### KGNode
Entity in the Knowledge Graph.
```typescript
interface KGNode {
id: string;
label: string;
properties: Record<string, unknown>;
}
```
#### SearchResult
Individual search match.
```typescript
interface SearchResult {
file: string;
line?: number;
snippet: string;
score: number;
highlights: string[];
metadata?: Record<string, unknown>;
}
```
### Value Objects
#### ImpactedFile
Immutable representation of affected file.
```typescript
interface ImpactedFile {
readonly file: string;
readonly reason: string;
readonly distance: number; // Graph distance from change
readonly riskScore: number;
}
```
#### DependencyEdge
Immutable dependency relationship.
```typescript
interface DependencyEdge {
readonly source: string;
readonly target: string;
readonly type: 'import' | 'call' | 'extends' | 'implements';
}
```
#### DependencyMetrics
Aggregate dependency statistics.
```typescript
interface DependencyMetrics {
readonly totalNodes: number;
readonly totalEdges: number;
readonly avgDegree: number;
readonly maxDepth: number;
readonly cyclomaticComplexity: number;
}
```
#### SearchFilter
Search constraint specification.
```typescript
interface SearchFilter {
readonly field: string;
readonly operator: 'eq' | 'contains' | 'gt' | 'lt';
readonly value: unknown;
}
```
## Domain Services
### CodeIntelligenceAPI
Primary API for the domain.
```typescript
interface CodeIntelligenceAPI {
index(request: IndexRequest): Promise<Result<IndexResult, Error>>;
search(request: SearchRequest): Promise<Result<SearchResults, Error>>;
analyzeImpact(request: ImpactRequest): Promise<Result<ImpactAnalysis, Error>>;
mapDependencies(request: DependencyRequest): Promise<Result<DependencyMap, Error>>;
queryKG(request: KGQueryRequest): Promise<Result<KGQueryResult, Error>>;
}
```
## Domain Events
| Event | Trigger | Payload |
|-------|---------|---------|
| `IndexCompletedEvent` | Indexing finished | `{ filesIndexed, nodesCreated, edgesCreated }` |
| `ImpactAnalyzedEvent` | Impact analysis done | `{ changedFiles, impactedFiles, riskLevel }` |
| `CycleDetectedEvent` | Dependency cycle found | `{ cycle, severity }` |
| `HotspotIdentifiedEvent` | High-coupling node found | `{ node, inDegree, outDegree }` |
## Knowledge Graph Schema
### Node Types
| Type | Properties | Description |
|------|------------|-------------|
| `File` | `path, language, size, lastModified` | Source file |
| `Module` | `name, path, exports` | ES/CJS module |
| `Class` | `name, file, methods, properties` | Class definition |
| `Function` | `name, file, params, async, exported` | Function definition |
| `Interface` | `name, file, properties` | TypeScript interface |
| `Test` | `name, file, type, assertions` | Test case |
### Edge Types
| Type | From | To | Description |
|------|------|-----|-------------|
| `IMPORTS` | Module | Module | Import relationship |
| `CALLS` | Function | Function | Function call |
| `EXTENDS` | Class | Class | Inheritance |
| `IMPLEMENTS` | Class | Interface | Implementation |
| `TESTS` | Test | Function/Class | Test target |
| `CONTAINS` | File | Class/Function | File contents |
## Search Capabilities
### Search Types
| Type | Use Case | Algorithm |
|------|----------|-----------|
| `semantic` | Natural language queries | HNSW + embeddings |
| `exact` | Precise string match | Inverted index |
| `fuzzy` | Approximate match | Levenshtein distance |
### Query Examples
```typescript
// Semantic search
const results = await api.search({
query: "function that handles user authentication",
type: 'semantic',
limit: 10,
});
// Exact search with filters
const results = await api.search({
query: "handleLogin",
type: 'exact',
filters: [
{ field: 'language', operator: 'eq', value: 'typescript' },
{ field: 'type', operator: 'eq', value: 'function' },
],
});
// Knowledge Graph query (Cypher)
const results = await api.queryKG({
query: "MATCH (f:Function)-[:CALLS]->(g:Function) WHERE f.name = 'processOrder' RETURN g",
type: 'cypher',
});
// Natural language KG query
const results = await api.queryKG({
query: "What functions call the authentication service?",
type: 'natural-language',
});
```
## Context Integration
### Upstream Dependencies
- AST parsers (ts-morph, babel, tree-sitter)
- Embedding models (transformers)
### Downstream Consumers
- **Test Generation**: Import paths, function signatures
- **Coverage Analysis**: File complexity
- **Defect Intelligence**: Dependency analysis
- **All Domains**: Semantic code search
### Anti-Corruption Layer
The domain abstracts different languages through language-specific AST parsers, exposing a unified Knowledge Graph interface.
## Task Handlers
| Task Type | Handler | Description |
|-----------|---------|-------------|
| `index-codebase` | `index()` | Build/update Knowledge Graph |
| `semantic-search` | `search()` | O(log n) code search |
| `analyze-impact` | `analyzeImpact()` | Change impact analysis |
| `map-dependencies` | `mapDependencies()` | Dependency graph |
| `query-kg` | `queryKG()` | Knowledge Graph queries |
## Impact Analysis Algorithm
```typescript
async function analyzeImpact(changedFiles: string[], depth: number): Promise<ImpactAnalysis> {
const directImpact: ImpactedFile[] = [];
const transitiveImpact: ImpactedFile[] = [];
const visited = new Set<string>();
// BFS to find impacted files
const queue: Array<{ file: string; distance: number }> =
changedFiles.map(f => ({ file: f, distance: 0 }));
while (queue.length > 0) {
const { file, distance } = queue.shift()!;
if (visited.has(file) || distance > depth) continue;
visited.add(file);
const dependents = await getDependents(file);
for (const dependent of dependents) {
const impact: ImpactedFile = {
file: dependent,
reason: `Depends on ${file}`,
distance: distance + 1,
riskScore: calculateRiskScore(dependent, distance + 1),
};
if (distance === 0) {
directImpact.push(impact);
} else {
transitiveImpact.push(impact);
}
queue.push({ file: dependent, distance: distance + 1 });
}
}
return {
directImpact,
transitiveImpact,
impactedTests: await findImpactedTests(visited),
riskLevel: calculateOverallRisk(directImpact, transitiveImpact),
recommendations: generateRecommendations(directImpact, transitiveImpact),
};
}
```
## ADR References
- **ADR-006**: Unified Memory Service (KG storage)
- **ADR-009**: Hybrid Memory Backend (HNSW indexing)