Skip to content

Conversation

@AshishKumar4
Copy link
Collaborator

@AshishKumar4 AshishKumar4 commented Nov 21, 2025

Summary

This PR refactors the agent architecture to separate behavior logic from infrastructure, enabling support for multiple agent behaviors (phasic and agentic modes).

Changes

Core Architecture

  • Renamed simpleGeneratorAgent.tsbaseAgent.ts with extracted base behavior class
  • Added AgentInfrastructure<TState> interface to decouple behavior from Durable Objects
  • Added BaseAgentBehavior<TState> abstract class with common agent functionality
  • Added worker/agents/core/phasic/behavior.ts (852 lines) - phasic behavior implementation
  • Added smartGeneratorAgent.ts - thin wrapper implementing AgentInfrastructure

State Management

  • Modified state.ts - split state into BaseProjectState, PhasicState, AgenticState
  • Modified types.ts - added BehaviorType, generic AgentInitArgs<TState>
  • Breaking: Replaced agentMode: 'deterministic' | 'smart' with behaviorType: 'phasic' | 'agentic'

Operations & Services

  • Modified 38 files in worker/agents/ to use ICodingAgent interface
  • Deleted ScreenshotAnalysisOperation.ts - screenshot handling moved/removed
  • Modified All tool files to accept ICodingAgent instead of concrete class
  • Modified PhaseImplementation.ts - simplified, moved logic to behavior
  • Modified GenerationContext.ts - added PhasicGenerationContext variant

Interface Changes

  • Modified ICodingAgent.ts - formalized interface for all agent behaviors
  • All tools now work through interface rather than concrete implementation

Motivation

The previous architecture tightly coupled agent behavior to Durable Objects infrastructure, making it difficult to:

  1. Support multiple agent behaviors (deterministic phasic vs autonomous agentic)
  2. Test agent logic without Durable Objects overhead
  3. Reuse agent logic in different execution contexts

This refactoring enables:

  • Behavior Switching: Select phasic or agentic mode per session
  • Testing: Mock AgentInfrastructure for unit tests
  • Extensibility: Add new behaviors without modifying infrastructure

Testing

Manual Testing:

  1. Create new session and verify phasic behavior works
  2. Test all LLM tools (generate_files, deep_debug, etc.)
  3. Verify state persistence across DO hibernation
  4. Test user conversation flow and file regeneration

Areas Requiring Extra Attention:

  • State migration for existing sessions (old agentMode → new behaviorType)
  • Screenshot upload functionality (ScreenshotAnalysis operation removed)
  • Deep debugger integration with new interface

Breaking Changes

State Schema:

  • Removed: agentMode: 'deterministic' | 'smart'
  • Added: behaviorType: 'phasic' | 'agentic'

Impact: Existing Durable Object sessions may need migration logic or will default to phasic mode.

Related Issues

  • Part 1 of agent generalization effort
  • Enables future agentic behavior implementation
  • May affect Improve Screenshot Workflow #249 (screenshot workflow) due to ScreenshotAnalysisOperation removal

This PR description was automatically generated by Claude Code

AshishKumar4 and others added 30 commits November 7, 2025 18:00
…ic coding agent implemented

- Abstracted behaviors and objectives
- Behavior and Objectives are bot h AgentComponent
- CodeGeneratorAgent (Agent DO) houses common business logic
- Implemented agentic coding agent and and assistant
- Implemented AI-powered project type prediction (app/workflow/presentation) with confidence scoring and auto-detection when projectType is 'auto'
- Enhanced template selection to filter by project type and skip AI selection for single-template scenarios in workflow/presentation types
- Added GitHub token caching in CodeGeneratorAgent for persistent OAuth sessions across exports
- Updated commitlint config to allow longer commit messages (
- Initialize template cache during agent setup to avoid redundant fetches
- Remove redundant project name prompt from template selection
- Clean up default projectType fallback logic
- Added concurrency control to prevent duplicate workflow runs on the same PR
- Replaced Claude-based comment cleanup with direct GitHub API deletion for better reliability
- Enhanced code debugger instructions to handle Vite dev server restarts and config file restrictions
- Replaced unsafe type assertions with proper type guards for legacy state detection
- Added explicit type definitions for deprecated state fields and legacy file formats
- Eliminated all 'any' types while maintaining backward compatibility with legacy states
…ess design

- Sandbox layer does not rely on templates now, instead expects raw files list
- Tools to init/list templates, files
- Templates can be chosen by agentic mode after creation
- Restructured system prompt with detailed architecture explanations covering virtual filesystem, sandbox environment, and deployment flow
- Better tool descriptions
- Improved communication guidelines and workflow steps for better agent reasoning and execution
- Replaced agent mode toggle with project mode selector (App/Slides/Chat) that determines behavior type
- Implemented agentic behavior detection for static content (docs, markdown) with automatic editor view
- Conditionally render PhaseTimeline and deployment controls based on behavior type (phasic vs agentic)
- Replaced manual template_manager tool with init_suitable_template that uses the original template selector ai
- Updated system prompts to emphasize template-first workflow for interactive projects with AI selector as mandatory first step
- Simplified template selection process by removing manual list/select commands in favor of intelligent matching
```
- Added conversation history support to AgenticProjectBuilder with message preparation and context tracking
- Implemented tool call completion callbacks to sync messages and trigger periodic compactification
- Modified AgenticCodingBehavior to queue user inputs during builds and inject them between tool call chains using abort mechanism
- Fix importTemplate to actually work
- Fixed template filtering logic to respect 'general' project type
- Added behaviorType to logger context for better debugging
- fixed not saving behaviorType to state
…ructor

- Moved behaviorType and projectType initialization from hardcoded values to constructor-based setup
- Changed initial state values to 'unknown' to ensure proper initialization through behavior constructor
- Cleared template details cache when importing new templates to prevent stale data
- Moved user input idle check from PhasicCodingBehavior to CodeGeneratorAgent for consistent behavior across all modes
- Fixed message order in agenticProjectBuilder to place history after user message instead of before
- Added replaceExisting parameter to addConversationMessage for better control over message updates
- Enhanced initial state restoration to include queued user messages and behaviorType
- Added status and queuePosition fields
- Single convo id needs to be broadcasted but messages need to be saved with unique ids.
- Fix message deduplication to use composite key (conversationId + role + tool_call_id)
- Improved tool message filtering to validate against parent assistant tool_calls
- Removed unused CodingAgentInterface stub file
- Simplified addConversationMessage interface by removing replaceExisting parameter
- Added CompletionDetector interface and CompletionConfig for detecting task completion signals
- Implemented dependency-aware parallel tool execution engine with resource conflict detection
- Added LoopDetector to prevent infinite tool call loops with contextual warnings
- Enhanced ToolCallContext with completion signal tracking and warning injection state
- Modified tool execution to respect dependencies and execute in parallel groups
… and debugger

- Added CompletionDetector to track completion signals via dedicated tools (mark_generation_complete, mark_debugging_complete)
- Implemented LoopDetector to prevent infinite tool call loops with contextual warnings
- Created wrapToolsWithLoopDetection utility to inject loop detection into tool execution flow
- Enhanced system prompts to emphasize efficient parallel tool usage and completion discipline
…ic coding agent implemented

- Abstracted behaviors and objectives
- Behavior and Objectives are bot h AgentComponent
- CodeGeneratorAgent (Agent DO) houses common business logic
- Implemented agentic coding agent and and assistant
- Implemented AI-powered project type prediction (app/workflow/presentation) with confidence scoring and auto-detection when projectType is 'auto'
- Enhanced template selection to filter by project type and skip AI selection for single-template scenarios in workflow/presentation types
- Added GitHub token caching in CodeGeneratorAgent for persistent OAuth sessions across exports
- Updated commitlint config to allow longer commit messages (
- Initialize template cache during agent setup to avoid redundant fetches
- Remove redundant project name prompt from template selection
- Clean up default projectType fallback logic
@AshishKumar4 AshishKumar4 changed the title Feat: Generalize coding agent to behaviors + objective part 1 (revert of revert) Feat: General agents + presentations Nov 30, 2025
@github-actions github-actions bot added dependencies Pull requests that update a dependency file ci/cd frontend labels Nov 30, 2025
@AshishKumar4 AshishKumar4 force-pushed the feat/refactor-agents-base branch from b0e7319 to f6f4207 Compare December 1, 2025 19:29
- Remove duplicate "Blueprint generation complete" message from chat hook
- Fix file saving logic to track saved files during generation callbacks
- Ensure deployment to sandbox after template setup completion
- Import FileState type for proper file tracking
Comment on lines +236 to 246
let migratedBehaviorType = state.behaviorType;
if (isStateWithAgentMode(state)) {
migratedBehaviorType = state.agentMode === 'smart' ? 'agentic' : 'phasic';
needsMigration = true;
logger.info('Migrating agentMode to behaviorType', {
oldMode: state.agentMode,
newType: migratedBehaviorType
});
}

return newState;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL: State Migration Bug - behaviorType Not Applied

The migration logic computes migratedBehaviorType (lines 236-244) but NEVER assigns it to the newState object. This means:

  1. The migration is logged but not actually applied
  2. Existing sessions with agentMode will lose their behavior preference
  3. They will default to whatever state.behaviorType was (likely undefined/unknown)

Fix Required:

Suggested change
let migratedBehaviorType = state.behaviorType;
if (isStateWithAgentMode(state)) {
migratedBehaviorType = state.agentMode === 'smart' ? 'agentic' : 'phasic';
needsMigration = true;
logger.info('Migrating agentMode to behaviorType', {
oldMode: state.agentMode,
newType: migratedBehaviorType
});
}
return newState;
let migratedBehaviorType = state.behaviorType;
if (isStateWithAgentMode(state)) {
migratedBehaviorType = state.agentMode === 'smart' ? 'agentic' : 'phasic';
needsMigration = true;
logger.info('Migrating agentMode to behaviorType', {
oldMode: state.agentMode,
newType: migratedBehaviorType
});
// Apply the migrated value
newState.behaviorType = migratedBehaviorType;
delete stateWithDeprecated.agentMode;
}
return newState;

This is a data integrity issue that could break existing user sessions.

Comment on lines +154 to +157
const isAgenticBlueprint = (bp?: Blueprint): bp is AgenticBlueprint => {
if (!bp) return false;
return 'plan' in bp && Array.isArray(bp.plan);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low Priority: Inline Type Guard Can Be Simplified

While this is functional, the type guard doesn't need to be this verbose. TypeScript can infer from the property check alone:

Suggested change
const isAgenticBlueprint = (bp?: Blueprint): bp is AgenticBlueprint => {
if (!bp) return false;
return 'plan' in bp && Array.isArray(bp.plan);
};
const isAgenticBlueprint = (bp?: Blueprint): bp is AgenticBlueprint => {
return !!bp && 'plan' in bp && Array.isArray(bp.plan);
};

Note: Previous review comment suggested avoiding (bp as any).plan which has been correctly fixed here. This is just a minor simplification suggestion.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 7, 2025

Claude finished @AshishKumar4's task —— View job


Code Review Complete

Task List

  • Check and update PR description if needed
  • Get PR diff and understand changes
  • Read project conventions (CLAUDE.md)
  • Review critical security paths (worker/api/, worker/database/)
  • Review code quality (type safety, DRY, architecture)
  • Review state migration logic
  • Address previous review comments
  • Post inline comments for critical issues
  • Post final review summary

Review Status: Complete

Recommendation: REQUEST CHANGES (1 critical bug found)

Summary:

  • 1 critical state migration bug requiring fix (see inline comment)
  • No security vulnerabilities found
  • Architecture refactoring is well-designed
  • Minor type safety improvements suggested

Full review posted in comment below.


Comment on lines +236 to 246
let migratedBehaviorType = state.behaviorType;
if (isStateWithAgentMode(state)) {
migratedBehaviorType = state.agentMode === 'smart' ? 'agentic' : 'phasic';
needsMigration = true;
logger.info('Migrating agentMode to behaviorType', {
oldMode: state.agentMode,
newType: migratedBehaviorType
});
}

return newState;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL: State Migration Bug - behaviorType Not Applied

The migration logic computes migratedBehaviorType (lines 236-244) but NEVER assigns it to the newState object. This causes:

  1. Data Loss: Existing Durable Objects with agentMode field lose their behavior preference
  2. Incorrect Defaults: Sessions fall back to undefined/unknown behavior instead of preserving user intent
  3. Breaking Change: The PR description acknowledges this breaking change but the migration doesn't actually preserve the data

Fix Required:

let migratedBehaviorType = state.behaviorType;
if (isStateWithAgentMode(state)) {
    migratedBehaviorType = state.agentMode === 'smart' ? 'agentic' : 'phasic';
    needsMigration = true;
    logger.info('Migrating agentMode to behaviorType', { 
        oldMode: state.agentMode, 
        newType: migratedBehaviorType 
    });
    // Apply the migrated value to newState
    newState.behaviorType = migratedBehaviorType;
    // Delete the old field
    delete stateWithDeprecated.agentMode;
}

return newState;

Note from PR author comment: "Smart mode was never introduced" - if this is true, then this migration may be unnecessary. However, if agentMode: 'deterministic' existed in production, it should still be migrated to behaviorType: 'phasic' to prevent data loss.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

Code Quality & Security Review

Recommendation: REQUEST CHANGES

Critical Issues

1. State Migration Bug - Data Loss Risk (CRITICAL)

Location: worker/agents/core/stateMigration.ts lines 236-246
Issue: The migration computes migratedBehaviorType but never applies it to newState, causing data loss for existing Durable Object sessions with the deprecated agentMode field.

Impact:

  • Existing sessions with agentMode will lose their behavior preference
  • Sessions default to undefined/unknown behavior instead of preserving user intent
  • Breaking change acknowledged in PR description but not properly handled

Fix: See inline comment on lines 236-246 for the required fix.

Note: PR author commented that "Smart mode was never introduced," but if agentMode: 'deterministic' existed in production, it should still be migrated to prevent data loss.


Code Quality Issues

2. Type Safety - any Type Usage (Medium)

Location: worker/agents/core/stateMigration.ts line 98
Issue: Uses any type in getTimestamp function, violating CLAUDE.md Rule #1

Several other instances of any exist in utility files:

  • worker/agents/inferutils/schemaFormatters.ts (multiple instances - appears to be schema validation utilities where any may be necessary)
  • worker/agents/core/stateMigration.ts line 98 (should be ConversationMessage)

Recommendation: Replace with proper ConversationMessage type for the stateMigration.ts instance. The schemaFormatters.ts instances appear to be in generic schema validation code where any may be unavoidable.


Security Review

No critical security vulnerabilities found.

Database Security

  • SQL queries properly use parameterized queries via template literals (Drizzle ORM)
  • No SQL injection vulnerabilities detected
  • D1 database access patterns are safe

Authentication & Authorization

  • JWT handling appears secure (not modified in this PR)
  • No new authentication/authorization code introduced
  • Existing auth patterns maintained

Secrets Handling

  • GitHub token cache is ephemeral (memory only, lost on DO eviction)
  • Secrets stored in state have proper typing (type: 'secret')
  • No secrets exposed in logs or responses

Context Note

This is the official Cloudflare Vibesdk codebase. The changes primarily affect agent architecture (worker/agents/core/) rather than security-critical API/database layers. The refactoring separates behavior from infrastructure but doesn't introduce new attack surfaces.


Architecture Review

Overall Assessment: The refactoring is well-designed and achieves its goals.

Strengths

  1. Clean separation of concerns: AgentInfrastructure<TState> interface properly decouples behavior from Durable Objects
  2. Type safety: Extensive use of TypeScript discriminated unions (BehaviorType, state types)
  3. Extensibility: New behaviors can be added without modifying infrastructure
  4. DRY compliance: Shared logic properly extracted to base classes

Areas for Improvement

  1. State migration: Critical bug must be fixed before merge
  2. Type safety: Minimal any usage should be reduced where possible
  3. Testing: Large refactoring (100 files) would benefit from explicit test coverage verification

Previous Review Comments

Most previous review comments have been addressed:

  • Type guard in AgenticProjectBuilder.ts (line 154-157) is now clean (no any cast)
  • Import issues mentioned in earlier reviews appear to be resolved
  • File structure properly reorganized

Recommendation

REQUEST CHANGES due to the critical state migration bug. Once fixed, this PR will be ready for approval.

Required Fix:

  1. Apply migratedBehaviorType to newState.behaviorType in stateMigration.ts

Suggested Improvements:

  1. Replace any type in stateMigration.ts line 98 with ConversationMessage
  2. Add migration tests to verify agentModebehaviorType conversion
  3. Consider adding explicit backward compatibility notes in deployment docs

Review completed by: Claude Code
Focus areas: Security (SQL injection, auth, secrets), Type safety (CLAUDE.md compliance), State migration logic, Architecture patterns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent ci/cd dependencies Pull requests that update a dependency file frontend size/XL worker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant