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.
129 lines
5.8 KiB
JavaScript
129 lines
5.8 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import crypto from 'node:crypto';
|
|
|
|
const ROLE_ID = /^local-work-model-roles-[a-f0-9]{12}$/;
|
|
const conflict = () => Object.assign(Error('agent_profile_conflict'), { code: 'agent_profile_conflict' });
|
|
const record = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
|
|
function readRegularBytes(file, missing = false) {
|
|
let stat;
|
|
try { stat = fs.lstatSync(file); }
|
|
catch (error) { if (missing && error.code === 'ENOENT') return null; throw error; }
|
|
if (!stat.isFile()) throw conflict();
|
|
const descriptor = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
|
|
try { return fs.readFileSync(descriptor); }
|
|
finally { fs.closeSync(descriptor); }
|
|
}
|
|
|
|
function declarativeManifest(registryFile, id) {
|
|
const managed = path.join(path.dirname(registryFile), 'managed');
|
|
const root = path.join(managed, id);
|
|
try {
|
|
if (!fs.lstatSync(managed).isDirectory() || !fs.lstatSync(root).isDirectory()) return false;
|
|
const value = JSON.parse(readRegularBytes(path.join(root, 'kimi.plugin.json')).toString('utf8'));
|
|
return record(value) && Object.keys(value).length === 4
|
|
&& value.name === id && value.version === '1.0.0' && value.description === 'Local Work model roles'
|
|
&& Array.isArray(value.agents) && value.agents.length === 1 && value.agents[0] === './agents';
|
|
} catch { return false; }
|
|
}
|
|
|
|
// Exact string equality keeps deleted-source recovery: the caller's literal
|
|
// source path is trusted without any existence check. Only different paths
|
|
// require a proven existing-directory equivalence.
|
|
function canonicalDirectory(file) {
|
|
try {
|
|
const canonical = fs.realpathSync(file);
|
|
// A file (or a symlink resolving to a file) is never directory equivalence.
|
|
return fs.statSync(canonical).isDirectory() ? canonical : null;
|
|
} catch { return null; }
|
|
}
|
|
|
|
function sourceMatches(sourceDirectory, originalSource) {
|
|
if (originalSource === sourceDirectory) return true;
|
|
const a = canonicalDirectory(sourceDirectory), b = canonicalDirectory(originalSource);
|
|
return a !== null && b !== null && a === b;
|
|
}
|
|
|
|
function rootMatches(registryFile, id, entryRoot) {
|
|
const expectedRoot = path.join(path.dirname(registryFile), 'managed', id);
|
|
if (entryRoot === expectedRoot) return true;
|
|
try {
|
|
// Ancestor aliases are allowed, but a direct symlink at the persisted root
|
|
// or at its immediate managed parent is never trusted.
|
|
if (!fs.lstatSync(entryRoot).isDirectory()) return false;
|
|
if (!fs.lstatSync(path.dirname(entryRoot)).isDirectory()) return false;
|
|
// Canonical WHOLE-root equality: a sibling directory under the same managed
|
|
// parent (or any other physical root) is never accepted.
|
|
return fs.realpathSync(entryRoot) === fs.realpathSync(expectedRoot);
|
|
} catch { return false; }
|
|
}
|
|
|
|
function snapshot({ registryFile, sourceDirectory }) {
|
|
if (typeof registryFile !== 'string' || !registryFile || typeof sourceDirectory !== 'string' || !sourceDirectory) {
|
|
throw conflict();
|
|
}
|
|
const bytes = readRegularBytes(registryFile, true);
|
|
if (bytes === null) return { bytes, document: null, entries: [], conflicts: [] };
|
|
const document = JSON.parse(bytes.toString('utf8'));
|
|
if (!record(document) || document.version !== 1 || !Array.isArray(document.plugins)) throw conflict();
|
|
const candidates = document.plugins.filter(entry => record(entry) && typeof entry.id === 'string' && ROLE_ID.test(entry.id));
|
|
const counts = new Map();
|
|
for (const entry of candidates) counts.set(entry.id, (counts.get(entry.id) || 0) + 1);
|
|
const entries = [], conflicts = [];
|
|
for (const entry of candidates) {
|
|
if (counts.get(entry.id) !== 1
|
|
|| entry.source !== 'local-path' || !sourceMatches(sourceDirectory, entry.originalSource)
|
|
|| !rootMatches(registryFile, entry.id, entry.root)
|
|
|| typeof entry.enabled !== 'boolean' || !declarativeManifest(registryFile, entry.id)) {
|
|
if (!conflicts.includes(entry.id)) conflicts.push(entry.id);
|
|
} else entries.push(entry);
|
|
}
|
|
return { bytes, document, entries, conflicts };
|
|
}
|
|
|
|
const inspection = saved => ({
|
|
owned: saved.entries.map(({ id, enabled }) => ({ id, enabled })), conflicts: saved.conflicts,
|
|
});
|
|
|
|
/** Discover role plugins from caller-supplied native paths; perform no RPC. */
|
|
export function inspectRolePlugins(options) {
|
|
try { return inspection(snapshot(options)); }
|
|
catch { throw conflict(); }
|
|
}
|
|
|
|
function publish(registryFile, bytes, expected) {
|
|
const temp = registryFile + '.' + process.pid + '.' + crypto.randomBytes(6).toString('hex') + '.tmp';
|
|
let created = false;
|
|
try {
|
|
const descriptor = fs.openSync(temp, 'wx', 0o600);
|
|
created = true;
|
|
try { fs.writeFileSync(descriptor, bytes); }
|
|
finally { fs.closeSync(descriptor); }
|
|
const current = readRegularBytes(registryFile, true);
|
|
if (current === null || !current.equals(expected)) throw conflict();
|
|
fs.renameSync(temp, registryFile);
|
|
} finally {
|
|
if (created) { try { fs.unlinkSync(temp); } catch (error) { if (error.code !== 'ENOENT') throw error; } }
|
|
}
|
|
}
|
|
|
|
/** Disable only selected owned registrations. Disk state does not acknowledge
|
|
* a loaded Kimi session: the caller retains its pending runtime-refresh work. */
|
|
export function disableRolePluginsSync(options) {
|
|
try {
|
|
const { registryFile, sourceDirectory, ids = [] } = options;
|
|
if (!Array.isArray(ids)) throw conflict();
|
|
const saved = snapshot({ registryFile, sourceDirectory });
|
|
const selected = new Set(ids), disabled = [];
|
|
for (const entry of saved.entries) {
|
|
if (selected.has(entry.id) && entry.enabled) {
|
|
entry.enabled = false;
|
|
disabled.push(entry.id);
|
|
}
|
|
}
|
|
if (disabled.length) publish(registryFile, Buffer.from(JSON.stringify(saved.document, null, 2) + '\n'), saved.bytes);
|
|
return { changed: disabled.length > 0, disabled, ...inspection(saved) };
|
|
} catch { throw conflict(); }
|
|
}
|