From f3e8dd4b04142d582558f5aaf36728576e73e59f Mon Sep 17 00:00:00 2001 From: bendtherules Date: Sat, 1 Aug 2026 16:57:58 +0530 Subject: [PATCH] fix: skip empty compaction instructions --- CHANGELOG.md | 4 ++++ README.md | 2 +- package.json | 2 +- src/index.ts | 10 +++++++++- test/index.test.ts | 20 ++++++++++++++++++-- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a5b194..5e59b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.1.2 + +- Skip compaction prompt changes when no prompt or memory file is provided. + ## 0.1.1 - Publish the standalone plugin from its public GitHub repository. diff --git a/README.md b/README.md index f038890..8e6c88f 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Create `.opencode/compaction.md` in the project when you have project-specific c | ------------------ | ------------------------------------ | ---------------------------------------------------------------------------------------------------------- | | `memoryFile` | `.opencode/compaction.md` | File resolved relative to the active worktree. | | `mode` | `append` | Append instructions to OpenCode's default prompt, or use `replace` to provide a complete prompt. | -| `prompt` | Built-in preservation prompt | Additional instructions used together with `memoryFile`; both are included in the compaction instructions. | +| `prompt` | "" (empty) | Additional instructions used together with `memoryFile`; both are included in the compaction instructions. | | `completionMarker` | `Custom compaction request honored.` | Exact text the model is asked to append at the end of the summary. | Append mode is the recommended default because it preserves OpenCode's built-in compaction behavior. Replace mode is available when the complete prompt needs to be controlled by this plugin. diff --git a/package.json b/package.json index fc2c236..4325b02 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencode-plugin-compaction-prompt", - "version": "0.1.1", + "version": "0.1.2", "description": "Customize OpenCode's compaction prompt to preserve important context and omit unnecessary details.", "type": "module", "repository": { diff --git a/src/index.ts b/src/index.ts index e083e9d..8b21b39 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,11 @@ function buildInstructions( prompt: string, completionMarker: string, memory: string, -): string { +): string | undefined { + if (!prompt && !memory) { + return undefined; + } + const sections = [ "## User Compaction Instructions", `These instructions take precedence over previous instructions if there is conflict. At the very end of the summary, echo exactly: **${completionMarker}**`, @@ -110,6 +114,10 @@ export const CompactionPromptPlugin: Plugin = async ( const memory = await readMemory(memoryPath, logError); const instructions = buildInstructions(prompt, completionMarker, memory); + if (!instructions) { + return; + } + if (mode === "replace") { output.prompt = instructions; } else { diff --git a/test/index.test.ts b/test/index.test.ts index 0fef545..0054f2d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -114,7 +114,23 @@ describe("CompactionPromptPlugin", () => { ); expect(context.logs).toHaveLength(0); - expect(output.context).toHaveLength(1); + expect(output.context).toHaveLength(0); + }); + + test("does not modify compaction when no instructions are provided", async () => { + const context = await createContext(); + const hooks = await CompactionPromptPlugin(context as never); + const output: { context: string[]; prompt?: string } = { + context: ["Existing context"], + }; + + await hooks["experimental.session.compacting"]?.( + { sessionID: "test" }, + output, + ); + + expect(output.context).toEqual(["Existing context"]); + expect(output.prompt).toBeUndefined(); }); test("logs non-missing memory file errors without failing compaction", async () => { @@ -131,6 +147,6 @@ describe("CompactionPromptPlugin", () => { ); expect(context.logs).toHaveLength(1); - expect(output.context).toHaveLength(1); + expect(output.context).toHaveLength(0); }); });