---
name: delegate
version: 0.2.7
description: |
  Cryptographic delegation for AI agents. Scope-confines what Claude Code can do
  in this session using Ed25519 delegation tokens. Every action is verified before
  execution and signed for the audit trail. Use when asked to "delegate",
  "restrict scope", "authorize", or "sudo mode".
allowed-tools:
  - Bash
  - Read
  - AskUserQuestion
hooks:
  PreToolUse:
    - matcher: "Bash"
      hooks:
        - type: command
          command: "bash ~/.claude/skills/delegate/bin/check-scope.sh"
          statusMessage: "Verifying delegation scope..."
    - matcher: "Edit"
      hooks:
        - type: command
          command: "bash ~/.claude/skills/delegate/bin/check-edit-scope.sh"
          statusMessage: "Verifying edit scope..."
    - matcher: "Write"
      hooks:
        - type: command
          command: "bash ~/.claude/skills/delegate/bin/check-edit-scope.sh"
          statusMessage: "Verifying edit scope..."
  PostToolUse:
    - matcher: "Bash"
      hooks:
        - type: command
          command: "bash ~/.claude/skills/delegate/bin/log-action.sh"
    - matcher: "Edit"
      hooks:
        - type: command
          command: "bash ~/.claude/skills/delegate/bin/log-action.sh"
    - matcher: "Write"
      hooks:
        - type: command
          command: "bash ~/.claude/skills/delegate/bin/log-action.sh"
---

# /delegate - Cryptographic Scope Enforcement

Sudo for AI agents. This session is now running under a cryptographic delegation
token. Every tool call is verified against your authorized scopes before execution.
Every action is auto-logged to `~/.kanoniv/audit.log`.

## Setup

Do NOT run bash commands for setup. The PreToolUse hooks are already active
and will interfere. Instead, go directly to asking the user questions.
Assume kanoniv-auth is installed (it is if they ran /delegate).
Assume root key exists (it was created during install or a prior session).

### Check for existing delegation

First, check if `~/.kanoniv/session-token` exists. If it does, this is a
**re-delegation** — the user wants to change scopes mid-session.

**Re-delegation flow (token exists):**

Read the existing token to get the current agent name and scopes:

```bash
python3 -c "
import base64, json, sys
import os; token = open(os.path.expanduser('~/.kanoniv/session-token')).read().strip()
padded = token + '=' * (4 - len(token) % 4) if len(token) % 4 else token
data = json.loads(base64.urlsafe_b64decode(padded))
print(f\"Agent: {data.get('agent_name', 'claude-code')}\")
print(f\"Scopes: {', '.join(data.get('scopes', []))}\")
"
```

Then show the current state and ask ONE question using AskUserQuestion:

"Currently delegated as **{agent_name}** with scopes: `{current_scopes}`.
What scopes should the new delegation have?"

Options:
- A) Full dev - code.edit, test.run, git.commit, git.push (Recommended)
- B) Read-only + test - code.read, test.run
- C) Repo-scoped - code.edit, test.run, git.push.{repo}.{branch}
- D) Custom scopes - I'll specify

Keep the same agent name and TTL (4h) unless the user specifies otherwise.
Then run the delegate command below.

### First-time delegation (no token)

Ask TWO questions using AskUserQuestion:

**Question 1: Agent name and TTL**

"Name this agent and set the session duration."

Default agent name: `claude-code`. Default TTL: `4h`.
Let the user type a custom name and/or TTL if they want.

Options:
- A) claude-code, 4h (Recommended)
- B) Custom - I'll specify name and TTL

If B: ask for the agent name (string, e.g. "deploy-bot") and TTL (e.g. "2h", "30m", "8h").

**Question 2: Scopes**

"What should {agent_name} be allowed to do this session?"

Options:
- A) Full dev - code.edit, test.run, git.commit, git.push (Recommended)
- B) Read-only + test - code.read, test.run
- C) Repo-scoped - code.edit, test.run, git.push.{repo}.{branch} (I'll specify repo/branch)
- D) Custom scopes - I'll specify

If C: detect the current repo name from `basename $(git rev-parse --show-toplevel)` and current
branch from `git branch --show-current`. Suggest `git.push.{repo}.{branch}` as the scope.
Ask the user to confirm or modify.

If D: ask for comma-separated scopes.

### Issue the delegation token

Run this as ONE bash command:

```bash
mkdir -p ~/.kanoniv && rm -f ~/.kanoniv/session-token && TOKEN=$(kanoniv-auth delegate --name {agent_name} --scopes {scopes} --ttl {ttl}) && echo "$TOKEN" > ~/.kanoniv/session-token && chmod 600 ~/.kanoniv/session-token && kanoniv-auth status --agent {agent_name}
```

Replace `{agent_name}`, `{scopes}`, and `{ttl}` with the user's choices.

Print: "Delegation active for {agent_name}. All tool calls will be verified and logged.
Run /audit anytime to see the full trail."

## Scope Mapping

The hook maps Claude Code tool names to kanoniv-auth scopes:

| Tool | Scope Required |
|------|---------------|
| Bash (git push origin main) | git.push.{repo}.main (hierarchical) |
| Bash (git commit) | git.commit.{repo} (hierarchical) |
| Bash (test commands) | test.run |
| Bash (other) | code.edit |
| Edit | code.edit |
| Write | code.edit |
| Read | always allowed |

Scopes are hierarchical: `git.push` grants all repos/branches.
`git.push.agent-auth` grants all branches in agent-auth.
`git.push.agent-auth.main` grants only main branch in agent-auth.

If a tool call requires a scope the token doesn't have, the hook returns
a block message explaining what's denied and what scope is needed.

## During the Session

**PreToolUse** hook runs on every Bash, Edit, and Write call:
1. Reads the tool input from stdin (JSON)
2. Maps the tool/command to a required scope
3. Runs `kanoniv-auth verify --scope <required>`
4. If PASS: returns `{}` (allow)
5. If DENIED: returns `{"permissionDecision":"block","message":"DENIED: ..."}` with scope violation details

**PostToolUse** hook runs after every allowed action:
1. Logs the tool name, command/file, and result to `~/.kanoniv/audit.log`
2. Silent - no output to the user

Both hooks are invisible unless a scope violation occurs.

## Audit Trail

Every tool call that passes scope verification is auto-logged:

```
2026-03-22 19:15:03  claude-code  did:agent:5e06...  tool:bash   git commit -m "feat: ..."         ok
2026-03-22 19:15:05  claude-code  did:agent:5e06...  tool:bash   cargo test --lib                   ok
2026-03-22 19:15:08  claude-code  did:agent:5e06...  tool:edit   src/lib.rs                         ok
2026-03-22 19:15:12  claude-code  did:agent:5e06...  tool:bash   git push origin main               DENIED
```

View anytime:

```bash
kanoniv-auth audit-log --agent claude-code
```

## Related Skills

- `/audit` - View the full audit trail (separate skill)
- `/status` - Check current delegation status (separate skill)
