🔐 Safe Refactor™️– 🛠️Re-structure without risk. No logic changed. No bugs added. Just cleaner code.

Tired of LLMs breaking your app while “helping”? Refactor Safe is the ultimate prompt that tells your AI exactly what to do: ✅ Keep logic identical ✅ Fix nothing, add nothing ✅ Just re-structure and tidy spaghetti ✅ Output a clean, 1:1 drop-in replacement No mystery rewrites. No sneaky bugs. Just the same code — but prettier, clearer, and easier to work with.

Prompt Text:

USER:       SOME CONTEXT: Ultra-deep thinking mode for rigorous, detail-oriented, multi-angle verification. Begin by outlining the task and decomposing the problem into subtasks. For each subtask, apply cross-checks and double-blind analysis. Scrutinise logic, assumptions, and conclusions. Independently validate every fact, inference, and conclusion against the original source-code—the single source of truth. Employ mathematical checks, web searches, logic-evaluation frameworks, and any other resources liberally for blind verification.

||your_code_to_be_re_factored_goes_here_in_user_prompt__paste_below_context__and_above_task||

      ---
      TASK:
      YOUR TASK: Re-structured Fidelity

      We start anew—re-structured fidelity is the goal:
      • Capture the logic and document it.  
      • Track functions, variables, structures, and other code-like entities, and document them.  
      • Maintain vigilance throughout refactoring to prevent complacency.

      Repeated attempts have proved challenging; your expertise is essential to reveal hidden nuances and list them.

      Refactor the code into precise, 1-to-1, drop-in module replacements. Before refactoring, determine the code flow to guide your work. Ensure exact 1-to-1 logic equivalence—verified, logic-checked, and re-flowed—while achieving zero side-effects and zero ambiguity.

      ### The Five Laws of Code Restructuring  
      **(Each law overrides those that follow)**  

      **Law 1 – Observable Fidelity**  
      Preserve the program’s externally observable behaviour—outputs, side-effects, timing quirks, log messages, and even seemingly harmless typos. Fidelity outranks every other concern. The original source-code is authoritative.  

      **Law 2 – Traceable Lineage**  
      Ensure every symbol, side-effect, and execution path in the original maps to an equally discoverable counterpart in the refactor. Demonstrate the ancestry of every byte via diffs, comments (`AI_CLARIFY`, `AI_BUG`, `AI_GOOD`, …), or tests.  

      **Law 3 – Incremental Clarity**  
      Once Laws 1 and 2 are satisfied, refactor to maximise human comprehensibility: aim for ≤ 50-line logical units (flexible when boilerplate inflates size). Use self-documenting names, linear control flow, idempotent helpers, and stub methods delegating to new modules. Treat all ambiguity as replication: flag unclear logic explicitly rather than altering it.  

      **Law 4 – Scoped Containment**  
      Encapsulate dependencies and maintain the existing public surface. Avoid introducing new globals, side-channels, or widening access modifiers. Break large pieces apart while keeping the public contract intact.  

      **Law 5 – Double-Blind Verification**  
      Subject every change to independent validation: static analysis, unit/integration tests, diff-driven audits, performance baselines, and (when possible) alternative implementations. Log discrepancies as `AI_BUG` for later resolution. Verification is continuous, automated, and thorough.

      ### Comment Tags  
      Prefix inline comments with one of:  
      * `AI_BUG` – Functional errors, logic mistakes, incorrect outputs (replicate the bug).  
      * `AI_CLARIFY` – Confusing logic or unclear intent (replicated but flagged).  
      * `AI_GOOD` – Confirmed as correct and clean.  
      * `AI_OTHER` – Noteworthy comment, free-form.

      Success is a 1-to-1 drop-in replacement that endures extensive refactoring and proves itself with cross-checked, zero-side-effect fidelity. Because this forms part of a much larger code-base, adhere strictly to the existing approach.

      Everything—including helpers—can and should be refactored. Replace existing function definitions with thin stubs that delegate to refactored versions when appropriate. If a function exceeds 50 lines, break it into coherent logical parts; few people can fully understand more than 50 consecutive lines. When uncertain, first craft a detailed plan to ensure the refactored code functions as a drop-in replacement.

      You may assume external functions exist and behave as implied by the original code.

      Include unchanged functions so the response is fully complete. Preserve the original artefact type: keep modules as modules, headers as headers, packages as packages, and inline code as inline.

      Track scope diligently. Confirm each variable remains in scope after restructuring; adapt calling code if argument-by-reference behaviour changes. Maintain pointer references and in-place mutations exactly as the authoritative source dictates.

      ### Hidden Dependencies  
      Preserve each log or print line verbatim, as hidden components may rely on precise content, order, or timing. Apply the same rigour to all observable interfaces.  

      ### Planning & Execution Phases  
      **A. PLANNING**  
      0 | Deep-refactor planning with detailed risk mini-plans  
      1 | Checklist & code-flow map  
      2 | Self-critique & risk sweep → crea