From 093fa92e7f4f902e17800741c26adcc8a3826c6c Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Tue, 6 Jan 2026 13:48:25 -0500 Subject: [PATCH] fix: remove isInternalAgent flag due to hook ordering race condition The isInternalAgent flag was causing a race condition where concurrent internal agents (title/summary generators) would set the flag to true, and normal session message transforms would see the stale value and skip injecting the prunable-tools list. This happened because messages.transform runs before system.transform, so the flag set by system.transform was always stale by the time messages.transform checked it. Fix: Remove the flag entirely and only skip system prompt injection for internal agents (detected in system.transform where it works reliably). --- index.ts | 6 +----- lib/hooks.ts | 2 +- lib/state/state.ts | 2 -- lib/state/types.ts | 1 - 4 files changed, 2 insertions(+), 9 deletions(-) diff --git a/index.ts b/index.ts index 28b4a6c..2372d2c 100644 --- a/index.ts +++ b/index.ts @@ -37,14 +37,10 @@ const plugin: Plugin = (async (ctx) => { "Summarize what was done in this conversation", ] if (internalAgentSignatures.some((sig) => systemText.includes(sig))) { - logger.info("Skipping DCP injection for internal agent") - state.isInternalAgent = true + logger.info("Skipping DCP system prompt injection for internal agent") return } - // Reset flag for normal sessions - state.isInternalAgent = false - const discardEnabled = config.tools.discard.enabled const extractEnabled = config.tools.extract.enabled diff --git a/lib/hooks.ts b/lib/hooks.ts index 231357d..e70c892 100644 --- a/lib/hooks.ts +++ b/lib/hooks.ts @@ -15,7 +15,7 @@ export function createChatMessageTransformHandler( return async (input: {}, output: { messages: WithParts[] }) => { await checkSession(client, state, logger, output.messages) - if (state.isSubAgent || state.isInternalAgent) { + if (state.isSubAgent) { return } diff --git a/lib/state/state.ts b/lib/state/state.ts index a85d61e..e68ecf8 100644 --- a/lib/state/state.ts +++ b/lib/state/state.ts @@ -43,7 +43,6 @@ export function createSessionState(): SessionState { return { sessionId: null, isSubAgent: false, - isInternalAgent: false, prune: { toolIds: [], }, @@ -63,7 +62,6 @@ export function createSessionState(): SessionState { export function resetSessionState(state: SessionState): void { state.sessionId = null state.isSubAgent = false - state.isInternalAgent = false state.prune = { toolIds: [], } diff --git a/lib/state/types.ts b/lib/state/types.ts index 9d72e19..1e41170 100644 --- a/lib/state/types.ts +++ b/lib/state/types.ts @@ -27,7 +27,6 @@ export interface Prune { export interface SessionState { sessionId: string | null isSubAgent: boolean - isInternalAgent: boolean prune: Prune stats: SessionStats toolParameters: Map