Files
archie/internal/tools/classification_test.go
T
sam 6aa3772ce9 feat(gateway): tool-approval gate, session tools for chat, and /delete command
Adds a human-consent gate for tools marked RequiresApproval, exposes
session management as LLM-invokable chat tools, and gives the operator
/delete to retire conversations permanently.

**Tool-approval gate (internal/tools, internal/agentexec)**

- ApprovalRequester contract lives in internal/tools (not gateway) so
  agentexec can import it without pulling in the chat-adapter stack.
- ToolSetOptions.Approval wires the approver through to toolExecute.
- BuildToolSetFrom omits RequiresApproval tools when no approver is
  configured; dispatch paths (invokeTool, tool_call bridge) refuse
  them too, so the invariant holds at every indirection.
- The gate fails closed: only Approved/PermanentlyApproved pass;
  an unknown decision value from a faulty approver does not execute.
- Tool-level timeout (2 min) returns a plain error, not a context
  cancellation, so the model can report "approval timed out" instead
  of the turn aborting.
- BuildApprovalDescription callback lets a tool surface the specific
  resource being acted on ("Delete session \"abc123\"") rather than
  showing a static tool description.

**Session tools (internal/gateware/session_tools.go)**

- session_list (idempotent), session_resume (mutating), session_title
  (mutating), session_delete (mutating + RequiresApproval).
- Scoped to the current platform + channel: GetByChannel replaces
  List() so a model in chat A cannot list or manipulate chat B.
- session_title validates the session belongs to this channel before
  writing; session_resume/session_delete resolve refs against the
  channel-scoped list.
- Shared limit helper extracted to tools.ListLimit.

**Telegram approval adapter (internal/channels/telegram/approval.go)**

- telegramApprover implements ApprovalRequester via the three-button
  inline keyboard (Approve/Permanent/Deny). 2-min token expiry.
- Own map + mutex + "approval:" callback prefix — separate from the
  dangerous-command system.
- Permanent approval uses exact-match (not prefix) lookup keyed by
  action + resource description, so permanently approving "delete
  session abc123" only covers that one session, not every call to
  session_delete for 24h.

**Session lifecycle**

- /delete <session-id>: removes a session and its history. Refuses
  prefixes shorter than 4 chars even when unique. Exact ID wins over
  an ambiguous prefix. sessionTracker.forget sweeps every channel+
  thread pointing at the deleted session.
- Extracted resolveSessionRef and shortSessionID for /resume and
  /delete to share.

**Wiring**

- submitTurn builds the approver onto ctx before the LLM turn.
- chatGenerateOptions reads approver from ctx, passes it through
  ToolSetOptions.
- SessionTools built per turn on the current message, combined with
  taskTools into a single toolset.
2026-08-08 16:42:59 +10:00

171 lines
4.3 KiB
Go

package tools
import (
"encoding/json"
"testing"
)
func TestToolClassificationIsIdempotent(t *testing.T) {
t.Run("zero value is not idempotent", func(t *testing.T) {
var c ToolClassification
if c.IsIdempotent() {
t.Error("zero classification should not be idempotent")
}
})
t.Run("explicit idempotent flag", func(t *testing.T) {
if !ClassIdempotent.IsIdempotent() {
t.Error("ClassIdempotent should be idempotent")
}
})
t.Run("idempotent is not mutating", func(t *testing.T) {
if ClassIdempotent.IsMutating() {
t.Error("idempotent should not be mutating")
}
})
}
func TestToolClassificationIsMutating(t *testing.T) {
t.Run("zero value is not mutating", func(t *testing.T) {
var c ToolClassification
if c.IsMutating() {
t.Error("zero classification should not be mutating")
}
})
t.Run("explicit mutating flag", func(t *testing.T) {
if !ClassMutating.IsMutating() {
t.Error("ClassMutating should be mutating")
}
})
t.Run("mutating is not idempotent", func(t *testing.T) {
if ClassMutating.IsIdempotent() {
t.Error("mutating should not be idempotent")
}
})
}
func TestToolClassificationCombined(t *testing.T) {
c := ClassIdempotent | ClassMutating
if !c.IsIdempotent() {
t.Error("combined should be idempotent")
}
if !c.IsMutating() {
t.Error("combined should be mutating")
}
}
func TestToolClassificationIsApprovalRequired(t *testing.T) {
t.Run("zero value does not require approval", func(t *testing.T) {
var c ToolClassification
if c.IsApprovalRequired() {
t.Error("zero classification should not require approval")
}
})
t.Run("explicit approval flag", func(t *testing.T) {
if !RequiresApproval.IsApprovalRequired() {
t.Error("RequiresApproval should require approval")
}
})
t.Run("approval is independent of idempotent", func(t *testing.T) {
c := ClassIdempotent | RequiresApproval
if !c.IsApprovalRequired() {
t.Error("idempotent + approval should require approval")
}
if !c.IsIdempotent() {
t.Error("idempotent + approval should still be idempotent")
}
})
t.Run("approval is independent of mutating", func(t *testing.T) {
c := ClassMutating | RequiresApproval
if !c.IsApprovalRequired() {
t.Error("mutating + approval should require approval")
}
if !c.IsMutating() {
t.Error("mutating + approval should still be mutating")
}
})
}
func TestToolClassificationString(t *testing.T) {
tests := []struct {
c ToolClassification
want string
}{
{0, "default"},
{ClassIdempotent, "idempotent"},
{ClassMutating, "mutating"},
{ClassIdempotent | ClassMutating, "idempotent|mutating"},
{RequiresApproval, "requires_approval"},
{ClassMutating | RequiresApproval, "mutating|requires_approval"},
{ToolClassification(8), "unknown"},
}
for _, tt := range tests {
got := tt.c.String()
if got != tt.want {
t.Errorf("String() = %q, want %q", got, tt.want)
}
}
}
func TestClassifyEntry(t *testing.T) {
t.Run("idempotent tools are parallel-safe", func(t *testing.T) {
e := ToolEntry{
Name: "reader",
Handler: noopHandler,
Classification: ClassIdempotent,
}
if got := ClassifyEntry(e); got != ExecParallelSafe {
t.Errorf("expected parallel-safe, got %v", got)
}
})
t.Run("mutating tools are never-parallel", func(t *testing.T) {
e := ToolEntry{
Name: "writer",
Handler: noopHandler,
Classification: ClassMutating,
}
if got := ClassifyEntry(e); got != ExecNeverParallel {
t.Errorf("expected never-parallel, got %v", got)
}
})
t.Run("unclassified tools default to never-parallel", func(t *testing.T) {
e := ToolEntry{Name: "unknown", Handler: noopHandler}
if got := ClassifyEntry(e); got != ExecNeverParallel {
t.Errorf("expected never-parallel, got %v", got)
}
})
}
func TestClassificationJSONRoundTrip(t *testing.T) {
e := ToolEntry{
Name: "classified",
Toolset: "test",
Description: "A classified tool",
Handler: noopHandler,
Classification: ClassIdempotent,
}
data, err := json.Marshal(e)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var decoded ToolEntry
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if decoded.Classification != ClassIdempotent {
t.Errorf("Classification = %v after round-trip, want idempotent", decoded.Classification)
}
}