Files
clice/tests/integration/test_rapid_edit.py
ykiko e239b0d32c feat: smart PCH rebuild, #include/import completion, rapid-edit robustness (#394)
## Summary

### Preamble completeness check
- `is_preamble_complete()` in `scan.cpp`: checks whether
`#include`/`import`/`export module` directives in the preamble region
are syntactically complete (have closing `>`/`"`/`;`)
- `ensure_pch` defers PCH rebuild when preamble is incomplete (user
still typing), reuses old PCH instead of failing

### #include / import completion
- Master intercepts completion requests in `#include "..."` / `#include
<...>` / `import ...` contexts before forwarding to worker
- `complete_include()`: searches include paths (from compile args via
`SearchConfig`) using `DirListingCache`, supports
quoted/angled/multi-level paths
- `complete_import()`: filters `path_to_module` map by prefix
- Word boundary checks prevent false matches (e.g. `important` not
treated as `import`)

### Detached compile task (rapid-edit fix)
- Compile operations (`ensure_deps` + `send_stateful` +
`publish_diagnostics`) run as detached tasks via `loop.schedule()`,
independent of the LSP request coroutine chain
- LSP `$/cancelRequest` can no longer kill in-flight compilations —
previously, cancellation would destroy the `ensure_compiled` coroutine
frame, leaving `doc.compiling` permanently set and hanging all
subsequent requests
- `CompileGuard` RAII ensures `doc.compiling` is always cleaned up even
if the detached task fails
- Stale feature requests (where `ast_dirty` became true after compile
finished) are dropped before forwarding to worker

### Other fixes
- `signal(SIGPIPE, SIG_IGN)` on POSIX: prevents server crash when LSP
client disconnects mid-write
- `CompilationUnitRef::file_path()` / `deps()`: null-check
`FileEntryRef` to prevent segfault on invalid FileID
- `stateless_worker.cpp`: log BuildPCH diagnostic errors for
debuggability
- Default worker counts changed to 2 stateful + 3 stateless
- `logging_dir` default changed to `.clice/logs` in config

### Tests
- 19 unit tests for `is_preamble_complete` (incomplete `#include`,
`import`, `export module`, mixed cases)
- Integration tests: `test_include_completion.py` (5 tests),
`test_import_completion.py` (4 tests), `test_rapid_edit.py` (2 tests),
`test_pch.py` (4 new tests)
- Smoke test: `rapid_edit.jsonl` — recorded VSCode session with 40 rapid
edits + 61 cancel requests

## Test plan
- [x] Unit tests: 463 passed
- [x] Integration tests: 104 passed
- [x] Smoke test (rapid_edit.jsonl): PASS
- [x] Manual VSCode testing with `#include <iostream>` project

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 14:49:09 +08:00

84 lines
2.8 KiB
Python

"""Integration tests for rapid editing: ensure no hang and correct hover results."""
import asyncio
import pytest
from lsprotocol.types import (
DidChangeTextDocumentParams,
DidCloseTextDocumentParams,
HoverParams,
Position,
TextDocumentContentChangeWholeDocument,
TextDocumentIdentifier,
VersionedTextDocumentIdentifier,
)
def _doc(uri: str) -> TextDocumentIdentifier:
return TextDocumentIdentifier(uri=uri)
@pytest.mark.workspace("hello_world")
async def test_rapid_edits_with_hover(client, workspace):
"""50 rapid edits, each followed by a hover on 'add' function.
The file has #include <iostream> so PCH build is non-trivial.
This must not hang and the final hover must return correct results.
"""
main_cpp = workspace / "main.cpp"
uri, content = await client.open_and_wait(main_cpp)
# Hover on 'add' (line 2, char 4) to verify initial state.
hover = await asyncio.wait_for(
client.text_document_hover_async(
HoverParams(
text_document=_doc(uri),
position=Position(line=2, character=4),
)
),
timeout=30.0,
)
assert hover is not None, "Initial hover on 'add' should not be None"
# 50 rapid body edits, each followed by a hover request.
for i in range(50):
new_content = content.replace("return a + b;", f"return a + b + {i};")
client.text_document_did_change(
DidChangeTextDocumentParams(
text_document=VersionedTextDocumentIdentifier(uri=uri, version=i + 2),
content_changes=[
TextDocumentContentChangeWholeDocument(text=new_content)
],
)
)
# Fire-and-forget hover on 'add' — just ensure it doesn't hang.
# We don't await the result here to simulate real editor behavior
# where requests overlap.
asyncio.ensure_future(
client.text_document_hover_async(
HoverParams(
text_document=_doc(uri),
position=Position(line=2, character=4),
)
)
)
await asyncio.sleep(0.02) # ~20ms between edits
# Wait a moment for in-flight requests to settle.
await asyncio.sleep(1.0)
# Final hover must succeed and return correct result.
final_hover = await asyncio.wait_for(
client.text_document_hover_async(
HoverParams(
text_document=_doc(uri),
position=Position(line=2, character=4),
)
),
timeout=30.0,
)
assert final_hover is not None, "Final hover returned None — worker may have hung"
assert final_hover.contents is not None, "Final hover contents should not be None"
client.text_document_did_close(DidCloseTextDocumentParams(text_document=_doc(uri)))