-
Notifications
You must be signed in to change notification settings - Fork 1
docs: add script-the-api guide (guides checkpoint 8) #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| --- | ||
| title: "Script the API" | ||
| summary: "Call the daemon API from your own scripts: health handshake, listing and launching sessions, following events with a cursor, and handling structured errors." | ||
| description: "Step-by-step guide to scripting the Coven daemon socket API with curl: health, sessions, session launch, event cursors, and the error envelope." | ||
| read_when: | ||
| - You want your own script or tool to drive Coven instead of the CLI | ||
| - You need the minimal correct client loop: health, launch, events, errors | ||
| --- | ||
|
|
||
| Everything the CLI and every other client does goes through the daemon's local HTTP API. This guide builds the minimal correct client loop with `curl`: handshake, list, launch, follow events, and branch on structured errors. The full contract is the [API reference](/docs/reference/api) and the [OpenAPI pages](/docs/openapi). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - The daemon running — see [Set up the daemon](/docs/guides/set-up-the-daemon). | ||
| - `curl`; `jq` helps but is optional. | ||
| - A project directory and one connected harness for the launch step — see [Connect a harness](/docs/guides/connect-a-harness). | ||
|
|
||
| All examples use the Unix socket. Set the path once: | ||
|
|
||
| ```bash | ||
| SOCK="$HOME/.coven/coven.sock" | ||
| ``` | ||
|
|
||
| ## Step 1 — Start with the health handshake | ||
|
|
||
| Every client should begin every run here: | ||
|
|
||
| ```bash | ||
| curl --unix-socket "$SOCK" http://localhost/api/v1/health | ||
| ``` | ||
|
|
||
| **Expected output:** `"ok": true`, `"apiVersion": "coven.daemon.v1"`, and a capability map. Scripts should verify the `apiVersion` matches the contract they were written for and check `GET /api/v1/capabilities` before using optional features — see [versioning rules](/docs/reference/api#versioning). | ||
|
|
||
| ## Step 2 — List sessions | ||
|
|
||
| ```bash | ||
| curl --unix-socket "$SOCK" http://localhost/api/v1/sessions | ||
| ``` | ||
|
|
||
| **Expected output:** a JSON array of session records with snake_case fields (`id`, `project_root`, `harness`, `title`, `status`, `created_at`, …). An empty array just means no active, non-archived sessions. The record shape is documented under [session records](/docs/reference/api#session-records). | ||
|
|
||
| ## Step 3 — Launch a session | ||
|
|
||
| `POST /api/v1/sessions` validates the project boundary before spawning anything: | ||
|
|
||
| ```bash | ||
| curl --unix-socket "$SOCK" http://localhost/api/v1/sessions \ | ||
| -H 'content-type: application/json' \ | ||
| -d '{ | ||
| "projectRoot": "/Users/you/code/your-repo", | ||
| "cwd": "/Users/you/code/your-repo", | ||
| "harness": "codex", | ||
| "prompt": "explain this repo in 5 bullets", | ||
| "title": "API launch smoke test" | ||
| }' | ||
| ``` | ||
|
|
||
| **Expected output:** `201` with the created session record — capture its `id` for the next step. `cwd` must resolve inside `projectRoot`, `harness` must be a supported harness id, and provider credentials are never part of the request. | ||
|
|
||
| ## Step 4 — Follow events with the cursor | ||
|
|
||
| Poll the event page for your session, then resume from `nextCursor.afterSeq`: | ||
|
|
||
| ```bash | ||
| curl --unix-socket "$SOCK" \ | ||
| "http://localhost/api/v1/events?sessionId=<session-id>&afterSeq=0&limit=100" | ||
| ``` | ||
|
|
||
| **Expected output:** an `events` array (each with `seq`, `kind`, `payload_json`), a `nextCursor`, and `hasMore`. The loop that never loses data: | ||
|
|
||
| 1. Request with your last `afterSeq` (start at `0`). | ||
| 2. Process the returned events. | ||
| 3. Persist `nextCursor.afterSeq` — it survives daemon restarts. | ||
| 4. Repeat while `hasMore` is `true`, then poll on your own interval. | ||
|
|
||
| The daemon caps `limit` at 1000. `GET /api/v1/sessions/<id>/events` is the same read, path-scoped — see [events](/docs/reference/api#events). | ||
|
|
||
| ## Step 5 — Branch on the error envelope | ||
|
|
||
| Force a failure to see the shape every error uses: | ||
|
|
||
| ```bash | ||
| curl --unix-socket "$SOCK" http://localhost/api/v1/sessions/does-not-exist | ||
| ``` | ||
|
|
||
| **Expected output:** | ||
|
|
||
| ```json | ||
| { | ||
| "error": { | ||
| "code": "session_not_found", | ||
| "message": "Session was not found.", | ||
| "details": { "sessionId": "does-not-exist" } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Branch on `error.code`, never on message text. The stable codes (`not_found`, `invalid_request`, `session_not_found`, `session_not_live`, `project_root_violation`, `pty_spawn_failed`, `runtime_unavailable`, `internal_error`) and their HTTP statuses are tabulated in the [error envelope reference](/docs/reference/api#error-envelope). A robust script treats `runtime_unavailable` as retry-after-status, and `session_not_live` as "switch to replay, do not send input." | ||
|
BunsDev marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Troubleshooting | ||
|
|
||
| - **`curl` cannot connect** — daemon down or wrong socket path for your `COVEN_HOME`; see [Set up the daemon](/docs/guides/set-up-the-daemon). | ||
| - **`invalid_request` on launch** — compare your body against Step 3's fields; an unknown harness id lands here too. | ||
| - **`project_root_violation`** — your `cwd` escapes `projectRoot`; see [`cwd` rejected](/docs/reference/troubleshooting#cwd-rejected). | ||
| - **Events stop arriving** — the session likely completed; check its `status` via `GET /api/v1/sessions/<id>`. | ||
| - **Version mismatch after an upgrade** — see [API version rejected](/docs/reference/troubleshooting#api-version-rejected) and [Upgrade Coven](/docs/guides/upgrade-coven). | ||
|
|
||
| ## Related | ||
|
|
||
| - [API reference](/docs/reference/api) | ||
| - [API architecture diagrams](/docs/reference/api-architecture) | ||
| - [OpenAPI endpoint pages](/docs/openapi) | ||
| - [Socket API overview](/docs/daemon/socket-api) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.