fix(cli): match managed prompt files by exact path

This commit is contained in:
Tam Nhu Tran
2026-04-13 08:39:59 -04:00
parent 45c231a717
commit 7fc39f7c03
7 changed files with 135 additions and 57 deletions
@@ -66,4 +66,17 @@ describe('appendThirdPartyImageAnalysisToolArgs', () => {
);
expect(fileFlags.length).toBeGreaterThanOrEqual(2);
});
it('does not treat unrelated user prompt files as the managed CCS steering prompt', () => {
const result = appendThirdPartyImageAnalysisToolArgs([
'-p',
'describe',
'--append-system-prompt-file',
'/tmp/user-ccs-prompt-image-analysis-tool-notes.txt',
]);
const filePaths = result.filter((arg, index) => result[index - 1] === '--append-system-prompt-file');
expect(filePaths).toContain('/tmp/user-ccs-prompt-image-analysis-tool-notes.txt');
expect(filePaths.some((filePath) => filePath.endsWith('/ccs-prompt-image-analysis-tool.txt'))).toBe(true);
});
});
@@ -1,13 +1,36 @@
import { describe, expect, it } from 'bun:test';
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {
detectPromptInjectionMode,
buildInlineSteeringArg,
buildFileSteeringArg,
buildSteeringArg,
detectPromptInjectionMode,
getManagedPromptFilePath,
hasManagedPromptFileArg,
PROMPT_FLAG_INLINE,
PROMPT_FLAG_FILE,
} from '../../../src/utils/prompt-injection-strategy';
let originalCcsHome: string | undefined;
let tempHome: string;
beforeEach(() => {
originalCcsHome = process.env.CCS_HOME;
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-prompt-strategy-'));
process.env.CCS_HOME = tempHome;
});
afterEach(() => {
if (originalCcsHome === undefined) {
delete process.env.CCS_HOME;
} else {
process.env.CCS_HOME = originalCcsHome;
}
fs.rmSync(tempHome, { recursive: true, force: true });
});
describe('detectPromptInjectionMode', () => {
it('returns inline when no prompt flags present', () => {
expect(detectPromptInjectionMode(['-p', 'hello'])).toBe('inline');
@@ -48,23 +71,47 @@ describe('buildInlineSteeringArg', () => {
});
describe('buildFileSteeringArg', () => {
it('returns file flag and writes temp file', () => {
const result = buildFileSteeringArg({promptFileName: 'ccs-test-prompt.txt', promptContent: 'hello world' });
it('returns file flag and writes the prompt into the isolated CCS home', () => {
const result = buildFileSteeringArg({
promptFileName: 'ccs-test-prompt.txt',
promptContent: 'hello world',
});
expect(result[0]).toBe('--append-system-prompt-file');
expect(result[1]).toContain('ccs-test-prompt.txt');
expect(result[1]).toBe(path.join(tempHome, '.ccs', 'prompts', 'ccs-test-prompt.txt'));
expect(fs.readFileSync(result[1], 'utf8')).toBe('hello world');
});
});
describe('hasManagedPromptFileArg', () => {
it('returns true for the exact CCS-managed prompt path', () => {
expect(
hasManagedPromptFileArg({
args: [PROMPT_FLAG_FILE, getManagedPromptFilePath('ccs-test')],
promptName: 'ccs-test',
})
).toBe(true);
});
it('returns false for unrelated user files that only contain the prompt name', () => {
expect(
hasManagedPromptFileArg({
args: [PROMPT_FLAG_FILE, '/tmp/user-ccs-test-notes.txt'],
promptName: 'ccs-test',
})
).toBe(false);
});
});
describe('buildSteeringArg', () => {
it('delegates to inline in inline mode', () => {
expect(buildSteeringArg({
args: [PROMPT_FLAG_INLINE],
promptName: 'ignored.txt',
promptContent: 'hello',
})).toEqual([
'--append-system-prompt',
'hello',
]);
expect(
buildSteeringArg({
args: [PROMPT_FLAG_INLINE],
promptName: 'ignored.txt',
promptContent: 'hello',
})
).toEqual(['--append-system-prompt', 'hello']);
});
it('delegates to file in file mode', () => {
@@ -74,6 +121,6 @@ describe('buildSteeringArg', () => {
promptContent: 'hello',
});
expect(result[0]).toBe('--append-system-prompt-file');
expect(result[1]).toContain('ccs-test.txt');
expect(result[1]).toBe(getManagedPromptFilePath('ccs-test'));
});
});
@@ -157,4 +157,16 @@ describe('appendThirdPartyWebSearchToolArgs', () => {
expect(fileFlags.length).toBeGreaterThanOrEqual(2);
expect(result).not.toContain('--append-system-prompt');
});
it('does not treat unrelated user prompt files as the managed CCS steering prompt', () => {
const result = appendThirdPartyWebSearchToolArgs([
'smoke',
'--append-system-prompt-file',
'/tmp/user-ccs-prompt-websearch-tool-notes.txt',
]);
const filePaths = result.filter((arg, index) => result[index - 1] === '--append-system-prompt-file');
expect(filePaths).toContain('/tmp/user-ccs-prompt-websearch-tool-notes.txt');
expect(filePaths.some((filePath) => filePath.endsWith('/ccs-prompt-websearch-tool.txt'))).toBe(true);
});
});