|
| 1 | +-- SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +||| CodeseekerMcp.SearchGraph: Formally verified code intelligence operations. |
| 4 | +||| |
| 5 | +||| Cartridge: codeseeker-mcp |
| 6 | +||| Matrix cell: Code Intelligence domain x {MCP, REST} protocols |
| 7 | +||| |
| 8 | +||| This module defines type-safe interfaces for CodeSeeker's local code |
| 9 | +||| intelligence capabilities: |
| 10 | +||| - Hybrid search (vector + text + path, fused via Reciprocal Rank Fusion) |
| 11 | +||| - Knowledge graph traversal (imports, calls, extends, implements) |
| 12 | +||| - Auto-detected pattern retrieval |
| 13 | +||| - Graph RAG context retrieval |
| 14 | +||| |
| 15 | +||| State machine prevents: |
| 16 | +||| - Searches before a codebase is indexed |
| 17 | +||| - Graph traversal on uninitialised indices |
| 18 | +||| - Concurrent index operations on the same path |
| 19 | +||| |
| 20 | +||| CodeSeeker stores all data locally in .codeseeker/ — no external services. |
| 21 | +module CodeseekerMcp.SearchGraph |
| 22 | + |
| 23 | +import Data.List |
| 24 | + |
| 25 | +%default total |
| 26 | + |
| 27 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 28 | +-- Indexer State Machine |
| 29 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 30 | + |
| 31 | +||| Lifecycle states for a CodeSeeker index session. |
| 32 | +||| Uninitialised: No index loaded for this path. |
| 33 | +||| Indexing: Index is being built/updated (blocks queries). |
| 34 | +||| Ready: Index is available for search and graph traversal. |
| 35 | +||| Querying: A search or graph traversal is in flight. |
| 36 | +||| IndexError: The indexer encountered an unrecoverable error. |
| 37 | +public export |
| 38 | +data IndexState |
| 39 | + = Uninitialised |
| 40 | + | Indexing |
| 41 | + | Ready |
| 42 | + | Querying |
| 43 | + | IndexError |
| 44 | + |
| 45 | +||| Equality for index states. |
| 46 | +public export |
| 47 | +Eq IndexState where |
| 48 | + Uninitialised == Uninitialised = True |
| 49 | + Indexing == Indexing = True |
| 50 | + Ready == Ready = True |
| 51 | + Querying == Querying = True |
| 52 | + IndexError == IndexError = True |
| 53 | + _ == _ = False |
| 54 | + |
| 55 | +||| Valid state transitions (enforced at the type level). |
| 56 | +||| Uninitialised -> Indexing : start an index build |
| 57 | +||| Indexing -> Ready : index build succeeded |
| 58 | +||| Indexing -> IndexError: index build failed |
| 59 | +||| Ready -> Querying : begin a search or traversal |
| 60 | +||| Querying -> Ready : query completed |
| 61 | +||| Querying -> IndexError: query failed |
| 62 | +||| IndexError -> Uninitialised : reset to try again |
| 63 | +public export |
| 64 | +data ValidIndexTransition : IndexState -> IndexState -> Type where |
| 65 | + StartIndex : ValidIndexTransition Uninitialised Indexing |
| 66 | + IndexComplete : ValidIndexTransition Indexing Ready |
| 67 | + IndexFail : ValidIndexTransition Indexing IndexError |
| 68 | + BeginQuery : ValidIndexTransition Ready Querying |
| 69 | + QueryDone : ValidIndexTransition Querying Ready |
| 70 | + QueryFail : ValidIndexTransition Querying IndexError |
| 71 | + ResetError : ValidIndexTransition IndexError Uninitialised |
| 72 | + |
| 73 | +||| Runtime transition validator. |
| 74 | +public export |
| 75 | +canIndexTransition : IndexState -> IndexState -> Bool |
| 76 | +canIndexTransition Uninitialised Indexing = True |
| 77 | +canIndexTransition Indexing Ready = True |
| 78 | +canIndexTransition Indexing IndexError = True |
| 79 | +canIndexTransition Ready Querying = True |
| 80 | +canIndexTransition Querying Ready = True |
| 81 | +canIndexTransition Querying IndexError = True |
| 82 | +canIndexTransition IndexError Uninitialised = True |
| 83 | +canIndexTransition _ _ = False |
| 84 | + |
| 85 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 86 | +-- Search Mode |
| 87 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 88 | + |
| 89 | +||| Search strategies available in CodeSeeker. |
| 90 | +||| Vector: Semantic similarity via embeddings. |
| 91 | +||| Text: Literal/regex text matching. |
| 92 | +||| Path: File path pattern matching. |
| 93 | +||| Hybrid: All three fused with Reciprocal Rank Fusion. |
| 94 | +public export |
| 95 | +data SearchMode |
| 96 | + = Vector -- Semantic embedding similarity |
| 97 | + | Text -- Literal / regex text search |
| 98 | + | Path -- File path pattern match |
| 99 | + | Hybrid -- RRF fusion of all three |
| 100 | + |
| 101 | +||| C-ABI encoding for search modes. |
| 102 | +public export |
| 103 | +searchModeToInt : SearchMode -> Int |
| 104 | +searchModeToInt Vector = 1 |
| 105 | +searchModeToInt Text = 2 |
| 106 | +searchModeToInt Path = 3 |
| 107 | +searchModeToInt Hybrid = 4 |
| 108 | + |
| 109 | +||| C-ABI decoding for search modes. |
| 110 | +public export |
| 111 | +intToSearchMode : Int -> Maybe SearchMode |
| 112 | +intToSearchMode 1 = Just Vector |
| 113 | +intToSearchMode 2 = Just Text |
| 114 | +intToSearchMode 3 = Just Path |
| 115 | +intToSearchMode 4 = Just Hybrid |
| 116 | +intToSearchMode _ = Nothing |
| 117 | + |
| 118 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 119 | +-- Knowledge Graph Relationship Types |
| 120 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 121 | + |
| 122 | +||| Edge types in CodeSeeker's knowledge graph. |
| 123 | +||| These correspond to structural relationships in source code. |
| 124 | +public export |
| 125 | +data GraphRelation |
| 126 | + = Imports -- Module/file imports another |
| 127 | + | Calls -- Function/method calls another |
| 128 | + | Extends -- Class extends a base class |
| 129 | + | Implements -- Class implements an interface |
| 130 | + | Uses -- Generic usage / reference relationship |
| 131 | + |
| 132 | +||| C-ABI encoding for graph relations. |
| 133 | +public export |
| 134 | +relationToInt : GraphRelation -> Int |
| 135 | +relationToInt Imports = 1 |
| 136 | +relationToInt Calls = 2 |
| 137 | +relationToInt Extends = 3 |
| 138 | +relationToInt Implements = 4 |
| 139 | +relationToInt Uses = 5 |
| 140 | + |
| 141 | +||| C-ABI decoding for graph relations. |
| 142 | +public export |
| 143 | +intToRelation : Int -> Maybe GraphRelation |
| 144 | +intToRelation 1 = Just Imports |
| 145 | +intToRelation 2 = Just Calls |
| 146 | +intToRelation 3 = Just Extends |
| 147 | +intToRelation 4 = Just Implements |
| 148 | +intToRelation 5 = Just Uses |
| 149 | +intToRelation _ = Nothing |
| 150 | + |
| 151 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 152 | +-- Index Session Record |
| 153 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 154 | + |
| 155 | +||| An active CodeSeeker index session for a codebase path. |
| 156 | +public export |
| 157 | +record IndexSession where |
| 158 | + constructor MkIndexSession |
| 159 | + sessionId : String -- Unique session identifier |
| 160 | + codebasePath : String -- Absolute path to the indexed codebase |
| 161 | + state : IndexState |
| 162 | + fileCount : Nat -- Number of files in the index (0 if not Ready) |
| 163 | + |
| 164 | +||| Proof that an index session is ready for querying. |
| 165 | +public export |
| 166 | +data IsReady : IndexSession -> Type where |
| 167 | + IndexReady : (s : IndexSession) -> |
| 168 | + (state s = Ready) -> |
| 169 | + IsReady s |
| 170 | + |
| 171 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 172 | +-- MCP Tool Definitions |
| 173 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 174 | + |
| 175 | +||| MCP tools exposed by the codeseeker-mcp cartridge. |
| 176 | +public export |
| 177 | +data McpTool |
| 178 | + = ToolIndex -- Index a codebase at a given path |
| 179 | + | ToolSearch -- Hybrid search (vector + text + path) |
| 180 | + | ToolTraverse -- Traverse knowledge graph from a symbol |
| 181 | + | ToolPatterns -- Retrieve auto-detected coding patterns |
| 182 | + | ToolGraphRag -- Graph RAG context for a query |
| 183 | + | ToolStatus -- Session and index status |
| 184 | + |
| 185 | +||| MCP tool name (for JSON-RPC method name). |
| 186 | +public export |
| 187 | +toolName : McpTool -> String |
| 188 | +toolName ToolIndex = "codeseeker/index" |
| 189 | +toolName ToolSearch = "codeseeker/search" |
| 190 | +toolName ToolTraverse = "codeseeker/traverse" |
| 191 | +toolName ToolPatterns = "codeseeker/patterns" |
| 192 | +toolName ToolGraphRag = "codeseeker/graph-rag" |
| 193 | +toolName ToolStatus = "codeseeker/status" |
| 194 | + |
| 195 | +||| Which tools require a Ready index (cannot run during Indexing). |
| 196 | +public export |
| 197 | +requiresReadyIndex : McpTool -> Bool |
| 198 | +requiresReadyIndex ToolIndex = False |
| 199 | +requiresReadyIndex ToolStatus = False |
| 200 | +requiresReadyIndex _ = True |
| 201 | + |
| 202 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 203 | +-- C-ABI Exports |
| 204 | +-- ═══════════════════════════════════════════════════════════════════════════ |
| 205 | + |
| 206 | +||| Index state to integer. |
| 207 | +public export |
| 208 | +indexStateToInt : IndexState -> Int |
| 209 | +indexStateToInt Uninitialised = 0 |
| 210 | +indexStateToInt Indexing = 1 |
| 211 | +indexStateToInt Ready = 2 |
| 212 | +indexStateToInt Querying = 3 |
| 213 | +indexStateToInt IndexError = 4 |
| 214 | + |
| 215 | +||| FFI: Validate an index state transition. |
| 216 | +export |
| 217 | +codeseeker_can_transition : Int -> Int -> Int |
| 218 | +codeseeker_can_transition from to = |
| 219 | + let fromState = case from of |
| 220 | + 0 => Uninitialised |
| 221 | + 1 => Indexing |
| 222 | + 2 => Ready |
| 223 | + 3 => Querying |
| 224 | + _ => IndexError |
| 225 | + toState = case to of |
| 226 | + 0 => Uninitialised |
| 227 | + 1 => Indexing |
| 228 | + 2 => Ready |
| 229 | + 3 => Querying |
| 230 | + _ => IndexError |
| 231 | + in if canIndexTransition fromState toState then 1 else 0 |
| 232 | + |
| 233 | +||| FFI: Check if a tool requires a ready index. |
| 234 | +export |
| 235 | +codeseeker_tool_requires_ready : Int -> Int |
| 236 | +codeseeker_tool_requires_ready 1 = 0 -- ToolIndex |
| 237 | +codeseeker_tool_requires_ready 6 = 0 -- ToolStatus |
| 238 | +codeseeker_tool_requires_ready _ = 1 -- All others require Ready |
0 commit comments