The headless browser for AI agents and web scraping
ci: validate capture cleanup on native ARM64 (#1575)
SGavrlbfb300e1d400
diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex c73883c0..c53491e2 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -1,13 +1,17 @@name: Releaseon:- # Manual validation uses Windows only and cannot publish a release.+ # Manual validation cannot publish a release.workflow_dispatch:inputs:windows_only:description: Validate only the Windows distributiontype: booleandefault: true+ arm64_capture:+ description: Validate capture and disconnect cleanup in a native ARM64 container+ type: boolean+ default: falsepush:tags:- 'v*'@@ -30,6 +34,7 @@ jobs:command: check advisories bans licenses sourcesbuild:+ if: github.event_name != 'workflow_dispatch' || !inputs.arm64_captureneeds: auditstrategy:fail-fast: false@@ -200,6 +205,118 @@ jobs:if-no-files-found: errorretention-days: 1+ capture-arm64:+ name: Native ARM64 capture lifecycle+ if: github.event_name == 'workflow_dispatch' && inputs.arm64_capture+ needs: audit+ runs-on: ubuntu-22.04-arm+ timeout-minutes: 60+ env:+ CARGO_BUILD_JOBS: "2"+ CARGO_INCREMENTAL: "0"+ steps:+ - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6+ with:+ persist-credentials: false+ - uses: dtolnay/rust-toolchain@4360b52568e2003a75bf9bc1d59f33a8e3fc893c # stable+ - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7+ with:+ node-version: '22'+ - uses: taiki-e/install-action@b20dedce73af6905cdc30d6611090c9b67557c8d # v2+ with:+ tool: nextest+ fallback: none+ - name: Check native architecture+ run: |+ test "$(uname -m)" = aarch64+ node -e 'if (process.arch !== "arm64") process.exit(1)'+ rustc -Vv+ - name: Install build and diagnostic tools+ run: |+ sudo apt-get update+ sudo apt-get install -y cmake clang libclang-dev llvm-dev gdb+ - name: Build render release+ run: cargo build --release -p obscura-cli --bins --features render+ - name: Check native grid redistribution progress+ run: |+ cargo nextest run --manifest-path vendor/taffy/Cargo.toml \+ --target-dir target/taffy-regressions --release --lib \+ -E 'test(track_distribution_)'+ - name: Check fontless capture and disconnect cleanup+ shell: bash+ run: |+ set -euo pipefail+ results_root="$RUNNER_TEMP/native-capture"+ mkdir -p "$results_root"+ git rev-parse HEAD > "$results_root/source.txt"+ sha256sum target/release/obscura target/release/obscura-worker > "$results_root/binaries.sha256"+ baseline_image=h4ckf0r0day/obscura:0.2.6+ runtime_image=gcr.io/distroless/cc-debian12:nonroot+ docker pull --platform linux/arm64 "$baseline_image"+ docker pull --platform linux/arm64 "$runtime_image"+ docker image inspect "$baseline_image" "$runtime_image" > "$results_root/images.json"+ test "$(docker image inspect --format '{{.Architecture}}' "$baseline_image")" = arm64+ test "$(docker image inspect --format '{{.Architecture}}' "$runtime_image")" = arm64+ container=''+ trap 'if [[ -n "$container" ]]; then docker rm -f "$container" >/dev/null 2>&1 || true; fi' EXIT+ capture_status=0+ probe() {+ local label="$1" image="$2" mode="$3"+ local output="$results_root/$label"+ mkdir -p "$output/ready"+ chmod 0777 "$output/ready"+ local mounts=(--volume "$output/ready:/capture")+ if [[ "$mode" == current ]]; then+ mounts+=(--volume "$PWD/target/release/obscura:/obscura:ro")+ mounts+=(--volume "$PWD/target/release/obscura-worker:/obscura-worker:ro")+ fi+ container="obscura-capture-$label"+ docker run --detach --name "$container" --network host \+ --read-only --tmpfs /tmp:rw,size=128m --memory 1g --cpus 3 --pids-limit 256 \+ --env TZ=UTC --entrypoint /obscura "${mounts[@]}" "$image" \+ serve --host 127.0.0.1 --port 0 --workers 1 \+ --ready-file /capture/ready.json --allow-private-network --quiet+ for _ in $(seq 1 300); do+ if [[ -s "$output/ready/ready.json" ]]; then break; fi+ if [[ "$(docker inspect --format '{{.State.Running}}' "$container")" != true ]]; then+ docker logs "$container"+ return 1+ fi+ sleep 0.1+ done+ test -s "$output/ready/ready.json"+ sudo chown "$(id -u):$(id -g)" "$output/ready/ready.json"+ local endpoint engine_pid+ endpoint="$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print("http://"+d["host"]+":"+str(d["port"]))' "$output/ready/ready.json")"+ engine_pid="$(docker inspect --format '{{.State.Pid}}' "$container")"+ docker inspect "$container" > "$output/container.json"+ set +e+ sudo "$(command -v node)" scripts/ci/capture_lifecycle.mjs \+ "$endpoint" "$engine_pid" "$output/observations"+ capture_status=$?+ set -e+ docker logs "$container" > "$output/server.log" 2>&1+ if [[ "$capture_status" != 0 ]]; then+ sudo timeout 20s gdb --batch -ex 'thread apply all bt 12' \+ -p "$engine_pid" > "$output/native-stacks.txt" 2>&1 || true+ fi+ docker rm -f "$container"+ container=''+ }+ probe baseline "$baseline_image" baseline+ printf '%s\n' "$capture_status" > "$results_root/baseline-exit.txt"+ probe current "$runtime_image" current+ printf '%s\n' "$capture_status" > "$results_root/current-exit.txt"+ test "$capture_status" = 0+ - name: Upload native capture evidence+ if: always()+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7+ with:+ name: native-capture-${{ github.run_id }}-${{ github.run_attempt }}+ path: ${{ runner.temp }}/native-capture+ if-no-files-found: warn+ retention-days: 7+publish:if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')needs: builddiff --git a/scripts/ci/capture_lifecycle.mjs b/scripts/ci/capture_lifecycle.mjsnew file mode 100644index 00000000..27b919cb--- /dev/null+++ b/scripts/ci/capture_lifecycle.mjs@@ -0,0 +1,142 @@+import assert from 'node:assert/strict';+import {fork, execFileSync} from 'node:child_process';+import {readFileSync, writeFileSync, mkdirSync, existsSync, openSync, closeSync} from 'node:fs';+import {join} from 'node:path';+import {fileURLToPath} from 'node:url';++const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));+const pngHeader = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);++async function client(endpoint, specification, output) {+ const test = JSON.parse(specification);+ const result = {url: test.url, expectedTitle: test.title, phase: 'connect', ok: false};+ let socket;+ const pending = new Map();+ let serial = 0;+ try {+ const version = await (await fetch(endpoint + '/json/version', {signal: AbortSignal.timeout(5000)})).json();+ socket = new WebSocket(version.webSocketDebuggerUrl);+ await new Promise((resolve, reject) => {+ const timer = setTimeout(() => reject(Error('WebSocket open timeout')), 5000);+ socket.addEventListener('open', () => {clearTimeout(timer); resolve();}, {once: true});+ socket.addEventListener('error', () => {clearTimeout(timer); reject(Error('WebSocket open failed'));}, {once: true});+ });+ socket.addEventListener('message', event => {+ const response = JSON.parse(event.data), request = pending.get(response.id);+ if (!request) return;+ pending.delete(response.id); clearTimeout(request.timer);+ response.error ? request.reject(Error(JSON.stringify(response.error))) : request.resolve(response.result);+ });+ const call = (method, params = {}, sessionId, timeout = 5000) => new Promise((resolve, reject) => {+ const id = ++serial;+ const timer = setTimeout(() => {pending.delete(id); reject(Error(method + ' timeout'));}, timeout);+ pending.set(id, {resolve, reject, timer});+ socket.send(JSON.stringify({id, method, params, ...(sessionId ? {sessionId} : {})}));+ });+ const {targetId} = await call('Target.createTarget', {url: 'about:blank'});+ const {sessionId} = await call('Target.attachToTarget', {targetId, flatten: true});+ result.phase = 'navigation';+ const start = performance.now();+ const navigation = await call('Page.navigate', {url: test.url}, sessionId, 35000);+ result.navigationMs = performance.now() - start;+ assert(!navigation.errorText, navigation.errorText);+ const ready = await call('Runtime.evaluate', {+ expression: 'JSON.stringify({readyState:document.readyState,title:document.title,url:location.href})', returnByValue: true,+ }, sessionId);+ result.page = JSON.parse(ready.result.value);+ assert.equal(result.page.title, test.title, 'unexpected document, not a valid capture reproduction');+ result.phase = 'capture';+ writeFileSync(output, JSON.stringify(result, null, 2) + '\n');+ const captureStart = performance.now();+ const capture = call('Page.captureScreenshot', {format: 'png'}, sessionId, 60000);+ process.send?.({phase: 'capture-sent'});+ const image = await capture;+ result.captureMs = performance.now() - captureStart;+ const bytes = Buffer.from(image.data, 'base64');+ assert(bytes.subarray(0, 8).equals(pngHeader), 'invalid PNG signature');+ assert(bytes.length > 10000, 'unexpectedly small screenshot for the fixture');+ result.image = {bytes: bytes.length, width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20), format: 'png'};+ writeFileSync(output + '.png', bytes);+ result.ok = true; result.phase = 'complete';+ } catch (error) {result.error = String(error);}+ finally {+ for (const request of pending.values()) clearTimeout(request.timer);+ try {socket?.close();} catch {}+ writeFileSync(output, JSON.stringify(result, null, 2) + '\n');+ }+ // Exiting the client closes its TCP socket even if the server cannot finish a close handshake.+ process.exit(result.ok ? 0 : 1);+}++if (process.argv[2] === '--client') {+ await client(...process.argv.slice(3));+} else {+ const [endpoint, pidText, directory] = process.argv.slice(2);+ assert(endpoint && /^\d+$/.test(pidText || '') && directory, 'usage: capture_lifecycle.mjs ENDPOINT SERVER_PID OUTPUT_DIRECTORY');+ const pid = Number(pidText);+ mkdirSync(directory, {recursive: false});+ const cases = JSON.parse(process.env.CAPTURE_CASES || JSON.stringify([+ {url: 'https://en.wikipedia.org/wiki/Seoul', title: 'Seoul - Wikipedia'},+ {url: 'https://en.wikipedia.org/wiki/Busan', title: 'Busan - Wikipedia'},+ ]));+ assert.equal(cases.length, 2);+ const ticks = Number(execFileSync('getconf', ['CLK_TCK'], {encoding: 'utf8'}));+ function resources() {+ let cpuSeconds = 0, rssKiB = 0;+ const queue = [pid], seen = new Set();+ while (queue.length) {+ const current = queue.pop(); if (seen.has(current)) continue; seen.add(current);+ try {+ const fields = readFileSync(`/proc/${current}/stat`, 'utf8').split(')').pop().trim().split(/\s+/);+ cpuSeconds += (Number(fields[11]) + Number(fields[12])) / ticks;+ rssKiB += Number(readFileSync(`/proc/${current}/status`, 'utf8').match(/^VmRSS:\s+(\d+)/m)?.[1] || 0);+ const children = readFileSync(`/proc/${current}/task/${current}/children`, 'utf8').trim();+ if (children) queue.push(...children.split(/\s+/).map(Number));+ } catch (error) {if (current === pid) throw error;}+ }+ return {timeMs: performance.now(), cpuSeconds, rssKiB};+ }+ async function attempt(label, test, abandon = false) {+ const output = join(directory, label + '.json'), fd = openSync(output + '.log', 'wx');+ const child = fork(fileURLToPath(import.meta.url), ['--client', endpoint, JSON.stringify(test), output], {stdio: ['ignore', fd, fd, 'ipc']});+ let captureSent = false, killed = false, killTimer;+ const deadline = setTimeout(() => {killed = true; child.kill('SIGKILL');}, 110000);+ child.on('message', message => {+ if (message.phase === 'capture-sent') {+ captureSent = true;+ if (abandon) killTimer = setTimeout(() => {killed = true; child.kill('SIGKILL');}, 5);+ }+ });+ const exit = await new Promise((resolve, reject) => {+ child.once('error', reject); child.once('exit', (code, signal) => resolve({code, signal}));+ }).finally(() => {clearTimeout(deadline); clearTimeout(killTimer); closeSync(fd);});+ await sleep(1000);+ const before = resources(); await sleep(2000); const after = resources();+ const busyCores = (after.cpuSeconds - before.cpuSeconds) / ((after.timeMs - before.timeMs) / 1000);+ return {label, abandon, captureSent, killed, exit, result: existsSync(output) ? JSON.parse(readFileSync(output, 'utf8')) : null,+ before, after, busyCores};+ }+ const report = {architecture: process.arch, platform: process.platform, pid, endpoint,+ methodology: 'Fresh raw CDP connection per attempt; no layout reads before capture. Client process exits or is killed after sending capture. CPU and RSS cover the server process tree. Timings are diagnostic, not benchmark claims.', attempts: []};+ let status = 1;+ try {+ for (let i = 0; i < 3; i++) {+ report.attempts.push(await attempt('capture-' + i, cases[0]));+ console.log(report.attempts.at(-1).label, JSON.stringify({ok: report.attempts.at(-1).result?.ok, busyCores: report.attempts.at(-1).busyCores}));+ }+ if (report.attempts.every(attempt => attempt.result?.ok)) report.attempts.push(await attempt('disconnect-during-capture', cases[0], true));+ report.attempts.push(await attempt('control-after-disconnect', cases[1]));+ const abandoned = report.attempts.find(attempt => attempt.abandon);+ const lastRss = report.attempts.at(-1).after.rssKiB, firstRss = report.attempts[0].after.rssKiB;+ report.checks = {+ completedCaptures: report.attempts.filter(attempt => !attempt.abandon).every(attempt => attempt.result?.ok),+ activeCaptureDisconnected: Boolean(abandoned?.captureSent && abandoned?.killed && abandoned?.result?.phase === 'capture'),+ cpuQuiescent: report.attempts.every(attempt => attempt.busyCores < 0.25),+ boundedRssGrowth: lastRss - firstRss < 96 * 1024,+ };+ status = Object.values(report.checks).every(Boolean) ? 0 : 1;+ console.log(JSON.stringify(report.checks));+ } catch (error) {report.error = String(error);}+ finally {writeFileSync(join(directory, 'result.json'), JSON.stringify(report, null, 2) + '\n');}+ process.exitCode = status;+}