tracel 0.12.0

High-level Tracel SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use std::ffi::OsString;
use std::path::PathBuf;
use std::sync::Arc;

use tracel_console::{Console, ConsoleError, CredentialSource, Env, ProjectHandle, ProjectRef};
use tracel_datasets::DatasetRegistry;
use tracel_experiment::Experiments;
use tracel_experiment::local::LocalExperiments;
use tracel_inference::{InferenceModule, NoopInferenceProvider};
use tracel_models::ModelRegistry;
#[cfg(feature = "station")]
use tracel_station::Station;
#[cfg(feature = "station")]
use url::Url;

use crate::running_job;

const TRACEL_TARGET: &str = "TRACEL_TARGET";
const TRACEL_RUNS_DIR: &str = "TRACEL_RUNS_DIR";
const TRACEL_API_KEY: &str = "TRACEL_API_KEY";
const TRACEL_NAMESPACE: &str = "TRACEL_NAMESPACE";
const TRACEL_PROJECT: &str = "TRACEL_PROJECT";
#[cfg(feature = "station")]
const TRACEL_STATION_URL: &str = "TRACEL_STATION_URL";

const DEFAULT_RUNS_DIR: &str = "./runs";
#[cfg(feature = "station")]
const DEFAULT_STATION_URL: &str = "http://localhost:8000";

/// The values `TRACEL_TARGET` accepts, as an error lists them.
#[cfg(not(feature = "station"))]
const TARGETS: &str = "offline or console";
#[cfg(feature = "station")]
const TARGETS: &str = "offline, console or station";

/// Where a program records its experiments and reaches models, datasets and inference
/// telemetry.
///
/// Build one directly, or read it from the environment with [`Target::from_env`]. Neither
/// performs network I/O, and neither do the services a target builds: the first request that
/// needs a server reaches it. Each call to a service method binds a new connection; to share one
/// across services, build them from a [`ProjectHandle`] instead.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Target {
    /// Records experiments on this machine and reaches no server.
    Offline {
        /// The directory runs are recorded under.
        dir: PathBuf,
    },
    /// A project on the Tracel console.
    Console {
        /// The console to reach.
        env: Env,
        /// Where the credential comes from.
        credentials: CredentialSource,
        /// The project experiments, models and datasets belong to.
        project: ProjectRef,
    },
    /// A Tracel Station (requires the `station` feature).
    #[cfg(feature = "station")]
    Station {
        /// The Station's base URL.
        url: Url,
    },
}

impl Target {
    /// Reads the target the environment names, without performing network I/O.
    ///
    /// `TRACEL_TARGET` picks the kind of target, and the variables that kind needs fill it in. An unset or empty variable takes its default:
    ///
    /// | Variable | Value | Default |
    /// | --- | --- | --- |
    #[cfg_attr(
        not(feature = "station"),
        doc = "| `TRACEL_TARGET` | `offline` or `console` | `offline` |"
    )]
    #[cfg_attr(
        feature = "station",
        doc = "| `TRACEL_TARGET` | `offline`, `console` or `station` | `offline` |"
    )]
    /// | `TRACEL_RUNS_DIR` | the directory offline runs are recorded under | `./runs` |
    /// | `TRACEL_ENV` | the console, as [`env_from_environment`](crate::console::env_from_environment) reads it | `Production` |
    /// | `TRACEL_API_KEY` | an API key or a job token | the stored `tracel login` session |
    /// | `TRACEL_NAMESPACE`, `TRACEL_PROJECT` | the console project | `namespace` and `project` in `tracel.toml` |
    #[cfg_attr(
        feature = "station",
        doc = "| `TRACEL_STATION_URL` | the Station's base URL | `http://localhost:8000` |"
    )]
    ///
    /// Fails with [`TargetError::UnknownTarget`] when `TRACEL_TARGET` names no target, and with
    /// [`TargetError::MissingSetting`] or [`TargetError::InvalidSetting`] naming the variable to
    /// set when the target cannot be filled in.
    pub fn from_env() -> Result<Target, TargetError> {
        Self::from_vars(|name| std::env::var_os(name))
    }

    /// [`from_env`](Self::from_env) over the values `lookup` gives this crate's variables. The
    /// console's own variables are read by [`tracel_console`].
    fn from_vars(lookup: impl Fn(&str) -> Option<OsString>) -> Result<Target, TargetError> {
        let var = |name: &str| lookup(name).filter(|value| !value.is_empty());
        let target = var(TRACEL_TARGET).map(|value| value.to_string_lossy().into_owned());

        match target.as_deref() {
            None | Some("offline") => Ok(Target::Offline {
                dir: var(TRACEL_RUNS_DIR).map_or_else(|| DEFAULT_RUNS_DIR.into(), PathBuf::from),
            }),
            Some("console") => Ok(Target::Console {
                env: tracel_console::env_from_environment()?,
                credentials: CredentialSource::from_env(),
                project: ProjectRef::from_env()?,
            }),
            #[cfg(feature = "station")]
            Some("station") => {
                let url = var(TRACEL_STATION_URL).map_or_else(
                    || DEFAULT_STATION_URL.to_string(),
                    |value| value.to_string_lossy().into_owned(),
                );
                match Url::parse(&url) {
                    Ok(url) if matches!(url.scheme(), "http" | "https") => {
                        Ok(Target::Station { url })
                    }
                    _ => Err(TargetError::InvalidSetting {
                        variable: TRACEL_STATION_URL,
                        value: url,
                        expected: "an http or https URL, such as http://localhost:8000",
                    }),
                }
            }
            #[cfg(not(feature = "station"))]
            Some("station") => Err(TargetError::FeatureRequired {
                target: "station",
                feature: "station",
            }),
            Some(value) => Err(TargetError::UnknownTarget {
                value: value.to_string(),
                expected: TARGETS,
            }),
        }
    }

    /// Returns the experiments this target records, without performing network I/O.
    ///
    /// Offline, the first run creates its directory.
    ///
    /// Built in a job, on the thread its adapter runs it on, the experiments are linked to that
    /// job, as its [`JobScope`](crate::runtime::JobScope) gives it, wherever runs are started from
    /// them: each experiment records the job's number, when its launcher gave one, as its
    /// `tracel.job_num` attribute and the job's input as its arguments, and prints where it
    /// records to stderr as it starts, such as `Recording experiment 42: <url>` or
    /// `Recording offline in <dir>`. Experiments built another way, such as
    /// [`ProjectHandle::experiments`] or [`Experiments::new`], are not linked unless the code links
    /// them through [`Experiments::attribute`], [`Experiments::arguments`] and
    /// [`Experiments::on_start`].
    pub fn experiments(&self) -> Result<Experiments, TargetError> {
        let experiments = match self {
            Target::Offline { dir } => Experiments::new(Arc::new(LocalExperiments::new(dir))),
            Target::Console {
                env,
                credentials,
                project,
            } => console_project(env, credentials, project)?.experiments(),
            #[cfg(feature = "station")]
            Target::Station { url } => Station::connect(url.clone()).experiments(),
        };
        Ok(running_job::linked(experiments))
    }

    /// Returns the model registry this target reaches, without performing network I/O.
    ///
    /// Fails with [`TargetError::Unsupported`] for an offline target.
    pub fn models(&self) -> Result<ModelRegistry, TargetError> {
        match self {
            Target::Offline { .. } => Err(TargetError::Unsupported {
                service: "models",
                target: "offline",
            }),
            Target::Console {
                env,
                credentials,
                project,
            } => Ok(console_project(env, credentials, project)?.models()),
            #[cfg(feature = "station")]
            Target::Station { url } => Ok(Station::connect(url.clone()).models()),
        }
    }

    /// Returns the dataset registry this target reaches, without performing network I/O.
    ///
    /// Fails with [`TargetError::Unsupported`] for an offline target.
    pub fn datasets(&self) -> Result<DatasetRegistry, TargetError> {
        match self {
            Target::Offline { .. } => Err(TargetError::Unsupported {
                service: "datasets",
                target: "offline",
            }),
            Target::Console {
                env,
                credentials,
                project,
            } => Ok(console_project(env, credentials, project)?.datasets()),
            #[cfg(feature = "station")]
            Target::Station { url } => Ok(Station::connect(url.clone()).datasets()),
        }
    }

    /// Returns the inference module this target ships telemetry to, without performing network
    /// I/O.
    ///
    /// Only a console target ships inference telemetry; elsewhere, sessions discard what they
    /// record. Build the module once and reuse it, as [`ProjectHandle::inference`] explains.
    pub fn inference(&self) -> Result<InferenceModule, TargetError> {
        match self {
            Target::Offline { .. } => Ok(noop_inference()),
            Target::Console {
                env,
                credentials,
                project,
            } => Ok(console_project(env, credentials, project)?.inference()),
            #[cfg(feature = "station")]
            Target::Station { .. } => Ok(noop_inference()),
        }
    }
}

/// Binds `project` on the console `env` names, with the credential `credentials` provides.
fn console_project(
    env: &Env,
    credentials: &CredentialSource,
    project: &ProjectRef,
) -> Result<ProjectHandle, TargetError> {
    let console = Console::connect(env.clone(), credentials.resolve(env)?);
    Ok(console.project(&project.namespace, &project.name))
}

fn noop_inference() -> InferenceModule {
    InferenceModule::new(Arc::new(NoopInferenceProvider::new()))
}

/// Errors reading a [`Target`] from the environment, or building a service from one.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TargetError {
    /// `TRACEL_TARGET` names no target.
    #[error("unknown TRACEL_TARGET value `{value}`: expected {expected}")]
    UnknownTarget {
        /// The value `TRACEL_TARGET` holds.
        value: String,
        /// The values it accepts.
        expected: &'static str,
    },
    /// `TRACEL_TARGET` names a target this build of `tracel` leaves out.
    #[error("TRACEL_TARGET={target} needs the `{feature}` feature of tracel")]
    FeatureRequired {
        /// The value `TRACEL_TARGET` holds.
        target: &'static str,
        /// The `tracel` feature that adds the target.
        feature: &'static str,
    },
    /// A setting the target needs is not set.
    #[error("{variable} is not set{}", fallback(.variable))]
    MissingSetting {
        /// The environment variable to set.
        variable: &'static str,
    },
    /// An environment variable holds a value the SDK does not accept.
    #[error("invalid {variable} value `{value}`: expected {expected}")]
    InvalidSetting {
        /// The environment variable.
        variable: &'static str,
        /// The value it holds.
        value: String,
        /// The values it accepts.
        expected: &'static str,
    },
    /// The target does not serve this service.
    #[error("the {target} target does not serve {service}")]
    Unsupported {
        /// The service asked for.
        service: &'static str,
        /// The kind of target that does not serve it.
        target: &'static str,
    },
    /// The console settings could not be read for another reason, such as a stored sign-in
    /// that cannot be read.
    #[error(transparent)]
    Console(ConsoleError),
}

/// What else provides a setting whose variable is unset, as [`TargetError::MissingSetting`] says.
fn fallback(variable: &str) -> &'static str {
    match variable {
        TRACEL_API_KEY => ", and there is no `tracel login` sign-in for this console",
        TRACEL_NAMESPACE => {
            ", and tracel.toml sets no `namespace` (expected keys: `namespace` and `project`)"
        }
        TRACEL_PROJECT => {
            ", and tracel.toml sets no `project` (expected keys: `namespace` and `project`)"
        }
        _ => "",
    }
}

impl From<ConsoleError> for TargetError {
    fn from(error: ConsoleError) -> Self {
        match error {
            ConsoleError::NoCredentials => Self::MissingSetting {
                variable: TRACEL_API_KEY,
            },
            ConsoleError::NoNamespace => Self::MissingSetting {
                variable: TRACEL_NAMESPACE,
            },
            ConsoleError::NoProject => Self::MissingSetting {
                variable: TRACEL_PROJECT,
            },
            ConsoleError::InvalidSetting {
                variable,
                value,
                expected,
            } => Self::InvalidSetting {
                variable,
                value,
                expected,
            },
            error => Self::Console(error),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use tracel_console::TracelCredentials;

    use super::*;

    fn target(vars: &[(&str, &str)]) -> Result<Target, TargetError> {
        Target::from_vars(|name| {
            vars.iter()
                .find(|(variable, _)| *variable == name)
                .map(|(_, value)| OsString::from(value))
        })
    }

    fn offline_dir(target: Result<Target, TargetError>) -> PathBuf {
        match target {
            Ok(Target::Offline { dir }) => dir,
            other => panic!("expected an offline target, got {other:?}"),
        }
    }

    #[test]
    fn an_unset_or_empty_target_records_offline_under_runs() {
        assert_eq!(offline_dir(target(&[])), Path::new("./runs"));
        assert_eq!(
            offline_dir(target(&[(TRACEL_TARGET, "")])),
            Path::new("./runs")
        );
        assert_eq!(
            offline_dir(target(&[(TRACEL_TARGET, "offline")])),
            Path::new("./runs")
        );
    }

    #[test]
    fn tracel_runs_dir_moves_offline_runs() {
        assert_eq!(
            offline_dir(target(&[(TRACEL_RUNS_DIR, "/data/runs")])),
            Path::new("/data/runs")
        );
        assert_eq!(
            offline_dir(target(&[(TRACEL_RUNS_DIR, "")])),
            Path::new("./runs")
        );
    }

    #[test]
    fn an_unknown_target_names_the_value_and_the_accepted_ones() {
        for value in ["cloud", "Offline", "CONSOLE", " console"] {
            let error = target(&[(TRACEL_TARGET, value)]).unwrap_err();

            assert!(
                matches!(
                    &error,
                    TargetError::UnknownTarget { value: held, .. } if held == value
                ),
                "{value}: {error:?}"
            );
            let message = error.to_string();
            assert!(message.contains("TRACEL_TARGET"), "{message}");
            assert!(message.contains(value), "{message}");
            assert!(message.contains("offline"), "{message}");
            assert!(message.contains("console"), "{message}");
        }
    }

    #[cfg(not(feature = "station"))]
    #[test]
    fn a_station_target_says_it_needs_the_station_feature() {
        let error = target(&[(TRACEL_TARGET, "station")]).unwrap_err();

        assert!(matches!(
            error,
            TargetError::FeatureRequired {
                feature: "station",
                ..
            }
        ));
        assert!(error.to_string().contains("`station` feature"), "{error}");
    }

    #[cfg(feature = "station")]
    #[test]
    fn a_station_target_reads_tracel_station_url() {
        let station_url = |vars: &[(&str, &str)]| match target(vars) {
            Ok(Target::Station { url }) => url.to_string(),
            other => panic!("expected a station target, got {other:?}"),
        };

        assert_eq!(
            station_url(&[(TRACEL_TARGET, "station")]),
            "http://localhost:8000/"
        );
        assert_eq!(
            station_url(&[
                (TRACEL_TARGET, "station"),
                (TRACEL_STATION_URL, "https://station.example.com")
            ]),
            "https://station.example.com/"
        );
    }

    #[cfg(feature = "station")]
    #[test]
    fn an_invalid_station_url_names_the_variable() {
        for value in ["not a url", "localhost:8000", "ftp://localhost:8000"] {
            let error =
                target(&[(TRACEL_TARGET, "station"), (TRACEL_STATION_URL, value)]).unwrap_err();

            assert!(
                matches!(
                    &error,
                    TargetError::InvalidSetting { variable: "TRACEL_STATION_URL", value: held, .. }
                        if held == value
                ),
                "{value}: {error:?}"
            );
            assert!(error.to_string().contains("TRACEL_STATION_URL"), "{error}");
        }
    }

    #[test]
    fn an_offline_target_serves_experiments_and_inference_without_touching_the_disk() {
        let root = tempfile::tempdir().unwrap();
        let dir = root.path().join("runs");
        let offline = Target::Offline { dir: dir.clone() };

        assert!(offline.experiments().is_ok());
        assert!(offline.inference().is_ok());
        assert!(!dir.exists());
    }

    #[test]
    fn an_offline_target_does_not_serve_models_or_datasets() {
        let offline = Target::Offline {
            dir: PathBuf::from("./runs"),
        };

        assert!(matches!(
            offline.models(),
            Err(TargetError::Unsupported {
                service: "models",
                target: "offline"
            })
        ));
        assert!(matches!(
            offline.datasets(),
            Err(TargetError::Unsupported {
                service: "datasets",
                target: "offline"
            })
        ));
    }

    #[test]
    fn a_console_target_builds_its_services_without_a_request() {
        let console = Target::Console {
            env: Env::Development,
            credentials: CredentialSource::Explicit(TracelCredentials::api_key(
                "tcl_key_not_checked_until_the_first_request",
            )),
            project: ProjectRef::new("owner", "project"),
        };

        assert!(console.experiments().is_ok());
        assert!(console.models().is_ok());
        assert!(console.datasets().is_ok());
        assert!(console.inference().is_ok());
    }

    #[test]
    fn a_missing_console_setting_names_the_variable_to_set() {
        for (error, variable, fallback) in [
            (ConsoleError::NoCredentials, TRACEL_API_KEY, "tracel login"),
            (ConsoleError::NoNamespace, TRACEL_NAMESPACE, "tracel.toml"),
            (ConsoleError::NoProject, TRACEL_PROJECT, "tracel.toml"),
        ] {
            let error = TargetError::from(error);

            assert!(
                matches!(error, TargetError::MissingSetting { variable: held } if held == variable),
                "{error:?}"
            );
            assert!(error.to_string().contains(variable), "{error}");
            assert!(error.to_string().contains(fallback), "{error}");
        }
    }

    #[test]
    fn a_missing_namespace_or_project_names_the_keys_of_tracel_toml() {
        for (error, key) in [
            (ConsoleError::NoNamespace, "no `namespace`"),
            (ConsoleError::NoProject, "no `project`"),
        ] {
            let message = TargetError::from(error).to_string();

            assert!(message.contains(key), "{message}");
            assert!(message.contains("`namespace` and `project`"), "{message}");
        }
    }
}