forge
Explore
Log in

Don't you dare go Hollow

🏷️ rename the echoed exam-room seq to client_seq

Browse files at this commit

61bd820ec6fe786b3ce32d89bb04038db99abd82Ahmet Kaan GümüşParent: 9e25eac
  • style
    • 🏷️ rename the echoed exam-room seq to client_seq
Show full message
style: 🏷️ rename the echoed exam-room seq to client_seq

`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.mdmodified+8 -8
@@ -1247,15 +1247,15 @@ frames:
12471247 | direction | frame |
12481248 |-----------|-------|
12491249 | server β†’ | `{"type":"state", status, attempt, deadline, remaining_ms, now, answered, question_count}` on connect, every ~2 s, and after each save |
1250-| client β†’ | `{"type":"answer", "question_id":"…", "selected":1}` or `{"type":"answer", "question_id":"…", "text":"…"}`, optionally `+ "seq":7` |
1251-| server β†’ | `{"type":"saved", question_id, updated_at, seq?}` β€” the autosave ack |
1250+| client β†’ | `{"type":"answer", "question_id":"…", "selected":1}` or `{"type":"answer", "question_id":"…", "text":"…"}`, optionally `+ "client_seq":7` |
1251+| server β†’ | `{"type":"saved", question_id, updated_at, client_seq?}` β€” the autosave ack |
12521252 | client β†’ | `{"type":"finish"}` β€” submit the attempt |
12531253 | server β†’ | `{"type":"finished", finished_at}`, then Close |
12541254 | server β†’ | `{"type":"expired"}`, then Close β€” a tick noticed the deadline |
12551255 | client β†’ | `{"type":"ping"}` β†’ server `{"type":"pong"}` |
1256-| server β†’ | `{"type":"error", message, question_id?, seq?}` β€” bad JSON, wrong kind, deadline, … |
1256+| server β†’ | `{"type":"error", message, question_id?, client_seq?}` β€” bad JSON, wrong kind, deadline, … |
12571257
1258-An `answer` may carry a `seq`: any number the client picks, echoed verbatim on
1258+An `answer` may carry a `client_seq`: any number the client picks, echoed verbatim on
12591259 that message's `saved` or `error` and on nothing else. The server never reads
12601260 it β€” no uniqueness, no ordering, no dedupe, all the client's business β€” and
12611261 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
12651265 UI would claim "Saved" for an answer the server never took. `question_id` on
12661266 an `error` is the separate, orthogonal question: whether the failure belongs
12671267 to that one question (payload or question) or to the room, in which case every
1268-save in flight is equally refused. An error can carry `seq` without
1268+save in flight is equally refused. An error can carry `client_seq` without
12691269 `question_id`.
12701270
12711271 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`)
12941294 ws.onmessage = (e) => {
12951295 const m = JSON.parse(e.data);
12961296 if (m.type === "state") renderCountdown(m.remaining_ms, m.answered, m.question_count);
1297- if (m.type === "saved") settle(m.seq, m.question_id);
1297+ if (m.type === "saved") settle(m.client_seq, m.question_id);
12981298 };
1299-let seq = 0;
1299+let clientSeq = 0;
13001300 const save = (question_id, selected) =>
1301- ws.send(JSON.stringify({ type: "answer", question_id, selected, seq: ++seq }));
1301+ ws.send(JSON.stringify({ type: "answer", question_id, selected, client_seq: ++clientSeq }));
13021302 ```
13031303
13041304 The live monitor rides along: each roster row now carries `answered` and
src/web/exam_ws.rsmodified+21 -17
@@ -10,25 +10,25 @@
1010 //! server β†’ client
1111 //! - `{"type":"state", status, attempt, deadline, remaining_ms, now, answered, question_count}`
1212 //! on connect, every [`EXAM_WS_TICK_SECS`], and after each save
13-//! - `{"type":"saved", question_id, updated_at, seq?}` β€” an answer landed
13+//! - `{"type":"saved", question_id, updated_at, client_seq?}` β€” an answer landed
1414 //! - `{"type":"pong"}`
1515 //! - `{"type":"finished", finished_at}` then Close β€” submitted (here or elsewhere)
1616 //! - `{"type":"expired"}` then Close β€” the deadline passed mid-session
17-//! - `{"type":"error", message, question_id?, seq?}` β€” bad JSON, unknown type,
17+//! - `{"type":"error", message, question_id?, client_seq?}` β€” bad JSON, unknown type,
1818 //! validation, deadline. `question_id` is present only when the failure
1919 //! belongs to that one `answer` (its payload or its question); absent means
2020 //! the failure is connection- or sitting-level, so every save in flight is
2121 //! equally refused
2222 //!
23-//! `seq` on either reply is whatever the `answer` sent, echoed verbatim: the
23+//! `client_seq` on either reply is whatever the `answer` sent, echoed verbatim: the
2424 //! server never reads it, never dedupes on it, and omits the key entirely when
2525 //! the request omitted it. `question_id` cannot serve as the correlation id β€”
2626 //! a re-save of the same question after a timeout leaves two sends
2727 //! outstanding for it.
2828 //!
2929 //! client β†’ server
30-//! - `{"type":"answer", question_id, selected? | text?, seq?}` (`selected` =
31-//! choice id, `seq` = the client's own correlation id)
30+//! - `{"type":"answer", question_id, selected? | text?, client_seq?}` (`selected` =
31+//! choice id, `client_seq` = the client's own correlation id)
3232 //! - `{"type":"finish"}`
3333 //! - `{"type":"ping"}`
3434 //!
@@ -110,7 +110,7 @@ enum ClientMessage {
110110 /// has two sends outstanding, and the first reply would otherwise
111111 /// settle the second.
112112 #[serde(default)]
113- seq: Option<u64>,
113+ client_seq: Option<u64>,
114114 },
115115 Finish,
116116 Ping,
@@ -379,11 +379,11 @@ async fn handle_message(
379379 question_id,
380380 selected,
381381 text,
382- seq,
382+ client_seq,
383383 } => {
384384 // Cap the key before it can be echoed β€” see [`MAX_QUESTION_ID_LEN`].
385385 if let Err(err) = validate_required("question_id", &question_id, MAX_QUESTION_ID_LEN) {
386- let frame = error_frame_for(&AppError::Validation(err), None, seq);
386+ let frame = error_frame_for(&AppError::Validation(err), None, client_seq);
387387 return send(socket, frame).await;
388388 }
389389 // Re-read the exam so the save is judged against the *current*
@@ -416,7 +416,7 @@ async fn handle_message(
416416 "question_id": answer.get_question().key(),
417417 "updated_at": answer.get_updated_at().as_millis(),
418418 });
419- with_seq(&mut frame, seq);
419+ with_client_seq(&mut frame, client_seq);
420420 send(socket, frame).await?;
421421 // Progress changed β€” refresh the countdown/answered state
422422 // right away rather than waiting out the tick.
@@ -426,7 +426,11 @@ async fn handle_message(
426426 let attributed = in_save && attributable(&err);
427427 send(
428428 socket,
429- error_frame_for(&err, attributed.then_some(question_id.as_str()), seq),
429+ error_frame_for(
430+ &err,
431+ attributed.then_some(question_id.as_str()),
432+ client_seq,
433+ ),
430434 )
431435 .await
432436 }
@@ -485,21 +489,21 @@ fn error_frame(err: &AppError) -> Value {
485489 }
486490
487491 /// Attach the request's correlation id, if it sent one. Omitted stays omitted
488-/// β€” never `null` β€” so a client that sends no `seq` sees byte-identical
492+/// β€” never `null` β€” so a client that sends no `client_seq` sees byte-identical
489493 /// frames.
490-fn with_seq(frame: &mut Value, seq: Option<u64>) {
491- if let Some(seq) = seq {
492- frame["seq"] = json!(seq);
494+fn with_client_seq(frame: &mut Value, client_seq: Option<u64>) {
495+ if let Some(client_seq) = client_seq {
496+ frame["client_seq"] = json!(client_seq);
493497 }
494498 }
495499
496500 /// [`error_frame`] plus the optional blame: `question_id` when the failure
497-/// belongs to one `answer` message, and `seq` whenever the `answer` carried
501+/// belongs to one `answer` message, and `client_seq` whenever the `answer` carried
498502 /// one β€” the two are independent, so an unattributable failure is still
499503 /// matchable to the send that caused it. Absent keeps its original meaning β€”
500504 /// an unattributed failure β€” so a client that ignores the fields behaves
501505 /// exactly as before.
502-fn error_frame_for(err: &AppError, question: Option<&str>, seq: Option<u64>) -> Value {
506+fn error_frame_for(err: &AppError, question: Option<&str>, client_seq: Option<u64>) -> Value {
503507 let message = match err {
504508 AppError::Validation(err) => err.to_string(),
505509 AppError::NotFound => "not found".to_string(),
@@ -526,6 +530,6 @@ fn error_frame_for(err: &AppError, question: Option<&str>, seq: Option<u64>) ->
526530 if let Some(question) = question {
527531 frame["question_id"] = json!(question);
528532 }
529- with_seq(&mut frame, seq);
533+ with_client_seq(&mut frame, client_seq);
530534 frame
531535 }
tests/e2e.rsmodified+12 -12
@@ -688,13 +688,13 @@ async fn exam_room_websocket_round_trip() {
688688 let saved = ws_frame_of_type(&mut ws, "saved").await;
689689 assert_eq!(saved["question_id"], question_id.as_str());
690690 assert!(saved["updated_at"].as_i64().is_some());
691- // No `seq` was sent, so no `seq` comes back β€” not even a null. Clients
691+ // No `client_seq` was sent, so no `client_seq` comes back β€” not even a null. Clients
692692 // that predate the field see exactly the frames they always saw.
693- assert!(saved.get("seq").is_none(), "{saved}");
693+ assert!(saved.get("client_seq").is_none(), "{saved}");
694694 let state = ws_frame_of_type(&mut ws, "state").await;
695695 assert_eq!(state["answered"], 1, "{state}");
696696
697- // With a `seq`, the ack carries it back verbatim: `question_id` alone
697+ // With a `client_seq`, the ack carries it back verbatim: `question_id` alone
698698 // cannot settle a send, since a re-save after a timeout leaves two of them
699699 // outstanding for the same question. The server assigns it no meaning β€”
700700 // 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() {
702702 ws_send(
703703 &mut ws,
704704 json!({ "type": "answer", "question_id": question_id,
705- "selected": room.choice_ids[1], "seq": 7 }),
705+ "selected": room.choice_ids[1], "client_seq": 7 }),
706706 )
707707 .await;
708708 let saved = ws_frame_of_type(&mut ws, "saved").await;
709- assert_eq!(saved["seq"], 7, "{saved}");
709+ assert_eq!(saved["client_seq"], 7, "{saved}");
710710 assert_eq!(saved["question_id"], question_id.as_str());
711711 ws_frame_of_type(&mut ws, "state").await;
712712 }
@@ -715,23 +715,23 @@ async fn exam_room_websocket_round_trip() {
715715 // fail the exact send, even when the error names no question.
716716 ws_send(
717717 &mut ws,
718- json!({ "type": "answer", "question_id": "x".repeat(70_000), "text": "x", "seq": 8 }),
718+ json!({ "type": "answer", "question_id": "x".repeat(70_000), "text": "x", "client_seq": 8 }),
719719 )
720720 .await;
721721 let error = ws_frame_of_type(&mut ws, "error").await;
722- assert_eq!(error["seq"], 8, "{error}");
722+ assert_eq!(error["client_seq"], 8, "{error}");
723723 assert!(error.get("question_id").is_none(), "{error}");
724- // ... and `seq` rides alongside the blame when there is one.
724+ // ... and `client_seq` rides alongside the blame when there is one.
725725 ws_send(
726726 &mut ws,
727- json!({ "type": "answer", "question_id": question_id, "text": "4", "seq": 9 }),
727+ json!({ "type": "answer", "question_id": question_id, "text": "4", "client_seq": 9 }),
728728 )
729729 .await;
730730 let error = ws_frame_of_type(&mut ws, "error").await;
731- assert_eq!(error["seq"], 9, "{error}");
731+ assert_eq!(error["client_seq"], 9, "{error}");
732732 assert_eq!(error["question_id"], question_id.as_str(), "{error}");
733733
734- // `seq` is legal only because it is a declared field: everything else on
734+ // `client_seq` is legal only because it is a declared field: everything else on
735735 // an `answer` is still refused outright.
736736 ws_send(
737737 &mut ws,
@@ -744,7 +744,7 @@ async fn exam_room_websocket_round_trip() {
744744 error["message"].as_str().unwrap().contains("unrecognized"),
745745 "{error}"
746746 );
747- assert!(error.get("seq").is_none(), "{error}");
747+ assert!(error.get("client_seq").is_none(), "{error}");
748748
749749 // A payload that doesn't fit the question is an error frame, not a close.
750750 ws_send(