{"record":{"id":"614e8f948eac1381","repo":"can1357/oh-my-pi","slug":"rewrite-header-reference-1-is-valid-only-in","errorCode":null,"errorMessage":"${REWRITE_HEADER}${reference[1]} is valid only in REWRITE, never MATCH.","messagePattern":"(.+?)(.+?) is valid only in REWRITE, never MATCH\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/edit/sloppy.ts","lineNumber":1115,"sourceCode":"\t\tconst reference = patternReference(inline.patternText);\n\t\tif (reference) {\n\t\t\tthrow new Error(\n\t\t\t\t`${REWRITE_HEADER}${reference[1]} is valid only inside an inline replacement or REWRITE, never MATCH.`,\n\t\t\t);\n\t\t}\n\t\tconst operation: Operation = {\n\t\t\tpatternText: relocateSelectionLines(inline.patternText),\n\t\t\tsourcePatternText,\n\t\t\trewrite: { kind: \"inline\", replacements: inline.replacements },\n\t\t\tall,\n\t\t};\n\t\tif (inline.notes.length > 0) operation.recoveryNote = inline.notes.join(\"\\n\");\n\t\treturn operation;\n\t}\n\n\tconst reference = patternReference(sourcePatternText);\n\tif (reference) {\n\t\tthrow new Error(`${REWRITE_HEADER}${reference[1]} is valid only in REWRITE, never MATCH.`);\n\t}\n\tif (rewriteText.trim() !== \"\") {\n\t\tconst echoed = expandEchoedLineSelection(sourcePatternText, rewriteText);\n\t\tif (echoed !== undefined) {\n\t\t\tconst operation: Operation = {\n\t\t\t\tpatternText: echoed,\n\t\t\t\tsourcePatternText,\n\t\t\t\trewrite: { kind: \"explicit\", text: rewriteText },\n\t\t\t\tall,\n\t\t\t};\n\t\t\toperation.recoveryNote = `Note: operation ${operationNumber}'s REWRITE restated the whole selection-bearing line, so the full line was replaced. A REWRITE after a bare selection replaces only the selected span; state just the span's new text, or select the whole line.`;\n\t\t\treturn operation;\n\t\t}\n\t}\n\treturn {\n\t\tpatternText: sourcePatternText,\n\t\tsourcePatternText,\n\t\trewrite: { kind: \"explicit\", text: rewriteText },","sourceCodeStart":1097,"sourceCodeEnd":1133,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/edit/sloppy.ts#L1097-L1133","documentation":"For non-inline (REWRITE/MATCH with explicit rewrite) operations, the source pattern itself must not contain a »N reference — such references are only allowed in the REWRITE body. A » in the source pattern text means the payload confuses the pattern with the replacement.","triggerScenarios":"createOperation parsing a MATCH-style operation where sourcePatternText (the pattern section) contains »N instead of the rewrite body containing it.","commonSituations":"An LLM places '»2' on the pattern side of the » separator instead of in the final-text side; misreading of the MATCH » REWRITE layout when handwriting the payload.","solutions":["Move the »N reference from the pattern section into the REWRITE (final text) section","Replace the reference in the pattern with the literal original text","Split the operation so the pattern is plain text and only the replacement references »N"],"exampleFix":"// before\n§ MATCH\nold »2\n// after\n§ MATCH\nold\n»2","handlingStrategy":"validation","validationCode":"if (/»\\d+/.test(sourcePatternText)) {\n  throw new Error('»N references belong in the REWRITE body, not the pattern');\n}","typeGuard":null,"tryCatchPattern":"try {\n  applySloppy(content, input, ctx);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('valid only in REWRITE')) {\n    // move »N below the » separator and re-send\n  } else throw err;\n}","preventionTips":["Keep pattern sections reference-free; put »N in the final-text section after the » header","Verify MATCH » REWRITE structure: pattern above, rewrite below","Never hand-write » on the pattern side unless escaping literal content"],"tags":["sloppy","rewrite-reference","payload-validation"],"backgroundTag":"misplaced-rewrite-reference","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}