diff --git a/docs/guides/project-knowledge.md b/docs/guides/project-knowledge.md index 11345f0bc6..e8f6b17c26 100644 --- a/docs/guides/project-knowledge.md +++ b/docs/guides/project-knowledge.md @@ -28,7 +28,6 @@ export default agent({ system: "Use knowledge as evidence when answering support questions.", skills: { "support-*": true, "support-internal": false }, knowledge: { "knowledge/support/**": true, "knowledge/support/private/**": false }, - toolLoading: "deferred", toolResultContext: true, }); ``` @@ -39,6 +38,8 @@ and subtracts exclusions. Omission, `false`, empty selections, and exclusion-onl maps disable the framework capability. A selection limits browsing, search, counts, and exact document reads; guessing an excluded path does not grant access. Broad filesystem tools remain separate capabilities. +With deferred tool loading, `veryfront__search_knowledge` is visible from the +first step, so the agent can search knowledge without a tool search first. The initial prompt does not include document bodies or the full file catalog. Search returns compact metadata pages, and an exact lookup retrieves content. diff --git a/src/agent/factory.test.ts b/src/agent/factory.test.ts index 53631112ac..262a23ac72 100644 --- a/src/agent/factory.test.ts +++ b/src/agent/factory.test.ts @@ -230,7 +230,7 @@ describe("agent factory", () => { assertEquals(names.filter((name) => name === "veryfront__search_knowledge").length, 1); }); - it("loads one deferred framework knowledge schema for tools true agents", async () => { + it("shows one deferred framework knowledge schema from the first tools true step", async () => { const model = scriptedModel([ { toolCalls: [{ @@ -258,9 +258,11 @@ describe("agent factory", () => { await assistant.generate({ input: "Load the knowledge tool" }); + // Knowledge search is shown up front, and loading it again through tool + // search must not add a second schema. assertEquals( model.toolNames(0).filter((name) => name === "veryfront__search_knowledge").length, - 0, + 1, ); assertEquals( model.toolNames(1).filter((name) => name === "veryfront__search_knowledge").length, @@ -323,7 +325,11 @@ describe("agent factory", () => { const response = await assistant.generate({ input: "Search support knowledge" }); - assertEquals(response.toolCalls[0]?.status, "error"); + // Deferred mode shows the scoped framework knowledge search from the first + // step, so the call reaches the scoped source, never the late registry tool. + const [knowledgeCall] = response.toolCalls; + assertEquals(knowledgeCall?.status, "completed"); + assertEquals((knowledgeCall?.result as { query?: unknown } | undefined)?.query, "support"); assertEquals(unscopedCalls, 0); assertEquals(JSON.stringify(response.toolCalls).includes("unscoped"), false); }); diff --git a/src/agent/hosted/chat-runtime-tool-assembly.test.ts b/src/agent/hosted/chat-runtime-tool-assembly.test.ts index 3ec85fc478..e48b60edbe 100644 --- a/src/agent/hosted/chat-runtime-tool-assembly.test.ts +++ b/src/agent/hosted/chat-runtime-tool-assembly.test.ts @@ -1376,6 +1376,106 @@ Deno.test("prepareHostedChatRuntimeToolAssembly supplies authored framework know assertEquals(toolAssembly.toolLoadingMode, "deferred"); assertEquals(toolAssembly.remoteToolNames.includes("veryfront__search_knowledge"), true); assertEquals(toolAssembly.availableToolNames.includes("veryfront__search_knowledge"), true); + assertEquals(taskContext.availableToolNames, [ + "veryfront__search_knowledge", + "veryfront__tool_search", + ]); +}); + +Deno.test("prepareHostedChatRuntimeToolAssembly shows authorized knowledge search on the first deferred step", async () => { + const scenarios = [ + { name: "knowledge enabled", knowledge: "knowledge/**/*.md", expected: true }, + { name: "knowledge disabled", knowledge: undefined, expected: false }, + { + name: "knowledge denied", + knowledge: "knowledge/**/*.md", + deniedToolNames: ["veryfront__search_knowledge"], + expected: false, + }, + { + name: "knowledge outside the allowed tools", + knowledge: "knowledge/**/*.md", + allowedToolNames: ["veryfront__create_file"], + expected: false, + }, + ] as const; + + for (const scenario of scenarios) { + const taskContext: HostedChatRuntimeToolAssemblyContext = { + authToken: "token", + projectId: "project-1", + model: "anthropic/claude-sonnet-4-6", + }; + const toolAssembly = await prepareHostedChatRuntimeToolAssembly({ + sourceIntegrationPolicy: unrestrictedSourceIntegrationPolicy, + taskContext, + instructions: "Base instructions", + localTools: {}, + apiUrl: "https://api.example.com", + apiMcpUrl: "https://api.example.com/mcp", + allowedToolNames: "allowedToolNames" in scenario ? scenario.allowedToolNames : null, + ...("deniedToolNames" in scenario ? { deniedToolNames: [...scenario.deniedToolNames] } : {}), + toolLoading: "deferred", + ...(scenario.knowledge === undefined ? {} : { knowledge: scenario.knowledge }), + createRemoteToolSource: remoteSourceFromConfig, + preloadLatestConversationUserText: false, + }); + + assertEquals(toolAssembly.toolLoadingMode, "deferred", scenario.name); + assertEquals( + toolAssembly.modelVisibleToolNames?.includes("veryfront__search_knowledge"), + scenario.expected, + scenario.name, + ); + assertEquals( + taskContext.availableToolNames?.includes("veryfront__search_knowledge"), + scenario.expected, + scenario.name, + ); + if (!scenario.expected) { + assertEquals( + toolAssembly.availableToolNames.includes("veryfront__search_knowledge"), + false, + scenario.name, + ); + } + // Other deferred tools stay behind search. + assertEquals( + toolAssembly.modelVisibleToolNames?.includes("veryfront__tool_search"), + true, + scenario.name, + ); + assertEquals( + toolAssembly.modelVisibleToolNames?.includes("veryfront__create_file"), + false, + scenario.name, + ); + } +}); + +Deno.test("prepareHostedChatRuntimeToolAssembly gives the last provider slot to tool search over knowledge search", async () => { + const taskContext: HostedChatRuntimeToolAssemblyContext = { + authToken: "token", + projectId: "project-1", + model: "openai/gpt-test", + }; + const toolAssembly = await prepareHostedChatRuntimeToolAssembly({ + sourceIntegrationPolicy: unrestrictedSourceIntegrationPolicy, + taskContext, + instructions: "Base instructions", + localTools: {}, + apiUrl: "https://api.example.com", + apiMcpUrl: "https://api.example.com/mcp", + allowedToolNames: null, + toolLoading: "deferred", + knowledge: "knowledge/**/*.md", + reservedToolSlots: 127, + createRemoteToolSource: remoteSourceFromConfig, + preloadLatestConversationUserText: false, + }); + + // Knowledge search stays authorized and loadable through tool search. + assertEquals(toolAssembly.availableToolNames.includes("veryfront__search_knowledge"), true); assertEquals(taskContext.availableToolNames, ["veryfront__tool_search"]); }); diff --git a/src/agent/hosted/chat-runtime-tool-assembly.ts b/src/agent/hosted/chat-runtime-tool-assembly.ts index a63e6ac4de..f97751a066 100644 --- a/src/agent/hosted/chat-runtime-tool-assembly.ts +++ b/src/agent/hosted/chat-runtime-tool-assembly.ts @@ -1210,10 +1210,20 @@ async function prepareHostedChatRuntimeToolAssemblyInternal< // the model, so the provider schema limit must not truncate its searchable or // executable authorization catalog. Eager mode still needs an up-front cap. const configuredBootstrapNames = snapshotConfiguredToolBootstrapNames(input.bootstrapTools); - const optionalBootstrapNames = snapshotConfiguredToolBootstrapNames( + const configuredOptionalBootstrapNames = snapshotConfiguredToolBootstrapNames( input.optionalBootstrapTools, "optionalBootstrapTools", ); + // An agent with a knowledge scope answers from that knowledge, so deferred + // mode shows its knowledge search from the first step instead of behind + // tool search. The name comes only from the knowledge source listing, which + // already applied the allow and deny selectors, and the optional bootstrap + // path below still requires it to be in the authorized catalog. + const optionalBootstrapNames = toolLoadingMode === "deferred" && + includesValue(knowledgeRemoteToolNames, FRAMEWORK_KNOWLEDGE_TOOL_NAME) && + !includesValue(configuredOptionalBootstrapNames, FRAMEWORK_KNOWLEDGE_TOOL_NAME) + ? [...configuredOptionalBootstrapNames, FRAMEWORK_KNOWLEDGE_TOOL_NAME] + : configuredOptionalBootstrapNames; const availableToolNames = toolLoadingMode === "deferred" ? authorizedToolNames : selectProviderCompatibleToolNames(providerVisibleAuthorizedToolNames, { diff --git a/src/agent/hosted/default-project-steering-refresh.test.ts b/src/agent/hosted/default-project-steering-refresh.test.ts index a2105b5595..c84800aad3 100644 --- a/src/agent/hosted/default-project-steering-refresh.test.ts +++ b/src/agent/hosted/default-project-steering-refresh.test.ts @@ -4,6 +4,7 @@ import { describe, it } from "#veryfront/testing/bdd.ts"; import { normalizeSourceIntegrationPolicy } from "#veryfront/integrations/source-policy.ts"; import type { RemoteToolSource, Tool } from "#veryfront/tool"; import { markTrustedHostToolProvenance } from "#veryfront/tool/host-tool-provenance.ts"; +import { markTrustedPlatformSource } from "#veryfront/tool/platform-source-provenance.ts"; import type { DefaultHostedChatRuntimeSystemRefreshInput } from "./default-chat-runtime.ts"; import { createDefaultHostedProjectSteeringRefresh, @@ -330,6 +331,133 @@ describe("agent/default-hosted-project-steering-refresh", () => { ]); }); + it("keeps deferred startup knowledge search only while the refreshed catalog lists it", async () => { + // Deferred mode authorizes the whole catalog, so the provider schema cap + // must not drop a listed startup tool that sorts after it (third case), but + // tool search keeps priority for the slots (fourth case). + const filler = (name: string) => ({ + name, + description: name, + parameters: { type: "object" as const, properties: {} }, + }); + for ( + const { stillListed, reservedToolSlots, extraToolNames, expected } of [ + { + stillListed: true, + reservedToolSlots: undefined, + extraToolNames: [], + expected: ["veryfront__search_knowledge", "veryfront__tool_search"], + }, + { + stillListed: false, + reservedToolSlots: undefined, + extraToolNames: [], + expected: ["veryfront__tool_search"], + }, + { + stillListed: true, + reservedToolSlots: 125, + extraToolNames: ["veryfront__a_tool", "veryfront__b_tool"], + expected: ["veryfront__search_knowledge", "veryfront__tool_search"], + }, + { + stillListed: true, + reservedToolSlots: 127, + extraToolNames: [], + expected: ["veryfront__tool_search"], + }, + ] + ) { + const catalogToolNames = [ + "veryfront__create_file", + ...extraToolNames, + ...(stillListed ? ["veryfront__search_knowledge"] : []), + ].sort(); + const remoteToolSource: RemoteToolSource = markTrustedPlatformSource({ + id: "framework-knowledge", + listTools: () => Promise.resolve(catalogToolNames.map(filler)), + executeTool: () => Promise.resolve({ ok: true }), + }); + const refresh = createDefaultHostedProjectSteeringRefresh({ + fetchProjectInstructions: () => Promise.resolve("Fresh instructions"), + fetchSkills: () => Promise.resolve([]), + buildInstructions: (input) => input.instructions, + }); + const startupToolNames = [ + "veryfront__create_file", + ...extraToolNames, + "veryfront__search_knowledge", + ].sort(); + const input = createRefreshInput({ + toolAssembly: { + sourceIntegrationPolicy: normalizeSourceIntegrationPolicy(undefined), + runtimeTools: {}, + remoteToolSources: [remoteToolSource], + localToolNames: [], + remoteToolNames: startupToolNames, + providerToolNames: [], + availableToolNames: startupToolNames, + modelVisibleToolNames: ["veryfront__search_knowledge", "veryfront__tool_search"], + toolLoadingMode: "deferred", + compatibleRemoteToolNames: startupToolNames, + systemInstructions: "", + ...(reservedToolSlots === undefined ? {} : { reservedToolSlots }), + }, + }); + + await refresh(input); + + assertEquals(input.taskContext.availableToolNames, expected); + } + }); + + it("keeps tool search when one slot remains and an authorized tool sorts after knowledge search", async () => { + const remoteToolSource: RemoteToolSource = markTrustedPlatformSource({ + id: "framework-knowledge", + listTools: () => + Promise.resolve([ + { + name: "veryfront__search_knowledge", + description: "Search knowledge", + parameters: { type: "object" as const, properties: {} }, + }, + { + name: "veryfront__write_file", + description: "Write a file", + parameters: { type: "object" as const, properties: {} }, + }, + ]), + executeTool: () => Promise.resolve({ ok: true }), + }); + const refresh = createDefaultHostedProjectSteeringRefresh({ + fetchProjectInstructions: () => Promise.resolve("Fresh instructions"), + fetchSkills: () => Promise.resolve([]), + buildInstructions: (input) => input.instructions, + }); + const input = createRefreshInput({ + toolAssembly: { + sourceIntegrationPolicy: normalizeSourceIntegrationPolicy(undefined), + runtimeTools: {}, + remoteToolSources: [remoteToolSource], + localToolNames: [], + remoteToolNames: ["veryfront__search_knowledge", "veryfront__write_file"], + providerToolNames: [], + availableToolNames: ["veryfront__search_knowledge", "veryfront__write_file"], + modelVisibleToolNames: ["veryfront__search_knowledge", "veryfront__tool_search"], + toolLoadingMode: "deferred", + compatibleRemoteToolNames: ["veryfront__search_knowledge", "veryfront__write_file"], + systemInstructions: "", + reservedToolSlots: 127, + }, + }); + + await refresh(input); + + // The single slot goes to tool search: veryfront__write_file stays + // reachable, and knowledge search remains loadable through search. + assertEquals(input.taskContext.availableToolNames, ["veryfront__tool_search"]); + }); + it("applies the source integration policy to the eager model-visible inventory", async () => { const remoteToolSource: RemoteToolSource = { id: "api", diff --git a/src/agent/hosted/default-project-steering-refresh.ts b/src/agent/hosted/default-project-steering-refresh.ts index cfb6f85591..bc241b6755 100644 --- a/src/agent/hosted/default-project-steering-refresh.ts +++ b/src/agent/hosted/default-project-steering-refresh.ts @@ -292,14 +292,51 @@ export function createDefaultHostedProjectSteeringRefresh( reservedToolSlots: input.toolAssembly.reservedToolSlots, requiredToolNames: providerVisibleLocalToolNames, }); - const bootstrapToolNames = (input.toolAssembly.modelVisibleToolNames ?? toolNames).filter( + const previousModelVisibleToolNames = input.toolAssembly.modelVisibleToolNames; + const requiredBootstrapToolNames = (previousModelVisibleToolNames ?? toolNames).filter( isLoadSkillToolName, ); - const selectedBootstrapToolNames = new Set(bootstrapToolNames); - const hasDeferredTools = toolNames.some((toolName) => { - if (selectedBootstrapToolNames.has(toolName)) return false; - return !isRuntimeProviderSchemaHiddenTool(input.toolAssembly.runtimeTools[toolName]); - }); + // Deferred mode authorizes the whole catalog, so check membership and + // deferred availability before the provider schema cap, as the assembly does. + const refreshedToolNames = new Set(allToolNames); + // Startup schemas the assembly already showed (such as knowledge search) + // stay listed while the refreshed catalog still authorizes them. + const retainedStartupToolNames = previousModelVisibleToolNames === undefined + ? [] + : previousModelVisibleToolNames.filter((toolName) => + !isLoadSkillToolName(toolName) && + toolName !== TOOL_SEARCH_TOOL_NAME && + refreshedToolNames.has(toolName) + ); + const hasDeferredToolsBeyond = (bootstrap: ReadonlySet) => + allToolNames.some((toolName) => { + if (bootstrap.has(toolName)) return false; + return !isRuntimeProviderSchemaHiddenTool(input.toolAssembly.runtimeTools[toolName]); + }); + const startupCandidateToolNames = [ + ...new Set([...requiredBootstrapToolNames, ...retainedStartupToolNames]), + ]; + const selectBootstrapToolNames = (reserveDiscoverySlots: boolean) => [ + ...new Set([ + ...requiredBootstrapToolNames, + ...selectProviderCompatibleToolNames(startupCandidateToolNames, { + ...(input.toolAssembly.toolSchemaTarget ?? { model: input.taskContext.model }), + requiredToolNames: requiredBootstrapToolNames, + // Retained startup schemas are best effort: like the assembly, keep + // slots for tool search and one discovered schema so every authorized + // deferred tool stays reachable. Tool search wins when both cannot fit. + reservedToolSlots: (input.toolAssembly.reservedToolSlots ?? 0) + + (reserveDiscoverySlots ? 2 : 0), + }), + ]), + ]; + const reserveDiscoverySlots = hasDeferredToolsBeyond(new Set(startupCandidateToolNames)); + let bootstrapToolNames = selectBootstrapToolNames(reserveDiscoverySlots); + if (!reserveDiscoverySlots && bootstrapToolNames.length < startupCandidateToolNames.length) { + // A dropped startup schema is itself deferred now, so tool search needs room. + bootstrapToolNames = selectBootstrapToolNames(true); + } + const hasDeferredTools = hasDeferredToolsBeyond(new Set(bootstrapToolNames)); const modelVisibleToolNames = input.toolAssembly.toolLoadingMode === "deferred" ? [ ...bootstrapToolNames, diff --git a/src/agent/runtime/agent-runtime-step.test.ts b/src/agent/runtime/agent-runtime-step.test.ts index 998ccf1d28..7d0dad2a52 100644 --- a/src/agent/runtime/agent-runtime-step.test.ts +++ b/src/agent/runtime/agent-runtime-step.test.ts @@ -17,6 +17,7 @@ import { import { createToolExposureState, searchToolExposure } from "./tool-exposure.ts"; import { markTrustedHostToolProvenance } from "#veryfront/tool/host-tool-provenance.ts"; import { flattenSystemInstructions } from "./tool-inventory.ts"; +import { createAgentKnowledgeSource, createAgentKnowledgeTool } from "./knowledge-tools.ts"; function systemText(system: AgentSystem): string { return typeof system === "string" ? system : flattenSystemInstructions(system); @@ -516,6 +517,134 @@ describe("agent/runtime-step", () => { ); }); + describe("direct-run knowledge search in deferred mode", () => { + const knowledgeConfig = { knowledge: "knowledge/**" } as AgentConfig; + + function knowledgeStepInput( + config: Record, + overrides: Partial[0]> = {}, + ): Parameters[0] { + return { + agentId: "agent_1", + activeSkillToolAvailability: undefined, + allowedRemoteToolNames: undefined, + config: { + model: "auto", + system: "Base", + toolLoading: "deferred", + ...config, + } as unknown as AgentConfig, + forwardedRemoteToolDefinitions: undefined, + getAvailableTools: async () => [ + toolDefinition("create_file"), + toolDefinition("load_skill"), + toolDefinition("veryfront__search_knowledge"), + ], + supportsToolCalling: true, + messages: [], + mode: "generate", + remoteToolSources: undefined, + resolveRuntimeState: async () => ({ systemPrompt: "Base" }), + runtimeContext: undefined, + step: 1, + systemPrompt: "Base", + toolContextBase: undefined, + toolExposureState: createToolExposureState(), + ...overrides, + }; + } + + function trustedLoader() { + return markTrustedHostToolProvenance({ id: "load_skill", description: "Loader" }); + } + + it("shows the agent's own knowledge tool on the first step", async () => { + const prepared = await prepareAgentRuntimeStep(knowledgeStepInput({ + tools: { + load_skill: trustedLoader(), + veryfront__search_knowledge: createAgentKnowledgeTool(knowledgeConfig), + }, + })); + + assertEquals( + prepared.tools.map((tool) => tool.name), + ["load_skill", "veryfront__search_knowledge", "veryfront__tool_search"], + ); + assertEquals(prepared.toolExposurePlan.deferred.map((tool) => tool.name), ["create_file"]); + }); + + it("shows knowledge search from the framework knowledge source behind tools: true", async () => { + const source = createAgentKnowledgeSource({ ...knowledgeConfig, tools: true }); + if (source === undefined) throw new Error("expected a knowledge source"); + const prepared = await prepareAgentRuntimeStep(knowledgeStepInput( + { tools: true, skills: false }, + { remoteToolSources: [source] }, + )); + + assertEquals( + prepared.tools.map((tool) => tool.name), + ["veryfront__search_knowledge", "veryfront__tool_search"], + ); + assertEquals( + prepared.toolExposurePlan.deferred.map((tool) => tool.name), + ["create_file", "load_skill"], + ); + }); + + it("keeps a project tool or source with the knowledge name behind tool search", async () => { + const projectTool = await prepareAgentRuntimeStep(knowledgeStepInput({ + tools: { + load_skill: trustedLoader(), + veryfront__search_knowledge: { + id: "veryfront__search_knowledge", + description: "Project tool", + }, + }, + })); + const projectSource = await prepareAgentRuntimeStep(knowledgeStepInput( + { tools: true, skills: false }, + { remoteToolSources: [remoteToolSource("framework-knowledge")] }, + )); + + for (const prepared of [projectTool, projectSource]) { + assertEquals( + prepared.tools.some((tool) => tool.name === "veryfront__search_knowledge"), + false, + ); + assertEquals( + prepared.toolExposurePlan.deferred.some((tool) => + tool.name === "veryfront__search_knowledge" + ), + true, + ); + } + }); + + it("does not show knowledge search the step excludes, and keeps host-supplied names", async () => { + const tools = { + load_skill: trustedLoader(), + veryfront__search_knowledge: createAgentKnowledgeTool(knowledgeConfig), + }; + const excluded = await prepareAgentRuntimeStep(knowledgeStepInput( + { tools }, + { excludedToolNames: new Set(["veryfront__search_knowledge"]) }, + )); + const hostSelected = await prepareAgentRuntimeStep(knowledgeStepInput({ + tools, + __vfToolBootstrapNames: ["load_skill"], + })); + + assertEquals(excluded.tools.map((tool) => tool.name), [ + "load_skill", + "veryfront__tool_search", + ]); + assertEquals( + hostSelected.tools.map((tool) => tool.name), + ["load_skill", "veryfront__tool_search"], + ); + }); + }); + it("keeps provider-native tools authorized but deferred until veryfront__tool_search loads them", async () => { const state = createToolExposureState(); const prepared = await prepareAgentRuntimeStep({ diff --git a/src/agent/runtime/agent-runtime-step.ts b/src/agent/runtime/agent-runtime-step.ts index 9442359819..789e199ae8 100644 --- a/src/agent/runtime/agent-runtime-step.ts +++ b/src/agent/runtime/agent-runtime-step.ts @@ -54,6 +54,7 @@ import { import type { VeryfrontCloudSurfaceId } from "#veryfront/provider/veryfront-cloud/model-catalog.ts"; import { resolveModelProviderOptionKey } from "./model-resolution.ts"; import { createProviderNativeToolExposureDefinitions } from "./provider-native-tool-inventory.ts"; +import { resolveTrustedKnowledgeToolName } from "./knowledge-tools.ts"; const ArrayIsArray = Array.isArray; const ObjectHasOwn = Object.hasOwn; @@ -409,7 +410,14 @@ export async function prepareAgentRuntimeStep( // bootstrap the loader the skill catalog names, chosen by trusted // provenance; a project tool owning a loader spelling is never bootstrapped. let bootstrapToolNames = getRuntimeToolBootstrapNames(input.config); + // Hosted runs fold knowledge search into the names they supply. A direct run + // shows the agent's own knowledge search up front, chosen by provenance. + let trustedKnowledgeToolName: string | undefined; if (bootstrapToolNames === undefined) { + trustedKnowledgeToolName = resolveTrustedKnowledgeToolName( + input.config.tools, + input.remoteToolSources, + ); const trustedSkillLoaderToolName = resolveTrustedSkillLoaderToolName(input.config.tools); const provenanceBootstrapToolNames = new IntrinsicSet(); if (trustedSkillLoaderToolName !== undefined) { @@ -454,10 +462,17 @@ export async function prepareAgentRuntimeStep( input.reservedToolSlots, ); const toolLoadingMode = resolveRuntimeToolLoading(input.config).mode; - if (optionalBootstrapNames.length > 0) { + // Knowledge search joins the optional startup names, so it is shown only when + // this step still authorizes it and the provider schema limit leaves room. + const startupOptionalBootstrapNames = toolLoadingMode === "deferred" && + trustedKnowledgeToolName !== undefined && + !somePrivateArray(optionalBootstrapNames, (name) => name === trustedKnowledgeToolName) + ? concatPrivateArrays(optionalBootstrapNames, [trustedKnowledgeToolName]) + : optionalBootstrapNames; + if (startupOptionalBootstrapNames.length > 0) { const candidates = [...bootstrapToolNames]; - for (let index = 0; index < optionalBootstrapNames.length; index++) { - const name = optionalBootstrapNames[index]!; + for (let index = 0; index < startupOptionalBootstrapNames.length; index++) { + const name = startupOptionalBootstrapNames[index]!; if ( setHas(existingToolNames, name) && somePrivateArray( diff --git a/src/agent/runtime/knowledge-tools.ts b/src/agent/runtime/knowledge-tools.ts index 5c11d2505a..6c8d7aa32b 100644 --- a/src/agent/runtime/knowledge-tools.ts +++ b/src/agent/runtime/knowledge-tools.ts @@ -10,6 +10,9 @@ import type { AgentConfig } from "../types.ts"; import type { RemoteToolSource, Tool } from "#veryfront/tool"; import { toolToProviderDefinition } from "#veryfront/tool/registry.ts"; import { createPrivateWeakStore } from "#veryfront/security/private-weak-store.ts"; +import { somePrivateArray } from "#veryfront/security/private-array.ts"; + +const ObjectHasOwn = Object.hasOwn; const markFrameworkKnowledgeOptions = claimFrameworkKnowledgeMarker(); const agentKnowledgeSources = createPrivateWeakStore(); @@ -80,3 +83,30 @@ export function createAgentKnowledgeSource(config: AgentConfig): RemoteToolSourc }, }); } + +const AGENT_KNOWLEDGE_TOOL_NAME = "veryfront__search_knowledge"; + +/** + * @internal The framework knowledge search name when the agent's own catalog + * carries it by provenance: the scoped local tool the factory adds to a tool + * record, or the framework knowledge source behind `tools: true`. A project + * tool or remote source using the same name never qualifies. + */ +export function resolveTrustedKnowledgeToolName( + tools: AgentConfig["tools"], + remoteToolSources: readonly RemoteToolSource[] | undefined, +): typeof AGENT_KNOWLEDGE_TOOL_NAME | undefined { + if ( + typeof tools === "object" && tools !== null && + ObjectHasOwn(tools, AGENT_KNOWLEDGE_TOOL_NAME) && + isAgentKnowledgeTool((tools as Record)[AGENT_KNOWLEDGE_TOOL_NAME]) + ) { + return AGENT_KNOWLEDGE_TOOL_NAME; + } + if ( + remoteToolSources !== undefined && somePrivateArray(remoteToolSources, isAgentKnowledgeSource) + ) { + return AGENT_KNOWLEDGE_TOOL_NAME; + } + return undefined; +}