mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(core): make Code Mode namespace updates unambiguous
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
export * as CodeModeCatalog from "./catalog.js"
|
||||
|
||||
import { toolExpression } from "@opencode/codemode"
|
||||
import { Schema } from "effect"
|
||||
|
||||
export type Tool = {
|
||||
@@ -84,7 +85,8 @@ export function summarize(inventory: Inventory, options: Options = {}): Summary
|
||||
sorted
|
||||
.filter((entry) => !entry.tool.pinned)
|
||||
.map((entry) => {
|
||||
const short = entry.path.slice(name.length + 1)
|
||||
// A tool at the namespace's own path, with no namespace or with child tools, has no shorter name.
|
||||
const short = entry.path === name ? toolExpression(entry.path) : entry.path.slice(name.length + 1)
|
||||
return entry.tool.parameters ? `${short}(${entry.tool.parameters.join(", ")})` : short
|
||||
}),
|
||||
budget,
|
||||
@@ -96,15 +98,10 @@ export function summarize(inventory: Inventory, options: Options = {}): Summary
|
||||
|
||||
/** The leading names whose cost fits `budget`. */
|
||||
function fit(names: ReadonlyArray<string>, budget: number) {
|
||||
const shown: string[] = []
|
||||
let remaining = budget
|
||||
for (const name of names) {
|
||||
const price = cost(name.length + 2)
|
||||
if (price > remaining) break
|
||||
shown.push(name)
|
||||
remaining -= price
|
||||
}
|
||||
return shown
|
||||
const totals: number[] = []
|
||||
for (const name of names) totals.push((totals.at(-1) ?? 0) + cost(name.length + 2))
|
||||
const end = totals.findIndex((total) => total > budget)
|
||||
return end === -1 ? names : names.slice(0, end)
|
||||
}
|
||||
|
||||
function suffix(tool: Tool) {
|
||||
@@ -115,7 +112,9 @@ function suffix(tool: Tool) {
|
||||
}
|
||||
|
||||
function compare(left: string, right: string) {
|
||||
return left < right ? -1 : left > right ? 1 : 0
|
||||
if (left < right) return -1
|
||||
if (left > right) return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
function flatten(entries: ReadonlyArray<Tool | Namespace>, path: ReadonlyArray<string> = []) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export * as CodeModeInstructions from "./instructions.js"
|
||||
|
||||
import { searchSignature } from "@opencode/codemode"
|
||||
import { searchSignature, toolExpression } from "@opencode/codemode"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { Instructions } from "../instructions/index.js"
|
||||
import { CodeModeCatalog } from "./catalog.js"
|
||||
@@ -13,7 +13,7 @@ const prompt = `# Code Mode
|
||||
|
||||
The tools listed below, and \`search\`, cannot be called directly: they only work as JavaScript inside the \`execute\` tool. Every other tool you have is a normal tool call: call it directly, never inside \`execute\`.
|
||||
|
||||
Each namespace lists its tools by name with their required parameters, and \`…\` when a tool also takes optional ones; pinned tools show their full signature. Before calling a tool whose signature is not shown, get it with the global \`search(...)\` function, for example \`search({ query: "tools.ns.tool_name" })\`, and return the result; one \`execute\` can search for several tools. A namespace that lists fewer tools than it has finds the rest the same way. Do not guess tool names or arguments. Each result's \`path\` is the complete call expression: write it out as code, for example \`await tools.ns.tool_name({ ... })\`. Do not use it as a key such as \`tools[path]\`.
|
||||
Each namespace lists its tools by name with their required parameters, and \`…\` when a tool also takes optional ones; a tool listed without parentheses takes input the list cannot summarize. Pinned tools show their full signature. Before calling a tool whose signature is not shown, get it with the global \`search(...)\` function, for example \`search({ query: "tools.ns.tool_name" })\`, and return the result; one \`execute\` can search for several tools. For a namespace that lists fewer tools than it has, find the rest the same way. Do not guess tool names or arguments. Each result's \`path\` is the complete call expression: write it out as code, for example \`await tools.ns.tool_name({ ... })\`, and call a name that is not a JavaScript identifier with brackets, as in \`tools.ns["tool-name"]({ ... })\`. Do not use a path as a key such as \`tools[path]\`.
|
||||
|
||||
- ${searchSignature}
|
||||
|
||||
@@ -40,12 +40,14 @@ ${render(current)}`
|
||||
(before, after) => CodeModeCatalog.renderSection(before) !== CodeModeCatalog.renderSection(after),
|
||||
)
|
||||
const parts = [
|
||||
...block("New tool namespaces are available:", sections.added),
|
||||
...(sections.added.length === 0
|
||||
? []
|
||||
: [["New tool namespaces are available:", ...sections.added.map(CodeModeCatalog.renderSection)].join("\n")]),
|
||||
...(sections.changed.length === 0
|
||||
? []
|
||||
: [
|
||||
[
|
||||
"These namespaces changed; a namespace shown with its whole listing replaces the previous one:",
|
||||
'These namespaces changed. Lines marked "new or changed", "no longer" or "unchanged" are changes to a namespace\'s previous listing; a namespace shown without them is its whole new listing:',
|
||||
...sections.changed.map((change) => changes(change.previous, change.current)),
|
||||
].join("\n"),
|
||||
]),
|
||||
@@ -62,8 +64,9 @@ ${render(current)}`
|
||||
return delta.length < replacement.length ? delta : replacement
|
||||
}
|
||||
|
||||
// One namespace's change: the tools added, changed or gone under its new header, or its whole listing when that is
|
||||
// shorter. A namespace too large to list in full can drop a name without losing the tool, so it says "listed".
|
||||
// One namespace's change under its new header: marked lines for the tools new, changed or gone, or its whole listing
|
||||
// when that is shorter. A namespace too large to list in full can drop a name without losing the tool, so it says
|
||||
// "listed" there; a tool that only moved between pinned and listed by name is not gone.
|
||||
function changes(previous: CodeModeCatalog.Section, current: CodeModeCatalog.Section) {
|
||||
const names = Instructions.diffByKey(previous.names, current.names, toolName, (before, after) => before !== after)
|
||||
const pinned = Instructions.diffByKey(
|
||||
@@ -72,17 +75,24 @@ function changes(previous: CodeModeCatalog.Section, current: CodeModeCatalog.Sec
|
||||
(entry) => entry.path,
|
||||
(before, after) => before.line !== after.line,
|
||||
)
|
||||
const gone = [
|
||||
...pinned.removed.map((entry) => entry.path.slice(current.name.length + 1)),
|
||||
...names.removed.map(toolName),
|
||||
]
|
||||
const capped = current.pinned.length + current.names.length < current.count
|
||||
const present = new Set([
|
||||
...current.names.map(toolName),
|
||||
...current.pinned.map((entry) => pinnedName(current, entry)),
|
||||
])
|
||||
const gone = [...pinned.removed.map((entry) => pinnedName(previous, entry)), ...names.removed.map(toolName)].filter(
|
||||
(name) => !present.has(name),
|
||||
)
|
||||
const signatures = [...pinned.added, ...pinned.changed.map((change) => change.current)]
|
||||
const listed = [...names.added, ...names.changed.map((change) => change.current)]
|
||||
const delta = [
|
||||
CodeModeCatalog.namespaceLine(current),
|
||||
...[...pinned.added, ...pinned.changed.map((change) => change.current)].map((entry) => entry.line),
|
||||
const capped = current.pinned.length + current.names.length < current.count
|
||||
const marked = [
|
||||
...(signatures.length === 0 ? [] : [" - new or changed signatures:", ...signatures.map((entry) => entry.line)]),
|
||||
...(listed.length === 0 ? [] : [` - new or changed: ${listed.join(", ")}`]),
|
||||
...(gone.length === 0 ? [] : [` - no longer ${capped ? "listed" : "available"}: ${gone.join(", ")}`]),
|
||||
]
|
||||
const delta = [
|
||||
CodeModeCatalog.namespaceLine(current),
|
||||
...(marked.length === 0 ? [" - listed tools unchanged"] : marked),
|
||||
].join("\n")
|
||||
const whole = CodeModeCatalog.renderSection(current)
|
||||
return delta.length < whole.length ? delta : whole
|
||||
@@ -93,8 +103,9 @@ function toolName(listing: string) {
|
||||
return open === -1 ? listing : listing.slice(0, open)
|
||||
}
|
||||
|
||||
function block(heading: string, sections: ReadonlyArray<CodeModeCatalog.Section>) {
|
||||
return sections.length === 0 ? [] : [[heading, ...sections.map(CodeModeCatalog.renderSection)].join("\n")]
|
||||
// The name a pinned tool would have if it were listed by name, so a tool moving between the two is recognized.
|
||||
function pinnedName(section: CodeModeCatalog.Section, entry: { readonly path: string }) {
|
||||
return entry.path === section.name ? toolExpression(entry.path) : entry.path.slice(section.name.length + 1)
|
||||
}
|
||||
|
||||
const key = Instructions.Key.make("core/codemode")
|
||||
|
||||
@@ -252,7 +252,13 @@ type CatalogNode = Node<CodeModeCatalog.Tool>
|
||||
// The parts of a tool's JSON Schema input that name its parameters. An Effect schema with an identifier encodes
|
||||
// as a `$ref` into its own definitions.
|
||||
const InputShape = Schema.Struct({
|
||||
type: Schema.optionalKey(Schema.Unknown),
|
||||
properties: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)),
|
||||
additionalProperties: Schema.optionalKey(Schema.Unknown),
|
||||
patternProperties: Schema.optionalKey(Schema.Unknown),
|
||||
anyOf: Schema.optionalKey(Schema.Unknown),
|
||||
oneOf: Schema.optionalKey(Schema.Unknown),
|
||||
allOf: Schema.optionalKey(Schema.Unknown),
|
||||
required: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
$ref: Schema.optionalKey(Schema.String),
|
||||
$defs: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)),
|
||||
@@ -260,7 +266,8 @@ const InputShape = Schema.Struct({
|
||||
})
|
||||
const decodeInputShape = Schema.decodeUnknownOption(InputShape)
|
||||
|
||||
// Required parameter names, then `…` when the tool also takes optional ones; undefined for an unreadable schema.
|
||||
// Required parameter names, then `…` when the tool also takes optional ones. Undefined unless the input is a plain
|
||||
// object, such as a union or a record, whose parameters a list of names would misstate; it is then listed by name.
|
||||
function parameters(schema: unknown) {
|
||||
const shape = Option.getOrUndefined(decodeInputShape(schema))
|
||||
if (shape === undefined) return undefined
|
||||
@@ -268,6 +275,9 @@ function parameters(schema: unknown) {
|
||||
const referenced = name === undefined ? undefined : (shape.$defs?.[name] ?? shape.definitions?.[name])
|
||||
const root = referenced === undefined ? shape : Option.getOrUndefined(decodeInputShape(referenced))
|
||||
if (root === undefined) return undefined
|
||||
const composed = [root.anyOf, root.oneOf, root.allOf, root.patternProperties].some((value) => value !== undefined)
|
||||
const open = root.additionalProperties !== undefined && root.additionalProperties !== false
|
||||
if (composed || (root.properties === undefined && (root.type !== "object" || open))) return undefined
|
||||
const names = Object.keys(root.properties ?? {})
|
||||
const required = names.filter((key) => root.required?.includes(key))
|
||||
return required.length < names.length ? [...required, "…"] : required
|
||||
|
||||
@@ -3,6 +3,9 @@ import { searchSignature } from "@opencode/codemode"
|
||||
import { CodeModeCatalog } from "@opencode/core/codemode/catalog"
|
||||
import { CodeModeInstructions } from "@opencode/core/codemode/instructions"
|
||||
|
||||
const CHANGED =
|
||||
'These namespaces changed. Lines marked "new or changed", "no longer" or "unchanged" are changes to a namespace\'s previous listing; a namespace shown without them is its whole new listing:'
|
||||
|
||||
const entry = (
|
||||
path: string,
|
||||
description: string,
|
||||
@@ -21,6 +24,9 @@ const tools = (namespace: string, count: number) =>
|
||||
entry(`${namespace}.tool${index}`, `${namespace} ${index}`, { parameters: ["id"] }),
|
||||
)
|
||||
|
||||
// The namespace blocks of an update, after its heading.
|
||||
const changed = (text: string) => text.slice(text.indexOf(CHANGED) + CHANGED.length)
|
||||
|
||||
const summarize = (inventory: CodeModeCatalog.Inventory["tools"], budget?: number) =>
|
||||
CodeModeCatalog.summarize({ tools: inventory }, budget === undefined ? {} : { budget })
|
||||
|
||||
@@ -90,6 +96,18 @@ describe("CodeModeCatalog.summarize", () => {
|
||||
)
|
||||
})
|
||||
|
||||
test("names a tool that sits at its namespace's own path in full", () => {
|
||||
const catalog = summarize([
|
||||
entry("deploy", "Deploy", { parameters: ["target"] }),
|
||||
entry("echo", "Echo", { parameters: ["text"] }),
|
||||
entry("echo.sub", "Sub", { parameters: ["id"] }),
|
||||
])
|
||||
expect(catalog.namespaces.map((section) => section.names)).toEqual([
|
||||
["tools.deploy(target)"],
|
||||
["tools.echo(text)", "sub(id)"],
|
||||
])
|
||||
})
|
||||
|
||||
test("keeps every namespace and its description even when nothing else fits", () => {
|
||||
const catalog = summarize(
|
||||
[{ type: "namespace", name: "docs", description: "Project docs", tools: tools("x", 3) }, ...tools("alpha", 2)],
|
||||
@@ -151,7 +169,7 @@ describe("CodeModeInstructions.update", () => {
|
||||
test("names the tools that changed in a namespace", () => {
|
||||
expect(update([...tools("alpha", 3), ...tools("beta", 2)], [...tools("alpha", 4), ...tools("beta", 2)])).toBe(
|
||||
"The Code Mode tool catalog has changed.\n\n" +
|
||||
"These namespaces changed; a namespace shown with its whole listing replaces the previous one:\n" +
|
||||
`${CHANGED}\n` +
|
||||
"- alpha (4 tools)\n - new or changed: tool3(id)",
|
||||
)
|
||||
const changed = [entry("alpha.tool0", "x", { parameters: ["id", "…"] }), ...tools("alpha", 3).slice(1)]
|
||||
@@ -172,8 +190,37 @@ describe("CodeModeInstructions.update", () => {
|
||||
|
||||
test("restates a namespace's whole listing when that is shorter than its changes", () => {
|
||||
const text = update(tools("alpha", 2), [entry("alpha.renamed", "x", { parameters: ["id"] })])
|
||||
expect(text).toContain("- alpha (1 tool)\n - renamed(id)")
|
||||
expect(text).not.toContain("new or changed")
|
||||
expect(changed(text)).toBe("\n- alpha (1 tool)\n - renamed(id)")
|
||||
})
|
||||
|
||||
test("marks a pinned signature change so the rest of the namespace stays listed", () => {
|
||||
const before = [entry("ns.pinned", "Pinned", { pinned: true }), ...tools("ns", 9)]
|
||||
const after = [
|
||||
entry("ns.pinned", "Pinned", {
|
||||
pinned: true,
|
||||
signature: "tools.ns.pinned({\n id: string,\n}): Promise<string>",
|
||||
}),
|
||||
...tools("ns", 9),
|
||||
]
|
||||
expect(update(before, after)).toBe(
|
||||
`The Code Mode tool catalog has changed.\n\n${CHANGED}\n` +
|
||||
"- ns (10 tools)\n - new or changed signatures:\n - tools.ns.pinned({\n id: string,\n}): Promise<string> // Pinned",
|
||||
)
|
||||
})
|
||||
|
||||
test("says the listed tools are unchanged when only a namespace's header changed", () => {
|
||||
const before = summarize(tools("big", 300), 100)
|
||||
const after = summarize(tools("big", 301), 100)
|
||||
expect(CodeModeInstructions.update(before, after)).toContain(
|
||||
`- big (301 tools, ${after.namespaces[0]!.names.length} listed; search for the rest)\n - listed tools unchanged`,
|
||||
)
|
||||
})
|
||||
|
||||
test("does not report a tool gone when it is only pinned or unpinned", () => {
|
||||
const listed = tools("ns", 9)
|
||||
const pinned = [entry("ns.tool0", "ns 0", { pinned: true, parameters: ["id"] }), ...listed.slice(1)]
|
||||
expect(changed(update(listed, pinned))).not.toContain("no longer")
|
||||
expect(changed(update(pinned, listed))).not.toContain("no longer")
|
||||
})
|
||||
|
||||
test("restates a section whose description changed", () => {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { CodeModeTool } from "@opencode/core/codemode/tool"
|
||||
import type { Info } from "@opencode/schema/tool"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { z } from "zod"
|
||||
|
||||
// What the catalog lists for one tool's input, through the same path as registered tools.
|
||||
const listed = (input: Info["input"]) => {
|
||||
const tool: Info = { name: "probe", description: "Probe", input, execute: () => Effect.die("unused") }
|
||||
const entry = CodeModeTool.catalog({ tools: new Map([["probe", tool]]) }).tools[0]
|
||||
return entry?.type === "tool" ? entry.parameters : undefined
|
||||
}
|
||||
|
||||
describe("catalog parameters", () => {
|
||||
test("lists required names, then … for optional ones, from a raw JSON Schema object", () => {
|
||||
expect(
|
||||
listed({
|
||||
type: "object",
|
||||
properties: { owner: { type: "string" }, repo: { type: "string" }, page: { type: "number" } },
|
||||
required: ["owner", "repo"],
|
||||
}),
|
||||
).toEqual(["owner", "repo", "…"])
|
||||
expect(listed({ type: "object", properties: {} })).toEqual([])
|
||||
expect(listed({ type: "object" })).toEqual([])
|
||||
})
|
||||
|
||||
test("reads Effect and zod objects, including an Effect struct named by an identifier", () => {
|
||||
expect(listed(Schema.Struct({ id: Schema.String, note: Schema.optionalKey(Schema.String) }))).toEqual(["id", "…"])
|
||||
expect(listed(Schema.Struct({ id: Schema.String }).annotate({ identifier: "Lookup" }))).toEqual(["id"])
|
||||
expect(listed(z.object({ query: z.string(), limit: z.number().default(10) }))).toEqual(["query", "…"])
|
||||
})
|
||||
|
||||
test("leaves inputs that are not plain objects unsummarized rather than claiming they take nothing", () => {
|
||||
expect(
|
||||
listed(Schema.Union([Schema.Struct({ a: Schema.String }), Schema.Struct({ b: Schema.String })])),
|
||||
).toBeUndefined()
|
||||
expect(listed(Schema.Record(Schema.String, Schema.String))).toBeUndefined()
|
||||
expect(
|
||||
listed(z.discriminatedUnion("kind", [z.object({ kind: z.literal("a") }), z.object({ kind: z.literal("b") })])),
|
||||
).toBeUndefined()
|
||||
expect(listed({ anyOf: [{ type: "object", properties: { a: { type: "string" } } }] })).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -89,6 +89,9 @@ import { agentHost, modelHost, host, noProviders } from "./plugin/host"
|
||||
import { CodeModeCatalog } from "@opencode/core/codemode/catalog"
|
||||
import { CodeModeInstructions } from "@opencode/core/codemode/instructions"
|
||||
|
||||
const CHANGED =
|
||||
'These namespaces changed. Lines marked "new or changed", "no longer" or "unchanged" are changes to a namespace\'s previous listing; a namespace shown without them is its whole new listing:'
|
||||
|
||||
const emptyCodeMode = `${CodeModeInstructions.render({ total: 0, namespaces: [] })}\n\n`
|
||||
type ToolBarrier = {
|
||||
readonly count: number
|
||||
@@ -2430,7 +2433,7 @@ describe("SessionRunnerLLM", () => {
|
||||
const grown = yield* section("alpha")
|
||||
expect(updates(s.requests.at(-1)).at(-1)).toBe(
|
||||
"The Code Mode tool catalog has changed.\n\n" +
|
||||
"These namespaces changed; a namespace shown with its whole listing replaces the previous one:\n" +
|
||||
`${CHANGED}\n` +
|
||||
"- alpha (13 tools)\n - new or changed: alpha_12(id, …)",
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user