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

+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
}