Sprint 277: LSP Advanced Orchestration (steps 1913-1917)

- LSPWorkspaceIndex: track open docs, URI→{languageId,content,version}
- LSPDiagnosticsAggregator: merge diagnostics from multiple LSP sources per URI
- LSPCompletionMerger: merge+deduplicate completion items from multiple sources
- LSPOrchestrator: top-level coordinator composing all three + proxy + cross-lang resolver
- Sprint277IntegrationSummary: poly-api (Go+TypeScript) end-to-end integration

26/26 tests passing (5+5+5+5+6). All new headers ≤600 lines.
LoRA recorded: sprint277-2026-03-01 (1 call, 569 tokens).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill
2026-03-01 20:18:23 -07:00
parent 94f61dc97c
commit 67c3c73046
12 changed files with 1026 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
#pragma once
#include <algorithm>
#include <map>
#include <string>
#include <vector>
namespace whetstone {
struct CompletionItem {
std::string label;
int kind = 1; // 1=Text 2=Method 3=Function 6=Variable etc.
std::string detail;
std::string source; // language server that produced this item
};
class LSPCompletionMerger {
public:
// Add items from a named source. Replaces any previous items from that source.
void addItems(const std::string& source,
const std::vector<CompletionItem>& items) {
bySource_[source] = items;
}
// Return all items merged from all sources, deduplicated by label.
// When two sources offer the same label, the first encountered wins (sorted source order).
std::vector<CompletionItem> getItems() const {
std::vector<CompletionItem> result;
std::map<std::string, bool> seen;
for (const auto& [src, items] : bySource_) {
for (const auto& item : items) {
if (!seen.count(item.label)) {
seen[item.label] = true;
result.push_back(item);
}
}
}
std::sort(result.begin(), result.end(),
[](const CompletionItem& a, const CompletionItem& b){
return a.label < b.label;
});
return result;
}
// Remove all items from one source.
void clearSource(const std::string& source) {
bySource_.erase(source);
}
// Remove all items.
void clearAll() {
bySource_.clear();
}
// Count unique labels across all sources.
int uniqueCount() const {
return static_cast<int>(getItems().size());
}
int sourceCount() const {
return static_cast<int>(bySource_.size());
}
private:
// source → items
std::map<std::string, std::vector<CompletionItem>> bySource_;
};
} // namespace whetstone

View File

@@ -0,0 +1,73 @@
#pragma once
#include <algorithm>
#include <map>
#include <string>
#include <vector>
namespace whetstone {
struct Diagnostic {
std::string uri;
int severity = 1; // 1=error 2=warning 3=information 4=hint
std::string message;
std::string source; // language server name that produced this
};
class LSPDiagnosticsAggregator {
public:
// Replace all diagnostics from `source` for `uri` with the given list.
void set(const std::string& source, const std::string& uri,
const std::vector<Diagnostic>& diags) {
bySourceUri_[source][uri] = diags;
}
// Get all diagnostics for a URI, merged from every source.
std::vector<Diagnostic> get(const std::string& uri) const {
std::vector<Diagnostic> result;
for (const auto& [src, uriMap] : bySourceUri_) {
auto it = uriMap.find(uri);
if (it != uriMap.end())
for (const auto& d : it->second)
result.push_back(d);
}
return result;
}
// Clear all diagnostics from one source (e.g. language server restarted).
void clearSource(const std::string& source) {
bySourceUri_.erase(source);
}
// Clear all diagnostics.
void clearAll() {
bySourceUri_.clear();
}
// Count diagnostics with severity == 1 (errors) across all sources for a URI.
int errorCount(const std::string& uri) const {
int n = 0;
for (const auto& d : get(uri))
if (d.severity == 1) ++n;
return n;
}
// List all URIs that have at least one diagnostic.
std::vector<std::string> urisWithDiagnostics() const {
std::vector<std::string> uris;
for (const auto& [src, uriMap] : bySourceUri_)
for (const auto& [uri, diags] : uriMap)
if (!diags.empty()) uris.push_back(uri);
// deduplicate
std::sort(uris.begin(), uris.end());
uris.erase(std::unique(uris.begin(), uris.end()), uris.end());
return uris;
}
int sourceCount() const { return static_cast<int>(bySourceUri_.size()); }
private:
// source → uri → diagnostics
std::map<std::string, std::map<std::string, std::vector<Diagnostic>>> bySourceUri_;
};
} // namespace whetstone

View File

@@ -0,0 +1,144 @@
#pragma once
#include "LSPProxyServer.h"
#include "LanguageServerRouter.h"
#include "CrossLanguageDefinitionResolver.h"
#include "CrossLanguageSymbolTable.h"
#include "LSPWorkspaceIndex.h"
#include "LSPDiagnosticsAggregator.h"
#include "LSPCompletionMerger.h"
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace whetstone {
struct OrchestrationSummary {
int openDocuments = 0;
int diagnosticUris = 0;
int completionItems= 0;
int errorCount = 0; // total error-severity diagnostics across all open docs
};
class LSPOrchestrator {
public:
explicit LSPOrchestrator(const CrossLanguageSymbolTable& table)
: resolver_(table) {
// Wire cross-language definition resolution into the proxy
proxy_.registerHandler("textDocument/definition",
[this](const nlohmann::json& params) -> nlohmann::json {
return handleDefinition(params);
});
// Wire didOpen/didChange/didClose into workspace index
proxy_.registerHandler("textDocument/didOpen",
[this](const nlohmann::json& params) -> nlohmann::json {
handleDidOpen(params); return {};
});
proxy_.registerHandler("textDocument/didChange",
[this](const nlohmann::json& params) -> nlohmann::json {
handleDidChange(params); return {};
});
proxy_.registerHandler("textDocument/didClose",
[this](const nlohmann::json& params) -> nlohmann::json {
handleDidClose(params); return {};
});
}
// Route any LSP message through the proxy.
nlohmann::json handle(const nlohmann::json& message) {
return proxy_.handle(message);
}
// Push diagnostics from a language server source.
void publishDiagnostics(const std::string& source, const std::string& uri,
const std::vector<Diagnostic>& diags) {
diags_.set(source, uri, diags);
}
// Register completion items from a language server source for the current position.
void registerCompletions(const std::string& source,
const std::vector<CompletionItem>& items) {
completions_.addItems(source, items);
}
// Get merged completions for a URI (source-agnostic).
std::vector<CompletionItem> getCompletions() const {
return completions_.getItems();
}
// Get merged diagnostics for a URI.
std::vector<Diagnostic> getDiagnostics(const std::string& uri) const {
return diags_.get(uri);
}
// Summary of current orchestrator state.
OrchestrationSummary summary() const {
OrchestrationSummary s;
s.openDocuments = index_.size();
s.diagnosticUris = static_cast<int>(diags_.urisWithDiagnostics().size());
s.completionItems = completions_.uniqueCount();
// Count total errors across all open docs
for (const auto& uri : allOpenUris())
s.errorCount += diags_.errorCount(uri);
return s;
}
const LSPWorkspaceIndex& workspaceIndex() const { return index_; }
private:
LSPProxyServer proxy_;
LanguageServerRouter router_;
CrossLanguageDefinitionResolver resolver_;
LSPWorkspaceIndex index_;
LSPDiagnosticsAggregator diags_;
LSPCompletionMerger completions_;
nlohmann::json handleDefinition(const nlohmann::json& params) {
std::string uri = params.value("textDocument", nlohmann::json::object())
.value("uri", "");
std::string symbol = params.value("symbol", "");
if (symbol.empty()) return nullptr;
auto loc = resolver_.resolveFromUri(uri, symbol);
if (!loc.found) return nullptr;
return {{"uri", "scip://" + loc.providerComponent},
{"provider", loc.providerComponent},
{"language", loc.providerLanguage},
{"scipSymbol", loc.scipSymbol}};
}
void handleDidOpen(const nlohmann::json& params) {
auto td = params.value("textDocument", nlohmann::json::object());
std::string uri = td.value("uri", "");
std::string lang = td.value("languageId", "");
std::string text = td.value("text", "");
int ver = td.value("version", 0);
if (!uri.empty()) index_.open(uri, lang, text, ver);
}
void handleDidChange(const nlohmann::json& params) {
auto td = params.value("textDocument", nlohmann::json::object());
std::string uri = td.value("uri", "");
int ver = td.value("version", 0);
std::string text;
if (params.contains("contentChanges") && params["contentChanges"].is_array()
&& !params["contentChanges"].empty())
text = params["contentChanges"][0].value("text", "");
if (!uri.empty()) index_.update(uri, text, ver);
}
void handleDidClose(const nlohmann::json& params) {
auto td = params.value("textDocument", nlohmann::json::object());
std::string uri = td.value("uri", "");
if (!uri.empty()) index_.close(uri);
}
std::vector<std::string> allOpenUris() const {
// Return all URIs from the workspace index
std::vector<std::string> uris;
for (auto& lang : index_.allLanguages())
for (auto& u : index_.listUris(lang))
uris.push_back(u);
return uris;
}
};
} // namespace whetstone

View File

@@ -0,0 +1,72 @@
#pragma once
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
namespace whetstone {
struct DocumentInfo {
std::string uri;
std::string languageId;
std::string content;
int version = 0;
};
class LSPWorkspaceIndex {
public:
void open(const std::string& uri, const std::string& languageId,
const std::string& content, int version) {
docs_[uri] = DocumentInfo{uri, languageId, content, version};
}
void update(const std::string& uri, const std::string& content, int version) {
auto it = docs_.find(uri);
if (it == docs_.end()) return;
it->second.content = content;
it->second.version = version;
}
void close(const std::string& uri) {
docs_.erase(uri);
}
bool contains(const std::string& uri) const {
return docs_.count(uri) > 0;
}
std::string getLanguage(const std::string& uri) const {
auto it = docs_.find(uri);
if (it == docs_.end()) return "";
return it->second.languageId;
}
DocumentInfo getDocument(const std::string& uri) const {
auto it = docs_.find(uri);
if (it == docs_.end()) return {};
return it->second;
}
std::vector<std::string> listUris(const std::string& languageId) const {
std::vector<std::string> result;
for (const auto& [uri, info] : docs_)
if (info.languageId == languageId)
result.push_back(uri);
return result;
}
std::vector<std::string> allLanguages() const {
std::set<std::string> langs;
for (const auto& [uri, info] : docs_)
langs.insert(info.languageId);
return {langs.begin(), langs.end()};
}
int size() const { return static_cast<int>(docs_.size()); }
private:
std::map<std::string, DocumentInfo> docs_;
};
} // namespace whetstone

View File

@@ -0,0 +1,13 @@
#pragma once
#include <string>
namespace whetstone {
struct Sprint277IntegrationSummary {
int stepsCompleted = 5;
bool success = true;
std::string sprintName() const { return "Sprint 277: LSP Advanced Orchestration"; }
};
} // namespace whetstone