Skip to content

listSessions() returns cwd: undefined when the first user message contains a large image #492

Description

@mantou132

Version: @anthropic-ai/claude-agent-sdk 0.3.286 (via @agentclientprotocol/claude-agent-acp 0.85.1), macOS

Description

listSessions() gets cwd by searching for "cwd":" in only the first 64 KB of the transcript. When the first user message has an inline base64 image, that JSONL line can be hundreds of KB long. The cwd field sits at the end of the line, so it falls outside the 64 KB window. The session then comes back with cwd: undefined.

Example from one transcript:

line 1  len=138     queue-operation
line 2  len=138     queue-operation
line 3  len=274326  first user message (base64 screenshot), "cwd" at byte ~274204

Impact

Consumers that need cwd drop these sessions. For example, claude-agent-acp's session/list skips any entry without cwd, so ACP clients never see the session. From the user's side, sessions that start with a screenshot "disappear" from the list. On one machine 13 real sessions were hidden this way, even though the transcripts were complete and could still be resumed with claude --resume.

Steps to reproduce

  1. Start a session whose first prompt includes a screenshot. The base64 data needs to be larger than about 64 KB.
  2. Call listSessions().
  3. That session's entry has cwd: undefined. Sessions without an image in the first prompt are fine.

Proposed fix

If cwd is not found in the head buffer, keep reading forward from where the head ends until the first "cwd":" is found. This gives the same value as for normal sessions: the cwd of the first user record. The extra read only happens for these transcripts and costs about the size of the inline image, so normal sessions are not affected.

A cheaper partial fallback is to look in the tail buffer, which is already read. But the tail can hold a different cwd if the session switched directories mid-way (e.g. via EnterWorktree), so reading forward is the accurate option.

Workaround

Moving the cwd key to the start of the first user record (the JSON content stays the same) makes the session show up again.

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions