The Agent Validator Plugin is a real-time monitoring and validation system for OpenCode agents. It tracks agent behavior, validates compliance with defined rules, and provides detailed reports on how agents execute tasks.
The plugin auto-loads from .opencode/plugin/ when OpenCode starts.
Install dependencies:
cd ~/.opencode/plugin
npm install
# or
bun install
Verify installation:
opencode --agent openagent
> "analyze_agent_usage"
If you see agent tracking data, the plugin is working! ✅
Start a session and do some work:
opencode --agent openagent
> "Run pwd command"
Agent: [requests approval]
> "proceed"
Check what was tracked:
> "analyze_agent_usage"
Validate compliance:
> "validate_session"
The plugin provides 7 validation tools:
analyze_agent_usagePurpose: Show which agents were active and what tools they used
Usage:
analyze_agent_usage
Example Output:
## Agent Usage Report
**Agents detected:** 2
**Total events:** 7
### openagent
**Active duration:** 133s
**Events:** 5
**Tools used:**
- bash: 2x
- read: 1x
- analyze_agent_usage: 2x
### build
**Active duration:** 0s
**Events:** 2
**Tools used:**
- bash: 2x
When to use:
validate_sessionPurpose: Comprehensive validation of agent behavior against defined rules
Usage:
validate_session
# or with details
validate_session --include_details true
Example Output:
## Validation Report
**Score:** 95%
- ✅ Passed: 18
- ⚠️ Warnings: 1
- ❌ Failed: 0
### ⚠️ Warnings
- **delegation_appropriateness**: Delegated but only 2 files (< 4 threshold)
What it checks:
When to use:
check_approval_gatesPurpose: Verify approval gates were enforced before execution operations
Usage:
check_approval_gates
Example Output:
✅ Approval gate compliance: PASSED
All 3 execution operation(s) were properly approved.
Or if violations found:
⚠️ Approval gate compliance: FAILED
Executed 2 operation(s) without approval:
- bash
- write
Critical rule violated: approval_gate
When to use:
analyze_context_readsPurpose: Show all context files that were read during the session
Usage:
analyze_context_reads
Example Output:
## Context Files Read
**Total reads:** 3
### Files Read:
- **code.md** (2 reads)
`.opencode/context/core/standards/code-quality.md`
- **delegation.md** (1 read)
`.opencode/context/core/workflows/task-delegation-basics.md`
### Timeline:
1. [10:23:45] code.md
2. [10:24:12] delegation.md
3. [10:25:01] code.md
When to use:
check_context_compliancePurpose: Verify required context files were read BEFORE executing tasks
Usage:
check_context_compliance
Example Output:
## Context Loading Compliance
**Score:** 100%
- ✅ Compliant: 2
- ⚠️ Non-compliant: 0
### ✅ Compliant Actions:
- ✅ Loaded standards/code-quality.md before code writing
- ✅ Loaded workflows/task-delegation-basics.md before delegation
### Context Loading Rules:
According to OpenAgent prompt, the agent should:
1. Detect task type from user request
2. Read required context file FIRST
3. Then execute task following those standards
**Pattern:** "Fetch context BEFORE starting work, not during or after"
Context loading rules:
standards/code-quality.mdstandards/documentation.mdstandards/test-coverage.mdworkflows/code-review.mdworkflows/task-delegation-basics.mdWhen to use:
analyze_delegationPurpose: Analyze delegation decisions against the 4+ file rule
Usage:
analyze_delegation
Example Output:
## Delegation Analysis
**Total delegations:** 3
- ✅ Appropriate: 2
- ⚠️ Questionable: 1
**File count per delegation:**
- Average: 4.3 files
- Range: 2 - 6 files
- Threshold: 4+ files
When to use:
debug_validatorPurpose: Inspect what the validator is tracking (for debugging)
Usage:
debug_validator
Example Output:
## Debug Information
```json
{
"sessionID": "abc123...",
"behaviorLogEntries": 7,
"behaviorLogSampleFirst": [
{
"timestamp": 1700000000000,
"agent": "openagent",
"event": "tool_executed",
"data": { "tool": "bash" }
}
],
"behaviorLogSampleLast": [...],
"messagesCount": 5,
"toolTracker": {
"approvalRequested": true,
"toolsExecuted": ["bash", "read"]
},
"allBehaviorLogs": 7
}
Analysis:
When to use:
export_validation_reportPurpose: Export comprehensive validation report to a markdown file
Usage:
export_validation_report
# or specify path
export_validation_report --output_path ./reports/validation.md
Example Output:
✅ Validation report exported to: .tmp/validation-abc12345.md
## Validation Report
[... summary ...]
Generated report includes:
When to use:
| Check | What It Validates | Pass Criteria |
|---|---|---|
approval_gate_enforcement |
Approval requested before execution | Approval language found before bash/write/edit/task |
stop_on_failure |
No auto-fix after errors | Agent stops and reports errors instead of fixing |
lazy_context_loading |
Context loaded only when needed | Context files read match task requirements |
delegation_appropriateness |
Delegation follows 4+ file rule | Delegated when 4+ files, or didn't delegate when <4 |
context_loading_compliance |
Context loaded BEFORE execution | Required context file read before task execution |
tool_usage |
Tool calls tracked | All tool invocations logged |
Scenario: You asked the agent to implement a feature and want to verify it followed its rules.
# 1. Complete your task
> "Create a user authentication system"
[Agent works...]
# 2. Check what agents were involved
> "analyze_agent_usage"
# 3. Validate compliance
> "validate_session"
# 4. Check specific concerns
> "check_approval_gates"
> "check_context_compliance"
# 5. Export report if needed
> "export_validation_report"
Scenario: You want to verify the plugin tracks agent switches correctly.
# 1. Start with one agent
opencode --agent openagent
> "Run pwd"
> "proceed"
# 2. Switch to another agent (manually or via delegation)
# [Switch happens]
# 3. Check tracking
> "analyze_agent_usage"
# Expected: Shows both agents with their respective tools
Scenario: You want to ensure the agent is loading the right context files.
# 1. Ask agent to do a task that requires context
> "Write a new API endpoint following our standards"
[Agent works...]
# 2. Check what context was loaded
> "analyze_context_reads"
# 3. Verify compliance
> "check_context_compliance"
# Expected: Should show standards/code-quality.md was read BEFORE writing
Scenario: Verify the agent always requests approval before execution.
# 1. Ask for an execution operation
> "Delete all .log files"
# 2. Agent should request approval
# Agent: "Approval needed before proceeding."
# 3. Approve
> "proceed"
# 4. Verify compliance
> "check_approval_gates"
# Expected: ✅ Approval gate compliance: PASSED
Scenario: Check if agent delegates appropriately for complex tasks.
# 1. Give a complex multi-file task
> "Refactor the authentication module across 5 files"
[Agent works...]
# 2. Check delegation
> "analyze_delegation"
# Expected: Should show delegation was appropriate (5 files >= 4 threshold)
Cause: Plugin just loaded, no tracking data yet
Solution:
Cause: No bash/write/edit/task operations performed yet
Solution:
Cause: Agent used different approval phrasing than expected
Solution:
agent-validator.ts (lines 12-22)Example customization:
const approvalKeywords = [
"approval",
"approve",
"proceed",
"confirm",
"permission",
"before proceeding",
"should i",
"may i",
"can i proceed",
// Add your custom patterns:
"ready to execute",
"waiting for go-ahead",
]
Cause: Timing issue - context read after task started
Solution:
analyze_context_readsCause: Agent name not properly captured
Solution:
debug_validator to see raw tracking datasessionAgentTracker in debug outputchat.message hookCause: No validation checks were performed
Solution:
debug_validator to see what's trackedEdit .opencode/plugin/agent-validator.ts to customize:
1. Add custom approval keywords:
// Line 12-22
const approvalKeywords = [
"approval",
"approve",
// Add yours:
"your custom phrase",
]
2. Adjust delegation threshold:
// Line 768
const shouldDelegate = writeEditCount >= 4 // Change 4 to your threshold
3. Add custom context loading rules:
// Line 824-851
const contextRules = [
{
taskKeywords: ["your task type"],
requiredFile: "your/context/file.md",
taskType: "your task name"
},
// ... existing rules
]
4. Change severity levels:
// Line 719-726
checks.push({
rule: "your_rule",
passed: condition,
severity: "error", // Change to "warning" or "info"
details: "Your message",
})
Export validation reports in automated workflows:
#!/bin/bash
# validate-agent-session.sh
# Run OpenCode task
opencode --agent openagent --input "Build the feature"
# Export validation report
opencode --agent openagent --input "export_validation_report --output_path ./reports/validation.md"
# Check exit code (if validation fails)
if grep -q "❌ Failed: [1-9]" ./reports/validation.md; then
echo "Validation failed!"
exit 1
fi
echo "Validation passed!"
Add new tools to the plugin:
// In agent-validator.ts, add to tool object:
your_custom_tool: tool({
description: "Your tool description",
args: {
your_arg: tool.schema.string().optional(),
},
async execute(args, context) {
const { sessionID } = context
// Your validation logic here
const result = analyzeYourMetric(sessionID)
return formatYourReport(result)
},
}),
Add custom event tracking:
// In the event() hook:
async event(input) {
const { event } = input
// Track your custom event
if (event.type === "your.custom.event") {
behaviorLog.push({
timestamp: Date.now(),
sessionID: event.properties.sessionID,
agent: event.properties.agent || "unknown",
event: "your_custom_event",
data: {
// Your custom data
},
})
}
}
Session:
$ opencode --agent openagent
> "Help me test this plugin, I am trying to verify if an agent keeps to its promises"
Agent: Let me run some tests to generate tracking data.
> "proceed"
[Agent runs: pwd, reads README.md]
> "analyze_agent_usage"
Result:
## Agent Usage Report
**Agents detected:** 1
**Total events:** 4
### openagent
**Active duration:** 133s
**Events:** 4
**Tools used:**
- bash: 2x
- read: 1x
- analyze_agent_usage: 1x
Verification: ✅ Plugin successfully tracked agent name, tools, and events
Session:
$ opencode --agent build
[Do some work with build agent]
$ opencode --agent openagent
[Switch to openagent]
> "analyze_agent_usage"
Result:
## Agent Usage Report
**Agents detected:** 2
**Total events:** 7
### build
**Active duration:** 0s
**Events:** 2
**Tools used:**
- bash: 2x
### openagent
**Active duration:** 133s
**Events:** 5
**Tools used:**
- bash: 2x
- read: 1x
- analyze_agent_usage: 2x
Verification: ✅ Plugin tracked both agents and their respective activities
Session:
> "Run npm install"
Agent: ## Proposed Plan
1. Run npm install
**Approval needed before proceeding.**
> "proceed"
[Agent executes]
> "check_approval_gates"
Result:
✅ Approval gate compliance: PASSED
All 1 execution operation(s) were properly approved.
Verification: ✅ Agent requested approval before bash execution
Always run validation after multi-step or complex tasks to ensure compliance.
Use export_validation_report to keep records of agent behavior over time.
Verify agents are loading the right context files with check_context_compliance.
Use analyze_agent_usage to track delegation and agent switching patterns.
If something seems off, run debug_validator immediately to see raw data.
Adjust validation rules, thresholds, and keywords to match your workflow.
Add validation checks to your development workflow or CI/CD pipeline.
A: No, tracking is lightweight and runs asynchronously. Minimal performance impact.
A: Yes, edit agent-validator.ts and comment out checks you don't need.
A: No, tracking is per-session. Each new OpenCode session starts fresh.
A: Yes, add custom event tracking and validation tools (see Advanced Usage).
A: Customize approval keywords and validation patterns in agent-validator.ts.
A: Yes, the plugin tracks any agent running in OpenCode.
A: Restart OpenCode - tracking resets on each session start.
A: Currently exports as Markdown. You can modify generateDetailedReport() for JSON.
agent-validator.ts for your needsHappy validating! 🎯