dash 4.1.3 → 4.2.1

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 (163) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dash/autoscale/connections/builder.rb +51 -0
  3. data/lib/dash/autoscale/connections.rb +102 -0
  4. data/lib/dash/autoscale/controller.rb +296 -0
  5. data/lib/dash/autoscale/cron.rb +75 -0
  6. data/lib/dash/autoscale/decision.rb +89 -0
  7. data/lib/dash/autoscale/duration.rb +19 -0
  8. data/lib/dash/autoscale/evaluation.rb +52 -0
  9. data/lib/dash/autoscale/guard.rb +48 -0
  10. data/lib/dash/autoscale/histogram.rb +48 -0
  11. data/lib/dash/autoscale/lease.rb +55 -0
  12. data/lib/dash/autoscale/lease_held.rb +2 -0
  13. data/lib/dash/autoscale/lease_lost.rb +2 -0
  14. data/lib/dash/autoscale/member.rb +28 -0
  15. data/lib/dash/autoscale/metrics.rb +70 -0
  16. data/lib/dash/autoscale/metrics_server.rb +71 -0
  17. data/lib/dash/autoscale/observation.rb +41 -0
  18. data/lib/dash/autoscale/pause.rb +34 -0
  19. data/lib/dash/autoscale/policy.rb +198 -0
  20. data/lib/dash/autoscale/pool.rb +103 -0
  21. data/lib/dash/autoscale/prometheus_client.rb +76 -0
  22. data/lib/dash/autoscale/provider/base.rb +43 -0
  23. data/lib/dash/autoscale/provider/exec.rb +94 -0
  24. data/lib/dash/autoscale/provider/upcloud.rb +140 -0
  25. data/lib/dash/autoscale/provider.rb +9 -0
  26. data/lib/dash/autoscale/provider_error.rb +4 -0
  27. data/lib/dash/autoscale/read_only_query.rb +38 -0
  28. data/lib/dash/autoscale/reader.rb +17 -0
  29. data/lib/dash/autoscale/recorder.rb +13 -0
  30. data/lib/dash/autoscale/role_state.rb +110 -0
  31. data/lib/dash/autoscale/sample.rb +18 -0
  32. data/lib/dash/autoscale/signal/pgbus.rb +19 -0
  33. data/lib/dash/autoscale/signal/prometheus.rb +8 -0
  34. data/lib/dash/autoscale/signal/proxy.rb +49 -0
  35. data/lib/dash/autoscale/signal/sidekiq.rb +49 -0
  36. data/lib/dash/autoscale/signal.rb +33 -0
  37. data/lib/dash/autoscale/simulation.rb +200 -0
  38. data/lib/dash/autoscale/state_store.rb +132 -0
  39. data/lib/dash/autoscale/timestamp.rb +15 -0
  40. data/lib/dash/autoscale/tunnel.rb +48 -0
  41. data/lib/dash/cli/accessory.rb +16 -0
  42. data/lib/dash/cli/app/boot.rb +69 -44
  43. data/lib/dash/cli/app/boot_state.rb +70 -0
  44. data/lib/dash/cli/app/rollout_boot.rb +28 -9
  45. data/lib/dash/cli/app/running_targets.rb +39 -0
  46. data/lib/dash/cli/app/slot_version.rb +29 -0
  47. data/lib/dash/cli/app.rb +89 -32
  48. data/lib/dash/cli/autoscale.rb +330 -0
  49. data/lib/dash/cli/base.rb +15 -3
  50. data/lib/dash/cli/lock.rb +6 -1
  51. data/lib/dash/cli/main.rb +77 -8
  52. data/lib/dash/cli/proxy/inspection.rb +52 -0
  53. data/lib/dash/cli/proxy/loadbalancer_deploy.rb +20 -0
  54. data/lib/dash/cli/proxy/loadbalancer_reboot.rb +1 -1
  55. data/lib/dash/cli/proxy/reboot.rb +4 -9
  56. data/lib/dash/cli/proxy.rb +12 -7
  57. data/lib/dash/cli/prune.rb +5 -3
  58. data/lib/dash/cli/report.rb +4 -1
  59. data/lib/dash/cli/scale/host_plan.rb +63 -0
  60. data/lib/dash/cli/scale/join.rb +128 -0
  61. data/lib/dash/cli/scale/leave.rb +58 -0
  62. data/lib/dash/cli/scale/placement.rb +54 -0
  63. data/lib/dash/cli/scale/power_off.rb +26 -0
  64. data/lib/dash/cli/scale/replica_join.rb +74 -0
  65. data/lib/dash/cli/scale/replica_leave.rb +121 -0
  66. data/lib/dash/cli/scale.rb +276 -0
  67. data/lib/dash/cli/server.rb +41 -0
  68. data/lib/dash/cli/stats_table.rb +34 -0
  69. data/lib/dash/cli/templates/sample_hooks/post-scale-in.sample +3 -0
  70. data/lib/dash/cli/templates/sample_hooks/post-scale-out.sample +3 -0
  71. data/lib/dash/cli/templates/sample_hooks/pre-scale-in.sample +6 -0
  72. data/lib/dash/cli/templates/sample_hooks/pre-scale-out.sample +6 -0
  73. data/lib/dash/commander.rb +17 -3
  74. data/lib/dash/commands/accessory/proxy.rb +1 -1
  75. data/lib/dash/commands/accessory.rb +21 -2
  76. data/lib/dash/commands/app/containers.rb +28 -3
  77. data/lib/dash/commands/app/logging.rb +13 -2
  78. data/lib/dash/commands/app/proxy.rb +4 -4
  79. data/lib/dash/commands/app.rb +90 -10
  80. data/lib/dash/commands/auditor.rb +2 -2
  81. data/lib/dash/commands/autoscale.rb +133 -0
  82. data/lib/dash/commands/base.rb +4 -0
  83. data/lib/dash/commands/proxy/services.rb +10 -0
  84. data/lib/dash/commands/prune.rb +10 -2
  85. data/lib/dash/commands/server.rb +16 -0
  86. data/lib/dash/configuration/accessory.rb +2 -2
  87. data/lib/dash/configuration/autoscale.rb +123 -0
  88. data/lib/dash/configuration/docs/autoscale.yml +142 -0
  89. data/lib/dash/configuration/docs/configuration.yml +8 -2
  90. data/lib/dash/configuration/docs/proxy.yml +8 -1
  91. data/lib/dash/configuration/docs/role.yml +238 -0
  92. data/lib/dash/configuration/loadbalancer.rb +3 -2
  93. data/lib/dash/configuration/proxy/run.rb +1 -1
  94. data/lib/dash/configuration/proxy.rb +26 -7
  95. data/lib/dash/configuration/role/drain.rb +49 -0
  96. data/lib/dash/configuration/role/replicas.rb +72 -0
  97. data/lib/dash/configuration/role/scale/guard.rb +40 -0
  98. data/lib/dash/configuration/role/scale/signal.rb +93 -0
  99. data/lib/dash/configuration/role/scale/window.rb +37 -0
  100. data/lib/dash/configuration/role/scale.rb +199 -0
  101. data/lib/dash/configuration/role.rb +123 -6
  102. data/lib/dash/configuration/validator/autoscale.rb +126 -0
  103. data/lib/dash/configuration/validator/proxy.rb +13 -0
  104. data/lib/dash/configuration/validator/role.rb +26 -5
  105. data/lib/dash/configuration.rb +131 -15
  106. data/lib/dash/diagnostics/audit.rb +28 -0
  107. data/lib/dash/diagnostics/autoscale_decisions.rb +18 -0
  108. data/lib/dash/diagnostics/autoscale_explain.rb +30 -0
  109. data/lib/dash/diagnostics/autoscale_signal.rb +82 -0
  110. data/lib/dash/diagnostics/base.rb +53 -0
  111. data/lib/dash/diagnostics/config.rb +32 -0
  112. data/lib/dash/diagnostics/container_stats.rb +67 -0
  113. data/lib/dash/diagnostics/containers.rb +20 -0
  114. data/lib/dash/diagnostics/controller_status.rb +44 -0
  115. data/lib/dash/diagnostics/docker_ps.rb +40 -0
  116. data/lib/dash/{cli → diagnostics}/doctor/config_checks.rb +3 -3
  117. data/lib/dash/diagnostics/doctor/controller_checks.rb +46 -0
  118. data/lib/dash/{cli → diagnostics}/doctor/endpoint_checks.rb +12 -4
  119. data/lib/dash/{cli → diagnostics}/doctor/host_checks.rb +5 -4
  120. data/lib/dash/diagnostics/doctor/pool_checks.rb +65 -0
  121. data/lib/dash/diagnostics/doctor/signal_checks.rb +78 -0
  122. data/lib/dash/diagnostics/doctor.rb +182 -0
  123. data/lib/dash/diagnostics/drift.rb +168 -0
  124. data/lib/dash/diagnostics/host_stats.rb +59 -0
  125. data/lib/dash/diagnostics/lines.rb +14 -0
  126. data/lib/dash/diagnostics/lock.rb +27 -0
  127. data/lib/dash/diagnostics/logs.rb +76 -0
  128. data/lib/dash/diagnostics/pool.rb +41 -0
  129. data/lib/dash/diagnostics/proxy_queue.rb +40 -0
  130. data/lib/dash/diagnostics/proxy_services.rb +36 -0
  131. data/lib/dash/diagnostics/redactor.rb +48 -0
  132. data/lib/dash/diagnostics/reports.rb +17 -0
  133. data/lib/dash/diagnostics/scale.rb +51 -0
  134. data/lib/dash/diagnostics/units.rb +31 -0
  135. data/lib/dash/mcp/base_tool.rb +43 -0
  136. data/lib/dash/mcp/runner.rb +28 -0
  137. data/lib/dash/mcp/server.rb +40 -0
  138. data/lib/dash/mcp/session.rb +51 -0
  139. data/lib/dash/mcp/tools/audit_tool.rb +11 -0
  140. data/lib/dash/mcp/tools/autoscale_decisions_tool.rb +22 -0
  141. data/lib/dash/mcp/tools/autoscale_explain_tool.rb +56 -0
  142. data/lib/dash/mcp/tools/config_tool.rb +10 -0
  143. data/lib/dash/mcp/tools/container_stats_tool.rb +15 -0
  144. data/lib/dash/mcp/tools/containers_tool.rb +10 -0
  145. data/lib/dash/mcp/tools/controller_status_tool.rb +18 -0
  146. data/lib/dash/mcp/tools/deploy_reports_tool.rb +10 -0
  147. data/lib/dash/mcp/tools/doctor_tool.rb +13 -0
  148. data/lib/dash/mcp/tools/drift_tool.rb +16 -0
  149. data/lib/dash/mcp/tools/host_stats_tool.rb +14 -0
  150. data/lib/dash/mcp/tools/lock_status_tool.rb +15 -0
  151. data/lib/dash/mcp/tools/logs_tool.rb +41 -0
  152. data/lib/dash/mcp/tools/pool_members_tool.rb +15 -0
  153. data/lib/dash/mcp/tools/proxy_services_tool.rb +10 -0
  154. data/lib/dash/mcp/tools/scale_status_tool.rb +10 -0
  155. data/lib/dash/mcp/tools/signal_sample_tool.rb +21 -0
  156. data/lib/dash/mcp/transport.rb +14 -0
  157. data/lib/dash/mcp.rb +56 -0
  158. data/lib/dash/sshkit_with_ext.rb +4 -4
  159. data/lib/dash/utils.rb +7 -0
  160. data/lib/dash/version.rb +1 -1
  161. data/lib/dash.rb +2 -0
  162. metadata +122 -5
  163. data/lib/dash/cli/doctor.rb +0 -113
@@ -0,0 +1,168 @@
1
+ # Compares what should be running with what is: the containers on each host against the
2
+ # targets dash-proxy routes to there, against the load balancer's targets. Pure - it reads
3
+ # the Containers, ProxyServices and Lock snapshots and touches nothing; `.take` captures
4
+ # them first.
5
+ #
6
+ # Each mismatch is `{ code:, host:, role:, detail: }`, with a code an agent can branch on:
7
+ #
8
+ # proxy_target_not_running the proxy routes to a container that is not running
9
+ # running_not_targeted a running container of a proxied role is not a target
10
+ # loadbalancer_target_missing a host the load balancer should forward to is not a target
11
+ # loadbalancer_target_extra the load balancer forwards to a host it should not
12
+ # version_mismatch a role's hosts run different versions
13
+ # multiple_running_versions one host runs more than one version of a role
14
+ # member_not_targeted a started pool member the load balancer does not forward to
15
+ # (reported instead of loadbalancer_target_missing for it)
16
+ # member_orphan a started pool member runs nothing of its role
17
+ #
18
+ # A host whose snapshot is an error is not compared; it is listed under `unread` instead,
19
+ # and the fleet is not called consistent.
20
+ # While the deploy lock is held the version codes are skipped too - a deploy in flight runs
21
+ # two versions on purpose - and `lock_held` says so (nil when the lock was not read).
22
+ class Dash::Diagnostics::Drift < Dash::Diagnostics::Base
23
+ FAILURES = %w[ proxy_target_not_running loadbalancer_target_missing ].freeze
24
+ REPLACED_SUFFIX = /_replaced_\h{16}\z/
25
+ TARGET_KEYS = %w[ targets reader_targets rollout_targets ].freeze
26
+
27
+ # The lock is only read when the primary host is in scope; narrowed away from it, the
28
+ # version codes are reported whether or not a deploy is in flight.
29
+ def self.take
30
+ lock = Dash::Diagnostics::Lock.new.to_h if DASH.hosts.include?(DASH.config.primary_host)
31
+
32
+ new(containers: Dash::Diagnostics::Containers.new(accessories: []).to_h, proxy_services: Dash::Diagnostics::ProxyServices.new.to_h, lock: lock)
33
+ end
34
+
35
+ def initialize(containers:, proxy_services:, lock: nil, config: nil)
36
+ @containers = containers
37
+ @proxy_services = proxy_services
38
+ @lock = lock
39
+ @config = config
40
+ end
41
+
42
+ def entries
43
+ @entries ||= proxy_entries + loadbalancer_entries + member_entries + version_entries
44
+ end
45
+
46
+ # The hosts whose containers or routes could not be read, so were not compared. A fleet
47
+ # with any is not reported consistent: no drift found is not the same as none there.
48
+ def unread
49
+ @unread ||= [ [ "containers", Array(@containers[:hosts]) ], [ "proxy_services", [ *@proxy_services[:hosts], @proxy_services[:loadbalancer] ].compact ] ]
50
+ .flat_map { |source, hosts| hosts.select { |host| host[:error] }.map { |host| { host: host[:host], source: source, error: host[:error] } } }
51
+ end
52
+
53
+ private
54
+ def snapshot
55
+ { consistent: entries.empty? && unread.empty?, lock_held: @lock&.dig(:lock, :held), drift: entries, unread: unread }
56
+ end
57
+
58
+ def proxy_entries
59
+ Array(@proxy_services[:hosts]).reject { |host| host[:error] }.flat_map do |proxy_host|
60
+ host = proxy_host[:host]
61
+ next [] unless running.key?(host)
62
+
63
+ proxied_roles_on(host).flat_map { |role| role_proxy_entries(role, host, targets(proxy_host[:services].to_h[role.container_prefix])) }
64
+ end
65
+ end
66
+
67
+ def role_proxy_entries(role, host, targets)
68
+ containers = running[host].select { |container| container[:role] == role.name }
69
+
70
+ not_running = targets.reject { |target| containers.any? { |container| container[:id].to_s.start_with?(target) } }.map do |target|
71
+ entry "proxy_target_not_running", host, role, "#{role.container_prefix} routes to #{target}, which is not a running container"
72
+ end
73
+
74
+ not_targeted = containers.reject { |container| targets.any? { |target| container[:id].to_s.start_with?(target) } }.map do |container|
75
+ entry "running_not_targeted", host, role, "#{container[:name]} (replica #{container[:replica]}) runs but #{role.container_prefix} does not route to it"
76
+ end
77
+
78
+ not_running + not_targeted
79
+ end
80
+
81
+ def loadbalancer_entries
82
+ loadbalancer = @proxy_services[:loadbalancer]
83
+ return [] if loadbalancer.nil? || loadbalancer[:error]
84
+
85
+ expected = Dash::Configuration::Loadbalancer.new(config: config, proxy_config: config.proxy.proxy_config, secrets: config.secrets).target_hosts.uniq
86
+ actual = targets(loadbalancer[:services].to_h[config.service])
87
+ host = loadbalancer[:host]
88
+
89
+ (expected - actual).map { |target| missing_target_entry(host, target) } +
90
+ (actual - expected).map { |target| entry "loadbalancer_target_extra", host, nil, "the load balancer forwards to #{target}, which no proxied role runs on" }
91
+ end
92
+
93
+ # A member that joined but never reached the load balancer serves nobody: capacity paid
94
+ # for and wasted, not a broken route - so it is its own code, not a failure.
95
+ def missing_target_entry(host, target)
96
+ if (member = started_members.find { |candidate| candidate.host == target })
97
+ role = config.roles.find { |candidate| candidate.name == member.role }
98
+ entry "member_not_targeted", host, role, "member #{member.id} (#{target}) is started but the load balancer does not forward to it"
99
+ else
100
+ entry "loadbalancer_target_missing", host, nil, "the load balancer does not forward to #{target}"
101
+ end
102
+ end
103
+
104
+ def member_entries
105
+ config.scaled_roles.flat_map do |role|
106
+ role.active_members.filter_map do |member|
107
+ next unless running.key?(member.host)
108
+ next if running[member.host].any? { |container| container[:role] == role.name }
109
+
110
+ entry "member_orphan", member.host, role, "member #{member.id} is started but runs no #{role} container"
111
+ end
112
+ end
113
+ end
114
+
115
+ def started_members
116
+ @started_members ||= config.scaled_roles.flat_map(&:active_members)
117
+ end
118
+
119
+ def version_entries
120
+ return [] if @lock&.dig(:lock, :held)
121
+
122
+ config.roles.flat_map do |role|
123
+ by_host = running.transform_values { |containers| versions(containers.select { |container| container[:role] == role.name }) }.reject { |_, versions| versions.empty? }
124
+
125
+ multiple = by_host.select { |_, versions| versions.size > 1 }.map do |host, versions|
126
+ entry "multiple_running_versions", host, role, "#{role} runs #{versions.join(", ")} on #{host}"
127
+ end
128
+
129
+ mismatch = if by_host.values.uniq.size > 1
130
+ [ entry("version_mismatch", nil, role, "#{role} runs " + by_host.map { |host, versions| "#{versions.join("+")} on #{host}" }.join(", ")) ]
131
+ end
132
+
133
+ multiple + mismatch.to_a
134
+ end
135
+ end
136
+
137
+ def config
138
+ @config ||= DASH.config
139
+ end
140
+
141
+ def running
142
+ @running ||= Array(@containers[:hosts]).reject { |host| host[:error] }.to_h do |host|
143
+ [ host[:host], Array(host[:containers]).select { |container| container[:state] == "running" } ]
144
+ end
145
+ end
146
+
147
+ def proxied_roles_on(host)
148
+ config.roles.select { |role| role.running_proxy? && role.hosts.include?(host) }
149
+ end
150
+
151
+ # dash-proxy lists targets as "<container id or host>:<port>"; the id is the short one
152
+ # dash deployed with, the host is what the load balancer forwards to.
153
+ def targets(service)
154
+ return [] unless service
155
+
156
+ listed = TARGET_KEYS.flat_map { |key| Array(service[key]) }
157
+ listed = service["target"].to_s.split(",") if listed.empty?
158
+ listed.map { |target| target.strip.sub(/:\d+\z/, "") }.reject(&:empty?).uniq
159
+ end
160
+
161
+ def versions(containers)
162
+ containers.map { |container| container[:version].to_s.sub(REPLACED_SUFFIX, "") }.uniq.sort
163
+ end
164
+
165
+ def entry(code, host, role, detail)
166
+ { code: code, host: host, role: role&.name, detail: detail }
167
+ end
168
+ end
@@ -0,0 +1,59 @@
1
+ # Per host, what the machine has left: load against its CPU count, memory and swap, disk
2
+ # for / and for Docker's data root, and uptime - one round trip of /proc reads and `df`.
3
+ #
4
+ # Each section is read on its own. A host whose Docker is down still reports its load,
5
+ # memory and root disk, with `docker_root: nil`; a section that does not parse is nil.
6
+ class Dash::Diagnostics::HostStats < Dash::Diagnostics::Base
7
+ KIB = 1024
8
+
9
+ def initialize(hosts: DASH.hosts)
10
+ @hosts = hosts
11
+ end
12
+
13
+ private
14
+ def snapshot
15
+ { hosts: per_host(@hosts) { |backend, _host| stats(backend.capture_with_info(*DASH.server.stats, raise_on_non_zero_exit: false)) } }
16
+ end
17
+
18
+ def stats(output)
19
+ loadavg, cpus, meminfo, df_root, df_docker, uptime = output.to_s.split(/^#{Regexp.escape(Dash::Commands::Base::SECTION_SEPARATOR)}$/).map(&:strip)
20
+ cpus = Integer(cpus.to_s, 10, exception: false)
21
+ meminfo = meminfo(meminfo)
22
+
23
+ { load: load(loadavg, cpus), cpus: cpus, memory: memory(meminfo), swap: swap(meminfo),
24
+ disk: { root: disk(df_root), docker_root: disk(df_docker) }, uptime_seconds: Float(uptime.to_s.split.first.to_s, exception: false)&.to_i }
25
+ end
26
+
27
+ # "0.52 0.48 0.40 2/345 6789"
28
+ def load(loadavg, cpus)
29
+ one, five, fifteen = loadavg.to_s.split.first(3).map { |value| Float(value, exception: false) }
30
+ return unless one && five && fifteen
31
+
32
+ { one: one, five: five, fifteen: fifteen, per_cpu: (cpus.to_i > 0 ? (one / cpus).round(2) : nil) }
33
+ end
34
+
35
+ # /proc/meminfo reports kB that are KiB.
36
+ def meminfo(section)
37
+ section.to_s.lines.to_h { |line| key, value = line.split(":", 2); [ key, value.to_s[/\d+/]&.to_i&.*(KIB) ] }
38
+ end
39
+
40
+ def memory(info)
41
+ total, available = info["MemTotal"], info["MemAvailable"]
42
+ return unless total && available && total > 0
43
+
44
+ { total_bytes: total, available_bytes: available, used_percent: ((total - available) * 100.0 / total).round(1) }
45
+ end
46
+
47
+ def swap(info)
48
+ { total_bytes: info["SwapTotal"], free_bytes: info["SwapFree"] } if info["SwapTotal"] && info["SwapFree"]
49
+ end
50
+
51
+ # `df -Pk`: a header, then "filesystem 1024-blocks used available capacity% mount".
52
+ def disk(section)
53
+ _, *numbers, capacity, mount = section.to_s.lines.last.to_s.split
54
+ blocks, used, available = numbers.map { |number| Integer(number.to_s, 10, exception: false) }
55
+ return unless numbers.size == 3 && blocks && used && available && capacity.to_s.match?(/\A\d+%\z/) && mount
56
+
57
+ { mount: mount, total_bytes: blocks * KIB, used_bytes: used * KIB, available_bytes: available * KIB, used_percent: capacity.to_f }
58
+ end
59
+ end
@@ -0,0 +1,14 @@
1
+ # The one bound on how many lines a diagnostic reads per host (audit, logs), so a large
2
+ # fleet or a chatty app cannot flood whoever asked - above all an agent's context.
3
+ module Dash::Diagnostics::Lines
4
+ MAX = 500
5
+
6
+ def self.bounded(lines)
7
+ count = Integer(lines.to_s, 10)
8
+ raise ArgumentError, "lines must be between 1 and #{MAX}, got #{lines}" unless count.between?(1, MAX)
9
+
10
+ count
11
+ rescue TypeError
12
+ raise ArgumentError, "lines must be a number, got #{lines.inspect}"
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ # Whether this destination's deploy lock is held on the primary host, and by whom, since
2
+ # when, at which version and why.
3
+ class Dash::Diagnostics::Lock < Dash::Diagnostics::Base
4
+ # "Locked by: Jane at 2026-10-05T10:00:00Z", "Version: 999", "Message: ..."
5
+ LOCKED_BY = /\ALocked by: (?<locked_by>.*) at (?<locked_at>\S+)\z/
6
+
7
+ # The lock lives on the configured primary host whatever --hosts narrows to; asking any
8
+ # other host would answer "not held" for a lock that is.
9
+ def initialize(host: DASH.config.primary_host)
10
+ @host = host
11
+ end
12
+
13
+ private
14
+ def snapshot
15
+ { lock: per_host([ @host ]) { |backend, _host| details(backend.capture_with_info(*DASH.lock.status, raise_on_non_zero_exit: false)) }.first }
16
+ end
17
+
18
+ # The status command fails, and prints nothing, when there is no lock directory.
19
+ def details(output)
20
+ return { held: false } if output.blank?
21
+
22
+ fields = output.lines.filter_map { |line| line.chomp.split(": ", 2) if line.include?(": ") }.to_h
23
+ locked_by = LOCKED_BY.match("Locked by: #{fields["Locked by"]}")
24
+
25
+ { held: true, locked_by: locked_by&.[](:locked_by), locked_at: locked_by&.[](:locked_at), version: fields["Version"], message: fields["Message"] }
26
+ end
27
+ end
@@ -0,0 +1,76 @@
1
+ require "securerandom"
2
+
3
+ # The tail of a role's containers on its hosts, every replica slot - or of an accessory's
4
+ # container on its hosts - for `dash mcp`.
5
+ #
6
+ # Built for input nobody vetted: an agent chooses the role, hosts, `lines`, `since` and
7
+ # `grep`. The role and hosts are looked up in the configuration, `lines` is bounded,
8
+ # `since` must look like a duration or a timestamp, and `grep` never reaches a shell at
9
+ # all - it is a plain substring match, applied here, after the capture.
10
+ class Dash::Diagnostics::Logs < Dash::Diagnostics::Base
11
+ # "42m", "1h30m", "2026-10-05", "2026-10-05T10:00:00Z", "2026-10-05T10:00:00+02:00"
12
+ SINCE = /\A(?:(?:\d+[smh])+|\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})?)?)\z/
13
+ MAX_GREP_LENGTH = 200
14
+
15
+ # With a redactor, lines are redacted before grep sees them: matching raw lines would let a
16
+ # grep probe a secret one guessed character at a time.
17
+ # Either a role (every replica slot) or an accessory (its one container on each host).
18
+ def initialize(role: nil, accessory: nil, hosts: nil, lines: 100, since: nil, grep: nil, redactor: nil)
19
+ @redactor = redactor
20
+ @accessory = accessory
21
+ @role = role || (DASH.primary_role unless accessory)
22
+ in_scope = accessory ? accessory.hosts & DASH.accessory_hosts : @role.hosts & DASH.hosts
23
+ @hosts = hosts ? hosts & in_scope : in_scope
24
+ @lines = Dash::Diagnostics::Lines.bounded(lines)
25
+ @since = validated_since(since)
26
+ @grep = validated_grep(grep)
27
+ end
28
+
29
+ private
30
+ def snapshot
31
+ if @accessory
32
+ { accessory: @accessory.name, hosts: per_host(@hosts) { |backend, _host| { lines: accessory_lines(backend) } } }
33
+ else
34
+ { role: @role.name, hosts: per_host(@hosts) { |backend, host| { replicas: replicas(backend, host) } } }
35
+ end
36
+ end
37
+
38
+ def accessory_lines(backend)
39
+ output = backend.capture_with_info(*DASH.accessory(@accessory.name).logs(lines: @lines, since: @since), raise_on_non_zero_exit: false)
40
+ matching(output.lines.map { |line| redacted(line.chomp) })
41
+ end
42
+
43
+ # One capture for every slot on the host, split back apart on a separator made for this
44
+ # read alone: a fixed one could be printed by the app to shift lines between slots.
45
+ def replicas(backend, host)
46
+ separator = "--dash-replica-#{SecureRandom.hex(8)}--"
47
+ command = DASH.app(role: @role, host: host).replica_logs(separator: separator, lines: @lines, since: @since)
48
+ slots = backend.capture_with_info(*command, raise_on_non_zero_exit: false).split(/^#{Regexp.escape(separator)}\n?/, -1).drop(1)
49
+
50
+ @role.replica_numbers.each_with_index.map do |replica, index|
51
+ { replica: replica, lines: matching(slots[index].to_s.lines.map { |line| redacted(line.chomp) }) }
52
+ end
53
+ end
54
+
55
+ def redacted(line)
56
+ @redactor ? @redactor.redact_text(line) : line
57
+ end
58
+
59
+ def matching(lines)
60
+ @grep ? lines.select { |line| line.include?(@grep) } : lines
61
+ end
62
+
63
+ def validated_since(since)
64
+ return if since.blank?
65
+ raise ArgumentError, "since must be a duration like 42m or 1h30m, or a timestamp like 2026-10-05T10:00:00Z, got #{since.inspect}" unless since.to_s.match?(SINCE)
66
+
67
+ since.to_s
68
+ end
69
+
70
+ def validated_grep(grep)
71
+ return if grep.blank?
72
+ raise ArgumentError, "grep must be at most #{MAX_GREP_LENGTH} characters" if grep.to_s.length > MAX_GREP_LENGTH
73
+
74
+ grep.to_s
75
+ end
76
+ end
@@ -0,0 +1,41 @@
1
+ # The pool members of every scaled role, for `dash scale status --json` and the MCP
2
+ # `pool_members` tool: per role its provider and bounds, and per member its provider id,
3
+ # host, state, labels and whether the provider vouched for it (`verified: false` is a host
4
+ # taken from --hosts while the provider was down). Started members are read for the
5
+ # versions they run; one running nothing of the role is an `orphan`. Provider credentials
6
+ # never reach this: members carry labels, not the request that listed them.
7
+ class Dash::Diagnostics::Pool < Dash::Diagnostics::Base
8
+ def initialize(roles: Dash::Diagnostics::Doctor.scoped_roles.select(&:scaled?))
9
+ @roles = roles
10
+ end
11
+
12
+ private
13
+ def snapshot
14
+ { roles: @roles.map { |role| role_pool(role) } }
15
+ end
16
+
17
+ def role_pool(role)
18
+ head = { role: role.name, provider: DASH.config.autoscale.provider_name, members: role.scale.members.to_s,
19
+ min: role.scale.min, max: role.scale.max, baseline: in_scope(role.baseline_hosts) }
20
+ members = role.members.select { |member| in_scope([ member.host ]).any? }
21
+ read = per_host(members.select(&:started?).map(&:host)) do |backend, host|
22
+ { versions: Dash::Diagnostics::Scale.replicas(role, backend.capture_with_info(*DASH.app(role: role, host: host).replica_status)).map { |replica| replica[:version] }.uniq }
23
+ end.index_by { |entry| entry[:host] }
24
+
25
+ head.merge(pool: members.map { |member| member_entry(member, read[member.host]) })
26
+ rescue Dash::Autoscale::ProviderError => e
27
+ head.merge(error: e.message)
28
+ end
29
+
30
+ # The run's --hosts, read directly: DASH.hosts would expand every role's hosts, and one
31
+ # other pool that cannot be read would fail this role too.
32
+ def in_scope(hosts)
33
+ DASH.specific_hosts ? hosts & DASH.specific_hosts : hosts
34
+ end
35
+
36
+ def member_entry(member, read)
37
+ entry = { id: member.id, host: member.host, state: member.state, labels: member.labels, verified: member.verified,
38
+ versions: read&.fetch(:versions, nil) || [], orphan: !!(read && read[:versions]&.empty?) }
39
+ read&.key?(:error) ? entry.merge(error: read[:error]) : entry
40
+ end
41
+ end
@@ -0,0 +1,40 @@
1
+ # What each host's dash-proxy measured of a role's request queue time: per target, the
2
+ # trailing window `dash-proxy list --json` reports (v1.1.0.5 on) under `target_details`.
3
+ # Read over SSH like Dash::Diagnostics::ProxyServices; the proxy signal
4
+ # (Dash::Autoscale::Signal::Proxy) merges it into the fleet's p95.
5
+ #
6
+ # A host whose proxy cannot be read, does not route the role, or predates the window is
7
+ # that host's error; the other hosts still answer.
8
+ class Dash::Diagnostics::ProxyQueue < Dash::Diagnostics::Base
9
+ # The role's targets in one `dash-proxy list --json` output, active and rollout slots
10
+ # alike (both serve requests). Buckets are [ [ upper bound ms, cumulative count ], ... ].
11
+ #
12
+ # dash-proxy omits empty lists, so a service with no targets has neither `targets` nor
13
+ # `target_details`; only targets without details is a proxy older than the window.
14
+ def self.targets(output, service:, host:)
15
+ routed = Dash::Commands::Proxy::Services.parse(output)[service] or raise "dash-proxy on #{host} routes no #{service}"
16
+ details = routed.fetch("target_details") do
17
+ raise "dash-proxy on #{host} reports no queue window (it predates v1.1.0.5), run dash proxy reboot" if Array(routed["targets"]).any?
18
+
19
+ []
20
+ end
21
+
22
+ details.map do |target|
23
+ queue = target.fetch("queue")
24
+
25
+ { address: target["address"], slot: target["slot"], state: target["state"], in_flight: target["in_flight"],
26
+ window_seconds: queue["window_seconds"], requests: queue.fetch("requests"), p95_ms: queue["p95_ms"], max_ms: queue["max_ms"], app: queue["app"], proxy: queue["proxy"],
27
+ buckets: queue.fetch("buckets").map { |bucket| [ bucket.fetch("le_ms"), bucket.fetch("count") ] } }
28
+ end
29
+ end
30
+
31
+ def initialize(role:)
32
+ @role = role
33
+ end
34
+
35
+ private
36
+ def snapshot
37
+ { role: @role.name,
38
+ hosts: per_host(@role.hosts) { |backend, host| { targets: self.class.targets(backend.capture_with_info(*DASH.proxy(host).list(json: true)), service: @role.container_prefix, host: host) } } }
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ # Per proxy host, what dash-proxy routes for this deploy (`dash-proxy list --json`), and
2
+ # the load balancer's routes when load balancing is on.
3
+ #
4
+ # Only this deploy's own services are kept: a proxy host serves every app and destination
5
+ # deployed to it, and the others are neither this deploy's business nor worth handing to
6
+ # an agent.
7
+ class Dash::Diagnostics::ProxyServices < Dash::Diagnostics::Base
8
+ def initialize(hosts: DASH.proxy_hosts)
9
+ @hosts = hosts
10
+ end
11
+
12
+ private
13
+ def snapshot
14
+ { hosts: per_host(@hosts) { |backend, host| { services: own(services(backend, DASH.proxy(host).list(json: true)), host) } },
15
+ loadbalancer: loadbalancer }
16
+ end
17
+
18
+ def loadbalancer
19
+ if DASH.config.proxy.load_balancing? && (host = DASH.config.proxy.effective_loadbalancer)
20
+ per_host([ host ]) { |backend, _host| { services: services(backend, DASH.loadbalancer.list(json: true)).slice(DASH.config.service) } }.first
21
+ end
22
+ end
23
+
24
+ def services(backend, command)
25
+ Dash::Commands::Proxy::Services.parse(backend.capture_with_info(*command))
26
+ end
27
+
28
+ def own(services, host)
29
+ services.slice(*service_names(host))
30
+ end
31
+
32
+ def service_names(host)
33
+ DASH.config.roles.select { |role| role.running_proxy? && role.hosts.include?(host) }.map(&:container_prefix) +
34
+ DASH.config.accessories.select { |accessory| accessory.running_proxy? && accessory.hosts.include?(host) }.map(&:service_name)
35
+ end
36
+ end
@@ -0,0 +1,48 @@
1
+ # Blanks credentials out of a diagnostic before it leaves the process for an agent.
2
+ #
3
+ # Two passes, because neither is enough alone:
4
+ #
5
+ # - By key: any value under a key that names a credential (`password`, `token`, `secret`,
6
+ # `key`) or a secret from `.dash/secrets` becomes "[REDACTED]", whole subtree and all.
7
+ # This over-redacts on purpose (`ssh_options.keys`, any `*_key` setting) - a hidden
8
+ # setting costs a question, a leaked one costs a rotation.
9
+ # - By value: every secret value from `.dash/secrets` is replaced wherever it appears
10
+ # inside any string, so a password embedded in a `DATABASE_URL`, an audit line, a log
11
+ # line or an SSH error message is caught too. Values shorter than MINIMUM_VALUE_LENGTH
12
+ # are left to the key pass, or a secret like "1" would blank every number.
13
+ class Dash::Diagnostics::Redactor
14
+ REDACTED = "[REDACTED]"
15
+ CREDENTIAL_KEY = /password|token|secret|key/i
16
+ MINIMUM_VALUE_LENGTH = 6
17
+
18
+ # Resolving the secrets runs any command substitution in `.dash/secrets` (a password
19
+ # manager), so build this once per process, not per question.
20
+ def self.for(config)
21
+ new(secrets: config.secrets.to_h)
22
+ end
23
+
24
+ def initialize(secrets: {})
25
+ @names = secrets.keys.map(&:to_s).to_set
26
+ values = secrets.values.map(&:to_s).select { |value| value.length >= MINIMUM_VALUE_LENGTH }.uniq
27
+ @pattern = Regexp.union(values.sort_by { |value| -value.length }) if values.any?
28
+ end
29
+
30
+ def redact(value)
31
+ case value
32
+ when Dash::Utils::Sensitive then value.redaction
33
+ when Hash then value.to_h { |key, nested| [ key, credential_key?(key) ? REDACTED : redact(nested) ] }
34
+ when Array then value.map { |element| redact(element) }
35
+ when String then redact_text(value)
36
+ else value
37
+ end
38
+ end
39
+
40
+ def redact_text(text)
41
+ @pattern ? text.to_s.gsub(@pattern, REDACTED) : text.to_s
42
+ end
43
+
44
+ private
45
+ def credential_key?(key)
46
+ key.to_s.match?(CREDENTIAL_KEY) || @names.include?(key.to_s)
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ # The deploy reports saved under `.dash/reports` for this destination, most recent first.
2
+ # Local and read-only: no SSH at all.
3
+ class Dash::Diagnostics::Reports < Dash::Diagnostics::Base
4
+ MAX = 20
5
+
6
+ def initialize(last: 1, directory: Dash::ProjectDirectory.join("reports"))
7
+ @last = Integer(last.to_s, 10)
8
+ raise ArgumentError, "last must be between 1 and #{MAX}, got #{last}" unless @last.between?(1, MAX)
9
+
10
+ @directory = directory
11
+ end
12
+
13
+ private
14
+ def snapshot
15
+ { reports: Dash::Report::History.new(@directory, destination: DASH.config.destination).recent(@last) }
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ # The shape of `dash scale status --json`: per role its bounds, its total, and per host each
2
+ # replica's slot, version and docker status - one `docker ps` per host. A host that cannot
3
+ # be read is listed under the role's `unread` rather than shown as running nothing.
4
+ #
5
+ # A scaled role adds its scale bounds and its pool members; a started member whose host
6
+ # lists no replica of the role is an `orphan`. A pool that cannot be read is the role's
7
+ # `error`, so one provider outage does not hide the other roles.
8
+ class Dash::Diagnostics::Scale < Dash::Diagnostics::Base
9
+ # `docker ps` lines of "<name>\t<status>"; names that are not one of the role's slots drop out.
10
+ def self.replicas(role, output)
11
+ output.lines.filter_map do |line|
12
+ name, status = line.chomp.split("\t", 2)
13
+ if name.present? && (replica = role.replica_from_name(name))
14
+ { replica: replica, version: role.version_from_name(name), status: status.to_s.strip }
15
+ end
16
+ end.sort_by { |replica| replica[:replica] }
17
+ end
18
+
19
+ def initialize(roles: DASH.roles)
20
+ @roles = roles
21
+ end
22
+
23
+ private
24
+ def snapshot
25
+ { roles: @roles.map { |role| role_status(role) } }
26
+ end
27
+
28
+ def role_status(role)
29
+ read = per_host(in_scope(role.hosts)) { |backend, host| { replicas: self.class.replicas(role, backend.capture_with_info(*DASH.app(role: role, host: host).replica_status)) } }
30
+ hosts = read.reject { |host| host[:error] }.to_h { |host| [ host[:host], host[:replicas] ] }
31
+
32
+ status = { role: role.name, min: role.replicas.min, max: role.replicas.max, total: hosts.values.sum(&:size),
33
+ unread: read.select { |host| host[:error] }, hosts: hosts }
34
+ status.merge!(scale: { min: role.scale.min, max: role.scale.max }, members: members(role, hosts)) if role.scaled?
35
+ status
36
+ rescue Dash::Autoscale::ProviderError => e
37
+ { role: role.name, error: e.message }
38
+ end
39
+
40
+ # The run's --hosts, read directly: DASH.hosts would expand every role's hosts, and one
41
+ # other pool that cannot be read would fail this role too.
42
+ def in_scope(hosts)
43
+ DASH.specific_hosts ? hosts & DASH.specific_hosts : hosts
44
+ end
45
+
46
+ def members(role, hosts)
47
+ role.members.map do |member|
48
+ { id: member.id, host: member.host, state: member.state, orphan: member.started? && hosts.key?(member.host) && hosts[member.host].empty? }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ # Reads the units `docker stats` prints. Memory comes in binary units ("120MiB / 1.9GiB"),
2
+ # network and block I/O in decimal ones ("1.2kB / 0B"), CPU as "1.23%". Anything else
3
+ # reads as nil, so a caller keeps the raw string rather than a wrong number.
4
+ module Dash::Diagnostics::Units
5
+ MULTIPLIERS = {
6
+ "B" => 1,
7
+ "kB" => 1000, "KB" => 1000, "MB" => 1000**2, "GB" => 1000**3, "TB" => 1000**4,
8
+ "KiB" => 1024, "MiB" => 1024**2, "GiB" => 1024**3, "TiB" => 1024**4
9
+ }.freeze
10
+
11
+ QUANTITY = /\A(\d+(?:\.\d+)?)\s*([kKMGT]i?B|B)\z/
12
+
13
+ module_function
14
+
15
+ def bytes(value)
16
+ match = QUANTITY.match(value.to_s.strip)
17
+ multiplier = match && MULTIPLIERS[match[2]]
18
+
19
+ (match[1].to_f * multiplier).round if multiplier
20
+ end
21
+
22
+ # "used / limit" or "in / out", as two byte counts.
23
+ def pair(value)
24
+ first, second = value.to_s.split("/", 2)
25
+ [ bytes(first), bytes(second) ]
26
+ end
27
+
28
+ def percent(value)
29
+ Float(value.to_s.strip.delete_suffix("%"), exception: false)
30
+ end
31
+ end
@@ -0,0 +1,43 @@
1
+ require "json"
2
+
3
+ # Every dash MCP tool: read-only by annotation, answered through the Session, and
4
+ # redacted at this boundary so a tool author cannot leak a secret by forgetting to.
5
+ #
6
+ # Write tools (scale, deploy, lock, reboot, exec) are out of scope. If they ever exist
7
+ # they get their own opt-in surface and never inherit from this class.
8
+ class Dash::Mcp::BaseTool < ::MCP::Tool
9
+ annotations read_only_hint: true, destructive_hint: false, idempotent_hint: true, open_world_hint: false
10
+
11
+ # The filters every SSH-backed tool takes. Values are matched against the configured
12
+ # hosts and roles, so nothing an agent passes reaches a shell.
13
+ SCOPE = {
14
+ hosts: { type: "array", items: { type: "string" }, description: "Only these hosts (* wildcards allowed), within the server's --hosts" },
15
+ roles: { type: "array", items: { type: "string" }, description: "Only these roles (* wildcards allowed), within the server's --roles" }
16
+ }.freeze
17
+
18
+ class << self
19
+ # MCP::Tool.inherited resets the annotations on every subclass; fall back to the
20
+ # nearest ancestor's so the read-only contract reaches every tool.
21
+ def annotations_value
22
+ super || (superclass.annotations_value if superclass.respond_to?(:annotations_value))
23
+ end
24
+
25
+ def answer(server_context, hosts: nil, roles: nil, &question)
26
+ session = server_context.fetch(:session)
27
+ json_response(session.answer(hosts: hosts, roles: roles, &question), session.redactor)
28
+ rescue ArgumentError, Dash::ConfigurationError, Psych::SyntaxError => e
29
+ error_response(session.redactor.redact_text(e.message))
30
+ rescue StandardError => e
31
+ # An SSHKit failure's message carries the whole command and its stderr.
32
+ error_response(session.redactor.redact_text("#{e.class}: #{e.message}"))
33
+ end
34
+
35
+ def json_response(value, redactor)
36
+ ::MCP::Tool::Response.new([ { type: "text", text: JSON.generate(redactor.redact(value)) } ])
37
+ end
38
+
39
+ def error_response(message)
40
+ ::MCP::Tool::Response.new([ { type: "text", text: message } ], error: true)
41
+ end
42
+ end
43
+ end