#!/usr/bin/env tsx
/**
 * Test to prove the concatenated execution ID bug
 */

import * as fs from 'fs/promises';
import * as path from 'path';

async function testConcatenatedBug() {
    const executionsDir = path.join('/Users/aneesh/Documents/HB/HumanBehaviorWorker/.pit', 'executions');
    
    try {
        const files = await fs.readdir(executionsDir);
        const executionFiles = files.filter(f => f.endsWith('.json'));
        
        const executions = [];
        for (const file of executionFiles) {
            try {
                const data = await fs.readFile(path.join(executionsDir, file), 'utf-8');
                const execution = JSON.parse(data);
                executions.push(execution);
            } catch (error) {
                console.error(`Error reading ${file}:`, error);
            }
        }
        
        // Filter for recent session analysis executions
        const sessionExecutions = executions.filter(e => 
            e.chain_group_id && 
            new Date(e.timestamp) > new Date('2025-08-12T09:00:00Z')
        );
        
        console.log(`Found ${sessionExecutions.length} recent session analysis executions`);
        
        // Collect all execution IDs and parent execution IDs
        const allExecutionIds = new Set(sessionExecutions.map(e => e.id));
        const allParentIds = sessionExecutions
            .map(e => e.parent_execution_id)
            .filter(Boolean);
        
        console.log(`Total execution IDs: ${allExecutionIds.size}`);
        console.log(`Total parent IDs referenced: ${allParentIds.length}`);
        
        // Find parent IDs that don't exist as execution IDs
        const orphanedParentIds = allParentIds.filter(parentId => !allExecutionIds.has(parentId));
        
        console.log(`Orphaned parent IDs (parents that don't exist): ${orphanedParentIds.length}`);
        
        if (orphanedParentIds.length > 0) {
            console.log('\n=== PROOF OF CONCATENATED EXECUTION BUG ===');
            console.log('The following parent execution IDs are referenced but do not exist as actual executions:');
            orphanedParentIds.forEach(id => {
                console.log(`  - ${id}`);
                // Find which executions reference this non-existent parent
                const children = sessionExecutions.filter(e => e.parent_execution_id === id);
                children.forEach(child => {
                    console.log(`    └── Referenced by: ${child.tag} [${child.id.slice(0, 8)}...]`);
                });
            });
            
            console.log('\n=== ANALYSIS ===');
            console.log('These orphaned parent IDs are likely from concatenated ModelResponse objects');
            console.log('that were created in-memory but never tracked as actual executions.');
        } else {
            console.log('No orphaned parent IDs found - all parent references exist as actual executions.');
        }
        
    } catch (error) {
        console.error('Error:', error);
    }
}

testConcatenatedBug().catch(console.error);