v0.48.1 deaab59

scheduler_runtime.go RawHistoryFolder

1package agent
2
3import (
4	"context"
5	"fmt"
6	"strings"
7	"sync"
8
9	"github.com/bnema/zut/packages/agent/extensions"
10	"github.com/bnema/zut/packages/agent/scheduler"
11	"github.com/bnema/zut/packages/core"
12	"github.com/bnema/zut/packages/provider"
13)
14
15// scheduledSessionRegistry keeps background runs from opening foreground
16// panels and waiting indefinitely for input from a hidden conversation.
17func scheduledSessionRegistry(registry core.Registry) core.Registry {
18	for name, tool := range registry {
19		if extensions.IsInteractiveTool(tool) {
20			delete(registry, name)
21		}
22	}
23	return registry
24}
25
26// runScheduledSession reconstructs an inactive session in the current zut
27// process and appends the resulting turn to its existing transcript. The
28// caller serializes this with session transitions before invoking it.
29func runScheduledSession(ctx context.Context, task scheduler.Task, args Args, base Resolved, extMgr *extensions.Manager, prepareRegistry func(core.Registry) core.Registry) error {
30	path, err := core.FindManagedSessionByID(ctx, ZutHome(), task.SessionID)
31	if err != nil {
32		return fmt.Errorf("find scheduled session %q: %w", task.SessionID, err)
33	}
34	if path == "" {
35		return fmt.Errorf("scheduled session %q no longer exists", task.SessionID)
36	}
37	sess, messages, err := core.OpenSession(path)
38	if err != nil {
39		return fmt.Errorf("open scheduled session: %w", err)
40	}
41	if sess.ID != task.SessionID {
42		closeErr := sess.Close()
43		if closeErr != nil {
44			return fmt.Errorf("scheduled session %q changed during resume: %w", task.SessionID, closeErr)
45		}
46		return fmt.Errorf("scheduled session %q changed during resume", task.SessionID)
47	}
48	defer sess.Close()
49
50	providerName := strings.TrimSpace(sess.Meta.Provider)
51	if providerName == "" {
52		providerName = base.Provider
53	}
54	model := strings.TrimSpace(sess.Meta.Model)
55	if model == "" {
56		model = base.Model
57	}
58	// The shared manager may be attached to the foreground TUI, but this
59	// reconstructed session has no visible user to answer interactive tools.
60	backgroundRegistry := func(registry core.Registry) core.Registry {
61		if prepareRegistry != nil {
62			registry = prepareRegistry(registry)
63		}
64		return scheduledSessionRegistry(registry)
65	}
66	ag, _, _, err := buildNonInteractiveSessionAgentWithRegistry(ctx, args, base, extMgr, providerName, model, backgroundRegistry)
67	if err != nil {
68		return fmt.Errorf("build scheduled session agent: %w", err)
69	}
70	ag.SetMessages(trimMessagesForResume(messages, 100))
71	// Seed the agent-owned plan from the reconstructed session so a scheduled
72	// turn sees the persisted checklist instead of empty core state.
73	ag.SetPlan(sess.Meta.PlanSteps())
74	// Route tool-result commits through the session so plan (and goal)
75	// mutations the scheduled turn makes are durable, matching the other modes.
76	ag.CommitToolResult = func(_ string, result core.ToolResult) error {
77		return persistToolResultState(nil, sess, result)
78	}
79	if cumulative, lastTurn, usageErr := core.SessionUsageDetail(path); usageErr == nil {
80		ag.SeedCost(cumulative)
81		ag.SeedLastTurnUsage(lastTurn)
82	}
83	ag.SetSessionTimeContext(sess.Meta.Started, sess.Meta.Timezone, sess.Meta.TimezoneOffset)
84	var appendMu sync.Mutex
85	var appendErr error
86	recordAppendError := func(err error) {
87		if err == nil {
88			return
89		}
90		appendMu.Lock()
91		defer appendMu.Unlock()
92		if appendErr == nil {
93			appendErr = err
94		}
95	}
96	ag.DisableRepetitionGuard = true
97	ag.OnMessageAppended = func(message provider.Message) {
98		recordAppendError(sess.AppendMessage(message))
99	}
100	ag.OnUsage = func(cumulative provider.Usage) {
101		recordAppendError(sess.AppendUsage(cumulative, cumulative))
102	}
103	ag.OnTranscriptCompacted = func(messages []provider.Message) {
104		recordAppendError(sess.AppendCompaction(messages))
105	}
106	if err := ag.Prompt(ctx, task.Message, nil, func(core.AgentEvent) {}); err != nil {
107		return err
108	}
109	appendMu.Lock()
110	err = appendErr
111	appendMu.Unlock()
112	if err != nil {
113		return fmt.Errorf("append scheduled transcript: %w", err)
114	}
115	if err := sess.Flush(); err != nil {
116		return fmt.Errorf("flush scheduled session: %w", err)
117	}
118	return nil
119}

Keyboard shortcuts

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

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

Branches 1

Tags 476

↑↓ moveenter openesc close