AkurAI Build
Menu

popagent

public

Latest change e000da68aa54f8d6dd2d3ed89e8eb089be63cf93 - Restore continue:false in createFinalResponseGuard now that the wire guard sanitizes it 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",
  modelSource: "configured",
  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: "orchistrator",
  agentName: "orchistrator",
  messages: [],
  ...overrides,
});

describe("autonomous execution policy", () => {
  test("reserves the last iteration for a guaranteed final response instead of exhausting the budget on tool calls", 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",
    })]);
  });

  test("redacts retired plaintext secret tools before managed hook dispatch", async () => {
    const marker = `credential-${crypto.randomUUID()}`;
    const events: HookEvent[] = [];
    const runtime = new HookRuntime({
      config: {
        version: 1,
        hooks: {
          PreToolUse: [{ id: "pre", type: "http", url: "https://hooks.example/pre" }],
          PostToolUse: [{ id: "post", type: "http", url: "https://hooks.example/post" }],
        },
      },
      transport: async (_handler, event) => {
        events.push(event);
        return { schemaVersion: 1, outcome: "pass" };
      },
    });
    const requestContext = createAgentRequestContext({
      resourceId: "popagent-user",
      sessionId: "session-secret",
      turnId: "turn-secret",
      model: "auto/fast",
      hookRuntime: runtime,
      runtimeSettings: settings,
    });
    const options = await specialistExecutionOptions({
      requestContext: requestContext as RequestContext,
    });

    await options.hooks?.beforeToolCall?.({
      toolName: "storeSecret",
      input: { name: "login/password", value: marker },
      context: { agent: { toolCallId: "tool-store" } },
    });
    await options.hooks?.afterToolCall?.({
      toolName: "recallSecret",
      input: { name: "login/password" },
      output: { name: "login/password", value: marker },
      context: { agent: { toolCallId: "tool-recall" } },
    });

    expect(JSON.stringify(events)).not.toContain(marker);
    expect(events.map((event) => event.detail)).toEqual([
      { tool: { name: "storeSecret", input: "[redacted]" } },
      { tool: { name: "recallSecret", input: "[redacted]", output: "[redacted]" } },
    ]);
  });
});