Fix Cursor hook misattribution and add token usage support#1263
Open
SnowingFox wants to merge 2 commits into
Open
Fix Cursor hook misattribution and add token usage support#1263SnowingFox wants to merge 2 commits into
SnowingFox wants to merge 2 commits into
Conversation
1. Add transcript-owner guard in executeAgentHook to prevent Cursor sessions from being claimed by Claude Code when only .claude/settings.json is installed (fixes entireio#1262). 2. Parse token fields (input_tokens, output_tokens, cache_read_tokens, cache_write_tokens) from Cursor's stop hook payload into Event.TokenUsage so Cursor sessions report non-zero token counts. 3. Prefer hook-provided TokenUsage over transcript-based calculation in handleLifecycleTurnEnd, since Cursor's JSONL transcript has no usage data. Co-authored-by: Cursor <cursoragent@cursor.com> Entire-Checkpoint: 2a89e658287e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
close #1262

close #1264
Dispatch — feat/cursor-hook-guard-and-tokens
Summary
This branch fixes Cursor session misattribution when only Claude Code hooks are installed (#1262), and adds token usage reporting for Cursor sessions by parsing the
stophook payload — the only authoritative source, since Cursor's JSONL transcript contains no usage fields.Work Items
Bug Fix: Cross-agent hook forwarding guard (#1262)
shouldSkipForwardedHook()inhook_guard.go— checks ifevent.SessionRef(transcript path) belongs to a different registered agent viaAgentForTranscriptPathexecuteAgentHook()inhook_registry.go, beforeDispatchLifecycleEvent— silently skips with a debug log when a forwarded hook is detected.claude/settings.jsonbut no.cursor/hooks.json; Cursor IDE fires Claude Code hooks; transcript path proves it's a Cursor sessionFeature: Cursor token usage from stop hook
TokenUsage *TokenUsagefield toagent.Event— allows hook parsers to inject authoritative token data that the framework prefers over transcript-based computationstopHookInputRawincursor/types.gowithinput_tokens,output_tokens,cache_read_tokens,cache_write_tokensfieldstokenUsageFromStop()incursor/lifecycle.go— derives fresh input tokens asmax(0, total_input - cache_read - cache_write), matching the story/apps/cli approachhandleLifecycleTurnEndinlifecycle.goto preferevent.TokenUsageoveragent.CalculateTokenUsage()fallbackTests
hook_guard_test.go: transcript belongs to other agent (skip), transcript belongs to self (pass through), fullexecuteAgentHookintegration test confirming no state file createdlifecycle_test.go:TestHandleLifecycleTurnEnd_PrefersEventTokenUsage— verifies hook-provided tokens flow into session statecursor/lifecycle_test.go: token extraction from stop hook, nil when omitted, negative clamp to zero