Skip to content

What happens when you send while a turn runs?

View as Markdown

A session runs one turn at a time. A Send that arrives while a turn runs follows the session’s busy policy, set with thread.BusyPolicy(p) or for one send with thread.As(p): Queue (the default) runs it next, Reject fails it with thread.ErrBusy, Steer delivers it into the running turn, Interrupt cancels the turn and queues the message behind any sends already waiting, and Rollback also branches back before the interrupted turn. Every message accepted while a turn runs is written to the storage, as a receipt entry, before Send returns; an idle Send writes its prompt entry instead.

sessions/busy.go builds a Reject session, sends while its tool runs, then steers one message in with As(Steer):

sessions/busy.go
s, err := thread.Create(ctx, thread.Memory(), weft.New(model, work),
ctxWindow, ids("s_reject", "a"), thread.BusyPolicy(thread.Reject))
if err != nil {
return err
}
turn, err := s.Send(ctx, weft.User("How far is the lighthouse?"))
if err != nil {
return err
}
<-started // the tool is running
_, err = s.Send(ctx, weft.User("Also, the ferry times?"))
fmt.Fprintln(w, "while busy:", errors.Is(err, thread.ErrBusy))
steer, err := s.Send(ctx, weft.User("Use metric units."), thread.As(thread.Steer))
if err != nil {
return err
}
close(release)
res, err := turn.Wait()
if err != nil {
return err
}
if _, err := steer.Wait(); err != nil { // nil result: a steer has no run of its own
return err
}
fmt.Fprintln(w, "steer:", steer.Outcome())
fmt.Fprintln(w, "the model's last request ends with:", model.LastRequest().LastText())
fmt.Fprintln(w, "answer:", res.Text())

sessions/busy.go on GitHub

while busy: true
steer: delivered
the model's last request ends with: Use metric units.
answer: About 8 kilometres.

The rejected send wrote nothing. The steered one joined the running turn: the model’s second request ended with it, and the answer is the running turn’s. In the demos, ids pins the ids for stable output and ctxWindow is thread.ContextWindow(128_000), the model’s context window (why).

thread.Policy is what a Send does when the session is already running a turn:

PolicyString()What a busy Send doesIts Turn
thread.QueuequeueHolds the message and runs it as the next turn, in acceptance order. An accepted receipt carries it until its turn starts. The default.the queued turn
thread.RejectrejectFails with thread.ErrBusy; nothing written.none
thread.SteersteerDelivers the message into the running turn at its next drain point; a queued receipt carries it until then.a receipt with no run of its own: delivered, deferred or dropped
thread.InterruptinterruptCancels the running turn and queues the message at the back, as Queue does: sends already waiting run first.a queued turn
thread.RollbackrollbackAn Interrupt that also branches the leaf back to before the interrupted turn’s receipt entry. Sends already waiting run first, on the rolled-back line.a queued turn

The policy is captured when Send is called and recorded on the turn entry (TurnEntry.Policy, the Policy.String value). On a session that is not busy every policy does the same thing: the message runs as a plain turn. That includes the window between a turn’s end and the session’s between-turn work. A Send while approval requests are pending is queued behind the boundary; under Reject a pending boundary reads as busy. Concurrent sends are accepted in the order they take the session’s lock. The package examples are ExampleBusyPolicy and ExampleAs.

A steer is delivered at the loop’s two drain points (ADR 0019): after a step’s tool batch, when every call already has its result, and at what would have been the final step, which the delivery turns into one more step. The delivered message is an ordinary user message in that run’s transcript. The model sees it once.

A steer that meets an intended end (StopWhen) or an open approval boundary is never drained, and neither is one that arrives after the run’s last drain point. It becomes a deferred follow-up that runs as the next turn, and steer.Next() is that turn. Here the tool that a StopWhen condition ends on steers:

sessions/busy.go
turn, err := s.Send(ctx, weft.User("File a ticket: my parcel is late."))
if err != nil {
return err
}
if _, err := turn.Wait(); err != nil { // StopWhen ended the run after submit
return err
}
if _, err := steer.Wait(); err != nil {
return err
}
fmt.Fprintln(w, "steer:", steer.Outcome())
followUp := steer.Next() // the turn the steer became
res, err := followUp.Wait()
if err != nil {
return err
}
fmt.Fprintln(w, "follow-up:", res.Text(), "run:", followUp.RunID())

sessions/busy.go on GitHub

steer: deferred
follow-up: Added your phone number to ticket 77. run: s_ticket-t3

The follow-up runs under the run options of the turn the steer was aimed at, with the steer’s own options after them, so a turn sent with weft.ParkAllExcept keeps its follow-ups parked. It runs on the steer’s Send context for cancellation. The steer reports run id t2, minted at acceptance and used by no run; the follow-up is t3. The package examples are ExampleSteer and ExampleTurn_Next.

s.Queue() returns what the session has accepted and not yet given to a model, as thread.QueuedSteer values: first the steers waiting for the running turn (Policy is Steer), then the sends waiting for a turn of their own (Policy is Queue). Receipt is the id the message’s Turn reports and Msg the message. s.ClearQueue(ctx) drops all of them in one atomic append and returns how many:

sessions/busy.go
later, err := s.Send(ctx, weft.User("Then email it to me.")) // Queue, the default
if err != nil {
return err
}
nudge, err := s.Send(ctx, weft.User("Skip the archived orders."), thread.As(thread.Steer))
if err != nil {
return err
}
for _, q := range s.Queue() { // steers first, then sends
fmt.Fprintln(w, "waiting:", q.Policy, q.Receipt, q.Msg.Text())
}
n, err := s.ClearQueue(ctx)
fmt.Fprintln(w, "dropped:", n, err)
close(release)
if _, err := running.Wait(); err != nil {
return err
}
_, err = later.Wait()
fmt.Fprintln(w, "queued send:", later.Outcome(), errors.Is(err, thread.ErrDropped))
fmt.Fprintln(w, "steer:", nudge.Outcome(), "model calls:", len(model.Requests()))

sessions/busy.go on GitHub

waiting: steer e_a5 Skip the archived orders.
waiting: queue e_a3 Then email it to me.
dropped: 2 <nil>
queued send: dropped true
steer: dropped model calls: 2

Each dropped message’s Turn ends TurnDropped and its Wait returns an error wrapping thread.ErrDropped. The model was called twice, both for the running turn. Delivered or deferred steers are untouched, though a deferred steer’s follow-up is a queued send and is dropped. The queue is unbounded: if users can pile messages onto a running turn, check len(s.Queue()) before you send. The package example is ExampleSession_Queue.

As(thread.Interrupt) cancels the running turn’s context and queues the new message. Here nothing else is waiting, so it runs next:

sessions/busy.go
long, err := s.Send(ctx, weft.User("Research everything about tides."))
if err != nil {
return err
}
<-started
short, err := s.Send(ctx, weft.User("Stop. Just the short version."), thread.As(thread.Interrupt))
if err != nil {
return err
}
_, err = long.Wait()
fmt.Fprintln(w, "first turn:", long.Outcome(), errors.Is(err, context.Canceled))
res, err := short.Wait()
if err != nil {
return err
}
fmt.Fprintln(w, "second turn:", res.Text())
for _, m := range s.Context() {
for _, p := range m.Content {
if r, ok := p.(weft.ToolResultPart); ok {
fmt.Fprintln(w, "the model reads:", r.Content)
}
}
}

sessions/busy.go on GitHub

first turn: canceled true
second turn: Two tides a day.
the model reads: tool call research was interrupted: the run was canceled for a newer message

The calls the interrupted turn left without a result carry that interruption text, so the next model sees what was cut off. An approval boundary the interrupt supersedes is denied with the interrupted reason. The interrupted turn’s entries stay on the tree as evidence. An interrupt that lands between Send and the run’s start still fells the run at birth.

Rollback does the same and moves the leaf back to before the interrupted turn’s receipt entry: the follow-up answers as though the interrupted message was never sent, while its entries keep their own line of the tree. The package examples are ExampleInterrupt and ExampleRollback.

A message accepted while the session is busy is durable at acceptance (thread rule T4): its receipt entry is appended to the storage and flushed before Send returns, so neither a queued send nor a steer is lost to a crash. The busy-policy session above holds two receipt entries:

sessions/busy.go
for _, e := range s.Entries() {
r, ok := e.(thread.ReceiptEntry)
if !ok {
continue
}
switch r.Status {
case thread.ReceiptQueued, thread.ReceiptAccepted: // acceptance: the message rides here
fmt.Fprintf(w, "receipt %s: %s %q\n", r.ID, r.Status, r.Msg.Text())
default: // a fate: delivered, deferred or dropped
fmt.Fprintf(w, "receipt %s: %s, settles %s\n", r.ID, r.Status, r.Receipt)
}
}

sessions/busy.go on GitHub

receipt e_a3: queued "Use metric units."
receipt e_a8: delivered, settles e_a3

A thread.ReceiptEntry’s Status is one of:

StatusMeaning
ReceiptQueued queuedA steer’s acceptance: the message is held for the running turn’s drain.
ReceiptAccepted acceptedA queued send’s acceptance: the message is held for a turn of its own; Turn names the id its prompt entry will take.
ReceiptDelivered deliveredThe running run drained the steer; RunID names the run. Unanswered marks a run that ended before the model answered it.
ReceiptDeferred deferredThe steer became a follow-up turn; Turn names its prompt entry.
ReceiptDropped droppedRemoved before it reached a model: ClearQueue, or an interrupting send that was refused.

A fate links to its acceptance through Receipt. A queued send is settled by its prompt entry landing, or by a dropped receipt. Receipt entries never enter the model’s context: the message reaches the model through the run that delivered it or the turn that ran it, exactly once. An acceptance with no fate is a message the writer never got to; Open restores it to s.Queue() and runs nothing until Send, Decide, Resume or Continue (What is a session?).

Send(…, As(Steer)) on an idle session runs the message as a plain turn. That is right for the session’s owner, and wrong for a caller acting on a turn it does not own, such as a debugger: its “is a turn running?” check could never be atomic with the send. s.Steer(ctx, runID, msgs...) is that caller’s verb. Under the session’s lock it checks that a turn is running now and is runID ("" accepts any running turn), and accepts the messages as a Steer-policy Send would:

sessions/busy.go
err = s.Steer(ctx, "", weft.User("Use metric units."))
fmt.Fprintln(w, "idle:", errors.Is(err, thread.ErrNotRunning), "entries:", len(s.Entries()))
turn, err := s.Send(ctx, weft.User("How far is the lighthouse?")) // its tool steers the run
if err != nil {
return err
}
res, err := turn.Wait()
if err != nil {
return err
}
fmt.Fprintln(w, "steer from the tool:", steerErr)
fmt.Fprintln(w, "answer:", res.Text(), "after:", model.LastRequest().LastText())
err = s.Steer(ctx, turn.RunID(), weft.User("Thanks."))
fmt.Fprintln(w, "after the turn:", errors.Is(err, thread.ErrNotRunning))

sessions/busy.go on GitHub

idle: true entries: 0
steer from the tool: <nil>
answer: About 8 kilometres. after: Use metric units.
after the turn: true

When no turn is running (idle, between turns, parked at an approval boundary, or a session reopened after a crash mid-turn), or the running turn is another run, Steer returns thread.ErrNotRunning and writes nothing. The call never starts a turn. A steer it delivered that then misses its drain point (the turn meets StopWhen, parks, or ends first) still defers to a follow-up turn, under the aimed turn’s options: that is the delivered steer’s fate, recorded on its receipt.

Every message must have the user role, or the call fails with weft.ErrInvalidSteer before anything is written; a closed session is ErrClosed. A multi-message Steer whose later append fails returns that error with the earlier messages already accepted.

sessions/busy.go runs each policy against a slow tool:

sessions/busy.go
package main
import (
"context"
"errors"
"fmt"
"io"
"github.com/weftgo/weft"
"github.com/weftgo/weft/thread"
"github.com/weftgo/weft/wefttest"
)
// demoBusy is the page "What happens when you send while a turn runs?".
func demoBusy(ctx context.Context, w io.Writer, _ string) error {
for _, demo := range []func(context.Context, io.Writer) error{
rejectAndSteer, queueAndClear, interrupt, steerVerb, deferred,
} {
if err := demo(ctx, w); err != nil {
return err
}
}
return nil
}
// rejectAndSteer: a Reject session refuses a send while a turn runs; one
// Send overrides the policy with As(Steer) and joins the running turn.
func rejectAndSteer(ctx context.Context, w io.Writer) error {
fmt.Fprintln(w, "== reject, and steer one send")
work, started, release := slowTool("convert")
model := wefttest.Script(
wefttest.ToolCalls(wefttest.Call{Name: "convert", ID: "call_1"}),
wefttest.Say("About 8 kilometres."),
)
s, err := thread.Create(ctx, thread.Memory(), weft.New(model, work),
ctxWindow, ids("s_reject", "a"), thread.BusyPolicy(thread.Reject))
if err != nil {
return err
}
turn, err := s.Send(ctx, weft.User("How far is the lighthouse?"))
if err != nil {
return err
}
<-started // the tool is running
_, err = s.Send(ctx, weft.User("Also, the ferry times?"))
fmt.Fprintln(w, "while busy:", errors.Is(err, thread.ErrBusy))
steer, err := s.Send(ctx, weft.User("Use metric units."), thread.As(thread.Steer))
if err != nil {
return err
}
close(release)
res, err := turn.Wait()
if err != nil {
return err
}
if _, err := steer.Wait(); err != nil { // nil result: a steer has no run of its own
return err
}
fmt.Fprintln(w, "steer:", steer.Outcome())
fmt.Fprintln(w, "the model's last request ends with:", model.LastRequest().LastText())
fmt.Fprintln(w, "answer:", res.Text())
printReceipts(w, s)
return s.Close(ctx)
}
// queueAndClear: the default policy queues; Queue lists what waits and
// ClearQueue drops it.
func queueAndClear(ctx context.Context, w io.Writer) error {
fmt.Fprintln(w, "== queue and clear")
work, started, release := slowTool("export")
model := wefttest.Script(
wefttest.ToolCalls(wefttest.Call{Name: "export", ID: "call_1"}),
wefttest.Say("Exported."),
)
s, err := thread.Create(ctx, thread.Memory(), weft.New(model, work), ctxWindow, ids("s_queue", "a"))
if err != nil {
return err
}
running, err := s.Send(ctx, weft.User("Export my order history."))
if err != nil {
return err
}
<-started
later, err := s.Send(ctx, weft.User("Then email it to me.")) // Queue, the default
if err != nil {
return err
}
nudge, err := s.Send(ctx, weft.User("Skip the archived orders."), thread.As(thread.Steer))
if err != nil {
return err
}
for _, q := range s.Queue() { // steers first, then sends
fmt.Fprintln(w, "waiting:", q.Policy, q.Receipt, q.Msg.Text())
}
n, err := s.ClearQueue(ctx)
fmt.Fprintln(w, "dropped:", n, err)
close(release)
if _, err := running.Wait(); err != nil {
return err
}
_, err = later.Wait()
fmt.Fprintln(w, "queued send:", later.Outcome(), errors.Is(err, thread.ErrDropped))
fmt.Fprintln(w, "steer:", nudge.Outcome(), "model calls:", len(model.Requests()))
printReceipts(w, s)
return s.Close(ctx)
}
// interrupt: As(Interrupt) cancels the running turn and runs the new
// message next.
func interrupt(ctx context.Context, w io.Writer) error {
fmt.Fprintln(w, "== interrupt")
research, started, _ := slowTool("research")
model := wefttest.Script(
wefttest.ToolCalls(wefttest.Call{Name: "research", ID: "call_1"}),
wefttest.Say("Two tides a day."),
)
s, err := thread.Create(ctx, thread.Memory(), weft.New(model, research), ctxWindow, ids("s_tides", "a"))
if err != nil {
return err
}
long, err := s.Send(ctx, weft.User("Research everything about tides."))
if err != nil {
return err
}
<-started
short, err := s.Send(ctx, weft.User("Stop. Just the short version."), thread.As(thread.Interrupt))
if err != nil {
return err
}
_, err = long.Wait()
fmt.Fprintln(w, "first turn:", long.Outcome(), errors.Is(err, context.Canceled))
res, err := short.Wait()
if err != nil {
return err
}
fmt.Fprintln(w, "second turn:", res.Text())
for _, m := range s.Context() {
for _, p := range m.Content {
if r, ok := p.(weft.ToolResultPart); ok {
fmt.Fprintln(w, "the model reads:", r.Content)
}
}
}
return s.Close(ctx)
}
// steerVerb: Session.Steer joins the turn running now, and never starts
// one.
func steerVerb(ctx context.Context, w io.Writer) error {
fmt.Fprintln(w, "== Session.Steer")
var s *thread.Session
var steerErr error
convert := weft.Tool("convert", "Convert a length.", func(ctx context.Context, _ struct{}) (string, error) {
call, _ := weft.CallFromContext(ctx)
steerErr = s.Steer(ctx, call.RunID, weft.User("Use metric units.")) // this run, by id
return "5 miles", nil
})
model := wefttest.Script(
wefttest.ToolCalls(wefttest.Call{Name: "convert", ID: "call_1"}),
wefttest.Say("About 8 kilometres."),
)
var err error
s, err = thread.Create(ctx, thread.Memory(), weft.New(model, convert), ctxWindow, ids("s_steer", "a"))
if err != nil {
return err
}
err = s.Steer(ctx, "", weft.User("Use metric units."))
fmt.Fprintln(w, "idle:", errors.Is(err, thread.ErrNotRunning), "entries:", len(s.Entries()))
turn, err := s.Send(ctx, weft.User("How far is the lighthouse?")) // its tool steers the run
if err != nil {
return err
}
res, err := turn.Wait()
if err != nil {
return err
}
fmt.Fprintln(w, "steer from the tool:", steerErr)
fmt.Fprintln(w, "answer:", res.Text(), "after:", model.LastRequest().LastText())
err = s.Steer(ctx, turn.RunID(), weft.User("Thanks."))
fmt.Fprintln(w, "after the turn:", errors.Is(err, thread.ErrNotRunning))
return s.Close(ctx)
}
// deferred: a steer that meets an intended end (StopWhen) is never
// drained; it becomes a follow-up turn.
func deferred(ctx context.Context, w io.Writer) error {
fmt.Fprintln(w, "== deferred steer")
var s *thread.Session
var steer *thread.Turn
// The user types while submit runs. The steer is sent on the app's
// ctx, not the tool's: a deferred steer's follow-up runs on the
// context its Send was given, and the tool's ends with the run.
submit := weft.Tool("submit", "Submit the ticket.", func(context.Context, struct{}) (string, error) {
steer, _ = s.Send(ctx, weft.User("Add my phone number too."), thread.As(thread.Steer))
return "ticket 77 submitted", nil
})
model := wefttest.Script(
wefttest.ToolCalls(wefttest.Call{Name: "submit", ID: "call_1"}),
wefttest.Say("Added your phone number to ticket 77."),
)
agent := weft.New(model, submit, weft.StopWhen(weft.HasToolCall("submit")))
var err error
s, err = thread.Create(ctx, thread.Memory(), agent, ctxWindow, ids("s_ticket", "a"))
if err != nil {
return err
}
turn, err := s.Send(ctx, weft.User("File a ticket: my parcel is late."))
if err != nil {
return err
}
if _, err := turn.Wait(); err != nil { // StopWhen ended the run after submit
return err
}
if _, err := steer.Wait(); err != nil {
return err
}
fmt.Fprintln(w, "steer:", steer.Outcome())
followUp := steer.Next() // the turn the steer became
res, err := followUp.Wait()
if err != nil {
return err
}
fmt.Fprintln(w, "follow-up:", res.Text(), "run:", followUp.RunID())
return s.Close(ctx)
}
// printReceipts lists a session's receipt entries: acceptance first, then
// each fate, linked to the acceptance it settles.
func printReceipts(w io.Writer, s *thread.Session) {
for _, e := range s.Entries() {
r, ok := e.(thread.ReceiptEntry)
if !ok {
continue
}
switch r.Status {
case thread.ReceiptQueued, thread.ReceiptAccepted: // acceptance: the message rides here
fmt.Fprintf(w, "receipt %s: %s %q\n", r.ID, r.Status, r.Msg.Text())
default: // a fate: delivered, deferred or dropped
fmt.Fprintf(w, "receipt %s: %s, settles %s\n", r.ID, r.Status, r.Receipt)
}
}
}

View sessions/busy.go on GitHub

View on GitHub · run it from docs-site/examples:

Terminal window
go run ./sessions busy
go test ./sessions
== reject, and steer one send
while busy: true
steer: delivered
the model's last request ends with: Use metric units.
answer: About 8 kilometres.
receipt e_a3: queued "Use metric units."
receipt e_a8: delivered, settles e_a3
== queue and clear
waiting: steer e_a5 Skip the archived orders.
waiting: queue e_a3 Then email it to me.
dropped: 2 <nil>
queued send: dropped true
steer: dropped model calls: 2
receipt e_a4: accepted "Then email it to me."
receipt e_a5: queued "Skip the archived orders."
receipt e_a6: dropped, settles e_a5
receipt e_a7: dropped, settles e_a4
== interrupt
first turn: canceled true
second turn: Two tides a day.
the model reads: tool call research was interrupted: the run was canceled for a newer message
== Session.Steer
idle: true entries: 0
steer from the tool: <nil>
answer: About 8 kilometres. after: Use metric units.
after the turn: true
== deferred steer
steer: deferred
follow-up: Added your phone number to ticket 77. run: s_ticket-t3
  • Send a steer on a context that outlives the turn. A deferred steer’s follow-up runs on its Send context; a tool’s context ends with the run, and the follow-up then fails with ErrNotRun. (Source: ADR 0019 amendment 2026-10-07; thread/errors.go:150)
  • Use s.Steer, not As(Steer), when you do not own the turn. As(Steer) on an idle session starts a turn; Steer never does. (Source: ADR 0019 amendment 2026-10-11; thread/steer.go:145)
  • Bound the queue yourself. The session does not cap waiting steers or sends; each costs one entry. (Source: docs/thread-operations.md §8; ADR 0019 amendment 2026-10-01)
  • A steer cannot decide a parked call. On an open approval boundary it defers to a follow-up turn that runs after the boundary resolves. (Source: ADR 0019 §2; ExampleTurn_Next)
  • Expect Interrupt and Rollback to wait behind the queue. They cancel the running turn but enqueue at the back, exactly as Queue does. With a turn running, then sends A and B queued, then C sent As(Interrupt), the model answers A, B and C in that order. Under Rollback, A and B also run on the rolled-back line. To run the new message next, call s.ClearQueue(ctx) before the send: the waiting sends and steers are dropped, and their Turns end with ErrDropped. (Source: thread/interrupt.go:25-49; thread/turn.go:372-383)
  • Treat ErrBusy as retry. The same call succeeds once the turn has ended (turn.Wait or s.WaitIdle first), or, when the turn parked, once its approval boundary is decided: under Reject an open boundary reads as busy. (Source: thread/errors.go:8-15; thread/turn.go:290-300; docs/thread-operations.md §7)

Symbols used: thread.Policy · Policy.String · thread.Queue · thread.Reject · thread.Steer · thread.Interrupt · thread.Rollback · thread.BusyPolicy · thread.As · Session.Queue · Session.ClearQueue · Session.Steer · thread.QueuedSteer · thread.ReceiptEntry · thread.ReceiptQueued · thread.ReceiptAccepted · thread.ReceiptDelivered · thread.ReceiptDeferred · thread.ReceiptDropped · Turn.Next · thread.ErrBusy · thread.ErrNotRunning · thread.ErrDropped · weft.StopWhen · weft.HasToolCall · weft.CallFromContext · weft.ToolResultPart · weft.ErrInvalidSteer · wefttest.Script