Skip to content

Commit c4bce00

Browse files
clawsweeper[bot]jason-allen-onealosolmaz
authored
fix(ollama): strip inline kimi cloud reasoning leak (#86515)
Summary: - This PR adds an Ollama Kimi-cloud visible-content sanitizer for streamed and final assistant replies, updates stream handling and regression tests, and adds a changelog entry. - PR surface: Source +183, Tests +473, Docs +1. Total +657 across 7 files. - Reproducibility: yes. from source and the linked report: current main appends Ollama `message.content` direc ... payload described in the issue would be shown. I did not run a live vendor repro in this read-only review. Automerge notes: - PR branch already contained follow-up commit before automerge: fix(ollama): sanitize kimi inline reasoning in stream events - PR branch already contained follow-up commit before automerge: fix(ollama): buffer kimi cloud stream reasoning - PR branch already contained follow-up commit before automerge: fix(ollama): cover kimi inline boundary variants - PR branch already contained follow-up commit before automerge: fix(ollama): preserve text start partial state - PR branch already contained follow-up commit before automerge: fix(ollama): bound kimi stream sanitizer hold - PR branch already contained follow-up commit before automerge: fix(ollama): keep kimi sanitizer deltas append-only Validation: - ClawSweeper review passed for head b709229. - Required merge gates passed before the squash merge. Prepared head SHA: b709229 Review: #86515 (comment) Co-authored-by: Jason O'Neal <jason.allen.oneal@gmail.com> Co-authored-by: Onur Solmaz <2453968+osolmaz@users.noreply.github.com> Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com> Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com> Approved-by: osolmaz Co-authored-by: osolmaz <2453968+osolmaz@users.noreply.github.com>
1 parent bc10fad commit c4bce00

7 files changed

Lines changed: 715 additions & 58 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Docs: https://docs.openclaw.ai
7575
- Codex harness: make subscription usage-limit errors without reset times explain that OpenClaw cannot determine the reset and point users to wait until Codex is available, use another Codex account, or switch to another configured model/provider. Thanks @amknight.
7676
- Google Vertex: support production ADC modes such as Workload Identity Federation, service-account credentials, and metadata-server ADC for the native Vertex transport. (#83971) Thanks @damianFelixPago.
7777
- Telegram: route normal `[telegram][diag]` polling diagnostics through `runtime.log` while keeping non-diag warnings and persistence failures on `runtime.error`, so healthy polling startup no longer looks like an error. Fixes #82957. (#82958) Thanks @galiniliev.
78+
- Providers/Ollama: strip inline Kimi cloud reasoning prefixes from streamed and final visible replies while keeping ordinary Kimi answers append-only. (#86286) Thanks @jason-allen-oneal.
7879

7980
- Gateway: require Talk secret authority before setup-code handoff can include Talk secrets. (#85690) Thanks @ngutman.
8081
- Agents: keep fallback error reporting scoped to the active model candidate so stale prior-provider quota/auth text is not reported for later fallback attempts. (#86134) thanks @zhangguiping-xydt.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { isOllamaCloudKimiModelRef } from "./sanitizers/kimi-inline-reasoning.js";
2+
3+
export function shouldWrapOllamaCompatMoonshotThinking(modelId: string): boolean {
4+
return isOllamaCloudKimiModelRef(modelId);
5+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
2+
import type {
3+
OllamaVisibleContentSanitizer,
4+
OllamaVisibleContentStreamResolution,
5+
} from "./visible-content-contract.js";
6+
7+
const INLINE_REASONING_MIN_PREFIX_CHARS = 80;
8+
const INLINE_REASONING_MAX_PENDING_CHARS = 512;
9+
const INLINE_REASONING_BOUNDARY_RE = /(^|\s)\uFE0F\s*/u;
10+
11+
type InlineReasoningVisibleTextResolution =
12+
| { kind: "visible"; text: string; bypassInlineReasoning?: boolean }
13+
| { kind: "pending" };
14+
15+
export function isOllamaCloudKimiModelRef(modelId: string): boolean {
16+
const normalizedModelId = normalizeLowercaseStringOrEmpty(modelId);
17+
const slashIndex = normalizedModelId.indexOf("/");
18+
const normalizedWireModelId =
19+
slashIndex === -1 ? normalizedModelId : normalizedModelId.slice(slashIndex + 1);
20+
return normalizedWireModelId.startsWith("kimi-k") && normalizedWireModelId.includes(":cloud");
21+
}
22+
23+
function resolveInlineReasoningVisibleText(params: {
24+
text: string;
25+
final: boolean;
26+
}): InlineReasoningVisibleTextResolution {
27+
const match = INLINE_REASONING_BOUNDARY_RE.exec(params.text);
28+
if (!match) {
29+
if (!params.final && params.text.length <= INLINE_REASONING_MAX_PENDING_CHARS) {
30+
return { kind: "pending" };
31+
}
32+
return {
33+
kind: "visible",
34+
text: params.text,
35+
bypassInlineReasoning:
36+
!params.final && params.text.length > INLINE_REASONING_MAX_PENDING_CHARS,
37+
};
38+
}
39+
40+
const boundaryStartIndex = match.index + match[1].length;
41+
const boundaryEndIndex = match.index + match[0].length;
42+
const prefix = params.text.slice(0, boundaryStartIndex).trim();
43+
const answer = params.text.slice(boundaryEndIndex).trim();
44+
if (prefix.length >= INLINE_REASONING_MIN_PREFIX_CHARS) {
45+
return { kind: "visible", text: answer };
46+
}
47+
48+
return params.final ? { kind: "visible", text: params.text } : { kind: "pending" };
49+
}
50+
51+
export function createKimiInlineReasoningSanitizer(): OllamaVisibleContentSanitizer {
52+
let bypassInlineReasoning = false;
53+
54+
return {
55+
resolveStreamText(params): OllamaVisibleContentStreamResolution {
56+
if (bypassInlineReasoning) {
57+
return { kind: "visible", text: params.text };
58+
}
59+
60+
const resolution = resolveInlineReasoningVisibleText(params);
61+
if (resolution.kind === "pending") {
62+
return resolution;
63+
}
64+
if (resolution.bypassInlineReasoning) {
65+
bypassInlineReasoning = true;
66+
}
67+
return { kind: "visible", text: resolution.text };
68+
},
69+
sanitizeFinalText(text) {
70+
const resolution = resolveInlineReasoningVisibleText({ text, final: true });
71+
return resolution.kind === "visible" ? resolution.text : text;
72+
},
73+
};
74+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type OllamaVisibleContentStreamResolution =
2+
| { kind: "visible"; text: string }
3+
| { kind: "pending" };
4+
5+
export type OllamaVisibleContentSanitizer = {
6+
resolveStreamText(params: { text: string; final: boolean }): OllamaVisibleContentStreamResolution;
7+
sanitizeFinalText(text: string): string;
8+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
createKimiInlineReasoningSanitizer,
3+
isOllamaCloudKimiModelRef,
4+
} from "./kimi-inline-reasoning.js";
5+
import type { OllamaVisibleContentSanitizer } from "./visible-content-contract.js";
6+
7+
const noopVisibleContentSanitizer: OllamaVisibleContentSanitizer = {
8+
resolveStreamText(params) {
9+
return { kind: "visible", text: params.text };
10+
},
11+
sanitizeFinalText(text) {
12+
return text;
13+
},
14+
};
15+
16+
export function createOllamaVisibleContentSanitizer(
17+
modelId: string,
18+
): OllamaVisibleContentSanitizer {
19+
if (isOllamaCloudKimiModelRef(modelId)) {
20+
return createKimiInlineReasoningSanitizer();
21+
}
22+
return noopVisibleContentSanitizer;
23+
}
24+
25+
export function sanitizeOllamaFinalVisibleContent(params: {
26+
modelId: string;
27+
text: string;
28+
}): string {
29+
return createOllamaVisibleContentSanitizer(params.modelId).sanitizeFinalText(params.text);
30+
}

0 commit comments

Comments
 (0)