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

102 lines
2.9 KiB
Go

package setup
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/steveyegge/beads/internal/utils"
)
var errRefuseSymlinkWrite = errors.New("refusing to write through symlink")
// atomicWriteFile writes data to a file atomically using a unique temporary file.
// This prevents race conditions when multiple processes write to the same file.
// If path is a symlink, the write is refused; callers that deliberately want
// write-through behavior should use atomicWriteFileFollowingSymlink.
// An optional permissions argument sets the file mode (default 0644).
//
//nolint:unparam // perm is intentionally variadic for callers that need non-default permissions
func atomicWriteFile(path string, data []byte, perm ...os.FileMode) error {
if info, err := os.Lstat(path); err == nil && info.Mode()&os.ModeSymlink != 0 {
return fmt.Errorf("%w: %s", errRefuseSymlinkWrite, path)
} else if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("inspect path: %w", err)
}
return atomicWriteFileFollowingSymlink(path, data, perm...)
}
// atomicWriteFileFollowingSymlink writes atomically to the resolved target.
// Keep this helper named at call sites so symlink write-through is explicit.
func atomicWriteFileFollowingSymlink(path string, data []byte, perm ...os.FileMode) error {
mode := os.FileMode(0644)
if len(perm) > 0 {
mode = perm[0]
}
targetPath, err := utils.ResolveForWrite(path)
if err != nil {
return fmt.Errorf("resolve path: %w", err)
}
dir := filepath.Dir(targetPath)
// Create unique temp file in same directory
tmpFile, err := os.CreateTemp(dir, ".*.tmp")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
tmpPath := tmpFile.Name()
// Write data
if _, err := tmpFile.Write(data); err != nil {
_ = tmpFile.Close() // best effort cleanup
_ = os.Remove(tmpPath) // best effort cleanup
return fmt.Errorf("write temp file: %w", err)
}
// Close temp file
if err := tmpFile.Close(); err != nil {
_ = os.Remove(tmpPath) // Best effort cleanup
return fmt.Errorf("close temp file: %w", err)
}
if err := os.Chmod(tmpPath, mode); err != nil {
_ = os.Remove(tmpPath) // Best effort cleanup
return fmt.Errorf("set permissions: %w", err)
}
// Atomic rename
if err := os.Rename(tmpPath, targetPath); err != nil {
_ = os.Remove(tmpPath) // Best effort cleanup
return fmt.Errorf("rename temp file: %w", err)
}
return nil
}
// DirExists checks if a directory exists
func DirExists(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
// FileExists checks if a file exists
func FileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
// EnsureDir creates a directory if it doesn't exist
func EnsureDir(path string, perm os.FileMode) error {
if DirExists(path) {
return nil
}
if err := os.MkdirAll(path, perm); err != nil {
return fmt.Errorf("create directory: %w", err)
}
return nil
}