Files
cleveragents-core/docs/api/resource.md
T
HAL9000 18d00c04c4
CI / lint (pull_request) Failing after 1m15s
CI / quality (pull_request) Successful in 1m21s
CI / typecheck (pull_request) Successful in 1m34s
CI / security (pull_request) Successful in 1m37s
CI / coverage (pull_request) Has been skipped
CI / unit_tests (pull_request) Failing after 1m37s
CI / docker (pull_request) Has been skipped
CI / build (pull_request) Successful in 33s
CI / helm (pull_request) Successful in 26s
CI / push-validation (pull_request) Successful in 19s
CI / e2e_tests (pull_request) Successful in 3m20s
CI / integration_tests (pull_request) Successful in 4m32s
CI / status-check (pull_request) Failing after 3s
fix(skills): implement multi-scope agent skill discovery for global, project, and local tiers
Implements AgentSkillDiscovery class to support discovering Agent Skills from
multiple configured directories across three scopes (global, project, local).
Handles name collisions with precedence: local > project > global.

Adds comprehensive BDD test coverage for multi-scope discovery scenarios including:
- Global-only, project-only, and local-only discovery
- Combined discovery from all scopes
- Name collision resolution with proper precedence
- Non-existent and empty scope directory handling
- Multiple skills in same scope discovery

ISSUES CLOSED: #9369
2026-05-06 19:55:22 +00:00

5.1 KiB

cleveragents.resource — Resource System

The resource package provides the resource type schema, handler infrastructure, and the type-inheritance resolution system. Resources are the managed external entities (files, databases, containers, LSP servers, cloud services) that plans read from and write to.

See ADR-008, ADR-036, and ADR-042 for design rationale.


Type Inheritance

resolve_inheritance_chain(type_name, registry) → list[str]

Returns the full inheritance chain for a resource type, from the type itself up to the root. Raises ResourceTypeCircularInheritanceError if a cycle is detected, or ResourceTypeInheritanceDepthError if the chain exceeds MAX_CHAIN_DEPTH (default: 16).

from cleveragents.resource import resolve_inheritance_chain

chain = resolve_inheritance_chain("container.docker", registry)
# → ["container.docker", "container", "resource"]

resolve_fields(type_name, registry) → dict[str, Any]

Merges field definitions from the full inheritance chain, with child fields overriding parent fields.

is_subtype_of(child, parent, registry) → bool

Returns True if child is a subtype of parent.

from cleveragents.resource import is_subtype_of

is_subtype_of("container.docker", "container", registry)  # True

find_subtypes(parent, registry) → list[str]

Returns all registered types that are subtypes of parent.

validate_chain(chain) → None

Validates a pre-computed inheritance chain for cycles and depth.

TypeRegistryMap

Type alias: dict[str, ResourceTypeDefinition]. The registry maps type names to their definitions.


Inheritance Errors

Exception Description
ResourceTypeCircularInheritanceError Cycle detected in type hierarchy
ResourceTypeInheritanceDepthError Chain exceeds MAX_CHAIN_DEPTH
ResourceTypeParentNotFoundError Parent type not registered
ResourceTypeParentRemovalError Attempt to remove a type that has children

Constants

Constant Value Description
MAX_CHAIN_DEPTH 16 Maximum inheritance chain depth

Resource Handlers

Resource handlers live in cleveragents.resource.handlers and implement CRUD, checkpoint, and rollback operations for specific resource types. Built-in handlers include:

Handler Resource Types
Database handler sqlite, postgresql, mysql, duckdb
Container handler container.docker, container.podman
LSP handler lsp.*
File handler file, directory
Cloud handler cloud.*

Each handler implements the ResourceHandler protocol:

class ResourceHandler(Protocol):
    async def read(self, resource_id: str, context: ...) -> Any: ...
    async def write(self, resource_id: str, data: Any, context: ...) -> None: ...
    async def delete(self, resource_id: str, context: ...) -> None: ...
    async def list_children(self, resource_id: str, context: ...) -> list[str]: ...
    async def diff(self, resource_id: str, other_id: str, context: ...) -> str: ...
    async def checkpoint(self, resource_id: str) -> str: ...
    async def rollback(self, resource_id: str, checkpoint_id: str) -> None: ...
    async def create_sandbox(self, resource_id: str) -> "Sandbox": ...

DatabaseResourceHandler

Full CRUD and checkpoint/rollback for SQLite and remote databases.

Method SQLite PostgreSQL / MySQL / DuckDB
read() Queries sqlite_master for schema Not supported (returns graceful failure)
write(sql) Executes SQL statement Not supported
delete() DROP TABLE IF EXISTS Not supported
list_children() Lists tables and views Not supported
diff() Content-hash schema comparison Not supported
create_checkpoint() SAVEPOINT <name> Not supported
rollback_to() ROLLBACK TO SAVEPOINT <name> Not supported

Remote database operations return a structured not-supported result rather than raising.

from cleveragents.resource.handlers import DatabaseResourceHandler

handler = DatabaseResourceHandler()
checkpoint_id = await handler.checkpoint("my-db")
await handler.rollback("my-db", checkpoint_id)

DevcontainerHandler

Full protocol implementation for container.devcontainer resources.

Method Implementation
delete() devcontainer exec rm -rf <path>
list_children() devcontainer exec ls -1 <path>
diff() Content-hash comparison of resource state
create_sandbox() Delegates to BaseResourceHandler with lazy activation

All methods return graceful failure results for missing or stopped containers rather than raising exceptions.

from cleveragents.resource.handlers import DevcontainerHandler

handler = DevcontainerHandler()
children = await handler.list_children("my-devcontainer:/workspace")