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 |
|---|---|
artifact_fingerprints |
Dependency-free fingerprints for persisted compiler artifacts. |
artifact_quality |
Commit-surface and completeness gates for reusable benchmark artifacts. |
cache_lock |
Cooperative serialization for a private compiler cache. |
checkout_identity |
Validate that a repository checkout matches a compiled manifest. |
graph_artifact |
Authenticated loading for persisted symbol-graph artifacts. |
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) |
manifest_source |
Bind live repository reads to a manifest's exact source-selection era. |
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. |
zoekt_artifact |
Authenticate Zoekt shards before handing them to an external process. |
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.
Requested views are rebuilt from the captured source generation. An incremental delta is unsafe until the Git cleanliness/HEAD observation can be bound to the same pinned repository authority as the source fingerprint; a path-based status check can otherwise observe a temporary replacement repository and authorize reuse for different bytes.
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
IndexCompilerConfig
dataclass
¶
IndexCompilerConfig(
cache_dir_name: str = REPO_INDEX_DIRNAME,
index_types: list[str] = (lambda: ["bm25", "vector", "symbol_graph"])(),
languages: list[str] = (lambda: ["python"])(),
source_selection: RepositorySourceSelection | None = None,
)
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 = "",
source_selection: RepositorySourceSelection | None = (
lambda: DEFAULT_REPOSITORY_SOURCE_SELECTION
)(),
last_indexed_source_selection_digest: 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 |
Atomically replace the manifest after flushing the JSON payload. |
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. |
Attributes:
| Name | Type | Description |
|---|---|---|
source_selection_digest |
str | None
|
Canonical current selection identity, or the v1.1 legacy sentinel. |
source_selection_digest
property
¶
Canonical current selection identity, or the v1.1 legacy sentinel.
save
¶
Atomically replace the manifest after flushing the JSON payload.
Source code in codenib/compiler/manifest.py
load
classmethod
¶
load(path: str | Path) -> RepoManifest
Load a manifest from a JSON file.
Source code in codenib/compiler/manifest.py
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,
native_index_authorization: NativeIndexAuthorization | None = None,
native_index_authorization_resolver: NativeIndexAuthorizationResolver | None = None,
source_selection: RepositorySourceSelection | 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.
Existing native vector views require either a capability already bound to the intended cache or a resolver that receives its current manifest entry. When this function itself compiles new bytes, it may mint only for that compiler-owned captured output.
Source code in codenib/compiler/skill_context.py
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 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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 | |
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",
native_index_authorization: NativeIndexAuthorization | None = None,
native_index_authorization_resolver: NativeIndexAuthorizationResolver | None = None
) -> 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.
native_index_authorization_resolver, when supplied, is invoked with
that exact vector IndexEntry and must return an out-of-band capability
bound to the same tree and config. Manifest data never mints authority.
Raises:
| Type | Description |
|---|---|
ValueError
|
if any |
Source code in codenib/compiler/skill_context.py
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
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.