Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/guides/project-knowledge.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
```
Expand All @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions src/agent/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
Expand Down
100 changes: 100 additions & 0 deletions src/agent/hosted/chat-runtime-tool-assembly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
});

Expand Down
12 changes: 11 additions & 1 deletion src/agent/hosted/chat-runtime-tool-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
128 changes: 128 additions & 0 deletions src/agent/hosted/default-project-steering-refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
49 changes: 43 additions & 6 deletions src/agent/hosted/default-project-steering-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
kojiwakayama marked this conversation as resolved.
Resolved
// 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<string>) =>
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,
Expand Down
Loading
Loading