Skip to content

Commit 20dd96a

Browse files
bostonaholicmboston Implementerclaude
committed
fix(cli): read the reaction target from the writer
The target lookup gates the write, so a read replica that has not caught up must not turn a just-published message into not_found. Co-authored-by: mboston Implementer <932d3b6ebea6cc83e091551ae46d1c7e7f8f7d0fa8e9cef5e6facc8544af413b@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matthew Boston <[email protected]>
1 parent 30e216e commit 20dd96a

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

‎crates/buzz-cli/src/commands/reactions.rs‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub async fn cmd_add_reaction(
3535
}
3636

3737
async fn fetch_target_author(client: &BuzzClient, event_id: &str) -> Result<PublicKey, CliError> {
38-
let filter = serde_json::json!({ "ids": [event_id], "limit": 1 });
38+
// Strong: a target just written may not have reached the read replica yet.
39+
let filter = serde_json::json!({ "ids": [event_id], "limit": 1, "consistency": "strong" });
3940
let raw = client.query(&filter).await?;
4041
let events: serde_json::Value = serde_json::from_str(&raw)
4142
.map_err(|e| CliError::Other(format!("failed to parse query response: {e}")))?;
@@ -172,20 +173,26 @@ mod tests {
172173

173174
type Submitted = Arc<Mutex<Option<serde_json::Value>>>;
174175

175-
async fn fake_relay(query_body: String) -> (String, Submitted) {
176+
async fn fake_relay(query_body: String) -> (String, Submitted, Submitted) {
176177
let submitted: Submitted = Arc::new(Mutex::new(None));
177-
let state = (query_body, submitted.clone());
178+
let queried: Submitted = Arc::new(Mutex::new(None));
179+
let state = (query_body, submitted.clone(), queried.clone());
178180
let app = Router::new()
179181
.route(
180182
"/query",
181-
post(|State((body, _)): State<(String, Submitted)>| async move {
182-
(StatusCode::OK, [("content-type", "application/json")], body)
183-
}),
183+
post(
184+
|State((body, _, queried)): State<(String, Submitted, Submitted)>,
185+
request: Bytes| async move {
186+
*queried.lock().unwrap() = serde_json::from_slice(&request).ok();
187+
(StatusCode::OK, [("content-type", "application/json")], body)
188+
},
189+
),
184190
)
185191
.route(
186192
"/events",
187193
post(
188-
|State((_, submitted)): State<(String, Submitted)>, body: Bytes| async move {
194+
|State((_, submitted, _)): State<(String, Submitted, Submitted)>,
195+
body: Bytes| async move {
189196
*submitted.lock().unwrap() = serde_json::from_slice(&body).ok();
190197
(
191198
StatusCode::OK,
@@ -199,7 +206,7 @@ mod tests {
199206
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
200207
let address = listener.local_addr().unwrap();
201208
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
202-
(format!("http://{address}"), submitted)
209+
(format!("http://{address}"), submitted, queried)
203210
}
204211

205212
fn target_response() -> String {
@@ -214,7 +221,7 @@ mod tests {
214221

215222
#[tokio::test]
216223
async fn add_reaction_tags_target_author() {
217-
let (url, submitted) = fake_relay(target_response()).await;
224+
let (url, submitted, _) = fake_relay(target_response()).await;
218225
let client = BuzzClient::new(url, Keys::generate(), None, None).unwrap();
219226

220227
cmd_add_reaction(&client, TARGET_ID, "👍", None)
@@ -231,7 +238,7 @@ mod tests {
231238

232239
#[tokio::test]
233240
async fn add_custom_emoji_reaction_tags_target_author() {
234-
let (url, submitted) = fake_relay(target_response()).await;
241+
let (url, submitted, _) = fake_relay(target_response()).await;
235242
let client = BuzzClient::new(url, Keys::generate(), None, None).unwrap();
236243

237244
cmd_add_reaction(
@@ -252,12 +259,33 @@ mod tests {
252259

253260
#[tokio::test]
254261
async fn add_reaction_to_unknown_event_fails_without_submitting() {
255-
let (url, submitted) = fake_relay("[]".to_string()).await;
262+
let (url, submitted, _) = fake_relay("[]".to_string()).await;
256263
let client = BuzzClient::new(url, Keys::generate(), None, None).unwrap();
257264

258265
let result = cmd_add_reaction(&client, TARGET_ID, "👍", None).await;
259266

260267
assert!(matches!(result, Err(CliError::NotFound(_))));
261268
assert!(submitted.lock().unwrap().is_none());
262269
}
270+
271+
#[tokio::test]
272+
async fn add_reaction_reads_target_from_the_writer() {
273+
let (url, _, queried) = fake_relay(target_response()).await;
274+
let client = BuzzClient::new(url, Keys::generate(), None, None).unwrap();
275+
276+
cmd_add_reaction(&client, TARGET_ID, "👍", None)
277+
.await
278+
.unwrap();
279+
280+
let query = queried.lock().unwrap().clone().expect("target queried");
281+
let filter = query
282+
.as_array()
283+
.and_then(|filters| filters.first())
284+
.unwrap_or(&query);
285+
assert_eq!(
286+
filter.get("consistency").and_then(|value| value.as_str()),
287+
Some("strong"),
288+
"the target lookup gates a write, so replica lag must not hide the target: {query}"
289+
);
290+
}
263291
}

0 commit comments

Comments
 (0)