fix(config): 适配 kimi-code 0.39.1——thinking keep 写字符串 off(布尔会整节作废)、存量布尔归一化;loop_control 默认只写 v2 键(legacy flag 才双写);effort 移除 max 档;实验 flags 补 remote-control、wait_for/minidb 标默认开启

This commit is contained in:
KimiSwitch Dev committed 2026-08-29 11:13:58 +08:00
1 parent 7435198fcc
commit 45ec073e6f
10 files changed
+306 -29

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
import { describe, expect, it } from "vitest";
import { getAgentSettings, setAgentSettings } from "./agent-settings";
// ---------------------------------------------------------------------------
// Helpers — drill into the written `[thinking]` / `[loop_control]` sections
// ---------------------------------------------------------------------------
function thinkingOf(raw: unknown): Record<string, unknown> {
const t = (raw as { thinking?: unknown })?.thinking;
return t && typeof t === "object" && !Array.isArray(t)
? (t as Record<string, unknown>)
: {};
}
function loopOf(raw: unknown): Record<string, unknown> {
const l = (raw as { loop_control?: unknown })?.loop_control;
return l && typeof l === "object" && !Array.isArray(l)
? (l as Record<string, unknown>)
: {};
}
// ---------------------------------------------------------------------------
// Reading — legacy off values normalized to "off", "max" → "high"
// ---------------------------------------------------------------------------
describe("getAgentSettings — thinking.keep normalization", () => {
it("normalizes a legacy boolean `false` to \"off\"", () => {
expect(getAgentSettings({ thinking: { keep: false } }).thinking?.keep).toBe(
"off"
);
});
it("normalizes the other legacy off values (0, null, \"no\", \"none\") to \"off\"", () => {
for (const v of [0, null, "no", "none"]) {
expect(
getAgentSettings({ thinking: { keep: v } }).thinking?.keep
).toBe("off");
}
});
it("keeps \"all\" and \"off\" as-is on read", () => {
expect(getAgentSettings({ thinking: { keep: "all" } }).thinking?.keep).toBe(
"all"
);
expect(getAgentSettings({ thinking: { keep: "off" } }).thinking?.keep).toBe(
"off"
);
});
it("keeps the default \"all\" when the key is absent", () => {
expect(getAgentSettings({}).thinking?.keep).toBe("all");
});
});
describe("getAgentSettings — effort \"max\" read mapping", () => {
it("normalizes a stored \"max\" to \"high\" on read", () => {
expect(getAgentSettings({ thinking: { effort: "max" } }).thinking?.effort).toBe(
"high"
);
});
});
// ---------------------------------------------------------------------------
// Writing — keep: "all" round-trips, "off" is written for legacy off values,
// absent keep is not materialized
// ---------------------------------------------------------------------------
describe("setAgentSettings — keep serialization", () => {
it("round-trips keep = \"all\"", () => {
const raw = { thinking: { keep: "all" } };
const next = setAgentSettings(raw, { thinking: { keep: "all" } });
expect(thinkingOf(next).keep).toBe("all");
});
it("writes \"off\" (string) for a legacy keep = false, never a boolean", () => {
const next = setAgentSettings({ thinking: { keep: false } }, {});
expect(thinkingOf(next).keep).toBe("off");
});
it("does not write the keep key when it was never set (absent in raw and patch)", () => {
expect(thinkingOf(setAgentSettings({}, {}))).not.toHaveProperty("keep");
const next = setAgentSettings(
{ thinking: { enabled: true } },
{ thinking: { effort: "high" } }
);
expect(thinkingOf(next)).not.toHaveProperty("keep");
expect(thinkingOf(next).effort).toBe("high");
});
it("writes the key when an absent raw config's keep is explicitly toggled off", () => {
const next = setAgentSettings({}, { thinking: { keep: "off" } });
expect(thinkingOf(next).keep).toBe("off");
});
});
// ---------------------------------------------------------------------------
// loop_control — v2 default writes only max_attempts_per_step; legacyV1 opts
// dual-writes; read fallback from the v1 key is preserved
// ---------------------------------------------------------------------------
describe("loop_control — v1/v2 key handling", () => {
const raw = { loop_control: { max_attempts_per_step: 5, max_retries_per_step: 5 } };
it("default (v2): a save strips the legacy max_retries_per_step key", () => {
const next = setAgentSettings(raw, { loop_control: { reserved_context_size: 60000 } });
expect(loopOf(next).max_attempts_per_step).toBe(5);
expect(loopOf(next)).not.toHaveProperty("max_retries_per_step");
});
it("default (v2): a dual-write patch ends up with only the v2 key", () => {
const next = setAgentSettings(
raw,
{ loop_control: { max_attempts_per_step: 7, max_retries_per_step: 7 } }
);
expect(loopOf(next).max_attempts_per_step).toBe(7);
expect(loopOf(next)).not.toHaveProperty("max_retries_per_step");
});
it("legacyV1: both keys are written", () => {
const next = setAgentSettings(
raw,
{ loop_control: { max_attempts_per_step: 7, max_retries_per_step: 7 } },
{ legacyV1: true }
);
expect(loopOf(next).max_attempts_per_step).toBe(7);
expect(loopOf(next).max_retries_per_step).toBe(7);
});
it("read fallback keeps a max_retries_per_step-only config editable", () => {
const s = getAgentSettings({ loop_control: { max_retries_per_step: 4 } });
expect(s.loop_control?.max_attempts_per_step).toBe(4);
expect(s.loop_control?.max_retries_per_step).toBe(4);
});
});
+33 -5
View File
@@ -49,11 +49,24 @@ export function getAgentSettings(rawOther: unknown): AgentSettings {
) {
loop.max_attempts_per_step = sectionLoop.max_retries_per_step;
}
const thinking = {
...DEFAULT_SETTINGS.thinking,
...getSection<AgentSettings["thinking"]>(rawOther, "thinking"),
};
// Upstream engines only accept string off values ("off"/"none"/"no"; see
// KEEP_OFF_VALUES) — a `false` from an old config fails the v2 validator
// and the v1 strict parse. Normalize every legacy off value to "off" for
// display; the serialization path (setAgentSettings) writes the string.
if (thinking.keep !== undefined && thinking.keep !== "all") {
thinking.keep = "off";
}
// The "max" effort tier was removed upstream (auto-migrates to "high");
// old configs still carrying it are shown as "high" (not rewritten on read).
if (thinking.effort === "max") {
thinking.effort = "high";
}
return {
thinking: {
...DEFAULT_SETTINGS.thinking,
...getSection<AgentSettings["thinking"]>(rawOther, "thinking"),
},
thinking,
loop_control: loop,
background: {
...DEFAULT_SETTINGS.background,
@@ -70,7 +83,8 @@ export function getAgentSettings(rawOther: unknown): AgentSettings {
export function setAgentSettings(
rawOther: unknown,
patch: Partial<AgentSettings>
patch: Partial<AgentSettings>,
opts?: { legacyV1?: boolean }
): unknown {
const root = { ...asRecord(rawOther) };
const current = getAgentSettings(rawOther);
@@ -84,6 +98,20 @@ export function setAgentSettings(
hooks: patch.hooks ?? current.hooks ?? [],
};
// `keep` is only written when the source config explicitly carries it or the
// patch sets it — an absent key keeps the engine default ("all") instead of
// being materialized, so a plain save does not add the key.
const rawThinking = asRecord(getSection(rawOther, "thinking"));
const patchHasKeep = patch.thinking !== undefined && "keep" in patch.thinking;
if (!("keep" in rawThinking) && !patchHasKeep) {
delete next.thinking?.keep;
}
// The v2 engine (default) only reads max_attempts_per_step; the legacy v1
// key is stripped on save unless KIMI_CODE_LEGACY_FLAG=1 (v1 engine compat).
if (!opts?.legacyV1) {
delete next.loop_control?.max_retries_per_step;
}
if (next.thinking) root.thinking = next.thinking;
if (next.loop_control) root.loop_control = next.loop_control;
if (next.background) root.background = next.background;
+25
View File
@@ -231,6 +231,31 @@ describe("pool mutations", () => {
expect(section(updated).default_model).toBe("kimi-k1");
});
it("upsertSubagentPoolEntry materializes the implicit default into a new table", () => {
// Implicit single-entry form (default_model set, no models table): adding a
// second alias must pull the default into the table, otherwise the result
// violates "default_model must be a pool key" (rule 3) and gets rejected.
const raw = rawWith({ default_model: "kimi-k1", force: false });
const added = upsertSubagentPoolEntry(raw, "kimi-k2", "");
expect(modelsOf(added)).toEqual({ "kimi-k1": "", "kimi-k2": "" });
expect(section(added).default_model).toBe("kimi-k1");
const pool = getSubagentModelPool(added);
expect(pool).toBeDefined();
expect(
validateSubagentPool(pool!, ["kimi-k1", "kimi-k2"]),
).toEqual([]);
});
it("upsertSubagentPoolEntry upgrades a legacy `model`-only section to a valid pool", () => {
const raw = rawWith({ model: "kimi-k1" });
const added = upsertSubagentPoolEntry(raw, "kimi-k2", "K2");
expect(modelsOf(added)).toEqual({ "kimi-k1": "", "kimi-k2": "K2" });
expect(section(added).default_model).toBe("kimi-k1");
expect(section(added).model).toBe("kimi-k1");
const pool = getSubagentModelPool(added);
expect(validateSubagentPool(pool!, ["kimi-k1", "kimi-k2"])).toEqual([]);
});
it("removeSubagentPoolEntry keeps the default when removing a non-default alias", () => {
const raw = rawWith({
default_model: "kimi-k1",
+39 -5
View File
@@ -28,27 +28,36 @@ export interface ExperimentalFlagDef {
id: string;
/** Single-feature env var that can force the flag (locks the UI toggle). */
envVar: string;
/** Feature turned on by default upstream; an absent config key falls back
* to this, while an explicit `false` in `[experimental]` still wins. */
defaultEnabled?: boolean;
}
/** Known experimental flags — mirrors the kimi-code v2 flag registry
* (the per-feature flag.ts files under packages/agent-core-v2/src:
* secondary-model, tool-select, persistence_minidb_readmodel, tower,
* subagent_fork, wait_for, auto_session_title). `acp-v2` was removed
* upstream and is dropped here. */
* subagent_fork, wait_for, auto_session_title, remote-control). `acp-v2` was
* removed upstream and is dropped here. */
export const EXPERIMENTAL_FLAGS: ExperimentalFlagDef[] = [
{ id: "secondary-model", envVar: "KIMI_CODE_EXPERIMENTAL_SECONDARY_MODEL" },
{ id: "tool-select", envVar: "KIMI_CODE_EXPERIMENTAL_TOOL_SELECT" },
{
id: "persistence_minidb_readmodel",
envVar: "KIMI_CODE_EXPERIMENTAL_PERSISTENCE_MINIDB_READMODEL",
defaultEnabled: true,
},
{ id: "tower", envVar: "KIMI_CODE_EXPERIMENTAL_TOWER" },
{ id: "subagent_fork", envVar: "KIMI_CODE_EXPERIMENTAL_SUBAGENT_FORK" },
{ id: "wait_for", envVar: "KIMI_CODE_EXPERIMENTAL_WAIT_FOR" },
{
id: "wait_for",
envVar: "KIMI_CODE_EXPERIMENTAL_WAIT_FOR",
defaultEnabled: true,
},
{
id: "auto_session_title",
envVar: "KIMI_CODE_EXPERIMENTAL_AUTO_SESSION_TITLE",
},
{ id: "remote-control", envVar: "KIMI_CODE_EXPERIMENTAL_REMOTE_CONTROL" },
];
export const EXPERIMENTAL_MASTER_ENV = "KIMI_CODE_EXPERIMENTAL_FLAG";
@@ -87,6 +96,15 @@ export function getExperimentalFlags(rawOther: unknown): Record<string, boolean>
return out;
}
/** Whether a flag is explicitly written in `[experimental]` (true *or* false).
* An absent flag falls back to the upstream default (`defaultEnabled`). */
export function isExperimentalFlagSet(rawOther: unknown, id: string): boolean {
return Object.prototype.hasOwnProperty.call(
asRecord(asRecord(rawOther)["experimental"]),
id
);
}
/** Set one flag in `[experimental]`. Removing all flags drops the section. */
export function setExperimentalFlag(
rawOther: unknown,
@@ -256,8 +274,11 @@ export function setSubagentModelPool(
return root;
}
/** Add or update one alias in the pool `models` table. The rest of the
* section (default, force, unknown fields) is preserved untouched. */
/** Add or update one alias in the pool `models` table. When the section is
* the implicit single-entry form (a `default_model`/`model` but no table),
* the effective default is materialized into the table first — otherwise the
* new table would fail the engine rule "default_model must be a pool key".
* The rest of the section (force, unknown fields) is preserved untouched. */
export function upsertSubagentPoolEntry(
rawOther: unknown,
alias: string,
@@ -269,8 +290,21 @@ export function upsertSubagentPoolEntry(
for (const [k, v] of Object.entries(section)) next[k] = v;
const models: Record<string, unknown> = {};
for (const [k, v] of Object.entries(asRecord(next.models))) models[k] = v;
const effectiveDefault =
typeof next.default_model === "string" && next.default_model !== ""
? next.default_model
: typeof next.model === "string" && next.model !== ""
? next.model
: undefined;
if (effectiveDefault !== undefined && !(effectiveDefault in models)) {
models[effectiveDefault] = "";
}
models[alias] = description;
next.models = models;
if (effectiveDefault !== undefined) {
next.default_model = effectiveDefault;
next.model = effectiveDefault;
}
root.secondary_model = next;
return root;
}