def train_cct_transfer_reset(bend_model, ground_model, trainloader, testloader,
                             cycles=50, batches_per_cycle=10,
                             lr=0.001, device='cuda',
                             entropy_threshold=0.27,
                             transfer_strength=0.1,   # how much bend -> ground on overfit
                             entropy_penalty_weight=0.5):
    """
    Cycles: number of meta-iterations (each cycle = batches_per_cycle batches)
    """
    bend_model.to(device)
    ground_model.to(device)
    ground_model.eval()
    
    optimizer = optim.Adam(bend_model.parameters(), lr=lr)
    criterion_ce = nn.CrossEntropyLoss()
    
    entropy_history = []
    test_acc_history = []
    transfer_cycles = []
    
    total_batches = 0
    for cycle in range(cycles):
        bend_model.train()
        cycle_entropy_sum = 0.0
        cycle_batches = 0
        
        # Run a small number of batches
        for batch_idx, (inputs, labels) in enumerate(trainloader):
            if batch_idx >= batches_per_cycle:
                break
            inputs, labels = inputs.to(device), labels.to(device)
            
            outputs = bend_model(inputs)
            loss_ce = criterion_ce(outputs, labels)
            
            # Entropy = divergence from ground
            with torch.no_grad():
                ground_out = ground_model(inputs)
                entropy = F.mse_loss(outputs, ground_out).item()
            
            loss = loss_ce + entropy_penalty_weight * entropy
            
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            
            cycle_entropy_sum += entropy
            cycle_batches += 1
            total_batches += 1
        
        avg_entropy = cycle_entropy_sum / cycle_batches
        entropy_history.append(avg_entropy)
        
        # ---- CCT Transfer & Reset ----
        if avg_entropy > entropy_threshold:
            # Transfer useful learning: ground ← (1 - α)*ground + α*bend
            with torch.no_grad():
                for g_param, b_param in zip(ground_model.parameters(), bend_model.parameters()):
                    g_param.data.mul_(1 - transfer_strength).add_(b_param.data, alpha=transfer_strength)
            # Reset bend to the improved ground
            bend_model.load_state_dict(copy.deepcopy(ground_model.state_dict()))
            # Also reset optimizer state (optional, but good to avoid momentum from bad trajectory)
            optimizer = optim.Adam(bend_model.parameters(), lr=lr)
            print(f"Cycle {cycle+1}: Entropy {avg_entropy:.3f} > {entropy_threshold} → TRANSFERRED (α={transfer_strength}) and RESET")
            transfer_cycles.append(cycle+1)
        else:
            # Optional: even when low entropy, slowly transfer (optional)
            # to keep ground improving gradually
            if avg_entropy < 0.15:  # relaxed state
                with torch.no_grad():
                    for g_param, b_param in zip(ground_model.parameters(), bend_model.parameters()):
                        g_param.data.mul_(0.99).add_(b_param.data, alpha=0.01)  # very slow consolidation
                print(f"Cycle {cycle+1}: Entropy {avg_entropy:.3f} < 0.15 → SLOW CONSOLIDATION")
        
        # Evaluate test accuracy after each cycle
        bend_model.eval()
        correct = 0
        total = 0
        with torch.no_grad():
            for inputs, labels in testloader:
                inputs, labels = inputs.to(device), labels.to(device)
                outputs = bend_model(inputs)
                _, predicted = torch.max(outputs, 1)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()
        acc = 100 * correct / total
        test_acc_history.append(acc)
        print(f"         Test Acc: {acc:.2f}%")
    
    return bend_model, ground_model, entropy_history, test_acc_history, transfer_cycles