v0.51.0 a9d674e

cli.go RawHistoryFolder

1package agent
2
3import (
4	"bufio"
5	"context"
6	"encoding/json"
7	"errors"
8	"fmt"
9	"io"
10	"os"
11	"os/signal"
12	"path/filepath"
13	"slices"
14	"strings"
15	"sync"
16	"syscall"
17	"time"
18
19	"github.com/bnema/zut/packages/agent/extensions"
20	"github.com/bnema/zut/packages/agent/extproto"
21	"github.com/bnema/zut/packages/agent/modes"
22	"github.com/bnema/zut/packages/agent/scheduler"
23	"github.com/bnema/zut/packages/agent/skills"
24	"github.com/bnema/zut/packages/agent/subagents"
25	"github.com/bnema/zut/packages/agent/tools"
26	"github.com/bnema/zut/packages/core"
27	"github.com/bnema/zut/packages/provider"
28	"github.com/bnema/zut/packages/provider/auth"
29	"github.com/bnema/zut/packages/tui"
30)
31
32// interactiveExtHooks is a tiny adapter that lets the extension
33// manager call back into the Interactive instance built later in
34// runInteractive. Alerts are buffered until the instance is attached so
35// an extension that signals attention during startup is not silently lost.
36// Keep the startup buffer bounded because extensions can flood their host.
37const maxBufferedInteractiveAlerts = 64
38
39type interactiveExtHooks struct {
40	mu              sync.Mutex
41	interactive     *modes.Interactive
42	pendingAlerts   []interactiveAlert
43	pendingStatuses map[string]interactiveStatus
44	pendingWidgets  map[string]interactiveWidget
45}
46
47type interactiveAlert struct {
48	extName string
49	alert   extproto.AlertRequest
50}
51
52type interactiveStatus struct {
53	extName string
54	key     string
55	level   string
56	text    string
57}
58
59type interactiveWidget struct {
60	extName  string
61	id       string
62	position string
63	title    string
64	lines    []string
65}
66
67func (h *interactiveExtHooks) iv() *modes.Interactive {
68	if h == nil {
69		return nil
70	}
71	h.mu.Lock()
72	defer h.mu.Unlock()
73	return h.interactive
74}
75
76func (h *interactiveExtHooks) SupportsInteractiveTools() bool { return h.iv() != nil }
77
78func (h *interactiveExtHooks) attachInteractive(iv *modes.Interactive) {
79	if h == nil || iv == nil {
80		return
81	}
82	h.mu.Lock()
83	h.interactive = iv
84	pendingAlerts := h.pendingAlerts
85	pendingStatuses := h.pendingStatuses
86	pendingWidgets := h.pendingWidgets
87	h.pendingAlerts = nil
88	h.pendingStatuses = nil
89	h.pendingWidgets = nil
90	h.mu.Unlock()
91	for _, item := range pendingAlerts {
92		iv.Alert(item.extName, item.alert)
93	}
94	for _, item := range pendingStatuses {
95		iv.SetStatus(item.extName, item.key, item.level, item.text)
96	}
97	for _, item := range pendingWidgets {
98		iv.SetWidget(item.extName, item.id, item.position, item.title, item.lines)
99	}
100}
101
102func (h *interactiveExtHooks) Notify(extName, level, message string) {
103	if iv := h.iv(); iv != nil {
104		iv.Notify(extName, level, message)
105	}
106}
107func (h *interactiveExtHooks) Alert(extName string, alert extproto.AlertRequest) {
108	if h == nil {
109		return
110	}
111	h.mu.Lock()
112	if h.interactive == nil {
113		if len(h.pendingAlerts) < maxBufferedInteractiveAlerts {
114			h.pendingAlerts = append(h.pendingAlerts, interactiveAlert{extName: extName, alert: alert})
115		}
116		h.mu.Unlock()
117		return
118	}
119	iv := h.interactive
120	h.mu.Unlock()
121	iv.Alert(extName, alert)
122}
123func (h *interactiveExtHooks) Submit(text string) {
124	if iv := h.iv(); iv != nil {
125		iv.SubmitOrQueue(text, nil)
126	}
127}
128func (h *interactiveExtHooks) SubmitSlash(text string) {
129	if iv := h.iv(); iv != nil {
130		iv.SubmitSlash(text)
131	}
132}
133func (h *interactiveExtHooks) Insert(text string) {
134	if iv := h.iv(); iv != nil {
135		iv.Insert(text)
136	}
137}
138func (h *interactiveExtHooks) Display(extName, text string) {
139	if iv := h.iv(); iv != nil {
140		iv.Display(extName, text)
141	}
142}
143func (h *interactiveExtHooks) ClearNotes(extName string) {
144	if iv := h.iv(); iv != nil {
145		iv.ClearNotes(extName)
146	}
147}
148func (h *interactiveExtHooks) OpenPanel(extName string, spec extproto.PanelSpec) {
149	if iv := h.iv(); iv != nil {
150		iv.OpenPanel(extName, spec)
151	}
152}
153func (h *interactiveExtHooks) UpdatePanel(extName, panelID, title string, lines []string, footer string) {
154	if iv := h.iv(); iv != nil {
155		iv.UpdatePanel(extName, panelID, title, lines, footer)
156	}
157}
158func (h *interactiveExtHooks) ClosePanel(extName, panelID string) {
159	if iv := h.iv(); iv != nil {
160		iv.ClosePanel(extName, panelID)
161	}
162}
163func pendingChromeKey(extName, key string) string { return extName + "\x00" + key }
164
165func (h *interactiveExtHooks) SetStatus(extName, key, level, text string) {
166	if h == nil {
167		return
168	}
169	h.mu.Lock()
170	if h.interactive == nil {
171		if h.pendingStatuses == nil {
172			h.pendingStatuses = map[string]interactiveStatus{}
173		}
174		pendingKey := pendingChromeKey(extName, key)
175		if strings.TrimSpace(text) == "" {
176			delete(h.pendingStatuses, pendingKey)
177		} else {
178			h.pendingStatuses[pendingKey] = interactiveStatus{extName: extName, key: key, level: level, text: text}
179		}
180		h.mu.Unlock()
181		return
182	}
183	iv := h.interactive
184	h.mu.Unlock()
185	iv.SetStatus(extName, key, level, text)
186}
187func (h *interactiveExtHooks) SetWidget(extName, id, position, title string, lines []string) {
188	if h == nil {
189		return
190	}
191	h.mu.Lock()
192	if h.interactive == nil {
193		if h.pendingWidgets == nil {
194			h.pendingWidgets = map[string]interactiveWidget{}
195		}
196		h.pendingWidgets[pendingChromeKey(extName, id)] = interactiveWidget{
197			extName: extName, id: id, position: position, title: title, lines: append([]string(nil), lines...),
198		}
199		h.mu.Unlock()
200		return
201	}
202	iv := h.interactive
203	h.mu.Unlock()
204	iv.SetWidget(extName, id, position, title, lines)
205}
206func (h *interactiveExtHooks) ClearWidget(extName, id string) {
207	if h == nil {
208		return
209	}
210	h.mu.Lock()
211	if h.interactive == nil {
212		if h.pendingWidgets != nil {
213			delete(h.pendingWidgets, pendingChromeKey(extName, id))
214		}
215		h.mu.Unlock()
216		return
217	}
218	iv := h.interactive
219	h.mu.Unlock()
220	iv.ClearWidget(extName, id)
221}
222
223func (h *interactiveExtHooks) ClearExtensionChrome(extName string) {
224	if h == nil {
225		return
226	}
227	h.mu.Lock()
228	if h.interactive == nil {
229		prefix := extName + "\x00"
230		for key := range h.pendingStatuses {
231			if strings.HasPrefix(key, prefix) {
232				delete(h.pendingStatuses, key)
233			}
234		}
235		for key := range h.pendingWidgets {
236			if strings.HasPrefix(key, prefix) {
237				delete(h.pendingWidgets, key)
238			}
239		}
240		h.mu.Unlock()
241		return
242	}
243	iv := h.interactive
244	h.mu.Unlock()
245	iv.ClearExtensionChrome(extName)
246}
247
248// extToolAdapter bridges *extensions.Manager to the
249// ExtensionToolSource interface declared in build.go (kept narrow to
250// avoid a build->extensions import cycle). One adapter instance per
251// run; used at every Resolve point so re-built agents pick up the
252// same set of extension tools.
253type extToolAdapter struct {
254	mgr *extensions.Manager
255}
256
257var _ ExtensionToolSource = (*extToolAdapter)(nil)
258var _ ExtensionSkillSource = (*extToolAdapter)(nil)
259
260func (a *extToolAdapter) Skills() []*skills.Skill {
261	if a == nil || a.mgr == nil {
262		return nil
263	}
264	return a.mgr.Skills()
265}
266
267func (a *extToolAdapter) Tools() []ExtensionToolInfo {
268	infos := a.mgr.Tools()
269	out := make([]ExtensionToolInfo, len(infos))
270	for i, t := range infos {
271		out[i] = ExtensionToolInfo{
272			Extension:   t.Extension,
273			Name:        t.Name,
274			Description: t.Description,
275			Schema:      t.Schema,
276			Deferred:    t.Deferred,
277			Interactive: t.Interactive,
278		}
279	}
280	return out
281}
282
283func (a *extToolAdapter) NewExtensionTool(info ExtensionToolInfo) core.Tool {
284	return extensions.NewTool(a.mgr, extensions.ToolInfo{
285		Extension:   info.Extension,
286		Name:        info.Name,
287		Description: info.Description,
288		Schema:      info.Schema,
289		Deferred:    info.Deferred,
290		Interactive: info.Interactive,
291	})
292}
293
294// liveInteractiveAgent returns the agent currently owned by the TUI. The
295// fallback is only used before the Interactive has been constructed.
296//
297// Agent rebuilds, such as cross-provider model switches, replace the TUI's
298// agent without replacing the startup pointer held by runInteractive. Session
299// operations must therefore resolve the live agent at the time of the action.
300func liveInteractiveAgent(iv *modes.Interactive, fallback *core.Agent) *core.Agent {
301	if iv != nil {
302		return iv.Agent()
303	}
304	return fallback
305}
306
307func newSessionTransition(mu *sync.RWMutex) func(func()) {
308	return func(fn func()) {
309		mu.Lock()
310		defer mu.Unlock()
311		fn()
312	}
313}
314
315// newPersistModelCallback returns the callback used by the TUI while its
316// session transition write lock is held. It must not acquire that lock again
317// because sync.RWMutex is not reentrant.
318func newPersistModelCallback(persistMu *sync.Mutex, sess **core.Session, activeProvider, activeModel *string, notify func(string)) func(string, string) {
319	return func(providerName, model string) {
320		providerName = strings.TrimSpace(providerName)
321		model = strings.TrimSpace(model)
322
323		persistMu.Lock()
324		var sessionErr error
325		if *sess != nil {
326			sessionErr = (*sess).UpdateModel(providerName, model)
327		}
328		*activeProvider = providerName
329		*activeModel = model
330		persistMu.Unlock()
331		if sessionErr != nil {
332			if notify != nil {
333				notify("model change was not persisted: " + sessionErr.Error())
334			}
335			return
336		}
337
338		cfg, err := LoadConfig()
339		if err != nil {
340			if notify != nil {
341				notify("model changed for this run, but config could not be read: " + err.Error())
342			}
343			return
344		}
345		cfg.Provider = providerName
346		cfg.Model = model
347		if err := SaveConfig(cfg); err != nil && notify != nil {
348			notify("model changed for this run, but config could not be saved: " + err.Error())
349		}
350	}
351}
352
353func closeAgentLSP(ag *core.Agent) {
354	if ag != nil {
355		_ = tools.CloseLSPManagers(ag.ToolsSnapshot())
356	}
357}
358
359func joinSessionCloseError(err error, sess *core.Session) error {
360	if sess == nil {
361		return err
362	}
363	if closeErr := sess.Close(); closeErr != nil {
364		return errors.Join(err, fmt.Errorf("close session: %w", closeErr))
365	}
366	return err
367}
368
369func closeResolvedLSP(r Resolved) {
370	_ = tools.CloseLSPManagers(r.ToolRegistry)
371}
372
373func trimMessagesForResume(msgs []provider.Message, keepTail int) []provider.Message {
374	if keepTail <= 0 || len(msgs) <= keepTail {
375		return provider.RepairOrphanedToolResults(msgs)
376	}
377	var out []provider.Message
378	start := len(msgs) - keepTail
379	var carriedToolNames []string
380	addToolNames := func(names []string) {
381		for _, name := range names {
382			if name == "" || slices.Contains(carriedToolNames, name) {
383				continue
384			}
385			carriedToolNames = append(carriedToolNames, name)
386		}
387	}
388	// Activation markers in the discarded prefix still affect the retained
389	// provider request. Carry them onto its first message rather than dropping
390	// deferred-tool availability when the resume tail is shortened.
391	for _, msg := range msgs[:start] {
392		addToolNames(msg.AddedToolNames)
393	}
394	// Preserve the synthetic compaction summary when present so an
395	// already-compacted session stays compacted after resume. Count it
396	// within the tail budget instead of dropping it at the exact boundary.
397	if len(msgs) > 0 && msgs[0].Meta["compaction"] == "true" && start > 0 {
398		out = append(out, msgs[0])
399		start++
400	}
401	// Avoid hydrating a tail that starts with orphan tool_result rows;
402	// provider APIs require those to be paired with an earlier tool_use.
403	for start < len(msgs) && msgs[start].Role == provider.RoleTool {
404		start++
405	}
406	out = append(out, msgs[start:]...)
407	if len(carriedToolNames) > 0 && len(out) > 0 {
408		first := out[0]
409		for _, name := range first.AddedToolNames {
410			addToolNames([]string{name})
411		}
412		first.AddedToolNames = append([]string(nil), carriedToolNames...)
413		out[0] = first
414	}
415	return provider.RepairOrphanedToolResults(out)
416}
417
418// sessionResumeCandidate contains all state needed to resume a session.
419// It is deliberately assembled before the caller changes any live state:
420// opening the transcript, reading usage, and rebuilding a provider/model
421// must all succeed before the candidate can replace the current session.
422type sessionResumeCandidate struct {
423	session          *core.Session
424	agent            *core.Agent
425	messages         []provider.Message
426	fullMessageCount int
427	cumulative       provider.Usage
428	lastTurn         provider.Usage
429	provider         string
430	model            string
431	rebuilt          bool
432	// planSnapshots is the per-call checklist reconstruction over the full
433	// on-disk transcript. The resumed agent only keeps the trimmed tail, so
434	// the interactive must seed from this rather than replay its messages.
435	planSnapshots map[string]core.PlanUpdate
436}
437
438// prepareSessionResume opens and validates a selected session without
439// touching the current agent. Missing provider/model metadata is treated as
440// legacy metadata: the current agent is reused, preserving compatibility
441// with sessions written before those fields were persisted.
442//
443// The returned candidate owns session until its caller commits it. On any
444// error this function closes the candidate session and leaves the current
445// agent untouched.
446func prepareSessionResume(path string, current *core.Agent, currentProvider, currentModel string, buildAgentFor func(string, string) (*core.Agent, string, string, error)) (sessionResumeCandidate, error) {
447	return prepareSessionResumeWithOptions(path, current, currentProvider, currentModel, false, false, false, buildAgentFor)
448}
449
450// prepareSessionResumeForWorkspace always rebuilds the agent. A selected
451// transcript from another cwd must not retain tools, instructions, or sandbox
452// state assembled for the previous workspace even when provider/model match.
453func prepareSessionResumeForWorkspace(path string, current *core.Agent, currentProvider, currentModel string, buildAgentFor func(string, string) (*core.Agent, string, string, error)) (sessionResumeCandidate, error) {
454	return prepareSessionResumeWithOptions(path, current, currentProvider, currentModel, false, false, true, buildAgentFor)
455}
456
457// prepareSessionResumeWithOptions is the provider/model-aware implementation.
458// An explicit CLI provider or model keeps that field authoritative; an empty
459// field falls back to the selected session's metadata when it is complete.
460func prepareSessionResumeWithOptions(path string, current *core.Agent, currentProvider, currentModel string, explicitProvider, explicitModel, forceRebuild bool, buildAgentFor func(string, string) (*core.Agent, string, string, error)) (candidate sessionResumeCandidate, err error) {
461	if current == nil {
462		return candidate, fmt.Errorf("no agent running; log in first")
463	}
464
465	sess, msgs, err := core.OpenSession(path)
466	if err != nil {
467		return candidate, err
468	}
469	keepSession := false
470	defer func() {
471		if !keepSession {
472			err = joinSessionCloseError(err, sess)
473		}
474	}()
475
476	cumulative, lastTurn, err := core.SessionUsageDetail(path)
477	if err != nil {
478		return candidate, fmt.Errorf("read session usage: %w", err)
479	}
480
481	fullMessageCount := len(msgs)
482	resumedMessages := trimMessagesForResume(msgs, 100)
483	// Reconstruct the historical checklists from the untrimmed messages: a
484	// plan delta inside the trim window needs the pre-window base to rebuild
485	// the right list, and the view must keep using the trimmed transcript.
486	planSnapshots := core.PlanUpdateSnapshots(msgs)
487	storedProvider := strings.TrimSpace(sess.Meta.Provider)
488	storedModel := strings.TrimSpace(sess.Meta.Model)
489	currentProvider = strings.TrimSpace(currentProvider)
490	currentModel = strings.TrimSpace(currentModel)
491
492	candidate = sessionResumeCandidate{
493		session:          sess,
494		agent:            current,
495		messages:         resumedMessages,
496		fullMessageCount: fullMessageCount,
497		cumulative:       cumulative,
498		lastTurn:         lastTurn,
499		provider:         currentProvider,
500		model:            currentModel,
501		planSnapshots:    planSnapshots,
502	}
503
504	// Old sessions may have either field absent. Only a complete stored
505	// selection is actionable; otherwise resume exactly as older zut did.
506	if storedProvider == "" || storedModel == "" {
507		keepSession = true
508		return candidate, nil
509	}
510
511	resumeProvider := storedProvider
512	if explicitProvider {
513		resumeProvider = currentProvider
514	}
515	resumeModel := storedModel
516	if explicitModel {
517		resumeModel = currentModel
518	}
519	if !forceRebuild && resumeProvider == currentProvider && resumeModel == currentModel {
520		keepSession = true
521		return candidate, nil
522	}
523	if buildAgentFor == nil {
524		return sessionResumeCandidate{}, fmt.Errorf("cannot resume session with provider %q and model %q: no agent builder configured", resumeProvider, resumeModel)
525	}
526
527	rebuilt, resolvedProvider, resolvedModel, err := buildAgentFor(resumeProvider, resumeModel)
528	if err != nil {
529		return sessionResumeCandidate{}, fmt.Errorf("rebuild agent for session provider %q/model %q: %w", resumeProvider, resumeModel, err)
530	}
531	if rebuilt == nil {
532		return sessionResumeCandidate{}, fmt.Errorf("rebuild agent for session provider %q/model %q returned no agent", resumeProvider, resumeModel)
533	}
534	resolvedProvider = strings.TrimSpace(resolvedProvider)
535	resolvedModel = strings.TrimSpace(resolvedModel)
536	if resolvedProvider == "" {
537		resolvedProvider = storedProvider
538	}
539	if resolvedModel == "" {
540		resolvedModel = storedModel
541	}
542
543	// Hydrate and seed the replacement while it is still private. The live
544	// agent is not modified until the caller commits the complete candidate.
545	rebuilt.SetMessages(resumedMessages)
546	rebuilt.SeedCost(cumulative)
547	rebuilt.SeedLastTurnUsage(lastTurn)
548	candidate.agent = rebuilt
549	candidate.provider = resolvedProvider
550	candidate.model = resolvedModel
551	candidate.rebuilt = true
552	keepSession = true
553	return candidate, nil
554}
555
556func buildNonInteractiveSessionAgentWithRegistry(ctx context.Context, args Args, base Resolved, extMgr *extensions.Manager, providerName, model string, prepareRegistry func(core.Registry) core.Registry) (*core.Agent, string, string, error) {
557	next := args
558	next.Provider = providerName
559	next.Model = model
560	resolved, err := Resolve(next, true)
561	if err != nil {
562		return nil, "", "", err
563	}
564	resolved.UseSandbox(base.Sandbox)
565	if extMgr != nil {
566		resolved.MergeExtensionTools(&extToolAdapter{mgr: extMgr})
567	}
568	if prepareRegistry != nil {
569		prepareRegistry(resolved.ToolRegistry)
570	}
571	ag := resolved.NewAgent()
572	wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
573	return ag, resolved.Provider, resolved.Model, nil
574}
575
576// applySessionResume shares the startup gate, candidate hydration, and
577// commit order used by interactive and non-interactive session opening. The
578// old append handle is closed only after the candidate has been prepared and
579// seeded successfully.
580func applySessionResume(sess *core.Session, ag *core.Agent, currentProvider, currentModel string, explicitProvider, explicitModel bool, buildAgentFor func(string, string) (*core.Agent, string, string, error)) (sessionResumeCandidate, error) {
581	candidate := sessionResumeCandidate{
582		session:          sess,
583		agent:            ag,
584		fullMessageCount: 0,
585		provider:         currentProvider,
586		model:            currentModel,
587	}
588	if ag != nil {
589		candidate.fullMessageCount = len(ag.Messages())
590	}
591	if sess == nil || ag == nil {
592		return candidate, nil
593	}
594	ag.SetSessionTimeContext(sess.Meta.Started, sess.Meta.Timezone, sess.Meta.TimezoneOffset)
595	// A newly created meta-only session is already on the active pair. Avoid
596	// reopening and closing it through the resume path: Close intentionally
597	// removes fresh empty sessions, and a second append handle would otherwise
598	// keep an unlinked file alive.
599	if len(ag.Messages()) == 0 &&
600		strings.TrimSpace(sess.Meta.Provider) == strings.TrimSpace(currentProvider) &&
601		strings.TrimSpace(sess.Meta.Model) == strings.TrimSpace(currentModel) {
602		ag.SetPlan(sess.Meta.PlanSteps())
603		return candidate, nil
604	}
605	candidate, err := prepareSessionResumeWithOptions(sess.Path, ag, currentProvider, currentModel, explicitProvider, explicitModel, false, buildAgentFor)
606	if err != nil {
607		return sessionResumeCandidate{}, joinSessionCloseError(err, sess)
608	}
609	// A replacement agent is still private at this point. Bind the durable
610	// identity before it can be published or run any startup work.
611	if err := candidate.agent.BindSessionID(candidate.session.Meta.ID); err != nil {
612		return sessionResumeCandidate{}, joinSessionCloseError(err, candidate.session)
613	}
614	if !candidate.rebuilt {
615		candidate.agent.SetMessages(candidate.messages)
616		candidate.agent.SeedCost(candidate.cumulative)
617		candidate.agent.SeedLastTurnUsage(candidate.lastTurn)
618	}
619	if closeErr := sess.Close(); closeErr != nil {
620		return sessionResumeCandidate{}, errors.Join(fmt.Errorf("close session: %w", closeErr), joinSessionCloseError(nil, candidate.session))
621	}
622	candidate.agent.SetSessionTimeContext(candidate.session.Meta.Started, candidate.session.Meta.Timezone, candidate.session.Meta.TimezoneOffset)
623	// Seed the agent-owned plan from the loaded session so a resumed session's
624	// core state matches its file: the transcript holds steps while core would
625	// otherwise hold none, which makes show answer "Plan is empty" and indexed
626	// update/remove fail. Done after the candidate is fully hydrated but before
627	// the caller commits it.
628	candidate.agent.SetPlan(candidate.session.Meta.PlanSteps())
629	return candidate, nil
630}
631
632// applyInitialSessionResume applies the same provider/model-aware candidate
633// contract used by interactive session switching to non-interactive startup
634// modes. The returned session owns the replacement append handle.
635func applyInitialSessionResume(ctx context.Context, args Args, base Resolved, extMgr *extensions.Manager, sess *core.Session, ag *core.Agent) (*core.Session, *core.Agent, string, string, error) {
636	return applyInitialSessionResumeWithRegistry(ctx, args, base, extMgr, sess, ag, nil)
637}
638
639func applyInitialSessionResumeWithRegistry(ctx context.Context, args Args, base Resolved, extMgr *extensions.Manager, sess *core.Session, ag *core.Agent, prepareRegistry func(core.Registry) core.Registry) (*core.Session, *core.Agent, string, string, error) {
640	candidate, err := applySessionResume(sess, ag, base.Provider, base.Model,
641		strings.TrimSpace(args.Provider) != "", strings.TrimSpace(args.Model) != "", func(providerName, model string) (*core.Agent, string, string, error) {
642			return buildNonInteractiveSessionAgentWithRegistry(ctx, args, base, extMgr, providerName, model, prepareRegistry)
643		})
644	if err != nil {
645		return nil, ag, "", "", err
646	}
647	return candidate.session, candidate.agent, candidate.provider, candidate.model, nil
648}
649
650func applyInitialSessionResumeWithRuntime(ctx context.Context, args Args, base Resolved, extMgr *extensions.Manager, sess *core.Session, ag *core.Agent, runtime *subagentRuntime) (*core.Session, *core.Agent, string, string, error) {
651	candidate, err := applySessionResume(sess, ag, base.Provider, base.Model,
652		strings.TrimSpace(args.Provider) != "", strings.TrimSpace(args.Model) != "", func(providerName, model string) (*core.Agent, string, string, error) {
653			next := args
654			next.Provider = providerName
655			next.Model = model
656			resolved, resolveErr := Resolve(next, true)
657			if resolveErr != nil {
658				return nil, "", "", resolveErr
659			}
660			resolved.UseSandbox(base.Sandbox)
661			if extMgr != nil {
662				resolved.MergeExtensionTools(&extToolAdapter{mgr: extMgr})
663			}
664			if runtime != nil {
665				runtime.SetProvider(resolved.Provider)
666				runtime.SetModel(resolved.Model)
667				runtime.SetContextWindow(resolved.ContextWindow)
668				runtime.SetProviderSettings(resolved.BaseURL, resolved.InsecureTLS)
669				runtime.SetFastMode(resolved.FastMode)
670				runtime.PrepareResolvedRegistry(resolved.ToolRegistry, resolved.WebSearchPolicy)
671			}
672			rebuilt := resolved.NewAgent()
673			wireNonInteractiveAgentExtHooks(ctx, rebuilt, extMgr)
674			return rebuilt, resolved.Provider, resolved.Model, nil
675		})
676	if err != nil {
677		return nil, ag, "", "", err
678	}
679	return candidate.session, candidate.agent, candidate.provider, candidate.model, nil
680}
681
682// fanoutAgentEvent translates a core.AgentEvent into the wire-format
683// EventFromHost and pushes it through the extension manager. Only
684// the events that have a clear extension-facing meaning are
685// forwarded; internal-only ones (text_delta, tool_progress) are
686// dropped to keep the per-extension stream sane.
687func sessionContext(sess *core.Session) *extproto.SessionContext {
688	if sess == nil {
689		return nil
690	}
691	return &extproto.SessionContext{
692		ID:        sess.Meta.ID,
693		ParentID:  sess.Meta.Parent,
694		Path:      sess.Path,
695		CWD:       sess.Meta.CWD,
696		ForkPoint: sess.Meta.ForkPoint,
697	}
698}
699
700func announceSession(mgr *extensions.Manager, sess *core.Session) {
701	if mgr == nil {
702		return
703	}
704	var states map[string]json.RawMessage
705	if sess != nil {
706		states = copyExtensionStates(sess.ExtensionState)
707	}
708	mgr.EmitSessionEvent("session_start", sessionContext(sess), states)
709	if sess != nil {
710		mgr.EmitSessionEvent("session_opened", sessionContext(sess), states)
711	}
712}
713
714func copyExtensionStates(states map[string]json.RawMessage) map[string]json.RawMessage {
715	if len(states) == 0 {
716		return nil
717	}
718	out := make(map[string]json.RawMessage, len(states))
719	for name, state := range states {
720		out[name] = append(json.RawMessage(nil), state...)
721	}
722	return out
723}
724
725var (
726	errGoalActiveReplacement  = errors.New("active goal replacement")
727	errGoalMissionMismatch    = errors.New("goal mission mismatch")
728	errGoalStaleUpdate        = errors.New("stale goal update")
729	errGoalStorageUnavailable = errors.New("goal storage unavailable")
730	errGoalStateWrite         = errors.New("goal state write")
731
732	errPlanStorageUnavailable = errors.New("plan storage unavailable")
733	errPlanStateWrite         = errors.New("plan state write")
734)
735
736func persistToolResultState(mgr *extensions.Manager, sess *core.Session, result core.ToolResult, goalMaxTokenBudget ...*uint64) error {
737	if sess == nil {
738		if _, ok := tools.GoalUpdateFromResult(result); ok {
739			return goalCommitError(errGoalStorageUnavailable)
740		}
741		if result.PlanOperation != nil && !result.IsError {
742			return planCommitError(errPlanStorageUnavailable)
743		}
744		return nil
745	}
746	if details, ok := result.Details.(extensions.ToolResultDetails); ok && details.Extension != "" && len(details.State) > 0 {
747		if err := sess.AppendExtensionState(details.Extension, details.State); err != nil {
748			return fmt.Errorf("extension state: %w", err)
749		}
750		if mgr != nil {
751			mgr.UpdateSessionState(details.Extension, details.State)
752		}
753	}
754	if err := persistGoalToolResult(sess, result, goalMaxTokenBudget...); err != nil {
755		return goalCommitError(err)
756	}
757	if err := persistPlanToolResult(sess, result); err != nil {
758		return planCommitError(err)
759	}
760	return nil
761}
762
763func goalCommitError(err error) error {
764	return &core.ToolResultCommitError{Message: safeGoalCommitMessage(err), Err: fmt.Errorf("goal state: %w", err)}
765}
766
767func safeGoalCommitMessage(err error) string {
768	switch {
769	case errors.Is(err, errGoalActiveReplacement):
770		return "goal transition rejected: the current goal is still active"
771	case errors.Is(err, errGoalMissionMismatch):
772		return "goal transition rejected: it does not belong to the active mission"
773	case errors.Is(err, errGoalStaleUpdate):
774		return "goal transition rejected: the active goal changed"
775	case errors.Is(err, errGoalStorageUnavailable):
776		return "goal state unavailable: session persistence is disabled"
777	case errors.Is(err, errGoalStateWrite):
778		return "goal state could not be saved"
779	default:
780		return "goal update could not be persisted"
781	}
782}
783
784func planCommitError(err error) error {
785	return &core.ToolResultCommitError{Message: safePlanCommitMessage(err), Err: fmt.Errorf("plan state: %w", err)}
786}
787
788func safePlanCommitMessage(err error) string {
789	switch {
790	case errors.Is(err, errPlanStorageUnavailable):
791		return "plan state unavailable: session persistence is disabled"
792	case errors.Is(err, errPlanStateWrite):
793		return "plan state could not be saved"
794	default:
795		return "plan update could not be persisted"
796	}
797}
798
799func persistGoalToolResult(sess *core.Session, result core.ToolResult, goalMaxTokenBudget ...*uint64) error {
800	if sess == nil {
801		return nil
802	}
803	update, ok := tools.GoalUpdateFromResult(result)
804	if !ok {
805		return nil
806	}
807	if update.Status == core.GoalSuperseded {
808		if sess.Meta.Goal == nil || sess.Meta.Goal.Status != core.GoalActive {
809			return nil
810		}
811		if update.GoalID != sess.Meta.Goal.ID {
812			return errGoalStaleUpdate
813		}
814		if sess.Meta.Mission != nil && update.MissionID != "" && update.MissionID != sess.Meta.Mission.ID {
815			return errGoalMissionMismatch
816		}
817		replacement := &core.SessionGoal{Objective: update.Objective, Status: core.GoalActive, Owner: core.GoalOwnerManager}
818		if len(goalMaxTokenBudget) > 0 && goalMaxTokenBudget[0] != nil && *goalMaxTokenBudget[0] > 0 {
819			budget := *goalMaxTokenBudget[0]
820			replacement.TokenBudget = &budget
821		}
822		if err := sess.SupersedeGoal(replacement); err != nil {
823			return fmt.Errorf("%w: %w", errGoalStateWrite, err)
824		}
825		return nil
826	}
827	if update.Status == core.GoalActive {
828		if strings.TrimSpace(update.Objective) == "" {
829			if sess.Meta.Goal == nil || sess.Meta.Goal.Status != core.GoalPaused {
830				return nil
831			}
832			if sess.Meta.Mission != nil && update.MissionID != "" && update.MissionID != sess.Meta.Mission.ID {
833				return errGoalMissionMismatch
834			}
835			goal := *sess.Meta.Goal
836			goal.Status = core.GoalActive
837			goal.Reason = ""
838			if err := sess.UpdateGoal(&goal); err != nil {
839				return fmt.Errorf("%w: %w", errGoalStateWrite, err)
840			}
841			return nil
842		}
843		if sess.Meta.Goal != nil && sess.Meta.Goal.Status == core.GoalActive {
844			return errGoalActiveReplacement
845		}
846		if sess.Meta.Mission != nil && update.MissionID != "" && update.MissionID != sess.Meta.Mission.ID {
847			return errGoalMissionMismatch
848		}
849		goal := &core.SessionGoal{Objective: update.Objective, Status: core.GoalActive, Owner: core.GoalOwnerManager}
850		if len(goalMaxTokenBudget) > 0 && goalMaxTokenBudget[0] != nil && *goalMaxTokenBudget[0] > 0 {
851			budget := *goalMaxTokenBudget[0]
852			goal.TokenBudget = &budget
853		}
854		if err := sess.UpdateGoal(goal); err != nil {
855			return fmt.Errorf("%w: %w", errGoalStateWrite, err)
856		}
857		return nil
858	}
859	if sess.Meta.Goal == nil || sess.Meta.Goal.Status != core.GoalActive {
860		return nil
861	}
862	if sess.Meta.Mission != nil && update.MissionID != "" && update.MissionID != sess.Meta.Mission.ID {
863		return errGoalMissionMismatch
864	}
865	goal := *sess.Meta.Goal
866	goal.Status = update.Status
867	goal.Reason = update.Reason
868	if err := sess.UpdateGoal(&goal); err != nil {
869		return fmt.Errorf("%w: %w", errGoalStateWrite, err)
870	}
871	return nil
872}
873
874// persistPlanToolResult writes the materialized checklist for a validated plan
875// mutation. show carries neither Details nor PlanOperation, and error results
876// were already rejected, so both are skipped here.
877func persistPlanToolResult(sess *core.Session, result core.ToolResult) error {
878	if sess == nil || result.IsError || result.PlanOperation == nil {
879		return nil
880	}
881	update, ok := result.Details.(core.PlanUpdate)
882	if !ok {
883		return nil
884	}
885	if err := sess.UpdatePlan(&core.SessionPlan{Steps: update.Plan}); err != nil {
886		return fmt.Errorf("%w: %w", errPlanStateWrite, err)
887	}
888	return nil
889}
890
891func fanoutAgentEvent(mgr *extensions.Manager, ev core.AgentEvent) {
892	if mgr == nil {
893		return
894	}
895	switch e := ev.(type) {
896	case core.EvTurnStart:
897		mgr.EmitEvent(extproto.EventFromHost{Event: "turn_start", Step: e.Step})
898	case core.EvToolCall:
899		mgr.EmitEvent(extproto.EventFromHost{
900			Event: "tool_call", ToolID: e.ID, ToolName: e.Name, ToolArgs: e.Args,
901		})
902	case core.EvAssistantMessage:
903		// Concat the visible text portions of the message; binary
904		// blocks (tool_use, etc.) are skipped because subscribers
905		// usually want a string they can grep / display.
906		var text string
907		for _, c := range e.Message.Content {
908			if tb, ok := c.(provider.TextBlock); ok {
909				text += tb.Text
910			}
911		}
912		mgr.EmitEvent(extproto.EventFromHost{Event: "assistant_message", Text: text})
913	case core.EvTurnEnd:
914		ev := extproto.EventFromHost{Event: "turn_end", Stop: string(e.Stop)}
915		if e.Err != nil {
916			ev.Error = e.Err.Error()
917		}
918		mgr.EmitEvent(ev)
919	}
920}
921
922// Run is the top-level entrypoint for the zut binary.
923func Run(rawArgs []string, version string) error {
924	// Apply network configuration before any subcommand can make an HTTP
925	// request. Standard proxy environment variables retain precedence.
926	applyConfiguredHTTPProxy()
927
928	// Extension installs can invoke external build and clone processes.
929	// Give those processes a cancellation context without changing signal
930	// handling for the interactive modes below.
931	extCtx := context.Background()
932	stopExtContext := func() {}
933	if len(rawArgs) > 0 && (rawArgs[0] == "ext" || rawArgs[0] == "mcp") {
934		extCtx, stopExtContext = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
935	}
936	defer stopExtContext()
937
938	// Subcommand router: `zut bot ...` is handled separately so the
939	// generic flag parser doesn't reject "bot" as a positional arg.
940	if handled, err := runBotCommand(rawArgs, version); handled {
941		return err
942	}
943	if handled, err := runExtCommand(extCtx, rawArgs, version); handled {
944		return err
945	}
946	if handled, err := runMCPCommand(extCtx, rawArgs, version, os.Stdout); handled {
947		return err
948	}
949	if handled, err := runUpdateCommand(rawArgs, version); handled {
950		return err
951	}
952	if handled, err := runSessionsCommand(rawArgs); handled {
953		return err
954	}
955	if handled, err := runZutfileCommand(rawArgs, version); handled {
956		return err
957	}
958	return runWithArgsRaw(rawArgs, version)
959}
960
961func runWithArgsRaw(rawArgs []string, version string) error {
962	// `zut rpc` is shorthand for `zut --rpc` so third-party apps can
963	// spawn the binary with a clean argv. Strip the leading 'rpc'
964	// token and let the rest flow through the normal arg parser.
965	if len(rawArgs) > 0 && rawArgs[0] == "rpc" {
966		rawArgs = append([]string{"--rpc"}, rawArgs[1:]...)
967	}
968
969	if fi, err := os.Stdin.Stat(); err == nil {
970		rawArgs = argsForStdin(rawArgs, fi.Mode())
971	}
972	args, err := ParseArgs(rawArgs)
973	if err != nil {
974		PrintHelp(version)
975		return err
976	}
977	if args.Help {
978		PrintHelp(version)
979		return nil
980	}
981	if args.Version {
982		fmt.Println("zut", version)
983		return nil
984	}
985	// Reject invalid orchestration combinations before catalog repair or model
986	// refresh can mutate local state or launch background work.
987	if err := validateOrchestrationArgs(args); err != nil {
988		return err
989	}
990	// Model catalog: load any cached discovery data before we inspect
991	// the model list (list-models, print/json, interactive).
992	userModels := LoadUserModels()
993	PrepareRuntimeCatalog(context.Background(), args.ListModels, args.Provider, args.APIKey, args.BaseURL, userModels, args.Model)
994
995	if args.ListModels {
996		printModels()
997		return nil
998	}
999
1000	return runWithArgs(args, version)
1001}
1002
1003func runWithArgs(args Args, version string) error {
1004	if err := validateOrchestrationArgs(args); err != nil {
1005		return err
1006	}
1007	ctx := context.Background()
1008	// MCP servers start once per process. Every later Resolve, including
1009	// resident subagents, shares these connections through args.
1010	stopMCP, err := startMCP(ctx, &args, version)
1011	if err != nil {
1012		return err
1013	}
1014	defer stopMCP()
1015	if args.mcpManager != nil && args.Mode != ModeInteractive && args.Mode != "" && !args.Orchestrate {
1016		// Plain headless modes terminate on signals. Stop MCP servers first so
1017		// their process groups and HTTP sessions do not outlive zut. The TUI
1018		// and --orchestrate own their signal handling and cancel gracefully.
1019		defer exitAfterMCPCleanupOnSignal(args.mcpManager)()
1020	}
1021	switch args.Mode {
1022	case ModePrint:
1023		return runPrintMode(ctx, args, version)
1024	case ModeStream:
1025		return runStreamMode(ctx, args, version)
1026	case ModeJSON:
1027		return runJSONMode(ctx, args, version)
1028	case ModeRPC:
1029		return runRPCMode(ctx, args, version)
1030	default:
1031		return runInteractive(ctx, args, version)
1032	}
1033}
1034
1035// ---- print / json modes: require credentials, run single-shot ----
1036
1037// nonInteractiveExtHooks is the HostHooks impl used by print / json
1038// modes. They have no TUI, so notify / display go to stderr and
1039// submit / insert are no-ops (the extension can't steer a
1040// single-shot run once it's in flight anyway).
1041type nonInteractiveExtHooks struct{}
1042
1043func (nonInteractiveExtHooks) Notify(ext, level, message string) {
1044	fmt.Fprintf(os.Stderr, "[%s] %s: %s\n", ext, level, message)
1045}
1046func (nonInteractiveExtHooks) Alert(string, extproto.AlertRequest)                  {}
1047func (nonInteractiveExtHooks) Submit(string)                                        {}
1048func (nonInteractiveExtHooks) SubmitSlash(string)                                   {}
1049func (nonInteractiveExtHooks) Insert(string)                                        {}
1050func (nonInteractiveExtHooks) Display(string, string)                               {}
1051func (nonInteractiveExtHooks) ClearNotes(string)                                    {}
1052func (nonInteractiveExtHooks) OpenPanel(string, extproto.PanelSpec)                 {}
1053func (nonInteractiveExtHooks) UpdatePanel(string, string, string, []string, string) {}
1054func (nonInteractiveExtHooks) ClosePanel(string, string)                            {}
1055func (nonInteractiveExtHooks) SetStatus(string, string, string, string)             {}
1056func (nonInteractiveExtHooks) SetWidget(string, string, string, string, []string)   {}
1057func (nonInteractiveExtHooks) ClearWidget(string, string)                           {}
1058
1059// setupNonInteractiveExtensions loads --ext paths and (unless
1060// --no-ext) runs discovery. Returns the manager so the caller can
1061// wire tools into the resolved registry, and a cleanup closure to
1062// defer. Mirrors the interactive-mode setup minus the TUI hooks.
1063func setupNonInteractiveExtensions(ctx context.Context, args Args, r *Resolved, version string) (*extensions.Manager, func()) {
1064	extMgr := extensions.New(ZutHome(), r.CWD, version, r.Provider, r.Model, nonInteractiveExtHooks{})
1065	for _, e := range extMgr.LoadExplicit(ctx, args.Exts) {
1066		fmt.Fprintln(os.Stderr, "extension load:", e)
1067	}
1068	if !args.NoExt {
1069		for _, e := range extMgr.Discover(ctx) {
1070			fmt.Fprintln(os.Stderr, "extension load:", e)
1071		}
1072	}
1073	extMgr.WaitForReady(3 * time.Second)
1074	r.MergeExtensionTools(&extToolAdapter{mgr: extMgr})
1075	return extMgr, func() { extMgr.Stop(2 * time.Second) }
1076}
1077
1078// wireNonInteractiveAgentExtHooks installs the same BeforeToolExecute
1079// / BeforeTurn / BeforeAssistantMessage / OnEvent hooks the
1080// interactive path wires up, so extensions get their normal
1081// event-intercept surface in print / json / rpc flows too.
1082func wireNonInteractiveAgentExtHooks(ctx context.Context, ag *core.Agent, extMgr *extensions.Manager) {
1083	if ag == nil || extMgr == nil {
1084		return
1085	}
1086	ag.BeforeToolExecute = func(call provider.ToolCallBlock) (bool, string, json.RawMessage) {
1087		res := extMgr.InterceptToolCall(ctx, call.ID, call.Name, call.Arguments)
1088		if res.Block {
1089			return false, res.Reason, nil
1090		}
1091		return true, "", res.ModifiedArgs
1092	}
1093	ag.BeforeTurnContext = func(turnCtx context.Context, step int) (bool, string, string) {
1094		res := extMgr.InterceptTurnStart(turnCtx, step)
1095		return !res.Block, res.Reason, res.Context
1096	}
1097	ag.BeforeAssistantMessage = func(text string) (bool, string, string) {
1098		res := extMgr.InterceptAssistantMessage(ctx, text)
1099		if res.Block {
1100			return false, res.Reason, ""
1101		}
1102		return true, "", res.ReplaceText
1103	}
1104	ag.OnEvent = func(ev core.AgentEvent) { fanoutAgentEvent(extMgr, ev) }
1105}
1106
1107type printStats struct {
1108	Provider              string `json:"provider"`
1109	Model                 string `json:"model"`
1110	PromptTokens          int    `json:"prompt_tokens"`
1111	ReasoningTokens       *int   `json:"reasoning_tokens"`
1112	GeneratedOutputTokens int    `json:"generated_output_tokens"`
1113	ElapsedMS             int64  `json:"elapsed_ms"`
1114}
1115
1116func writePrintStats(path, providerID, model string, usage provider.Usage, elapsed time.Duration) error {
1117	var reasoning *int
1118	generated := usage.OutputTokens
1119	if usage.ReasoningTokensKnown {
1120		count := usage.ReasoningTokens
1121		reasoning = &count
1122		generated -= count
1123		if generated < 0 {
1124			generated = 0
1125		}
1126	}
1127	stats := printStats{
1128		Provider:              providerID,
1129		Model:                 model,
1130		PromptTokens:          usage.InputTokens + usage.CacheReadTokens + usage.CacheWriteTokens,
1131		ReasoningTokens:       reasoning,
1132		GeneratedOutputTokens: generated,
1133		ElapsedMS:             elapsed.Milliseconds(),
1134	}
1135	data, err := json.MarshalIndent(stats, "", "  ")
1136	if err != nil {
1137		return fmt.Errorf("encode stats: %w", err)
1138	}
1139	data = append(data, '\n')
1140	if err := os.WriteFile(path, data, 0o644); err != nil {
1141		return fmt.Errorf("write stats %q: %w", path, err)
1142	}
1143	return nil
1144}
1145
1146func runPrintMode(ctx context.Context, args Args, version string) (runErr error) {
1147	if args.Orchestrate {
1148		return runOrchestratedPrintMode(ctx, args, version)
1149	}
1150	if args.NoYolo {
1151		fmt.Fprintln(os.Stderr, "warning: --no-yolo has no effect in print mode (no interactive prompt available); tools will run without confirmation")
1152	}
1153	r, err := Resolve(args, true)
1154	if err != nil {
1155		return err
1156	}
1157	extMgr, stopExt := setupNonInteractiveExtensions(ctx, args, &r, version)
1158	defer stopExt()
1159
1160	ag := r.NewAgent()
1161	initialAg := ag
1162	defer func() {
1163		closeAgentLSP(ag)
1164		if ag != initialAg {
1165			closeAgentLSP(initialAg)
1166		}
1167	}()
1168	wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
1169	sess, freshSession, err := openOrCreateSessionState(ctx, args, r, ag, version)
1170	if err != nil {
1171		return err
1172	}
1173	if sess != nil {
1174		var providerName, model string
1175		sess, ag, providerName, model, err = applyInitialSessionResume(ctx, args, r, extMgr, sess, ag)
1176		if err != nil {
1177			return err
1178		}
1179		r.Provider, r.Model = providerName, model
1180		ag.CommitToolResult = func(_ string, result core.ToolResult) error { return persistToolResultState(extMgr, sess, result) }
1181		defer func() { runErr = joinSessionCloseError(runErr, sess) }()
1182	}
1183	announceSession(extMgr, sess)
1184	wireRetryLifecyclePersistence(ag, sess)
1185	if sess != nil {
1186		defer func() { runErr = errors.Join(runErr, sess.FlushRetryLifecycle(ag.LastTurnUsage(), ag.Cost())) }()
1187	}
1188
1189	piped, _ := readAllStdin()
1190	prompt := combinePromptInput(piped, args.Prompt)
1191	if prompt == "" {
1192		return fmt.Errorf("print mode requires a prompt (arg or stdin)")
1193	}
1194
1195	start := len(ag.Messages())
1196	if err := runZutfileStartupPre(ctx, args.StartupPre, r.CWD, r.Sandbox, ag, nil, os.Stderr); err != nil {
1197		return err
1198	}
1199	if err := reloadResourcesAfterStartupPre(ctx, args, extMgr, r.Sandbox, ag); err != nil {
1200		return err
1201	}
1202	var persistCompaction func([]provider.Message) error
1203	if sess != nil {
1204		persistCompaction = sess.AppendCompaction
1205	}
1206	started := time.Now()
1207	prompt = preloadSkillPins(args, freshSession, prompt, extMgr.Skills(), os.Stderr)
1208	usage, recovery, err := modes.RunPrintWithContextRecovery(ctx, ag, prompt, nil, os.Stdout, persistCompaction)
1209	elapsed := time.Since(started)
1210	transcriptStart := start
1211	if recovery.Compacted {
1212		transcriptStart = recovery.OutputStart
1213	}
1214	if persistErr := WriteNewTranscript(ag, sess, transcriptStart); persistErr != nil {
1215		if err != nil {
1216			return errors.Join(err, persistErr)
1217		}
1218		return persistErr
1219	}
1220	if err != nil {
1221		return err
1222	}
1223	if args.StatsPath != "" {
1224		return writePrintStats(args.StatsPath, r.Provider, r.Model, usage, elapsed)
1225	}
1226	return nil
1227}
1228
1229func runStreamMode(ctx context.Context, args Args, version string) (runErr error) {
1230	if args.Orchestrate {
1231		return runOrchestratedStreamMode(ctx, args, version)
1232	}
1233	if args.NoYolo {
1234		fmt.Fprintln(os.Stderr, "warning: --no-yolo has no effect in stream mode (no interactive prompt available); tools will run without confirmation")
1235	}
1236	r, err := Resolve(args, true)
1237	if err != nil {
1238		return err
1239	}
1240	extMgr, stopExt := setupNonInteractiveExtensions(ctx, args, &r, version)
1241	defer stopExt()
1242
1243	ag := r.NewAgent()
1244	initialAg := ag
1245	defer func() {
1246		closeAgentLSP(ag)
1247		if ag != initialAg {
1248			closeAgentLSP(initialAg)
1249		}
1250	}()
1251	wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
1252	sess, freshSession, err := openOrCreateSessionState(ctx, args, r, ag, version)
1253	if err != nil {
1254		return err
1255	}
1256	if sess != nil {
1257		var providerName, model string
1258		sess, ag, providerName, model, err = applyInitialSessionResume(ctx, args, r, extMgr, sess, ag)
1259		if err != nil {
1260			return err
1261		}
1262		r.Provider, r.Model = providerName, model
1263		ag.CommitToolResult = func(_ string, result core.ToolResult) error { return persistToolResultState(extMgr, sess, result) }
1264		defer func() { runErr = joinSessionCloseError(runErr, sess) }()
1265	}
1266	announceSession(extMgr, sess)
1267	wireRetryLifecyclePersistence(ag, sess)
1268	if sess != nil {
1269		defer func() { runErr = errors.Join(runErr, sess.FlushRetryLifecycle(ag.LastTurnUsage(), ag.Cost())) }()
1270	}
1271
1272	piped, _ := readAllStdin()
1273	prompt := combinePromptInput(piped, args.Prompt)
1274	if prompt == "" {
1275		return fmt.Errorf("stream mode requires a prompt (arg or stdin)")
1276	}
1277
1278	start := len(ag.Messages())
1279	preSink, finishPre := newStreamTextSink(os.Stdout)
1280	if err := runZutfileStartupPre(ctx, args.StartupPre, r.CWD, r.Sandbox, ag, preSink, os.Stderr); err != nil {
1281		finishPre()
1282		return err
1283	}
1284	finishPre()
1285	if err := reloadResourcesAfterStartupPre(ctx, args, extMgr, r.Sandbox, ag); err != nil {
1286		return err
1287	}
1288	var persistCompaction func([]provider.Message) error
1289	if sess != nil {
1290		persistCompaction = sess.AppendCompaction
1291	}
1292	prompt = preloadSkillPins(args, freshSession, prompt, extMgr.Skills(), os.Stderr)
1293	recovery, err := modes.RunStreamWithContextRecovery(ctx, ag, prompt, nil, os.Stdout, os.Stderr, persistCompaction)
1294	transcriptStart := start
1295	if recovery.Compacted {
1296		transcriptStart = recovery.OutputStart
1297	}
1298	if persistErr := WriteNewTranscript(ag, sess, transcriptStart); persistErr != nil {
1299		if err != nil {
1300			return errors.Join(err, persistErr)
1301		}
1302		return persistErr
1303	}
1304	return err
1305}
1306
1307func newStreamTextSink(out io.Writer) (func(core.AgentEvent), func()) {
1308	var streamed, wroteText, lastWasNL bool
1309	writeText := func(text string) {
1310		if text == "" {
1311			return
1312		}
1313		_, _ = fmt.Fprint(out, text)
1314		if syncer, ok := out.(interface{ Sync() error }); ok {
1315			_ = syncer.Sync()
1316		}
1317		wroteText = true
1318		lastWasNL = strings.HasSuffix(text, "\n")
1319	}
1320	sink := func(ev core.AgentEvent) {
1321		switch e := ev.(type) {
1322		case core.EvAssistantStart:
1323			streamed = false
1324		case core.EvTextDelta:
1325			streamed = true
1326			writeText(e.Delta)
1327		case core.EvAssistantMessage:
1328			if streamed {
1329				return
1330			}
1331			var text strings.Builder
1332			for _, content := range e.Message.Content {
1333				if block, ok := content.(provider.TextBlock); ok {
1334					if text.Len() > 0 {
1335						text.WriteString("\n")
1336					}
1337					text.WriteString(block.Text)
1338				}
1339			}
1340			writeText(text.String())
1341		}
1342	}
1343	finish := func() {
1344		if wroteText && !lastWasNL {
1345			writeText("\n")
1346		}
1347	}
1348	return sink, finish
1349}
1350
1351func runJSONMode(ctx context.Context, args Args, version string) (runErr error) {
1352	if args.Orchestrate {
1353		return runOrchestratedJSONMode(ctx, args, version)
1354	}
1355	if args.NoYolo {
1356		fmt.Fprintln(os.Stderr, "warning: --no-yolo has no effect in json mode (no interactive prompt available); tools will run without confirmation")
1357	}
1358	r, err := Resolve(args, true)
1359	if err != nil {
1360		return err
1361	}
1362	extMgr, stopExt := setupNonInteractiveExtensions(ctx, args, &r, version)
1363	defer stopExt()
1364
1365	ag := r.NewAgent()
1366	initialAg := ag
1367	defer func() {
1368		closeAgentLSP(ag)
1369		if ag != initialAg {
1370			closeAgentLSP(initialAg)
1371		}
1372	}()
1373	wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
1374	sess, freshSession, err := openOrCreateSessionState(ctx, args, r, ag, version)
1375	if err != nil {
1376		return err
1377	}
1378	if sess != nil {
1379		var providerName, model string
1380		sess, ag, providerName, model, err = applyInitialSessionResume(ctx, args, r, extMgr, sess, ag)
1381		if err != nil {
1382			return err
1383		}
1384		r.Provider, r.Model = providerName, model
1385		ag.CommitToolResult = func(_ string, result core.ToolResult) error { return persistToolResultState(extMgr, sess, result) }
1386		defer func() { runErr = joinSessionCloseError(runErr, sess) }()
1387	}
1388	announceSession(extMgr, sess)
1389	wireRetryLifecyclePersistence(ag, sess)
1390	if sess != nil {
1391		defer func() { runErr = errors.Join(runErr, sess.FlushRetryLifecycle(ag.LastTurnUsage(), ag.Cost())) }()
1392	}
1393
1394	piped, _ := readAllStdin()
1395	prompt := combinePromptInput(piped, args.Prompt)
1396	if prompt == "" {
1397		return fmt.Errorf("json mode requires a prompt (arg or stdin)")
1398	}
1399
1400	start := len(ag.Messages())
1401	enc := json.NewEncoder(os.Stdout)
1402	preSink := func(ev core.AgentEvent) {
1403		_ = enc.Encode(modes.EventToJSON(ev))
1404	}
1405	if err := runZutfileStartupPre(ctx, args.StartupPre, r.CWD, r.Sandbox, ag, preSink, os.Stderr); err != nil {
1406		_ = enc.Encode(map[string]any{"type": "error", "message": err.Error()})
1407		return err
1408	}
1409	if err := reloadResourcesAfterStartupPre(ctx, args, extMgr, r.Sandbox, ag); err != nil {
1410		_ = enc.Encode(map[string]any{"type": "error", "message": err.Error()})
1411		return err
1412	}
1413	var persistCompaction func([]provider.Message) error
1414	if sess != nil {
1415		persistCompaction = sess.AppendCompaction
1416	}
1417	prompt = preloadSkillPins(args, freshSession, prompt, extMgr.Skills(), os.Stderr)
1418	recovery, err := modes.RunJSONWithContextRecovery(ctx, ag, prompt, nil, os.Stdout, persistCompaction)
1419	transcriptStart := start
1420	if recovery.Compacted {
1421		transcriptStart = recovery.OutputStart
1422	}
1423	if persistErr := WriteNewTranscript(ag, sess, transcriptStart); persistErr != nil {
1424		finalErr := persistErr
1425		if err != nil {
1426			finalErr = errors.Join(err, persistErr)
1427		}
1428		_ = enc.Encode(map[string]any{"type": "error", "message": finalErr.Error()})
1429		return finalErr
1430	}
1431	return err
1432}
1433
1434// runZutfileStartupPre runs entry.pre before the main non-interactive prompt.
1435// "!command" values execute via BashTool; other values are sent as an agent turn.
1436// shellOut receives live shell chunks (typically os.Stderr); sink receives
1437// agent events for plain-text pre (stream mode wires a live text sink).
1438func runZutfileStartupPre(ctx context.Context, pre, cwd string, sandbox *tools.Sandbox, ag *core.Agent, sink func(core.AgentEvent), shellOut io.Writer) error {
1439	pre = strings.TrimSpace(pre)
1440	if pre == "" {
1441		return nil
1442	}
1443	if cmd, ok := modes.ShellEscapeCommand(pre); ok {
1444		raw, err := json.Marshal(map[string]any{
1445			"command": cmd,
1446			"timeout": modes.ShellEscapeTimeoutSeconds,
1447		})
1448		if err != nil {
1449			return err
1450		}
1451		if shellOut != nil {
1452			fmt.Fprintf(shellOut, "$ %s\n", cmd)
1453			if f, ok := shellOut.(interface{ Sync() error }); ok {
1454				_ = f.Sync()
1455			}
1456		}
1457		progress := func(chunk string) {
1458			if shellOut == nil || chunk == "" {
1459				return
1460			}
1461			fmt.Fprint(shellOut, chunk)
1462			if f, ok := shellOut.(interface{ Sync() error }); ok {
1463				_ = f.Sync()
1464			}
1465		}
1466		bash := &tools.BashTool{CWD: cwd, Sandbox: sandbox}
1467		res, err := bash.Execute(ctx, raw, progress)
1468		if err != nil {
1469			return fmt.Errorf("zutfile entry.pre: %w", err)
1470		}
1471		if res.IsError {
1472			var sb strings.Builder
1473			for _, c := range res.Content {
1474				if tb, ok := c.(provider.TextBlock); ok {
1475					sb.WriteString(tb.Text)
1476				}
1477			}
1478			msg := strings.TrimSpace(sb.String())
1479			if msg == "" {
1480				msg = "command failed"
1481			}
1482			return fmt.Errorf("zutfile entry.pre: %s", msg)
1483		}
1484		return nil
1485	}
1486	if ag == nil {
1487		return fmt.Errorf("zutfile entry.pre requires an agent for non-shell prompts")
1488	}
1489	if sink == nil {
1490		sink = func(core.AgentEvent) {}
1491	}
1492	return ag.Prompt(ctx, pre, nil, sink)
1493}
1494
1495var errInteractiveAgentChanged = errors.New("interactive agent or web-search policy changed during prompt refresh")
1496
1497// refreshAgentToolsAndPrompt re-resolves tools (including rediscovered
1498// skills and currently loaded extension tools) and updates the live
1499// agent's registry and system prompt. Used after /reload-ext and after
1500// zutfile entry.pre installs new skills or extensions.
1501// mutateRegistry, if non-nil, can inject session-specific tools (e.g. the subagent tool).
1502// interactive, when non-nil, serializes the final commit with agent replacement.
1503func refreshAgentToolsAndPrompt(args Args, sharedSandbox *tools.Sandbox, extToolAdapter ExtensionToolSource, ag *core.Agent, mutateRegistry func(core.Registry) core.Registry, interactive *modes.Interactive) (subagents.WebSearchPolicy, error) {
1504	if ag == nil {
1505		return subagents.WebSearchDeny, nil
1506	}
1507	var webSearchPolicyGeneration uint64
1508	if interactive != nil {
1509		// Capture before Resolve: the final commit must fail if settings,
1510		// Telegram attachment, or a newer registry commit changes policy while
1511		// filesystem/config/extension discovery is in flight.
1512		webSearchPolicyGeneration = interactive.WebSearchPolicyGeneration()
1513	}
1514	resolved, err := Resolve(args, true)
1515	if err != nil {
1516		return subagents.WebSearchDeny, fmt.Errorf("refresh agent prompt and tools: %w", err)
1517	}
1518	if sharedSandbox != nil {
1519		resolved.UseSandbox(sharedSandbox)
1520	}
1521	if extToolAdapter != nil {
1522		resolved.MergeExtensionTools(extToolAdapter)
1523	}
1524	reg := resolved.ToolRegistry
1525	if resolved.WebSearchPolicy != subagents.WebSearchAllow {
1526		tools.RemoveWebCapabilities(reg)
1527	}
1528	if mutateRegistry != nil {
1529		reg = mutateRegistry(reg)
1530	}
1531	if interactive != nil {
1532		oldTools, applied := interactive.ApplyAgentPromptConfigAtWebSearchGeneration(ag, resolved.SystemPrompt, reg, webSearchPolicyGeneration)
1533		if !applied {
1534			_ = tools.CloseLSPManagers(reg)
1535			return subagents.WebSearchDeny, errInteractiveAgentChanged
1536		}
1537		interactive.DeferUntilIdle(func() {
1538			_ = tools.CloseLSPManagers(oldTools)
1539		})
1540		return webSearchPolicyForRegistry(resolved.WebSearchPolicy, ag.ToolsSnapshot()), nil
1541	}
1542	oldTools := ag.SetPromptConfig(resolved.SystemPrompt, reg)
1543	_ = tools.CloseLSPManagers(oldTools)
1544	return webSearchPolicyForRegistry(resolved.WebSearchPolicy, reg), nil
1545}
1546
1547func webSearchPolicyForRegistry(policy subagents.WebSearchPolicy, reg core.Registry) subagents.WebSearchPolicy {
1548	if policy != subagents.WebSearchAllow {
1549		return subagents.WebSearchDeny
1550	}
1551	for _, name := range tools.WebCapabilityNames {
1552		if _, ok := reg[name]; !ok {
1553			return subagents.WebSearchDeny
1554		}
1555	}
1556	return subagents.WebSearchAllow
1557}
1558
1559func webSearchToolAllowedForSession(args Args) bool {
1560	if args.NoTools || args.PermissionSet != nil {
1561		return false
1562	}
1563	if args.ToolsSet || len(args.Tools) > 0 {
1564		return toolListContains(args.Tools, "web_search")
1565	}
1566	return true
1567}
1568
1569func webSearchExplicitlyEnabledForSession(args Args) bool {
1570	return !args.NoTools && args.PermissionSet == nil && (args.ToolsSet || len(args.Tools) > 0) && toolListContains(args.Tools, "web_search")
1571}
1572
1573// reloadResourcesAfterStartupPre reloads extensions (if any) and
1574// refreshes the agent tool registry + system prompt so skills/extensions
1575// installed by entry.pre are visible to the following turn.
1576func reloadResourcesAfterStartupPre(ctx context.Context, args Args, extMgr *extensions.Manager, sharedSandbox *tools.Sandbox, ag *core.Agent) error {
1577	_, err := reloadResourcesAfterStartupPreWithRegistry(ctx, args, extMgr, sharedSandbox, ag, nil)
1578	return err
1579}
1580
1581func reloadResourcesAfterStartupPreWithRegistry(ctx context.Context, args Args, extMgr *extensions.Manager, sharedSandbox *tools.Sandbox, ag *core.Agent, mutateRegistry func(core.Registry) core.Registry) (subagents.WebSearchPolicy, error) {
1582	if strings.TrimSpace(args.StartupPre) == "" || ag == nil {
1583		return subagents.WebSearchDeny, nil
1584	}
1585	adapter := &extToolAdapter{mgr: extMgr}
1586	if extMgr != nil {
1587		_ = extMgr.Reload(ctx, 2*time.Second)
1588	}
1589	return refreshAgentToolsAndPrompt(args, sharedSandbox, adapter, ag, mutateRegistry, nil)
1590}
1591
1592// ---- interactive mode: opens the TUI even without credentials ----
1593
1594func interactiveQuickModelShortcuts(shortcuts []QuickModelShortcut) []modes.QuickModelShortcut {
1595	result := make([]modes.QuickModelShortcut, len(shortcuts))
1596	for idx, s := range shortcuts {
1597		p := normalizedModelProfile(s)
1598		result[idx] = modes.QuickModelShortcut{Provider: p.Provider, Model: p.Model, Reasoning: p.Reasoning, FastMode: p.FastMode}
1599	}
1600	return result
1601}
1602
1603func runInteractive(ctx context.Context, args Args, version string) (runErr error) {
1604	initialCfg, _ := LoadConfig()
1605	if initialCfg.Goals.MaxTokenBudget != nil && *initialCfg.Goals.MaxTokenBudget == 0 {
1606		return errors.New("goals.max_token_budget must be greater than zero")
1607	}
1608	// Resolve WITHOUT requiring credentials.
1609	r, err := Resolve(args, false)
1610	if err != nil {
1611		return err
1612	}
1613	// Resolve may have repaired a stale favorite. Use the saved repair for
1614	// the UI's active-profile comparison instead of the pre-resolution copy.
1615	if resolvedCfg, err := LoadConfig(); err == nil {
1616		initialCfg.QuickModelShortcuts = resolvedCfg.QuickModelShortcuts
1617		initialCfg.ActiveModelProfile = resolvedCfg.ActiveModelProfile
1618	}
1619
1620	authStore := AuthStoreFor()
1621	mgr := auth.NewManager(authStore)
1622	defer mgr.Close()
1623
1624	// Keep the sandbox pointer stable across agent rebuilds (login / model
1625	// switch). The Interactive UI toggles the lock via this pointer, and
1626	// rebuilt tool instances must share the same one so the lock sticks.
1627	sharedSandbox := r.Sandbox
1628
1629	// persistMu guards the active session and selection across the agent
1630	// loop, TUI callbacks, and session swaps. Declare it before tool
1631	// construction because subagent defaults must read the live selection too.
1632	var persistMu sync.Mutex
1633	// Serialize session transitions with persistence callbacks so a candidate
1634	// is read only after the old agent is flushed and cannot be overwritten by
1635	// an in-flight append before commit.
1636	var sessionTransitionMu sync.RWMutex
1637	activeProvider := r.Provider
1638	activeModel := r.Model
1639	var sess *core.Session
1640	scheduleEngine := scheduler.NewEngine(nil)
1641	var scheduledSessionMu sync.Mutex
1642	scheduledSessionLocks := make(map[string]*sync.Mutex)
1643	lockScheduledSession := func(sessionID string) func() {
1644		scheduledSessionMu.Lock()
1645		lock := scheduledSessionLocks[sessionID]
1646		if lock == nil {
1647			lock = new(sync.Mutex)
1648			scheduledSessionLocks[sessionID] = lock
1649		}
1650		scheduledSessionMu.Unlock()
1651		lock.Lock()
1652		return lock.Unlock
1653	}
1654
1655	// Build the extension manager BEFORE the agent so we can fold
1656	// extension-defined tools into the registry. Attach the interactive
1657	// host after constructing it below so startup alerts can be buffered.
1658	var iv *modes.Interactive
1659	extHooks := &interactiveExtHooks{}
1660	extMgr := extensions.New(ZutHome(), r.CWD, version, r.Provider, r.Model, extHooks)
1661	var startupExtensionErrors []string
1662	// --ext paths first so they win against installed extensions of
1663	// the same name (loadOne's first-write-wins semantics).
1664	for _, e := range extMgr.LoadExplicit(ctx, args.Exts) {
1665		startupExtensionErrors = append(startupExtensionErrors, e.Error())
1666	}
1667	// --no-ext skips the global + project-local discovery scan;
1668	// explicit --ext paths above are still honoured so you can run
1669	// "only this extension" with --no-ext --ext ./x.
1670	if !args.NoExt {
1671		for _, e := range extMgr.Discover(ctx) {
1672			startupExtensionErrors = append(startupExtensionErrors, e.Error())
1673		}
1674	}
1675	// Wait briefly for extensions to flush their initial register_tool
1676	// frames before we build the agent's tool registry. Half a second
1677	// is plenty for any extension that's actually well-behaved; ones
1678	// that don't send a ready frame eat the full grace and proceed.
1679	// 3s is the per-extension grace period for the ready frame.
1680	// Native binaries are instant; runtimes like `npx tsx` take ~1.5s
1681	// from cold cache. The wait is tight only for extensions that
1682	// haven't sent ready by then; ones that signalled earlier release
1683	// the wait immediately.
1684	extMgr.WaitForReady(3 * time.Second)
1685	defer extMgr.Stop(2 * time.Second)
1686
1687	extToolAdapter := &extToolAdapter{mgr: extMgr}
1688	r.MergeExtensionTools(extToolAdapter)
1689	webSearchGuard := &webSearchSessionGuard{}
1690
1691	// Keep the shared subagent runtime in the outer scope: session reloads,
1692	// dashboard updates, agent rebuilds, and shutdown all use the same owner.
1693	var runtime *subagentRuntime
1694	onResidentAccepted := func(spec subagents.ResidentChildSpec, turnID, _ string) {
1695		if iv != nil {
1696			iv.TrackResidentSubagent(spec.ID, turnID)
1697		}
1698	}
1699	onResidentCompletion := func(completion subagents.ResidentCompletion) bool {
1700		return iv != nil && iv.ReportResidentSubagent(completion)
1701	}
1702	activeProviderForSubagents := func() string {
1703		persistMu.Lock()
1704		defer persistMu.Unlock()
1705		return activeProvider
1706	}
1707	activeModelForSubagents := func() string {
1708		persistMu.Lock()
1709		defer persistMu.Unlock()
1710		return activeModel
1711	}
1712
1713	runtime = newSubagentRuntime(subagentRuntimeConfig{
1714		Args:               args,
1715		Root:               filepath.Join(ZutHome(), "subagents"),
1716		RepoRoot:           r.CWD,
1717		Provider:           r.Provider,
1718		Model:              r.Model,
1719		Reasoning:          r.Reasoning,
1720		BaseURL:            r.BaseURL,
1721		InsecureTLS:        r.InsecureTLS,
1722		FastMode:           r.FastMode,
1723		ContextWindow:      r.ContextWindow,
1724		Policy:             subagentPolicyFromConfig(initialCfg.Subagents),
1725		WebSearchPolicy:    webSearchPolicyForRegistry(r.WebSearchPolicy, r.ToolRegistry),
1726		WebSearchGuard:     webSearchGuard,
1727		ActiveProvider:     activeProviderForSubagents,
1728		ActiveModel:        activeModelForSubagents,
1729		OnResidentSpawned:  onResidentAccepted,
1730		ResidentCompletion: onResidentCompletion,
1731	})
1732	reloadDone := make(chan struct{})
1733	var reloadErrs []error
1734	if resident := runtime.ResidentManager(); resident != nil {
1735		// Resident reconciliation reads only the new journal format. It marks
1736		// active durable work interrupted and exposes it for an explicit new
1737		// prompt; it never constructs a child or replays an accepted task.
1738		go func() {
1739			reloadErrs = resident.Reconcile()
1740			close(reloadDone)
1741		}()
1742	} else {
1743		close(reloadDone)
1744	}
1745	defer func() {
1746		<-reloadDone
1747		_ = closeSubagentRuntimeFresh(runtime)
1748	}()
1749
1750	prepareInteractiveRegistry := runtime.PrepareRegistry
1751	prepareResolvedInteractiveRegistry := runtime.PrepareResolvedRegistry
1752	prepareResolvedInteractiveRegistry(r.ToolRegistry, r.WebSearchPolicy)
1753	initialWebSearchPolicy := webSearchPolicyForRegistry(r.WebSearchPolicy, r.ToolRegistry)
1754	webSearchGuard.setAvailable(initialWebSearchPolicy == subagents.WebSearchAllow)
1755	runtime.SetWebSearchPolicy(initialWebSearchPolicy)
1756	resolveSubagent := runtime.resolveSubagent
1757
1758	// Confirmation gate: when --no-yolo is on, the agent must ask
1759	// the user before every tool call. In interactive mode the TUI
1760	// provides the Confirmer; in print/json/rpc modes there's no
1761	// way to prompt, so the gate is constructed with a nil inner
1762	// which auto-refuses every call with a helpful reason.
1763	var confirmGate *core.ConfirmGate
1764	if args.NoYolo {
1765		confirmGate = core.NewConfirmGate(nil) // set below for interactive
1766	}
1767
1768	// Capture current args in a closure so BuildAgent can re-resolve
1769	// after a successful login (picks up the newly stored credential).
1770	wireAgentExt := func(a *core.Agent) *core.Agent {
1771		if a == nil {
1772			return a
1773		}
1774		a.BeforeToolExecute = func(call provider.ToolCallBlock) (bool, string, json.RawMessage) {
1775			// Guards run before confirmation because they may rewrite the
1776			// arguments. The user must approve the effective call that will
1777			// actually execute, not the model's original arguments.
1778			r := extMgr.InterceptToolCall(ctx, call.ID, call.Name, call.Arguments)
1779			if r.Block {
1780				return false, r.Reason, nil
1781			}
1782			effectiveArgs := call.Arguments
1783			if len(r.ModifiedArgs) > 0 && json.Valid(r.ModifiedArgs) {
1784				effectiveArgs = r.ModifiedArgs
1785			}
1786			if confirmGate != nil {
1787				var content strings.Builder
1788				// The remembered "always allow" grant is keyed by tool name,
1789				// or a finer per-action key when the tool asks for one (the
1790				// subagent facade). Compute it from the effective args so a
1791				// grant matches the call that will actually run.
1792				_, currentTools := a.PromptConfig()
1793				confirmationKey := confirmationKeyFor(currentTools, call.Name, effectiveArgs)
1794				if tool, err := currentTools.Get(call.Name); err == nil {
1795					if previewer, ok := tool.(core.ToolPreviewer); ok {
1796						preview, err := previewer.Preview(ctx, effectiveArgs)
1797						if err != nil {
1798							return false, err.Error(), nil
1799						}
1800						for _, block := range preview.Content {
1801							if text, ok := block.(provider.TextBlock); ok {
1802								if content.Len() > 0 {
1803									content.WriteString("\n")
1804								}
1805								content.WriteString(text.Text)
1806							}
1807						}
1808					}
1809				}
1810				ok, reason, _ := confirmGate.CheckToolCall(core.ToolCallConfirmation{
1811					ID:      call.ID,
1812					Name:    call.Name,
1813					Key:     confirmationKey,
1814					Summary: core.BuildPreview(effectiveArgs, 120),
1815					Content: content.String(),
1816					Origin:  call.Origin,
1817				})
1818				if !ok {
1819					return false, reason, nil
1820				}
1821			}
1822			return true, "", r.ModifiedArgs
1823		}
1824		a.BeforeTurnContext = func(turnCtx context.Context, step int) (bool, string, string) {
1825			r := extMgr.InterceptTurnStart(turnCtx, step)
1826			return !r.Block, r.Reason, r.Context
1827		}
1828		a.BeforeAssistantMessage = func(text string) (bool, string, string) {
1829			r := extMgr.InterceptAssistantMessage(ctx, text)
1830			if r.Block {
1831				return false, r.Reason, ""
1832			}
1833			return true, "", r.ReplaceText
1834		}
1835		a.OnEvent = func(ev core.AgentEvent) { fanoutAgentEvent(extMgr, ev) }
1836		runtime.WireRequiredWorkerGate(a)
1837		return a
1838	}
1839
1840	scheduleTool := &tools.ScheduleTool{
1841		Engine: scheduleEngine,
1842		SessionID: func() string {
1843			persistMu.Lock()
1844			defer persistMu.Unlock()
1845			if sess == nil {
1846				return ""
1847			}
1848			return sess.ID
1849		},
1850		Location: func() *time.Location { return time.Local },
1851	}
1852	schedulerAllowed := !args.NoTools && (!args.ToolsSet && len(args.Tools) == 0 || toolListContains(args.Tools, tools.ScheduleToolName))
1853	installSchedulerTool := func(registry core.Registry) core.Registry {
1854		if schedulerAllowed {
1855			registry[tools.ScheduleToolName] = scheduleTool
1856		}
1857		return registry
1858	}
1859	installSchedulerTool(r.ToolRegistry)
1860	prepareInteractiveRegistryWithScheduler := func(registry core.Registry) core.Registry {
1861		return prepareInteractiveRegistry(installSchedulerTool(registry))
1862	}
1863
1864	var ag *core.Agent
1865	buildAgent := func() (*core.Agent, string, string, error) {
1866		resolved, err := Resolve(args, true)
1867		if err != nil {
1868			return nil, "", "", err
1869		}
1870		resolved.UseSandbox(sharedSandbox)
1871		runtime.SetProvider(resolved.Provider)
1872		runtime.SetModel(resolved.Model)
1873		runtime.SetContextWindow(resolved.ContextWindow)
1874		runtime.SetProviderSettings(resolved.BaseURL, resolved.InsecureTLS)
1875		runtime.SetFastMode(resolved.FastMode)
1876		resolved.MergeExtensionTools(extToolAdapter)
1877		installSchedulerTool(resolved.ToolRegistry)
1878		prepareResolvedInteractiveRegistry(resolved.ToolRegistry, resolved.WebSearchPolicy)
1879		return wireAgentExt(resolved.newInteractiveAgent()), resolved.Provider, resolved.Model, nil
1880	}
1881
1882	// Rebuild agent with an explicit provider/model override.
1883	buildAgentFor := func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
1884		next := args
1885		if providerOverride != "" {
1886			next.Provider = providerOverride
1887		}
1888		if modelOverride != "" {
1889			next.Model = modelOverride
1890		}
1891		resolved, err := Resolve(next, true)
1892		if err != nil {
1893			return nil, "", "", err
1894		}
1895		resolved.UseSandbox(sharedSandbox)
1896		runtime.SetProvider(resolved.Provider)
1897		runtime.SetModel(resolved.Model)
1898		runtime.SetContextWindow(resolved.ContextWindow)
1899		runtime.SetProviderSettings(resolved.BaseURL, resolved.InsecureTLS)
1900		runtime.SetFastMode(resolved.FastMode)
1901		resolved.MergeExtensionTools(extToolAdapter)
1902		installSchedulerTool(resolved.ToolRegistry)
1903		prepareResolvedInteractiveRegistry(resolved.ToolRegistry, resolved.WebSearchPolicy)
1904		return wireAgentExt(resolved.newInteractiveAgent()), resolved.Provider, resolved.Model, nil
1905	}
1906
1907	// Rebuild agent for the rescue picker after a recoverable failure.
1908	// Unlike buildAgentFor, this drops launch-time --api-key and
1909	// --base-url overrides because those are typically the cause of the
1910	// rescue (a bad key, a typo'd base URL, or a corporate gateway that
1911	// only the originally-picked provider needed). Re-resolving without
1912	// them lets the rescue retry use env vars / auth.json / provider
1913	// defaults the way zut would have without the overrides.
1914	buildAgentForRescue := func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
1915		next := args
1916		next.APIKey = ""
1917		next.BaseURL = ""
1918		if providerOverride != "" {
1919			next.Provider = providerOverride
1920		}
1921		if modelOverride != "" {
1922			next.Model = modelOverride
1923		}
1924		resolved, err := Resolve(next, true)
1925		if err != nil {
1926			return nil, "", "", err
1927		}
1928		resolved.UseSandbox(sharedSandbox)
1929		runtime.SetProvider(resolved.Provider)
1930		runtime.SetModel(resolved.Model)
1931		runtime.SetContextWindow(resolved.ContextWindow)
1932		runtime.SetProviderSettings(resolved.BaseURL, resolved.InsecureTLS)
1933		runtime.SetFastMode(resolved.FastMode)
1934		resolved.MergeExtensionTools(extToolAdapter)
1935		installSchedulerTool(resolved.ToolRegistry)
1936		prepareResolvedInteractiveRegistry(resolved.ToolRegistry, resolved.WebSearchPolicy)
1937		return wireAgentExt(resolved.newInteractiveAgent()), resolved.Provider, resolved.Model, nil
1938	}
1939
1940	if r.HasCredential() {
1941		ag = wireAgentExt(r.newInteractiveAgent())
1942	}
1943
1944	// /reload-ext callback: after the manager has respawned every
1945	// extension, re-resolve the tool registry (built-ins + freshly-
1946	// registered extension tools) and swap it onto the current
1947	// agent in-place. The current agent may have been replaced by a
1948	// /model swap since spawn, so re-read the live `ag` on each
1949	// invocation.
1950	extMgr.SetOnReload(func() {
1951		current := liveInteractiveAgent(iv, ag)
1952		if current == nil {
1953			return
1954		}
1955		if _, err := refreshAgentToolsAndPrompt(args, sharedSandbox, extToolAdapter, current, prepareInteractiveRegistryWithScheduler, iv); err != nil && iv != nil {
1956			iv.ReportError(err)
1957		}
1958	})
1959
1960	freshSession := true
1961	var sessBaselineMsgs int // messages already on disk when current session opened
1962	var sessionTitlePending bool
1963	// initialPlanSnapshots is reconstructed from the untrimmed resumed
1964	// transcript. The interactive seeds from it because the live agent only
1965	// receives the trim window after a resume.
1966	var initialPlanSnapshots map[string]core.PlanUpdate
1967	if !args.NoSess && ag != nil {
1968		var sessErr error
1969		sess, freshSession, sessErr = openOrCreateSessionState(ctx, args, r, ag, version)
1970		if sessErr != nil {
1971			return sessErr
1972		}
1973		sessBaselineMsgs = len(ag.Messages())
1974		if sess != nil {
1975			// Startup resume uses the same provider/model-aware preparation as
1976			// the interactive session picker. This matters for --continue,
1977			// --resume, and explicit --session paths whose latest meta row was
1978			// written by another provider/model.
1979			candidate, resumeErr := applySessionResume(sess, ag, r.Provider, r.Model,
1980				strings.TrimSpace(args.Provider) != "", strings.TrimSpace(args.Model) != "", buildAgentFor)
1981			if resumeErr != nil {
1982				return resumeErr
1983			}
1984			sess = candidate.session
1985			ag = candidate.agent
1986			sessBaselineMsgs = candidate.fullMessageCount
1987			initialPlanSnapshots = candidate.planSnapshots
1988			activeProvider = candidate.provider
1989			activeModel = candidate.model
1990			r.Provider = candidate.provider
1991			r.Model = candidate.model
1992		}
1993		if sess != nil && ag != nil {
1994			sessionTitlePending = sess.Meta.Parent != "" && sess.Title == "" && len(ag.Messages()) <= sess.Meta.ForkPoint
1995		}
1996	}
1997	announceSession(extMgr, sess)
1998	// Print the hint after the session close defer below and all terminal/TUI
1999	// cleanup defers. Close removes a fresh empty session, so only a session
2000	// that remains discoverable through the hinted command gets a hint.
2001	defer func() {
2002		if !iv.ExitedViaCtrlC() {
2003			return
2004		}
2005		persistMu.Lock()
2006		current := sess
2007		persistMu.Unlock()
2008		hasMessages := false
2009		if finalAg := iv.Agent(); finalAg != nil {
2010			hasMessages = finalAg.MessageCount() > 0
2011		}
2012		if resumeID := resumableSessionID(ZutHome(), current, hasMessages); resumeID != "" {
2013			fmt.Print(resumeSessionHint(resumeID))
2014		}
2015	}()
2016	defer func() {
2017		persistMu.Lock()
2018		defer persistMu.Unlock()
2019		runErr = joinSessionCloseError(runErr, sess)
2020	}()
2021
2022	// persistMessage is the per-message hook bound to the agent. It
2023	// appends each new transcript message to the live session as soon
2024	// as it lands, so a kill / closed terminal / OS crash costs at
2025	// most the in-flight turn instead of the whole session. The
2026	// baseline counter advances in lock-step so the exit-time flush
2027	// doesn't double-write rows already on disk.
2028	persistMessage := func(m provider.Message) {
2029		sessionTransitionMu.RLock()
2030		defer sessionTransitionMu.RUnlock()
2031		persistMu.Lock()
2032		defer persistMu.Unlock()
2033		if sess == nil {
2034			return
2035		}
2036		if err := sess.AppendMessage(m); err == nil {
2037			sessBaselineMsgs++
2038		}
2039	}
2040	persistUsage := func(cum provider.Usage) {
2041		sessionTransitionMu.RLock()
2042		defer sessionTransitionMu.RUnlock()
2043		persistMu.Lock()
2044		defer persistMu.Unlock()
2045		if sess == nil {
2046			return
2047		}
2048		_ = sess.AppendUsage(cum, cum)
2049	}
2050	persistRetryLifecycle := func(record core.RetryLifecycleRecord) {
2051		sessionTransitionMu.RLock()
2052		defer sessionTransitionMu.RUnlock()
2053		persistMu.Lock()
2054		defer persistMu.Unlock()
2055		if sess != nil {
2056			sess.RecordRetryLifecycle(record)
2057		}
2058	}
2059	persistCompaction := func(messages []provider.Message) {
2060		sessionTransitionMu.RLock()
2061		var session *extproto.SessionContext
2062		var states map[string]json.RawMessage
2063		persistMu.Lock()
2064		if sess != nil {
2065			if err := sess.AppendCompaction(messages); err == nil {
2066				sessBaselineMsgs = len(messages)
2067				session = sessionContext(sess)
2068				states = copyExtensionStates(sess.ExtensionState)
2069			}
2070		}
2071		persistMu.Unlock()
2072		sessionTransitionMu.RUnlock()
2073		if session != nil {
2074			extMgr.EmitSessionEvent("session_compacted", session, states)
2075		}
2076	}
2077	persistCompactHandoff := func(state json.RawMessage) error {
2078		sessionTransitionMu.RLock()
2079		defer sessionTransitionMu.RUnlock()
2080		persistMu.Lock()
2081		defer persistMu.Unlock()
2082		if sess == nil {
2083			return nil
2084		}
2085		return sess.UpdateCompactHandoff(state)
2086	}
2087	currentCompactHandoff := func() json.RawMessage {
2088		persistMu.Lock()
2089		defer persistMu.Unlock()
2090		if sess == nil {
2091			return nil
2092		}
2093		return append(json.RawMessage(nil), sess.Meta.CompactHandoff...)
2094	}
2095	persistGoal := func(goal *core.SessionGoal) error {
2096		sessionTransitionMu.RLock()
2097		defer sessionTransitionMu.RUnlock()
2098		persistMu.Lock()
2099		defer persistMu.Unlock()
2100		if sess == nil {
2101			return errors.New("session persistence is disabled")
2102		}
2103		return sess.UpdateGoal(goal)
2104	}
2105	persistGoalRuntime := func(goal *core.SessionGoal) error {
2106		sessionTransitionMu.RLock()
2107		defer sessionTransitionMu.RUnlock()
2108		persistMu.Lock()
2109		defer persistMu.Unlock()
2110		if sess == nil {
2111			return errors.New("session persistence is disabled")
2112		}
2113		return sess.UpdateGoalRuntime(goal)
2114	}
2115	ensureMission := func(objective string) error {
2116		sessionTransitionMu.RLock()
2117		defer sessionTransitionMu.RUnlock()
2118		persistMu.Lock()
2119		defer persistMu.Unlock()
2120		if sess == nil {
2121			return errors.New("session persistence is disabled")
2122		}
2123		return sess.EnsureMission(objective, core.MissionSourceUser)
2124	}
2125	currentGoal := func() *core.SessionGoal {
2126		persistMu.Lock()
2127		defer persistMu.Unlock()
2128		if sess == nil || sess.Meta.Goal == nil {
2129			return nil
2130		}
2131		goal := *sess.Meta.Goal
2132		return &goal
2133	}
2134	currentGoalHistory := func() []core.SessionGoal {
2135		persistMu.Lock()
2136		defer persistMu.Unlock()
2137		if sess == nil {
2138			return nil
2139		}
2140		return append([]core.SessionGoal(nil), sess.Meta.GoalHistory...)
2141	}
2142	currentPlan := func() []core.PlanStep {
2143		return liveInteractiveAgent(iv, ag).CurrentPlan()
2144	}
2145	persistedPlan := func() *core.SessionPlan {
2146		persistMu.Lock()
2147		defer persistMu.Unlock()
2148		if sess == nil || sess.Meta.Plan == nil {
2149			return nil
2150		}
2151		plan := *sess.Meta.Plan
2152		plan.Steps = append([]core.PlanStep(nil), sess.Meta.Plan.Steps...)
2153		return &plan
2154	}
2155	persistPlan := func(plan *core.SessionPlan) error {
2156		sessionTransitionMu.RLock()
2157		defer sessionTransitionMu.RUnlock()
2158		persistMu.Lock()
2159		defer persistMu.Unlock()
2160		if sess == nil {
2161			return errors.New("session persistence is disabled")
2162		}
2163		return sess.UpdatePlan(plan)
2164	}
2165	bindAgentToolResultSession := func(a *core.Agent, boundSession *core.Session) {
2166		if a == nil {
2167			return
2168		}
2169		a.CommitToolResult = func(_ string, result core.ToolResult) error {
2170			persistMu.Lock()
2171			defer persistMu.Unlock()
2172			return persistToolResultState(extMgr, boundSession, result, initialCfg.Goals.MaxTokenBudget)
2173		}
2174	}
2175	wireAgentPersist := func(a *core.Agent) *core.Agent {
2176		if a == nil {
2177			return a
2178		}
2179		persistMu.Lock()
2180		currentSession := sess
2181		persistMu.Unlock()
2182		if currentSession != nil {
2183			a.SetSessionTimeContext(currentSession.Meta.Started, currentSession.Meta.Timezone, currentSession.Meta.TimezoneOffset)
2184		}
2185		a.OnMessageAppended = persistMessage
2186		a.OnUsage = persistUsage
2187		a.OnRetryLifecycle = persistRetryLifecycle
2188		a.OnTranscriptCompacted = persistCompaction
2189		bindAgentToolResultSession(a, currentSession)
2190		return a
2191	}
2192	wireAgentPersist(ag)
2193	defer func() {
2194		closeAgentLSP(liveInteractiveAgent(iv, ag))
2195		closeResolvedLSP(r)
2196	}()
2197
2198	// Re-wrap the build closures so any agent constructed by the TUI
2199	// (login, /model swap to a different provider) also gets the
2200	// persistence hooks. Without this, switching provider would
2201	// silently revert to the old in-memory-only behaviour. Selection state
2202	// is published by the successful transition itself, not by preparation.
2203	baseBuildAgent := buildAgent
2204	buildAgent = func() (*core.Agent, string, string, error) {
2205		a, p, m, err := baseBuildAgent()
2206		return wireAgentPersist(a), p, m, err
2207	}
2208	baseBuildAgentFor := buildAgentFor
2209	buildAgentFor = func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
2210		a, p, m, err := baseBuildAgentFor(providerOverride, modelOverride)
2211		return wireAgentPersist(a), p, m, err
2212	}
2213	baseBuildAgentForRescue := buildAgentForRescue
2214	buildAgentForRescue = func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
2215		a, p, m, err := baseBuildAgentForRescue(providerOverride, modelOverride)
2216		return wireAgentPersist(a), p, m, err
2217	}
2218
2219	// loadSession replaces the current session with the one at path and
2220	// hands its messages to the agent. Used by the /sessions picker.
2221	loadSessionContext := func(loadCtx context.Context, path string) (loadErr error) {
2222		if err := loadCtx.Err(); err != nil {
2223			return err
2224		}
2225		selectedMeta, err := core.ManagedSessionMeta(loadCtx, agentSessionsRoot(ZutHome(), args), path)
2226		if err != nil {
2227			return fmt.Errorf("validate selected session: %w", err)
2228		}
2229		if info, statErr := os.Stat(selectedMeta.CWD); statErr != nil || !info.IsDir() {
2230			if statErr != nil {
2231				return fmt.Errorf("selected session cwd: %w", statErr)
2232			}
2233			return fmt.Errorf("selected session cwd is not a directory: %s", selectedMeta.CWD)
2234		}
2235		// Hold the transition lock from the pre-flush through the commit.
2236		// Persistence callbacks take the read side, so an active session
2237		// cannot be snapshotted before its lazy writes land or overwritten by
2238		// a callback while the candidate is being installed.
2239		if err := loadCtx.Err(); err != nil {
2240			return err
2241		}
2242		sessionTransitionMu.Lock()
2243		defer sessionTransitionMu.Unlock()
2244		if err := loadCtx.Err(); err != nil {
2245			return err
2246		}
2247		workspaceChanged := filepath.Clean(selectedMeta.CWD) != filepath.Clean(args.CWD)
2248
2249		currentAg := liveInteractiveAgent(iv, ag)
2250		if currentAg == nil {
2251			return fmt.Errorf("no agent running; log in first")
2252		}
2253
2254		persistMu.Lock()
2255		currentProvider := activeProvider
2256		currentModel := activeModel
2257		oldSess := sess
2258		// Flush before reading the candidate. Use the locked form rather
2259		// than the public callback, which would acquire the read lock held
2260		// by this transition.
2261		if oldSess != nil {
2262			next, flushErr := writeNewTranscriptLocked(currentAg, oldSess, sessBaselineMsgs)
2263			sessBaselineMsgs = next
2264			if flushErr != nil {
2265				persistMu.Unlock()
2266				return fmt.Errorf("flush current session: %w", flushErr)
2267			}
2268		}
2269		currentMessageCount := len(currentAg.Messages())
2270		currentCost := currentAg.Cost()
2271		persistMu.Unlock()
2272
2273		// A candidate build must not publish its provider/model as the live
2274		// selection before the candidate commits. The builder also stages
2275		// provider settings on the shared runtime, so restore those settings on
2276		// every failed path, including errors returned during preparation.
2277		runtimeConfig := runtime.snapshotConfiguration()
2278		oldCWD := args.CWD
2279		oldResolvedCWD := r.CWD
2280		oldPermissionSet := args.PermissionSet
2281		oldSandboxRoot := ""
2282		oldSandboxLocked := false
2283		if sharedSandbox != nil {
2284			oldSandboxRoot = sharedSandbox.Root
2285			oldSandboxLocked = sharedSandbox.Locked()
2286		}
2287		rollbackWorkspace := func() {
2288			if !workspaceChanged {
2289				return
2290			}
2291			args.CWD = oldCWD
2292			r.CWD = oldResolvedCWD
2293			args.PermissionSet = oldPermissionSet
2294			runtime.SetRepoRoot(oldResolvedCWD)
2295			if sharedSandbox != nil {
2296				sharedSandbox.Root = oldSandboxRoot
2297				if oldSandboxLocked {
2298					sharedSandbox.Lock()
2299				} else {
2300					sharedSandbox.Unlock()
2301				}
2302			}
2303		}
2304		if workspaceChanged {
2305			args.CWD = selectedMeta.CWD
2306			r.CWD = selectedMeta.CWD
2307			runtime.SetRepoRoot(selectedMeta.CWD)
2308			if args.PermissionSet != nil {
2309				expanded := args.PermissionSet.Expand(selectedMeta.CWD, args.AgentDataDir)
2310				args.PermissionSet = &expanded
2311			}
2312			if sharedSandbox != nil {
2313				sharedSandbox.Root = selectedMeta.CWD
2314				if oldSandboxLocked {
2315					sharedSandbox.Lock()
2316				} else {
2317					sharedSandbox.Unlock()
2318				}
2319			}
2320		}
2321		committed := false
2322		var candidate sessionResumeCandidate
2323		defer func() {
2324			if !committed {
2325				rollbackWorkspace()
2326				runtime.restoreConfiguration(runtimeConfig)
2327				loadErr = joinSessionCloseError(loadErr, candidate.session)
2328			}
2329		}()
2330		if workspaceChanged {
2331			candidate, err = prepareSessionResumeForWorkspace(path, currentAg, currentProvider, currentModel, baseBuildAgentFor)
2332		} else {
2333			candidate, err = prepareSessionResume(path, currentAg, currentProvider, currentModel, baseBuildAgentFor)
2334		}
2335		if err != nil {
2336			// prepareSessionResume closes its append handle on failure;
2337			// no live agent or session has been changed.
2338			return err
2339		}
2340
2341		// Wire the private candidate before it can become visible. This is
2342		// also required for legacy same-provider resumes, where the current
2343		// agent is reused and only its transcript/usage are refreshed.
2344		wireAgentPersist(candidate.agent)
2345
2346		if err := loadCtx.Err(); err != nil {
2347			return err
2348		}
2349		persistMu.Lock()
2350		// A provider rebuild ran outside persistMu. Refuse to install it if
2351		// the live agent, session, selection, transcript, or cost changed in
2352		// the meantime; otherwise a slow resume could discard a newer turn or
2353		// model swap.
2354		live := liveInteractiveAgent(iv, ag)
2355		changed := sess != oldSess || activeProvider != currentProvider || activeModel != currentModel || live != currentAg
2356		if !changed && live != nil {
2357			changed = len(live.Messages()) != currentMessageCount || live.Cost() != currentCost
2358		}
2359		if changed {
2360			persistMu.Unlock()
2361			return fmt.Errorf("session changed while loading; please try again")
2362		}
2363		newStates := copyExtensionStates(candidate.session.ExtensionState)
2364		var oldCloseErr error
2365		if oldSess != nil {
2366			oldCloseErr = oldSess.Close()
2367		}
2368		sess = candidate.session
2369		bindAgentToolResultSession(candidate.agent, candidate.session)
2370		// Seed core plan state from the loaded session before the swap publishes
2371		// the agent; see applySessionResume for why this is required on resume.
2372		candidate.agent.SetPlan(candidate.session.Meta.PlanSteps())
2373		candidate.agent.SetSessionTimeContext(candidate.session.Meta.Started, candidate.session.Meta.Timezone, candidate.session.Meta.TimezoneOffset)
2374		sessionTitlePending = candidate.session != nil && candidate.session.Meta.Parent != "" && candidate.session.Title == "" && candidate.fullMessageCount <= candidate.session.Meta.ForkPoint
2375		// The live agent only receives a compact resume window, but the
2376		// session file remains intact. Keep the persistence baseline at
2377		// the original on-disk message count so future turns append after
2378		// the full session instead of duplicating the hydrated tail.
2379		sessBaselineMsgs = candidate.fullMessageCount
2380
2381		if candidate.rebuilt {
2382			// Commit the TUI agent while persistMu still protects the session
2383			// writer, so no new message can land on the old file between the
2384			// two state changes.
2385			if iv != nil {
2386				if workspaceChanged {
2387					iv.ApplySessionAgentWithCWD(candidate.agent, candidate.provider, candidate.model, selectedMeta.CWD, candidate.session.Meta.CompactHandoff)
2388				} else {
2389					iv.ApplySessionAgentWithCompactHandoff(candidate.agent, candidate.provider, candidate.model, candidate.session.Meta.CompactHandoff)
2390				}
2391			}
2392			ag = candidate.agent
2393		} else {
2394			currentAg.SetMessages(candidate.messages)
2395			currentAg.ResetRepetitionGuard()
2396			currentAg.SeedCost(candidate.cumulative)
2397			currentAg.SeedLastTurnUsage(candidate.lastTurn)
2398			ag = currentAg
2399		}
2400		activeProvider = candidate.provider
2401		activeModel = candidate.model
2402		if candidate.session != nil {
2403			// Keep the resident parent's session association in the same commit
2404			// as the session, agent, usage, and persistence baseline.
2405			runtime.SetActiveSession(candidate.session.ID)
2406			if iv != nil {
2407				iv.SetSubagentSessionScope(candidate.session.ID)
2408			}
2409		}
2410		committed = true
2411		persistMu.Unlock()
2412		if iv != nil {
2413			iv.SetPinnedSkillsPending(false)
2414			iv.RefreshGoal()
2415			iv.RefreshPlan(candidate.planSnapshots)
2416		}
2417		if candidate.session.Meta.Parent != "" {
2418			extMgr.EmitSessionEvent("session_forked", sessionContext(candidate.session), newStates)
2419		}
2420		extMgr.EmitSessionEvent("session_switched", sessionContext(candidate.session), newStates)
2421		if oldCloseErr != nil {
2422			return fmt.Errorf("close current session: %w", oldCloseErr)
2423		}
2424		return nil
2425	}
2426	loadSession := func(path string) error {
2427		return loadSessionContext(context.Background(), path)
2428	}
2429
2430	// changeCWD switches the running session to a new working directory.
2431	// Wired into InteractiveConfig.ChangeCWD and invoked by the hidden
2432	// /cd slash command (which itself is only fired by the workspaces
2433	// extension's panel-key Enter handler today; the user can type /cd
2434	// directly but it's not in autocomplete / help / the README).
2435	//
2436	// Steps, in order:
2437	//   1. resolve + validate the new path (~ expansion, abs/rel)
2438	//   2. stage the candidate cwd, sandbox, and subagent state
2439	//   3. rebuild the agent and allocate its new session
2440	//   4. commit all new state together, closing the old session last
2441	//   5. push the committed state into the running Interactive
2442	//
2443	// The /jail state is preserved verbatim: if the sandbox was locked
2444	// to the old cwd, it stays locked, just re-pointed at the new one.
2445	changeCWD := func(path string) error {
2446		sessionTransitionMu.Lock()
2447		defer sessionTransitionMu.Unlock()
2448		if path == "" {
2449			return fmt.Errorf("empty path")
2450		}
2451		// ~ expansion.
2452		if path == "~" || strings.HasPrefix(path, "~/") {
2453			home, herr := os.UserHomeDir()
2454			if herr != nil || home == "" {
2455				return fmt.Errorf("cannot expand ~: %v", herr)
2456			}
2457			if path == "~" {
2458				path = home
2459			} else {
2460				path = filepath.Join(home, path[2:])
2461			}
2462		}
2463		if !filepath.IsAbs(path) {
2464			path = filepath.Join(args.CWD, path)
2465		}
2466		absPath, err := filepath.Abs(path)
2467		if err != nil {
2468			return err
2469		}
2470		info, err := os.Stat(absPath)
2471		if err != nil {
2472			return err
2473		}
2474		if !info.IsDir() {
2475			return fmt.Errorf("not a directory: %s", absPath)
2476		}
2477
2478		currentAg := liveInteractiveAgent(iv, ag)
2479		if currentAg == nil {
2480			return fmt.Errorf("no agent running; log in first")
2481		}
2482
2483		// Stage the candidate state while retaining a complete rollback
2484		// snapshot. A failed rebuild or session allocation must leave the
2485		// current agent, session, sandbox, and subagent root usable.
2486		oldCWD := args.CWD
2487		oldResolvedCWD := r.CWD
2488		oldPermissionSet := args.PermissionSet
2489		persistMu.Lock()
2490		oldActiveProvider := activeProvider
2491		oldActiveModel := activeModel
2492		persistMu.Unlock()
2493		runtimeConfig := runtime.snapshotConfiguration()
2494		oldSandboxRoot := ""
2495		oldSandboxLocked := false
2496		if sharedSandbox != nil {
2497			oldSandboxRoot = sharedSandbox.Root
2498			oldSandboxLocked = sharedSandbox.Locked()
2499		}
2500		rollback := func() {
2501			args.CWD = oldCWD
2502			r.CWD = oldResolvedCWD
2503			args.PermissionSet = oldPermissionSet
2504			persistMu.Lock()
2505			activeProvider = oldActiveProvider
2506			activeModel = oldActiveModel
2507			persistMu.Unlock()
2508			runtime.restoreConfiguration(runtimeConfig)
2509			if sharedSandbox != nil {
2510				sharedSandbox.Root = oldSandboxRoot
2511				if oldSandboxLocked {
2512					sharedSandbox.Lock()
2513				} else {
2514					sharedSandbox.Unlock()
2515				}
2516			}
2517		}
2518
2519		args.CWD = absPath
2520		r.CWD = absPath
2521		runtime.SetRepoRoot(absPath)
2522		if args.PermissionSet != nil {
2523			expanded := args.PermissionSet.Expand(absPath, args.AgentDataDir)
2524			args.PermissionSet = &expanded
2525		}
2526		if sharedSandbox != nil {
2527			sharedSandbox.Root = absPath
2528			if oldSandboxLocked {
2529				sharedSandbox.Lock()
2530			} else {
2531				sharedSandbox.Unlock()
2532			}
2533		}
2534
2535		// Rebuild the agent so tools / AGENTS.md / system prompt
2536		// re-bind to the new cwd. buildAgent() reads from args + r.
2537		newAg, newProvider, newModel, berr := buildAgent()
2538		if berr != nil {
2539			rollback()
2540			return fmt.Errorf("rebuild agent: %w", berr)
2541		}
2542
2543		// Fresh session in the new cwd's bucket. We bypass
2544		// openOrCreateSession's --continue / --resume branches
2545		// because /cd's semantics are "start fresh here", matching
2546		// what relaunching `zut --cwd <path>` would do today.
2547		var newSess *core.Session
2548		if !args.NoSess {
2549			newRoot := agentSessionsRoot(ZutHome(), args)
2550			core.PruneEmptySessions(newRoot, absPath)
2551			var serr error
2552			newSess, serr = core.NewSession(newRoot, absPath, newProvider, newModel, version)
2553			if serr != nil {
2554				rollback()
2555				return fmt.Errorf("open session in %s: %w", absPath, serr)
2556			}
2557		}
2558
2559		// Invalidate any title request before swapping the host-owned session
2560		// pointer. The interactive state reset below happens after the swap,
2561		// so this guard closes the window where a late result could otherwise
2562		// be written to the new session.
2563		if iv != nil {
2564			iv.CancelPendingSessionTitle()
2565		}
2566
2567		// Commit only after every fallible operation has succeeded. Close
2568		// the old session last so rollback never has to resurrect it.
2569		var newStates map[string]json.RawMessage
2570		var oldCloseErr error
2571		persistMu.Lock()
2572		if sess != nil {
2573			next, flushErr := writeNewTranscriptLocked(currentAg, sess, sessBaselineMsgs)
2574			sessBaselineMsgs = next
2575			if flushErr != nil {
2576				persistMu.Unlock()
2577				rollbackErr := joinSessionCloseError(fmt.Errorf("flush current session: %w", flushErr), newSess)
2578				rollback()
2579				return rollbackErr
2580			}
2581			oldCloseErr = sess.Close()
2582		}
2583		sess = newSess
2584		bindAgentToolResultSession(newAg, newSess)
2585		if newSess != nil {
2586			newAg.SetSessionTimeContext(newSess.Meta.Started, newSess.Meta.Timezone, newSess.Meta.TimezoneOffset)
2587			newStates = copyExtensionStates(newSess.ExtensionState)
2588		}
2589		sessBaselineMsgs = 0
2590		sessionTitlePending = false
2591		activeProvider = newProvider
2592		activeModel = newModel
2593		persistMu.Unlock()
2594		ag = newAg
2595		closeAgentLSP(currentAg)
2596
2597		// Push the new state into the running Interactive.
2598		if iv != nil {
2599			startupPaths := instructionContextPaths(loadAgentsContext(absPath, ZutHome()))
2600			iv.ApplyChangedCWDWithStartupContext(newAg, newProvider, newModel, absPath, startupPaths)
2601			iv.RefreshGoal()
2602			// A new cwd starts a fresh session with no plan; reset the counters
2603			// and snapshot map so the previous workspace's checklist is gone.
2604			iv.RefreshPlan(nil)
2605		}
2606
2607		// Re-scope the subagent dashboard to the new session.
2608		if newSess != nil {
2609			runtime.SetActiveSession(newSess.ID)
2610			if iv != nil {
2611				iv.SetSubagentSessionScope(newSess.ID)
2612			}
2613		}
2614		if newSess != nil {
2615			extMgr.EmitSessionEvent("session_opened", sessionContext(newSess), newStates)
2616		}
2617		if oldCloseErr != nil {
2618			return fmt.Errorf("close current session: %w", oldCloseErr)
2619		}
2620		return nil
2621	}
2622
2623	devBuild := isDevVersion(version)
2624	if devBuild {
2625		fmt.Fprintln(os.Stderr, "zut:", devVersionNotice)
2626	}
2627	term := tui.NewProcTerm()
2628
2629	var updateCh chan modes.UpdateInfo
2630	var changelogCh chan modes.ChangelogPayload
2631	if !devBuild {
2632		// Kick off the async update check so the banner can appear when the
2633		// http response eventually arrives (usually <1s on cached DNS). Map
2634		// agent.UpdateInfo -> modes.UpdateInfo here to avoid a cyclic import.
2635		updateCh = make(chan modes.UpdateInfo, 1)
2636		go func() {
2637			defer close(updateCh)
2638			src := <-CheckForUpdateAsync(ZutHome(), version)
2639			updateCh <- modes.UpdateInfo{
2640				Current:   src.Current,
2641				Latest:    src.Latest,
2642				Available: src.Available,
2643				URL:       src.URL,
2644			}
2645		}()
2646
2647		// Changelog: when the running version differs from the last
2648		// version whose release notes the user dismissed, fetch the
2649		// release body from GitHub and have the TUI show it once. On
2650		// first-ever launch (no prior LastChangelogShown), seed the
2651		// stored version silently — don't dump release notes at someone
2652		// who just installed.
2653		changelogCh = make(chan modes.ChangelogPayload, 1)
2654		go func() {
2655			defer close(changelogCh)
2656			cfg, _ := LoadConfig()
2657			if cfg.LastChangelogShown == "" {
2658				SeedChangelogVersion(version)
2659				return
2660			}
2661			if !ShouldShowChangelog(version, cfg) {
2662				return
2663			}
2664			info := <-FetchChangelogAsync(version)
2665			if info.Body == "" {
2666				return
2667			}
2668			changelogCh <- modes.ChangelogPayload{
2669				Version: info.Version,
2670				Body:    info.Body,
2671				URL:     info.URL,
2672			}
2673		}()
2674	}
2675
2676	fastMode := r.FastMode
2677	quickModelShortcuts := interactiveQuickModelShortcuts(initialCfg.QuickModelShortcuts)
2678	themeEnv := strings.ToLower(strings.TrimSpace(os.Getenv("ZUT_THEME")))
2679	if themeEnv != "" && themeEnv != "auto" && themeEnv != "dark" && themeEnv != "light" {
2680		fmt.Fprintf(os.Stderr, "theme override: unsupported ZUT_THEME=%q; ignoring it\n", themeEnv)
2681		themeEnv = ""
2682	}
2683	themePreference := tui.ResolveThemePreference(initialCfg.Theme, themeEnv)
2684	theme, _, themeErr := tui.DetectThemeWithCustom(ZutHome(), themePreference.Effective, 80*time.Millisecond)
2685	if themeErr != nil {
2686		fmt.Fprintln(os.Stderr, "theme load:", themeErr)
2687		if !themePreference.Forced {
2688			themePreference = tui.ResolveThemePreference("", themeEnv)
2689			if initialCfg.Theme != "" {
2690				initialCfg.Theme = ""
2691				if err := SaveConfig(initialCfg); err != nil {
2692					fmt.Fprintln(os.Stderr, "theme reset:", err)
2693				}
2694			}
2695		}
2696	}
2697
2698	// Scope the dashboard to the active host session. The runtime owns
2699	// shutdown separately and closes the resident runtime exactly once.
2700	if sess != nil {
2701		runtime.SetActiveSession(sess.ID)
2702	}
2703
2704	var startupSkills []*skills.Skill
2705	if r.SkillTool != nil {
2706		startupSkills = r.SkillTool.Skills()
2707	}
2708	homeDir, _ := os.UserHomeDir()
2709	discoveredSubagents, _ := subagents.Discover(r.CWD, homeDir)
2710	subagentsAddendum := subagents.SystemPromptAddendum(discoveredSubagents)
2711	autoSubagentsToolAllowedForSession := autoSubagentsToolAllowed(args)
2712	autoSubagentsStatusToolAllowedForSession := autoSubagentsStatusToolAllowed(args)
2713	autoSubagentsStopToolAllowedForSession := autoSubagentsStopToolAllowed(args)
2714	autoSubagentsResumeToolAllowedForSession := autoSubagentsResumeToolAllowed(args)
2715	autoSubagentsInterruptToolAllowedForSession := autoSubagentsInterruptToolAllowed(args)
2716	webSearchToolAllowedForInvocation := webSearchToolAllowedForSession(args)
2717	webSearchInvocationOverride := webSearchExplicitlyEnabledForSession(args)
2718	setWebSearchAvailable := func(available bool) {
2719		webSearchGuard.setAvailable(available)
2720		policy := subagents.WebSearchDeny
2721		if available {
2722			policy = subagents.WebSearchAllow
2723		}
2724		runtime.SetWebSearchPolicy(policy)
2725	}
2726
2727	iv = modes.NewInteractive(modes.InteractiveConfig{
2728		Terminal:                          term,
2729		Theme:                             theme,
2730		InlineImagesEnabled:               initialCfg.InlineImagesEnabled,
2731		TerminalAlertsEnabled:             initialCfg.TerminalAlertsEnabled,
2732		TerminalTitleEnabled:              initialCfg.TerminalTitleEnabled,
2733		AutoSubagentsEnabled:              initialCfg.AutoSubagentsEnabled,
2734		WebSearchEnabled:                  initialCfg.WebSearchEnabled,
2735		WebSearchToolAllowed:              &webSearchToolAllowedForInvocation,
2736		WebSearchInvocationOverride:       webSearchInvocationOverride,
2737		AutoSubagentsToolAllowed:          &autoSubagentsToolAllowedForSession,
2738		AutoSubagentsStatusToolAllowed:    &autoSubagentsStatusToolAllowedForSession,
2739		AutoSubagentsStopToolAllowed:      &autoSubagentsStopToolAllowedForSession,
2740		AutoSubagentsResumeToolAllowed:    &autoSubagentsResumeToolAllowedForSession,
2741		AutoSubagentsInterruptToolAllowed: &autoSubagentsInterruptToolAllowedForSession,
2742		FastMode:                          &fastMode,
2743		LSPEnabled:                        initialCfg.LSPEnabled,
2744		SubagentLSPEnabled:                initialCfg.SubagentLSPEnabled,
2745		AutoCompactThreshold:              initialCfg.AutoCompactThreshold,
2746		GoalMaxTokenBudget:                initialCfg.Goals.MaxTokenBudget,
2747		JailByDefault:                     initialCfg.JailByDefault,
2748		QuickModelShortcuts:               quickModelShortcuts,
2749		ActiveModelProfile:                initialCfg.ActiveModelProfile,
2750		RecursiveFileSuggest:              initialCfg.RecursiveFileSuggest,
2751		RespectGitignore:                  initialCfg.RespectGitignore,
2752		CompactMode:                       initialCfg.CompactMode,
2753		TUIInputStyle:                     initialCfg.TUIInputStyle,
2754		TUIStatusPosition:                 initialCfg.TUIStatusPosition,
2755		TUIWorkingPosition:                initialCfg.TUIWorkingPosition,
2756		ThemeName:                         initialCfg.Theme,
2757		EffectiveThemeName:                themePreference.Effective,
2758		ThemeForced:                       themePreference.Forced,
2759		TerminalProfile:                   theme.Terminal,
2760		FlatTools:                         initialCfg.FlatToolRender(),
2761		CompactUser:                       initialCfg.CompactUserInput(),
2762		ExtensionThemes:                   extMgr.ThemeOptions,
2763		AutoSubagentsSystemAddendum:       ProactiveSubagentsSystemAddendumFor(subagentActionsAllowed(args)),
2764		OnDemandSubagentsSystemAddendum:   OnDemandSubagentsSystemAddendum,
2765		SubagentsSystemAddendum:           subagentsAddendum,
2766		SettingsStore:                     configSettingsStore{},
2767		Model:                             r.Model,
2768		Provider:                          r.Provider,
2769		AuthMethod:                        r.AuthMethod,
2770		BaseURL:                           r.BaseURL,
2771		Reasoning:                         r.Reasoning,
2772		OnReasoningChanged:                runtime.SetReasoning,
2773		OnFastModeChanged:                 runtime.SetFastMode,
2774		SystemPrompt:                      r.SystemPrompt,
2775		WritingGuidance:                   WritingGuidance(),
2776		Tools:                             r.ToolRegistry,
2777		MaxSteps:                          r.MaxSteps,
2778		CWD:                               r.CWD,
2779		StartupAgentName:                  args.AgentName,
2780		StartupContextPaths:               instructionContextPaths(r.ContextFiles),
2781		StartupExtensionNames:             startupExtensionNames(extMgr.All()),
2782		StartupExtensionErrors:            append(startupExtensionErrors, args.mcpStartupErrors...),
2783		StartupSkillNames:                 startupSkillNames(startupSkills),
2784		ShowInstructionsAtStartup:         initialCfg.ShowInstructionsAtStartup,
2785		ZutHome:                           ZutHome(),
2786		SessionsRoot:                      agentSessionsRoot(ZutHome(), args),
2787		SessionsDisabled:                  args.NoSess,
2788		Version:                           version,
2789		UpdateInfoChan:                    updateCh,
2790		Sandbox:                           sharedSandbox,
2791		Agent:                             ag,
2792		InitialCompactHandoff:             currentCompactHandoff(),
2793		InitialSessionTitle: func() string {
2794			if sess == nil || sessionTitlePending {
2795				return ""
2796			}
2797			return sess.Title
2798		}(),
2799		InitialSessionTitlePending: sessionTitlePending,
2800		InitialInput:               args.Prompt,
2801		StartupPre:                 args.StartupPre,
2802		OnStartupPreDone: func() {
2803			// entry.pre often installs skills/extensions; rediscover them
2804			// before the user starts a model turn.
2805			current := liveInteractiveAgent(iv, ag)
2806			if extMgr != nil {
2807				_ = extMgr.Reload(context.Background(), 2*time.Second)
2808			}
2809			if current != nil {
2810				if _, err := refreshAgentToolsAndPrompt(args, sharedSandbox, extToolAdapter, current, prepareInteractiveRegistryWithScheduler, iv); err != nil {
2811					iv.ReportError(err)
2812				}
2813			}
2814		},
2815		RefreshTools: func() error {
2816			current := liveInteractiveAgent(iv, ag)
2817			webSearchPolicy, err := refreshAgentToolsAndPrompt(args, sharedSandbox, extToolAdapter, current, prepareInteractiveRegistryWithScheduler, iv)
2818			if err != nil {
2819				if !errors.Is(err, errInteractiveAgentChanged) {
2820					setWebSearchAvailable(false)
2821				}
2822				return err
2823			}
2824			setWebSearchAvailable(webSearchPolicy == subagents.WebSearchAllow)
2825			return nil
2826		},
2827		SetWebSearchAvailable:       setWebSearchAvailable,
2828		AuthManager:                 mgr,
2829		LlamaCPPConfig:              ResolveLlamaCPPConfig,
2830		RefreshLlamaCPPModels:       RefreshLlamaCPPModels,
2831		RefreshCustomProviderModels: RefreshCustomProviderModels,
2832		CustomDiscoveryConfigured:   CustomDiscoveryConfigured,
2833		BuildAgent:                  buildAgent,
2834		SetKimiCLIFallbackDisabled:  SetKimiCLIFallbackDisabled,
2835		BuildAgentFor:               buildAgentFor,
2836		BuildAgentForRescue:         buildAgentForRescue,
2837		LoggedInProviders:           modelPickerProviders,
2838		LoadSession:                 loadSession,
2839		LoadSessionContext:          loadSessionContext,
2840		ChangeCWD:                   changeCWD,
2841		PersistCompactHandoff:       persistCompactHandoff,
2842		CurrentCompactHandoff:       currentCompactHandoff,
2843		PersistGoal:                 persistGoal,
2844		PersistGoalRuntime:          persistGoalRuntime,
2845		CurrentGoal:                 currentGoal,
2846		CurrentGoalHistory:          currentGoalHistory,
2847		CurrentPlan:                 currentPlan,
2848		PersistPlan:                 persistPlan,
2849		PersistedPlan:               persistedPlan,
2850		InitialPlanSnapshots:        initialPlanSnapshots,
2851		EnsureMission:               ensureMission,
2852		MCPStatus:                   func() string { return mcpStatusText(args) },
2853		CurrentSessionPath: func() string {
2854			persistMu.Lock()
2855			defer persistMu.Unlock()
2856			if sess == nil {
2857				return ""
2858			}
2859			return sess.Path
2860		},
2861		FlushSession: func() {
2862			sessionTransitionMu.RLock()
2863			defer sessionTransitionMu.RUnlock()
2864			// Append any not-yet-persisted agent messages to the
2865			// current session file, then advance the baseline so
2866			// the final WriteNewTranscript at exit doesn't write
2867			// duplicates. Per-message persistence keeps the on-
2868			// disk file current already, so this is mostly a
2869			// defensive flush — still needed for /session export
2870			// to guarantee the exported bytes include the very
2871			// last in-flight turn.
2872			currentAg := iv.Agent()
2873			if currentAg == nil {
2874				return
2875			}
2876			persistMu.Lock()
2877			defer persistMu.Unlock()
2878			if sess == nil {
2879				return
2880			}
2881			next, flushErr := writeNewTranscriptLocked(currentAg, sess, sessBaselineMsgs)
2882			sessBaselineMsgs = next
2883			if flushErr != nil {
2884				iv.ReportError(fmt.Errorf("flush session: %w", flushErr))
2885				return
2886			}
2887		},
2888		SessionTransition: newSessionTransition(&sessionTransitionMu),
2889		Extensions:        extMgr,
2890		ResidentManager:   runtime.ResidentManager(),
2891		SpawnResident: func(ctx context.Context, request tools.ResidentSpawnRequest) (string, error) {
2892			manager := runtime.ResidentManager()
2893			if manager == nil {
2894				return "", errors.New("resident runtime is unavailable")
2895			}
2896			catalogue := r.ToolRegistry
2897			if iv != nil && iv.Agent() != nil {
2898				catalogue = iv.Agent().ToolsSnapshot()
2899			}
2900			spec, err := runtime.buildResidentChildSpec(ctx, request, catalogue)
2901			if err != nil {
2902				return "", err
2903			}
2904			if _, err := manager.Spawn(ctx, spec, request.Task); err != nil {
2905				return "", err
2906			}
2907			return spec.ID, nil
2908		},
2909		BuildResidentSpec: func(ctx context.Context, request tools.ResidentSpawnRequest) (subagents.ResidentChildSpec, error) {
2910			catalogue := r.ToolRegistry
2911			if iv != nil && iv.Agent() != nil {
2912				catalogue = iv.Agent().ToolsSnapshot()
2913			}
2914			return runtime.buildResidentChildSpec(ctx, request, catalogue)
2915		},
2916		ResolveSubagent: resolveSubagent,
2917		ChangelogChan:   changelogCh,
2918		OnChangelogDismiss: func() {
2919			// For development builds, store the actual release version
2920			// so the same changelog doesn't show again next launch.
2921			// For real builds, store the binary version.
2922			v := version
2923			if devBuild {
2924				if iv != nil && iv.ChangelogVersion() != "" {
2925					v = iv.ChangelogVersion()
2926				}
2927			}
2928			_ = MarkChangelogShown(v)
2929		},
2930		SkillSnapshot: func() []*skills.Skill {
2931			return userSkillSnapshot(args, r.CWD, extMgr.Skills())
2932		},
2933		LoadSkillPins: func(cwd string) (skills.Pins, error) {
2934			if args.NoSkill {
2935				return skills.Pins{}, nil
2936			}
2937			return loadSkillPins(cwd)
2938		},
2939		ToggleSkillPin:      toggleSkillPin,
2940		PreloadPinnedSkills: freshSession && !args.NoSkill,
2941		NoYolo:              args.NoYolo,
2942		ConfirmGate:         confirmGate,
2943		PersistModel: newPersistModelCallback(&persistMu, &sess, &activeProvider, &activeModel, func(message string) {
2944			if iv != nil {
2945				iv.Notify("session", "error", message)
2946			}
2947		}),
2948		CurrentSessionTitle: func() string {
2949			sessionTransitionMu.RLock()
2950			defer sessionTransitionMu.RUnlock()
2951			persistMu.Lock()
2952			defer persistMu.Unlock()
2953			if sess == nil || sessionTitlePending {
2954				return ""
2955			}
2956			return sess.Title
2957		},
2958		CurrentSessionTitlePending: func() bool {
2959			sessionTransitionMu.RLock()
2960			defer sessionTransitionMu.RUnlock()
2961			persistMu.Lock()
2962			defer persistMu.Unlock()
2963			return sessionTitlePending
2964		},
2965		PersistTitle: func(title string) error {
2966			persistMu.Lock()
2967			defer persistMu.Unlock()
2968			if sess == nil {
2969				return nil
2970			}
2971			if err := sess.UpdateTitle(title); err != nil {
2972				return err
2973			}
2974			sessionTitlePending = false
2975			return nil
2976		},
2977		OnSessionTitleChanged: func(title string) {
2978			sessionTransitionMu.RLock()
2979			defer sessionTransitionMu.RUnlock()
2980			persistMu.Lock()
2981			defer persistMu.Unlock()
2982			if sess != nil {
2983				sess.Title = title
2984				sessionTitlePending = false
2985			}
2986		},
2987	})
2988	extHooks.attachInteractive(iv)
2989	schedulerCtx, cancelScheduler := context.WithCancel(ctx)
2990	defer cancelScheduler()
2991	go func() {
2992		if err := scheduleEngine.Run(schedulerCtx, func(taskCtx context.Context, task scheduler.Task) error {
2993			unlockSession := lockScheduledSession(task.SessionID)
2994			defer unlockSession()
2995			// Keep a background session run mutually exclusive with /session
2996			// transitions. The active target uses Interactive's distinct-turn
2997			// ingress instead, so it never injects into an in-flight model loop.
2998			sessionTransitionMu.RLock()
2999			defer sessionTransitionMu.RUnlock()
3000			persistMu.Lock()
3001			activeTarget := sess != nil && sess.ID == task.SessionID
3002			persistMu.Unlock()
3003			if activeTarget {
3004				return iv.SubmitFollowUp(taskCtx, task.Message)
3005			}
3006			// A resumed background agent must schedule any follow-up work against
3007			// the transcript it resumed, not whichever session is visible in the
3008			// interactive TUI while it runs.
3009			backgroundRegistry := func(registry core.Registry) core.Registry {
3010				if schedulerAllowed {
3011					registry[tools.ScheduleToolName] = &tools.ScheduleTool{
3012						Engine:    scheduleEngine,
3013						SessionID: func() string { return task.SessionID },
3014						Location:  func() *time.Location { return time.Local },
3015					}
3016				}
3017				return registry
3018			}
3019			err := runScheduledSession(taskCtx, task, args, r, extMgr, backgroundRegistry)
3020			if err != nil {
3021				iv.Notify("scheduler", "error", fmt.Sprintf("scheduled task %s failed: %v", task.ID, err))
3022			} else {
3023				iv.Notify("scheduler", "success", fmt.Sprintf("scheduled task %s completed in session %s", task.ID, task.SessionID))
3024			}
3025			return err
3026		}); err != nil && schedulerCtx.Err() == nil {
3027			iv.ReportError(fmt.Errorf("scheduler stopped: %w", err))
3028		}
3029	}()
3030	go func() {
3031		<-reloadDone
3032		for _, reloadErr := range reloadErrs {
3033			if reloadErr != nil {
3034				iv.ReportError(fmt.Errorf("reload subagents: %w", reloadErr))
3035			}
3036		}
3037	}()
3038
3039	// Bind the interactive TUI as the Confirmer. We deferred this
3040	// until now because the gate is constructed before the TUI
3041	// (the BeforeToolExecute closure captures it). SetConfirmer
3042	// is mutex-guarded on the gate so this is safe.
3043	if confirmGate != nil {
3044		confirmGate.SetConfirmer(&confirmationEventConfirmer{
3045			inner: iv,
3046			emit:  extMgr.EmitEvent,
3047		})
3048	}
3049
3050	// Signal-driven flush: a SIGTERM / SIGHUP to the zut process
3051	// (closed terminal window, system shutdown, kill) used to lose
3052	// the entire in-memory transcript because the deferred post-Run
3053	// flush below never ran. Per-message persistence above covers
3054	// most of it; this handler writes any in-flight remainder and
3055	// then exits the process so we don't double-paint over a
3056	// broken terminal that the TUI's restore deferreds can no
3057	// longer fix from a signal context.
3058	//
3059	// SIGINT is intentionally NOT handled here — the TUI consumes
3060	// Ctrl+C as a regular key event for cancel/clear semantics, and
3061	// installing a SIGINT notifier here would swallow it.
3062	sigCh := make(chan os.Signal, 1)
3063	signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGHUP)
3064	defer signal.Stop(sigCh)
3065	go func() {
3066		_, ok := <-sigCh
3067		if !ok {
3068			return
3069		}
3070		finalAg := iv.Agent()
3071		if finalAg != nil {
3072			persistMu.Lock()
3073			if sess != nil {
3074				next, flushErr := writeNewTranscriptLocked(finalAg, sess, sessBaselineMsgs)
3075				sessBaselineMsgs = next
3076				if flushErr != nil {
3077					fmt.Fprintf(os.Stderr, "flush session: %v\n", flushErr)
3078				}
3079				if closeErr := sess.Close(); closeErr != nil {
3080					fmt.Fprintf(os.Stderr, "close session: %v\n", closeErr)
3081				}
3082				sess = nil
3083			}
3084			persistMu.Unlock()
3085		}
3086		closeAgentLSP(finalAg)
3087		closeResolvedLSP(r)
3088		// Stop supervised workers before exiting. os.Exit skips deferred
3089		// cleanup, so wait for reload and close the shared runtime explicitly.
3090		<-reloadDone
3091		_ = closeSubagentRuntimeFresh(runtime)
3092		// MCP servers run in their own process groups; stop them too.
3093		if args.mcpManager != nil {
3094			_ = args.mcpManager.Close()
3095		}
3096		// Exit cleanly. Re-raising the signal would skip os.Exit's
3097		// at-exit hooks; explicit exit is fine because we've already
3098		// flushed the only at-risk state (the session file) and stopped
3099		// supervised workers.
3100		os.Exit(0)
3101	}()
3102
3103	runErr = iv.Run(ctx)
3104
3105	// Flush final transcript to session (only if we had / ended up with an agent).
3106	if finalAg := iv.Agent(); finalAg != nil {
3107		persistMu.Lock()
3108		if sess != nil {
3109			next, flushErr := writeNewTranscriptLocked(finalAg, sess, sessBaselineMsgs)
3110			sessBaselineMsgs = next
3111			if flushErr != nil {
3112				runErr = errors.Join(runErr, fmt.Errorf("flush session: %w", flushErr))
3113			}
3114		}
3115		persistMu.Unlock()
3116	}
3117	return runErr
3118}
3119
3120func resumableSessionID(root string, sess *core.Session, hasMessages bool) string {
3121	if !hasMessages || sess == nil || sess.ID == "" || sess.Path == "" {
3122		return ""
3123	}
3124	managedRoot, err := filepath.Abs(filepath.Join(root, "sessions"))
3125	if err != nil {
3126		return ""
3127	}
3128	path, err := filepath.Abs(sess.Path)
3129	if err != nil {
3130		return ""
3131	}
3132	rel, err := filepath.Rel(managedRoot, path)
3133	if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
3134		return ""
3135	}
3136	if _, err := os.Stat(path); err != nil {
3137		return ""
3138	}
3139	return sess.ID
3140}
3141
3142func resumeSessionHint(id string) string {
3143	return fmt.Sprintf("Session ID: %s\nResume with: zut --resume %s\n\n", id, id)
3144}
3145
3146func agentSessionsRoot(root string, args Args) string {
3147	if args.AgentName == "" {
3148		return root
3149	}
3150	return filepath.Join(root, "sessions", "agents", safeAgentName(args.AgentName))
3151}
3152
3153// openOrCreateSession returns a session for the run. sess may be nil
3154// with a nil error if session persistence is disabled.
3155func openOrCreateSession(ctx context.Context, args Args, r Resolved, ag *core.Agent, version string) (*core.Session, error) {
3156	sess, _, err := openOrCreateSessionState(ctx, args, r, ag, version)
3157	return sess, err
3158}
3159
3160func pickSession(root, cwd string) (string, error) {
3161	files := core.ListSessions(root, cwd)
3162	if len(files) == 0 {
3163		fmt.Fprintln(os.Stderr, "no sessions for", cwd)
3164		return "", nil
3165	}
3166	for i, f := range files {
3167		fmt.Fprintf(os.Stderr, "  %2d) %s\n", i+1, f)
3168	}
3169	fmt.Fprint(os.Stderr, "pick #: ")
3170	rd := bufio.NewReader(os.Stdin)
3171	line, _ := rd.ReadString('\n')
3172	line = strings.TrimSpace(line)
3173	var n int
3174	if _, err := fmt.Sscanf(line, "%d", &n); err != nil || n < 1 || n > len(files) {
3175		return "", fmt.Errorf("invalid selection")
3176	}
3177	return files[n-1], nil
3178}
3179
3180// WriteNewTranscript appends only messages after index `from` from the
3181// agent's transcript to the session. Used by callers that don't hold
3182// the persistMu (non-interactive print/json modes which run a single
3183// turn under their own goroutine).
3184func WriteNewTranscript(ag *core.Agent, sess *core.Session, from int) error {
3185	_, err := writeNewTranscriptLocked(ag, sess, from)
3186	return err
3187}
3188
3189func wireRetryLifecyclePersistence(ag *core.Agent, sess *core.Session) {
3190	if ag == nil {
3191		return
3192	}
3193	if sess == nil {
3194		ag.OnRetryLifecycle = nil
3195		return
3196	}
3197	ag.OnRetryLifecycle = sess.RecordRetryLifecycle
3198}
3199
3200// writeNewTranscriptLocked is the same as WriteNewTranscript. The
3201// suffix marks that interactive callers must hold persistMu when
3202// invoking it so concurrent appends from the agent loop don't race
3203// with this catch-up flush.
3204func writeNewTranscriptLocked(ag *core.Agent, sess *core.Session, from int) (next int, err error) {
3205	next = from
3206	if sess == nil || ag == nil {
3207		return next, nil
3208	}
3209	msgs, total := ag.MessagesFrom(from)
3210	for offset, msg := range msgs {
3211		index := from + offset
3212		if err := sess.AppendMessage(msg); err != nil {
3213			return next, fmt.Errorf("append transcript message %d: %w", index, err)
3214		}
3215		next = index + 1
3216	}
3217	if len(msgs) == 0 {
3218		next = total
3219	}
3220	cum := ag.Cost()
3221	if err := sess.AppendUsage(cum, cum); err != nil {
3222		return next, fmt.Errorf("append transcript usage: %w", err)
3223	}
3224	if err := sess.Flush(); err != nil {
3225		return next, fmt.Errorf("flush transcript: %w", err)
3226	}
3227	return next, nil
3228}
3229
3230func argsForStdin(rawArgs []string, mode os.FileMode) []string {
3231	if mode&os.ModeCharDevice != 0 {
3232		return rawArgs
3233	}
3234	return append([]string{"--print"}, rawArgs...)
3235}
3236
3237func combinePromptInput(stdin, positional string) string {
3238	parts := make([]string, 0, 2)
3239	if stdin = strings.TrimSpace(stdin); stdin != "" {
3240		parts = append(parts, stdin)
3241	}
3242	if positional = strings.TrimSpace(positional); positional != "" {
3243		parts = append(parts, positional)
3244	}
3245	return strings.Join(parts, "\n")
3246}
3247
3248func readAllStdin() (string, error) {
3249	return readAllPipe(os.Stdin)
3250}
3251
3252// readAllPipe reads f to EOF unless it is a terminal.
3253func readAllPipe(f *os.File) (string, error) {
3254	fi, err := f.Stat()
3255	if err != nil {
3256		return "", err
3257	}
3258	if (fi.Mode() & os.ModeCharDevice) != 0 {
3259		return "", nil
3260	}
3261	return readAllStdinFrom(f)
3262}
3263
3264// readAllStdinContext is readAllStdin that returns ctx.Err() when ctx ends
3265// first. The abandoned read finishes or dies with the process.
3266func readAllStdinContext(ctx context.Context) (string, error) {
3267	type result struct {
3268		text string
3269		err  error
3270	}
3271	done := make(chan result, 1)
3272	stdin := os.Stdin
3273	go func() {
3274		text, err := readAllPipe(stdin)
3275		done <- result{text, err}
3276	}()
3277	select {
3278	case r := <-done:
3279		return r.text, r.err
3280	case <-ctx.Done():
3281		return "", ctx.Err()
3282	}
3283}
3284
3285func readAllStdinFrom(r io.Reader) (string, error) {
3286	b, err := io.ReadAll(r)
3287	return string(b), err
3288}
3289
3290func printModels() {
3291	models := provider.Active()
3292
3293	// Compute column widths from actual data so wide providers (e.g.
3294	// xiaomi-token-plan-sgp) and long bedrock model ids don't force the
3295	// `name` column off-screen. Floors mirror the historical layout so
3296	// short catalogs look the same as before.
3297	provW, idW, srcW := len("provider"), len("model id"), len("source")
3298	for _, m := range models {
3299		if w := len(m.Provider); w > provW {
3300			provW = w
3301		}
3302		if w := len(m.ID); w > idW {
3303			idW = w
3304		}
3305		source := m.Source
3306		if source == "" {
3307			source = "catalog"
3308		}
3309		if m.Speculative {
3310			source = "speculative"
3311		}
3312		if w := len(source); w > srcW {
3313			srcW = w
3314		}
3315	}
3316
3317	header := fmt.Sprintf("%-*s  %-*s  %8s  %8s  %s  %-*s  %s",
3318		provW, "provider",
3319		idW, "model id",
3320		"context", "max-out", "reasoning",
3321		srcW, "source",
3322		"name")
3323	fmt.Println(header)
3324
3325	for _, m := range models {
3326		reason := " "
3327		if m.Reasoning {
3328			reason = "✓"
3329		}
3330		source := m.Source
3331		if source == "" {
3332			source = "catalog"
3333		}
3334		if m.Speculative {
3335			source = "speculative"
3336		}
3337		fmt.Printf("%-*s  %-*s  %8d  %8d     %s      %-*s  %s\n",
3338			provW, m.Provider,
3339			idW, m.ID,
3340			m.ContextWindow, m.MaxOutput,
3341			reason,
3342			srcW, source,
3343			m.DisplayName)
3344	}
3345}
3346
3347// confirmationKeyFor returns the session-scoped confirmation grant key for a
3348// tool call. A tool that implements core.ConfirmationKeyer may expose a finer
3349// key than its name (the subagent facade keys one grant per action); every
3350// other tool, and any unknown tool, keeps its own name. It mirrors the
3351// confirmation gate's lookup so a remembered grant matches the call that will
3352// actually run.
3353func confirmationKeyFor(reg core.Registry, name string, args json.RawMessage) string {
3354	tool, err := reg.Get(name)
3355	if err != nil {
3356		return name
3357	}
3358	return core.ConfirmationKey(tool, name, args)
3359}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close