fix(skills): inject no cached skill when the registry load fails

A failed loadFromDatabase leaves the previous load's entries in the registry,
and the request then advertised them through injectSkillsWithMetadata, so a
removed or changed skill could still reach the model. The request now skips
custom-skill injection after a failed load; client and memory tools stay as
they were.
This commit is contained in:
serenity
2026-11-18 23:42:18 -03:00
committed by diegosouzapw
parent 81e6ee6983
commit 1b2a4152d0
2 changed files with 55 additions and 14 deletions

View File

@@ -282,30 +282,36 @@ export async function injectMemoryAndSkills({
// Memory builtins and registered Skills are only executed by the
// non-streaming server-owned tool loop; stream clients execute tools
// client-side, so we skip injection for stream requests.
let skillsLoaded = true;
try {
await skillRegistry.loadFromDatabase(memoryOwnerId);
} catch (skillsErr) {
// Skills are optional extras: a failed load (one corrupt row) must not fail the request.
// The registry may still hold an earlier load's entries (a removed or changed skill), so
// this request injects no custom skill; client and memory tools are left as they are.
skillsLoaded = false;
log?.debug?.(
"SKILLS",
`Skills load skipped: ${skillsErr instanceof Error ? skillsErr.message : String(skillsErr)}`
);
}
const existingTools = Array.isArray(body.tools) ? body.tools : [];
const { tools: mergedTools, injectedNames } = injectSkillsWithMetadata({
provider: getSkillsProviderForFormat(sourceFormat),
existingTools,
apiKeyId: memoryOwnerId,
model: typeof effectiveModel === "string" ? effectiveModel : undefined,
sourceFormat,
targetFormat,
backgroundReason,
messages: Array.isArray(body.messages)
? body.messages
: Array.isArray(body.input)
? body.input
: undefined,
});
const { tools: mergedTools, injectedNames } = !skillsLoaded
? { tools: existingTools, injectedNames: [] as string[] }
: injectSkillsWithMetadata({
provider: getSkillsProviderForFormat(sourceFormat),
existingTools,
apiKeyId: memoryOwnerId,
model: typeof effectiveModel === "string" ? effectiveModel : undefined,
sourceFormat,
targetFormat,
backgroundReason,
messages: Array.isArray(body.messages)
? body.messages
: Array.isArray(body.input)
? body.input
: undefined,
});
if (mergedTools.length > existingTools.length) {
body = {

View File

@@ -74,6 +74,41 @@ test("a corrupt skill row does not make the injection reject", async () => {
assert.deepEqual(result.body.messages, body.messages, "the request still goes through");
});
// A failed load leaves the previous load's entries in the registry: those may describe a removed
// or changed skill, so the request must not advertise them. Client tools still go through.
test("a failed skill load injects no cached skill and keeps the client's tools", async (t) => {
await updateSettings({ memoryEnabled: true, memoryMaxTokens: 2000, skillsEnabled: true });
invalidateMemorySettingsCache();
resetSkillsRegistry();
await skillRegistry.register({
name: "stale-skill",
version: "1.0.0",
description: "cached before the failed load",
schema: { input: {}, output: {} },
handler: "h",
enabled: true,
apiKeyId: "key-stale",
mode: "on",
} as unknown as Parameters<typeof skillRegistry.register>[0]);
t.mock.method(skillRegistry, "loadFromDatabase", async () => {
throw new Error("database is locked");
});
const clientTool = { type: "function", function: { name: "client_tool", parameters: {} } };
const body = {
model: "gpt-4o",
messages: [{ role: "user", content: "hi" }],
tools: [clientTool],
};
const result = await call(body, "key-stale");
assert.deepEqual(result.injectedCustomSkillNames, [], "no skill from the stale cache");
const names = (result.body.tools as Array<{ function?: { name?: string } }>).map(
(tool) => tool.function?.name
);
assert.ok(names.includes("client_tool"), "the client's tool is kept");
assert.ok(!names.some((name) => String(name).includes("stale")), JSON.stringify(names));
resetSkillsRegistry();
});
// Automatic prefix caching (OpenAI, Codex, Azure) needs a byte-identical prefix from token 0:
// a per-query memory text at messages[0] misses the cache on every turn.
test("OpenAI memory injection leaves the prefix of the conversation untouched", async () => {