v0.17.0 949637b

goal.go RawHistoryFolder

1package modes
2
3import (
4	"context"
5	"fmt"
6	"strings"
7
8	agenttools "github.com/bnema/zut/packages/agent/tools"
9	"github.com/bnema/zut/packages/core"
10	"github.com/bnema/zut/packages/provider"
11)
12
13const goalContinueMetaKey = "zut_goal_continue"
14
15func (i *Interactive) goalUpdatesAvailable() bool {
16	ag := i.Agent()
17	if ag == nil {
18		return false
19	}
20	_, ok := ag.ToolsSnapshot()[agenttools.UpdateGoalToolName]
21	return ok
22}
23
24func (i *Interactive) goalContinuationMessage() (provider.Message, bool) {
25	if i.cfg.CurrentGoal == nil || !i.goalUpdatesAvailable() {
26		return provider.Message{}, false
27	}
28	goal := i.cfg.CurrentGoal()
29	if goal == nil || goal.Status != core.GoalActive || strings.TrimSpace(goal.Objective) == "" {
30		return provider.Message{}, false
31	}
32	text := goal.Objective +
33		"\n\nContinue working autonomously toward this active goal. Do not stop at a progress update. Continue until the goal is complete or blocked, then call update_goal."
34	if goal.MissionID != "" {
35		text += " When setting a next goal, use mission_id " + goal.MissionID + " and keep it within the same user mission."
36	}
37	return provider.Message{
38		Role:    provider.RoleUser,
39		Content: []provider.Content{provider.TextBlock{Text: text}},
40		Meta:    map[string]string{goalContinueMetaKey: "true"},
41	}, true
42}
43
44func (i *Interactive) goalIdleLocked() bool {
45	return !i.busy && i.agent != nil && len(i.queued) == 0 && i.agent.QueuedMessageCount() == 0
46}
47
48func (i *Interactive) requestGoalContinuationIfIdle(parent context.Context) bool {
49	message, ok := i.goalContinuationMessage()
50	if !ok || i.coordinatorHasPendingWorkers() {
51		return false
52	}
53	i.mu.Lock()
54	if !i.goalIdleLocked() {
55		i.mu.Unlock()
56		return false
57	}
58	i.busy = true
59	i.mu.Unlock()
60	i.startGoalContinuation(parent, message)
61	return true
62}
63
64func (i *Interactive) startGoalContinuation(parent context.Context, message provider.Message) {
65	goalMessage, goalActive := i.goalContinuationMessage()
66	i.mu.Lock()
67	ag := i.agent
68	if ag == nil {
69		i.busy = false
70		pendingIdleWork := i.takePendingIdleWorkLocked()
71		i.mu.Unlock()
72		runPendingIdleWork(pendingIdleWork)
73		i.invalidate()
74		return
75	}
76	if len(i.queued) > 0 {
77		next := i.queued[0]
78		i.queued = i.queued[1:]
79		i.mu.Unlock()
80		i.startTurn(parent, next)
81		return
82	}
83	if ag.QueuedMessageCount() > 0 {
84		i.mu.Unlock()
85		i.startTurnRequest(parent, "", nil, true, false)
86		return
87	}
88	if !goalActive || userMessageText(goalMessage) != userMessageText(message) {
89		i.busy = false
90		pendingIdleWork := i.takePendingIdleWorkLocked()
91		i.mu.Unlock()
92		runPendingIdleWork(pendingIdleWork)
93		// The objective may have been replaced while the prior turn was
94		// finishing. Re-evaluate it now instead of waiting for another event;
95		// the idle gate still gives queued user work priority.
96		i.requestGoalContinuationIfIdle(parent)
97		i.invalidate()
98		return
99	}
100	i.mu.Unlock()
101	ag.AppendUserContext(userMessageText(message), message.Meta)
102
103	// Persistence callbacks run synchronously from AppendUserContext, so never
104	// hold the TUI mutex across it. Re-check user work that may have arrived
105	// while the hidden continuation was being persisted.
106	i.mu.Lock()
107	if len(i.queued) > 0 {
108		next := i.queued[0]
109		i.queued = i.queued[1:]
110		i.mu.Unlock()
111		i.startTurn(parent, next)
112		return
113	}
114	i.mu.Unlock()
115	i.startTurnRequest(parent, "", nil, true, false)
116}
117
118func copySessionGoal(goal *core.SessionGoal) *core.SessionGoal {
119	if goal == nil {
120		return nil
121	}
122	copyGoal := *goal
123	return &copyGoal
124}
125
126func (i *Interactive) updateActiveGoal(status core.GoalStatus, reason string) {
127	if i.cfg.CurrentGoal == nil || i.cfg.PersistGoal == nil {
128		return
129	}
130	goal := copySessionGoal(i.cfg.CurrentGoal())
131	if goal == nil || goal.Status != core.GoalActive {
132		return
133	}
134	goal.Status = status
135	goal.Reason = reason
136	if err := i.cfg.PersistGoal(goal); err != nil {
137		i.ReportError(fmt.Errorf("persist goal: %w", err))
138		return
139	}
140	i.setGoalStatus(goal)
141}
142
143func (i *Interactive) runGoalCommand(ctx context.Context, cmd string, parts []string) {
144	if i.cfg.CurrentGoal == nil || i.cfg.PersistGoal == nil {
145		i.setGoalCommandError("goal: session persistence is unavailable")
146		return
147	}
148
149	current := copySessionGoal(i.cfg.CurrentGoal())
150	if len(parts) == 2 && strings.EqualFold(parts[1], "history") {
151		if i.cfg.CurrentGoalHistory == nil {
152			i.setGoalCommandError("goal: history is unavailable")
153			return
154		}
155		history := i.cfg.CurrentGoalHistory()
156		if len(history) == 0 {
157			i.setGoalCommandStatus("no goal history")
158			return
159		}
160		entries := make([]string, 0, len(history))
161		for _, goal := range history {
162			owner := goal.Owner
163			if owner == "" {
164				owner = core.GoalOwnerUser
165			}
166			entry := fmt.Sprintf("%s (%s): %s", goal.Status, owner, goal.Objective)
167			if goal.Reason != "" {
168				entry += " (" + goal.Reason + ")"
169			}
170			entries = append(entries, entry)
171		}
172		i.setGoalCommandStatus("goal history: " + strings.Join(entries, " → "))
173		return
174	}
175	if len(parts) == 1 {
176		if current == nil {
177			i.setGoalCommandStatus("no autonomous goal")
178			return
179		}
180		owner := current.Owner
181		if owner == "" {
182			owner = core.GoalOwnerUser
183		}
184		status := fmt.Sprintf("goal %s (%s): %s", current.Status, owner, current.Objective)
185		if current.Reason != "" {
186			status += " (" + current.Reason + ")"
187		}
188		i.setGoalCommandStatus(status)
189		return
190	}
191
192	startIfIdle := false
193	startTitle := false
194	arg := ""
195	if len(parts) == 2 {
196		arg = strings.ToLower(parts[1])
197	}
198	switch arg {
199	case "pause":
200		if current == nil {
201			i.setGoalCommandError("goal: no goal to pause")
202			return
203		}
204		current.Status = core.GoalPaused
205		current.Reason = ""
206	case "resume":
207		if current == nil {
208			i.setGoalCommandError("goal: no goal to resume")
209			return
210		}
211		current.Status = core.GoalActive
212		current.Reason = ""
213		startIfIdle = true
214	case "clear":
215		if err := i.cfg.PersistGoal(nil); err != nil {
216			i.setGoalCommandError("goal: " + err.Error())
217			return
218		}
219		i.setGoalStatus(nil)
220		i.setGoalCommandStatus("autonomous goal cleared")
221		return
222	default:
223		objective := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(cmd), parts[0]))
224		if objective == "" {
225			i.setGoalCommandError("usage: /goal <objective>|pause|resume|clear|history")
226			return
227		}
228		current = &core.SessionGoal{Objective: objective, Status: core.GoalActive, Owner: core.GoalOwnerUser}
229		startIfIdle = true
230		startTitle = true
231	}
232
233	if startIfIdle && !i.goalUpdatesAvailable() {
234		i.setGoalCommandError("goal: update_goal is unavailable; enable the default tools")
235		return
236	}
237	if err := i.cfg.PersistGoal(current); err != nil {
238		i.setGoalCommandError("goal: " + err.Error())
239		return
240	}
241	i.setGoalStatus(current)
242	i.setGoalCommandStatus(fmt.Sprintf("autonomous goal %s: %s", current.Status, current.Objective))
243	if startTitle {
244		i.maybeStartSessionTitle(ctx, current.Objective)
245	}
246	if startIfIdle {
247		i.requestGoalContinuationIfIdle(ctx)
248	}
249}
250
251// RefreshGoal reloads the status badge after the CLI commits a session switch.
252func (i *Interactive) RefreshGoal() {
253	if i.cfg.CurrentGoal == nil {
254		i.setGoalStatus(nil)
255		return
256	}
257	i.setGoalStatus(i.cfg.CurrentGoal())
258}
259
260func (i *Interactive) setGoalStatus(goal *core.SessionGoal) {
261	status := core.GoalStatus("")
262	if goal != nil {
263		status = goal.Status
264	}
265	i.mu.Lock()
266	i.goalStatus = status
267	i.mu.Unlock()
268	i.invalidate()
269}
270
271func (i *Interactive) setGoalCommandStatus(message string) {
272	i.mu.Lock()
273	i.statusOK = message
274	i.statusErr = ""
275	i.mu.Unlock()
276	i.invalidate()
277}
278
279func (i *Interactive) setGoalCommandError(message string) {
280	i.mu.Lock()
281	i.statusOK = ""
282	i.statusErr = message
283	i.mu.Unlock()
284	i.invalidate()
285}

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