pinecallDocs
Advanced / A deploy never cuts a call

A deploy never cuts a call

What the gateway, the worker, the app and the page do when one of them restarts mid-conversation.

A live call belongs to the runtime, and its durable record is its log. No process on the path (the gateway, the worker, the tenant's app, the page) holds in memory something the call needs and cannot get back. The caller never hears any of it: the audio is LiveKit's and the worker's, and neither is touched by the others restarting.

A call is its agent's, not a socket's#

A call is handed to one app socket when it opens and served by it until it goes. When that socket closes the call is parked, and handed at once to the socket that would take a new call of that agent in that scope, or to the next process that registers it. A console (takes_unclaimed: false) never takes one. The socket that takes it hears call.attached first, {app, started, state, seq, claimed}, then every tool.call still waiting for an answer, and sends the call's whole prompt and tools again: the log keeps a prompt's hash, not its text.

On a box of several gateways it does not matter which gateway opened the call, which one held the socket that left, or which one the next process registers on. The gateway that opened the call is the one that parks it, told by the gateway the socket was on, and every gateway hears a process take the agent: the one holding a parked call of it hands the call over, and the tools waiting on it reach the new socket.

A spoken call outlives the gateway that opened it: its worker goes on, and reads its commands from another gateway. Each gateway says the calls it opened every second; once the one that opened a call has said nothing for five seconds, the gateway the call's worker reads its commands from adopts it. From then on the call is that gateway's as if it had opened it: it counts in concurrent_calls there, and it is parked and handed to the socket that holds its agent from there, which hears call.attached. Two gateways that adopted one call, each deaf to the other for a moment, settle it when they hear each other again: the one of the greater id leaves it.

A process that leaves on purpose#

agent.drain on the socket: it is handed no new call, each call it served goes to another socket or waits parked, and agent.draining {app, env, handed, parked} lands on the agent's log. It still holds the agent, so the tools it is running answer. pinecall start drains on SIGTERM.

A tool asked while nobody holds the agent is written as tool.call and waits its own timeout_s for the next socket.

The gateway restarts#

What a call is, its context and the config it runs on, is kept in Postgres when it opens, so a gateway that restarted, or another gateway of the platform, serves any endpoint of it from one read the first time an endpoint asks, and from memory after. A call sealed is 409 at every endpoint. A call an older release opened kept nothing: its endpoint answers 404, and the worker says the call again, once, POST /v1/calls/{call}/reopened {agent, context}, and asks again: no quota asked, no token spent, no second call.ringing. While the gateway is away the worker retries an entry, the seal, a tool and the command stream, backing off from half a second to five, for as long as nothing answers or a 5xx does; a 4xx is an answer. A tool retries within its own deadline, the seal within thirty seconds. A tool call is run once per call id: a retry joins the round trip still running, and one that finished, before the answer was lost or the gateway restarted, is answered with the tool.result its log holds and never reaches the app a second time. The log is the channel both ways: a tool.call reaches the app through whichever gateway holds its socket, its tool.result is written there, once per call id, and a retry that lands on another gateway waits for that answer, out of what is left of the tool's deadline, counted from its tool.call.

A command reaches its call once. Every gateway that serves a spoken call keeps the app's commands for its worker's stream (GET /v1/calls/{call}/commands), each under the id it was sent with, which the stream carries as its id:. A worker whose stream moved to another gateway is sent again what the first one sent, and drops every id it applied already.

A tool's answer follows what its app said before it. The answer goes back on the worker's own request and the app's commands come down that stream, so the two could cross: when the app answers a tool, the gateway holding its socket says tool.settled {call_id} down the stream, after every command the app sent first, and the worker hands the answer to the model once it has read it (it waits a second at most, for a gateway that says nothing). tool.settled is the gateway's own word, as caller.hung_up and stages.changed are: an app's socket refuses it.

A retried entry is written once when it travels in a batch, {after, entries} down the call's socket WS /v1/calls/{call}/entries (or, to a gateway that has no such socket, POST /v1/calls/{call}/entries with the same body): a call's log has one writer, which sends its entries in order and says how many the log already took from it, and the log's head counts them. A batch that starts where the count stands is written, whole, under the next seqs; the same batch sent again because its answer was lost is answered with the seqs it was given and writes nothing; anything else is a 409 that says both counts. Each entry keeps the worker's ts, when it happened, clamped to the gateway's clock. The gateway's own entries take seqs and are not counted.

The worker writes its call's entries as these batches and no other way: what is queued goes as one batch, and a batch retried goes again unchanged, after the same count, so a retry can no longer write an entry twice. A 409 refuses the whole batch and the call goes on with the next. The entries a worker writes outside its session take the same writer: a command the session refused is an error on the session's own, and the call.ended of a leg nobody answered and the overflow's sentence and call.ended go through one the job opens with the call. The worker that takes up a call whose worker died writes through a writer that follows on from the dead worker's: POST /v1/calls/{call}/taken-up answers the count the log's head keeps, after the gateway's own entry that says the loss (error, worker_lost), which is counted as the writer's so that a batch the dead worker sends late writes nothing. A job that can only tell the caller follows on from the count its dispatch carries (entries_written). A seal says which of the call's workers sends it (taken_up), and one from a worker the call was taken from is answered 410, by whichever gateway it reaches: the count is the head's (call_log_head.taken_up), moved by the take-up in one statement and read by the seal under its lease. A job that can only tell the caller seals as the last worker its dispatch names (taken_up). The one-entry endpoint, POST /v1/calls/{call}/events, still answers for a worker of an older release, and counts nothing. A written call's session batches too, straight to its log; taken up after a restart, it goes on from the count its log's head keeps.

A release of the workers#

A worker told to stop takes no new call, finishes the ones it holds (up to ten minutes) and leaves; a release moves no call from one worker to another (only a worker that dies has its calls taken up, Scaling, "A worker that dies"). So a release keeps a fleet open by never stopping its last worker: each environment's workers are a Deployment that starts a new pod and waits for it to answer its startup probe before it tells an old one to stop (maxUnavailable: 0, maxSurge: 1), and Kubernetes gives the old one fifteen minutes to drain. While one drains the others take every new call of its environment, so no caller of a deploy hears the overflow's sentence (Scaling, "Deploys drain, cordons shrink").

A written call#

A chat over WS /v1/chat or a WhatsApp thread runs in the gateway's own process, and a restart ends its session, never the call. It is taken up the next time it is spoken to: WS /v1/chat?call=<id> dials again naming the call; a WhatsApp contact's next message finds their open call. A WhatsApp message that arrives while nobody holds the agent is written on the agent's log as message.waiting and answered, in order, the moment a socket holds it again (message.taken says on which call); one older than Meta's 24-hour window is let go. The session is rebuilt from the log, and the app's socket hears call.attached.

A call nobody is running#

A worker that dies mid-call is heard of at once: LiveKit's webhook says its agent's connection was lost, and the gateway ends the call as drained, offers the caller a call back and sends a job into the room that says the overflow's sentence and closes it (Scaling). What that leaves, and a call whose caller had already gone, is the reaper's.

The reaper looks every minute. A call in a room with no agent left in it, quiet five minutes, is ended as drained and its room taken down. A written call no process serves is ended as timeout once quiet as long as its endpoint waits: five minutes for a chat, two hours for WhatsApp. A parked call is handed, at the look, to a process that took its agent since, if the gateways' word of it was lost; and it is ended as app_detached once two minutes have passed with none. A call another gateway sealed, or one that was erased, stops counting in concurrent_calls on the gateway that opened it at the reaper's next look. One call the reaper cannot seal is said in the gateway's journal and left: the calls behind it are sealed, and the next look comes.

The page#

A page follows its call with the log_token its server's mint answered, straight from the gateway. A gateway restarting cuts the stream; it reconnects with Last-Event-ID and misses nothing.