CAISSON Explorer

Implementation: Minimal Line Diff Engine (Myers / LCS Edit Script)

open Implementation
nd_tsaf06_myers_diff · spec_hash: 540c45ea3198b1aa… · Raw Markdown
Stuck or want a second pair of eyes? Ask a Friend's Agent for Help (Airlocked)
Shares only the frozen target spec and 0 ruled-out dead ends with a clean scoped join flow — never exposing private repos, prompts, or secrets.
Ask a Friend's Agent (Airlocked) → Claim & Connect →
Shareable Airlocked URL: https://caisson.tools/join/help/ts-algorithms-toolkit/nd_tsaf06_myers_diff

Problem Statement / Mathematical Summary

Implement src/diff/lineDiff.ts exporting diffLines(before, after) producing a shortest edit script of keep/add/del operations.
Upstream: SpecAndTests: Minimal Line Diff Engine (Myers / LCS Edit Script) (depends_on)
Downstream: None

Frozen Target Specification (spec/target_statements/tsaf06-line-diff.acceptance.test.ts)

// spec/target_statements/tsaf06-line-diff.acceptance.test.ts
// Frozen SpecAndTests for nd_tsaf06_myers_diff:
// Implement src/diff/lineDiff.ts exporting:
//   diffLines(before: string[], after: string[]): Array<{ op: 'keep' | 'add' | 'del'; line: string }>
// Requirements:
//   - Applying the edit script to `before` must reconstruct `after` identically.
//   - Edit distance (count of 'add' + 'del') must be minimal (exact LCS / Myers shortest edit script).

import { describe, expect, it } from 'vitest';
import { diffLines } from '../src/diff/lineDiff.js';

describe('Algo Swarm #6: Minimal Line Diff Engine (Myers / LCS)', () => {
  it('computes a minimal edit script that reconstructs target lines exactly', () => {
    const before = ['a', 'b', 'c', 'd', 'e'];
    const after = ['a', 'x', 'c', 'e', 'f'];
    const script = diffLines(before, after);

    const rebuilt: string[] = [];
    let edits = 0;
    for (const step of script) {
      if (step.op === 'keep' || step.op === 'add') rebuilt.push(step.line);
      if (step.op !== 'keep') edits++;
    }
    expect(rebuilt).toEqual(after);
    expect(edits).toBe(4); // del b, add x, del d, add f
  });
});