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

111 lines
2.8 KiB
Go

// Package atomicfile provides atomic file writes via temp-file + rename.
//
// Writes land in a temporary file in the same directory as the target,
// are fsynced, then atomically renamed into place. Readers never see a
// partial or truncated file — only the previous complete version or the
// new complete version.
package atomicfile
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
)
// WriteFile atomically writes data to path with the given permissions.
// It creates an atomic Writer, copies data in via io.Copy, then
// fsyncs and renames into place.
func WriteFile(path string, data []byte, perm os.FileMode) error {
w, err := Create(path, perm)
if err != nil {
return err
}
if _, err := io.Copy(w, bytes.NewReader(data)); err != nil {
_ = w.Abort()
return err
}
return w.Close()
}
// Writer is an io.WriteCloser that writes to a temporary file and
// atomically renames it to the target path on Close. Call Abort to
// discard the temp file without touching the target.
type Writer struct {
target string
f *os.File
perm os.FileMode
done bool
}
// Create returns a Writer that will atomically replace path on Close.
// The temp file is created in the same directory as path to guarantee
// same-filesystem rename semantics.
func Create(path string, perm os.FileMode) (*Writer, error) {
dir := filepath.Dir(path)
base := filepath.Base(path)
f, err := os.CreateTemp(dir, ".~"+base+".")
if err != nil {
return nil, fmt.Errorf("atomicfile: create temp: %w", err)
}
return &Writer{
target: path,
f: f,
perm: perm,
}, nil
}
// Write delegates to the underlying temp file.
func (w *Writer) Write(p []byte) (int, error) {
return w.f.Write(p)
}
// Close fsyncs the temp file and atomically renames it to the target path.
// After Close returns successfully, the target contains exactly the data
// written. On error the temp file is removed and the target is untouched.
func (w *Writer) Close() error {
if w.done {
return nil
}
w.done = true
// Ensure permissions before rename — CreateTemp uses 0600 by default.
if err := w.f.Chmod(w.perm); err != nil {
_ = w.f.Close()
_ = os.Remove(w.f.Name())
return fmt.Errorf("atomicfile: chmod: %w", err)
}
if err := w.f.Sync(); err != nil {
_ = w.f.Close()
_ = os.Remove(w.f.Name())
return fmt.Errorf("atomicfile: sync: %w", err)
}
if err := w.f.Close(); err != nil {
_ = os.Remove(w.f.Name())
return fmt.Errorf("atomicfile: close: %w", err)
}
if err := os.Rename(w.f.Name(), w.target); err != nil {
_ = os.Remove(w.f.Name())
return fmt.Errorf("atomicfile: rename: %w", err)
}
return nil
}
// Abort discards the temp file without renaming. The target is untouched.
// Safe to call multiple times or after Close.
func (w *Writer) Abort() error {
if w.done {
return nil
}
w.done = true
_ = w.f.Close()
return os.Remove(w.f.Name())
}