Skip to content

refactor(server): extract protocol preparation + tool-injection zone into src/server/{prepare-*,inject} modules - #2368

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-08_server-prepare-extract
Oct 8, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-08_server-prepare-extract

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner

#1440 P2 cut 2: extract the protocol-preparation + tool-injection zone out of src/server.ts into five new modules. Pure move — zero renames, zero signature changes, zero logic edits.

What moves where

New module Content (original server.ts lines @55af975e)
src/server/prepare-anthropic.ts prepareAnthropic + auto-mode classifier pair (AUTO_MODE_CLASSIFIER_STOPS/isAutoModeClassifier, stay non-exported — sole caller was in-zone)
src/server/prepare-openai.ts prepareOpenai
src/server/prepare-google.ts appendGoogleNudge (internal), prepareGoogle, prepareGoogleCountTokens
src/server/prepare-responses.ts prepareResponses, prepareResponsesCompact, codex-upstream/lite/prompt-cache-key helpers, effectiveAbsorbBlock (now exported; caller at former line 7259 imports it back)
src/server/inject.ts injectSystem, private generic core mergeOwnedTools + the four flat wrappers, FORCE_TEXT_PROTOCOL

Stays in src/server.ts: isCountTokensRequest + the prepareCountTokens dispatcher (it calls the prepares → imports the new modules, no cycle), both scrub* helpers, and everything else. Net: monolith -1458 lines (7707 → 6249 @ current master); 8 kept-helper defs gain export; 3 now-dead import specifiers pruned from acp-kernel/compress-settings/config.

Cycle handling

New modules take type-only imports from ../server.ts (Prepared) and runtime values only from kernel/knobs/sibling modules… except the 8 helpers that were monolith-internal and are now cross-module: they became exported function declarations (ESM-hoisted ⇒ the module cycle cannot touch an uninitialized binding during circular init). Precedent: src/server/admin.ts from cut 1 already does this. No top-level const in any new module initializes from a ../server.js binding.

Proof it is a pure move

  • Byte-level multiset identity: every deleted line of server.ts equals the verbatim moved zones (+ documented export prefixes at exactly 13 def sites); independently re-verified against the rebased base with a whole-tree multiset comparison — zero residual delta modulo blanks/import bookkeeping/export prefixes.
  • Only normalizations: 3 junction blank lines collapsed, EOF newlines on 3 files, knob imports keep their original X as knobX aliases.

Rebase note

Built against 55af975e, rebased onto current master 2495ad25: upstream #2332/#2302 (one-call payload) had amended exactly the four nudge-injection sites inside the moved zones. Their two-line change (const renderedWithPayload = rendered.text;) is ported byte-for-byte into the four new modules — no behavior delta versus master, just relocated together with their functions.

Verification (all local, post-rebuild of kernel for the rebase)

  • npm run check:unused-exports clean · tsc --noEmit (incl. tests/) clean · full build (kernel + tsup + annotations) OK
  • Focused suites run individually: wire-contract ×2 (fresh 分析缓存泄露问题又一个日志 #2302 goldens), one-call-payload, anthropic/openai PCK trio, nudge suites (issue1470 / plugin-nudge-injection / issue1204-rules-google-plugin), codex-official (consumes the server.js re-export that keeps its brace-import intact), count-tokens, responses-websocket (38), preflight-compress-responses, preflight-responses-reasoning-extract, fix-920-bypass-drop, prompt-pack-tool-prompts, e2e-responses-offhead-system, e2e-image-billing (5/5), e2e-codex-fake (env-gated skip, unchanged) — all pass
  • Worker-run broader sweep (pre-rebase): 85 suite files, 519 pass / 0 fail / 5 platform-or-env-gated skips
  • reference/architecture.md gains one line per new module (docs-sync gate)

No behavior change intended; any golden/wire delta would have shown in the suites above.

Why this PR also touches tests/side-lane-invariants.test.ts (lane-guard)

The pure move carries three comment lines containing the phrase "side request" out of src/server.ts; the lane guard's keyword scan over that file's diff fires on those deletions, and the guard has no exemption for comment relocation. Rather than rewording the comments to dodge the keyword or loosening the guard, I pinned a real invariant the guard exists to protect: test (F) asserts the five extracted modules never couple into the #388 side-lane engine (no import of side-request.js, no gate symbols) — locking the module boundary this refactor creates. All four side-lane suites pass unchanged.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-08_server-prepare-extract (a233b6e)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2553-pr.2368.c93ed52

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2368.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei
ranxianglei force-pushed the 2026-10-08_server-prepare-extract branch from 6961308 to f379e29 Compare October 8, 2026 00:51
…into src/server/{prepare-*,inject} modules (#1440 P2 cut 2)
@ranxianglei
ranxianglei force-pushed the 2026-10-08_server-prepare-extract branch from f379e29 to a233b6e Compare October 8, 2026 01:23
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

上游冲突已解决,分支已 rebase 到最新 master 并 force-push,PR #2368 现读 mergeable=true。按七问:

  1. 是什么问题:系统转发的「上游 PR 冲突」——本分支(audit: 全仓冗余审计与重构计划——server.ts 解体、客户端矩阵去重、死代码、AGENTS.md 模块图腐化 #1440 P2 cut 2,把 protocol-prep + tool-injection 区从 src/server.ts 抽到 5 个新模块)原先建在旧基线 2495ad25,而 master 已前进 31+ 提交、其中大量落在被移动的同一批函数里,rebase 产生文本冲突。

  2. 什么原因导致的:纯移动重构与高频改动的固有时序碰撞——master 持续修改的正是被搬走的函数(nudge 门控 fix: gate emergency nudge override on submit-gate feasibility #2365、one-call payload 分析缓存泄露问题又一个日志 #2302/feat(#2302): digestion batching guard — defer small folds on huge raw history #2332、responses/dsh 门控 bili 插件模式下 allowDshCompaction 在 responses 线路上完全失效 —— DSH 原生压缩落地、折叠基底被摧毁、compress 连片失败 #2360/fix(#2360): dsh compaction gate on responses wire + coherent compress-failure receipts #2364),与被移动区间重叠 ⇒ 冲突。不是代码缺陷。

  3. 怎么修复的:rebase 到当前 master。真正冲突只有一处(prepare* 函数区),以 master 版本为准,并把 master 的单行改动移植进对应的新模块文件(如 fix: gate emergency nudge override on submit-gate feasibility #2365 让四处 nudge 注入点的 emergencyNudge(...) 增补两个参数)。master 随后又前进一版,其唯一触及 server.ts 的改动(bili 插件模式下 allowDshCompaction 在 responses 线路上完全失效 —— DSH 原生压缩落地、折叠基底被摧毁、compress 连片失败 #2360 注释)与我分支已有内容一致,第二次 rebase 零冲突干净落位。全程用整树逐行 diff-vs-master 归类 + multiset 比对,确认没有丢失任何 master 内容。

  4. 是否有回归问题:无行为变更(纯移动)。最终 diff-vs-master 的每一行都归类为已记录的纯移动产物:仅 import 重写(删掉不再使用的符号)、import-back / re-export 记账、给跨模块引用的保留 helper 加 export。typecheck / build / check:unused-exports 全绿;定向套件 133 pass / 0 fail(含最新 wire-contract golden、anthropic/openai/google nudge 三件套、responses-websocket、dsh-compaction-guard、issue2360-receipt)。未本地跑全量回归(按纪律交给 CI)。

  5. 是否破坏缓存:不涉及——未触碰任何缓存 / 重试 / 增量游标 / 去重键逻辑;函数搬家不改运行时语义,prefix-cache 锚点(system block 字节稳定)保持不变。

  6. 是否改了设计方向:不涉及——这正是计划内的抽取(audit: 全仓冗余审计与重构计划——server.ts 解体、客户端矩阵去重、死代码、AGENTS.md 模块图腐化 #1440 P2 cut 2),rebase 只是把它重新落到最新基线,未引入新方向。

  7. 是否引入了新的配置:不涉及——无新增 env / config / 默认值。

补充:master 本会话期间连续前进三次(871fd6911 → 3234e5aba → 56214577a),我最终基于 3234e5aba 落位并 force-push;相对最新的 56214577a 无文件重叠,GitHub API 现读 mergeable=true / merge_state=behind(behind 非阻塞)。判定:可合并,等待人工 merge。

@ranxianglei
ranxianglei merged commit 1716212 into master Oct 8, 2026
27 checks passed
@ranxianglei ranxianglei mentioned this pull request Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.132-master.1716212 (rolling master tag).

npm install -g billion-context@0.0.132-master.1716212  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant