1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Attach completion handling: outcome publication, refusal and sticky
//! bookkeeping, per-project warm status rows, and the status-line
//! message for every decision — silence is not a report.
use super::super::super::picker::ProjectStatus;
use super::super::attach::AttachRecord;
use super::super::*;
use super::layer_suffix;
use std::sync::mpsc::channel;
impl Editor {
pub(crate) fn handle_lsp_attach(&mut self, record: AttachRecord) {
trace_attach(&record);
self.worker_handles.remove(&record.ticket);
let key = self
.lsp_state
.attach
.pending
.iter()
.find_map(|(key, &owner)| (owner == record.ticket).then(|| key.clone()));
let Some(key) = key else {
trace::services::rejected("lsp", "attach completion superseded");
self.retire_superseded_transport(record.server);
return;
};
self.lsp_state.attach.pending.remove(&key);
// Warm-up completions feed per-project status rows (0063 §2);
// document attach completions already have the status line.
let warm = self.lsp_state.attach.warm_attempts.remove(&key);
if key.target != record.target || key.language != record.language {
self.retire_superseded_transport(record.server);
trace::services::rejected("lsp", "attach result differs from its requested workspace");
return;
}
let attach::AttachRecord {
ticket: _,
server,
language,
name,
root,
target,
outcome,
layers,
} = record;
// Malformed layers are diagnosed whatever the outcome (0033
// §2): a healthy fallback server must not erase them.
self.record_layer_diagnostics(&layers);
let warning = layer_suffix(&layers);
match outcome {
attach::AttachDecision::Cancelled => {}
attach::AttachDecision::Attached => {
if warm {
self.record_project_status(key.path.clone(), ProjectStatus::healthy());
}
let Some(server) = server else { return };
if self.lsp_state.attach.attached.iter().any(|attachment| {
attachment.language == language
&& attachment.root == root
&& attachment.target == target
}) {
self.retire_superseded_transport(Some(server));
self.lsp_did_open_current();
return;
}
self.lsp_state
.attach
.attached
.retain(|a| !(a.language == language && a.root == root && a.target == target));
// The placement exists before any didOpen resolves
// against it — live and replayed alike.
self.lsp_state.attach.attached.push(attach::Attachment {
language: language.clone(),
root: root.clone(),
server,
target: target.clone(),
});
match warning {
Some(warning) => self.message = format!("lsp: {warning}"),
None => self.message = format!("lsp: {name} starting"),
}
let transport = self
.lsp_state
.attach
.transport
.lock()
.ok()
.and_then(|mut table| table.remove(&server));
match transport {
Some(attach::LiveTransport { client, rx }) => {
// TUI: forward like every late-attaching server.
if let Some(app_tx) = &self.app_tx {
let tx = app_tx.clone();
std::thread::spawn(move || {
while let Ok(event) = rx.recv() {
if tx
.send(crate::editor::events::AppEvent::Lsp(event))
.is_err()
{
break;
}
}
});
let (_, empty) = channel();
self.lsp_servers.push(LspServer {
id: server,
client: Some(client),
rx: empty,
ready: false,
});
} else {
self.lsp_servers.push(LspServer {
id: server,
client: Some(client),
rx,
ready: false,
});
}
}
None => {
// Replayed server: identity only, replies arrive
// through the injected event stream.
self.lsp_servers.push(LspServer {
id: server,
client: None,
rx: channel().1,
ready: false,
});
}
}
self.lsp_did_open_current();
}
decision => {
if warm {
self.record_project_status(
key.path.clone(),
ProjectStatus::from_decision(&decision, &name),
);
}
if matches!(
decision,
attach::AttachDecision::TrustRequired { .. }
| attach::AttachDecision::TrustError { .. }
) {
self.lsp_state
.attach
.trust_roots
.insert(key.clone(), root.clone());
}
let sticky = !matches!(
decision,
attach::AttachDecision::TrustRequired { .. }
| attach::AttachDecision::TrustError { .. }
| attach::AttachDecision::RemoteIo { .. }
);
let first = self
.lsp_state
.attach
.refused
.insert(key, decision.clone())
.is_none();
if sticky && !first {
return;
}
self.message = match decision {
attach::AttachDecision::NoServer => format!("no language server for {name}"),
attach::AttachDecision::TrustRequired { command } => {
format!("project config wants to run `{command}` — :trust to allow (once)")
}
attach::AttachDecision::TrustError { error } => {
format!("project trust: {error}")
}
attach::AttachDecision::NotExecutable {
command,
reason,
hint,
} => {
format!("lsp: {command} {reason} — {hint}")
}
attach::AttachDecision::SpawnFailed { reason } => {
format!("lsp: {name} could not start — {reason}")
}
attach::AttachDecision::RemoteIo { reason } => {
format!("lsp: remote discovery failed — {reason}")
}
attach::AttachDecision::Attached | attach::AttachDecision::Cancelled => {
unreachable!("matched above")
}
};
if let Some(warning) = warning {
self.message = format!("{} — {}", self.message, warning);
}
}
}
// A completion freed a warm-up slot: keep the bounded queue
// moving (0063 §2).
self.lsp_warm_progress();
}
/// Record newly reported malformed-layer diagnostics (0033 §2),
/// deduped: every later attach for the same layers is already
/// covered.
fn record_layer_diagnostics(&mut self, layers: &[strop_lsp::languages::LayerDiagnostic]) {
for diagnostic in layers {
let state = &mut self.lsp_state.attach;
if !state.layer_diagnostics.contains(diagnostic) {
state.layer_diagnostics.push(diagnostic.clone());
}
}
}
fn retire_superseded_transport(&mut self, server: Option<ServerId>) {
let Some(server) = server else { return };
let transport = self
.lsp_state
.attach
.transport
.lock()
.ok()
.and_then(|mut table| table.remove(&server));
if let Some(attach::LiveTransport { client, .. }) = transport {
// Joining never blocks the input thread (same policy as
// lsp_failed).
std::thread::spawn(move || {
client.shutdown();
client.wait(std::time::Duration::from_secs(2));
});
}
}
}
/// Attach completions reach the structured trace with their outcome
/// and any malformed-layer diagnostics (0033 §2/§3) — silence is not a
/// report. Runs at handler entry, before ownership decisions.
fn trace_attach(record: &attach::AttachRecord) {
use strop_trace::{record_with, EventKind};
record_with(EventKind::JobFinished, || {
let mut value = serde_json::json!({
"service": "lsp",
"result": "attach",
"outcome": record.outcome.label(),
"language": record.language,
"name": record.name,
"server": record.server,
"target": record.target.label(),
"root": trace::services::NativePath(record.root.clone()),
"layers": &record.layers,
});
match &record.outcome {
attach::AttachDecision::NotExecutable {
command,
reason,
hint,
} => {
value["command"] = serde_json::json!(command);
value["reason"] = serde_json::json!(reason);
value["hint"] = serde_json::json!(hint);
}
attach::AttachDecision::SpawnFailed { reason }
| attach::AttachDecision::RemoteIo { reason } => {
value["reason"] = serde_json::json!(reason);
}
_ => {}
}
value
});
}