Skip to main content

Hook event contract for OrchestKit — Pydantic models, JSON schemas, and a thin validation helper. Zero runtime deps beyond pydantic.

Project description

orchestkit-hook-contract

Pydantic v2 + JSON Schema contract for Claude Code hook events. Python sibling of the npm package @orchestkit/hook-contract. Both packages regenerate from the single source of truth at spec/hook-events.spec.yml in the repo root.

Status

  • M141-3: scaffold — Pydantic models, JSON schemas, structural validator, hand-rolled codegen from spec.
  • M141-4 (this change): HMAC signing protocol + reference verifier. Spec at ../hook-contract/docs/signing-rfc.md. Shared golden vectors at ../hook-contract/test-vectors/signing/.
  • M141-6: cross-language parity gate CI — diff this output against the npm side field-by-field.

Install

pip install orchestkit-hook-contract

Requires Python 3.11+ and pydantic>=2.5.

Usage

from orchestkit_hook_contract import (
    HookEvent,
    HOOK_EVENT_NAMES,
    PreToolUsePayload,
    validate_hook_event,
)

# Structural validation
result = validate_hook_event({"event": "PreToolUse"})
if result.valid:
    print("known event:", result.event)

# Typed payload access
p = PreToolUsePayload(tool_name="Bash", tool_input={"cmd": "ls"})
assert p.tool_name == "Bash"

# Pydantic envelope
e = HookEvent(event="PreToolUse", payload={"tool_name": "Bash", "tool_input": {}})

HMAC signing (M141-4)

Sign hook deliveries on the sender and verify on the receiver. Full spec: ../hook-contract/docs/signing-rfc.md.

from orchestkit_hook_contract import sign, verify, HOOK_SIGNATURE_HEADER

# Sender
import json, requests, os
body = json.dumps(event).encode("utf-8")
header = sign(body, os.environ["HOOK_SECRET"])
requests.post(url, headers={HOOK_SIGNATURE_HEADER: header, "Content-Type": "application/json"}, data=body)

# Receiver (Flask / FastAPI / etc. — capture raw body, NOT json.dumps(req.json))
result = verify(
    request.headers.get(HOOK_SIGNATURE_HEADER),
    request.get_data(),  # raw bytes
    os.environ["HOOK_SECRET"],
    tolerance_sec=300,
)
if not result.valid:
    abort(401, result.reason)  # "missing_header" | "malformed_header" | "stale" | "signature_mismatch"

Mirrors the npm sibling byte-for-byte against the same 13 golden vectors. Zero deps beyond hmac + hashlib from the stdlib.

Coverage

13 of the 19 hook events have typed payload classes (PreToolUsePayload, NotificationPayload, etc.). The remaining 6 events stay envelope-only — their CC payloads aren't documented enough to lock down: SessionEnd, Setup, InstructionsLoaded, WorktreeCreate, WorktreeRemove, ConfigChange.

Develop

cd packages/hook-contract-py
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest -v
python scripts/codegen-py.py --check   # drift gate
python scripts/codegen-py.py           # regenerate from spec
mypy src/
ruff check src/ tests/

Why this package exists

The hook event surface is the contract between OrchestKit's TypeScript hook fan-out and any Python receiver (yonatan-hq/platform, custom analytics consumers). When the contracts drift, fields go missing in transit and observability lies. This package + the npm sibling both regenerate from one spec, eliminating that drift class.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orchestkit_hook_contract-0.1.2.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

orchestkit_hook_contract-0.1.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file orchestkit_hook_contract-0.1.2.tar.gz.

File metadata

  • Download URL: orchestkit_hook_contract-0.1.2.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for orchestkit_hook_contract-0.1.2.tar.gz
Algorithm Hash digest
SHA256 556e504394e76488ede7b49928ea14d10325119009238e29bf532e600622e04d
MD5 4255730cf287bad71cd9b0d05b17a05e
BLAKE2b-256 c26f38025296e94be67bf4b726e191a4a5cd98ec73ec92f740da275bce453e87

See more details on using hashes here.

Provenance

The following attestation bundles were made for orchestkit_hook_contract-0.1.2.tar.gz:

Publisher: publish-hook-contract-py.yml on yonatangross/orchestkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file orchestkit_hook_contract-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for orchestkit_hook_contract-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f20ff4bbc88642c8415df5964077793e993aa97e020b679852d25000cb175994
MD5 bfd5ba17d4673832ee79c9cdc6a40ea0
BLAKE2b-256 54ba48b5c2f21a7f7666ce93253a2c843fe04f0d012aeb090e4c40cb15dffe41

See more details on using hashes here.

Provenance

The following attestation bundles were made for orchestkit_hook_contract-0.1.2-py3-none-any.whl:

Publisher: publish-hook-contract-py.yml on yonatangross/orchestkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page