Class: RubyLLM::Toolbox::Tools::ApplyPatch

Inherits:
Base
  • Object
show all
Includes:
GitHelpers
Defined in:
lib/ruby_llm/toolbox/tools/apply_patch.rb

Overview

EXEC. Applies a unified diff to files in fs_root using ‘git apply`. Complements edit_file when a model wants to emit a whole multi-hunk patch. The patch is validated with `git apply –check` before anything is written. Patch paths are also checked against PathJail so a patch cannot escape fs_root even when fs_root is a subdirectory of a git repo. Works with or without a git repository.

Constant Summary

Constants included from GitHelpers

GitHelpers::GIT_ENV, GitHelpers::REF_RE

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods included from GitHelpers

#git_result, #repo_relative, #run_git, #valid_ref?

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(patch:, check: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_llm/toolbox/tools/apply_patch.rb', line 30

def execute(patch:, check: false)
  diff = patch.to_s
  diff += "\n" unless diff.end_with?("\n")
  return error("patch is empty", code: :empty_patch) if diff.strip.empty?

  validate_patch_paths!(diff)

  verify = run_git("apply", "--check", stdin: diff)
  unless succeeded?(verify)
    return error("patch does not apply cleanly: #{message(verify)}", code: :patch_failed)
  end

  files = changed_files(diff)
  return "Patch applies cleanly (dry run). Affected files: #{files.join(', ')}" if check

  result = run_git("apply", stdin: diff)
  return error("apply failed: #{message(result)}", code: :patch_failed) unless succeeded?(result)

  "Applied patch to #{files.size} file#{files.size == 1 ? '' : 's'}: #{files.join(', ')}"
rescue Safety::PathJail::Jailbreak => e
  error("patch path escapes fs_root: #{e.message}", code: :patch_failed)
rescue Errno::ENOENT
  error("git is not available on the host", code: :unavailable)
end