lsp-max 26.7.3

Law-state LSP runtime: max LSP 3.18 coverage, process-mining conformance, receipt-chain admission
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use async_trait::async_trait;
use lsp_types_max::*;
use serde_json::json;
use tower::ServiceExt;

use super::super::*;
use super::*;
use crate::jsonrpc::Result;

#[derive(Debug)]
struct MockLsp318;

#[async_trait]
impl LanguageServer for MockLsp318 {
    async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
        Ok(InitializeResult::default())
    }

    async fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    async fn inline_completion(
        &self,
        _params: lsp_types_max::InlineCompletionParams,
    ) -> Result<Option<lsp_types_max::InlineCompletionResponse>> {
        Ok(Some(lsp_types_max::InlineCompletionResponse::List(
            lsp_types_max::InlineCompletionList {
                items: vec![lsp_types_max::InlineCompletionItem {
                    insert_text: lsp_types_max::StringOrStringValue::String(
                        "hello_world".to_string(),
                    ),
                    filter_text: None,
                    range: None,
                    command: None,
                    insert_text_format: None,
                }],
            },
        )))
    }

    async fn text_document_content(
        &self,
        _params: crate::max_protocol::lsp_3_18::TextDocumentContentParams,
    ) -> Result<crate::max_protocol::lsp_3_18::TextDocumentContentResult> {
        Ok(crate::max_protocol::lsp_3_18::TextDocumentContentResult {
            text: "test content".to_string(),
        })
    }
}

#[tokio::test(flavor = "current_thread")]
async fn test_max_rpc_endpoints() {
    // Remove any stale receipt files from previous failed runs
    let _ = std::fs::remove_file("admission.receipt");
    let _ = std::fs::remove_file("security.receipt");
    let _ = std::fs::remove_file("auth.receipt");
    let _ = std::fs::remove_file("debug.log");

    let (mut service, _) = LspService::new(|_| Mock);

    // 1. Initialize
    let initialize = initialize_request(1);
    let response = service.ready().await.unwrap().call(initialize).await;
    assert!(response.is_ok());

    // 2. Call max/snapshot
    let req = Request::build("max/snapshot").id(2).finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let snapshot_id: max_protocol::SnapshotId = serde_json::from_value(res.unwrap()).unwrap();
    assert!(snapshot_id.0.starts_with("snap-"));

    // 3. After initialize, gate-state-check is active (initialized != Uninitialized),
    //    so diag-uninitialized-admission must NOT exist.
    let req = Request::build("max/explainDiagnostic")
        .params("diag-uninitialized-admission".to_string())
        .id(3)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let err = res.unwrap_err();
    assert_eq!(
        err.message,
        "Diagnostic 'diag-uninitialized-admission' not found"
    );

    // 3b. Verify conformanceVector has no refused axes (gate-state-check now passes).
    let req = Request::build("max/conformanceVector").id(4).finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let cv: max_protocol::ConformanceVector = serde_json::from_value(res.unwrap()).unwrap();
    assert!(
        cv.refused.is_empty(),
        "Expected no refused axes after initialize, got: {:?}",
        cv.refused
    );
    // Note: under strict mode, unknown axes block release, so admits_release() is false.
    // We only verify that there are no active refused axes.
    // 6. Test Law 3: Receipt Integrity (Missing Validation Receipt)
    // Get the repair plan for diag-missing-receipt
    let req = Request::build("max/repairPlan")
        .params("diag-missing-receipt".to_string())
        .id(7)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let plans2: Vec<max_protocol::MaxCodeAction> = serde_json::from_value(res.unwrap()).unwrap();
    let action_with_dep = plans2[0].clone();

    // Attempting to apply it fails because expected_receipts has "rcpt-security-auth" which is not registered yet
    let req = Request::build("max/applyRepairTransaction")
        .params(serde_json::to_value(action_with_dep.clone()).unwrap())
        .id(8)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let err = res.unwrap_err();
    assert!(err.message.contains("Receipt integrity violation"));

    // Retrieve generator action to get security auth receipt
    let req = Request::build("max/repairPlan")
        .params("diag-auth-generator".to_string())
        .id(9)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let plans3: Vec<max_protocol::MaxCodeAction> = serde_json::from_value(res.unwrap()).unwrap();
    let gen_action = plans3[0].clone();

    // Apply generator action to obtain "rcpt-security-auth"
    let req = Request::build("max/applyRepairTransaction")
        .params(serde_json::to_value(gen_action).unwrap())
        .id(10)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let gen_receipt: max_protocol::Receipt = serde_json::from_value(res.unwrap()).unwrap();
    assert_eq!(gen_receipt.receipt_id, "rcpt-security-auth");

    // Now apply action_with_dep again - it should succeed!
    let req = Request::build("max/applyRepairTransaction")
        .params(serde_json::to_value(action_with_dep).unwrap())
        .id(11)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let final_receipt: max_protocol::Receipt = serde_json::from_value(res.unwrap()).unwrap();
    assert!(final_receipt.receipt_id.starts_with("rcpt-"));

    // 7. Verify we can lookup receipt
    let req = Request::build("max/receipt")
        .params("rcpt-security-auth".to_string())
        .id(12)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let retrieved: max_protocol::Receipt = serde_json::from_value(res.unwrap()).unwrap();
    assert_eq!(retrieved.hash, gen_receipt.hash);

    // 8. Test max/runGate
    let req = Request::build("max/runGate")
        .params(serde_json::to_value(max_protocol::GateId("some-gate".to_string())).unwrap())
        .id(13)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let gate_result: bool = serde_json::from_value(res.unwrap()).unwrap();
    assert!(gate_result);

    // 9. Export Analysis Bundle for the snapshot
    let req = Request::build("max/exportAnalysisBundle")
        .params(serde_json::to_value(snapshot_id.clone()).unwrap())
        .id(14)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let bundle: max_protocol::AnalysisBundle = serde_json::from_value(res.unwrap()).unwrap();
    assert_eq!(bundle.snapshot_id.0, snapshot_id.0);
    assert!(!bundle.diagnostics.is_empty());

    // Cleanup created receipt files from disk
    let _ = std::fs::remove_file("admission.receipt");
    let _ = std::fs::remove_file("security.receipt");
    let _ = std::fs::remove_file("auth.receipt");
}

#[tokio::test(flavor = "current_thread")]
async fn test_lsp_3_18_methods() {
    let (mut service, _) = LspService::new(|_| Mock);

    let initialize = initialize_request(1);
    let response = service.ready().await.unwrap().call(initialize).await;
    assert!(response.is_ok());

    // 1. textDocument/inlineCompletion
    let req = Request::build("textDocument/inlineCompletion")
        .params(json!({
            "textDocument": { "uri": "file:///foo.rs" },
            "position": { "line": 0, "character": 0 },
            "context": { "triggerKind": 1 }
        }))
        .id(2)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    // inline_completion default delegates to the impl which returns Ok(None) (not MethodNotFound)
    assert!(res.is_ok());

    // 2. workspace/textDocumentContent
    let req = Request::build("workspace/textDocumentContent")
        .params(json!({
            "textDocument": { "uri": "file:///foo.rs" }
        }))
        .id(3)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    assert_eq!(res.unwrap_err().code, ErrorCode::MethodNotFound);

    // 3. workspace/textDocumentContent/refresh
    let req = Request::build("workspace/textDocumentContent/refresh")
        .params(json!({
            "uri": "file:///foo.rs"
        }))
        .id(4)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    assert_eq!(res.unwrap_err().code, ErrorCode::MethodNotFound);
}

#[tokio::test(flavor = "current_thread")]
async fn test_lsp_3_18_methods_routing() {
    let (mut service, _) = LspService::new(|_| MockLsp318);

    let initialize = initialize_request(1);
    let response = service.ready().await.unwrap().call(initialize).await;
    assert!(response.is_ok());

    // 1. textDocument/inlineCompletion
    let req = Request::build("textDocument/inlineCompletion")
        .params(json!({
            "textDocument": { "uri": "file:///foo.rs" },
            "position": { "line": 0, "character": 0 },
            "context": { "triggerKind": 1 }
        }))
        .id(2)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let val = res.unwrap();
    assert_eq!(
        val,
        json!({
            "items": [
                {
                    "insertText": "hello_world",
                }
            ]
        })
    );

    // 2. workspace/textDocumentContent
    let req = Request::build("workspace/textDocumentContent")
        .params(json!({
            "textDocument": { "uri": "file:///foo.rs" }
        }))
        .id(3)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    let val = res.unwrap();
    assert_eq!(
        val,
        json!({
            "text": "test content"
        })
    );

    // 3. workspace/textDocumentContent/refresh
    let req = Request::build("workspace/textDocumentContent/refresh")
        .params(json!({
            "uri": "file:///foo.rs"
        }))
        .id(4)
        .finish();
    let response = service
        .ready()
        .await
        .unwrap()
        .call(req)
        .await
        .unwrap()
        .unwrap();
    let (_, res) = response.into_parts();
    assert_eq!(res.unwrap_err().code, ErrorCode::MethodNotFound);
}