v0.51.0 a9d674e

plan_modes_persist_test.go RawHistoryFolder

1package agent
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"io"
8	"net/http"
9	"net/http/httptest"
10	"sync/atomic"
11	"testing"
12
13	"github.com/bnema/zut/packages/agent/scheduler"
14	"github.com/bnema/zut/packages/core"
15	"github.com/bnema/zut/packages/provider"
16)
17
18// TestBotSessionPersistsPlanMutation guards GO-002 for the bot mode: the bot
19// session must persist a plan mutation through the tool-result commit path so
20// the next resume seeds from the file instead of an empty core state.
21func TestBotSessionPersistsPlanMutation(t *testing.T) {
22	t.Setenv("ZUT_HOME", t.TempDir())
23	ag := core.NewAgent(nil, "model", "", nil)
24
25	sess, _, err := openOrCreateSessionForBot(Args{}, Resolved{Provider: "provider", Model: "model"}, ag, "test")
26	if err != nil {
27		t.Fatal(err)
28	}
29	t.Cleanup(func() { _ = sess.Close() })
30	if ag.CommitToolResult == nil {
31		t.Fatal("bot session did not wire CommitToolResult")
32	}
33
34	want := []core.PlanStep{{Step: "one", Status: core.PlanPending}}
35	if err := ag.CommitToolResult("plan-call", planMutationResult(want)); err != nil {
36		t.Fatal(err)
37	}
38	if sess.Meta.Plan == nil {
39		t.Fatal("plan mutation did not write SessionMeta.Plan")
40	}
41
42	path := sess.Path
43	if err := sess.Close(); err != nil {
44		t.Fatal(err)
45	}
46	reopened, _, err := core.OpenSession(path)
47	if err != nil {
48		t.Fatal(err)
49	}
50	defer reopened.Close()
51	if reopened.Meta.Plan == nil {
52		t.Fatal("reopened bot session lost the persisted plan")
53	}
54}
55
56// TestScheduledSessionPersistsPlanMutation guards GO-002 for the scheduled mode:
57// a scheduled turn that calls plan must persist the mutation to the session it
58// reconstructed.
59func TestScheduledSessionDisablesRepetitionGuard(t *testing.T) {
60	home := t.TempDir()
61	cwd := t.TempDir()
62	t.Setenv("ZUT_HOME", home)
63	sess, err := core.NewSession(home, cwd, "openai", "gpt-5", "test")
64	if err != nil {
65		t.Fatal(err)
66	}
67	id, path := sess.Meta.ID, sess.Path
68	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69		_, _ = io.ReadAll(r.Body)
70		w.Header().Set("content-type", "text/event-stream")
71		chunk := map[string]any{
72			"choices": []any{map[string]any{"index": 0, "delta": map[string]string{"content": "done"}, "finish_reason": "stop"}},
73			"usage":   map[string]int{"prompt_tokens": 8, "completion_tokens": 2},
74		}
75		data, _ := json.Marshal(chunk)
76		_, _ = fmt.Fprintf(w, "data: %s\n\ndata: [DONE]\n\n", data)
77	}))
78	defer server.Close()
79
80	args := Args{Provider: "openai", Model: "gpt-5", BaseURL: server.URL, APIKey: "test-key"}
81	base, err := Resolve(args, true)
82	if err != nil {
83		t.Fatal(err)
84	}
85	managedPath, err := core.ImportSession(path, home, cwd, "test")
86	if err != nil {
87		t.Fatal(err)
88	}
89	if err := runScheduledSession(context.Background(), scheduler.Task{ID: "task-guard", SessionID: id, Message: "scheduled task"}, args, base, nil, nil); err != nil {
90		t.Fatal(err)
91	}
92	reopened, messages, err := core.OpenSession(managedPath)
93	if err != nil {
94		t.Fatal(err)
95	}
96	defer reopened.Close()
97	for _, message := range messages {
98		if message.Meta[core.RepetitionGuardMetaKey] == "true" {
99			t.Fatalf("scheduled run unexpectedly injected repetition warning: %#v", message)
100		}
101	}
102}
103
104func TestScheduledSessionPersistsPlanMutation(t *testing.T) {
105	home := t.TempDir()
106	t.Setenv("ZUT_HOME", home)
107	server := scheduledPlanProvider(t)
108	defer server.Close()
109
110	sess, err := core.NewSession(home, t.TempDir(), "openai", "gpt-5", "test")
111	if err != nil {
112		t.Fatal(err)
113	}
114	if err := sess.AppendMessage(provider.Message{
115		Role:    provider.RoleUser,
116		Content: []provider.Content{provider.TextBlock{Text: "scheduled task"}},
117	}); err != nil {
118		t.Fatal(err)
119	}
120	id := sess.Meta.ID
121	path := sess.Path
122	if err := sess.Close(); err != nil {
123		t.Fatal(err)
124	}
125
126	args := Args{Provider: "openai", Model: "gpt-5", BaseURL: server.URL, APIKey: "test-key"}
127	base, err := Resolve(args, true)
128	if err != nil {
129		t.Fatal(err)
130	}
131	task := scheduler.Task{ID: "task-1", SessionID: id, Message: "scheduled task"}
132	if err := runScheduledSession(context.Background(), task, args, base, nil, nil); err != nil {
133		t.Fatal(err)
134	}
135
136	reopened, _, err := core.OpenSession(path)
137	if err != nil {
138		t.Fatal(err)
139	}
140	defer reopened.Close()
141	if reopened.Meta.Plan == nil {
142		t.Fatal("scheduled session did not persist the plan mutation")
143	}
144}
145
146// scheduledPlanProvider answers the first request with a plan tool call and the
147// follow-up with a final answer, matching the OpenAI streaming shape the
148// provider parses.
149func scheduledPlanProvider(t *testing.T) *httptest.Server {
150	t.Helper()
151	var calls atomic.Int32
152	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
153		_, _ = io.ReadAll(r.Body)
154		w.Header().Set("content-type", "text/event-stream")
155		if calls.Add(1) == 1 {
156			arguments := `{"action":"set","steps":[{"step":"one","status":"pending"}]}`
157			chunk := map[string]any{
158				"choices": []any{map[string]any{
159					"index": 0,
160					"delta": map[string]any{"tool_calls": []any{map[string]any{
161						"index":    0,
162						"id":       "plan-call-1",
163						"type":     "function",
164						"function": map[string]string{"name": "plan", "arguments": arguments},
165					}}},
166					"finish_reason": "tool_calls",
167				}},
168				"usage": map[string]int{"prompt_tokens": 8, "completion_tokens": 2},
169			}
170			data, _ := json.Marshal(chunk)
171			_, _ = fmt.Fprintf(w, "data: %s\n\ndata: [DONE]\n\n", data)
172			return
173		}
174		chunk := map[string]any{
175			"choices": []any{map[string]any{"index": 0, "delta": map[string]string{"content": "done"}, "finish_reason": "stop"}},
176			"usage":   map[string]int{"prompt_tokens": 8, "completion_tokens": 2},
177		}
178		data, _ := json.Marshal(chunk)
179		_, _ = fmt.Fprintf(w, "data: %s\n\ndata: [DONE]\n\n", data)
180	}))
181}

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