Datasets:

Modalities:
Text
Languages:
English
Size:
< 1K
ArXiv:
Libraries:
Datasets
Harbor
License:
lewtun HF Staff commited on
Commit
3170d7b
·
verified ·
1 Parent(s): 477390f

grader: refuse links, hardlinks and non-regular answers (#120 review); check.py: cheat cases and Trials (part 5)

Browse files
tasks/agg-p31-n01/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p31-n02/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p31-n04/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p31-n08/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p31-n16/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p32-n01/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p32-n02/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p32-n04/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p32-n08/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY
tasks/agg-p32-n16/tests/test.sh CHANGED
@@ -2,41 +2,78 @@
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
- # booleans, a missing, extra or dropped duplicate number, and a wrong order. The script writes 0
6
- # before it checks anything, so a crash of the check also leaves reward 0, and keeps a copy of
7
- # the answer with the trial's results. ANSWER, EXPECTED and VERIFIER_DIR override the paths, to
8
- # test the grader locally.
 
 
 
9
  answer=${ANSWER:-/app/answer.json}
10
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
11
  verifier=${VERIFIER_DIR:-/logs/verifier}
12
  mkdir -p "$verifier"
 
13
  echo 0 > "$verifier/reward.txt"
14
- cp "$answer" "$verifier/answer.json" 2>/dev/null || true
15
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
16
  import json
17
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def reject_constant(name):
21
  raise ValueError(f"{name} is not JSON")
22
 
23
 
24
- def load(path):
25
- with open(path, "rb") as stream:
26
- return json.loads(stream.read().decode("utf-8"), parse_constant=reject_constant)
27
 
28
 
 
 
29
  try:
30
- answer, expected = load(os.environ["ANSWER"]), load(os.environ["EXPECTED"])
 
 
31
  valid = (
32
  isinstance(answer, list)
33
  and all(type(x) is int for x in answer) # not bool, float or str
34
  and answer == expected
35
  )
36
- except Exception: # missing file, invalid UTF-8 or JSON, NaN, RecursionError
37
  valid = False
38
  if valid:
39
- with open(os.path.join(os.environ["VERIFIER_DIR"], "reward.txt"), "w") as stream:
40
- stream.write("1")
41
  print("reward:", 1 if valid else 0)
42
  PY
 
2
  # Reward 1 if /app/answer.json is exactly expected.json: a JSON array of the team's integers in
3
  # ascending order, one per agent (equal numbers kept). Anything else scores 0: a missing or
4
  # unreadable file, invalid UTF-8 or JSON, NaN or Infinity, a non-array, strings, floats or
5
+ # booleans, a missing, extra or dropped duplicate number, and a wrong order.
6
+ # The answer counts only as a regular file of its own at exactly that path: a symlink (e.g. to
7
+ # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
8
+ # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
9
+ # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
10
+ # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
11
+ # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
12
  answer=${ANSWER:-/app/answer.json}
13
  expected=${EXPECTED:-$(dirname "$0")/expected.json}
14
  verifier=${VERIFIER_DIR:-/logs/verifier}
15
  mkdir -p "$verifier"
16
+ rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
17
  echo 0 > "$verifier/reward.txt"
 
18
  ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
19
  import json
20
  import os
21
+ import stat
22
+
23
+ LIMIT = 1 << 20 # bytes
24
+
25
+
26
+ def read_own(path):
27
+ # The bytes of the regular file at exactly `path`: every folder on the way is opened
28
+ # without following a link, and so is the file (a FIFO cannot block it); one link only.
29
+ parts = os.path.abspath(path).split("/")[1:]
30
+ folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY)
31
+ try:
32
+ for part in parts[:-1]:
33
+ inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder)
34
+ os.close(folder)
35
+ folder = inner
36
+ fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder)
37
+ finally:
38
+ os.close(folder)
39
+ with os.fdopen(fd, "rb") as stream:
40
+ info = os.fstat(stream.fileno())
41
+ if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
42
+ raise ValueError("not a regular file of its own")
43
+ data = stream.read(LIMIT + 1)
44
+ if len(data) > LIMIT:
45
+ raise ValueError("too large")
46
+ return data
47
+
48
+
49
+ def write_new(path, text):
50
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
51
+ with os.fdopen(fd, "w") as stream:
52
+ stream.write(text)
53
 
54
 
55
  def reject_constant(name):
56
  raise ValueError(f"{name} is not JSON")
57
 
58
 
59
+ def parse(data):
60
+ return json.loads(data.decode("utf-8"), parse_constant=reject_constant)
 
61
 
62
 
63
+ verifier = os.environ["VERIFIER_DIR"]
64
+ valid = False
65
  try:
66
+ data = read_own(os.environ["ANSWER"])
67
+ write_new(os.path.join(verifier, "answer.json"), data.decode("utf-8", "replace"))
68
+ answer, expected = parse(data), parse(read_own(os.environ["EXPECTED"]))
69
  valid = (
70
  isinstance(answer, list)
71
  and all(type(x) is int for x in answer) # not bool, float or str
72
  and answer == expected
73
  )
74
+ except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, NaN, RecursionError
75
  valid = False
76
  if valid:
77
+ write_new(os.path.join(verifier, "reward.txt"), "1")
 
78
  print("reward:", 1 if valid else 0)
79
  PY