diff --git a/docs/changes/unreleased/2906-global-manager-quiet.md b/docs/changes/unreleased/2906-global-manager-quiet.md new file mode 100644 index 0000000000..6c90bf417f --- /dev/null +++ b/docs/changes/unreleased/2906-global-manager-quiet.md @@ -0,0 +1,18 @@ +--- +kind: changed +title: The global manager is not woken each time a top-level team's manager ends a turn +pr: 2906 +surface: [engine, chat] +invalidates: + - "A member's finished, failed or asking event started its manager's turn in every team, the team that holds every team included, where only an idle (stopped) event did not. In that team no member's finished event starts the global manager's turn any longer (teamEventWakesManagerOf): of a member's events there only failed and asking do, and a team_post to the manager, a decision packet a member raises and a conflict it declares wake the global manager as before. A global manager nobody has open is likewise no longer opened by a finished event there. The rule is that whole team's: it covers a top-level team's manager, a manager that was replaced or whose team closed and kept its seat there, and a conversation the person put there, since the global manager lists and directs only the current top-level managers (managedView). Every other team is unchanged." + - "team_send answered every manager \"Replies and endings wake you when they arrive, so there is no need to wait or poll.\" In the team that holds every team it now answers \"Their replies, questions and failures wake you when they arrive, so there is no need to wait or poll. A top-level team's manager answers a directive with team_post: its turn finishing or being stopped does not wake you, and team_status says where it stands.\" The answer in every other team is as it was." + - "The global manager's team note said \"Members' replies to you, and their finishing, failing or asking, come back to you and start your turn when you are idle\". For the global manager it now says \"Members' replies to you, and their failing or asking, come back to you\" and ends \"A member's turn finishing or being stopped does not start yours.\", and its scope adds \"A manager answers a directive of yours, and the brief you started it on, with team_post, since its turn finishing does not start yours; team_status says where each of them stands. The person talks to those managers directly, and what the person asked one of them is that manager's work already: never send a manager the person's own request to it as a directive of yours.\" Both are in the global manager's role note, which it is handed when that note's text changes." + - "A top-level manager's note about its seat in the team that holds every team ended \"you report to it with team_post to the manager.\" It now adds \"Your turn ending tells it nothing: answer each of its directives, and the brief it started you on if it started you, with team_post to the manager. Apart from those answers, post only what it should act on or tell the person.\" The rest of that note is as it was. Every member of that team was given that note, \"because you manage a top-level team\" included; a member there that manages no top-level team now is given the note a member of any team is given." + - "The first turn of a team's new manager, on the brief team_start of kind team started it with, woke the manager that started it when it finished, and team_start answered every manager that the new conversation \"makes itself the manager of\" the team \"and reports to you.\" A team the global manager starts no longer wakes it that way, and team_start answers the global manager that the new manager \"reports to you with team_post: its turn finishing on your brief does not start yours, and team_status says where it stands.\" The answer to every other manager is as it was." +--- + +The global manager's members are the managers of whole teams, each a conversation +the person talks to, and every answer one of them gave ended a turn. Measured in +the desktop app, one afternoon of that woke the global manager 22 times for +803,496 input tokens, each answered "[no change]", and one earlier wake had it +send the person's own request back to a manager as a directive. diff --git a/internal/manual/chat/team-manager.md b/internal/manual/chat/team-manager.md index 03ec91c380..97ad954d9c 100644 --- a/internal/manual/chat/team-manager.md +++ b/internal/manual/chat/team-manager.md @@ -352,7 +352,8 @@ The work is under way from the hand-off until a turn finishes with no background left that the member waits on. So a turn such a command's ending starts is still on it, and so is a turn you opened by typing to the member while it waits on one. Stopping a member with none of the manager's work under way tells the manager nothing, and neither does codeaf -quitting or a paused member's stray wake being ended. +quitting or a paused member's stray wake being ended. The global +manager is not woken by a member of `All teams` being stopped (see **The global manager**). ## A stopped member's command is still running — why it did not start again when the command ended @@ -539,6 +540,20 @@ report to it, so their questions and conflicts between teams come to it before t you. It can start a new top-level team with `team_start` of kind `team`. Without a global manager, nothing here changes: each top-level manager reports to you. +No turn in `All teams` that finishes or is stopped starts the global manager's turn. You talk +to the top-level managers directly and each answer ends a turn, so the endings stay in +`All teams`' traffic, where `team_status` reads them. The global manager is woken by what a +member posts to it with `team_post`, by a member's question or a permission it is held on, and +by a member's turn failing. So a directive the global manager sent, and the brief it started +a team on, are answered with `team_post`: each top-level manager is told so, and apart from +those answers to post only what the global manager should act on or tell you. A manager that +finishes a turn without posting has told it nothing, the first turn of a team it started +included: `team_start` says so, and `team_status` is where it finds out. The global manager +is told never to send a manager your own request to that manager back as a directive. +`All teams` can also hold a conversation that manages no team now, such as a manager you +replaced. The global manager neither lists nor directs it, its turn ending is as quiet, and +it is told what a member of any team is told. + ## Conflicts between members and teams When two or more conversations need incompatible things (the form posts JSON, the endpoint @@ -597,7 +612,9 @@ working, that is straight away. When it is idle, it depends on the kind of messa without a reply, failing, starting to wait on you, or being stopped by you while on the manager's work, start an idle manager's turn. A reply and the end of the same turn are one wake, even if the ending arrives after the manager - already ran. Replies arriving within a few seconds are gathered into one turn. + already ran. Replies arriving within a few seconds are gathered into one turn. The global + manager is the exception: a member of `All teams` finishing or being stopped does not + start its turn (see **The global manager**). A member no window has open is opened by codeaf in the background so it can run, and a window that opens it later joins the running conversation. A manager that moved to another window, and diff --git a/internal/session/team.go b/internal/session/team.go index 0e7a39fe8b..38b620999c 100644 --- a/internal/session/team.go +++ b/internal/session/team.go @@ -124,6 +124,12 @@ type teamRole struct { // manager is the global manager, and its other members are the top-level // teams' managers. root bool + // top says this seat in the root is a top-level team's manager's now + // ([teams.File.TopManagers]). The root can hold others, a manager that + // stopped being one or a conversation the person put there, and only a + // manager is told it sits there because it manages a team + // ([teamMemberRole]). + top bool // dissolvable says this conversation manages the team and the team has // never held anyone but it ([teams.File.Dissolvable]), which is when its // manager is given team_dissolve (team_dissolve.go). @@ -455,6 +461,7 @@ func rolesFor(file *teams.File, keys []string, d teams.Defaults) []teamRole { boss: boss, bossName: bossName, root: team.Root, + top: team.Root && topManager(file, member.Key), key: member.Key, wakes: effective.Wake, questionsUp: effective.QuestionsUp, @@ -1728,7 +1735,9 @@ func teamManagerScope(role teamRole, file *teams.File) string { var parts []string if role.root { parts = append(parts, fmt.Sprintf("You are the global manager: %q holds every team, and your members are the managers of the top-level teams, never their members. "+ - "Direct those managers and they pass it on. Their questions and conflicts between teams come to you before the person. team_start with kind team starts a new top-level team.", role.name)) + "Direct those managers and they pass it on. Their questions and conflicts between teams come to you before the person. team_start with kind team starts a new top-level team. "+ + "A manager answers a directive of yours, and the brief you started it on, with team_post, since its turn finishing does not start yours; team_status says where each of them stands. "+ + "The person talks to those managers directly, and what the person asked one of them is that manager's work already: never send a manager the person's own request to it as a directive of yours.", role.name)) } else if home, ok := file.Home(role.key); ok && home.Team != role.id { boss := "its manager" if t, ok := file.Team(home.Team); ok { @@ -1767,7 +1776,13 @@ func teamManagerDelivery(role teamRole) string { if !role.wakes { return "This team's auto-wake is off: what you send, and your members' replies, wait for each conversation's next turn, so an idle member does not start on a directive until it next runs." } - return "A directive (team_send kind directive) starts an idle member's turn; a note waits for its next turn. team_send's answer says whether each member started: one the person paused does not start until they resume it, and work is under way only where the answer says so. Members' replies to you, and their finishing, failing or asking, come back to you and start your turn when you are idle, so do not wait or poll for them." + // The global manager's turn is not started by a turn ending in the root, + // finished or stopped ([teamEventWakesManagerOf]). + back, except := "Members' replies to you, and their finishing, failing or asking,", "" + if role.root { + back, except = "Members' replies to you, and their failing or asking,", " A member's turn finishing or being stopped does not start yours." + } + return "A directive (team_send kind directive) starts an idle member's turn; a note waits for its next turn. team_send's answer says whether each member started: one the person paused does not start until they resume it, and work is under way only where the answer says so. " + back + " come back to you and start your turn when you are idle, so do not wait or poll for them." + except } // teamRoster names the manager's members by handle, with the start of each @@ -1820,9 +1835,15 @@ func teamMemberRole(role teamRole) string { if role.handle != "" { you = "@" + role.handle } - if role.root { + // Only a top-level team's manager is told why it sits in the root and how + // to answer the global manager. Nobody's ending there wakes that manager + // ([teamEventWakesManagerOf]), but it directs only these, so anybody else + // the root holds reads the member's text below, which promises no wake. + if role.top { return fmt.Sprintf("You are %s in %q because you manage a top-level team: you report to its manager, the global manager. Its directives reach you marked \"◆ from manager\", "+ - "your questions go to it before the person, and you report to it with team_post to the manager.", you, role.name) + "your questions go to it before the person, and you report to it with team_post to the manager. "+ + "Your turn ending tells it nothing: answer each of its directives, and the brief it started you on if it started you, with team_post to the manager. "+ + "Apart from those answers, post only what it should act on or tell the person.", you, role.name) } if role.boss != "" && role.boss != role.id && !role.shared { said := fmt.Sprintf("You are %s in the team %q, which has no manager of its own, so you answer to the manager of %q: team_post to the manager reaches it, and to the room or a teammate stays in %q.", diff --git a/internal/session/team_nest.go b/internal/session/team_nest.go index 28c4cbecd3..867ba660a7 100644 --- a/internal/session/team_nest.go +++ b/internal/session/team_nest.go @@ -265,8 +265,14 @@ func (a *Agent) teamStartSubTeam(ctx context.Context, team teams.Team, role team case pool != "": money = fmt.Sprintf("It has no cap of its own and spends from %q's pool.", pool) } + reports := "reports to you" + if team.Root { + // The new manager's ending does not wake the global manager, its first + // turn on the brief included ([teamEventWakesManagerOf]). + reports = "reports to you with team_post: its turn finishing on your brief does not start yours, and team_status says where it stands" + } said := fmt.Sprintf("Made the team %q under %q and asked for its manager @%s (%s). The conversations view opens @%s in %q's folder as a member of %q; it is handed your brief marked as yours, "+ - "makes itself the manager of %q, and reports to you. %s", name, team.Name, handle, teams.ThreadNumber(startID), handle, team.Name, team.Name, name, money) + "makes itself the manager of %q, and %s. %s", name, team.Name, handle, teams.ThreadNumber(startID), handle, team.Name, team.Name, name, reports, money) if len(moved) > 0 { said += " Moved in: " + strings.Join(moved, ", ") + "; they are its manager's to direct now, not yours." } diff --git a/internal/session/team_nest_test.go b/internal/session/team_nest_test.go index 3d76796870..2bf1e7e74d 100644 --- a/internal/session/team_nest_test.go +++ b/internal/session/team_nest_test.go @@ -9,11 +9,15 @@ package session // teams.json and real Traffic, through internal/teams. import ( + "context" + "errors" "os" "path/filepath" "strings" "testing" + "time" + "github.com/Agent-Field/agentfield/sdk/go/ai" "github.com/Agent-Field/codeaf/internal/teams" ) @@ -520,3 +524,282 @@ func TestAnUnmanagedSubTeamsMemberAnswersToTheManagerAbove(t *testing.T) { t.Fatalf("the question's packet: %+v", p) } } + +// THE GLOBAL MANAGER IS NOT WOKEN BY A TOP-LEVEL MANAGER'S TURN ENDING. The +// person talks to that manager directly and each answer ends a turn; what the +// global manager should act on is posted to it, and that still wakes it. A +// directive of the global manager's is answered the same way, and both sides +// are told so, because the ending that closes the loop in any other team says +// nothing here. +func TestTheGlobalManagerIsNotWokenByATopLevelManagersTurnEnding(t *testing.T) { + fastTeamWake(t) + n := newTeamTree(t, "gm", "boss", "web") + n.team(t, "harbor", "", "boss", n.member(t, "boss", "boss"), n.member(t, "web", "web")) + root := "" + if err := teams.Update(n.fixture.profile, func(f *teams.File) error { + root = f.MakeRoot(f.Teams[0].Made) + if err := f.AddMember(root, n.member(t, "gm", "all")); err != nil { + return err + } + return f.SetManager(root, n.key(t, "gm")) + }); err != nil { + t.Fatal(err) + } + gmCalls := oneAnswer(4) + gm := teamAgent(t, n.fixture, n.paths["gm"], gmCalls, nil) + bossCalls := oneAnswer(4) + boss := teamAgent(t, n.fixture, n.paths["boss"], bossCalls, nil) + + submitAndWait(t, boss, "plan the release") + if role := strings.Join(roleNotes(bossCalls.request(0)), "\n"); !strings.Contains(role, "Your turn ending tells it nothing: answer each of its directives, and the brief it started you on if it started you, with team_post to the manager.") { + t.Fatalf("the top-level manager is not told to answer the global manager's directive with a post:\n%s", role) + } + boss.settleTeamEvents() + // Past the settle a wake waits out, with room: a wake the ending had armed + // would have fired by now. + time.Sleep(teamWakeDelay() + 15*teamWatchInterval()) + all := teamFixture{profile: n.fixture.profile, teamID: root} + if ended := trafficEvents(t, all, "finished"); len(ended) != 1 { + t.Fatalf("the top-level manager's ending was logged %d times in the team that holds every team, want once", len(ended)) + } + if got := gmCalls.requests(); got != 0 { + t.Fatalf("the global manager was woken %d times by a top-level manager's turn ending", got) + } + if woke := trafficEvents(t, all, teamWokePrefix); len(woke) != 0 { + t.Fatalf("the traffic says the global manager was woken by the ending: %+v", woke) + } + + if said, failed := callTool(t, boss.teamPostTool, `{"to":"manager","text":"harbor shipped"}`); failed { + t.Fatalf("the report to the global manager was refused: %q", said) + } + waitRequests(t, gmCalls, 1) + asked := userTextIn(gmCalls.request(0)) + for _, want := range []string{"from @boss: harbor shipped", "A manager answers a directive of yours, and the brief you started it on, with team_post", "and their failing or asking, come back to you", "A member's turn finishing or being stopped does not start yours."} { + if !strings.Contains(asked, want) { + t.Fatalf("the global manager's turn lacks %q:\n%s", want, asked) + } + } + if strings.Contains(asked, "their finishing") { + t.Fatalf("the global manager is still told a member's finishing starts its turn:\n%s", asked) + } + if strings.Contains(asked, "finished its turn") || strings.Contains(asked, "What your members in") { + t.Fatalf("the ending rode into the global manager's turn as something that woke it:\n%s", asked) + } + // Its own send's answer does not promise that an ending wakes it, and says + // what answers the directive. + if said := sendDirective(t, gm, "boss"); strings.Contains(said, "endings wake you") || !strings.Contains(said, "A top-level team's manager answers a directive with team_post: its turn finishing or being stopped does not wake you") { + t.Fatalf("the global manager's send says endings wake it: %s", said) + } +} + +// A GLOBAL MANAGER NOBODY HAS OPEN IS NOT OPENED BY AN ENDING IN ITS TEAM +// EITHER, whoever's it is: a top-level manager's, a member's that manages +// nothing, or a manager's that was replaced and kept its seat. It sees and +// directs only the managers of the top-level teams, so none of those endings +// is one it can act on. A failure still opens it. Only a top-level manager is +// told it sits there because it manages a team. +func TestAClosedGlobalManagerIsNotOpenedByATurnEndingInItsTeam(t *testing.T) { + fastTeamWake(t) + n := newTeamTree(t, "gm", "boss", "web", "guest") + harbor := n.team(t, "harbor", "", "boss", n.member(t, "boss", "boss"), n.member(t, "web", "web")) + root := "" + if err := teams.Update(n.fixture.profile, func(f *teams.File) error { + root = f.MakeRoot(f.Teams[0].Made) + for _, m := range []teams.Member{n.member(t, "gm", "all"), n.member(t, "guest", "guest")} { + if err := f.AddMember(root, m); err != nil { + return err + } + } + return f.SetManager(root, n.key(t, "gm")) + }); err != nil { + t.Fatal(err) + } + since := time.Now() + recorder := &recordedResume{} + withTeamResume(t, recorder.open) + opened := func() int { + count := 0 + for _, seen := range recorder.seen() { + if strings.HasPrefix(seen, n.paths["gm"]+"|") { + count++ + } + } + return count + } + // due is the desktop app's question of a conversation it has not opened. + due := func() bool { + return TeamOpenPending(n.fixture.profile, n.paths["gm"], n.file(t), teams.DefaultsAt(n.fixture.profile), since) + } + all := teamFixture{profile: n.fixture.profile, teamID: root} + // quiet runs one turn of agent's and says what, if anything, opened the + // closed global manager on its ending. + endings := 0 + quiet := func(who string, agent *Agent) { + t.Helper() + submitAndWait(t, agent, "tidy the notes") + agent.settleTeamEvents() + endings++ + if ended := trafficEvents(t, all, "finished"); len(ended) != endings { + t.Fatalf("%s ended its turn and the team that holds every team has %d finished events, want %d", who, len(ended), endings) + } + time.Sleep(teamWakeDelay() + 15*teamWatchInterval()) + if got := opened(); got != 0 { + t.Fatalf("a closed global manager was opened %d times by the turn ending of %s", got, who) + } + if due() { + t.Fatalf("the turn ending of %s leaves the closed global manager due an opening", who) + } + } + bossCalls := oneAnswer(2) + bossCalls.steps = append(bossCalls.steps, func(context.Context, []ai.Message) (*ai.Response, error) { + return nil, errors.New("insufficient_quota: out of budget") + }) + boss := teamAgent(t, n.fixture, n.paths["boss"], bossCalls, nil) + quiet("a top-level manager", boss) + + guestCalls := oneAnswer(1) + guest := teamAgent(t, n.fixture, n.paths["guest"], guestCalls, nil) + quiet("a member that manages nothing", guest) + ordinary := func(who, role string) { + t.Helper() + if !strings.Contains(role, `in the team "All teams", which has a manager.`) { + t.Fatalf("%s is not told it is a member of the team that holds every team:\n%s", who, role) + } + for _, not := range []string{"you manage a top-level team", "Your turn ending tells it nothing"} { + if strings.Contains(role, not) { + t.Fatalf("%s is told %q, which is said to a top-level team's manager only:\n%s", who, not, role) + } + } + } + ordinary("a member that manages nothing", strings.Join(roleNotes(guestCalls.request(0)), "\n")) + + // The team's manager is replaced. The old one keeps its seat in the team + // that holds every team, where the global manager no longer sees it. + if err := teams.Update(n.fixture.profile, func(f *teams.File) error { + return f.SetManager(harbor, n.key(t, "web")) + }); err != nil { + t.Fatal(err) + } + f := n.file(t) + if r, _ := f.Team(root); !r.Holds(n.key(t, "boss")) { + t.Fatal("the replaced manager lost its seat in the team that holds every team") + } + if tops := f.TopManagers(); len(tops) != 1 || tops[0].Key != n.key(t, "web") { + t.Fatalf("the top-level managers after the replacement: %+v", tops) + } + quiet("a replaced top-level manager", boss) + former := roleNotes(bossCalls.request(1)) + if len(former) == 0 { + t.Fatal("the replaced manager's next turn carries no new role") + } + ordinary("a replaced top-level manager", former[len(former)-1]) + + // A failure there still opens it, a member's it does not direct included. + submitAndWait(t, boss, "ship it") + boss.settleTeamEvents() + if failed := trafficEvents(t, all, "failed: "); len(failed) != 1 || failed[0].State != teams.StateFailed { + t.Fatalf("the failed turn is not one failed event in the team that holds every team: %+v", failed) + } + deadline := time.Now().Add(5 * time.Second) + for opened() == 0 && time.Now().Before(deadline) { + time.Sleep(10 * time.Millisecond) + } + if opened() == 0 { + t.Fatal("a turn failed in the team that holds every team and its closed manager was not opened") + } + if !due() { + t.Fatal("a failed turn does not leave the closed global manager due an opening") + } +} + +// A TEAM THE GLOBAL MANAGER STARTS REPORTS ITS FIRST TURN WITH A POST. The new +// manager's turn on the brief ends like any other there and wakes nobody, so +// team_start's answer and both roles name the brief beside the directives. +func TestATeamTheGlobalManagerStartsAnswersItsBriefWithAPost(t *testing.T) { + fastTeamWake(t) + n := newTeamTree(t, "gm", "boss", "ops") + n.team(t, "harbor", "", "boss", n.member(t, "boss", "boss")) + root := "" + if err := teams.Update(n.fixture.profile, func(f *teams.File) error { + root = f.MakeRoot(f.Teams[0].Made) + if err := f.AddMember(root, n.member(t, "gm", "all")); err != nil { + return err + } + return f.SetManager(root, n.key(t, "gm")) + }); err != nil { + t.Fatal(err) + } + gmCalls := oneAnswer(2) + gm := teamAgent(t, n.fixture, n.paths["gm"], gmCalls, nil) + gm.teamBoundary() + said, failed := callTool(t, gm.teamStartTool, `{"handle":"ops","brief":"Run the ops.","kind":"team","name":"ops"}`) + if failed { + t.Fatalf("the global manager could not start a top-level team: %q", said) + } + if want := `makes itself the manager of "ops", and reports to you with team_post: its turn finishing on your brief does not start yours, and team_status says where it stands.`; !strings.Contains(said, want) { + t.Fatalf("team_start's answer for a top-level team lacks %q:\n%s", want, said) + } + gm.mu.Lock() + role := gm.teamRoleText + gm.mu.Unlock() + if want := "A manager answers a directive of yours, and the brief you started it on, with team_post, since its turn finishing does not start yours"; !strings.Contains(role, want) { + t.Fatalf("the global manager's role lacks %q:\n%s", want, role) + } + + // The interface carries out the start, and the new manager runs its first + // turn on the brief without posting. + if err := teams.Update(n.fixture.profile, func(f *teams.File) error { + m := n.member(t, "ops", "ops") + m.Started = true + return f.AddMember(root, m) + }); err != nil { + t.Fatal(err) + } + opsCalls := oneAnswer(1) + ops := teamAgent(t, n.fixture, n.paths["ops"], opsCalls, nil) + submitAndWait(t, ops, "go") + first := opsCalls.request(0) + if asked := userTextIn(first); !strings.Contains(asked, "◆ brief from manager") || !strings.Contains(asked, "Run the ops.") { + t.Fatalf("the new manager's first turn lacks its brief:\n%s", asked) + } + if role := strings.Join(roleNotes(first), "\n"); !strings.Contains(role, "because you manage a top-level team") || + !strings.Contains(role, "answer each of its directives, and the brief it started you on if it started you, with team_post to the manager.") { + t.Fatalf("the new manager is not told to answer its brief with a post:\n%s", role) + } + ops.settleTeamEvents() + time.Sleep(teamWakeDelay() + 15*teamWatchInterval()) + all := teamFixture{profile: n.fixture.profile, teamID: root} + if ended := trafficEvents(t, all, "finished"); len(ended) != 1 { + t.Fatalf("the new manager's first turn was logged %d times in the team that holds every team, want once", len(ended)) + } + if got := gmCalls.requests(); got != 0 { + t.Fatalf("the global manager was woken %d times by the new manager's first turn ending", got) + } + + if said, failed := callTool(t, ops.teamPostTool, `{"to":"manager","text":"ops is set up"}`); failed { + t.Fatalf("the new manager's report was refused: %q", said) + } + waitRequests(t, gmCalls, 1) + if asked := userTextIn(gmCalls.request(0)); !strings.Contains(asked, "from @ops: ops is set up") { + t.Fatalf("the global manager's turn lacks the new manager's report:\n%s", asked) + } +} + +func TestTeamEventWakesTheGlobalManager(t *testing.T) { + for _, c := range []struct { + state, text string + root, want bool + }{ + {teams.StateFinished, "finished", false, true}, + {teams.StateFinished, "finished", true, false}, + {teams.StateIdle, teams.StoppedByPerson, false, true}, + {teams.StateIdle, teams.StoppedByPerson, true, false}, + {teams.StateFailed, "failed: no key", true, true}, + {teams.StateAsking, "asks: which branch?", true, true}, + {teams.StateRunning, "no longer waiting", true, false}, + } { + if got := teamEventWakesManagerOf(c.root, c.state, c.text); got != c.want { + t.Errorf("in the team that holds every team %v, %s %q wakes the manager: %v, want %v", c.root, c.state, c.text, got, c.want) + } + } +} diff --git a/internal/session/team_sendoutcome.go b/internal/session/team_sendoutcome.go index d901ca5dbc..23f1cadd60 100644 --- a/internal/session/team_sendoutcome.go +++ b/internal/session/team_sendoutcome.go @@ -137,7 +137,7 @@ func (a *Agent) teamSendOutcome(ctx context.Context, profile string, team teams. said = append(said, strings.Join(asking, ", ")+" "+isAre(len(asking))+" waiting on the person and "+readsRead(len(asking))+" it once they answer.") } if len(started)+len(busy)+len(asking) > 0 { - said = append(said, "Replies and endings wake you when they arrive, so there is no need to wait or poll.") + said = append(said, teamSendWakesSentence(team.Root)) } for _, member := range paused { said = append(said, fmt.Sprintf("@%s did NOT start: %s. Tell the person it is paused; only they can resume it.", member.Handle, teamPausedWhy)) @@ -203,3 +203,15 @@ func itThey(n int) string { } return "they" } + +// teamSendWakesSentence is what a send's answer says comes back on its own. +// The global manager is not woken by a turn ending in the root, finished or +// stopped ([teamEventWakesManagerOf]), so its answer does not promise that, +// and says what closes a directive instead. Its sends reach top-level +// managers only ([managedView]), which is who the sentence names. +func teamSendWakesSentence(root bool) string { + if root { + return "Their replies, questions and failures wake you when they arrive, so there is no need to wait or poll. A top-level team's manager answers a directive with team_post: its turn finishing or being stopped does not wake you, and team_status says where it stands." + } + return "Replies and endings wake you when they arrive, so there is no need to wait or poll." +} diff --git a/internal/session/team_wakewatch.go b/internal/session/team_wakewatch.go index 7066f6740e..3ef7dc983a 100644 --- a/internal/session/team_wakewatch.go +++ b/internal/session/team_wakewatch.go @@ -455,11 +455,51 @@ func teamEventWakesManager(state, text string) bool { return false } +// teamEventWakesManagerOf is [teamEventWakesManager] for an event in one team, +// root when that is the team that holds every team. +// +// The global manager is not woken by a turn ending in the root, finished or +// stopped. Its members are the managers of whole teams, each a conversation +// the person talks to, and every answer one of them gave the person ended a +// turn. Measured 2026-11-24 in the desktop app: one afternoon of the person +// talking to two crew managers woke the global manager 22 times, 803,496 input +// tokens, and it answered "[no change]" to all 22; the day before, one such +// wake had it read a manager's conversation and send the person's own request +// back to that manager as a directive. What a top-level manager wants the +// global manager to act on it posts (a note wakes), and what needs somebody +// still wakes: a failure, and a question or a permission it is held on. The +// ending itself stays in the team's traffic, where team_status reads it. +// +// The rule is the whole root's, not the top-level managers' alone. A seat in +// the root is never taken back, so it also holds a manager that was replaced +// or whose team closed, and whoever the person put there; the global manager +// neither sees nor directs those ([managedView]), so their endings are ones +// it can do nothing with. +func teamEventWakesManagerOf(root bool, state, text string) bool { + if root { + return state == teams.StateFailed || state == teams.StateAsking + } + return teamEventWakesManager(state, text) +} + +// topManager reports whether key manages an open top-level team now +// ([teams.File.TopManagers]), which is what [teamRole.top] says of a seat in +// the root. +func topManager(file *teams.File, key string) bool { + for _, m := range file.TopManagers() { + if m.Key == key { + return true + } + } + return false +} + // teamWakes reports whether an entry starts this conversation's turn when it // is idle: a directive from the manager to its handle or to everyone, for a // member; a member's post to the manager, or a member's finished, failed or // asking event, for the manager. The watch suppresses a finished event when -// the same turn already posted a reply. +// the same turn already posted a reply, and the global manager is not woken by +// a turn ending in the root ([teamEventWakesManagerOf]). func teamWakes(role teamRole, entry teams.Entry) bool { if !role.joinedAt.IsZero() && entry.At.Before(role.joinedAt) { return false @@ -481,12 +521,7 @@ func teamWakes(role teamRole, entry teams.Entry) bool { case teams.KindNote: return entry.To == teams.ToManager && strings.TrimSpace(entry.Text) != "" case teams.KindEvent: - // The global manager is not woken by a stop the person made in a - // conversation they were talking to directly. - if role.root && entry.State == teams.StateIdle { - return false - } - return teamEventWakesManager(entry.State, entry.Text) + return teamEventWakesManagerOf(role.root, entry.State, entry.Text) } return false } diff --git a/internal/session/teamevent.go b/internal/session/teamevent.go index 22d5522191..62c831e1b5 100644 --- a/internal/session/teamevent.go +++ b/internal/session/teamevent.go @@ -184,7 +184,7 @@ func (a *Agent) writeTeamEvent(event teamEvent) { if err != nil || !role.wakes { continue } - if teamEventWakesManager(event.state, text) && !(role.root && event.state == teams.StateIdle) { + if teamEventWakesManagerOf(role.root, event.state, text) { // These wake the manager, so a manager nobody has open is opened // (team_wakewatch.go). a.rouseManager(profile, role, event.origin.provenance(role.key))