Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.
Python aiavx512cpu-inferenceedge-aiggufinference-enginellama-cppllmllm-inferencelocal-ailocal-llmno-gpuon-device-aiopenai-apiopenai-compatiblequantizationqwenself-hostedspeculative-decodingternary
fix(install): validate prebuilt binaries before replacing the working engine (#72)
Afloatd89743b4c02a
diff --git a/install.sh b/install.shindex 23d7147..1d723f5 100755--- a/install.sh+++ b/install.sh@@ -125,10 +125,31 @@ if [ "$MODEL_ONLY" = 1 ]; then:elif [ -n "$BIN_URL" ]; thensay "Fetching prebuilt binaries: $BIN_URL"- curl -fSL --retry 3 "$BIN_URL" | tar -xz -C "$PREFIX/bin" --strip-components=1 \+ BIN_STAGE=$(mktemp -d "$PREFIX/.bin.XXXXXX")+ BIN_PREVIOUS=$BIN_STAGE.previous+ cleanup_bin_update() {+ local update_status=$?+ trap - EXIT+ trap '' INT TERM+ if [ -d "$BIN_PREVIOUS" ] && [ ! -e "$PREFIX/bin" ]; then+ if ! mv "$BIN_PREVIOUS" "$PREFIX/bin"; then+ echo "could not restore the previous engine; binaries remain in $BIN_PREVIOUS" >&2+ [ "$update_status" != 0 ] || update_status=1+ fi+ fi+ if ! rm -rf "$BIN_STAGE"; then+ echo "could not remove the staging directory $BIN_STAGE" >&2+ [ "$update_status" != 0 ] || update_status=1+ fi+ exit "$update_status"+ }+ trap cleanup_bin_update EXIT+ trap 'exit 130' INT+ trap 'exit 143' TERM+ curl -fSL --retry 3 "$BIN_URL" | tar -xz -C "$BIN_STAGE" --strip-components=1 \|| die "could not download or unpack $BIN_URL"# a prebuilt engine only runs on the distribution it was built on or a newer one: find out now, before the download- engine_starts() { ver=$(LD_LIBRARY_PATH="$PREFIX/bin" "$PREFIX/bin/llama-server" --version 2>&1); }+ engine_starts() { ver=$(LD_LIBRARY_PATH="$BIN_STAGE" "$BIN_STAGE/llama-server" --version 2>&1); }if ! engine_starts && case $ver in *libgomp.so.1*) true ;; *) false ;; esac; thensay "Installing: libgomp1"; apt_install libgomp1 || true # the one runtime library a bare system often lacksfi@@ -138,6 +159,16 @@ elif [ -n "$BIN_URL" ]; thenA missing library or a 'version ... not found' line means it was built on a newer distribution than this one.Run plain ./install.sh to build the engine here instead."fi+ mv "$PREFIX/bin" "$BIN_PREVIOUS"+ if mv "$BIN_STAGE" "$PREFIX/bin"; then+ :+ else+ update_status=$?+ echo "could not publish the prebuilt engine; restoring the previous binaries" >&2+ exit "$update_status"+ fi+ rm -rf "$BIN_PREVIOUS"+ trap - EXIT INT TERMelsestamp="$FORK_SHA portable=$PORTABLE $(cat "$HERE"/patches/*.patch "$HERE"/kernels/* | cksum | cut -d' ' -f1)"fidiff --git a/tests/test_install_prebuilt_transaction.py b/tests/test_install_prebuilt_transaction.pynew file mode 100644index 0000000..c910d2b--- /dev/null+++ b/tests/test_install_prebuilt_transaction.py@@ -0,0 +1,122 @@+import os+import io+import tarfile+from pathlib import Path+import shutil+import subprocess+import tempfile+import unittest++ROOT = Path(__file__).resolve().parents[1]+++class InstallRegression(unittest.TestCase):+ def setUp(self):+ self.temp = tempfile.TemporaryDirectory()+ self.addCleanup(self.temp.cleanup)+ self.work = Path(self.temp.name)+ self.prefix = self.work / "installation"+ self.tools = self.work / "tools"+ self.tools.mkdir()+ (self.prefix / "bin").mkdir(parents=True)+ (self.prefix / "models").mkdir()+ self.command(self.prefix / "bin/llama-server", "#!/bin/sh\nexit 0\n")+ for name in ("dirname", "uname", "grep", "cut", "sed", "awk", "cat", "mkdir", "id",+ "mv", "rm", "touch", "tar", "gzip", "head", "mktemp", "basename", "flock"):+ executable = shutil.which(name)+ if executable:+ (self.tools / name).symlink_to(executable)+ self.command(self.tools / "df", "#!/bin/sh\nprintf 'Filesystem 1024-blocks Used Available Capacity Mounted\nfixture 999999999 0 999999999 0%% /\n'\n")+ self.command(self.tools / "du", "#!/bin/sh\nprintf '0 fixture\n'\n")+ self.command(self.tools / "python3", "#!/bin/sh\nexit 0\n")+ self.command(self.tools / "curl", "#!/bin/sh\nprintf download >> \"$DOWNLOAD_LOG\"\nwhile [ \"$#\" -gt 0 ]; do if [ \"$1\" = -o ]; then shift; printf fixture > \"$1\"; fi; shift; done\n")+ self.env = {**os.environ, "OPENSET_LLM_HOME": str(self.prefix),+ "PATH": str(self.tools), "DOWNLOAD_LOG": str(self.work / "downloads")}+ self.env.pop("OPENSET_LLM_BIN_URL", None)+ self.env.pop("HF_TOKEN", None)++ def command(self, path, content):+ path.write_text(content)+ path.chmod(0o755)++ def install(self, *args):+ return subprocess.run(["/bin/bash", str(ROOT / "install.sh"), *args],+ env=self.env, text=True, capture_output=True, timeout=15)++ def archive(self, truncate=False, executable=True):+ payload = b'#!/bin/sh\nexit 0\n# replacement engine\n' if executable else b'#!/bin/sh\nexit 12\n'+ stream = io.BytesIO()+ with tarfile.open(fileobj=stream, mode='w:gz') as tar:+ entry = tarfile.TarInfo('openset-llm/llama-server')+ entry.mode = 0o755+ entry.size = len(payload)+ tar.addfile(entry, io.BytesIO(payload))+ data = stream.getvalue()+ archive = self.work / 'engine.tar.gz'+ archive.write_bytes(data[:-8] if truncate else data)+ self.env['ARCHIVE'] = str(archive)+ self.command(self.tools / 'curl', '#!/bin/sh\ncat "$ARCHIVE"\n')+ (self.prefix / 'models/model.gguf').write_bytes(b'cached')+ return payload++ def test_truncated_archive_preserves_the_working_engine(self):+ before = (self.prefix / 'bin/llama-server').read_bytes()+ self.archive(truncate=True)+ result = self.install('--bin-url', 'https://example.invalid/engine.tar.gz', '--no-mtp')+ self.assertNotEqual(result.returncode, 0)+ self.assertEqual((self.prefix / 'bin/llama-server').read_bytes(), before)+ self.assertEqual(list(self.prefix.glob('.bin.*')), [])++ def test_unusable_replacement_preserves_the_working_engine(self):+ before = (self.prefix / 'bin/llama-server').read_bytes()+ self.archive(executable=False)+ result = self.install('--bin-url', 'https://example.invalid/engine.tar.gz', '--no-mtp')+ self.assertNotEqual(result.returncode, 0)+ self.assertEqual((self.prefix / 'bin/llama-server').read_bytes(), before)++ def test_successful_replacement_removes_obsolete_libraries(self):+ (self.prefix / 'bin/obsolete.so').write_bytes(b'old backend')+ expected = self.archive()+ result = self.install('--bin-url', 'https://example.invalid/engine.tar.gz', '--no-mtp')+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertEqual((self.prefix / 'bin/llama-server').read_bytes(), expected)+ self.assertFalse((self.prefix / 'bin/obsolete.so').exists())++ def test_interruption_between_renames_restores_the_previous_engine(self):+ real_mv = shutil.which('mv')+ before = (self.prefix / 'bin/llama-server').read_bytes()+ for signal_name, exit_code in (('TERM', 143), ('INT', 130)):+ with self.subTest(signal=signal_name):+ (self.prefix / 'bin').mkdir(exist_ok=True)+ (self.prefix / 'bin/llama-server').write_bytes(before)+ self.archive()+ (self.tools / 'mv').unlink()+ self.command(self.tools / 'mv', '#!/bin/sh\n'+ f'"{real_mv}" "$@" || exit $?\n'+ 'if [ "$1" = "$OPENSET_LLM_HOME/bin" ]; then\n'+ f' kill -{signal_name} "$PPID"\n'+ 'fi\n')+ result = self.install('--bin-url', 'https://example.invalid/engine.tar.gz', '--no-mtp')+ self.assertEqual(result.returncode, exit_code, result.stderr)+ self.assertEqual((self.prefix / 'bin/llama-server').read_bytes(), before)+ self.assertEqual(list(self.prefix.glob('.bin.*')), [])++ def test_failed_publish_restores_engine_and_preserves_error_status(self):+ real_mv = shutil.which('mv')+ before = (self.prefix / 'bin/llama-server').read_bytes()+ self.archive()+ (self.tools / 'mv').unlink()+ self.command(self.tools / 'mv', '#!/bin/sh\n'+ 'case "$1" in\n'+ ' "$OPENSET_LLM_HOME"/.bin.*.previous) ;;\n'+ ' "$OPENSET_LLM_HOME"/.bin.*) exit 37 ;;\n'+ 'esac\n'+ f'exec "{real_mv}" "$@"\n')+ result = self.install('--bin-url', 'https://example.invalid/engine.tar.gz', '--no-mtp')+ self.assertEqual(result.returncode, 37, result.stderr)+ self.assertEqual((self.prefix / 'bin/llama-server').read_bytes(), before)+ self.assertEqual(list(self.prefix.glob('.bin.*')), [])+++if __name__ == '__main__':+ unittest.main()