Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

feat(ai): let mobile chat propose actions behind mandatory approval

rcourtman1b7488e55265
diff --git a/internal/api/ai_handler.go b/internal/api/ai_handler.go
index 55e311d26c..bacffcb30b 100644
--- a/internal/api/ai_handler.go
+++ b/internal/api/ai_handler.go
@@ -90,7 +90,25 @@ type AIService interface {
func assistantExecuteAuthority(ctx context.Context) bool {
token := pulsauth.GetAPIToken(ctx)
- return token == nil || token.HasScope(config.ScopeAIExecute)
+ if token == nil || token.HasScope(config.ScopeAIExecute) {
+ return true
+ }
+ // Relay-mobile callers may propose actions. Their sessions are pinned to
+ // approval-required mode at the chat entrypoint, so this authority never
+ // lets a mobile-originated action run unattended.
+ return token.HasScope(config.ScopeRelayMobileAccess)
+}
+
+// assistantApprovalBoundExecuteAuthority reports whether the caller's execute
+// authority exists only by virtue of relay-mobile access. Such sessions must
+// never run autonomously; every action they propose stays on the approval
+// queue, where the mobile app's own approval surface decides it.
+func assistantApprovalBoundExecuteAuthority(ctx context.Context) bool {
+ token := pulsauth.GetAPIToken(ctx)
+ if token == nil || token.HasScope(config.ScopeAIExecute) {
+ return false
+ }
+ return token.HasScope(config.ScopeRelayMobileAccess)
}
type aiServiceRuntimeConfigReader interface {
@@ -2692,6 +2710,10 @@ func (h *AIHandler) HandleChat(w http.ResponseWriter, r *http.Request) {
// Stream from AI chat service
serviceSentDone := false
serviceSentError := false
+ autonomousMode := chatAutonomousModeForFindingHandoff(nil, findingID, handoffContext, handoffResources, handoffActions, handoffMetadata)
+ if assistantApprovalBoundExecuteAuthority(ctx) {
+ autonomousMode = chatApprovalRequiredAutonomousMode()
+ }
err := svc.ExecuteStream(ctx, chat.ExecuteRequest{
Prompt: req.Prompt,
SessionID: req.SessionID,
@@ -2702,7 +2724,7 @@ func (h *AIHandler) HandleChat(w http.ResponseWriter, r *http.Request) {
HandoffResources: handoffResources,
HandoffActions: handoffActions,
HandoffMetadata: handoffMetadata,
- AutonomousMode: chatAutonomousModeForFindingHandoff(nil, findingID, handoffContext, handoffResources, handoffActions, handoffMetadata),
+ AutonomousMode: autonomousMode,
HasExecuteAuthority: assistantExecuteAuthority(ctx),
SuppressSessionEvent: true,
}, func(event chat.StreamEvent) {
diff --git a/internal/api/ai_handler_test.go b/internal/api/ai_handler_test.go
index aa66ddc190..5cd14e0cc5 100644
--- a/internal/api/ai_handler_test.go
+++ b/internal/api/ai_handler_test.go
@@ -45,7 +45,9 @@ func TestAssistantExecuteAuthorityTokenMatrix(t *testing.T) {
}{
{name: "browser session", ctx: context.Background(), want: true},
{name: "chat token", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeAIChat: true}), want: false},
- {name: "relay token", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeRelayMobileAccess: true}), want: false},
+ // Relay-mobile tokens gained propose authority on 2026-07-18; the
+ // chat entrypoint pins their sessions to approval-required mode.
+ {name: "relay token", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeRelayMobileAccess: true}), want: true},
{name: "execute token", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeAIChat: true, config.ScopeAIExecute: true}), want: true},
{name: "unknown scope", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{"ai:unknown": true}), want: false},
}
@@ -56,6 +58,25 @@ func TestAssistantExecuteAuthorityTokenMatrix(t *testing.T) {
}
}
+func TestAssistantApprovalBoundExecuteAuthority(t *testing.T) {
+ tests := []struct {
+ name string
+ ctx context.Context
+ want bool
+ }{
+ {name: "browser session", ctx: context.Background(), want: false},
+ {name: "relay token", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeRelayMobileAccess: true}), want: true},
+ {name: "relay token with execute scope", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeRelayMobileAccess: true, config.ScopeAIExecute: true}), want: false},
+ {name: "execute token", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeAIExecute: true}), want: false},
+ {name: "chat token", ctx: pulsauth.WithAPIToken(context.Background(), assistantScopeToken{config.ScopeAIChat: true}), want: false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equal(t, tt.want, assistantApprovalBoundExecuteAuthority(tt.ctx))
+ })
+ }
+}
+
type syncingMockAIService struct {
MockAIService
cfg *config.AIConfig
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index ffc493430f..206c0e66a9 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -692,7 +692,10 @@ func TestContract_AssistantFindingContextUsesModelOnlyHandoff(t *testing.T) {
"func chatAutonomousModeForFindingHandoff(requested *bool, findingID, handoffContext string, handoffResources []chat.HandoffResource, handoffActions []chat.HandoffAction, handoffMetadata chat.HandoffMetadata) *bool",
`if strings.TrimSpace(findingID) != ""`,
"chatApprovalRequiredAutonomousMode()",
- "AutonomousMode: chatAutonomousModeForFindingHandoff(nil, findingID, handoffContext, handoffResources, handoffActions, handoffMetadata)",
+ "autonomousMode := chatAutonomousModeForFindingHandoff(nil, findingID, handoffContext, handoffResources, handoffActions, handoffMetadata)",
+ "if assistantApprovalBoundExecuteAuthority(ctx) {",
+ "autonomousMode = chatApprovalRequiredAutonomousMode()",
+ "AutonomousMode: autonomousMode",
`appendChatContextLine(&b, "Finding Status", unifiedFindingChatStatus(f, time.Now()))`,
`appendChatContextLine(&b, "Finding Detected At", f.DetectedAt.Format(time.RFC3339))`,
`appendChatContextLine(&b, "Finding Last Seen At", f.LastSeenAt.Format(time.RFC3339))`,