Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix:新建文件夹中/memory检查不到JIUWENSWARM.md

openJiuwen-bot56c62882ad09
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts
index 7a1aa4bd3a..23128c05ae 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts
@@ -1,4 +1,6 @@
-import { join } from "path";
+import { existsSync, readdirSync, statSync, readFileSync } from "node:fs";
+import { homedir } from "node:os";
+import { dirname, join, parse, relative } from "node:path";
import { addError, addInfo, makeItem } from "../helpers.js";
import { CommandKind, type SlashCommand } from "../types.js";
import { getEditorInfo } from "../../utils/editor.js";
@@ -76,6 +78,140 @@ interface MemoryOpenResult {
coding_memory_dir?: string;
}
+// ---------------------------------------------------------------------------
+// Frontend-side memory file discovery (mirrors Claude Code's unguarded walk)
+// ---------------------------------------------------------------------------
+
+/** File patterns to scan at each directory level (aligned with backend's files.py). */
+const PROJECT_MEMORY_FILES: [string, string][] = [
+ ["JIUWENSWARM.md", "project"],
+ [".jiuwen/JIUWENSWARM.md", "project"],
+];
+const LOCAL_MEMORY_FILES: [string, string][] = [
+ ["JIUWENSWARM.local.md", "local"],
+];
+
+/** Probe a single path on disk; returns real state if file exists, placeholder if not. */
+function probeFile(absPath: string, relPath: string, kind: string): MemoryFile {
+ if (existsSync(absPath)) {
+ try {
+ const stat = statSync(absPath);
+ const content = readFileSync(absPath, "utf-8");
+ const lines = content.split("\n").length;
+ return {
+ path: absPath,
+ relative_path: relPath,
+ kind,
+ exists: true,
+ size: stat.size,
+ mtime: Math.floor(stat.mtimeMs / 1000),
+ lines,
+ };
+ } catch {
+ // stat/read failed — still mark exists, just with zero metrics
+ return { path: absPath, relative_path: relPath, kind, exists: true, size: 0, mtime: 0, lines: 0 };
+ }
+ }
+ return { path: absPath, relative_path: relPath, kind, exists: false, size: 0, mtime: 0, lines: 0 };
+}
+
+/** Normalize path for de-duplication (case-insensitive on Windows). */
+function normalizePathKey(p: string): string {
+ try {
+ return process.platform === "win32" ? p.toLowerCase() : p;
+ } catch {
+ return p;
+ }
+}
+
+/**
+ * Walk from CWD upward to root, scanning each directory for memory files.
+ * This mirrors Claude Code's unguarded traversal in claudemd.ts — no project
+ * root marker is required, every level is scanned unconditionally.
+ *
+ * Order: root → CWD (outermost ancestor first, CWD last), so closer files
+ * have higher priority (loaded later → override earlier).
+ */
+function discoverMemoryFilesFromFs(cwd: string): MemoryFile[] {
+ const results: MemoryFile[] = [];
+ const seenPaths = new Set<string>();
+
+ // 1. User-level memory
+ const userJiuwenDir = join(homedir(), ".jiuwen");
+ const userMemoryPath = join(userJiuwenDir, "JIUWENSWARM.md");
+ const userFile = probeFile(userMemoryPath, relative(homedir(), userMemoryPath), "user");
+ if (userFile.exists) {
+ results.push(userFile);
+ seenPaths.add(normalizePathKey(userFile.path));
+ }
+ // .jiuwen/rules/*.md at user level
+ const userRulesDir = join(userJiuwenDir, "rules");
+ if (existsSync(userRulesDir)) {
+ try {
+ for (const entry of readdirSync(userRulesDir)) {
+ if (entry.endsWith(".md")) {
+ const absPath = join(userRulesDir, entry);
+ const f = probeFile(absPath, relative(homedir(), absPath), "user");
+ if (f.exists && !seenPaths.has(normalizePathKey(f.path))) {
+ results.push(f);
+ seenPaths.add(normalizePathKey(f.path));
+ }
+ }
+ }
+ } catch { /* ignore unreadable dirs */ }
+ }
+
+ // 2. Project & Local — walk from root → CWD (reversed so closer dirs come last = higher priority)
+ const dirs: string[] = [];
+ let currentDir = cwd;
+ const root = parse(currentDir).root;
+ while (currentDir !== root) {
+ dirs.push(currentDir);
+ currentDir = dirname(currentDir);
+ }
+ // root directory itself is NOT included (same as Claude Code)
+
+ // Reverse: root → CWD so closer-to-CWD files appear later (higher priority)
+ dirs.reverse();
+
+ for (const dir of dirs) {
+ for (const [rel, kind] of PROJECT_MEMORY_FILES) {
+ const absPath = join(dir, rel);
+ const f = probeFile(absPath, relative(cwd, absPath), kind);
+ if (!seenPaths.has(normalizePathKey(absPath))) {
+ seenPaths.add(normalizePathKey(absPath));
+ if (f.exists) results.push(f);
+ }
+ }
+ // .jiuwen/rules/*.md at this level
+ const rulesDir = join(dir, ".jiuwen", "rules");
+ if (existsSync(rulesDir)) {
+ try {
+ for (const entry of readdirSync(rulesDir)) {
+ if (entry.endsWith(".md")) {
+ const absPath = join(rulesDir, entry);
+ if (!seenPaths.has(normalizePathKey(absPath))) {
+ seenPaths.add(normalizePathKey(absPath));
+ const f = probeFile(absPath, relative(cwd, absPath), "project");
+ if (f.exists) results.push(f);
+ }
+ }
+ }
+ } catch { /* ignore */ }
+ }
+ for (const [rel, kind] of LOCAL_MEMORY_FILES) {
+ const absPath = join(dir, rel);
+ if (!seenPaths.has(normalizePathKey(absPath))) {
+ seenPaths.add(normalizePathKey(absPath));
+ const f = probeFile(absPath, relative(cwd, absPath), kind);
+ if (f.exists) results.push(f);
+ }
+ }
+ }
+
+ return results;
+}
+
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
@@ -241,44 +377,39 @@ async function editMemoryInteractive(
const workspaceDir = ctx.getWorkspaceDir() || "";
const projectDir = ctx.getCurrentProjectDir();
- // Check if project and local memory exist
- const hasProjectMemory = files.some(
+ // Frontend-side unguarded traversal (aligned with Claude Code's claudemd.ts):
+ // Walk from CWD up to root, scanning every directory for memory files.
+ // This supplements the backend's `discover_and_load_memory_files`, which
+ // skips the entire project layer when `find_project_root()` returns None
+ // (e.g. empty dirs without .git/.jiuwen markers), causing JIUWENSWARM.md
+ // to never be discovered even if it exists on disk.
+ const discovered = discoverMemoryFilesFromFs(projectDir);
+ const seenPaths = new Set(files.map((f) => normalizePathKey(f.path)));
+
+ // Merge: backend results take precedence; frontend-discovered files fill gaps.
+ const mergedFiles: MemoryFile[] = [...files];
+ for (const f of discovered) {
+ if (!seenPaths.has(normalizePathKey(f.path))) {
+ mergedFiles.push(f);
+ seenPaths.add(normalizePathKey(f.path));
+ }
+ }
+
+ // Always provide JIUWENSWARM.md / JIUWENSWARM.local.md entries so users
+ // can create them if they don't exist yet. Use actual file state when
+ // available rather than fabricating a stale "0 lines (new)" placeholder.
+ const hasProjectMemory = mergedFiles.some(
(f) => f.path.endsWith("JIUWENSWARM.md") && !f.path.endsWith("JIUWENSWARM.local.md"),
);
- const hasLocalMemory = files.some((f) => f.path.endsWith("JIUWENSWARM.local.md"));
+ const hasLocalMemory = mergedFiles.some((f) => f.path.endsWith("JIUWENSWARM.local.md"));
- // Add default options if not exist
const projectMemoryPath = join(projectDir, "JIUWENSWARM.md");
const localMemoryPath = join(projectDir, "JIUWENSWARM.local.md");
const allFiles: MemoryFile[] = [
- ...files,
- ...(hasProjectMemory
- ? []
- : [
- {
- path: projectMemoryPath,
- relative_path: "JIUWENSWARM.md",
- kind: "project",
- exists: false,
- size: 0,
- mtime: 0,
- lines: 0,
- },
- ]),
- ...(hasLocalMemory
- ? []
- : [
- {
- path: localMemoryPath,
- relative_path: "JIUWENSWARM.local.md",
- kind: "local",
- exists: false,
- size: 0,
- mtime: 0,
- lines: 0,
- },
- ]),
+ ...mergedFiles,
+ ...(hasProjectMemory ? [] : [probeFile(projectMemoryPath, "JIUWENSWARM.md", "project")]),
+ ...(hasLocalMemory ? [] : [probeFile(localMemoryPath, "JIUWENSWARM.local.md", "local")]),
];
if (allFiles.length === 0) {