feat(brain): structured wing/hall taxonomy + obsidian-compatible layout
Adds a two-dimensional address (wing, hall) to brain notes. A wing is a
topic domain (e.g. jepa-fx, hyperguild); a hall is one of a closed
vocabulary of memory types (facts, decisions, failures, hypotheses,
sources). Notes route to brain/wiki/<wing>/<hall>/<slug>.md with
wing/hall/created_at YAML frontmatter, making the directory a valid
Obsidian vault.
Changes:
- new package ingestion/internal/brain (NotePath, ValidHalls, Sanitise,
BuildWingIndex, BuildAllWingIndexes)
- api.WriteNote refactored to WriteNoteOptions; wing+hall routes to
brain/wiki/, otherwise falls back to brain/knowledge/ (legacy)
- search.Query → QueryOptions with optional Wing/Hall filtering; Result
carries wing/hall extracted from frontmatter or path segments
- MCP tools brain_write and brain_query gain optional wing/hall params
(hall enum-validated); new brain_index tool regenerates _index.md MOC
- POST /index REST endpoint mirrors brain_index
- brain_write auto-rebuilds the wing's _index.md after a wing+hall write
- scripts/migrate-brain-halls.sh migrates flat brain/wiki/{concepts,entities}/
into the new layout (dry-run by default, --commit applies)
All existing tests pass; new tests cover wing/hall write routing, scope
filtering, invalid hall rejection, _index.md generation, and migration
script paths.
Closes hyperguild#1.
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/mathiasbq/hyperguild/ingestion/internal/brain"
|
||||
)
|
||||
|
||||
// Result is a single search hit from the brain wiki.
|
||||
@@ -17,24 +19,41 @@ type Result struct {
|
||||
Title string `json:"title"`
|
||||
Excerpt string `json:"excerpt"`
|
||||
Score int `json:"score"`
|
||||
Wing string `json:"wing,omitempty"`
|
||||
Hall string `json:"hall,omitempty"`
|
||||
}
|
||||
|
||||
// Query searches all .md files under brainDir/wiki/ for pages containing
|
||||
// any of the whitespace-separated terms in query. Returns up to limit results
|
||||
// sorted by score descending.
|
||||
func Query(brainDir, query string, limit int) ([]Result, error) {
|
||||
if limit <= 0 {
|
||||
limit = 5
|
||||
// QueryOptions configures a search.
|
||||
//
|
||||
// When Wing is set, the walk is restricted to brain/wiki/<wing>/.
|
||||
// When Hall is additionally set, the walk is restricted to
|
||||
// brain/wiki/<wing>/<hall>/. Without either, the legacy walk over
|
||||
// brain/knowledge/ and brain/wiki/ is used.
|
||||
type QueryOptions struct {
|
||||
Query string
|
||||
Limit int
|
||||
Wing string
|
||||
Hall string
|
||||
}
|
||||
|
||||
// Query searches the brain. Returns up to opts.Limit results sorted by
|
||||
// score descending. Empty query returns nil.
|
||||
func Query(brainDir string, opts QueryOptions) ([]Result, error) {
|
||||
if opts.Limit <= 0 {
|
||||
opts.Limit = 5
|
||||
}
|
||||
terms := strings.Fields(strings.ToLower(query))
|
||||
terms := strings.Fields(strings.ToLower(opts.Query))
|
||||
if len(terms) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var results []Result
|
||||
roots, err := resolveRoots(brainDir, opts.Wing, opts.Hall)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, subdir := range []string{"knowledge", "wiki"} {
|
||||
dir := filepath.Join(brainDir, subdir)
|
||||
var results []Result
|
||||
for _, dir := range roots {
|
||||
if _, statErr := os.Stat(dir); os.IsNotExist(statErr) {
|
||||
continue
|
||||
}
|
||||
@@ -46,13 +65,11 @@ func Query(brainDir, query string, limit int) ([]Result, error) {
|
||||
if d.IsDir() || !strings.HasSuffix(path, ".md") {
|
||||
return nil
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
slog.Warn("search: skipping unreadable file", "path", path, "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
lower := strings.ToLower(string(content))
|
||||
score := 0
|
||||
for _, term := range terms {
|
||||
@@ -61,18 +78,19 @@ func Query(brainDir, query string, limit int) ([]Result, error) {
|
||||
if score == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
rel, err := filepath.Rel(brainDir, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rel path: %w", err)
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
|
||||
wing, hall := extractWingHall(string(content), rel)
|
||||
results = append(results, Result{
|
||||
Path: rel,
|
||||
Title: extractTitle(string(content), d.Name()),
|
||||
Excerpt: excerpt(string(content), 300),
|
||||
Score: score,
|
||||
Wing: wing,
|
||||
Hall: hall,
|
||||
})
|
||||
return nil
|
||||
})
|
||||
@@ -84,12 +102,81 @@ func Query(brainDir, query string, limit int) ([]Result, error) {
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
return results[i].Score > results[j].Score
|
||||
})
|
||||
if len(results) > limit {
|
||||
results = results[:limit]
|
||||
if len(results) > opts.Limit {
|
||||
results = results[:opts.Limit]
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// resolveRoots returns the directories to walk for the given wing/hall
|
||||
// filters. Validates hall against the closed vocabulary when set.
|
||||
func resolveRoots(brainDir, wing, hall string) ([]string, error) {
|
||||
if hall != "" && !brain.IsValidHall(hall) {
|
||||
return nil, fmt.Errorf("invalid hall %q", hall)
|
||||
}
|
||||
if wing != "" {
|
||||
w := brain.Sanitise(wing)
|
||||
if w == "" {
|
||||
return nil, fmt.Errorf("invalid wing %q", wing)
|
||||
}
|
||||
if hall != "" {
|
||||
return []string{filepath.Join(brainDir, "wiki", w, hall)}, nil
|
||||
}
|
||||
return []string{filepath.Join(brainDir, "wiki", w)}, nil
|
||||
}
|
||||
if hall != "" {
|
||||
return nil, fmt.Errorf("hall filter requires wing")
|
||||
}
|
||||
return []string{
|
||||
filepath.Join(brainDir, "knowledge"),
|
||||
filepath.Join(brainDir, "wiki"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// extractWingHall reads wing/hall from frontmatter first, falling back to
|
||||
// path segments brain/wiki/<wing>/<hall>/.
|
||||
func extractWingHall(content, relPath string) (wing, hall string) {
|
||||
scanner := bufio.NewScanner(strings.NewReader(content))
|
||||
inFrontmatter := false
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.TrimSpace(line) == "---" {
|
||||
if !inFrontmatter {
|
||||
inFrontmatter = true
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
if !inFrontmatter {
|
||||
continue
|
||||
}
|
||||
key, val, ok := strings.Cut(line, ":")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
v := strings.Trim(strings.TrimSpace(val), `"'`)
|
||||
switch strings.TrimSpace(key) {
|
||||
case "wing":
|
||||
wing = v
|
||||
case "hall":
|
||||
hall = v
|
||||
}
|
||||
}
|
||||
if wing != "" && hall != "" {
|
||||
return wing, hall
|
||||
}
|
||||
parts := strings.Split(relPath, "/")
|
||||
if len(parts) >= 4 && parts[0] == "wiki" {
|
||||
if wing == "" {
|
||||
wing = parts[1]
|
||||
}
|
||||
if hall == "" && brain.IsValidHall(parts[2]) {
|
||||
hall = parts[2]
|
||||
}
|
||||
}
|
||||
return wing, hall
|
||||
}
|
||||
|
||||
func extractTitle(content, filename string) string {
|
||||
scanner := bufio.NewScanner(strings.NewReader(content))
|
||||
inFrontmatter := false
|
||||
@@ -113,7 +200,6 @@ func extractTitle(content, filename string) string {
|
||||
}
|
||||
|
||||
func excerpt(content string, maxLen int) string {
|
||||
// Skip frontmatter, return first maxLen chars of body.
|
||||
parts := strings.SplitN(content, "---", 3)
|
||||
body := content
|
||||
if len(parts) == 3 {
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestSearch_ReturnsMatchingPages(t *testing.T) {
|
||||
0o644,
|
||||
))
|
||||
|
||||
results, err := search.Query(dir, "retry transient", 5)
|
||||
results, err := search.Query(dir, search.QueryOptions{Query: "retry transient", Limit: 5})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 1)
|
||||
assert.Equal(t, "knowledge/retry-logic.md", results[0].Path)
|
||||
@@ -36,6 +36,49 @@ func TestSearch_ReturnsMatchingPages(t *testing.T) {
|
||||
assert.Contains(t, results[0].Excerpt, "Retry")
|
||||
}
|
||||
|
||||
func TestSearch_WingHallScoping(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, p := range []struct{ rel, body string }{
|
||||
{"wiki/jepa-fx/decisions/val-vol.md", "---\nwing: jepa-fx\nhall: decisions\n---\nval-vol-r2 keyword.\n"},
|
||||
{"wiki/jepa-fx/facts/architecture.md", "---\nwing: jepa-fx\nhall: facts\n---\nval-vol-r2 keyword in facts.\n"},
|
||||
{"wiki/hyperguild/decisions/routing.md", "---\nwing: hyperguild\nhall: decisions\n---\nval-vol-r2 reference.\n"},
|
||||
{"knowledge/loose.md", "---\n---\nval-vol-r2 in knowledge.\n"},
|
||||
} {
|
||||
full := filepath.Join(dir, p.rel)
|
||||
require.NoError(t, os.MkdirAll(filepath.Dir(full), 0o755))
|
||||
require.NoError(t, os.WriteFile(full, []byte(p.body), 0o644))
|
||||
}
|
||||
|
||||
// No filter: walk both knowledge/ and wiki/ — all 4 match.
|
||||
got, err := search.Query(dir, search.QueryOptions{Query: "val-vol-r2", Limit: 10})
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, got, 4)
|
||||
|
||||
// Wing scope: 2 jepa-fx hits, no hyperguild, no knowledge.
|
||||
got, err = search.Query(dir, search.QueryOptions{Query: "val-vol-r2", Limit: 10, Wing: "jepa-fx"})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 2)
|
||||
for _, r := range got {
|
||||
assert.Equal(t, "jepa-fx", r.Wing)
|
||||
}
|
||||
|
||||
// Wing+Hall scope: 1 hit.
|
||||
got, err = search.Query(dir, search.QueryOptions{Query: "val-vol-r2", Limit: 10, Wing: "jepa-fx", Hall: "decisions"})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 1)
|
||||
assert.Equal(t, "jepa-fx", got[0].Wing)
|
||||
assert.Equal(t, "decisions", got[0].Hall)
|
||||
assert.Equal(t, "wiki/jepa-fx/decisions/val-vol.md", got[0].Path)
|
||||
|
||||
// Invalid hall rejected.
|
||||
_, err = search.Query(dir, search.QueryOptions{Query: "x", Wing: "jepa-fx", Hall: "garbage"})
|
||||
require.Error(t, err)
|
||||
|
||||
// Hall without wing rejected.
|
||||
_, err = search.Query(dir, search.QueryOptions{Query: "x", Hall: "facts"})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestSearch_RespectsLimit(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(dir, "knowledge"), 0o755))
|
||||
@@ -46,7 +89,7 @@ func TestSearch_RespectsLimit(t *testing.T) {
|
||||
0o644,
|
||||
))
|
||||
}
|
||||
results, err := search.Query(dir, "retry", 3)
|
||||
results, err := search.Query(dir, search.QueryOptions{Query: "retry", Limit: 3})
|
||||
require.NoError(t, err)
|
||||
assert.LessOrEqual(t, len(results), 3)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user