mirror of
https://github.com/BigBodyCobain/Shadowbroker.git
synced 2026-12-06 01:24:28 +01:00
voice-agent U7: hardening & acceptance — cross-milestone integration matrix (opt-in uniformity, deterministic journey, cross-surface budget latch, kill switch, watchdog seam, MESH_ONLY guard), RTL/reduced-motion a11y pass, delta-health E2E script (#36), Tauri mic spike notes, acceptance scripts (#37-44), CSP posture decision documented in threat-model
This commit is contained in:
1 parent
bdc760d92b
commit
3430be7232
4 files changed
+529
-5
No files matched your search
@@ -0,0 +1,296 @@
|
||||
"""Voice-agent U7 integration matrix (plan §6 U7 row — spans U2–U6 surfaces).
|
||||
|
||||
Cross-milestone rows that no single-subsystem suite covers end-to-end:
|
||||
|
||||
1. Opt-in gate uniformity: every billable/sensitive voice endpoint refuses
|
||||
with honest reasons while the two-step opt-in is incomplete (status stays
|
||||
200 — it is the surface that EXPLAINS the refusal).
|
||||
2. Full operator journey in deterministic mode (zero model, zero outbound):
|
||||
status → tools manifest → stateless tool execute → turn/hud-summary/
|
||||
interrogate degrade with 503 ``chat_unconfigured``.
|
||||
3. Cross-surface budget latch: one persisted daily ledger, crossed once,
|
||||
429s session + turn + hud-summary + interrogate alike (§2.7 latched cap).
|
||||
4. Status never-500 contract under a hostile store (catch-all honesty).
|
||||
5. Kill-switch lifecycle: mint → DELETE → 404 on repeat.
|
||||
6. Watchdog producer hook: ``broadcast_to_agents`` fans ``type:"alert"``
|
||||
payloads into the voice tap (§3.8) without voice importing the watchdog.
|
||||
7. MESH_ONLY source guard: the lean-mode stub + import guard remain wired.
|
||||
|
||||
The WS upgrade/wire-contract suites live in test_voice_session_api.py; the
|
||||
per-endpoint gating matrices live in test_voice_router.py. This file asserts
|
||||
the seams BETWEEN the milestones.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
import services.config as config_mod
|
||||
import services.api_settings as api_settings_mod
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def voice_env(tmp_path, monkeypatch):
|
||||
"""Isolate all VOICE_* configuration (same contract as test_voice_router)."""
|
||||
from services.voice_broker import _sessions
|
||||
from services.voice_session_tokens import reset_voice_session_tokens_for_tests
|
||||
from services.voice_daily_budget import _reset_for_tests as reset_budget
|
||||
from limiter import limiter
|
||||
|
||||
for name in list(os.environ):
|
||||
if name.startswith("VOICE_"):
|
||||
monkeypatch.delenv(name, raising=False)
|
||||
monkeypatch.setattr(api_settings_mod, "OPERATOR_KEYS_ENV_PATH", tmp_path / "operator_keys.env")
|
||||
monkeypatch.setenv("SB_DATA_DIR", str(tmp_path / "data"))
|
||||
config_mod.get_settings.cache_clear()
|
||||
_sessions.clear()
|
||||
reset_voice_session_tokens_for_tests()
|
||||
reset_budget()
|
||||
yield monkeypatch
|
||||
config_mod.get_settings.cache_clear()
|
||||
_sessions.clear()
|
||||
reset_voice_session_tokens_for_tests()
|
||||
reset_budget()
|
||||
try:
|
||||
limiter.reset()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _enable(monkeypatch, **extra):
|
||||
monkeypatch.setenv("VOICE_AGENT_OUTBOUND_ENABLE", "allow")
|
||||
monkeypatch.setenv("VOICE_AGENT_OUTBOUND_ACKNOWLEDGE", "true")
|
||||
for key, value in extra.items():
|
||||
monkeypatch.setenv(key, value)
|
||||
config_mod.get_settings.cache_clear()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def loopback(voice_env):
|
||||
from main import app
|
||||
|
||||
class _Client:
|
||||
def __init__(self, peer_ip: str = "127.0.0.1"):
|
||||
import asyncio
|
||||
|
||||
self._loop = asyncio.new_event_loop()
|
||||
self._transport = ASGITransport(app=app, client=(peer_ip, 12345))
|
||||
self._base = f"http://{peer_ip}:8000"
|
||||
|
||||
def _do(self, method: str, url: str, **kw):
|
||||
import asyncio
|
||||
|
||||
async def go():
|
||||
async with AsyncClient(transport=self._transport, base_url=self._base) as ac:
|
||||
return await ac.request(method, url, **kw)
|
||||
return self._loop.run_until_complete(go())
|
||||
|
||||
def get(self, url, **kw): return self._do("GET", url, **kw)
|
||||
def post(self, url, **kw): return self._do("POST", url, **kw)
|
||||
def delete(self, url, **kw): return self._do("DELETE", url, **kw)
|
||||
def close(self): self._loop.close()
|
||||
|
||||
c = _Client()
|
||||
yield c
|
||||
c.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row 1 — opt-in gate uniformity across every sensitive surface
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestOptInUniformity:
|
||||
def test_every_sensitive_surface_refuses_while_opt_in_incomplete(self, loopback):
|
||||
"""No voice surface leaks billable functionality pre-opt-in. Session
|
||||
and chat answer 403 (§4.3 canonical refusal); the U5/U6 surfaces
|
||||
answer 409 ``voice_disabled`` — each with honest reasons."""
|
||||
expectations = [
|
||||
("POST", "/api/voice/session", 403, {}),
|
||||
("POST", "/api/voice/chat", 403, {"text": "hello"}),
|
||||
("POST", "/api/voice/hud-summary", 409, {}),
|
||||
("POST", "/api/voice/turn", 409, {"text": "hello"}),
|
||||
("POST", "/api/voice/interrogate", 409, {"question": "why this alert?"}),
|
||||
]
|
||||
for method, url, expected, payload in expectations:
|
||||
response = loopback.post(url, json=payload) if method == "POST" else loopback.get(url)
|
||||
assert response.status_code == expected, f"{url}: {response.status_code} != {expected}"
|
||||
body = response.json()
|
||||
assert body.get("error") in ("opt_in_required", "voice_disabled"), url
|
||||
assert body.get("reasons"), f"{url} must carry human-readable refusal reasons"
|
||||
|
||||
def test_status_stays_200_and_explains_the_refusal(self, loopback):
|
||||
response = loopback.get("/api/voice/status")
|
||||
assert response.status_code == 200
|
||||
body = response.json()
|
||||
assert body["ok"] is True
|
||||
assert body["effectiveEnable"] is False
|
||||
assert body["reasons"], "status is the surface that explains why voice is off"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row 2 — deterministic operator journey (zero model, zero outbound)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDeterministicJourney:
|
||||
def test_full_journey_zero_model(self, loopback, monkeypatch):
|
||||
_enable(monkeypatch)
|
||||
# Status: enabled but provider unresolved — honest degradedReason.
|
||||
status = loopback.get("/api/voice/status").json()
|
||||
assert status["effectiveEnable"] is True
|
||||
assert status["provider"]["resolved"] is None
|
||||
assert status["degradedReason"], "unresolved provider must carry verbatim reason"
|
||||
|
||||
# Tools manifest reachable (U3 mount inside U2's router).
|
||||
tools = loopback.get("/api/voice/tools")
|
||||
assert tools.status_code == 200
|
||||
tool_names = {t.get("name") for t in tools.json().get("tools", [])}
|
||||
assert tool_names, "manifest must not be empty"
|
||||
assert "what_changed" in tool_names
|
||||
|
||||
# Stateless server-tool execute works with NO model (deterministic).
|
||||
executed = loopback.post("/api/voice/tool", json={"tool": "what_changed", "args": {}})
|
||||
assert executed.status_code == 200
|
||||
assert isinstance(executed.json(), dict)
|
||||
|
||||
# Model-dialing surfaces degrade with 503 chat_unconfigured.
|
||||
assert loopback.post("/api/voice/turn", json={"text": "fly to Kyiv"}).status_code == 503
|
||||
# Minimal VALID sb.grounding.v1: schema + view + layers (§2.4 validator).
|
||||
valid_snapshot = {"schema": "sb.grounding.v1", "view": {}, "layers": {}}
|
||||
assert loopback.post("/api/voice/hud-summary", json={"snapshot": valid_snapshot}).status_code == 503
|
||||
|
||||
# Interrogate with a real question but no model: either the target
|
||||
# resolver refuses first (400 no_target) or the chat dial degrades
|
||||
# (503) — both are honest; a 4xx validation error is NOT acceptable
|
||||
# here because the question is well-formed.
|
||||
interrogate = loopback.post("/api/voice/interrogate", json={"question": "why this alert?"})
|
||||
assert interrogate.status_code in (400, 503)
|
||||
if interrogate.status_code == 400:
|
||||
assert interrogate.json().get("error") == "no_target"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row 3 — one persisted ledger latches EVERY billable surface
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCrossSurfaceBudgetLatch:
|
||||
def test_crossed_daily_budget_429s_all_billable_surfaces(self, loopback, monkeypatch):
|
||||
_enable(monkeypatch, VOICE_OPENAI_API_KEY="sk-test", VOICE_DAILY_BUDGET_USD="0.01")
|
||||
|
||||
from services.voice_daily_budget import record_response
|
||||
|
||||
record_response(usd=0.02) # 2x the daily budget, persisted to the ledger
|
||||
|
||||
# Session (Gate 3) — carries Retry-After.
|
||||
session = loopback.post("/api/voice/session", json={"tier": "standard"})
|
||||
assert session.status_code == 429
|
||||
assert session.json()["error"] == "budget"
|
||||
assert "retry-after" in {k.lower() for k in session.headers}
|
||||
|
||||
# The U5/U6 chat-dialing surfaces share the same latched cap.
|
||||
valid_snapshot = {"schema": "sb.grounding.v1", "view": {}, "layers": {}}
|
||||
for url, payload in (
|
||||
("/api/voice/turn", {"text": "fly to Kyiv"}),
|
||||
("/api/voice/hud-summary", {"snapshot": valid_snapshot}),
|
||||
("/api/voice/interrogate", {"question": "why this alert?"}),
|
||||
):
|
||||
response = loopback.post(url, json=payload)
|
||||
assert response.status_code == 429, f"{url}: {response.status_code}"
|
||||
assert response.json()["error"] == "budget", url
|
||||
|
||||
# Status surfaces the exhausted state honestly.
|
||||
spend = loopback.get("/api/voice/status").json()["dailySpend"]
|
||||
assert spend["remainingUsd"] == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row 4 — status never-500 contract under a hostile store
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestStatusNeverFails:
|
||||
def test_status_survives_exploding_spend_view(self, loopback, monkeypatch):
|
||||
_enable(monkeypatch)
|
||||
|
||||
def _explode(*a, **kw):
|
||||
raise RuntimeError("disk on fire")
|
||||
|
||||
monkeypatch.setattr("services.voice_broker.daily_spend_view", _explode)
|
||||
response = loopback.get("/api/voice/status")
|
||||
assert response.status_code == 200
|
||||
body = response.json()
|
||||
assert body["ok"] is True
|
||||
assert "status unavailable" in " ".join(body.get("reasons", [])) or "status unavailable" in str(body.get("degradedReason"))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row 5 — kill-switch lifecycle
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestKillSwitchLifecycle:
|
||||
def test_mint_then_delete_then_404(self, loopback, monkeypatch):
|
||||
_enable(monkeypatch, VOICE_OPENAI_API_KEY="sk-test")
|
||||
created = loopback.post("/api/voice/session", json={"tier": "standard"})
|
||||
assert created.status_code == 201
|
||||
session_id = created.json()["sessionId"]
|
||||
|
||||
killed = loopback.delete(f"/api/voice/session/{session_id}")
|
||||
assert killed.status_code == 200
|
||||
assert killed.json()["ok"] is True
|
||||
|
||||
repeat = loopback.delete(f"/api/voice/session/{session_id}")
|
||||
assert repeat.status_code == 404
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row 6 — watchdog producer hook (§3.8 seam)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestWatchdogProducerHook:
|
||||
def test_broadcast_to_agents_fans_alerts_into_voice_tap(self, monkeypatch):
|
||||
"""The U6 producer hook in ai_intel.broadcast_to_agents forwards
|
||||
type:alert payloads to publish_watchdog_alert — voice never imports
|
||||
the watchdog, the watchdog never learns about voice."""
|
||||
import routers.ai_intel as ai_intel
|
||||
|
||||
seen: list = []
|
||||
monkeypatch.setattr(
|
||||
"services.voice_broker.publish_watchdog_alert",
|
||||
lambda alert: seen.append(alert) or 0,
|
||||
)
|
||||
|
||||
asyncio.run(ai_intel.broadcast_to_agents({"type": "alert", "alert": {"kind": "geofence", "id": "a1"}}))
|
||||
assert seen == [{"kind": "geofence", "id": "a1"}]
|
||||
|
||||
# Non-alert broadcasts must NOT touch the voice tap.
|
||||
asyncio.run(ai_intel.broadcast_to_agents({"type": "task", "payload": {}}))
|
||||
assert len(seen) == 1
|
||||
|
||||
def test_publish_with_no_active_relays_is_a_safe_noop(self):
|
||||
from services.voice_broker import publish_watchdog_alert
|
||||
|
||||
assert publish_watchdog_alert({"kind": "geofence"}) == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row 7 — MESH_ONLY lean-mode guard stays wired at both ends
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestMeshOnlyGuardPresent:
|
||||
def test_router_and_main_keep_the_lean_mode_defenses(self):
|
||||
router_src = (Path(__file__).resolve().parent.parent / "routers" / "voice.py").read_text(encoding="utf-8")
|
||||
main_src = (Path(__file__).resolve().parent.parent / "main.py").read_text(encoding="utf-8")
|
||||
# routers/voice.py: canonical MESH_ONLY import guard (§1.5).
|
||||
assert "MESH_ONLY" in router_src, "voice router lost its lean-mode import guard"
|
||||
# main.py: stubbed router branch + optional loader + include.
|
||||
assert "voice_router" in main_src, "main.py lost the voice router wiring"
|
||||
@@ -54,9 +54,18 @@ third-party egress lane with its own discipline:
|
||||
`error` strings (structured fields only), server-side validation as the sole
|
||||
authority on tool calls, and narration that asserts only what `ok:true`
|
||||
envelopes actually show.
|
||||
- **Pre-existing desktop-shell allowance (documented, unchanged by this
|
||||
feature).** The Tauri shell CSP already permits page→third-party https egress
|
||||
- **Pre-existing desktop-shell allowance (U7 posture decision: documented, not
|
||||
tightened).** The Tauri shell CSP already permits page→third-party https egress
|
||||
(`connect-src 'self' http://127.0.0.1:* https://*`,
|
||||
`desktop-shell/tauri.conf.json`). The voice feature adds no new CSP origins;
|
||||
closing or tightening that pre-existing allowance is tracked separately on the
|
||||
hardening milestone.
|
||||
`desktop-shell/tauri.conf.json:36`). The voice feature adds no new CSP origins.
|
||||
Decision rationale: the companion window renders the same SPA as the browser
|
||||
deployment, whose MapLibre basemap legitimately fetches tiles and fonts from
|
||||
third-party HTTPS CDNs (`*.basemaps.cartocdn.com` et al. — `docs/OUTBOUND_DATA.md`
|
||||
"Basemap CDN" #354); dropping `https://*` without a per-origin allowlist would
|
||||
break the basemap in the packaged shell. The voice agent's privacy guarantees
|
||||
therefore rest on code discipline, not the shell CSP: server-side brokering
|
||||
only, no provider keys in the client, and the SpeechRecognition ban below.
|
||||
Replacing `https://*` with an explicit per-origin tile/font allowlist is the
|
||||
proper follow-up and belongs to the basemap self-hosting decision (#354), made
|
||||
behind its own two-step acknowledgment if it ever changes operator-visible
|
||||
behavior.
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* §6 U7 — reduced-motion / RTL hardening pass.
|
||||
*
|
||||
* Reduced-motion: the animated phase indicators (waveform, spinner, TX
|
||||
* equalizer) must carry Tailwind `motion-reduce:` guards — asserted
|
||||
* structurally here because jsdom cannot emulate the media query.
|
||||
* RTL: the app sets `document.documentElement.dir = 'rtl'` for fa/ar
|
||||
* locales (i18n/index.tsx:121); both voice surfaces must render intact
|
||||
* with their key affordances present — no component may assume LTR to
|
||||
* function.
|
||||
*/
|
||||
import React from 'react';
|
||||
import { cleanup, render, screen } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
import VoiceMicButton from '@/components/VoiceMicButton';
|
||||
import VoiceHudPanel from '@/components/VoiceHudPanel';
|
||||
import { voiceController } from '@/lib/voiceController';
|
||||
import type { VoiceStatusResponse } from '@/lib/voiceTypes';
|
||||
|
||||
function enabledStatus(overrides: Partial<VoiceStatusResponse> = {}): VoiceStatusResponse {
|
||||
return {
|
||||
ok: true,
|
||||
effectiveEnable: true,
|
||||
opt_in_state: 'enabled',
|
||||
flags: { enableSet: true, acknowledgeSet: true },
|
||||
provider: {
|
||||
resolved: 'openai_realtime',
|
||||
requestedMode: 'auto',
|
||||
openaiKeySet: true,
|
||||
compatKeySet: false,
|
||||
compatBaseUrlSet: false,
|
||||
chatModelConfigured: false,
|
||||
},
|
||||
degradedReason: null,
|
||||
reasons: [],
|
||||
mode: 'full',
|
||||
costDefaults: { warnUsd: 'off', capUsd: 'off', dailyBudgetUsd: 'off' },
|
||||
dailySpend: { date: '', usdSpent: 0, budgetUsd: 'off', remainingUsd: 'off', ledgerQuarantined: false },
|
||||
activeSessions: 0,
|
||||
maxConcurrent: 1,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('voice a11y pass (U7)', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.setItem('sb_voice_hint_seen_v1', '1');
|
||||
voiceController.resetForNewSession();
|
||||
voiceController.resetTranscript();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
document.documentElement.dir = '';
|
||||
});
|
||||
|
||||
it('animated indicators carry motion-reduce guards', () => {
|
||||
voiceController.applyStatus(enabledStatus());
|
||||
voiceController.backToIdle();
|
||||
voiceController.beginPtt();
|
||||
voiceController.markListening();
|
||||
|
||||
const { container } = render(<VoiceMicButton />);
|
||||
const waveform = container.querySelector('[data-testid="voice-waveform"]');
|
||||
expect(waveform).not.toBeNull();
|
||||
// The container hides entirely under reduced motion; each animated bar
|
||||
// freezes instead of pulsing.
|
||||
expect(waveform!.className).toContain('motion-reduce:hidden');
|
||||
container.querySelectorAll('.animate-pulse').forEach((bar) => {
|
||||
expect(bar.className).toContain('motion-reduce:animate-none');
|
||||
});
|
||||
});
|
||||
|
||||
it('thinking spinner freezes under reduced motion', () => {
|
||||
voiceController.applyStatus(enabledStatus());
|
||||
voiceController.backToIdle();
|
||||
// idle→thinking is illegal (LEGAL_TRANSITIONS); the real path is
|
||||
// idle → listening → thinking.
|
||||
voiceController.markListening();
|
||||
voiceController.beginThinking();
|
||||
const { container } = render(<VoiceMicButton />);
|
||||
const spinner = container.querySelector('.animate-spin');
|
||||
expect(spinner).not.toBeNull();
|
||||
expect(spinner!.className).toContain('motion-reduce:animate-none');
|
||||
});
|
||||
|
||||
it('VoiceMicButton renders intact under RTL', () => {
|
||||
document.documentElement.dir = 'rtl';
|
||||
voiceController.applyStatus(enabledStatus());
|
||||
voiceController.backToIdle();
|
||||
|
||||
render(<VoiceMicButton />);
|
||||
expect(screen.getByTestId('voice-mic-button')).not.toBeNull();
|
||||
// Phase chip + accessible label survive direction flip.
|
||||
expect(screen.getByLabelText(/voice/i)).not.toBeNull();
|
||||
});
|
||||
|
||||
it('degraded TXT badge renders under RTL', () => {
|
||||
document.documentElement.dir = 'rtl';
|
||||
voiceController.applyStatus(
|
||||
enabledStatus({ provider: { ...enabledStatus().provider, resolved: null }, degradedReason: 'no model' }),
|
||||
);
|
||||
voiceController.backToIdle();
|
||||
|
||||
render(<VoiceMicButton />);
|
||||
expect(screen.getByTestId('voice-txt-badge')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('VoiceHudPanel renders intact under RTL', () => {
|
||||
document.documentElement.dir = 'rtl';
|
||||
voiceController.applyStatus(enabledStatus());
|
||||
voiceController.pushTurn({ role: 'agent', kind: 'say', text: 'Brief ready.' });
|
||||
|
||||
render(<VoiceHudPanel onSubmit={vi.fn()} onClose={vi.fn()} />);
|
||||
expect(screen.getByRole('textbox')).not.toBeNull();
|
||||
expect(screen.getByText('Brief ready.')).not.toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Acceptance #36 (voice-agent plan §7.E) — Delta-mode health E2E.
|
||||
*
|
||||
* Post-U0 observable: with the `tracked_flights` / `correlations`
|
||||
* `_mark_fresh` stamps in place, a polling session that echoes the store's
|
||||
* per-layer versions back via `lv=` must be answered `X-SB-Mode: delta`
|
||||
* for the overwhelming majority of fast polls (pre-fix this was 0% because
|
||||
* the missing stamps forced the all-keys fallback to a full snapshot —
|
||||
* routers/data.py returns a snapshot when ANY requested delta key lacks a
|
||||
* version).
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/voice/delta_health_e2e.mjs [--base http://127.0.0.1:8000]
|
||||
* [--polls 30] [--interval-ms 2000]
|
||||
* [--min-rate 0.8]
|
||||
*
|
||||
* Exit 0 when the delta rate meets --min-rate, exit 1 otherwise.
|
||||
* This is the automated headless variant of the plan's "5-minute browser
|
||||
* session" — same protocol (initial snapshot → lv echo → delta), no UI.
|
||||
*/
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
function argOf(name, dflt) {
|
||||
const i = args.indexOf(name);
|
||||
return i >= 0 && args[i + 1] ? args[i + 1] : dflt;
|
||||
}
|
||||
|
||||
const BASE = String(argOf('--base', 'http://127.0.0.1:8000')).replace(/\/$/, '');
|
||||
const POLLS = Math.max(2, parseInt(argOf('--polls', '30'), 10));
|
||||
const INTERVAL_MS = Math.max(250, parseInt(argOf('--interval-ms', '2000'), 10));
|
||||
const MIN_RATE = Math.min(1, Math.max(0, parseFloat(argOf('--min-rate', '0.8'))));
|
||||
|
||||
function encodeLv(layerVersions) {
|
||||
return Object.entries(layerVersions)
|
||||
.map(([k, v]) => `${encodeURIComponent(k)}:${encodeURIComponent(String(v))}`)
|
||||
.join(',');
|
||||
}
|
||||
|
||||
async function poll(url) {
|
||||
const res = await fetch(url, { headers: { accept: 'application/json' } });
|
||||
if (!res.ok) throw new Error(`${url} -> HTTP ${res.status}`);
|
||||
const modeHeader = res.headers.get('x-sb-mode') || '(none)';
|
||||
const body = await res.json();
|
||||
return { modeHeader, mode: String(body.mode || 'snapshot'), body };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log(`Delta-health E2E against ${BASE}`);
|
||||
console.log(`polls=${POLLS} interval=${INTERVAL_MS}ms min-rate=${Math.round(MIN_RATE * 100)}%\n`);
|
||||
|
||||
// 1. Startup snapshot (initial=true is the capped first-paint payload).
|
||||
const first = await poll(`${BASE}/api/live-data/fast?initial=true`);
|
||||
console.log(`initial: mode=${first.mode} header=${first.modeHeader}`);
|
||||
let layerVersions = first.body.layer_versions;
|
||||
if (!layerVersions || typeof layerVersions !== 'object') {
|
||||
console.error('FAIL: initial snapshot carried no layer_versions — cannot enter delta mode.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 2. Echo versions back; the server should answer delta while nothing
|
||||
// invalidates the ring (and snapshot only on genuine skew/miss).
|
||||
let delta = 0;
|
||||
let snapshot = 0;
|
||||
const rows = [];
|
||||
for (let i = 0; i < POLLS; i++) {
|
||||
await new Promise((r) => setTimeout(r, INTERVAL_MS));
|
||||
let p;
|
||||
try {
|
||||
p = await poll(`${BASE}/api/live-data/fast?lv=${encodeLv(layerVersions)}`);
|
||||
} catch (err) {
|
||||
console.error(`poll ${i + 1}: ${err.message}`);
|
||||
snapshot += 1;
|
||||
rows.push({ i: i + 1, mode: 'error' });
|
||||
continue;
|
||||
}
|
||||
if (p.body.layer_versions && typeof p.body.layer_versions === 'object') {
|
||||
layerVersions = p.body.layer_versions;
|
||||
}
|
||||
if (p.mode === 'delta') delta += 1;
|
||||
else snapshot += 1;
|
||||
rows.push({ i: i + 1, mode: p.mode });
|
||||
console.log(`poll ${String(i + 1).padStart(3)}: mode=${p.mode} header=${p.modeHeader}`);
|
||||
}
|
||||
|
||||
const rate = delta / (delta + snapshot);
|
||||
console.log(`\nResult: ${delta} delta / ${snapshot} snapshot — rate ${(rate * 100).toFixed(1)}% (floor ${Math.round(MIN_RATE * 100)}%)`);
|
||||
if (rate < MIN_RATE) {
|
||||
console.error('FAIL: delta rate below floor. If tracked_flights/correlations lost their');
|
||||
console.error('_mark_fresh stamps, routers/data.py silently degrades EVERY fast poll');
|
||||
console.error('to a full snapshot — check backend/tests/test_freshness_stamps.py.');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('PASS: delta-mode health confirmed.');
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(`FATAL: ${err.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user