Skip to content
Kward Search API index

Module: Kward::ConfigFiles

Defined in:
lib/kward/config_files.rb

Overview

Resolves Kward configuration, cache, memory, prompt, skill, and plugin paths, and reads/writes the JSON config file used by the CLI and RPC server.

This module is the configuration boundary, not a runtime settings cache. Most methods read the filesystem each time so CLI commands and RPC reloads can observe edits made outside the process. Callers that need caching should own invalidation explicitly, as Client#reload_config does for provider state.

Keep path decisions here. Higher-level code should ask ConfigFiles for config, prompt, skill, plugin, cache, memory, and session locations instead of reconstructing ~/.kward paths independently.

Defined Under Namespace

Classes: PromptTemplate, Skill

Constant Summary collapse

MAX_SKILL_FILE_BYTES =
100_000
MAX_PROMPT_FILE_BYTES =
32 * 1024
DEFAULT_OVERLAY_SETTINGS =
{ "alignment" => "center", "width" => "maximum" }.freeze
DEFAULT_PERSONAS =
{
  "characters" => [
    {
      "key" => "kward",
      "label" => "Kward",
      "instruction" => "Your name is Kward, the grim Andruid - robotic keeper of the Forrest of Code, protecting the nature of good engineering priciples. Speak like an old druid, be suspicous of everyone, but with a good intend."
    }
  ],
  "default" => "kward"
}.freeze
OVERLAY_ALIGNMENTS =
%w[left center right].freeze
OVERLAY_WIDTHS =
%w[capped maximum].freeze

Class Method Summary collapse

Class Method Details

.active_persona_label(workspace_root:, model: nil, config: read_config) ⇒ Object

Returns the label of the persona selected by default/workspace/model rules.



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
# File 'lib/kward/config_files.rb', line 288

def active_persona_label(workspace_root:, model: nil, config: read_config)
  personas = config["personas"]
  return nil unless personas.is_a?(Hash)

  labels = crew_character_labels(personas)
  active_label = persona_label_for_key(personas["default"], labels) unless personas["default"].nil?

  workspaces = personas["workspaces"]
  if workspaces.is_a?(Hash)
    root = canonical_workspace_root(workspace_root)
    workspaces.each do |path, key|
      next unless canonical_workspace_root(path) == root

      active_label = persona_label_for_key(key, labels)
      break
    end
  end

  models = personas["models"]
  if models.is_a?(Hash) && !model.to_s.empty? && models.key?(model.to_s)
    active_label = persona_label_for_key(models[model.to_s], labels)
  end

  active_label
end

.add_persona_entry(entries, layer, value, name: nil) ⇒ Object



398
399
400
401
402
403
# File 'lib/kward/config_files.rb', line 398

def add_persona_entry(entries, layer, value, name: nil)
  text = presence(value)
  return unless text

  entries << { layer: layer.to_s, name: name.to_s, prompt: text }
end

.agents_promptString?

Reads global principle instructions from the config directory.

PRINCIPLES.md is preferred. AGENTS.md remains a backwards-compatible alias for existing installations.

Returns:

  • (String, nil)

    prompt text, or nil when absent/too large



250
251
252
253
254
255
# File 'lib/kward/config_files.rb', line 250

def agents_prompt
  path = config_principles_path
  return read_prompt_file(path, "Kward principles file") if File.exist?(path)

  read_prompt_file(config_agents_path, "Kward AGENTS.md alias")
end

.cache_dirObject



63
64
65
# File 'lib/kward/config_files.rb', line 63

def cache_dir
  File.join(config_dir, "cache")
end

.canonical_workspace_root(path) ⇒ Object



393
394
395
396
# File 'lib/kward/config_files.rb', line 393

def canonical_workspace_root(path)
  expanded = File.expand_path(path.to_s.empty? ? Dir.pwd : path.to_s)
  File.directory?(expanded) ? File.realpath(expanded) : expanded
end

.character_entries(raw) ⇒ Object



445
446
447
448
449
450
451
452
453
454
# File 'lib/kward/config_files.rb', line 445

def character_entries(raw)
  case raw
  when Hash
    raw.map { |key, definition| [key, definition] }
  when Array
    raw.filter_map { |entry| character_entry(entry) }
  else
    []
  end
end

.character_entry(entry) ⇒ Object



456
457
458
459
460
461
462
463
464
465
# File 'lib/kward/config_files.rb', line 456

def character_entry(entry)
  return nil unless entry.is_a?(Hash)

  if entry.length == 1 && entry.keys.first.is_a?(String)
    [entry.keys.first, entry.values.first]
  else
    key = entry["key"] || entry[:key] || entry["id"] || entry[:id] || entry["name"] || entry[:name]
    key.to_s.empty? ? nil : [key, entry]
  end
end

.code_search_cache_dirObject



96
97
98
# File 'lib/kward/config_files.rb', line 96

def code_search_cache_dir
  File.join(cache_dir, "code_search")
end

.composer_busy_help?(config = read_config) ⇒ Boolean

Returns whether the composer should show busy-state keyboard help.

Returns:

  • (Boolean)


191
192
193
194
# File 'lib/kward/config_files.rb', line 191

def composer_busy_help?(config = read_config)
  composer = config["composer"].is_a?(Hash) ? config["composer"] : {}
  composer["busy_help"] != false
end

.config_agents_pathObject



261
262
263
# File 'lib/kward/config_files.rb', line 261

def config_agents_path
  File.join(config_dir, "AGENTS.md")
end

.config_dirString

Directory that contains Kward's user config and adjacent prompt/skill data. Defaults to ~/.kward, or the directory of KWARD_CONFIG_PATH.

Returns:

  • (String)

    expanded config directory path



51
52
53
54
55
56
# File 'lib/kward/config_files.rb', line 51

def config_dir
  config_path = ENV["KWARD_CONFIG_PATH"]
  return File.expand_path(File.dirname(config_path)) if config_path && !config_path.empty?

  File.expand_path("~/.kward")
end

.config_pathString

Returns expanded JSON config file path.

Returns:

  • (String)

    expanded JSON config file path



59
60
61
# File 'lib/kward/config_files.rb', line 59

def config_path
  File.expand_path(ENV["KWARD_CONFIG_PATH"] || File.join(config_dir, "config.json"))
end

.config_principles_pathObject



257
258
259
# File 'lib/kward/config_files.rb', line 257

def config_principles_path
  File.join(config_dir, "PRINCIPLES.md")
end

.config_value(config, *keys) ⇒ Object

Returns the first present non-empty string value among several config keys.



167
168
169
170
171
172
173
# File 'lib/kward/config_files.rb', line 167

def config_value(config, *keys)
  keys.each do |key|
    text = presence(config[key])
    return text if text
  end
  nil
end

.crew_character_labels(personas) ⇒ Object



411
412
413
414
415
# File 'lib/kward/config_files.rb', line 411

def crew_character_labels(personas)
  named_character_values(personas) do |_key, definition|
    extract_character_label(definition)
  end
end

.crew_characters(personas) ⇒ Object



405
406
407
408
409
# File 'lib/kward/config_files.rb', line 405

def crew_characters(personas)
  named_character_values(personas) do |_key, definition|
    extract_character_instruction(definition)
  end
end

.default_configObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kward/config_files.rb', line 67

def default_config
  {
    "personas" => JSON.parse(JSON.generate(DEFAULT_PERSONAS)),
    "memory" => {
      "enabled" => false,
      "auto_summary" => false
    },
    "composer" => {
      "busy_help" => true
    },
    "sessions" => {
      "auto_resume" => false
    },
    "enforce_workspace_agents_file" => false,
    "tools" => {
      "workspace_guardrails" => true
    }
  }
end

.delete_config_key(key, path = config_path) ⇒ Object

Removes a top-level config key when it exists.



158
159
160
161
162
163
164
# File 'lib/kward/config_files.rb', line 158

def delete_config_key(key, path = config_path)
  config = read_config(path)
  existed = config.key?(key.to_s)
  config.delete(key.to_s)
  write_config(config, path) if existed
  existed
end

.enforce_workspace_agents_file?(config = read_config) ⇒ Boolean

Returns whether workspace AGENTS.md contents should be injected directly instead of a compact read-when-relevant instruction.

Returns:

  • (Boolean)


210
211
212
# File 'lib/kward/config_files.rb', line 210

def enforce_workspace_agents_file?(config = read_config)
  config["enforce_workspace_agents_file"] == true
end

.ensure_default_config!(path = config_path) ⇒ Object

Performs ensure default config for configuration file and path handling.



88
89
90
91
92
93
94
# File 'lib/kward/config_files.rb', line 88

def ensure_default_config!(path = config_path)
  path = File.expand_path(path)
  return false if File.exist?(path)

  write_config(default_config, path)
  true
end

.extract_character_instruction(definition) ⇒ Object



473
474
475
476
477
478
479
480
481
482
# File 'lib/kward/config_files.rb', line 473

def extract_character_instruction(definition)
  return nil if definition.nil?

  if definition.is_a?(Hash)
    value = definition["instruction"] || definition[:instruction]
    return presence(value)
  end

  presence(definition)
end

.extract_character_label(definition) ⇒ Object



467
468
469
470
471
# File 'lib/kward/config_files.rb', line 467

def extract_character_label(definition)
  return nil unless definition.is_a?(Hash)

  presence(definition["label"] || definition[:label])
end

.inside_directory?(path, base) ⇒ Boolean

Returns:

  • (Boolean)


573
574
575
# File 'lib/kward/config_files.rb', line 573

def inside_directory?(path, base)
  path == base || path.start_with?(base + File::SEPARATOR)
end

.markdown_parts(path) ⇒ Object



560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/kward/config_files.rb', line 560

def markdown_parts(path)
  content = File.read(path)
  return [{}, content] unless content.start_with?("---\n", "---\r\n")

  _opening, rest = content.split(/\A---\r?\n/, 2)
  yaml_text, body = rest.to_s.split(/\r?\n---\r?\n/, 2)
  raise "missing frontmatter closing delimiter" if body.nil?

  data = yaml_text.to_s.empty? ? {} : YAML.safe_load(yaml_text, permitted_classes: [], aliases: false)
  frontmatter = data.is_a?(Hash) ? data.transform_keys(&:to_s) : {}
  [frontmatter, body]
end

.memory_core_pathObject



109
110
111
# File 'lib/kward/config_files.rb', line 109

def memory_core_path
  File.join(memory_dir, "core.json")
end

.memory_dirString

Returns directory containing structured memory files.

Returns:

  • (String)

    directory containing structured memory files



105
106
107
# File 'lib/kward/config_files.rb', line 105

def memory_dir
  File.join(config_dir, "memory")
end

.memory_events_pathObject



117
118
119
# File 'lib/kward/config_files.rb', line 117

def memory_events_path
  File.join(memory_dir, "events.jsonl")
end

.memory_soft_pathObject



113
114
115
# File 'lib/kward/config_files.rb', line 113

def memory_soft_path
  File.join(memory_dir, "soft.jsonl")
end

.named_character_values(personas) ⇒ Object



436
437
438
439
440
441
442
443
# File 'lib/kward/config_files.rb', line 436

def named_character_values(personas)
  character_entries(personas["characters"] || personas["crew"]).each_with_object({}) do |(key, definition), mapping|
    value = yield(key, definition)
    next if value.to_s.empty?

    mapping[key.to_s] = value
  end
end

.openrouter_models_cache_pathObject



100
101
102
# File 'lib/kward/config_files.rb', line 100

def openrouter_models_cache_path
  File.join(cache_dir, "openrouter_models.json")
end

.overlay_settings(config = read_config) ⇒ Hash

Returns validated overlay settings with defaults for missing or invalid values.

Parameters:

  • config (Hash) (defaults to: read_config)

    parsed config object

Returns:

  • (Hash)

    overlay settings with alignment and width



180
181
182
183
184
185
186
187
188
# File 'lib/kward/config_files.rb', line 180

def overlay_settings(config = read_config)
  overlay = config["overlay"].is_a?(Hash) ? config["overlay"] : {}
  settings = DEFAULT_OVERLAY_SETTINGS.dup
  alignment = overlay["alignment"].to_s
  width = overlay["width"].to_s
  settings["alignment"] = alignment if OVERLAY_ALIGNMENTS.include?(alignment)
  settings["width"] = width if OVERLAY_WIDTHS.include?(width)
  settings
end

.persona_entries(workspace_root:, model: nil, reasoning_effort: nil, now: Time.now, config: read_config, include_reasoning: true) ⇒ Object



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
# File 'lib/kward/config_files.rb', line 314

def persona_entries(workspace_root:, model: nil, reasoning_effort: nil, now: Time.now, config: read_config, include_reasoning: true)
  personas = config["personas"]
  return [] unless personas.is_a?(Hash)

  characters = crew_characters(personas)
  entries = []
  active_persona = { layer: "default", value: personas["default"], name: nil }

  workspaces = personas["workspaces"]
  if workspaces.is_a?(Hash)
    root = canonical_workspace_root(workspace_root)
    workspaces.each do |path, key|
      if canonical_workspace_root(path) == root
        active_persona = { layer: "workspace", value: key, name: path }
        break
      end
    end
  end

  models = personas["models"]
  if models.is_a?(Hash) && !model.to_s.empty? && models.key?(model.to_s)
    active_persona = { layer: "model", value: models[model.to_s], name: model.to_s }
  end

  add_persona_entry(
    entries,
    active_persona.fetch(:layer),
    resolved_persona_text(active_persona.fetch(:value), characters: characters),
    name: active_persona[:name]
  )

  modifiers = personas["persona_modifiers"]
  if modifiers.is_a?(Hash)
    if include_reasoning
      reasoning = modifiers["reasoning"]
      add_persona_entry(entries, "reasoning", reasoning[reasoning_effort.to_s]) if reasoning.is_a?(Hash) && !reasoning_effort.to_s.empty?
    end

    time_of_day = modifiers["time_of_day"]
    bucket = time_of_day_bucket(now)
    add_persona_entry(entries, "time_of_day", time_of_day[bucket], name: bucket) if time_of_day.is_a?(Hash)

    weekday = modifiers["weekday"]
    day = weekday_name(now)
    add_persona_entry(entries, "weekday", weekday[day], name: day) if weekday.is_a?(Hash)

    add_persona_entry(entries, "suffix", modifiers["suffix"])
  end

  entries
end

.persona_label_for_key(value, labels) ⇒ Object



429
430
431
432
433
434
# File 'lib/kward/config_files.rb', line 429

def persona_label_for_key(value, labels)
  key = value.to_s.strip
  return nil if key.empty?

  presence(labels[key])
end

.persona_prompt(workspace_root, model: nil, reasoning_effort: nil, now: Time.now, config: read_config) ⇒ String?

Builds persona prompt text from default, workspace, model, reasoning, time-of-day, weekday, and suffix config entries.

Persona resolution is intentionally data-driven so users can edit config without plugin code. Keep new persona selectors additive and deterministic; prompt construction depends on stable ordering.

Parameters:

  • workspace_root (String)

    active workspace root

  • model (String, nil) (defaults to: nil)

    active model name

  • reasoning_effort (String, nil) (defaults to: nil)

    active reasoning effort

  • now (Time) (defaults to: Time.now)

    local time used for time-based modifiers

  • config (Hash) (defaults to: read_config)

    parsed config object

Returns:

  • (String, nil)

    persona prompt text when entries match



278
279
280
281
282
283
284
285
# File 'lib/kward/config_files.rb', line 278

def persona_prompt(workspace_root, model: nil, reasoning_effort: nil, now: Time.now, config: read_config)
  text = persona_entries(workspace_root: workspace_root, model: model, reasoning_effort: reasoning_effort, now: now, config: config).map do |entry|
    entry[:prompt]
  end.join("\n\n")
  return nil if text.empty?

  text
end

.plugin_dirString

Returns trusted user plugin directory.

Returns:

  • (String)

    trusted user plugin directory



505
506
507
# File 'lib/kward/config_files.rb', line 505

def plugin_dir
  File.expand_path("~/.kward/plugins")
end

.plugin_pathsArray<String>

Finds trusted top-level plugin files.

Plugins are intentionally loaded only from ~/.kward/plugins, not from a workspace or custom KWARD_CONFIG_PATH directory.

Returns:

  • (Array<String>)

    sorted plugin file paths



515
516
517
518
519
520
521
522
523
# File 'lib/kward/config_files.rb', line 515

def plugin_paths
  plugins_root = plugin_dir
  return [] unless Dir.exist?(plugins_root)

  Dir.glob(File.join(plugins_root, "*.rb")).sort
rescue StandardError => e
  warn "Warning: skipping Kward plugins in #{plugins_root}: #{e.message}"
  []
end

.presence(value) ⇒ Object



577
578
579
580
# File 'lib/kward/config_files.rb', line 577

def presence(value)
  text = value.to_s
  text.empty? ? nil : text
end

.prompt_template_registryObject



552
553
554
555
556
557
558
# File 'lib/kward/config_files.rb', line 552

def prompt_template_registry
  Prompts::Templates.new(
    config_dir: config_dir,
    template_class: PromptTemplate,
    markdown_parser: method(:markdown_parts)
  )
end

.prompt_templates(reserved_commands: []) ⇒ Array<PromptTemplate>

Lists prompt templates exposed as slash commands.

Parameters:

  • reserved_commands (Array<String>) (defaults to: [])

    command names unavailable to templates

Returns:



529
530
531
# File 'lib/kward/config_files.rb', line 529

def prompt_templates(reserved_commands: [])
  prompt_template_registry.prompt_templates(reserved_commands: reserved_commands)
end

.read_config(path = config_path) ⇒ Hash

Reads the JSON config file.

Missing files are treated as an empty config. Invalid JSON raises a user-facing error that includes the file path. This method does not merge defaults; callers should apply feature-specific defaults at the point where behavior is decided.

Parameters:

  • path (String) (defaults to: config_path)

    config file path

Returns:

  • (Hash)

    parsed config object



130
131
132
133
134
135
136
137
# File 'lib/kward/config_files.rb', line 130

def read_config(path = config_path)
  path = File.expand_path(path)
  return {} unless File.exist?(path)

  JSON.parse(File.read(path))
rescue JSON::ParserError
  raise "Invalid Kward config JSON: #{path}"
end

.read_prompt_file(path, label) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/kward/config_files.rb', line 378

def read_prompt_file(path, label)
  return nil unless File.exist?(path)

  size = File.size(path)
  if size > MAX_PROMPT_FILE_BYTES
    warn "Warning: skipping #{label} #{path}: file too large (#{size} bytes; limit is #{MAX_PROMPT_FILE_BYTES} bytes)"
    return nil
  end

  File.read(path)
rescue StandardError => e
  warn "Warning: skipping #{label} #{path}: #{e.message}"
  nil
end

.read_skill_file(name, relative_path = nil) ⇒ String

Reads a skill file by skill name and optional relative path.

Parameters:

  • name (String)

    configured skill name

  • relative_path (String, nil) (defaults to: nil)

    path inside the skill directory

Returns:

  • (String)

    file contents or an error string



538
539
540
# File 'lib/kward/config_files.rb', line 538

def read_skill_file(name, relative_path = nil)
  skills_registry.read_skill_file(name, relative_path)
end

.resolved_persona_text(value, characters: {}) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
# File 'lib/kward/config_files.rb', line 417

def resolved_persona_text(value, characters: {})
  return nil if value.nil?

  key = value.to_s.strip
  return nil if key.empty?

  text = characters[key.to_s]
  return text unless text.to_s.empty?

  value
end

.session_auto_resume_enabled?(config = read_config) ⇒ Boolean

Returns whether new frontends should resume the last active session automatically.

Returns:

  • (Boolean)


203
204
205
206
# File 'lib/kward/config_files.rb', line 203

def session_auto_resume_enabled?(config = read_config)
  sessions = config["sessions"].is_a?(Hash) ? config["sessions"] : {}
  sessions["auto_resume"] == true
end

.skillsArray<Skill>

Lists configured skills discovered under the config directory.

Returns:

  • (Array<Skill>)

    skill metadata available to the model



500
501
502
# File 'lib/kward/config_files.rb', line 500

def skills
  skills_registry.skills
end

.skills_registryObject



542
543
544
545
546
547
548
549
550
# File 'lib/kward/config_files.rb', line 542

def skills_registry
  Skills::Registry.new(
    config_dir: config_dir,
    skill_class: Skill,
    max_file_bytes: MAX_SKILL_FILE_BYTES,
    markdown_parser: method(:markdown_parts),
    inside_directory: method(:inside_directory?)
  )
end

.time_of_day_bucket(now) ⇒ Object



484
485
486
487
488
489
490
491
# File 'lib/kward/config_files.rb', line 484

def time_of_day_bucket(now)
  hour = now.hour
  return "morning" if hour >= 5 && hour < 11
  return "before_lunch" if hour == 11
  return "late_evening" if hour >= 21 || hour < 5

  nil
end

.update_config(values, path = config_path) ⇒ Object

Merges top-level config values and writes the updated config privately.



148
149
150
151
152
153
154
155
# File 'lib/kward/config_files.rb', line 148

def update_config(values, path = config_path)
  raise "Config values must be an object" unless values.is_a?(Hash)

  config = read_config(path)
  values.each { |key, value| config[key.to_s] = value }
  write_config(config, path)
  config
end

.update_overlay_settings(values) ⇒ Object

Validates and persists terminal overlay settings.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/kward/config_files.rb', line 221

def update_overlay_settings(values)
  raise "Overlay settings must be an object" unless values.is_a?(Hash)

  config = read_config
  overlay = config["overlay"].is_a?(Hash) ? config["overlay"].dup : {}
  values.each do |key, value|
    key = key.to_s
    value = value.to_s
    case key
    when "alignment"
      raise "Overlay alignment must be left, center, or right" unless OVERLAY_ALIGNMENTS.include?(value)
    when "width"
      raise "Overlay width must be capped or maximum" unless OVERLAY_WIDTHS.include?(value)
    else
      raise "Unknown overlay setting: #{key}"
    end
    overlay[key] = value
  end
  config["overlay"] = overlay
  write_config(config)
  overlay_settings(config)
end

.web_search_config(config = read_config) ⇒ Object

Returns the nested web-search config object, or an empty config when absent.



215
216
217
218
# File 'lib/kward/config_files.rb', line 215

def web_search_config(config = read_config)
  value = config["web_search"]
  value.is_a?(Hash) ? value : {}
end

.weekday_name(now) ⇒ Object



493
494
495
# File 'lib/kward/config_files.rb', line 493

def weekday_name(now)
  %w[sunday monday tuesday wednesday thursday friday saturday][now.wday]
end

.workspace_agents_file?(workspace_root) ⇒ Boolean

Returns:

  • (Boolean)


370
371
372
# File 'lib/kward/config_files.rb', line 370

def workspace_agents_file?(workspace_root)
  File.exist?(workspace_agents_path(workspace_root))
end

.workspace_agents_path(workspace_root) ⇒ Object



366
367
368
# File 'lib/kward/config_files.rb', line 366

def workspace_agents_path(workspace_root)
  File.join(canonical_workspace_root(workspace_root), "AGENTS.md")
end

.workspace_agents_prompt(workspace_root) ⇒ Object



374
375
376
# File 'lib/kward/config_files.rb', line 374

def workspace_agents_prompt(workspace_root)
  read_prompt_file(workspace_agents_path(workspace_root), "workspace AGENTS.md")
end

.workspace_guardrails_enabled?(config = read_config) ⇒ Boolean

Returns whether file tools must stay inside the active workspace root.

Returns:

  • (Boolean)


197
198
199
200
# File 'lib/kward/config_files.rb', line 197

def workspace_guardrails_enabled?(config = read_config)
  tools = config["tools"].is_a?(Hash) ? config["tools"] : {}
  tools["workspace_guardrails"] != false
end

.write_config(config, path = config_path) ⇒ Object

Writes config JSON using private file permissions.

Parameters:

  • config (Hash)

    config object to persist

  • path (String) (defaults to: config_path)

    config file path



143
144
145
# File 'lib/kward/config_files.rb', line 143

def write_config(config, path = config_path)
  PrivateFile.write_json(path, config)
end