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
@@ -0,0 +1,490 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "fileutils"
6
+
7
+ # The execution events each service of a served system appends to the file
8
+ # `{events}` names (docs/execution-events.md, docs/spec.md "Execution
9
+ # events"): each step's slice of them, written beside its record when the
10
+ # step ends, and, once the system has stopped, each service's redacted copy
11
+ # and each step's lines that arrived after its record. Boundary: owns no
12
+ # file (the program writes it, Program makes and removes it), and decides
13
+ # nothing a line says (Events, Selection, Tally do).
14
+ #
15
+ # One reader over every service: a step's trace runs across them, and one
16
+ # wait reads them all, so a line one service wrote for a request another
17
+ # service passed on closes work the first service's file leaves open. A
18
+ # service whose events nukadoko does not read (one that names no
19
+ # `{events}`, or one at a `url`) has no file here, and the steps that sent
20
+ # it a request say so (`service-unrecorded`).
21
+ #
22
+ # A line belongs to the step whose trace id its parsed `trace` field
23
+ # holds, never to one whose id merely appears in its text: a value inside
24
+ # an event can hold an id too. An unreadable line has no owner, so no
25
+ # step's events take data from it; when its text carries a step's trace
26
+ # id by the program log's rule, the step's slice counts it as damage to
27
+ # its trace, and the line is kept only in the system's copy.
28
+ #
29
+ # The program writes the file directly, not through a pipe nukadoko
30
+ # redacts, so only redacted copies reach the run directory: each line is
31
+ # parsed, redacted with the step's secrets, and written again as JSON, so
32
+ # a copy is JSON wherever a secret was, and a record shows exactly what
33
+ # was read. An unreadable line has no object to redact, so its text is
34
+ # redacted as the program log's is.
35
+ #
36
+ # The file nukadoko made is the one read. A file that is replaced, or made
37
+ # shorter, while the scenarios run is no longer read, and every step from
38
+ # then on says so (`file-replaced`), since what it read may be incomplete.
39
+ #
40
+ # A step that stamped a request, or gave a process its trace, reads the
41
+ # file as it waits, as the program log's slice does (Trace::Log): until no
42
+ # line of its own has arrived for QUIET seconds after the later of its
43
+ # last such line and the moment it ended, and no longer than SETTLE.
44
+ module Nukadoko
45
+ module Engine
46
+ module Events
47
+ SYSTEM_FILE = "events.jsonl"
48
+ # The most one of a step's files holds; a step whose lines come to
49
+ # more keeps the first of them, marked `truncated`.
50
+ SLICE_CAP = 16 * 1024 * 1024
51
+ QUIET = 0.03
52
+ SETTLE = 0.3
53
+ POLL = 0.01
54
+ READ_CHUNK = 1024 * 1024
55
+
56
+ # The file beside the step record that holds a service's lines of the
57
+ # step.
58
+ #: (String) -> String
59
+ def self.slice_file(service) = "events.#{service}.jsonl"
60
+
61
+ # The file beside the step record for a service's lines of the step
62
+ # that arrived after its record was written.
63
+ #: (String) -> String
64
+ def self.late_file(service) = "events-late.#{service}.jsonl"
65
+
66
+ # Whether `name` is a file a service's slice or late lines take beside
67
+ # a step record, whatever the service's name, in any case: a step may
68
+ # not attach one.
69
+ #: (String) -> bool
70
+ def self.events_file?(name) = name.match?(/\Aevents(-late)?\.[a-z0-9_-]+\.jsonl\z/i)
71
+
72
+ # The line as a copy keeps it: the parsed object redacted and written
73
+ # again, or the text redacted when there is no object.
74
+ #: (String, Line, Secrets) -> String
75
+ def self.kept_line(text, line, secrets)
76
+ return secrets.redact_string(text) unless line.readable?
77
+
78
+ JsJson.generate(secrets.redact(line.object))
79
+ rescue ArgumentError
80
+ # Two keys that redact to one: the text, redacted, loses neither.
81
+ secrets.redact_string(text)
82
+ end
83
+
84
+ # Hands each complete line of `path` from byte `from` to the block,
85
+ # with the byte it starts at, a chunk at a time, and returns the byte
86
+ # after the last complete line: a line still being written is read by
87
+ # the next call. A file that does not exist yet has no lines.
88
+ #: (String, Integer) { (String, Integer) -> void } -> Integer
89
+ def self.each_line(path, from)
90
+ File.open(path, "rb") do |file|
91
+ offset = from
92
+ carry = "".b
93
+ loop do
94
+ chunk = file.pread(READ_CHUNK, offset + carry.bytesize)
95
+ bytes = carry + chunk
96
+ stop = bytes.rindex("\n")
97
+ if stop.nil?
98
+ carry = bytes
99
+ next
100
+ end
101
+ start = 0
102
+ bytes.byteslice(0, stop + 1).to_s.each_line("\n") do |raw|
103
+ yield raw.chomp("\n").force_encoding("UTF-8").scrub, offset + start
104
+ start += raw.bytesize
105
+ end
106
+ offset += stop + 1
107
+ carry = bytes.byteslice(stop + 1, bytes.bytesize).to_s
108
+ rescue EOFError
109
+ return offset
110
+ end
111
+ end
112
+ rescue SystemCallError
113
+ from
114
+ end
115
+
116
+ # A step's process count as a tally reads it: none, all ended, or one
117
+ # that may still send.
118
+ #: (Array[Hash[Symbol, untyped]]?) -> String
119
+ def self.processes_state(processes)
120
+ return "none" if processes.nil? || processes.empty?
121
+
122
+ processes.all? { _1[:group] == "ended" } ? "ended" : "unbounded"
123
+ end
124
+
125
+ # What a tally needs of a step's trace. `unread` names the services
126
+ # whose events nukadoko does not read, of which the ones the step sent
127
+ # a request to leave the trace open under their name.
128
+ #: (Trace::Boundary, Array[Hash[Symbol, untyped]]?, ?Array[String]) -> Hash[Symbol, untyped]
129
+ def self.inputs_of(boundary, processes, unread = [])
130
+ unrecorded = unread.select { boundary.stamped_by.fetch(_1, 0).positive? }
131
+ { spans: boundary.spans.dup, spans_not_kept: boundary.spans_not_kept.dup, processes: processes_state(processes),
132
+ process_spans: boundary.process_spans.dup, mcp_spans: boundary.mcp_spans.dup,
133
+ **(unrecorded.empty? ? {} : { unrecorded: unrecorded }) }
134
+ end
135
+
136
+ # The tally inputs of a recorded slice: each service's stamped spans
137
+ # as its entry holds them. The slice's own gaps say what its processes
138
+ # left open: `processes-unbounded` names one that may still send, and
139
+ # `no-requests` a step that ran none and stamped nothing. A step with
140
+ # no gap of either kind that stamped nothing ran processes that all
141
+ # ended. A service whose events the run did not read is named by the
142
+ # slice's own `service-unrecorded`, since its entry is nil.
143
+ #: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
144
+ def self.inputs_of_slice(slice)
145
+ read = (slice[:services] || {}).reject { |_, own| own.nil? }
146
+ has = ->(word) { (slice[:open] || []).any? { _1[:gap] == word } }
147
+ processes = if has.("processes-unbounded") then "unbounded"
148
+ elsif has.("no-requests") || slice[:stamped].to_i.positive? then "none"
149
+ else "ended"
150
+ end
151
+ unrecorded = (slice[:open] || []).filter_map { _1[:service] if _1[:gap] == "service-unrecorded" }
152
+ { spans: read.to_h { |service, own| [service.to_s, own[:stamped_spans] || []] },
153
+ spans_not_kept: read.to_h { |service, own| [service.to_s, [0, own[:stamped].to_i - (own[:stamped_spans] || []).size].max] },
154
+ processes: processes, process_spans: slice[:process_spans] || [], mcp_spans: slice[:mcp_spans] || [],
155
+ **(unrecorded.empty? ? {} : { unrecorded: unrecorded }) }
156
+ end
157
+
158
+ # Every service of a slice whose lines the run read, in its order.
159
+ #: (Hash[Symbol, untyped]) -> Array[String]
160
+ def self.services_of(slice) = (slice[:services] || {}).filter_map { |service, own| service.to_s unless own.nil? }
161
+
162
+ # What one of a step's files may still hold, out of SLICE_CAP.
163
+ class Kept
164
+ attr_reader :lines #: Array[String]
165
+ attr_reader :truncated #: bool
166
+
167
+ #: () -> void
168
+ def initialize
169
+ @lines = []
170
+ @bytes = 0
171
+ @truncated = false
172
+ end
173
+
174
+ #: (String) -> void
175
+ def keep(text)
176
+ size = text.bytesize + 1
177
+ if @truncated || @bytes + size > SLICE_CAP
178
+ @truncated = true
179
+ return
180
+ end
181
+ @bytes += size
182
+ @lines << text
183
+ end
184
+
185
+ # Writes the kept lines to `name` in `dir` and returns `name`, or
186
+ # nil when none were kept or the file could not be written.
187
+ #: (String, String) -> String?
188
+ def write(dir, name)
189
+ return nil if @lines.empty?
190
+
191
+ FileUtils.mkdir_p(dir)
192
+ File.write(File.join(dir, name), "#{@lines.join("\n")}\n")
193
+ name
194
+ rescue SystemCallError
195
+ nil
196
+ end
197
+ end
198
+
199
+ # One service's events file, as a claim step's reading reads it
200
+ # (Events.read).
201
+ class ServiceFile
202
+ attr_reader :service #: String
203
+ attr_reader :path #: String
204
+ attr_reader :prefix #: String
205
+
206
+ #: (String, String, String) -> void
207
+ def initialize(service, path, prefix)
208
+ @service = service
209
+ @path = path
210
+ @prefix = prefix
211
+ @made = identity
212
+ @replaced = @made.nil?
213
+ @offset = @made ? @made[2] : 0
214
+ # The service's kinds, read forward from the file's start the
215
+ # first time a reading asks: earlier scenarios' lines say what
216
+ # the program reports.
217
+ @kinds = KindsSeen.new
218
+ @kinds_read = 0
219
+ end
220
+
221
+ attr_accessor :offset #: Integer
222
+
223
+ # Whether the file is still the one nukadoko made, asked before each
224
+ # read from `offset`: once it answers false, it always does.
225
+ #: (Integer) -> bool
226
+ def readable?(offset)
227
+ return false if @replaced
228
+
229
+ now = identity
230
+ @replaced = now.nil? || now[0] != @made&.[](0) || now[1] != @made&.[](1) || now[2] < offset
231
+ !@replaced
232
+ end
233
+
234
+ #: () -> bool
235
+ def replaced? = @replaced
236
+
237
+ # The kinds this service's program has reported, through the end of
238
+ # the file as it stands now.
239
+ #: () -> KindsSeen
240
+ def kinds_seen
241
+ @kinds_read = Events.each_line(@path, @kinds_read) { |text, _| @kinds.note(Events.parse_line(text)) } if readable?(@kinds_read)
242
+ @kinds
243
+ end
244
+
245
+ private
246
+
247
+ #: () -> [Integer, Integer, Integer]?
248
+ def identity
249
+ stat = File.stat(@path)
250
+ [stat.dev, stat.ino, stat.size]
251
+ rescue SystemCallError
252
+ nil
253
+ end
254
+ end
255
+
256
+ # Every service's events file of one system, read forward as its steps
257
+ # end.
258
+ class Reader
259
+ attr_reader :prefix #: String
260
+ # One per service whose file this reader reads, in the program's
261
+ # order.
262
+ attr_reader :systems #: Array[ServiceFile]
263
+
264
+ # `services` is each service of the system with a URL: `service` and
265
+ # `path`, the file it appends its events to, or nil for a service
266
+ # whose events nukadoko does not read.
267
+ #: (Array[Hash[Symbol, untyped]], String, ^() -> Secrets) -> void
268
+ def initialize(services, prefix, secrets)
269
+ @prefix = prefix
270
+ @secrets = secrets
271
+ @unread = services.filter_map { _1[:service].to_s if _1[:path].nil? }
272
+ @systems = services.filter_map { ServiceFile.new(_1[:service].to_s, _1[:path].to_s, prefix) if _1[:path] }
273
+ @producers = {} #: Hash[String, Hash[Symbol, untyped]]
274
+ end
275
+
276
+ # Where the step whose trace is `trace_id` began, nil when this
277
+ # reader sliced no such step.
278
+ #: (String) -> Hash[Symbol, untyped]?
279
+ def producer(trace_id) = @producers[trace_id]
280
+
281
+ # Reads what every service's file gained since the last call and
282
+ # writes the slice of `boundary`'s trace into `dir`.
283
+ #: (String, Trace::Boundary, Array[Hash[Symbol, untyped]]?) -> Hash[Symbol, untyped]
284
+ def slice(dir, boundary, processes)
285
+ inputs = Events.inputs_of(boundary, processes, @unread)
286
+ @producers[boundary.trace_id] = { trace_id: boundary.trace_id, from: @systems.to_h { [_1.service, _1.offset] }, inputs: inputs }
287
+ tally = Tally.new(inputs)
288
+ kept = @systems.to_h { [_1.service, Kept.new] }
289
+ secrets = @secrets.()
290
+ fold = lambda do |service, text|
291
+ line = Events.parse_line(text)
292
+ if !line.readable?
293
+ next false unless Trace.id_in(text, @prefix) == boundary.trace_id
294
+
295
+ tally.damaged(service)
296
+ next true
297
+ end
298
+ owner = Events.owner_of(line, @prefix)
299
+ tally.unowned(line, service) if owner.nil?
300
+ next false unless owner == boundary.trace_id
301
+
302
+ tally.owned(line, service)
303
+ kept.fetch(service).keep(Events.kept_line(text, line, secrets))
304
+ true
305
+ end
306
+ read_step(boundary.last_stamp_at, fold)
307
+ reading = tally.reading(@systems.to_h { [_1.service, { truncated: kept.fetch(_1.service).truncated, replaced: _1.replaced? }] })
308
+ services = @systems.to_h do |file|
309
+ own = reading[:services].fetch(file.service)
310
+ [file.service, { lines: own[:lines], file: kept.fetch(file.service).write(dir, Events.slice_file(file.service)), kinds: own[:kinds],
311
+ stamped: boundary.stamped_by.fetch(file.service, 0), stamped_spans: boundary.spans[file.service] || [],
312
+ unreadable: own[:unreadable], unowned: own[:unowned], process_requests: own[:process_requests] }]
313
+ end
314
+ { trace_id: boundary.trace_id, stamped: boundary.stamped, services: services.merge(@unread.to_h { [_1, nil] }),
315
+ process_spans: boundary.process_spans.dup, **(boundary.mcp_spans.empty? ? {} : { mcp_spans: boundary.mcp_spans.dup }),
316
+ whole: reading[:whole], open: reading[:open] }
317
+ end
318
+
319
+ # Once the system stopped: each service's redacted copy at
320
+ # `copies[service]`, each step's lines past its slice in its
321
+ # `Events.late_file(service)`, and what each file holds. `steps`
322
+ # maps a trace id to [lines its slice read per service, its record's
323
+ # directory, its tally inputs]. With inputs, a step's path and its
324
+ # writes that persisted to a table that holds state are folded from
325
+ # every line of its trace, across every service, and each of
326
+ # `recounts` (`{ trace_id:, inputs:, selection:, service:,
327
+ # replaced: }`) is counted again over every line of its trace,
328
+ # leaving out what `tables`, each service's tables list, leaves out.
329
+ # A writer's own kind counts in a path by `own_fields`, the fields
330
+ # the change's intent reads on it.
331
+ #: (Hash[String, String], Hash[String, Array[untyped]], ?recounts: Array[Hash[Symbol, untyped]], ?tables: Hash[String, Hash[Symbol, untyped]], ?own_fields: Hash[String, Array[String]]) -> Hash[Symbol, untyped]
332
+ def collect(copies, steps, recounts: [], tables: {}, own_fields: {})
333
+ own = { fields: own_fields, redact: ->(value) { @secrets.().redact(value) } }
334
+ folds = steps.filter_map { |id, (_, _, inputs)| [id, PathFold.new(inputs.merge(tables: tables), own)] if inputs }.to_h
335
+ writes = steps.filter_map { |id, (_, _, inputs)| [id, Tally.new(inputs.merge(selections: [{ selection: Selection::PERSISTED_WRITES }], tables: tables))] if inputs }.to_h
336
+ # One per service: a kind one service reports says nothing about
337
+ # another's program.
338
+ kinds = @systems.to_h { [_1.service, KindsSeen.new] }
339
+ tallies = recounts.map do
340
+ Tally.new(_1[:inputs].merge(selections: [{ selection: _1[:selection], **(_1[:service] ? { service: _1[:service] } : {}) }], tables: tables))
341
+ end
342
+ tallies_of = Hash.new { |h, k| h[k] = [] } #: Hash[String, Array[Tally]]
343
+ recounts.each_with_index { |recount, index| tallies_of[recount[:trace_id]] << tallies.fetch(index) }
344
+ read = {} #: Hash[String, Hash[Symbol, untyped]]
345
+ late = {} #: Hash[String, Hash[String, Hash[Symbol, untyped]]]
346
+ @systems.each do |file|
347
+ found = collect_service(file, copies[file.service], steps, folds: folds, writes: writes, kinds: kinds.fetch(file.service), tallies_of: tallies_of)
348
+ read[file.service] = found[:counted]
349
+ found[:late].each { |id, slice| (late[id] ||= {})[file.service] = slice }
350
+ end
351
+ # Nothing is kept, so nothing is cut.
352
+ files_of = lambda do |replaced|
353
+ @systems.to_h { [_1.service, { truncated: false, replaced: replaced[_1.service] == true }] }
354
+ end
355
+ paths = folds.to_h { |id, fold| [id, fold.path(files_of.((steps.fetch(id)[3] || {})))] }
356
+ # Only a write the lines say persisted counts.
357
+ secrets = @secrets.()
358
+ state_writes = writes.filter_map do |id, tally|
359
+ counted = tally.reading(files_of.((steps.fetch(id)[3] || {})))[:counts][0][:counted]
360
+ [id, secrets.redact(counted)] if counted.any?
361
+ end.to_h
362
+ recounted = recounts.each_with_index.map do |recount, index|
363
+ reading = tallies.fetch(index).reading(files_of.(recount[:replaced] || {}))
364
+ count = reading[:counts][0]
365
+ # The kinds of the service the claim counted, or of every
366
+ # service it counted lines of.
367
+ seen = recount[:service] ? (kinds[recount[:service]] || KindsSeen.new) : AnyKindsSeen.new(kinds.values)
368
+ { lines: reading[:lines], unreadable: reading[:unreadable], count: count.merge(counted: secrets.redact(count[:counted])),
369
+ open: Events.reading_gaps(reading[:open], recount[:selection]["kind"], seen) }
370
+ end
371
+ { services: read, late: late, paths: paths, state_writes: state_writes, recounted: recounted }
372
+ end
373
+
374
+ private
375
+
376
+ # Reads one service's whole file into the folds and tallies the stop
377
+ # shares across its services, and writes its copy and its steps'
378
+ # late lines.
379
+ #: (ServiceFile, String?, Hash[String, Array[untyped]], folds: Hash[String, PathFold], writes: Hash[String, Tally], kinds: KindsSeen, tallies_of: Hash[String, Array[Tally]]) -> Hash[Symbol, untyped]
380
+ def collect_service(file, copy, steps, folds:, writes:, kinds:, tallies_of:)
381
+ service = file.service
382
+ secrets = @secrets.()
383
+ counts = { lines: 0, owned: 0, unowned: 0, unreadable: 0 }
384
+ undefined_kinds = Hash.new(0) #: Hash[String, Integer]
385
+ seen = Hash.new(0) #: Hash[String, Integer]
386
+ late = {} #: Hash[String, Hash[Symbol, untyped]]
387
+ late_kept = {} #: Hash[String, Kept]
388
+ copied = !copy.nil?
389
+ out = if copy.nil?
390
+ nil
391
+ else
392
+ begin
393
+ FileUtils.mkdir_p(File.dirname(copy))
394
+ File.open(copy, "w")
395
+ rescue SystemCallError
396
+ copied = false
397
+ nil
398
+ end
399
+ end
400
+ Events.each_line(file.path, 0) do |text, _|
401
+ line = Events.parse_line(text)
402
+ counts[:lines] += 1
403
+ owner = Events.owner_of(line, @prefix)
404
+ if !line.readable? then counts[:unreadable] += 1
405
+ elsif owner.nil? then counts[:unowned] += 1
406
+ end
407
+ undefined_kinds[line.kind.to_s] += 1 if line.state == "undefined-kind"
408
+ kinds.note(line)
409
+ # Ownership as the slice reads it: a damaged line by the id its
410
+ # text carries, a readable one by its `trace` field.
411
+ if !line.readable?
412
+ if (id = Trace.id_in(text, @prefix))
413
+ folds[id]&.damaged(service)
414
+ writes[id]&.damaged(service)
415
+ tallies_of.fetch(id, []).each { _1.damaged(service) }
416
+ end
417
+ elsif owner
418
+ folds[owner]&.owned(line, service)
419
+ writes[owner]&.owned(line, service)
420
+ tallies_of.fetch(owner, []).each { _1.owned(line, service) }
421
+ end
422
+ shown = Events.kept_line(text, line, secrets)
423
+ begin
424
+ out&.puts(shown)
425
+ rescue SystemCallError
426
+ copied = false
427
+ end
428
+ next unless owner
429
+
430
+ counts[:owned] += 1
431
+ step = steps[owner] or next
432
+ seen[owner] += 1
433
+ next if seen[owner] <= (step[0].is_a?(Hash) ? (step[0][service] || 0) : step[0].to_i)
434
+
435
+ (late_kept[owner] ||= Kept.new).keep(shown)
436
+ late[owner] = { lines: (late.dig(owner, :lines) || 0) + 1 }
437
+ end
438
+ out&.close
439
+ late.each do |id, entry|
440
+ kept = late_kept.fetch(id)
441
+ entry[:file] = kept.lines.empty? ? nil : append(File.join(steps.fetch(id)[1], Events.late_file(service)), kept.lines, service)
442
+ entry[:truncated] = true if kept.truncated
443
+ end
444
+ { counted: { copied: copied, **counts, undefined_kinds: undefined_kinds }, late: late }
445
+ end
446
+
447
+ #: (String, Array[String], String) -> String?
448
+ def append(path, lines, service)
449
+ FileUtils.mkdir_p(File.dirname(path))
450
+ File.open(path, "a") { _1.write("#{lines.join("\n")}\n") }
451
+ Events.late_file(service)
452
+ rescue SystemCallError
453
+ nil
454
+ end
455
+
456
+ #: () -> Float
457
+ def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
458
+
459
+ # Reads what every service's file gained and hands each line to
460
+ # `own`, which answers whether it is the ending step's; then, when
461
+ # the step stamped anything, keeps reading as it waits for the
462
+ # step's lines still on their way (module header). One wait over
463
+ # every file: a line a service wrote for a request another service
464
+ # passed on closes work the first service's file leaves open.
465
+ #: (Float?, ^(String, String) -> bool) -> void
466
+ def read_step(last_stamp_at, own)
467
+ last_own = nil #: Float?
468
+ read = lambda do
469
+ @systems.each do |file|
470
+ next unless file.readable?(file.offset)
471
+
472
+ file.offset = Events.each_line(file.path, file.offset) { |text, _| last_own = now if own.(file.service, text) }
473
+ end
474
+ end
475
+ read.()
476
+ return unless last_stamp_at
477
+
478
+ ended = now
479
+ loop do
480
+ quiet_since = [ended, last_own || 0].max
481
+ break if now - quiet_since >= QUIET || now >= ended + SETTLE
482
+
483
+ sleep POLL
484
+ read.()
485
+ end
486
+ end
487
+ end
488
+ end
489
+ end
490
+ end