Skip to content

feat(memory): configurable git sync backend - #707

Merged
edenreich merged 20 commits into
mainfrom
feat/memory-git-sync-backend
Jul 1, 2026
Merged

edenreich merged 20 commits into
mainfrom
feat/memory-git-sync-backend

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #670 (persistent agent memory). #670 shipped a local file-based memory — a directory of Markdown fact-files under ~/.infer/memory, catalogued by MEMORY.md and injected into context at session start. This adds a configurable memory sync backend so that directory can be backed by a git remote: pull on run start, commit + push on change. type: local (the default) preserves today's behavior exactly.

Closes #683.

Config (memory.yaml)

enabled: true
dir: ""                 # ~/.infer/memory
max_chars: 4000
backend:
  type: local           # local (default) | git
  git:
    repo: "[email protected]:my-org/agent-memory.git"
    branch: main
    commit_message: "chore(memory): sync"
    timeout: 60         # seconds per git op
    sync:
      on_start: pull    # pull (default) | off
      on_finish: push   # push (default) | off

Env overrides follow the INFER_MEMORY_BACKEND_* convention (e.g. INFER_MEMORY_BACKEND_TYPE, INFER_MEMORY_BACKEND_GIT_REPO, INFER_MEMORY_BACKEND_GIT_BRANCH, ...).

Design

  • domain.MemoryBackend (SyncIn / SyncOut) with a local no-op and a git implementation, selected by a factory that mirrors the storage backend pattern (internal/infra/memory/).
  • SyncIn clones-or-pulls once per process at session start; an ls-remote probe decides clone vs. init-in-place (for an empty remote or pre-existing local memory).
  • SyncOut commits and pushes only when the memory dir changed (git status --porcelain), with a bounded push → pull-rebase → retry loop plus a per-host flock so concurrent runs (channels / scheduler / heartbeat) don't clobber each other.
  • Wiring: chat pulls at session start and pushes via the Memory tool on write/delete (the chat post-session hook fires per message, so pushing there would commit-storm); headless infer agent pulls on start and pushes at run finish. All best-effort — a sync failure logs and never aborts the run.
  • Auth uses the user's default git config (ssh-agent / credential helper / GIT_*); the backend injects no ssh/env override. A per-op timeout prevents hangs on credential prompts. A warning is logged if the repo URL embeds credentials (they persist in .git/config) — ssh is preferred.
  • Adds a periodic memory-hygiene reminder (every 4 turns) nudging the agent to record durable facts, alongside the existing turn-1 orientation reminder.

Testing

  • internal/infra/memory backend tests use a real bare repo as the remote: clone-on-missing, pull-on-present, push-when-dirty, concurrent-push rebase, no-op-when-clean, adopt-empty-remote, sync-in-once, failure-degrades; plus local no-op and factory selection.
  • Memory tool test asserts write/delete trigger SyncOut and read does not.
  • Config validation + INFER_MEMORY_BACKEND_* env-override tests; reminder interval + prune tests.
  • task test, task lint (0 issues), and task build all pass.

Notes

Back the persistent memory directory (#670) with an optional git remote so
memory can be shared and durable across machines and ephemeral headless runs
(issue #683).

- memory.backend config: type local (default, behavior unchanged) | git, with
  repo/branch/commit_message/timeout and sync.on_start/on_finish; validation and
  INFER_MEMORY_BACKEND_* env overrides.
- domain.MemoryBackend with a local no-op and a git implementation selected by a
  factory (mirrors the storage backend pattern).
- SyncIn clones-or-pulls once per process at session start; SyncOut commits and
  pushes only when the memory dir changed, with a bounded push/pull-rebase retry
  loop plus a per-host flock so concurrent runs don't clobber each other.
- Wired best-effort at the agent hook seams: chat pulls at start and pushes via
  the Memory tool on write/delete; headless infer agent pulls on start and
  pushes at run finish. A sync failure logs and never aborts the run.
- Auth uses the user's default git config (no ssh/env override); the per-op
  timeout prevents hangs on credential prompts.
- Add a periodic memory-hygiene reminder (every 4 turns) nudging the agent to
  record durable facts.
@edenreich
edenreich requested a review from a team as a code owner July 1, 2026 18:47
Comment thread cmd/agent.go Resolved
Comment thread config/memory.go Resolved
Comment thread config/memory.go Resolved
Comment thread internal/infra/memory/git.go Resolved
Comment thread internal/infra/memory/git.go Resolved
Comment thread internal/infra/memory/git.go Resolved
Comment thread internal/infra/memory/git_test.go Resolved
Comment thread internal/infra/memory/git_test.go Resolved
Comment thread internal/infra/memory/git_test.go Resolved
Comment thread internal/infra/memory/git_test.go Resolved
Comment thread internal/infra/memory/git_test.go Resolved
Comment thread internal/infra/memory/git_test.go Resolved
edenreich added 16 commits July 1, 2026 21:27
Co-authored-by: Eden Reich <[email protected]>
init is create-if-missing, so an existing reminders.yaml never gains a newly
added built-in reminder (memory-hygiene) - LoadReminders reads the file
wholesale. Reconcile the built-in memory reminders against the memory flag at
load: inject any missing ones when memory is enabled (so the feature ships to
existing users without regenerating the file) and prune them when disabled.
MemoryReminders() is now the single source of truth for both seeding and
reconciliation; only absent reminders are added, so user edits are preserved.
--overwrite only relaxed the pre-flight guard; the userspace-only files
(keybindings, reminders, heartbeat) were still written create-if-missing, so
init --overwrite silently left them stale - e.g. an existing reminders.yaml
never gained the memory-hygiene reminder. Thread overwrite into
createFileIfAbsent so home-mode --overwrite regenerates them, while --project
init still never clobbers files owned by a prior home init. memory.yaml stays
protected by design (it holds the user's memory/backend config).
Revert the load-time injection of built-in memory reminders: reminders.yaml is
the single source of truth, seeded by init / init --overwrite. At load we only
prune the memory reminders when memory is disabled (the original #670 posture),
so runtime never silently diverges from the file. MemoryReminders() remains the
single definition used to seed the file and to identify which to prune.
Memory git sync ran silently: the only failure signal for a bad remote
was a single "exit status 128" warning that scrolled past at session
start, so a misconfigured repo looked like nothing happened.

- Surface git's own stderr on an unreachable remote (ls-remote output)
  instead of the opaque exit code, so "Repository not found" / "Permission
  denied (publickey)" reaches the log.
- Add debug logs across the whole lifecycle - backend selection (git vs
  local no-op), sync-in start/clone/pull, sync-out start, nothing-to-push,
  commit, push - and at each trigger seam (chat pre_session, memory-tool
  mutation, headless pre/post_session) so the actions are greppable when
  e2e testing.
Once the memory dir was initialized, ensureRepo returned early and never
touched the origin remote, so changing memory.backend.git.repo (e.g.
switching ssh -> https) was silently ignored: pushes kept going to the URL
captured at first init. ensureRepo now reconciles origin to the configured
repo on every sync, adding it when missing and updating it when it drifted.
Sync-in ran git pull against an already-initialized memory dir even when
the remote branch did not exist yet, failing with "couldn't find remote
ref main" and never uploading the local memory. Sync-in now checks the
remote first: it pulls only when the branch exists, and otherwise seeds the
remote - creating the branch and pushing whatever local memory is present
(both for an existing repo and a fresh dir initialized in place). The
commit+push path is shared with sync-out via stageCommitPush.
edenreich added 3 commits July 1, 2026 22:55
… adopt)

Two robustness gaps in the git memory backend surfaced by review:

- Supply a fallback commit identity for whichever of user.name/user.email the
  ambient git config leaves unset, so sync-out commits succeed in an
  identity-less environment (a fresh container or CI runner where the
  channels/scheduler/heartbeat daemon runs) instead of silently failing with
  "Please tell me who you are". A configured identity - or one from
  GIT_AUTHOR_*/GIT_COMMITTER_* - is preserved; the fallback only fills a gap.

- Adopt an already-populated remote branch on sync-in when the local memory dir
  already has content (non-git, non-empty): fetch and merge with
  --allow-unrelated-histories (-X ours) so later push/pull reconcile normally,
  instead of leaving an unrelated-history repo that can never push or pull.
Memory tools run in the agent's parallel execution pool (Memory never requires
approval), so two writes in one turn race the MEMORY.md read-modify-write and
the second's atomic rewrite silently drops the first's entry - a file-level loss
the -race detector can't see. Guard the index upsert/remove with a package mutex
and add a concurrent-writes test that asserts every entry survives (it loses 15
of 16 without the lock).
@edenreich
edenreich merged commit 6a68c3d into main Jul 1, 2026
1 check passed
@edenreich
edenreich deleted the feat/memory-git-sync-backend branch July 1, 2026 21:43
inference-gateway-releaser Bot added a commit that referenced this pull request Jul 2, 2026
## [0.127.0](v0.126.0...v0.127.0) (2026-07-02)

### 🚀 Features

* **plan:** compact context and start a fresh session when a plan is approved ([#705](#705)) ([28ddba7](28ddba7))
* **memory:** configurable git sync backend ([#707](#707)) ([6a68c3d](6a68c3d)), closes [#670](#670) [#670](#670)
* **history:** each subagent gets its own history file ([#711](#711)) ([16c8fa1](16c8fa1)), closes [#699](#699)

### 🐛 Bug Fixes

* **plan:** accepting a plan switches to auto-accept mode by default ([#714](#714)) ([7b0befc](7b0befc)), closes [#710](#710)
* **tools:** guard concurrent gitignore and screenshot caches ([#713](#713)) ([0ff4276](0ff4276)), closes [#712](#712)
* **tools:** guard Registry.tools with RWMutex against concurrent MCP registration ([#709](#709)) ([8a70871](8a70871)), closes [#708](#708)
* **ui:** update dim color from [#565f89](https://github.com/inference-gateway/cli/issues/565f89) to [#7a7f9a](https://github.com/inference-gateway/cli/issues/7a7f9a) for improved contrast ([#706](#706)) ([b2ee67e](b2ee67e))

### 🔧 Build System

* **deps:** bump github.com/go-telegram/bot from 1.21.0 to 1.22.0 in the gomod group ([#703](#703)) ([3de5136](3de5136))
* **deps:** bump the github-actions group with 5 updates ([#704](#704)) ([80221f7](80221f7))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.127.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Jul 2, 2026
inference-gateway-releaser Bot added a commit to inference-gateway/infer-action that referenced this pull request Jul 3, 2026
## [0.21.0](v0.20.3...v0.21.0) (2026-07-03)

### Features

* support the persistent memory git backend ([#142](#142)) ([c8d9ff7](c8d9ff7)), closes [#140](#140) [cli#707](inference-gateway/cli#707) [#140](#140) [#141](#141)

### Maintenance

* **deps:** bump the github-actions group with 2 updates ([#141](#141)) ([81360c0](81360c0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant