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