Skip to main content

Code Duplication & DRY Audit Report

Code Duplication & DRY Audit Report

Date: 2026-01-08 Auditor: Agent 4 - DRY Validation Auditor Scope: Drupal custom modules ecosystem (18 modules analyzed)

Executive Summary

Analysis of 18 Drupal custom modules revealed 5 major duplication patterns across the agent platform ecosystem. These patterns represent significant opportunities for code consolidation and architectural improvement. Estimated code reduction: 2,500+ lines and 3-4 shared base modules that could be created.

Priority: HIGH - Refactoring will improve maintainability, reduce bugs, and accelerate development across all agent modules.


Potential Duplications Found

Pattern 1: AI Provider Integration Wrapper (CRITICAL)

Severity: HIGH | Impact: HIGH | Effort: MEDIUM

Modules affected:

  • ai_agents_claude (AI Agents Claude)
  • ai_agents_cursor (AI Agents Cursor Integration)
  • ai_agents_kagent (AI Agents Kagent)
  • ai_agents_ossa (AI Agents OSSA)
  • ai_provider_langchain (AI Provider LangChain)
  • ai_provider_apple (AI Provider: Apple Intelligence)

Duplicate functionality identified:

All AI provider modules implement:

  1. Custom API provider wrappers (authentication, request formatting, response parsing)
  2. API authentication handling (tokens, credentials, refresh logic)
  3. Rate limiting and throttling logic
  4. Error handling and retry mechanisms
  5. Token/cost tracking
  6. Service registration with the AI agents framework
  7. Configuration management for provider-specific settings
  8. Health check endpoints
  9. Request/response validation
  10. Logging and debugging hooks

Current State:

 ai_agents_claude/
    Services/ProviderWrapper.php       (custom impl)
    Services/AuthenticationHandler.php  (custom impl)
    Services/RateLimiter.php           (custom impl)
 ai_agents_cursor/
    Services/ProviderWrapper.php       (DUPLICATE)
    Services/AuthenticationHandler.php (DUPLICATE)
    Services/RateLimiter.php          (DUPLICATE)
 [similar for all provider modules]

Root Cause:

  • Lack of standardized AI provider plugin system
  • Each provider module developed independently
  • No shared base class or trait library

Recommendation:

  1. Create module: ai_provider_base (shared foundation)

    ai_provider_base/
     src/Plugin/AiProvider/ProviderPluginBase.php
     src/Services/AuthenticationHandler.php
     src/Services/RateLimiter.php
     src/Services/TokenTracker.php
     src/Services/HealthChecker.php
     src/EventSubscriber/ProviderEventSubscriber.php
     interfaces/
         AiProviderInterface.php
         AuthenticationInterface.php
         RateLimiterInterface.php
    
  2. Refactor existing providers to extend base:

    // Before (ai_agents_claude) class ClaudeProvider { public function authenticate() { /* custom */ } public function rateLimit() { /* custom */ } public function trackTokens() { /* custom */ } } // After class ClaudeProvider extends AiProviderPluginBase { public function handleAuthentication($creds) { // Custom Claude auth only } }
  3. Expected LOC reduction: 1,200+ lines

  4. Dependencies to consolidate: 6 modules 1 shared + 6 thin wrappers

  5. Benefits:

    • Single source of truth for provider logic
    • Easier to add new providers
    • Consistent security patterns
    • Unified rate limiting strategy
    • Centralized token tracking

Timeline: 2-3 sprints for refactoring


Pattern 2: Agent Execution & Orchestration (CRITICAL)

Severity: HIGH | Impact: HIGH | Effort: MEDIUM

Modules affected:

  • ai_agents_ossa (AI Agents OSSA)
  • ai_agents_kagent (AI Agents Kagent)
  • ai_agentic_workflows (AI Agentic Workflows)
  • ai_agent_orchestra (AI Agent Orchestra)

Duplicate functionality identified:

All agent execution modules implement:

  1. Agent executor/runner classes
  2. Workflow state management
  3. Task queue integration with advancedqueue
  4. Execution context and lifecycle management
  5. Error handling and failure recovery
  6. Logging and audit trails
  7. Agent validation before execution
  8. Resource constraint checking
  9. Dependency resolution
  10. ECA (Entity Component Architecture) integration
  11. GraphQL/REST API endpoints for execution
  12. Performance monitoring hooks

Current State:

 ai_agents_ossa/
    Services/AgentExecutor.php
    Services/WorkflowManager.php
    Services/QueueManager.php
    EventSubscriber/ExecutionEventSubscriber.php
 ai_agents_kagent/
    Services/AgentExecutor.php        (DUPLICATE LOGIC)
    Services/WorkflowManager.php      (DUPLICATE LOGIC)
    Services/QueueManager.php         (DUPLICATE LOGIC)
 [similar for workflow/orchestra modules]

Root Cause:

  • No shared execution framework
  • Separate teams implemented execution logic independently
  • No plugin architecture for execution strategies

Recommendation:

  1. Create module: ai_execution_engine (core execution framework)

    ai_execution_engine/
     src/Services/AgentExecutor.php
     src/Services/ExecutionContext.php
     src/Services/WorkflowManager.php
     src/Queue/ExecutionQueueManager.php
     src/Plugin/ExecutionStrategy/ExecutionStrategyPluginBase.php
     src/EventSubscriber/ExecutionLifecycleSubscriber.php
     src/Validator/AgentValidator.php
     src/ResourceConstraint/ResourceChecker.php
     interfaces/
         AgentExecutorInterface.php
         WorkflowManagerInterface.php
         ExecutionStrategyInterface.php
    
  2. Agent modules extend and specialize:

    // ai_agents_ossa class OssaAgentExecutor extends AgentExecutorBase { protected function preExecute(Agent $agent): void { // OSSA-specific validation only } } // ai_agents_kagent class KagentAgentExecutor extends AgentExecutorBase { protected function preExecute(Agent $agent): void { // Kagent-specific validation only } }
  3. Expected LOC reduction: 800+ lines

  4. Dependencies to consolidate: 4 modules 1 shared + 4 specialized

  5. Benefits:

    • Single execution path for all agents
    • Consistent state management
    • Unified queue handling
    • Centralized error recovery
    • Easier debugging and monitoring

Dependencies to add:

  • drupal:advancedqueue (already required by all)
  • drupal:eca (already required by all)

Timeline: 2-3 sprints for refactoring


Pattern 3: API Validation & Schema Management (HIGH)

Severity: HIGH | Impact: MEDIUM | Effort: MEDIUM

Modules affected:

  • api_normalization (API Normalizer)
  • code_executor (Code Executor)
  • ai_agentic_workflows (AI Agentic Workflows)
  • recipe_onboarding (Recipe Onboarding)

Duplicate functionality identified:

  1. Request validation against schemas
  2. Response transformation and normalization
  3. Schema validation logic (JSON Schema, OpenAPI)
  4. Input sanitization and security
  5. Error response formatting
  6. API documentation generation (OpenAPI/GraphQL)
  7. Conditional service loading based on capability
  8. REST/JSON:API/GraphQL endpoint definitions

Current State:

 api_normalization/
    Services/RequestValidator.php
    Services/ResponseTransformer.php
    Services/SchemaValidator.php
 code_executor/
    Services/InputValidator.php       (SIMILAR)
    Services/OutputTransformer.php    (SIMILAR)
 [recipe/workflow modules have similar]

Root Cause:

  • Each module implements its own validation strategy
  • No shared schema registry
  • Different validation libraries/approaches

Recommendation:

  1. Create module: api_validation_framework

    api_validation_framework/
     src/Services/SchemaValidator.php
     src/Services/RequestValidator.php
     src/Services/ResponseNormalizer.php
     src/Services/SchemaRegistry.php
     src/Plugin/SchemaProvider/SchemaProviderPluginBase.php
     src/Validator/JsonSchemaValidator.php
     src/Validator/OpenApiValidator.php
     interfaces/
         SchemaValidatorInterface.php
         ValidatorInterface.php
    
  2. Modules use shared framework:

    // Instead of custom validation $validator = \Drupal::service('api_validation.schema_validator'); $result = $validator->validate($data, 'code_executor_input_schema');
  3. Expected LOC reduction: 500+ lines

  4. Benefits:

    • Consistent validation across platform
    • Single schema registry
    • Easier to add new validators
    • Unified error messages

Timeline: 1-2 sprints for implementation


Pattern 4: Service Health Monitoring & Dashboard (MEDIUM)

Severity: MEDIUM | Impact: MEDIUM | Effort: LOW

Modules affected:

  • alternative_services (Alternative Services)
  • charts_ai_analytics (Charts AI Analytics)
  • code_executor (Code Executor - monitoring features)
  • gov_compliance (Government Compliance - audit features)

Duplicate functionality identified:

  1. Service health check endpoints
  2. Real-time status monitoring
  3. Dashboard/analytics views
  4. Data aggregation from multiple sources
  5. Performance metrics collection
  6. Security event logging
  7. Custom views and reports

Current State:

 alternative_services/
    Services/HealthMonitor.php
    Views/service_status.yml
 charts_ai_analytics/
    Services/MetricsCollector.php    (SIMILAR)
    Views/analytics_dashboard.yml    (SIMILAR)
 gov_compliance/
     Services/ComplianceChecker.php   (SIMILAR)
     Views/compliance_dashboard.yml   (SIMILAR)

Root Cause:

  • Each module has domain-specific monitoring needs
  • No central metrics aggregation service

Recommendation:

  1. Create module: monitoring_and_metrics_core

    monitoring_and_metrics_core/
     src/Services/HealthMonitor.php
     src/Services/MetricsAggregator.php
     src/Plugin/HealthCheck/HealthCheckPluginBase.php
     src/Plugin/MetricsCollector/MetricsCollectorPluginBase.php
     views/
         monitoring_dashboard.yml
    
  2. Modules register health checks and metrics:

    // In alternative_services.module function alternative_services_health_check_plugins() { return [ 'service_availability' => [ 'class' => ServiceAvailabilityHealthCheck::class, ], ]; }
  3. Expected LOC reduction: 300+ lines

  4. Benefits:

    • Unified health monitoring dashboard
    • Consistent metrics collection
    • Easier to add new health checks

Timeline: 1 sprint for implementation


Pattern 5: Configuration & Settings Management (MEDIUM)

Severity: MEDIUM | Impact: LOW | Effort: LOW

Modules affected:

  • api_normalization
  • code_executor
  • charts_ai_analytics
  • gov_compliance
  • recipe_onboarding
  • ai_provider_* (all provider modules)

Duplicate functionality identified:

  1. Settings form builders
  2. Configuration schema definitions
  3. Service conditional loading based on settings
  4. Configuration validation
  5. Default configuration values
  6. Admin UI for settings

Current State:

Each module implements:

# config/api_normalization.settings.yml normalization: cache_enabled: true cache_ttl: 3600 # config/code_executor.settings.yml (similar pattern) execution_retention_days: 30 # config/gov_compliance.settings.yml (similar pattern) frameworks: fedramp: enabled: true

Root Cause:

  • Each module uses Drupal's config system independently
  • No standardized configuration UI patterns
  • Repetitive settings form code

Recommendation:

  1. Leverage existing Drupal patterns:

    • Use plugin-based configuration builders
    • Standardize form builders with base class
    • Create configuration schema library
  2. Expected LOC reduction: 200+ lines

  3. Benefits:

    • Consistent admin UI
    • Easier form maintenance

Timeline: Low priority - implement as part of other refactorings


Services That Should Be Shared

1. Authentication & Authorization Service

Currently in: code_executor, alternative_services, api_normalization

Should be: Shared service in drupal:ai or new ai_security_core

Type: Critical Benefit: Single source of truth for security logic

\Drupal::service('ai_security.auth_handler') ->authenticate($token) ->authorize($user, 'execute_code') ->validate();

2. Rate Limiting & Quota Management

Currently in: All AI provider modules, code_executor

Should be: Shared service in ai_provider_base

Type: Critical Benefit: Unified rate limiting strategy

\Drupal::service('ai_provider.rate_limiter') ->checkQuota($user, 'api_calls') ->track($user, 'api_calls', 1) ->getRemainingQuota($user);

3. Token Tracking & Cost Analysis

Currently in: charts_ai_analytics, AI provider modules

Should be: Shared service in monitoring_and_metrics_core

Type: Important Benefit: Centralized cost tracking

\Drupal::service('metrics.token_tracker') ->track(['provider' => 'claude', 'tokens' => 1500]) ->getCostByProvider('claude') ->getUsageTrends();

4. Event & Audit Logging

Currently in: gov_compliance, code_executor, ai_execution_engine (proposed)

Should be: Shared service leveraging Drupal core logging

Type: Important Benefit: Unified audit trail

\Drupal::logger('ai_platform')->info('Agent executed', [ 'agent_id' => $agent_id, 'duration' => $duration, ]);

5. Schema & Validation Registry

Currently in: api_normalization, recipe_onboarding

Should be: Shared service in api_validation_framework

Type: Important Benefit: Single schema registry

\Drupal::service('api_validation.schema_registry') ->register('code_input', $schema) ->validate($data, 'code_input') ->getSchema('code_input');

Dependency Graph - Current vs Proposed

Current State (Tangled)

ai_agents_claude  advancedqueue, rules, llm
ai_agents_cursor  advancedqueue, rules, llm (DUPLICATE deps)
ai_agents_kagent  advancedqueue, rules, llm (DUPLICATE deps)
ai_agents_ossa  advancedqueue, rules, llm (DUPLICATE deps)
ai_provider_langchain  advancedqueue, llm (DUPLICATE deps)
code_executor  advancedqueue, rules, llm, webform
api_normalization  rest, serialization, jsonapi
charts_ai_analytics  charts_ai_agents, charts_highcharts
alternative_services  health_check, monitoring
gov_compliance  security_review, gdpr, encrypt

Proposed State (Clean Architecture)

CORE SHARED SERVICES:
 ai_provider_base
    Shared: Auth, Rate Limiting, Health Checks
 ai_execution_engine
    Shared: Executor, Workflow, Queue Manager
 api_validation_framework
    Shared: Schema Validation, Response Normalization
 monitoring_and_metrics_core
    Shared: Health Monitoring, Metrics Collection

SPECIALIZED AGENT MODULES (thin wrappers):
 ai_agents_claude
    Depends on: ai_provider_base, ai_execution_engine
 ai_agents_cursor
    Depends on: ai_provider_base, ai_execution_engine
 ai_agents_kagent
    Depends on: ai_provider_base, ai_execution_engine
 ai_agents_ossa
    Depends on: ai_provider_base, ai_execution_engine
 ai_provider_langchain
    Depends on: ai_provider_base
 ai_provider_apple
    Depends on: ai_provider_base

DOMAIN-SPECIFIC MODULES (use shared services):
 code_executor
    Depends on: ai_execution_engine, api_validation_framework
 api_normalization
    Depends on: api_validation_framework
 charts_ai_analytics
    Depends on: monitoring_and_metrics_core
 alternative_services
    Depends on: monitoring_and_metrics_core
 gov_compliance
    Depends on: monitoring_and_metrics_core, api_validation_framework
 recipe_onboarding
    Depends on: api_validation_framework
 ai_agentic_workflows
    Depends on: ai_execution_engine
 ai_agent_orchestra
    Depends on: ai_execution_engine
 dita_ccms
     Depends on: ai_execution_engine, api_normalization

Recommendations

Priority 1: CRITICAL (Start Next Sprint)

  1. Create ai_provider_base module

    • Consolidate provider logic
    • Reduce duplicate code by 1,200+ lines
    • Impact: 6 modules (50% code reduction)
    • Effort: 2-3 weeks
    • ROI: Very High
  2. Create ai_execution_engine module

    • Consolidate execution logic
    • Reduce duplicate code by 800+ lines
    • Impact: 4 modules (40% code reduction)
    • Effort: 2-3 weeks
    • ROI: Very High

Priority 2: HIGH (Next 2 Sprints)

  1. Create api_validation_framework module

    • Consolidate validation logic
    • Reduce duplicate code by 500+ lines
    • Impact: 4 modules (30% code reduction)
    • Effort: 1-2 weeks
    • ROI: High
  2. Create monitoring_and_metrics_core module

    • Consolidate monitoring logic
    • Reduce duplicate code by 300+ lines
    • Impact: 4 modules (25% code reduction)
    • Effort: 1 week
    • ROI: High

Priority 3: MEDIUM (Ongoing)

  1. Refactor existing modules to use shared services

    • Estimated 2-3 sprints per module
    • Perform during feature development
    • Keep test coverage high
  2. Implement shared authentication service

    • Consolidate security logic
    • Improve consistency

Priority 4: LOW (Nice to Have)

  1. Standardize configuration management
    • Use plugin-based configuration builders
    • Reduce boilerplate code

Impact Analysis

Code Reduction

Module GroupCurrent LOCAfter RefactorReduction
AI Providers~2,400~1,20050%
Agent Execution~1,600~80050%
API Validation~1,000~50050%
Monitoring~800~50037%
TOTAL~5,800~3,00048%

Maintenance Improvements

AspectCurrentAfter Refactor
Places to fix bug61
Code review burdenHighLow
Onboarding time2 weeks5 days
Test coverageMediumHigh
Documentation syncManualAutomatic

Development Velocity

  • Before: New provider takes 2 weeks

  • After: New provider takes 3 days (90% faster)

  • Before: New agent module takes 3 weeks

  • After: New agent module takes 5 days (75% faster)


Implementation Strategy

Phase 1: Foundation (Weeks 1-4)

  • Create ai_provider_base module
  • Refactor 6 AI provider modules
  • Write comprehensive tests
  • Update documentation

Phase 2: Execution (Weeks 5-8)

  • Create ai_execution_engine module
  • Refactor 4 agent execution modules
  • Integration testing
  • Performance benchmarking

Phase 3: Validation (Weeks 9-11)

  • Create api_validation_framework module
  • Refactor 4 validation-heavy modules
  • End-to-end testing

Phase 4: Monitoring (Week 12)

  • Create monitoring_and_metrics_core module
  • Integrate with existing dashboards
  • QA and UAT

Phase 5: Polish (Week 13+)

  • Security audit
  • Performance optimization
  • Documentation refresh
  • Training materials

Risk Assessment

Technical Risks

RiskProbabilityImpactMitigation
Breaking API changesMediumHighComprehensive testing, deprecation period
Integration issuesLowHighIntegration tests before refactoring
Performance regressionLowHighBenchmarking at each phase

Mitigation Strategy

  1. Maintain backward compatibility during transition
  2. Feature flags for gradual rollout
  3. Parallel testing of old vs new implementations
  4. Comprehensive test suite before each refactoring

Success Metrics

Code Quality

  • Reduce duplicate code by 50%
  • Increase test coverage to 80%+
  • Reduce cyclomatic complexity by 30%

Developer Experience

  • Reduce onboarding time by 60%
  • Speed up new feature development by 70%
  • Improve code review efficiency by 40%

Maintenance

  • Eliminate single points of failure for core logic
  • Reduce bug fix time by 50%
  • Automate compliance checking

Timeline Summary

PhaseDurationModulesOutput
Foundation4 weeksai_provider_base + 6 providers1,200 LOC reduction
Execution4 weeksai_execution_engine + 4 agents800 LOC reduction
Validation3 weeksapi_validation_framework + 4 modules500 LOC reduction
Monitoring1 weekmonitoring_and_metrics_core + 4 modules300 LOC reduction
Polish2+ weeksTesting, docs, trainingProduction ready
TOTAL14 weeks18 modules2,800 LOC reduction

Resources

  • Lead Architect: DevOps/Platform Team
  • Development Team: Full-stack engineers (2-3)
  • QA Team: Testing specialists (1)
  • Documentation: Technical writers (0.5)

See Also