Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e8dbdc7df | ||
|
|
1c7db6d499 | ||
|
|
0786ddcb06 | ||
|
|
eeb5b79cf2 | ||
|
|
5990feb1f6 | ||
|
|
d27a738fee | ||
|
|
669e184cf6 | ||
|
|
ebc3f948c1 | ||
|
|
fe6a02024c | ||
|
|
4a065d9838 | ||
|
|
e3c6ef5684 | ||
|
|
98c14e23f1 | ||
|
|
ff1716de40 | ||
|
|
fec5325c7a | ||
|
|
27d9521e7f | ||
|
|
a7cb8d4c76 | ||
|
|
53cd00284d | ||
|
|
b73b4d4f97 | ||
|
|
1d1f6a90ff | ||
|
|
f220bcd95a | ||
|
|
678a1d1dba | ||
|
|
8781c0c3eb | ||
|
|
d6b17e8819 |
@@ -36,11 +36,10 @@ PLAN.md decisions + execution + open questions
|
|||||||
docs/ published docs
|
docs/ published docs
|
||||||
skills/ in-project agent skills (vendored, no external links)
|
skills/ in-project agent skills (vendored, no external links)
|
||||||
bin/ self-describing tools bin/{subject}/{method}.go (shebang)
|
bin/ self-describing tools bin/{subject}/{method}.go (shebang)
|
||||||
bin/brain/ search.go (deduction); libs in internal/brain
|
bin/brain/ search.go, serve.go; libs in internal/brain and internal/httpapi
|
||||||
internal/ shared Go (brain/rank is cgo-free)
|
bin/chats/ sync.go import.go facts.go apply.go; libs in internal/chats
|
||||||
bin/serve.go async Go HTTP server entry (self-executing go run shebang)
|
internal/ shared Go (brain/rank is cgo-free; chats parsers too)
|
||||||
bin/watch/ corpus watcher Go package (mtimes, no inotify deps)
|
bin/watch/ corpus watcher (internal via bin/brain/watch later)
|
||||||
bin/server/ async Go HTTP server (goroutines, bounded worker pool)
|
|
||||||
bin/mail/ mail pipeline: sync (Go), import (md), index_mail (rebuild)
|
bin/mail/ mail pipeline: sync (Go), import (md), index_mail (rebuild)
|
||||||
bin/tools/ vendored python libs behind bin/* (kblib, yamlout, websearch)
|
bin/tools/ vendored python libs behind bin/* (kblib, yamlout, websearch)
|
||||||
bin/docker-entrypoint container entrypoint (brain index|search|serve|watch)
|
bin/docker-entrypoint container entrypoint (brain index|search|serve|watch)
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ detective method: **a fact needs ≥2 independent sources or it is
|
|||||||
|---|----------|--------|
|
|---|----------|--------|
|
||||||
| D1 | RAG corpus | ops stack (chat, onlyoffice, gitea/NPM, searchxng, observability, ai-bot, mcp-servers, `~/.ssh/config`) + portfolio. Exclude `office.dev` + jobs/applications. |
|
| D1 | RAG corpus | ops stack (chat, onlyoffice, gitea/NPM, searchxng, observability, ai-bot, mcp-servers, `~/.ssh/config`) + portfolio. Exclude `office.dev` + jobs/applications. |
|
||||||
| D2 | skill merging | integrate skills **in this project** `skills/`; skip gitea / brain-dependent skills. |
|
| D2 | skill merging | integrate skills **in this project** `skills/`; skip gitea / brain-dependent skills. |
|
||||||
| D3 | web search | import `web-search`, retire local `searxng-ops`. Vendored here, no remote link. |
|
| D3 | web search | Vendored client; SearXNG URL is config. Optional Compose instance (sanitized settings). Do not run a second copy on a host that already has one. Empty/`throttled` ≠ “nothing exists”. |
|
||||||
| D4 | embeddings | **model2vec** `minishlab/potion-multilingual-128M` instead of embeddinggemma. |
|
| D4 | embeddings | **model2vec** `minishlab/potion-multilingual-128M` instead of embeddinggemma. |
|
||||||
| D5 | parser | **mistune** for MD → leaf extraction (duckdb-md documented as future optional SQL/export layer, not v1). |
|
| D5 | parser | **mistune** for MD → leaf extraction (duckdb-md documented as future optional SQL/export layer, not v1). |
|
||||||
| D6 | graph engine | **LadybugDB** (Kuzu successor, MIT, embedded, native FTS+vector+Cypher). Python binding for `bin/*`; Go shebang for golang tools. |
|
| D6 | graph engine | **LadybugDB**. Go is the service (`bin/brain/search.go`, `internal/brain`); Python remains for index/write until the Go write path is safe. |
|
||||||
| D7 | db access | `db-yaml`/`psql-yq`-style, read-only, YAML out. OnlyOffice Postgres via SSH tunnel (`127.0.0.1:5433`). |
|
| D7 | db access | `db-yaml`/`psql-yq`-style, read-only, YAML out. OnlyOffice Postgres via SSH tunnel (`127.0.0.1:5433`). |
|
||||||
| D8 | evidence | detective method: ≥2 independent sources or `(not confirmed)`. Auto-pair docker ps × compose × ssh-config × docs. |
|
| D8 | evidence | detective method: ≥2 independent sources or `(not confirmed)`. Auto-pair docker ps × compose × ssh-config × docs. |
|
||||||
| D9 | facts/goal model | Who / What / How / Where / When + evidence + confidence on every edge. |
|
| D9 | facts/goal model | Who / What / How / Where / When + evidence + confidence on every edge. |
|
||||||
@@ -40,6 +40,8 @@ detective method: **a fact needs ≥2 independent sources or it is
|
|||||||
| D14 | tooling style | `bin/{subject}/{method}.go` shebang (e.g. `bin/brain/search.go`). Shared code in `internal/`. One root `go.mod` + `go.work`. No `bin/*/main.go`, no nested modules. |
|
| D14 | tooling style | `bin/{subject}/{method}.go` shebang (e.g. `bin/brain/search.go`). Shared code in `internal/`. One root `go.mod` + `go.work`. No `bin/*/main.go`, no nested modules. |
|
||||||
| D15 | repo | Gitea [`eSlider/2dph`](https://git.produktor.io/eSlider/2dph) is origin + [issues](https://git.produktor.io/eSlider/2dph/issues). GitHub `eSlider/2dph` is the public clone (PRs + Actions CI). No direct `main` pushes. TDD → PR → CI green → merge. |
|
| D15 | repo | Gitea [`eSlider/2dph`](https://git.produktor.io/eSlider/2dph) is origin + [issues](https://git.produktor.io/eSlider/2dph/issues). GitHub `eSlider/2dph` is the public clone (PRs + Actions CI). No direct `main` pushes. TDD → PR → CI green → merge. |
|
||||||
| D16 | contradictions | ≥2 yes vs ≥2 no → unrelated sources conflict → hypothesis → `(not confirmed)`. Resolution (authority, staleness adjudication) = **v2**, tracked as open question. |
|
| D16 | contradictions | ≥2 yes vs ≥2 no → unrelated sources conflict → hypothesis → `(not confirmed)`. Resolution (authority, staleness adjudication) = **v2**, tracked as open question. |
|
||||||
|
| D17 | assertion gate | Fact-check every *claim* (facts → info → live sources → web), not every edit. Missing graph ≠ “does not exist”. |
|
||||||
|
| D18 | reasoner | Pluggable OpenAI-compatible URL. RAM: Qwen3.5-9B. Quality: Bonsai-27B or Qwen3.6-27B. No official Qwen3.6-9B. |
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
@@ -51,9 +53,12 @@ detective method: **a fact needs ≥2 independent sources or it is
|
|||||||
bin/
|
bin/
|
||||||
facts/extract auto-pair 2 sources → lexicon yaml + graph
|
facts/extract auto-pair 2 sources → lexicon yaml + graph
|
||||||
facts/audit ["self"|"facts"|"info"|"stale"] 2-source + staleness gate
|
facts/audit ["self"|"facts"|"info"|"stale"] 2-source + staleness gate
|
||||||
kb/index build FTS + HNSW from corpus
|
kb/index build FTS + HNSW from corpus (Python, for now)
|
||||||
kb/search deduction: facts → info → web-search; --hop N
|
brain/search.go deduction: facts → info → web-search
|
||||||
kb/get kb/stats kb/eval
|
kb/get kb/stats kb/eval
|
||||||
|
brain/serve.go HTTP API (internal/httpapi)
|
||||||
|
chats/sync.go import.go facts.go apply.go
|
||||||
|
(libs in internal/chats; no chats index)
|
||||||
md/import md/select md/tables md/gaps (mistune)
|
md/import md/select md/tables md/gaps (mistune)
|
||||||
brain/extract brain/audit brain/deduce (thinking wrapper)
|
brain/extract brain/audit brain/deduce (thinking wrapper)
|
||||||
web/search (vendored)
|
web/search (vendored)
|
||||||
@@ -110,19 +115,18 @@ Common props on every node/edge: `root`, `confidence`, `evidence[]`, `how`,
|
|||||||
corrupts its WAL on bulk-insert into an already-indexed DB. Conversion and
|
corrupts its WAL on bulk-insert into an already-indexed DB. Conversion and
|
||||||
indexing stay separate for crash safety.
|
indexing stay separate for crash safety.
|
||||||
4. Result: 17,835 messages → 28,918 info leafs, FTS + HNSW healthy, searchable
|
4. Result: 17,835 messages → 28,918 info leafs, FTS + HNSW healthy, searchable
|
||||||
via `bin/kb/search`.
|
via `bin/brain/search.go`.
|
||||||
|
|
||||||
## CI/CD pipeline (D15)
|
## CI/CD pipeline (D15)
|
||||||
|
|
||||||
`.github/workflows/ci.yml`:
|
`.github/workflows/ci.yml`:
|
||||||
|
|
||||||
1. go vet + go test ./... (Go tools; root module)
|
1. go vet + go test ./... (root module; packages without ladybug cgo)
|
||||||
2. `go test ./rank` in `bin/kbsearch` (cgo-free ranking + flag parser; nested module still needs ladybug for the rest)
|
2. `go test ./internal/brain/rank` (cgo-free ranking + flag parser)
|
||||||
3. `go test ./...` in `bin/chats` (Telegram + LinkedIn parsers; nested module)
|
3. python -m unittest discover -s bin/tools (includes published-docs SoT)
|
||||||
4. python -m unittest discover (Py tools)
|
4. bin/facts/audit self (lexicon internal consistency)
|
||||||
5. bin/facts/audit self (lexicon internal consistency)
|
5. bin/kb/eval (recall@5 ≥ 0.95, gates index regressions)
|
||||||
6. bin/kb/eval (recall@5 ≥ 0.95, gates index regressions)
|
6. md-docs build/lint if docs tooling arrives.
|
||||||
7. md-docs build/lint if docs tooling arrives.
|
|
||||||
|
|
||||||
Feedback loop: every commit → PR → CI → green/gate → merge. Same discipline as
|
Feedback loop: every commit → PR → CI → green/gate → merge. Same discipline as
|
||||||
`db/tech-poc`: contract first where there is an OpenAPI/message shape.
|
`db/tech-poc`: contract first where there is an OpenAPI/message shape.
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ graph TB
|
|||||||
AU["bin/facts/audit<br/>confidence + staleness"]
|
AU["bin/facts/audit<br/>confidence + staleness"]
|
||||||
IDX["bin/kb/index<br/>chunk + embed"]
|
IDX["bin/kb/index<br/>chunk + embed"]
|
||||||
MD["bin/md/import<br/>mistune leaves"]
|
MD["bin/md/import<br/>mistune leaves"]
|
||||||
SR["bin/kb/search<br/>deduction + --hop"]
|
SR["bin/brain/search.go<br/>deduction"]
|
||||||
end
|
end
|
||||||
|
|
||||||
subgraph store["Ladybug var/kb.lbug"]
|
subgraph store["Ladybug var/kb.lbug"]
|
||||||
@@ -85,21 +85,23 @@ fact; conflicting sources or a single source → `hypothesis` → `(not confirme
|
|||||||
## Deduction search
|
## Deduction search
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bin/kb/search "Matrix federation over HTTPS" # facts → info → web-search
|
bin/brain/search.go "Matrix federation over HTTPS" # facts → info → web-search
|
||||||
bin/kb/search "what runs on arc-2" --hop 1 # walk graph edges
|
bin/brain/search.go "onlyoffice postgres" --root facts
|
||||||
bin/kb/search "where is cs-lexicon" --json | yq '.' # YAML by default
|
bin/brain/search.go "where is cs-lexicon" --json | yq '.'
|
||||||
bin/kb/get <id> --body # full chunk on demand
|
bin/kb/get <id> --body # full chunk on demand
|
||||||
bin/kb/stats # index health
|
bin/kb/stats # index health
|
||||||
bin/kb/eval # recall@5 gate
|
bin/kb/eval # recall@5 gate
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`--hop` is not implemented (needs File/FROM_FILE edges); the flag errors instead of walking. `bin/kb/search` is a deprecated wrapper around `bin/brain/search.go`.
|
||||||
|
|
||||||
Mail is a first-class corpus (retrievable through the same search):
|
Mail is a first-class corpus (retrievable through the same search):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bin/mail/sync.go --source onlyoffice,gmail --workers 8 --out var/mail # raw sync (Go)
|
bin/mail/sync.go --source onlyoffice,gmail --workers 8 --out var/mail # raw sync (Go)
|
||||||
bin/mail/import --from-raw var/mail # JSON → markdown
|
bin/mail/import --from-raw var/mail # JSON → markdown
|
||||||
bin/mail/index_mail # rebuild brain incl. mail
|
bin/mail/index_mail # rebuild brain incl. mail
|
||||||
bin/kb/search "Mietwagen Nürnberg invoice" # now answers from mail
|
bin/brain/search.go "invoice from last week" # same search over mail leafs
|
||||||
```
|
```
|
||||||
|
|
||||||
## Storage
|
## Storage
|
||||||
@@ -117,10 +119,10 @@ bin/kb/search "Mietwagen Nürnberg invoice" # now a
|
|||||||
|
|
||||||
## Tooling conventions
|
## Tooling conventions
|
||||||
|
|
||||||
`bin/{subject}/{method}` — self-describing: shebang on line 1, usage comment
|
`bin/{subject}/{method}.go` — self-describing: shebang on line 1, usage comment
|
||||||
from line 2. bash + python primary; golang via the Go shebang when a compiled
|
from line 2. Shared code in `internal/`. YAML default output, `--json` for
|
||||||
helper is right. YAML default output, `--json` for machines. Everything that
|
machines. Tests gate every commit. HTTP: `bin/brain/serve.go` (default search
|
||||||
touches network/db is read-only, throttled, cached. Tests gate every commit.
|
binary `var/bin/brain-search`, not Python).
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
@@ -136,7 +138,7 @@ Docker (optional, cached model + var volumes):
|
|||||||
```bash
|
```bash
|
||||||
docker compose run --rm brain index # (re)index corpus
|
docker compose run --rm brain index # (re)index corpus
|
||||||
docker compose run --rm brain search "query" # one-shot query
|
docker compose run --rm brain search "query" # one-shot query
|
||||||
docker compose run --rm brain serve # async Go HTTP server
|
docker compose run --rm brain serve # bin/brain/serve.go
|
||||||
docker compose up brain-watch # auto re-index on change
|
docker compose up brain-watch # auto re-index on change
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Executable
+26
@@ -0,0 +1,26 @@
|
|||||||
|
//usr/bin/env go run -tags=brain_serve "$0" "$@"; exit
|
||||||
|
//go:build brain_serve
|
||||||
|
//
|
||||||
|
// bin/brain/serve.go - HTTP API for the 2dph brain.
|
||||||
|
//
|
||||||
|
// KB_ROOT=/path/to/2dph ./bin/brain/serve.go
|
||||||
|
// KB_SEARCH_CMD=... KB_WORKERS=4 KB_PORT=8630 ./bin/brain/serve.go
|
||||||
|
//
|
||||||
|
// Default search backend is var/bin/brain-search (Go), not Python.
|
||||||
|
// NOTE: never run `gofmt -w` on this file — it breaks the shebang.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/eSlider/2dph/internal/httpapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if os.Getenv("KB_ROOT") == "" {
|
||||||
|
if wd, err := os.Getwd(); err == nil {
|
||||||
|
os.Setenv("KB_ROOT", wd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
httpapi.Run()
|
||||||
|
}
|
||||||
Executable
+19
@@ -0,0 +1,19 @@
|
|||||||
|
//usr/bin/env go run -tags=chats_apply "$0" "$@"; exit
|
||||||
|
//go:build chats_apply
|
||||||
|
//
|
||||||
|
// bin/chats/apply.go - push extracted chat facts to OnlyOffice CRM.
|
||||||
|
//
|
||||||
|
// ./bin/chats/apply.go [--dry-run]
|
||||||
|
//
|
||||||
|
// NOTE: never run `gofmt -w` on this file — it breaks the shebang.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/eSlider/2dph/internal/chats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
os.Exit(chats.RunApply(os.Args[1:]))
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// Commands in this directory are shebang mains (sync.go, import.go, facts.go,
|
||||||
|
// apply.go), each behind an exclusive build tag so `go build ./bin/chats`
|
||||||
|
// does not see two mains. Shared code lives in internal/chats.
|
||||||
|
package main
|
||||||
Executable
+20
@@ -0,0 +1,20 @@
|
|||||||
|
//usr/bin/env go run -tags=chats_facts "$0" "$@"; exit
|
||||||
|
//go:build chats_facts
|
||||||
|
//
|
||||||
|
// bin/chats/facts.go - extract phone/email/linkedin facts from JSONL.
|
||||||
|
//
|
||||||
|
// ./bin/chats/facts.go
|
||||||
|
//
|
||||||
|
// Writes var/chats/facts/. Does not index the brain.
|
||||||
|
// NOTE: never run `gofmt -w` on this file — it breaks the shebang.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/eSlider/2dph/internal/chats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
os.Exit(chats.RunFacts(os.Args[1:]))
|
||||||
|
}
|
||||||
Executable
+20
@@ -0,0 +1,20 @@
|
|||||||
|
//usr/bin/env go run -tags=chats_import "$0" "$@"; exit
|
||||||
|
//go:build chats_import
|
||||||
|
//
|
||||||
|
// bin/chats/import.go - JSONL → markdown under var/chats/md/.
|
||||||
|
//
|
||||||
|
// ./bin/chats/import.go
|
||||||
|
//
|
||||||
|
// Conversion only. Brain ingest is bin/brain/index.go, not this command.
|
||||||
|
// NOTE: never run `gofmt -w` on this file — it breaks the shebang.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/eSlider/2dph/internal/chats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
os.Exit(chats.RunImport(os.Args[1:]))
|
||||||
|
}
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func runIndex(args []string) int {
|
|
||||||
fs := flag.NewFlagSet("chats index", flag.ContinueOnError)
|
|
||||||
help := fs.Bool("help", false, "")
|
|
||||||
fs.SetOutput(os.Stderr)
|
|
||||||
if err := fs.Parse(args); err != nil {
|
|
||||||
return 2
|
|
||||||
}
|
|
||||||
if *help {
|
|
||||||
fmt.Fprintln(os.Stderr, "usage: chats index")
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
root := repoRoot()
|
|
||||||
mdDir := filepath.Join(chatsDir(), "md")
|
|
||||||
|
|
||||||
_, err := os.Stat(mdDir)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
fmt.Fprintf(os.Stderr, "chats index: no chat markdown at %s; run 'chats import' first\n", mdDir)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
indexScript := filepath.Join(root, "bin", "kb", "index")
|
|
||||||
if _, err := os.Stat(indexScript); os.IsNotExist(err) {
|
|
||||||
fmt.Fprintf(os.Stderr, "chats index: %s not found\n", indexScript)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := exec.Command(indexScript, "--corpus", mdDir)
|
|
||||||
var outBuf, errBuf bytes.Buffer
|
|
||||||
cmd.Stdout = &outBuf
|
|
||||||
cmd.Stderr = &errBuf
|
|
||||||
cmd.Dir = root
|
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "chats index: %v\n%s", err, errBuf.String())
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
result := strings.TrimSpace(outBuf.String())
|
|
||||||
if result == "" {
|
|
||||||
result = strings.TrimSpace(errBuf.String())
|
|
||||||
}
|
|
||||||
fmt.Printf("chats index: %s\n", result)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
// bin/chats - sync, import, index, extract facts, and apply chat data
|
|
||||||
// from Telegram, WhatsApp, LinkedIn into the brain and OnlyOffice CRM.
|
|
||||||
//
|
|
||||||
// Usage:
|
|
||||||
//
|
|
||||||
// chats sync telegram [--limit N] [--since DATE] [--phone PHONE]
|
|
||||||
// chats sync whatsapp [--qr] [--limit N]
|
|
||||||
// chats sync linkedin [--limit N]
|
|
||||||
// chats import # JSONL → MD (all sources)
|
|
||||||
// chats index # rebuild var/kb.lbug with chats
|
|
||||||
// chats facts # extract + cross-check
|
|
||||||
// chats apply [--dry-run] # push to OnlyOffice CRM
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
if len(os.Args) < 2 {
|
|
||||||
usage()
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
cmd := os.Args[1]
|
|
||||||
args := os.Args[2:]
|
|
||||||
switch cmd {
|
|
||||||
case "sync":
|
|
||||||
if len(args) < 1 {
|
|
||||||
usage()
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
platform := args[0]
|
|
||||||
platformArgs := args[1:]
|
|
||||||
switch platform {
|
|
||||||
case "telegram":
|
|
||||||
os.Exit(runSyncTelegram(platformArgs))
|
|
||||||
case "whatsapp":
|
|
||||||
fmt.Fprintf(os.Stderr, "chats: WhatsApp not implemented yet\n")
|
|
||||||
os.Exit(1)
|
|
||||||
case "linkedin":
|
|
||||||
os.Exit(runSyncLinkedIn(platformArgs))
|
|
||||||
default:
|
|
||||||
fmt.Fprintf(os.Stderr, "chats: unknown platform %q\n", platform)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
case "import":
|
|
||||||
os.Exit(runImport(args))
|
|
||||||
case "index":
|
|
||||||
os.Exit(runIndex(args))
|
|
||||||
case "facts":
|
|
||||||
os.Exit(runFacts(args))
|
|
||||||
case "apply":
|
|
||||||
os.Exit(runApply(args))
|
|
||||||
case "help", "-h", "--help":
|
|
||||||
usage()
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
fmt.Fprintf(os.Stderr, "chats: unknown command %q\n", cmd)
|
|
||||||
usage()
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func usage() {
|
|
||||||
w := os.Stderr
|
|
||||||
fmt.Fprintln(w, `Usage: chats <command> [args]
|
|
||||||
|
|
||||||
Commands:
|
|
||||||
sync telegram [--limit N] [--since DATE] [--phone PHONE]
|
|
||||||
sync whatsapp [--qr] [--limit N]
|
|
||||||
sync linkedin [--limit N]
|
|
||||||
import JSONL → MD (all sources)
|
|
||||||
index rebuild var/kb.lbug with chats
|
|
||||||
facts extract + cross-check facts
|
|
||||||
apply [--dry-run] push to OnlyOffice CRM
|
|
||||||
|
|
||||||
Output layout:
|
|
||||||
var/chats/<platform>/<chat_id>/messages.jsonl
|
|
||||||
var/chats/md/<platform>/<chat_name>/messages.md`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// repoRoot locates the 2dph project root by walking up from the binary.
|
|
||||||
func repoRoot() string {
|
|
||||||
if v := os.Getenv("KB_ROOT"); v != "" {
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return "."
|
|
||||||
}
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
if _, err := os.Stat(wd + "/var"); err == nil {
|
|
||||||
return wd
|
|
||||||
}
|
|
||||||
if _, err := os.Stat(wd + "/.git"); err == nil {
|
|
||||||
return wd
|
|
||||||
}
|
|
||||||
parent := wd
|
|
||||||
if idx := strings.LastIndex(wd, "/"); idx >= 0 {
|
|
||||||
parent = wd[:idx]
|
|
||||||
}
|
|
||||||
if parent == wd {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
wd = parent
|
|
||||||
}
|
|
||||||
return "."
|
|
||||||
}
|
|
||||||
|
|
||||||
// chatsDir returns var/chats under the repo root.
|
|
||||||
func chatsDir() string {
|
|
||||||
return repoRoot() + "/var/chats"
|
|
||||||
}
|
|
||||||
Executable
+41
@@ -0,0 +1,41 @@
|
|||||||
|
//usr/bin/env go run -tags=chats_sync "$0" "$@"; exit
|
||||||
|
//go:build chats_sync
|
||||||
|
//
|
||||||
|
// bin/chats/sync.go - download chat messages to var/chats/<platform>/.
|
||||||
|
//
|
||||||
|
// ./bin/chats/sync.go telegram [--limit N] [--phone PHONE]
|
||||||
|
// ./bin/chats/sync.go linkedin [--limit N] [--refresh]
|
||||||
|
//
|
||||||
|
// NOTE: never run `gofmt -w` on this file — it breaks the shebang.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/eSlider/2dph/internal/chats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
fmt.Fprintln(os.Stderr, `usage: bin/chats/sync.go telegram|linkedin [flags]`)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
platform := os.Args[1]
|
||||||
|
args := os.Args[2:]
|
||||||
|
switch platform {
|
||||||
|
case "telegram":
|
||||||
|
os.Exit(chats.RunSyncTelegram(args))
|
||||||
|
case "linkedin":
|
||||||
|
os.Exit(chats.RunSyncLinkedIn(args))
|
||||||
|
case "whatsapp":
|
||||||
|
fmt.Fprintln(os.Stderr, "chats: WhatsApp not implemented yet")
|
||||||
|
os.Exit(1)
|
||||||
|
case "help", "-h", "--help":
|
||||||
|
fmt.Fprintln(os.Stderr, `usage: bin/chats/sync.go telegram|linkedin [flags]`)
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(os.Stderr, "chats: unknown platform %q\n", platform)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
// Deprecated shebang mains at bin root (serve.go is tagged brain_serve).
|
||||||
|
package main
|
||||||
+8
-13
@@ -1,27 +1,22 @@
|
|||||||
//usr/bin/env go run "$0" "$@"; exit
|
//usr/bin/env go run -tags=brain_serve "$0" "$@"; exit
|
||||||
// bin/serve.go - async Go HTTP server for the 2dph brain (see bin/server).
|
//go:build brain_serve
|
||||||
//
|
//
|
||||||
// KB_ROOT=/path/to/2dph ./bin/serve.go # serve the brain
|
// bin/serve.go — deprecated; use bin/brain/serve.go.
|
||||||
// KB_SEARCH_CMD=... KB_WORKERS=4 KB_PORT=8630 ./bin/serve.go
|
|
||||||
//
|
|
||||||
// Shebang trick: the first line is a Go `//` comment; when executed, env runs
|
|
||||||
// `go run "$0"` so this file doubles as an executable script. The real code
|
|
||||||
// lives in the importable package (module path, never a relative import).
|
|
||||||
// NOTE: never run `gofmt -w` on this file - it rewrites `//usr/bin/env` to
|
|
||||||
// `// usr/...` and breaks the shebang.
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/eSlider/2dph/bin/server"
|
"github.com/eSlider/2dph/internal/httpapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if env := os.Getenv("KB_ROOT"); env == "" {
|
fmt.Fprintln(os.Stderr, "bin/serve.go is deprecated; use bin/brain/serve.go")
|
||||||
|
if os.Getenv("KB_ROOT") == "" {
|
||||||
if wd, err := os.Getwd(); err == nil {
|
if wd, err := os.Getwd(); err == nil {
|
||||||
os.Setenv("KB_ROOT", wd)
|
os.Setenv("KB_ROOT", wd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
server.Run()
|
httpapi.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,3 +34,32 @@ class BinLayoutTest(unittest.TestCase):
|
|||||||
def test_no_main_go_under_bin_brain(self) -> None:
|
def test_no_main_go_under_bin_brain(self) -> None:
|
||||||
main = ROOT / "bin" / "brain" / "main.go"
|
main = ROOT / "bin" / "brain" / "main.go"
|
||||||
self.assertFalse(main.exists(), "bin/brain/main.go is not a method")
|
self.assertFalse(main.exists(), "bin/brain/main.go is not a method")
|
||||||
|
|
||||||
|
def test_chats_methods_are_shebangs_not_main(self) -> None:
|
||||||
|
chats = ROOT / "bin" / "chats"
|
||||||
|
self.assertFalse(
|
||||||
|
(chats / "main.go").exists(),
|
||||||
|
"bin/chats/main.go is a dispatcher, not a method",
|
||||||
|
)
|
||||||
|
self.assertFalse(
|
||||||
|
(chats / "index_cmd.go").exists(),
|
||||||
|
"chats index is a brain write hiding under the wrong subject",
|
||||||
|
)
|
||||||
|
for method in ("sync.go", "import.go", "facts.go", "apply.go"):
|
||||||
|
p = chats / method
|
||||||
|
self.assertTrue(p.is_file(), f"missing bin/chats/{method}")
|
||||||
|
first = p.read_text().splitlines()[0]
|
||||||
|
self.assertTrue(
|
||||||
|
first.startswith("//usr/bin/env go run"),
|
||||||
|
f"{method} shebang, got {first!r}",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_chats_lib_lives_in_internal(self) -> None:
|
||||||
|
self.assertTrue(
|
||||||
|
(ROOT / "internal" / "chats" / "linkedin.go").is_file(),
|
||||||
|
"LinkedIn parser must live in internal/chats",
|
||||||
|
)
|
||||||
|
self.assertFalse(
|
||||||
|
(ROOT / "bin" / "chats" / "linkedin.go").exists(),
|
||||||
|
"parser must not stay under bin/chats as a second main",
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
"""Published docs must match live commands (Gitea SoT, brain/search, no fake --hop)."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[2]
|
||||||
|
|
||||||
|
|
||||||
|
class PublishedDocsTest(unittest.TestCase):
|
||||||
|
def test_readme_points_issues_at_gitea(self) -> None:
|
||||||
|
text = (ROOT / "README.md").read_text()
|
||||||
|
self.assertIn(
|
||||||
|
"https://git.produktor.io/eSlider/2dph/issues",
|
||||||
|
text,
|
||||||
|
"README must point issues at Gitea",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_plan_d15_names_gitea_origin(self) -> None:
|
||||||
|
text = (ROOT / "PLAN.md").read_text()
|
||||||
|
self.assertIn("D15", text)
|
||||||
|
self.assertIn("git.produktor.io/eSlider/2dph", text)
|
||||||
|
|
||||||
|
def test_readme_primary_search_is_brain(self) -> None:
|
||||||
|
text = (ROOT / "README.md").read_text()
|
||||||
|
self.assertIn(
|
||||||
|
"bin/brain/search.go",
|
||||||
|
text,
|
||||||
|
"README deduction search must name bin/brain/search.go",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_docs_do_not_claim_hop_walks(self) -> None:
|
||||||
|
paths = [
|
||||||
|
ROOT / "README.md",
|
||||||
|
ROOT / "docs" / "design.md",
|
||||||
|
ROOT / "skills" / "kb-search" / "SKILL.md",
|
||||||
|
ROOT / "skills" / "diataxis-docs" / "SKILL.md",
|
||||||
|
]
|
||||||
|
# Command-style `--hop 1` / `--hop N` plus follow/walk = the old lie.
|
||||||
|
# Honest "not implemented" notes must not match.
|
||||||
|
lie = re.compile(r"--hop (?:N|1).*(?:follow|walk)", re.I | re.S)
|
||||||
|
for path in paths:
|
||||||
|
text = path.read_text()
|
||||||
|
self.assertIsNone(
|
||||||
|
lie.search(text),
|
||||||
|
f"{path.relative_to(ROOT)} still claims --hop walks the graph",
|
||||||
|
)
|
||||||
@@ -8,4 +8,7 @@ Brain/ops/eSlider stack. Facts need proof or they are
|
|||||||
- [design](design.md) — schema, deduction model, sources
|
- [design](design.md) — schema, deduction model, sources
|
||||||
- [Gitea issues](https://git.produktor.io/eSlider/2dph/issues) — work board (origin)
|
- [Gitea issues](https://git.produktor.io/eSlider/2dph/issues) — work board (origin)
|
||||||
|
|
||||||
|
Search: `bin/brain/search.go "query"` (HTTP: `bin/brain/serve.go`). `--hop` is
|
||||||
|
not a walk; the flag errors until File/FROM_FILE edges exist.
|
||||||
|
|
||||||
Published docs live here and mirror the project state.
|
Published docs live here and mirror the project state.
|
||||||
|
|||||||
@@ -15,9 +15,10 @@ OO_CLI (default: $HOME/go/bin/oo)
|
|||||||
## Quick reference
|
## Quick reference
|
||||||
|
|
||||||
```
|
```
|
||||||
./bin/chat sync telegram --limit 100
|
./bin/chats/sync.go telegram --limit 100
|
||||||
./bin/chat import
|
./bin/chats/import.go
|
||||||
./bin/chat index
|
./bin/chats/facts.go
|
||||||
./bin/chat facts
|
./bin/chats/apply.go --dry-run
|
||||||
./bin/chat apply --dry-run
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
JSONL → markdown only. Brain ingest is `bin/brain/index.go` (not a `chats index`).
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# CRM association proof (oo CLI ↔ corpus)
|
||||||
|
|
||||||
|
Proven with `oo` (eslider/go-onlyoffice) against the OnlyOffice portal
|
||||||
|
(`office.produktor.io`). Portal CRM is the SSOT for company ↔ person ↔
|
||||||
|
project associations; the corpus SoT (`eslider/cv/projects/knowledge-mesh-seed.yaml`)
|
||||||
|
is the second, independent source. Facts that can be backed by both are
|
||||||
|
written to the brain under `root=facts` by `bin/facts/crm`.
|
||||||
|
|
||||||
|
## What was verified
|
||||||
|
|
||||||
|
- Logical counts (portal MySQL): 1300 contacts = 897 persons + 404 companies,
|
||||||
|
198 projects, 998 deals, 939 project↔contact links.
|
||||||
|
- Every client company linked to a project has ≥1 person underneath.
|
||||||
|
- Every person `company_id` resolves to an existing company.
|
||||||
|
- Corpus org list (9) maps 1:1 onto CRM companies:
|
||||||
|
ProProdukt SL / produktor.io, Dyvenia, Immowelt AG, WhereGroup,
|
||||||
|
Keynote SIGOS, D2S/SYSTEMS, GRID, Pack und Cup, Markets Platform.
|
||||||
|
- 78 person↔company association facts written to the brain
|
||||||
|
(`how=crm-crosscheck`, `type=association`). Recall@5 in `bin/kb/eval` = 1.0.
|
||||||
|
|
||||||
|
## Mistakes found
|
||||||
|
|
||||||
|
| # | Mistake | Fix |
|
||||||
|
|---|---------|-----|
|
||||||
|
| 1 | Duplicate legal entity `GoldenRatio.Exchange` (contact 759) vs `Golden Ratio Exchange` (763); 3 deals (211, 287, 559) were linked to 759 | `oo contacts merge 759 763` — 763 kept, 759 removed, deal links re-pointed to 763 |
|
||||||
|
| 2 | `env/`-wide: OnlyOffice creds file used wrong UX (user `eslider`, password with `$2` suffix) making `oo` auth fail | `.env` fixed to `eslider@gmail.com` + clean password; `.env` stays gitignored |
|
||||||
|
|
||||||
|
## Gates after fix
|
||||||
|
|
||||||
|
- `uv run python -m unittest discover -s bin/tools -t .` → 26 tests OK
|
||||||
|
- `bin/facts/audit self` + `bin/facts/audit db` → ok
|
||||||
|
- `bin/kb/eval` → recall@5 = 1.0
|
||||||
|
- `go test ./...` (bin/server + bin/watch) → ok
|
||||||
+3
-3
@@ -17,14 +17,14 @@ as one consistent state.
|
|||||||
## Deduction search
|
## Deduction search
|
||||||
|
|
||||||
```
|
```
|
||||||
bin/kb/search "question"
|
bin/brain/search.go "question"
|
||||||
1. facts root — confirmed answers only → return with evidence links
|
1. facts root — confirmed answers only → return with evidence links
|
||||||
2. info root — supporting narrative → snippets, marked (not confirmed)
|
2. info root — supporting narrative → snippets, marked (not confirmed)
|
||||||
3. web-search — second independent source → upgrade hypothesis to confirmed
|
3. web-search — second independent source → upgrade hypothesis to confirmed
|
||||||
```
|
```
|
||||||
|
|
||||||
`--hop N` follows graph edges (sibling leaves under a heading, owning file,
|
`--hop` is not implemented yet (needs File/FROM_FILE edges). The flag is an
|
||||||
`related:` files, vector-neighbour leaves) — the deduction walk.
|
error; it is not a graph walk.
|
||||||
|
|
||||||
## Who / What / How / Where / When + evidence
|
## Who / What / How / Where / When + evidence
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -24,7 +24,7 @@ type ooContact struct {
|
|||||||
} `json:"commonData"`
|
} `json:"commonData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func runApply(args []string) int {
|
func RunApply(args []string) int {
|
||||||
fs := flag.NewFlagSet("chats apply", flag.ContinueOnError)
|
fs := flag.NewFlagSet("chats apply", flag.ContinueOnError)
|
||||||
dryRun := fs.Bool("dry-run", false, "show what would be done without writing")
|
dryRun := fs.Bool("dry-run", false, "show what would be done without writing")
|
||||||
help := fs.Bool("help", false, "")
|
help := fs.Bool("help", false, "")
|
||||||
@@ -176,7 +176,7 @@ func runApply(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadFacts() ([]ExtractedFact, error) {
|
func loadFacts() ([]ExtractedFact, error) {
|
||||||
factsPath := filepath.Join(chatsDir(), "facts", "chat-facts.json")
|
factsPath := filepath.Join(Dir(), "facts", "chat-facts.json")
|
||||||
data, err := os.ReadFile(factsPath)
|
data, err := os.ReadFile(factsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
// These are integration tests using real data and real Telegram API (when
|
// These are integration tests using real data and real Telegram API (when
|
||||||
// credentials are available). They follow the TDD workflow pattern:
|
// credentials are available). They follow the TDD workflow pattern:
|
||||||
// sync → import → facts → verify.
|
// sync → import → facts → verify.
|
||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -48,7 +48,7 @@ func TestChatsImport(t *testing.T) {
|
|||||||
t.Cleanup(func() { os.Chdir(cwd) })
|
t.Cleanup(func() { os.Chdir(cwd) })
|
||||||
t.Setenv("KB_ROOT", dir)
|
t.Setenv("KB_ROOT", dir)
|
||||||
|
|
||||||
exitCode := runImport([]string{})
|
exitCode := RunImport([]string{})
|
||||||
if exitCode != 0 {
|
if exitCode != 0 {
|
||||||
t.Fatalf("import exit code %d", exitCode)
|
t.Fatalf("import exit code %d", exitCode)
|
||||||
}
|
}
|
||||||
@@ -140,7 +140,7 @@ func TestChatsImportEmpty(t *testing.T) {
|
|||||||
t.Cleanup(func() { os.Chdir(cwd) })
|
t.Cleanup(func() { os.Chdir(cwd) })
|
||||||
t.Setenv("KB_ROOT", dir)
|
t.Setenv("KB_ROOT", dir)
|
||||||
|
|
||||||
exitCode := runImport([]string{})
|
exitCode := RunImport([]string{})
|
||||||
if exitCode == 0 {
|
if exitCode == 0 {
|
||||||
t.Fatal("expected non-zero exit for empty data dir")
|
t.Fatal("expected non-zero exit for empty data dir")
|
||||||
}
|
}
|
||||||
@@ -171,7 +171,7 @@ func TestChatsRoundTrip(t *testing.T) {
|
|||||||
t.Cleanup(func() { os.Chdir(cwd) })
|
t.Cleanup(func() { os.Chdir(cwd) })
|
||||||
t.Setenv("KB_ROOT", dir)
|
t.Setenv("KB_ROOT", dir)
|
||||||
|
|
||||||
if code := runImport([]string{}); code != 0 {
|
if code := RunImport([]string{}); code != 0 {
|
||||||
t.Fatalf("import exit %d", code)
|
t.Fatalf("import exit %d", code)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -77,7 +75,7 @@ type ExtractedFact struct {
|
|||||||
MessageID string `json:"message_id"`
|
MessageID string `json:"message_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func runFacts(args []string) int {
|
func RunFacts(args []string) int {
|
||||||
fs := flag.NewFlagSet("chats facts", flag.ContinueOnError)
|
fs := flag.NewFlagSet("chats facts", flag.ContinueOnError)
|
||||||
help := fs.Bool("help", false, "")
|
help := fs.Bool("help", false, "")
|
||||||
fs.SetOutput(os.Stderr)
|
fs.SetOutput(os.Stderr)
|
||||||
@@ -89,7 +87,7 @@ func runFacts(args []string) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
root := chatsDir()
|
root := Dir()
|
||||||
telegramDir := filepath.Join(root, "telegram")
|
telegramDir := filepath.Join(root, "telegram")
|
||||||
|
|
||||||
entries, err := os.ReadDir(telegramDir)
|
entries, err := os.ReadDir(telegramDir)
|
||||||
@@ -149,7 +147,7 @@ func runFacts(args []string) int {
|
|||||||
}
|
}
|
||||||
fmt.Printf("chats facts: saved to %s\n", factsPath)
|
fmt.Printf("chats facts: saved to %s\n", factsPath)
|
||||||
|
|
||||||
writeFactsToBrain(root, allFacts)
|
writeFactsMarkdown(allFacts)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@@ -273,14 +271,10 @@ func filterFacts(facts []ExtractedFact, factType string) []ExtractedFact {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeFactsToBrain(root string, facts []ExtractedFact) {
|
// writeFactsMarkdown stores a sidecar for humans. Brain ingest is
|
||||||
indexScript := filepath.Join(root, "bin", "kb", "index")
|
// bin/brain/index.go (not this subject).
|
||||||
if _, err := os.Stat(indexScript); os.IsNotExist(err) {
|
func writeFactsMarkdown(facts []ExtractedFact) {
|
||||||
fmt.Fprintf(os.Stderr, "chats facts: kb/index not found, skipping brain write\n")
|
mdDir := filepath.Join(Dir(), "facts")
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
mdDir := filepath.Join(chatsDir(), "facts")
|
|
||||||
if err := os.MkdirAll(mdDir, 0755); err != nil {
|
if err := os.MkdirAll(mdDir, 0755); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "chats facts: mkdir %s: %v\n", mdDir, err)
|
fmt.Fprintf(os.Stderr, "chats facts: mkdir %s: %v\n", mdDir, err)
|
||||||
return
|
return
|
||||||
@@ -288,7 +282,7 @@ func writeFactsToBrain(root string, facts []ExtractedFact) {
|
|||||||
|
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
sb.WriteString("---\n")
|
sb.WriteString("---\n")
|
||||||
sb.WriteString("root: facts\n")
|
sb.WriteString("root: info\n")
|
||||||
sb.WriteString("---\n\n")
|
sb.WriteString("---\n\n")
|
||||||
sb.WriteString("# Chat-Derived Facts\n\n")
|
sb.WriteString("# Chat-Derived Facts\n\n")
|
||||||
for _, f := range facts {
|
for _, f := range facts {
|
||||||
@@ -302,15 +296,5 @@ func writeFactsToBrain(root string, facts []ExtractedFact) {
|
|||||||
fmt.Fprintf(os.Stderr, "chats facts: write %s: %v\n", factsMD, err)
|
fmt.Fprintf(os.Stderr, "chats facts: write %s: %v\n", factsMD, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Printf("chats facts: markdown %s (index via brain, not chats)\n", factsMD)
|
||||||
cmd := exec.Command(indexScript, "--corpus", mdDir, "--skip-indexes")
|
|
||||||
var outBuf, errBuf bytes.Buffer
|
|
||||||
cmd.Stdout = &outBuf
|
|
||||||
cmd.Stderr = &errBuf
|
|
||||||
cmd.Dir = root
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "chats facts: brain index: %v\n%s", err, errBuf.String())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Printf("chats facts: written to brain (%s)\n", strings.TrimSpace(outBuf.String()))
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runImport(args []string) int {
|
func RunImport(args []string) int {
|
||||||
fs := flag.NewFlagSet("chats import", flag.ContinueOnError)
|
fs := flag.NewFlagSet("chats import", flag.ContinueOnError)
|
||||||
help := fs.Bool("help", false, "")
|
help := fs.Bool("help", false, "")
|
||||||
fs.SetOutput(os.Stderr)
|
fs.SetOutput(os.Stderr)
|
||||||
@@ -25,7 +25,7 @@ func runImport(args []string) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
root := chatsDir()
|
root := Dir()
|
||||||
mdRoot := filepath.Join(root, "md")
|
mdRoot := filepath.Join(root, "md")
|
||||||
glob := filepath.Join(root, "telegram", "*", "messages.jsonl")
|
glob := filepath.Join(root, "telegram", "*", "messages.jsonl")
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package chats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Root locates the 2dph project root (KB_ROOT, or walk up for var/ or .git).
|
||||||
|
func Root() string {
|
||||||
|
if v := os.Getenv("KB_ROOT"); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return "."
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
if _, err := os.Stat(wd + "/var"); err == nil {
|
||||||
|
return wd
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(wd + "/.git"); err == nil {
|
||||||
|
return wd
|
||||||
|
}
|
||||||
|
parent := wd
|
||||||
|
if idx := strings.LastIndex(wd, "/"); idx >= 0 {
|
||||||
|
parent = wd[:idx]
|
||||||
|
}
|
||||||
|
if parent == wd {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
wd = parent
|
||||||
|
}
|
||||||
|
return "."
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dir is var/chats under the project root.
|
||||||
|
func Dir() string {
|
||||||
|
return Root() + "/var/chats"
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -28,7 +28,7 @@ func checkLinkedInSession(userDataDir string) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runSyncLinkedIn(args []string) int {
|
func RunSyncLinkedIn(args []string) int {
|
||||||
fs := flag.NewFlagSet("chats sync linkedin", flag.ContinueOnError)
|
fs := flag.NewFlagSet("chats sync linkedin", flag.ContinueOnError)
|
||||||
limit := fs.Int("limit", 0, "max messages per conversation (0 = all)")
|
limit := fs.Int("limit", 0, "max messages per conversation (0 = all)")
|
||||||
refresh := fs.Bool("refresh", false, "refresh session from live webtop browser before sync")
|
refresh := fs.Bool("refresh", false, "refresh session from live webtop browser before sync")
|
||||||
@@ -72,7 +72,7 @@ func runSyncLinkedIn(args []string) int {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if err := src.Sync(ctx, chatsDir(), *limit); err != nil {
|
if err := src.Sync(ctx, Dir(), *limit); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "chats sync linkedin: %v\n", err)
|
fmt.Fprintf(os.Stderr, "chats sync linkedin: %v\n", err)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package chats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runSyncTelegram(args []string) int {
|
func RunSyncTelegram(args []string) int {
|
||||||
fs := flag.NewFlagSet("chats sync telegram", flag.ContinueOnError)
|
fs := flag.NewFlagSet("chats sync telegram", flag.ContinueOnError)
|
||||||
limit := fs.Int("limit", 0, "max messages per chat (0 = all)")
|
limit := fs.Int("limit", 0, "max messages per chat (0 = all)")
|
||||||
phone := fs.String("phone", "", "phone number (default env TELEGRAM_PHONE)")
|
phone := fs.String("phone", "", "phone number (default env TELEGRAM_PHONE)")
|
||||||
@@ -78,7 +78,7 @@ func runSyncTelegram(args []string) int {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if err := src.Sync(ctx, chatsDir(), *limit); err != nil {
|
if err := src.Sync(ctx, Dir(), *limit); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "chats sync telegram: %v\n", err)
|
fmt.Fprintf(os.Stderr, "chats sync telegram: %v\n", err)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
Vendored
@@ -2,15 +2,10 @@
|
|||||||
//
|
//
|
||||||
// Async by design: every request runs on its own goroutine, and CPU-heavy
|
// Async by design: every request runs on its own goroutine, and CPU-heavy
|
||||||
// searches are serialized through a bounded worker pool (a counting
|
// searches are serialized through a bounded worker pool (a counting
|
||||||
// semaphore) so N requests can't spawn N Python interpreters at once.
|
// semaphore) so N requests can't spawn N search processes at once.
|
||||||
//
|
//
|
||||||
// Used by bin/serve.go which is a self-executing shebang script:
|
// Used by bin/brain/serve.go.
|
||||||
//
|
package httpapi
|
||||||
// ///usr/bin/env go run "$0" "$@"; exit
|
|
||||||
// package main
|
|
||||||
// import "github.com/eSlider/2dph/bin/server"
|
|
||||||
// func main() { server.Run() }
|
|
||||||
package server
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -100,8 +95,8 @@ func writeRaw(w http.ResponseWriter, code int, body []byte) {
|
|||||||
w.Write(body)
|
w.Write(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// brainSearcher shells out to bin/kb/search --json. A single python search
|
// brainSearcher shells out to the Go brain-search binary (not Python).
|
||||||
// is bounded and short-lived; the worker pool keeps at most N live.
|
// A single search is bounded and short-lived; the worker pool keeps at most N live.
|
||||||
type brainSearcher struct {
|
type brainSearcher struct {
|
||||||
cmdPath string
|
cmdPath string
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
@@ -122,15 +117,18 @@ func (b *brainSearcher) Search(ctx context.Context, query string, limit int) ([]
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run starts the HTTP server. Reads env: KB_SEARCH_CMD (default bin/kb/search,
|
func defaultSearchCmd(root string) string {
|
||||||
// relative to the repo root given by KB_ROOT), KB_WORKERS (default 4), KB_PORT
|
if env := os.Getenv("KB_SEARCH_CMD"); env != "" {
|
||||||
// (default 8630).
|
return env
|
||||||
|
}
|
||||||
|
return filepath.Join(root, "var", "bin", "brain-search")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run starts the HTTP server. Reads env: KB_SEARCH_CMD (default
|
||||||
|
// $KB_ROOT/var/bin/brain-search), KB_WORKERS (default 4), KB_PORT (default 8630).
|
||||||
func Run() {
|
func Run() {
|
||||||
root := os.Getenv("KB_ROOT")
|
root := os.Getenv("KB_ROOT")
|
||||||
searchPath := os.Getenv("KB_SEARCH_CMD")
|
searchPath := defaultSearchCmd(root)
|
||||||
if searchPath == "" {
|
|
||||||
searchPath = filepath.Join(root, "bin", "kb", "search")
|
|
||||||
}
|
|
||||||
workers := 4
|
workers := 4
|
||||||
if raw := os.Getenv("KB_WORKERS"); raw != "" {
|
if raw := os.Getenv("KB_WORKERS"); raw != "" {
|
||||||
if n, err := strconv.Atoi(raw); err == nil && n > 0 {
|
if n, err := strconv.Atoi(raw); err == nil && n > 0 {
|
||||||
@@ -147,7 +145,7 @@ func Run() {
|
|||||||
searcher := &brainSearcher{cmdPath: searchPath, timeout: 60 * time.Second}
|
searcher := &brainSearcher{cmdPath: searchPath, timeout: 60 * time.Second}
|
||||||
handler := NewServer(searcher, workers)
|
handler := NewServer(searcher, workers)
|
||||||
addr := "127.0.0.1:" + strconv.Itoa(port)
|
addr := "127.0.0.1:" + strconv.Itoa(port)
|
||||||
log.Printf("serve: %s (workers=%d)", addr, workers)
|
log.Printf("serve: %s (workers=%d cmd=%s)", addr, workers, searchPath)
|
||||||
if err := http.ListenAndServe(addr, handler); err != nil {
|
if err := http.ListenAndServe(addr, handler); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
package server
|
package httpapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -141,6 +142,17 @@ func TestSearchRejectsBadLimit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultSearchCmdIsBrainNotPython(t *testing.T) {
|
||||||
|
t.Setenv("KB_SEARCH_CMD", "")
|
||||||
|
cmd := defaultSearchCmd("/repo")
|
||||||
|
if strings.Contains(strings.ToLower(cmd), "python") {
|
||||||
|
t.Fatalf("search path still python: %s", cmd)
|
||||||
|
}
|
||||||
|
if !strings.Contains(cmd, "brain") {
|
||||||
|
t.Fatalf("search path must be the Go brain binary, got %s", cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSearchTimeout(t *testing.T) {
|
func TestSearchTimeout(t *testing.T) {
|
||||||
fs := &fakeSearcher{delay: time.Second}
|
fs := &fakeSearcher{delay: time.Second}
|
||||||
h := NewServer(fs, 1)
|
h := NewServer(fs, 1)
|
||||||
@@ -30,12 +30,11 @@ related:
|
|||||||
---
|
---
|
||||||
```
|
```
|
||||||
|
|
||||||
`bin/kb/index` reads this. `type` becomes a searchable column and `related`
|
`bin/kb/index` reads this. `type` becomes a searchable column. `related:` is
|
||||||
becomes a graph edge:
|
frontmatter for humans; graph hops from it are not implemented yet.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bin/kb/search "deploy" --type howto
|
bin/brain/search.go "deploy"
|
||||||
bin/kb/search "Stecktafel" --hop 1 # follow links and related
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Audit checklist
|
## Audit checklist
|
||||||
@@ -46,8 +45,7 @@ bin/kb/search "Stecktafel" --hop 1 # follow links and related
|
|||||||
The indexer chunks on H2, so split files also search better.
|
The indexer chunks on H2, so split files also search better.
|
||||||
3. Is `status: archive` set on anything superseded? Archived files stay indexed
|
3. Is `status: archive` set on anything superseded? Archived files stay indexed
|
||||||
but stop competing with current ones for a reader's attention.
|
but stop competing with current ones for a reader's attention.
|
||||||
4. Does every explanation link the reference it explains, and vice versa? That
|
4. Does every explanation link the reference it explains, and vice versa?
|
||||||
link is what `--hop 1` walks.
|
|
||||||
|
|
||||||
## Rule
|
## Rule
|
||||||
|
|
||||||
|
|||||||
+13
-12
@@ -2,9 +2,10 @@
|
|||||||
name: kb-search
|
name: kb-search
|
||||||
description: >-
|
description: >-
|
||||||
Deduction search over the 2dph brain (Ladybug graph: ops corpus, portfolio,
|
Deduction search over the 2dph brain (Ladybug graph: ops corpus, portfolio,
|
||||||
ssh hosts) with bin/kb/search instead of reading files or grepping repos.
|
ssh hosts) with bin/brain/search.go instead of reading files or grepping
|
||||||
Use whenever a question starts with "where is", "what runs on", "which file
|
repos. Use whenever a question starts with "where is", "what runs on",
|
||||||
describes", "who is", "how is X done", before opening any documentation.
|
"which file describes", "who is", "how is X done", before opening any
|
||||||
|
documentation.
|
||||||
---
|
---
|
||||||
|
|
||||||
# kb-search — deduction over facts and info
|
# kb-search — deduction over facts and info
|
||||||
@@ -22,15 +23,17 @@ second independent source when local roots cannot confirm. An answer is
|
|||||||
`(not confirmed)`.
|
`(not confirmed)`.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bin/kb/search "Matrix federation" # pointers + snippets, YAML
|
bin/brain/search.go "Matrix federation" # pointers + snippets, YAML
|
||||||
bin/kb/search "what runs on arc-2" --hop 1 # follow graph edges
|
bin/brain/search.go "onlyoffice postgres" --root facts # restrict to confirmed
|
||||||
bin/kb/search "onlyoffice postgres" --root facts # restrict to confirmed
|
bin/brain/search.go "where is cs-lexicon" --json | yq '.[].ref'
|
||||||
bin/kb/search "where is cs-lexicon" --json | yq '.[].ref'
|
bin/kb/get <id> --body # full chunk only when needed
|
||||||
bin/kb/get <id> --body # full chunk only when needed
|
bin/kb/stats # index health
|
||||||
bin/kb/stats # index health
|
bin/kb/eval # recall@5 >= 0.95 gate
|
||||||
bin/kb/eval # recall@5 >= 0.95 gate
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`bin/kb/search` is a deprecated wrapper. `--hop` errors (File/FROM_FILE edges
|
||||||
|
are not wired yet); do not treat it as a graph walk.
|
||||||
|
|
||||||
## Rules
|
## Rules
|
||||||
|
|
||||||
- Search before you read. Never grep a repo for a concept the graph covers.
|
- Search before you read. Never grep a repo for a concept the graph covers.
|
||||||
@@ -38,8 +41,6 @@ bin/kb/eval # recall@5 >= 0.95 gate
|
|||||||
facts first, then info leafs clearly marked `(not confirmed)`.
|
facts first, then info leafs clearly marked `(not confirmed)`.
|
||||||
- If recall looks wrong, run `bin/kb/eval`; it gates control questions and
|
- If recall looks wrong, run `bin/kb/eval`; it gates control questions and
|
||||||
should stay at or above 95% recall@5.
|
should stay at or above 95% recall@5.
|
||||||
- `--hop N` follows sibling leaves, owning files, `related:` links and
|
|
||||||
vector-neighbours — that is the deduction walk, not random expansion.
|
|
||||||
- Escalate to `web-search` (the `web-search` skill) as the independent second
|
- Escalate to `web-search` (the `web-search` skill) as the independent second
|
||||||
source when both local roots cannot confirm; never report an unconfirmed
|
source when both local roots cannot confirm; never report an unconfirmed
|
||||||
single-source local answer as fact.
|
single-source local answer as fact.
|
||||||
@@ -18,9 +18,10 @@ bin/web/search "postgres partial index" --lang en --fresh year
|
|||||||
|
|
||||||
## Web or knowledge base
|
## Web or knowledge base
|
||||||
|
|
||||||
`bin/kb/search` holds our own facts: the ops stack, portfolio, ssh hosts, the
|
`bin/brain/search.go` holds our own facts: the ops stack, portfolio, ssh hosts,
|
||||||
lexicon. Go there first. Reach for `bin/web/search` when the answer is outside
|
the lexicon. Go there first. Reach for `bin/web/search` when the answer is
|
||||||
our repos: upstream library behaviour, vendor documentation, public standards.
|
outside our repos: upstream library behaviour, vendor documentation, public
|
||||||
|
standards.
|
||||||
|
|
||||||
Keep the two apart. A finding is stronger when the reader can see that one
|
Keep the two apart. A finding is stronger when the reader can see that one
|
||||||
source was ours and one was not.
|
source was ours and one was not.
|
||||||
|
|||||||
Reference in New Issue
Block a user