一本书是怎么被写出来的 书架

第 05 章 / 共 10 章

第五章 · 换腿判据

换不换腿,整个系统只看一个函数的返回值:shouldTryNextLeg(error)。输入是上一条腿的错误文本,输出是一个布尔——true = 这条腿废了,换下一条;false = 书本身写坏了,认输。全部判据是三个正则数组,钉在 book-legs.ts 里。

这段代码在哪

抽成独立文件、不 import server.ts 的原因写在文件头:「判别与顺序都抽在这里,是为了能脱开 server.ts(import 即起服务)单测」。

它做什么

判定顺序是先否定、后肯定——先看「明确不是配额」,再看模型被拒,最后看配额/凭据:

export function shouldTryNextLeg(error: string | undefined | null): boolean {
  const s = String(error ?? "");
  if (!s) return false;
  if (NOT_QUOTA_PATTERNS.some((re) => re.test(s))) return false;
  if (MODEL_REJECTED_PATTERNS.some((re) => re.test(s))) return true;
  return QUOTA_PATTERNS.some((re) => re.test(s));
}

三类「该换腿」的模式:

类别代表模式对应的真实错误
① 配额满/usage limit/i、/quota/i、/rate[ _-]?limit/i、/\b429\b/、/api error: 403/i、/overloaded/iKimi「You've reached your 5-hour usage limit」;Kimi 兼容端点把配额耗尽报成 403
② 凭据失效/not logged in/i、/\b401\b/、/invalid.{0,12}api[ _-]?key/i、/credit balance is too low/i、/refresh[ _-]?token/i、/token.{0,24}(revoked\|invalidated)/i、/session has ended/icodex「Not logged in」;refresh token 被吊销
③ 模型被账号拒/model.{0,40}does not exist/i、/do not have access to it/i、/model.{0,40}is not supported/i、/model[ _-]?not[ _-]?found/i404「The model `gpt-5.5` does not exist…」;400「'gpt-5.6' model is not supported…」

「不该换腿」的否定模式只有两个,但位置最高:

const NOT_QUOTA_PATTERNS: RegExp[] = [
  /error_max_turns/i,
  /high risk/i,
];

error_max_turns 是书写太长撞轮数上限,high risk 是 Kimi 内容风控——两者换腿重跑必然重蹈覆辙。注释:「这些词出现时,即便文本里混进了上面某个词也不换腿」。单测里就钉了一个混合串:"error_max_turns (quota fine)" 必须判 false。

为什么这样写

怎么排查

一本书是怎么被写出来的 · 王建硕
费曼式写法 · 由多个 AI 代理撰写与互相审校