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
2 changes: 1 addition & 1 deletion examples/agents/04-guardrails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const factChecker = guardrail(
{
name: 'fact_checker',
position: 'output',
onFail: 'human',
onFail: 'retry',
},
);

Expand Down
65 changes: 65 additions & 0 deletions src/agents/__tests__/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import {
} from "../runtime.js";
import { AgentConfig } from "../config.js";
import { LivenessMonitor } from "../liveness.js";
import type { GuardrailDef } from "../types.js";
import { TokenResource } from "../../open-api/generated";

const mockedGenerateToken = TokenResource.generateToken as jest.MockedFunction<
Expand Down Expand Up @@ -884,6 +885,70 @@ describe("AgentRuntime", () => {
}
});
});

describe("_registerGuardrailWorker (custom guardrail on_fail contract)", () => {
const baseGDef: GuardrailDef = {
name: "fact_checker",
position: "output",
onFail: "retry",
guardrailType: "custom",
taskName: "fact_checker",
func: undefined,
};

async function registerAndInvoke(
gDef: GuardrailDef,
inputData: Record<string, unknown>,
): Promise<Record<string, unknown>> {
const runtime = new AgentRuntime();
let handler: ((inputData: unknown) => Promise<Record<string, unknown>>) | undefined;
jest
.spyOn((runtime as any).workerManager, "addWorker")
.mockImplementation((_taskName: unknown, fn: unknown) => {
handler = fn as typeof handler;
});

await (runtime as any)._registerGuardrailWorker(gDef);
return handler!(inputData);
}

it("reports on_fail as 'pass' when the guardrail passes, even though onFail is 'retry'", async () => {
const result = await registerAndInvoke(
{ ...baseGDef, func: () => ({ passed: true }) },
{ content: "clean content" },
);

expect(result).toMatchObject({ passed: true, on_fail: "pass", should_continue: true });
});

it("reports on_fail as the configured value when the guardrail fails", async () => {
const result = await registerAndInvoke(
{ ...baseGDef, func: () => ({ passed: false, message: "Unverifiable claims: always" }) },
{ content: "This always works." },
);

expect(result).toMatchObject({
passed: false,
on_fail: "retry",
should_continue: false,
message: "Unverifiable claims: always",
});
});

it("reports on_fail as the configured value when the guardrail function throws", async () => {
const result = await registerAndInvoke(
{
...baseGDef,
func: () => {
throw new Error("boom");
},
},
{ content: "anything" },
);

expect(result).toMatchObject({ passed: false, on_fail: "retry", should_continue: false });
});
});
});

// ── Singleton functions ─────────────────────────────────
Expand Down
7 changes: 4 additions & 3 deletions src/agents/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,13 +1092,14 @@ export class AgentRuntime {
const content = typeof raw === "object" ? JSON.stringify(raw) : String(raw);
try {
const result = await fn(content);
const passed = result.passed ?? true;
return {
passed: result.passed ?? true,
passed,
message: result.message ?? "",
on_fail: gDef.onFail ?? "raise",
on_fail: passed ? "pass" : (gDef.onFail ?? "raise"),
fixed_output: result.fixedOutput,
guardrail_name: gDef.name,
should_continue: result.passed ?? true,
should_continue: passed,
};
} catch (err) {
return {
Expand Down
Loading