Durable promises, signals and awakeables
Three primitives let an invocation wait for something that happens outside it — an approval, a webhook, a click in an email. All three suspend the invocation durably: it can wait for weeks without holding a process.
| Primitive | Addressed by | Available in | Completed by |
|---|---|---|---|
| Durable promise | service + key + promise name | Virtual objects, workflows | Another handler on the same object, or POST /promises/... |
| Signal | invocation ID + signal name | Every handler | POST /invocations/{id}/signal, ctx.sendSignal, the client |
| Awakeable | an opaque awk_... ID |
Every handler | POST /awakeables/{id}/resolve |
Pick a promise when the outside system knows the object (a workflow ID, an order number). Pick an awakeable when you hand an ID to a third party and want nothing else exposed. Pick a signal when the sender already knows the invocation ID.
Durable promises
Section titled “Durable promises”A promise is a named value that belongs to one object (a virtual object key or a workflow ID).
import { type Context, workflow } from "@cairnflow/sdk";
const signup = workflow("UserSignup", { async run(ctx: Context, user: { email: string }) { const secret = await ctx.run("secret", () => crypto.randomUUID()); await ctx.run("mail", () => sendVerification(user, secret)); const clicked = await ctx.promise<string>("email-link-clicked").get(); return clicked === secret; }, async click(ctx: Context, req: { secret: string }) { await ctx.promise<string>("email-link-clicked").resolve(req.secret); },});| Method | Meaning |
|---|---|
get() |
Wait until the promise is completed. A rejected promise throws TerminalError |
peek() |
null if not completed yet, without waiting |
resolve(value) |
Complete with a value. Throws TerminalError if already completed |
reject(message, code?) |
Complete with a failure. Throws TerminalError if already completed |
Completing a promise over HTTP
Section titled “Completing a promise over HTTP”No extra handler is needed: an outside system can complete the promise directly with an API key that can invoke handlers.
# Resolve: the body is the value.curl -X POST "$CAIRN_URL/promises/UserSignup/u-1/email-link-clicked/resolve" \ -H "authorization: Bearer $CAIRN_KEY" \ -H "content-type: application/json" -d '"s3cr3t"'
# Reject: waiters get a terminal error with this code and message.curl -X POST "$CAIRN_URL/promises/UserSignup/u-1/email-link-clicked/reject" \ -H "authorization: Bearer $CAIRN_KEY" \ -H "content-type: application/json" -d '{"code": 403, "message": "link expired"}'
# Has it been completed yet?curl "$CAIRN_URL/promises/UserSignup/u-1/email-link-clicked" \ -H "authorization: Bearer $CAIRN_KEY"For an object in a scope, prefix the path with /scope/{scope}. With the
typed client: c.promise("UserSignup", "u-1", "email-link-clicked").resolve(value).
Properties you can rely on:
- Completing twice is a no-op, not an error. The second HTTP call answers
completed: false. Webhook retries are safe without an extra idempotency key. - Completing before anyone waits still works. The promise is durable; a
get()that runs later returns the value at once.
Signals
Section titled “Signals”A signal is a named message delivered to one invocation. ctx.signal(name) returns a future you can
await or race against a deadline:
const approval = await ctx.race([ ctx.signal<{ by: string }>("approve"), ctx.sleep(24 * 3600_000),]);if (approval === undefined) throw new TerminalError("approval timed out", 408);Send it from outside with the invocation ID:
curl -X POST "$CAIRN_URL/invocations/$ID/signal" \ -H "authorization: Bearer $CAIRN_KEY" \ -H "content-type: application/json" \ -d '{"signal": "approve", "value": {"by": "alice"}}'From the client: await c.invocation(id).signal("approve", { by: "alice" }). From another handler:
ctx.sendSignal(invocationId, "approve", { by: "alice" }) — one-way, it does not wait.
- A signal without a value resolves to
null. - A delivered signal stays delivered. Sending the same signal again is a no-op. And once
approvehas arrived, every laterctx.signal("approve")in that invocation resolves at once with the same value — use a new name for a new round of waiting (approve-2). - Signals sent to an invocation that has already finished are ignored.
Awakeables
Section titled “Awakeables”An awakeable is a wait point with a generated ID. Hand the ID to an outside system — inside a
ctx.run, so it is sent once — then await the promise:
const { id, promise } = ctx.awakeable<{ approved: boolean }>();await ctx.run("request approval", () => mailer.send(approver, id));const answer = await ctx.race([promise, ctx.sleep(7 * 24 * 3600_000)]);The outside system completes it with an API key of the same environment that can invoke handlers:
curl -X POST "$CAIRN_URL/awakeables/$AWAKEABLE_ID/resolve" \ -H "authorization: Bearer $CAIRN_KEY" \ -H "content-type: application/json" -d '{"approved": true}'
curl -X POST "$CAIRN_URL/awakeables/$AWAKEABLE_ID/reject" \ -H "authorization: Bearer $CAIRN_KEY" \ -H "content-type: application/json" -d '{"message": "denied", "code": 403}'With the client: c.awakeable(id).resolve(value) or c.awakeable(id).reject(message, code). A
rejected awakeable throws TerminalError in the handler.
Starting work that waits for a person
Section titled “Starting work that waits for a person”Start the invocation with /send (or send.<handler> in the client), not /invoke: holding an
HTTP request open until someone clicks a button is what /send avoids. For a workflow, fetch the
result later with POST /workflow/{service}/{key}/attach — it needs only the workflow ID, so the
outside system does not have to store an invocation ID.