-
Notifications
You must be signed in to change notification settings - Fork 1
feat(claude-code-hook): report Cowork sessions as unbound_app_label='cowork' #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1178,6 +1178,24 @@ def build_account_identity(probe: bool = False) -> Dict: | |||||||||||||||||||||||||||||||||||||||||||
| return identity | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def _unbound_app_label() -> str: | ||||||||||||||||||||||||||||||||||||||||||||
| """Cowork sessions run the same CLI + hooks as Claude Code; the desktop | ||||||||||||||||||||||||||||||||||||||||||||
| app marks the surface in the hook environment. Report them under their | ||||||||||||||||||||||||||||||||||||||||||||
| own label so the gateway can scope policies/analytics per surface. | ||||||||||||||||||||||||||||||||||||||||||||
| Requires gateway support for 'cowork' (ai-gateway#716) — old | ||||||||||||||||||||||||||||||||||||||||||||
| gateways would drop the label out of their label-keyed maps.""" | ||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||
| if os.environ.get('CLAUDE_CODE_IS_COWORK') == '1': | ||||||||||||||||||||||||||||||||||||||||||||
| return 'cowork' | ||||||||||||||||||||||||||||||||||||||||||||
| if os.environ.get('CLAUDE_CODE_ENTRYPOINT') in ( | ||||||||||||||||||||||||||||||||||||||||||||
| 'local-agent', 'local_agent', 'remote_cowork' | ||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||
| return 'cowork' | ||||||||||||||||||||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||||||
| return 'claude-code' | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1187
to
+1196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def process_pre_tool_use(event: Dict, api_key: str) -> Dict: | ||||||||||||||||||||||||||||||||||||||||||||
| """Process PreToolUse event - DO NOT LOG.""" | ||||||||||||||||||||||||||||||||||||||||||||
| session_id = event.get('session_id') | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1248,7 +1266,7 @@ def process_pre_tool_use(event: Dict, api_key: str) -> Dict: | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| request_body = { | ||||||||||||||||||||||||||||||||||||||||||||
| 'conversation_id': session_id, | ||||||||||||||||||||||||||||||||||||||||||||
| 'unbound_app_label': 'claude-code', | ||||||||||||||||||||||||||||||||||||||||||||
| 'unbound_app_label': _unbound_app_label(), | ||||||||||||||||||||||||||||||||||||||||||||
|
cursor[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||
| 'model': model, | ||||||||||||||||||||||||||||||||||||||||||||
| 'event_name': 'tool_use', | ||||||||||||||||||||||||||||||||||||||||||||
| 'pre_tool_use_data': { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1343,7 +1361,7 @@ def process_user_prompt_submit(event: Dict, api_key: str) -> Dict: | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| request_body = { | ||||||||||||||||||||||||||||||||||||||||||||
| 'conversation_id': session_id, | ||||||||||||||||||||||||||||||||||||||||||||
| 'unbound_app_label': 'claude-code', | ||||||||||||||||||||||||||||||||||||||||||||
| 'unbound_app_label': _unbound_app_label(), | ||||||||||||||||||||||||||||||||||||||||||||
| 'model': model, | ||||||||||||||||||||||||||||||||||||||||||||
| 'event_name': 'user_prompt', | ||||||||||||||||||||||||||||||||||||||||||||
| 'account_identity': build_account_identity(), | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
CLAUDE_CODE_IS_COWORK=0is set explicitly (meaning "this is not a Cowork session") butCLAUDE_CODE_ENTRYPOINTis simultaneously set tolocal-agentorremote_cowork, the function skips the IS_COWORK check (it's not'1') and then falls through to the entrypoint check, returning'claude-cowork'anyway. The explicit negative signal is effectively ignored. The entrypoint fallback should only fire whenCLAUDE_CODE_IS_COWORKis absent (None), not when it is explicitly set to'0'. If the desktop app ever sets both variables in a non-Cowork context, sessions will be silently mislabeled and routed against wrong gateway policy maps.