API reference

API time & episodes

Time Chamber repeats a run against your mocked dependencies from a known starting point. You take a snapshot of the proxy's MOCK state, run a scenario inside an isolated episode, move that episode's virtual clock forward when the scenario needs time to pass, then reset the episode to the snapshot and run the scenario again. Each episode has its own resource state, its own seeded chaos stream and its own clock, and every response it serves is written to a journal you can compare between runs.

Enabled in the standard install

The Compose files, for both the Full and the Lite stack, and the .env template turn episodes on with GOSTLY_EPISODES=1. Time Chamber runs in ghost-proxy and does not need the AI services. The proxy writes episode journals to /data/trajectories and snapshot artifacts to /data/snapshots on the data volume.

  • Check it. docker compose config ghost-proxy | grep GOSTLY_EPISODES prints GOSTLY_EPISODES: "1". If it prints nothing, run git pull in the install directory to get the current Compose files, and keep your existing .env.
  • Turn it off. Set GOSTLY_EPISODES=0 in .env, then run docker compose up -d ghost-proxy.
  • Serve from MOCK. A snapshot needs the global mode (the Default mode on the dashboard's Services page) and every service route in MOCK. Otherwise the proxy answers 409 environment_not_deterministic and names the reason, such as global_mode_must_be_mock or all_episode_routes_must_be_mock.
  • Seed chaos. If chaos can fire in MOCK, set CHAOS_SEED in .env to an unsigned 64-bit integer and run docker compose up -d ghost-proxy before taking a snapshot. The seed also applies to chaos outside episodes.

The control routes are on the proxy's port, 8080, and take the same service key as the API, in the X-API-Key header (see service authentication). The examples read it from .env:

export GHOST_API_KEY="$(grep '^GHOST_API_KEY=' .env | cut -d= -f2- | tr -d "'\"")"
: "${GHOST_API_KEY:?No GHOST_API_KEY in .env, copy the key from Settings in the dashboard}"

If your .env has no GHOST_API_KEY line, an admin can copy the service key from Settings in the dashboard.

The workflow: snapshot, run, advance, reset, repeat

1. Take a snapshot

A snapshot captures default-namespace resources for _global and the services in the agent's applied routing table, together with the seeded chaos position and virtual clock. Services absent from that table are excluded. On a standalone agent with no API configured and no routing table ever pushed, it captures every default-namespace service resource. Snapshot and episode names use letters, digits, _ and -, up to 64 characters.

An agent configured with an API must receive its first routing table before a snapshot can succeed. Until then, a snapshot can return 409 routing_table_pending; wait for the table to arrive, then retry. Other snapshot admission checks still apply.

curl -sS -X POST http://localhost:8080/ghost/snapshot \
  -H "X-API-Key: $GHOST_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"name": "baseline"}'

Through POST /v1/time-chamber/snapshots, which the dashboard's Snapshot control also uses, every 409 refusal from the agent, this one and the readiness checks above (global_mode_must_be_mock, all_episode_routes_must_be_mock) included, is recorded as a failed operation with error_code: episode_conflict. After resolving the cause, retry with a new Idempotency-Key. Repeating the same request under the old key returns its recorded result without dispatching again; a different request cannot reuse that key. The direct-agent curl above does not use this API operation key.

2. Start an episode from it

A reset in snapshot mode creates the episode, or restores an existing one, from the named snapshot. The response carries a reset_correlation_id that identifies this generation of the episode's journal.

curl -sS -X POST http://localhost:8080/ghost/reset \
  -H "X-API-Key: $GHOST_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"episode": "checkout-run", "from": "baseline", "mode": "snapshot"}'

3. Run the scenario through the proxy

Send your usual requests to the proxy with three headers on each one. The proxy never forwards these headers to an upstream. Give each step its own action ID and reuse the same IDs every time you repeat the run, so the journals line up step by step.

curl -sS http://localhost:8080/v1/orders/42 \
  -H 'X-Gostly-Episode: checkout-run' \
  -H 'X-Gostly-Run-Id: attempt-1' \
  -H 'X-Gostly-Action-Id: step-01'

4. Advance virtual time

The episode's clock starts at 2020-01-01T00:00:00Z and moves only when you call this route, by up to ten years per call. The response returns the new virtual_time. Eligible statechart timers that fall due run during the advance. An advance that would fire a timer for a service absent from the applied routing table is refused with 409 service_unrouted and a list of service_ids, before changing the clock or resources.

curl -sS -X POST http://localhost:8080/ghost/episodes/checkout-run/clock \
  -H "X-API-Key: $GHOST_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"advance_ms": 60000, "run_id": "attempt-1", "action_id": "advance-60s"}'

Check the listed services and wait for the intended routing table to be applied. Take a new snapshot and reset the episode from it to use that routing scope. If you intend to include an archived service, you can first restore it, subject to route and service-limit checks; a deleted service cannot be restored. A routing change can also invalidate the episode's existing snapshot, so restoring a service alone may not repair the episode.

5. Reset and repeat

Call the reset from step 2 again. It restores the episode's resources, chaos position, virtual clock and rate-limit windows to the snapshot and starts a new journal generation. Then run step 3 again with the same action IDs, for example under run ID attempt-2, and compare the two runs.

6. Inspect and clean up

GET /ghost/episodes lists live episodes and snapshots with their virtual time and journal state. In the dashboard, the Time Chamber page, available to admins and owners, lists the same episodes and opens each one's journal. Close an episode with DELETE /ghost/episodes/checkout-run and remove a snapshot with DELETE /ghost/snapshots/baseline, both with the X-API-Key header.

Example: rate-limit recovery

A support app refreshes its available model options. Two requests succeed. The next returns 429 Too Many Requests with Retry-After: 30. The Guide combines the walkthrough, request sequence and saved evidence in one place.

Open the rate-limit recovery walkthrough

API time and application time are separate

Time Chamber advances the simulated API clock in this scenario. It does not advance browser timers, worker sleeps or an SDK's retry scheduler. Control those in your client test harness when needed. Advancing the API clock and retrying are deliberately separate actions here.

Try it with your application

Use a licensed, isolated Gostly installation for a disposable test. Follow the current installation guide and the setup instructions on this page. Prepare the dependency through Learn and Build, or authored mocks, before configuring the condition you want to test.

Start with one concrete assertion, such as: “When this refresh is rate limited, keep the previous result visible; when a later request succeeds, replace it.” Point your test client at the prepared mock API, make the requests, advance API time explicitly, and let the client or test harness decide when to retry. Record the observed application result alongside the HTTP responses.

A plain mock and your test runner's timers may already cover a single retry function. Evaluate this workflow when controlling and repeating an HTTP dependency's conditions would make your integration test easier to run or inspect.

Set up a local Gostly test

Expiring resources inside an episode: the X-Gostly-Episode-Create opt-in

A resource that an episode creates, such as an access token, can expire on that episode's clock. You opt a recorded create into this with an X-Gostly-Episode-Create header on its recorded response. Inside an episode, the proxy then answers the create with a new id, keeps the new resource in the episode's own state with the episode's virtual time as its creation time, and refuses reads of it and statechart transitions on it once its lifetime has passed on the episode clock. Outside an episode, the same create serves its recorded response.

Follow the expiring-resource test for a complete before, at and after expiry plan that controls the application scheduler separately. The contract and configuration below define the supported resource behavior.

The contract header

X-Gostly-Episode-Create: <id-kind>;[prefix=<prefix>;]pointers=<pointer>[,<pointer>...][;url-pointers=<pointer>[,<pointer>...]]

For example, opaque-v1;prefix=tok_;pointers=/id declares that the create returns an id such as tok_qa7k2m9p4x in its top-level id field.

  • <id-kind> is the shape of the id the create returns. The new id has the same shape.
    • opaque-v1: the prefix given in prefix= (1 to 32 letters, digits, _ or -), then at least 8 letters, digits, _ or - that allow at least 2^37 values: each letter counts 26, each digit 10, and _ and - count 1. For example, 8 letters or 12 digits qualify; 8 digits do not.
    • uuid-v4: a lowercase canonical UUID.
    • js-safe-integer: a decimal integer of 12 to 16 digits, no larger than 9007199254740991.
  • pointers= lists 1 to 16 JSON pointers (RFC 6901) to the response-body fields that hold the id, for example /id or /id,/links/id. Only these fields receive the new id. The list must include the field the proxy reads the id from (see What the create needs), and every listed field must hold exactly the recorded id; otherwise the create answers 409 episode_create_rewrite_failed.
  • url-pointers= is optional. It lists 1 to 16 JSON pointers to URL fields whose path contains the id, for example /links/self, and repeats no field from pointers=.
  • The parameters come in this order, separated by ; without spaces. An unknown kind or parameter, another order or a second contract header answers 409 invalid_episode_create_contract. An id that does not fit its kind answers 409 episode_create_id_unsupported. A refused create carries X-Ghost-Resource: refused and creates nothing.

What the create needs

  • A recorded create. A POST that answered 200 or 201 with a JSON body, on a registered service's route, at a collection path such as /auth/tokens. The proxy reads the recorded id from a field named id, _id or the collection name without a trailing s plus _id (token_id for /auth/tokens), at the top level or nested up to three levels deep. A body without one, such as a token response that carries only access_token, answers 409 episode_create_id_missing.
  • A recorded read of it. A GET on the same service for the resource by its recorded id, such as GET /auth/tokens/tok_qa7k2m9p4x, answering 200 with the same id. Without it the create answers 409 episode_create_route_unproven.
  • A lifetime. A non-negative integer expires_in, in seconds, at the top level of the create's response body. The resource expires at its creation time on the episode clock plus expires_in. A create without expires_in makes a resource that does not expire.

Adding the header to a recording

  • Your own service. Send the header on the create response while you record in LEARN, and record one GET of the created resource by its id. The proxy keeps the header with the recording and removes it from every response a client receives.
  • A third-party API, or any recording you cannot change at the source. Add the contract through the mock API on port 8000, with the same service key, after the create is recorded:
    1. Find the recorded create with GET /v1/mocks?service_id=<service id>&method=POST. The list is newest first, up to limit entries per page (200 by default, at most 1000). While the response's next_cursor is not null, request the next page with &cursor=<next_cursor>. Each entry carries its request and response; GET /v1/services lists the service ids.
    2. Add the create again with POST /v1/mocks: the same method, path and request body, the recorded status, headers and body, and the contract header added to the response headers. For one method and path on a service, the proxy serves the entry added last, so this entry replaces the recorded one. Recording the create again in LEARN adds a newer entry without the contract header, which then replaces this one, so repeat steps 1 to 4 after you re-record.
    3. Add the read with POST /v1/mocks: GET on the create path plus the recorded id, status 200, the same body.
    4. Take a new snapshot. A reset from a snapshot taken before the change answers 409 snapshot_environment_drift.
curl -sS -X POST http://localhost:8000/v1/mocks \
  -H "X-API-Key: $GHOST_API_KEY" \
  -H 'Content-Type: application/json' \
  -d @- <<'EOF'
{
  "service_upstream_id": "<service id>",
  "request_method": "POST",
  "request_uri": "/auth/tokens",
  "request_body": "{\"client\": \"qa\"}",
  "response_status": 201,
  "response_headers": {
    "Content-Type": "application/json",
    "X-Gostly-Episode-Create": "opaque-v1;prefix=tok_;pointers=/id"
  },
  "response_body": "{\"id\": \"tok_qa7k2m9p4x\", \"token_type\": \"Bearer\", \"expires_in\": 3600}"
}
EOF

curl -sS -X POST http://localhost:8000/v1/mocks \
  -H "X-API-Key: $GHOST_API_KEY" \
  -H 'Content-Type: application/json' \
  -d @- <<'EOF'
{
  "service_upstream_id": "<service id>",
  "request_method": "GET",
  "request_uri": "/auth/tokens/tok_qa7k2m9p4x",
  "response_status": 200,
  "response_headers": {"Content-Type": "application/json"},
  "response_body": "{\"id\": \"tok_qa7k2m9p4x\", \"token_type\": \"Bearer\", \"expires_in\": 3600}"
}
EOF

Recipe: a token that expires after an hour

  1. Take a snapshot and start an episode from it (workflow steps 1 and 2).
  2. Send the create inside the episode, with the three episode headers. It answers with the recorded status and body and a new id, for example 201 {"id": "tok_di3f2g8k6z", ...}.
  3. Read the new resource: GET /auth/tokens/tok_di3f2g8k6z with the episode headers answers 200 with X-Ghost-Resource: true.
  4. Advance the episode clock by the lifetime: {"advance_ms": 3600000} for "expires_in": 3600.
  5. Read it again. It answers 401 with X-Ghost-Resource: expired from the instant the virtual time reaches expired_at:
    {"error": "invalid_token", "expired_at": "2020-01-01T01:00:00+00:00", "virtual_now": "2020-01-01T01:00:00+00:00", "hint": "the recorded expires_in elapsed on the episode's virtual clock; mint a fresh token inside the episode"}
  6. Reset the episode from the snapshot and send the create again. It creates the token again with a different id, and reading that id answers 200.

Limits

  • Virtual time stays in the proxy. The clock belongs to the episode. It changes what Gostly serves (statechart timers, the lifetimes of resources an episode creates, recorded rate-limit windows and seeded Markov chaos), not the clock of your application, its database or the host. Application code that reads its own clock still sees real time.
  • A proxy restart ends live episodes and snapshots. They are held in the proxy's memory. After ghost-proxy restarts, requests for an earlier episode answer 404 episode_not_active and a reset from an earlier snapshot answers 409 no_snapshot, so take a new snapshot. Journals and snapshot artifacts already written stay on the data volume.
  • Episodes must be enabled. With GOSTLY_EPISODES off, the control routes answer 403 episodes_disabled and requests carrying X-Gostly-Episode answer 409 episodes_disabled.
  • Same steps, same order. A repeat reproduces a run when it sends the same requests in the same order with the same clock advances. Concurrent requests inside one episode can arrive in a different order.
  • Serving changes need a new snapshot. If mocks, routes, mode, chaos or statecharts change after a snapshot, a reset from it answers 409 snapshot_environment_drift. Reset restores the episode, not the proxy's configuration.
  • Quotas. By default the proxy holds up to 64 live episodes and 64 snapshots. A new episode over the limit answers 429.
  • Idle episodes and snapshots expire. An episode with no activity for an hour is closed, and its journal and resources are removed. A snapshot that no reset has used for 24 hours is removed with its artifact.
  • A recorded lifetime starts at the recording's wall-clock time. A token recorded in LEARN with expires_in keeps an expiry anchored to the wall-clock time of the recording, while the episode clock starts at 2020-01-01T00:00:00Z, so advancing an episode by an hour does not expire it. The proxy never reads a JWT's exp claim. For a token that must expire inside an episode, use the X-Gostly-Episode-Create opt-in and read the token by its id: other requests that carry it, for example in an Authorization header, are served as usual.
  • Created ids change on every reset. After a reset, each opted-in create returns a new id, even with the same run and action IDs, so responses that carry the id differ between repeats.

Next steps