resume active sessions

This commit is contained in:
ari committed 2026-11-01 15:52:46 -05:00
1 parent 9e94c05fb6
commit 917a361370
19 files changed
+871 -42

No files matched your search

+2
View File
@@ -7,6 +7,7 @@ go_library(
"assistant.go",
"bot.go",
"files.go",
"inflight.go",
"lifecycle.go",
"listen.go",
"manage.go",
@@ -40,6 +41,7 @@ go_test(
srcs = [
"agent_test.go",
"bot_test.go",
"inflight_test.go",
"lifecycle_test.go",
"update_test.go",
"version_test.go",
+39 -5
View File
@@ -55,6 +55,7 @@ const helpText = "I pass your messages to an agent running in its own container.
"• `@slacker /listen channel all-messages` / `@slacker /listen channel only-mentions`: set the channel's default for threads " +
"(a top-level `/listen all-messages` does the same). `@slacker /listen channel`: show it. `listen` without the slash works too\n" +
"• `permissions`: who can scale, change storage, move or set timeouts here\n" +
"• `in-flight` / `in-flight all`: super users only. Shows the replies I'd pick back up after a restart, for debugging\n" +
"• `managers`: who the user managers are\n" +
"• Managers: `grant @alice move scale`, `revoke @alice move` / `revoke @alice`, `permissions reset`, " +
"`grant default @alice all` / `revoke default @alice`, `dm storage on|off`, `managers add|remove @alice`\n" +
@@ -85,6 +86,8 @@ type Bot struct {
waitMu sync.Mutex
waiting map[string]map[string]bool
turnsWG sync.WaitGroup
}
type incoming struct {
@@ -122,7 +125,10 @@ func New(cfg *config.Config, store *state.Store, mgr *manager.Manager) (*Bot, er
func (b *Bot) Run(ctx context.Context) error {
go b.events(ctx)
go b.reaper(ctx)
return b.sm.RunContext(ctx)
go b.resumeTurns(ctx)
err := b.sm.RunContext(ctx)
b.waitForTurns(shutdownGrace)
return err
}
func (b *Bot) events(ctx context.Context) {
@@ -199,6 +205,13 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
if commandable && m.threadTS != "" && !m.heard && b.moveCommand(ctx, m, m.threadTS) {
return
}
b.converse(ctx, m, nil)
}
// converse runs one turn with the agent. prior is the turn a restart cut off, if this picks it back up.
func (b *Bot) converse(ctx context.Context, m incoming, prior *model.Turn) {
b.turnsWG.Add(1)
defer b.turnsWG.Done()
root := m.threadTS
if root == "" {
root = m.ts
@@ -209,6 +222,9 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
defer unlock()
sess := b.store.Session(m.channel, root)
if prior != nil {
b.say(ctx, m.channel, root, ":arrows_counterclockwise: slackerbot restarted part-way through this reply, so I'm picking it back up.")
}
_, ph, err := b.api.PostMessageContext(ctx, m.channel,
slack.MsgOptionText(":hourglass_flowing_sand: Getting the container ready…", false),
slack.MsgOptionTS(root))
@@ -216,6 +232,8 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
log.Printf("slack post: %v", err)
return
}
turn := b.trackTurn(m, root, ph, prior)
defer b.untrackTurn(ctx, turn)
edit := func(text string) {
_, _, _, _ = b.api.UpdateMessageContext(ctx, m.channel, ph, slack.MsgOptionText(text, false))
}
@@ -276,10 +294,15 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
sl.up(ctx)
var id string
// A new session (the old one was dropped) never saw the cut-off turn, so it gets the message again.
carryOn := turn.Prompted && sess != nil
text := m.text
if !m.dm {
text = b.displayName(ctx, m.user) + ": " + text
}
if carryOn {
text = resumeNote
}
if sess == nil {
if id, err = acp.NewSession(ctx, b.cfg.AgentCwd); err != nil {
edit(":warning: Couldn't start a session: " + err.Error())
@@ -304,10 +327,10 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
}
}
if m.dm {
if m.dm && !carryOn {
text = b.viewingNote(ctx, m.channel, root) + text
}
if len(m.files) > 0 {
if len(m.files) > 0 && !carryOn {
edit(":paperclip: Copying the attachments into the container…")
note, problems := b.attach(ctx, u, m)
if note != "" {
@@ -329,6 +352,8 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
b.setAssistantStatus(ctx, m.channel, root, "is working…")
defer b.setAssistantStatus(ctx, m.channel, root, "")
}
turn.Prompted = true
b.saveTurn(turn)
st := NewStreamer(b.api, m.channel, root, ph)
var sends tools.Calls
cmds := b.agentCommands(u, m, root)
@@ -337,7 +362,10 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
cmds.Update(ctx, up)
st.Update(up)
})
if pctx.Err() != nil && ctx.Err() == nil {
switch {
case ctx.Err() != nil:
stop, err = stopInterrupted, nil
case pctx.Err() != nil:
stop, err = "cancelled", nil
}
st.Finish(stop, err)
@@ -346,7 +374,7 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
_ = b.store.Touch(u.ID)
b.offerUpdate(ctx, u, m.channel, root)
switch {
case stop == "cancelled":
case stop == "cancelled", stop == stopInterrupted:
outcome = ""
case err == nil:
outcome = "white_check_mark"
@@ -424,6 +452,12 @@ func (b *Bot) runCommand(ctx context.Context, m incoming, verb, args string, age
reply = b.permissionsList(ctx, m.channel, kind)
case verb == "storage" || verb == "disk":
return b.storageFor(ctx, ownerID, kind, args)
case isInFlight(verb) && (args == "" || strings.EqualFold(args, "all")):
reply = b.inFlightFor(m, args != "", agent)
if !agent {
b.whisper(ctx, m, reply)
return "", true
}
default:
return "", false
}
+179
View File
@@ -0,0 +1,179 @@
package bot
import (
"context"
"fmt"
"log"
"sort"
"strings"
"time"
"unicode/utf8"
"github.com/slack-go/slack"
"slackerbot/internal/model"
)
const (
stopInterrupted = "interrupted"
shutdownGrace = 5 * time.Second
resumeWithin = time.Hour
maxResumes = 3
resumeNote = "(slackerbot restarted while you were working on the last message, so your turn was cut off. " +
"Carry on from where you stopped, without redoing work that's already done.)"
inFlightSlackLimit = 35000
)
func isInFlight(verb string) bool { return verb == "in-flight" || verb == "inflight" }
func (b *Bot) trackTurn(m incoming, root, replyTS string, prior *model.Turn) *model.Turn {
ownerID, _ := m.owner()
t := &model.Turn{
Channel: m.channel, ThreadTS: root, MessageTS: m.ts, ReplyTS: replyTS,
User: m.user, OwnerID: ownerID, DM: m.dm, Heard: m.heard, Text: m.text,
StartedAt: time.Now(),
}
for _, f := range m.files {
t.Files = append(t.Files, model.TurnFile{Name: f.Name, Mode: f.Mode, URL: f.URLPrivateDownload, Size: f.Size})
}
if prior != nil {
t.Prompted, t.Resumes, t.StartedAt = prior.Prompted, prior.Resumes+1, prior.StartedAt
}
b.saveTurn(t)
return t
}
func (b *Bot) saveTurn(t *model.Turn) {
if err := b.store.PutTurn(t); err != nil {
log.Printf("in-flight: saving %s/%s: %v", t.Channel, t.ThreadTS, err)
}
}
// A turn cut off by shutdown stays recorded, so the next start picks it back up.
func (b *Bot) untrackTurn(ctx context.Context, t *model.Turn) {
if ctx.Err() != nil {
return
}
if err := b.store.DeleteTurn(t.Channel, t.ThreadTS); err != nil {
log.Printf("in-flight: clearing %s/%s: %v", t.Channel, t.ThreadTS, err)
}
}
// waitForTurns gives running turns a moment to post their last edit before the process exits.
func (b *Bot) waitForTurns(grace time.Duration) {
done := make(chan struct{})
go func() {
b.turnsWG.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(grace):
}
}
func turnMessage(t model.Turn) incoming {
m := incoming{user: t.User, channel: t.Channel, ts: t.MessageTS, text: t.Text, dm: t.DM, heard: t.Heard}
if t.ThreadTS != t.MessageTS {
m.threadTS = t.ThreadTS
}
for _, f := range t.Files {
m.files = append(m.files, slack.File{Name: f.Name, Mode: f.Mode, URLPrivateDownload: f.URL, Size: f.Size})
}
return m
}
func (b *Bot) resumeTurns(ctx context.Context) {
for _, t := range b.store.Turns() {
go b.resume(ctx, t)
}
}
func (b *Bot) resume(ctx context.Context, t model.Turn) {
if time.Since(t.StartedAt) > resumeWithin || t.Resumes >= maxResumes {
log.Printf("in-flight: giving up on %s/%s (started %s, resumed %d times)", t.Channel, t.ThreadTS, t.StartedAt.Format(time.RFC3339), t.Resumes)
b.say(ctx, t.Channel, t.ThreadTS, ":warning: slackerbot restarted while it was answering this, and it's too late to pick it back up. "+
"Send your message again if you still need it.")
b.unreact(ctx, t.Channel, t.MessageTS, "eyes")
b.react(ctx, t.Channel, t.MessageTS, "warning")
if err := b.store.DeleteTurn(t.Channel, t.ThreadTS); err != nil {
log.Printf("in-flight: clearing %s/%s: %v", t.Channel, t.ThreadTS, err)
}
return
}
log.Printf("in-flight: resuming %s/%s for %s after a restart (prompted %v, resume %d)", t.Channel, t.ThreadTS, t.User, t.Prompted, t.Resumes+1)
b.converse(ctx, turnMessage(t), &t)
}
// inFlightFor shows the in-flight files as they are on disk. They hold message
// text, so only super users and user managers may read them.
func (b *Bot) inFlightFor(m incoming, all, agent bool) string {
p := b.store.Permissions()
channel := m.channel
if all {
channel = ""
if !p.IsManager(m.user) && len(p.Default[m.user]) == 0 {
return ":lock: Only user managers and super users on the default list can see every channel's in-flight replies."
}
} else if !p.IsSuperUser(m.channel, m.user) {
return ":lock: Only super users here and user managers can see the in-flight replies."
}
files, err := b.store.InFlightFiles(channel)
if err != nil {
return ":warning: Couldn't read the in-flight files: " + err.Error()
}
if len(files) == 0 {
if all {
return "No replies are in flight anywhere."
}
return "No replies are in flight here."
}
limit := inFlightSlackLimit
if agent {
limit = maxReplyBytes / 2
}
names := make([]string, 0, len(files))
for name := range files {
names = append(names, name)
}
sort.Strings(names)
var sb strings.Builder
sb.WriteString("*In-flight replies* (what I'd pick back up after a restart):\n")
for i, name := range names {
data := strings.TrimSpace(string(files[name]))
room := limit - sb.Len() - len(name) - 20
if room <= 0 {
fmt.Fprintf(&sb, "_(%d more files not shown)_", len(names)-i)
break
}
if len(data) > room {
data = cutBytes(data, room) + fmt.Sprintf("\n… (cut off, %d bytes in total)", len(files[name]))
}
fmt.Fprintf(&sb, "`%s`\n```\n%s\n```\n", name, data)
}
return strings.TrimSpace(sb.String())
}
// The files hold other people's messages, so only the asker sees them.
func (b *Bot) whisper(ctx context.Context, m incoming, text string) {
opts := []slack.MsgOption{slack.MsgOptionText(text, false)}
if m.threadTS != "" {
opts = append(opts, slack.MsgOptionTS(m.threadTS))
}
if _, err := b.api.PostEphemeralContext(ctx, m.channel, m.user, opts...); err != nil {
log.Printf("slack ephemeral: %v", err)
}
}
func cutBytes(s string, n int) string {
if len(s) <= n {
return s
}
for n > 0 && !utf8.RuneStart(s[n]) {
n--
}
return s[:n]
}
+177
View File
@@ -0,0 +1,177 @@
package bot
import (
"context"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/slack-go/slack"
"slackerbot/internal/acp"
"slackerbot/internal/model"
"slackerbot/internal/testutil"
"slackerbot/internal/testutil/clustertest"
)
type turnHarness struct {
c *clustertest.Cluster
log *slackLog
url string
goose *testutil.FakeGoose
}
func newTurnHarness(t *testing.T) *turnHarness {
c := clustertest.New(t)
c.Cfg.AgentCwd = "/root"
c.AddOwner(t, testutil.HomeData(), true)
u := c.Store.Owner("C1")
u.Secret = "agent-secret"
_ = c.Store.PutOwner(u)
c.Store.SeedPermissions(model.PermissionsConfig{Managers: []string{"UBOSS"}, Default: map[string][]string{"UBOSS": {"all"}, "USUPER": {"keep"}}})
goose := testutil.NewFakeGoose()
c.Master.Goose = goose
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
srv := httptest.NewServer(log)
t.Cleanup(srv.Close)
return &turnHarness{c: c, log: log, url: srv.URL, goose: goose}
}
// Each bot is a fresh process: nothing in memory carries over, only the store.
func (h *turnHarness) bot() *Bot {
return &Bot{cfg: h.c.Cfg, store: h.c.Store, mgr: h.c.Mgr, api: slack.New("xoxb", slack.OptionAPIURL(h.url+"/")),
acps: map[string]*acp.ACP{}, busy: map[string]int{}}
}
func (s *slackLog) anyText(want string) bool {
s.mu.Lock()
defer s.mu.Unlock()
for _, text := range s.text {
if strings.Contains(text, want) {
return true
}
}
return false
}
func TestRestartResumesInFlightTurn(t *testing.T) {
h := newTurnHarness(t)
c := h.c
h.goose.Hold(true)
ctx, shutdown := context.WithCancel(c.Ctx)
done := make(chan struct{})
go func() {
defer close(done)
h.bot().onMessage(ctx, incoming{user: "UANA", channel: "C1", ts: "5.0", text: "build the thing"})
}()
select {
case <-h.goose.Holding:
case <-time.After(10 * time.Second):
t.Fatal("the prompt never reached the agent")
}
turns := c.Store.Turns()
if len(turns) != 1 || !turns[0].Prompted || turns[0].ThreadTS != "5.0" || turns[0].User != "UANA" {
t.Fatalf("the running turn should be recorded: %+v", turns)
}
if _, err := os.Stat(filepath.Join(c.Cfg.StateDir, "in-flight-C1.json")); err != nil {
t.Fatal(err)
}
shutdown()
<-done
if len(c.Store.Turns()) != 1 {
t.Fatal("a turn cut off by shutdown must stay recorded")
}
if !h.log.anyText("slackerbot is restarting") {
t.Fatal("the cut-off reply should say it'll be picked back up")
}
h.goose.Hold(false)
h.bot().resumeTurns(c.Ctx)
testutil.WaitFor(t, "the turn to be resumed and finish", func() bool { return len(c.Store.Turns()) == 0 })
prompts := h.goose.Prompts()
if len(prompts) != 2 || !strings.Contains(prompts[0], "build the thing") || prompts[1] != resumeNote {
t.Fatalf("the agent should be told to carry on, not get the message twice: %q", prompts)
}
if !h.log.anyText("picking it back up") {
t.Fatal("the thread should say the reply is being picked back up")
}
if _, err := os.Stat(filepath.Join(c.Cfg.StateDir, "in-flight-C1.json")); !os.IsNotExist(err) {
t.Fatal("the in-flight file should go once the turn is done")
}
}
func TestResumeSendsMessageTheAgentNeverGot(t *testing.T) {
h := newTurnHarness(t)
_ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "7.0", MessageTS: "7.0", User: "UANA", OwnerID: "C1",
Text: "check the logs", StartedAt: time.Now()})
h.bot().resumeTurns(h.c.Ctx)
testutil.WaitFor(t, "the turn to finish", func() bool { return len(h.c.Store.Turns()) == 0 })
if prompts := h.goose.Prompts(); len(prompts) != 1 || !strings.Contains(prompts[0], "check the logs") {
t.Fatalf("a turn cut off before the prompt should send the message: %q", prompts)
}
}
func TestStaleTurnIsDroppedNotResumed(t *testing.T) {
h := newTurnHarness(t)
_ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "8.0", MessageTS: "8.0", User: "UANA", OwnerID: "C1",
Text: "old", Prompted: true, StartedAt: time.Now().Add(-2 * resumeWithin)})
_ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "9.0", MessageTS: "9.0", User: "UANA", OwnerID: "C1",
Text: "looping", Prompted: true, Resumes: maxResumes, StartedAt: time.Now()})
h.bot().resumeTurns(h.c.Ctx)
testutil.WaitFor(t, "the turns to be dropped", func() bool { return len(h.c.Store.Turns()) == 0 })
if len(h.goose.Prompts()) != 0 {
t.Fatal("stale turns must not reach the agent")
}
if !h.log.anyText("too late to pick it back up") {
t.Fatal("the thread should say to send the message again")
}
}
func TestInFlightIsForSuperUsers(t *testing.T) {
h := newTurnHarness(t)
_ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "1.0", MessageTS: "1.0", Text: "secret plans", StartedAt: time.Now()})
_ = h.c.Store.PutTurn(&model.Turn{Channel: "D2", ThreadTS: "2.0", MessageTS: "2.0", Text: "dm plans", DM: true, StartedAt: time.Now()})
b := h.bot()
ask := func(user, channel string, all bool) string {
return b.inFlightFor(incoming{user: user, channel: channel}, all, true)
}
if got := ask("UANA", "C1", false); !strings.Contains(got, ":lock:") || strings.Contains(got, "secret plans") {
t.Fatalf("ordinary users can't read it: %q", got)
}
if got := ask("USUPER", "C1", false); !strings.Contains(got, "in-flight-C1.json") || !strings.Contains(got, "secret plans") || strings.Contains(got, "dm plans") {
t.Fatalf("a super user sees this channel's file: %q", got)
}
if got := ask("USUPER", "C1", true); !strings.Contains(got, "in-flight-D2.json") || !strings.Contains(got, "secret plans") {
t.Fatalf("a default-list super user sees every file: %q", got)
}
if got := ask("UBOSS", "C3", false); !strings.Contains(got, "No replies are in flight here") {
t.Fatalf("empty channel: %q", got)
}
_ = h.c.Store.UpdatePermissions(func(p *model.PermissionsConfig) error {
p.Grant("C1", []string{"UANA"}, []model.Action{model.ActMove})
return nil
})
if got := ask("UANA", "C1", false); !strings.Contains(got, "secret plans") {
t.Fatalf("a channel's own super users see its file: %q", got)
}
if got := ask("UANA", "C1", true); !strings.Contains(got, ":lock:") {
t.Fatalf("every channel needs the default list or a user manager: %q", got)
}
}
func TestInFlightCommandIsPrivate(t *testing.T) {
h := newTurnHarness(t)
b := h.bot()
reply, ok := b.runCommand(h.c.Ctx, incoming{user: "UBOSS", channel: "C1", ts: "1.0"}, "in-flight", "", false)
if !ok || reply != "" {
t.Fatalf("in-flight should be answered ephemerally: %q %v", reply, ok)
}
if _, ok := b.runCommand(h.c.Ctx, incoming{user: "UBOSS", channel: "C1", ts: "1.0"}, "in-flight", "entertainment is great", false); ok {
t.Fatal("a sentence that starts with in-flight goes to the agent")
}
}
+2
View File
@@ -91,6 +91,8 @@ func (s *Streamer) Finish(stopReason string, err error) {
note = "_(the agent declined this one)_"
case stopReason == "cancelled":
note = "_(cancelled)_"
case stopReason == stopInterrupted:
note = "_(slackerbot is restarting. I'll pick this reply back up once it's back.)_"
}
if note != "" {
if s.full.Len() > 0 {
+27
View File
@@ -71,6 +71,33 @@ type Session struct {
Listen ListenMode `json:"listen,omitempty"`
}
// Turn is a reply the bot is still working on, kept so it can carry on after a restart.
type Turn struct {
Channel string `json:"channel"`
ThreadTS string `json:"thread_ts"`
MessageTS string `json:"message_ts"`
ReplyTS string `json:"reply_ts"`
User string `json:"user"`
OwnerID string `json:"owner_id"`
DM bool `json:"dm,omitempty"`
Heard bool `json:"heard,omitempty"`
Text string `json:"text"`
Files []TurnFile `json:"files,omitempty"`
// The agent got the prompt, so the session already holds part of the turn.
Prompted bool `json:"prompted,omitempty"`
Resumes int `json:"resumes,omitempty"`
StartedAt time.Time `json:"started_at"`
}
type TurnFile struct {
Name string `json:"name"`
Mode string `json:"mode,omitempty"`
URL string `json:"url"`
Size int `json:"size"`
}
// Whether the bot reads every reply in a channel thread, or only ones that
// @mention it. DMs always read everything.
type ListenMode string
+7
View File
@@ -124,6 +124,13 @@ func (p PermissionsConfig) Allowed(kind OwnerKind, channel, user string, a Actio
func (p PermissionsConfig) IsManager(user string) bool { return slices.Contains(p.Managers, user) }
// A super user is anyone on the list that applies to the channel, with any permission.
// DM channels have no list of their own, so the default list applies.
func (p PermissionsConfig) IsSuperUser(channel, user string) bool {
list, _ := p.SuperUsers(channel)
return p.IsManager(user) || len(list[user]) > 0
}
func (p PermissionsConfig) CanManage(channel, user string) bool {
return p.IsManager(user) || p.Allowed(OwnerChannel, channel, user, ActManage)
}
+8 -2
View File
@@ -2,7 +2,10 @@ load("@rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "state",
srcs = ["store.go"],
srcs = [
"inflight.go",
"store.go",
],
importpath = "slackerbot/internal/state",
visibility = ["//slackerbot:__subpackages__"],
deps = ["//slackerbot/internal/model"],
@@ -10,7 +13,10 @@ go_library(
go_test(
name = "state_test",
srcs = ["store_test.go"],
srcs = [
"inflight_test.go",
"store_test.go",
],
embed = [":state"],
deps = ["//slackerbot/internal/model"],
)
+156
View File
@@ -0,0 +1,156 @@
package state
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"slackerbot/internal/model"
)
const (
inFlightPrefix = "in-flight-"
inFlightSuffix = ".json"
)
var channelID = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
type inFlightFile struct {
Turns map[string]*model.Turn `json:"turns"`
}
func (s *Store) InFlightPath(channel string) (string, error) {
if !channelID.MatchString(channel) {
return "", fmt.Errorf("%q isn't a channel ID", channel)
}
return filepath.Join(filepath.Dir(s.path), inFlightPrefix+channel+inFlightSuffix), nil
}
// A broken file is left alone for debugging rather than stopping the bot.
func (s *Store) loadTurns() error {
paths, err := filepath.Glob(filepath.Join(filepath.Dir(s.path), inFlightPrefix+"*"+inFlightSuffix))
if err != nil {
return err
}
for _, p := range paths {
b, err := os.ReadFile(p)
if err != nil {
return err
}
var f inFlightFile
if err := json.Unmarshal(b, &f); err != nil {
log.Printf("state: skipping %s: %v", p, err)
continue
}
channel := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(p), inFlightPrefix), inFlightSuffix)
for thread, t := range f.Turns {
if t == nil {
continue
}
if s.turns[channel] == nil {
s.turns[channel] = map[string]*model.Turn{}
}
s.turns[channel][thread] = t
}
}
return nil
}
func (s *Store) saveTurns(channel string) error {
p, err := s.InFlightPath(channel)
if err != nil {
return err
}
if len(s.turns[channel]) == 0 {
delete(s.turns, channel)
if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
b, err := json.MarshalIndent(inFlightFile{Turns: s.turns[channel]}, "", " ")
if err != nil {
return err
}
tmp := p + ".tmp"
if err := os.WriteFile(tmp, b, 0o600); err != nil {
return err
}
return os.Rename(tmp, p)
}
// One turn runs per thread at a time, so the thread keys it.
func (s *Store) PutTurn(t *model.Turn) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, err := s.InFlightPath(t.Channel); err != nil {
return err
}
if s.turns[t.Channel] == nil {
s.turns[t.Channel] = map[string]*model.Turn{}
}
c := *t
c.Files = append([]model.TurnFile(nil), t.Files...)
s.turns[t.Channel][t.ThreadTS] = &c
return s.saveTurns(t.Channel)
}
func (s *Store) DeleteTurn(channel, threadTS string) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.turns[channel][threadTS]; !ok {
return nil
}
delete(s.turns[channel], threadTS)
return s.saveTurns(channel)
}
// Turns lists every in-flight turn, oldest first.
func (s *Store) Turns() []model.Turn {
s.mu.Lock()
defer s.mu.Unlock()
var out []model.Turn
for _, list := range s.turns {
for _, t := range list {
c := *t
c.Files = append([]model.TurnFile(nil), t.Files...)
out = append(out, c)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].StartedAt.Before(out[j].StartedAt) })
return out
}
// InFlightFiles reads the in-flight files as they are on disk, for debugging.
// An empty channel means every channel.
func (s *Store) InFlightFiles(channel string) (map[string][]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()
pattern := inFlightPrefix + "*" + inFlightSuffix
if channel != "" {
p, err := s.InFlightPath(channel)
if err != nil {
return nil, err
}
pattern = filepath.Base(p)
}
paths, err := filepath.Glob(filepath.Join(filepath.Dir(s.path), pattern))
if err != nil {
return nil, err
}
out := map[string][]byte{}
for _, p := range paths {
b, err := os.ReadFile(p)
if err != nil {
return nil, err
}
out[filepath.Base(p)] = b
}
return out, nil
}
@@ -0,0 +1,83 @@
package state
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"time"
"slackerbot/internal/model"
)
func TestTurnsLiveInOneFilePerChannel(t *testing.T) {
dir := t.TempDir()
s, err := Open(filepath.Join(dir, "state.json"))
if err != nil {
t.Fatal(err)
}
now := time.Now()
_ = s.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "1.0", MessageTS: "1.0", Text: "first", StartedAt: now.Add(-time.Minute),
Files: []model.TurnFile{{Name: "a.txt", URL: "https://files/a"}}})
_ = s.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "2.0", MessageTS: "3.0", Text: "second", StartedAt: now})
_ = s.PutTurn(&model.Turn{Channel: "D9", ThreadTS: "4.0", MessageTS: "4.0", DM: true, StartedAt: now})
if err := s.PutTurn(&model.Turn{Channel: "../evil", ThreadTS: "1.0"}); err == nil {
t.Fatal("a channel that isn't an ID must not name a file")
}
raw, err := os.ReadFile(filepath.Join(dir, "in-flight-C1.json"))
if err != nil {
t.Fatal(err)
}
var f struct {
Turns map[string]model.Turn `json:"turns"`
}
if err := json.Unmarshal(raw, &f); err != nil || len(f.Turns) != 2 || f.Turns["2.0"].Text != "second" {
t.Fatalf("in-flight-C1.json: %s %v", raw, err)
}
if _, err := os.Stat(filepath.Join(dir, "in-flight-D9.json")); err != nil {
t.Fatal("each channel gets its own file")
}
s2, err := Open(filepath.Join(dir, "state.json"))
if err != nil {
t.Fatal(err)
}
turns := s2.Turns()
if len(turns) != 3 || turns[0].Text != "first" || len(turns[0].Files) != 1 {
t.Fatalf("turns not reloaded oldest-first: %+v", turns)
}
_ = s2.DeleteTurn("D9", "4.0")
if _, err := os.Stat(filepath.Join(dir, "in-flight-D9.json")); !errors.Is(err, os.ErrNotExist) {
t.Fatal("a channel's file goes once its last turn ends")
}
files, err := s2.InFlightFiles("")
if err != nil || len(files) != 1 || files["in-flight-C1.json"] == nil {
t.Fatalf("in-flight files: %v %v", files, err)
}
if files, _ := s2.InFlightFiles("D9"); len(files) != 0 {
t.Fatalf("no file for a channel with nothing in flight: %v", files)
}
}
func TestBrokenInFlightFileDoesNotStopStartup(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "in-flight-C1.json"), []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "state.json"), []byte(`{"owners":{},"sessions":{}}`), 0o600); err != nil {
t.Fatal(err)
}
s, err := Open(filepath.Join(dir, "state.json"))
if err != nil {
t.Fatal(err)
}
if len(s.Turns()) != 0 {
t.Fatal("a broken file has no turns")
}
if _, err := os.Stat(filepath.Join(dir, "in-flight-C1.json")); err != nil {
t.Fatal("a broken file is kept for debugging")
}
}
+10 -6
View File
@@ -13,9 +13,10 @@ import (
)
type Store struct {
path string
mu sync.Mutex
data struct {
path string
mu sync.Mutex
turns map[string]map[string]*model.Turn // channel → thread → turn, one in-flight file per channel
data struct {
Owners map[string]*model.Owner `json:"owners"`
Sessions map[string]*model.Session `json:"sessions"`
Permissions *model.PermissionsConfig `json:"permissions,omitempty"`
@@ -24,14 +25,17 @@ type Store struct {
}
func Open(path string) (*Store, error) {
s := &Store{path: path}
s := &Store{path: path, turns: map[string]map[string]*model.Turn{}}
s.data.Owners = map[string]*model.Owner{}
s.data.Sessions = map[string]*model.Session{}
s.data.Listen = map[string]model.ListenMode{}
b, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return s, os.MkdirAll(filepath.Dir(path), 0o700)
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return s, err
}
return s, s.loadTurns()
}
if err != nil {
return nil, err
@@ -48,7 +52,7 @@ func Open(path string) (*Store, error) {
if s.data.Listen == nil {
s.data.Listen = map[string]model.ListenMode{}
}
return s, nil
return s, s.loadTurns()
}
func (s *Store) save() error {
+4 -1
View File
@@ -2,7 +2,10 @@ load("@rules_go//go:def.bzl", "go_library")
go_library(
name = "testutil",
srcs = ["testutil.go"],
srcs = [
"goose.go",
"testutil.go",
],
importpath = "slackerbot/internal/testutil",
visibility = ["//slackerbot:__subpackages__"],
deps = [
+133
View File
@@ -0,0 +1,133 @@
package testutil
import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"slackerbot/internal/node"
)
// FakeGoose speaks just enough ACP for the bot's turns. Held prompts don't
// finish until they're cancelled, like an agent in the middle of a long turn.
type FakeGoose struct {
mu sync.Mutex
prompts []string
hold bool
sessions int
// Gets each held prompt's text as it starts.
Holding chan string
}
func NewFakeGoose() *FakeGoose { return &FakeGoose{Holding: make(chan string, 8)} }
func (g *FakeGoose) Hold(on bool) {
g.mu.Lock()
defer g.mu.Unlock()
g.hold = on
}
func (g *FakeGoose) Prompts() []string {
g.mu.Lock()
defer g.mu.Unlock()
return append([]string(nil), g.prompts...)
}
func (g *FakeGoose) Conn() node.MsgConn {
return &gooseConn{g: g, out: make(chan []byte, 64), closed: make(chan struct{}), held: map[string]json.RawMessage{}}
}
type gooseConn struct {
g *FakeGoose
out chan []byte
closed chan struct{}
once sync.Once
mu sync.Mutex
held map[string]json.RawMessage
}
func (c *gooseConn) Read(ctx context.Context) ([]byte, error) {
select {
case b := <-c.out:
return b, nil
case <-c.closed:
return nil, errors.New("closed")
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (c *gooseConn) Close() error { c.once.Do(func() { close(c.closed) }); return nil }
func (c *gooseConn) send(v any) {
b, _ := json.Marshal(v)
select {
case c.out <- b:
case <-c.closed:
}
}
func (c *gooseConn) reply(id json.RawMessage, result any) {
c.send(map[string]any{"jsonrpc": "2.0", "id": id, "result": result})
}
func (c *gooseConn) Write(_ context.Context, b []byte) error {
var m struct {
ID json.RawMessage `json:"id"`
Method string `json:"method"`
Params struct {
SessionID string `json:"sessionId"`
Prompt []struct {
Text string `json:"text"`
} `json:"prompt"`
} `json:"params"`
}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
g := c.g
switch m.Method {
case "initialize":
c.reply(m.ID, map[string]any{"protocolVersion": 1, "agentCapabilities": map[string]any{"loadSession": true}})
case "session/new":
g.mu.Lock()
g.sessions++
id := fmt.Sprintf("sess-%d", g.sessions)
g.mu.Unlock()
c.reply(m.ID, map[string]any{"sessionId": id})
case "session/load":
c.reply(m.ID, nil)
case "session/prompt":
text := ""
if len(m.Params.Prompt) > 0 {
text = m.Params.Prompt[0].Text
}
g.mu.Lock()
g.prompts = append(g.prompts, text)
hold := g.hold
g.mu.Unlock()
if hold {
c.mu.Lock()
c.held[m.Params.SessionID] = m.ID
c.mu.Unlock()
g.Holding <- text
return nil
}
c.send(map[string]any{"jsonrpc": "2.0", "method": "session/update", "params": map[string]any{
"sessionId": m.Params.SessionID, "update": map[string]any{"sessionUpdate": "agent_message_chunk",
"content": map[string]any{"type": "text", "text": "done"}}}})
c.reply(m.ID, map[string]any{"stopReason": "end_turn"})
case "session/cancel":
c.mu.Lock()
id, ok := c.held[m.Params.SessionID]
delete(c.held, m.Params.SessionID)
c.mu.Unlock()
if ok {
c.reply(id, map[string]any{"stopReason": "cancelled"})
}
}
return nil
}
+5
View File
@@ -29,6 +29,8 @@ type FakeNode struct {
FailWrites bool
Usable model.Size
Version string
// Answers agent connections when set. Otherwise they echo.
Goose *FakeGoose
}
func NewFakeNode(name string) *FakeNode {
@@ -81,6 +83,9 @@ func (f *FakeNode) DialAgent(_ context.Context, machine, secret string) (node.Ms
if secret != "agent-secret" {
return nil, errors.New("bad agent secret")
}
if f.Goose != nil {
return f.Goose.Conn(), nil
}
return newEcho(), nil
}
+6 -1
View File
@@ -22,6 +22,8 @@ type SlackCommand struct {
Required bool
// With no args it only shows a setting.
Shows bool
// It only shows things, whatever the args.
ReadOnly bool
}
const (
@@ -66,6 +68,9 @@ var Commands = []SlackCommand{
{Name: "dm_permission", Verb: "dm", Required: true,
Help: "Allow or forbid everyone changing a setting on their own DM container. User managers only.",
Args: "A permission and on or off, like `storage off`"},
{Name: "in_flight", Verb: "in-flight", Shows: true, ReadOnly: true,
Help: "For debugging: show slackerbot's in-flight files, the replies it's still working on and would pick back up after a restart. Super users and user managers only.",
Args: "`all` for every channel's file. Empty shows this channel's"},
{Name: "managers", Verb: "managers", Shows: true,
Help: "Show, add or remove user managers.",
Args: "`add <@U012AB>` or `remove <@U012AB>`. Empty lists them"},
@@ -90,7 +95,7 @@ func (c SlackCommand) Missing(args string) bool {
}
func (c SlackCommand) Changes(args string) bool {
return !c.Shows || strings.TrimSpace(args) != ""
return !c.ReadOnly && (!c.Shows || strings.TrimSpace(args) != "")
}
// Reply is what the bot answers a command call with.