Skip to content

Commit 27e4184

Browse files
mariusaefacebook-github-bot
authored andcommitted
'this' -> 'cx' in handlers (#429)
Summary: Pull Request resolved: #429 Now that we have altered actor `handle` method to take a `Context`, let's also name the variables accordingly. This was a mix of LLM edits and manual ones: I ended up trying Devmate, Claude, and Gemini. Interestingly, this ended up being kryptonite for both Devmate and Claude. Gemini did a little better, but got itself into some weird loops too. Here's the initial prompt: ``` In hyperactor/src/actor.rs, we define a Handler trait: `pub trait Handler<M>: Actor { /// Handle the next M-typed message. async fn handle(&mut self, this: &Context<Self>, message: M) -> Result<(), anyhow::Error>; }` The reason the Context-typed argument is called 'this' is that the type used to be called 'Instance'.Can you rename all instances of this argument to 'cx' instead? And also all of the usages of course.Please do this for all of the rust files in this project. ``` After Gemini got a start, but got stuck, I tried Claude again with the following prompt: ``` This repository contains an in-progress change to change instances of "this: &hyperactor::Context<...>" to "cx: &hyperactor::Context<...>" and then update the usages │ │ (this->cx). Can you finish the job? ``` It claimed it was done a few times, and I had to then cajole it into completing the task by showing grep output with evidence to the contrary. There were also a few instances of ... frankly strange hallucinations. For example, replacing `this` with `1`. Maybe the models are trying to keep us on our toes. (It would be simpler if Devmate could just use the LSP to perform the renames. In that case, it would only need to identify the candidate sites, and instruct rust-analyzer to do the job.) ghstack-source-id: 294345876 exported-using-ghexport Reviewed By: shayne-fletcher Differential Revision: D77753150 fbshipit-source-id: b67e4cb67db4f0f963bf5954fa084946ae685e37
1 parent d702385 commit 27e4184

File tree

33 files changed

+476
-527
lines changed

33 files changed

+476
-527
lines changed

controller/src/lib.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ impl Actor for ControllerActor {
149149
})
150150
}
151151

152-
async fn init(&mut self, this: &hyperactor::Instance<Self>) -> Result<(), anyhow::Error> {
152+
async fn init(&mut self, cx: &hyperactor::Instance<Self>) -> Result<(), anyhow::Error> {
153153
self.comm_actor_ref.send(
154-
this,
154+
cx,
155155
CommActorMode::ImplicitWithWorldId(self.worker_gang_ref.gang_id().world_id().clone()),
156156
)?;
157157
Ok(())
@@ -213,12 +213,12 @@ impl ControllerActor {
213213
// M = self.worker_progress_check_interval
214214
async fn request_status_if_needed(
215215
&mut self,
216-
this: &Context<'_, Self>,
216+
cx: &Context<'_, Self>,
217217
) -> Result<(), anyhow::Error> {
218218
if let Some((expected_seq, ..)) = self.history.deadline(
219219
self.operations_per_worker_progress_request,
220220
self.operation_timeout,
221-
this.clock(),
221+
cx.clock(),
222222
) {
223223
if self.last_controller_request_status.is_none_or(
224224
|(last_requested_seq, last_requested_time)| {
@@ -232,7 +232,7 @@ impl ControllerActor {
232232
) {
233233
// Send to all workers.
234234
self.send(
235-
this,
235+
cx,
236236
Ranks::Slice(
237237
ndslice::Slice::new(0, vec![self.history.world_size()], vec![1]).unwrap(),
238238
),
@@ -245,7 +245,7 @@ impl ControllerActor {
245245
.await?;
246246

247247
self.last_controller_request_status =
248-
Some((expected_seq.clone(), this.clock().now()));
248+
Some((expected_seq.clone(), cx.clock().now()));
249249
}
250250
}
251251

@@ -260,18 +260,18 @@ struct CheckWorkerProgress;
260260
impl Handler<CheckWorkerProgress> for ControllerActor {
261261
async fn handle(
262262
&mut self,
263-
this: &Context<Self>,
263+
cx: &Context<Self>,
264264
_check_worker_progress: CheckWorkerProgress,
265265
) -> Result<(), anyhow::Error> {
266266
let client = self.client()?;
267267

268268
if let Some((expected_seq, deadline, reported)) = self.history.deadline(
269269
self.operations_per_worker_progress_request,
270270
self.operation_timeout,
271-
this.clock(),
271+
cx.clock(),
272272
) {
273273
if !reported
274-
&& this.clock().now() > deadline
274+
&& cx.clock().now() > deadline
275275
&& expected_seq >= self.history.min_incomplete_seq_reported()
276276
{
277277
let timed_out_ranks = self
@@ -297,7 +297,7 @@ impl Handler<CheckWorkerProgress> for ControllerActor {
297297
self.operation_timeout.as_secs()
298298
);
299299
if client
300-
.log(this, LogLevel::Warn, message.clone())
300+
.log(cx, LogLevel::Warn, message.clone())
301301
.await
302302
.is_ok()
303303
{
@@ -307,7 +307,7 @@ impl Handler<CheckWorkerProgress> for ControllerActor {
307307
if self.fail_on_worker_timeout {
308308
client
309309
.result(
310-
this,
310+
cx,
311311
expected_seq,
312312
Some(Err(Exception::Failure(DeviceFailure {
313313
actor_id: self.worker_gang_ref.rank(failed_rank).actor_id().clone(),
@@ -318,10 +318,10 @@ impl Handler<CheckWorkerProgress> for ControllerActor {
318318
.await?;
319319
}
320320
}
321-
self.request_status_if_needed(this).await?;
321+
self.request_status_if_needed(cx).await?;
322322
}
323323

324-
this.self_message_with_delay(CheckWorkerProgress, self.worker_progress_check_interval)?;
324+
cx.self_message_with_delay(CheckWorkerProgress, self.worker_progress_check_interval)?;
325325
Ok(())
326326
}
327327
}
@@ -360,7 +360,7 @@ fn slice_to_selection(slice: Slice) -> Selection {
360360
impl ControllerMessageHandler for ControllerActor {
361361
async fn attach(
362362
&mut self,
363-
this: &Context<Self>,
363+
cx: &Context<Self>,
364364
client_actor: ActorRef<ClientActor>,
365365
) -> Result<(), anyhow::Error> {
366366
tracing::debug!("attaching client actor {}", client_actor);
@@ -369,34 +369,34 @@ impl ControllerMessageHandler for ControllerActor {
369369
.map_err(|actor_ref| anyhow::anyhow!("client actor {} already attached", actor_ref))?;
370370

371371
// Trigger periodical checking of supervision status and worker progress.
372-
this.self_message_with_delay(
372+
cx.self_message_with_delay(
373373
ControllerMessage::CheckSupervision {},
374374
self.supervision_query_interval,
375375
)?;
376-
this.self_message_with_delay(CheckWorkerProgress, self.worker_progress_check_interval)?;
376+
cx.self_message_with_delay(CheckWorkerProgress, self.worker_progress_check_interval)?;
377377
Ok(())
378378
}
379379

380380
async fn node(
381381
&mut self,
382-
this: &Context<Self>,
382+
cx: &Context<Self>,
383383
seq: Seq,
384384
defs: Vec<Ref>,
385385
uses: Vec<Ref>,
386386
) -> Result<(), anyhow::Error> {
387387
let failures = self.history.add_invocation(seq, uses, defs);
388388
let client = self.client()?;
389389
for (seq, failure) in failures {
390-
let _ = client.result(this, seq, failure).await;
390+
let _ = client.result(cx, seq, failure).await;
391391
}
392-
self.request_status_if_needed(this).await?;
392+
self.request_status_if_needed(cx).await?;
393393

394394
Ok(())
395395
}
396396

397397
async fn drop_refs(
398398
&mut self,
399-
_this: &Context<Self>,
399+
_cx: &Context<Self>,
400400
refs: Vec<Ref>,
401401
) -> Result<(), anyhow::Error> {
402402
self.history.delete_invocations_for_refs(refs);
@@ -405,7 +405,7 @@ impl ControllerMessageHandler for ControllerActor {
405405

406406
async fn send(
407407
&mut self,
408-
this: &Context<Self>,
408+
cx: &Context<Self>,
409409
ranks: Ranks,
410410
message: Serialized,
411411
) -> Result<(), anyhow::Error> {
@@ -423,7 +423,7 @@ impl ControllerMessageHandler for ControllerActor {
423423
}),
424424
};
425425
let message = CastMessageEnvelope::from_serialized(
426-
this.self_id().clone(),
426+
cx.self_id().clone(),
427427
DestinationPort::new::<WorkerActor, WorkerMessage>(
428428
// This is awkward, but goes away entirely with meshes.
429429
self.worker_gang_ref
@@ -442,7 +442,7 @@ impl ControllerMessageHandler for ControllerActor {
442442
.reshape_with_limit(Limit::from(CASTING_FANOUT_SIZE));
443443

444444
self.comm_actor_ref.send(
445-
this,
445+
cx,
446446
CastMessage {
447447
dest: Uslice {
448448
// TODO: pass both slice and selection from client side
@@ -457,20 +457,20 @@ impl ControllerMessageHandler for ControllerActor {
457457

458458
async fn remote_function_failed(
459459
&mut self,
460-
this: &Context<Self>,
460+
cx: &Context<Self>,
461461
seq: Seq,
462462
error: WorkerError,
463463
) -> Result<(), anyhow::Error> {
464464
let rank = error.worker_actor_id.rank();
465465
self.history
466466
.propagate_exception(seq, Exception::Error(seq, seq, error.clone()));
467-
mark_worker_complete_and_propagate_exceptions(self, this, rank, &seq).await?;
467+
mark_worker_complete_and_propagate_exceptions(self, cx, rank, &seq).await?;
468468
Ok(())
469469
}
470470

471471
async fn status(
472472
&mut self,
473-
_this: &Context<Self>,
473+
cx: &Context<Self>,
474474
seq: Seq,
475475
worker_actor_id: ActorId,
476476
controller: bool,
@@ -480,26 +480,26 @@ impl ControllerMessageHandler for ControllerActor {
480480
if controller {
481481
self.history.update_deadline_tracking(rank, seq);
482482
} else {
483-
mark_worker_complete_and_propagate_exceptions(self, _this, rank, &seq).await?;
483+
mark_worker_complete_and_propagate_exceptions(self, cx, rank, &seq).await?;
484484
}
485485
Ok(())
486486
}
487487

488488
async fn fetch_result(
489489
&mut self,
490-
_this: &Context<Self>,
490+
_cx: &Context<Self>,
491491
seq: Seq,
492492
result: Result<Serialized, WorkerError>,
493493
) -> Result<(), anyhow::Error> {
494494
self.history.set_result(seq, result);
495495
Ok(())
496496
}
497497

498-
async fn check_supervision(&mut self, this: &Context<Self>) -> Result<(), anyhow::Error> {
498+
async fn check_supervision(&mut self, cx: &Context<Self>) -> Result<(), anyhow::Error> {
499499
let gang_id: GangId = self.worker_gang_ref.clone().into();
500500
let world_state = self
501501
.system_supervision_actor_ref
502-
.state(this, gang_id.world_id().clone())
502+
.state(cx, gang_id.world_id().clone())
503503
.await?;
504504

505505
if let Some(world_state) = world_state {
@@ -545,7 +545,7 @@ impl ControllerMessageHandler for ControllerActor {
545545
tracing::error!("Sending failure to client: {exc:?}");
546546
// Seq does not matter as the client will raise device error immediately before setting the results.
547547
self.client()?
548-
.result(this, Seq::default(), Some(Err(exc)))
548+
.result(cx, Seq::default(), Some(Err(exc)))
549549
.await?;
550550
tracing::error!("Failure successfully sent to client");
551551

@@ -554,7 +554,7 @@ impl ControllerMessageHandler for ControllerActor {
554554
}
555555

556556
// Schedule the next supervision check.
557-
this.self_message_with_delay(
557+
cx.self_message_with_delay(
558558
ControllerMessage::CheckSupervision {},
559559
self.supervision_query_interval,
560560
)?;
@@ -563,27 +563,27 @@ impl ControllerMessageHandler for ControllerActor {
563563

564564
async fn debugger_message(
565565
&mut self,
566-
this: &Context<Self>,
566+
cx: &Context<Self>,
567567
debugger_actor_id: ActorId,
568568
action: DebuggerAction,
569569
) -> Result<(), anyhow::Error> {
570570
self.client()?
571-
.debugger_message(this, debugger_actor_id, action)
571+
.debugger_message(cx, debugger_actor_id, action)
572572
.await
573573
}
574574

575575
#[cfg(test)]
576576
async fn get_first_incomplete_seqs_unit_tests_only(
577577
&mut self,
578-
_this: &Context<Self>,
578+
_cx: &Context<Self>,
579579
) -> Result<Vec<Seq>, anyhow::Error> {
580580
Ok(self.history.first_incomplete_seqs().to_vec())
581581
}
582582

583583
#[cfg(not(test))]
584584
async fn get_first_incomplete_seqs_unit_tests_only(
585585
&mut self,
586-
_this: &Context<Self>,
586+
_cx: &Context<Self>,
587587
) -> Result<Vec<Seq>, anyhow::Error> {
588588
unimplemented!("get_first_incomplete_seqs_unit_tests_only is only for unit tests")
589589
}
@@ -1798,7 +1798,7 @@ mod tests {
17981798
impl PanickingMessageHandler for PanickingActor {
17991799
async fn panic(
18001800
&mut self,
1801-
_this: &Context<Self>,
1801+
_cx: &Context<Self>,
18021802
err_msg: String,
18031803
) -> Result<(), anyhow::Error> {
18041804
panic!("{}", err_msg);

hyper/src/commands/demo.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,29 +230,29 @@ impl Actor for DemoActor {
230230
impl DemoMessageHandler for DemoActor {
231231
async fn echo(
232232
&mut self,
233-
_this: &Context<Self>,
233+
_cx: &Context<Self>,
234234
message: String,
235235
) -> Result<String, anyhow::Error> {
236236
tracing::info!("demo: message: {}", message);
237237
Ok(message)
238238
}
239239

240-
async fn increment(&mut self, _this: &Context<Self>, num: u64) -> Result<u64, anyhow::Error> {
240+
async fn increment(&mut self, _cx: &Context<Self>, num: u64) -> Result<u64, anyhow::Error> {
241241
tracing::info!("demo: increment: {}", num);
242242
Ok(num + 1)
243243
}
244244

245-
async fn panic(&mut self, _this: &Context<Self>) -> Result<(), anyhow::Error> {
245+
async fn panic(&mut self, _cx: &Context<Self>) -> Result<(), anyhow::Error> {
246246
tracing::info!("demo: panic!");
247247
panic!()
248248
}
249249

250-
async fn spawn_child(&mut self, this: &Context<Self>) -> Result<ActorRef<Self>, anyhow::Error> {
250+
async fn spawn_child(&mut self, cx: &Context<Self>) -> Result<ActorRef<Self>, anyhow::Error> {
251251
tracing::info!("demo: spawn child");
252-
Ok(Self::spawn(this, ()).await?.bind())
252+
Ok(Self::spawn(cx, ()).await?.bind())
253253
}
254254

255-
async fn error(&mut self, _this: &Context<Self>, message: String) -> Result<(), anyhow::Error> {
255+
async fn error(&mut self, _cx: &Context<Self>, message: String) -> Result<(), anyhow::Error> {
256256
tracing::info!("demo: message: {}", message);
257257
anyhow::bail!("{}", message)
258258
}

hyperactor/example/derive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ impl Actor for ShoppingListActor {
6868
#[async_trait]
6969
#[hyperactor::forward(ShoppingList)]
7070
impl ShoppingListHandler for ShoppingListActor {
71-
async fn add(&mut self, _this: &Context<Self>, item: String) -> Result<(), anyhow::Error> {
71+
async fn add(&mut self, _cx: &Context<Self>, item: String) -> Result<(), anyhow::Error> {
7272
eprintln!("insert {}", item);
7373
self.0.insert(item);
7474
Ok(())
7575
}
7676

77-
async fn remove(&mut self, _this: &Context<Self>, item: String) -> Result<(), anyhow::Error> {
77+
async fn remove(&mut self, _cx: &Context<Self>, item: String) -> Result<(), anyhow::Error> {
7878
eprintln!("remove {}", item);
7979
self.0.remove(&item);
8080
Ok(())
8181
}
8282

83-
async fn exists(&mut self, _this: &Context<Self>, item: String) -> Result<bool, anyhow::Error> {
83+
async fn exists(&mut self, _cx: &Context<Self>, item: String) -> Result<bool, anyhow::Error> {
8484
Ok(self.0.contains(&item))
8585
}
8686

87-
async fn list(&mut self, _this: &Context<Self>) -> Result<Vec<String>, anyhow::Error> {
87+
async fn list(&mut self, _cx: &Context<Self>) -> Result<Vec<String>, anyhow::Error> {
8888
Ok(self.0.iter().cloned().collect())
8989
}
9090
}

0 commit comments

Comments
 (0)