Skip to content

Commit 0029d7a

Browse files
committed
feat: add code-tree knowledge graph for AI-assisted development and PR reviews
Restructure the monolithic 214-line CLAUDE.md into a slim entry point that redirects to a new AGENTS.md, with detailed content moved to topic-specific guides under docs/agents/. Code-tree tooling (tools/code-tree/): - code_tree.py: zero-dependency knowledge graph builder that parses Go, Python, TypeScript, Java, Rust via Python AST, tree-sitter, or regex fallback. Extracts symbols, imports, calls, inheritance, and test mappings. Outputs graph.json, tags.json, modules.json, and summary.md. Supports incremental rebuilds via file hashing. - query_graph.py: CLI query interface for symbol lookup, dependency and reverse-dependency queries, call graph traversal (callers, callees, call chains), inheritance hierarchy, BFS-based impact analysis, test impact analysis, and code chunk extraction. - test_query_graph.py: 31 unit tests covering impact BFS depth limiting, edge-type filtering, output determinism, symbol lookup, dependency queries, call graph, hierarchy, search, and stats. Agent guides (docs/agents/): - architecture.md: end-to-end flow, subsystem boundaries, dependency rules, CRD types, controller patterns, protocols, module structure - go-guide.md: controllers, API, ADK, CLI, coding standards, error handling, concurrency rules, CRD field workflow, linting - python-guide.md: ADK executor flow, UV workspace, type alignment with Go, LLM provider integration, framework support - ui-guide.md: Next.js 16 routing, components, technologies, Shadcn/ui primitives, hub components, development patterns - testing-ci.md: test suites, CI workflow matrix, E2E database matrix, coverage requirements, local CI reproduction, common failure patterns - code-tree.md: development workflow, query examples, entry points, hub files, module dependencies, type hierarchies PR review guides (.github/review-guides/): - architecture-context.md: subsystem boundaries, dependency directions, controller patterns, CRD type alignment, backward compatibility, cardinality changes - impact-analysis.md: PR size flags, hub files, cross-boundary changes, CRD change checklist, generated files policy - language-checklists.md: Go (error handling, concurrency, code quality, CRD-specific), Python (type hints, async, ruff), TypeScript (strict mode, Radix UI, Jest), YAML (Helm, CI) - security-review.md: container SecurityContext, RBAC manifests, credentials/secrets, network security, CI workflow security - test-quality.md: coverage requirements, proportional coverage table, type alignment coverage, test organization, fixture integrity, resource management Entry points: - AGENTS.md: main agent guide with kagent-dev skill reference, language guidelines, API versioning, code reuse/testing/commit policies, best practices, essential commands, reference file index, task routing table, validation checklist, and both code-tree options (in-repo tools + code-review-graph MCP) - .github/copilot-instructions.md: tier 1 PR reviewer with batch-based file review strategy, context pruning, code-tree impact analysis integration, security scan, review guide index - CLAUDE.md: slimmed to ./AGENTS.md redirect - .gitignore: added docs/code-tree/ and .code-review-graph/ Verified against codebase: 21 claims audited, all file paths, entry points, CI matrix values, and tool versions confirmed accurate. Code-tree builder tested on kagent repo: indexed 4,123 nodes and 12,435 edges across 557 files in 5 languages. All 31 unit tests pass. Signed-off-by: Jaison Paul <paul.jaison@gmail.com>
1 parent ddebd6d commit 0029d7a

18 files changed

Lines changed: 5301 additions & 215 deletions

.github/copilot-instructions.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copilot Instructions
2+
3+
Tier 1 PR reviewer for the kagent monorepo. Maintainer time is limited; your vigilance helps ensure project quality.
4+
5+
## Review style
6+
7+
- Explain the "why" behind recommendations.
8+
- Skip details caught by CI linters (golangci-lint, ruff, eslint).
9+
- Verify: clear docstrings, type hints, descriptive names, proper decomposition, sufficient comments.
10+
- Verify: new logic has tests proportional to change size; PR description explains the "why".
11+
- Identify design flaws, redundancy, and security issues.
12+
- Estimate LLM-generation likelihood with confidence level as an HTML comment in "PR Overview".
13+
- Comment "All Copilot criteria are met." when all criteria are met.
14+
15+
## Review strategy
16+
17+
For each batch: load review guide -> read diffs -> run code-tree queries -> post comments -> compress or drop context -> unload guide.
18+
19+
### Small-batch file review
20+
21+
Review files in batches of 3-5, in this order:
22+
23+
1. **CRD Types** -- `go/api/v1alpha2/` changes (type safety, backward compat, JSON tags)
24+
2. **Controllers & Translators** -- `go/core/internal/controller/` changes (reconciliation, error handling)
25+
3. **Go ADK & HTTP** -- `go/adk/`, `go/core/internal/httpserver/` changes
26+
4. **Python ADK** -- `python/packages/kagent-adk/` changes (type alignment, async patterns)
27+
5. **UI Components** -- `ui/src/` changes (TypeScript strict mode, React patterns)
28+
6. **Helm & CI** -- `helm/`, `.github/workflows/` changes (security, RBAC)
29+
7. **Tests** -- test files reviewed against the code they test
30+
31+
### Context pruning between batches
32+
33+
After each batch, summarize key observations (new symbols, behavior changes, test gaps). Drop file contents; keep only the summary for cross-referencing in later batches.
34+
35+
## Code-tree impact analysis
36+
37+
Before posting a final review, run these queries on the changed files:
38+
39+
```bash
40+
# 1. Regenerate graph (incremental, quiet)
41+
python3 tools/code-tree/code_tree.py --repo-root . --incremental -q
42+
43+
# 2. For each changed source file, check blast radius
44+
python3 tools/code-tree/query_graph.py --impact <changed-file> --depth 3
45+
46+
# 3. Check which tests are affected
47+
python3 tools/code-tree/query_graph.py --test-impact <changed-file>
48+
49+
# 4. For hub file changes, increase depth
50+
python3 tools/code-tree/query_graph.py --impact <hub-file> --depth 5
51+
```
52+
53+
Flag untested impact paths in the review.
54+
55+
## Security scan
56+
57+
For PRs touching Helm templates, RBAC, Dockerfiles, or credentials, load [security-review.md](review-guides/security-review.md) and apply its checklist.
58+
59+
## Review guide files
60+
61+
Load the relevant guide per batch:
62+
63+
| Guide | When to load |
64+
|-------|-------------|
65+
| [architecture-context.md](review-guides/architecture-context.md) | Multi-subsystem PRs, controller changes, CRD hierarchy changes |
66+
| [impact-analysis.md](review-guides/impact-analysis.md) | Large PRs, CRD changes, hub file changes |
67+
| [language-checklists.md](review-guides/language-checklists.md) | Any code change (pick relevant language section) |
68+
| [security-review.md](review-guides/security-review.md) | Helm, RBAC, security contexts, credentials, Dockerfiles |
69+
| [test-quality.md](review-guides/test-quality.md) | PRs adding/modifying tests, or PRs missing tests |
70+
71+
## Quick checklist
72+
73+
- [ ] Code reuse: no duplicated logic; shared helpers extracted
74+
- [ ] Tests proportional to change size
75+
- [ ] CRD changes: types + manifests + translator + ADK types (Go + Python) + tests
76+
- [ ] Hub file changes: impact analysis run, extra test coverage
77+
- [ ] Security: no hardcoded credentials, proper RBAC, non-root containers
78+
- [ ] Generated files: not hand-edited, regeneration commands run
79+
- [ ] Cross-language alignment: Go ↔ Python types match
80+
- [ ] Conventional commit message format
81+
- [ ] DCO sign-off present on all commits
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Architecture Context for Reviews
2+
3+
Load when reviewing PRs that touch multiple subsystems, modify controllers, or change CRD types.
4+
5+
**See also:** [impact-analysis.md](impact-analysis.md), [language-checklists.md](language-checklists.md)
6+
7+
---
8+
9+
## High-level architecture
10+
11+
```
12+
UI (Next.js) -> Controller HTTP Server (Go) -> A2A proxy -> Agent Pod (Python/Go ADK)
13+
-> MCP Tool Servers -> LLM Provider -> back to UI via SSE streaming
14+
```
15+
16+
## Key subsystem boundaries
17+
18+
| Subsystem | Language | Root path |
19+
|-----------|----------|-----------|
20+
| CRD Types & API | Go | `go/api/` |
21+
| Controllers | Go | `go/core/internal/controller/` |
22+
| HTTP Server | Go | `go/core/internal/httpserver/` |
23+
| Database Layer | Go | `go/core/internal/database/` |
24+
| A2A Protocol | Go | `go/core/internal/a2a/` |
25+
| MCP Integration | Go | `go/core/internal/mcp/` |
26+
| CLI | Go | `go/core/cli/` |
27+
| Go ADK | Go | `go/adk/` |
28+
| Python ADK | Python | `python/packages/kagent-adk/` |
29+
| Web UI | TypeScript | `ui/src/` |
30+
| Helm Charts | YAML | `helm/` |
31+
32+
## Critical dependency directions
33+
34+
Flag violations of these dependency rules:
35+
36+
```
37+
# Allowed direction (arrow = "may depend on"). Reverse is forbidden.
38+
go/core/ -> go/api/ (core may use api types, NOT the reverse)
39+
go/adk/ -> go/api/ (adk may use api types, NOT the reverse)
40+
go/core/internal/controller/ -> go/core/internal/database/
41+
42+
# Forbidden imports
43+
go/api/ must NOT import go/core/ or go/adk/
44+
ui/ must NOT import go/ or python/ directly
45+
```
46+
47+
Full dependency map: see [code-tree.md](../../docs/agents/code-tree.md#key-module-dependencies).
48+
49+
## Controller patterns
50+
51+
- **Shared reconciler**: All controllers share `kagentReconciler` — changes affect all CRD types
52+
- **Translator pattern**: CRD spec → K8s resources via translators. PRs must update translator when adding CRD fields
53+
- **Database-level concurrency**: Atomic upserts, no application-level locks. Do NOT introduce mutexes
54+
- **Idempotent reconciliation**: Each loop iteration must be safe to retry
55+
- **Network I/O outside transactions**: Long-running operations must not hold database locks
56+
57+
## CRD type alignment
58+
59+
When Go CRD types mirror Python ADK types (e.g., `AgentSpec``types.py`):
60+
61+
- Add cross-reference comments in both languages
62+
- Go types are the source of truth
63+
- Flag changes to one side without corresponding changes to the other
64+
- Both serialize to JSON via `config.json` — field names must match
65+
66+
## Backward compatibility
67+
68+
For CRD/API changes:
69+
70+
- New fields must have safe defaults (zero-value must not break existing agents)
71+
- What happens when an old controller reads a new CRD? Migration path must be explicit
72+
- `v1alpha2` allows breaking changes, but prefer backward-compatible additions
73+
- Database schema changes require migration logic in `go/core/internal/database/`
74+
75+
## Cardinality changes
76+
77+
When a value changes from single to list (or vice versa):
78+
79+
1. Use `--callers` and `--rdeps` to find all consumers
80+
2. Check for single-value assumptions in translators, HTTP handlers, and UI
81+
3. Verify database model handles the change
82+
4. Verify Helm templates handle the change
83+
84+
## Type hierarchies
85+
86+
Changes to base types affect all consumers. Key hierarchies in [code-tree.md](../../docs/agents/code-tree.md#key-type-hierarchies).
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Impact Analysis for Reviews
2+
3+
Load when assessing blast radius, CRD changes, or reviewing large PRs.
4+
5+
**See also:** [architecture-context.md](architecture-context.md) (backward compat, cardinality), [test-quality.md](test-quality.md) (coverage adequacy)
6+
7+
---
8+
9+
## PR size and scope
10+
11+
- Flag PRs > 500 additions crossing 3+ subsystems as "needs decomposition discussion"
12+
- XXL PRs (>1000 additions): each component needs dedicated tests, not just E2E
13+
- Suggest splitting when a single PR includes: CRD types + controller + ADK types + E2E + Helm + UI
14+
15+
## Hub files
16+
17+
Changes to these files need extra scrutiny. Full table in [code-tree.md](../../docs/agents/code-tree.md#hub-files-most-connected).
18+
19+
| File | Role |
20+
|------|------|
21+
| `go/api/v1alpha2/agent_types.go` | Agent CRD — changes cascade to controllers, translators, ADK, UI |
22+
| `go/api/adk/types.go` | ADK config — changes require Python mirror update |
23+
| `python/packages/kagent-adk/src/kagent/adk/_agent_executor.py` | Core executor — all agent requests flow through here |
24+
| `ui/src/lib/messageHandlers.ts` | A2A parsing — all UI message rendering depends on this |
25+
26+
## Cross-boundary changes
27+
28+
Flag without clear justification:
29+
30+
- CRD type changes that don't update the translator
31+
- Go ADK type changes without Python mirror updates
32+
- Controller changes that modify HTTP server behavior
33+
- Helm template changes without corresponding Go/Python changes
34+
- UI changes that assume new API fields without backend support
35+
36+
## Cross-file consistency
37+
38+
When the same pattern is applied to many files (new CRD field across types, new config option):
39+
40+
- Spot-check at least 3 files for consistency
41+
- Verify the change propagates through: CRD types → translator → ADK types (Go + Python) → UI
42+
43+
## CRD change checklist
44+
45+
1. Field added to type struct in `go/api/v1alpha2/`
46+
2. `make -C go manifests generate` run (CRD YAML + DeepCopy updated)
47+
3. Translator updated in `go/core/internal/controller/translator/`
48+
4. ADK types updated in `go/api/adk/types.go`
49+
5. Python types mirrored in `kagent-adk/src/kagent/adk/types.py`
50+
6. Helm CRD templates updated if schema changed
51+
7. Unit tests for translator handling
52+
8. E2E test for end-to-end flow
53+
9. Generated CRD manifests committed (not hand-edited)
54+
55+
## Generated files
56+
57+
Never hand-edit. Key generated files:
58+
59+
- `go/api/config/crd/` — CRD manifests (generated by controller-gen)
60+
- `go/api/v1alpha2/zz_generated.deepcopy.go` — DeepCopy methods
61+
62+
Reject PRs modifying generated files without running generators.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Language-Specific Review Checklists
2+
3+
Load when reviewing code changes. Pick the relevant language section.
4+
5+
---
6+
7+
## Go (controllers, API, ADK)
8+
9+
### Error handling and control flow
10+
- Every `err :=` must be checked or returned. Swallowed errors cause silent failures.
11+
- Wrap errors: `fmt.Errorf("context: %w", err)`
12+
- `return` inside loops: verify it should be `continue`, not premature exit
13+
- Loops assigning to a variable: verify intermediate values aren't discarded
14+
- No variable shadowing of function arguments
15+
- No unnecessary `else` after `return`
16+
17+
### Concurrency
18+
- No nested goroutines (`go func() { go func() { ... } }()`)
19+
- Reuse K8s clients from struct fields, not `kubernetes.NewForConfig()` per call
20+
- Fire-and-forget goroutines: require `context.WithTimeout` + error logging
21+
- Database operations: atomic upserts, no application-level mutexes
22+
23+
### Code quality
24+
- `golangci-lint run` passes
25+
- Context propagation (`context.Context` as first parameter)
26+
- Resource cleanup (`defer` close for readers/connections)
27+
- Table-driven tests for new functions
28+
- Descriptive variable names (`fingerPrint` not `fp`, `cacheKey` not `ck`)
29+
- camelCase for locals, PascalCase for exports
30+
- No deprecated fields in new code
31+
32+
### CRD-specific
33+
- New fields have JSON tags matching the Go field name (camelCase)
34+
- `+optional` marker for optional fields
35+
- DeepCopy generated (`make -C go generate`)
36+
- CRD manifests regenerated (`make -C go manifests`)
37+
38+
## Python (ADK, skills, integrations)
39+
40+
- Type hints on all function signatures
41+
- No bare `Any` types without justification
42+
- Ruff formatting compliance
43+
- `async/await` used consistently (no mixing sync and async)
44+
- Cross-language types: add `# Must stay aligned with Go type in ...` comments
45+
- No bare `Exception` catches — use specific exception types
46+
- No mutable default arguments
47+
- Tests use `pytest` with `@pytest.mark.asyncio` for async tests
48+
49+
## TypeScript (UI)
50+
51+
- Strict mode compliance (no `any` type)
52+
- No inline styles — use TailwindCSS classes
53+
- No direct DOM manipulation — use React patterns
54+
- Radix UI primitives for accessibility
55+
- React Hook Form + Zod for form validation
56+
- Jest tests for logic-heavy components
57+
- ESLint + Next.js lint passing
58+
59+
## YAML (Helm charts, CI)
60+
61+
- Helm templates use proper `.Values` references
62+
- No hardcoded image tags — use chart values
63+
- CI workflows: no secrets in logs, pinned action versions
64+
- CRD templates match generated manifests in `go/api/config/crd/`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Security Review Guide
2+
3+
Load when reviewing PRs touching Helm templates, RBAC, security contexts, Dockerfiles, or credentials.
4+
5+
---
6+
7+
## Principles
8+
9+
- **Enforcement > defaults**: Security contexts must be enforced, not just defaulted
10+
- **Never configurable**: `allowPrivilegeEscalation`, `privileged` must be hardcoded `false` — no env var overrides
11+
- **System containers**: Controller, ADK runtime, UI must ALWAYS run non-root
12+
- **Defense in depth**: Backend must enforce policy independently of UI. UI is convenience; controller is enforcement
13+
- **Threat model required**: PRs adding security-configurable fields should answer: "What could a malicious agent definition do?"
14+
15+
## Container SecurityContext checklist
16+
17+
**NEVER user-configurable:** `privileged`, `add_capabilities`, `allowPrivilegeEscalation`
18+
**ALWAYS hardcoded:** `drop: ["ALL"]`, `seccompProfile: RuntimeDefault`
19+
**MAY be user-configurable:** `runAsUser` (warn on UID 0), `runAsGroup`, `readOnlyRootFilesystem`
20+
21+
- Container-level `securityContext` must not conflict with pod-level `PodSecurityContext`
22+
- Admin-set contexts take precedence over user-specified values
23+
- Tests must not normalize violations
24+
25+
## RBAC manifest review
26+
27+
- `resourceNames` restrictions on `update`/`patch`/`delete` where possible
28+
- `create` can't be scoped by `resourceNames` — document why needed
29+
- Helm chart RBAC templates use `.Values` for namespace
30+
- New ClusterRoles follow `kagent-*` naming convention
31+
- ServiceAccount per agent pod (not shared)
32+
33+
## Credentials and secrets
34+
35+
- No hardcoded credentials in any file
36+
- API keys stored in Kubernetes Secrets, referenced via `apiKeySecret` in ModelConfig CRD
37+
- `ValueRef` type used for secret references (namespace + name + key)
38+
- MCP server auth headers referenced via `headersFrom` (SecretKeySelector)
39+
- No credentials in container environment variables — mount as files or use Secret references
40+
41+
## Network security
42+
43+
- Agent pods communicate with controller via ClusterIP services
44+
- MCP tool server connections should use TLS when available
45+
- A2A protocol endpoints should validate request origin
46+
- `allowedNamespaces` on RemoteMCPServer controls cross-namespace access
47+
48+
## General
49+
50+
- No secrets in CI workflow logs
51+
- Pinned action versions in GitHub workflows (SHA, not tag)
52+
- Docker images use non-root USER
53+
- Helm values: sensitive defaults are empty, not example values
54+
- Input validation on agent names and configurations
55+
- ConfigMap/Secret cleanup on resource deletion (no orphans)

0 commit comments

Comments
 (0)