gitgram/aitimemachine/beads/cmd/bd/dolt_autopush.go
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

203 lines
6.2 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage"
)
// pushState tracks auto-push state in a local file (.beads/push-state.json)
// instead of the Dolt metadata table, to avoid merge conflicts on multi-machine
// setups (GH#2466).
type pushState struct {
LastPush string `json:"last_push"` // RFC3339 timestamp
LastCommit string `json:"last_commit"` // Dolt commit hash
}
type autoPushTarget interface {
GetCurrentCommit(ctx context.Context) (string, error)
Push(ctx context.Context) error
}
func pushStatePath() (string, error) {
beadsDir := beads.FindBeadsDir()
if beadsDir == "" {
return "", fmt.Errorf("%s", activeWorkspaceNotFoundError())
}
return filepath.Join(beadsDir, "push-state.json"), nil
}
func loadPushState() (*pushState, error) {
path, err := pushStatePath()
if err != nil {
return nil, err
}
data, err := os.ReadFile(path) //nolint:gosec // path is constructed internally
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
var ps pushState
if err := json.Unmarshal(data, &ps); err != nil {
return nil, err
}
return &ps, nil
}
func savePushState(ps *pushState) error {
path, err := pushStatePath()
if err != nil {
return err
}
data, err := json.MarshalIndent(ps, "", " ")
if err != nil {
return err
}
return atomicWriteFile(path, data)
}
// autoPushTimeout bounds the st.Push() call that shells out to git fetch,
// which blocks indefinitely when the remote is unreachable (GH#3370).
const autoPushTimeout = 30 * time.Second
// isDoltAutoPushEnabled returns whether auto-push to Dolt remote should run.
// Returns true only when explicitly opted in via dolt.auto-push=true in
// .beads/config.yaml (or BD_DOLT_AUTO_PUSH=true env var).
//
// Previously the default was to auto-enable when an "origin" remote exists.
// That is unsafe at any concurrency above one writer: git+ssh remotes have no
// chunk-level upload atomicity, so concurrent dolt pushes race on the remote
// manifest and can leave it referencing chunks that were never uploaded. Any
// subsequent fetch/clone/push propagates the dangling reference. Set
// dolt.auto-push=true to restore the old behavior on single-writer setups.
func isDoltAutoPushEnabled(_ context.Context) bool {
return config.GetBool("dolt.auto-push")
}
// pushWithContext is a caller-side guard. Push implementations should honor
// ctx directly, but this keeps auto-push from blocking forever if one does not.
func pushWithContext(ctx context.Context, target autoPushTarget) error {
errCh := make(chan error, 1)
go func() {
errCh <- target.Push(ctx)
}()
select {
case err := <-errCh:
return err
case <-ctx.Done():
return ctx.Err()
}
}
// maybeAutoPush pushes to the Dolt remote if enabled and the debounce interval has passed.
// Called from PersistentPostRun after auto-commit and auto-backup.
func maybeAutoPush(ctx context.Context) {
if isSandboxMode() {
debug.Logf("dolt auto-push: skipped (sandbox mode)\n")
return
}
if !isDoltAutoPushEnabled(ctx) {
return
}
st := getStore()
if st == nil {
return
}
if lm, ok := storage.UnwrapStore(st).(storage.LifecycleManager); ok && lm.IsClosed() {
return
}
// Load local push state (file-based, not in Dolt metadata table).
// This avoids merge conflicts on multi-machine setups (GH#2466).
ps, err := loadPushState()
if err != nil {
debug.Logf("dolt auto-push: failed to load push state: %v\n", err)
return
}
// Debounce: skip if we pushed recently
interval := config.GetDuration("dolt.auto-push-interval")
if interval == 0 {
interval = 5 * time.Minute
}
if ps != nil && ps.LastPush != "" {
lastPush, err := time.Parse(time.RFC3339, ps.LastPush)
if err == nil && time.Since(lastPush) < interval {
debug.Logf("dolt auto-push: throttled (last push %s ago, interval %s)\n",
time.Since(lastPush).Round(time.Second), interval)
return
}
}
// Change detection: skip if nothing changed since last push
currentCommit, err := st.GetCurrentCommit(ctx)
if err != nil {
debug.Logf("dolt auto-push: failed to get current commit: %v\n", err)
return
}
if ps != nil && currentCommit == ps.LastCommit && ps.LastCommit != "" {
debug.Logf("dolt auto-push: no changes since last push\n")
return
}
// Push with a bounded timeout so an unreachable remote doesn't block
// the CLI indefinitely (GH#3370). The timeout is configurable via
// dolt.auto-push-timeout (default 30s).
pushTimeout := config.GetDuration("dolt.auto-push-timeout")
if pushTimeout == 0 {
pushTimeout = autoPushTimeout
}
pushCtx, pushCancel := context.WithTimeout(ctx, pushTimeout)
defer pushCancel()
debug.Logf("dolt auto-push: pushing to origin (timeout %s)...\n", pushTimeout)
if err := pushWithContext(pushCtx, st); err != nil {
if !isQuiet() && !jsonOutput {
if pushCtx.Err() == context.DeadlineExceeded {
fmt.Fprintf(os.Stderr, "Warning: dolt auto-push timed out after %s (remote may be unreachable)\n", pushTimeout)
} else {
fmt.Fprintf(os.Stderr, "Warning: dolt auto-push failed: %v\n", err)
}
if isAncestorPKMismatchErr(err) {
printAncestorPKMismatchGuidance(err)
} else if isDivergedHistoryErr(err) {
printDivergedHistoryGuidance("push")
}
}
debug.Logf("dolt auto-push: push error: %v\n", err)
// Throttle retries after failure so a hanging remote doesn't make every
// subsequent bd command pay the push timeout. We record the attempt
// timestamp but NOT a new LastCommit, so when the remote recovers the
// change-detection check (currentCommit != LastCommit) still triggers.
if ps == nil {
ps = &pushState{}
}
ps.LastPush = time.Now().UTC().Format(time.RFC3339)
if saveErr := savePushState(ps); saveErr != nil {
debug.Logf("dolt auto-push: failed to save push state after error: %v\n", saveErr)
}
return
}
// Record last push time and commit to local file
now := time.Now().UTC().Format(time.RFC3339)
if err := savePushState(&pushState{LastPush: now, LastCommit: currentCommit}); err != nil {
debug.Logf("dolt auto-push: failed to save push state: %v\n", err)
}
debug.Logf("dolt auto-push: pushed successfully\n")
}