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
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
# Unified JSON Schema for LLM Toolset

**Created:** 2026-01-22
**Status:** Design Specification
**Applies to:** magellan, splice, llmtransform, llmsearch, llmastsearch, llmfilewrite

---

## Executive Summary

This document defines a unified JSON output schema across all LLM-native tools, building on the proven patterns from **Magellan** and **Splice**. The goal is to create a consistent, LLM-parsable output format that enables:

1. **Tool chaining** - Output from one tool can be consumed by another
2. **Traceability** - All operations tracked with stable IDs
3. **Precision** - Byte-accurate spans with UTF-8 safety
4. **Validation** - Checksums for integrity verification
5. **Extensibility** - Schema versioning for backward compatibility

---

## Table of Contents

1. [Design Principles]#design-principles
2. [Core Types]#core-types
3. [Response Wrappers]#response-wrappers
4. [Span Model]#span-model
5. [Rich Span Extensions]#rich-span-extensions
6. [Tool-Specific Variants]#tool-specific-variants
7. [Error Diagnostics]#error-diagnostics
8. [Implementation Guide]#implementation-guide
9. [Migration Path]#migration-path
10. [CLI Flags for Optional Features]#cli-flags

---

## Design Principles

### 1. Schema Versioning

All responses include a `schema_version` field following semantic versioning:

```json
{
  "schema_version": "1.0.0",
  ...
}
```

**Magellan's pattern (adopted):** Use `schema_version` instead of `version` for clarity.

### 2. Execution Tracking

All operations include a stable `execution_id` (UUID v4):

```json
{
  "execution_id": "550e8400-e29b-41d4-a716-446655440000",
  ...
}
```

**Splice's inconsistency (to fix):** Splice uses `operation_id` instead of `execution_id`.

### 3. Half-Open Ranges

All spans use half-open ranges `[start, end)`:

- `byte_start` is **inclusive**
- `byte_end` is **exclusive**
- Length = `byte_end - byte_start`

**Magellan's documentation (adopted):** Extensive comments explaining half-open semantics.

### 4. UTF-8 Byte Offsets

All positions are UTF-8 byte offsets (not character indices):
- Matches tree-sitter's API
- Matches Rust's string slicing
- Safe for all Unicode content

### 5. Stable Identifiers

All entities have stable IDs:
- `execution_id`: UUID v4 for operation tracking
- `span_id`: SHA-256 hash of `file_path:byte_start:byte_end` (Magellan's pattern)
- `match_id`: UUID v4 for individual matches

---

## Core Types

### Span (Canonical Definition)

**Source:** Magellan's `src/output/command.rs:176-389`

```rust
/// Span in source code (byte + line/column)
///
/// Represents a **half-open range** [start, end) where:
/// - byte_start is inclusive (first byte INCLUDED)
/// - byte_end is exclusive (first byte NOT included)
///
/// All offsets are UTF-8 byte positions. Lines are 1-indexed for user-friendliness.
/// Columns are 0-indexed byte offsets within each line.
pub struct Span {
    /// Stable span ID (SHA-256 hash of file_path:byte_start:byte_end)
    pub span_id: String,

    /// File path (absolute or root-relative)
    pub file_path: String,

    /// Byte range start (inclusive, first byte INCLUDED)
    pub byte_start: usize,

    /// Byte range end (exclusive, first byte NOT included)
    pub byte_end: usize,

    /// Start line (1-indexed)
    pub start_line: usize,

    /// Start column (0-indexed, byte-based)
    pub start_col: usize,

    /// End line (1-indexed)
    pub end_line: usize,

    /// End column (0-indexed, byte-based)
    pub end_col: usize,
}
```

**Span ID Generation (Magellan's algorithm):**

```rust
pub fn generate_span_id(file_path: &str, byte_start: usize, byte_end: usize) -> String {
    let mut hasher = Sha256::new();
    hasher.update(file_path.as_bytes());
    hasher.update(b":");
    hasher.update(byte_start.to_be_bytes());
    hasher.update(b":");
    hasher.update(byte_end.to_be_bytes());

    let result = hasher.finalize();
    // First 8 bytes as 16 hex characters
    format!("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7])
}
```

**Properties:**
- Deterministic: Same inputs always produce same ID
- Platform-independent: SHA-256 consistent across architectures
- Position-based: Not affected by content changes at same position

### Match Result

```rust
/// Match result with symbol information
pub struct Match {
    /// Stable match ID (UUID v4)
    pub match_id: String,

    /// Span information
    pub span: Span,

    /// Matched content (for text search) or capture name (for AST)
    pub content: String,

    /// Symbol kind (for AST queries): "function", "class", etc.
    pub kind: Option<String>,

    /// Parent symbol name (for nested definitions)
    pub parent: Option<String>,

    /// Stable symbol ID (for cross-reference)
    pub symbol_id: Option<String>,
}
```

---

## Response Wrappers

### Query/Search Response Wrapper

**Magellan's pattern (adopted for all query tools):**

```rust
/// Wrapper for all JSON responses
pub struct JsonResponse<T> {
    /// Schema version for parsing stability
    pub schema_version: String,

    /// Unique execution ID for this run
    pub execution_id: String,

    /// Response data
    pub data: T,

    /// Whether the response is partial (e.g., truncated)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub partial: Option<bool>,
}
```

**Usage:**

```json
{
  "schema_version": "1.0.0",
  "execution_id": "550e8400-e29b-41d4-a716-446655440000",
  "data": {
    "matches": [...]
  },
  "partial": false
}
```

### Mutation Response Wrapper

**Splice's pattern (adopted for mutation tools):**

```rust
/// Top-level operation result for mutations
pub struct OperationResult {
    /// Schema version
    pub schema_version: String,

    /// Unique operation ID (UUID v4)
    pub execution_id: String,

    /// Operation type: "patch", "delete", "rename", "apply_files"
    pub operation_type: String,

    /// Status: "ok", "error", "partial"
    pub status: String,

    /// Human-readable message
    pub message: String,

    /// Timestamp (ISO 8601)
    pub timestamp: String,

    /// Workspace root (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace: Option<String>],

    /// Primary result data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<OperationData>,

    /// Error details if status is "error"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<ErrorDetails>,
}
```

**Usage:**

```json
{
  "schema_version": "2.0.0",
  "execution_id": "550e8400-e29b-41d4-a716-446655440000",
  "operation_type": "patch",
  "status": "ok",
  "message": "Successfully patched 1 symbol in 1 file",
  "timestamp": "2026-01-22T10:00:00Z",
  "workspace": "/path/to/project",
  "result": {
    "type": "patch",
    "file": "src/lib.rs",
    "spans": [...]
  }
}
```

---

## Span Model

### Coordinate Field Names

**Current inconsistencies:**

| Tool | Line Start | Line End | Col Start | Col End |
|------|-----------|---------|-----------|---------|
| Magellan | `start_line` | `end_line` | `start_col` | `end_col` |
| Splice | `line_start` | `line_end` | `col_start` | `col_end` |
| llmsearch | `line_number` | N/A | `column_number` | N/A |
| llmastsearch | `start.row` | `end.row` | `start.column` | `end.column` |

**Standardized format (Magellan's convention):**

```rust
pub struct SpanCoordinates {
    pub byte_start: usize,
    pub byte_end: usize,
    pub start_line: usize,    // NOT line_start
    pub end_line: usize,      // NOT line_end
    pub start_col: usize,     // NOT col_start
    pub end_col: usize,       // NOT col_end
}
```

**Rationale for Magellan's convention:**
- More explicit (start_line vs line_start reads better)
- Matches semantic naming (start/end pair)
- Consistent with LSP's startPosition/endPosition pattern

### Span Serialization

**All tools must serialize spans as:**

```json
{
  "span_id": "a1b2c3d4e5f6g7h8",
  "file_path": "src/main.rs",
  "byte_start": 100,
  "byte_end": 200,
  "start_line": 5,
  "start_col": 4,
  "end_line": 10,
  "end_col": 2
}
```

**Minimal span (line/col unknown):**

```json
{
  "span_id": "...",
  "file_path": "src/main.rs",
  "byte_start": 100,
  "byte_end": 200,
  "start_line": 0,
  "end_line": 0,
  "start_col": 0,
  "end_col": 0
}
```

---

## Rich Span Extensions (Optional Fields)

The following OPTIONAL fields extend spans with semantic and contextual information. All fields are opt-in via flags like `--with-context`, `--with-relationships`, etc.

### 1. Context Field

**Purpose:** Provide surrounding lines without additional file reads.

```rust
pub struct SpanContext {
    /// Lines before the span (default: 3 lines)
    pub before: Vec<String>,

    /// The actual span content (for verification)
    pub selected: Vec<String>,

    /// Lines after the span (default: 3 lines)
    pub after: Vec<String>,
}
```

**Usage:**

```json
{
  "span_id": "...",
  "file_path": "src/main.rs",
  "byte_start": 100,
  "byte_end": 200,
  "start_line": 5,
  "start_col": 4,
  "end_line": 10,
  "end_col": 2,
  "context": {
    "before": [
      "/// Documentation comment",
      "use std::collections::HashMap;",
      ""
    ],
    "selected": [
      "fn process(data: &str) -> Result<String> {",
      "    // implementation",
      "}"
    ],
    "after": [
      "",
      "fn main() {",
      "    process(\"hello\").unwrap();",
      "}"
    ]
  }
}
```

**Why this matters:**
- LLM stops guessing surrounding structure
- Splice patches become safer
- No need for additional Magellan calls
- Multi-line patches become deterministic

### 2. Semantic Kind and Language

**Purpose:** Add semantic meaning beyond syntactic position.

```rust
pub struct SpanSemantics {
    /// Semantic kind: "function", "class", "method", "struct", "enum", "trait", etc.
    pub semantic_kind: String,

    /// Programming language: "rust", "python", "typescript", etc.
    pub language: String,
}
```

**Usage:**

```json
{
  "span": { /* ... */ },
  "semantic_kind": "function",
  "language": "rust"
}
```

**Why this matters:**
- Enables smarter transforms
- Safe symbol-level operations
- Lets Splice enforce AST boundaries
- Enables multi-language refactors
- LLM can infer correct patch structure

**Supported semantic kinds by language:**

| Language | Kinds |
|----------|-------|
| Rust | function, method, struct, enum, trait, impl, mod, const, static, type, macro |
| Python | function, method, class, async_function, decorator, module, variable |
| TypeScript | function, method, class, interface, type, enum, namespace, variable |
| JavaScript | function, method, class, variable, statement |

### 3. Relationships Block

**Purpose:** Embed call graph information directly into spans.

```rust
pub struct SpanRelationships {
    /// Functions that call this symbol
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub callers: Vec<SymbolReference>,

    /// Functions called by this symbol
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub callees: Vec<SymbolReference>,

    /// Import statements referencing this symbol
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub imports: Vec<SymbolReference>,

    /// Export statements exposing this symbol
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub exports: Vec<SymbolReference>,
}

pub struct SymbolReference {
    pub file: String,
    pub symbol: String,
    pub byte_start: usize,
    pub byte_end: usize,
    pub line: usize,
}
```

**Usage:**

```json
{
  "span": { /* ... */ },
  "relationships": {
    "callers": [
      {"file": "src/a.rs", "symbol": "main", "byte_start": 120, "byte_end": 124, "line": 8}
    ],
    "callees": [
      {"file": "src/b.rs", "symbol": "helper", "byte_start": 50, "byte_end": 56, "line": 3},
      {"file": "vendor/lib.rs", "symbol": "parse", "byte_start": 200, "byte_end": 205, "line": 15}
    ],
    "imports": [
      {"file": "src/lib.rs", "symbol": "use crate::helper", "byte_start": 5, "byte_end": 20, "line": 1}
    ],
    "exports": []
  }
}
```

**Why this matters:**
- LLM can analyze impact before patching
- Removes need for separate Magellan queries
- Enables safe bulk refactors
- Helps Splice verify patch safety
- Foundation of blast radius analysis

### 4. Checksums (Race Condition Protection)

**Purpose:** Verify file/symbol hasn't changed before applying patches.

```rust
pub struct SpanChecksums {
    /// SHA-256 of span content before patch
    #[serde(skip_serializing_if = "Option::is_none")]
    pub checksum_before: Option<String>,

    /// SHA-256 of span content after patch (for verification)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub checksum_after: Option<String>,

    /// SHA-256 of entire file before operation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_checksum_before: Option<String>,
}
```

**Usage:**

```json
{
  "span": { /* ... */ },
  "checksums": {
    "checksum_before": "sha256:a1b2c3d4e5f6...",
    "file_checksum_before": "sha256:fedcba987654..."
  }
}
```

**Splice validation flow:**

1. Read file, compute SHA-256
2. Compare with `file_checksum_before`
3. If mismatch: reject patch (file changed)
4. Read span, compute SHA-256
5. Compare with `checksum_before`
6. If mismatch: reject span (symbol shifted)
7. Apply patch
8. Compute `checksum_after` for verification

**Why this matters:**
- Prevents applying outdated patches
- Avoids Magellan race conditions
- Prevents partial patch corruption
- Same pattern used by Google/Meta for automated refactoring

### 5. Suggested Action (Future-Proofing)

**Purpose:** Enable intelligent batching and self-repair.

```rust
pub struct SuggestedAction {
    /// Action type: "rename", "delete", "extract", "inline", etc.
    pub action_type: String,

    /// Action parameters (varies by type)
    pub params: serde_json::Value,
}
```

**Usage:**

```json
{
  "span": { /* ... */ },
  "suggested_action": {
    "action_type": "rename",
    "params": {
      "from": "foo",
      "to": "bar"
    }
  }
}
```

**Why this matters (future):**
- Automatic merge of multi-step refactors
- Intelligent batching of operations
- LLM self-repair suggestions
- Opportunistic optimizations

### 6. Tool Hints (Behavior Guidance)

**Purpose:** Let tools coordinate behavior automatically.

```rust
pub struct ToolHints {
    /// Whether this span requires full file context for safe patching
    #[serde(skip_serializing_if = "Option::is_none")]
    pub requires_full_context: Option<bool>,

    /// Whether this operation must be atomic (all-or-nothing)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub apply_atomically: Option<bool>,

    /// Search sensitivity
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_case_sensitive: Option<bool>,

    /// Language-specific hints
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language_hints: Option<serde_json::Value>,
}
```

**Usage:**

```json
{
  "span": { /* ... */ },
  "tool_hints": {
    "requires_full_context": false,
    "apply_atomically": true,
    "search_case_sensitive": true,
    "language_hints": {
      "rust_macro_expansion": true
    }
  }
}
```

**Why this matters:**
- Rust macros require full context
- Some patches must be atomic (no partial application)
- Tools can adapt automatically
- Turns schema into shared contract

### 7. Unified Error Codes

**Purpose:** Machine-readable error codes for automated repair.

**Error code format:** `{TOOL}-{CATEGORY}-{NUMBER}`

| Tool | Prefix | Examples |
|------|--------|----------|
| Magellan | `MAG` | `MAG-REF-001`, `MAG-QRY-002` |
| Splice | `SPL` | `SPL-E001`, `SPL-V002` |
| llmsearch | `LMS` | `LMS-IO-001`, `LMS-QRY-002` |
| llmastsearch | `LMA` | `LMA-AST-001`, `LMA-QRY-002` |
| llmtransform | `LMT` | `LMT-IO-001`, `LMT-CSUM-002` |

**Usage:**

```json
{
  "diagnostics": [
    {
      "code": "MAG-REF-001",
      "tool": "magellan",
      "severity": "error",
      "message": "Symbol not found",
      "span": null,
      "remediation": "Check spelling or use 'magellan find' to search"
    }
  ]
}
```

**Error categories:**

| Category | Suffix | Examples |
|----------|--------|----------|
| IO | `-IO-` | File not found, permission denied |
| QUERY | `-QRY-` | Invalid query, parse error |
| REF | `-REF-` | Symbol not found, undefined reference |
| VALIDATION | `-V-` | Checksum mismatch, span invalid |
| AST | `-AST-` | Parse error, invalid syntax |

**Why this matters:**
- Automatic repair strategies
- LLM retry logic without hallucination
- Splice ↔ Magellan ↔ LLM integrated debugging
- Real agent workflows

---

## Complete Rich Span Example

Combining all optional fields:

```json
{
  "schema_version": "1.1.0",
  "execution_id": "550e8400-e29b-41d4-a716-446655440000",
  "tool": "magellan",
  "timestamp": "2026-01-22T10:30:00Z",
  "data": {
    "matches": [{
      "match_id": "bbb22222-3333-4444-5555-666666666666",
      "span": {
        "span_id": "a1b2c3d4e5f6g7h8",
        "file_path": "src/lib.rs",
        "byte_start": 100,
        "byte_end": 200,
        "start_line": 10,
        "start_col": 4,
        "end_line": 15,
        "end_col": 2,
        "context": {
          "before": ["/// Process input data", "use std::collections;", ""],
          "selected": ["fn process(data: &str) -> Result<String> {", "    data.trim().to_string()", "}"],
          "after": ["", "fn main() {", "    process(\"hello\").unwrap();", "}"]
        },
        "semantic_kind": "function",
        "language": "rust",
        "relationships": {
          "callers": [
            {"file": "src/main.rs", "symbol": "main", "line": 5}
          ],
          "callees": [
            {"file": "src/lib.rs", "symbol": "trim", "line": 11},
            {"file": "src/lib.rs", "symbol": "to_string", "line": 11}
          ]
        },
        "checksums": {
          "checksum_before": "sha256:abc123...",
          "file_checksum_before": "sha256:def456..."
        },
        "tool_hints": {
          "requires_full_context": false,
          "apply_atomically": true
        }
      },
      "name": "process",
      "kind": "function"
    }],
    "count": 1
  },
  "diagnostics": []
}
```

---

## Tool-Specific Variants

### 1. Magellan (Symbol Query)

**Current state:** Reference implementation

**Response format:**

```json
{
  "schema_version": "1.0.0",
  "execution_id": "...",
  "data": {
    "matches": [
      {
        "match_id": "...",
        "span": { /* Span */ },
        "name": "function_name",
        "kind": "function",
        "parent": "module_name",
        "symbol_id": "stable-symbol-id"
      }
    ],
    "count": 1
  },
  "partial": false
}
```

### 2. Splice (Refactoring)

**Changes needed:**
1. Rename `operation_id` → `execution_id`
2. Rename `version` → `schema_version`
3. Adopt Magellan's `start_line`/`start_col` convention
4. Use SHA-256 span_id instead of UUID

**Updated response format:**

```json
{
  "schema_version": "2.0.0",
  "execution_id": "...",
  "operation_type": "patch",
  "status": "ok",
  "message": "Successfully patched 1 symbol in 1 file",
  "timestamp": "2026-01-22T10:00:00Z",
  "result": {
    "type": "patch",
    "file": "src/lib.rs",
    "symbol": "function_name",
    "kind": "function",
    "spans": [
      {
        "span_id": "...",
        "file_path": "src/lib.rs",
        "byte_start": 100,
        "byte_end": 200,
        "start_line": 5,
        "start_col": 4,
        "end_line": 10,
        "end_col": 2,
        "before_hash": "abc123",
        "after_hash": "def456"
      }
    ],
    "lines_added": 5,
    "lines_removed": 3
  }
}
```

### 3. llmsearch (Text Search)

**Changes needed:**
1. Add `JsonResponse` wrapper
2. Rename `file` → `file_path`
3. Rename `line_number` → `start_line`
4. Rename `column_number` → `start_col`
5. Add `Span` structure
6. Add `match_id` (UUID v4)

**Updated response format:**

```json
{
  "schema_version": "1.0.0",
  "execution_id": "...",
  "data": {
    "pattern": "search_pattern",
    "matches": [
      {
        "match_id": "...",
        "span": {
          "span_id": "...",
          "file_path": "src/main.rs",
          "byte_start": 100,
          "byte_end": 116,
          "start_line": 5,
          "start_col": 0,
          "end_line": 5,
          "end_col": 16
        },
        "matched_text": "search_pattern",
        "context_before": ["line before"],
        "context_after": ["line after"]
      }
    ],
    "match_count": 1
  },
  "partial": false
}
```

### 4. llmastsearch (AST Query)

**Changes needed:**
1. Add `JsonResponse` wrapper
2. Flatten `Position` structure into `Span`
3. Add `match_id` (UUID v4)
4. Convert `PathBuf` → `String`
5. Convert `Uuid` → `String` for execution_id
6. Add `Span` structure

**Updated response format:**

```json
{
  "schema_version": "1.0.0",
  "execution_id": "...",
  "data": {
    "file_path": "src/main.rs",
    "language": "Rust",
    "query": "(function_item) @func",
    "matches": [
      {
        "match_id": "...",
        "span": {
          "span_id": "...",
          "file_path": "src/main.rs",
          "byte_start": 0,
          "byte_end": 50,
          "start_line": 1,
          "start_col": 0,
          "end_line": 5,
          "end_col": 1
        },
        "pattern_index": 0,
        "captures": [
          {
            "name": "func",
            "byte_start": 3,
            "byte_end": 7,
            "content": "main"
          }
        ]
      }
    ],
    "match_count": 1
  },
  "partial": false
}
```

### 5. llmtransform (Text Mutation)

**Changes needed:**
1. Add `OperationResult` wrapper
2. Add `Span` to each edit result
3. Add checksums for validation

**Updated response format:**

```json
{
  "schema_version": "1.0.0",
  "execution_id": "...",
  "operation_type": "edit",
  "status": "ok",
  "message": "Successfully applied 2 edits",
  "timestamp": "2026-01-22T10:00:00Z",
  "result": {
    "type": "edit",
    "file_path": "src/main.rs",
    "final_checksum": "def456...",
    "total_byte_shift": 12,
    "applied_count": 2,
    "skipped_count": 0,
    "error_count": 0,
    "edits": [
      {
        "match_id": "...",
        "span": {
          "span_id": "...",
          "file_path": "src/main.rs",
          "byte_start": 10,
          "byte_end": 20,
          "start_line": 1,
          "start_col": 10,
          "end_line": 1,
          "end_col": 20
        },
        "status": "applied",
        "before_checksum": "abc123",
        "after_checksum": "def456"
      }
    ]
  }
}
```

### 6. llmfilewrite (Code Creation)

**Design (new tool):**

```json
{
  "schema_version": "1.0.0",
  "execution_id": "...",
  "operation_type": "create",
  "status": "approved",
  "message": "File created and validated",
  "timestamp": "2026-01-22T10:00:00Z",
  "result": {
    "type": "create",
    "path": "src/main.rs",
    "language": "Rust",
    "lsp_used": "rust-analyzer",
    "checksum": "abc123...",
    "diagnostics": []
  }
}
```

**With diagnostics:**

```json
{
  "status": "rejected",
  "message": "LSP validation failed",
  "result": {
    "path": "src/main.rs",
    "diagnostics": [
      {
        "level": "error",
        "message": "expected identifier, found `;`",
        "file": "src/main.rs",
        "start_line": 5,
        "start_col": 10,
        "end_line": 5,
        "end_col": 11,
        "code": "E0382"
      }
    ]
  }
}
```

---

## Error Diagnostics

### Unified Error Structure

**Splice's `DiagnosticPayload` (enhanced and adopted):**

```rust
/// Standard diagnostic payload for all tools
pub struct DiagnosticPayload {
    /// Tool that generated this diagnostic
    pub tool: String,

    /// Severity level
    pub level: DiagnosticLevel,

    /// Primary message
    pub message: String,

    /// File path (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<String>,

    /// Span information (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub span: Option<Span>,

    /// Stable error code
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,

    /// Additional context or notes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,

    /// Suggested remediation or fix
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remediation: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DiagnosticLevel {
    #[serde(rename = "error")]
    Error,

    #[serde(rename = "warning")]
    Warning,

    #[serde(rename = "note")]
    Note,
}
```

**Error response format:**

```json
{
  "schema_version": "1.0.0",
  "execution_id": "...",
  "operation_type": "patch",
  "status": "error",
  "message": "Operation failed",
  "timestamp": "2026-01-22T10:00:00Z",
  "error": {
    "tool": "splice",
    "level": "error",
    "message": "Symbol not found: 'nonexistent_function'",
    "file": "src/main.rs",
    "span": {
      "span_id": "...",
      "file_path": "src/main.rs",
      "byte_start": 100,
      "byte_end": 120,
      "start_line": 10,
      "start_col": 0,
      "end_line": 10,
      "end_col": 20
    },
    "code": "SPLICE_E001",
    "note": "The symbol could not be resolved in the code graph",
    "remediation": "Use 'splice query' to list available symbols"
  }
}
```

---

## Implementation Guide

### Shared Types Crate

Create a new crate at `/home/feanor/Projects/llm-types/`:

```
llm-types/
├── Cargo.toml
└── src/
    ├── lib.rs
    ├── span.rs          // Span, SpanCoordinates
    ├── match.rs         // Match, MatchResult
    ├── response.rs      // JsonResponse, OperationResult
    ├── diagnostic.rs    // DiagnosticPayload
    └── error.rs         // Error types
```

**Cargo.toml:**

```toml
[package]
name = "llm-types"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.10"
uuid = { version = "1.0", features = ["serde", "v4"] }
chrono = { version = "0.4", features = ["serde"] }
```

### Migration Steps per Tool

**For each tool (llmsearch, llmastsearch, llmtransform, Splice):**

1. Add dependency:
   ```toml
   [dependencies]
   llm-types = { path = "../llm-types" }
   ```

2. Update output types:
   ```rust
   // Old
   pub struct Match {
       pub file: String,
       pub line_number: usize,
       ...
   }

   // New
   use llm_types::{JsonResponse, Match, Span};

   pub type JsonResponse<T> = llm_types::JsonResponse<T>;
   ```

3. Update field names (compatibility layer):
   ```rust
   // For backward compatibility during transition
   #[serde(alias = "file")]
   pub file_path: String,

   #[serde(alias = "line_number")]
   pub start_line: usize,
   ```

4. Update tests to use new format

5. Update documentation

**For Magellan:**
- Already reference implementation
- Export types to `llm-types` crate
- Add compatibility shims if needed

**For llmfilewrite:**
- Implement from scratch using `llm-types`

---

## Migration Path

### Phase 1: Create Shared Types (Week 1)

1. Create `llm-types` crate
2. Add all core types from Magellan/Splice
3. Publish to local registry or git dependency

### Phase 2: Update Simple Tools (Week 2)

1. **llmsearch** - Simplest, good starting point
2. Add `llm-types` dependency
3. Replace internal types
4. Add compatibility layer
5. Update tests

### Phase 3: Update Complex Tools (Weeks 3-4)

1. **llmastsearch** - Medium complexity
2. **llmtransform** - Medium complexity
3. Follow same process as llmsearch

### Phase 4: Update Splice (Week 5)

1. **Splice** - Highest complexity
2. Rename `operation_id` → `execution_id`
3. Rename `version` → `schema_version`
4. Adopt Magellan span format
5. Extensive testing required

### Phase 5: Deprecation (Week 8+)

1. Announce old format deprecation
2. Add migration warnings
3. Remove compatibility layers after 3 months

---

## Summary of Changes

| Tool | Key Changes | Priority |
|------|-------------|----------|
| Magellan | Export types to shared crate | Low (reference impl) |
| Splice | Rename fields, adopt Magellan span format | High |
| llmsearch | Add wrapper, flatten Position, rename fields | Medium |
| llmastsearch | Add wrapper, flatten Position, add match_id | Medium |
| llmtransform | Add wrapper, add Span to edits | Medium |
| llmfilewrite | Implement from scratch using shared types | High |

---

## Field Name Reference

### Standardized Field Names

| Concept | Standard | Aliases (for transition) |
|---------|----------|--------------------------|
| Operation tracking | `execution_id` | `operation_id` (Splice) |
| Schema version | `schema_version` | `version` (Splice) |
| File path | `file_path` | `file` (llmsearch) |
| Line start | `start_line` | `line_start` (Splice), `line_number` (llmsearch), `start.row` (llmastsearch) |
| Line end | `end_line` | `line_end` (Splice), `end.row` (llmastsearch) |
| Column start | `start_col` | `col_start` (Splice), `column_number` (llmsearch), `start.column` (llmastsearch) |
| Column end | `end_col` | `col_end` (Splice), `end.column` (llmastsearch) |

---

## Examples

### Complete Workflow Example

**1. Query symbol (Magellan):**

```bash
magellan find --name "function_name" --db code.db
```

```json
{
  "schema_version": "1.0.0",
  "execution_id": "aaa11111-2222-3333-4444-555555555555",
  "data": {
    "matches": [{
      "match_id": "bbb22222-3333-4444-5555-666666666666",
      "span": {
        "span_id": "a1b2c3d4e5f6g7h8",
        "file_path": "src/lib.rs",
        "byte_start": 100,
        "byte_end": 200,
        "start_line": 10,
        "start_col": 4,
        "end_line": 15,
        "end_col": 2
      },
      "name": "function_name",
      "kind": "function"
    }],
    "count": 1
  }
}
```

**2. Search text (llmsearch):**

```bash
llmsearch --pattern "TODO" --glob "**/*.rs"
```

```json
{
  "schema_version": "1.0.0",
  "execution_id": "ccc33333-4444-5555-6666-777777777777",
  "data": {
    "pattern": "TODO",
    "matches": [{
      "match_id": "ddd44444-5555-6666-7777-888888888888",
      "span": {
        "span_id": "i1j2k3l4m5n6o7p8",
        "file_path": "src/lib.rs",
        "byte_start": 250,
        "byte_end": 254,
        "start_line": 20,
        "start_col": 8,
        "end_line": 20,
        "end_col": 12
      },
      "matched_text": "TODO",
      "context_before": ["// Fix this later"],
      "context_after": ["let x = 1;"]
    }],
    "match_count": 1
  }
}
```

**3. Patch function (Splice):**

```bash
splice patch --file src/lib.rs --symbol function_name --with new_impl.rs
```

```json
{
  "schema_version": "2.0.0",
  "execution_id": "eee55555-6666-7777-8888-999999999999",
  "operation_type": "patch",
  "status": "ok",
  "message": "Successfully patched 1 symbol in 1 file",
  "timestamp": "2026-01-22T10:00:00Z",
  "result": {
    "type": "patch",
    "file": "src/lib.rs",
    "symbol": "function_name",
    "kind": "function",
    "spans": [{
      "span_id": "a1b2c3d4e5f6g7h8",
      "file_path": "src/lib.rs",
      "byte_start": 100,
      "byte_end": 200,
      "start_line": 10,
      "start_col": 4,
      "end_line": 15,
      "end_col": 2,
      "before_hash": "abc123...",
      "after_hash": "def456..."
    }],
    "lines_added": 5,
    "lines_removed": 3
  }
}
```

**4. Query AST (llmastsearch):**

```bash
llmastsearch --query "(function_item name: (identifier) @name)" --glob "**/*.rs"
```

```json
{
  "schema_version": "1.0.0",
  "execution_id": "fff66666-7777-8888-9999-000000000000",
  "data": {
    "file_path": "src/main.rs",
    "language": "Rust",
    "query": "(function_item name: (identifier) @name)",
    "matches": [{
      "match_id": "00077777-8888-9999-0000-111111111111",
      "span": {
        "span_id": "q1r2s3t4u5v6w7x8",
        "file_path": "src/main.rs",
        "byte_start": 100,
        "byte_end": 200,
        "start_line": 10,
        "start_col": 0,
        "end_line": 15,
        "end_col": 1
      },
      "pattern_index": 0,
      "captures": [{
        "name": "name",
        "byte_start": 103,
        "byte_end": 116,
        "content": "function_name"
      }]
    }],
    "match_count": 1
  }
}
```

---

## CLI Flags for Optional Features

All rich span extensions are **opt-in** via CLI flags. This keeps the default output minimal while allowing tools to request additional data when needed.

### Context Flags

| Flag | Short | Purpose | Default |
|------|-------|---------|---------|
| `--with-context` | `-C` | Include context (before/after/selected) | Off |
| `--context-lines <n>` | | Number of context lines (default: 3) | 3 |
| `--no-context` | | Disable context even if requested | - |

**Examples:**

```bash
# Magellan: Get symbol with 5 lines of context
magellan find --name "process" --with-context --context-lines 5

# Splice: Get symbol with context for safer patching
splice get --symbol "process" --with-context

# llmsearch: Search with context
llmsearch --pattern "TODO" --with-context --context-lines 2
```

### Relationship Flags

| Flag | Short | Purpose | Default |
|------|-------|---------|---------|
| `--with-callers` | | Include callers in relationships | Off |
| `--with-callees` | | Include callees in relationships | Off |
| `--with-imports` | | Include imports in relationships | Off |
| `with-exports` | | Include exports in relationships | Off |
| `--with-all-relationships` | `-R` | Include all relationship types | Off |

**Examples:**

```bash
# Get symbol with full call graph
magellan find --name "process" --with-all-relationships

# Get impact analysis before patching
splice delete --symbol "helper" --with-callers --with-callees
```

### Semantic Flags

| Flag | Short | Purpose | Default |
|------|-------|---------|---------|
| `--with-semantics` | `-S` | Include semantic_kind and language | Off |
| `--no-semantics` | | Explicitly exclude semantics | - |

**Examples:**

```bash
# Get symbol with semantic info
llmastsearch --query "(function_item) @func" --with-semantics
```

### Checksum Flags

| Flag | Short | Purpose | Default |
|------|-------|---------|---------|
| `--with-checksums` | | Include checksums for validation | Off |
| `verify-checksums` | | Verify checksums before applying (Splice only) | Off |

**Examples:**

```bash
# Get spans with checksums for safe patching
magellan find --name "foo" --with-checksums

# Apply patch with checksum verification
splice patch --file src/lib.rs --span "...span_id..." --verify-checksums
```

### Tool Hint Flags

| Flag | Purpose | Default |
|------|---------|---------|
| `--atomic` | Require atomic operation | false |
| `--full-context` | Require full file context for patches | false |

**Examples:**

```bash
# Atomic operation (all or nothing)
splice patch --plan plan.json --atomic

# Full context for macros
splice patch --file src/lib.rs --full-context
```

### Error Code Flags

| Flag | Short | Purpose | Default |
|------|-------|---------|---------|
| `--error-codes` | `-E` | Include machine-readable error codes | Off |
| `explain <code>` | | Show detailed explanation for error code | - |

**Examples:**

```bash
# Query with error codes
magellan query --labels rust fn --error-codes

# Explain an error
splice explain SPL-E001
```

### Combined Flag Examples

```bash
# Rich span for LLM consumption (everything enabled)
magellan find --name "process" \
  --with-context --context-lines 5 \
  --with-all-relationships \
  --with-semantics \
  --with-checksums \
  --error-codes

# Safe patching workflow
splice patch --file src/lib.rs --symbol "process" \
  --with-context \
  --with-callers \
  --with-checksums \
  --verify-checksums \
  --atomic

# Impact analysis before deletion
splice delete --symbol "helper" \
  --with-callers \
  --with-callees \
  --error-codes
```

---

## Extension Summary

| Extension | Flag(s) | Adds to Output | Use Case |
|-----------|---------|---------------|----------|
| **Context** | `--with-context`, `-C` | `context.before`, `context.selected`, `context.after` | LLM context, safer patches |
| **Relationships** | `--with-callers`, `--with-callees`, `-R` | `relationships.*` | Impact analysis, blast radius |
| **Semantics** | `--with-semantics`, `-S` | `semantic_kind`, `language` | Smart transforms, multi-language |
| **Checksums** | `--with-checksums`, `--verify-checksums` | `checksums.*` | Race protection, validation |
| **Tool Hints** | `--atomic`, `--full-context` | `tool_hints.*` | Coordination, safety |
| **Error Codes** | `--error-codes`, `-E` | `diagnostics[].code` | Automated repair, debugging |

**All extensions are:**
- **Optional** - Default output remains minimal
- **Opt-in** - Only included when explicitly requested
- **Backward compatible** - Old parsers continue to work
- **Composable** - Flags can be combined

---

*Document created: 2026-01-22*
*Last updated: 2026-01-22*
*Status: Ready for review and implementation*