Checking how many agents are working

How this works
Contact
← Blog
A cinematic photograph of three brass dials in a row on a dark steel panel, each set to a different number, an amber light behind the middle one, in teal and steel, the AMARBARO mark and wordmark centred over them.
✎blogbericht

What a version number like 1.4.2 means

The short version

A semantic version has major, minor and patch numbers that compare as numbers, not text, and mojo-semver follows the same rules as Python's semver package on 474 parse results.

What this is about

You have seen version numbers all your life. A phone app is at 4.12.1. A tool says it needs version 2.3 or newer. This article explains what those numbers promise and how a computer compares them.

The standard in use here is called Semantic Versioning, or semver for short. It is a small set of rules, and you can learn it in ten minutes.

Why it matters

Updates can break things. If you know what a version bump means, you know how risky an update is before you install it. A patch bump is usually safe. A major bump can ask you to change your code.

Computers also have to compare versions, for example "is the installed tool at least 1.4.0?" A naive comparison of text gets this wrong, and a wrong answer can pick the old tool or reject a good one.

How it works

A version looks like major.minor.patch. Take 1.4.2.

  • Patch (the last number, 2): a bug fix. Nothing new, nothing removed. Going from 1.4.2 to 1.4.3 should be safe.
  • Minor (the middle number, 4): a new feature that does not break old uses. Going from 1.4.2 to 1.5.0 adds something. Old code keeps working.
  • Major (the first number, 1): a change that can break old uses. Going from 1.4.2 to 2.0.0 means "read the notes first".

When a bigger number goes up, the smaller ones reset to zero.

Two more parts are optional.

  • A prerelease tag comes after a dash, as in 1.4.2-rc.1. It means "a candidate for 1.4.2, not final". A prerelease is older than the final version. So 1.4.2-rc.1 sorts before 1.4.2.
  • Build metadata comes after a plus sign, as in 1.4.2+build.5. It is a label, and it is ignored when comparing.

Versions below 1.0.0 are a special case. The rule says that anything may change at any time. Many of our own libraries are at 0.1.x for that reason.

Why 1.10.0 is newer than 1.4.2

Compare as text, and "1.4.2" comes after "1.10.0", because the character 4 is larger than the character 1. That is wrong. Each part is a number, and 10 is larger than 4. A semver library compares number by number.

Let's try it in Mojo

Our mojo-semver library follows the exact rules of Python's semver package. Here is a short program. We ran it on Mojo 1.2.0.

from semver import parse


def main() raises:
    var v = parse("1.4.2")
    print(v.bump_patch(), v.bump_minor(), v.bump_major())
    print(parse("1.4.2") < parse("1.10.0"))
    print(parse("1.4.2-rc.1") < parse("1.4.2"))
    print(v.match(">=1.4.0"))
    print(v.bump_prerelease())

It printed:

1.4.3 1.5.0 2.0.0
True
True
True
1.4.2-rc.1

Read the output line by line.

  1. The three bumps show the reset rule. Patch gives 1.4.3, minor gives 1.5.0 and major gives 2.0.0.
  2. 1.4.2 is less than 1.10.0. Right, as numbers.
  3. A prerelease sorts before the final version.
  4. match(">=1.4.0") asks if the version fits a condition. It does.
  5. bump_prerelease() starts a candidate series with the token rc.

A habit for your own projects

If you publish a script or a library, even a small one, version it with these rules.

  1. Start at 0.1.0 while you are still changing things freely.
  2. Raise the patch number for every bug fix.
  3. Raise the minor number when you add a feature that keeps old code working.
  4. Raise the major number only when you must break something, and write what changed.
  5. Write the version in one place, so a tool can read it.

Our own Mojo libraries do this. Each version in the mojo-libs table is the one in that library's own pyproject.toml, and an index file is generated from them, so a stale row fails a check before it reaches the page. For example, mojo-semver is at 0.1.2 and mojo-imkit at 0.11.0. The 0 in front is the signal that these libraries are still young.

Mojo itself uses numbers in the same spirit. Our libraries build on Mojo 1.2 stable, and every week also on a nightly build with a version such as 1.3.0.dev2026110805. The part after dev is a build stamp, a kind of prerelease tag, which tells you it is a candidate for 1.3.0 and not the final one.

How it was checked

The README compares the library with Python semver 3.0.4 on 474 parse results, made of the standard's own examples and 400 generated versions. It also checks a 60 by 60 table of comparisons plus 880 more, 15 match expressions per version, every bump, and the error messages. All were byte for byte the same, and six of six planted bugs were caught.

On speed, on a Ryzen 7 7800X3D, parsing took 42 ns and comparing 9.4 ns, against 1,579 ns and 549 ns for the Python package. For a handful of versions, that does not matter. It matters when a tool compares thousands.

What it does not do

  • Major, minor and patch must fit in 18 digits. Python accepts any size.
  • Digits must be plain ASCII. Python's pattern also accepts other Unicode digits. This parser refuses them.
  • The Version(...) constructor does not check its input. Build versions from text with parse.
  • No caret or tilde ranges, because Python's semver has none to check against.

The library lives in mojo-libs under libs/semver. The older mojo-semver repository is read-only.

Words you may not know

  • Semver: semantic versioning, a rule set for what version numbers mean.
  • Prerelease: a version that is not final yet, such as a release candidate.
  • Build metadata: a label after a plus sign that does not change the order.
  • Bump: raise a version number by one step.
  • Parse: read text and split it into parts a program can use.

Try it

  1. Find a tool you use and read its version. Decide what a patch, minor and major bump to it would mean.
  2. Run the Mojo example above after pixi add mojo-semver, using the mojo-libs install steps.
  3. Open the mojo-semver repository and read its README table of calls.

Reacties

Nog geen reacties.

Inloggen om te reageren.