Part II — Intermediate Development · Chapter 11 · Lesson 2 of 6 · 9 min read
Lesson 2: Deterministic Locks and Reproducible Builds
Lesson level: Intermediate
Purpose: Turn version constraints into one recorded set of concrete versions, so that every developer, build server, and release produces the same result from the same inputs.
Release note: lock-file names, field names, and commands in this lesson are illustrative. Verify them against the installed v0.63 release before relying on them.
Learning objectives
By the end of this lesson, you should be able to:
- explain the difference between a constraint and a locked version;
- describe what a lock file records and why it is machine-generated;
- define determinism and reproducibility, and say how they differ;
- list the inputs that a reproducible build must hold fixed;
- update a single dependency without disturbing the rest;
- use a frozen or locked mode in automation;
- review a lock-file diff for risk; and
- diagnose a build that differs between two machines.
2.1 The problem a lock solves
Lesson 1 let the order_rules package accept input_checks ^1.2.0. That constraint admits many versions. If a developer resolves it today and a build server resolves it next month, they may receive different versions. The code is identical, the manifest is identical, and the behavior still differs.
A lock removes that freedom. It records the exact version chosen for every dependency, including dependencies of dependencies, so later resolutions reuse the recorded choice instead of choosing again.
2.2 Constraint versus lock
| Aspect | Manifest constraint | Lock entry |
|---|---|---|
| Written by | A person | The tool |
| Describes | A set of acceptable versions | One chosen version |
| Changes when | The author decides to widen or narrow the range | A resolution or update is run |
| Reviewed as | A statement of intent | A record of fact |
| Example | input_checks "^1.2.0" | input_checks 1.2.4 |
The manifest says what you are willing to accept. The lock says what you actually used. Both are needed, and neither replaces the other.
2.3 What a lock records
A useful lock records, for every package in the resolved graph:
- the package name;
- the exact version;
- the source it came from, such as a registry, a Git revision, or a local path;
- an integrity value, such as a checksum, that identifies the exact content; and
- the packages it depends on.
# pea.lock (illustrative; generated by the tool)
[[package]]
name = "input_checks"
version = "1.2.4"
source = "registry"
checksum = "sha256:<recorded-by-tool>"
dependencies = []
The layout above is a teaching sketch. Do not copy it into a real project. Let the installed tool generate the real file.
2.4 Why a lock is generated, not written
- Hand edits can create combinations the resolver would never choose.
- The tool can recompute integrity values; a person cannot do so reliably.
- A generated file has a predictable layout, so diffs show only real changes.
- Regeneration from the manifest is the repair for a damaged lock.
2.5 Determinism and reproducibility
Determinism means the same inputs always produce the same output. Reproducibility means someone else, on another machine at another time, can rebuild and obtain the same result. Determinism is a property of a process. Reproducibility is a property of the whole setup, including its inputs and environment.
| Concept | Question it answers | Typical failure |
|---|---|---|
| Determinism | Does the same input give the same output here? | Hidden randomness or clock reads |
| Reproducibility | Can another party obtain the same output? | An unrecorded input, such as a floating version |
2.6 Inputs a reproducible build must fix
- the source code at a known revision;
- the manifest and the lock;
- the version of the PeaLang toolchain itself;
- the contents of every dependency, verified by integrity values;
- build options and feature flags; and
- any generated or embedded data, with no timestamps or machine-specific paths.
If any input is allowed to float, the build is reproducible only by luck.
2.7 The lock lifecycle
- Resolve: the tool reads the manifest and chooses concrete versions that satisfy every constraint.
- Write: the tool records the choices in the lock.
- Commit: the team keeps the lock in version control beside the manifest.
- Install: later builds read the lock and install exactly what it records.
- Update: a person deliberately asks the tool to choose newer versions, then reviews the change.
2.8 Illustrative commands
pea lock
pea install --locked
pea update input_checks
pea lock resolves the manifest and writes or refreshes the lock.
pea install --locked installs from the lock and fails if the lock is missing or does not match the manifest.
pea update input_checks re-resolves one dependency and leaves the others alone.
These command names and options are illustrative. Verify the real names with the help output of your installed release.
2.9 Frozen mode for automation
A build server should never silently change its inputs. Use the strictest installation mode available: it should refuse to run when the lock is absent, stale, or inconsistent with the manifest. A failure here is a gift. It tells you that someone changed the manifest without regenerating the lock.
2.10 Updating deliberately
| Goal | Approach | Risk |
|---|---|---|
| Take a security fix for one package | Update only that package, then run the full suite | Low |
| Refresh everything | Update all packages in a separate change | Higher; review the full diff |
| Move to a new major version | Edit the constraint, resolve, fix callers, test | Highest; plan the work |
Keep dependency updates in their own changes. A single change that mixes feature work with a lock refresh makes failures hard to attribute.
2.11 Reading a lock diff
A lock diff is long but regular. Review it with these questions:
- Which direct dependencies changed version, and by how much?
- Did any package move across a major version?
- Did a new package appear that no manifest names directly?
- Did a package disappear, and is anything still relying on it?
- Did a source change, for example from a registry to a Git revision?
- Did any checksum change without a version change? Treat that as a serious warning.
2.12 Libraries and applications
An application is a finished product, so it should commit its lock and install strictly from it. A library is consumed by other packages, whose own locks decide the final versions. A library may still keep a lock for its own tests and continuous integration, but consumers will not use it. Make this choice explicit in the project documentation.
2.13 Worked example: a surprise upgrade
The order_rules suite passes on a developer laptop. On the build server it fails in the boundary layer. The manifest and source are identical. The lock file is absent from version control, so the build server resolved input_checks ^1.2.0 afresh and received 1.3.0, which changed how a negative quantity is reported.
| Step | Finding |
|---|---|
| Compare manifests | Identical |
| Compare installed versions | Laptop 1.2.4; build server 1.3.0 |
| Root cause | No lock committed; the range admitted a newer release |
| Fix | Commit the lock; install in locked mode; open a separate change to evaluate 1.3.0 |
| Prevention | Fail the build when the lock is missing |
2.14 Reproducibility beyond dependencies
- Timestamps: embedding the build time makes every build differ. Prefer a fixed value supplied from the source revision.
- Ordering: iteration over unordered collections can change output order. Sort before writing.
- Paths: absolute machine paths in artifacts differ between machines. Use relative paths.
- Randomness: seed it, or keep it out of build output.
- Toolchain version: record the exact PeaLang version, as Chapter 1 recommended.
2.15 Verifying reproducibility
- Build the package twice on the same machine from a clean checkout.
- Compare a hash of each artifact. They must match.
- Build once more on a different machine or in a clean container.
- Compare the hash again.
- If hashes differ, inspect the artifacts for timestamps, ordering, and path differences.
Record the hash and the inputs used. That record becomes release evidence in later chapters.
2.16 Common mistakes
| Mistake | Why it hurts | Better practice |
|---|---|---|
| Not committing the lock | Each machine resolves independently | Commit it with the manifest |
| Editing the lock by hand | Creates states the resolver would not choose | Regenerate with the tool |
| Ignoring a lock mismatch failure | Hides an unrecorded change | Fix the cause and regenerate |
| Updating everything at once | A failure cannot be attributed | Update one package or one group per change |
| Treating a lock as a security control by itself | A lock records choices but does not prove trust | Add checksums and signature checks |
| Embedding timestamps in artifacts | Every build differs | Use a fixed value from the source revision |
| Leaving the toolchain version unrecorded | Behavior may change with the tool | Record the exact PeaLang version |
2.17 Guided practice
- In your own words, explain how a lock differs from a constraint.
- List the five facts a lock should record for each package.
- Explain why determinism does not by itself guarantee reproducibility.
- Name four inputs that a reproducible build must fix.
- Decide whether an application or a library should commit its lock, and justify it.
- Describe how you would update one dependency without touching others.
- Given a lock diff in which a checksum changed but the version did not, state what you would do first.
- Explain why a build server should refuse to run when the lock is stale.
2.18 Hands-on lab: Make order_rules reproducible
Goal: Prove that the order_rules package builds the same way twice and on a second environment.
- Start from the Lesson 1 manifest with its runtime and development dependencies.
- Resolve the manifest and generate the lock using the installed tool. Record the command and its output.
- Install from the lock in strict mode in a clean directory.
- Build the package twice and record a hash of each artifact. Confirm that they match.
- Introduce a deliberate defect: remove the lock and resolve again after a newer version is available, or edit the manifest without regenerating the lock. Record what the strict install reports.
- Restore the lock and confirm that the strict install succeeds.
- Update exactly one dependency, review the lock diff using the questions in section 2.11, and run the full Chapter 10 suite.
- Rebuild in a second clean environment and compare hashes.
Deliverable: The manifest, the lock, the command transcript, the two artifact hashes from the same machine, the hash from the second environment, the defect note, and the lock-diff review. Use only synthetic package names, and do not include registry credentials.
2.19 Review questions
- What problem does a lock solve?
- Who writes a constraint, and who writes a lock entry?
- What five facts should a lock record for each package?
- What is the difference between determinism and reproducibility?
- Why should a build server install in a strict mode?
- How do you update a single dependency safely?
- What does it mean when a checksum changes but the version does not?
- When should a library commit its lock?
- Name three sources of non-reproducible output other than dependencies.
- Why should dependency updates be separate changes?
2.20 Applied challenge
Write a one-page reproducibility policy for a team that maintains several PeaLang packages. State which files are committed, which installation mode automation uses, how updates are proposed and reviewed, how artifact hashes are recorded, and who may approve an exception. Include a short checklist that a reviewer can apply to any pull request that touches the manifest or the lock.
Lesson summary
A lock converts the flexible constraints in a manifest into one recorded set of exact versions, sources, and integrity values. Deterministic resolution and strict installation keep later builds from silently drifting, while a reproducible build also fixes the source revision, toolchain, build options, and any embedded data. Deliberate, separate updates and careful review of lock diffs keep change visible, and recorded artifact hashes provide evidence that a build can be repeated.
Transition to Lesson 3
A lock records where each package came from, but so far every example used one registry. The next lesson covers local, Git, and registry sources, showing how each source type is declared, what it can and cannot guarantee, and when each one is appropriate.
Lesson status: Ready for review.