feat: add candidate-bound domain qualification and planning

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-14 12:21:52 +03:00
1 parent 2242643000
commit 573da3f797
15 files changed
+2486 -10

No files matched your search

+91 -1
View File
@@ -24,7 +24,7 @@ DNS_CLI = Path(CLI_TMP.name) / 'autotune'
subprocess.run(['go', 'build', '-o', str(DNS_CLI), './cmd/zapret-autotune'],
cwd=ROOT, env=dict(os.environ, GOPROXY='off', GOTOOLCHAIN='local'), check=True)
FAKE = r'''#!/usr/bin/env python3
import ctypes, os, pathlib, signal, subprocess, sys, time
import ctypes, json, os, pathlib, signal, subprocess, sys, time
root = pathlib.Path(os.environ['FIXTURE'])
name = pathlib.Path(sys.argv[0]).name
scenario = os.environ['SCENARIO']
@@ -71,6 +71,11 @@ if name == 'date':
sys.exit(0)
if name == 'sleep':
seconds = int(sys.argv[1])
if scenario.startswith('qualify-') and (root / 'qualified').exists():
log('safe-idle')
os.kill(os.getppid(), signal.SIGTERM)
time.sleep(.02)
sys.exit(0)
if scenario.startswith('generic-budget-') and seconds != 60:
os.execv(os.environ['REAL_SLEEP'], [os.environ['REAL_SLEEP'], *sys.argv[1:]])
if seconds == 60 and scenario in ('generic-ss', 'generic-unsafe', 'generic-budget-monitor', 'generic-budget-reconcile', 'generic-budget-ack'):
@@ -125,6 +130,27 @@ if name == 'zapret2':
sys.exit(1)
sys.exit(0)
assert name == 'autotune'
if sys.argv[1] == 'qualification-race-barrier':
# Deterministic interleaving: child has exited, shell has not reached wait.
pid = int(sys.argv[2])
deadline = time.monotonic() + 5
while True:
stat = pathlib.Path('/proc/' + str(pid) + '/stat')
if not stat.exists() or stat.read_text().rsplit(') ', 1)[1].split()[0] == 'Z':
break
assert time.monotonic() < deadline, 'qualifier did not exit at barrier'
time.sleep(.001)
log('term-before-parent-reap')
os.kill(os.getppid(), signal.SIGTERM)
sys.exit(0)
if sys.argv[1] == 'qualification-completion-check':
ack = root / 'completion-ack'
if not ack.exists() or ack.is_symlink() or ack.stat().st_mode & 0o777 != 0o600:
sys.exit(78)
payload = json.loads(ack.read_text())
assert payload == {'pid': int(sys.argv[2]), 'code': int(sys.argv[3]), 'cleanup_complete': True}
log('completion-checked')
sys.exit(0)
if sys.argv[1:] == ['target-config-check']:
sys.exit(0)
if sys.argv[1] == 'dns-forwarder':
@@ -194,6 +220,40 @@ if sys.argv[1] == 'dns-forwarder':
os.kill(os.getpid(), signal.SIGKILL)
while True:
time.sleep(.01)
if sys.argv[1] == 'qualify':
assert sys.argv[2:] == ['--config', '/operator/resources.json', '--strategies', '/operator/strategies.json', '--isolated']
assert os.getsid(0) == os.getpid(), 'qualifier lacks owned session'
assert 'SS_PASSWORD' not in os.environ
events = (root / 'events').read_text().splitlines()
assert 'start' not in events and 'stop' not in events
assert events[-1] == 'reconcile', events
log('qualify')
assert increment('qualification_runs') == 1, 'qualification restarted'
def acknowledge(code):
ack = root / 'completion-ack'
ack.write_text(json.dumps({'pid': os.getpid(), 'code': code, 'cleanup_complete': True}))
ack.chmod(0o600)
if not scenario.endswith('-race'):
(root / 'qualified').touch()
if scenario in ('qualify-unsafe', 'qualify-unsafe-race'):
if not scenario.endswith('-race'):
(root / 'qualified').touch()
sys.exit(78)
if scenario in ('qualify-blocked', 'qualify-blocked-race'):
acknowledge(78)
sys.exit(78)
if scenario == 'qualify-cancel':
def restored(*_):
log('qualification-restored')
acknowledge(0)
sys.exit(0)
signal.signal(signal.SIGTERM, restored)
os.kill(os.getppid(), signal.SIGTERM)
while True:
time.sleep(.01)
log('qualification-restored')
acknowledge(0)
sys.exit(0)
command = sys.argv[2]
if command == 'init':
log('init')
@@ -304,6 +364,11 @@ def run(scenario, expected, dns='', mode='native'):
text = text.replace('config_dir=/run/zapret-autotune-ss', f'config_dir={configs}')
text = text.replace('/usr/local/bin/zapret-autotune', str(binary / 'autotune'))
text = text.replace('/opt/zapret2/init.d/sysv/zapret2', str(binary / 'zapret2'))
if scenario.endswith('-race'):
barrier = f'{binary / "autotune"} qualification-race-barrier "$monitor_pid"\n'
loop = 'while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do'
assert loop in text
text = text.replace(loop, barrier + loop, 1)
if realtime:
# Private fixture scaling only: production has no configurable grace.
text = text.replace('$(date +%s) + 80', '$(date +%s) + 12')
@@ -317,6 +382,12 @@ def run(scenario, expected, dns='', mode='native'):
env['ZAPRET_AUTOTUNE_RESOURCE_CONFIG'] = '/operator/resources.json'
else:
env.pop('ZAPRET_AUTOTUNE_RESOURCE_CONFIG', None)
env.pop('ZAPRET_AUTOTUNE_QUALIFY_CONFIG', None)
env.pop('ZAPRET_AUTOTUNE_STRATEGY_CONFIG', None)
if scenario.startswith('qualify-'):
env['ZAPRET_AUTOTUNE_QUALIFY_CONFIG'] = '/operator/resources.json'
if scenario != 'qualify-missing':
env['ZAPRET_AUTOTUNE_STRATEGY_CONFIG'] = '/operator/strategies.json'
if mode is None:
env.pop('ZAPRET_AUTOTUNE_DNS_MODE', None)
else:
@@ -432,6 +503,25 @@ for scenario in ('dns-term', 'dns-int'):
events = run(scenario, 0, mode='doh')
assert events == ['dns-start', 'dns-stop'], events
for scenario in ('qualify-pass', 'qualify-negative', 'qualify-blocked', 'qualify-cancel'):
events = run(scenario, 0, mode='doh')
assert events.count('qualify') == 1 and not any(e.startswith('monitor ') for e in events), events
assert 'start' not in events and 'stop' not in events, events
assert events.index('dns-ready') < events.index('qualify') < events.index('dns-stop'), events
if scenario != 'qualify-blocked':
assert events.index('qualification-restored') < events.index('dns-stop'), events
events = run('qualify-missing', 64, mode='doh')
assert not events, events
for scenario, code in (('qualify-blocked-race', 0), ('qualify-unsafe-race', 78), ('qualify-unsafe', 78)):
events = run(scenario, code, mode='doh')
assert events.count('qualify') == 1 and 'start' not in events and 'stop' not in events, events
if scenario.endswith('-race'):
assert events.count('term-before-parent-reap') == 1, events
if code == 0:
assert events.index('term-before-parent-reap') < events.index('completion-checked') < events.index('dns-stop'), events
else:
assert 'completion-checked' not in events, events
def check_watchdog_traps(shell):
"""Same bounded_wait from normal flow, plain EXIT, and TERM -> EXIT traps."""