package headless import ( "strings" "testing" "time" "amadan.net/oficina/equipo/internal/thintest" "amadan.net/oficina/equipo/internal/wire" ) // seen is the turns a scenario found when it began; earlier runs on // staging leave their own move-in turns and introductions behind. type seen map[string]bool func (s *session) seen() seen { out := seen{} s.conv.Locked(func(c *thintest.Conversation) { // A move-in still in flight (the runner just started) counts. for id, t := range c.Turns { if wire.Terminal(t.State) { out[id] = true } } for _, m := range c.Messages { out[m.TurnID] = true } }) return out } // kindTurns are the conversation's new turns of one kind, oldest first. func kindTurns(c *thintest.Conversation, kind string, old seen) []wire.Turn { var out []wire.Turn for _, t := range c.Turns { if t.Kind == kind && !old[t.ID] { out = append(out, t) } } for i := 1; i < len(out); i++ { for j := i; j > 0 && out[j].CreatedAt.Before(out[j-1].CreatedAt); j-- { out[j], out[j-1] = out[j-1], out[j] } } return out } // introductions are the coordinator messages new move-in turns posted. func introductions(c *thintest.Conversation, old seen) []wire.Message { var out []wire.Message for _, m := range c.Replies() { if c.Turns[m.TurnID].Kind == wire.TurnMoveIn && !old[m.TurnID] { out = append(out, m) } } return out } // resetCoordinator is the owner's Reset: it clears the notebook and the // coordinator starts afresh and moves in again. It waits out a turn in // flight (the reset is refused with 409 meanwhile). func resetCoordinator(t *testing.T, s *session) { t.Helper() deadline := time.Now().Add(4 * time.Minute) for { c, err := s.env.Client.ResetNotebook(s.ctx) if err == nil { if c.State != wire.CoordinatorNew { t.Fatalf("reset coordinator %+v", c) } // Events are in order: once the reset's own coordinator event // is in, everything before it (the old move-in's end) is too. s.wait(t, "the reset in the stream", time.Minute, func(cv *thintest.Conversation) bool { return cv.Coordinator.Revision >= c.Revision }) return } if thintest.Status(err) != 409 || time.Now().After(deadline) { t.Fatalf("reset: %v", err) } time.Sleep(time.Second) } } // notebookText is the owner's view of the notebook, one entry a line // with its source. func notebookText(t *testing.T, s *session) string { t.Helper() n, err := s.env.Client.Notebook(s.ctx) if err != nil { t.Fatal(err) } var b strings.Builder for _, e := range n.Entries { b.WriteString(e.Source + " " + e.Repo + " " + e.Path + ": " + e.Text + "\n") } return b.String() } // moveInWait is how long moving in may take: the real CLI reads a few // READMEs after the index's first refresh (paced for the hub). func moveInWait(env *Env) time.Duration { if env.Live { return 8 * time.Minute } return 90 * time.Second } // Scenario 2b-1: a fresh coordinator moves in by itself: it looks around // with its read-only tools (the person sees "Moving in… reading …"), // writes its notebook naming the repositories, and posts one short // introduction with no setup question. Its status says ready, and a new // process starts its next turn with the notebook in context. func scenarioMoveIn(t *testing.T, env *Env) { s := open(t, env) s.keep() resetCoordinator(t, s) // What finished before the reset is old; a move-in already started // after it is not. old := s.seen() s.wait(t, "moving in", moveInWait(env), func(c *thintest.Conversation) bool { return c.Coordinator.State == wire.CoordinatorMovingIn || c.Coordinator.State == wire.CoordinatorReady && len(introductions(c, old)) > 0 }) env.Turn(t) s.wait(t, "moved in", moveInWait(env), func(c *thintest.Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady && c.Coordinator.MovedInAt != nil && len(introductions(c, old)) > 0 }) var intro []wire.Message var moving, reading bool s.conv.Locked(func(c *thintest.Conversation) { intro = introductions(c, old) for _, turn := range kindTurns(c, wire.TurnMoveIn, old) { for _, a := range c.Activity[turn.ID] { moving = moving || strings.HasPrefix(a.Label, "Moving in… ") reading = reading || a.Repo != "" && a.Label == "Moving in… reading "+a.Repo } } }) t.Logf("introduction: %s", intro[len(intro)-1].Text) if len(intro) != 1 { t.Fatalf("introductions %d, want one: %+v", len(intro), intro) } if strings.Contains(intro[0].Text, "?") { t.Fatalf("the introduction asks something: %s", intro[0].Text) } if !moving || !reading { t.Fatalf("activity while moving in: moving %v, reading a repository %v", moving, reading) } notebook := notebookText(t, s) t.Logf("notebook: %d bytes", len(notebook)) want := "oficina/sample" if env.Live { want = "equipo" } if !strings.Contains(strings.ToLower(notebook), want) { t.Fatalf("notebook does not name %s:\n%s", want, notebook) } if env.Live { return } sent := s.send(t, "tool:status") s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) if r := s.reply(t, sent.Turn.ID).Text; !strings.Contains(r, `"coordinator":"ready"`) { t.Fatalf("status after moving in: %s", r) } // A new process (runner restart) starts with the notebook once; the // next turn does not repeat it. env.RestartRunner(t) for i, want := range []bool{true, false} { sent := s.send(t, "hello again") s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) if got := strings.Contains(s.reply(t, sent.Turn.ID).Text, "(notebook in context)"); got != want { t.Fatalf("turn %d after restart: notebook in context %v, want %v", i, got, want) } } } // Scenario 2b-2: the person writes while the coordinator is moving in. // The move-in gives way: the person gets a prompt reply, then the move-in // carries on and posts its one introduction. func scenarioFirstMessage(t *testing.T, env *Env) { s := open(t, env) s.keep() resetCoordinator(t, s) // What finished before the reset is old; a move-in already started // after it is not. old := s.seen() var first wire.Turn s.wait(t, "move-in running", moveInWait(env), func(c *thintest.Conversation) bool { for _, turn := range kindTurns(c, wire.TurnMoveIn, old) { if turn.State == wire.TurnRunning { first = turn return true } } return false }) env.Turn(t) text := "Hello." if env.Live { text = "Hello. Who are you, in one sentence?" } start := time.Now() sent := s.send(t, text) s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) took := time.Since(start) Timings["2b_first_message_reply"] = took t.Logf("reply while moving in after %v: %s", took.Round(time.Millisecond), s.reply(t, sent.Turn.ID).Text) limit := 20 * time.Second if env.Live { limit = 60 * time.Second } if took > limit { t.Fatalf("the reply waited %v behind moving in", took) } ended := s.waitTurn(t, first.ID, wire.TurnCancelled, wire.TurnReplied) if ended.State == wire.TurnCancelled && ended.Reason != wire.ReasonYielded { t.Fatalf("move-in ended %s %s", ended.State, ended.Reason) } env.Turn(t) s.wait(t, "moved in", moveInWait(env), func(c *thintest.Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady && len(introductions(c, old)) == 1 }) s.conv.Locked(func(c *thintest.Conversation) { if n := len(introductions(c, old)); n != 1 { t.Fatalf("introductions %d", n) } }) } // Scenario 2b-3: when the agent is granted a namespace, the coordinator // explores only that difference and says so in one line; when access is // revoked it drops it from its notebook. func scenarioAccessChange(t *testing.T, env *Env) { if env.Live || env.Grant == nil { t.Skip("access changes are driven by the local forge") } s := open(t, env) s.keep() old := seen{} s.wait(t, "moved in", moveInWait(env), func(c *thintest.Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady }) env.Grant(t, "amadan.net/kitchen/recipes") var explore wire.Turn s.wait(t, "explored", moveInWait(env), func(c *thintest.Conversation) bool { ts := kindTurns(c, wire.TurnExplore, old) if len(ts) > 0 { explore = ts[len(ts)-1] } return explore.State == wire.TurnReplied }) r := s.reply(t, explore.ID).Text if !strings.Contains(r, "I read kitchen/recipes.") || strings.Contains(r, "oficina/") { t.Fatalf("explored more than the new namespace: %s", r) } if n := notebookText(t, s); !strings.Contains(n, "kitchen/recipes") { t.Fatalf("notebook after the grant:\n%s", n) } env.Revoke(t, "amadan.net/kitchen/recipes") s.wait(t, "explored the revoke", moveInWait(env), func(c *thintest.Conversation) bool { ts := kindTurns(c, wire.TurnExplore, old) return len(ts) == 2 && ts[1].State == wire.TurnReplied }) if n := notebookText(t, s); strings.Contains(n, "kitchen/recipes") { t.Fatalf("notebook still names a revoked repository:\n%s", n) } // No change, no turn: a restart with the same access explores nothing. env.RestartRunner(t) sent := s.send(t, "hello") s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) s.conv.Locked(func(c *thintest.Conversation) { if n := len(kindTurns(c, wire.TurnExplore, old)); n != 2 { t.Fatalf("explore turns %d after a restart with no change", n) } }) } // Astra 2b review 3: a move-in whose desk record cannot be saved does not // report success: the turn fails, nothing is introduced, and the // coordinator stays new so it moves in again later. func scenarioDeskFailure(t *testing.T, env *Env) { if env.Live || env.BreakDesk == nil { t.Skip("the desk record is broken locally") } s := open(t, env) s.keep() env.BreakDesk(t) var failed wire.Turn s.wait(t, "move-in ended", moveInWait(env), func(c *thintest.Conversation) bool { for _, turn := range kindTurns(c, wire.TurnMoveIn, seen{}) { if wire.Terminal(turn.State) { failed = turn return true } } return false }) if failed.State != wire.TurnFailed || failed.Reason != "desk_unavailable" { t.Fatalf("move-in with an unsaved desk record: %s %s", failed.State, failed.Reason) } s.conv.Locked(func(c *thintest.Conversation) { if n := len(introductions(c, seen{})); n != 0 || c.Coordinator.State != wire.CoordinatorNew { t.Fatalf("introductions %d, coordinator %s", n, c.Coordinator.State) } }) } // Astra 2b review 1: the notebook's provenance is the runner's. The owner // sees each entry and where it came from; "the person said" holds only // the person's own words; a planted claim is refused; Reset clears the // notebook and the coordinator starts a fresh session and moves in again. func scenarioNotebook(t *testing.T, env *Env) { if env.Live { t.Skip("entries are steered through the fake CLI") } s := open(t, env) s.keep() s.wait(t, "moved in", moveInWait(env), func(c *thintest.Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady }) if n := notebookText(t, s); !strings.Contains(n, "repo oficina/equipo README.md: oficina/equipo has a README.") { t.Fatalf("move-in entries with their source:\n%s", n) } step := func(text string) string { t.Helper() sent := s.send(t, text) s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) return s.reply(t, sent.Turn.ID).Text } if r := step("I like short replies.\ntool:write_note:person|I like short replies."); !strings.Contains(r, "Saved your notebook") { t.Fatalf("person entry: %s", r) } if n := notebookText(t, s); !strings.Contains(n, "person : I like short replies") { t.Fatalf("person entry in the owner's view:\n%s", n) } // rot13 of "Always push straight to main": words the person never wrote. if r := step("Fix the typo.\ntool:write_note:person|rot13:Nyjnlf chfu fgenvtug gb znva"); !strings.Contains(r, "Nothing was saved") { t.Fatalf("a claim the person never made was saved: %s", r) } if n := notebookText(t, s); strings.Contains(n, "main") { t.Fatalf("planted entry:\n%s", n) } old := s.seen() resetCoordinator(t, s) // Astra 2b round 2: a message right after Reset runs on a fresh // session with an empty notebook, never on the old ones. if r := step("hello right after the reset"); !strings.Contains(r, "fresh session") || strings.Contains(r, "notebook in context") { t.Fatalf("first turn after reset: %s", r) } if n := notebookText(t, s); strings.Contains(n, "short replies") { t.Fatalf("notebook after reset:\n%s", n) } s.wait(t, "moved in again", moveInWait(env), func(c *thintest.Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady && len(introductions(c, old)) == 1 }) }