Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions .github/workflows/version_bump.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
name: Version bump

# Raises VERSION on main each time a change enters it. It creates no tags: tags belong to the release.
# Contributors work on forks, so the bump cannot happen in the pull request. It happens after the merge.
#
# A run works on the tip of main, not on the commit that started it, and GitHub keeps only the newest run that is
# waiting. Merges that land close together can therefore share one version.
#
# The reference is the highest VERSION that main has held before its tip, not the content of VERSION at the tip, so a
# stale VERSION cannot move the version backwards or repeat one.
# Only main's own history counts (first parent), not the commits inside a pull request.
#
# VERSION at the tip is above that -> a maintainer set it (for example 1.1.0). It stays as it is.
# otherwise -> the patch number of the highest one goes up by one (1.0.9 -> 1.0.10), in a
# commit pushed to main.
#
# A minor or major step is a pull request by a maintainer that sets VERSION to the new version.
#
# The bump commit goes straight to main, which needs a token that may bypass the rule that asks for pull requests.
# Add a repository secret VERSION_BUMP_TOKEN (a GitHub App token or a PAT with contents:write) and let it bypass.
# With the default GITHUB_TOKEN the push is refused while main is protected.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 3 lines could be replaced by "The bump commit goes straight to main, so uses VERSION_BUMP_TOKEN to bypass the rule asking for pull requests.". Most of their content right now is the LLM giving guidelines about how to set up the bump workflow and should not end up as code comment.


on:
push:
branches: [main]

permissions:
contents: write

concurrency:
group: version-bump
cancel-in-progress: false

jobs:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this job should run only in this repo, not in the forks so probably needs something like if: github.repository == 'mlcommons/chakra'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

otherwise forks syncing will become merges isntead of fast forwards i think

bump:
# [skip ci] already stops the bump commit from starting this workflow again. The actor check is a second guard.
if: github.actor != 'github-actions[bot]' && !contains(github.event.head_commit.message, '[skip ci]')
runs-on: ubuntu-latest
steps:
- name: Warn when the bump token is missing
env:
HAS_TOKEN: ${{ secrets.VERSION_BUMP_TOKEN != '' }}
run: |
if [ "$HAS_TOKEN" != "true" ]; then
echo "::warning::VERSION_BUMP_TOKEN is not set. The push to main fails while main is protected."
fi

# The tip of main, not the commit that started this run: a merge may have landed since.
# The whole history, because the versions main has held are read from it.
- uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
token: ${{ secrets.VERSION_BUMP_TOKEN || github.token }}

- name: Bump VERSION above the highest one main has held, unless a maintainer raised it
run: |
set -euo pipefail
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

for attempt in 1 2 3; do
git fetch -q origin main
git reset -q --hard origin/main

current=$(tr -d '[:space:]' < VERSION)
# Every value VERSION had on main before the tip: the first-parent commits that changed it.
held=()
if git rev-parse -q --verify HEAD^ > /dev/null; then
while read -r commit; do
held+=("$(git show "$commit:VERSION" 2>/dev/null | tr -d '[:space:]')")
done < <(git rev-list --first-parent HEAD^ -- VERSION)
fi
decision=$(python3 - "$current" "${held[@]}" <<'PY'
import re, sys

current, *held = sys.argv[1:]
number = r"(0|[1-9]\d*)"
version = rf"{number}\.{number}\.{number}"
if not re.fullmatch(version, current):
sys.exit(f"VERSION must look like X.Y.Z, got {current!r}")


def key(text):
return tuple(map(int, text.split(".")))


# A value that main held before, even a bad one that was fixed later, counts only if it is a version.
previous = [value for value in held if re.fullmatch(version, value)]
if not previous or key(current) > key(max(previous, key=key)):
print("keep", current)
else:
highest = max(previous, key=key)
if key(current) < key(highest):
print(f"::warning::VERSION {current} is below {highest}, which main held before, counting from {highest}", file=sys.stderr)
major, minor, patch = key(highest)
print("bump", f"{major}.{minor}.{patch + 1}")
PY
) || { echo "::error::Could not work out the next version."; exit 1; }
action=${decision%% *}
new=${decision#* }
if [ -z "$new" ] || [ "$new" = "$decision" ]; then
echo "::error::Unexpected result: '$decision'"
exit 1
fi

if [ "$action" = keep ]; then
echo "VERSION $new is above every version main held before: it stays as it is"
exit 0
fi

echo "VERSION $current -> $new"
echo "$new" > VERSION
git add VERSION
git commit -q -m "chore: bump version to $new [skip ci]"
# A rejected push means main moved, so start again from its new tip.
if git push origin HEAD:main; then
exit 0
fi
echo "push rejected, main moved (attempt $attempt), trying again"
done
echo "::error::Could not push the version bump after 3 attempts."
exit 1
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.1
14 changes: 12 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "mlc-chakra"
requires-python = ">=3.11,<3.15"
version = "1.0.0"
dynamic = ["version"]
readme = "README.md"
license = {file = "LICENSE.md"}
authors = [
Expand All @@ -27,7 +27,11 @@ Homepage = "https://github.com/mlcommons/chakra"
Documentation = "https://github.com/mlcommons/chakra/README.md"
Repository = "https://github.com/mlcommons/chakra.git"

[tool.setuptools.dynamic]
version = {file = "VERSION"}

[tool.setuptools.package-dir]
"chakra" = "src/chakra_pkg"
"chakra.schema.protobuf" = "schema/protobuf"
"chakra.src.converter" = "src/converter"
"chakra.src.generator" = "src/generator"
Expand Down Expand Up @@ -106,10 +110,16 @@ dev = [

[tool.uv]
default-groups = ["dev"]
# Rebuild the package, and so the generated *_pb2.py files, when a .proto file changes. The default keys miss it.
# chakra is a real package (it holds __version__), and the default editable finder cannot resolve chakra.src.* under it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This "chakra is a real package" phrasing is strange.

# strict links the files into a tree that has the same layout as the wheel.
config-settings = { editable_mode = "strict" }
# Rebuild the package, and so the generated *_pb2.py files, when a .proto file changes, and so that a new module is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

these will probably require a careful rebasing after the proto PR#282.

# linked into the strict editable tree. The default keys miss both.
cache-keys = [
{ file = "pyproject.toml" },
{ file = "setup.py" },
{ file = "setup.cfg" },
{ file = "schema/protobuf/*.proto" },
{ file = "src/**/*.py" },
{ file = "VERSION" },
]
24 changes: 18 additions & 6 deletions scripts/wheel_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,34 @@ wheel=$(echo "$work"/dist/*.whl)
uv venv --quiet "$work/venv"
uv pip install --quiet --python "$python" "$wheel"

# 1. The wheel holds the generated protobuf modules and the .proto files.
listing=$("$python" -m zipfile -l "$wheel")
for file in chakra/schema/protobuf/et_def_pb2.py chakra/schema/protobuf/storage_pb2.py \
# Every python call below uses -I (isolated): without it Python also looks for packages and metadata in the current
# directory, and a stale *.egg-info in the repository root would win over the wheel that is being checked.

# 1. The wheel holds the package __init__, the generated protobuf modules and the .proto files.
listing=$("$python" -I -m zipfile -l "$wheel")
for file in chakra/__init__.py chakra/schema/protobuf/et_def_pb2.py chakra/schema/protobuf/storage_pb2.py \
chakra/schema/protobuf/et_def.proto chakra/schema/protobuf/storage.proto; do
if ! grep -q "$file" <<<"$listing"; then
echo "wheel_check: $file is missing from the wheel" >&2
exit 1
fi
done

# 1b. The version in the wheel METADATA and in chakra.__version__ is the one in the VERSION file.
expected=$(tr -d '[:space:]' <"$source/VERSION")
metadata_version=$("$python" -I -c "from importlib.metadata import version; print(version('mlc-chakra'))")
module_version=$("$python" -I -c "import chakra; print(chakra.__version__)")
if [ "$metadata_version" != "$expected" ] || [ "$module_version" != "$expected" ]; then
echo "wheel_check: VERSION is $expected, METADATA says $metadata_version, chakra.__version__ is $module_version" >&2
exit 1
fi

# 2. Import the generated modules with the newest protobuf, then with the lowest one that pyproject.toml allows.
check='import chakra.schema.protobuf.et_def_pb2, chakra.schema.protobuf.storage_pb2'
"$python" -c "$check"
floor=$("$python" -c "import re; from importlib.metadata import requires; print(next(re.search(r'>=([0-9.]+)', r).group(1) for r in requires('mlc-chakra') if r.startswith('protobuf')))")
"$python" -I -c "$check"
floor=$("$python" -I -c "import re; from importlib.metadata import requires; print(next(re.search(r'>=([0-9.]+)', r).group(1) for r in requires('mlc-chakra') if r.startswith('protobuf')))")
uv pip install --quiet --python "$python" "protobuf==$floor"
"$python" -c "$check"
"$python" -I -c "$check"

# 3. The commands start. With no arguments, argparse exits with 2, as in the CI end-to-end workflow.
for command in chakra_trace_link chakra_converter chakra_jsonizer chakra_timeline_visualizer chakra_visualizer; do
Expand Down
8 changes: 8 additions & 0 deletions src/chakra_pkg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Chakra execution trace tools."""

from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("mlc-chakra")
except PackageNotFoundError:
__version__ = "0+unknown"
3 changes: 2 additions & 1 deletion tests/test_tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def test_editable_install_builds_and_imports(tmp_path: Path) -> None:
venv = tmp_path / "venv"
python = venv / "bin" / "python"
subprocess.run(["uv", "venv", "-q", "--python", "3.11", str(venv)], check=True)
subprocess.run(["uv", "pip", "install", "-q", "--python", str(python), "-e", str(ROOT)], check=True)
# cwd=ROOT: `uv pip` takes config-settings (editable_mode) from the pyproject.toml of the working directory
subprocess.run(["uv", "pip", "install", "-q", "--python", str(python), "-e", str(ROOT)], check=True, cwd=ROOT)
subprocess.run([str(python), "-c", "import chakra.schema.protobuf.et_def_pb2"], check=True)


Expand Down
Loading