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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Recording session management.
//!
//! This module provides [`RecordSession`], which encapsulates the full lifecycle of
//! a recording session: setup, integration with the reporter, and finalization.
//! This allows both `run` and `bench` commands to share recording logic.
use super::{
CompletedRunStats, RecordedRunStatus, RunRecorder, RunStore, ShortestRunIdPrefix, StoreSizes,
StressCompletedRunStats, records_state_dir,
retention::{PruneResult, RecordRetentionPolicy},
};
use crate::{
errors::{RecordPruneError, RecordSetupError, RunStoreError},
record::{Styles, format::RerunInfo},
reporter::{
RunFinishedInfo,
events::{FinalRunStats, RunFinishedStats, StressFinalRunStats},
},
};
use bytesize::ByteSize;
use camino::{Utf8Path, Utf8PathBuf};
use chrono::{DateTime, FixedOffset};
use owo_colors::OwoColorize;
use quick_junit::ReportUuid;
use semver::Version;
use std::{collections::BTreeMap, fmt};
/// Configuration for creating a recording session.
#[derive(Clone, Debug)]
pub struct RecordSessionConfig<'a> {
/// The workspace root path, used to determine the state directory.
pub workspace_root: &'a Utf8Path,
/// The unique identifier for this run.
pub run_id: ReportUuid,
/// The version of nextest creating this recording.
pub nextest_version: Version,
/// When the run started.
pub started_at: DateTime<FixedOffset>,
/// The command-line arguments used to invoke nextest.
pub cli_args: Vec<String>,
/// Build scope arguments (package and target selection).
///
/// These determine which packages and targets are built. In a rerun chain,
/// these are inherited from the original run unless explicitly overridden.
pub build_scope_args: Vec<String>,
/// Environment variables that affect nextest behavior (NEXTEST_* and CARGO_*).
pub env_vars: BTreeMap<String, String>,
/// Maximum size per output file before truncation.
pub max_output_size: ByteSize,
/// Rerun-specific metadata, if this is a rerun.
///
/// If present, this will be written to `meta/rerun-info.json` in the archive.
pub rerun_info: Option<RerunInfo>,
}
/// Result of setting up a recording session.
#[derive(Debug)]
pub struct RecordSessionSetup {
/// The session handle for later finalization.
pub session: RecordSession,
/// The recorder to pass to the structured reporter.
pub recorder: RunRecorder,
/// The shortest unique prefix for the run ID.
///
/// This can be used for display purposes to highlight the unique prefix
/// portion of the run ID.
pub run_id_unique_prefix: ShortestRunIdPrefix,
}
/// Manages the full lifecycle of a recording session.
///
/// This type encapsulates setup, execution integration, and finalization.
#[derive(Debug)]
pub struct RecordSession {
state_dir: Utf8PathBuf,
run_id: ReportUuid,
}
impl RecordSession {
/// Sets up a new recording session.
///
/// Creates the run store, acquires an exclusive lock, and creates the
/// recorder. The lock is released after setup completes (the recorder
/// writes independently).
///
/// Returns a setup result containing the session handle and recorder, or an
/// error if setup fails.
pub fn setup(config: RecordSessionConfig<'_>) -> Result<RecordSessionSetup, RecordSetupError> {
let state_dir =
records_state_dir(config.workspace_root).map_err(RecordSetupError::StateDirNotFound)?;
let store = RunStore::new(&state_dir).map_err(RecordSetupError::StoreCreate)?;
let locked_store = store
.lock_exclusive()
.map_err(RecordSetupError::StoreLock)?;
let (mut recorder, run_id_unique_prefix) = locked_store
.create_run_recorder(
config.run_id,
config.nextest_version,
config.started_at,
config.cli_args,
config.build_scope_args,
config.env_vars,
config.max_output_size,
config.rerun_info.as_ref().map(|info| info.parent_run_id),
)
.map_err(RecordSetupError::RecorderCreate)?;
// If this is a rerun, write the rerun info to the archive.
if let Some(rerun_info) = config.rerun_info {
recorder
.write_rerun_info(&rerun_info)
.map_err(RecordSetupError::RecorderCreate)?;
}
let session = RecordSession {
state_dir,
run_id: config.run_id,
};
Ok(RecordSessionSetup {
session,
recorder,
run_id_unique_prefix,
})
}
/// Returns the run ID for this session.
pub fn run_id(&self) -> ReportUuid {
self.run_id
}
/// Returns the state directory for this session.
pub fn state_dir(&self) -> &Utf8Path {
&self.state_dir
}
/// Finalizes the recording session after the run completes.
///
/// This method marks the run as completed with its final sizes and stats.
///
/// All errors during finalization are non-fatal and returned as warnings,
/// since the recording itself has already completed successfully.
///
/// This should be called after `reporter.finish()` returns the recording sizes.
///
/// The `exit_code` parameter should be the exit code that the process will
/// return. This is stored in the run metadata for later inspection.
pub fn finalize(
self,
recording_sizes: Option<StoreSizes>,
run_finished: Option<RunFinishedInfo>,
exit_code: i32,
policy: &RecordRetentionPolicy,
) -> RecordFinalizeResult {
let mut result = RecordFinalizeResult::default();
// If recording didn't produce sizes, there's nothing to finalize.
let Some(sizes) = recording_sizes else {
return result;
};
// Convert run finished info to status and duration.
let (status, duration_secs) = match run_finished {
Some(info) => (
convert_run_stats_to_status(info.stats, exit_code),
Some(info.elapsed.as_secs_f64()),
),
// This shouldn't happen when recording_sizes is Some, but handle gracefully.
None => (RecordedRunStatus::Incomplete, None),
};
// Re-open the store and acquire the lock.
let store = match RunStore::new(&self.state_dir) {
Ok(store) => store,
Err(err) => {
result
.warnings
.push(RecordFinalizeWarning::StoreOpenFailed(err));
return result;
}
};
let mut locked_store = match store.lock_exclusive() {
Ok(locked) => locked,
Err(err) => {
result
.warnings
.push(RecordFinalizeWarning::StoreLockFailed(err));
return result;
}
};
// Mark the run as completed and persist.
match locked_store.complete_run(self.run_id, sizes, status, duration_secs) {
Ok(true) => {}
Ok(false) => {
// Run was not found in the store, likely pruned during execution.
result
.warnings
.push(RecordFinalizeWarning::RunNotFoundDuringComplete(
self.run_id,
));
}
Err(err) => {
result
.warnings
.push(RecordFinalizeWarning::MetadataPersistFailed(err));
}
}
// Continue with pruning even if metadata persistence failed.
// Prune old runs if needed (once daily or if limits exceeded by 1.5x).
match locked_store.prune_if_needed(policy) {
Ok(Some(mut prune_result)) => {
// Move any errors that occurred during pruning into warnings.
for error in prune_result.errors.drain(..) {
result
.warnings
.push(RecordFinalizeWarning::PruneError(error));
}
result.prune_result = Some(prune_result);
}
Ok(None) => {
// Pruning was skipped; nothing to do.
}
Err(err) => {
result
.warnings
.push(RecordFinalizeWarning::PruneFailed(err));
}
}
result
}
}
/// Converts `RunFinishedStats` to `RecordedRunStatus`.
fn convert_run_stats_to_status(stats: RunFinishedStats, exit_code: i32) -> RecordedRunStatus {
match stats {
RunFinishedStats::Single(run_stats) => {
let completed_stats = CompletedRunStats {
initial_run_count: run_stats.initial_run_count,
passed: run_stats.passed,
failed: run_stats.failed_count(),
exit_code,
};
// Check if the run was cancelled based on final stats.
match run_stats.summarize_final() {
FinalRunStats::Success
| FinalRunStats::NoTestsRun
| FinalRunStats::Failed { .. } => RecordedRunStatus::Completed(completed_stats),
FinalRunStats::Cancelled { .. } => RecordedRunStatus::Cancelled(completed_stats),
}
}
RunFinishedStats::Stress(stress_stats) => {
let stress_completed_stats = StressCompletedRunStats {
initial_iteration_count: stress_stats.completed.total,
success_count: stress_stats.success_count,
failed_count: stress_stats.failed_count,
exit_code,
};
// Check if the stress run was cancelled.
match stress_stats.summarize_final() {
StressFinalRunStats::Success
| StressFinalRunStats::NoTestsRun
| StressFinalRunStats::Failed => {
RecordedRunStatus::StressCompleted(stress_completed_stats)
}
StressFinalRunStats::Cancelled => {
RecordedRunStatus::StressCancelled(stress_completed_stats)
}
}
}
}
}
/// Result of finalizing a recording session.
#[derive(Debug, Default)]
pub struct RecordFinalizeResult {
/// Warnings encountered during finalization.
pub warnings: Vec<RecordFinalizeWarning>,
/// The prune result, if pruning was performed.
pub prune_result: Option<PruneResult>,
}
impl RecordFinalizeResult {
/// Logs warnings and pruning statistics from the finalization result.
pub fn log(&self, styles: &Styles) {
for warning in &self.warnings {
tracing::warn!("{warning}");
}
if let Some(prune_result) = &self.prune_result
&& (prune_result.deleted_count > 0 || prune_result.orphans_deleted > 0)
{
tracing::info!(
"{}(hint: {} to replay runs)",
prune_result.display(styles),
"cargo nextest replay".style(styles.count),
);
}
}
}
/// Non-fatal warning during recording finalization.
#[derive(Debug)]
pub enum RecordFinalizeWarning {
/// Recording completed but the run store couldn't be opened.
StoreOpenFailed(RunStoreError),
/// Recording completed but the run store couldn't be locked.
StoreLockFailed(RunStoreError),
/// Recording completed but run metadata couldn't be persisted.
MetadataPersistFailed(RunStoreError),
/// Recording completed but the run was not found in the store.
///
/// This can happen if an aggressive prune deleted the run while the test
/// was still executing.
RunNotFoundDuringComplete(ReportUuid),
/// Error during pruning (overall prune operation failed).
PruneFailed(RunStoreError),
/// Error during pruning (individual run or orphan deletion failed).
PruneError(RecordPruneError),
}
impl fmt::Display for RecordFinalizeWarning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::StoreOpenFailed(err) => {
write!(f, "recording completed but failed to open run store: {err}")
}
Self::StoreLockFailed(err) => {
write!(f, "recording completed but failed to lock run store: {err}")
}
Self::MetadataPersistFailed(err) => {
write!(
f,
"recording completed but failed to persist run metadata: {err}"
)
}
Self::RunNotFoundDuringComplete(run_id) => {
write!(
f,
"recording completed but run {run_id} was not found in store \
(may have been pruned during execution)"
)
}
Self::PruneFailed(err) => write!(f, "error during prune: {err}"),
Self::PruneError(msg) => write!(f, "error during prune: {msg}"),
}
}
}