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:
1247 | direction | frame |1247 | direction | frame |
1248 |-----------|-------|1248 |-----------|-------|
1249 | server β†’ | `{"type":"state", status, attempt, deadline, remaining_ms, now, answered, question_count}` on connect, every ~2 s, and after each save |1249 | 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` |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, seq?}` β€” the autosave ack |1251+| server β†’ | `{"type":"saved", question_id, updated_at, client_seq?}` β€” the autosave ack |
1252 | client β†’ | `{"type":"finish"}` β€” submit the attempt |1252 | client β†’ | `{"type":"finish"}` β€” submit the attempt |
1253 | server β†’ | `{"type":"finished", finished_at}`, then Close |1253 | server β†’ | `{"type":"finished", finished_at}`, then Close |
1254 | server β†’ | `{"type":"expired"}`, then Close β€” a tick noticed the deadline |1254 | server β†’ | `{"type":"expired"}`, then Close β€” a tick noticed the deadline |
1255 | client β†’ | `{"type":"ping"}` β†’ server `{"type":"pong"}` |1255 | 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, … |
1257 1257
1258-An `answer` may carry a `seq`: any number the client picks, echoed verbatim on1258+An `answer` may carry a `client_seq`: any number the client picks, echoed verbatim on
1259 that message's `saved` or `error` and on nothing else. The server never reads1259 that message's `saved` or `error` and on nothing else. The server never reads
1260 it β€” no uniqueness, no ordering, no dedupe, all the client's business β€” and1260 it β€” no uniqueness, no ordering, no dedupe, all the client's business β€” and
1261 omits the key entirely when the request omitted it, so leaving it out keeps1261 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
1265 UI would claim "Saved" for an answer the server never took. `question_id` on1265 UI would claim "Saved" for an answer the server never took. `question_id` on
1266 an `error` is the separate, orthogonal question: whether the failure belongs1266 an `error` is the separate, orthogonal question: whether the failure belongs
1267 to that one question (payload or question) or to the room, in which case every1267 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` without1268+save in flight is equally refused. An error can carry `client_seq` without
1269 `question_id`.1269 `question_id`.
1270 1270
1271 Every tick and every save re-read the exam, so a mid-exam `ends_at` extension1271 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`)
1294 ws.onmessage = (e) => {1294 ws.onmessage = (e) => {
1295 const m = JSON.parse(e.data);1295 const m = JSON.parse(e.data);
1296 if (m.type === "state") renderCountdown(m.remaining_ms, m.answered, m.question_count);1296 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);
1298 };1298 };
1299-let seq = 0;1299+let clientSeq = 0;
1300 const save = (question_id, selected) =>1300 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 }));
1302 ```1302 ```
1303 1303
1304 The live monitor rides along: each roster row now carries `answered` and1304 The live monitor rides along: each roster row now carries `answered` and
src/web/exam_ws.rsmodified+21 -17
@@ -10,25 +10,25 @@
10 //! server β†’ client10 //! server β†’ client
11 //! - `{"type":"state", status, attempt, deadline, remaining_ms, now, answered, question_count}`11 //! - `{"type":"state", status, attempt, deadline, remaining_ms, now, answered, question_count}`
12 //! on connect, every [`EXAM_WS_TICK_SECS`], and after each save12 //! on connect, every [`EXAM_WS_TICK_SECS`], and after each save
13-//! - `{"type":"saved", question_id, updated_at, seq?}` β€” an answer landed13+//! - `{"type":"saved", question_id, updated_at, client_seq?}` β€” an answer landed
14 //! - `{"type":"pong"}`14 //! - `{"type":"pong"}`
15 //! - `{"type":"finished", finished_at}` then Close β€” submitted (here or elsewhere)15 //! - `{"type":"finished", finished_at}` then Close β€” submitted (here or elsewhere)
16 //! - `{"type":"expired"}` then Close β€” the deadline passed mid-session16 //! - `{"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,
18 //! validation, deadline. `question_id` is present only when the failure18 //! validation, deadline. `question_id` is present only when the failure
19 //! belongs to that one `answer` (its payload or its question); absent means19 //! belongs to that one `answer` (its payload or its question); absent means
20 //! the failure is connection- or sitting-level, so every save in flight is20 //! the failure is connection- or sitting-level, so every save in flight is
21 //! equally refused21 //! equally refused
22 //!22 //!
23-//! `seq` on either reply is whatever the `answer` sent, echoed verbatim: the23+//! `client_seq` on either reply is whatever the `answer` sent, echoed verbatim: the
24 //! server never reads it, never dedupes on it, and omits the key entirely when24 //! server never reads it, never dedupes on it, and omits the key entirely when
25 //! the request omitted it. `question_id` cannot serve as the correlation id β€”25 //! the request omitted it. `question_id` cannot serve as the correlation id β€”
26 //! a re-save of the same question after a timeout leaves two sends26 //! a re-save of the same question after a timeout leaves two sends
27 //! outstanding for it.27 //! outstanding for it.
28 //!28 //!
29 //! client β†’ server29 //! client β†’ server
30-//! - `{"type":"answer", question_id, selected? | text?, seq?}` (`selected` =30+//! - `{"type":"answer", question_id, selected? | text?, client_seq?}` (`selected` =
31-//! choice id, `seq` = the client's own correlation id)31+//! choice id, `client_seq` = the client's own correlation id)
32 //! - `{"type":"finish"}`32 //! - `{"type":"finish"}`
33 //! - `{"type":"ping"}`33 //! - `{"type":"ping"}`
34 //!34 //!
@@ -110,7 +110,7 @@ enum ClientMessage {
110 /// has two sends outstanding, and the first reply would otherwise110 /// has two sends outstanding, and the first reply would otherwise
111 /// settle the second.111 /// settle the second.
112 #[serde(default)]112 #[serde(default)]
113- seq: Option<u64>,113+ client_seq: Option<u64>,
114 },114 },
115 Finish,115 Finish,
116 Ping,116 Ping,
@@ -379,11 +379,11 @@ async fn handle_message(
379 question_id,379 question_id,
380 selected,380 selected,
381 text,381 text,
382- seq,382+ client_seq,
383 } => {383 } => {
384 // Cap the key before it can be echoed β€” see [`MAX_QUESTION_ID_LEN`].384 // Cap the key before it can be echoed β€” see [`MAX_QUESTION_ID_LEN`].
385 if let Err(err) = validate_required("question_id", &question_id, MAX_QUESTION_ID_LEN) {385 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);
387 return send(socket, frame).await;387 return send(socket, frame).await;
388 }388 }
389 // Re-read the exam so the save is judged against the *current*389 // Re-read the exam so the save is judged against the *current*
@@ -416,7 +416,7 @@ async fn handle_message(
416 "question_id": answer.get_question().key(),416 "question_id": answer.get_question().key(),
417 "updated_at": answer.get_updated_at().as_millis(),417 "updated_at": answer.get_updated_at().as_millis(),
418 });418 });
419- with_seq(&mut frame, seq);419+ with_client_seq(&mut frame, client_seq);
420 send(socket, frame).await?;420 send(socket, frame).await?;
421 // Progress changed β€” refresh the countdown/answered state421 // Progress changed β€” refresh the countdown/answered state
422 // right away rather than waiting out the tick.422 // right away rather than waiting out the tick.
@@ -426,7 +426,11 @@ async fn handle_message(
426 let attributed = in_save && attributable(&err);426 let attributed = in_save && attributable(&err);
427 send(427 send(
428 socket,428 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+ ),
430 )434 )
431 .await435 .await
432 }436 }
@@ -485,21 +489,21 @@ fn error_frame(err: &AppError) -> Value {
485 }489 }
486 490
487 /// Attach the request's correlation id, if it sent one. Omitted stays omitted491 /// 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-identical492+/// β€” never `null` β€” so a client that sends no `client_seq` sees byte-identical
489 /// frames.493 /// frames.
490-fn with_seq(frame: &mut Value, seq: Option<u64>) {494+fn with_client_seq(frame: &mut Value, client_seq: Option<u64>) {
491- if let Some(seq) = seq {495+ if let Some(client_seq) = client_seq {
492- frame["seq"] = json!(seq);496+ frame["client_seq"] = json!(client_seq);
493 }497 }
494 }498 }
495 499
496 /// [`error_frame`] plus the optional blame: `question_id` when the failure500 /// [`error_frame`] plus the optional blame: `question_id` when the failure
497-/// belongs to one `answer` message, and `seq` whenever the `answer` carried501+/// belongs to one `answer` message, and `client_seq` whenever the `answer` carried
498 /// one β€” the two are independent, so an unattributable failure is still502 /// one β€” the two are independent, so an unattributable failure is still
499 /// matchable to the send that caused it. Absent keeps its original meaning β€”503 /// matchable to the send that caused it. Absent keeps its original meaning β€”
500 /// an unattributed failure β€” so a client that ignores the fields behaves504 /// an unattributed failure β€” so a client that ignores the fields behaves
501 /// exactly as before.505 /// 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 {
503 let message = match err {507 let message = match err {
504 AppError::Validation(err) => err.to_string(),508 AppError::Validation(err) => err.to_string(),
505 AppError::NotFound => "not found".to_string(),509 AppError::NotFound => "not found".to_string(),
@@ -526,6 +530,6 @@ fn error_frame_for(err: &AppError, question: Option<&str>, seq: Option<u64>) ->
526 if let Some(question) = question {530 if let Some(question) = question {
527 frame["question_id"] = json!(question);531 frame["question_id"] = json!(question);
528 }532 }
529- with_seq(&mut frame, seq);533+ with_client_seq(&mut frame, client_seq);
530 frame534 frame
531 }535 }
tests/e2e.rsmodified+12 -12
@@ -688,13 +688,13 @@ async fn exam_room_websocket_round_trip() {
688 let saved = ws_frame_of_type(&mut ws, "saved").await;688 let saved = ws_frame_of_type(&mut ws, "saved").await;
689 assert_eq!(saved["question_id"], question_id.as_str());689 assert_eq!(saved["question_id"], question_id.as_str());
690 assert!(saved["updated_at"].as_i64().is_some());690 assert!(saved["updated_at"].as_i64().is_some());
691- // No `seq` was sent, so no `seq` comes back β€” not even a null. Clients691+ // No `client_seq` was sent, so no `client_seq` comes back β€” not even a null. Clients
692 // that predate the field see exactly the frames they always saw.692 // 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}");
694 let state = ws_frame_of_type(&mut ws, "state").await;694 let state = ws_frame_of_type(&mut ws, "state").await;
695 assert_eq!(state["answered"], 1, "{state}");695 assert_eq!(state["answered"], 1, "{state}");
696 696
697- // With a `seq`, the ack carries it back verbatim: `question_id` alone697+ // With a `client_seq`, the ack carries it back verbatim: `question_id` alone
698 // cannot settle a send, since a re-save after a timeout leaves two of them698 // cannot settle a send, since a re-save after a timeout leaves two of them
699 // outstanding for the same question. The server assigns it no meaning β€”699 // outstanding for the same question. The server assigns it no meaning β€”
700 // a repeat of an already-used value is saved and echoed like any other.700 // 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() {
702 ws_send(702 ws_send(
703 &mut ws,703 &mut ws,
704 json!({ "type": "answer", "question_id": question_id,704 json!({ "type": "answer", "question_id": question_id,
705- "selected": room.choice_ids[1], "seq": 7 }),705+ "selected": room.choice_ids[1], "client_seq": 7 }),
706 )706 )
707 .await;707 .await;
708 let saved = ws_frame_of_type(&mut ws, "saved").await;708 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}");
710 assert_eq!(saved["question_id"], question_id.as_str());710 assert_eq!(saved["question_id"], question_id.as_str());
711 ws_frame_of_type(&mut ws, "state").await;711 ws_frame_of_type(&mut ws, "state").await;
712 }712 }
@@ -715,23 +715,23 @@ async fn exam_room_websocket_round_trip() {
715 // fail the exact send, even when the error names no question.715 // fail the exact send, even when the error names no question.
716 ws_send(716 ws_send(
717 &mut ws,717 &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 }),
719 )719 )
720 .await;720 .await;
721 let error = ws_frame_of_type(&mut ws, "error").await;721 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}");
723 assert!(error.get("question_id").is_none(), "{error}");723 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.
725 ws_send(725 ws_send(
726 &mut ws,726 &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 }),
728 )728 )
729 .await;729 .await;
730 let error = ws_frame_of_type(&mut ws, "error").await;730 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}");
732 assert_eq!(error["question_id"], question_id.as_str(), "{error}");732 assert_eq!(error["question_id"], question_id.as_str(), "{error}");
733 733
734- // `seq` is legal only because it is a declared field: everything else on734+ // `client_seq` is legal only because it is a declared field: everything else on
735 // an `answer` is still refused outright.735 // an `answer` is still refused outright.
736 ws_send(736 ws_send(
737 &mut ws,737 &mut ws,
@@ -744,7 +744,7 @@ async fn exam_room_websocket_round_trip() {
744 error["message"].as_str().unwrap().contains("unrecognized"),744 error["message"].as_str().unwrap().contains("unrecognized"),
745 "{error}"745 "{error}"
746 );746 );
747- assert!(error.get("seq").is_none(), "{error}");747+ assert!(error.get("client_seq").is_none(), "{error}");
748 748
749 // A payload that doesn't fit the question is an error frame, not a close.749 // A payload that doesn't fit the question is an error frame, not a close.
750 ws_send(750 ws_send(