Files
plugins/packages/kimi-desktop/catalog-migration.test.mjs
Changtian Li bc5b8332ec kimi-desktop 0.1.6: per-role reasoning effort and context input budget (#813)
kimi-desktop 0.1.6: separate reasoning effort and an optional context input budget for the primary and secondary roles.

The change (f07e7f4a..1d14a185, since the round-6 approval):
- The settings page and API save `<role>ReasoningEffort` and `<role>ContextTokens` for each role. Only efforts and context limits the catalog publishes are offered; an unknown limit allows no custom budget.
- Inference drops Kimi's top-level `thinking`, `reasoning` and `reasoning_effort`, and sends the chosen effort, or none for the provider default. Kimi's native menu aliases no longer choose the model.
- A custom budget counts a copy of the request through magpie's `/v1/messages/count_tokens`. Over the budget gives 400 `context_length_exceeded`; a failed count gives fixed 503 `context_count_unavailable`. No generation is sent in either case. Without a budget, nothing extra is sent.
- Role-scoped worker profiles (`v2:[model, role]`) tell primary from secondary even on the same model. Older roleless profiles keep their binding and use defaults.
- 1d14a185 removes blank lines only.

Run at 1d14a1859f63fd4bb512d492d1de419ad3e6c79a on mote (macOS, Bun 1.4.2, HOME/TMPDIR/XDG in the run's own folder):
- `bun test` in packages/kimi-desktop: 626 pass, 0 fail, 29 files.
- `bun scripts/check.mjs kimi-desktop`: exit 0.
- 14 hand breaks, each red in the package suite:
  - drop `applyReasoningOptions`;
  - strip thinking fields only when an effort is set;
  - drop the budget check, or make it `>=`;
  - `useFresh` false, or without its model check;
  - a legacy roleless profile treated as primary;
  - a profile name without its role;
  - no `registerRoles`;
  - no `optionsMatch` in save;
  - no `refreshLiveModelOptions`;
  - count with `redirect: 'follow'`, or with no reply size limit;
  - a 131072 fallback for an unknown catalog context.
- CI at the head: test and on-npm pass. The PR merges onto main f379bf90+ with no conflicts.

Not blocking, left for a follow-up:
- Each inference now reads connection.json (adapter.mjs:547) with no fallback to the live connection. An unreadable file turns every request into 502, even without a budget.
- The comment above `applyReasoningOptions` ("Preserve the legacy connection untouched…") no longer matches the always-strip policy.
2026-11-09 10:07:07 +08:00

118 lines
6.4 KiB
JavaScript

import { test, expect } from 'bun:test';
import fs from 'node:fs';
import { canonicalMenuAlias, isMenuAlias, normalizeModels, neutralPromptAliases } from './catalog.mjs';
import { recoveryPeer } from './test-fixtures/control-peer.mjs';
const read = file => JSON.parse(fs.readFileSync(file, 'utf8'));
test('neutral model aliases retain the old namespace for migration and cleanup', () => {
const rows = normalizeModels({ data: [{ id: 'provider/main' }] });
const alias = rows[0].alias, legacy = alias.replace('local-model-', 'magpie-local-');
expect(alias).toMatch(/^local-model-[a-f0-9]{24}$/);
expect(rows[0].menu.displayName).not.toMatch(/magpie/i);
expect(isMenuAlias(alias)).toBe(true);
expect(isMenuAlias(legacy)).toBe(true);
expect(canonicalMenuAlias(legacy)).toBe(alias);
const prose = { messages: [{ role: 'user', content: 'magpie is a word in my own instructions' }] };
expect(neutralPromptAliases(prose)).toEqual(prose);
});
test('catalog migration acknowledges a native selection before removing the old selected alias', async () => {
const f = recoveryPeer();
try {
await f.controller.tick();
const oldSelection = f.selectAlias();
const before = f.calls.length;
f.controller.catalogAfter = 0;
await f.controller.tick();
expect(read(f.controller.configFile).model.current).toBe('native');
expect(isMenuAlias(oldSelection)).toBe(true);
expect(f.menu.map(row => row.model)).toEqual(['native']);
expect(Object.keys(read(f.controller.configFile).model.models)).toEqual(['native']);
expect(f.calls.slice(before).filter(call => call.method.startsWith('conversations.')).map(call => [call.method, call.params])).toEqual([
['conversations.getKimiModelList', {}],
['conversations.setModel', { modelAlias: 'native' }],
['conversations.updateKimiModelList', { models: [{ model: 'native', maxContextSize: 131072, capabilities: [] }] }],
]);
expect(f.controller.status.code).toBe('connected');
} finally { await f.cleanup(); }
});
test('review guard: a native catalog containing only local aliases never selects or updates a menu', async () => {
const f = recoveryPeer();
try {
await f.controller.tick();
const alias = f.selectAlias({ legacy: false });
const doc = read(f.controller.configFile);
// Keep a native current selection so removing the empty-native guard would
// reach updateKimiModelList instead of throwing while choosing native[0].
doc.model.current = 'native';
fs.writeFileSync(f.controller.configFile, JSON.stringify(doc));
const control = f.controller.rpcCall;
const writes = [];
f.controller.rpcCall = async (runner, method, params, options) => {
if (method === 'conversations.getKimiModelList') return { models: [{ model: alias }] };
if (method === 'conversations.setModel' || method === 'conversations.updateKimiModelList') {
writes.push({ method, params });
return { ok: true };
}
return control(runner, method, params, options);
};
const before = fs.readFileSync(f.controller.configFile);
f.controller.catalogAfter = 0;
await f.controller.tick();
expect(writes).toEqual([]);
expect(f.controller.catalogError).toBe('catalog_sync_failed');
expect(fs.readFileSync(f.controller.configFile)).toEqual(before);
// Avoid exercising shutdown with the deliberately incomplete catalog.
f.controller.rpcCall = control;
} finally { await f.cleanup(); }
});
test('normal user data and non-Agent tools retain alias-shaped filenames and distinct keys', () => {
const alias = normalizeModels({ data: [{ id: 'provider/main' }] })[0].alias;
const old = alias.replace('local-model-', 'magpie-local-'), filename = `/tmp/${old}.json`;
const payload = { tools: [
{ type: 'function', function: { name: 'read_file', parameters: { properties: { [old]: { type: 'string' }, [alias]: { type: 'integer' } } } } },
{ type: 'function', function: { name: 'Agent', description: `Read ${filename} if needed.\nAvailable models (pass via model):\n- ${old} [default]: Secondary model`,
parameters: { properties: { model: { type: 'string', enum: [old] }, prompt: { description: filename } } } } },
], messages: [
{ role: 'user', content: `Read exactly ${filename}` },
{ role: 'assistant', tool_calls: [{ type: 'function', function: { name: 'read_file', arguments: JSON.stringify({ path: filename }) } }] },
{ role: 'tool', content: JSON.stringify({ [old]: 'first', [alias]: 'second' }) },
{ role: 'assistant', tool_calls: [{ type: 'function', function: { name: 'Agent', arguments: JSON.stringify({ model: old, prompt: `Read ${filename}` }) } }] },
] };
const normalized = neutralPromptAliases(payload);
expect(normalized.messages.slice(0, 3)).toEqual(payload.messages.slice(0, 3));
expect(normalized.tools[0]).toEqual(payload.tools[0]);
expect(normalized.tools[1].function.parameters.properties.prompt).toEqual(payload.tools[1].function.parameters.properties.prompt);
expect(normalized.tools[1].function.description).toContain(filename);
expect(normalized.tools[1].function.description).toContain(`- ${alias} [default]:`);
const args = JSON.parse(normalized.messages[3].tool_calls[0].function.arguments);
expect(args).toEqual({ model: alias, prompt: `Read ${filename}` });
expect(normalized.tools[1].function.parameters.properties.model.enum).toEqual([alias]);
});
for (const after of [false, true]) {
test(`an unacknowledged native selection retains old definitions until retry (${after ? 'after' : 'before'} effect)`, async () => {
const f = recoveryPeer();
try {
await f.controller.tick();
const old = f.selectAlias();
const before = f.calls.length;
f.fail('select', after); f.controller.catalogAfter = 0;
await f.controller.tick();
expect(read(f.controller.configFile).model.current).toBe(after ? 'native' : old);
expect(f.menu.some(row => row.model === old)).toBe(true);
expect(read(f.controller.configFile).model.models[old]).toBeDefined();
expect(f.calls.slice(before).some(call => call.method === 'conversations.updateKimiModelList')).toBe(false);
expect(f.controller.catalogError).toBe('catalog_sync_failed');
f.clearFault(); f.controller.catalogAfter = 0;
await f.controller.tick();
expect(read(f.controller.configFile).model.current).toBe('native');
expect(f.menu.map(row => row.model)).toEqual(['native']);
expect(f.controller.catalogError).toBeNull();
} finally { await f.cleanup(); }
});
}