Cheatsheets

Idiomatic patterns for Python, Go, SQL, Git, and Docker — written for engineers who know the language but forget the exact syntax. Switch languages to see how the same pattern looks in each.

Session Runtime

Create / Resume Session

Keep session identity explicit so the runtime can resume, fork, and reconnect.

Python
from dataclasses import dataclass

@dataclass
class Session:
    session_id: str
    session_key: str
    websocket_url: str | None = None


def create_or_resume_session(api, workspace_id: str, session_key: str) -> Session:
    payload = {
        "workspace_id": workspace_id,
        "client_kind": "cli",
        "permission_mode": "default",
        "resume_session_key": session_key,
    }
    data = api.post("/sessions", json=payload).json()
    return Session(**data)

Submit User Turn

Treat each turn as work against a session, not a standalone chat completion.

Python
def submit_turn(api, session_id: str, message: str) -> str:
    payload = {
        "message": message,
        "fork_context": True,
    }
    data = api.post(f"/sessions/{session_id}/messages", json=payload).json()
    return data["request_id"]
Permission Gating

Resolve Approval Request

The runtime blocks a risky tool call until the client returns allow or deny.

Python
def resolve_permission(api, session_id: str, request_id: str, allow: bool) -> None:
    payload = {
        "behavior": "allow" if allow else "deny",
        "persist_rule": False,
    }
    api.post(
        f"/sessions/{session_id}/permission-requests/{request_id}",
        json=payload,
    ).raise_for_status()

Allow / Ask / Deny Decision

Simple policy evaluation before executing a tool.

Python
from fnmatch import fnmatch

def permission_decision(tool_name: str, path: str, allow: list[str], deny: list[str]) -> str:
    if any(fnmatch(path, rule) for rule in deny):
        return "deny"
    if any(fnmatch(path, rule) for rule in allow):
        return "allow"
    return "ask"
Delegation

Spawn Bounded Subagent

Delegate a side task with clear scope and a capped loop.

Python
def spawn_subagent(api, session_id: str, task: str) -> str:
    payload = {
        "task": task,
        "agent_type": "explorer",
        "max_turns": 4,
    }
    data = api.post(f"/sessions/{session_id}/agents", json=payload).json()
    return data["agent_id"]

Integrate Child Results

Keep parent/child responsibilities clear when merging back.

Python
def integrate_results(parent_notes: list[str], child_findings: list[str]) -> list[str]:
    seen = set(parent_notes)
    merged = list(parent_notes)
    for finding in child_findings:
        if finding not in seen:
            merged.append(finding)
            seen.add(finding)
    return merged