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.1.tar.gz (17.8 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.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for orchestkit_hook_contract-0.1.1.tar.gz
Algorithm Hash digest
SHA256 261307e68449925ac372c4d97925772346a4327d6cf5e678b63d2dd1436b175e
MD5 d222c2fe572a8ea9ad34995916ff96c4
BLAKE2b-256 ebebe17fbe6de02cbfb0a9461342c623633fa17d19734a7b1cbf5cb158d0d8db

See more details on using hashes here.

Provenance

The following attestation bundles were made for orchestkit_hook_contract-0.1.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for orchestkit_hook_contract-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5057f0da09c0d3913c53e98a960391263a5f9733946af4949f72e7c107eda0e1
MD5 7681049a631b07c93ae7c54f109a6b75
BLAKE2b-256 59cd34e06b53dc1a1a68b98becf4d0aa981b824c627ed6d103a2bcbaa302c927

See more details on using hashes here.

Provenance

The following attestation bundles were made for orchestkit_hook_contract-0.1.1-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