Skip to content

Commit 33b2321

Browse files
Fix memory-wiki bridge self-import loop (#95666)
Merged via squash. Prepared head SHA: 0f74629 Co-authored-by: TurboTheTurtle <[email protected]> Co-authored-by: vincentkoc <[email protected]> Reviewed-by: @vincentkoc
1 parent 667e5bf commit 33b2321

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

‎extensions/memory-wiki/src/bridge.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,78 @@ describe("syncMemoryWikiBridgeSources", () => {
150150
expect(logLines).toHaveLength(2);
151151
});
152152

153+
it("skips generated artifacts from its own vault", async () => {
154+
const workspaceDir = await createBridgeWorkspace("self-import-workspace");
155+
const vaultDir = path.join(workspaceDir, "memory", "wiki");
156+
const { config } = await createVault({
157+
rootDir: vaultDir,
158+
config: {
159+
vaultMode: "bridge",
160+
bridge: {
161+
enabled: true,
162+
readMemoryArtifacts: true,
163+
indexDailyNotes: true,
164+
},
165+
},
166+
});
167+
168+
const dailyNotePath = path.join(workspaceDir, "memory", "2026-06-22.md");
169+
const generatedSourcePath = path.join(
170+
vaultDir,
171+
"sources",
172+
"bridge-workspace-remote-memory-daily-old.md",
173+
);
174+
const generatedIndexPath = path.join(vaultDir, "index.md");
175+
await fs.mkdir(path.dirname(dailyNotePath), { recursive: true });
176+
await fs.mkdir(path.dirname(generatedSourcePath), { recursive: true });
177+
await fs.writeFile(dailyNotePath, "# Daily Note\n", "utf8");
178+
await fs.writeFile(generatedSourcePath, "# Previously Imported Source\n", "utf8");
179+
await fs.writeFile(generatedIndexPath, "# Generated Index\n", "utf8");
180+
181+
registerBridgeArtifacts([
182+
{
183+
kind: "daily-note",
184+
workspaceDir,
185+
relativePath: "memory/2026-06-22.md",
186+
absolutePath: dailyNotePath,
187+
agentIds: ["main"],
188+
contentType: "markdown",
189+
},
190+
{
191+
kind: "daily-note",
192+
workspaceDir,
193+
relativePath: "memory/wiki/sources/bridge-workspace-remote-memory-daily-old.md",
194+
absolutePath: generatedSourcePath,
195+
agentIds: ["main"],
196+
contentType: "markdown",
197+
},
198+
{
199+
kind: "daily-note",
200+
workspaceDir,
201+
relativePath: "memory/wiki/index.md",
202+
absolutePath: generatedIndexPath,
203+
agentIds: ["main"],
204+
contentType: "markdown",
205+
},
206+
]);
207+
208+
const appConfig: OpenClawConfig = {
209+
agents: {
210+
list: [{ id: "main", default: true, workspace: workspaceDir }],
211+
},
212+
};
213+
214+
const result = await syncMemoryWikiBridgeSources({ config, appConfig });
215+
216+
expect(result.artifactCount).toBe(1);
217+
expect(result.importedCount).toBe(1);
218+
expect(result.pagePaths).toHaveLength(1);
219+
expect(result.pagePaths[0]).not.toContain("memory-wiki-sources");
220+
const sourcePages = await fs.readdir(path.join(vaultDir, "sources"));
221+
expect(sourcePages.filter((name) => name.startsWith("bridge-"))).toHaveLength(2);
222+
expect(sourcePages.filter((name) => name.includes("memory-wiki-sources-"))).toEqual([]);
223+
});
224+
153225
it("imports bridge artifacts from legacy providers without agent ids", async () => {
154226
const workspaceDir = await createBridgeWorkspace("legacy-agentids-workspace");
155227
const { rootDir: vaultDir, config } = await createVault({

‎extensions/memory-wiki/src/bridge.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,19 @@ function shouldImportArtifact(
6464

6565
async function collectBridgeArtifacts(
6666
bridgeConfig: ResolvedMemoryWikiConfig["bridge"],
67+
vaultRoot: string,
6768
artifacts: MemoryPluginPublicArtifact[],
6869
): Promise<BridgeArtifact[]> {
6970
const collected: BridgeArtifact[] = [];
71+
const vaultRootKey = await resolveArtifactKey(vaultRoot);
7072
for (const artifact of artifacts) {
7173
if (!shouldImportArtifact(artifact, bridgeConfig)) {
7274
continue;
7375
}
7476
const syncKey = await resolveArtifactKey(artifact.absolutePath);
77+
if (isPathInsideOrEqual(vaultRootKey, syncKey)) {
78+
continue;
79+
}
7580
collected.push({
7681
syncKey,
7782
artifactType: artifact.kind === "event-log" ? "memory-events" : "markdown",
@@ -87,6 +92,14 @@ async function collectBridgeArtifacts(
8792
return [...deduped.values()];
8893
}
8994

95+
function isPathInsideOrEqual(parentPath: string, candidatePath: string): boolean {
96+
const relative = path.relative(parentPath, candidatePath);
97+
return (
98+
relative === "" ||
99+
(relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative))
100+
);
101+
}
102+
90103
function resolveBridgeTitle(artifact: BridgeArtifact, agentIds: string[]): string {
91104
if (artifact.artifactType === "memory-events") {
92105
if (agentIds.length === 0) {
@@ -227,7 +240,11 @@ export async function syncMemoryWikiBridgeSources(params: {
227240
const publicArtifacts = await listActiveMemoryPublicArtifacts({ cfg: params.appConfig });
228241
const results: Array<{ pagePath: string; changed: boolean; created: boolean }> = [];
229242
const activeKeys = new Set<string>();
230-
const artifacts = await collectBridgeArtifacts(params.config.bridge, publicArtifacts);
243+
const artifacts = await collectBridgeArtifacts(
244+
params.config.bridge,
245+
params.config.vault.path,
246+
publicArtifacts,
247+
);
231248
const state = await readMemoryWikiSourceSyncState(params.config.vault.path);
232249
assertMemoryWikiSourceSyncStateCapacity({
233250
state,

0 commit comments

Comments
 (0)