diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 52788fc..07b2ebe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -145,6 +145,7 @@ jobs: node --check preload.cjs node --test backend-shutdown.test.cjs node --test ui-entry.test.cjs + node --test candidate-profile.test.cjs npx electron --version - name: Build and stage packaged backend run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index e1537b6..621fce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ### Added +- Isolated Windows candidate builder with fresh packaged backend, short artifact + paths, separate metadata-only profile and bounded build/live-test storage. + A local 122-second packaged UI run reconciled 10,189 accepted/acknowledged/stored + packets; complete VPN-return and multi-hour acceptance remain open gates. - Source-built experimental desktop entry with canonical components, a shared controller and stable API identity. Retired compiled-bundle patching; isolated Electron and desktop/mobile fixture checks cover navigation and selection. diff --git a/desktop/electron/candidate-main.cjs b/desktop/electron/candidate-main.cjs new file mode 100644 index 0000000..b53f9d7 --- /dev/null +++ b/desktop/electron/candidate-main.cjs @@ -0,0 +1,32 @@ +const { app } = require('electron'); +const fs = require('node:fs'); +const path = require('node:path'); +const net = require('node:net'); +const { candidateEnvironment, candidatePort } = require('./candidate-profile.cjs'); + +const profile = process.env.NETBOT_CANDIDATE_PROFILE || path.join(app.getPath('appData'), 'netbot-candidate-preview'); +Object.assign(process.env, candidateEnvironment(profile)); +// Candidate packages must not silently run an inherited, older backend or dev URL. +delete process.env.NETBOT_BACKEND_BIN; +delete process.env.NETBOT_PYTHON_BIN; +delete process.env.NETBOT_DESKTOP_DEV_SERVER_URL; +app.setPath('userData', profile); +app.setName('NetBotPro Candidate'); +const config = path.join(profile, 'config'); +fs.mkdirSync(config, { recursive: true }); +const settings = path.join(config, 'settings.json'); +if (!fs.existsSync(settings)) { + fs.writeFileSync(settings, JSON.stringify({ iface: 'iface=default', autostart_sniffer: false, + auto_block: false, persist_logs: true, retention_minutes: 15, sniffer_sample_rate: 1, + payload_capture_enabled: false, capture_mode: 'metadata' })); +} +// Resolve a loopback port before main.cjs snapshots its backend configuration. +const reservation = net.createServer(); +reservation.on('error', error => { throw error; }); +reservation.listen(candidatePort(process.env.NETBOT_CANDIDATE_PORT), '127.0.0.1', () => { + const port = reservation.address().port; + reservation.close(() => { + process.env.NETBOT_PORT = String(port); + require('./main.cjs'); + }); +}); diff --git a/desktop/electron/candidate-profile.cjs b/desktop/electron/candidate-profile.cjs new file mode 100644 index 0000000..f026863 --- /dev/null +++ b/desktop/electron/candidate-profile.cjs @@ -0,0 +1,24 @@ +const path = require('node:path'); + +function candidateEnvironment(profile) { + if (!path.isAbsolute(profile) || !path.basename(profile).startsWith('netbot-candidate-')) { + throw new Error('Candidate profile must be an absolute, explicitly named netbot-candidate-* directory'); + } + return { + NETBOT_DESKTOP_UI: 'desktop', NETBOT_CAPTURE_MODE: 'metadata', NETBOT_HOST: '127.0.0.1', NETBOT_PROFILE: 'desktop', + NETBOT_CAPTURE_PROCESS: '1', NETBOT_CAPTURE_JOURNAL: '1', NETBOT_JOURNAL_ACK_MODE: 'history', + NETBOT_PAYLOAD_CAPTURE: '0', NETBOT_ALLOW_FULL_CAPTURE: '0', NETBOT_REDACT_SENSITIVE_DATA: '1', + NETBOT_STABILITY_RECORDING: '0', NETBOT_PROFILE_STAGES: '0', + NETBOT_DB_PATH: path.join(profile, 'data', 'history.db'), + NETBOT_FLOW_DB_PATH: path.join(profile, 'data', 'flows.db'), + NETBOT_CAPTURE_JOURNAL_PATH: path.join(profile, 'data', 'journal.db'), + NETBOT_RUNTIME_DIR: path.join(profile, 'runtime'), NETBOT_CACHE_DIR: path.join(profile, 'cache'), + }; +} +function candidatePort(value) { + if (value === undefined || value === '') return 0; + const port = Number(value); + if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('Invalid candidate port'); + return port; +} +module.exports = { candidateEnvironment, candidatePort }; diff --git a/desktop/electron/candidate-profile.test.cjs b/desktop/electron/candidate-profile.test.cjs new file mode 100644 index 0000000..35d9721 --- /dev/null +++ b/desktop/electron/candidate-profile.test.cjs @@ -0,0 +1,26 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const path = require('node:path'); +const { candidateEnvironment, candidatePort } = require('./candidate-profile.cjs'); + +test('candidate storage paths stay in its dedicated profile', () => { + const root = path.resolve('netbot-candidate-fixture'); + const env = candidateEnvironment(root); + for (const [key, value] of Object.entries(env)) { + if (key.endsWith('_PATH') || key.endsWith('_DIR')) assert.ok(value.startsWith(root + path.sep)); + } + assert.equal(env.NETBOT_JOURNAL_ACK_MODE, 'history'); + assert.equal(env.NETBOT_CAPTURE_MODE, 'metadata'); + assert.equal(env.NETBOT_STABILITY_RECORDING, '0'); +}); +test('candidate rejects relative and ordinary installed profiles', () => { + for (const value of ['netbot-candidate-fixture', path.resolve('Netbotpro'), path.parse(process.cwd()).root]) { + assert.throws(() => candidateEnvironment(value)); + } +}); +test('candidate port defaults to ephemeral and validates explicit requests', () => { + assert.equal(candidatePort(), 0); + assert.equal(candidatePort(''), 0); + assert.equal(candidatePort('49152'), 49152); + for (const value of ['0', '-1', '65536', '8765/path', '1.5']) assert.throws(() => candidatePort(value)); +}); diff --git a/docs/DESKTOP_CANDIDATE.md b/docs/DESKTOP_CANDIDATE.md new file mode 100644 index 0000000..083f206 --- /dev/null +++ b/docs/DESKTOP_CANDIDATE.md @@ -0,0 +1,127 @@ +# Isolated Windows Desktop Candidate + +This is a validation package, not an installed-app replacement or a release. +The normal Electron entry and installed profile are unchanged. + +## Build + +Install the repository's Python, frontend and desktop dependencies first, then +commit source changes so the candidate has an unambiguous source revision: + +```powershell +python scripts/release/build_desktop_candidate.py +``` + +The builder creates a new revision/timestamp directory under +`%LOCALAPPDATA%/NetBotCandidates/` (a short path avoids legacy Windows path limits +inside scientific-library bundles). It packages a fresh PyInstaller backend and both +Vite entries, then builds an unpacked Electron candidate. It never copies the old +security-build backend or stages over `packaging/runtime/backend` or an installed +app. `candidate.json` records its source revision and executable path. Build +dependencies must already exist; the builder does not install global software. + +Build steps have 15-minute deadlines and a sampled 3 GiB workspace budget. +Starting requires 5 GiB free space, and the build stops below 2 GiB free. Sampling +can overshoot briefly. Failed artifacts are retained for diagnosis, not deleted +automatically. New builds are refused once retained candidate artifacts exceed +2 GiB, preventing repeated test builds from accumulating indefinitely. A newly +admitted build can still add up to its own sampled 3 GiB budget. Existing artifacts +are not silently deleted; review their ownership before any manual cleanup. + +## Candidate Profile + +The candidate bootstrap selects `desktop.html`, sets its own user-data directory, +and overrides history, flow, journal, cache and runtime paths within that profile. +It rejects ordinary profile paths and inherited backend/dev-server overrides. +It chooses a free loopback API port rather than inheriting the standard app's +port; tests can explicitly reserve one with `NETBOT_CANDIDATE_PORT`. There is a +small release-to-backend-bind race, so an unlikely startup collision remains +possible. The profile and generated local authentication token remain separate. +The default is `%APPDATA%/netbot-candidate-preview`; tests explicitly use a new +`netbot-candidate-smoke-*` temporary profile instead. + +New profiles start with metadata-only capture, auto-block and autostart disabled, +15-minute metadata retention and sampling rate one. Candidate history-confirmed +journaling is explicitly enabled. This does not change installed/default capture +policy or establish exactly-once analysis. The candidate is not a production soak +configuration or a hard disk quota. + +## Explicit Live Test + +```powershell +$env:NETBOT_CANDIDATE_EXE = '' +$env:NETBOT_SMOKE_PYTHON_BIN = '' +$env:NETBOT_CANDIDATE_DURATION = '60' +node frontend/desktop-reference/test-candidate-capture.mjs +``` + +This starts real capture through the candidate's UI. Run only on a network you +are authorized to inspect and with a working Npcap driver. It does not change VPN, +routes, firewall rules or generate test traffic. Duration is restricted to 15..120 +seconds, with a sampled 128 MiB temporary-profile budget. It exercises packet +selection, investigation, Auto-follow and Stop, then verifies acknowledged history +against SQLite rows after a normal acknowledged app shutdown. + +Only numeric counters are printed. Raw traffic screenshots, domains and packet +records are not exported. The temporary test profile is removed after the test; +an emergency deadline can terminate only the owned candidate process tree. This +test fixture's data is disposable, unlike an installed recovery journal. + +An interface-change count does not prove a complete VPN-return scenario. Such a +scenario requires an actual user-approved route change while capture is active. +Missing driver statistics remain unknown, not proof of zero wire loss. A short +successful capture does not establish multi-hour stability. + +## Local Evidence: 2026-11-12 + +The independently built candidate used source revision +`a9512a398dde95622ed7c7ae5b2db18669e45023`, a fresh packaged backend and Electron +45.2.3. The complete build workspace, including intermediate files, was +857,832,152 bytes. An initial long-checkout build exposed a Windows path-length +failure while copying a scientific-library binary; the short candidate root +resolved it without changing OS settings or using an old packaged backend. + +The final live run used the improved page-aware test harness: + +| Observation | Measured | +| --- | ---: | +| Capture plus UI checks | 122 seconds | +| Accepted / acknowledged / SQLite packet rows | 10,189 / 10,189 / 10,189 | +| Final journal pending | 0 | +| Sampled peak pending | 128 | +| Failed / dropped history writes | 0 / 0 | +| Reported driver buffer drops | 0; final statistics coverage complete | +| Renderer errors | 0 | +| Sampled local API p95 | 40 ms | +| Packet selection through investigation findings | 175 ms | +| Stop completion | 473 ms | +| Sampled temporary profile peak | 28,535,468 bytes | +| Acknowledged app shutdown and temporary-profile removal | Passed | + +These API and selection samples include test overhead and are not controlled +throughput/latency benchmarks. Memory/CPU and multi-hour growth still need +dedicated profiling. Driver counters do not prove zero wire loss. + +In a separate 22-second run with one observed interface change, all 1,170 +accepted packets were acknowledged and stored, final pending was zero, and UI +checks passed. A longer transition observation recorded two interface switches +with positive progressing capture counters, but its UI check failed because the +test assumed Live Monitor stayed selected while another page was active. +The harness now explicitly navigates back before selecting a packet and records +the active page with numeric samples. That failed UI assertion is not counted as +a successful complete VPN-return acceptance run. The final 122-second run had +no interface changes, so it does not close the full VPN-return/long-soak gate. + +No installed application or installed profile was replaced. Only numeric +summaries were exported; real traffic screenshots and raw records were not kept. + +The subsequent candidate from revision +`8a3c459b7734ef05e30dcfb2d9b864a0c9c505c2` also passed a 31-second real capture +with a deliberately occupied inherited API port. Its bootstrap chose another +loopback port, and all 11,705 packets were accepted, acknowledged and stored. +Final pending was zero, sampled peak pending was 3,533, history failures/drops +and renderer errors were zero, and final driver coverage reported zero buffer +drops. Sampled API p95 was 51 ms, selection/investigation 218 ms, Stop 584 ms, +and temporary profile peak 26,005,043 bytes. The larger transient backlog is +retained as an observation, not described as zero queue pressure. This run had +no interface switch and does not close the full VPN-return gate. diff --git a/docs/README.md b/docs/README.md index 6edb673..62a07b3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -29,6 +29,7 @@ describe their own release, not the current branch or installed desktop overlay. - [Validation and CPU measurement](VALIDATION.md) - [Isolated journal/history probe and local evidence](JOURNAL_HISTORY_PROBE.md) - [Standard frontend live-follow continuity](LIVE_FOLLOW_VALIDATION.md) +- [Isolated Windows desktop candidate](DESKTOP_CANDIDATE.md) - [Screenshot provenance](SCREENSHOTS.md) - [Repository and GitHub maintenance](REPOSITORY_MAINTENANCE.md) diff --git a/frontend/desktop-reference/test-candidate-capture.mjs b/frontend/desktop-reference/test-candidate-capture.mjs new file mode 100644 index 0000000..a1df2cc --- /dev/null +++ b/frontend/desktop-reference/test-candidate-capture.mjs @@ -0,0 +1,155 @@ +// Explicit live test of a freshly packaged candidate, with disposable metadata storage. +import { _electron } from 'playwright'; +import assert from 'node:assert/strict'; +import fs from 'node:fs/promises'; +import path from 'node:path'; +import os from 'node:os'; +import net from 'node:net'; +import { execFileSync } from 'node:child_process'; + +const executable = process.env.NETBOT_CANDIDATE_EXE; +const python = process.env.NETBOT_SMOKE_PYTHON_BIN; +const duration = Number(process.env.NETBOT_CANDIDATE_DURATION || 60); +assert.ok(executable && python, 'Explicit candidate executable and report Python are required'); +assert.ok(duration >= 15 && duration <= 120, 'Live test must last 15..120 seconds'); +await fs.access(executable); +const profile = await fs.mkdtemp(path.join(os.tmpdir(), 'netbot-candidate-smoke-')); +const listener = net.createServer(); +await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)); +const port = listener.address().port; +listener.unref(); +async function bytes(folder) { + let total = 0; + for (const entry of await fs.readdir(folder, { withFileTypes: true })) { + const file = path.join(folder, entry.name); + try { total += entry.isDirectory() ? await bytes(file) : (await fs.stat(file)).size; } + catch (error) { if (error.code !== 'ENOENT') throw error; } + } + return total; +} +let app, ownerPid, report, peakBytes = 0; +const requestDurations = []; +try { + const env = { ...process.env, NETBOT_CANDIDATE_PROFILE: profile, NETBOT_PORT: String(port), NETBOT_HOST: '127.0.0.1', NETBOT_DISABLE_GPU: '1' }; + delete env.NETBOT_CANDIDATE_PORT; + app = await _electron.launch({ executablePath: executable, args: [], timeout: 45000, + env }); + ownerPid = app.process().pid; + const window = await app.firstWindow(); + const errors = []; + window.on('pageerror', error => errors.push(error.name)); + await window.getByRole('heading', { name: 'Command Center', exact: true }).waitFor({ timeout: 45000 }); + const runtime = await window.evaluate(() => window.netbotproDesktop); + const apiBase = new URL(runtime.apiBase); + assert.equal(apiBase.hostname, '127.0.0.1'); + assert.notEqual(apiBase.port, String(port), 'Candidate reused the deliberately occupied inherited API port'); + const request = async (endpoint, options = {}) => { + const requestedAt = Date.now(); + const result = await fetch(`${runtime.apiBase}${endpoint}`, { ...options, + headers: { 'X-NetBot-Token': runtime.localToken, ...options.headers }, signal: AbortSignal.timeout(10000) }); + assert.ok(result.ok, `Candidate API status ${result.status}`); + const body = await result.json(); + requestDurations.push(Date.now() - requestedAt); + if (requestDurations.length > 256) requestDurations.shift(); + return body; + }; + await window.locator('nav.app-nav').getByRole('button', { name: 'Live Monitor', exact: true }).click(); + const bar = window.locator('header[aria-label="Capture status"]'); + await bar.getByRole('button', { name: 'Start', exact: true }).click({ timeout: 20000 }); + await window.waitForFunction(() => { const stop = document.querySelector('.soc-stop'); return stop && !stop.disabled; }, null, { timeout: 20000 }); + console.log(JSON.stringify({ stage: 'capture_started', durationSeconds: duration, isolatedCandidate: true })); + const started = Date.now(); + let pendingPeak = 0, interfaceChanges = 0, previousInterface, last, nextSample = 0; + while (Date.now() - started < duration * 1000) { + last = await request('/sniffer/state'); + assert.equal(last.running, true, 'Capture unexpectedly stopped'); + assert.ok(!last.ipc_callback_errors, 'Capture callback failure'); + pendingPeak = Math.max(pendingPeak, Number(last.journal_pending_records || 0)); + if (previousInterface && last.selected_iface && previousInterface !== last.selected_iface) interfaceChanges++; + previousInterface = last.selected_iface || previousInterface; + if (Date.now() - started >= nextSample) { + const history = await request('/packets?limit=25&offset=0&only_remote=false'); + const remote = await request('/packets?limit=1&offset=0&only_remote=true'); + const ui = await window.evaluate(() => ({ activePage: document.querySelector('nav [aria-current="page"]')?.dataset.navId, + follow: document.querySelector('.lm-follow input')?.checked, + streamLabel: document.querySelector('.app-nav-footer small:last-child')?.textContent })); + console.log(JSON.stringify({ stage: 'capture_sample', seconds: Math.round((Date.now() - started) / 1000), + accepted: last.ipc_accepted, acknowledged: last.journal_acknowledged_records, + pending: last.journal_pending_records, historyTotal: history.total, historyItems: history.items?.length, remoteTotal: remote.total, + ui, websocketClients: last.observability?.event_bus?.subscribers, + uiRows: await window.locator('.lm-packets tbody tr[tabindex]').count(), rendererErrors: errors.length, + interfaceChanges, routeSwitches: last.route_switches, captureError: !!last.capture_error, + routeWarning: !!last.route_warning, health: last.health, controlSampleAgeSeconds: last.control_sample_age_seconds, + driverBufferDroppedReported: last.driver_stats?.buffer_dropped_reported ?? null })); + nextSample += 15000; + if (ui.activePage === 'monitor' && history.total > 0 && await window.locator('.lm-packets tbody tr[tabindex]').count() === 0) { + await window.bringToFront(); + await new Promise(resolve => setTimeout(resolve, 500)); + console.log(JSON.stringify({ stage: 'foreground_check', uiRows: await window.locator('.lm-packets tbody tr[tabindex]').count() })); + } + } + peakBytes = Math.max(peakBytes, await bytes(profile)); + assert.ok(peakBytes < 128 * 1024**2, 'Candidate storage budget exceeded'); + await new Promise(resolve => setTimeout(resolve, 1000)); + } + await window.locator('nav.app-nav').getByRole('button', { name: 'Live Monitor', exact: true }).click(); + await window.getByRole('heading', { name: 'Live Monitor', exact: true }).waitFor(); + const panel = window.locator('.lm-packets'); + const selectionStarted = Date.now(); + await panel.locator('tbody tr[tabindex]').first().locator('td').first().click({ timeout: 10000 }); + await window.getByRole('button', { name: 'Open in Investigate', exact: true }).click(); + await window.getByRole('heading', { name: 'Key Findings', exact: true }).waitFor({ timeout: 15000 }); + const selectionMilliseconds = Date.now() - selectionStarted; + await window.locator('nav.app-nav').getByRole('button', { name: 'Live Monitor', exact: true }).click(); + const follow = panel.getByRole('switch', { name: 'Auto-follow' }); + if (await follow.isChecked()) await follow.click(); + await follow.click(); + await window.waitForFunction(() => document.querySelector('.lm-follow input')?.checked); + const stoppedAt = Date.now(); + await bar.getByRole('button', { name: 'Stop', exact: true }).click(); + await window.waitForFunction(() => { const start = document.querySelector('.soc-start'); return start && !start.disabled; }); + let final; + for (let attempt = 0; attempt < 30; attempt++) { + final = await request('/sniffer/state'); + if (!final.running) break; + await new Promise(resolve => setTimeout(resolve, 500)); + } + assert.equal(final.running, false, 'Capture did not stop'); + const capture = final.observability?.capture || final; + const persistence = final.observability?.persistence || {}; + report = { live: true, packagedCandidate: true, apiPortIsolated: true, durationSeconds: Math.round((Date.now() - started) / 1000), + accepted: capture.ipc_accepted, acknowledged: capture.journal_acknowledged_records, + pendingFinal: capture.journal_pending_records, pendingPeak, interfaceChanges, routeSwitches: capture.route_switches, + historyFailed: persistence.failed_writes, historyDropped: persistence.dropped_writes, + driverBufferDroppedReported: capture.driver_stats?.buffer_dropped_reported ?? null, + driverCoverageComplete: capture.driver_stats?.coverage_complete ?? null, + stopMilliseconds: Date.now() - stoppedAt, selectionMilliseconds, + sampledApiP95Milliseconds: requestDurations.toSorted((a, b) => a - b)[Math.max(0, Math.ceil(requestDurations.length * .95) - 1)], + rendererErrors: errors.length, profilePeakBytes: peakBytes }; + console.log(JSON.stringify({ stage: 'capture_stopped', ...report })); + assert.ok(report.acknowledged > 0, 'No history-confirmed traffic'); + assert.equal(report.accepted, report.acknowledged, 'Capture acknowledgement mismatch'); + assert.equal(report.pendingFinal, 0, 'Journal pending after Stop'); + assert.equal(report.historyFailed, 0, 'History write failure'); + assert.equal(report.historyDropped, 0, 'History write drop'); + assert.equal(report.rendererErrors, 0, 'Renderer error'); + assert.ok(!report.driverBufferDroppedReported, 'Driver reported drops'); + await app.close(); app = null; + const log = await fs.readFile(path.join(profile, 'logs/desktop-backend.log'), 'utf8'); + assert.match(log, /capture and history drain confirmed/); + const rows = Number(execFileSync(python, ['-c', 'import sqlite3,sys; c=sqlite3.connect(sys.argv[1]); print(c.execute("SELECT COUNT(*) FROM packets").fetchone()[0]); c.close()', path.join(profile, 'data/history.db')], { encoding: 'utf8', windowsHide: true, timeout: 10000 }).trim()); + report.historyRows = rows; + assert.equal(rows, report.acknowledged, 'Stored history mismatch'); +} finally { + if (app) { + let timer; + try { await Promise.race([app.close(), new Promise((_, reject) => { timer = setTimeout(() => reject(Error('Candidate close deadline')), 15000); })]); } + catch { if (ownerPid) execFileSync('taskkill', ['/PID', String(ownerPid), '/T', '/F'], { windowsHide: true, stdio: 'ignore', timeout: 10000 }); } + finally { clearTimeout(timer); } + } + assert.equal(path.dirname(profile), path.resolve(os.tmpdir())); + assert.ok(path.basename(profile).startsWith('netbot-candidate-smoke-')); + await fs.rm(profile, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }); + await new Promise(resolve => listener.close(resolve)); +} +console.log(JSON.stringify({ ...report, temporaryProfileRemoved: true })); diff --git a/scripts/release/build_desktop_candidate.py b/scripts/release/build_desktop_candidate.py new file mode 100644 index 0000000..1826d52 --- /dev/null +++ b/scripts/release/build_desktop_candidate.py @@ -0,0 +1,107 @@ +"""Build a fresh isolated Windows candidate; never stage over installed/runtime files.""" +from __future__ import annotations + +from datetime import datetime, timezone +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import time + +import psutil + +ROOT = Path(__file__).resolve().parents[2] +GIB = 1024**3 + + +def folder_bytes(folder: Path) -> int: + total = 0 + for file in folder.rglob('*'): + try: + if file.is_file(): + total += file.stat().st_size + except FileNotFoundError: + pass + return total + + +def check_build_admission(destination: Path) -> None: + if shutil.disk_usage(destination).free < 5 * GIB: + raise RuntimeError('At least 5 GiB free space is required') + if folder_bytes(destination) > 2 * GIB: + raise RuntimeError('candidate_cache_budget_exceeded; review existing artifacts before rebuilding') + + +def guarded_run(command: list[str], cwd: Path, folder: Path) -> None: + with (folder / 'build.log').open('ab') as log: + process = subprocess.Popen(command, cwd=cwd, stdout=log, stderr=subprocess.STDOUT) + started = time.monotonic() + observed = {} + try: + while process.poll() is None: + owner = psutil.Process(process.pid) + observed.update((p.pid, p) for p in owner.children(recursive=True)) + if time.monotonic() - started > 900: + raise RuntimeError('candidate_build_deadline_exceeded') + if folder_bytes(folder) > 3 * GIB or shutil.disk_usage(folder).free < 2 * GIB: + raise RuntimeError('candidate_build_storage_budget_exceeded') + time.sleep(2) + if process.returncode: + raise RuntimeError('candidate_build_step_failed; inspect bounded build.log') + finally: + if process.poll() is None: + observed[process.pid] = psutil.Process(process.pid) + for item in reversed(list(observed.values())): + try: + item.kill() + except psutil.NoSuchProcess: + pass + psutil.wait_procs(list(observed.values()), timeout=5) + process.wait(timeout=5) + + +def main() -> None: + if os.name != 'nt': + raise RuntimeError('Windows candidate builder requires Windows') + destination = Path(os.environ['LOCALAPPDATA']) / 'NetBotCandidates' + destination.mkdir(parents=True, exist_ok=True) + check_build_admission(destination) + if subprocess.check_output(['git', 'status', '--porcelain'], cwd=ROOT, text=True).strip(): + raise RuntimeError('Commit candidate source changes before building an identifiable candidate') + revision = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip() + stamp = datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ') + # Deep scientific-library paths exceed legacy Windows limits under a long checkout. + folder = destination / f'{revision[:8]}-{stamp}' + folder.mkdir(parents=True, exist_ok=False) + guarded_run([sys.executable, '-m', 'PyInstaller', 'packaging/pyinstaller/netbotpro_backend.spec', + '--noconfirm', '--distpath', str(folder / 'backend-dist'), '--workpath', str(folder / 'work')], ROOT, folder) + node = shutil.which('node') + if not node: + raise RuntimeError('Node.js is required') + guarded_run([node, str(ROOT / 'frontend/node_modules/vite/bin/vite.js'), 'build'], ROOT / 'frontend', folder) + package = json.loads((ROOT / 'desktop/electron/package.json').read_text())['build'] + package.update(appId='com.netbotpro.candidate', productName='NetBotPro Candidate', + extraMetadata={'main': 'candidate-main.cjs'}, + directories={**package['directories'], 'output': str(folder / 'app')}) + package['files'] += ['candidate-main.cjs', 'candidate-profile.cjs'] + package['extraResources'] = [ + {'from': str(ROOT / 'frontend/dist'), 'to': 'frontend'}, + {'from': str(folder / 'backend-dist/netbotpro-backend'), 'to': 'runtime/backend'}, + ] + config = folder / 'electron-builder.json' + config.write_text(json.dumps(package, indent=2), encoding='utf-8') + guarded_run([node, str(ROOT / 'desktop/electron/node_modules/electron-builder/cli.js'), + '--dir', '--win', '--config', str(config)], ROOT / 'desktop/electron', folder) + executable = folder / 'app/win-unpacked/NetBotPro Candidate.exe' + if not executable.is_file(): + raise RuntimeError('Candidate executable missing') + report = {'revision': revision, 'executable': str(executable), 'artifact_bytes': folder_bytes(folder), + 'installed_app_modified': False, 'built_at': stamp} + (folder / 'candidate.json').write_text(json.dumps(report, indent=2), encoding='utf-8') + print(json.dumps(report)) + + +if __name__ == '__main__': + main() diff --git a/tests/test_desktop_candidate_builder.py b/tests/test_desktop_candidate_builder.py new file mode 100644 index 0000000..a151646 --- /dev/null +++ b/tests/test_desktop_candidate_builder.py @@ -0,0 +1,68 @@ +from pathlib import Path +import tempfile +from types import SimpleNamespace +import unittest +from unittest.mock import Mock, patch + +from scripts.release import build_desktop_candidate as candidate + + +class CandidateBuildGuardTests(unittest.TestCase): + def test_repeated_builds_cannot_accumulate_unbounded_artifacts(self): + with patch.object(candidate, 'folder_bytes', return_value=2 * candidate.GIB + 1), \ + patch.object(candidate.shutil, 'disk_usage', return_value=SimpleNamespace(free=10 * candidate.GIB)): + with self.assertRaisesRegex(RuntimeError, 'cache_budget_exceeded'): + candidate.check_build_admission(Path.cwd()) + + def test_admission_requires_free_disk_headroom(self): + with patch.object(candidate.shutil, 'disk_usage', return_value=SimpleNamespace(free=candidate.GIB)): + with self.assertRaisesRegex(RuntimeError, '5 GiB'): + candidate.check_build_admission(Path.cwd()) + + def run_guard(self, process, *, size=0, clock=(0, 1)): + owner = Mock() + owner.children.return_value = [] + with tempfile.TemporaryDirectory(prefix='netbot-candidate-unit-') as folder: + with patch.object(candidate.subprocess, 'Popen', return_value=process), \ + patch.object(candidate.psutil, 'Process', return_value=owner), \ + patch.object(candidate.psutil, 'wait_procs'), \ + patch.object(candidate, 'folder_bytes', return_value=size), \ + patch.object(candidate.shutil, 'disk_usage', return_value=SimpleNamespace(free=10 * candidate.GIB)), \ + patch.object(candidate.time, 'monotonic', side_effect=clock), \ + patch.object(candidate.time, 'sleep'): + try: + candidate.guarded_run(['fixture-command'], Path(folder), Path(folder)) + finally: + self.owner = owner + + def test_success_does_not_terminate_completed_process(self): + process = Mock(pid=123, returncode=0) + process.poll.side_effect = [None, 0, 0] + self.run_guard(process) + self.owner.kill.assert_not_called() + + def test_storage_budget_terminates_only_owned_process(self): + process = Mock(pid=123) + process.poll.return_value = None + with self.assertRaisesRegex(RuntimeError, 'storage_budget_exceeded'): + self.run_guard(process, size=3 * candidate.GIB + 1) + self.owner.kill.assert_called_once() + process.wait.assert_called_once_with(timeout=5) + + def test_deadline_terminates_owned_process(self): + process = Mock(pid=123) + process.poll.return_value = None + with self.assertRaisesRegex(RuntimeError, 'deadline_exceeded'): + self.run_guard(process, clock=(0, 901)) + self.owner.kill.assert_called_once() + + def test_failed_build_is_not_reported_as_success(self): + process = Mock(pid=123, returncode=1) + process.poll.return_value = 1 + with self.assertRaisesRegex(RuntimeError, 'step_failed'): + self.run_guard(process, clock=(0,)) + self.owner.kill.assert_not_called() + + +if __name__ == '__main__': + unittest.main()