agentmux_common/ipc/types.rs
1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The small shapes the commands and events share: rectangles, window kinds and snapshots,
5//! and the enums that name a phase, a severity, a kind of drift or an error.
6
7use serde::{Deserialize, Serialize};
8
9/// Phase B.9.1 — rectangle in Win32 screen coordinates (pixels).
10/// Matches Windows' `RECT` semantics: `right` and `bottom` are one
11/// past the last included pixel, so `right - left == width`.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13pub struct Rect {
14 pub left: i32,
15 pub top: i32,
16 pub right: i32,
17 pub bottom: i32,
18}
19
20/// Window role in the multi-window model — the single definition, used by
21/// the host (`agentmux-cef::state` re-exports it) and deserialized by the
22/// launcher. The host used to carry a byte-identical private copy and map
23/// between the two variant by variant; that copy is gone
24/// (`docs/reports/REPORT_DRY_AND_MODULARITY_AUDIT_2026_09_06.md` §2.2).
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "snake_case")]
27pub enum WindowKind {
28 /// Independent AgentMux window. Appears in the Windows taskbar.
29 FullInstance,
30 /// Hidden from the taskbar; closes when its parent FullInstance closes.
31 Subwindow,
32}
33
34/// Phase D.1 — serializable view of one window in the launcher
35/// reducer's canonical state. Maps 1:1 to the launcher's internal
36/// `WindowMirror` minus `opened_at` (which is launcher-local clock
37/// data not meaningful to subscribers).
38#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
39pub struct WindowSnapshot {
40 pub label: String,
41 pub kind: WindowKind,
42 pub parent_label: Option<String>,
43 pub hwnd: Option<u64>,
44 pub visible: bool,
45 pub iconic: bool,
46 pub last_rect: Option<Rect>,
47 pub foregrounded_since_open: bool,
48}
49
50/// Phase B.9.1 — six classes of CEF↔Win32 disagreement the reducer
51/// can detect at event-dispatch time. See the WRR design doc for
52/// the per-kind triggering Command and reducer action.
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
54#[serde(rename_all = "snake_case")]
55pub enum HwndDriftKind {
56 /// `state.windows` has a label whose `hwnd` field never got
57 /// populated, AND a follow-up event arrived that should have
58 /// reconciled it. CEF says open, Win32 has no matching HWND.
59 BrowserWithoutHwnd,
60 /// HWND in the host's report doesn't map to any
61 /// `state.windows` / `state.pool` label. Win32 has it, CEF
62 /// didn't open it. The "stray taskbar" case.
63 HwndWithoutBrowser,
64 /// HWND for a known label was never SHOWN (no foreground
65 /// event) since open and a subsequent state transition has
66 /// elapsed. User can't see it.
67 HiddenSinceOpen,
68 /// Window rect doesn't intersect any monitor in
69 /// `state.monitors`. Off-screen orphan.
70 OffMonitor,
71 /// `ReportHwndDestroyed` arrived without a preceding
72 /// `ReportWindowClosed` for the matching label. Renderer
73 /// crashed, took the HWND with it.
74 OrphanDestroy,
75 /// `ReportWindowClosed` arrived, but subsequent OS events for
76 /// the HWND keep firing (it never went away on the Win32 side).
77 LingeringHwnd,
78 /// Phase B.9.3 — host process is alive and registered, but
79 /// `state.windows` just transitioned to empty. The host's own
80 /// close path doesn't reap the warm pool when the last
81 /// user-visible window closes (pool windows hold
82 /// `state.browsers` non-empty, so `quit_message_loop` never
83 /// fires). The launcher's reducer is the only place that knows
84 /// "all user-meaningful labels are gone" cleanly, so we
85 /// surface the signal here. Paired with `Event::HostShouldQuit`
86 /// emitted in the same reducer call (see B.9.3 saga).
87 OrphanInstance,
88}
89
90/// Phase B.9.1 — drift severity, assigned per drift kind by the
91/// launcher's `severity_for` (`crates/launcher/src/wrr/mod.rs`). The
92/// operator-tunable severity floor the B.9 design described was never
93/// built: nothing filters events by severity.
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
95#[serde(rename_all = "lowercase")]
96pub enum Severity {
97 Info,
98 Warn,
99 Error,
100}
101
102/// Coarse-grained launcher state. Spec §4: Starting → Running →
103/// Quitting → Dead, no other transitions allowed. The reducer in
104/// agentmux-launcher::reducer enforces this; a violation panics
105/// (Job Object reaps via OS).
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
107#[serde(rename_all = "lowercase")]
108pub enum LifecyclePhase {
109 /// Initial state; launcher has not yet seen the host register.
110 Starting,
111 /// Host has registered and the canonical state is being
112 /// maintained. Steady state.
113 Running,
114 /// Quit { reason } received, ack outstanding to subscribers.
115 /// Phase B.3 keeps this state-shape only — the actual Quit
116 /// command lands in a later sub-PR.
117 Quitting,
118 /// Cleanup done; launcher about to exit. Transient.
119 Dead,
120}
121
122/// Phase B.4 follow-up — which mirror diverged. Tagged so subscribers
123/// can route alerts (windows-drift might page; pool-drift is more
124/// ephemeral since the pool turns over fast).
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
126#[serde(rename_all = "snake_case")]
127pub enum DriftKind {
128 Windows,
129 Pool,
130}
131
132/// Discriminant for `Event::Error` — keeps clients structured against
133/// failure modes without parsing message text.
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
135#[serde(rename_all = "snake_case")]
136pub enum ErrorCode {
137 /// Couldn't deserialize the line into a Command.
138 InvalidCommand,
139 /// Command sent before Register.
140 NotRegistered,
141 /// Register sent twice on the same connection.
142 AlreadyRegistered,
143}