Skip to content

CM-62381-add-session-start-hook#434

Open
RoniCycode wants to merge 3 commits intomainfrom
CM-62381-refactor-ensure-auth-command
Open

CM-62381-add-session-start-hook#434
RoniCycode wants to merge 3 commits intomainfrom
CM-62381-refactor-ensure-auth-command

Conversation

@RoniCycode
Copy link
Copy Markdown
Collaborator

  • Change "ensure-auth" to "session-start" which includes conversation creation and MCP usage checks (once per session)

Comment on lines +71 to +75
# Step 2: Read stdin payload (backward compat: old hooks pipe no stdin)
if sys.stdin.isatty():
logger.debug('No stdin payload (TTY), skipping session initialization')
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain

Copy link
Copy Markdown
Collaborator Author

@RoniCycode RoniCycode Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it does: sys.stdin.isatty() returns True when stdin is attached to a terminal — i.e. nothing is piped in. If we didn't have this guard, sys.stdin.read() on the next line would block forever waiting
for input that never arrives.

Why it's needed:

  1. Manual/interactive invocation. If you run cycode ai-guardrails session-start in a shell to debug it, stdin is the terminal. Without this check, the command would hang.
  2. Backward compat with the old ensure-auth hook. In init.py we kept ensure-auth as a deprecated alias:
    app.command(hidden=True, name='ensure-auth', ...)(session_start_command)
  3. Users who installed hooks before this branch have cycode ai-guardrails ensure-auth wired into their IDE config. The old command only ran auth and didn't read stdin. After they upgrade the CLI, that same
    hook now invokes session_start_command. The guard ensures that even if the invocation context somehow doesn't provide a JSON payload on stdin, we still do the auth step (which is what the old command did)
    and then gracefully skip the conversation/MCP reporting steps instead of hanging.
    Flow:
  • Hook invoked by IDE → stdin is a pipe → isatty() is False → read JSON, create conversation, report MCP
  • Hook invoked with no stdin (manual / edge case) → isatty() is True → auth happens above, then we return early

If you want to make the intent clearer, the comment could be:

  • No piped input: would block forever on read(). Happens when run manually,
  • or with the legacy ensure-auth hook which didn't supply stdin.

def _build_session_payload(payload: dict, ide: str) -> AIHookPayload:
"""Build an AIHookPayload from a session-start stdin payload."""
if ide == AIIDEType.CLAUDE_CODE:
ide_version, model, _ = _extract_from_claude_transcript(payload.get('transcript_path'))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this function definition and conversation creation on every hook

model=payload.get('model'),
ide_provider=AIIDEType.CLAUDE_CODE.value,
ide_version=ide_version,
ide_version=None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still need to get the version

if not mcp_servers:
return
try:
ai_client.report_data_flow(mcp_servers)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename

return
try:
ai_client.report_data_flow(mcp_servers)
except Exception as e:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already have try catch inside report_data_flow

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