From b0f5c98e68f2ceb9d67c293195c1a66c4a508c30 Mon Sep 17 00:00:00 2001 From: Sanyue Date: Sat, 17 Jan 2026 19:54:38 +0800 Subject: [PATCH 1/2] feat(config): add configurable compaction threshold Add compactionThreshold config option (default 0.80) to control when context compaction triggers. Previously hardcoded at 80%, now users can adjust via supermemory.jsonc. - Add compactionThreshold to SupermemoryConfig interface - Add default value 0.80 in DEFAULTS - Export compactionThreshold in CONFIG - Pass threshold to createCompactionHook - Update README with config documentation --- README.md | 5 ++++- src/config.ts | 3 +++ src/index.ts | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8cde4bc..edc3199 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,10 @@ Create `~/.config/opencode/supermemory.jsonc`: "containerTagPrefix": "opencode", // Extra keyword patterns for memory detection (regex) - "keywordPatterns": ["log\\s+this", "write\\s+down"] + "keywordPatterns": ["log\\s+this", "write\\s+down"], + + // Context usage ratio that triggers compaction (0-1) + "compactionThreshold": 0.80 } ``` diff --git a/src/config.ts b/src/config.ts index 2a4351e..89bbfd7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -19,6 +19,7 @@ interface SupermemoryConfig { containerTagPrefix?: string; filterPrompt?: string; keywordPatterns?: string[]; + compactionThreshold?: number; } const DEFAULT_KEYWORD_PATTERNS = [ @@ -49,6 +50,7 @@ const DEFAULTS: Required> = { containerTagPrefix: "opencode", filterPrompt: "You are a stateful coding agent. Remember all the information, including but not limited to user's coding preferences, tech stack, behaviours, workflows, and any other relevant details.", keywordPatterns: [], + compactionThreshold: 0.80, }; function isValidRegex(pattern: string): boolean { @@ -91,6 +93,7 @@ export const CONFIG = { ...DEFAULT_KEYWORD_PATTERNS, ...(fileConfig.keywordPatterns ?? []).filter(isValidRegex), ], + compactionThreshold: fileConfig.compactionThreshold ?? DEFAULTS.compactionThreshold, }; export function isConfigured(): boolean { diff --git a/src/index.ts b/src/index.ts index fc844f7..b6ff342 100644 --- a/src/index.ts +++ b/src/index.ts @@ -47,7 +47,9 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { } const compactionHook = isConfigured() && ctx.client - ? createCompactionHook(ctx as CompactionContext, tags) + ? createCompactionHook(ctx as CompactionContext, tags, { + threshold: CONFIG.compactionThreshold, + }) : null; return { From b327b609731ddf982f7c439992a627d4687e1557 Mon Sep 17 00:00:00 2001 From: Sanyue0v0 Date: Sat, 17 Jan 2026 20:04:22 +0800 Subject: [PATCH 2/2] fix(config): validate compactionThreshold to ensure 0-1 range Prevents silent failures when users enter invalid values like 80 instead of 0.80, which would disable compaction and risk context overflow. --- src/config.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index 89bbfd7..e9c98bb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -62,6 +62,14 @@ function isValidRegex(pattern: string): boolean { } } +function validateCompactionThreshold(value: number | undefined): number { + if (value === undefined || typeof value !== 'number' || isNaN(value)) { + return DEFAULTS.compactionThreshold; + } + if (value <= 0 || value > 1) return DEFAULTS.compactionThreshold; + return value; +} + function loadConfig(): SupermemoryConfig { for (const path of CONFIG_FILES) { if (existsSync(path)) { @@ -93,7 +101,7 @@ export const CONFIG = { ...DEFAULT_KEYWORD_PATTERNS, ...(fileConfig.keywordPatterns ?? []).filter(isValidRegex), ], - compactionThreshold: fileConfig.compactionThreshold ?? DEFAULTS.compactionThreshold, + compactionThreshold: validateCompactionThreshold(fileConfig.compactionThreshold), }; export function isConfigured(): boolean {