fix(v2): register tools with AI-SDK shape + idempotent system prompt - #18
Merged
Conversation
setupV2 registered the four bili_* tools by spreading @bili/core's V1
ToolDef ({description, args(zod), execute(args, ToolContext)}) straight into
tools.add(). opencode V2 needs {name, description, input(JSON Schema),
execute(input,ctx)->{content}} — so the V2 tools had no name, a zod object
instead of a JSON Schema, and an unmapped execute ctx. Separately
SYSTEM_MARKER was 'BILI CONTEXT MANAGEMENT' while SYSTEM_PROMPT begins with
'ACP TOOLS (billion-context)', so the upsert never matched and a duplicate
system prompt was appended on every dispatch.
- new src/v2-tools.ts: V1 ToolDef -> V2 shape; input derived via
z.toJSONSchema(z.object(tool.args)) (zod 4.4.3) so the schema cannot drift.
- index.ts: SYSTEM_MARKER corrected + exported; setupV2 uses the V2 wrappers.
- new tests/v2-tools.test.ts: shape, schema projection, ctx mapping, marker.
Ports the fixes from PR #8 (rorshopping) into the dual-shape entry per
AGENTS.md §2.3 (single package); supersedes #4 and #8. V1 path unchanged.
Verified: typecheck, 54/54 tests, build (677 KB), smoke.
This was referenced Aug 13, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Ports the two real V2 bug fixes from #8 (by @rorshopping) into the existing dual-shape entry — instead of merging #8's separate
packages/v2/second package, which contradicts AGENTS.md §2.3 (single dual-shape package is load-bearing). Supersedes #4 and #8.The bugs (both confirmed present on
master)setupV2didtools.add({ ...makeCompressTool(runtime), options }), spreading the V1ToolDef{ description, args(zod), execute(args, ToolContext) }. opencode V2'stools.add()needs the AI-SDK shape{ name, description, input(JSON Schema), execute(input, ctx) → { content } }. So under V2 the fourbili_*tools had noname, a zod object instead of a JSON Schema, and an unmapped execute ctx (V1ToolContextrequiresdirectory/worktree, which V2 doesn't provide). The V2 tools were effectively non-functional.SYSTEM_MARKER = "BILI CONTEXT MANAGEMENT"butSYSTEM_PROMPTbegins withACP TOOLS (billion-context)(packages/core/src/system-prompt.ts:14).findIndexalways returned-1, so a duplicate system prompt was appended on every dispatch.The fix
src/v2-tools.ts—toV2Tool(name, tool)wraps a V1ToolDefinto V2 shape. TheinputJSON Schema is derived viaz.toJSONSchema(z.object(tool.args))(zod 4.4.3, already bundled) — so the V2 schema cannot drift from the V1 definitions (this is better than fix(v2): register tools with the opencode2 API shape #8's hand-written schemas, which can).executemaps the V2 ctx onto the V1ToolContext(callID ← ctx.id, emptydirectory/worktree) and unwraps the V1 result into{ content }.src/index.ts—SYSTEM_MARKERcorrected to"ACP TOOLS (billion-context)"andexported;setupV2'stools.addblock uses the four V2 wrappers. The V1 path and the dual-shape export are untouched.tests/v2-tools.test.ts— 4 regression tests: tool shape, schema projection (compresscontent[].{startId,endId,summary},required:["content"]), execute ctx mapping +{ content }unwrap, andSYSTEM_PROMPT.includes(SYSTEM_MARKER).Why not merge #8 as-is
#8 implements these fixes in a second package
packages/v2/(billion-context-opencode-v2). AGENTS.md §2.3 is explicit: the project deliberately ships one package whose default export isObject.assign(biliAcpPluginV1, { id, setup })— callable for V1 and carrying{ id, setup }for V2. "Do not refactor the dual-shape export into two separate entries."masterremovedpackages/v2/specifically to adopt dual-shape, which is also why #8 is DIRTY (its base predates the removal). The correct home for these fixes is the existingsetupV2.Verification
npm run typecheck(both workspaces)npm testnpm run builddist/index.js677.51 KB (zero new runtime deps;z.toJSONSchemais in the already-bundled zod)node smoke.mjsscripts/ci/check-pr.shScope / follow-ups
@bili/core, the dual-shape export, and the bundle's dependency surface are unchanged.'compress'in its call↔result pairing rules, leaking consumedbili_compressinvocations — needs a kernel-side fix.