func (m *Manager) Start(ctx context.Context) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.running {
m.logger.Info("KNIRVHASHER already running")
return nil
}
if m.config.BinaryPath == "" {
m.config.BinaryPath = "./knirvhasher"
}
resolved, err := resolveBinaryPath(m.config.BinaryPath)
if err != nil {
return fmt.Errorf("KNIRVHASHER binary not found: %w", err)
}
m.config.BinaryPath = resolved
hasherDir := filepath.Dir(m.socketPath)
if hasherDir != "." && hasherDir != "/" {
if err := os.MkdirAll(hasherDir, 0755); err != nil {
return fmt.Errorf("failed to create socket directory: %w", err)
}
}
if _, err := os.Stat(m.socketPath); err == nil {
os.Remove(m.socketPath)
}
m.logger.Info("Starting KNIRVHASHER",
zap.String("binary", m.config.BinaryPath),
zap.String("socket", m.socketPath))
// --- ENVIRONMENT VARIABLES ---
env := os.Environ()
env = append(env,
fmt.Sprintf("HASHER_SOCKET_PATH=%s", m.socketPath),
fmt.Sprintf("HASHER_DATA_PATH=%s", m.config.DataPath),
)
if m.config.GRPCSocketPath != "" {
env = append(env, fmt.Sprintf("HASHER_GRPC_SOCKET_PATH=%s", m.config.GRPCSocketPath))
}
if appDataDir := os.Getenv("KNIRV_APP_DATA_DIR"); appDataDir != "" {
env = append(env, fmt.Sprintf("KNIRV_APP_DATA_DIR=%s", appDataDir))
} else if _, err := os.Stat("/var/lib/knirvserver"); err == nil {
env = append(env, "KNIRV_APP_DATA_DIR=/var/lib/knirvserver")
}
if m.config.ArxivEnabled {
env = append(env, "DATAMINER_MODE=production", "ARXIV_ENABLED=true")
}
// When running as root (e.g. via sudo), propagate the original user's Python
// site-packages so the data-miner's embedded Python can find en_core_web_sm.
if pyPath := sudoUserPythonPath(); pyPath != "" {
env = append(env, "PYTHONPATH="+pyPath)
}
for k, v := range m.config.EnvOverrides {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
// --- COMMAND LINE ARGUMENTS ---
args := []string{}
if m.config.HeadlessMode {
socketPerm := fmt.Sprintf("%04o", m.config.SocketPerm)
args = append(args,
"--headless",
fmt.Sprintf("--socket-path=%s", m.socketPath),
fmt.Sprintf("--socket-perm=%s", socketPerm),
)
}
// Default pipeline: MAPPER mode via Hugging Face API.
// Data-connector (CONNECTION source) only runs after full user onboarding.
if m.config.PipelineType == "goat" {
args = append(args, "-goat")
}
m.cmd = exec.Command(m.config.BinaryPath, args...)
m.cmd.Env = env
if w, ok := m.config.Stdout.(io.Writer); ok {
m.cmd.Stdout = w
} else {
m.cmd.Stdout = os.Stdout
}
if w, ok := m.config.Stderr.(io.Writer); ok {
m.cmd.Stderr = w
} else {
m.cmd.Stderr = os.Stderr
}
m.cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if err := m.cmd.Start(); err != nil {
return fmt.Errorf("failed to start KNIRVHASHER: %w", err)
}
m.startTime = time.Now()
m.logger.Info("KNIRVHASHER subprocess started",
zap.Int("pid", m.cmd.Process.Pid))
if err := m.waitForSocket(ctx); err != nil {
m.cmd.Process.Kill()
return fmt.Errorf("socket creation failed: %w", err)
}
if m.config.HeadlessMode {
m.logger.Info("KNIRVHASHER Unix socket ready")
// Pipeline is NOT auto-started here. The user triggers it via the
// frontend "Start Training" button, which calls hasherStart → Start()
// followed by RunPipeline() on the Manager, which POSTs to the
// headless server's /api/v1/pipeline/run endpoint.
}
m.running = true
m.logger.Info("KNIRVHASHER started successfully",
zap.String("socket", m.socketPath))
return nil
}
3. Command Line Arguments and Environment Variables
Command-Line Arguments (lines 183-196)
Arg
Value
Condition
--headless
(flag, no value)
HeadlessMode == true
--socket-path=<path>
From m.socketPath
HeadlessMode == true
--socket-perm=<octal>
e.g. 0660
HeadlessMode == true
-goat
(flag, no value)
PipelineType == "goat"
The CLI binary that parses these is at:
/home/gperry/Documents/GitHub/KNIRV/KNIRV_NETWORK/packages/KNIRVHASHER/cmd/cli/main.go, lines 36-42:
go
headlessMode = flag.Bool("headless", false, "run in headless mode with HTTP API server over Unix socket")
socketPath = flag.String("socket-path", "/var/run/knirvhasher.sock", "Unix socket path for headless API server")
socketPerm = flag.String("socket-perm", "0660", "Unix socket permissions (octal)")
goatMode = flag.Bool("goat", false, "MAPPER mode: use Hugging Face API as data source")
Environment Variables (lines 154-181)
Variable
Source
Purpose
HASHER_SOCKET_PATH
m.config.SocketPath
Unix socket for headless HTTP API
HASHER_DATA_PATH
m.config.DataPath
Data directory for KNIRVHASHER
HASHER_GRPC_SOCKET_PATH
m.config.GRPCSocketPath
gRPC socket for data-connector to dial KNIRVSERVER
KNIRV_APP_DATA_DIR
os.Getenv or /var/lib/knirvserver
App data directory
DATAMINER_MODE=production
ArxivEnabled
Production mode for data-miner
ARXIV_ENABLED=true
ArxivEnabled
ArXiv paper mining enabled
PYTHONPATH
sudoUserPythonPath()
Python site-packages for spaCy models
EnvOverrides (populated in main.go lines 1479-1497)
These come from the root.key secrets configuration in backend/cmd/backend_server/main.go:
Variable
Source
DEVICE_IP
rootKeySecrets.DeviceIp
DEVICE_PASSWORD
rootKeySecrets.DevicePassword
DEVICE_USERNAME
rootKeySecrets.DeviceUsername
CLOUDFLARE_EMBEDDINGS_URL
rootKeySecrets.CloudflareEmbeddingsUrl
FRAMES_DIR
hasherConfig.FramesDir (from server config YAML)
4. How the HEARTService is Started
HEARTService runs as a completely independent, standalone binary -- it is NOT started within the knirvhasher subprocess. There is no code path in the knirvhasher CLI (cmd/cli/main.go) or the headless server that launches HEARTService.
POST /api/v1/pipeline/run -- triggers pipeline (see RunPipeline() at manager.go lines 430-464)
GET /api/v1/health -- health check (see GetHealth() at manager.go lines 466-488)
POST /api/v1/pipeline/stop -- stops pipeline (see StopPipeline() at manager.go lines 490-512)
Headless server routes (from /home/gperry/Documents/GitHub/KNIRV/KNIRV_NETWORK/packages/KNIRVHASHER/internal/cli/headless/server.go, lines 62-89):
code
GET /api/v1/status
GET /api/v1/health
POST /api/v1/driver/start
POST /api/v1/driver/stop
GET /api/v1/driver/status
POST /api/v1/pipeline/run
GET /api/v1/pipeline/status
GET /api/v1/pipeline/logs (SSE stream)
POST /api/v1/verify
POST /api/v1/asic/discover
POST /api/v1/asic/probe
POST /api/v1/asic/protocol
POST /api/v1/asic/provision
POST /api/v1/asic/troubleshoot
POST /api/v1/asic/configure
POST /api/v1/asic/test
POST /api/v1/rules
POST /api/v1/chat
POST /api/v1/shutdown
B. gRPC over Unix Socket (KNIRVSERVER side -- Used by data-connector subprocess)
Socket:ManagerConfig.GRPCSocketPath (unique path, e.g. /var/lib/knirvserver/sockets/hasher-grpc.sock)
Server:HasherGRPCServer at /home/gperry/Documents/GitHub/KNIRV/KNIRV_NETWORK/packages/KNIRVSERVER/backend/internal/services/dvemanager/hasher_grpc_server.go
Client:HasherIntegration at /home/gperry/Documents/GitHub/KNIRV/KNIRV_NETWORK/packages/KNIRVSERVER/backend/internal/services/dvemanager/hasher_integration.go
Direction: The gRPC server runs inside KNIRVSERVER (backend_server). The data-connector subprocess (launched by the pipeline controller inside the knirvhasher subprocess) connects back to KNIRVSERVER via this gRPC socket to receive exported data (see data-connector's LoadConfig() which reads HASHER_GRPC_SOCKET_PATH env var). The HasherIntegration is a client-side wrapper that KNIRVSERVER's own code uses to call into this same gRPC server.
C. HTTP over TCP (hasher-host -- Separate from Manager control)
Port: Auto-detected, written to /tmp/hasher-host.port
Protocol: Gin HTTP API on TCP
Binary:hasher-host at cmd/driver/hasher-host/main.go
Endpoints:/api/v1/infer, /api/v1/batch, /api/v1/health, /api/v1/chat, /api/v1/train, etc.
Note: This is a separate service from the headless mode. The knirvhasher CLI (when in interactive/TUI mode) starts hasher-host as a subprocess. The headless mode does NOT start hasher-host unless explicitly triggered via POST /api/v1/driver/start.
Data Flow Summary
code
KNIRVSERVER (backend_server)
│
├── (1) Manager.Start() launches: knirvhasher --headless --socket-path=... -goat
│ │
│ └── knirvhasher subprocess (cmd/cli/main.go headless mode):
│ │
│ ├── Listens on Unix socket for HTTP API (/api/v1/*)
│ │ ◄── Manager.RunPipeline() sends POST /api/v1/pipeline/run
│ │
│ └── Controller.RunPipeline() launches pipeline stages:
│ ├── data-connector ──gRPC──► KNIRVSERVER (HasherGRPCServer)
│ ├── data-miner
│ ├── data-encoder
│ └── data-trainer
│
├── (2) HasherGRPCServer (Unix gRPC socket at GRPCSocketPath)
│ │
│ ├── HasherIntegration (KNIRVSERVER internal client)
│ └── data-connector (knirvhasher subprocess pipeline stage)
│
└── (3) HEARTService (completely independent binary, NOT started by Manager)
└── HTTP server at :8090 with /heart/* endpoints
The HEARTService is not integrated into the Manager's startup flow. It is a standalone binary at cmd/heart/main.go that would need to be started separately (e.g., as a sidecar process or systemd service). It is unrelated to the knirvhasher subprocess lifecycle managed by Manager.Start()/Manager.Stop().