can1357/oh-my-pi · error

${REWRITE_HEADER}${reference[1]} is valid only in REWRITE, n

Error message

${REWRITE_HEADER}${reference[1]} is valid only in REWRITE, never MATCH.

What it means

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.

Source

Thrown at packages/coding-agent/src/edit/sloppy.ts:1115

		const reference = patternReference(inline.patternText);
		if (reference) {
			throw new Error(
				`${REWRITE_HEADER}${reference[1]} is valid only inside an inline replacement or REWRITE, never MATCH.`,
			);
		}
		const operation: Operation = {
			patternText: relocateSelectionLines(inline.patternText),
			sourcePatternText,
			rewrite: { kind: "inline", replacements: inline.replacements },
			all,
		};
		if (inline.notes.length > 0) operation.recoveryNote = inline.notes.join("\n");
		return operation;
	}

	const reference = patternReference(sourcePatternText);
	if (reference) {
		throw new Error(`${REWRITE_HEADER}${reference[1]} is valid only in REWRITE, never MATCH.`);
	}
	if (rewriteText.trim() !== "") {
		const echoed = expandEchoedLineSelection(sourcePatternText, rewriteText);
		if (echoed !== undefined) {
			const operation: Operation = {
				patternText: echoed,
				sourcePatternText,
				rewrite: { kind: "explicit", text: rewriteText },
				all,
			};
			operation.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.`;
			return operation;
		}
	}
	return {
		patternText: sourcePatternText,
		sourcePatternText,
		rewrite: { kind: "explicit", text: rewriteText },

View on GitHub (pinned to 9690622007)

Solutions

  1. Move the »N reference from the pattern section into the REWRITE (final text) section
  2. Replace the reference in the pattern with the literal original text
  3. Split the operation so the pattern is plain text and only the replacement references »N

Example fix

// before
§ MATCH
old »2
// after
§ MATCH
old
»2
Defensive patterns

Strategy: validation

Validate before calling

if (/»\d+/.test(sourcePatternText)) {
  throw new Error('»N references belong in the REWRITE body, not the pattern');
}

Try / catch

try {
  applySloppy(content, input, ctx);
} catch (err) {
  if (err instanceof Error && err.message.includes('valid only in REWRITE')) {
    // move »N below the » separator and re-send
  } else throw err;
}

Prevention

When it happens

Trigger: createOperation parsing a MATCH-style operation where sourcePatternText (the pattern section) contains »N instead of the rewrite body containing it.

Common situations: 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.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/614e8f948eac1381. Report an issue: GitHub.