feat: 重写 ACP 认证流程,支持终端登录和 OAuth Device Flow#1512
feat: 重写 ACP 认证流程,支持终端登录和 OAuth Device Flow#1512zhang-xzh wants to merge 35 commits intoMoonshotAI:mainfrom
Conversation
…lize
Fixes an issue where IDE integration fails with 'list.index(x): x not in list'
when sys.argv doesn't contain 'kimi' (e.g., when started via python -m kimi_cli).
- Wrap sys.argv.index('kimi') in try/except ValueError
- Add test coverage for initialize method with various sys.argv scenarios
- Add test cases for different sys.argv scenarios in `test_argv_handling.py` - Enhance `server.py` to handle module-style invocations (e.g., `python -m kimi_cli`)
…ions - Update `server.py` to handle `__main__.py` in sys.argv[0] and construct a runnable command - Add and update test cases in `test_acp_server_auth.py` and `test_argv_handling.py` for different sys.argv scenarios - Refactor `test_argv_handling.py` to use a helper function `_simulate_argv_logic` for better readability
- Add handling for empty `sys.argv` in `server.py` and `test_argv_handling.py` - Add new test cases for empty `sys.argv` and other edge cases in `test_argv_handling.py` - Update existing test cases to reflect the new fallback behavior
…preter path - Update `server.py` to use `sys.executable` for constructing the command when `__main__.py` is in `sys.argv[0]` - Add and update test cases in `test_acp_server_auth.py` and `test_argv_handling.py` to reflect the new behavior - Handle `kimi-cli` and other edge cases in `test_argv_handling.py`
- Remove complex sys.argv parsing in `server.py` and use a simple default for terminal-auth login - Remove `test_argv_handling.py` as the simplified logic no longer requires extensive testing - Update `test_acp_server_auth.py` to focus on the core functionality of the `initialize` method
- Update `server.py` to use `sys.executable` for constructing the terminal-auth command - Update `test_acp_server_auth.py` to reflect the new command and arguments - Simplify comments in `server.py` to explain the use of `sys.executable`
- Add comprehensive test cases for ACP server authentication methods in `test_server_auth.py` - Cover terminal login, OAuth device flow, and session management scenarios - Update `server.py` to include new methods for triggering terminal login and OAuth device flow - Implement session update and cancellation functionalities
- Refactor `_check_auth` to support auto-triggering OAuth device flow - Add new tests for OAuth device flow, including success, failure, and timeout scenarios - Update `server.py` to handle temporary session IDs for auto-authentication - Enhance test coverage for `authenticate` and `check_auth` methods
…vice flow - Remove auto-triggered OAuth device flow from `_check_auth` method - Update tests in `test_acp_server_auth.py` to use async test functions - Remove related test cases for auto-authentication in `test_server_auth.py` - Update `server.py` to handle AUTH_REQUIRED errors and trigger manual authentication
- Add logic to configure models/providers after successful OAuth - Update session ID handling to use a sentinel string for temporary sessions - Refactor authentication task cancellation to handle `CancelledError` properly
- Move `_check_auth` logic to validate only for new sessions - Ensure correct model state updates after session resume - Improve error handling for cancelled and failed login attempts - Update terminal-auth methods to include additional metadata - Enhance documentation with terminal login details
…ic in terminal login handling
…neration in `_build_auth_methods_data`
- Use `asyncio.shield` to protect terminal cleanup from `CancelledError` - Forward informational messages in OAuth device flow for real sessions - Cancel existing auth tasks before starting new ones - Ensure proper cleanup and cancellation of authentication tasks in edge cases
…p logic - Introduce `_auth_verification_urls` to store verification URLs keyed by session ID. - Ensure proper cleanup of verification URLs during session cancellation or task cleanup. - Update `ext_method` to expose authentication status alongside the verification URL. - Switch session updates in `_send_auth_progress` to use `AgentThoughtChunk` for better separation of auxiliary messages.
…etter message stream separation
…h/cancel` extension method
- Replace Chinese comments with English translations for consistency. - Enhance readability and clarity in authentication flow methods, including terminal and OAuth device flow handling. - Adjust `prompt_capabilities` to set `embedded_context=True` in `AGENTS.md`.
… coverage - Ensure fallback to OAuth device flow on terminal login failure or unavailability. - Update session ID handling to prioritize `session_id` from `kwargs` when provided. - Add tests for terminal login failure fallback and `session_id` usage.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a187c9ff61
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| # Verify | ||
| assert result is not None | ||
| # Auth task should be stored under the specific session | ||
| assert "specific-session" in server_with_conn._active_auth_sessions or True # Task cleaned up after completion |
There was a problem hiding this comment.
Remove tautological assertion from session-id auth test
assert "specific-session" in server_with_conn._active_auth_sessions or True is always true, so this test never verifies that authenticate(..., session_id="specific-session") actually binds auth state to the caller-provided session. If the implementation regresses to using the wrong session key, this case will still pass and the multi-session bug will go undetected in CI.
Useful? React with 👍 / 👎.
ACP 服务器认证系统完整实现
概述
本 PR 为 Kimi Code CLI 的 ACP 服务器实现了一个完整、健壮的认证系统,支持终端登录和 OAuth Device Flow 两种认证方式。
主要变更
🔐 核心认证架构
_check_auth()AUTH_REQUIRED_trigger_login_in_terminal()_trigger_oauth_device_flow()_send_auth_progress()AgentMessageChunk发送认证进度authenticate()🖥️ 终端登录流程
关键特性:
sys.frozen)shlex.quote处理含空格路径RequestError和意外异常finally块释放终端)📱 OAuth Device Flow 备选
当客户端不支持终端或无活跃 session 时自动回退:
关键特性:
login_kimi_code异步生成器__auth__哨兵值)🔄 认证进度通知
通过 ACP 协议发送状态更新:
verification_urlwaitingcompletedfailedcancelled🛡️ 安全性改进
resume_session仅在加载新 session 时检查认证_build_auth_methods_data()确保AUTH_REQUIRED错误格式一致CancelledError转换为协议错误(from None隐藏内部细节)代码质量
重构亮点
_build_auth_methods_data()方法(原代码重复 3 次)set_session_model使用 reload + 特定字段更新模式self.sessions[session_id]文档更新
AGENTS.md:移除authenticate未实现标记,新增 Authentication 章节测试覆盖
破坏性变更
无。完全向后兼容。
修复问题
AUTH_REQUIRED错误格式Failed to initialize ACP session. Error: Internal error: "list.index(x): x not in list" #1355
Jetbrains IDE kimi不可用 #1334
resume_session仅在加载新 session 时检查认证self.sessions中的模型信息