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_dir ⇒ String

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_dir ⇒ Object



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

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

#config_dir ⇒ String

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_path ⇒ String

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.



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

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

#default_config ⇒ Hash

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
164
# 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" => {},
    "plugins" => {},
    "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.



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

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.



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

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_path ⇒ String

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_paths ⇒ Array<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



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

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_path ⇒ Object



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

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

#memory_dir ⇒ String

Returns directory containing structured memory files.

Returns:

  • (String) —

    directory containing structured memory files



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

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

#memory_events_path ⇒ Object



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

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

#memory_soft_path ⇒ Object



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

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

#merge_hook_maps(left, right) ⇒ Object



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

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



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

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)


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

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_path ⇒ Object



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

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

#project_browser_state_path ⇒ Object



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

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



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

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



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

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_config ⇒ Object



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

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



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

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_hooks ⇒ Object



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

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



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

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



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

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_path ⇒ Object



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



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

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_path ⇒ Object



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

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.



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

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.



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

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_sink ⇒ Object



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



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

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)


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

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



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

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