Hashing, Change Detection and Versions

# Use development build when interactive *and* explicitly enabled via env var.
dev_mode <- (Sys.getenv("DEV_VIGNETTES", "false") == "true")

if (dev_mode && requireNamespace("pkgload", quietly = TRUE)) {
  pkgload::load_all(
    export_all = FALSE,
    helpers = FALSE,
    attach_testthat = FALSE
  )
} else {
  # fall back to the installed package (the path CRAN, CI, and pkgdown take)
  library(stamp)
}
old_opts <- st_opts_get()
old_alias <- st_alias_get()

This article explains how stamp detects changes and records versions. We’ll cover:

The core idea: stamp compares object/code hashes and applicable restoration metadata with the saved state. For native RDS/QS2 data.tables, original keys are recorded separately from the content hash. Equal hashes alone do not imply a skipped save if the original key descriptor changed.

Save with hashes (and skip if content identical)

root <- tempfile("stamp-hashing-")
st_init(root)
## ✔ stamp initialized
##   alias: default
##   root:
##   /var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d
##   state:
##   /var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/.stamp
p <- "demo.qs"
x <- data.frame(a = 1:3)

# First write: creates artifact + sidecars + catalog entry
st_save(x, p, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/demo.qs'
##   @ version 5e9f01acdfbfaa0d
# Second write, same content & same code: skipped (no new version)
st_save(x, p, code = function(z) z, alias = NULL)
## ✔ Skip save (reason: no_change_policy) for
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/demo.qs'
nrow(st_versions(p, alias = NULL)) # should be 1
## [1] 1

In the snippet above the data.frame content, code and applicable restoration state are unchanged, so content policy skips the second save. For a data.table, a key-only change can instead create a metadata version without changing its content hash; unkeyed inputs never receive an inferred key.

Note: the first write will always create the artifact and its sidecar(s). If you see the first write skipped, check that the path you passed to st_save() exactly matches subsequent calls.

If you change content (or change the code), a new version is recorded:

x2 <- transform(x, a = a + 1L)
st_save(x2, p, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/demo.qs'
##   @ version 7f1e06c83ddd99a2
nrow(st_versions(p, alias = NULL)) # now 2
## [1] 2
st_latest(p, alias = NULL) # latest version id (string)
## [1] "7f1e06c83ddd99a2"

Policy: By design, changing the code= you pass to st_save() creates a new version even if x is identical. This makes code provenance explicit.

A short practical pattern:

Inspect sidecars & metadata

meta <- st_read_sidecar(p, alias = NULL)
meta[c(
  "format",
  "created_at",
  "size_bytes",
  "content_hash",
  "code_hash",
  "file_hash"
)]
## $format
## [1] "qs2"
## 
## $created_at
## [1] "2026-11-12T16:27:17.624624Z"
## 
## $size_bytes
## [1] 196
## 
## $content_hash
## [1] "989888efe871dabb"
## 
## $code_hash
## [1] "488e8fa49c740261"
## 
## $file_hash
## [1] "4a608c8aeaab8722"

Explanation:

Change detection (without writing)

Use these before doing expensive work, to decide whether to recompute.

x_same <- x2
x_new <- transform(x2, a = a + 10L)

st_changed(p, x = x_same, code = function(z) z, alias = NULL)
## $changed
## [1] FALSE
## 
## $reason
## [1] "no_change"
## 
## $details
## $details$content_changed
## [1] FALSE
## 
## $details$code_changed
## [1] FALSE
## 
## $details$file_changed
## [1] FALSE
st_changed_reason(p, x = x_same, code = function(z) z, alias = NULL) # "no_change"
## [1] "no_change"
st_changed(p, x = x_new, code = function(z) z, alias = NULL)
## $changed
## [1] TRUE
## 
## $reason
## [1] "content"
## 
## $details
## $details$content_changed
## [1] TRUE
## 
## $details$code_changed
## [1] FALSE
## 
## $details$file_changed
## [1] FALSE
st_changed_reason(p, x = x_new, code = function(z) z, alias = NULL) # "content"
## [1] "content"
st_should_save(p, x = x_same, code = function(z) z, alias = NULL) # recommends skip
## $save
## [1] FALSE
## 
## $reason
## [1] "no_change_policy"
st_should_save(p, x = x_new, code = function(z) z, alias = NULL) # recommends save
## $save
## [1] TRUE
## 
## $reason
## [1] "content"

When you call st_changed() or st_changed_reason() you avoid performing any file writes. These helpers are ideal as guards inside functions that compute expensive results only when necessary:

Example pattern inside your pipeline function:

if (st_should_save(p, x = out, code = my_transform)$save) {
  st_save(out, p, code = my_transform)
} else {
  message("Skipping write; content and code unchanged")
}

Loading specific versions

vids <- st_versions(p, alias = NULL)
head(vids)
##          version_id      artifact_id     content_hash        code_hash
##              <char>           <char>           <char>           <char>
## 1: 7f1e06c83ddd99a2 a0e3f417a97cded8 989888efe871dabb 488e8fa49c740261
## 2: 5e9f01acdfbfaa0d a0e3f417a97cded8 62ba8debaab06c1c 488e8fa49c740261
##    size_bytes                  created_at sidecar_format
##         <num>                      <char>         <char>
## 1:        196 2026-11-12T16:27:17.624624Z           both
## 2:        245 2026-11-12T16:27:17.515495Z           both
vid_latest <- st_latest(p, alias = NULL)
obj_latest <- st_load_version(p, vid_latest, alias = NULL)
## ✔ Loaded ← demo.qs @ 7f1e06c83ddd99a2 [qs2]
# Load an older version by id
if (nrow(vids) > 1L) {
  vid_old <- vids$version_id[[nrow(vids)]]
  obj_old <- st_load_version(p, vid_old, alias = NULL)
}
## ✔ Loaded ← demo.qs @ 5e9f01acdfbfaa0d [qs2]

st_versions() returns a table of version metadata. Each row includes the version_id, created_at, and a snapshot of sidecar fields available at commit time. Use st_load_version() to restore the artifact as it was at that version.

Where are versions stored?

Snapshots live in a versions/ directory next to each artifact, not centralized in .stamp/. Each artifact manages its own version history.

Storage pattern: <root>/<path>/<filename>/versions/<version_id>/

p <- "demo.qs"
x <- data.frame(a = 1:5)

# Write once to create a version snapshot
st_save(x, p, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/demo.qs'
##   @ version c09d9e0ee306f350
# Version history is stored next to the artifact
info <- st_info(p, alias = NULL)
artifact_dir <- fs::path_dir(info$sidecar$path)  # e.g., root/demo.qs/
vroot <- fs::path(artifact_dir, "versions")

if (fs::dir_exists(vroot)) {
  cat("Versions directory:", vroot, "\n\n")
  fs::dir_tree(vroot, recurse = TRUE, all = TRUE)
} else {
  cat("No versions directory found (versioning may be disabled)\n")
}
## No versions directory found (versioning may be disabled)

Each snapshot dir contains:

Additionally each snapshot may include a parents.json file capturing committed lineage between artifacts; this is created when stamp records explicit parents during a commit. Sidecar metadata (in stmeta/ next to the artifact) is the primary local source used to decide whether to write, while snapshots are the long-term committed record.

Note: Version directories are now distributed (one versions/ folder per artifact), not centralized in .stamp/versions/. This makes it easy to locate all history for a specific artifact.

Integrity checks on load (optional)

If verify_on_load = TRUE and a content_hash exists in the sidecar, st_load() recomputes the object’s hash and warns if it differs. A mismatch can indicate external changes, stale metadata, or a conversion by the file format.

invisible(st_load(p, alias = NULL)) # triggers optional verify; warns on mismatch
## Warning: No primary key recorded for
## '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/demo.qs'.
## ℹ You can add one with `st_add_pk()`.
## ✔ Loaded [qs2] ←
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/demo.qs'

If verify_on_load = TRUE, st_load() recomputes st_hash_obj() and compares it to the content_hash recorded in the sidecar or snapshot. Compare the loaded data with the original source before updating metadata or saving again. Use RDS or QS2 when exact ‘R’ object round-trips are required.

Troubleshooting

Q: The first st_save() was skipped and st_versions(p) is 0. A: The first write should never be skipped. Ensure you’re using the current st_should_save() which returns save = TRUE when the artifact is missing or the sidecar is missing.

Q: st_changed_reason() says "missing_meta". A: The artifact exists but the sidecar was removed or is unreadable. Call st_save(x, p, code = ...) once; it will re-materialize metadata and record a version.

Q: Changing only code= didn’t create a new version. A: By design, a code change does create a new version. Confirm st_opts("code_hash", .get = TRUE) is TRUE and you passed code= consistently (e.g., a function literal, not different object pointers to identical functions in rare cases).

Q: CSV round-trips aren’t byte-identical. A: data.table::fread/fwrite may coerce types (e.g., integers vs doubles). Compare with relaxed checks or coerce types before comparison.

Q: I see a warning on load about hash mismatch. A: With verify_on_load = TRUE, stamp recomputes the object hash and warns if it differs from the sidecar’s content_hash. The cause can be an external change, stale metadata, or a format conversion such as the CSV type changes described above. Check the data before saving again.

Q: qs2 isn’t installed. A: qs2 is a required dependency. The "qs2" handler does not fall back to {qs}. Restore the {qs2} installation before reading or writing QS2 artifacts.

Q: Sidecars not appearing. A: Check st_opts("meta_format", .get = TRUE) — set to "json", "qs2", or "both". Sidecars are written to stmeta/ next to the artifact.

Q: Versions aren’t where I expect. A: Version snapshots now live next to each artifact in <artifact-dir>/versions/<version_id>/, not centralized in .stamp/versions/. Use st_info(path)$snapshot_dir to find the exact location, or explore <artifact-dir>/versions/ directly.

Tips & conventions

Further reading / next steps: