Cleanup pass: one home for duplicated logic, stale comments out

Nothing behavioral except two status codes; mostly removing places where
the same rule was written down more than once and could drift.

- server/src/private.rs: the owner-only create/write helpers, which
  config.rs, certs.rs, and the session dirs each had their own copy of
  (certs.rs even duplicated the explanatory comment). One module owns the
  modes now, so the "nothing this server writes is readable by anyone
  else" property is checkable in one place.
- server/src/media.rs: the image media-type/extension table, which the
  four places that have to agree on it each spelled out separately --
  storing an upload, serving it back, building a content block, saving a
  produced image. The differing *defaults* stay at the call sites with
  the reasoning, since they genuinely differ by direction.
- routes.rs: a missing file was a 400 and an unreadable one a 400 with a
  hand-rolled log line; they are now 404 and Internal respectively.
  UnknownSession became NotFound, since it was the only 404-with-message.
- main.rs: xdg_dir takes the variable's value instead of reading the
  environment, which drops the unsafe set_var from its test and lets the
  test actually assert the relative-path rule.
- echo.rs had its own 4-byte hex generator beside session::random_hex.
- claude.rs: the two impl Translator blocks were one type's methods.
- Stale comments: phase-2 markers on shipped work, a permission-mode list
  that had drifted from the CLI's, "dev-updater" as the leaf certificate's
  fallback common name, a half-written sentence in build-apk.sh.
- App: the JSONArray walk written out in four fetchers, the four
  near-identical BackHandlers in AppRoot, and SessionScreen's inline
  fully-qualified names where the file otherwise imports.
- server/wg-test.log was committed by accident; *.log is ignored now, and
  the gitignore comments describe where state actually lives.
- PLAN.md's backend layout gains the new modules and drops hosts.rs for
  the ssh.rs that was built instead.

Verified: 35 server tests, clippy clean, app compiles warning-free, and a
scratch server driven over curl -- attachment upload/serve round-trip with
both a known and an unknown content type, the new 404s, transcript and
session-dir deletion, plus a real claude-cli session answering a prompt.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017xn8nHw1tw1R6PtiY1eEtw
This commit is contained in:
irisandClaude Opus 5 committed 2026-08-25 16:10:33 -04:00
1 parent d4a4ee7808
commit 99bcc341c1
18 files changed
+295 -241

No files matched your search

+15 -17
View File
@@ -69,8 +69,8 @@ pub fn router(manager: Arc<SessionManager>) -> Router {
#[derive(Debug, thiserror::Error)]
enum ApiError {
#[error("no session {0}")]
UnknownSession(String),
#[error("{0}")]
NotFound(String),
#[error("no such route")]
UnknownRoute,
#[error("{0}")]
@@ -82,7 +82,7 @@ enum ApiError {
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let status = match self {
Self::UnknownSession(_) | Self::UnknownRoute => StatusCode::NOT_FOUND,
Self::NotFound(_) | Self::UnknownRoute => StatusCode::NOT_FOUND,
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
Self::Internal(err) => {
// The only variant whose real cause isn't safe to hand
@@ -103,7 +103,7 @@ fn bad_request(err: anyhow::Error) -> ApiError {
}
fn lookup(manager: &SessionManager, id: &str) -> Result<Arc<LiveSession>, ApiError> {
manager.session(id).ok_or_else(|| ApiError::UnknownSession(id.to_string()))
manager.session(id).ok_or_else(|| ApiError::NotFound(format!("no session {id}")))
}
async fn list_sessions(State(manager): State<Arc<SessionManager>>) -> axum::Json<Vec<SessionInfo>> {
@@ -207,8 +207,8 @@ async fn delete_session(
#[serde(rename_all = "camelCase")]
struct MessageRequest {
text: String,
/// Ids from `POST /attachments` (phase 2); accepted now so the request
/// shape doesn't change under the app.
/// Ids from `POST /attachments`, uploaded before the message that
/// references them.
#[serde(default)]
attachment_ids: Vec<String>,
}
@@ -326,18 +326,16 @@ async fn serve_file(
let candidates =
[session.dir().join("files").join(&name), session.dir().join("attachments").join(&name)];
let Some(path) = candidates.iter().find(|path| path.is_file()) else {
return Err(ApiError::BadRequest(format!("no file {name} in session {id}")));
};
let bytes = std::fs::read(path).map_err(|err| {
tracing::error!("read {} failed: {err}", path.display());
ApiError::BadRequest("file unreadable".to_string())
})?;
let content_type = match name.rsplit('.').next() {
Some("png") => "image/png",
Some("gif") => "image/gif",
Some("webp") => "image/webp",
_ => "image/jpeg",
return Err(ApiError::NotFound(format!("no file {name} in session {id}")));
};
// A file that is there but unreadable is this server's fault, not the
// request's -- Internal logs it and says nothing more to the caller.
let bytes = std::fs::read(path)
.with_context(|| format!("read {}", path.display()))
.map_err(ApiError::Internal)?;
// Names are server-generated, so an unrecognized extension can only
// mean a file this server didn't write.
let content_type = crate::media::media_type_for(&name).unwrap_or("image/jpeg");
Ok(([(axum::http::header::CONTENT_TYPE, content_type)], bytes).into_response())
}