feat: mark compaction status

This commit is contained in:
2026-08-01 19:06:21 +05:30
parent 09a0e73bb9
commit 01201a28e8
5 changed files with 38 additions and 14 deletions
+4
View File
@@ -1,5 +1,9 @@
# Changelog # Changelog
## 0.2.2
- Add explicit markers for applied and skipped custom compaction instructions.
## 0.2.1 ## 0.2.1
- Add the server entrypoint expected by OpenCode's npm plugin loader. - Add the server entrypoint expected by OpenCode's npm plugin loader.
+5 -3
View File
@@ -27,7 +27,7 @@ You can also add it manually to your global `opencode.jsonc`:
{ {
"memoryFile": ".opencode/compaction.md", "memoryFile": ".opencode/compaction.md",
"mode": "append", "mode": "append",
"completionMarker": "Custom compaction request honored." "completionMarker": "opencode-plugin-compaction-prompt: Custom compaction done."
} }
] ]
] ]
@@ -39,14 +39,16 @@ Create `.opencode/compaction.md` in the project when you have project-specific c
## Options ## Options
| Option | Default | Description | | Option | Default | Description |
| ------------------ | ------------------------------------ | ---------------------------------------------------------------------------------------------------------- | | ------------------ | ------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
| `memoryFile` | `.opencode/compaction.md` | File resolved relative to the active worktree. | | `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. | | `mode` | `append` | Append instructions to OpenCode's default prompt, or use `replace` to provide a complete prompt. |
| `prompt` | "" (empty) | 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. | | `completionMarker` | `opencode-plugin-compaction-prompt: Custom compaction done.` | 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. 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.
When neither a prompt nor a memory file is available, the plugin adds `opencode-plugin-compaction-prompt: No custom compaction applied.` instead.
## Development ## Development
```bash ```bash
+1 -1
View File
@@ -7,7 +7,7 @@
"memoryFile": ".opencode/compaction.md", "memoryFile": ".opencode/compaction.md",
"mode": "append", "mode": "append",
"prompt": "<Example prompt for compaction>", "prompt": "<Example prompt for compaction>",
"completionMarker": "Custom compaction request honored." "completionMarker": "opencode-plugin-compaction-prompt: Custom compaction done."
} }
] ]
] ]
+10 -1
View File
@@ -13,7 +13,10 @@ export type CompactionOptions = PluginOptions & {
const defaultMemoryFile = ".opencode/compaction.md"; const defaultMemoryFile = ".opencode/compaction.md";
const defaultPrompt = ""; const defaultPrompt = "";
const defaultCompletionMarker = "Custom compaction request honored."; const defaultCompletionMarker =
"opencode-plugin-compaction-prompt: Custom compaction done.";
const skippedCompactionMarker =
"opencode-plugin-compaction-prompt: No custom compaction applied.";
function asString(value: unknown): string | undefined { function asString(value: unknown): string | undefined {
if (typeof value !== "string") { if (typeof value !== "string") {
@@ -115,6 +118,12 @@ export const CompactionPromptPlugin: Plugin = async (
const instructions = buildInstructions(prompt, completionMarker, memory); const instructions = buildInstructions(prompt, completionMarker, memory);
if (!instructions) { if (!instructions) {
if (mode === "replace") {
output.prompt = skippedCompactionMarker;
} else {
output.context.push(skippedCompactionMarker);
}
return; return;
} }
+14 -5
View File
@@ -55,7 +55,9 @@ describe("CompactionPromptPlugin", () => {
); );
expect(output.context[0]).toContain("Keep the active hypothesis."); expect(output.context[0]).toContain("Keep the active hypothesis.");
expect(output.context[0]).toContain("Custom compaction request honored."); expect(output.context[0]).toContain(
"opencode-plugin-compaction-prompt: Custom compaction done.",
);
}); });
test("combines custom instructions with a configured memory file", async () => { test("combines custom instructions with a configured memory file", async () => {
@@ -114,10 +116,12 @@ describe("CompactionPromptPlugin", () => {
); );
expect(context.logs).toHaveLength(0); expect(context.logs).toHaveLength(0);
expect(output.context).toHaveLength(0); expect(output.context).toEqual([
"opencode-plugin-compaction-prompt: No custom compaction applied.",
]);
}); });
test("does not modify compaction when no instructions are provided", async () => { test("adds a skipped marker when no instructions are provided", async () => {
const context = await createContext(); const context = await createContext();
const hooks = await CompactionPromptPlugin(context as never); const hooks = await CompactionPromptPlugin(context as never);
const output: { context: string[]; prompt?: string } = { const output: { context: string[]; prompt?: string } = {
@@ -129,7 +133,10 @@ describe("CompactionPromptPlugin", () => {
output, output,
); );
expect(output.context).toEqual(["Existing context"]); expect(output.context).toEqual([
"Existing context",
"opencode-plugin-compaction-prompt: No custom compaction applied.",
]);
expect(output.prompt).toBeUndefined(); expect(output.prompt).toBeUndefined();
}); });
@@ -147,6 +154,8 @@ describe("CompactionPromptPlugin", () => {
); );
expect(context.logs).toHaveLength(1); expect(context.logs).toHaveLength(1);
expect(output.context).toHaveLength(0); expect(output.context).toEqual([
"opencode-plugin-compaction-prompt: No custom compaction applied.",
]);
}); });
}); });