codenib.compiler
¶
Compiler infrastructure for CodeNib.
The package provides:
-
Index compilation (Phase 1):
IndexCompilerbuilds all indexes for a repository and writes aRepoManifestrecording what was built, where, and when. -
Resource management:
ResourceResolverchecks index freshness and producesResourcePlandecisions used byResourceGuardto filter available skills at query time. -
Parameter resolution:
SessionContext+resolve_paramsmerge config defaults, session adjustments, and query-time overrides.
Modules:
| Name | Description |
|---|---|
index_builders |
Index builder protocol, registry, and concrete implementations. |
index_compiler |
Index Compiler — Phase 1 of the two-phase compilation architecture. |
manifest |
Repo manifest — the linking protocol between index compilation (Phase 1) |
params |
Layered parameter resolution for skill execution. |
resources |
Index resource management for the skill compiler. |
skill_context |
Skill-aware context construction. |
snapshot_store |
Content-addressed repository snapshots and reusable analysis artifacts. |
verification |
Admission control for incrementally updated indexes. |
Classes:
| Name | Description |
|---|---|
IndexBuilderRegistry |
Maps index_type names to their builder implementations. |
IndexCompiler |
Orchestrate Phase 1: build all indexes for a repository and write |
IndexCompilerConfig |
Configuration for the IndexCompiler. |
ManifestIndexStateStore |
Read-only |
RepoManifest |
Top-level manifest describing a compiled repository. |
ResolvedParams |
Final resolved parameter set after merging all layers. |
SessionContext |
Layer 3: Runtime session/repo context injected once per session. |
IndexRequirement |
Declaration of an index dependency from a skill config. |
IndexState |
Lifecycle state of an index. |
ResourcePlan |
Aggregate resource decisions for all indexes needed by a plan. |
ResourceResolver |
Resolves index requirements against current index state. |
Functions:
| Name | Description |
|---|---|
resolve_params |
Merge parameter layers with later layers taking precedence. |
build_skill_contexts |
Build the union of indexes required by skill_ids and package them. |
load_contexts_from_manifest |
Load index artifacts directly from a pre-built |
required_index_types |
Union of |
IndexBuilderRegistry
¶
IndexCompiler
¶
IndexCompiler(
builder_registry: IndexBuilderRegistry, config: IndexCompilerConfig | None = None
)
Orchestrate Phase 1: build all indexes for a repository and write a manifest.
The manifest (repo_manifest.json) is the linking protocol that
Phase 2 (AgentRunner + ResourceGuard) reads to know
what indexes are available at query time.
Methods:
| Name | Description |
|---|---|
compile_repo |
Build all requested indexes and write the repo manifest. |
update_repo |
Advance an existing manifest to the repo's current HEAD. |
Source code in codenib/compiler/index_compiler.py
compile_repo
¶
compile_repo(
repo_path: str,
*,
index_types: list[str] | None = None,
cache_dir: str | None = None
) -> RepoManifest
Build all requested indexes and write the repo manifest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path
|
str
|
Absolute path to the repository root. |
required |
index_types
|
list[str] | None
|
Which indexes to build (default: config.index_types). |
None
|
cache_dir
|
str | None
|
Override for the cache directory
(default: |
None
|
Returns:
| Type | Description |
|---|---|
RepoManifest
|
The completed |
Source code in codenib/compiler/index_compiler.py
update_repo
¶
update_repo(
repo_path: str,
*,
index_types: list[str] | None = None,
cache_dir: str | None = None
) -> RepoManifest
Advance an existing manifest to the repo's current HEAD.
Each builder is asked for an incremental update rather than a full build. Builders without a real delta path fall back to a rebuild internally, so the result is always correct -- only the cost differs.
Falls back to a full :meth:compile_repo when there is no existing
manifest, or when the previously indexed commit cannot be determined.
Returns the existing manifest untouched when HEAD has not moved.
Source code in codenib/compiler/index_compiler.py
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 | |
IndexCompilerConfig
dataclass
¶
IndexCompilerConfig(
cache_dir_name: str = REPO_INDEX_DIRNAME,
index_types: list[str] = (lambda: ["bm25", "vector", "symbol_graph"])(),
languages: list[str] = (lambda: ["python"])(),
)
Configuration for the IndexCompiler.
ManifestIndexStateStore
¶
ManifestIndexStateStore(manifest: RepoManifest)
Read-only IndexStateStore backed by a RepoManifest.
Maps manifest IndexEntry objects to IndexStatus objects that
ResourceResolver can consume. Does not support set_status —
the manifest is written only by IndexCompiler.
Source code in codenib/compiler/manifest.py
RepoManifest
dataclass
¶
RepoManifest(
version: str = MANIFEST_VERSION,
repo_path: str = "",
commit: str = "",
last_indexed_commit: str = "",
source_fingerprint: str = "",
last_indexed_source_fingerprint: str = "",
languages: list[str] = list(),
file_count: int = 0,
indexes: dict[str, IndexEntry] = dict(),
capabilities: dict[str, bool] = dict(),
compiled_at: str = "",
compiled_at_epoch: float = 0.0,
)
Top-level manifest describing a compiled repository.
Written by IndexCompiler after Phase 1. Read by
ManifestIndexStateStore at query time (Phase 2).
Methods:
| Name | Description |
|---|---|
save |
Write the manifest to a JSON file. |
load |
Load a manifest from a JSON file. |
derive_capabilities |
Compute capabilities from the set of available indexes. |
index_is_current |
Whether a view was built for the manifest's current source state. |
save
¶
Write the manifest to a JSON file.
load
classmethod
¶
load(path: str | Path) -> RepoManifest
derive_capabilities
¶
Compute capabilities from the set of available indexes.
Source code in codenib/compiler/manifest.py
index_is_current
¶
Whether a view was built for the manifest's current source state.
Source code in codenib/compiler/manifest.py
ResolvedParams
dataclass
¶
Final resolved parameter set after merging all layers.
SessionContext
dataclass
¶
SessionContext(
repo_path: str | None = None,
repo_size: int | None = None,
primary_language: str | None = None,
index_freshness: dict[str, float] = dict(),
user_expertise: str = "intermediate",
extras: dict[str, Any] = dict(),
)
Layer 3: Runtime session/repo context injected once per session.
IndexRequirement
dataclass
¶
IndexRequirement(
index_type: str,
max_age_seconds: float = 3600.0,
scope: str = "current_repo",
required: bool = True,
)
Declaration of an index dependency from a skill config.
IndexState
¶
Bases: str, Enum
Lifecycle state of an index.
ResourcePlan
dataclass
¶
ResourcePlan(
decisions: list[ResourceDecision] = list(),
can_execute: bool = True,
blocking_builds: list[str] = list(),
async_updates: list[str] = list(),
warnings: list[str] = list(),
)
Aggregate resource decisions for all indexes needed by a plan.
ResourceResolver
¶
resolve_params
¶
resolve_params(
defaults: dict[str, Any],
session_ctx: SessionContext | None = None,
query_params: dict[str, Any] | None = None,
*,
session_rules: SessionParamRules | None = None
) -> ResolvedParams
Merge parameter layers with later layers taking precedence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
defaults
|
dict[str, Any]
|
Layer 1+2 defaults from config.yaml. |
required |
session_ctx
|
SessionContext | None
|
Layer 3 runtime session context. |
None
|
query_params
|
dict[str, Any] | None
|
Layer 4 query-time overrides (invocation inputs). |
None
|
session_rules
|
SessionParamRules | None
|
Optional rules for session-based adjustment. |
None
|
Returns:
| Type | Description |
|---|---|
ResolvedParams
|
|
Source code in codenib/compiler/params.py
build_skill_contexts
¶
build_skill_contexts(
repo_path: str,
skill_ids: Iterable[str],
*,
languages: Sequence[str] = ("python",),
cache_dir: str | None = None,
skills_dir: str | None = None,
skill_registry: SkillRegistry | None = None,
builder_registry: IndexBuilderRegistry | None = None,
embedding_model: str = DEFAULT_EMBEDDING_MODEL,
embedding_revision: str | None = None,
embedding_dimension: int = DEFAULT_EMBEDDING_DIMENSION,
default_top_k: int = 10,
default_level: str = "l2",
rebuild: bool = False,
trust_remote_code: bool | None = None,
embedding_batch_size: int | None = None,
embedding_max_seq_length: int | None = None
) -> dict[str, Any]
Build the union of indexes required by skill_ids and package them.
Returns the contexts dict accepted by SkillLoader.load_all /
SkillLoader.load_skill: {"retrieve": RetrieveContext, "expand":
ExpandContext, ...}. Keys are only present when at least one skill of
that type was requested AND its index dependencies could be loaded.
Build behaviour
- Already-populated index directories under
cache_dir/<index_type>are loaded, not rebuilt. - Missing types trigger
IndexCompiler.compile_repofor that subset. rebuild=Trueforces a fresh build of every requested type.
The function does no agent-side work; it returns context objects ready
to hand to SkillLoader. If a requested skill has no index
requirements (e.g. code_to_query), it will not contribute a key
here — its skill type still gets a context if a sibling skill needs
one (e.g. embedding_search shares the retrieve key with
bm25_search).
Skill-metadata resolution: when skills_dir is given, each skill's
index_requirements / skill_type are read directly from its
config.yaml on disk, so callers no longer have to pre-populate the
SkillRegistry before calling this (see #154). The registry is only
consulted for skills that are not present on disk.
Source code in codenib/compiler/skill_context.py
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 444 445 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 | |
load_contexts_from_manifest
¶
load_contexts_from_manifest(
manifest: RepoManifest,
*,
skill_ids: Iterable[str],
skill_registry: SkillRegistry | None = None,
default_top_k: int = 10,
default_level: str = "l2"
) -> dict[str, Any]
Load index artifacts directly from a pre-built RepoManifest.
The AoT counterpart to :func:build_skill_contexts: instead of
deciding what to build from skill_ids and then building, this
function consumes a manifest written by a previous
:class:~codenib.compiler.index_compiler.IndexCompiler.compile_repo
run, validates that the indexes the requested skill_ids need
exist in it, and packages them into the same context dict shape that
:class:~codenib.agent.skills.loader.SkillLoader consumes.
Source of truth for paths and configs is the manifest itself —
manifest.indexes[t].path is loaded directly, ignoring any
conventional <cache_dir>/<t> layout. The vector index's embedding
model/dimension are read from manifest.indexes["vector"].config
when present.
Raises:
| Type | Description |
|---|---|
ValueError
|
if any |
Source code in codenib/compiler/skill_context.py
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 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
required_index_types
¶
required_index_types(
skill_ids: Iterable[str],
*,
skills_dir: str | None = None,
skill_registry: SkillRegistry | None = None
) -> set[str]
Union of index_type strings the given skills must have built.
index_requirements is declarative skill metadata that lives on disk in
each skill's config.yaml. The SkillRegistry is a runtime binding
layer built on top of that metadata — and it is empty until SkillLoader
has loaded the very contexts this resolution feeds into. To break that
chicken-and-egg, prefer the on-disk configs.
Resolution order
- If
skills_diris given, read each skill'sconfig.yamlfrom disk. This is the authoritative source for bundled skills. - Fall back to
skill_registry(or the singleton) for skills that don't live on disk — e.g. registered via the@skilldecorator at import time.
Requirements marked required: false are excluded — those are "use if
present, don't force a build" (e.g. bm25_search's symbol_graph,
used only to relabel names). Use :func:optional_index_types for those.
Skills found in neither source are ignored — callers that need to fail loudly should check membership themselves.