Module: RobotLab::To::Guards::PathResolution

Defined in:
lib/robot_lab/to/guards/path_resolution.rb

Overview

Shared, pure path helpers used by the file guards. No hook state here so each function is trivially testable in isolation.

Constant Summary collapse

PATH_KEYS =

Keys a tool might use to carry the destination path.

%w[path file_path].freeze

Class Method Summary collapse

Class Method Details

.normalize(file_path, cwd: Dir.pwd) ⇒ String

Normalize a model-supplied path against cwd.

Two deterministic rewrites (ported from little-coder write-guard):

1. "/<single-segment>" (e.g. "/foo.md") -> "<cwd>/foo.md" — small
 models anchor at filesystem root when handed an "absolute path"
 schema; a real system path always has an intermediate dir.
2. bare/relative path -> resolved against cwd.

Any absolute path with an intermediate directory is left untouched.

Parameters:

  • file_path (String)
  • cwd (String) (defaults to: Dir.pwd)

Returns:

  • (String)

    the resolved absolute path



38
39
40
41
42
43
44
45
46
# File 'lib/robot_lab/to/guards/path_resolution.rb', line 38

def normalize(file_path, cwd: Dir.pwd)
  if file_path.match?(%r{\A/[^/]+\z})
    File.join(cwd, file_path.delete_prefix("/"))
  elsif !file_path.start_with?("/")
    File.join(cwd, file_path)
  else
    file_path
  end
end

.raw_path(args) ⇒ String?

Extract the raw path string from tool args, accepting either path (RubyLLM/MCP style) or file_path.

Parameters:

  • args (Hash)

Returns:

  • (String, nil)


19
20
21
22
23
24
# File 'lib/robot_lab/to/guards/path_resolution.rb', line 19

def raw_path(args)
  key = PATH_KEYS.find { |k| args[k].is_a?(String) || args[k.to_sym].is_a?(String) }
  return nil unless key

  (args[key] || args[key.to_sym]).to_s
end

.resolve(args, cwd: Dir.pwd) ⇒ String?

Resolve a tool's path argument to a concrete absolute path, or nil if the args carry no resolvable path.

Parameters:

  • args (Hash)
  • cwd (String) (defaults to: Dir.pwd)

Returns:

  • (String, nil)


54
55
56
57
# File 'lib/robot_lab/to/guards/path_resolution.rb', line 54

def resolve(args, cwd: Dir.pwd)
  raw = raw_path(args)
  raw && normalize(raw, cwd: cwd)
end