1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! The re-arrival rule (ADR-0478 Decision 3): the frontier BFS's
//! pool-exactness seam, serving both pools. For the default pool
//! (DIFFERENT RELATIONSHIPS ⇒ TRAIL) it is the trail-existence check the
//! runner consults at every accepting arrival it cannot already prove a
//! trail for: the BFS answers WALK reachability; the engine's default
//! pool answers trail existence — the two coincide unless every in-bounds
//! walk to a node reuses a relationship (the measured `knows{1,2}` SF0.1
//! case: engine-trail 855 distinct endpoints vs the frontier's 856, the
//! delta being the seed, re-reached by a rel-reusing 2-walk whose
//! shortest trail falls below `min`;
//! the frontier-NFA design's §1 rule). For the WALK
//! pool (bounded REPEATABLE ELEMENTS) the in-bounds arrivals are the
//! frontier's native semantics — no rule question — and the seam answers
//! exactly one shape for it: the below-min candidates, through the
//! walk-length check (point 4 below).
//!
//! **Remedy (a) — a bounded local trail-existence search — not (b).**
//! The design note §7.1's cheaper alternative (b), "accept a re-arrival
//! only when ≥ 2 distinct rels connect the duplicated rel's pair", is
//! unsound: it is type-blind — a same-pair rel of a type the transition
//! cannot traverse repairs nothing (`0-[:KNOWS]-1-[:KNOWS]-2` plus a
//! `LIKES` parallel on the middle pair: (b) sees the parallel and
//! admits, no in-bounds `KNOWS` trail exists, the engine rejects —
//! pinned in `rearival_tests`), and a parallel of the right type may
//! already be used elsewhere on the chain. Remedy (a) proves trails,
//! never repairs walks.
//!
//! The rule's two proofs, cheapest first:
//!
//! 1. **The representative-chain proof** (no search): the arrival's
//! representative walk — the predecessor pair's first-arrival parent
//! chain plus the arriving rel — is a valid accepting NFA run of the
//! arrival's own hop count, in bounds. If its node sequence is
//! simple, the walk is a trail: a repeated relationship forces a
//! repeated node (a rel's second traversal departs from an endpoint
//! the first traversal already visited), so node-simple ⇒
//! rel-distinct. This is where the design note's "a shortest walk is
//! always a trail" claim lives, and it is the whole story for first
//! arrivals of the flat one-state family. It is NOT the whole story
//! for every first arrival: a context rel sharing a type with the
//! round it precedes (`(x)-[:KNOWS]->(m)-[:KNOWS*1..]-(y)`) admits a
//! rel-reusing walk as a pair's FIRST arrival — measured against the
//! engine and pinned in `rearival_tests` — so the rule runs for
//! every accepting arrival whose representative chain is not
//! node-simple, first or re-.
//! 2. **The bounded trail-existence search**: a distinct-rels DFS from
//! the seed over the same NFA, seeking any accepting arrival at the
//! target with hops in `[min_hops, max_hops]` — any length in
//! bounds, not the arriving hop count, because the endpoint contract
//! is "a trail exists", never "this walk is one".
//! 3. **The below-min candidates** (found by the differential corpus,
//! not anticipated by the design note): a node whose every OBSERVED
//! accepting arrival sits below `min` can still be the endpoint of a
//! longer trail — one whose final hop departs from a pair the dedup
//! filter expanded only at its own, earlier level, so the BFS never
//! observes the in-bounds arrival at all (a self-loop at the seed:
//! the trail `1-(loop)-1-(rel)-0` of `*2..3`, node 0 observed only
//! at hop 1). The runner flags every below-min accepting arrival as
//! a candidate and asks the rule once per candidate after the BFS —
//! the same memoized, budget-shared search (no chain proof applies:
//! the observed arrival is out of bounds by definition).
//! 4. **The walk-length check** — the WALK pool's version of the
//! candidate rule. Walk semantics have a `min` too, and the same
//! dedup blind spot: a self-loop at the seed hides the 2-walk
//! `1-(loop)-1-(rel)-0` of `*2..3` from the BFS exactly as it hides
//! the trail, so the walk pool flagged nothing and answered a wrong
//! endpoint set (engine REPEATABLE `{0,1}`, walk pool `{1}` — the
//! measured minimal case, pinned in `rearival_tests`). Rel reuse
//! being legal, no trail proof applies; a candidate is admitted iff
//! an in-bounds WALK to the node exists — a
//! reachability-within-hop-bounds question over the same NFA. With
//! no distinct-rels set to carry, the search's whole state is the
//! position `(node, state, hops)`: a failed position is a proven
//! dead end, memoized, so the search is polynomial in
//! pairs × hop bound — strictly cheaper than the trail search, whose
//! rel-set state forbids position memoization. The completeness
//! argument carries over from the default pool: any in-bounds
//! endpoint's walk has a final hop departing a pair `(u, s)` the BFS
//! marked at a first-arrival level `j ≤ hops - 1`, and expanding
//! that pair at level `j` fires the same accepting transition at hop
//! `j + 1 ≤ max` — so every in-bounds endpoint has an OBSERVED
//! accepting arrival, in-bounds (admitted outright, the pool's
//! native semantics) or below-min (flagged as a candidate); the
//! walk-length check then re-proves what the unobserved arrival
//! would have shown, and admits no node without a found in-bounds
//! walk — exact in both directions. The matcher declines unbounded
//! REPEATABLE (ADR-0478 §5), so every production walk-pool shape is
//! bounded and the check exact; an unbounded `run` call falls to
//! [`REARRIVAL_DEPTH_CEILING`] and the budget corner below.
//!
//! **The bound** (the rule's whole point — never a full unbounded
//! enumeration): the chain proof is O(arrival hop count) table walks,
//! no graph access; the search is capped twice — depth at `max_hops`,
//! or [`REARRIVAL_DEPTH_CEILING`] for an unbounded quantifier (a trail
//! is at most rel-count long; 64 covers every realistic trail while the
//! engine's own unbounded default-pool cap is the rel count), and work
//! at a shared [`REARRIVAL_WORK_BUDGET`] expansion budget across the
//! whole run — a run can never spend more on the rule than that, spread
//! over as many checks as it performs. The verdict per target node is
//! memoized: the search is target-shaped, not arrival-shaped, so one
//! node is searched once however many times it is re-arrived.
//!
//! **Witnesses, not just verdicts** (the `witness-emit` node's
//! extension): every search that proves a path returns it — the found
//! branch as a [`Chain`], memoized with the verdict — because the
//! capped emission's witness for a below-min candidate, or for a
//! default-pool re-arrival the chain proof could not settle, is the
//! *path the search proved*, never the recorded arrival's
//! representative (which is exactly the chain that failed the proof).
//! The capped ACYCLIC/SIMPLE modes add the third search,
//! [`RearivalRule::mode_witness`]: node-simple (the close allowed for
//! SIMPLE) alongside rel-distinct, the per-witness mode check's own
//! bound.
//!
//! **One search, three policies.** The three bounded searches — the
//! trail-existence DFS, the walk-length check, the node-simple
//! ACYCLIC/SIMPLE ask — share their whole skeleton (the budget guard,
//! the branch reset, the start state's epsilon-closure loop, the
//! per-rel expansion, the witness clone), so the search core
//! ([`search`], the child module split from this one by the file-size
//! rule) spells that skeleton ONCE: [`SeekTables`] carries
//! the immutable tables, [`RearivalRule::search`] below drives,
//! [`seek`] is the step core, and [`SearchPolicy`] is the one axis the
//! three differ on — what state a search carries, which rels it may
//! traverse, which arrivals it may descend into, and whether a
//! fully-explored miss is a memoizable dead end. The three named
//! searches are one-line policy selections ([`TrailPolicy`],
//! [`WalkPolicy`], [`NodeSimple`]).
//!
//! **Soundness direction**: an endpoint is admitted only on a proven
//! trail — the chain proof or a search that found one. A shape the
//! bounded search cannot cover is NOT admitted — but the two covers
//! differ, and the difference is load-bearing (ADR-0516): a miss at the
//! depth ceiling is a disproof *within the bounds* (the search answers
//! "within `[min_hops, depth_cap]`", exact for every bounded
//! quantifier; a longer trail stays the documented conservative
//! corner), while a search the shared WORK budget cut is no verdict at
//! all. The verdict is therefore tri-state ([`Verdict`]): a cut search
//! memoizes as [`Verdict::Exhausted`], never as a disproof, and the
//! rule flags the run ([`RearivalRule::budget_exhausted`]) so the rider
//! wiring discards the run's buffered rows and declines the clause to
//! the next rung unchanged — the DFS answers these shapes exactly, and
//! no partial or unproven answer escapes. The silent alternative was
//! the measured bug (LABS-952): under a `*k..k` one-state cycle every
//! endpoint is a below-min candidate, the first hopeless candidate's
//! search drains the shared budget, and caching the cut as a rejection
//! emptied the whole answer.
use ;
use crate;
use crateExecGraph;
use ;
use ;
use StateMemos;
use ;
use NodeMap;
/// The trail-existence search's depth ceiling for unbounded quantifiers
/// (`max_hops = None`): a trail is at most rel-count hops long, but the
/// engine's own unbounded default-pool cap being the rel count does not
/// make rel count a searchable bound — 64 covers every trail the
/// differential corpus and the measured classes produce, and a longer
/// one is the documented conservative rejection.
pub const REARRIVAL_DEPTH_CEILING: u32 = 64;
/// The re-arrival rule's run state: the shared tables the proofs read
/// ([`SeekTables`]), the recycled sparse buffers ([`RuleBuffers`],
/// borrowed), and the shared expansion budget. Constructed once per run
/// by the BFS runner under the run's pool; asked once per accepting
/// arrival the default pool's chain proof cannot settle, and once per
/// below-min candidate under either pool.
pub
/// The rule's per-clause recycled buffers: the memoized verdict map and
/// the chain proof's node-visit set, both sparse ([`NodeMap`] — sized by
/// the nodes the rule asks about, never by the store's span), created
/// once per engaged clause and handed to each run's [`RearivalRule`] in
/// turn — the rule's half of the frontier family's allocation bound
/// (ADR-0478 Decision 6's allocation half). The reset between runs and
/// between chain walks is generational, never a sweep: [`NodeMap::clear`]
/// bumps the map's generation, so a fresh memo per run (and a fresh
/// visit set per walk) costs O(1), the per-run verdict semantics exact
/// (a verdict is seed-shaped, so a previous run's verdicts must never
/// leak into the next).
pub
/// One bounded search's three-way verdict (ADR-0516): the budget corner
/// made explicit. A budget-cut search is neither a proof nor a
/// disproof — memoizing it as a rejection was the silent-wrong-answer
/// bug (LABS-952): under a `*k..k` one-state cycle every endpoint is a
/// below-min candidate, the first hopeless candidate's search drains
/// the shared budget, and caching the cut as "rejected" emptied every
/// remaining endpoint's answer.
pub