Class: Space::Architect::Harness::OpenCodeHarness

Inherits:
Object
  • Object
show all
Defined in:
lib/space_architect/harness.rb

Instance Method Summary collapse

Constructor Details

#initialize(model:, max_turns:, bin: nil, config_dir:, effort: nil) ⇒ OpenCodeHarness

Returns a new instance of OpenCodeHarness.



165
166
167
168
169
170
171
# File 'lib/space_architect/harness.rb', line 165

def initialize(model:, max_turns:, bin: nil, config_dir:, effort: nil)
  @model      = model
  @max_turns  = max_turns
  @bin        = bin || ENV.fetch("ARCHITECT_OPENCODE_BIN", "opencode")
  @config_dir = Pathname.new(config_dir)
  @effort     = effort
end

Instance Method Details

#builder_configObject

Returns the agent config hash (deterministic, unit-testable).



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/space_architect/harness.rb', line 174

def builder_config
  cfg = {
    "agent" => {
      "builder" => {
        "steps" => @max_turns,
        "permission" => {
          "bash" => {
            "git commit *"   => "deny",
            "git push *"     => "deny",
            "git reset *"    => "deny",
            "git rebase *"   => "deny",
            "git checkout *" => "deny",
            "*"              => "allow"
          }
        }
      }
    }
  }
  cfg.merge!(reasoning_provider_config) if @effort
  cfg
end

#run(prompt_path:, run_log_path:, chdir:) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/space_architect/harness.rb', line 196

def run(prompt_path:, run_log_path:, chdir:)
  prompt_path  = Pathname.new(prompt_path)
  run_log_path = Pathname.new(run_log_path)
  config_path  = write_config

  env = {
    "OPENCODE_CONFIG"                 => config_path.to_s,
    "OPENCODE_DISABLE_PROJECT_CONFIG" => "1"
  }

  File.open(prompt_path, "r") do |prompt_io|
    File.open(run_log_path, "w") do |log|
      status = Sync do
        Async::Process.spawn(env, *argv(chdir),
                             chdir: chdir.to_s, in: prompt_io, out: log, err: log)
      end
      status.exitstatus
    end
  end
end

#run_detached(prompt_path:, run_log_path:, chdir:) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/space_architect/harness.rb', line 217

def run_detached(prompt_path:, run_log_path:, chdir:)
  prompt_path  = Pathname.new(prompt_path)
  run_log_path = Pathname.new(run_log_path)
  config_path  = write_config

  env = {
    "OPENCODE_CONFIG"                 => config_path.to_s,
    "OPENCODE_DISABLE_PROJECT_CONFIG" => "1"
  }

  prompt_io = File.open(prompt_path, "r")
  log       = File.open(run_log_path, "w")
  begin
    pid = Process.spawn(env, *argv(chdir), chdir: chdir.to_s, pgroup: true,
                        in: prompt_io, out: log, err: log)
    Process.detach(pid)
  ensure
    prompt_io.close
    log.close
  end
  pid
end