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.
31 lines
1.1 KiB
Go
31 lines
1.1 KiB
Go
package tools
|
|
|
|
import "testing"
|
|
|
|
func TestListLimit(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input map[string]any
|
|
def int
|
|
max int
|
|
want int
|
|
}{
|
|
{name: "absent", input: map[string]any{}, def: 20, max: 100, want: 20},
|
|
{name: "zero", input: map[string]any{"limit": float64(0)}, def: 20, max: 100, want: 20},
|
|
{name: "negative", input: map[string]any{"limit": float64(-5)}, def: 20, max: 100, want: 20},
|
|
{name: "float64 within range", input: map[string]any{"limit": float64(42)}, def: 20, max: 100, want: 42},
|
|
{name: "int within range", input: map[string]any{"limit": 7}, def: 20, max: 100, want: 7},
|
|
{name: "clamped to max", input: map[string]any{"limit": float64(200)}, def: 20, max: 100, want: 100},
|
|
{name: "exactly max", input: map[string]any{"limit": float64(100)}, def: 20, max: 100, want: 100},
|
|
{name: "exactly def", input: map[string]any{"limit": float64(20)}, def: 20, max: 100, want: 20},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := ListLimit(tt.input, tt.def, tt.max); got != tt.want {
|
|
t.Errorf("ListLimit() = %d, want %d", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|