Skip to content

Commit 07d3a53

Browse files
authored
Merge pull request #128 from Opencode-DCP/fix/nudge-counter-after-restart
fix: nudge counter incorrect after session restart
2 parents 3a77692 + 8b5037e commit 07d3a53

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

lib/hooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ export function createChatMessageTransformHandler(
1414
logger: Logger,
1515
config: PluginConfig
1616
) {
17-
return async(
17+
return async (
1818
input: {},
1919
output: { messages: WithParts[] }
2020
) => {
21-
checkSession(client, state, logger, output.messages);
21+
await checkSession(client, state, logger, output.messages)
22+
2223
if (state.isSubAgent) {
2324
return
2425
}

lib/state/state.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { loadSessionState } from "./persistence"
44
import { getLastUserMessage } from "../messages/utils"
55
import { isSubAgentSession } from "../utils"
66

7-
export const checkSession = (
7+
export const checkSession = async (
88
client: any,
99
state: SessionState,
1010
logger: Logger,
1111
messages: WithParts[]
12-
) => {
12+
): Promise<void> => {
1313

1414
const lastUserMessage = getLastUserMessage(messages)
1515
if (!lastUserMessage) {
@@ -20,14 +20,11 @@ export const checkSession = (
2020

2121
if (state.sessionId === null || state.sessionId !== lastSessionId) {
2222
logger.info(`Session changed: ${state.sessionId} -> ${lastSessionId}`)
23-
ensureSessionInitialized(
24-
client,
25-
state,
26-
lastSessionId,
27-
logger
28-
).catch((err) => {
23+
try {
24+
await ensureSessionInitialized(client, state, lastSessionId, logger)
25+
} catch (err: any) {
2926
logger.error("Failed to initialize session state", { error: err.message })
30-
} )
27+
}
3128
}
3229
}
3330

lib/state/tool-cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const MAX_TOOL_CACHE_SIZE = 500
66

77
/**
88
* Sync tool parameters from OpenCode's session.messages() API.
9-
* This is the single source of truth for tool parameters, replacing
10-
* format-specific parsing from LLM API requests.
119
*/
1210
export async function syncToolCache(
1311
state: SessionState,
@@ -24,6 +22,8 @@ export async function syncToolCache(
2422
continue
2523
}
2624

25+
const alreadyPruned = state.prune.toolIds.includes(part.callID)
26+
2727
state.toolParameters.set(
2828
part.callID,
2929
{
@@ -35,7 +35,7 @@ export async function syncToolCache(
3535
}
3636
)
3737

38-
if (!config.strategies.pruneTool.protectedTools.includes(part.tool)) {
38+
if (!alreadyPruned && !config.strategies.pruneTool.protectedTools.includes(part.tool)) {
3939
state.nudgeCounter++
4040
}
4141

0 commit comments

Comments
 (0)