Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

preserve literal dollar sequences in prompt arguments (#2128)

kt24519c308569
diff --git a/packages/coding-agent/.changes/eng-6014-literal-prompt-arguments.md b/packages/coding-agent/.changes/eng-6014-literal-prompt-arguments.md
new file mode 100644
index 0000000000..2ace40b860
--- /dev/null
+++ b/packages/coding-agent/.changes/eng-6014-literal-prompt-arguments.md
@@ -0,0 +1 @@
+- Fixed prompt templates altering literal dollar sequences and expanding placeholders inside user arguments.
diff --git a/packages/coding-agent/src/core/prompt-templates.ts b/packages/coding-agent/src/core/prompt-templates.ts
index bdabc9a107..afe72c1d39 100644
--- a/packages/coding-agent/src/core/prompt-templates.ts
+++ b/packages/coding-agent/src/core/prompt-templates.ts
@@ -71,39 +71,22 @@ export function parseCommandArgs(argsString: string): string[] {
* containing patterns like $1, $@, or $ARGUMENTS are NOT recursively substituted.
*/
export function substituteArgs(content: string, args: string[]): string {
- let result = content;
-
- // Replace $1, $2, etc. with positional args FIRST (before wildcards)
- // This prevents wildcard replacement values containing $<digit> patterns from being re-substituted
- result = result.replace(/\$(\d+)/g, (_, num) => {
- const index = parseInt(num, 10) - 1;
- return args[index] ?? "";
- });
-
- // Replace ${@:start} or ${@:start:length} with sliced args (bash-style)
- // Process BEFORE simple $@ to avoid conflicts
- result = result.replace(/\$\{@:(\d+)(?::(\d+))?\}/g, (_, startStr, lengthStr) => {
- let start = parseInt(startStr, 10) - 1; // Convert to 0-indexed (user provides 1-indexed)
- // Treat 0 as 1 (bash convention: args start at 1)
- if (start < 0) start = 0;
-
- if (lengthStr) {
- const length = parseInt(lengthStr, 10);
- return args.slice(start, start + length).join(" ");
- }
- return args.slice(start).join(" ");
- });
-
- // Pre-compute all args joined (optimization)
const allArgs = args.join(" ");
-
- // Replace $ARGUMENTS with all args joined (new syntax, aligns with Claude, Codex, OpenCode)
- result = result.replace(/\$ARGUMENTS/g, allArgs);
-
- // Replace $@ with all args joined (existing syntax)
- result = result.replace(/\$@/g, allArgs);
-
- return result;
+ return content.replace(
+ /\$(?:(\d+)|\{@:(\d+)(?::(\d+))?\}|ARGUMENTS|@)/g,
+ (_, num: string | undefined, startStr: string | undefined, lengthStr: string | undefined) => {
+ if (num !== undefined) {
+ return args[parseInt(num, 10) - 1] ?? "";
+ }
+ if (startStr !== undefined) {
+ // Slices are 1-indexed; zero also starts at the first argument.
+ const start = Math.max(0, parseInt(startStr, 10) - 1);
+ const end = lengthStr !== undefined ? start + parseInt(lengthStr, 10) : undefined;
+ return args.slice(start, end).join(" ");
+ }
+ return allArgs;
+ },
+ );
}
function loadTemplateFromFile(filePath: string, sourceInfo: SourceInfo): PromptTemplate | null {
diff --git a/packages/coding-agent/test/suite/regressions/6014-literal-prompt-arguments.test.ts b/packages/coding-agent/test/suite/regressions/6014-literal-prompt-arguments.test.ts
new file mode 100644
index 0000000000..f2b5d4d0ed
--- /dev/null
+++ b/packages/coding-agent/test/suite/regressions/6014-literal-prompt-arguments.test.ts
@@ -0,0 +1,80 @@
+import { writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { fauxAssistantMessage } from "@earendil-works/pi-ai";
+import { describe, expect, test } from "vitest";
+import {
+ expandPromptTemplate,
+ loadPromptTemplates,
+ type PromptTemplate,
+ substituteArgs,
+} from "../../../src/core/prompt-templates.js";
+import { createTestResourceLoader } from "../../utilities.js";
+import { createHarness, getMessageText, getUserTexts } from "../harness.js";
+
+describe("ENG-6014 literal prompt arguments", () => {
+ test.each(["$$", "$&", "$`", "$'", "$1", "$10", "$ARGUMENTS", "$@", `\${@:2}`, `\${@:1:2}`])(
+ "preserves %s in every placeholder form",
+ (literal) => {
+ const args = [literal, "tail"];
+ expect(substituteArgs("Before $1 after", args)).toBe(`Before ${literal} after`);
+ expect(substituteArgs("Before $ARGUMENTS after", args)).toBe(`Before ${literal} tail after`);
+ expect(substituteArgs("Before $@ after", args)).toBe(`Before ${literal} tail after`);
+ expect(substituteArgs(`Before \${@:1} after`, args)).toBe(`Before ${literal} tail after`);
+ expect(substituteArgs(`Before \${@:2:1} after`, ["head", literal, "tail"])).toBe(`Before ${literal} after`);
+ },
+ );
+
+ test("does not expand placeholders formed across insertion boundaries", () => {
+ expect(substituteArgs("$1ARGUMENTS|$1@|$1{@:2}", ["$", "tail"])).toBe(`$ARGUMENTS|$@|\${@:2}`);
+ });
+
+ test("expands mixed and repeated template placeholders without expanding inserted arguments", () => {
+ expect(substituteArgs(`$1|$2|\${@:2:1}|$@|$ARGUMENTS|$1`, ["$@", `\${@:1}`, "$ARGUMENTS"])).toBe(
+ `$@|\${@:1}|\${@:1}|$@ \${@:1} $ARGUMENTS|$@ \${@:1} $ARGUMENTS|$@`,
+ );
+ });
+
+ test.each([
+ { input: "$$", expected: "Explain: $$" },
+ { input: "$&", expected: "Explain: $&" },
+ { input: "$@ tail", expected: "Explain: $@ tail" },
+ { input: '"$`"', expected: "Explain: $`" },
+ { input: '"$\'"', expected: "Explain: $'" },
+ { input: "ordinary text", expected: "Explain: ordinary text" },
+ ])("preserves $input through a loaded template and the provider context", async ({ input, expected }) => {
+ const prompts: PromptTemplate[] = [];
+ const resourceLoader = createTestResourceLoader();
+ resourceLoader.getPrompts = () => ({ prompts, diagnostics: [] });
+ const harness = await createHarness({ resourceLoader, tools: [] });
+ try {
+ const filePath = join(harness.tempDir, "explain.md");
+ writeFileSync(filePath, "Explain: $ARGUMENTS");
+ prompts.push(
+ ...loadPromptTemplates({
+ cwd: harness.tempDir,
+ agentDir: harness.tempDir,
+ promptPaths: [filePath],
+ includeDefaults: false,
+ }),
+ );
+ expect(prompts).toHaveLength(1);
+ const command = `/explain ${input}`;
+ expect(expandPromptTemplate(command, prompts)).toBe(expected);
+ const providerTexts: string[] = [];
+ harness.setResponses([
+ (context) => {
+ providerTexts.push(...context.messages.filter((message) => message.role === "user").map(getMessageText));
+ return fauxAssistantMessage("ok");
+ },
+ ]);
+
+ await harness.session.prompt(command);
+
+ expect(getUserTexts(harness)).toEqual([expected]);
+ expect(providerTexts).toEqual([expected]);
+ expect(harness.faux.state.callCount).toBe(1);
+ } finally {
+ harness.cleanup();
+ }
+ });
+});