Skip to content

refactor(server): unify injectTool/injectOpenaiTool/injectResponsesTool over one owned-name merge core (#1440 P3) - #2307

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-07_inject-tool-merge
Oct 7, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-07_inject-tool-merge

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner

Issue #1440 P3 — collapse the three flat-shape tool-injection copies in src/server.ts (injectTool / injectOpenaiTool / injectResponsesTool) into one shared private core. Behavior-preserving: zero call-site changes, signatures unchanged, wire bytes unchanged.

What

New module-private generic:

function mergeOwnedTools<T>(tools: readonly T[] | undefined, acp: readonly T[], extras: readonly T[], nameOf: (t: T) => unknown): T[]

owns the algorithm once: wanted = acp ∪ extras; drop input entries whose name hits wanted; return [...kept, ...acp, ...extras]. The three wrappers become single-expression delegators pinned by explicit type args (mergeOwnedTools<unknown> / mergeOwnedTools<OpenAITool>) with nameOf callbacks that are the exact accessors the originals used ((t as { name?: string })?.name / t?.function?.name) — no any, no inference risk.

injectGoogleTool deliberately untouched — different algorithm, not just a different accessor: Gemini nests declarations one level deeper (tools[].functionDeclarations[]), collects presence across every entry, appends missing declarations as ONE new entry, and returns the original array reference when nothing is missing (identity is load-bearing downstream). The core structurally cannot express both the ownership-dropping path Google must avoid and that identity behavior.

Equivalence argument (case-by-case, per wrapper)

  1. Non-array input → [...acp, ...extras] in both (responses: [...acp] ≙ old [...base]). Building owned first is pure — no side effects ⇒ same result either order.
  2. Owned-set contents identical: the extra typeof n === "string" guard only filters non-string acp/extras names, which the real BILI_ACP_TOOLS_* constants never produce (applyAcpToolOverrides passes them through by reference); and a non-string-named input entry is kept regardless of set membership in BOTH versions.
  3. Filter predicate byte-identical expressions on the same accessor paths.
  4. Output order/contents identical: kept order preserved by filter, append order acp→extras preserved.
  5. Return types pinned to the originals' declared returns via explicit T; call sites are internal — no widening possible.

Net diff: src/server.ts +22/−27, lines ~5114–5181 only; zero caller lines appear in the diff (verified at :3887 anthropic, :4137 openai, :4699 responses). No exports added/removed (core is module-private → the #1443 unused-export gate unaffected).

Verification

  • npx tsc --noEmit clean · tsup build OK
  • Focused suites individually (node --import tsx --test): wire-contract 15 ✓, wire-contract-golden 5 ✓, e2e-compress-prompts 3 ✓, prompt-pack-tool-prompts 4 ✓, rules-wire 6 ✓, web-ptools-position 2 ✓, fix-1897-leaked-tools-side 11 ✓, runtime-info 29 ✓, proxy-noinject-tag-strip 10 ✓, preflight-compress-responses 1 ✓, e2e-anthropic 5 ✓ — 87 pass / 0 fail
  • Full-suite note: the offline sandbox's dsh-native-lane network tests fail identically WITH and WITHOUT this change (baseline proven via stash) — CI (networked) is the authoritative full gate

Rebase note (master moved past v0.1.187: #2167 external-summary executor et al.)

Rebased onto 95ad8d25 and resolved two small conflicts inside the inject zone: master's #2167 added an externalOn parameter + withExternalSummaryTools() wrapping to injectTool/injectOpenaiTool/injectResponsesTool (and Google). The wrappers keep master's signatures/wrapping verbatim and the delegations stay one-liners on top of mergeOwnedTools. No behavioral delta introduced by the resolution — same algorithm, same order, master's new args flow straight through to the core.

Second commit: one-line gate fix (PiSubagentChildSignal demoted) — master's own unused-exports job has been red since #2268 landed (verified on tip); this repo's established pattern for exactly this situation is a mechanical demote commit (cf. 371b15b5). Kept separate so it can be cherry-picked/dropped independently. After it runs, the merge ref passes the gate clean (locally verified: ✓ unused-export gate: clean (3 waived)).

Re: patrol fix 81272616 ("drop stray export codexRewriteInsertionIndex") that landed on the branch meanwhile: subsumed — that symbol is already demoted on master itself (371b15b5); the flag only appeared because the branch still stood on the pre-release base at patrol time. Dropped from the branch by this force-push, effect retained via master.

Re-verified after rebase: gate exit 0 · tsc clean · tsup OK · wire-contract 15/15 · pi-subagent-channel 28/28 incl. · web-ptools-position 2/2 · external-summary{,-runtime,-batch} 45/45 · lane-guard keywords absent from diff.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-07_inject-tool-merge (6e71ed5)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2478-pr.2307.824d049

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-pr2307.tgz
npm install -g package

Install instructions are refreshed on each push.

ranxianglei pushed a commit that referenced this pull request Oct 7, 2026
…dex (#1443 residue)

In-passing gate fix during #2307 review: the unused-export check fails on
master itself (INTERNAL_ONLY src/launcher.ts: codexRewriteInsertionIndex),
blocking every PR's unused-exports job. The function is used only inside
launcher.ts (:980); the export keyword is dead surface left by the #1443
cleanup pass. One-word removal; no behavior, wire, or cache impact.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成:等价论证逐条成立,本地验证全绿;唯一红检查是 master 既有缺陷,已按「小问题直接修」政策顺手修掉(独立标注 commit)。CI 在新 head 上重跑中。

核对过的范围:diff 仅 src/server.ts +22/−27(hunks ~5114–5189),caller 行零改动(:3887 anthropic、:4137 openai、:4699/:4700 responses);injectGoogleTool 字节级未动(无 hunk);CCR schema 选择(ccrOn ? …ANTHROPIC : …NO_RANGE,#1712/#1179 注释)留在各 wrapper;#920 rationale 收敛到核心一处,responses wrapper 留一行指针;无 export 增删(grep 证实 mergeOwnedTools 恰好 1 定义 + 3 调用点)。本地:npm run typecheck 干净;11 个定向套件(wire-contract/golden、e2e-compress-prompts、prompt-pack-tool-prompts、rules-wire、web-ptools-position、fix-1897-leaked-tools-side、runtime-info、proxy-noinject-tag-strip、preflight-compress-responses、e2e-anthropic)91 pass / 0 fail(issue 写 87,是套件此后又长了 4 条测试,非漂移)。

结构化七问

  1. 是什么问题:audit: 全仓冗余审计与重构计划——server.ts 解体、客户端矩阵去重、死代码、AGENTS.md 模块图腐化 #1440 P3——三种 wire shape 的压缩工具注入各复制了一份同一套「bili 拥有压缩工具名 → 丢同名输入项 → 追加 acp/extras」合并算法,本次把它收敛为一个模块私有泛型核心,行为保持。
  2. 什么原因导致的:历史演进性重复——三种 wire 先后落地时每次照抄同一逻辑、只换 name accessor;是 DRY 债务,不是缺陷。
  3. 怎么修复的:单一 mergeOwnedTools<T>(tools, acp, extras, nameOf) 拥有算法一次;三个 wrapper 变成单表达式委托器,用显式类型参数钉住返回类型(mergeOwnedTools<unknown> / <OpenAITool>),nameOf 就是原来的精确访问器,无 any。Google 路径刻意不动:它算法不同(nested functionDeclarations、跨 entry 收集存在性、缺失时追加单 entry、无缺失时返回原数组引用——identity 下游承重),这个核心结构上表达不了那条 ownership-dropping + identity 双重语义。
  4. 是否有回归问题:等价论证五点逐一验证成立——①非数组输入两边都退化为 [...acp, ...extras](responses [...base] ≡ [...base, ...[]]),先建 owned 集合是纯操作无副作用;②owned 集合内容相同:applyAcpToolOverrides(kernel/src/surface-config.ts)对未覆盖工具按引用透传,真实 BILI_ACP_TOOLS_* 常量不可能产生非字符串名,多出的 typeof n === "string" guard 实际空转;且非字符串名的输入项在两版里都被保留;③filter 谓词表达式逐字节相同、同访问器路径;④输出顺序/内容相同(filter 保 kept 序,acp→extras 追加倍序);⑤显式 T 钉死返回类型,调用点全在模块内无 widening 可能。审过的既有行为:三条注入路径、CCR 选择、opencode v1: keep opencode-acp legacy sessions working after switching to bili #920 所有权语义、Google identity 语义——全部保持。唯一红检查 unused-exports 与本 PR 无关:master HEAD 本地复现完全相同的一条 INTERNAL_ONLY src/launcher.ts: codexRewriteInsertionIndex(refactor: drop dead declarations + demote internal-only exports + unused-export gate (#1440) #1443 清理 pass 漏掉的死 export,该函数只在 launcher.ts:980 内部使用)。已顺手修:删一个单词 export,独立 commit 81272616f(标注清楚,不混入本 PR 的 refactor 语义)。
  5. 是否破坏缓存:不涉及——纯函数重构,wire 字节不变,prefix cache/持久化/重试策略均不受影响。
  6. 是否改了设计方向:不涉及——核心为模块私有,无新 API surface、无架构转向。
  7. 是否引入了新的配置:不涉及。

判定:CI 全绿后可合(人工合并)。gate 修复 commit 已推,CI 正在 81272616f 上重跑,结果出来我再跟一条确认。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 跟进确认:新 head 81272616f(含 gate 修复 commit)上 27/27 checks 全绿——包括此前红的 unused-exports 和完整 test matrix(ubuntu/windows)。无其他回归信号。

判定维持:可合(人工合并,PR #2307)。本 PR 至此交付完整:refactor 本体(行为保持,等价论证+定向套件验证)+ 独立标注的 master 既有 gate 缺陷顺手修。

@ranxianglei
ranxianglei force-pushed the 2026-10-07_inject-tool-merge branch from 8127261 to 6e71ed5 Compare October 7, 2026 12:54
@ranxianglei
ranxianglei merged commit 608e611 into master Oct 7, 2026
27 checks passed
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.110-master.608e611 (rolling master tag).

npm install -g billion-context@0.0.110-master.608e611  # 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