caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

154 lines
5.2 KiB
Go

//go:build !windows
package doltserver
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
)
// portConflictHint is the platform-specific command to diagnose port conflicts.
// Used in error messages when a port is busy but the occupying process can't be identified.
const portConflictHint = "lsof -i :%d"
// processListHint is the platform-specific command to list dolt processes.
// Used in error messages when too many dolt servers are running.
const processListHint = "pgrep -la 'dolt sql-server'"
// procAttrDetached returns SysProcAttr to detach a child process from the parent
// process group so it survives parent exit.
func procAttrDetached() *syscall.SysProcAttr {
return &syscall.SysProcAttr{Setpgid: true}
}
// findPIDOnPort returns the PID of the process listening on a TCP port.
// Uses lsof to look up the listener. Returns 0 if no process found or on error.
func findPIDOnPort(port int) int {
out, err := exec.Command("lsof", "-ti", fmt.Sprintf(":%d", port), "-sTCP:LISTEN").Output() //nolint:gosec // G702: port is internal int, not user input
if err != nil {
return 0
}
// lsof may return multiple PIDs; take the first one
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if pid, err := strconv.Atoi(strings.TrimSpace(line)); err == nil && pid > 0 {
return pid
}
}
return 0
}
// listDoltProcessPIDs returns PIDs of all running dolt sql-server processes.
// Excludes zombies and defunct processes. Callers derive count (len) and
// membership (linear scan) from the returned slice.
//
// The pgrep pattern uses `dolt.*sql-server` rather than the literal
// `dolt sql-server` so it still matches when top-level dolt flags appear
// between the binary name and the subcommand — e.g. debug mode launches
// the server as `dolt --prof cpu --prof-path /…/dolt-pprof sql-server …`,
// in which `dolt sql-server` no longer appears as a contiguous substring.
// Without this, IsRunning's isDoltProcess check would reject the PID as
// "not a dolt process" and wipe the PID/port files of a healthy server,
// breaking `bd dolt status` and auto-start reattachment. The per-PID
// substring check below still requires both "dolt" and "sql-server" in
// the cmdline, so this only widens the first-stage filter; it does not
// loosen identity validation.
func listDoltProcessPIDs() []int {
out, err := exec.Command("pgrep", "-f", "dolt.*sql-server").Output()
if err != nil {
return nil
}
var pids []int
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
pid, err := strconv.Atoi(strings.TrimSpace(line))
if err != nil || pid <= 0 {
continue
}
// Exclude zombies: ps -o state= returns Z for zombie, X for dead
stateOut, err := exec.Command("ps", "-p", strconv.Itoa(pid), "-o", "state=").Output()
if err != nil {
continue
}
state := strings.TrimSpace(string(stateOut))
if len(state) > 0 && (state[0] == 'Z' || state[0] == 'X') {
continue
}
// Verify command line contains both "dolt" and "sql-server"
cmdOut, err := exec.Command("ps", "-p", strconv.Itoa(pid), "-o", "command=").Output()
if err != nil {
continue
}
cmdline := strings.TrimSpace(string(cmdOut))
if strings.Contains(cmdline, "dolt") && strings.Contains(cmdline, "sql-server") {
pids = append(pids, pid)
}
}
return pids
}
// isProcessInDir checks if a process's working directory matches the given path.
// Uses lsof to look up the CWD, which is more reliable than checking command-line
// args since dolt sql-server is started with cmd.Dir (not a --data-dir flag).
func isProcessInDir(pid int, dir string) bool {
// On macOS, lsof requires -a to AND selectors together; without it,
// "-p <pid>" and "-d cwd" can yield cwd entries from unrelated processes.
out, err := exec.Command("lsof", "-a", "-p", strconv.Itoa(pid), "-d", "cwd", "-Fn").Output()
if err != nil {
return false
}
absDir, _ := filepath.Abs(dir)
// lsof -Fn output format: "p<pid>\nfcwd\nn<path>"
for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "n") {
cwd := strings.TrimSpace(line[1:])
absCwd, _ := filepath.Abs(cwd)
if absCwd == absDir {
return true
}
}
}
return false
}
// isProcessAlive checks if a process with the given PID is running.
// Uses signal 0 which doesn't send a signal but checks process existence.
func isProcessAlive(pid int) bool {
process, err := os.FindProcess(pid)
if err != nil {
return false
}
return process.Signal(syscall.Signal(0)) == nil
}
// gracefulStop sends SIGTERM, waits for the process to exit, then SIGKILL if needed.
// Used by reclaimPort and StopWithForce where data has already been flushed.
func gracefulStop(pid int, timeout time.Duration) error {
process, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("finding process %d: %w", pid, err)
}
if err := process.Signal(syscall.SIGTERM); err != nil {
return fmt.Errorf("sending SIGTERM to PID %d: %w", pid, err)
}
// Poll for exit
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
time.Sleep(500 * time.Millisecond)
if process.Signal(syscall.Signal(0)) != nil {
return nil // exited
}
}
// Still running — force kill
_ = process.Signal(syscall.SIGKILL)
time.Sleep(100 * time.Millisecond)
return nil
}