Skip to content

Commit a28a02c

Browse files
authored
fix(compaction): respect previous message agent on context injection (#10)
* docs: fix oh-my-opencode hook name The correct hook name is 'anthropic-context-window-limit-recovery', not 'anthropic-auto-compact'. 🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * fix(cli): update hook name to anthropic-context-window-limit-recovery Also update CLI flag from --disable-auto-compact to --disable-context-recovery to match the new hook naming convention. 🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * fix(compaction): respect previous message agent on context injection Remove hardcoded 'agent: "general"' from injectCompactionContext. The injectHookMessage function already has fallback logic that uses findNearestMessageWithFields to get the actual agent from previous messages. By not passing an explicit agent, we allow the existing fallback to work: resolvedAgent = originalMessage.agent ?? fallback?.agent ?? "general" This ensures the compaction context message is correctly attributed to the agent that was active when compaction was triggered, maintaining proper agent context across session compaction. 🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
1 parent 48dd1f6 commit a28a02c

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

bun.lock

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/compaction.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ interface SummarizeContext {
4848
modelID: string;
4949
usageRatio: number;
5050
directory: string;
51+
agent?: string;
5152
}
5253

5354
export interface CompactionOptions {
@@ -282,7 +283,7 @@ export function createCompactionHook(
282283
const prompt = createCompactionPrompt(projectMemories);
283284

284285
const success = injectHookMessage(summarizeCtx.sessionID, prompt, {
285-
agent: "general",
286+
agent: summarizeCtx.agent,
286287
model: { providerID: summarizeCtx.providerID, modelID: summarizeCtx.modelID },
287288
path: { cwd: summarizeCtx.directory },
288289
});
@@ -329,8 +330,19 @@ export function createCompactionHook(
329330
const tokens = lastAssistant.tokens;
330331
if (!tokens) return;
331332

332-
const modelID = lastAssistant.modelID ?? "";
333-
const providerID = lastAssistant.providerID ?? "";
333+
let modelID = lastAssistant.modelID ?? "";
334+
let providerID = lastAssistant.providerID ?? "";
335+
let agent: string | undefined;
336+
337+
// Fallback: find model/agent from stored messages if not available
338+
const messageDir = getMessageDir(sessionID);
339+
const storedMessage = messageDir ? findNearestMessageWithFields(messageDir) : null;
340+
341+
if (!providerID || !modelID) {
342+
if (storedMessage?.model?.providerID) providerID = storedMessage.model.providerID;
343+
if (storedMessage?.model?.modelID) modelID = storedMessage.model.modelID;
344+
}
345+
agent = storedMessage?.agent;
334346

335347
if (!isSupportedModel(modelID)) {
336348
log("[compaction] skipping unsupported model", { modelID });
@@ -381,6 +393,7 @@ export function createCompactionHook(
381393
modelID,
382394
usageRatio,
383395
directory: ctx.directory,
396+
agent,
384397
});
385398

386399
state.summarizedSessions.add(sessionID);

0 commit comments

Comments
 (0)