Skip to content

fix(cmd): keep cmd output per session - #141

Merged
Haleclipse merged 1 commit into
Haleclipse:masterfrom
nsdont:fix/cmd-cache-per-session
Nov 1, 2026
Merged

Haleclipse merged 1 commit into
Haleclipse:masterfrom
nsdont:fix/cmd-cache-per-session

Conversation

@nsdont

@nsdont nsdont commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A cmd item gets the session's status line payload on stdin, but its output was cached by command line and directory only. Sessions in the same directory therefore overwrote each other's output, and each one showed whichever session refreshed last.

 fn paths(run, cwd, session)
-  key = file_key("{run}\0{cwd}")
+  key = file_key("{run}\0{cwd}\0{session}")

 refresh_in_background
-  ccline refresh-cmd --timeout-ms N [--cwd DIR] -- RUN
+  ccline refresh-cmd --timeout-ms N [--cwd DIR] [--session ID] -- RUN

With no session_id, the key is run + cwd + "", the same for render and refresh.

Evidence

Setup: two sessions open in the same directory. The cmd reads .transcript_path from stdin and prints the time of that session's last user message:

{ metric = "cmd", options = { run = "~/.claude/ccline/last-prompt.sh", cache_s = 10 } }
  • Before (v2.0.1): both sessions render the same value, which flips between them every refresh.
    session A: ↑ 01:01 · 14h46m
    session B: ↑ 01:01 · 14h46m   # should be 15:38
    
    After: each session keeps its own output across repeated renders.
    session A: ↑ 01:01 · 14h46m
    session B: ↑ 15:38 · 9m
    
  • New test cmd_cache::tests::sessions_keep_their_own_output: same run and cwd give the same path for the same session, and different paths for different sessions or no session. cargo fmt --check, cargo clippy -- -D warnings and cargo test pass.

Merge Danger

Door: two-way

Only cache keys change. Old cache files are no longer read, and the existing sweep removes them after a day.

Blast Radius: small

  • Commands whose output does not depend on the session (e.g. kubectl config current-context) now run once per session instead of once per directory. A new session's first render can come up empty while its first background run finishes.
  • Not addressed here (pre-existing): within one session, the main line and subagent lines still share a key although their stdin payloads differ (status line JSON vs. the tasks payload).

local-review: 81c72ac055cd826633abb17f4b096f92804a0467 · P0 0 · P1 0 · nit 0

Summary by Sourcery

Isolate command output caches by session to ensure each status line displays results for its own session.

Bug Fixes:

  • Keep cached cmd output isolated per session so sessions in the same directory no longer overwrite or display each other’s results.

Enhancements:

  • Propagate the optional session identifier through command refreshes and use it consistently when locating cached output.

Tests:

  • Add coverage confirming cache paths are stable for the same session and distinct across sessions.

A cmd's output was cached by command line and directory only, so sessions
in the same directory overwrote each other's output even though each run
gets its own session's payload on stdin. Key the cache by session id too,
and pass it to the background refresh.
@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Command output caches are now isolated by session in addition to command and working directory, with session context propagated through rendering, background refreshes, and the refresh-cmd CLI. This prevents sessions sharing a directory from overwriting each other’s session-dependent output while retaining the existing behavior when no session ID is available.

Sequence diagram for session-isolated command output caching

sequenceDiagram
    participant Session as Session
    participant Metric as metrics::custom::cmd
    participant Cache as cmd_cache::output
    participant Refresh as ccline refresh-cmd
    participant Store as Session-keyed cache

    Session->>Metric: cmd(ctx, opts)
    Metric->>Cache: output(Job{run, cwd, session, stdin})
    Cache->>Store: lookup(run + cwd + session)
    alt cache is fresh
        Store-->>Cache: cached output
    else cache is stale or missing
        Cache->>Refresh: refresh-cmd --cwd --session -- run
        Refresh->>Store: refresh(run, cwd, session, timeout)
        Store-->>Cache: session-specific output
    end
    Cache-->>Metric: output for this session
Loading

Flow diagram for session-aware command cache keys

flowchart LR
    Job[Job with run, cwd, session] --> Key["file_key(run + cwd + session)"]
    Key --> Cache[Session-specific output cache]
    Cache --> Render[Render command output]
    Job --> Refresh[refresh-cmd with --session]
    Refresh --> Cache
Loading

File-Level Changes

Change Details Files
Include the session identifier in command-output cache identity.
  • Extended cache-path hashing from run and working directory to run, working directory, and optional session.
  • Preserved a shared key shape for callers without a session identifier.
  • Added coverage for stable same-session paths and distinct cross-session or sessionless paths.
src/state/cmd_cache.rs
Propagate session context through command refreshes.
  • Added an optional --session CLI argument to refresh-cmd.
  • Forwarded the session identifier from the CLI into cache refresh logic.
  • Forwarded session context when spawning background refresh commands.
src/cli.rs
src/main.rs
src/state/cmd_cache.rs
Pass the originating session into custom command metrics.
  • Populated command jobs with the input session ID so rendering and refreshing use the same cache key.
src/metrics/custom.rs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/state/cmd_cache.rs" line_range="32" />
<code_context>
     pub cwd: Option<&'a Path>,
+    /// The session the payload comes from: each session keeps its own
+    /// output, as the command may read session details from stdin.
+    pub session: Option<&'a str>,
     /// The status line payload, passed on stdin.
     pub stdin: &'a str,
</code_context>
<issue_to_address>
**Downstream Rust callers stop compiling**

When a downstream crate uses the public `ccometixline::state::cmd_cache` API, downstream crates that construct `Job` with the previous fields or call `cmd_cache::refresh` with its previous arguments fail to compile; the new required field and changed function arity are breaking public API changes.

Preserve the existing public entry points and add session-aware alternatives, or treat this as an intentional breaking API change with an appropriate major-version release.

Also at `src/state/cmd_cache.rs:175`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and including the session in the cache key makes each session keep a separate command result and can cause the command to run separately for each one. If the keying or cached output is wrong, a session may see the wrong result until the cache is refreshed; rerunning or clearing the cache repairs it.

Blocking findings: src/state/cmd_cache.rs:32


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/state/cmd_cache.rs
pub cwd: Option<&'a Path>,
/// The session the payload comes from: each session keeps its own
/// output, as the command may read session details from stdin.
pub session: Option<&'a str>,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Downstream Rust callers stop compiling

When a downstream crate uses the public ccometixline::state::cmd_cache API, downstream crates that construct Job with the previous fields or call cmd_cache::refresh with its previous arguments fail to compile; the new required field and changed function arity are breaking public API changes.

Preserve the existing public entry points and add session-aware alternatives, or treat this as an intentional breaking API change with an appropriate major-version release.

Also at src/state/cmd_cache.rs:175.

Prompt for AI agents
In `src/state/cmd_cache.rs` at line 32:

**Downstream Rust callers stop compiling**

When a downstream crate uses the public `ccometixline::state::cmd_cache` API, downstream crates that construct `Job` with the previous fields or call `cmd_cache::refresh` with its previous arguments fail to compile; the new required field and changed function arity are breaking public API changes.

Preserve the existing public entry points and add session-aware alternatives, or treat this as an intentional breaking API change with an appropriate major-version release.

Also at `src/state/cmd_cache.rs:175`.

@Haleclipse
Haleclipse merged commit 1749ff8 into Haleclipse:master Nov 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants