Skip to Content
Motoko

Durable approval

A call whose tier the policy gates (the session's own rule, if it has one) is parked by a beforeTool hook that returns { sleep: { signals: [ApprovalSignal.key(callId)] } }, pi-durable 1.6's gate. The park is the tool task's committed state; nothing waits in memory. A delegate's call also sleeps once it has started, while its child works, and so does a deferred call while it waits for its reply, and an app tool's call that parks on its own work. Those parks are their own, recorded in runtime.calls, so they raise no question. The gate judges every call of a session, the app extensions' tools' too. approve() delivers every decision of its batch with tx.signal() in one commit that also records them in a conversation document (runtime.approvals): if any call of the batch is not waiting, the commit is discarded and nothing is decided (ApprovalNotPendingError names that call). Restarting, reinstalling the registry or the decision arriving reruns the gate, which reads the signal first and passes, or blocks a declined call, whatever the session's rule says by then; an afterTool hook then hands the model The operator declined this call: <comment> as the call's result.

The operator is asked about the arguments the call runs with. The gate is the last beforeTool hook, after the app's, and when a call first parks it memoizes the arguments it sees on the call's tool task (motoko.gate.args). approval:request.args are those, and approve() records them with the decision, since the memo goes when the task ends. So the live stream, a catch-up and a replay ask with the same arguments: the decision's, else the memo's, else, for a call the gate never saw park (one decided before it parked, or by an earlier Motoko), the model's. A call no app hook rewrote is asked about with the model's arguments. An approved call runs with the memoized arguments, whatever the app's hooks give when the decision wakes it, so what runs is what the operator was asked about.

A call may be decided before it parks: approve() also takes a call whose task exists and that the gate will gate but has not reached, such as the next call of a sequential round while its app hooks run. Its operator was asked about the model's arguments, so it runs only with those. The first app hook that rewrites a call memoizes the arguments pi handed it (motoko.model.args): the model's, as the tool repairs and coerces them, which no app hook has touched. The gate checks an approved call that has no memo of its own against these, and blocks it when it would run with others, with The call's arguments changed after the operator was asked about it. pi's coercion is not a rewrite: a call no app hook rewrote runs, even when the model wrote "42" for a number or null for an optional property.

A nested call, one a tool makes through pi-durable's executeTool() (a code mode script's tools.remove({ key: "a" }), say), passes the gate as a call of the model's does, and approve() decides it the same way, by its id: <callId>/<key>, as pi.live.nestedTools shows it. Its approval:request carries parentToolCallId, the model's call it was made under, directly or through other nested calls, and its args in the same order: the decision's, else the memo's, else those the tool made it with. Its approval:resolved goes out as it moves on from the gate, so a client resuming anywhere folds what a client connected all along does. A nested call has no tool:start or tool:end of its own: its question ends with the call it was made under, and a replay after that call ended shows neither. A declined nested call fails, for the tool that made it, with The operator declined this call: <comment> as its error (an error diagnostic, code declined), which a code mode script's call rejects with.

A nested call waiting for a decision holds a process, unlike a call of the model's: the tool that made it awaits it, so that call's invocation runs, and the instance does not close idle meanwhile (on Cloudflare, its Durable Object stays awake). So it waits nestedApprovalTimeoutMs at most (default an hour), from when it first parks; a restart does not move that deadline. With no decision by then, it fails for the tool that made it with No decision arrived in time. as its error (an error diagnostic, code no_decision), its question ends as a declined one's, with an approval:resolved that carries approved: false and those words as its comment, and approve() refuses it with ApprovalNotPendingError. A call of the model's has no such deadline. A restart cuts the invocation off. A replay-safe tool, as code mode is, runs again after it and finds its nested call still waiting, so the question and its decision carry on. A code mode script can also bound itself, waits for decisions included: its // @options line's timeout_ms is code mode's timeoutMs for that script, which has none by default.