Ex-Fallows

refactor-prompt.json

Jun 3rd, 2026
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 16.34 KB | Source Code | 0 0
  1. === README.md ==============================================
  2.  
  3. # Route File Refactor Prompt Pack
  4.  
  5. A safe, step-by-step instruction set for splitting a large monolithic route file into smaller, organized domain modules — without changing how your code works.
  6.  
  7. ---
  8.  
  9. ## What is this?
  10.  
  11. When a backend grows over time, one route file ends up handling everything — authentication, payments, user data, API keys, admin tools. It becomes hard to read, hard to test, and risky to change.
  12.  
  13. This pack gives an AI agent (specifically Replit Agent) a precise set of instructions to split that file into smaller focused files, safely, one piece at a time.
  14.  
  15. **Nothing changes about how your code behaves.** Same routes, same logic, same error messages. Just better organized.
  16.  
  17. ---
  18.  
  19. ## What is in this pack?
  20.  
  21. | File | What it is |
  22. |---|---|
  23. | `README.md` | This file |
  24. | `refactor-prompt.json` | The instruction set you hand to the agent |
  25. | `REFACTOR_BASELINE.md` | Created automatically by the agent during the process — your safety record |
  26. | `REFACTOR_LOG.md` | Created automatically by the agent — a log of every step taken |
  27.  
  28. > The last two files do not exist yet. The agent creates them when it starts work.
  29.  
  30. ---
  31.  
  32. ## What does the agent actually do?
  33.  
  34. ### Before touching any code
  35. The agent asks you 8 setup questions — your file path, test command, type-check command, and a few safety checks. It will not start until you answer all of them.
  36.  
  37. ### Phase 0 — Verification
  38. The agent reads your route file independently and maps out everything:
  39. - Every route, its HTTP method, and its line number (saved to `REFACTOR_BASELINE.md`)
  40. - All shared helpers and middleware
  41. - Any closures that might cause problems when moved
  42. - Whether your webhook handler is safely located
  43. - Whether any security middleware has hidden dependencies
  44.  
  45. It then asks you to confirm the groupings before anything moves.
  46.  
  47. ### Phase 1 — The actual split
  48. Six steps, each requiring your approval before the next begins:
  49.  
  50. 1. Extract all shared helpers into `_middleware.ts`
  51. 2. Extract the simplest domain files first
  52. 3. Extract any feature-flag-gated blocks
  53. 4. Extract medium-complexity domains
  54. 5. Extract SSE/streaming routes last (highest risk, done when everything else is clean)
  55. 6. Create the final `index.ts` and remove the original file
  56.  
  57. ### After every single file change
  58. The agent runs:
  59. - Your test suite
  60. - Your type-checker
  61. - A route count diff against the baseline
  62.  
  63. If anything fails it restores the file immediately and stops. It does not try to fix errors on its own.
  64.  
  65. ---
  66.  
  67. ## How to use it
  68.  
  69. ### Option A — Paste directly into Replit Agent chat
  70. 1. Open `refactor-prompt.json`
  71. 2. Copy the entire contents
  72. 3. Paste into your Replit Agent chat
  73. 4. The agent will ask you the setup questions and begin
  74.  
  75. ### Option B — Save to your repo
  76. 1. Copy `refactor-prompt.json` into a `prompts/` folder in your repo
  77. 2. When ready to refactor, open the file and paste its contents into the agent chat
  78.  
  79. ---
  80.  
  81. ## Requirements
  82.  
  83. - Your project must have a working test suite before you start
  84. - Your working tree must be clean (no uncommitted changes) — changes are reverted via git, so a clean state is what makes rollback possible
  85. - You need to know your test command and type-check command
  86. - You need to know where your webhook handler lives (if you have one)
  87. - Designed for Express/TypeScript. The route-detection grep pattern is Express-specific but easy to adjust for Fastify, Hono, or Koa.
  88.  
  89. ---
  90.  
  91. ## What files can this be used for?
  92.  
  93. This pack was designed for route files but the same approach works for any large monolithic file. Common candidates:
  94.  
  95. - `server/agents.ts` — multi agent stage logic
  96. - `server/index.ts` — server setup mixed with middleware and webhooks
  97. - Any file that has grown beyond 1000 lines and handles multiple unrelated concerns
  98.  
  99. The JSON is roughly 80% reusable for other files. The Phase 0 questions and verification steps stay the same. Only the domain groupings change.
  100.  
  101. ---
  102.  
  103. ## What the agent will NOT do
  104.  
  105. - Edit any handler logic
  106. - Change status codes or error messages
  107. - Add or remove dependencies
  108. - Rename imports beyond correcting path depths
  109. - Fix forward if a test fails — it always restores and stops
  110.  
  111. ---
  112.  
  113. ## If something goes wrong
  114.  
  115. The agent writes every finding and every verification result to `REFACTOR_LOG.md` as it works. If a session ends unexpectedly, open that file to see exactly where things stopped and what state the repo is in.
  116.  
  117. ---
  118.  
  119. ## Glossary
  120.  
  121. | Term | Plain meaning |
  122. |---|---|
  123. | Refactor | Split or reorganize code without changing what it does |
  124. | Monolithic file | One big file doing too many things |
  125. | Domain module | A smaller file responsible for one area (e.g. auth, payments) |
  126. | Middleware | Code that runs before your route handler (e.g. checking if user is logged in) |
  127. | SSE | Server-Sent Events — a way of streaming live updates to the browser |
  128. | Registration order | The sequence routes are added to the server — order matters in Express |
  129. | Hermetic block | A section of code that is self-contained and does not depend on anything outside it |
  130. | Closure | A function that uses variables from the scope it was defined in |
  131.  
  132.  
  133. === refactor-prompt.json ============================================================
  134.  
  135. {
  136.   "name": "Monolithic Route File Refactor",
  137.   "version": "1.0",
  138.   "target_agent": "Replit Agent",
  139.   "description": "Safe incremental refactor of a monolithic route file into per-domain route modules. Zero behavior change.",
  140.  
  141.   "setup": {
  142.     "instruction": "Ask the user all of these questions before opening any files. Do not proceed until all are answered.",
  143.     "questions": [
  144.       "What is the path to the monolithic route file you want refactored?",
  145.       "What should the target routes directory be named?",
  146.       "What is your test command?",
  147.       "What is your type-check command?",
  148.       "Do you have an HTTP integration test or smoke test script? If yes, what is the command?",
  149.       "Where does your raw-body webhook handler live?",
  150.       "Are there any uncommitted changes or in-progress work in the repo right now? If yes, stop — do not proceed until the working tree is clean. A clean git state is required so that any step can be reverted.",
  151.       "Confirm you want me to stop and wait for your approval after every step before continuing."
  152.     ]
  153.   },
  154.  
  155.   "global_rules": [
  156.     "Do one file at a time. Do not open and edit multiple files simultaneously.",
  157.     "Run the full verification suite after every single file change.",
  158.     "If verification fails: restore the file to its previous state immediately. Do not attempt to fix the error. Write the failure to REFACTOR_LOG.md and stop. Wait for the user.",
  159.     "If you are unsure about anything, stop and ask. Do not make assumptions.",
  160.     "After completing each step, stop and wait for user approval before starting the next one.",
  161.     "Do not edit the package manifest.",
  162.     "Do not change any handler logic, status codes, error messages, or middleware chains.",
  163.     "Do not introduce a Router-per-file pattern unless the original already used one.",
  164.     "Do not use sub-mounting unless the original already did.",
  165.     "Import path depth corrections are permitted but the set of imported symbols must remain identical.",
  166.     "If any move requires touching handler internals to compile, stop and report the coupling. Do not edit logic."
  167.   ],
  168.  
  169.   "phase_0": {
  170.     "name": "Ground-Truth Verification",
  171.     "instruction": "Mandatory. Do before writing any code.",
  172.     "steps": [
  173.       {
  174.         "id": "0a",
  175.         "name": "Write baseline to file",
  176.         "action": "Run grep -n 'app.(get|post|put|patch|delete|use)(' against the route file. This pattern matches Express-style registration; adjust it for other frameworks (Fastify, Hono, Koa). Write full output to REFACTOR_BASELINE.md in the repo root. This is the source of truth for the entire refactor."
  177.       },
  178.       {
  179.         "id": "0b",
  180.         "name": "Verify structural facts",
  181.         "instruction": "Open the route file. Do not trust any line numbers the user gave. Verify everything yourself. Write each finding to REFACTOR_BASELINE.md under a clearly labelled section.",
  182.         "checks": [
  183.           {
  184.             "id": "a",
  185.             "name": "Entrypoint",
  186.             "action": "Identify the exported entrypoint function signature. Confirm routes are attached directly to the app object."
  187.           },
  188.           {
  189.             "id": "b",
  190.             "name": "Module-level helpers",
  191.             "action": "List everything defined outside the entrypoint function: limiters, schemas, middleware factories, utility functions, type declarations. Ask the user to confirm the list is complete before proceeding."
  192.           },
  193.           {
  194.             "id": "c",
  195.             "name": "In-function closures",
  196.             "action": "Grep for any functions or closures defined inside the entrypoint function. For each, identify exactly what variables it captures.",
  197.             "stop_condition": "If it captures anything scoped inside the entrypoint (not module-level imports), stop, write the finding to REFACTOR_BASELINE.md, and report to the user. Do not continue."
  198.           },
  199.           {
  200.             "id": "d",
  201.             "name": "Feature-flag-gated blocks",
  202.             "action": "Grep for any if (process.env.) blocks wrapping route registrations. For each, verify it does not reference variables defined outside that block but inside the entrypoint.",
  203.             "stop_condition": "If any external references exist, stop and report."
  204.           },
  205.           {
  206.             "id": "e",
  207.             "name": "Webhook handler",
  208.             "action": "Confirm there is no raw-body webhook handler in this file. Cross-check against the file the user named in setup.",
  209.             "stop_condition": "If you find a webhook here, stop immediately and report."
  210.           },
  211.           {
  212.             "id": "f",
  213.             "name": "Security middleware",
  214.             "action": "Ask the user to name every security-critical middleware function in the file. For each, verify it does not close over variables scoped inside the entrypoint.",
  215.             "stop_condition": "If any do, stop and report."
  216.           },
  217.           {
  218.             "id": "g",
  219.             "name": "Inline type declarations",
  220.             "action": "List every type and interface declared in the file, their line numbers, and which handlers reference them."
  221.           },
  222.           {
  223.             "id": "h",
  224.             "name": "Async registration",
  225.             "action": "Check whether any route registration setup code contains await expressions. Note which routes if found."
  226.           }
  227.         ]
  228.       },
  229.       {
  230.         "id": "0c",
  231.         "name": "Domain grouping",
  232.         "action": "Present the full route inventory to the user. Ask them to assign each route to a domain module file. Do not assume any groupings. Write the confirmed mapping to REFACTOR_BASELINE.md."
  233.       },
  234.       {
  235.         "id": "0d",
  236.         "name": "Registration order",
  237.         "action": "Ask the user to confirm the required middleware and route registration order. Write the confirmed order to REFACTOR_BASELINE.md. This order is load-bearing and must not change."
  238.       }
  239.     ],
  240.     "gate": "Do not proceed to Phase 1 until the user has reviewed and approved REFACTOR_BASELINE.md."
  241.   },
  242.  
  243.   "phase_1": {
  244.     "name": "Refactor Steps",
  245.     "steps": [
  246.       {
  247.         "id": "1",
  248.         "name": "Create _middleware.ts",
  249.         "actions": [
  250.           "Create _middleware.ts in the routes directory.",
  251.           "Move all module-level helpers, limiters, schemas, middleware factories, utility functions, and inline type declarations into it.",
  252.           "Lift any in-function closures confirmed in Phase 0 to capture only module-level imports — move them to module scope here.",
  253.           "Import everything back into the original route file so it compiles identically.",
  254.           "No routes move in this step."
  255.         ],
  256.         "gate": "Run verification. Write results to REFACTOR_LOG.md. Stop and wait for approval."
  257.       },
  258.       {
  259.         "id": "2",
  260.         "name": "Extract leaf domain modules",
  261.         "actions": [
  262.           "Extract the domains the user marked as lowest-risk in Step 0c.",
  263.           "One domain per approval cycle.",
  264.           "For each: create the domain file, move its routes, update import path depths, import the registrar into the main route file and call it at the exact same position.",
  265.           "Do not change any imported symbols."
  266.         ],
  267.         "gate": "Run verification after each domain. Write results to REFACTOR_LOG.md. Stop and wait for approval before the next domain."
  268.       },
  269.       {
  270.         "id": "3",
  271.         "name": "Extract hermetic feature-flag blocks",
  272.         "actions": [
  273.           "For each feature-flag-gated block confirmed hermetic in Phase 0:",
  274.           "Create its domain file.",
  275.           "Move the entire block contents into an exported registrar function.",
  276.           "In the main route file, call the registrar inside an identical guard at the identical position."
  277.         ],
  278.         "gate": "Run verification. Write results to REFACTOR_LOG.md. Stop and wait for approval."
  279.       },
  280.       {
  281.         "id": "4",
  282.         "name": "Extract intermediate-risk domain modules",
  283.         "actions": [
  284.           "Extract the domains the user marked as medium complexity.",
  285.           "Same process as Step 2, one domain per approval cycle."
  286.         ],
  287.         "gate": "Run verification after each domain. Write results to REFACTOR_LOG.md. Stop and wait for approval before the next domain."
  288.       },
  289.       {
  290.         "id": "5",
  291.         "name": "Extract SSE and streaming routes",
  292.         "actions": [
  293.           "Extract the domains the user identified as touching SSE or streaming.",
  294.           "These go last because all surrounding noise is now gone.",
  295.           "Same process as Step 2, one domain per approval cycle."
  296.         ],
  297.         "gate": "Run verification after each domain. Write results to REFACTOR_LOG.md. Stop and wait for approval before the next domain."
  298.       },
  299.       {
  300.         "id": "6",
  301.         "name": "Finalize index.ts",
  302.         "actions": [
  303.           "Do not rename the original route file.",
  304.           "Create a new file at <routes-directory>/index.ts.",
  305.           "Copy only the entrypoint function into it — it should contain only calls to each domain registrar in the confirmed registration order from REFACTOR_BASELINE.md.",
  306.           "Update whatever file imports the entrypoint to point to the new location.",
  307.           "Run verification.",
  308.           "Only if verification passes: delete the original route file.",
  309.           "Run verification again after deletion."
  310.         ],
  311.         "gate": "Write results to REFACTOR_LOG.md. Stop and wait for final approval."
  312.       }
  313.     ]
  314.   },
  315.  
  316.   "verification_protocol": {
  317.     "instruction": "Run all checks after every file change.",
  318.     "checks": [
  319.       {
  320.         "name": "Test suite",
  321.         "action": "Run the test command the user provided. All tests must pass.",
  322.         "report": "Pass/fail/skip counts."
  323.       },
  324.       {
  325.         "name": "Type-checker",
  326.         "action": "Run the type-check command the user provided.",
  327.         "report": "Must be zero errors."
  328.       },
  329.       {
  330.         "name": "Route inventory diff",
  331.         "action": "Grep the same pattern used in Step 0a across all files in the routes directory. Diff against REFACTOR_BASELINE.md.",
  332.         "report": "Must be identical: same paths, same count, same order."
  333.       },
  334.       {
  335.         "name": "Smoke test",
  336.         "action": "If the user provided a smoke test command in setup, run it.",
  337.         "report": "Pass or fail."
  338.       }
  339.     ],
  340.     "failure_protocol": "If any check fails: restore the changed file immediately. Write the error to REFACTOR_LOG.md. Stop. Report to the user. Do not proceed."
  341.   },
  342.  
  343.   "output_format": {
  344.     "instruction": "Use this format after every step.",
  345.     "fields": {
  346.       "step": "Number and name",
  347.       "files_changed": "List of created, modified, deleted files",
  348.       "test_result": "Pass/fail and counts",
  349.       "type_check_result": "Zero errors or N errors",
  350.       "route_inventory_diff": "Identical or differences listed",
  351.       "smoke_test": "Pass, fail, or not configured",
  352.       "blockers_or_findings": "None or details"
  353.     },
  354.     "closing_line": "Awaiting your approval to continue."
  355.   }
  356. }
  357.  
Advertisement
Add Comment
Please, Sign In to add comment