nukadoko 0.6.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 (200) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +541 -3
  3. data/README.ja.md +20 -5
  4. data/README.md +12 -6
  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 +395 -48
  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 +1 -1
  77. data/lib/nukadoko/judgments.rb +4 -4
  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 +1148 -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 +799 -29
  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/version.rb +1 -1
  104. data/lib/nukadoko/vocabulary.rb +166 -23
  105. data/lib/nukadoko/walk.rb +2 -2
  106. data/lib/nukadoko/walk_step.rb +1 -1
  107. data/lib/nukadoko.rb +23 -0
  108. data/llms.txt +41 -34
  109. data/sig/generated/nukadoko/base.rbs +4 -4
  110. data/sig/generated/nukadoko/changed_files.rbs +42 -0
  111. data/sig/generated/nukadoko/check.rbs +147 -10
  112. data/sig/generated/nukadoko/claim_drops.rbs +135 -0
  113. data/sig/generated/nukadoko/claim_line.rbs +11 -0
  114. data/sig/generated/nukadoko/claim_rules.rbs +49 -4
  115. data/sig/generated/nukadoko/claim_sides.rbs +63 -12
  116. data/sig/generated/nukadoko/cli.rbs +80 -7
  117. data/sig/generated/nukadoko/codes.rbs +19 -4
  118. data/sig/generated/nukadoko/command.rbs +68 -1
  119. data/sig/generated/nukadoko/command_help.rbs +18 -0
  120. data/sig/generated/nukadoko/compat.rbs +392 -0
  121. data/sig/generated/nukadoko/criteria.rbs +1 -1
  122. data/sig/generated/nukadoko/definition.rbs +50 -0
  123. data/sig/generated/nukadoko/error_frames.rbs +1 -1
  124. data/sig/generated/nukadoko/events/count.rbs +69 -0
  125. data/sig/generated/nukadoko/events/counted.rbs +70 -0
  126. data/sig/generated/nukadoko/events/format.rbs +163 -0
  127. data/sig/generated/nukadoko/events/lists.rbs +47 -0
  128. data/sig/generated/nukadoko/events/path.rbs +161 -0
  129. data/sig/generated/nukadoko/events/read.rbs +109 -0
  130. data/sig/generated/nukadoko/events/reader.rbs +226 -0
  131. data/sig/generated/nukadoko/events/selection.rbs +100 -0
  132. data/sig/generated/nukadoko/events/tally.rbs +227 -0
  133. data/sig/generated/nukadoko/features.rbs +6 -4
  134. data/sig/generated/nukadoko/fixtures.rbs +67 -11
  135. data/sig/generated/nukadoko/http_recording.rbs +1 -1
  136. data/sig/generated/nukadoko/intent.rbs +132 -0
  137. data/sig/generated/nukadoko/intent_file.rbs +46 -0
  138. data/sig/generated/nukadoko/intent_view.rbs +69 -0
  139. data/sig/generated/nukadoko/judgment_file.rbs +1 -1
  140. data/sig/generated/nukadoko/judgments.rbs +1 -1
  141. data/sig/generated/nukadoko/measure_mcp.rbs +104 -0
  142. data/sig/generated/nukadoko/parts.rbs +2 -2
  143. data/sig/generated/nukadoko/path_view.rbs +105 -0
  144. data/sig/generated/nukadoko/place_rows.rbs +43 -0
  145. data/sig/generated/nukadoko/program.rbs +627 -23
  146. data/sig/generated/nukadoko/program_log.rbs +67 -0
  147. data/sig/generated/nukadoko/program_shape.rbs +147 -0
  148. data/sig/generated/nukadoko/project_fixtures.rbs +16 -3
  149. data/sig/generated/nukadoko/project_program.rbs +2 -1
  150. data/sig/generated/nukadoko/repeated_prefix.rbs +172 -0
  151. data/sig/generated/nukadoko/report_run.rbs +1 -1
  152. data/sig/generated/nukadoko/run.rbs +286 -41
  153. data/sig/generated/nukadoko/run_record.rbs +20 -12
  154. data/sig/generated/nukadoko/scaffold.rbs +3 -0
  155. data/sig/generated/nukadoko/schema.rbs +5 -0
  156. data/sig/generated/nukadoko/secrets.rbs +26 -3
  157. data/sig/generated/nukadoko/states.rbs +214 -0
  158. data/sig/generated/nukadoko/step.rbs +58 -1
  159. data/sig/generated/nukadoko/step_fixtures.rbs +39 -0
  160. data/sig/generated/nukadoko/summary.rbs +256 -2
  161. data/sig/generated/nukadoko/table.rbs +3 -2
  162. data/sig/generated/nukadoko/trace.rbs +108 -33
  163. data/sig/generated/nukadoko/tree_passes.rbs +36 -8
  164. data/sig/generated/nukadoko/underneath.rbs +141 -0
  165. data/sig/generated/nukadoko/vocabulary.rbs +45 -6
  166. data/skills/nukadoko/SKILL.md +18 -18
  167. data/skills/nukadoko/references/checking-steps.md +16 -83
  168. data/skills/nukadoko/references/diagnosing.md +6 -6
  169. data/skills/nukadoko/references/evidence.md +19 -6
  170. data/skills/nukadoko/references/exploring.md +22 -7
  171. data/skills/nukadoko/references/fixtures.md +12 -32
  172. data/skills/nukadoko/references/judgments.md +12 -0
  173. data/skills/nukadoko/references/maintenance.md +1 -1
  174. data/skills/nukadoko/references/merge-risk.md +1 -1
  175. data/skills/nukadoko/references/parts.md +46 -23
  176. data/skills/nukadoko/references/{perturbed-worlds.md → perturbed-systems.md} +30 -12
  177. data/skills/nukadoko/references/program.md +168 -169
  178. data/skills/nukadoko/references/reviewing.md +20 -20
  179. data/skills/nukadoko/references/ruby/checking-steps.md +106 -0
  180. data/skills/nukadoko/references/ruby/examples/cli.md +212 -0
  181. data/skills/nukadoko/references/ruby/fixtures.md +34 -0
  182. data/skills/nukadoko/references/ruby/program.md +112 -0
  183. data/skills/nukadoko/references/ruby/step-files.md +115 -0
  184. data/skills/nukadoko/references/running.md +34 -34
  185. data/skills/nukadoko/references/scenario-rules.md +7 -6
  186. data/skills/nukadoko/references/states.md +7 -1
  187. data/skills/nukadoko/references/step-files.md +41 -120
  188. data/skills/nukadoko/references/typescript/checking-steps.md +129 -0
  189. data/skills/nukadoko/references/{examples → typescript/examples}/cli.md +1 -1
  190. data/skills/nukadoko/references/{examples → typescript/examples}/http-api.md +6 -6
  191. data/skills/nukadoko/references/{examples → typescript/examples}/workflow.md +1 -1
  192. data/skills/nukadoko/references/typescript/fixtures.md +56 -0
  193. data/skills/nukadoko/references/typescript/program.md +111 -0
  194. data/skills/nukadoko/references/typescript/step-files.md +104 -0
  195. data/skills/nukadoko/references/verdicts.md +10 -10
  196. data/skills/nukadoko/references/writing-steps.md +67 -67
  197. data/skills/nukadoko/references/writing-the-feature.md +1 -1
  198. data/skills/nukadoko-verification/SKILL.md +13 -8
  199. metadata +103 -7
  200. data/skills/nukadoko/references/ruby.md +0 -80
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+
6
+ # Responsibility: the lists a program prints of the places it has
7
+ # (docs/spec.md "Lists"): its routes, its flags, and its tables, one JSON
8
+ # object per line, each place named in the words the execution events use
9
+ # (a request line's `method` and `route`, a flag line's `name` and `side`,
10
+ # a db line's `table`), so a list joins with the lines by text alone. As
11
+ # data: every kind and its fields, and the one function that turns what a
12
+ # list command printed into the kind's entries or the reason it is not a
13
+ # list. Pure. Boundary: running the command is program.rb's, and what a
14
+ # run reads against a list is run.rb's and place_rows.rb's.
15
+ #
16
+ # A list is read whole or not at all. Read in part, it would leave out what
17
+ # a line it could not read named, and a request to that route would then
18
+ # read as one the list does not name, with nothing to say why. So one line
19
+ # that is not one entry of the kind, with a key the kind does not have, a
20
+ # value of the wrong type, or a method that is not uppercase (a request
21
+ # line's method is), makes the whole list unread, naming the line.
22
+ #
23
+ # A list is a set: an entry printed twice is one entry. A route printed
24
+ # twice with two handlers keeps both, and a flag printed twice keeps every
25
+ # side either line declares. A table printed twice with two different
26
+ # holdings is refused, naming both lines: what a table holds decides
27
+ # whether a count counts a write to it, so neither line can win.
28
+
29
+ module Nukadoko
30
+ module Engine
31
+ module Events
32
+ # Every kind, in the order a system reads them.
33
+ LIST_KINDS = %w[routes flags tables].freeze
34
+
35
+ module Lists
36
+ TEXT = lambda do |value|
37
+ next "is not a string" unless value.is_a?(String)
38
+
39
+ value.empty? ? "is empty" : nil
40
+ end #: ^(untyped) -> String?
41
+ METHOD = lambda do |value|
42
+ problem = TEXT.(value)
43
+ next problem if problem
44
+
45
+ value == value.upcase ? nil : "is #{JSON.generate(value)}, which is not uppercase, as a request line's method is"
46
+ end #: ^(untyped) -> String?
47
+ HOLDS = ->(value) { TABLE_HOLDS.include?(value) ? nil : "is #{JSON.generate(value)}, which is not one of #{TABLE_HOLDS.join(", ")}" } #: ^(untyped) -> String?
48
+ SIDES = lambda do |value|
49
+ next "is not an array of strings" unless value.is_a?(Array) && value.all?(String)
50
+ next "is empty" if value.empty?
51
+
52
+ value.include?("") ? "holds an empty side" : nil
53
+ end #: ^(untyped) -> String?
54
+
55
+ # Every kind of list and the fields its lines hold, each with
56
+ # whether it is required and why a value cannot be it. A key that is
57
+ # not one of them, and does not start with `x_`, is refused, so a
58
+ # misspelt optional field is not read as absent.
59
+ FIELDS = {
60
+ "routes" => { "method" => [true, METHOD], "route" => [true, TEXT], "handler" => [false, TEXT] },
61
+ "flags" => { "name" => [true, TEXT], "sides" => [true, SIDES] },
62
+ "tables" => { "table" => [true, TEXT], "holds" => [false, HOLDS] }
63
+ }.freeze #: Hash[String, Hash[String, [bool, ^(untyped) -> String?]]]
64
+
65
+ module_function
66
+
67
+ # What a list command of `kind` printed, as the kind's entries
68
+ # (`{ entries: }`), or why it is not a list (`{ problem: }`). A line
69
+ # of whitespace alone holds no entry, so a blank line at the end, or
70
+ # a CR before each line feed, reads as nothing.
71
+ #: (String, String) -> Hash[Symbol, untyped]
72
+ def parse(kind, output)
73
+ lines = [] #: Array[[Hash[String, untyped], Integer]]
74
+ output.split("\n", -1).each_with_index do |text, index|
75
+ next if text.strip.empty?
76
+
77
+ read = line(kind, text.delete_suffix("\r"), index + 1)
78
+ return read if read.key?(:problem)
79
+
80
+ lines << [read[:line], index + 1]
81
+ end
82
+ collect(kind, lines)
83
+ end
84
+
85
+ # Line `number` of the output as an entry of `kind`, or why it is
86
+ # not one.
87
+ #: (String, String, Integer) -> Hash[Symbol, untyped]
88
+ def line(kind, text, number)
89
+ value = begin
90
+ JSON.parse(text)
91
+ rescue JSON::ParserError
92
+ return { problem: "line #{number} is not JSON" }
93
+ end
94
+ return { problem: "line #{number} is not one JSON object" } unless value.is_a?(Hash)
95
+
96
+ repeated = Events.repeated_key(text)
97
+ return { problem: "line #{number} holds the key #{JSON.generate(repeated)} twice" } if repeated
98
+
99
+ fields = FIELDS.fetch(kind)
100
+ value.each_key do |key|
101
+ next if fields.key?(key) || key.start_with?(EXTENSION_PREFIX)
102
+
103
+ return { problem: "line #{number} has the key #{JSON.generate(key)}, which a #{kind} line does not have " \
104
+ "(#{fields.keys.join(", ")}, or an #{EXTENSION_PREFIX} key of the program's own)" }
105
+ end
106
+ fields.each do |name, (required, problem_of)|
107
+ unless value.key?(name)
108
+ return { problem: "line #{number} has no #{name}" } if required
109
+
110
+ next
111
+ end
112
+ problem = problem_of.(value[name])
113
+ return { problem: "line #{number}'s #{name} #{problem}" } if problem
114
+ end
115
+ { line: value }
116
+ end
117
+
118
+ # What a kind's checked lines come to, merged per place, or why they
119
+ # are not one list.
120
+ #: (String, Array[[Hash[String, untyped], Integer]]) -> Hash[Symbol, untyped]
121
+ def collect(kind, lines)
122
+ case kind
123
+ when "routes"
124
+ routes = {} #: Hash[[String, String], Hash[Symbol, untyped]]
125
+ lines.each do |line, _|
126
+ entry = routes[[line["method"], line["route"]]] ||= { method: line["method"], route: line["route"], handlers: [] }
127
+ entry[:handlers] |= [line["handler"]] if line["handler"].is_a?(String)
128
+ end
129
+ { entries: routes.values.map { _1.merge(handlers: _1[:handlers].sort) } }
130
+ when "flags"
131
+ flags = {} #: Hash[String, Array[String]]
132
+ lines.each { |line, _| flags[line["name"]] = (flags[line["name"]] || []) | line["sides"] }
133
+ { entries: flags.map { { name: _1, sides: _2 } } }
134
+ else
135
+ tables = {} #: Hash[String, [String, Integer]]
136
+ lines.each do |line, number|
137
+ holds = line["holds"] || "state"
138
+ earlier = tables[line["table"]]
139
+ if earlier.nil? then tables[line["table"]] = [holds, number]
140
+ elsif earlier.first != holds
141
+ return { problem: "line #{number} says table #{JSON.generate(line["table"])} holds #{holds}, and line #{earlier.last} says it holds #{earlier.first}" }
142
+ end
143
+ end
144
+ { entries: tables.map { { table: _1, holds: _2.first } } }
145
+ end
146
+ end
147
+
148
+ # A read tables list, as a count reads it: the tables it marks.
149
+ #: (Array[Hash[Symbol, untyped]]) -> Hash[Symbol, untyped]
150
+ def tables_of(entries) = { marked: entries.reject { _1[:holds] == "state" }.to_h { [_1[:table], _1[:holds]] } }
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,285 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # A step's coarse execution path: what one trace's lines say the program
5
+ # did, counted by what each line did and no finer (a write by table and
6
+ # op, a job by name and phase, a served request by route and status), and
7
+ # the range a count of one entry lies in on a path, by the gaps the trace
8
+ # leaves open. Pure. Which lines are the trace's, and reading them, are
9
+ # the caller's (Reader#collect folds every line of a system's file when
10
+ # the system stops).
11
+ #
12
+ # PATH_KINDS is the one table of what a path keeps. Its order is the
13
+ # order a reader ranks a difference by: what persists outside the process
14
+ # first, then the route through it.
15
+ #
16
+ # Each service of the system writes its own file, and a step's trace runs
17
+ # across all of them, so every entry and every request names the service
18
+ # whose file held its lines: a write to one service's database is not the
19
+ # same entry as a write to another's, and a request a service passed on
20
+ # counts where it was served. A key with no service is one a record
21
+ # written before a system could hold several services holds.
22
+ #
23
+ # A path leaves out what is too fine or too noisy to compare across two
24
+ # runs of one criterion: a database read (a query count moves with every
25
+ # cache), a `tx` line, a row's ids, and an actor. A write to a table the
26
+ # program's tables list marks as bookkeeping or infrastructure stays in
27
+ # it, its entry carrying what the table holds, since another reader of
28
+ # that table may depend on it.
29
+ #
30
+ # A count on a path is a range, as a count claim's is. Lines that may be
31
+ # any line raise only its upper bound, and a trace that is not whole has
32
+ # none, except that the step's own requests are written inside first, so
33
+ # under `jobs-pending` alone a request count stays bounded, and a step
34
+ # that sent nothing and has no line has an exact empty path.
35
+ #
36
+ # A writer's own kind (`x_decision`) is in a path after the format's
37
+ # kinds, counted by its kind and by the fields the project names, each
38
+ # read at a dotted path, and by nothing else: a writer may put an id or a
39
+ # time in its own field, which would make every run differ from the last.
40
+ #
41
+ # An outgoing call's origin on loopback is kept without its port: a test
42
+ # run starts the services it calls on free ports, so a key with the port
43
+ # would make two runs differ by nothing the program decided.
44
+ module Nukadoko
45
+ module Engine
46
+ module Events
47
+ PATH_KINDS = {
48
+ "error" => { fields: %w[class handled], compare: "count" },
49
+ "db" => { fields: %w[table op], compare: "count", writes_only: true },
50
+ "http" => { fields: %w[method origin path status], compare: "count" },
51
+ "job" => { fields: %w[name phase], compare: "count" },
52
+ "request" => { fields: %w[method route status], compare: "count" },
53
+ "flag" => { fields: %w[name side], compare: "seen", condition_premise: true }
54
+ }.freeze #: Hash[String, Hash[Symbol, untyped]]
55
+
56
+ PATH_KIND_ORDER = PATH_KINDS.keys.freeze
57
+
58
+ NO_OWN_READING = { fields: {}, redact: ->(value) { value } }.freeze #: Hash[Symbol, untyped]
59
+
60
+ LOOPBACK_HOSTS = %w[localhost 127.0.0.1 [::1]].freeze
61
+ ORIGIN = %r{\A([A-Za-z][A-Za-z0-9+.-]*://)(\[[^\]]*\]|[^:/\[\]]*)(?::[0-9]*)?\z}
62
+
63
+ # Whether a kind is a writer's own.
64
+ #: (untyped) -> bool
65
+ def self.own_kind?(kind) = kind.is_a?(String) && kind.start_with?(EXTENSION_PREFIX) && kind.size > EXTENSION_PREFIX.size
66
+
67
+ # The value at a dotted path of a JSON object, nil where it has none.
68
+ #: (Hash[String, untyped], String) -> untyped
69
+ def self.value_at(object, field)
70
+ field.split(".").reduce(object) do |value, segment|
71
+ return nil unless value.is_a?(Hash) && value.key?(segment)
72
+
73
+ value[segment]
74
+ end
75
+ end
76
+
77
+ # The fields of a key: the format's kinds' in PATH_KINDS order, a
78
+ # writer's own in its key's order.
79
+ #: (Hash[String, untyped]) -> Array[String]
80
+ def self.key_fields(key)
81
+ return key.keys.select { _1.start_with?(EXTENSION_PREFIX) } if own_kind?(key["kind"])
82
+
83
+ PATH_KINDS.fetch(key["kind"])[:fields]
84
+ end
85
+
86
+ # A key as one text, the same for equal keys: what two paths are
87
+ # joined by. The service comes first, so one service's entries are
88
+ # told from another's wherever a key is compared.
89
+ #: (Hash[String, untyped]) -> String
90
+ def self.path_key_text(key)
91
+ fields = key_fields(key)
92
+ service = key["service"]
93
+ return JsJson.generate([key["kind"], service, *fields.flat_map { [_1, key[_1]] }]) if own_kind?(key["kind"])
94
+
95
+ JsJson.generate([key["kind"], service, *fields.map { key[_1] }])
96
+ end
97
+
98
+ # Where a kind ranks: PATH_KINDS order, then every writer's own kind.
99
+ #: (String) -> Integer
100
+ def self.kind_rank(kind) = own_kind?(kind) ? PATH_KIND_ORDER.size : PATH_KIND_ORDER.index(kind) || PATH_KIND_ORDER.size
101
+
102
+ #: (Hash[String, untyped], Hash[String, untyped]) -> Integer
103
+ def self.by_path_key(left, right)
104
+ (kind_rank(left["kind"]) <=> kind_rank(right["kind"])).nonzero? || (path_key_text(left) <=> path_key_text(right)) || 0
105
+ end
106
+
107
+ # The order of a flag's gates: the format's first, in its order,
108
+ # then a word it does not define, then nil.
109
+ #: (String?) -> [Integer, String]
110
+ def self.gate_order(gate)
111
+ known = KINDS.fetch("flag").fetch("gate").values || []
112
+ return [known.size + 1, ""] if gate.nil?
113
+
114
+ [known.index(gate) || known.size, gate]
115
+ end
116
+
117
+ #: (String?, String?) -> Integer
118
+ def self.by_gate(left, right) = gate_order(left) <=> gate_order(right) || 0
119
+
120
+ # An `http` line's origin as a path keeps it: on a loopback host,
121
+ # without its port; any other origin as written.
122
+ #: (String) -> String
123
+ def self.path_origin(origin)
124
+ match = ORIGIN.match(origin)
125
+ return origin unless match && LOOPBACK_HOSTS.include?(match[2].to_s.downcase)
126
+
127
+ "#{match[1]}#{match[2]}"
128
+ end
129
+
130
+ # The entry a line of `service`'s file counts toward, or nil for a
131
+ # line that is not in a coarse path.
132
+ #: (Line, ?String?) -> Hash[String, untyped]?
133
+ def self.path_key_of(line, service = nil)
134
+ return nil unless line.event? && PATH_KINDS.key?(line.kind)
135
+
136
+ spec = PATH_KINDS.fetch(line.kind.to_s)
137
+ object = line.object || {}
138
+ return nil if spec[:writes_only] && object["op"] == "read"
139
+
140
+ key = { "kind" => line.kind, **(service ? { "service" => service } : {}), **spec[:fields].to_h { [_1, object[_1]] } }
141
+ key["origin"] = path_origin(key["origin"]) if key["kind"] == "http" && key["origin"].is_a?(String)
142
+ key
143
+ end
144
+
145
+ # The key a writer's own line counts toward, by the fields `reading`
146
+ # names for its kind.
147
+ #: (String, Hash[String, untyped], Hash[Symbol, untyped], ?String?) -> Hash[String, untyped]
148
+ def self.own_key_of(kind, object, reading, service = nil)
149
+ fields = reading[:fields][kind] || []
150
+ { "kind" => kind, **(service ? { "service" => service } : {}), **fields.sort.to_h { [_1, reading[:redact].(value_at(object, _1))] } }
151
+ end
152
+
153
+ # The key alone of an entry, without its count and gates.
154
+ #: (Hash[String, untyped]) -> Hash[String, untyped]
155
+ def self.key_only(entry)
156
+ { "kind" => entry["kind"], **(entry["service"] ? { "service" => entry["service"] } : {}), **key_fields(entry).to_h { [_1, entry[_1]] } }
157
+ end
158
+
159
+ # The key of a served request: the service that served it, when the
160
+ # request names one.
161
+ #: (Hash[String, untyped]) -> Hash[String, untyped]
162
+ def self.request_key(request)
163
+ { "kind" => "request", **(request["service"] ? { "service" => request["service"] } : {}),
164
+ "method" => request["method"], "route" => request["route"], "status" => request["status"] }
165
+ end
166
+
167
+ # One trace's lines, folded into its path in any order.
168
+ class PathFold
169
+ #: (Hash[Symbol, untyped], ?Hash[Symbol, untyped]) -> void
170
+ def initialize(inputs, own = NO_OWN_READING)
171
+ @tally = Tally.new(inputs.merge(selections: []))
172
+ @tables = inputs[:tables] || {}
173
+ @own = own
174
+ # The order the step sent its requests, across the services it
175
+ # sent them to: one trace, so a span id names one request wherever
176
+ # its line was written.
177
+ @sent = Events.spans_of(inputs).each_with_index.to_h #: Hash[String, Integer]
178
+ @requests = [] #: Array[[Integer, Integer, Hash[String, untyped]]]
179
+ @counted = {} #: Hash[String, Hash[Symbol, untyped]]
180
+ @maybe = 0
181
+ end
182
+
183
+ # A readable line of `service`'s file whose trace is the fold's.
184
+ #: (Line, String) -> void
185
+ def owned(line, service)
186
+ @tally.owned(line, service)
187
+ @maybe += 1 if %w[undefined-kind unknown-version].include?(line.state)
188
+ if (request = Events.request_of(line))
189
+ span = line.trace&.span
190
+ order = span ? @sent.fetch(span, @sent.size) : @sent.size
191
+ @requests << [order, @requests.size, request.transform_keys(&:to_s).merge("service" => service)]
192
+ return
193
+ end
194
+ key = if line.state == "extension" && Events.own_kind?(line.kind) then Events.own_key_of(line.kind.to_s, line.object || {}, @own, service)
195
+ else Events.path_key_of(line, service)
196
+ end
197
+ return unless key
198
+
199
+ entry = @counted[Events.path_key_text(key)] ||= { key: key, service: service, count: 0, gates: [] }
200
+ entry[:count] += 1
201
+ flag = Events.flag_of(line)
202
+ entry[:gates] << flag[:gate] if flag && !entry[:gates].include?(flag[:gate])
203
+ end
204
+
205
+ # An unreadable line of `service`'s file whose text carries the
206
+ # fold's trace id.
207
+ #: (String) -> void
208
+ def damaged(service)
209
+ @tally.damaged(service)
210
+ @maybe += 1
211
+ end
212
+
213
+ # The path of the lines folded so far, with what each service's
214
+ # reader knows of its file.
215
+ #: (Hash[String, Hash[Symbol, bool]]) -> Hash[Symbol, untyped]
216
+ def path(files)
217
+ entries = @counted.values.sort { |a, b| Events.by_path_key(a[:key], b[:key]) }.map do |entry|
218
+ key = entry[:key]
219
+ next key.merge("count" => entry[:count]) if Events.own_kind?(key["kind"])
220
+ next key.merge("count" => entry[:count], "gates" => entry[:gates].sort { |a, b| Events.by_gate(a, b) }) if key["kind"] == "flag"
221
+
222
+ # A line is placed by the list of the service that wrote it.
223
+ holds = key["kind"] == "db" ? Events.mark_of(@tables[entry[:service]] || NO_MARKED_TABLES, key["table"]) : nil
224
+ key.merge("count" => entry[:count], **(holds ? { "holds" => holds } : {}))
225
+ end
226
+ { requests: @requests.sort_by { [_1[0], _1[1]] }.map(&:last), entries: entries, maybe: @maybe,
227
+ open: @tally.reading(files)[:open] }
228
+ end
229
+ end
230
+
231
+ # How many lines of `path` reported `key`.
232
+ #: (Hash[Symbol, untyped], Hash[String, untyped]) -> Integer
233
+ def self.count_of(path, key)
234
+ text = path_key_text(key)
235
+ return path[:requests].count { path_key_text(request_key(_1)) == text } if key["kind"] == "request"
236
+
237
+ path[:entries].find { path_key_text(_1) == text }&.[]("count") || 0
238
+ end
239
+
240
+ # Every key the paths hold, their requests' included, once each, in
241
+ # entry order.
242
+ #: (*Hash[Symbol, untyped]) -> Array[Hash[String, untyped]]
243
+ def self.keys_of(*paths)
244
+ keys = {} #: Hash[String, Hash[String, untyped]]
245
+ paths.each { |path| [*path[:entries].map { key_only(_1) }, *path[:requests].map { request_key(_1) }].each { keys[path_key_text(_1)] = _1 } }
246
+ keys.values.sort { |a, b| by_path_key(a, b) }
247
+ end
248
+
249
+ # What the table of `key` holds on `path`, when its entry is marked.
250
+ #: (Hash[Symbol, untyped], Hash[String, untyped]) -> String?
251
+ def self.holds_on(path, key)
252
+ return nil unless key["kind"] == "db"
253
+
254
+ text = path_key_text(key)
255
+ path[:entries].find { path_key_text(_1) == text }&.[]("holds")
256
+ end
257
+
258
+ # What PATH_KINDS says of a kind, apart from its fields.
259
+ #: (String) -> Hash[Symbol, untyped]
260
+ def self.kind_rule(kind) = own_kind?(kind) ? { compare: "count" } : PATH_KINDS.fetch(kind).except(:fields)
261
+
262
+ # The gaps that leave a count on `path` unbounded: the trace's,
263
+ # except that a step that sent nothing and has no line has none.
264
+ #: (Hash[Symbol, untyped]) -> Array[Hash[Symbol, untyped]]
265
+ def self.bounding_gaps(path)
266
+ empty = path[:entries].empty? && path[:requests].empty? && path[:maybe].zero?
267
+ return [] if empty && path[:open].size == 1 && path[:open][0][:gap] == "no-requests"
268
+
269
+ path[:open]
270
+ end
271
+
272
+ # The range the count of `key` on `path` lies in.
273
+ #: (Hash[Symbol, untyped], Hash[String, untyped]) -> Hash[Symbol, Integer?]
274
+ def self.range_on(path, key)
275
+ count = count_of(path, key)
276
+ gaps = bounding_gaps(path)
277
+ bounded = gaps.empty? || (key["kind"] == "request" && gaps.all? { _1[:gap] == "jobs-pending" })
278
+ range = { at_least: count, at_most: bounded ? count + path[:maybe] : nil }
279
+ return range if kind_rule(key["kind"])[:compare] == "count"
280
+
281
+ { at_least: [1, count].min, at_most: [1, range[:at_most] || 1].min }
282
+ end
283
+ end
284
+ end
285
+ end