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 "strings"
14 "sync"
15 "syscall"
16 "time"
17
18 "github.com/patriceckhart/zot/packages/agent/extensions"
19 "github.com/patriceckhart/zot/packages/agent/extproto"
20 "github.com/patriceckhart/zot/packages/agent/modes"
21 "github.com/patriceckhart/zot/packages/agent/skills"
22 "github.com/patriceckhart/zot/packages/agent/swarm"
23 "github.com/patriceckhart/zot/packages/agent/tools"
24 "github.com/patriceckhart/zot/packages/core"
25 "github.com/patriceckhart/zot/packages/provider"
26 "github.com/patriceckhart/zot/packages/provider/auth"
27 "github.com/patriceckhart/zot/packages/tui"
28)
29
30// interactiveExtHooks is a tiny adapter that lets the extension
31// manager call back into the Interactive instance built later in
32// runInteractive. The forward-declared *modes.Interactive is filled
33// in immediately after manager construction.
34type interactiveExtHooks struct {
35 ivPtr **modes.Interactive
36}
37
38func (h *interactiveExtHooks) iv() *modes.Interactive {
39 if h == nil || h.ivPtr == nil {
40 return nil
41 }
42 return *h.ivPtr
43}
44
45func (h *interactiveExtHooks) Notify(extName, level, message string) {
46 if iv := h.iv(); iv != nil {
47 iv.Notify(extName, level, message)
48 }
49}
50func (h *interactiveExtHooks) Submit(text string) {
51 if iv := h.iv(); iv != nil {
52 iv.SubmitOrQueue(text, nil)
53 }
54}
55func (h *interactiveExtHooks) SubmitSlash(text string) {
56 if iv := h.iv(); iv != nil {
57 iv.SubmitSlash(text)
58 }
59}
60func (h *interactiveExtHooks) Insert(text string) {
61 if iv := h.iv(); iv != nil {
62 iv.Insert(text)
63 }
64}
65func (h *interactiveExtHooks) Display(extName, text string) {
66 if iv := h.iv(); iv != nil {
67 iv.Display(extName, text)
68 }
69}
70func (h *interactiveExtHooks) ClearNotes(extName string) {
71 if iv := h.iv(); iv != nil {
72 iv.ClearNotes(extName)
73 }
74}
75func (h *interactiveExtHooks) OpenPanel(extName string, spec extproto.PanelSpec) {
76 if iv := h.iv(); iv != nil {
77 iv.OpenPanel(extName, spec)
78 }
79}
80func (h *interactiveExtHooks) UpdatePanel(extName, panelID, title string, lines []string, footer string) {
81 if iv := h.iv(); iv != nil {
82 iv.UpdatePanel(extName, panelID, title, lines, footer)
83 }
84}
85func (h *interactiveExtHooks) ClosePanel(extName, panelID string) {
86 if iv := h.iv(); iv != nil {
87 iv.ClosePanel(extName, panelID)
88 }
89}
90
91// extToolAdapter bridges *extensions.Manager to the
92// ExtensionToolSource interface declared in build.go (kept narrow to
93// avoid a build->extensions import cycle). One adapter instance per
94// run; used at every Resolve point so re-built agents pick up the
95// same set of extension tools.
96type extToolAdapter struct {
97 mgr *extensions.Manager
98}
99
100func (a *extToolAdapter) Tools() []ExtensionToolInfo {
101 infos := a.mgr.Tools()
102 out := make([]ExtensionToolInfo, len(infos))
103 for i, t := range infos {
104 out[i] = ExtensionToolInfo{
105 Extension: t.Extension,
106 Name: t.Name,
107 Description: t.Description,
108 Schema: t.Schema,
109 Deferred: t.Deferred,
110 }
111 }
112 return out
113}
114
115func (a *extToolAdapter) NewExtensionTool(info ExtensionToolInfo) core.Tool {
116 return extensions.NewTool(a.mgr, extensions.ToolInfo{
117 Extension: info.Extension,
118 Name: info.Name,
119 Description: info.Description,
120 Schema: info.Schema,
121 Deferred: info.Deferred,
122 })
123}
124
125// liveInteractiveAgent returns the agent currently owned by the TUI. The
126// fallback is only used before the Interactive has been constructed.
127//
128// Agent rebuilds, such as cross-provider model switches, replace the TUI's
129// agent without replacing the startup pointer held by runInteractive. Session
130// operations must therefore resolve the live agent at the time of the action.
131func liveInteractiveAgent(iv *modes.Interactive, fallback *core.Agent) *core.Agent {
132 if iv != nil {
133 return iv.Agent()
134 }
135 return fallback
136}
137
138func trimMessagesForResume(msgs []provider.Message, keepTail int) []provider.Message {
139 if keepTail <= 0 || len(msgs) <= keepTail {
140 return provider.RepairOrphanedToolResults(msgs)
141 }
142 var out []provider.Message
143 start := len(msgs) - keepTail
144 // Preserve the synthetic compaction summary when present so an
145 // already-compacted session stays compacted after resume.
146 if len(msgs) > 0 && msgs[0].Meta["compaction"] == "true" && start > 1 {
147 out = append(out, msgs[0])
148 }
149 // Avoid hydrating a tail that starts with orphan tool_result rows;
150 // provider APIs require those to be paired with an earlier tool_use.
151 for start < len(msgs) && msgs[start].Role == provider.RoleTool {
152 start++
153 }
154 out = append(out, msgs[start:]...)
155 return provider.RepairOrphanedToolResults(out)
156}
157
158// fanoutAgentEvent translates a core.AgentEvent into the wire-format
159// EventFromHost and pushes it through the extension manager. Only
160// the events that have a clear extension-facing meaning are
161// forwarded; internal-only ones (text_delta, tool_progress) are
162// dropped to keep the per-extension stream sane.
163func fanoutAgentEvent(mgr *extensions.Manager, ev core.AgentEvent) {
164 if mgr == nil {
165 return
166 }
167 switch e := ev.(type) {
168 case core.EvTurnStart:
169 mgr.EmitEvent(extproto.EventFromHost{Event: "turn_start", Step: e.Step})
170 case core.EvToolCall:
171 mgr.EmitEvent(extproto.EventFromHost{
172 Event: "tool_call", ToolID: e.ID, ToolName: e.Name, ToolArgs: e.Args,
173 })
174 case core.EvAssistantMessage:
175 // Concat the visible text portions of the message; binary
176 // blocks (tool_use, etc.) are skipped because subscribers
177 // usually want a string they can grep / display.
178 var text string
179 for _, c := range e.Message.Content {
180 if tb, ok := c.(provider.TextBlock); ok {
181 text += tb.Text
182 }
183 }
184 mgr.EmitEvent(extproto.EventFromHost{Event: "assistant_message", Text: text})
185 case core.EvTurnEnd:
186 ev := extproto.EventFromHost{Event: "turn_end", Stop: string(e.Stop)}
187 if e.Err != nil {
188 ev.Error = e.Err.Error()
189 }
190 mgr.EmitEvent(ev)
191 }
192}
193
194// Run is the top-level entrypoint for the zot binary.
195func Run(rawArgs []string, version string) error {
196 // Apply network configuration before any subcommand can make an HTTP
197 // request. Standard proxy environment variables retain precedence.
198 applyConfiguredHTTPProxy()
199
200 // Subcommand router: `zot bot ...` is handled separately so the
201 // generic flag parser doesn't reject "bot" as a positional arg.
202 if handled, err := runBotCommand(rawArgs, version); handled {
203 return err
204 }
205 if handled, err := runExtCommand(rawArgs, version); handled {
206 return err
207 }
208 if handled, err := runUpdateCommand(rawArgs, version); handled {
209 return err
210 }
211 if handled, err := runSessionsCommand(rawArgs); handled {
212 return err
213 }
214 if handled, err := runZotfileCommand(rawArgs, version); handled {
215 return err
216 }
217 return runWithArgsRaw(rawArgs, version)
218}
219
220func runWithArgsRaw(rawArgs []string, version string) error {
221 // `zot rpc` is shorthand for `zot --rpc` so third-party apps can
222 // spawn the binary with a clean argv. Strip the leading 'rpc'
223 // token and let the rest flow through the normal arg parser.
224 if len(rawArgs) > 0 && rawArgs[0] == "rpc" {
225 rawArgs = append([]string{"--rpc"}, rawArgs[1:]...)
226 }
227
228 if fi, err := os.Stdin.Stat(); err == nil {
229 rawArgs = argsForStdin(rawArgs, fi.Mode())
230 }
231 args, err := ParseArgs(rawArgs)
232 if err != nil {
233 PrintHelp(version)
234 return err
235 }
236 if args.Help {
237 printHelp(os.Stdout, version)
238 return nil
239 }
240 if args.Version {
241 fmt.Println("zot", version)
242 return nil
243 }
244 // Model catalog: load any cached discovery data before we inspect
245 // the model list (list-models, print/json, interactive).
246 prepareRuntimeCatalog()
247
248 if args.ListModels {
249 printModels()
250 return nil
251 }
252
253 return runWithArgs(args, version)
254}
255
256func runWithArgs(args Args, version string) error {
257 ctx := context.Background()
258 switch args.Mode {
259 case ModePrint:
260 return runPrintMode(ctx, args, version)
261 case ModeStream:
262 return runStreamMode(ctx, args, version)
263 case ModeJSON:
264 return runJSONMode(ctx, args, version)
265 case ModeRPC:
266 return runRPCMode(ctx, args, version)
267 case ModeSwarmAgent:
268 return runSwarmAgentMode(ctx, args, version)
269 default:
270 return runInteractive(ctx, args, version)
271 }
272}
273
274// ---- print / json modes: require credentials, run single-shot ----
275
276// nonInteractiveExtHooks is the HostHooks impl used by print / json
277// modes. They have no TUI, so notify / display go to stderr and
278// submit / insert are no-ops (the extension can't steer a
279// single-shot run once it's in flight anyway).
280type nonInteractiveExtHooks struct{}
281
282func (nonInteractiveExtHooks) Notify(ext, level, message string) {
283 fmt.Fprintf(os.Stderr, "[%s] %s: %s\n", ext, level, message)
284}
285func (nonInteractiveExtHooks) Submit(string) {}
286func (nonInteractiveExtHooks) SubmitSlash(string) {}
287func (nonInteractiveExtHooks) Insert(string) {}
288func (nonInteractiveExtHooks) Display(string, string) {}
289func (nonInteractiveExtHooks) ClearNotes(string) {}
290func (nonInteractiveExtHooks) OpenPanel(string, extproto.PanelSpec) {}
291func (nonInteractiveExtHooks) UpdatePanel(string, string, string, []string, string) {}
292func (nonInteractiveExtHooks) ClosePanel(string, string) {}
293
294// setupNonInteractiveExtensions loads --ext paths and (unless
295// --no-ext) runs discovery. Returns the manager so the caller can
296// wire tools into the resolved registry, and a cleanup closure to
297// defer. Mirrors the interactive-mode setup minus the TUI hooks.
298func setupNonInteractiveExtensions(ctx context.Context, args Args, r *Resolved, version string) (*extensions.Manager, func()) {
299 reportSkillDiagnostics(os.Stderr, r.SkillDiagnostics)
300 extMgr := extensions.New(ZotHome(), r.CWD, version, r.Provider, r.Model, nonInteractiveExtHooks{})
301 for _, e := range extMgr.LoadExplicit(ctx, args.Exts) {
302 fmt.Fprintln(os.Stderr, "extension load:", e)
303 }
304 if !args.NoExt {
305 for _, e := range extMgr.Discover(ctx) {
306 fmt.Fprintln(os.Stderr, "extension load:", e)
307 }
308 }
309 extMgr.WaitForReady(3 * time.Second)
310 r.MergeExtensionTools(&extToolAdapter{mgr: extMgr})
311 extMgr.EmitEvent(extproto.EventFromHost{Event: "session_start"})
312 return extMgr, func() { extMgr.Stop(2 * time.Second) }
313}
314
315func reportSkillDiagnostics(w io.Writer, diagnostics []string) {
316 for _, diagnostic := range diagnostics {
317 fmt.Fprintln(w, "skill discovery:", diagnostic)
318 }
319}
320
321// wireNonInteractiveAgentExtHooks installs the same BeforeToolExecute
322// / BeforeTurn / BeforeAssistantMessage / OnEvent hooks the
323// interactive path wires up, so extensions get their normal
324// event-intercept surface in print / json / rpc flows too.
325func wireNonInteractiveAgentExtHooks(ctx context.Context, ag *core.Agent, extMgr *extensions.Manager) {
326 if ag == nil || extMgr == nil {
327 return
328 }
329 ag.BeforeToolExecute = func(call provider.ToolCallBlock) (bool, string, json.RawMessage) {
330 res := extMgr.InterceptToolCall(ctx, call.ID, call.Name, call.Arguments)
331 if res.Block {
332 return false, res.Reason, nil
333 }
334 return true, "", res.ModifiedArgs
335 }
336 ag.BeforeTurn = func(step int) (bool, string) {
337 res := extMgr.InterceptTurnStart(ctx, step)
338 return !res.Block, res.Reason
339 }
340 ag.BeforeAssistantMessage = func(text string) (bool, string, string) {
341 res := extMgr.InterceptAssistantMessage(ctx, text)
342 if res.Block {
343 return false, res.Reason, ""
344 }
345 return true, "", res.ReplaceText
346 }
347 ag.OnEvent = func(ev core.AgentEvent) { fanoutAgentEvent(extMgr, ev) }
348}
349
350type printStats struct {
351 Provider string `json:"provider"`
352 Model string `json:"model"`
353 PromptTokens int `json:"prompt_tokens"`
354 ReasoningTokens *int `json:"reasoning_tokens"`
355 GeneratedOutputTokens int `json:"generated_output_tokens"`
356 ElapsedMS int64 `json:"elapsed_ms"`
357}
358
359func writePrintStats(path, providerID, model string, usage provider.Usage, elapsed time.Duration) error {
360 var reasoning *int
361 generated := usage.OutputTokens
362 if usage.ReasoningTokensKnown {
363 count := usage.ReasoningTokens
364 reasoning = &count
365 generated -= count
366 if generated < 0 {
367 generated = 0
368 }
369 }
370 stats := printStats{
371 Provider: providerID,
372 Model: model,
373 PromptTokens: usage.InputTokens + usage.CacheReadTokens + usage.CacheWriteTokens,
374 ReasoningTokens: reasoning,
375 GeneratedOutputTokens: generated,
376 ElapsedMS: elapsed.Milliseconds(),
377 }
378 data, err := json.MarshalIndent(stats, "", " ")
379 if err != nil {
380 return fmt.Errorf("encode stats: %w", err)
381 }
382 data = append(data, '\n')
383 if err := os.WriteFile(path, data, 0o644); err != nil {
384 return fmt.Errorf("write stats %q: %w", path, err)
385 }
386 return nil
387}
388
389func runPrintMode(ctx context.Context, args Args, version string) error {
390 if args.NoYolo {
391 fmt.Fprintln(os.Stderr, "warning: --no-yolo has no effect in print mode (no interactive prompt available); tools will run without confirmation")
392 }
393 r, err := Resolve(args, true)
394 if err != nil {
395 return err
396 }
397 extMgr, stopExt := setupNonInteractiveExtensions(ctx, args, &r, version)
398 defer stopExt()
399
400 ag := r.NewAgent()
401 wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
402 sess, _ := openOrCreateSession(args, r, ag, version)
403 defer sess.Close()
404
405 piped, _ := readAllStdin()
406 prompt := combinePromptInput(piped, args.Prompt)
407 if prompt == "" {
408 return fmt.Errorf("print mode requires a prompt (arg or stdin)")
409 }
410
411 start := len(ag.Messages())
412 if err := runZotfileStartupPre(ctx, args.StartupPre, r.CWD, r.Sandbox, ag, nil, os.Stderr); err != nil {
413 return err
414 }
415 reloadResourcesAfterStartupPre(ctx, args, extMgr, r.Sandbox, ag)
416 started := time.Now()
417 usage, err := modes.RunPrint(ctx, ag, prompt, nil, os.Stdout)
418 elapsed := time.Since(started)
419 WriteNewTranscript(ag, sess, start)
420 if err != nil {
421 return err
422 }
423 if args.StatsPath != "" {
424 return writePrintStats(args.StatsPath, r.Provider, r.Model, usage, elapsed)
425 }
426 return nil
427}
428
429func runStreamMode(ctx context.Context, args Args, version string) error {
430 if args.NoYolo {
431 fmt.Fprintln(os.Stderr, "warning: --no-yolo has no effect in stream mode (no interactive prompt available); tools will run without confirmation")
432 }
433 r, err := Resolve(args, true)
434 if err != nil {
435 return err
436 }
437 extMgr, stopExt := setupNonInteractiveExtensions(ctx, args, &r, version)
438 defer stopExt()
439
440 ag := r.NewAgent()
441 wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
442 sess, _ := openOrCreateSession(args, r, ag, version)
443 defer sess.Close()
444
445 piped, _ := readAllStdin()
446 prompt := combinePromptInput(piped, args.Prompt)
447 if prompt == "" {
448 return fmt.Errorf("stream mode requires a prompt (arg or stdin)")
449 }
450
451 start := len(ag.Messages())
452 preSink, finishPre := newStreamTextSink(os.Stdout)
453 if err := runZotfileStartupPre(ctx, args.StartupPre, r.CWD, r.Sandbox, ag, preSink, os.Stderr); err != nil {
454 finishPre()
455 return err
456 }
457 finishPre()
458 reloadResourcesAfterStartupPre(ctx, args, extMgr, r.Sandbox, ag)
459 err = modes.RunStream(ctx, ag, prompt, nil, os.Stdout)
460 WriteNewTranscript(ag, sess, start)
461 return err
462}
463
464func newStreamTextSink(out io.Writer) (func(core.AgentEvent), func()) {
465 var streamed, wroteText, lastWasNL bool
466 writeText := func(text string) {
467 if text == "" {
468 return
469 }
470 _, _ = fmt.Fprint(out, text)
471 if syncer, ok := out.(interface{ Sync() error }); ok {
472 _ = syncer.Sync()
473 }
474 wroteText = true
475 lastWasNL = strings.HasSuffix(text, "\n")
476 }
477 sink := func(ev core.AgentEvent) {
478 switch e := ev.(type) {
479 case core.EvAssistantStart:
480 streamed = false
481 case core.EvTextDelta:
482 streamed = true
483 writeText(e.Delta)
484 case core.EvAssistantMessage:
485 if streamed {
486 return
487 }
488 var text strings.Builder
489 for _, content := range e.Message.Content {
490 if block, ok := content.(provider.TextBlock); ok {
491 if text.Len() > 0 {
492 text.WriteString("\n")
493 }
494 text.WriteString(block.Text)
495 }
496 }
497 writeText(text.String())
498 }
499 }
500 finish := func() {
501 if wroteText && !lastWasNL {
502 writeText("\n")
503 }
504 }
505 return sink, finish
506}
507
508func runJSONMode(ctx context.Context, args Args, version string) error {
509 if args.NoYolo {
510 fmt.Fprintln(os.Stderr, "warning: --no-yolo has no effect in json mode (no interactive prompt available); tools will run without confirmation")
511 }
512 r, err := Resolve(args, true)
513 if err != nil {
514 return err
515 }
516 extMgr, stopExt := setupNonInteractiveExtensions(ctx, args, &r, version)
517 defer stopExt()
518
519 ag := r.NewAgent()
520 wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
521 sess, _ := openOrCreateSession(args, r, ag, version)
522 defer sess.Close()
523
524 piped, _ := readAllStdin()
525 prompt := combinePromptInput(piped, args.Prompt)
526 if prompt == "" {
527 return fmt.Errorf("json mode requires a prompt (arg or stdin)")
528 }
529
530 start := len(ag.Messages())
531 enc := json.NewEncoder(os.Stdout)
532 preSink := func(ev core.AgentEvent) {
533 _ = enc.Encode(modes.EventToJSON(ev))
534 }
535 if err := runZotfileStartupPre(ctx, args.StartupPre, r.CWD, r.Sandbox, ag, preSink, os.Stderr); err != nil {
536 _ = enc.Encode(map[string]any{"type": "error", "message": err.Error()})
537 return err
538 }
539 reloadResourcesAfterStartupPre(ctx, args, extMgr, r.Sandbox, ag)
540 err = modes.RunJSON(ctx, ag, prompt, nil, os.Stdout)
541 WriteNewTranscript(ag, sess, start)
542 return err
543}
544
545// runZotfileStartupPre runs entry.pre before the main non-interactive prompt.
546// "!command" values execute via BashTool; other values are sent as an agent turn.
547// shellOut receives live shell chunks (typically os.Stderr); sink receives
548// agent events for plain-text pre (stream mode wires a live text sink).
549func runZotfileStartupPre(ctx context.Context, pre, cwd string, sandbox *tools.Sandbox, ag *core.Agent, sink func(core.AgentEvent), shellOut io.Writer) error {
550 pre = strings.TrimSpace(pre)
551 if pre == "" {
552 return nil
553 }
554 if cmd, ok := modes.ShellEscapeCommand(pre); ok {
555 raw, err := json.Marshal(map[string]any{"command": cmd})
556 if err != nil {
557 return err
558 }
559 if shellOut != nil {
560 fmt.Fprintf(shellOut, "$ %s\n", cmd)
561 if f, ok := shellOut.(interface{ Sync() error }); ok {
562 _ = f.Sync()
563 }
564 }
565 progress := func(chunk string) {
566 if shellOut == nil || chunk == "" {
567 return
568 }
569 fmt.Fprint(shellOut, chunk)
570 if f, ok := shellOut.(interface{ Sync() error }); ok {
571 _ = f.Sync()
572 }
573 }
574 bash := &tools.BashTool{CWD: cwd, Sandbox: sandbox}
575 res, err := bash.Execute(ctx, raw, progress)
576 if err != nil {
577 return fmt.Errorf("zotfile entry.pre: %w", err)
578 }
579 if res.IsError {
580 var sb strings.Builder
581 for _, c := range res.Content {
582 if tb, ok := c.(provider.TextBlock); ok {
583 sb.WriteString(tb.Text)
584 }
585 }
586 msg := strings.TrimSpace(sb.String())
587 if msg == "" {
588 msg = "command failed"
589 }
590 return fmt.Errorf("zotfile entry.pre: %s", msg)
591 }
592 return nil
593 }
594 if ag == nil {
595 return fmt.Errorf("zotfile entry.pre requires an agent for non-shell prompts")
596 }
597 if sink == nil {
598 sink = func(core.AgentEvent) {}
599 }
600 return ag.Prompt(ctx, pre, nil, sink)
601}
602
603// refreshAgentToolsAndPrompt re-resolves tools (including rediscovered
604// skills and currently loaded extension tools) and updates the live
605// agent's registry and system prompt. Used after /reload-ext and after
606// zotfile entry.pre installs new skills or extensions.
607// mutateRegistry, if non-nil, can inject session-specific tools (e.g. swarm_spawn).
608func refreshAgentToolsAndPrompt(args Args, sharedSandbox *tools.Sandbox, extToolAdapter ExtensionToolSource, ag *core.Agent, mutateRegistry func(core.Registry) core.Registry) {
609 if ag == nil {
610 return
611 }
612 resolved, err := Resolve(args, true)
613 if err != nil {
614 return
615 }
616 if sharedSandbox != nil {
617 resolved.UseSandbox(sharedSandbox)
618 }
619 if extToolAdapter != nil {
620 resolved.MergeExtensionTools(extToolAdapter)
621 }
622 reg := resolved.ToolRegistry
623 if mutateRegistry != nil {
624 reg = mutateRegistry(reg)
625 }
626 ag.SetTools(reg)
627 ag.System = resolved.SystemPrompt
628}
629
630// reloadResourcesAfterStartupPre reloads extensions (if any) and
631// refreshes the agent tool registry + system prompt so skills/extensions
632// installed by entry.pre are visible to the following turn.
633func reloadResourcesAfterStartupPre(ctx context.Context, args Args, extMgr *extensions.Manager, sharedSandbox *tools.Sandbox, ag *core.Agent) {
634 if strings.TrimSpace(args.StartupPre) == "" || ag == nil {
635 return
636 }
637 adapter := &extToolAdapter{mgr: extMgr}
638 if extMgr != nil {
639 _ = extMgr.Reload(ctx, 2*time.Second)
640 }
641 refreshAgentToolsAndPrompt(args, sharedSandbox, adapter, ag, nil)
642}
643
644// ---- interactive mode: opens the TUI even without credentials ----
645
646func runInteractive(ctx context.Context, args Args, version string) error {
647 // Resolve WITHOUT requiring credentials.
648 r, err := Resolve(args, false)
649 if err != nil {
650 return err
651 }
652
653 authStore := AuthStoreFor()
654 mgr := auth.NewManager(authStore)
655 defer mgr.Close()
656
657 // Keep the sandbox pointer stable across agent rebuilds (login / model
658 // switch). The Interactive UI toggles the lock via this pointer, and
659 // rebuilt tool instances must share the same one so the lock sticks.
660 sharedSandbox := r.Sandbox
661
662 // Build the extension manager BEFORE the agent so we can fold
663 // extension-defined tools into the registry. Forward-declare iv so
664 // the host hooks adapter can dereference it after construction.
665 var iv *modes.Interactive
666 extHooks := &interactiveExtHooks{ivPtr: &iv}
667 extMgr := extensions.New(ZotHome(), r.CWD, version, r.Provider, r.Model, extHooks)
668 startupExtensionErrors := make([]string, 0, len(r.SkillDiagnostics))
669 for _, diagnostic := range r.SkillDiagnostics {
670 startupExtensionErrors = append(startupExtensionErrors, "skill discovery: "+diagnostic)
671 }
672 // --ext paths first so they win against installed extensions of
673 // the same name (loadOne's first-write-wins semantics).
674 for _, e := range extMgr.LoadExplicit(ctx, args.Exts) {
675 startupExtensionErrors = append(startupExtensionErrors, e.Error())
676 }
677 // --no-ext skips the global + project-local discovery scan;
678 // explicit --ext paths above are still honoured so you can run
679 // "only this extension" with --no-ext --ext ./x.
680 if !args.NoExt {
681 for _, e := range extMgr.Discover(ctx) {
682 startupExtensionErrors = append(startupExtensionErrors, e.Error())
683 }
684 }
685 // Wait briefly for extensions to flush their initial register_tool
686 // frames before we build the agent's tool registry. Half a second
687 // is plenty for any extension that's actually well-behaved; ones
688 // that don't send a ready frame eat the full grace and proceed.
689 // 3s is the per-extension grace period for the ready frame.
690 // Native binaries are instant; runtimes like `npx tsx` take ~1.5s
691 // from cold cache. The wait is tight only for extensions that
692 // haven't sent ready by then; ones that signalled earlier release
693 // the wait immediately.
694 extMgr.WaitForReady(3 * time.Second)
695 defer extMgr.Stop(2 * time.Second)
696
697 extToolAdapter := &extToolAdapter{mgr: extMgr}
698 r.MergeExtensionTools(extToolAdapter)
699
700 // Build the swarm supervisor BEFORE the agent so the auto-swarm
701 // tool can reference it during tool-registry construction. State
702 // lives under ZotHome/swarm so per-agent meta/events survive
703 // restarts; the user can hunt orphaned agents down with
704 // `git worktree list` if anything misbehaves.
705 //
706 // swarmMgr is also captured by loadSession / changeCWD closures
707 // further down the function, which is why we keep the variable
708 // in this outer scope rather than scoping it tighter.
709 var swarmMgr *swarm.Swarm
710 swarmMgr = swarm.New(swarm.Config{
711 Root: filepath.Join(ZotHome(), "swarm"),
712 RepoRoot: r.CWD,
713 ResolveCredential: func(ctx context.Context, providerID string) (swarm.Credential, error) {
714 if providerID == "ollama" {
715 return swarm.Credential{Value: "ollama", Method: "apikey"}, nil
716 }
717 credential, method, accountID, err := ResolveCredentialFullContext(ctx, providerID, "")
718 if err != nil {
719 // Providers backed only by a local endpoint do not need a key.
720 // Leave stdin untouched and let the child resolve that endpoint.
721 if !CredentialAvailable(providerID) {
722 return swarm.Credential{}, nil
723 }
724 return swarm.Credential{}, err
725 }
726 return swarm.Credential{Value: credential, Method: method, AccountID: accountID}, nil
727 },
728 })
729 // Pull any previously-spawned agents off disk so the dashboard
730 // shows them as detached and the user can resume / remove them.
731 _, _ = swarmMgr.Reload()
732
733 // onSpawnedSwarm is the OnSpawned callback the swarm_spawn tool
734 // fires after every successful spawn. It hands the agent off to
735 // the running Interactive so the watcher can flush a summary back
736 // into chat when all sub-agents finish. Reads `iv` lazily because
737 // the Interactive is constructed after the agent.
738 onSpawnedSwarm := func(a *swarm.Agent, task string) {
739 if iv != nil {
740 iv.TrackSwarmAgent(a, task)
741 }
742 }
743
744 // Inject the swarm_spawn auto-swarm tool only when /settings ->
745 // auto-swarm is currently enabled. Registering it unconditionally
746 // leaves the model trying to call it (and getting a polite error)
747 // even when the user has switched the feature off. The /settings
748 // toggle live-mutates the running agent's registry separately so
749 // flipping the flag mid-session takes effect on the next turn.
750 injectSwarmSpawn := func(reg core.Registry) core.Registry {
751 if reg == nil {
752 return reg
753 }
754 if !AutoSwarmEnabled() {
755 return reg
756 }
757 reg["swarm_spawn"] = &tools.SwarmSpawnTool{
758 Swarm: swarmMgr,
759 Enabled: AutoSwarmEnabled,
760 DefaultModel: func() string { return r.Model },
761 DefaultProvider: func() string { return r.Provider },
762 OnSpawned: onSpawnedSwarm,
763 }
764 return reg
765 }
766 injectSwarmSpawn(r.ToolRegistry)
767
768 // Confirmation gate: when --no-yolo is on, the agent must ask
769 // the user before every tool call. In interactive mode the TUI
770 // provides the Confirmer; in print/json/rpc modes there's no
771 // way to prompt, so the gate is constructed with a nil inner
772 // which auto-refuses every call with a helpful reason.
773 var confirmGate *core.ConfirmGate
774 if args.NoYolo {
775 confirmGate = core.NewConfirmGate(nil) // set below for interactive
776 }
777
778 // Capture current args in a closure so BuildAgent can re-resolve
779 // after a successful login (picks up the newly stored credential).
780 wireAgentExt := func(a *core.Agent) *core.Agent {
781 if a == nil {
782 return a
783 }
784 a.BeforeToolExecute = func(call provider.ToolCallBlock) (bool, string, json.RawMessage) {
785 // Guards run before confirmation because they may rewrite the
786 // arguments. The user must approve the effective call that will
787 // actually execute, not the model's original arguments.
788 r := extMgr.InterceptToolCall(ctx, call.ID, call.Name, call.Arguments)
789 if r.Block {
790 return false, r.Reason, nil
791 }
792 effectiveArgs := call.Arguments
793 if len(r.ModifiedArgs) > 0 && json.Valid(r.ModifiedArgs) {
794 effectiveArgs = r.ModifiedArgs
795 }
796 if confirmGate != nil {
797 var content strings.Builder
798 if tool, err := a.Tools.Get(call.Name); err == nil {
799 if previewer, ok := tool.(core.ToolPreviewer); ok {
800 preview, err := previewer.Preview(ctx, effectiveArgs)
801 if err != nil {
802 return false, err.Error(), nil
803 }
804 for _, block := range preview.Content {
805 if text, ok := block.(provider.TextBlock); ok {
806 if content.Len() > 0 {
807 content.WriteString("\n")
808 }
809 content.WriteString(text.Text)
810 }
811 }
812 }
813 }
814 ok, reason, _ := confirmGate.CheckToolCall(core.ToolCallConfirmation{
815 ID: call.ID,
816 Name: call.Name,
817 Summary: core.BuildPreview(effectiveArgs, 120),
818 Content: content.String(),
819 })
820 if !ok {
821 return false, reason, nil
822 }
823 }
824 return true, "", r.ModifiedArgs
825 }
826 a.BeforeTurn = func(step int) (bool, string) {
827 r := extMgr.InterceptTurnStart(ctx, step)
828 return !r.Block, r.Reason
829 }
830 a.BeforeAssistantMessage = func(text string) (bool, string, string) {
831 r := extMgr.InterceptAssistantMessage(ctx, text)
832 if r.Block {
833 return false, r.Reason, ""
834 }
835 return true, "", r.ReplaceText
836 }
837 a.OnEvent = func(ev core.AgentEvent) { fanoutAgentEvent(extMgr, ev) }
838 return a
839 }
840
841 buildAgent := func() (*core.Agent, string, string, error) {
842 resolved, err := Resolve(args, true)
843 if err != nil {
844 return nil, "", "", err
845 }
846 resolved.UseSandbox(sharedSandbox)
847 resolved.MergeExtensionTools(extToolAdapter)
848 injectSwarmSpawn(resolved.ToolRegistry)
849 return wireAgentExt(resolved.NewAgent()), resolved.Provider, resolved.Model, nil
850 }
851
852 // Rebuild agent with an explicit provider/model override.
853 buildAgentFor := func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
854 next := args
855 if providerOverride != "" {
856 next.Provider = providerOverride
857 }
858 if modelOverride != "" {
859 next.Model = modelOverride
860 }
861 resolved, err := Resolve(next, true)
862 if err != nil {
863 return nil, "", "", err
864 }
865 resolved.UseSandbox(sharedSandbox)
866 resolved.MergeExtensionTools(extToolAdapter)
867 injectSwarmSpawn(resolved.ToolRegistry)
868 return wireAgentExt(resolved.NewAgent()), resolved.Provider, resolved.Model, nil
869 }
870
871 // Rebuild agent for the rescue picker after a recoverable failure.
872 // Unlike buildAgentFor, this drops launch-time --api-key and
873 // --base-url overrides because those are typically the cause of the
874 // rescue (a bad key, a typo'd base URL, or a corporate gateway that
875 // only the originally-picked provider needed). Re-resolving without
876 // them lets the rescue retry use env vars / auth.json / provider
877 // defaults the way zot would have without the overrides.
878 buildAgentForRescue := func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
879 next := args
880 next.APIKey = ""
881 next.BaseURL = ""
882 if providerOverride != "" {
883 next.Provider = providerOverride
884 }
885 if modelOverride != "" {
886 next.Model = modelOverride
887 }
888 resolved, err := Resolve(next, true)
889 if err != nil {
890 return nil, "", "", err
891 }
892 resolved.UseSandbox(sharedSandbox)
893 resolved.MergeExtensionTools(extToolAdapter)
894 injectSwarmSpawn(resolved.ToolRegistry)
895 return wireAgentExt(resolved.NewAgent()), resolved.Provider, resolved.Model, nil
896 }
897
898 var ag *core.Agent
899 if r.HasCredential() {
900 ag = wireAgentExt(r.NewAgent())
901 }
902
903 // /reload-ext callback: after the manager has respawned every
904 // extension, re-resolve the tool registry (built-ins + freshly-
905 // registered extension tools) and swap it onto the current
906 // agent in-place. The current agent may have been replaced by a
907 // /model swap since spawn, so re-read the live `ag` on each
908 // invocation.
909 extMgr.SetOnReload(func() {
910 current := liveInteractiveAgent(iv, ag)
911 if current == nil {
912 return
913 }
914 refreshAgentToolsAndPrompt(args, sharedSandbox, extToolAdapter, current, injectSwarmSpawn)
915 })
916
917 // Fire session_start once we know the manager's running.
918 extMgr.EmitEvent(extproto.EventFromHost{Event: "session_start"})
919
920 var sess *core.Session
921 var sessBaselineMsgs int // messages already on disk when current session opened
922 // persistMu guards sess + sessBaselineMsgs against concurrent access
923 // from the agent loop's per-message persistence hook (runs on the
924 // agent goroutine) and the TUI's session swap / flush callbacks
925 // (run on the TUI goroutine). Without this, a /sessions swap that
926 // races with a finishing turn could double-write or lose messages.
927 var persistMu sync.Mutex
928 if !args.NoSess && ag != nil {
929 sess, _ = openOrCreateSession(args, r, ag, version)
930 if ag != nil {
931 sessBaselineMsgs = len(ag.Messages())
932 }
933 }
934 defer func() {
935 persistMu.Lock()
936 defer persistMu.Unlock()
937 if sess != nil {
938 sess.Close()
939 }
940 }()
941
942 // persistMessage is the per-message hook bound to the agent. It
943 // appends each new transcript message to the live session as soon
944 // as it lands, so a kill / closed terminal / OS crash costs at
945 // most the in-flight turn instead of the whole session. The
946 // baseline counter advances in lock-step so the exit-time flush
947 // doesn't double-write rows already on disk.
948 persistMessage := func(m provider.Message) {
949 persistMu.Lock()
950 defer persistMu.Unlock()
951 if sess == nil {
952 return
953 }
954 if err := sess.AppendMessage(m); err == nil {
955 sessBaselineMsgs++
956 }
957 }
958 persistUsage := func(cum provider.Usage) {
959 persistMu.Lock()
960 defer persistMu.Unlock()
961 if sess == nil {
962 return
963 }
964 _ = sess.AppendUsage(cum, cum)
965 }
966 persistCompaction := func(messages []provider.Message) {
967 persistMu.Lock()
968 defer persistMu.Unlock()
969 if sess == nil {
970 return
971 }
972 if err := sess.AppendCompaction(messages); err == nil {
973 sessBaselineMsgs = len(messages)
974 }
975 }
976 wireAgentPersist := func(a *core.Agent) *core.Agent {
977 if a == nil {
978 return a
979 }
980 a.OnMessageAppended = persistMessage
981 a.OnUsage = persistUsage
982 a.OnTranscriptCompacted = persistCompaction
983 return a
984 }
985 wireAgentPersist(ag)
986
987 // Re-wrap the build closures so any agent constructed by the TUI
988 // (login, /model swap to a different provider) also gets the
989 // persistence hooks. Without this, switching provider would
990 // silently revert to the old in-memory-only behaviour.
991 bindLiveAgentSession := func(a *core.Agent) {
992 persistMu.Lock()
993 s := sess
994 persistMu.Unlock()
995 bindAgentSession(a, s)
996 }
997 baseBuildAgent := buildAgent
998 buildAgent = func() (*core.Agent, string, string, error) {
999 a, p, m, err := baseBuildAgent()
1000 a = wireAgentPersist(a)
1001 bindLiveAgentSession(a)
1002 return a, p, m, err
1003 }
1004 baseBuildAgentFor := buildAgentFor
1005 buildAgentFor = func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
1006 a, p, m, err := baseBuildAgentFor(providerOverride, modelOverride)
1007 a = wireAgentPersist(a)
1008 bindLiveAgentSession(a)
1009 return a, p, m, err
1010 }
1011 baseBuildAgentForRescue := buildAgentForRescue
1012 buildAgentForRescue = func(providerOverride, modelOverride string) (*core.Agent, string, string, error) {
1013 a, p, m, err := baseBuildAgentForRescue(providerOverride, modelOverride)
1014 a = wireAgentPersist(a)
1015 bindLiveAgentSession(a)
1016 return a, p, m, err
1017 }
1018
1019 // loadSession replaces the current session with the one at path and
1020 // hands its messages to the agent. Used by the /sessions picker.
1021 loadSession := func(path string) error {
1022 currentAg := liveInteractiveAgent(iv, ag)
1023 if currentAg == nil {
1024 return fmt.Errorf("no agent running; log in first")
1025 }
1026 newSess, msgs, err := core.OpenSession(path)
1027 if err != nil {
1028 return err
1029 }
1030 fullMsgCount := len(msgs)
1031 msgs = trimMessagesForResume(msgs, 100)
1032 persistMu.Lock()
1033 // Flush any unsaved messages to the old session before swapping.
1034 // Per-message persistence keeps sessBaselineMsgs current, so
1035 // this is a defensive no-op in the common case; it still
1036 // matters for the rare race where a turn just finished and
1037 // hadn't fired its hook yet.
1038 if sess != nil {
1039 writeNewTranscriptLocked(currentAg, sess, sessBaselineMsgs)
1040 _ = sess.Close()
1041 }
1042 sess = newSess
1043 currentAg.SetMessages(msgs)
1044 bindAgentSession(currentAg, sess)
1045 if cum, last, uerr := core.SessionUsageDetail(path); uerr == nil {
1046 currentAg.SeedCost(cum)
1047 currentAg.SeedLastTurnUsage(last)
1048 }
1049 // The live agent only receives a compact resume window, but
1050 // the session file remains intact. Keep the persistence
1051 // baseline at the original on-disk message count so future
1052 // turns append after the full session instead of duplicating
1053 // the hydrated tail.
1054 sessBaselineMsgs = fullMsgCount
1055 persistMu.Unlock()
1056 // Re-scope the swarm dashboard to the new session so /swarm
1057 // only shows agents this session spawned. swarmMgr may be nil
1058 // here if we haven't reached the construction site yet (it
1059 // shouldn't be, since the interactive loop is what triggers
1060 // loadSession, but the nil check is cheap insurance).
1061 if swarmMgr != nil && newSess != nil {
1062 swarmMgr.SetActiveSession(newSess.ID)
1063 }
1064 return nil
1065 }
1066
1067 // changeCWD switches the running session to a new working directory.
1068 // Wired into InteractiveConfig.ChangeCWD and invoked by the hidden
1069 // /cd slash command (which itself is only fired by the workspaces
1070 // extension's panel-key Enter handler today; the user can type /cd
1071 // directly but it's not in autocomplete / help / the README).
1072 //
1073 // Steps, in order:
1074 // 1. resolve + validate the new path (~ expansion, abs/rel)
1075 // 2. close the current session, flushing pending messages
1076 // 3. mutate captured args.CWD + r.CWD so future buildAgent
1077 // calls bind to the new cwd
1078 // 4. re-root the shared sandbox («·«) so /jail follows the
1079 // session into the new cwd instead of widening or silently
1080 // dropping
1081 // 5. rebuild the agent via buildAgent() so tools, AGENTS.md
1082 // addendum, system prompt, sessions dir all bind correctly
1083 // 6. open a fresh session in the new cwd's bucket
1084 // 7. push the new state into the running Interactive
1085 // 8. re-scope the swarm dashboard to the freshly-opened session
1086 //
1087 // The /jail state is preserved verbatim: if the sandbox was locked
1088 // to the old cwd, it stays locked, just re-pointed at the new one.
1089 changeCWD := func(path string) error {
1090 if path == "" {
1091 return fmt.Errorf("empty path")
1092 }
1093 // ~ expansion.
1094 if path == "~" || strings.HasPrefix(path, "~/") {
1095 home, herr := os.UserHomeDir()
1096 if herr != nil || home == "" {
1097 return fmt.Errorf("cannot expand ~: %v", herr)
1098 }
1099 if path == "~" {
1100 path = home
1101 } else {
1102 path = filepath.Join(home, path[2:])
1103 }
1104 }
1105 if !filepath.IsAbs(path) {
1106 path = filepath.Join(args.CWD, path)
1107 }
1108 absPath, err := filepath.Abs(path)
1109 if err != nil {
1110 return err
1111 }
1112 info, err := os.Stat(absPath)
1113 if err != nil {
1114 return err
1115 }
1116 if !info.IsDir() {
1117 return fmt.Errorf("not a directory: %s", absPath)
1118 }
1119
1120 currentAg := ag
1121 if currentAg == nil {
1122 return fmt.Errorf("no agent running; log in first")
1123 }
1124
1125 // Close the current session before we drop the reference.
1126 // Per-message persistence keeps it current already; this is
1127 // a defensive flush + final fsync via Close.
1128 persistMu.Lock()
1129 if sess != nil {
1130 writeNewTranscriptLocked(currentAg, sess, sessBaselineMsgs)
1131 _ = sess.Close()
1132 sess = nil
1133 }
1134 sessBaselineMsgs = 0
1135 persistMu.Unlock()
1136
1137 // Mutate captured state so subsequent agent rebuilds and
1138 // session opens see the new cwd.
1139 wasJailed := sharedSandbox != nil && sharedSandbox.Locked()
1140 args.CWD = absPath
1141 r.CWD = absPath
1142 if args.PermissionSet != nil {
1143 expanded := args.PermissionSet.Expand(absPath, args.AgentDataDir)
1144 args.PermissionSet = &expanded
1145 }
1146 if sharedSandbox != nil {
1147 sharedSandbox.Root = absPath
1148 if wasJailed {
1149 sharedSandbox.Lock()
1150 } else {
1151 sharedSandbox.Unlock()
1152 }
1153 }
1154
1155 // Rebuild the agent so tools / AGENTS.md / system prompt
1156 // re-bind to the new cwd. buildAgent() reads from args + r.
1157 newAg, newProvider, newModel, berr := buildAgent()
1158 if berr != nil {
1159 return fmt.Errorf("rebuild agent: %v", berr)
1160 }
1161 ag = newAg
1162
1163 // Fresh session in the new cwd's bucket. We bypass
1164 // openOrCreateSession's --continue / --resume branches
1165 // because /cd's semantics are "start fresh here", matching
1166 // what relaunching `zot --cwd <path>` would do today.
1167 if !args.NoSess {
1168 newRoot := agentSessionsRoot(ZotHome(), args)
1169 core.PruneEmptySessions(newRoot, absPath)
1170 newSess, serr := core.NewSession(newRoot, absPath, newProvider, newModel, version)
1171 if serr != nil {
1172 return fmt.Errorf("open session in %s: %v", absPath, serr)
1173 }
1174 persistMu.Lock()
1175 sess = newSess
1176 sessBaselineMsgs = 0
1177 persistMu.Unlock()
1178 bindAgentSession(newAg, newSess)
1179 }
1180
1181 // Push the new state into the running Interactive.
1182 if iv != nil {
1183 startupPaths := instructionContextPaths(loadAgentsContext(absPath, ZotHome()))
1184 iv.ApplyChangedCWDWithStartupContext(newAg, newProvider, newModel, absPath, startupPaths)
1185 }
1186
1187 // Re-scope the swarm dashboard to the new session.
1188 if swarmMgr != nil && sess != nil {
1189 swarmMgr.SetActiveSession(sess.ID)
1190 }
1191 return nil
1192 }
1193
1194 term := tui.NewProcTerm()
1195
1196 // Kick off the async update check so the banner can appear when the
1197 // http response eventually arrives (usually <1s on cached DNS). Map
1198 // agent.UpdateInfo -> modes.UpdateInfo here to avoid a cyclic import.
1199 updateCh := make(chan modes.UpdateInfo, 1)
1200 go func() {
1201 defer close(updateCh)
1202 src := <-CheckForUpdateAsync(ZotHome(), version)
1203 updateCh <- modes.UpdateInfo{
1204 Current: src.Current,
1205 Latest: src.Latest,
1206 Available: src.Available,
1207 URL: src.URL,
1208 }
1209 }()
1210
1211 // Changelog: when the running version differs from the last
1212 // version whose release notes the user dismissed, fetch the
1213 // release body from GitHub and have the TUI show it once. On
1214 // first-ever launch (no prior LastChangelogShown), seed the
1215 // stored version silently — don't dump release notes at someone
1216 // who just installed.
1217 changelogCh := make(chan modes.ChangelogPayload, 1)
1218 go func() {
1219 defer close(changelogCh)
1220 cfg, _ := LoadConfig()
1221 if cfg.LastChangelogShown == "" {
1222 SeedChangelogVersion(version)
1223 return
1224 }
1225 if !ShouldShowChangelog(version, cfg) {
1226 return
1227 }
1228 info := <-FetchChangelogAsync(version)
1229 if info.Body == "" {
1230 return
1231 }
1232 // For dev builds (0.0.0), skip if the latest release was
1233 // already shown (stored by the dismiss callback).
1234 if version == "0.0.0" && info.Version == cfg.LastChangelogShown {
1235 return
1236 }
1237 changelogCh <- modes.ChangelogPayload{
1238 Version: info.Version,
1239 Body: info.Body,
1240 URL: info.URL,
1241 }
1242 }()
1243
1244 initialCfg, _ := LoadConfig()
1245 quickModelShortcuts := make([]modes.QuickModelShortcut, len(initialCfg.QuickModelShortcuts))
1246 for idx, s := range initialCfg.QuickModelShortcuts {
1247 quickModelShortcuts[idx] = modes.QuickModelShortcut{Provider: s.Provider, Model: s.Model}
1248 }
1249 theme, _, themeErr := tui.DetectThemeWithCustom(ZotHome(), initialCfg.Theme, 80*time.Millisecond)
1250 if themeErr != nil {
1251 fmt.Fprintln(os.Stderr, "theme load:", themeErr)
1252 if initialCfg.Theme != "" && !tui.ThemeExists(ZotHome(), initialCfg.Theme) {
1253 initialCfg.Theme = ""
1254 _ = SaveConfig(initialCfg)
1255 }
1256 }
1257
1258 // swarmMgr was constructed and reloaded earlier (before the agent
1259 // build, so the auto-swarm tool could capture it). Here we just
1260 // scope the dashboard to the active host session so /swarm only
1261 // shows agents this session spawned (and any pre-upgrade unscoped
1262 // agents — see SnapshotAll docs). Updated again whenever the
1263 // user swaps sessions via loadSession below.
1264 if sess != nil {
1265 swarmMgr.SetActiveSession(sess.ID)
1266 }
1267 // Best-effort shutdown on interactive exit: stop all running
1268 // agents so they don't outlive their parent zot.
1269 defer swarmMgr.StopAll()
1270
1271 var startupSkills []*skills.Skill
1272 if r.SkillTool != nil {
1273 startupSkills = r.SkillTool.Skills()
1274 }
1275
1276 iv = modes.NewInteractive(modes.InteractiveConfig{
1277 Terminal: term,
1278 Theme: theme,
1279 InlineImagesEnabled: initialCfg.InlineImagesEnabled,
1280 AutoSwarmEnabled: initialCfg.AutoSwarmEnabled,
1281 AutoCompactThreshold: initialCfg.AutoCompactThreshold,
1282 JailByDefault: initialCfg.JailByDefault,
1283 OpenRouterServerToolsEnabled: initialCfg.OpenRouterServerToolsEnabled,
1284 OpenRouterServerToolCallLimit: initialCfg.OpenRouterServerToolCallLimit,
1285 NoTools: args.NoTools,
1286 QuickModelShortcuts: quickModelShortcuts,
1287 RecursiveFileSuggest: initialCfg.RecursiveFileSuggest,
1288 RespectGitignore: initialCfg.RespectGitignore,
1289 CompactMode: initialCfg.CompactMode,
1290 TUIInputStyle: initialCfg.TUIInputStyle,
1291 TUIStatusPosition: initialCfg.TUIStatusPosition,
1292 TUIWorkingPosition: initialCfg.TUIWorkingPosition,
1293 ThemeName: initialCfg.Theme,
1294 FlatTools: initialCfg.FlatToolRender(),
1295 CompactUser: initialCfg.CompactUserInput(),
1296 ExtensionThemes: extMgr.ThemeOptions,
1297 AutoSwarmSystemAddendum: AutoSwarmSystemAddendum,
1298 SettingsStore: configSettingsStore{},
1299 Model: r.Model,
1300 Provider: r.Provider,
1301 AuthMethod: r.AuthMethod,
1302 BaseURL: r.BaseURL,
1303 Reasoning: r.Reasoning,
1304 SystemPrompt: r.SystemPrompt,
1305 Tools: r.ToolRegistry,
1306 MaxSteps: r.MaxSteps,
1307 CWD: r.CWD,
1308 StartupAgentName: args.AgentName,
1309 StartupContextPaths: instructionContextPaths(r.ContextFiles),
1310 StartupExtensionNames: startupExtensionNames(extMgr.All()),
1311 StartupExtensionErrors: startupExtensionErrors,
1312 StartupSkillNames: startupSkillNames(startupSkills),
1313 ShowInstructionsAtStartup: initialCfg.ShowInstructionsAtStartup,
1314 ZotHome: ZotHome(),
1315 SessionsRoot: agentSessionsRoot(ZotHome(), args),
1316 Version: version,
1317 UpdateInfoChan: updateCh,
1318 Sandbox: sharedSandbox,
1319 Agent: ag,
1320 InitialInput: args.Prompt,
1321 StartupPre: args.StartupPre,
1322 OnStartupPreDone: func() {
1323 // entry.pre often installs skills/extensions; rediscover them
1324 // before the user starts a model turn.
1325 current := liveInteractiveAgent(iv, ag)
1326 if extMgr != nil {
1327 _ = extMgr.Reload(context.Background(), 2*time.Second)
1328 }
1329 if current != nil {
1330 refreshAgentToolsAndPrompt(args, sharedSandbox, extToolAdapter, current, injectSwarmSpawn)
1331 }
1332 },
1333 AuthManager: mgr,
1334 LlamaCPPConfig: ResolveLlamaCPPConfig,
1335 RefreshLlamaCPPModels: RefreshLlamaCPPModels,
1336 BuildAgent: buildAgent,
1337 SetKimiCLIFallbackDisabled: SetKimiCLIFallbackDisabled,
1338 BuildAgentFor: buildAgentFor,
1339 BuildAgentForRescue: buildAgentForRescue,
1340 LoggedInProviders: func() []string {
1341 var out []string
1342 seen := map[string]bool{}
1343 for _, p := range knownProviders {
1344 if CredentialAvailable(p) && !seen[p] {
1345 out = append(out, p)
1346 seen[p] = true
1347 }
1348 }
1349 // Include custom providers that have credentials stored.
1350 for p := range provider.CustomProviders() {
1351 if CredentialAvailable(p) && !seen[p] {
1352 out = append(out, p)
1353 seen[p] = true
1354 }
1355 }
1356 // Ollama models are always available (no auth needed).
1357 if !seen["ollama"] {
1358 out = append(out, "ollama")
1359 }
1360 return out
1361 },
1362 LoadSession: loadSession,
1363 ChangeCWD: changeCWD,
1364 CurrentSessionPath: func() string {
1365 if sess == nil {
1366 return ""
1367 }
1368 return sess.Path
1369 },
1370 FlushSession: func() {
1371 // Append any not-yet-persisted agent messages to the
1372 // current session file, then advance the baseline so
1373 // the final WriteNewTranscript at exit doesn't write
1374 // duplicates. Per-message persistence keeps the on-
1375 // disk file current already, so this is mostly a
1376 // defensive flush — still needed for /session export
1377 // to guarantee the exported bytes include the very
1378 // last in-flight turn.
1379 currentAg := iv.Agent()
1380 if currentAg == nil {
1381 return
1382 }
1383 persistMu.Lock()
1384 defer persistMu.Unlock()
1385 if sess == nil {
1386 return
1387 }
1388 writeNewTranscriptLocked(currentAg, sess, sessBaselineMsgs)
1389 sessBaselineMsgs = len(currentAg.Messages())
1390 },
1391 Extensions: extMgr,
1392 Swarm: swarmMgr,
1393 ChangelogChan: changelogCh,
1394 OnChangelogDismiss: func() {
1395 // For dev builds (0.0.0) store the actual release version
1396 // so the same changelog doesn't show again next launch.
1397 // For real builds, store the binary version.
1398 v := version
1399 if v == "0.0.0" {
1400 if iv != nil && iv.ChangelogVersion() != "" {
1401 v = iv.ChangelogVersion()
1402 }
1403 }
1404 _ = MarkChangelogShown(v)
1405 },
1406 SkillSnapshot: func() []*skills.Skill {
1407 if args.NoSkill {
1408 // --no-skill: nothing for the picker to show.
1409 return nil
1410 }
1411 // Re-discover so the picker reflects edits made during
1412 // the session. Cheap; SKILL.md files are small. Filter
1413 // out built-in skills — they're hidden from user-facing
1414 // surfaces because they're implementation detail; the
1415 // model still sees them through the system-prompt
1416 // manifest and the skill tool.
1417 userHome, _ := os.UserHomeDir()
1418 var sources []skills.Source
1419 if args.WithSkills {
1420 sources = append(sources, skills.SearchSources(ZotHome(), r.CWD, userHome)...)
1421 }
1422 if !args.NoExt || len(args.Exts) > 0 {
1423 extSources, _ := extensions.PlanSkillSources(ZotHome(), r.CWD, args.Exts, !args.NoExt)
1424 sources = append(extSources, sources...)
1425 }
1426 list, _ := skills.DiscoverSources(sources, true)
1427 return skills.VisibleSkills(list)
1428 },
1429 NoYolo: args.NoYolo,
1430 ConfirmGate: confirmGate,
1431 PersistModel: func(providerName, model string) {
1432 // Update config.json so next launch uses the same pick.
1433 cfg, _ := LoadConfig()
1434 cfg.Provider = providerName
1435 cfg.Model = model
1436 _ = SaveConfig(cfg)
1437 // Update the active session's meta so resume picks this up.
1438 if sess != nil {
1439 _ = sess.UpdateModel(providerName, model)
1440 }
1441 },
1442 })
1443
1444 // Bind the interactive TUI as the Confirmer. We deferred this
1445 // until now because the gate is constructed before the TUI
1446 // (the BeforeToolExecute closure captures it). SetConfirmer
1447 // is mutex-guarded on the gate so this is safe.
1448 if confirmGate != nil {
1449 confirmGate.SetConfirmer(&confirmationEventConfirmer{
1450 inner: iv,
1451 emit: extMgr.EmitEvent,
1452 })
1453 }
1454
1455 // Signal-driven flush: a SIGTERM / SIGHUP to the zot process
1456 // (closed terminal window, system shutdown, kill) used to lose
1457 // the entire in-memory transcript because the deferred post-Run
1458 // flush below never ran. Per-message persistence above covers
1459 // most of it; this handler writes any in-flight remainder and
1460 // then exits the process so we don't double-paint over a
1461 // broken terminal that the TUI's restore deferreds can no
1462 // longer fix from a signal context.
1463 //
1464 // SIGINT is intentionally NOT handled here — the TUI consumes
1465 // Ctrl+C as a regular key event for cancel/clear semantics, and
1466 // installing a SIGINT notifier here would swallow it.
1467 sigCh := make(chan os.Signal, 1)
1468 signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGHUP)
1469 defer signal.Stop(sigCh)
1470 go func() {
1471 _, ok := <-sigCh
1472 if !ok {
1473 return
1474 }
1475 if finalAg := iv.Agent(); finalAg != nil {
1476 persistMu.Lock()
1477 if sess != nil {
1478 writeNewTranscriptLocked(finalAg, sess, sessBaselineMsgs)
1479 sessBaselineMsgs = len(finalAg.Messages())
1480 _ = sess.Close()
1481 sess = nil
1482 }
1483 persistMu.Unlock()
1484 }
1485 // Exit cleanly. Re-raising the signal would skip os.Exit's
1486 // at-exit hooks; explicit exit is fine because we've already
1487 // flushed the only at-risk state (the session file).
1488 os.Exit(0)
1489 }()
1490
1491 runErr := iv.Run(ctx)
1492
1493 // Flush final transcript to session (only if we had / ended up with an agent).
1494 if finalAg := iv.Agent(); finalAg != nil {
1495 persistMu.Lock()
1496 if sess != nil {
1497 writeNewTranscriptLocked(finalAg, sess, sessBaselineMsgs)
1498 sessBaselineMsgs = len(finalAg.Messages())
1499 }
1500 persistMu.Unlock()
1501 }
1502 return runErr
1503}
1504
1505func agentSessionsRoot(root string, args Args) string {
1506 if args.AgentName == "" {
1507 return root
1508 }
1509 return filepath.Join(root, "sessions", "agents", safeAgentName(args.AgentName))
1510}
1511
1512// openOrCreateSession returns a session for the run. sess may be nil
1513// with a nil error if session persistence is disabled. When a session
1514// exists, its id is bound onto ag so providers can sticky-route.
1515func openOrCreateSession(args Args, r Resolved, ag *core.Agent, version string) (*core.Session, error) {
1516 if args.NoSess {
1517 return nil, nil
1518 }
1519 // Sweep meta-only files left over from older zot versions (and from
1520 // any session that crashed before its first AppendMessage). Cheap;
1521 // reads the first few bytes of each file in the cwd's session dir.
1522 sessionsRoot := agentSessionsRoot(ZotHome(), args)
1523 core.PruneEmptySessions(sessionsRoot, args.CWD)
1524 var (
1525 s *core.Session
1526 msgs []provider.Message
1527 err error
1528 )
1529 switch {
1530 case args.Session != "":
1531 s, msgs, err = core.OpenSession(args.Session)
1532 // The swarm-agent child passes a fixed --session path that
1533 // may not exist yet on first Spawn. Treat ENOENT as "create
1534 // a fresh session AT THIS PATH" so the conversation actually
1535 // gets persisted; without this fallback the swarm child runs
1536 // with sess==nil and every Resume re-starts with no memory
1537 // of the prior turns. Other openers (--continue / --resume /
1538 // the picker) never see ENOENT here because they only choose
1539 // paths that already exist on disk.
1540 if err != nil && errors.Is(err, os.ErrNotExist) {
1541 s, err = core.NewSessionAtPath(args.Session, args.CWD, r.Provider, r.Model, version)
1542 msgs = nil
1543 }
1544 case args.Continue:
1545 latest := core.LatestSession(sessionsRoot, args.CWD)
1546 if latest != "" {
1547 s, msgs, err = core.OpenSession(latest)
1548 }
1549 case args.Resume:
1550 picked, perr := pickSession(sessionsRoot, args.CWD)
1551 if perr != nil {
1552 return nil, perr
1553 }
1554 if picked != "" {
1555 s, msgs, err = core.OpenSession(picked)
1556 }
1557 }
1558 if err != nil {
1559 return nil, err
1560 }
1561 if s == nil {
1562 s, err = core.NewSession(sessionsRoot, args.CWD, r.Provider, r.Model, version)
1563 if err != nil {
1564 return nil, err
1565 }
1566 } else {
1567 ag.SetMessages(msgs)
1568 if cum, last, uerr := core.SessionUsageDetail(s.Path); uerr == nil {
1569 ag.SeedCost(cum)
1570 ag.SeedLastTurnUsage(last)
1571 }
1572 }
1573 bindAgentSession(ag, s)
1574 return s, nil
1575}
1576
1577func pickSession(root, cwd string) (string, error) {
1578 files := core.ListSessions(root, cwd)
1579 if len(files) == 0 {
1580 fmt.Fprintln(os.Stderr, "no sessions for", cwd)
1581 return "", nil
1582 }
1583 for i, f := range files {
1584 fmt.Fprintf(os.Stderr, " %2d) %s\n", i+1, f)
1585 }
1586 fmt.Fprint(os.Stderr, "pick #: ")
1587 rd := bufio.NewReader(os.Stdin)
1588 line, _ := rd.ReadString('\n')
1589 line = strings.TrimSpace(line)
1590 var n int
1591 if _, err := fmt.Sscanf(line, "%d", &n); err != nil || n < 1 || n > len(files) {
1592 return "", fmt.Errorf("invalid selection")
1593 }
1594 return files[n-1], nil
1595}
1596
1597// WriteNewTranscript appends only messages after index `from` from the
1598// agent's transcript to the session. Used by callers that don't hold
1599// the persistMu (non-interactive print/json modes which run a single
1600// turn under their own goroutine).
1601func WriteNewTranscript(ag *core.Agent, sess *core.Session, from int) {
1602 writeNewTranscriptLocked(ag, sess, from)
1603}
1604
1605// bindAgentSession stamps the zot session id onto the agent so providers
1606// that support sticky routing (OpenRouter session_id) can reuse it.
1607// No-op when persistence is disabled.
1608func bindAgentSession(ag *core.Agent, sess *core.Session) {
1609 if ag == nil || sess == nil {
1610 return
1611 }
1612 ag.SessionID = sess.ID
1613}
1614
1615// writeNewTranscriptLocked is the same as WriteNewTranscript. The
1616// suffix marks that interactive callers must hold persistMu when
1617// invoking it so concurrent appends from the agent loop don't race
1618// with this catch-up flush.
1619func writeNewTranscriptLocked(ag *core.Agent, sess *core.Session, from int) {
1620 if sess == nil || ag == nil {
1621 return
1622 }
1623 msgs := ag.Messages()
1624 for i := from; i < len(msgs); i++ {
1625 _ = sess.AppendMessage(msgs[i])
1626 }
1627 cum := ag.Cost()
1628 _ = sess.AppendUsage(cum, cum)
1629}
1630
1631func argsForStdin(rawArgs []string, mode os.FileMode) []string {
1632 if mode&os.ModeCharDevice != 0 {
1633 return rawArgs
1634 }
1635 return append([]string{"--print"}, rawArgs...)
1636}
1637
1638func combinePromptInput(stdin, positional string) string {
1639 parts := make([]string, 0, 2)
1640 if stdin = strings.TrimSpace(stdin); stdin != "" {
1641 parts = append(parts, stdin)
1642 }
1643 if positional = strings.TrimSpace(positional); positional != "" {
1644 parts = append(parts, positional)
1645 }
1646 return strings.Join(parts, "\n")
1647}
1648
1649func readAllStdin() (string, error) {
1650 fi, err := os.Stdin.Stat()
1651 if err != nil {
1652 return "", err
1653 }
1654 if (fi.Mode() & os.ModeCharDevice) != 0 {
1655 return "", nil
1656 }
1657 b, err := io.ReadAll(os.Stdin)
1658 return string(b), err
1659}
1660
1661func printModels() {
1662 models := provider.Active()
1663
1664 // Compute column widths from actual data so wide providers (e.g.
1665 // xiaomi-token-plan-sgp) and long bedrock model ids don't force the
1666 // `name` column off-screen. Floors mirror the historical layout so
1667 // short catalogs look the same as before.
1668 provW, idW, srcW := len("provider"), len("model id"), len("source")
1669 for _, m := range models {
1670 if w := len(m.Provider); w > provW {
1671 provW = w
1672 }
1673 if w := len(m.ID); w > idW {
1674 idW = w
1675 }
1676 source := m.Source
1677 if source == "" {
1678 source = "catalog"
1679 }
1680 if m.Speculative {
1681 source = "speculative"
1682 }
1683 if w := len(source); w > srcW {
1684 srcW = w
1685 }
1686 }
1687
1688 header := fmt.Sprintf("%-*s %-*s %8s %8s %s %-*s %s",
1689 provW, "provider",
1690 idW, "model id",
1691 "context", "max-out", "reasoning",
1692 srcW, "source",
1693 "name")
1694 fmt.Println(header)
1695
1696 for _, m := range models {
1697 reason := " "
1698 if m.Reasoning {
1699 reason = "✓"
1700 }
1701 source := m.Source
1702 if source == "" {
1703 source = "catalog"
1704 }
1705 if m.Speculative {
1706 source = "speculative"
1707 }
1708 fmt.Printf("%-*s %-*s %8d %8d %s %-*s %s\n",
1709 provW, m.Provider,
1710 idW, m.ID,
1711 m.ContextWindow, m.MaxOutput,
1712 reason,
1713 srcW, source,
1714 m.DisplayName)
1715 }
1716}