agent-zero/tests/test_docker_initialize_limits.py
Alessandro 2afffe8edc
Some checks are pending
Build And Publish Docker Images / plan (push) Waiting to run
Build And Publish Docker Images / build (push) Blocked by required conditions
Overlap embedding warm-up with chat restoration
Load sentence-transformers on the existing cache miss and construct the
preloaded model off the shared background event loop. Start warm-up before
chat restoration inside extensible init_a0, preserving start-hook control
and sharing the loaded model with ordinary runtime requests.

Remove unused speech preload imports and dead warm-up functions. Seed APT
indexes only when absent, skipping the refresh on normal container boots.
Keep the existing Python tunnel service and its provider lifecycle.

Validation: 49 focused framework tests passed, including extension-hook
short-circuit behavior, concurrent embedding-cache reuse, APT cache states,
tunnel behavior and CSRF origins. Three cold Fable chat trials per version:
median submit-to-first-token 9.588s -> 4.234s;
local preparation 6.904s -> 1.197s;
HTTP readiness 42.637s -> 41.139s;
boot-to-first-token 52.093s -> 46.657s.
Provider timing varies; these are local repeated measurements. The live
baseline already omitted APT refresh, so it receives no timing credit.
2026-10-10 16:36:08 +02:00

91 lines
2.5 KiB
Python

import re
import resource
import subprocess
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[1]
INITIALIZE_SCRIPT = REPO_ROOT / "docker" / "run" / "fs" / "exe" / "initialize.sh"
def _script_function(name: str) -> str:
text = INITIALIZE_SCRIPT.read_text(encoding="utf-8")
match = re.search(rf"^{name}\(\) \{{\n.*?^\}}\n", text, re.M | re.S)
assert match, f"initialize.sh must define {name}"
return match.group(0)
def _run_bash(script: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["bash", "-c", script],
check=True,
text=True,
capture_output=True,
)
def test_initialize_raises_soft_open_file_limit_to_requested_target():
function = _script_function("raise_open_file_limit")
result = _run_bash(
f"""
set -euo pipefail
{function}
ulimit -S -n 1024
A0_NOFILE_LIMIT=4096
raise_open_file_limit
test "$(ulimit -S -n)" = "4096"
"""
)
assert "Raised open file soft limit from 1024 to 4096" in result.stdout
def test_initialize_caps_open_file_limit_at_hard_limit():
_soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
if hard != resource.RLIM_INFINITY and hard < 2048:
pytest.skip("host hard open-file limit is too low for this regression test")
function = _script_function("raise_open_file_limit")
result = _run_bash(
f"""
set -euo pipefail
{function}
ulimit -S -n 1024
ulimit -H -n 2048
A0_NOFILE_LIMIT=65535
raise_open_file_limit
test "$(ulimit -S -n)" = "2048"
"""
)
assert "Raised open file soft limit from 1024 to 2048" in result.stdout
@pytest.mark.parametrize("package_index", [None, "mirror_Packages", "mirror_Packages.lz4"])
def test_initialize_updates_only_an_empty_package_cache(tmp_path, package_index):
lists = tmp_path / "lists"
lists.mkdir()
(lists / "partial").mkdir()
(lists / "lock").touch()
if package_index:
(lists / package_index).write_text("cached package index")
calls = tmp_path / "apt-calls"
function = _script_function("initialize_package_lists").replace(
"/var/lib/apt/lists", str(lists)
)
_run_bash(f"""
set -euo pipefail
apt-get() {{ echo "$*" >> "{calls}"; }}
{function}
initialize_package_lists
wait
""")
assert (calls.read_text() if calls.exists() else "") == (
"" if package_index else "update\n"
)