Rank memory search in SQL instead of rebuilding BM25 in Python
The product path pulled every atom into Python and rebuilt a BM25Okapi
index per query: p50 20.16s over 165,973 atoms. It now runs one SQLite
statement with LIMIT/OFFSET.
- text_index.py: one normalizer for both sides of the index. CJK runs are
split per character (unicode61 cannot segment them), camelCase and
separators are split so "search atoms" finds SearchAtoms, latin tokens
get a prefix match. Rows written before this existed hold raw text in
atom_fts, so schema v3 rebuilds the index on upgrade.
- retrieval.py: filters (holder / scenario / source / modality / since /
until / min_trust), a base64 cursor, and reciprocal rank fusion in place
of the 0.55/0.35/0.10 blend whose graph term was always zero.
- An empty result is {status: "no_match"}, not a bare array.
- Go: search_atoms and compile_context rank through POST /api/search. The
second Go tokenizer is deleted; two implementations could drift, which
is what the MCP/HTTP parity requirement forbids.
- db.py passed 500 lines, so the row codec moved to row_codec.py.
- Tooling: installed the repo's own pyright. pyrightconfig.json had no
exclude and swept the downloaded corpus (1171 errors); with excludes the
count is 62, all pre-existing (evals 48, profiles 4, training 4,
ingestion 3, runtime 2, cartridge_assets 1). backend/pyproject.toml
carried a second [tool.pyright] that shadowed the root config; deleted.
- bench/scale_check.py measures the product search at corpus size. The
100k-atom p50 number is not written until it is measured on an idle
machine.
Verification: 112 pytest passed; go build/vet/test clean, gofmt clean;
ruff clean on every touched file; mypy clean on the 14 touched Python
files; repo pyright 62 errors, unchanged per-file ledger.
This commit is contained in:
1 parent
3d792417cf
commit
674c88e84e
29 files changed
+1097
-356
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/ZacharyZhang-NY/ely-eye/ely-eye-mcp/internal/apiclient"
|
||||
"github.com/ZacharyZhang-NY/ely-eye/ely-eye-mcp/internal/contextpack"
|
||||
"github.com/ZacharyZhang-NY/ely-eye/ely-eye-mcp/internal/domain"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
@@ -21,8 +22,10 @@ type GetCartridgeInput struct {
|
||||
}
|
||||
|
||||
type SearchAtomsInput struct {
|
||||
Query string `json:"query" jsonschema:"search query for the Ely-Eye FTS atom index"`
|
||||
Query string `json:"query" jsonschema:"search query, matched by the server's own ranking"`
|
||||
Modality string `json:"modality,omitempty" jsonschema:"optional exact modality filter such as text, code, image, pdf_page, pdf_region, video_frame, ui_screenshot"`
|
||||
Holder string `json:"holder,omitempty" jsonschema:"optional: only this holder's memories may answer"`
|
||||
Scenario string `json:"scenario,omitempty" jsonschema:"optional: only this scenario, e.g. home or work"`
|
||||
Limit int `json:"limit,omitempty" jsonschema:"maximum number of atoms to return, capped at 100"`
|
||||
}
|
||||
|
||||
@@ -160,11 +163,17 @@ func (s *Server) getCartridgeTool(ctx context.Context, _ *mcp.CallToolRequest, i
|
||||
}
|
||||
|
||||
func (s *Server) searchAtomsTool(ctx context.Context, _ *mcp.CallToolRequest, input SearchAtomsInput) (*mcp.CallToolResult, SearchAtomsOutput, error) {
|
||||
hits, err := s.store.SearchAtoms(ctx, input.Query, input.Modality, input.Limit)
|
||||
result, err := s.api.Search(ctx, apiclient.SearchOptions{
|
||||
Query: input.Query,
|
||||
Modality: input.Modality,
|
||||
Holder: input.Holder,
|
||||
Scenario: input.Scenario,
|
||||
Limit: input.Limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, SearchAtomsOutput{}, err
|
||||
}
|
||||
return nil, SearchAtomsOutput{Hits: contextpack.SummarizeHits(hits, 1200)}, nil
|
||||
return nil, SearchAtomsOutput{Hits: contextpack.SummarizeHits(result.Hits, 1200)}, nil
|
||||
}
|
||||
|
||||
func (s *Server) fetchAtomTool(ctx context.Context, _ *mcp.CallToolRequest, input FetchAtomInput) (*mcp.CallToolResult, FetchAtomOutput, error) {
|
||||
@@ -179,11 +188,11 @@ func (s *Server) compileContextTool(ctx context.Context, _ *mcp.CallToolRequest,
|
||||
if input.Question == "" {
|
||||
return nil, CompileContextOutput{}, errors.New("question is required")
|
||||
}
|
||||
hits, err := s.store.SearchAtoms(ctx, input.Question, "", input.Limit)
|
||||
hits, err := s.api.Search(ctx, apiclient.SearchOptions{Query: input.Question, Limit: input.Limit})
|
||||
if err != nil {
|
||||
return nil, CompileContextOutput{}, err
|
||||
}
|
||||
pack := contextpack.Build(input.Question, input.Profile, hits, input.TokenBudget)
|
||||
pack := contextpack.Build(input.Question, input.Profile, hits.Hits, input.TokenBudget)
|
||||
return nil, CompileContextOutput{Context: pack}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user