package thintest import ( "context" "encoding/json" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // toolRig is a rig with a fake runner holding a running turn. type toolRig struct { *rig runner *FakeRunner turn string seq int // attempt is the turn attempt calls belong to (0: 1). attempt int64 // held are frames other than tool results that arrived during a call, // for expect. held []heldFrame // placeOnMerge: a merge confirms the place card it raises (sitesOn). placeOnMerge bool } type heldFrame struct { f wire.Frame body any raw []byte } func newToolRig(t *testing.T) *toolRig { return newToolRigAt(t, wire.Protocol1) } // primaryAt is a Linux primary's protocol 2+ hello fields. func primaryAt(h wire.Hello, protocol int) wire.Hello { h.Protocol, h.Platform, h.Label, h.Sandbox = protocol, &wire.Platform{OS: "linux", Arch: "amd64"}, "docs-t1", wire.SandboxBubblewrap h.Capabilities = &wire.Capabilities{Tags: []string{wire.TagLinux, wire.TagClaude, wire.TagCodex, wire.TagGo}} return h } // newToolRigAt is a tool rig whose runner speaks protocol: 1 is the // slice 4 wire; wire.ProtocolMergeFacts lets Equipo merge on its own. func newToolRigAt(t *testing.T, protocol int) *toolRig { return newToolRigSaying(t, protocol, "hello") } // newToolRigSaying is newToolRigAt whose turn is the person's message text. func newToolRigSaying(t *testing.T, protocol int, text string) *toolRig { r := newRig(t) // Housekeeping's clock runs from midday UTC today, so no test meets // the default quiet hours (22:00 to 08:00) because of when it runs. r.inst.Store.Chat().SetHousekeepingClock(daytime()) var runner *FakeRunner if protocol <= wire.Protocol1 { runner = r.dial(0) } else { f, err := DialRunnerHello(r.ctx, r.inst.URL, r.runner, primaryAt(wire.Hello{Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, protocol)) if err != nil { t.Fatal(err) } t.Cleanup(f.Close) runner = f } hello := toolHello(runner.Welcome.Generation) if protocol > wire.Protocol1 { hello = primaryAt(hello, protocol) } if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { t.Fatal(err) } if protocol <= wire.Protocol1 { shape(t, "frames/hello.json", runner.Sent[len(runner.Sent)-1]) } r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) sent, err := r.client.Send(r.ctx, text, "k-tools") if err != nil { t.Fatal(err) } f, _, err := runner.Expect(r.ctx, wire.FrameMessage) if err != nil { t.Fatal(err) } if err = runner.Send(r.ctx, "a-msg", wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { t.Fatal(err) } r.wait("running", func(c *Conversation) bool { return c.Turns[sent.Turn.ID].State == wire.TurnRunning }) return &toolRig{rig: r, runner: runner, turn: sent.Turn.ID} } // call sends one tool_call and returns the tool_result, acking it. func (r *toolRig) call(id, name string, args any) wire.ToolResult { r.t.Helper() raw, _ := json.Marshal(args) r.seq++ frame := "r-tc-" + id + "-" + itoa(r.seq) attempt := max(r.attempt, 1) if err := r.runner.Send(r.ctx, frame, wire.FrameToolCall, wire.ToolCall{CallID: r.turn + "/" + itoa(int(attempt)) + "/" + id, TurnID: r.turn, Attempt: attempt, Name: name, Args: raw}); err != nil { r.t.Fatal(err) } for { f, body, err := r.runner.Next(r.ctx) if err != nil { r.t.Fatal(err) } if f.Type != wire.FrameAck && f.Type != wire.FrameToolResult { r.held = append(r.held, heldFrame{f, body, r.runner.Received[len(r.runner.Received)-1]}) continue } if res, ok := body.(*wire.ToolResult); ok { shape(r.t, "frames/tool_result.json", r.runner.Received[len(r.runner.Received)-1]) if err := r.runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { r.t.Fatal(err) } return *res } } } func itoa(n int) string { raw, _ := json.Marshal(n); return string(raw) } func TestContractTools(t *testing.T) { r := newToolRigSaying(t, wire.Protocol1, "Create a new project for the site.") shape(t, "frames/tool_call.json", func() []byte { raw, _ := json.Marshal(wire.Frame{ID: "x", Type: wire.FrameToolCall, Data: json.RawMessage(`{"call_id":"c","turn_id":"t","attempt":1,"name":"add_project","args":{"repo":"oficina/sample"}}`)}) return raw }()) // Not on the runner's allowlist: refused, nothing added. res := r.call("u1", wire.ToolAddProject, map[string]string{"repo": "oficina/secret"}) if !res.IsError || res.Code != "not_allowed" || !strings.Contains(res.Text, "oficina/equipo, oficina/sample") { t.Fatalf("not allowed: %+v", res) } res = r.call("u2", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) if res.IsError || res.Code != "added" || res.Repo != "amadan.net/oficina/sample" { t.Fatalf("add: %+v", res) } r.wait("card and project", func(c *Conversation) bool { return len(c.Cards) == 1 && len(c.Projects) == 1 }) // The same call id again returns the stored result and adds nothing. again := r.call("u2", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) if again != res { t.Fatalf("repeat: %+v vs %+v", again, res) } snap, _ := r.client.Snapshot(r.ctx) if len(snap.Cards) != 1 || len(snap.Projects) != 1 { t.Fatalf("repeat added: %+v", snap) } shape(t, "api/snapshot.json", snap) list, err := r.client.Projects(r.ctx) if err != nil || len(list.Items) != 1 || len(list.Allowed) != 2 { t.Fatalf("projects %+v %v", list, err) } shape(t, "api/projects.json", list) status := r.call("u3", wire.ToolStatus, map[string]string{}) for _, want := range []string{`"projects":[{"name":"oficina/sample"}]`, `"can_add":["oficina/equipo","oficina/sample"]`, `"create_in":["oficina"]`} { if !strings.Contains(status.Text, want) { t.Fatalf("status lacks %s: %s", want, status.Text) } } if strings.Contains(status.Text, "token") || strings.Contains(status.Text, "/home/") { t.Fatalf("status leaks: %s", status.Text) } found := r.call("u4", wire.ToolFind, map[string]string{"query": "oficina/sample"}) if found.Code != "found" || !strings.Contains(found.Text, "oficina/sample") { t.Fatalf("find: %+v", found) } // On the runner's list but not read yet: said so, not "unreachable". if res := r.call("u5", wire.ToolReadFile, map[string]string{"repo": "oficina/equipo", "path": "README.md"}); !res.IsError || res.Code != "not_indexed" { t.Fatalf("read outside the catalog: %+v", res) } if res := r.call("u5b", wire.ToolReadFile, map[string]string{"repo": "oficina/secret", "path": "README.md"}); !res.IsError || res.Code != "unreachable" { t.Fatalf("read outside the allowlist: %+v", res) } if res := r.call("u6", wire.ToolSearchCode, map[string]string{"repo": "oficina/sample", "query": "Edge"}); res.IsError || res.Code != "read" || res.Repo != "amadan.net/oficina/sample" { t.Fatalf("read: %+v", res) } if res := r.call("u7", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/carlos/site"}); !res.IsError || res.Code != "no_create" { t.Fatalf("create outside create_in: %+v", res) } // project_created is accepted only after this turn authorised it. if res := r.call("u8/created", wire.ToolProjectCreated, map[string]string{"repo": "amadan.net/oficina/site"}); !res.IsError { t.Fatalf("unauthorised created: %+v", res) } if res := r.call("u9", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/site"}); res.Code != "create" { t.Fatalf("authorise: %+v", res) } if res := r.call("u9/created", wire.ToolProjectCreated, map[string]string{"repo": "amadan.net/oficina/site"}); res.Code != "created" { t.Fatalf("created: %+v", res) } q := r.call("u10", wire.ToolAskPerson, map[string]any{"question": "Slide on iPad too?", "options": []string{"Yes", "No"}}) if q.Code != "asked" { t.Fatalf("ask: %+v", q) } var card wire.Card r.wait("question card", func(c *Conversation) bool { for _, k := range c.Cards { if k.Kind == wire.CardQuestion { card = k } } return card.ID != "" }) shape(t, "api/card.json", card) ans, err := r.client.Answer(r.ctx, card.ID, "Yes", card.Revision) if err != nil { t.Fatal(err) } shape(t, "api/answered.json", ans) _, err = r.client.Answer(r.ctx, card.ID, "Yes", ans.Card.Revision) st, ok := err.(*ErrStatus) if !ok || st.Status != 409 { t.Fatalf("second answer %v", err) } shape(t, "api/error-card-conflict.json", st.Body) // Activity becomes an event with the copy label, never the input. if err := r.runner.Send(r.ctx, "r-a-1", wire.FrameActivity, wire.ActivityReport{TurnID: r.turn, Attempt: 1, Kind: wire.ActivityTool, Tool: wire.ToolAddProject}); err != nil { t.Fatal(err) } shape(t, "frames/activity.json", r.runner.Sent[len(r.runner.Sent)-1]) // Events from the start match the fixtures. s, err := r.client.Open(r.ctx, 0) if err != nil { t.Fatal(err) } defer s.Close() kinds := map[string]bool{} deadline := time.After(5 * time.Second) for !kinds[wire.EventActivity] || !kinds[wire.EventCard] || !kinds[wire.EventProject] { select { case e := <-s.Events: raw, _ := json.Marshal(e) switch e.Type { case wire.EventCard: shape(t, "api/event-card.json", raw) case wire.EventProject: shape(t, "api/event-project.json", raw) case wire.EventActivity: shape(t, "api/event-activity.json", raw) var a wire.Activity _ = json.Unmarshal(e.Data, &a) if (a.Label != "Adding a project…" && a.Label != "Adding oficina/sample…") || a.Tool != wire.ToolAddProject { t.Fatalf("activity %+v", a) } } kinds[e.Type] = true case <-deadline: t.Fatalf("events seen %v", kinds) } } // After the turn ends, its tools do nothing. if err := r.runner.Send(r.ctx, "r-reply", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Done."}); err != nil { t.Fatal(err) } r.wait("replied", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) if res := r.call("u11", wire.ToolAddProject, map[string]string{"repo": "oficina/equipo"}); res.Code != "turn_ended" { t.Fatalf("after the turn: %+v", res) } } func TestContractProjectsAPI(t *testing.T) { r := newToolRig(t) if _, err := r.client.AddProject(r.ctx, "oficina/secret"); Status(err) != 422 { t.Fatalf("not allowed: %v", err) } p, err := r.client.AddProject(r.ctx, "oficina/equipo") if err != nil || p.Name != "oficina/equipo" || p.State != wire.ProjectActive { t.Fatalf("add %+v %v", p, err) } shape(t, "api/project.json", p) gone, err := r.client.RemoveProject(r.ctx, p.ID) if err != nil || gone.State != wire.ProjectRemoved || gone.Revision != p.Revision+1 { t.Fatalf("remove %+v %v", gone, err) } if again, err := r.client.RemoveProject(r.ctx, p.ID); err != nil || again.Revision != gone.Revision { t.Fatalf("remove twice %+v %v", again, err) } if _, err := r.client.RemoveProject(r.ctx, "p_missing"); Status(err) != 404 { t.Fatalf("missing: %v", err) } // A teammate is not an owner: they cannot add or remove projects. if _, err := r.inst.DB.Writer().Exec("UPDATE members SET role = 'member' WHERE address = ?", Owner); err != nil { t.Fatal(err) } if _, err := r.client.AddProject(r.ctx, "oficina/equipo"); Status(err) != 403 { t.Fatalf("member add: %v", err) } } // Every coordinator tool has an activity label. func TestActivityLabels(t *testing.T) { r := newToolRig(t) for i, tool := range wire.ConverseTools { if err := r.runner.Send(r.ctx, "r-al-"+itoa(i), wire.FrameActivity, wire.ActivityReport{TurnID: r.turn, Attempt: 1, Kind: wire.ActivityTool, Tool: tool}); err != nil { t.Fatal(err) } } r.wait("labels", func(c *Conversation) bool { return len(c.Activity[r.turn]) == len(wire.ConverseTools) }) r.conv.Locked(func(c *Conversation) { for _, a := range c.Activity[r.turn] { if a.Label == "" || a.Label == "Working…" { t.Fatalf("no label for %s: %q", a.Tool, a.Label) } } }) } // Review fix 1: a stored authorisation is not returned after the owner // is demoted mid-turn, and a call id cannot be replayed for another tool, // arguments or attempt. func TestToolReplayRevalidates(t *testing.T) { r := newToolRigSaying(t, wire.Protocol1, "Create a new project for the site.") if res := r.call("rv1", wire.ToolSearchCode, map[string]string{"repo": "oficina/sample", "query": "x"}); res.Code != "read" { t.Fatalf("read: %+v", res) } if res := r.call("rv2", wire.ToolStatus, map[string]string{}); res.Code != "ok" { t.Fatalf("status: %+v", res) } // Same call id, other tool or arguments: refused, not the stored result. if res := r.call("rv1", wire.ToolSearchCode, map[string]string{"repo": "oficina/sample", "query": "other"}); !res.IsError || res.Code != "invalid" { t.Fatalf("replay with other arguments: %+v", res) } // A stored create authorisation is not returned once the turn ended. if res := r.call("rv3", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/site"}); res.Code != "create" { t.Fatalf("authorise: %+v", res) } if err := r.runner.Send(r.ctx, "r-reply-rv", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Done."}); err != nil { t.Fatal(err) } r.wait("replied", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) if res := r.call("rv3", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/site"}); res.Code != "turn_ended" { t.Fatalf("replay after the turn ended: %+v", res) } if _, err := r.inst.DB.Writer().Exec("UPDATE members SET role = 'member' WHERE address = ?", Owner); err != nil { t.Fatal(err) } // A demoted person's runner holds no turn: the replay gets no // authorisation back (no result is delivered to it at all). raw, _ := json.Marshal(map[string]string{"repo": "oficina/sample", "query": "x"}) if err := r.runner.Send(r.ctx, "r-tc-rv1-again", wire.FrameToolCall, wire.ToolCall{CallID: r.turn + "/1/rv1", TurnID: r.turn, Attempt: 1, Name: wire.ToolSearchCode, Args: raw}); err != nil { t.Fatal(err) } ctx, cancel := context.WithTimeout(r.ctx, 2*time.Second) defer cancel() for { _, body, err := r.runner.Next(ctx) if err != nil { return } if res, ok := body.(*wire.ToolResult); ok && (res.Code == "read" || !res.IsError) { t.Fatalf("replay after demotion kept authority: %+v", res) } } } // Review fix 3: an answer dispatched at once carries its question; a // Change carries the project it replaces. func TestAnswerDispatchCarriesContext(t *testing.T) { r := newToolRig(t) if res := r.call("q1", wire.ToolAskPerson, map[string]any{"question": "Slide on iPad too?", "options": []string{"Yes", "No"}}); res.Code != "asked" { t.Fatalf("ask: %+v", res) } if res := r.call("q2", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}); res.Code != "added" { t.Fatalf("add: %+v", res) } if err := r.runner.Send(r.ctx, "r-reply-q", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Asked."}); err != nil { t.Fatal(err) } r.wait("replied", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) var question, project wire.Card r.conv.Locked(func(c *Conversation) { for _, k := range c.Cards { if k.Kind == wire.CardQuestion { question = k } else if k.Kind == wire.CardProject { project = k } } }) next := func(want, own string) { t.Helper() for { f, body, err := r.runner.Next(r.ctx) if err != nil { t.Fatal(err) } if m, ok := body.(*wire.MessageCommand); ok { if !strings.Contains(m.Text, want) { t.Fatalf("dispatched %q, want it to contain %q", m.Text, want) } // Astra 2b round 2: the person's own words travel apart // from the model's question. if own == "" && len(m.PersonText) != 0 || own != "" && (len(m.PersonText) != 1 || m.PersonText[0] != own) { t.Fatalf("person_text %q, want [%q]", m.PersonText, own) } _ = r.runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) _ = r.runner.Send(r.ctx, "r-reply-"+f.ID, wire.FrameReply, wire.Reply{TurnID: m.TurnID, Attempt: 1, Text: "Ok."}) return } } } ans, err := r.client.Answer(r.ctx, question.ID, "Yes", question.Revision) if err != nil { t.Fatal(err) } next(`(My answer to your question "Slide on iPad too?") Yes`, "") r.wait("answer turn replied", func(c *Conversation) bool { return c.Turns[ans.Turn.ID].State == wire.TurnReplied }) if _, err := r.client.Answer(r.ctx, project.ID, "oficina/equipo", project.Revision); err != nil { t.Fatal(err) } next("(Use this repository instead of oficina/sample for that work) oficina/equipo", "") } // Review minor: an actionable project card with a stale revision is // card_stale, not card_answered. func TestProjectCardStaleRevision(t *testing.T) { r := newToolRig(t) r.call("s1", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) var card wire.Card r.wait("card", func(c *Conversation) bool { for _, k := range c.Cards { card = k } return card.ID != "" }) _, err := r.client.Answer(r.ctx, card.ID, "oficina/equipo", card.Revision+5) var conflict wire.CardConflict st, ok := err.(*ErrStatus) if !ok || st.Status != 409 || json.Unmarshal(st.Body, &conflict) != nil || conflict.Code != "card_stale" { t.Fatalf("stale project card: %v", err) } } // next ends the running turn with a reply and starts a turn for text. func (r *toolRig) next(text string) { r.t.Helper() prev := r.turn if err := r.runner.Send(r.ctx, "r-reply-"+prev, wire.FrameReply, wire.Reply{TurnID: prev, Attempt: 1, Text: "Ok."}); err != nil { r.t.Fatal(err) } r.wait("replied", func(c *Conversation) bool { return c.Turns[prev].State == wire.TurnReplied }) r.seq++ sent, err := r.client.Send(r.ctx, text, "k-next-"+itoa(r.seq)) if err != nil { r.t.Fatal(err) } f, _, err := r.runner.Expect(r.ctx, wire.FrameMessage) if err != nil { r.t.Fatal(err) } if err = r.runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { r.t.Fatal(err) } r.wait("running", func(c *Conversation) bool { return c.Turns[sent.Turn.ID].State == wire.TurnRunning }) r.turn = sent.Turn.ID } func (r *toolRig) homeCard() wire.Card { r.t.Helper() var card wire.Card r.wait("home card", func(c *Conversation) bool { card = wire.Card{} for _, k := range c.Cards { if k.Kind == wire.CardHome && k.TurnID == r.turn { card = k } } return card.ID != "" }) return card } // setHome asks for a home change and taps the card, as the person does, // returning the card (now a receipt with Undo). func (r *toolRig) setHome(id, ns string) wire.Card { r.t.Helper() if res := r.call(id, wire.ToolSetProjectHome, map[string]string{"namespace": ns}); res.Code != "asked_owner" { r.t.Fatalf("set_project_home %q: %+v", ns, res) } card := r.homeCard() if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceConfirm, card.Revision); err != nil { r.t.Fatal(err) } r.wait("set", func(c *Conversation) bool { card = c.Cards[card.ID]; return card.State == wire.CardHomeSet }) return card } // The team's home for new projects: unset by default; a change needs the // person's tap and leaves a receipt with Undo, which restores the value // before it (never set, or the earlier namespace). func TestProjectHomeSetting(t *testing.T) { r := newToolRig(t) if res := r.call("h1", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":""`) { t.Fatalf("unset home: %s", res.Text) } if res := r.call("h2", wire.ToolSetProjectHome, map[string]string{"namespace": "carlos"}); !res.IsError || res.Code != "no_create" { t.Fatalf("home outside create_in: %+v", res) } if res := r.call("h2c", wire.ToolSetProjectHome, map[string]string{"namespace": ""}); !res.IsError || res.Code != "no_home" { t.Fatalf("clear with no home: %+v", res) } card := r.setHome("h3", "oficina") shape(t, "api/card.json", card) if res := r.call("h4", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":"oficina"`) { t.Fatalf("home after the tap: %s", res.Text) } if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceUndo, card.Revision); err != nil { t.Fatal(err) } if res := r.call("h4u", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":""`) { t.Fatalf("home after undo: %s", res.Text) } r.wait("undone", func(c *Conversation) bool { return c.Cards[card.ID].State == wire.CardUndone }) if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceUndo, card.Revision+1); err == nil { t.Fatal("undo twice") } r.next("Use oficina again.") r.setHome("h5s", "oficina") r.next("Clear it.") cleared := r.setHome("h5", "") if res := r.call("h6", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":""`) { t.Fatalf("home after clear: %s", res.Text) } if _, err := r.client.Answer(r.ctx, cleared.ID, wire.ChoiceUndo, cleared.Revision); err != nil { t.Fatal(err) } if res := r.call("h7", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":"oficina"`) { t.Fatalf("home after undoing the clear: %s", res.Text) } } // Astra re-check 2: an explicit clear means no home, even when the // runner advertises its own default; never set falls back to it. func TestProjectHomeClearBeatsRunnerDefault(t *testing.T) { r := newToolRigSaying(t, wire.Protocol1, "Create a new project for the site.") hello := wire.Hello{Generation: r.runner.Welcome.Generation, Repos: []string{"amadan.net/oficina/sample"}, CreateIn: []string{"oficina", "work"}, Home: "work", Agent: "equipo-test", Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{}} if err := r.runner.Send(r.ctx, "h2", wire.FrameHello, hello); err != nil { t.Fatal(err) } waitHome := func(want string) { t.Helper() deadline := time.Now().Add(5 * time.Second) for { p, err := r.client.Projects(r.ctx) if err != nil { t.Fatal(err) } if p.Home == want { return } if time.Now().After(deadline) { t.Fatalf("home %q, want %q", p.Home, want) } time.Sleep(50 * time.Millisecond) } } waitHome("work") if res := r.call("d1", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":"work"`) { t.Fatalf("runner default: %s", res.Text) } r.next("Unset the home for new projects, please.") if k := r.setHome("d2", ""); k.Question != "work" { t.Fatalf("clear card names %q, want the runner default work", k.Question) } waitHome("") if res := r.call("d3", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":""`) { t.Fatalf("status after clear: %s", res.Text) } r.next("Create a new project for the site.") if res := r.call("d4", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/work/site"}); !res.IsError { t.Fatalf("create in the runner default after a clear: %+v", res) } if res := r.call("d5", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/site"}); res.Code != "create" { t.Fatalf("create in a related namespace: %+v", res) } } // Astra 2b review 4: Undo is bound to the change it made. After // unset -> oficina -> work -> oficina, the first card's Undo is refused // although the value matches again. func TestProjectHomeStaleUndo(t *testing.T) { r := newToolRig(t) hello := wire.Hello{Generation: r.runner.Welcome.Generation, Repos: []string{"amadan.net/oficina/sample"}, CreateIn: []string{"oficina", "work"}, Agent: "equipo-test", Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{}} if err := r.runner.Send(r.ctx, "h-su", wire.FrameHello, hello); err != nil { t.Fatal(err) } var cards []wire.Card for i, ns := range []string{"oficina", "work", "oficina"} { r.next("Put new projects in " + ns + ".") deadline := time.Now().Add(5 * time.Second) for { // The second hello (create_in with work) may still be on its way. if res := r.call("su"+itoa(i)+"-"+itoa(int(time.Now().UnixNano()%1e6)), wire.ToolSetProjectHome, map[string]string{"namespace": ns}); res.Code == "asked_owner" { break } else if time.Now().After(deadline) { t.Fatalf("set %s: %+v", ns, res) } time.Sleep(50 * time.Millisecond) } card := r.homeCard() if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceConfirm, card.Revision); err != nil { t.Fatal(err) } r.wait("set", func(c *Conversation) bool { card = c.Cards[card.ID]; return card.State == wire.CardHomeSet }) cards = append(cards, card) } first := cards[0] r.wait("first card", func(c *Conversation) bool { first = c.Cards[first.ID]; return true }) _, err := r.client.Answer(r.ctx, first.ID, wire.ChoiceUndo, first.Revision) if st, ok := err.(*ErrStatus); !ok || st.Status != 409 { t.Fatalf("superseded undo: %v", err) } if res := r.call("su-check", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":"oficina"`) { t.Fatalf("home after a refused undo: %s", res.Text) } last := cards[2] if _, err := r.client.Answer(r.ctx, last.ID, wire.ChoiceUndo, last.Revision); err != nil { t.Fatalf("latest undo: %v", err) } if res := r.call("su-after", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":"work"`) { t.Fatalf("latest undo restores work: %s", res.Text) } } // Astra 2b round 2, item 2: no text is parsed for intent any more. A // set_project_home call never changes the setting, however the person // phrased it; it posts the one-tap card, and only the person's tap on // that card, at its current revision, changes it. A clear asks "Stop // using X for new projects?". func TestProjectHomeOnlyByTap(t *testing.T) { r := newToolRig(t) for i, text := range []string{"New projects go to oficina.", `Explain the phrase "use oficina for new projects".`, "Don’t put new projects in oficina.", "hello"} { r.next(text) res := r.call("ot"+itoa(i), wire.ToolSetProjectHome, map[string]string{"namespace": "oficina"}) if res.IsError || res.Code != "asked_owner" { t.Fatalf("%q: %+v", text, res) } if res := r.call("os"+itoa(i), wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":""`) { t.Fatalf("%q: a tool call alone changed the home: %s", text, res.Text) } if k := r.homeCard(); k.State != wire.CardHomeAsk || k.Name != "oficina" { t.Fatalf("%q: card %+v", text, k) } } card := r.homeCard() if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceConfirm, card.Revision+1); Status(err) != 409 { t.Fatalf("tap at a stale revision: %v", err) } if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceConfirm, card.Revision); err != nil { t.Fatal(err) } if res := r.call("ot-set", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":"oficina"`) { t.Fatalf("home after the tap: %s", res.Text) } // Clearing asks too, naming what it stops using. r.next("Clear where new projects go.") if res := r.call("oc", wire.ToolSetProjectHome, map[string]string{"namespace": ""}); res.Code != "asked_owner" { t.Fatalf("clear: %+v", res) } if res := r.call("oc-s", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":"oficina"`) { t.Fatalf("a clear call alone changed the home: %s", res.Text) } clear := r.homeCard() if clear.State != wire.CardHomeAsk || clear.Name != "" || clear.Question != "oficina" { t.Fatalf("clear card %+v", clear) } if _, err := r.client.Answer(r.ctx, clear.ID, wire.ChoiceConfirm, clear.Revision); err != nil { t.Fatal(err) } if res := r.call("oc-after", wire.ToolStatus, map[string]string{}); !strings.Contains(res.Text, `"home":""`) { t.Fatalf("home after the clear tap: %s", res.Text) } } // daytime is a housekeeping clock at midday UTC today, moving with real // time from when it was made. func daytime() func() time.Time { start := time.Now() base := start.UTC().Truncate(24 * time.Hour).Add(12 * time.Hour) return func() time.Time { return base.Add(time.Since(start)) } } // toolHello is the tool rig's hello (protocol 1). The agent reads // moonbeam/docs only (a reader grant: catalog, not Repos), so moonbeam is // a namespace Equipo knows, and an unreached repository there gets an // access card (#48). func toolHello(generation int64) wire.Hello { return wire.Hello{Generation: generation, Repos: []string{"amadan.net/oficina/sample", "amadan.net/oficina/equipo"}, CreateIn: []string{"oficina"}, Agent: "equipo-test", Catalog: []wire.RepoInfo{{Repo: "amadan.net/oficina/sample", DefaultBranch: "main", Summary: "Woodstar is a reading app.", Names: []string{"sample", "Woodstar"}, Hints: []string{"go"}, Gate: "make ci", HeadSHA: strings.Repeat("9", 40), HeadSubject: "Trim the compose sheet"}, {Repo: "amadan.net/moonbeam/docs", DefaultBranch: "main", Names: []string{"docs"}, Hints: []string{}}}, Connections: []wire.HeldConnection{{LocalID: "claude-a", Person: Owner, Provider: "claude", Kind: "subscription", Label: Owner, State: "healthy"}}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}} }