Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

4.0 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { writeWindsurfContextFile } from '../../src/services/integrations/WindsurfHooksInstaller.js';

/**
 * Windsurf loads `.windsurf/rules/claude-mem-context.md` into every prompt, and
 * the writer caps it at 6000 characters. The cap is counted in UTF-16 code
 * units, so a context that is long enough to truncate could put the cut between
 * the two halves of an emoji: the old `slice(0, 5950)` kept the lone high half,
 * and writing that string as UTF-8 stored a stray U+FFFD in the rules file.
 *
 * Like every other auto-loaded writer, the context now goes through
 * `toBmpSafe` (#2787), so no character outside the BMP reaches the file at all,
 * and the cut itself is made on a code point boundary.
 *
 * Pads the context with 0 and 1 leading ASCII characters so the cut would land
 * inside a pair for one of them whatever the fixed header's parity is.
 */
describe('Windsurf rules file truncation', () => {
  let workspace: string;

  beforeEach(() => {
    workspace = mkdtempSync(path.join(tmpdir(), 'windsurf-context-'));
  });

  afterEach(() => {
    rmSync(workspace, { recursive: true, force: true });
  });

  const rulesFile = () => path.join(workspace, '.windsurf', 'rules', 'claude-mem-context.md');

  /** True when a high surrogate has no low half after it, or a low one has no high half before it. */
  const hasLoneSurrogate = (text: string): boolean => {
    for (let index = 0; index < text.length; index++) {
      const unit = text.charCodeAt(index);
      if (unit >= 0xdc00 && unit <= 0xdfff) return true;
      if (unit < 0xd800 || unit > 0xdbff) continue;
      const next = text.charCodeAt(index + 1);
      if (!(next >= 0xdc00 && next <= 0xdfff)) return true;
      index++;
    }
    return false;
  };

  const hasAstralCodePoint = (text: string): boolean =>
    Array.from(text).some((char) => (char.codePointAt(0) ?? 0) > 0xffff);

  it('never leaves half an emoji at the cut', () => {
    for (const pad of ['', 'a']) {
      writeWindsurfContextFile(workspace, pad + '😀'.repeat(8000));

      const text = readFileSync(rulesFile(), 'utf-8');
      expect(text.length).toBeLessThanOrEqual(6000);
      expect(text).toContain('[Truncated');
      // A lone high half reaches the file as U+FFFD: the encoder has no code
      // point for it, so the emoji the observation ended with becomes "�".
      expect(text).not.toContain('\uFFFD');
      expect(hasLoneSurrogate(text)).toBe(false);
      expect(hasAstralCodePoint(text)).toBe(false);
      // The emoji reaches the file as toBmpSafe's neutral bullet.
      expect(text).toContain('•'.repeat(100));
      // The file still ends with the whole note, not a half-written one.
      expect(text.endsWith('*[Truncated — use MCP search for full history]*\n')).toBe(true);
    }
  });

  it('writes the type markers as BMP glyphs, like the other auto-loaded writers', () => {
    writeWindsurfContextFile(workspace, '🔴 Fixed X\n🟣 Added Y');

    const text = readFileSync(rulesFile(), 'utf-8');
    expect(text).toContain('● Fixed X');
    expect(text).toContain('◆ Added Y');
    expect(hasAstralCodePoint(text)).toBe(false);
  });

  it('keeps a character that fits the last slot', () => {
    // 'é' is a single code unit, so a 20-character budget keeps all 20 — the
    // code-point step must not shave a character that never straddles the cut.
    writeWindsurfContextFile(workspace, 'é'.repeat(7000));

    const text = readFileSync(rulesFile(), 'utf-8');
    expect(text.length).toBe(6000);
    expect(hasLoneSurrogate(text)).toBe(false);
  });

  it('leaves a context under the cap untouched, with no note', () => {
    writeWindsurfContextFile(workspace, 'short context');

    const text = readFileSync(rulesFile(), 'utf-8');
    expect(text).toContain('short context');
    expect(text).not.toContain('[Truncated');
  });
});