{"data":{"slug":"github-xberg-io-xberg-ai-rulez-skills-config-loading-precedence-0f918efb7f6e","title":"Configure Extraction with Precedence Rules","summary":"Configure extraction tasks with priority rules for CLI, JSON, and file-based settings. Manage config precedence effectively.","url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-config-loading-precedence-0f918efb7f6e","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-xberg-io-xberg-ai-rulez-skills-config-loading-precedence-0f918efb7f6e","markdown_url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-config-loading-precedence-0f918efb7f6e.md","download_url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-config-loading-precedence-0f918efb7f6e\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"The skill requires familiarity with TOML, YAML, and JSON formats for configuration files. Users must ensure that CLI flags override config files, and JSON merge is field-level, not whole-object replacement.","instructions":"---\ntype: Playbook\ntitle: Config Loading Precedence\ndescription: \"Resolve config precedence: CLI, server\/MCP, walk-up files, JSON and overrides.\"\npaths:\n  - \"crates\/xberg\/src\/core\/config\/**\"\n  - \"crates\/xberg-cli\/src\/**\"\nx-ai-rulez:\n  kind: skill\n  id: config-loading-precedence\n  metadata:\n    name: config-loading-precedence\n---\n\n# Configuration Loading & Precedence\n\n## CLI Mode Precedence (highest to lowest)\n\n1. Individual CLI flags (`--ocr`, `--output-format`, `--chunk`)\n2. Inline JSON config (`--config-json` or `--config-json-base64`)\n3. Config file (`--config path.toml`)\n4. Auto-discovered config (`xberg.{toml,yaml,yml,json}` in cwd\/parents, then the user config dir)\n5. Default values\n\n## Server\/MCP Mode Precedence\n\n1. CLI arguments (`--host`, `--port`)\n2. Environment variables (`XBERG_HOST`, `XBERG_PORT`)\n3. Config file `[server]` section\n4. Defaults (`127.0.0.1:8000`)\n\n## Config File Discovery\n\n`ExtractionConfig::discover()` (`core\/config\/extraction\/loaders.rs`) uses the same deterministic\nformat precedence at every level:\n\n1. Walks the current directory and its parents, probing `xberg.toml`, `xberg.yaml`,\n   `xberg.yml`, then `xberg.json` in each directory. The first hit wins.\n2. If that finds nothing, falls back to the per-user global config directory\n   (`dirs::config_dir()\/xberg`) and probes four basenames in a fixed order:\n   `xberg.toml`, `xberg.yaml`, `xberg.yml`, `xberg.json`.\n\n## Inline JSON Config\n\nField-level merge (not whole-object replacement):\n\n```rust\nfn merge_json_into_config(base: &ExtractionConfig, json: Value) -> Result<ExtractionConfig> {\n    let mut config_json = serde_json::to_value(base)?;\n    \/\/ Merge fields from json into config_json\n    serde_json::from_value(merged)?\n}\n```\n\nUse `--config-json-base64` for shell escaping.\n\n## Config File Formats\n\n**TOML** (`xberg.toml`):\n\n```toml\nuse_cache = true\n[ocr]\nbackend = \"tesseract\"\nlanguages = [\"eng\", \"deu\"]\n[security_limits]\nmax_archive_size = 524288000\n```\n\n**YAML** and **JSON** follow equivalent structure.\n\n## CLI Flag Overrides\n\n`crates\/xberg-cli\/src\/commands\/overrides\/mod.rs`: the `ExtractionOverrides` struct's `validate()`\nruns first, then `apply(self, config: &mut ExtractionConfig)` lays individual CLI flags over\nthe merged config. There is no `apply_extraction_overrides()` and no `commands.rs` \u2014\n`commands\/` is a directory.\n\nInline JSON enters through `apply_json_overrides(config, config_json, config_json_base64)`\n(`crates\/xberg-cli\/src\/input.rs`), which delegates to `merge_json_into_config`.\n\n## Two silent no-op mechanisms\n\n**Duplicate `Default` impls.** `TesseractConfig` is defined twice \u2014 public\n(`types\/formats.rs`, binding-friendly types) and internal (`ocr\/types.rs`, engine-side types)\n\u2014 bridged by a `From` impl, each with its own `Default`. Changing one default fixes only the\nroutes that materialise that struct. Before changing any config default, grep the **bare**\ntype name (a `pub use module::*;` makes a qualified path unsearchable) and check for a second\ndefinition.\n\n**Unknown keys are rejected.** Nearly every config struct under `core\/config\/` (including\n`ExtractionConfig`, `OcrConfig`, `PdfConfig`, `ChunkingConfig` and `LlmConfig`) carries\n`#[serde(deny_unknown_fields)]`, so a typo'd key fails to parse instead of being ignored. Check the\nstruct's definition before relying on that for a new one. Write config fixtures against the serde\n**wire** names (`ChunkingConfig`'s `max_characters` is `max_chars` on the wire; the field name\nis only an alias).\n\n## Critical Rules\n\n1. CLI flags always win over config file\n2. JSON merge is field-level, not whole-object\n3. Auto-discovery probes TOML, YAML, YML, then JSON in each directory before walking to its parent\n4. `--config-json-base64` for shell-safe JSON passing\n5. Server config uses `[server]` section + extraction config\n","use_cases":["Set OCR backend and language preferences via CLI flags","Merge inline JSON config with existing settings","Override default values with environment variables"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/xberg-io\/xberg\/blob\/ffbf7971796a126fc52f6bb8fb40418772071f61\/.ai-rulez\/skills\/config-loading-precedence\/SKILL.md","source_commit":"ffbf7971796a126fc52f6bb8fb40418772071f61","license":null,"version":null,"listing_approved_at":"2026-11-16","created_at":"2026-11-15T13:22:01.000000Z","updated_at":"2026-11-16T05:16:54.000000Z","seo_title":"Configure Extraction with Precedence Rules","seo_description":"Configure extraction tasks with priority rules for CLI, JSON, and file-based settings. Manage config precedence effectively.","seo_keywords":["configure extraction settings","config precedence rules","merge JSON config","override extraction defaults","CLI config flags"]}}