Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

Socket error replies carry a code the Reason CLI acts on

Lars Talian71009ba48ed8
diff --git a/src/main.rs b/src/main.rs
index 673aec3..d978631 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -677,14 +677,7 @@ fn json_error<T>(json: bool, e: anyhow::Error) -> Result<T> {
/// message.
fn json_failure(e: &anyhow::Error) -> serde_json::Value {
let msg = format!("{e:#}");
- let error = if msg.contains(team::SAME_ACCOUNT) {
- team::SAME_ACCOUNT
- } else if msg.contains("is already in this team") {
- "name_taken"
- } else {
- msg.as_str()
- };
- serde_json::json!({ "error": error })
+ serde_json::json!({ "error": proto::error_code(&msg).unwrap_or(&msg) })
}
/// The running daemon's pid, from its lock file.
diff --git a/src/proto.rs b/src/proto.rs
index 5873c1b..4371fe8 100644
--- a/src/proto.rs
+++ b/src/proto.rs
@@ -235,15 +235,32 @@ pub struct Reply {
pub result: Option<Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
+ /// For a failure the Reason CLI handles: `name_taken` or `same_account_needs_owner` (see [`error_code`]).
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub code: Option<String>,
}
impl Reply {
pub fn ok(id: u64, result: Value) -> Self {
- Reply { id, ok: true, result: Some(result), error: None }
+ Reply { id, ok: true, result: Some(result), error: None, code: None }
}
pub fn err(id: u64, e: anyhow::Error) -> Self {
- Reply { id, ok: false, result: None, error: Some(format!("{e:#}")) }
+ let error = format!("{e:#}");
+ Reply { id, ok: false, result: None, code: error_code(&error).map(String::from), error: Some(error) }
+ }
+}
+
+/// The failures a client acts on, by what the message says (as every build says it, a joiner hears an inviting machine's
+/// own words): `same_account_needs_owner` when one of the person's own computers should let this one in, `name_taken`
+/// when the name is someone else's in the team.
+pub fn error_code(error: &str) -> Option<&'static str> {
+ if error.contains(crate::team::SAME_ACCOUNT) {
+ Some(crate::team::SAME_ACCOUNT)
+ } else if error.contains("is already in this team") {
+ Some("name_taken")
+ } else {
+ None
}
}
@@ -451,4 +468,19 @@ mod tests {
.unwrap();
assert_eq!((old.id.as_str(), old.name.as_str()), ("", "vibes"));
}
+
+ #[test]
+ fn error_replies_carry_a_code_the_reason_cli_acts_on() {
+ let reply = |m: &str| serde_json::to_value(Reply::err(7, anyhow::anyhow!(m.to_string()))).unwrap();
+ assert_eq!(
+ reply("'alex' is already in this team; join with another name (--as)"),
+ serde_json::json!({"id": 7, "ok": false, "error": "'alex' is already in this team; join with another name (--as)",
+ "code": "name_taken"})
+ );
+ let same = "only one of alex's own computers lets this one in as alex (same_account_needs_owner)";
+ assert_eq!(reply(same)["code"], "same_account_needs_owner");
+ // Anything else, and every success, has none.
+ assert_eq!(reply("this sign-in proof expired; sign in again").get("code"), None);
+ assert_eq!(serde_json::to_value(Reply::ok(7, serde_json::json!(1))).unwrap().get("code"), None);
+ }
}
diff --git a/tests/e2e/two_machines.py b/tests/e2e/two_machines.py
index 30318c8..9b15db9 100644
--- a/tests/e2e/two_machines.py
+++ b/tests/e2e/two_machines.py
@@ -168,6 +168,21 @@ _, out = run(mal, "init", "x", "--proof", proof("u_mallory", "mallory", mal_ep,
check("init refuses a proof for something else", "isn't from Reason Network" in out and "Created" not in out, out)
out, both = run(mal, "join", link.group(1), "--proof", "garbage", "--no-setup", "--json")
check("with --json every failure is JSON", as_json(out) == {"error": "this sign-in proof is damaged"}, both)
+# The Reason CLI joins over the daemon's socket (the proof never on a command line): its error replies carry a code.
+sock_path = os.path.join(mal["REASON_NETWORK_HOME"], "network.sock")
+if len(sock_path) < 100: # longer ones get a hashed name elsewhere (see paths::socket)
+ import socket
+ with socket.socket(socket.AF_UNIX) as sock:
+ sock.settimeout(90)
+ sock.connect(sock_path)
+ req = {"id": 1, "op": "join", "ticket": link.group(1), "person": "x", "proof": proof("u_mallory", "alex", mal_ep)}
+ sock.sendall((json.dumps(req) + "\n").encode())
+ reply = as_json(sock.makefile().readline())
+ check("over the socket, a taken name's error reply has code name_taken",
+ reply.get("id") == 1 and reply.get("ok") is False and reply.get("code") == "name_taken"
+ and "is already in this team" in reply.get("error", ""), reply)
+else:
+ check("the socket's path is short enough to reach (set TMPDIR shorter)", False, sock_path)
code = re.search(r"Invite code for a teammate: ([a-z]+(?:-[a-z]+){3})", cli("alex", "invite", "--code"))
_, out = run(mal, "join", code.group(1), "--proof", proof("u_mallory", "mallory", mal_ep), "--no-setup")
check("a sign-in joins only with a link, never a four-word code", "not a four-word code" in out and "Joined" not in out, out)