Menu
popagent
publicLatest change 6edc7e928c57dab23c0973ed3f7ee70b3ebb72d5 - Initial popagent baseline by AkurAI Build
import { describe, expect, test } from "bun:test";
import type { IterationCompleteContext } from "@mastra/core/agent";
import type { RequestContext } from "@mastra/core/request-context";
import { createAgentRequestContext } from "./agent-context";
import { createFinalResponseGuard, specialistExecutionOptions } from "./agent-autonomy";
import type { AgentRuntimeSettings } from "./api-types";
import { HookRuntime, type HookEvent } from "./hooks";
const settings: AgentRuntimeSettings = {
defaultModel: "test/default",
supervisorMaxSteps: 24,
specialistMaxSteps: 12,
toolConcurrency: 3,
delegationContextMessages: 12,
delegationResultCharacters: 16_000,
maxProcessorRetries: 3,
finalResponseFeedback: "Return the best complete response now",
delegationFailureFeedback: "Specialist {{agentId}} failed: {{error}}",
delegationResultTruncationMarker: "\n\n[Subagent result truncated]",
taskConcurrency: 1,
taskPollIntervalMs: 15_000,
taskTimeoutMs: 900_000,
taskStaleAfterMs: 86_400_000,
updatedAt: "",
};
const iteration = (overrides: Partial<IterationCompleteContext> = {}): IterationCompleteContext => ({
iteration: 1,
maxIterations: 24,
text: "",
toolCalls: [{ id: "tool-1", name: "webSearch", args: {} }],
toolResults: [],
isFinal: false,
finishReason: "tool-calls",
runId: "run-1",
agentId: "popagent",
agentName: "popagent",
messages: [],
...overrides,
});
describe("autonomous execution policy", () => {
test("reserves the last iteration for a final response", async () => {
const reserveFinalResponse = createFinalResponseGuard(settings);
expect(await reserveFinalResponse(iteration({ iteration: 22 }))).toBeUndefined();
expect(await reserveFinalResponse(iteration({ iteration: 23 }))).toEqual({
continue: false,
feedback: settings.finalResponseFeedback,
});
expect(await reserveFinalResponse(iteration({ iteration: 23, toolCalls: [] }))).toBeUndefined();
});
test("applies managed tool hooks inside delegated specialist runs", async () => {
const events: HookEvent[] = [];
const runtime = new HookRuntime({
config: {
version: 1,
hooks: {
PreToolUse: [{
id: "specialist-policy",
type: "http",
url: "https://hooks.example/specialist-policy",
matcher: ["webSearch"],
}],
},
},
transport: async (_handler, event) => {
events.push(event);
return { schemaVersion: 1, outcome: "pass" };
},
});
const requestContext = createAgentRequestContext({
resourceId: "popagent-user",
sessionId: "session-1",
turnId: "turn-1",
model: "auto/fast",
hookRuntime: runtime,
runtimeSettings: settings,
});
const options = await specialistExecutionOptions({
requestContext: requestContext as RequestContext,
});
await options.hooks?.beforeToolCall?.({
toolName: "webSearch",
input: { query: "Mastra delegation hooks" },
context: { agent: { toolCallId: "tool-1" } },
});
expect(events).toEqual([expect.objectContaining({
eventName: "PreToolUse",
sessionId: "session-1",
turnId: "turn-1",
model: "auto/fast",
toolCallId: "tool-1",
})]);
});
});