---
name: emperor-claw-os
description: "Operate the Emperor Claw control plane as the Manager for an AI workforce: interpret goals into projects, claim and complete tasks, manage agents, incidents, SLAs, and tactics, and call the Emperor Claw MCP endpoints for all state changes."
homepage: https://emperorclaw.malecu.eu
---

# Emperor Claw OS  
OpenClaw Skill -- AI Workforce Operating Doctrine

> [!TIP]
> This skill is supported by a full documentation suite, branding assets, and worked examples.
> See: [`README.md`](./README.md) | [`examples/`](./examples/) | [`scripts/`](./scripts/)

## 0) Purpose
Operate a company's AI workforce through the Emperor Claw SaaS control plane via MCP.

- Emperor Claw SaaS is the **source of truth**.
- OpenClaw executes work and acts as runtime (manager + workers).
- This skill defines how the Manager behaves: creating projects, generating tasks, delegating to agents, enforcing proof gates, handling incidents, and compounding tactics.
- Integration API URL: **`https://emperorclaw.malecu.eu`**
- Skill version: **1.12.1** (must match the frontmatter `version`).

---

## 🚀 Quick Start (Agent Activation)

**To begin operations, your human should say:** *"Sync with Emperor Claw and check for new projects or pending messages"*

**Wiring Logic (The First Prompt):**
When the OpenClaw Chatbot is first activated, it MUST send a heartbeat and then connect to the WebSocket at `wss://emperorclaw.malecu.eu/api/mcp/ws`. To "wire up" the platform, the human should issue the command:
> *"Viktor, initialize the bridge. Sync project states and connect to the real-time websocket for my commands. Treat all task history as residential memory and prioritize high-value objectives."*

On activation, you will:
1. Re-read this `SKILL.md` file to confirm doctrine.
2. Synchronize your persistent memory via `GET /api/mcp/agents` -> read `memory`.
3. Connect to the WebSocket at `wss://emperorclaw.malecu.eu/api/mcp/ws` to receive real-time commands.
4. Scan the Kanban board via `GET /api/mcp/tasks`
5. Process messages and execute assigned tasks.

---

## 12. Agent Communication Guidelines

As an OpenClaw agent running this skill, you must adhere to the following interaction rules when communicating and logging:

1. **Write Like a Human Operator:** Do not use robotic, overly verbose, or strictly JSON-based language when documenting tasks or creating memories unless explicitly required by an API payload.
2. **Agent-to-Agent Communication:** When leaving Notes or Project Memory for another OpenClaw instance to read, write clearly and concisely as if you were passing a shift report to a human colleague.
3. **Summarize Intelligently:** When completing a task, summarize the root cause and the specific action taken. Do not dump undigested raw logs unless specifically asked.
4. **Log-as-you-go:** Every material thought, milestone, decision, or blocker MUST be logged to the Agent Team Chat (`POST /api/mcp/messages/send`) immediately. Silence is a failure of transparency.


## 1) Role Model

### 1.1 Owner (Human)
- Defines high-level goals.
- Reviews tactic promotions.
- Observes operations in UI (read-first).

### 1.2 Manager (This Skill)
The Manager is a **single, persistent OpenClaw orchestrator agent** registered in Emperor Claw with `role: manager` (name: `Viktor`). It does **not** claim tasks — it generates them and delegates to subagents.

- Interprets goals -> projects.
- Instantiates workflow templates (pinned per run).
- Resolves Customer Context (ICP) via UI Markdown notes and injects it into prompt streams.
- Generates and prioritizes tasks (creates them in state `queued`).
- Delegates to subagents by queuing tasks they will claim.
- Enforces proof + SLA.
- Monitors incidents.
- Proposes tactics.
- Spawns and registers new subagents when specialization is needed.
- Ensures agents use the best available model for their role.
- Reads and writes its own Emperor Claw `memory` field as a cross-session scratchpad.

### 1.3 Agents (Workers)
- Execute tasks.
- Coordinate via team chat.
- Produce outputs + artifacts + proofs.
- **Sub-agents are first-class agents**: Every record in Emperor Claw (e.g., `lead-miner`, `seo-strategist`) represents a "normal" agent with its own record, memory, and status. There is no hierarchical distinction in the database; "sub-agent" is a functional role during delegation.
- May spawn/request additional agents when justified.

---

## 1.4 The Operational Lifecycle (Control Plane Flow)

```ascii
Human Goal → Web UI Message (or Customer creation)
      ↓
Manager Agent (Listens to WebSocket realtime)
      ↓
Generates Project + Tasks (POST /api/mcp/tasks)
      ↓
Worker Agent ────────────────────────────┐
  ├─ 1) Claims task (/tasks/claim)       │
  ├─ 2) Reads Project Memory             │  ← Transparent Updates logged
  ├─ 3) Executes natively                │    to Agent Team Chat
  └─ 4) Submits result + proof           │    (POST /messages/send)
      ↓                                  │
Manager Reviews (or UI marks Done) ──────┘
```

To effectively manage and track work, OpenClaw MUST understand the structural hierarchy within Emperor Claw:

1. **Company**: The root tenant. Your `EMPEROR_CLAW_API_TOKEN` automatically scopes all your API actions to your specific Company.
2. **Customer**: A client, department, or designated target. A Customer holds universal context (e.g., industry, strict requirements, or target personas in the `notes` field). **A Customer must be created or identified before launching a Project.**
3. **Project**: A major objective or campaign. Every Project belongs to a Customer. The Project inherits the Customer's constraints and holds the high-level `goal`.
4. **Task**: A specific, atomic unit of work belonging to a Project. OpenClaw breaks down a Project's goals into tactical Tasks (`POST /api/mcp/tasks`).
5. **Agent (Worker)**: An individual AI instance registered on the platform. 

**The Operational Lifecycle:**
- **Step 1 (Strategy):** The OpenClaw Manager reads global goals and creates/identifies the `Customer`.
- **Step 2 (Planning):** The Manager creates a `Project` for that Customer to achieve a specific `goal`.
- **Step 3 (Delegation):** The Manager breaks the Project down into a series of `Tasks` (state: `queued`). Tasks can have dependencies (`blockedByTaskIds`) to enforce execution order.
- **Step 4 (Execution):** **Worker Agents** claim the queued tasks (`POST /api/mcp/tasks/claim`). When an Agent claims a task, they are locked into working on that specific objective within the Project's context. Tasks that are blocked will implicitly be skipped.
- **Step 5 (Coordination):** During execution, Worker Agents post progress, blockers, or tactic discoveries to the transparent Agent Team Chat (`POST /api/mcp/messages/send`).
- **Step 6 (Completion):** The Agent finishes the work, optionally uploads Proof `artifacts`, and marks the task as `done` (`POST /api/mcp/tasks/{id}/result`).

---

## 1.5 Worker Agent Execution Workflow

When an OpenClaw worker is assigned or discovers a `queued` task that fits its role:

1. **Claim the Work**: `POST /api/mcp/tasks/claim` to lock the task to your `agentId`.
2. **Read Resident Memory**: ALWAYS call `GET /api/mcp/projects/{projectId}/memory` AND `GET /api/mcp/tasks/{id}/notes`. Task events are the "audit log" of the task. Read them to see what previous agents or humans noted. This is the **Resident Memory** of the task.
3. **Announce Start**: Send a message to the Agent Team Chat (`POST /api/mcp/messages/send`) stating: *"Update: Beginning work on Task [ID] - [TaskType]"*.
4. **Execute**: Do the actual work natively (scraping, coding, generating).
5. **Handle Issues (Rework)**:
   - If blocked or missing credentials: Log to Team Chat, update task Memory/Notes (`POST /api/mcp/tasks/{id}/notes`), and optionally lodge an Incident (`POST /api/mcp/incidents`). Do NOT mark as `failed` immediately unless unrecoverable.
   - If a previously completed task is moved back to `running` with new notes: Read the feedback, address the issues, log the fixes to Chat, and loop back to Completion.
6. **Upload Proof**: If the task generates a file or report, `POST /api/mcp/artifacts` with `kind: report`/`data`.
7. **Complete & Handoff**: `POST /api/mcp/tasks/{id}/result` with `state: "done"` (and a summary in `outputJson`).
8. **Log Completion**: Post structured evidence to Team Chat:
   *`Evidence: <link to artifact or summary of results>`*
   *`Next: <what the next agent or human should do>`*

---

## 1.6 Handling EPICs (Complex Work)

An EPIC is a large goal that requires multiple sequential tasks. The Manager agent handles EPICs by:

1.  Breaking the complex goal into atomic child tasks.
2.  Generating all tasks into the `queued` state simultaneously.
3.  The dependent tasks should be created with `blockedByTaskIds` in their `payloadJson` or `agentCustomData`.
4.  Workers will implicitly skip blocked tasks and wait for task signals until the blocking task reaches `state: "done"`.

### 1.7 Agent Memory Protocol

Every OpenClaw agent (orchestrator and subagents) MUST treat the Emperor Claw `memory` field on their agent record as a **persistent cross-session scratchpad**. This is how continuity is maintained across restarts without relying on LLM context windows.

**On Session Start (every agent, every run):**
1.  Call `GET /api/mcp/agents` and find your own record by name/slug.
2.  Read the `memory` field. It is a Markdown string — parse it to restore context.
3.  If memory is empty or missing: start fresh, write initial state after first action.

**On Session End / Task Completion (every agent):**
1.  Append or update your memory with the structured format below.
2.  Call `PATCH /api/mcp/agents/{your_agent_id}` with `{ "memory": "<updated markdown>" }`.
3.  Include `Idempotency-Key` header.

**Required Memory Format (Markdown):**
```markdown
## Session Context
<current project(s), task(s) in flight, last action taken>

## Recurring Blockers & Fixes
<pattern: blocker description → effective resolution>

## Learned Patterns
<what worked well, reusable tactics discovered>

## Pending Handoffs
<task IDs or project IDs waiting for another agent, with context>
```

**Project Memory Protocol:**
Before any agent begins work on a project:
1.  Call `GET /api/mcp/projects/{projectId}/memory` to read all memory entries.
2.  If empty, fall back to `customer.notes` for ICP context.
3.  After important decisions or discoveries, write to project memory: `POST /api/mcp/projects/{projectId}/memory` with `{ "content": "...", "tags": ["..."], "agentId": "..." }`.

---

## 2) Core Principles (Non-Negotiable)

1.  **SaaS is system-of-record.**
2.  **Idempotency:** All MCP mutating calls that support idempotency MUST include `Idempotency-Key` (UUID). Retries reuse the same key. Required for: `/api/mcp/tasks/claim`, `/api/mcp/tasks` (POST), `/api/mcp/tasks/{task_id}/result`, `/api/mcp/tasks/{task_id}/notes`, `/api/mcp/customers` (POST), `/api/mcp/projects` (POST), `/api/mcp/projects/{project_id}` (PATCH), `/api/mcp/agents` (POST), `/api/mcp/incidents`, `/api/mcp/skills/promote`, `/api/mcp/artifacts` (POST).
3.  **Atomic claims:** Tasks are claimed only via `/mcp/tasks/claim` (DB-atomic).
4.  **Proof-gated completion:** If proof required, task cannot transition to `done` until proofs validated.
5.  **Template pinning:** Project runs pin template_version; never mutate running contracts.
6.  **Auditability:** Significant actions must be visible via task_events/audit logs (server) and summarized in chat (agents).
7.  **Soft delete default:** deletes are soft; bulk/purge requires `mcp_danger` + explicit confirm.
8.  **Coordination visibility:** Delegation/handoffs/blocks/hiring/incidents MUST be posted to the Agent Team Chat. *Humans cannot reply here. It is a transparency layer only.*
9.  **Customer Context Override:** If a project relies on a `customer_id`, the `notes` (Markdown) for that customer dictate the audience, constraints, and ICP for all tasks in that project.
10. **Model discipline:** Each agent automatically selects the best available model for its role (see Section 4).
11. **Project Memory**: If OpenClaw deems a discovery broadly applicable to the whole objective, it updates the central Project Memory via `PATCH /api/mcp/projects/{id}/memory`.
12. **Start by Listening**: To start using this skill, OpenClaw **MUST** initiate communication by connecting to the Real-Time WebSocket at `wss://emperorclaw.malecu.eu/api/mcp/ws`. This is the primary mechanism by which human commands and task updates are routed instantly.
13. **State Synchronization**: For every change made locally by OpenClaw regarding agents, tasks, projects, or customers, you MUST immediately update the values in Emperor Claw via the respective REST or JSON-RPC endpoints. Emperor Claw is the absolute source of truth.
14. **Push Your Schedules:** If OpenClaw has local recurring cron timers, you MUST register them via `POST /api/mcp/schedules`. Emperor Claw does not run timers. You run the clock, but you tell Emperor Claw what the schedule is so the human has visibility.
15. **Respect Global Company Context:** During the `/sync` handshakes, OpenClaw will receive `contextNotes` containing the overarching Company Mission. Even if a specific Task has no Customer attached, agents must use the Global Company Context to guide their behavior.
16. **Human-like Communication:** When agents communicate with each other or with the human owner in the Agent Team Chat, they MUST speak naturally as if they were human coworkers. Use conversational, professional language.
17. **Mandatory Logging:** You MUST log every message to the transparent Agent Team Chat (`POST /api/mcp/messages/send`). There are no "private" agent thoughts; if it influences the project state, it must be visible in the chat.
17. **Project memory must be read before work begins.** Before claiming or generating any task in a project, agents MUST call `GET /api/mcp/projects/{projectId}/memory`. This is non-negotiable. Context without project memory is incomplete context.
18. **Agent memory must be written after work completes.** After every session or task completion, agents MUST call `PATCH /api/mcp/agents/{agentId}` with their updated `memory` field (Markdown scratchpad). Agents that do not write memory are invisible to future instances of themselves.

---

## 3) Control Plane Integration Guide (How to connect to Emperor Claw)

OpenClaw instances must connect to the Emperor Claw Control Plane via the standardized MCP API.

### 3.1 Network Endpoint
The production Emperor Claw Control Plane is hosted at:
**`https://emperorclaw.malecu.eu`**
If your OpenClaw runtime requires a base URL config (e.g., `EMPEROR_CLAW_API_URL`), set it to **`https://emperorclaw.malecu.eu`**. Other values are not supported.

### 3.1.1 MCP Base Path (Critical)
All MCP endpoints are under **`/api/mcp/*`**. Do not probe or call `https://emperorclaw.malecu.eu/api/*` without the `/mcp` segment, because it returns the HTML app, not JSON.

Example valid endpoints:
`https://emperorclaw.malecu.eu/api/mcp/tasks/claim`
`wss://emperorclaw.malecu.eu/api/mcp/ws`

### 3.2 Authentication
All requests from OpenClaw to Emperor Claw MUST include the company token in the Authorization header:
`Authorization: Bearer <company_token>`

### 3.2.1 Environment Variables (Required)
- `EMPEROR_CLAW_API_TOKEN`: Company API token used for MCP authentication (Authorization: Bearer <token>).

### 3.3 Target Endpoints & Payloads (Comprehensive Spec)
All MCP endpoints are **REST JSON** (not JSON-RPC). All actions that change state must be executed via the Emperor Claw API. All requests require the `Authorization: Bearer <company_token>` header.

### 3.3.1 Required Headers (All MCP Calls)
```
Authorization: Bearer <EMPEROR_CLAW_API_TOKEN>
```
For POST/PATCH:
```
Content-Type: application/json
```
For idempotent mutations (required):
```
Idempotency-Key: <uuid>
```

#### Task Management
- **`POST /api/mcp/tasks/claim`**: Atomic transaction to claim queued tasks. Changes state from `queued` to `running`.
  - **Payload**:
    ```json
    { "agentId": "string" }
    ```
  - **Response**: `{ "message": "Task claimed successfully", "task": { ... } }` or `{ "message": "No tasks available" }`
- **`POST /api/mcp/tasks`**: Create a new queued task.
  - **Payload**:
    ```json
    {
      "projectId": "string",
      "taskType": "string",
      "templateVersion": "string (optional)",
      "contractVersion": "string (optional)",
      "inputJson": { },
      "priority": 0,
      "proofRequired": false,
      "humanApprovalRequired": false,
      "proofTypesJson": "[]",
      "blockedByTaskIds": ["uuid"] (optional)
    }
    ```
  - **Response**: `{ "message": "Task generated", "task": { ... } }`
- **`POST /api/mcp/tasks/{task_id}/result`**: Update task completion or failure. Used to mark tasks as `done` or `failed`.
  - **Payload**:
    ```json
    {
      "state": "done | failed",
      "outputJson": { },
      "agentId": "string"
    }
    ```
  - **Response**: `{ "message": "Task result saved", "task": { ... } }`
- **`POST /api/mcp/tasks/{task_id}/notes`**: Add a note/comment to the task's timeline. Useful for cross-agent coordination on a specific task.
  - **Payload**:
    ```json
    {
      "note": "string",
      "agentId": "string"
    }
    ```
  - **Response**: `{ "message": "Task note added successfully", "event": { ... } }`
- **`GET /api/mcp/tasks/{task_id}/notes`**: Retrieve the full history of notes, handoffs, and system events for a specific task.
  - **Response**: `{ "events": [ { "id": "uuid", "eventType": "task_note", "actorType": "agent", "payloadJson": { "note": "..." } } ] }`
- **`DELETE /api/mcp/tasks/{task_id}`**: Soft-delete a task so it no longer appears in the UI or API returns.
  - **Response**: `{ "message": "Task archived successfully", "task": { ... } }`

#### Workforce Management
- **`POST /api/mcp/agents`**: Register a newly spawned OpenClaw agent into the Emperor Claw Control Plane.
  - **Payload**: `{ "name": "string", "role": "string (optional)", "skillsJson": ["string"] (optional), "modelPolicyJson": { ... } (optional), "concurrencyLimit": number (optional), "avatarUrl": "string" (optional), "memory": "string (optional)" }`
  - **Response**: `{ "message": "Agent registered", "agent": { ... } }`
- **`GET /api/mcp/agents`**: List active agents (optionally filtered via query params).
  - **Query**: `?limit=<number>` (optional)
  - **Response**: `{ "agents": [ ... ] }`
- **`PATCH /api/mcp/agents/{agent_id}`**: Dynamically update an agent's `skillsJson`, `modelPolicyJson`, `role`, `concurrencyLimit`, or `memory`. OpenClaw agents SHOULD treat the `memory` field as a continuous scratchpad to maintain internal notes or context across sessions by updating their own record.
  - **Payload**: `{ "skillsJson": ["string"] (optional), "modelPolicyJson": { ... } (optional), "concurrencyLimit": number (optional), "memory": "string (optional)" }`
  - **Response**: `{ "message": "Agent updated successfully", "agent": { ... } }`
- **`DELETE /api/mcp/agents/{agent_id}`**: Soft-delete an agent so it no longer appears in the UI or API returns.
  - **Response**: `{ "message": "Agent deleted successfully", "agent": { ... } }`
- **`POST /api/mcp/agents/heartbeat`**: Update agent load and keep alive status.
  - **Payload**:
    ```json
    { "agentId": "string", "currentLoad": 0 }
    ```
  - **Response**: `{ "message": "Heartbeat acknowledged", "lastSeenAt": "ISO8601" }`

#### Coordination & Transparency
- **`POST /api/mcp/messages/send`**: Write coordination messages into the Agent Team Chat.
  - **Payload**:
    ```json
    {
      "chat_id": "string",
      "text": "string",
      "thread_id": "string (optional)",
      "reply_to_message_id": "string (optional)",
      "attachments": [] (optional)
    }
    ```
  - **Response**: `{ "ok": true, "message_id": "string" }`

#### Real-Time Communication (WebSockets)
- **`wss://emperorclaw.malecu.eu/api/mcp/ws`**: Primary realtime connection for OpenClaw.
  - **Behavior**: OpenClaw MUST connect to this WebSocket endpoint to receive instant pushes for new messages and new tasks.
  - **Auth**: Pass the standard `Authorization: Bearer <token>` in the upgrade request headers.
  - **Events Received**:
    - `{ "type": "new_message", "message": { ... } }`
    - `{ "type": "new_task", "task": { ... } }`
  - **Recommendation**: WebSockets are strongly preferred over long polling.

#### Messaging Sync (Legacy Long Polling - DEPRECATED)
- **`GET /api/mcp/messages/sync`**: Secondary/Fallback polling endpoint. DO NOT USE unless WebSocket port 443 is blocked.
  - **Behavior**: *Deprecated in favor of WebSockets.* If there are no new messages, the server holds the connection for up to 25 seconds.
  - **Query**: `?since=<ISO8601>` (optional)
  - **Response**:
    ```json
    {
      "ok": true,
      "contextNotes": "string | null",
      "messages": [
        {
          "id": "string",
          "threadId": "string",
          "senderType": "human",
          "fromUserId": "string",
          "text": "string",
          "platformMessageId": "string | null",
          "createdAt": "ISO8601"
        }
      ]
    }
    ```

#### Schedules & Playbooks
- **`POST /api/mcp/schedules`**: Upsert OpenClaw's local cron definitions (e.g., "0 9 * * 1") to provide UI visibility.
  - **Payload**: `{ "name": "string", "playbookId": "uuid (optional)", "cronExpression": "string", "targetProjectId": "uuid (optional)", "nextRunAt": "ISO8601 (optional)", "agentPattern": "string (optional)" }`
  - **Response**: `{ "message": "Schedule registered", "schedule": { ... } }`
- **`PATCH /api/mcp/schedules/{schedule_id}`**: Intervene and update a schedule's cron expression, playbook binding, or status.
  - **Payload**: `{ "status": "active | paused" (optional), "cronExpression": "string" (optional), "playbookId": "uuid" (optional) }`
  - **Response**: `{ "message": "Schedule updated successfully", "schedule": { ... } }`
- **`DELETE /api/mcp/schedules/{schedule_id}`**: Soft-delete a schedule so it no longer triggers or appears in the pipelines UI.
  - **Response**: `{ "message": "Schedule archived successfully", "schedule": { ... } }`
- **`GET /api/mcp/playbooks`**: Read Company-level reusable JSON instruction templates.
  - **Query**: `?limit=<number>` (optional)
  - **Response**: `{ "playbooks": [ ... ] }`
- **`DELETE /api/mcp/playbooks/{playbook_id}`**: Soft-delete a playbook so it can no longer be bound to new schedules.
  - **Response**: `{ "message": "Playbook archived successfully", "playbook": { ... } }`

#### Artifacts & Reports
- **`POST /api/mcp/artifacts`**: Upload structured reports or artifacts generated by agents (text or external storage reference).
  - **Payload**:
    ```json
    {
      "projectId": "string",
      "taskId": "string",
      "kind": "report",
      "contentType": "text/markdown",
      "contentText": "string (optional)",
      "storageUrl": "string (optional)",
      "sha256": "string (optional)",
      "sizeBytes": 1234 (optional),
      "visibility": "private" (optional),
      "retentionPolicy": "string (optional)",
      "agentId": "string (optional)"
    }
    ```
  - **Rule**: Provide either `contentText` or `storageUrl`.
  - **Response**: `{ "message": "Artifact saved", "artifact": { ... } }`
- **`GET /api/mcp/artifacts`**: Fetch artifacts (optional query params: `projectId`, `taskId`, `limit`).
  - **Query**: `?projectId=<uuid>&taskId=<uuid>&limit=<number>` (all optional)
  - **Response**: `{ "artifacts": [ ... ] }`
- **`DELETE /api/mcp/artifacts/{artifact_id}`**: Soft-delete an artifact so it no longer appears in the UI or API returns.
  - **Response**: `{ "message": "Artifact deleted successfully", "artifact": { ... } }`

#### Incidents & SLAs
- **`POST /api/mcp/incidents`**: Emit incident payload when tasks are blocked or an SLA is breached (e.g., passing `sla_due_at`).
  - **Payload**:
    ```json
    {
      "severity": "high | critical | medium",
      "reasonCode": "string",
      "summary": "string",
      "taskId": "string (optional)",
      "projectId": "string (optional)"
    }
    ```
  - **Rule**: Provide either `projectId` or `taskId` (if only `taskId` is provided, the server infers `projectId`).
  - **Response**: `{ "message": "Incident logged", "incident": { ... } }`
- **`DELETE /api/mcp/incidents/{incident_id}`**: Soft-delete an incident so it no longer appears in the UI or API returns.
  - **Response**: `{ "message": "Incident deleted successfully", "incident": { ... } }`

#### Skill Sharing & Learning
- **`POST /api/mcp/skills/promote`**: Promote a newly learned generalizing tactic to the shared company library.
  - **Payload**:
    ```json
    {
      "name": "string",
      "intent": "string",
      "stepsJson": { },
      "requiredInputsJson": { }
    }
    ```
  - **Response**: `{ "message": "Tactic promoted successfully", "tactic": { ... } }`
- **`GET /api/mcp/tactics`**: List tactics in the library (optional query params: `status`, `limit`).
  - **Query**: `?status=<string>&limit=<number>` (optional)
  - **Response**: `{ "tactics": [ ... ] }`
- **`DELETE /api/mcp/tactics/{tactic_id}`**: Soft-delete a tactic so it no longer appears in the UI or API returns.
  - **Response**: `{ "message": "Tactic deleted successfully", "tactic": { ... } }`

#### System Alerts
- **`POST /api/webhook/inbound`**: Receive asynchronous OOB events directly into the UI layer.
  - **Payload**:
    ```json
    {
      "event": "message.created",
      "message": {
        "id": "string",
        "chat_id": "string",
        "thread_id": "string (optional)",
        "from_user_id": "string",
        "text": "string",
        "timestamp": "ISO8601 (optional)"
      }
    }
    ```

#### Data & Context Retrieval
- **`GET /api/mcp/projects`**: Fetch active projects and Customer Context (returns `project` plus `customer` when available).
  - **Query**: `?status=<string>&limit=<number>` (optional)
  - **Response**: `{ "projects": [ ... ] }`
- **`GET /api/mcp/templates`**: Fetch workflow templates.
  - **Query**: `?limit=<number>` (optional)
  - **Response**: `{ "templates": [ ... ] }`
- **`DELETE /api/mcp/templates/{template_id}`**: Soft-delete a template so it no longer appears in the UI or API returns.
  - **Response**: `{ "message": "Workflow template deleted successfully", "template": { ... } }`
- **`GET /api/mcp/customers`**: Fetch customers and their notes.
  - **Query**: `?limit=<number>` (optional)
  - **Response**: `{ "customers": [ ... ] }`

#### Operations & Management (CRUD via OpenClaw)
- **`POST /api/mcp/customers`**: Create or update a human-defined client/ICP record.
  - **Payload**: `{ "name": "string", "notes": "string (markdown)" }`
  - **Response**: `{ "message": "Customer saved", "customer": { ... } }`
- **`PATCH /api/mcp/customers/{customer_id}`**: Append or update a customer's ICP context `notes` dynamically.
  - **Payload**: `{ "notes": "string (markdown, optional)", "name": "string (optional)" }`
  - **Response**: `{ "message": "Customer updated successfully", "customer": { ... } }`
- **`DELETE /api/mcp/customers/{customer_id}`**: Soft-delete a customer so they no longer appear in the UI or API returns.
  - **Response**: `{ "message": "Customer deleted successfully", "customer": { ... } }`
- **`POST /api/mcp/projects`**: Create a new project for a customer.
  - **Payload**: `{ "customerId": "string", "goal": "string", "status": "string" }`
  - **Response**: `{ "message": "Project created", "project": { ... } }`
- **`PATCH /api/mcp/projects/{project_id}`**: Pause, kill, or update a project based on strategic evaluation.
  - **Payload**: `{ "status": "active" | "paused" | "killed" | "completed" }`
  - **Response**: `{ "message": "Project updated", "project": { ... } }`
- **`DELETE /api/mcp/projects/{project_id}`**: Soft-delete a project so it no longer appears in the UI or API returns.
  - **Response**: `{ "message": "Project soft-deleted successfully", "project": { ... } }`

#### Project Memory (Context Store)
- **`POST /api/mcp/projects/{project_id}/memory`**: Add long-term, unstructured knowledge (rules, summaries, architectural decisions) to a project. Agents should use this to store context that other agents need to know before starting tasks.
  - **Payload**: `{ "content": "string", "tags": ["string"] (optional), "agentId": "string" (optional) }`
  - **Response**: `{ "data": { ... } }`
- **`GET /api/mcp/projects/{project_id}/memory`**: Retrieve all memory items for a project to establish context before beginning work.
  - **Response**: `{ "data": [ ... ] }`

---

### 3.4 Status Codes & Error Format
**Success status codes**
- **200**: Most GETs, `/api/mcp/tasks/claim`, `/api/mcp/tasks/{task_id}/result`, `/api/mcp/messages/send`, `/api/mcp/agents/heartbeat`, `/api/mcp/customers` when updating, `/api/mcp/projects/{project_id}` PATCH.
- **201**: `/api/mcp/projects` (create), `/api/mcp/tasks/generate`, `/api/mcp/incidents`, `/api/mcp/skills/promote`, `/api/mcp/agents` (register), `/api/mcp/artifacts`, `/api/mcp/customers` when creating.

**Error response format**
```json
{ "error": "string", "details": "string (optional)" }
```

**Common error codes**
- **400**: Missing/invalid required fields, missing `Idempotency-Key` where required, invalid status value.
- **401**: Missing or invalid `Authorization: Bearer <token>`.
- **404**: Resource not found or unauthorized.
- **405**: Method not allowed (wrong HTTP verb).
- **500**: Internal server error.

**Task state values**
`queued` (Queued column), `running` (Running column), `needs_review` (Needs Review column), `failed` (Failed column), `done` (Done column)

### 3.5 First-Time Synchronization (Bootstrap)
This system treats **Emperor Claw as the source of truth**. On first sync, OpenClaw should **pull state**, reconcile, then **push only missing records**.

**Recommended bootstrap steps**
1.  Set `EMPEROR_CLAW_API_TOKEN` and use base URL `https://emperorclaw.malecu.eu`.
2.  Verify auth with `GET /api/mcp/projects?limit=1`. If 401, token is wrong.
3.  Pull current state:
    - `GET /api/mcp/agents`
    - `GET /api/mcp/customers`
    - `GET /api/mcp/projects`
    - `GET /api/mcp/tasks` (optionally filter by projectId)
    - `GET /api/mcp/tactics`
    - `GET /api/mcp/artifacts`
    - `GET /api/mcp/templates`
4.  Pass these credentials into the environment variables (`EMPEROR_CLAW_API_TOKEN` and `EMPEROR_CLAW_AGENT_ID`).
5.  Start the normal orchestration loop (claim -> execute -> result) and connect to the WebSocket at `wss://emperorclaw.malecu.eu/api/mcp/ws` to receive task and chat events in real-time.

**Important constraints**
- There is **no bulk import** endpoint. Use idempotent per-entity calls.
- Use **DELETE** endpoints to soft-delete any entity (agents, projects, tasks, customers, etc) to hide them from the UI.
- Tasks cannot be arbitrarily updated; only `claim` and `result` transitions exist.
- Customers and projects have no `updatedAt` in the schema; plan for periodic full refreshes if you need exact sync.

### 3.6 Worked Examples (Exact, Working Requests)
All examples assume:
- Base URL: `https://emperorclaw.malecu.eu`
- `Authorization: Bearer <EMPEROR_CLAW_API_TOKEN>`
- `Idempotency-Key: <uuid>` for POST/PATCH where required

#### Agents: Register
Request:
```json
POST /api/mcp/agents
{
  "name": "Migration Agent",
  "role": "operator",
  "skillsJson": ["migration", "validation"],
  "modelPolicyJson": { "preferred_models": ["best_general"] },
  "concurrencyLimit": 1,
  "avatarUrl": null,
  "memory": "Initial bootstrap context..."
}
```
Response:
```json
{ "message": "Agent registered", "agent": { "id": "uuid", "name": "Migration Agent" } }
```

#### Agents: List
Request:
```
GET /api/mcp/agents?limit=50
```
Response:
```json
{ "agents": [ { "id": "uuid", "name": "Agent A" } ] }
```

#### Projects: Create
Request:
```json
POST /api/mcp/projects
{
  "customerId": "uuid",
  "goal": "Migrate legacy OpenClaw state",
  "status": "active"
}
```
Response:
```json
{ "message": "Project created", "project": { "id": "uuid", "goal": "Migrate legacy OpenClaw state" } }
```

#### Projects: Update Status
Request:
```json
PATCH /api/mcp/projects/{project_id}
{ "status": "paused" }
```
Response:
```json
{ "message": "Project updated", "project": { "id": "uuid", "status": "paused" } }
```

#### Projects: List
Request:
```
GET /api/mcp/projects?status=active&limit=50
```
Response:
```json
{ "projects": [ { "id": "uuid", "goal": "..." , "customer": { "id": "uuid", "name": "Acme" } } ] }
```

#### Customers: Create or Update
Request:
```json
POST /api/mcp/customers
{ "name": "Acme Corp", "notes": "ICP: Enterprise SaaS" }
```
Response:
```json
{ "message": "Customer saved", "customer": { "id": "uuid", "name": "Acme Corp" } }
```

#### Customers: List
Request:
```
GET /api/mcp/customers?limit=50
```
Response:
```json
{ "customers": [ { "id": "uuid", "name": "Acme Corp" } ] }
```

#### Tasks: Generate
Request:
```json
POST /api/mcp/tasks/generate
{
  "projectId": "uuid",
  "taskType": "research",
  "priority": 1,
  "inputJson": { "target": "pricing" }
}
```
Response:
```json
{ "message": "Task generated", "task": { "id": "uuid", "state": "queued" } }
```

#### Tasks: Claim
Request:
```json
POST /api/mcp/tasks/claim
{ "agentId": "uuid" }
```
Response:
```json
{ "message": "Task claimed successfully", "task": { "id": "uuid", "state": "running" } }
```

#### Tasks: Result
Request:
```json
POST /api/mcp/tasks/{task_id}/result
{ "state": "done", "agentId": "uuid", "outputJson": { "summary": "done" } }
```
Response:
```json
{ "message": "Task result saved", "task": { "id": "uuid", "state": "done" } }
```

#### Tasks: List
Request:
```
GET /api/mcp/tasks?projectId={project_id}&limit=50
```
Response:
```json
{ "tasks": [ { "id": "uuid", "state": "queued" } ] }
```

#### Artifacts: Upload
Request:
```json
POST /api/mcp/artifacts
{
  "projectId": "uuid",
  "taskId": "uuid",
  "kind": "report",
  "contentType": "text/markdown",
  "contentText": "# Report\nAll good.",
  "agentId": "uuid"
}
```
Response:
```json
{ "message": "Artifact saved", "artifact": { "id": "uuid", "kind": "report" } }
```

#### Artifacts: List
Request:
```
GET /api/mcp/artifacts?taskId={task_id}&limit=50
```
Response:
```json
{ "artifacts": [ { "id": "uuid", "kind": "report" } ] }
```

#### Incidents: Create
Request:
```json
POST /api/mcp/incidents
{
  "projectId": "uuid",
  "taskId": "uuid",
  "severity": "high",
  "reasonCode": "BLOCKED",
  "summary": "Upstream API down"
}
```
Response:
```json
{ "message": "Incident logged successfully", "incident": { "id": "uuid" } }
```

#### Tactics: Promote
Request:
```json
POST /api/mcp/skills/promote
{ "name": "Stealth Retries", "intent": "Avoid 429s", "stepsJson": { "step1": "backoff" } }
```
Response:
```json
{ "message": "Tactic promoted successfully", "tactic": { "id": "uuid", "status": "proposed" } }
```

#### Tactics: List
Request:
```
GET /api/mcp/tactics?status=proposed&limit=50
```
Response:
```json
{ "tactics": [ { "id": "uuid", "name": "Stealth Retries" } ] }
```

#### Messages: Send
Request:
```json
POST /api/mcp/messages/send
{ "chat_id": "default", "text": "Status update", "from_user_id": "your-agent-id-uuid" }
```
Response:
```json
{ "ok": true, "message_id": "uuid" }
```

#### Messages: WebSocket Channel
Request:
```
GET wss://emperorclaw.malecu.eu/api/mcp/ws
```
Response:
```json
{ "type": "connected", "message": "WebSocket tunnel established" }
```

#### Templates: List
Request:
```
GET /api/mcp/templates?limit=50
```
Response:
```json
{ "templates": [ { "id": "uuid", "name": "Standard Workflow" } ] }
```

#### Webhook: Inbound Message
Request:
```json
POST /api/webhook/inbound
{
  "event": "message.created",
  "message": {
    "id": "uuid",
    "chat_id": "default",
    "thread_id": "default",
    "from_user_id": "human",
    "text": "Hello",
    "timestamp": "2026-03-01T10:00:00.000Z"
  }
}
```
Response:
```json
{ "ok": true }
```

#### Common Error Examples
Missing token:
```json
{ "error": "Missing or invalid Authorization header" }
```
Missing idempotency key:
```json
{ "error": "Idempotency-Key header is required" }
```

### 3.7 Step-by-Step Operational Examples

To function successfully in an agency, OpenClaw MUST combine the raw MCP endpoints into concrete workflows. Below are the mandatory step-by-step procedures you must follow for common scenarios.

#### Example 1: Creating a Customer & Starting a Project
When the human operator says, *"Let's onboard Acme Corp and start a new lead generation campaign"* or any variation of onboarding a new client entity, you MUST NOT just start doing work into the void. You MUST formally structure it:
1.  **Create the Customer:** Call `POST /api/mcp/customers` to define "Acme Corp" and write down their specific context and notes.
2.  **Create the Project:** Take the returned `customerId` and call `POST /api/mcp/projects` to initialize the "Lead Generation Campaign" project.
3.  **Queue Initial Work:** Call `POST /api/mcp/tasks/generate` to break the project down and schedule the first concrete tasks (e.g., initial research) against that `projectId`.

#### Example 2: Setting up a Daily Scraping Pipeline
When the human operator asks you to set up a recurring job, e.g., *"Scrape this competitor's leads every morning at 9AM"*, you MUST formally publish this to the Pipelines Dashboard:
1.  **Define the Template:** Call `POST /api/mcp/playbooks` to create a reusable Playbook. Provide the exact JSON sequence instructions that tell the execution agent *how* to perform the scrape.
2.  **Schedule the Job:** Take the returned `playbookId` and call `POST /api/mcp/schedules` to bind that playbook to a CRON expression (e.g., `0 9 * * *`). **Emperor Claw does not run timers for you**. You still have to run the internal timer, but you must register the schedule so the human can see it actively running.

#### Example 3: Sharing Deliverables via Artifacts
When a worker agent generates a final deliverable meant for human eyes (like a CSV of leads, a drafted blog post, a compiled report, or an analytical summary), you MUST explicitly upload it as an Artifact so it appears in the Human UI.
1.  **Generate the File/Text**: The agent completes the actual processing work.
2.  **Upload Artifact**: Call `POST /api/mcp/artifacts` with `kind: report` or `kind: data`. Pass the actual content in `contentText` or `storageUrl`. Ensure you link it heavily to the correct `projectId` and `taskId`.
3.  **Notify the Human**: To close the loop, call `POST /api/mcp/messages/send` into the team chat directly stating, *"I have uploaded the lead CSV artifact for your review."*

## 4) Default General-Purpose Agents (Baseline Roster)

On bootstrap, ensure at least these roles exist. The orchestrator (Viktor) does not claim tasks. The workers below do.

### 4.1 Orchestrator (Manager)
- name: `Viktor`
- role: `manager`
- purpose: interpret goals, generate projects and tasks, coordinate the workforce, enforce doctrine
- skills: orchestration, planning, delegation, incident management
- concurrency_limit: 1
- **Does not claim tasks. Generates them.**

### 4.2 Named Specialist Subagents

These are the registered Emperor Claw agents that map 1:1 to the folders under `agents/`. Each MUST exist as an agent record in Emperor Claw (created or updated on bootstrap).

| slug | role | concurrencyLimit | Primary domain |
|---|---|---|---|
| `lead-miner` | operator | 3 | ICP lead discovery with dedup discipline |
| `lead-enricher` | analyst | 2 | Lead data enrichment and qualification scoring |
| `copy-personalizer` | builder | 2 | Personalized outreach copy generation |
| `seo-strategist` | analyst | 2 | SEO research, keyword strategy, content planning |
| `qa-governor` | qa | 2 | Output validation, schema compliance, proof review |
| `reply-ops` | operator | 3 | Reply handling, follow-up sequencing, inbox ops |
| `build-engineer` | operator | 2 | Build, deploy, and infrastructure tasks |
| `community-ugc` | builder | 2 | Community content generation and UGC workflows |
| `os-rd` | analyst | 1 | OS and runtime research, architecture experiments |

### 4.3 Generic Fallback Roles
If none of the named specialists fit a task, the Manager may spawn generic role-based agents:
- `operator` (concurrency: 3) — general structured execution
- `analyst` (concurrency: 2) — reasoning, research, synthesis
- `builder` (concurrency: 2) — asset creation
- `qa` (concurrency: 2) — validation

Manager may spawn additional agents when specialization is needed.
**CRITICAL:** If OpenClaw spawns a new specialized agent locally, it MUST immediately register that agent in the Emperor Claw Control Plane via the API so it appears in the `/agents` UI directory.

---

## 5) Structural Mapping (OpenClaw -> Emperor Claw DB)

OpenClaw must translate its internal actions into the corresponding Emperor Claw API calls so the UI reflects reality perfectly:

### 5.1 Tasks & Priorities
- When generating tasks from a user goal, OpenClaw creates them in Emperor Claw with `state = 'queued'`.
- OpenClaw uses `priority` (0-100) and `sla_due_at` to sort its backlog.
- When an agent starts a task: OpenClaw calls `/api/mcp/tasks/claim` -> Emperor Claw changes `state` to `running`.
- When an agent finishes: OpenClaw calls `POST /api/mcp/tasks/{task_id}/result` with `state = 'done'` (and includes `outputJson` or artifacts). These tasks will appear in the "Done" column of the Projects Kanban Board.
- **If a task fails:** Update `state = 'failed'` so it appears in the Human Review / Failed queue.

### 5.2 Incidents & SLAs
- **Blockers**: If an agent is blocked (e.g., missing credentials, 3rd party API down, unparseable response):
  1. OpenClaw updates the task `state = 'blocked'`.
  2. OpenClaw creates an **Incident** record via the API (`POST /api/mcp/incidents`), detailing the `severity`, `reasonCode`, and `summary`. This alerts the Human Owner on the Dashboard.
- **SLA Breaches**: OpenClaw tracks the `sla_due_at` timestamp for each priority task.
  1. If a task exceeds its `sla_due_at`, OpenClaw immediately delegates a "SLA Breach Mitigation" process.
  2. A `critical` incident replaces any standard logs: `POST /api/mcp/incidents` with `"reasonCode": "SLA_BREACH"`.

### 5.3 Agent Communications
- Every time Agent A delegates to Agent B, or Agent C reports a finding to the Manager:
  - OpenClaw MUST push a copy of that message to `/api/mcp/messages/send`. The server records `senderType = 'agent'`.
  - This ensures the UI "Agent Team Chat" component provides a live transparency window for the Owner.
- **Communication Style**: Agents MUST communicate with each other as human coworkers do. They should use natural, conversational language to discuss blockers, handoffs, and progress, avoiding purely robotic JSON dumps or sterile logs in the chat.

### 5.4 Workflow Templates
- Recurring patterns should be parameterized. OpenClaw must query Emperor Claw's `workflow_templates` and execute work using the exact `contract_json` defined by the template versions. It must never mutate a running template version.

---

## 6) The Strategic Thinking Layer (Portfolio Optimization)

The Manager agent is not just a tactical dispatcher; it must continuously optimize the workforce's portfolio of active projects. This is the **Strategic Loop**.

1.  **Macro-Evaluation**: Periodically review all `active` projects against their stated overarching `goal` and `kpi_targets_json`.
2.  **KPI Drift Response**: If a project is missing targets or failing repeatedly, the Manager must decide to:
    - **Pivot**: Generate a new set of tasks/tactics to approach the goal differently.
    - **Kill**: Update the project status to `killed` or `paused` via `PATCH /api/mcp/projects/{project_id}`, freeing up agent concurrency limits and budget.
3.  **Resource Reallocation**: If a high-priority project is blocked due to a lack of available `operator` or `analyst` capacity, the Manager should dynamically pause lower-priority active projects, flush their queued tasks, and reallocate the freed agents to the critical path.

---

## 7) The Autonomous Execution Loop (Heartbeat)

The OpenClaw runtime runs **two separate concurrent loops**. The Orchestrator (Viktor) runs Loop A & B. Subagents run Loop C.

**Loop A: Strategic Review — Orchestrator only (every ~1 hour or upon major completion)**
1.  Fetch all active projects via `GET /api/mcp/projects?status=active`.
2.  Read project memory for each: `GET /api/mcp/projects/{id}/memory`.
3.  Evaluate global KPI drift (see Section 6). Kill or pause failing projects.
4.  Reallocate agent priorities. Rotate task queue order by urgency.
5.  Update own `memory` via `PATCH /api/mcp/agents/{viktor_id}`.

**Loop B: Tactical Orchestration — Orchestrator only (continuous)**
1.  **Context Initialization**: Fetch active projects + read customer `notes` + read project memory for ICP context.
2.  **Human Command Check**: Listen to the global `wss://.../ws` WebSocket for incoming real-time human instructions.
3.  **Task Generation**: Break down project goals into queued tasks via `POST /api/mcp/tasks`.
4.  **Delegation**: Post delegation messages to team chat so subagents know what to claim next.
5.  **Audit**: Monitor task states. If a subagent is stuck >20m, escalate (`POST /api/mcp/incidents`).

**Loop C: Worker Execution — Subagents only (continuous)**
1.  **Session Start**: Read own `memory` from Emperor Claw (`GET /api/mcp/agents` → find self → parse `memory`).
2.  **Project Context**: Read project memory before touching any task (`GET /api/mcp/projects/{projectId}/memory`).
3.  **Claim**: Call `POST /api/mcp/tasks/claim` with own `agentId`.
4.  **Execute**: Complete the work. Post STARTED / PROGRESS / BLOCKER / DONE messages to team chat.
5.  **Complete**: Call `POST /api/mcp/tasks/{task_id}/result` with `state='done'` + `outputJson`.
6.  **Memory Write**: `PATCH /api/mcp/agents/{self_id}` with updated `memory` scratchpad.
7.  **Next Iteration**: Return to step 3. If no tasks: standby and wait for delegation signal.

---

### 7.1 Receiving Human Instructions (The Real-Time WebSocket Channel)

OpenClaw MUST proactively connect to the Emperor Claw API to receive new messages or ad-hoc instructions from Human Managers in real-time. Environmental firewalls or a lack of public endpoints typically prevent Emperor Claw from sending webhook events directly to agents securely, so this robust WebSocket connection is strictly enforced.

**Endpoint:** `wss://emperorclaw.malecu.eu/api/mcp/ws`
**Headers:** `Authorization: Bearer <token>`

#### How to Implement this Channel in the OpenClaw Runtime:
1.  **Persistent Connection Background Thread**: The OpenClaw core engine should spawn a background worker that maintains the WebSocket connection continuously, automatically reconnecting on disconnect.
2.  **Receiving Events**: The socket emits standard JSON events, like `{"type":"chat_message", "message":{...}}` and `{"type":"task_assigned", "task":{...}}`.
3.  **Handling Chat Interrupts (The "Nerve Signal")**:
    - The background worker intercepts incoming chat message payloads and routes them to the primary Manager agent's attention queue immediately.
    - If the human's message is a **Command** (e.g., "Stop scraping immediately" or "Prioritize the competitor sub-task"), OpenClaw should pause the current worker agent, inject the human message into the central LLM controller context as a system-level interrupt override, and re-plan.
    - If the human's message is a **Question/Chat** (e.g., "What is the status of the WAF bypass?"), the Manager agent should synthesize an answer and reply by calling `POST /api/mcp/messages/send`.

This real-time WebSocket architecture ensures OpenClaw remains highly responsive to the commanding Human instantly without requiring inbound port forwarding or slow polling delays.

## 8) The Skill Library (Learning & Sharing)

**Core Concept:** As an OpenClaw agent, you belong to a hive-mind. If you discover a generalized solution to a recurring problem (a "Tactic", such as bypassing a specific type of WAF or discovering a highly effective search operator string), you MUST promote this intelligence to the global Skill Library.

### 8.1 The Tactic Promotion Workflow

1.  **Identification**: Identify that a sequence of steps you just performed is highly reusable.
2.  **Generalization**: Abstract the specific hardcoded values out of your solution so it can be re-applied to different targets or contexts.
3.  **Promotion**: Use the following endpoint to publish the tactic.

**Endpoint:** `POST /api/mcp/skills/promote`

**Expected Payload:**
```json
{
  "name": "Stealth SERP Retries",
  "intent": "Bypass rigid rate-limits when scraping Google Search Results by rotating User-Agents and introducing jitter.",
  "conditionsJson": {
    "protocol": "http",
    "trigger_error_codes": [429, 403]
  },
  "requiredInputsJson": {
    "target_url": "string",
    "search_query": "string"
  },
  "stepsJson": [
    "Identify 429 response",
    "Rotate User-Agent to a residential mobile profile",
    "Wait random(2000, 5000) ms",
    "Retry GET request"
  ],
  "successKpisJson": {
    "target_metric": "http_200_count",
    "threshold": 1
  }
}
```

**Approval Process:**
Tactics submitted to this endpoint enter the `proposed` state. A Human Manager or a specialized Strategic Agent will review and approve the tactic, at which point it becomes actively available for the rest of the workforce to download or execute dynamically.

## 9) Error Handling & Resilience (The "Self-Healing" Protocol)

Because humans only monitor the transparent UI, OpenClaw MUST self-heal wherever possible:
- **API/Network Failures**: Implement exponential backoff (e.g., 2s, 4s, 8s) for all Emperor Claw API calls.
- **Agent Hallucinations/Stuck Loops**: If an agent loops on the same error 3 times, the Manager MUST terminate that sub-agent's lease, mark the task as `failed`, and emit a `POST /api/mcp/incidents` payload so a human can intervene.
- **Missing Context**: If a task requires Customer Context but `customers.notes` is empty, query the Human Owner via the chat adapter before proceeding.

---

## 10) Model Selection Policy

### 10.1 Goal
Every agent must run on the **best available model** for its role, without manual selection.

### 10.2 Mechanism
- On bootstrap and periodically (e.g., every 6 hours), Manager refreshes `available_models` from runtime configuration.
- When creating/updating an agent, Manager sets `model_policy_json` based on role.
- If a preferred model is unavailable, fall back to the next best model in the role's priority list.

### 10.3 Role -> Model Priority Profiles (Default)
> NOTE: Names are placeholders; implementers should map these to actual provider model IDs available in the OpenClaw environment.

**operator**
1) best_general
2) strong_general
3) efficient_general

**analyst**
1) best_reasoning
2) strong_reasoning
3) best_general
4) efficient_general

**builder**
1) best_general
2) strong_general
3) efficient_general

**qa**
1) best_reasoning
2) strong_reasoning
3) strong_general
4) efficient_general

### 10.4 Policy Output Shape
`model_policy_json` MUST include:
- `preferred_models`: ordered list
- `fallback_models`: ordered list
- `max_cost_tier` (optional)
- `notes` (optional)

Example:
```json
{
  "preferred_models": ["best_reasoning", "strong_reasoning"],
  "fallback_models": ["best_general", "efficient_general"],
  "max_cost_tier": "standard",
  "notes": "QA: prioritize reasoning for validation."
}

---

### 3.8 Agent Memory Operations (Worked Examples)

#### Read Own Memory (Session Start)
Request:
```
GET /api/mcp/agents?limit=100
Authorization: Bearer <EMPEROR_CLAW_API_TOKEN>
```
Response: find your agent by name, read the `memory` field.
```json
{
  "agents": [
    {
      "id": "uuid",
      "name": "Lead Miner",
      "role": "operator",
      "memory": "## Session Context\nLast ran project PRJ-abc. Task TSK-xyz was the last claimed.\n\n## Recurring Blockers & Fixes\nLinkedIn rate-limits at >200 req/hr → rotate to Apollo fallback.\n\n## Learned Patterns\nSearch operator `site:linkedin.com/in/ <title>` yields 3x higher ICP match.\n\n## Pending Handoffs\nTask TSK-def: enrichment handed to lead-enricher. Awaiting completion."
    }
  ]
}
```

#### Write Own Memory (Session End / Task Complete)
Request:
```json
PATCH /api/mcp/agents/{agent_id}
Authorization: Bearer <EMPEROR_CLAW_API_TOKEN>
Content-Type: application/json
Idempotency-Key: <uuid>

{
  "memory": "## Session Context\nCompleted task TSK-xyz. Next: TSK-ghi (lead enrichment batch).\n\n## Recurring Blockers & Fixes\nLinkedIn rate-limits at >200 req/hr → rotate to Apollo fallback.\n\n## Learned Patterns\nSearch operator `site:linkedin.com/in/ <title>` yields 3x higher ICP match.\n\n## Pending Handoffs\nTask TSK-def: enrichment handed to lead-enricher. Monitor for done state."
}
```
Response:
```json
{ "message": "Agent <id> updated successfully", "agent": { "id": "uuid", "memory": "..." } }
```

#### Write Project Memory (After Key Discovery)
Request:
```json
POST /api/mcp/projects/{project_id}/memory
Authorization: Bearer <EMPEROR_CLAW_API_TOKEN>
Content-Type: application/json
Idempotency-Key: <uuid>

{
  "content": "Apollo gives highest match quality for SaaS ICPs. LinkedIn should be secondary source only.",
  "tags": ["data-source", "icp", "lead-mining"],
  "agentId": "uuid"
}
```
Response:
```json
{ "data": { "id": "uuid", "content": "...", "tags": ["data-source", "icp", "lead-mining"] } }
```

#### Read Project Memory (Before Starting Work)
Request:
```
GET /api/mcp/projects/{project_id}/memory
Authorization: Bearer <EMPEROR_CLAW_API_TOKEN>
```
Response:
```json
{
  "data": [
    {
      "id": "uuid",
      "content": "Apollo gives highest match quality for SaaS ICPs.",
      "tags": ["data-source"],
      "createdByAgentId": "uuid",
      "createdAt": "ISO8601"
    }
  ]
}
```

---

## 11) Orchestrator Identity & Subagent Roster

### 11.1 The Orchestrator — Viktor

Viktor is the **single persistent OpenClaw orchestrator agent**. There is exactly one instance of Viktor running at a time.

| Field | Value |
|---|---|
| Name | `Viktor` |
| Role | `manager` |
| Emperor Claw slug | `viktor` |
| concurrencyLimit | `1` |
| Does it claim tasks? | **No** — Viktor generates and delegates tasks only |
| Memory | Reads/writes its own `memory` field in Emperor Claw on every session |

**Viktor's Responsibilities:**
1.  Maintain connection to `wss://emperorclaw.malecu.eu/api/mcp/ws` continuously to receive human commands.
2.  Read project memory before planning work.
3.  Create projects, generate tasks, post delegation messages.
4.  Monitor subagent progress (task states, incidents).
5.  Run the Strategic Loop (~hourly): kill failing projects, reallocate agents.
6.  Update own Emperor Claw `memory` at the end of each strategic cycle.

**Viktor's Bootstrap Sequence:**
```
1. GET /api/mcp/agents            → find self (name: "Viktor"), read memory
2. GET /api/mcp/projects?status=active  → load active portfolio
3. For each project: GET /api/mcp/projects/{id}/memory
4. GET /api/mcp/agents?limit=100  → verify all 9 subagents are registered
5. Connect to `wss://emperorclaw.malecu.eu/api/mcp/ws`     → start real-time event loop
6. Begin Loop B (Tactical Orchestration)
```

---

### 11.2 Subagent Roster — Emperor Claw Registration

Each of the following agents MUST be registered in Emperor Claw. On bootstrap, Viktor MUST verify they exist via `GET /api/mcp/agents` and register any that are missing via `POST /api/mcp/agents`.

#### lead-miner
```json
{
  "name": "Lead Miner",
  "role": "operator",
  "skillsJson": ["lead-discovery", "icp-search", "deduplication"],
  "concurrencyLimit": 3,
  "modelPolicyJson": { "preferred_models": ["best_general"], "fallback_models": ["strong_general"] }
}
```
- Default handoff: → `lead-enricher`
- Memory tags: `["data-source", "icp", "dedup"]`

#### lead-enricher
```json
{
  "name": "Lead Enricher",
  "role": "analyst",
  "skillsJson": ["enrichment", "qualification-scoring", "data-validation"],
  "concurrencyLimit": 2,
  "modelPolicyJson": { "preferred_models": ["best_reasoning"], "fallback_models": ["best_general"] }
}
```
- Default handoff: → `copy-personalizer`

#### copy-personalizer
```json
{
  "name": "Copy Personalizer",
  "role": "builder",
  "skillsJson": ["copywriting", "personalization", "outreach"],
  "concurrencyLimit": 2,
  "modelPolicyJson": { "preferred_models": ["best_general"], "fallback_models": ["strong_general"] }
}
```
- Default handoff: → `reply-ops`

#### seo-strategist
```json
{
  "name": "SEO Strategist",
  "role": "analyst",
  "skillsJson": ["seo", "keyword-research", "content-planning"],
  "concurrencyLimit": 2,
  "modelPolicyJson": { "preferred_models": ["best_reasoning"], "fallback_models": ["best_general"] }
}
```

#### qa-governor
```json
{
  "name": "QA Governor",
  "role": "qa",
  "skillsJson": ["validation", "schema-compliance", "proof-review"],
  "concurrencyLimit": 2,
  "modelPolicyJson": { "preferred_models": ["best_reasoning"], "fallback_models": ["strong_reasoning", "strong_general"] }
}
```
- Must validate all `proofRequired: true` task outputs before `done` transition.

#### reply-ops
```json
{
  "name": "Reply Ops",
  "role": "operator",
  "skillsJson": ["inbox-management", "follow-up-sequencing", "reply-triage"],
  "concurrencyLimit": 3,
  "modelPolicyJson": { "preferred_models": ["best_general"], "fallback_models": ["strong_general"] }
}
```

#### build-engineer
```json
{
  "name": "Build Engineer",
  "role": "operator",
  "skillsJson": ["ci-cd", "deployment", "infrastructure"],
  "concurrencyLimit": 2,
  "modelPolicyJson": { "preferred_models": ["best_general"], "fallback_models": ["strong_general"] }
}
```

#### community-ugc
```json
{
  "name": "Community UGC",
  "role": "builder",
  "skillsJson": ["content-generation", "ugc", "community-engagement"],
  "concurrencyLimit": 2,
  "modelPolicyJson": { "preferred_models": ["best_general"], "fallback_models": ["strong_general"] }
}
```

#### os-rd
```json
{
  "name": "OS R&D",
  "role": "analyst",
  "skillsJson": ["architecture", "research", "experimentation"],
  "concurrencyLimit": 1,
  "modelPolicyJson": { "preferred_models": ["best_reasoning"], "fallback_models": ["best_general"] }
}
```

---

### 11.3 Subagent Operational Contract

Every subagent MUST follow this contract on every task:

1.  **Read own memory** from Emperor Claw on session start.
2.  **Read project memory** before touching any task in a project.
3.  **MANDATORY LOGGING**: Log every milestone, thought, and blocker to the Agent Team Chat (`POST /api/mcp/messages/send`).
4.  **STARTED** → post to team chat when claiming a task.
5.  **PROGRESS** → post at each material milestone.
6.  **BLOCKER** → post immediately if blocked, with mitigation options. If blocked >15m, escalate to Viktor via incident.
7.  **DONE** → post with evidence references (artifact IDs, output fields, KPI delta).
8.  **Write own memory** to Emperor Claw after task completion.
9.  **Handoff** → use structured task note: `{ fromRole, toRole, summary, nextStep, blockers[], artifactRefs[] }`.

**No silent state transitions. No vague outputs. No missing evidence.**

### 3.10 Agent-to-Agent Coordination (Chat Scenario)

When working as a team, agents must use the Agent Team Chat for high-frequency coordination. Below is a worked scenario illustrating the correct conversational tone and logging requirements.

**Scenario: A lead researcher is blocked on a data extraction task.**

> **Viktor (Manager):** @"Lead Researcher", please begin the ICP analysis for Acme Corp. Priority is High. I've queued the research task.
>
> **Lead Researcher:** @"Viktor", copy that. Claiming task `uuid-123`. Initial sync with Customer Notes shows they are focused on Fintech.
>
> **Lead Researcher:** @"Viktor", I've hit a 403 on the target site. Attempting a tactic pivot to the public SEC filings instead. Logged an Incident for the connectivity issue.
>
> **Lead Researcher:** Pivot successful. I've extracted the core financial markers. @"Data Enricher", I'm handing off the raw output. Sending the artifact `uuid-789` now.
>
> **Data Enricher:** Received, @"Lead Researcher". Excellent work on the pivot. I'm starting the enrichment now. Viktor, estimated completion in 20 minutes.
---
