Capsules are trusted-local executable manifests. Store only code you own or have reviewed.
1. What a Capsule Is
A Capsule is the durable form of a Python callable. It stores enough metadata to restore and run a function later, including:- identity: namespace, name, and explicit version
- signature: parameters, return annotation, and JSON schema
- restore layers: source, import path, and optional
cloudpicklepayload - dependencies: Python version and imports found in source
- capabilities: whether it can be used locally, exported to MCP, or exported for Anthropic programmatic tools
- trust and fingerprints: local-only execution flags and hashes for review
system extension (sys-capsule). Canonical implementation lives under src/heavenbase/extensions/system/capsule/; import hb.Capsule from import heavenbase as hb.
2. Store a Function
Capsule.register(...) defaults to overwrite=False. Pass registry_config=... to isolate registry storage in tests or demos.
3. Load and Run It Elsewhere
4. Docstrings and Formats
Capsules parse callable docstrings during capture and can synthesize a callable docstring from the stored signature metadata.Capsule.from_str(...) restores JSON and YAML manifest strings back into a Capsule.
5. Architecture Data Flow
The Capsule flow is explicit and layered:- Python callable → internal serialization strategy inspects source, import path, docstring, signature, dependencies, and optional binary fallback.
- Strategy →
CapsuleManifestrecords identity, schema, restore layers, capabilities, trust flags, and fingerprints. capsule.register()→ hidden registry workspace stores the activesys-capsulerow.Capsule.load(...)→ manifest is read by id or by namespace/name/version; checksums are verified on load.Capsule.to_func()→ restore policy tries source, then import path, then trusted-local binary payload.ToolandToolkitwrap Capsules for local calls, MCP serving, and programmatic tool exports.
module.qualname reference instead of failing generic JSON serialization.
6. Registry Records
Capsules are stored as normal HeavenBase entities in a hidden registry workspace:sys-capsule: active manifest row with identity, manifest payload, capabilities, fingerprints, andlast_reasonon overwrite
last_reason.
The default registry config is under heavenbase.capsule.registry in CM_HVNB. You can pass registry_config=... when a demo, test, or application should keep executable data in a dedicated file.
Most application code should prefer capsule.register(...) and Capsule.load(...). CapsuleRegistry remains available for advanced registry administration and inspection through hb.capsule_registry(config).
7. Trust Model
Capsules do not verify themselves against the current content of the original Python function. The registry checksum only verifies that a loaded Capsule payload still matches the hashes stored with that same manifest. It is a record-integrity check, not a code-safety proof. HeavenBase caches successful checksum checks in a thread-safe in-memory LRU cache (heavenbase.capsule.registry.trust.verify_cache_size), so a process verifies a Capsule payload once per cache key instead of recalculating hashes every time the Capsule is loaded or restored.
Use source-only Capsules by default. include_cloudpickle defaults to False; enable binary payloads only for reviewed local code that cannot be represented by source or import path. If source-only restore fails, HeavenBase reports that no trusted-local binary fallback is stored.
8. Demo
Run the math demo from the HeavenBase source checkout:add, mul, sub, div, mod, and fibonacci as Capsules, stores a Toolkit, loads it through the public API, and validates the MCP server with a FastMCP client.

