Skip to content

The platform for continuously improving AI agents.

4.3 KB
Raw Blame
// covers: console-logic
import { expect, it } from "vitest";

import {
  ExperimentMonitorAgentFromJSON,
  TrainingAgentFromJSON,
  TrainingCampaignFromJSON,
} from "@/openapi";
import type { ExperimentRun } from "./document";
import { campaignBindings, campaignEvents, campaignWorker, operatorWorkers } from "./monitor-data";

const campaign = (state: string, trials: object[], selected: string | null = null) =>
  TrainingCampaignFromJSON({
    completed_at: state === "running" ? null : "2026-12-03T13:00:00Z",
    created_at: "2026-12-03T11:00:00Z",
    id: "campaign",
    max_trials: 3,
    selected_job: selected,
    state,
    summary: state === "running" ? "" : "Run 2 kept: lowest development loss 0.81",
    trials,
  });
const trial = (index: number, status: string, extra: object = {}) => ({
  best_development_loss: null,
  index,
  job: `job-${index}`,
  reason: index === 1 ? "First run" : "Lower the learning rate after a rising loss",
  recipe: index === 1 ? {} : { epochs: 2, learning_rate: 0.00005 },
  status,
  ...extra,
});

it("shows waiting operators and training runs without claiming that a model is working", () => {
  const agent = ExperimentMonitorAgentFromJSON({
    active: true,
    activity: { reason: "The flow is complete" },
    assignment: "owner",
    id: "agent",
    role: "operator",
    state: "waiting",
    tool: "wait",
    updated_at: "2026-12-03T12:00:00Z",
  });
  expect(operatorWorkers([agent])[0]).toMatchObject({
    detail: "The flow is complete",
    state: "waiting",
  });
  expect(operatorWorkers([{ ...agent, active: false }])).toEqual([]);
  const binding = { id: "campaign", node: "train", run: "run", title: "Train" };
  expect(campaignWorker(binding, campaign("running", [trial(1, "succeeded")])).state).toBe(
    "waiting"
  );
  expect(campaignWorker(binding, campaign("running", [trial(1, "running")]))).toMatchObject({
    activity: "Training run 1 of up to 3",
    state: "working",
  });
  expect(campaignWorker(binding, campaign("completed", [], "job-2"))).toMatchObject({
    activity: "Best run kept",
    state: "done",
  });
  expect(campaignWorker(binding, campaign("cancelled", [])).state).toBe("failed");
});

it("finds a newly admitted campaign before it has a training job and preserves its run lineage", () => {
  const run = {
    document: { nodes: [{ id: "train", title: "Candidate" }] },
    id: "run",
    nodes: [
      {
        node: "train",
        outputs: {},
        result: { campaign: { id: "campaign", trials: [] } },
        tool: "training",
      },
    ],
  } as unknown as ExperimentRun;
  expect(campaignBindings(run)).toEqual([
    { id: "campaign", node: "train", run: "run", title: "Candidate" },
  ]);
});

it("keeps the agent's requested change distinct from its runs and the kept result", () => {
  const binding = { id: "campaign", node: "train", run: "run", title: "Train" };
  const agent = TrainingAgentFromJSON({
    checkpoints: [],
    clip_norm: null,
    controls: [
      {
        arguments: { value: 0.0001 },
        author: "agent",
        created_at: "2026-12-03T12:30:00Z",
        id: "control",
        operation: "set_learning_rate",
        reason: "Comparable loss checks regressed",
        state: "requested",
      },
    ],
    learning_rate: null,
    limits: null,
    reviews: [],
    unavailable: null,
  });
  const entries = campaignEvents(
    binding,
    campaign(
      "completed",
      [trial(1, "succeeded"), trial(2, "succeeded", { best_development_loss: 0.81 })],
      "job-2"
    ),
    [
      { createdAt: new Date("2026-12-03T11:00:00Z"), job: "job-1" },
      { agent, createdAt: new Date("2026-12-03T12:00:00Z"), job: "job-2" },
    ]
  );
  expect(entries.map((entry) => entry.title)).toEqual([
    "Start training run 1 on Train",
    "Start training run 2 on Train",
    "Change learning rate · run 2 on Train",
    "Kept the best run on Train",
  ]);
  expect(entries[1]).toMatchObject({
    actor: "campaign",
    details: ["Learning rate 0.00005", "2 epochs"],
    evidence: ["Best development loss 0.81"],
    node: "train",
    reason: "Lower the learning rate after a rising loss",
    run: "run",
  });
  expect(entries[2]).toMatchObject({
    reason: "Comparable loss checks regressed",
    state: "requested",
  });
  expect(campaignEvents(binding, campaign("running", [trial(1, "running")]), [])).toEqual([]);
});