polyrun 3.2.0-java

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 (225) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +234 -0
  3. data/CODE_OF_CONDUCT.md +26 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/LICENSE +21 -0
  6. data/README.md +251 -0
  7. data/SECURITY.md +25 -0
  8. data/bin/polyrun +6 -0
  9. data/docs/QUALITY.md +82 -0
  10. data/docs/QUALITY_CONFIGURATION.md +95 -0
  11. data/docs/SETUP_PROFILE.md +139 -0
  12. data/lib/polyrun/benchmark/pressure/cooker.rb +191 -0
  13. data/lib/polyrun/benchmark/pressure/process_sample.rb +127 -0
  14. data/lib/polyrun/benchmark/pressure.rb +144 -0
  15. data/lib/polyrun/benchmark/profile.rb +191 -0
  16. data/lib/polyrun/benchmark/report.rb +87 -0
  17. data/lib/polyrun/cli/benchmark_commands.rb +66 -0
  18. data/lib/polyrun/cli/ci_shard_hooks.rb +129 -0
  19. data/lib/polyrun/cli/ci_shard_run_command.rb +177 -0
  20. data/lib/polyrun/cli/ci_shard_run_parse.rb +68 -0
  21. data/lib/polyrun/cli/config_command.rb +42 -0
  22. data/lib/polyrun/cli/coverage_commands.rb +150 -0
  23. data/lib/polyrun/cli/coverage_merge_io.rb +150 -0
  24. data/lib/polyrun/cli/database_commands.rb +163 -0
  25. data/lib/polyrun/cli/default_run.rb +144 -0
  26. data/lib/polyrun/cli/doctor_command.rb +94 -0
  27. data/lib/polyrun/cli/env_commands.rb +43 -0
  28. data/lib/polyrun/cli/failure_commands.rb +99 -0
  29. data/lib/polyrun/cli/graph_command.rb +68 -0
  30. data/lib/polyrun/cli/help.rb +83 -0
  31. data/lib/polyrun/cli/helpers.rb +162 -0
  32. data/lib/polyrun/cli/hooks_command.rb +97 -0
  33. data/lib/polyrun/cli/init_command.rb +108 -0
  34. data/lib/polyrun/cli/mutation_command.rb +67 -0
  35. data/lib/polyrun/cli/partition_diagnostics.rb +22 -0
  36. data/lib/polyrun/cli/plan_command.rb +184 -0
  37. data/lib/polyrun/cli/prepare_command.rb +70 -0
  38. data/lib/polyrun/cli/prepare_recipe.rb +82 -0
  39. data/lib/polyrun/cli/quality_commands.rb +65 -0
  40. data/lib/polyrun/cli/queue_command.rb +161 -0
  41. data/lib/polyrun/cli/quick_command.rb +13 -0
  42. data/lib/polyrun/cli/report_commands.rb +108 -0
  43. data/lib/polyrun/cli/run_queue_command.rb +127 -0
  44. data/lib/polyrun/cli/run_shards_command.rb +164 -0
  45. data/lib/polyrun/cli/run_shards_parallel_children.rb +112 -0
  46. data/lib/polyrun/cli/run_shards_parallel_wait.rb +252 -0
  47. data/lib/polyrun/cli/run_shards_plan_boot_phases.rb +207 -0
  48. data/lib/polyrun/cli/run_shards_plan_options.rb +74 -0
  49. data/lib/polyrun/cli/run_shards_planning.rb +123 -0
  50. data/lib/polyrun/cli/run_shards_run.rb +171 -0
  51. data/lib/polyrun/cli/run_shards_worker_interrupt.rb +101 -0
  52. data/lib/polyrun/cli/skip_env.rb +34 -0
  53. data/lib/polyrun/cli/spec_quality_commands.rb +149 -0
  54. data/lib/polyrun/cli/start_bootstrap.rb +126 -0
  55. data/lib/polyrun/cli/timing_command.rb +31 -0
  56. data/lib/polyrun/cli/tool_options.rb +48 -0
  57. data/lib/polyrun/cli/validate_command.rb +26 -0
  58. data/lib/polyrun/cli.rb +272 -0
  59. data/lib/polyrun/config/dotted_path.rb +21 -0
  60. data/lib/polyrun/config/effective.rb +82 -0
  61. data/lib/polyrun/config/resolver.rb +114 -0
  62. data/lib/polyrun/config/schema.rb +39 -0
  63. data/lib/polyrun/config/suite.rb +109 -0
  64. data/lib/polyrun/config/tool_defaults.rb +42 -0
  65. data/lib/polyrun/config/tool_validation.rb +79 -0
  66. data/lib/polyrun/config/tools.rb +120 -0
  67. data/lib/polyrun/config.rb +97 -0
  68. data/lib/polyrun/coverage/cobertura_zero_lines.rb +32 -0
  69. data/lib/polyrun/coverage/collector.rb +169 -0
  70. data/lib/polyrun/coverage/collector_blob.rb +53 -0
  71. data/lib/polyrun/coverage/collector_finish.rb +98 -0
  72. data/lib/polyrun/coverage/collector_fragment_meta.rb +57 -0
  73. data/lib/polyrun/coverage/config_resolve.rb +131 -0
  74. data/lib/polyrun/coverage/example_diff.rb +184 -0
  75. data/lib/polyrun/coverage/example_diff_snapshot.rb +44 -0
  76. data/lib/polyrun/coverage/example_diff_track_filter.rb +51 -0
  77. data/lib/polyrun/coverage/file_stats_report.rb +36 -0
  78. data/lib/polyrun/coverage/filter.rb +22 -0
  79. data/lib/polyrun/coverage/formatter.rb +137 -0
  80. data/lib/polyrun/coverage/merge/formatters.rb +188 -0
  81. data/lib/polyrun/coverage/merge/formatters_html.rb +221 -0
  82. data/lib/polyrun/coverage/merge/html/_file_list.html.erb +21 -0
  83. data/lib/polyrun/coverage/merge/html/_file_section.html.erb +26 -0
  84. data/lib/polyrun/coverage/merge/html/_groups_table.html.erb +18 -0
  85. data/lib/polyrun/coverage/merge/html/_overview.html.erb +47 -0
  86. data/lib/polyrun/coverage/merge/html/report.css +147 -0
  87. data/lib/polyrun/coverage/merge/html/report.js +48 -0
  88. data/lib/polyrun/coverage/merge/html/template.html.erb +30 -0
  89. data/lib/polyrun/coverage/merge.rb +160 -0
  90. data/lib/polyrun/coverage/merge_fragment_meta.rb +58 -0
  91. data/lib/polyrun/coverage/merge_merge_two.rb +168 -0
  92. data/lib/polyrun/coverage/merge_native.rb +39 -0
  93. data/lib/polyrun/coverage/rails.rb +126 -0
  94. data/lib/polyrun/coverage/reporting.rb +41 -0
  95. data/lib/polyrun/coverage/result.rb +18 -0
  96. data/lib/polyrun/coverage/track_files.rb +150 -0
  97. data/lib/polyrun/data/cached_fixtures.rb +122 -0
  98. data/lib/polyrun/data/factory_counts.rb +48 -0
  99. data/lib/polyrun/data/factory_instrumentation.rb +50 -0
  100. data/lib/polyrun/data/fixtures.rb +68 -0
  101. data/lib/polyrun/data/parallel_provisioning.rb +93 -0
  102. data/lib/polyrun/data/snapshot.rb +84 -0
  103. data/lib/polyrun/database/clone_shards.rb +106 -0
  104. data/lib/polyrun/database/provision.rb +83 -0
  105. data/lib/polyrun/database/shard.rb +64 -0
  106. data/lib/polyrun/database/url_builder/connection/infer.rb +53 -0
  107. data/lib/polyrun/database/url_builder/connection/url_builders.rb +43 -0
  108. data/lib/polyrun/database/url_builder/connection.rb +191 -0
  109. data/lib/polyrun/database/url_builder/template_prepare.rb +21 -0
  110. data/lib/polyrun/database/url_builder.rb +188 -0
  111. data/lib/polyrun/debug.rb +81 -0
  112. data/lib/polyrun/env/ci.rb +65 -0
  113. data/lib/polyrun/error.rb +3 -0
  114. data/lib/polyrun/export/csv.rb +25 -0
  115. data/lib/polyrun/export/markdown.rb +47 -0
  116. data/lib/polyrun/graph/client.rb +64 -0
  117. data/lib/polyrun/graph/error.rb +13 -0
  118. data/lib/polyrun/graph/index.rb +75 -0
  119. data/lib/polyrun/graph/inspect.rb +58 -0
  120. data/lib/polyrun/graph/plan.rb +134 -0
  121. data/lib/polyrun/graph/planner.rb +117 -0
  122. data/lib/polyrun/graph/syntax.rb +114 -0
  123. data/lib/polyrun/graph/transaction.rb +81 -0
  124. data/lib/polyrun/graph/worker.rb +55 -0
  125. data/lib/polyrun/graph/workspace.rb +117 -0
  126. data/lib/polyrun/hooks/dsl.rb +128 -0
  127. data/lib/polyrun/hooks/shell_runner.rb +33 -0
  128. data/lib/polyrun/hooks/worker_runner.rb +27 -0
  129. data/lib/polyrun/hooks/worker_shell.rb +50 -0
  130. data/lib/polyrun/hooks.rb +190 -0
  131. data/lib/polyrun/log.rb +86 -0
  132. data/lib/polyrun/minitest_support.rb +60 -0
  133. data/lib/polyrun/mutation/candidates.rb +91 -0
  134. data/lib/polyrun/mutation/operators.rb +51 -0
  135. data/lib/polyrun/mutation/runner.rb +133 -0
  136. data/lib/polyrun/mutation/subjects.rb +88 -0
  137. data/lib/polyrun/partition/constraints.rb +80 -0
  138. data/lib/polyrun/partition/hrw.rb +70 -0
  139. data/lib/polyrun/partition/min_heap.rb +64 -0
  140. data/lib/polyrun/partition/paths.rb +175 -0
  141. data/lib/polyrun/partition/paths_build.rb +141 -0
  142. data/lib/polyrun/partition/plan.rb +250 -0
  143. data/lib/polyrun/partition/plan_lpt.rb +91 -0
  144. data/lib/polyrun/partition/plan_sharding.rb +56 -0
  145. data/lib/polyrun/partition/reports.rb +139 -0
  146. data/lib/polyrun/partition/stable_shuffle.rb +18 -0
  147. data/lib/polyrun/partition/suite.rb +120 -0
  148. data/lib/polyrun/partition/timing_diagnostics.rb +138 -0
  149. data/lib/polyrun/partition/timing_keys.rb +86 -0
  150. data/lib/polyrun/prepare/artifacts.rb +40 -0
  151. data/lib/polyrun/prepare/assets.rb +64 -0
  152. data/lib/polyrun/process_stdio.rb +96 -0
  153. data/lib/polyrun/quality/command.rb +84 -0
  154. data/lib/polyrun/quality/measure.rb +38 -0
  155. data/lib/polyrun/quality/observe.rb +53 -0
  156. data/lib/polyrun/quality/observe_hook.rb +103 -0
  157. data/lib/polyrun/quality/sandbox.rb +70 -0
  158. data/lib/polyrun/queue/duration.rb +30 -0
  159. data/lib/polyrun/queue/file_store.rb +413 -0
  160. data/lib/polyrun/queue/file_store_intent.rb +188 -0
  161. data/lib/polyrun/queue/file_store_pending.rb +48 -0
  162. data/lib/polyrun/queue/worker_loop.rb +73 -0
  163. data/lib/polyrun/quick/assertions.rb +32 -0
  164. data/lib/polyrun/quick/config.rb +56 -0
  165. data/lib/polyrun/quick/errors.rb +6 -0
  166. data/lib/polyrun/quick/example_group.rb +111 -0
  167. data/lib/polyrun/quick/example_runner.rb +129 -0
  168. data/lib/polyrun/quick/matchers.rb +159 -0
  169. data/lib/polyrun/quick/matchers_predicates.rb +122 -0
  170. data/lib/polyrun/quick/reporter.rb +76 -0
  171. data/lib/polyrun/quick/runner.rb +224 -0
  172. data/lib/polyrun/quick.rb +1 -0
  173. data/lib/polyrun/railtie.rb +7 -0
  174. data/lib/polyrun/reporting/failure_merge.rb +171 -0
  175. data/lib/polyrun/reporting/junit.rb +126 -0
  176. data/lib/polyrun/reporting/junit_emit.rb +58 -0
  177. data/lib/polyrun/reporting/junit_report.rb +87 -0
  178. data/lib/polyrun/reporting/rspec_failure_fragment_formatter.rb +95 -0
  179. data/lib/polyrun/reporting/rspec_junit.rb +39 -0
  180. data/lib/polyrun/rspec_support/active_record_timeout_recovery.rb +51 -0
  181. data/lib/polyrun/rspec_support/example_debug.rb +167 -0
  182. data/lib/polyrun/rspec_support/example_debug_instrumentation.rb +73 -0
  183. data/lib/polyrun/rspec_support/sharded_formatter_compat.rb +57 -0
  184. data/lib/polyrun/rspec_support.rb +110 -0
  185. data/lib/polyrun/spec_quality/config.rb +139 -0
  186. data/lib/polyrun/spec_quality/fragment.rb +39 -0
  187. data/lib/polyrun/spec_quality/merge.rb +78 -0
  188. data/lib/polyrun/spec_quality/minitest_hook.rb +42 -0
  189. data/lib/polyrun/spec_quality/plan_loader.rb +47 -0
  190. data/lib/polyrun/spec_quality/profile.rb +102 -0
  191. data/lib/polyrun/spec_quality/report.rb +363 -0
  192. data/lib/polyrun/spec_quality/rspec_hook.rb +55 -0
  193. data/lib/polyrun/spec_quality/sql_counter.rb +34 -0
  194. data/lib/polyrun/spec_quality.rb +212 -0
  195. data/lib/polyrun/sus_support.rb +55 -0
  196. data/lib/polyrun/templates/POLYRUN.md +51 -0
  197. data/lib/polyrun/templates/ci_matrix.polyrun.yml +22 -0
  198. data/lib/polyrun/templates/minimal_gem.polyrun.yml +13 -0
  199. data/lib/polyrun/templates/polyrun_hooks_spec_quality.rb +12 -0
  200. data/lib/polyrun/templates/polyrun_spec_quality.yml +20 -0
  201. data/lib/polyrun/templates/rails_prepare.polyrun.yml +51 -0
  202. data/lib/polyrun/templates/rails_prepare_task.rake +16 -0
  203. data/lib/polyrun/timing/merge.rb +36 -0
  204. data/lib/polyrun/timing/rspec_example_formatter.rb +60 -0
  205. data/lib/polyrun/timing/stats.rb +76 -0
  206. data/lib/polyrun/timing/summary.rb +78 -0
  207. data/lib/polyrun/timing/variance_report.rb +51 -0
  208. data/lib/polyrun/version.rb +3 -0
  209. data/lib/polyrun/worker_output.rb +181 -0
  210. data/lib/polyrun/worker_output_forwarders.rb +94 -0
  211. data/lib/polyrun/worker_ping.rb +77 -0
  212. data/lib/polyrun.rb +59 -0
  213. data/polyrun.gemspec +49 -0
  214. data/sig/polyrun/cli.rbs +6 -0
  215. data/sig/polyrun/config.rbs +20 -0
  216. data/sig/polyrun/debug.rbs +12 -0
  217. data/sig/polyrun/log.rbs +12 -0
  218. data/sig/polyrun/minitest_support.rbs +7 -0
  219. data/sig/polyrun/quick.rbs +19 -0
  220. data/sig/polyrun/rspec_support.rbs +21 -0
  221. data/sig/polyrun/sus_support.rbs +7 -0
  222. data/sig/polyrun/worker_output.rbs +11 -0
  223. data/sig/polyrun/worker_ping.rbs +10 -0
  224. data/sig/polyrun.rbs +11 -0
  225. metadata +463 -0
@@ -0,0 +1,184 @@
1
+ # rubocop:disable Polyrun/FileLength -- plan argv + partition emit
2
+ require "json"
3
+ require "optparse"
4
+
5
+ module Polyrun
6
+ class CLI
7
+ module PlanCommand
8
+ private
9
+
10
+ def cmd_plan(argv, config_path)
11
+ manifest, code = plan_command_compute_manifest(argv, config_path)
12
+ return code if code != 0
13
+
14
+ Polyrun::Log.puts JSON.generate(manifest)
15
+ 0
16
+ end
17
+
18
+ # @return [Array(Hash, Integer)] manifest hash and exit code (+0+ on success, non-zero on failure)
19
+ def plan_command_compute_manifest(argv, config_path)
20
+ cfg = Polyrun::Config.load(path: config_path || ENV["POLYRUN_CONFIG"])
21
+ pc = cfg.partition
22
+ ctx = plan_command_initial_context(pc)
23
+ plan_command_parse_argv!(argv, ctx)
24
+
25
+ paths_file = ctx[:paths_file] || (pc["paths_file"] || pc[:paths_file])
26
+ code = Polyrun::Partition::PathsBuild.apply!(partition: pc, cwd: Dir.pwd)
27
+ return [nil, code] if code != 0
28
+
29
+ plan_command_manifest_from_paths(cfg, pc, argv, ctx, paths_file)
30
+ end
31
+
32
+ def plan_command_manifest_from_paths(cfg, pc, argv, ctx, paths_file)
33
+ timing_path = plan_resolve_timing_path(pc, ctx[:timing_path], ctx[:strategy])
34
+ ctx[:timing_granularity] = resolve_partition_timing_granularity(pc, ctx[:timing_granularity])
35
+ Polyrun::Log.warn "polyrun plan: using #{cfg.path}" if @verbose && cfg.path
36
+
37
+ bundle = plan_command_items_costs_strategy(paths_file, argv, timing_path, ctx)
38
+ return [nil, 2] if bundle.nil?
39
+
40
+ items, costs, strategy = bundle
41
+ constraints = load_partition_constraints(pc, ctx[:constraints_path])
42
+ stable = load_stable_assignment(pc)
43
+
44
+ manifest, plan = plan_command_build_manifest(
45
+ items: items,
46
+ total: ctx[:total],
47
+ strategy: strategy,
48
+ seed: ctx[:seed],
49
+ costs: costs,
50
+ constraints: constraints,
51
+ shard: ctx[:shard],
52
+ timing_granularity: ctx[:timing_granularity],
53
+ stable_assignment: stable,
54
+ shard_weights: pc["shard_weights"] || pc[:shard_weights]
55
+ )
56
+ partition_emit_diagnostics!(
57
+ plan: plan,
58
+ items: items,
59
+ costs: costs,
60
+ timing_path: timing_path,
61
+ granularity: ctx[:timing_granularity]
62
+ )
63
+ [manifest, 0]
64
+ end
65
+
66
+ def plan_command_items_costs_strategy(paths_file, argv, timing_path, ctx)
67
+ items = plan_plan_items(paths_file, argv)
68
+ return nil if items.nil?
69
+
70
+ loaded = plan_load_costs_and_strategy(
71
+ timing_path,
72
+ ctx[:strategy],
73
+ ctx[:timing_granularity],
74
+ strategy_explicit: ctx[:strategy_explicit]
75
+ )
76
+ return nil if loaded.nil?
77
+
78
+ costs, strategy = loaded
79
+ [items, costs, strategy]
80
+ end
81
+
82
+ def plan_command_initial_context(pc)
83
+ {
84
+ shard: resolve_shard_index(pc),
85
+ total: resolve_shard_total(pc),
86
+ strategy: (pc["strategy"] || pc[:strategy] || "round_robin").to_s,
87
+ strategy_explicit: !!(pc["strategy"] || pc[:strategy]),
88
+ seed: pc["seed"] || pc[:seed],
89
+ paths_file: nil,
90
+ timing_path: nil,
91
+ constraints_path: nil,
92
+ timing_granularity: nil
93
+ }
94
+ end
95
+
96
+ # Partition flags shared by +polyrun plan+ and +queue init+ (excluding +--paths-file+, which each command registers once).
97
+ def plan_command_register_partition_options!(opts, ctx)
98
+ opts.on("--shard INDEX", Integer) { |v| ctx[:shard] = v }
99
+ opts.on("--total N", Integer) { |v| ctx[:total] = v }
100
+ opts.on("--strategy NAME", String) do |v|
101
+ ctx[:strategy] = v
102
+ ctx[:strategy_explicit] = true
103
+ end
104
+ opts.on("--seed VAL") { |v| ctx[:seed] = v }
105
+ opts.on("--constraints PATH", "YAML: pin / serial_glob (see spec_queue.md)") { |v| ctx[:constraints_path] = v }
106
+ opts.on("--timing PATH", "path => seconds JSON; implies cost_binpack unless strategy is explicit or timing-aware") do |v|
107
+ ctx[:timing_path] = v
108
+ end
109
+ opts.on("--timing-granularity VAL", "file (default) or example (experimental: path:line items)") do |v|
110
+ ctx[:timing_granularity] = v
111
+ end
112
+ end
113
+
114
+ # Shared by +polyrun plan+ and +queue init+ so partition flags match +Partition::Plan+ / +plan+ JSON.
115
+ def plan_command_register_options!(opts, ctx)
116
+ opts.on("--paths-file PATH", String) { |v| ctx[:paths_file] = v }
117
+ plan_command_register_partition_options!(opts, ctx)
118
+ end
119
+
120
+ def plan_command_parse_argv!(argv, ctx)
121
+ OptionParser.new do |opts|
122
+ opts.banner = "usage: polyrun plan [options] [--] [paths...]"
123
+ plan_command_register_options!(opts, ctx)
124
+ end.parse!(argv)
125
+ end
126
+
127
+ def plan_command_build_manifest(items:, total:, strategy:, seed:, costs:, constraints:, shard:, timing_granularity: :file, stable_assignment: nil, shard_weights: nil)
128
+ plan = Polyrun::Debug.time("Partition::Plan.new (plan command)") do
129
+ Polyrun::Partition::Plan.new(
130
+ items: items,
131
+ total_shards: total,
132
+ strategy: strategy,
133
+ seed: seed,
134
+ costs: costs,
135
+ constraints: constraints,
136
+ root: Dir.pwd,
137
+ timing_granularity: timing_granularity,
138
+ stable_assignment: stable_assignment,
139
+ shard_weights: shard_weights
140
+ )
141
+ end
142
+ Polyrun::Debug.log_kv(
143
+ plan: "emit manifest JSON",
144
+ shard: shard,
145
+ total: total,
146
+ strategy: strategy,
147
+ path_count: items.size
148
+ )
149
+ [plan.manifest(shard), plan]
150
+ end
151
+
152
+ def plan_resolve_timing_path(pc, timing_path, _strategy = nil)
153
+ return timing_path if timing_path
154
+
155
+ pc["timing_file"] || pc[:timing_file]
156
+ end
157
+
158
+ def plan_plan_items(paths_file, argv)
159
+ if paths_file
160
+ Polyrun::Partition::Paths.read_lines(paths_file)
161
+ elsif argv.empty?
162
+ Polyrun::Log.warn "polyrun plan: provide spec paths, --paths-file, or partition.paths_file in polyrun.yml"
163
+ nil
164
+ else
165
+ argv
166
+ end
167
+ end
168
+
169
+ def plan_load_costs_and_strategy(timing_path, strategy, timing_granularity, strategy_explicit: false)
170
+ costs, resolved, err = partition_resolve_costs_and_strategy(
171
+ timing_path,
172
+ strategy,
173
+ timing_granularity,
174
+ strategy_explicit: strategy_explicit,
175
+ log_prefix: "polyrun plan"
176
+ )
177
+ return nil if err
178
+
179
+ [costs, resolved]
180
+ end
181
+ end
182
+ end
183
+ end
184
+ # rubocop:enable Polyrun/FileLength
@@ -0,0 +1,70 @@
1
+ require "json"
2
+ require "optparse"
3
+
4
+ require_relative "prepare_recipe"
5
+
6
+ module Polyrun
7
+ class CLI
8
+ module PrepareCommand
9
+ include PrepareRecipe
10
+
11
+ private
12
+
13
+ def cmd_prepare(argv, config_path)
14
+ dry = false
15
+ OptionParser.new do |opts|
16
+ opts.on("--dry-run", "Print steps only") { dry = true }
17
+ end.parse!(argv)
18
+
19
+ cfg = Polyrun::Config.load(path: config_path || ENV["POLYRUN_CONFIG"])
20
+ prep = cfg.prepare
21
+ recipe = prep["recipe"] || prep[:recipe] || "default"
22
+ prep_env = Polyrun::Config::Resolver.prepare_env_yaml_string_map(prep)
23
+ child_env = prep_env.empty? ? nil : Polyrun::Config::Resolver.merged_prepare_env(prep)
24
+ manifest = prepare_build_manifest(recipe, dry, prep_env)
25
+
26
+ exit_code = prepare_dispatch_recipe(manifest, prep, recipe, dry, child_env)
27
+ return exit_code unless exit_code.nil?
28
+
29
+ prepare_write_artifact_manifest!(manifest, recipe, dry)
30
+ end
31
+
32
+ def prepare_write_artifact_manifest!(manifest, recipe, dry)
33
+ entries = (manifest["artifacts"] || []).map do |p|
34
+ {"path" => p, "kind" => (File.directory?(p) ? "directory" : "file")}
35
+ end
36
+ manifest["artifact_manifest_path"] = Polyrun::Prepare::Artifacts.write!(root: Dir.pwd, recipe: recipe, entries: entries, dry_run: dry)
37
+ Polyrun::Log.puts JSON.generate(manifest)
38
+ 0
39
+ end
40
+
41
+ def prepare_build_manifest(recipe, dry, prep_env)
42
+ manifest = {
43
+ "recipe" => recipe,
44
+ "dry_run" => dry,
45
+ "artifacts" => [],
46
+ "executed" => !dry
47
+ }
48
+ manifest["env"] = prep_env unless prep_env.empty?
49
+ manifest
50
+ end
51
+
52
+ def prepare_dispatch_recipe(manifest, prep, recipe, dry, child_env)
53
+ case recipe
54
+ when "default", nil, ""
55
+ prepare_recipe_default(manifest, recipe)
56
+ nil
57
+ when "assets"
58
+ m, err = prepare_recipe_assets(manifest, prep, dry, child_env)
59
+ err
60
+ when "shell"
61
+ m, err = prepare_recipe_shell(manifest, prep, dry, child_env)
62
+ err
63
+ else
64
+ Polyrun::Log.warn "unknown prepare recipe: #{recipe}"
65
+ 1
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,82 @@
1
+ require_relative "../process_stdio"
2
+
3
+ module Polyrun
4
+ class CLI
5
+ module PrepareRecipe
6
+ private
7
+
8
+ def prepare_recipe_default(manifest, recipe)
9
+ Polyrun::Log.warn "polyrun prepare: default recipe (no side effects)" if @verbose
10
+ [manifest, nil]
11
+ end
12
+
13
+ def prepare_recipe_assets(manifest, prep, dry, child_env)
14
+ rails_root = File.expand_path(prep["rails_root"] || prep[:rails_root] || ".", Dir.pwd)
15
+ manifest["rails_root"] = rails_root
16
+ custom = prep["command"] || prep[:command]
17
+ if dry
18
+ manifest["actions"] = [
19
+ custom ? custom.to_s.strip : "bin/rails assets:precompile"
20
+ ]
21
+ manifest["executed"] = false
22
+ return [manifest, nil]
23
+ end
24
+ if custom && !custom.to_s.strip.empty?
25
+ st = prepare_run_shell_inherit_stdio(child_env, custom.to_s, rails_root, silent: !@verbose)
26
+ unless st.success?
27
+ Polyrun::Log.warn "polyrun prepare: assets custom command failed (exit #{st.exitstatus})"
28
+ return [manifest, 1]
29
+ end
30
+ else
31
+ Polyrun::Prepare::Assets.precompile!(rails_root: rails_root, silent: !@verbose)
32
+ end
33
+ manifest["artifacts"] = [File.join(rails_root, "public", "assets").to_s]
34
+ [manifest, nil]
35
+ end
36
+
37
+ def prepare_shell_command_lines(prep)
38
+ command = prep["command"] || prep[:command]
39
+ commands = prep["commands"] || prep[:commands]
40
+ lines = []
41
+ lines.concat(Array(commands).map { |c| c.to_s.strip }.reject(&:empty?)) if commands
42
+ lines << command.to_s.strip if command && !command.to_s.strip.empty?
43
+ lines
44
+ end
45
+
46
+ def prepare_recipe_shell(manifest, prep, dry, child_env)
47
+ rails_root = File.expand_path(prep["rails_root"] || prep[:rails_root] || ".", Dir.pwd)
48
+ manifest["rails_root"] = rails_root
49
+ lines = prepare_shell_command_lines(prep)
50
+
51
+ if lines.empty?
52
+ Polyrun::Log.warn "polyrun prepare: shell recipe requires prepare.command and/or prepare.commands"
53
+ return [manifest, 1]
54
+ end
55
+ manifest["actions"] = lines
56
+ if dry
57
+ manifest["executed"] = false
58
+ return [manifest, nil]
59
+ end
60
+ lines.each_with_index do |line, i|
61
+ st = prepare_run_shell_inherit_stdio(child_env, line, rails_root, silent: !@verbose)
62
+ unless st.success?
63
+ Polyrun::Log.warn "polyrun prepare: shell step #{i + 1} failed (exit #{st.exitstatus})"
64
+ return [manifest, 1]
65
+ end
66
+ end
67
+ [manifest, nil]
68
+ end
69
+
70
+ def prepare_run_shell_inherit_stdio(child_env, script, rails_root, silent: false)
71
+ Polyrun::ProcessStdio.inherit_stdio_spawn_wait(
72
+ child_env,
73
+ "sh",
74
+ "-c",
75
+ script.to_s,
76
+ chdir: rails_root,
77
+ silent: silent
78
+ )
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,65 @@
1
+ require "json"
2
+ require_relative "../graph/error"
3
+
4
+ module Polyrun
5
+ class CLI
6
+ module QualityCommands
7
+ private
8
+
9
+ def cmd_measure(argv)
10
+ options = {}
11
+ command = extract_tool_command!(argv)
12
+ parser = OptionParser.new do |opts|
13
+ common_tool_options(opts, options)
14
+ opts.on("--label NAME") { |value| options[:label] = value }
15
+ opts.on("--ceilings PATH") { |value| options[:ceilings] = JSON.parse(File.read(value)) }
16
+ opts.on("--output PATH") { |value| options[:output] = value }
17
+ end
18
+ raise ArgumentError, "unexpected measure arguments" unless parser.parse(argv).empty?
19
+
20
+ settings = configured_tool("measure", options)
21
+ require_relative "../quality/measure"
22
+ result = Quality::Measure.call(tool_command(settings, command), **tool_execution_options(settings, "label", "ceilings"))
23
+ write_quality_result(result, settings["output"])
24
+ quality_result_status(result)
25
+ rescue OptionParser::ParseError, ArgumentError, SystemCallError, JSON::ParserError, Graph::Error, Psych::Exception => e
26
+ Polyrun::Log.warn "measure: #{e.message}"
27
+ 2
28
+ end
29
+
30
+ def cmd_observe(argv)
31
+ options = {}
32
+ command = extract_tool_command!(argv)
33
+ parser = OptionParser.new do |opts|
34
+ common_tool_options(opts, options)
35
+ opts.on("--output PATH") { |value| options[:output] = value }
36
+ opts.on("--max-entries N", Integer) { |value| options[:max_entries] = value }
37
+ end
38
+ raise ArgumentError, "unexpected observe arguments" unless parser.parse(argv).empty?
39
+
40
+ settings = configured_tool("observe", options)
41
+ require_relative "../quality/observe"
42
+ raise ArgumentError, "--output is required" unless settings["output"]
43
+ result = Quality::Observe.call(tool_command(settings, command), root: settings["root"], output: settings["output"],
44
+ timeout: settings["timeout"], limit: settings["max_entries"], max_output: settings["max_output_bytes"], max_artifact_bytes: settings["max_artifact_bytes"])
45
+ write_quality_result(result, nil)
46
+ quality_result_status(result)
47
+ rescue OptionParser::ParseError, ArgumentError, SystemCallError, JSON::ParserError, Graph::Error, Psych::Exception => e
48
+ Polyrun::Log.warn "observe: #{e.message}"
49
+ 2
50
+ end
51
+
52
+ def write_quality_result(result, output)
53
+ payload = JSON.generate(result)
54
+ File.write(output, payload + "\n") if output
55
+ Polyrun::Log.puts payload
56
+ end
57
+
58
+ def quality_result_status(result)
59
+ return result["exitstatus"] unless result["exitstatus"].zero?
60
+
61
+ result["ok"] ? 0 : 1
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,161 @@
1
+ require "json"
2
+ require "optparse"
3
+
4
+ module Polyrun
5
+ class CLI
6
+ module QueueCommand
7
+ private
8
+
9
+ # File-backed queue (spec_queue.md): init → claim batches → ack (ledger append-only).
10
+ #
11
+ # *N×M and load balancing*: +queue init+ uses the same +Partition::Plan+ slice as +polyrun plan+
12
+ # when +--shard+ / +--total+ (or +partition.shard_index+ / +shard_total+ / CI env) define a matrix.
13
+ # Each CI job runs +init+ once for its shard; the queue holds **only** that shard's paths (no duplicate
14
+ # work across matrix jobs). **M** local workers use +queue claim+ on a **shared** queue directory
15
+ # (NFS or the runner disk): claims are mutually exclusive — **dynamic** balance (fast workers pull more
16
+ # batches), **not** the same static per-worker wall time as +cost_binpack+ on the full suite.
17
+ # +--timing+ on init sorts dequeue order (heavy items first when weights are known); it does **not**
18
+ # replace binpack across workers — for that, use static +plan+ / +run-shards+ with +cost_binpack+.
19
+ def cmd_queue(argv)
20
+ dir = ".polyrun-queue"
21
+ paths_file = nil
22
+ worker = ENV["USER"] || "worker"
23
+ batch = 5
24
+ lease_id = nil
25
+
26
+ sub = argv.shift
27
+ Polyrun::Debug.log("queue: subcommand=#{sub.inspect}")
28
+ case sub
29
+ when "init"
30
+ queue_cmd_init(argv, dir)
31
+ when "claim"
32
+ queue_cmd_claim(argv, dir, worker, batch)
33
+ when "ack"
34
+ queue_cmd_ack(argv, dir, lease_id, worker)
35
+ when "status"
36
+ queue_cmd_status(argv, dir)
37
+ when "reclaim"
38
+ queue_cmd_reclaim(argv, dir)
39
+ else
40
+ Polyrun::Log.warn "usage: polyrun queue {init|claim|ack|status|reclaim} [options]"
41
+ 2
42
+ end
43
+ end
44
+
45
+ def queue_cmd_init(argv, dir)
46
+ cfg = Polyrun::Config.load(path: ENV["POLYRUN_CONFIG"])
47
+ pc = cfg.partition
48
+ ctx = plan_command_initial_context(pc)
49
+ paths_file = nil
50
+ OptionParser.new do |opts|
51
+ opts.banner = "usage: polyrun queue init --paths-file P [--dir DIR] [same partition options as polyrun plan]"
52
+ opts.on("--dir PATH") { |v| dir = v }
53
+ opts.on("--paths-file PATH", String) { |v| paths_file = v }
54
+ plan_command_register_partition_options!(opts, ctx)
55
+ end.parse!(argv)
56
+ unless paths_file
57
+ Polyrun::Log.warn "queue init: need --paths-file"
58
+ return 2
59
+ end
60
+ code = Polyrun::Partition::PathsBuild.apply!(partition: pc, cwd: Dir.pwd)
61
+ return code if code != 0
62
+
63
+ ordered, code = queue_partition_manifest_and_ordered_paths(cfg, pc, ctx, paths_file)
64
+ return code if code != 0
65
+
66
+ Polyrun::Queue::FileStore.new(dir).init!(ordered)
67
+ Polyrun::Log.puts JSON.generate({"dir" => File.expand_path(dir), "count" => ordered.size})
68
+ 0
69
+ end
70
+
71
+ def queue_partition_manifest_and_ordered_paths(cfg, pc, ctx, paths_file)
72
+ Polyrun::Log.warn "polyrun queue init: using #{cfg.path}" if @verbose && cfg.path
73
+
74
+ manifest, code = plan_command_manifest_from_paths(cfg, pc, [], ctx, paths_file)
75
+ return [nil, code] if code != 0
76
+
77
+ paths = manifest["paths"] || []
78
+ g = resolve_partition_timing_granularity(pc, ctx[:timing_granularity])
79
+ timing_for_sort = plan_resolve_timing_path(pc, ctx[:timing_path], ctx[:strategy])
80
+ costs = queue_init_timing_costs(timing_for_sort, g)
81
+ [queue_init_ordered_items(paths, costs, g), 0]
82
+ end
83
+
84
+ def queue_init_timing_costs(timing_for_sort, g)
85
+ return nil unless timing_for_sort
86
+
87
+ Polyrun::Partition::Plan.load_timing_costs(
88
+ File.expand_path(timing_for_sort.to_s, Dir.pwd),
89
+ granularity: g
90
+ )
91
+ end
92
+
93
+ def queue_init_ordered_items(items, costs, granularity = :file)
94
+ if costs && !costs.empty?
95
+ dw = costs.values.sum / costs.size.to_f
96
+ items.sort_by { |p| [-queue_weight_for(p, costs, dw, granularity: granularity), p] }
97
+ else
98
+ items.sort
99
+ end
100
+ end
101
+
102
+ def queue_cmd_claim(argv, dir, worker, batch)
103
+ OptionParser.new do |opts|
104
+ opts.banner = "usage: polyrun queue claim [--dir DIR] [--worker ID] [--batch N]"
105
+ opts.on("--dir PATH") { |v| dir = v }
106
+ opts.on("--worker ID") { |v| worker = v }
107
+ opts.on("--batch N", Integer) { |v| batch = v }
108
+ end.parse!(argv)
109
+ r = Polyrun::Queue::FileStore.new(dir).claim!(worker_id: worker, batch_size: batch)
110
+ Polyrun::Log.puts JSON.generate(r)
111
+ 0
112
+ end
113
+
114
+ def queue_cmd_ack(argv, dir, lease_id, worker)
115
+ OptionParser.new do |opts|
116
+ opts.banner = "usage: polyrun queue ack --lease ID [--dir DIR] [--worker ID]"
117
+ opts.on("--dir PATH") { |v| dir = v }
118
+ opts.on("--lease ID") { |v| lease_id = v }
119
+ opts.on("--worker ID") { |v| worker = v }
120
+ end.parse!(argv)
121
+ unless lease_id
122
+ Polyrun::Log.warn "queue ack: need --lease"
123
+ return 2
124
+ end
125
+ Polyrun::Queue::FileStore.new(dir).ack!(lease_id: lease_id, worker_id: worker)
126
+ Polyrun::Log.puts "ok"
127
+ 0
128
+ end
129
+
130
+ def queue_cmd_status(argv, dir)
131
+ json_detail = false
132
+ OptionParser.new do |opts|
133
+ opts.banner = "usage: polyrun queue status [--dir DIR] [--json]"
134
+ opts.on("--dir PATH") { |v| dir = v }
135
+ opts.on("--json", "Include lease details") { json_detail = true }
136
+ end.parse!(argv)
137
+ s = Polyrun::Queue::FileStore.new(dir).status(detailed: json_detail)
138
+ Polyrun::Log.puts JSON.generate(s)
139
+ 0
140
+ end
141
+
142
+ def queue_cmd_reclaim(argv, dir)
143
+ older_than = nil
144
+ worker = nil
145
+ OptionParser.new do |opts|
146
+ opts.banner = "usage: polyrun queue reclaim [--dir DIR] [--older-than DURATION] [--worker ID]"
147
+ opts.on("--dir PATH") { |v| dir = v }
148
+ opts.on("--older-than DUR", "e.g. 10m, 1h, 600s") { |v| older_than = Polyrun::Queue::Duration.parse_seconds(v) }
149
+ opts.on("--worker ID") { |v| worker = v }
150
+ end.parse!(argv)
151
+ unless older_than || worker
152
+ Polyrun::Log.warn "queue reclaim: need --older-than or --worker"
153
+ return 2
154
+ end
155
+ n = Polyrun::Queue::FileStore.new(dir).reclaim!(older_than: older_than, worker_id: worker)
156
+ Polyrun::Log.puts JSON.generate({"reclaimed_paths" => n})
157
+ 0
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,13 @@
1
+ module Polyrun
2
+ class CLI
3
+ module QuickCommand
4
+ private
5
+
6
+ def cmd_quick(argv)
7
+ require_relative "../quick/runner"
8
+ paths = argv.dup
9
+ Polyrun::Quick::Runner.run(paths: paths.empty? ? nil : paths, verbose: @verbose)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,108 @@
1
+ require "json"
2
+ require "optparse"
3
+
4
+ module Polyrun
5
+ class CLI
6
+ module ReportCommands
7
+ private
8
+
9
+ def cmd_report_junit(argv)
10
+ inputs, output, report_format = report_junit_parse_inputs(argv)
11
+ inputs.uniq!
12
+ if inputs.empty?
13
+ Polyrun::Log.warn "report-junit: need -i FILE (existing path after glob expansion)"
14
+ return 2
15
+ end
16
+
17
+ inputs = inputs.map { |p| File.expand_path(p) }
18
+ return 2 unless report_junit_inputs_exist?(inputs)
19
+
20
+ out = report_junit_resolved_output(inputs, output, report_format)
21
+ path =
22
+ if inputs.size == 1
23
+ Polyrun::Reporting::Junit.write_from_json_file(inputs.first, output_path: out, format: report_format)
24
+ else
25
+ Polyrun::Reporting::Junit.merge_rspec_json_files(inputs, output_path: out, format: report_format)
26
+ end
27
+ Polyrun::Log.puts path
28
+ 0
29
+ end
30
+
31
+ def report_junit_parse_inputs(argv)
32
+ inputs = []
33
+ output = nil
34
+ report_format = "xml"
35
+ OptionParser.new do |opts|
36
+ opts.banner = "usage: polyrun report-junit -i FILE [-i FILE]... [-o PATH] [--format xml|csv|markdown]"
37
+ opts.on("-i", "--input PATH", "RSpec JSON (repeatable; globs ok; multiple files merge examples)") do |v|
38
+ expand_merge_input_pattern(v).each { |x| inputs << x }
39
+ end
40
+ opts.on("-o", "--output PATH", "Default: <dir of first input>/junit.<ext>") { |v| output = v }
41
+ opts.on("--format VAL", "xml (default), csv, or markdown") { |v| report_format = v }
42
+ end.parse!(argv)
43
+ if inputs.empty? && argv.first
44
+ expand_merge_input_pattern(argv.first).each { |x| inputs << x }
45
+ end
46
+ [inputs, output, report_format]
47
+ end
48
+
49
+ def report_junit_inputs_exist?(inputs)
50
+ inputs.each do |p|
51
+ unless File.file?(p)
52
+ Polyrun::Log.warn "report-junit: not a file: #{p}"
53
+ return false
54
+ end
55
+ end
56
+ true
57
+ end
58
+
59
+ def report_junit_resolved_output(inputs, output, format)
60
+ if output
61
+ File.expand_path(output)
62
+ else
63
+ extension = case format.to_s.downcase
64
+ when "csv" then "csv"
65
+ when "markdown", "md" then "md"
66
+ else "xml"
67
+ end
68
+ File.join(File.dirname(inputs.first), "junit.#{extension}")
69
+ end
70
+ end
71
+
72
+ def cmd_report_timing(argv)
73
+ input = nil
74
+ out_file = nil
75
+ top = 30
76
+ report_format = "text"
77
+ OptionParser.new do |opts|
78
+ opts.banner = "usage: polyrun report-timing -i FILE [-o PATH] [--top N] [--format text|csv|markdown]"
79
+ opts.on("-i", "--input PATH", "Merged polyrun_timing.json (path => seconds)") { |v| input = v }
80
+ opts.on("-o", "--output PATH", "Write summary to file instead of stdout") { |v| out_file = v }
81
+ opts.on("--top N", Integer) { |v| top = v }
82
+ opts.on("--format VAL", "text (default), csv, or markdown") { |v| report_format = v }
83
+ end.parse!(argv)
84
+ input ||= argv.first
85
+
86
+ unless input && File.file?(input)
87
+ Polyrun::Log.warn "report-timing: need -i FILE"
88
+ return 2
89
+ end
90
+
91
+ merged = JSON.parse(File.read(File.expand_path(input)))
92
+ begin
93
+ text = Polyrun::Timing::Summary.render(merged, format: report_format, top: top)
94
+ rescue Polyrun::Error => e
95
+ Polyrun::Log.warn e.message.to_s
96
+ return 2
97
+ end
98
+ if out_file
99
+ File.write(File.expand_path(out_file), text)
100
+ Polyrun::Log.puts File.expand_path(out_file)
101
+ else
102
+ Polyrun::Log.print text
103
+ end
104
+ 0
105
+ end
106
+ end
107
+ end
108
+ end