Skip to content

feat: emit permission_asked events in --format json mode for headless integrations - #39447

Closed
baizhu945 wants to merge 1 commit into
anomalyco:devfrom
baizhu945:feat/json-format-permission-events
Closed

feat: emit permission_asked events in --format json mode for headless integrations#39447
baizhu945 wants to merge 1 commit into
anomalyco:devfrom
baizhu945:feat/json-format-permission-events

Conversation

@baizhu945

Copy link
Copy Markdown

Problem

When OpenCode runs in --format json non-interactive mode (as used by external tools like cc-connect), all permission requests are automatically rejected internally. The permission.asked event is never emitted to stdout, so external tools cannot relay permission verification cards to users on messaging platforms (Telegram, Feishu, etc.).

See: chenhg5/cc-connect#1420

Solution

In --format json mode:

  1. Keep stdin open for permission replies: stdin is no longer consumed for the prompt (the prompt is passed as a positional command-line argument), so it stays available as a reply channel
  2. Emit permission_asked JSON events to stdout with full permission details (id, permission name, patterns, metadata, tool info)
  3. Read permission replies from stdin via readPermissionReply() helper using node:readline, with a 120-second timeout defaulting to "reject"

Data Flow

cc-connect → opencode run --format json "user message"
                ↓ (wants to call a tool)
                ↓ emits permission_asked JSON → stdout
                ↓ blocks reading stdin for reply
cc-connect receives JSON → shows permission card to user on Telegram
user clicks [Allow] / [Deny]
cc-connect writes {"requestID":"…","reply":"once"} → stdin
                ↓ opencode continues with approved tool call

Changes

  • packages/opencode/src/cli/cmd/run.ts: Modified stdin handling, permission.asked event handler, added readPermissionReply() helper

Testing

Tested end-to-end with cc-connect + Telegram bot. Non-JSON-format mode behavior is unchanged.

… integrations

Enables OpenCode in --format json headless mode to emit permission_asked
NDJSON events to stdout and accept permission replies via stdin, instead
of automatically auto-rejecting all permission requests.

This allows external tools (e.g. cc-connect) to relay permission verification
cards to messaging platforms and return user decisions back to OpenCode.

Changes:
- Keep stdin open in --format json mode for permission reply channel (prompt
  is passed as positional argument instead of piped via stdin)
- Emit permission_asked JSON event with full permission details
- Add readPermissionReply() helper using node:readline, with 120s timeout
  defaulting to reject

See: chenhg5/cc-connect#1420
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

The search found one potentially related PR:

Related PR:

The search results primarily show PR #39447 itself, suggesting there are no exact duplicates currently open. The #37902 PR is related to permission handling but addresses a different scope (child/subagent sessions vs. headless integrations).

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jul 29, 2026
@github-actions github-actions Bot closed this Jul 29, 2026
@baizhu945
baizhu945 deleted the feat/json-format-permission-events branch July 29, 2026 06:12
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.

1 participant