From 53907ecf25f614d71f9f62d76da1b9e1ebfe1a6a Mon Sep 17 00:00:00 2001 From: "A.Shakhmatov" Date: Sat, 10 Oct 2026 13:30:10 +0300 Subject: [PATCH] cli: inspect anonymous source properties iteratively --- docs/design/air-type-depth-audit.md | 8 ++ docs/design/open-questions.md | 2 + docs/plan/phase-6-source-language.md | 6 + src/frontend/type_properties.zig | 185 ++++++++++++++++++++++++++- 4 files changed, 194 insertions(+), 7 deletions(-) diff --git a/docs/design/air-type-depth-audit.md b/docs/design/air-type-depth-audit.md index 93afb92..bc52229 100644 --- a/docs/design/air-type-depth-audit.md +++ b/docs/design/air-type-depth-audit.md @@ -140,3 +140,11 @@ The 16,384-array source again aborts before this increment at 24a3876: the fresh Tests compare 1,736 shallow leaf/array/wrapper combinations against the independent pre-repair derivation, including unknown names/constructors, array boundary lengths and competing error priority. A 32,768-level mixed annotation is constructed and inspected iteratively; unknown and nonvalue leaves retain their original diagnostics. Every allocator request is failed in turn, with arena cleanup checked. Type-position and Copy-content CLI integration pass 74 and 119 checks. All units and format checks pass. All 1,791 conformance cases pass with exact corpus coverage; all 1,421 public source checks and 506 accepted lowerings retain byte-identical old/new exit/stdout/stderr. Complete checking still accepts the 4,096-array input. At 16,384 it now passes resolveType and aborts in type_properties.firstInvalid (10,001 symbolized frames, exit -6 and no diagnostic JSON). Only child core capture is disabled; no timeout ends a probe. Recursive diagnostic type text and other source property/query paths remain audit candidates. This is not arbitrary-depth source acceptance: Q-059, Q-060's closing validation and Phase 6 gates remain open. Claude's previous nominal-property review covers neither this increment nor the generic-use repair. + +## Source anonymous property scans + +At b23f3e6, the ordinary 16,384-array source aborts in type_properties.firstInvalid (10,001 frames). A second source wraps the same array annotation in own_array; it aborts in linear (7,060 frames). Both exit -6 without diagnostic JSON. The repair makes firstInvalid follow array/wrapper syntax with constant traversal state, preserving outer-before-inner own_array checks and the island/frozen/arc stop. Its known-linearity helper peels only anonymous array chains; named declaration lookup, parameter binding/shadowing, memo generations, cyclic paths and fixed-point retries retain their original algorithm. No new allocation or error conversion is introduced by either loop. + +Tests compare 2,160 shallow cases against the independent pre-repair anonymous traversal, including nominal/generic content, cycles with a positive witness, unknown parameters, parameter shadowing, nested own_array and graph stops; the nominal declaration algorithm is shared, not an independent nominal oracle. Deep array/mixed-wrapper queries cover 32,768 levels, positive/negative content and original failure spans, while an allocator failing at its first request verifies zero work allocations on these anonymous paths. Existing diamond/cycle tests, all units and format checks pass. Copy-content/generic CLI integration passes 119/32 checks; all 1,421 public source checks and 506 accepted lowerings retain exact old/new exit/stdout/stderr. All 1,791 conformance cases pass with exact corpus coverage. + +Both previously aborting complete 16,384-level checks now accept without output, as does the 4,096-array input. Non-Copy content behind the deep own_array returns exactly one E7005 at 2:19, with constraint Copy and type_constructor own_array. Core capture alone is disabled and no timeout ends a probe. These process outcomes establish repairs for these inputs, not arbitrary-depth source acceptance. A separate 8,192-declaration source probe confirms remaining native recursion through named source types: before this repair, 3,326 linear and 3,325 declaration frames; afterward, 3,306 and 3,305, both exit -6 without diagnostic JSON. Source Ty equality/text/AIR conversion and other closing call paths also remain to audit. Q-059 and Phase 6 gates remain open; Q-060's full per-step validation remains separate. No syntax/feature/code/bundle growth, runtime/effect/ownership rule or acceptance limit changes. No fresh Claude review is claimed. diff --git a/docs/design/open-questions.md b/docs/design/open-questions.md index 444197e..71f6b9a 100644 --- a/docs/design/open-questions.md +++ b/docs/design/open-questions.md @@ -481,6 +481,8 @@ The [source generic-use increment](air-type-depth-audit.md#source-generic-use-de The [source annotation resolution increment](air-type-depth-audit.md#source-annotation-resolution) replaces recursive source-to-Ty construction with explicit continuations while preserving pre/post checks and error positions. Deep isolated resolution succeeds; complete checking exposes the next confirmed failure in type_properties.firstInvalid. Source property traversal, diagnostic text and closing call-path checks remain open; no universal source depth limit is added. +The [source anonymous property increment](air-type-depth-audit.md#source-anonymous-property-scans) removes confirmed recursion from firstInvalid and the array-chain portion of known linearity. Both 16,384-array and own_array checks now accept, while non-Copy content retains E7005 at its original span. A separate 8,192-declaration probe confirms remaining linear/declaration native-stack exhaustion before and after this increment. Named-declaration repair and source Ty/query closing checks remain open; no acceptance limit or semantic rule changes. + ## Q-060 — Native recursion while materializing runtime values The [executed runtime audit](runtime-value-depth-audit.md) confirms that a verified one-byte value with 32,768 length-one nested arrays aborts inside zeroValue under `aji run --fuel 8`, rather than producing a run record. The instruction has not exhausted fuel. Iterative runtime traversal must preserve initialization, value shape, allocator/root lifetimes, collection safety and instruction accounting; adding a depth trap/acceptance limit needs a normative decision. At that baseline, recursive copying, value sizing, collection and serialization were additional audit candidates with unverified failure paths; the increment below records subsequent executed failures. Compiler type repairs in Q-059 do not resolve Q-060. diff --git a/docs/plan/phase-6-source-language.md b/docs/plan/phase-6-source-language.md index 1070f5b..a375af1 100644 --- a/docs/plan/phase-6-source-language.md +++ b/docs/plan/phase-6-source-language.md @@ -466,3 +466,9 @@ Units/format checks and generic/static-function CLI integration (32/25) pass. Al The [Q-059 annotation-resolution increment](../design/air-type-depth-audit.md#source-annotation-resolution) lowers anonymous source types with explicit continuations. Constructor-before-child and content-after-child checks preserve the old error priority and spans; nominal identity and syntax are unchanged. Tests cover 1,736 independent shallow comparisons, 32,768 mixed annotation levels, leaf rejection and allocation-error cleanup. Type-position/Copy-content integration passes 74/119 checks; all units/format checks and 1,791 conformance cases pass with exact corpus coverage. All 1,421 public source checks and 506 accepted lowerings retain byte-identical old/new exit/stdout/stderr. The complete 4,096-array check still accepts; 16,384 now reaches the next confirmed native-stack failure in type_properties.firstInvalid. No normative/bundle/feature/code change or accepted-depth limit is introduced. Q-059's source property/closing audit, Q-060's closing validation and Phase 6 gates remain open. No fresh Claude review of this increment is claimed. + +## Source anonymous property scans (2026-10-10) + +The [Q-059 source-property increment](../design/air-type-depth-audit.md#source-anonymous-property-scans) makes syntax scanning and anonymous-array linearity iterative, retaining own_array priority, graph stops and nominal fixed points. The two complete 16,384-level source checks that previously aborted now accept; deep non-Copy content returns the original E7005. Tests cover 2,160 independent shallow anonymous comparisons, 32,768-level queries and zero work allocations on anonymous paths. + +Units/format checks, Copy-content/generic CLI integration (119/32) and byte-identical old/new checks/lowerings (1,421/506) pass; all 1,791 conformance cases pass with exact corpus coverage. A separate 8,192-declaration source confirms the remaining linear/declaration stack failure before and after this increment. Named-source repair, source Ty/query closing checks, Q-060 validation and Phase 6 gates remain open. No normative/bundle/feature/code change or accepted-depth limit is introduced; no fresh Claude review is claimed. diff --git a/src/frontend/type_properties.zig b/src/frontend/type_properties.zig index 71b54a3..4c74f81 100644 --- a/src/frontend/type_properties.zig +++ b/src/frontend/type_properties.zig @@ -15,8 +15,9 @@ const Memo = struct { generation: usize = 0, }; -pub fn firstInvalid(t: ast.Type, context: anytype, lookup: anytype, params: []const ast.TypeParam) std.mem.Allocator.Error!?ast.Type { - switch (t.kind) { +pub fn firstInvalid(annotation: ast.Type, context: anytype, lookup: anytype, params: []const ast.TypeParam) std.mem.Allocator.Error!?ast.Type { + var t = annotation; + while (true) switch (t.kind) { .wrapped => |w| { if (std.mem.eql(u8, w.wrapper, "own_array")) { var memo: Memo = .{}; @@ -28,21 +29,25 @@ pub fn firstInvalid(t: ast.Type, context: anytype, lookup: anytype, params: []co } // Graph well-formedness keeps its independently specified E2111. for ([_][]const u8{ "island", "frozen", "arc" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return null; - return firstInvalid(w.inner.*, context, lookup, params); + t = w.inner.*; }, - .array => |a| return firstInvalid(a.elem.*, context, lookup, params), + .array => |a| t = a.elem.*, else => return null, - } + }; } -fn linear(t: ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool { +fn linear(annotation: ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool { + var t = annotation; + // Arrays add no environment or nominal path state. Peel only this + // anonymous chain; declaration fixed points retain their original rules. + while (t.kind == .array) t = t.kind.array.elem.*; switch (t.kind) { .file => return true, .wrapped => |w| { for ([_][]const u8{ "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "atomic" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return true; return false; }, - .array => |a| return linear(a.elem.*, context, lookup, env, path, memo), + .array => unreachable, .name => |name| { for (env.params, 0..) |p, i| if (std.mem.eql(u8, p.name, name)) { return i < env.known_linear.len and env.known_linear[i]; @@ -148,3 +153,169 @@ test "Copy-content query shares cyclic diamond branches" { // An unshared walk repeats every path to the same cycle. Count queries, not elapsed time. try std.testing.expect(probe.lookups <= 22); } + +// Independent pre-repair anonymous traversal, used only on shallow trees. +const ReferenceAnonymousProperties = struct { + fn firstInvalid(t: ast.Type, context: anytype, lookup: anytype, params: []const ast.TypeParam) std.mem.Allocator.Error!?ast.Type { + switch (t.kind) { + .wrapped => |w| { + if (std.mem.eql(u8, w.wrapper, "own_array")) { + var memo: Memo = .{}; + while (true) { + const generation = memo.generation; + if (try ReferenceAnonymousProperties.linear(w.inner.*, context, lookup, &.{ .params = params }, null, &memo)) return w.inner.*; + if (generation == memo.generation) break; + } + } + // Graph well-formedness keeps its independently specified E2111. + for ([_][]const u8{ "island", "frozen", "arc" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return null; + return ReferenceAnonymousProperties.firstInvalid(w.inner.*, context, lookup, params); + }, + .array => |a| return ReferenceAnonymousProperties.firstInvalid(a.elem.*, context, lookup, params), + else => return null, + } + } + fn linear(t: ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool { + switch (t.kind) { + .file => return true, + .wrapped => |w| { + for ([_][]const u8{ "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "atomic" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return true; + return false; + }, + .array => |a| return ReferenceAnonymousProperties.linear(a.elem.*, context, lookup, env, path, memo), + .name => |name| { + for (env.params, 0..) |p, i| if (std.mem.eql(u8, p.name, name)) { + return i < env.known_linear.len and env.known_linear[i]; + }; + if (lookup(context, name, t.span)) |d| return declaration(d, &.{}, context, lookup, env, path, memo); + for ([_][]const u8{ "region", "stream", "listener" }) |tag| if (std.mem.eql(u8, name, tag)) return true; + return false; + }, + .applied => |a| { + const d = lookup(context, a.name, t.span) orelse return false; + return declaration(d, a.args, context, lookup, env, path, memo); + }, + else => return false, + } + } +}; + +const PropertyProbe = struct { + arena: std.mem.Allocator, + module: ast.Module = .{ .name = "properties", .types = &.{}, .functions = &.{} }, + lookups: usize = 0, + + fn lookup(p: *@This(), name: []const u8, _: ast.Span) ?ast.TypeDecl { + p.lookups += 1; + for (p.module.types) |d| if (std.mem.eql(u8, d.name, name)) return d; + return null; + } +}; + +fn expectAnonymousPropertyMatch(arena: std.mem.Allocator, m: ast.Module, t: ast.Type, params: []const ast.TypeParam) !void { + var original: PropertyProbe = .{ .arena = arena, .module = m }; + var current: PropertyProbe = .{ .arena = arena, .module = m }; + try std.testing.expectEqualDeep( + try ReferenceAnonymousProperties.firstInvalid(t, &original, PropertyProbe.lookup, params), + try firstInvalid(t, ¤t, PropertyProbe.lookup, params), + ); + try std.testing.expectEqual(original.lookups, current.lookups); +} + +test "anonymous source properties preserve shallow failures and graph stops" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const a = arena.allocator(); + const source = + \\module properties + \\struct Plain { x: u8 } + \\struct Linear { x: own } + \\struct Cycle { a: Cycle, b: Linear } + \\enum Choice { empty, value(Linear) } + \\struct Box { value: T } + \\fn main() -> void effects [] {} + ; + const m = (try @import("parse.zig").parse(a, source)).ok; + const inner_span: ast.Span = .{ .start = .{ .line = 9, .col = 11 }, .end = .{ .line = 9, .col = 18 }, .file = "properties.aji" }; + const outer_span: ast.Span = .{ .start = .{ .line = 8, .col = 2 }, .end = .{ .line = 10, .col = 5 }, .file = "properties.aji" }; + const scalar: ast.Type = .{ .span = inner_span, .kind = .{ .name = "u8" } }; + const owner: ast.Type = .{ .span = inner_span, .kind = .{ .wrapped = .{ .wrapper = "own", .inner = &scalar } } }; + const leaves = [_]ast.Type{ + scalar, + owner, + .{ .span = inner_span, .kind = .{ .name = "missing" } }, + .{ .span = inner_span, .kind = .{ .name = "region" } }, + .{ .span = inner_span, .kind = .{ .name = "stream" } }, + .{ .span = inner_span, .kind = .{ .name = "Plain" } }, + .{ .span = inner_span, .kind = .{ .name = "Linear" } }, + .{ .span = inner_span, .kind = .{ .name = "Cycle" } }, + .{ .span = inner_span, .kind = .{ .name = "Choice" } }, + .{ .span = inner_span, .kind = .{ .name = "T" } }, + .{ .span = inner_span, .kind = .{ .file = "read" } }, + .{ .span = inner_span, .kind = .{ .cap = "mem.alloc" } }, + .{ .span = inner_span, .kind = .{ .function_ref = .{ .name = "operation" } } }, + .{ .span = inner_span, .kind = .{ .applied = .{ .name = "Box", .args = &.{scalar} } } }, + .{ .span = inner_span, .kind = .{ .applied = .{ .name = "Box", .args = &.{owner} } } }, + }; + const wrappers = [_][]const u8{ "ref", "mut", "slice", "slice_mut", "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "iref", "atomic", "unknown" }; + const param: ast.TypeParam = .{ .name = "Linear", .span = inner_span, .constraints = &.{} }; + var comparisons: usize = 0; + for ([_][]const ast.TypeParam{ &.{}, &.{param} }) |params| for (&leaves) |*leaf| { + const array: ast.Type = .{ .span = outer_span, .kind = .{ .array = .{ .elem = leaf, .len = 3 } } }; + for ([_]ast.Type{ leaf.*, array }) |base| { + try expectAnonymousPropertyMatch(a, m, base, params); + comparisons += 1; + const owned: ast.Type = .{ .span = outer_span, .kind = .{ .wrapped = .{ .wrapper = "own_array", .inner = &base } } }; + try expectAnonymousPropertyMatch(a, m, owned, params); + comparisons += 1; + for (wrappers) |wrapper| { + const wrapped: ast.Type = .{ .span = inner_span, .kind = .{ .wrapped = .{ .wrapper = wrapper, .inner = &owned } } }; + try expectAnonymousPropertyMatch(a, m, wrapped, params); + comparisons += 1; + const outer_owned: ast.Type = .{ .span = outer_span, .kind = .{ .wrapped = .{ .wrapper = "own_array", .inner = &wrapped } } }; + try expectAnonymousPropertyMatch(a, m, outer_owned, params); + comparisons += 1; + } + } + }; + try std.testing.expectEqual(@as(usize, 2160), comparisons); +} + +test "anonymous source properties traverse deep types without work allocations" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const a = arena.allocator(); + const span: ast.Span = .{ .start = .{ .line = 2, .col = 3 }, .end = .{ .line = 2, .col = 9 } }; + const content_span: ast.Span = .{ .start = .{ .line = 7, .col = 12 }, .end = .{ .line = 7, .col = 18 } }; + const scalar: ast.Type = .{ .span = span, .kind = .{ .name = "u8" } }; + var leaf = scalar; + const nodes = try a.alloc(ast.Type, 32_768); + var child: *const ast.Type = &leaf; + for (nodes) |*node| { + node.* = .{ .span = content_span, .kind = .{ .array = .{ .elem = child, .len = 1 } } }; + child = node; + } + var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 }); + var probe: PropertyProbe = .{ .arena = failing.allocator() }; + try std.testing.expect(try firstInvalid(child.*, &probe, PropertyProbe.lookup, &.{}) == null); + const owned: ast.Type = .{ .span = span, .kind = .{ .wrapped = .{ .wrapper = "own_array", .inner = child } } }; + try std.testing.expect(try firstInvalid(owned, &probe, PropertyProbe.lookup, &.{}) == null); + leaf.kind = .{ .wrapped = .{ .wrapper = "own", .inner = &scalar } }; + const bad = (try firstInvalid(owned, &probe, PropertyProbe.lookup, &.{})).?; + try std.testing.expect(bad.kind == .array); + try std.testing.expectEqualDeep(content_span, bad.span); + for ([_][]const u8{ "island", "frozen", "arc" }) |wrapper| { + const graph: ast.Type = .{ .span = span, .kind = .{ .wrapped = .{ .wrapper = wrapper, .inner = &owned } } }; + try std.testing.expect(try firstInvalid(graph, &probe, PropertyProbe.lookup, &.{}) == null); + } + // Mixed wrappers are followed by firstInvalid, while the linearity + // query still stops at non-owning wrappers instead of following them. + leaf = scalar; + for (nodes, 0..) |*node, i| if (i % 2 == 0) { + node.kind = .{ .wrapped = .{ .wrapper = "ref", .inner = if (i == 0) &leaf else &nodes[i - 1] } }; + }; + try std.testing.expect(try firstInvalid(child.*, &probe, PropertyProbe.lookup, &.{}) == null); + try std.testing.expect(try firstInvalid(owned, &probe, PropertyProbe.lookup, &.{}) == null); + try std.testing.expect(!failing.has_induced_failure); + try std.testing.expectEqual(@as(usize, 0), failing.allocations); +}