Menu
popagent
publicLatest change 6edc7e928c57dab23c0973ed3f7ee70b3ebb72d5 - Initial popagent baseline by AkurAI Build
import { describe, expect, test } from "bun:test";
import { AGENT_CONTEXT_KEYS, agentContextValue, createAgentRequestContext } from "./agent-context";
import type { RequestContext } from "@mastra/core/request-context";
import { createWorkspaceTools } from "@mastra/core/workspace";
import { agent } from "./agent";
import { resolveModel } from "./models";
import { agentSettings } from "./agent-settings";
import { agentRuntimeSettings } from "./agent-runtime-settings";
import { agentSkills } from "./agent-skills";
describe("agent", () => {
test("uses the configured default model", async () => {
const runtime = await agentRuntimeSettings.get();
const model = await agent.getModel();
expect(model.modelId).toBe(runtime.defaultModel);
});
test("exposes stable runtime names for hook matching", async () => {
expect(Object.keys(await agent.listTools())).toEqual(expect.arrayContaining([
"getTime",
"remember",
"storeSecret",
"recallSecret",
"updateSecret",
"webSearch",
]));
});
test("constrains specialist workspace capabilities by role", async () => {
const delegated = await agent.listAgents();
expect(Object.keys(delegated)).toEqual(["researcher", "implementer", "reviewer"]);
const specialists = {
researcher: delegated.researcher! as unknown as typeof agent,
implementer: delegated.implementer! as unknown as typeof agent,
reviewer: delegated.reviewer! as unknown as typeof agent,
};
const supervisorWorkspace = await agent.getWorkspace();
expect(await specialists.implementer.getWorkspace()).toBe(supervisorWorkspace);
for (const id of ["researcher", "reviewer"] as const) {
const workspace = await specialists[id].getWorkspace();
expect(workspace).not.toBe(supervisorWorkspace);
const tools = Object.keys(await createWorkspaceTools(workspace!));
expect(tools).toEqual(expect.arrayContaining([
"mastra_workspace_read_file",
"mastra_workspace_list_files",
"mastra_workspace_grep",
]));
expect(tools).not.toEqual(expect.arrayContaining([
"mastra_workspace_write_file",
"mastra_workspace_edit_file",
"mastra_workspace_delete",
"mastra_workspace_execute_command",
]));
}
const implementerWorkspace = await specialists.implementer.getWorkspace();
const implementerTools = Object.keys(await createWorkspaceTools(implementerWorkspace!));
expect(implementerTools).toEqual(expect.arrayContaining([
"mastra_workspace_write_file",
"mastra_workspace_edit_file",
"mastra_workspace_execute_command",
]));
const runtime = await agentRuntimeSettings.get();
for (const specialist of Object.values(specialists)) {
expect((await specialist.getDefaultOptions()).maxSteps).toBe(runtime.specialistMaxSteps);
expect((await specialist.getDefaultOptions()).toolCallConcurrency)
.toEqual({ limit: runtime.toolConcurrency, strategy: "called" });
}
});
test("bounds supervisor tool loops from persisted runtime settings", async () => {
const runtime = await agentRuntimeSettings.get();
expect((await agent.getDefaultOptions()).maxSteps).toBe(runtime.supervisorMaxSteps);
expect((await agent.getDefaultOptions()).toolCallConcurrency)
.toEqual({ limit: runtime.toolConcurrency, strategy: "called" });
});
test("routes the selected model into specialist runs", async () => {
const researcher = (await agent.listAgents()).researcher!;
const requestContext = createAgentRequestContext({
resourceId: "popagent-user",
sessionId: "session-1",
turnId: "turn-1",
model: "cc/claude-sonnet-5",
});
expect(agentContextValue(requestContext, AGENT_CONTEXT_KEYS.model)).toBe("cc/claude-sonnet-5");
const model = await (researcher as unknown as typeof agent).getModel({
requestContext: requestContext as RequestContext,
});
expect(model.modelId).toBe("cc/claude-sonnet-5");
});
test("applies persisted specialist capabilities on the next resolution", async () => {
const implementer = (await agent.listAgents()).implementer! as unknown as typeof agent;
const original = await agentSettings.get("implementer");
if (!original) throw new Error("implementer settings missing");
try {
await agentSettings.update("implementer", {
...original,
workspaceAccess: "none",
browserAccess: "none",
delegationEnabled: false,
tools: ["getTime"],
});
expect(Object.keys(await implementer.listTools())).toContain("getTime");
expect(Object.keys(await implementer.listTools())).not.toContain("webSearch");
expect(await implementer.getWorkspace()).toBeUndefined();
expect((await agent.listAgents()).implementer).toBeUndefined();
} finally {
await agentSettings.update("implementer", original);
await agent.listAgents();
}
});
test("loads specialist instructions entirely from the database", async () => {
const researcher = (await agent.listAgents()).researcher!;
const original = await agentSettings.get("researcher");
if (!original) throw new Error("researcher settings missing");
const custom = `# Role: Database Researcher\n\nPrefer primary sources ${crypto.randomUUID()}`;
try {
await agentSettings.update("researcher", { ...original, instructions: custom });
expect(await researcher.getInstructions()).toBe(
`Display identity: ${original.name}.\n\n${custom}`,
);
} finally {
await agentSettings.update("researcher", original);
}
});
test("applies persisted specialist display identity and delegation description at runtime", async () => {
const researcher = (await agent.listAgents()).researcher!;
const original = await agentSettings.get("researcher");
if (!original) throw new Error("researcher settings missing");
const customName = `Source Analyst ${crypto.randomUUID().slice(0, 8)}`;
const customDescription = `Delegated source analysis ${crypto.randomUUID().slice(0, 8)}`;
try {
await agentSettings.update("researcher", {
...original,
name: customName,
description: customDescription,
});
expect(await researcher.getInstructions()).toContain(`Display identity: ${customName}.`);
expect(researcher.getDescription()).toBe(customDescription);
} finally {
await agentSettings.update("researcher", original);
await agent.listAgents();
}
});
test("resolves persisted skills for specialist agents", async () => {
const researcher = (await agent.listAgents()).researcher!;
expect((await (researcher as unknown as typeof agent).listSkills()).map((item) => item.name))
.toContain("codebase-investigation");
const name = `research-sources-${crypto.randomUUID().slice(0, 8)}`;
const skill = await agentSkills.create("researcher", {
name,
description: "Use for source-sensitive research.",
instructions: "Prefer primary sources and report URLs.",
references: {},
enabled: true,
userInvocable: false,
});
try {
expect((await (researcher as unknown as typeof agent).listSkills()).map((item) => item.name)).toContain(name);
} finally {
await agentSkills.delete("researcher", skill.id);
}
});
test("loads supervisor instructions and provenance from the database", async () => {
const stored = await agentSettings.get("popagent");
expect(stored?.sourceUrls).toEqual(
expect.arrayContaining([expect.stringContaining("engineering-multi-agent-systems-architect.md")]),
);
expect(await agent.getInstructions()).toContain(stored!.instructions);
});
test("streams through 9router with the default model", async () => {
const out = await agent.stream("Reply with exactly the word: pong");
expect((await out.text).toLowerCase()).toContain("pong");
}, 60000);
test("answers with a per-call model override", async () => {
const out = await agent.stream("Reply with exactly the word: pong", {
model: resolveModel("auto/fast"),
});
expect((await out.text).toLowerCase()).toContain("pong");
}, 60000);
});