From 61bd820ec6fe786b3ce32d89bb04038db99abd82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 25 Jul 2026 09:26:05 +0300 Subject: [PATCH] =?UTF-8?q?style:=20=F0=9F=8F=B7=EF=B8=8F=20rename=20the?= =?UTF-8?q?=20echoed=20exam-room=20seq=20to=20client=5Fseq?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `seq` already means the attempt number everywhere else in this domain — it is the key of an exam sitting, and the room's own `state` frame ships it as `attempt`. A second `seq` on the neighbouring frames read as the same thing. Wire rename only, and the field shipped minutes ago, so nothing is depending on the old spelling yet. --- README.md | 16 ++++++++-------- src/web/exam_ws.rs | 38 +++++++++++++++++++++----------------- tests/e2e.rs | 24 ++++++++++++------------ 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 747a004..06735b9 100644 --- a/README.md +++ b/README.md @@ -1247,15 +1247,15 @@ frames: | direction | frame | |-----------|-------| | server → | `{"type":"state", status, attempt, deadline, remaining_ms, now, answered, question_count}` on connect, every ~2 s, and after each save | -| client → | `{"type":"answer", "question_id":"…", "selected":1}` or `{"type":"answer", "question_id":"…", "text":"…"}`, optionally `+ "seq":7` | -| server → | `{"type":"saved", question_id, updated_at, seq?}` — the autosave ack | +| client → | `{"type":"answer", "question_id":"…", "selected":1}` or `{"type":"answer", "question_id":"…", "text":"…"}`, optionally `+ "client_seq":7` | +| server → | `{"type":"saved", question_id, updated_at, client_seq?}` — the autosave ack | | client → | `{"type":"finish"}` — submit the attempt | | server → | `{"type":"finished", finished_at}`, then Close | | server → | `{"type":"expired"}`, then Close — a tick noticed the deadline | | client → | `{"type":"ping"}` → server `{"type":"pong"}` | -| server → | `{"type":"error", message, question_id?, seq?}` — bad JSON, wrong kind, deadline, … | +| server → | `{"type":"error", message, question_id?, client_seq?}` — bad JSON, wrong kind, deadline, … | -An `answer` may carry a `seq`: any number the client picks, echoed verbatim on +An `answer` may carry a `client_seq`: any number the client picks, echoed verbatim on that message's `saved` or `error` and on nothing else. The server never reads it — no uniqueness, no ordering, no dedupe, all the client's business — and omits the key entirely when the request omitted it, so leaving it out keeps @@ -1265,7 +1265,7 @@ outstanding for the same id — the first reply would settle the second and the UI would claim "Saved" for an answer the server never took. `question_id` on an `error` is the separate, orthogonal question: whether the failure belongs to that one question (payload or question) or to the room, in which case every -save in flight is equally refused. An error can carry `seq` without +save in flight is equally refused. An error can carry `client_seq` without `question_id`. Every tick and every save re-read the exam, so a mid-exam `ends_at` extension @@ -1294,11 +1294,11 @@ const ws = new WebSocket(`${BASE.replace("http", "ws")}/exams/${id}/attempt/ws`) ws.onmessage = (e) => { const m = JSON.parse(e.data); if (m.type === "state") renderCountdown(m.remaining_ms, m.answered, m.question_count); - if (m.type === "saved") settle(m.seq, m.question_id); + if (m.type === "saved") settle(m.client_seq, m.question_id); }; -let seq = 0; +let clientSeq = 0; const save = (question_id, selected) => - ws.send(JSON.stringify({ type: "answer", question_id, selected, seq: ++seq })); + ws.send(JSON.stringify({ type: "answer", question_id, selected, client_seq: ++clientSeq })); ``` The live monitor rides along: each roster row now carries `answered` and diff --git a/src/web/exam_ws.rs b/src/web/exam_ws.rs index a30f741..d0ff4d4 100644 --- a/src/web/exam_ws.rs +++ b/src/web/exam_ws.rs @@ -10,25 +10,25 @@ //! server → client //! - `{"type":"state", status, attempt, deadline, remaining_ms, now, answered, question_count}` //! on connect, every [`EXAM_WS_TICK_SECS`], and after each save -//! - `{"type":"saved", question_id, updated_at, seq?}` — an answer landed +//! - `{"type":"saved", question_id, updated_at, client_seq?}` — an answer landed //! - `{"type":"pong"}` //! - `{"type":"finished", finished_at}` then Close — submitted (here or elsewhere) //! - `{"type":"expired"}` then Close — the deadline passed mid-session -//! - `{"type":"error", message, question_id?, seq?}` — bad JSON, unknown type, +//! - `{"type":"error", message, question_id?, client_seq?}` — bad JSON, unknown type, //! validation, deadline. `question_id` is present only when the failure //! belongs to that one `answer` (its payload or its question); absent means //! the failure is connection- or sitting-level, so every save in flight is //! equally refused //! -//! `seq` on either reply is whatever the `answer` sent, echoed verbatim: the +//! `client_seq` on either reply is whatever the `answer` sent, echoed verbatim: the //! server never reads it, never dedupes on it, and omits the key entirely when //! the request omitted it. `question_id` cannot serve as the correlation id — //! a re-save of the same question after a timeout leaves two sends //! outstanding for it. //! //! client → server -//! - `{"type":"answer", question_id, selected? | text?, seq?}` (`selected` = -//! choice id, `seq` = the client's own correlation id) +//! - `{"type":"answer", question_id, selected? | text?, client_seq?}` (`selected` = +//! choice id, `client_seq` = the client's own correlation id) //! - `{"type":"finish"}` //! - `{"type":"ping"}` //! @@ -110,7 +110,7 @@ enum ClientMessage { /// has two sends outstanding, and the first reply would otherwise /// settle the second. #[serde(default)] - seq: Option, + client_seq: Option, }, Finish, Ping, @@ -379,11 +379,11 @@ async fn handle_message( question_id, selected, text, - seq, + client_seq, } => { // Cap the key before it can be echoed — see [`MAX_QUESTION_ID_LEN`]. if let Err(err) = validate_required("question_id", &question_id, MAX_QUESTION_ID_LEN) { - let frame = error_frame_for(&AppError::Validation(err), None, seq); + let frame = error_frame_for(&AppError::Validation(err), None, client_seq); return send(socket, frame).await; } // Re-read the exam so the save is judged against the *current* @@ -416,7 +416,7 @@ async fn handle_message( "question_id": answer.get_question().key(), "updated_at": answer.get_updated_at().as_millis(), }); - with_seq(&mut frame, seq); + with_client_seq(&mut frame, client_seq); send(socket, frame).await?; // Progress changed — refresh the countdown/answered state // right away rather than waiting out the tick. @@ -426,7 +426,11 @@ async fn handle_message( let attributed = in_save && attributable(&err); send( socket, - error_frame_for(&err, attributed.then_some(question_id.as_str()), seq), + error_frame_for( + &err, + attributed.then_some(question_id.as_str()), + client_seq, + ), ) .await } @@ -485,21 +489,21 @@ fn error_frame(err: &AppError) -> Value { } /// Attach the request's correlation id, if it sent one. Omitted stays omitted -/// — never `null` — so a client that sends no `seq` sees byte-identical +/// — never `null` — so a client that sends no `client_seq` sees byte-identical /// frames. -fn with_seq(frame: &mut Value, seq: Option) { - if let Some(seq) = seq { - frame["seq"] = json!(seq); +fn with_client_seq(frame: &mut Value, client_seq: Option) { + if let Some(client_seq) = client_seq { + frame["client_seq"] = json!(client_seq); } } /// [`error_frame`] plus the optional blame: `question_id` when the failure -/// belongs to one `answer` message, and `seq` whenever the `answer` carried +/// belongs to one `answer` message, and `client_seq` whenever the `answer` carried /// one — the two are independent, so an unattributable failure is still /// matchable to the send that caused it. Absent keeps its original meaning — /// an unattributed failure — so a client that ignores the fields behaves /// exactly as before. -fn error_frame_for(err: &AppError, question: Option<&str>, seq: Option) -> Value { +fn error_frame_for(err: &AppError, question: Option<&str>, client_seq: Option) -> Value { let message = match err { AppError::Validation(err) => err.to_string(), AppError::NotFound => "not found".to_string(), @@ -526,6 +530,6 @@ fn error_frame_for(err: &AppError, question: Option<&str>, seq: Option) -> if let Some(question) = question { frame["question_id"] = json!(question); } - with_seq(&mut frame, seq); + with_client_seq(&mut frame, client_seq); frame } diff --git a/tests/e2e.rs b/tests/e2e.rs index 2e2848e..717c8f9 100644 --- a/tests/e2e.rs +++ b/tests/e2e.rs @@ -688,13 +688,13 @@ async fn exam_room_websocket_round_trip() { let saved = ws_frame_of_type(&mut ws, "saved").await; assert_eq!(saved["question_id"], question_id.as_str()); assert!(saved["updated_at"].as_i64().is_some()); - // No `seq` was sent, so no `seq` comes back — not even a null. Clients + // No `client_seq` was sent, so no `client_seq` comes back — not even a null. Clients // that predate the field see exactly the frames they always saw. - assert!(saved.get("seq").is_none(), "{saved}"); + assert!(saved.get("client_seq").is_none(), "{saved}"); let state = ws_frame_of_type(&mut ws, "state").await; assert_eq!(state["answered"], 1, "{state}"); - // With a `seq`, the ack carries it back verbatim: `question_id` alone + // With a `client_seq`, the ack carries it back verbatim: `question_id` alone // cannot settle a send, since a re-save after a timeout leaves two of them // outstanding for the same question. The server assigns it no meaning — // a repeat of an already-used value is saved and echoed like any other. @@ -702,11 +702,11 @@ async fn exam_room_websocket_round_trip() { ws_send( &mut ws, json!({ "type": "answer", "question_id": question_id, - "selected": room.choice_ids[1], "seq": 7 }), + "selected": room.choice_ids[1], "client_seq": 7 }), ) .await; let saved = ws_frame_of_type(&mut ws, "saved").await; - assert_eq!(saved["seq"], 7, "{saved}"); + assert_eq!(saved["client_seq"], 7, "{saved}"); assert_eq!(saved["question_id"], question_id.as_str()); ws_frame_of_type(&mut ws, "state").await; } @@ -715,23 +715,23 @@ async fn exam_room_websocket_round_trip() { // fail the exact send, even when the error names no question. ws_send( &mut ws, - json!({ "type": "answer", "question_id": "x".repeat(70_000), "text": "x", "seq": 8 }), + json!({ "type": "answer", "question_id": "x".repeat(70_000), "text": "x", "client_seq": 8 }), ) .await; let error = ws_frame_of_type(&mut ws, "error").await; - assert_eq!(error["seq"], 8, "{error}"); + assert_eq!(error["client_seq"], 8, "{error}"); assert!(error.get("question_id").is_none(), "{error}"); - // ... and `seq` rides alongside the blame when there is one. + // ... and `client_seq` rides alongside the blame when there is one. ws_send( &mut ws, - json!({ "type": "answer", "question_id": question_id, "text": "4", "seq": 9 }), + json!({ "type": "answer", "question_id": question_id, "text": "4", "client_seq": 9 }), ) .await; let error = ws_frame_of_type(&mut ws, "error").await; - assert_eq!(error["seq"], 9, "{error}"); + assert_eq!(error["client_seq"], 9, "{error}"); assert_eq!(error["question_id"], question_id.as_str(), "{error}"); - // `seq` is legal only because it is a declared field: everything else on + // `client_seq` is legal only because it is a declared field: everything else on // an `answer` is still refused outright. ws_send( &mut ws, @@ -744,7 +744,7 @@ async fn exam_room_websocket_round_trip() { error["message"].as_str().unwrap().contains("unrecognized"), "{error}" ); - assert!(error.get("seq").is_none(), "{error}"); + assert!(error.get("client_seq").is_none(), "{error}"); // A payload that doesn't fit the question is an error frame, not a close. ws_send(