AkurAI Build
Menu

popagent

public

Latest change c5cc989445aeac1b3fd80068df03e23002a15a47 - Let evolution retain bounded durable facts by AkurAI Build

import { expect, test } from "bun:test";
import type { AutonomySettings, EvolutionSignal } from "./api-types";
import { reflectWithAgent } from "./evolution-runtime";

const settings: AutonomySettings = {
  enabled: true,
  reflectionIntervalMs: 60_000,
  batchSize: 1,
  maxAttempts: 3,
  autoApplyStrategies: true,
  autoCreateSkills: true,
  autoRetainFacts: true,
  selfUpdateEnabled: true,
  selfUpdateCron: "0 3 * * *",
  idleImprovementEnabled: false,
  idleDeploymentEnabled: false,
  idleWorkspaceIds: [],
  updatedAt: new Date().toISOString(),
};

test("reflection consumes the streaming structured object required by 9Router", async () => {
  const evidenceId = crypto.randomUUID();
  const signal: EvolutionSignal = {
    id: evidenceId,
    sessionId: "reflection-stream-contract",
    turnId: crypto.randomUUID(),
    traceId: crypto.randomUUID().replaceAll("-", ""),
    agentId: "orchistrator",
    workspaceId: "default",
    kind: "turn-success",
    summary: "Goal: Cite evidence.\nClassification: success\nOutcome: Evidence cited.",
    status: "pending",
    attempts: 0,
    nextAttemptAt: new Date().toISOString(),
    processedAt: null,
    error: null,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  };
  let streamed = false;
  const reflector = {
    stream: async (_prompt: string, options: { structuredOutput?: unknown }) => {
      streamed = true;
      expect(options.structuredOutput).toBeDefined();
      return {
        object: Promise.resolve({
          proposals: [
            {
              agentId: "orchistrator" as const,
              strategy: "evidence-first" as const,
              rationale: "The result must cite evidence.",
              evidenceIds: [evidenceId],
            },
            {
              agentId: "orchistrator" as const,
              strategy: "retain-fact" as const,
              factKey: "verified-command",
              content: "Use bun run verify for this repository.",
              rationale: "The verified repository command should be recalled.",
              evidenceIds: [evidenceId],
            },
          ],
        }),
      };
    },
  };

  expect(await reflectWithAgent(reflector, [signal], settings)).toEqual({
    proposals: [
      {
        agentId: "orchistrator",
        targetType: "skill",
        targetKey: "learned-evidence-first",
        content: "Before declaring completion or readiness, verify the observable result and report the exact evidence.",
        rationale: "The result must cite evidence.",
        evidenceIds: [evidenceId],
      },
      {
        agentId: "orchistrator",
        targetType: "fact",
        targetKey: "verified-command",
        content: "Use bun run verify for this repository.",
        rationale: "The verified repository command should be recalled.",
        evidenceIds: [evidenceId],
      },
    ],
  });
  expect(streamed).toBe(true);
});