resume active sessions
This commit is contained in:
1 parent
9e94c05fb6
commit
917a361370
19 files changed
+871
-42
No files matched your search
@@ -54,7 +54,7 @@ nix flake check --no-build
|
||||
|
||||
## Behaviour you must not break
|
||||
|
||||
- The `state.json` format: existing owners, sessions and permissions must still load. JSON field names are effectively a public format.
|
||||
- The `state.json` format: existing owners, sessions and permissions must still load. JSON field names are effectively a public format. The same goes for `in-flight-<channel>.json`: turns written by the previous build are resumed by the next one.
|
||||
- The cluster wire protocol: frame types and call names in `manager/cluster.go`. A master and slaves may run different versions for a while.
|
||||
- Machine names (`u-<id>`, `c-<id>`), unit names (`slackerbot-<machine>.service`), and paths (`/var/lib/slackerbot`, the agent env file at `/var/lib/slackerbot/agent.env`, which must match `nix/container.nix`).
|
||||
- Anything that decides whether a message is a command or goes to the agent: `commands.ErrNotCommand`. Ordinary sentences must still reach the agent.
|
||||
|
||||
+6
-4
@@ -29,6 +29,8 @@ flowchart LR
|
||||
|
||||
**A message's path:** Slack event (`app_mention`, a DM, or a plain reply in a channel thread set to `all-messages`) → `bot` → `manager.Ensure` (find the container, or place and start it) → `manager.DialAgent` (direct on the master, relayed for slaves) → `acp` session → updates stream back through `bot`'s streamer, which edits the Slack message. While the reply runs, its newest message also has a status line (what the agent is doing, and when it last sent an update) and a **Cancel** button whose value is the placeholder's ts, the key in `Bot.running`. The final edit removes both.
|
||||
|
||||
**In-flight turns:** once the placeholder is posted, `bot.converse` records the turn (thread, message and placeholder ts, sender, text, attachments) in `state_dir/in-flight-<channel>.json`, one file per channel keyed by thread, and marks it `prompted` just before `session/prompt`. The record is removed when the turn ends, unless it ended because the bot is shutting down: then the reply ends with a restarting note, and `Bot.Run` waits up to 5s for those last edits. On start, `Bot.Run` resumes every recorded turn through the same path. If the agent already got the prompt, it gets a short "carry on" note in the same session instead of the message again, because goose keeps the partial turn and resending could repeat what it already did. If it didn't (or the session is gone), it gets the original message and attachments. Turns older than an hour, or already resumed 3 times, are dropped with a note asking the user to send them again. A broken file is skipped and left on disk. Super users can read the files with `in-flight` (Slack, answered ephemerally) or the `in_flight` tool.
|
||||
|
||||
**Lifecycle threads:** `manager.EnsureProgress` reports `StartEvent`s (placing, booting, registered) only when it really starts a container. The bot (`bot/lifecycle.go`) turns them into replies in a dedicated top-level message for that container, pins it, and unpins the previous run's message. The message's channel and timestamp are kept on the owner (`lifecycle_channel`, `lifecycle_ts`). The user's thread only gets a link. Stops (`stop`, the button, the idle reaper) post to the same thread, and `manager.WaitStopped` confirms when the machine is gone.
|
||||
|
||||
**Files:** attachments on a message (`file_share`, or `files` on an `app_mention`) are downloaded by the bot and written into the home with the node's `write_file` call, then their paths are added to the prompt. The other way, goose gets the `slackerbot` MCP server (`slackerbot tools`, run inside the container as `slackerbot-tools`) through `mcpServers` in `session/new` and `session/load`. Its `initialize` reply carries `instructions` (how to use Nix in the container), which goose adds to its system prompt. Its `send_file` tool only checks the path and size and answers "queued". The bot watches the ACP `tool_call` / `tool_call_update` messages, and when a `send_file` call completes it reads the file with `read_file` and uploads it to the thread with `files.uploadV2`. Bytes go node → master → Slack, so slaves never need a Slack token. Both calls work in 4 MB chunks, open files with `os.Root` so symlinks and `..` can't leave the home, and are capped by `max_send_file_mb`. An older slave answers "unknown call", which surfaces as `node.ErrTooOld` and a polite reply.
|
||||
@@ -41,7 +43,7 @@ flowchart LR
|
||||
|
||||
**System updates:** each node reports running containers whose `slackerbot-<machine>.service` command ends with a different system's `init` than the node's current `system` (`outdated` in `RunningMachine`, read over D-Bus; unknown counts as current). After a reply, the bot asks once per container (`bot/update.go`, remembered in memory) whether to restart. **Restart now** calls `manager.Restart` (power off, wait, then the normal start), unless a reply is in progress.
|
||||
|
||||
**State** lives only on the master, in `state_dir/state.json`: owners (container, size, node, drive, storage limits, idle limit, lifecycle message), sessions (Slack thread → ACP session id, plus the thread's listen mode), each channel's default listen mode, and permissions. Nodes report what's running and how much disk each home uses. Nothing else is stored.
|
||||
**State** lives only on the master, in `state_dir/state.json`: owners (container, size, node, drive, storage limits, idle limit, lifecycle message), sessions (Slack thread → ACP session id, plus the thread's listen mode), each channel's default listen mode, and permissions. Turns still in progress go in `state_dir/in-flight-<channel>.json` (see **In-flight turns**). Nodes report what's running and how much disk each home uses. Nothing else is stored.
|
||||
|
||||
## Placement and capacity
|
||||
|
||||
@@ -98,7 +100,7 @@ flowchart TD
|
||||
| `commands` | Parses Slack command text: `scale`, `keep`, `storage`, `grant`, and which permission a command needs |
|
||||
| `config` | Loads and validates `config.toml` |
|
||||
| `storage` | Drive math (`DriveReport`, headroom, overbooking) and btrfs subvolume/quota commands |
|
||||
| `state` | `state.json` persistence |
|
||||
| `state` | `state.json` and `in-flight-<channel>.json` persistence |
|
||||
| `node` | `Node` interface, wire types, offline/full errors, the WebSocket connection used to reach goose |
|
||||
| `local` | The `Node` on this machine: nspawn via `systemd-run`, machined and systemd over D-Bus, capacity detection, btrfs send/receive |
|
||||
| `balance` | Builds a view of each node and picks where a container goes |
|
||||
@@ -107,7 +109,7 @@ flowchart TD
|
||||
| `acp` | JSON-RPC client for goose's ACP endpoint. Tool permission requests are auto-approved |
|
||||
| `bot` | Slack events and buttons, replies, permission checks, `grant`/`revoke`, move announcements, idle reaper, streaming replies, thread listen modes, stop reactions, the assistant pane, commands the agent calls as tools |
|
||||
| `util` | `KeyedMutex`, `RandomHex` |
|
||||
| `testutil`, `testutil/clustertest` | Fake nodes, and a real master + slave harness for tests |
|
||||
| `testutil`, `testutil/clustertest` | Fake nodes, a fake goose, and a real master + slave harness for tests |
|
||||
| `nix/container.nix` | NixOS system for each container |
|
||||
| `nix/host.nix` | NixOS module: the service, the `ve-*` network, firewall |
|
||||
|
||||
@@ -119,6 +121,6 @@ There are two builds. Nix (`flake.nix`) builds the deployable package, the conta
|
||||
- Anyone in a channel controls that channel's container and everything the agent can reach from it.
|
||||
- The agent runs commands with the sender's permissions, so whatever steers it (a file, a web page) can scale, move or re-grant as far as that person may. The thread shows every change it makes.
|
||||
- Containers run as root without user namespaces, and container root can get around btrfs quotas. That's fine for fair sharing among trusted users, but not safe for hostile ones.
|
||||
- Restarting the master makes slaves look offline for a few seconds. Restarting the bot doesn't stop containers.
|
||||
- Restarting the master makes slaves look offline for a few seconds. Restarting the bot doesn't stop containers, and replies in progress are picked back up, but Slack doesn't replay messages sent while the bot was down.
|
||||
- `state.json` is fine for hundreds of users. Swap the `state` package for SQLite if you need more.
|
||||
- Home folders are never deleted automatically (see INSTALLATION.md).
|
||||
+2
-1
@@ -36,7 +36,7 @@ Copy `config.example.toml` and edit it. The important parts:
|
||||
| Setting | What |
|
||||
| ------------------------------------------------------ | -------------------------------------------------------------------------------------------------- |
|
||||
| `system` | The build from step 2, e.g. `/var/lib/slackerbot/agent-system` |
|
||||
| `state_dir` | Where `state.json`, env files and cluster certificates live |
|
||||
| `state_dir` | Where `state.json`, in-flight replies, env files and cluster certificates live |
|
||||
| `[agent_env]` | Environment for goose (provider, model, API key). `$VARS` are expanded from the bot's environment |
|
||||
| `[container]`, `[max_container]` | Default and largest container size |
|
||||
| `[host_reserve]` | RAM/CPU kept free for the host |
|
||||
@@ -151,5 +151,6 @@ Check the log for the node's capacity line and "connected to Slack", then DM the
|
||||
|
||||
- **Permissions** live in the master's `state.json` after the first start, and editing the config doesn't change them. To start over from the config, stop the master and delete the `permissions` entry from `state.json`.
|
||||
- **Deleting a home folder for good:** stop its container, run `btrfs subvolume delete <drive path>/<u-… or c-…>`, then, with the bot stopped, remove its owner from `state.json`. Nothing deletes homes automatically, so their guaranteed space stays taken. This includes homes left on a dead slave after **Create new container**.
|
||||
- **In-flight replies** are in `state_dir/in-flight-<channel>.json`, one file per channel while it has a reply in progress. The bot picks them back up when it starts. To stop a reply from being resumed, stop the bot and delete its entry (or the file).
|
||||
- **Upgrading** the bot doesn't stop running containers, because each runs in its own `slackerbot-<machine>.service` unit.
|
||||
- Find user and channel IDs in Slack with **Copy member ID** on a profile, or at the bottom of a channel's **About** tab.
|
||||
+24
-21
@@ -27,7 +27,8 @@ In DMs the bot reads every message. You can also chat with it in Slack's assista
|
||||
9. **Attach files** to your message (images, logs, zips) and they're copied into the container at `/root/slack-uploads/<message time>/<name>`, and the agent is told where. A message with files always goes to the agent, never to a command. Files over the server's size limit (default 100 MB) are left out, and the home folder's max size still applies.
|
||||
10. **The agent can send you files.** It has a `send_file` tool, and the files land in the thread when its reply ends, where Slack previews images, video and PDFs. At most 5 files per reply, each under the server's size limit.
|
||||
11. **The agent can run the commands below for you**, so you can ask it things like "give yourself 8G" or "keep running over the weekend". It runs them as you, the sender of the message it's answering, with your permissions. Anything that changes the container is posted in the thread as ":robot_face: The agent ran `scale 8G` for Alice", with the bot's answer. If the agent stops the container, it waits until the reply ends. Its moves get their own thread like any other move. It can do everything a command can except `help`, and it can't press buttons.
|
||||
12. **System updates ask first.** When the server gets a new container system (new tools, for example) while yours is running, the bot asks once, under the agent's next reply, whether to restart onto it. **Restart now** restarts the container right away (it waits if the agent is still replying), and **Later** leaves it alone. Either way, `/root` and your sessions are kept, and the update also applies the next time the container starts for any reason.
|
||||
12. **Restarts don't lose your reply.** If slackerbot restarts while the agent is working on your message, the reply ends with a note that it's restarting. When it's back, it posts ":arrows_counterclockwise: slackerbot restarted part-way through this reply, so I'm picking it back up" in the thread, and the agent carries on where it stopped (or starts, if it hadn't got your message yet). After an hour, or after 3 restarts in a row, it asks you to send the message again instead. Messages sent while slackerbot is down aren't seen, so send those again.
|
||||
13. **System updates ask first.** When the server gets a new container system (new tools, for example) while yours is running, the bot asks once, under the agent's next reply, whether to restart onto it. **Restart now** restarts the container right away (it waits if the agent is still replying), and **Later** leaves it alone. Either way, `/root` and your sessions are kept, and the update also applies the next time the container starts for any reason.
|
||||
|
||||
Other replies you may get:
|
||||
|
||||
@@ -53,26 +54,27 @@ Anyone in the channel can change these. In an `all-messages` thread, `move` stil
|
||||
|
||||
Send these as a **new message**, not in a thread. `move` and `listen` are the exceptions, and also work in a thread. In the assistant pane, the first message of a chat counts as new.
|
||||
|
||||
| Command | What it does |
|
||||
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `help` | Shows the command list |
|
||||
| `status` | State, size, time unused and idle limit, with **Stop** and **Scale up** buttons |
|
||||
| `sessions` | Past sessions with links. Open one and reply in its thread to continue |
|
||||
| `stop` | Shuts the container down. Files are kept |
|
||||
| `scale up` / `scale down` | Doubles or halves RAM and CPU |
|
||||
| `scale 8G 4cpu` | Sets an exact size |
|
||||
| `scale +2G` / `scale -1cpu` / `scale 512M +0.5cpu` | `+` or `-` changes by that much. Memory and CPU can be mixed or left out |
|
||||
| `keep 24h` (or `3d`, `1d12h`, `2w`, `5 hours`) | Stops the container after that long unused |
|
||||
| `keep indefinite` (or `forever`) | Never stops it for being unused. Use `stop` when done |
|
||||
| `keep default` | Back to the server's default |
|
||||
| `keep` | Shows the current idle limit |
|
||||
| `storage` | Shows home folder usage, guaranteed space and max |
|
||||
| `storage 20G` / `storage +5G` / `storage -2G` | Sets or changes the max (the cap) |
|
||||
| `storage min 8G` / `storage min 8G max 20G` | Sets the guaranteed space, or both |
|
||||
| `nodes` | Every node with what's free on it, and where this container runs. Flags nodes running a different version than the master |
|
||||
| `move <node>` / `move to <node>` / `migrate <node>` | Moves this container to that node |
|
||||
| `move` / `move auto` | Moves it to whichever other node keeps resources most even |
|
||||
| `permissions` | Who may change what here |
|
||||
| Command | What it does |
|
||||
| --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `help` | Shows the command list |
|
||||
| `status` | State, size, time unused and idle limit, with **Stop** and **Scale up** buttons |
|
||||
| `sessions` | Past sessions with links. Open one and reply in its thread to continue |
|
||||
| `stop` | Shuts the container down. Files are kept |
|
||||
| `scale up` / `scale down` | Doubles or halves RAM and CPU |
|
||||
| `scale 8G 4cpu` | Sets an exact size |
|
||||
| `scale +2G` / `scale -1cpu` / `scale 512M +0.5cpu` | `+` or `-` changes by that much. Memory and CPU can be mixed or left out |
|
||||
| `keep 24h` (or `3d`, `1d12h`, `2w`, `5 hours`) | Stops the container after that long unused |
|
||||
| `keep indefinite` (or `forever`) | Never stops it for being unused. Use `stop` when done |
|
||||
| `keep default` | Back to the server's default |
|
||||
| `keep` | Shows the current idle limit |
|
||||
| `storage` | Shows home folder usage, guaranteed space and max |
|
||||
| `storage 20G` / `storage +5G` / `storage -2G` | Sets or changes the max (the cap) |
|
||||
| `storage min 8G` / `storage min 8G max 20G` | Sets the guaranteed space, or both |
|
||||
| `nodes` | Every node with what's free on it, and where this container runs. Flags nodes running a different version than the master |
|
||||
| `move <node>` / `move to <node>` / `migrate <node>` | Moves this container to that node |
|
||||
| `move` / `move auto` | Moves it to whichever other node keeps resources most even |
|
||||
| `permissions` | Who may change what here |
|
||||
| `in-flight` / `in-flight all` | Super users only. For debugging: shows the replies slackerbot would pick back up after a restart, here or in every channel. Only you see the answer |
|
||||
|
||||
If what follows `scale`, `keep`, `storage` or `move` doesn't look like a size, a time or a node (e.g. "keep the tests passing"), the message goes to the agent instead.
|
||||
|
||||
@@ -106,6 +108,7 @@ Roughly how it fits together:
|
||||
- **User managers** can change everything: any channel's list, the default list, DM switches and the manager list. **Channel managers** (`manage`) can only edit their own channel's list. There's always at least one user manager.
|
||||
- If you lack a permission, the bot answers with :lock: and names who has it, without tagging them.
|
||||
- Automatic moves don't need a permission.
|
||||
- `in-flight` shows other people's messages, so it needs you to be a **super user** (anyone on the list that applies here, with any permission) or a user manager. `in-flight all` needs you on the default list, or a user manager. DMs count the default list.
|
||||
|
||||
| Command | Who | What it does |
|
||||
| ------------------------------------------------------------- | ---------------------- | ---------------------------------------------------- |
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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"],
|
||||
)
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user