polyrun 3.0.1 → 3.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +16 -2
- data/docs/QUALITY.md +82 -0
- data/docs/QUALITY_CONFIGURATION.md +95 -0
- data/ext/polyrun_coverage_merge/coverage_merge.c +13 -26
- data/lib/polyrun/cli/ci_shard_run_command.rb +1 -1
- data/lib/polyrun/cli/graph_command.rb +68 -0
- data/lib/polyrun/cli/help.rb +5 -1
- data/lib/polyrun/cli/helpers.rb +3 -2
- data/lib/polyrun/cli/mutation_command.rb +67 -0
- data/lib/polyrun/cli/quality_commands.rb +65 -0
- data/lib/polyrun/cli/run_shards_parallel_wait.rb +48 -70
- data/lib/polyrun/cli/run_shards_plan_options.rb +1 -1
- data/lib/polyrun/cli/run_shards_worker_interrupt.rb +16 -0
- data/lib/polyrun/cli/tool_options.rb +48 -0
- data/lib/polyrun/cli.rb +17 -1
- data/lib/polyrun/config/effective.rb +9 -0
- data/lib/polyrun/config/resolver.rb +9 -0
- data/lib/polyrun/config/schema.rb +8 -3
- data/lib/polyrun/config/suite.rb +6 -1
- data/lib/polyrun/config/tool_defaults.rb +42 -0
- data/lib/polyrun/config/tool_validation.rb +79 -0
- data/lib/polyrun/config/tools.rb +120 -0
- data/lib/polyrun/config.rb +6 -4
- data/lib/polyrun/coverage/collector.rb +7 -1
- data/lib/polyrun/coverage/example_diff.rb +4 -8
- data/lib/polyrun/error.rb +3 -0
- data/lib/polyrun/graph/client.rb +64 -0
- data/lib/polyrun/graph/error.rb +13 -0
- data/lib/polyrun/graph/index.rb +75 -0
- data/lib/polyrun/graph/inspect.rb +58 -0
- data/lib/polyrun/graph/plan.rb +134 -0
- data/lib/polyrun/graph/planner.rb +117 -0
- data/lib/polyrun/graph/syntax.rb +114 -0
- data/lib/polyrun/graph/transaction.rb +81 -0
- data/lib/polyrun/graph/worker.rb +55 -0
- data/lib/polyrun/graph/workspace.rb +117 -0
- data/lib/polyrun/minitest_support.rb +1 -1
- data/lib/polyrun/mutation/candidates.rb +91 -0
- data/lib/polyrun/mutation/operators.rb +51 -0
- data/lib/polyrun/mutation/runner.rb +133 -0
- data/lib/polyrun/mutation/subjects.rb +88 -0
- data/lib/polyrun/process_stdio.rb +13 -8
- data/lib/polyrun/quality/command.rb +84 -0
- data/lib/polyrun/quality/measure.rb +38 -0
- data/lib/polyrun/quality/observe.rb +53 -0
- data/lib/polyrun/quality/observe_hook.rb +103 -0
- data/lib/polyrun/quality/sandbox.rb +70 -0
- data/lib/polyrun/rspec_support.rb +1 -1
- data/lib/polyrun/spec_quality/config.rb +8 -3
- data/lib/polyrun/spec_quality.rb +3 -4
- data/lib/polyrun/version.rb +1 -1
- data/lib/polyrun/worker_output.rb +25 -33
- data/lib/polyrun/worker_output_forwarders.rb +11 -7
- data/lib/polyrun.rb +1 -2
- data/polyrun.gemspec +6 -1
- data/sig/polyrun/config.rbs +2 -2
- metadata +44 -2
|
@@ -3,6 +3,7 @@ require "fileutils"
|
|
|
3
3
|
require "json"
|
|
4
4
|
|
|
5
5
|
require_relative "../version"
|
|
6
|
+
require_relative "../error"
|
|
6
7
|
require_relative "cobertura_zero_lines"
|
|
7
8
|
require_relative "filter"
|
|
8
9
|
require_relative "formatter"
|
|
@@ -44,6 +45,11 @@ module Polyrun
|
|
|
44
45
|
def start!(root:, reject_patterns: [], track_under: ["lib"], track_files: nil, groups: nil, output_path: nil, minimum_line_percent: nil, strict: nil, meta: {}, formatter: nil, report_output_dir: nil, report_basename: "polyrun-coverage")
|
|
45
46
|
return if disabled?
|
|
46
47
|
|
|
48
|
+
branches = branch_coverage_enabled?
|
|
49
|
+
if branches && !::Coverage.supported?(:branches)
|
|
50
|
+
raise Polyrun::Error, "#{RUBY_ENGINE} does not support branch coverage"
|
|
51
|
+
end
|
|
52
|
+
|
|
47
53
|
root = File.expand_path(root)
|
|
48
54
|
basename = fragment_default_basename_from_env
|
|
49
55
|
output_path ||= File.join(root, "coverage", "polyrun-fragment-#{basename}.json")
|
|
@@ -73,7 +79,7 @@ module Polyrun
|
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
unless ::Coverage.running?
|
|
76
|
-
::Coverage.start(lines: true, branches:
|
|
82
|
+
::Coverage.start(lines: true, branches: branches)
|
|
77
83
|
end
|
|
78
84
|
unless instance_variable_defined?(:@collector_finish_at_exit_registered)
|
|
79
85
|
@collector_finish_at_exit_registered = true
|
|
@@ -26,11 +26,10 @@ module Polyrun
|
|
|
26
26
|
filter = track_filter_for(root: root, track_under: track_under, ignore_paths: ignore_paths)
|
|
27
27
|
out = {}
|
|
28
28
|
raw.each do |path, coverage_entry|
|
|
29
|
-
next unless coverage_entry.is_a?(Hash)
|
|
30
29
|
next if filter && !filter.include_path?(path.to_s)
|
|
31
30
|
|
|
32
|
-
lines = coverage_entry
|
|
33
|
-
next
|
|
31
|
+
lines = line_array(coverage_entry)
|
|
32
|
+
next if lines.empty?
|
|
34
33
|
|
|
35
34
|
out[path.to_s] = Snapshot.sparse_snapshot_lines(lines)
|
|
36
35
|
end
|
|
@@ -84,6 +83,7 @@ module Polyrun
|
|
|
84
83
|
end
|
|
85
84
|
|
|
86
85
|
def line_array(entry)
|
|
86
|
+
return entry if entry.is_a?(Array)
|
|
87
87
|
return [] unless entry.is_a?(Hash)
|
|
88
88
|
return [] if entry["sparse"]
|
|
89
89
|
|
|
@@ -158,11 +158,7 @@ module Polyrun
|
|
|
158
158
|
end
|
|
159
159
|
|
|
160
160
|
def lines_from_peek_entry(path, after_raw)
|
|
161
|
-
|
|
162
|
-
return [] unless entry.is_a?(Hash)
|
|
163
|
-
|
|
164
|
-
arr = entry[:lines] || entry["lines"]
|
|
165
|
-
arr.is_a?(Array) ? arr : []
|
|
161
|
+
line_array(after_raw[path])
|
|
166
162
|
end
|
|
167
163
|
|
|
168
164
|
def track_filter_for(root:, track_under:, ignore_paths:)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require "fileutils"
|
|
2
|
+
require_relative "plan"
|
|
3
|
+
require_relative "../quality/command"
|
|
4
|
+
require "rbconfig"
|
|
5
|
+
require_relative "../quality/sandbox"
|
|
6
|
+
|
|
7
|
+
module Polyrun
|
|
8
|
+
module Graph
|
|
9
|
+
class Client
|
|
10
|
+
def initialize(root: Dir.pwd, timeout: nil, settings: {})
|
|
11
|
+
@settings = Config::Tools.runtime("graph", settings.merge(timeout ? {"timeout" => timeout} : {}))
|
|
12
|
+
@root = File.realpath(root)
|
|
13
|
+
library = File.expand_path("../..", __dir__)
|
|
14
|
+
@command = [RbConfig.ruby, "-I", library, "-rpolyrun/graph/worker", "-e", "exit Polyrun::Graph::Worker.run(ARGV)", "--"]
|
|
15
|
+
@timeout = @settings["timeout"]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def call(arguments, prove: [])
|
|
19
|
+
state = Workspace.new(@root, settings: @settings).path(@settings["state_dir"])
|
|
20
|
+
|
|
21
|
+
FileUtils.mkdir_p(state)
|
|
22
|
+
lock_path = File.join(state, "polyrun.lock")
|
|
23
|
+
raise Error.new("INVALID_PLAN", "symbolic link in lock path") if File.symlink?(lock_path)
|
|
24
|
+
|
|
25
|
+
File.open(lock_path, File::RDWR | File::CREAT, 0o600) do |lock|
|
|
26
|
+
raise Error.new("WORKSPACE_BUSY", "another graph command is running") unless lock.flock(File::LOCK_EX | File::LOCK_NB)
|
|
27
|
+
|
|
28
|
+
dispatch(arguments, prove)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def dispatch(arguments, prove)
|
|
35
|
+
verb, id = arguments.reject { |argument| argument == "--json" }
|
|
36
|
+
return check(Plan.new(@root, id, settings: @settings), arguments, prove) if verb == "check"
|
|
37
|
+
|
|
38
|
+
invoke(arguments)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def invoke(arguments)
|
|
42
|
+
Quality::Command.call(@command + arguments, root: @root, timeout: @timeout, max_output: @settings["max_output_bytes"], env: {"POLYRUN_GRAPH_ROOT" => @root, "POLYRUN_GRAPH_SETTINGS" => JSON.generate(@settings), "RUBYOPT" => nil})
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def check(plan, arguments, prove)
|
|
46
|
+
checked = false
|
|
47
|
+
plan.invalidate!
|
|
48
|
+
result = invoke(arguments)
|
|
49
|
+
return result unless result["exitstatus"].zero?
|
|
50
|
+
|
|
51
|
+
plan.record_check!
|
|
52
|
+
prove_result = prove.empty? ? nil : Quality::Command.call(prove, root: plan.tree, timeout: @timeout, max_output: @settings["max_output_bytes"], env: Quality::Sandbox.environment(plan.tree))
|
|
53
|
+
if prove_result && !prove_result["exitstatus"].zero?
|
|
54
|
+
raise Error.new("PROVE_FAILED", "prove command failed: #{prove_result["stderr"]}", exitstatus: prove_result["exitstatus"])
|
|
55
|
+
end
|
|
56
|
+
plan.verify!
|
|
57
|
+
checked = true
|
|
58
|
+
result
|
|
59
|
+
ensure
|
|
60
|
+
plan.invalidate! unless checked
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require_relative "workspace"
|
|
2
|
+
require_relative "syntax"
|
|
3
|
+
|
|
4
|
+
module Polyrun
|
|
5
|
+
module Graph
|
|
6
|
+
class Index
|
|
7
|
+
def initialize(root, settings: {})
|
|
8
|
+
@workspace = Workspace.new(root, settings: settings)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
hashes = @workspace.hashes
|
|
13
|
+
snapshot = @workspace.snapshot(hashes)
|
|
14
|
+
existing = cached_index(snapshot)
|
|
15
|
+
return existing if existing
|
|
16
|
+
|
|
17
|
+
methods = []
|
|
18
|
+
calls = []
|
|
19
|
+
hashes.each_key do |relative|
|
|
20
|
+
syntax = Syntax.new(relative, File.binread(@workspace.path(relative)), max_nodes: @workspace.settings["max_nodes"]).parse
|
|
21
|
+
methods.concat(syntax.methods)
|
|
22
|
+
calls.concat(syntax.calls)
|
|
23
|
+
enforce_node_limit!(methods.size + calls.size)
|
|
24
|
+
end
|
|
25
|
+
raise Error.new("STALE_SNAPSHOT", "source changed while indexing") unless @workspace.hashes == hashes
|
|
26
|
+
|
|
27
|
+
edges = resolve(methods, calls)
|
|
28
|
+
@workspace.write_json("#{@workspace.state}/index.json", {"format" => 1, "snapshot" => snapshot,
|
|
29
|
+
"files" => hashes, "methods" => methods, "calls" => calls, "edges" => edges, "reused" => false})
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def load
|
|
33
|
+
value = @workspace.read_json("#{@workspace.state}/index.json")
|
|
34
|
+
unless value.is_a?(Hash) && value["format"] == 1 && %w[methods calls edges].all? { |key| value[key].is_a?(Array) }
|
|
35
|
+
raise Error.new("INVALID_PLAN", "invalid graph index")
|
|
36
|
+
end
|
|
37
|
+
value
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def cached_index(snapshot)
|
|
43
|
+
return unless File.file?(@workspace.path("#{@workspace.state}/index.json"))
|
|
44
|
+
|
|
45
|
+
existing = load
|
|
46
|
+
return unless existing["snapshot"] == snapshot
|
|
47
|
+
|
|
48
|
+
enforce_node_limit!(existing["methods"].size + existing["calls"].size)
|
|
49
|
+
existing.merge("reused" => true)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def enforce_node_limit!(count)
|
|
53
|
+
raise Error.new("RESOURCE_LIMIT", "too many graph nodes") if count > @workspace.settings["max_nodes"]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def resolve(methods, calls)
|
|
57
|
+
lookup = methods.group_by { |method| [method["owner"], method["scope"], method["name"]] }
|
|
58
|
+
calls.filter_map do |call|
|
|
59
|
+
owner, scope = if call["receiver"].nil? || call["receiver"] == "self"
|
|
60
|
+
[call["owner"], call["scope"]]
|
|
61
|
+
else
|
|
62
|
+
[call["receiver"].delete_prefix("::"), "singleton"]
|
|
63
|
+
end
|
|
64
|
+
candidates = owner && lookup[[owner, scope, call["name"]]]
|
|
65
|
+
target = candidates&.one? ? candidates.first : nil
|
|
66
|
+
call["target"] = target && target["id"]
|
|
67
|
+
call["target_provenance"] = target ? "static_guess" : "unknown"
|
|
68
|
+
next unless target && call["method_id"]
|
|
69
|
+
|
|
70
|
+
{"from_id" => call["method_id"], "to_id" => target["id"], "call_id" => call["id"], "provenance" => "static_guess"}
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require_relative "index"
|
|
2
|
+
|
|
3
|
+
module Polyrun
|
|
4
|
+
module Graph
|
|
5
|
+
class Inspect
|
|
6
|
+
def initialize(index)
|
|
7
|
+
@index = index
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def call(selector, flags)
|
|
11
|
+
return control_plane if flags.include?("--control-plane")
|
|
12
|
+
|
|
13
|
+
methods = select_methods(selector)
|
|
14
|
+
raise Error.new("UNKNOWN_TARGET", "no matching method") if methods.empty?
|
|
15
|
+
raise Error.new("AMBIGUOUS_NODE", "choose a method owner or source path") unless methods.one?
|
|
16
|
+
|
|
17
|
+
nodes = methods.map do |method|
|
|
18
|
+
value = method.dup
|
|
19
|
+
value["calls"] = @index["calls"].select { |node| node["method_id"] == method["id"] } if flags.include?("--calls")
|
|
20
|
+
%w[callers callees].each do |direction|
|
|
21
|
+
next unless flags.include?("--#{direction}")
|
|
22
|
+
|
|
23
|
+
value[direction] = connections(method, direction)
|
|
24
|
+
end
|
|
25
|
+
value
|
|
26
|
+
end
|
|
27
|
+
{"ok" => true, "snapshot" => @index["snapshot"], "nodes" => nodes}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def connections(method, direction)
|
|
33
|
+
lookup = @index["methods"].to_h { |entry| [entry["id"], entry] }
|
|
34
|
+
from, to = (direction == "callers") ? %w[to_id from_id] : %w[from_id to_id]
|
|
35
|
+
@index["edges"].select { |edge| edge[from] == method["id"] }.map do |edge|
|
|
36
|
+
lookup.fetch(edge[to]).merge("provenance" => edge["provenance"])
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def select_methods(selector)
|
|
41
|
+
if selector&.start_with?("path:")
|
|
42
|
+
path, name = selector.delete_prefix("path:").split("#", 2)
|
|
43
|
+
@index["methods"].select { |method| method["path"] == path && (!name || method["name"] == name) }
|
|
44
|
+
else
|
|
45
|
+
@index["methods"].select { |method| [method["id"], method["subject"], method["name"]].include?(selector) }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def control_plane
|
|
50
|
+
lifecycle = @index["methods"].select { |method| method["name"].match?(/\A(?:start|stop|shutdown|run|perform|call|process|poll|tick|schedule)[!?]?\z/) }
|
|
51
|
+
signals = @index["calls"].select { |node| node["name"].match?(/\A(?:notify|broadcast|signal|enqueue|publish|perform_async)[!?]?\z/) }
|
|
52
|
+
signal_ids = signals.to_h { |node| [node["id"], true] }
|
|
53
|
+
{"ok" => true, "snapshot" => @index["snapshot"], "control_plane" => {"lifecycle" => lifecycle,
|
|
54
|
+
"signals" => signals, "signal_edges" => @index["edges"].select { |edge| signal_ids[edge["call_id"]] }}}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "digest"
|
|
3
|
+
require "find"
|
|
4
|
+
require_relative "error"
|
|
5
|
+
require_relative "workspace"
|
|
6
|
+
require "prism"
|
|
7
|
+
require_relative "transaction"
|
|
8
|
+
|
|
9
|
+
module Polyrun
|
|
10
|
+
module Graph
|
|
11
|
+
class Plan
|
|
12
|
+
attr_reader :tree, :directory
|
|
13
|
+
|
|
14
|
+
def initialize(root, id, settings: {})
|
|
15
|
+
raise Error.new("INVALID_PLAN", "invalid plan id") unless id.is_a?(String) && id.match?(/\A[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}\z/)
|
|
16
|
+
|
|
17
|
+
@settings = Config::Tools.runtime("graph", settings)
|
|
18
|
+
@root = File.realpath(root)
|
|
19
|
+
@directory = safe_path(@root, "#{@settings["state_dir"]}/plans/#{id}")
|
|
20
|
+
@tree = safe_path(@directory, "tree")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def metadata
|
|
24
|
+
read_json(safe_path(@directory, "meta.json"))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def verify!
|
|
28
|
+
receipt = read_json(safe_path(@directory, "polyrun-check.json"))
|
|
29
|
+
raise Error.new("CHECK_FAILED", "plan changed since check") unless receipt == fingerprint
|
|
30
|
+
|
|
31
|
+
verify_originals!
|
|
32
|
+
true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def record_check!
|
|
36
|
+
check = read_json(safe_path(@directory, "check.json"))
|
|
37
|
+
raise Error.new("CHECK_FAILED", "graph check did not succeed") unless check["status"] == "ok"
|
|
38
|
+
|
|
39
|
+
verify_originals!
|
|
40
|
+
receipt = JSON.generate(fingerprint)
|
|
41
|
+
raise Error.new("RESOURCE_LIMIT", "plan receipt is too large") if receipt.bytesize > @settings["max_plan_bytes"]
|
|
42
|
+
|
|
43
|
+
File.write(safe_path(@directory, "polyrun-check.json"), receipt)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def invalidate!
|
|
47
|
+
%w[check.json polyrun-check.json].each do |name|
|
|
48
|
+
path = safe_path(@directory, name)
|
|
49
|
+
File.unlink(path) if File.exist?(path)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def verify_originals!
|
|
54
|
+
hashes = metadata.fetch("pre_hashes")
|
|
55
|
+
raise Error.new("INVALID_PLAN", "invalid original file hashes") unless hashes.is_a?(Hash) && !hashes.empty?
|
|
56
|
+
|
|
57
|
+
hashes.each do |relative, expected|
|
|
58
|
+
path = safe_path(@root, relative)
|
|
59
|
+
staged = safe_path(@tree, relative)
|
|
60
|
+
raise Error.new("INVALID_PLAN", "staged file missing: #{relative}") unless File.file?(staged)
|
|
61
|
+
unless File.file?(path) && Digest::SHA256.file(path).hexdigest == expected
|
|
62
|
+
raise Error.new("STALE_SNAPSHOT", "file changed before apply: #{relative}")
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
raise Error.new("STALE_SNAPSHOT", "source file inventory changed") unless Workspace.new(@root, settings: @settings).files == hashes.keys.sort
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def check!
|
|
69
|
+
verify_originals!
|
|
70
|
+
metadata.fetch("pre_hashes").each_key do |relative|
|
|
71
|
+
path = safe_path(@tree, relative)
|
|
72
|
+
raise Error.new("PARSE_FAILED", "invalid staged Ruby: #{relative}") if Prism.parse_file(path).failure?
|
|
73
|
+
expected = metadata.fetch("staged_hashes").fetch(relative)
|
|
74
|
+
raise Error.new("CHECK_FAILED", "staged operation changed: #{relative}") unless Digest::SHA256.file(path).hexdigest == expected
|
|
75
|
+
end
|
|
76
|
+
result = {"ok" => true, "status" => "ok", "provenance" => "syntax"}
|
|
77
|
+
File.write(safe_path(@directory, "check.json"), JSON.generate(result))
|
|
78
|
+
result
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def apply!
|
|
82
|
+
transaction = Transaction.new(@root, @directory, settings: @settings)
|
|
83
|
+
transaction.recover!
|
|
84
|
+
verify!
|
|
85
|
+
replacements = metadata.fetch("pre_hashes").keys.filter_map do |relative|
|
|
86
|
+
original = safe_path(@root, relative)
|
|
87
|
+
staged = safe_path(@tree, relative)
|
|
88
|
+
[relative, File.binread(original), File.binread(staged)] if Digest::SHA256.file(original).hexdigest != Digest::SHA256.file(staged).hexdigest
|
|
89
|
+
end
|
|
90
|
+
transaction.commit(replacements)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
def fingerprint
|
|
96
|
+
files = {}
|
|
97
|
+
Find.find(@tree) do |path|
|
|
98
|
+
raise Error.new("INVALID_PLAN", "symbolic link in plan tree") if File.symlink?(path)
|
|
99
|
+
next if File.directory?(path)
|
|
100
|
+
raise Error.new("INVALID_PLAN", "nonregular file in plan tree") unless File.file?(path)
|
|
101
|
+
raise Error.new("INVALID_PLAN", "too many plan files") if files.length >= @settings["max_receipt_files"]
|
|
102
|
+
|
|
103
|
+
files[path.delete_prefix("#{@tree}/")] = Digest::SHA256.file(path).hexdigest
|
|
104
|
+
end
|
|
105
|
+
{"metadata" => Digest::SHA256.file(safe_path(@directory, "meta.json")).hexdigest, "files" => files}
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def safe_path(base, relative)
|
|
109
|
+
parts = relative.is_a?(String) ? relative.split("/", -1) : []
|
|
110
|
+
if parts.empty? || parts.any? { |part| part.empty? || part == "." || part == ".." } || relative.include?("\0")
|
|
111
|
+
raise Error.new("INVALID_PLAN", "invalid plan path")
|
|
112
|
+
end
|
|
113
|
+
parts.reduce(base) do |parent, part|
|
|
114
|
+
path = File.join(parent, part)
|
|
115
|
+
raise Error.new("INVALID_PLAN", "symbolic link in plan path") if File.symlink?(path)
|
|
116
|
+
|
|
117
|
+
path
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def read_json(path)
|
|
122
|
+
raise Error.new("CHECK_FAILED", "check or plan metadata missing") unless File.file?(path)
|
|
123
|
+
raise Error.new("INVALID_PLAN", "plan metadata too large") if File.size(path) > @settings["max_plan_bytes"]
|
|
124
|
+
|
|
125
|
+
value = JSON.parse(File.binread(path))
|
|
126
|
+
raise Error.new("INVALID_PLAN", "plan metadata must be an object") unless value.is_a?(Hash)
|
|
127
|
+
|
|
128
|
+
value
|
|
129
|
+
rescue JSON::ParserError => e
|
|
130
|
+
raise Error.new("INVALID_PLAN", e.message)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
require "securerandom"
|
|
2
|
+
require "yaml"
|
|
3
|
+
require_relative "index"
|
|
4
|
+
require_relative "plan"
|
|
5
|
+
require_relative "../quality/sandbox"
|
|
6
|
+
|
|
7
|
+
module Polyrun
|
|
8
|
+
module Graph
|
|
9
|
+
class Planner
|
|
10
|
+
def initialize(root, settings: {})
|
|
11
|
+
@workspace = Workspace.new(root, settings: settings)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call(change_path)
|
|
15
|
+
change = load_change(change_path)
|
|
16
|
+
index = fresh_index(change)
|
|
17
|
+
operations = change["operations"]
|
|
18
|
+
raise Error.new("INVALID_PLAN", "operations must be a nonempty array") unless operations.is_a?(Array) && (1..@workspace.settings["max_operations"]).cover?(operations.length)
|
|
19
|
+
|
|
20
|
+
replacements = prepare(operations, index)
|
|
21
|
+
id = "plan-#{SecureRandom.hex(12)}"
|
|
22
|
+
plan = Plan.new(@workspace.root, id, settings: @workspace.settings)
|
|
23
|
+
stage_plan(plan, replacements)
|
|
24
|
+
save_plan(plan, id, index, operations)
|
|
25
|
+
hashes = index.fetch("files")
|
|
26
|
+
{"ok" => true, "plan_id" => id, "diff" => diff(plan, hashes)}
|
|
27
|
+
rescue JSON::ParserError, Psych::Exception, KeyError, TypeError => error
|
|
28
|
+
raise Error.new("INVALID_PLAN", error.message)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def stage_plan(plan, replacements)
|
|
34
|
+
Quality::Sandbox.new(@workspace.root, settings: @workspace.settings["staging"].merge("exclude_paths" => @workspace.settings["staging"]["exclude_paths"] + [@workspace.state])).copy_to(plan.tree)
|
|
35
|
+
write_edits(plan, replacements)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def save_plan(plan, id, index, operations)
|
|
39
|
+
hashes = index.fetch("files")
|
|
40
|
+
staged = hashes.keys.to_h { |relative| [relative, Digest::SHA256.file(@workspace.path(relative, base: plan.tree)).hexdigest] }
|
|
41
|
+
@workspace.write_json("#{@workspace.state}/plans/#{id}/meta.json", {"id" => id, "snapshot" => index["snapshot"],
|
|
42
|
+
"pre_hashes" => hashes, "staged_hashes" => staged, "operations" => operations}, limit: @workspace.settings["max_plan_bytes"])
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def load_change(change_path)
|
|
46
|
+
file = File.expand_path(change_path, @workspace.root)
|
|
47
|
+
raise Error.new("RESOURCE_LIMIT", "change description is too large") if File.size(file) > @workspace.settings["max_input_bytes"]
|
|
48
|
+
|
|
49
|
+
raw = File.read(file)
|
|
50
|
+
raw.lstrip.start_with?("{") ? JSON.parse(raw) : YAML.safe_load(raw)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def fresh_index(change)
|
|
54
|
+
index = Index.new(@workspace.root, settings: @workspace.settings).load
|
|
55
|
+
hashes = @workspace.hashes
|
|
56
|
+
unless change.is_a?(Hash) && change["snapshot"] == index["snapshot"] && hashes == index["files"]
|
|
57
|
+
raise Error.new("STALE_SNAPSHOT", "index the current source before planning changes")
|
|
58
|
+
end
|
|
59
|
+
index
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def write_edits(plan, replacements)
|
|
63
|
+
replacements.group_by { |operation| operation.fetch("path") }.each do |relative, edits|
|
|
64
|
+
path = @workspace.path(relative, base: plan.tree)
|
|
65
|
+
source = File.binread(path)
|
|
66
|
+
edits.sort_by { |edit| -edit.fetch("byte_start") }.each do |edit|
|
|
67
|
+
source[edit["byte_start"]...edit["byte_end"]] = edit["replacement"]
|
|
68
|
+
end
|
|
69
|
+
raise Error.new("PARSE_FAILED", "replacement is not valid Ruby") if Prism.parse(source).failure?
|
|
70
|
+
|
|
71
|
+
File.binwrite(path, source)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def prepare(operations, index)
|
|
76
|
+
nodes = index["calls"].to_h { |node| [node["id"], node] }
|
|
77
|
+
seen = {}
|
|
78
|
+
operations.map do |operation|
|
|
79
|
+
raise Error.new("INVALID_PLAN", "unsupported graph operation") unless operation.is_a?(Hash) && operation["op"] == "replace_call_name"
|
|
80
|
+
|
|
81
|
+
node = nodes[operation["node"]]
|
|
82
|
+
raise Error.new("UNKNOWN_TARGET", "call site missing") unless node
|
|
83
|
+
raise Error.new("INVALID_PLAN", "duplicate call-site operation") if seen[node["id"]]
|
|
84
|
+
|
|
85
|
+
seen[node["id"]] = true
|
|
86
|
+
validate_edit!(operation, node)
|
|
87
|
+
node.merge("replacement" => operation["replacement"].b)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def validate_edit!(operation, node)
|
|
92
|
+
raise Error.new("EXPECT_MISMATCH", "call-site fingerprint changed") unless operation["expect"] == node.slice("name", "fingerprint")
|
|
93
|
+
|
|
94
|
+
replacement = operation["replacement"]
|
|
95
|
+
unless replacement.is_a?(String) && replacement.match?(/\A[[:alpha:]_][[:alnum:]_]*[!?=]?\z/)
|
|
96
|
+
raise Error.new("INVALID_PLAN", "replacement must be a method name")
|
|
97
|
+
end
|
|
98
|
+
original = File.binread(@workspace.path(node["path"])).byteslice(node["byte_start"]...node["byte_end"])
|
|
99
|
+
raise Error.new("EXPECT_MISMATCH", "call-site bytes changed or unsupported target") unless original == node["name"].b
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def diff(plan, hashes)
|
|
103
|
+
text = hashes.keys.filter_map do |relative|
|
|
104
|
+
before = File.binread(@workspace.path(relative))
|
|
105
|
+
after = File.binread(@workspace.path(relative, base: plan.tree))
|
|
106
|
+
next if before == after
|
|
107
|
+
|
|
108
|
+
"--- a/#{relative}\n+++ b/#{relative}\n@@ -1,#{before.lines.size} +1,#{after.lines.size} @@\n" +
|
|
109
|
+
before.lines.map { |line| "-#{line.chomp}\n" }.join + after.lines.map { |line| "+#{line.chomp}\n" }.join
|
|
110
|
+
end.join
|
|
111
|
+
raise Error.new("RESOURCE_LIMIT", "plan diff is too large") if text.bytesize > @workspace.settings["max_diff_bytes"]
|
|
112
|
+
|
|
113
|
+
text
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
require "prism"
|
|
2
|
+
require "digest"
|
|
3
|
+
require_relative "error"
|
|
4
|
+
require_relative "../config/tool_defaults"
|
|
5
|
+
|
|
6
|
+
module Polyrun
|
|
7
|
+
module Graph
|
|
8
|
+
class Syntax < Prism::Visitor
|
|
9
|
+
attr_reader :methods, :calls
|
|
10
|
+
|
|
11
|
+
def initialize(path, source, max_nodes: Config::ToolDefaults::COMMON.fetch("max_nodes"))
|
|
12
|
+
@max_nodes = max_nodes
|
|
13
|
+
@path = path
|
|
14
|
+
@source = source
|
|
15
|
+
@methods = []
|
|
16
|
+
@calls = []
|
|
17
|
+
@owner = "Object"
|
|
18
|
+
@singleton = false
|
|
19
|
+
@method = nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def parse
|
|
23
|
+
parsed = Prism.parse(@source)
|
|
24
|
+
raise Error.new("PARSE_FAILED", "invalid Ruby: #{@path}") if parsed.failure?
|
|
25
|
+
|
|
26
|
+
parsed.value.accept(self)
|
|
27
|
+
self
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def visit_class_node(node)
|
|
31
|
+
scope(node.constant_path) { node.body&.accept(self) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def visit_module_node(node)
|
|
35
|
+
scope(node.constant_path) { node.body&.accept(self) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def visit_singleton_class_node(node)
|
|
39
|
+
previous = [@owner, @singleton]
|
|
40
|
+
@owner = constant_name(node.expression) unless node.expression.is_a?(Prism::SelfNode)
|
|
41
|
+
@singleton = true
|
|
42
|
+
node.body&.accept(self)
|
|
43
|
+
ensure
|
|
44
|
+
@owner, @singleton = previous
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def visit_def_node(node)
|
|
48
|
+
previous = @method
|
|
49
|
+
owner = (node.receiver && !node.receiver.is_a?(Prism::SelfNode)) ? constant_name(node.receiver) : @owner
|
|
50
|
+
singleton = @singleton || !node.receiver.nil?
|
|
51
|
+
@method = entry("method", node.name.to_s, node.location).merge("owner" => owner,
|
|
52
|
+
"scope" => singleton ? "singleton" : "instance", "subject" => owner && "#{owner}#{singleton ? "." : "#"}#{node.name}",
|
|
53
|
+
"body_start" => node.body&.location&.start_offset, "body_end" => node.body&.location&.end_offset)
|
|
54
|
+
@methods << @method
|
|
55
|
+
super
|
|
56
|
+
ensure
|
|
57
|
+
@method = previous
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def visit_call_node(node)
|
|
61
|
+
if node.message_loc
|
|
62
|
+
receiver = if node.receiver.nil?
|
|
63
|
+
nil
|
|
64
|
+
elsif node.receiver.is_a?(Prism::SelfNode)
|
|
65
|
+
"self"
|
|
66
|
+
else
|
|
67
|
+
constant_name(node.receiver) || "dynamic"
|
|
68
|
+
end
|
|
69
|
+
@calls << entry("call", node.name.to_s, node.message_loc).merge("method_id" => @method&.fetch("id"),
|
|
70
|
+
"owner" => @method ? @method["owner"] : @owner, "scope" => @method ? @method["scope"] : "singleton", "receiver" => receiver)
|
|
71
|
+
end
|
|
72
|
+
super
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def scope(node)
|
|
78
|
+
previous = [@owner, @singleton, @method]
|
|
79
|
+
name = constant_name(node)
|
|
80
|
+
@owner = if name.nil? || @owner.nil?
|
|
81
|
+
nil
|
|
82
|
+
elsif name.start_with?("::") || @owner == "Object"
|
|
83
|
+
name.delete_prefix("::")
|
|
84
|
+
elsif name.include?("::")
|
|
85
|
+
nil
|
|
86
|
+
else
|
|
87
|
+
[@owner, name].compact.join("::")
|
|
88
|
+
end
|
|
89
|
+
@singleton = false
|
|
90
|
+
@method = nil
|
|
91
|
+
yield
|
|
92
|
+
ensure
|
|
93
|
+
@owner, @singleton, @method = previous
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def constant_name(node)
|
|
97
|
+
node.full_name if node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
|
|
98
|
+
rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def entry(kind, name, location)
|
|
103
|
+
raise Error.new("RESOURCE_LIMIT", "too many graph nodes") if @methods.size + @calls.size >= @max_nodes
|
|
104
|
+
|
|
105
|
+
bytes = @source.byteslice(location.start_offset, location.length)
|
|
106
|
+
fingerprint = Digest::SHA256.hexdigest(bytes)
|
|
107
|
+
identity = Digest::SHA256.hexdigest([kind, @path, location.start_offset, fingerprint].join("\0"))
|
|
108
|
+
{"id" => "#{kind}-#{identity[0, 24]}", "kind" => kind, "name" => name, "path" => @path,
|
|
109
|
+
"line" => location.start_line, "byte_start" => location.start_offset, "byte_end" => location.end_offset,
|
|
110
|
+
"fingerprint" => fingerprint, "provenance" => "syntax"}
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|