From 7dcb26a76f7d81c6c73e57a95546e0b95ac34a06 Mon Sep 17 00:00:00 2001 From: "kiloconnect[bot]" <240665456+kiloconnect[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 11:40:38 +0000 Subject: [PATCH 1/2] fix(cli): handle tool-call without prior tool-input-start event When a provider emits tool-call without a preceding tool-input-start event, the tool call was silently dropped causing conversation history corruption and 400 errors on subsequent API requests. This change creates a fallback tool part in the tool-call handler when tool-input-start was never emitted, ensuring the tool call is persisted and included in conversation history. --- packages/opencode/src/session/processor.ts | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index abf1707f4b6..63d59500b09 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -135,6 +135,32 @@ export namespace SessionProcessor { break case "tool-call": { + // kilocode_change start + // If tool-input-start was never emitted for a non-provider-executed + // tool (e.g. provider skipped it for unparseable arguments), create + // the tool part now so it is persisted and included in future + // conversation history. + if (!toolcalls[value.toolCallId] && !value.providerExecuted) { + log.warn("tool-call without prior tool-input-start", { + toolCallId: value.toolCallId, + toolName: value.toolName, + }) + const created = await Session.updatePart({ + id: Identifier.ascending("part"), + messageID: input.assistantMessage.id, + sessionID: input.assistantMessage.sessionID, + type: "tool", + tool: value.toolName, + callID: value.toolCallId, + state: { + status: "pending", + input: {}, + raw: "", + }, + }) + toolcalls[value.toolCallId] = created as MessageV2.ToolPart + } + // kilocode_change end const match = toolcalls[value.toolCallId] if (match) { const part = await Session.updatePart({ From b0aa8acf1c350bf3e4b498909eaa018bde2d55ba Mon Sep 17 00:00:00 2001 From: Christiaan Arnoldus Date: Tue, 31 Mar 2026 15:47:45 +0200 Subject: [PATCH 2/2] Clarify comment --- packages/opencode/src/session/processor.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 63d59500b09..e95c3e1e3a5 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -136,10 +136,8 @@ export namespace SessionProcessor { case "tool-call": { // kilocode_change start - // If tool-input-start was never emitted for a non-provider-executed - // tool (e.g. provider skipped it for unparseable arguments), create - // the tool part now so it is persisted and included in future - // conversation history. + // If tool-input-start was never emitted, this can happen if arguments are unparseable, + // create the tool part now to prevent missing tool results down the line. if (!toolcalls[value.toolCallId] && !value.providerExecuted) { log.warn("tool-call without prior tool-input-start", { toolCallId: value.toolCallId,