Skip to content

Commit 5bb0b7b

Browse files
branchseerclaude
andcommitted
feat(session): add CacheUpdateStatus to ExecutionEventKind::Finish
Add CacheUpdateStatus enum to track whether cache was updated after task execution and why. This provides visibility into cache behavior in the Finish event. - Add CacheNotUpdatedReason enum with CacheHit, CacheDisabled, NonZeroExitStatus - Add CacheUpdateStatus enum with Updated and NotUpdated variants - Include cache_update_status in ExecutionEventKind::Finish - Update all Finish event emissions to include appropriate status Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 0492491 commit 5bb0b7b

File tree

3 files changed

+79
-40
lines changed

3 files changed

+79
-40
lines changed

crates/vite_task/src/session/event.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ pub enum CacheDisabledReason {
1919
CycleDetected,
2020
}
2121

22+
#[derive(Debug)]
23+
pub enum CacheNotUpdatedReason {
24+
/// Cache was hit - task was replayed from cache, no update needed
25+
CacheHit,
26+
/// Caching was disabled for this task
27+
CacheDisabled,
28+
/// Execution exited with non-zero status
29+
NonZeroExitStatus,
30+
}
31+
32+
#[derive(Debug)]
33+
pub enum CacheUpdateStatus {
34+
/// Cache was successfully updated with new fingerprint and outputs
35+
Updated,
36+
/// Cache was not updated (with reason)
37+
NotUpdated(CacheNotUpdatedReason),
38+
}
39+
2240
#[derive(Debug)]
2341
pub enum CacheStatus {
2442
Disabled(CacheDisabledReason),
@@ -50,5 +68,5 @@ pub enum ExecutionEventKind {
5068
Start { display: Option<ExecutionItemDisplay>, cache_status: CacheStatus },
5169
Output { kind: OutputKind, content: BString },
5270
Error { message: String },
53-
Finish { status: Option<i32> },
71+
Finish { status: Option<i32>, cache_update_status: CacheUpdateStatus },
5472
}

crates/vite_task/src/session/execute/mod.rs

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use self::{
1818
use super::{
1919
cache::{CommandCacheValue, ExecutionCache},
2020
event::{
21-
CacheDisabledReason, CacheStatus, ExecutionEvent, ExecutionEventKind, ExecutionId,
22-
ExecutionItemDisplay, OutputKind,
21+
CacheDisabledReason, CacheNotUpdatedReason, CacheStatus, CacheUpdateStatus, ExecutionEvent,
22+
ExecutionEventKind, ExecutionId, ExecutionItemDisplay, OutputKind,
2323
},
2424
reporter::{ExitStatus, Reporter},
2525
};
@@ -153,10 +153,15 @@ impl ExecutionContext<'_> {
153153
},
154154
});
155155

156-
// Emit Finish WITHOUT cache_status (already in Start event)
156+
// Emit Finish with CacheDisabled status (in-process executions don't cache)
157157
self.event_handler.handle_event(ExecutionEvent {
158158
execution_id,
159-
kind: ExecutionEventKind::Finish { status: Some(0) },
159+
kind: ExecutionEventKind::Finish {
160+
status: Some(0),
161+
cache_update_status: CacheUpdateStatus::NotUpdated(
162+
CacheNotUpdatedReason::CacheDisabled,
163+
),
164+
},
160165
});
161166
}
162167
LeafExecutionKind::Spawn(spawn_execution) => {
@@ -228,10 +233,15 @@ impl ExecutionContext<'_> {
228233
},
229234
});
230235
}
231-
// Emit Finish without cache_status (status already in Start event)
236+
// Emit Finish with CacheHit status (no cache update needed)
232237
self.event_handler.handle_event(ExecutionEvent {
233238
execution_id,
234-
kind: ExecutionEventKind::Finish { status: Some(0) },
239+
kind: ExecutionEventKind::Finish {
240+
status: Some(0),
241+
cache_update_status: CacheUpdateStatus::NotUpdated(
242+
CacheNotUpdatedReason::CacheHit,
243+
),
244+
},
235245
});
236246
return Ok(());
237247
}
@@ -276,51 +286,62 @@ impl ExecutionContext<'_> {
276286
}
277287
};
278288

279-
// 5. Update cache if successful
280-
// Only update cache if: (a) tracking was enabled, and (b) execution succeeded
281-
if let Some((track_result, cache_metadata)) = track_result_with_cache_metadata
282-
&& result.exit_status.success()
289+
// 5. Update cache if successful and determine cache update status
290+
let cache_update_status = if let Some((track_result, cache_metadata)) =
291+
track_result_with_cache_metadata
283292
{
284-
let fingerprint_ignores =
285-
cache_metadata.spawn_fingerprint.fingerprint_ignores().map(|v| v.as_slice());
286-
match PostRunFingerprint::create(
287-
&track_result.path_reads,
288-
&*self.cache_base_path,
289-
fingerprint_ignores,
290-
) {
291-
Ok(post_run_fingerprint) => {
292-
let cache_value = CommandCacheValue {
293-
post_run_fingerprint,
294-
std_outputs: track_result.std_outputs.clone().into(),
295-
duration: result.duration,
296-
};
297-
if let Err(err) = self.cache.update(cache_metadata, cache_value).await {
293+
if result.exit_status.success() {
294+
// Execution succeeded, attempt cache update
295+
let fingerprint_ignores =
296+
cache_metadata.spawn_fingerprint.fingerprint_ignores().map(|v| v.as_slice());
297+
match PostRunFingerprint::create(
298+
&track_result.path_reads,
299+
&*self.cache_base_path,
300+
fingerprint_ignores,
301+
) {
302+
Ok(post_run_fingerprint) => {
303+
let cache_value = CommandCacheValue {
304+
post_run_fingerprint,
305+
std_outputs: track_result.std_outputs.clone().into(),
306+
duration: result.duration,
307+
};
308+
if let Err(err) = self.cache.update(cache_metadata, cache_value).await {
309+
self.event_handler.handle_event(ExecutionEvent {
310+
execution_id,
311+
kind: ExecutionEventKind::Error {
312+
message: format!("Failed to update cache: {err}"),
313+
},
314+
});
315+
return Err(ExecutionAborted);
316+
}
317+
CacheUpdateStatus::Updated
318+
}
319+
Err(err) => {
298320
self.event_handler.handle_event(ExecutionEvent {
299321
execution_id,
300322
kind: ExecutionEventKind::Error {
301-
message: format!("Failed to update cache: {err}"),
323+
message: format!("Failed to create post-run fingerprint: {err}"),
302324
},
303325
});
304326
return Err(ExecutionAborted);
305327
}
306328
}
307-
Err(err) => {
308-
self.event_handler.handle_event(ExecutionEvent {
309-
execution_id,
310-
kind: ExecutionEventKind::Error {
311-
message: format!("Failed to create post-run fingerprint: {err}"),
312-
},
313-
});
314-
return Err(ExecutionAborted);
315-
}
329+
} else {
330+
// Execution failed with non-zero exit status, don't update cache
331+
CacheUpdateStatus::NotUpdated(CacheNotUpdatedReason::NonZeroExitStatus)
316332
}
317-
}
333+
} else {
334+
// Caching was disabled for this task
335+
CacheUpdateStatus::NotUpdated(CacheNotUpdatedReason::CacheDisabled)
336+
};
318337

319-
// 6. Emit finish WITHOUT cache_status
320-
// Cache status was already emitted in Start event
338+
// 6. Emit finish with cache_update_status
321339
self.event_handler.handle_event(ExecutionEvent {
322340
execution_id,
323-
kind: ExecutionEventKind::Finish { status: result.exit_status.code() },
341+
kind: ExecutionEventKind::Finish {
342+
status: result.exit_status.code(),
343+
cache_update_status,
344+
},
324345
});
325346

326347
Ok(())

crates/vite_task/src/session/reporter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl<W: Write> Reporter for LabeledReporter<W> {
489489
ExecutionEventKind::Error { message } => {
490490
self.handle_error(event.execution_id, message);
491491
}
492-
ExecutionEventKind::Finish { status } => {
492+
ExecutionEventKind::Finish { status, cache_update_status: _ } => {
493493
self.handle_finish(event.execution_id, status);
494494
}
495495
}

0 commit comments

Comments
 (0)