Repository navigation
fix: 上游没正常走完 finish 时不再谎报成功(max_output_tokens / pause_turn / *-error / 无 finish 事件) - #39
Merged
Merged
Conversation
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.
修复 #38。
68664c2修了stop_reason的一个成员('tool-calls'连字符),方向完全正确 ——但同一族里还有四个成员没处理,后果都比它更严重:上游明明截断了,下游收到的是「正常结束」。
现状
max_output_tokens/model_context_window_exceededend_turncompletedpause_turnfinish_reason:"pause_turn"(非法)end_turncompletednetwork-error等end_turncompletedfinish事件[DONE],连 finish_reason 块都没有stopReason || 'end_turn'无条件兜底completedCLI 侧的完整语义
改法
mapFinishReason全量归一化:length家族(含max_output_tokens/model_context_window_exceeded)→length;network|connection|upstream[-_ ]error→upstream_error。未知值原样返回,不再静默折成stop。mapAnthropicStopReason增加pause_turn/refusal原样透出:pause_turn是Anthropic 的合法枚举,表示「这一轮被暂停,后面还有」,CLI 是靠自动续写把它吸收掉的;
代理不自动续写,就必须如实上报。
toOpenAIFinishReason:pause_turn→length(OpenAI 没有对应枚举,折成
length至少有「输出不完整」的含义,折成stop就是谎报完成)。incompleteUpstreamDetail(sawFinish, finishReason)与incompleteUpstreamError(),三条协议共用一套判定。
sawFinish跟踪:没走完 finish 时,非流式报 502 可重试,流式发 error 事件而不是补一个假的结束标志(
message_stop/[DONE])。finishReason === 'length',改为用归一化后的值 ——否则
max_output_tokens会漏判成completed。finish时,「no finish event」才是根因,按 429 报会把真正的原因盖掉。
关于
sawFinish的口径判定的是「上游给过任何完成信号」:终态
finish,以及本代理一直在处理的finish-step。(
finish-step并不在 CLI 的事件集里 ——dist/cli.mjs中 0 处出现 —— 但既然代理认它,就不能让它变成「没完成」,否则会把原本正常的响应误判成 502。真正要拦的是
「一个完成信号都没有就断了」。)
没有做自动续写
CLI 遇到
pause_turn会重发同一个请求体并拼接 content / usage,最多 6 次。本 PR 没有照做,理由是:续写语义依赖服务端行为(重发是否幂等、返回的是续接内容还是从头再来),
在没有真实 key 的情况下无法验证;而「如实上报
pause_turn」不会丢内容 ——下游(尤其是 Anthropic 原生 SDK)本来就知道该怎么续。
如果确实要做续写,那应该是另一个 PR,并且需要先确认服务端语义。
测试
新增
test/stream-end.test.mjs:三种协议 × 四种情形 = 12 条,外加 3 条回归(正常
stop仍照常完成、tool-calls仍报tool_use/tool_calls、max_output_tokens在 Responses 上报
incomplete),共 15 条。只带这一组测试所需的脚手架:
test/helpers.mjs与package.json的test脚本。helpers.mjs与 PR #34 中的文件逐字节一致,两边先后合并都不会冲突(git 对「两侧新增同一份相同内容」不视为冲突),所以本 PR 不依赖 #34。
复验