nukadoko 0.1.0 → 0.3.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.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +417 -0
  3. data/README.ja.md +62 -7
  4. data/README.md +19 -5
  5. data/exe/nuka +8 -0
  6. data/lib/nukadoko/aria_snapshot.rb +1008 -0
  7. data/lib/nukadoko/aria_tree.rb +150 -0
  8. data/lib/nukadoko/base.rb +294 -0
  9. data/lib/nukadoko/check.rb +669 -22
  10. data/lib/nukadoko/claim_check.rb +177 -0
  11. data/lib/nukadoko/claim_check_run.rb +293 -0
  12. data/lib/nukadoko/claim_line.rb +381 -0
  13. data/lib/nukadoko/claim_rules.rb +519 -0
  14. data/lib/nukadoko/claim_sides.rb +469 -0
  15. data/lib/nukadoko/claims.rb +576 -37
  16. data/lib/nukadoko/cli.rb +814 -22
  17. data/lib/nukadoko/codes.json +947 -0
  18. data/lib/nukadoko/codes.rb +19 -0
  19. data/lib/nukadoko/command.rb +214 -12
  20. data/lib/nukadoko/criteria.rb +172 -0
  21. data/lib/nukadoko/definition.rb +33 -41
  22. data/lib/nukadoko/env_check.rb +88 -0
  23. data/lib/nukadoko/features.rb +80 -10
  24. data/lib/nukadoko/fixtures.rb +329 -11
  25. data/lib/nukadoko/http_recording.rb +68 -0
  26. data/lib/nukadoko/init.rb +129 -0
  27. data/lib/nukadoko/js_json.rb +274 -0
  28. data/lib/nukadoko/js_pattern.rb +531 -0
  29. data/lib/nukadoko/judge.rb +221 -0
  30. data/lib/nukadoko/judgment_file.rb +78 -0
  31. data/lib/nukadoko/judgments.rb +256 -0
  32. data/lib/nukadoko/literal.rb +129 -0
  33. data/lib/nukadoko/measured.rb +105 -0
  34. data/lib/nukadoko/order.rb +84 -0
  35. data/lib/nukadoko/parts.rb +43 -22
  36. data/lib/nukadoko/pattern.rb +73 -3
  37. data/lib/nukadoko/poll.rb +21 -8
  38. data/lib/nukadoko/program.rb +105 -17
  39. data/lib/nukadoko/project_files.rb +17 -0
  40. data/lib/nukadoko/project_fixtures.rb +17 -2
  41. data/lib/nukadoko/project_program.rb +119 -0
  42. data/lib/nukadoko/report_allure.rb +751 -0
  43. data/lib/nukadoko/report_messages.rb +167 -0
  44. data/lib/nukadoko/report_run.rb +169 -0
  45. data/lib/nukadoko/run.rb +898 -91
  46. data/lib/nukadoko/run_record.rb +535 -0
  47. data/lib/nukadoko/scaffold.rb +1015 -0
  48. data/lib/nukadoko/schema.rb +120 -21
  49. data/lib/nukadoko/secrets.rb +375 -0
  50. data/lib/nukadoko/selection.rb +163 -0
  51. data/lib/nukadoko/signature.rb +4 -1
  52. data/lib/nukadoko/skill.rb +36 -0
  53. data/lib/nukadoko/spaces.rb +28 -0
  54. data/lib/nukadoko/step.rb +68 -21
  55. data/lib/nukadoko/summary.rb +662 -0
  56. data/lib/nukadoko/table.rb +34 -2
  57. data/lib/nukadoko/text_fit.rb +179 -0
  58. data/lib/nukadoko/trace.rb +244 -0
  59. data/lib/nukadoko/trace_actions.rb +116 -0
  60. data/lib/nukadoko/tree_passes.rb +122 -0
  61. data/lib/nukadoko/verdict.rb +128 -0
  62. data/lib/nukadoko/version.rb +2 -1
  63. data/lib/nukadoko/vocabulary.rb +6 -0
  64. data/lib/nukadoko/walk.rb +305 -0
  65. data/lib/nukadoko/walk_files.rb +473 -0
  66. data/lib/nukadoko/walk_step.rb +149 -0
  67. data/lib/nukadoko.rb +60 -2
  68. data/llms.txt +297 -0
  69. data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
  70. data/sig/generated/nukadoko/aria_tree.rbs +142 -0
  71. data/sig/generated/nukadoko/base.rbs +142 -0
  72. data/sig/generated/nukadoko/check.rbs +255 -0
  73. data/sig/generated/nukadoko/claim_check.rbs +118 -0
  74. data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
  75. data/sig/generated/nukadoko/claim_line.rbs +158 -0
  76. data/sig/generated/nukadoko/claim_rules.rbs +172 -0
  77. data/sig/generated/nukadoko/claim_sides.rbs +207 -0
  78. data/sig/generated/nukadoko/claims.rbs +208 -0
  79. data/sig/generated/nukadoko/cli.rbs +182 -0
  80. data/sig/generated/nukadoko/codes.rbs +16 -0
  81. data/sig/generated/nukadoko/command.rbs +109 -0
  82. data/sig/generated/nukadoko/criteria.rbs +83 -0
  83. data/sig/generated/nukadoko/definition.rbs +30 -0
  84. data/sig/generated/nukadoko/env_check.rbs +34 -0
  85. data/sig/generated/nukadoko/features.rbs +101 -0
  86. data/sig/generated/nukadoko/fixtures.rbs +175 -0
  87. data/sig/generated/nukadoko/http_recording.rbs +29 -0
  88. data/sig/generated/nukadoko/init.rbs +64 -0
  89. data/sig/generated/nukadoko/js_json.rbs +109 -0
  90. data/sig/generated/nukadoko/js_pattern.rbs +194 -0
  91. data/sig/generated/nukadoko/judge.rbs +64 -0
  92. data/sig/generated/nukadoko/judgment_file.rbs +39 -0
  93. data/sig/generated/nukadoko/judgments.rbs +120 -0
  94. data/sig/generated/nukadoko/literal.rbs +71 -0
  95. data/sig/generated/nukadoko/measured.rbs +42 -0
  96. data/sig/generated/nukadoko/order.rbs +54 -0
  97. data/sig/generated/nukadoko/parts.rbs +35 -0
  98. data/sig/generated/nukadoko/pattern.rbs +62 -0
  99. data/sig/generated/nukadoko/poll.rbs +41 -0
  100. data/sig/generated/nukadoko/program.rbs +101 -0
  101. data/sig/generated/nukadoko/project_files.rbs +19 -0
  102. data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
  103. data/sig/generated/nukadoko/project_program.rbs +55 -0
  104. data/sig/generated/nukadoko/report_allure.rbs +268 -0
  105. data/sig/generated/nukadoko/report_messages.rbs +68 -0
  106. data/sig/generated/nukadoko/report_run.rbs +102 -0
  107. data/sig/generated/nukadoko/run.rbs +264 -0
  108. data/sig/generated/nukadoko/run_record.rbs +175 -0
  109. data/sig/generated/nukadoko/scaffold.rbs +328 -0
  110. data/sig/generated/nukadoko/schema.rbs +155 -0
  111. data/sig/generated/nukadoko/secrets.rbs +172 -0
  112. data/sig/generated/nukadoko/selection.rbs +73 -0
  113. data/sig/generated/nukadoko/signature.rbs +17 -0
  114. data/sig/generated/nukadoko/skill.rbs +20 -0
  115. data/sig/generated/nukadoko/spaces.rbs +31 -0
  116. data/sig/generated/nukadoko/step.rbs +115 -0
  117. data/sig/generated/nukadoko/summary.rbs +204 -0
  118. data/sig/generated/nukadoko/table.rbs +43 -0
  119. data/sig/generated/nukadoko/text_fit.rbs +70 -0
  120. data/sig/generated/nukadoko/trace.rbs +127 -0
  121. data/sig/generated/nukadoko/trace_actions.rbs +40 -0
  122. data/sig/generated/nukadoko/tree_passes.rbs +48 -0
  123. data/sig/generated/nukadoko/verdict.rbs +56 -0
  124. data/sig/generated/nukadoko/version.rbs +5 -0
  125. data/sig/generated/nukadoko/vocabulary.rbs +25 -0
  126. data/sig/generated/nukadoko/walk.rbs +139 -0
  127. data/sig/generated/nukadoko/walk_files.rbs +189 -0
  128. data/sig/generated/nukadoko/walk_step.rbs +50 -0
  129. data/sig/generated/nukadoko.rbs +26 -0
  130. data/sig/nukadoko.rbs +5 -48
  131. data/skills/nukadoko/SKILL.md +120 -0
  132. data/skills/nukadoko/references/checking-steps.md +200 -0
  133. data/skills/nukadoko/references/diagnosing.md +200 -0
  134. data/skills/nukadoko/references/done.md +99 -0
  135. data/skills/nukadoko/references/evidence.md +124 -0
  136. data/skills/nukadoko/references/examples/cli.md +230 -0
  137. data/skills/nukadoko/references/examples/http-api.md +342 -0
  138. data/skills/nukadoko/references/examples/workflow.md +173 -0
  139. data/skills/nukadoko/references/exploring.md +184 -0
  140. data/skills/nukadoko/references/fixtures.md +198 -0
  141. data/skills/nukadoko/references/from-prose.md +51 -0
  142. data/skills/nukadoko/references/judgments.md +95 -0
  143. data/skills/nukadoko/references/maintenance.md +97 -0
  144. data/skills/nukadoko/references/merge-risk.md +74 -0
  145. data/skills/nukadoko/references/parts.md +152 -0
  146. data/skills/nukadoko/references/perturbed-worlds.md +109 -0
  147. data/skills/nukadoko/references/program.md +200 -0
  148. data/skills/nukadoko/references/reviewing.md +200 -0
  149. data/skills/nukadoko/references/running.md +200 -0
  150. data/skills/nukadoko/references/scenario-rules.md +188 -0
  151. data/skills/nukadoko/references/states.md +66 -0
  152. data/skills/nukadoko/references/step-files.md +200 -0
  153. data/skills/nukadoko/references/verdicts.md +200 -0
  154. data/skills/nukadoko/references/writing-steps.md +200 -0
  155. data/skills/nukadoko/references/writing-the-feature.md +165 -0
  156. data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
  157. data/skills/nukadoko/scripts/merge-risk.jq +56 -0
  158. data/skills/nukadoko-verification/SKILL.md +263 -0
  159. metadata +133 -1
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "open3"
6
+ require "rbconfig"
7
+
8
+ # The base and the old world's passes of a `--base` run. Each row plans the
9
+ # cells its verdict needs from its head cells (RunRecord.planned_world_cells),
10
+ # and each tree runs its planned rows one environment at a time, in a
11
+ # process of its own started in the tree: a step file is loaded into the
12
+ # process that runs it, and head's steps are already loaded in this one.
13
+ # The pass writes its step records into the head run's directory, and its
14
+ # executions and cells come back as JSON. A cell whose pass did not run
15
+ # says why.
16
+ module Nukadoko
17
+ module Engine
18
+ class TreePasses
19
+ EXE = File.expand_path("../../exe/nuka", __dir__.to_s)
20
+ LIB = File.expand_path("..", __dir__.to_s)
21
+
22
+ # `judged_by` says why head's passing executions of a row judged no
23
+ # claim in Then position (Run.then_judged_by).
24
+ #: (Base, Array[Scenario], Hash[Scenario, Array[Hash[Symbol, untyped]]], Array[Environment], String, IO, ?^(Scenario) -> untyped, ?head_root: String) -> void
25
+ def initialize(base, scenarios, head_cells, environments, dir, out, judged_by = ->(_) {}, head_root: Dir.pwd)
26
+ @head_root = head_root
27
+ @judged_by = judged_by
28
+ @base = base
29
+ @scenarios = scenarios
30
+ @head_cells = head_cells
31
+ @environments = environments
32
+ @old_environments = base.old_environments || environments
33
+ @dir = dir
34
+ @out = out
35
+ @worlds = []
36
+ end
37
+
38
+ # The worlds each tree's passes started, as run.json names them.
39
+ attr_reader :worlds
40
+
41
+ # Appends each tree execution to `executions`, and returns each row's
42
+ # base and old cells by its scenario.
43
+ #: (Array[Hash[Symbol, untyped]]) -> Hash[Scenario, Array[Hash[Symbol, untyped]]]
44
+ def call(executions)
45
+ planned = @scenarios.to_h { [_1, plan(_1)] }
46
+ found = {}
47
+ [["base", @base.base_root, @environments], ["old", @base.old_root, @old_environments]].each do |world, root, environments|
48
+ environments.each do |environment|
49
+ rows = @scenarios.select { planned[_1][world.to_sym].include?(environment.name) }
50
+ next if rows.empty?
51
+
52
+ outcome = (root ? run_tree(world, root, environment, rows) : { not_run: "the #{world} world was not made" }) #: Hash[Symbol, untyped]
53
+ @worlds.concat(outcome[:worlds] || [])
54
+ rows.each do |scenario|
55
+ result = outcome[:results]&.find { |entry| [entry[:feature], entry[:line]] == place(world, scenario) }
56
+ found[[scenario, world, environment.name]] = result || { reason: outcome[:not_run] || "no execution of it was recorded" }
57
+ result&.dig(:executions)&.each do |json|
58
+ json.delete(:do)
59
+ json.merge!(role: "required", world: world, row: { feature: scenario.uri, line: scenario.line })
60
+ executions << { json: json }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ @scenarios.to_h do |scenario|
66
+ cells = [["base", @environments], ["old", @old_environments]].flat_map do |world, environments|
67
+ environments.select { planned[scenario][world.to_sym].include?(_1.name) }.map { cell(scenario, world, _1, found[[scenario, world, _1.name]]) }
68
+ end
69
+ [scenario, cells]
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ #: (Scenario) -> Hash[Symbol, Array[String]]
76
+ def plan(scenario)
77
+ change = @base.changes[Criteria.key(scenario.uri, scenario.line)]
78
+ context = change && { change: change[:change], held: scenario.tags.include?(Criteria::HELD), mapping_changed: @base.mapping[:changed],
79
+ retagged: change[:retagged] == true, renamed: !change[:renamed_from].nil?, then_judges_no_claim: @judged_by.(scenario) }
80
+ tags = Table.condition_tags(scenario.tags)
81
+ old_roles = @old_environments.map { { environment: _1.name, role: Table.role(tags, _1.conditions) } }
82
+ RunRecord.planned_world_cells(@head_cells[scenario], context, old_roles)
83
+ end
84
+
85
+ # Where a tree runs a head row: at its own place in the base tree,
86
+ # which holds head's features, and at the ref's place in the old one.
87
+ #: (String, Scenario) -> Array[untyped]
88
+ def place(world, scenario)
89
+ return [scenario.uri, scenario.line] if world == "base"
90
+
91
+ @base.at(Criteria.key(scenario.uri, scenario.line)).values_at(:feature, :line)
92
+ end
93
+
94
+ #: (Scenario, String, Environment, Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
95
+ def cell(scenario, world, environment, found)
96
+ role = world == "base" ? (@head_cells[scenario].find { _1[:environment] == environment.name }&.dig(:role) || "required") : "required"
97
+ conditions = Table.conditions_of(scenario.tags, environment.conditions)
98
+ own = found[:cell]
99
+ return { world: world, environment: environment.name, role: role, conditions: conditions, outcome: "not-run", runs: 0, reason: found[:reason] } unless own
100
+
101
+ { **own, world: world, environment: environment.name, role: role, conditions: conditions }
102
+ end
103
+
104
+ #: (String, String, Environment, Array[Scenario]) -> Hash[Symbol, untyped]
105
+ def run_tree(world, root, environment, rows)
106
+ plan = File.join(@dir, "trees", "#{world}-#{environment.name}.plan.json")
107
+ result = File.join(@dir, "trees", "#{world}-#{environment.name}.result.json")
108
+ targets = rows.map { |scenario| place(world, scenario).then { { feature: _1, line: _2 } } }
109
+ config = Nukadoko.config_options.fetch(:secrets, {}) #: Hash[Symbol, untyped]
110
+ head = { root: File.expand_path(@head_root), env_files: environment.env_files, public: (config[:public] || []).map(&:to_s), redact: (config[:redact] || []).map(&:to_s) }
111
+ File.write(plan, JSON.generate(world: world, environment: environment.name, targets: targets, dir: @dir, result: result, head: head))
112
+ output, status = Open3.capture2e(RbConfig.ruby, "-I", LIB, EXE, "__tree", plan, chdir: root)
113
+ @out.write(output.gsub(@dir, File.join(".nukadoko", "runs", File.basename(@dir))))
114
+ return JSON.parse(File.read(result), symbolize_names: true) if File.exist?(result)
115
+
116
+ { not_run: "the #{world} world's pass ended with exit #{status.exitstatus} before it recorded a result: #{output.lines.last.to_s.strip}" }
117
+ ensure
118
+ FileUtils.rm_f([plan, result].compact)
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "time"
6
+
7
+ # `nuka verdict`: whether the newest run that ended judged the working tree
8
+ # as it is now and left nothing red. It reads run.json files and one git
9
+ # tree id, and never starts a run. A run's red findings are relayed only
10
+ # when it judged this tree: a red row of an earlier tree may be fixed
11
+ # already, and a green one may not hold now.
12
+ module Nukadoko
13
+ module Engine
14
+ class Verdict
15
+ FIXES = {
16
+ "run-without-base" => "No change is necessary. Run with --base and the commit the change started from to have new and changed criteria proven against it.",
17
+ "working-tree-unknown" => "No change is necessary. In a git repository with a commit, nuka verdict compares the tree with the one the run judged."
18
+ }.freeze
19
+
20
+ attr_reader :judged #: String
21
+ attr_reader :findings #: Array[Hash[Symbol, untyped]]
22
+ attr_reader :newest #: Hash[Symbol, untyped]?
23
+ attr_reader :working_tree #: String?
24
+
25
+ #: (String) -> void
26
+ def initialize(project)
27
+ # The tree is read before the runs: a run that ends in between is
28
+ # judged against the tree before it, which can only make the answer
29
+ # stale, never wrongly fresh.
30
+ @working_tree = RunRecord.commit(project)&.fetch(:working_tree)
31
+ @newest = Verdict.newest_run(project)
32
+ @judged, @findings = judge
33
+ end
34
+
35
+ # The run with the latest start; a run.json whose run_id is not its
36
+ # directory's name, or that does not parse, is no run.
37
+ #: (String) -> Hash[Symbol, untyped]?
38
+ def self.newest_run(project)
39
+ Dir.glob(File.join(project, ".nukadoko", "runs", "*", "run.json")).filter_map do |file|
40
+ run = JSON.parse(File.read(file), symbolize_names: true)
41
+ started = Time.iso8601(run[:started_at]) rescue nil
42
+ run if started && run[:run_id] == File.basename(File.dirname(file))
43
+ rescue JSON::ParserError
44
+ nil
45
+ end.max_by { [Time.iso8601(_1[:started_at]), _1[:run_id]] }
46
+ end
47
+
48
+ #: () -> bool
49
+ def open? = findings.any? { %w[failed error].include?(_1[:severity]) }
50
+
51
+ #: () -> Hash[Symbol, untyped]
52
+ def envelope_fields
53
+ { judged: judged, working_tree: working_tree, run_id: newest&.fetch(:run_id), run_dir: newest&.fetch(:run_dir, nil),
54
+ run_working_tree: newest && recorded, base: newest&.dig(:table, :base) }
55
+ end
56
+
57
+ #: () -> String
58
+ def headline
59
+ id = newest && newest[:run_id]
60
+ case judged
61
+ when "missing" then "failed: no run has ended, so nothing judged this working tree"
62
+ when "stale" then "failed: the newest run, #{id}, judged another working tree"
63
+ when "unknown"
64
+ "#{open? ? "failed" : "passed"}: the newest run, #{id}, #{open? ? "leaves open what is below" : "left nothing red"}; whether it judged this working tree is unknown"
65
+ else
66
+ open? ? "failed: the newest run, #{id}, judged this working tree, and it leaves open what is below" : "passed: the newest run, #{id}, judged this working tree and left nothing red"
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ #: () -> String?
73
+ def recorded
74
+ run = newest or return nil
75
+ tree = run.dig(:commit, :working_tree) if run[:commit].is_a?(Hash)
76
+ tree.is_a?(String) && !tree.empty? ? tree : nil
77
+ end
78
+
79
+ #: () -> String
80
+ def again = "nuka run"
81
+
82
+ #: () -> [String, Array[Hash[Symbol, untyped]]]
83
+ def judge
84
+ unless newest
85
+ return ["missing", [{ severity: "failed", code: "run-missing", message: "no nuka run has ended in this project, so nothing judged this working tree", do: again }]]
86
+ end
87
+
88
+ id = newest[:run_id]
89
+ if working_tree && recorded != working_tree
90
+ return ["stale", [{ severity: "failed", code: "run-stale", message: stale_message, do: again }]]
91
+ end
92
+
93
+ findings = []
94
+ unless working_tree
95
+ findings << note("working-tree-unknown", "git gave no id for the working tree (outside a repository, before its first commit, or a git call failed), so whether the newest run #{id} judged it is unknown; the verdict below is that run's own")
96
+ end
97
+ if newest[:selection] == "partial"
98
+ findings << { severity: "failed", code: "run-partial", do: again,
99
+ message: "the newest run #{id} left out scenarios of the files it named (a :line target or --tag), so the criteria it left out were not judged" }
100
+ end
101
+ red = (newest[:findings] || []).select { %w[failed error].include?(_1[:severity]) }
102
+ findings.concat(red)
103
+ if newest[:verdict] == "failed" && red.empty?
104
+ findings << { severity: "failed", code: "run-failed", message: "the newest run #{id} failed, and its run.json holds no failed finding that says why",
105
+ do: "nuka report #{newest[:run_dir] || id} --format markdown" }
106
+ end
107
+ if newest.dig(:table, :base).nil?
108
+ findings << note("run-without-base", "the newest run #{id} had no --base, so no row says which criteria are new or changed, and a new one that passes without the change was not caught")
109
+ end
110
+ [working_tree ? "judged" : "unknown", findings]
111
+ end
112
+
113
+ #: (String, String) -> Hash[Symbol, untyped]
114
+ def note(code, message) = { severity: "note", code: code, message: message, fix: FIXES.fetch(code) }
115
+
116
+ #: () -> String
117
+ def stale_message
118
+ run = newest || {}
119
+ tree = working_tree.to_s[0, 12]
120
+ head = "the newest run #{run[:run_id]} (started #{run[:started_at]})"
121
+ ran = recorded
122
+ return "#{head} recorded no working tree id (outside git then, or written by an earlier nukadoko), so it cannot be matched to the working tree #{tree}" unless ran
123
+
124
+ "#{head} judged working tree #{ran[0, 12]}, and the working tree is now #{tree}: its verdicts say nothing about what changed since"
125
+ end
126
+ end
127
+ end
128
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  module Nukadoko
4
- VERSION = "0.1.0"
5
+ VERSION = "0.3.0"
5
6
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  # What `nuka steps` and `nuka describe` print: each step as data, in the
4
5
  # fields the TypeScript engine prints, so an agent picks a step from either
@@ -7,6 +8,7 @@ module Nukadoko
7
8
  module Vocabulary
8
9
  BROWSER = %i[page context pageAs].freeze
9
10
 
11
+ #: (singleton(Step), String) -> Hash[Symbol, untyped]
10
12
  def self.entry(step, project)
11
13
  {
12
14
  name: step.step_name, kind: "typed", file: step.file.delete_prefix("#{project}/"),
@@ -17,6 +19,7 @@ module Nukadoko
17
19
  }.compact
18
20
  end
19
21
 
22
+ #: (singleton(Step), String) -> Hash[Symbol, untyped]
20
23
  def self.describe(step, project)
21
24
  # A step with no returns returns an empty object, as zod's default says.
22
25
  returns = step.returns&.json_schema(output: true) || { "$schema": "https://json-schema.org/draft/2020-12/schema", default: {}, type: "object", properties: {}, additionalProperties: false }
@@ -24,6 +27,7 @@ module Nukadoko
24
27
  end
25
28
 
26
29
 
30
+ #: (singleton(Step)) -> Hash[untyped, untyped]?
27
31
  def self.from(step)
28
32
  return nil if step.from.empty?
29
33
 
@@ -33,6 +37,7 @@ module Nukadoko
33
37
  end
34
38
  end
35
39
 
40
+ #: (singleton(Step)) -> String
36
41
  def self.text(step)
37
42
  lines = ["#{step.step_name} typed #{step.claims ? "claims" : step.mutating? ? "mutates" : "read-only"}",
38
43
  " #{step.pattern || "(no pattern)"}", " #{step.description}"]
@@ -41,6 +46,7 @@ module Nukadoko
41
46
  lines.join("\n")
42
47
  end
43
48
 
49
+ #: (Hash[Symbol, untyped]) -> String
44
50
  def self.claim_text(claim)
45
51
  return "each #{claim[:actual]}" if claim[:each]
46
52
 
@@ -0,0 +1,305 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # A walk of `nuka run --step` as data: the states one walk passes through,
5
+ # the one transition the run applies to move it, and what one `nuka step`
6
+ # call does next and prints, in the TypeScript engine's shape
7
+ # (src/run/walk.ts). Pure: no file system, no clock, no process. WalkFiles
8
+ # reads and writes the files both sides share, and Run holds the step that
9
+ # pauses.
10
+ #
11
+ # One walk is one execution of one scenario. Pause k comes after step k, so
12
+ # a pause number names the step a person was shown, and an answer names the
13
+ # walk and the pause it answers: an answer to a pause nobody was shown, or
14
+ # to an earlier walk the same command once answered, is refused, never
15
+ # applied to whichever pause is open. Pause numbers only increase, and
16
+ # `ended` absorbs every later event, so a replayed answer or a late signal
17
+ # cannot move a walk backwards.
18
+ #
19
+ # A state is a hash with symbol keys, as walk.json holds it once parsed:
20
+ # `state` is starting, running (with `after`), paused (with `pause`), or
21
+ # ended (with `end`, `ended_at`, and `reached`).
22
+ module Nukadoko
23
+ module Engine
24
+ module Walk
25
+ # A transition the walk's table does not have: a bug in the run that
26
+ # applies events, never something a reader of walk.json can cause.
27
+ class TransitionError < StandardError; end
28
+
29
+ START_KEYS = %i[number pid target started_at].freeze
30
+ RUN_KEYS = %i[run_id run_dir scenario world].freeze
31
+
32
+ module_function
33
+
34
+ # One line on what failed the scenario at a pause. The Ruby engine has
35
+ # no AfterStep hooks, but a pause read from a TypeScript walk can
36
+ # name one.
37
+ #: (Hash[Symbol, untyped]) -> String
38
+ def failure_text(pause)
39
+ failure = pause[:failure]
40
+ message = failure[:message].to_s.split("\n").first || ""
41
+ failure[:by] == "step" ? "step #{pause[:pause]} failed: #{message}" : "an AfterStep hook after step #{pause[:pause]} failed: #{message}"
42
+ end
43
+
44
+ # The walk's first state, before the run knows its scenario.
45
+ #: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
46
+ def start(start) = { state: "starting", **start.slice(*START_KEYS) }
47
+
48
+ # One transition:
49
+ #
50
+ # starting --begun--> running(after 0) --paused(k > after)--> paused(k)
51
+ # paused(k) --answered(k)--> running(after k)
52
+ # any live state --ended--> ended, which absorbs every later event
53
+ #
54
+ # Raises TransitionError for any other pair.
55
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
56
+ def advance(state, event)
57
+ return state if state[:state] == "ended"
58
+
59
+ starts = state.slice(*START_KEYS)
60
+ if event[:kind] == "ended"
61
+ run = state[:state] == "starting" ? {} : state.slice(*RUN_KEYS)
62
+ reached = case state[:state]
63
+ when "paused" then state[:pause][:pause]
64
+ when "running" then state[:after]
65
+ else 0
66
+ end
67
+ return { state: "ended", **starts, **run, end: event[:end], ended_at: event[:at], reached: reached }
68
+ end
69
+ case [state[:state], event[:kind]]
70
+ when %w[starting begun] then return { state: "running", **starts, **event[:run].slice(*RUN_KEYS), after: 0 }
71
+ when %w[running paused]
72
+ return { state: "paused", **starts, **state.slice(*RUN_KEYS), pause: event[:pause] } if event[:pause][:pause] > state[:after]
73
+ when %w[paused answered]
74
+ return { state: "running", **starts, **state.slice(*RUN_KEYS), after: state[:pause][:pause] } if event[:pause] == state[:pause][:pause]
75
+ end
76
+ raise TransitionError, "a walk #{state[:state]} cannot take #{event[:kind]}"
77
+ end
78
+
79
+ # The one spelling of an answer, for the do lines and the refusals.
80
+ #: (Integer, Integer, String) -> String
81
+ def answer_command(walk, pause, answer) = "nuka step #{answer} #{pause} --walk #{walk}"
82
+
83
+ # The one spelling of a wait for one walk.
84
+ #: (Integer) -> String
85
+ def wait_command(walk) = "nuka step wait --walk #{walk}"
86
+
87
+ # The command a walk that ended has to be started with again.
88
+ #: (String) -> String
89
+ def restart_command(target) = "nuka run #{target} --step"
90
+
91
+ # walk.json as `nuka step` reads it. A walk whose process is gone
92
+ # without recording its end is `gone`: killed, or crashed. `none`: no
93
+ # walk was ever recorded in the project.
94
+ #: (Hash[Symbol, untyped]?, ^(Integer) -> bool) -> Hash[Symbol, untyped]
95
+ def see(stored, alive)
96
+ return { state: "none" } if stored.nil?
97
+ return { state: "gone", last: stored } if stored[:state] != "ended" && !alive.(stored[:pid])
98
+
99
+ stored
100
+ end
101
+
102
+ #: (Hash[Symbol, untyped]) -> Integer
103
+ def number_of(seen) = seen[:state] == "gone" ? seen[:last][:number] : seen[:number]
104
+
105
+ # The walk one `nuka step` call is about, fixed when the call starts.
106
+ # A call is { kind: "wait", walk: Integer? } or { kind: "answer",
107
+ # walk:, pause:, answer: }. A wait that names no walk is about the
108
+ # newest walk until some `nuka step` call has reported how that walk
109
+ # ended, and about the next walk after that, which a `nuka run
110
+ # --step` started just before the wait may not have recorded yet. So
111
+ # a walk that ended before anyone looked is still reported, and an
112
+ # end is not reported in place of the walk that follows it.
113
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped], ^(Integer) -> bool) -> Integer
114
+ def call_target(call, at_start, end_reported)
115
+ return call[:walk] if call[:kind] == "answer" || call[:walk]
116
+ return 1 if at_start[:state] == "none"
117
+
118
+ number = number_of(at_start)
119
+ over = %w[ended gone].include?(at_start[:state])
120
+ over && end_reported.(number) ? number + 1 : number
121
+ end
122
+
123
+ # How a walk stands, after its number: "ended: interrupted by SIGTERM,
124
+ # exit 143", "is at pause 2".
125
+ #: (Hash[Symbol, untyped]) -> String
126
+ def standing(seen)
127
+ case seen[:state]
128
+ when "gone" then "is gone: its nuka run --step process (pid #{seen[:last][:pid]}) ended without recording how"
129
+ when "paused" then "is at pause #{seen[:pause][:pause]}"
130
+ when "running" then seen[:after].zero? ? "is running its first step" : "is running the step after pause #{seen[:after]}"
131
+ when "starting" then "is starting"
132
+ else "ended#{how_ended(seen[:end])}"
133
+ end
134
+ end
135
+
136
+ # What follows "ended" in a walk's standing.
137
+ #: (Hash[Symbol, untyped]) -> String
138
+ def how_ended(finish)
139
+ case finish[:how]
140
+ when "interrupted" then ": interrupted by #{finish[:signal]}, exit #{finish[:exit_code]}"
141
+ when "not-run" then " before its scenario ran, exit #{finish[:exit_code]}"
142
+ when "error" then " on an unexpected error, exit #{finish[:exit_code]}"
143
+ else
144
+ stopped = finish[:stopped]
145
+ return ": the run finished, exit #{finish[:exit_code]}" unless stopped
146
+
147
+ ": stopped after step #{stopped[:after_step]}#{stopped[:line].nil? ? "" : " (line #{stopped[:line]})"}, exit #{finish[:exit_code]}"
148
+ end
149
+ end
150
+
151
+ # The message of a wait that found no walk to report before its time
152
+ # ran out: which walk it waited for, and the last walk there is and
153
+ # how it stands, so the caller is not told that nothing ran.
154
+ #: (Hash[Symbol, untyped], Integer, Hash[Symbol, untyped]) -> String
155
+ def nothing_started_message(call, seconds, seen)
156
+ waited = call[:walk] ? "walk #{call[:walk]} did not start within #{seconds}s" : "no new walk started within #{seconds}s"
157
+ return "#{waited}, and this project has recorded no walk" if seen[:state] == "none"
158
+
159
+ "#{waited}; the last walk, walk #{number_of(seen)}, #{standing(seen)}"
160
+ end
161
+
162
+ #: (Hash[Symbol, untyped]) -> String
163
+ def where_text(seen)
164
+ case seen[:state]
165
+ when "paused" then "the walk is at pause #{seen[:pause][:pause]}"
166
+ when "running" then seen[:after].zero? ? "the walk is running its first step" : "the walk is running the step after pause #{seen[:after]}"
167
+ else "the walk has not reached its first pause"
168
+ end
169
+ end
170
+
171
+ # Where the walk walk.json holds stands, for a refusal that names it.
172
+ #: (Hash[Symbol, untyped]) -> String
173
+ def current_text(seen)
174
+ case seen[:state]
175
+ when "gone" then "walk #{seen[:last][:number]}, which is gone"
176
+ when "ended" then "walk #{seen[:number]}, which has ended"
177
+ when "paused" then "walk #{seen[:number]}, at pause #{seen[:pause][:pause]}"
178
+ when "running" then "walk #{seen[:number]}, running"
179
+ else "walk #{seen[:number]}, starting"
180
+ end
181
+ end
182
+
183
+ # A call that names a walk walk.json no longer holds, or does not hold
184
+ # yet. The do is a wait for the walk there is, which answers nothing.
185
+ #: (Hash[Symbol, untyped], Integer, Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
186
+ def not_current(call, target, seen)
187
+ what = call[:kind] == "answer" ? "this answer" : "this wait"
188
+ { kind: "refuse", code: "walk-not-current", message: "#{what} is for walk #{target}, and the walk in this project is #{current_text(seen)}",
189
+ do: wait_command(number_of(seen)) }
190
+ end
191
+
192
+ # What one `nuka step` call does next, from walk.json as it stands now
193
+ # (`seen`), the walk the call is about (`target`), and the answers
194
+ # that walk's run directory holds, by pause. Pure, so that repeating a
195
+ # call is a property test: an answer already given the same way
196
+ # reports where the walk went from it, and writes nothing again.
197
+ #: (Hash[Symbol, untyped], Integer, Hash[Symbol, untyped], Hash[Integer, String]) -> Hash[Symbol, untyped]
198
+ def decide(call, target, seen, answers)
199
+ if call[:kind] == "wait"
200
+ return { kind: "wait" } if seen[:state] == "none" || number_of(seen) < target
201
+ # A wait that names its walk is about that walk alone.
202
+ return not_current(call, target, seen) if call[:walk] && number_of(seen) > target
203
+ return { kind: "wait" } if %w[starting running].include?(seen[:state])
204
+
205
+ return { kind: "report", seen: seen }
206
+ end
207
+ if seen[:state] == "none"
208
+ return { kind: "refuse", code: "walk-not-found", message: "no walk to answer: nuka run --step has not started one in this project" }
209
+ end
210
+ return not_current(call, target, seen) if number_of(seen) != target
211
+ return { kind: "report", seen: seen } if seen[:state] == "gone"
212
+
213
+ given = answers[call[:pause]]
214
+ if given && given != call[:answer]
215
+ ended = seen[:state] == "ended" ? ", and walk #{target} #{standing(seen)}" : ""
216
+ return { kind: "refuse", code: "pause-answered", message: "pause #{call[:pause]} was already answered #{given}#{ended}, so this #{call[:answer]} changes nothing",
217
+ do: wait_command(target) }
218
+ end
219
+ if seen[:state] == "ended"
220
+ # A pause the walk never reached is no answer to it, and a report
221
+ # would read as one.
222
+ if call[:pause] > seen[:reached]
223
+ finish = seen[:end]
224
+ when_ = if finish[:how] == "not-run" then ""
225
+ elsif seen[:reached].zero? then " before its first pause"
226
+ else " after pause #{seen[:reached]}"
227
+ end
228
+ return { kind: "refuse", code: "pause-not-current", message: "pause #{call[:pause]} is not a pause of walk #{target}, which ended#{when_}#{how_ended(finish)}",
229
+ do: wait_command(target) }
230
+ end
231
+ # Nothing is left to answer, and how the walk ended is what the
232
+ # caller needs.
233
+ return { kind: "report", seen: seen }
234
+ end
235
+ if given.nil?
236
+ return { kind: "write-answer", run_dir: seen[:run_dir] } if seen[:state] == "paused" && seen[:pause][:pause] == call[:pause]
237
+
238
+ return { kind: "refuse", code: "pause-not-current", message: "pause #{call[:pause]} is not the pause the walk waits at: #{where_text(seen)}",
239
+ do: wait_command(target) }
240
+ end
241
+ # Given this way already: a repeat, or this call's own write. Wait
242
+ # until the run has read it and moved on.
243
+ return { kind: "report", seen: seen } if seen[:state] == "paused" && seen[:pause][:pause] > call[:pause]
244
+
245
+ { kind: "wait" }
246
+ end
247
+
248
+ #: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
249
+ def run_fields(state)
250
+ return {} if state[:state] == "starting"
251
+
252
+ state.slice(*RUN_KEYS)
253
+ end
254
+
255
+ # What `nuka step` prints beside the envelope's head: its exit code,
256
+ # its findings, and its fields. 0 for a walk paused, starting,
257
+ # running, or ended green; 1 for one paused where its scenario
258
+ # failed, ended red, interrupted, or gone. A walk starting or running
259
+ # is one the call's wait ran out on: the call itself is then the
260
+ # `do`, since repeating it is safe.
261
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
262
+ def report(seen, call)
263
+ case seen[:state]
264
+ when "gone"
265
+ last = seen[:last]
266
+ finding = { severity: "failed", code: "walk-gone",
267
+ message: "the nuka run --step process (pid #{last[:pid]}) ended without recording how: it was killed or it crashed, so its world and browser may still run",
268
+ do: restart_command(last[:target]) }
269
+ { exit_code: 1, findings: [finding], fields: { walk: "gone", last: last } }
270
+ when "ended"
271
+ findings = seen[:end][:how] == "interrupted" ? [] : seen[:end][:findings]
272
+ { exit_code: seen[:end][:exit_code].zero? ? 0 : 1, findings: findings, fields: { walk: "ended", **run_fields(seen), end: seen[:end] } }
273
+ when "paused"
274
+ pause = seen[:pause]
275
+ # A pause after a failure answers no differently, but what it
276
+ # shows is a red scenario: a caller that reads only the exit code
277
+ # and the findings must not read it as passing.
278
+ findings = if pause[:failure]
279
+ [{ severity: "failed", code: "walk-failed", message: failure_text(pause), do: answer_command(seen[:number], pause[:pause], "next") }]
280
+ else []
281
+ end
282
+ answers = { next: answer_command(seen[:number], pause[:pause], "next"),
283
+ **(pause[:remaining].positive? ? { stop: answer_command(seen[:number], pause[:pause], "stop") } : {}) }
284
+ { exit_code: findings.empty? ? 0 : 1, findings: findings, fields: { walk: "paused", **run_fields(seen), pause: pause, do: answers } }
285
+ else
286
+ again = call[:kind] == "answer" ? answer_command(call[:walk], call[:pause], call[:answer]) : wait_command(seen[:number])
287
+ return { exit_code: 0, findings: [], fields: { walk: "starting", do: again } } if seen[:state] == "starting"
288
+
289
+ { exit_code: 0, findings: [], fields: { walk: "running", **run_fields(seen), after: seen[:after], do: again } }
290
+ end
291
+ end
292
+
293
+ # The lines `nuka run --step` writes on stderr at a pause of walk
294
+ # `walk`, outside `--json`: the step, what failed when the scenario
295
+ # failed there, and the answers.
296
+ #: (Integer, Hash[Symbol, untyped]) -> String
297
+ def pause_lines(walk, pause)
298
+ following = answer_command(walk, pause[:pause], "next")
299
+ either = pause[:remaining].positive? ? " (or: #{answer_command(walk, pause[:pause], "stop")})" : ""
300
+ failed = pause[:failure] ? " failure: #{failure_text(pause)}\n" : ""
301
+ "paused after step #{pause[:pause]}/#{pause[:of]} (#{pause[:status]}): #{pause[:text]}\n#{failed} do: #{following}#{either}\n"
302
+ end
303
+ end
304
+ end
305
+ end