v0.3.11 2c9dbfa

manager.go RawHistoryFolder

1// Package extensions implements the host side of zot's subprocess
2// extension protocol. The Manager discovers extensions in well-known
3// directories, spawns each one, completes the hello handshake, and
4// routes slash commands to the right extension.
5//
6// Each extension is its own process, communicating with zot over its
7// stdin/stdout in newline-delimited JSON. Stderr is redirected to a
8// per-extension log file under $ZOT_HOME/logs/. Crashing one
9// extension does not affect the others or the host.
10//
11// See docs/extensions.md for the user-facing reference and
12// packages/agent/extproto for the wire format.
13package extensions
14
15import (
16	"bufio"
17	"context"
18	"encoding/json"
19	"errors"
20	"fmt"
21	"io"
22	"os"
23	"os/exec"
24	"path/filepath"
25	"sort"
26	"strings"
27	"sync"
28	"syscall"
29	"time"
30
31	"github.com/patriceckhart/zot/packages/agent/extproto"
32	"github.com/patriceckhart/zot/packages/tui"
33)
34
35// Manifest is the extension.json file shipped alongside an
36// extension's executable. It tells zot how to launch the extension
37// and provides display metadata.
38type Manifest struct {
39	Name        string   `json:"name"`
40	Version     string   `json:"version,omitempty"`
41	Exec        string   `json:"exec"`               // executable path, relative to manifest dir
42	Args        []string `json:"args,omitempty"`     // extra argv passed to exec
43	Language    string   `json:"language,omitempty"` // informational ("go", "python", "typescript", ...)
44	Enabled     *bool    `json:"enabled,omitempty"`  // nil = enabled
45	Description string   `json:"description,omitempty"`
46}
47
48// IsEnabled returns the manifest's effective enabled state. Default
49// is true so adding a new extension folder Just Works without an
50// extra zot ext enable command.
51func (m Manifest) IsEnabled() bool {
52	return m.Enabled == nil || *m.Enabled
53}
54
55// Extension is a running extension subprocess and the metadata zot
56// tracks about it.
57type Extension struct {
58	Manifest Manifest
59	Dir      string // absolute path to extension directory
60	LogPath  string
61
62	cmd      *exec.Cmd
63	stdin    io.WriteCloser
64	stdout   io.ReadCloser
65	logFile  *os.File
66	helloAck bool
67	commands []extproto.RegisterCommandFromExt
68	tools    []extproto.RegisterToolFromExt
69
70	// readyCh is closed when the extension sends a ReadyFromExt
71	// frame, or when the host gives up waiting (registrationGrace).
72	readyCh   chan struct{}
73	readyOnce sync.Once
74
75	// pending command invocations waiting on a CommandResponseFromExt
76	// keyed by the id we sent in CommandInvokedFromHost.
77	// pendingTool is the same idea for tool calls.
78	// pendingIntercept is the same idea for event_intercept calls.
79	mu               sync.Mutex
80	pending          map[string]chan extproto.CommandResponseFromExt
81	pendingTool      map[string]chan extproto.ToolResultFromExt
82	pendingIntercept map[string]chan extproto.EventInterceptResponseFromExt
83
84	// lastFrameTime is updated by the read loop on every frame it
85	// processes. Used by the auto-ready idle watchdog so legacy
86	// extensions (no `ready` frame) don't pin the WaitForReady wait
87	// to its full grace.
88	lastFrameTime time.Time
89
90	// eventSubs and interceptSubs are the sets of event names this
91	// extension subscribed to via SubscribeFromExt. Used by
92	// EmitEvent / InterceptToolCall to filter recipients.
93	eventSubs     map[string]struct{}
94	interceptSubs map[string]struct{}
95
96	autoReady     bool
97	readyTimedOut bool
98	diagnostics   []string
99}
100
101// HostHooks is the small interface the manager calls back into the
102// running TUI through. Decouples extensions from interactive.go.
103type HostHooks interface {
104	// Notify pushes an ext-originated status message into the chat.
105	// Level is one of "info", "warn", "error", "success".
106	Notify(extName, level, message string)
107
108	// Submit feeds text as if the user had typed and pressed enter,
109	// running it through the agent loop.
110	Submit(text string)
111
112	// SubmitSlash runs text as a slash command in the TUI as if the
113	// user had typed it (text must start with '/'). Unlike Submit it
114	// does NOT run text through the model. Wired to the spontaneous
115	// submit_slash frame from extensions; ignored when the host is
116	// not interactive.
117	SubmitSlash(text string)
118
119	// Insert places text at the cursor in the editor.
120	Insert(text string)
121
122	// Display appends a one-shot styled note to the chat without
123	// invoking the model and without writing to the transcript.
124	Display(extName, text string)
125
126	// ClearNotes removes any notes previously pushed by extName via
127	// Notify/Display so transient status lines do not stack forever.
128	ClearNotes(extName string)
129
130	OpenPanel(extName string, spec extproto.PanelSpec)
131	UpdatePanel(extName, panelID, title string, lines []string, footer string)
132	ClosePanel(extName, panelID string)
133}
134
135type commandRegistration struct {
136	ext  *Extension
137	name string
138}
139
140// Manager owns every extension subprocess for the lifetime of zot.
141type Manager struct {
142	zotHome    string
143	cwd        string
144	zotVersion string
145	provider   string
146	model      string
147	hooks      HostHooks
148
149	mu  sync.RWMutex
150	ext map[string]*Extension // keyed by manifest name
151
152	// commandIndex maps a lower-cased slash-command name (without the
153	// leading /) to its canonical registration. First-come-first-served:
154	// later registrations that differ only by case are dropped with a warning.
155	commandIndex map[string]commandRegistration
156
157	// toolIndex maps an extension-defined tool name to its owning
158	// extension. Same first-come-first-served rule as commandIndex.
159	toolIndex map[string]*Extension
160
161	// explicitPaths remembers ad-hoc paths passed via --ext so
162	// Reload can respawn them alongside the discovered set.
163	explicitPaths []string
164
165	// onReload, if set, is invoked after a successful Reload. Used
166	// by the host so it can rebuild the agent's tool registry with
167	// the freshly-registered extension tools.
168	onReload func()
169}
170
171// New constructs an empty Manager. Call Discover to populate it from
172// the on-disk extension directories.
173func New(zotHome, cwd, zotVersion, provider, model string, hooks HostHooks) *Manager {
174	return &Manager{
175		zotHome:      zotHome,
176		cwd:          cwd,
177		zotVersion:   zotVersion,
178		provider:     provider,
179		model:        model,
180		hooks:        hooks,
181		ext:          map[string]*Extension{},
182		commandIndex: map[string]commandRegistration{},
183		toolIndex:    map[string]*Extension{},
184	}
185}
186
187// Discover scans the global and project extension dirs and starts
188// every extension whose manifest is enabled. Spawns happen in
189// parallel so a slow runtime (e.g. `npx tsx` cold-start, ~1.5s)
190// doesn't block other extensions from starting. Returns a slice of
191// errors encountered (one per extension); a single bad extension
192// doesn't abort the rest.
193func (m *Manager) Discover(ctx context.Context) []error {
194	type loadJob struct {
195		dir string
196	}
197	var jobs []loadJob
198	seenDirs := map[string]bool{} // dedup by basename so project wins
199	for _, dir := range m.searchDirs() {
200		entries, err := os.ReadDir(dir)
201		if err != nil {
202			continue // missing directory is fine
203		}
204		for _, e := range entries {
205			if !e.IsDir() {
206				continue
207			}
208			if seenDirs[e.Name()] {
209				continue // higher-priority location already queued
210			}
211			seenDirs[e.Name()] = true
212			jobs = append(jobs, loadJob{dir: filepath.Join(dir, e.Name())})
213		}
214	}
215
216	var wg sync.WaitGroup
217	errCh := make(chan error, len(jobs))
218	for _, j := range jobs {
219		wg.Add(1)
220		go func(extDir string) {
221			defer wg.Done()
222			if err := m.loadOne(ctx, extDir); err != nil {
223				errCh <- fmt.Errorf("%s: %w", extDir, err)
224			}
225		}(j.dir)
226	}
227	wg.Wait()
228	close(errCh)
229
230	var errs []error
231	for e := range errCh {
232		errs = append(errs, e)
233	}
234	return errs
235}
236
237// searchDirs returns the directories the discoverer walks, in
238// priority order: project-local first (so a project can override
239// global behavior), then global.
240func (m *Manager) searchDirs() []string {
241	var dirs []string
242	if m.cwd != "" {
243		dirs = append(dirs, filepath.Join(m.cwd, ".zot", "extensions"))
244	}
245	if m.zotHome != "" {
246		dirs = append(dirs, filepath.Join(m.zotHome, "extensions"))
247	}
248	return dirs
249}
250
251// loadOne reads a single extension's manifest and, if enabled,
252// spawns its subprocess + completes the hello handshake.
253func (m *Manager) loadOne(ctx context.Context, dir string) error {
254	manifestPath := filepath.Join(dir, "extension.json")
255	raw, err := os.ReadFile(manifestPath)
256	if err != nil {
257		return fmt.Errorf("read manifest: %w", err)
258	}
259	var mf Manifest
260	if err := json.Unmarshal(raw, &mf); err != nil {
261		return fmt.Errorf("parse manifest: %w", err)
262	}
263	if mf.Name == "" {
264		return errors.New("manifest: name is required")
265	}
266	hasTheme := HasExtensionTheme(dir)
267	if mf.Exec == "" && !hasTheme {
268		return errors.New("manifest: exec is required")
269	}
270	if !mf.IsEnabled() {
271		// Quietly skip disabled extensions; zot ext list will show them.
272		return nil
273	}
274
275	m.mu.RLock()
276	_, dup := m.ext[mf.Name]
277	m.mu.RUnlock()
278	if dup {
279		// Project-local copy already won; ignore the global one.
280		return nil
281	}
282
283	ext := &Extension{
284		Manifest:         mf,
285		Dir:              dir,
286		readyCh:          make(chan struct{}),
287		pending:          map[string]chan extproto.CommandResponseFromExt{},
288		pendingTool:      map[string]chan extproto.ToolResultFromExt{},
289		pendingIntercept: map[string]chan extproto.EventInterceptResponseFromExt{},
290		eventSubs:        map[string]struct{}{},
291		interceptSubs:    map[string]struct{}{},
292	}
293	if mf.Exec != "" {
294		if err := m.spawn(ctx, ext); err != nil {
295			return err
296		}
297	} else {
298		ext.readyOnce.Do(func() { close(ext.readyCh) })
299	}
300
301	m.mu.Lock()
302	m.ext[mf.Name] = ext
303	// Note: ext.commands and ext.tools may be empty here — they're
304	// populated by the read loop as register_* frames arrive after
305	// hello. Indexing happens in the read loop too. Discover()'s
306	// caller can WaitForReady() before relying on the registries.
307	m.mu.Unlock()
308	return nil
309}
310
311// LoadExplicit loads each path as an ad-hoc extension. Used for
312// `zot --ext <path>` so extension authors can iterate on a working
313// copy without having to `zot ext install` after every change.
314//
315// Loaded BEFORE Discover so explicit paths win on name conflicts
316// against installed extensions. Spawns happen in parallel like the
317// regular discovery path; errors are returned per path.
318// HasExtensionTheme reports whether an extension directory contains a theme
319// file in one of the supported extension-owned locations.
320func HasExtensionTheme(dir string) bool {
321	for _, file := range []string{"theme.json", filepath.Join("themes", "theme.json")} {
322		if _, err := os.Stat(filepath.Join(dir, file)); err == nil {
323			return true
324		}
325	}
326	return false
327}
328
329func (m *Manager) ThemeOptions() []tui.ThemeOption {
330	m.mu.RLock()
331	defer m.mu.RUnlock()
332	names := make([]string, 0, len(m.ext))
333	for name := range m.ext {
334		names = append(names, name)
335	}
336	sort.Strings(names)
337	var out []tui.ThemeOption
338	for _, name := range names {
339		ext := m.ext[name]
340		for _, file := range []string{"theme.json", "themes/theme.json"} {
341			path := filepath.Join(ext.Dir, file)
342			value := path
343			if opt, ok := tui.ThemeOptionFromFile(path, value, "extension "+ext.Manifest.Name); ok {
344				out = append(out, opt)
345			}
346		}
347	}
348	return out
349}
350
351func (m *Manager) LoadExplicit(ctx context.Context, paths []string) []error {
352	if len(paths) == 0 {
353		return nil
354	}
355
356	var wg sync.WaitGroup
357	errCh := make(chan error, len(paths))
358	absPaths := make([]string, 0, len(paths))
359	for _, p := range paths {
360		abs, err := filepath.Abs(p)
361		if err != nil {
362			errCh <- fmt.Errorf("%s: %w", p, err)
363			continue
364		}
365		absPaths = append(absPaths, abs)
366		wg.Add(1)
367		go func(extDir string) {
368			defer wg.Done()
369			if err := m.loadOne(ctx, extDir); err != nil {
370				errCh <- fmt.Errorf("%s: %w", extDir, err)
371			}
372		}(abs)
373	}
374	wg.Wait()
375	close(errCh)
376
377	m.mu.Lock()
378	m.explicitPaths = append(m.explicitPaths, absPaths...)
379	m.mu.Unlock()
380
381	var errs []error
382	for e := range errCh {
383		errs = append(errs, e)
384	}
385	return errs
386}
387
388// SetOnReload registers a callback fired after a successful Reload.
389// Hosts use it to rebuild the agent's tool registry with freshly-
390// registered extension tools.
391func (m *Manager) SetOnReload(fn func()) {
392	m.mu.Lock()
393	m.onReload = fn
394	m.mu.Unlock()
395}
396
397// ReloadStats summarises the outcome of Reload.
398type ReloadStats struct {
399	Stopped int     // how many old processes were torn down
400	Loaded  int     // how many new processes reached spawn
401	Ready   int     // how many of those signalled ready in time
402	Errors  []error // non-fatal per-extension errors
403}
404
405// Reload tears down every running extension, re-reads the manifests
406// from disk, respawns everyone (including the --ext paths remembered
407// from LoadExplicit), waits up to grace for ready signals, and
408// invokes the SetOnReload callback so the host can rebuild its tool
409// registry. The manager's internal maps are cleared before the
410// new load to ensure a clean slate.
411//
412// Safe to call concurrently with normal host operations: the lock is
413// released between stop and respawn so pending InvokeTool / Invoke
414// calls on the old processes get a clean error as their stdin
415// closes.
416func (m *Manager) Reload(ctx context.Context, grace time.Duration) ReloadStats {
417	stats := ReloadStats{}
418
419	// Snapshot and remember the explicit paths before we wipe state.
420	m.mu.Lock()
421	old := m.ext
422	explicit := append([]string(nil), m.explicitPaths...)
423	stats.Stopped = len(old)
424	m.ext = map[string]*Extension{}
425	m.commandIndex = map[string]commandRegistration{}
426	m.toolIndex = map[string]*Extension{}
427	m.explicitPaths = nil
428	callback := m.onReload
429	m.mu.Unlock()
430
431	// Graceful stop of the old set (reuses Stop's shutdown logic,
432	// but Stop re-reads m.ext which is now empty, so we replicate
433	// the small shutdown loop here on the snapshot).
434	oldExts := make([]*Extension, 0, len(old))
435	for _, ext := range old {
436		oldExts = append(oldExts, ext)
437	}
438	stopExtensions(oldExts, grace)
439
440	// Fresh load. Explicit paths first (they still win on conflict).
441	if errs := m.LoadExplicit(ctx, explicit); len(errs) > 0 {
442		stats.Errors = append(stats.Errors, errs...)
443	}
444	if errs := m.Discover(ctx); len(errs) > 0 {
445		stats.Errors = append(stats.Errors, errs...)
446	}
447
448	m.mu.RLock()
449	stats.Loaded = len(m.ext)
450	m.mu.RUnlock()
451
452	// Wait for ready frames. Use the same 3s grace zot uses at
453	// startup so the reload feels no slower than a cold boot.
454	readyDeadline := time.Now().Add(grace)
455	if time.Until(readyDeadline) < 3*time.Second {
456		readyDeadline = time.Now().Add(3 * time.Second)
457	}
458	m.WaitForReady(time.Until(readyDeadline))
459
460	m.mu.RLock()
461	for _, ext := range m.ext {
462		select {
463		case <-ext.readyCh:
464			stats.Ready++
465		default:
466		}
467	}
468	m.mu.RUnlock()
469
470	if callback != nil {
471		callback()
472	}
473
474	return stats
475}
476
477// WaitForReady blocks until every loaded extension has signalled
478// ReadyFromExt, or the grace period expires for the slowest one.
479//
480// Waits run in parallel: total time is max(per-extension wait), not
481// sum. Without this, a single slow extension (e.g. `npx tsx` cold)
482// would gate every other extension's wait too and zot startup would
483// scale linearly with the number of slow runtimes installed.
484//
485// Call after Discover and before relying on tool registrations.
486func (m *Manager) WaitForReady(grace time.Duration) {
487	m.mu.RLock()
488	exts := make([]*Extension, 0, len(m.ext))
489	for _, e := range m.ext {
490		exts = append(exts, e)
491	}
492	m.mu.RUnlock()
493
494	deadline := time.After(grace)
495	var wg sync.WaitGroup
496	for _, ext := range exts {
497		wg.Add(1)
498		go func(ext *Extension) {
499			defer wg.Done()
500			select {
501			case <-ext.readyCh:
502			case <-deadline:
503				ext.mu.Lock()
504				ext.readyTimedOut = true
505				ext.diagnostics = append(ext.diagnostics, "timed out waiting for ready frame")
506				ext.mu.Unlock()
507				fmt.Fprintf(ext.logFile, "[zot] timed out waiting for ready frame; proceeding\n")
508				ext.readyOnce.Do(func() { close(ext.readyCh) })
509			}
510		}(ext)
511	}
512	wg.Wait()
513}
514
515// spawn launches the subprocess, hooks up pipes, logs stderr, and
516// runs the synchronous portion of the hello handshake. Asynchronous
517// frames are processed in a goroutine started here.
518func (m *Manager) spawn(ctx context.Context, ext *Extension) error {
519	logsDir := filepath.Join(m.zotHome, "logs")
520	_ = os.MkdirAll(logsDir, 0o755)
521	logPath := filepath.Join(logsDir, "ext-"+ext.Manifest.Name+".log")
522	logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
523	if err != nil {
524		return fmt.Errorf("open log: %w", err)
525	}
526	ext.LogPath = logPath
527	ext.logFile = logFile
528	fmt.Fprintf(logFile, "\n[zot] starting %s/%s at %s\n", ext.Manifest.Name, ext.Manifest.Version, time.Now().Format(time.RFC3339))
529
530	// Exec resolution rules:
531	//   - absolute path:                 used as-is.
532	//   - starts with "." (./ or ../):  resolved relative to ext.Dir.
533	//   - bare name (no path separator): looked up via $PATH so
534	//                                    "node", "npx", "python3",
535	//                                    "tsx" etc. work without
536	//                                    forcing absolute paths.
537	//   - other relative form (foo/bar): resolved relative to ext.Dir.
538	execPath := ext.Manifest.Exec
539	switch {
540	case filepath.IsAbs(execPath):
541		// keep
542	case strings.HasPrefix(execPath, "."+string(filepath.Separator)) ||
543		strings.HasPrefix(execPath, ".."+string(filepath.Separator)) ||
544		execPath == "." || execPath == "..":
545		execPath = filepath.Join(ext.Dir, execPath)
546	case strings.ContainsRune(execPath, filepath.Separator):
547		execPath = filepath.Join(ext.Dir, execPath)
548	default:
549		// bare name: leave as-is for exec.LookPath via exec.Command.
550	}
551	cmd := exec.CommandContext(ctx, execPath, ext.Manifest.Args...)
552	cmd.Dir = ext.Dir
553	cmd.Stderr = logFile
554	isolateExtensionProcess(cmd)
555
556	var stdin io.WriteCloser
557	var stdout io.ReadCloser
558	started := false
559	cleanup := true
560	defer func() {
561		if !cleanup {
562			return
563		}
564		if stdin != nil {
565			_ = stdin.Close()
566		}
567		if stdout != nil {
568			_ = stdout.Close()
569		}
570		if started {
571			_ = cmd.Process.Kill()
572			_ = cmd.Wait()
573		}
574		_ = logFile.Close()
575	}()
576
577	stdin, err = cmd.StdinPipe()
578	if err != nil {
579		return fmt.Errorf("stdin pipe (stderr log: %s): %w", logPath, err)
580	}
581	stdout, err = cmd.StdoutPipe()
582	if err != nil {
583		return fmt.Errorf("stdout pipe (stderr log: %s): %w", logPath, err)
584	}
585	if err := cmd.Start(); err != nil {
586		return fmt.Errorf("spawn (stderr log: %s): %w", logPath, err)
587	}
588	started = true
589	ext.cmd = cmd
590	ext.stdin = stdin
591	ext.stdout = stdout
592
593	// Hello handshake. Read the extension's HelloFromExt synchronously
594	// so we can fail fast on a broken extension; everything after is
595	// processed in the read goroutine.
596	scanner := bufio.NewScanner(stdout)
597	scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
598	if !scanner.Scan() {
599		if err := scanner.Err(); err != nil {
600			return fmt.Errorf("read hello (stderr log: %s): %w", logPath, err)
601		}
602		return fmt.Errorf("extension exited before hello (stderr log: %s)", logPath)
603	}
604	var hello extproto.HelloFromExt
605	if err := json.Unmarshal(scanner.Bytes(), &hello); err != nil {
606		return fmt.Errorf("parse hello (stderr log: %s): %w", logPath, err)
607	}
608	if hello.Type != "hello" || hello.Name == "" {
609		return fmt.Errorf("first frame must be hello (got %q; stderr log: %s)", hello.Type, logPath)
610	}
611	// Trust the manifest's name; ignore mismatch from the hello.
612	ext.helloAck = true
613
614	ack, _ := extproto.Encode(extproto.HelloAckFromHost{
615		Type:            "hello_ack",
616		ProtocolVersion: extproto.ProtocolVersion,
617		ZotVersion:      m.zotVersion,
618		Provider:        m.provider,
619		Model:           m.model,
620		CWD:             m.cwd,
621		ExtensionDir:    ext.Dir,
622		DataDir:         ext.Dir,
623	})
624	if _, err := stdin.Write(ack); err != nil {
625		return fmt.Errorf("send hello_ack (stderr log: %s): %w", logPath, err)
626	}
627
628	cleanup = false
629
630	// Spin up the read loop now that the handshake is done.
631	go m.readLoop(ext, scanner)
632
633	// Compatibility shim: extensions built against the phase-1 SDK
634	// don't send a ready frame. Watch the read loop's frame arrival
635	// rate; if nothing's arrived for readyIdleWindow we treat the
636	// extension as ready so WaitForReady doesn't burn the full grace
637	// on every startup. Newer extensions still trigger the explicit
638	// path on their own ready frame.
639	go m.assumeReadyAfterIdle(ext)
640
641	return nil
642}
643
644// readyIdleWindow is how long the manager waits for a frame after
645// hello before assuming an extension that doesn't send `ready` is
646// nevertheless ready. 250ms is enough for any well-behaved native
647// binary to flush its register frames; slow runtimes (npx tsx) flush
648// even faster once they've started, so this rarely affects them.
649const readyIdleWindow = 250 * time.Millisecond
650
651func (m *Manager) assumeReadyAfterIdle(ext *Extension) {
652	ext.mu.Lock()
653	last := ext.lastFrameTime
654	ext.mu.Unlock()
655	for {
656		select {
657		case <-ext.readyCh:
658			return
659		case <-time.After(readyIdleWindow):
660		}
661		ext.mu.Lock()
662		current := ext.lastFrameTime
663		ext.mu.Unlock()
664		if current.Equal(last) {
665			// No new frame in the idle window. Treat as ready.
666			ext.readyOnce.Do(func() {
667				ext.mu.Lock()
668				ext.autoReady = true
669				ext.diagnostics = append(ext.diagnostics, "no ready frame; auto-ready after idle")
670				ext.mu.Unlock()
671				fmt.Fprintf(ext.logFile, "[zot] no ready frame; auto-readying after idle (legacy SDK?)\n")
672				close(ext.readyCh)
673			})
674			return
675		}
676		last = current
677	}
678}
679
680// readLoop processes every frame the extension sends after hello.
681// Returns when stdout closes.
682func (m *Manager) readLoop(ext *Extension, scanner *bufio.Scanner) {
683	defer func() {
684		// On close, drop every command + tool this extension owned so
685		// future invocations don't dangle. The subprocess is gone; we
686		// won't hear back about its commands or tool calls anymore.
687		m.mu.Lock()
688		for name, registration := range m.commandIndex {
689			if registration.ext == ext {
690				delete(m.commandIndex, name)
691			}
692		}
693		for name, owner := range m.toolIndex {
694			if owner == ext {
695				delete(m.toolIndex, name)
696			}
697		}
698		m.mu.Unlock()
699		ext.readyOnce.Do(func() { close(ext.readyCh) })
700		fmt.Fprintf(ext.logFile, "[zot] extension %s read loop exited at %s\n", ext.Manifest.Name, time.Now().Format(time.RFC3339))
701	}()
702
703	for scanner.Scan() {
704		line := scanner.Bytes()
705		ext.mu.Lock()
706		ext.lastFrameTime = time.Now()
707		ext.mu.Unlock()
708		var frame extproto.Frame
709		if err := json.Unmarshal(line, &frame); err != nil {
710			ext.recordDiagnostic(fmt.Sprintf("malformed json frame: %v", err))
711			fmt.Fprintf(ext.logFile, "[zot] malformed json from extension: %v\n", err)
712			continue
713		}
714		switch frame.Type {
715		case "register_command":
716			var rc extproto.RegisterCommandFromExt
717			if err := json.Unmarshal(line, &rc); err == nil {
718				// Both ext.commands and m.commandIndex are read by
719				// the public Commands() / HasCommand() helpers under
720				// m.mu, so the writes have to take the same lock to
721				// keep the race detector happy.
722				m.mu.Lock()
723				ext.commands = append(ext.commands, rc)
724				key := strings.ToLower(rc.Name)
725				_, shadowed := m.commandIndex[key]
726				if !shadowed {
727					m.commandIndex[key] = commandRegistration{ext: ext, name: rc.Name}
728				}
729				m.mu.Unlock()
730				if shadowed {
731					ext.recordDiagnostic(fmt.Sprintf("command /%s conflicts with another extension registering the same name", rc.Name))
732				}
733			}
734		case "register_tool":
735			var rt extproto.RegisterToolFromExt
736			if err := json.Unmarshal(line, &rt); err != nil {
737				fmt.Fprintf(ext.logFile, "[zot] bad register_tool frame: %v\n", err)
738				continue
739			}
740			// Validate the schema parses as JSON. If not, refuse to
741			// register — a broken schema confuses the model.
742			if len(rt.Schema) > 0 {
743				var tmp any
744				if err := json.Unmarshal(rt.Schema, &tmp); err != nil {
745					ext.recordDiagnostic(fmt.Sprintf("tool %q schema is not valid json: %v", rt.Name, err))
746					fmt.Fprintf(ext.logFile, "[zot] tool %q: schema is not valid json (%v); skipped\n", rt.Name, err)
747					continue
748				}
749			}
750			m.mu.Lock()
751			ext.tools = append(ext.tools, rt)
752			_, shadowed := m.toolIndex[rt.Name]
753			if !shadowed {
754				m.toolIndex[rt.Name] = ext
755			}
756			m.mu.Unlock()
757			if shadowed {
758				ext.recordDiagnostic(fmt.Sprintf("tool %s conflicts with another extension registering the same name", rt.Name))
759			}
760		case "ready":
761			ext.readyOnce.Do(func() { close(ext.readyCh) })
762		case "subscribe":
763			var sub extproto.SubscribeFromExt
764			if err := json.Unmarshal(line, &sub); err == nil {
765				ext.mu.Lock()
766				for _, ev := range sub.Events {
767					ext.eventSubs[ev] = struct{}{}
768				}
769				for _, ev := range sub.Intercept {
770					switch ev {
771					case "tool_call", "turn_start", "assistant_message":
772						ext.interceptSubs[ev] = struct{}{}
773					}
774				}
775				ext.mu.Unlock()
776			}
777		case "event_intercept_response":
778			var er extproto.EventInterceptResponseFromExt
779			if err := json.Unmarshal(line, &er); err == nil {
780				ext.mu.Lock()
781				ch, ok := ext.pendingIntercept[er.ID]
782				if ok {
783					delete(ext.pendingIntercept, er.ID)
784				}
785				ext.mu.Unlock()
786				if ok {
787					select {
788					case ch <- er:
789					default:
790					}
791				}
792			}
793		case "tool_result":
794			var tr extproto.ToolResultFromExt
795			if err := json.Unmarshal(line, &tr); err == nil {
796				ext.mu.Lock()
797				ch, ok := ext.pendingTool[tr.ID]
798				if ok {
799					delete(ext.pendingTool, tr.ID)
800				}
801				ext.mu.Unlock()
802				if ok {
803					select {
804					case ch <- tr:
805					default:
806					}
807				}
808			}
809		case "notify":
810			var n extproto.NotifyFromExt
811			if err := json.Unmarshal(line, &n); err == nil {
812				m.hooks.Notify(ext.Manifest.Name, n.Level, n.Message)
813			}
814		case "clear_notes":
815			m.hooks.ClearNotes(ext.Manifest.Name)
816		case "submit":
817			var s extproto.SubmitFromExt
818			if err := json.Unmarshal(line, &s); err == nil {
819				text := strings.TrimSpace(s.Text)
820				if text != "" {
821					m.hooks.Submit(text)
822				}
823			}
824		case "submit_slash":
825			// Spontaneous request to invoke a slash command in the
826			// TUI. Refused unless the payload looks like a slash
827			// command so a misbehaving extension can't sneak a model
828			// prompt through this path.
829			var s extproto.SubmitSlashFromExt
830			if err := json.Unmarshal(line, &s); err == nil {
831				text := strings.TrimSpace(s.Text)
832				if strings.HasPrefix(text, "/") {
833					m.hooks.SubmitSlash(text)
834				} else {
835					fmt.Fprintf(ext.logFile, "[zot] submit_slash refused (not a slash command): %q\n", s.Text)
836				}
837			}
838		case "command_response":
839			var cr extproto.CommandResponseFromExt
840			if err := json.Unmarshal(line, &cr); err == nil {
841				ext.mu.Lock()
842				ch, ok := ext.pending[cr.ID]
843				if ok {
844					delete(ext.pending, cr.ID)
845				}
846				ext.mu.Unlock()
847				if ok {
848					select {
849					case ch <- cr:
850					default:
851					}
852				}
853			}
854		case "open_panel":
855			var op extproto.OpenPanelFromExt
856			if err := json.Unmarshal(line, &op); err == nil {
857				m.hooks.OpenPanel(ext.Manifest.Name, op.Panel)
858			}
859		case "panel_render":
860			var pr extproto.PanelRenderFromExt
861			if err := json.Unmarshal(line, &pr); err == nil {
862				m.hooks.UpdatePanel(ext.Manifest.Name, pr.PanelID, pr.Title, pr.Lines, pr.Footer)
863			}
864		case "panel_close":
865			var pc extproto.PanelCloseFromExt
866			if err := json.Unmarshal(line, &pc); err == nil {
867				m.hooks.ClosePanel(ext.Manifest.Name, pc.PanelID)
868			}
869		case "shutdown_ack":
870			// Caller of Stop is waiting on the process exit, not this frame.
871		default:
872			ext.recordDiagnostic(fmt.Sprintf("unknown frame type %q", frame.Type))
873			fmt.Fprintf(ext.logFile, "[zot] unknown frame type %q\n", frame.Type)
874		}
875	}
876}
877
878func (ext *Extension) recordDiagnostic(msg string) {
879	ext.mu.Lock()
880	defer ext.mu.Unlock()
881	ext.diagnostics = append(ext.diagnostics, msg)
882}
883
884// RegisteredCommandDiagnostic describes one command an extension attempted
885// to register and whether it owns the active dispatch slot.
886type RegisteredCommandDiagnostic struct {
887	Name        string
888	Description string
889	Active      bool
890}
891
892// RegisteredToolDiagnostic describes one tool an extension attempted to
893// register and whether it owns the active dispatch slot.
894type RegisteredToolDiagnostic struct {
895	Name        string
896	Description string
897	Active      bool
898}
899
900// ExtensionDiagnostic is a stable snapshot for diagnostic commands.
901type ExtensionDiagnostic struct {
902	Name          string
903	Version       string
904	Dir           string
905	LogPath       string
906	ThemeOnly     bool
907	Ready         bool
908	AutoReady     bool
909	ReadyTimedOut bool
910	Commands      []RegisteredCommandDiagnostic
911	Tools         []RegisteredToolDiagnostic
912	Messages      []string
913}
914
915// Diagnostics returns a snapshot of loaded extension state for commands such
916// as `zot ext doctor`. It is read-only and does not change manager behavior.
917func (m *Manager) Diagnostics() []ExtensionDiagnostic {
918	m.mu.RLock()
919	defer m.mu.RUnlock()
920	names := make([]string, 0, len(m.ext))
921	for name := range m.ext {
922		names = append(names, name)
923	}
924	sort.Strings(names)
925	out := make([]ExtensionDiagnostic, 0, len(names))
926	for _, name := range names {
927		ext := m.ext[name]
928		ext.mu.Lock()
929		msgs := append([]string(nil), ext.diagnostics...)
930		autoReady := ext.autoReady
931		readyTimedOut := ext.readyTimedOut
932		ext.mu.Unlock()
933
934		diag := ExtensionDiagnostic{
935			Name:          ext.Manifest.Name,
936			Version:       ext.Manifest.Version,
937			Dir:           ext.Dir,
938			LogPath:       ext.LogPath,
939			ThemeOnly:     ext.Manifest.Exec == "",
940			AutoReady:     autoReady,
941			ReadyTimedOut: readyTimedOut,
942			Messages:      msgs,
943		}
944		select {
945		case <-ext.readyCh:
946			diag.Ready = true
947		default:
948		}
949		for _, c := range ext.commands {
950			registration, ok := m.commandIndex[strings.ToLower(c.Name)]
951			diag.Commands = append(diag.Commands, RegisteredCommandDiagnostic{
952				Name:        c.Name,
953				Description: c.Description,
954				Active:      ok && registration.ext == ext && registration.name == c.Name,
955			})
956		}
957		for _, t := range ext.tools {
958			diag.Tools = append(diag.Tools, RegisteredToolDiagnostic{
959				Name:        t.Name,
960				Description: t.Description,
961				Active:      m.toolIndex[t.Name] == ext,
962			})
963		}
964		out = append(out, diag)
965	}
966	return out
967}
968
969// Commands returns a snapshot of every (extension, command) pair
970// currently registered. Used by the slash autocomplete + /help.
971func (m *Manager) Commands() []CommandInfo {
972	m.mu.RLock()
973	defer m.mu.RUnlock()
974	var out []CommandInfo
975	for _, ext := range m.ext {
976		for _, c := range ext.commands {
977			registration, ok := m.commandIndex[strings.ToLower(c.Name)]
978			if !ok || registration.ext != ext || registration.name != c.Name {
979				continue
980			}
981			out = append(out, CommandInfo{
982				Extension:   ext.Manifest.Name,
983				Name:        c.Name,
984				Description: c.Description,
985			})
986		}
987	}
988	return out
989}
990
991// CommandInfo is one extension-registered slash command, surfaced to
992// the rest of zot for display purposes.
993type CommandInfo struct {
994	Extension   string
995	Name        string
996	Description string
997}
998
999// ToolInfo is one extension-registered tool. Used by the agent's
1000// build step to materialise core.Tool wrappers.
1001type ToolInfo struct {
1002	Extension   string
1003	Name        string
1004	Description string
1005	Schema      json.RawMessage
1006	Deferred    bool
1007}
1008
1009// Tools returns a snapshot of every (extension, tool) pair currently
1010// registered. Used at agent-build time to fold extension tools into
1011// the runtime tool registry.
1012func (m *Manager) Tools() []ToolInfo {
1013	m.mu.RLock()
1014	defer m.mu.RUnlock()
1015	var out []ToolInfo
1016	for _, ext := range m.ext {
1017		for _, t := range ext.tools {
1018			out = append(out, ToolInfo{
1019				Extension:   ext.Manifest.Name,
1020				Name:        t.Name,
1021				Description: t.Description,
1022				Schema:      t.Schema,
1023				Deferred:    t.Deferred,
1024			})
1025		}
1026	}
1027	return out
1028}
1029
1030// HasTool reports whether name is registered by any extension.
1031func (m *Manager) HasTool(name string) bool {
1032	m.mu.RLock()
1033	defer m.mu.RUnlock()
1034	_, ok := m.toolIndex[name]
1035	return ok
1036}
1037
1038// InvokeTool sends a tool_call to the owning extension and waits for
1039// the matching tool_result. Used by the core.Tool wrapper that the
1040// agent registers per extension-defined tool.
1041func (m *Manager) InvokeTool(ctx context.Context, name string, args json.RawMessage, timeout time.Duration) (extproto.ToolResultFromExt, error) {
1042	m.mu.RLock()
1043	ext, ok := m.toolIndex[name]
1044	m.mu.RUnlock()
1045	if !ok {
1046		return extproto.ToolResultFromExt{}, fmt.Errorf("no extension registered for tool %q", name)
1047	}
1048
1049	id := newCorrelationID()
1050	ch := make(chan extproto.ToolResultFromExt, 1)
1051	ext.mu.Lock()
1052	ext.pendingTool[id] = ch
1053	ext.mu.Unlock()
1054
1055	frame, _ := extproto.Encode(extproto.ToolCallFromHost{
1056		Type: "tool_call",
1057		ID:   id,
1058		Name: name,
1059		Args: args,
1060	})
1061	if _, err := ext.stdin.Write(frame); err != nil {
1062		ext.mu.Lock()
1063		delete(ext.pendingTool, id)
1064		ext.mu.Unlock()
1065		return extproto.ToolResultFromExt{}, fmt.Errorf("write: %w", err)
1066	}
1067
1068	select {
1069	case resp := <-ch:
1070		return resp, nil
1071	case <-time.After(timeout):
1072		ext.mu.Lock()
1073		delete(ext.pendingTool, id)
1074		ext.mu.Unlock()
1075		return extproto.ToolResultFromExt{}, fmt.Errorf("timeout waiting for %s/%s", ext.Manifest.Name, name)
1076	case <-ctx.Done():
1077		ext.mu.Lock()
1078		delete(ext.pendingTool, id)
1079		ext.mu.Unlock()
1080		return extproto.ToolResultFromExt{}, ctx.Err()
1081	}
1082}
1083
1084// HasCommand reports whether name is registered by any extension.
1085func (m *Manager) HasCommand(name string) bool {
1086	m.mu.RLock()
1087	defer m.mu.RUnlock()
1088	_, ok := m.commandIndex[strings.ToLower(name)]
1089	return ok
1090}
1091
1092func (m *Manager) CommandOwner(name string) string {
1093	m.mu.RLock()
1094	defer m.mu.RUnlock()
1095	if registration, ok := m.commandIndex[strings.ToLower(name)]; ok && registration.ext != nil {
1096		return registration.ext.Manifest.Name
1097	}
1098	return ""
1099}
1100
1101// Invoke fires the named slash command's handler in the owning
1102// extension and waits up to timeout for the response. Returns the
1103// extension's CommandResponse so the caller can act on the action
1104// (prompt / insert / display).
1105func (m *Manager) Invoke(ctx context.Context, name, args string, timeout time.Duration) (extproto.CommandResponseFromExt, error) {
1106	m.mu.RLock()
1107	registration, ok := m.commandIndex[strings.ToLower(name)]
1108	m.mu.RUnlock()
1109	if !ok {
1110		return extproto.CommandResponseFromExt{}, fmt.Errorf("no extension registered for /%s", name)
1111	}
1112	ext := registration.ext
1113	name = registration.name
1114
1115	id := newCorrelationID()
1116	ch := make(chan extproto.CommandResponseFromExt, 1)
1117	ext.mu.Lock()
1118	ext.pending[id] = ch
1119	ext.mu.Unlock()
1120
1121	frame, _ := extproto.Encode(extproto.CommandInvokedFromHost{
1122		Type: "command_invoked",
1123		ID:   id,
1124		Name: name,
1125		Args: args,
1126	})
1127	if _, err := ext.stdin.Write(frame); err != nil {
1128		ext.mu.Lock()
1129		delete(ext.pending, id)
1130		ext.mu.Unlock()
1131		return extproto.CommandResponseFromExt{}, fmt.Errorf("write: %w", err)
1132	}
1133
1134	select {
1135	case resp := <-ch:
1136		return resp, nil
1137	case <-time.After(timeout):
1138		ext.mu.Lock()
1139		delete(ext.pending, id)
1140		ext.mu.Unlock()
1141		return extproto.CommandResponseFromExt{}, fmt.Errorf("timeout waiting for %s/%s", ext.Manifest.Name, name)
1142	case <-ctx.Done():
1143		ext.mu.Lock()
1144		delete(ext.pending, id)
1145		ext.mu.Unlock()
1146		return extproto.CommandResponseFromExt{}, ctx.Err()
1147	}
1148}
1149
1150// Stop cleanly terminates every extension. Sends ShutdownFromHost,
1151// waits up to gracePeriod for each subprocess to exit, then SIGTERMs
1152// (and SIGKILLs after another second) the holdouts.
1153func (m *Manager) SendPanelKey(extName, panelID, key, text string) error {
1154	m.mu.RLock()
1155	ext, ok := m.ext[extName]
1156	m.mu.RUnlock()
1157	if !ok {
1158		return fmt.Errorf("no extension %q", extName)
1159	}
1160	frame, _ := extproto.Encode(extproto.PanelKeyFromHost{Type: "panel_key", PanelID: panelID, Key: key, Text: text})
1161	_, err := ext.stdin.Write(frame)
1162	return err
1163}
1164
1165func (m *Manager) SendPanelClose(extName, panelID string) error {
1166	m.mu.RLock()
1167	ext, ok := m.ext[extName]
1168	m.mu.RUnlock()
1169	if !ok {
1170		return fmt.Errorf("no extension %q", extName)
1171	}
1172	frame, _ := extproto.Encode(extproto.PanelCloseFromHost{Type: "panel_close", PanelID: panelID})
1173	_, err := ext.stdin.Write(frame)
1174	return err
1175}
1176
1177func (m *Manager) Stop(gracePeriod time.Duration) {
1178	m.mu.RLock()
1179	exts := make([]*Extension, 0, len(m.ext))
1180	for _, e := range m.ext {
1181		exts = append(exts, e)
1182	}
1183	m.mu.RUnlock()
1184	stopExtensions(exts, gracePeriod)
1185}
1186
1187func stopExtensions(exts []*Extension, gracePeriod time.Duration) {
1188	for _, ext := range exts {
1189		if ext.stdin == nil {
1190			continue
1191		}
1192		if frame, err := extproto.Encode(extproto.ShutdownFromHost{Type: "shutdown"}); err == nil {
1193			_, _ = ext.stdin.Write(frame)
1194		}
1195		_ = ext.stdin.Close()
1196	}
1197
1198	deadline := time.Now().Add(gracePeriod)
1199	for _, ext := range exts {
1200		if ext.cmd == nil {
1201			if ext.logFile != nil {
1202				_ = ext.logFile.Close()
1203			}
1204			continue
1205		}
1206		remaining := time.Until(deadline)
1207		if remaining <= 0 {
1208			remaining = 100 * time.Millisecond
1209		}
1210		done := make(chan struct{})
1211		go func() { _ = ext.cmd.Wait(); close(done) }()
1212		select {
1213		case <-done:
1214		case <-time.After(remaining):
1215			_ = ext.cmd.Process.Signal(syscall.SIGTERM)
1216			select {
1217			case <-done:
1218			case <-time.After(time.Second):
1219				_ = ext.cmd.Process.Kill()
1220				<-done
1221			}
1222		}
1223		if ext.logFile != nil {
1224			_ = ext.logFile.Close()
1225		}
1226	}
1227}
1228
1229// All returns every extension currently tracked, enabled or not.
1230// Used by `zot ext list`.
1231func (m *Manager) All() []*Extension {
1232	m.mu.RLock()
1233	defer m.mu.RUnlock()
1234	out := make([]*Extension, 0, len(m.ext))
1235	for _, e := range m.ext {
1236		out = append(out, e)
1237	}
1238	return out
1239}
1240
1241// newCorrelationID returns a short non-cryptographic id. We don't
1242// need uniqueness across processes, just within the lifetime of one
1243// extension's pending map.
1244func newCorrelationID() string {
1245	return strings.ReplaceAll(time.Now().Format("150405.000000"), ".", "")
1246}

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 477

↑↓ moveenter openesc close