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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Unit tests for the skill preference/acknowledgment loaders, driven against a
* hand-built runtime stub (no real DB). Asserts the fast-fail contract: a
* genuinely-empty cache reads as `{}`, but a cache *read failure* propagates
* instead of being masked as "no preferences" — the callers read-modify-write
* these maps before saving them back, so a fabricated empty would wipe state.
*/

import type { AgentRuntime } from "@elizaos/core";
import { describe, expect, it, vi } from "vitest";
import {
loadSkillAcknowledgments,
loadSkillPreferences,
} from "./skill-discovery-helpers";

function runtimeWithCache(getCache: () => Promise<unknown>): AgentRuntime {
return { getCache: vi.fn(getCache) } as unknown as AgentRuntime;
}

describe("loadSkillPreferences", () => {
it("returns an empty map when no runtime is available", async () => {
await expect(loadSkillPreferences(null)).resolves.toEqual({});
});

it("returns an empty map when nothing has been persisted yet", async () => {
const runtime = runtimeWithCache(async () => undefined);
await expect(loadSkillPreferences(runtime)).resolves.toEqual({});
});

it("returns the persisted map when the cache read succeeds", async () => {
const runtime = runtimeWithCache(async () => ({ "skill-a": true }));
await expect(loadSkillPreferences(runtime)).resolves.toEqual({
"skill-a": true,
});
});

it("propagates a cache read failure instead of masking it as an empty map", async () => {
const dbError = new Error("cache backend down");
const runtime = runtimeWithCache(async () => {
throw dbError;
});
await expect(loadSkillPreferences(runtime)).rejects.toBe(dbError);
});
});

describe("loadSkillAcknowledgments", () => {
it("returns an empty map when no runtime is available", async () => {
await expect(loadSkillAcknowledgments(null)).resolves.toEqual({});
});

it("returns an empty map when nothing has been persisted yet", async () => {
const runtime = runtimeWithCache(async () => undefined);
await expect(loadSkillAcknowledgments(runtime)).resolves.toEqual({});
});

it("propagates a cache read failure instead of masking it as an empty map", async () => {
const dbError = new Error("cache backend down");
const runtime = runtimeWithCache(async () => {
throw dbError;
});
await expect(loadSkillAcknowledgments(runtime)).rejects.toBe(dbError);
});
});
36 changes: 19 additions & 17 deletions plugins/plugin-agent-skills/src/api/skill-discovery-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ function isAgentSkillsServiceLike(
}

/**
* Load persisted skill preferences from the agent's database.
* Returns an empty map when the runtime or database isn't available.
* Load persisted skill preferences from the agent's database. Returns an empty
* map when no runtime is available or nothing has been persisted yet; a cache
* read *failure* is reported and rethrown, never masked as "no preferences".
* Callers read-modify-write this map (`prefs[id] = x; save(prefs)`), so a
* fabricated empty map on a transient DB error would overwrite every other
* skill's saved preference — the failure must surface, not read as empty.
*/
export async function loadSkillPreferences(
runtime: AgentRuntime | null,
): Promise<SkillPreferencesMap> {
if (!runtime) return {};
try {
const prefs = await runtime.getCache<SkillPreferencesMap>(
SKILL_PREFS_CACHE_KEY,
);
return prefs ?? {};
} catch {
return {};
}
const prefs = await runtime.getCache<SkillPreferencesMap>(
SKILL_PREFS_CACHE_KEY,
);
return prefs ?? {};
}

/**
Expand Down Expand Up @@ -81,17 +81,19 @@ type SkillAcknowledgmentMap = Record<
{ acknowledgedAt: string; findingCount: number }
>;

/**
* Load persisted skill scan acknowledgments. Same contract as
* {@link loadSkillPreferences}: an empty map means "none persisted yet"; a cache
* read failure propagates so a broken DB read is never merged over and saved
* back as an acknowledgment wipe.
*/
export async function loadSkillAcknowledgments(
runtime: AgentRuntime | null,
): Promise<SkillAcknowledgmentMap> {
if (!runtime) return {};
try {
const acks =
await runtime.getCache<SkillAcknowledgmentMap>(SKILL_ACK_CACHE_KEY);
return acks ?? {};
} catch {
return {};
}
const acks =
await runtime.getCache<SkillAcknowledgmentMap>(SKILL_ACK_CACHE_KEY);
return acks ?? {};
}

export async function saveSkillAcknowledgments(
Expand Down
29 changes: 14 additions & 15 deletions plugins/plugin-agent-skills/src/api/skills-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,18 @@ function shouldExposeBinanceSkillRecord(skill: {
const SKILL_PREFS_CACHE_KEY = "eliza:skill-preferences";
type SkillPreferencesMap = Record<string, boolean>;

// An empty map means "none persisted yet" (the `?? {}` below); a cache read
// *failure* propagates. Callers read-modify-write this map before saving it
// back, so masking a transient DB error as `{}` would overwrite every other
// skill's saved preference — the read failure must surface, not read as empty.
async function loadSkillPreferences(
runtime: AgentRuntime | null,
): Promise<SkillPreferencesMap> {
if (!runtime) return {};
try {
const prefs = await runtime.getCache<SkillPreferencesMap>(
SKILL_PREFS_CACHE_KEY,
);
return prefs ?? {};
} catch {
return {};
}
const prefs = await runtime.getCache<SkillPreferencesMap>(
SKILL_PREFS_CACHE_KEY,
);
return prefs ?? {};
}

async function saveSkillPreferences(
Expand All @@ -268,17 +268,16 @@ type SkillAcknowledgmentMap = Record<
{ acknowledgedAt: string; findingCount: number }
>;

// Same contract as loadSkillPreferences: `{}` means "none acknowledged yet"; a
// cache read failure propagates rather than being merged over and saved back as
// an acknowledgment wipe.
async function loadSkillAcknowledgments(
runtime: AgentRuntime | null,
): Promise<SkillAcknowledgmentMap> {
if (!runtime) return {};
try {
const acks =
await runtime.getCache<SkillAcknowledgmentMap>(SKILL_ACK_CACHE_KEY);
return acks ?? {};
} catch {
return {};
}
const acks =
await runtime.getCache<SkillAcknowledgmentMap>(SKILL_ACK_CACHE_KEY);
return acks ?? {};
}

async function saveSkillAcknowledgments(
Expand Down
33 changes: 32 additions & 1 deletion plugins/plugin-browser/src/__tests__/browser-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* BrowserService tests for target registration, resolution, and dispatch behavior.
*/

import { ElizaError } from "@elizaos/core";
import { ElizaError, logger } from "@elizaos/core";
import { afterEach, describe, expect, it, vi } from "vitest";
import { BrowserService, type BrowserTarget } from "../browser-service.js";

Expand Down Expand Up @@ -70,6 +70,37 @@ describe("BrowserService target routing", () => {
expect(bridge.execute).toHaveBeenCalledTimes(1);
});

it("excludes an unpinned target whose availability check throws, still resolves a healthy one, and logs the exclusion", async () => {
const logSpy = vi
.spyOn(logger, "debug")
.mockImplementation(() => logger as never);
const service = new BrowserService();
const broken = createTarget({
id: "broken",
priority: 200,
availableError: new Error("probe boom"),
});
const workspace = createTarget({ id: "workspace", priority: 100 });
service.registerTarget(broken);
service.registerTarget(workspace);

// Automatic (unpinned) resolution must not throw: the broken candidate is
// skipped and the next healthy target is selected (designed failover).
const result = await service.execute({ subaction: "state" });
expect(result.value).toBe("workspace");
expect(broken.execute).not.toHaveBeenCalled();
// The exclusion is observable, not silently swallowed by an empty catch.
expect(
logSpy.mock.calls.some(
(call) =>
typeof call[0] === "string" &&
call[0].includes('"broken"') &&
call[0].includes("probe boom"),
),
).toBe(true);
logSpy.mockRestore();
});

it("does not fall back when the caller pins a target", async () => {
const service = new BrowserService();
service.registerTarget(
Expand Down
12 changes: 10 additions & 2 deletions plugins/plugin-browser/src/browser-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,16 @@ export class BrowserService extends Service {
target,
});
}
} catch {
// Ignore unhealthy targets during target resolution.
} catch (err) {
// error-policy:J4 failover scan — a candidate whose availability check
// throws is excluded so the other registered targets can still be
// selected (unlike the pinned-`preferredId` path, which throws). Log so
// a systemically broken target is observable instead of silently gone.
logger.debug(
`[BrowserService] target "${id}" excluded from resolution; availability check failed: ${
err instanceof Error ? err.message : String(err)
}`,
);
}
}

Expand Down
Loading