fix(parser): 修复 Gemini init 事件 session_id 未提取的问题#111
Merged
cexll merged 1 commit intoJan 8, 2026
Conversation
Gemini CLI 的 session_id 出现在 init 事件中,但 parser 的 isGemini 判定条件只检查 role/delta/status 字段,导致 init 事件被当作 "Unknown event" 忽略,session_id 无法提取。 修复方案:在 isGemini 条件中增加对 init 事件的识别。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Author
|
这个问题,是昨天遇到windows 下 codex后端无法识别 完成 并退出 时 一块发现的,用 cc +dev +codeagent(cc+codex)修的,然后单独让codex review 了 一遍,不懂具体代码,尽力了只能说 |
Contributor
Author
Auto generated by Codex总体:LGTM。这个修复是必要且合理的:Gemini stream-json 的 session_id 出现在 init 事件里,而旧的 isGemini 只看 role/delta/status,导致 init 被当 Unknown 丢掉。 实现点评:
建议/潜在改进:
安全/泄露检查(粗检):
格式/断行:
测试说明(环境有限):
|
cexll
approved these changes
Jan 8, 2026
Collaborator
cexll
left a comment
There was a problem hiding this comment.
代码审查通过 ✅
审查结论
修复方案准确,直接命中根因,代码正确性、安全性、性能均无问题。批准合并。
关键发现
- ✅ 修复逻辑正确:将
init事件纳入isGemini判定,使session_id能在 parser.go:257 正确提取 - ✅ 影响面窄:仅改变之前被忽略的特定事件归类
- ✅ 对其他后端无影响:Codex/Claude 分支在前且条件互斥
- ✅ 性能影响几乎为零
- ✅ CI 测试通过
改进建议(非阻塞)
建议后续补充测试用例,覆盖"session_id 只在 init 出现、后续事件不含 session_id"的场景:
func TestBackendParseJSONStream_GeminiInitOnlySessionID(t *testing.T) {
input := `{"type":"init","session_id":"gemini-xyz"}
{"type":"message","content":"Hello","role":"assistant"}
{"type":"result","status":"completed"}`
_, threadID := parseJSONStream(strings.NewReader(input))
if threadID != "gemini-xyz" {
t.Fatalf("threadID=%q, want %q", threadID, "gemini-xyz")
}
}技术债务提醒
isGemini 条件持续变长,未来可考虑将 threadID 提取逻辑从后端判定中解耦。
审查工具:codeagent (codex backend)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
使用
codeagent-wrapper --backend gemini调用 Gemini CLI 后,未输出SESSION_ID,导致无法使用 resume 功能延续会话。说明(环境有限):
仅本人Windows11使用遇到此问题,且修复完成后只在本机Windows环境测试通过
根因分析
Gemini CLI 的
session_id出现在init事件中:{"type":"init","timestamp":"...","session_id":"0e05a575-4e72-4315-b3a2-15a57457226d","model":"auto-gemini-3"}但
parser.go:174的isGemini判定条件只检查role/delta/status字段:导致
init事件被当作 "Unknown event" 忽略(parser.go:282),session_id无法提取。Claude 后端正常是因为其
session_id出现在type=="result"事件中,有专门的处理逻辑。修复方案
在
isGemini条件中增加对init事件的识别:改动文件
codeagent-wrapper/parser.go:174- 修复 isGemini 判定条件codeagent-wrapper/main_test.go- 新增TestBackendParseJSONStream_GeminiInitEventSessionID单测手动测试记录
Gemini 后端(修复目标)
Codex 后端(回归测试)
Claude 后端(回归测试)
单元测试
$ go test -v ./codeagent-wrapper/... -run TestBackendParseJSONStream_Gemini === RUN TestBackendParseJSONStream_GeminiEvents --- PASS: TestBackendParseJSONStream_GeminiEvents (0.00s) === RUN TestBackendParseJSONStream_GeminiInitEventSessionID --- PASS: TestBackendParseJSONStream_GeminiInitEventSessionID (0.00s) === RUN TestBackendParseJSONStream_GeminiEvents_DeltaFalseStillDetected --- PASS: TestBackendParseJSONStream_GeminiEvents_DeltaFalseStillDetected (0.00s) === RUN TestBackendParseJSONStream_GeminiEvents_OnMessageTriggeredOnStatus --- PASS: TestBackendParseJSONStream_GeminiEvents_OnMessageTriggeredOnStatus (0.00s) PASS🤖 Generated with Claude Code