Skip to content

Commit 62352a7

Browse files
committed
fix: instructions save
1 parent 146a61b commit 62352a7

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

codex-cli/src/utils/config.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export const PRETTY_PRINT = Boolean(process.env["PRETTY_PRINT"] || "");
151151
export const PROJECT_DOC_MAX_BYTES = 32 * 1024; // 32 kB
152152

153153
const PROJECT_DOC_FILENAMES = ["codex.md", ".codex.md", "CODEX.md"];
154+
const PROJECT_DOC_SEPARATOR = "\n\n--- project-doc ---\n\n";
154155

155156
export function discoverProjectDocPath(startDir: string): string | null {
156157
const cwd = resolvePath(startDir);
@@ -305,7 +306,7 @@ export const loadConfig = (
305306

306307
const combinedInstructions = [userInstructions, projectDoc]
307308
.filter((s) => s && s.trim() !== "")
308-
.join("\n\n--- project-doc ---\n\n");
309+
.join(PROJECT_DOC_SEPARATOR);
309310

310311
// Treat empty string ("" or whitespace) as absence so we can fall back to
311312
// the latest DEFAULT_MODEL.
@@ -456,5 +457,12 @@ export const saveConfig = (
456457
writeFileSync(targetPath, JSON.stringify(configToSave, null, 2), "utf-8");
457458
}
458459

459-
writeFileSync(instructionsPath, config.instructions, "utf-8");
460+
if (config.instructions.includes(PROJECT_DOC_SEPARATOR)) {
461+
const userInstructions = config.instructions.split(PROJECT_DOC_SEPARATOR)[0];
462+
if (userInstructions !== undefined) {
463+
writeFileSync(instructionsPath, userInstructions, "utf-8");
464+
}
465+
} else {
466+
writeFileSync(instructionsPath, config.instructions, "utf-8");
467+
}
460468
};

codex-cli/tests/config.test.tsx

+41
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,44 @@ test("loads and saves providers correctly", () => {
234234
expect(mergedConfig.providers["openai"]).toBeDefined();
235235
}
236236
});
237+
238+
test("saves and loads instructions with project doc separator correctly", () => {
239+
const userInstructions = "user specific instructions";
240+
const projectDoc = "project specific documentation";
241+
const combinedInstructions = `${userInstructions}\n\n--- project-doc ---\n\n${projectDoc}`;
242+
243+
const testConfig = {
244+
model: "test-model",
245+
instructions: combinedInstructions,
246+
notify: false,
247+
};
248+
249+
saveConfig(testConfig, testConfigPath, testInstructionsPath);
250+
251+
expect(memfs[testInstructionsPath]).toBe(userInstructions);
252+
253+
const loadedConfig = loadConfig(testConfigPath, testInstructionsPath, {
254+
disableProjectDoc: true,
255+
});
256+
expect(loadedConfig.instructions).toBe(userInstructions);
257+
});
258+
259+
test("handles empty user instructions when saving with project doc separator", () => {
260+
const projectDoc = "project specific documentation";
261+
const combinedInstructions = `\n\n--- project-doc ---\n\n${projectDoc}`;
262+
263+
const testConfig = {
264+
model: "test-model",
265+
instructions: combinedInstructions,
266+
notify: false,
267+
};
268+
269+
saveConfig(testConfig, testConfigPath, testInstructionsPath);
270+
271+
expect(memfs[testInstructionsPath]).toBe("");
272+
273+
const loadedConfig = loadConfig(testConfigPath, testInstructionsPath, {
274+
disableProjectDoc: true,
275+
});
276+
expect(loadedConfig.instructions).toBe("");
277+
});

0 commit comments

Comments
 (0)