v0.46.0 ea0e112

subagent_status_test.go RawHistoryFolder

1package tools
2
3import (
4	"context"
5	"encoding/json"
6	"os"
7	"path/filepath"
8	"strings"
9	"testing"
10
11	"github.com/bnema/zut/packages/agent/subagents"
12	"github.com/bnema/zut/packages/core"
13)
14
15func TestResidentStatusWatchReportsLiveActivity(t *testing.T) {
16	ready := make(chan struct{})
17	release := make(chan struct{})
18	manager := subagents.NewResidentManager(t.TempDir(), func(_ subagents.ResidentChildSpec, journal *subagents.ResidentJournal) (subagents.ResidentRuntime, error) {
19		return subagents.ResidentTurnRunner(func(ctx context.Context, _ string) error {
20			_ = journal.RecordAgentEvent(core.EvTextDelta{Delta: "Reading the parser"})
21			_ = journal.RecordAgentEvent(core.EvToolExecutionStarted{ID: "call-1", Name: "read"})
22			close(ready)
23			select {
24			case <-release:
25				return nil
26			case <-ctx.Done():
27				return ctx.Err()
28			}
29		}), nil
30	})
31	t.Cleanup(func() { _ = manager.Close(context.Background()) })
32	spec := subagents.ResidentChildSpec{ID: "watched", InitialTurnID: "initial", SessionID: "session", Provider: "openai", Model: "test"}
33	if _, err := manager.Spawn(t.Context(), spec, "investigate"); err != nil {
34		t.Fatal(err)
35	}
36	<-ready
37	tool := &SubagentStatusTool{ResidentManager: manager, Enabled: func() bool { return true }}
38
39	result, err := tool.Execute(t.Context(), json.RawMessage(`{"agent_id":"watched","watch":1}`), nil)
40	if err != nil || result.IsError {
41		t.Fatalf("result = %#v, error = %v", result, err)
42	}
43	var response subagentStatusResponse
44	if err := json.Unmarshal([]byte(toolResultText(t, result)), &response); err != nil {
45		t.Fatal(err)
46	}
47	if response.Watch == nil || len(response.Watch.Timeline) == 0 || response.Watch.EndState != subagents.ResidentRunning {
48		t.Fatalf("watch = %#v", response.Watch)
49	}
50	activity := response.Activity
51	if activity == nil || activity.Phase != "tools" || activity.Text != "Reading the parser" || len(activity.Tools) != 1 || activity.Tools[0].Name != "read" {
52		t.Fatalf("activity = %#v", activity)
53	}
54
55	close(release)
56	result, err = tool.Execute(t.Context(), json.RawMessage(`{"agent_id":"watched","watch":60}`), nil)
57	if err != nil || result.IsError {
58		t.Fatalf("result = %#v, error = %v", result, err)
59	}
60	response = subagentStatusResponse{}
61	if err := json.Unmarshal([]byte(toolResultText(t, result)), &response); err != nil {
62		t.Fatal(err)
63	}
64	if response.Watch == nil || response.Watch.EndState == subagents.ResidentRunning || response.Watch.Seconds >= 30 || response.Activity != nil {
65		t.Fatalf("watch did not end with the turn: %#v", response)
66	}
67}
68
69func TestResidentStatusWatchValidation(t *testing.T) {
70	manager := subagents.NewResidentManager(t.TempDir(), nil)
71	t.Cleanup(func() { _ = manager.Close(context.Background()) })
72	tool := &SubagentStatusTool{ResidentManager: manager, Enabled: func() bool { return true }}
73	result, err := tool.Execute(t.Context(), json.RawMessage(`{"watch":5}`), nil)
74	if err != nil || !result.IsError || !strings.Contains(toolResultText(t, result), "watch requires agent_id") {
75		t.Fatalf("result = %#v, error = %v", result, err)
76	}
77}
78
79func TestResidentStatusResultReadErrors(t *testing.T) {
80	for _, scenario := range []struct {
81		name string
82		want string
83	}{
84		{"missing", "no saved result exists yet"},
85		{"foreign", "owned by another zut process"},
86		{"corrupt", "could not read or decode the saved result"},
87	} {
88		t.Run(scenario.name, func(t *testing.T) {
89			root := t.TempDir()
90			manager := subagents.NewResidentManager(root, func(subagents.ResidentChildSpec, *subagents.ResidentJournal) (subagents.ResidentRuntime, error) {
91				return subagents.ResidentTurnRunner(func(ctx context.Context, _ string) error {
92					<-ctx.Done()
93					return ctx.Err()
94				}), nil
95			})
96			t.Cleanup(func() { _ = manager.Close(context.Background()) })
97			spec := subagents.ResidentChildSpec{ID: "status-error", InitialTurnID: "initial", SessionID: "session", Provider: "openai", Model: "test"}
98			if _, err := manager.Spawn(t.Context(), spec, "investigate"); err != nil {
99				t.Fatal(err)
100			}
101			statusManager := manager
102			if scenario.name == "foreign" {
103				statusManager = subagents.NewResidentManager(root, nil)
104				t.Cleanup(func() { _ = statusManager.Close(context.Background()) })
105				if errs := statusManager.Reconcile(); len(errs) != 0 {
106					t.Fatal(errs)
107				}
108			}
109			if scenario.name == "corrupt" {
110				if err := os.WriteFile(filepath.Join(root, spec.ID, "result.json"), []byte("private malformed payload"), 0o600); err != nil {
111					t.Fatal(err)
112				}
113			}
114			tool := &SubagentStatusTool{ResidentManager: statusManager, Enabled: func() bool { return true }}
115			result, err := tool.Execute(t.Context(), json.RawMessage(`{"agent_id":"status-error","include_result":true}`), nil)
116			if err != nil || !result.IsError {
117				t.Fatalf("result = %#v, error = %v", result, err)
118			}
119			text := toolResultText(t, result)
120			if !strings.Contains(text, scenario.want) || strings.Contains(text, root) || strings.Contains(text, "private") {
121				t.Fatalf("unexpected error text: %q", text)
122			}
123		})
124	}
125}

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 2

Tags 477

↑↓ moveenter openesc close