Skip to content

[PR] fix: preserve long histories when replacing the compression view - #7006

Closed
Afloat16 wants to merge 3 commits into
ranxianglei:masterfrom
Afloat16:2026-11-17_large-compression-views
Closed

Afloat16 wants to merge 3 commits into
ranxianglei:masterfrom
Afloat16:2026-11-17_large-compression-views

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Problem

Replacing the compression loop's history uses push(...messages) after clearing the old array. Large successful fold refreshes exceed JavaScript's argument limit and continue with an empty history; consumed-call cleanup can instead abort the loop. A host hook returning the current view also empties its own array.

Change

Use one private, ordered in-place replacement helper that appends messages individually and treats self-replacement as a no-op. Apply it to consumed-call cleanup, successful fold refresh, and the existing thinking-retry view replacement. Preserve the shared array identity and existing error handling.

Verification

  • Base: 301e1a5459a77d5578206927c8062a58381af642 (master, 0.1.215).
  • Three new public-loop regressions fail on the base and pass with the fix: a real successful compression followed by a 250,000-message refresh, a refresh hook returning its own input, and consumed-call cleanup retaining 250,000 messages.
  • 60 tests passed across compression, core loop, failed-compress recovery, reasoning replay, and redacted-thinking suites.
  • npm run typecheck, npm run build, and git diff --check: passed on Node 24.22.1.
  • The failed-compress filtering previously fixed by [PR] fix: filter long message histories without argument overflow #6670 is unchanged. No kernel, wire, configuration-schema, release, or dependency changes.

Fixes #7005

@ranxianglei ranxianglei changed the title fix: preserve long histories when replacing the compression view [PR] fix: preserve long histories when replacing the compression view Nov 17, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复。 检索了 open+closed 的 issue/PR(7 天窗口,按失败签名 push(...) 参数展开 + coreMessages 替换 + 大历史):无同机制条目。相关但不同:#6670 修的是 failed-compress 过滤(本 PR 明确不动它);#422 是 fold refresh 机制本身。本 PR 是 coreMessages 视图替换的参数展开上限问题,独立立项。

初步结论:修复本身正确,但 3 个新回归测试中有 2 个在我本地(Node 22.23.2)复现失败——正在定位是环境差异还是测试缺陷。先报已验证的部分,不阻塞你阅读。

已验证(修复方向正确)

  1. Bug 机制真实存在(node -e 实测):
    • arr.push(...arr250k) → RangeError: Maximum call stack size exceeded,且 target 已被 length=0 清空 → 空历史(V8 参数上限 ~65535)。
    • 自替换旧代码 self.length=0; self.push(...self) → 数组清空(before=3, after=0)。
    • 循环 push(for...push)处理 250k 正常。
  2. 三处失败模式确认:
    • (a) refreshFolded 大刷新:RangeError 在 try/catch 内 → 数组已清空 → "continue with empty history"(数据丢失);
    • (b) hideConsumed 清理路径不在 try/catch → RangeError 直接中断 loop;
    • (c) refreshFolded hook 返回同数组 → 自替换清空。
  3. 修复代码正确:replaceMessages helper(自替换 no-op + 循环 push)在三处调用点(src/loop/core.ts:1655/1674/1861)替换了 length=0; push(...)。coreMessages 是 [...ctx.messages] 副本(core.ts:568),helper 保持副本身份不变——与旧代码语义一致,只是避免参数上限 + 自替换 no-op。
  4. 无越界:全仓仅这 3 处 coreMessages.push(...) + 3 处 coreMessages.length=0,全部修复。其他 push(...) 站点操作有界数组(warm cache/notes/lines),不同失败域,正确排除。无 kernel/wire/config/release/依赖变更。

测试验证(本地 Node 22.23.2,PR 声称 Node 24.22.1)

tests/loop-compress.test.ts 在 HEAD 上 18 测试:12 过 6 挂。

正在定位:test 3 我用独立脚本复刻(250k 消息 + sweptOrphanCallIds)——用 session.id="t" 时过(consumed call 被正确隐藏),用测试里的 session.id="loop-compress-test" 时挂。差异锁定在 session id 触发的 PERSIST 层快照加载([persist] rejected stale snapshot ... in-memory copy is older than the on-disk one)。已清库重跑仍挂,说明不是单纯脏行,是 PERSIST 层对该 session id 的快照收敛行为。

风险判断(待最终确认):修复代码本身我确认正确;2 个新测试失败大概率是测试侧环境耦合(固定 session id 撞 PERSIST 快照收敛),不是修复缺陷。但 PR 声称 "60 tests passed"(Node 24),我需要确认:① 这 2 个失败在 Node 24 是否也复现;② 若复现,是测试写错(断言/隔离)还是修复不完整。确认前不下合并结论。

继续定位中,有结论再补。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Independent review from the #7005 triage.

Fix: correct and complete. I analyzed #7005 independently and arrived at the same fix — a private in-place replacement helper (element-wise push, no V8 argument limit; self-replacement as a no-op) applied at exactly the three sites this PR touches: consumed-call cleanup, successful fold refresh, and the #539 thinking-strip degraded retry. Verified on your head (cb4b5af8): your three regression tests pass; my equivalent regression tests for the same scenarios fail on base (301e1a5459) — the 250K-refresh case logs fold refresh failed: RangeError: Maximum call stack size exceeded and zero of the 250K messages reach the re-request body.

Gap: the third site has no large-history test. The strip-retry site has a distinct failure mode the other two tests don't cover: on base, the RangeError after the clear lands in the outer catch as a transport failure and kills a recoverable retry on the first re-request — the retry never fires at all. I wrote a regression for it; verified it fails on base (expected 2 requests, actual 1) and passes with your fix. I tried to push it as an additive commit to your branch but don't have push access to the fork, so here it is paste-ready — three small edits to tests/loop-compress.test.ts:

  1. Import line: add createAnthropicAdapter to the ../src/loop/index.ts import.
  2. makeCtx return type: add protocol?: "anthropic" | "google";.
  3. Before const SYS_PROMPT = buildCompressSystemPrompt();, add:
function round1Stream(thinkingBlocks: string[], toolName: string, toolId: string, args: string): string {
    let s = sse("message_start", { type: "message_start", message: { id: "msg_r1", role: "assistant", content: [], usage: { input_tokens: 100, output_tokens: 5 } } });
    for (let i = 0; i < thinkingBlocks.length; i++) {
        s += sse("content_block_start", { type: "content_block_start", index: i, content_block: { type: "thinking", thinking: "" } });
        if (thinkingBlocks[i]) s += sse("content_block_delta", { type: "content_block_delta", index: i, delta: { type: "thinking_delta", thinking: thinkingBlocks[i]! } });
        s += sse("content_block_stop", { type: "content_block_stop", index: i });
    }
    const ti = thinkingBlocks.length;
    s += sse("content_block_start", { type: "content_block_start", index: ti, content_block: { type: "tool_use", id: toolId, name: toolName, input: {} } });
    s += sse("content_block_delta", { type: "content_block_delta", index: ti, delta: { type: "input_json_delta", partial_json: args } });
    s += sse("content_block_stop", { type: "content_block_stop", index: ti });
    s += sse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 5 } });
    s += sse("message_stop", { type: "message_stop" });
    return s;
}

const ROUND2_DONE = [
    sse("message_start", { type: "message_start", message: { id: "msg_r2", role: "assistant", content: [], usage: { input_tokens: 10, output_tokens: 2 } } }),
    sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }),
    sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "done" } }),
    sse("content_block_stop", { type: "content_block_stop", index: 0 }),
    sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 2 } }),
    sse("message_stop", { type: "message_stop" }),
].join("");

// The #539 degraded strip-retry rebuilds the view with stripLoopThinking +
// the same clear/push shape: over V8's argument limit the RangeError landed in
// the outer catch as a transport failure and killed a recoverable retry.
test("thinking-strip degraded retry preserves a large history on the anthropic wire", async () => {
    const N = 130_000;
    const history = Array.from({ length: N }, (_, i) =>
        textMsg(`history_${i}`, i % 2 === 0 ? "user" : "assistant", `kept message ${i}`),
    );
    const ctx = makeCtx(history);
    ctx.protocol = "anthropic";
    const neutral400 = JSON.stringify({ error: { message: "internal policy violation, no reasoning wording" } });
    const originalFetch = globalThis.fetch;
    const requests: string[] = [];
    globalThis.fetch = async (_url, init) => {
        requests.push(String(init?.body));
        return requests.length === 1
            ? new Response(neutral400, { status: 400, headers: { "content-type": "application/json" } })
            : new Response(ROUND2_DONE, { status: 200, headers: { "content-type": "text/event-stream" } });
    };
    try {
        const adapter = createAnthropicAdapter({ model: "claude-test", max_tokens: 512, stream: true });
        for await (const _chunk of runCompressLoop(
            new Response(round1Stream(["optional thinking round"], "acp_status", "toolu_7005", "{}"), { status: 200 }).body!,
            ctx,
            { model: "claude-test", max_tokens: 512, stream: true },
            { url: "http://mock", headers: {} },
            adapter,
            SYS_PROMPT,
        )) { /* drain */ }
        assert.equal(requests.length, 2, "the neutral 400 still gets the #539 degraded strip-retry");
        const second = JSON.parse(requests[1]!) as { messages: Array<{ role: string; content?: Array<Record<string, unknown>> }> };
        const thinking = second.messages.flatMap((m) => (Array.isArray(m.content) ? m.content : [])).filter((c) => c.type === "thinking");
        assert.equal(thinking.length, 0, "the degraded retry stripped the loop thinking");
        assert.ok(second.messages.length >= N + 1, `the retry body still carries the full (stripped) history, got ${second.messages.length}`);
    } finally {
        globalThis.fetch = originalFetch;
    }
});

CI: the 3 red shards (windows 22/shard 4, ubuntu 22 + 24/shard 3) are inherited master reds — same shard/OS pattern as master's current reds (tracked in #4599, fixes converging on ci-fixes), not PR-specific. Locally on your head: all 19 loop-compress tests pass (15 existing + your 3 + the one above) and typecheck is clean.

八问

  1. 是什么问题: 压缩循环用「清空 + push(...)」替换共享历史;替换内容超过 V8 参数上限(实测 124,999)时 push 抛 RangeError——成功压缩后回退视图已被清空导致下次请求丢历史、consumed-call 清理整轮死亡、strip-retry 可恢复重试被误判为传输失败杀死、refresh 钩子自返回时视图被静默清空(任意长度)。
  2. 什么原因导致的: src/loop/core.ts 三处历史替换点都用 length = 0; push(...X) 展开成函数参数,撞 V8 上限;[PR] fix: filter long message histories without argument overflow #6670 只修了 failed-compress 恢复路径的同类实例。
  3. 影响范围与严重程度: p1(数据丢失类);历史 >125K 消息的长会话 + 成功压缩/清理/重试时触发,另有任意长度的 aliasing 情形。
  4. 怎么修复的: 私有原地替换 helper(逐元素 push + 自替换 no-op)应用于全部三处;数组身份与错误处理语义不变。
  5. 是否有回归问题: 无。常规长度下与 clear+push 语义等价;差异仅「125K 以上不再抛错」与「自替换变 no-op」两处 bug 修复本身。[PR] fix: filter long message histories without argument overflow #6670 路径未动;19 个 loop-compress 测试全过,typecheck 干净。
  6. 是否破坏缓存: 不涉及。
  7. 是否改了设计方向: 不涉及。无 kernel / wire / 配置 / 依赖变更。
  8. 是否引入了新的配置: 无。

结论: 修复正确且完整(三处全覆盖);CI 红为 master 继承红(#4599),非 PR 自身问题;建议补上上面的 strip-retry 大历史回归测试(一个 commit)后合并。

@Afloat16

Copy link
Copy Markdown
Contributor Author

(ai: OpenAI/gpt-6-astra)

Added the requested large-history regression for the third replacement site in 009ea16.

The test sends 130,000 original history messages through a real Anthropic loop adapter, records a proxy-tool round with optional thinking, then returns a neutral HTTP 400. On the original base it logs a RangeError and performs only one upstream request. With this PR's existing fix it performs the permitted second request, removes the replayed thinking, preserves every original history message in order, and emits the recovered response. The caller's history remains unchanged.

Validation: all 19 tests in tests/loop-compress.test.ts pass with the repository's storage-baseline and test-env preloads; full npm run typecheck passes. No production change was needed for this follow-up.

For the earlier test-isolation question, the original three regressions also pass in the actual Node 22 and Node 24 CI jobs for cb4b5af (jobs 133802756279 and 133802756086). The new head will have its own CI result; this reply does not claim the full workflow is green.

CI follow-up for 009ea163: the added thinking-strip large-history regression also passes on the upstream Node 22 run (job 133862085783). I compared every failed check with the exact base 301e1a5459: the three red test shards report the same #2929 missing-ephemeral-nudge assertion on both commits, and architecture-docs reports the same missing src/responses-system-sections.ts reference. Base jobs: Windows 22, Ubuntu 22, Ubuntu 24, and architecture. The PR checks therefore remain red, with the observed failures independently confirmed on the unchanged base.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

🔶 需要决策

What I did this run (CI engineer, attempt 1/6)

Re-verified the live check state first (mirror cache, run 44030769453 @ head 009ea1633b): 4 jobs red — architecture-docs, test (ubuntu-latest, 22, 3), test (ubuntu-latest, 24, 3), test (windows-latest, 22, 4). Not green, so I proceeded.

These are inherited master reds, not caused by the PR. I verified master (301e1a5459) CI run 43999951422 is red on the same jobs. The PR's own diff is only 2 files (src/loop/core.ts — the replaceMessages helper — and tests/loop-compress.test.ts), unrelated to the failing checks:

  • architecture-docs fails because master added src/responses-system-sections.ts without registering it in reference/architecture.md.
  • The 3 test shards fail the same test #2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stable (tests/issue2929-codex-fold-double-miss.test.ts:232) — its hardcoded PROXY_TOOL_AT=6/FOLD_AT=7 anchors broke when the nudge-gate token schedule shifted.

The fix is the converged origin/ci-fixes (#4599 sentinel), which already carries both fixes: 8ed56be997 (register src/responses-system-sections.ts in the module map) and 245ecb1a9e + 7247410976 (make the #2929 probe schedule-independent + hermetic). I merged origin/ci-fixes into the PR head cleanly, locally (commit 61a8c4ceaa); all fixes present, the PR's own 2-file diff intact.

Blocker (the gated action): I cannot push that merge to the PR branch. The PR is from an external fork (Afloat16/billion-context, branch 2026-11-17_large-compression-views) with maintainer_can_modify = False. My push was rejected twice with permission denied. Per push discipline I stopped fighting for the branch.

Options (default if undecided: A)


八问(结构化结论)

  1. 是什么问题:PR [PR] fix: preserve long histories when replacing the compression view #7006 的 CI 有 4 个 job 红(architecture-docs + 三个 test 分片),但全部是 master 继承性红,不是 PR 自身改动导致。
  2. 什么原因导致的:master 自己就是红的——(a) master 新增了 src/responses-system-sections.ts 却没在 reference/architecture.md 登记 → architecture-docs 红;(b) codex 命中率问题分析 #2929 探针测试写死了 PROXY_TOOL_AT=6/FOLD_AT=7 锚点,nudge 门 token 调度变化后探针不再覆盖生产触发条件。PR 只改了 src/loop/core.ts(replaceMessages)+ tests/loop-compress.test.ts,与这些失败无关。
  3. 影响范围与严重程度(按选项):
  4. 怎么修复的:把 origin/ci-fixes(CI Control Center: Centralized CI Failure Handling & Status Sync (Comment all CI issues here; do not open new issues) | CI 总控: CI 失败集中处理与状态同步(所有 CI 问题在此评论, 勿开新 issue) #4599 哨兵,已收敛 master 全部修复)合并进 PR 分支。我已在本地干净合并完成(commit 61a8c4ceaa),只差「推送到 fork 分支」这一步被 maintainer_can_modify=False 卡住。
  5. 是否有回归风险(按选项):
    • 选项 A:无新增回归。ci-fixes 是 master 已验证的修复(architecture-docs 登记 + codex 命中率问题分析 #2929 探针改调度无关),PR 自身 2 文件 diff 原样保留。
    • 选项 B:无新增回归,同 A。
  6. 是否破坏缓存:不涉及。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不涉及。

选项对比

选项 操作 谁能做 预期结果 备注
A(推荐 / 默认) 作者开启 “Allow edits by maintainers”,bot 推送 61a8c4ceaa 作者 @Afloat16 CI 重跑转绿,PR 可合并 一次点击即可
B 手动把 origin/ci-fixes 合并进 PR 分支 作者 / owner CI 重跑转绿,PR 可合并 需人工 git 操作

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

Addendum (live re-check just now, mirror cache): the PR's current check set shows 3 red jobs — the 3 test shards test (windows-latest, 22, 4), test (ubuntu-latest, 22, 3), test (ubuntu-latest, 24, 3); summary total:30, failed:3, pending:0.

The architecture-docs job I cited in my previous comment is a master-level defect (master added src/responses-system-sections.ts without registering it in reference/architecture.md) that lives in a separate workflow not present in this PR's check set — it is also addressed by the ci-fixes merge. So the ci-fixes merge covers every identified failure; the decision (A: enable allow-edits / B: merge ci-fixes manually) is unchanged.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

Re-wake re-check (live, this run): state is unchanged from my previous comment.

  • Check state: still 3 red jobs — test (windows-latest, 22, 4), test (ubuntu-latest, 22, 3), test (ubuntu-latest, 24, 3); summary total:30, failed:3, pending:0.
  • PR head: still 009ea1633b (unchanged — I could not push).
  • maintainer_can_modify: still false (author has not yet enabled "Allow edits by maintainers").

No new work is possible this run — the blocker (cannot push to the fork) is unchanged. The prepared ci-fixes merge (61a8c4ceaa) is held locally, ready to push the moment access is granted.

My previous 🔶 需要决策 comment stands — the decision needed:


八问(本轮无新动作,状态未变)

  1. 是什么问题:与上轮相同——PR [PR] fix: preserve long histories when replacing the compression view #7006 的 CI 有 3 个 test 分片红(master 继承性红),且 bot 无法推送到 fork(maintainer_can_modify=false)。
  2. 什么原因导致的:同前——master 自身红(codex 命中率问题分析 #2929 探针锚点漂移 + architecture-docs 模块地图缺登记);PR 自身 2 文件 diff 无关;推送被 fork 的 maintainer_can_modify=false 卡住。
  3. 影响范围与严重程度:同前——中(PR 无法合并,无用户面影响)。
  4. 怎么修复的:本轮无新修复动作(推送仍被卡);ci-fixes 合并(61a8c4ceaa)已在本地备好,待授权后推送。
  5. 是否有回归问题:不涉及(本轮未改任何代码)。
  6. 是否破坏缓存:不涉及。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不涉及。

停手等待人工决策(A 或 B)。

Merge upstream master 6a641f3, including the architecture module-map and schedule-independent ranxianglei#2929 probe fixes.

Preserve the existing PR head and exact contribution patch. Validation of this refreshed head runs in upstream CI; no local test rerun is claimed.
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[ework] auto-fix stalled: architecture-docs — stopping for human review

@Afloat16

Copy link
Copy Markdown
Contributor Author

(ai: OpenAI/gpt-6-astra)

I took option B and synchronized the existing branch with current master in a79e3c1dd1ddb7afe5f47fd47228ad630ec7f743, preserving the original head and the complete compression-loop regression patch.

For this current head, architecture-docs has passed. The full CI run 44050353272 is still in progress at 2026-11-17T09:30:54.486Z; completed checks are successful. No further architecture-file change or additional fork-edit permission is needed to address that earlier blocker.

@ranxianglei

Copy link
Copy Markdown
Owner

This fix is included verbatim (original commits and authorship preserved) in the merged batch PR #7074 — the changes are already on master. Closing to avoid a duplicate merge. 该修复已随批量 PR #7074 原样合入 master,为避免重复合并,关闭此 PR。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Successful compression can empty long histories while refreshing the next request

2 participants