diff --git a/.github/workflows/ci-unit-isolation.yml b/.github/workflows/ci-unit-isolation.yml new file mode 100644 index 00000000..a340314b --- /dev/null +++ b/.github/workflows/ci-unit-isolation.yml @@ -0,0 +1,91 @@ +name: CI Unit Isolation and Startup Safety +on: + pull_request: + paths: + - '.github/workflows/ci.yml' + - '.github/workflows/reusable-build-smoke-gate.yml' + - '.github/workflows/ci-unit-isolation.yml' + - 'docker/entrypoint.sh' + - 'scripts/init/30-start-services.sh' + - 'scripts/init/winepath-with-retry.sh' + - 'tests/test_winepath_startup_retry.py' + - 'scripts/ci/test.sh' + - 'scripts/ci/classify-startup-logs.py' + - 'tests/test_ci_unit_isolation.py' + +permissions: + contents: read + +jobs: + safe-startup-contract: + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - name: Checkout public source without saved credentials + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd + with: + persist-credentials: false + - name: Shell syntax and isolated CI regression + shell: bash + run: | + set -euo pipefail + bash -n docker/entrypoint.sh scripts/init/30-start-services.sh scripts/ci/test.sh + python3 tests/test_ci_unit_isolation.py + python3 -m py_compile scripts/ci/classify-startup-logs.py + - name: Validate GitHub Actions workflow YAML + shell: bash + run: | + set -euo pipefail + python3 -m pip install --disable-pip-version-check PyYAML==6.0.3 + python3 -c 'import yaml; from pathlib import Path; files = (".github/workflows/ci.yml", ".github/workflows/ci-unit-isolation.yml"); [yaml.safe_load(Path(p).read_text(encoding="utf-8")) for p in files]; print("PASS: workflow YAML parsed")' + + runtime-independent-unit-suite: + runs-on: ubuntu-24.04 + timeout-minutes: 40 + steps: + - name: Checkout public source without saved credentials + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd + with: + persist-credentials: false + - name: Build unit image without starting Wine + run: docker compose -f compose/docker-compose.yml --profile test --profile interactive build test-runner + - name: Execute real project unit suite with no service dependencies + run: docker compose -f compose/docker-compose.yml --profile test --profile interactive run --rm --no-deps test-runner scripts/ci/test.sh + - name: Cleanup test-only containers + if: always() + run: docker compose -f compose/docker-compose.yml --profile test --profile interactive down --remove-orphans || true + + bounded-interactive-startup: + runs-on: ubuntu-24.04 + timeout-minutes: 40 + steps: + - name: Checkout public source without saved credentials + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd + with: + persist-credentials: false + - name: Build exact WineBot interactive image on public runner + run: docker compose -f compose/docker-compose.yml --profile interactive build winebot-interactive + - name: Exercise real interactive health gate + id: startup + continue-on-error: true + run: docker compose -f compose/docker-compose.yml --profile interactive up --detach --wait --wait-timeout 150 winebot-interactive + - name: Classify startup failure without revealing service logs + if: ${{ always() && steps.startup.outcome == 'failure' }} + run: | + set -euo pipefail + cid="$(docker compose -f compose/docker-compose.yml --profile interactive ps -q winebot-interactive || true)" + if [ -z "$cid" ]; then + echo "STARTUP_DIAGNOSTIC container=absent" + else + docker inspect --format='STARTUP_DIAGNOSTIC status={{.State.Status}} exit_code={{.State.ExitCode}} oom={{.State.OOMKilled}}' "$cid" || true + fi + docker compose -f compose/docker-compose.yml --profile interactive logs --no-color --tail 200 winebot-interactive 2>/dev/null | + python3 scripts/ci/classify-startup-logs.py || true + - name: Reject unqualified interactive startup + if: always() + env: + STARTUP_OUTCOME: ${{ steps.startup.outcome }} + run: test "$STARTUP_OUTCOME" = success + - name: Stop isolated interactive service + if: always() + run: docker compose -f compose/docker-compose.yml --profile interactive down --remove-orphans || true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 55113741..47b8b4cf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,7 +27,7 @@ concurrency: jobs: pre-flight: name: Pre-flight (Lint & Unit) - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd - name: Verify capability matrix references @@ -38,18 +38,40 @@ jobs: - name: Containerized Linting run: docker compose -f compose/docker-compose.yml --profile lint run --rm lint-runner scripts/ci/lint.sh - - name: Containerized Unit Tests - run: docker compose -f compose/docker-compose.yml --profile test --profile interactive run --rm test-runner scripts/ci/test.sh + - name: Containerized Unit Tests (runtime-independent) + run: docker compose -f compose/docker-compose.yml --profile test --profile interactive run --rm --no-deps test-runner scripts/ci/test.sh + + - name: Start interactive dependency for browser E2E + id: start-e2e-runtime + run: docker compose -f compose/docker-compose.yml --profile interactive up --detach --wait --wait-timeout 150 winebot-interactive + + - name: Classify failed interactive startup without printing service logs + if: ${{ failure() && steps.start-e2e-runtime.outcome == 'failure' }} + run: | + set -euo pipefail + container_id="$(docker compose -f compose/docker-compose.yml --profile interactive ps -q winebot-interactive || true)" + if [ -z "$container_id" ]; then + echo "STARTUP_DIAGNOSTIC container=absent" + else + docker inspect --format='STARTUP_DIAGNOSTIC status={{.State.Status}} exit_code={{.State.ExitCode}} oom={{.State.OOMKilled}}' "$container_id" || true + fi + # Read privately; emit only enumerated stage names, never raw logs or tokens. + docker compose -f compose/docker-compose.yml --profile interactive logs --no-color --tail 200 winebot-interactive 2>/dev/null | + python3 scripts/ci/classify-startup-logs.py || true - name: Deterministic E2E subset run: | - docker compose -f compose/docker-compose.yml --profile interactive --profile test run --rm test-runner \ + docker compose -f compose/docker-compose.yml --profile test --profile interactive run --rm --no-deps test-runner \ pytest -q /work/tests/e2e/test_dashboard_e2e.py /work/tests/e2e/test_ux_keyboard_accessibility.py + - name: Stop E2E runtime + if: always() + run: docker compose -f compose/docker-compose.yml --profile interactive down --remove-orphans || true + conformance: name: Conformance (Contracts) needs: pre-flight - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 timeout-minutes: 30 steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd diff --git a/.github/workflows/reusable-build-smoke-gate.yml b/.github/workflows/reusable-build-smoke-gate.yml index 14d58b2b..c5a4ea74 100644 --- a/.github/workflows/reusable-build-smoke-gate.yml +++ b/.github/workflows/reusable-build-smoke-gate.yml @@ -70,7 +70,7 @@ defaults: jobs: build-smoke-gate: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 timeout-minutes: 60 steps: - name: Maximize disk space diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 44c5e16c..ccd34e85 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -41,6 +41,9 @@ if [ "$(id -u)" = "0" ]; then fi # --- USER CONTEXT (winebot) --- +# Classify an early failure by a fixed stage, never echoing environment values. +WINEBOT_STARTUP_STAGE="configuration" +trap 'startup_rc=$?; if [ "$startup_rc" -ne 0 ]; then printf "WINEBOT_STARTUP_ERROR stage=%s rc=%s\\n" "$WINEBOT_STARTUP_STAGE" "$startup_rc" >&2; fi' EXIT write_instance_state() { local state="$1" local reason="${2:-}" @@ -116,17 +119,22 @@ if { [ "$BUILD_INTENT" = "rel" ] || [ "$BUILD_INTENT" = "rel-runner" ]; } && [ " echo "--> Support Mode enabled for ${ttl_min} minutes (until epoch ${WINEBOT_SUPPORT_MODE_UNTIL_EPOCH})." fi +WINEBOT_STARTUP_STAGE="infrastructure" # 2. Infrastructure Setup (Session, X11, WM) echo "--> Pass 2: Infrastructure..." source /scripts/init/10-setup-infra.sh +WINEBOT_STARTUP_STAGE="wine" # 3. Wine Environment Setup (Prefix, Theme, VNC) echo "--> Pass 3: Wine Environment..." source /scripts/init/20-setup-wine.sh +WINEBOT_STARTUP_STAGE="services" # 4. Service Startup (API, Recorder, Supervisor) echo "--> Pass 4: Services..." source /scripts/init/30-start-services.sh +# Startup complete; do not misclassify later application failures. +trap - EXIT if [ "${DEBUG:-0}" = "1" ]; then echo "--> DEBUG: Windows automation tool versions..." diff --git a/scripts/ci/classify-startup-logs.py b/scripts/ci/classify-startup-logs.py new file mode 100644 index 00000000..b680b46a --- /dev/null +++ b/scripts/ci/classify-startup-logs.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""Emit only fixed startup diagnostic codes; never echo untrusted service log text.""" + +from __future__ import annotations + +import sys + +SPECIFIC_STAGES = ( + ("services/trace-log-path", "failed_trace_log_path"), + ("services/trace-script-path", "failed_trace_script_path"), + ("services/trace-launch", "failed_trace_launch"), + ("services/api-token", "failed_api_token"), + ("services/native-input-trace", "failed_native_trace"), + ("services/recording", "failed_recorder"), + ("services/api", "failed_api"), + ("services/supervisor", "failed_supervisor"), + ("services/input-trace", "failed_input_trace"), + ("services/complete", "failed_services_complete"), + ("services", "failed_services"), + ("configuration", "failed_configuration"), + ("infrastructure", "failed_infrastructure"), + ("wine", "failed_wine"), +) + + +def main() -> int: + # Compose logs may contain credentials or user-facing window titles. + # Read job-locally; print ONLY enumerated codes, never raw text. + raw = sys.stdin.read(262144) + for marker, code in SPECIFIC_STAGES: + if "WINEBOT_STARTUP_ERROR stage=" + marker + " " in raw: + print("STARTUP_DIAGNOSTIC stage=" + code) + return 0 + for marker, code in ( + ("Pass 4: Services", "reached_services"), + ("Pass 3: Wine Environment", "reached_wine"), + ("Pass 2: Infrastructure", "reached_infrastructure"), + ("Xvfb failed to start", "xvfb_failure"), + ("Configuration admission rejected", "configuration_rejected"), + ): + if marker in raw: + print("STARTUP_DIAGNOSTIC stage=" + code) + return 0 + print("STARTUP_DIAGNOSTIC stage=unknown") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/ci/test.sh b/scripts/ci/test.sh index f0253ca2..fdff62e5 100755 --- a/scripts/ci/test.sh +++ b/scripts/ci/test.sh @@ -26,4 +26,6 @@ pytest \ tests/test_visual_profile_cli.py \ tests/test_ci_trust_pack_provenance.py \ tests/test_ci_public_conformance_summary.py \ + tests/test_ci_unit_isolation.py \ + tests/test_winepath_startup_retry.py \ tests/test_wininspect_error_redaction.py diff --git a/scripts/init/30-start-services.sh b/scripts/init/30-start-services.sh index 3ba7665d..1528d52e 100755 --- a/scripts/init/30-start-services.sh +++ b/scripts/init/30-start-services.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash # 30-start-services.sh: Start API, Tracing, and Supervisor Loop +WINEBOT_STARTUP_STAGE="services/input-trace" echo "--> Pass 4: Service Startup (WINEBOT_INPUT_TRACE_WINDOWS=${WINEBOT_INPUT_TRACE_WINDOWS:-unset})" # Post-Init Tracing (Ensures wineserver is stable) @@ -9,8 +10,11 @@ if [ "${WINEBOT_INPUT_TRACE_WINDOWS:-0}" = "1" ]; then sleep 2 WIN_TRACE_MS="${WINEBOT_INPUT_TRACE_WINDOWS_SAMPLE_MS:-5}" mkdir -p "$SESSION_DIR/logs" - WINE_LOG_PATH=$(winepath -w "$SESSION_DIR/logs/input_events_windows.jsonl") - WINE_SCRIPT_PATH=$(winepath -w /automation/core/input_trace_windows.ahk) + WINEBOT_STARTUP_STAGE="services/trace-log-path" + WINE_LOG_PATH="$(/scripts/init/winepath-with-retry.sh "$SESSION_DIR/logs/input_events_windows.jsonl")" + WINEBOT_STARTUP_STAGE="services/trace-script-path" + WINE_SCRIPT_PATH="$(/scripts/init/winepath-with-retry.sh /automation/core/input_trace_windows.ahk)" + WINEBOT_STARTUP_STAGE="services/trace-launch" echo "--> Starting Windows Input Trace..." ( TRACE_RESTART_WINDOW="${WINEBOT_TRACE_RESTART_WINDOW_SECONDS:-120}" @@ -47,11 +51,13 @@ if [ "${WINEBOT_INPUT_TRACE_WINDOWS:-0}" = "1" ]; then ) & fi +WINEBOT_STARTUP_STAGE="services/native-input-trace" if [ "${WINEBOT_INPUT_TRACE:-0}" = "1" ]; then python3 -m automation.input_trace start --session-dir "$SESSION_DIR" >/dev/null 2>&1 & fi # Recorder (if enabled) +WINEBOT_STARTUP_STAGE="services/recording" if [ "${WINEBOT_RECORD:-0}" = "1" ]; then if [[ "$SCREEN" == *x*x* ]]; then RES="${SCREEN%x*}"; else RES="$SCREEN"; fi @@ -73,12 +79,16 @@ if [ "${WINEBOT_RECORD:-0}" = "1" ]; then fi # API +WINEBOT_STARTUP_STAGE="services/api" if [ "${ENABLE_API:-0}" = "1" ]; then if [ -z "${API_TOKEN:-}" ]; then # Generate a random 16-char token for secure-by-default operation + WINEBOT_STARTUP_STAGE="services/api-token" export API_TOKEN=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1) - echo "--> [SECURITY] No API_TOKEN provided. Generated random token: ${API_TOKEN}" - echo "--> [SECURITY] Use this token in 'X-API-Key' header for all requests." + WINEBOT_STARTUP_STAGE="services/api" + # Never print the generated credential into public CI/service logs. + echo "--> [SECURITY] Generated an ephemeral API token (value not logged)." + echo "--> [SECURITY] Token is available only through existing local file discovery." fi # Persist for internal tool discovery (use /tmp as /run is root-only) @@ -101,6 +111,7 @@ if [ "${ENABLE_API:-0}" = "1" ]; then fi # Supervisor +WINEBOT_STARTUP_STAGE="services/supervisor" if [ "${WINEBOT_SUPERVISE_EXPLORER:-1}" = "1" ]; then echo "--> Starting Desktop Supervisor..." # Settle time @@ -182,3 +193,5 @@ if [ "${WINEBOT_SUPERVISE_EXPLORER:-1}" = "1" ]; then else echo "--> Desktop Supervisor disabled (WINEBOT_SUPERVISE_EXPLORER=0)." fi + +WINEBOT_STARTUP_STAGE="services/complete" diff --git a/scripts/init/winepath-with-retry.sh b/scripts/init/winepath-with-retry.sh new file mode 100755 index 00000000..7c309e59 --- /dev/null +++ b/scripts/init/winepath-with-retry.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Convert one Linux path for Wine input tracing, retrying transient Wine startup. +# No fallback path and no sensitive Wine stderr are admitted to caller logs. +set -euo pipefail +if [ "$#" -ne 1 ] || [ -z "$1" ]; then + exit 2 +fi +for attempt in 1 2 3 4; do + translated="" + if translated="$(timeout 8s winepath -w "$1" 2>/dev/null)" && + [ -n "$translated" ] && + [[ "$translated" != *$'\n'* ]] && + [[ "$translated" != *$'\r'* ]]; then + printf '%s\n' "$translated" + exit 0 + fi + if [ "$attempt" -lt 4 ]; then sleep 1; fi +done +exit 1 diff --git a/tests/test_ci_unit_isolation.py b/tests/test_ci_unit_isolation.py new file mode 100644 index 00000000..c6cab0ce --- /dev/null +++ b/tests/test_ci_unit_isolation.py @@ -0,0 +1,75 @@ +"""Source-level regression for isolated unit CI and safe startup diagnostics.""" + +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent + + +class CIUnitIsolationTests(unittest.TestCase): + def test_units_do_not_start_interactive_runtime(self): + source = (ROOT / ".github/workflows/ci.yml").read_text(encoding="utf-8") + unit = "--profile test --profile interactive run --rm --no-deps test-runner scripts/ci/test.sh" + startup = "up --detach --wait --wait-timeout 150 winebot-interactive" + e2e = "pytest -q /work/tests/e2e/test_dashboard_e2e.py" + self.assertEqual(source.count(unit), 1) + self.assertEqual(source.count(startup), 1) + self.assertLess(source.index(unit), source.index(startup)) + self.assertLess(source.index(startup), source.index(e2e)) + self.assertIn("Stop E2E runtime", source) + self.assertIn("if: always()", source) + + def test_failed_startup_emits_only_bounded_markers(self): + source = (ROOT / ".github/workflows/ci.yml").read_text(encoding="utf-8") + self.assertIn("Classify failed interactive startup without printing service logs", source) + self.assertIn("python3 scripts/ci/classify-startup-logs.py", source) + self.assertIn('STARTUP_DIAGNOSTIC stage=', (ROOT / "scripts/ci/classify-startup-logs.py").read_text(encoding="utf-8")) + self.assertIn("State.OOMKilled", source) + self.assertNotIn("docker compose -f compose/docker-compose.yml logs --tail", source) + + def test_entrypoint_stage_is_fixed_and_cleared_at_ready(self): + source = (ROOT / "docker/entrypoint.sh").read_text(encoding="utf-8") + for marker in ('WINEBOT_STARTUP_STAGE="configuration"', + 'WINEBOT_STARTUP_STAGE="infrastructure"', + 'WINEBOT_STARTUP_STAGE="wine"', + 'WINEBOT_STARTUP_STAGE="services"', + "WINEBOT_STARTUP_ERROR stage=%s", + "trap - EXIT"): + self.assertIn(marker, source) + self.assertLess(source.index('WINEBOT_STARTUP_STAGE="services"'), + source.index("trap - EXIT")) + + def test_classifier_redacts_untrusted_log_contents(self): + import subprocess + import sys + + marker = "SYNTHETIC-CREDENTIAL-NOT-FOR-RELEASE" + output = subprocess.run( + [sys.executable, str(ROOT / "scripts/ci/classify-startup-logs.py")], + input=marker + "\\nWINEBOT_STARTUP_ERROR stage=services/trace-log-path rc=1\\n", + text=True, + capture_output=True, + check=True, + ).stdout + self.assertEqual(output.strip(), "STARTUP_DIAGNOSTIC stage=failed_trace_log_path") + self.assertNotIn(marker, output) + + def test_wine_ci_runner_image_pinned_to_qualified_public_venue(self): + ci = (ROOT / ".github/workflows/ci.yml").read_text(encoding="utf-8") + reusable = (ROOT / ".github/workflows/reusable-build-smoke-gate.yml").read_text( + encoding="utf-8" + ) + self.assertEqual(ci.count("runs-on: ubuntu-24.04"), 2) + self.assertEqual(reusable.count("runs-on: ubuntu-24.04"), 1) + self.assertNotIn("runs-on: ubuntu-latest", ci) + self.assertNotIn("runs-on: ubuntu-latest", reusable) + + def test_generated_api_token_is_not_ever_echoed(self): + source = (ROOT / "scripts/init/30-start-services.sh").read_text(encoding="utf-8") + self.assertNotIn("Generated random token: ${API_TOKEN}", source) + self.assertIn("value not logged", source) + self.assertIn('echo -n "$API_TOKEN" > /tmp/winebot_api_token', source) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_winepath_startup_retry.py b/tests/test_winepath_startup_retry.py new file mode 100644 index 00000000..1bf3d6ab --- /dev/null +++ b/tests/test_winepath_startup_retry.py @@ -0,0 +1,75 @@ +"""Fail-closed Wine trace-path retries without native Wine or private fixtures.""" + +import os +import subprocess +from pathlib import Path + +import pytest + +SCRIPT = Path("scripts/init/winepath-with-retry.sh") + + +@pytest.mark.parametrize( + ("mode", "expected_rc", "expected_count"), + [("recover", 0, 3), ("fail", 1, 4), ("multiline", 1, 4)], +) +def test_bounded_trace_path_retry( + tmp_path: Path, mode: str, expected_rc: int, expected_count: int +) -> None: + tool = tmp_path / "winepath" + tool.write_text( + "#!/bin/sh\n" + "set -eu\n" + 'count="$(cat "$WINEPATH_COUNTER" 2>/dev/null || printf 0)"\n' + 'count=$((count + 1))\n' + 'printf "%s" "$count" > "$WINEPATH_COUNTER"\n' + 'if [ "$WINEPATH_MODE" = recover ] && [ "$count" -ge 3 ]; then\n' + " printf 'Z:\\\\trace.jsonl\\n'\n" + " exit 0\n" + "fi\n" + 'if [ "$WINEPATH_MODE" = multiline ]; then\n' + " printf 'Z:\\\\trace.jsonl\\nSYNTHETIC_PRIVATE_MARKER\\n'\n" + " exit 0\n" + "fi\n" + "echo SYNTHETIC_PRIVATE_MARKER >&2\n" + "exit 1\n", + encoding="utf-8", + ) + tool.chmod(0o755) + sleep = tmp_path / "sleep" + sleep.write_text("#!/bin/sh\nexit 0\n", encoding="utf-8") + sleep.chmod(0o755) + counter = tmp_path / "counter" + env = os.environ.copy() + env.update( + PATH=f"{tmp_path}:{env['PATH']}", + WINEPATH_COUNTER=str(counter), + WINEPATH_MODE=mode, + ) + result = subprocess.run( + ["bash", str(SCRIPT), "/synthetic/trace.jsonl"], + env=env, + capture_output=True, + text=True, + timeout=45, + check=False, + ) + assert result.returncode == expected_rc + assert int(counter.read_text(encoding="utf-8")) == expected_count + assert "SYNTHETIC_PRIVATE_MARKER" not in result.stdout + result.stderr + if expected_rc == 0: + assert result.stdout == "Z:\\trace.jsonl\n" + else: + assert not result.stdout + + +def test_no_input_rejected_without_wine_process() -> None: + result = subprocess.run( + ["bash", str(SCRIPT)], + capture_output=True, + text=True, + timeout=5, + check=False, + ) + assert result.returncode == 2 + assert result.stdout == ""