splice 2.6.4

Span-safe refactoring kernel for 7 languages with Magellan code graph integration
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
# Schema Reference

This document describes the data schema used by Magellan, intended to be followed by other tools in the integrated toolset for consistent data representation.

## Table of Contents

1. [ID Formats]#id-formats
2. [Symbol Types]#symbol-types
3. [Symbol Properties]#symbol-properties
4. [Reference Types]#reference-types
5. [Call Graph Structure]#call-graph-structure
6. [Canonical FQN vs Display FQN]#canonical-fqn-vs-display-fqn
7. [Node Types]#node-types
8. [Edge Types]#edge-types
9. [Language Support]#language-support

---

## ID Formats

### Symbol ID

Stable identifier for symbols across indexing runs.

**Format**: 16 hex characters (64 bits)

**Generation**:
```
symbol_id = SHA256(language + ":" + fqn + ":" + span_id)[0:16]
```

**Components**:
- `language`: Programming language (e.g., "rust", "python")
- `fqn`: Fully-qualified name
- `span_id`: Span-based stable ID

**Stability**:
- Stable across re-indexing when position unchanged
- Changes on: rename, move, file rename, signature change

**Example**: `a1b2c3d4e5f6g7h8`

### Span ID

Stable identifier for a location in source code.

**Format**: 16 hex characters (64 bits)

**Generation**:
```
span_id = SHA256(file_path + ":" + byte_start + ":" + byte_end)[0:16]
```

**Components**:
- `file_path`: Path to source file
- `byte_start`: Start byte offset
- `byte_end`: End byte offset

**Example**: `f8e9d0c1b2a3f4e5`

### Match ID

Identifier for query results (ephemeral, not persisted).

**Format**: Hexadecimal hash string

**Generation**:
```
match_id = hash(symbol_name + file_path + byte_start)
```

**Example**: `abc123def456`

### Reference Match ID

Identifier for reference query results (ephemeral).

**Format**: Hexadecimal string with "ref_" prefix

**Example**: `ref_abc123def456`

### Execution ID

Unique identifier for a tool execution.

**Format**: `{timestamp_hex}-{pid_hex}`

**Generation**:
```
execution_id = format("{:x}-{:x}", unix_timestamp, process_id)
```

**Example**: `67abc123d4567-123a`

---

## Symbol Types

### Symbol Kinds (Normalized)

Internal normalized kinds used for querying:

| Normalized | Display Names | Description |
|------------|---------------|-------------|
| `fn` | Function, function | Standalone function |
| `method` | Method | Function within a type/impl |
| `struct` | Struct, Class | Composite data type |
| `enum` | Enum | Enumeration type |
| `trait` | Trait, Interface | Type interface/contract |
| `module` | Module, namespace | Namespace/scope |
| `variable` | Variable, let | Local variable |
| `const` | Const, static | Compile-time constant |
| `type` | Type, TypeAlias | Type alias |
| `union` | Union | Union type |
| `impl` | Impl block | Implementation block |
| `unknown` | Unknown | Unrecognized kind |

### Language-Specific Mappings

#### Rust

| Tree-sitter Kind | Normalized |
|------------------|------------|
| `function_item` | fn |
| `method_definition` | method |
| `struct_item` | struct |
| `enum_item` | enum |
| `trait_item` | trait |
| `mod_item` | module |
| `const_item` | const |
| `static_item` | const |
| `type_item` | type |
| `union_item` | union |
| `impl_item` | impl |

#### Python

| Tree-sitter Kind | Normalized |
|------------------|------------|
| `function_definition` | fn |
| `class_definition` | struct |
| `decorated_definition` | (depends on inner) |

#### JavaScript/TypeScript

| Tree-sitter Kind | Normalized |
|------------------|------------|
| `function_declaration` | fn |
| `method_definition` | method |
| `class_declaration` | struct |
| `interface_declaration` | trait |
| `type_alias_declaration` | type |

#### Java

| Tree-sitter Kind | Normalized |
|------------------|------------|
| `method_declaration` | method |
| `class_declaration` | struct |
| `interface_declaration` | trait |
| `enum_declaration` | enum |

#### C/C++

| Tree-sitter Kind | Normalized |
|------------------|------------|
| `function_definition` | fn |
| `class_specifier` | struct |
| `struct_specifier` | struct |
| `enum_specifier` | enum |

---

## Symbol Properties

### SymbolNode Schema

Stored in graph database for each symbol:

```json
{
  "symbol_id": "a1b2c3d4e5f6g7h8",
  "fqn": "my_crate::my_module::my_function",
  "canonical_fqn": "my_crate::src/lib.rs::Function my_function",
  "display_fqn": "my_crate::my_module::my_function",
  "name": "my_function",
  "kind": "Function",
  "kind_normalized": "fn",
  "byte_start": 42,
  "byte_end": 100,
  "start_line": 3,
  "start_col": 4,
  "end_line": 7,
  "end_col": 8
}
```

### Field Descriptions

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `symbol_id` | string | No | Stable symbol ID (16 hex chars) |
| `fqn` | string | No | Fully-qualified name |
| `canonical_fqn` | string | No | Unambiguous identity with file path |
| `display_fqn` | string | No | Human-readable FQN without file path |
| `name` | string | No | Simple symbol name |
| `kind` | string | Yes | Symbol kind (display form) |
| `kind_normalized` | string | No | Normalized kind for querying |
| `byte_start` | number | Yes | Start byte offset |
| `byte_end` | number | Yes | End byte offset |
| `start_line` | number | Yes | Start line (1-indexed) |
| `start_col` | number | Yes | Start column (0-indexed) |
| `end_line` | number | Yes | End line (1-indexed) |
| `end_col` | number | Yes | End column (0-indexed) |

### Span Position Fields

All spans use consistent position representation:

- **byte_start**: UTF-8 byte offset from file start (inclusive)
- **byte_end**: UTF-8 byte offset from file start (exclusive)
- **start_line**: Line number where span starts (1-indexed)
- **start_col**: Byte offset within start_line (0-indexed)
- **end_line**: Line number where span ends (1-indexed)
- **end_col**: Byte offset within end_line (0-indexed, exclusive)

---

## Reference Types

### Reference Kinds

Categorization of references:

| Kind | Description |
|------|-------------|
| `call` | Function/method call |
| `read` | Variable read access |
| `write` | Variable write access |
| `type_ref` | Type reference in annotation |
| `import` | Module/import reference |
| `inheritance` | Class/interface inheritance |

### ReferenceNode Schema

Stored in graph database for each reference:

```json
{
  "file": "src/main.rs",
  "byte_start": 150,
  "byte_end": 160,
  "start_line": 10,
  "start_col": 8,
  "end_line": 10,
  "end_col": 18
}
```

### ReferenceFact Schema

Transient fact during indexing:

```json
{
  "file_path": "src/main.rs",
  "referenced_symbol": "println",
  "byte_start": 150,
  "byte_end": 160,
  "start_line": 10,
  "start_col": 8,
  "end_line": 10,
  "end_col": 18
}
```

---

## Call Graph Structure

### Call Fact

Represents a caller → callee relationship:

```json
{
  "file_path": "src/main.rs",
  "caller": "main",
  "callee": "println",
  "caller_symbol_id": "a1b2c3d4",
  "callee_symbol_id": "e5f6g7h8",
  "byte_start": 150,
  "byte_end": 160,
  "start_line": 10,
  "start_col": 8,
  "end_line": 10,
  "end_col": 18
}
```

### CallNode Schema

Stored in graph database:

```json
{
  "file": "src/main.rs",
  "caller": "main",
  "callee": "println",
  "caller_symbol_id": "a1b2c3d4",
  "callee_symbol_id": "e5f6g7h8",
  "byte_start": 150,
  "byte_end": 160,
  "start_line": 10,
  "start_col": 8,
  "end_line": 10,
  "end_col": 18
}
```

### Call Direction

| Direction | Description |
|-----------|-------------|
| `in` | Incoming calls (callers of the symbol) |
| `out` | Outgoing calls (callees from the symbol) |

---

## Canonical FQN vs Display FQN

### Canonical FQN (Unambiguous Identity)

Format: `{crate}::{file_path}::{kind} {symbol_name}`

Purpose: Provides unambiguous symbol identity including file path.

Examples:
- `my_crate::src/lib.rs::Function my_function`
- `my_crate::src/main.rs::Function main`
- `my_crate::src/types.rs::Struct MyStruct`

Used for:
- Symbol identity across files
- Collision detection
- Stable references

### Display FQN (Human-Readable)

Format: `{crate}::{module_chain}::{type}::{symbol_name}`

Purpose: Shortened form for user-facing output.

Examples:
- `my_crate::my_function`
- `my_crate::my_module::my_helper`
- `my_crate::MyStruct::my_method`

Used for:
- CLI output
- User interaction
- Logging

### FQN Builder API

```rust
FqnBuilder::new(crate_name, file_path, scope_separator)
    .canonical(&scope_stack, symbol_kind, symbol_name)
    .display(&scope_stack, symbol_kind, symbol_name)
```

### Scope Separator by Language

| Language | Separator |
|----------|-----------|
| Rust | `::` |
| Python | `.` |
| Java | `.` |
| JavaScript/TypeScript | `.` |
| C/C++ | `::` |

---

## Node Types

### Graph Node Kinds

Stored in sqlitegraph:

| Kind | Description | Properties |
|------|-------------|------------|
| `File` | Source file | path, hash, last_indexed_at |
| `Symbol` | Code symbol | SymbolNode data |
| `Reference` | Symbol reference | ReferenceNode data |
| `Call` | Function call | CallNode data |
| `CodeChunk` | Source code chunk | content, checksum |

### FileNode Schema

```json
{
  "path": "src/main.rs",
  "hash": "sha256:abc123...",
  "last_indexed_at": 1706102400,
  "last_modified": 1706102350
}
```

Fields:
- `path`: File path (absolute or root-relative)
- `hash`: SHA-256 content hash
- `last_indexed_at`: Unix timestamp (seconds) of last indexing
- `last_modified`: Unix timestamp (seconds) of file mtime when indexed

---

## Edge Types

### Graph Edge Kinds

| Kind | From | To | Description |
|------|------|-------|-------------|
| `DEFINES` | File | Symbol | File defines a symbol |
| `REFERS` | Symbol | Reference | Symbol references another symbol |
| `CALLS` | Symbol | Call | Symbol makes a call |

### Edge Schema

```json
{
  "from": 123,
  "to": 456,
  "edge_type": "DEFINES",
  "data": {}
}
```

---

## Language Support

### Supported Languages

| Language | Extension | Status |
|----------|-----------|--------|
| Rust | `.rs` | Full support |
| Python | `.py` | Full support |
| JavaScript | `.js` | Full support |
| TypeScript | `.ts` | Full support |
| Java | `.java` | Full support |
| C | `.c`, `.h` | Full support |
| C++ | `.cpp`, `.hpp`, `.cc` | Full support |

### Language Detection

Based on file extension:
- `.rs` → Rust
- `.py` → Python
- `.js`, `.mjs`, `.cjs` → JavaScript
- `.ts` → TypeScript
- `.java` → Java
- `.c`, `.h` → C
- `.cpp`, `.hpp`, `.cc`, `.cxx` → C++

### Language Labels

Symbols are labeled with their language for efficient querying:

```bash
# Query by language
label --db db.db --label rust
```

---

## Conventions for Other Tools

When implementing data schemas for other tools:

1. **Use consistent ID formats**: 16 hex chars for stable IDs
2. **Follow span conventions**: Half-open ranges, 1-indexed lines, 0-indexed columns
3. **Include FQN variants**: Both canonical (with path) and display (readable)
4. **Normalize symbol kinds**: Use consistent kind_normalized values
5. **Label by language**: Enable language-based filtering
6. **Document schema version**: Track breaking changes
7. **Use SHA-256 for hashes**: Content checksums, ID generation

### Example: Symbol Schema for New Tool

```json
{
  "entity_id": "a1b2c3d4e5f6g7h8",
  "entity_type": "symbol",
  "language": "rust",
  "name": "my_function",
  "kind": "Function",
  "kind_normalized": "fn",
  "fqn": "my_crate::my_module::my_function",
  "canonical_fqn": "my_crate::src/lib.rs::Function my_function",
  "display_fqn": "my_crate::my_module::my_function",
  "location": {
    "file_path": "src/lib.rs",
    "byte_start": 42,
    "byte_end": 100,
    "start_line": 3,
    "start_col": 4,
    "end_line": 7,
    "end_col": 8
  }
}
```