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,305 @@
1
+ require "json"
2
+ require "pathname"
3
+ require "open3"
4
+ require_relative "../paths"
5
+ require_relative "../util/probes"
6
+
7
+ module Mimic
8
+ module Agent
9
+ class Claude
10
+ SHIM_DIR = Paths::STATE_DIR / "bin"
11
+
12
+ DYNAMIC_SECTIONS_FLAG = "--exclude-dynamic-system-prompt-sections"
13
+ MIN_DYNAMIC_SECTIONS_VERSION = [2, 1, 98].freeze
14
+
15
+ # Claude ignores a smaller CLAUDE_CODE_AUTO_COMPACT_WINDOW and warns on every launch.
16
+ MIN_AUTO_COMPACT_WINDOW = 100_000
17
+ # Claude reserves min(max output, 20k) + 3k before compacting; a cap at or above 20k gains no room.
18
+ COMPACT_OUTPUT_RESERVE = 20_000
19
+ OUTPUT_TOKEN_MAX = 32_000
20
+ # Tokens left free when the agent's own estimate runs low: 10% of the window, at most this.
21
+ COMPACT_HEADROOM_MAX = 32_768
22
+ # Provider routing overrides ANTHROPIC_BASE_URL and would send the
23
+ # session past llama-server.
24
+ ENV_UNSET = %w[
25
+ ANTHROPIC_API_KEY CLAUDE_CODE_OAUTH_TOKEN ANTHROPIC_UNIX_SOCKET
26
+ CLAUDE_CODE_USE_FOUNDRY ANTHROPIC_FOUNDRY_BASE_URL ANTHROPIC_FOUNDRY_RESOURCE
27
+ CLAUDE_CODE_USE_BEDROCK CLAUDE_CODE_USE_VERTEX CLAUDE_CODE_USE_ANTHROPIC_AWS
28
+ CLAUDE_CODE_USE_ANTHROPIC_GOOGLE_CLOUD CLAUDE_CODE_USE_MANTLE
29
+ ].freeze
30
+
31
+ # WebSearch/WebFetch are Anthropic server-executed tools -- llama.cpp's
32
+ # /v1/messages bridge has no implementation (server log: "unsupported
33
+ # Responses tool type 'web_search'"), so they silently no-op instead of
34
+ # failing. Denied so the failure is visible; an MCP server named in
35
+ # Paths::MCP_SERVERS_PATH can take the internet-access job over.
36
+ DENIED_TOOLS = %w[WebSearch WebFetch].freeze
37
+
38
+ # SHIM_DIR holds a `claude` that execs this result -- resolving to it
39
+ # would loop.
40
+ def self.find_binary
41
+ path = ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).reject { |p| p == SHIM_DIR.to_s }.join(File::PATH_SEPARATOR)
42
+ Util::Probes.which("claude", path)
43
+ end
44
+
45
+ def initialize(settings)
46
+ @settings = settings
47
+ end
48
+
49
+ # The environment `mimic claude` would launch with, without launching.
50
+ # Used for the hermes-lcc bridge, where Hermes spawns Claude Code
51
+ # itself, so the bridge gets the same context/KV-cache handling.
52
+ def env
53
+ entry = model_entry
54
+ base = @settings.llama.base_url
55
+ # env diff (not a full merge): Process.exec/spawn apply a Hash first
56
+ # arg as a diff on top of the inherited environment -- a key must be
57
+ # explicit `=> nil` to unset it, omitting it leaves it inherited.
58
+ result = local_env(base, Agent::AUTH_TOKEN, entry)
59
+ ENV_UNSET.each { |name| result[name] = nil }
60
+ if self.class.find_binary
61
+ # Everything command() puts before the caller's own arguments: a
62
+ # bare `claude` typed by Hermes' skill has to reach the same model
63
+ # and overlay as `mimic claude` does.
64
+ shim = write_shim(command(entry["id"], [])[1..])
65
+ result["PATH"] = [shim.dirname.to_s, ENV.fetch("PATH", "")].join(File::PATH_SEPARATOR)
66
+ end
67
+ result
68
+ end
69
+
70
+ def launch(extra_args)
71
+ abort "claude binary not found -- install Claude Code first" if self.class.find_binary.nil?
72
+
73
+ entry = model_entry
74
+ base = @settings.llama.base_url
75
+ launch_env = local_env(base, Agent::AUTH_TOKEN, entry)
76
+ ENV_UNSET.each { |name| launch_env[name] = nil }
77
+ puts "mimic ready at #{base} · model #{entry['id']}"
78
+ code = Agent.spawn_and_wait(command(entry["id"], extra_args), launch_env)
79
+ raise SystemExit, code if code&.nonzero?
80
+ end
81
+
82
+ private
83
+
84
+ def ensure_config_dir
85
+ config_dir = Paths::CLAUDE_CONFIG_DIR
86
+ config_dir.mkpath
87
+ seed = config_dir / ".claude.json"
88
+ seed.write("#{JSON.generate({ hasCompletedOnboarding: true })}\n") unless seed.exist?
89
+ settings_file = config_dir / "settings.json"
90
+ user_settings = Pathname.new(Dir.home) / ".claude" / "settings.json"
91
+ settings_file.make_symlink(user_settings) if !settings_file.exist? && user_settings.exist?
92
+ merge_mcp_config(seed)
93
+ config_dir
94
+ end
95
+
96
+ # Whatever the user opted into, or nothing.
97
+ def mcp_servers
98
+ JSON.parse(Paths::MCP_SERVERS_PATH.read)
99
+ rescue SystemCallError, JSON::ParserError
100
+ {}
101
+ end
102
+
103
+ # Seeds/refreshes those entries in the isolated .claude.json -- merged,
104
+ # not overwritten, since CC writes its own runtime state (onboarding,
105
+ # tips, startup counts) into this same file and the user may have added
106
+ # servers of their own.
107
+ def merge_mcp_config(seed)
108
+ wanted = mcp_servers
109
+ return if wanted.empty?
110
+
111
+ data = seed.exist? ? JSON.parse(seed.read) : {}
112
+ current = data["mcpServers"].is_a?(Hash) ? data["mcpServers"] : {}
113
+ return if wanted.all? { |name, spec| current[name] == spec }
114
+
115
+ data["mcpServers"] = current.merge(wanted)
116
+ seed.write("#{JSON.generate(data)}\n")
117
+ rescue JSON::ParserError
118
+ nil
119
+ end
120
+
121
+ def model_entry
122
+ { "id" => @settings.model.name, "context_length" => @settings.model.context_length }
123
+ end
124
+
125
+ def version
126
+ executable = Util::Probes.which("claude")
127
+ return nil if executable.nil?
128
+
129
+ out, = Open3.capture2(executable, "--version")
130
+ match = out.match(/(\d+)\.(\d+)\.(\d+)/)
131
+ match ? match.captures.map(&:to_i) : [0]
132
+ rescue Errno::ENOENT
133
+ [0]
134
+ end
135
+
136
+ # Status line whose ctx denominator is the served window.
137
+ def status_line
138
+ used = %w[input_tokens cache_creation_input_tokens cache_read_input_tokens]
139
+ .map { |field| "(.context_window.current_usage.#{field} // 0)" }.join(" + ")
140
+ window = @settings.model.context_length.to_i
141
+ jq = %("\\(.model.display_name) | effort: \\(.effort.level // "?") | ctx \\(((#{used}) / 1000) | floor)K/#{window / 1000}K")
142
+ { "type" => "command", "command" => "jq -r '#{jq}'" }
143
+ end
144
+
145
+ # mimic's Claude Code settings on top of upstream's defaults. Model
146
+ # aliases keep subagent/haiku routing on the local model instead of a
147
+ # cloud one that isn't served here. COMMENT_GATE_OFF spares the served
148
+ # model: the gate's judge call lands on the same single-slot llama-server
149
+ # and evicts the session's cached prefix, costing a full re-prefill.
150
+ def extra_env
151
+ model_alias = @settings.model.name
152
+ {
153
+ "COMMENT_GATE_OFF" => "1",
154
+ "ANTHROPIC_CUSTOM_MODEL_OPTION" => model_alias,
155
+ "ANTHROPIC_SMALL_FAST_MODEL" => model_alias,
156
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL" => model_alias,
157
+ "ANTHROPIC_DEFAULT_SONNET_MODEL" => model_alias,
158
+ "ANTHROPIC_DEFAULT_OPUS_MODEL" => model_alias,
159
+ "CLAUDE_CONFIG_DIR" => ensure_config_dir.to_s,
160
+ }
161
+ end
162
+
163
+ # Claude applies user/project settings env after the process env, so
164
+ # the local env is pinned again here, at the command-line tier, with
165
+ # ENV_UNSET blanked.
166
+ # A forwarded --settings (user) sits underneath: Claude keeps only the
167
+ # last --settings, so it is merged in rather than passed separately.
168
+ def settings_overlay(model_id, user = {})
169
+ pinned = local_env(@settings.llama.base_url, Agent::AUTH_TOKEN, model_entry).except("CLAUDE_CONFIG_DIR")
170
+ overlay = {
171
+ "env" => ENV_UNSET.to_h { |name| [name, ""] }.merge(
172
+ pinned,
173
+ "CLAUDE_CODE_ATTRIBUTION_HEADER" => "0",
174
+ "CLAUDE_CODE_SUBAGENT_MODEL" => "inherit",
175
+ ),
176
+ "availableModels" => [model_id],
177
+ # A user's apiKeyHelper would still run and send its cloud key to llama-server.
178
+ "apiKeyHelper" => "",
179
+ "permissions" => { "ask" => %w[Task Agent], "deny" => DENIED_TOOLS },
180
+ "statusLine" => status_line,
181
+ }
182
+ user.merge(
183
+ overlay,
184
+ "env" => user.fetch("env", {}).merge(overlay["env"]),
185
+ "permissions" => joined_permissions(user.fetch("permissions", {}), overlay["permissions"]),
186
+ )
187
+ end
188
+
189
+ def joined_permissions(user, mimic)
190
+ user.merge(mimic) { |_rule, theirs, ours| theirs.is_a?(Array) ? (theirs + ours).uniq : ours }
191
+ end
192
+
193
+ def flags(model_id, user = {})
194
+ settings = ["--settings", JSON.generate(settings_overlay(model_id, user))]
195
+ current = version
196
+ return settings if current && !Agent.version_at_least?(current, MIN_DYNAMIC_SECTIONS_VERSION)
197
+
198
+ [DYNAMIC_SECTIONS_FLAG, *settings]
199
+ end
200
+
201
+ def local_env(base, key, entry)
202
+ env = {
203
+ "ANTHROPIC_BASE_URL" => base,
204
+ "ANTHROPIC_AUTH_TOKEN" => key,
205
+ "ANTHROPIC_MODEL" => entry["id"],
206
+ "CLAUDE_CODE_ATTRIBUTION_HEADER" => "0",
207
+ # Per-tool countdown reminders change the system prefix.
208
+ "CLAUDE_CODE_TOTAL_TOKENS_REMINDER" => "off",
209
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC" => "1",
210
+ "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS" => "1",
211
+ "CLAUDE_CODE_NO_FLICKER" => "1",
212
+ }
213
+ window = entry["context_length"]&.to_i
214
+ if window&.positive?
215
+ # Claude assumes 200k for a model id it does not know and clamps
216
+ # AUTO_COMPACT_WINDOW to [100k, that]; MAX_CONTEXT_TOKENS sets the window.
217
+ env["CLAUDE_CODE_MAX_CONTEXT_TOKENS"] = window.to_s
218
+ # Blank, not omitted, below the minimum so an inherited value cannot apply.
219
+ env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] = window >= MIN_AUTO_COMPACT_WINDOW ? window.to_s : ""
220
+ env["CLAUDE_AUTOCOMPACT_PCT_OVERRIDE"] = ((window - [window / 10, COMPACT_HEADROOM_MAX].min) * 100 / window).to_s
221
+ output = [window / 4, OUTPUT_TOKEN_MAX].min.clamp(1..)
222
+ env["CLAUDE_CODE_MAX_OUTPUT_TOKENS"] = output.to_s if output < COMPACT_OUTPUT_RESERVE
223
+ end
224
+ env.merge(extra_env)
225
+ end
226
+
227
+ # An unreadable forwarded --settings keeps the old order: it goes
228
+ # first and mimic's overlay, applied last, replaces it.
229
+ def command(model_id, extra_args)
230
+ user_settings, rest = split_user_settings(extra_args)
231
+ user = read_user_settings(user_settings)
232
+ return ["claude", "--model", model_id, *flags(model_id, user), *rest] if user
233
+
234
+ ["claude", *user_settings, "--model", model_id, *flags(model_id), *rest]
235
+ end
236
+
237
+ # The forwarded --settings values merged in order, each inline JSON or
238
+ # a file path, or nil when one cannot be read.
239
+ def read_user_settings(settings_args)
240
+ values = []
241
+ index = 0
242
+ while index < settings_args.length
243
+ if settings_args[index] == "--settings"
244
+ values << settings_args[index + 1]
245
+ index += 2
246
+ else
247
+ values << settings_args[index].delete_prefix("--settings=")
248
+ index += 1
249
+ end
250
+ end
251
+ values.reduce({}) do |merged, value|
252
+ text = value.lstrip.start_with?("{") ? value : File.read(File.expand_path(value))
253
+ loaded = JSON.parse(text)
254
+ return nil unless loaded.is_a?(Hash) && %w[env permissions].all? { |key| loaded.fetch(key, {}).is_a?(Hash) }
255
+
256
+ merged.merge(
257
+ loaded,
258
+ "env" => merged.fetch("env", {}).merge(loaded.fetch("env", {})),
259
+ "permissions" => merged.fetch("permissions", {}).merge(loaded.fetch("permissions", {})),
260
+ )
261
+ end
262
+ rescue SystemCallError, JSON::ParserError
263
+ nil
264
+ end
265
+
266
+ # A forwarded --settings goes before mimic's, so mimic's overlay is the
267
+ # one Claude applies last. Anything after `--` stays where it is.
268
+ def split_user_settings(args)
269
+ separator = args.index("--") || args.length
270
+ head = args[0...separator]
271
+ user_settings = []
272
+ rest = []
273
+ index = 0
274
+ while index < head.length
275
+ arg = head[index]
276
+ if arg == "--settings" && index + 1 < head.length
277
+ user_settings.push(arg, head[index + 1])
278
+ index += 2
279
+ next
280
+ end
281
+ (arg.start_with?("--settings=") ? user_settings : rest) << arg
282
+ index += 1
283
+ end
284
+ [user_settings, rest + args[separator..]]
285
+ end
286
+
287
+ # A `claude` that re-applies upstream's launch flags to any invocation.
288
+ # Hermes' claude-code skill types its own `claude ...` command line, so
289
+ # the only place to reattach the --settings overlay is a PATH shim.
290
+ def write_shim(launch_flags)
291
+ SHIM_DIR.mkpath
292
+ shim = SHIM_DIR / "claude"
293
+ real = Pathname.new(self.class.find_binary).realpath.to_s
294
+ shim.write(<<~SH)
295
+ #!/usr/bin/env ruby
296
+ REAL = #{real.inspect}
297
+ FLAGS = #{launch_flags.inspect}
298
+ exec(REAL, *FLAGS, *ARGV)
299
+ SH
300
+ shim.chmod(0o755)
301
+ shim
302
+ end
303
+ end
304
+ end
305
+ end
@@ -0,0 +1,254 @@
1
+ require "json"
2
+ require "pathname"
3
+ require "open3"
4
+ require "fileutils"
5
+ require_relative "../paths"
6
+ require_relative "../util/probes"
7
+
8
+ module Mimic
9
+ module Agent
10
+ class Codex
11
+ PROFILE = "unsloth_api"
12
+ ENV_KEY = "UNSLOTH_STUDIO_AUTH_TOKEN"
13
+ PROVIDER_HEADER = "[model_providers.#{PROFILE}]"
14
+ MODEL_CATALOG_MIN_VERSION = [0, 110, 0].freeze
15
+ # An older codex exits with "config profile `unsloth_api` not found".
16
+ PROFILE_FILE_MIN_VERSION = [0, 134, 0].freeze
17
+ USER_FILES = %w[AGENTS.md AGENTS.override.md skills rules prompts].freeze
18
+ PATCH_LINE_ENDINGS_MIN_VERSION = [0, 148, 0].freeze
19
+ ENV_UNSET = %w[OPENAI_API_KEY CODEX_API_KEY CODEX_ACCESS_TOKEN].freeze
20
+ FALLBACK_PROMPT = Pathname.new(__dir__) / "codex_fallback_prompt.md"
21
+
22
+ MANAGED_KEYS = /\s*(oss_provider|model_provider|model|model_context_window)\s*=/
23
+
24
+ # Codex's 300000 ms default counts the whole byte-less period, and llama-server
25
+ # sends nothing while it processes the prompt. Its reconnect lands on another
26
+ # parallel slot that shares no KV prefix, so the retries restart prefill from
27
+ # zero and never converge. 20 minutes clears a slow CPU-MoE first turn.
28
+ STREAM_IDLE_TIMEOUT_MS = 1_200_000
29
+
30
+ def initialize(settings)
31
+ @settings = settings
32
+ end
33
+
34
+ def launch(extra_args)
35
+ if Util::Probes.which("codex").nil? && !ENV["MIMIC_ALLOW_INSTALL"]
36
+ abort "codex binary not found -- install it with `npm install -g @openai/codex`"
37
+ end
38
+ abort_if_too_old_for_profile_file
39
+
40
+ entry = model_entry
41
+ base = @settings.llama.base_url
42
+ seed_home
43
+ link_user_files
44
+ write_config(base, entry)
45
+ launch_env = { ENV_KEY => Agent::AUTH_TOKEN, "CODEX_HOME" => home.to_s }
46
+ ENV_UNSET.each { |name| launch_env[name] = nil }
47
+ puts "mimic ready at #{base} · model #{entry['id']}"
48
+ code = Agent.spawn_and_wait(command(extra_args), launch_env)
49
+ raise SystemExit, code if code&.nonzero?
50
+ end
51
+
52
+ private
53
+
54
+ def model_entry
55
+ { "id" => @settings.model.name, "context_length" => @settings.model.context_length }
56
+ end
57
+
58
+ def merge_config(existing, base)
59
+ chunks = existing.split(/(?=^\[)/)
60
+ chunks[0] = "" if chunks.empty?
61
+ unless chunks[0].to_s.match?(/^\s*oss_provider\s*=/)
62
+ chunks[0] = chunks[0].to_s
63
+ chunks[0] += "\n" unless chunks[0].empty? || chunks[0].end_with?("\n")
64
+ chunks[0] += "oss_provider = \"#{PROFILE}\"\n"
65
+ end
66
+ stale = [PROVIDER_HEADER, "#{PROVIDER_HEADER[0..-2]}."]
67
+ text = chunks.reject { |c| stale.any? { |s| c.start_with?(s) } }.join
68
+ text += "\n" unless text.end_with?("\n")
69
+ text += "\n" unless text.end_with?("\n\n")
70
+ text +
71
+ "#{PROVIDER_HEADER}\n" \
72
+ "name = \"Unsloth Studio\"\n" \
73
+ "base_url = #{JSON.generate("#{base}/v1")}\n" \
74
+ "env_key = \"#{ENV_KEY}\"\n" \
75
+ "wire_api = \"responses\"\n" \
76
+ "requires_openai_auth = false\n" \
77
+ "stream_idle_timeout_ms = #{STREAM_IDLE_TIMEOUT_MS}\n"
78
+ end
79
+
80
+ # nil: no codex on PATH yet, so assume a current build. An unreadable
81
+ # version counts as too old.
82
+ def codex_version
83
+ executable = Util::Probes.which("codex")
84
+ return nil if executable.nil?
85
+
86
+ out, status = Open3.capture2(executable, "--version")
87
+ match = status.success? && out.match(/(\d+)\.(\d+)\.(\d+)/)
88
+ match ? match.captures.map(&:to_i) : [0]
89
+ rescue Errno::ENOENT
90
+ [0]
91
+ end
92
+
93
+ # [0] means the version could not be read, and that still launches.
94
+ def abort_if_too_old_for_profile_file
95
+ version = codex_version
96
+ return if version.nil? || version.size < 3 || Agent.version_at_least?(version, PROFILE_FILE_MIN_VERSION)
97
+
98
+ abort "Codex #{PROFILE_FILE_MIN_VERSION.join('.')} or newer is needed, but " \
99
+ "#{Util::Probes.which('codex')} is #{version.join('.')}. " \
100
+ "Update it with `npm install -g @openai/codex@latest`."
101
+ end
102
+
103
+ def supports?(version, floor)
104
+ version.nil? || Agent.version_at_least?(version, floor)
105
+ end
106
+
107
+ # Conservative metadata for a model unknown to Codex's built-in catalog.
108
+ def model_catalog(model)
109
+ window = model["context_length"]
110
+ entry = {
111
+ "slug" => model["id"],
112
+ "display_name" => model["id"],
113
+ "description" => "Model served by llama-server",
114
+ "supported_reasoning_levels" => [],
115
+ "shell_type" => "default",
116
+ "visibility" => "none",
117
+ "supported_in_api" => true,
118
+ "priority" => 99,
119
+ "availability_nux" => nil,
120
+ "upgrade" => nil,
121
+ "base_instructions" => FALLBACK_PROMPT.read,
122
+ "supports_reasoning_summaries" => false,
123
+ "supports_reasoning_summary_parameter" => false,
124
+ "support_verbosity" => false,
125
+ "default_verbosity" => nil,
126
+ "apply_patch_tool_type" => "freeform",
127
+ "truncation_policy" => { "mode" => "bytes", "limit" => 10_000 },
128
+ "supports_parallel_tool_calls" => false,
129
+ "experimental_supported_tools" => [],
130
+ }
131
+ if window.to_i.positive?
132
+ entry["context_window"] = window.to_i
133
+ entry["max_context_window"] = window.to_i
134
+ end
135
+ { "models" => [entry] }
136
+ end
137
+
138
+ def write_config(base, model)
139
+ home.mkpath
140
+ config = home / "config.toml"
141
+ existing = config.exist? ? config.read : ""
142
+ merged = merge_config(existing, base)
143
+ config.write(merged) if merged != existing
144
+
145
+ profile_text = +"oss_provider = \"#{PROFILE}\"\n" \
146
+ "model_provider = \"#{PROFILE}\"\n" \
147
+ "model = #{JSON.generate(model['id'])}\n"
148
+ version = codex_version
149
+ if supports?(version, PATCH_LINE_ENDINGS_MIN_VERSION)
150
+ profile_text << "suppress_unstable_features_warning = true\n" \
151
+ "features.apply_patch_preserve_line_endings = true\n"
152
+ end
153
+ if supports?(version, MODEL_CATALOG_MIN_VERSION) && FALLBACK_PROMPT.file?
154
+ catalog = home / "model-catalog.json"
155
+ catalog_text = "#{JSON.pretty_generate(model_catalog(model))}\n"
156
+ catalog.write(catalog_text) if !catalog.exist? || catalog.read != catalog_text
157
+ profile_text << "model_catalog_json = #{JSON.generate(catalog.basename.to_s)}\n"
158
+ end
159
+ window = model["context_length"]
160
+ profile_text << "model_context_window = #{window.to_i}\n" if window.to_i.positive?
161
+ profile = home / "#{PROFILE}.config.toml"
162
+ profile.write(profile_text) if !profile.exist? || profile.read != profile_text
163
+ end
164
+
165
+ def command(extra_args)
166
+ ["codex", "--oss", "--profile", PROFILE, *extra_args]
167
+ end
168
+
169
+ def strip_newlines(s)
170
+ s.sub(/\A\n+/, "").sub(/\n+\z/, "")
171
+ end
172
+
173
+ # Carry the user's own Codex settings into the agent home, minus
174
+ # provider wiring: MCP servers, project trust, approval policy and
175
+ # personality stay; model/provider selection belongs to write_config.
176
+ def seed_config(text)
177
+ chunks = text.split(/^(?=\[)/)
178
+ # each_line + chomp: no trailing empty element for a final newline,
179
+ # unlike String#split("\n").
180
+ preamble = chunks[0].to_s.each_line.map(&:chomp).reject { |line| line.match?(MANAGED_KEYS) }.join("\n")
181
+ tables = chunks[1..].to_a.reject { |c| c.start_with?("[model_providers.") }
182
+ "#{strip_newlines([strip_newlines(preamble), *tables].join("\n"))}\n"
183
+ end
184
+
185
+ def home
186
+ Paths::AGENTS_DIR / "codex"
187
+ end
188
+
189
+ # Give the agent home a starting config once, then never touch it
190
+ # again -- only provider wiring is refreshed on each launch by
191
+ # write_config.
192
+ def seed_home
193
+ target = home
194
+ target.mkpath(mode: 0o700)
195
+ session = target / "config.toml"
196
+ return if session.exist?
197
+
198
+ user_config = Pathname.new(Dir.home) / ".codex/config.toml"
199
+ session.write(user_config.file? ? seed_config(user_config.read) : "")
200
+ end
201
+
202
+ # An entry the home already has is the home's own and stays, so the
203
+ # commands approved in mimic sessions (rules/default.rules) are kept.
204
+ # Codex makes skills/.system in every home, so the user's entries go
205
+ # into an existing folder one by one rather than being skipped.
206
+ def link_user_files
207
+ user_codex = Pathname.new(Dir.home) / ".codex"
208
+ USER_FILES.each do |name|
209
+ source = user_codex / name
210
+ target = home / name
211
+ drop_dangling_links(target)
212
+ next unless source.exist?
213
+
214
+ # Codex appends approvals to rules/default.rules: a linked folder
215
+ # would write them into the user's own file.
216
+ target.mkpath if name == "rules" && source.directory? && !present?(target)
217
+ if !present?(target)
218
+ target.make_symlink(source.realpath)
219
+ elsif source.directory? && target.directory? && !target.symlink?
220
+ source.children.each { |child| add_user_child(name, child, target) }
221
+ end
222
+ end
223
+ end
224
+
225
+ def add_user_child(name, child, folder)
226
+ if name == "rules" && child.file?
227
+ copy_user_rule(child, folder)
228
+ else
229
+ link = folder / child.basename
230
+ link.make_symlink(child.realpath) unless present?(link)
231
+ end
232
+ end
233
+
234
+ # Codex skips a symlinked *.rules file, so a rule is hard-linked, or
235
+ # copied across disks. The home's own default.rules stays its own.
236
+ def copy_user_rule(rule, folder)
237
+ copy = folder / rule.basename
238
+ copy = folder / "user-#{rule.basename}" if rule.basename.to_s == "default.rules" || present?(copy)
239
+ return if present?(copy)
240
+
241
+ File.link(rule.realpath, copy)
242
+ rescue SystemCallError
243
+ FileUtils.cp(rule.realpath, copy)
244
+ end
245
+
246
+ def drop_dangling_links(target)
247
+ candidates = target.directory? && !target.symlink? ? target.children : [target]
248
+ candidates.each { |path| path.delete if path.symlink? && !path.exist? }
249
+ end
250
+
251
+ def present?(path) = path.exist? || path.symlink?
252
+ end
253
+ end
254
+ end