feat(telegram): /stop stops the chat agent, else the tasks it created, or one task; delete the dead approval actions
This commit is contained in:
1 parent
399098329b
commit
b0f1ad7cdc
52 files changed
+2385
-2781
No files matched your search
+10
-89
@@ -22,7 +22,6 @@ import (
|
||||
type ChatService struct {
|
||||
Contract messaging.ChatContract
|
||||
Updates ChatUpdateService
|
||||
Dangerous *DangerousService
|
||||
updateMu sync.Mutex
|
||||
updateInProgress bool
|
||||
}
|
||||
@@ -126,14 +125,6 @@ type chatToolView struct {
|
||||
Failed bool `json:"failed"`
|
||||
}
|
||||
|
||||
type dangerousRequest struct {
|
||||
Spec string `json:"spec"`
|
||||
}
|
||||
|
||||
type dangerousDecisionRequest struct {
|
||||
Decision string `json:"decision"`
|
||||
}
|
||||
|
||||
func (s *Server) chatReady(w http.ResponseWriter) (*ChatService, bool) {
|
||||
if s.Chat == nil || s.Chat.Contract == nil {
|
||||
http.Error(w, "chat is not configured", http.StatusNotImplemented)
|
||||
@@ -176,30 +167,21 @@ func (s *Server) handleChatSessions(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
writeJSON(w, map[string]any{
|
||||
"sessions": out,
|
||||
"models": snapshot.Models,
|
||||
"models_by_provider": snapshot.ModelsByProvider,
|
||||
"providers": snapshot.Providers,
|
||||
"active_model": snapshot.ActiveModel,
|
||||
"active_provider": snapshot.ActiveProvider,
|
||||
"personas": snapshot.Personas,
|
||||
"active_personas": active,
|
||||
"commands": chatCommandSpecs(chat),
|
||||
"restart_available": snapshot.RestartAvailable,
|
||||
"dangerous_available": chat.Dangerous != nil,
|
||||
"sessions": out,
|
||||
"models": snapshot.Models,
|
||||
"models_by_provider": snapshot.ModelsByProvider,
|
||||
"providers": snapshot.Providers,
|
||||
"active_model": snapshot.ActiveModel,
|
||||
"active_provider": snapshot.ActiveProvider,
|
||||
"personas": snapshot.Personas,
|
||||
"active_personas": active,
|
||||
"commands": chatCommandSpecs(chat),
|
||||
"restart_available": snapshot.RestartAvailable,
|
||||
})
|
||||
}
|
||||
|
||||
func chatCommandSpecs(chat *ChatService) []messaging.CommandSpec {
|
||||
specs := messaging.LocalCommandSpecs()
|
||||
if chat.Dangerous != nil {
|
||||
specs = append(
|
||||
specs,
|
||||
messaging.CommandSpec{Command: "/rollback", Description: "Request approval to restore a filesystem checkpoint", Usage: "/rollback [number]"},
|
||||
messaging.CommandSpec{Command: "/stop", Description: "Request approval to terminate a background process", Usage: "/stop <process-name>"},
|
||||
messaging.CommandSpec{Command: "/deny", Description: "Deny a pending dangerous action", Usage: "/deny <action-id>"},
|
||||
)
|
||||
}
|
||||
return specs
|
||||
}
|
||||
|
||||
@@ -618,67 +600,6 @@ func (s *Server) handleChatUpdateInstall(w http.ResponseWriter, r *http.Request)
|
||||
writeJSON(w, map[string]any{"ok": true, "progress": progress, "result": result})
|
||||
}
|
||||
|
||||
func (s *Server) handleChatDangerousState(w http.ResponseWriter, r *http.Request) {
|
||||
chat, ok := s.chatReady(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if chat.Dangerous == nil {
|
||||
http.Error(w, "dangerous sandbox actions are not configured", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
checkpoints, err := chat.Dangerous.Checkpoints(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"checkpoints": checkpoints, "pending": chat.Dangerous.Pending()})
|
||||
}
|
||||
|
||||
func (s *Server) handleChatDangerousRequest(w http.ResponseWriter, r *http.Request) {
|
||||
chat, ok := s.chatReady(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if chat.Dangerous == nil {
|
||||
http.Error(w, "dangerous sandbox actions are not configured", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
var req dangerousRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.Spec) == "" {
|
||||
http.Error(w, "spec is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
action, result, executed, err := chat.Dangerous.Request(r.Context(), r.PathValue("kind"), req.Spec)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"action": action, "result": result, "executed": executed})
|
||||
}
|
||||
|
||||
func (s *Server) handleChatDangerousDecision(w http.ResponseWriter, r *http.Request) {
|
||||
chat, ok := s.chatReady(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if chat.Dangerous == nil {
|
||||
http.Error(w, "dangerous sandbox actions are not configured", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
var req dangerousDecisionRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.Decision) == "" {
|
||||
http.Error(w, "decision is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
result, err := chat.Dangerous.Decide(r.Context(), r.PathValue("id"), req.Decision)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"ok": true, "result": result})
|
||||
}
|
||||
|
||||
func newChatSourceID() string {
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
|
||||
@@ -104,7 +104,7 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.Error(), status)
|
||||
return
|
||||
}
|
||||
created, err := s.Store.EnqueueChatTask(r.Context(), owner, repo, request.Title, request.Instructions, request.Workflow, request.Identity, request.Inputs)
|
||||
created, err := s.Store.EnqueueChatTask(r.Context(), owner, repo, request.Title, request.Instructions, request.Workflow, request.Identity, "", request.Inputs)
|
||||
if err != nil {
|
||||
s.logf("work request enqueue failed", "err", err)
|
||||
http.Error(w, "work request rejected", http.StatusBadRequest)
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
const (
|
||||
dangerousApprovalLifetime = 10 * time.Minute
|
||||
// The "permanent" decision on the wire grants an extended, but still
|
||||
// bounded, approval: 24 hours, then requests queue again.
|
||||
extendedApprovalLifetime = 24 * time.Hour
|
||||
)
|
||||
|
||||
// DangerousService adapts the daemon-owned sandbox authority to an
|
||||
// interactive approval workflow. It intentionally has no host process or
|
||||
// filesystem access of its own.
|
||||
type DangerousService struct {
|
||||
Authority messaging.DangerousCommandAuthority
|
||||
|
||||
mu sync.Mutex
|
||||
actions map[string]dangerousAction
|
||||
extended map[string]time.Time
|
||||
}
|
||||
|
||||
type dangerousAction struct {
|
||||
ID string
|
||||
Kind string
|
||||
Spec string
|
||||
Description string
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
type DangerousActionView struct {
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Description string `json:"description"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
func NewDangerousService(authority messaging.DangerousCommandAuthority) *DangerousService {
|
||||
return &DangerousService{
|
||||
Authority: authority,
|
||||
actions: make(map[string]dangerousAction),
|
||||
extended: make(map[string]time.Time),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DangerousService) enabled() error {
|
||||
if s == nil || s.Authority == nil {
|
||||
return errors.New("dangerous sandbox actions are not configured")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DangerousService) Checkpoints(ctx context.Context) ([]messaging.CheckpointInfo, error) {
|
||||
if err := s.enabled(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.Authority.ListCheckpoints(ctx)
|
||||
}
|
||||
|
||||
func (s *DangerousService) Pending() []DangerousActionView {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
views := make([]DangerousActionView, 0, len(s.actions))
|
||||
for id, action := range s.actions {
|
||||
if !action.ExpiresAt.After(now) {
|
||||
delete(s.actions, id)
|
||||
continue
|
||||
}
|
||||
views = append(views, DangerousActionView{
|
||||
ID: action.ID, Kind: action.Kind, Description: action.Description, ExpiresAt: action.ExpiresAt,
|
||||
})
|
||||
}
|
||||
return views
|
||||
}
|
||||
|
||||
// Request creates a pending action unless the local operator has granted a
|
||||
// still-valid extended (24-hour) approval for this operation family.
|
||||
func (s *DangerousService) Request(ctx context.Context, kind, spec string) (DangerousActionView, string, bool, error) {
|
||||
if err := s.enabled(); err != nil {
|
||||
return DangerousActionView{}, "", false, err
|
||||
}
|
||||
kind = strings.ToLower(strings.TrimSpace(kind))
|
||||
spec = strings.TrimSpace(spec)
|
||||
if kind != "rollback" && kind != "stop" || spec == "" {
|
||||
return DangerousActionView{}, "", false, errors.New("invalid dangerous action")
|
||||
}
|
||||
description := fmt.Sprintf("%s %s", kind, spec)
|
||||
s.mu.Lock()
|
||||
now := time.Now()
|
||||
if expires := s.extended[kind]; expires.After(now) {
|
||||
s.mu.Unlock()
|
||||
result, err := s.execute(ctx, kind, spec)
|
||||
return DangerousActionView{Kind: kind, Description: description, ExpiresAt: expires}, result, true, err
|
||||
}
|
||||
id := uuid.NewString()
|
||||
action := dangerousAction{ID: id, Kind: kind, Spec: spec, Description: description, ExpiresAt: now.Add(dangerousApprovalLifetime)}
|
||||
s.actions[id] = action
|
||||
s.mu.Unlock()
|
||||
return DangerousActionView{ID: id, Kind: kind, Description: description, ExpiresAt: action.ExpiresAt}, "", false, nil
|
||||
}
|
||||
|
||||
func (s *DangerousService) Decide(ctx context.Context, id, decision string) (string, error) {
|
||||
if err := s.enabled(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
s.mu.Lock()
|
||||
action, ok := s.actions[id]
|
||||
if ok {
|
||||
delete(s.actions, id)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
if !ok || !action.ExpiresAt.After(time.Now()) {
|
||||
return "", errors.New("dangerous action is expired or unknown")
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(decision)) {
|
||||
case "deny":
|
||||
return "Command denied.", nil
|
||||
case "approve":
|
||||
return s.execute(ctx, action.Kind, action.Spec)
|
||||
case "permanent":
|
||||
// Wire kind "permanent" grants the 24-hour extended approval, not an
|
||||
// unlimited one; the literal string is part of the contract with the
|
||||
// channels that dispatch decisions (see the telegram gateway).
|
||||
s.mu.Lock()
|
||||
s.extended[action.Kind] = time.Now().Add(extendedApprovalLifetime)
|
||||
s.mu.Unlock()
|
||||
return s.execute(ctx, action.Kind, action.Spec)
|
||||
default:
|
||||
return "", errors.New("decision must be approve, permanent, or deny")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DangerousService) execute(ctx context.Context, kind, spec string) (string, error) {
|
||||
if kind == "stop" {
|
||||
if err := s.Authority.StopProcess(ctx, spec); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "Process stopped.", nil
|
||||
}
|
||||
var checkpoint int
|
||||
if _, err := fmt.Sscan(spec, &checkpoint); err != nil || checkpoint <= 0 {
|
||||
return "", errors.New("checkpoint must be a positive number")
|
||||
}
|
||||
return s.Authority.Rollback(ctx, checkpoint)
|
||||
}
|
||||
@@ -360,9 +360,6 @@ func (s *Server) registerChatRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/chat/update", s.handleChatUpdate)
|
||||
mux.HandleFunc("POST /api/chat/update/defer", s.handleChatUpdateDefer)
|
||||
mux.HandleFunc("POST /api/chat/update/install", s.handleChatUpdateInstall)
|
||||
mux.HandleFunc("GET /api/chat/dangerous", s.handleChatDangerousState)
|
||||
mux.HandleFunc("POST /api/chat/dangerous/{kind}", s.handleChatDangerousRequest)
|
||||
mux.HandleFunc("POST /api/chat/dangerous/{id}/decision", s.handleChatDangerousDecision)
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
|
||||
Reference in new issue
Block a user