1use std::collections::BTreeSet;
4use std::ffi::OsStr;
5use std::path::{Path, PathBuf};
6use std::time::{Duration, Instant};
7
8use anyhow::{Context, Result, bail, ensure};
9
10use crate::hel_session_manager::{
11 ManagedSessionHandle, ManagedSessionLease, SessionManagerControl, StandaloneSession,
12 new_command_id, worker_connect_needs_restart,
13};
14use crate::hel_worker_client::RelayRejected;
15use hel::hel_archive::{
16 BundleManifest, CanonicalSessionSnapshot, SessionManifest, TargetManifest,
17 verify_archive_streaming,
18};
19use hel::hel_checkpoint::{
20 CHECKPOINT_EXPORT_PROTOCOL_VERSION, CHECKPOINT_STAGING_PROTOCOL_VERSION, CapturedCheckpoint,
21 CheckpointCaptureSpec, CheckpointExportSpec, CheckpointPackSpec, CheckpointRepositoryCapture,
22 CheckpointRepositorySpec, CheckpointTransfer, canonical_session_contains_prompt,
23 capture_stdin_command, checkpoint_sha256, export_command, export_stdin_command,
24 pack_stdin_command,
25};
26use hel::hel_config::{HarnessKind, sessions_dir};
27use hel::hel_projection::canonical_session_from_materialized;
28use hel::hel_state::{
29 CheckpointMetadata, HelState, ManagedSessionSnapshot, SessionRecord, SessionState,
30};
31use hel::hel_targets::{
32 self, CommandExecutor, CommandOutput, CommandSpec, ProcessExecutor, ProvisionStage,
33 ProvisionStageGuard,
34};
35use hel::hel_worker::{RelayCommand, RelayCursor, RelayExecutionState};
36
37use super::backend::backend_locator;
38use super::readiness::wait_for_native_session_in_stage;
39use super::worker_restart::{InstalledWorkerRestart, RESTART_FOR_CHECKPOINT};
40use super::{
41 Controller, execute_checked, now, persist_session_record_transition_or_restore,
42 scp_command_spec, ssh_command_spec, target_kind, target_profile_home,
43};
44
45const CHECKPOINT_BARRIER_TIMEOUT: Duration = Duration::from_secs(30);
50const CHECKPOINT_CANCEL_TIMEOUT: Duration = Duration::from_secs(30);
54const CHECKPOINT_BARRIER_TIMEOUT_AFTER_RESTART: Duration = Duration::from_secs(300);
57
58pub fn reconcile_managed_checkpoint_archives() -> Result<usize> {
62 let mut state = HelState::load()?;
63 for operation in hel::hel_database::load_move_operations()? {
66 if operation.retains_checkpoint()
67 && let Some(checkpoint) = operation.checkpoint
68 && let Some(mut session) = state.sessions.get(&operation.selection.session_id).cloned()
69 {
70 session.checkpoint = Some(checkpoint);
71 state
72 .sessions
73 .insert(format!("move:{}", operation.operation_id), session);
74 }
75 }
76 reconcile_managed_checkpoint_archives_in(&sessions_dir(), &state)
77}
78
79fn reconcile_managed_checkpoint_archives_in(directory: &Path, state: &HelState) -> Result<usize> {
80 if !directory.exists() {
81 return Ok(0);
82 }
83 let referenced_names = state
84 .sessions
85 .values()
86 .filter_map(|session| session.checkpoint.as_ref())
87 .filter_map(|checkpoint| checkpoint.archive_path.file_name())
88 .map(ToOwned::to_owned)
89 .collect::<BTreeSet<_>>();
90 let mut removed = 0;
91 for entry in std::fs::read_dir(directory)
92 .with_context(|| format!("scan checkpoint directory {}", directory.display()))?
93 {
94 let entry = entry?;
95 let file_type = entry.file_type()?;
96 if !file_type.is_file()
97 || !is_managed_checkpoint_archive_name(&entry.file_name())
98 || referenced_names.contains(&entry.file_name())
99 {
100 continue;
101 }
102 std::fs::remove_file(entry.path()).with_context(|| {
103 format!(
104 "remove unreferenced managed checkpoint {}",
105 entry.path().display()
106 )
107 })?;
108 removed += 1;
109 }
110 Ok(removed)
111}
112
113fn is_managed_checkpoint_archive_name(name: &OsStr) -> bool {
114 let Some(stem) = name.to_str().and_then(|name| name.strip_suffix(".hel.zip")) else {
115 return false;
116 };
117 let Some((frontier_prefix, nonce)) = stem.rsplit_once("-archive-") else {
118 return false;
119 };
120 if nonce.len() != 32
121 || !nonce
122 .bytes()
123 .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
124 {
125 return false;
126 }
127 let Some((session_id, frontier)) = frontier_prefix.rsplit_once('-') else {
128 return false;
129 };
130 !session_id.is_empty()
131 && frontier.parse::<u64>().is_ok()
132 && hel::hel_config::validate_id("session", session_id).is_ok()
133}
134
135#[derive(Debug, Clone)]
136pub struct CheckpointArtifact {
137 pub metadata: CheckpointMetadata,
138 pub native_session_id: String,
139 pub event_frontier_digest: String,
141}
142
143pub(super) enum ControllerRelayLease {
151 Managed {
152 handle: ManagedSessionHandle,
153 lease: Option<ManagedSessionLease>,
154 },
155 Standalone(StandaloneSession),
156}
157
158impl ControllerRelayLease {
159 pub(super) fn connection_mut(&mut self) -> &mut StandaloneSession {
162 match self {
163 Self::Managed { lease, .. } => lease
164 .as_mut()
165 .expect("checkpoint latch has already returned its connection")
166 .connection_mut(),
167 Self::Standalone(connection) => connection,
168 }
169 }
170
171 async fn submit(&mut self, command_id: String, command: RelayCommand) -> Result<u64> {
172 match self {
173 Self::Managed {
174 lease: Some(lease), ..
175 } => lease.connection_mut().submit(command_id, command).await,
176 Self::Managed { handle, .. } => handle.submit(command_id, command).await,
177 Self::Standalone(connection) => connection.submit(command_id, command).await,
178 }
179 }
180
181 async fn sync_snapshot(&mut self) -> Result<ManagedSessionSnapshot> {
182 match self {
183 Self::Managed {
184 lease: Some(lease), ..
185 } => lease.connection_mut().sync().await,
186 Self::Managed { handle, .. } => {
187 handle.sync_now().await?;
188 handle
189 .view()
190 .snapshot
191 .context("managed session has no snapshot")
192 }
193 Self::Standalone(connection) => connection.sync().await,
194 }
195 }
196
197 fn replace_connection(&mut self, connection: StandaloneSession) {
199 match self {
200 Self::Managed {
201 lease: Some(lease), ..
202 } => lease.replace_connection(connection),
203 Self::Standalone(existing) => *existing = connection,
204 Self::Managed { lease: None, .. } => {
205 *self = Self::Standalone(connection);
206 }
207 }
208 }
209
210 fn end_latch(&mut self) {
214 if let Self::Managed { lease, .. } = self
215 && let Some(lease) = lease.take()
216 {
217 lease.release();
218 }
219 }
220
221 async fn cancel_abandoned_barrier(&mut self) -> Result<()> {
229 let Self::Managed { handle, lease } = self else {
230 return Ok(());
233 };
234 match lease.take() {
235 Some(lease) => drop(lease),
236 None => drop(handle.lease_connection().await?),
237 }
238 Ok(())
239 }
240
241 pub(super) fn release(self) {
242 if let Self::Managed {
243 lease: Some(lease), ..
244 } = self
245 {
246 lease.release();
247 }
248 }
249}
250
251#[derive(Debug, Clone, Copy, PartialEq, Eq)]
253pub(super) enum LatchExclusivity {
254 ReleaseAfterLatch,
259 HoldThroughClose,
262}
263
264#[derive(Debug, Clone, Copy, PartialEq, Eq)]
266pub(super) enum CheckpointExportPolicy {
267 Always,
269 ReuseUnchangedArchive,
273}
274
275#[derive(Debug, Clone, Copy, PartialEq, Eq)]
277pub(super) enum CheckpointCompletion {
278 HeldBarrier,
282 ReleasedAfterCapture,
285}
286
287pub(super) struct LatchedCheckpoint {
288 pub(super) artifact: CheckpointArtifact,
289 pub(super) relay: ControllerRelayLease,
290 pub(super) barrier_command_id: String,
291 pub(super) cursor: RelayCursor,
292 pub(super) completion: CheckpointCompletion,
293}
294
295impl LatchedCheckpoint {
302 async fn complete(mut self) -> Result<()> {
304 let (prefix, command) = match self.completion {
305 CheckpointCompletion::HeldBarrier => (
306 "checkpoint-complete",
307 RelayCommand::CompleteCheckpoint {
308 barrier_command_id: self.barrier_command_id.clone(),
309 },
310 ),
311 CheckpointCompletion::ReleasedAfterCapture => (
314 "checkpoint-floor",
315 RelayCommand::AdvanceRecoveryFloor {
316 through: self.cursor.clone(),
317 },
318 ),
319 };
320 let command_id = new_command_id(prefix)?;
321 self.relay.submit(command_id, command).await.map(|_| ())
322 }
323
324 async fn abandon(mut self, session_id: &str) {
330 if self.completion == CheckpointCompletion::ReleasedAfterCapture {
331 return;
335 }
336 if let Err(error) = self.relay.cancel_abandoned_barrier().await {
337 tracing::warn!(
338 session_id,
339 "abandoned checkpoint could not cancel its relay barrier: {error:#}"
340 );
341 }
342 }
343}
344
345impl Controller {
346 pub(super) fn persist_checkpoint_transition_or_restore(
347 &mut self,
348 session_id: &str,
349 previous: &SessionRecord,
350 context: &'static str,
351 ) -> Result<()> {
352 persist_session_record_transition_or_restore(
353 &mut self.state,
354 session_id,
355 previous,
356 context,
357 &hel::hel_database::save_checkpointed_session,
358 )
359 }
360
361 pub(super) fn persist_failed_checkpoint_state_or_restore(
362 &mut self,
363 session_id: &str,
364 previous: &SessionRecord,
365 primary: anyhow::Error,
366 ) -> anyhow::Error {
367 match self.persist_session_state(session_id) {
368 Ok(()) => primary,
369 Err(error) => self.restore_prior_session_after_persistence_failure(
370 session_id,
371 previous,
372 primary.context(format!(
373 "failed to persist the checkpoint rollback state: {error:#}"
374 )),
375 ),
376 }
377 }
378
379 pub async fn checkpoint_session(&mut self, session_id: &str) -> Result<CheckpointMetadata> {
383 self.checkpoint_session_controlled(session_id, &ProcessExecutor)
384 .await
385 }
386
387 pub async fn checkpoint_session_controlled(
388 &mut self,
389 session_id: &str,
390 executor: &(impl CommandExecutor + Sync),
391 ) -> Result<CheckpointMetadata> {
392 self.checkpoint_session_controlled_with_manager(session_id, executor, None)
393 .await
394 }
395
396 async fn checkpoint_session_controlled_with_manager(
397 &mut self,
398 session_id: &str,
399 executor: &(impl CommandExecutor + Sync),
400 manager: Option<&SessionManagerControl>,
401 ) -> Result<CheckpointMetadata> {
402 let previous = self
403 .state
404 .sessions
405 .get(session_id)
406 .with_context(|| format!("unknown session {session_id}"))?
407 .clone();
408 ensure!(
409 !matches!(
410 previous.state,
411 SessionState::Closing | SessionState::Destroying
412 ),
413 "session {session_id} is already closing; resume that close instead of starting an ordinary checkpoint"
414 );
415 let record = self.state.sessions.get_mut(session_id).unwrap();
416 record.state = SessionState::Checkpointing;
417 record.updated_at = now();
418 record.last_checkpoint_error = None;
419 self.persist_session_transition_or_restore(
420 session_id,
421 &previous,
422 "persist checkpointing state before creating a checkpoint",
423 )?;
424
425 match self
426 .checkpoint_session_latched(
427 session_id,
428 executor,
429 manager,
430 LatchExclusivity::ReleaseAfterLatch,
431 CheckpointExportPolicy::Always,
432 )
433 .await
434 {
435 Ok(latched) => {
436 let artifact = latched.artifact.clone();
437 if let Err(error) = hel::hel_test_hooks::reach_test_hook(
438 "checkpoint_archive_before_database_publication",
439 ) {
440 latched.abandon(session_id).await;
441 return Err(remove_uninstalled_checkpoint(
442 &artifact.metadata.archive_path,
443 error,
444 ));
445 }
446 {
447 let record = self.state.sessions.get_mut(session_id).unwrap();
448 record.state = SessionState::Running;
449 record.native_session_id = Some(artifact.native_session_id.clone());
450 record.checkpoint = Some(artifact.metadata.clone());
451 record.updated_at = now();
452 record.last_error = None;
453 record.last_checkpoint_error = None;
454 }
455 let persist_started = Instant::now();
456 if let Err(error) = self.persist_checkpoint_transition_or_restore(
457 session_id,
458 &previous,
459 "persist verified checkpoint before releasing relay history",
460 ) {
461 latched.abandon(session_id).await;
462 return Err(error);
463 }
464 tracing::info!(
465 session_id,
466 persist_ms = persist_started.elapsed().as_millis() as u64,
467 "checkpoint metadata persisted"
468 );
469 prune_replaced_checkpoint(previous.checkpoint.as_ref(), &artifact.metadata);
470 release_projection_behind_checkpoint(session_id, &artifact.metadata);
471 if let Err(error) = latched.complete().await {
472 tracing::warn!(
478 session_id,
479 "verified checkpoint was saved, but the relay could not be told to release the history it covers: {error:#}"
480 );
481 }
482 Ok(artifact.metadata)
483 }
484 Err(error) => {
485 let deferred = checkpoint_was_deferred(&error);
490 if let Some(record) = self.state.sessions.get_mut(session_id) {
491 record.state = if previous.state == SessionState::Checkpointing {
492 SessionState::Running
493 } else {
494 previous.state
495 };
496 record.updated_at = now();
497 if !deferred {
498 record.last_checkpoint_error = Some(format!("{error:#}"));
499 }
500 }
501 Err(self.persist_failed_checkpoint_state_or_restore(session_id, &previous, error))
502 }
503 }
504 }
505
506 pub async fn create_recovery_checkpoint_managed_controlled(
509 &self,
510 session_id: &str,
511 manager: &SessionManagerControl,
512 executor: &(impl CommandExecutor + Sync),
513 ) -> Result<CheckpointArtifact> {
514 self.create_recovery_checkpoint_with_manager(session_id, Some(manager), executor)
515 .await
516 }
517
518 async fn create_recovery_checkpoint_with_manager(
519 &self,
520 session_id: &str,
521 manager: Option<&SessionManagerControl>,
522 executor: &(impl CommandExecutor + Sync),
523 ) -> Result<CheckpointArtifact> {
524 let previous_checkpoint = self
525 .state
526 .sessions
527 .get(session_id)
528 .with_context(|| format!("unknown session {session_id}"))?
529 .checkpoint
530 .clone();
531 let latched = self
532 .checkpoint_session_latched_with_recovery_stage(
533 session_id,
534 executor,
535 manager,
536 LatchExclusivity::ReleaseAfterLatch,
537 CheckpointExportPolicy::Always,
538 true,
539 )
540 .await?;
541 let artifact = latched.artifact.clone();
542 let verification = {
543 let _verifying = ProvisionStageGuard::new(executor, ProvisionStage::Verifying);
544 verify_checkpoint_artifact(session_id, &artifact)
545 };
546 if let Err(error) = verification {
547 latched.abandon(session_id).await;
548 return Err(remove_uninstalled_checkpoint(
549 &artifact.metadata.archive_path,
550 error.context("final recovery checkpoint verification"),
551 ));
552 }
553 if let Err(error) =
554 hel::hel_test_hooks::reach_test_hook("checkpoint_archive_before_database_publication")
555 {
556 latched.abandon(session_id).await;
557 return Err(remove_uninstalled_checkpoint(
558 &artifact.metadata.archive_path,
559 error,
560 ));
561 }
562 let persist_started = Instant::now();
563 if let Err(error) = hel::hel_database::record_recovery_success(
564 session_id,
565 &artifact.native_session_id,
566 &artifact.metadata,
567 ) {
568 latched.abandon(session_id).await;
569 return Err(error
570 .context("persist verified recovery checkpoint before releasing relay history"));
571 }
572 tracing::info!(
573 session_id,
574 persist_ms = persist_started.elapsed().as_millis() as u64,
575 "recovery checkpoint metadata persisted"
576 );
577 if let Err(error) = latched.complete().await {
578 tracing::warn!(
583 session_id,
584 "recovery checkpoint was saved, but the relay could not be told to release the history it covers: {error:#}"
585 );
586 }
587 prune_replaced_checkpoint(previous_checkpoint.as_ref(), &artifact.metadata);
588 release_projection_behind_checkpoint(session_id, &artifact.metadata);
589 Ok(artifact)
590 }
591
592 pub(super) async fn checkpoint_session_latched(
593 &self,
594 session_id: &str,
595 executor: &(impl CommandExecutor + Sync),
596 manager: Option<&SessionManagerControl>,
597 exclusivity: LatchExclusivity,
598 export_policy: CheckpointExportPolicy,
599 ) -> Result<LatchedCheckpoint> {
600 self.checkpoint_session_latched_with_recovery_stage(
601 session_id,
602 executor,
603 manager,
604 exclusivity,
605 export_policy,
606 exclusivity == LatchExclusivity::HoldThroughClose,
607 )
608 .await
609 }
610
611 async fn checkpoint_session_latched_with_recovery_stage(
612 &self,
613 session_id: &str,
614 executor: &(impl CommandExecutor + Sync),
615 manager: Option<&SessionManagerControl>,
616 exclusivity: LatchExclusivity,
617 export_policy: CheckpointExportPolicy,
618 recovery_copy: bool,
619 ) -> Result<LatchedCheckpoint> {
620 if let Some(operation) = hel::hel_database::load_move_operation(session_id)?
621 && operation.queue_admission_started
622 && !operation.queue_admission_finished
623 {
624 bail!(
627 "move queue admission is incomplete; retry Move before checkpointing this destination"
628 );
629 }
630 let session = self
631 .state
632 .sessions
633 .get(session_id)
634 .with_context(|| format!("unknown session {session_id}"))?
635 .clone();
636 let locator = session
637 .target
638 .as_ref()
639 .context("session has no live target")?;
640 let backend = backend_locator(locator, &session, &self.config)?;
641 let profile = self
642 .config
643 .profiles
644 .get(&session.last_profile)
645 .context("session profile is missing")?;
646 let bundle = session
647 .project_directory
648 .is_none()
649 .then(|| self.config.bundles.get(&session.bundle_id))
650 .flatten();
651 let reconnect = hel_targets::reconnect_plan(&backend, session_id)?
652 .commands
653 .into_iter()
654 .next()
655 .context("reconnect plan is empty")?;
656 let worker_root = hel_targets::worker_root(&backend, session_id)?;
657 let harness_home = target_profile_home(&backend, session_id, profile);
658 let (workspace_root, primary_repository, repositories) =
659 if let Some(project_directory) = &session.project_directory {
660 let parent = project_directory
661 .parent()
662 .context("bare project directory has no parent")?;
663 let destination = project_directory
664 .file_name()
665 .context("bare project directory cannot be the filesystem root")?;
666 (
667 parent.to_string_lossy().into_owned(),
668 "project".to_owned(),
669 vec![CheckpointRepositorySpec {
670 id: "project".into(),
671 relative_destination: PathBuf::from(destination),
672 capture: if session.managed_worktree.is_some() {
678 CheckpointRepositoryCapture::DeltaFrom {
679 base_commit: super::worktree::raw_checkout_position(
680 &session,
681 &self.config,
682 project_directory,
683 executor,
684 )?
685 .head_commit,
686 }
687 } else {
688 CheckpointRepositoryCapture::MetadataOnly
689 },
690 origin_override: None,
691 }],
692 )
693 } else {
694 let bundle = bundle.context("session bundle is missing")?;
695 let workspace_root = match &backend {
696 hel_targets::TargetLocator::LocalPodman { .. }
697 | hel_targets::TargetLocator::LocalDocker { .. }
698 | hel_targets::TargetLocator::AppleContainer { .. }
699 | hel_targets::TargetLocator::SshPodman { .. }
700 | hel_targets::TargetLocator::SshDocker { .. } => "/workspace".to_string(),
701 hel_targets::TargetLocator::AwsEc2 { workspace, .. }
702 | hel_targets::TargetLocator::SshBare { workspace, .. } => workspace.clone(),
703 hel_targets::TargetLocator::LocalBare { worker_root } => worker_root.clone(),
704 };
705 let repositories = bundle
706 .repositories
707 .iter()
708 .map(|repository| CheckpointRepositorySpec {
709 id: repository.id.clone(),
710 relative_destination: repository.destination.clone(),
711 capture: CheckpointRepositoryCapture::RemoteWorkspace,
712 origin_override: None,
713 })
714 .collect();
715 (workspace_root, bundle.primary_repo.clone(), repositories)
716 };
717 let target_path = |path: &str| match &backend {
718 hel_targets::TargetLocator::AwsEc2 { .. }
719 | hel_targets::TargetLocator::SshBare { .. }
720 if !path.starts_with('/') =>
721 {
722 PathBuf::from(format!("~/{path}"))
723 }
724 _ => PathBuf::from(path),
725 };
726 let remote_spec = format!("{worker_root}/checkpoint-spec.json");
727 let remote_archive = format!("{worker_root}/checkpoint.hel.zip");
728 let remote_stage = format!(
729 "{worker_root}/checkpoint-stage-{}",
730 new_command_id("capture")?
731 );
732 let checkpointed_at = now();
733 let target_manifest = TargetManifest {
734 template_id: session.target_template_id.clone(),
735 target_kind: target_kind(&backend).into(),
736 details: Default::default(),
737 };
738 let bundle_manifest = BundleManifest {
739 id: session.bundle_id.clone(),
740 primary_repository,
741 };
742 let session_manifest = |native_session_id: &str| SessionManifest {
743 id: session.id.clone(),
744 title: session.title.clone(),
745 harness_kind: session.harness_kind,
746 profile_id: session.last_profile.clone(),
747 native_session_id: native_session_id.to_owned(),
748 created_at: session.created_at.clone(),
749 checkpointed_at: checkpointed_at.clone(),
750 hel_version: env!("CARGO_PKG_VERSION").into(),
751 relay_version: env!("CARGO_PKG_VERSION").into(),
752 adapter_version: "acp-v1".into(),
753 };
754 let releases_after_capture = exclusivity == LatchExclusivity::ReleaseAfterLatch;
755 if releases_after_capture
756 && let Some(native_session_id) = session.native_session_id.as_deref()
757 {
758 let prestage = CheckpointCaptureSpec {
759 protocol_version: CHECKPOINT_STAGING_PROTOCOL_VERSION,
760 session: session_manifest(native_session_id),
761 target: target_manifest.clone(),
762 bundle: bundle_manifest.clone(),
763 relay_root: target_path(&worker_root),
764 harness_home: target_path(&harness_home),
765 workspace_root: target_path(&workspace_root),
766 repositories: repositories.clone(),
767 allow_empty_native: false,
768 stage_path: target_path(&remote_stage),
769 refresh_existing: false,
770 };
771 let prestage_started = Instant::now();
772 let prestaged = {
773 let _recovery_copy = recovery_copy
774 .then(|| ProvisionStageGuard::new(executor, ProvisionStage::RecoveryCopy));
775 run_checkpoint_staging_command(
776 executor,
777 &backend,
778 session_id,
779 &prestage,
780 capture_stdin_command,
781 "prestage target checkpoint",
782 )
783 };
784 match prestaged {
785 Ok(output) => match serde_json::from_slice::<CapturedCheckpoint>(&output.stdout) {
786 Ok(captured) => tracing::info!(
787 session_id,
788 prestage_ms = prestage_started.elapsed().as_millis() as u64,
789 native_bytes = captured.native_bytes,
790 repository_bytes = captured.repository_bytes,
791 reused_native = captured.reused_native,
792 "checkpoint target state prestaged while ACP dispatch remained active"
793 ),
794 Err(error) => tracing::warn!(
795 session_id,
796 error = format!("{error:#}"),
797 "checkpoint prestage returned an invalid result; barrier capture will replace it"
798 ),
799 },
800 Err(error) => {
801 if executor.cancellation_requested() {
802 return Err(error.context("checkpoint prestage was cancelled"));
803 }
804 tracing::warn!(
805 session_id,
806 error = format!("{error:#}"),
807 "checkpoint prestage failed; barrier capture will collect a fresh generation"
808 );
809 }
810 }
811 }
812 let (mut relay, mut restarted_worker) = self
813 .open_checkpoint_relay(
814 session_id,
815 executor,
816 manager,
817 InstalledWorkerRestart {
818 backend: &backend,
819 worker_root: &worker_root,
820 reconnect: &reconnect,
821 launch: None,
822 messages: &RESTART_FOR_CHECKPOINT,
823 },
824 exclusivity == LatchExclusivity::HoldThroughClose
825 || session.harness_kind != HarnessKind::Kimi,
826 )
827 .await?;
828 let (barrier, barrier_command_id) = loop {
829 wait_for_native_session_in_stage(
833 relay.connection_mut(),
834 executor,
835 hel_targets::ProvisionStage::Starting,
836 )
837 .await?;
838 if exclusivity == LatchExclusivity::ReleaseAfterLatch {
839 let snapshot = relay.connection_mut().sync().await?;
840 if snapshot.operational.execution == RelayExecutionState::Running {
841 relay.release();
844 return Err(CheckpointDeferred::harness_busy().into());
845 }
846 if !snapshot
847 .operational
848 .safe_for_checkpoint(session.harness_kind)
849 {
850 relay.release();
855 return Err(CheckpointDeferred::background_work().into());
856 }
857 }
858 let barrier_command_id = new_command_id("checkpoint")?;
859 let timeout = if restarted_worker {
860 CHECKPOINT_BARRIER_TIMEOUT_AFTER_RESTART
861 } else {
862 CHECKPOINT_BARRIER_TIMEOUT
863 };
864 let result = {
865 let connection = relay.connection_mut();
866 connection
867 .submit(
868 barrier_command_id.clone(),
869 RelayCommand::BeginCheckpoint {
870 reason: Some("controller archive checkpoint".into()),
871 },
872 )
873 .await?;
874 wait_for_checkpoint_barrier(
875 connection,
876 session_id,
877 &barrier_command_id,
878 timeout,
879 BarrierBusyPolicy::of(exclusivity),
880 session.harness_kind,
881 )
882 .await
883 };
884 match result {
885 Ok(barrier) => break (barrier, barrier_command_id),
886 Err(error)
887 if !restarted_worker && checkpoint_barrier_needs_worker_restart(&error) =>
888 {
889 if exclusivity == LatchExclusivity::ReleaseAfterLatch
890 && session.harness_kind == HarnessKind::Kimi
891 {
892 let safe_to_restart =
893 relay.connection_mut().sync().await.is_ok_and(|snapshot| {
894 snapshot.operational.safe_to_replace(HarnessKind::Kimi)
895 });
896 if !safe_to_restart {
897 return Err(error.context(CheckpointDeferred::background_work()));
898 }
899 }
900 tracing::warn!(
901 session_id,
902 "checkpoint requires a worker restart; restarting and retrying: {error:#}"
903 );
904 let connection = self
905 .restart_worker_for_checkpoint(
906 session_id,
907 executor,
908 &backend,
909 &worker_root,
910 &reconnect,
911 )
912 .await?;
913 relay.replace_connection(connection);
914 restarted_worker = true;
915 }
916 Err(error) => return Err(error),
917 }
918 };
919 let barrier_ready_at = Instant::now();
920 relay
924 .connection_mut()
925 .sync_project_memory()
926 .await
927 .context("synchronize project memory for checkpoint")?;
928 let cursor = barrier
929 .operational
930 .checkpoint_ready
931 .clone()
932 .context("relay reported a checkpoint barrier without its ready cursor")?;
933 let materialized = barrier.materialized;
934 let expected_ordinal = materialized.applied_event_ordinal;
935 let expected_digest = materialized.applied_event_digest.clone();
936 ensure!(
937 expected_ordinal == barrier.operational.latest_ordinal,
938 "checkpoint projection frontier {expected_ordinal} does not match relay frontier {}",
939 barrier.operational.latest_ordinal
940 );
941 ensure!(
942 expected_digest == barrier.operational.latest_digest,
943 "checkpoint projection digest does not match the relay frontier digest"
944 );
945 ensure_exact_checkpoint_cut(&cursor, expected_ordinal, &expected_digest)?;
946 let canonical_session = canonical_session_from_materialized(&materialized)?;
947 let native_session_id = barrier
948 .operational
949 .native_session_id
950 .or_else(|| session.native_session_id.clone())
951 .context("harness did not report its native session ID")?;
952
953 if exclusivity == LatchExclusivity::ReleaseAfterLatch {
958 relay.end_latch();
959 }
960
961 if export_policy == CheckpointExportPolicy::ReuseUnchangedArchive
967 && session.managed_worktree.is_none()
971 && let Some(artifact) = reusable_installed_checkpoint(
972 session_id,
973 session.checkpoint.as_ref(),
974 &native_session_id,
975 cursor.ordinal,
976 &canonical_session,
977 )
978 {
979 return Ok(LatchedCheckpoint {
980 artifact,
981 relay,
982 barrier_command_id,
983 cursor,
984 completion: CheckpointCompletion::HeldBarrier,
985 });
986 }
987
988 let mut completion = CheckpointCompletion::HeldBarrier;
993
994 let exported: Result<CheckpointArtifact> = async {
995 let spec = CheckpointExportSpec {
996 protocol_version: CHECKPOINT_EXPORT_PROTOCOL_VERSION,
997 session: session_manifest(&native_session_id),
998 target: target_manifest,
999 bundle: bundle_manifest,
1000 relay_root: target_path(&worker_root),
1001 harness_home: target_path(&harness_home),
1002 workspace_root: target_path(&workspace_root),
1003 repositories,
1004 canonical_session,
1005 output_path: target_path(&remote_archive),
1006 };
1007 let mut export_ms: Option<u64> = None;
1010 let exported = if releases_after_capture {
1011 let capture_spec = CheckpointCaptureSpec {
1012 protocol_version: CHECKPOINT_STAGING_PROTOCOL_VERSION,
1013 session: spec.session.clone(),
1014 target: spec.target.clone(),
1015 bundle: spec.bundle.clone(),
1016 relay_root: spec.relay_root.clone(),
1017 harness_home: spec.harness_home.clone(),
1018 workspace_root: spec.workspace_root.clone(),
1019 repositories: spec.repositories.clone(),
1020 allow_empty_native: !canonical_session_contains_prompt(&spec.canonical_session),
1021 stage_path: target_path(&remote_stage),
1022 refresh_existing: true,
1023 };
1024 let capture_started = Instant::now();
1025 let captured = {
1026 let _recovery_copy = recovery_copy.then(|| {
1027 ProvisionStageGuard::new(executor, ProvisionStage::RecoveryCopy)
1028 });
1029 run_checkpoint_staging_command(
1030 executor,
1031 &backend,
1032 session_id,
1033 &capture_spec,
1034 capture_stdin_command,
1035 "capture target checkpoint",
1036 )?
1037 };
1038 let captured: CapturedCheckpoint = serde_json::from_slice(&captured.stdout)
1039 .context("decode captured checkpoint result")?;
1040 tracing::info!(
1041 session_id,
1042 capture_ms = capture_started.elapsed().as_millis() as u64,
1043 barrier_held_ms = barrier_ready_at.elapsed().as_millis() as u64,
1044 native_bytes = captured.native_bytes,
1045 repository_bytes = captured.repository_bytes,
1046 reused_native = captured.reused_native,
1047 "checkpoint target state captured; releasing ACP dispatch"
1048 );
1049 completion = release_checkpoint_after_capture(
1050 &mut relay,
1051 session_id,
1052 &barrier_command_id,
1053 &cursor,
1054 session.harness_kind,
1055 )
1056 .await?;
1057 let pack_spec = CheckpointPackSpec {
1058 protocol_version: CHECKPOINT_STAGING_PROTOCOL_VERSION,
1059 relay_root: spec.relay_root.clone(),
1060 stage_path: target_path(&remote_stage),
1061 canonical_session: spec.canonical_session.clone(),
1062 output_path: spec.output_path.clone(),
1063 };
1064 let pack_started = Instant::now();
1065 let output = {
1066 let _recovery_copy = recovery_copy.then(|| {
1067 ProvisionStageGuard::new(executor, ProvisionStage::RecoveryCopy)
1068 });
1069 run_checkpoint_staging_command(
1070 executor,
1071 &backend,
1072 session_id,
1073 &pack_spec,
1074 pack_stdin_command,
1075 "pack target checkpoint",
1076 )?
1077 };
1078 tracing::info!(
1079 session_id,
1080 pack_ms = pack_started.elapsed().as_millis() as u64,
1081 "checkpoint archive packaged after ACP dispatch resumed"
1082 );
1083 output
1084 } else {
1085 let export_started = Instant::now();
1086 let output = {
1087 let _recovery_copy = recovery_copy.then(|| {
1088 ProvisionStageGuard::new(executor, ProvisionStage::RecoveryCopy)
1089 });
1090 export_target_checkpoint(
1091 executor,
1092 &backend,
1093 session_id,
1094 &spec,
1095 &remote_spec,
1096 )?
1097 };
1098 export_ms = Some(export_started.elapsed().as_millis() as u64);
1099 output
1100 };
1101 let target_checkpoint: hel::hel_checkpoint::TargetCheckpoint =
1102 serde_json::from_slice(&exported.stdout)
1103 .context("decode target checkpoint result")?;
1104 if let Some(export_ms) = export_ms {
1105 let timings = target_checkpoint.timings.unwrap_or_default();
1108 tracing::info!(
1109 session_id,
1110 export_ms,
1111 timings_reported = target_checkpoint.timings.is_some(),
1112 native_ms = timings.native_ms,
1113 repositories_ms = timings.repositories_ms,
1114 archive_ms = timings.archive_ms,
1115 worker_total_ms = timings.total_ms,
1116 "checkpoint archive exported on the target"
1117 );
1118 }
1119 if target_checkpoint.event_frontier != expected_ordinal {
1120 bail!(
1121 "target checkpoint event frontier changed: expected {expected_ordinal}, found {}",
1122 target_checkpoint.event_frontier
1123 );
1124 }
1125 if target_checkpoint.event_frontier_digest != expected_digest {
1126 bail!("target checkpoint event frontier digest changed");
1127 }
1128
1129 let archive_id = new_command_id("archive")?;
1134 let destination = sessions_dir().join(format!(
1135 "{session_id}-{}-{archive_id}.hel.zip",
1136 target_checkpoint.event_frontier
1137 ));
1138 let transfer = CheckpointTransfer {
1139 locator: &backend,
1140 session_id,
1141 remote_archive: &remote_archive,
1142 destination: &destination,
1143 expected_sha256: &target_checkpoint.sha256,
1144 expected_event_frontier: target_checkpoint.event_frontier,
1145 expected_event_frontier_digest: &target_checkpoint.event_frontier_digest,
1146 };
1147 let metadata = {
1148 let _verifying = ProvisionStageGuard::new(executor, ProvisionStage::Verifying);
1149 let transfer_started = Instant::now();
1150 let verified = transfer.execute(executor)?;
1151 tracing::info!(
1152 session_id,
1153 transfer_and_checksum_ms = transfer_started.elapsed().as_millis() as u64,
1154 "checkpoint archive transferred and checksum-verified"
1155 );
1156 let installed_archive = verified.archive_path().to_path_buf();
1157 let validate_transferred = || -> Result<()> {
1158 ensure!(
1159 verified.sha256() == target_checkpoint.sha256,
1160 "target and controller checkpoint checksums differ"
1161 );
1162 ensure!(
1163 verified.event_frontier_digest() == expected_digest,
1164 "verified checkpoint event frontier digest changed"
1165 );
1166 Ok(())
1167 };
1168 if let Err(error) = validate_transferred() {
1169 return Err(remove_uninstalled_checkpoint(&installed_archive, error));
1170 }
1171 if completion == CheckpointCompletion::HeldBarrier {
1175 let revalidated = relay.sync_snapshot().await.and_then(|snapshot| {
1176 if releases_after_capture {
1177 validate_automatic_checkpoint_barrier_snapshot(
1178 &snapshot,
1179 &barrier_command_id,
1180 &cursor,
1181 session.harness_kind,
1182 )
1183 } else {
1184 validate_checkpoint_barrier_snapshot(
1185 &snapshot,
1186 &barrier_command_id,
1187 &cursor,
1188 )
1189 }
1190 });
1191 if let Err(error) = revalidated {
1192 return Err(remove_uninstalled_checkpoint(
1193 &installed_archive,
1194 error.context(
1195 "checkpoint barrier changed while transferring its archive",
1196 ),
1197 ));
1198 }
1199 }
1200 if let Err(error) = transfer
1201 .cleanup_plan(&verified)
1202 .and_then(|plan| plan.execute(executor).map(|_| ()))
1203 {
1204 return Err(remove_uninstalled_checkpoint(
1205 &installed_archive,
1206 error.context("clean target checkpoint staging"),
1207 ));
1208 }
1209 CheckpointMetadata {
1210 archive_path: verified.archive_path().to_path_buf(),
1211 sha256: verified.sha256().to_string(),
1212 created_at: checkpointed_at.clone(),
1213 event_frontier: verified.event_frontier(),
1214 }
1215 };
1216 Ok(CheckpointArtifact {
1217 metadata,
1218 native_session_id,
1219 event_frontier_digest: expected_digest,
1220 })
1221 }
1222 .await;
1223
1224 let artifact = match exported {
1225 Ok(artifact) => artifact,
1226 Err(error) => {
1227 if completion == CheckpointCompletion::HeldBarrier
1233 && let Err(cancel_error) = relay.cancel_abandoned_barrier().await
1234 {
1235 tracing::warn!(
1236 session_id,
1237 "failed checkpoint could not cancel its relay barrier: {cancel_error:#}"
1238 );
1239 }
1240 return Err(error);
1241 }
1242 };
1243 Ok(LatchedCheckpoint {
1244 artifact,
1245 relay,
1246 barrier_command_id,
1247 cursor,
1248 completion,
1249 })
1250 }
1251
1252 async fn open_checkpoint_relay(
1256 &self,
1257 session_id: &str,
1258 executor: &(impl CommandExecutor + Sync),
1259 manager: Option<&SessionManagerControl>,
1260 target: InstalledWorkerRestart<'_>,
1261 restart_if_unreachable: bool,
1262 ) -> Result<(ControllerRelayLease, bool)> {
1263 let project_memory = match self.project_memory_sync_target(session_id) {
1264 Ok(target) => Some(target),
1265 Err(error) => {
1266 tracing::warn!(
1267 session_id,
1268 error = format!("{error:#}"),
1269 "project memory will not be synchronized during checkpoint reconnect"
1270 );
1271 None
1272 }
1273 };
1274 match connect_checkpoint_relay(
1275 session_id,
1276 manager,
1277 target.reconnect,
1278 project_memory.clone(),
1279 )
1280 .await
1281 {
1282 Ok(relay) => Ok((relay, false)),
1283 Err(error) if worker_connect_needs_restart(&error) && restart_if_unreachable => {
1284 tracing::warn!(
1285 session_id,
1286 "checkpoint could not reach the worker; restarting it: {error:#}"
1287 );
1288 let mut connection = self
1289 .restart_worker_for_checkpoint(
1290 session_id,
1291 executor,
1292 target.backend,
1293 target.worker_root,
1294 target.reconnect,
1295 )
1296 .await?;
1297 connection.set_project_memory_target(project_memory);
1298 let relay =
1299 adopt_restarted_checkpoint_relay(session_id, manager, connection).await?;
1300 Ok((relay, true))
1301 }
1302 Err(error) if worker_connect_needs_restart(&error) => {
1303 Err(error.context(CheckpointDeferred::background_work()))
1304 }
1305 Err(error) => Err(error).context("connect to the session worker for checkpoint"),
1306 }
1307 }
1308
1309 async fn restart_worker_for_checkpoint(
1313 &self,
1314 session_id: &str,
1315 executor: &(impl CommandExecutor + Sync),
1316 backend: &hel_targets::TargetLocator,
1317 worker_root: &str,
1318 reconnect: &hel_targets::CommandSpec,
1319 ) -> Result<StandaloneSession> {
1320 self.restart_worker_with_installed_binary(
1321 session_id,
1322 executor,
1323 InstalledWorkerRestart {
1324 backend,
1325 worker_root,
1326 reconnect,
1327 launch: None,
1328 messages: &RESTART_FOR_CHECKPOINT,
1329 },
1330 )
1331 .await
1332 }
1333}
1334
1335async fn connect_checkpoint_relay(
1336 session_id: &str,
1337 manager: Option<&SessionManagerControl>,
1338 reconnect: &hel_targets::CommandSpec,
1339 project_memory: Option<crate::hel_session_manager::ProjectMemorySyncTarget>,
1340) -> Result<ControllerRelayLease> {
1341 if let Some(manager) = manager {
1342 let handle = manager
1343 .wait_for_session(session_id, Duration::from_secs(5))
1344 .await?;
1345 let mut lease = handle.lease_connection().await?;
1346 lease
1347 .connection_mut()
1348 .set_project_memory_target(project_memory);
1349 Ok(ControllerRelayLease::Managed {
1350 handle,
1351 lease: Some(lease),
1352 })
1353 } else {
1354 let target = crate::hel_session_manager::RelaySessionTarget {
1355 session_id: session_id.to_owned(),
1356 spec: reconnect.clone(),
1357 worker_recovery: None,
1358 project_memory,
1359 };
1360 Ok(ControllerRelayLease::Standalone(
1361 StandaloneSession::connect(&target).await?,
1362 ))
1363 }
1364}
1365
1366async fn adopt_restarted_checkpoint_relay(
1367 session_id: &str,
1368 manager: Option<&SessionManagerControl>,
1369 connection: StandaloneSession,
1370) -> Result<ControllerRelayLease> {
1371 let Some(manager) = manager else {
1372 return Ok(ControllerRelayLease::Standalone(connection));
1373 };
1374 let handle = manager
1375 .wait_for_session(session_id, Duration::from_secs(5))
1376 .await?;
1377 match handle.lease_connection().await {
1378 Ok(mut lease) => {
1379 lease.replace_connection(connection);
1380 Ok(ControllerRelayLease::Managed {
1381 handle,
1382 lease: Some(lease),
1383 })
1384 }
1385 Err(error) => {
1386 tracing::warn!(
1387 session_id,
1388 "session actor could not lease after worker restart; using the restarted proxy: {error:#}"
1389 );
1390 Ok(ControllerRelayLease::Standalone(connection))
1391 }
1392 }
1393}
1394
1395#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1397enum BarrierBusyPolicy {
1398 DeferWhileRunning,
1404 InterruptWhileRunning,
1408}
1409
1410impl BarrierBusyPolicy {
1411 fn of(exclusivity: LatchExclusivity) -> Self {
1412 match exclusivity {
1413 LatchExclusivity::ReleaseAfterLatch => Self::DeferWhileRunning,
1414 LatchExclusivity::HoldThroughClose => Self::InterruptWhileRunning,
1415 }
1416 }
1417}
1418
1419async fn wait_for_checkpoint_barrier(
1420 relay: &mut StandaloneSession,
1421 session_id: &str,
1422 command_id: &str,
1423 timeout: Duration,
1424 busy: BarrierBusyPolicy,
1425 harness: HarnessKind,
1426) -> Result<ManagedSessionSnapshot> {
1427 let deadline = tokio::time::Instant::now() + timeout;
1428 let mut cancel_submitted = false;
1429 let mut cancel_deadline = None;
1430 let mut cancel_started_at: Option<Instant> = None;
1431 loop {
1432 let snapshot = relay.sync().await?;
1433 if busy == BarrierBusyPolicy::DeferWhileRunning
1434 && !snapshot.operational.safe_for_checkpoint(harness)
1435 {
1436 return Err(CheckpointDeferred::background_work().into());
1441 }
1442 if checkpoint_barrier_is_ready(&snapshot, command_id) {
1443 if let Some(started_at) = cancel_started_at {
1444 tracing::info!(
1445 session_id,
1446 barrier_command_id = command_id,
1447 cancellation_ms = started_at.elapsed().as_millis() as u64,
1448 "active turn cancellation settled before checkpoint barrier"
1449 );
1450 }
1451 return Ok(snapshot);
1452 }
1453 if busy == BarrierBusyPolicy::InterruptWhileRunning
1454 && snapshot.operational.execution == RelayExecutionState::Running
1455 && !cancel_submitted
1456 {
1457 let cancel_turn = RelayCommand::CancelTurn;
1458 if relay.protocol_version() < cancel_turn.minimum_protocol() {
1459 return Err(CheckpointBarrierUnreachable::cancel_turn_unavailable(
1460 command_id,
1461 relay.protocol_version(),
1462 )
1463 .into());
1464 }
1465 let cancel_command_id = new_command_id("checkpoint-cancel-turn")?;
1466 match relay.submit(cancel_command_id, cancel_turn).await {
1467 Ok(_) => {
1468 cancel_submitted = true;
1469 cancel_started_at = Some(Instant::now());
1470 cancel_deadline = Some(tokio::time::Instant::now() + CHECKPOINT_CANCEL_TIMEOUT);
1471 tracing::info!(
1472 session_id,
1473 barrier_command_id = command_id,
1474 "requested active turn cancellation before checkpoint barrier"
1475 );
1476 }
1477 Err(error) if checkpoint_cancel_turn_needs_worker_restart(&error) => {
1478 return Err(error.context(
1479 CheckpointBarrierUnreachable::cancel_turn_unavailable(
1480 command_id,
1481 relay.protocol_version(),
1482 ),
1483 ));
1484 }
1485 Err(error) if worker_connect_needs_restart(&error) => {
1486 return Err(error.context(
1487 CheckpointBarrierUnreachable::cancel_turn_unreachable(command_id),
1488 ));
1489 }
1490 Err(error) => {
1491 if let Ok(snapshot) = relay.sync().await
1495 && checkpoint_barrier_is_ready(&snapshot, command_id)
1496 {
1497 tracing::info!(
1498 session_id,
1499 barrier_command_id = command_id,
1500 "active turn settled while submitting checkpoint cancellation"
1501 );
1502 return Ok(snapshot);
1503 }
1504 return Err(error.context("cancel active ACP turn before checkpoint barrier"));
1505 }
1506 }
1507 continue;
1508 }
1509 let out_of_time = tokio::time::Instant::now() >= cancel_deadline.unwrap_or(deadline);
1510 if let Some(error) = checkpoint_barrier_wait_ended(
1511 &snapshot,
1512 command_id,
1513 busy,
1514 out_of_time,
1515 cancel_submitted,
1516 ) {
1517 return Err(error);
1518 }
1519 tokio::time::sleep(std::time::Duration::from_millis(100)).await;
1520 }
1521}
1522
1523fn checkpoint_barrier_wait_ended(
1530 snapshot: &ManagedSessionSnapshot,
1531 command_id: &str,
1532 busy: BarrierBusyPolicy,
1533 out_of_time: bool,
1534 cancel_submitted: bool,
1535) -> Option<anyhow::Error> {
1536 if snapshot.operational.execution == RelayExecutionState::Closed {
1537 return Some(CheckpointBarrierUnreachable::runtime_stopped().into());
1538 }
1539 if snapshot.operational.execution == RelayExecutionState::Running {
1540 return Some(match busy {
1541 BarrierBusyPolicy::DeferWhileRunning => CheckpointDeferred::harness_busy().into(),
1542 BarrierBusyPolicy::InterruptWhileRunning if out_of_time && cancel_submitted => {
1543 CheckpointBarrierUnreachable::cancel_timed_out(command_id).into()
1544 }
1545 BarrierBusyPolicy::InterruptWhileRunning => return None,
1546 });
1547 }
1548 out_of_time.then(|| CheckpointBarrierUnreachable::not_admitted(command_id).into())
1549}
1550
1551#[derive(Debug)]
1558struct CheckpointBarrierUnreachable(String);
1559
1560impl CheckpointBarrierUnreachable {
1561 fn runtime_stopped() -> Self {
1562 Self("ACP runtime stopped before reaching the checkpoint barrier".to_owned())
1563 }
1564
1565 fn not_admitted(command_id: &str) -> Self {
1566 Self(format!(
1567 "ACP relay did not reach checkpoint barrier {command_id}"
1568 ))
1569 }
1570
1571 fn cancel_timed_out(command_id: &str) -> Self {
1572 Self(format!(
1573 "active ACP turn did not settle after cancellation before checkpoint barrier {command_id}"
1574 ))
1575 }
1576
1577 fn cancel_turn_unavailable(command_id: &str, protocol_version: u32) -> Self {
1578 Self(format!(
1579 "worker protocol {protocol_version} cannot cancel the active ACP turn before checkpoint barrier {command_id} (requires protocol {})",
1580 RelayCommand::CancelTurn.minimum_protocol(),
1581 ))
1582 }
1583
1584 fn cancel_turn_unreachable(command_id: &str) -> Self {
1585 Self(format!(
1586 "worker transport became unavailable while cancelling the active ACP turn before checkpoint barrier {command_id}"
1587 ))
1588 }
1589}
1590
1591impl std::fmt::Display for CheckpointBarrierUnreachable {
1592 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1593 formatter.write_str(&self.0)
1594 }
1595}
1596
1597impl std::error::Error for CheckpointBarrierUnreachable {}
1598
1599fn checkpoint_barrier_needs_worker_restart(error: &anyhow::Error) -> bool {
1600 error
1601 .downcast_ref::<CheckpointBarrierUnreachable>()
1602 .is_some()
1603}
1604
1605fn checkpoint_cancel_turn_needs_worker_restart(error: &anyhow::Error) -> bool {
1610 error.chain().any(|cause| {
1611 let Some(rejected) = cause.downcast_ref::<RelayRejected>() else {
1612 return false;
1613 };
1614 rejected.0.code == hel::hel_worker::RelayErrorCode::IncompatibleProtocol
1615 })
1616}
1617
1618#[derive(Debug)]
1628pub struct CheckpointDeferred(String);
1629
1630impl CheckpointDeferred {
1631 pub(crate) fn harness_busy() -> Self {
1632 Self("the agent is working; try again when it is idle".to_owned())
1633 }
1634
1635 fn background_work() -> Self {
1636 Self(
1637 "Kimi background-agent state is unknown or still active; checkpoint deferred until it is synchronized and idle"
1638 .to_owned(),
1639 )
1640 }
1641
1642 fn frontier_moved() -> Self {
1643 Self(
1644 "the session moved past the checkpoint-ready cursor before the barrier latched, so this checkpoint was deferred"
1645 .to_owned(),
1646 )
1647 }
1648
1649 fn harness_turn_during_capture() -> Self {
1650 Self(
1651 "the agent started a turn of its own while target state was captured, so this checkpoint was deferred"
1652 .to_owned(),
1653 )
1654 }
1655}
1656
1657impl std::fmt::Display for CheckpointDeferred {
1658 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1659 formatter.write_str(&self.0)
1660 }
1661}
1662
1663impl std::error::Error for CheckpointDeferred {}
1664
1665pub fn checkpoint_was_deferred(error: &anyhow::Error) -> bool {
1670 error
1671 .chain()
1672 .any(|cause| cause.downcast_ref::<CheckpointDeferred>().is_some())
1673}
1674
1675fn checkpoint_barrier_is_ready(snapshot: &ManagedSessionSnapshot, command_id: &str) -> bool {
1676 snapshot.operational.checkpoint_barrier.as_deref() == Some(command_id)
1677 && snapshot.operational.checkpoint_ready.is_some()
1678}
1679
1680fn ensure_exact_checkpoint_cut(
1688 cursor: &RelayCursor,
1689 expected_ordinal: u64,
1690 expected_digest: &str,
1691) -> Result<()> {
1692 if cursor.ordinal != expected_ordinal || cursor.digest != expected_digest {
1693 bail!(CheckpointDeferred::frontier_moved());
1694 }
1695 Ok(())
1696}
1697
1698fn validate_checkpoint_barrier_snapshot(
1713 snapshot: &ManagedSessionSnapshot,
1714 command_id: &str,
1715 expected: &RelayCursor,
1716) -> Result<()> {
1717 ensure!(
1718 snapshot.operational.checkpoint_barrier.as_deref() == Some(command_id),
1719 "checkpoint barrier {command_id} is no longer active"
1720 );
1721 ensure!(
1722 snapshot.operational.checkpoint_ready.as_ref() == Some(expected),
1723 "checkpoint barrier {command_id} has a different ready cursor"
1724 );
1725 if snapshot
1726 .operational
1727 .last_harness_turn_started_ordinal
1728 .is_some_and(|ordinal| ordinal > expected.ordinal)
1729 {
1730 bail!(CheckpointDeferred::harness_turn_during_capture());
1731 }
1732 Ok(())
1733}
1734
1735fn validate_automatic_checkpoint_barrier_snapshot(
1739 snapshot: &ManagedSessionSnapshot,
1740 command_id: &str,
1741 expected: &RelayCursor,
1742 harness: HarnessKind,
1743) -> Result<()> {
1744 validate_checkpoint_barrier_snapshot(snapshot, command_id, expected)?;
1745 ensure!(
1746 snapshot.operational.safe_for_checkpoint(harness),
1747 CheckpointDeferred::background_work()
1748 );
1749 Ok(())
1750}
1751
1752fn remove_uninstalled_checkpoint(path: &Path, error: anyhow::Error) -> anyhow::Error {
1753 match std::fs::remove_file(path) {
1754 Ok(()) => error,
1755 Err(remove_error) if remove_error.kind() == std::io::ErrorKind::NotFound => error,
1756 Err(remove_error) => error.context(format!(
1757 "also failed to remove uninstalled checkpoint {}: {remove_error}",
1758 path.display()
1759 )),
1760 }
1761}
1762
1763pub(super) async fn wait_for_relay_closed(relay: &mut StandaloneSession) -> Result<()> {
1764 let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(30);
1765 loop {
1766 if relay.sync().await?.operational.execution == RelayExecutionState::Closed {
1767 return Ok(());
1768 }
1769 if tokio::time::Instant::now() >= deadline {
1770 bail!("ACP runtime did not close within 30 seconds");
1771 }
1772 tokio::time::sleep(std::time::Duration::from_millis(100)).await;
1773 }
1774}
1775
1776async fn release_checkpoint_after_capture(
1788 relay: &mut ControllerRelayLease,
1789 session_id: &str,
1790 barrier_command_id: &str,
1791 cursor: &RelayCursor,
1792 harness: HarnessKind,
1793) -> Result<CheckpointCompletion> {
1794 relay
1795 .sync_snapshot()
1796 .await
1797 .and_then(|snapshot| {
1798 validate_automatic_checkpoint_barrier_snapshot(
1799 &snapshot,
1800 barrier_command_id,
1801 cursor,
1802 harness,
1803 )
1804 })
1805 .context("checkpoint barrier changed while capturing target state")?;
1806 match relay
1807 .submit(
1808 new_command_id("checkpoint-release")?,
1809 RelayCommand::ReleaseCheckpoint {
1810 barrier_command_id: barrier_command_id.to_owned(),
1811 },
1812 )
1813 .await
1814 {
1815 Ok(_) => Ok(CheckpointCompletion::ReleasedAfterCapture),
1816 Err(error) => {
1817 tracing::debug!(
1818 session_id,
1819 "relay kept the checkpoint barrier through the transfer: {error:#}"
1820 );
1821 Ok(CheckpointCompletion::HeldBarrier)
1822 }
1823 }
1824}
1825
1826fn run_checkpoint_staging_command<T: serde::Serialize>(
1827 executor: &impl CommandExecutor,
1828 locator: &hel_targets::TargetLocator,
1829 session_id: &str,
1830 spec: &T,
1831 command: fn(&hel_targets::TargetLocator, &str) -> Result<CommandSpec>,
1832 operation: &str,
1833) -> Result<CommandOutput> {
1834 let body = serde_json::to_vec(spec).with_context(|| format!("serialize {operation} spec"))?;
1835 let mut replaced_worker = false;
1836 loop {
1837 let command = command(locator, session_id)?;
1838 let output = executor.execute_with_stdin(&command, &mut body.as_slice())?;
1839 if output.status == 0 {
1840 return Ok(output);
1841 }
1842 let failure = String::from_utf8_lossy(&output.stderr).into_owned();
1843 if staging_protocol_unsupported(&failure)
1844 && replace_stale_export_worker(
1845 executor,
1846 locator,
1847 session_id,
1848 None,
1849 &failure,
1850 &mut replaced_worker,
1851 )?
1852 {
1853 continue;
1854 }
1855 bail!(
1856 "{operation} failed with status {}: {failure}",
1857 output.status
1858 );
1859 }
1860}
1861
1862fn export_target_checkpoint(
1867 executor: &impl CommandExecutor,
1868 locator: &hel_targets::TargetLocator,
1869 session_id: &str,
1870 spec: &CheckpointExportSpec,
1871 remote_spec: &str,
1872) -> Result<CommandOutput> {
1873 export_target_checkpoint_with_worker(executor, locator, session_id, spec, remote_spec, None)
1874}
1875
1876fn export_target_checkpoint_with_worker(
1877 executor: &impl CommandExecutor,
1878 locator: &hel_targets::TargetLocator,
1879 session_id: &str,
1880 spec: &CheckpointExportSpec,
1881 remote_spec: &str,
1882 worker_binary: Option<&Path>,
1883) -> Result<CommandOutput> {
1884 let body = serde_json::to_vec(spec).context("serialize checkpoint export spec")?;
1885 let mut replaced_worker = false;
1886 loop {
1887 let streamed = export_stdin_command(locator, session_id)?;
1888 let output = executor.execute_with_stdin(&streamed, &mut body.as_slice())?;
1889 if output.status == 0 {
1890 return Ok(output);
1891 }
1892 let failure = String::from_utf8_lossy(&output.stderr).into_owned();
1893 if export_spec_stdin_unsupported(&failure) {
1894 tracing::debug!(
1895 session_id,
1896 "target worker predates streamed checkpoint specs; uploading the spec file instead"
1897 );
1898 let output = export_uploaded_spec(executor, locator, session_id, spec, remote_spec)?;
1899 if output.status == 0 {
1900 return Ok(output);
1901 }
1902 let failure = String::from_utf8_lossy(&output.stderr).into_owned();
1903 if replace_stale_export_worker(
1904 executor,
1905 locator,
1906 session_id,
1907 worker_binary,
1908 &failure,
1909 &mut replaced_worker,
1910 )? {
1911 continue;
1912 }
1913 bail!(
1914 "export target checkpoint failed with status {}: {failure}",
1915 output.status
1916 );
1917 }
1918 if replace_stale_export_worker(
1919 executor,
1920 locator,
1921 session_id,
1922 worker_binary,
1923 &failure,
1924 &mut replaced_worker,
1925 )? {
1926 continue;
1927 }
1928 bail!(
1929 "{} failed with status {}: {failure}",
1930 streamed.purpose,
1931 output.status
1932 );
1933 }
1934}
1935
1936fn export_uploaded_spec(
1937 executor: &impl CommandExecutor,
1938 locator: &hel_targets::TargetLocator,
1939 session_id: &str,
1940 spec: &CheckpointExportSpec,
1941 remote_spec: &str,
1942) -> Result<CommandOutput> {
1943 let staging = tempfile::tempdir().context("create checkpoint staging")?;
1944 let local_spec = staging.path().join("checkpoint-spec.json");
1945 spec.write(&local_spec)?;
1946 upload_checkpoint_spec(executor, locator, session_id, &local_spec, remote_spec)?;
1947 executor.execute(&export_command(locator, session_id, remote_spec)?)
1948}
1949
1950fn replace_stale_export_worker(
1955 executor: &impl CommandExecutor,
1956 locator: &hel_targets::TargetLocator,
1957 session_id: &str,
1958 worker_binary: Option<&Path>,
1959 failure: &str,
1960 replaced_worker: &mut bool,
1961) -> Result<bool> {
1962 if *replaced_worker || !staging_protocol_unsupported(failure) {
1963 return Ok(false);
1964 }
1965 tracing::debug!(
1966 session_id,
1967 "target worker does not support this checkpoint export protocol; replacing the installed Mjolnir binary and retrying"
1968 );
1969 let owned_binary;
1970 let binary = if let Some(path) = worker_binary {
1971 path
1972 } else {
1973 owned_binary = super::worker_binary::worker_binary_for(locator, executor)?;
1974 owned_binary.as_path()
1975 };
1976 super::worker_binary::replace_installed_worker_binary(executor, locator, session_id, binary)?;
1977 *replaced_worker = true;
1978 Ok(true)
1979}
1980
1981fn export_spec_stdin_unsupported(failure: &str) -> bool {
1989 failure.contains("read checkpoint export spec -")
1990 || failure.contains("unexpected argument")
1991 || failure.contains("invalid value")
1992}
1993
1994fn export_spec_schema_unsupported(failure: &str) -> bool {
1999 failure.contains("parse checkpoint")
2000 && (failure.contains("unknown field") || failure.contains("unknown variant"))
2001}
2002
2003fn export_protocol_unsupported(failure: &str) -> bool {
2004 export_spec_schema_unsupported(failure)
2005 || failure.contains("unsupported checkpoint export protocol version")
2006}
2007
2008fn staging_protocol_unsupported(failure: &str) -> bool {
2009 export_protocol_unsupported(failure)
2010 || failure.contains("unsupported checkpoint staging protocol version")
2011 || failure.contains("unrecognized subcommand")
2012 || failure.contains("unexpected argument")
2013}
2014
2015pub(super) fn upload_checkpoint_spec(
2016 executor: &impl CommandExecutor,
2017 locator: &hel_targets::TargetLocator,
2018 session_id: &str,
2019 local: &Path,
2020 remote: &str,
2021) -> Result<()> {
2022 match locator {
2023 hel_targets::TargetLocator::LocalBare { .. } => {
2024 std::fs::copy(local, remote)
2025 .with_context(|| format!("copy checkpoint specification to {remote}"))?;
2026 Ok(())
2027 }
2028 hel_targets::TargetLocator::LocalPodman { container_id, .. } => execute_checked(
2029 executor,
2030 CommandSpec::new(
2031 "podman",
2032 [
2033 "cp".into(),
2034 local.to_string_lossy().into_owned(),
2035 format!("{container_id}:{remote}"),
2036 ],
2037 )
2038 .purpose("upload checkpoint specification"),
2039 )
2040 .map(|_| ()),
2041 hel_targets::TargetLocator::LocalDocker { container_id } => execute_checked(
2042 executor,
2043 CommandSpec::new(
2044 "docker",
2045 [
2046 "cp".into(),
2047 local.to_string_lossy().into_owned(),
2048 format!("{container_id}:{remote}"),
2049 ],
2050 )
2051 .purpose("upload checkpoint specification"),
2052 )
2053 .map(|_| ()),
2054 hel_targets::TargetLocator::AppleContainer { container_id } => execute_checked(
2055 executor,
2056 CommandSpec::new(
2057 "container",
2058 [
2059 "cp".into(),
2060 local.to_string_lossy().into_owned(),
2061 format!("{container_id}:{remote}"),
2062 ],
2063 )
2064 .purpose("upload checkpoint specification"),
2065 )
2066 .map(|_| ()),
2067 hel_targets::TargetLocator::AwsEc2 { ssh, .. }
2068 | hel_targets::TargetLocator::SshBare { ssh, .. } => execute_checked(
2069 executor,
2070 scp_command_spec(ssh, local, remote, false).purpose("upload checkpoint specification"),
2071 )
2072 .map(|_| ()),
2073 hel_targets::TargetLocator::SshPodman {
2074 ssh, container_id, ..
2075 }
2076 | hel_targets::TargetLocator::SshDocker { ssh, container_id } => {
2077 let engine = match locator {
2078 hel_targets::TargetLocator::SshPodman { .. } => "podman",
2079 hel_targets::TargetLocator::SshDocker { .. } => "docker",
2080 _ => unreachable!("matched remote container target"),
2081 };
2082 let staging = format!(".local/share/hel/uploads/{session_id}-checkpoint.json");
2083 execute_checked(
2084 executor,
2085 ssh_command_spec(ssh, ["mkdir", "-p", ".local/share/hel/uploads"])
2086 .purpose("create remote checkpoint staging"),
2087 )?;
2088 execute_checked(
2089 executor,
2090 scp_command_spec(ssh, local, &staging, false)
2091 .purpose("upload remote container checkpoint specification"),
2092 )?;
2093 execute_checked(
2094 executor,
2095 ssh_command_spec(
2096 ssh,
2097 [engine, "cp", &staging, &format!("{container_id}:{remote}")],
2098 )
2099 .purpose("install remote container checkpoint specification"),
2100 )?;
2101 execute_checked(
2102 executor,
2103 ssh_command_spec(ssh, ["rm", "-f", "--", &staging])
2104 .purpose("remove remote checkpoint staging"),
2105 )?;
2106 Ok(())
2107 }
2108 }?;
2109 Ok(())
2110}
2111
2112fn reusable_installed_checkpoint(
2120 session_id: &str,
2121 installed: Option<&CheckpointMetadata>,
2122 native_session_id: &str,
2123 latched_ordinal: u64,
2124 latched_session: &CanonicalSessionSnapshot,
2125) -> Option<CheckpointArtifact> {
2126 let installed = installed?;
2127 if installed.event_frontier > latched_ordinal {
2128 tracing::warn!(
2129 session_id,
2130 installed_frontier = installed.event_frontier,
2131 latched_ordinal,
2132 "installed checkpoint is ahead of the latched cursor; exporting a fresh archive"
2133 );
2134 return None;
2135 }
2136 let verified = match verify_archive_streaming(&installed.archive_path) {
2137 Ok(verified) => verified,
2138 Err(error) => {
2139 tracing::warn!(
2140 session_id,
2141 path = %installed.archive_path.display(),
2142 "installed checkpoint could not be verified for reuse: {error:#}"
2143 );
2144 return None;
2145 }
2146 };
2147 if verified.archive_sha256 != installed.sha256
2148 || verified.manifest.session.id != session_id
2149 || verified.canonical_session.event_frontier != installed.event_frontier
2150 {
2151 tracing::warn!(
2152 session_id,
2153 path = %installed.archive_path.display(),
2154 "installed checkpoint no longer matches its controller metadata; exporting a fresh archive"
2155 );
2156 return None;
2157 }
2158 if !verified.canonical_session.content_matches(latched_session) {
2159 tracing::info!(
2160 session_id,
2161 archive_frontier = verified.canonical_session.event_frontier,
2162 latched_ordinal,
2163 "session content changed since the installed checkpoint; exporting a fresh archive"
2164 );
2165 return None;
2166 }
2167 tracing::info!(
2168 session_id,
2169 archive_frontier = verified.canonical_session.event_frontier,
2170 latched_ordinal,
2171 "reusing the installed checkpoint archive; only relay bookkeeping moved"
2172 );
2173 Some(CheckpointArtifact {
2174 metadata: installed.clone(),
2175 native_session_id: native_session_id.to_owned(),
2176 event_frontier_digest: verified.canonical_session.event_frontier_digest,
2177 })
2178}
2179
2180pub(super) fn verify_installed_checkpoint_gate(
2181 session_id: &str,
2182 checkpoint: &CheckpointMetadata,
2183) -> Result<()> {
2184 let sha256 = checkpoint_sha256(&checkpoint.archive_path).with_context(|| {
2185 format!(
2186 "hash installed checkpoint {} before target cleanup",
2187 checkpoint.archive_path.display()
2188 )
2189 })?;
2190 ensure!(
2191 sha256 == checkpoint.sha256,
2192 "refusing target cleanup for session {session_id}: installed checkpoint SHA changed"
2193 );
2194 Ok(())
2195}
2196
2197fn verify_checkpoint_artifact(session_id: &str, artifact: &CheckpointArtifact) -> Result<()> {
2198 let sha256 = checkpoint_sha256(&artifact.metadata.archive_path).with_context(|| {
2199 format!(
2200 "hash completed checkpoint {}",
2201 artifact.metadata.archive_path.display()
2202 )
2203 })?;
2204 ensure!(
2205 sha256 == artifact.metadata.sha256,
2206 "completed checkpoint SHA changed before persistence for session {session_id}"
2207 );
2208 Ok(())
2209}
2210
2211pub(super) fn release_projection_behind_checkpoint(session_id: &str, current: &CheckpointMetadata) {
2219 match hel::hel_database::compact_materialized_transcript_through(
2220 session_id,
2221 current.event_frontier,
2222 ) {
2223 Ok(retention) if retention.items == 0 => {}
2224 Ok(retention) => tracing::info!(
2225 session_id,
2226 items = retention.items,
2227 bytes = retention.bytes,
2228 remaining = retention.remaining,
2229 event_frontier = current.event_frontier,
2230 "released projection history the checkpoint covers"
2231 ),
2232 Err(error) => tracing::warn!(
2233 session_id,
2234 "checkpoint was saved, but the projection history it covers could not be released: {error:#}"
2235 ),
2236 }
2237}
2238
2239pub(super) fn prune_replaced_checkpoint(
2240 previous: Option<&CheckpointMetadata>,
2241 current: &CheckpointMetadata,
2242) {
2243 let Some(previous) = previous.filter(|old| old.archive_path != current.archive_path) else {
2244 return;
2245 };
2246 match hel::hel_database::move_checkpoint_is_retained(&previous.archive_path) {
2247 Ok(true) => return,
2248 Ok(false) => {}
2249 Err(error) => {
2250 tracing::warn!(%error, "could not check move retention; keeping superseded checkpoint");
2251 return;
2252 }
2253 }
2254 if let Err(error) = std::fs::remove_file(&previous.archive_path)
2255 && error.kind() != std::io::ErrorKind::NotFound
2256 {
2257 tracing::warn!(
2258 path = %previous.archive_path.display(),
2259 "could not remove superseded recovery copy: {error}"
2260 );
2261 }
2262}
2263
2264#[cfg(test)]
2265mod tests {
2266 use std::cell::{Cell, RefCell};
2267 use std::collections::BTreeMap;
2268 use std::fs::OpenOptions;
2269 use std::path::{Path, PathBuf};
2270 #[cfg(unix)]
2271 use std::process::Command;
2272 #[cfg(unix)]
2273 use std::time::Duration;
2274
2275 #[cfg(unix)]
2276 use agent_client_protocol::schema::v1::{ContentBlock, TextContent};
2277 use anyhow::Result;
2278
2279 #[cfg(unix)]
2280 use crate::hel_controller::now;
2281 use crate::hel_controller::restore_session_after_persistence_failure;
2282 use crate::hel_controller::test_support::{
2283 checkpoint_test_session, write_checkpoint_gate_archive,
2284 };
2285 #[cfg(unix)]
2286 use crate::hel_session_manager::{ManagedSessionHandle, new_command_id};
2287 use crate::hel_worker_client::RelayTransportDead;
2288 use hel::hel_archive::{
2289 BundleManifest, CanonicalTranscriptBody, CanonicalTranscriptItem, TargetManifest,
2290 };
2291 use hel::hel_checkpoint::CheckpointExportSpec;
2292 #[cfg(unix)]
2293 use hel::hel_config::{
2294 HarnessProfile, HelConfig, ProjectBundle, ProjectRepository, TargetTemplate,
2295 };
2296 use hel::hel_projection::canonical_session_from_materialized;
2297 #[cfg(unix)]
2298 use hel::hel_state::TargetLocator;
2299 use hel::hel_state::{
2300 CheckpointMetadata, HelState, ManagedSessionSnapshot, MaterializedSession, SessionState,
2301 };
2302 #[cfg(unix)]
2303 use hel::hel_targets::ProvisionStage;
2304 use hel::hel_targets::{self, CommandExecutor, CommandOutput, CommandSpec};
2305 #[cfg(unix)]
2306 use hel::hel_worker::RelayCommandOutcome;
2307 use hel::hel_worker::{RelayCommand, RelayCursor, RelayExecutionState};
2308
2309 use super::*;
2310
2311 #[test]
2312 fn startup_reconciliation_only_removes_unreferenced_controller_checkpoints() {
2313 let directory = tempfile::tempdir().unwrap();
2314 let session_id = "1123456789abcdef0123456789abcdef";
2315 let referenced_name =
2316 format!("{session_id}-7-archive-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.hel.zip");
2317 let orphan_name =
2318 format!("{session_id}-8-archive-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.hel.zip");
2319 let imported_name = format!("{session_id}.hel.zip");
2320 for name in [
2321 &referenced_name,
2322 &orphan_name,
2323 &imported_name,
2324 "notes.hel.zip",
2325 ] {
2326 std::fs::write(directory.path().join(name), b"test").unwrap();
2327 }
2328 let mut state = HelState::default();
2329 let mut session = checkpoint_test_session(session_id);
2330 session.checkpoint = Some(CheckpointMetadata {
2331 archive_path: directory.path().join(&referenced_name),
2332 sha256: "c".repeat(64),
2333 created_at: "2026-08-12T00:00:00Z".into(),
2334 event_frontier: 7,
2335 });
2336 state.sessions.insert(session_id.into(), session);
2337
2338 assert_eq!(
2339 reconcile_managed_checkpoint_archives_in(directory.path(), &state).unwrap(),
2340 1
2341 );
2342 assert!(directory.path().join(referenced_name).exists());
2343 assert!(!directory.path().join(orphan_name).exists());
2344 assert!(directory.path().join(imported_name).exists());
2345 assert!(directory.path().join("notes.hel.zip").exists());
2346 }
2347 #[test]
2348 fn recovery_artifact_final_verification_checks_the_archive_digest() {
2349 let directory = tempfile::tempdir().unwrap();
2350 let session_id = "1123456789abcdef0123456789abcdef";
2351 let metadata = write_checkpoint_gate_archive(directory.path(), session_id, 7);
2352 let mut artifact = CheckpointArtifact {
2353 metadata,
2354 native_session_id: "native-session".into(),
2355 event_frontier_digest: "a".repeat(64),
2356 };
2357
2358 verify_checkpoint_artifact(session_id, &artifact).unwrap();
2359 artifact.metadata.sha256 = "b".repeat(64);
2360 assert!(
2361 verify_checkpoint_artifact(session_id, &artifact)
2362 .unwrap_err()
2363 .to_string()
2364 .contains("checkpoint SHA changed")
2365 );
2366 }
2367 fn checkpoint_barrier_snapshot(cursor: &RelayCursor) -> ManagedSessionSnapshot {
2370 let mut materialized = MaterializedSession::empty("session-1");
2371 materialized.applied_event_ordinal = cursor.ordinal;
2372 materialized.applied_event_digest = cursor.digest.clone();
2373 ManagedSessionSnapshot {
2374 window: hel::hel_state::ProjectionWindow::of(&materialized),
2375 materialized,
2376 latest_credential_sync_signal: None,
2377 worker_build: None,
2378 operational: hel::hel_worker::RelayOperationalState {
2379 activity_turn_started_at_ms: None,
2380 acp_ready: None,
2381 store_id: None,
2382 idle_since_ms: None,
2383 session_id: "session-1".into(),
2384 execution: RelayExecutionState::Idle,
2385 latest_ordinal: cursor.ordinal,
2386 latest_digest: cursor.digest.clone(),
2387 acknowledged_through: cursor.ordinal,
2388 acknowledged_digest: cursor.digest.clone(),
2389 recovery_floor_ordinal: 0,
2390 recovery_floor_digest: hel::hel_worker::RELAY_EVENT_GENESIS_DIGEST.into(),
2391 native_session_id: Some("native-session".into()),
2392 agent_capabilities: None,
2393 agent_info: None,
2394 steering_supported: None,
2395 config_options: Vec::new(),
2396 modes: None,
2397 available_commands: Vec::new(),
2398 config: BTreeMap::new(),
2399 active_prompt: None,
2400 queued_prompts: Vec::new(),
2401 active_user_shells: Vec::new(),
2402 active_agent_terminals: Vec::new(),
2403 checkpoint_barrier: Some("checkpoint-1".into()),
2404 checkpoint_ready: None,
2405 last_acp_activity_at_ms: None,
2406 current_step_started_at_ms: None,
2407 foreground_tool_started_at_ms: None,
2408 harness_turn: None,
2409 last_harness_turn_started_ordinal: None,
2410 background_commands: Vec::new(),
2411 background_work_known: None,
2412 },
2413 }
2414 }
2415 #[test]
2416 fn checkpoint_barrier_is_not_reached_until_its_ready_cursor_is_projected() {
2417 let cursor = RelayCursor {
2418 ordinal: 7,
2419 digest: "a".repeat(64),
2420 };
2421 let mut snapshot = checkpoint_barrier_snapshot(&cursor);
2422
2423 assert!(!checkpoint_barrier_is_ready(&snapshot, "checkpoint-1"));
2424 snapshot.operational.checkpoint_ready = Some(cursor.clone());
2425 assert!(checkpoint_barrier_is_ready(&snapshot, "checkpoint-1"));
2426 validate_checkpoint_barrier_snapshot(&snapshot, "checkpoint-1", &cursor).unwrap();
2427 }
2428 #[test]
2429 fn checkpoint_revalidation_accepts_a_frontier_that_moved_past_the_ready_cursor() {
2430 let cursor = RelayCursor {
2431 ordinal: 7,
2432 digest: "a".repeat(64),
2433 };
2434 let mut snapshot = checkpoint_barrier_snapshot(&cursor);
2435 snapshot.operational.checkpoint_ready = Some(cursor.clone());
2436
2437 snapshot.operational.latest_ordinal = cursor.ordinal + 2;
2441 snapshot.operational.latest_digest = "b".repeat(64);
2442 snapshot.materialized.applied_event_ordinal = cursor.ordinal + 2;
2443 snapshot.materialized.applied_event_digest = "b".repeat(64);
2444 validate_checkpoint_barrier_snapshot(&snapshot, "checkpoint-1", &cursor).unwrap();
2445
2446 snapshot.operational.checkpoint_ready = Some(RelayCursor {
2448 ordinal: cursor.ordinal + 1,
2449 digest: "c".repeat(64),
2450 });
2451 assert!(validate_checkpoint_barrier_snapshot(&snapshot, "checkpoint-1", &cursor).is_err());
2452 snapshot.operational.checkpoint_ready = Some(cursor.clone());
2453 snapshot.operational.checkpoint_barrier = None;
2454 assert!(validate_checkpoint_barrier_snapshot(&snapshot, "checkpoint-1", &cursor).is_err());
2455 }
2456
2457 #[test]
2458 fn routine_kimi_checkpoint_defers_when_background_liveness_is_not_safe() {
2459 let cursor = RelayCursor {
2460 ordinal: 7,
2461 digest: "a".repeat(64),
2462 };
2463 let mut snapshot = checkpoint_barrier_snapshot(&cursor);
2464 snapshot.operational.checkpoint_ready = Some(cursor.clone());
2465
2466 for (known, has_task, label) in [
2467 (Some(false), false, "tracker reported a failure"),
2468 (Some(true), true, "a native task is still active"),
2469 (None, false, "an older worker omitted the tracker field"),
2470 ] {
2471 snapshot.operational.background_work_known = known;
2472 snapshot.operational.background_commands = has_task
2473 .then(|| hel::hel_worker::BackgroundCommand {
2474 id: "kimi:agent-1".into(),
2475 started_at_ms: 1,
2476 command: "background agent".into(),
2477 can_stop: false,
2478 })
2479 .into_iter()
2480 .collect();
2481 let error = validate_automatic_checkpoint_barrier_snapshot(
2482 &snapshot,
2483 "checkpoint-1",
2484 &cursor,
2485 HarnessKind::Kimi,
2486 )
2487 .expect_err(label);
2488 assert!(checkpoint_was_deferred(&error), "{label}: {error:#}");
2489 assert!(!checkpoint_barrier_needs_worker_restart(&error));
2490 }
2491
2492 snapshot.operational.background_work_known = None;
2496 snapshot.operational.background_commands = vec![hel::hel_worker::BackgroundCommand {
2497 id: "legacy-task".into(),
2498 started_at_ms: 1,
2499 command: "legacy background work".into(),
2500 can_stop: false,
2501 }];
2502 validate_automatic_checkpoint_barrier_snapshot(
2503 &snapshot,
2504 "checkpoint-1",
2505 &cursor,
2506 HarnessKind::Codex,
2507 )
2508 .expect("non-Kimi checkpoint compatibility");
2509 }
2510 fn exported_checkpoint_json() -> Vec<u8> {
2512 serde_json::to_vec(&hel::hel_checkpoint::TargetCheckpoint {
2513 path: PathBuf::from("/var/lib/hel/workers/session/checkpoint.hel.zip"),
2514 sha256: "c".repeat(64),
2515 event_frontier: 7,
2516 event_frontier_digest: "d".repeat(64),
2517 timings: None,
2518 })
2519 .unwrap()
2520 }
2521 fn export_spec_fixture() -> CheckpointExportSpec {
2522 CheckpointExportSpec {
2523 protocol_version: CHECKPOINT_EXPORT_PROTOCOL_VERSION,
2524 session: hel::hel_archive::SessionManifest {
2525 id: LATCH_RELAY_SESSION.into(),
2526 title: "streamed spec".into(),
2527 harness_kind: hel::hel_config::HarnessKind::Codex,
2528 profile_id: "codex".into(),
2529 native_session_id: "native-session".into(),
2530 created_at: "2026-08-12T00:00:00Z".into(),
2531 checkpointed_at: "2026-08-16T00:00:00Z".into(),
2532 hel_version: "test".into(),
2533 relay_version: "test".into(),
2534 adapter_version: "acp-v1".into(),
2535 },
2536 target: TargetManifest {
2537 template_id: "podman".into(),
2538 target_kind: "local-podman".into(),
2539 details: BTreeMap::new(),
2540 },
2541 bundle: BundleManifest {
2542 id: "project".into(),
2543 primary_repository: "app".into(),
2544 },
2545 relay_root: PathBuf::from("/var/lib/hel/workers/session"),
2546 harness_home: PathBuf::from("/var/lib/hel/profiles/codex"),
2547 workspace_root: PathBuf::from("/workspace"),
2548 repositories: Vec::new(),
2549 canonical_session: canonical_session_from_materialized(&MaterializedSession::empty(
2550 LATCH_RELAY_SESSION.to_owned(),
2551 ))
2552 .unwrap(),
2553 output_path: PathBuf::from("/var/lib/hel/workers/session/checkpoint.hel.zip"),
2554 }
2555 }
2556 struct ExportExecutor {
2559 streamed_status: i32,
2560 streamed_stderr: String,
2561 retry_stdin_after_failure: bool,
2562 stdin_calls: Cell<usize>,
2563 purposes: RefCell<Vec<String>>,
2564 streamed_spec: RefCell<Vec<u8>>,
2565 }
2566 impl ExportExecutor {
2567 fn new(streamed_status: i32, streamed_stderr: &str) -> Self {
2568 Self {
2569 streamed_status,
2570 streamed_stderr: streamed_stderr.to_owned(),
2571 retry_stdin_after_failure: false,
2572 stdin_calls: Cell::new(0),
2573 purposes: RefCell::new(Vec::new()),
2574 streamed_spec: RefCell::new(Vec::new()),
2575 }
2576 }
2577
2578 fn retry_stdin_after_failure(mut self) -> Self {
2579 self.retry_stdin_after_failure = true;
2580 self
2581 }
2582 }
2583 impl CommandExecutor for ExportExecutor {
2584 fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
2585 self.purposes.borrow_mut().push(command.purpose.clone());
2586 Ok(CommandOutput {
2587 status: 0,
2588 stdout: exported_checkpoint_json(),
2589 stderr: Vec::new(),
2590 })
2591 }
2592
2593 fn execute_with_stdin(
2594 &self,
2595 command: &CommandSpec,
2596 input: &mut (dyn std::io::Read + Send),
2597 ) -> Result<CommandOutput> {
2598 self.purposes.borrow_mut().push(command.purpose.clone());
2599 let mut spec = Vec::new();
2600 input.read_to_end(&mut spec)?;
2601 *self.streamed_spec.borrow_mut() = spec;
2602 let attempt = self.stdin_calls.get();
2603 self.stdin_calls.set(attempt + 1);
2604 let failed =
2605 self.streamed_status != 0 && (attempt == 0 || !self.retry_stdin_after_failure);
2606 Ok(CommandOutput {
2607 status: if failed { self.streamed_status } else { 0 },
2608 stdout: if failed {
2609 Vec::new()
2610 } else {
2611 exported_checkpoint_json()
2612 },
2613 stderr: if failed {
2614 self.streamed_stderr.clone().into_bytes()
2615 } else {
2616 Vec::new()
2617 },
2618 })
2619 }
2620 }
2621 #[test]
2622 fn docker_checkpoint_fallback_upload_uses_docker_cp() {
2623 struct RecordingExecutor {
2624 commands: RefCell<Vec<CommandSpec>>,
2625 }
2626 impl CommandExecutor for RecordingExecutor {
2627 fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
2628 self.commands.borrow_mut().push(command.clone());
2629 Ok(CommandOutput {
2630 status: 0,
2631 stdout: Vec::new(),
2632 stderr: Vec::new(),
2633 })
2634 }
2635 }
2636
2637 let executor = RecordingExecutor {
2638 commands: RefCell::new(Vec::new()),
2639 };
2640 let locator = hel_targets::TargetLocator::LocalDocker {
2641 container_id: "hel-session-12345678".to_owned(),
2642 };
2643 upload_checkpoint_spec(
2644 &executor,
2645 &locator,
2646 LATCH_RELAY_SESSION,
2647 Path::new("checkpoint-spec.json"),
2648 "/var/lib/hel/workers/session/checkpoint-spec.json",
2649 )
2650 .unwrap();
2651
2652 let commands = executor.commands.borrow();
2653 assert_eq!(commands.len(), 1);
2654 assert_eq!(commands[0].program, "docker");
2655 assert_eq!(
2656 commands[0].args,
2657 [
2658 "cp",
2659 "checkpoint-spec.json",
2660 "hel-session-12345678:/var/lib/hel/workers/session/checkpoint-spec.json"
2661 ]
2662 );
2663 assert_eq!(commands[0].purpose, "upload checkpoint specification");
2664 }
2665 #[test]
2666 fn checkpoint_export_streams_its_spec_instead_of_uploading_it() {
2667 let locator = hel_targets::TargetLocator::LocalPodman {
2668 container_id: hel_targets::resource_name(LATCH_RELAY_SESSION).unwrap(),
2669 workspace_storage: Default::default(),
2670 };
2671 let spec = export_spec_fixture();
2672 let executor = ExportExecutor::new(0, "");
2673
2674 let output = export_target_checkpoint(
2675 &executor,
2676 &locator,
2677 LATCH_RELAY_SESSION,
2678 &spec,
2679 "/var/lib/hel/workers/session/checkpoint-spec.json",
2680 )
2681 .unwrap();
2682
2683 assert_eq!(output.stdout, exported_checkpoint_json());
2684 assert_eq!(
2685 serde_json::from_slice::<CheckpointExportSpec>(&executor.streamed_spec.borrow())
2686 .unwrap(),
2687 spec
2688 );
2689 assert_eq!(
2690 executor.purposes.into_inner(),
2691 vec!["export target checkpoint".to_owned()]
2692 );
2693 }
2694 #[test]
2697 fn an_export_that_cannot_read_stdin_falls_back_to_uploading_the_spec() {
2698 let locator = hel_targets::TargetLocator::LocalPodman {
2699 container_id: hel_targets::resource_name(LATCH_RELAY_SESSION).unwrap(),
2700 workspace_storage: Default::default(),
2701 };
2702 let executor = ExportExecutor::new(
2703 1,
2704 "Error: read checkpoint export spec -\n\nCaused by:\n \
2705 No such file or directory (os error 2)\n",
2706 );
2707
2708 let output = export_target_checkpoint(
2709 &executor,
2710 &locator,
2711 LATCH_RELAY_SESSION,
2712 &export_spec_fixture(),
2713 "/var/lib/hel/workers/session/checkpoint-spec.json",
2714 )
2715 .unwrap();
2716
2717 assert_eq!(output.stdout, exported_checkpoint_json());
2718 assert_eq!(
2719 executor.purposes.into_inner(),
2720 vec![
2721 "export target checkpoint".to_owned(),
2722 "upload checkpoint specification".to_owned(),
2723 "export target checkpoint".to_owned(),
2724 ]
2725 );
2726 }
2727 #[test]
2728 fn a_failing_export_is_not_retried_as_an_old_worker() {
2729 let locator = hel_targets::TargetLocator::LocalPodman {
2730 container_id: hel_targets::resource_name(LATCH_RELAY_SESSION).unwrap(),
2731 workspace_storage: Default::default(),
2732 };
2733 let executor = ExportExecutor::new(1, "Error: repository 'app' is missing\n");
2734
2735 let error = export_target_checkpoint(
2736 &executor,
2737 &locator,
2738 LATCH_RELAY_SESSION,
2739 &export_spec_fixture(),
2740 "/var/lib/hel/workers/session/checkpoint-spec.json",
2741 )
2742 .unwrap_err();
2743
2744 assert!(
2745 format!("{error:#}").contains("repository 'app' is missing"),
2746 "{error:#}"
2747 );
2748 assert_eq!(
2749 executor.purposes.into_inner(),
2750 vec!["export target checkpoint".to_owned()]
2751 );
2752 }
2753 #[test]
2756 fn a_legacy_export_worker_is_replaced_before_it_runs_obsolete_behavior() {
2757 let locator = hel_targets::TargetLocator::LocalPodman {
2758 container_id: hel_targets::resource_name(LATCH_RELAY_SESSION).unwrap(),
2759 workspace_storage: Default::default(),
2760 };
2761 let spec = export_spec_fixture();
2762 let executor = ExportExecutor::new(
2763 1,
2764 "Error: parse checkpoint export spec from standard input\n\nCaused by:\n \
2765 unknown field `protocol_version`, expected `session` at line 1 column 20\n",
2766 )
2767 .retry_stdin_after_failure();
2768 let worker_binary = Path::new("/hel-test-worker");
2769
2770 let output = export_target_checkpoint_with_worker(
2771 &executor,
2772 &locator,
2773 LATCH_RELAY_SESSION,
2774 &spec,
2775 "/var/lib/hel/workers/session/checkpoint-spec.json",
2776 Some(worker_binary),
2777 )
2778 .unwrap();
2779
2780 assert_eq!(output.stdout, exported_checkpoint_json());
2781 assert_eq!(
2782 serde_json::from_slice::<CheckpointExportSpec>(&executor.streamed_spec.borrow())
2783 .unwrap(),
2784 spec
2785 );
2786 assert_eq!(
2787 executor.purposes.into_inner(),
2788 vec![
2789 "export target checkpoint".to_owned(),
2790 "stage replacement Mjolnir worker".to_owned(),
2791 "replace installed Mjolnir worker".to_owned(),
2792 "make replaced Mjolnir worker executable".to_owned(),
2793 "export target checkpoint".to_owned(),
2794 ]
2795 );
2796 }
2797 #[test]
2798 fn a_schema_mismatch_after_uploading_the_spec_still_replaces_the_worker_binary() {
2799 let locator = hel_targets::TargetLocator::LocalPodman {
2800 container_id: hel_targets::resource_name(LATCH_RELAY_SESSION).unwrap(),
2801 workspace_storage: Default::default(),
2802 };
2803 struct FileThenRefreshExecutor {
2804 purposes: RefCell<Vec<String>>,
2805 file_export_calls: Cell<usize>,
2806 }
2807 impl CommandExecutor for FileThenRefreshExecutor {
2808 fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
2809 self.purposes.borrow_mut().push(command.purpose.clone());
2810 if command.purpose == "export target checkpoint" {
2811 let attempt = self.file_export_calls.get();
2812 self.file_export_calls.set(attempt + 1);
2813 if attempt == 0 {
2814 return Ok(CommandOutput {
2815 status: 1,
2816 stdout: Vec::new(),
2817 stderr: b"Error: parse checkpoint export spec /spec.json\n\nCaused by:\n unknown variant `terminal_output`, expected one of `user`, `agent`, `thought`, `tool`, `plan`, `system`\n".to_vec(),
2818 });
2819 }
2820 }
2821 Ok(CommandOutput {
2822 status: 0,
2823 stdout: exported_checkpoint_json(),
2824 stderr: Vec::new(),
2825 })
2826 }
2827
2828 fn execute_with_stdin(
2829 &self,
2830 command: &CommandSpec,
2831 input: &mut (dyn std::io::Read + Send),
2832 ) -> Result<CommandOutput> {
2833 self.purposes.borrow_mut().push(command.purpose.clone());
2834 let mut discarded = Vec::new();
2835 input.read_to_end(&mut discarded)?;
2836 let stdin_calls = self
2837 .purposes
2838 .borrow()
2839 .iter()
2840 .filter(|purpose| *purpose == "export target checkpoint")
2841 .count();
2842 if stdin_calls == 1 {
2843 return Ok(CommandOutput {
2844 status: 1,
2845 stdout: Vec::new(),
2846 stderr: b"Error: read checkpoint export spec -\n\nCaused by:\n No such file or directory (os error 2)\n".to_vec(),
2847 });
2848 }
2849 Ok(CommandOutput {
2850 status: 0,
2851 stdout: exported_checkpoint_json(),
2852 stderr: Vec::new(),
2853 })
2854 }
2855 }
2856
2857 let executor = FileThenRefreshExecutor {
2858 purposes: RefCell::new(Vec::new()),
2859 file_export_calls: Cell::new(0),
2860 };
2861 let output = export_target_checkpoint_with_worker(
2862 &executor,
2863 &locator,
2864 LATCH_RELAY_SESSION,
2865 &export_spec_fixture(),
2866 "/var/lib/hel/workers/session/checkpoint-spec.json",
2867 Some(Path::new("/hel-test-worker")),
2868 )
2869 .unwrap();
2870
2871 assert_eq!(output.stdout, exported_checkpoint_json());
2872 assert_eq!(
2873 executor.purposes.into_inner(),
2874 vec![
2875 "export target checkpoint".to_owned(),
2876 "upload checkpoint specification".to_owned(),
2877 "export target checkpoint".to_owned(),
2878 "stage replacement Mjolnir worker".to_owned(),
2879 "replace installed Mjolnir worker".to_owned(),
2880 "make replaced Mjolnir worker executable".to_owned(),
2881 "export target checkpoint".to_owned(),
2882 ]
2883 );
2884 }
2885 #[test]
2889 fn a_working_session_defers_but_close_waits_for_cancellation_before_recovery() {
2890 let cursor = RelayCursor {
2891 ordinal: 7,
2892 digest: "a".repeat(64),
2893 };
2894 let mut snapshot = checkpoint_barrier_snapshot(&cursor);
2895 snapshot.operational.execution = RelayExecutionState::Running;
2896
2897 let deferred = checkpoint_barrier_wait_ended(
2898 &snapshot,
2899 "checkpoint-1",
2900 BarrierBusyPolicy::DeferWhileRunning,
2901 false,
2902 false,
2903 )
2904 .expect("a working session ends the wait at once");
2905 assert!(checkpoint_was_deferred(&deferred), "{deferred:#}");
2906 assert!(
2907 !checkpoint_barrier_needs_worker_restart(&deferred),
2908 "a deferred copy must never restart the worker: {deferred:#}"
2909 );
2910 assert_eq!(
2911 BarrierBusyPolicy::of(LatchExclusivity::HoldThroughClose),
2912 BarrierBusyPolicy::InterruptWhileRunning
2913 );
2914
2915 assert!(
2918 checkpoint_barrier_wait_ended(
2919 &snapshot,
2920 "checkpoint-1",
2921 BarrierBusyPolicy::InterruptWhileRunning,
2922 false,
2923 false,
2924 )
2925 .is_none()
2926 );
2927 let interrupted = checkpoint_barrier_wait_ended(
2928 &snapshot,
2929 "checkpoint-1",
2930 BarrierBusyPolicy::InterruptWhileRunning,
2931 true,
2932 true,
2933 )
2934 .expect("an unresponsive cancellation ends the wait at the deadline");
2935 assert!(
2936 checkpoint_barrier_needs_worker_restart(&interrupted),
2937 "{interrupted:#}"
2938 );
2939 assert!(!checkpoint_was_deferred(&interrupted), "{interrupted:#}");
2940
2941 snapshot.operational.execution = RelayExecutionState::Idle;
2944 let wedged = checkpoint_barrier_wait_ended(
2945 &snapshot,
2946 "checkpoint-1",
2947 BarrierBusyPolicy::DeferWhileRunning,
2948 true,
2949 false,
2950 )
2951 .expect("the deadline ends the wait");
2952 assert!(
2953 checkpoint_barrier_needs_worker_restart(&wedged),
2954 "{wedged:#}"
2955 );
2956 assert!(!checkpoint_was_deferred(&wedged), "{wedged:#}");
2957 }
2958
2959 #[test]
2962 fn a_frontier_that_moved_before_the_latch_defers_the_checkpoint() {
2963 let cursor = RelayCursor {
2964 ordinal: 220,
2965 digest: "a".repeat(64),
2966 };
2967 ensure_exact_checkpoint_cut(&cursor, cursor.ordinal, &cursor.digest)
2968 .expect("a projection latched at the ready cursor is an exact cut");
2969
2970 for (ordinal, digest) in [(223, "a".repeat(64)), (220, "b".repeat(64))] {
2971 let error = ensure_exact_checkpoint_cut(&cursor, ordinal, &digest)
2972 .expect_err("a projection past the ready cursor is not an exact cut");
2973 assert!(checkpoint_was_deferred(&error), "{error:#}");
2974 assert!(
2975 !checkpoint_barrier_needs_worker_restart(&error),
2976 "{error:#}"
2977 );
2978 }
2979 }
2980
2981 #[test]
2985 fn a_harness_turn_started_during_capture_abandons_the_archive() {
2986 let cursor = RelayCursor {
2987 ordinal: 220,
2988 digest: "a".repeat(64),
2989 };
2990 let mut snapshot = checkpoint_barrier_snapshot(&cursor);
2991 snapshot.operational.checkpoint_ready = Some(cursor.clone());
2992
2993 snapshot.operational.last_harness_turn_started_ordinal = Some(cursor.ordinal);
2994 validate_checkpoint_barrier_snapshot(&snapshot, "checkpoint-1", &cursor)
2995 .expect("a turn that started at or before the cursor is covered by the archive");
2996
2997 snapshot.operational.last_harness_turn_started_ordinal = Some(cursor.ordinal + 1);
2998 let error = validate_checkpoint_barrier_snapshot(&snapshot, "checkpoint-1", &cursor)
2999 .expect_err("a turn that started after the cursor invalidates the capture");
3000 assert!(checkpoint_was_deferred(&error), "{error:#}");
3001 }
3002
3003 #[test]
3004 fn a_stuck_checkpoint_barrier_is_retried_by_restarting_the_worker() {
3005 for failure in [
3008 CheckpointBarrierUnreachable::not_admitted(
3009 "checkpoint-976f6746887c5ccd93b9d8bbe120ef06",
3010 ),
3011 CheckpointBarrierUnreachable::runtime_stopped(),
3012 ] {
3013 let error = anyhow::Error::new(failure).context("latch a session checkpoint");
3014 assert!(checkpoint_barrier_needs_worker_restart(&error), "{error:#}");
3015 }
3016 assert!(!checkpoint_barrier_needs_worker_restart(&anyhow::anyhow!(
3017 "export target checkpoint failed with status 1"
3018 )));
3019 assert!(!checkpoint_barrier_needs_worker_restart(&anyhow::anyhow!(
3022 "ACP relay did not reach checkpoint barrier checkpoint-1"
3023 )));
3024 }
3025
3026 #[test]
3027 fn an_incompatible_cancel_turn_requests_worker_recovery() {
3028 let error = anyhow::Error::new(RelayRejected(hel::hel_worker::RelayProtocolError {
3029 code: hel::hel_worker::RelayErrorCode::IncompatibleProtocol,
3030 message: "request uses protocol 6".into(),
3031 retryable: false,
3032 detail: None,
3033 }))
3034 .context("cancel active ACP turn before checkpoint barrier");
3035 assert!(
3036 checkpoint_cancel_turn_needs_worker_restart(&error),
3037 "{error:#}"
3038 );
3039 assert!(checkpoint_barrier_needs_worker_restart(&error.context(
3040 CheckpointBarrierUnreachable::cancel_turn_unavailable("checkpoint-1", 6,)
3041 )));
3042 }
3043 #[test]
3044 fn a_dead_worker_hello_failure_is_retried_by_restarting_the_worker() {
3045 let dead = anyhow::Error::new(RelayTransportDead::new("the proxy is gone"))
3046 .context("connect to the session worker for checkpoint");
3047 assert!(worker_connect_needs_restart(&dead), "{dead:#}");
3048 assert!(!worker_connect_needs_restart(&anyhow::anyhow!(
3049 "unknown session"
3050 )));
3051 }
3052 #[cfg(unix)]
3053 #[tokio::test]
3054 async fn checkpoint_restart_stop_failure_names_mjolnir() {
3055 struct FailingStop;
3056
3057 impl CommandExecutor for FailingStop {
3058 fn execute(&self, _command: &CommandSpec) -> Result<CommandOutput> {
3059 Ok(CommandOutput {
3060 status: 1,
3061 stdout: Vec::new(),
3062 stderr: b"permission denied".to_vec(),
3063 })
3064 }
3065 }
3066
3067 let session_id = "0123456789abcdef0123456789abcdef";
3068 let worker_root = format!("/tmp/mjolnir-checkpoint-test/{session_id}");
3069 let backend = hel_targets::TargetLocator::LocalBare {
3070 worker_root: worker_root.clone(),
3071 };
3072 let controller = Controller {
3073 config: HelConfig::default(),
3074 state: HelState::default(),
3075 };
3076 let reconnect = CommandSpec::new("unused", std::iter::empty::<&str>());
3077
3078 let result = controller
3079 .restart_worker_for_checkpoint(
3080 session_id,
3081 &FailingStop,
3082 &backend,
3083 &worker_root,
3084 &reconnect,
3085 )
3086 .await;
3087 let error = match result {
3088 Ok(_) => panic!("a failed worker stop unexpectedly restarted the checkpoint worker"),
3089 Err(error) => error,
3090 };
3091 let detail = format!("{error:#}");
3092 assert!(
3093 detail.starts_with("stop wedged Mjolnir worker before retrying checkpoint"),
3094 "{detail}"
3095 );
3096 assert!(detail.contains("permission denied"), "{detail}");
3097 }
3098 #[test]
3099 fn export_spec_schema_mismatch_is_detected_from_the_parse_error() {
3100 assert!(export_spec_schema_unsupported(
3101 "Error: parse checkpoint export spec from standard input\n\nCaused by:\n \
3102 unknown field `terminal_refs`, expected `call` at line 1 column 7276552\n"
3103 ));
3104 assert!(export_spec_schema_unsupported(
3105 "Error: parse checkpoint export spec /spec.json\n\nCaused by:\n \
3106 unknown variant `terminal_output`, expected one of `user`, `agent`\n"
3107 ));
3108 assert!(!export_spec_schema_unsupported(
3109 "Error: repository 'app' is missing\n"
3110 ));
3111 assert!(!export_spec_schema_unsupported(
3112 "Error: parse checkpoint export spec from standard input\n\nCaused by:\n \
3113 missing field `relay_root`\n"
3114 ));
3115 assert!(export_protocol_unsupported(
3116 "Error: unsupported checkpoint export protocol version 3; worker supports 2\n"
3117 ));
3118 }
3119 const LATCH_RELAY_ROOT: &str = "MJ_TEST_LATCH_RELAY_ROOT";
3120 const LATCH_RELAY_STARTS: &str = "MJ_TEST_LATCH_RELAY_STARTS";
3121 const LATCH_RELAY_REJECT_RELEASE: &str = "MJ_TEST_LATCH_REJECT_RELEASE";
3122 #[cfg(unix)]
3123 const LATCH_RELAY_RUNNING: &str = "MJ_TEST_LATCH_RELAY_RUNNING";
3124 #[cfg(unix)]
3125 const LATCH_TEST_CHILD: &str = "MJ_TEST_LATCH_CHILD";
3126 #[cfg(unix)]
3127 const ABANDON_TEST_CHILD: &str = "MJ_TEST_ABANDON_LATCH_CHILD";
3128 #[cfg(unix)]
3129 const RELEASE_TEST_CHILD: &str = "MJ_TEST_RELEASE_LATCH_CHILD";
3130 #[cfg(unix)]
3131 const LEGACY_RELEASE_TEST_CHILD: &str = "MJ_TEST_LEGACY_RELEASE_LATCH_CHILD";
3132 #[cfg(unix)]
3133 const REUSE_TEST_CHILD: &str = "MJ_TEST_REUSE_LATCH_CHILD";
3134 const LATCH_RELAY_STARTUP_DELAY_MS: &str = "MJ_TEST_LATCH_STARTUP_DELAY_MS";
3135 const LATCH_RELAY_SESSION: &str = "018f9dd2-a3b4-7c8d-9000-0123456789ab";
3136 #[cfg(unix)]
3138 #[derive(Clone, Copy, PartialEq, Eq)]
3139 enum ReleaseSupport {
3140 Supported,
3141 Rejected,
3144 }
3145 #[test]
3152 fn latch_relay_child_serves_stdio() {
3153 let Some(root) = std::env::var_os(LATCH_RELAY_ROOT) else {
3154 return;
3155 };
3156 println!();
3160 if let Some(starts) = std::env::var_os(LATCH_RELAY_STARTS) {
3163 use std::io::Write;
3164 let mut log = OpenOptions::new()
3165 .create(true)
3166 .append(true)
3167 .open(starts)
3168 .expect("open the relay start log");
3169 writeln!(log, "{}", std::process::id()).expect("record this relay start");
3170 }
3171 let mut relay =
3172 hel::hel_worker::DurableRelay::open(Path::new(&root), LATCH_RELAY_SESSION, "1.0.0")
3173 .expect("open the test relay journal");
3174 if relay.operational_state().native_session_id.is_none() {
3175 relay
3176 .record_observation(hel::hel_worker::RelayObservation::SessionOpened {
3177 native_session_id: "native-session".into(),
3178 resumed: true,
3179 })
3180 .unwrap();
3181 }
3182 let ready_at = Instant::now()
3183 + Duration::from_millis(
3184 std::env::var(LATCH_RELAY_STARTUP_DELAY_MS)
3185 .ok()
3186 .map(|value| value.parse::<u64>().unwrap())
3187 .unwrap_or(0),
3188 );
3189 let reject_release = std::env::var_os(LATCH_RELAY_REJECT_RELEASE).is_some();
3190 #[cfg(unix)]
3191 let running = std::env::var_os(LATCH_RELAY_RUNNING).is_some();
3192 #[cfg(unix)]
3193 if running && relay.operational_state().active_prompt.is_none() {
3194 let response = relay.handle(hel::hel_worker::RelayRequestEnvelope {
3195 request_id: "seed-running-request".into(),
3196 protocol_version: hel::hel_worker::RELAY_PROTOCOL_VERSION,
3197 request: hel::hel_worker::RelayRequest::Submit {
3198 command_id: "seed-running-prompt".into(),
3199 command: RelayCommand::Prompt {
3200 prompt: vec![ContentBlock::Text(TextContent::new("running"))],
3201 },
3202 },
3203 });
3204 assert!(matches!(
3205 response.body,
3206 hel::hel_worker::RelayResponseBody::Ok {
3207 payload: hel::hel_worker::RelayResponsePayload::Accepted { .. }
3208 }
3209 ));
3210 let claimed = relay
3211 .claim_pending_commands(true)
3212 .expect("seed the running prompt");
3213 assert_eq!(claimed.len(), 1);
3214 assert_eq!(claimed[0].command_id, "seed-running-prompt");
3215 }
3216 let mut reader = std::io::stdin().lock();
3217 let mut writer = std::io::stdout().lock();
3218 let mut configured = false;
3219 while let Some(request) =
3220 hel::hel_worker::read_relay_frame(&mut reader).expect("read a relay request")
3221 {
3222 if !configured && Instant::now() >= ready_at {
3223 relay
3224 .record_observation(hel::hel_worker::RelayObservation::SessionConfigured {
3225 config_options: Vec::new(),
3226 })
3227 .unwrap();
3228 configured = true;
3229 }
3230 if matches!(
3231 &request.request,
3232 hel::hel_worker::RelayRequest::Submit {
3233 command: RelayCommand::BeginCheckpoint { .. },
3234 ..
3235 }
3236 ) {
3237 assert!(
3238 relay.operational_state().native_session_is_ready(),
3239 "checkpoint submitted before current ACP startup finished"
3240 );
3241 }
3242 let response = if reject_release && requests_checkpoint_release(&request) {
3243 unparseable_request_response(&request)
3244 } else {
3245 relay.handle(request)
3246 };
3247 hel::hel_worker::write_relay_frame(&mut writer, &response)
3248 .expect("answer a relay request");
3249 for claimed in relay
3250 .claim_pending_commands(true)
3251 .expect("claim relay commands")
3252 {
3253 match claimed.command {
3254 RelayCommand::BeginCheckpoint { .. } => {
3255 relay
3256 .record_checkpoint_ready(&claimed.command_id)
3257 .expect("report the checkpoint barrier ready");
3258 }
3259 #[cfg(unix)]
3260 RelayCommand::CancelTurn => {
3261 let prompt_id = relay
3262 .operational_state()
3263 .active_prompt
3264 .as_ref()
3265 .map(|prompt| prompt.command_id.clone())
3266 .expect("a prompt to cancel");
3267 relay
3268 .record_command_completed(
3269 &claimed.command_id,
3270 RelayCommandOutcome::Cancelled,
3271 )
3272 .expect("complete the cancellation");
3273 relay
3274 .record_command_completed(
3275 &prompt_id,
3276 RelayCommandOutcome::Prompt {
3277 stop_reason: "cancelled".into(),
3278 },
3279 )
3280 .expect("complete the cancelled prompt");
3281 }
3282 _ => {}
3283 }
3284 }
3285 }
3286 }
3287 fn requests_checkpoint_release(request: &hel::hel_worker::RelayRequestEnvelope) -> bool {
3288 matches!(
3289 &request.request,
3290 hel::hel_worker::RelayRequest::Submit {
3291 command: RelayCommand::ReleaseCheckpoint { .. },
3292 ..
3293 }
3294 )
3295 }
3296 fn unparseable_request_response(
3300 request: &hel::hel_worker::RelayRequestEnvelope,
3301 ) -> hel::hel_worker::RelayResponseEnvelope {
3302 hel::hel_worker::RelayResponseEnvelope {
3303 request_id: request.request_id.clone(),
3304 protocol_version: request.protocol_version,
3305 body: hel::hel_worker::RelayResponseBody::Error {
3306 error: hel::hel_worker::RelayProtocolError {
3307 code: hel::hel_worker::RelayErrorCode::InvalidRequest,
3308 message: "unknown variant `release_checkpoint`".into(),
3309 retryable: false,
3310 detail: None,
3311 },
3312 },
3313 }
3314 }
3315 #[cfg(unix)]
3318 fn latch_relay_target(
3319 relay_root: &Path,
3320 starts: Option<&Path>,
3321 release: ReleaseSupport,
3322 running: bool,
3323 ) -> crate::hel_session_manager::RelaySessionTarget {
3324 let script = format!(
3327 "\"$0\" --exact {}::latch_relay_child_serves_stdio --nocapture | \
3328 grep --line-buffered '^{{'",
3329 module_path!()
3330 .strip_prefix("mj_controller::")
3331 .unwrap_or(module_path!())
3332 );
3333 let mut spec = CommandSpec::new(
3334 "sh",
3335 [
3336 "-c".to_owned(),
3337 script,
3338 std::env::current_exe()
3339 .unwrap()
3340 .to_string_lossy()
3341 .into_owned(),
3342 ],
3343 )
3344 .purpose("test latch relay");
3345 spec.env.insert(
3346 LATCH_RELAY_ROOT.to_owned(),
3347 relay_root.to_string_lossy().into_owned(),
3348 );
3349 if let Some(starts) = starts {
3350 spec.env.insert(
3351 LATCH_RELAY_STARTS.to_owned(),
3352 starts.to_string_lossy().into_owned(),
3353 );
3354 }
3355 if release == ReleaseSupport::Rejected {
3356 spec.env
3357 .insert(LATCH_RELAY_REJECT_RELEASE.to_owned(), "1".to_owned());
3358 }
3359 if running {
3360 spec.env
3361 .insert(LATCH_RELAY_RUNNING.to_owned(), "1".to_owned());
3362 }
3363 crate::hel_session_manager::RelaySessionTarget {
3364 session_id: LATCH_RELAY_SESSION.to_owned(),
3365 spec,
3366 worker_recovery: None,
3367 project_memory: None,
3368 }
3369 }
3370 #[cfg(unix)]
3373 async fn latch_a_live_checkpoint(
3374 relay_root: &Path,
3375 starts: Option<&Path>,
3376 release: ReleaseSupport,
3377 running: bool,
3378 ) -> (
3379 crate::hel_session_manager::SessionManagerChannels,
3380 ManagedSessionHandle,
3381 ControllerRelayLease,
3382 String,
3383 RelayCursor,
3384 ) {
3385 hel::hel_database::save_session(&checkpoint_test_session(LATCH_RELAY_SESSION)).unwrap();
3388 let channels = crate::hel_session_manager::spawn_session_manager().unwrap();
3389 channels
3390 .targets
3391 .send(vec![latch_relay_target(
3392 relay_root, starts, release, running,
3393 )])
3394 .unwrap();
3395 let handle = channels
3396 .control
3397 .wait_for_session(LATCH_RELAY_SESSION, Duration::from_secs(10))
3398 .await
3399 .unwrap();
3400
3401 let lease = handle.lease_connection().await.unwrap();
3402 let mut relay = ControllerRelayLease::Managed {
3403 handle: handle.clone(),
3404 lease: Some(lease),
3405 };
3406 let barrier_command_id = new_command_id("checkpoint").unwrap();
3407 let connection = relay.connection_mut();
3408 connection
3409 .submit(
3410 barrier_command_id.clone(),
3411 RelayCommand::BeginCheckpoint { reason: None },
3412 )
3413 .await
3414 .unwrap();
3415 let barrier = wait_for_checkpoint_barrier(
3416 connection,
3417 LATCH_RELAY_SESSION,
3418 &barrier_command_id,
3419 CHECKPOINT_BARRIER_TIMEOUT,
3420 BarrierBusyPolicy::InterruptWhileRunning,
3421 HarnessKind::Codex,
3422 )
3423 .await
3424 .unwrap();
3425 assert_eq!(
3426 barrier.materialized.applied_event_ordinal,
3427 barrier.operational.latest_ordinal
3428 );
3429 let cursor = barrier.operational.checkpoint_ready.clone().unwrap();
3430 (channels, handle, relay, barrier_command_id, cursor)
3431 }
3432
3433 #[cfg(unix)]
3438 #[tokio::test]
3439 async fn a_close_checkpoint_cancels_a_running_turn_without_restarting_the_worker() {
3440 if std::env::var_os(LATCH_TEST_CHILD).is_none() {
3443 let directory = tempfile::tempdir().unwrap();
3444 let test_name = format!(
3445 "{}::a_close_checkpoint_cancels_a_running_turn_without_restarting_the_worker",
3446 module_path!()
3447 .strip_prefix("mj_controller::")
3448 .unwrap_or(module_path!())
3449 );
3450 let output = Command::new(std::env::current_exe().unwrap())
3451 .args(["--exact", &test_name, "--nocapture"])
3452 .env(LATCH_TEST_CHILD, "1")
3453 .env("MJ_DATA_DIR", directory.path())
3454 .output()
3455 .unwrap();
3456 assert!(
3457 output.status.success(),
3458 "isolated cancellation checkpoint test failed\nstdout:\n{}\nstderr:\n{}",
3459 String::from_utf8_lossy(&output.stdout),
3460 String::from_utf8_lossy(&output.stderr)
3461 );
3462 return;
3463 }
3464 let _writer = hel::hel_database::install_isolated_test_writer();
3465 let relay_root = tempfile::tempdir().unwrap();
3466 let start_log_directory = tempfile::tempdir().unwrap();
3467 let start_log = start_log_directory.path().join("relay-starts");
3468 let (_channels, _handle, mut relay, _barrier_command_id, _cursor) =
3469 latch_a_live_checkpoint(
3470 relay_root.path(),
3471 Some(&start_log),
3472 ReleaseSupport::Supported,
3473 true,
3474 )
3475 .await;
3476 let snapshot = relay.sync_snapshot().await.unwrap();
3477 assert_eq!(
3478 snapshot.operational.execution,
3479 RelayExecutionState::Idle,
3480 "the close wait returned before the cancelled turn became idle"
3481 );
3482 assert!(
3483 snapshot.operational.active_prompt.is_none(),
3484 "the close wait returned before the cancelled prompt settled"
3485 );
3486 assert_eq!(
3487 relay_starts(&start_log),
3488 1,
3489 "responsive cancellation restarted worker"
3490 );
3491 }
3492 #[cfg(unix)]
3495 async fn wait_until_the_actor_serves_again(handle: &ManagedSessionHandle) {
3496 for attempt in 0.. {
3497 if handle.sync_now().await.is_ok() {
3498 return;
3499 }
3500 assert!(attempt < 200, "the actor never took its connection back");
3501 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
3502 }
3503 }
3504 #[cfg(unix)]
3508 #[tokio::test]
3509 async fn ending_the_checkpoint_latch_returns_the_connection_to_its_actor() {
3510 if std::env::var_os(LATCH_TEST_CHILD).is_none() {
3513 let directory = tempfile::tempdir().unwrap();
3514 let test_name = format!(
3515 "{}::ending_the_checkpoint_latch_returns_the_connection_to_its_actor",
3516 module_path!()
3517 .strip_prefix("mj_controller::")
3518 .unwrap_or(module_path!())
3519 );
3520 let output = Command::new(std::env::current_exe().unwrap())
3521 .args(["--exact", &test_name, "--nocapture"])
3522 .env(LATCH_TEST_CHILD, "1")
3523 .env("MJ_DATA_DIR", directory.path())
3524 .output()
3525 .unwrap();
3526 assert!(
3527 output.status.success(),
3528 "isolated checkpoint latch test failed\nstdout:\n{}\nstderr:\n{}",
3529 String::from_utf8_lossy(&output.stdout),
3530 String::from_utf8_lossy(&output.stderr)
3531 );
3532 return;
3533 }
3534 let _writer = hel::hel_database::install_isolated_test_writer();
3536
3537 std::thread::spawn(|| {
3540 std::thread::sleep(std::time::Duration::from_secs(120));
3541 eprintln!("the checkpoint latch never returned its connection");
3542 std::process::exit(101);
3543 });
3544
3545 let relay_root = tempfile::tempdir().unwrap();
3546 let (_channels, handle, mut relay, barrier_command_id, cursor) =
3547 latch_a_live_checkpoint(relay_root.path(), None, ReleaseSupport::Supported, false)
3548 .await;
3549
3550 assert!(
3553 handle.sync_now().await.is_err(),
3554 "a latched projection must not be advanced by its own actor"
3555 );
3556
3557 relay.end_latch();
3558 wait_until_the_actor_serves_again(&handle).await;
3559
3560 let latched = relay.sync_snapshot().await.unwrap();
3564 validate_checkpoint_barrier_snapshot(&latched, &barrier_command_id, &cursor).unwrap();
3565
3566 let prompt_ordinal = relay
3569 .submit(
3570 new_command_id("prompt").unwrap(),
3571 RelayCommand::Prompt {
3572 prompt: vec![ContentBlock::Text(TextContent::new("hello"))],
3573 },
3574 )
3575 .await
3576 .unwrap();
3577 assert!(prompt_ordinal > cursor.ordinal);
3578 let snapshot = relay.sync_snapshot().await.unwrap();
3579 assert!(snapshot.operational.latest_ordinal > cursor.ordinal);
3580 validate_checkpoint_barrier_snapshot(&snapshot, &barrier_command_id, &cursor).unwrap();
3581
3582 latched_checkpoint(
3583 relay,
3584 barrier_command_id,
3585 cursor,
3586 CheckpointCompletion::HeldBarrier,
3587 )
3588 .complete()
3589 .await
3590 .unwrap();
3591 handle.sync_now().await.unwrap();
3592 assert_eq!(
3593 handle
3594 .view()
3595 .snapshot
3596 .expect("the actor published the completed barrier")
3597 .operational
3598 .checkpoint_barrier,
3599 None
3600 );
3601 }
3602 #[cfg(unix)]
3606 #[tokio::test]
3607 async fn releasing_a_checkpoint_after_capture_defers_only_the_recovery_floor() {
3608 if std::env::var_os(RELEASE_TEST_CHILD).is_none() {
3611 let directory = tempfile::tempdir().unwrap();
3612 let test_name = format!(
3613 "{}::releasing_a_checkpoint_after_capture_defers_only_the_recovery_floor",
3614 module_path!()
3615 .strip_prefix("mj_controller::")
3616 .unwrap_or(module_path!())
3617 );
3618 let output = Command::new(std::env::current_exe().unwrap())
3619 .args(["--exact", &test_name, "--nocapture"])
3620 .env(RELEASE_TEST_CHILD, "1")
3621 .env("MJ_DATA_DIR", directory.path())
3622 .output()
3623 .unwrap();
3624 assert!(
3625 output.status.success(),
3626 "isolated checkpoint release test failed\nstdout:\n{}\nstderr:\n{}",
3627 String::from_utf8_lossy(&output.stdout),
3628 String::from_utf8_lossy(&output.stderr)
3629 );
3630 return;
3631 }
3632 let _writer = hel::hel_database::install_isolated_test_writer();
3634
3635 std::thread::spawn(|| {
3638 std::thread::sleep(std::time::Duration::from_secs(120));
3639 eprintln!("the captured checkpoint never released its barrier");
3640 std::process::exit(101);
3641 });
3642
3643 let relay_root = tempfile::tempdir().unwrap();
3644 let (_channels, handle, mut relay, barrier_command_id, cursor) =
3645 latch_a_live_checkpoint(relay_root.path(), None, ReleaseSupport::Supported, false)
3646 .await;
3647 relay.end_latch();
3648 wait_until_the_actor_serves_again(&handle).await;
3649
3650 let completion = release_checkpoint_after_capture(
3653 &mut relay,
3654 LATCH_RELAY_SESSION,
3655 &barrier_command_id,
3656 &cursor,
3657 HarnessKind::Codex,
3658 )
3659 .await
3660 .unwrap();
3661 assert_eq!(completion, CheckpointCompletion::ReleasedAfterCapture);
3662 let released = relay.sync_snapshot().await.unwrap();
3663 assert_eq!(released.operational.checkpoint_barrier, None);
3664 assert_eq!(released.operational.checkpoint_ready, None);
3665 assert_eq!(
3666 released.operational.recovery_floor_ordinal, 0,
3667 "an exported archive that is not installed may not release journal history"
3668 );
3669
3670 relay
3673 .submit(
3674 new_command_id("prompt").unwrap(),
3675 RelayCommand::Prompt {
3676 prompt: vec![ContentBlock::Text(TextContent::new("during transfer"))],
3677 },
3678 )
3679 .await
3680 .unwrap();
3681 let mut dispatched = None;
3682 for attempt in 0.. {
3683 let snapshot = relay.sync_snapshot().await.unwrap();
3684 if let Some(active) = snapshot.operational.active_prompt {
3685 dispatched = Some(active);
3686 break;
3687 }
3688 assert!(attempt < 200, "a released barrier still froze ACP dispatch");
3689 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
3690 }
3691 assert!(dispatched.is_some());
3692
3693 latched_checkpoint(
3696 relay,
3697 barrier_command_id,
3698 cursor.clone(),
3699 CheckpointCompletion::ReleasedAfterCapture,
3700 )
3701 .complete()
3702 .await
3703 .unwrap();
3704 handle.sync_now().await.unwrap();
3705 let installed = handle
3706 .view()
3707 .snapshot
3708 .expect("the actor published the advanced recovery floor");
3709 assert_eq!(installed.operational.recovery_floor_ordinal, cursor.ordinal);
3710 assert_eq!(installed.operational.recovery_floor_digest, cursor.digest);
3711 }
3712 #[cfg(unix)]
3715 #[tokio::test]
3716 async fn a_worker_that_rejects_the_release_keeps_its_barrier_through_the_transfer() {
3717 if std::env::var_os(LEGACY_RELEASE_TEST_CHILD).is_none() {
3720 let directory = tempfile::tempdir().unwrap();
3721 let test_name = format!(
3722 "{}::a_worker_that_rejects_the_release_keeps_its_barrier_through_the_transfer",
3723 module_path!()
3724 .strip_prefix("mj_controller::")
3725 .unwrap_or(module_path!())
3726 );
3727 let output = Command::new(std::env::current_exe().unwrap())
3728 .args(["--exact", &test_name, "--nocapture"])
3729 .env(LEGACY_RELEASE_TEST_CHILD, "1")
3730 .env("MJ_DATA_DIR", directory.path())
3731 .output()
3732 .unwrap();
3733 assert!(
3734 output.status.success(),
3735 "isolated legacy checkpoint release test failed\nstdout:\n{}\nstderr:\n{}",
3736 String::from_utf8_lossy(&output.stdout),
3737 String::from_utf8_lossy(&output.stderr)
3738 );
3739 return;
3740 }
3741 let _writer = hel::hel_database::install_isolated_test_writer();
3743
3744 std::thread::spawn(|| {
3747 std::thread::sleep(std::time::Duration::from_secs(120));
3748 eprintln!("the rejected release never finished its checkpoint");
3749 std::process::exit(101);
3750 });
3751
3752 let relay_root = tempfile::tempdir().unwrap();
3753 let start_log = tempfile::tempdir().unwrap();
3754 let start_log = start_log.path().join("relay-starts");
3755 let (_channels, handle, mut relay, barrier_command_id, cursor) = latch_a_live_checkpoint(
3756 relay_root.path(),
3757 Some(&start_log),
3758 ReleaseSupport::Rejected,
3759 false,
3760 )
3761 .await;
3762 relay.end_latch();
3763 wait_until_the_actor_serves_again(&handle).await;
3764
3765 let completion = release_checkpoint_after_capture(
3766 &mut relay,
3767 LATCH_RELAY_SESSION,
3768 &barrier_command_id,
3769 &cursor,
3770 HarnessKind::Codex,
3771 )
3772 .await
3773 .unwrap();
3774 assert_eq!(completion, CheckpointCompletion::HeldBarrier);
3775 assert_eq!(relay_starts(&start_log), 1);
3778
3779 let transferring = relay.sync_snapshot().await.unwrap();
3783 validate_checkpoint_barrier_snapshot(&transferring, &barrier_command_id, &cursor).unwrap();
3784 latched_checkpoint(relay, barrier_command_id, cursor.clone(), completion)
3785 .complete()
3786 .await
3787 .unwrap();
3788 handle.sync_now().await.unwrap();
3789 let completed = handle
3790 .view()
3791 .snapshot
3792 .expect("the actor published the completed barrier");
3793 assert_eq!(completed.operational.checkpoint_barrier, None);
3794 assert_eq!(completed.operational.recovery_floor_ordinal, cursor.ordinal);
3795 }
3796 #[cfg(unix)]
3801 #[tokio::test]
3802 async fn abandoning_a_latched_checkpoint_drops_the_connection_that_opened_its_barrier() {
3803 if std::env::var_os(ABANDON_TEST_CHILD).is_none() {
3806 let directory = tempfile::tempdir().unwrap();
3807 let test_name = format!(
3808 "{}::abandoning_a_latched_checkpoint_drops_the_connection_that_opened_its_barrier",
3809 module_path!()
3810 .strip_prefix("mj_controller::")
3811 .unwrap_or(module_path!())
3812 );
3813 let output = Command::new(std::env::current_exe().unwrap())
3814 .args(["--exact", &test_name, "--nocapture"])
3815 .env(ABANDON_TEST_CHILD, "1")
3816 .env("MJ_DATA_DIR", directory.path())
3817 .output()
3818 .unwrap();
3819 assert!(
3820 output.status.success(),
3821 "isolated abandoned checkpoint test failed\nstdout:\n{}\nstderr:\n{}",
3822 String::from_utf8_lossy(&output.stdout),
3823 String::from_utf8_lossy(&output.stderr)
3824 );
3825 return;
3826 }
3827 let _writer = hel::hel_database::install_isolated_test_writer();
3829
3830 std::thread::spawn(|| {
3833 std::thread::sleep(std::time::Duration::from_secs(120));
3834 eprintln!("an abandoned checkpoint never released its relay connection");
3835 std::process::exit(101);
3836 });
3837
3838 let relay_root = tempfile::tempdir().unwrap();
3839 let start_log = tempfile::tempdir().unwrap();
3840 let start_log = start_log.path().join("relay-starts");
3841 let (_channels, handle, mut relay, barrier_command_id, cursor) = latch_a_live_checkpoint(
3842 relay_root.path(),
3843 Some(&start_log),
3844 ReleaseSupport::Supported,
3845 false,
3846 )
3847 .await;
3848 relay.end_latch();
3849 wait_until_the_actor_serves_again(&handle).await;
3850 assert_eq!(relay_starts(&start_log), 1);
3851
3852 latched_checkpoint(
3853 relay,
3854 barrier_command_id,
3855 cursor,
3856 CheckpointCompletion::HeldBarrier,
3857 )
3858 .abandon(LATCH_RELAY_SESSION)
3859 .await;
3860
3861 wait_until_the_actor_serves_again(&handle).await;
3866 assert_eq!(relay_starts(&start_log), 2);
3867 }
3868 #[cfg(unix)]
3873 #[tokio::test]
3874 async fn a_close_latch_reuses_an_unchanged_archive_and_exports_after_new_content() {
3875 if std::env::var_os(REUSE_TEST_CHILD).is_none() {
3878 let directory = tempfile::tempdir().unwrap();
3879 let test_name = format!(
3880 "{}::a_close_latch_reuses_an_unchanged_archive_and_exports_after_new_content",
3881 module_path!()
3882 .strip_prefix("mj_controller::")
3883 .unwrap_or(module_path!())
3884 );
3885 let output = Command::new(std::env::current_exe().unwrap())
3886 .args(["--exact", &test_name, "--nocapture"])
3887 .env(REUSE_TEST_CHILD, "1")
3888 .env(LATCH_RELAY_STARTUP_DELAY_MS, "31000")
3891 .env("MJ_DATA_DIR", directory.path())
3892 .output()
3893 .unwrap();
3894 assert!(
3895 output.status.success(),
3896 "isolated checkpoint reuse test failed\nstdout:\n{}\nstderr:\n{}",
3897 String::from_utf8_lossy(&output.stdout),
3898 String::from_utf8_lossy(&output.stderr)
3899 );
3900 return;
3901 }
3902 let _writer = hel::hel_database::install_isolated_test_writer();
3904
3905 std::thread::spawn(|| {
3908 std::thread::sleep(std::time::Duration::from_secs(120));
3909 eprintln!("the reuse checkpoint never finished its latch");
3910 std::process::exit(101);
3911 });
3912
3913 #[derive(Default)]
3914 struct RecordingExecutor {
3915 purposes: std::sync::Mutex<Vec<String>>,
3916 active_stages: std::sync::Mutex<Vec<ProvisionStage>>,
3917 stage_events: std::sync::Mutex<Vec<(ProvisionStage, bool)>>,
3918 observed_stages: std::sync::Mutex<Vec<(String, Vec<ProvisionStage>)>>,
3919 }
3920
3921 impl RecordingExecutor {
3922 fn refused(&self, command: &CommandSpec) -> Result<CommandOutput> {
3923 self.purposes.lock().unwrap().push(command.purpose.clone());
3924 self.observed_stages.lock().unwrap().push((
3925 command.purpose.clone(),
3926 self.active_stages.lock().unwrap().clone(),
3927 ));
3928 Ok(CommandOutput {
3929 status: 1,
3930 stdout: Vec::new(),
3931 stderr: b"no target is provisioned for this test".to_vec(),
3932 })
3933 }
3934
3935 fn purposes(&self) -> Vec<String> {
3936 self.purposes.lock().unwrap().clone()
3937 }
3938
3939 fn observed_stages(&self) -> Vec<(String, Vec<ProvisionStage>)> {
3940 self.observed_stages.lock().unwrap().clone()
3941 }
3942
3943 fn stage_events(&self) -> Vec<(ProvisionStage, bool)> {
3944 self.stage_events.lock().unwrap().clone()
3945 }
3946 }
3947
3948 impl CommandExecutor for RecordingExecutor {
3949 fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
3950 self.refused(command)
3951 }
3952
3953 fn execute_with_stdin(
3954 &self,
3955 command: &CommandSpec,
3956 _input: &mut (dyn std::io::Read + Send),
3957 ) -> Result<CommandOutput> {
3958 self.refused(command)
3959 }
3960
3961 fn stage_started(&self, stage: ProvisionStage) {
3962 self.active_stages.lock().unwrap().push(stage);
3963 self.stage_events.lock().unwrap().push((stage, true));
3964 }
3965
3966 fn stage_finished(&self, stage: ProvisionStage) {
3967 let mut active = self.active_stages.lock().unwrap();
3968 let position = active
3969 .iter()
3970 .position(|active_stage| *active_stage == stage)
3971 .expect("stage finished without a matching start");
3972 active.remove(position);
3973 self.stage_events.lock().unwrap().push((stage, false));
3974 }
3975 }
3976
3977 let data_directory = PathBuf::from(std::env::var_os("MJ_DATA_DIR").unwrap());
3978 let relay_root = data_directory.join("relay");
3979 let profile_home = data_directory.join("profile");
3980 let archive_directory = data_directory.join("archives");
3981 for directory in [&relay_root, &profile_home, &archive_directory] {
3982 std::fs::create_dir_all(directory).unwrap();
3983 }
3984 let checkpoint = write_checkpoint_gate_archive(&archive_directory, LATCH_RELAY_SESSION, 2);
3987
3988 let mut session = checkpoint_test_session(LATCH_RELAY_SESSION);
3989 session.target_template_id = "local".into();
3990 session.target = Some(TargetLocator::LocalBare {
3991 worker_root: data_directory.join("workers").join(LATCH_RELAY_SESSION),
3992 });
3993 session.checkpoint = Some(checkpoint.clone());
3994 hel::hel_database::save_session(&session).unwrap();
3995
3996 let mut config = HelConfig::default();
3997 config.profiles.insert(
3998 "codex".into(),
3999 HarnessProfile {
4000 kind: hel::hel_config::HarnessKind::Codex,
4001 home: profile_home,
4002 environment: BTreeMap::new(),
4003 context_window_bytes: None,
4004 },
4005 );
4006 config
4007 .targets
4008 .insert("local".into(), TargetTemplate::LocalBare);
4009 config.bundles.insert(
4010 "project".into(),
4011 ProjectBundle {
4012 primary_repo: "project".into(),
4013 repositories: vec![ProjectRepository {
4014 id: "project".into(),
4015 github: Some("example/project".into()),
4016 local: None,
4017 destination: "project".into(),
4018 git_ref: None,
4019 }],
4020 },
4021 );
4022 let controller = Controller {
4023 config,
4024 state: HelState {
4025 sessions: BTreeMap::from([(LATCH_RELAY_SESSION.into(), session)]),
4026 ..HelState::default()
4027 },
4028 };
4029
4030 let channels = crate::hel_session_manager::spawn_session_manager().unwrap();
4031 channels
4032 .targets
4033 .send(vec![latch_relay_target(
4034 &relay_root,
4035 None,
4036 ReleaseSupport::Supported,
4037 false,
4038 )])
4039 .unwrap();
4040 let handle = channels
4041 .control
4042 .wait_for_session(LATCH_RELAY_SESSION, Duration::from_secs(10))
4043 .await
4044 .unwrap();
4045
4046 let executor = RecordingExecutor::default();
4047 let latched = controller
4048 .checkpoint_session_latched(
4049 LATCH_RELAY_SESSION,
4050 &executor,
4051 Some(&channels.control),
4052 LatchExclusivity::HoldThroughClose,
4053 CheckpointExportPolicy::ReuseUnchangedArchive,
4054 )
4055 .await
4056 .unwrap();
4057
4058 assert!(
4059 executor.purposes().is_empty(),
4060 "an unchanged session exported an archive anyway: {:?}",
4061 executor.purposes()
4062 );
4063 assert_eq!(latched.artifact.metadata, checkpoint);
4064 assert!(checkpoint.archive_path.exists());
4065
4066 assert!(latched.cursor.ordinal > checkpoint.event_frontier);
4069 let cursor = latched.cursor.clone();
4070 latched.complete().await.unwrap();
4071 wait_until_the_actor_serves_again(&handle).await;
4072
4073 handle
4077 .submit(
4078 new_command_id("busy-prompt").unwrap(),
4079 RelayCommand::Prompt {
4080 prompt: vec![ContentBlock::Text(TextContent::new("keep working"))],
4081 },
4082 )
4083 .await
4084 .unwrap();
4085 let mut connection = handle.lease_connection().await.unwrap();
4086 let before = connection.connection_mut().sync().await.unwrap();
4087 assert_eq!(before.operational.execution, RelayExecutionState::Running);
4088 connection.release();
4089 let deferred = controller
4090 .checkpoint_session_latched(
4091 LATCH_RELAY_SESSION,
4092 &executor,
4093 Some(&channels.control),
4094 LatchExclusivity::ReleaseAfterLatch,
4095 CheckpointExportPolicy::ReuseUnchangedArchive,
4096 )
4097 .await;
4098 assert!(
4099 matches!(deferred, Err(ref error) if error.downcast_ref::<CheckpointDeferred>().is_some())
4100 );
4101 wait_until_the_actor_serves_again(&handle).await;
4102 let mut connection = handle.lease_connection().await.unwrap();
4103 let after = connection.connection_mut().sync().await.unwrap();
4104 assert_eq!(after.operational.execution, RelayExecutionState::Running);
4105 assert!(after.operational.checkpoint_barrier.is_none());
4106 let journal =
4107 std::fs::read_to_string(relay_root.join("relay-journal/active.jsonl")).unwrap();
4108 for line in journal.lines() {
4109 let event: hel::hel_worker::RelayEvent = serde_json::from_str(line).unwrap();
4110 if event.ordinal > before.operational.latest_ordinal {
4111 assert!(
4112 !matches!(
4113 event.observation,
4114 hel::hel_worker::RelayObservation::CommandQueued {
4115 command: RelayCommand::BeginCheckpoint { .. },
4116 ..
4117 } | hel::hel_worker::RelayObservation::CommandInterrupted {
4118 command: hel::hel_worker::RelayCommandKind::BeginCheckpoint,
4119 ..
4120 }
4121 ),
4122 "busy deferral journaled checkpoint activity: {event:?}"
4123 );
4124 }
4125 }
4126 connection.release();
4127 handle
4128 .submit(
4129 new_command_id("finish-busy-prompt").unwrap(),
4130 RelayCommand::CancelTurn,
4131 )
4132 .await
4133 .unwrap();
4134 handle.sync_now().await.unwrap();
4135
4136 handle
4138 .submit(
4139 new_command_id("resume-notice").unwrap(),
4140 RelayCommand::RecordNotice {
4141 text: "the session changed".into(),
4142 },
4143 )
4144 .await
4145 .unwrap();
4146 for attempt in 0.. {
4147 handle.sync_now().await.unwrap();
4148 let materialized = handle.view().snapshot.map(|snapshot| snapshot.materialized);
4149 if materialized.is_some_and(|materialized| {
4150 materialized.applied_event_ordinal > cursor.ordinal
4151 && !materialized.transcript.is_empty()
4152 }) {
4153 break;
4154 }
4155 assert!(attempt < 200, "the notice never reached the projection");
4156 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
4157 }
4158
4159 let changed = controller
4160 .checkpoint_session_latched(
4161 LATCH_RELAY_SESSION,
4162 &executor,
4163 Some(&channels.control),
4164 LatchExclusivity::HoldThroughClose,
4165 CheckpointExportPolicy::ReuseUnchangedArchive,
4166 )
4167 .await;
4168 let Err(error) = changed else {
4169 panic!("a changed session reused its installed archive");
4170 };
4171
4172 assert!(
4173 executor
4174 .purposes()
4175 .contains(&"export target checkpoint".to_owned()),
4176 "a changed session skipped its export: {:?}",
4177 executor.purposes()
4178 );
4179 assert!(
4180 format!("{error:#}").contains("no target is provisioned for this test"),
4181 "{error:#}"
4182 );
4183 assert!(
4184 executor.observed_stages().iter().any(|(purpose, stages)| {
4185 purpose == "export target checkpoint"
4186 && stages.contains(&ProvisionStage::RecoveryCopy)
4187 }),
4188 "close checkpoint export did not run inside RecoveryCopy: {:?}",
4189 executor.observed_stages()
4190 );
4191 assert_eq!(
4192 executor
4193 .stage_events()
4194 .into_iter()
4195 .filter(|(stage, _)| *stage == ProvisionStage::RecoveryCopy)
4196 .collect::<Vec<_>>(),
4197 vec![
4198 (ProvisionStage::RecoveryCopy, true),
4199 (ProvisionStage::RecoveryCopy, false)
4200 ]
4201 );
4202 assert!(executor.active_stages.lock().unwrap().is_empty());
4203 assert!(checkpoint.archive_path.exists());
4204 }
4205 #[cfg(unix)]
4206 fn relay_starts(path: &Path) -> usize {
4207 std::fs::read_to_string(path)
4208 .unwrap_or_default()
4209 .lines()
4210 .count()
4211 }
4212 #[cfg(unix)]
4215 fn latched_checkpoint(
4216 relay: ControllerRelayLease,
4217 barrier_command_id: String,
4218 cursor: RelayCursor,
4219 completion: CheckpointCompletion,
4220 ) -> LatchedCheckpoint {
4221 LatchedCheckpoint {
4222 artifact: CheckpointArtifact {
4223 metadata: CheckpointMetadata {
4224 archive_path: PathBuf::from("checkpoint.hel.zip"),
4225 sha256: "a".repeat(64),
4226 created_at: now(),
4227 event_frontier: cursor.ordinal,
4228 },
4229 native_session_id: "native-session".into(),
4230 event_frontier_digest: cursor.digest.clone(),
4231 },
4232 relay,
4233 barrier_command_id,
4234 cursor,
4235 completion,
4236 }
4237 }
4238 #[test]
4239 fn checkpoint_persistence_rollback_restores_memory_and_reports_both_failures() {
4240 let session_id = "0123456789abcdef0123456789abcdef";
4241 let previous = checkpoint_test_session(session_id);
4242 let mut changed = previous.clone();
4243 changed.state = SessionState::Closing;
4244 changed.last_checkpoint_error = Some("partially installed checkpoint".into());
4245 let mut state = HelState::default();
4246 state.sessions.insert(session_id.into(), changed);
4247
4248 let error = restore_session_after_persistence_failure(
4249 &mut state,
4250 session_id,
4251 &previous,
4252 anyhow::anyhow!("verified checkpoint persistence failed"),
4253 |record| {
4254 assert_eq!(record, &previous);
4255 Err(anyhow::anyhow!("rollback database write failed"))
4256 },
4257 );
4258
4259 assert_eq!(state.sessions.get(session_id), Some(&previous));
4260 let detail = format!("{error:#}");
4261 assert!(detail.contains("verified checkpoint persistence failed"));
4262 assert!(detail.contains("rollback database write failed"));
4263 }
4264 #[test]
4265 fn installed_checkpoint_gate_reopens_and_checks_sha() {
4266 let directory = tempfile::tempdir().unwrap();
4267 let session_id = "0123456789abcdef0123456789abcdef";
4268 let checkpoint = write_checkpoint_gate_archive(directory.path(), session_id, 7);
4269 verify_installed_checkpoint_gate(session_id, &checkpoint).unwrap();
4270
4271 let mut wrong_sha = checkpoint.clone();
4272 wrong_sha.sha256 = "b".repeat(64);
4273 assert!(
4274 verify_installed_checkpoint_gate(session_id, &wrong_sha)
4275 .unwrap_err()
4276 .to_string()
4277 .contains("SHA changed")
4278 );
4279 std::fs::write(
4280 &checkpoint.archive_path,
4281 b"changed after first verification",
4282 )
4283 .unwrap();
4284 assert!(
4285 format!(
4286 "{:#}",
4287 verify_installed_checkpoint_gate(session_id, &checkpoint).unwrap_err()
4288 )
4289 .contains("installed checkpoint SHA changed")
4290 );
4291 }
4292 #[test]
4293 fn an_installed_archive_is_reused_when_only_relay_bookkeeping_moved() {
4294 let directory = tempfile::tempdir().unwrap();
4295 let session_id = "0123456789abcdef0123456789abcdef";
4296 let checkpoint = write_checkpoint_gate_archive(directory.path(), session_id, 7);
4297 let archived = verify_archive_streaming(&checkpoint.archive_path)
4298 .unwrap()
4299 .canonical_session;
4300
4301 let mut latched = archived.clone();
4304 latched.event_frontier += 6;
4305 latched.event_frontier_digest = "b".repeat(64);
4306 latched.session.last_activity_at_ms = Some(9_999);
4307
4308 let artifact = reusable_installed_checkpoint(
4309 session_id,
4310 Some(&checkpoint),
4311 "native-session",
4312 latched.event_frontier,
4313 &latched,
4314 )
4315 .expect("an unchanged session reuses its installed archive");
4316
4317 assert_eq!(artifact.metadata, checkpoint);
4318 assert_eq!(artifact.native_session_id, "native-session");
4319 assert_eq!(
4320 artifact.event_frontier_digest,
4321 archived.event_frontier_digest
4322 );
4323 verify_checkpoint_artifact(session_id, &artifact).unwrap();
4325 verify_installed_checkpoint_gate(session_id, &artifact.metadata).unwrap();
4326 }
4327 #[test]
4328 fn archive_reuse_falls_back_to_a_full_export_for_anything_but_bookkeeping() {
4329 let directory = tempfile::tempdir().unwrap();
4330 let session_id = "0123456789abcdef0123456789abcdef";
4331 let checkpoint = write_checkpoint_gate_archive(directory.path(), session_id, 7);
4332 let archived = verify_archive_streaming(&checkpoint.archive_path)
4333 .unwrap()
4334 .canonical_session;
4335 let mut latched = archived.clone();
4336 latched.event_frontier += 6;
4337 let reuse = |installed: Option<&CheckpointMetadata>,
4338 ordinal: u64,
4339 session: &CanonicalSessionSnapshot| {
4340 reusable_installed_checkpoint(session_id, installed, "native-session", ordinal, session)
4341 };
4342
4343 assert!(reuse(None, latched.event_frontier, &latched).is_none());
4344
4345 let mut with_new_content = latched.clone();
4346 with_new_content.transcript.push(CanonicalTranscriptItem {
4347 stable_id: "system:notice:notice-1".into(),
4348 position: latched.event_frontier,
4349 latest_content_event_ordinal: None,
4350 created_at_ms: 2_000,
4351 last_changed_at_ms: 2_000,
4352 body: CanonicalTranscriptBody::System {
4353 text: "resumed".into(),
4354 },
4355 });
4356 assert!(reuse(Some(&checkpoint), latched.event_frontier, &with_new_content).is_none());
4357
4358 assert!(reuse(Some(&checkpoint), checkpoint.event_frontier - 1, &latched).is_none());
4360
4361 let mut wrong_sha = checkpoint.clone();
4362 wrong_sha.sha256 = "b".repeat(64);
4363 assert!(reuse(Some(&wrong_sha), latched.event_frontier, &latched).is_none());
4364
4365 let another_session =
4366 write_checkpoint_gate_archive(directory.path(), "1123456789abcdef0123456789abcdef", 7);
4367 assert!(reuse(Some(&another_session), latched.event_frontier, &latched).is_none());
4368
4369 std::fs::write(&checkpoint.archive_path, b"not an archive any more").unwrap();
4370 assert!(reuse(Some(&checkpoint), latched.event_frontier, &latched).is_none());
4371 }
4372}