This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
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

195 lines
4.4 KiB
Go

package server
import (
"context"
"io"
"net"
"sync"
)
type TestDatabaseServer interface {
DatabaseServer
Snapshot() Counters
}
type Counters struct {
IDCalls int64
DSNCalls int64
StartCalls int64
StopCalls int64
RunningCalls int64
DialCalls int64
DialErrors int64
AcceptedConns int64 // cumulative
OpenConns int64 // current (not cumulative)
BytesIn int64 // bytes the handler read from the proxy side
BytesOut int64 // bytes the handler wrote back
}
// TestDatabaseServerImpl is the in-memory pipe-based implementation of
// TestDatabaseServer. Behavior knobs (StartErr, DialErr, Handler, ...) are
// exported fields; tests set them directly on the concrete type before
// exercising the proxy. They should not be mutated while the proxy is
// running.
type TestDatabaseServerImpl struct {
ID_ string // returned by ID(); default "test-server"
DSN_ string // returned by DSN(); default "test://in-memory"
StartErr error // if non-nil, Start returns it (and does not mark started)
StopErr error // if non-nil, Stop returns it after closing conns
DialErr error // if non-nil, Dial returns it
Handler func(backend net.Conn) // runs once per Dial; default EchoHandler
mu sync.Mutex
counters Counters
started bool
openConns []net.Conn
}
var _ TestDatabaseServer = (*TestDatabaseServerImpl)(nil)
func New() *TestDatabaseServerImpl {
return &TestDatabaseServerImpl{
ID_: "test-server",
DSN_: "test://in-memory",
Handler: EchoHandler,
}
}
// EchoHandler reads bytes from c and writes them back. Default Dial handler.
func EchoHandler(c net.Conn) {
defer func() { _ = c.Close() }()
_, _ = io.Copy(c, c)
}
// DiscardHandler reads bytes from c and throws them away. Useful when tests
// only care that the proxy attempted Dial / forwarded one direction.
func DiscardHandler(c net.Conn) {
defer func() { _ = c.Close() }()
_, _ = io.Copy(io.Discard, c)
}
// SetDialErr atomically replaces DialErr. Use to flip Dial behavior after
// the proxy has already reached readiness.
func (s *TestDatabaseServerImpl) SetDialErr(err error) {
s.mu.Lock()
s.DialErr = err
s.mu.Unlock()
}
func (s *TestDatabaseServerImpl) Snapshot() Counters {
s.mu.Lock()
defer s.mu.Unlock()
return s.counters
}
func (s *TestDatabaseServerImpl) ID(_ context.Context) string {
s.mu.Lock()
s.counters.IDCalls++
s.mu.Unlock()
return s.ID_
}
func (s *TestDatabaseServerImpl) DSN(_ context.Context, _, _, _ string) string {
s.mu.Lock()
s.counters.DSNCalls++
s.mu.Unlock()
return s.DSN_
}
func (s *TestDatabaseServerImpl) Start(_ context.Context) error {
s.mu.Lock()
s.counters.StartCalls++
if s.StartErr == nil {
s.started = true
}
err := s.StartErr
s.mu.Unlock()
return err
}
func (s *TestDatabaseServerImpl) Stop(_ context.Context) error {
s.mu.Lock()
s.counters.StopCalls++
s.started = false
conns := s.openConns
s.openConns = nil
err := s.StopErr
s.mu.Unlock()
for _, c := range conns {
_ = c.Close()
}
return err
}
func (s *TestDatabaseServerImpl) Running(_ context.Context) bool {
s.mu.Lock()
defer s.mu.Unlock()
s.counters.RunningCalls++
return s.started
}
func (s *TestDatabaseServerImpl) Dial(_ context.Context) (net.Conn, error) {
s.mu.Lock()
s.counters.DialCalls++
if s.DialErr != nil {
s.counters.DialErrors++
err := s.DialErr
s.mu.Unlock()
return nil, err
}
s.mu.Unlock()
proxySide, backendSide := net.Pipe()
wrapped := &countingConn{Conn: backendSide, s: s}
s.mu.Lock()
s.openConns = append(s.openConns, wrapped)
s.counters.AcceptedConns++
s.counters.OpenConns++
s.mu.Unlock()
go func() {
defer s.untrackConn(wrapped)
s.Handler(wrapped)
}()
return proxySide, nil
}
func (s *TestDatabaseServerImpl) untrackConn(c net.Conn) {
s.mu.Lock()
for i, oc := range s.openConns {
if oc == c {
s.openConns = append(s.openConns[:i], s.openConns[i+1:]...)
break
}
}
s.counters.OpenConns--
s.mu.Unlock()
}
type countingConn struct {
net.Conn
s *TestDatabaseServerImpl
}
func (c *countingConn) Read(b []byte) (int, error) {
n, err := c.Conn.Read(b)
if n > 0 {
c.s.mu.Lock()
c.s.counters.BytesIn += int64(n)
c.s.mu.Unlock()
}
return n, err
}
func (c *countingConn) Write(b []byte) (int, error) {
n, err := c.Conn.Write(b)
if n > 0 {
c.s.mu.Lock()
c.s.counters.BytesOut += int64(n)
c.s.mu.Unlock()
}
return n, err
}