Skip to content

fix(conductor): decode bridge child output with errors=replace again - #3397

Open
asheshgoplani wants to merge 1 commit into
mainfrom
fix/bridge-non-utf8-cli-output
Open

asheshgoplani wants to merge 1 commit into
mainfrom
fix/bridge-non-utf8-cli-output

Conversation

@asheshgoplani

@asheshgoplani asheshgoplani commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

What problem does this solve?

The conductor bridge crashes again when agent-deck prints a byte that is not valid UTF-8 (ANSI art, a multibyte character cut in half, locale-encoded output). run_cli raises UnicodeDecodeError and the bridge dies. This was fixed in #920 (via #926), and the fix was lost when #1406 moved bridge.py to a single go:embed source. Refs #920.

Why this change

run_cli uses subprocess.Popen(..., text=True) with strict decoding. It now decodes as UTF-8 with errors="replace", so bad bytes become U+FFFD. The encoding and error policy live in one pair of module constants (_CHILD_TEXT_ENCODING, _CHILD_TEXT_ERRORS) that each text-mode child call passes.

Sibling: run_hook had the same strict decode. A hook that printed a non-UTF-8 byte and exited 0 was reported as Hook crashed (exit 1). It now uses the same constants.

Left strict on purpose: the macOS keychain lookup (/usr/bin/security ... -w). Its output is a secret, so it must not be silently altered; a decode error there is already caught and returns "".

So the fix cannot be lost silently again, TestBridgeChildOutputSurvivesNonUTF8Bytes runs the embedded bridge.py against a fake agent-deck and a fake hook that print invalid UTF-8 on stdout and stderr. It covers run_cli (with and without stdin), get_session_status, get_session_output and run_hook, under both C.UTF-8 and C locales. Each row checks that the call succeeds and the bad bytes became U+FFFD.

User impact

The Telegram/Slack/Discord conductor bridge no longer crashes, and hooks are no longer reported as failed, when CLI or hook output contains a non-UTF-8 byte. Bad bytes show as U+FFFD.

Evidence

Fail first, on main 0b4bc1d2 with only the new test added (deck-test -count=1 -run TestBridgeChildOutputSurvivesNonUTF8Bytes -v ./internal/session/):

--- FAIL: TestBridgeChildOutputSurvivesNonUTF8Bytes (0.99s)
    --- FAIL: .../C.UTF-8/run_cli_stdout_and_stderr      File "/usr/lib/python3.14/subprocess.py", line 2194, in _communicate ... UnicodeDecodeError
    --- FAIL: .../C.UTF-8/run_hook                        AssertionError: (1, '', "'utf-8' codec can't decode byte 0xff in position 5: invalid start byte")
    (all 10 rows FAIL)

With the fix:

--- PASS: TestBridgeChildOutputSurvivesNonUTF8Bytes (0.88s)   (all 10 rows PASS)
ok  	github.com/asheshgoplani/agent-deck/internal/session	59.929s

Mutation: with only the run_hook decode reverted, the run_hook rows fail and the others pass (see the lane log).
Full suite on 93b2596 in the sandbox container (deck-test -count=1 ./...): 113 packages ok. internal/session and cmd/agent-deck hit go test's default 10m timeout under host load (CI uses 20m). Rerun with -timeout 30m: internal/session ok. In cmd/agent-deck the only failure is TestPerf_ColdStart_Version (40.5 ms against a 40 ms budget); the same perf tests fail on unmodified main in the same container at the same time (TestPerf_ColdStart_Help and _Version). tools/visualcheck passed in the full run and once showed a 12-attach-shell golden diff in the rerun; this change does not touch the TUI.

Review notes

Two independent reviews (Claude, Codex) both passed. Nits, left as is:

  • the C locale rows repeat the C.UTF-8 ones: Python already decodes as UTF-8 under the C locale
  • errors="replace" also applies when run_cli encodes stdin text (a lone surrogate becomes ?)
  • the keychain call has no comment saying it is deliberately strict
  • the get_session_status row only checks that a string comes back
  • one #nosec G306 is redundant

Not verified: a live Telegram round trip with a real conductor; the test drives the real bridge.py code paths with a fake CLI.

AI disclosure

  • Human-written
  • AI-assisted (I directed and reviewed it)
  • AI-authored (a model wrote most of it)

Model(s), if AI helped: unsure

Prompt / session log (optional): —

What actually bothered you

A new regression-test sweep found that the #920 crash fix was gone. My human asked: "conductor bridge run_cli crashes on non-UTF-8 output again: the #920 fix (decode with errors=replace) was lost in #1406. Restore it so it can't be lost again (a test that feeds invalid UTF-8 through run_cli)."

Checklist

  • Targeted diff: one problem, no unrelated changes
  • Tests added or updated for new behavior
  • Test suite passes sandboxed, inside a container: run skills/agent-deck/scripts/contributing/self-check.sh, which prints the Docker command (never go test on your host)
  • If this touches a hot path (list, status, session output, startup, tmux layer): before/after timing evidence included (not a hot path: the decode options of two subprocess calls in the bridge)
  • CHANGELOG.md untouched (entries are added at landing)
  • AI-assisted? Disclosed above, with validation evidence, and I can answer questions about the code
  • "Allow edits from maintainers" is enabled

The #920 fix (decode CLI output with errors="replace") was lost when #1406
moved bridge.py to a single go:embed source: run_cli went back to strict
text-mode decoding, so one non-UTF-8 byte in agent-deck output (ANSI art, a
multibyte char cut in half) raised UnicodeDecodeError and crashed the bridge.

run_cli and run_hook now decode child output as UTF-8 with errors="replace"
from one pair of module constants. run_hook had the same strict decode: a
hook that printed a non-UTF-8 byte was reported as failed even when it
exited 0.

TestBridgeChildOutputSurvivesNonUTF8Bytes feeds invalid UTF-8 through
run_cli, get_session_status, get_session_output and run_hook under both
C.UTF-8 and C locales, so the decode cannot be lost silently again.

Committed by Ashesh Goplani
@github-actions github-actions Bot added intake:clean PR/issue passed the intake contract ai-authored Primarily authored by an AI agent labels Dec 1, 2026
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

Parsing warnings: 5

Validation error: Unrecognized key: "requirements" at "reviews.pre_merge_checks.description"; Unrecognized key: "threshold" at "reviews.pre_merge_checks.issue_assessment"; Unrecognized key: "gosec" at "reviews.tools"; Unrecognized keys: "estimated_code_review_effort", "ignore_users" at "reviews"; Unrecognized key: "language" at "code_generation.unit_tests"
Configuration instructions

  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

No actionable comments were generated in the recent review.

Walkthrough

The bridge now decodes CLI and hook subprocess output as UTF-8, replacing invalid byte sequences. Regression tests cover CLI and hook output under C.UTF-8 and C locales.

Changes: Child-process output handling

Layer / File(s) Summary
UTF-8 decoding and regression coverage
internal/session/conductor_bridge.py, internal/session/conductor_bridge_non_utf8_test.go
run_cli and run_hook use UTF-8 decoding with replacement for invalid bytes. Regression tests cover CLI and hook output across two locales.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: johnkcox

Merge Risk: Minimal · up to 93b25

The bridge now tolerates invalid bytes in CLI and hook output instead of crashing or reporting successful hooks as failed. A regression test covers the change, and no merge-blocking risk was found.

Pre-merge checks: Passed 6

Check name Status Explanation
Title check Passed The title uses the Conventional Commits format with the fix(conductor) prefix, which matches the configured title requirement. It also describes the main change: the bridge child output now decodes wi…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Remote_parity Passed The remote_parity check applies only when a pull request changes TUI behavior in internal/ui/ or cmd/agent-deck/. The reviewed diff (refs/coderabbit/pre-merge-check/base..head) changes two files, both…
Test_coverage_per_surface Passed The check applies to new features. It requires tests for each applicable surface (TUI / Web / CLI / remote) and forbids Web UI stubs. This pull request is a bug fix, not a new feature. The PR title, d…
Full details: Title check

Explanation

The title uses the Conventional Commits format with the fix(conductor) prefix, which matches the configured title requirement. It also describes the main change: the bridge child output now decodes with errors=replace again, which matches the summary of the bridge.py UTF-8 handling change and the regression test.

Full details: Remote_parity

Explanation

The remote_parity check applies only when a pull request changes TUI behavior in internal/ui/ or cmd/agent-deck/. The reviewed diff (refs/coderabbit/pre-merge-check/base..head) changes two files, both under internal/session/: conductor_bridge.py (M) and conductor_bridge_non_utf8_test.go (A). It touches no file under internal/ui/ or cmd/agent-deck/. The check is therefore inapplicable, and the PR passes. The PR changes decoding in the conductor bridge subprocess calls (run_cli, run_hook), not TUI behavior, so no RemoteSession handling is required for this change.

Full details: Test_coverage_per_surface

Explanation

The check applies to new features. It requires tests for each applicable surface (TUI / Web / CLI / remote) and forbids Web UI stubs. This pull request is a bug fix, not a new feature. The PR title, description, and objectives all describe restoring the #920 fix: run_cli and run_hook now decode child output with errors="replace". The diff (confirmed with git diff against the review base) changes two files: - internal/session/conductor_bridge.py (+12/-0): adds _CHILD_TEXT_ENCODING and _CHILD_TEXT_ERRORS constants and passes them to the run_cli and run_hook subprocess calls. It adds no new user-facing feature and no new surface. - internal/session/conductor_bridge_non_utf8_test.go (new): a regression test that runs the embedded bridge.py with a fake CLI and hook. It covers run_cli, get_session_status, get_session_output, and run_hook. No new TUI, Web, CLI, or remote feature is introduced, so the per-surface coverage requirement is not triggered. The new test also covers the bridge paths that the fix touches. The primary PR author is asheshgoplani (verified).

Resolution

None required.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0b4bc1d and 93b2596.

Files selected for processing: 2

  • internal/session/conductor_bridge.py
  • internal/session/conductor_bridge_non_utf8_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

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

ai-authored Primarily authored by an AI agent intake:clean PR/issue passed the intake contract

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant