mimic-llm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/NOTICE +16 -0
  4. data/README.md +175 -0
  5. data/exe/lllm +5 -0
  6. data/exe/llm +5 -0
  7. data/exe/mimic +5 -0
  8. data/lib/mimic/README.md +144 -0
  9. data/lib/mimic/agent/claude.rb +305 -0
  10. data/lib/mimic/agent/codex.rb +254 -0
  11. data/lib/mimic/agent/codex_fallback_prompt.md +275 -0
  12. data/lib/mimic/agent/hermes.rb +107 -0
  13. data/lib/mimic/agent/muse.rb +106 -0
  14. data/lib/mimic/agent.rb +46 -0
  15. data/lib/mimic/cli.rb +159 -0
  16. data/lib/mimic/client/base.rb +44 -0
  17. data/lib/mimic/client/crush.rb +28 -0
  18. data/lib/mimic/client/goose.rb +52 -0
  19. data/lib/mimic/client/hermes.rb +21 -0
  20. data/lib/mimic/client/opencode.rb +29 -0
  21. data/lib/mimic/client/pi.rb +50 -0
  22. data/lib/mimic/client.rb +19 -0
  23. data/lib/mimic/engine/README.md +140 -0
  24. data/lib/mimic/engine/base.rb +138 -0
  25. data/lib/mimic/engine/chat_template.rb +207 -0
  26. data/lib/mimic/engine/ik_llama.rb +273 -0
  27. data/lib/mimic/engine/launcher.rb +25 -0
  28. data/lib/mimic/engine/llama.rb +124 -0
  29. data/lib/mimic/engine/llama_embed.rb +108 -0
  30. data/lib/mimic/engine/router.rb +57 -0
  31. data/lib/mimic/engine/server_process.rb +108 -0
  32. data/lib/mimic/engine.rb +5 -0
  33. data/lib/mimic/engine_cli/binary_options.rb +46 -0
  34. data/lib/mimic/engine_cli/builder.rb +98 -0
  35. data/lib/mimic/engine_cli/diffusion.rb +117 -0
  36. data/lib/mimic/engine_cli/llama_preset.rb +119 -0
  37. data/lib/mimic/engine_cli/registry.rb +94 -0
  38. data/lib/mimic/engine_cli/source.rb +110 -0
  39. data/lib/mimic/engine_cli/status.rb +161 -0
  40. data/lib/mimic/engine_cli/update.rb +68 -0
  41. data/lib/mimic/engine_cli.rb +7 -0
  42. data/lib/mimic/health/checks.rb +92 -0
  43. data/lib/mimic/health.rb +82 -0
  44. data/lib/mimic/llm_cli/base.rb +36 -0
  45. data/lib/mimic/llm_cli/configure.rb +84 -0
  46. data/lib/mimic/llm_cli/embed.rb +80 -0
  47. data/lib/mimic/llm_cli/engines.rb +44 -0
  48. data/lib/mimic/llm_cli/models.rb +31 -0
  49. data/lib/mimic/llm_cli/models_table.rb +119 -0
  50. data/lib/mimic/llm_cli/paths.rb +48 -0
  51. data/lib/mimic/llm_cli/root.rb +215 -0
  52. data/lib/mimic/llm_cli/wizard/context_preflight.rb +82 -0
  53. data/lib/mimic/llm_cli/wizard/embed.rb +89 -0
  54. data/lib/mimic/llm_cli/wizard/moe.rb +82 -0
  55. data/lib/mimic/llm_cli/wizard/parallel.rb +76 -0
  56. data/lib/mimic/llm_cli/wizard/setup.rb +211 -0
  57. data/lib/mimic/llm_cli.rb +5 -0
  58. data/lib/mimic/llm_config/embed.rb +48 -0
  59. data/lib/mimic/llm_config/llama_settings.rb +60 -0
  60. data/lib/mimic/llm_config/model_settings.rb +84 -0
  61. data/lib/mimic/llm_config/sampling.rb +54 -0
  62. data/lib/mimic/llm_config/settings.rb +92 -0
  63. data/lib/mimic/llm_config.rb +132 -0
  64. data/lib/mimic/model/footprint.rb +303 -0
  65. data/lib/mimic/model/gguf.rb +204 -0
  66. data/lib/mimic/model/header.rb +169 -0
  67. data/lib/mimic/model/library.rb +132 -0
  68. data/lib/mimic/model/memory.rb +126 -0
  69. data/lib/mimic/model/store.rb +46 -0
  70. data/lib/mimic/model/upstream.rb +81 -0
  71. data/lib/mimic/model.rb +116 -0
  72. data/lib/mimic/paths.rb +45 -0
  73. data/lib/mimic/result.rb +8 -0
  74. data/lib/mimic/ui.rb +89 -0
  75. data/lib/mimic/util/fmt.rb +15 -0
  76. data/lib/mimic/util/hf.rb +55 -0
  77. data/lib/mimic/util/host.rb +95 -0
  78. data/lib/mimic/util/probes.rb +255 -0
  79. data/lib/mimic/version.rb +3 -0
  80. data/lib/mimic.rb +22 -0
  81. metadata +186 -0
@@ -0,0 +1,94 @@
1
+ require "pathname"
2
+ require_relative "../paths"
3
+ require_relative "../util/probes"
4
+
5
+ module Mimic
6
+ module EngineCli
7
+ # The one table of engines: where each one's source comes from, how its
8
+ # build is configured, and which llama-server binaries count as its own.
9
+ #
10
+ # `flags` are the non-default configure options each engine's existing
11
+ # build was made with, recovered from its CMake cache. They live here
12
+ # because that cache was their only record, and it does not survive the
13
+ # checkout being moved.
14
+ #
15
+ # `binaries` is an ordered resolution list. :build is the engine's own
16
+ # checkout, :local is ~/.local/bin, :path is whatever `llama-server`
17
+ # resolves to. An engine that omits :path cannot borrow another build --
18
+ # PrismML's fork reads ternary weights no upstream build understands.
19
+ module Registry
20
+ DEFAULT = "llama.cpp"
21
+
22
+ ENGINES = {
23
+ "llama.cpp" => {
24
+ url: "https://github.com/ggml-org/llama.cpp.git",
25
+ branch: "master",
26
+ flags: [],
27
+ binaries: %i[path build local],
28
+ },
29
+ "ik_llama.cpp" => {
30
+ url: "https://github.com/ikawrakow/ik_llama.cpp.git",
31
+ branch: "main",
32
+ flags: ["-DLLAMA_CURL=OFF", "-DGGML_SCHED_MAX_COPIES=1"],
33
+ binaries: %i[build path],
34
+ },
35
+ "prism-v7-llama.cpp" => {
36
+ url: "https://github.com/PrismML-Eng/llama.cpp.git",
37
+ branch: "prism",
38
+ flags: [],
39
+ binaries: %i[build],
40
+ },
41
+ }.freeze
42
+
43
+ LOCAL_BINARY = Pathname.new(Dir.home) / ".local/bin/llama-server"
44
+
45
+ class << self
46
+ # Every engine this build offers. Drives the setup wizard's picker, the
47
+ # `--rollback` enum and the update sweep -- so a dev-only engine is
48
+ # absent here in an installed gem and present in a checkout.
49
+ def names = ENGINES.reject { |_, entry| entry[:dev] && !Paths.dev_build? }.keys
50
+
51
+ def entry(name) = ENGINES.fetch(name, ENGINES.fetch(DEFAULT))
52
+
53
+ def known?(name) = ENGINES.key?(name)
54
+
55
+ def url(name) = entry(name).fetch(:url)
56
+
57
+ def branch(name) = entry(name).fetch(:branch)
58
+
59
+ def flags(name) = entry(name).fetch(:flags)
60
+
61
+ def repo(name) = Paths::LLM_ROOT / (known?(name) ? name : DEFAULT)
62
+
63
+ def for(settings)
64
+ (settings.llama.ik? ? Engine::IkLlama : Engine::Llama).new(settings)
65
+ end
66
+
67
+ def binaries(name)
68
+ entry(name)[:binaries].map { |kind| resolve(kind, name) }
69
+ end
70
+
71
+ # Callers that must have *this* engine's binary rather than any
72
+ # llama-server on the box.
73
+ def builds(name)
74
+ entry(name)[:binaries].reject { |kind| kind == :path }.map { |kind| resolve(kind, name) }
75
+ end
76
+
77
+ def installed_binary
78
+ candidates = [Util::Probes.which("llama-server"), *names.flat_map { |name| builds(name) }]
79
+ candidates.compact.map(&:to_s).find { |c| File.file?(c) }
80
+ end
81
+
82
+ private
83
+
84
+ def resolve(kind, name)
85
+ case kind
86
+ when :path then Util::Probes.which("llama-server")
87
+ when :local then LOCAL_BINARY
88
+ else repo(name) / "build/bin/llama-server"
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,110 @@
1
+ require "open3"
2
+ require "etc"
3
+ require "pathname"
4
+ require_relative "../paths"
5
+ require_relative "../util/host"
6
+ require_relative "registry"
7
+
8
+ module Mimic
9
+ module EngineCli
10
+ module Source
11
+ LAST_GOOD_DIR = Paths::LLM_ROOT / "last-good"
12
+
13
+ BASE_BUILD_FLAGS = ["-DCMAKE_BUILD_TYPE=Release"].freeze
14
+
15
+ class << self
16
+ def local_status(name)
17
+ repo = Registry.repo(name)
18
+ return nil unless (repo / ".git").exist?
19
+
20
+ commit = git(repo, "rev-parse", "HEAD")
21
+ return nil if commit.empty?
22
+
23
+ { commit: commit[0, 9], dirty: !git(repo, "status", "--porcelain").empty? }
24
+ end
25
+
26
+ def build_command(name, ref: nil)
27
+ repo = Registry.repo(name)
28
+ checkout = ref ? "git checkout #{ref}" : "git pull --ff-only"
29
+ "cd #{repo} && \\\n" \
30
+ " #{checkout} && \\\n" \
31
+ " #{configure_step(repo, Registry.flags(name))} && \\\n" \
32
+ " cmake --build build -j #{jobs} --target llama-server"
33
+ end
34
+
35
+ # Fetch and report how far behind upstream the engine checkout is.
36
+ def status(name)
37
+ repo = Registry.repo(name)
38
+ return nil unless (repo / ".git").exist?
39
+
40
+ branch = Registry.branch(name)
41
+ Open3.capture2("git", "-C", repo.to_s, "fetch", "--quiet", "origin", branch)
42
+ local = git(repo, "rev-parse", "HEAD")
43
+ remote = git(repo, "rev-parse", "origin/#{branch}")
44
+ behind = git(repo, "rev-list", "--count", "HEAD..origin/#{branch}")
45
+ dirty = !git(repo, "status", "--porcelain").empty?
46
+ {
47
+ name: name,
48
+ repo: repo,
49
+ branch: branch,
50
+ local: local[0, 9],
51
+ remote: remote[0, 9],
52
+ behind: behind.to_i,
53
+ dirty: dirty,
54
+ subject: git(repo, "log", "-1", "--format=%s", "origin/#{branch}"),
55
+ }
56
+ end
57
+
58
+ def record_last_good(name, sha)
59
+ LAST_GOOD_DIR.mkpath
60
+ (LAST_GOOD_DIR / "#{name}.sha").write("#{sha}\n")
61
+ end
62
+
63
+ def last_good(name)
64
+ marker = LAST_GOOD_DIR / "#{name}.sha"
65
+ marker.file? ? marker.read.strip : nil
66
+ end
67
+
68
+ private
69
+
70
+ def configure_step(repo, engine_flags)
71
+ return "cmake -B build" if cache_usable?(repo)
72
+
73
+ flags = (BASE_BUILD_FLAGS + Util::Host.gpu_build_flags + engine_flags).join(" ")
74
+ reset = (repo / "build").exist? ? "rm -rf build && \\\n " : ""
75
+ "#{reset}cmake -B build #{flags}"
76
+ end
77
+
78
+ # CMake bakes absolute paths into its cache and refuses to run when
79
+ # either no longer matches, so a moved checkout can only be rebuilt
80
+ # from scratch.
81
+ def cache_usable?(repo)
82
+ cache = repo / "build/CMakeCache.txt"
83
+ return false unless cache.file?
84
+
85
+ cache_entry(cache, "CMAKE_HOME_DIRECTORY") == repo.to_s &&
86
+ cache_entry(cache, "CMAKE_CACHEFILE_DIR") == (repo / "build").to_s
87
+ end
88
+
89
+ def cache_entry(cache, key)
90
+ cache.each_line do |line|
91
+ name, value = line.chomp.split("=", 2)
92
+ return value if name == "#{key}:INTERNAL"
93
+ end
94
+ nil
95
+ rescue SystemCallError
96
+ nil
97
+ end
98
+
99
+ def git(repo, *args)
100
+ out, status = Open3.capture2("git", "-C", repo.to_s, *args)
101
+ status.success? ? out.strip : ""
102
+ end
103
+
104
+ def jobs
105
+ [(Etc.nprocessors || 4) - 2, 1].max
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,161 @@
1
+ require_relative "../ui"
2
+ require "json"
3
+ require_relative "../util/fmt"
4
+ require_relative "registry"
5
+ require_relative "../engine"
6
+ require_relative "../util/probes"
7
+
8
+ module Mimic
9
+ # What `llm status` reports: the config profile, corrected by whatever the
10
+ # running instance was actually started with.
11
+ module EngineCli
12
+ class Status
13
+ SAMPLING_LABELS = { "presence-penalty" => "presence", "repeat-penalty" => "repeat" }.freeze
14
+
15
+ def initialize(config)
16
+ @config = config
17
+ @engine = Registry.for(config)
18
+ end
19
+
20
+ def print
21
+ running = Engine::Llama.new(@config).running?
22
+ running ? print_running : print_stopped
23
+
24
+ ctx, kv, n_slots = running ? live_profile : config_profile
25
+ model_path = @config.model.path
26
+ field("Model", model_path.empty? ? "<unset>" : File.basename(model_path))
27
+ field("Context", "#{Util::Fmt.commas(ctx)} tokens#{slot_split(ctx, n_slots)}")
28
+ field("KV cache", kv)
29
+ cpu_moe = running ? cpu_moe_label : nil
30
+ field("CPU MoE", cpu_moe) if cpu_moe
31
+ field("Vision", vision_label(running))
32
+ field("Reasoning", reasoning_label(running))
33
+ field("Thinking", effort_label(running))
34
+ field("Sampling", sampling_label(running))
35
+ field("Binary", @config.llama.binary)
36
+ end
37
+
38
+ private
39
+
40
+ # Padding goes inside the colour: an escape sequence has no printing
41
+ # width, so formatting an already-coloured label pads by the wrong
42
+ # amount and the values stop lining up.
43
+ def field(label, value) = puts("#{Ui.green(format('%-10s', "#{label}:"))} #{value}")
44
+
45
+ def vision_label(running)
46
+ projector = (running && live_arg(:mmproj)) || @config.model.mmproj_path
47
+ projector ? "on (#{File.basename(projector)})" : "off"
48
+ end
49
+
50
+ def reasoning_label(running)
51
+ mode = (running && live_arg(:reasoning)) || @config.model.reasoning
52
+ return mode unless mode == "auto"
53
+
54
+ chat_template.thinks? ? "on" : "off"
55
+ end
56
+
57
+ def effort_label(running)
58
+ return "off" if reasoning_label(running) == "off"
59
+
60
+ level = (running && live_effort) || @config.model.reasoning_effort
61
+ return level if level
62
+
63
+ default = chat_template.default_effort
64
+ default ? "#{default} (template default)" : "the chat template has no levels"
65
+ end
66
+
67
+ # llama.cpp reports --reasoning-effort; ik carries the level inside
68
+ # --chat-template-kwargs.
69
+ def live_effort
70
+ live_arg(:reasoning_effort) || kwargs_effort(live_arg(:chat_template_kwargs))
71
+ end
72
+
73
+ def kwargs_effort(json)
74
+ json && JSON.parse(json)["reasoning_effort"]
75
+ rescue JSON::ParserError
76
+ nil
77
+ end
78
+
79
+ def chat_template
80
+ @chat_template ||= Engine::ChatTemplate.new(@config.model.path, model_alias: @config.model.name)
81
+ end
82
+
83
+ def sampling_label(running)
84
+ configured = @config.model.sampling.to_h
85
+ values = LlmConfig::Sampling::FLAGS.values.map do |flag|
86
+ live = running ? live_sampling(flag) : nil
87
+ "#{SAMPLING_LABELS.fetch(flag, flag)} #{live || configured[flag]}"
88
+ end
89
+ values.join(" ")
90
+ end
91
+
92
+ # llama.cpp's router re-emits the preset's `temp` key as `--temperature`;
93
+ # ik's launch record keeps the `--temp` the engine was handed.
94
+ def live_sampling(flag)
95
+ spellings = flag == "temp" ? %w[--temp --temperature] : ["--#{flag}"]
96
+ spellings.filter_map { |spelling| live_argv && arg_value(live_argv, spelling) }.first
97
+ end
98
+
99
+ def live_arg(key)
100
+ flag = arg_flags[key]
101
+ flag && live_argv && arg_value(live_argv, flag)
102
+ end
103
+
104
+ def arg_flags = @engine.class::ARG_FLAGS
105
+
106
+ def live_argv
107
+ return @live_argv if defined?(@live_argv)
108
+
109
+ @live_argv = @engine.live_args
110
+ end
111
+
112
+ def config_profile = [@config.model.context_length, @config.llama.cache_type, @config.llama.n_slots]
113
+
114
+ def live_profile
115
+ ctx, kv, n_slots = config_profile
116
+ argv = live_argv
117
+ unless argv
118
+ puts Ui.yellow(" (couldn't confirm live ctx/slots from server -- showing config.yaml, may be stale)")
119
+ return [ctx, kv, n_slots]
120
+ end
121
+
122
+ [(arg_value(argv, arg_flags[:ctx]) || ctx).to_i,
123
+ arg_value(argv, arg_flags[:cache_type]) || kv,
124
+ (arg_value(argv, arg_flags[:n_slots]) || n_slots).to_i]
125
+ end
126
+
127
+ # Only the engine that can offload experts reports the line, and only
128
+ # from the argv it really launched with -- the cached value is what the
129
+ # next start would use, not what this instance is running.
130
+ def cpu_moe_label
131
+ flag = arg_flags[:n_cpu_moe]
132
+ return nil unless flag && live_argv
133
+
134
+ layers = arg_value(live_argv, flag).to_i
135
+ layers.positive? ? "#{layers} layers offloaded" : "none (all layers on GPU)"
136
+ end
137
+
138
+ def print_running
139
+ pid = Engine::ServerProcess.chat.pid || "?"
140
+ field("Engine", @config.llama.engine)
141
+ field("Status", "running at #{@config.llama.base_url} (pid #{pid})")
142
+ end
143
+
144
+ def print_stopped
145
+ field("Engine", @config.llama.engine)
146
+ field("Status", Ui.yellow("not running"))
147
+ end
148
+
149
+ def slot_split(ctx, n_slots)
150
+ return "" unless n_slots > 1
151
+
152
+ " (#{n_slots} x #{Util::Fmt.commas(n_slots.positive? ? ctx / n_slots : ctx)})"
153
+ end
154
+
155
+ def arg_value(argv, flag)
156
+ idx = argv.index(flag)
157
+ idx ? argv[idx + 1] : nil
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,68 @@
1
+ require_relative "../ui"
2
+ require_relative "../result"
3
+ require_relative "registry"
4
+ require_relative "source"
5
+
6
+ module Mimic
7
+ module EngineCli
8
+ class Update
9
+ REBUILD_NOTE = "CUDA rebuild takes 10-25 min per engine. " \
10
+ "Restart llama-server afterwards (`llm restart`).".freeze
11
+ DIVIDER = ("-" * 64).freeze
12
+
13
+ def run
14
+ pending = Registry.names.filter_map { |name| check(name) }
15
+ return puts Ui.green("Update pass complete.") if pending.empty?
16
+
17
+ puts DIVIDER
18
+ puts Ui.bold("rebuilding: #{pending.map(&:first).join(', ')}")
19
+ puts Ui.yellow(REBUILD_NOTE)
20
+ failed = pending.reject { |name, command| run_build(name, command) }
21
+ puts DIVIDER
22
+ return puts Ui.green("Rebuild complete.") if failed.empty?
23
+
24
+ puts Ui.red("failed: #{failed.map(&:first).join(', ')}")
25
+ puts "`llm engines update --rollback <engine>` rebuilds the last-good commit."
26
+ end
27
+
28
+ def rollback(name)
29
+ sha = Source.last_good(name)
30
+ return Result.error("no recorded last-good commit for #{name}") unless sha
31
+
32
+ puts "Rebuilding #{name} at last-good #{sha[0, 9]}:\n"
33
+ return nil if run_build(name, Source.build_command(name, ref: sha))
34
+
35
+ Result.error("#{name} rollback build failed -- see output above")
36
+ end
37
+
38
+ private
39
+
40
+ # Returns [name, build command] when the checkout is behind, nil otherwise.
41
+ def check(name)
42
+ info = Source.status(name)
43
+ return skip(name, "not checked out -- skipped") if info.nil?
44
+ return skip(name, "working tree dirty -- skipped") if info[:dirty]
45
+
46
+ if info[:behind].zero?
47
+ puts "#{name}: #{Ui.green('up to date')} (#{info[:local]})"
48
+ return nil
49
+ end
50
+
51
+ Source.record_last_good(name, info[:local])
52
+ puts "#{name}: #{Ui.yellow("#{info[:behind]} commits behind")} " \
53
+ "#{info[:local]} -> #{info[:remote]} (#{info[:subject]})"
54
+ [name, Source.build_command(name)]
55
+ end
56
+
57
+ def skip(name, reason)
58
+ puts "#{name}: #{Ui.yellow(reason)}"
59
+ nil
60
+ end
61
+
62
+ def run_build(name, command)
63
+ puts "\n#{Ui.bold("[#{name}]")}"
64
+ system(command)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,7 @@
1
+ require_relative "engine"
2
+ require_relative "engine_cli/registry"
3
+ require_relative "engine_cli/llama_preset"
4
+ require_relative "engine_cli/source"
5
+ require_relative "engine_cli/builder"
6
+ require_relative "engine_cli/update"
7
+ require_relative "engine_cli/status"
@@ -0,0 +1,92 @@
1
+ require "net/http"
2
+ require "timeout"
3
+ require "open3"
4
+ require_relative "../paths"
5
+ require_relative "../util/probes"
6
+ require_relative "../engine"
7
+ require_relative "../agent/hermes"
8
+
9
+ module Mimic
10
+ module Health
11
+ # The install/service probes `verify` reports on. Each answers with a
12
+ # Result rather than raising, so one dead component never hides the rest.
13
+ class Checks
14
+ DB_QUERY = "SELECT (SELECT COUNT(*) FROM messages WHERE active=1)," \
15
+ " (SELECT COALESCE(SUM(input_tokens+output_tokens),0) FROM sessions)".freeze
16
+
17
+ def initialize(settings)
18
+ @settings = settings
19
+ end
20
+
21
+ # [[component name, Result], ...] in report order.
22
+ def to_a
23
+ [
24
+ ["llama.cpp", service(Engine::Llama.new(@settings).models_url)],
25
+ ["Hermes binary", hermes_install],
26
+ ["Hermes DB", db],
27
+ ["claude-code", claude_code],
28
+ ["Claude config dir", claude_config_dir],
29
+ ]
30
+ end
31
+
32
+ private
33
+
34
+ # Probe a service endpoint.
35
+ def service(url, headers: {}, timeout: 5)
36
+ res = Util::Probes.get(url, headers: headers, timeout: timeout)
37
+ Result.new(ok: res.is_a?(Net::HTTPSuccess), message: "HTTP #{res.code}")
38
+ rescue StandardError => e
39
+ Result.error(e.message)
40
+ end
41
+
42
+ # Check Hermes state DB is accessible and has active sessions.
43
+ #
44
+ # Shelled out rather than through the sqlite3 gem: this is the only
45
+ # query mimic makes, and it reads a schema Hermes owns and can rename
46
+ # under us, which is not worth a native extension in the gemspec.
47
+ def db
48
+ path = Agent::Hermes::STATE_DB
49
+ return Result.error("not found (#{path})") unless path.file?
50
+
51
+ out, err, status = Open3.capture3("sqlite3", "-readonly", path.to_s, DB_QUERY)
52
+ return Result.error(err.strip.lines.last.to_s.strip) unless status.success?
53
+
54
+ rows, tokens = out.strip.split("|")
55
+ Result.ok("#{rows} messages, #{tokens.to_i} tokens total")
56
+ rescue SystemCallError => e
57
+ Result.error(e.message)
58
+ end
59
+
60
+ # Verify claude-code binary exists and reports version.
61
+ def claude_code
62
+ out, status = Timeout.timeout(5) { Open3.capture2("claude", "--version") }
63
+ Result.new(ok: status.success?, message: out.strip)
64
+ rescue Errno::ENOENT
65
+ Result.error("not found")
66
+ rescue StandardError => e
67
+ Result.error(e.message)
68
+ end
69
+
70
+ # Hermes binary -- the venv one if present, else whatever is on PATH.
71
+ def hermes_install
72
+ binary = Agent::Hermes.find_binary
73
+ return Result.error("not found (looked in #{Agent::Hermes::VENV}/bin, PATH)") if binary.nil?
74
+
75
+ Result.ok(binary)
76
+ end
77
+
78
+ # The isolated config dir Claude Code runs against.
79
+ #
80
+ # Absent is not a failure: claude_env() creates and seeds it on every
81
+ # launch, so it simply means no local Claude Code session has started
82
+ # yet.
83
+ def claude_config_dir
84
+ config_dir = Paths::CLAUDE_CONFIG_DIR
85
+ return Result.ok("#{config_dir} (created on first launch)") unless config_dir.directory?
86
+
87
+ seeded = (config_dir / ".claude.json").exist?
88
+ Result.ok("#{config_dir}#{seeded ? '' : ' (not yet seeded)'}")
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,82 @@
1
+ require_relative "ui"
2
+ require "time"
3
+ require_relative "agent/hermes"
4
+ require_relative "health/checks"
5
+
6
+ module Mimic
7
+ module Health
8
+ LOG_LINE_PAT = /\A(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}),\d+ (WARNING|ERROR)/
9
+
10
+ class << self
11
+ # Verify the whole stack: what is installed, what is running, recent errors.
12
+ def run(settings)
13
+ checks = Checks.new(settings).to_a
14
+ errs = count_recent_errors
15
+
16
+ Ui.table(header: ["Component", "Status", "Detail"], rows: report_rows(checks, errs))
17
+ print_error_sample if errs[:error].positive?
18
+
19
+ checks.all? { |_, result| result.ok? } && errs[:error].zero?
20
+ end
21
+
22
+ private
23
+
24
+ # Count WARNING/ERROR lines in the Hermes logs within window.
25
+ def count_recent_errors(minutes: 60)
26
+ counts = { warn: 0, error: 0 }
27
+ cutoff = Time.now - (minutes * 60)
28
+ [Agent::Hermes::AGENT_LOG, Agent::Hermes::ERRORS_LOG].each { |logpath| tally_log(logpath, counts, cutoff) }
29
+ counts
30
+ end
31
+
32
+ # Grab last N ERROR lines from errors.log.
33
+ def recent_error_sample(n: 3)
34
+ return [] unless Agent::Hermes::ERRORS_LOG.exist?
35
+
36
+ lines = read_lines(Agent::Hermes::ERRORS_LOG) || []
37
+ lines.select { |line| line.include?(" ERROR ") }.last(n)
38
+ end
39
+
40
+ def tally_log(logpath, counts, cutoff)
41
+ return unless logpath.exist?
42
+
43
+ lines = read_lines(logpath)
44
+ return if lines.nil?
45
+
46
+ lines.reverse_each do |line|
47
+ m = LOG_LINE_PAT.match(line)
48
+ next unless m
49
+ break if Time.strptime(m[1], "%Y-%m-%d %H:%M:%S") < cutoff
50
+
51
+ m[2] == "WARNING" ? counts[:warn] += 1 : counts[:error] += 1
52
+ end
53
+ end
54
+
55
+ # A log the engine wrote can hold a truncated multibyte character;
56
+ # naming an external encoding on read transcodes nothing, so the bytes
57
+ # have to be scrubbed after the fact. nil means the log could not be read
58
+ # at all, which is not a reason to fail the whole check.
59
+ def read_lines(path)
60
+ path.binread.force_encoding(Encoding::UTF_8).scrub.lines(chomp: true)
61
+ rescue SystemCallError
62
+ nil
63
+ end
64
+
65
+ def report_rows(checks, errs)
66
+ rows = checks.map do |name, result|
67
+ [name, result.ok? ? Ui.green("OK") : Ui.red("FAIL"), result.message]
68
+ end
69
+ err_label = errs[:error].zero? ? "OK" : "#{errs[:error]} errors, #{errs[:warn]} warns"
70
+ rows << ["Errors (60m)", errs[:error].zero? ? Ui.green("OK") : Ui.red("ISSUES"), err_label]
71
+ end
72
+
73
+ def print_error_sample
74
+ puts "\n#{Ui.bold_red('Recent errors:')}"
75
+ recent_error_sample.each do |line|
76
+ short = line.length > 200 ? "#{line[0, 200]}..." : line
77
+ puts " #{short}"
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "../ui"
2
+ require "thor"
3
+
4
+ module Mimic
5
+ module LlmCli
6
+ # Abstract parent for every `llm` command class. It declares no commands
7
+ # of its own -- a public instance method here would become a Thor task on
8
+ # all of them -- only the colour/prompt handles and the one way this CLI
9
+ # reports a Result.
10
+ class Base < Thor
11
+ def self.exit_on_failure? = true
12
+
13
+ # Ctrl-C is the only way out of a cli-ui selection and it raises
14
+ # Interrupt, which Thor does not handle -- taking the documented exit
15
+ # would otherwise print a backtrace through thor/dispatch.
16
+ def self.start(given_args = ARGV, config = {})
17
+ super
18
+ rescue Interrupt
19
+ warn Ui.yellow("cancelled")
20
+ raise SystemExit, 130
21
+ end
22
+
23
+ def initialize(*args)
24
+ super
25
+ @prompt = Ui::Prompt.new
26
+ end
27
+
28
+ private
29
+
30
+ def report(result)
31
+ puts result.ok? ? Ui.green(result.message) : Ui.red(result.message)
32
+ raise SystemExit, 1 unless result.ok?
33
+ end
34
+ end
35
+ end
36
+ end