Approve and steer your app's own turns
The devtools panel and Studio’s run page can decide a parked turn of your app’s own weft/thread session (approve, deny, resolve) and steer a turn while it runs. To allow that, your app installs weft/runtime with runtime.Sessions and a resolver that returns the thread.Session behind a public id. Studio never runs your agent or writes your session: it forwards the verb to your process, and your own Session does the work, under its writer lease. Without the option, the turn is read-only and the verbs are refused app-owned turn · read-only: … with the line of code to add.
Hand the runtime your app’s live sessions, in docs-site/examples/devtools/appturns.go:
// resolve is the runtime.Sessions resolver: the app's live Session// behind a public id, with nothing to release (the app keeps writing// it). (nil, nil, nil) says the conversation is not this app's.func (c *conversations) resolve(ctx context.Context, publicID string) (*thread.Session, func(), error) { c.mu.Lock() defer c.mu.Unlock() if s, ok := c.live[publicID]; ok { return s, nil, nil } return nil, nil, nil}
// install opens the runtime link with the opt-in: the app's own turns// then take the panel's continue / skip / resolve and steer. The link// opens under WEFT_ENV=dev; extra adds the caller's options.func (c *conversations) install(link runtime.Option, extra ...runtime.Option) (shutdown func()) { return runtime.Install(append([]runtime.Option{ link, // runtime.Local(srv) for an embedded Studio, else runtime.Studio(url, token) runtime.Agents(c.agt), runtime.Sessions(c.resolve), }, extra...)...)}The session must carry the public id the page names, which is how the resolver is asked for it:
// open returns the conversation's Session, created on first use with// the public id the page names: runtime.Sessions finds it by that id.func (c *conversations) open(ctx context.Context, publicID string) (*thread.Session, error) { c.mu.Lock() defer c.mu.Unlock() if s, ok := c.live[publicID]; ok { return s, nil } s, err := thread.Create(ctx, c.st, c.agt, thread.PublicID(publicID)) if err != nil { return nil, err } c.live[publicID] = s return s, nil}Open the page, ask something that parks on a weft.RequireApproval tool, and the panel’s parked block offers approve, deny and resolve… (before 0.18.0: continue, skip, resolve…). The link opens under WEFT_ENV=dev, or with runtime.Enabled(true).
Runtime sessions
Section titled “Runtime sessions”runtime.Sessions(resolve) is an option of runtime.Install. Its argument is a runtime.SessionResolver:
func(ctx context.Context, publicID string) (s *thread.Session, release func(), err error)| Return | Means |
|---|---|
your live Session, nil release | Preferred. The runtime acts through your own writer; there is no lease to contend for |
a Session you opened from your store, with its Close as release | Works, with two costs: the runtime holds the writer lease through the whole resumed turn (your own writes fail thread.ErrLocked meanwhile), and the resumed run’s context carries none of your app’s values (loggers, tenancy, deadlines) |
nil, nil, nil | Not this app’s conversation: refused no session for this public id |
an error wrapping thread.ErrLocked | Refused the app is writing this session; fork instead |
an error wrapping thread.ErrStale | The runtime releases and asks once more |
| any other error | Refused with its text, which reaches whoever acted, so it must carry no secret |
Each resolve, and the lease claim after it, is bounded at 10 s (session resolver timed out, session claim timed out).
With the option, the runtime’s registration says "sessions": true in GET /api/runtimes, and GET /api/meta reads "debug_scope":"own+runtime" while it is connected. Without it nothing about your app’s runs changes. ExampleSessions is the godoc’s version of the region above.
What a turn needs
Section titled “What a turn needs”A turn takes the verbs when all of these hold:
- It is a turn of a
weft/threadsession created withthread.PublicID, so its run carriesweft.public_idandweft.session.id. - Studio serves the playground:
studio.New(…, studio.Playground(true)), orweft studiowithout--no-playground. Without it the approvals, steer and cancel routes do not exist, and the panel saysapp-owned turn · read-only: the playground is off on this Studio. - A runtime of the run’s own service is connected and registered
runtime.Sessions. The runtime names its service fromOTEL_SERVICE_NAME, else the binary’s name, and compares it with the run row’sservice, the run’sservice.name. - In the panel, the turn is its session’s latest.
- The token may act: no token in setup A, the dev token, or a playground-scoped panel token for this public id. A read-scoped panel token never can (Secure the devtools panel).
meta.debug_scope is global, so a Sessions runtime of another service, or only breakpoints_own, leaves the turn read-only; the panel checks the run’s own service.
The same two conditions decide breakpoints: an agent built with runtime.Breakpoints() parks a thread turn on a breakpoint only when the process set runtime.Sessions and the session has a public id, so the park can be decided (Pause runs at breakpoints).
Decide and steer
Section titled “Decide and steer”A parked app turn’s block in the panel reads awaiting a decision, one card per parked call, each naming the tool with its arguments on one line, as the step’s call list does:
| Verb | Decision | What the model sees |
|---|---|---|
| approve | approve | the handler runs for real |
| deny | deny | a denied result; the field is the reason, else skipped from the devtools panel |
| resolve… | resolve | the value typed in the field as the tool’s result; the handler never runs |
The field beside the verbs is 25 characters wide, its placeholder result or deny reason, shown whole: type the result for resolve… or the reason for deny there.


The verbs post POST /api/runs/{id}/approvals, the playground’s own route. Studio answers 202 at once with the command to poll:
{"command_id":"cmd_01M578SXW81BVCSNMWJ1JZT065","state":"queued","run_id":"s_01M578SXW1PRXVTS5M0KGQKAPZ-t1","runtime_id":"rt_221532cdf96d5401"}The panel polls GET /api/playground/commands/{id} until the command ends. Your runtime resolves the session, claims its writer lease, acks, and then calls the session’s own Decide, which resumes the turn as the session’s next turn: your app’s run, with its own weft.session.id and weft.turn, never a playground run. The parked turn then reads decided — resumed as <session>-t2 (a button that selects that turn), and the turn list shows parked → t2. A reload says the same, with no verbs.
A refusal arrives on the command row as state: "rejected" and is shown word for word:
| Refusal | Why |
|---|---|
the app is writing this session; fork instead | another writer holds the session; nothing is written |
no session for this public id | your resolver answered nil |
the session resolver answered session <id> for public id <pub>, not the run's session <id> | one public id names two sessions, as when an app creates a new session per process start |
this session requires signed decisions: decide it in the app (thread.DecideSigned) | a thread.RequireSigned session; the runtime decides unsigned only, so your app decides it with s.DecideSigned |
Steer while it runs
Section titled “Steer while it runs”While an app turn runs, the panel shows steer this turn, a field (“a message delivered mid-flight”) and a steer button. It posts POST /api/runs/{id}/steer with the run’s own id, and the runtime calls thread.Session.Steer with that id. The message joins the turn at its next drain point (after the current tool batch, or before what would be the final step), and the panel says steered; the finished turn’s Story shows steered: <message>.
Steer is delivered only into the run it names, and only while that run is the session’s running turn. Since 0.20.0 Studio refuses a steer on an app turn that is parked, or that succeeded or failed, at once, 409, before any command is queued. A parked turn gets no running turn to steer: run <id> is parked: decide its pending calls (approve/deny) instead. A turn whose row reads succeeded or failed gets no running turn to steer: run <id> already finished (<status>). A turn whose row still reads running goes to your runtime, and when it has ended there the command finishes failed, nothing written: no running turn to steer for run <id> (…), naming the run that is running instead when there is one. A delivered steer that misses its drain point (the turn hits its StopWhen, parks, or ends first) becomes a follow-up turn, as any thread steer does.
Cancel is never offered on an app turn: thread has no per-turn cancel. Through the API, POST /api/runs/{id}/cancel on an app turn with a Sessions runtime connected is refused at once. The 409 reads cancel is not supported on an app turn: run <id> is the app's own turn (on a parked turn, run <id> is parked: decide its pending calls (approve/deny) instead). Before 0.20.0 it answered 202 and the command was rejected afterwards. Without such a runtime it is the 403 below.
What is refused without it, and why
Section titled “What is refused without it, and why”An agent is immutable after weft.New, and a session has one writer, your app’s. Studio cannot run your agent, and writing your session behind its Session would break thread’s first rule. So a run no runtime started is viewer-only until your app opts in and does the writing itself.
Without a way to act, the approvals, steer and cancel routes answer alike, 403 with the fix beside the error:
{"error":{"code":"forbidden","message":"app-owned turn · read-only: run s_01M578SXXF5QVNEJBQYT9EZ161-t1 is the app's own, and no connected runtime of its service devtools.test acts on the app's sessions"},"fix":"set runtime.Sessions(resolver) at runtime.Install in the app (service devtools.test): its turns then take approvals and steer"}| The 403’s message | Fix |
|---|---|
app-owned turn · read-only: no runtime of service <svc> is connected | open the runtime link in the app: WEFT_ENV=dev (or runtime.Enabled(true)) at runtime.Install, with runtime.Sessions(resolver) |
app-owned turn · read-only: run <id> is the app's own, and no connected runtime of its service <svc> acts on the app's sessions | set runtime.Sessions(resolver) at runtime.Install in the app (service <svc>): its turns then take approvals and steer |
app-owned turn · read-only: the app's runtime <id> of service <svc> disconnected — last seen <UTC second> | restart the app: its runtime link registers again |
app-owned turn · read-only: no runtime of service <svc> is connected here — the app's link dialled <url> | open the panel there (data-endpoint), or link in-process: runtime.Local(srv) |
app-owned turn · read-only: run <id> is not a turn of a session with a public id | run the agent through weft/thread with thread.PublicID(...): runtime.Sessions resolves a session by its public id; for a plain Generate’s park (no session), resume it in your app; to decide here, run it as a session turn with thread.PublicID(...) (weft/thread) |
Studio picks the first that applies. An app that registered runtime.Sessions and went away (it stopped, crashed, or Studio restarted under it) reads disconnected, with the restart fix, even while another process of the same service, a CLI, is connected without Sessions. While a Sessions runtime of the service is connected, no disconnected app is reported. The “dialled” case applies when no runtime of the service is known here and this process’s own runtime link dialled another Studio, the one /api/meta’s runtime_link.studio_url names. That case is said only to the server token, or to setup A on loopback, never to a panel token.
In the panel the block reads awaiting a decision (read-only). The three verbs are drawn disabled (data-weft-refused="app-owned"), followed by one line in the server’s words, app-owned turn · read-only: … — fix: …. Under a read-scoped panel token the line is this token is read-only: mint it with "playground": true to act. The panel reads GET /api/runtimes at most every 10 s while such a turn is drawn, so the verbs come back on their own once the link opens or the fix is applied.
Other parked runs the panel tells apart:
| Run | The panel says |
|---|---|
a plain agt.Generate that parked (no session) | parked · app-owned run · the app resumes it (weft.Approve), with the thread.PublicID(...) fix |
a weft.Subagent child | parked · a subagent's call — decide on the parent turn <parent>, no verb |
a thread/pool child | parked · a pool session's turn — decide on the parent session <session>, followed by · started by turn <run> when known; no verb |
A plain Generate is resumed by your code, agt.Generate(ctx, weft.Messages(res.Messages...), weft.Approve(id)) (Require approval before a tool runs). The panel’s turn lists hold top-level runs only, so it draws child runs only when you reach one another way.
Runs started inside a tool handler are told apart by their ids:
| Started in a tool handler | Id | Reads as |
|---|---|---|
Another agent’s Generate | a parent run id and an id of its own | A plain Generate: it never parks onto the parent, even though it inherits the enclosing turn’s session id, public id and weft.session.parent through context metadata |
A session’s own Send | <session>-tN | A turn of that session, gated like any other |
A session’s turn stamps every identity key it owns with its own value or "", so a session created without thread.PublicID reads as having none, never the enclosing session’s. The keys are weft.public_id, weft.session.title, weft.session.forked_from, weft.session.parent and weft.session.parent_call (since v0.15.1).
A failed read is said as such, never as a verdict, and the run page still draws the events and the parked block:
| Read that failed | Line |
|---|---|
| The session | could not read the session: … |
| The runtimes | could not read the runtimes: … |
/api/meta | could not read the Studio's capabilities: …; Studio’s traces, live, experiments, playground and compare pages say it the same way, never that the capability is off |
| Behaviour | Rule |
|---|---|
| A 403 refusal | Holds until a runtimes read that started after it says decide |
| The session re-read | Every 5 s while the turn is parked or running; it stops once the next turn is known, or after a 401, 403, 404 or 410 |
| The runtimes poll | Held at 5 s while the turn is parked or running |
Complete example
Section titled “Complete example”appturns.go is a support app’s conversations: an agent with a gated refund and a track lookup, one Session per public id, and the resolver above:
package main
// appturns.go is the app-turns and security pages' program: the app's// own thread turns, decided and steered from the devtools panel (and// Studio's run page) through runtime.Sessions, and the per-page panel// token a shipped page carries.//// Studio never runs the app's agent and never writes its session. A// verb on one of the app's turns (approve, deny, resolve, steer) goes// to a connected runtime of the app's service that registered// runtime.Sessions, and the runtime acts through the Session the app's// resolver hands back. appturns_test.go runs it offline against an// in-process Studio.
import ( "bytes" "context" "encoding/json" "fmt" "html/template" "net/http" "strings" "sync" "sync/atomic"
"github.com/weftgo/weft" "github.com/weftgo/weft/runtime" "github.com/weftgo/weft/thread")
// refunds counts the refunds the gated tool issued.var refunds atomic.Int32
// newTurnsAgent is the support agent of the app's conversations: refund// is gated (weft.RequireApproval parks every call), and track looks a// parcel up, waiting on hold first — the test holds it so a turn is// still running when the steer arrives.func newTurnsAgent(m weft.Model, hold func(context.Context) error, opts ...weft.Option) *weft.Agent { refund := weft.Tool("refund", "Refund an order.", func(ctx context.Context, in struct { OrderID string `json:"order_id"` }) (string, error) { refunds.Add(1) return "refunded " + in.OrderID, nil }, weft.RequireApproval()) track := weft.Tool("track", "Track an order's parcel.", func(ctx context.Context, in struct { OrderID string `json:"order_id"` }) (string, error) { if err := hold(ctx); err != nil { return "", err } return "order " + in.OrderID + " is with the carrier", nil }) return weft.New(m, append([]weft.Option{weft.Name("acme-support"), refund, track}, opts...)...)}
// conversations holds the app's live Sessions, one per public id: the// conversation the page shows, the one the panel follows.type conversations struct { agt *weft.Agent st thread.Storage mu sync.Mutex live map[string]*thread.Session}
func newConversations(agt *weft.Agent) *conversations { return &conversations{agt: agt, st: thread.Memory(), live: map[string]*thread.Session{}}}
// open returns the conversation's Session, created on first use with// the public id the page names: runtime.Sessions finds it by that id.func (c *conversations) open(ctx context.Context, publicID string) (*thread.Session, error) { c.mu.Lock() defer c.mu.Unlock() if s, ok := c.live[publicID]; ok { return s, nil } s, err := thread.Create(ctx, c.st, c.agt, thread.PublicID(publicID)) if err != nil { return nil, err } c.live[publicID] = s return s, nil}
// resolve is the runtime.Sessions resolver: the app's live Session// behind a public id, with nothing to release (the app keeps writing// it). (nil, nil, nil) says the conversation is not this app's.func (c *conversations) resolve(ctx context.Context, publicID string) (*thread.Session, func(), error) { c.mu.Lock() defer c.mu.Unlock() if s, ok := c.live[publicID]; ok { return s, nil, nil } return nil, nil, nil}
// install opens the runtime link with the opt-in: the app's own turns// then take the panel's continue / skip / resolve and steer. The link// opens under WEFT_ENV=dev; extra adds the caller's options.func (c *conversations) install(link runtime.Option, extra ...runtime.Option) (shutdown func()) { return runtime.Install(append([]runtime.Option{ link, // runtime.Local(srv) for an embedded Studio, else runtime.Studio(url, token) runtime.Agents(c.agt), runtime.Sessions(c.resolve), }, extra...)...)}
// close closes every Session (thread's T2).func (c *conversations) close() { c.mu.Lock() defer c.mu.Unlock() for _, s := range c.live { _ = s.Close(context.Background()) }}
// mintPanelToken asks Studio for a token scoped to one conversation,// with the server token, which never leaves the backend. playground// lets the page act (replay, decide, steer); without it the token reads.func mintPanelToken(ctx context.Context, studioURL, serverToken, publicID string, playground bool) (string, error) { body, err := json.Marshal(map[string]any{"public_id": publicID, "ttl": "15m", "playground": playground}) if err != nil { return "", err } req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimSuffix(studioURL, "/")+"/api/panel-tokens", bytes.NewReader(body)) if err != nil { return "", err } req.Header.Set("Authorization", "Bearer "+serverToken) req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { return "", err } defer func() { _ = resp.Body.Close() }() var out struct { Token string `json:"token"` } if resp.StatusCode != http.StatusOK || json.NewDecoder(resp.Body).Decode(&out) != nil { return "", fmt.Errorf("mint panel token: %s", resp.Status) } return out.Token, nil}
// panelTag is the page's script tag: the hosted Studio, the minted// token and the conversation, escaped as attributes.var panelTag = template.Must(template.New("panel").Parse( `<script type="module" src="{{.Studio}}panel.js" data-weft` + ` data-endpoint="{{.Studio}}" data-token="{{.Token}}" data-scope="{{.PublicID}}"></script>`))View on GitHub · its tests run each verb offline, against Studio embedded in the test with studio.New(…, studio.Playground(true)) and the runtime linked in-process with runtime.Local. From docs-site/examples:
go test -run 'AppTurn' -v ./devtools=== RUN TestDecideAppTurn appturns_test.go:192: POST /api/runs/s_01M578SXW1PRXVTS5M0KGQKAPZ-t1/approvals: 202 {"command_id":"cmd_01M578SXW81BVCSNMWJ1JZT065","state":"queued","run_id":"s_01M578SXW1PRXVTS5M0KGQKAPZ-t1","runtime_id":"rt_221532cdf96d5401"} appturns_test.go:192: command: {State:finished RunID:s_01M578SXW1PRXVTS5M0KGQKAPZ-t2 Status:succeeded Error:<nil>}--- PASS: TestDecideAppTurn (0.03s)=== RUN TestSteerAppTurn appturns_test.go:241: POST /api/runs/s_01M578SXWQEMYM7R6ZV1J2XWE1-t1/steer: 202 {"command_id":"cmd_01M578SXWW8K6Y8ADFJW3EBCH3","state":"queued","run_id":"s_01M578SXWQEMYM7R6ZV1J2XWE1-t1","runtime_id":"rt_221532cdf96d5401"} appturns_test.go:241: command: {State:finished RunID:s_01M578SXWQEMYM7R6ZV1J2XWE1-t1 Status:succeeded Error:<nil>}--- PASS: TestSteerAppTurn (0.02s)=== RUN TestAppTurnReadOnlyWithoutSessions appturns_test.go:293: POST /approvals: 403 {"error":{"code":"forbidden","message":"app-owned turn · read-only: run s_01M578SXXF5QVNEJBQYT9EZ161-t1 is the app's own, and no connected runtime of its service devtools.test acts on the app's sessions"},"fix":"set runtime.Sessions(resolver) at runtime.Install in the app (service devtools.test): its turns then take approvals and steer"} …--- PASS: TestAppTurnReadOnlyWithoutSessions (0.01s)PASSTestDecideAppTurn parks a turn on refund, approves it through the approvals route, and checks the command names the resumed turn, turn.Next() is that turn, and the refund ran once. TestSteerAppTurn holds track so the turn is still running, steers it, and finds the message in the turn’s transcript. TestAppTurnReadOnlyWithoutSessions installs the runtime without runtime.Sessions and gets the 403 from all three routes, the turn still parked.
weft’s own demo is thread/examples/approvals -serve: WEFT_ENV=dev go run ./thread/examples/approvals -serve 127.0.0.1:7331, ask “deploy to prod”, and decide it in the panel docked on the page.
Best practices & gotchas
Section titled “Best practices & gotchas”- Name the service with
OTEL_SERVICE_NAME. The runtime reads only that variable, else the binary’s name; a run named byotel.Service(...)orOTEL_RESOURCE_ATTRIBUTEShas anotherservice, so its turns stay read-only. (Source:runtime/registry.go:243-252;otel/options.go:58;otel/env.go:20) - Return your live
Sessionfrom the resolver. An opened one holds the writer lease through the resumed turn and loses your context’s values. (Source:runtime/options.go:183-191) - Give every conversation’s session a
thread.PublicID, and reopen the same session for it after a restart. The resolver is asked by public id; one public id naming two sessions is refused. (Source:studio/appturn.go:76-80;studio/README.md:1773) - Open the link in development only. Without
WEFT_ENV=dev(orruntime.Enabled(true)) no runtime of the service is connected, and the turn readsno runtime of service … is connected. (Source:runtime.Enabledgodoc,runtime/options.go:249-251;studio/appturn.go:95-117) - Decide signed sessions in your app. The runtime refuses a
thread.RequireSignedsession before the ack; useDecideSigned. (Source:runtime/sessions.go:34,runtime/sessions.go:96-102) - Read
steeredas delivered to the running turn, not as applied. A steer that misses its drain point becomes a follow-up turn. (Source:runtime/options.go:215-222) - With several replicas of one service, Studio picks the Sessions runtime seen most recently. It may not hold the session; the command is refused (
the app is writing this session; fork insteadorno session for this public id) and nothing is written. (Source:studio/README.md:1773)
See also
Section titled “See also”- Pause runs at breakpoints, then steer or cancel them
- Decide approvals in a session
- Connect your app to Studio’s playground
- Steer a running turn
- Secure the devtools panel
Symbols used: runtime.Sessions · runtime.SessionResolver · runtime.Install · runtime.Agents · runtime.Local · runtime.Enabled · thread.Session · thread.PublicID · thread.Create · thread.ErrLocked · thread.ErrStale · Session.Decide · Session.Steer · weft.RequireApproval · weft.Approve · studio.New · studio.Playground