Conversation
📦 Previous Review (superseded by newer review)Code Review: PR #51 - Add Codex SupportOverall assessment: 7.5/10 - Solid foundation with critical gaps that need addressing. SummaryStrengths:
Critical Issues:
Must-Fix Before Merge1. Test Coverage Gaps Files missing tests:
Required tests: Token classification, credential creation/validation, staging directory generation, session persistence Location: internal/credential/openai.go:280 2. Apple Container Race Condition Location: internal/container/apple.go:921-935 The Apple runtime StartAttached() starts the container then attaches (race condition). Docker's implementation correctly attaches first, then starts. Container may exit before attach completes. 3. Detached Mode UX Issue Location: cmd/moat/cli/codex_run.go:196-199 When running with -d flag, users get NO output showing the run ID. They don't know how to attach later. Need to add else clause printing run ID and moat attach command. 4. Session Concurrency Safety Location: internal/session/session.go (save method) File writes lack locking - concurrent sessions can corrupt metadata. Need file locking or atomic write-rename pattern. 5. Network Host Validation Location: cmd/moat/cli/codex_run.go:131-142 User-provided --allow-host values are appended without validation. Invalid hosts cause runtime failures. Should-Fix6. Token Classification Edge Cases (openai.go:280-283) 7. Credential Expiration Handling (codex_run.go:74) 8. Flag Naming Confusion (codex.go:99) 9. Codex Login Timeout (grant.go:574) 10. Documentation Gaps Positive FindingsExcellent Security:
Clean Architecture:
Code Quality:
Testing ChecklistBefore merge:
Per CLAUDE.md Guidelines
Verdict: Strong implementation with thoughtful design, but test coverage is mandatory before production merge. The credential and session code must be tested to ensure security and reliability. |
e4b76c0 to
924b652
Compare
Add `moat codex` command to run OpenAI Codex CLI in isolated containers with credential injection, following the same patterns as `moat claude`. Features: - OpenAI credential support (API keys and ChatGPT subscription tokens) - Automatic --full-auto mode (container provides isolation) - Session management for tracking Codex runs - MCP server configuration support - Network filtering for OpenAI API endpoints Also consolidates shared code: - Move helper functions to cmd/moat/cli/helpers.go - Move ProxyInjectedPlaceholder to credential package - Extract resolveWorkspacePath, parseEnvFlags, hasCredential, hasDependency
Move duplicated session management code from claude and codex packages into a shared internal/session package. Both packages now use thin wrappers that provide the same API while delegating to the shared implementation. This eliminates ~400 lines of duplicated code and makes it easier to add session management for future agent runners.
- Replace recursive menu patterns with loops in grant commands to prevent potential stack overflow from repeated invalid inputs - Add named constants for token length thresholds in IsCodexToken - Make codexKeychainService and codexCredentialsFile private - Fix validateMCPServerSpec to show correct section in error messages (claude.mcp vs codex.mcp)
- Rename examples/claude-code to examples/agent-claude - Add examples/agent-codex for OpenAI Codex CLI - Update READMEs to use simpler `moat claude` and `moat codex` commands
- Use `codex exec` for non-interactive mode (with prompt) - Use plain `codex` for interactive TUI mode - Add TTY resize support to send terminal dimensions to container - Set TERM=xterm-256color for TUI applications - Remove incorrect --full-auto flag from interactive mode
Previously, auth.json was only written for ChatGPT subscription tokens. API keys also need auth.json with the placeholder value so Codex CLI recognizes it's authenticated and routes requests through the proxy. - Write auth.json for all OpenAI credentials (API keys use api_key field) - Set needsCodexInit for any OpenAI grant, not just subscription tokens
- Fix auth.json field name to OPENAI_API_KEY (matches Codex CLI format) - Use valid-looking API key placeholder to bypass local format validation - Add StartAttached method to attach TTY before container starts (fixes "cursor position could not be read" for TUI apps) - Add chatgpt.com to network whitelist and proxy credential injection - Fix IsCodexToken to use prefix-only detection for long API keys - Fix subscription token expiration (was using Go zero time)
Test Coverage: - Add openai_test.go with token classification, expiration, and validation tests - Add session_test.go with persistence, concurrency, and edge case tests Bug Fixes: - Fix Apple container StartAttached race condition by attaching before start - Fix session file corruption with mutex locking and atomic write-rename - Fix token classification to reject empty strings and short invalid tokens - Add credential expiration check in hasCredential() UX Improvements: - Print run ID and attach command when using detached mode (-d flag) - Rename --noyolo flag to --full-auto with inverted default (true) - Increase Codex login timeout from 2 to 5 minutes Validation: - Add network host validation for --allow-host flag 🤖 Generated with [Claude Code](https://claude.com/claude-code)
📦 Previous Review (superseded by newer review)Pull Request Review: feat(agents): Add Codex supportThis PR adds comprehensive support for OpenAI's Codex CLI with credential management, session handling, and MCP server integration. Overall AssessmentStrengths:
Areas for Improvement:
Code Quality & Best Practices✅ Excellent:
|
📦 Previous Review (superseded by newer review)Pull Request Review: feat(agents): Add Codex supportThis is a solid PR that adds OpenAI Codex CLI support to moat with comprehensive credential management, session tracking, and test coverage. The implementation follows existing patterns from the Claude integration and includes several quality improvements. Here are my findings: ✅ Strengths1. Excellent Code Reuse and Refactoring
2. Comprehensive Test Coverage
3. Security Considerations
4. Good Error Messages
🐛 Potential Issues1. Race Condition in Apple Container StartAttached (Line 919-1000 in // Start attach process
attachCmd := exec.CommandContext(ctx, r.containerBin, "attach", containerID)
if err := attachCmd.Start(); err != nil { ... }
// Brief delay to let attach establish
time.Sleep(100 * time.Millisecond)
// Start the container
cmd := exec.CommandContext(ctx, r.containerBin, "start", containerID)The 100ms sleep is brittle. If the attach process takes longer to initialize (slow system, high load), the race condition still exists. Consider:
2. Orphaned Process Cleanup (Line 990-995 in if attachProcess != nil && attachProcess.Process != nil {
if err := attachProcess.Process.Kill(); err != nil {
log.Debug("failed to kill orphaned attach process", "error", err)
}
}The error is only logged. Consider:
3. Token Classification Logic ( if len(token) < 10 {
return false // Too short to be any valid OpenAI token
}This could incorrectly classify truncated or malformed tokens. Consider:
4. Missing Context Cancellation ( cmd := exec.Command("security", "find-generic-password", ...)
output, err := cmd.Output()The command doesn't use context, so it can't be canceled. Should use 5. Silent Error Handling ( if err != nil {
slog.Debug("skipping corrupted session", "id", entry.Name(), "error", err)
continue
}While logging is good, corrupted sessions indicate a problem. Consider:
|
Tests: - Add tests for internal/codex/generate.go (MCP config generation) - Add tests for cmd/moat/cli/helpers.go (path resolution, env parsing, host validation) Validation: - Add grant name validation (ValidateGrant, IsKnownProvider, KnownProviders) - Validate grants in codex_run.go before use Logging: - Log errors when skipping corrupted sessions in session.go - Log errors when killing orphaned attach process in apple.go Session ID Validation: - Stricter regex: IDs must not end with hyphen 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Pull Request Review: feat(agents): Add Codex supportThis is a well-structured PR that adds OpenAI Codex CLI support to moat, following established patterns from the Claude integration. The code quality is generally excellent with good test coverage and thoughtful architecture. Below is my detailed feedback. ✅ Strengths1. Excellent Code Reuse & Refactoring
2. Strong Test Coverage
3. Security Best Practices
4. StartAttached Innovation
5. Comprehensive Documentation
|
🔍 Issues & SuggestionsCritical Issues1. Race Condition in Apple Container StartAttached (apple.go:926-1013) The current implementation has a race condition with a 500ms sleep that's a fragile timing assumption. If the attach command takes longer to start (e.g., under load), the container may start before attach is ready, defeating the purpose of Recommendation: Use synchronization primitives instead of sleep:
2. Missing Credential Expiration Refresh Logic The code checks for expired credentials ( Recommendation:
High Priority Issues3. Inconsistent Error Handling in Session Manager (session.go:150-157) Corrupted sessions are silently skipped with only a warning log. While logged, this could hide serious problems (disk corruption, concurrent access issues). Recommendation:
4. Token Classification Edge Cases (openai.go:295-329) The Recommendation:
5. Network Validation Doesn't Catch All Invalid Hosts (helpers.go:129-203) The Recommendation:
|
Medium Priority Issues6. Grant Validation Timing (codex_run.go:71-93) Invalid grants from config file are only caught at runtime, not during config parsing. Recommendation:
7. Potential Keychain Permission Issues (openai.go:199-222) Permission denied errors or other keychain issues are silently ignored with fallback to file-based lookup. Recommendation:
8. Hardcoded Timeout Values Several timing constants lack configurability:
Recommendation:
9. Missing Context Propagation (openai.go:195-196)
Recommendation:
Low Priority / Polish10. Test Coverage Gaps Missing test coverage for:
Recommendation: Add integration/E2E tests for these scenarios. |
🎯 Performance & SecurityPerformance Considerations:
🔐 Security AssessmentOverall: Strong security model ✅ Good:
📝 Documentation & StyleAdherence to CLAUDE.md:
Code style:
🧪 Test QualityStrong test suite overall:
Suggestions:
|
📊 Summary
Recommendation: Approve with requested changes for critical issues (#1, #2). 🔧 Action ItemsBefore Merge (Critical):
Follow-up PRs (Recommended): Great work on this PR! The architecture is solid, the implementation is clean, and the test coverage is strong. The refactoring to extract shared session management is particularly well done. Once the critical race condition is addressed, this will be an excellent addition to the codebase. |
|
Remaining flagged issues are acceptable for now. |
moat codexcommand to run OpenAI Codex CLI in isolated containers with automatic credential injectionstarts
claude-codetoagent-claudeand add new agent-codex example