Phase 11, part 4: agents' limits, recent calls and a pause switch

Each agent has calls a minute and things it may ask to send out a day,
checked on every tool call and every request to send; every call is
recorded (tool, outcome, time) and the agent's page lists the last 50 and
what it's waiting on. Pause refuses all its calls at once. Also fixes each
agent's MCP server being shared with others that had the same scopes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 11:09:18 -07:00
co-authored by Claude Opus 5.5
parent 311d2e815b
commit 6bf0517163
16 changed files with 527 additions and 13 deletions
+93 -1
View File
@@ -1,12 +1,16 @@
package web
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
"supermarket/internal/approvals"
"supermarket/internal/audit"
"supermarket/internal/fileutil"
@@ -42,7 +46,64 @@ func (s *Server) AgentAuth(r *http.Request, key string) (*mcpserver.Caller, erro
if err != nil {
return nil, users.ErrBadKey
}
return &mcpserver.Caller{ID: u.ID, Name: u.Name, Can: m.Can}, nil
lim, err := users.LimitsOf(s.DB, u.ID)
if err != nil {
return nil, err
}
if !lim.Paused.IsZero() {
return nil, &mcpserver.Refusal{Status: http.StatusForbidden,
Reason: u.Name + " is paused: an admin can resume it under Settings → Agents."}
}
id := u.ID
return &mcpserver.Caller{ID: u.ID, Name: u.Name, Can: m.Can,
Before: func(ctx context.Context, tool string) error { return s.beforeCall(id, tool) },
After: func(tool string, res *mcp.CallToolResult, err error, took time.Duration) {
s.afterCall(id, tool, res, err, took)
}}, nil
}
// beforeCall keeps an agent within its calls a minute, and stops a paused
// agent's call even mid-session.
func (s *Server) beforeCall(agent int64, tool string) error {
now := time.Now()
lim, err := users.LimitsOf(s.DB, agent)
if err != nil {
return err
}
if !lim.Paused.IsZero() {
return errors.New("this agent is paused: an admin can resume it under Settings → Agents")
}
n, err := users.CallsSince(s.DB, agent, now.Add(-time.Minute))
if err != nil {
return err
}
if n >= lim.PerMinute {
_ = users.RecordCall(s.DB, users.Call{Agent: agent, At: now, Tool: tool, Outcome: users.CallRefused,
Detail: fmt.Sprintf("over %d calls a minute", lim.PerMinute)})
return fmt.Errorf("too many calls: this agent may make %d a minute; wait a little", lim.PerMinute)
}
return nil
}
// afterCall records an agent's tool call.
func (s *Server) afterCall(agent int64, tool string, res *mcp.CallToolResult, err error, took time.Duration) {
c := users.Call{Agent: agent, At: time.Now().Add(-took), Tool: tool, Outcome: users.CallOK, Took: took}
switch {
case err != nil:
c.Outcome, c.Detail = users.CallError, err.Error()
case res != nil && res.IsError:
c.Outcome = users.CallError
fallthrough
case res != nil:
if len(res.Content) > 0 {
if t, ok := res.Content[0].(*mcp.TextContent); ok {
c.Detail = strings.Join(strings.Fields(t.Text), " ")
}
}
}
if err := users.RecordCall(s.DB, c); err != nil {
s.Log.Warn("agents: recording a call", "err", err)
}
}
// hermesKey is the setting naming the local edition's built-in Hermes agent.
@@ -108,6 +169,7 @@ type agentRow struct {
Working int // keys that work now
Last *users.Key // the last used
BuiltIn bool
Limits users.Limits
}
func (s *Server) agentRows(now time.Time) ([]agentRow, error) {
@@ -121,6 +183,9 @@ func (s *Server) agentRows(now time.Time) ([]agentRow, error) {
continue
}
row := agentRow{TeamUser: m, Access: agentAccessText(m.Membership), BuiltIn: s.isHermes(m.ID)}
if row.Limits, err = users.LimitsOf(s.DB, m.ID); err != nil {
return nil, err
}
if row.Keys, err = users.Keys(s.DB, m.ID); err != nil {
return nil, err
}
@@ -276,6 +341,9 @@ type agentData struct {
Grants []grant
Allowed []approvals.Grant // what it may do on its own
Asked []approvals.Request
Waiting []approvals.Request // what it's waiting on
Calls []users.Call
SentDay int // what it asked to send out in the last 24 hours
Problem string
Now time.Time
}
@@ -320,6 +388,9 @@ func (s *Server) renderAgent(w http.ResponseWriter, r *http.Request, u *users.Us
d := agentData{Row: row, Grants: agentGrants(), Problem: problem, Now: now}
d.Allowed, _ = approvals.Grants(s.DB, u.ID)
d.Asked, _ = approvals.List(s.DB, "", u.ID, 20)
d.Waiting, _ = approvals.List(s.DB, approvals.Waiting, u.ID, 50)
d.Calls, _ = users.RecentCalls(s.DB, u.ID, 50)
d.SentDay, _ = approvals.AskedSince(s.DB, u.ID, now.Add(-24*time.Hour))
s.render(w, r, "agent", page{Title: u.Name, Nav: "settings/agents", Data: d})
return
}
@@ -397,6 +468,27 @@ func (s *Server) agentAction(w http.ResponseWriter, r *http.Request) {
redirect(w, r, back, "key-revoked")
return
}
case "limits":
pm, _ := strconv.Atoi(r.PostFormValue("per_minute"))
pd, _ := strconv.Atoi(r.PostFormValue("out_per_day"))
if err = users.SetLimits(s.DB, u.ID, pm, pd); err != nil {
s.renderAgent(w, r, u, "Each limit is a whole number, at least 1.", http.StatusUnprocessableEntity)
return
}
s.audit(r, "agent-limits", u.Name, fmt.Sprintf("%d calls a minute, %d sent out a day", pm, pd))
redirect(w, r, back, "saved")
return
case "pause", "resume":
by := int64(0)
if v := viewerOf(r); v.User != nil {
by = v.User.ID
}
paused := r.PathValue("action") == "pause"
if err = users.Pause(s.DB, u.ID, paused, by, now); err == nil {
s.audit(r, ifElse(paused, "agent-paused", "agent-resumed"), u.Name, "")
redirect(w, r, back, ifElse(paused, "agent-paused", "agent-resumed"))
return
}
case "disable":
if err = users.SetStatus(s.DB, u.ID, users.Disabled); err == nil {
s.audit(r, "disabled", u.Name, "an agent: its keys stop working")