File size: 4,635 Bytes
a070805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { act, renderHook } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import useMetricsStore from "#/stores/metrics-store";
import { useEventStore } from "#/stores/use-event-store";
import type { OpenHandsEvent } from "#/types/agent-server/core";
import {
  buildContextCompactionResult,
  useAwaitContextCompaction,
} from "./use-await-context-compaction";

function condensationEvent(id: string): OpenHandsEvent {
  return {
    id,
    timestamp: new Date().toISOString(),
    source: "environment",
    kind: "Condensation",
    forgotten_event_ids: ["a", "b"],
  } as OpenHandsEvent;
}

describe("buildContextCompactionResult", () => {
  it("computes tokens saved without going negative", () => {
    expect(buildContextCompactionResult(1000, 400, "compacted")).toEqual({
      beforeToken: 1000,
      afterToken: 400,
      savedToken: 600,
      outcome: "compacted",
    });
    expect(buildContextCompactionResult(400, 500, "no_change").savedToken).toBe(
      0,
    );
  });
});

describe("useAwaitContextCompaction", () => {
  beforeEach(() => {
    vi.useFakeTimers();
    useEventStore.getState().clearEvents();
    useMetricsStore.getState().setMetrics({
      cost: null,
      max_budget_per_task: null,
      usage: {
        prompt_tokens: 0,
        completion_tokens: 0,
        cache_read_tokens: 0,
        cache_write_tokens: 0,
        context_window: 100_000,
        per_turn_token: 10_000,
      },
    });
  });

  afterEach(() => {
    vi.useRealTimers();
    useEventStore.getState().clearEvents();
  });

  it("reports tokens freed once condensation lands and metrics drop", () => {
    const onComplete = vi.fn();

    renderHook(() =>
      useAwaitContextCompaction({
        beforeToken: 10_000,
        onComplete,
      }),
    );

    act(() => {
      useEventStore.getState().addEvent(condensationEvent("cond-1"));
      useMetricsStore.getState().setMetrics({
        cost: null,
        max_budget_per_task: null,
        usage: {
          prompt_tokens: 0,
          completion_tokens: 0,
          cache_read_tokens: 0,
          cache_write_tokens: 0,
          context_window: 100_000,
          per_turn_token: 4_000,
        },
      });
    });

    expect(onComplete).toHaveBeenCalledWith({
      beforeToken: 10_000,
      afterToken: 4_000,
      savedToken: 6_000,
      outcome: "compacted",
    });
  });

  it("finishes after the settle window when metrics do not drop", () => {
    const onComplete = vi.fn();

    renderHook(() =>
      useAwaitContextCompaction({
        beforeToken: 10_000,
        onComplete,
      }),
    );

    act(() => {
      useEventStore.getState().addEvent(condensationEvent("cond-2"));
    });
    expect(onComplete).not.toHaveBeenCalled();

    act(() => {
      vi.advanceTimersByTime(2_500);
    });

    expect(onComplete).toHaveBeenCalledWith({
      beforeToken: 10_000,
      afterToken: 10_000,
      savedToken: 0,
      outcome: "no_change",
    });
  });

  it("reports timeout when no condensation event arrives in time", () => {
    const onComplete = vi.fn();

    renderHook(() =>
      useAwaitContextCompaction({
        beforeToken: 10_000,
        onComplete,
      }),
    );

    act(() => {
      vi.advanceTimersByTime(90_000);
    });

    expect(onComplete).toHaveBeenCalledWith({
      beforeToken: 10_000,
      afterToken: 10_000,
      savedToken: 0,
      outcome: "timeout",
    });
  });

  it("detects a condensation that landed before the post-ack effect started", () => {
    // The condense POST can return only after the server already emitted its
    // Condensation event. The pre-request baseline (captured before the POST
    // fired) must not contain it, so it still counts as new.
    const baseline = new Set(useEventStore.getState().eventIds);
    act(() => {
      useEventStore.getState().addEvent(condensationEvent("cond-early"));
      useMetricsStore.getState().setMetrics({
        cost: null,
        max_budget_per_task: null,
        usage: {
          prompt_tokens: 0,
          completion_tokens: 0,
          cache_read_tokens: 0,
          cache_write_tokens: 0,
          context_window: 100_000,
          per_turn_token: 4_000,
        },
      });
    });

    const onComplete = vi.fn();
    renderHook(() =>
      useAwaitContextCompaction({
        beforeToken: 10_000,
        baselineEventIds: baseline,
        onComplete,
      }),
    );

    expect(onComplete).toHaveBeenCalledWith({
      beforeToken: 10_000,
      afterToken: 4_000,
      savedToken: 6_000,
      outcome: "compacted",
    });
  });
});