raysense 0.10.0

Architectural X-ray for your codebase. Live, local, agent-ready.
Documentation
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
 *   Copyright (c) 2025-2026 Anton Kundenko <singaraiona@gmail.com>
 *   All rights reserved.

 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:

 *   The above copyright notice and this permission notice shall be included in all
 *   copies or substantial portions of the Software.

 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *   SOFTWARE.
 */

/* ==========================================================================
 *  Rayfall builtins for the graph algorithm family (.graph.*).
 *
 *  Mirrors the HNSW pattern in embedding.c: the graph itself is an opaque
 *  ray_rel_t* wrapped inside a -RAY_I64 atom tagged with RAY_ATTR_GRAPH.
 *  Lifecycle:
 *    (.graph.build T 'src 'dst [ 'weight ])  → handle
 *    (.graph.info  h)                         → dict
 *    (.graph.free  h)                         → null  (idempotent)
 *
 *  Algorithm wrappers all share the same micro-skeleton:
 *    1. Validate the graph handle.
 *    2. Validate the remaining arguments.
 *    3. Build a one-node DAG, dispatch via ray_execute, return the table.
 *
 *  No libc malloc/free here — all scratch allocation goes through
 *  ray_alloc / ray_sys_alloc (CSR uses ray_sys_alloc internally).
 * ========================================================================== */

#include "ops/internal.h"
#include "lang/internal.h"

/* --------------------------------------------------------------------------
 * Handle wrap / unwrap
 * -------------------------------------------------------------------------- */

static ray_rel_t* graph_unwrap(ray_t* h) {
    if (!h) return NULL;
    if (h->type != -RAY_I64) return NULL;
    if (!(h->attrs & RAY_ATTR_GRAPH)) return NULL;
    return (ray_rel_t*)(uintptr_t)h->i64;
}

static ray_t* graph_wrap(ray_rel_t* rel) {
    ray_t* h = ray_alloc(0);
    if (!h || RAY_IS_ERR(h)) return h ? h : ray_error("oom", NULL);
    h->type   = -RAY_I64;
    h->attrs |= RAY_ATTR_GRAPH;
    h->i64    = (int64_t)(uintptr_t)rel;
    return h;
}

/* --------------------------------------------------------------------------
 * Argument helpers
 * -------------------------------------------------------------------------- */

static bool atom_is_int(ray_t* a) {
    return a && (a->type == -RAY_I64 || a->type == -RAY_I32 || a->type == -RAY_I16);
}

static int64_t atom_to_i64(ray_t* a) {
    if (!a) return 0;
    switch (a->type) {
        case -RAY_I64: return a->i64;
        case -RAY_I32: return (int64_t)a->i32;
        case -RAY_I16: return (int64_t)a->i16;
        default:       return 0;
    }
}

/* Resolve a sym-or-string argument to an interned sym ID.  Used by the
 * column-name slots of (.graph.build) — accept either 'name (already an
 * interned RAY_SYM atom) or "name" (turn into an intern). */
static int64_t arg_to_sym(ray_t* a) {
    if (!a) return -1;
    if (a->type == -RAY_SYM) return a->i64;
    if (a->type == -RAY_STR) {
        const char* p = ray_str_ptr(a);
        size_t len = ray_str_len(a);
        if (!p || len == 0) return -1;
        return ray_sym_intern(p, len);
    }
    return -1;
}

/* Coerce an integer column to RAY_I64 by allocating a fresh I64 vec.
 * For RAY_I64 input, just retain and return.  For RAY_I32/I16/U8/SYM,
 * widen.  Caller releases the result. */
static ray_t* widen_to_i64(ray_t* col) {
    if (!col || !ray_is_vec(col)) return NULL;
    if (col->type == RAY_I64) {
        ray_retain(col);
        return col;
    }
    int64_t n = col->len;
    ray_t* out = ray_vec_new(RAY_I64, n > 0 ? n : 1);
    if (!out || RAY_IS_ERR(out)) return NULL;
    out->len = n;
    int64_t* dst = (int64_t*)ray_data(out);
    void* src = ray_data(col);
    switch (col->type) {
        case RAY_I32:
            for (int64_t i = 0; i < n; i++) dst[i] = (int64_t)((int32_t*)src)[i];
            break;
        case RAY_I16:
            for (int64_t i = 0; i < n; i++) dst[i] = (int64_t)((int16_t*)src)[i];
            break;
        case RAY_U8:
        case RAY_BOOL:
            for (int64_t i = 0; i < n; i++) dst[i] = (int64_t)((uint8_t*)src)[i];
            break;
        case RAY_SYM:
            for (int64_t i = 0; i < n; i++)
                dst[i] = read_col_i64(src, i, RAY_SYM, col->attrs);
            break;
        default:
            ray_release(out);
            return NULL;
    }
    return out;
}

/* Compute n_nodes for a relationship by scanning a widened I64 column.
 * Returns max(col[i]) + 1, or 0 if the column is empty. */
static int64_t max_plus_one(ray_t* col_i64) {
    int64_t n = col_i64->len;
    if (n <= 0) return 0;
    int64_t* d = (int64_t*)ray_data(col_i64);
    int64_t mx = d[0];
    for (int64_t i = 1; i < n; i++) if (d[i] > mx) mx = d[i];
    return mx >= 0 ? mx + 1 : 0;
}

/* --------------------------------------------------------------------------
 * (.graph.build T 'src 'dst [ 'weight ]) → graph handle
 *
 * Builds a CSR over the rows of T.  src/dst columns must be integer- or
 * sym-typed; if either is not RAY_I64 it is widened into a temporary I64
 * vector for ray_rel_from_edges.  When a weight column is supplied, it
 * must be RAY_F64 and is exposed via ray_rel_set_props for algorithms
 * that consume edge weights (dijkstra, mst, k-shortest, astar).
 * -------------------------------------------------------------------------- */
ray_t* ray_graph_build_fn(ray_t** args, int64_t n) {
    if (n < 3 || n > 4) return ray_error("rank", NULL);
    ray_t* tbl = args[0];
    if (!tbl || tbl->type != RAY_TABLE) return ray_error("type", NULL);

    int64_t src_sym = arg_to_sym(args[1]);
    int64_t dst_sym = arg_to_sym(args[2]);
    if (src_sym < 0 || dst_sym < 0) return ray_error("type", NULL);

    int64_t weight_sym = -1;
    if (n == 4) {
        weight_sym = arg_to_sym(args[3]);
        if (weight_sym < 0) return ray_error("type", NULL);
    }

    ray_t* src_col = ray_table_get_col(tbl, src_sym);
    ray_t* dst_col = ray_table_get_col(tbl, dst_sym);
    if (!src_col || !dst_col) return ray_error("name", NULL);
    if (!ray_is_vec(src_col) || !ray_is_vec(dst_col)) return ray_error("type", NULL);
    if (src_col->len != dst_col->len) return ray_error("length", NULL);

    /* Widen src/dst into RAY_I64 if needed.  ray_rel_from_edges insists
     * on I64 columns directly. */
    ray_t* src_i64 = widen_to_i64(src_col);
    if (!src_i64) return ray_error("type", NULL);
    ray_t* dst_i64 = widen_to_i64(dst_col);
    if (!dst_i64) { ray_release(src_i64); return ray_error("type", NULL); }

    /* Determine n_nodes from the larger of (max(src), max(dst)) + 1.  This
     * lets the user omit a node-count argument; bigger graphs that need a
     * specific node universe can post-process by reusing fk-build. */
    int64_t n_src = max_plus_one(src_i64);
    int64_t n_dst = max_plus_one(dst_i64);
    int64_t n_nodes = n_src > n_dst ? n_src : n_dst;

    /* Build a temporary edge table that holds the widened columns under the
     * sym IDs that ray_rel_from_edges expects ("src", "dst" — names are
     * looked up by these strings).  The original table's column names are
     * arbitrary; we always re-name internally. */
    int64_t s_sym = sym_intern_safe("src", 3);
    int64_t d_sym = sym_intern_safe("dst", 3);
    ray_t* edges = ray_table_new(2);
    if (!edges || RAY_IS_ERR(edges)) {
        ray_release(src_i64); ray_release(dst_i64);
        return ray_error("oom", NULL);
    }
    edges = ray_table_add_col(edges, s_sym, src_i64);
    ray_release(src_i64);
    edges = ray_table_add_col(edges, d_sym, dst_i64);
    ray_release(dst_i64);
    if (!edges || RAY_IS_ERR(edges)) return ray_error("oom", NULL);

    ray_rel_t* rel = ray_rel_from_edges(edges, "src", "dst",
                                         n_nodes, n_nodes, true);
    if (!rel) {
        ray_release(edges);
        return ray_error("oom", NULL);
    }

    /* Optional: attach a weight column.  Algorithms that need edge weights
     * (dijkstra, mst, …) look the column up by sym ID via rel->fwd.props.
     * The column is referenced by its original name in the user's table. */
    if (weight_sym >= 0) {
        ray_t* w_col = ray_table_get_col(tbl, weight_sym);
        if (!w_col || !ray_is_vec(w_col)) {
            ray_rel_free(rel); ray_release(edges);
            return ray_error("name", NULL);
        }
        if (w_col->len != src_col->len) {
            ray_rel_free(rel); ray_release(edges);
            return ray_error("length", NULL);
        }
        if (w_col->type != RAY_F64 && w_col->type != RAY_I64 &&
            w_col->type != RAY_I32 && w_col->type != RAY_F32) {
            ray_rel_free(rel); ray_release(edges);
            return ray_error("type", NULL);
        }

        /* Coerce weights to RAY_F64 (the algorithms expect F64). */
        ray_t* w_f64 = NULL;
        if (w_col->type == RAY_F64) {
            ray_retain(w_col);
            w_f64 = w_col;
        } else {
            int64_t nrow = w_col->len;
            w_f64 = ray_vec_new(RAY_F64, nrow > 0 ? nrow : 1);
            if (!w_f64 || RAY_IS_ERR(w_f64)) {
                ray_rel_free(rel); ray_release(edges);
                return ray_error("oom", NULL);
            }
            w_f64->len = nrow;
            double* dst = (double*)ray_data(w_f64);
            void*   src = ray_data(w_col);
            switch (w_col->type) {
                case RAY_I64:
                    for (int64_t i = 0; i < nrow; i++) dst[i] = (double)((int64_t*)src)[i];
                    break;
                case RAY_I32:
                    for (int64_t i = 0; i < nrow; i++) dst[i] = (double)((int32_t*)src)[i];
                    break;
                case RAY_F32:
                    for (int64_t i = 0; i < nrow; i++) dst[i] = (double)((float*)src)[i];
                    break;
                default: break;
            }
        }

        /* Wrap as a one-column "weight" props table — the algorithms read
         * the column by the sym name baked into op->graph.weight_col_sym. */
        int64_t w_sym_local = weight_sym;
        ray_t* props = ray_table_new(1);
        if (!props || RAY_IS_ERR(props)) {
            ray_release(w_f64);
            ray_rel_free(rel); ray_release(edges);
            return ray_error("oom", NULL);
        }
        props = ray_table_add_col(props, w_sym_local, w_f64);
        ray_release(w_f64);
        if (!props || RAY_IS_ERR(props)) {
            ray_rel_free(rel); ray_release(edges);
            return ray_error("oom", NULL);
        }
        ray_rel_set_props(rel, props);
        ray_release(props);
    }

    ray_release(edges);

    ray_t* h = graph_wrap(rel);
    if (!h || RAY_IS_ERR(h)) { ray_rel_free(rel); return h; }
    return h;
}

/* (.graph.free h) → null.  Idempotent: clears the ATTR bit so a second
 * call returns "type" instead of double-freeing. */
ray_t* ray_graph_free_fn(ray_t* h) {
    ray_rel_t* rel = graph_unwrap(h);
    if (!rel) return ray_error("type", NULL);
    ray_rel_free(rel);
    h->i64 = 0;
    h->attrs &= ~RAY_ATTR_GRAPH;
    return RAY_NULL_OBJ;
}

/* (.graph.info h) → dict { n_nodes, n_edges, sorted, has_weights }.
 * Mirrors hnsw-info — keys are interned syms with no hyphens so the
 * 'foo lookup syntax works. */
ray_t* ray_graph_info_fn(ray_t* h) {
    ray_rel_t* rel = graph_unwrap(h);
    if (!rel) return ray_error("type", NULL);

    ray_t* keys = ray_sym_vec_new(RAY_SYM_W64, 4);
    if (RAY_IS_ERR(keys)) return keys;
    ray_t* vals = ray_list_new(4);
    if (RAY_IS_ERR(vals)) { ray_release(keys); return vals; }

    int64_t k_nodes  = sym_intern_safe("n_nodes",  7);
    int64_t k_edges  = sym_intern_safe("n_edges",  7);
    int64_t k_sorted = sym_intern_safe("sorted",   6);
    int64_t k_wts    = sym_intern_safe("has_weights", 11);

    ray_t* v_nodes  = make_i64(rel->fwd.n_nodes);
    ray_t* v_edges  = make_i64(rel->fwd.n_edges);
    ray_t* v_sorted = make_bool(rel->fwd.sorted ? 1 : 0);
    ray_t* v_wts    = make_bool(rel->fwd.props != NULL);

    keys = ray_vec_append(keys, &k_nodes);
    vals = ray_list_append(vals, v_nodes); ray_release(v_nodes);
    keys = ray_vec_append(keys, &k_edges);
    vals = ray_list_append(vals, v_edges); ray_release(v_edges);
    keys = ray_vec_append(keys, &k_sorted);
    vals = ray_list_append(vals, v_sorted); ray_release(v_sorted);
    keys = ray_vec_append(keys, &k_wts);
    vals = ray_list_append(vals, v_wts); ray_release(v_wts);

    return ray_dict_new(keys, vals);
}

/* --------------------------------------------------------------------------
 * Algorithm wrappers — every one of them follows the same template:
 *   1. Unwrap the graph handle (and validate).
 *   2. Pull optional/positional algorithm parameters off args[].
 *   3. ray_graph_new(NULL) → builder → ray_execute → ray_graph_free.
 *   4. Return the resulting table (or error verbatim).
 * -------------------------------------------------------------------------- */

/* Helper: retrieve weight-column sym ID from rel->fwd.props.  Algorithms
 * like dijkstra/mst need a const char* but op-builders take it through
 * ray_sym_intern; we just look up the first column name. */
static const char* rel_weight_col_name(ray_rel_t* rel) {
    if (!rel || !rel->fwd.props) return NULL;
    int64_t name_id = ray_table_col_name(rel->fwd.props, 0);
    if (name_id < 0) return NULL;
    ray_t* s = ray_sym_str(name_id);
    if (!s) return NULL;
    /* ray_sym_str returns an arena-owned string view — its data outlives
     * the call (the symbol table is process-lived).  Safe to alias. */
    const char* p = ray_str_ptr(s);
    ray_release(s);
    return p;
}

/* (.graph.pagerank h [iter] [damping]) → table {_node, _rank} */
ray_t* ray_graph_pagerank_fn(ray_t** args, int64_t n) {
    if (n < 1 || n > 3) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);

    uint16_t iters = 30;
    double damping = 0.85;
    if (n >= 2) {
        if (!atom_is_int(args[1])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[1]);
        if (v <= 0 || v > 65535) return ray_error("domain", NULL);
        iters = (uint16_t)v;
    }
    if (n >= 3) {
        if (args[2]->type != -RAY_F64 && !atom_is_int(args[2]))
            return ray_error("type", NULL);
        damping = (args[2]->type == -RAY_F64) ? args[2]->f64
                                              : (double)atom_to_i64(args[2]);
        if (damping <= 0.0 || damping >= 1.0) return ray_error("domain", NULL);
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_pagerank(g, rel, iters, damping);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.connected h) → table {_node, _component} */
ray_t* ray_graph_connected_fn(ray_t** args, int64_t n) {
    if (n != 1) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_connected_comp(g, rel);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.dijkstra h src [dst] [max-depth]) → table {_node, _dist, _depth} */
ray_t* ray_graph_dijkstra_fn(ray_t** args, int64_t n) {
    if (n < 2 || n > 4) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    if (!atom_is_int(args[1])) return ray_error("type", NULL);
    int64_t src_id = atom_to_i64(args[1]);

    int64_t dst_id = -1;
    if (n >= 3 && args[2]) {
        /* nil-or-int permitted for dst; nil => single-source mode. */
        if (RAY_IS_NULL(args[2])) {
            dst_id = -1;
        } else if (atom_is_int(args[2])) {
            dst_id = atom_to_i64(args[2]);
        } else {
            return ray_error("type", NULL);
        }
    }
    uint8_t max_depth = 255;
    if (n >= 4) {
        if (!atom_is_int(args[3])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[3]);
        if (v <= 0 || v > 255) return ray_error("domain", NULL);
        max_depth = (uint8_t)v;
    }

    const char* w_col = rel_weight_col_name(rel);
    if (!w_col) return ray_error("schema", "graph has no weight column");

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* src_op = ray_const_i64(g, src_id);
    ray_op_t* dst_op = (dst_id >= 0) ? ray_const_i64(g, dst_id) : NULL;
    if (!src_op || (dst_id >= 0 && !dst_op)) {
        ray_graph_free(g); return ray_error("oom", NULL);
    }
    ray_op_t* op = ray_dijkstra(g, src_op, dst_op, rel, w_col, max_depth);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.louvain h [max-iter]) → table {_node, _community} */
ray_t* ray_graph_louvain_fn(ray_t** args, int64_t n) {
    if (n < 1 || n > 2) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    uint16_t iters = 100;
    if (n == 2) {
        if (!atom_is_int(args[1])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[1]);
        if (v <= 0 || v > 65535) return ray_error("domain", NULL);
        iters = (uint16_t)v;
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_louvain(g, rel, iters);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.degree h) → table {_node, _in_degree, _out_degree, _degree} */
ray_t* ray_graph_degree_fn(ray_t** args, int64_t n) {
    if (n != 1) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_degree_cent(g, rel);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.topsort h) → table {_node, _order} */
ray_t* ray_graph_topsort_fn(ray_t** args, int64_t n) {
    if (n != 1) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_topsort(g, rel);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.dfs h src [max-depth]) → table {_node, _depth, _parent} */
ray_t* ray_graph_dfs_fn(ray_t** args, int64_t n) {
    if (n < 2 || n > 3) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    if (!atom_is_int(args[1])) return ray_error("type", NULL);
    int64_t src_id = atom_to_i64(args[1]);
    uint8_t max_depth = 255;
    if (n == 3) {
        if (!atom_is_int(args[2])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[2]);
        if (v < 0 || v > 255) return ray_error("domain", NULL);
        max_depth = (uint8_t)v;
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* src_op = ray_const_i64(g, src_id);
    if (!src_op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_op_t* op = ray_dfs(g, src_op, rel, max_depth);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.cluster h) → table {_node, _coefficient} */
ray_t* ray_graph_cluster_fn(ray_t** args, int64_t n) {
    if (n != 1) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_cluster_coeff(g, rel);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.betweenness h [sample]) → table {_node, _centrality} */
ray_t* ray_graph_betweenness_fn(ray_t** args, int64_t n) {
    if (n < 1 || n > 2) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    uint16_t sample = 0;  /* 0 = exact */
    if (n == 2) {
        if (!atom_is_int(args[1])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[1]);
        if (v < 0 || v > 65535) return ray_error("domain", NULL);
        sample = (uint16_t)v;
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_betweenness(g, rel, sample);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.closeness h [sample]) → table {_node, _centrality} */
ray_t* ray_graph_closeness_fn(ray_t** args, int64_t n) {
    if (n < 1 || n > 2) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    uint16_t sample = 0;
    if (n == 2) {
        if (!atom_is_int(args[1])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[1]);
        if (v < 0 || v > 65535) return ray_error("domain", NULL);
        sample = (uint16_t)v;
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_closeness(g, rel, sample);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.mst h) → table {_src, _dst, _weight} */
ray_t* ray_graph_mst_fn(ray_t** args, int64_t n) {
    if (n != 1) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    const char* w_col = rel_weight_col_name(rel);
    if (!w_col) return ray_error("schema", "graph has no weight column");

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* op = ray_mst(g, rel, w_col);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.random-walk h src [walk-len]) → table {_step, _node} */
ray_t* ray_graph_random_walk_fn(ray_t** args, int64_t n) {
    if (n < 2 || n > 3) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    if (!atom_is_int(args[1])) return ray_error("type", NULL);
    int64_t src_id = atom_to_i64(args[1]);
    uint16_t walk_len = 10;
    if (n == 3) {
        if (!atom_is_int(args[2])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[2]);
        if (v <= 0 || v > 65535) return ray_error("domain", NULL);
        walk_len = (uint16_t)v;
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* src_op = ray_const_i64(g, src_id);
    if (!src_op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_op_t* op = ray_random_walk(g, src_op, rel, walk_len);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.k-shortest h src dst k) → table {_path_id, _node, _dist} */
ray_t* ray_graph_k_shortest_fn(ray_t** args, int64_t n) {
    if (n != 4) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    if (!atom_is_int(args[1]) || !atom_is_int(args[2]) || !atom_is_int(args[3]))
        return ray_error("type", NULL);
    int64_t src_id = atom_to_i64(args[1]);
    int64_t dst_id = atom_to_i64(args[2]);
    int64_t k_v    = atom_to_i64(args[3]);
    if (k_v <= 0 || k_v > 65535) return ray_error("domain", NULL);
    const char* w_col = rel_weight_col_name(rel);
    if (!w_col) return ray_error("schema", "graph has no weight column");

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* src_op = ray_const_i64(g, src_id);
    ray_op_t* dst_op = ray_const_i64(g, dst_id);
    if (!src_op || !dst_op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_op_t* op = ray_k_shortest(g, src_op, dst_op, rel, w_col, (uint16_t)k_v);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.shortest-path h src dst [max-depth]) → BFS-shortest path table.
 *
 * The underlying ray_shortest_path consumes its src/dst operands as op
 * inputs; we materialise them via ray_const_i64 the same way the other
 * src/dst wrappers do. */
ray_t* ray_graph_shortest_path_fn(ray_t** args, int64_t n) {
    if (n < 3 || n > 4) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    if (!atom_is_int(args[1]) || !atom_is_int(args[2])) return ray_error("type", NULL);
    int64_t src_id = atom_to_i64(args[1]);
    int64_t dst_id = atom_to_i64(args[2]);
    uint8_t max_depth = 255;
    if (n == 4) {
        if (!atom_is_int(args[3])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[3]);
        if (v <= 0 || v > 255) return ray_error("domain", NULL);
        max_depth = (uint8_t)v;
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* src_op = ray_const_i64(g, src_id);
    ray_op_t* dst_op = ray_const_i64(g, dst_id);
    if (!src_op || !dst_op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_op_t* op = ray_shortest_path(g, src_op, dst_op, rel, max_depth);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.expand h src [direction]) → table {_src, _dst}.
 * direction: 0=forward (default), 1=reverse, 2=both. */
ray_t* ray_graph_expand_fn(ray_t** args, int64_t n) {
    if (n < 2 || n > 3) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    if (!atom_is_int(args[1])) return ray_error("type", NULL);
    int64_t src_id = atom_to_i64(args[1]);
    uint8_t direction = 0;
    if (n == 3) {
        if (!atom_is_int(args[2])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[2]);
        if (v < 0 || v > 2) return ray_error("domain", NULL);
        direction = (uint8_t)v;
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    /* OP_EXPAND consumes a src-nodes vector input.  Pack the single seed
     * into a 1-element vec so the execution path matches multi-seed use. */
    ray_t* seed_vec = ray_vec_from_raw(RAY_I64, &src_id, 1);
    if (!seed_vec || RAY_IS_ERR(seed_vec)) {
        ray_graph_free(g); return ray_error("oom", NULL);
    }
    ray_op_t* src_op = ray_const_vec(g, seed_vec);
    ray_release(seed_vec);
    if (!src_op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_op_t* op = ray_expand(g, src_op, rel, direction);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}

/* (.graph.var-expand h src min-depth max-depth [direction] [track-path]) */
ray_t* ray_graph_var_expand_fn(ray_t** args, int64_t n) {
    if (n < 4 || n > 6) return ray_error("rank", NULL);
    ray_rel_t* rel = graph_unwrap(args[0]);
    if (!rel) return ray_error("type", NULL);
    if (!atom_is_int(args[1]) || !atom_is_int(args[2]) || !atom_is_int(args[3]))
        return ray_error("type", NULL);
    int64_t src_id    = atom_to_i64(args[1]);
    int64_t min_d_v   = atom_to_i64(args[2]);
    int64_t max_d_v   = atom_to_i64(args[3]);
    if (min_d_v < 0 || min_d_v > 255 || max_d_v < min_d_v || max_d_v > 255)
        return ray_error("domain", NULL);
    uint8_t direction = 0;
    bool    track     = false;
    if (n >= 5) {
        if (!atom_is_int(args[4])) return ray_error("type", NULL);
        int64_t v = atom_to_i64(args[4]);
        if (v < 0 || v > 2) return ray_error("domain", NULL);
        direction = (uint8_t)v;
    }
    if (n >= 6) {
        if (args[5]->type != -RAY_BOOL && !atom_is_int(args[5]))
            return ray_error("type", NULL);
        track = (args[5]->type == -RAY_BOOL) ? (args[5]->b8 != 0)
                                              : (atom_to_i64(args[5]) != 0);
    }

    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_t* seed_vec = ray_vec_from_raw(RAY_I64, &src_id, 1);
    if (!seed_vec || RAY_IS_ERR(seed_vec)) {
        ray_graph_free(g); return ray_error("oom", NULL);
    }
    ray_op_t* src_op = ray_const_vec(g, seed_vec);
    ray_release(seed_vec);
    if (!src_op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_op_t* op = ray_var_expand(g, src_op, rel, direction,
                                   (uint8_t)min_d_v, (uint8_t)max_d_v, track);
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    ray_t* result = ray_execute(g, op);
    ray_graph_free(g);
    return result;
}