Skip to content

[FEATURE] Configurable memory sync backend (git/GitHub: clone on start, push on finish) #683

Closed
inference-gateway/docs
#298

Description

Summary

Follow-up to #670 (persistent agent memory). #670 shipped a local file-based memory: a directory of Markdown fact-files under a configurable dir (default ~/.infer/memory), catalogued by a MEMORY.md index and injected into context at session start. It is local-only - there is no way to share or persist that memory beyond the machine the agent runs on.

This adds a configurable memory sync backend so the memory directory can be backed by a remote (starting with git/GitHub): clone/pull the memory into the local home dir when a run starts, and run a deterministic command to commit and push it back when the run finishes - only if something changed.

Requested in #670:

I want to have a memory backend configurable, for example in github - that whenever the project runs it will clone the memory to its local home dir and when it finished a deterministic command will be executed to push it back to github, if there were any changes.

Motivation

  • Memory today lives only on the local disk of whatever machine the agent runs on. A laptop, a CI runner, and the channels-manager daemon each keep separate, divergent memory.
  • Teams want a single shared memory (a git repo) the agent reads at start and writes back at the end - version-controlled, diffable, and auditable.
  • The headless runtimes are especially affected: channels-manager / scheduler / heartbeat each spawn a fresh infer agent subprocess per fire, so without a remote, durable cross-run memory never accumulates in ephemeral or containerized deployments.

Proposed design

Config (memory.yaml)

Extend MemoryConfig (config/memory.go) with a backend block. The default type: local preserves today's behavior exactly (no remote, no-op):

enabled: true
dir: ""                 # ~/.infer/memory (unchanged)
max_chars: 4000
backend:
  type: local           # local (default) | git
  git:
    repo: "[email protected]:my-org/agent-memory.git"
    branch: main
    # Auth uses the ambient git credential chain (ssh-agent, gh auth, GIT_*/token env).
    sync:
      on_start: pull      # clone if the dir is absent, else fast-forward / rebase pull
      on_finish: push     # commit + push, only when `git diff` shows changes
    commit_message: "chore(memory): sync"   # deterministic, non-LLM

Env overrides should follow the existing INFER_* convention. Note that memory.yaml is a split-out config file (Memory is tagged yaml:"-" mapstructure:"-" on the main Config), so wire env handling the same way its siblings (Heartbeat/Channels/ComputerUse) do rather than assuming viper picks it up from config.yaml.

Where it attaches

The agent loop already exposes symmetric hook points (internal/domain/hooks.go): HookPreSession ("run begins, before the first stream") and HookPostSession ("run finished"). A git backend attaches:

  • pre_session -> clone (if dir is missing) or pull into ResolveMemoryDir().
  • post_session -> git add -A; if there are staged changes, commit with the configured message and push; otherwise no-op (the "if there were any changes" part).

These are deterministic git commands run by the runtime, not the LLM - matching the request. They are the command-action sibling of the existing reminder/hook machinery (HookCommand in internal/domain/hooks.go), so they can flow through the same dispatch seam and bash allow-list.

Backend abstraction

Mirror the storage backend pattern (internal/infra/storage/factory.go selects jsonl/sqlite/postgres/redis by a type field):

  • New domain.MemoryBackend interface (e.g. SyncIn(ctx) error / SyncOut(ctx) error) in internal/domain - adding an interface here triggers a mock regeneration, so add the counterfeiter line per CLAUDE.md.
  • A local no-op implementation and a git implementation under internal/infra.
  • A factory that selects by backend.type, wired into the container alongside the existing memory plumbing.

Safety / robustness

  • Best-effort, never fatal. A failed clone/pull/push logs and continues; it must never break the agent run (same posture as a git-context or storage degrade).
  • Auth. Reuse the ambient git credential chain; do not invent new secret storage. Document SSH vs gh auth vs token-in-URL.
  • Concurrency. channels-manager / scheduler / heartbeat can run overlapping infer agent subprocesses against the same remote. Pull-with-rebase + push-with-retry (or a local lock) to avoid clobbering; document the race and the chosen mitigation.
  • Sandbox. The memory dir is already a carve-out (isWithinMemoryDir, config/config.go); a git working copy living there must stay readable/writable while .infer/ protection holds elsewhere.

Alternatives considered

Users can already hand-wire this today with pre_session / post_session command hooks (hooks.yaml) running raw git pull / git commit && git push. This feature turns that into a first-class, turnkey memory.backend with sane defaults, auth handling, change-detection, and concurrency safety - instead of every user re-deriving the git incantations and the bash allow-list entries.

Out of scope (future)

  • Non-git backends (S3 / object store, a hosted memory service).
  • Conflict-resolution UX beyond pull-rebase / last-writer-wins.
  • Encrypted-at-rest memory or per-fact ACLs.
  • Selective sync (e.g. push project facts, keep user facts local).

Acceptance criteria

  • memory.backend block (type + git settings) with defaults (type: local, behavior unchanged), validation, and env overrides following the INFER_* convention.
  • domain.MemoryBackend interface with a local no-op and a git implementation, selected by a factory (mirrors storage).
  • On run start the git backend clones-or-pulls the memory dir; on run finish it commits + pushes only when there are changes, via deterministic (non-LLM) commands.
  • Wired at HookPreSession / HookPostSession; failures are best-effort (log + continue) and never abort the run.
  • Auth uses the ambient git credential chain; concurrent-run safety is addressed and documented.
  • Works in headless infer agent runs (channels / scheduler / heartbeat).
  • Tests cover local no-op, clone-on-missing, pull-on-present, push-only-when-dirty, and failure degradation; task lint / task test pass; docs updated.

Follow-up to #670.

Activity

  1. moved this from Todo to QA in 2026on Jul 1, 2026
  2. edenreich commented on Jul 2, 2026

    @edenreich
    Contributor

    @claude I think we're missing docs for this in docs repo? Can you file it if we're missing it?

  3. moved this from QA to Done in 2026on Jul 2, 2026
  4. inference-gateway-maintainer commented on Jul 2, 2026

    @inference-gateway-maintainer
    ContributorAuthor

    Claude finished @edenreich's task in 2m 48s —— View job


    Docs coverage check for memory sync backend (#683)

    You're right - the docs were missing. I filed a tracking issue in the docs repo.

    Filed: inference-gateway/docs#296 - [DOCS] Document memory git sync backend (memory.backend)

    What I found

    The feature merged in #707 (feat(memory): configurable git sync backend), which closed this issue.

    The docs repo's cli.md has a Persistent Memory section, but its Configuration (memory.yaml) subsection only documents the original local knobs from #679:

    Documented today Missing (shipped in #707)
    enabled, dir, max_chars the entire backend block
    INFER_MEMORY_ENABLED/DIR/MAX_CHARS INFER_MEMORY_BACKEND_* (7 vars)

    Specifically undocumented: backend.type (local/git), backend.git.{repo,branch,commit_message,timeout}, backend.git.sync.{on_start,on_finish}, their INFER_MEMORY_BACKEND_* env vars, plus the behavior notes (clone-or-pull on start, push-only-when-dirty with push->rebase->retry + flock, chat-vs-headless push points, best-effort degradation, and the ambient git-credential auth model).

    I searched the docs repo first and confirmed no existing [DOCS] issue or coverage for this, so the new issue is not a duplicate. The issue body includes the full config surface, the env-var table, and the behavior notes for whoever picks it up. No code changes were needed here.

  5. inference-gateway-releaser commented on Jul 2, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.127.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementreleased

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions