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/// `notifications.all` — broadcast end-user notification (SPEC
12/// §2.2.1 / Phase E). Mirrors [`COMMANDS_ALL`] but on the
13/// notification fan-out plane: the backend publishes here, the
14/// `NOTIFICATIONS` stream retains it, and every agent forwards it to
15/// the Client Apps in matching user sessions.
16pub const NOTIFICATIONS_ALL: &str = "notifications.all";
17
18/// `notifications.group.{group_name}` — group-scoped end-user
19/// notification. Sibling of [`commands_group`] on the notification
20/// plane.
21pub fn notifications_group(name: &str) -> String {
22 format!("notifications.group.{name}")
23}
24
25/// `notifications.pc.{pc_id}` — single-PC end-user notification.
26/// Sibling of [`commands_pc`] on the notification plane.
27pub fn notifications_pc(pc_id: &str) -> String {
28 format!("notifications.pc.{pc_id}")
29}
30
31/// `events.notifications.acked.{pc_id}.{user_sid}.{notif_id}` — the
32/// agent publishes this when a user clicks "確認" on a notification
33/// (SPEC §2.2.2 / Phase E). The `{user_sid}` segment distinguishes
34/// concurrent users on a shared PC (Fast User Switching / RDP). Lives
35/// under `events.>` so the existing `EVENTS` stream retains it; the
36/// backend's notification-acks projector consumes the narrowed
37/// [`EVENTS_NOTIFICATIONS_ACKED_FILTER`] to build the SPA's
38/// per-recipient confirmation view.
39///
40/// Subject spelling is fixed by SPEC §2.2.2 / §2.12.8 (`events.>`), so
41/// acks ride the `EVENTS` stream's retention (shorter than the 90-day
42/// `NOTIFICATIONS` history), not the notification stream's. That only
43/// bounds **re-projection** after a `-WipeDb`: the durable source of
44/// truth for ack_status is the `notification_acks` SQLite table, which
45/// persists independently — so a live fleet keeps full ack history;
46/// only a DB wipe truncates re-derivable acks to the EVENTS window,
47/// the same limitation every `events.*`-projected table already has.
48pub fn events_notifications_acked(pc_id: &str, user_sid: &str, notif_id: &str) -> String {
49 format!("events.notifications.acked.{pc_id}.{user_sid}.{notif_id}")
50}
51
52// `commands_exec` (subject `commands.exec.<job_id>`) was removed in
53// v0.22.1. The STREAM_EXEC stream now catches the existing
54// `commands.{all,group.X,pc.Y}` subjects directly, so the dedicated
55// per-exec subject isn't needed any more. See
56// `kanade-agent::command_replay` for how reconnecting agents catch
57// up on missed messages.
58
59pub fn results(request_id: &str) -> String {
60 format!("results.{request_id}")
61}
62
63pub fn heartbeat(pc_id: &str) -> String {
64 format!("heartbeat.{pc_id}")
65}
66
67/// `host_perf.<pc_id>` — Phase 1 of the perf telemetry pipeline. The
68/// agent publishes a whole-host CPU / Memory / Disk I/O / Network
69/// snapshot here on the cadence set by `host_perf_interval` in
70/// agent_config (default 60 s). Distinct subject from `heartbeat.<pc_id>`
71/// so the periodic heartbeat publisher stays untouched and pre-host_perf
72/// backends that don't subscribe simply ignore the new traffic.
73pub fn host_perf(pc_id: &str) -> String {
74 format!("host_perf.{pc_id}")
75}
76
77/// `process_perf.<pc_id>` — Phase 2: top-N per-process snapshot
78/// published only while `process_perf_enabled` is `true` AND the
79/// `process_perf_expires_at` deadline is in the future. Separate
80/// subject from `host_perf.<pc_id>` because process-perf is an
81/// opt-in investigation mode — having its own subject lets the
82/// projector skip the heavy table entirely for hosts that never
83/// turn it on.
84pub fn process_perf(pc_id: &str) -> String {
85 format!("process_perf.{pc_id}")
86}
87
88/// `obs.<pc_id>` — per-PC observability event stream (Issue #246).
89/// The agent publishes one [`crate::wire::ObsEvent`] per timeline
90/// event (sign-in / out, power on / off, sleep / resume, agent
91/// milestones, diagnostic bundle pointers). Distinct from
92/// `events.started.*` (in-flight script lifecycle) and
93/// `host_perf.<pc_id>` (numeric telemetry) — `obs.*` is the
94/// semantic-event stream the SPA Timeline page consumes.
95pub fn obs(pc_id: &str) -> String {
96 format!("obs.{pc_id}")
97}
98
99/// `obs.>` — filter the backend projector subscribes to so a new
100/// PC starts flowing into the timeline without any per-PC SUB
101/// registration. Pairs with [`obs`] for publish.
102pub const OBS_FILTER: &str = "obs.>";
103
104/// `kill.<exec_id>` — Spec §2.6 Layer 3 abort signal. The exec_id is
105/// the deployment / scheduler-fire UUID (formerly named `job_id`
106/// pre-v0.29; renamed for accuracy — every `Command.exec_id` is a
107/// per-deploy UUID, not a job-catalog id).
108pub fn kill(exec_id: &str) -> String {
109 format!("kill.{exec_id}")
110}
111
112pub fn inventory(pc_id: &str, category: &str) -> String {
113 format!("inventory.{pc_id}.{category}")
114}
115
116/// `events.started.<exec_id>.<pc_id>` — v0.30 / PR α' lifecycle
117/// event published by the agent just before spawning a script's
118/// child process. Lets the backend project an in-flight row into
119/// `execution_results` (with `finished_at = NULL`) so the SPA
120/// Activity table can show running rows alongside finished ones.
121/// Backend subscribes via [`EVENTS_STARTED_FILTER`].
122pub fn events_started(exec_id: &str, pc_id: &str) -> String {
123 format!("events.started.{exec_id}.{pc_id}")
124}
125
126/// Wildcard the backend events projector consumes on STREAM_EVENTS.
127/// Narrow (`events.started.>`) rather than the whole `events.>` so
128/// future event types can carry their own filters without rerouting
129/// the started subset.
130pub const EVENTS_STARTED_FILTER: &str = "events.started.>";
131
132/// Wildcard the backend notification-acks projector consumes on
133/// `STREAM_EVENTS`. Narrow (`events.notifications.acked.>`) rather
134/// than the whole `events.>` so the projector only wakes for ack
135/// events and not the high-volume `events.started.*` lifecycle
136/// traffic (which the events projector handles separately).
137pub const EVENTS_NOTIFICATIONS_ACKED_FILTER: &str = "events.notifications.acked.>";
138
139pub const INVENTORY_HW: &str = "hw";
140pub const INVENTORY_SW: &str = "sw";
141pub const INVENTORY_NET: &str = "net";
142
143/// `logs.fetch.<pc_id>` — request/reply: operator (or backend) sends
144/// a `LogsRequest`; the addressed agent replies with the tail of its
145/// local log file. On-demand only, no stream.
146pub fn logs_fetch(pc_id: &str) -> String {
147 format!("logs.fetch.{pc_id}")
148}
149
150/// `job.tail.<pc_id>` — request/reply for the live tail of a
151/// still-running job's stdout/stderr. The operator (or backend, on
152/// the SPA's behalf) sends a [`crate::wire::JobTailRequest`] carrying
153/// the `result_id`; the addressed agent replies with the current
154/// ring-buffer tail from its in-memory live registry. On-demand only,
155/// no stream — the SPA polls this every few seconds (same shape as
156/// `logs.fetch.<pc_id>`) while a job is in flight. Distinct subject
157/// from `logs.fetch.<pc_id>` (whole-agent log file) because this is
158/// scoped to a single job's captured output, not the agent's log.
159pub fn job_tail(pc_id: &str) -> String {
160 format!("job.tail.{pc_id}")
161}
162
163/// `agents.<pc_id>.ping` — v0.38 / #133 request/reply for the
164/// active "ping" round-trip. The agent answers with a fresh
165/// `Heartbeat` on demand instead of the backend waiting up to ~30 s
166/// for the next periodic heartbeat tick to land. Distinct subject
167/// from `heartbeat.<pc_id>` so the periodic publisher is unaffected
168/// and old agents that don't subscribe simply time the request out.
169pub fn ping(pc_id: &str) -> String {
170 format!("agents.{pc_id}.ping")
171}
172
173// v0.14: subject::inventory_request was retired alongside the
174// hardcoded inventory loop. On-demand collection now goes through
175// the normal exec path (`kanade exec configs/jobs/inventory-
176// hw.yaml`) — Command + ExecResult + the inventory-fact projector
177// give operators the same effect with no extra subject.
178
179#[cfg(test)]
180mod tests {
181 use super::*;
182
183 #[test]
184 fn commands_all_constant() {
185 assert_eq!(COMMANDS_ALL, "commands.all");
186 }
187
188 #[test]
189 fn commands_group_formats_name() {
190 assert_eq!(commands_group("canary"), "commands.group.canary");
191 assert_eq!(commands_group("wave1"), "commands.group.wave1");
192 }
193
194 #[test]
195 fn commands_pc_formats_id() {
196 assert_eq!(commands_pc("pc-01"), "commands.pc.pc-01");
197 assert_eq!(commands_pc("PC1234"), "commands.pc.PC1234");
198 }
199
200 #[test]
201 fn notifications_all_constant() {
202 assert_eq!(NOTIFICATIONS_ALL, "notifications.all");
203 }
204
205 #[test]
206 fn notifications_group_formats_name() {
207 assert_eq!(
208 notifications_group("tokyo-office"),
209 "notifications.group.tokyo-office"
210 );
211 }
212
213 #[test]
214 fn notifications_pc_formats_id() {
215 assert_eq!(notifications_pc("PC1234"), "notifications.pc.PC1234");
216 }
217
218 #[test]
219 fn events_notifications_acked_formats_all_segments() {
220 assert_eq!(
221 events_notifications_acked("PC1234", "S-1-5-21-1001", "notif-9f3a"),
222 "events.notifications.acked.PC1234.S-1-5-21-1001.notif-9f3a"
223 );
224 }
225
226 #[test]
227 fn events_notifications_acked_filter_is_narrow_wildcard() {
228 assert_eq!(
229 EVENTS_NOTIFICATIONS_ACKED_FILTER,
230 "events.notifications.acked.>"
231 );
232 // Must stay a strict subset of the EVENTS stream's `events.>`
233 // subjects so STREAM_EVENTS retains it without a config change.
234 assert!(EVENTS_NOTIFICATIONS_ACKED_FILTER.starts_with("events."));
235 }
236
237 #[test]
238 fn results_formats_request_id() {
239 assert_eq!(results("req-1"), "results.req-1");
240 }
241
242 #[test]
243 fn heartbeat_formats_pc_id() {
244 assert_eq!(heartbeat("pc-01"), "heartbeat.pc-01");
245 }
246
247 #[test]
248 fn host_perf_formats_pc_id() {
249 assert_eq!(host_perf("pc-01"), "host_perf.pc-01");
250 assert_eq!(host_perf("PC1234"), "host_perf.PC1234");
251 }
252
253 #[test]
254 fn process_perf_formats_pc_id() {
255 assert_eq!(process_perf("pc-01"), "process_perf.pc-01");
256 assert_eq!(process_perf("PC1234"), "process_perf.PC1234");
257 }
258
259 #[test]
260 fn obs_formats_pc_id() {
261 assert_eq!(obs("pc-01"), "obs.pc-01");
262 assert_eq!(obs("PC1234"), "obs.PC1234");
263 }
264
265 #[test]
266 fn obs_filter_constant() {
267 assert_eq!(OBS_FILTER, "obs.>");
268 }
269
270 #[test]
271 fn kill_formats_exec_id() {
272 assert_eq!(kill("exec-uuid-1"), "kill.exec-uuid-1");
273 }
274
275 #[test]
276 fn logs_fetch_formats_pc_id() {
277 assert_eq!(logs_fetch("pc-01"), "logs.fetch.pc-01");
278 }
279
280 #[test]
281 fn ping_formats_pc_id() {
282 assert_eq!(ping("pc-01"), "agents.pc-01.ping");
283 }
284
285 #[test]
286 fn job_tail_formats_pc_id() {
287 assert_eq!(job_tail("pc-01"), "job.tail.pc-01");
288 assert_eq!(job_tail("PC1234"), "job.tail.PC1234");
289 }
290
291 #[test]
292 fn events_started_formats_exec_id_and_pc_id() {
293 assert_eq!(
294 events_started("exec-uuid-1", "pc-01"),
295 "events.started.exec-uuid-1.pc-01",
296 );
297 }
298
299 #[test]
300 fn events_started_filter_is_narrow_wildcard() {
301 assert_eq!(EVENTS_STARTED_FILTER, "events.started.>");
302 }
303
304 #[test]
305 fn inventory_formats_pc_id_and_category() {
306 assert_eq!(inventory("pc-01", "hw"), "inventory.pc-01.hw");
307 assert_eq!(inventory("pc-01", INVENTORY_HW), "inventory.pc-01.hw");
308 assert_eq!(inventory("pc-01", INVENTORY_SW), "inventory.pc-01.sw");
309 assert_eq!(inventory("pc-01", INVENTORY_NET), "inventory.pc-01.net");
310 }
311}