mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-23 22:29:33 +01:00
merge feat/desktop-app (the PR #763 review fixes) into the graphical installer branch
Both branches changed the connect page and three Rust files. How the conflicts were settled: - Layout (connect.html): both sides fixed the page being cut off above a small window. One layout is kept, the one that also holds the header still: a fixed top margin on `main`. The wizard's own top margin is dropped, since the page is anchored from the top in every state now and a second margin would move the header when the wizard opens. `#elsewhere` is a child of the card since the status line moved above it, so the wizard hides it too. - State (connect.html): `gui` from the installer next to `askedFrom`, `reopen` and the starting-server counters from the review. - setup.rs: `running_install` for the port, `install_in_app` for the flag. - local_server.rs: `login_environment` and `in_home` both stay. - README: the installer's This Mac row with the sentence about a starting server; both new paragraphs. test/desktop-connect-page.browser.test.ts reaches the Terminal install the way the page now offers it (Install, a failed preparation, "Install in Terminal instead"), since Install opens the graphical install here. Checked on the merged tree: 138 Rust unit tests, clippy and fmt on the Mac mini; test/desktop-connect-install.test.ts (jsdom) and test/desktop-connect-page.browser.test.ts (Chromium) both pass. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
commit
c04955ca68
13 files changed
+2910
-217
No files matched your search
+144
-29
@@ -326,16 +326,17 @@ fn parse_ps(text: &str) -> Vec<(u32, String)> {
|
||||
/// `src/index.ts web`), not one of its short-lived control commands.
|
||||
pub fn is_codeman_server(command: &str) -> bool {
|
||||
let words: Vec<&str> = command.split_whitespace().collect();
|
||||
let entry = words.iter().position(|w| {
|
||||
let is_entry = |w: &str| {
|
||||
let name = w.rsplit('/').next().unwrap_or(w);
|
||||
w.ends_with("dist/index.js") || w.ends_with("src/index.ts") || name == "codeman" || name == "aicodeman"
|
||||
});
|
||||
match entry {
|
||||
Some(i) => {
|
||||
words.get(i + 1) == Some(&"web") && !words[i + 1..].iter().any(|w| matches!(*w, "--stop" | "--status"))
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
};
|
||||
// Any entry word that `web` follows: an earlier word may only look like
|
||||
// one (a folder named codeman in `tsx watch --cwd ~/codeman src/index.ts web`).
|
||||
(0..words.len()).any(|i| {
|
||||
is_entry(words[i])
|
||||
&& words.get(i + 1) == Some(&"web")
|
||||
&& !words[i + 1..].iter().any(|w| matches!(*w, "--stop" | "--status"))
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether a command line is an install the app started on this Mac (the
|
||||
@@ -378,6 +379,16 @@ fn codeman_servers(procs: &[(u32, String)]) -> Vec<u32> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The install the app started on this Mac, if one runs, with the port it
|
||||
/// asked for as soon as one of its processes names it. The Terminal script
|
||||
/// that wraps the installer names none and is the older process, so the first
|
||||
/// match alone would report no port for the whole install.
|
||||
fn running_install(procs: &[(u32, String)]) -> Option<Option<u16>> {
|
||||
let mut installs = procs.iter().filter_map(|(_, c)| install_in_progress(c)).peekable();
|
||||
installs.peek()?;
|
||||
Some(installs.flatten().next())
|
||||
}
|
||||
|
||||
/// A listening TCP socket: its pid, address (`*`, `127.0.0.1`, `[::1]`, a
|
||||
/// specific IP) and port.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -440,9 +451,8 @@ fn serves_loopback_port(found: &[Listener], port: u16) -> bool {
|
||||
/// all-interfaces bind, else its own address.
|
||||
fn listener_url(l: &Listener) -> Option<Url> {
|
||||
let host = match l.host.as_str() {
|
||||
"*" | "0.0.0.0" | "127.0.0.1" => "127.0.0.1".to_string(),
|
||||
h if h.starts_with('[') => h.to_string(),
|
||||
h => h.to_string(),
|
||||
"*" | "0.0.0.0" => "127.0.0.1",
|
||||
h => h,
|
||||
};
|
||||
Url::parse(&format!("http://{host}:{}/", l.port)).ok()
|
||||
}
|
||||
@@ -455,6 +465,9 @@ pub enum LocalState {
|
||||
Running,
|
||||
/// Codeman answers but wants a login the app does not have.
|
||||
NeedsLogin,
|
||||
/// A Codeman server of this user runs but listens nowhere yet: it is
|
||||
/// still starting (the LaunchAgent at login, a restart).
|
||||
Starting,
|
||||
/// A Codeman server of this user runs, but not at an address the app can
|
||||
/// open (bound to one network address, say): see `elsewhere`.
|
||||
RunningElsewhere,
|
||||
@@ -497,6 +510,33 @@ pub struct LocalStatus {
|
||||
pub outcome: ProbeOutcome,
|
||||
}
|
||||
|
||||
/// Whether the probe got an answer from a Codeman server, logged in or not.
|
||||
fn answers(outcome: &ProbeOutcome) -> bool {
|
||||
matches!(
|
||||
outcome,
|
||||
ProbeOutcome::Ok { .. }
|
||||
| ProbeOutcome::PasswordChangeRequired
|
||||
| ProbeOutcome::NeedsLogin
|
||||
| ProbeOutcome::BadCredentials
|
||||
| ProbeOutcome::RateLimited { .. }
|
||||
)
|
||||
}
|
||||
|
||||
/// What the This Mac tab shows for what `inspect` found. `server_runs`: one of
|
||||
/// this user's Codeman server processes exists. `server_listens`: one of them
|
||||
/// has a listening socket. `installed`: Codeman is on this Mac.
|
||||
fn local_state(outcome: &ProbeOutcome, server_runs: bool, server_listens: bool, installed: bool) -> LocalState {
|
||||
match outcome {
|
||||
ProbeOutcome::Ok { .. } | ProbeOutcome::PasswordChangeRequired => LocalState::Running,
|
||||
_ if answers(outcome) => LocalState::NeedsLogin,
|
||||
ProbeOutcome::Refused if server_runs && !server_listens => LocalState::Starting,
|
||||
ProbeOutcome::Refused if server_runs => LocalState::RunningElsewhere,
|
||||
ProbeOutcome::Refused if installed => LocalState::Stopped,
|
||||
ProbeOutcome::Refused => LocalState::NotInstalled,
|
||||
_ => LocalState::Other,
|
||||
}
|
||||
}
|
||||
|
||||
/// How hard `inspect` looks for an installed Codeman when nothing answers.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Depth {
|
||||
@@ -516,7 +556,7 @@ pub fn inspect(depth: Depth) -> LocalStatus {
|
||||
let procs = list_processes();
|
||||
let servers = codeman_servers(&procs);
|
||||
let found = listeners(&servers);
|
||||
let install = procs.iter().find_map(|(_, c)| install_in_progress(c));
|
||||
let install = running_install(&procs);
|
||||
let install_in_app = procs.iter().any(|(_, c)| gui_install::is_install_run(c));
|
||||
|
||||
// The login goes only to this user's own Codeman on that port.
|
||||
@@ -565,20 +605,13 @@ pub fn inspect(depth: Depth) -> LocalStatus {
|
||||
|
||||
let app = home.join(".codeman/app");
|
||||
let install_complete = app.join(".install-complete").is_file();
|
||||
let answering = match &outcome {
|
||||
ProbeOutcome::Ok { .. } | ProbeOutcome::PasswordChangeRequired => Some(LocalState::Running),
|
||||
ProbeOutcome::NeedsLogin | ProbeOutcome::BadCredentials | ProbeOutcome::RateLimited { .. } => {
|
||||
Some(LocalState::NeedsLogin)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
// Something else on Codeman's port (a dev server on 3000, say) says
|
||||
// nothing about whether Codeman is installed, so look either way.
|
||||
let on_disk = install_complete
|
||||
|| app.join("dist/index.js").is_file()
|
||||
|| agent_path(&home).is_file()
|
||||
|| daemon_path().is_file();
|
||||
let installed = answering.is_some()
|
||||
let installed = answers(&outcome)
|
||||
|| !servers.is_empty()
|
||||
|| on_disk
|
||||
|| match depth {
|
||||
@@ -586,13 +619,7 @@ pub fn inspect(depth: Depth) -> LocalStatus {
|
||||
Depth::Login => local_server::installed(false),
|
||||
Depth::Interactive => local_server::installed(true),
|
||||
};
|
||||
let state = match (answering, &outcome) {
|
||||
(Some(state), _) => state,
|
||||
(None, ProbeOutcome::Refused) if !servers.is_empty() => LocalState::RunningElsewhere,
|
||||
(None, ProbeOutcome::Refused) if installed => LocalState::Stopped,
|
||||
(None, ProbeOutcome::Refused) => LocalState::NotInstalled,
|
||||
(None, _) => LocalState::Other,
|
||||
};
|
||||
let state = local_state(&outcome, !servers.is_empty(), !found.is_empty(), installed);
|
||||
if state != LocalState::RunningElsewhere {
|
||||
elsewhere.clear();
|
||||
}
|
||||
@@ -636,6 +663,15 @@ pub fn already_running() -> Option<String> {
|
||||
))
|
||||
}
|
||||
|
||||
/// Whether `url` is where the launchd job `service` answers: its scheme and
|
||||
/// port on this Mac, under `localhost` as much as 127.0.0.1. The path is left
|
||||
/// out: a job with a base path answers below `/` too, and a second server on
|
||||
/// its port would run without the job's settings.
|
||||
fn served_by(service: &ServiceConfig, url: &Url) -> bool {
|
||||
let at = local_url(Some(service));
|
||||
policy::is_loopback(url) && url.scheme() == at.scheme() && url.port_or_known_default() == at.port_or_known_default()
|
||||
}
|
||||
|
||||
/// Start the Codeman on this Mac for `url`. A LaunchAgent that serves that
|
||||
/// address is started through launchd, so it runs with its own settings
|
||||
/// (host, password) and stays supervised; anything else gets `codeman web -d`
|
||||
@@ -645,7 +681,7 @@ pub fn start_local(url: &Url) -> Result<String, String> {
|
||||
return Err(why);
|
||||
}
|
||||
match read_service() {
|
||||
Some(s) if local_url(Some(&s)) == *url => {
|
||||
Some(s) if served_by(&s, url) => {
|
||||
if s.daemon {
|
||||
return Err(format!(
|
||||
"A system LaunchDaemon runs Codeman on this Mac; an administrator starts it: sudo launchctl kickstart -k system/{SERVICE_LABEL}"
|
||||
@@ -701,7 +737,7 @@ fn start_launch_agent(port: u16) -> Result<String, String> {
|
||||
if !port_free(port) {
|
||||
return Ok(format!("launchd started Codeman ({SERVICE_LABEL})."));
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
Err("launchd started Codeman, but it did not answer within 30 seconds. Its log is /tmp/codeman.log for an install.sh setup.".into())
|
||||
}
|
||||
@@ -1242,6 +1278,71 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_start_goes_to_the_launchd_job_that_serves_the_address() {
|
||||
let job = parse_service(&json!({
|
||||
"ProgramArguments": ["node", "i.js", "web"],
|
||||
"EnvironmentVariables": { "CODEMAN_PORT": "3001", "CODEMAN_BASE_URL": "/codeman" }
|
||||
}));
|
||||
// The app's own address for it, and what a user types for the same server.
|
||||
assert!(served_by(&job, &u("http://127.0.0.1:3001/codeman/")));
|
||||
assert!(served_by(&job, &u("http://localhost:3001/codeman/")));
|
||||
assert!(served_by(&job, &u("http://LOCALHOST:3001/")));
|
||||
// Another port, scheme or machine is not this job's server.
|
||||
assert!(!served_by(&job, &u("http://127.0.0.1:3000/codeman/")));
|
||||
assert!(!served_by(&job, &u("https://127.0.0.1:3001/codeman/")));
|
||||
assert!(!served_by(&job, &u("http://192.168.1.5:3001/codeman/")));
|
||||
assert!(!served_by(&job, &u("http://localhost.example.com:3001/")));
|
||||
|
||||
let https = parse_service(&json!({ "ProgramArguments": ["node", "i.js", "web", "--https"] }));
|
||||
assert!(served_by(&https, &u("https://localhost:3000/")));
|
||||
assert!(!served_by(&https, &u("http://localhost:3000/")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn this_mac_state_for_what_was_found() {
|
||||
let ok = ProbeOutcome::Ok {
|
||||
version: "1.48.0".into(),
|
||||
};
|
||||
// Whatever else is true, a Codeman that answers is what the tab shows.
|
||||
for (runs, listens, installed) in [(false, false, false), (true, true, true), (true, false, false)] {
|
||||
assert_eq!(local_state(&ok, runs, listens, installed), LocalState::Running);
|
||||
assert_eq!(
|
||||
local_state(&ProbeOutcome::PasswordChangeRequired, runs, listens, installed),
|
||||
LocalState::Running
|
||||
);
|
||||
for login in [
|
||||
ProbeOutcome::NeedsLogin,
|
||||
ProbeOutcome::BadCredentials,
|
||||
ProbeOutcome::RateLimited {
|
||||
sent_credentials: false,
|
||||
retry_after_secs: None,
|
||||
},
|
||||
] {
|
||||
assert_eq!(local_state(&login, runs, listens, installed), LocalState::NeedsLogin);
|
||||
}
|
||||
}
|
||||
// Nothing answers. A server process with no socket yet is starting,
|
||||
// which comes before "runs somewhere else": the app must not send the
|
||||
// user to another address for a server that is two seconds from up.
|
||||
let refused = ProbeOutcome::Refused;
|
||||
assert_eq!(local_state(&refused, true, false, true), LocalState::Starting);
|
||||
assert_eq!(local_state(&refused, true, false, false), LocalState::Starting);
|
||||
assert_eq!(local_state(&refused, true, true, true), LocalState::RunningElsewhere);
|
||||
assert_eq!(local_state(&refused, false, false, true), LocalState::Stopped);
|
||||
assert_eq!(local_state(&refused, false, false, false), LocalState::NotInstalled);
|
||||
// Something else holds the port, or the look itself failed.
|
||||
for other in [
|
||||
ProbeOutcome::NotCodeman { status: 404 },
|
||||
ProbeOutcome::Timeout,
|
||||
ProbeOutcome::MaybeHttps,
|
||||
] {
|
||||
assert_eq!(local_state(&other, true, true, true), LocalState::Other);
|
||||
assert_eq!(local_state(&other, false, false, false), LocalState::Other);
|
||||
}
|
||||
assert!(answers(&ProbeOutcome::BadCredentials) && !answers(&refused));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn processes_and_listeners() {
|
||||
let ps = " 101 /opt/homebrew/bin/node /Users/k/.codeman/app/dist/index.js web\n\
|
||||
@@ -1255,6 +1356,15 @@ mod tests {
|
||||
909 bash /var/folders/x/T/codeman-install.Ab12Cd --local --service --port 3001\n\
|
||||
1001 /bin/bash /var/folders/x/T/install-codeman-remote-55-1.command\n\
|
||||
garbage line\n";
|
||||
// A folder named like the command comes before the real entry point.
|
||||
assert!(is_codeman_server(
|
||||
"node /x/tsx/cli.mjs watch --cwd /Users/k/codeman src/index.ts web"
|
||||
));
|
||||
assert!(is_codeman_server(
|
||||
"node --watch-path /Users/k/codeman /Users/k/codeman/src/index.ts web --port 3001"
|
||||
));
|
||||
assert!(!is_codeman_server("node /Users/k/codeman/scripts/build.mjs web"));
|
||||
assert!(!is_codeman_server("/opt/homebrew/bin/codeman attach web"));
|
||||
let procs = parse_ps(ps);
|
||||
assert_eq!(
|
||||
procs[0],
|
||||
@@ -1266,6 +1376,11 @@ mod tests {
|
||||
assert_eq!(codeman_servers(&procs), [101, 202, 303]);
|
||||
let installs: Vec<Option<u16>> = procs.iter().filter_map(|(_, c)| install_in_progress(c)).collect();
|
||||
assert_eq!(installs, [None, Some(3001)]);
|
||||
// The Terminal script comes first and names no port: the installer's wins.
|
||||
assert_eq!(running_install(&procs), Some(Some(3001)));
|
||||
// Before the installer is downloaded, only the script runs.
|
||||
assert_eq!(running_install(&procs[..8]), Some(None));
|
||||
assert_eq!(running_install(&procs[..7]), None);
|
||||
|
||||
let lsof = "p101\nf21\nn127.0.0.1:3000\nf22\nn172.17.0.1:3999\np202\nf9\nn*:4000\np303\nn[::1]:5000\nnbogus\n";
|
||||
let found = parse_lsof(lsof);
|
||||
|
||||
Reference in new issue
Block a user