guldo-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- guldo/__init__.py +0 -0
- guldo/_src/COMPILER.md +329 -0
- guldo/_src/DEPLOY.md +200 -0
- guldo/_src/LAUNCH.md +85 -0
- guldo/_src/PROTOCOL.md +294 -0
- guldo/_src/RULES.md +25 -0
- guldo/_src/SUBMIT.md +170 -0
- guldo/_src/__init__.py +0 -0
- guldo/_src/attest.py +257 -0
- guldo/_src/build_site.py +958 -0
- guldo/_src/bundle_templates/check.py +1382 -0
- guldo/_src/bundle_templates/qcheck.py +253 -0
- guldo/_src/bundle_templates/qscore.py +402 -0
- guldo/_src/bundle_templates/score.py +649 -0
- guldo/_src/certificate.py +828 -0
- guldo/_src/certs_site.py +108 -0
- guldo/_src/claims.py +343 -0
- guldo/_src/compile_domain.py +1312 -0
- guldo/_src/compile_quality.py +286 -0
- guldo/_src/count_runner.py +248 -0
- guldo/_src/counted.py +274 -0
- guldo/_src/declared.py +215 -0
- guldo/_src/deploy/Caddyfile +48 -0
- guldo/_src/deploy/ledger-backup.service +9 -0
- guldo/_src/deploy/ledger-backup.sh +73 -0
- guldo/_src/deploy/ledger-backup.timer +10 -0
- guldo/_src/deploy/ledger-export.service +12 -0
- guldo/_src/deploy/ledger-export.timer +9 -0
- guldo/_src/deploy/ledger-web.service +38 -0
- guldo/_src/deploy/pin_cpu.sh +34 -0
- guldo/_src/deploy/provision_eval.sh +128 -0
- guldo/_src/deploy_smoke.py +148 -0
- guldo/_src/dlog_experiment.py +165 -0
- guldo/_src/domains/aidecode/compiled.json +586 -0
- guldo/_src/domains/aidecode/decl.py +134 -0
- guldo/_src/domains/aidecode/reference.py +73 -0
- guldo/_src/domains/aiinfer/compiled.json +722 -0
- guldo/_src/domains/aiinfer/decl.py +91 -0
- guldo/_src/domains/aiinfer/reference.py +42 -0
- guldo/_src/domains/dlog/compiled.json +248 -0
- guldo/_src/domains/dlog/decl.py +41 -0
- guldo/_src/domains/dlog/reference.py +10 -0
- guldo/_src/domains/maxcut/compiled.json +291 -0
- guldo/_src/domains/maxcut/decl.py +46 -0
- guldo/_src/domains/maxcut/reference.py +16 -0
- guldo/_src/drift_check.py +89 -0
- guldo/_src/export_bundle.py +750 -0
- guldo/_src/extrapolate.py +326 -0
- guldo/_src/families.py +53 -0
- guldo/_src/gen_cheat_table.py +36 -0
- guldo/_src/gpu_meter.py +634 -0
- guldo/_src/gpu_runner.py +63 -0
- guldo/_src/harness.py +273 -0
- guldo/_src/instances.py +86 -0
- guldo/_src/instances_factor.py +40 -0
- guldo/_src/instances_mlwe.py +69 -0
- guldo/_src/judge.py +278 -0
- guldo/_src/limits_plan.py +158 -0
- guldo/_src/official.py +189 -0
- guldo/_src/ops.py +281 -0
- guldo/_src/portal.py +313 -0
- guldo/_src/portal_core.py +57 -0
- guldo/_src/power.py +174 -0
- guldo/_src/prereg.py +173 -0
- guldo/_src/public_portal.py +500 -0
- guldo/_src/quality_core.py +126 -0
- guldo/_src/recheck.py +236 -0
- guldo/_src/remeasure.py +88 -0
- guldo/_src/report_html.py +493 -0
- guldo/_src/results/.keep +0 -0
- guldo/_src/rounds.py +846 -0
- guldo/_src/runner.py +52 -0
- guldo/_src/sandbox/Dockerfile +11 -0
- guldo/_src/sandbox/Dockerfile.v2 +19 -0
- guldo/_src/sandbox/README.md +40 -0
- guldo/_src/sandbox/linux_selftest.py +117 -0
- guldo/_src/sandbox/meter.py +89 -0
- guldo/_src/sandbox/meter_core.py +28 -0
- guldo/_src/site_assets.py +176 -0
- guldo/_src/site_live_check.py +79 -0
- guldo/_src/solvers/aidecode_flops_kv.py +73 -0
- guldo/_src/solvers/aidecode_flops_lossy.py +73 -0
- guldo/_src/solvers/aidecode_flops_ref.py +64 -0
- guldo/_src/solvers/aidecode_kv.py +76 -0
- guldo/_src/solvers/aidecode_kv_f32.py +77 -0
- guldo/_src/solvers/aidecode_lossy.py +76 -0
- guldo/_src/solvers/aidecode_repeat.py +5 -0
- guldo/_src/solvers/aiinfer_batched.py +40 -0
- guldo/_src/solvers/aiinfer_f32.py +41 -0
- guldo/_src/solvers/aiinfer_gpu_batched.py +42 -0
- guldo/_src/solvers/aiinfer_gpu_ref.py +45 -0
- guldo/_src/solvers/aiinfer_lossy.py +42 -0
- guldo/_src/solvers/baseline_bkz.py +14 -0
- guldo/_src/solvers/cheat_lookup.py +24 -0
- guldo/_src/solvers/cheat_table.json +1 -0
- guldo/_src/solvers/cheat_table_v1_stale.json +1 -0
- guldo/_src/solvers/daemon_cpu_hider.py +17 -0
- guldo/_src/solvers/dlog_bsgs.py +19 -0
- guldo/_src/solvers/dlog_stride4.py +18 -0
- guldo/_src/solvers/egg_base.py +11 -0
- guldo/_src/solvers/egg_eightaxis.py +68 -0
- guldo/_src/solvers/egg_template.py +11 -0
- guldo/_src/solvers/factor_brent.py +34 -0
- guldo/_src/solvers/factor_floyd.py +19 -0
- guldo/_src/solvers/factor_trial_fast_py.py +21 -0
- guldo/_src/solvers/factor_trial_np.py +21 -0
- guldo/_src/solvers/factor_trial_py.py +12 -0
- guldo/_src/solvers/fixed_overhead_ref.py +21 -0
- guldo/_src/solvers/fork_bomb.py +18 -0
- guldo/_src/solvers/gpu_noop.py +6 -0
- guldo/_src/solvers/hostile_probe.py +36 -0
- guldo/_src/solvers/kill_meter.py +27 -0
- guldo/_src/solvers/memory_bomb.py +10 -0
- guldo/_src/solvers/noop.py +5 -0
- guldo/_src/solvers/orphan_cpu_hider.py +16 -0
- guldo/_src/solvers/planted_peek.py +23 -0
- guldo/_src/solvers/sleeping_daemon_hang.py +12 -0
- guldo/_src/solvers/small_n_trick.py +22 -0
- guldo/_src/solvers/starter_template.py +21 -0
- guldo/_src/solvers/stdout_flood.py +9 -0
- guldo/_src/solvers/tmp_fill.py +10 -0
- guldo/_src/solvers/tuned_bkz.py +25 -0
- guldo/_src/static_check.py +214 -0
- guldo/_src/submit_check.py +166 -0
- guldo/_src/svp.py +889 -0
- guldo/_src/tensorops.py +304 -0
- guldo/_src/tflops.py +453 -0
- guldo/_src/tflops_runner.py +134 -0
- guldo/cli.py +60 -0
- guldo_cli-0.1.0.dist-info/METADATA +30 -0
- guldo_cli-0.1.0.dist-info/RECORD +134 -0
- guldo_cli-0.1.0.dist-info/WHEEL +5 -0
- guldo_cli-0.1.0.dist-info/entry_points.txt +2 -0
- guldo_cli-0.1.0.dist-info/top_level.txt +1 -0
guldo/_src/ops.py
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
"""Operator tool for a public competition. Run it as the OPERATOR user, never as the web tier's user (it reads seeds).
|
|
2
|
+
|
|
3
|
+
ops.py round-new --data D --seed-dir S --id r1 --title "..." --family gm --closes 2026-12-31T23:59:00Z [--seeds 3]
|
|
4
|
+
Create k secret seeds under S/<id> (mode 0600, outside the project tree), publish their commitments as entry 0 of
|
|
5
|
+
the round's log, and open the round. The commitments are public from this moment.
|
|
6
|
+
With --count --count-dims 38 40 42 [--count-n 8 --count-cap 20] it is a COUNTED round (deterministic step counting on a ladder committed in
|
|
7
|
+
entry 0; every submission must publish its source; the worker then needs --unsandboxed). See COMPILER.md, "Counted rounds".
|
|
8
|
+
ops.py close --data D --id r1 [--force] close at the deadline (or earlier with --force; logged)
|
|
9
|
+
ops.py worker --data D --id r1 --seed-dir S/r1 evaluate every queued submission IN THE DOCKER SANDBOX; refuses unless the
|
|
10
|
+
sandbox is verified (portal.py verify-sandbox)
|
|
11
|
+
ops.py reveal --data D --id r1 --seed-dir S/r1 publish the seeds, only after evaluation, only if they match the commitments
|
|
12
|
+
ops.py preflight --data D [--seed-dir S] is THIS host set up as DEPLOY.md says (seed permissions, no seed in the data dir, sandbox marker, logs)
|
|
13
|
+
ops.py certs --data D --id r1 --seed-dir S/r1 after the reveal: build a verified certificate per evaluated submission (stored beside the round;
|
|
14
|
+
public copy for everyone (no solver source unless the owner published it), full copy to the owner)
|
|
15
|
+
ops.py export --data D --out site/ledger write rounds.json, round_<id>.json, log_<id>.jsonl for the static site
|
|
16
|
+
ops.py verify-log --data D --id r1 check a round's hash chain (exit 1 if it fails)
|
|
17
|
+
ops.py status --data D
|
|
18
|
+
ops.py kill-switch --data D on|off pause / resume submissions at the web tier
|
|
19
|
+
"""
|
|
20
|
+
import argparse
|
|
21
|
+
import calendar
|
|
22
|
+
import json
|
|
23
|
+
import os
|
|
24
|
+
import sys
|
|
25
|
+
import time
|
|
26
|
+
|
|
27
|
+
import families
|
|
28
|
+
import official
|
|
29
|
+
import portal_core
|
|
30
|
+
import rounds
|
|
31
|
+
|
|
32
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
33
|
+
DEFAULT_MAX_SUBMISSIONS = 50
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def parse_when(s):
|
|
37
|
+
try:
|
|
38
|
+
return calendar.timegm(time.strptime(s, "%Y-%m-%dT%H:%M:%SZ"))
|
|
39
|
+
except ValueError:
|
|
40
|
+
raise SystemExit(f"bad time {s!r}: use UTC like 2026-12-31T23:59:00Z")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def preflight(base, seed_root=None):
|
|
44
|
+
"""Is THIS machine set up the way DEPLOY.md says? -> [(status, check, detail)], status in pass / warn / fail. Read-only: it creates nothing."""
|
|
45
|
+
import shutil
|
|
46
|
+
import stat
|
|
47
|
+
out = []
|
|
48
|
+
add = lambda st, name, detail: out.append((st, name, detail))
|
|
49
|
+
base = os.path.abspath(base)
|
|
50
|
+
if os.path.isdir(os.path.join(base, "rounds")):
|
|
51
|
+
add("pass", "data directory", base)
|
|
52
|
+
mode = stat.S_IMODE(os.stat(base).st_mode)
|
|
53
|
+
add("pass" if mode & 0o020 else "warn", "data directory shared with the web tier",
|
|
54
|
+
f"mode {oct(mode)}: group-writable" if mode & 0o020 else f"mode {oct(mode)} is not group-writable: the web tier (another user, same group) could not record submissions")
|
|
55
|
+
stray = [os.path.join(dp, f) for dp, _, fs in os.walk(base) for f in fs if f.endswith(".seed")]
|
|
56
|
+
add("fail" if stray else "pass", "no seed under the data directory", f"{len(stray)} seed file(s) found, e.g. {stray[0]}: the web tier can read this directory" if stray else "none")
|
|
57
|
+
else:
|
|
58
|
+
add("fail", "data directory", f"{base} has no rounds/ directory (create it as in DEPLOY.md step 2)")
|
|
59
|
+
if seed_root:
|
|
60
|
+
sr = os.path.abspath(os.path.expanduser(seed_root))
|
|
61
|
+
if not os.path.isdir(sr):
|
|
62
|
+
add("fail", "seed directory", f"{sr} does not exist")
|
|
63
|
+
else:
|
|
64
|
+
mode = stat.S_IMODE(os.stat(sr).st_mode)
|
|
65
|
+
add("pass" if not mode & 0o077 else "fail", "seed directory is private", f"mode {oct(mode)}" + ("" if not mode & 0o077 else
|
|
66
|
+
": group or others can enter it, so the web tier could read the seeds. chmod 0700 and own it by the operator only"))
|
|
67
|
+
inside = sr == base or sr.startswith(base + os.sep) or sr == HERE or sr.startswith(HERE + os.sep)
|
|
68
|
+
add("fail" if inside else "pass", "seed directory is outside the data and code trees", "it is inside one of them" if inside else "outside both")
|
|
69
|
+
loose = [os.path.join(dp, f) for dp, _, fs in os.walk(sr) for f in fs if f.endswith(".seed") and stat.S_IMODE(os.stat(os.path.join(dp, f)).st_mode) & 0o077]
|
|
70
|
+
add("fail" if loose else "pass", "seed files are owner-only", f"{len(loose)} readable by others, e.g. {loose[0]}" if loose else "all 0600 or tighter")
|
|
71
|
+
code_mode = stat.S_IMODE(os.stat(HERE).st_mode)
|
|
72
|
+
add("fail" if code_mode & 0o002 else "pass", "code directory not world-writable", f"mode {oct(code_mode)}")
|
|
73
|
+
try:
|
|
74
|
+
rounds.require_python()
|
|
75
|
+
add("pass", "python", sys.version.split()[0])
|
|
76
|
+
except SystemExit as e:
|
|
77
|
+
add("fail", "python", str(e))
|
|
78
|
+
ok = portal_core.sandbox_verified(base)
|
|
79
|
+
add("pass" if ok else "warn", "sandbox verified", "the marker is present and matches the sandbox files" if ok else
|
|
80
|
+
"NO: submissions can be taken and logged, but no round can be evaluated (`python3 portal.py verify-sandbox --data D` on the evaluating host)")
|
|
81
|
+
add("pass" if shutil.which("docker") else "warn", "docker on this host", shutil.which("docker") or "not found: evaluate on a host that has it (DEPLOY.md, separate VM)")
|
|
82
|
+
add("warn" if os.path.exists(os.path.join(base, "KILL_SWITCH")) else "pass", "kill switch", "ON: submissions are paused" if os.path.exists(os.path.join(base, "KILL_SWITCH")) else "off")
|
|
83
|
+
for r in rounds.list_rounds(base) if os.path.isdir(os.path.join(base, "rounds")) else []:
|
|
84
|
+
good, n, head, err = rounds.verify_log(rounds.rdir(base, r["id"]), r["commitments"])
|
|
85
|
+
add("pass" if good else "fail", f"round {r['id']}: log", f"{n} entries, head {head[:16]}" if good else err)
|
|
86
|
+
return out
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def main(argv=None):
|
|
90
|
+
rounds.require_python()
|
|
91
|
+
ap = argparse.ArgumentParser()
|
|
92
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
93
|
+
p = sub.add_parser("round-new")
|
|
94
|
+
for a in ("--data", "--seed-dir", "--id", "--title", "--family", "--closes"):
|
|
95
|
+
p.add_argument(a, required=True)
|
|
96
|
+
p.add_argument("--seeds", type=int, default=3)
|
|
97
|
+
p.add_argument("--n", type=int, default=None,
|
|
98
|
+
help="instances per size (default: the family's official value, 24 for gm). A larger n is logged as the round's rule and lets a "
|
|
99
|
+
"round certify larger speedups (see PROTOCOL.md, Power at the official configuration) at proportionally more machine time.")
|
|
100
|
+
p.add_argument("--require-source", action="store_true",
|
|
101
|
+
help="every submission must publish its source (after the reveal). Logged in the round's first entry as a public rule. Without it a submitter "
|
|
102
|
+
"may keep their solver private: the public certificate then carries only its hash, and its timings rest on your sandbox alone")
|
|
103
|
+
p.add_argument("--max-submissions", type=int, default=DEFAULT_MAX_SUBMISSIONS,
|
|
104
|
+
help=f"submissions this round will accept (default {DEFAULT_MAX_SUBMISSIONS}; at most {rounds.MAX_PER_ROUND}). It is logged in the round's "
|
|
105
|
+
"first entry. A submission that fails everything is charged the cap on every instance, so each can cost hours: size this to your machine.")
|
|
106
|
+
p.add_argument("--count", action="store_true",
|
|
107
|
+
help="a COUNTED round: submissions and the baseline are measured by deterministic step counting (counted.py) on the ladder given by --count-dims/--count-n/"
|
|
108
|
+
"--count-cap, committed in the round's first log entry. Anyone re-executes the counts to check them, so the result does not rest on your machine or "
|
|
109
|
+
"sandbox. Needs a pure-Python family (factor), Python 3.12+ here, and forces --require-source (the proof is re-execution). Its worker runs solvers "
|
|
110
|
+
"UNSANDBOXED (`worker --unsandboxed`): use a throwaway user or VM. Not available for the lattice families")
|
|
111
|
+
p.add_argument("--count-dims", type=int, nargs="+", help="with --count: the sizes to count (e.g. 38 40 42 for factor)")
|
|
112
|
+
p.add_argument("--count-n", type=int, default=8, help="with --count: instances per size (default 8)")
|
|
113
|
+
p.add_argument("--count-cap", type=float, default=20.0, help="with --count: the most counted seconds (x 1e6 instructions) one instance may use (default 20)")
|
|
114
|
+
p.add_argument("--opens")
|
|
115
|
+
p.add_argument("--allow-few-seeds", action="store_true", help="demo only: a round with fewer than 3 seeds can never be official")
|
|
116
|
+
p = sub.add_parser("close")
|
|
117
|
+
p.add_argument("--data", required=True)
|
|
118
|
+
p.add_argument("--id", required=True)
|
|
119
|
+
p.add_argument("--force", action="store_true")
|
|
120
|
+
for name in ("worker", "reveal"):
|
|
121
|
+
p = sub.add_parser(name)
|
|
122
|
+
p.add_argument("--data", required=True)
|
|
123
|
+
p.add_argument("--id", required=True)
|
|
124
|
+
p.add_argument("--seed-dir", required=True)
|
|
125
|
+
if name == "worker":
|
|
126
|
+
p.add_argument("--accept-code-change", action="store_true",
|
|
127
|
+
help="evaluate even though the ladder/baseline/scoring code differ from what the round committed to; logged publicly")
|
|
128
|
+
p.add_argument("--unsandboxed", action="store_true",
|
|
129
|
+
help="COUNTED rounds only: confirm that solvers run unsandboxed under step counting (as a throwaway user or VM with nothing of value on it)")
|
|
130
|
+
p.add_argument("--results-dir", help="where a counted evaluation writes its ladders (default: this project's results/)")
|
|
131
|
+
p = sub.add_parser("preflight")
|
|
132
|
+
p.add_argument("--data", required=True)
|
|
133
|
+
p.add_argument("--seed-dir", help="the operator's seed root (e.g. /srv/ledger/seeds): its permissions are checked")
|
|
134
|
+
p = sub.add_parser("certs")
|
|
135
|
+
p.add_argument("--data", required=True)
|
|
136
|
+
p.add_argument("--id", required=True)
|
|
137
|
+
p.add_argument("--seed-dir", required=True)
|
|
138
|
+
p.add_argument("--results-dir", help="where the evaluation wrote its ladders (default: this project's results/)")
|
|
139
|
+
p = sub.add_parser("export")
|
|
140
|
+
p.add_argument("--data", required=True)
|
|
141
|
+
p.add_argument("--out", required=True)
|
|
142
|
+
p = sub.add_parser("verify-log")
|
|
143
|
+
p.add_argument("--data", required=True)
|
|
144
|
+
p.add_argument("--id", required=True)
|
|
145
|
+
p = sub.add_parser("status")
|
|
146
|
+
p.add_argument("--data", required=True)
|
|
147
|
+
p = sub.add_parser("kill-switch")
|
|
148
|
+
p.add_argument("--data", required=True)
|
|
149
|
+
p.add_argument("state", choices=["on", "off"])
|
|
150
|
+
a = ap.parse_args(argv)
|
|
151
|
+
base = os.path.abspath(a.data)
|
|
152
|
+
# The web tier runs as a DIFFERENT user in a shared group (DEPLOY.md). Under the default umask 022 everything created here is
|
|
153
|
+
# 0755/0644, which that user cannot write: no submission would ever succeed. Group-writable, nothing for others.
|
|
154
|
+
old_umask = os.umask(0o007)
|
|
155
|
+
try:
|
|
156
|
+
return _run(a, base)
|
|
157
|
+
finally:
|
|
158
|
+
os.umask(old_umask) # main() is also called from tests; do not leave process-wide state changed
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _run(a, base):
|
|
162
|
+
if a.cmd != "preflight": # a read-only check must never create the directory it is checking (a mistyped --data would then "pass")
|
|
163
|
+
os.makedirs(os.path.join(base, "rounds"), exist_ok=True)
|
|
164
|
+
try:
|
|
165
|
+
if a.cmd == "round-new":
|
|
166
|
+
if not a.allow_few_seeds and a.seeds < 3:
|
|
167
|
+
raise SystemExit("REFUSED: an official round needs at least 3 seeds")
|
|
168
|
+
# validate EVERYTHING before any seed exists: a typo must not leave seeds on disk that make the corrected retry fail
|
|
169
|
+
if not rounds.ROUND_ID_RE.match(a.id):
|
|
170
|
+
raise SystemExit("REFUSED: round id must be 2-41 characters of a-z, 0-9 and '-'")
|
|
171
|
+
if a.family not in families.FAMILIES:
|
|
172
|
+
raise SystemExit(f"REFUSED: unknown family {a.family!r} (one of {sorted(families.FAMILIES)})")
|
|
173
|
+
count = None
|
|
174
|
+
if a.count:
|
|
175
|
+
if a.n is not None:
|
|
176
|
+
raise SystemExit("REFUSED: --n is for timed rounds; a counted round's instances per size is --count-n")
|
|
177
|
+
if not a.count_dims:
|
|
178
|
+
raise SystemExit("REFUSED: --count needs --count-dims (the sizes to count, small enough that counting finishes: e.g. --family factor --count-dims 38 40 42)")
|
|
179
|
+
count = {"dims": a.count_dims, "n": a.count_n, "cap": a.count_cap}
|
|
180
|
+
rounds.count_config(a.family, **count) # every value checked before any seed exists (raises Rejected: nothing is created)
|
|
181
|
+
elif a.count_dims:
|
|
182
|
+
raise SystemExit("REFUSED: --count-dims only goes with --count")
|
|
183
|
+
if a.n is not None and not (families.OFFICIAL[a.family]["n"] <= a.n <= rounds.MAX_N):
|
|
184
|
+
raise SystemExit(f"REFUSED: n must be an integer from {families.OFFICIAL[a.family]['n']} to {rounds.MAX_N} (instances per size)")
|
|
185
|
+
closes, opens = parse_when(a.closes), (parse_when(a.opens) if a.opens else time.time())
|
|
186
|
+
if closes <= time.time():
|
|
187
|
+
raise SystemExit("REFUSED: the deadline must be in the future")
|
|
188
|
+
if closes <= opens:
|
|
189
|
+
raise SystemExit("REFUSED: the deadline must be after the opening time")
|
|
190
|
+
seed_dir = os.path.join(os.path.abspath(os.path.expanduser(a.seed_dir)), a.id)
|
|
191
|
+
if os.path.exists(os.path.join(rounds.rdir(base, a.id), "round.json")):
|
|
192
|
+
raise SystemExit(f"REFUSED: round {a.id!r} already exists")
|
|
193
|
+
commits = official.make_commitments(seed_dir, a.seeds)
|
|
194
|
+
r = rounds.create_round(base, a.id, a.title, a.family, commits, closes,
|
|
195
|
+
opens_at=parse_when(a.opens) if a.opens else None, allow_few_seeds=a.allow_few_seeds,
|
|
196
|
+
max_submissions=a.max_submissions, n=a.n, allow_private=not a.require_source, count=count)
|
|
197
|
+
print(f"round {r['id']} is open until {a.closes}. Seeds are in {seed_dir} (mode 0600): keep that directory off the web tier's user.")
|
|
198
|
+
if count is not None:
|
|
199
|
+
import counted
|
|
200
|
+
c = r["count"]
|
|
201
|
+
worst_h = len(c["dims"]) * c["n"] * len(commits) * c["cap"] * counted.STEPS_PER_SECOND / counted.SLOW_STEPS_PER_SECOND / 3600.0
|
|
202
|
+
print(f"COUNTED round: sizes {c['dims']}, {c['n']} instances per size, cap {c['cap']:g} counted seconds ({int(c['cap'] * counted.STEPS_PER_SECOND):,} instructions), "
|
|
203
|
+
f"on {c['python']}; all of it committed in the first log entry. Every submission must publish its source (the proof is re-execution).\n"
|
|
204
|
+
f"Counting is slow: worst case (every instance spending its whole cap at a pessimistic throughput) about {worst_h:.1f} hours per submission, plus the same "
|
|
205
|
+
f"for the baselines once; a solver that works costs far less. The worker needs `--unsandboxed` and must run as a throwaway user or VM; the same Python "
|
|
206
|
+
f"({c['python']}) must be used to evaluate and by anyone re-running.\ncommitments (already public in the log):\n" + "\n".join(r["commitments"]))
|
|
207
|
+
return 0
|
|
208
|
+
plan = official.plan_cost(a.family, len(commits), n=r["n"], budget=r["budget"])
|
|
209
|
+
per_sub = plan["instances_per_ladder"] * len(commits) * r["budget"] / 3600.0 # a submission failing every instance, charged the cap each time
|
|
210
|
+
base_h = per_sub # the baseline ladders, measured once per seed and reused
|
|
211
|
+
print(f"{r['n']} instances per size; "
|
|
212
|
+
f"worst case (every submission fails every instance, each charged the {r['budget']:g} s cap): {per_sub:.1f} CPU-hours per submission, "
|
|
213
|
+
f"so {r['max_submissions']} submissions = {per_sub * r['max_submissions']:.1f} h, plus {base_h:.1f} h for the baselines. "
|
|
214
|
+
"A solver that works costs far less. Lower --max-submissions if that exceeds your machine time.")
|
|
215
|
+
print("Solvers: " + ("every submission must publish its source (a public rule, logged in the first entry)." if a.require_source else
|
|
216
|
+
"a submitter may keep their solver private (the public certificate carries only its hash; its timings then rest on your sandbox alone). "
|
|
217
|
+
"Use --require-source if you do not want that."))
|
|
218
|
+
print("commitments (already public in the log):\n" + "\n".join(r["commitments"]))
|
|
219
|
+
elif a.cmd == "close":
|
|
220
|
+
r = rounds.close_round(base, a.id, force=a.force)
|
|
221
|
+
print(f"round {a.id}: {r['state']}")
|
|
222
|
+
elif a.cmd == "worker":
|
|
223
|
+
n = rounds.evaluate_round(base, a.id, a.seed_dir, lambda: portal_core.sandbox_verified(base), allow_code_change=a.accept_code_change,
|
|
224
|
+
results_dir=a.results_dir, allow_unsandboxed=a.unsandboxed)
|
|
225
|
+
print(f"evaluated {n} submission(s); round is {rounds.get_round(base, a.id)['state']}")
|
|
226
|
+
elif a.cmd == "reveal":
|
|
227
|
+
seeds = rounds.reveal(base, a.id, a.seed_dir)
|
|
228
|
+
print(f"revealed {len(seeds)} seed(s) for round {a.id}")
|
|
229
|
+
elif a.cmd == "preflight":
|
|
230
|
+
res = preflight(base, a.seed_dir)
|
|
231
|
+
for st, name, detail in res:
|
|
232
|
+
print(f" {st.upper():4s} {name}: {detail}")
|
|
233
|
+
bad = [r for r in res if r[0] == "fail"]
|
|
234
|
+
print("\nREADY (warnings are things only you can judge)" if not bad else f"\n{len(bad)} problem(s) to fix before this host serves a round")
|
|
235
|
+
return 1 if bad else 0
|
|
236
|
+
elif a.cmd == "certs":
|
|
237
|
+
res = rounds.build_certificates(base, a.id, a.seed_dir, a.results_dir)
|
|
238
|
+
print(f"built {len(res['built'])} certificate(s) for round {a.id}")
|
|
239
|
+
for sid, why in sorted(res["skipped"].items()):
|
|
240
|
+
print(f" skipped {sid}: {why}")
|
|
241
|
+
print("The public certificate (GET /api/cert/<round>/<id>.json) is open to everyone: it carries the solver's source only for submissions whose owner "
|
|
242
|
+
"chose to publish it; for the others it is a PRIVATE-SOLVER copy (hash, no source, no failure text). The full certificate with the source "
|
|
243
|
+
"(GET /api/cert-full/<round>/<id>.json) is served only to the holder of the handle's token, in an `Authorization: Bearer` header.")
|
|
244
|
+
elif a.cmd == "export":
|
|
245
|
+
os.umask(0o022) # these files are SERVED by another user (the static-site web server): they must be world-readable
|
|
246
|
+
idx = rounds.export_public(base, a.out)
|
|
247
|
+
print(f"exported {len(idx)} round(s) to {a.out}")
|
|
248
|
+
elif a.cmd == "verify-log":
|
|
249
|
+
r = rounds.get_round(base, a.id)
|
|
250
|
+
ok, n, head, err = rounds.verify_log(rounds.rdir(base, a.id), r["commitments"])
|
|
251
|
+
print(f"{'OK' if ok else 'FAILED'}: {n} entries" + (f", head {head}" if ok else f": {err}"))
|
|
252
|
+
problems = rounds.check_consistency(base, a.id, hash_files=True) if ok else []
|
|
253
|
+
for p in problems: # the chain can be intact while the stored files are not: check them against the hashes it commits to
|
|
254
|
+
print("INCONSISTENT: " + p)
|
|
255
|
+
if ok and not problems:
|
|
256
|
+
print("Publish this head hash somewhere the operator cannot edit it (a signed post, a git tag) to anchor the log.")
|
|
257
|
+
return 0 if ok and not problems else 1
|
|
258
|
+
elif a.cmd == "status":
|
|
259
|
+
print(f"sandbox verified: {portal_core.sandbox_verified(base)} kill switch: {os.path.exists(os.path.join(base, 'KILL_SWITCH'))}")
|
|
260
|
+
for r in rounds.list_rounds(base):
|
|
261
|
+
subs = rounds.list_submissions(base, r["id"])
|
|
262
|
+
print(f"{r['id']:20s} {r['family']:7s} {r['state']:11s} closes {time.strftime('%Y-%m-%d %H:%M', time.gmtime(r['closes_at']))} UTC "
|
|
263
|
+
f"{len(subs)} submission(s), {sum(1 for m in subs if m['state'] == 'queued')} queued")
|
|
264
|
+
elif a.cmd == "kill-switch":
|
|
265
|
+
path = os.path.join(base, "KILL_SWITCH")
|
|
266
|
+
if a.state == "on":
|
|
267
|
+
open(path, "w", encoding="utf-8").close()
|
|
268
|
+
elif os.path.exists(path):
|
|
269
|
+
os.remove(path)
|
|
270
|
+
print(f"kill switch {a.state}")
|
|
271
|
+
except rounds.Rejected as e:
|
|
272
|
+
print(str(e), file=sys.stderr)
|
|
273
|
+
return 1
|
|
274
|
+
except KeyError as e:
|
|
275
|
+
print(f"no such round: {e}", file=sys.stderr)
|
|
276
|
+
return 1
|
|
277
|
+
return 0
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
if __name__ == "__main__":
|
|
281
|
+
sys.exit(main())
|
guldo/_src/portal.py
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"""Local submission portal: upload a solver, see its lint result and queue status, see the leaderboard.
|
|
2
|
+
|
|
3
|
+
python3 portal.py serve [--port 8765] [--data portal_data] (binds 127.0.0.1 only)
|
|
4
|
+
python3 portal.py verify-sandbox run the sandbox tests; writes the marker only if they PASS
|
|
5
|
+
python3 portal.py worker --seed-dir ~/judge-seeds [--once] evaluate queued submissions IN THE SANDBOX
|
|
6
|
+
|
|
7
|
+
SAFETY RULES (each has a test):
|
|
8
|
+
* The portal never executes an upload. It only parses the source with `ast` (submit_check.lint).
|
|
9
|
+
* The worker refuses to evaluate anything until `verify-sandbox` has written <data>/SANDBOX_VERIFIED, and that marker is
|
|
10
|
+
only written when the sandbox tests ran and passed with nothing skipped. Evaluation is always `--sandbox docker`;
|
|
11
|
+
there is deliberately no way to evaluate an upload in a bare local process.
|
|
12
|
+
* Uploads are size-limited, stored under a random id (never the client's filename), and every dynamic string in a page
|
|
13
|
+
is HTML-escaped. There are no user accounts: this is a local tool, not a public service.
|
|
14
|
+
"""
|
|
15
|
+
import argparse
|
|
16
|
+
import html
|
|
17
|
+
import json
|
|
18
|
+
import os
|
|
19
|
+
import re
|
|
20
|
+
import subprocess
|
|
21
|
+
import sys
|
|
22
|
+
import time
|
|
23
|
+
import uuid
|
|
24
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
25
|
+
|
|
26
|
+
import families
|
|
27
|
+
import judge
|
|
28
|
+
import report_html
|
|
29
|
+
import submit_check
|
|
30
|
+
|
|
31
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
32
|
+
MAX_UPLOAD = 200_000 # bytes of Python source
|
|
33
|
+
MAX_PENDING = 50
|
|
34
|
+
MAX_STORED = 500 # every stored submission, including rejected ones (each can be up to MAX_UPLOAD bytes)
|
|
35
|
+
NAME_RE = re.compile(r"[^A-Za-z0-9_.-]")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def data_dir(base=None):
|
|
39
|
+
d = os.path.abspath(base or os.path.join(HERE, "portal_data"))
|
|
40
|
+
os.makedirs(os.path.join(d, "submissions"), exist_ok=True)
|
|
41
|
+
return d
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def clean_name(s):
|
|
45
|
+
return (NAME_RE.sub("_", s.strip())[:40] or "unnamed")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# the sandbox gate and the multipart parser live in portal_core.py (standard library only) and are re-exported here unchanged
|
|
49
|
+
from portal_core import (MARKER, SANDBOX_FILES, parse_multipart, sandbox_fingerprint, sandbox_verified, # noqa: E402,F401
|
|
50
|
+
write_marker)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def list_submissions(base):
|
|
54
|
+
out = []
|
|
55
|
+
root = os.path.join(base, "submissions")
|
|
56
|
+
for sid in sorted(os.listdir(root)):
|
|
57
|
+
p = os.path.join(root, sid, "meta.json")
|
|
58
|
+
if os.path.exists(p):
|
|
59
|
+
with open(p, encoding="utf-8") as f:
|
|
60
|
+
out.append(json.load(f))
|
|
61
|
+
return sorted(out, key=lambda m: m["submitted_at"], reverse=True)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def save_meta(base, meta):
|
|
65
|
+
d = os.path.join(base, "submissions", meta["id"])
|
|
66
|
+
with open(os.path.join(d, "meta.json"), "w", encoding="utf-8") as f:
|
|
67
|
+
json.dump(meta, f, indent=1)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def accept_submission(base, name, family, source_bytes):
|
|
71
|
+
"""Validate and store an upload WITHOUT running it. Returns the meta dict (state 'queued' or 'rejected')."""
|
|
72
|
+
meta = {"id": uuid.uuid4().hex[:12], "name": clean_name(name), "family": family, "submitted_at": time.time(),
|
|
73
|
+
"warnings": [], "state": "queued", "reason": ""}
|
|
74
|
+
# unique file name: judge.py names a board entry after the file, so a shared name would merge every contestant's results
|
|
75
|
+
meta["file"] = f"{meta['name']}-{meta['id']}.py"
|
|
76
|
+
existing = list_submissions(base)
|
|
77
|
+
if family not in families.FAMILIES:
|
|
78
|
+
meta.update(state="rejected", reason=f"unknown family {family!r}")
|
|
79
|
+
elif len(source_bytes) == 0:
|
|
80
|
+
meta.update(state="rejected", reason="empty file")
|
|
81
|
+
elif len(source_bytes) > MAX_UPLOAD:
|
|
82
|
+
meta.update(state="rejected", reason=f"file is {len(source_bytes)} bytes; the limit is {MAX_UPLOAD}")
|
|
83
|
+
elif len(existing) >= MAX_STORED:
|
|
84
|
+
meta.update(state="rejected", reason="the portal has reached its storage limit; ask the owner to clear old submissions")
|
|
85
|
+
elif sum(1 for m in existing if m["state"] == "queued") >= MAX_PENDING:
|
|
86
|
+
meta.update(state="rejected", reason="the queue is full; try again later")
|
|
87
|
+
else:
|
|
88
|
+
try:
|
|
89
|
+
text = source_bytes.decode("utf-8")
|
|
90
|
+
except UnicodeDecodeError:
|
|
91
|
+
meta.update(state="rejected", reason="the file is not UTF-8 text")
|
|
92
|
+
text = None
|
|
93
|
+
if text is not None:
|
|
94
|
+
d = os.path.join(base, "submissions", meta["id"])
|
|
95
|
+
os.makedirs(d, exist_ok=True)
|
|
96
|
+
path = os.path.join(d, meta["file"])
|
|
97
|
+
with open(path, "wb") as f:
|
|
98
|
+
f.write(source_bytes) # the exact uploaded bytes, whatever the locale's text encoding is
|
|
99
|
+
try:
|
|
100
|
+
warnings, fatal = submit_check.lint(path) # ast only: never imports or runs the file
|
|
101
|
+
except SyntaxError as e:
|
|
102
|
+
warnings, fatal = [], [f"syntax error: {e.msg} (line {e.lineno})"]
|
|
103
|
+
meta["warnings"] = warnings
|
|
104
|
+
if fatal:
|
|
105
|
+
meta.update(state="rejected", reason="; ".join(fatal))
|
|
106
|
+
if meta["state"] == "rejected" and "storage limit" in meta["reason"]:
|
|
107
|
+
return meta # at the limit a rejection must leave NO trace, or the limit itself would still permit unbounded growth
|
|
108
|
+
os.makedirs(os.path.join(base, "submissions", meta["id"]), exist_ok=True)
|
|
109
|
+
save_meta(base, meta)
|
|
110
|
+
return meta
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# ------------------------------------------------------------------ pages
|
|
114
|
+
def e(x):
|
|
115
|
+
return html.escape(str(x), quote=True)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
STATE_NOTE = {"queued": "Waiting for the sandbox", "rejected": "Rejected", "evaluating": "Being evaluated",
|
|
119
|
+
"done": "Evaluated", "failed": "Evaluation failed"}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _row(m):
|
|
123
|
+
"""One table row. Built with plain concatenation: a backslash inside an f-string expression is a SyntaxError before Python 3.12,
|
|
124
|
+
and the public service and the operator tool both import this module, so it must parse on every supported Python."""
|
|
125
|
+
note = ("<div class='small muted'>" + e(m["reason"]) + "</div>") if m.get("reason") else ""
|
|
126
|
+
warns = "".join("<div class='small muted'>warning: " + e(w) + "</div>" for w in m.get("warnings", []))
|
|
127
|
+
when = e(time.strftime("%Y-%m-%d %H:%M", time.localtime(m["submitted_at"])))
|
|
128
|
+
return (f'<tr><td class="strong">{e(m["name"])}</td><td>{e(m["family"])}</td><td>{e(STATE_NOTE.get(m["state"], m["state"]))}'
|
|
129
|
+
f'{note}{warns}</td><td class="mono small">{when}</td></tr>')
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def page(base, flash=""):
|
|
133
|
+
subs = list_submissions(base)
|
|
134
|
+
verified = sandbox_verified(base)
|
|
135
|
+
rows = "".join(_row(m) for m in subs)
|
|
136
|
+
fam_opts = "".join(f'<option value="{e(f)}">{e(f)}</option>' for f in sorted(families.FAMILIES))
|
|
137
|
+
gate = ('<p class="ok-note">The sandbox is verified: queued submissions can be evaluated by the worker.</p>' if verified else
|
|
138
|
+
'<p class="warn-note"><strong>Evaluation is switched off.</strong> The sandbox has not passed its tests on this machine '
|
|
139
|
+
'(<code>portal.py verify-sandbox</code>), so uploads are checked and queued but nothing is run.</p>')
|
|
140
|
+
board = []
|
|
141
|
+
if os.path.exists(judge.BOARD):
|
|
142
|
+
with open(judge.BOARD, encoding="utf-8") as f:
|
|
143
|
+
board = json.load(f)
|
|
144
|
+
lb = report_html.leaderboard(board)
|
|
145
|
+
body = f"""<title>Solver Portal</title>
|
|
146
|
+
<style>{report_html.CSS}
|
|
147
|
+
form.up{{display:grid;gap:12px;max-width:520px}} label{{font-weight:600;font-size:13.5px;display:grid;gap:5px}}
|
|
148
|
+
input[type=text],select,input[type=file]{{font:inherit;padding:8px 10px;border:1px solid var(--line);border-radius:7px;background:var(--card);color:var(--ink);min-width:0}}
|
|
149
|
+
button{{font:600 14px var(--body);padding:10px 16px;border-radius:8px;border:0;background:var(--accent);color:#fff;cursor:pointer;justify-self:start}}
|
|
150
|
+
button:focus-visible,input:focus-visible,select:focus-visible{{outline:2px solid var(--ink);outline-offset:2px}}
|
|
151
|
+
.warn-note{{background:var(--warn-bg);color:var(--ink);padding:10px 12px;border-radius:8px}} .ok-note{{background:var(--ok-bg);color:var(--ink);padding:10px 12px;border-radius:8px}}
|
|
152
|
+
.flash{{background:var(--idle-bg);padding:10px 12px;border-radius:8px}}</style>
|
|
153
|
+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:wght@600&family=Instrument+Sans:wght@400;600&family=JetBrains+Mono:wght@400;600&display=swap">
|
|
154
|
+
<main class="wrap">
|
|
155
|
+
<header><h1>Solver Portal</h1><p class="lede">Send in a lattice solver and see how it is checked. Your file is read, never run, until the sandbox is verified.</p></header>
|
|
156
|
+
{f'<div class="flash" role="status">{e(flash)}</div>' if flash else ''}
|
|
157
|
+
<section class="card"><h2>Submit a solver</h2>{gate}
|
|
158
|
+
<form class="up" method="post" action="/submit" enctype="multipart/form-data">
|
|
159
|
+
<label for="name">Name<input id="name" name="name" type="text" maxlength="40" required placeholder="my_solver"></label>
|
|
160
|
+
<label for="family">Instance family<select id="family" name="family">{fam_opts}</select></label>
|
|
161
|
+
<label for="file">Python file<input id="file" name="file" type="file" accept=".py" required></label>
|
|
162
|
+
<button type="submit">Check and queue</button></form>
|
|
163
|
+
<p class="small muted">One file exposing <code>solve(basis, bound2)</code>; only fpylll and numpy exist in the sandbox. Rules: COMPETITORS.md.</p></section>
|
|
164
|
+
<section class="card"><h2>Submissions</h2>{'<div class="scroll"><table class="lb"><thead><tr><th>Name</th><th>Family</th><th>Status</th><th>Sent</th></tr></thead><tbody>' + rows + '</tbody></table></div>' if rows else '<p class="muted">Nothing submitted yet.</p>'}</section>
|
|
165
|
+
<section class="card"><h2>Leaderboard</h2>{lb}<p class="small muted">Full charts and limits: results/report.html (python3 report_html.py).</p></section>
|
|
166
|
+
</main>"""
|
|
167
|
+
return body.encode("utf-8")
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class Handler(BaseHTTPRequestHandler):
|
|
171
|
+
base = None
|
|
172
|
+
timeout = 10 # seconds a client may stall on any read; without it one idle connection pins a thread forever
|
|
173
|
+
|
|
174
|
+
def log_message(self, *a):
|
|
175
|
+
pass
|
|
176
|
+
|
|
177
|
+
def _guard(self):
|
|
178
|
+
"""Reject requests a browser on another site (or a rebinding attacker) could make against a loopback service.
|
|
179
|
+
Host must be this server's own loopback name:port; a POST's Origin, when sent, must be exactly this server."""
|
|
180
|
+
port = self.server.server_address[1]
|
|
181
|
+
host = (self.headers.get("Host") or "").lower()
|
|
182
|
+
if host not in (f"127.0.0.1:{port}", f"localhost:{port}"):
|
|
183
|
+
self._send(403, b"forbidden: unexpected Host header", "text/plain")
|
|
184
|
+
return False
|
|
185
|
+
origin = self.headers.get("Origin")
|
|
186
|
+
if self.command == "POST" and origin is not None and origin.lower() not in (f"http://127.0.0.1:{port}", f"http://localhost:{port}"):
|
|
187
|
+
self._send(403, b"forbidden: cross-site request", "text/plain")
|
|
188
|
+
return False
|
|
189
|
+
if self.command == "POST" and self.headers.get("Sec-Fetch-Site") == "cross-site":
|
|
190
|
+
self._send(403, b"forbidden: cross-site request", "text/plain")
|
|
191
|
+
return False
|
|
192
|
+
return True
|
|
193
|
+
|
|
194
|
+
def _send(self, code, payload, ctype="text/html; charset=utf-8"):
|
|
195
|
+
self.send_response(code)
|
|
196
|
+
self.send_header("Content-Type", ctype)
|
|
197
|
+
self.send_header("Content-Length", str(len(payload)))
|
|
198
|
+
self.send_header("X-Content-Type-Options", "nosniff")
|
|
199
|
+
self.send_header("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline' https://fonts.googleapis.com; "
|
|
200
|
+
"font-src https://fonts.gstatic.com; form-action 'self'; base-uri 'none'")
|
|
201
|
+
self.end_headers()
|
|
202
|
+
self.wfile.write(payload)
|
|
203
|
+
|
|
204
|
+
def do_GET(self):
|
|
205
|
+
if not self._guard():
|
|
206
|
+
return
|
|
207
|
+
if self.path.split("?")[0] == "/":
|
|
208
|
+
self._send(200, page(self.base))
|
|
209
|
+
else:
|
|
210
|
+
self._send(404, b"not found", "text/plain")
|
|
211
|
+
|
|
212
|
+
def do_POST(self):
|
|
213
|
+
if not self._guard():
|
|
214
|
+
return
|
|
215
|
+
if self.path != "/submit":
|
|
216
|
+
return self._send(404, b"not found", "text/plain")
|
|
217
|
+
raw_len = self.headers.get("Content-Length") or ""
|
|
218
|
+
if not re.fullmatch(r"[0-9]{1,9}", raw_len): # rejects negative, empty and non-numeric lengths
|
|
219
|
+
return self._send(400, page(self.base, "The request had no valid Content-Length."), "text/html; charset=utf-8")
|
|
220
|
+
n = int(raw_len)
|
|
221
|
+
if n > MAX_UPLOAD + 20_000:
|
|
222
|
+
return self._send(413, page(self.base, "That upload is too large."))
|
|
223
|
+
try:
|
|
224
|
+
body = self.rfile.read(n)
|
|
225
|
+
except OSError: # includes socket.timeout: the client stalled, so drop it rather than hold a thread
|
|
226
|
+
self.close_connection = True
|
|
227
|
+
return
|
|
228
|
+
if len(body) < n:
|
|
229
|
+
return self._send(400, page(self.base, "The upload ended early."))
|
|
230
|
+
form = parse_multipart(self.headers.get("Content-Type", ""), body)
|
|
231
|
+
if "file" not in form or "name" not in form:
|
|
232
|
+
return self._send(400, page(self.base, "Missing name or file."))
|
|
233
|
+
meta = accept_submission(self.base, form["name"][1].decode("utf-8", "replace"),
|
|
234
|
+
form.get("family", (None, b"gm"))[1].decode("utf-8", "replace"), form["file"][1])
|
|
235
|
+
msg = (f"{meta['name']}: rejected. {meta['reason']}" if meta["state"] == "rejected" else
|
|
236
|
+
f"{meta['name']}: passed the static check and is queued." + (f" {len(meta['warnings'])} warning(s) listed below." if meta["warnings"] else ""))
|
|
237
|
+
self._send(200 if meta["state"] != "rejected" else 422, page(self.base, msg))
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def make_server(base, port=0):
|
|
241
|
+
handler = type("H", (Handler,), {"base": data_dir(base)})
|
|
242
|
+
srv = ThreadingHTTPServer(("127.0.0.1", port), handler) # loopback only
|
|
243
|
+
srv.daemon_threads = True
|
|
244
|
+
return srv
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
# ------------------------------------------------------------------ worker + sandbox gate
|
|
248
|
+
def verify_sandbox(base):
|
|
249
|
+
"""Run the sandbox tests; write the marker ONLY if they ran, passed, and none were skipped."""
|
|
250
|
+
r = subprocess.run([sys.executable, "-m", "unittest", "test_sandbox", "-v"], cwd=HERE, capture_output=True, text=True)
|
|
251
|
+
out = r.stdout + r.stderr
|
|
252
|
+
ok = r.returncode == 0 and "skipped" not in out and "Ran 0 tests" not in out and re.search(r"Ran [1-9]\d* tests?", out)
|
|
253
|
+
path = os.path.join(base, MARKER)
|
|
254
|
+
if ok:
|
|
255
|
+
write_marker(base)
|
|
256
|
+
elif os.path.exists(path):
|
|
257
|
+
os.remove(path)
|
|
258
|
+
return bool(ok), out
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def run_worker(base, seed_dir, once=True, runner=subprocess.run):
|
|
262
|
+
"""Evaluate queued submissions in the sandbox. Refuses outright if the sandbox is not verified."""
|
|
263
|
+
if not sandbox_verified(base):
|
|
264
|
+
return ("REFUSED: the sandbox is not verified, or its files changed since it was (run `portal.py verify-sandbox`); "
|
|
265
|
+
"nothing was evaluated.")
|
|
266
|
+
done = 0
|
|
267
|
+
for m in list_submissions(base):
|
|
268
|
+
if m["state"] != "queued":
|
|
269
|
+
continue
|
|
270
|
+
m["state"] = "evaluating"
|
|
271
|
+
save_meta(base, m)
|
|
272
|
+
path = os.path.join(base, "submissions", m["id"], m.get("file", "solver.py"))
|
|
273
|
+
r = runner([sys.executable, os.path.join(HERE, "official.py"), "evaluate", "--family", m["family"], "--submission", path,
|
|
274
|
+
"--seed-dir", seed_dir, "--sandbox", "docker"], cwd=HERE, capture_output=True, text=True)
|
|
275
|
+
m["state"] = "done" if r.returncode == 0 else "failed"
|
|
276
|
+
m["reason"] = "" if r.returncode == 0 else (r.stdout + r.stderr)[-300:]
|
|
277
|
+
save_meta(base, m)
|
|
278
|
+
done += 1
|
|
279
|
+
if once and done >= 1:
|
|
280
|
+
break
|
|
281
|
+
return f"evaluated {done} submission(s)"
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def main():
|
|
285
|
+
ap = argparse.ArgumentParser()
|
|
286
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
287
|
+
p = sub.add_parser("serve")
|
|
288
|
+
p.add_argument("--port", type=int, default=8765)
|
|
289
|
+
p.add_argument("--data")
|
|
290
|
+
p = sub.add_parser("verify-sandbox")
|
|
291
|
+
p.add_argument("--data")
|
|
292
|
+
p = sub.add_parser("worker")
|
|
293
|
+
p.add_argument("--data")
|
|
294
|
+
p.add_argument("--seed-dir", required=True)
|
|
295
|
+
p.add_argument("--all", action="store_true", help="evaluate every queued submission, not just the oldest-first one")
|
|
296
|
+
a = ap.parse_args()
|
|
297
|
+
base = data_dir(a.data)
|
|
298
|
+
if a.cmd == "serve":
|
|
299
|
+
srv = make_server(base, a.port)
|
|
300
|
+
print(f"portal on http://127.0.0.1:{srv.server_address[1]} (data: {base}; evaluation "
|
|
301
|
+
f"{'ENABLED' if sandbox_verified(base) else 'DISABLED until the sandbox is verified'})")
|
|
302
|
+
srv.serve_forever()
|
|
303
|
+
elif a.cmd == "verify-sandbox":
|
|
304
|
+
ok, out = verify_sandbox(base)
|
|
305
|
+
print(out[-1200:])
|
|
306
|
+
print("SANDBOX VERIFIED: marker written" if ok else "NOT VERIFIED: the sandbox tests did not all run and pass")
|
|
307
|
+
sys.exit(0 if ok else 1)
|
|
308
|
+
else:
|
|
309
|
+
print(run_worker(base, a.seed_dir, once=not a.all))
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
if __name__ == "__main__":
|
|
313
|
+
main()
|