Skip to main content

kanade_shared/
subject.rs

1pub const COMMANDS_ALL: &str = "commands.all";
2
3pub fn commands_group(name: &str) -> String {
4    format!("commands.group.{name}")
5}
6
7pub fn commands_pc(pc_id: &str) -> String {
8    format!("commands.pc.{pc_id}")
9}
10
11// `commands_exec` (subject `commands.exec.<job_id>`) was removed in
12// v0.22.1. The STREAM_EXEC stream now catches the existing
13// `commands.{all,group.X,pc.Y}` subjects directly, so the dedicated
14// per-exec subject isn't needed any more. See
15// `kanade-agent::command_replay` for how reconnecting agents catch
16// up on missed messages.
17
18pub fn results(request_id: &str) -> String {
19    format!("results.{request_id}")
20}
21
22pub fn heartbeat(pc_id: &str) -> String {
23    format!("heartbeat.{pc_id}")
24}
25
26/// `host_perf.<pc_id>` — Phase 1 of the perf telemetry pipeline. The
27/// agent publishes a whole-host CPU / Memory / Disk I/O / Network
28/// snapshot here on the cadence set by `host_perf_interval` in
29/// agent_config (default 60 s). Distinct subject from `heartbeat.<pc_id>`
30/// so the periodic heartbeat publisher stays untouched and pre-host_perf
31/// backends that don't subscribe simply ignore the new traffic.
32pub fn host_perf(pc_id: &str) -> String {
33    format!("host_perf.{pc_id}")
34}
35
36/// `process_perf.<pc_id>` — Phase 2: top-N per-process snapshot
37/// published only while `process_perf_enabled` is `true` AND the
38/// `process_perf_expires_at` deadline is in the future. Separate
39/// subject from `host_perf.<pc_id>` because process-perf is an
40/// opt-in investigation mode — having its own subject lets the
41/// projector skip the heavy table entirely for hosts that never
42/// turn it on.
43pub fn process_perf(pc_id: &str) -> String {
44    format!("process_perf.{pc_id}")
45}
46
47/// `obs.<pc_id>` — per-PC observability event stream (Issue #246).
48/// The agent publishes one [`crate::wire::ObsEvent`] per timeline
49/// event (sign-in / out, power on / off, sleep / resume, agent
50/// milestones, diagnostic bundle pointers). Distinct from
51/// `events.started.*` (in-flight script lifecycle) and
52/// `host_perf.<pc_id>` (numeric telemetry) — `obs.*` is the
53/// semantic-event stream the SPA Timeline page consumes.
54pub fn obs(pc_id: &str) -> String {
55    format!("obs.{pc_id}")
56}
57
58/// `obs.>` — filter the backend projector subscribes to so a new
59/// PC starts flowing into the timeline without any per-PC SUB
60/// registration. Pairs with [`obs`] for publish.
61pub const OBS_FILTER: &str = "obs.>";
62
63/// `kill.<exec_id>` — Spec §2.6 Layer 3 abort signal. The exec_id is
64/// the deployment / scheduler-fire UUID (formerly named `job_id`
65/// pre-v0.29; renamed for accuracy — every `Command.exec_id` is a
66/// per-deploy UUID, not a job-catalog id).
67pub fn kill(exec_id: &str) -> String {
68    format!("kill.{exec_id}")
69}
70
71pub fn inventory(pc_id: &str, category: &str) -> String {
72    format!("inventory.{pc_id}.{category}")
73}
74
75/// `events.started.<exec_id>.<pc_id>` — v0.30 / PR α' lifecycle
76/// event published by the agent just before spawning a script's
77/// child process. Lets the backend project an in-flight row into
78/// `execution_results` (with `finished_at = NULL`) so the SPA
79/// Activity table can show running rows alongside finished ones.
80/// Backend subscribes via [`EVENTS_STARTED_FILTER`].
81pub fn events_started(exec_id: &str, pc_id: &str) -> String {
82    format!("events.started.{exec_id}.{pc_id}")
83}
84
85/// Wildcard the backend events projector consumes on STREAM_EVENTS.
86/// Narrow (`events.started.>`) rather than the whole `events.>` so
87/// future event types can carry their own filters without rerouting
88/// the started subset.
89pub const EVENTS_STARTED_FILTER: &str = "events.started.>";
90
91pub const INVENTORY_HW: &str = "hw";
92pub const INVENTORY_SW: &str = "sw";
93pub const INVENTORY_NET: &str = "net";
94
95/// `logs.fetch.<pc_id>` — request/reply: operator (or backend) sends
96/// a `LogsRequest`; the addressed agent replies with the tail of its
97/// local log file. On-demand only, no stream.
98pub fn logs_fetch(pc_id: &str) -> String {
99    format!("logs.fetch.{pc_id}")
100}
101
102/// `agents.<pc_id>.ping` — v0.38 / #133 request/reply for the
103/// active "ping" round-trip. The agent answers with a fresh
104/// `Heartbeat` on demand instead of the backend waiting up to ~30 s
105/// for the next periodic heartbeat tick to land. Distinct subject
106/// from `heartbeat.<pc_id>` so the periodic publisher is unaffected
107/// and old agents that don't subscribe simply time the request out.
108pub fn ping(pc_id: &str) -> String {
109    format!("agents.{pc_id}.ping")
110}
111
112// v0.14: subject::inventory_request was retired alongside the
113// hardcoded inventory loop. On-demand collection now goes through
114// the normal exec path (`kanade exec configs/jobs/inventory-
115// hw.yaml`) — Command + ExecResult + the inventory-fact projector
116// give operators the same effect with no extra subject.
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn commands_all_constant() {
124        assert_eq!(COMMANDS_ALL, "commands.all");
125    }
126
127    #[test]
128    fn commands_group_formats_name() {
129        assert_eq!(commands_group("canary"), "commands.group.canary");
130        assert_eq!(commands_group("wave1"), "commands.group.wave1");
131    }
132
133    #[test]
134    fn commands_pc_formats_id() {
135        assert_eq!(commands_pc("minipc"), "commands.pc.minipc");
136        assert_eq!(commands_pc("PC1234"), "commands.pc.PC1234");
137    }
138
139    #[test]
140    fn results_formats_request_id() {
141        assert_eq!(results("req-1"), "results.req-1");
142    }
143
144    #[test]
145    fn heartbeat_formats_pc_id() {
146        assert_eq!(heartbeat("minipc"), "heartbeat.minipc");
147    }
148
149    #[test]
150    fn host_perf_formats_pc_id() {
151        assert_eq!(host_perf("minipc"), "host_perf.minipc");
152        assert_eq!(host_perf("PC1234"), "host_perf.PC1234");
153    }
154
155    #[test]
156    fn process_perf_formats_pc_id() {
157        assert_eq!(process_perf("minipc"), "process_perf.minipc");
158        assert_eq!(process_perf("PC1234"), "process_perf.PC1234");
159    }
160
161    #[test]
162    fn obs_formats_pc_id() {
163        assert_eq!(obs("minipc"), "obs.minipc");
164        assert_eq!(obs("PC1234"), "obs.PC1234");
165    }
166
167    #[test]
168    fn obs_filter_constant() {
169        assert_eq!(OBS_FILTER, "obs.>");
170    }
171
172    #[test]
173    fn kill_formats_exec_id() {
174        assert_eq!(kill("exec-uuid-1"), "kill.exec-uuid-1");
175    }
176
177    #[test]
178    fn logs_fetch_formats_pc_id() {
179        assert_eq!(logs_fetch("minipc"), "logs.fetch.minipc");
180    }
181
182    #[test]
183    fn ping_formats_pc_id() {
184        assert_eq!(ping("minipc"), "agents.minipc.ping");
185    }
186
187    #[test]
188    fn events_started_formats_exec_id_and_pc_id() {
189        assert_eq!(
190            events_started("exec-uuid-1", "minipc"),
191            "events.started.exec-uuid-1.minipc",
192        );
193    }
194
195    #[test]
196    fn events_started_filter_is_narrow_wildcard() {
197        assert_eq!(EVENTS_STARTED_FILTER, "events.started.>");
198    }
199
200    #[test]
201    fn inventory_formats_pc_id_and_category() {
202        assert_eq!(inventory("minipc", "hw"), "inventory.minipc.hw");
203        assert_eq!(inventory("minipc", INVENTORY_HW), "inventory.minipc.hw");
204        assert_eq!(inventory("minipc", INVENTORY_SW), "inventory.minipc.sw");
205        assert_eq!(inventory("minipc", INVENTORY_NET), "inventory.minipc.net");
206    }
207}