monarch_messages/
client.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9use enum_as_inner::EnumAsInner;
10use hyperactor::ActorId;
11use hyperactor::HandleClient;
12use hyperactor::Handler;
13use hyperactor::RefClient;
14use serde::Deserialize;
15use serde::Serialize;
16use typeuri::Named;
17
18use crate::controller::DeviceFailure;
19use crate::controller::Seq;
20use crate::controller::WorkerError;
21use crate::debugger::DebuggerAction;
22
23/// An exception to commanded execution, exposed to the client.
24#[derive(
25    Debug,
26    Clone,
27    Serialize,
28    Deserialize,
29    PartialEq,
30    thiserror::Error,
31    EnumAsInner
32)]
33pub enum Exception {
34    /// A failure is a non-deterministic problem with the underlying device
35    /// or its infrastructure. For example, a controller may enter a crash loop,
36    /// or its GPU may be lost
37    #[error("failure: {0}")]
38    Failure(#[from] DeviceFailure),
39
40    /// A deterministic problem with the user's code. For example, an OOM
41    /// resulting in trying to allocate too much GPU memory, or violating
42    /// some invariant enforced by the various APIs.
43    #[error("WorkerError: seq: {0}, error: {1}")]
44    Error(Seq, Seq, WorkerError),
45}
46
47/// Log levels for ClientMessage::Log.
48#[derive(Debug, Deserialize, Clone, Serialize)]
49pub enum LogLevel {
50    /// Log with severity INFO
51    Info,
52
53    /// Log with severity WARNING
54    Warn,
55
56    /// Log with severity ERROR
57    Error,
58}
59
60/// Client messages. These define the messages that the controller can
61/// send to the client. The actual handling of these messages will be
62/// defined on the python side in the client implementation.
63// TODO: Potentially just create a Client derive macro that can be used
64// to just generate the client code without generating the handler for
65// cases where the actor will be implemented in a different language over
66// ffi.
67#[derive(
68    Handler,
69    HandleClient,
70    RefClient,
71    Serialize,
72    Deserialize,
73    Debug,
74    Clone,
75    EnumAsInner,
76    Named
77)]
78pub enum ClientMessage {
79    /// A fetched result of an invoked operation.
80    Result {
81        seq: Seq,
82        result: Option<Result<wirevalue::Any, Exception>>,
83    },
84
85    /// Notify the client of an event.
86    Log { level: LogLevel, message: String },
87
88    /// Notify the client of a debugger event.
89    DebuggerMessage {
90        /// The actor id of the debugger.
91        debugger_actor_id: ActorId,
92        /// The action to take.
93        action: DebuggerAction,
94    },
95}
96
97hyperactor::behavior!(ClientActor, ClientMessage);