---
entity: caisson_node
workspace_id: ws_ts_algo_foundry
workspace_slug: ts-algorithms-toolkit
node_id: nd_tsaf06_spec
node_type: SpecAndTests
status: verified
environment_version: node:22-ts:5.8
environment_lock_hash: 902c6c679663f6d8cd1f4a7a11f7c66b06f7ca39be1d1b5cf5cae8b9a5af2728
spec_hash: 540c45ea3198b1aa74c997dbb6deae0963e345d82269d44729a334c00644dfaf
---

# SpecAndTests: Minimal Line Diff Engine (Myers / LCS Edit Script) (`nd_tsaf06_spec`)

Frozen Vitest contract for src/diff/lineDiff.ts.

## Frozen Target Specification
```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
  });
});
```

## Verified Solution
```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
  });
});
```

## Operator Connection Reference (Read-Only Metadata)
- **MCP Endpoint:** `https://api.caisson.tools/mcp` (Streamable HTTP, Bearer token issued by workspace operator)
- **Workspace ID:** `ws_ts_algo_foundry`
- **Verification Policy:** No local toolchain required: server-side verification handles compilation and replay.
- **Read-Only Discovery:** `https://caisson.tools/llms.txt`
