File size: 3,895 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it } from "node:test";
import assert from "node:assert/strict";

import {
  applyContextEditingToBody,
  CLEAR_TOOL_USES_STRATEGY,
  CONTEXT_EDITING_DEFAULT_TRIGGER_TOKENS,
  CONTEXT_EDITING_DEFAULT_KEEP_TOOL_USES,
} from "../../../open-sse/config/contextEditing.ts";

const CLEAR_THINKING_STRATEGY = "clear_thinking_20251015";

describe("applyContextEditingToBody", () => {
  it("does nothing when disabled", () => {
    const body: Record<string, unknown> = { model: "claude-opus-4-8" };
    applyContextEditingToBody(body, { enabled: false });
    assert.equal(body.context_management, undefined);
  });

  it("is a no-op for null/non-object bodies", () => {
    // Should not throw.
    applyContextEditingToBody(null, { enabled: true });
    applyContextEditingToBody(undefined, { enabled: true });
    assert.ok(true);
  });

  it("adds the clear_tool_uses edit with default trigger/keep on an empty body", () => {
    const body: Record<string, unknown> = { model: "claude-opus-4-8" };
    applyContextEditingToBody(body, { enabled: true });

    const cm = body.context_management as Record<string, unknown>;
    assert.ok(cm, "context_management should be set");
    const edits = cm.edits as Array<Record<string, unknown>>;
    assert.equal(edits.length, 1);
    assert.deepEqual(edits[0], {
      type: CLEAR_TOOL_USES_STRATEGY,
      trigger: { type: "input_tokens", value: CONTEXT_EDITING_DEFAULT_TRIGGER_TOKENS },
      keep: { type: "tool_uses", value: CONTEXT_EDITING_DEFAULT_KEEP_TOOL_USES },
    });
  });

  it("composes with an existing clear_thinking edit, keeping thinking FIRST", () => {
    const body: Record<string, unknown> = {
      model: "claude-opus-4-8",
      context_management: {
        edits: [{ type: CLEAR_THINKING_STRATEGY, keep: "all" }],
      },
    };
    applyContextEditingToBody(body, { enabled: true });

    const edits = (body.context_management as Record<string, unknown>).edits as Array<
      Record<string, unknown>
    >;
    assert.equal(edits.length, 2);
    assert.equal(edits[0].type, CLEAR_THINKING_STRATEGY, "clear_thinking must be first");
    assert.equal(edits[1].type, CLEAR_TOOL_USES_STRATEGY);
  });

  it("is idempotent — calling twice does not duplicate the tool-use edit", () => {
    const body: Record<string, unknown> = { model: "claude-opus-4-8" };
    applyContextEditingToBody(body, { enabled: true });
    applyContextEditingToBody(body, { enabled: true });

    const edits = (body.context_management as Record<string, unknown>).edits as Array<
      Record<string, unknown>
    >;
    assert.equal(
      edits.filter((e) => e.type === CLEAR_TOOL_USES_STRATEGY).length,
      1,
      "only one clear_tool_uses edit should exist"
    );
  });

  it("does not duplicate when a tool-use edit already exists (preserves caller's edit)", () => {
    const preset = {
      type: CLEAR_TOOL_USES_STRATEGY,
      trigger: { type: "input_tokens", value: 50000 },
      keep: { type: "tool_uses", value: 1 },
    };
    const body: Record<string, unknown> = {
      model: "claude-opus-4-8",
      context_management: { edits: [preset] },
    };
    applyContextEditingToBody(body, { enabled: true });

    const edits = (body.context_management as Record<string, unknown>).edits as Array<
      Record<string, unknown>
    >;
    assert.equal(edits.length, 1);
    assert.deepEqual(edits[0], preset, "existing tool-use edit must be left untouched");
  });

  it("preserves unrelated context_management properties", () => {
    const body: Record<string, unknown> = {
      model: "claude-opus-4-8",
      context_management: { edits: [], some_future_flag: true },
    };
    applyContextEditingToBody(body, { enabled: true });

    const cm = body.context_management as Record<string, unknown>;
    assert.equal(cm.some_future_flag, true);
    assert.equal((cm.edits as unknown[]).length, 1);
  });
});