Skip to content

Commit 2b53d74

Browse files
authored
feat: bin/brain/serve.go; search backend is Go not Python (#9)
* feat(brain): HTTP serve from bin/brain/serve.go, default Go search binary. Move the HTTP package to internal/httpapi. Default backend is var/bin/brain-search, not Python. bin/serve.go stays as a deprecation shim. * feat(httpapi): default search backend is var/bin/brain-search. bin/brain/serve.go is the command; bin/serve.go stays as a tagged deprecation shim. Tests fail if the default path still names Python.
1 parent db8e398 commit 2b53d74

6 files changed

Lines changed: 67 additions & 36 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ PLAN.md decisions + execution + open questions
3636
docs/ published docs
3737
skills/ in-project agent skills (vendored, no external links)
3838
bin/ self-describing tools bin/{subject}/{method}.go (shebang)
39-
bin/brain/ search.go (deduction); libs in internal/brain
39+
bin/brain/ search.go, serve.go; libs in internal/brain and internal/httpapi
4040
internal/ shared Go (brain/rank is cgo-free)
41-
bin/serve.go async Go HTTP server entry (self-executing go run shebang)
42-
bin/watch/ corpus watcher Go package (mtimes, no inotify deps)
43-
bin/server/ async Go HTTP server (goroutines, bounded worker pool)
41+
bin/watch/ corpus watcher (internal via bin/brain/watch later)
4442
bin/mail/ mail pipeline: sync (Go), import (md), index_mail (rebuild)
4543
bin/tools/ vendored python libs behind bin/* (kblib, yamlout, websearch)
4644
bin/docker-entrypoint container entrypoint (brain index|search|serve|watch)

‎bin/brain/serve.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//usr/bin/env go run -tags=brain_serve "$0" "$@"; exit
2+
//go:build brain_serve
3+
//
4+
// bin/brain/serve.go - HTTP API for the 2dph brain.
5+
//
6+
// KB_ROOT=/path/to/2dph ./bin/brain/serve.go
7+
// KB_SEARCH_CMD=... KB_WORKERS=4 KB_PORT=8630 ./bin/brain/serve.go
8+
//
9+
// Default search backend is var/bin/brain-search (Go), not Python.
10+
// NOTE: never run `gofmt -w` on this file — it breaks the shebang.
11+
package main
12+
13+
import (
14+
"os"
15+
16+
"github.com/eSlider/2dph/internal/httpapi"
17+
)
18+
19+
func main() {
20+
if os.Getenv("KB_ROOT") == "" {
21+
if wd, err := os.Getwd(); err == nil {
22+
os.Setenv("KB_ROOT", wd)
23+
}
24+
}
25+
httpapi.Run()
26+
}

‎bin/doc.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Deprecated shebang mains at bin root (serve.go is tagged brain_serve).
2+
package main

‎bin/serve.go‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
//usr/bin/env go run "$0" "$@"; exit
2-
// bin/serve.go - async Go HTTP server for the 2dph brain (see bin/server).
1+
//usr/bin/env go run -tags=brain_serve "$0" "$@"; exit
2+
//go:build brain_serve
33
//
4-
// KB_ROOT=/path/to/2dph ./bin/serve.go # serve the brain
5-
// KB_SEARCH_CMD=... KB_WORKERS=4 KB_PORT=8630 ./bin/serve.go
6-
//
7-
// Shebang trick: the first line is a Go `//` comment; when executed, env runs
8-
// `go run "$0"` so this file doubles as an executable script. The real code
9-
// lives in the importable package (module path, never a relative import).
10-
// NOTE: never run `gofmt -w` on this file - it rewrites `//usr/bin/env` to
11-
// `// usr/...` and breaks the shebang.
4+
// bin/serve.go — deprecated; use bin/brain/serve.go.
125
package main
136

147
import (
8+
"fmt"
159
"os"
1610

17-
"github.com/eSlider/2dph/bin/server"
11+
"github.com/eSlider/2dph/internal/httpapi"
1812
)
1913

2014
func main() {
21-
if env := os.Getenv("KB_ROOT"); env == "" {
15+
fmt.Fprintln(os.Stderr, "bin/serve.go is deprecated; use bin/brain/serve.go")
16+
if os.Getenv("KB_ROOT") == "" {
2217
if wd, err := os.Getwd(); err == nil {
2318
os.Setenv("KB_ROOT", wd)
2419
}
2520
}
26-
server.Run()
21+
httpapi.Run()
2722
}
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
//
33
// Async by design: every request runs on its own goroutine, and CPU-heavy
44
// searches are serialized through a bounded worker pool (a counting
5-
// semaphore) so N requests can't spawn N Python interpreters at once.
5+
// semaphore) so N requests can't spawn N search processes at once.
66
//
7-
// Used by bin/serve.go which is a self-executing shebang script:
8-
//
9-
// ///usr/bin/env go run "$0" "$@"; exit
10-
// package main
11-
// import "github.com/eSlider/2dph/bin/server"
12-
// func main() { server.Run() }
13-
package server
7+
// Used by bin/brain/serve.go.
8+
package httpapi
149

1510
import (
1611
"context"
@@ -100,8 +95,8 @@ func writeRaw(w http.ResponseWriter, code int, body []byte) {
10095
w.Write(body)
10196
}
10297

103-
// brainSearcher shells out to bin/kb/search --json. A single python search
104-
// is bounded and short-lived; the worker pool keeps at most N live.
98+
// brainSearcher shells out to the Go brain-search binary (not Python).
99+
// A single search is bounded and short-lived; the worker pool keeps at most N live.
105100
type brainSearcher struct {
106101
cmdPath string
107102
timeout time.Duration
@@ -122,15 +117,18 @@ func (b *brainSearcher) Search(ctx context.Context, query string, limit int) ([]
122117
return out, nil
123118
}
124119

125-
// Run starts the HTTP server. Reads env: KB_SEARCH_CMD (default bin/kb/search,
126-
// relative to the repo root given by KB_ROOT), KB_WORKERS (default 4), KB_PORT
127-
// (default 8630).
120+
func defaultSearchCmd(root string) string {
121+
if env := os.Getenv("KB_SEARCH_CMD"); env != "" {
122+
return env
123+
}
124+
return filepath.Join(root, "var", "bin", "brain-search")
125+
}
126+
127+
// Run starts the HTTP server. Reads env: KB_SEARCH_CMD (default
128+
// $KB_ROOT/var/bin/brain-search), KB_WORKERS (default 4), KB_PORT (default 8630).
128129
func Run() {
129130
root := os.Getenv("KB_ROOT")
130-
searchPath := os.Getenv("KB_SEARCH_CMD")
131-
if searchPath == "" {
132-
searchPath = filepath.Join(root, "bin", "kb", "search")
133-
}
131+
searchPath := defaultSearchCmd(root)
134132
workers := 4
135133
if raw := os.Getenv("KB_WORKERS"); raw != "" {
136134
if n, err := strconv.Atoi(raw); err == nil && n > 0 {
@@ -147,7 +145,7 @@ func Run() {
147145
searcher := &brainSearcher{cmdPath: searchPath, timeout: 60 * time.Second}
148146
handler := NewServer(searcher, workers)
149147
addr := "127.0.0.1:" + strconv.Itoa(port)
150-
log.Printf("serve: %s (workers=%d)", addr, workers)
148+
log.Printf("serve: %s (workers=%d cmd=%s)", addr, workers, searchPath)
151149
if err := http.ListenAndServe(addr, handler); err != nil {
152150
log.Fatal(err)
153151
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package server
1+
package httpapi
22

33
import (
44
"context"
55
"encoding/json"
66
"net/http"
77
"net/http/httptest"
8+
"strings"
89
"sync"
910
"sync/atomic"
1011
"testing"
@@ -141,6 +142,17 @@ func TestSearchRejectsBadLimit(t *testing.T) {
141142
}
142143
}
143144

145+
func TestDefaultSearchCmdIsBrainNotPython(t *testing.T) {
146+
t.Setenv("KB_SEARCH_CMD", "")
147+
cmd := defaultSearchCmd("/repo")
148+
if strings.Contains(strings.ToLower(cmd), "python") {
149+
t.Fatalf("search path still python: %s", cmd)
150+
}
151+
if !strings.Contains(cmd, "brain") {
152+
t.Fatalf("search path must be the Go brain binary, got %s", cmd)
153+
}
154+
}
155+
144156
func TestSearchTimeout(t *testing.T) {
145157
fs := &fakeSearcher{delay: time.Second}
146158
h := NewServer(fs, 1)

0 commit comments

Comments
 (0)