MAINFRAME TERMINAL #113

TYPES DOCUMENTATION

File: results.md

TECHNICAL BULLETIN NO. 005

RESULTS TYPES - OPERATION OUTCOME DEFINITION SYSTEM

DOCUMENT ID: task-o-matic-results-types-v1
CLEARANCE: All Personnel

MANDATORY COMPLIANCE: Yes

⚠️ CRITICAL SURVIVAL NOTICE

Citizen, ignore these result types and your operations become blind in the wasteland. Success and failure states merge into chaos, error handling becomes guesswork, and your API responses turn to radioactive static. This is your operation outcome foundation.

TYPE SYSTEM ARCHITECTURE

The results types system uses discriminated union patterns to create type-safe operation outcomes. It provides standardized result structures for all major operations while maintaining extensibility for custom data. The architecture supports:

  • Success/Failure States: Clear discriminated unions
  • Operation-Specific Results: Tailored result interfaces
  • Metrics Collection: Standardized performance and usage data
  • Error Context: Rich error information with suggestions
  • Metadata Support: Extensible metadata for debugging

This design enables predictable error handling while providing detailed operation feedback.

COMPLETE TYPE DOCUMENTATION

#### OperationResult Type

export type OperationResult<T = any> =
  | {
      success: true;
      data: T;
      stats?: Record<string, any>;
      metadata?: Record<string, any>;
    }
  | {
      success: false;
      error: string;
      stats?: Record<string, any>;
      metadata?: Record<string, any>;
    };

Purpose: Generic operation result using discriminated union pattern

Success Branch Properties:

  • success (Literal true): Discriminator for success state
  • data (Type T): Operation result data
  • stats (Optional, Record): Operation statistics
  • metadata (Optional, Record): Additional metadata

Failure Branch Properties:

  • success (Literal false): Discriminator for failure state
  • error (Required, string): Error message
  • stats (Optional, Record): Operation statistics even on failure
  • metadata (Optional, Record): Error context metadata

Usage Examples:

// Success result
const successResult: OperationResult<Task> = {
  success: true,
  data: createdTask,
  stats: { duration: 1500, tokensUsed: 250 }
};

// Failure result
const failureResult: OperationResult<Task> = {
  success: false,
  error: "Task creation failed: Invalid title",
  stats: { duration: 500, attempts: 3 }
};

// Type-safe usage
function handleResult(result: OperationResult<Task>): void {
  if (result.success) {
    console.log(`Success: ${result.data.title}`);
    console.log(`Stats: ${JSON.stringify(result.stats)}`);
  } else {
    console.error(`Error: ${result.error}`);
    console.log(`Stats: ${JSON.stringify(result.stats)}`);
  }
}

// Guaranteed type narrowing
function processData(result: OperationResult<string>): string {
  if (result.success) {
    // TypeScript knows result.data exists and is string
    return result.data.toUpperCase();
  } else {
    // TypeScript knows result.error exists and is string
    return `ERROR: ${result.error.toUpperCase()}`;
  }
}

#### CreateTaskResult Interface

export interface CreateTaskResult {
  success: true;
  task: Task;
  aiMetadata?: TaskAIMetadata;
}

Purpose: Result of task creation operation (always successful)

Properties:

  • success (Literal true): Always true for this operation
  • task (Required, Task): Created task object
  • aiMetadata (Optional, TaskAIMetadata): AI operation metadata

Usage Examples:

// Basic task creation result
const basicResult: CreateTaskResult = {
  success: true,
  task: {
    id: "task-123",
    title: "New task",
    status: "todo",
    createdAt: Date.now(),
    updatedAt: Date.now()
  }
};

// AI-enhanced task creation result
const aiResult: CreateTaskResult = {
  success: true,
  task: {
    id: "task-456",
    title: "AI-enhanced task",
    content: "Enhanced description with AI improvements",
    status: "todo",
    createdAt: Date.now(),
    updatedAt: Date.now()
  },
  aiMetadata: {
    taskId: "task-456",
    aiGenerated: true,
    aiPrompt: "Enhance task with better structure and details",
    confidence: 0.92,
    aiProvider: "anthropic",
    aiModel: "claude-3.5-sonnet",
    generatedAt: Date.now()
  }
};

// Handling in service layer
async function createTask(options: CreateTaskOptions): Promise<CreateTaskResult> {
  const task = await this.storage.createTask(options);
  
  if (options.aiEnhance) {
    const aiMetadata = await this.aiService.enhanceTask(task);
    return {
      success: true,
      task: aiMetadata.enhancedTask,
      aiMetadata: aiMetadata.metadata
    };
  }
  
  return {
    success: true,
    task
  };
}

#### EnhanceTaskResult Interface

export interface EnhanceTaskResult {
  success: true;
  task: Task;
  enhancedContent: string;
  stats: {
    originalLength: number;
    enhancedLength: number;
    duration: number;
    tokenUsage?: {
      prompt: number;
      completion: number;
      total: number;
    };
    timeToFirstToken?: number; // ms
    cost?: number; // USD
  };
  metadata: {
    aiProvider: string;
    aiModel: string;
    confidence: number;
  };
}

Purpose: Result of task enhancement operation with detailed metrics

Properties:

  • success (Literal true): Always true for this operation
  • task (Required, Task): Enhanced task object
  • enhancedContent (Required, string): AI-enhanced content
  • stats (Required, object): Operation statistics

- originalLength (Required, number): Original content length

- enhancedLength (Required, number): Enhanced content length

- duration (Required, number): Operation duration in milliseconds

- tokenUsage (Optional, object): AI token usage

- prompt (Required, number): Prompt tokens

- completion (Required, number): Completion tokens

- total (Required, number): Total tokens

- timeToFirstToken (Optional, number): Time to first token in ms

- cost (Optional, number): Estimated cost in USD

  • metadata (Required, object): AI operation metadata

- aiProvider (Required, string): AI provider used

- aiModel (Required, string): AI model used

- confidence (Required, number): AI confidence score

Usage Examples:

// Enhancement result with full metrics
const enhancementResult: EnhanceTaskResult = {
  success: true,
  task: enhancedTask,
  enhancedContent: "Enhanced task description with detailed requirements and acceptance criteria...",
  stats: {
    originalLength: 150,
    enhancedLength: 450,
    duration: 3200,
    tokenUsage: {
      prompt: 200,
      completion: 300,
      total: 500
    },
    timeToFirstToken: 800,
    cost: 0.015
  },
  metadata: {
    aiProvider: "anthropic",
    aiModel: "claude-3.5-sonnet",
    confidence: 0.89
  }
};

// Enhancement result without token tracking
const basicEnhancement: EnhanceTaskResult = {
  success: true,
  task: enhancedTask,
  enhancedContent: "Simple enhancement without detailed metrics",
  stats: {
    originalLength: 100,
    enhancedLength: 200,
    duration: 1500
  },
  metadata: {
    aiProvider: "openrouter",
    aiModel: "anthropic/claude-3.5-sonnet",
    confidence: 0.85
  }
};

// Service layer handling
async function enhanceTask(taskId: string): Promise<EnhanceTaskResult> {
  const startTime = Date.now();
  const task = await this.storage.getTask(taskId);
  
  if (!task) {
    throw new Error(`Task ${taskId} not found`);
  }
  
  const originalLength = task.content?.length || 0;
  const enhancedContent = await this.aiService.enhanceContent(task.content);
  const duration = Date.now() - startTime;
  
  const enhancedTask = {
    ...task,
    content: enhancedContent,
    updatedAt: Date.now()
  };
  
  await this.storage.updateTask(taskId, enhancedTask);
  
  return {
    success: true,
    task: enhancedTask,
    enhancedContent,
    stats: {
      originalLength,
      enhancedLength: enhancedContent.length,
      duration,
      tokenUsage: await this.getTokenUsage(),
      timeToFirstToken: await this.getTimeToFirstToken(),
      cost: await this.calculateCost()
    },
    metadata: {
      aiProvider: this.aiConfig.provider,
      aiModel: this.aiConfig.model,
      confidence: 0.92
    }
  };
}

#### SplitTaskResult Interface

export interface SplitTaskResult {
  success: true;
  task: Task;
  subtasks: Task[];
  stats: {
    subtasksCreated: number;
    duration: number;
    tokenUsage?: {
      prompt: number;
      completion: number;
      total: number;
    };
    timeToFirstToken?: number; // ms
    cost?: number; // USD
  };
  metadata: {
    aiProvider: string;
    aiModel: string;
    confidence?: number;
  };
}

Purpose: Result of task splitting operation with subtask details

Properties:

  • success (Literal true): Always true for this operation
  • task (Required, Task): Original task that was split
  • subtasks (Required, Task[]): Array of created subtasks
  • stats (Required, object): Operation statistics

- subtasksCreated (Required, number): Number of subtasks created

- duration (Required, number): Operation duration in milliseconds

- tokenUsage (Optional, Object): AI token usage

- timeToFirstToken (Optional, number): Time to first token in ms

- cost (Optional, number): Estimated cost in USD

  • metadata (Required, object): AI operation metadata

- aiProvider (Required, string): AI provider used

- aiModel (Required, string): AI model used

- confidence (Optional, number): AI confidence score

Usage Examples:

// Task splitting result
const splitResult: SplitTaskResult = {
  success: true,
  task: originalTask,
  subtasks: [
    {
      id: "task-457",
      title: "Design database schema",
      content: "Create user table with proper fields",
      parentId: "task-123",
      status: "todo",
      createdAt: Date.now(),
      updatedAt: Date.now()
    },
    {
      id: "task-458", 
      title: "Implement authentication endpoints",
      content: "Create login and registration API endpoints",
      parentId: "task-123",
      status: "todo",
      createdAt: Date.now(),
      updatedAt: Date.now()
    }
  ],
  stats: {
    subtasksCreated: 3,
    duration: 2800,
    tokenUsage: {
      prompt: 300,
      completion: 400,
      total: 700
    },
    timeToFirstToken: 1200,
    cost: 0.021
  },
  metadata: {
    aiProvider: "anthropic",
    aiModel: "claude-3.5-sonnet",
    confidence: 0.91
  }
};

// Service layer implementation
async function splitTask(taskId: string): Promise<SplitTaskResult> {
  const startTime = Date.now();
  const task = await this.storage.getTask(taskId);
  
  if (!task) {
    throw new Error(`Task ${taskId} not found`);
  }
  
  const subtasks = await this.aiService.splitTask(task);
  const duration = Date.now() - startTime;
  
  // Save subtasks
  for (const subtask of subtasks) {
    await this.storage.createTask({
      ...subtask,
      parentId: taskId
    });
  }
  
  return {
    success: true,
    task,
    subtasks,
    stats: {
      subtasksCreated: subtasks.length,
      duration,
      tokenUsage: await this.getTokenUsage(),
      timeToFirstToken: await this.getTimeToFirstToken(),
      cost: await this.calculateCost()
    },
    metadata: {
      aiProvider: this.aiConfig.provider,
      aiModel: this.aiConfig.model,
      confidence: 0.88
    }
  };
}

#### PlanTaskResult Interface

export interface PlanTaskResult {
  success: true;
  task: Task;
  plan: string;
  stats: {
    duration: number;
    tokenUsage?: {
      prompt: number;
      completion: number;
      total: number;
    };
    timeToFirstToken?: number; // ms
    cost?: number; // USD
  };
  metadata: {
    aiProvider: string;
    aiModel: string;
  };
}

Purpose: Result of task planning operation

Properties:

  • success (Literal true): Always true for this operation
  • task (Required, Task): Task for which plan was generated
  • plan (Required, string): Generated implementation plan
  • stats (Required, object): Operation statistics

- duration (Required, number): Operation duration in milliseconds

- tokenUsage (Optional, Object): AI token usage

- timeToFirstToken (Optional, number): Time to first token in ms

- cost (Optional, number): Estimated cost in USD

  • metadata (Required, object): AI operation metadata

- aiProvider (Required, string): AI provider used

- aiModel (Required, string): AI model used

Usage Examples:

// Task planning result
const planResult: PlanTaskResult = {
  success: true,
  task: originalTask,
  plan: `1. Setup project structure with TypeScript
2. Configure build tools (Webpack, Babel)
3. Create component architecture
4. Implement authentication system
5. Add database integration
6. Write comprehensive tests
7. Setup CI/CD pipeline

Estimated effort: 2-3 weeks
Key dependencies: TypeScript, Node.js, PostgreSQL`,
  stats: {
    duration: 3500,
    tokenUsage: {
      prompt: 400,
      completion: 600,
      total: 1000
    },
    timeToFirstToken: 1500,
    cost: 0.030
  },
  metadata: {
    aiProvider: "openrouter",
    aiModel: "anthropic/claude-3.5-sonnet"
  }
};

// Service layer implementation
async function planTask(taskId: string): Promise<PlanTaskResult> {
  const startTime = Date.now();
  const task = await this.storage.getTask(taskId);
  
  if (!task) {
    throw new Error(`Task ${taskId} not found`);
  }
  
  const plan = await this.aiService.generatePlan(task);
  const duration = Date.now() - startTime;
  
  // Update task with plan
  const updatedTask = {
    ...task,
    plan,
    updatedAt: Date.now()
  };
  
  await this.storage.updateTask(taskId, updatedTask);
  
  return {
    success: true,
    task: updatedTask,
    plan,
    stats: {
      duration,
      tokenUsage: await this.getTokenUsage(),
      timeToFirstToken: await this.getTimeToFirstToken(),
      cost: await this.calculateCost()
    },
    metadata: {
      aiProvider: this.aiConfig.provider,
      aiModel: this.aiConfig.model
    }
  };
}

#### DocumentTaskResult Interface

export interface DocumentTaskResult {
  success: true;
  task: Task;
  analysis?: DocumentationDetection;
  documentation?: any;
  stats: {
    duration: number;
  };
}

Purpose: Result of task documentation operation

Properties:

  • success (Literal true): Always true for this operation
  • task (Required, Task): Task that was documented
  • analysis (Optional, DocumentationDetection): Documentation analysis results
  • documentation (Optional, any): Generated documentation content
  • stats (Required, object): Operation statistics

- duration (Required, number): Operation duration in milliseconds

Usage Examples:

// Documentation result with analysis
const docResult: DocumentTaskResult = {
  success: true,
  task: documentedTask,
  analysis: {
    libraries: [
      { name: "react", context7Id: "/react", reason: "React components detected", searchQuery: "React hooks best practices" },
      { name: "typescript", context7Id: "/typescript", reason: "TypeScript usage detected", searchQuery: "TypeScript advanced types" }
    ],
    confidence: 0.92,
    toolResults: [
      { toolName: "context7-search", output: { docs: ["React documentation"] } }
    ],
    files: ["UserProfile.tsx", "useAuth.ts", "types.ts"]
  },
  documentation: {
    summary: "React authentication component with TypeScript",
    libraries: ["React", "TypeScript"],
    examples: [
      { code: "const UserProfile: React.FC<Props> = ({ user }) => { ... }", description: "Functional component pattern" }
    ],
    references: ["React Docs", "TypeScript Handbook"]
  },
  stats: {
    duration: 2200
  }
};

// Service layer implementation
async function documentTask(taskId: string): Promise<DocumentTaskResult> {
  const startTime = Date.now();
  const task = await this.storage.getTask(taskId);
  
  if (!task) {
    throw new Error(`Task ${taskId} not found`);
  }
  
  const analysis = await this.documentationService.analyzeTask(task);
  const documentation = await this.documentationService.generateDocumentation(task, analysis);
  const duration = Date.now() - startTime;
  
  // Update task with documentation
  const updatedTask = {
    ...task,
    documentation,
    updatedAt: Date.now()
  };
  
  await this.storage.updateTask(taskId, updatedTask);
  
  return {
    success: true,
    task: updatedTask,
    analysis,
    documentation,
    stats: {
      duration
    }
  };
}

#### DeleteTaskResult Interface

export interface DeleteTaskResult {
  success: true;
  deleted: Task[];
  orphanedSubtasks: Task[];
}

Purpose: Result of task deletion operation

Properties:

  • success (Literal true): Always true for this operation
  • deleted (Required, Task[]): Array of deleted tasks
  • orphanedSubtasks (Required, Task[]): Array of orphaned subtasks

Usage Examples:

// Simple task deletion
const deleteResult: DeleteTaskResult = {
  success: true,
  deleted: [deletedTask],
  orphanedSubtasks: []
};

// Cascade deletion with orphans
const cascadeResult: DeleteTaskResult = {
  success: true,
  deleted: [
    parentTask,
    childTask1,
    childTask2,
    grandchildTask1
  ],
  orphanedSubtasks: [
    {
      id: "orphan-1",
      title: "Orphaned subtask",
      content: "This subtask's parent was deleted",
      status: "todo",
      parentId: null, // Now orphaned
      createdAt: Date.now(),
      updatedAt: Date.now()
    }
  ]
};

// Service layer implementation
async function deleteTask(taskId: string, cascade: boolean = false): Promise<DeleteTaskResult> {
  const task = await this.storage.getTask(taskId);
  
  if (!task) {
    throw new Error(`Task ${taskId} not found`);
  }
  
  const deleted: Task[] = [task];
  let orphanedSubtasks: Task[] = [];
  
  if (cascade) {
    // Get all subtasks recursively
    const allDescendants = await this.getAllDescendants(taskId);
    
    // Delete all descendants
    for (const descendant of allDescendants) {
      await this.storage.deleteTask(descendant.id);
      deleted.push(descendant);
    }
    
    // Find orphaned subtasks (children of deleted tasks whose parent was also deleted)
    orphanedSubtasks = allDescendants.filter(descendant => 
      descendant.parentId && 
      !deleted.some(deleted => deleted.id === descendant.parentId)
    );
  }
  
  await this.storage.deleteTask(taskId);
  
  return {
    success: true,
    deleted,
    orphanedSubtasks
  };
}

#### PRDParseResult Interface

export interface PRDParseResult {
  success: true;
  prd: {
    overview: string;
    objectives: string[];
    features: string[];
  };
  tasks: Task[];
  stats: {
    tasksCreated: number;
    duration: number;
    aiProvider: string;
    aiModel: string;
    tokenUsage?: {
      prompt: number;
      completion: number;
      total: number;
    };
    timeToFirstToken?: number; // ms
    cost?: number; // USD
  };
  steps: {
    step: string;
    status: "completed" | "failed";
    duration: number;
    details?: any;
  }[];
}

Purpose: Result of PRD parsing operation with detailed breakdown

Properties:

  • success (Literal true): Always true for this operation
  • prd (Required, object): Parsed PRD structure

- overview (Required, string): PRD overview

- objectives (Required, string[]): Project objectives

- features (Required, string[]): Project features

  • tasks (Required, Task[]): Created tasks from PRD
  • stats (Required, object): Operation statistics

- tasksCreated (Required, number): Number of tasks created

- duration (Required, number): Total operation duration

- aiProvider (Required, string): AI provider used

- aiModel (Required, string): AI model used

- tokenUsage (Optional, Object): AI token usage

- timeToFirstToken (Optional, number): Time to first token in ms

- cost (Optional, number): Estimated cost in USD

  • steps (Required, array): Operation step breakdown

- step (Required, string): Step name

- status (Required, union): Step status

- duration (Required, number): Step duration

- details (Optional, any): Additional step details

Usage Examples:

// PRD parsing result
const prdResult: PRDParseResult = {
  success: true,
  prd: {
    overview: "E-commerce platform with user authentication, product catalog, and order management",
    objectives: [
      "Provide secure user authentication and authorization",
      "Enable product browsing and search functionality", 
      "Implement shopping cart and checkout process",
      "Support order tracking and user profiles"
    ],
    features: [
      "User registration and login",
      "Product catalog with categories",
      "Shopping cart functionality",
      "Order processing and payment integration",
      "User profile management",
      "Admin dashboard for inventory management"
    ]
  },
  tasks: [
    {
      id: "task-001",
      title: "Design user authentication system",
      content: "Create secure login, registration, and password reset functionality",
      status: "todo",
      effort: "large",
      createdAt: Date.now(),
      updatedAt: Date.now()
    },
    {
      id: "task-002",
      title: "Implement product catalog",
      content: "Build product listing, search, and categorization features",
      status: "todo",
      effort: "large",
      createdAt: Date.now(),
      updatedAt: Date.now()
    }
  ],
  stats: {
    tasksCreated: 8,
    duration: 4500,
    aiProvider: "anthropic",
    aiModel: "claude-3.5-sonnet",
    tokenUsage: {
      prompt: 1200,
      completion: 1800,
      total: 3000
    },
    timeToFirstToken: 2000,
    cost: 0.090
  },
  steps: [
    { step: "PRD analysis", status: "completed", duration: 800, details: { sections: 6, complexity: "high" } },
    { step: "Task extraction", status: "completed", duration: 1200, details: { tasks: 8, confidence: 0.92 } },
    { step: "Task creation", status: "completed", duration: 2500, details: { tasks: 8, storage: "file-system" } }
  ]
};

// Service layer implementation
async function parsePRD(prdContent: string): Promise<PRDParseResult> {
  const startTime = Date.now();
  const steps: any[] = [];
  
  try {
    // Step 1: Analyze PRD
    const analysisStart = Date.now();
    const analysis = await this.aiService.analyzePRD(prdContent);
    steps.push({
      step: "PRD analysis",
      status: "completed",
      duration: Date.now() - analysisStart,
      details: analysis
    });
    
    // Step 2: Extract tasks
    const extractionStart = Date.now();
    const tasksData = await this.aiService.extractTasks(prdContent, analysis);
    steps.push({
      step: "Task extraction", 
      status: "completed",
      duration: Date.now() - extractionStart,
      details: tasksData
    });
    
    // Step 3: Create tasks
    const creationStart = Date.now();
    const tasks = [];
    for (const taskData of tasksData.tasks) {
      const task = await this.storage.createTask({
        title: taskData.title,
        content: taskData.description,
        effort: taskData.effort,
        status: "todo"
      });
      tasks.push(task);
    }
    steps.push({
      step: "Task creation",
      status: "completed", 
      duration: Date.now() - creationStart,
      details: { tasks: tasks.length, storage: "file-system" }
    });
    
    const duration = Date.now() - startTime;
    
    return {
      success: true,
      prd: analysis.prd,
      tasks,
      stats: {
        tasksCreated: tasks.length,
        duration,
        aiProvider: this.aiConfig.provider,
        aiModel: this.aiConfig.model,
        tokenUsage: await this.getTotalTokenUsage(),
        timeToFirstToken: await this.getTimeToFirstToken(),
        cost: await this.calculateTotalCost()
      },
      steps
    };
    
  } catch (error) {
    steps.push({
      step: "PRD analysis",
      status: "failed",
      duration: Date.now() - startTime,
      details: { error: error.message }
    });
    
    throw error;
  }
}

FUNCTION DOCUMENTATION

No standalone functions in this module - this is a type definition file.

INTEGRATION PROTOCOLS

#### Service Layer Integration

// services/base-service.ts
import { 
  OperationResult, 
  CreateTaskResult, 
  EnhanceTaskResult,
  SplitTaskResult,
  PlanTaskResult,
  DocumentTaskResult,
  DeleteTaskResult,
  PRDParseResult
} from '../types/results';

export abstract class BaseService {
  protected abstract executeOperation<T>(
    operationName: string,
    operation: () => Promise<T>
  ): Promise<OperationResult<T>> {
    try {
      const result = await operation();
      
      return {
        success: true,
        data: result,
        stats: { duration: Date.now() - startTime }
      };
    } catch (error) {
      return {
        success: false,
        error: error.message,
        stats: { duration: Date.now() - startTime }
      };
    }
  }

  protected async executeTaskOperation<T>(
    operationName: string,
    operation: () => Promise<T>
  ): Promise<T> {
    const result = await this.executeOperation(operationName, operation);
    
    if (!result.success) {
      throw new Error(result.error);
    }
    
    return result.data;
  }
}

#### CLI Integration

// cli/result-handler.ts
import { 
  OperationResult,
  CreateTaskResult,
  EnhanceTaskResult
} from '../types/results';

export class ResultHandler {
  static handleCreateTask(result: CreateTaskResult): void {
    console.log(`✅ Created task: ${result.task.id}`);
    console.log(`📝 Title: ${result.task.title}`);
    
    if (result.aiMetadata) {
      console.log(`🤖 AI Enhanced: ${result.aiMetadata.aiProvider}/${result.aiMetadata.aiModel}`);
      console.log(`📊 Confidence: ${(result.aiMetadata.confidence * 100).toFixed(1)}%`);
    }
  }

  static handleEnhanceTask(result: EnhanceTaskResult): void {
    console.log(`✅ Enhanced task: ${result.task.id}`);
    console.log(`📊 Content growth: ${result.stats.originalLength}${result.stats.enhancedLength} characters`);
    console.log(`⏱️ Duration: ${result.stats.duration}ms`);
    
    if (result.stats.tokenUsage) {
      console.log(`🪙 Tokens: ${result.stats.tokenUsage.total} (${result.stats.tokenUsage.prompt} prompt + ${result.stats.tokenUsage.completion} completion)`);
    }
    
    if (result.stats.cost) {
      console.log(`💰 Cost: $${result.stats.cost.toFixed(4)}`);
    }
    
    console.log(`🤖 Provider: ${result.metadata.aiProvider}/${result.metadata.aiModel}`);
  }

  static handleOperationResult<T>(result: OperationResult<T>): void {
    if (result.success) {
      console.log(`✅ Operation completed successfully`);
      if (result.stats) {
        console.log(`📊 Stats: ${JSON.stringify(result.stats)}`);
      }
    } else {
      console.log(`❌ Operation failed: ${result.error}`);
      if (result.stats) {
        console.log(`📊 Stats: ${JSON.stringify(result.stats)}`);
      }
    }
  }

  static formatStats(stats: any): string {
    const parts: string[] = [];
    
    if (stats.duration) {
      parts.push(`Duration: ${stats.duration}ms`);
    }
    
    if (stats.tokenUsage) {
      parts.push(`Tokens: ${stats.tokenUsage.total}`);
    }
    
    if (stats.cost) {
      parts.push(`Cost: $${stats.cost.toFixed(4)}`);
    }
    
    return parts.join(' | ');
  }
}

#### Error Handling Integration

// utils/error-handler.ts
import { OperationResult } from '../types/results';

export class ErrorHandler {
  static wrapOperation<T>(
    operation: () => Promise<T>
  ): Promise<OperationResult<T>> {
    try {
      const result = await operation();
      
      return {
        success: true,
        data: result,
        stats: { timestamp: Date.now() }
      };
    } catch (error) {
      const errorResult: OperationResult<T> = {
        success: false,
        error: error.message,
        stats: { 
          timestamp: Date.now(),
          errorType: error.constructor.name,
          stackTrace: error.stack
        }
      };
      
      // Log error for debugging
      console.error('Operation failed:', errorResult);
      
      return errorResult;
    }
  }

  static isSuccess<T>(result: OperationResult<T>): result is { success: true; data: T } {
    return result.success === true;
  }

  static isFailure<T>(result: OperationResult<T>): result is { success: false; error: string } {
    return result.success === false;
  }

  static getData<T>(result: OperationResult<T>): T | null {
    return result.success ? result.data : null;
  }

  static getError<T>(result: OperationResult<T>): string | null {
    return result.success ? null : result.error;
  }
}

SURVIVAL SCENARIOS

#### Scenario 1: Complete Task Operation Pipeline

// Complete task management with result handling
class TaskOperationPipeline {
  private resultHandler: ResultHandler;
  private metrics: MetricsCollector;

  constructor() {
    this.resultHandler = new ResultHandler();
    this.metrics = new MetricsCollector();
  }

  async executeFullPipeline(taskId: string): Promise<void> {
    console.log(`🚀 Starting full pipeline for task: ${taskId}`);

    try {
      // Step 1: Enhance task
      console.log('\n📝 Step 1: Enhancing task...');
      const enhanceResult = await this.taskService.enhanceTask(taskId);
      this.resultHandler.handleEnhanceTask(enhanceResult);
      this.metrics.recordEnhancement(enhanceResult.stats);

      // Step 2: Split task if large
      const task = await this.taskService.getTask(taskId);
      if (task?.estimatedEffort === 'large') {
        console.log('\n🔧 Step 2: Splitting large task...');
        const splitResult = await this.taskService.splitTask(taskId);
        this.resultHandler.handleSplitTask(splitResult);
        this.metrics.recordSplitting(splitResult.stats);
      }

      // Step 3: Generate plan
      console.log('\n📋 Step 3: Generating implementation plan...');
      const planResult = await this.taskService.planTask(taskId);
      this.resultHandler.handlePlanTask(planResult);
      this.metrics.recordPlanning(planResult.stats);

      // Step 4: Generate documentation
      console.log('\n📚 Step 4: Generating documentation...');
      const docResult = await this.taskService.documentTask(taskId);
      this.resultHandler.handleDocumentTask(docResult);
      this.metrics.recordDocumentation(docResult.stats);

      // Final summary
      this.printPipelineSummary();

    } catch (error) {
      console.error(`❌ Pipeline failed: ${error.message}`);
      this.metrics.recordError(error);
    }
  }

  private printPipelineSummary(): void {
    console.log('\n' + '='.repeat(60));
    console.log('📊 PIPELINE SUMMARY');
    console.log('='.repeat(60));

    const summary = this.metrics.getSummary();
    for (const [operation, stats] of Object.entries(summary)) {
      console.log(`${operation}:`);
      console.log(`  Duration: ${stats.totalDuration}ms`);
      console.log(`  Success Rate: ${stats.successRate}%`);
      console.log(`  Average Duration: ${stats.averageDuration}ms`);
      
      if (stats.totalCost) {
        console.log(`  Total Cost: $${stats.totalCost.toFixed(4)}`);
      }
      
      if (stats.totalTokens) {
        console.log(`  Total Tokens: ${stats.totalTokens}`);
      }
    }
  }
}

// Metrics collector for tracking operations
class MetricsCollector {
  private operations: Map<string, any[]> = new Map();

  recordEnhancement(stats: EnhanceTaskResult['stats']): void {
    this.operations.set('enhancement', [...(this.operations.get('enhancement') || []), stats]);
  }

  recordSplitting(stats: SplitTaskResult['stats']): void {
    this.operations.set('splitting', [...(this.operations.get('splitting') || []), stats]);
  }

  recordPlanning(stats: PlanTaskResult['stats']): void {
    this.operations.set('planning', [...(this.operations.get('planning') || []), stats]);
  }

  recordDocumentation(stats: DocumentTaskResult['stats']): void {
    this.operations.set('documentation', [...(this.operations.get('documentation') || []), stats]);
  }

  recordError(error: Error): void {
    console.error('Pipeline error recorded:', error);
  }

  getSummary(): Record<string, any> {
    const summary: Record<string, any> = {};

    for (const [operation, stats] of this.operations.entries()) {
      const totalDuration = stats.reduce((sum, stat) => sum + stat.duration, 0);
      const successCount = stats.length;
      const averageDuration = totalDuration / successCount;
      
      summary[operation] = {
        totalDuration,
        successRate: 100, // All operations in this pipeline succeed
        averageDuration,
        totalCost: stats.reduce((sum, stat) => sum + (stat.cost || 0), 0),
        totalTokens: stats.reduce((sum, stat) => sum + (stat.tokenUsage?.total || 0), 0)
      };
    }

    return summary;
  }
}

#### Scenario 2: PRD Processing with Detailed Results

// PRD processing with comprehensive result tracking
class PRDProcessor {
  private resultHandler: ResultHandler;

  constructor(resultHandler: ResultHandler) {
    this.resultHandler = resultHandler;
  }

  async processPRD(prdPath: string): Promise<void> {
    console.log(`📄 Processing PRD: ${prdPath}`);

    try {
      // Read PRD content
      const prdContent = await fs.readFile(prdPath, 'utf-8');
      console.log(`📝 PRD content: ${prdContent.length} characters`);

      // Parse PRD
      console.log('\n🔍 Parsing PRD...');
      const parseResult = await this.prdService.parsePRD(prdContent);
      this.resultHandler.handlePRDParseResult(parseResult);

      if (parseResult.success) {
        console.log(`✅ Parsed ${parseResult.tasks.length} tasks from PRD`);
        
        // Display PRD structure
        console.log('\n📋 PRD Structure:');
        console.log(`Overview: ${parseResult.prd.overview}`);
        console.log(`Objectives: ${parseResult.prd.objectives.join(', ')}`);
        console.log(`Features: ${parseResult.prd.features.join(', ')}`);

        // Display created tasks
        console.log('\n📋 Created Tasks:');
        for (const task of parseResult.tasks) {
          console.log(`  ${task.id}: ${task.title} (${task.effort})`);
          if (task.content) {
            console.log(`    ${task.content.substring(0, 100)}${task.content.length > 100 ? '...' : ''}`);
          }
        }

        // Display operation steps
        console.log('\n⚙️ Operation Steps:');
        for (const step of parseResult.steps) {
          const status = step.status === 'completed' ? '' : '';
          console.log(`  ${status} ${step.step} (${step.duration}ms)`);
          if (step.details) {
            console.log(`    Details: ${JSON.stringify(step.details)}`);
          }
        }

        // Display statistics
        console.log('\n📊 Statistics:');
        console.log(`  Tasks Created: ${parseResult.stats.tasksCreated}`);
        console.log(`  Duration: ${parseResult.stats.duration}ms`);
        console.log(`  Provider: ${parseResult.stats.aiProvider}/${parseResult.stats.aiModel}`);
        
        if (parseResult.stats.tokenUsage) {
          console.log(`  Tokens: ${parseResult.stats.tokenUsage.total} (${parseResult.stats.tokenUsage.prompt} + ${parseResult.stats.tokenUsage.completion})`);
        }
        
        if (parseResult.stats.cost) {
          console.log(`  Cost: $${parseResult.stats.cost.toFixed(4)}`);
        }
      }

    } catch (error) {
      console.error(`❌ PRD processing failed: ${error.message}`);
    }
  }

  async processMultiplePRDs(prdPaths: string[]): Promise<void> {
    console.log(`📁 Processing ${prdPaths.length} PRDs...`);

    const results = [];
    for (const prdPath of prdPaths) {
      try {
        const result = await this.processSinglePRD(prdPath);
        results.push(result);
      } catch (error) {
        console.error(`❌ Failed to process ${prdPath}: ${error.message}`);
        results.push({ success: false, error: error.message });
      }
    }

    // Summary
    const successful = results.filter(r => r.success).length;
    const failed = results.length - successful;
    
    console.log('\n' + '='.repeat(60));
    console.log(`📊 BATCH PROCESSING SUMMARY`);
    console.log('='.repeat(60));
    console.log(`Total PRDs: ${prdPaths.length}`);
    console.log(`Successful: ${successful}`);
    console.log(`Failed: ${failed}`);
    console.log(`Success Rate: ${((successful / prdPaths.length) * 100).toFixed(1)}%`);
  }
}

#### Scenario 3: Error Recovery and Retry Logic

// Advanced error handling with result analysis
class RobustOperationExecutor {
  private maxRetries: number = 3;
  private baseDelay: number = 1000;

  async executeWithRetry<T>(
    operation: () => Promise<T>,
    context: string
  ): Promise<OperationResult<T>> {
    let lastError: Error | null = null;
    
    for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
      try {
        console.log(`🔄 Attempt ${attempt}/${this.maxRetries} for ${context}...`);
        
        const result = await operation();
        
        if (result.success) {
          console.log(`✅ Success on attempt ${attempt} for ${context}`);
          return result;
        }
        
        // Operation failed, prepare for retry
        lastError = new Error(result.error);
        
      } catch (error) {
        lastError = error;
        console.log(`❌ Error on attempt ${attempt} for ${context}: ${error.message}`);
      }

      // Wait before retry (exponential backoff)
      if (attempt < this.maxRetries) {
        const delay = this.baseDelay * Math.pow(2, attempt - 1);
        console.log(`⏳ Waiting ${delay}ms before retry...`);
        await new Promise(resolve => setTimeout(resolve, delay));
      }
    }

    // All retries exhausted
    const finalError = lastError || new Error('Unknown error');
    console.error(`💀 All retries exhausted for ${context}: ${finalError.message}`);
    
    return {
      success: false,
      error: finalError.message,
      stats: {
        attempts: this.maxRetries,
        totalDuration: this.baseDelay * (Math.pow(2, this.maxRetries) - 1), // Approximate total time
        errorType: finalError.constructor.name,
        lastError: finalError.message
      }
    };
  }

  async executeTaskWithCircuitBreaker<T>(
    operation: () => Promise<T>,
    context: string,
    circuitBreakerThreshold: number = 5
  ): Promise<OperationResult<T>> {
    const failureCount = this.getRecentFailureCount(context);
    
    if (failureCount >= circuitBreakerThreshold) {
      console.log(`🔌 Circuit breaker open for ${context} (${failureCount} recent failures)`);
      
      return {
        success: false,
        error: `Circuit breaker open: ${failureCount} recent failures`,
        stats: {
          circuitBreakerOpen: true,
          recentFailures: failureCount
        }
      };
    }

    try {
      const result = await operation();
      
      if (result.success) {
        this.resetFailureCount(context);
        console.log(`✅ Success for ${context} (circuit breaker reset)`);
      } else {
        this.incrementFailureCount(context);
      }
      
      return result;
      
    } catch (error) {
      this.incrementFailureCount(context);
      
      return {
        success: false,
        error: error.message,
        stats: {
          circuitBreakerOpen: false,
          recentFailures: this.getRecentFailureCount(context) + 1
        }
      };
    }
  }

  private getRecentFailureCount(context: string): number {
    const key = `failure_count_${context}`;
    return parseInt(localStorage.getItem(key) || '0', 10);
  }

  private incrementFailureCount(context: string): void {
    const key = `failure_count_${context}`;
    const current = parseInt(localStorage.getItem(key) || '0', 10);
    localStorage.setItem(key, String(current + 1));
  }

  private resetFailureCount(context: string): void {
    const key = `failure_count_${context}`;
    localStorage.setItem(key, '0');
  }
}

TECHNICAL SPECIFICATIONS

#### Result Type Guarantees

  • Type Safety: Discriminated unions prevent invalid states
  • Exhaustive Checking: TypeScript ensures all properties handled
  • Generic Support: Flexible data typing for any operation
  • Metadata Extensibility: Optional additional properties supported

#### Performance Characteristics

  • Memory Efficiency: Minimal object overhead
  • Type Checking: Compile-time validation
  • Serialization: JSON-friendly structure
  • Error Handling: Consistent error patterns

#### Integration Patterns

  • Service Layer: Standardized result interfaces
  • CLI Layer: User-friendly result formatting
  • Error Boundaries: Clear error propagation
  • Metrics Collection: Consistent performance tracking

#### Error Recovery Strategies

  • Retry Logic: Exponential backoff with max attempts
  • Circuit Breaking: Failure threshold detection
  • Graceful Degradation: Fallback operation modes
  • Context Preservation: Error context maintenance

Remember: Citizen, in the wasteland of operations, well-defined result types are your compass. Every discriminated union is a path through the fog of uncertainty, and every metadata field is a beacon of debugging light. Without them, your operations become lost in the radioactive dust of ambiguous outcomes.