v0.35.0 21873e8

completion.go RawHistoryFolder

1package subagents
2
3import (
4	"context"
5	"errors"
6	"fmt"
7	"strings"
8	"sync"
9)
10
11// Completion is one resident child's terminal turn outcome.
12type Completion struct {
13	AgentID string
14	TurnID  string
15	Status  string
16	Task    string
17	Error   string
18	Summary string
19}
20
21// Completion projects a typed resident outcome into the parent notification
22// shared by interactive and orchestrated modes.
23func (c ResidentCompletion) Completion() Completion {
24	result := Completion{AgentID: c.ChildID, TurnID: c.TurnID, Status: string(ResidentCompleted), Task: c.Task, Summary: c.Summary}
25	if c.Err != nil {
26		result.Status, result.Error = string(ResidentFailed), c.Err.Error()
27		if errors.Is(c.Err, context.Canceled) {
28			result.Status = string(ResidentInterrupted)
29		}
30	}
31	return result
32}
33
34// CompletionTracker coordinates resident child completions with the parent
35// turn. It has no process or worker-event dependency.
36type CompletionTracker struct {
37	mu      sync.Mutex
38	pending map[string]struct{}
39	ready   []Completion
40	changed chan struct{}
41}
42
43func NewCompletionTracker() *CompletionTracker {
44	return &CompletionTracker{changed: make(chan struct{}), pending: make(map[string]struct{})}
45}
46
47// TrackResident records an accepted resident turn. It returns false when the
48// turn is incomplete or already tracked.
49func (t *CompletionTracker) TrackResident(agentID, turnID string) bool {
50	if t == nil {
51		return false
52	}
53	key := completionKey(agentID, turnID)
54	if key == "" {
55		return false
56	}
57	t.mu.Lock()
58	defer t.mu.Unlock()
59	if _, exists := t.pending[key]; exists {
60		return false
61	}
62	t.pending[key] = struct{}{}
63	t.signalLocked()
64	return true
65}
66
67// Report accepts one terminal completion for an accepted resident turn. It
68// returns false for duplicate or unknown terminal reports.
69func (t *CompletionTracker) Report(completion Completion) bool {
70	if t == nil {
71		return false
72	}
73	key := completionKey(completion.AgentID, completion.TurnID)
74	if key == "" {
75		return false
76	}
77	t.mu.Lock()
78	defer t.mu.Unlock()
79	if _, exists := t.pending[key]; !exists {
80		return false
81	}
82	delete(t.pending, key)
83	t.ready = append(t.ready, completion)
84	t.signalLocked()
85	return true
86}
87
88func completionKey(agentID, turnID string) string {
89	if agentID == "" || turnID == "" {
90		return ""
91	}
92	return agentID + "\x00" + turnID
93}
94
95func (t *CompletionTracker) Pending() int {
96	if t == nil {
97		return 0
98	}
99	t.mu.Lock()
100	defer t.mu.Unlock()
101	return len(t.pending)
102}
103
104// Reset drops buffered outcomes at a parent-turn cancellation boundary.
105func (t *CompletionTracker) Reset() {
106	if t == nil {
107		return
108	}
109	t.mu.Lock()
110	t.pending = make(map[string]struct{})
111	t.ready = nil
112	t.signalLocked()
113	t.mu.Unlock()
114}
115
116func (t *CompletionTracker) WaitIdle(ctx context.Context) ([]Completion, error) {
117	return t.wait(ctx, true)
118}
119
120// WaitReady returns available completions without waiting for sibling turns.
121// It returns an empty batch when no accepted turns remain.
122func (t *CompletionTracker) WaitReady(ctx context.Context) ([]Completion, error) {
123	return t.wait(ctx, false)
124}
125
126func (t *CompletionTracker) wait(ctx context.Context, idle bool) ([]Completion, error) {
127	if t == nil {
128		return nil, nil
129	}
130	for {
131		t.mu.Lock()
132		if len(t.pending) == 0 || (!idle && len(t.ready) != 0) {
133			ready := append([]Completion(nil), t.ready...)
134			t.ready = nil
135			t.mu.Unlock()
136			return ready, nil
137		}
138		changed := t.changed
139		t.mu.Unlock()
140		select {
141		case <-ctx.Done():
142			return nil, ctx.Err()
143		case <-changed:
144		}
145	}
146}
147
148func (t *CompletionTracker) signalLocked() { close(t.changed); t.changed = make(chan struct{}) }
149
150func FormatCompletionUpdate(batch []Completion, instruction string) string {
151	if len(batch) == 0 {
152		return ""
153	}
154	var b strings.Builder
155	b.WriteString("[auto-subagents update]\n")
156	for _, completion := range batch {
157		fmt.Fprintf(&b, "- %s: %s", completion.AgentID, completion.Status)
158		if completion.Error != "" {
159			fmt.Fprintf(&b, " (%s)", completion.Error)
160		}
161		if completion.Task != "" {
162			fmt.Fprintf(&b, " — %s", completion.Task)
163		}
164		if completion.Summary != "" {
165			label := "final"
166			if completion.Status != string(ResidentCompleted) {
167				label = "partial"
168			}
169			fmt.Fprintf(&b, "\n  %s: %s", label, completion.Summary)
170		}
171		b.WriteByte('\n')
172	}
173	if instruction != "" {
174		b.WriteString(instruction)
175	}
176	return strings.TrimSpace(b.String())
177}

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