amos 913d3c367f Add the usable task catalog and admit batch 6 tasks
All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 17:43:15 +08:00

624 lines
45 KiB
Python

import copy
import json
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
CONTEXT = {'pc_closure.hpp': '#ifndef PC_CLOSURE_HPP\n#define PC_CLOSURE_HPP\n\n#include <array>\n#include <cstddef>\n#include <cstdint>\n#include <string>\n#include <vector>\n\nnamespace pc_closure {\n\nenum class Status : int { ok = 0, invalid = -22 };\nenum class Mode : std::uint32_t { shared = 0, static_link = 1 };\nenum class Relation : std::uint32_t {\n none = 0, equal = 1, not_equal = 2, less = 3,\n less_equal = 4, greater = 5, greater_equal = 6\n};\nenum class Field : std::size_t {\n version = 0, requires = 1, requires_private = 2,\n libs = 3, libs_private = 4, cflags = 5\n};\n\nstruct Source {\n std::string name;\n std::string text;\n};\n\nstruct Variable {\n std::string name;\n std::string value;\n};\n\nstruct Package {\n std::string name;\n std::vector<Variable> variables;\n std::array<std::string, 6> fields{};\n};\n\nstruct Repository {\n std::vector<Package> packages;\n};\n\nstruct ExpandedPackage {\n std::string name;\n std::array<std::string, 6> fields{};\n};\n\nstruct Expansion {\n std::vector<ExpandedPackage> packages;\n std::uint64_t work = 0;\n std::uint64_t bytes = 0;\n};\n\nstruct Requirement {\n std::string name;\n Relation relation = Relation::none;\n std::vector<std::uint32_t> version;\n};\n\nstruct Request {\n Mode mode = Mode::shared;\n std::vector<Requirement> roots;\n std::string sysroot;\n};\n\nstruct Closure {\n std::vector<std::string> packages;\n};\n\nstruct Plan {\n std::vector<std::string> packages;\n std::vector<std::string> cflags;\n std::vector<std::string> libs;\n};\n\n// Outputs own their values. Each error replaces the complete output with T{}.\n// Inputs and output objects must be distinct and must not overlap.\nStatus parse_sources(const std::vector<Source> &sources, Repository &output);\nStatus expand_repository(const Repository &repository, Expansion &output);\nStatus resolve_closure(const Repository &repository, const Request &request,\n Closure &output);\nStatus plan_flags(const Repository &repository, const Request &request,\n Plan &output);\n\n} // namespace pc_closure\n#endif\n', 'pc_observe.cpp': '#include "pc_closure.hpp"\n#include <charconv>\n#include <iostream>\n#include <sstream>\n#include <string>\n#include <utility>\n#include <vector>\n\nnamespace {\nusing namespace pc_closure;\n\nclass Words {\npublic:\n explicit Words(const std::string &line)\n {\n std::istringstream input(line);\n std::string value;\n while (input >> value) values_.push_back(value);\n }\n bool empty() const { return values_.empty(); }\n const std::string &op() const { return values_.front(); }\n bool done() const { return next_ == values_.size(); }\n template<class T> bool number(T &value)\n {\n if (done()) return false;\n const std::string &word = values_[next_++];\n auto result = std::from_chars(word.data(), word.data() + word.size(), value);\n return result.ec == std::errc{} && result.ptr == word.data() + word.size();\n }\n bool bytes(std::string &value)\n {\n if (done()) return false;\n const std::string &word = values_[next_++];\n value.clear();\n if (word == "-") return true;\n if (word.size() % 2 != 0) return false;\n for (std::size_t i = 0; i < word.size(); i += 2) {\n const int high = digit(word[i]);\n const int low = digit(word[i + 1]);\n if (high < 0 || low < 0) return false;\n value.push_back(static_cast<char>(high * 16 + low));\n }\n return true;\n }\nprivate:\n static int digit(char ch)\n {\n if (ch >= \'0\' && ch <= \'9\') return ch - \'0\';\n if (ch >= \'a\' && ch <= \'f\') return ch - \'a\' + 10;\n if (ch >= \'A\' && ch <= \'F\') return ch - \'A\' + 10;\n return -1;\n }\n std::vector<std::string> values_;\n std::size_t next_ = 1;\n};\n\nvoid hex(std::ostream &out, const std::string &value)\n{\n static const char digits[] = "0123456789abcdef";\n out << \'"\';\n for (unsigned char byte : value) out << digits[byte / 16] << digits[byte % 16];\n out << \'"\';\n}\n\ntemplate<class T, class Print>\nvoid list(std::ostream &out, const std::vector<T> &values, Print print)\n{\n out << \'[\';\n for (std::size_t i = 0; i < values.size(); ++i) {\n if (i != 0) out << \',\';\n print(out, values[i]);\n }\n out << \']\';\n}\n\nvoid strings(std::ostream &out, const std::vector<std::string> &values)\n{\n list(out, values, hex);\n}\n\nvoid fields(std::ostream &out, const std::array<std::string, 6> &values)\n{\n out << \'[\';\n for (std::size_t i = 0; i < values.size(); ++i) {\n if (i != 0) out << \',\';\n hex(out, values[i]);\n }\n out << \']\';\n}\n\nvoid package(std::ostream &out, const Package &value)\n{\n out << \'[\';\n hex(out, value.name);\n out << \',\';\n list(out, value.variables, [](std::ostream &s, const Variable &v) {\n s << \'[\'; hex(s, v.name); s << \',\'; hex(s, v.value); s << \']\';\n });\n out << \',\';\n fields(out, value.fields);\n out << \']\';\n}\n\nvoid repository(std::ostream &out, const Repository &value)\n{\n list(out, value.packages, package);\n}\n\nvoid expansion(std::ostream &out, const Expansion &value)\n{\n out << \'[\' << value.work << \',\' << value.bytes << \',\';\n list(out, value.packages, [](std::ostream &s, const ExpandedPackage &p) {\n s << \'[\'; hex(s, p.name); s << \',\'; fields(s, p.fields); s << \']\';\n });\n out << \']\';\n}\n\nvoid closure(std::ostream &out, const Closure &value)\n{\n strings(out, value.packages);\n}\n\nvoid plan(std::ostream &out, const Plan &value)\n{\n out << \'[\';\n strings(out, value.packages); out << \',\';\n strings(out, value.cflags); out << \',\';\n strings(out, value.libs); out << \']\';\n}\n\nvoid sources(std::ostream &out, const std::vector<Source> &values)\n{\n list(out, values, [](std::ostream &s, const Source &source) {\n s << \'[\'; hex(s, source.name); s << \',\'; hex(s, source.text); s << \']\';\n });\n}\n\nvoid request(std::ostream &out, const Request &value)\n{\n out << \'[\' << static_cast<std::uint32_t>(value.mode) << \',\';\n list(out, value.roots, [](std::ostream &s, const Requirement &r) {\n s << \'[\'; hex(s, r.name);\n s << \',\' << static_cast<std::uint32_t>(r.relation) << \',\';\n list(s, r.version, [](std::ostream &d, std::uint32_t n) { d << n; });\n s << \']\';\n });\n out << \',\'; hex(out, value.sysroot); out << \']\';\n}\n\ntemplate<class T, class Print> std::string capture(const T &value, Print print)\n{\n std::ostringstream out;\n print(out, value);\n return out.str();\n}\n\nbool row(const std::string &op, Words &words)\n{\n std::string line;\n if (!std::getline(std::cin, line)) return false;\n words = Words(line);\n return !words.empty() && words.op() == op;\n}\n\nbool read_sources(Words &words, std::vector<Source> &output)\n{\n unsigned count = 0;\n if (!words.number(count) || !words.done() || count > 65) return false;\n std::vector<Source> draft;\n for (unsigned i = 0; i < count; ++i) {\n Words item("");\n Source source;\n if (!row("s", item) || !item.bytes(source.name) || !item.bytes(source.text) ||\n !item.done()) return false;\n draft.push_back(std::move(source));\n }\n output = std::move(draft);\n return true;\n}\n\nbool read_repository(Words &words, Repository &output)\n{\n unsigned count = 0;\n if (!words.number(count) || !words.done() || count > 65) return false;\n Repository draft;\n for (unsigned i = 0; i < count; ++i) {\n Words item("");\n Package value;\n unsigned variables = 0;\n if (!row("p", item) || !item.bytes(value.name) || !item.number(variables) ||\n !item.done() || variables > 257) return false;\n for (unsigned j = 0; j < variables; ++j) {\n Words binding("");\n Variable variable;\n if (!row("v", binding) || !binding.bytes(variable.name) ||\n !binding.bytes(variable.value) || !binding.done()) return false;\n value.variables.push_back(std::move(variable));\n }\n Words field_row("");\n if (!row("f", field_row)) return false;\n for (std::string &field : value.fields) if (!field_row.bytes(field)) return false;\n if (!field_row.done()) return false;\n draft.packages.push_back(std::move(value));\n }\n output = std::move(draft);\n return true;\n}\n\nbool read_request(Words &words, Request &output)\n{\n std::uint32_t mode = 0;\n unsigned roots = 0;\n Request draft;\n if (!words.number(mode) || !words.number(roots) || !words.bytes(draft.sysroot) ||\n !words.done() || roots > 33) return false;\n draft.mode = static_cast<Mode>(mode);\n for (unsigned i = 0; i < roots; ++i) {\n Words item("");\n Requirement root;\n std::uint32_t relation = 0;\n unsigned count = 0;\n if (!row("r", item) || !item.bytes(root.name) || !item.number(relation) ||\n !item.number(count) || count > 17) return false;\n root.relation = static_cast<Relation>(relation);\n for (unsigned j = 0; j < count; ++j) {\n std::uint32_t segment = 0;\n if (!item.number(segment)) return false;\n root.version.push_back(segment);\n }\n if (!item.done()) return false;\n draft.roots.push_back(std::move(root));\n }\n output = std::move(draft);\n return true;\n}\n\nstruct Results {\n Repository parsed;\n Expansion expanded;\n Closure closed;\n Plan flags;\n};\n\nvoid results(std::ostream &out, const Results &value)\n{\n out << \'[\'; repository(out, value.parsed);\n out << \',\'; expansion(out, value.expanded);\n out << \',\'; closure(out, value.closed);\n out << \',\'; plan(out, value.flags); out << \']\';\n}\n\nstruct Bank {\n std::vector<Source> sources;\n Repository repository;\n Request request;\n Results last;\n};\n\nint protocol_error()\n{\n std::cerr << "invalid observation command\\n";\n return 2;\n}\n} // namespace\n\nint main()\n{\n using namespace pc_closure;\n Bank banks[2];\n Results saved[4];\n unsigned bank = 0;\n std::string line;\n std::cout << std::boolalpha;\n while (std::getline(std::cin, line)) {\n Words words(line);\n if (words.empty() || words.op()[0] == \'#\') continue;\n const std::string op = words.op();\n Bank &current = banks[bank];\n if (op == "S") {\n if (!read_sources(words, current.sources)) return protocol_error();\n } else if (op == "R") {\n if (!read_repository(words, current.repository)) return protocol_error();\n } else if (op == "Q") {\n if (!read_request(words, current.request)) return protocol_error();\n } else if (op == "U") {\n unsigned selected = 0;\n if (!words.number(selected) || !words.done() || selected > 1) return protocol_error();\n bank = selected;\n } else if (op == "P") {\n if (!words.done()) return protocol_error();\n const std::string before = capture(current.sources, sources);\n current.last.parsed = Repository{{Package{"dirty", {{"dirty", "dirty"}}, {}}}};\n const Status status = parse_sources(current.sources, current.last.parsed);\n std::cout << "[\\"P\\"," << static_cast<int>(status) << \',\';\n repository(std::cout, current.last.parsed);\n std::cout << \',\' << (before == capture(current.sources, sources)) << "]\\n";\n current.repository = current.last.parsed;\n } else if (op == "E" || op == "C" || op == "F") {\n if (!words.done()) return protocol_error();\n const std::string before_repo = capture(current.repository, repository);\n const std::string before_request = capture(current.request, request);\n Status status = Status::invalid;\n if (op == "E") {\n current.last.expanded = Expansion{{ExpandedPackage{"dirty", {}}}, 99, 98};\n status = expand_repository(current.repository, current.last.expanded);\n } else if (op == "C") {\n current.last.closed = Closure{{"dirty"}};\n status = resolve_closure(current.repository, current.request, current.last.closed);\n } else {\n current.last.flags = Plan{{"dirty"}, {"dirty"}, {"dirty"}};\n status = plan_flags(current.repository, current.request, current.last.flags);\n }\n std::cout << "[\\"" << op << "\\"," << static_cast<int>(status) << \',\';\n if (op == "E") expansion(std::cout, current.last.expanded);\n else if (op == "C") closure(std::cout, current.last.closed);\n else plan(std::cout, current.last.flags);\n const bool unchanged = before_repo == capture(current.repository, repository) &&\n before_request == capture(current.request, request);\n std::cout << \',\' << unchanged << "]\\n";\n } else if (op == "K" || op == "O") {\n unsigned slot = 0;\n if (!words.number(slot) || !words.done() || slot > 3) return protocol_error();\n if (op == "K") saved[slot] = current.last;\n std::cout << "[\\"" << op << "\\"," << slot << \',\';\n results(std::cout, saved[slot]);\n std::cout << "]\\n";\n } else return protocol_error();\n }\n if (std::cin.bad()) return protocol_error();\n return 0;\n}\n'}
CANDIDATES = ('pc_text.cpp', 'pc_expand.cpp', 'pc_closure.cpp', 'pc_flags.cpp')
FIELDS = ('Version', 'Requires', 'Requires.private', 'Libs', 'Libs.private', 'Cflags')
PACKAGE = re.compile(r'[A-Za-z_][A-Za-z0-9_.-]{0,63}\Z', re.ASCII)
VARIABLE = re.compile(r'[A-Za-z_][A-Za-z0-9_]{0,63}\Z', re.ASCII)
REFERENCE = re.compile(r'\$\{([A-Za-z_][A-Za-z0-9_]{0,63})\}', re.ASCII)
class Invalid(ValueError):
pass
def require(condition):
if not condition:
raise Invalid()
def package(name='a', variables=(), fields=None):
return dict(name=name, variables=list(variables), fields=list(fields or ['1', '', '', '', '', '']))
def request(mode=0, roots=(), sysroot=''):
return dict(mode=mode, roots=list(roots), sysroot=sysroot)
def root(name='a', relation=0, version=()):
return [name, relation, list(version)]
def expr(text):
chunks = []
literal = []
i = 0
while i < len(text):
if text[i] != '$':
literal.append(text[i]); i += 1
elif text.startswith('$$', i):
literal.append('$'); i += 2
else:
found = REFERENCE.match(text, i)
require(found is not None)
if literal:
chunks.append((False, ''.join(literal))); literal = []
chunks.append((True, found.group(1)))
i += len(found.group(0))
if literal:
chunks.append((False, ''.join(literal)))
return chunks
def parsed(sources):
require(len(sources) <= 64 and sum(len(text) for _, text in sources) <= 262144)
require(len({name for name, _ in sources}) == len(sources))
result = []
variable_count = 0
for name, text in sources:
require(PACKAGE.fullmatch(name) is not None)
for i, ch in enumerate(text):
require(32 <= ord(ch) <= 126 or ch in '\t\n' or
ch == '\r' and i + 1 < len(text) and text[i + 1] == '\n')
variables, values = {}, {}
for row in text.split('\n'):
row = row.removesuffix('\r').strip(' \t')
if not row or row.startswith('#'):
continue
indices = [row.find(c) for c in '=:' if c in row]
require(bool(indices))
pos = min(indices)
key, value = row[:pos].strip(' \t'), row[pos + 1:].strip(' \t')
expr(value)
if row[pos] == '=':
require(VARIABLE.fullmatch(key) is not None and key not in variables)
variable_count += 1
require(variable_count <= 256)
variables[key] = value
else:
require(key in FIELDS and key not in values)
values[key] = value
require('Version' in values)
result.append(package(name, sorted(variables.items()), [values.get(key, '') for key in FIELDS]))
return sorted(result, key=lambda value: value['name'])
def expanded(repository):
require(len(repository) <= 64 and len({p['name'] for p in repository}) == len(repository))
require(sum(len(p['variables']) for p in repository) <= 256)
require(sum(len(value) for p in repository for value in
[v for _, v in p['variables']] + p['fields']) <= 262144)
prepared = []
total_work, total_bytes = 0, 0
for p in sorted(repository, key=lambda value: value['name']):
require(PACKAGE.fullmatch(p['name']) is not None)
require(len({n for n, _ in p['variables']}) == len(p['variables']))
require(all(VARIABLE.fullmatch(n) is not None for n, _ in p['variables']))
nodes = {}
for name, value in p['variables']:
require(all(ch == '\t' or 32 <= ord(ch) <= 126 for ch in value))
nodes[name] = expr(value)
field_nodes = []
for value in p['fields']:
require(all(ch == '\t' or 32 <= ord(ch) <= 126 for ch in value))
field_nodes.append(expr(value))
require(all(not reference or value in nodes for pieces in list(nodes.values()) + field_nodes
for reference, value in pieces))
# Exact arbitrary-size integer costs, computed without expanding any string.
costs, pending = {}, dict(nodes)
while pending:
ready = [name for name, parts in pending.items()
if all(not ref or target in costs for ref, target in parts)]
require(bool(ready))
for name in ready:
parts = pending.pop(name)
costs[name] = (1 + sum(costs[t][0] for ref, t in parts if ref),
sum(costs[t][1] if ref else len(t) for ref, t in parts))
for parts in field_nodes:
total_work += 1 + sum(costs[t][0] for ref, t in parts if ref)
total_bytes += sum(costs[t][1] if ref else len(t) for ref, t in parts)
prepared.append((p['name'], nodes, field_nodes))
require(total_work <= 65536 and total_bytes <= 1048576)
result = []
for name, nodes, field_nodes in prepared:
values = {}
def render(parts):
answer = []
for ref, token in parts:
if ref:
if token not in values:
values[token] = render(nodes[token])
answer.append(values[token])
else:
answer.append(token)
return ''.join(answer)
result.append([name, [render(parts) for parts in field_nodes]])
return result, total_work, total_bytes
def version(text):
text = text.strip(' \t')
require(re.fullmatch(r'[0-9]+(?:\.[0-9]+){0,15}', text, flags=re.ASCII) is not None)
result = []
for part in text.split('.'):
significant = part.lstrip('0') or '0'
require(len(significant) <= 10)
value = int(significant)
require(value <= 0xffffffff)
result.append(value)
return result
RELATIONS = {'=': 1, '!=': 2, '<': 3, '<=': 4, '>': 5, '>=': 6}
def dependencies(text):
if not text.strip(' \t'):
return []
result = []
for item in text.split(','):
item = item.strip(' \t')
found = re.fullmatch(r'([A-Za-z_][A-Za-z0-9_.-]{0,63})[ \t]*(?:(<=|>=|!=|=|<|>)[ \t]*(.*))?', item, flags=re.ASCII)
require(found is not None)
name, relation, wanted = found.groups()
result.append(root(name, RELATIONS[relation], version(wanted)) if relation else root(name))
return result
def tokens(text):
return re.findall(r'[^ \t]+', text, flags=re.ASCII)
def normalize(path):
stack = []
for part in path.split('/'):
if part in ('', '.'):
continue
if part == '..':
if stack:
stack.pop()
else:
stack.append(part)
return '/' + '/'.join(stack)
def predicate(wanted, actual):
_, relation, target = wanted
width = max(len(actual), len(target))
left = tuple(actual + [0] * (width - len(actual)))
right = tuple(target + [0] * (width - len(target)))
return [True, left == right, left != right, left < right, left <= right,
left > right, left >= right][relation]
def resolved(repository, wanted):
packages, _, _ = expanded(repository)
index = {p[0]: i for i, p in enumerate(packages)}
versions = [version(p[1][0]) for p in packages]
edges = [[dependencies(p[1][1]), dependencies(p[1][2])] for p in packages]
require(sum(len(es) for pair in edges for es in pair) <= 512)
token_count = 0
for _, values in packages:
for text in values[3:]:
words = tokens(text)
token_count += len(words)
i = 0
while i < len(words):
if words[i] in ('-I', '-L'):
i += 1
require(i < len(words))
i += 1
require(token_count <= 4096)
reach = [0] * len(packages)
for i, pair in enumerate(edges):
for es in pair:
for edge in es:
require(edge[0] in index)
reach[i] |= 1 << index[edge[0]]
for k in range(len(packages)):
for i in range(len(packages)):
if reach[i] >> k & 1:
reach[i] |= reach[k]
require(all(not (mask >> i & 1) for i, mask in enumerate(reach)))
require(wanted['mode'] in (0, 1) and len(wanted['roots']) <= 32)
sysroot = wanted['sysroot']
require(not sysroot or len(sysroot) <= 4096 and sysroot.startswith('/') and
all(33 <= ord(ch) <= 126 for ch in sysroot))
for name, relation, target in wanted['roots']:
require(PACKAGE.fullmatch(name) is not None and name in index and 0 <= relation <= 6)
require(not target if relation == 0 else 1 <= len(target) <= 16)
seen, order = set(), []
def visit(edge):
name = edge[0]
node = index[name]
require(predicate(edge, versions[node]))
if name in seen:
return
seen.add(name); order.append(name)
for child in edges[node][0] + (edges[node][1] if wanted['mode'] else []):
visit(child)
for edge in wanted['roots']:
visit(edge)
return order, packages
def projected(repository, wanted):
order, packages = resolved(repository, wanted)
table = dict(packages)
sysroot = normalize(wanted['sysroot']) if wanted['sysroot'] else ''
cflags, libs = [], []
for name in order:
for field in [5, 3] + ([4] if wanted['mode'] else []):
words = tokens(table[name][field])
destination = cflags if field == 5 else libs
i = 0
while i < len(words):
token = words[i]
if token.startswith(('-I', '-L')):
prefix = token[:2]
if len(token) == 2:
i += 1; directory = words[i]
else:
directory = token[2:]
if directory.startswith('/'):
directory = normalize(directory)
if sysroot and sysroot != '/' and directory != sysroot and not directory.startswith(sysroot + '/'):
directory = sysroot if directory == '/' else sysroot + directory
token = prefix + directory
destination.append(token)
i += 1
require(sum(map(len, cflags + libs)) <= 1048576)
return order, cflags, libs
def hx(text):
return text.encode('latin1').hex()
def wire(text):
return hx(text) or '-'
def repo_json(repository):
return [[hx(p['name']), [[hx(n), hx(v)] for n, v in p['variables']], list(map(hx, p['fields']))]
for p in repository]
class Observations:
def __init__(self):
self.commands, self.expected, self.labels = [], [], []
self.banks = [dict(sources=[], repository=[], request=request(),
last=[[], [0, 0, []], [], [[], [], []]]) for _ in range(2)]
self.bank = 0
self.saved = [[[], [0, 0, []], [], [[], [], []]] for _ in range(4)]
@property
def current(self):
return self.banks[self.bank]
def use(self, bank):
self.commands.append('U %d' % bank); self.bank = bank
def source(self, sources):
self.commands.append('S %d' % len(sources))
self.commands.extend('s %s %s' % (wire(n), wire(t)) for n, t in sources)
self.current['sources'] = copy.deepcopy(sources)
def load(self, repository):
self.commands.append('R %d' % len(repository))
for p in repository:
self.commands.append('p %s %d' % (wire(p['name']), len(p['variables'])))
self.commands.extend('v %s %s' % (wire(n), wire(v)) for n, v in p['variables'])
self.commands.append('f ' + ' '.join(map(wire, p['fields'])))
self.current['repository'] = copy.deepcopy(repository)
def query(self, wanted):
self.commands.append('Q %d %d %s' % (wanted['mode'], len(wanted['roots']), wire(wanted['sysroot'])))
for name, relation, target in wanted['roots']:
self.commands.append('r %s %d %d%s' % (wire(name), relation, len(target),
''.join(' %d' % value for value in target)))
self.current['request'] = copy.deepcopy(wanted)
def emit(self, op, value, label, clauses):
self.commands.append(op)
self.expected.append(value)
self.labels.append((label, tuple(sorted(set(['P01', 'P29', 'P30'] + list(clauses))))))
def call(self, op, label, clauses=()):
allowed = {'P': {2, 3, 4, 5, 6, 25, 26},
'E': {2, 6, 7, 8, 9, 25, 26, 27, 28},
'C': {2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 25, 26, 27, 28},
'F': set(range(6, 29)) | {2}}
base = {'P': [2, 3, 4, 5, 6], 'E': [7, 8, 9],
'C': [10, 17, 18, 19, 20], 'F': [10, 17, 18, 19, 20, 21, 22, 23, 24]}
clauses = ['P%02d' % n for n in base[op]] + [c for c in clauses if int(c[1:]) in allowed[op]]
current, status = self.current, 0
slot = 'PECF'.index(op)
output = copy.deepcopy([[], [0, 0, []], [], [[], [], []]][slot])
try:
if op == 'P':
value = parsed(current['sources']); output = repo_json(value)
elif op == 'E':
ps, w, b = expanded(current['repository'])
output = [w, b, [[hx(n), list(map(hx, fs))] for n, fs in ps]]
elif op == 'C':
order, _ = resolved(current['repository'], current['request'])
output = list(map(hx, order))
else:
order, cflags, libs = projected(current['repository'], current['request'])
output = [list(map(hx, values)) for values in (order, cflags, libs)]
except Invalid:
status = -22
current['last'][slot] = copy.deepcopy(output)
self.emit(op, [op, status, output, True], label, clauses)
if op == 'P':
current['repository'] = value if status == 0 else []
def hold(self, slot, label):
self.saved[slot] = copy.deepcopy(self.current['last'])
self.emit('K %d' % slot, ['K', slot, copy.deepcopy(self.saved[slot])], label, ['P01', 'P25'])
def old(self, slot, label):
self.emit('O %d' % slot, ['O', slot, copy.deepcopy(self.saved[slot])], label, ['P01', 'P25'])
def series(obs, repository, label, clauses=(), wanted=None, operations='ECF'):
obs.load(repository)
obs.query(wanted or request(roots=[root(repository[0]['name'])] if repository else []))
for op in operations:
obs.call(op, label + '/' + op, clauses)
def plan():
obs = Observations()
rng = random.Random(172943)
field_sources = [(name, 'prefix=/%s\nVersion: 1\nCflags: -I${prefix}/include\nLibs: -l%s\n' % (name, name))
for name in ('alpha', 'beta')]
obs.source(field_sources); obs.query(request(roots=[root('alpha'), root('beta')]))
obs.call('P', 'field/parse', ['P02', 'P03', 'P04', 'P05', 'P06'])
for op in 'ECF':
obs.call(op, 'field/' + op, ['P06', 'P07', 'P08', 'P18', 'P19', 'P21', 'P22', 'P23'])
obs.hold(0, 'field/hold'); obs.old(0, 'field/old')
for mode in (0, 1):
series(obs, [], 'empty/%d' % mode, ['P02', 'P08', 'P09', 'P10', 'P21'], request(mode))
obs.source([]); obs.call('P', 'source/empty', ['P02', 'P05'])
for suffix in ('\n', '\r\n', ''):
obs.source([('z', ' b = ${a} #=:\n a=$$\n Version : 01.0\n Cflags:${b}' + suffix),
('A', '\t# full comment\nVersion:0\nVersion=x')])
obs.call('P', 'source/newline-' + repr(suffix), ['P03', 'P04', 'P05', 'P06'])
obs.call('E', 'source/forward-dollar-' + repr(suffix), ['P06', 'P07', 'P08', 'P09'])
for code in range(256):
obs.source([('a', '#' + chr(code) + '\nVersion:1\n')])
obs.call('P', 'source/comment-byte-%03d' % code, ['P03', 'P04', 'P05'])
series(obs, [package(variables=[('v', 'x' + chr(code) + 'y')])],
'expression/byte-%03d' % code, ['P06', 'P07'], operations='E')
bad_rows = ['', 'x', 'Unknown:0', 'Version:1\nVersion:2', 'v=1\nv=2\nVersion:1',
'bad-name=x\nVersion:1', 'Version:${}', 'Version:$', 'Version:${x',
'Version:${a.b}', 'Version:${1a}', 'Version:$x', 'Version:1\r#x',
'Version:1\n#bad\x00', 'Version:1\n#bare\rX', 'Version:1\nBad:unused']
for i, text in enumerate(bad_rows):
obs.source([('a', text)]); obs.call('P', 'source/invalid-%02d' % i, ['P03', 'P04', 'P05', 'P06'])
for name in ('a', '_' * 64, '_' * 65, '1a', 'a.b-c', 'a+b', '', 'a\x00', 'a/b'):
obs.source([(name, 'Version:1')]); obs.call('P', 'names/' + hx(name), ['P02', 'P05'])
for text in ('', '$$', '$${missing}', '$$${x}', '${x}', 'a=b:c#d', '${x}${x}',
'\t${later} ', '{x}', '\\${x}', '$$$$', '$$$$$', '${MISSING}'):
series(obs, [package(variables=[('later', '${x}'), ('x', 'X'), ('v', text)],
fields=['1', '', '', '', '', '${v}'])],
'expression/syntax-' + hx(text), ['P06', 'P07', 'P08', 'P09', 'P22'])
graph_bad = [[('x', '${x}')], [('x', '${y}'), ('y', '${x}')], [('x', '${absent}')],
[('x', 'ok'), ('x', 'again')], [('a.b', 'ok')]]
for i, variables in enumerate(graph_bad):
series(obs, [package(variables=variables)], 'expression/unreferenced-invalid-%d' % i,
['P02', 'P06', 'P07', 'P10'])
for i in range(12):
a, b = 'p%d' % i, 'q%d' % i
values = [package(name, [('prefix', '/%s/%d' % (name, i))], ['1', '', '', '', '', '-I${prefix}'])
for name in (b, a)]
series(obs, values, 'isolate/A-%02d' % i, ['P06', 'P07', 'P09', 'P25', 'P26'],
request(roots=[root(b), root(a)]))
values = [package('a', fields=['1', '', 'b', '-la', '-lp', '']), package('b', fields=['1', '', '', '-lb', '', ''])]
series(obs, values, 'isolate/B-%02d' % i, ['P13', 'P18', 'P19', 'P21'], request(1, [root('a')]))
values = [package('a', fields=['1', 'c>=1', '', '', '', '']),
package('b', fields=['1', 'c<3', '', '', '', '']), package('c', fields=['3', '', '', '', '', ''])]
series(obs, values, 'isolate/C-%02d' % i, ['P11', 'P12', 'P19', 'P20'], request(0, [root('a'), root('b')]))
values = [package(variables=[('flag', '-I/p/%d -L/q/%d' % (i, i))], fields=['1', '', '', '', '', '${flag}'])]
series(obs, values, 'isolate/D-%02d' % i, ['P14', 'P15', 'P16', 'P22', 'P23'], request(0, [root()], '/sdk'))
empty_chain = [('v0', '')] + [('v%d' % i, '${v%d}${v%d}' % (i - 1, i - 1)) for i in range(1, 15)]
exact_work = ''.join('${v%d}' % i for i in range(15)) + '${v0}' * 11
for extra in ('', '${v0}'):
values = [package(variables=empty_chain, fields=['1', '', '', exact_work + extra, '', ''])]
series(obs, values, 'budget/W-' + ('exact' if not extra else 'plus-one'), ['P07', 'P08', 'P09', 'P27', 'P28'])
byte_chain = [('v0', 'a' * 63)] + [('v%d' % i, '${v%d}${v%d}' % (i - 1, i - 1)) for i in range(1, 14)]
for extra in (0, 1):
values = [package(variables=byte_chain, fields=['1', '', '', '${v13}${v13}' + 'a' * (16383 + extra), '', ''])]
series(obs, values, 'budget/B-%d' % extra, ['P07', 'P08', 'P09', 'P14', 'P27', 'P28'], operations='EC')
unused = [('v0', '')] + [('v%d' % i, '${v%d}${v%d}' % (i - 1, i - 1)) for i in range(1, 256)]
series(obs, [package(variables=unused)], 'budget/unused-exponential-256', ['P02', 'P07', 'P08', 'P28'])
series(obs, [package(variables=unused + [('extra', '')])], 'budget/variables-257', ['P02', 'P07', 'P26'])
deep = [('v0', 'x')] + [('v%d' % i, '${v%d}' % (i - 1)) for i in range(1, 256)]
series(obs, [package(variables=deep, fields=['1', '', '', '${v255}', '', ''])], 'budget/depth-256', ['P07', 'P08', 'P28'])
for excess in (0, 1):
series(obs, [package(variables=[('unused', 'a' * (262143 + excess))])],
'budget/raw-repository-%d' % excess, ['P07', 'P26'], operations='E')
text = 'Version:1\n#' + 'a' * (262144 - len('Version:1\n#') + excess)
obs.source([('a', text)]); obs.call('P', 'budget/raw-source-%d' % excess, ['P02', 'P03', 'P26'])
for count in (64, 65):
values = [package('p%02d' % i) for i in range(count)]
series(obs, values, 'budget/packages-%d' % count, ['P02', 'P07', 'P26'])
obs.source([('p%02d' % i, 'Version:1') for i in range(count)])
obs.call('P', 'budget/sources-%d' % count, ['P02', 'P05', 'P26'])
for count in (256, 257):
obs.source([('a', '\n'.join(['Version:1'] + ['v%d=' % i for i in range(count)]))])
obs.call('P', 'budget/source-variables-%d' % count, ['P02', 'P04', 'P05', 'P26'])
for length in (64, 65):
obs.source([('a', 'Version:1\n' + '_' * length + '=x')])
obs.call('P', 'names/variable-length-%d' % length, ['P02', 'P04', 'P05'])
for count in (512, 513):
values = [package('a', fields=['1', ','.join(['b'] * count), '', '', '', '']), package('b')]
series(obs, values, 'budget/edges-%d' % count, ['P12', 'P13', 'P19', 'P20', 'P26'])
for count in (4096, 4097):
series(obs, [package(fields=['1', '', '', ' '.join(['x'] * count), '', ''])],
'budget/tokens-%d' % count, ['P14', 'P21', 'P26'])
for count in (32, 33):
series(obs, [package()], 'budget/roots-%d' % count, ['P17', 'P19', 'P20', 'P26'], request(roots=[root()] * count))
bad_graphs = [[package(fields=['1', '', 'missing', '', '', ''])],
[package(fields=['1', 'a', '', '', '', ''])],
[package('a', fields=['1', 'b', '', '', '', '']), package('b', fields=['1', '', 'a', '', '', ''])],
[package('a'), package('b', fields=['1', 'b', '', '', '', ''])]]
for i, values in enumerate(bad_graphs):
series(obs, values, 'dependency/global-bad-%d' % i, ['P10', 'P12', 'P13'], request())
for dep in ('b,', ',b', 'b,,b', 'b==1', 'b <>1', 'b 1', 'b=+1', 'b>=', 'b!==1', 'b=1..2'):
series(obs, [package('a', fields=['1', '', dep, '', '', '']), package('b')],
'dependency/invalid-' + hx(dep), ['P10', 'P11', 'P12', 'P13'], request())
for mode in (0, 1):
values = [package('a', fields=['1', '', 'b>99', '-la', '-lprivate', '-DA']), package('b')]
series(obs, values, 'dependency/private-predicate-%d' % mode, ['P10', 'P13', 'P18', 'P20', 'P21'], request(mode, [root()]))
series(obs, values, 'dependency/empty-roots-%d' % mode, ['P10', 'P13', 'P18', 'P20', 'P21'], request(mode))
for relation in ('=', '!=', '<', '<=', '>', '>='):
values = [package('a', fields=['1', 'b%s01.0' % relation, '', '', '', '']), package('b')]
series(obs, values, 'dependency/relation-' + relation, ['P11', 'P12', 'P20'], operations='CF')
series(obs, [package()], 'constraint/later-root', ['P11', 'P17', 'P19', 'P20'],
request(roots=[root(), root('a', 5, [1])]), operations='CF')
series(obs, [package('a', fields=['1', 'b>=0,b>9', '', '', '', '']), package('b')],
'constraint/later-duplicate-edge', ['P11', 'P12', 'P19', 'P20'], operations='CF')
for actual in ('0', '1', '01.0.0', '1.2', '1.10', '4294967295', '0.' * 15 + '1'):
for relation in range(7):
target = [] if relation == 0 else [1, 0]
series(obs, [package(fields=[actual, '', '', '', '', ''])], 'version/compare-%s-%d' % (actual, relation),
['P11', 'P17', 'P20'], request(roots=[root('a', relation, target)]), operations='CF')
for text in ('', '.1', '1.', '1..2', '+1', '-0', '1e0', '1 2', '1\t2', '4294967296', '1.' * 16 + '1', '0' * 10000):
series(obs, [package(fields=[text, '', '', '', '', ''])], 'version/syntax-' + (hx(text) if len(text) < 50 else 'long-zero'),
['P10', 'P11'], operations='C')
for ending in ('zero', '1', '4294967295', '4294967296'):
width = 64 if ending == 'zero' else 63
variables = [('z0', '0' * width)] + [('z%d' % i, '${z%d}${z%d}' % (i - 1, i - 1)) for i in range(1, 14)]
tail = '' if ending == 'zero' else '0' * (16384 - len(ending)) + ending
values = [package(variables=variables, fields=['${z13}${z13}' + tail, '', '', '', '', ''])]
series(obs, values, 'version/one-MiB-' + ending, ['P07', 'P08', 'P09', 'P10', 'P11', 'P28'],
operations='ECF' if ending == 'zero' else 'CF')
for ending in ('4294967295', '4294967296'):
text = '0' * 5000 + '.000' + ending
series(obs, [package(fields=[text, '', '', '', '', ''])], 'version/long-final-segment-' + ending,
['P10', 'P11'], operations='CF')
for wanted in [request(2), request(0xffffffff), request(roots=[root('a', 7, [1])]),
request(roots=[root('a', 0xffffffff, [1])]), request(roots=[root('a', 0, [1])]),
request(roots=[root('a', 1)]), request(roots=[root('a', 1, [0] * 17)]),
request(roots=[root('missing')]), request(sysroot='relative'), request(sysroot='/x y'),
request(sysroot='/\x00'), request(sysroot='/' + 's' * 4095), request(sysroot='/' + 's' * 4096)]:
series(obs, [package()], 'request/invalid-or-boundary-%03d' % len(obs.labels), ['P17', 'P26'], wanted)
series(obs, [package(fields=['4294967295', '', '', '', '', ''])], 'request/u32-max-equal',
['P11', 'P17', 'P20'], request(roots=[root('a', 1, [0xffffffff] + [0] * 15)]), operations='CF')
for code in range(256):
series(obs, [package()], 'request/sysroot-byte-%03d' % code, ['P17'],
request(sysroot='/x' + chr(code)), operations='C')
paths = ['/', '/x', '//x///y/', '/x/./y/../z', '/../../x', '/sdk', '/sdk/a', '/sdk2/a',
'../x//./y', 'relative', '-L', '-I/x', '/sdk/../outside', '/a\\b', '/a#b', '/a:b']
for sysroot in ('', '/', '/sdk', '/a/../sdk//'):
for directory in paths:
text = '-I ' + directory + ' -L' + directory + ' -Wl,-rpath,/lib -lx -lx'
series(obs, [package(fields=['1', '', '', text, text, text])], 'paths/%s/%s' % (hx(sysroot), hx(directory)),
['P14', 'P15', 'P16', 'P18', 'P21', 'P22', 'P23'], request(1, [root()], sysroot), operations='F')
for field in (3, 4, 5):
values = ['1', '', '', '', '', '']; values[field] = '-I'
series(obs, [package('unused', fields=values), package('a')], 'flags/unreachable-bare-%d' % field,
['P10', 'P14', 'P15'], request(roots=[root()]))
long_root = '/' + 's' * 4095
large_flags = ' '.join(['-I/x'] * 255 + ['a' * 3076])
for extra in ('', 'a'):
series(obs, [package(fields=['1', '', '', '', '', large_flags + extra])], 'budget/argv-' + ('exact' if not extra else 'plus-one'),
['P14', 'P17', 'P21', 'P23', 'P24'], request(roots=[root()], sysroot=long_root), operations='CF')
for index in range(220):
count = rng.randrange(1, 7)
values = []
for i in range(count):
public, private = [], []
for j in range(i):
if rng.randrange(3) == 0:
(public if rng.randrange(2) else private).append('p%d%s' % (j, rng.choice(['', '>=0', '<=9', '!=7'])))
fields = [str(rng.randrange(6)), ','.join(public), ','.join(private),
'-lp%d -lp%d' % (i, i), '-lm', rng.choice(['-I${prefix}/h -DOK', '${flag}', '$${literal}', '-I ../relative'])]
values.append(package('p%d' % i, [('flag', '-I/a/%d -L /b/%d' % (i, i)),
('prefix', '/pkg%d' % i)], fields))
rng.shuffle(values)
roots = [root('p%d' % rng.randrange(count), rng.choice([0, 1, 4, 5]), []) for _ in range(rng.randrange(4))]
for item in roots:
if item[1]: item[2] = [rng.randrange(7)]
wanted = request(rng.randrange(2), roots, rng.choice(['', '/sdk', '/pkg0', '/a/../sys']))
series(obs, values, 'random/%03d' % index, ['P06', 'P07', 'P08', 'P10', 'P11', 'P12', 'P13',
'P14', 'P15', 'P17', 'P18', 'P19', 'P20', 'P21', 'P22', 'P23', 'P25'], wanted)
if index % 20 == 0:
obs.hold(1, 'random/hold-%03d' % index)
obs.use(1); series(obs, [package('other')], 'banks/other-%03d' % index, ['P25'])
obs.old(1, 'random/held-after-other-%03d' % index); obs.use(0)
obs.old(0, 'field/still-independent')
# Appended coverage preserves every earlier command and expected observation.
quoted = "left'quote right\"quote 'two words' \"three words\" -I/'x' -L/\"y\" #literal \\tail"
series(obs, [package(fields=['1', '', '', quoted, quoted, quoted])], 'literal/quotes-all-fields',
['P14', 'P15', 'P16', 'P21', 'P22', 'P23'], request(1, [root()], '/sdk'), operations='F')
series(obs, [package(variables=[('quoted', "'a b' \"c d\"")], fields=['1', '', '', '', '', '${quoted}'])],
'literal/quotes-after-expansion', ['P06', 'P14', 'P21', 'P22'], operations='F')
series(obs, [package(variables=unused, fields=['1', '', '', '${v255}', '', ''])],
'budget/used-empty-exponential-W', ['P07', 'P08', 'P09', 'P27', 'P28'], request())
huge_bytes = [('h0', 'x' * 65536)] + [('h%d' % i, '${h%d}${h%d}' % (i - 1, i - 1)) for i in range(1, 14)]
series(obs, [package(variables=huge_bytes, fields=['1', '', '', '${h13}', '', ''])],
'budget/used-nonempty-exponential-B', ['P07', 'P08', 'P09', 'P27', 'P28'], request())
obs.source([('duplicate', 'Version:1'), ('duplicate', 'Version:2')])
obs.call('P', 'names/duplicate-source', ['P02', 'P04', 'P05'])
series(obs, [package('duplicate'), package('duplicate', fields=['2', '', '', '', '', ''])],
'names/duplicate-repository-empty-roots', ['P02', 'P07', 'P10', 'P26'], request())
return obs.commands, obs.expected, obs.labels
def main():
commands, expected, labels = plan()
scratch = Path(tempfile.mkdtemp(prefix='embedded-hard-pc-', dir='/tmp'))
for name, content in CONTEXT.items():
(scratch / name).write_text(content)
for name in CANDIDATES:
(scratch / name).write_bytes((Path(sys.argv[1]) / name).read_bytes())
built = subprocess.run(['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic',
'pc_observe.cpp', *CANDIDATES, '-o', 'observe'], cwd=scratch,
text=True, encoding='utf-8', errors='replace', capture_output=True, timeout=30)
if built.returncode:
print('check=build clauses=P30 build failed: ' + built.stderr[-1500:].replace('\n', ' '))
return 1
# shortcut: retained scratch needs external exact-ownership cleanup; upgrade when the supervisor provides a private disposable workdir with its own precise cleanup receipt.
# After the candidate runs the trusted parent never reads/writes/traverses this shared scratch.
observed = subprocess.run([str(scratch / 'observe')], input='\n'.join(commands) + '\n', cwd=scratch,
text=True, encoding='utf-8', errors='replace', capture_output=True, timeout=20)
if observed.returncode:
print('check=execution clauses=P30 exit=%d stderr=%s' % (observed.returncode, observed.stderr[-300:]))
return 1
actual, failures = observed.stdout.splitlines(), []
for index in range(max(len(actual), len(expected))):
raw = actual[index] if index < len(actual) else '<missing>'
try:
got = json.loads(raw)
except (ValueError, TypeError):
got = raw
want = expected[index] if index < len(expected) else '<end>'
if (index >= len(actual) or index >= len(expected) or
json.dumps(got, sort_keys=True) != json.dumps(want, sort_keys=True)):
label, clauses = labels[index] if index < len(labels) else ('extra-output', ('P30',))
failures.append((index, label, clauses, got, want))
if failures:
for index, label, clauses, got, want in failures[:20]:
print('check=%s clauses=%s observation=%d got=%r expected=%r' %
(label, ','.join(clauses), index, str(got)[:180], str(want)[:180]))
print('mismatches=%d observations=%d' % (len(failures), len(expected)))
return 1
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())