Skip to content

Commit db678c3

Browse files
committed
refactor(hickey): pass Note through noteMatchOf to drop re-lookup and latent title divergence
In findNotes the note is already in scope; the previous noteMatchOf discarded it and re-derived the title via M.modelLookupTitle, doing a second IxSet lookup per match. More subtly, the predicate's match key (note._noteTitle) and the report's title (modelLookupTitle) were computed via different paths — they agreed today only accidentally. noteMatchOf now takes a Note directly. The route-only callers (getBacklinks, RRTAmbiguous in resolveWikilink) go through a new noteMatchOfRoute helper that does one lookup at the call boundary.
1 parent 476eccf commit db678c3

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

emanote/src/Emanote/MCP/Tools.hs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,31 @@ data NoteMatch = NoteMatch
7878
deriving stock (Eq, Show, Generic)
7979
deriving anyclass (ToJSON)
8080

81-
noteMatchOf :: Model -> R.LMLRoute -> NoteMatch
82-
noteMatchOf model r =
83-
let p = toText $ R.lmlSourcePath r
81+
noteMatchOf :: N.Note -> NoteMatch
82+
noteMatchOf note =
83+
let p = toText $ R.lmlSourcePath (note ^. N.noteRoute)
8484
in NoteMatch
8585
{ path = p
86-
, title = Tit.toPlain (M.modelLookupTitle r model)
86+
, title = Tit.toPlain (note ^. N.noteTitle)
8787
, uri = kindToUri (Note (toString p))
8888
}
8989

90+
{- | Build a 'NoteMatch' from a route. Falls back to a route-derived title
91+
when the note can't be looked up — used by callers that hold a route but
92+
not the 'N.Note' (e.g. backlink sources).
93+
-}
94+
noteMatchOfRoute :: Model -> R.LMLRoute -> NoteMatch
95+
noteMatchOfRoute model r =
96+
case M.modelLookupNoteByRoute' r model of
97+
Just note -> noteMatchOf note
98+
Nothing ->
99+
let p = toText $ R.lmlSourcePath r
100+
in NoteMatch
101+
{ path = p
102+
, title = Tit.toPlain (Tit.fromRoute r)
103+
, uri = kindToUri (Note (toString p))
104+
}
105+
90106
-- ---------------------------------------------------------------------------
91107
-- find_notes
92108
-- ---------------------------------------------------------------------------
@@ -101,11 +117,9 @@ findNotes :: Text -> Int -> Model -> [NoteMatch]
101117
findNotes query lim model =
102118
let q = T.toLower query
103119
hit note =
104-
let r = note ^. N.noteRoute
105-
t = Tit.toPlain (note ^. N.noteTitle)
106-
p = toText (R.lmlSourcePath r)
107-
in if q `T.isInfixOf` T.toLower t || q `T.isInfixOf` T.toLower p
108-
then Just (noteMatchOf model r)
120+
let m = noteMatchOf note
121+
in if q `T.isInfixOf` T.toLower (title m) || q `T.isInfixOf` T.toLower (path m)
122+
then Just m
109123
else Nothing
110124
in take (max 0 lim) $ mapMaybe hit $ Ix.toList (model ^. M.modelNotes)
111125

@@ -147,7 +161,7 @@ getBacklinks fp model =
147161
case R.mkLMLRouteFromMdOrOrgFilePath fp of
148162
Nothing -> Left $ "Not a recognised note path: " <> toText fp
149163
Just r ->
150-
Right $ noteMatchOf model . fst <$> G.modelLookupBacklinks r model
164+
Right $ noteMatchOfRoute model . fst <$> G.modelLookupBacklinks r model
151165

152166
getBacklinksTool :: IO Model -> ToolHandler
153167
getBacklinksTool readModel =
@@ -213,16 +227,13 @@ resolveWikilink wlText mFromPath model = do
213227
Nothing -> Right (M.modelIndexRoute model)
214228
Just p -> maybeToRight ("Not a recognised note path: " <> toText p) (R.mkLMLRouteFromMdOrOrgFilePath p)
215229
Right $ case Resolve.resolveWikiLinkMustExist model fromR wl of
216-
Rel.RRTFound (Left (_, note)) ->
217-
ResolvedNote (noteMatchOf model (note ^. N.noteRoute))
218-
Rel.RRTFound (Right sf) ->
219-
ResolvedStatic (staticFilePath sf)
230+
Rel.RRTFound (Left (_, note)) -> ResolvedNote (noteMatchOf note)
231+
Rel.RRTFound (Right sf) -> ResolvedStatic (staticFilePath sf)
220232
Rel.RRTMissing -> UnresolvedMissing
221-
Rel.RRTAmbiguous cs ->
222-
UnresolvedAmbiguous $ toList $ candidate <$> cs
233+
Rel.RRTAmbiguous cs -> UnresolvedAmbiguous $ toList $ candidate <$> cs
223234
where
224235
candidate = \case
225-
Left (_, note) -> Left $ noteMatchOf model (note ^. N.noteRoute)
236+
Left (_, note) -> Left (noteMatchOf note)
226237
Right sf -> Right (staticFilePath sf)
227238
staticFilePath sf = toText $ R.encodeRoute (sf ^. SF.staticFileRoute)
228239

0 commit comments

Comments
 (0)