bazel start + mcp start

This commit is contained in:
ari committed 2026-11-01 14:55:33 -05:00
1 parent a90653260a
commit 4c082aca7c
33 files changed
+1728 -79

No files matched your search

+36 -7
View File
@@ -12,10 +12,21 @@ import (
)
type Update struct {
Kind string
Text string
Title string
Status string
Kind string
Text string
Title string
Status string
ToolID string
ToolName string
RawInput json.RawMessage
}
// MCP servers started inside the container for every session.
type MCPServer struct {
Name string `json:"name"`
Command string `json:"command"`
Args []string `json:"args"`
Env []any `json:"env"`
}
type rpcError struct {
@@ -46,6 +57,7 @@ type ACP struct {
loaded map[string]bool
CanLoad bool
Servers []MCPServer
done chan struct{}
err error
@@ -96,7 +108,7 @@ func (a *ACP) NewSession(ctx context.Context, cwd string) (string, error) {
var res struct {
SessionID string `json:"sessionId"`
}
if err := a.call(ctx, "session/new", map[string]any{"cwd": cwd, "mcpServers": []any{}}, &res); err != nil {
if err := a.call(ctx, "session/new", map[string]any{"cwd": cwd, "mcpServers": a.servers()}, &res); err != nil {
return "", err
}
a.mu.Lock()
@@ -116,7 +128,7 @@ func (a *ACP) LoadSession(ctx context.Context, id, cwd string) error {
if !a.CanLoad {
return errors.New("this agent can't reopen old sessions")
}
if err := a.call(ctx, "session/load", map[string]any{"sessionId": id, "cwd": cwd, "mcpServers": []any{}}, nil); err != nil {
if err := a.call(ctx, "session/load", map[string]any{"sessionId": id, "cwd": cwd, "mcpServers": a.servers()}, nil); err != nil {
return err
}
a.mu.Lock()
@@ -125,6 +137,13 @@ func (a *ACP) LoadSession(ctx context.Context, id, cwd string) error {
return nil
}
func (a *ACP) servers() []MCPServer {
if a.Servers == nil {
return []MCPServer{}
}
return a.Servers
}
func (a *ACP) Prompt(ctx context.Context, id, text string, onUpdate func(Update)) (stopReason string, err error) {
a.mu.Lock()
a.handlers[id] = onUpdate
@@ -236,12 +255,22 @@ func (a *ACP) handleUpdate(params json.RawMessage) {
Content json.RawMessage `json:"content"`
Title string `json:"title"`
Status string `json:"status"`
ToolID string `json:"toolCallId"`
Input json.RawMessage `json:"rawInput"`
Meta struct {
Goose struct {
ToolCall struct {
ToolName string `json:"toolName"`
} `json:"toolCall"`
} `json:"goose"`
} `json:"_meta"`
} `json:"update"`
}
if json.Unmarshal(params, &p) != nil {
return
}
u := Update{Kind: p.Update.Kind, Title: p.Update.Title, Status: p.Update.Status}
u := Update{Kind: p.Update.Kind, Title: p.Update.Title, Status: p.Update.Status,
ToolID: p.Update.ToolID, ToolName: p.Update.Meta.Goose.ToolCall.ToolName, RawInput: p.Update.Input}
// Message chunks carry one content block; tool updates carry a list.
var block struct {
Type string `json:"type"`
+28 -8
View File
@@ -27,6 +27,7 @@ import (
"slackerbot/internal/node"
"slackerbot/internal/state"
"slackerbot/internal/storage"
"slackerbot/internal/tools"
"slackerbot/internal/units"
"slackerbot/internal/util"
)
@@ -84,6 +85,7 @@ type incoming struct {
user, channel, ts, threadTS, text string
dm bool
heard bool
files []slack.File
}
func (m incoming) owner() (string, model.OwnerKind) {
@@ -152,17 +154,17 @@ func (b *Bot) onEvent(ctx context.Context, ev slackevents.EventsAPIEvent) {
if e.BotID != "" {
return
}
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, b.stripMention(e.Text), false, false})
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, b.stripMention(e.Text), false, false, eventFiles(ev)})
case *slackevents.MessageEvent:
if e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
if e.BotID != "" || (e.SubType != "" && e.SubType != "file_share") || e.User == "" || e.User == b.botID {
return
}
text := b.stripMention(e.Text)
switch {
case e.ChannelType == "im":
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false})
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false, eventFiles(ev)})
case b.listening(e.Channel, e.ThreadTimeStamp, e.Text):
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, true})
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, true, eventFiles(ev)})
}
case *slackevents.ReactionAddedEvent:
b.onReaction(e)
@@ -178,15 +180,17 @@ func (b *Bot) stripMention(text string) string {
}
func (b *Bot) onMessage(ctx context.Context, m incoming) {
if b.listenCommand(ctx, m) {
// A message with files always goes to the agent, so a file with no text isn't read as `help`.
commandable := len(m.files) == 0
if commandable && b.listenCommand(ctx, m) {
return
}
// Every assistant-pane chat is a DM thread, so its first message counts as new.
fresh := m.threadTS == "" || m.dm && b.store.Session(m.channel, m.threadTS) == nil
if fresh && b.command(ctx, m) {
if commandable && fresh && b.command(ctx, m) {
return
}
if m.threadTS != "" && !m.heard && b.moveCommand(ctx, m, m.threadTS) {
if commandable && m.threadTS != "" && !m.heard && b.moveCommand(ctx, m, m.threadTS) {
return
}
root := m.threadTS
@@ -297,6 +301,16 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
if m.dm {
text = b.viewingNote(ctx, m.channel, root) + text
}
if len(m.files) > 0 {
edit(":paperclip: Copying the attachments into the container…")
note, problems := b.attach(ctx, u, m)
if note != "" {
text += "\n" + note
}
if len(problems) > 0 {
b.say(ctx, m.channel, root, ":warning: "+strings.Join(problems, "\n"))
}
}
edit("_working…_")
pctx, cancel := context.WithCancel(ctx)
@@ -311,11 +325,16 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
defer b.setAssistantStatus(ctx, m.channel, root, "")
}
st := NewStreamer(b.api, m.channel, root, ph)
stop, err := acp.Prompt(pctx, id, text, st.Update)
var sends tools.Calls
stop, err := acp.Prompt(pctx, id, text, func(up acpUpdate) {
sends.Update(up)
st.Update(up)
})
if pctx.Err() != nil && ctx.Err() == nil {
stop, err = "cancelled", nil
}
st.Finish(stop, err)
b.upload(ctx, u, m.channel, root, sends.Files())
_ = b.store.Touch(u.ID)
switch {
case stop == "cancelled":
@@ -1043,6 +1062,7 @@ func (b *Bot) client(ctx context.Context, u *model.Owner) (*acp.ACP, error) {
if c, err = acp.New(initCtx, conn); err != nil {
return nil, err
}
c.Servers = b.mcpServers()
b.acpMu.Lock()
b.acps[key] = c
b.acpMu.Unlock()
+145
View File
@@ -0,0 +1,145 @@
package bot
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"path"
"strconv"
"strings"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"slackerbot/internal/acp"
"slackerbot/internal/manager"
"slackerbot/internal/model"
"slackerbot/internal/node"
"slackerbot/internal/tools"
)
const (
maxFilesPerTurn = 5
uploadsDir = "slack-uploads"
)
// onMessage's local `acp` client shadows the package name.
type acpUpdate = acp.Update
func (b *Bot) mcpServers() []acp.MCPServer {
return []acp.MCPServer{tools.Spec(b.cfg.AgentCwd, b.cfg.MaxSendFileMB)}
}
// slack-go's app_mention event has no files field, so they're read from the raw event.
func eventFiles(ev slackevents.EventsAPIEvent) []slack.File {
cb, ok := ev.Data.(*slackevents.EventsAPICallbackEvent)
if !ok || cb.InnerEvent == nil {
return nil
}
var inner struct {
Files []slack.File `json:"files"`
}
_ = json.Unmarshal(*cb.InnerEvent, &inner)
return inner.Files
}
func uploadName(f slack.File, i int, seen map[string]bool) string {
name := strings.NewReplacer("/", "_", "\x00", "").Replace(path.Base(f.Name))
if name == "" || name == "." || name == ".." {
name = "file-" + strconv.Itoa(i+1)
}
if seen[name] {
name = strconv.Itoa(i+1) + "-" + name
}
seen[name] = true
return name
}
// attach copies the message's files into the container and says where they went.
func (b *Bot) attach(ctx context.Context, u *model.Owner, m incoming) (note string, problems []string) {
maxBytes := b.cfg.MaxSendFileMB << 20
dir := path.Join(b.cfg.AgentCwd, uploadsDir, m.ts)
seen := map[string]bool{}
var paths []string
for i, f := range m.files {
name := uploadName(f, i, seen)
if f.Mode == "external" || f.Mode == "tombstone" || f.URLPrivateDownload == "" {
problems = append(problems, fmt.Sprintf("%s isn't a file I can download.", name))
continue
}
if int64(f.Size) > maxBytes {
problems = append(problems, fmt.Sprintf("%s is bigger than the %d MB limit, so I left it out.", name, b.cfg.MaxSendFileMB))
continue
}
dst := path.Join(dir, name)
if err := b.copyIn(ctx, u, f, dst, maxBytes); err != nil {
if errors.Is(err, node.ErrTooOld) {
return "", []string{"This container's node can't take attachments yet, so I left them out. It needs a newer slackerbot."}
}
problems = append(problems, fmt.Sprintf("Couldn't copy %s into the container: %v.", name, err))
continue
}
paths = append(paths, dst)
}
if len(paths) > 0 {
note = "(Attached: " + strings.Join(paths, ", ") + ")"
}
return note, problems
}
func (b *Bot) copyIn(ctx context.Context, u *model.Owner, f slack.File, dst string, maxBytes int64) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
pr, pw := io.Pipe()
go func() { pw.CloseWithError(b.api.GetFileContext(ctx, f.URLPrivateDownload, pw)) }()
err := b.mgr.WriteFile(ctx, u, dst, maxBytes, pr)
pr.CloseWithError(err)
if errors.Is(err, manager.ErrFileTooBig) {
return fmt.Errorf("it's bigger than the %d MB limit", b.cfg.MaxSendFileMB)
}
return err
}
func (b *Bot) upload(ctx context.Context, u *model.Owner, channel, thread string, reqs []tools.FileRequest) {
if len(reqs) > maxFilesPerTurn {
b.say(ctx, channel, thread, fmt.Sprintf(":warning: The agent tried to send %d files. I only send %d per reply, so the rest were skipped.", len(reqs), maxFilesPerTurn))
reqs = reqs[:maxFilesPerTurn]
}
for _, r := range reqs {
if err := b.uploadOne(ctx, u, channel, thread, r); err != nil {
name := path.Base(r.Path)
switch {
case errors.Is(err, node.ErrTooOld):
b.say(ctx, channel, thread, "This container's node is too old to send files. It needs a newer slackerbot.")
return
case errors.Is(err, manager.ErrFileTooBig):
b.say(ctx, channel, thread, fmt.Sprintf(":warning: Couldn't send %s: it's bigger than the %d MB limit.", name, b.cfg.MaxSendFileMB))
default:
log.Printf("send file %s from %s: %v", r.Path, u.Machine, err)
b.say(ctx, channel, thread, fmt.Sprintf(":warning: Couldn't send %s: %v", name, err))
}
}
}
}
// The file is buffered on the master, because files.uploadV2 needs the size up front.
func (b *Bot) uploadOne(ctx context.Context, u *model.Owner, channel, thread string, r tools.FileRequest) error {
var buf bytes.Buffer
n, err := b.mgr.ReadFile(ctx, u, r.Path, b.cfg.MaxSendFileMB<<20, &buf)
if err != nil {
return err
}
if n == 0 {
return errors.New("the file is empty")
}
name := path.Base(r.Path)
_, err = b.api.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{
Reader: &buf, FileSize: int(n), Filename: name, Title: name,
Channel: channel, ThreadTimestamp: thread, InitialComment: r.Comment,
})
return err
}
+5
View File
@@ -49,6 +49,8 @@ type Config struct {
AgentCwd string `toml:"agent_cwd"`
AgentEnv map[string]string `toml:"agent_env"`
MaxSendFileMB int64 `toml:"max_send_file_mb"`
ROBinds []string `toml:"ro_binds"`
RWBinds []string `toml:"rw_binds"`
@@ -85,6 +87,7 @@ func LoadConfig(path, mode string) (*Config, error) {
AllowIndefinite: true,
BootTimeout: 2 * time.Minute,
AgentCwd: "/root",
MaxSendFileMB: 100,
Container: model.Size{MemoryMB: 2048, CPUs: 1},
MaxContainer: model.Size{MemoryMB: 8192, CPUs: 4},
HostReserve: model.Size{MemoryMB: 2048, CPUs: 1},
@@ -133,6 +136,8 @@ func LoadConfig(path, mode string) (*Config, error) {
return nil, fmt.Errorf("system (the NixOS build) is required")
case c.Container.MemoryMB <= 0 || c.Container.CPUs <= 0:
return nil, fmt.Errorf("container memory_mb and cpus must be above zero")
case c.MaxSendFileMB <= 0 || c.MaxSendFileMB > 1024:
return nil, fmt.Errorf("max_send_file_mb must be between 1 and 1024 (Slack's limit)")
case c.IdleTimeout <= 0:
return nil, fmt.Errorf("idle_timeout must be above zero")
case !c.Container.Fits(c.MaxContainer):
+98
View File
@@ -0,0 +1,98 @@
package local
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"syscall"
)
// os.Root keeps symlinks and ".." from escaping the home folder.
func (l *Local) ReadFile(ctx context.Context, drive, machine, path string, offset int64, limit int) ([]byte, int64, error) {
home, err := l.home(drive, machine)
if err != nil {
return nil, 0, err
}
f, err := os.OpenInRoot(home, path)
if err != nil {
return nil, 0, fileErr(path, err)
}
defer f.Close()
st, err := f.Stat()
if err != nil {
return nil, 0, err
}
if !st.Mode().IsRegular() {
return nil, 0, fmt.Errorf("%s isn't a regular file", path)
}
buf := make([]byte, min(int64(limit), max(0, st.Size()-offset)))
n, err := f.ReadAt(buf, offset)
if errors.Is(err, io.EOF) {
err = nil
}
return buf[:n], st.Size(), err
}
// Writing at offset 0 creates or truncates the file; later chunks follow on.
func (l *Local) WriteFile(ctx context.Context, drive, machine, path string, offset int64, data []byte) error {
home, err := l.home(drive, machine)
if err != nil {
return err
}
root, err := os.OpenRoot(home)
if err != nil {
return err
}
defer root.Close()
flags := os.O_WRONLY | os.O_CREATE
if offset == 0 {
flags |= os.O_TRUNC
if err := mkdirAll(root, filepath.Dir(path)); err != nil {
return fileErr(path, err)
}
}
f, err := root.OpenFile(path, flags, 0o644)
if err != nil {
return fileErr(path, err)
}
_, err = f.WriteAt(data, offset)
if cerr := f.Close(); err == nil {
err = cerr
}
return fileErr(path, err)
}
func mkdirAll(root *os.Root, dir string) error {
cur := ""
for _, part := range strings.Split(filepath.Clean(dir), "/") {
if part == "" || part == "." {
continue
}
cur = filepath.Join(cur, part)
if err := root.Mkdir(cur, 0o755); err != nil && !errors.Is(err, os.ErrExist) {
return err
}
}
return nil
}
// Errors go back to Slack, so they shouldn't name host paths.
func fileErr(path string, err error) error {
switch {
case err == nil:
return nil
case errors.Is(err, syscall.EDQUOT), errors.Is(err, syscall.ENOSPC):
return errors.New("the home folder is full")
case errors.Is(err, os.ErrNotExist):
return fmt.Errorf("there's no file %s", path)
}
var pe *os.PathError
if errors.As(err, &pe) {
return fmt.Errorf("%s: %s %v", path, pe.Op, pe.Err)
}
return err
}
+26
View File
@@ -468,6 +468,23 @@ func (r *Remote) DeleteHome(ctx context.Context, drive, machine string) error {
return r.callNode(ctx, "delete_home", callParams{Drive: drive, Machine: machine}, nil)
}
func (r *Remote) ReadFile(ctx context.Context, drive, machine, path string, offset int64, limit int) ([]byte, int64, error) {
var res callParams
err := r.callNode(ctx, "read_file", callParams{Drive: drive, Machine: machine, Path: path, Offset: offset, Max: limit}, &res)
return res.Data, res.FileSize, tooOld(err)
}
func (r *Remote) WriteFile(ctx context.Context, drive, machine, path string, offset int64, data []byte) error {
return tooOld(r.callNode(ctx, "write_file", callParams{Drive: drive, Machine: machine, Path: path, Offset: offset, Data: data}, nil))
}
func tooOld(err error) error {
if err != nil && strings.HasPrefix(err.Error(), "unknown call ") {
return node.ErrTooOld
}
return err
}
func (r *Remote) DialAgent(ctx context.Context, machine, secret string) (node.MsgConn, error) {
l, err := r.currentLink()
if err != nil {
@@ -708,6 +725,10 @@ type callParams struct {
Max int `json:"max,omitempty"`
Data []byte `json:"data,omitempty"`
EOF bool `json:"eof,omitempty"`
Path string `json:"path,omitempty"`
Offset int64 `json:"offset,omitempty"`
FileSize int64 `json:"file_size,omitempty"`
}
func serveCall(ctx context.Context, l *link, local node.Node, f frame) {
@@ -751,6 +772,11 @@ func serveCall(ctx context.Context, l *link, local node.Node, f frame) {
l.reply(f.ID, nil, local.AbortImport(ctx, p.ID))
case "delete_home":
l.reply(f.ID, nil, local.DeleteHome(ctx, p.Drive, p.Machine))
case "read_file":
data, size, err := local.ReadFile(ctx, p.Drive, p.Machine, p.Path, p.Offset, min(p.Max, transferChunk))
l.reply(f.ID, callParams{Data: data, FileSize: size}, err)
case "write_file":
l.reply(f.ID, nil, local.WriteFile(ctx, p.Drive, p.Machine, p.Path, p.Offset, p.Data))
case "open_agent":
agent, err := local.DialAgent(ctx, p.Machine, p.Secret)
if err != nil {
+89
View File
@@ -0,0 +1,89 @@
package manager
import (
"context"
"errors"
"fmt"
"io"
"path"
"strings"
"slackerbot/internal/model"
)
var ErrFileTooBig = errors.New("the file is bigger than allowed")
// homeRel turns a path inside the container (/root/…) into one relative to the home folder.
func (m *Manager) homeRel(p string) (string, error) {
home := path.Clean(m.cfg.AgentCwd)
if !path.IsAbs(p) {
p = path.Join(home, p)
}
p = path.Clean(p)
rel := strings.TrimPrefix(p, home+"/")
if rel == p || rel == "" {
return "", fmt.Errorf("%s isn't inside %s", p, home)
}
return rel, nil
}
// ReadFile streams a file from the owner's home into w, at most maxBytes.
func (m *Manager) ReadFile(ctx context.Context, u *model.Owner, p string, maxBytes int64, w io.Writer) (int64, error) {
rel, err := m.homeRel(p)
if err != nil {
return 0, err
}
n, err := m.nodeByName(u.Node)
if err != nil {
return 0, err
}
var off int64
for {
data, size, err := n.ReadFile(ctx, u.Drive, u.Machine, rel, off, transferChunk)
if err != nil {
return off, err
}
if size > maxBytes {
return off, ErrFileTooBig
}
if _, err := w.Write(data); err != nil {
return off, err
}
off += int64(len(data))
if len(data) == 0 || off >= size {
return off, nil
}
}
}
// WriteFile streams r into a file in the owner's home, giving up past maxBytes.
func (m *Manager) WriteFile(ctx context.Context, u *model.Owner, p string, maxBytes int64, r io.Reader) error {
rel, err := m.homeRel(p)
if err != nil {
return err
}
n, err := m.nodeByName(u.Node)
if err != nil {
return err
}
buf := make([]byte, transferChunk)
var off int64
for {
k, rerr := io.ReadFull(r, buf)
if k > 0 || off == 0 {
if off+int64(k) > maxBytes {
return ErrFileTooBig
}
if err := n.WriteFile(ctx, u.Drive, u.Machine, rel, off, buf[:k]); err != nil {
return err
}
off += int64(k)
}
if errors.Is(rerr, io.EOF) || errors.Is(rerr, io.ErrUnexpectedEOF) {
return nil
}
if rerr != nil {
return rerr
}
}
}
+37
View File
@@ -0,0 +1,37 @@
package manager_test
import (
"bytes"
"errors"
"testing"
"slackerbot/internal/manager"
"slackerbot/internal/model"
"slackerbot/internal/testutil"
"slackerbot/internal/testutil/clustertest"
)
func TestFilesThroughSlave(t *testing.T) {
c := clustertest.New(t)
c.Cfg.AgentCwd = "/root"
u := &model.Owner{ID: "C1", Machine: "c-c1", Node: "slave-1", Drive: "/d"}
data := testutil.HomeData()
if err := c.Mgr.WriteFile(c.Ctx, u, "/root/slack-uploads/1.0/big.bin", 100<<20, bytes.NewReader(data)); err != nil {
t.Fatal(err)
}
if got := c.Slave.Files["/d/c-c1/slack-uploads/1.0/big.bin"]; !bytes.Equal(got, data) {
t.Fatalf("slave got %d bytes, want %d", len(got), len(data))
}
var out bytes.Buffer
if n, err := c.Mgr.ReadFile(c.Ctx, u, "/root/slack-uploads/1.0/big.bin", 100<<20, &out); err != nil || n != int64(len(data)) || !bytes.Equal(out.Bytes(), data) {
t.Fatalf("read back %d bytes: %v", n, err)
}
if _, err := c.Mgr.ReadFile(c.Ctx, u, "/root/slack-uploads/1.0/big.bin", 1<<20, &out); !errors.Is(err, manager.ErrFileTooBig) {
t.Fatalf("over the cap: %v", err)
}
if _, err := c.Mgr.ReadFile(c.Ctx, u, "/etc/passwd", 100<<20, &out); err == nil {
t.Fatal("paths outside the home must be refused")
}
}
+8
View File
@@ -2,6 +2,7 @@ package node
import (
"context"
"errors"
"fmt"
"net/http"
"time"
@@ -32,8 +33,15 @@ type Node interface {
FinishImport(ctx context.Context, id string, quotaMB int64) error
AbortImport(ctx context.Context, id string) error
DeleteHome(ctx context.Context, drive, machine string) error
// Paths are relative to the home folder.
ReadFile(ctx context.Context, drive, machine, path string, offset int64, limit int) (data []byte, size int64, err error)
WriteFile(ctx context.Context, drive, machine, path string, offset int64, data []byte) error
}
// Returned when a node runs an older version that doesn't know a call.
var ErrTooOld = errors.New("this node runs an older version")
type NodeInfo struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
+24 -1
View File
@@ -21,6 +21,7 @@ type FakeNode struct {
Runs map[string]bool
Last node.StartSpec
Homes map[string][]byte
Files map[string][]byte
Exports map[string]*fakeExport
Imports map[string]*fakeImport
Passes int
@@ -30,7 +31,7 @@ type FakeNode struct {
}
func NewFakeNode(name string) *FakeNode {
return &FakeNode{name: name, Runs: map[string]bool{}, Homes: map[string][]byte{},
return &FakeNode{name: name, Runs: map[string]bool{}, Homes: map[string][]byte{}, Files: map[string][]byte{},
Exports: map[string]*fakeExport{}, Imports: map[string]*fakeImport{}, Usable: model.Size{MemoryMB: 8192, CPUs: 4}}
}
@@ -188,6 +189,28 @@ func (f *FakeNode) DeleteHome(_ context.Context, drive, machine string) error {
return nil
}
func (f *FakeNode) ReadFile(_ context.Context, drive, machine, path string, offset int64, limit int) ([]byte, int64, error) {
f.Mu.Lock()
defer f.Mu.Unlock()
data, ok := f.Files[drive+"/"+machine+"/"+path]
if !ok {
return nil, 0, errors.New("there's no file " + path)
}
end := min(int64(len(data)), offset+int64(limit))
return data[offset:end], int64(len(data)), nil
}
func (f *FakeNode) WriteFile(_ context.Context, drive, machine, path string, offset int64, data []byte) error {
f.Mu.Lock()
defer f.Mu.Unlock()
key := drive + "/" + machine + "/" + path
if offset == 0 {
f.Files[key] = nil
}
f.Files[key] = append(f.Files[key], data...)
return nil
}
type fakeExport struct {
data []byte
pos int
+137
View File
@@ -0,0 +1,137 @@
// Package tools is the stdio MCP server that runs inside each container.
// Its tools only check their input; the bot sees the calls over ACP and does the work.
package tools
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
const (
ServerName = "slackerbot"
SendFile = "send_file"
)
const sendFileHelp = "Send a file to the user in Slack: images, videos, PDFs, archives, reports or anything else " +
"they should see or download. Prefer it to pasting long output. The file must be under %s and at most %d MB. " +
"It's uploaded into the conversation when your turn ends, so mention it in your reply. " +
"At most a few files per turn."
type Server struct {
Home string
MaxMB int64
}
type msg struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result any `json:"result,omitempty"`
Error *rpcError `json:"error,omitempty"`
}
type rpcError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (s *Server) Serve(in io.Reader, out io.Writer) error {
sc := bufio.NewScanner(in)
sc.Buffer(make([]byte, 1<<20), 16<<20)
enc := json.NewEncoder(out)
for sc.Scan() {
var m msg
if json.Unmarshal(sc.Bytes(), &m) != nil || len(m.ID) == 0 {
continue
}
reply := msg{JSONRPC: "2.0", ID: m.ID}
reply.Result, reply.Error = s.handle(m)
if err := enc.Encode(reply); err != nil {
return err
}
}
return sc.Err()
}
func (s *Server) handle(m msg) (any, *rpcError) {
switch m.Method {
case "initialize":
var p struct {
ProtocolVersion string `json:"protocolVersion"`
}
_ = json.Unmarshal(m.Params, &p)
return map[string]any{
"protocolVersion": p.ProtocolVersion,
"capabilities": map[string]any{"tools": map[string]any{}},
"serverInfo": map[string]string{"name": ServerName, "version": "1"},
}, nil
case "ping":
return map[string]any{}, nil
case "tools/list":
return map[string]any{"tools": []any{map[string]any{
"name": SendFile,
"description": fmt.Sprintf(sendFileHelp, s.Home, s.MaxMB),
"inputSchema": map[string]any{
"type": "object",
"properties": map[string]any{
"path": map[string]string{"type": "string", "description": "Path of the file, e.g. " + s.Home + "/out/chart.png"},
"comment": map[string]string{"type": "string", "description": "Optional message posted with the file"},
},
"required": []string{"path"},
},
}}}, nil
case "tools/call":
var p struct {
Name string `json:"name"`
Arguments struct {
Path string `json:"path"`
} `json:"arguments"`
}
_ = json.Unmarshal(m.Params, &p)
if p.Name != SendFile {
return nil, &rpcError{-32602, "unknown tool " + p.Name}
}
text, isErr := "queued", false
if err := s.Check(p.Arguments.Path); err != nil {
text, isErr = err.Error(), true
}
return map[string]any{"content": []any{map[string]string{"type": "text", "text": text}}, "isError": isErr}, nil
}
return nil, &rpcError{-32601, "method not found: " + m.Method}
}
func (s *Server) Check(path string) error {
if path == "" {
return fmt.Errorf("path is required")
}
if !filepath.IsAbs(path) {
path = filepath.Join(s.Home, path)
}
real, err := filepath.EvalSymlinks(path)
if err != nil {
return fmt.Errorf("can't find %s", path)
}
if !strings.HasPrefix(real, filepath.Clean(s.Home)+"/") {
return fmt.Errorf("%s isn't under %s; copy it there first", path, s.Home)
}
st, err := os.Stat(real)
if err != nil {
return err
}
if !st.Mode().IsRegular() {
return fmt.Errorf("%s isn't a regular file (zip a folder first)", path)
}
if st.Size() == 0 {
return fmt.Errorf("%s is empty", path)
}
if st.Size() > s.MaxMB<<20 {
return fmt.Errorf("%s is %d MB, over the %d MB limit", path, st.Size()>>20, s.MaxMB)
}
return nil
}
+51
View File
@@ -0,0 +1,51 @@
package tools
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func TestSendFileTool(t *testing.T) {
home := t.TempDir()
os.WriteFile(filepath.Join(home, "chart.png"), []byte("png"), 0o644)
os.Mkdir(filepath.Join(home, "dir"), 0o755)
os.Symlink("/etc/passwd", filepath.Join(home, "escape"))
s := &Server{Home: home, MaxMB: 1}
call := func(path string) string {
return `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"send_file","arguments":{"path":"` + path + `"}}}`
}
in := strings.Join([]string{
`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`,
`{"jsonrpc":"2.0","method":"notifications/initialized"}`,
call("chart.png"), call(home + "/dir"), call(home + "/escape"), call("missing"),
}, "\n")
var out bytes.Buffer
if err := s.Serve(strings.NewReader(in), &out); err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(out.String()), "\n")
if len(lines) != 5 || !strings.Contains(lines[0], `"protocolVersion":"2025-06-18"`) {
t.Fatalf("replies: %v", lines)
}
want := []struct {
text string
isErr bool
}{{"queued", false}, {"regular file", true}, {"isn't under", true}, {"can't find", true}}
for i, w := range want {
var r struct {
Result struct {
Content []struct{ Text string }
IsError bool
}
}
_ = json.Unmarshal([]byte(lines[i+1]), &r)
if r.Result.IsError != w.isErr || !strings.Contains(r.Result.Content[0].Text, w.text) {
t.Fatalf("call %d: %s", i, lines[i+1])
}
}
}
+83
View File
@@ -0,0 +1,83 @@
package tools
import (
"encoding/json"
"flag"
"os"
"strconv"
"strings"
"sync"
"slackerbot/internal/acp"
)
// Installed by nix/container.nix.
const Command = "/run/current-system/sw/bin/slackerbot-tools"
// Main runs the server, for `slackerbot tools`.
func Main(args []string) error {
fs := flag.NewFlagSet("tools", flag.ExitOnError)
home := fs.String("home", "/root", "the folder files may be sent from")
maxMB := fs.Int64("max-file-mb", 100, "the largest file send_file accepts")
_ = fs.Parse(args)
s := &Server{Home: *home, MaxMB: *maxMB}
return s.Serve(os.Stdin, os.Stdout)
}
// Spec is how goose starts the server, passed in session/new and session/load.
func Spec(home string, maxMB int64) acp.MCPServer {
return acp.MCPServer{
Name: ServerName,
Command: Command,
Args: []string{"tools", "-home", home, "-max-file-mb", strconv.FormatInt(maxMB, 10)},
Env: []any{},
}
}
type FileRequest struct {
Path string `json:"path"`
Comment string `json:"comment"`
}
// Calls watches a turn's ACP updates and collects send_file calls that completed.
type Calls struct {
mu sync.Mutex
pending map[string]FileRequest
done []FileRequest
}
func isSendFile(u acp.Update) bool {
return u.ToolName == ServerName+"__"+SendFile ||
strings.HasPrefix(u.Title, ServerName+": "+strings.ReplaceAll(SendFile, "_", " "))
}
func (c *Calls) Update(u acp.Update) {
c.mu.Lock()
defer c.mu.Unlock()
switch u.Kind {
case "tool_call":
var req FileRequest
if u.ToolID == "" || !isSendFile(u) || json.Unmarshal(u.RawInput, &req) != nil || req.Path == "" {
return
}
if c.pending == nil {
c.pending = map[string]FileRequest{}
}
c.pending[u.ToolID] = req
case "tool_call_update":
req, ok := c.pending[u.ToolID]
if !ok || u.Status != "completed" && u.Status != "failed" {
return
}
delete(c.pending, u.ToolID)
if u.Status == "completed" {
c.done = append(c.done, req)
}
}
}
func (c *Calls) Files() []FileRequest {
c.mu.Lock()
defer c.mu.Unlock()
return c.done
}
+24
View File
@@ -0,0 +1,24 @@
package tools
import (
"encoding/json"
"testing"
"slackerbot/internal/acp"
)
func TestCallsCollectsCompletedSends(t *testing.T) {
var c Calls
in := json.RawMessage(`{"path":"/root/a.png","comment":"here"}`)
c.Update(acp.Update{Kind: "tool_call", ToolID: "1", ToolName: "slackerbot__send_file", RawInput: in})
c.Update(acp.Update{Kind: "tool_call", ToolID: "2", Title: "slackerbot: send file · /root/b.zip", RawInput: json.RawMessage(`{"path":"/root/b.zip"}`)})
c.Update(acp.Update{Kind: "tool_call", ToolID: "3", ToolName: "developer__shell", RawInput: json.RawMessage(`{"path":"/root/x"}`)})
c.Update(acp.Update{Kind: "tool_call_update", ToolID: "1", Status: "completed"})
c.Update(acp.Update{Kind: "tool_call_update", ToolID: "2", Status: "failed"})
c.Update(acp.Update{Kind: "tool_call_update", ToolID: "3", Status: "completed"})
got := c.Files()
if len(got) != 1 || got[0].Path != "/root/a.png" || got[0].Comment != "here" {
t.Fatalf("got %+v", got)
}
}