Skip to content

The agent that grows with you

fix(bootstrap-installer): write the shared receipt schema and resolve the commit without git

teknium107be7d47ed1c
diff --git a/apps/bootstrap-installer/src-tauri/src/bootstrap.rs b/apps/bootstrap-installer/src-tauri/src/bootstrap.rs
index 50dd3a6999e..7743e55db29 100644
--- a/apps/bootstrap-installer/src-tauri/src/bootstrap.rs
+++ b/apps/bootstrap-installer/src-tauri/src/bootstrap.rs
@@ -259,30 +259,78 @@ pub(crate) fn hermes_is_installed(install_root: &std::path::Path) -> bool {
&& resolve_hermes_desktop_exe(install_root).is_some()
}
-fn resolve_marker_commit(install_root: &Path, pin: &Pin) -> Option<String> {
- if let Some(commit) = pin
- .commit
- .as_ref()
- .filter(|commit| !commit.trim().is_empty())
- {
- return Some(commit.clone());
- }
+fn is_full_sha(value: &str) -> bool {
+ value.len() == 40
+ && value
+ .bytes()
+ .all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
+}
- let output = std::process::Command::new("git")
- .args(["rev-parse", "HEAD"])
- .current_dir(install_root)
- .output()
- .ok()?;
- if !output.status.success() {
- return None;
- }
+/// HEAD of the checkout read from its ref files. install.ps1 resolves git
+/// through the PM-staged binary, which is not on PATH, so a bare `git` spawn
+/// here returns nothing on a git-less machine.
+fn read_checkout_head(install_root: &Path) -> Option<String> {
+ let git_dir = install_root.join(".git");
+ let head = std::fs::read_to_string(git_dir.join("HEAD")).ok()?;
+ let head = head.trim();
+ let sha = match head.strip_prefix("ref: ") {
+ None => head.to_string(),
+ Some(name) => match std::fs::read_to_string(git_dir.join(name)) {
+ Ok(loose) => loose.trim().to_string(),
+ Err(_) => std::fs::read_to_string(git_dir.join("packed-refs"))
+ .ok()?
+ .lines()
+ .find_map(|line| {
+ let (sha, ref_name) = line.split_once(' ')?;
+ (ref_name == name).then(|| sha.to_string())
+ })?,
+ },
+ };
+ is_full_sha(&sha).then_some(sha)
+}
- let commit = String::from_utf8_lossy(&output.stdout).trim().to_string();
- if commit.is_empty() {
- None
- } else {
- Some(commit)
- }
+/// The receipt install.ps1's Stage-Complete published moments earlier in this
+/// run. Windows PowerShell writes it with a UTF-8 BOM.
+fn read_existing_marker_commit(marker_path: &Path) -> Option<String> {
+ let raw = std::fs::read(marker_path).ok()?;
+ let body = raw.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(&raw);
+ let marker: serde_json::Value = serde_json::from_slice(body).ok()?;
+ let commit = marker.get("pinnedCommit")?.as_str()?;
+ is_full_sha(commit).then(|| commit.to_string())
+}
+
+/// Same order as install.ps1's Stage-Complete: the pinned commit, else the
+/// checkout's HEAD; the prior receipt is the last resort.
+fn resolve_marker_commit(install_root: &Path, pin: &Pin, marker_path: &Path) -> Option<String> {
+ pin.commit
+ .clone()
+ .filter(|commit| !commit.trim().is_empty())
+ .or_else(|| read_checkout_head(install_root))
+ .or_else(|| read_existing_marker_commit(marker_path))
+}
+
+/// UTC ISO-8601 with milliseconds, the `completedAt` format install.ps1,
+/// Electron (`toISOString`) and hermes_cli/source_stamp.py all write.
+fn iso8601_utc(since_epoch: std::time::Duration) -> String {
+ let secs = since_epoch.as_secs();
+ let (days, rem) = ((secs / 86_400) as i64, secs % 86_400);
+ // Days-to-civil conversion (proleptic Gregorian), valid for any post-1970 date.
+ let z = days + 719_468;
+ let era = z.div_euclid(146_097);
+ let doe = z.rem_euclid(146_097);
+ let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
+ let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
+ let mp = (5 * doy + 2) / 153;
+ let day = doy - (153 * mp + 2) / 5 + 1;
+ let month = if mp < 10 { mp + 3 } else { mp - 9 };
+ let year = yoe + era * 400 + i64::from(month <= 2);
+ format!(
+ "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}.{:03}Z",
+ rem / 3_600,
+ rem % 3_600 / 60,
+ rem % 60,
+ since_epoch.subsec_millis()
+ )
}
fn write_bootstrap_complete_marker(install_root: &Path, pin: &Pin) -> Result<serde_json::Value> {
@@ -298,15 +346,14 @@ fn write_bootstrap_complete_marker(install_root: &Path, pin: &Pin) -> Result<ser
})?;
}
- let completed_at_unix = SystemTime::now()
+ let completed_at = SystemTime::now()
.duration_since(UNIX_EPOCH)
- .map(|duration| duration.as_secs())
.unwrap_or_default();
let marker = serde_json::json!({
"schemaVersion": 1,
- "pinnedCommit": resolve_marker_commit(install_root, pin),
+ "pinnedCommit": resolve_marker_commit(install_root, pin, &marker_path),
"pinnedBranch": pin.branch.clone(),
- "completedAtUnix": completed_at_unix,
+ "completedAt": iso8601_utc(completed_at),
});
let mut body = serde_json::to_vec_pretty(&marker)?;
body.push(b'\n');
@@ -1183,9 +1230,51 @@ mod tests {
assert_eq!(from_disk["pinnedCommit"], "abcdef1234567890");
assert_eq!(from_disk["pinnedBranch"], "main");
assert!(
- from_disk["completedAtUnix"].as_u64().is_some(),
- "marker must carry a completion timestamp"
+ from_disk.get("completedAtUnix").is_none(),
+ "receipt must use the shared completedAt field"
+ );
+ assert_eq!(
+ iso8601_utc(std::time::Duration::from_millis(1_709_251_199_123)),
+ "2024-02-29T23:59:59.123Z"
+ );
+ let completed_at = from_disk["completedAt"].as_str().unwrap();
+ assert!(
+ completed_at.len() == 24 && completed_at.ends_with('Z'),
+ "completedAt must be ISO-8601 UTC: {completed_at}"
+ );
+ let _ = std::fs::remove_dir_all(&root);
+ }
+
+ #[test]
+ fn branch_pin_marker_resolves_commit_from_checkout_without_git() {
+ // A branch pin has no commit. The checkout's refs name it; the dir is
+ // not a real repo, so any `git` spawn cannot resolve HEAD here.
+ let root = unique_tmp_dir("marker-branch-pin");
+ let head = "0123456789abcdef0123456789abcdef01234567";
+ std::fs::create_dir_all(root.join(".git")).unwrap();
+ std::fs::write(root.join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
+ std::fs::write(
+ root.join(".git/packed-refs"),
+ format!("# pack-refs with: peeled fully-peeled sorted\n{head} refs/heads/main\n"),
+ )
+ .unwrap();
+ let pin = Pin {
+ commit: None,
+ branch: Some("main".to_string()),
+ };
+
+ let marker = write_bootstrap_complete_marker(&root, &pin).unwrap();
+ assert_eq!(marker["pinnedCommit"], head);
+
+ // Without readable refs, the commit install.ps1 already recorded
+ // (UTF-8 BOM, as Windows PowerShell writes it) survives the rewrite.
+ std::fs::remove_dir_all(root.join(".git")).unwrap();
+ let receipt = format!(
+ "\u{feff}{{\"schemaVersion\":1,\"pinnedCommit\":\"{head}\",\"pinnedBranch\":\"main\",\"completedAt\":\"2026-01-01T00:00:00.000Z\"}}"
);
+ std::fs::write(root.join(".hermes-bootstrap-complete"), receipt).unwrap();
+ let marker = write_bootstrap_complete_marker(&root, &pin).unwrap();
+ assert_eq!(marker["pinnedCommit"], head);
let _ = std::fs::remove_dir_all(&root);
}