cli: lower task spawn arguments iteratively

This commit is contained in:
dev committed 2026-10-11 17:34:08 +03:00
1 parent b85df6777e
commit c5d0d09a81
5 files changed
+442 -31

No files matched your search

+106
View File
@@ -955,6 +955,112 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
assert formatted_error[key] == value, formatted_error
print(f'task join rejection depth: {leaf}', flush=True)
# Spawn arguments preserve per-argument checking and loans until join.
# These valid chains keep at most one child outstanding in the same scope.
for kind in ('spawn-join', 'spawn-pair', 'spawn-mixed'):
for depth in (0, 1, 16, 256, 1024, 16384, 32768, 65536):
if depth == 32768 and kind != 'spawn-mixed':
continue
path = root / f'{kind}-{depth}.aji'
if kind == 'spawn-pair':
worker = 'fn pair(a: i64, b: i64) -> i64 effects [] { b }\n'
expression = 'task.join(task.spawn(s, pair, 2i64, ' * depth + '42i64' + '))' * depth
instructions, fuel, value = 3 * depth + 5, 4 * depth + 4, 42
elif kind == 'spawn-mixed':
worker = 'fn id(x: i64) -> i64 effects [] { x }\n'
expression = 'task.join(task.spawn(s, id, id(- ' * depth + 'id(42i64)' + ')))' * depth
instructions, fuel, value = 5 * depth + 6, 7 * depth + 6, 42 if depth % 2 == 0 else -42
else:
worker = 'fn id(x: i64) -> i64 effects [] { x }\n'
expression = 'task.join(task.spawn(s, id, ' * depth + '42i64' + '))' * depth
instructions, fuel, value = 2 * depth + 5, 3 * depth + 4, 42
path.write_text('module depth\n' + worker + 'fn main() -> i64 effects [task.spawn] { scope s { return ' + expression + '; } }\n')
limited = kind == 'spawn-mixed' and depth == 65536
checked = cli('check', path, code=1 if limited else 0)
if limited:
error = json.loads(checked)
assert (error['code'], error['limit'], error['max'], error['found']) == ('E5101', 'instructions per function', 262144, 327685), error
for command in ('run', 'report', 'explore'):
assert cli(command, path, code=1) == checked
else:
assert checked == b''
lowered = cli('lower', path)
actual = sum((line.lstrip().startswith(b'%') and b' = ' in line) or line.strip().startswith((b'ret', b'task.scope.end ')) for line in lowered.splitlines())
assert actual == instructions, (kind, depth, actual, instructions)
if depth <= 1024:
options = ('--quantum', 1, '--schedule-seed', 7, '--fuel', fuel)
result = cli('run', path, *options)
record = {'status': 'returned', 'value': value}
exhausted = {'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1}
if depth:
schedule = {'seed': 7, 'quantum': 1, 'choices': depth}
record['schedule'] = schedule
exhausted['schedule'] = schedule
assert json.loads(result) == record, result
assert json.loads(cli('run', path, '--quantum', 1, '--schedule-seed', 7, '--fuel', fuel - 1, code=5)) == exhausted
air = path.with_suffix('.air')
air.write_bytes(lowered)
assert cli('run', air, *options) == result
if depth <= 16:
for seed in (0, 1):
options = ('--quantum', 1, '--schedule-seed', seed, '--fuel', fuel)
seeded = cli('run', path, *options)
if depth:
record['schedule'] = {'seed': seed, 'quantum': 1, 'choices': depth}
assert json.loads(seeded) == record
assert cli('run', air, *options) == seeded
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
# Exact AIR parity avoids a second expensive deep verifier walk.
assert cli('lower', formatted) == lowered
if depth <= 1024:
assert cli('check', formatted) == b''
print(f'spawn argument accepted depth: {kind}', flush=True)
for leaf, code, offset, extra in (
('unknown', 'E7002', 0, {}),
('true', 'E7005', 0, {'found': 'bool'}),
('ref n', 'E7006', 0, {'reason': 'invalid_borrow'}),
('b"xy"', 'E7006', 0, {'reason': 'invalid_borrow'}),
('task.spawn(s, id)', 'E7005', 0, {'expected': '1', 'found': '0'}),
('task.spawn(s, missing, unknown)', 'E7002', len('task.spawn(s, '), {}),
('task.spawn(n, missing, unknown)', 'E7005', len('task.spawn('), {'found': 'i64'}),
('task.spawn(s, retained)', 'E7006', 0, {'reason': 'invalid_borrow_return'}),
('task.spawn(s, pair, unknown, ref n)', 'E7002', len('task.spawn(s, pair, '), {}),
('task.spawn(s, pair, id(1i64), ref n)', 'E7006', len('task.spawn(s, pair, id(1i64), '), {'reason': 'invalid_borrow'}),
('t', 'E7003', 0, {'suggestion': 'move t'}),
('move t', 'E7005', 0, {'expected': 'i64', 'found': 'task<i64>'}),
):
for depth in (16, 1024, 65536):
prefix = 'module depth\nfn id(x: i64) -> i64 effects [] { x }\nfn pair(a: i64, b: i64) -> i64 effects [] { b }\nfn retained(p: ref<i64>) -> ref<i64> from p effects [] { p }\nfn main() -> i64 effects [task.spawn] { var n: i64 = 1i64; scope s { let t = task.spawn(s, id, 0i64); return '
opening = 'task.join(task.spawn(s, id, '
expression = opening * depth + leaf + '))' * depth
path = root / f'spawn-argument-reject-{code}-{len(leaf)}-{offset}-{depth}.aji'
path.write_text(prefix + expression + '; } }\n')
expected = cli('check', path, code=1)
error = json.loads(expected)
assert (error['code'], error['file'], error['line'], error['col']) == (code, str(path), 5, len(prefix.split('\n')[-1]) + len(opening) * depth + offset + 1), error
for key, value in extra.items():
assert error[key] == value, error
for command in ('lower', 'run', 'report', 'explore'):
assert cli(command, path, code=1) == expected
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
formatted_error = json.loads(cli('check', formatted, code=1))
text = output.decode()
if leaf == 't':
pos = list(re.finditer(r'(?<![A-Za-z0-9_])t(?![A-Za-z0-9_])', text))[-1].start()
else:
pos = text.index(leaf) + offset
assert (formatted_error['code'], formatted_error['file'], formatted_error['line'], formatted_error['col']) == (code, str(formatted), text[:pos].count('\n') + 1, pos - text.rfind('\n', 0, pos)), formatted_error
for key, value in extra.items():
assert formatted_error[key] == value, formatted_error
print(f'spawn argument rejection depth: {leaf}', flush=True)
# Block/control callbacks remain native. Their existing shallow/deeper
# syntax must retain its capacity while expression dispatch is moved out
# of the native stack, including explicit-return callback re-entry.