Space Hooks & Webhooks
Trigger shell commands, Agent prompts, or published App Actions automatically on Space events and inbound HTTP webhooks using declarative files under .cohub/hooks/.
This page explains how to react automatically to Space activity and external HTTP calls without maintaining a polling loop: place declarative hook files under .cohub/hooks/ to run shell commands, send Agent prompts, or invoke published App Actions whenever matching events or inbound webhooks arrive.
How Space Hooks work
Each hook is a top-level .yml, .yaml, or .json file directly inside .cohub/hooks/ in a Space workspace (subdirectories are not loaded). Every hook file must declare:
schema: cohub.space-hook.v1on: the event name (on.event) and any event-specific filters- Exactly one action:
run,prompt, oruses
Dispatch, caching, and loop protection
- Two-stage execution: Broadcast Space events first run through an internal dispatch job (
space_hook.dispatch) that loads hook definitions from a Redis cache (5-minute TTL when definitions exist; 30-second negative TTL when.cohub/hooks/is empty). The dispatch step never writes atask_runsrow. - Cache refresh: The cache automatically invalidates when
space.workspace.readyfires or when any file under.cohub/hooks/changes. - Execution history: Only when at least one hook matches (or an inbound webhook resolves to a hook) does Cohub enqueue a
space_hookTask run, verify each matched hook's SHA-256 definition fingerprint, and execute it under the Space owner's identity. - Re-entrancy guard:
.cohub/**paths are always ignored byspace.fs.changedhooks, and hook-generatedsession.turn.finalizedandtask.updatedevents are filtered out so hooks cannot trigger themselves in an infinite loop.
Events and filters
on.event |
When it fires | Supported filters under on |
|---|---|---|
space.fs.changed |
Workspace files are created, modified, deleted, or renamed (resync-only signals with no concrete paths do not fire) | paths (glob list), ignore (glob list; .cohub/** is always ignored), kinds (create, modify, delete, rename) |
space.workspace.ready |
The Space workspace becomes ready | None |
session.turn.finalized |
A Chat turn finishes (excluding hook-spawned turns) | sessionIds, ignoreSessionIds, sources (turn meta.source), labels (any, all, none session label refs) |
checkpoint.created |
A Space Save (checkpoint) is created | None |
app.version.published |
A Space App version is published | None |
task.updated |
A Task run changes state (excluding hook-generated child tasks) | None |
webhook |
External or CLI POST to /api/spaces/:id/webhooks/<name> (never triggered by broadcast events) |
secret (optional shared secret, max 256 characters) |
on.labels is valid only for session.turn.finalized, and on.secret is valid only for webhook.
Actions: run, prompt, and uses
Each hook file declares exactly one action.
run: execute a shell command
Runs a shell command in the Space's Sandbox as a child run_command task. Supports optional timeoutSecs (default 600, max 1800) and env.
schema: cohub.space-hook.v1
on:
event: space.fs.changed
paths:
- "src/**"
ignore:
- "**/*.tmp"
kinds:
- create
- modify
timeoutSecs: 300
env:
NODE_ENV: test
run: |
set -euo pipefail
echo "Changed files (${COHUB_HOOK_FS_CHANGE_COUNT}):"
printf '%s\n' "${COHUB_HOOK_FS_PATHS}"
prompt: send an Agent prompt
Sends a prompt to an existing Chat (prompt.sessionId, validated to belong to the same Space) or starts a new Chat when sessionId is omitted. You can write prompt as a plain string or as an object:
schema: cohub.space-hook.v1
on:
event: session.turn.finalized
labels:
any:
- "Review"
prompt:
title: "Post-turn check"
text: "Inspect the latest turn output and summarize any follow-up work."
intent: followup
accessMode: read_only
model: claude-sonnet-4-5
labelRefs:
- "Automated"
Supported prompt fields: text (required; content is accepted as an alias), sessionId, title, intent (followup or steer), accessMode (read_only or full_access), model, provider, thinkingLevel, and labelRefs. Cohub automatically appends a --- / Hook context block containing non-empty event metadata, changed file paths, and (for webhooks) the JSON request body.
uses: invoke a published App Action
Calls a published directory App Action addressed as username/spaceSlug/appSlug/action, passing optional JSON-serializable with input (max 16 KB) on stdin:
schema: cohub.space-hook.v1
on:
event: checkpoint.created
uses: acme/tools/notifier/send
with:
channel: releases
Hook context and environment variables
Cohub exports system COHUB_HOOK_* variables into every run and uses execution (and mirrors non-empty values into the prompt context appendix). User-declared env keys can never override system COHUB_HOOK_* keys. Optional keys are exported as empty strings so shell scripts using set -u can expand them safely.
- Always present:
COHUB_HOOK_PATH,COHUB_HOOK_TASK_RUN_ID,COHUB_HOOK_EVENT_ID,COHUB_HOOK_EVENT_TYPE,COHUB_HOOK_SPACE_ID,COHUB_HOOK_OCCURRED_AT,COHUB_HOOK_EXECUTION_USER_ID. - Event-specific:
COHUB_HOOK_ACTOR_USER_ID,COHUB_HOOK_SESSION_ID,COHUB_HOOK_TURN_ID,COHUB_HOOK_CHECKPOINT_ID,COHUB_HOOK_APP_ID,COHUB_HOOK_APP_VERSION_ID,COHUB_HOOK_APP_VERSION. - Filesystem (
space.fs.changed):COHUB_HOOK_FS_CHANGE_COUNT,COHUB_HOOK_FS_PATHS(newline-separated, capped at 100 paths),COHUB_HOOK_FS_KINDS(comma-separated). - Tasks (
task.updated):COHUB_HOOK_TASK_ID,COHUB_HOOK_TASK_TYPE,COHUB_HOOK_TASK_STATUS,COHUB_HOOK_TASK_CHANGED,COHUB_HOOK_TASK_ERROR. - Webhooks (
webhook):COHUB_HOOK_WEBHOOK_NAME,COHUB_HOOK_WEBHOOK_BODY(raw JSON string, ornullwhen empty).
The complete event payload is also saved on the space_hook Task run (payload.data.event) and can be fetched with cohub tasks get <taskRunId> --json or GET /api/tasks/:id.
Inbound webhooks
Declare an inbound HTTP webhook by creating .cohub/hooks/<name>.yml (or .yaml / .json) with on.event: webhook. The filename stem <name> must match ^[a-z0-9][a-z0-9_-]{0,63}$.
schema: cohub.space-hook.v1
on:
event: webhook
secret: whsec_replace_me
prompt:
title: "CI failure triage"
text: "Inspect the webhook payload and check failing tests in the workspace."
Calling and testing webhooks
- Trigger endpoint:
POST /api/spaces/:id/webhooks/<name>accepts an optional JSON body up to 64 KB (an empty body becomesnull) and returns HTTP 202 with{ taskRunId, hook, eventId }. - Authentication: The trigger endpoint does not require a Cohub user token. When
on.secretis set, callers supply it via thex-cohub-webhook-secretheader or?secret=<value>query parameter (compared in constant time). Omiton.secretonly if any caller with the Space ID should be able to trigger the hook. - Forwarded headers: Only
content-type,user-agent, andx-request-idare forwarded into the event payload; authorization headers, cookies, and signatures are dropped. - Listing endpoint:
GET /api/spaces/:id/webhooksrequiresfile.viewpermission and returns{ name, path, action, hasSecret }—secret values are never exposed. - CLI management and local testing: Webhooks are managed via
.cohub/hooks/files, the HTTP API, andcohub spaces webhooks(there is no separate Webhooks UI page):
# List declared webhook hooks in a Space
cohub -s <space> spaces webhooks ls
# Print the full URL external services should POST to
cohub -s <space> spaces webhooks url ci-triage
# Trigger a webhook locally with a JSON payload and secret
cohub -s <space> spaces webhooks trigger ci-triage \
--body '{"branch":"main","status":"failed"}' \
--secret whsec_replace_me
# Inspect recent hook executions
cohub tasks ls --space <space> --type space_hook
Boundaries and common errors
Hard limits and execution boundaries
- File placement: Only top-level
.yml,.yaml, or.jsonfiles directly under.cohub/hooks/are loaded. - Payload and definition limits:
runandprompttext max 64 KB;withJSON max 16 KB;on.secretmax 256 characters; webhook HTTP body max 64 KB;timeoutSecsdefaults to 600 s and is capped at 1800 s;COHUB_HOOK_FS_PATHSlists up to 100 paths. - Webhook rate limit: 60 requests per minute per Space (
429 too many webhook requests). - Execution model: Hooks run under the Space owner's identity with no separate per-hook permission or approval step and no hook-level concurrency limit. Failed hooks are recorded in the
space_hookTask run result without automatic queue retries. - Cloud workspace loading: Hook definitions are loaded from the cloud Space workspace; a Local Runtime filesystem is not mounted on the cloud worker.
Common errors
| Error / symptom | Cause | Fix |
|---|---|---|
404 webhook not found |
Unknown Space ID, invalid or missing <name>, or wrong/missing on.secret (intentionally the same 404 so callers cannot probe names or secrets). |
Check cohub -s <space> spaces webhooks ls and verify the x-cohub-webhook-secret header or ?secret= value. |
409 webhook name is ambiguous; rename one of the hook files |
Two files share the same stem (for example deploy.yml and deploy.json) with on.event: webhook. |
Rename or remove one of the conflicting files under .cohub/hooks/. |
400 webhook body must be JSON / 413 webhook body exceeds 65536 bytes |
Request body is not valid JSON or exceeds 64 KB. | Send valid JSON under 64 KB (or an empty body). |
429 too many webhook requests |
Space exceeded 60 webhook requests per minute. | Back off and retry after the current minute window. |
unsupported hook schema ...; expected cohub.space-hook.v1 / exactly one of run, prompt or uses is required |
Missing/wrong schema, or zero/multiple actions defined in one file. |
Set schema: cohub.space-hook.v1 and keep exactly one of run, prompt, or uses. |
on.labels is only supported for session.turn.finalized / on.secret is only supported for webhook |
Filter used on an incompatible event type. | Remove the filter or switch on.event to the supported event. |
app not found / app is not published / app actions require a directory app |
uses points to an unpublished, missing, or non-directory App. |
Publish the directory App and use the exact username/spaceSlug/appSlug/action reference. |
| File change hook did not fire | Edited files under .cohub/**, received a resync-only event without paths, or the file sits in a subdirectory of .cohub/hooks/. |
Place hook files directly in .cohub/hooks/ and verify on.paths / on.ignore / on.kinds against non-.cohub/ files. |
Next steps
- Tasks & Scheduled prompts — inspect
space_hookTask runs and set up time-based schedules - Mods, Skills & Prompts — share reusable instructions and skills with hook-triggered Agents
- Files & Sandbox — how workspace files and Sandbox commands run
- App development — build and publish directory App Actions for
useshooks - CLI — full
cohubcommand reference