AkurAI Build
Menu

popagent

public

Latest change 2d4fbaa6bc85383309531c6c35a15ea4c23a0bc0 - Fix autonomous reflection over 9Router streaming 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,
  selfUpdateEnabled: true,
  selfUpdateCron: "0 3 * * *",
  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: "popagent",
    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: "popagent" as const,
            strategy: "evidence-first" as const,
            rationale: "The result must cite evidence.",
            evidenceIds: [evidenceId],
          }],
        }),
      };
    },
  };

  expect(await reflectWithAgent(reflector, [signal], settings)).toEqual({
    proposals: [{
      agentId: "popagent",
      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);
});