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
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  # The run's verdict table: a row per criterion (a scenario, or an Examples
4
5
  # row), a cell per environment. A cell is `required` where its environment
@@ -9,6 +10,19 @@ module Nukadoko
9
10
  module Table
10
11
  PROOF_RUNS = 2
11
12
 
13
+ # A run's --proof-runs, read by every judgment of the one run this
14
+ # process makes.
15
+ class << self
16
+ #: (Integer) -> Integer
17
+ def proof_runs=(count)
18
+ @proof_runs = count
19
+ end
20
+
21
+ #: () -> Integer
22
+ def proof_runs = @proof_runs || PROOF_RUNS
23
+ end
24
+
25
+ #: (Array[String], Hash[String, untyped]) -> String
12
26
  def self.role(tags, conditions)
13
27
  meets = tags.all? do |tag|
14
28
  case tag
@@ -22,9 +36,13 @@ module Nukadoko
22
36
  end
23
37
 
24
38
  # What a cell's executions came to. An execution is `[:pass]`,
25
- # `[:fail, line, claim]`, or `[:broke, line]`.
39
+ # `[:fail, line, claim]`, `[:broke, line]`, or `[:stopped, line]`
40
+ # for a walk a person stopped after that line.
41
+ #: (Array[Array[untyped]]) -> Hash[Symbol, untyped]
26
42
  def self.cell(executions)
27
43
  first = executions.first
44
+ return { outcome: "not-run", runs: 0 } if first.nil?
45
+
28
46
  if executions.uniq.size > 1
29
47
  line = executions.find { _1.first != :pass }&.at(1)
30
48
  { outcome: "flaky", line: line }
@@ -33,15 +51,29 @@ module Nukadoko
33
51
  end.merge(runs: executions.size)
34
52
  end
35
53
 
54
+ #: (Array[Hash[Symbol, untyped]]) -> String
36
55
  def self.verdict(cells)
37
56
  required = cells.select { _1[:role] == "required" }
38
57
  return "failed" if required.empty? || required.any? { _1[:outcome] != "pass" }
39
58
 
40
- proven = cells.any? { _1[:role] == "control" && _1[:outcome] == "fail" && _1[:runs] >= PROOF_RUNS }
59
+ proven = cells.any? { _1[:role] == "control" && _1[:outcome] == "fail" && _1[:runs] >= proof_runs }
41
60
  proven ? "proven" : "held"
42
61
  end
43
62
 
63
+ #: (Array[String]) -> Array[String]
44
64
  def self.condition_tags(tags) = tags.grep(/\A@with(out)?:/)
65
+
66
+ # The tags a row lists: its condition tags and @held, as written.
67
+ #: (Array[String]) -> Array[String]
68
+ def self.row_tags(tags) = [*condition_tags(tags), *(tags.include?("@held") ? ["@held"] : [])]
69
+
70
+ # For each condition the tags name, the value the environment declares
71
+ # for it, or nil: what a cell's role came from.
72
+ #: (Array[String], Hash[String, untyped]) -> Hash[String, untyped]
73
+ def self.conditions_of(tags, conditions)
74
+ names = condition_tags(tags).map { _1[/\A@with(?:out)?:([\w.-]+)/, 1].to_s }.uniq
75
+ names.to_h { [_1, conditions.fetch(_1, nil)] }
76
+ end
45
77
  end
46
78
  end
47
79
  end
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # `fits-text`, as the TypeScript engine judges it: an expected text fits an
5
+ # actual one line by line, where a line that is exactly `...` takes any run
6
+ # of actual lines, `[..]` any characters inside one line, and `[..:name]`
7
+ # does too, and then every stand-in of one name must have read one text and
8
+ # two names two texts. The judge and the claim line both read this module,
9
+ # so the two never disagree about where a text stopped fitting. It walks
10
+ # the expected lines once and keeps only the actual lines each one can end
11
+ # before, so a text costs time in its length rather than a power of its
12
+ # `...` lines.
13
+ module Nukadoko
14
+ module Engine
15
+ module TextFit
16
+ ANY_LINES = "..."
17
+ STAND_IN_NAME = /\A[A-Za-z0-9_-]+\z/
18
+
19
+ # An expected line split at its stand-ins: the fixed texts, and the
20
+ # place of each named stand-in among them.
21
+ Fixed = Data.define(:parts, :names)
22
+
23
+ module_function
24
+
25
+ # A `[..` that `]`, or `:`, a name, and `]`, do not follow is text.
26
+ #: (String) -> Fixed
27
+ def fixed_line(line)
28
+ parts = []
29
+ names = {}
30
+ from = 0
31
+ open = line.index("[..")
32
+ while open
33
+ close = line.index("]", open + 3)
34
+ inner = close && line[(open + 3)...close]
35
+ unless close && (inner == "" || (inner.to_s.start_with?(":") && inner.to_s[1..].to_s.match?(STAND_IN_NAME)))
36
+ open = line.index("[..", open + 1)
37
+ next
38
+ end
39
+ names[parts.size] = inner.to_s[1..] unless inner == ""
40
+ parts << line[from...open]
41
+ from = close + 1
42
+ open = line.index("[..", from)
43
+ end
44
+ parts << line[from..]
45
+ Fixed.new(parts: parts, names: names)
46
+ end
47
+
48
+ # A text's lines, at "\n" or "\r\n", with one final empty line dropped.
49
+ #: (String) -> Array[String]
50
+ def lines(text)
51
+ split = text.split(/\r?\n/, -1)
52
+ split.pop if split.last == ""
53
+ split
54
+ end
55
+
56
+ # Each part between the first and the last is found leftmost after
57
+ # the one before it, which is enough when a stand-in takes anything.
58
+ #: (Array[String], String, ?Array[String?]?) -> bool
59
+ def parts_fit(parts, line, reads = nil)
60
+ return parts[0] == line if parts.size == 1
61
+
62
+ first = parts[0]
63
+ last = parts[-1]
64
+ return false if line.size < first.size + last.size || !line.start_with?(first) || !line.end_with?(last)
65
+
66
+ at = first.size
67
+ stop = line.size - last.size
68
+ parts[1...-1].to_a.each do |part|
69
+ found = line.index(part, at)
70
+ return false if found.nil? || found + part.size > stop
71
+
72
+ reads&.push(line[at...found])
73
+ at = found + part.size
74
+ end
75
+ reads&.push(line[at...stop])
76
+ true
77
+ end
78
+
79
+ #: (String, String) -> bool
80
+ def line_fits(pattern, line) = parts_fit(fixed_line(pattern).parts, line)
81
+
82
+ # { fits: true }, or where the text stopped fitting: `fitted` expected
83
+ # lines fit, `expected` fit nothing from actual line `at` on, which is
84
+ # `actual`; or `readings`, the two named stand-ins that disagree.
85
+ #: (String, String) -> Hash[Symbol, untyped]
86
+ def fit(actual, expected)
87
+ have = lines(actual)
88
+ want_lines = lines(expected)
89
+ want = want_lines.map { _1 == ANY_LINES ? nil : fixed_line(_1) }
90
+ reach = { at: [0] } #: Hash[Symbol, untyped]
91
+ want.each_with_index do |line, index|
92
+ earliest = reach[:from] || reach[:at][0] #: Integer
93
+ if line.nil?
94
+ reach = { from: earliest }
95
+ next
96
+ end
97
+ starts = reach[:from] ? (reach[:from]...have.size).to_a : reach[:at]
98
+ after = starts.select { _1 < have.size && parts_fit(line.parts, have[_1]) }.map { _1 + 1 }
99
+ if after.empty?
100
+ return { fits: false, fitted: index, expected: want_lines[index], at: earliest + 1, **(have[earliest].nil? ? {} : { actual: have[earliest] }) }
101
+ end
102
+
103
+ reach = { at: after }
104
+ end
105
+ if reach[:at] && reach[:at][-1] != have.size
106
+ left = reach[:at][-1]
107
+ return { fits: false, fitted: want.size, at: left + 1, actual: have[left] }
108
+ end
109
+ return { fits: true } unless want.any? { _1 && _1.names.any? }
110
+
111
+ names_fit(have, want, first_fit(have, want))
112
+ end
113
+
114
+ # The actual line each expected line takes in the first way the text
115
+ # fits, each `...` taking as few lines as it can from the top: a walk
116
+ # from the end learns which lines the rest can start at.
117
+ #: (Array[String], Array[Fixed?]) -> Array[Integer?]
118
+ def first_fit(have, want)
119
+ rests = []
120
+ rest = { at: [have.size] } #: Hash[Symbol, untyped]
121
+ (want.size - 1).downto(0) do |index|
122
+ line = want[index]
123
+ if line.nil?
124
+ rest = { up_to: rest[:up_to] || rest[:at][-1] }
125
+ else
126
+ candidates = rest[:up_to] ? (0...rest[:up_to]).to_a : rest[:at].select(&:positive?).map { _1 - 1 }
127
+ rest = { at: candidates.select { _1 < have.size && parts_fit(line.parts, have.fetch(_1)) } }
128
+ end
129
+ rests[index] = rest
130
+ end
131
+ taken = []
132
+ at = 0
133
+ open = false
134
+ want.each_with_index do |line, index|
135
+ if line.nil?
136
+ open = true
137
+ taken << nil
138
+ next
139
+ end
140
+ at = rests[index][:at].find { _1 >= at } if open
141
+ taken << at
142
+ at += 1
143
+ open = false
144
+ end
145
+ taken
146
+ end
147
+
148
+ #: (Array[String], Array[Fixed?], Array[Integer?]) -> Hash[Symbol, untyped]
149
+ def names_fit(have, want, taken)
150
+ by_name = {}
151
+ by_text = {}
152
+ want.each_with_index do |line, index|
153
+ next if line.nil? || line.names.empty?
154
+
155
+ at = taken[index] #: Integer
156
+ reads = [] #: Array[String?]
157
+ parts_fit(line.parts, have[at].to_s, reads)
158
+ line.names.each do |place, name|
159
+ 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
167
+ end
168
+ end
169
+ { fits: true }
170
+ end
171
+
172
+ # Whether an expected text states nothing beyond its stand-ins.
173
+ #: (String) -> bool
174
+ def only_stand_ins?(expected)
175
+ lines(expected).all? { _1 == ANY_LINES || Spaces.blank?(fixed_line(_1).parts.join) }
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,244 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "securerandom"
5
+ require "uri"
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.
15
+ module Nukadoko
16
+ module Engine
17
+ module Trace
18
+ PREFIX_LENGTH = 8
19
+ ID_LENGTH = 32
20
+ MAX_SPANS = 1000
21
+
22
+ #: () -> String
23
+ def self.new_prefix = SecureRandom.hex(PREFIX_LENGTH / 2)
24
+
25
+ # W3C reserves an id of all zeros as invalid, so one is drawn again.
26
+ #: (String) -> String
27
+ def self.new_id(prefix)
28
+ loop do
29
+ id = prefix + SecureRandom.hex((ID_LENGTH - prefix.size) / 2)
30
+ return id unless id.match?(/\A0+\z/)
31
+ end
32
+ end
33
+
34
+ #: () -> String
35
+ def self.new_span
36
+ loop do
37
+ span = SecureRandom.hex(8)
38
+ return span unless span.match?(/\A0+\z/)
39
+ end
40
+ end
41
+
42
+ #: (String, String) -> String
43
+ def self.traceparent(trace_id, span) = "00-#{trace_id}-#{span}-01"
44
+
45
+ # The trace id of the world `prefix` names that `line` carries, or nil.
46
+ #: (String, String) -> String?
47
+ def self.id_in(line, prefix)
48
+ text = line.downcase
49
+ at = text.index(prefix)
50
+ while at
51
+ return nil if at + ID_LENGTH > text.size
52
+
53
+ id = text[at, ID_LENGTH].to_s
54
+ return id if id.match?(/\A\h{#{ID_LENGTH}}\z/)
55
+
56
+ at = text.index(prefix, at + 1)
57
+ end
58
+ nil
59
+ end
60
+
61
+ #: (URI::Generic) -> String
62
+ def self.origin(uri) = "#{uri.scheme}://#{uri.host}#{":#{uri.port}" unless uri.port == uri.default_port}"
63
+
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.
67
+ #: (untyped, untyped) -> bool
68
+ def self.stamps?(world, target)
69
+ target = URI(target.to_s)
70
+ world = URI(world.to_s)
71
+ return true if origin(target) == origin(world)
72
+
73
+ world.host == "localhost" && target.host.to_s.end_with?(".localhost") && target.scheme == world.scheme && target.port == world.port
74
+ rescue URI::Error
75
+ false
76
+ end
77
+
78
+ # The step that runs now, kept per thread as the processes are.
79
+ #: () -> Boundary?
80
+ def self.current = Thread.current[:nukadoko_trace]
81
+
82
+ #: [T] (Boundary) { () -> T } -> T
83
+ def self.within(boundary)
84
+ Thread.current[:nukadoko_trace] = boundary
85
+ yield
86
+ ensure
87
+ Thread.current[:nukadoko_trace] = nil
88
+ end
89
+
90
+ # One step's trace: what it stamped, and when it last did.
91
+ class Boundary
92
+ attr_reader :trace_id #: String
93
+ attr_reader :origin #: String
94
+ attr_reader :stamped #: Integer
95
+ attr_reader :spans #: Array[String]
96
+ attr_reader :process_spans #: Array[String]
97
+ attr_reader :last_stamp_at #: Float?
98
+
99
+ #: (String, String) -> void
100
+ def initialize(trace_id, origin)
101
+ @trace_id = trace_id
102
+ @origin = origin
103
+ @stamped = 0
104
+ @spans = []
105
+ @process_spans = []
106
+ @last_stamp_at = nil
107
+ end
108
+
109
+ # The traceparent a request to `url` goes out with, or nil when it
110
+ # is not the world's.
111
+ #: (untyped) -> String?
112
+ def stamp(url)
113
+ return nil unless Trace.stamps?(@origin, url)
114
+
115
+ 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
119
+ Trace.traceparent(@trace_id, span)
120
+ end
121
+
122
+ # A process's TRACEPARENT is not a stamped request: nothing waits
123
+ # for the request it may send, but the log is read a little longer.
124
+ #: () -> String
125
+ def process_traceparent
126
+ span = Trace.new_span
127
+ @last_stamp_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
128
+ @process_spans << span if @process_spans.size < MAX_SPANS
129
+ Trace.traceparent(@trace_id, span)
130
+ end
131
+ end
132
+
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.
138
+ class Log
139
+ SLICE_FILE = "program-log.txt"
140
+ LATE_FILE = "program-log-late.txt"
141
+ QUIET = 0.03
142
+ SETTLE = 0.3
143
+ POLL = 0.01
144
+ KEPT = 16 * 1024 * 1024
145
+
146
+ attr_reader :path #: String
147
+ attr_reader :prefix #: String
148
+
149
+ #: (String, String) -> void
150
+ def initialize(path, prefix)
151
+ @path = path
152
+ @prefix = prefix
153
+ @offset = File.size?(path).to_i
154
+ end
155
+
156
+ #: (String, Boundary) -> Hash[Symbol, untyped]
157
+ def slice(dir, boundary)
158
+ kept = []
159
+ bytes = 0
160
+ truncated = false
161
+ last_own = nil
162
+ read = lambda do
163
+ each_new_line do |line|
164
+ next unless Trace.id_in(line, @prefix) == boundary.trace_id
165
+
166
+ last_own = now
167
+ if bytes + line.bytesize + 1 > KEPT
168
+ truncated = true
169
+ else
170
+ bytes += line.bytesize + 1
171
+ end
172
+ kept << line
173
+ end
174
+ end
175
+ read.()
176
+ if boundary.last_stamp_at
177
+ ended = now
178
+ loop do
179
+ quiet_since = [ended, last_own || 0].max
180
+ break if now - quiet_since >= QUIET || now >= ended + SETTLE
181
+
182
+ sleep POLL
183
+ read.()
184
+ end
185
+ end
186
+ lines = kept.size
187
+ file = nil
188
+ if lines.positive?
189
+ text = +""
190
+ 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
193
+ end
194
+ { trace_id: boundary.trace_id, lines: lines, file: file, stamped: boundary.stamped, **(truncated ? { truncated: true } : {}) }
195
+ end
196
+
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
199
+ # beside its record. `steps` maps a trace id to [lines read, dir].
200
+ #: (Hash[String, Array[untyped]]) -> Hash[Symbol, untyped]
201
+ def late(steps)
202
+ tagged = 0
203
+ seen = Hash.new(0)
204
+ late = {}
205
+ File.foreach(@path) do |line|
206
+ line = line.chomp
207
+ id = Trace.id_in(line, @prefix) or next
208
+ tagged += 1
209
+ step = steps[id] or next
210
+ seen[id] += 1
211
+ next if seen[id] <= step[0]
212
+
213
+ File.open(File.join(step[1], LATE_FILE), "a") { _1.puts(line) }
214
+ late[id] ||= { lines: 0, file: LATE_FILE }
215
+ late[id][:lines] += 1
216
+ end
217
+ { tagged: tagged, late: late }
218
+ rescue SystemCallError
219
+ { tagged: tagged, late: late }
220
+ end
221
+
222
+ private
223
+
224
+ #: () -> Float
225
+ def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
226
+
227
+ # A line still being written, after the last newline, is left for
228
+ # the next read.
229
+ #: () { (String) -> void } -> void
230
+ def each_new_line
231
+ size = File.size?(@path).to_i
232
+ return if size <= @offset
233
+
234
+ chunk = File.binread(@path, size - @offset, @offset)
235
+ stop = chunk.rindex("\n") or return
236
+ @offset += stop + 1
237
+ chunk.byteslice(0, stop).to_s.force_encoding("UTF-8").scrub.split("\n", -1).each { yield _1 }
238
+ rescue SystemCallError
239
+ nil
240
+ end
241
+ end
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "time"
6
+ require "zlib"
7
+
8
+ # A step's trace.zip read back into the step record's `actions`: every
9
+ # Playwright call the step made through its page, as the TypeScript engine
10
+ # lists them. Only `trace.trace` inside the zip is read, by walking the
11
+ # zip's central directory, so no zip library is needed. A trace that cannot
12
+ # be read gives no actions rather than failing the step: measuring never
13
+ # breaks the execution. A trace format version this build has not measured
14
+ # gives none either, since guessing at its shape would turn an unread
15
+ # format into a record.
16
+ module Nukadoko
17
+ module Engine
18
+ module TraceActions
19
+ # Version 8 as the TypeScript engine measured it, and version 10 as
20
+ # Playwright 1.65.1 writes it for the Ruby client: the same `before`
21
+ # and `after` fields, with a number for `callId`.
22
+ KNOWN_VERSIONS = [8, 10].freeze
23
+ MAX_ACTIONS = 100
24
+ INTERNAL_WAIT = "__waitInfo__"
25
+ JUDGED_VIEW_GROUP = "nukadoko: view of the judged reading"
26
+
27
+ module_function
28
+
29
+ # { actions:, truncated: } for the trace at `path`, or nil when there
30
+ # is none.
31
+ #: (String) -> Hash[Symbol, untyped]?
32
+ def read(path)
33
+ return nil unless File.exist?(path)
34
+
35
+ trace = zip_entry(File.binread(path), "trace.trace")
36
+ trace ? parse(trace) : { actions: [] }
37
+ rescue StandardError
38
+ { actions: [] }
39
+ end
40
+
41
+ #: (String) -> Hash[Symbol, untyped]
42
+ def parse(text)
43
+ header = nil #: Hash[String, untyped]?
44
+ pending = {}
45
+ # The calls nukadoko made for the judged view, and every call under them.
46
+ own = {}
47
+ actions = []
48
+ total = 0
49
+ text.force_encoding("UTF-8").each_line do |line|
50
+ entry = JSON.parse(line) rescue next
51
+ next unless entry.is_a?(Hash) && entry["type"].is_a?(String)
52
+
53
+ case entry["type"]
54
+ when "context-options"
55
+ header ||= entry if %w[version wallTime monotonicTime].all? { entry[_1].is_a?(Numeric) }
56
+ when "before"
57
+ next unless entry["method"].is_a?(String) && entry["startTime"].is_a?(Numeric)
58
+
59
+ if (entry["method"] == "tracingGroup" && entry["title"] == JUDGED_VIEW_GROUP) || own[entry["parentId"]]
60
+ own[entry["callId"]] = true
61
+ next
62
+ end
63
+ pending[entry["callId"]] = entry
64
+ when "after"
65
+ before = pending.delete(entry["callId"]) or next
66
+ next if before["method"] == INTERNAL_WAIT || !entry["endTime"].is_a?(Numeric) || header.nil?
67
+
68
+ total += 1
69
+ actions << action(before, entry, header) if actions.size < MAX_ACTIONS
70
+ end
71
+ end
72
+ return { actions: [] } if header.nil? || !KNOWN_VERSIONS.include?(header["version"])
73
+
74
+ { actions: actions, **(total > MAX_ACTIONS ? { truncated: total } : {}) }
75
+ end
76
+
77
+ #: (Hash[String, untyped], Hash[String, untyped], Hash[String, untyped]) -> Hash[Symbol, untyped]
78
+ def action(before, after, header)
79
+ params = before["params"].is_a?(Hash) ? before["params"] : {}
80
+ at = Time.at((header["wallTime"] + (before["startTime"] - header["monotonicTime"])) / 1000.0).utc.iso8601(3)
81
+ { method: before["method"],
82
+ **(params["expression"].is_a?(String) ? { expression: params["expression"] } : {}),
83
+ **(params["selector"].is_a?(String) ? { selector: params["selector"] } : {}),
84
+ **(params["url"].is_a?(String) ? { url: params["url"] } : {}),
85
+ **([true, false].include?(params["isNot"]) ? { is_not: params["isNot"] } : {}),
86
+ **(params["timeout"].is_a?(Numeric) ? { timeout_ms: params["timeout"] } : {}),
87
+ ms: (after["endTime"] - before["startTime"]).round, outcome: after.key?("error") ? "failed" : "passed", at: at }
88
+ end
89
+
90
+ #: (String, String) -> String?
91
+ def zip_entry(bytes, name)
92
+ eocd = bytes.rindex([0x06054b50].pack("V")) or return nil
93
+ # A cut past the end reads as empty, which fails the checks below.
94
+ cut = ->(at, length) { bytes.byteslice(at, length).to_s }
95
+ u16 = ->(at) { cut.(at, 2).unpack1("v").to_i }
96
+ u32 = ->(at) { cut.(at, 4).unpack1("V").to_i }
97
+ offset = u32.(eocd + 16)
98
+ stop = offset + u32.(eocd + 12)
99
+ while offset < stop
100
+ return nil unless u32.(offset) == 0x02014b50
101
+
102
+ method = u16.(offset + 10)
103
+ compressed = u32.(offset + 20)
104
+ name_size = u16.(offset + 28)
105
+ local = u32.(offset + 42)
106
+ if cut.(offset + 46, name_size) == name
107
+ data = cut.(local + 30 + u16.(local + 26) + u16.(local + 28), compressed)
108
+ return method.zero? ? data : Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(data)
109
+ end
110
+ offset += 46 + name_size + u16.(offset + 30) + u16.(offset + 32)
111
+ end
112
+ nil
113
+ end
114
+ end
115
+ end
116
+ end