Skip to content

fix(sessions): read session titles from top-level transcript entries - #1427

Open
mertkayacs wants to merge 2 commits into
anthropics:mainfrom
mertkayacs:fix/session-title-tool-input
Open

mertkayacs wants to merge 2 commits into
anthropics:mainfrom
mertkayacs:fix/session-title-tool-input

Conversation

@mertkayacs

Copy link
Copy Markdown

Summary

list_sessions(), get_session_info() and fork_session() read customTitle and aiTitle by scanning the raw transcript text for the key at any depth. When a custom MCP tool's input has a customTitle field, that value replaces the title the user set, and the default fork title is derived from it too.

  • read customTitle and aiTitle only from top-level JSONL entries
  • keep tool inputs from changing session listings and fork titles
  • add regressions for the listing and fork paths

Testing

  • python -m pytest tests/ (1631 passed, 6 skipped)
  • python -m ruff check src/ tests/
  • python -m ruff format --check src/ tests/
  • python -m mypy src/

@Somay-kousis Somay-kousis 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.

Ran this locally on c89e009 (based on main 7b790c7). The fix does what it says. One narrow regression from switching to whole-line parsing, details below.

What I ran

  • The 3 new tests: 3 passed on the PR head. With src/ reverted to 7b790c7 and the new tests kept, all 3 fail with the nested value winning ('Nested tool value' == 'My Custom Title', 'Nested tool value (fork)' == 'Original title (fork)'). So they catch the bug.
  • tests/test_sessions.py tests/test_session_mutations.py: 178 passed.
  • Full suite: 1631 passed, 6 skipped.
  • ruff check src/ tests/ scripts/, ruff format --check src/ tests/ scripts/, mypy src/ scripts/: clean.
  • Store path for the same input (list_sessions_from_store with a top-level custom-title entry followed by a tool_use whose input has customTitle): already returns the real title on main, since session_summary.py reads top-level keys. This PR brings the local-disk path in line with it.

Finding (worth considering): a title line cut by the tail window is now dropped

_extract_last_top_level_json_string_field (sessions.py:254) only accepts lines that parse as JSON. tail is the last 64 KiB of the file, so its first line is usually a fragment. The old regex scan still found "customTitle":"..." inside that fragment. The new code skips it, and if the file is over 128 KiB the head fallback does not have it either.

Repro (probe_1427.py, case 1): a session of 136083 bytes whose only custom-title line starts at byte 70542, with the tail window starting at byte 70547.

  • main: custom_title = 'Straddle title'
  • this PR: custom_title = None

It needs a large file, no later title entry, and the window boundary landing in the few bytes before the key, so it is rare. Possible fix: for the first line of tail only, if json.loads fails and the fragment contains "type":"custom-title" or "type":"ai-title", fall back to _extract_last_json_string_field on that fragment. Or accept the tradeoff and note it in the docstring. I did not check whether the CLI re-appends title entries near the end of the file, which would make this moot.

Nit

session_mutations.py:565: the docstring still says precedence matches _extract_last_json_string_field. That helper is no longer used for titles.

Same bug class, not in this PR

summary, lastPrompt and gitBranch still use the any-depth scan (sessions.py:470-471, 480). A tool_use input with a summary key becomes the session summary when there is no title, and one with gitBranch becomes git_branch. Same output on main and this PR (probe_1427.py cases 2 and 3). Maybe a follow-up, since summary is a common tool input field name.

Not checked

Performance on large transcripts (the new code json.loads every line that contains the key). Windows line endings. Python 3.10.

@mertkayacs
mertkayacs force-pushed the fix/session-title-tool-input branch from c89e009 to b4d8f9a Compare October 25, 2026 11:45
@mertkayacs

Copy link
Copy Markdown
Author

@Somay-kousis Good catch, thanks for the repro. When the first line in the tail window is cut, the title is now taken only if it sits at the top level of that entry, so nested tool values still lose. Your exact file returns "Straddle title" again, and it's a test now. I also fixed the old docstring in session_mutations.py.

@Somay-kousis

Copy link
Copy Markdown

Reran on b4d8f9a: my straddle file returns 'Straddle title' again, and test_sessions.py + test_session_mutations.py pass (182). Thanks.

This branch has not been deployed

No deployments
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