File size: 2,665 Bytes
c4ae742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { describe, expect, it } from "vitest";

import {
  buildIterationMemoryPrompt,
  createIterationMemory,
  optimizeIterationMemory,
} from "../queue/iteration-memory.js";

describe("iteration memory optimizer", () => {
  it("accumulates repeated weak dimensions into a compact memory prompt", () => {
    const first = optimizeIterationMemory(
      createIterationMemory(),
      {
        total_score: 8,
        max_score: 12,
        verdict: "NEEDS_REVISION",
        dimensions: {
          task_realism: { score: 1, explanation: "The task reads like a procedural checklist." },
          trajectory_task_alignment: { score: 0, explanation: "The task asks for an action missing from the trajectory." },
        },
      },
      1,
      "first feedback",
    );
    const second = optimizeIterationMemory(
      first,
      {
        total_score: 9,
        max_score: 12,
        verdict: "NEEDS_REVISION",
        dimensions: {
          task_realism: { score: 2, explanation: "Now reads naturally." },
          trajectory_task_alignment: { score: 0, explanation: "Still contains the unsupported action." },
        },
      },
      2,
      "second feedback",
    );

    expect(second.bestScore).toBe(9);
    expect(second.resolvedDimensions).toContain("task_realism");
    expect(second.activeFindings).toHaveLength(1);
    expect(second.activeFindings[0]).toMatchObject({
      dimension: "trajectory_task_alignment",
      firstSeenIteration: 1,
      lastSeenIteration: 2,
      hits: 2,
    });

    const prompt = buildIterationMemoryPrompt(second);
    expect(prompt).toContain("ITERATION MEMORY");
    expect(prompt).toContain("trajectory_task_alignment");
    expect(prompt).toContain("repeated 2x");
    expect(prompt).toContain("RESOLVED DIMENSIONS: task_realism");
    expect(prompt).toContain("second feedback");
  });

  it("removes active findings once the dimension is fixed", () => {
    const first = optimizeIterationMemory(
      createIterationMemory(),
      {
        total_score: 7,
        dimensions: {
          rubric_quality: { score: 0, explanation: "Rubric is not observable." },
        },
      },
      1,
      "needs observable rubric",
    );
    const fixed = optimizeIterationMemory(
      first,
      {
        total_score: 11,
        dimensions: {
          rubric_quality: { score: 2, explanation: "Rubric is now observable." },
        },
      },
      2,
      "fixed",
    );

    expect(fixed.activeFindings).toHaveLength(0);
    expect(fixed.resolvedDimensions).toEqual(["rubric_quality"]);
    expect(buildIterationMemoryPrompt(fixed)).toContain("No active weak dimensions remain");
  });
});