omni-dev 0.39.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, and Datadog.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! `omni-dev daemon` — supervise the long-lived daemon and its services.

pub(crate) mod bridge;
pub(crate) mod control;
pub(crate) mod logs;
pub(crate) mod restart;
pub(crate) mod run;
pub(crate) mod service;
pub(crate) mod start;
pub(crate) mod status;
pub(crate) mod stop;

use anyhow::Result;
use clap::{Parser, Subcommand};

use crate::daemon::protocol::StatusReport;

/// Daemon: host long-lived services (e.g. the browser bridge) under one
/// supervised, menu-bar-controllable process.
#[derive(Parser)]
pub struct DaemonCommand {
    /// The daemon subcommand to execute.
    #[command(subcommand)]
    pub command: DaemonSubcommands,
}

/// Daemon subcommands.
#[derive(Subcommand)]
pub enum DaemonSubcommands {
    /// Runs the daemon in the foreground (the process launchd execs).
    Run(run::RunCommand),
    /// Starts the daemon in the background.
    Start(start::StartCommand),
    /// Stops the running daemon.
    Stop(stop::StopCommand),
    /// Restarts the daemon.
    Restart(restart::RestartCommand),
    /// Reports daemon and per-service status.
    Status(status::StatusCommand),
    /// Reads (and optionally follows) the daemon's log file.
    Logs(logs::LogsCommand),
    /// Controls the daemon-hosted browser bridge (restart, disconnect a tab, …).
    Bridge(bridge::BridgeCommand),
    /// Sends an arbitrary operation to any daemon service (low-level escape hatch).
    Service(service::ServiceCommand),
}

/// Prints a non-fatal warning when the resident daemon differs from this CLI
/// binary — the client is driving a stale daemon after a binary upgrade (#1113).
///
/// Comparison prefers the git commit SHA when both sides know theirs, so a
/// rebuilt-but-same-crate-version daemon the CLI has outrun is still flagged
/// (#1374); it falls back to crate-version string equality otherwise. A daemon
/// that advertises neither is treated as unknown and never warns.
pub(crate) fn warn_version_mismatch(report: &StatusReport) {
    if is_daemon_stale(
        report.version.as_deref(),
        report.provenance.commit_long.as_deref(),
        crate::VERSION,
        crate::build_info::GIT_SHA,
    ) {
        let cli = describe_build(crate::VERSION, crate::build_info::GIT_SHA_SHORT);
        let daemon = describe_build(
            report.version.as_deref().unwrap_or("unknown"),
            report.provenance.commit.as_deref(),
        );
        eprintln!(
            "warning: omni-dev CLI {cli} is talking to daemon {daemon}; \
             run `omni-dev daemon restart` to upgrade the resident daemon"
        );
    }
}

/// Whether a resident daemon is stale relative to this CLI. When both sides
/// report a commit SHA the comparison is commit-level (catching a same-version
/// rebuild); otherwise it falls back to crate-version string equality. A daemon
/// that advertises no version and no commit is never stale.
fn is_daemon_stale(
    daemon_version: Option<&str>,
    daemon_commit: Option<&str>,
    cli_version: &str,
    cli_commit: Option<&str>,
) -> bool {
    match (cli_commit, daemon_commit) {
        // Both know their commit: a mismatch means different code even at the
        // same crate version (#1374).
        (Some(cli), Some(daemon)) => cli != daemon,
        // Otherwise fall back to crate-version equality (a pre-#1374 daemon, or
        // a build with no git metadata on either side).
        _ => matches!(daemon_version, Some(v) if v != cli_version),
    }
}

/// Formats a `v<version> (<short-sha>)` build descriptor, omitting the commit
/// when unknown.
fn describe_build(version: &str, short_commit: Option<&str>) -> String {
    match short_commit {
        Some(commit) => format!("v{version} ({commit})"),
        None => format!("v{version}"),
    }
}

/// Sends one operation to a named daemon service over the control socket and
/// returns its payload, turning an `ok: false` reply into an error. Stamps the
/// caller's request-log `invocation_id` so any HTTP the daemon issues while
/// serving the op correlates back to this invocation (#1198). Shared by the
/// typed `daemon bridge` command and the generic `daemon service` passthrough.
pub(crate) async fn call_service(
    socket: &std::path::Path,
    service: &str,
    op: &str,
    payload: serde_json::Value,
) -> Result<serde_json::Value> {
    use crate::daemon::client::DaemonClient;
    use crate::daemon::protocol::DaemonEnvelope;

    let origin = crate::request_log::current_context().invocation_id;
    let reply = DaemonClient::new(socket)
        .request(DaemonEnvelope::service(service, op, payload).with_origin(origin))
        .await?;
    if reply.ok {
        Ok(reply.payload)
    } else {
        anyhow::bail!(
            "daemon returned an error: {}",
            reply.error.as_deref().unwrap_or("unknown error")
        )
    }
}

impl DaemonCommand {
    /// Executes the daemon command.
    pub async fn execute(self) -> Result<()> {
        match self.command {
            DaemonSubcommands::Run(cmd) => cmd.execute().await,
            DaemonSubcommands::Start(cmd) => cmd.execute().await,
            DaemonSubcommands::Stop(cmd) => cmd.execute().await,
            DaemonSubcommands::Restart(cmd) => cmd.execute().await,
            DaemonSubcommands::Status(cmd) => cmd.execute().await,
            DaemonSubcommands::Logs(cmd) => cmd.execute().await,
            DaemonSubcommands::Bridge(cmd) => cmd.execute().await,
            DaemonSubcommands::Service(cmd) => cmd.execute().await,
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use std::path::Path;

    /// Mirrors the `omni-dev daemon` argv surface for parse tests.
    #[derive(Parser)]
    struct Wrapper {
        #[command(subcommand)]
        cmd: DaemonSubcommands,
    }

    fn parse(args: &[&str]) -> DaemonSubcommands {
        let mut full = vec!["omni-dev"];
        full.extend_from_slice(args);
        Wrapper::try_parse_from(full).unwrap().cmd
    }

    #[test]
    fn parses_all_subcommands() {
        assert!(matches!(parse(&["run"]), DaemonSubcommands::Run(_)));
        assert!(matches!(parse(&["start"]), DaemonSubcommands::Start(_)));
        assert!(matches!(parse(&["stop"]), DaemonSubcommands::Stop(_)));
        assert!(matches!(parse(&["restart"]), DaemonSubcommands::Restart(_)));
        assert!(matches!(parse(&["status"]), DaemonSubcommands::Status(_)));
        assert!(matches!(parse(&["logs"]), DaemonSubcommands::Logs(_)));
        assert!(matches!(
            parse(&["bridge", "status"]),
            DaemonSubcommands::Bridge(_)
        ));
        assert!(matches!(
            parse(&["service", "browser-bridge", "status"]),
            DaemonSubcommands::Service(_)
        ));
    }

    #[test]
    fn logs_flags_and_defaults_parse() {
        let DaemonSubcommands::Logs(cmd) = parse(&["logs"]) else {
            panic!("expected logs");
        };
        assert_eq!(cmd.lines, 200);
        assert!(!cmd.follow);
        assert!(cmd.socket.is_none());

        let DaemonSubcommands::Logs(cmd) =
            parse(&["logs", "-f", "-n", "50", "--socket", "/tmp/d.sock"])
        else {
            panic!("expected logs");
        };
        assert!(cmd.follow);
        assert_eq!(cmd.lines, 50);
        assert_eq!(cmd.socket.as_deref(), Some(Path::new("/tmp/d.sock")));
    }

    #[test]
    fn is_daemon_stale_falls_back_to_version_when_a_commit_is_unknown() {
        // No commits known on either side → crate-version string comparison.
        assert!(!is_daemon_stale(Some("1.2.3"), None, "1.2.3", None));
        assert!(is_daemon_stale(Some("1.2.2"), None, "1.2.3", None));
        assert!(is_daemon_stale(Some("1.3.0"), None, "1.2.3", None));
        // A daemon that advertises neither version nor commit is never stale.
        assert!(!is_daemon_stale(None, None, "1.2.3", None));
        // One side missing a commit still falls back to the version compare.
        assert!(is_daemon_stale(Some("1.2.2"), None, "1.2.3", Some("aaaa")));
        assert!(is_daemon_stale(Some("1.2.2"), Some("bbbb"), "1.2.3", None));
    }

    #[test]
    fn is_daemon_stale_compares_on_commit_when_both_are_known() {
        // Same crate version, different commit → stale (the #1374 blind-spot fix).
        assert!(is_daemon_stale(
            Some("1.2.3"),
            Some("bbbbbbbb"),
            "1.2.3",
            Some("aaaaaaaa")
        ));
        // Same commit → not stale, regardless of any version noise.
        assert!(!is_daemon_stale(
            Some("1.2.3"),
            Some("aaaaaaaa"),
            "1.2.3",
            Some("aaaaaaaa")
        ));
    }

    #[test]
    fn services_flag_parses_a_subset_on_every_launcher() {
        use crate::daemon::DaemonServiceKind;

        let DaemonSubcommands::Run(cmd) = parse(&["run", "--services", "worktrees,sessions"])
        else {
            panic!("expected run");
        };
        assert_eq!(
            cmd.services,
            vec![DaemonServiceKind::Worktrees, DaemonServiceKind::Sessions]
        );

        let DaemonSubcommands::Start(cmd) = parse(&["start", "--services", "browser-bridge"])
        else {
            panic!("expected start");
        };
        assert_eq!(cmd.services, vec![DaemonServiceKind::Bridge]);

        let DaemonSubcommands::Restart(cmd) = parse(&["restart", "--services", "snowflake"]) else {
            panic!("expected restart");
        };
        assert_eq!(cmd.services, vec![DaemonServiceKind::Snowflake]);
    }

    #[test]
    fn services_flag_defaults_empty_and_rejects_unknown_values() {
        let DaemonSubcommands::Run(cmd) = parse(&["run"]) else {
            panic!("expected run");
        };
        assert!(cmd.services.is_empty());

        // An unknown service name is rejected by clap at parse time (a typo fails
        // loudly instead of silently baking a name the daemon can't host).
        assert!(Wrapper::try_parse_from(["omni-dev", "run", "--services", "bogus"]).is_err());
    }

    #[test]
    fn warn_version_mismatch_covers_every_branch_without_panicking() {
        // Exercises the print branch (a commit mismatch) and the no-op branches
        // (matching, and a daemon advertising nothing). It writes to stderr, so
        // there is nothing to assert beyond it not panicking.
        let mismatch = StatusReport {
            version: Some("0.0.0-mismatch".to_string()),
            provenance: crate::build_info::Provenance {
                commit: Some("deadbeef".to_string()),
                commit_long: Some("deadbeefdeadbeef".to_string()),
                ..crate::build_info::Provenance::default()
            },
            ..StatusReport::default()
        };
        warn_version_mismatch(&mismatch);
        warn_version_mismatch(&StatusReport::current(vec![]));
        warn_version_mismatch(&StatusReport::default());
    }

    #[tokio::test]
    async fn call_service_returns_the_payload_of_an_ok_reply() {
        let (_dir, sock, server) = crate::daemon::testutil::fake_daemon_reply(
            serde_json::json!({ "ok": true, "payload": { "restarted": true } }),
        );
        let payload = call_service(&sock, "browser-bridge", "restart", serde_json::Value::Null)
            .await
            .unwrap();
        assert_eq!(payload, serde_json::json!({ "restarted": true }));
        server.await.unwrap();
    }

    #[tokio::test]
    async fn daemon_execute_routes_logs_without_a_daemon() {
        // The `Logs` dispatch arm reads the log file directly (no socket), so a
        // missing `daemon.log` beside an absent socket is a clean no-op.
        let dir = tempfile::tempdir().unwrap();
        let cmd = DaemonCommand {
            command: DaemonSubcommands::Logs(logs::LogsCommand {
                socket: Some(dir.path().join("daemon.sock")),
                lines: 10,
                follow: false,
            }),
        };
        cmd.execute().await.unwrap();
    }

    #[tokio::test]
    async fn daemon_execute_routes_bridge_and_service_over_the_socket() {
        // The `Bridge` and `Service` dispatch arms both reach a service over the
        // control socket; a fake daemon acknowledges each.
        let (_bdir, bsock, bserver) = crate::daemon::testutil::fake_daemon_reply(
            serde_json::json!({ "ok": true, "payload": { "restarted": true } }),
        );
        DaemonCommand {
            command: DaemonSubcommands::Bridge(bridge::BridgeCommand {
                command: bridge::BridgeSubcommands::Restart(bridge::SocketArg {
                    socket: Some(bsock),
                }),
            }),
        }
        .execute()
        .await
        .unwrap();
        bserver.await.unwrap();

        let (_sdir, ssock, sserver) = crate::daemon::testutil::fake_daemon_reply(
            serde_json::json!({ "ok": true, "payload": { "connected": false } }),
        );
        DaemonCommand {
            command: DaemonSubcommands::Service(service::ServiceCommand {
                service: "browser-bridge".to_string(),
                op: "status".to_string(),
                payload: None,
                socket: Some(ssock),
            }),
        }
        .execute()
        .await
        .unwrap();
        sserver.await.unwrap();
    }

    #[tokio::test]
    async fn call_service_maps_an_error_reply_to_an_err() {
        let (_dir, sock, server) = crate::daemon::testutil::fake_daemon_reply(
            serde_json::json!({ "ok": false, "error": "no connected tab with id 9" }),
        );
        let err = call_service(
            &sock,
            "browser-bridge",
            "disconnect-tab",
            serde_json::Value::Null,
        )
        .await
        .unwrap_err();
        assert!(
            err.to_string().contains("no connected tab with id 9"),
            "{err}"
        );
        server.await.unwrap();
    }

    #[test]
    fn socket_override_parses() {
        let DaemonSubcommands::Run(cmd) = parse(&["run", "--socket", "/tmp/x.sock"]) else {
            panic!("expected run");
        };
        assert_eq!(cmd.socket.as_deref(), Some(Path::new("/tmp/x.sock")));
    }

    #[test]
    fn status_json_flag_parses() {
        let DaemonSubcommands::Status(cmd) = parse(&["status", "--json"]) else {
            panic!("expected status");
        };
        assert!(cmd.json);
    }

    #[test]
    fn socket_defaults_to_none() {
        let DaemonSubcommands::Status(cmd) = parse(&["status"]) else {
            panic!("expected status");
        };
        assert!(cmd.socket.is_none());
        assert!(!cmd.json);
    }

    /// `daemon status` against a socket with no daemon dispatches through to the
    /// "not running" path (table and `--json`) without erroring or side effects.
    #[tokio::test]
    async fn status_dispatch_reports_not_running() {
        let dir = tempfile::tempdir().unwrap();
        let socket = dir.path().join("absent.sock");
        for json in [false, true] {
            let cmd = DaemonCommand {
                command: DaemonSubcommands::Status(status::StatusCommand {
                    socket: Some(socket.clone()),
                    output: crate::cli::format::TableOrJson::Table,
                    json,
                }),
            };
            cmd.execute().await.unwrap();
        }
    }
}