Files
magicka-vm/crates/web_client/src/lib.rs
T
linus-dandClaude Opus 4.8 9d9d5ce41c Add web game (plan2.md) on the independent runtime, merge-blocking gates
Builds the browser game around the existing Rust runtime: a window into the
universe, not a second simulation. Pure std, no external crates.

New crates:
- protocol: versioned, hashable client/server messages + hand-rolled JSON value
  and total parser (malformed packet -> Err, never panic).
- game_runtime: authoritative match state. Resolves turns through the
  INDEPENDENT interpreter (runtime_under_test::native_resolve), not the
  reference engine; filters visibility/knowledge; records and regenerates
  replays. A match is a pure function of (seed, roster, ordered inputs).
- web_assets/web_client: embedded browser client (arena, rune editor,
  knowledge panels, replay viewer) + static HTTP delivery.
- server: std::net HTTP + WebSocket (hand-rolled SHA-1/base64/RFC-6455 framing),
  turn timer, disconnect handling, panic-proof dispatch, poison-tolerant lock.
- web_tests: dependency-free WebSocket test client + Phase H gates.

Trust hardening per review:
- game_runtime no longer delegates to reference_runtime::execute; it runs the
  independent interpreter that the runtime-equivalence gate proves correct.
- Protocol/socket/replay/visibility/resilience gates are merge-blocking
  (added to the merge_group-required job in merge-gates.yml): 1k matches/0
  drift, 10k fuzz/0 panics, 100 headless socket E2E, 0 hidden-state leaks.
- Rendered-browser E2E is marked EXTERNAL-BLOCKED: Playwright runs advisory-only
  (continue-on-error, artifacts) until CI infrastructure with a browser exists;
  it is treated as unsatisfied, not green. The headless 100-match gate is
  labeled protocol-level coverage, not rendered-browser coverage.
- README documents the hand-rolled crypto/parser audit risk explicitly.

Fixes an integer-overflow panic in observed-volatility inference (i64 sum /
abs near i64::MIN) that could poison the server mutex.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 20:20:46 -07:00

55 lines
1.7 KiB
Rust

//! `web_client` — the client delivery layer. It owns *how* the embedded
//! [`web_assets`] reach the browser (the HTTP response framing), keeping the
//! raw asset bytes (`web_assets`) separate from delivery concerns. The server
//! depends on this crate, not on `web_assets` directly.
pub use web_assets::{resolve, Asset};
/// Build a complete HTTP/1.1 response for a static GET path. Returns `None`
/// for unknown paths so the caller can emit a 404.
pub fn http_response(path: &str) -> Option<Vec<u8>> {
let asset = resolve(path)?;
let body = asset.body.as_bytes();
let mut out = Vec::with_capacity(body.len() + 128);
let header = format!(
"HTTP/1.1 200 OK\r\nContent-Type: {}\r\nContent-Length: {}\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n",
asset.content_type,
body.len()
);
out.extend_from_slice(header.as_bytes());
out.extend_from_slice(body);
Some(out)
}
/// The canonical 404 response.
pub fn not_found() -> Vec<u8> {
let body = b"404 not found";
let mut out = Vec::new();
let header = format!(
"HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);
out.extend_from_slice(header.as_bytes());
out.extend_from_slice(body);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serves_shell() {
let r = http_response("/").unwrap();
let s = String::from_utf8_lossy(&r);
assert!(s.starts_with("HTTP/1.1 200 OK"));
assert!(s.contains("text/html"));
assert!(s.contains("Magicka VM"));
}
#[test]
fn unknown_path_is_none() {
assert!(http_response("/secret").is_none());
}
}