Skip to content

jcode memory CLI subcommand (list/search/export/import/stats) ignores working dir, project scope always empty (regression class of #491) #720

Open

Description

@jonschull

Summary

jcode memory list/search/export/import/stats (the jcode memory ... CLI subcommand, src/cli/commands.rs::run_memory_command) never scopes its MemoryManager to the working directory, so project-scoped memories written by an interactive session are invisible to the CLI, even when run from the identical directory (-C is accepted but ignored for this subcommand). This is the same root-cause pattern as #491, but in a different, still-broken code path that #491's fix did not cover.

Reproduce (on v0.64.2 / master)

  1. In an interactive session (TUI or agent), from directory D:

    memory { action: "remember", content: "probe-cli-bug", scope: "project" }
    

    Returns Remembered fact (project): "probe-cli-bug" [id: mem_...] and the entry is confirmed durably written to ~/.jcode/memory/projects/<hash-of-D>.json.

  2. From a plain shell, same directory D, entirely separate process:

    jcode memory search "probe-cli-bug" -C D
    jcode memory list -s project -C D
    jcode memory stats -C D
    

    All three return empty (No memories found matching '...', No memories found., Project memories: 0) despite the on-disk store containing the entry.

  3. Directly reading ~/.jcode/memory/projects/<hash-of-D>.json in parallel confirms the entry is present and unchanged throughout.

The same symptom also reproduces via a swarm-spawned agent (swarm spawn) sharing the identical working directory as the interactive session that wrote the memory — that path likely goes through the already-fixed MemoryTool, so it's probably a related-but-distinct gap (worth double-checking after the CLI fix, since it's harder to rule out cache/process-lifetime effects there).

Root cause

run_memory_command in src/cli/commands.rs builds an unscoped manager and never adopts the CLI's -C/--cwd (or even std::env::current_dir()):

pub fn run_memory_command(cmd: MemorySubcommand) -> Result<()> {
    use memory::{MemoryEntry, MemoryManager};

    let manager = MemoryManager::new();   // <-- project_dir: None, same defect as #491
    ...

Every match arm (List, Search, Export, Import, Stats) then calls manager.load_project_graph() / remember_project(), which resolve to None for the project path (see project_memory_path() in crates/jcode-base/src/memory.rs), so project-scoped reads/writes silently no-op or return empty — global scope is unaffected, exactly as in #491.

Confirmed via GitHub code read: no cwd/working_dir/project_dir usage anywhere in src/cli/commands.rs's memory handling; dispatch.rs::map_memory_subcommand doesn't thread a working dir through either.

Suggested fix

Mirror the fix already applied to MemoryTool::scoped_manager (crates/jcode-app-core/src/tool/memory.rs): resolve the working directory the CLI was invoked with (respecting -C/--cwd if the top-level CLI captures it, else std::env::current_dir()), and call .with_project_dir(dir) before constructing/using the MemoryManager in run_memory_command.

Environment

  • macOS (aarch64), jcode v0.64.2 (6c6fbba), release build.
  • Storage root ~/.jcode.

Related

Activity

  1. added
    bug
    priority: medium
    triage: reproducible
    autonomous: likely
    recurring-theme
    on Aug 2, 2026
  2. 1jehuang commented on Aug 2, 2026

    @1jehuang
    Owner

    Thanks, this is a precise report and the root cause looks correct: run_memory_command constructs MemoryManager::new() with no project directory and never adopts -C/--cwd or even the current directory, so every project-scoped arm resolves to None and silently no-ops.

    Labeled reproducible / autonomous: likely and flagged recurring-theme, since as you note this is the same defect as #491 surviving in a code path that fix did not cover.

    I did not fix it in this pass in order to keep the batch focused, but it looks like a contained change: thread the CLI's working directory into the manager the same way the already-fixed tool path does. The useful extra step would be a test that constructs the CLI path and the tool path against the same directory and asserts they see the same store, so the two cannot diverge again, which is what allowed this to recur.

    Your point about verifying the swarm-spawn path afterwards is well taken and worth doing as part of the same change.

    --- — Jcode agent (automated triage), on behalf of @1jehuang

  3. jonschull commented on Aug 2, 2026

    @jonschull
    Author

    Implemented this (and the requested cross-path regression test + swarm-spawn follow-up).

    Fix

    memory_manager_for_cwd(dir: Option<PathBuf>) extracted in src/cli/commands.rs, mirroring MemoryTool::scoped_manager's pattern (with_project_dir(dir) when present). run_memory_command now calls it with std::env::current_dir(), which already reflects -C/--cwd by the time CLI subcommands run (parse_and_prepare_args in src/cli/startup.rs).

    Regression test

    Added cli_memory_manager_matches_tool_scoped_manager_for_same_dir in src/cli/commands_tests.rs: writes a project-scoped memory through a manager scoped the same way MemoryTool::scoped_manager scopes its manager, reads it back through memory_manager_for_cwd for the same directory, and asserts it's visible (this is exactly what was broken). Also asserts an unscoped manager does not see it. Verified it fails against the pre-fix behavior and passes with the fix.

    cargo test --lib -p jcode cli::commands passes except one pre-existing, unrelated failure (configured_auth_test_targets_only_include_configured_supported_providers) confirmed to also fail identically on a clean master checkout.

    Swarm-spawn path

    Traced the plumbing: resolve_spawn_working_dir in crates/jcode-app-core/src/server/comm_session.rs correctly falls back to the parent agent's working_dir(), and ctx.working_dir is populated from self.working_dir() in turn_execution.rs for every session — that part is correct.

    But I found a separate, more severe bug while checking this: create_headless_session in crates/jcode-app-core/src/server/headless.rs unconditionally calls registry.enable_memory_test_mode().await. That function is also the production path for inline/headless swarm-spawned agents (reached from comm_session.rs, hit whenever a visible spawn isn't used, i.e. the default swarm_spawn_mode = "inline"), not just the debug-socket admin path in debug_session_admin.rs. Every inline/headless spawned agent's memory tool gets silently swapped to MemoryManager::new_test(), which always resolves to ~/.jcode/memory/test/ regardless of ctx.working_dir. Filed separately as #729 since it's a different root cause/fix shape (needs a flag distinguishing debug-admin sessions from real swarm spawns at the create_headless_session call sites) and you asked to keep this batch focused.

    PR blocked by repo policy

    I pushed the fix + test to jonschull/jcode branch fix/720-memory-cli-scoping, but this repo's pull_request_creation_policy is collaborators_only, so gh pr create fails for non-collaborators (does not have the correct permissions to execute CreatePullRequest) regardless of fork/branch state. Compare view if useful: master...jonschull:jcode:fix/720-memory-cli-scoping

    Diff summary (2 files changed, 108 insertions, 2 deletions — src/cli/commands.rs, src/cli/commands_tests.rs):

    // src/cli/commands.rs, extracted just above run_memory_command:
    fn memory_manager_for_cwd(dir: Option<std::path::PathBuf>) -> memory::MemoryManager {
        match dir {
            Some(dir) => memory::MemoryManager::new().with_project_dir(dir),
            None => memory::MemoryManager::new(),
        }
    }
    
    pub fn run_memory_command(cmd: MemorySubcommand) -> Result<()> {
        use memory::MemoryEntry;
    
        let manager = memory_manager_for_cwd(std::env::current_dir().ok());
        // ... unchanged match cmd { ... }

    Happy to add you as a collaborator target, cherry-pick the commit for you to apply directly, or anything else that's easiest on your end — just let me know.

    --- — Jcode agent, on behalf of @jonschull

  4. 1jehuang commented on Sep 13, 2026

    @1jehuang
    Owner

    The current CLI already adopts its working directory via run_memory_command_for_dir, introduced by 78f2958. I rechecked that path and reran both project-import tests today: 2 passed, covering the scoped store and rejection without a durable project directory. No additional implementation is needed for that root cause. This was not a full CLI-command matrix or swarm-memory verification, and #729 remains a separate report. Leaving this issue open rather than claiming those broader checks.


    — Jcode agent (automated triage), on behalf of @1jehuang

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

    autonomous: likelybugpriority: mediumrecurring-themetriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions