Repository navigation
Description
Problem
PR #30 (deferred tool loading, Layer 1 of #15) and PR #34 (tool source economy) independently solve the same core concern: control which tool schemas enter the provider-visible set, so heavy schemas stay off the wire until needed. They currently ship as two parallel mechanisms — two activation paths, two catalogs, two diagnostics surfaces. That is tech debt: every future change to "which tools the model sees" has to be made twice and reconciled.
This issue proposes converging them into one mechanism, following @likun666661's principle from the group chat: 一个机制一个策略 (one mechanism, one policy).
Current state (after PR #30 merged)
PR #30 was rebased onto PR #34, so today they coexist through a thin, intentional bridge in ai-sdk-backend.ts:
ToolSourceEconomyRuntime.selectTools()picks the base tool surface (economy layer: which sources are visible).- The deferred layer then seeds the loaded-set on top of that selection —
canonicalizeToolSet(baseTools, invalid, seedLoadedNames)+buildDeferredPrepareStep. toolCountdiagnostics were unified to the active (wire-visible) subset — it now excludes the invalid fallback, deferred-unloaded, and economy-hidden schemas. This required updating three economy test assertions (see PR feat: Layer 1 deferred (on-demand) tool loading — defer heavy schemas via load_tool #30).
This bridge keeps both mechanisms green while we design the merge. It is not the end state — it is exactly the two-implementations debt this issue exists to remove.
The two mechanisms side by side
| Concern | A — deferred (PR #30) | B — economy (PR #34) |
|---|---|---|
| Opt-in | deferredCatalog present |
toolSourceEconomy.mode === 'source_economy' |
| Hidden-tool tag | exposure: 'deferred' per tool |
toolSource: { id } grouping |
| Connector | load_tool (permission-free) |
connect_tool_source (permission-free) |
| Activation timing | same-turn — prepareStep activates at step N+1 after a load at step N, + execute-boundary guard |
next-request — the enabled set only applies to a later send() |
| Persistence | durable cross-turn seed from the RuntimeEvent ledger | instance-local Set (not durable) |
| Diagnostics | active-subset recompute, honest same-turn cost | reduction fields: enabledSourceIds, availableSources, toolSchemaChangeReason |
Neutral head-to-head verdict (Codex, component-level)
- A wins: selection path, connector, activation (same-turn beats next-request), execute-boundary guard, persistence (durable ledger seed).
- B wins: diagnostics shell (richer reduction fields), catalog metadata model (source grouping with labels).
Proposed unified design: one ToolAvailabilityRuntime
One runtime owns "which tools are model-visible this step":
- One catalog model. A group has
id,toolNames,initiallyActive(bool),sourceMetadata(label, etc.). This subsumes both today's tags:exposure: 'deferred'becomes a group withinitiallyActive: false;toolSource: { id }becomes a group withinitiallyActive: true. - One connector — a single permission-free tool that activates a group on demand (merges
load_tool+connect_tool_source). - One activation policy — same-turn
prepareStep+ execute-boundary guard. next-request is dropped: it was economy's implementation limitation (no prepareStep), not a design value. - One canonicalize path —
canonicalizeToolSet(tools, invalid, activeSet). - One diagnostics — economy's reduction-field shell, fed by A's active-subset recompute.
toolCount= active (already done in PR feat: Layer 1 deferred (on-demand) tool loading — defer heavy schemas via load_tool #30). - Ledger persistence — A's durable cross-turn seed, so an activated group survives turns.
The real config dimension
Per @likun666661: timing is not the knob (it is always same-turn). The real per-group dimension is initiallyActive:
initiallyActive: true, economy-gated → "source economy" behavior (visible unless economy mode hides it; connect to re-enable).initiallyActive: false→ "deferred" behavior (hidden until loaded — heavy schemas).
One catalog, one policy, one knob.
Migration sketch
- Introduce
ToolAvailabilityRuntimewrapping the unified catalog + same-turn activation + guard + ledger seed (mostly A's engine). - Port economy's diagnostics shell (reduction fields) onto it.
- Map both
exposure: 'deferred'andtoolSource: { id }onto the unified group model. - Collapse
load_tool+connect_tool_sourceinto one connector. - Remove the coexist bridge in
ai-sdk-backend.ts(theselectTools() → deferred seedtwo-step). - Delete the second runtime.
Open questions for @likun666661
- Does this match your "一个机制一个策略" intent end-to-end?
- Connector naming: keep
load_tool, keepconnect_tool_source, or a new neutral name? - For a group that is "visible by default but economy can hide it", how should
initiallyActive+ economy-gating compose in the catalog config? - Anything in B's next-request semantics that carries value beyond the implementation limitation?
Refs: #15 (parent), PR #30 (deferred), PR #34 (tool source economy).
Activity
likun666661 commentedon Jun 17, 2026
I agree with the direction of this issue. My read is that PR #34 and PR #30 solved adjacent parts of the same problem, but at different abstraction levels and with different maturity.
The reason PR #34 introduced
toolSource/ source definitions / source-economy diagnostics was not to create a second dynamic-loader policy. It was solving the DeepSeek cost-runtime question at the source/group level:tool_schema_changedhappened;core,files.write,shell,web,browser,office, orrive, rather than forcing the model to infer individual tool names;enabledSourceIds,availableSourceIds,visibleToolNamesBySource, connector name, tool counts, and schema-char reductions are useful product/diagnostic fields;That part is still valuable and should be retained.
The part I would not preserve as final policy is PR #34's next-request activation semantics. That was a minimal first implementation of source economy before the deferred-loader work had proven the
prepareSteppath. PR #30 has the stronger runtime mechanics: same-turn activation, an execute-boundary guard, current-step active snapshots, honest active-subset diagnostics, and durable RuntimeEvent seeding.So the unified design I would aim for is:
ToolAvailabilityRuntimeas the only owner of provider-visible tool availability.id,toolNames,initiallyActive, and optional metadata such aslabel/description.exposure: 'deferred'=> a group withinitiallyActive: false;toolSource: { id, label, description }/sourceDefinitions=> source/group metadata in the same catalog.load_toolas the canonical name because it matches the same-turn semantics better.connect_tool_sourceshould be accepted for historical RuntimeEvent seeding/backcompat, but not remain a separate provider-visible connector.prepareStepplus execute-boundary guard. I would not makesame-turn vs next-requesta configuration knob; next-request was an implementation limitation, not a product value.toolCount, schema chars, prefix/request-shape hashes,enabledSourceIds,availableSourceIds, and visible tool names should all describe the same provider-visible active set.load_toolandconnect_tool_sourcecalls so existing sessions do not regress.A good migration boundary would be to make
AiSdkBackendstop doing the current two-step bridge:and instead ask one runtime for:
The important invariant is "one mechanism, one policy": tool availability should have one catalog, one connector, one active snapshot, one diagnostics source, and one place where prefix/tool-schema changes are classified.
Astro-Han commentedon Jun 17, 2026
Thanks for the detailed response. We agree on direction. Here's where I want to push further.
Drop
initiallyActive, use a global switchThe issue proposes a per-group
initiallyActiveboolean. I think we can go simpler: one global economy toggle (on by default) + a configurable core set.files.writetools belong in core;browser/officedon't.Why no per-group flag: it will interact with other dimensions (permission modes, deployment contexts), and each interaction is a new edge case. A global switch with a tunable core boundary covers the same ground without the combinatorial blowup. Want a group always visible? Put its tools in core. Otherwise economy hides it.
This lines up with your framing: the real config boundary is group-level availability (core always visible, heavy groups hidden until needed), not per-group activation policy.
Grouping: only for natural clusters
Groups pull their weight for product semantics (capability inventory, cost attribution). But only when tools actually cluster together. Rules:
Long-term, ToolSearch-style semantic lookup replaces "load this group" with "find me the tool for X." Groups as product semantics (capability inventory, diagnostic attribution) stick around. Keep the group count small and membership obvious, and the "where does this tool go?" problem doesn't come up.
Connector:
load_toolsRename
load_tool→load_tools(plural):connect_tool_sourcestays as a historical alias for RuntimeEvent seeding. Not exposed as a separate provider-visible connector.Open questions
load_tools.initiallyActive+ economy-gating composition: Gone. No per-group flag, just "in core or not."prepareStep+ execute-boundary guard is the only policy.Catalog model
No
initiallyActiveon groups. Visibility is set by the globaleconomyflag: on → hidden (unless incoreToolNames); off → visible.Migration
ToolAvailabilityRuntimewith this config + PR feat: Layer 1 deferred (on-demand) tool loading — defer heavy schemas via load_tool #30's engine (same-turn activation, guard, ledger seed).load_toolandconnect_tool_sourcehistorical events for ledger seeding.load_tools.ai-sdk-backend.ts.ToolSourceEconomyRuntime.If this looks right, I'll start building.
Astro-Han commentedon Jun 17, 2026
已按本 issue 收敛的方案实现并提交:PR #41 (
Closes #37,合并后自动关闭本 issue)。落地要点(一个机制、一个策略):
ToolAvailabilityRuntime取代tool-source-economy与deferred-activation/load-tool(后三者已删除);economy开关 + 可配coreToolNames,分组来自ToolAvailabilityConfig.groups,移除每工具的exposure(feat: Layer 1 deferred (on-demand) tool loading — defer heavy schemas via load_tool #30)与toolSource(Add Tool Source Economy v1 #34)标签;load_tools,历史load_tool/connect_tool_source仅作账本回填别名、不再对模型暴露;prepareStep+ 执行边界 gating),丢弃"下一请求才生效"的旧语义。验证:typecheck 全绿;core 624/624、storage 82/82、runtime 535/536、desktop 1491/1491(runtime 唯一未过的是既有的
network/proxy-test环境用例,在main上同样失败,本次未触及)。refactor(runtime): unify deferred loading and tool-source economy int…
refactor(runtime): unify deferred loading and tool-source economy int…
refactor(runtime): unify deferred loading and tool-source economy int…
refactor(runtime): unify deferred loading and tool-source economy int…
refactor(runtime): unify deferred loading and tool-source economy int…
refactor(runtime): unify deferred loading and tool-source economy int…
refactor(runtime): unify deferred loading and tool-source economy int…