running and cancel all commands

This commit is contained in:
ari committed 2026-11-01 20:29:39 -05:00
1 parent 917a361370
commit af249326e8
7 files changed
+145 -6

No files matched your search

+7 -1
View File
@@ -37,10 +37,12 @@ const helpText = "I pass your messages to an agent running in its own container.
"and anyone here can use it by @mentioning me.\n" +
"• Every new message starts a new session. Reply in its thread to keep going.\n" +
"• In a channel thread I only read replies that @mention me, unless the thread or channel is switched with `listen` (see below).\n" +
"• React with :x: to stop a reply part-way.\n" +
"• React with :x: to stop a reply part-way, or send `cancel all` to stop every reply here.\n" +
"*Commands* (send as a new message, not in a thread. `move` and `listen` work in threads too):\n" +
"• `status`: state, size and idle limit, with buttons\n" +
"• `sessions`: past sessions, with links\n" +
"• `running`: the replies I'm working on here right now\n" +
"• `cancel all`: cancel every reply I'm working on here (sessions are kept)\n" +
"• `stop`: shut the container down (files are kept)\n" +
"• `scale up` / `scale down`: double or halve RAM and CPU\n" +
"• `scale 8G 4cpu`: set an exact size. `scale +2G` or `scale -1cpu`: change by that much\n" +
@@ -434,6 +436,10 @@ func (b *Bot) runCommand(ctx context.Context, m incoming, verb, args string, age
}
case verb == "sessions" && args == "":
reply = b.sessionList(ctx, ownerID)
case verb == "running" && args == "":
reply = b.runningList(ctx, m.channel)
case isCancelAll(verb, args):
reply = b.cancelAll(m, agent)
case verb == "status" && args == "":
if agent {
return b.statusText(ctx, ownerID, kind), true
+85
View File
@@ -11,6 +11,7 @@ import (
"github.com/slack-go/slack"
"slackerbot/internal/commands"
"slackerbot/internal/model"
)
@@ -28,6 +29,90 @@ const (
func isInFlight(verb string) bool { return verb == "in-flight" || verb == "inflight" }
func isCancelAll(verb, args string) bool { return verb == "cancel" && strings.EqualFold(args, "all") }
func (b *Bot) channelTurns(channel string) []model.Turn {
var out []model.Turn
for _, t := range b.store.Turns() {
if t.Channel == channel {
out = append(out, t)
}
}
return out
}
// A turn can only be cancelled once its prompt is running, the same as the Cancel button.
func (b *Bot) turnCancel(t model.Turn) (context.CancelFunc, bool) {
cancel, ok := b.running.Load(t.Channel + "/" + t.ReplyTS)
if !ok {
return nil, false
}
return cancel.(context.CancelFunc), true
}
func (b *Bot) runningList(ctx context.Context, channel string) string {
turns := b.channelTurns(channel)
if len(turns) == 0 {
return "Nothing is running here right now."
}
var sb strings.Builder
sb.WriteString("*Running replies* (`cancel all` stops them all):\n")
for _, t := range turns {
name := title(t.Text)
if s := b.store.Session(t.Channel, t.ThreadTS); s != nil {
name = s.Title
}
if link, err := b.api.GetPermalinkContext(ctx, &slack.PermalinkParameters{Channel: t.Channel, Ts: t.ThreadTS}); err == nil {
name = fmt.Sprintf("<%s|%s>", link, name)
}
doing := "getting the container ready"
if _, ok := b.turnCancel(t); ok {
doing = "working"
}
fmt.Fprintf(&sb, "• %s · for %s · %s, started %s ago\n", name, b.displayName(ctx, t.User), doing,
commands.Human(time.Since(t.StartedAt)))
}
return strings.TrimSpace(sb.String())
}
// The agent's own reply is spared, or calling the tool would cut off its answer.
func (b *Bot) cancelAll(m incoming, agent bool) string {
var cancelled, starting int
for _, t := range b.channelTurns(m.channel) {
if agent && t.ThreadTS == m.threadTS {
continue
}
cancel, ok := b.turnCancel(t)
if !ok {
starting++
continue
}
cancel()
cancelled++
}
if cancelled == 0 && starting == 0 {
if agent {
return "No other replies are running here."
}
return "Nothing is running here right now."
}
reply := fmt.Sprintf("Cancelled %s. Each session carries on with the next message in its thread.", replies(cancelled))
if cancelled == 0 {
reply = "Nothing was cancelled."
}
if starting > 0 {
reply += fmt.Sprintf(" Not cancelled yet, because the container is still getting ready: %s. Try again in a moment.", replies(starting))
}
return reply
}
func replies(n int) string {
if n == 1 {
return "1 reply"
}
return fmt.Sprintf("%d replies", n)
}
func (b *Bot) trackTurn(m incoming, root, replyTS string, prior *model.Turn) *model.Turn {
ownerID, _ := m.owner()
t := &model.Turn{
+38
View File
@@ -164,6 +164,44 @@ func TestInFlightIsForSuperUsers(t *testing.T) {
}
}
func TestRunningAndCancelAll(t *testing.T) {
h := newTurnHarness(t)
b := h.bot()
cancelled := map[string]bool{}
for _, ts := range []string{"1.0", "2.0", "3.0"} {
_ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: ts, MessageTS: ts, ReplyTS: ts + "1", User: "UANA",
Text: "task " + ts, StartedAt: time.Now()})
}
_ = h.c.Store.PutTurn(&model.Turn{Channel: "C2", ThreadTS: "4.0", MessageTS: "4.0", ReplyTS: "4.01", Text: "elsewhere", StartedAt: time.Now()})
for _, key := range []string{"C1/1.01", "C1/2.01", "C2/4.01"} {
b.running.Store(key, context.CancelFunc(func() { cancelled[key] = true }))
}
m := incoming{user: "UANA", channel: "C1", ts: "9.0"}
reply, ok := b.runCommand(h.c.Ctx, m, "running", "", false)
if !ok || !strings.Contains(reply, "task 1.0") || !strings.Contains(reply, "task 3.0") || strings.Contains(reply, "elsewhere") ||
!strings.Contains(reply, "getting the container ready") {
t.Fatalf("running lists this channel's replies: %q %v", reply, ok)
}
agent := m
agent.threadTS = "1.0"
if reply, _ := b.runCommand(h.c.Ctx, agent, "cancel", "all", true); !strings.Contains(reply, "Cancelled 1 reply") ||
cancelled["C1/1.01"] || !cancelled["C1/2.01"] {
t.Fatalf("the agent cancels every other reply but its own: %q %v", reply, cancelled)
}
if reply, _ := b.runCommand(h.c.Ctx, m, "cancel", "all", false); !strings.Contains(reply, "Cancelled 2 replies") ||
!strings.Contains(reply, "still getting ready: 1 reply") || !cancelled["C1/1.01"] || cancelled["C2/4.01"] {
t.Fatalf("cancel all stops this channel's replies only: %q %v", reply, cancelled)
}
if _, ok := b.runCommand(h.c.Ctx, m, "cancel", "all the meetings", false); ok {
t.Fatal("a sentence that starts with cancel goes to the agent")
}
if reply, _ := b.runCommand(h.c.Ctx, incoming{user: "UANA", channel: "C3"}, "running", "", false); reply != "Nothing is running here right now." {
t.Fatalf("empty channel: %q", reply)
}
}
func TestInFlightCommandIsPrivate(t *testing.T) {
h := newTurnHarness(t)
b := h.bot()
+4
View File
@@ -36,6 +36,10 @@ var Commands = []SlackCommand{
Help: "Show this container's state, size, node, time unused, idle limit, home folder usage and every node."},
{Name: "sessions", Verb: "sessions", Shows: true,
Help: "List this container's recent sessions (Slack threads), with links."},
{Name: "running", Verb: "running", Shows: true, ReadOnly: true,
Help: "List the replies being worked on right now in this channel or DM, including yours, with links, who asked and how long they've run."},
{Name: "cancel_all", Verb: "cancel all",
Help: "Cancel every other reply being worked on right now in this channel or DM, like pressing Cancel on each. Your own reply carries on, and every session is kept."},
{Name: "nodes", Verb: "nodes", Shows: true,
Help: "List every node with the CPU, RAM and storage free on it, and where this container runs."},
{Name: "permissions", Verb: "permissions", Shows: true,
+5 -1
View File
@@ -18,7 +18,7 @@ func TestEveryCommandIsListed(t *testing.T) {
for _, tool := range s.tools() {
names = append(names, tool.(map[string]any)["name"].(string))
}
for _, want := range []string{"send_file", "status", "sessions", "nodes", "permissions", "stop", "scale", "keep",
for _, want := range []string{"send_file", "status", "sessions", "running", "cancel_all", "nodes", "permissions", "stop", "scale", "keep",
"storage", "move", "listen", "grant", "revoke", "reset_permissions", "dm_permission", "managers"} {
if !strings.Contains(" "+strings.Join(names, " ")+" ", " "+want+" ") {
t.Fatalf("%s missing from %v", want, names)
@@ -85,6 +85,10 @@ func TestCommandCall(t *testing.T) {
if !ok || c.Text(args) != "scale up" {
t.Fatalf("by title: %+v %q %v", c, args, ok)
}
c, args, ok = CommandCall(acp.Update{Kind: "tool_call", Title: "slackerbot: cancel all"})
if !ok || c.Text(args) != "cancel all" || !c.Changes(args) {
t.Fatalf("cancel all by title: %+v %q %v", c, args, ok)
}
if _, _, ok := CommandCall(acp.Update{Kind: "tool_call", ToolName: "slackerbot__send_file"}); ok {
t.Fatal("send_file isn't a command")
}