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.
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// approvalStub is a test double that returns a fixed decision or error.
|
|
type approvalStub struct {
|
|
decision ApprovalDecision
|
|
err error
|
|
}
|
|
|
|
func (s *approvalStub) RequestApproval(_ context.Context, _, _ string) (ApprovalDecision, error) {
|
|
return s.decision, s.err
|
|
}
|
|
|
|
func TestApprovalDecisionConstantsAreDistinct(t *testing.T) {
|
|
seen := make(map[ApprovalDecision]bool)
|
|
for _, d := range []ApprovalDecision{
|
|
ApprovalApproved,
|
|
ApprovalPermanentlyApproved,
|
|
ApprovalDenied,
|
|
} {
|
|
if seen[d] {
|
|
t.Errorf("duplicate decision value %d", d)
|
|
}
|
|
seen[d] = true
|
|
}
|
|
}
|
|
|
|
func TestApprovalStubRecordsRequests(t *testing.T) {
|
|
s := &approvalStub{decision: ApprovalApproved}
|
|
d, err := s.RequestApproval(context.Background(), "test_tool", "does something")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if d != ApprovalApproved {
|
|
t.Errorf("decision = %v, want Approved", d)
|
|
}
|
|
}
|
|
|
|
func TestApprovalStubReturnsDenial(t *testing.T) {
|
|
s := &approvalStub{decision: ApprovalDenied}
|
|
d, err := s.RequestApproval(context.Background(), "x", "")
|
|
if err != nil || d != ApprovalDenied {
|
|
t.Errorf("(decision=%v, err=%v), want (Denied, nil)", d, err)
|
|
}
|
|
}
|
|
|
|
func TestApprovalStubReturnsError(t *testing.T) {
|
|
s := &approvalStub{err: errors.New("timeout")}
|
|
_, err := s.RequestApproval(context.Background(), "x", "")
|
|
if err == nil {
|
|
t.Error("expected error from stub")
|
|
}
|
|
}
|
|
|
|
func TestErrApprovalDeniedIsDistinguishable(t *testing.T) {
|
|
if !errors.Is(ErrApprovalDenied, ErrApprovalDenied) {
|
|
t.Error("ErrApprovalDenied does not match itself")
|
|
}
|
|
if errors.Is(ErrApprovalDenied, ErrApprovalNotConfigured) {
|
|
t.Error("ErrApprovalDenied should not match ErrApprovalNotConfigured")
|
|
}
|
|
}
|
|
|
|
func TestToolApprovalTimeoutIsReasonable(t *testing.T) {
|
|
if ToolApprovalTimeout < time.Minute {
|
|
t.Errorf("timeout %v is too short for a human to decide", ToolApprovalTimeout)
|
|
}
|
|
if ToolApprovalTimeout > 5*time.Minute {
|
|
t.Errorf("timeout %v is too long for an LLM connection to hold", ToolApprovalTimeout)
|
|
}
|
|
}
|