Skip to content
Kward Search API index

Module: Kward::ConfigFiles::Core

Extended by:
Core
Included in:
Kward::ConfigFiles, Core
Defined in:
lib/kward/config/core.rb

Instance Method Summary collapse

Instance Method Details

#cache_dirString

Returns directory containing reusable local caches.

Returns:

  • (String)

    directory containing reusable local caches



78
79
80
# File 'lib/kward/config/core.rb', line 78

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

#code_search_cache_dirObject



175
176
177
# File 'lib/kward/config/core.rb', line 175

def code_search_cache_dir
  File.join(cache_dir, "code_search")
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



65
66
67
68
69
70
# File 'lib/kward/config/core.rb', line 65

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



73
74
75
# File 'lib/kward/config/core.rb', line 73

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

#config_value(config, *keys) ⇒ Object

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



373
374
375
376
377
378
379
# File 'lib/kward/config/core.rb', line 373

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

#default_configHash

Returns a fresh default config suitable for first-run persistence.

Returns:

  • (Hash)

    deep-mutable config defaults



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
# File 'lib/kward/config/core.rb', line 103

def default_config
  {
    "personas" => JSON.parse(JSON.generate(DEFAULT_PERSONAS)),
    "memory" => {
      "enabled" => false,
      "auto_summary" => false
    },
    "composer" => {
      "busy_help" => true,
      "tab_keybindings" => "auto"
    },
    "editor" => {
      "mode" => "modern",
      "agent" => {},
      "runners" => {},
      "auto_indent" => true,
      "auto_close_pairs" => true,
      "soft_wrap" => true,
      "bar_cursor" => true,
      "line_numbers" => "absolute",
      "diff_view" => "auto"
    },
    "overlay" => DEFAULT_OVERLAY_SETTINGS.dup,
    "project_browser" => {
      "icons" => "off"
    },
    "web_search" => {
      "enabled" => true,
      "provider" => "auto",
      "allow_model_providers" => false
    },
    "updates" => {
      "check" => true
    },
    "sessions" => {
      "auto_resume" => false
    },
    "skills" => {
      "trust_project" => false
    },
    "enforce_workspace_agents_file" => false,
    "system_prompt" => {
      "include_principles" => true
    },
    "mcpServers" => {},
    "transports" => {},
    "tools" => {
      "workspace_guardrails" => true
    },
    "permissions" => {
      "enabled" => false,
      "mode" => "ask"
    },
    "sandbox" => {
      "mode" => "off",
      "network" => "deny",
      "writable_roots" => [],
      "protect_git_metadata" => true
    }
  }
end

#delete_config_key(key, path = config_path) ⇒ Object

Removes a top-level config key when it exists.



364
365
366
367
368
369
370
# File 'lib/kward/config/core.rb', line 364

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

#ensure_default_config!(path = config_path) ⇒ Object

Performs ensure default config for configuration file and path handling.



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

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

  write_config(default_config, path)
  true
end

#kwshrc_pathString

Returns embedded-shell rc config path.

Returns:

  • (String)

    embedded-shell rc config path



83
84
85
# File 'lib/kward/config/core.rb', line 83

def kwshrc_path
  File.join(config_dir, "kwshrc")
end

#kwshrc_pathsArray<String>

Returns embedded-shell rc config paths in load order.

Returns:

  • (Array<String>)

    embedded-shell rc config paths in load order



88
89
90
# File 'lib/kward/config/core.rb', line 88

def kwshrc_paths
  [kwshrc_path, File.expand_path("~/.kwshrc")].uniq
end

#lifecycle_hooks_config(workspace_root = Dir.pwd) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/kward/config/core.rb', line 255

def lifecycle_hooks_config(workspace_root = Dir.pwd)
  config = read_config
  workspace_config = read_trusted_workspace_hooks_config(workspace_root)
  return config if workspace_config.empty?

  merge_hooks_config(config, workspace_config)
end

#memory_core_pathObject



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

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



206
207
208
# File 'lib/kward/config/core.rb', line 206

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

#memory_events_pathObject



218
219
220
# File 'lib/kward/config/core.rb', line 218

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

#memory_soft_pathObject



214
215
216
# File 'lib/kward/config/core.rb', line 214

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

#merge_hook_maps(left, right) ⇒ Object



311
312
313
314
315
316
317
318
319
# File 'lib/kward/config/core.rb', line 311

def merge_hook_maps(left, right)
  left = left.is_a?(Hash) ? left : {}
  right = right.is_a?(Hash) ? right : {}
  merged = JSON.parse(JSON.generate(left))
  right.each do |event, entries|
    merged[event.to_s] = Array(merged[event.to_s]) + Array(entries)
  end
  merged
end

#merge_hooks_config(config, workspace_config) ⇒ Object



305
306
307
308
309
# File 'lib/kward/config/core.rb', line 305

def merge_hooks_config(config, workspace_config)
  merged = JSON.parse(JSON.generate(config || {}))
  merged["hooks"] = merge_hook_maps(merged["hooks"], workspace_config["hooks"] || workspace_config[:hooks])
  merged
end

#model_catalog_cache_path(provider_id) ⇒ Object

Raises:

  • (ArgumentError)


183
184
185
186
187
188
# File 'lib/kward/config/core.rb', line 183

def model_catalog_cache_path(provider_id)
  provider = provider_id.to_s.gsub(/[^a-z0-9_-]/i, "_")
  raise ArgumentError, "Provider id must be a non-empty string" if provider.empty?

  File.join(cache_dir, "models", "#{provider}.json")
end

#openrouter_models_cache_pathObject



179
180
181
# File 'lib/kward/config/core.rb', line 179

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

#project_browser_state_pathObject



194
195
196
# File 'lib/kward/config/core.rb', line 194

def project_browser_state_path
  File.join(cache_dir, "project_browser_state.json")
end

#prompt_history_path(cwd, config_dir: self.config_dir, kind: "prompt") ⇒ Object



198
199
200
201
202
203
# File 'lib/kward/config/core.rb', line 198

def prompt_history_path(cwd, config_dir: self.config_dir, kind: "prompt")
  key = Digest::SHA256.hexdigest(canonical_workspace_root(cwd))[0, 24]
  return File.join(config_dir, "history", "#{key}.jsonl") if kind.to_s == "prompt"

  File.join(config_dir, "history", kind.to_s, "#{key}.jsonl")
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



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/kward/config/core.rb', line 231

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

  config = JSON.parse(File.read(path))
  return config if config.is_a?(Hash)

  raise ConfigError.new(path: path, format: "JSON", detail: "top-level value must be an object")
rescue JSON::ParserError => e
  raise ConfigError.new(path: path, format: "JSON", detail: e.message)
end

#read_kwsh_configObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/kward/config/core.rb', line 321

def read_kwsh_config
  config = {
    shell: Kwsh::DEFAULT_SHELL,
    timeout_seconds: Kwsh::DEFAULT_TIMEOUT_SECONDS,
    max_output_bytes: Kwsh::DEFAULT_MAX_OUTPUT_BYTES,
    history_limit: Kwsh::DEFAULT_HISTORY_LIMIT,
    env: {},
    aliases: {}
  }
  rc_config = read_kwshrc_config
  config[:env].merge!(rc_config[:env])
  config[:aliases].merge!(rc_config[:aliases])
  config
end

#read_kwshrc_config(paths = kwshrc_paths, env: ENV.to_h) ⇒ Object



336
337
338
339
340
# File 'lib/kward/config/core.rb', line 336

def read_kwshrc_config(paths = kwshrc_paths, env: ENV.to_h)
  Kwshrc.read(paths, env: env)
rescue Kwshrc::ParseError => e
  raise "Invalid kwshrc config: #{e.message}"
end

#read_trusted_workspace_hooksObject



298
299
300
301
302
303
# File 'lib/kward/config/core.rb', line 298

def read_trusted_workspace_hooks
  config = read_config(trusted_workspace_hooks_path)
  config.is_a?(Hash) ? config : {}
rescue ConfigError
  {}
end

#read_trusted_workspace_hooks_config(workspace_root = Dir.pwd) ⇒ Object



263
264
265
266
267
268
# File 'lib/kward/config/core.rb', line 263

def read_trusted_workspace_hooks_config(workspace_root = Dir.pwd)
  path = workspace_hooks_path(workspace_root)
  return {} unless workspace_hooks_trusted?(workspace_root)

  read_config(path)
end

#skip_config=(value) ⇒ Object



45
46
47
# File 'lib/kward/config/core.rb', line 45

def skip_config=(value)
  @skip_config = value
end

#skip_config?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/kward/config/core.rb', line 57

def skip_config?
  @skip_config == true
end

#trust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object



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

def trust_workspace_hooks!(workspace_root = Dir.pwd)
  path = workspace_hooks_path(workspace_root)
  raise "No workspace hook config found: #{path}" unless File.file?(path)

  trusted = read_trusted_workspace_hooks
  trusted[File.expand_path(path)] = workspace_hooks_digest(workspace_root)
  PrivateFile.write_json(trusted_workspace_hooks_path, trusted)
end

#trusted_workspace_hooks_pathObject



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

def trusted_workspace_hooks_path
  File.join(config_dir, "trusted_workspace_hooks.json")
end

#untrust_workspace_hooks!(workspace_root = Dir.pwd) ⇒ Object



287
288
289
290
291
292
# File 'lib/kward/config/core.rb', line 287

def untrust_workspace_hooks!(workspace_root = Dir.pwd)
  path = File.expand_path(workspace_hooks_path(workspace_root))
  trusted = read_trusted_workspace_hooks
  trusted.delete(path)
  PrivateFile.write_json(trusted_workspace_hooks_path, trusted)
end

#update_check_cache_pathObject



190
191
192
# File 'lib/kward/config/core.rb', line 190

def update_check_cache_path
  File.join(cache_dir, "update_check.json")
end

#update_config(values, path = config_path) ⇒ Object

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



343
344
345
346
347
348
349
350
# File 'lib/kward/config/core.rb', line 343

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_nested_config(section, values, path = config_path) ⇒ Object

Merges values into a one-level nested config section and writes privately.



353
354
355
356
357
358
359
360
361
# File 'lib/kward/config/core.rb', line 353

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

  config = read_config(path)
  current = config[section.to_s].is_a?(Hash) ? config[section.to_s].dup : {}
  config[section.to_s] = current.merge(values.transform_keys(&:to_s))
  write_config(config, path)
  config
end

#warning_sinkObject



53
54
55
# File 'lib/kward/config/core.rb', line 53

def warning_sink
  @warning_sink
end

#warning_sink=(sink) ⇒ Object



49
50
51
# File 'lib/kward/config/core.rb', line 49

def warning_sink=(sink)
  @warning_sink = sink
end

#workspace_hooks_digest(workspace_root = Dir.pwd) ⇒ Object



294
295
296
# File 'lib/kward/config/core.rb', line 294

def workspace_hooks_digest(workspace_root = Dir.pwd)
  Digest::SHA256.file(workspace_hooks_path(workspace_root)).hexdigest
end

#workspace_hooks_path(root = Dir.pwd) ⇒ Object



92
93
94
# File 'lib/kward/config/core.rb', line 92

def workspace_hooks_path(root = Dir.pwd)
  File.join(File.expand_path(root), ".kward", "hooks.json")
end

#workspace_hooks_trusted?(workspace_root = Dir.pwd) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
273
274
275
276
# File 'lib/kward/config/core.rb', line 270

def workspace_hooks_trusted?(workspace_root = Dir.pwd)
  path = workspace_hooks_path(workspace_root)
  return false unless File.file?(path)

  trusted = read_trusted_workspace_hooks
  trusted[File.expand_path(path)] == workspace_hooks_digest(workspace_root)
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



248
249
250
251
252
253
# File 'lib/kward/config/core.rb', line 248

def write_config(config, path = config_path)
  path = File.expand_path(path)
  raise "Cannot write Kward config while --skip-config is active: #{path}" if skip_config? && path == config_path

  PrivateFile.write_json(path, config)
end