Skip to content

fix(acp): reuse session by cwd in NewSession to persist history across Zed reloads #1600

Closed
Pnkcaht wants to merge 1 commit intodocker:mainfrom
Pnkcaht:fix/zed-session-history
Closed

fix(acp): reuse session by cwd in NewSession to persist history across Zed reloads #1600
Pnkcaht wants to merge 1 commit intodocker:mainfrom
Pnkcaht:fix/zed-session-history

Conversation

@Pnkcaht
Copy link
Contributor

@Pnkcaht Pnkcaht commented Feb 4, 2026

What I Did

  • Updated NewSession in pkg/acp/agent.go to persist and reuse sessions by working directory (cwd) using the SQLite session store.
  • Use params.Cwd as a stable key to identify sessions (fallback to new UUID if empty).
  • Check existing sessions in the DB via store.GetSessions(ctx).
  • If a matching session by WorkingDir is found → reuse its ID and loaded session object.
  • If not found → create new session, set sessObj.WorkingDir = key, and persist immediately with store.AddSession.
  • Runtime is created with the persistent store, ensuring history is loaded/saved.
  • Added debug logs for reuse/create paths and warnings for store failures.

This prevents session history loss after Zed reloads when cagent runs as an ACP server.

Releated issue

Closes #1591

Before vs After

Before (history lost after Zed reload):

  • Every NewSession call from Zed creates a new UUID.
  • No lookup in persistent store → always new in-memory session.
  • After Zed reload → new session ID → no previous messages/history visible.
  • cagent sessions shows only current in-memory sessions (old ones disappear).

After (history preserved across Zed reloads):

  • NewSession uses params.Cwd as key.
  • If a session with matching WorkingDir exists in cagent_sessions.db → reuse same session ID and load full state.
  • Zed reconnects → gets same session ID → history (messages, context) loads from SQLite store.
  • New sessions only created when cwd is truly new or missing.

Flow Diagram (Before vs After)

sequenceDiagram
    participant Zed as Zed Editor
    participant Cagent as cagent (ACP Server)
    participant DB as cagent_sessions.db

    Note over Zed,Cagent: Before (broken)
    Zed->>Cagent: NewSession (cwd: /project)
    Cagent-->>Zed: New UUID
    Note over Zed: Reload
    Zed->>Cagent: NewSession (same cwd)
    Cagent-->>Zed: Another new UUID
    Note over Zed: History lost

    Note over Zed,Cagent: After (fixed)
    Zed->>Cagent: NewSession (cwd: /project)
    Cagent->>DB: Find by WorkingDir
    DB-->>Cagent: Reuse ID abc-789
    Cagent-->>Zed: Same SessionId
    Note over Zed: Reload → History preserved
Loading

…s Zed reloads (docker#1591)

Signed-off-by: pnkcaht <samzoovsk19@gmail.com>
@Pnkcaht Pnkcaht requested a review from a team as a code owner February 4, 2026 19:37
@krissetto
Copy link
Contributor

I implemented the proper fixes in #1601 to make sure we follow the patterns used for other commands in cagent and create a consistent experience

@Pnkcaht
Copy link
Contributor Author

Pnkcaht commented Feb 4, 2026

I implemented the proper fixes in #1601 to make sure we follow the patterns used for other commands in cagent and create a consistent experience

Ok, thanks friend :)

@krissetto krissetto closed this Feb 5, 2026
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.

Zed session history lost after reload when using cagent as ACP server

2 participants