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

@@ -12,7 +12,10 @@ import java.net.URL
// carrying the server's own explanation where it sent one, since those
// messages are written to be read on this screen.
private const val CONNECT_TIMEOUT_MS = 5000
// Shared with EventStream.kt, which connects the same way but then reads
// without a deadline.
const val CONNECT_TIMEOUT_MS = 5000
private const val READ_TIMEOUT_MS = 5000
class ApiException(message: String, cause: Throwable? = null) : Exception(message, cause)
@@ -32,7 +35,7 @@ fun <T> requestFromServer(
jsonBody: String? = null,
/** Raw request body as content-type to bytes -- the upload path. */
binaryBody: Pair<String, ByteArray>? = null,
readTimeoutMs: Int = 5000,
readTimeoutMs: Int = READ_TIMEOUT_MS,
readBody: (HttpURLConnection) -> T,
): T {
val connection = URL("${settings.baseUrl}$path").openConnection() as HttpURLConnection
@@ -88,6 +91,19 @@ fun <T> requestFromServer(
}
}
/** The response body as one JSON object. */
private fun HttpURLConnection.jsonObject(): JSONObject =
JSONObject(inputStream.bufferedReader().readText())
/** The response body as a JSON array of objects, each mapped through [parse]. */
private fun <T> HttpURLConnection.jsonObjects(parse: (JSONObject) -> T): List<T> =
JSONArray(inputStream.bufferedReader().readText()).mapObjects(parse)
private fun <T> JSONArray.mapObjects(parse: (JSONObject) -> T): List<T> =
(0 until length()).map { parse(getJSONObject(it)) }
private fun JSONArray.strings(): List<String> = (0 until length()).map { getString(it) }
// One row of GET /sessions. `provider` is what runs it, `host` where --
// the two are independent, so a session names both.
data class SessionSummary(
@@ -111,10 +127,7 @@ private fun parseSession(session: JSONObject) = SessionSummary(
)
fun fetchSessions(settings: ServerSettings): List<SessionSummary> =
requestFromServer(settings, "/sessions") { connection ->
val sessions = JSONArray(connection.inputStream.bufferedReader().readText())
(0 until sessions.length()).map { parseSession(sessions.getJSONObject(it)) }
}
requestFromServer(settings, "/sessions") { it.jsonObjects(::parseSession) }
// What the server offers, so the spawn screen has no hardcoded lists: a
// provider or host added to the server's config.json appears here with no
@@ -125,23 +138,19 @@ data class RemoteHost(val name: String, val address: String)
fun fetchProviders(settings: ServerSettings): List<Provider> =
requestFromServer(settings, "/providers") { connection ->
val providers = JSONArray(connection.inputStream.bufferedReader().readText())
(0 until providers.length()).map { i ->
val provider = providers.getJSONObject(i)
val models = provider.optJSONArray("models")
connection.jsonObjects { provider ->
Provider(
name = provider.getString("name"),
kind = provider.getString("kind"),
models = (0 until (models?.length() ?: 0)).map { models!!.getString(it) },
// Omitted entirely when the provider offers none.
models = provider.optJSONArray("models")?.strings().orEmpty(),
)
}
}
fun fetchHosts(settings: ServerSettings): List<RemoteHost> =
requestFromServer(settings, "/hosts") { connection ->
val hosts = JSONArray(connection.inputStream.bufferedReader().readText())
(0 until hosts.length()).map { i ->
val host = hosts.getJSONObject(i)
connection.jsonObjects { host ->
RemoteHost(name = host.getString("name"), address = host.getString("address"))
}
}
@@ -171,7 +180,7 @@ fun spawnSession(
}.toString(),
readTimeoutMs = 30000,
) { connection ->
parseSession(JSONObject(connection.inputStream.bufferedReader().readText()))
parseSession(connection.jsonObject())
}
fun sendMessage(
@@ -212,7 +221,7 @@ fun uploadAttachment(
binaryBody = "multipart/form-data; boundary=$boundary" to (head + bytes + tail),
readTimeoutMs = 60000,
) { connection ->
JSONObject(connection.inputStream.bufferedReader().readText()).getString("id")
connection.jsonObject().getString("id")
}
}
@@ -240,16 +249,12 @@ data class UsageSnapshot(
/** The backend caches; refreshing more often than its poll interval just re-reads the cache. */
fun fetchUsage(settings: ServerSettings): List<UsageSnapshot> =
requestFromServer(settings, "/usage", readTimeoutMs = 30000) { connection ->
val snapshots = JSONArray(connection.inputStream.bufferedReader().readText())
(0 until snapshots.length()).map { i ->
val snapshot = snapshots.getJSONObject(i)
val windows = snapshot.getJSONArray("windows")
connection.jsonObjects { snapshot ->
UsageSnapshot(
provider = snapshot.getString("provider"),
available = snapshot.getBoolean("available"),
error = snapshot.optString("error").ifEmpty { null },
windows = (0 until windows.length()).map { j ->
val window = windows.getJSONObject(j)
windows = snapshot.getJSONArray("windows").mapObjects { window ->
UsageWindow(
label = window.getString("label"),
percent = window.getDouble("percent"),