AkurAI Build
Menu

popagent

public

Latest change 682410a285bb0a73662cc4650c92c31c918e1cc9 - Add idle autonomous improvement workflow by Ólafur Búi Ólafsson

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,
  selfUpdateEnabled: true,
  selfUpdateCron: "0 3 * * *",
  idleImprovementEnabled: false,
  idleDeploymentEnabled: false,
  idleGraceMs: 600_000,
  idleWorkspaceIds: [],
  lastUserActivityAt: new Date(0).toISOString(),
  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],
          }],
        }),
      };
    },
  };

  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],
    }],
  });
  expect(streamed).toBe(true);
});