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



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

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

#code_search_cache_dir ⇒ Object



188
189
190
# File 'lib/kward/config/core.rb', line 188

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



74
75
76
77
78
79
# File 'lib/kward/config/core.rb', line 74

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



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

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.



386
387
388
389
390
391
392
# File 'lib/kward/config/core.rb', line 386

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



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
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/kward/config/core.rb', line 112

def default_config
  {
    "personas" => JSON.parse(JSON.generate(DEFAULT_PERSONAS)),
    "memory" => {
      "enabled" => false,
      "auto_summary" => false
    },
    "transcript" => {
      "turn_stats" => "compact"
    },
    "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.



377
378
379
380
381
382
383
# File 'lib/kward/config/core.rb', line 377

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.



179
180
181
182
183
184
185
186
# File 'lib/kward/config/core.rb', line 179

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

#ignore_local_instructions=(value) ⇒ Object



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

def ignore_local_instructions=(value)
  @ignore_local_instructions = value
end

#ignore_local_instructions? ⇒ Boolean

Returns:

  • (Boolean)


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

def ignore_local_instructions?
  @ignore_local_instructions == true
end

#kwshrc_path ⇒ String

Returns embedded-shell rc config path.

Returns:

  • (String) —

    embedded-shell rc config path



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

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



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

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

#lifecycle_hooks_config(workspace_root = Dir.pwd) ⇒ Object



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

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



223
224
225
# File 'lib/kward/config/core.rb', line 223

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



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

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

#memory_events_path ⇒ Object



231
232
233
# File 'lib/kward/config/core.rb', line 231

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

#memory_soft_path ⇒ Object



227
228
229
# File 'lib/kward/config/core.rb', line 227

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

#merge_hook_maps(left, right) ⇒ Object



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

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



318
319
320
321
322
# File 'lib/kward/config/core.rb', line 318

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)


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

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



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

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

#project_browser_state_path ⇒ Object



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

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



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

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



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/kward/config/core.rb', line 244

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



334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/kward/config/core.rb', line 334

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



349
350
351
352
353
# File 'lib/kward/config/core.rb', line 349

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



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

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



276
277
278
279
280
281
# File 'lib/kward/config/core.rb', line 276

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



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

def skip_config=(value)
  @skip_config = value
end

#skip_config? ⇒ Boolean

Returns:

  • (Boolean)


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

def skip_config?
  @skip_config == true
end

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



291
292
293
294
295
296
297
298
# File 'lib/kward/config/core.rb', line 291

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



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

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

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



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

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



203
204
205
# File 'lib/kward/config/core.rb', line 203

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.



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

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.



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

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



62
63
64
# File 'lib/kward/config/core.rb', line 62

def warning_sink
  @warning_sink
end

#warning_sink=(sink) ⇒ Object



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

def warning_sink=(sink)
  @warning_sink = sink
end

#workspace_hooks_digest(workspace_root = Dir.pwd) ⇒ Object



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

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



101
102
103
# File 'lib/kward/config/core.rb', line 101

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)


283
284
285
286
287
288
289
# File 'lib/kward/config/core.rb', line 283

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



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

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