Code Mirror Generation¶
akms.graph.generate_mirror
¶
generate_mirror.py — Code Mirror Generation + Drift Check (§2.9 of system design).
Generates the code mirror — a search index that allows qmd to replace grep. Mirror nodes exist in the graph as simple existence markers (no edges, no tags).
Runs as part of the write-back cycle after update_graph.py. Only processes
files modified in the current phase (from git diff).
Generation algorithm (legacy provider):
1. Parse source file with ast module
2. For each FunctionDef, AsyncFunctionDef, ClassDef:
- Extract docstring → rendered as markdown (semantic search layer)
- Extract full source → wrapped in `python block (literal search layer)
3. Write mirror file toknowledge/code-mirror/{module_path}.md``
4. Write mirror node frontmatter (marker only)
Provider routing (A2-4):
Public :func:generate_mirror dispatches through the mirror-provider
protocol (default: legacy AST). The pure legacy body is
:func:generate_mirror_legacy.
Docstring drift detection lives in :mod:akms.graph.drift and is
re-exported here for backward-compatible imports.
check_docstring_drift_llm
¶
Check for semantic docstring drift using an LLM call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definitions
|
list[dict]
|
Extracted definitions from extract_definitions(). |
required |
llm_fn
|
Any
|
Callable (prompt: str) → str. If None, falls back to structural check. |
None
|
Returns list of drift warning dicts with keys
function, type, detail, llm_response
Source code in packages/akms/src/akms/graph/drift.py
check_docstring_drift_structural
¶
Check for structural docstring drift (pure heuristic, no LLM).
Catches the most obvious contradictions: - Docstring mentions parameters not in the signature - Docstring misses parameters that are in the signature (excluding self/cls) - Docstring mentions 'returns None' but function has a non-None annotation
For full semantic drift detection, use check_docstring_drift_llm() which requires an LLM API call.
Returns list of drift warning dicts.
Source code in packages/akms/src/akms/graph/drift.py
extract_definitions
¶
Parse Python source and extract all top-level and class-level definitions.
Returns a list of dicts with keys
name, type (function|async_function|class), docstring, source, parameters, return_annotation, decorators
Source code in packages/akms/src/akms/graph/generate_mirror.py
write_mirror_file
¶
write_mirror_file(
repo_root: Path,
source_file: str,
source_content: str,
phase: int,
generated_at: datetime | None = None,
) -> dict[str, Any] | None
Generate and write a mirror file for a single source file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_root
|
Path
|
Path to the repository root. |
required |
source_file
|
str
|
Relative path to the source file (e.g., 'src/module.py'). |
required |
source_content
|
str
|
Python source code to parse. |
required |
phase
|
int
|
Phase number that generated this mirror. |
required |
generated_at
|
datetime | None
|
Timestamp (defaults to now). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Dict with mirror info or None if no definitions found. |
Source code in packages/akms/src/akms/graph/generate_mirror.py
get_changed_files
¶
get_changed_files(
repo_root: Path,
parent_branch: str = "main",
extensions: set[str] | None = None,
) -> list[str]
Get files changed between parent_branch and HEAD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_root
|
Path
|
Repository root path. |
required |
parent_branch
|
str
|
Branch to diff against (default 'main'). |
'main'
|
extensions
|
set[str] | None
|
Set of extensions to include (default {'.py'}). |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of relative file paths. |
Source code in packages/akms/src/akms/graph/generate_mirror.py
generate_mirror_legacy
¶
generate_mirror_legacy(
repo_root: str | Path,
phase: int,
parent_branch: str = "main",
source_files: list[str] | None = None,
drift_check: bool = True,
llm_fn: Any = None,
generated_at: datetime | None = None,
) -> dict[str, Any]
Legacy in-process Python AST mirror generation (provider implementation body).
This is the historical algorithm, unchanged in behavior. Prefer the public
:func:generate_mirror entry point which routes through the provider protocol.
Source code in packages/akms/src/akms/graph/generate_mirror.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
generate_mirror
¶
generate_mirror(
repo_root: str | Path,
phase: int,
parent_branch: str = "main",
source_files: list[str] | None = None,
drift_check: bool = True,
llm_fn: Any = None,
*,
config: Any = None,
provider_name: str | None = None,
) -> dict[str, Any]
Generate code mirror files via the configured mirror provider.
Default provider is legacy (Python AST), preserving historical behavior
for callers that omit config / provider_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_root
|
str | Path
|
Path to the repository root. |
required |
phase
|
int
|
Current phase number. |
required |
parent_branch
|
str
|
Branch to diff against (default 'main'). |
'main'
|
source_files
|
list[str] | None
|
Explicit list of source files (overrides git diff). |
None
|
drift_check
|
bool
|
Whether to run drift detection. |
True
|
llm_fn
|
Any
|
Optional LLM callable for semantic drift check. |
None
|
config
|
Any
|
Optional |
None
|
provider_name
|
str | None
|
Override provider name (default from config / legacy). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with keys: |
dict[str, Any]
|
{ "mirrors": [...], "drift_warnings": [...], "files_processed": int, "definitions_total": int, additive provider fields:¶"provider": str, "provider_metadata": dict, "errors": list, "success": bool, "fallback_used": bool, |
dict[str, Any]
|
} |
Source code in packages/akms/src/akms/graph/generate_mirror.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |