@@ -38,21 +38,27 @@ extern char *_custom_strdup(const char *str) {
38
38
}
39
39
40
40
// ==================================================================
41
- // AI magic tricks
41
+ // TI Reasoning Metadata (lightweight struct for audit/debug)
42
42
// ==================================================================
43
+ typedef struct {
44
+ const char * input ;
45
+ const char * suggested ;
46
+ int edit_distance ;
47
+ float confidence_score ; // 0.0 to 1.0
48
+ const char * reason ;
49
+ } fossil_ti_reason_t ;
43
50
44
- // Function to calculate Levenshtein Distance
51
+ // ==================================================================
52
+ // Levenshtein Distance (Unchanged Core)
53
+ // ==================================================================
45
54
int levenshtein_distance (const char * s1 , const char * s2 ) {
46
55
int len1 = strlen (s1 ), len2 = strlen (s2 );
47
56
int i , j ;
48
-
49
- // Allocate the dp array dynamically
50
57
int * * dp = (int * * )malloc ((len1 + 1 ) * sizeof (int * ));
51
58
if (!dp ) return INT_MAX ;
52
59
for (i = 0 ; i <= len1 ; i ++ ) {
53
60
dp [i ] = (int * )malloc ((len2 + 1 ) * sizeof (int ));
54
61
if (!dp [i ]) {
55
- // Free previously allocated rows
56
62
for (j = 0 ; j < i ; j ++ ) free (dp [j ]);
57
63
free (dp );
58
64
return INT_MAX ;
@@ -73,27 +79,47 @@ int levenshtein_distance(const char *s1, const char *s2) {
73
79
dp [i ][j ] = min ;
74
80
}
75
81
}
82
+
76
83
int result = dp [len1 ][len2 ];
77
84
for (i = 0 ; i <= len1 ; i ++ ) free (dp [i ]);
78
85
free (dp );
79
86
return result ;
80
87
}
81
88
82
- // Function to find the closest command match
83
- const char * suggest_command (const char * input , fossil_io_parser_palette_t * palette ) {
89
+ // ==================================================================
90
+ // TI-Aware Command Suggestion (with traceable reasoning)
91
+ // ==================================================================
92
+ const char * suggest_command_ti (const char * input , fossil_io_parser_palette_t * palette , fossil_ti_reason_t * out_reason ) {
84
93
fossil_io_parser_command_t * current = palette -> commands ;
85
94
const char * best_match = NULL ;
86
95
int min_distance = INT_MAX ;
96
+ int best_length = 1 ; // Avoid divide-by-zero
87
97
88
98
while (current ) {
89
99
int distance = levenshtein_distance (input , current -> name );
90
100
if (distance < min_distance ) {
91
101
min_distance = distance ;
92
102
best_match = current -> name ;
103
+ best_length = strlen (current -> name );
93
104
}
94
105
current = current -> next ;
95
106
}
96
- return (min_distance <= 3 ) ? best_match : NULL ; // Suggest only if close enough
107
+
108
+ // Compute confidence score based on edit distance
109
+ float confidence = 1.0f - ((float )min_distance / (float )best_length );
110
+ confidence = (confidence < 0.0f ) ? 0.0f : (confidence > 1.0f ) ? 1.0f : confidence ;
111
+
112
+ // Optional reasoning trace
113
+ if (out_reason ) {
114
+ out_reason -> input = input ;
115
+ out_reason -> suggested = best_match ;
116
+ out_reason -> edit_distance = min_distance ;
117
+ out_reason -> confidence_score = confidence ;
118
+ out_reason -> reason = (confidence >= 0.7f ) ? "Close semantic match" : "Low confidence match" ;
119
+ }
120
+
121
+ // Only return suggestions above a minimum confidence threshold
122
+ return (confidence >= 0.7f ) ? best_match : NULL ;
97
123
}
98
124
99
125
// ==================================================================
@@ -443,12 +469,25 @@ void fossil_io_parser_parse(fossil_io_parser_palette_t *palette, int argc, char
443
469
}
444
470
445
471
if (!command ) {
446
- // Suggest a similar command or show an error
447
- const char * suggestion = suggest_command (command_name , palette );
472
+ // Use TI-aware suggestion system
473
+ fossil_ti_reason_t ti_reason = {0 };
474
+ const char * suggestion = suggest_command_ti (command_name , palette , & ti_reason );
475
+
448
476
if (suggestion ) {
449
- fossil_io_fprintf (FOSSIL_STDERR , "{red}Unknown command: '%s'. Did you mean '%s'?{reset}\n" , command_name , suggestion );
477
+ fossil_io_fprintf (FOSSIL_STDERR ,
478
+ "{red}Unknown command: '%s'. Did you mean '%s'?{reset}\n"
479
+ "{yellow}[TI] Suggestion confidence: %.2f | Distance: %d | Reason: %s{reset}\n" ,
480
+ command_name ,
481
+ suggestion ,
482
+ ti_reason .confidence_score ,
483
+ ti_reason .edit_distance ,
484
+ ti_reason .reason
485
+ );
450
486
} else {
451
- fossil_io_fprintf (FOSSIL_STDERR , "{red}Unknown command: '%s'. Type '--help' to see available commands.{reset}\n" , command_name );
487
+ fossil_io_fprintf (FOSSIL_STDERR ,
488
+ "{red}Unknown command: '%s'. Type '--help' to see available commands.{reset}\n" ,
489
+ command_name
490
+ );
452
491
}
453
492
return ;
454
493
}
0 commit comments