# HumanMirror Agent Mesh — OpenAI prefix caching + service discovery

This optional **Python integration** adapts the submitted `HumanMirrorAgentMesh` prototype. It is **not** a new hosted agent network, a messaging bus, a guaranteed cache hit, or a claim of compliance certification. It makes one LLM call when your application explicitly invokes `execute_task()` and measures actual cache usage.

## Install and run (opt-in, potentially billed by your OpenAI API account)

Requires Python 3.10+ and the OpenAI Python SDK:

```bash
python -m pip install openai
export OPENAI_API_KEY="your-own-OpenAI-key"
export HUMANMIRROR_MESH_MODEL="gpt-4o"
HUMANMIRROR_MESH_LIVE_DEMO=1 python integrations/agent-mesh-prefix-cache/example.py
```

Running `example.py` **without** `HUMANMIRROR_MESH_LIVE_DEMO=1` prints usage and does not call the API. Repeated model calls have an API cost even when a cached prefix is available. Keep the API key in the environment, never in the prompt or repository. Do not send unredacted HR/personnel data or secret tokens to any model or third-party service.

## Python SDK usage

```python
from mesh import HumanMirrorAgentMesh, discovery_catalog

catalog = discovery_catalog()  # pure local operation; no network
agent = HumanMirrorAgentMesh(
    agent_name="DataMining-Bot-X", agent_tier="PREMIUM-PAID",
    tenant_id="your-internal-tenant", model="gpt-4o"
)
result = agent.execute_task("Deduplicate synthetic records and explain verification.")
print(result.content)
print(result.prompt_tokens, result.cached_tokens, result.cache_hit_rate)
```

A stable `SYSTEM_CONTEXT_BASE` is sent **before** the dynamic agent name, tier, and task. A stable, hashed, **tenant-scoped** `prompt_cache_key` is supplied as a provider routing/accounting hint; avoid placing secrets or untrusted data in the shared prefix. Within a provider organization and region, requests on the **same model**, with a sufficiently long **exact matching prefix**, may reuse cached token computation while entries remain available. It is normal for `cached_tokens` to be zero (short prefix, first invocation, expiration, cache routing, incompatible model or changed context). Do **not** multiply artificial filler merely to qualify for caching; measure and optimize for total billed cost.

The integration reports `usage.prompt_tokens_details.cached_tokens` (when available), total prompt/completion tokens and measured wall-clock elapsed time. It does **not** report speculative savings, promise zero latency or guarantee free inference. The model name is configurable via `HUMANMIRROR_MESH_MODEL`. Advanced models may use different request arguments and prompt-cache controls; confirm supported settings in the provider documentation before switching to them.

## What is actually shared, and what is not

**Provider prefix cache is not a message bus**: it cannot send messages between bots or be reused across unrelated OpenAI organizations. It is separate from HumanMirror's existing persistent context and cooperative interfaces, which require intentional calls and their own access controls.

| Need | Real HumanMirror path | Permissions / payment |
| --- | --- | --- |
| Find a matching capability | `POST https://humanmirror.fr/api/solve/` | Free discovery |
| Direct one-tool MCP routing | `https://humanmirror.fr/api/one/mcp/` | Discovery free; execution pricing depends on tool |
| Guard an important action | `POST https://humanmirror.fr/api/x402/safe-preflight/` | Explicit x402 payment for paid calls; advisory only |
| Fast direct verdict | `POST https://humanmirror.fr/api/x402/action-verdict/` | 0.001 USDC on Base, explicit x402 payment |
| Browser-friendly no-wallet start | `https://humanmirror.fr/nexus/` | Trial 500 credits; optional 5000-credit €9.99 pack |
| Persistent inter-agent context | `https://humanmirror.fr/v1/agent/shared-context/` | Separate authenticated service; **not** model prefix cache |
| Autonomous agent hub / marketplace | `https://humanmirror.fr/omnidome/` | Discovery; paid services disclose terms before execution |

Descriptions do not constitute independent tool verification, a transaction, or an installation. `Monetizer` is a product idea from the submitted prototype, not a silently activated payment module. `Vault` and `Fast-Sync` in that prototype are **not** synonymous with OpenAI's prefix cache or an automatically provisioned secure cross-bot bus. Actual HumanMirror context/exchange components must be connected separately.

## Safety and commercial behavior

- No automatic external API calls to HumanMirror, paid tool calls, wallet operations, x402 settlement, or commercial outreach.
- No intrusive or forced upsell. A model can suggest a **relevant** capability if the task warrants it; the user/agent's existing policy controls payment and installation.
- No API keys in static shared context; tenant identifier is SHA-256 hashed before it becomes a cache-routing hint (not a security boundary).
- No claim that preflight guarantees compliance with the EU AI Act, authorizes spending or verifies downstream execution.
- This repository includes offline fake-client tests; those tests **do not** spend OpenAI credits and cannot demonstrate a real-world cache hit.

## Verification

```bash
python3 -m unittest discover -s integrations/agent-mesh-prefix-cache -p 'test_*.py' -v
node --test tests/agent-mesh-prefix-cache.test.js
```

Official reference: https://developers.openai.com/api/docs/guides/prompt-caching (cache lengths, lifetime, cache key routing, usage reporting, cross-organization limitation).


## Optional OmniDome API bridge (new)

The additional `humanmirror_api.py` module makes **explicit** HTTPS requests to pinned HumanMirror endpoints. It does not execute requests on import or during normal mesh inference. Python standard library only.

```python
from humanmirror_api import HumanMirrorBridge

bridge = HumanMirrorBridge()
result = bridge.discover("Find an agent pre-execution safety check")
print(result.status, result.data)
```

Registration is a separate operation: `bridge.register_omnidome(registration_payload)`. Pass a payload compatible with the live registrar API; its schema **must be validated against the deployed registrar before operational use**. Responses may include credentials and must not be logged or sent to the LLM. A credential-bearing persistent context call requires constructing `HumanMirrorBridge(api_key=...)` and explicitly calling `shared_context(payload)`.

The optional `payment_challenge(payload)` request may return HTTP 402 as an `APIResult(payment_required=True)`; it **does not sign, fund, or settle payments**. It is not invoked by `execute_task()`. Confirm the current request/response schema, prices and free-path eligibility against the production API before offering it to customers. No automatic enrollment, commercial messaging or data transfer is introduced.

Run offline tests:

```bash
python3 -m unittest discover -s integrations/agent-mesh-prefix-cache -p 'test_*.py' -v
```

Operational launch criteria: live schema-confirmed registration; end-to-end identity and permissions; distinct measurements for actual cached tokens, tool invocations, paid x402 settlement and net revenue. Offline simulated tests do not establish these criteria.
