File size: 22,856 Bytes
d522ffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import { readFile, mkdir, writeFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { parse as parseToml, stringify as stringifyToml } from 'smol-toml';
import {
  ModelRecordSchema,
  ProviderConfigSchema,
  modelsFromToml,
  providersFromToml,
} from '@moonshot-ai/agent-core-v2/app/kosongConfig/configSection';
import { HookDefSchema } from '@moonshot-ai/agent-core-v2/features/externalHooks/configSection';
import { getConfigSectionContributions } from '@moonshot-ai/agent-core-v2/app/config/configSectionContributions';
import { getContributedFlags } from '@moonshot-ai/agent-core-v2/app/flag/flagRegistry';
import { camelToSnake } from '@moonshot-ai/agent-core-v2/app/config/toml';

import '@moonshot-ai/agent-core-v2/agent/loop/configSection';
import '@moonshot-ai/agent-core-v2/agent/task/configSection';
import '@moonshot-ai/agent-core-v2/agent/permissionMode/configSection';
import '@moonshot-ai/agent-core-v2/app/mcpConfig/configSection';
import '@moonshot-ai/agent-core-v2/app/auth/configSection';
import '@moonshot-ai/agent-core-v2/app/flag/flag';
import '@moonshot-ai/agent-core-v2/features/skill/catalog/configSection';

import '@moonshot-ai/agent-core-v2/session/subagent/flag';
import '@moonshot-ai/agent-core-v2/features/tower/flag';
import '@moonshot-ai/agent-core-v2/agent/toolSelect/flag';
import '@moonshot-ai/agent-core-v2/agent/tools/task/task-wait/flag';
import '@moonshot-ai/agent-core-v2/persistence/configSection';

import { atomicWrite } from '../atomic-write.js';
import { DEFAULT_CONFIG_FILE_TEXT, isTuiStubOrMissing } from '../stub-detect.js';
import { readSourceConfig } from '../source-config.js';
import {
  targetConfigFile,
  targetTuiFile,
  siblingConfigToml,
  siblingTuiToml,
} from '../paths.js';

// `theme` / `default_editor` belong in tui.toml, not config.toml.
const TUI_TOP_LEVEL_KEYS = new Set(['theme', 'default_editor']);
const TOP_LEVEL_KEYS_TO_DROP = new Set(['plan_mode', 'yolo']);
const LOOP_CONTROL_FIELDS_TO_KEEP = new Set([
  'reserved_context_size',
]);
const BACKGROUND_FIELDS_TO_KEEP = new Set([
  'max_running_tasks',
  'keep_alive_on_exit',
]);
const REGISTERED_EXPERIMENTAL_FLAGS: ReadonlySet<string> = new Set(
  getContributedFlags().map((definition) => definition.id),
);

// kimi-code's tui.toml `theme` enum (mirrors apps/kimi-code TuiThemeSchema).
// A legacy theme outside this set would fail loadTuiConfig()'s whole-file
// validation, taking the migrated editor command down with it — so drop it.
const TUI_THEMES: ReadonlySet<string> = new Set(['dark', 'light', 'auto']);

const SUPPORTED_PROVIDER_TYPES: ReadonlySet<string> = new Set([
  'anthropic',
  'openai',
  'kimi',
  'google-genai',
  'openai_responses',
  'vertexai',
]);

// The config.toml top-level keys kimi-code understands, derived from the v2
// config-section registry so the set tracks the v2 runtime. `providers` /
// `models` / `hooks` are filtered per-entry, not via this set. `default_model`
// / `default_provider` are unregistered-but-preserved v2 keys (the v2
// ConfigRegistry passes unregistered domains through unchanged), so they are
// kept explicitly.
const SUPPORTED_TOP_LEVEL_KEYS: ReadonlySet<string> = new Set([
  ...getConfigSectionContributions()
    .map((contribution) => contribution.domain)
    .filter((d) => d !== 'providers' && d !== 'models' && d !== 'hooks')
    .map(camelToSnake),
  'default_model',
  'default_provider',
]);

export interface ConfigStepInput {
  readonly sourceHome: string;
  readonly targetHome: string;
}

export interface ConfigStepResult {
  readonly migrated: boolean;
  readonly tuiExtracted: boolean;
  readonly droppedProviders: readonly string[];
  readonly droppedModels: readonly string[];
  /** Top-level keys dropped because kimi-code's config schema lacks them. */
  readonly droppedKeys: readonly string[];
  /**
   * Keys/sections the existing target config and the kimi-cli config both set
   * to a different value — the target's value was kept.
   */
  readonly configConflicts: readonly string[];
  /** A `config.toml` conflict forced a `config.migrated-from-kimi-cli.toml` sibling. */
  readonly wroteSiblingDueToConflict: boolean;
  /** A `tui.toml` conflict forced a `tui.migrated-from-kimi-cli.toml` sibling. */
  readonly wroteTuiSibling: boolean;
  /** Count of kimi-cli hook entries written into the LIVE target config. */
  readonly migratedHooks: number;
  /** Count of kimi-cli hook entries dropped because kimi-code's schema rejects them. */
  readonly droppedHooks: number;
  readonly sourceUnreadable: boolean;
  /** Legacy `device_id` was copied because the target had none of its own. */
  readonly deviceIdCopied: boolean;
  /**
   * When sibling mode kicks in (`wroteSiblingDueToConflict === true`), the
   * content that landed in `config.migrated-from-kimi-cli.toml` instead of
   * the live `config.toml`. Surfaced by the result screen so the user knows
   * what they need to merge by hand. Empty in `overwrite` / `merge` modes.
   */
  readonly siblingContents: {
    readonly providers: readonly string[];
    readonly models: readonly string[];
    readonly hooks: number;
  };
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === 'object' && value !== null && !Array.isArray(value);
}

function emptyResult(): ConfigStepResult {
  return {
    migrated: false,
    tuiExtracted: false,
    droppedProviders: [],
    droppedModels: [],
    droppedKeys: [],
    configConflicts: [],
    wroteSiblingDueToConflict: false,
    wroteTuiSibling: false,
    migratedHooks: 0,
    droppedHooks: 0,
    sourceUnreadable: false,
    deviceIdCopied: false,
    siblingContents: { providers: [], models: [], hooks: 0 },
  };
}

function filterFields(
  value: Record<string, unknown>,
  fieldsToKeep: ReadonlySet<string>,
): Record<string, unknown> | undefined {
  const keptEntries = Object.entries(value).filter(([field]) => fieldsToKeep.has(field));
  return keptEntries.length > 0 ? Object.fromEntries(keptEntries) : undefined;
}

function filterRegisteredExperimentalFlags(
  value: Record<string, unknown>,
): Record<string, unknown> | undefined {
  const keptEntries = Object.entries(value).filter(
    ([field, flag]) => REGISTERED_EXPERIMENTAL_FLAGS.has(field) && typeof flag === 'boolean',
  );
  return keptEntries.length > 0 ? Object.fromEntries(keptEntries) : undefined;
}

/** True when the kimi-cli provider entry validates against kimi-code's v2 schema
 * and its (already type-mapped) `type` is one the kosong runtime can construct. */
function providerIsSupported(prov: Record<string, unknown>): boolean {
  const transformed = providersFromToml({ x: prov });
  const entry = isRecord(transformed) ? transformed['x'] : undefined;
  if (entry === undefined) return false;
  try {
    ProviderConfigSchema.parse(entry);
  } catch {
    return false;
  }
  const type = isRecord(entry) ? entry['type'] : undefined;
  return typeof type === 'string' && SUPPORTED_PROVIDER_TYPES.has(type);
}

/** True when the kimi-cli model entry validates against kimi-code's v2 schema. */
function modelIsSupported(mod: Record<string, unknown>): boolean {
  const transformed = modelsFromToml({ x: mod });
  const entry = isRecord(transformed) ? transformed['x'] : undefined;
  if (entry === undefined) return false;
  try {
    ModelRecordSchema.parse(entry);
    return true;
  } catch {
    return false;
  }
}

/** Order-insensitive deep-equality key, so re-ordered tables are not conflicts. */
function stableKey(value: unknown): string {
  if (Array.isArray(value)) return `[${value.map(stableKey).join(',')}]`;
  if (isRecord(value)) {
    return `{${Object.keys(value)
      .toSorted()
      .map((k) => `${JSON.stringify(k)}:${stableKey(value[k])}`)
      .join(',')}}`;
  }
  return JSON.stringify(value) ?? 'null';
}

function deepEqual(a: unknown, b: unknown): boolean {
  return stableKey(a) === stableKey(b);
}

const LEGACY_PROVIDER_TYPE_MAP: Readonly<Record<string, string>> = {
  openai_legacy: 'openai',
  google_genai: 'google-genai',
  gemini: 'google-genai',
};

function mapLegacyProviderTypes(parsed: Record<string, unknown>): Record<string, unknown> {
  const providers = parsed['providers'];
  if (!isRecord(providers)) return parsed;
  let changed = false;
  const mapped: Record<string, unknown> = {};
  for (const [name, prov] of Object.entries(providers)) {
    if (!isRecord(prov)) {
      mapped[name] = prov;
      continue;
    }
    const mappedType =
      typeof prov['type'] === 'string' ? LEGACY_PROVIDER_TYPE_MAP[prov['type']] : undefined;
    if (mappedType === undefined) {
      mapped[name] = prov;
      continue;
    }
    mapped[name] = { ...prov, type: mappedType };
    changed = true;
  }
  return changed ? { ...parsed, providers: mapped } : parsed;
}

/**
 * Additively merge the kimi-cli config into the existing target config: add
 * keys/providers/models the target lacks, keep the target's value on a real
 * conflict, and record those conflicts. A target value is never overwritten.
 */
function mergeConfig(
  target: Record<string, unknown>,
  migrated: Record<string, unknown>,
): { merged: Record<string, unknown>; conflicts: string[] } {
  const merged: Record<string, unknown> = { ...target };
  const conflicts: string[] = [];
  for (const [key, value] of Object.entries(migrated)) {
    if ((key === 'providers' || key === 'models') && isRecord(value)) {
      const section: Record<string, unknown> = isRecord(merged[key]) ? { ...merged[key] } : {};
      for (const [name, entry] of Object.entries(value)) {
        if (section[name] === undefined) {
          section[name] = entry;
        } else if (!deepEqual(section[name], entry)) {
          conflicts.push(`${key}.${name}`);
        }
      }
      merged[key] = section;
      continue;
    }
    if (merged[key] === undefined) {
      merged[key] = value;
    } else if (!deepEqual(merged[key], value)) {
      conflicts.push(key);
    }
  }
  return { merged, conflicts };
}

export async function migrateConfigStep(input: ConfigStepInput): Promise<ConfigStepResult> {
  const source = await readSourceConfig(input.sourceHome);
  if (source.kind === 'missing') return emptyResult();
  if (source.kind === 'unreadable') {
    return { ...emptyResult(), sourceUnreadable: true };
  }
  const deviceIdCopied = await copyDeviceId(input.sourceHome, input.targetHome);
  const parsed: Record<string, unknown> = mapLegacyProviderTypes(source.parsed);

  // Decide how the target config.toml is handled: a missing or pristine-stub
  // target is overwritten; a parseable user config is merged into; an
  // unparseable target falls back to a side file (it cannot be merged).
  const configPath = targetConfigFile(input.targetHome);
  let targetText: string | undefined;
  try {
    targetText = await readFile(configPath, 'utf-8');
  } catch {
    targetText = undefined;
  }
  let targetMode: 'overwrite' | 'merge' | 'sibling';
  let targetParsed: Record<string, unknown> = {};
  if (targetText === undefined || targetText === DEFAULT_CONFIG_FILE_TEXT) {
    targetMode = 'overwrite';
  } else {
    try {
      const tp: unknown = parseToml(targetText);
      targetParsed = isRecord(tp) ? tp : {};
      targetMode = 'merge';
    } catch {
      targetMode = 'sibling';
    }
  }

  // Provider names available to migrated models: those kept by this run, plus
  // any already present in the target config being merged into.
  const availableProviderNames = new Set<string>(
    isRecord(targetParsed['providers']) ? Object.keys(targetParsed['providers']) : [],
  );

  // Model alias names already present in the target config being merged into —
  // a migrated `default_model` may legitimately point at one of these.
  const availableModelNames = new Set<string>(
    isRecord(targetParsed['models']) ? Object.keys(targetParsed['models']) : [],
  );

  // 1) Providers — keep only those kimi-code's schema accepts.
  const droppedProviders: string[] = [];
  const keptProviders: Record<string, Record<string, unknown>> = {};
  if (isRecord(parsed['providers'])) {
    for (const [name, prov] of Object.entries(parsed['providers'])) {
      if (isRecord(prov) && providerIsSupported(prov)) {
        keptProviders[name] = prov;
      } else {
        droppedProviders.push(name);
      }
    }
  }

  // Provider names the merge resolves to a DIFFERENT entry than the kimi-cli
  // one: the target already defines a same-named provider with other settings,
  // so `mergeConfig` keeps the target's. A migrated model bound to such a name
  // would silently run against the target's endpoint/credentials, not the
  // legacy ones it was configured for — so treat the name as unavailable.
  const targetProviders: Record<string, unknown> = isRecord(targetParsed['providers'])
    ? targetParsed['providers']
    : {};
  const conflictedProviderNames = new Set<string>();
  for (const [name, prov] of Object.entries(keptProviders)) {
    const targetProv = targetProviders[name];
    if (targetProv !== undefined && !deepEqual(targetProv, prov)) {
      conflictedProviderNames.add(name);
    }
  }

  // 2) Models — keep only those kimi-code's schema accepts, and not those
  //    whose provider was dropped as unsupported (they could never resolve).
  const droppedModels: string[] = [];
  const keptModels: Record<string, Record<string, unknown>> = {};
  if (isRecord(parsed['models'])) {
    for (const [name, mod] of Object.entries(parsed['models'])) {
      if (!isRecord(mod) || !modelIsSupported(mod)) {
        droppedModels.push(name);
        continue;
      }
      // `modelIsSupported` guarantees `provider` is a string. Keep the model
      // only if that provider is available — kept by this run or already in
      // the target config — and is not a name whose merged entry will differ
      // from the legacy provider this model was configured against.
      const provider = mod['provider'];
      if (
        typeof provider !== 'string' ||
        (keptProviders[provider] === undefined && !availableProviderNames.has(provider)) ||
        conflictedProviderNames.has(provider)
      ) {
        droppedModels.push(name);
        continue;
      }
      keptModels[name] = mod;
    }
  }

  for (const [name, prov] of Object.entries(keptProviders)) {
    const reasoningKey = prov['reasoning_key'];
    delete prov['reasoning_key'];
    if (typeof reasoningKey !== 'string' || reasoningKey.length === 0) continue;
    for (const mod of Object.values(keptModels)) {
      if (mod['provider'] === name && mod['reasoning_key'] === undefined) {
        mod['reasoning_key'] = reasoningKey;
      }
    }
  }

  // 2b) Hooks — keep only entries kimi-code's HookDefSchema accepts. kimi-cli
  //     and kimi-code share an identical hook shape, so a valid legacy hook
  //     passes straight through; the per-entry filter only guards against
  //     future schema drift (an event type / field kimi-code does not know).
  //     Hook fields are all single lowercase words, so — unlike providers /
  //     models — no `transformTomlData` snake→camel pass is needed first.
  let droppedHooks = 0;
  const keptHooks: unknown[] = [];
  if (Array.isArray(parsed['hooks'])) {
    for (const entry of parsed['hooks']) {
      if (HookDefSchema.safeParse(entry).success) {
        keptHooks.push(entry);
      } else {
        droppedHooks++;
      }
    }
  }

  // 3) Split out the keys that belong in tui.toml.
  const tuiEditor: Record<string, unknown> = {};
  const tuiOut: Record<string, unknown> = {
    editor: tuiEditor,
    notifications: { enabled: true, notification_condition: 'unfocused' },
  };
  const themeVal = parsed['theme'];
  if (typeof themeVal === 'string' && TUI_THEMES.has(themeVal)) {
    tuiOut['theme'] = themeVal;
  }
  const editorVal = parsed['default_editor'];
  if (typeof editorVal === 'string') {
    tuiEditor['command'] = editorVal;
  }

  // 4) Build the migrated top-level — only keys kimi-code's schema supports.
  const droppedKeys: string[] = [];
  const migratedTop: Record<string, unknown> = {};
  for (const [k, v] of Object.entries(parsed)) {
    if (k === 'providers' || k === 'models' || k === 'hooks') continue;
    if (TUI_TOP_LEVEL_KEYS.has(k)) continue;
    if (TOP_LEVEL_KEYS_TO_DROP.has(k)) continue;
    if (k === 'default_yolo') {
      // kimi-cli's `default_yolo` maps to kimi-code's `default_permission_mode`.
      if (v === true) migratedTop['default_permission_mode'] = 'yolo';
      continue;
    }
    if (!SUPPORTED_TOP_LEVEL_KEYS.has(k)) {
      droppedKeys.push(k);
      continue;
    }
    // Drop default_model unless it points at a model that will exist in the
    // written config — one kept from kimi-cli, or already in the target being
    // merged into. A dangling alias (dropped, stale, or never present) would
    // fail the next session-create.
    if (
      k === 'default_model' &&
      typeof v === 'string' &&
      keptModels[v] === undefined &&
      !availableModelNames.has(v)
    ) {
      continue;
    }
    if (k === 'loop_control' && isRecord(v)) {
      const filteredLoopControl = filterFields(v, LOOP_CONTROL_FIELDS_TO_KEEP);
      if (filteredLoopControl !== undefined) {
        migratedTop[k] = filteredLoopControl;
      }
      continue;
    }
    if (k === 'background' && isRecord(v)) {
      const filteredBackground = filterFields(v, BACKGROUND_FIELDS_TO_KEEP);
      if (filteredBackground !== undefined) {
        migratedTop[k] = filteredBackground;
      }
      continue;
    }
    if (k === 'experimental' && isRecord(v)) {
      const filteredExperimental = filterRegisteredExperimentalFlags(v);
      if (filteredExperimental !== undefined) {
        migratedTop[k] = filteredExperimental;
      }
      continue;
    }
    migratedTop[k] = v;
  }
  if (Object.keys(keptProviders).length > 0) migratedTop['providers'] = keptProviders;
  if (Object.keys(keptModels).length > 0) migratedTop['models'] = keptModels;
  if (keptHooks.length > 0) migratedTop['hooks'] = keptHooks;

  // 4b) Drop any supported top-level key whose VALUE the v2 config section
  //     rejects (e.g. `merge_all_available_skills = "yes"`). Providers/models
  //     are already validated per-entry above, so section failures here can
  //     only come from plain top-level keys. Unregistered-but-preserved keys
  //     (default_model / default_provider) have no section schema and pass.
  const sectionsBySnake = new Map(
    getConfigSectionContributions().map((contribution) => [
      camelToSnake(contribution.domain),
      contribution,
    ]),
  );
  for (const [k, v] of Object.entries(migratedTop)) {
    if (k === 'providers' || k === 'models' || k === 'hooks') continue;
    const section = sectionsBySnake.get(k);
    if (section === undefined) continue;
    const transformed =
      section.options.fromToml === undefined ? v : section.options.fromToml(v);
    try {
      section.schema.parse(transformed);
    } catch {
      delete migratedTop[k];
      droppedKeys.push(k);
    }
  }

  // 5) Write config.toml per the target mode.
  await mkdir(input.targetHome, { recursive: true, mode: 0o700 });
  let wroteConfigSibling = false;
  let configConflicts: readonly string[] = [];
  if (targetMode === 'sibling') {
    await atomicWrite(siblingConfigToml(input.targetHome), stringifyToml(migratedTop));
    wroteConfigSibling = true;
  } else if (targetMode === 'merge') {
    const { merged, conflicts } = mergeConfig(targetParsed, migratedTop);
    configConflicts = conflicts;
    await atomicWrite(configPath, stringifyToml(merged));
  } else {
    await atomicWrite(configPath, stringifyToml(migratedTop));
  }

  // 6) Write tui.toml (or a sibling if the target tui.toml is user-modified).
  const tuiPath = targetTuiFile(input.targetHome);
  const canOverwriteTui = await isTuiStubOrMissing(tuiPath);
  const renderedTui = stringifyToml(tuiOut);
  const hasThemeExtracted = tuiOut['theme'] !== undefined;
  const hasEditorExtracted = tuiEditor['command'] !== undefined;
  let wroteTuiSibling = false;
  let tuiExtracted = false;
  if (hasThemeExtracted || hasEditorExtracted) {
    if (canOverwriteTui) {
      await atomicWrite(tuiPath, renderedTui);
    } else {
      await atomicWrite(siblingTuiToml(input.targetHome), renderedTui);
      wroteTuiSibling = true;
    }
    tuiExtracted = true;
  }

  // `migratedHooks` counts hooks the runtime will actually see — i.e. hooks
  // we wrote into the LIVE `config.toml`. That happens only when:
  //  - overwrite mode (target was missing / pristine stub, we wrote fresh), or
  //  - merge mode AND the target had no `hooks` key (mergeConfig added ours).
  // In merge mode where target already declares `hooks` (any value: empty,
  // identical, different, or even non-array invalid), `mergeConfig` keeps
  // the target's value, so the source hooks never land in the live config.
  // In sibling mode the source hooks land in `config.migrated-from-kimi-cli.toml`,
  // which the runtime never reads — they're accounted for via `siblingContents`,
  // not `migratedHooks`.
  const hooksLandedInLiveConfig =
    keptHooks.length > 0 &&
    (targetMode === 'overwrite' ||
      (targetMode === 'merge' && targetParsed['hooks'] === undefined));
  const migratedHooks = hooksLandedInLiveConfig ? keptHooks.length : 0;

  // In sibling mode, enumerate what landed in the sibling file so the result
  // screen can tell the user exactly what is awaiting manual merge.
  const siblingContents =
    targetMode === 'sibling'
      ? {
          providers: Object.keys(keptProviders),
          models: Object.keys(keptModels),
          hooks: keptHooks.length,
        }
      : { providers: [] as string[], models: [] as string[], hooks: 0 };

  return {
    migrated: true,
    tuiExtracted,
    droppedProviders,
    droppedModels,
    droppedKeys,
    configConflicts,
    wroteSiblingDueToConflict: wroteConfigSibling,
    wroteTuiSibling,
    migratedHooks,
    droppedHooks,
    sourceUnreadable: false,
    deviceIdCopied,
    siblingContents,
  };
}

// Telemetry identity continuity: kimi-cli and kimi-code share the same
// `device_id` concept (uuid hex, `kfc_device_id_` user-id prefix). Copy it only
// when the target has none — a target that already launched once keeps its own.
async function copyDeviceId(sourceHome: string, targetHome: string): Promise<boolean> {
  const targetPath = join(targetHome, 'device_id');
  if (existsSync(targetPath)) return false;
  let content: string;
  try {
    content = await readFile(join(sourceHome, 'device_id'), 'utf-8');
  } catch {
    return false;
  }
  if (content.trim().length === 0) return false;
  await mkdir(targetHome, { recursive: true, mode: 0o700 });
  await writeFile(targetPath, content, { mode: 0o600 });
  return true;
}