Skip to content

Update --json mode exit handling, type safety, and adapter consistency - #3081

Merged
sejas merged 1 commit into
add/cli-studio-ai-json-flagfrom
update/cli-studio-ai-json-flag
Apr 14, 2026
Merged

Update --json mode exit handling, type safety, and adapter consistency#3081
sejas merged 1 commit into
add/cli-studio-ai-json-flagfrom
update/cli-studio-ai-json-flag

Conversation

@sejas

@sejas sejas commented Apr 13, 2026

Copy link
Copy Markdown
Member

Related issues

Proposed Changes

  • Replace process.exit() with process.exitCode in JSON mode single-turn path and JsonAdapter.askUser() so the event loop drains naturally — stdout flushes and async session persistence completes before the process terminates
  • Refactor HandleMessageResult from a fragile optional-undefined union to a proper discriminated union with a type field ('result' | 'max_turns')
  • Make JsonAdapter.showProgress() emit progress NDJSON events consistently with setLoaderMessage() instead of silently discarding input
  • Replace unsafe ui as AiChatUI cast with a runtime instanceof guard that throws a clear error if the adapter isn't AiChatUI

Testing Instructions

  1. Build the CLI: npm run cli:build
  2. Run in JSON mode and verify NDJSON events stream correctly and the process exits cleanly:
    node apps/cli/dist/cli/main.mjs code "hello" --json
  3. Verify error exit code is set on failure (e.g., with invalid API key)
  4. Run tests: npm test -- apps/cli/commands/ai/tests/ai.test.ts

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?
…and adapter consistency

- Replace process.exit() with process.exitCode in JSON mode to let the event loop drain naturally,
  ensuring stdout flushes and async persistence completes before the process terminates
- Use discriminated union (type field) for HandleMessageResult instead of optional undefined trick
- Make showProgress emit progress events consistently with setLoaderMessage
- Add runtime guard for AiChatUI instanceof check instead of unsafe type cast

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sejas sejas self-assigned this Apr 13, 2026
@sejas
sejas requested a review from wesleyfantinel April 13, 2026 23:32
@sejas sejas changed the title Harden JSON mode exit handling, type safety, and adapter consistency Apr 13, 2026
@sejas
sejas merged commit d0692d7 into add/cli-studio-ai-json-flag Apr 14, 2026
6 checks passed
@sejas
sejas deleted the update/cli-studio-ai-json-flag branch April 14, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant