Skip to content

feat(cli): bundle create + appendix-bundles.md (#391) - #469

Merged
thiras merged 5 commits into
mainfrom
feat/391-bundle-create
May 8, 2026
Merged

thiras merged 5 commits into
mainfrom
feat/391-bundle-create

Conversation

@thiras

@thiras thiras commented May 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • New decdn bundle create -i <dir> -o <file> subcommand. Walks the directory, BLAKE3-hashes each regular file, emits a canonical single-line JSON manifest. Determinism is the load-bearing property: same input directory → byte-identical bytes → byte-identical bundle BLAKE3, which is what makes single-hash distribution viable.
  • New adr/appendix-bundles.md (~150 lines): JSON v1 schema, b3:<hex> hash convention, path-safety rules, determinism contract, bundle-as-blob property, non-relationship to ADR 012's DECDNMAN chunk manifest.
  • Implements the unblocked portion of decdn bundle subcommand: pull and create directory bundles #391. bundle pull stays deferred behind a missing client-side fetch primitive; issue stays open after merge.

Test plan

  • cargo fmt --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo nextest run -p decdn-cli -p decdn-common — 234 tests pass (16 new integration + 13 new unit for bundle create)
  • Manual smoke: determinism across runs, --json status with bundle_hash, gitignore-style --exclude, invalid-glob error message, atomic temp+rename write, symlink skip default + follow + escape rejection, directory self-loop bound

🤖 Generated with Claude Code

`decdn bundle create` walks a directory, BLAKE3-hashes each regular
file, and emits a canonical JSON manifest. Same input dir yields
byte-identical output, so the manifest's own BLAKE3 is stable across
runs — single-hash bundle distribution.

`bundle pull` (both `-i` and `--hash` forms) is deferred behind a
missing client-side fetch primitive; the issue stays open.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 7, 2026 20:42
@thiras thiras linked an issue May 7, 2026 that may be closed by this pull request
4 tasks
@codecov

codecov Bot commented May 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.80645% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/cli/src/commands/bundle.rs 95.79% 13 Missing ⚠️

📢 Thoughts on this report? Let us know!

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the decdn bundle create command, which generates deterministic JSON manifests for directory contents using BLAKE3 hashing. The changes include a new ADR appendix documenting the bundle format, the CLI implementation in crates/cli, and comprehensive integration tests. Review feedback suggests aligning the path validation logic with the ADR's requirement to reject current-directory components, utilizing the blake3 crate's update_reader method for more efficient hashing, and mitigating potential TOCTOU vulnerabilities by reusing file handles for both metadata and content reading.

Comment thread crates/cli/src/commands/bundle.rs Resolved
Comment thread crates/cli/src/commands/bundle.rs Resolved
Comment thread crates/cli/src/commands/bundle.rs Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds initial support for deterministic “directory bundles” to the decdn CLI, including a new bundle create subcommand and an appendix documenting the on-disk bundle format/spec so bundle files can be content-addressed and distributed by a single BLAKE3 hash.

Changes:

  • Introduces decdn bundle create -i <dir> -o <file> to walk a directory, hash files with BLAKE3, and emit canonical single-line JSON.
  • Adds extensive unit + integration coverage for determinism, exclude globs, symlink behavior, and JSON status output.
  • Documents the bundle JSON v1 format, path-safety rules, and determinism contract in adr/appendix-bundles.md.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
crates/common/src/cli/mod.rs Wires the new bundle CLI namespace into shared CLI types/exports.
crates/common/src/cli/bundle.rs Defines clap args/types for decdn bundle and bundle create flags.
crates/cli/src/main.rs Dispatches the new Command::Bundle to the CLI implementation.
crates/cli/src/commands/mod.rs Exposes the new commands::bundle module.
crates/cli/src/commands/bundle.rs Implements directory walk, hashing, canonical JSON serialization, and reporting for bundle create.
crates/cli/tests/bundle_create.rs Adds integration tests covering determinism, sorting, excludes, symlink rules, and --json output.
crates/cli/Cargo.toml Adds CLI dependencies needed for walking, glob excludes, and BLAKE3 hashing.
Cargo.toml Adds workspace dependency versions for walkdir, globset, and blake3.
Cargo.lock Locks new transitive dependencies (globset, bstr, etc.).
adr/appendix-bundles.md Adds the bundle file format appendix and invariants/spec for determinism and safety.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/cli/src/commands/bundle.rs Resolved
Comment thread crates/cli/src/commands/bundle.rs Resolved
Comment thread crates/cli/src/commands/bundle.rs Resolved
thiras and others added 2 commits May 7, 2026 23:53
- `validate_relpath` now bails on `Component::CurDir` to match the
  ADR's "every component must be `Component::Normal`" rule (was
  silently skipping). Caught by gemini-code-assist + Copilot.
- `hash_file_streaming` becomes `hash_file_at`: opens the file once
  and pulls both the BLAKE3 hash and the size from the open handle
  via `metadata()` (fstat) + `blake3::Hasher::update_reader`. Closes
  the TOCTOU window between the previous separate `entry.metadata()`
  + `File::open` calls and lets the upstream-recommended streaming
  primitive manage buffering and EINTR retries. `HASH_BUF_SIZE`
  drops out as the manual loop is gone.
- `write_bundle` switches to `tempfile::NamedTempFile::new_in` +
  `persist`. The unique O_CREAT|O_EXCL temp avoids the predictable
  `<output>.partial` filename (no symlink-clobber, no concurrent-
  writer surprise), and `persist` is the cross-platform atomic
  rename-replace primitive.

Tests updated for the new behavior; 90/90 nextest pass; clippy +
fmt clean. PR #469.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…t test

- Adds `validate_relpath_rejects_current_dir` unit test pinning the
  CurDir bail behavior added in 17add20. The bail itself was reachable
  but had no regression net; a future "simplification" to silently skip
  CurDir would have flipped behavior with no failing test.
- Fixes the `appendix-bundles.md § Path-safety rules` heading anchor
  in the CurDir comment (was hyphenated "Path-safety-rules"; the actual
  heading uses a space). Now grep-aligned with the other three anchors
  in this file.
- Adds `write_bundle_replaces_symlink_target_does_not_follow` (Unix)
  test pinning the rename(2) semantics the new docstring claims:
  if `--output` is a pre-existing symlink, the rename replaces the
  symlink with a regular file rather than following it. Two reviewers
  flagged the missing test; the behavior is rename(2)'s, but pinning
  it locally protects against a future swap to e.g. persist_noclobber.
- Narrows the "crash mid-write" wording on the `write_bundle` doc to
  distinguish Rust-side errors (Drop guard cleans up) from hard kills
  (random-named temp may survive — but never under the operator-
  expected name, which is the actual safety property).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alpergundogdu

Copy link
Copy Markdown
Contributor

Code review

Found 2 issues:

  1. The inline comment claims the path-safety check fires "regardless of the follow flag", but the symlink branch hits continue before the canonicalize-and-contain check below. With --follow-symlinks=false, the check is never reached for symlinked entries — they're silently skipped, which is fine but the comment is misleading.

}
// With follow_links=false, symlinks come through as symlink entries
// we never read — skip silently and surface the count in --json.
// With follow_links=true, walkdir resolves the link transparently
// and the entry presents as a regular file; the path-safety check
// below catches escapes regardless of the follow flag.
if ftype.is_symlink() {
skipped_symlinks = skipped_symlinks.saturating_add(1);
continue;
}
if !ftype.is_file() {

  1. appendix-bundles.md Rule 1 says "A symlink that resolves outside the root is a hard error regardless of --follow-symlinks", but Rule 5 says any symlink is "skipped silently" when --follow-symlinks is off. The implementation follows Rule 5 (silent skip); Rule 1's "regardless of --follow-symlinks" clause should be qualified to apply only when the flag is on.

1. **Canonicalize-and-contain.** The `--input` directory is
canonicalized once. Every walked path is canonicalized and required
to sit under that root via `starts_with`. Same shape as
`FilesystemOrigin::fetch` in the cache crate. A symlink that
resolves outside the root is a hard error regardless of
`--follow-symlinks` — the bundle's `path` field is a relative POSIX
string and cannot truthfully describe an external target.
2. **No `..`, root, or current-directory components.** Each component
of the produced relative path must be `Component::Normal`. The
canonicalize-and-strip-prefix flow shouldn't produce other variants;
we still assert because the invariant is load-bearing.
3. **No absolute paths.** Falls out of the no-root rule.
4. **UTF-8 only.** Non-UTF-8 path components are a hard error; no
`to_string_lossy` substitution.
5. **Symlinks.** Off by default: any symlink encountered is skipped
silently and surfaced via the `skipped_symlinks` counter on the
`--json` status report. With `--follow-symlinks`: walked
transparently, and the canonicalize-and-contain check still fires.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

thiras and others added 2 commits May 8, 2026 16:38
Two doc-only inconsistencies flagged on PR #469:

- bundle.rs:169-173 claimed the canonicalize-and-contain check fires
  "regardless of the follow flag", but with --follow-symlinks=false the
  symlink branch hits `continue` before reaching the check, matching
  appendix Rule 5's "skipped silently" semantics, not Rule 1's escape
  check.
- appendix-bundles.md Rule 1 said "regardless of --follow-symlinks",
  contradicting Rule 5 and the implementation. Qualify Rule 1 to apply
  only when --follow-symlinks is on, and cross-link Rule 5 for the
  off case.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thiras

thiras commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Both confirmed and fixed in f039d6d:

  1. bundle.rs:169-177 — comment now states the check is bypassed via continue for symlinks when follow_links=false (matches Rule 5), and only runs for resolved-symlink entries when follow_links=true.
  2. appendix-bundles.md Rule 1 — "regardless of --follow-symlinks" qualifier dropped. The hard-error clause now explicitly applies only with the flag on, with a back-reference to Rule 5 for the off case.

@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 87.72% (was 87.49%, ▲ +0.23%) — 11575/13195 lines

File Coverage Lines Δ
crates/cli/src/commands/bundle.rs 91.57% 304/332 new
crates/cli/src/main.rs 70.37% 19/27 ▲ +70.37%
History
Commit Total Δ vs base
6d9e1ad 87.72% ▲ +0.23%
f039d6d (HEAD) 87.72% ▲ +0.23%

@thiras
thiras merged commit ea439ca into main May 8, 2026
12 checks passed
@thiras
thiras deleted the feat/391-bundle-create branch May 8, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants