Raven-1/scripts/publish_beta.py
zhanghui 010ca4b44d
fix(*): probe raven web before launch and install plugins from the release list (#588)
## Summary

The one-line installers are served from `main` and install the latest
published release, so an ending on `raven web` (PR #476) shipped against
v0.1.13, which has no `web`: since 2026-09-17 every `curl
raven.evermind.ai/install.sh | sh` has ended in `No such command 'web'`
with exit code 2. This PR fixes that class of failure and the upgrade
defect behind the next release.

- **Probe rule.** Every `raven <sub>` call in `install.sh` and
`install.ps1` is preceded by `raven <sub> --help`. When the probe fails
the install ends on the one command every release has (`raven`), and
neither the stop nor the page's own exit code becomes the script's.
`RAVEN_NO_LAUNCH=1` skips the launch (CI, Dockerfiles); the PATH hint
the launch displaced is back.
- **The release says what it ships.** `release.yml` writes
`raven-plugins.txt`, one `name @ url` line per plugin wheel it just
built, beside the wheel and the constraints. Both installers derive
every URL from the release page redirect and hand the list to `uv tool
install --with-requirements`. No GitHub API call, no grepping asset
names out of its JSON. A release without a list (0.1.13 and older)
installs raven alone, as it effectively did before.
- **Upgrade keeps the plugins.** uv replaces a tool's requirement set
with what one command names, so the old helper (`raven[channels]` only)
uninstalled every plugin the installer had put in. The helper now
installs from the same list, descends one loss at a time (engines, then
the memory plugin, then the channel extras), and refuses to run without
the list rather than install raven alone. `raven upgrade` on an
up-to-date install whose list names a plugin it lacks reinstalls the
version with its plugins. The helper also moves a beta wheel URL's
deploy token into the request header (urllib reads `user:token@host` as
a host name) and copies it onto the list's URLs for uv.
- **Gates.** CI installs the latest release with the PR's scripts,
piped, on ubuntu and windows whenever an installer changes. The release
job installs itself from `dist/` with `install.sh` before the draft is
created and asserts the three plugin packages and `web` on the result.
The beta publisher builds and lists the same three plugin wheels. A
contract test holds the release workflow, the beta publisher, both
installers (remote list and local clone mode) and the helper to one
plugin list.

Decisions, for the reviewer: the plugins are not published to PyPI and
no names are reserved there; the list carries direct URLs, so no plugin
is ever resolved by name against an index. The LibreOffice and chromium
steps in the scripts are unchanged. The three hosted-memory plugins
(mem0, zep, memos) are built and uploaded as before but stay off the
default list.

## Type

- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

All installs below ran piped (`cat install.sh | sh`) in isolated
directories (own HOME, UV_TOOL_DIR, UV_TOOL_BIN_DIR, UV_CACHE_DIR,
RAVEN_HOME) with `RAVEN_MINIMAL=1`.

- `main`'s `install.sh` against the live latest release (v0.1.13): exit
2, `No such command 'web'`. This branch's `install.sh`, same
environment: exit 0, `Raven installed. Open a new terminal ... then run:
raven`; `raven --version` prints 0.1.13. With `RAVEN_NO_LAUNCH=1`: exit
0.
- A local release directory built by `scripts/publish_beta.py` (`_build`
at 0.1.14b1: raven wheel, three plugin wheels, constraints, list): this
branch's `install.sh` with `RAVEN_WHEEL_URL=file://...` installs raven
0.1.14b1, `python -c "import raven_everos, raven_design, raven_ppt"`
succeeds in the tool venv, and the uv receipt lists raven plus the three
plugins.
- The release workflow's two new step bodies (`Write the plugin list`,
`Install the release from dist with install.sh`) extracted and run
locally against that build: exit 0; removing one plugin wheel makes the
list step exit 1.
- The script's ending against the 0.1.14b1 install: `raven web --help`
exit 0, `raven web --stop` exit 0 with nothing to stop, `raven web
--foreground --port 18999` answers `GET /` with HTTP 200 after about 20
seconds.
- `uv run raven upgrade` and `uv run raven upgrade --check` from the
checkout against live GitHub: `Raven 0.1.13 is up to date.`, exit 0.
- `uv run pytest tests/integration/test_cli_upgrade_real_uv.py`: 2
passed. The test now installs the old fixture `--with` a companion wheel
and asserts the companion survives the upgrade; against `main`'s helper
the same test fails on that assertion.
- `uv run pytest tests/test_install_script.py
tests/test_release_plugin_list.py tests/test_updates_upgrade.py
tests/test_publish_beta.py tests/test_cli_smoke.py`: all pass (37 + 6 +
188 on the last run of each).
- `uv run pytest` (full suite, before the last installer rewrite): 23543
passed, 121 skipped, 4 failed. The 4 (`test_agents_oncall_launcher`,
`test_agents_code_launcher`, `test_agent_loop_token_budget`,
`test_ppt_engine_image_search`) fail identically on plain `github/main`
in a separate worktree.
- `uv run ruff check` and `uv run ruff format --check` on every touched
Python file: clean. `sh -n install.sh`, `bash -n install.sh`: clean.
Both workflow files parse.
- Not run here: the new CI job's Windows leg and the release gate inside
GitHub Actions (this PR is their first run). `install.ps1` has no local
PowerShell to execute it; its changes mirror `install.sh` and are pinned
by the text tests.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed

## Risk

User-visible changes: the installers no longer fail on a release without
`web`; they print the plugins they install and a warning when a release
carries none; `raven upgrade` refuses to proceed when the target
release's plugin list cannot be fetched (previously it would install
raven alone and drop the plugins), and repairs an up-to-date install
that lacks listed plugins. The beta channel's next publish carries the
three plugin wheels and the list; a beta upgrade to a build published
before this change will refuse for lack of a list, and rerunning the
beta installer is the remedy.

Rollback: revert the commit. Releases published with `raven-plugins.txt`
keep working with the old scripts, which ignore the file.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

N/A

---------

Co-authored-by: Claude (claude-fable-5-1) <noreply@anthropic.com>
2026-09-21 16:25:33 +08:00

340 lines
14 KiB
Python

"""Build the current checkout and publish it to the beta channel.
Run from a clean worktree after merging what testers should get:
make beta
The build steps mirror ``.github/workflows/release.yml`` exactly -- the TUI
bundle and the served page are gitignored build artifacts, so a wheel built
without them installs a Raven whose `raven tui` cannot start and whose page
answers "No front end built here".
Versions are ``<next patch>b<serial>``: a beta of 0.1.11 is 0.1.12b1, which
orders above released 0.1.11 and below an eventual 0.1.12. The serial comes
from whatever the channel already points at, so publishing twice in a row
gives b1 then b2.
The publish credential is a GitLab deploy token scoped to
``write_package_registry`` on the distribution project, read from
``RAVEN_BETA_PUBLISH_TOKEN`` or from ``~/.raven/beta_publish.json``. It is not
the token testers hold: theirs only reads.
"""
from __future__ import annotations
import argparse
import json
import os
import re
import shutil
import subprocess
import sys
import tomllib
from pathlib import Path
import httpx
_HOST = "gitlab.com"
_TIMEOUT_S = 120.0
_POINTER_PATH = "latest/latest.json"
_VERSION_RE = re.compile(r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$")
_PYPROJECT_VERSION_RE = re.compile(r'^version = "[^"]*"$', re.MULTILINE)
class PublishError(RuntimeError):
pass
def _repo_root() -> Path:
return Path(__file__).resolve().parent.parent
def _run(argv: list[str], *, cwd: Path) -> None:
print(f" $ {' '.join(argv)}", flush=True)
result = subprocess.run(argv, cwd=cwd) # noqa: S603 - argv is built here, not from input
if result.returncode != 0:
raise PublishError(f"Command failed ({result.returncode}): {' '.join(argv)}")
def _credentials() -> tuple[str, str, str]:
"""``(project, publish token, tester token)``, or raise with how to supply them.
Two tokens because they are two different powers: the publish one writes to
the registry, the tester one only reads it and is what gets baked into the
installer we hand out.
"""
from raven.config.loader import raven_home
path = raven_home() / "beta_publish.json"
data: dict[str, object] = {}
try:
parsed = json.loads(path.read_text(encoding="utf-8"))
if isinstance(parsed, dict):
data = parsed
except (OSError, ValueError):
pass
def _field(env: str, key: str) -> str:
return (os.environ.get(env, "").strip()) or str(data.get(key, "")).strip()
project = _field("RAVEN_BETA_PROJECT", "project")
publish_token = _field("RAVEN_BETA_PUBLISH_TOKEN", "publish_token")
read_token = _field("RAVEN_BETA_READ_TOKEN", "read_token")
if not project or not publish_token:
raise PublishError(
f"No publish credentials. Set RAVEN_BETA_PROJECT and RAVEN_BETA_PUBLISH_TOKEN, or write {path}"
)
return project, publish_token, read_token
def _api_base(project: str) -> str:
return f"https://{_HOST}/api/v4/projects/{project}/packages/generic/raven"
def _published_version(api_base: str, token: str) -> str | None:
"""The version the channel currently points at, or ``None`` for an empty one."""
with httpx.Client(timeout=_TIMEOUT_S, follow_redirects=True) as client:
response = client.get(f"{api_base}/{_POINTER_PATH}", headers={"DEPLOY-TOKEN": token})
if response.status_code == 404:
return None
response.raise_for_status()
payload = response.json()
if not isinstance(payload, dict):
return None
version = payload.get("version")
return version if isinstance(version, str) else None
def _next_version(base_version: str, published: str | None) -> str:
"""``<next patch>b<serial>``, continuing the serial when the base is unchanged."""
match = _VERSION_RE.fullmatch(base_version)
if match is None:
raise PublishError(f"pyproject version is not X.Y.Z: {base_version}")
major, minor, patch = (int(part) for part in match.groups())
target = f"{major}.{minor}.{patch + 1}"
serial = 1
if published is not None:
prior = re.fullmatch(r"(\d+\.\d+\.\d+)b(\d+)", published)
if prior is not None and prior.group(1) == target:
serial = int(prior.group(2)) + 1
return f"{target}b{serial}"
# The plugin distributions a complete install carries beside raven. The release
# workflow builds the same three (tests/test_release_plugin_list.py holds the
# two lists equal); the beta channel has to ship them too, or the upgrade
# helper, which refuses to install raven without its version's plugin list,
# would have nothing to read.
PLUGIN_DISTRIBUTIONS = ("everos-memory", "design-engine", "ppt-engine")
PLUGIN_LIST_NAME = "raven-plugins.txt"
def _plugin_list(wheels: list[Path], base_url: str) -> str:
"""The ``name @ url`` lines the upgrade helper installs from, one per wheel.
The name comes from the wheel's own filename (``everos_memory-1.2.0-...``),
so the list can only ever name what was built.
"""
lines = []
for wheel in wheels:
name = wheel.name.split("-", 1)[0].replace("_", "-")
lines.append(f"{name} @ {base_url}/{wheel.name}")
return "".join(line + "\n" for line in lines)
def _build(root: Path, version: str) -> tuple[Path, Path, list[Path]]:
"""Build the wheels at ``version``, returning ``(wheel, constraints, plugin wheels)``.
The version reaches the wheel through pyproject, which is restored before
this returns whether the build worked or not -- a half-published beta must
not leave the checkout claiming to be one.
"""
dist = root / "dist"
if dist.exists():
shutil.rmtree(dist)
print("Building the TUI bundle...", flush=True)
_run(["npm", "ci"], cwd=root / "ui-tui")
_run(["npm", "run", "build"], cwd=root / "ui-tui")
if not (root / "ui-tui/dist/entry.js").is_file():
raise PublishError("ui-tui/dist/entry.js did not land")
print("Building the served page...", flush=True)
_run(["npm", "ci", "--prefix", "ui-web"], cwd=root)
_run(["npm", "run", "--prefix", "ui-web", "build"], cwd=root)
_run([sys.executable, "ui-web/build.py"], cwd=root)
if not (root / "ui-web/dist/index.html").is_file():
raise PublishError("ui-web/dist/index.html did not land")
pyproject = root / "pyproject.toml"
original = pyproject.read_text(encoding="utf-8")
stamped, count = _PYPROJECT_VERSION_RE.subn(f'version = "{version}"', original, count=1)
if count != 1:
raise PublishError("Could not find the version line in pyproject.toml")
# uv.lock records the project's own version, so `uv export` rewrites it to
# match the stamp and leaves the checkout claiming to be a beta. That
# survives into whatever you commit next, and a lockfile disagreeing with
# pyproject is exactly what `uv export --locked` fails the release on.
lock = root / "uv.lock"
original_lock = lock.read_text(encoding="utf-8") if lock.is_file() else None
try:
pyproject.write_text(stamped, encoding="utf-8")
print(f"Building the wheel at {version}...", flush=True)
_run(["uv", "build", "--wheel"], cwd=root)
for plugin in PLUGIN_DISTRIBUTIONS:
_run(["uv", "build", "--wheel", f"plugins-dist/{plugin}", "-o", "dist"], cwd=root)
_run(
["uv", "export", "--all-extras", "--no-hashes", "--no-emit-workspace", "-o", "dist/raven-constraints.txt"],
cwd=root,
)
finally:
pyproject.write_text(original, encoding="utf-8")
if original_lock is not None:
lock.write_text(original_lock, encoding="utf-8")
wheel = dist / f"raven-{version}-py3-none-any.whl"
constraints = dist / "raven-constraints.txt"
if not wheel.is_file():
# Listed defensively: a build that failed early leaves no dist/ at all,
# and an iterdir() on it would replace the real reason with an
# unrelated FileNotFoundError.
found = sorted(p.name for p in dist.iterdir()) if dist.is_dir() else "no dist/ directory"
raise PublishError(f"Expected {wheel.name} in dist/, found: {found}")
if not constraints.is_file():
raise PublishError("Constraints export did not land")
plugin_wheels = []
for plugin in PLUGIN_DISTRIBUTIONS:
matches = sorted(dist.glob(f"{plugin.replace('-', '_')}-*.whl"))
if len(matches) != 1:
raise PublishError(f"Expected one {plugin} wheel in dist/, found: {[m.name for m in matches]}")
plugin_wheels.append(matches[0])
_verify_wheel(wheel)
return wheel, constraints, plugin_wheels
def _verify_wheel(wheel: Path) -> None:
"""The same gate the release workflow applies, for the same reason."""
import zipfile
names = zipfile.ZipFile(wheel).namelist()
if "raven/ui-tui/dist/entry.js" not in names:
raise PublishError("entry.js missing from wheel")
if "raven/ui/dist/index.html" not in names:
raise PublishError("index.html missing from wheel")
if not any(name.startswith("raven/ui/dist/assets/") for name in names):
raise PublishError("page assets missing from wheel")
if any("node_modules" in name for name in names):
raise PublishError("node_modules leaked into wheel")
# The agent-product tree degrades quietly in the other direction: a wheel built
# from an sdist simply has no folders, and onboarding step 5 reports an empty
# installation rather than failing.
if not any(name.endswith("/subagent.json") for name in names):
raise PublishError("agent products missing from wheel")
leaked = [name for name in names if _is_secret_entry(name)]
if leaked:
raise PublishError(f"secrets leaked into wheel: {leaked}")
def _is_secret_entry(name: str) -> bool:
"""A filled-in secrets file rather than the template beside it."""
leaf = name.rsplit("/", 1)[-1]
if not (leaf == ".env" or leaf.startswith(".env.")):
return False
return not leaf.endswith((".example", ".sample", ".template"))
def _upload(client: httpx.Client, url: str, token: str, body: bytes) -> None:
response = client.put(url, headers={"DEPLOY-TOKEN": token}, content=body)
response.raise_for_status()
def _installer_script(root: Path, project: str, read_token: str) -> bytes:
"""``beta.sh`` with the channel filled in.
The repository copy carries placeholders so the credential lives only in
the registry, behind that same credential.
"""
text = (root / "beta.sh").read_text(encoding="utf-8")
for placeholder, value in (("__RAVEN_BETA_PROJECT__", project), ("__RAVEN_BETA_TOKEN__", read_token)):
if placeholder not in text:
raise PublishError(f"beta.sh no longer contains {placeholder}")
text = text.replace(placeholder, value)
return text.encode("utf-8")
def _publish(
api_base: str,
token: str,
version: str,
wheel: Path,
constraints: Path,
plugin_wheels: list[Path],
installer: bytes | None,
) -> None:
"""Upload the build, then move the pointer -- never the other way round.
The pointer is what every tester polls, so it goes last: until it moves,
the channel still names a version that is fully uploaded.
"""
with httpx.Client(timeout=_TIMEOUT_S, follow_redirects=True) as client:
print(f"Uploading {wheel.name} ({wheel.stat().st_size // 1024} KiB)...", flush=True)
_upload(client, f"{api_base}/{version}/{wheel.name}", token, wheel.read_bytes())
print("Uploading raven-constraints.txt...", flush=True)
_upload(client, f"{api_base}/{version}/raven-constraints.txt", token, constraints.read_bytes())
for plugin_wheel in plugin_wheels:
print(f"Uploading {plugin_wheel.name} ({plugin_wheel.stat().st_size // 1024} KiB)...", flush=True)
_upload(client, f"{api_base}/{version}/{plugin_wheel.name}", token, plugin_wheel.read_bytes())
# Plain URLs: the helper copies the wheel URL's credentials onto them.
print(f"Uploading {PLUGIN_LIST_NAME}...", flush=True)
plugin_list = _plugin_list(plugin_wheels, f"{api_base}/{version}").encode("utf-8")
_upload(client, f"{api_base}/{version}/{PLUGIN_LIST_NAME}", token, plugin_list)
if installer is not None:
print("Uploading beta.sh...", flush=True)
_upload(client, f"{api_base}/latest/beta.sh", token, installer)
print("Moving the pointer...", flush=True)
pointer = json.dumps({"version": version}).encode("utf-8")
_upload(client, f"{api_base}/{_POINTER_PATH}", token, pointer)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Publish this checkout to the Raven beta channel.")
parser.add_argument("--version", help="Publish exactly this version instead of the next serial.")
parser.add_argument("--dry-run", action="store_true", help="Build and report, upload nothing.")
args = parser.parse_args(argv)
root = _repo_root()
try:
project, publish_token, read_token = _credentials()
api_base = _api_base(project)
base_version = tomllib.loads((root / "pyproject.toml").read_text(encoding="utf-8"))["project"]["version"]
published = _published_version(api_base, publish_token)
version = args.version or _next_version(base_version, published)
print(f"Channel is at {published or '(empty)'}; publishing {version}\n", flush=True)
installer = _installer_script(root, project, read_token) if read_token else None
if installer is None:
print("note: no tester token configured, so beta.sh will not be refreshed", flush=True)
wheel, constraints, plugin_wheels = _build(root, version)
if args.dry_run:
built = ", ".join([wheel.name] + [p.name for p in plugin_wheels])
print(f"\nDry run: built {built}, uploaded nothing.")
return 0
_publish(api_base, publish_token, version, wheel, constraints, plugin_wheels, installer)
except PublishError as exc:
print(f"error: {exc}", file=sys.stderr)
return 1
except httpx.HTTPError as exc:
print(f"error: publishing failed: {exc}", file=sys.stderr)
return 1
print(f"\nPublished {version}.")
print("Every tester's gateway polls this pointer once a minute, so they are offered it within about that.")
return 0
if __name__ == "__main__":
raise SystemExit(main())