nukadoko 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +291 -0
  3. data/README.ja.md +58 -5
  4. data/README.md +15 -3
  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 +274 -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/claims.rb +407 -30
  15. data/lib/nukadoko/cli.rb +802 -19
  16. data/lib/nukadoko/codes.json +942 -0
  17. data/lib/nukadoko/codes.rb +19 -0
  18. data/lib/nukadoko/command.rb +214 -12
  19. data/lib/nukadoko/criteria.rb +172 -0
  20. data/lib/nukadoko/definition.rb +33 -41
  21. data/lib/nukadoko/env_check.rb +90 -0
  22. data/lib/nukadoko/features.rb +80 -10
  23. data/lib/nukadoko/fixtures.rb +296 -10
  24. data/lib/nukadoko/http_recording.rb +68 -0
  25. data/lib/nukadoko/init.rb +130 -0
  26. data/lib/nukadoko/judge.rb +221 -0
  27. data/lib/nukadoko/judgment_file.rb +78 -0
  28. data/lib/nukadoko/judgments.rb +256 -0
  29. data/lib/nukadoko/literal.rb +129 -0
  30. data/lib/nukadoko/measured.rb +105 -0
  31. data/lib/nukadoko/order.rb +84 -0
  32. data/lib/nukadoko/parts.rb +42 -21
  33. data/lib/nukadoko/pattern.rb +11 -1
  34. data/lib/nukadoko/poll.rb +17 -6
  35. data/lib/nukadoko/program.rb +105 -17
  36. data/lib/nukadoko/project_fixtures.rb +17 -2
  37. data/lib/nukadoko/report_allure.rb +751 -0
  38. data/lib/nukadoko/report_messages.rb +167 -0
  39. data/lib/nukadoko/report_run.rb +169 -0
  40. data/lib/nukadoko/run.rb +865 -88
  41. data/lib/nukadoko/run_record.rb +532 -0
  42. data/lib/nukadoko/scaffold.rb +1015 -0
  43. data/lib/nukadoko/schema.rb +61 -4
  44. data/lib/nukadoko/secrets.rb +359 -0
  45. data/lib/nukadoko/selection.rb +163 -0
  46. data/lib/nukadoko/signature.rb +4 -1
  47. data/lib/nukadoko/skill.rb +36 -0
  48. data/lib/nukadoko/step.rb +62 -21
  49. data/lib/nukadoko/summary.rb +662 -0
  50. data/lib/nukadoko/table.rb +34 -2
  51. data/lib/nukadoko/text_fit.rb +179 -0
  52. data/lib/nukadoko/trace.rb +244 -0
  53. data/lib/nukadoko/trace_actions.rb +116 -0
  54. data/lib/nukadoko/tree_passes.rb +119 -0
  55. data/lib/nukadoko/verdict.rb +128 -0
  56. data/lib/nukadoko/version.rb +2 -1
  57. data/lib/nukadoko/vocabulary.rb +6 -0
  58. data/lib/nukadoko/walk.rb +305 -0
  59. data/lib/nukadoko/walk_files.rb +473 -0
  60. data/lib/nukadoko/walk_step.rb +149 -0
  61. data/lib/nukadoko.rb +36 -0
  62. data/llms.txt +296 -0
  63. data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
  64. data/sig/generated/nukadoko/aria_tree.rbs +142 -0
  65. data/sig/generated/nukadoko/base.rbs +136 -0
  66. data/sig/generated/nukadoko/check.rbs +255 -0
  67. data/sig/generated/nukadoko/claim_check.rbs +118 -0
  68. data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
  69. data/sig/generated/nukadoko/claim_line.rbs +158 -0
  70. data/sig/generated/nukadoko/claim_rules.rbs +172 -0
  71. data/sig/generated/nukadoko/claims.rbs +165 -0
  72. data/sig/generated/nukadoko/cli.rbs +182 -0
  73. data/sig/generated/nukadoko/codes.rbs +16 -0
  74. data/sig/generated/nukadoko/command.rbs +109 -0
  75. data/sig/generated/nukadoko/criteria.rbs +83 -0
  76. data/sig/generated/nukadoko/definition.rbs +30 -0
  77. data/sig/generated/nukadoko/env_check.rbs +38 -0
  78. data/sig/generated/nukadoko/features.rbs +101 -0
  79. data/sig/generated/nukadoko/fixtures.rbs +152 -0
  80. data/sig/generated/nukadoko/http_recording.rbs +29 -0
  81. data/sig/generated/nukadoko/init.rbs +66 -0
  82. data/sig/generated/nukadoko/judge.rbs +64 -0
  83. data/sig/generated/nukadoko/judgment_file.rbs +39 -0
  84. data/sig/generated/nukadoko/judgments.rbs +120 -0
  85. data/sig/generated/nukadoko/literal.rbs +71 -0
  86. data/sig/generated/nukadoko/measured.rbs +42 -0
  87. data/sig/generated/nukadoko/order.rbs +54 -0
  88. data/sig/generated/nukadoko/parts.rbs +35 -0
  89. data/sig/generated/nukadoko/pattern.rbs +28 -0
  90. data/sig/generated/nukadoko/poll.rbs +40 -0
  91. data/sig/generated/nukadoko/program.rbs +101 -0
  92. data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
  93. data/sig/generated/nukadoko/report_allure.rbs +268 -0
  94. data/sig/generated/nukadoko/report_messages.rbs +68 -0
  95. data/sig/generated/nukadoko/report_run.rbs +102 -0
  96. data/sig/generated/nukadoko/run.rbs +257 -0
  97. data/sig/generated/nukadoko/run_record.rbs +174 -0
  98. data/sig/generated/nukadoko/scaffold.rbs +328 -0
  99. data/sig/generated/nukadoko/schema.rbs +137 -0
  100. data/sig/generated/nukadoko/secrets.rbs +157 -0
  101. data/sig/generated/nukadoko/selection.rbs +73 -0
  102. data/sig/generated/nukadoko/signature.rbs +17 -0
  103. data/sig/generated/nukadoko/skill.rbs +20 -0
  104. data/sig/generated/nukadoko/step.rbs +115 -0
  105. data/sig/generated/nukadoko/summary.rbs +204 -0
  106. data/sig/generated/nukadoko/table.rbs +43 -0
  107. data/sig/generated/nukadoko/text_fit.rbs +70 -0
  108. data/sig/generated/nukadoko/trace.rbs +127 -0
  109. data/sig/generated/nukadoko/trace_actions.rbs +40 -0
  110. data/sig/generated/nukadoko/tree_passes.rbs +48 -0
  111. data/sig/generated/nukadoko/verdict.rbs +56 -0
  112. data/sig/generated/nukadoko/version.rbs +5 -0
  113. data/sig/generated/nukadoko/vocabulary.rbs +25 -0
  114. data/sig/generated/nukadoko/walk.rbs +139 -0
  115. data/sig/generated/nukadoko/walk_files.rbs +189 -0
  116. data/sig/generated/nukadoko/walk_step.rbs +50 -0
  117. data/sig/generated/nukadoko.rbs +15 -0
  118. data/sig/nukadoko.rbs +5 -48
  119. data/skills/nukadoko/SKILL.md +120 -0
  120. data/skills/nukadoko/references/checking-steps.md +200 -0
  121. data/skills/nukadoko/references/diagnosing.md +200 -0
  122. data/skills/nukadoko/references/done.md +99 -0
  123. data/skills/nukadoko/references/evidence.md +124 -0
  124. data/skills/nukadoko/references/examples/cli.md +230 -0
  125. data/skills/nukadoko/references/examples/http-api.md +339 -0
  126. data/skills/nukadoko/references/examples/workflow.md +173 -0
  127. data/skills/nukadoko/references/exploring.md +184 -0
  128. data/skills/nukadoko/references/fixtures.md +198 -0
  129. data/skills/nukadoko/references/from-prose.md +51 -0
  130. data/skills/nukadoko/references/judgments.md +95 -0
  131. data/skills/nukadoko/references/maintenance.md +97 -0
  132. data/skills/nukadoko/references/merge-risk.md +74 -0
  133. data/skills/nukadoko/references/parts.md +152 -0
  134. data/skills/nukadoko/references/perturbed-worlds.md +107 -0
  135. data/skills/nukadoko/references/program.md +200 -0
  136. data/skills/nukadoko/references/reviewing.md +200 -0
  137. data/skills/nukadoko/references/running.md +200 -0
  138. data/skills/nukadoko/references/scenario-rules.md +188 -0
  139. data/skills/nukadoko/references/states.md +66 -0
  140. data/skills/nukadoko/references/step-files.md +200 -0
  141. data/skills/nukadoko/references/verdicts.md +200 -0
  142. data/skills/nukadoko/references/writing-steps.md +200 -0
  143. data/skills/nukadoko/references/writing-the-feature.md +165 -0
  144. data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
  145. data/skills/nukadoko/scripts/merge-risk.jq +56 -0
  146. data/skills/nukadoko-verification/SKILL.md +263 -0
  147. metadata +121 -1
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "fileutils"
5
+
6
+ # `nuka init`: the config file, the steps directory, and the .gitignore
7
+ # entries, in the TypeScript engine's order and words. A config file
8
+ # already there refuses the whole command before anything is written, so a
9
+ # half-done init never looks like a finished one. It ends with a
10
+ # self-check that loads the config and the steps as `nuka steps` does; a
11
+ # failure there is reported and the files stay, so what init wrote can be
12
+ # looked at and fixed.
13
+ #
14
+ # The TypeScript engine also adds a package.json script and tsconfig
15
+ # entries. A Ruby project's one command for people and CI is a rake task,
16
+ # so a Rakefile with no `scenarios` task gets one that runs `nuka run`; a
17
+ # project with no Rakefile gets none, since a Rakefile written only for
18
+ # this would be a second place to look. The Gemfile is read too: one that
19
+ # does not name nukadoko gets a line on stderr.
20
+ module Nukadoko
21
+ module Engine
22
+ class Init
23
+ CONFIG = "nukadoko.config.rb"
24
+ STEPS = File.join("features", "steps")
25
+ # `.env` files are ignored since a committed one is configuration, not
26
+ # a secret source; `!.env.example` must follow `.env.*` to re-include it.
27
+ IGNORED = [".nukadoko/", ".env", ".env.*", "!.env.example"].freeze
28
+ # The gem's copy is staged at release; a checkout has only the root's.
29
+ STAGED_LLMS = File.expand_path("../../llms.txt", __dir__.to_s)
30
+ LLMS = File.file?(STAGED_LLMS) ? STAGED_LLMS : File.expand_path("../../../llms.txt", __dir__.to_s)
31
+ # The gem ships no docs/, so the config file points at the spec online.
32
+ SPEC = "https://github.com/meganemura/nukadoko/blob/main/docs/spec.md"
33
+ TASK = <<~RUBY
34
+ desc "Run the acceptance scenarios: nuka check, then every scenario under features/"
35
+ task :scenarios do
36
+ sh "bundle exec nuka run"
37
+ end
38
+ RUBY
39
+
40
+ #: (String, out: IO, err: IO) -> void
41
+ def initialize(project, out:, err:)
42
+ @project = project
43
+ @out = out
44
+ @err = err
45
+ end
46
+
47
+ # The exit code, or a Refusal when a config file exists.
48
+ #: () -> (Integer | Refusal)
49
+ def call
50
+ config = File.join(@project, CONFIG)
51
+ if File.exist?(config)
52
+ return Refusal.new("config-exists", "Refusing to init: #{config} already exists; nothing was written", run: "nuka check")
53
+ end
54
+
55
+ File.write(config, <<~RUBY)
56
+ # Every config key nukadoko reads, one line each: #{SPEC}, "Environments and secrets"
57
+ Nukadoko.config
58
+ RUBY
59
+ @out.puts CONFIG
60
+ FileUtils.mkdir_p(File.join(@project, STEPS))
61
+ @out.puts STEPS
62
+ @out.puts ".gitignore" if ignore_entries
63
+ @out.puts "Rakefile" if scenarios_task
64
+ gemfile_note
65
+ self_check
66
+ end
67
+
68
+ private
69
+
70
+ # Appends each entry no line already ignores; true when it wrote.
71
+ #: () -> bool
72
+ def ignore_entries
73
+ path = File.join(@project, ".gitignore")
74
+ existing = File.exist?(path) ? File.read(path) : ""
75
+ lines = existing.split(/\r?\n/)
76
+ missing = IGNORED.reject { |entry| lines.any? { covers?(_1, entry) } }
77
+ return false if missing.empty?
78
+
79
+ prefix = existing.empty? || existing.end_with?("\n") ? existing : "#{existing}\n"
80
+ File.write(path, "#{prefix}#{missing.join("\n")}\n")
81
+ true
82
+ end
83
+
84
+ # Appends the `scenarios` task to a Rakefile that has none; true when it wrote.
85
+ #: () -> bool
86
+ def scenarios_task
87
+ path = File.join(@project, "Rakefile")
88
+ return false unless File.exist?(path)
89
+
90
+ existing = File.read(path)
91
+ return false if existing.match?(/^\s*task\s*\(?\s*(:scenarios|["']scenarios["'])/)
92
+
93
+ prefix = existing.empty? || existing.end_with?("\n") ? existing : "#{existing}\n"
94
+ File.write(path, "#{prefix}#{"\n" unless prefix.empty?}#{TASK}")
95
+ true
96
+ end
97
+
98
+ # A root directory entry is also ignored by `/name`, `name`, or `name/**`.
99
+ #: (String, String) -> bool
100
+ def covers?(line, entry)
101
+ trimmed = line.strip
102
+ return true if trimmed == entry
103
+ return false unless entry.end_with?("/")
104
+
105
+ trimmed.delete_prefix("/").sub(%r{/(\*\*)?\z}, "") == entry.delete_suffix("/")
106
+ end
107
+
108
+ #: () -> void
109
+ def gemfile_note
110
+ gemfile = File.join(@project, "Gemfile")
111
+ return unless File.exist?(gemfile)
112
+ return if File.read(gemfile).match?(/^\s*gem\s+["']nukadoko["']/)
113
+
114
+ @err.puts %(note: the Gemfile names no nukadoko gem; add gem "nukadoko", group: :test so bundle exec nuka runs this version)
115
+ end
116
+
117
+ #: () -> Integer
118
+ def self_check
119
+ load File.join(@project, CONFIG)
120
+ Nukadoko.load_steps(File.join(@project, STEPS))
121
+ @err.puts "next: read #{LLMS}, written for an agent: the loop, every command, and every finding code; its Ruby section is how a Ruby step is written"
122
+ 0
123
+ rescue StandardError, ScriptError => e
124
+ @err.puts ["error", "unexpected-error", "-", "Self-check failed: #{e.message}"].join("\t")
125
+ @err.puts " do: nuka check"
126
+ 1
127
+ end
128
+ end
129
+ end
130
+ end
@@ -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