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
+125
View File
@@ -0,0 +1,125 @@
package users
import (
"database/sql"
"errors"
"time"
"supermarket/internal/db"
)
// An agent's limits: calls a minute, things sent out a day, and whether
// it's paused. Agents without a row have the defaults.
// Default limits.
const (
DefaultPerMinute = 60
DefaultOutPerDay = 50
)
// Limits are an agent's.
type Limits struct {
PerMinute int
OutPerDay int
Paused time.Time // zero when running
PausedBy int64
}
// LimitsOf are an agent's limits.
func LimitsOf(d *db.DB, agent int64) (Limits, error) {
l := Limits{PerMinute: DefaultPerMinute, OutPerDay: DefaultOutPerDay}
var paused string
err := d.QueryRow(`SELECT per_minute, out_per_day, paused_at, paused_by FROM agent_limits WHERE agent_id = ?`, agent).
Scan(&l.PerMinute, &l.OutPerDay, &paused, &l.PausedBy)
if errors.Is(err, sql.ErrNoRows) {
return l, nil
}
l.Paused = parseTS(paused)
return l, err
}
// SetLimits changes an agent's calls a minute and sends a day (each at
// least 1).
func SetLimits(d *db.DB, agent int64, perMinute, outPerDay int) error {
if perMinute < 1 || outPerDay < 1 {
return errors.New("limits are at least 1")
}
_, err := d.Exec(`INSERT INTO agent_limits (agent_id, per_minute, out_per_day) VALUES (?, ?, ?)
ON CONFLICT (agent_id) DO UPDATE SET per_minute = excluded.per_minute, out_per_day = excluded.out_per_day`, agent, perMinute, outPerDay)
return err
}
// Pause stops all of an agent's calls (by: who), or lets them go on.
func Pause(d *db.DB, agent int64, paused bool, by int64, now time.Time) error {
at := ""
if paused {
at = ts(now)
}
_, err := d.Exec(`INSERT INTO agent_limits (agent_id, per_minute, out_per_day, paused_at, paused_by) VALUES (?, ?, ?, ?, ?)
ON CONFLICT (agent_id) DO UPDATE SET paused_at = excluded.paused_at, paused_by = excluded.paused_by`,
agent, DefaultPerMinute, DefaultOutPerDay, at, by)
return err
}
// Call is one tool call an agent made.
type Call struct {
ID int64
Agent int64
At time.Time
Tool string
Outcome string // ok, error, refused
Detail string
Took time.Duration
}
// Outcomes.
const (
CallOK = "ok"
CallError = "error"
CallRefused = "refused"
)
// RecordCall notes a tool call.
func RecordCall(d *db.DB, c Call) error {
if r := []rune(c.Detail); len(r) > 300 {
c.Detail = string(r[:299]) + "…"
}
_, err := d.Exec(`INSERT INTO agent_calls (agent_id, at, tool, outcome, detail, ms) VALUES (?, ?, ?, ?, ?, ?)`,
c.Agent, ts(c.At), c.Tool, c.Outcome, c.Detail, c.Took.Milliseconds())
return err
}
// CallsSince counts an agent's calls since a time, not counting those
// refused over the rate.
func CallsSince(d *db.DB, agent int64, since time.Time) (int, error) {
var n int
err := d.QueryRow(`SELECT count(*) FROM agent_calls WHERE agent_id = ? AND at >= ? AND outcome <> ?`, agent, ts(since), CallRefused).Scan(&n)
return n, err
}
// RecentCalls are an agent's last calls, newest first.
func RecentCalls(d *db.DB, agent int64, limit int) ([]Call, error) {
rows, err := d.Query(`SELECT id, agent_id, at, tool, outcome, detail, ms FROM agent_calls WHERE agent_id = ? ORDER BY id DESC LIMIT ?`, agent, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var out []Call
for rows.Next() {
var c Call
var at string
var ms int64
if err := rows.Scan(&c.ID, &c.Agent, &at, &c.Tool, &c.Outcome, &c.Detail, &ms); err != nil {
return nil, err
}
c.At, c.Took = parseTS(at), time.Duration(ms)*time.Millisecond
out = append(out, c)
}
return out, rows.Err()
}
// PruneCalls forgets calls older than 30 days.
func PruneCalls(d *db.DB, now time.Time) error {
_, err := d.Exec(`DELETE FROM agent_calls WHERE at < ?`, ts(now.Add(-30*24*time.Hour)))
return err
}
+37
View File
@@ -105,3 +105,40 @@ func TestAgentKeys(t *testing.T) {
t.Error("keys outlived the agent")
}
}
func TestAgentLimitsAndCalls(t *testing.T) {
d := openDB(t)
now := time.Now()
a, _ := CreateAgent(d, "Bot", "", now)
l, _ := LimitsOf(d, a.ID)
if l.PerMinute != DefaultPerMinute || l.OutPerDay != DefaultOutPerDay || !l.Paused.IsZero() {
t.Errorf("defaults %+v", l)
}
if err := SetLimits(d, a.ID, 0, 5); err == nil {
t.Error("zero accepted")
}
SetLimits(d, a.ID, 10, 5)
Pause(d, a.ID, true, 7, now)
l, _ = LimitsOf(d, a.ID)
if l.PerMinute != 10 || l.OutPerDay != 5 || l.Paused.IsZero() || l.PausedBy != 7 {
t.Errorf("set %+v", l)
}
Pause(d, a.ID, false, 7, now)
if l, _ = LimitsOf(d, a.ID); !l.Paused.IsZero() || l.PerMinute != 10 {
t.Errorf("resumed %+v", l)
}
RecordCall(d, Call{Agent: a.ID, At: now.Add(-2 * time.Minute), Tool: "x", Outcome: CallOK})
RecordCall(d, Call{Agent: a.ID, At: now, Tool: "y", Outcome: CallError, Detail: strings.Repeat("e", 400), Took: 1500 * time.Millisecond})
RecordCall(d, Call{Agent: a.ID, At: now, Tool: "z", Outcome: CallRefused})
if n, _ := CallsSince(d, a.ID, now.Add(-time.Minute)); n != 1 {
t.Errorf("calls in a minute: %d", n)
}
calls, _ := RecentCalls(d, a.ID, 10)
if len(calls) != 3 || calls[1].Tool != "y" || calls[1].Took != 1500*time.Millisecond || len([]rune(calls[1].Detail)) != 300 {
t.Errorf("recent %+v", calls)
}
PruneCalls(d, now.Add(31*24*time.Hour))
if calls, _ = RecentCalls(d, a.ID, 10); len(calls) != 0 {
t.Error("not pruned")
}
}