/quiescent:quiescentfrom quiescent
Provides guidance on using Quiescent for composable async tasks in Clojure/ClojureScript, covering structured concurrency, task creation, cancellation, and core invariants like task boundaries and auto-flattening.
Ships in quiescent. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Clojure/ClojureScript library for composable async tasks with automatic parallelization, structured concurrency, and parent-child/chain cancellation.
Repository: https://github.com/multiplyco/quiescent —
co.multiply/quiescent {:mvn/version "0.11.5"}, JDK 21+ (25 recommended), Clojure 1.12+.
This file covers the semantics you cannot infer — the ones where a reasonable guess from other async libraries is wrong.
It is not an API reference. Every public var carries a thorough docstring, so for signatures, arities and options,
prefer (clojure.repl/doc q/timeout) over guessing. Sections marked See also name a var and what it is for; read
its docstring when you need it.
When writing .cljs or .cljc, also load the quiescent-cljs skill.
compel)Every task boundary guarantees dereferencing returns a concrete value with no nested tasks:
Task[Task[A]] (nested tasks)q/then flattens, enabling clean composition without flatMap noise(require '[co.multiply.quiescent :as q :refer [q qdo qjoin qfor qlet qmerge if-qlet when-qlet]])
;; Current thread — for static values/minimal computation
(q value)
;; Virtual thread — for IO/blocking operations (default for most work)
(q/task expr)
;; Platform thread — for compute-intensive work only
(q/cpu-task expr)
task spawns on a virtual thread, cpu-task on a platform thread, q runs synchronously. Otherwise semantically
equivalent.
;; Already-settled failure — short-circuit without spawning
(q/failed-task (ex-info "Invalid input" {:code 400}))
Style — :refer the q-prefixed names. The q prefix exists to disambiguate these from clojure.core (for,
let, do, merge), so alias-qualifying them (q/qfor, q/qlet) stacks a hat on a hat. Refer them bare — including
q itself, plus if-qlet/when-qlet — and use the q/ alias for everything else (q/task, q/then,
q/catch, ...).
Resolve externally — useful for bridging callback APIs:
(def p (q/promise))
(deliver p "result") ; or (q/fail p (ex-info "oops" {}))
@p ;; => "result"
Promises implement the full task API (then, catch, cancel, race, etc.) but cannot be compelled.
Tasks form a tree. Inner tasks become children of the outer:
(def parent
(q/task
(let [child (q/task (Thread/sleep 100) :done)]
@child)))
A child task cannot outlive its parent. When a parent settles, all unsettled children are cancelled automatically. This applies recursively to all descendants.
compel Escape HatchProtect tasks that genuinely need to outlive their parent (cleanup, flushing):
(q/task
(q/compel (flush-to-disk))
:done)
The compelled task won't be cancelled when the parent settles. Cascade cancellation stops at the compel moat. Direct
cancellation of a compelled task still works if you hold a reference to it.
Compel the creation, not a task you already hold. Only an inline-constructed task is itself compelled; given a
pre-existing task, compel protects the wrapper alone, leaving the inner task exposed to the cascade that was already
coming for it. The distinction is invisible at the call site and fails silently:
;; GOOD — the task is constructed inside compel, so it is compelled
(q/compel (flush-to-disk))
;; BAD — t is already a child of the enclosing scope; only the wrapper is compelled
(let [t (q/task (flush-to-disk))]
(q/compel t))
Promises can never be compelled — compel throws on one, since blocking teardown on something externally controlled
would risk a leak.
When a task returns a data structure containing nested tasks, those tasks are resolved in parallel and inlined — called "grounding":
@(q/task
{:user (fetch-user 123) ; Returns Task
:orders (fetch-orders 123)}) ; Returns Task
;; => {:user {...}, :orders [...]}
Grounding is transitive, non-blocking (parent thread returns to pool), and if any task fails, all siblings are cancelled immediately.
Grounding is a safe operation when performed at the border ofcpu-tasks. The platform thread won't be pinned; it
returns to the pool, and the grounded tasks complete independently.
then — Transform success values;; Single task
(-> (q/task (fetch-user 123))
(q/then :name))
;; Multiple tasks — waits for all in parallel (variadic)
(q/then task-a task-b task-c
(fn [a b c] (+ a b c)))
The function receives dereferenced values from all tasks as separate arguments:
(q/then
(fetch-user user-id)
(fetch-posts user-id)
(fetch-comments user-id)
(fn [user posts comments]
{:user user :posts posts :comments comments}))
catch — Recover from errors(-> (q/task (fetch-user 123))
(q/then :name)
(q/catch (fn [e] "Unknown")))
;; Multiple exception types (exclusive-or, like try/catch)
(-> (q/task (risky-operation))
(q/catch
IllegalArgumentException (fn [e] :bad-arg)
IOException (fn [e] :io-error)
Throwable (fn [e] :other)))
handle — Unified success/error handling(-> (q/task (fetch-data))
(q/handle
(fn [value error]
(if error
{:status :error :message (ex-message error)}
{:status :ok :data value}))))
Check error for nil (reliable). Checking value for nil is unreliable since nil can be a valid result.
qmerge — Merge maps with task values(qmerge
{:user (fetch-user id)}
{:orders (fetch-orders id)})
;; => {:user {...} :orders [...]}
| Handler | Runs on success | Runs on error | Runs on cancellation | Transforms value |
|---|---|---|---|---|
ok | yes | no | ||
err | yes | no | ||
done | yes | yes | no | |
finally | yes | yes | yes | no |
Side-effect handlers don't alter the outcome — the original value/error passes through. However, exceptions thrown
from side-effect handlers do propagate and fail the task chain. (monitor is the one exception — see below.)
(-> (q/task (fetch-user 123))
(q/ok (fn [user] (log "Fetched" {:id (:id user)})))
(q/err (fn [e] (log "Failed" {:error e})))
(q/finally (fn [_v _e c] (release-resource))))
finally receives three args: value, error, cancelled flag. It's the only handler that runs on cancellation.
monitor — Trigger side effect on slow tasks(-> (q/task (Thread/sleep 5000) :ok)
(q/monitor 500 #(println "This is taking a long time.")))
Imposes no deadline: it returns the task you gave it and cannot affect the outcome. The side effect fires only if the task hasn't settled by then, and is skipped once it does.
Unlike the other side-effect handlers, an exception thrown by the side effect is contained — it does not propagate
and is not reported anywhere. A bare log/warn is the normal usage and needs no protection. If the side effect does
something with consequences beyond reporting, handle its failure inside the function; nothing outside will see it.
time — Measure task duration(-> (fetch-user id)
(q/time (fn [value error cancelled ms]
(log/info "fetch-user took" ms "ms"))))
qdo — Sequential side effects (async do)Macro. Executes clauses sequentially: each clause is awaited before the next one is evaluated, like an imperative do.
Returns a Task with the last clause's value. If a clause fails, the remaining clauses are never evaluated.
(qdo
(mark-pending! id) ; awaited first
(upload! id payload) ; starts only after the previous clause settles
(mark-active! id)) ; result returned
Prefer this over chains of (q/then ... (fn [_] ...)) that ignore the previous result.
qjoin — Parallel fan-in, return lastFunction. Awaits all tasks in parallel and returns only the last one's value. Tasks are hot (already running when passed
in), so qjoin controls only the fan-in. If one task fails, all are cancelled.
(qjoin
(log-event) ; awaited but result discarded
(perform-work)) ; result returned
| Handler | Primary purpose | Runs on success | Runs on error | Runs on cancellation |
|---|---|---|---|---|
then | Transformation | yes | ||
catch | Transformation | yes | ||
handle | Transformation | yes | yes | |
ok | Side-effect | yes | ||
err | Side-effect | yes | ||
done | Side-effect | yes | yes | |
finally | Teardown | yes | yes | yes |
Cancellation is a control signal, not an error. Only finally runs on cancellation.
All handlers have -cpu variants (then-cpu, ok-cpu, etc.) that run on platform threads instead of virtual threads.
qletAnalyzes symbol dependencies and parallelizes independent bindings automatically:
@(qlet [user (fetch-user id) ; starts immediately
posts (fetch-posts id) ; parallel with user (independent)
promos (fetch-promotions) ; parallel with both (independent)
profile (process-user user) ; waits for user
result (build-dashboard profile posts promos)] ; waits for all deps
result)
How it works:
@ to deref or chain with q/thenImportant: Forms with no dependencies are not automatically wrapped in tasks. Supply your own task/cpu-task for
inline task construction.
See also if-qlet and when-qlet — await, bind if truthy, else branch / execute body. They are exactly the
clojure.core forms with qlet binding semantics.
qfor — Parallel mapping;; For task-returning functions: auto-flattens
(qfor [id ids]
(fetch-user id))
;; For blocking operations: wrap in q/task
(qfor [batch batches]
(q/task
(Thread/sleep wait)
(process-batch batch)))
;; The collection may itself be a task
(qfor [user (fetch-users)]
(enrich user))
Mapping runs on a single virtual thread, elements in order. The body may park (deref, await), but a blocking body
serializes the loop — wrap it in q/task to run elements in parallel.
Like a qlet binding, the collection expression is resolved before mapping: it may be a task, and any task
elements are grounded first, so the body always sees plain values. To map over tasks as tasks, use ordinary Clojure:
(q (mapv f tasks)).
Unlike clojure.core/for, qfor takes a single binding pair and supports no :let, :when or :while
modifiers. Filter the collection before the loop, or bind inside the body.
(def my-task (q/task (slow-operation)))
(q/cancel my-task) ; Returns task resolving to boolean (true=cancelled, false=already settled)
A cancelled task throws if dereferenced. Failure-driven cancellation retains the original exception through cascades;
explicit cancellation and cleanup after success generate a CancellationException on the JVM. Use q/cancelled?,
not the exception's type, to identify cancellation. catch and handle still skip cancelled tasks even when they
carry an originating failure. Failure and its cancellation can race; the first settlement wins.
See also for reading a task without deref's throwing behaviour:
(q/await task) / (q/await task 1000) — a task settling to true at quiescence, or false if the bound expired
(deref to block; CLJ only). Never throws on failure or cancellation, and never touches the observed task: an expired
bound does not cancel it — await observes where timeout owns.(q/get-now task :not-ready) — the value if settled, else the default. Does throw if it settled exceptionally.(q/get-ex task) — the exception as a value if the task settled exceptionally, whether through failure or
cancellation; nil otherwise. Never throws. catch and handle never see cancellation — this does.(q/cancelled? some-task) ; => true or false
;; Inside finally:
(q/finally task
(fn [_v _e c]
(when c (log/info "Task was cancelled"))))
See also the predicates q/task?, q/promise?, q/exceptional? (settled with an exception, without dereferencing)
and q/taskable? — true for anything Quiescent handles specially: Task, Promise, CompletableFuture and Future (CLJ),
js/Promise (CLJS), and core.async channels once the adapter ns is required.
Limit concurrent tasks:
(let [g (q/gate 20)]
@(qfor [n (range 1000)]
(q/gate-task g
(process n))))
Use (q/try-gate-task g (process n)) for skip-when-busy admission. It attempts to reserve a permit before returning;
if busy, it returns an already-cancelled task without queueing or evaluating the body. An admitted body runs
asynchronously and grounds its result just like gate-task. Rejection skips then, catch, and handle; observe it
with cancelled? or finally. compel does not bypass admission. Both operations share the reentrancy and cancellation
rules below and work in CLJ and CLJS.
A gate is a coordinator, not a task: it is off the cancellation tree, holds no claim on gated work, and q/cancel on a
gate throws. It is safe to def a gate globally — even from inside a task — and share it: holding a gate grants
admission width only. A gated task is structured under whatever created it, so cascade cancellation from its creator
reaches it (running or queued); cancelling one cohort's spawning task leaves other cohorts sharing the gate untouched.
Wrap in compel to detach fire-and-forget gated work from its creator.
Rule of thumb: tasks and promises are work — tree members, mortal, cancel them through their handles. Gates and
semaphores are coordinators — off-tree, immortal, GC-managed. The constructs you would def globally are exactly the
ones that are safe to.
Gates are reentrant while the enclosing gated task is unsettled, including through other gates (A → B → A).
Each nested gated task retains a share of that permit until it settles. The permit returns when all sharers settle,
including compelled nested tasks that outlive the original holder. A descendant created from an already-settled
holder's scope must acquire normally.
Chained handlers do not retain a gated task's permit; put their work inside the body if it needs the permit. Gates limit admission: cancellation releases a task's share at settlement, while already-running code must cooperate with cancellation. Capacity must be an integer from 0 through 2147483647; zero queues until cancellation or rejects tries.
See also q/semaphore / q/with-semaphore (CLJ only) — a plain counting semaphore for guarding a blocking section,
where gate limits task admission. Prefer gate unless you specifically need the lock shape.
(q/retry f opts) calls (f retrying?) with exponential backoff. Options: :retries (default 3), :backoff-ms
(default 2000), :backoff-factor (default 2), :retry-callback (fn [e retries backoff]), and :validate
(fn [value error]) — throw from :validate to treat a successful result as a failure worth retrying, which is how
you retry on e.g. a 503 response body.
See also (q/sleep ms) — a task that settles after a delay, optionally with a value: (q/sleep 500 :expired).
Durations are ms or java.time.Duration, must be non-negative (0 settles immediately), and anything else throws.
Both sleep and timeout interpret their default polymorphically: a fn is called at the deadline, a Throwable
is thrown, anything else is used as the value.
timeout — Bound a task by a deadline(q/timeout t 5000) ; throws TimeoutException at the deadline
(q/timeout t (Exception. "Oh no!")) ; custom exception thrown at the deadline
(q/timeout t 5000 :gave-up) ; settles with a value instead
(q/timeout t 5000 #(fallback)) ; calls a fn — may itself return a task
(q/timeout t (Duration/ofMinutes 5))
Behaviours you can assume:
default covers the timer firing and nothing else. A task that fails throws its own exception, promptly, even
when a default was supplied. To absorb failures too, say so explicitly:
(-> t (q/timeout ms fallback) (q/catch (fn [_] fallback))).timeout is called
wins outright, whatever the duration.timeout cancels the awaited task; the timer is torn down as soon as the awaited task settles, so it
never outlives the work.q/finally cleanup still runs.;; Race individual tasks — first *successful* result wins, losers are cancelled
(q/race task-a task-b task-c)
;; Race data structures (first group to fully ground wins)
(q/race #{t1 t2} #{t2 t3} #{t1 t3})
;; race-stateful: cleanup function for realized-but-losing tasks
(q/race-stateful Socket/.close task-a task-b)
;; any-of: like race, but observes instead of owns — losers keep running,
;; and cancelling the result does not propagate to the entrants. Use for
;; tasks that are shared or owned elsewhere.
(q/any-of cache-lookup fresh-fetch)
Naming the winner. A race settles with the winner's value — the winning task's identity is not in the result. Tag
entrants with data structures to recover it: (q/race [:eu eu-fetch] [:us us-fetch]) → [:us result]. Each vector
grounds exactly when its task settles, so race ordering is unchanged. With race-stateful, release then receives the
tagged pair, not the raw resource: (q/race-stateful #(-> % last Socket/.close) [:a conn-a] [:b conn-b]).
"All of" needs no function: a data structure is one — [t1 t2 t3] grounds when every task has completed.
Uses Scoped for efficient scope propagation (ScopedValue on JDK 25+):
(require '[co.multiply.scoped :refer [ask scoping]])
(def ^:dynamic *name*)
(scoping [*name* "Alice"]
(q/task
(println "Hello," (ask *name*))))
CompletableFuture and Future (CLJ) and js/Promise (CLJS) are auto-converted — returned from a task body or
passed to a chaining function, they just work:
(qlet [result (s3/async-get ...)] ; CompletableFuture, no conversion needed
(process result))
Convert explicitly with q/as-task (in) and q/as-cf / q/as-jsp (out).
core.async is the exception — channels are not auto-converted during grounding, so conversion is always explicit.
Require co.multiply.quiescent.adapter.core-async, then q/as-task for channel → task and the adapter's
as-chan for task → channel.
Dereferencing on a platform thread throws by default (prevents blocking carrier threads):
@my-task ; throws "Refusing to park platform thread"
(q/deref-cpu my-task) ; explicitly allow blocking deref
(q/throw-on-platform-park! false) ; disable check globally
Tasks are values, not one-shot channels: once settled they hold their result permanently. A task can be chained from
repeatedly and dereferenced any number of times, and each chain is independent — (q/then a f1) and (q/then a f2)
both see a's value without re-running it.
Avoid using @ (deref) on a Task inside any Quiescent callback (q/then, q/ok, q/handle, q/catch,
q/finally, qlet, qfor, etc.). Derefing blocks the thread and defeats async coordination.
There are occasions where one might need to break this rule, but it should have a strong motivation. For example: test assertions where you need to unwrap a value for use outside the task graph.
Quiescent task grounding and task composition provides a strong foundation to express the vast majority of patterns without dereferencing.
;; Generally BAD: Blocking deref inside q/then
(-> (fetch-user id)
(q/then (fn [user]
@(save-audit-log user)))) ; Blocks thread
;; Generally GOOD: Return the task — automatically grounds
(-> (fetch-user id)
(q/then (fn [user]
(save-audit-log user)))) ; Returns Task
Side-effect handlers are the exception worth knowing separately — they neither ground nor await what the handler returns, so both the rule and its escape hatches differ. See Launching a task inside a side-effect handler.
ok, err, done and finally ignore what the handler returns — the original outcome passes through the moment
the handler returns, not when work it started settles. This is deliberate: it lets a handler kick off something
open-ended and move on. It also means a bare task created inside one is an ordinary child of a link that settles
immediately, so it is cancelled before it can run — and nothing throws or logs, the effect simply doesn't happen.
Two independent questions decide the fix: must the effect survive cancellation of the chain (compel), and must
downstream observe it (await)?
| Inside a side-effect handler | Survives cancellation | Downstream observes |
|---|---|---|
(persist! ...) | no — never runs at all | no |
(q/compel (persist! ...)) | yes | no |
@(persist! ...) | no — torn down mid-write | yes |
@(q/compel (persist! ...)) | yes | yes |
Row three is the subtle one: a bare @ orders downstream correctly, but the awaited task is still an ordinary child, so
cancelling the chain tears it down mid-write. Writing it asserts a half-completed persist! is acceptable — for a
persist it rarely is. Prefer @(q/compel ...) unless the effect is genuinely abandonable. (@ is CLJ only; it parks a
virtual thread, which is cheap. On finally's cancellation path there is no downstream to order, so only the
compel axis is meaningful there.)
If you only need ordering and the effect is abandonable, restructure instead of blocking — a side-effect handler
cannot express ordering at all, whereas qlet + qdo says it directly and works in CLJS:
;; qdo awaits each clause, returns the last — so `value` still passes through.
(qlet [value (q/task 42)]
(qdo
(persist! store data)
value))
The async counterpart to with-open: open a resource, lend it to f, and release it however f settles. finally
is the handler for this — it is the only one that also runs on cancellation, so a resource is released even when the
task tree is torn down from above.
(defn with-stream
"Open a stream, lend it to f, and close it however f settles."
[path f]
(let [s (io/input-stream path)]
(-> (f s)
(q/finally (fn [& _] (InputStream/.close s))))))
;; Usage
(with-stream "data.bin"
(fn [s]
(q/task (parse-records s))))
Ignore the handler's arguments — value, error and cancelled flag are irrelevant when the answer is "close it either
way". The resource passes through unchanged: finally doesn't transform, so callers see f's own result.
f settlesok/done would missclose sidesteps the return-value trap above entirely; if release is itself async, wrap its
creation in compel — (q/compel (close-async! s)) — since finally ignores what the handler returns and the
chain is already settlingQuiescent runs on both platforms with the same semantics, but the JS runtime forces a handful of differences — no
blocking deref (so no @), no cpu-task or -cpu variants, gate instead of semaphore, predicate-based catch,
cancellation as ex-info rather than CancellationException, and abort-signal for Fetch.
When writing ClojureScript, load the quiescent-cljs skill — it covers the full delta. The most common trap is
reaching for a CLJ-only pattern from this file: anything using @ needs restructuring, typically to qlet + qdo.
What SKILL.md sets; muted values are Claude Code's defaults
2 skills in this plugin
npx claudepluginhub multiplyco/quiescent --plugin quiescentOther plugins' skills for the same job