Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

32.1 KB
Raw Blame
/**
 * @file Generates `router.preset.ini` from the per-model `model.yml` files under
 * `<providerPath>/models/<modelId>/model.yml`.
 *
 * The file has a `[*]` section of engine-wide defaults, then one `[<modelId>]`
 * section per model. Only values that differ from llama.cpp's own defaults are
 * written, so the preset stays intent-revealing and a setting left alone keeps
 * whatever upstream (or the GGUF) decided. `load-on-startup = false` is the one
 * unconditional line.
 *
 * An ini key is the CLI flag with its leading dashes removed, resolved by
 * `common/preset.cpp` `get_map_key_opt`. Defaults quoted below are from
 * `common/common.h` at the pinned build.
 */

import { fs, joinPath } from '@janhq/core'
import { findModelEntries } from './model-tree'
import { parseLoadMode } from './load-mode'
import type { LlamacppConfig, ModelConfig } from '@janhq/tauri-plugin-llamacpp-api'

// ModelConfig is intentionally widened — model.yml may carry extra fields like
// `chat_template` that aren't yet in the strict typing.
type ModelYaml = ModelConfig & {
  chat_template?: string
  grammar?: string
  ctx_size?: number
  n_gpu_layers?: number
  flash_attn?: string
  cache_type_k?: string
  cache_type_v?: string
  load_mode?: string
  parallel?: number
  cont_batching?: boolean
  pooling?: 'none' | 'mean' | 'cls' | 'last' | 'rank'
  ubatch_size?: number
  batch_size?: number
  mtp_layers?: number
  mtp?: boolean
  mtp_model_path?: string
  spec_type?: string
  temperature?: number
  top_k?: number
  top_p?: number
  min_p?: number
  repeat_last_n?: number
  repeat_penalty?: number
  presence_penalty?: number
  frequency_penalty?: number
  spec_draft_n_max?: number
  spec_draft_n_min?: number
  spec_draft_p_min?: number
  spec_draft_sampling?: string
  spec_draft_n_ubatch?: number
  cpu_moe?: boolean
  n_cpu_moe?: number
  n_cpu_ffn?: number
  moe_cache_mib?: number
  moe_cache_cpu_share?: number
  expert_count?: number
  no_kv_offload?: boolean
  override_tensor?: string
  mmproj_offload?: boolean
}

/**
 * The ubatch every embedding model's preset section is pinned to.
 *
 * Exported because `embed()` has to budget its batches against the *embedder's*
 * ubatch, not the engine-wide setting: llama.cpp rejects a batch wider than
 * n_ubatch outright, with no retry path.
 */
export const DEFAULT_EMBEDDING_UBATCH = 2048

/**
 * Where each thread's saved KV cache lives.
 *
 * Under the provider directory rather than a temp dir: it is a cache the user
 * paid prefill time for and expects to survive a reboot, which is the whole
 * point. Shared verbatim with the Rust side, which llama.cpp joins file names
 * onto, so the two must not compute it differently -- this function is the one
 * definition.
 */
export function threadCacheDir(providerPath: string): string {
  const sep = isWindowsPath(providerPath) ? '\\' : '/'
  return `${providerPath.replace(/[\\/]+$/, '')}${sep}thread-cache`
}

/**
 * Whether a path is rooted the Windows way: a drive letter, a UNC share, or an
 * extended `\\?\` prefix.
 *
 * Tested on the root rather than on "contains a backslash" because `\` is a
 * legal filename character on Linux. Win32 normalizes `/` to `\` for the first
 * two, but *not* for an extended path, where a `/` is a literal name character
 * -- `create_dir_all` then fails with ERROR_INVALID_NAME and the worker exits
 * before it serves anything.
 */
function isWindowsPath(p: string): boolean {
  return /^(?:[A-Za-z]:|\\\\)/.test(p)
}

// Fallback context size when the user hasn't set one, to avoid loading a
// model's full trained context (which can OOM on large-context models).
const DEFAULT_CTX_SIZE = 8192

/**
 * The `--spec-type` values llama.cpp accepts for a draft model
 * (common/speculative.cpp). A model.yml records one at import; anything else
 * -- an older install with no record, or a value we do not recognise -- falls
 * back to MTP, which is what every embedded-head model is.
 */
const SPEC_TYPES = new Set([
  'draft-mtp',
  'draft-eagle3',
  'draft-dflash',
  'draft-dspark',
])
const DEFAULT_SPEC_TYPE = 'draft-mtp'

/**
 * The non-default `--spec-draft-sampling` mode (common/arg.cpp, added in
 * 0.7.0). `greedy` is upstream's default and is never written, so a model
 * keeps tracking it.
 */
const NON_DEFAULT_DRAFT_SAMPLING = 'probabilistic'

/**
 * A built-in template name (`chatml`, `llama3`, ...) as opposed to a template
 * body. `--chat-template` takes either, but `--chat-template-file` reads its
 * value as a path, so the two have to be told apart. Any real jinja carries
 * `{`, whitespace or a newline, none of which match here.
 */
const BUILTIN_TEMPLATE_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/

// Absolute paths only: a relative one would resolve against llama.cpp's cwd,
// which is not something the user can predict. Covers POSIX, drive-letter and
// UNC forms.
const ABSOLUTE_PATH_RE = /^(?:\/|[A-Za-z]:[\\/]|\\\\)/

/**
 * When a setting value is an absolute path to an existing file, the preset
 * passes it through to the corresponding `*-file` flag instead of treating it
 * as an inline body.
 */
async function existingFilePath(value: string): Promise<string | null> {
  if (!ABSOLUTE_PATH_RE.test(value)) return null
  try {
    return (await fs.existsSync(value)) ? value : null
  } catch {
    return null
  }
}

/**
 * A user's Parallel Sequences value as a slot count, or undefined for auto.
 * Settings saved by older builds can hold the number as a string, so both the
 * emission and the speculative-decoding pin read it through here: if they
 * disagreed, a string '2' would suppress the pin without ever being emitted,
 * and llama.cpp would resolve auto to 4 slots anyway.
 */
function explicitParallel(v: unknown): number | undefined {
  const n = typeof v === 'string' && v.trim() !== '' ? Number(v) : v
  if (typeof n !== 'number' || !Number.isFinite(n)) return undefined
  const slots = Math.floor(n)
  return slots > 0 ? slots : undefined
}

function escapeIniValue(v: string): string {
  // INI values for llama-server are read as strings; trim surrounding whitespace
  // and strip stray newlines that would break parsing.
  return String(v).replace(/[\r\n]+/g, ' ').trim()
}

/**
 * Walks `<providerPath>/models/*` recursively for `model.yml` files and emits
 * a router preset INI to `<providerPath>/router.preset.ini`. Returns the
 * absolute path of the written file.
 */
export async function generatePreset(
  providerPath: string,
  janDataFolderPath: string,
  config: LlamacppConfig
): Promise<{ path: string; embeddingCount: number }> {
  const modelsDir = await joinPath([providerPath, 'models'])

  // Ensure the directory exists; an empty install is fine — we still emit a
  // valid (mostly empty) preset so the router can start.
  if (!(await fs.existsSync(modelsDir))) {
    await fs.mkdir(modelsDir)
  }

  // A model.yml at the models root itself has no ID to key a section on.
  const modelEntries = (await findModelEntries(modelsDir)).filter(
    (e) => e.modelId.length > 0
  )

  modelEntries.sort((a, b) => a.modelId.localeCompare(b.modelId))

  const kvUnifiedIsAuto =
    config.kv_unified !== 'on' && config.kv_unified !== 'off'

  const lines: string[] = []

  // Emit only values that differ from llama.cpp's compiled defaults so the
  // preset stays minimal and intent-revealing. Defaults sourced from
  // tools/server/README.md.
  lines.push('[*]')
  // fit default = 'on'
  if (config.fit === false) {
    lines.push('fit = off')
  }
  // fit-target default = '1024' (MiB per device)
  if (
    typeof config.fit_target === 'string' &&
    config.fit_target.length > 0 &&
    config.fit_target !== '1024'
  ) {
    lines.push(`fit-target = ${escapeIniValue(config.fit_target)}`)
  }
  // fit-ctx default = 4096
  const fitCtxNum =
    typeof config.fit_ctx === 'number'
      ? config.fit_ctx
      : typeof config.fit_ctx === 'string' && config.fit_ctx.length > 0
        ? Number(config.fit_ctx)
        : NaN
  if (Number.isFinite(fitCtxNum) && fitCtxNum > 0 && fitCtxNum !== 4096) {
    lines.push(`fit-ctx = ${fitCtxNum}`)
  }
  // ctx-size: llama.cpp's own default loads the model's full trained context,
  // which can OOM on a large-context model, so a conservative cap stands in.
  // There is no engine-level context setting to read here -- per-model
  // `ctx_len` owns that -- so this is purely the guard.
  //
  // Skipped when auto-fit is on: emitting it would make n_ctx non-zero for
  // every model and stop fit from reducing context to make a model fit.
  const fitEnabled = config.fit !== false
  if (!fitEnabled) {
    lines.push(`ctx-size = ${DEFAULT_CTX_SIZE}`)
  }
  // flash-attn default = 'auto'; explicit on/off only.
  if (
    typeof config.flash_attn === 'string' &&
    (config.flash_attn === 'on' || config.flash_attn === 'off')
  ) {
    lines.push(`flash-attn = ${config.flash_attn}`)
  }
  // cache-type-k/v default = 'f16'
  if (
    typeof config.cache_type_k === 'string' &&
    config.cache_type_k.length > 0 &&
    config.cache_type_k !== 'f16'
  ) {
    lines.push(`cache-type-k = ${escapeIniValue(config.cache_type_k)}`)
  }
  if (
    typeof config.cache_type_v === 'string' &&
    config.cache_type_v.length > 0 &&
    config.cache_type_v !== 'f16'
  ) {
    lines.push(`cache-type-v = ${escapeIniValue(config.cache_type_v)}`)
  }
  // parallel default = 0 (llama.cpp's own auto resolution); a positive user
  // value is intent and is emitted verbatim. Jan adds no hidden slot of its
  // own: every pinned surface shares slot 0 and is told apart by `thread_id`
  // (web-app/src/constants/models.ts), so no slot has to be reserved for one.
  const globalParallel = explicitParallel(config.parallel)
  if (globalParallel !== undefined) {
    lines.push(`parallel = ${globalParallel}`)
    // llama.cpp only turns on unified KV as part of resolving parallel = -1;
    // passing parallel explicitly leaves it off, which splits ctx-size into
    // ctx-size/parallel per slot. Restore the auto behaviour so the configured
    // context is what each slot actually gets.
    if (kvUnifiedIsAuto) lines.push('kv-unified = true')
  }
  // cont-batching default = true; emit only the explicit-off case.
  if (config.cont_batching === false) {
    lines.push('cont-batching = false')
  }
  // threads default = -1 (logical cores)
  if (
    typeof config.threads === 'number' &&
    Number.isFinite(config.threads) &&
    config.threads > 0
  ) {
    lines.push(`threads = ${Math.floor(config.threads)}`)
  }
  // threads-batch default = same as threads; emit only if positive and distinct.
  if (
    typeof config.threads_batch === 'number' &&
    Number.isFinite(config.threads_batch) &&
    config.threads_batch > 0 &&
    config.threads_batch !== config.threads
  ) {
    lines.push(`threads-batch = ${Math.floor(config.threads_batch)}`)
  }
  // n-predict default = -1 (infinity)
  if (
    typeof config.n_predict === 'number' &&
    Number.isFinite(config.n_predict) &&
    config.n_predict !== -1
  ) {
    lines.push(`n-predict = ${Math.floor(config.n_predict)}`)
  }
  // batch-size default = 2048 (common.h: n_batch)
  if (
    typeof config.batch_size === 'number' &&
    Number.isFinite(config.batch_size) &&
    config.batch_size > 0 &&
    config.batch_size !== 2048
  ) {
    lines.push(`batch-size = ${Math.floor(config.batch_size)}`)
  }
  // ubatch-size default = 512
  if (
    typeof config.ubatch_size === 'number' &&
    Number.isFinite(config.ubatch_size) &&
    config.ubatch_size > 0 &&
    config.ubatch_size !== 512
  ) {
    lines.push(`ubatch-size = ${Math.floor(config.ubatch_size)}`)
  }
  // n-cpu-moe default = 0 (no MoE weights pinned to the host)
  if (
    typeof config.n_cpu_moe === 'number' &&
    Number.isFinite(config.n_cpu_moe) &&
    config.n_cpu_moe > 0
  ) {
    lines.push(`n-cpu-moe = ${Math.floor(config.n_cpu_moe)}`)
  }
  // n-cpu-ffn default = 0. The dense-model counterpart of n-cpu-moe, added in
  // llama.cpp 0.4.0; the two are independent and a model can want either.
  if (
    typeof config.n_cpu_ffn === 'number' &&
    Number.isFinite(config.n_cpu_ffn) &&
    config.n_cpu_ffn > 0
  ) {
    lines.push(`n-cpu-ffn = ${Math.floor(config.n_cpu_ffn)}`)
  }
  // no-kv-offload default = false (the cache is offloaded). Spelled negatively
  // to match llama.cpp's own flag.
  // common_preset's parse_bool_arg recognises `no-kv-offload` as the negated
  // half of the kv-offload pair (arg.cpp:2417-2424) and inverts it, so `true`
  // here does disable offloading.
  if (config.no_kv_offload === true) {
    lines.push('no-kv-offload = true')
  }
  // tensor-split default = empty (even split across devices).
  if (
    typeof config.tensor_split === 'string' &&
    config.tensor_split.trim().length > 0
  ) {
    lines.push(`tensor-split = ${escapeIniValue(config.tensor_split.trim())}`)
  }
  // no-op-offload default = false (host tensor ops are offloaded).
  if (config.no_op_offload === true) {
    lines.push('no-op-offload = true')
  }
  // ctx-checkpoints default = 32, checkpoint-min-step default = 8192.
  if (
    typeof config.ctx_checkpoints === 'number' &&
    Number.isFinite(config.ctx_checkpoints) &&
    config.ctx_checkpoints >= 0 &&
    config.ctx_checkpoints !== 32
  ) {
    lines.push(`ctx-checkpoints = ${Math.floor(config.ctx_checkpoints)}`)
  }
  if (
    typeof config.checkpoint_min_step === 'number' &&
    Number.isFinite(config.checkpoint_min_step) &&
    config.checkpoint_min_step >= 0 &&
    config.checkpoint_min_step !== 8192
  ) {
    lines.push(`checkpoint-min-step = ${Math.floor(config.checkpoint_min_step)}`)
  }
  // device default = empty (auto-pick)
  if (typeof config.device === 'string' && config.device.trim().length > 0) {
    lines.push(`device = ${escapeIniValue(config.device)}`)
  }
  // split-mode default = 'layer'
  if (
    typeof config.split_mode === 'string' &&
    config.split_mode.length > 0 &&
    config.split_mode !== 'layer'
  ) {
    lines.push(`split-mode = ${escapeIniValue(config.split_mode)}`)
  }
  // main-gpu default = 0
  if (
    typeof config.main_gpu === 'number' &&
    Number.isFinite(config.main_gpu) &&
    config.main_gpu > 0
  ) {
    lines.push(`main-gpu = ${Math.floor(config.main_gpu)}`)
  }
  // `--mlock`, `--mmap`/`--no-mmap` and `--direct-io` were deprecated aliases of
  // the single params.load_mode field and were deleted outright in 0.4.1, so
  // `load-mode` is the only spelling llama.cpp understands. default = auto, so
  // only a non-auto value is named here; a model section can still name `auto`
  // to opt out of a non-auto global.
  const loadMode = parseLoadMode(config.load_mode)
  if (loadMode !== null && loadMode !== 'auto') {
    lines.push(`load-mode = ${loadMode}`)
  }
  // rope-scaling default is UNSPECIFIED (let the model decide), not 'none'.
  // Treating 'none' as the default made "None" -- an explicit request to
  // disable scaling -- impossible to express.
  if (
    typeof config.rope_scaling === 'string' &&
    config.rope_scaling.length > 0 &&
    config.rope_scaling !== 'auto'
  ) {
    lines.push(`rope-scaling = ${escapeIniValue(config.rope_scaling)}`)
  }
  // rope-freq-base default = 0 (loaded from model).
  if (
    typeof config.rope_freq_base === 'number' &&
    Number.isFinite(config.rope_freq_base) &&
    config.rope_freq_base > 0
  ) {
    lines.push(`rope-freq-base = ${config.rope_freq_base}`)
  }
  // rope-freq-scale default = 0 (loaded from model). 1.0 is not the default: it
  // is an explicit "force no scaling", so it must still be emitted. `rope_scale`
  // is gone -- it was a second spelling of this same upstream field (as 1/N).
  if (
    typeof config.rope_freq_scale === 'number' &&
    Number.isFinite(config.rope_freq_scale) &&
    config.rope_freq_scale > 0
  ) {
    lines.push(`rope-freq-scale = ${config.rope_freq_scale}`)
  }
  // context-shift default = disabled
  if (config.ctx_shift === true) {
    lines.push('context-shift = true')
  }
  // cache-ram default = 8192 MiB
  if (
    typeof config.cache_ram === 'number' &&
    Number.isFinite(config.cache_ram) &&
    config.cache_ram !== 8192
  ) {
    lines.push(`cache-ram = ${Math.floor(config.cache_ram)}`)
  }
  // lazy-mode default = auto (on for arch-marked tensors above 4 GiB). Needs
  // mmap, so it is silently inert under a load-mode that does not map the file;
  // `off` is the pre-0.4.0 behaviour of always keeping those tensors resident.
  if (config.lazy_mode === 'on' || config.lazy_mode === 'off') {
    lines.push(`lazy-mode = ${config.lazy_mode}`)
  }
  // slot-save-path has no default: naming it is what enables llama.cpp's slot
  // save/restore routes at all. Emitted even with the feature off, so the worker
  // knows which directory to keep clear and can still erase a deleted thread's
  // state; the *budget* is what decides whether anything is written. The C++ arg
  // handler throws if the directory is missing -- the worker creates it first.
  lines.push(`slot-save-path = ${escapeIniValue(threadCacheDir(providerPath))}`)
  // cache-reuse default = 0 (disabled)
  if (
    typeof config.cache_reuse === 'number' &&
    Number.isFinite(config.cache_reuse) &&
    config.cache_reuse > 0
  ) {
    lines.push(`cache-reuse = ${Math.floor(config.cache_reuse)}`)
  }
  if (config.swa_full === true) {
    lines.push('swa-full = true')
  }
  // auto is handled next to each `parallel` emission above; with no explicit
  // parallel the flag is omitted so llama.cpp's own auto resolution applies.
  if (config.kv_unified === 'on') {
    lines.push('kv-unified = true')
  } else if (config.kv_unified === 'off') {
    lines.push('kv-unified = false')
  }
  // kv-unified-per-slot default = unset. Caps one slot's share of the shared KV
  // pool, which is what bounds a single conversation once the pool is unified
  // and every surface shares a slot. Only sizes the pool itself when no
  // ctx-size is pinned (server.cpp:160-170, mirrored in the shim).
  if (
    typeof config.kv_unified_per_slot === 'number' &&
    Number.isFinite(config.kv_unified_per_slot) &&
    config.kv_unified_per_slot > 0
  ) {
    lines.push(`kv-unified-per-slot = ${Math.floor(config.kv_unified_per_slot)}`)
  }
  // reasoning-preserve defaults to on since 0.4.0, for any template advertising
  // `supports_preserve_reasoning`. Emitted only to turn it off, which restores
  // the template's own default and keeps a per-request `chat_template_kwargs`
  // (Jan's per-model `preserve_thinking`) the authoritative control.
  if (config.reasoning_preserve === false) {
    lines.push('reasoning-preserve = false')
  }
  // keep default = 0
  if (
    typeof config.keep === 'number' &&
    Number.isFinite(config.keep) &&
    config.keep !== 0
  ) {
    lines.push(`keep = ${Math.floor(config.keep)}`)
  }
  lines.push('')

  // ---------- per-model sections ----------
  const moeCacheAllowed =
    config.split_mode !== 'tensor' &&
    !(
      typeof config.device === 'string' &&
      config.device.trim().toLowerCase() === 'none'
    )
  let embeddingCount = 0
  for (const { modelId, configPath } of modelEntries) {
    let mc: ModelYaml
    try {
      mc = await fs.readYaml<ModelYaml>(configPath)
    } catch {
      // Skip unreadable model entries rather than aborting the whole preset.
      continue
    }

    if (!mc?.model_path) continue

    const modelAbs = await joinPath([janDataFolderPath, mc.model_path])

    lines.push(`[${modelId}]`)
    lines.push(`model = ${escapeIniValue(modelAbs)}`)

    if (mc.mmproj_path) {
      const mmprojAbs = await joinPath([janDataFolderPath, mc.mmproj_path])
      lines.push(`mmproj = ${escapeIniValue(mmprojAbs)}`)
    }

    // A template body cannot survive the ini: values have no line
    // continuation, and `#`/`;` anywhere in one starts a comment. So an
    // absolute path to an existing file passes through as-is, a built-in name
    // goes inline, and anything else is written beside model.yml and passed by
    // path, which llama.cpp reads verbatim.
    const chatTemplate =
      typeof mc.chat_template === 'string' ? mc.chat_template.trim() : ''
    if (chatTemplate.length > 0) {
      const templateFile = await existingFilePath(chatTemplate)
      if (templateFile) {
        lines.push(`chat-template-file = ${escapeIniValue(templateFile)}`)
      } else if (BUILTIN_TEMPLATE_NAME_RE.test(chatTemplate)) {
        lines.push(`chat-template = ${chatTemplate}`)
      } else {
        const templatePath = await joinPath([
          modelsDir,
          modelId,
          'chat_template.jinja',
        ])
        await fs.writeFileSync(templatePath, chatTemplate)
        lines.push(`chat-template-file = ${escapeIniValue(templatePath)}`)
      }
    }

    // GBNF uses `#` for comments and is usually multi-line, so an inline body
    // can never go through the ini; it always reaches llama.cpp as a file.
    const grammar = typeof mc.grammar === 'string' ? mc.grammar.trim() : ''
    if (grammar.length > 0) {
      const grammarFile = await existingFilePath(grammar)
      if (grammarFile) {
        lines.push(`grammar-file = ${escapeIniValue(grammarFile)}`)
      } else {
        const grammarPath = await joinPath([modelsDir, modelId, 'grammar.gbnf'])
        await fs.writeFileSync(grammarPath, grammar)
        lines.push(`grammar-file = ${escapeIniValue(grammarPath)}`)
      }
    }

    // Per-model overrides -- same default-skipping rules as the [*] block.
    // An explicit 0 means "native" (load the model's own trained context).
    //
    // Emitted even with auto-fit on, unlike n-gpu-layers below: upstream's fit
    // explicitly leaves a user-set context alone (common/fit.cpp: "context size
    // set by user -> no change") and only bails on an explicit n_gpu_layers. The
    // old gate is why "Increase Context Size" did nothing while Fit was on.
    let ctxEmitted = false
    if (
      typeof mc.ctx_size === 'number' &&
      Number.isFinite(mc.ctx_size) &&
      mc.ctx_size >= 0
    ) {
      lines.push(`ctx-size = ${mc.ctx_size}`)
      ctxEmitted = true
    }
    // Skipped when auto-fit is on: an explicit n-gpu-layers makes fit abort its
    // layer-offload computation. -1 is auto and -2 or below means all layers,
    // so the floor is -2 rather than 0.
    if (
      !fitEnabled &&
      typeof mc.n_gpu_layers === 'number' &&
      mc.n_gpu_layers >= -2
    ) {
      lines.push(`n-gpu-layers = ${mc.n_gpu_layers}`)
    }
    if (
      typeof mc.flash_attn === 'string' &&
      (mc.flash_attn === 'on' || mc.flash_attn === 'off')
    ) {
      lines.push(`flash-attn = ${mc.flash_attn}`)
    }
    if (
      typeof mc.cache_type_k === 'string' &&
      mc.cache_type_k.length > 0 &&
      mc.cache_type_k !== 'f16'
    ) {
      lines.push(`cache-type-k = ${escapeIniValue(mc.cache_type_k)}`)
    }
    if (
      typeof mc.cache_type_v === 'string' &&
      mc.cache_type_v.length > 0 &&
      mc.cache_type_v !== 'f16'
    ) {
      lines.push(`cache-type-v = ${escapeIniValue(mc.cache_type_v)}`)
    }
    // MTP either lives in the main gguf (mtp_layers > 0) or ships as a separate
    // draft gguf (mtp_model_path), which is passed to the engine as the draft.
    const hasMtpModel =
      typeof mc.mtp_model_path === 'string' && mc.mtp_model_path.length > 0
    const hasMtpLayers =
      typeof mc.mtp_layers === 'number' && mc.mtp_layers > 0
    const specDecoding = mc.mtp === true && (hasMtpLayers || hasMtpModel)

    const modelParallel = explicitParallel(mc.parallel)
    if (modelParallel !== undefined) {
      lines.push(`parallel = ${modelParallel}`)
      if (kvUnifiedIsAuto) lines.push('kv-unified = true')
    } else if (
      specDecoding &&
      explicitParallel(config.parallel) === undefined
    ) {
      // A model with speculative decoding gets one slot when parallel is auto.
      // Every slot of a hybrid model (qwen35) carries (1 + spec-draft-n-max)
      // rows of recurrent state for draft rollback -- about 600 MiB each on a
      // 27B -- so llama.cpp's auto resolution to 4 slots costs ~1.2 GB more
      // VRAM than the 2 Jan used before 0.8.5. On Windows the NVIDIA driver
      // then spills into shared system RAM instead of failing, and decode
      // drops 4x (59 -> 14 tok/s on a 12 GB card). Jan's own surfaces share
      // slot 0 anyway. Only a positive value, global or per-model, counts as
      // the user's: auto (0) is also what the 0.8.5 migration wrote, so the
      // two cannot be told apart. A legacy numeric string still counts, and is
      // emitted in [*] above (see explicitParallel).
      lines.push('parallel = 1')
      if (kvUnifiedIsAuto) lines.push('kv-unified = true')
    }
    // Unlike [*], `auto` is emitted: it is a real override of a non-auto
    // global. Unset (empty in the UI) means inherit, so nothing is written.
    const modelLoadMode = parseLoadMode(mc.load_mode)
    if (modelLoadMode !== null) {
      lines.push(`load-mode = ${modelLoadMode}`)
    }
    if (mc.cont_batching === false) {
      lines.push('cont-batching = false')
    }
    if (
      typeof mc.batch_size === 'number' &&
      mc.batch_size > 0 &&
      mc.batch_size !== 2048
    ) {
      lines.push(`batch-size = ${Math.floor(mc.batch_size)}`)
    }
    if (
      typeof mc.ubatch_size === 'number' &&
      mc.ubatch_size > 0 &&
      mc.ubatch_size !== 512
    ) {
      lines.push(`ubatch-size = ${Math.floor(mc.ubatch_size)}`)
    }
    if (mc.cpu_moe === true) {
      lines.push('cpu-moe = true')
    }
    if (typeof mc.n_cpu_moe === 'number' && mc.n_cpu_moe > 0) {
      lines.push(`n-cpu-moe = ${Math.floor(mc.n_cpu_moe)}`)
    }
    if (typeof mc.n_cpu_ffn === 'number' && mc.n_cpu_ffn > 0) {
      lines.push(`n-cpu-ffn = ${Math.floor(mc.n_cpu_ffn)}`)
    }
    // moe-cache-mib default = 0 (off): a GPU cache for the experts that
    // cpu-moe / n-cpu-moe keep in host memory, added in 0.7.0. Per-model only,
    // never in [*]: llama.cpp refuses to load a model without experts when it
    // is set ("MoE cache requires a MoE model", llama-moe-cache.cpp), so an
    // engine-wide value would break every dense and embedding model. For the
    // same reason it is emitted only when the GGUF declares experts
    // (`expert_count`, recorded at import); a model whose count is not yet
    // known is treated as dense, since that only skips the cache. The same
    // file also throws under tensor split mode and with no GPU backend, so the
    // key is dropped where the preset can see either: it would only turn a
    // working CPU-offloaded load into a failed one.
    if (
      moeCacheAllowed &&
      typeof mc.expert_count === 'number' &&
      mc.expert_count > 0 &&
      typeof mc.moe_cache_mib === 'number' &&
      Number.isFinite(mc.moe_cache_mib) &&
      mc.moe_cache_mib > 0
    ) {
      lines.push(`moe-cache-mib = ${Math.floor(mc.moe_cache_mib)}`)
      // Read only while building that cache; the parser throws outside [0, 1].
      const share = mc.moe_cache_cpu_share
      if (typeof share === 'number' && share > 0 && share <= 1) {
        lines.push(`moe-cache-cpu-share = ${share}`)
      }
    }
    if (mc.no_kv_offload === true) {
      // INI key is the negated form; parse_bool_arg flips it server-side.
      // Writing `no-kv-offload = true` => kv-offload disabled.
      lines.push('no-kv-offload = true')
    }
    if (typeof mc.override_tensor === 'string' && mc.override_tensor.trim().length > 0) {
      lines.push(`override-tensor = ${escapeIniValue(mc.override_tensor)}`)
    }
    // mmproj-offload defaults to on; only emit when explicitly disabled.
    if (mc.mmproj_offload === false) {
      lines.push('mmproj-offload = false')
    }

    if (specDecoding) {
      const specType =
        typeof mc.spec_type === 'string' && SPEC_TYPES.has(mc.spec_type)
          ? mc.spec_type
          : DEFAULT_SPEC_TYPE
      lines.push(`spec-type = ${specType}`)
      if (hasMtpModel) {
        const mtpAbs = await joinPath([janDataFolderPath, mc.mtp_model_path!])
        lines.push(`spec-draft-model = ${escapeIniValue(mtpAbs)}`)
      }
      if (
        typeof mc.spec_draft_n_max === 'number' &&
        mc.spec_draft_n_max > 0
      ) {
        lines.push(`spec-draft-n-max = ${Math.floor(mc.spec_draft_n_max)}`)
      }
      if (
        typeof mc.spec_draft_n_min === 'number' &&
        mc.spec_draft_n_min >= 0
      ) {
        lines.push(`spec-draft-n-min = ${Math.floor(mc.spec_draft_n_min)}`)
      }
      if (
        typeof mc.spec_draft_p_min === 'number' &&
        mc.spec_draft_p_min >= 0 &&
        mc.spec_draft_p_min <= 1
      ) {
        lines.push(`spec-draft-p-min = ${mc.spec_draft_p_min}`)
      }
      // Both added in 0.7.0. Upstream throws on an unknown sampling mode or a
      // negative ubatch, which would abort the load, so anything else is
      // dropped; 0 means "same as ubatch-size".
      if (mc.spec_draft_sampling === NON_DEFAULT_DRAFT_SAMPLING) {
        lines.push(`spec-draft-sampling = ${NON_DEFAULT_DRAFT_SAMPLING}`)
      }
      if (
        typeof mc.spec_draft_n_ubatch === 'number' &&
        Number.isFinite(mc.spec_draft_n_ubatch) &&
        mc.spec_draft_n_ubatch > 0
      ) {
        lines.push(`spec-draft-n-ubatch = ${Math.floor(mc.spec_draft_n_ubatch)}`)
      }
    }

    // Per-model sampling defaults. llama-server applies these as server-side
    // defaults for every request to the model (chat and external API clients);
    // a per-request JSON field still overrides them. INI keys are the CLI
    // long-form names minus dashes.
    //
    // Skipping a value equal to llama.cpp's default is not just tidiness here:
    // passing any of the first six sets a `user_sampling_config` bit that
    // suppresses the GGUF's own `general.sampling.*` recommendations, so writing
    // an identical-looking default silently overrode what the model asked for.
    // `null` means "no default, always emit" -- the two penalties set no bit.
    const samplingIniKeys: Array<[keyof ModelYaml, string, number | null]> = [
      ['temperature', 'temperature', 0.8],
      ['top_k', 'top-k', 40],
      ['top_p', 'top-p', 0.95],
      ['min_p', 'min-p', 0.05],
      ['repeat_last_n', 'repeat-last-n', 64],
      ['repeat_penalty', 'repeat-penalty', 1.0],
      ['presence_penalty', 'presence-penalty', null],
      ['frequency_penalty', 'frequency-penalty', null],
    ]
    for (const [yamlKey, iniKey, upstreamDefault] of samplingIniKeys) {
      const v = mc[yamlKey]
      if (typeof v !== 'number' || !Number.isFinite(v)) continue
      if (upstreamDefault !== null && v === upstreamDefault) continue
      // Upstream throws on a negative window rather than clamping, which aborts
      // the load; a legacy -1 from the old "-1 = full context" UI must not reach
      // the preset.
      if (iniKey === 'repeat-last-n' && v < 0) continue
      lines.push(`${iniKey} = ${v}`)
    }

    if (mc.embedding === true) {
      embeddingCount++
      lines.push('embeddings = true')
      // Embedders have a small trained context (e.g. MiniLM = 512). Without an
      // explicit override they inherit the global [*] ctx-size (8192), which
      // exceeds n_ctx_train and fails to load. Pin to native (0 = load from
      // model) unless model.yml already set a positive per-model ctx-size.
      if (!ctxEmitted) {
        lines.push('ctx-size = 0')
      }
      const pooling =
        typeof mc.pooling === 'string' && mc.pooling.length > 0
          ? mc.pooling
          : 'mean'
      lines.push(`pooling = ${escapeIniValue(pooling)}`)
      const ubatch =
        typeof mc.ubatch_size === 'number' && mc.ubatch_size > 0
          ? mc.ubatch_size
          : DEFAULT_EMBEDDING_UBATCH
      const batch =
        typeof mc.batch_size === 'number' && mc.batch_size >= ubatch
          ? mc.batch_size
          : ubatch
      lines.push(`ubatch-size = ${ubatch}`)
      lines.push(`batch-size = ${batch}`)
    }

    lines.push('load-on-startup = false')
    lines.push('')
  }

  const outPath = await joinPath([providerPath, 'router.preset.ini'])
  const tmpPath = await joinPath([providerPath, 'router.preset.ini.tmp'])
  const body = lines.join('\n')

  // Atomic write: tmp + rename. fs.mv overwrites on Tauri's side.
  await fs.writeFileSync(tmpPath, body)
  try {
    // Best-effort cleanup of any prior file before rename — fs.mv may not
    // overwrite on all platforms.
    if (await fs.existsSync(outPath)) {
      try {
        await fs.rm(outPath)
      } catch {
        /* ignore */
      }
    }
    await fs.mv(tmpPath, outPath)
  } catch {
    // Fallback: if rename fails, write directly to the target.
    await fs.writeFileSync(outPath, body)
    try {
      await fs.rm(tmpPath)
    } catch {
      /* ignore */
    }
  }

  return { path: outPath, embeddingCount }
}