Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
import { test, expect, describe, beforeEach } from "bun:test";
import {
usageLimitNotice, usageResetAt, noteUsageLimit, usageGateRemainingMs, noteUsageRetry,
resetUsageGate, isInfraError, isAuthError,
USAGE_GATE_FALLBACK_MS, USAGE_GATE_MAX_MS, MAX_USAGE_RETRIES,
} from "../src/run_outcome";
import { securityUsageLimitMessage } from "@bbbug/shared/security_agent_failure";
/**
* 生产实测(2026-10-27 04:13 PDT):执行账号撞了 session 额度上限,CLI 用一个干净的
* success 帧回了一句「You've hit your session limit · resets 5:40am (America/Los_Angeles)」。
* isInfraError 把它当成「正常结束、只是没写 STATUS」,5 分钟里 14 张 bug 被标成 fix_failed,
* 其中一张已经修完、提交、真机验证过,只差写 FixReport。下面这两帧就是那次会话的原样尾巴。
*/
const SYNTHETIC = JSON.stringify({
type: "assistant",
message: { model: "<synthetic>", role: "assistant", content: [{ type: "text", text: "You've hit your session limit · resets 5:40am (America/Los_Angeles)" }] },
});
const RESULT = JSON.stringify({
type: "result", subtype: "success", is_error: false, num_turns: 1,
result: "You've hit your session limit · resets 5:40am (America/Los_Angeles)",
});
const PROD_TAIL = [JSON.stringify({ type: "system", subtype: "init" }), SYNTHETIC, RESULT].join("\n");
const resultFrame = (text: string) => JSON.stringify({ type: "result", subtype: "success", is_error: false, result: text });
describe("额度用尽的识别", () => {
test("生产那次的尾巴:认得出,而且 infra / auth 两条老路都认不出 —— 这就是漏判的原因", () => {
expect(usageLimitNotice(PROD_TAIL)).toContain("hit your session limit");
expect(isInfraError(PROD_TAIL)).toBe(false);
expect(isAuthError(PROD_TAIL)).toBe(false);
});
test("老措辞和 usage limit 措辞也认", () => {
expect(usageLimitNotice(resultFrame("You've hit your limit · resets at 5pm"))).not.toBeNull();
expect(usageLimitNotice(resultFrame("You’ve hit your weekly usage limit · resets Oct 30, 5am"))).not.toBeNull();
expect(usageLimitNotice(resultFrame("Claude AI usage limit reached|1793102400"))).not.toBeNull();
});
test("agent 自己的正文里讨论额度,不算", () => {
// 锚在开头:一张讲「用户撞了额度上限」的 bug,agent 的总结不能把节点停掉。
expect(usageLimitNotice(resultFrame("修复了弹窗:用户 You've hit your limit 文案溢出"))).toBeNull();
const agentProse = JSON.stringify({
type: "assistant",
message: { model: "claude-opus-5-5", content: [{ type: "text", text: "You've hit your limit — that's the toast we need to fix." }] },
});
expect(usageLimitNotice(agentProse)).toBeNull();
});
test("安全线共用的识别规则也认得新措辞", () => {
expect(securityUsageLimitMessage("claude", "You've hit your session limit · resets 5:40am (America/Los_Angeles)"))
.toContain("session limit");
});
});
describe("重置时间", () => {
// 2026-10-27 11:13:51Z = 04:13:51 PDT,就是生产撞限的那一刻。
const AT = Date.parse("2026-10-27T11:13:51Z");
test("带时区:算到那个时区的下一个 5:40", () => {
expect(usageResetAt("You've hit your session limit · resets 5:40am (America/Los_Angeles)", AT))
.toBe(Date.parse("2026-10-27T12:40:00Z"));
});
test("时间已经过了今天的点,就是明天", () => {
expect(usageResetAt("resets 3am (America/Los_Angeles)", AT)).toBe(Date.parse("2026-10-28T10:00:00Z"));
});
test("pm / 12 点的换算", () => {
expect(usageResetAt("resets 12pm (UTC)", AT)).toBe(Date.parse("2026-10-27T12:00:00Z"));
expect(usageResetAt("resets 12am (UTC)", AT)).toBe(Date.parse("2026-10-28T00:00:00Z"));
});
test("读不出时间就返回 null,别编", () => {
expect(usageResetAt("You've hit your limit", AT)).toBeNull();
expect(usageResetAt("Claude AI usage limit reached|1793102400", AT)).toBeNull();
});
});
describe("节点闸", () => {
beforeEach(() => resetUsageGate());
test("闸拉到重置时刻之后,而不是固定几分钟", () => {
const now = Date.now();
const until = noteUsageLimit("You've hit your session limit · resets 5:40am (America/Los_Angeles)", now);
const reset = usageResetAt("resets 5:40am (America/Los_Angeles)", now)!;
expect(until).toBeGreaterThan(reset);
expect(until - reset).toBeLessThanOrEqual(5 * 60_000);
expect(usageGateRemainingMs()).toBeGreaterThan(0);
});
test("读不出时间按兜底冷却", () => {
const now = Date.now();
expect(noteUsageLimit("You've hit your limit", now)).toBe(now + USAGE_GATE_FALLBACK_MS);
});
test("并行的几路同时撞限,闸只延长不缩短", () => {
const now = Date.now();
const long = noteUsageLimit("You've hit your limit", now);
expect(noteUsageLimit("You've hit your limit", now - 60_000)).toBe(long);
});
test("再离谱的重置时间也封顶", () => {
const now = Date.now();
expect(noteUsageLimit("resets 11:59pm (Pacific/Kiritimati)", now) - now).toBeLessThanOrEqual(USAGE_GATE_MAX_MS);
});
test("按 item 的重排有上限,最终还是交给人", () => {
for (let i = 1; i <= MAX_USAGE_RETRIES; i++) expect(noteUsageRetry("x").retry).toBe(true);
expect(noteUsageRetry("x").retry).toBe(false);
});
});