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,221 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "time"
6
+
7
+ # `nuka judge <file>:<line>`, the one way a judgment gets into a judgment
8
+ # file (docs/spec.md "Judgments"). Without a verdict flag it shows what the
9
+ # criterion asks a judge: each value its Then read, the quality the
10
+ # sentence names, and where the record keeps the value. With `--meets` or
11
+ # `--misses`, `--by`, and `--because` it records one judgment of each.
12
+ #
13
+ # It checks what a machine can, and only that: the run judged this
14
+ # criterion, its required head cells passed, each value is in a channel
15
+ # nukadoko recorded, and the value the step record holds has the digest
16
+ # run.json recorded, so a judgment can only cite a value a run wrote down.
17
+ # It never checks the judgment, nor who made it: `--by` is a name, and the
18
+ # commit that adds the file, with its review, is the record of who judged.
19
+ # It shows and records; it never runs.
20
+ module Nukadoko
21
+ class CLI
22
+ class Judge
23
+ RUNS = File.join(".nukadoko", "runs")
24
+
25
+ #: (String, Array[String], Hash[String, Array[String]], json: bool, out: IO) -> void
26
+ def initialize(project, rest, options, json:, out:)
27
+ @project = project
28
+ @target = rest.first.to_s
29
+ @run_name = options["--run"]&.last
30
+ @meets = options.key?("--meets")
31
+ @misses = options.key?("--misses")
32
+ @by = options["--by"]&.last
33
+ @because = options["--because"]&.last
34
+ @json = json
35
+ @out = out
36
+ end
37
+
38
+ #: () -> Integer
39
+ def call
40
+ return refused("--meets and --misses say opposite things: give one") if @meets && @misses
41
+
42
+ place = parse_target(@target) or return refused("#{@target} names no criterion: write it as <feature file>:<scenario line>")
43
+ verdict = (@meets && "meets") || (@misses && "misses")
44
+ if verdict && (@by.to_s.strip.empty? || @because.to_s.strip.empty?)
45
+ return refused("a judgment needs --by, the judge's name, and --because, why the value meets or misses the sentence, in one sentence")
46
+ end
47
+ return refused("--because is one sentence, on one line") if verdict && @because.to_s.match?(/[\r\n]/)
48
+
49
+ target = "#{place[:feature]}:#{place[:line]}"
50
+ rerun = "nuka run #{Engine::Selection.shell_word(target)}"
51
+ run = run_named(@run_name)
52
+ unless run
53
+ return refused(@run_name ? "no run that ended is named #{@run_name}" : "no run has ended in this project, so no value was read to judge", run: rerun)
54
+ end
55
+ row = run[:table][:rows].find { _1[:feature] == place[:feature] && _1[:line] == place[:line] }
56
+ values = row ? Engine::Judgments.judged_values(row, run[:scenarios]) : []
57
+ why = unjudgeable(row, target, run, values)
58
+ return refused(why, run: rerun) if why
59
+
60
+ file = Engine::JudgmentFile.read(@project, place[:feature])
61
+ return refused("the judgments of #{place[:feature]} were not read: #{file[:problem]}") if file[:problem]
62
+
63
+ run_dir = File.join(@project, run[:run_dir] || File.join(RUNS, run[:run_id]))
64
+ reads = Engine::Judgments.read_each(values, row[:name], file[:entries])
65
+ shown = values.each_with_index.map do |value, index|
66
+ read = reads.fetch(index)
67
+ if verdict
68
+ if read[:state] == "uncitable"
69
+ return refused("line #{read[:line]} read #{read[:value_from]}, which no channel of a step record holds, so a judgment cannot cite it")
70
+ end
71
+
72
+ record = begin
73
+ JSON.parse(File.read(File.join(run_dir, value[:record].to_s, "record.json"), encoding: "UTF-8"))
74
+ rescue StandardError => e
75
+ return refused("#{run[:run_id]} holds no readable step record for line #{value[:line]}: #{e.message}")
76
+ end
77
+ disagree = record_disagrees(value, record)
78
+ if disagree
79
+ return refused(disagree, fix: "a run's records changed after it ended, so neither can be cited: run the criterion again, and judge the value the new run reads", run: rerun)
80
+ end
81
+ end
82
+ { read: read, value: value[:value], seen: seen_of(run, read) }
83
+ end
84
+ return show(target, row, run, shown, place) unless verdict
85
+
86
+ record(target, row, run, shown, place, file[:entries], verdict, rerun)
87
+ end
88
+
89
+ private
90
+
91
+ #: (String) -> Hash[Symbol, untyped]?
92
+ def parse_target(target)
93
+ match = /\A(.+):([1-9][0-9]*)\z/.match(target) or return nil
94
+ { feature: match[1].to_s, line: match[2].to_i }
95
+ end
96
+
97
+ # The run a name names (its id or its directory), or the newest run
98
+ # that ended.
99
+ #: (String?) -> Hash[Symbol, untyped]?
100
+ def run_named(name)
101
+ runs = Dir.glob(File.join(@project, RUNS, "*", "run.json")).sort.filter_map do |file|
102
+ document = JSON.parse(File.read(file, encoding: "UTF-8"), symbolize_names: true)
103
+ document if document.is_a?(Hash) && document[:run_id] == File.basename(File.dirname(file))
104
+ rescue JSON::ParserError
105
+ nil
106
+ end
107
+ return runs.find { _1[:run_id] == name || _1[:run_dir] == name || File.basename(_1[:run_dir].to_s) == name } if name
108
+
109
+ runs.max_by { [(Time.iso8601(_1[:started_at].to_s) rescue Time.at(0)), _1[:run_id]] }
110
+ end
111
+
112
+ #: (Hash[Symbol, untyped]?, String, Hash[Symbol, untyped], Array[untyped]) -> String?
113
+ def unjudgeable(row, target, run, values)
114
+ return "#{run[:run_id]} ran no criterion at #{target}" unless row
115
+
116
+ required = row[:cells].select { _1[:world] == "head" && _1[:role] == "required" }
117
+ if required.empty? || required.any? { _1[:outcome] != "pass" }
118
+ return "#{target} did not pass in #{run[:run_id]} (#{row[:reason]}), and only a criterion whose required cells passed has a value to judge"
119
+ end
120
+ return "#{target} asks for no judgment in #{run[:run_id]}: no Then of it has a judged-as claim that read a value" if values.empty?
121
+
122
+ nil
123
+ end
124
+
125
+ # Why the value a step record holds for a read is not the one run.json
126
+ # recorded, or nil when the two agree.
127
+ #: (Hash[Symbol, untyped], untyped) -> String?
128
+ def record_disagrees(read, record)
129
+ name = read[:record] || "the step record"
130
+ claims = record.is_a?(Hash) ? record["claims"] : nil
131
+ claim = (claims.is_a?(Array) ? claims[read[:claim]] : nil) #: untyped
132
+ return "#{name} holds no value for claim #{read[:claim]}" unless claim.is_a?(Hash) && claim.key?("actual")
133
+ return "#{name} records claim #{read[:claim]} as #{claim["outcome"]}, not for-judgment" unless claim["outcome"] == "for-judgment"
134
+
135
+ digest = Engine::Judgments.value_digest(claim["actual"])
136
+ digest == read[:value_digest] ? nil : "#{name} holds a value whose digest is #{digest}, and run.json recorded #{read[:value_digest]}, so the two records disagree"
137
+ end
138
+
139
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped]) -> Array[String]
140
+ def seen_of(run, read)
141
+ scenario = run[:scenarios].find { |entry| !entry[:world] && entry[:environment] == read[:environment] && entry[:steps].any? { _1[:line] == read[:line] && _1[:text] == read[:step] } }
142
+ step = scenario && scenario[:steps].find { _1[:line] == read[:line] && _1[:text] == read[:step] }
143
+ (step&.dig(:judged_view, :pages) || []).flat_map { [_1.dig(:screenshot, :file), _1.dig(:aria, :file)].grep(String) }
144
+ end
145
+
146
+ #: (untyped) -> Array[String]
147
+ def value_block(value)
148
+ text = value.is_a?(String) ? value : JSON.pretty_generate(value)
149
+ text.split("\n", -1).map { " #{_1}" }
150
+ end
151
+
152
+ #: (String, Hash[Symbol, untyped], Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Hash[Symbol, untyped]) -> Integer
153
+ def show(target, row, run, shown, place)
154
+ if @json
155
+ values = shown.map { |entry| { **entry[:read], value: entry[:value], seen: entry[:seen] } }
156
+ return emit({ run_id: run[:run_id], feature: place[:feature], line: place[:line], criterion: row[:name], values: values })
157
+ end
158
+
159
+ dir = run[:run_dir] || File.join(RUNS, run[:run_id])
160
+ lines = [%(#{target} "#{row[:name]}" in #{run[:run_id]}: #{shown.size == 1 ? "1 value" : "#{shown.size} values"} to judge)]
161
+ shown.each do |entry|
162
+ read = entry[:read]
163
+ lines << %(- line #{read[:line]} "#{read[:step]}", claim #{read[:claim]}, in #{read[:environment]}: #{read[:state]})
164
+ lines << " quality: #{read[:quality] || "the sentence"}"
165
+ lines << " value, #{read[:value_from]}, measured #{read[:measured] || "none"}:"
166
+ lines.concat(value_block(entry[:value]))
167
+ lines << " digest: #{read[:value_digest]}"
168
+ lines << " record: #{File.join(dir, read[:record], "record.json")}" if read[:record]
169
+ lines << " seen: #{entry[:seen].map { File.join(dir, _1) }.join(", ")}" if entry[:seen].any?
170
+ judgment = read[:judgment]
171
+ lines << " judgment: #{judgment[:verdict]} by #{judgment[:by]} in #{judgment[:run_id]}: #{judgment[:because]}" if judgment
172
+ end
173
+ @out.puts lines
174
+ @out.puts " fix: judge each value against its quality, then record it: nuka judge #{Engine::Selection.shell_word(target)} --run #{run[:run_id]} " \
175
+ "--meets or --misses --by <who> --because <one sentence>"
176
+ 0
177
+ end
178
+
179
+ #: (String, Hash[Symbol, untyped], Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], String, String) -> Integer
180
+ def record(target, row, run, shown, place, entries, verdict, rerun)
181
+ at = Time.now.utc.iso8601(3)
182
+ shown.each do |entry|
183
+ read = entry[:read]
184
+ entries = Engine::Judgments.add(entries, { criterion: row[:name], then: read[:step], claim: read[:claim], quality: read[:quality],
185
+ value_digest: read[:value_digest], value: entry[:value], verdict: verdict,
186
+ because: @because.to_s.strip, by: @by.to_s.strip, run_id: run[:run_id],
187
+ commit: run.dig(:commit, :sha), at: at })
188
+ end
189
+ written = Engine::JudgmentFile.write(@project, place[:feature], entries)
190
+ if @json
191
+ return emit({ run_id: run[:run_id], feature: place[:feature], line: place[:line], verdict: verdict, file: written,
192
+ value_digests: shown.map { _1[:read][:value_digest] }, do: rerun })
193
+ end
194
+
195
+ shown.each { @out.puts [verdict, target, "line #{_1[:read][:line]} claim #{_1[:read][:claim]}", _1[:read][:value_digest]].join("\t") }
196
+ $stderr.puts "wrote #{written}: commit it, so its review records who judged; the next run reads #{target} against it"
197
+ $stderr.puts " do: #{rerun}"
198
+ 0
199
+ end
200
+
201
+ #: (Hash[Symbol, untyped]) -> Integer
202
+ def emit(fields)
203
+ @out.puts JSON.pretty_generate({ schema_version: Engine::RunRecord::SCHEMA_VERSION, nukadoko: VERSION, command: "judge", **fields, verdict: "passed", findings: [] })
204
+ 0
205
+ end
206
+
207
+ #: (String, ?fix: String?, ?run: String?) -> Integer
208
+ def refused(message, fix: nil, run: nil)
209
+ finding = Engine::Refusal.new("judgment-refused", message, fix: fix, run: run).to_h("judge")
210
+ if @json
211
+ @out.puts JSON.pretty_generate({ schema_version: Engine::RunRecord::SCHEMA_VERSION, nukadoko: VERSION, command: "judge", verdict: "refused", findings: [finding] })
212
+ else
213
+ @out.puts [finding[:severity], finding[:code], Engine::Summary.where_of(finding), finding[:message]].join("\t")
214
+ @out.puts " fix: #{finding[:fix]}" if finding[:fix]
215
+ @out.puts " do: #{finding[:do]}" if finding[:do]
216
+ end
217
+ 2
218
+ end
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "fileutils"
5
+
6
+ # Where the judgments of a feature file live, reading and writing them,
7
+ # and, under `--base`, which of them changed since the ref (docs/spec.md
8
+ # "Judgments"). What an entry means is lib/nukadoko/judgments.rb's.
9
+ #
10
+ # The files sit in `judgments/` at the project root, one per feature file
11
+ # at the same relative path with `.json` added, outside the features
12
+ # directory on purpose: there every file is mapping, so a judgment added
13
+ # would make `--base` run each unchanged criterion again in the old world,
14
+ # when a judgment changes no step. Nor do they live in the run directory,
15
+ # which retention removes and nobody reviews: a judgment is committed, and
16
+ # the commit and its review are the record of who judged.
17
+ module Nukadoko
18
+ module Engine
19
+ module JudgmentFile
20
+ DIR = "judgments"
21
+
22
+ module_function
23
+
24
+ #: (String) -> String
25
+ def of(feature) = "#{DIR}/#{feature}.json"
26
+
27
+ # What a feature's judgment file holds: its entries, none when there
28
+ # is no file, or why it could not be read.
29
+ #: (String, String) -> Hash[Symbol, untyped]
30
+ def read(root, feature)
31
+ text = begin
32
+ File.read(File.join(root, of(feature)), encoding: "UTF-8")
33
+ rescue Errno::ENOENT
34
+ return { entries: [] }
35
+ rescue SystemCallError, IOError => e
36
+ return { problem: "it could not be read: #{e.message}" }
37
+ end
38
+ Judgments.parse_file(text)
39
+ end
40
+
41
+ # Writes a feature's judgment file whole, and returns its path
42
+ # relative to the project root.
43
+ #: (String, String, Array[Hash[Symbol, untyped]]) -> String
44
+ def write(root, feature, entries)
45
+ relative = of(feature)
46
+ file = File.join(root, relative)
47
+ FileUtils.mkdir_p(File.dirname(file))
48
+ File.write(file, Judgments.file_text(entries))
49
+ relative
50
+ end
51
+
52
+ # Each judgment file the working tree and `commit` hold with other
53
+ # text, or one holds and the other does not, relative to the project
54
+ # root and sorted. nil when git does not answer.
55
+ #: (String, String) -> Array[String]?
56
+ def changed(project, commit)
57
+ top, ok = Base.git(project, "rev-parse", "--show-toplevel")
58
+ return nil unless ok
59
+
60
+ prefix = Base.git(project, "rev-parse", "--show-prefix").first.delete_suffix("/")
61
+ dir = prefix.empty? ? "#{DIR}/" : "#{prefix}/#{DIR}/"
62
+ listed, ok = Base.git(top, "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", dir, strip: false)
63
+ return nil unless ok
64
+
65
+ at_base, ok = Base.git(top, "ls-tree", "-r", "-z", "--name-only", commit, "--", dir, strip: false)
66
+ return nil unless ok
67
+
68
+ names = (listed.split("\0") + at_base.split("\0")).reject(&:empty?).uniq
69
+ names.filter_map do |name|
70
+ now = File.file?(File.join(top, name)) ? File.binread(File.join(top, name)) : nil
71
+ before, found = Base.git(top, "show", "#{commit}:#{name}", strip: false)
72
+ before = nil unless found
73
+ name.delete_prefix(prefix.empty? ? "" : "#{prefix}/") if now != before&.b
74
+ end.sort
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,256 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "digest"
5
+ require "json"
6
+
7
+ # Judgments (docs/spec.md "Judgments"): the verdicts on a criterion that no
8
+ # compare can give, such as whether a text is friendly. A Then asks for one
9
+ # with a `judged-as` claim; the run reads the value and records it, and a
10
+ # reader judges it after the run and commits the judgment. nukadoko checks
11
+ # two things alone, both exactly: that a judgment cites a value this run's
12
+ # records hold (by its digest), and that the value did not change since.
13
+ # The judgment itself is never checked, and never folded into the machine
14
+ # verdict: a row keeps its verdict from the cells, and gains a `judgment`
15
+ # beside it, whose state has a gate of its own.
16
+ #
17
+ # Pure, and in the words the TypeScript engine uses. The files on disk are
18
+ # lib/nukadoko/judgment_file.rb's, and recording one is the judge command's.
19
+ #
20
+ # The digest is of the value's JSON with object keys sorted, so a program
21
+ # that writes its keys in another order does not make a judgment stale,
22
+ # and with numbers written the way JavaScript writes them, so both engines
23
+ # give one value one digest.
24
+ module Nukadoko
25
+ module Engine
26
+ module Judgments
27
+ # The order a row's state is chosen in: a judge's no first, then what
28
+ # no judgment can fix, then what a new judgment fixes.
29
+ STATE_ORDER = %w[misses uncitable stale pending judged].freeze
30
+ # The states in the order the summary's title counts them.
31
+ TITLE_ORDER = %w[judged misses stale pending uncitable].freeze
32
+ ARIA_LABEL = "ARIA snapshot of"
33
+ RECORD_FIX = "a judge who did not write the change reads the value against the sentence, then records it with nuka judge and --meets or --misses, --by, and --because"
34
+ UNCITABLE_FIX = "read the value whole from a channel the step record holds (a response, a process's output, a file) and narrow it with view and at, " \
35
+ "or return the locator or page from observe so nukadoko takes its ARIA snapshot"
36
+ ENTRY_STRINGS = %w[criterion then value_digest because by run_id at].freeze
37
+
38
+ module_function
39
+
40
+ #: (untyped) -> String
41
+ def canonical_json(value)
42
+ case value
43
+ when Array then "[#{value.map { canonical_json(_1) }.join(",")}]"
44
+ when Hash
45
+ pairs = value.to_a.map { |key, item| [key.to_s, item] }.sort_by { _1[0].codepoints }
46
+ "{#{pairs.map { |key, item| "#{JSON.generate(key)}:#{canonical_json(item)}" }.join(",")}}"
47
+ when Float then value.finite? ? AriaSnapshot.js_number(value) : "null"
48
+ when Integer, String, true, false, nil then JSON.generate(value)
49
+ else JSON.generate(value.to_s)
50
+ end
51
+ end
52
+
53
+ #: (untyped) -> String
54
+ def value_digest(value) = "sha256:#{Digest::SHA256.hexdigest(canonical_json(value))}"
55
+
56
+ # Where a judged value was measured, as a read names it: the channel,
57
+ # with the step record that holds it when that is another step's,
58
+ # `aria-snapshot` for a tree nukadoko took, or nil.
59
+ #: (Hash[Symbol, untyped], String?) -> String?
60
+ def measured_of(claim, own_record_id)
61
+ return "aria-snapshot" if claim[:actual_from].to_s.start_with?(ARIA_LABEL)
62
+
63
+ measured = claim[:measured] or return nil
64
+ measured[:step_record_id] == own_record_id ? measured[:channel] : "#{measured[:channel]} of #{measured[:step_record_id]}"
65
+ end
66
+
67
+ # Every value the passing required head executions of `row` read for a
68
+ # judgment, once per Then, claim, and value.
69
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
70
+ def judged_values(row, scenarios)
71
+ required = row[:cells].select { _1[:world] == "head" && _1[:role] == "required" }.map { _1[:environment] }
72
+ values = [] #: Array[Hash[Symbol, untyped]]
73
+ scenarios.each do |scenario|
74
+ next if scenario[:world] || scenario[:feature] != row[:feature] || scenario[:line] != row[:line]
75
+ next if scenario[:role] == "control" || scenario[:status] != "passed" || !required.include?(scenario[:environment])
76
+
77
+ scenario[:steps].each do |step|
78
+ (step[:claims] || []).each_with_index do |claim, index|
79
+ next if claim[:each] || claim[:outcome] != "for-judgment" || claim[:value_digest].nil?
80
+
81
+ digest = claim[:value_digest]
82
+ next if values.any? { _1[:step] == step[:text] && _1[:claim] == index && _1[:value_digest] == digest }
83
+
84
+ values << { line: step[:line] || scenario[:line], step: step[:text], claim: index, environment: scenario[:environment],
85
+ quality: claim[:expected_text], value_from: claim[:actual_from], value_digest: digest,
86
+ measured: measured_of(claim, step[:step_record_id]), record: step[:record], value: claim[:actual] }
87
+ end
88
+ end
89
+ end
90
+ values
91
+ end
92
+
93
+ #: (Hash[Symbol, untyped], ?Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
94
+ def cited(entry, extra = {})
95
+ { value_digest: entry[:value_digest], verdict: entry[:verdict], by: entry[:by], because: entry[:because], run_id: entry[:run_id], at: entry[:at], **extra }
96
+ end
97
+
98
+ # Where one value stands against the judgments of its criterion: a
99
+ # value in no recorded channel is `uncitable` whatever a file says; an
100
+ # entry with its digest decides it; entries of the same claim that
101
+ # cite only values this run did not read make it `stale`, citing the
102
+ # latest of them; otherwise it is `pending`. `read_now` is every digest
103
+ # the run read for the same claim, so a value one environment reads
104
+ # beside a judged value another reads is a value of its own to judge.
105
+ #: (Hash[Symbol, untyped], String, Array[Hash[Symbol, untyped]], ?Array[String]?) -> Hash[Symbol, untyped]
106
+ def read_against(value, criterion, entries, read_now = nil)
107
+ read_now ||= [value[:value_digest]]
108
+ rest = value.except(:value)
109
+ return { **rest, state: "uncitable" } if value[:measured].nil?
110
+
111
+ same = entries.select { _1[:criterion] == criterion && _1[:then] == value[:step] && _1[:claim] == value[:claim] }
112
+ match = same.find { _1[:value_digest] == value[:value_digest] }
113
+ return { **rest, state: match[:verdict] == "meets" ? "judged" : "misses", judgment: cited(match) } if match
114
+ return { **rest, state: "pending" } if same.empty? || same.any? { read_now.include?(_1[:value_digest]) }
115
+
116
+ latest = same.sort_by { _1[:at].to_s }.last #: Hash[Symbol, untyped]
117
+ difference = ClaimLine.equal_difference(value[:value], latest[:value])
118
+ { **rest, state: "stale", judgment: cited(latest, { value: latest[:value], **(difference ? { difference: difference } : {}) }) }
119
+ end
120
+
121
+ #: (Array[Hash[Symbol, untyped]], String, Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
122
+ def read_each(values, criterion, entries)
123
+ values.map do |value|
124
+ now = values.select { _1[:step] == value[:step] && _1[:claim] == value[:claim] }.map { _1[:value_digest] }
125
+ read_against(value, criterion, entries, now)
126
+ end
127
+ end
128
+
129
+ # A value as a reason quotes it, cut at 120 characters of its JSON.
130
+ #: (untyped) -> String
131
+ def value_preview(value)
132
+ text = JSON.generate(value)
133
+ text.size > 120 ? "#{text[0, 117]}..." : text
134
+ end
135
+
136
+ #: (Hash[Symbol, untyped]) -> String
137
+ def quality(read) = read[:quality] || "what the sentence says"
138
+
139
+ #: (Hash[Symbol, untyped], untyped) -> String
140
+ def read_clause(read, value)
141
+ at = "line #{read[:line]}"
142
+ judgment = read[:judgment] || {}
143
+ case read[:state]
144
+ when "judged" then "#{judgment[:by]} judged the value #{at} read as #{quality(read)}: #{judgment[:because]}"
145
+ when "misses" then "#{judgment[:by]} judged the value #{at} read, #{value_preview(value)}, not #{quality(read)}: #{judgment[:because]}"
146
+ when "uncitable" then "#{at} asks for a judgment of #{read[:value_from]}, which no channel of a step record holds, so no judgment can cite it"
147
+ when "stale"
148
+ # Where the two differ says more than two cut previews of a long
149
+ # value, which can read the same.
150
+ if judgment[:difference].nil?
151
+ "#{at} read #{value_preview(value)}, and the judgment by #{judgment[:by]} in #{judgment[:run_id]} cited #{value_preview(judgment[:value])}, so it does not cover what ran"
152
+ else
153
+ "#{at} read another value than the one the judgment by #{judgment[:by]} in #{judgment[:run_id]} cited, so it does not cover what ran; " \
154
+ "#{judgment[:difference]} (actual is this run's value, expected the judged one)"
155
+ end
156
+ else "#{at} read #{value_preview(value)} for a judgment as #{quality(read)}, and no judgment cites it"
157
+ end
158
+ end
159
+
160
+ # The judgment of one row, or nil for a row that read no value for a
161
+ # judgment.
162
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]]) -> Hash[Symbol, untyped]?
163
+ def row_judgment(row, scenarios, entries)
164
+ values = judged_values(row, scenarios)
165
+ return nil if values.empty?
166
+
167
+ reads = read_each(values, row[:name], entries)
168
+ state = STATE_ORDER.find { |candidate| reads.any? { _1[:state] == candidate } }
169
+ shown = reads.each_with_index.filter_map { |read, index| read_clause(read, values.fetch(index)[:value]) if read[:state] == state }
170
+ { state: state, reason: shown.join("; "), reads: reads }
171
+ end
172
+
173
+ # The table's rows with a `judgment` on each row whose required head
174
+ # cells all passed and read a value for a judgment. The verdict is
175
+ # never touched.
176
+ #: (Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]], ^(String) -> Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
177
+ def with_judgments(rows, scenarios, entries_of)
178
+ rows.map do |row|
179
+ required = row[:cells].select { _1[:world] == "head" && _1[:role] == "required" }
180
+ next row if required.empty? || required.any? { _1[:outcome] != "pass" }
181
+
182
+ judgment = row_judgment(row, scenarios, entries_of.(row[:feature]))
183
+ judgment ? { **row, judgment: judgment } : row
184
+ end
185
+ end
186
+
187
+ #: (Array[Hash[Symbol, untyped]]) -> bool
188
+ def open?(rows) = rows.any? { _1[:judgment] && _1[:judgment][:state] != "judged" }
189
+
190
+ #: (Hash[Symbol, untyped], String) -> String
191
+ def show_command(row, run_id) = ["nuka", "judge", "#{row[:feature]}:#{row[:line]}", "--run", run_id].map { Selection.shell_word(_1) }.join(" ")
192
+
193
+ # One finding per row whose judgment leaves the gate red.
194
+ #: (Array[Hash[Symbol, untyped]], String) -> Array[Hash[Symbol, untyped]]
195
+ def findings(rows, run_id)
196
+ rows.flat_map do |row|
197
+ judgment = row[:judgment]
198
+ next [] if judgment.nil? || judgment[:state] == "judged"
199
+
200
+ base = { severity: "failed", code: "judgment-#{judgment[:state]}", file: row[:feature], line: row[:line], message: %(Scenario "#{row[:name]}" #{judgment[:reason]}) }
201
+ case judgment[:state]
202
+ when "uncitable" then [{ **base, fix: UNCITABLE_FIX }]
203
+ when "misses" then [{ **base, fix: "change the program until the value meets the sentence, then run it again and have it judged again", do: show_command(row, run_id) }]
204
+ else [{ **base, fix: RECORD_FIX, do: show_command(row, run_id) }]
205
+ end
206
+ end
207
+ end
208
+
209
+ # `entries` with `entry` in: it replaces an entry that judged the same
210
+ # value of the same claim, and is added beside the entries of others.
211
+ #: (Array[Hash[Symbol, untyped]], Hash[Symbol, untyped]) -> Array[Hash[Symbol, untyped]]
212
+ def add(entries, entry)
213
+ keys = %i[criterion then claim value_digest]
214
+ [*entries.reject { |other| keys.all? { other[_1] == entry[_1] } }, entry]
215
+ end
216
+
217
+ # A judgment file's text: its entries ordered by criterion, Then,
218
+ # claim, and time, so two judges who add one each make a diff of two
219
+ # hunks. Keys keep the order the TypeScript engine writes.
220
+ #: (Array[Hash[Symbol, untyped]]) -> String
221
+ def file_text(entries)
222
+ compare = ->(a, b) { a.to_s.codepoints <=> b.to_s.codepoints }
223
+ ordered = entries.sort do |a, b|
224
+ [compare.(a[:criterion], b[:criterion]), compare.(a[:then], b[:then]), a[:claim] <=> b[:claim], compare.(a[:at], b[:at]),
225
+ compare.(a[:value_digest], b[:value_digest])].find { _1 != 0 } || 0
226
+ end
227
+ "#{JSON.pretty_generate({ judgments: ordered })}\n"
228
+ end
229
+
230
+ # A judgment file's entries, or why its text holds none: a file a
231
+ # person edited by hand can break, and a judgment it no longer holds
232
+ # must read as unread, never as absent.
233
+ #: (String) -> Hash[Symbol, untyped]
234
+ def parse_file(text)
235
+ parsed = begin
236
+ JSON.parse(text)
237
+ rescue JSON::ParserError => e
238
+ return { problem: "it is not JSON: #{e.message.lines.first.to_s.chomp}" }
239
+ end
240
+ list = (parsed.is_a?(Hash) ? parsed["judgments"] : nil) #: untyped
241
+ return { problem: "it holds no judgments array" } unless list.is_a?(Array)
242
+
243
+ list.each_with_index do |entry, index|
244
+ return { problem: "judgments[#{index}] is not an object" } unless entry.is_a?(Hash)
245
+
246
+ missing = ENTRY_STRINGS.find { !entry[_1].is_a?(String) }
247
+ return { problem: "judgments[#{index}] has no text #{missing}" } if missing
248
+ unless entry["claim"].is_a?(Numeric) && %w[meets misses].include?(entry["verdict"])
249
+ return { problem: "judgments[#{index}] needs a claim number and a verdict of meets or misses" }
250
+ end
251
+ end
252
+ { entries: list.map { |entry| entry.to_h { |key, item| [key.to_sym, item] } } }
253
+ end
254
+ end
255
+ end
256
+ end