Skip to content

Self-hosted MCP server + dashboard giving Claude Code, Cursor & Hermes agents shared long-term memory over a git-backed markdown vault. Agent-native, Obsidian-compatible second brain — markdown RAG, no vector DB.

fix: pull remote changes into the active vault (auto + on boot + manual)

Ronalds Palacise116400e1d9c
diff --git a/app/api/sync/route.ts b/app/api/sync/route.ts
index 1efb464..2917984 100644
--- a/app/api/sync/route.ts
+++ b/app/api/sync/route.ts
@@ -1,7 +1,12 @@
-import { syncStatus } from "@/lib/git";
+import { pullActive, syncStatus } from "@/lib/git";
export const dynamic = "force-dynamic";
export async function GET() {
return Response.json(await syncStatus());
}
+
+/** Manual refresh — pull the active vault's remote now (index rebuilds on change). */
+export async function POST() {
+ return Response.json(await pullActive());
+}
diff --git a/components/workspace-switcher.tsx b/components/workspace-switcher.tsx
index 1325568..9915bf7 100644
--- a/components/workspace-switcher.tsx
+++ b/components/workspace-switcher.tsx
@@ -3,7 +3,7 @@
import useSWR, { useSWRConfig } from "swr";
import Link from "next/link";
import { useState } from "react";
-import { Check, ChevronsUpDown, Settings2 } from "lucide-react";
+import { Check, ChevronsUpDown, RefreshCw, Settings2 } from "lucide-react";
import { fetcher } from "@/lib/client";
interface Repo {
@@ -16,9 +16,11 @@ export function WorkspaceSwitcher() {
const { data } = useSWR<{ repos: Repo[]; active: Repo | null }>("/api/repos", fetcher);
const { mutate } = useSWRConfig();
const [open, setOpen] = useState(false);
+ const [refreshing, setRefreshing] = useState(false);
const repos = data?.repos ?? [];
const activeName = data?.active?.name ?? "Sample vault";
+ const hasRemote = !!data?.active;
async function switchTo(id: string) {
await fetch("/api/repos/active", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id }) });
@@ -26,15 +28,36 @@ export function WorkspaceSwitcher() {
setOpen(false);
}
+ async function refresh() {
+ if (refreshing) return;
+ setRefreshing(true);
+ try {
+ await fetch("/api/sync", { method: "POST" });
+ for (const k of ["/api/tree", "/api/notes", "/api/sync"]) mutate(k);
+ } finally {
+ setRefreshing(false);
+ }
+ }
+
return (
- <div className="relative px-2 pb-2">
+ <div className="relative flex items-center gap-1 px-2 pb-2">
<button
onClick={() => setOpen((o) => !o)}
- className="flex w-full items-center gap-2 rounded-md border border-border px-2 py-1.5 text-xs"
+ className="flex min-w-0 flex-1 items-center gap-2 rounded-md border border-border px-2 py-1.5 text-xs"
>
<span className="flex-1 truncate text-left text-foreground">{activeName}</span>
<ChevronsUpDown size={13} className="shrink-0 text-muted-foreground" />
</button>
+ {hasRemote && (
+ <button
+ onClick={refresh}
+ disabled={refreshing}
+ title="Pull latest from the remote"
+ className="inline-flex size-8 shrink-0 items-center justify-center rounded-md border border-border text-muted-foreground transition-colors hover:text-foreground disabled:opacity-50"
+ >
+ <RefreshCw size={13} className={refreshing ? "animate-spin" : ""} />
+ </button>
+ )}
{open && (
<>
<div className="fixed inset-0 z-10" onClick={() => setOpen(false)} />
diff --git a/instrumentation.ts b/instrumentation.ts
index 19cde38..d29148e 100644
--- a/instrumentation.ts
+++ b/instrumentation.ts
@@ -1,7 +1,11 @@
-// Runs once when the server starts. Re-clones the active workspace's vault into its
-// dir if missing (e.g. a fresh volume). No-op when no workspace is configured.
+// Runs once when the server starts. Re-clones the active workspace's vault if missing
+// (e.g. a fresh volume), pulls the latest from its remote, then starts a periodic pull so
+// externally-pushed changes keep flowing in without a redeploy.
export async function register() {
if (process.env.NEXT_RUNTIME !== "nodejs") return;
const { ensureActiveCloned } = await import("@/lib/repos");
+ const { pullActive, startPullLoop } = await import("@/lib/git");
await ensureActiveCloned();
+ await pullActive().catch(() => {});
+ startPullLoop();
}
diff --git a/lib/git.ts b/lib/git.ts
index f11376f..243c039 100644
--- a/lib/git.ts
+++ b/lib/git.ts
@@ -1,6 +1,7 @@
import { simpleGit } from "simple-git";
-import { activeVaultDir } from "@/lib/repos";
+import { activeVaultDir, getActive } from "@/lib/repos";
import { gitAuthor, gitSyncEnabled } from "@/lib/settings";
+import { rebuildIndex } from "@/lib/vault/store";
let timer: ReturnType<typeof setTimeout> | null = null;
let pending: string[] = [];
@@ -67,3 +68,38 @@ export async function syncStatus() {
return { enabled: true as const, error: true };
}
}
+
+/**
+ * Pull remote commits into the ACTIVE vault clone (rebase, autostash). This is how changes
+ * pushed to the repo from OUTSIDE Engram (an agent, a teammate, a direct git push) show up —
+ * the chokidar watcher then rebuilds the index. Independent of the push side; a fresh
+ * connected workspace should always reflect its remote. No-op for the sample/local vault.
+ */
+export async function pullActive(): Promise<{ ok: boolean; changed: boolean; error?: string }> {
+ const active = getActive();
+ if (!active) return { ok: true, changed: false };
+ const dir = activeVaultDir();
+ try {
+ const g = simpleGit(dir);
+ const before = await g.revparse(["HEAD"]).catch(() => "");
+ await g.fetch();
+ await g.pull(["--rebase", "--autostash"]);
+ const after = await g.revparse(["HEAD"]).catch(() => "");
+ const changed = before !== after;
+ if (changed) rebuildIndex();
+ return { ok: true, changed };
+ } catch (e) {
+ console.error("[git] pull failed", e);
+ return { ok: false, changed: false, error: e instanceof Error ? e.message : String(e) };
+ }
+}
+
+let pullTimer: ReturnType<typeof setInterval> | null = null;
+/** Poll the remote for the active vault so the brain stays fresh without a redeploy. */
+export function startPullLoop(intervalMs = 30_000): void {
+ if (pullTimer) return;
+ pullTimer = setInterval(() => {
+ pullActive().catch(() => {});
+ }, intervalMs);
+ pullTimer.unref?.();
+}