Skip to content

fix(tui): handle fixed-sampling compaction and report post-compaction context - #19

Merged
h3c-hexin merged 4 commits into
Pinvou:pinvou3-cleanfrom
qiuYliangM:pinvou3/fixed-sampling-and-compaction-usage
Aug 25, 2026
Merged

fix(tui): handle fixed-sampling compaction and report post-compaction context#19
h3c-hexin merged 4 commits into
Pinvou:pinvou3-cleanfrom
qiuYliangM:pinvou3/fixed-sampling-and-compaction-usage

Conversation

@qiuYliangM

@qiuYliangM qiuYliangM commented Aug 20, 2026

Copy link
Copy Markdown

Background

Compaction requests use an explicit temperature: 0.3. The Kimi Code membership route rejected that value for kimi-for-coding, so manual and automatic compaction could fail while normal chat continued to work. Kimi Code's current official roster also includes the K3 IDs k3 and k3-256k, which share the membership route's fixed-sampling contract.

The Pinvou host also needs a post-compaction context estimate immediately; otherwise its usage chip keeps showing the stale pre-compaction value until another provider usage event arrives.

Changes

  • On the exact Kimi Code membership endpoint, omit client-supplied temperature and top_p for the four documented membership IDs: k3, k3-256k, kimi-for-coding, and kimi-for-coding-highspeed. Custom gateways and unknown/future IDs remain untouched.
  • Register k3-256k as a first-class Kimi Code model with the K3 nested reasoning contract, a 262,144-token static context window, and a conservative 131,072-token output ceiling.
  • Keep bare k3 separate for membership-plan context handling: only bare k3 can enter the existing 1M plan-tier/provider-metadata path; k3-256k uses the ordinary fixed model metadata path.
  • Resolve exact known model metadata before numeric name hints, so k3-256k is not misread as 256,000 tokens.
  • Keep the observed DeepSeek workaround only on the exact deepseek-v4-flash Responses route. DeepSeek Chat preserves the documented temperature field, including on custom and lookalike hosts.
  • Add CompactionCompleted.post_input_tokens using the engine canonical complete input estimate, including the system prompt and accumulated compaction summary.
  • Add regression coverage for route lookalikes, unknown/future model IDs, K3 route boundaries, fixed sampling, model metadata, and the emitted host token value.

Validation

  • cargo fmt --all -- --check
  • cargo test -p codewhale-tui --lib --locked kimi_code — 48 passed
  • cargo test -p codewhale-tui --lib --locked forkguard_ — 27 passed
  • cargo test -p codewhale-tui --lib --locked models::tests — 27 passed
  • exact K3 request-boundary tests — 2 passed
  • exact K3 product-pairing regression — passed
  • cargo check -p codewhale-tui --locked — passed
  • Original contributor validation: live Kimi Code manual compaction succeeded after the sampling fix

Known risks

  • Current DeepSeek documentation advertises a 0..=2 temperature range, and live testing on 2026-08-25 could not reproduce the original DeepSeek 400. The compatibility shim remains only for the exact V4-Flash Responses path; Chat and every other model preserve their sampling contract.
  • k3-256k gets the documented 262,144-token static model fact and does not inherit bare k3's automatic 1M entitlement path. The existing explicit context_window operator override remains authoritative; this PR deliberately does not add a model-specific clamp or new limit-provenance machinery.
  • The host consumption and fork register updates are stacked in fix(codex): integrate compaction usage and fixed-sampling compatibility pinvou-agent#322 and should pin the final commit produced when this PR lands.

No-Issue: repository Issues are disabled (creation returns 410); the reproduction and validation evidence are recorded above.

@github-actions

Copy link
Copy Markdown

Thanks @qiuYliangM for taking the time to contribute.

This repository is observing a maintainer-managed PR intake gate in dry-run mode, so this pull request is staying open. This note helps maintainers prepare the allowlist before any enforcement is considered.

Please read CONTRIBUTING.md for the expected contribution shape. A maintainer can grant recurring PR access by commenting /lgtm on a pull request.

@h3c-hexin h3c-hexin changed the title fix(client): 固定采样路由剥离非 1 的 temperature,修复压缩 400;压缩完成事件携带上下文估算 fix(tui): handle fixed-sampling compaction and report post-compaction context Aug 25, 2026
@h3c-hexin

h3c-hexin commented Aug 25, 2026

Copy link
Copy Markdown

I pushed two corrective commits after reviewing the full route and host contracts:

  • 2e9eb9c338517fb830effac6161e9ca6dd7274d0 tightens the original compaction and usage contracts.
  • 7f1a592a993d0a3cddc504df046d854a5dfff3ec adds the missing current Kimi Code K3 model variants.

The resulting implementation:

  • preserves the documented DeepSeek Chat sampling contract and limits the remaining Responses workaround to exact deepseek-v4-flash;
  • recognizes all four model IDs in the current official Kimi Code membership roster: k3, k3-256k, kimi-for-coding, and kimi-for-coding-highspeed;
  • omits temperature and top_p only for those exact IDs on the exact first-party membership endpoint;
  • gives both K3 IDs the same nested reasoning dialect while keeping bare k3 alone in the existing 1M plan-entitlement path;
  • records k3-256k as 262,144 context tokens and prevents the generic name-hint parser from misreading it as 256,000;
  • reports the engine's complete post-compaction input estimate, including the system prompt.

I initially explored a larger model-specific context-clamp/provenance change, but removed it before committing. The final supplement reuses the existing roster, route predicate, model metadata, and operator override paths; no new foundation abstraction was added.

Validation completed:

  • cargo fmt --all -- --check
  • Kimi Code suite — 48 passed
  • fork-guard suite — 27 passed
  • model metadata suite — 27 passed
  • exact request-boundary tests — 2 passed
  • cargo check -p codewhale-tui --locked

The dependent Pinvou/pinvou-agent#322 gitlink should be finalized only after this PR lands, using the resulting commit that is reachable from Pinvou/CodeWhale.

@qiuYliangM

Copy link
Copy Markdown
Author

Verified the fixed-sampling claims against the live endpoints with real first-party credentials (results from today, 2026-08-25):

DeepSeek official endpoint — 400 not reproducible anywhere

Request Result
Chat deepseek-v4-pro, temperature: 0.3 200
Chat deepseek-v4-pro, temperature omitted 200
Chat deepseek-v4-flash, temperature: 0.3 200
Responses deepseek-v4-flash, temperature: 0.3 200
Responses deepseek-v4-pro, temperature: 0.3 200
Responses flash, streamed + instructions + input items + top_p: 0.95, temperature: 0.3 200

So the original commit message's claim that the official DeepSeek v4 routes reject non-1 temperature does not hold today. Either the provider relaxed the constraint after 2026-08-19, or the observed 400 was misattributed. Consequences for this revision:

  • Dropping the Chat-side apply_deepseek_v4_official_fixed_sampling is safe — v4-pro (the model actually bound to the Chat dialect) accepts 0.3.
  • The Responses shim narrowed to exact deepseek-v4-flash no longer fixes a reproducible failure either; it is harmless insurance for compaction-only traffic. Keep or drop, both defensible.

Kimi Code membership route — 400 reproduced exactly

  • POST https://api.kimi.com/coding/v1/chat/completions, model: kimi-for-coding, temperature: 0.3400 {"error":{"message":"invalid temperature: only 1 is allowed for this model"}} (verbatim the error in the original commit message).
  • Same request with temperature omitted → 200.

So the real fixed-sampling route is the Kimi Code membership route, and the exact-ID allowlist (kimi-for-coding, kimi-for-coding-highspeed) covers the model IDs actually configured on that route. The narrowing from prefix match to the two documented IDs is fine.

Minor: the api.deepseek.com.evil.example case in forkguard_deepseek_v4_chat_preserves_documented_temperature is now a dead assertion — with the host-matching helper removed, no Chat-side code path distinguishes lookalike hosts at all. Consider dropping that case or rewording the comment.

Cross-repo heads-up: the host wiring in pinvou-agent PR Hmbown#322 currently pins the CodeWhale gitlink to 6062fde85 (before this revision) and adds fork-guard fingerprints referencing apply_deepseek_v4_official_fixed_sampling( / forkguard_deepseek_v4_official_route_strips_fixed_temperature / forkguard_deepseek_v4_responses_drops_non_one_temperature. I'll update Hmbown#322 to match this revision: gitlink → 2e9eb9c33, fingerprints → the new symbol names, and the fork-modifications.md entry → the complete-estimate (system prompt included) semantics of post_input_tokens.

luzeyang (INT) and others added 4 commits August 25, 2026 17:54
DeepSeek 官方端点 v4 系列与 Kimi Code 会员路由 kimi-for-coding 系列
的采样参数被固定(temperature 只允许 1),compaction 等辅助调用硬编码
temperature 0.3 在这些路由必现 400 "only 1 is allowed for this model"
——正常聊天不显式携带 temperature 所以不受影响,表现为聊天正常、
一点压缩就炸,自动压缩到达阈值后同样失败,长会话无法续命。

修复在出站侧按精确路由剥离:Chat 方言在 build_chat_wire_body seam
新增 apply_deepseek_v4_official_fixed_sampling 与
apply_kimi_code_coding_plan_fixed_sampling;Responses 方言在
build_responses_body_for_provider 对 v4 模型剥离非 1 值。中转网关、
旧代模型与其他 provider 的 wire 契约不动。

新增 forkguard_deepseek_v4_official_route_strips_fixed_temperature、
forkguard_deepseek_v4_responses_drops_non_one_temperature 与
forkguard_kimi_code_coding_plan_strips_non_one_temperature 锁定行为。

Signed-off-by: luzeyang (INT) <lu.zeyang@h3c.com>
新增 post_input_tokens 字段:emit_compaction_completed 在
replace_messages 之后对压缩后的新消息列表做保守估算
(estimate_input_tokens_conservative,不含 system prompt)。

宿主(pinvou3 代码页用量 chip)靠它在压缩完成后立即刷新展示的
上下文占用;此前只能等下一轮对话的真实 usage,期间一直显示压缩前
的旧值。TUI 与 runtime thread 持久化路径不消费该字段。

Signed-off-by: luzeyang (INT) <lu.zeyang@h3c.com>
Preserve the documented DeepSeek Chat sampling contract while limiting the observed Responses compatibility shim to the exact V4-Flash model. Keep Kimi fixed sampling on the two known K2.7 membership IDs and exact first-party route.

Emit the engine's complete post-compaction input estimate, including the system prompt, and cover route lookalikes, unknown model IDs, and the host event value with regression tests.

Signed-off-by: hexin <he.xin@h3c.com>
Signed-off-by: hexin <he.xin@h3c.com>
@qiuYliangM
qiuYliangM force-pushed the pinvou3/fixed-sampling-and-compaction-usage branch from 7f1a592 to ad3f26b Compare August 25, 2026 09:57
@qiuYliangM

Copy link
Copy Markdown
Author

Follow-up: the branch was based on a36e6cd53 (#13) and no longer contained #15/#16/#17 — any consumer pinning the gitlink to the old head 2e9eb9c33 would silently drop the r9 work (steer, edit-last-turn, Shell cancellation). I rebased the branch onto the current pinvou3-clean head 07d183e35; the rebase was conflict-free and now includes the latest K3-variant commit as well. New head: ad3f26b39 (force-pushed with lease). pinvou-agent#322 will pin this SHA.

One note on the K3 commit: the membership allowlist grew to four IDs (k3, k3-256k, kimi-for-coding, kimi-for-coding-highspeed) behind is_kimi_code_membership_model — verified the forkguard_kimi_code_coding_plan_strips_non_one_temperature / forkguard_deepseek_v4_chat_preserves_documented_temperature names are unchanged, so the parent fork-guard fingerprints stay valid.

@h3c-hexin

Copy link
Copy Markdown

/lgtm

@github-actions

Copy link
Copy Markdown

@qiuYliangM is already approved for pr contributions in .github/APPROVED_CONTRIBUTORS.

@h3c-hexin h3c-hexin closed this Aug 25, 2026
@h3c-hexin h3c-hexin reopened this Aug 25, 2026
@h3c-hexin
h3c-hexin merged commit feb8761 into Pinvou:pinvou3-clean Aug 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants