Skip to content
Open

Fix #664

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/patches/systemPrompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('systemPrompts.ts', () => {
expect(result.newContent).toBe('msg:"Say \\"Hello\\""');
});

it('should not double-escape already-escaped double quotes', async () => {
it('should escape backslashes before quotes to preserve literal backslash-quotes (#660)', async () => {
const mockPromptData = buildMockPromptData({
content: 'Say \\"Hello\\"',
regex: 'Say \\\\"Hello\\\\"',
Expand All @@ -211,7 +211,7 @@ describe('systemPrompts.ts', () => {

const result = await applySystemPrompts(cliContent, '1.0.0', false);

expect(result.newContent).toBe('msg:"Say \\"Hello\\""');
expect(result.newContent).toBe('msg:"Say \\\\\\"Hello\\\\\\""');
});

it('should auto-escape backticks in template literal context', async () => {
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('systemPrompts.ts', () => {
);
});

it('should not double-escape already-escaped backticks', async () => {
it('should escape backslashes before backticks to preserve literal backslash-backticks (#660)', async () => {
const mockPromptData = buildMockPromptData({
content: 'Use \\`foo\\` for config',
regex: 'Use \\\\`foo\\\\` for config',
Expand All @@ -287,7 +287,7 @@ describe('systemPrompts.ts', () => {

const result = await applySystemPrompts(cliContent, '1.0.0', false);

expect(result.newContent).toBe('desc:`Use \\`foo\\` for config`');
expect(result.newContent).toBe('desc:`Use \\\\\\`foo\\\\\\` for config`');
});

it('should auto-escape backticks adjacent to template expressions', async () => {
Expand All @@ -307,7 +307,7 @@ describe('systemPrompts.ts', () => {
expect(result.newContent).toBe('desc:`Value: \\`${x}\\``');
});

it('should auto-escape only unescaped backticks when mixed with escaped ones', async () => {
it('should escape backslashes in already-escaped backticks and also escape bare backticks (#660)', async () => {
const mockPromptData = buildMockPromptData({
content: 'Use \\`foo\\` and `bar` for config',
regex: 'Use \\\\`foo\\\\` and `bar` for config',
Expand All @@ -322,7 +322,7 @@ describe('systemPrompts.ts', () => {
const result = await applySystemPrompts(cliContent, '1.0.0', false);

expect(result.newContent).toBe(
'desc:`Use \\`foo\\` and \\`bar\\` for config`'
'desc:`Use \\\\\\`foo\\\\\\` and \\`bar\\` for config`'
);
});

Expand Down Expand Up @@ -406,7 +406,7 @@ describe('systemPrompts.ts', () => {
expect(result.newContent).toBe("msg:'It\\'s working'");
});

it('should not double-escape already-escaped single quotes', async () => {
it('should escape backslashes before single quotes to preserve literal backslash-quotes (#660)', async () => {
const mockPromptData = buildMockPromptData({
content: "It\\'s working",
regex: "It\\\\'s working",
Expand All @@ -420,7 +420,7 @@ describe('systemPrompts.ts', () => {

const result = await applySystemPrompts(cliContent, '1.0.0', false);

expect(result.newContent).toBe("msg:'It\\'s working'");
expect(result.newContent).toBe("msg:'It\\\\\\'s working'");
});

it('should set applied:true when auto-escape changes content even if char delta is 0', async () => {
Expand Down
11 changes: 9 additions & 2 deletions src/patches/systemPrompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ export const applySystemPrompts = async (

let replacementContent = interpolatedContent;

// Escape literal backslashes FIRST so they survive JS string
// embedding. Without this, a markdown `\"user\"` ends up as `"user"`
// because the backslash is consumed as an escape character. (#660)
if (delimiter === '"' || delimiter === "'" || delimiter === '`') {
replacementContent = replacementContent.replace(/\\/g, '\\\\');
}

if (delimiter === '"') {
replacementContent = replacementContent.replace(/\n/g, '\\n');
replacementContent = escapeUnescapedChar(replacementContent, '"');
Expand All @@ -157,7 +164,7 @@ export const applySystemPrompts = async (
replacementContent = escapeUnescapedChar(replacementContent, "'");
} else if (delimiter === '`') {
const { content: escaped, incomplete } =
escapeDepthZeroBackticks(interpolatedContent);
escapeDepthZeroBackticks(replacementContent);
if (incomplete) {
console.log(
chalk.red(
Expand All @@ -173,7 +180,7 @@ export const applySystemPrompts = async (
});
continue;
}
if (escaped !== interpolatedContent) {
if (escaped !== replacementContent) {
console.log(
chalk.yellow(`Auto-escaped unescaped backticks in "${prompt.name}"`)
);
Expand Down
Loading