nukadoko 0.5.0 → 0.7.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 (203) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +553 -3
  3. data/README.ja.md +22 -6
  4. data/README.md +13 -7
  5. data/docs/ci.ja.md +542 -0
  6. data/docs/ci.md +635 -0
  7. data/docs/commands.ja.md +1524 -0
  8. data/docs/commands.md +1281 -0
  9. data/docs/engines.json +19 -0
  10. data/docs/execution-events.ja.md +336 -0
  11. data/docs/execution-events.md +423 -0
  12. data/docs/glossary.json +1348 -0
  13. data/docs/guide.ja.md +733 -0
  14. data/docs/guide.md +815 -0
  15. data/docs/intent.ja.md +133 -0
  16. data/docs/intent.md +156 -0
  17. data/docs/layout.ja.md +442 -0
  18. data/docs/layout.md +399 -0
  19. data/docs/releasing.ja.md +186 -0
  20. data/docs/releasing.md +237 -0
  21. data/docs/ruby/reference.ja.md +302 -0
  22. data/docs/ruby/reference.md +368 -0
  23. data/docs/spec.ja.md +8916 -0
  24. data/docs/spec.md +11250 -0
  25. data/docs/tutorial.ja.md +451 -0
  26. data/docs/tutorial.md +458 -0
  27. data/docs/typescript/adopting.ja.md +231 -0
  28. data/docs/typescript/adopting.md +251 -0
  29. data/docs/typescript/doors.ja.md +301 -0
  30. data/docs/typescript/doors.md +329 -0
  31. data/docs/typescript/migration-playwright-test.ja.md +189 -0
  32. data/docs/typescript/migration-playwright-test.md +212 -0
  33. data/docs/typescript/migration.ja.md +391 -0
  34. data/docs/typescript/migration.md +505 -0
  35. data/docs/typescript/reference.ja.md +176 -0
  36. data/docs/typescript/reference.md +197 -0
  37. data/docs/typescript/upgrading.ja.md +1774 -0
  38. data/docs/typescript/upgrading.md +2224 -0
  39. data/docs/web-app.ja.md +627 -0
  40. data/docs/web-app.md +659 -0
  41. data/docs/writing-scenarios.ja.md +544 -0
  42. data/docs/writing-scenarios.md +563 -0
  43. data/lib/nukadoko/base.rb +12 -5
  44. data/lib/nukadoko/changed_files.rb +95 -0
  45. data/lib/nukadoko/check.rb +459 -51
  46. data/lib/nukadoko/claim_drops.rb +271 -0
  47. data/lib/nukadoko/claim_line.rb +24 -4
  48. data/lib/nukadoko/claim_rules.rb +143 -1
  49. data/lib/nukadoko/claim_sides.rb +157 -9
  50. data/lib/nukadoko/cli.rb +403 -50
  51. data/lib/nukadoko/codes.json +246 -21
  52. data/lib/nukadoko/codes.rb +21 -4
  53. data/lib/nukadoko/command.rb +120 -13
  54. data/lib/nukadoko/command_help.rb +128 -0
  55. data/lib/nukadoko/compat.rb +469 -0
  56. data/lib/nukadoko/criteria.rb +1 -1
  57. data/lib/nukadoko/definition.rb +157 -7
  58. data/lib/nukadoko/error_frames.rb +6 -3
  59. data/lib/nukadoko/events/count.rb +146 -0
  60. data/lib/nukadoko/events/counted.rb +114 -0
  61. data/lib/nukadoko/events/format.rb +257 -0
  62. data/lib/nukadoko/events/lists.rb +154 -0
  63. data/lib/nukadoko/events/path.rb +285 -0
  64. data/lib/nukadoko/events/read.rb +284 -0
  65. data/lib/nukadoko/events/reader.rb +490 -0
  66. data/lib/nukadoko/events/selection.rb +252 -0
  67. data/lib/nukadoko/events/tally.rb +438 -0
  68. data/lib/nukadoko/features.rb +2 -2
  69. data/lib/nukadoko/fixtures.rb +160 -35
  70. data/lib/nukadoko/http_recording.rb +6 -2
  71. data/lib/nukadoko/intent.rb +364 -0
  72. data/lib/nukadoko/intent_file.rb +81 -0
  73. data/lib/nukadoko/intent_view.rb +190 -0
  74. data/lib/nukadoko/js_json.rb +2 -1
  75. data/lib/nukadoko/judge.rb +2 -2
  76. data/lib/nukadoko/judgment_file.rb +22 -1
  77. data/lib/nukadoko/judgments.rb +33 -3
  78. data/lib/nukadoko/measure_mcp.rb +202 -0
  79. data/lib/nukadoko/parts.rb +8 -2
  80. data/lib/nukadoko/path_view.rb +252 -0
  81. data/lib/nukadoko/place_rows.rb +161 -0
  82. data/lib/nukadoko/program.rb +1265 -51
  83. data/lib/nukadoko/program_log.rb +122 -0
  84. data/lib/nukadoko/program_shape.rb +385 -0
  85. data/lib/nukadoko/project_fixtures.rb +43 -10
  86. data/lib/nukadoko/project_program.rb +5 -4
  87. data/lib/nukadoko/repeated_prefix.rb +310 -0
  88. data/lib/nukadoko/report_run.rb +3 -3
  89. data/lib/nukadoko/run.rb +1156 -159
  90. data/lib/nukadoko/run_record.rb +70 -46
  91. data/lib/nukadoko/scaffold.rb +24 -6
  92. data/lib/nukadoko/schema.rb +15 -2
  93. data/lib/nukadoko/secrets.rb +55 -5
  94. data/lib/nukadoko/states.rb +478 -0
  95. data/lib/nukadoko/step.rb +71 -2
  96. data/lib/nukadoko/step_fixtures.rb +52 -3
  97. data/lib/nukadoko/summary.rb +803 -30
  98. data/lib/nukadoko/table.rb +4 -2
  99. data/lib/nukadoko/text_fit.rb +20 -7
  100. data/lib/nukadoko/trace.rb +152 -45
  101. data/lib/nukadoko/tree_passes.rb +95 -33
  102. data/lib/nukadoko/underneath.rb +311 -0
  103. data/lib/nukadoko/verdict.rb +18 -1
  104. data/lib/nukadoko/version.rb +1 -1
  105. data/lib/nukadoko/vocabulary.rb +166 -23
  106. data/lib/nukadoko/walk.rb +2 -2
  107. data/lib/nukadoko/walk_step.rb +1 -1
  108. data/lib/nukadoko.rb +23 -0
  109. data/llms.txt +42 -35
  110. data/sig/generated/nukadoko/base.rbs +4 -4
  111. data/sig/generated/nukadoko/changed_files.rbs +42 -0
  112. data/sig/generated/nukadoko/check.rbs +147 -10
  113. data/sig/generated/nukadoko/claim_drops.rbs +135 -0
  114. data/sig/generated/nukadoko/claim_line.rbs +11 -0
  115. data/sig/generated/nukadoko/claim_rules.rbs +49 -4
  116. data/sig/generated/nukadoko/claim_sides.rbs +63 -12
  117. data/sig/generated/nukadoko/cli.rbs +80 -7
  118. data/sig/generated/nukadoko/codes.rbs +19 -4
  119. data/sig/generated/nukadoko/command.rbs +68 -1
  120. data/sig/generated/nukadoko/command_help.rbs +18 -0
  121. data/sig/generated/nukadoko/compat.rbs +392 -0
  122. data/sig/generated/nukadoko/criteria.rbs +1 -1
  123. data/sig/generated/nukadoko/definition.rbs +50 -0
  124. data/sig/generated/nukadoko/error_frames.rbs +1 -1
  125. data/sig/generated/nukadoko/events/count.rbs +69 -0
  126. data/sig/generated/nukadoko/events/counted.rbs +70 -0
  127. data/sig/generated/nukadoko/events/format.rbs +163 -0
  128. data/sig/generated/nukadoko/events/lists.rbs +47 -0
  129. data/sig/generated/nukadoko/events/path.rbs +161 -0
  130. data/sig/generated/nukadoko/events/read.rbs +109 -0
  131. data/sig/generated/nukadoko/events/reader.rbs +226 -0
  132. data/sig/generated/nukadoko/events/selection.rbs +100 -0
  133. data/sig/generated/nukadoko/events/tally.rbs +227 -0
  134. data/sig/generated/nukadoko/features.rbs +6 -4
  135. data/sig/generated/nukadoko/fixtures.rbs +67 -11
  136. data/sig/generated/nukadoko/http_recording.rbs +1 -1
  137. data/sig/generated/nukadoko/intent.rbs +132 -0
  138. data/sig/generated/nukadoko/intent_file.rbs +46 -0
  139. data/sig/generated/nukadoko/intent_view.rbs +69 -0
  140. data/sig/generated/nukadoko/judgment_file.rbs +7 -1
  141. data/sig/generated/nukadoko/judgments.rbs +10 -0
  142. data/sig/generated/nukadoko/measure_mcp.rbs +104 -0
  143. data/sig/generated/nukadoko/parts.rbs +2 -2
  144. data/sig/generated/nukadoko/path_view.rbs +105 -0
  145. data/sig/generated/nukadoko/place_rows.rbs +43 -0
  146. data/sig/generated/nukadoko/program.rbs +627 -23
  147. data/sig/generated/nukadoko/program_log.rbs +67 -0
  148. data/sig/generated/nukadoko/program_shape.rbs +147 -0
  149. data/sig/generated/nukadoko/project_fixtures.rbs +16 -3
  150. data/sig/generated/nukadoko/project_program.rbs +2 -1
  151. data/sig/generated/nukadoko/repeated_prefix.rbs +172 -0
  152. data/sig/generated/nukadoko/report_run.rbs +1 -1
  153. data/sig/generated/nukadoko/run.rbs +286 -41
  154. data/sig/generated/nukadoko/run_record.rbs +20 -12
  155. data/sig/generated/nukadoko/scaffold.rbs +3 -0
  156. data/sig/generated/nukadoko/schema.rbs +5 -0
  157. data/sig/generated/nukadoko/secrets.rbs +26 -3
  158. data/sig/generated/nukadoko/states.rbs +214 -0
  159. data/sig/generated/nukadoko/step.rbs +58 -1
  160. data/sig/generated/nukadoko/step_fixtures.rbs +39 -0
  161. data/sig/generated/nukadoko/summary.rbs +256 -2
  162. data/sig/generated/nukadoko/table.rbs +3 -2
  163. data/sig/generated/nukadoko/trace.rbs +108 -33
  164. data/sig/generated/nukadoko/tree_passes.rbs +36 -8
  165. data/sig/generated/nukadoko/underneath.rbs +141 -0
  166. data/sig/generated/nukadoko/verdict.rbs +8 -0
  167. data/sig/generated/nukadoko/vocabulary.rbs +45 -6
  168. data/skills/nukadoko/SKILL.md +18 -18
  169. data/skills/nukadoko/references/checking-steps.md +16 -83
  170. data/skills/nukadoko/references/diagnosing.md +6 -6
  171. data/skills/nukadoko/references/done.md +22 -0
  172. data/skills/nukadoko/references/evidence.md +19 -6
  173. data/skills/nukadoko/references/exploring.md +22 -7
  174. data/skills/nukadoko/references/fixtures.md +12 -32
  175. data/skills/nukadoko/references/judgments.md +18 -0
  176. data/skills/nukadoko/references/maintenance.md +1 -1
  177. data/skills/nukadoko/references/merge-risk.md +1 -1
  178. data/skills/nukadoko/references/parts.md +46 -23
  179. data/skills/nukadoko/references/{perturbed-worlds.md → perturbed-systems.md} +30 -12
  180. data/skills/nukadoko/references/program.md +168 -169
  181. data/skills/nukadoko/references/reviewing.md +20 -20
  182. data/skills/nukadoko/references/ruby/checking-steps.md +106 -0
  183. data/skills/nukadoko/references/ruby/examples/cli.md +212 -0
  184. data/skills/nukadoko/references/ruby/fixtures.md +34 -0
  185. data/skills/nukadoko/references/ruby/program.md +112 -0
  186. data/skills/nukadoko/references/ruby/step-files.md +115 -0
  187. data/skills/nukadoko/references/running.md +34 -34
  188. data/skills/nukadoko/references/scenario-rules.md +7 -6
  189. data/skills/nukadoko/references/states.md +7 -1
  190. data/skills/nukadoko/references/step-files.md +41 -120
  191. data/skills/nukadoko/references/typescript/checking-steps.md +129 -0
  192. data/skills/nukadoko/references/{examples → typescript/examples}/cli.md +1 -1
  193. data/skills/nukadoko/references/{examples → typescript/examples}/http-api.md +6 -6
  194. data/skills/nukadoko/references/{examples → typescript/examples}/workflow.md +1 -1
  195. data/skills/nukadoko/references/typescript/fixtures.md +56 -0
  196. data/skills/nukadoko/references/typescript/program.md +111 -0
  197. data/skills/nukadoko/references/typescript/step-files.md +104 -0
  198. data/skills/nukadoko/references/verdicts.md +10 -10
  199. data/skills/nukadoko/references/writing-steps.md +67 -67
  200. data/skills/nukadoko/references/writing-the-feature.md +1 -1
  201. data/skills/nukadoko-verification/SKILL.md +13 -8
  202. metadata +103 -7
  203. data/skills/nukadoko/references/ruby.md +0 -80
@@ -36,8 +36,9 @@ module Nukadoko
36
36
  end
37
37
 
38
38
  # What a cell's executions came to. An execution is `[:pass]`,
39
- # `[:fail, line, claim]`, `[:broke, line]`, or `[:stopped, line]`
40
- # for a walk a person stopped after that line.
39
+ # `[:fail, line, claim]`, `[:broke, line]` (with a reason when one is
40
+ # known, such as a claim judged before its events had arrived), or
41
+ # `[:stopped, line]` for a walk a person stopped after that line.
41
42
  #: (Array[Array[untyped]]) -> Hash[Symbol, untyped]
42
43
  def self.cell(executions)
43
44
  first = executions.first
@@ -47,6 +48,7 @@ module Nukadoko
47
48
  line = executions.find { _1.first != :pass }&.at(1)
48
49
  { outcome: "flaky", line: line }
49
50
  elsif first.first == :pass then { outcome: "pass" }
51
+ elsif first.first == :broke && first[2].is_a?(String) then { outcome: "broke", line: first[1], reason: first[2] }
50
52
  else { outcome: first.first.to_s, line: first[1] }
51
53
  end.merge(runs: executions.size)
52
54
  end
@@ -147,8 +147,12 @@ module Nukadoko
147
147
 
148
148
  #: (Array[String], Array[Fixed?], Array[Integer?]) -> Hash[Symbol, untyped]
149
149
  def names_fit(have, want, taken)
150
+ # The first reading of a name, and the first of a text, each with
151
+ # how many readings came before it, so a reading that disagrees
152
+ # with two of them names the one that was read first.
150
153
  by_name = {}
151
154
  by_text = {}
155
+ read = 0
152
156
  want.each_with_index do |line, index|
153
157
  next if line.nil? || line.names.empty?
154
158
 
@@ -157,13 +161,22 @@ module Nukadoko
157
161
  parts_fit(line.parts, have[at].to_s, reads)
158
162
  line.names.each do |place, name|
159
163
  reading = { name: name, text: reads[place], at: at + 1, line: have[at] }
160
- before = by_name[name] || by_text[reading[:text]]
161
- if before.nil?
162
- by_name[name] = reading
163
- by_text[reading[:text]] = reading
164
- elsif before[:name] != name || before[:text] != reading[:text]
165
- return { fits: false, readings: [before, reading] }
166
- end
164
+ same_name = by_name[name]
165
+ same_text = by_text[reading[:text]]
166
+ # One reading can break the text against two earlier ones at
167
+ # once: its own name has read another text, and another name
168
+ # has read this text. The earlier of the two is the one named,
169
+ # so the pair a reader is given is the first two readings that
170
+ # break the text, whichever rule each of them breaks.
171
+ broken = [same_name, same_text].compact.select { _1[:reading][:name] != name || _1[:reading][:text] != reading[:text] }
172
+ before = broken.min_by { _1[:read] }
173
+ return { fits: false, readings: [before[:reading], reading] } if before
174
+
175
+ # Each hash keeps the first reading of a name and the first of
176
+ # a text: a later one that agrees with it says nothing new.
177
+ by_name[name] = { reading: reading, read: read } if same_name.nil?
178
+ by_text[reading[:text]] = { reading: reading, read: read } if same_text.nil?
179
+ read += 1
167
180
  end
168
181
  end
169
182
  { fits: true }
@@ -4,14 +4,16 @@
4
4
  require "securerandom"
5
5
  require "uri"
6
6
 
7
- # The W3C trace context the engine puts on what a step sends to the world,
8
- # and the program's log read back by it, as the TypeScript engine does it.
9
- # Each step gets one trace id; each request to the world's origin, and
10
- # each process run_command starts, gets a span id of its own under it. A
11
- # line of program.log belongs to the step whose trace id it carries,
12
- # wherever it appears: every trace id of one world starts with the same
13
- # eight characters, so the id a line carries is the first place where
14
- # those begin 32 hexadecimal ones, and no list of ids is needed to read it.
7
+ # The W3C trace context the engine puts on what a step sends to the system,
8
+ # and each service's log read back by it, as the TypeScript engine does it.
9
+ # Each step gets one trace id; each request to the origin of a service of
10
+ # the system, and each process run_command starts, gets a span id of its
11
+ # own under it. A line of a service's program.log belongs to the step whose
12
+ # trace id it carries, wherever it appears, a line a service wrote for a
13
+ # request another service passed on included: every trace id of one system
14
+ # starts with the same eight characters, so the id a line carries is the
15
+ # first place where those begin 32 hexadecimal ones, and no list of ids is
16
+ # needed to read it.
15
17
  module Nukadoko
16
18
  module Engine
17
19
  module Trace
@@ -42,7 +44,7 @@ module Nukadoko
42
44
  #: (String, String) -> String
43
45
  def self.traceparent(trace_id, span) = "00-#{trace_id}-#{span}-01"
44
46
 
45
- # The trace id of the world `prefix` names that `line` carries, or nil.
47
+ # The trace id of the system `prefix` names that `line` carries, or nil.
46
48
  #: (String, String) -> String?
47
49
  def self.id_in(line, prefix)
48
50
  text = line.downcase
@@ -61,25 +63,32 @@ module Nukadoko
61
63
  #: (URI::Generic) -> String
62
64
  def self.origin(uri) = "#{uri.scheme}://#{uri.host}#{":#{uri.port}" unless uri.port == uri.default_port}"
63
65
 
64
- # Only the world's origin gets the header: a third party's server may
65
- # answer a header it did not expect differently. A world at localhost
66
- # also answers at each name below localhost on the same port.
66
+ # Only the origins of the system's own services get the header: a
67
+ # third party's server, a service with `external: true` among them,
68
+ # may answer a header it did not expect differently. A service at
69
+ # localhost also answers at each name below localhost on the same port.
67
70
  #: (untyped, untyped) -> bool
68
- def self.stamps?(world, target)
71
+ def self.stamps?(system, target)
69
72
  target = URI(target.to_s)
70
- world = URI(world.to_s)
71
- return true if origin(target) == origin(world)
73
+ system = URI(system.to_s)
74
+ return true if origin(target) == origin(system)
72
75
 
73
- world.host == "localhost" && target.host.to_s.end_with?(".localhost") && target.scheme == world.scheme && target.port == world.port
76
+ system.host == "localhost" && target.host.to_s.end_with?(".localhost") && target.scheme == system.scheme && target.port == system.port
74
77
  rescue URI::Error
75
78
  false
76
79
  end
77
80
 
81
+ # The service of the system a request to `target` reaches: the first
82
+ # of `origins` (System#trace_origins) whose URL `stamps?` accepts, or
83
+ # nil for a request to no service's origin.
84
+ #: (Array[Hash[Symbol, untyped]], untyped) -> Hash[Symbol, untyped]?
85
+ def self.service_at(origins, target) = origins.find { stamps?(_1[:url], target) }
86
+
78
87
  # The step that runs now, kept per thread as the processes are.
79
88
  #: () -> Boundary?
80
89
  def self.current = Thread.current[:nukadoko_trace]
81
90
 
82
- #: [T] (Boundary) { () -> T } -> T
91
+ #: [T] (Boundary?) { () -> T } -> T
83
92
  def self.within(boundary)
84
93
  Thread.current[:nukadoko_trace] = boundary
85
94
  yield
@@ -87,35 +96,98 @@ module Nukadoko
87
96
  Thread.current[:nukadoko_trace] = nil
88
97
  end
89
98
 
90
- # One step's trace: what it stamped, and when it last did.
99
+ # One step's trace: what it stamped, at which service, and when it
100
+ # last did.
91
101
  class Boundary
92
102
  attr_reader :trace_id #: String
93
- attr_reader :origin #: String
103
+ attr_reader :origins #: Array[Hash[Symbol, untyped]]
94
104
  attr_reader :stamped #: Integer
95
- attr_reader :spans #: Array[String]
105
+ attr_reader :stamped_by #: Hash[String, Integer]
106
+ # The span ids of the requests the step stamped, by the service each
107
+ # went to, in the order sent: each service's events file reports the
108
+ # requests it served, so a request with no line is told from one
109
+ # another service has yet to report.
110
+ attr_reader :spans #: Hash[String, Array[String]]
111
+ # Stamped requests past MAX_SPANS, whose span ids are not kept, by
112
+ # service.
113
+ attr_reader :spans_not_kept #: Hash[String, Integer]
96
114
  attr_reader :process_spans #: Array[String]
115
+ attr_reader :mcp_spans #: Array[String]
97
116
  attr_reader :last_stamp_at #: Float?
98
117
 
99
- #: (String, String) -> void
100
- def initialize(trace_id, origin)
118
+ # `origins` are the system's services with a URL, each with whether a
119
+ # request there gets the step's traceparent.
120
+ #: (String, Array[Hash[Symbol, untyped]]) -> void
121
+ def initialize(trace_id, origins)
101
122
  @trace_id = trace_id
102
- @origin = origin
123
+ @origins = origins
103
124
  @stamped = 0
104
- @spans = []
125
+ @stamped_by = {}
126
+ @spans = {}
127
+ @spans_not_kept = {}
128
+ @spans_kept = 0
105
129
  @process_spans = []
130
+ @mcp_spans = []
106
131
  @last_stamp_at = nil
107
132
  end
108
133
 
134
+ # The service a request to `url` gets the step's traceparent at, or
135
+ # nil when it is no stamped service's.
136
+ #: (untyped) -> Hash[Symbol, untyped]?
137
+ def stamped_at(url)
138
+ at = Trace.service_at(@origins, url)
139
+ at if at && at[:stamped]
140
+ end
141
+
142
+ #: (untyped) -> bool
143
+ def stamps?(url) = !stamped_at(url).nil?
144
+
145
+ # The name of the service of the system a request to `url` reaches,
146
+ # stamped or not, or nil when it is no service's origin: what an
147
+ # http.jsonl line names.
148
+ #: (untyped) -> String?
149
+ def service_at(url) = Trace.service_at(@origins, url)&.dig(:service)&.to_s
150
+
151
+ # The URL of each service a request to gets the traceparent.
152
+ #: () -> Array[String]
153
+ def stamped_urls = @origins.select { _1[:stamped] }.map { _1[:url].to_s }
154
+
155
+ # The traceparent an MCP call carries in `params._meta`, with a
156
+ # span of its own.
157
+ #: () -> String
158
+ def mcp_traceparent
159
+ span = Trace.new_span
160
+ @last_stamp_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
161
+ @mcp_spans << span if @mcp_spans.size < MAX_SPANS
162
+ Trace.traceparent(@trace_id, span)
163
+ end
164
+
165
+ # An MCP call's HTTP request to a stamped service goes out with the
166
+ # call's traceparent, and counts as one stamped request with the
167
+ # call's span however many requests its send makes.
168
+ #: (Hash[Symbol, untyped], String) -> void
169
+ def mcp_sent(call, url)
170
+ at = stamped_at(url) or return
171
+ origin = Trace.origin(URI(url))
172
+ sent_to = call[:sent_to] #: Array[String]
173
+ sent_to << origin unless sent_to.include?(origin)
174
+ @last_stamp_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
175
+ return if call[:counted]
176
+
177
+ call[:counted] = true
178
+ count(at[:service].to_s)
179
+ keep_span(at[:service].to_s, call[:span])
180
+ end
181
+
109
182
  # The traceparent a request to `url` goes out with, or nil when it
110
- # is not the world's.
183
+ # is no stamped service's.
111
184
  #: (untyped) -> String?
112
185
  def stamp(url)
113
- return nil unless Trace.stamps?(@origin, url)
186
+ at = stamped_at(url) or return nil
114
187
 
115
188
  span = Trace.new_span
116
- @stamped += 1
117
- @last_stamp_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
118
- @spans << span if @spans.size < MAX_SPANS
189
+ count(at[:service].to_s)
190
+ keep_span(at[:service].to_s, span)
119
191
  Trace.traceparent(@trace_id, span)
120
192
  end
121
193
 
@@ -128,16 +200,38 @@ module Nukadoko
128
200
  @process_spans << span if @process_spans.size < MAX_SPANS
129
201
  Trace.traceparent(@trace_id, span)
130
202
  end
203
+
204
+ private
205
+
206
+ #: (String) -> void
207
+ def count(service)
208
+ @stamped += 1
209
+ @stamped_by[service] = @stamped_by.fetch(service, 0) + 1
210
+ @last_stamp_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
211
+ end
212
+
213
+ # The span id of a request to `service`, under one cap over every
214
+ # service: a step that sends more requests than the cap is rare, and
215
+ # its events count the rest.
216
+ #: (String, String) -> void
217
+ def keep_span(service, span)
218
+ if @spans_kept < MAX_SPANS
219
+ (@spans[service] ||= []) << span
220
+ @spans_kept += 1
221
+ else
222
+ @spans_not_kept[service] = @spans_not_kept.fetch(service, 0) + 1
223
+ end
224
+ end
131
225
  end
132
226
 
133
- # program.log read forward as the steps run: each step takes the
134
- # complete lines written since the last step read, keeps those that
135
- # carry its trace id, and, when it stamped anything, waits until the
136
- # log has been quiet for 30 ms (at most 300 ms) for lines still on
137
- # their way.
227
+ # A service's program.log read forward as the steps run: each step
228
+ # takes the complete lines written since the last step read, keeps
229
+ # those that carry its trace id, and, when it stamped anything, waits
230
+ # until the log has been quiet for 30 ms (at most 300 ms) for lines
231
+ # still on their way. Its slice goes beside the step record in
232
+ # program-log.<service>.txt, and its lines that arrived later in
233
+ # program-log-late.<service>.txt.
138
234
  class Log
139
- SLICE_FILE = "program-log.txt"
140
- LATE_FILE = "program-log-late.txt"
141
235
  QUIET = 0.03
142
236
  SETTLE = 0.3
143
237
  POLL = 0.01
@@ -145,11 +239,24 @@ module Nukadoko
145
239
 
146
240
  attr_reader :path #: String
147
241
  attr_reader :prefix #: String
242
+ attr_reader :service #: String
148
243
 
149
- #: (String, String) -> void
150
- def initialize(path, prefix)
244
+ #: (String) -> String
245
+ def self.slice_file(service) = "program-log.#{service}.txt"
246
+
247
+ #: (String) -> String
248
+ def self.late_file(service) = "program-log-late.#{service}.txt"
249
+
250
+ # Whether `name` is a file a service's slice or late lines take
251
+ # beside a step record, whatever the service's name, in any case.
252
+ #: (String) -> bool
253
+ def self.file?(name) = name.match?(/\Aprogram-log(-late)?\.[a-z0-9_-]+\.txt\z/i)
254
+
255
+ #: (String, String, String) -> void
256
+ def initialize(path, prefix, service)
151
257
  @path = path
152
258
  @prefix = prefix
259
+ @service = service
153
260
  @offset = File.size?(path).to_i
154
261
  end
155
262
 
@@ -188,14 +295,14 @@ module Nukadoko
188
295
  if lines.positive?
189
296
  text = +""
190
297
  kept.each { |line| text.bytesize + line.bytesize + 1 > KEPT ? break : text << line << "\n" }
191
- File.write(File.join(dir, SLICE_FILE), text)
192
- file = SLICE_FILE
298
+ file = Log.slice_file(@service)
299
+ File.write(File.join(dir, file), text)
193
300
  end
194
- { trace_id: boundary.trace_id, lines: lines, file: file, stamped: boundary.stamped, **(truncated ? { truncated: true } : {}) }
301
+ { lines: lines, file: file, stamped: boundary.stamped_by.fetch(@service, 0), **(truncated ? { truncated: true } : {}) }
195
302
  end
196
303
 
197
- # Once the world stopped: how many lines carry a trace id of the
198
- # world, and each step's lines that its slice did not read, written
304
+ # Once the system stopped: how many lines carry a trace id of the
305
+ # system, and each step's lines that its slice did not read, written
199
306
  # beside its record. `steps` maps a trace id to [lines read, dir].
200
307
  #: (Hash[String, Array[untyped]]) -> Hash[Symbol, untyped]
201
308
  def late(steps)
@@ -210,8 +317,8 @@ module Nukadoko
210
317
  seen[id] += 1
211
318
  next if seen[id] <= step[0]
212
319
 
213
- File.open(File.join(step[1], LATE_FILE), "a") { _1.puts(line) }
214
- late[id] ||= { lines: 0, file: LATE_FILE }
320
+ File.open(File.join(step[1], Log.late_file(@service)), "a") { _1.puts(line) }
321
+ late[id] ||= { lines: 0, file: Log.late_file(@service) }
215
322
  late[id][:lines] += 1
216
323
  end
217
324
  { tagged: tagged, late: late }
@@ -5,14 +5,21 @@ require "json"
5
5
  require "open3"
6
6
  require "rbconfig"
7
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),
8
+ # The base and the old system's passes of a `--base` run. Each row plans the
9
+ # cells its verdict needs from its head cells (RunRecord.planned_system_cells),
10
10
  # and each tree runs its planned rows one environment at a time, in a
11
11
  # process of its own started in the tree: a step file is loaded into the
12
12
  # process that runs it, and head's steps are already loaded in this one.
13
13
  # The pass writes its step records into the head run's directory, and its
14
14
  # executions and cells come back as JSON. A cell whose pass did not run
15
15
  # says why.
16
+ #
17
+ # Where both programs name {events}, the base system also runs, once and
18
+ # for run.json's `paths` alone, each row whose head cell is required and
19
+ # ran and that no base cell runs, so a criterion that did not change still
20
+ # shows what the code change did to its path. Such an execution carries
21
+ # `for_paths` and is no cell, so the table, the proof runs, and the
22
+ # verdicts never read it.
16
23
  module Nukadoko
17
24
  module Engine
18
25
  class TreePasses
@@ -21,9 +28,22 @@ module Nukadoko
21
28
 
22
29
  # `judged_by` says why head's passing executions of a row judged no
23
30
  # 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)
31
+ # `writes_events` says whether head's system of an environment writes
32
+ # execution events.
33
+ # `builds` are the builds head's pass ran (Program#builds): each pass
34
+ # gets them with those of the passes before it, so a checkout outside
35
+ # the repository builds once a run.
36
+ # `urls` is the key of the URL each service of head's system of each
37
+ # environment reached (Program.url_key), which the base's pass
38
+ # compares its own url function's answer with.
39
+ #: (Base, Array[Scenario], Hash[Scenario, Array[Hash[Symbol, untyped]]], Array[Environment], String, IO, ?^(Scenario) -> untyped, ?head_root: String, ?writes_events: ^(Environment) -> bool, ?own_fields: Hash[String, Array[String]], ?builds: Hash[String, [String, String?]], ?urls: Hash[String, Hash[String, String]]) -> void
40
+ def initialize(base, scenarios, head_cells, environments, dir, out, judged_by = ->(_) {}, head_root: Dir.pwd, writes_events: ->(_) { false }, own_fields: {}, builds: {},
41
+ urls: {})
42
+ @urls = urls
26
43
  @head_root = head_root
44
+ @builds = builds.dup
45
+ @own_fields = own_fields
46
+ @writes_events = writes_events
27
47
  @judged_by = judged_by
28
48
  @base = base
29
49
  @scenarios = scenarios
@@ -32,11 +52,11 @@ module Nukadoko
32
52
  @old_environments = base.old_environments || environments
33
53
  @dir = dir
34
54
  @out = out
35
- @worlds = []
55
+ @systems = []
36
56
  end
37
57
 
38
- # The worlds each tree's passes started, as run.json names them.
39
- attr_reader :worlds
58
+ # The systems each tree's passes started, as run.json names them.
59
+ attr_reader :systems
40
60
 
41
61
  # Appends each tree execution to `executions`, and returns each row's
42
62
  # base and old cells by its scenario.
@@ -44,27 +64,38 @@ module Nukadoko
44
64
  def call(executions)
45
65
  planned = @scenarios.to_h { [_1, plan(_1)] }
46
66
  found = {}
47
- [["base", @base.base_root, @environments], ["old", @base.old_root, @old_environments]].each do |world, root, environments|
67
+ [["base", @base.base_root, @environments], ["old", @base.old_root, @old_environments]].each do |system, root, environments|
48
68
  environments.each do |environment|
49
- rows = @scenarios.select { planned[_1][world.to_sym].include?(environment.name) }
50
- next if rows.empty?
69
+ rows = @scenarios.select { planned[_1][system.to_sym].include?(environment.name) }
70
+ paths = system == "base" && @writes_events.(environment) ? @scenarios.select { !rows.include?(_1) && ran_required?(_1, environment) } : []
71
+ next if rows.empty? && (paths.empty? || root.nil?)
51
72
 
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] || [])
73
+ outcome = (root ? run_tree(system, root, environment, rows, paths) : { not_run: "the #{system} system was not made" }) #: Hash[Symbol, untyped]
74
+ @systems.concat(outcome[:systems] || [])
75
+ paths.each do |scenario|
76
+ result = outcome[:results]&.find { |entry| [entry[:feature], entry[:line]] == place(system, scenario) }
77
+ came = result&.dig(:cell)
78
+ result&.dig(:executions)&.each do |json|
79
+ json.delete(:do)
80
+ json.merge!(role: "required", system: system, row: { feature: scenario.uri, line: scenario.line },
81
+ for_paths: { outcome: came&.dig(:outcome), line: came&.dig(:line) }.compact)
82
+ executions << { json: json }
83
+ end
84
+ end
54
85
  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" }
86
+ result = outcome[:results]&.find { |entry| [entry[:feature], entry[:line]] == place(system, scenario) }
87
+ found[[scenario, system, environment.name]] = result || { reason: outcome[:not_run] || "no execution of it was recorded" }
57
88
  result&.dig(:executions)&.each do |json|
58
89
  json.delete(:do)
59
- json.merge!(role: "required", world: world, row: { feature: scenario.uri, line: scenario.line })
90
+ json.merge!(role: "required", system: system, row: { feature: scenario.uri, line: scenario.line })
60
91
  executions << { json: json }
61
92
  end
62
93
  end
63
94
  end
64
95
  end
65
96
  @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]]) }
97
+ cells = [["base", @environments], ["old", @old_environments]].flat_map do |system, environments|
98
+ environments.select { planned[scenario][system.to_sym].include?(_1.name) }.map { cell(scenario, system, _1, found[[scenario, system, _1.name]]) }
68
99
  end
69
100
  [scenario, cells]
70
101
  end
@@ -72,6 +103,13 @@ module Nukadoko
72
103
 
73
104
  private
74
105
 
106
+ # Whether head's cell of a row in an environment is required and ran.
107
+ #: (Scenario, Environment) -> bool
108
+ def ran_required?(scenario, environment)
109
+ cell = @head_cells[scenario].find { _1[:environment] == environment.name }
110
+ !cell.nil? && cell[:role] == "required" && cell[:outcome] != "not-run"
111
+ end
112
+
75
113
  #: (Scenario) -> Hash[Symbol, Array[String]]
76
114
  def plan(scenario)
77
115
  change = @base.changes[Criteria.key(scenario.uri, scenario.line)]
@@ -79,44 +117,68 @@ module Nukadoko
79
117
  retagged: change[:retagged] == true, renamed: !change[:renamed_from].nil?, then_judges_no_claim: @judged_by.(scenario) }
80
118
  tags = Table.condition_tags(scenario.tags)
81
119
  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)
120
+ RunRecord.planned_system_cells(@head_cells[scenario], context, old_roles)
83
121
  end
84
122
 
85
123
  # Where a tree runs a head row: at its own place in the base tree,
86
124
  # which holds head's features, and at the ref's place in the old one.
87
125
  #: (String, Scenario) -> Array[untyped]
88
- def place(world, scenario)
89
- return [scenario.uri, scenario.line] if world == "base"
126
+ def place(system, scenario)
127
+ return [scenario.uri, scenario.line] if system == "base"
90
128
 
91
129
  @base.at(Criteria.key(scenario.uri, scenario.line)).values_at(:feature, :line)
92
130
  end
93
131
 
94
132
  #: (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"
133
+ def cell(scenario, system, environment, found)
134
+ role = system == "base" ? (@head_cells[scenario].find { _1[:environment] == environment.name }&.dig(:role) || "required") : "required"
97
135
  conditions = Table.conditions_of(scenario.tags, environment.conditions)
98
136
  own = found[:cell]
99
- return { world: world, environment: environment.name, role: role, conditions: conditions, outcome: "not-run", runs: 0, reason: found[:reason] } unless own
137
+ return { system: system, environment: environment.name, role: role, conditions: conditions, outcome: "not-run", runs: 0, reason: found[:reason] } unless own
100
138
 
101
- { **own, world: world, environment: environment.name, role: role, conditions: conditions }
139
+ { **own, system: system, environment: environment.name, role: role, conditions: conditions }
102
140
  end
103
141
 
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 } } }
142
+ # `paths` are the rows the tree runs for paths alone, which its pass
143
+ # drops when the tree's own program names no {events}.
144
+ #: (String, String, Environment, Array[Scenario], ?Array[Scenario]) -> Hash[Symbol, untyped]
145
+ def run_tree(system, root, environment, rows, paths = [])
146
+ plan = File.join(@dir, "trees", "#{system}-#{environment.name}.plan.json")
147
+ result = File.join(@dir, "trees", "#{system}-#{environment.name}.result.json")
148
+ targets = rows.map { |scenario| place(system, scenario).then { { feature: _1, line: _2 } } }
149
+ for_paths = paths.map { |scenario| place(system, scenario).then { { feature: _1, line: _2 } } }
109
150
  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))
151
+ head = { root: File.expand_path(@head_root), env_files: environment.env_files, public: (config[:public] || []).map(&:to_s), redact: (config[:redact] || []).map(&:to_s),
152
+ own_fields: @own_fields, builds: @builds, urls: @urls }
153
+ File.write(plan, JSON.generate(system: system, environment: environment.name, targets: targets, for_paths: for_paths, dir: @dir, result: result, head: head))
112
154
  output, status = Open3.capture2e(RbConfig.ruby, "-I", LIB, EXE, "__tree", plan, chdir: root)
113
155
  @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)
156
+ if File.exist?(result)
157
+ read = JSON.parse(File.read(result), symbolize_names: true) #: Hash[Symbol, untyped]
158
+ (read.delete(:builds) || {}).each { |key, (built_in, failed)| @builds[key.to_s] = [built_in, failed] }
159
+ return read
160
+ end
115
161
 
116
- { not_run: "the #{world} world's pass ended with exit #{status.exitstatus} before it recorded a result: #{output.lines.last.to_s.strip}" }
162
+ { not_run: "the #{system} system's pass ended #{TreePasses.ended(status)} before it recorded a result: #{output.lines.last.to_s.strip}" }
117
163
  ensure
118
164
  FileUtils.rm_f([plan, result].compact)
119
165
  end
166
+
167
+ # How a pass's process ended, for a cell whose result was never
168
+ # written. Process::Status#exitstatus is nil once a signal stopped the
169
+ # process, where "ended with exit " named neither the signal nor that
170
+ # there was one.
171
+ #: (Process::Status) -> String
172
+ def self.ended(status)
173
+ code = status.exitstatus
174
+ return "with exit #{code}" if code
175
+
176
+ signal = status.termsig
177
+ return "without an exit status" unless signal
178
+
179
+ name = Signal.signame(signal)
180
+ name ? "on SIG#{name}" : "on signal #{signal}"
181
+ end
120
182
  end
121
183
  end
122
184
  end