Skip to content

fix: filter non-JSON log messages from ACP agent output#160

Open
gandli wants to merge 5 commits intoopenclaw:mainfrom
gandli:fix/iflow-json-parse-error
Open

fix: filter non-JSON log messages from ACP agent output#160
gandli wants to merge 5 commits intoopenclaw:mainfrom
gandli:fix/iflow-json-parse-error

Conversation

@gandli
Copy link
Contributor

@gandli gandli commented Mar 18, 2026

Problem

acpx fails to integrate with iflow CLI due to JSON parse errors when the agent outputs log messages like:

[iFlow ACP Agent] ACP adapter factory initialized
Failed to parse JSON message: [iFlow ACP Agent] ACP adapter factory initialized
SyntaxError: Unexpected token 'i', "[iFlow ACP "... is not valid JSON

Root Cause

iflow CLI outputs initialization log messages to stdout/stderr during startup. These non-JSON lines are passed to @agentclientprotocol/sdk's ndJsonStream parser, which attempts to parse them as JSON and fails.

Solution

Added two-layer filtering to prevent non-JSON log messages from breaking the ACP protocol stream:

  1. filterAcpOutputStream() - New method that filters the output stream before ndJsonStream parsing, with:
    • Fail-fast mechanism: throws error after 10 consecutive non-JSON lines
    • EOF buffer flush: handles final JSON-RPC frame without trailing newline
  2. isValidAcpMessage() - Additional validation layer in createTappedStream()

Testing

  • ✅ All 253 existing tests pass
  • acpx iflow sessions new now works without JSON parse errors
  • ✅ Compatible with other ACP agents that output log messages

Impact

Fixes integration with iflow CLI and other ACP agents that output initialization log messages to stdout/stderr during startup.

Codex Review Addressed

  • ✅ P2: Fail fast on stdout protocol violations - Added error after 10 consecutive non-JSON lines
  • ✅ P2: Flush the buffered final frame at EOF - Buffer is now flushed when stream closes

gandli added 5 commits March 18, 2026 22:09
- Add filterAcpOutputStream() to filter out non-JSON lines from agent stdout before ndJsonStream parsing
- Add isValidAcpMessage() check in createTappedStream() as additional safety layer
- Prevents '[iFlow ACP Agent] ...' style log messages from causing JSON parse errors in @agentclientprotocol/sdk

Fixes integration with iflow CLI and other ACP agents that output log messages to stdout/stderr.
This hook automatically removes any Co-authored-by lines from commit messages,
preventing AI assistants from adding their signature to commits.
- Remove unused 'parsed' variable in filterAcpOutputStream()
- Keep JSON.parse() call for validation but don't assign to variable
- Add fail-fast mechanism: throw error after 10 consecutive non-JSON lines
  to detect adapters that don't support stdio ACP mode
- Flush buffered content at EOF to handle final JSON-RPC frame without trailing newline
- Reset non-JSON counter after successful JSON parse to allow occasional log lines

Addresses:
- P2: Fail fast on stdout protocol violations
- P2: Flush the buffered final frame at EOF
The global git template at ~/.git-templates/hooks/commit-msg
will handle removing Co-authored-by lines for all repositories.
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

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