#!/usr/bin/env python3 """Fix brace structure around cmd_result == 9 dispatch.""" path = '/mnt/d/resonance-engine-active/cuda/khra_gixx_1024_v5_observer.cu' with open(path, 'r', encoding='utf-8') as f: content = f.read() # The problem: between cmd_result==8 and cmd_result==9 there's a stray '}' # that closes the chain prematurely. Then cmd_result==9 dispatch is missing # its own closing '}' before the ACK section. old = ''' health_check_pending = 1; } } else if (cmd_result == 9) { // perturb_stress -- launch kernel on current distribution total_stress_perts++; perturb_stress_kernel<<>>(d_f[current], pstress_cx, pstress_cy, pstress_sigma, pstress_strength); CUDA_CHECK(cudaDeviceSynchronize()); printf("[v5] perturb_stress #%llu applied at (%.1f,%.1f) sigma=%.1f str=%.4f\\n", total_stress_perts, pstress_cx, pstress_cy, pstress_sigma, pstress_strength); fflush(stdout); char pert_msg[512]; snprintf(pert_msg, sizeof(pert_msg), "{\\"perturb_id\\":%llu,\\"cycle\\":%d,\\"x\\":%.1f,\\"y\\":%.1f,\\"sigma\\":%.1f,\\"strength\\":%.4f}", total_stress_perts, cycle, pstress_cx, pstress_cy, pstress_sigma, pstress_strength); zmq_send_resilient(&ack_pub, zmq_ctx, "tcp://127.0.0.1:5559", ZMQ_PUB, 1, pert_msg, strlen(pert_msg), 0, &ack_fail_count); // v4: Publish ACK on port 5559 (for commands that didn't already send a response)''' new = ''' health_check_pending = 1; } else if (cmd_result == 9) { // perturb_stress -- launch kernel on current distribution total_stress_perts++; perturb_stress_kernel<<>>(d_f[current], pstress_cx, pstress_cy, pstress_sigma, pstress_strength); CUDA_CHECK(cudaDeviceSynchronize()); printf("[v5] perturb_stress #%llu applied at (%.1f,%.1f) sigma=%.1f str=%.4f\\n", total_stress_perts, pstress_cx, pstress_cy, pstress_sigma, pstress_strength); fflush(stdout); char pert_msg[512]; snprintf(pert_msg, sizeof(pert_msg), "{\\"perturb_id\\":%llu,\\"cycle\\":%d,\\"x\\":%.1f,\\"y\\":%.1f,\\"sigma\\":%.1f,\\"strength\\":%.4f}", total_stress_perts, cycle, pstress_cx, pstress_cy, pstress_sigma, pstress_strength); zmq_send_resilient(&ack_pub, zmq_ctx, "tcp://127.0.0.1:5559", ZMQ_PUB, 1, pert_msg, strlen(pert_msg), 0, &ack_fail_count); } // v4: Publish ACK on port 5559 (for commands that didn't already send a response)''' if old in content: content = content.replace(old, new) print("Replaced: removed stray brace, added closing brace for cmd_result==9") else: print("ERROR: pattern not found") # Try finding the lines directly lines = content.split('\n') for i, line in enumerate(lines): if 'health_check_pending = 1;' in line: print(f"Found at line {i+1}:") for j in range(i, min(i+5, len(lines))): print(f" {j+1}: {lines[j]}") if '} else if (cmd_result == 9) {' in line: print(f"Found cmd_result==9 at line {i+1}") exit(1) with open(path, 'w', encoding='utf-8') as f: f.write(content) # Verify with open(path, 'r', encoding='utf-8') as f: lines = f.readlines() for i, line in enumerate(lines): if 'health_check_pending = 1;' in line: print(f"After fix, lines around cmd_result==8:") for j in range(i, min(i+5, len(lines))): print(f" {j+1}: {lines[j].rstrip()}") if '} else if (cmd_result == 9) {' in line: print(f"cmd_result==9 at line {i+1}: {line.rstrip()}") # Show the closing brace for cmd_result==9 for j in range(i, min(i+20, len(lines))): if lines[j].strip() == '}' and j > i + 10: print(f" closing brace at line {j+1}") break