Metadata-Version: 2.4
Name: apcore
Version: 0.34.0
Summary: Build once, invoke by code or AI — with schema, ACL, approval, and audit enforced at every call
Author-email: apstack <tercel.yi@gmail.com>
License: Apache-2.0
Project-URL: Homepage, https://apstack.org
Project-URL: Documentation, https://apcore.apstack.org/
Project-URL: Repository, https://github.com/apstack/apcore/tree/main/python
Project-URL: Issues, https://github.com/apstack/apcore/issues
Project-URL: Changelog, https://github.com/apstack/apcore/blob/main/python/CHANGELOG.md
Keywords: ai,llm,framework,capability-framework,schema-driven,pydantic,middleware,observability,tracing,metrics,async,access-control,registry,trace-context
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pydantic<3,>=2.7
Requires-Dist: jsonschema<5,>=4.21
Requires-Dist: referencing<1,>=0.30
Requires-Dist: typing-extensions<5,>=4.7
Provides-Extra: config
Requires-Dist: pyyaml<7,>=6.0; extra == "config"
Provides-Extra: discovery
Requires-Dist: pyyaml<7,>=6.0; extra == "discovery"
Requires-Dist: watchdog>=3.0; extra == "discovery"
Provides-Extra: events
Requires-Dist: aiohttp>=3.9; extra == "events"
Provides-Extra: observability
Requires-Dist: opentelemetry-sdk<2,>=1.34; extra == "observability"
Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.34; extra == "observability"
Provides-Extra: all
Requires-Dist: apcore[config,discovery,events,observability]; extra == "all"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: apcore[config,discovery]; extra == "dev"
Requires-Dist: ruff<0.17,>=0.15.0; extra == "dev"
Requires-Dist: mypy>=1.0; extra == "dev"
Requires-Dist: apdev[dev]>=0.2.9; extra == "dev"
Dynamic: license-file

<div align="center">
  <img src="https://raw.githubusercontent.com/apstack/apcore/main/apcore-logo.svg" alt="apcore logo" width="200"/>
</div>

# apcore

![Python](https://img.shields.io/badge/python-3.11+-blue.svg)
![License](https://img.shields.io/badge/license-Apache%202.0-green.svg)
[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/12294/badge)](https://www.bestpractices.dev/projects/12294)

> **Define once. Govern every call.**
>
> An embeddable microframework for application capabilities that agents and code can call.

**apcore** is an AI-Perceivable module standard that makes every interface naturally perceivable and understandable by AI through enforced Schema definitions and behavioral annotations. It provides strict type safety, access control, middleware pipelines, and built-in observability — enabling you to define modules with structured input/output schemas that are easily consumed by both code and AI.

## Features

- **Schema-driven modules** -- Define input/output contracts using Pydantic models (with automatic validation) or plain JSON Schema dicts
- **Execution Pipeline** -- Context creation, call chain guard, ACL enforcement, approval gate, middleware before, validation, execution, output validation, middleware after, and return -- with step metadata (`match_modules`, `ignore_errors`, `pure`, `timeout_ms`) and YAML pipeline configuration
- **`@module` decorator** -- Turn plain functions into fully schema-aware modules with zero boilerplate
- **YAML bindings** -- Register modules declaratively without modifying source code
- **Access control (ACL)** -- Pattern-based, first-match-wins rules with wildcard support
- **Middleware system** -- Composable before/after hooks with error recovery
- **Observability** -- Tracing (spans), metrics collection, and structured context logging
- **Async support** -- Seamless sync and async module execution
- **Safety guards** -- Call depth limits, circular call detection, frequency throttling
- **Approval system** -- Pluggable approval gate (Step 5) with sync/async handlers, scoped Phase B resume, and audit events; fails closed by default (`strict=True`, `gate_destructive=True`) with execution-time `ExecutionPolicy` overrides (#76)
- **Extension points** -- Unified extension management for discoverers, middleware, ACL, approval handlers, policy evaluators, capability search providers, span exporters, and module validators
- **Async task management** -- Background module execution with status tracking, cancellation, and concurrency limiting
- **Behavioral annotations** -- Declare module traits (readonly, destructive, idempotent, cacheable, paginated, streaming) for AI-aware orchestration
- **W3C Trace Context** -- traceparent header injection/extraction for distributed tracing interop
- **Circuit breakers** -- `CircuitBreakerMiddleware` for per-`(module_id, caller_id)` OPEN/CLOSED/HALF_OPEN protection; `CircuitBreakerWrapper` (`from apcore.events.circuit_breaker import CircuitBreakerWrapper`) for per-subscriber event delivery resilience
- **Multi-class module discovery** -- Opt-in `@multi_class` decorator (`from apcore.registry.multi_class import multi_class`) for multiple Module classes per file with snake_case ID derivation (PROTOCOL_SPEC §2.1.1)
- **Pluggable stores** -- `TaskStore` for async task persistence; `ObservabilityStore` for error/metric backends; `AuditStore` for control-module audit trails; `ArtifactStore` for bounded host-owned artifact reads
- **Prometheus text rendering** -- `render_prometheus_metrics` (`from apcore.observability import render_prometheus_metrics`) renders `MetricsCollector` and `UsageCollector` data in Prometheus exposition format (HTTP serving lives in `apcore-toolkit`)

## API Overview

**Core**

| Class | Description |
|-------|-------------|
| `APCore` | High-level client -- register modules, call, stream, validate |
| `Registry` | Module storage -- discover, register, get, list |
| `Executor` | Execution engine -- call with middleware pipeline, ACL, approval |
| `Context` | Request context -- trace ID, identity, call chain, cancel token |
| `Config` | Configuration -- load from YAML, get/set values, namespace-partitioned Config Bus |
| `Identity` | Caller identity -- id, type, roles, attributes |
| `FunctionModule` | Wrapped function module created by `@module` decorator |

**Access Control & Approval**

| Class | Description |
|-------|-------------|
| `ACL` | Access control -- rule-based caller/target authorization |
| `ApprovalHandler` | Pluggable approval gate protocol |
| `AlwaysDenyHandler` / `AutoApproveHandler` / `CallbackApprovalHandler` | Built-in approval handlers |
| `ExecutionPolicy` / `PolicyRule` | Execution-time governance overrides (#76) -- force/exempt approval, `gate_destructive`, `strict` fail-closed |

**Middleware**

| Class | Description |
|-------|-------------|
| `Middleware` | Pipeline hooks -- before/after/on_error interception |
| `BeforeMiddleware` / `AfterMiddleware` | Single-phase middleware adapters |
| `LoggingMiddleware` | Structured logging middleware |
| `RetryMiddleware` | Automatic retry with backoff |
| `ErrorHistoryMiddleware` | Records errors into ErrorHistory |
| `PlatformNotifyMiddleware` | Emits events on error rate/latency spikes |
| `ObsLoggingMiddleware` | Observability-aware structured logging middleware |
| `CircuitBreakerMiddleware` | Circuit breaker keyed on `(module_id, caller_id)` (OPEN/CLOSED/HALF_OPEN) |

**Schema**

| Class | Description |
|-------|-------------|
| `SchemaLoader` | Load schemas from YAML or native types |
| `SchemaValidator` | Validate data against schemas |
| `SchemaExporter` | Generic JSON Schema export and strict conversion (`export_generic`, `to_strict_schema`); provider-specific projections (MCP, OpenAI, Anthropic) live in Surfaces |
| `RefResolver` | Resolve `$ref` references in JSON Schema |

**Observability**

| Class | Description |
|-------|-------------|
| `TracingMiddleware` | Distributed tracing with span export |
| `BatchSpanProcessor` | Non-blocking OTEL span export with configurable queue (`from apcore.observability import BatchSpanProcessor`) |
| `MetricsMiddleware` / `MetricsCollector` | Call count, latency, error rate metrics |
| `ContextLogger` | Context-aware structured logging with `RedactionConfig` |
| `ErrorHistory` | Min-heap error ring buffer with SHA-256 fingerprint deduplication |
| `UsageCollector` | Per-module usage statistics, trends, and Prometheus export |
| `UsageMiddleware` | Per-call usage tracking middleware |
| `TraceContext` | W3C Trace Context propagation (traceparent/tracestate) |
| `InMemoryExporter` | Span exporter that stores spans in memory |
| `StdoutExporter` | Span exporter that writes spans to stdout |
| `OTLPExporter` | Span exporter using OpenTelemetry Protocol |
| `render_prometheus_metrics` | Renders Prometheus exposition text from `MetricsCollector` and optional `UsageCollector` (`from apcore.observability import render_prometheus_metrics`); use `apcore-toolkit` `create_prometheus_server` for HTTP serving |

**Events & Extensions**

| Class | Description |
|-------|-------------|
| `EventEmitter` | Event system -- subscribe, emit, flush |
| `WebhookSubscriber` | Built-in webhook event subscriber (A2A event delivery lives in `apcore-a2a`) |
| `ExtensionManager` | Unified extension point management |
| `AsyncTaskManager` | Background module execution with pluggable `TaskStore`, retry, and reaper |
| `TaskStore` / `InMemoryTaskStore` | Pluggable async task persistence backend |
| `RetryPolicy` / `BackoffStrategy` | **Deprecated** — per-task retry configuration. Use `RetryConfig` for cross-language field-name parity. |
| `CancelToken` | Cooperative cancellation token |
| `BindingLoader` | Load modules from YAML binding files |
| `ErrorCodeRegistry` | Central registry for structured error codes |
| `ErrorFormatterRegistry` | Surface-specific error formatter registry (MCP, A2A, CLI adapters) |

**Name canonicalization**

`canonicalize_name()` converts a bare ASCII name into one canonical segment,
retaining the original for display and audit. It rejects Unicode rather than
dropping or transliterating characters and returns a diagnostic instead of
throwing for invalid strings. `normalize_to_canonical_id()` remains the
non-repairing A02 module-ID converter. The name `system` is a valid segment;
reserved namespace enforcement and name collisions belong to registration
and scanning, not this utility.

```python
from apcore import canonicalize_name

source_name = " HTTP / UserProfile "
result = canonicalize_name(source_name)
if result.error is None:
    print({"original_name": result.original_name, "canonical_name": result.canonical_name})
else:
    print({"original_name": result.original_name, "error": result.error.value})
```

## Cross-Language Parity Notes

The Python, TypeScript, and Rust SDKs share contracts for background tasks,
extensions, circuit breaking, Prometheus metrics, and error recovery. Their
`AsyncTaskManager` accepts a pluggable `TaskStore` and ships with an in-memory
backend.

Each SDK withholds module-level preflight and preview when the ACL denies
access. The result includes the failed ACL check. A permitted call can receive
module-level preflight and preview even if schema validation fails.

See the [Python changelog](./CHANGELOG.md) for release history.

Intentional cross-language differences (e.g. Python's synchronous `call()` ergonomics,
the legacy/deprecated `RetryPolicy`/`BackoffStrategy` aliases that exist only in Python,
and the module-level convenience functions) are documented inline in the relevant feature
specs as "Cross-language note" admonitions in the canonical protocol repo
([apcore/docs/features](https://github.com/apstack/apcore/tree/main/docs/features)).
Anything that differs across SDKs without such a note is drift, not a feature.

## Configuration

### Config Bus and Namespace Registration

`Config` doubles as an ecosystem-level Config Bus. Any package can register a named namespace with optional JSON Schema validation, env prefix, and default values:

```python
from apcore import Config

# Register a namespace (class-level, shared across all Config instances)
Config.register_namespace(
    "my_plugin",
    schema={"type": "object", "properties": {"timeout_ms": {"type": "integer"}}},
    env_prefix="MY_PLUGIN__",
    defaults={"timeout_ms": 5000},
)

# Load config as usual
config = Config.load("project.yaml")

# Namespace-aware access
timeout = config.get("my_plugin.timeout_ms")   # dot-path with namespace resolution
subtree = config.namespace("my_plugin")          # full subtree as dict

# Typed access
config.get_typed("my_plugin.timeout_ms", int)

# Mount an external source (no unified YAML required)
config.mount("my_plugin", from_dict={"timeout_ms": 3000})

# Introspect registered namespaces
names = Config.registered_namespaces()
```

### Built-in Namespaces

apcore pre-registers three namespaces that promote its existing flat config keys:

| Namespace | Env prefix | Keys |
|-----------|-----------|------|
| `observability` | `APCORE_OBSERVABILITY` | tracing, metrics, logging, error_history, platform_notify |
| `obs` | `APCORE_OBS` | redaction.regex_patterns, redaction.sensitive_keys, redaction.replacement |
| `sys_modules` | `APCORE_SYS` | events.thresholds.error_rate, events.thresholds.latency_p99_ms |

### Environment Variable Conventions

| Pattern | When to use | Example |
|---------|------------|---------|
| `APCORE_KEY_NAME` | Override a flat top-level apcore key (existing convention) | `APCORE_EXECUTOR_DEFAULT__TIMEOUT=5000` |
| `APCORE_NAMESPACE` prefix | Override keys inside a registered namespace | `APCORE_OBSERVABILITY_TRACING_ENABLED=true` |
| Custom prefix declared in `register_namespace` | Third-party packages with their own prefix | `MY_PLUGIN__TIMEOUT_MS=3000` |

The longest-prefix-match dispatch algorithm ensures that `APCORE_OBSERVABILITY_TRACING_ENABLED` routes to the `observability` namespace (not to a core flat key). Within each namespace, a single `_` maps to `.` and `__` maps to a literal `_`.

### New Error Codes (0.15.0)

| Code | Meaning |
|------|---------|
| `CONFIG_NAMESPACE_DUPLICATE` | A namespace with this name is already registered |
| `CONFIG_NAMESPACE_RESERVED` | The namespace name is reserved (`_config`, `apcore`) |
| `CONFIG_ENV_PREFIX_CONFLICT` | Two namespaces share the same env prefix |
| `CONFIG_MOUNT_ERROR` | Failed to load or parse a mounted config source |
| `CONFIG_BIND_ERROR` | Failed to deserialize a namespace subtree into the requested type |
| `ERROR_FORMATTER_DUPLICATE` | A formatter for this surface is already registered |

### Event Type Names

Canonical event type names use dot-namespaced identifiers. `apcore.*` is reserved for core framework events; adapter packages use their own prefix (e.g., `apcore-mcp.*`).

| Canonical name | Emitted by |
|---------------|-----------|
| `apcore.module.toggled` | `system.control.toggle_feature` |
| `apcore.health.recovered` | `PlatformNotifyMiddleware` (error rate recovery) |
| `apcore.config.updated` | `system.control.update_config` |
| `apcore.module.reloaded` | `system.control.reload_module` |

> **v0.18.0 — legacy aliases removed.** Listeners that previously subscribed to
> `module_health_changed` or `config_changed` will no longer receive events.
> Migrate subscriptions to the canonical names above.

## Documentation

For full documentation, including Quick Start guides and runtime profiles, visit:
**[https://apcore.apstack.org/getting-started/](https://apcore.apstack.org/getting-started/)**

## Requirements

- Python >= 3.11

## Installation and Runtime Profiles

Install the base package for the in-memory Core profile:

```bash
pip install apcore
```

Install `apcore[all]` for the full host assembly (`apcore.full`), or select individual extras (`config`, `discovery`, `observability`, `events`):

```bash
pip install "apcore[all]"
```

```python
from apcore.full import APCore, Config

client = APCore(config=Config.load("apcore.yaml"))
client.discover()
```

The default approval gate rejects approval-required calls when no `ApprovalHandler` is configured and requires approval for `destructive` modules by default. See the [Runtime Profiles guide](https://apcore.apstack.org/guides/runtime-profiles/).

### Development

```bash
pip install -e ".[dev]"
```

## Quick Start

### Simple usage (Global Client)

For simple scripts or prototypes, you can use the global `apcore` functions:

```python
import apcore

@apcore.module(id="math.add", description="Add two integers")
def add(a: int, b: int) -> int:
    return a + b

# Directly call it
result = apcore.call("math.add", {"a": 10, "b": 5})
print(result)  # {'result': 15}
```

### Simplified Client (Recommended)

The `APCore` client provides a unified entry point that manages everything for you:

```python
from apcore import APCore

client = APCore()

@client.module(id="math.add", description="Add two integers")
def add(a: int, b: int) -> int:
    return a + b

# Call the module
result = client.call("math.add", {"a": 10, "b": 5})
print(result)  # {'result': 15}
```

### Advanced: Define a module with a class

```python
from pydantic import BaseModel
from apcore import Context, APCore

client = APCore()

class GreetInput(BaseModel):
    name: str

class GreetOutput(BaseModel):
    message: str

class GreetModule:
    input_schema = GreetInput
    output_schema = GreetOutput
    description = "Greet a user"

    def execute(self, inputs: dict, context: Context) -> dict:
        return {"message": f"Hello, {inputs['name']}!"}

client.register("greet", GreetModule())
result = client.call("greet", {"name": "Alice"})
# {"message": "Hello, Alice!"}
```

### Alternative: Define schemas with plain dicts

If you prefer not to use Pydantic, pass raw JSON Schema dicts directly:

```python
from apcore import APCore

client = APCore()

class WeatherModule:
    input_schema = {"type": "object", "properties": {"city": {"type": "string"}}}
    output_schema = {"type": "object", "properties": {"temp": {"type": "number"}}}
    description = "Get current temperature"

    def execute(self, inputs: dict, context=None) -> dict:
        return {"temp": 22.5}

client.register("weather", WeatherModule())
result = client.call("weather", {"city": "Tokyo"})
# {"temp": 22.5}
```

> **Note:** Dict schemas are strictly validated against JSON Schema Draft 2020-12 at the module boundary with the same `SCHEMA_VALIDATION_ERROR` wire contract as Pydantic models. Use Pydantic models when you want typed Python model ergonomics.

### Add middleware

```python
from apcore import LoggingMiddleware, TracingMiddleware

client.use(LoggingMiddleware())
client.use(TracingMiddleware())
```


### Access control

```python
from apcore import ACL, ACLRule, Executor, Registry

registry = Registry()
acl = ACL(rules=[
    ACLRule(callers=["admin.*"], targets=["*"], effect="allow", description="Admins can call anything"),
    ACLRule(callers=["*"], targets=["admin.*"], effect="deny", description="Others cannot call admin modules"),
])
executor = Executor(registry=registry, acl=acl)
```

## Examples

The `examples/` directory contains runnable demos:

---

### `simple_client` — APCore client with decorator-based modules

Initializes an `APCore` client, registers modules with `@client.module()`, and calls them directly.

```python
from apcore import APCore

client = APCore()

@client.module(id="math.add", description="Add two integers")
def add(a: int, b: int) -> int:
    return a + b

result = client.call("math.add", {"a": 10, "b": 5})
print(result)  # {'result': 15}

@client.module(id="greet")
def greet(name: str, greeting: str = "Hello") -> dict:
    return {"message": f"{greeting}, {name}!"}

result = client.call("greet", {"name": "Alice"})
print(result)  # {'message': 'Hello, Alice!'}
```

---

### `global_client` — Minimal global client usage

No explicit initialization needed — use the default global client directly.

```python
import apcore

@apcore.module(id="math.add")
def add(a: int, b: int) -> int:
    return a + b

result = apcore.call("math.add", {"a": 10, "b": 5})
print(result)  # {'result': 15}
```

---

### `greet` — Duck-typed module with Pydantic schemas

Demonstrates the class-based module interface with Pydantic `BaseModel` for input/output schemas.

```python
from pydantic import BaseModel

class GreetInput(BaseModel):
    name: str

class GreetOutput(BaseModel):
    message: str

class GreetModule:
    input_schema = GreetInput
    output_schema = GreetOutput
    description = "Greet a user by name"

    def execute(self, inputs: dict, context) -> dict:
        name = inputs["name"]
        return {"message": f"Hello, {name}!"}
```

---

### `get_user` — Readonly module with `ModuleAnnotations`

Demonstrates behavioral annotations (`readonly`, `idempotent`) and simulated database lookup.

```python
from pydantic import BaseModel
from apcore.module import ModuleAnnotations

class GetUserInput(BaseModel):
    user_id: str

class GetUserOutput(BaseModel):
    id: str
    name: str
    email: str

class GetUserModule:
    input_schema = GetUserInput
    output_schema = GetUserOutput
    description = "Get user details by ID"
    annotations = ModuleAnnotations(readonly=True, idempotent=True)

    _users = {
        "user-1": {"id": "user-1", "name": "Alice", "email": "alice@example.com"},
        "user-2": {"id": "user-2", "name": "Bob", "email": "bob@example.com"},
    }

    def execute(self, inputs: dict, context) -> dict:
        user_id = inputs["user_id"]
        user = self._users.get(user_id)
        if user is None:
            return {"id": user_id, "name": "Unknown", "email": "unknown@example.com"}
        return dict(user)
```

---

### `send_email` — Destructive module with sensitive fields and ContextLogger

Shows `x-sensitive` on schema fields (for log redaction), `ModuleAnnotations` with metadata, `ModuleExample` for AI-perceivable documentation, and `ContextLogger` usage. Schema redaction follows local JSON pointers, anchors, and self references through object, array, and composition schemas, including positional prefixes, tuple items, and their tail schemas; recursive references are evaluated alongside the data without schema expansion or external retrieval.

```python
from pydantic import BaseModel, Field
from apcore.module import ModuleAnnotations, ModuleExample
from apcore.observability import ContextLogger

class SendEmailInput(BaseModel):
    to: str
    subject: str
    body: str
    api_key: str = Field(..., json_schema_extra={"x-sensitive": True})

class SendEmailOutput(BaseModel):
    status: str
    message_id: str

class SendEmailModule:
    input_schema = SendEmailInput
    output_schema = SendEmailOutput
    description = "Send an email message"
    tags = ["email", "communication", "external"]
    version = "1.2.0"
    metadata = {"provider": "example-smtp", "max_retries": 3}
    annotations = ModuleAnnotations(destructive=True, idempotent=False, open_world=True)
    examples = [
        ModuleExample(
            title="Send a welcome email",
            inputs={"to": "user@example.com", "subject": "Welcome!", "body": "...", "api_key": "sk-xxx"},
            output={"status": "sent", "message_id": "msg-12345"},
            description="Sends a welcome email to a new user.",
        ),
    ]

    def execute(self, inputs: dict, context) -> dict:
        logger = ContextLogger.from_context(context, name="send_email")
        logger.info("Sending email", extra={"to": inputs["to"], "subject": inputs["subject"]})
        message_id = f"msg-{hash(inputs['to']) % 100000:05d}"
        logger.info("Email sent successfully", extra={"message_id": message_id})
        return {"status": "sent", "message_id": message_id}
```

---

### `decorated_add` — `@module` decorator for simple functions

```python
from apcore.decorator import module

@module(description="Add two integers", tags=["math", "utility"])
def add(a: int, b: int) -> int:
    return a + b
```

## Development

### Run tests

```bash
pytest
```

### Run tests with coverage

```bash
pytest --cov=src/apcore --cov-report=html
```

### Lint and format

```bash
ruff check --fix src/ tests/
ruff format src/ tests/
```

### Type check

```bash
mypy src/ tests/
```


## 📄 License

Apache-2.0

## 🔗 Links

- **Documentation**: [docs/apcore](https://github.com/apstack/apcore) - Complete documentation
- **Website**: [apstack.org](https://apstack.org)
- **GitHub**: [Python SDK source](https://github.com/apstack/apcore/tree/main/python)
- **PyPI**: [apcore](https://pypi.org/project/apcore/)
- **Issues**: [GitHub Issues](https://github.com/apstack/apcore/issues)
- **Discussions**: [GitHub Discussions](https://github.com/apstack/apcore/discussions)
