---
title: Approve and steer your app's own turns
description: Decide a parked turn of your app's own thread session (approve, deny, resolve) or steer a running one from the devtools panel and Studio's run page, by installing weft/runtime with runtime.Sessions; without it the verbs are refused "app-owned turn · read-only" with the one-line fix.
url: https://weftgo.dev/docs/observe/devtools/app-turns/
type: how-to
sources:
  - 'W11-tag3, v0.20.2: weft/studio/web/src/panel/element.ts:7354-7368 (parkedHead: a parked call''s arguments on one line); weft/CHANGELOG.md:18 (0.20.2)'
  - 'weft/runtime/options.go:174-198 (SessionResolver: the live Session or an opened one with release; (nil, nil, nil); ErrLocked, ErrStale; error text reaches whoever acted), 200-246 (Sessions: decide through Decide, steer only into the named running run, cancel refused, the lease claimed before the ack, RequireSigned refused, the skipped reason, opt-in, 10 s bounds)'
  - 'weft/runtime/sessions.go:1-44 (the verbs, the refusals word for word, skippedReason), 55-160 (dispatchSession: validation, resolve and claim, the ack before the write, Decide, Steer, the finished ack''s words)'
  - weft/runtime/example_test.go:ExampleSessions
  - 'weft/studio/appturn.go:1-17 (Studio never runs the app''s agent; 202 at once; refusals on the command row), 57-151 (appTurnRuntime and appRefusal: the 403s and their fixes, in this order: the app''s runtime disconnected, a service runtime without Sessions, the link dialled another Studio (mayReadDBPath, meta.go:299-308), none connected; then no session or no public id), 169-196 (enqueueAppTurn: 404, 503, 409, 202 {command_id, state, run_id, runtime_id})'
  - 'weft/studio/debugger.go:1-11, 89-223 (serveSteer: mayAct, the app-owned path, a panel token inside its public id)'
  - 'weft/studio/README.md:1511-1560 (the app''s own turns in the panel, plan F4.3: the gate, continue / skip / resolve, the poll, decided — resumed as, the rejection verbatim, steer, cancel never offered, the read-only words, the runtimes poll, plain Generate and session-without-public-id lines, children)'
  - 'weft/thread/turn.go:1349-1444 (runMetadata: every session-owned key stamped, "" when none; sessionOwnedKeys)'
  - 'weft/CHANGELOG.md:435-451 (0.15.1: a session''s run carries its own identity, never an enclosing run''s)'
  - 'weft/studio/web/src/lib/honesty.ts:569-575 (RUNTIMES_UNREAD, SESSION_UNREAD, CAPS_UNREAD), 677-770 (parkedKind: Subagent child, pool child, nested Generate plain, a session''s own Send its turn)'
  - 'weft/studio/web/src/components/studio/parked-calls.tsx:363-403 (APP_GATE_POLL_MS 5 s: the session re-read until resumed, or a 401/403/404/410 at 280-282; useRuntimesNeeded)'
  - weft/studio/web/src/components/studio/caps-unread.tsx:4-13; weft/studio/web/src/routes/live.tsx, experiments.index.tsx, compare.tsx, traces.index.tsx, playground.tsx (CapsUnread)
  - weft/studio/README.md:1571-1621 (nested runs, failed reads, the 403's lifetime)
  - 'weft/studio/README.md:1596-1616 (the demo: thread/examples/approvals -serve; without WEFT_ENV=dev the link is closed)'
  - 'weft/studio/README.md:1773 (plan F4.1: the session command, the 202, the rejections, the finished-failed answers, several replicas, the 403)'
  - 'weft/AGENTS.md:357-360 (block 7: a parked app turn decided in the panel and on the run page; steer while it runs; refused in the server''s words), 484 (runtime.Sessions), 523-529 (who decides a parked run; debug_scope; steer on the app''s turns)'
  - weft/CHANGELOG.md:476 (the panel's verbs on app turns, plan F4.3; the closed-link 403), 590 (runtime.Sessions, plan F4.1), 591 (the session frame), 625 (approvals, steer and cancel on an app-owned run answer alike; cancel 403), 626 (meta.debug_scope own+runtime)
  - 'weft/thread/examples/approvals/serve.go (the served demo: runtime.Breakpoints, runtime.Sessions, the resolver claiming the lease)'
  - docs-site/examples/devtools/appturns_test.go:TestDecideAppTurn, TestSteerAppTurn, TestAppTurnReadOnlyWithoutSessions
  - 'docs-site/factcheck/playground.md (findings 29-31, 35; open question 3: a steer on a finished run is dropped)'
  - 'observed: Chromium (Playwright 1.62.0) against a scratch app embedding weft studio v0.17.0 with runtime.Local (WEFT_DISCOVERY=off). With runtime.Sessions: a parked turn''s block reads awaiting a decision with continue (title Approve: the handler runs for real), skip (Deny: the model sees a denied result), a result field and resolve… (Resolve: the model sees the result typed here; the handler never runs); continue made it read decided — resumed as s_…-t2, and the turn list parked → t2; a running turn''s steer this turn box (a message delivered mid-flight) read steered, and the finished turn''s story shows steered: <message>; no cancel verb. Without runtime.Sessions: awaiting a decision (read-only), the three verbs disabled with data-weft-refused=app-owned and the line app-owned turn · read-only: run … is the app''s own, and no connected runtime of its service dt2-app acts on the app''s sessions — fix: set runtime.Sessions(resolver) at runtime.Install in the app (service dt2-app): its turns then take approvals and steer'
  - weft/studio/web/src/lib/honesty.ts:579-583, 381-388 (the playground is off on this Studio), 602-603, 909-910 (a pool session's turn; · started by turn)
  - 'weft/studio/web/src/panel/element.ts:6412-6420, 6571-6575 (decided — resumed as: a data-weft-resumed button)'
  - 'W11-tag3, v0.20.2: weft/studio/web/src/lib/decisions.ts:114-115 (DECIDE_FIELD: result or deny reason, whole in the 25ch field)'
  - 'weft/runtime/registry.go:243-252 (serviceName: OTEL_SERVICE_NAME, else os.Args[0]); weft/otel/options.go:58 (otel.Service); weft/otel/env.go:20'
  - 'weft/AGENTS.md:141-144 (block 3a: the two drain points), 521-526 (block 7a: a thread turn parks on a breakpoint only with runtime.Sessions and a public id)'
  - 'weft/studio/cancel.go:55-58 (appCancelRefused), 95-110 (an app turn with a Sessions runtime: 409 at once, the parked case first); weft/studio/debugger.go:128-131 (appSteerRefused), 176-191 (a steer on a parked or finished app turn: 409 at once); weft/CHANGELOG.md:87 (0.20.0 Breaking, U33.1), 107 (0.19.0: the disconnected app, the dialled Studio)'
---

# 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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#Sessions) and a resolver that returns the [`thread.Session`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/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`:

```go title="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...)...)
}
```

[devtools/appturns.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/devtools/appturns.go)

The session must carry the public id the page names, which is how the resolver is asked for it:

```go title="devtools/appturns.go"
// 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
}
```

[devtools/appturns.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/devtools/appturns.go)

Open the page, ask something that parks on a [`weft.RequireApproval`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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

`runtime.Sessions(resolve)` is an option of `runtime.Install`. Its argument is a [`runtime.SessionResolver`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#SessionResolver):

```text
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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#example-Sessions) is the godoc's version of the region above.

### What a turn needs

A turn takes the verbs when all of these hold:

- It is a turn of a `weft/thread` session created with [`thread.PublicID`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#PublicID), so its run carries `weft.public_id` and `weft.session.id`.
- Studio serves the playground: `studio.New(…, studio.Playground(true))`, or `weft studio` without `--no-playground`. Without it the approvals, steer and cancel routes do not exist, and the panel says `app-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 from `OTEL_SERVICE_NAME`, else the binary's name, and compares it with the run row's `service`, the run's `service.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](https://weftgo.dev/docs/observe/devtools/security/#what-each-token-can-do)).

`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](https://weftgo.dev/docs/observe/playground/breakpoints/#breakpoints-on-your-own-runs)).

## 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.

![A parked app turn in the panel: the refund call is awaiting a decision, with approve, deny, a result or deny reason field and resolve… under it](https://weftgo.dev/docs/_astro/panel-parked-turn.light.CTN3vEDS.webp)

*An app turn parked on its refund call, awaiting a decision.*

The verbs post `POST /api/runs/{id}/approvals`, the playground's own route. Studio answers 202 at once with the command to poll:

```json
{"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

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](https://weftgo.dev/docs/sessions/busy-and-steering/) 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

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](https://weftgo.dev/docs/sessions/overview/). 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:

```json
{"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](https://weftgo.dev/docs/guides/approvals/)). 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

`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:

```go title="devtools/appturns.go"
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>`))
```

[devtools/appturns.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/devtools/appturns.go)

[View on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/devtools/appturns.go) · 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`:

```sh
go test -run 'AppTurn' -v ./devtools
```

```text
=== 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)
PASS
```

`TestDecideAppTurn` 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

- **Name the service with `OTEL_SERVICE_NAME`.** The runtime reads only that variable, else the binary's name; a run named by `otel.Service(...)` or `OTEL_RESOURCE_ATTRIBUTES` has another `service`, so its turns stay read-only. (Source: `runtime/registry.go:243-252`; `otel/options.go:58`; `otel/env.go:20`)
- **Return your live `Session` from 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` (or `runtime.Enabled(true)`) no runtime of the service is connected, and the turn reads `no runtime of service … is connected`. (Source: `runtime.Enabled` godoc, `runtime/options.go:249-251`; `studio/appturn.go:95-117`)
- **Decide signed sessions in your app.** The runtime refuses a `thread.RequireSigned` session before the ack; use `DecideSigned`. (Source: `runtime/sessions.go:34`, `runtime/sessions.go:96-102`)
- **Read `steered` as 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 instead` or `no session for this public id`) and nothing is written. (Source: `studio/README.md:1773`)

## See also

- [Pause runs at breakpoints, then steer or cancel them](https://weftgo.dev/docs/observe/playground/breakpoints/)
- [Decide approvals in a session](https://weftgo.dev/docs/sessions/approvals/)
- [Connect your app to Studio's playground](https://weftgo.dev/docs/observe/playground/connect/)
- [Steer a running turn](https://weftgo.dev/docs/guides/steering/)
- [Secure the devtools panel](https://weftgo.dev/docs/observe/devtools/security/)

Symbols used: [`runtime.Sessions`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#Sessions) · [`runtime.SessionResolver`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#SessionResolver) · [`runtime.Install`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#Install) · [`runtime.Agents`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#Agents) · [`runtime.Local`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#Local) · [`runtime.Enabled`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#Enabled) · [`thread.Session`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#Session) · [`thread.PublicID`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#PublicID) · [`thread.Create`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#Create) · [`thread.ErrLocked`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#ErrLocked) · [`thread.ErrStale`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#ErrStale) · [`Session.Decide`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#Session.Decide) · [`Session.Steer`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#Session.Steer) · [`weft.RequireApproval`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#RequireApproval) · [`weft.Approve`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Approve) · [`studio.New`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#New) · [`studio.Playground`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Playground)
