Skip to content

LSP Phase 6: edit-tool diagnostic integration - #30

Merged
yogthos merged 1 commit into
mainfrom
feature/lsp-phase-6-edit-integration
May 20, 2026
Merged

LSP Phase 6: edit-tool diagnostic integration#30
yogthos merged 1 commit into
mainfrom
feature/lsp-phase-6-edit-integration

Conversation

@yogthos

@yogthos yogthos commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Phase 6 of 9. Stacked on #29 (Phase 5).

After every `write` / `edit`, the LSP server's fresh diagnostics surface in the tool result so the agent corrects errors on the same turn — instead of writing broken code and discovering it via a manual `cargo check` round-trip.

What's in this PR

`src/lsp/diagnostic.rs` (new)

Function Purpose
`pretty(d)` One-line `SEVERITY [line:col] msg` with 0→1-based coordinate conversion
`report(file, issues)` Wraps in `<diagnostics file="...">` block. ERROR severity only. Caps at 20 with `... and N more` overflow.
`build_report_block(current, all)` The full agent-facing block: current-file errors + up to 5 other-file errors

15 diagnostic tests covering severity labels, coordinate conversion, error filtering, MAX_PER_FILE cap with overflow footer, other-files cap, warning-only-files-don't-appear, and caller-path preservation through canonicalize lookup.

`src/agent/tools/write.rs` + `edit.rs`

New optional `Option<Arc>` field. When set, after the write/edit:

  1. `manager.touch_file(path, TouchMode::AwaitPush { after: write_at, timeout: 10s })` so the server processes `didChange` and publishes fresh diagnostics.
  2. `manager.all_diagnostics()` → `diagnostic::build_report_block` → append to tool output.

The shared `append_lsp_block` helper lives in `write.rs` and is called from both tools. Errors during touch/wait are swallowed — diagnostic surfacing is a side-effect; the primary tool contract is "wrote/edited the file."

`src/agent/tools/read.rs`

New optional `Option<Arc>` field. Read fires off a `tokio::spawn(touch_file(Notify))` so the server has the file open by the time the agent edits it. No diagnostic block in the read's output — that's the edit tool's job.

Test coverage

  • Diagnostic module: 15 tests (severities, error-only filter, caps, path preservation regression).
  • WriteTool: 2 integration tests — no-manager path preserves pre-LSP output exactly (regression); manager-with-no-diagnostics appends nothing.
  • Pre-LSP tests for write/edit/read still pass (no behavior change in the no-manager path).

Code review fixes applied before push

  • Display the caller's path (not the canonical form) in the current-file diagnostic section so macOS `/tmp ↔ /private/tmp` asymmetry doesn't show the agent a path it didn't type. Regression test added.

Scope

  • builder.rs passes `None` for all three tool constructors' new `lsp_manager` arg. Phase 7's CLI/config plumbing will create the manager from build_channels and populate this.
  • Tools' integration is end-to-end-functional once the manager is wired through; the no-manager path is exercised by tests, the with-manager path is exercised by the diagnostic-block-formatting tests + a manual test in Phase 9.

Test plan

  • `cargo build` clean
  • `cargo test --bin dirge -- --skip plugin` → 422 passing (was 405; +17 from Phase 6)
  • `cargo fmt --check` clean
  • End-to-end manual test against rust-analyzer: edit a file with a deliberate compile error, observe the diagnostic block in the edit tool's output — comes in Phase 9.

Next: Phase 7 (config + CLI plumbing — `build_channels` creates the `LspManager` with `ProcessSpawner` configured for the 4 built-in servers; threads through every `build_agent` call site).

@yogthos
yogthos changed the base branch from feature/lsp-phase-5-tool to main May 20, 2026 01:46
src/lsp/diagnostic.rs (new)
- pretty(diagnostic): one-line 'SEVERITY [line:col] msg' rendering with
  LSP 0-based → display 1-based coordinate conversion.
- report(file, issues): wraps in <diagnostics file="..."> block.
  ERROR severity only (warnings/hints are noise). MAX_PER_FILE=20 with
  '... and N more' overflow footer.
- build_report_block(current, all): the full agent-facing block that
  write/edit tools append. Two sections (each optional): current file
  errors, then up to MAX_PROJECT_DIAGNOSTICS_FILES=5 other-file errors
  (e.g. downstream callers that now fail type-checking).
- 15 tests covering: severity labels, 1-based coordinate display,
  missing-severity defaults to ERROR (regression — pretty must never
  panic or hide a diagnostic), error filtering (regression — warnings
  must NOT appear), wrap tags, cap at MAX_PER_FILE with overflow
  (regression — generated files with hundreds of errors must not blow
  context), other-files cap (regression — single edit shouldn't dump
  entire project state), warning-only files don't appear, caller path
  preserved in display (regression — not canonical form).

src/agent/tools/write.rs
- New optional Option<Arc<LspManager>> field. With manager set, after
  the write: touch_file(AwaitPush) + build_report_block + append to
  output. DIAGNOSTIC_WAIT = 10s matches opencode.
- Shared append_lsp_block helper used by both write and edit.
- 2 tests: no-manager path preserves pre-LSP output exactly
  (regression); manager-with-no-diagnostics appends nothing.

src/agent/tools/edit.rs
- Same Option<Arc<LspManager>> field + integration. Diagnostic block
  appended after the existing diff block.

src/agent/tools/read.rs
- New optional Option<Arc<LspManager>> field. Read warms the LSP server
  with a fire-and-forget tokio::spawn(touch_file(Notify)). The read's
  primary output is unchanged — no diagnostic block (that's the edit
  tool's job).

src/agent/builder.rs
- All three tool constructors now pass None for lsp_manager. Phase 7's
  CLI/config plumbing will populate it.

Code review fixes applied before push:
- Display caller path (not canonical form) in current-file section so
  macOS /tmp / /private/tmp asymmetry doesn't confuse the agent.

Phase 5: 100, Phase 6: +16 -> 116 LSP tests. Suite: 405 -> 421.
@yogthos
yogthos force-pushed the feature/lsp-phase-6-edit-integration branch from 0fb964e to 29885e2 Compare May 20, 2026 01:53
@yogthos
yogthos merged commit 0b7c6f4 into main May 20, 2026
@yogthos
yogthos deleted the feature/lsp-phase-6-edit-integration branch May 20, 2026 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant