Skip to content

Latest commit

 

History

History
156 lines (121 loc) · 4.92 KB

APPROVAL_STATUS_DESIGN_IMPROVEMENT.md

File metadata and controls

156 lines (121 loc) · 4.92 KB

Approval Status Design Improvement

Problem Identified (2025-11-11)

User correctly identified confusing state model in approval workflow:

# Current Design (CONFUSING)
pending_approval: Optional[Dict[str, Any]] = None

Issues with current design:

  • pending_approval=None is ambiguous - could mean:
    • No approval needed
    • Approval was processed (but was it approved or denied?)
    • Approval was cancelled
  • Forces polling loop to check command history to determine actual outcome
  • Inconsistent with command execution queue's proper status model

Current Implementation

AgentTerminalSession (Confusing Pattern)

@dataclass
class AgentTerminalSession:
    pending_approval: Optional[Dict[str, Any]] = None  # Dict or None - unclear

Flow:

  1. Approval requested → pending_approval = {"command": ..., "risk": ..., "reasons": ...}
  2. Approval processed → pending_approval = None
  3. Must check command history to know if approved or denied

Command Execution Queue (Correct Pattern)

# backend/services/agent_terminal_service.py already uses proper status strings:
status="pending_approval"  # Awaiting user decision
status="approved"          # User approved
status="denied"            # User rejected
status="pre_approved"      # Auto-approved by system

Proposed Solution

Replace with Explicit Status Enum

from enum import Enum
from typing import Optional

class ApprovalStatus(Enum):
    """Explicit approval status states"""
    PENDING = "pending"           # Awaiting user decision
    APPROVED = "approved"         # User approved command
    DENIED = "denied"             # User rejected command
    PRE_APPROVED = "pre_approved" # Auto-approved (low risk)
    COMMENTED = "commented"       # User commented without decision
    NONE = None                   # No approval required

@dataclass
class AgentTerminalSession:
    # BEFORE (confusing)
    # pending_approval: Optional[Dict[str, Any]] = None

    # AFTER (clear)
    approval_status: ApprovalStatus = ApprovalStatus.NONE
    approval_request: Optional[Dict[str, Any]] = None  # Separate field for request details

Benefits of New Design

  1. Clear State Machine: Each state has explicit meaning
  2. No Ambiguity: None only means "no approval required"
  3. Type Safety: Enum prevents invalid states
  4. Self-Documenting: Code clearly shows all possible approval states
  5. Consistent: Matches command execution queue pattern
  6. Easier Debugging: Can log exact state without checking history
  7. Simpler Polling: No need to check command history to determine outcome

Migration Path

Phase 1: Add New Fields (Backward Compatible)

@dataclass
class AgentTerminalSession:
    # Keep old field for backward compatibility
    pending_approval: Optional[Dict[str, Any]] = None

    # Add new fields
    approval_status: ApprovalStatus = ApprovalStatus.NONE
    approval_request: Optional[Dict[str, Any]] = None

Phase 2: Dual Write (Both Fields)

Update all code to write to both old and new fields:

# When requesting approval
session.pending_approval = approval_data  # OLD
session.approval_status = ApprovalStatus.PENDING  # NEW
session.approval_request = approval_data  # NEW

# When approving
session.pending_approval = None  # OLD
session.approval_status = ApprovalStatus.APPROVED  # NEW

Phase 3: Migrate Readers

Update all code that reads approval state:

# BEFORE
if session.pending_approval is None:
    # Was it approved or denied? Must check history!

# AFTER
if session.approval_status == ApprovalStatus.APPROVED:
    # Clear and explicit!
elif session.approval_status == ApprovalStatus.DENIED:
    # Also clear!

Phase 4: Remove Old Field

After all reads are migrated, remove pending_approval field.

Impact Areas

Files that need updates:

  1. backend/services/agent_terminal_service.py - Main approval logic
  2. src/chat_workflow_manager.py - Approval polling
  3. autobot-backend/api/agent.py - API endpoints
  4. autobot-frontend/src/components/chat/ChatMessages.vue - Frontend approval UI

Related Issues

  • Infinite approval polling loop (fixed 2025-11-11, commit 2452aeb)
    • Bug was caused by unclear state transitions from None
    • New enum design would have prevented this bug

Priority

Medium-High - While current workaround exists, the confusing state model:

  • Makes code harder to understand and maintain
  • Increases risk of future bugs
  • Wastes CPU cycles checking command history

User Feedback

"pending aproval state none is confusing that would mean there is no state, should be pending, aproved, rejected, and some other state that describes that user comented on command"

Absolutely correct assessment. User identified the exact problem and the proper solution.


Created: 2025-11-11 Status: Design Doc - Awaiting Implementation Priority: Medium-High Effort: Medium (impacts multiple files, needs careful migration)