Let agents cancel open questions; fix snapshots missing same-size edits

This commit is contained in:
Ian Gulliver
2026-10-14 22:57:01 -07:00
parent 9039860594
commit f5333586fe
14 changed files with 269 additions and 107 deletions
+82
View File
@@ -3,8 +3,10 @@ package agents
import (
"context"
"encoding/json"
"path/filepath"
"strings"
"testing"
"time"
"g.fc.run/misc/byre/internal/api"
"g.fc.run/misc/byre/internal/store"
@@ -52,6 +54,86 @@ func TestValidateAsk(t *testing.T) {
}
}
func TestCancelQuestion(t *testing.T) {
ctx := context.Background()
st, err := store.Open(filepath.Join(t.TempDir(), "byre.db"), func(api.Event) {})
if err != nil {
t.Fatalf("open store: %v", err)
}
t.Cleanup(func() { st.Close() })
now := time.Now().UTC()
_, err = st.EnsureUser(ctx, "alice", "Alice")
if err != nil {
t.Fatalf("ensure user: %v", err)
}
err = st.CreateProject(ctx, api.Project{Id: "p1", Name: "demo", RepoPath: "/nonexistent", DefaultBranch: "main", Remote: "none", CreatedBy: "alice", CreatedAt: now})
if err != nil {
t.Fatalf("create project: %v", err)
}
err = st.CreateTask(ctx, api.Task{Id: "t1", ProjectId: "p1", Title: "Fix the thing", Branch: "b", WorktreePath: "/nonexistent", BaseRef: "x", Status: api.TaskStatusActive, CreatedBy: "alice", CreatedAt: now})
if err != nil {
t.Fatalf("create task: %v", err)
}
err = st.CreateSession(ctx, api.AgentSession{Id: "s1", TaskId: "t1", Vendor: api.Claude, VendorSessionId: "s1", State: api.AgentSessionStateIdle, Role: api.Implementer, CreatedBy: "alice", CreatedAt: now}, "p1")
if err != nil {
t.Fatalf("create session: %v", err)
}
m := New(st, DefaultLimits, t.TempDir())
l := &live{id: "s1", dir: t.TempDir(), vendor: api.Claude, taskID: "t1", projectID: "p1"}
other := &live{id: "s2", dir: t.TempDir(), vendor: api.Claude, taskID: "t1", projectID: "p1"}
yes := true
raw, err := json.Marshal(askArgs{Context: "c", Question: "Red or blue?", WhyBlocked: "w", Options: []string{"red", "blue"}, RecommendedOption: "red", Blocking: &yes})
if err != nil {
t.Fatalf("marshal: %v", err)
}
out, err := m.ask(ctx, l, raw)
if err != nil {
t.Fatalf("ask: %v", err)
}
items, err := st.ListInbox(ctx, store.InboxFilter{SessionID: "s1", State: api.Open})
if err != nil || len(items) != 1 {
t.Fatalf("inbox after ask: %+v %v", items, err)
}
id := items[0].Id
if !strings.Contains(out, "question_id: "+id) || !strings.Contains(out, "cancel_question") {
t.Fatalf("ask result doesn't give the id and cancel_question: %s", out)
}
cancel := func(l *live, id string) error {
raw, err := json.Marshal(cancelArgs{QuestionID: id})
if err != nil {
t.Fatalf("marshal: %v", err)
}
_, err = m.cancelQuestion(ctx, l, raw)
return err
}
if cancel(l, "nope") == nil {
t.Fatalf("cancelled a question that doesn't exist")
}
if cancel(other, id) == nil {
t.Fatalf("cancelled another session's question")
}
err = cancel(l, id)
if err != nil {
t.Fatalf("cancel: %v", err)
}
item, err := st.GetInboxItem(ctx, id)
if err != nil || item.State != api.Dismissed || item.ResolvedBy != nil || item.ResolvedAt == nil {
t.Fatalf("after cancel: %+v %v", item, err)
}
waiting, err := st.AwaitingHuman(ctx, "s1")
if err != nil || waiting != 0 {
t.Fatalf("still awaiting human after cancel: %d %v", waiting, err)
}
if cancel(l, id) == nil {
t.Fatalf("cancelled a question twice")
}
}
func TestAsksQuestion(t *testing.T) {
cases := []struct {
name string