Skip to content

Agent Supervision System and Open Source Model Enhancements#206

Merged
asdek merged 15 commits intomasterfrom
feature/next_release
Mar 15, 2026
Merged

Agent Supervision System and Open Source Model Enhancements#206
asdek merged 15 commits intomasterfrom
feature/next_release

Conversation

@asdek
Copy link
Contributor

@asdek asdek commented Mar 15, 2026

Description of the Change

Problem

Smaller open source models (< 32B parameters) required additional supervision to match cloud API quality. Agents could get stuck in loops or miss optimal attack paths. Vector store searches returned insufficient results due to single-query limitation. Target information leakage in vector memory could cause agents to switch targets mid-attack. No production-grade guide existed for air-gapped deployments with local LLM inference.

Solution

Implemented multi-layered agent supervision system with:

  • Execution Monitoring (beta): Automatic mentor (adviser) intervention analyzes execution patterns, detects loops, suggests alternative strategies. Testing with Qwen3.5-27B-FP8 shows 2x improvement in result quality vs. baseline
  • Task Planning (beta): Planner generates 3-7 step execution plans before specialist agents begin work, preventing scope creep
  • Tool Call Limits: Hard caps prevent infinite loops (general agents: 100, limited agents: 20) with reflector-guided graceful termination
  • Multi-Query Vector Search: Agents can submit up to 5 search queries simultaneously for better knowledge retrieval
  • Target Anonymization: Search/store operations anonymize IP addresses and domains to prevent target switching
  • vLLM Deployment Guide: Comprehensive documentation for running Qwen3.5-27B-FP8 locally (13K TPS prompt processing on 4× RTX 5090)
  • Installer Configuration: AI Agents Settings form expanded with supervision controls
  • Prompt Improvements: Enhanced stability and OOB attack coverage across all agent prompts
  • langchaingo Update: v0.1.14-update.5 with Bedrock multi-tool fixes and OpenAI streaming robustness
  • Ollama Cloud Support: Added API key support and enhanced configuration

Closes #175, #176

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • 🚀 New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Configuration change
  • 🧪 Test update
  • 🛡️ Security update

Areas Affected

  • Core Services (Frontend UI/Backend API)
  • AI Agents (Researcher/Developer/Executor)
  • Security Tools Integration
  • Memory System (Vector Store/Knowledge Base)
  • Monitoring Stack (Grafana/OpenTelemetry)
  • Analytics Platform (Langfuse)
  • External Integrations (LLM/Search APIs)
  • Documentation
  • Infrastructure/DevOps

Testing and Verification

Test Configuration

PentAGI Version: Latest development (feature/next_release)
Docker Version: 24.0.x+
Host OS: Linux (Ubuntu 22.04)
LLM Provider: vLLM (Qwen3.5-27B-FP8), OpenAI, Anthropic
Enabled Features: Core + supervision features (beta)
Hardware: 4× RTX 5090 (128GB VRAM total)

Test Steps

  1. Run all backend unit tests (go test ./...)
  2. Test execution monitoring with Qwen3.5-27B-FP8 (complex penetration testing flows)
  3. Test task planning with enhanced adviser configuration
  4. Verify multi-query vector search (up to 5 simultaneous queries)
  5. Test target anonymization in vector store operations
  6. Test infinite loop prevention with tool call limits
  7. Test detached command context isolation
  8. Verify Chinese provider configurations (DeepSeek, GLM, Kimi, Qwen)
  9. Test AI Agents Settings installer form with all 8 fields
  10. Verify vLLM deployment guide with real hardware

Test Results

  • ✅ All existing and new unit tests pass
  • Quality improvement: 2x better results with supervision enabled on Qwen3.5-27B-FP8
  • Performance verified: 13K TPS prompt processing, 650 TPS completion on 4× RTX 5090
  • ✅ Infinite loop prevention working (7 consecutive calls → abort)
  • ✅ Multi-query vector search returns diverse results
  • ✅ Target anonymization prevents context leakage
  • ✅ Detached commands survive parent context cancellation
  • ✅ Chinese provider tests added and passing
  • ✅ Installer form validates all supervision parameters correctly
  • ✅ No breaking changes to existing functionality

Security Considerations

Enhanced Security:

  • Target Anonymization: IP addresses and domains anonymized in vector store to prevent accidental target switching during multi-phase attacks
  • Context Isolation: Detached terminal commands now properly isolated from parent context cancellation (prevents premature termination)
  • Loop Prevention: Hard tool call limits prevent resource exhaustion from runaway agents

No New Attack Vectors:

  • All supervision features are read-only analysis mechanisms
  • No new credentials or external dependencies required
  • Maintains existing security model
  • Backward compatible with all deployments

Performance Impact

With Supervision Features Enabled (for models < 32B):

  • Token consumption: 2-3x increase due to mentor/planner invocations
  • Execution time: 2-3x longer due to analysis and planning steps
  • Result quality: 2x improvement in completeness, accuracy, and attack coverage
  • Trade-off: User choice between speed vs. quality

Multi-Query Vector Search:

  • Up to 5 parallel queries vs. previous single query
  • Better knowledge retrieval diversity
  • Minimal latency increase (queries run in parallel)

Infinite Loop Prevention:

  • Eliminates unbounded executions
  • Reduces resource waste from repeated failed attempts

vLLM Performance (Qwen3.5-27B-FP8 on 4× RTX 5090):

  • Prompt processing: ~13,000 tokens/sec
  • Completion generation: ~650 tokens/sec
  • Concurrent flows: 12+ simultaneously
  • VRAM usage: ~30GB per GPU at 0.75 utilization

Documentation Updates

  • README.md updates - Advanced Agent Supervision section, vLLM guide link, feature descriptions
  • API documentation updates
  • Configuration documentation updates - backend/docs/config.md (supervision settings, defaults)
  • GraphQL schema updates - Multi-query search support
  • Other: New comprehensive vLLM deployment guide (examples/guides/vllm-qwen35-27b-fp8.md), backend/docs/flow_execution.md (supervision system), provider configs with/without thinking mode

Deployment Notes

New Environment Variables (Optional - Beta Features Disabled by Default):

# Execution monitoring (beta)
EXECUTION_MONITOR_ENABLED=false              # Default: false
EXECUTION_MONITOR_SAME_TOOL_LIMIT=5          # Default: 5
EXECUTION_MONITOR_TOTAL_TOOL_LIMIT=10        # Default: 10

# Tool call limits (always active)
MAX_GENERAL_AGENT_TOOL_CALLS=100             # Default: 100
MAX_LIMITED_AGENT_TOOL_CALLS=20              # Default: 20

# Task planning (beta)
AGENT_PLANNING_STEP_ENABLED=false            # Default: false

# Ollama Cloud support (optional)
OLLAMA_SERVER_API_KEY=                       # For Ollama Cloud
PENTAGI_OLLAMA_DIR=                          # Custom Ollama volume path

Database Migrations:

  • 20260310_153000_agent_supervision.sql - Adds supervision-related columns and indexes
  • Migrations run automatically on startup

Recommended Configuration for Open Source Models < 32B:
Enable supervision features for 2x quality improvement:

EXECUTION_MONITOR_ENABLED=true
AGENT_PLANNING_STEP_ENABLED=true

Configure adviser with enhanced settings (see examples/configs/vllm-qwen3.5-27b-fp8.provider.yml)

Compatibility:

  • ✅ Fully backward compatible
  • ✅ Beta features disabled by default (no impact on existing deployments)
  • ✅ No breaking API changes
  • ✅ Existing flows work without modification

Checklist

Code Quality

  • My code follows the project's coding standards
  • I have added/updated necessary documentation
  • I have added tests to cover my changes
  • All new and existing tests pass
  • I have run go fmt and go vet (for Go code)
  • I have run npm run lint (for TypeScript/JavaScript code)

Security

  • I have considered security implications
  • Changes maintain or improve the security model
  • Sensitive information has been properly handled

Compatibility

  • Changes are backward compatible
  • Breaking changes are clearly marked and documented
  • Dependencies are properly updated

Documentation

  • Documentation is clear and complete
  • Comments are added for non-obvious code
  • API changes are documented

Additional Notes

Key Changes by Category

🤖 Agent Supervision System (New - Beta)

Execution Monitoring (PRs #178, #179, #180):

  • executionMonitorDetector tracks tool call patterns (same tool: 5, total: 10 thresholds)
  • performMentor invokes adviser agent for execution analysis
  • Enhanced tool responses with <mentor_analysis> and <original_result> sections
  • Prevents loops, suggests alternatives, guides toward completion
  • Configurable via EXECUTION_MONITOR_ENABLED, EXECUTION_MONITOR_SAME_TOOL_LIMIT, EXECUTION_MONITOR_TOTAL_TOOL_LIMIT
  • Testing shows 2x quality improvement on Qwen3.5-27B-FP8
  • Contributors: @asdek

Task Planning:

  • performPlanner generates 3-7 step execution plans via adviser in planning mode
  • question_task_planner.tmpl prompt template for structured planning
  • task_assignment_wrapper.tmpl wraps requests with execution plans
  • Prevents scope creep, improves completion rates for specialist agents
  • Works best with enhanced adviser configuration (maximum reasoning mode)
  • Configurable via AGENT_PLANNING_STEP_ENABLED
  • Contributors: @asdek

Tool Call Limits:

  • Differentiated limits by agent type (general: 100, limited: 20)
  • Reflector-guided graceful termination when approaching limits
  • Prevents infinite loops independently from beta features
  • Configurable via MAX_GENERAL_AGENT_TOOL_CALLS, MAX_LIMITED_AGENT_TOOL_CALLS
  • Contributors: @mason5052, @asdek

Prompt Templates:

  • New: question_execution_monitor.tmpl for mentor invocations
  • New: question_task_planner.tmpl for plan generation
  • New: task_assignment_wrapper.tmpl for wrapping specialist requests
  • Updated: All agent prompts (primary, assistant, coder, pentester, installer, adviser, searcher, enricher, memorist) with supervision awareness and OOB attack improvements
  • Contributors: @asdek

🧠 Memory System Enhancements

Multi-Query Vector Search:

  • search_in_memory, search_guide, search_answer, search_code now support up to 5 simultaneous queries
  • queries array parameter replaces single query string
  • Backward compatible: single query still supported
  • Returns diverse results from multiple search angles
  • Contributors: @asdek

Target Anonymization:

  • New memory_utils.go with IP/domain anonymization functions
  • Replaces actual targets with placeholders (e.g., 203.0.113.1TARGET_IP_1, example.comTARGET_DOMAIN_1)
  • Applied to all search/store vector operations
  • Prevents agents from switching targets based on historical context
  • Unit tests in memory_utils_test.go
  • Contributors: @asdek

Enhanced Storage:

  • Tool results include parent agent context for better traceability
  • Improved metadata filtering and relevance scoring
  • Contributors: @asdek

🐛 Critical Bug Fixes

Infinite Loop Prevention (PR #178, Closes #175):

  • Added hard iteration cap (100) to performAgentChain main loop
  • Repeating detector escalation: 4 soft warnings → abort on 7th consecutive identical call
  • New constant maxSoftDetectionsBeforeAbort with sync comments
  • Contributors: @mason5052

Context Isolation for Detached Commands (PR #179, Closes #176):

  • Fixed detached terminal commands inheriting parent context cancellation
  • Used context.WithoutCancel() for detached goroutines
  • Preserves tracing/logging context values while preventing cancellation propagation
  • Non-detached commands unchanged (still respect parent cancellation)
  • Unit tests: TestExecCommandDetachSurvivesParentCancel, TestExecCommandNonDetachRespectsParentCancel
  • Contributors: @mason5052

🧪 Test Coverage

Chinese Provider Tests (PR #189):

  • Added comprehensive tests for DeepSeek, GLM, Kimi, Qwen providers
  • Config loading, provider type, model loading, model prefix, missing API key, usage tests
  • GLM tests use non-negative price assertions for free-tier models
  • Contributors: @mason5052

Repeating Detector Tests (PR #180):

  • TestRepeatingDetector: 9 cases covering threshold triggering, reset logic, escalation
  • TestRepeatingDetectorEscalationThreshold: validates abort math (threshold + 4 = 7)
  • TestClearCallArguments: message field stripping, key sorting, invalid JSON passthrough
  • Contributors: @mason5052

Config Tests:

  • New backend/pkg/config/config_test.go with supervision settings validation
  • Contributors: @asdek

📚 Documentation & Guides

vLLM + Qwen3.5-27B-FP8 Guide (New):

  • Comprehensive deployment guide: examples/guides/vllm-qwen35-27b-fp8.md
  • Hardware requirements, installation steps, server configuration
  • Two provider configs: with/without thinking mode
  • Performance benchmarks: 13K TPS prompt, 650 TPS completion on 4× RTX 5090
  • Troubleshooting section for common issues
  • Contributors: @asdek

Provider Configurations:

  • Updated vllm-qwen3.5-27b-fp8.provider.yml with optimal sampling parameters
  • New vllm-qwen3.5-27b-fp8-no-think.provider.yml for faster inference without thinking
  • Enhanced adviser configuration examples (maximum reasoning mode)
  • Contributors: @asdek

Configuration Documentation:

  • backend/docs/config.md: New Agent Supervision Settings section with usage details, recommended settings, supervision system integration
  • backend/docs/flow_execution.md: Expanded Advanced Agent Supervision section with execution monitoring, task planning, integration diagrams
  • Updated defaults to false for beta features
  • Contributors: @asdek

README Updates:

  • New Advanced Agent Supervision collapsible section with beta features, recommendations for open source models, trade-offs, configuration strategies
  • Updated feature descriptions for autonomous execution and specialist delegation
  • Added vLLM guide reference in multiple locations
  • Contributors: @asdek

🔧 Installer Enhancements

AI Agents Settings Form Expansion:

  • Expanded from 2 to 8 configuration fields
  • Added integer input fields with validation (ranges: 1-50, 1-100, 10-500, 5-200)
  • Helper methods: createBooleanField, createIntegerField, validateBooleanField, validateIntegerField, formatNumber
  • Updated controller: GetAIAgentsConfig, UpdateAIAgentsConfig, ResetAIAgentsConfig
  • Beta warnings with ⚠️ symbols in UI
  • Comprehensive locale strings with recommendations for open source models
  • Contributors: @asdek

Ollama Enhancements:

  • Added OLLAMA_SERVER_API_KEY for Ollama Cloud support
  • New PENTAGI_OLLAMA_DIR volume configuration
  • SSH key generation in entrypoint for Ollama
  • Added min_p parameter support across all providers
  • Contributors: @asdek

📦 Dependencies

langchaingo v0.1.14-update.5:

  • Fixed Bedrock ValidationException with multiple tool calls (6 message chain variants supported)
  • Fixed OpenAI streaming tool call errors (function name caching for incomplete chunks)
  • Updated Ollama SDK to v0.18.0
  • Contributors: @asdek

🎨 Code Quality

ftester Improvements:

  • Updated mocks for new multi-query vector search API
  • Enhanced test executor and worker logic
  • Contributors: @asdek

Provider Enhancements:

  • Pointer-based model options for memory efficiency
  • Enhanced error handling in tool call retry logic
  • Improved reflector invocation on generation failures
  • Contributors: @asdek

Merged Pull Requests

Issues Addressed

Migration Path

For existing deployments:

  1. Pull latest changes: git pull origin feature/next_release
  2. Rebuild containers: docker compose build
  3. Restart services: docker compose up -d
  4. Migrations apply automatically
  5. Beta features disabled by default - no impact on existing workflows
  6. To enable supervision for open source models < 32B: set EXECUTION_MONITOR_ENABLED=true and AGENT_PLANNING_STEP_ENABLED=true in .env
  7. For local vLLM deployment: follow examples/guides/vllm-qwen35-27b-fp8.md

No manual intervention required. Existing deployments continue working without changes.

dependabot bot and others added 15 commits March 5, 2026 03:26
…dates

Bumps the npm_and_yarn group with 2 updates in the /frontend directory: [minimatch](https://github.com/isaacs/minimatch) and [rollup](https://github.com/rollup/rollup).


Updates `minimatch` from 3.1.2 to 3.1.5
- [Changelog](https://github.com/isaacs/minimatch/blob/main/changelog.md)
- [Commits](isaacs/minimatch@v3.1.2...v3.1.5)

Updates `rollup` from 4.53.1 to 4.59.0
- [Release notes](https://github.com/rollup/rollup/releases)
- [Changelog](https://github.com/rollup/rollup/blob/master/CHANGELOG.md)
- [Commits](rollup/rollup@v4.53.1...v4.59.0)

---
updated-dependencies:
- dependency-name: minimatch
  dependency-version: 3.1.5
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: rollup
  dependency-version: 4.59.0
  dependency-type: indirect
  dependency-group: npm_and_yarn
...

Signed-off-by: dependabot[bot] <support@github.com>
The performAgentChain loop has no iteration cap, allowing infinite loops
when a model repeatedly calls the same tool. The repeating detector
returns a message (not an error), so the loop never breaks.

Add two safety mechanisms:
- Hard cap of 100 iterations on the main agent chain loop
- Escalation to error after 5 consecutive repeating detections
  (8 total identical calls: 3 before first detection + 5 detections)

The soft "please try another tool" response is preserved for the first
4 detections, giving the LLM a chance to course-correct before aborting.

Closes #175

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
- Rename maxRepeatingDetectionsBeforeErr to maxSoftDetectionsBeforeAbort
  for clarity (name now matches behavior: 4 soft warnings before abort)
- Adjust threshold value from 5 to 4 and remove -1 from condition
  (same runtime behavior: abort on 7th consecutive identical call)
- Use errors.New() instead of fmt.Errorf("%s", msg) for non-formatted
  error strings (more idiomatic Go)

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
Detached terminal commands (detach=true) inherit the parent context.
When the parent context is canceled (e.g., agent delegation timeout),
the detached goroutine's ctx.Done() fires and kills the background
command, even though it has its own timeout.

Use context.WithoutCancel(ctx) for the detached goroutine. This
preserves context values (tracing, logging) but prevents parent
cancellation from propagating. The command's own timeout via
context.WithTimeout in getExecResult continues to work.

Non-detached commands are unchanged and still respect parent
cancellation.

Closes #176

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
Validates the core fix: detached goroutine must survive parent context
cancellation (context.WithoutCancel behavior).

TestExecCommandDetachSurvivesParentCancel:
- Starts detach=true command, cancels parent ctx after quick return
- Asserts goroutine does NOT see cancellation (ctxWasCanceled=false)
- This test would FAIL without context.WithoutCancel

TestExecCommandNonDetachRespectsParentCancel:
- Starts detach=false command, cancels parent ctx after 200ms
- Asserts command DOES fail with context error
- Ensures WithoutCancel was NOT applied to non-detach path

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
Rename mockTermLogProvider to contextTestTermLogProvider in
terminal_context_test.go to prevent redeclaration error when
both PR #179 and PR #181 are merged into the same package.

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
Add comprehensive test coverage for the repeating tool call detection
logic that guards against infinite agent chain loops (related to #175).

TestRepeatingDetector (9 cases):
- nil function call, first/second/third identical calls
- threshold triggering at RepeatingToolCallThreshold (3)
- funcCalls reset on different call
- escalation threshold validation (6 vs 7 consecutive calls)
- argument normalization (message field stripping, key ordering)

TestRepeatingDetectorEscalationThreshold:
- Validates escalation math: abort at len >= threshold + 4 = 7

TestClearCallArguments (3 cases):
- message field stripping, key sorting, invalid JSON passthrough

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
- Replace hardcoded +4 with testMaxSoftDetectionsBeforeAbort constant
  with sync comment pointing to performer.go
- Add test case for same function name with different non-message args
  resetting funcCalls (covers the other reset condition in detect())

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
Add config loading, provider type, models loading, model prefix,
missing API key, and usage tests for the four Chinese LLM providers
that were expanded in PR #185 but had no test coverage.

GLM tests use non-negative price assertions to accommodate free-tier
models (glm-4.7-flash, glm-4.5-flash).

Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
…nd/npm_and_yarn-8a6d6a6aaf

chore(deps): bump the npm_and_yarn group across 1 directory with 2 updates
fix: prevent infinite loop in performAgentChain on repeating tool calls
…tion

fix: isolate detached command context from parent cancellation
…eration-cap

test: add unit tests for repeatingDetector and clearCallArguments
test: add unit tests for DeepSeek, GLM, Kimi, and Qwen providers
…anning

- Added new environment variables for execution monitoring and agent planning in `.env.example` and updated `docker-compose.yml` to include these configurations.
- Implemented execution monitoring features, including thresholds for tool call limits and automatic mentor intervention.
- Introduced task planning capabilities for agents to generate structured execution plans.
- Updated documentation to reflect new agent supervision settings and their usage.

This update aims to improve the reliability and efficiency of agent operations in complex scenarios.
@asdek asdek merged commit 8034890 into master Mar 15, 2026
6 checks passed
@asdek asdek deleted the feature/next_release branch March 15, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants