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
@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2025

Claude finished @AshishKumar4's task —— View job


Code Quality & Security Review

Recommendation: REQUEST CHANGES (Critical bug found)


Executive Summary

This is a significant architectural refactoring that successfully decouples agent behavior from Durable Objects infrastructure. The overall design is solid, but there is one critical bug in state migration that must be fixed before merging, plus several code quality improvements needed per CLAUDE.md standards.


Critical Issues

1. CRITICAL: State Migration Bug - Data Loss Risk

Location: worker/agents/core/stateMigration.ts:236-246
Severity: HIGH - Data Integrity Issue

The migration code computes migratedBehaviorType but never applies it to newState. This means:

  • Existing Durable Object sessions with agentMode will lose their behavior preference
  • Sessions will default to undefined/unknown behavior type
  • User experience will break for existing sessions

Impact: Affects all existing production sessions during deployment.

See inline comment for fix


Code Quality Issues (CLAUDE.md Violations)

2. Type Safety: 'any' Type Usage

Location: worker/agents/core/stateMigration.ts:98
CLAUDE.md Rule #1: "NEVER use any type"

const getTimestamp = (msg: any) => {

The parameter is already typed as ConversationMessage from the array iteration. Use proper types.

Other instances found:

  • worker/agents/inferutils/schemaFormatters.ts - Multiple any usages (lines 384, 763, 979, 1093)
  • These appear to be in schema transformation utilities where any may be intentional for dynamic schema handling, but should be reviewed

Architecture Review

Positive Changes

Well-Designed Abstractions:

  • AgentInfrastructure<TState> interface cleanly separates infrastructure from behavior
  • BaseAgentBehavior<TState> provides good foundation for multiple behaviors
  • State split into BaseProjectState, PhasicState, AgenticState is logical
  • Operations properly refactored to use ICodingAgent interface

Code Organization:

  • 1750 lines in base.ts, 717 in phasic.ts, 392 in agentic.ts - reasonable distribution
  • Clear separation of concerns between behaviors and infrastructure
  • Proper use of TypeScript discriminated unions for state types

Migration Strategy:

  • Good migration logic for legacy fields (latestScreenshot, templateDetails)
  • Conversation deduplication logic is solid
  • Proper cleanup of internal memos

Areas of Concern

1. Type Assertions in Phasic Behavior
The code at worker/agents/core/behaviors/phasic.ts:201-214 uses a type guard which is GOOD:

const context = GenerationContext.from(this.state, this.getTemplateDetails(), this.logger);
if (!GenerationContext.isPhasic(context)) {
    throw new Error('Expected PhasicGenerationContext');
}

This is the correct approach and addresses previous review comments about unsafe type assertions.

2. Missing Import - Agentic Behavior
Previous review comments noted missing AgenticAgentBehavior import in smartGeneratorAgent.ts. Checking current code:

  • File worker/agents/core/smartGeneratorAgent.ts was DELETED
  • File worker/agents/core/codingAgent.ts now handles behavior instantiation (lines 14-15):
import { PhasicCodingBehavior } from './behaviors/phasic';
import { AgenticCodingBehavior } from './behaviors/agentic';

This issue is RESOLVED - imports are present and correct.

3. State Machine Complexity
The phasic behavior state machine (lines 284-313) is complex but well-structured with clear state transitions. No concerns.


Security Analysis

Reviewed critical paths: worker/api/, worker/database/, worker/agents/

Findings:

  • No SQL injection vulnerabilities found
  • No authentication/authorization issues introduced
  • Git operations properly use parameterized queries via isomorphic-git
  • WebSocket message handling maintains proper validation
  • File system operations properly sanitized via normalizePath and isPathSafe
  • Image upload uses proper validation via uploadImage utility

Note: This PR primarily refactors internal architecture without touching security-sensitive authentication, database queries, or API endpoints. The main security concern is the state migration bug which could cause availability issues.


Breaking Changes Review

Breaking Change: agentModebehaviorType

The PR description acknowledges this. The migration logic exists but is buggy (see Critical Issue #1). Once fixed, the migration should handle backward compatibility correctly.

Missing from Migration:

  • Should also delete the old agentMode field after migration (currently only logs it)
  • Fix has been provided in inline comment

Testing Recommendations

Given the scope of this refactor, recommend:

  1. Manual Testing:

    • ✓ Create new phasic session
    • ✓ Create new agentic session
    • CRITICAL: Test state migration from old session with agentMode field
    • ✓ Verify state persistence across DO hibernation
    • ✓ Test all LLM tools work through new interface
  2. Edge Cases:

    • Session with agentMode: 'smart' → should become behaviorType: 'agentic'
    • Session with agentMode: 'deterministic' → should become behaviorType: 'phasic'
    • Session with neither field → should default appropriately

Summary

Must Fix Before Merge:

  1. State migration bug in stateMigration.ts:236-246 - CRITICAL

Should Fix (CLAUDE.md compliance):
2. Remove any type usage in stateMigration.ts:98

Optional Improvements:
3. Simplify type guard in AgenticProjectBuilder.ts:154-157
4. Review any usage in schemaFormatters.ts for necessity

Overall Assessment:
Excellent architectural refactoring with clean abstractions and proper separation of concerns. The critical state migration bug is straightforward to fix. Once addressed, this will significantly improve the codebase's testability and extensibility.


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.

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