rubik 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +108 -43
  4. data/README.md +5 -1
  5. data/lib/rubik/adapter/base.rb +19 -0
  6. data/lib/rubik/adapter/generic.rb +0 -14
  7. data/lib/rubik/adapter/node.rb +2 -0
  8. data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
  9. data/lib/rubik/cli/workspace_trust.rb +16 -0
  10. data/lib/rubik/config/defaults.rb +23 -0
  11. data/lib/rubik/config/store.rb +14 -0
  12. data/lib/rubik/kernel/dispatcher.rb +4 -4
  13. data/lib/rubik/log.rb +2 -1
  14. data/lib/rubik/lsp/client.rb +83 -180
  15. data/lib/rubik/lsp/document_store.rb +209 -0
  16. data/lib/rubik/mcp/client.rb +4 -3
  17. data/lib/rubik/mcp/manager.rb +6 -2
  18. data/lib/rubik/mcp/transport/stdio.rb +51 -9
  19. data/lib/rubik/orchestrator/goal_session.rb +505 -0
  20. data/lib/rubik/orchestrator/subagent/instance.rb +4 -0
  21. data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
  22. data/lib/rubik/orchestrator/subagent/questioner.rb +21 -6
  23. data/lib/rubik/orchestrator/subagent/step_runner.rb +1 -1
  24. data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
  25. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  26. data/lib/rubik/orchestrator.rb +1 -0
  27. data/lib/rubik/platform.rb +15 -0
  28. data/lib/rubik/plugin/storage.rb +20 -29
  29. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  30. data/lib/rubik/repl/commands/agent_execution.rb +7 -470
  31. data/lib/rubik/repl/commands/buffer.rb +1 -1
  32. data/lib/rubik/repl/commands/lsp.rb +1 -1
  33. data/lib/rubik/repl/input_loop.rb +1 -1
  34. data/lib/rubik/repl/lifecycle.rb +28 -0
  35. data/lib/rubik/runtime/manifest_store.rb +116 -0
  36. data/lib/rubik/runtime/overlay_storage.rb +247 -196
  37. data/lib/rubik/runtime/task_manager.rb +7 -2
  38. data/lib/rubik/runtime.rb +1 -0
  39. data/lib/rubik/security/allowlist.rb +167 -0
  40. data/lib/rubik/security/audit_log.rb +72 -0
  41. data/lib/rubik/security/classifier/classifier.rb +8 -0
  42. data/lib/rubik/security/command_gate.rb +567 -0
  43. data/lib/rubik/security/guard.rb +88 -1075
  44. data/lib/rubik/security/path_policy.rb +117 -0
  45. data/lib/rubik/security/synthesis_gate.rb +300 -0
  46. data/lib/rubik/security.rb +5 -0
  47. data/lib/rubik/storage/ranking.rb +113 -0
  48. data/lib/rubik/storage/store.rb +2 -92
  49. data/lib/rubik/storage/transcript_indexer.rb +2 -3
  50. data/lib/rubik/tools/builtins/grep.rb +2 -3
  51. data/lib/rubik/tools/builtins/lifecycle.rb +90 -87
  52. data/lib/rubik/tools/builtins/synthesis.rb +3 -0
  53. data/lib/rubik/tools/lifecycle_rollbacks.rb +87 -0
  54. data/lib/rubik/tools/native_search.rb +2 -1
  55. data/lib/rubik/tools/truncator.rb +25 -0
  56. data/lib/rubik/version.rb +1 -1
  57. metadata +11 -1
@@ -4,6 +4,7 @@ require "open3"
4
4
  require "json"
5
5
  require "uri"
6
6
  require_relative "server_command"
7
+ require_relative "document_store"
7
8
  require_relative "../platform"
8
9
  require_relative "../log"
9
10
 
@@ -35,7 +36,7 @@ module Rubik
35
36
  "vue" => "vue-language-server --stdio"
36
37
  }.freeze
37
38
 
38
- attr_reader :language, :server_cmd, :root_dir, :running, :diagnostics, :server_capabilities,
39
+ attr_reader :language, :server_cmd, :root_dir, :running, :server_capabilities,
39
40
  :sync_capabilities
40
41
 
41
42
  def initialize(language, root_dir: Dir.pwd, server_cmd: nil)
@@ -65,11 +66,13 @@ module Rubik
65
66
  @responses = {}
66
67
  @pending_requests = {}
67
68
  @reader_failure = nil
68
- @diagnostics = {}
69
- @open_documents = {}
70
- @document_contents = {}
71
- @diagnostic_states = {}
72
- @version_counter = 0
69
+ # The ONE acquisition rule for document state (see the invariant
70
+ # documented on Rubik::LSP::DocumentStore): hold @document_mutex
71
+ # across every @document_store call; the store takes @mutex itself
72
+ # for the diagnostic hashes. Transport writes (@write_mutex) may only
73
+ # happen inside those document_mutex regions, giving the single
74
+ # legal order document_mutex -> write_mutex -> mutex.
75
+ @document_store = DocumentStore.new(@mutex, @cv)
73
76
  @server_capabilities = {}
74
77
  @sync_capabilities = {
75
78
  source: :not_advertised, open_close: false, change: 0,
@@ -95,13 +98,7 @@ module Rubik
95
98
 
96
99
  @document_mutex.synchronize do
97
100
  @req_id = 0
98
- @open_documents.clear
99
- @document_contents.clear
100
- @version_counter = 0
101
- @mutex.synchronize do
102
- @diagnostics.clear
103
- @diagnostic_states.clear
104
- end
101
+ @document_store.reset
105
102
  end
106
103
  @mutex.synchronize do
107
104
  @responses.clear
@@ -173,7 +170,11 @@ module Rubik
173
170
  end
174
171
  @running = true
175
172
  init_res
176
- rescue StandardError
173
+ rescue StandardError => e
174
+ # The handshake failure cause (bad server binary, protocol mismatch,
175
+ # JSON parse error) is the only diagnostic an operator gets — never
176
+ # discard it silently (log.rb doctrine: Log, not raw warn, in lib/).
177
+ Rubik::Log.error(:lsp, "handshake failed: #{e.class}: #{e.message}")
177
178
  stop
178
179
  false
179
180
  end
@@ -238,50 +239,25 @@ module Rubik
238
239
  Rubik::Platform.kill_tree(@wait_thr.pid)
239
240
  begin
240
241
  @wait_thr.join
241
- rescue
242
+ rescue StandardError
242
243
  nil
243
244
  end
244
245
  end
245
246
  alias dispose stop
246
247
 
247
248
  def definition(file_path, line, character)
248
- return { ok: false, error: "LSP server not running" } unless alive?
249
-
250
- res = send_request("textDocument/definition", {
251
- textDocument: { uri: document_uri(file_path) },
252
- position: { line: line, character: character }
253
- })
254
-
255
- { ok: true, result: res }
256
- rescue StandardError => e
257
- { ok: false, error: e.message }
249
+ position_request("textDocument/definition", file_path, line, character)
258
250
  end
259
251
 
260
252
  def references(file_path, line, character)
261
- return { ok: false, error: "LSP server not running" } unless alive?
262
-
263
- res = send_request("textDocument/references", {
264
- textDocument: { uri: document_uri(file_path) },
265
- position: { line: line, character: character },
266
- context: { includeDeclaration: true }
267
- })
268
-
269
- { ok: true, result: res }
270
- rescue StandardError => e
271
- { ok: false, error: e.message }
253
+ position_request(
254
+ "textDocument/references", file_path, line, character,
255
+ context: { includeDeclaration: true }
256
+ )
272
257
  end
273
258
 
274
259
  def hover(file_path, line, character)
275
- return { ok: false, error: "LSP server not running" } unless alive?
276
-
277
- res = send_request("textDocument/hover", {
278
- textDocument: { uri: document_uri(file_path) },
279
- position: { line: line, character: character }
280
- })
281
-
282
- { ok: true, result: res }
283
- rescue StandardError => e
284
- { ok: false, error: e.message }
260
+ position_request("textDocument/hover", file_path, line, character)
285
261
  end
286
262
 
287
263
  def did_open(file_path, content, language_id = @language)
@@ -289,36 +265,24 @@ module Rubik
289
265
 
290
266
  abs_path = normalized_document_path(file_path)
291
267
  @document_mutex.synchronize do
292
- if @open_documents.key?(abs_path)
293
- return true if @document_contents[abs_path] == content
268
+ if @document_store.open?(abs_path)
269
+ return true if @document_store.content_for(abs_path) == content
294
270
 
295
271
  return send_document_change_unlocked(abs_path, content)
296
272
  end
297
273
 
298
- version = next_document_version
299
- @open_documents[abs_path] = version
300
- @document_contents[abs_path] = content
301
- old_state = @mutex.synchronize do
302
- previous = @diagnostic_states[abs_path]
303
- @diagnostic_states[abs_path] = pending_diagnostics(version)
304
- previous
305
- end
306
- sent = send_notification("textDocument/didOpen", {
307
- textDocument: {
308
- uri: document_uri(abs_path),
309
- languageId: language_id.to_s,
310
- version: version,
311
- text: content
312
- }
313
- })
314
- unless sent
315
- @open_documents.delete(abs_path)
316
- @document_contents.delete(abs_path)
317
- @mutex.synchronize do
318
- old_state ? @diagnostic_states[abs_path] = old_state : @diagnostic_states.delete(abs_path)
319
- end
274
+ @document_store.commit(abs_path) do
275
+ version = @document_store.next_version
276
+ @document_store.stage_version(abs_path, content, version)
277
+ send_notification("textDocument/didOpen", {
278
+ textDocument: {
279
+ uri: document_uri(abs_path),
280
+ languageId: language_id.to_s,
281
+ version: version,
282
+ text: content
283
+ }
284
+ })
320
285
  end
321
- sent
322
286
  end
323
287
  rescue StandardError
324
288
  false
@@ -338,27 +302,14 @@ module Rubik
338
302
 
339
303
  abs_path = normalized_document_path(file_path)
340
304
  @document_mutex.synchronize do
341
- version = @open_documents[abs_path]
342
- return false unless version
305
+ return false unless @document_store.open?(abs_path)
343
306
 
344
- content = @document_contents[abs_path]
345
- state, diagnostics = @mutex.synchronize do
346
- [@diagnostic_states.delete(abs_path), @diagnostics.delete(abs_path)]
307
+ @document_store.commit(abs_path) do
308
+ @document_store.evict(abs_path)
309
+ send_notification("textDocument/didClose", {
310
+ textDocument: { uri: document_uri(abs_path) }
311
+ })
347
312
  end
348
- @open_documents.delete(abs_path)
349
- @document_contents.delete(abs_path)
350
- sent = send_notification("textDocument/didClose", {
351
- textDocument: { uri: document_uri(abs_path) }
352
- })
353
- unless sent
354
- @open_documents[abs_path] = version
355
- @document_contents[abs_path] = content
356
- @mutex.synchronize do
357
- @diagnostic_states[abs_path] = state if state
358
- @diagnostics[abs_path] = diagnostics if diagnostics
359
- end
360
- end
361
- sent
362
313
  end
363
314
  rescue StandardError
364
315
  false
@@ -380,7 +331,7 @@ module Rubik
380
331
  return false if file_path.nil? || file_path.to_s.strip.empty?
381
332
 
382
333
  abs_path = normalized_document_path(file_path)
383
- @document_mutex.synchronize { @open_documents.key?(abs_path) }
334
+ @document_mutex.synchronize { @document_store.open?(abs_path) }
384
335
  rescue ArgumentError
385
336
  false
386
337
  end
@@ -390,45 +341,29 @@ module Rubik
390
341
  end
391
342
 
392
343
  def all_diagnostics
393
- @mutex.synchronize do
394
- @diagnostics.dup
395
- end
344
+ @document_store.all_diagnostics
345
+ end
346
+
347
+ def diagnostics
348
+ @document_store.all_diagnostics
396
349
  end
397
350
 
398
351
  def diagnostics_snapshot_for(file_path)
399
352
  abs_path = normalized_document_path(file_path)
400
- @mutex.synchronize { diagnostics_snapshot_unlocked(abs_path) }
353
+ @document_store.diagnostic_snapshot(abs_path)
401
354
  rescue ArgumentError, TypeError
402
355
  { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
403
356
  end
404
357
 
405
358
  def wait_for_diagnostics(file_path, timeout: 0.2)
406
359
  abs_path = normalized_document_path(file_path)
407
- deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
408
- @mutex.synchronize do
409
- while @diagnostic_states[abs_path]&.dig(:state) == :pending
410
- remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
411
- break if remaining <= 0
412
-
413
- @cv.wait(@mutex, remaining)
414
- end
415
- diagnostics_snapshot_unlocked(abs_path)
416
- end
360
+ @document_store.await_diagnostics(abs_path, timeout)
417
361
  rescue ArgumentError, TypeError
418
362
  { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
419
363
  end
420
364
 
421
365
  def all_diagnostic_snapshots
422
- @mutex.synchronize do
423
- @diagnostic_states.each_with_object({}) do |(path, state), result|
424
- result[path] = {
425
- state: state[:state],
426
- diagnostics: (@diagnostics[path] || []).dup,
427
- document_version: state[:document_version],
428
- published_version: state[:published_version]
429
- }
430
- end
431
- end
366
+ @document_store.all_diagnostic_snapshots
432
367
  end
433
368
 
434
369
  private
@@ -504,18 +439,7 @@ module Rubik
504
439
  published_version = params["version"]
505
440
 
506
441
  @document_mutex.synchronize do
507
- current_version = @open_documents[norm_path]
508
- next if published_version.is_a?(Numeric) && current_version && published_version < current_version
509
-
510
- @mutex.synchronize do
511
- @diagnostics[norm_path] = diags
512
- @diagnostic_states[norm_path] = {
513
- state: published_version.is_a?(Numeric) ? :fresh : :unversioned,
514
- document_version: current_version,
515
- published_version: published_version
516
- }
517
- @cv.broadcast
518
- end
442
+ @document_store.record_diagnostics(norm_path, diags, published_version)
519
443
  end
520
444
  end
521
445
  end
@@ -626,8 +550,22 @@ module Rubik
626
550
  [prefix, *segments].compact.reject(&:empty?).join("/").downcase
627
551
  end
628
552
 
629
- def next_document_version
630
- @version_counter += 1
553
+ # The collapsed positional query (issue #406): definition, references
554
+ # and hover differ only in the LSP method string (references adds the
555
+ # includeDeclaration context). Adding typeDefinition/implementation is
556
+ # a one-line public wrapper around this helper.
557
+ def position_request(method, file_path, line, character, extra_params = {})
558
+ return { ok: false, error: "LSP server not running" } unless alive?
559
+
560
+ params = {
561
+ textDocument: { uri: document_uri(file_path) },
562
+ position: { line: line, character: character }
563
+ }.merge(extra_params)
564
+ res = send_request(method, params)
565
+
566
+ { ok: true, result: res }
567
+ rescue StandardError => e
568
+ { ok: false, error: e.message }
631
569
  end
632
570
 
633
571
  def resolved_binary(bin)
@@ -641,55 +579,24 @@ module Rubik
641
579
  Rubik::Platform.which(bin)
642
580
  end
643
581
 
644
- def pending_diagnostics(version)
645
- {
646
- state: :pending,
647
- document_version: version,
648
- published_version: nil
649
- }
650
- end
651
-
652
- def diagnostics_snapshot_unlocked(abs_path)
653
- state = @diagnostic_states[abs_path]
654
- {
655
- state: state ? state[:state] : :unavailable,
656
- diagnostics: (@diagnostics[abs_path] || []).dup,
657
- document_version: state && state[:document_version],
658
- published_version: state && state[:published_version]
659
- }
660
- end
661
-
662
582
  def send_document_change_unlocked(abs_path, content)
663
583
  return false unless @sync_capabilities[:change].to_i.positive?
664
-
665
- previous_version = @open_documents[abs_path]
666
- return false unless previous_version
667
- return true if @document_contents[abs_path] == content
668
-
669
- previous_content = @document_contents[abs_path]
670
- previous_state = @mutex.synchronize { @diagnostic_states[abs_path] }
671
- version = next_document_version
672
- @open_documents[abs_path] = version
673
- @document_contents[abs_path] = content
674
- @mutex.synchronize { @diagnostic_states[abs_path] = pending_diagnostics(version) }
675
-
676
- sent = send_notification("textDocument/didChange", {
677
- textDocument: {
678
- uri: document_uri(abs_path),
679
- version: version
680
- },
681
- # A text-only event is a full-document replacement,
682
- # which is valid for both Full and Incremental sync.
683
- contentChanges: [{ text: content }]
684
- })
685
- unless sent
686
- @open_documents[abs_path] = previous_version
687
- @document_contents[abs_path] = previous_content
688
- @mutex.synchronize do
689
- previous_state ? @diagnostic_states[abs_path] = previous_state : @diagnostic_states.delete(abs_path)
690
- end
584
+ return false unless @document_store.open?(abs_path)
585
+ return true if @document_store.content_for(abs_path) == content
586
+
587
+ @document_store.commit(abs_path) do
588
+ version = @document_store.next_version
589
+ @document_store.stage_version(abs_path, content, version)
590
+ send_notification("textDocument/didChange", {
591
+ textDocument: {
592
+ uri: document_uri(abs_path),
593
+ version: version
594
+ },
595
+ # A text-only event is a full-document replacement,
596
+ # which is valid for both Full and Incremental sync.
597
+ contentChanges: [{ text: content }]
598
+ })
691
599
  end
692
- sent
693
600
  end
694
601
 
695
602
  def normalize_sync_capabilities(value)
@@ -787,11 +694,7 @@ module Rubik
787
694
  end
788
695
 
789
696
  def sanitized_spawn_env
790
- env = {}
791
- ENV.each_key do |k|
792
- env[k] = nil if k.start_with?("BUNDLE_", "BUNDLER_") || ["RUBYOPT", "RUBYLIB"].include?(k)
793
- end
794
- env
697
+ Rubik::Platform.sanitized_spawn_env
795
698
  end
796
699
  end
797
700
  end
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module LSP
5
+ # Owns the four per-document state hashes of LSP::Client (issue #406):
6
+ #
7
+ # open_documents path -> open version
8
+ # document_contents path -> full text of the open document
9
+ # diagnostic_states path -> { state:, document_version:, published_version: }
10
+ # diagnostics path -> last published diagnostics array
11
+ #
12
+ # ## Lock discipline — the single home of the LSP lock-order invariant
13
+ #
14
+ # This class creates no locks of its own. The owning Client must hold its
15
+ # document mutex across EVERY call into this store — that is the only
16
+ # synchronization for open_documents / document_contents. The diagnostic
17
+ # hashes share the client's protocol mutex and its condition variable,
18
+ # injected here as diagnostic_mutex / diagnostics_cv: this store acquires
19
+ # the diagnostics mutex for every diagnostic read/write and broadcasts the
20
+ # condition variable under that same hold, so waiters can never miss a
21
+ # wakeup (Client#wait_for_diagnostics waits on the same pair).
22
+ #
23
+ # The resulting global acquisition order — and the only legal one — is:
24
+ #
25
+ # document_mutex -> write_mutex -> diagnostics mutex
26
+ #
27
+ # write_mutex sits between the two because transport writes happen inside
28
+ # document transitions and write_message's failure path takes the
29
+ # diagnostics mutex. No call site may re-derive this order: document state
30
+ # flows exclusively through this store.
31
+ class DocumentStore
32
+ # Per-path pre-transition state. A nil field means "path absent before
33
+ # the transition", so the restore deletes instead of writing.
34
+ Snapshot = Struct.new(:version, :content, :state, :diagnostics)
35
+
36
+ def initialize(diagnostic_mutex, diagnostics_cv)
37
+ @diagnostic_mutex = diagnostic_mutex
38
+ @diagnostics_cv = diagnostics_cv
39
+ @open_documents = {}
40
+ @document_contents = {}
41
+ @diagnostic_states = {}
42
+ @diagnostics = {}
43
+ @version_counter = 0
44
+ end
45
+
46
+ # Start-of-session reset (Client#start): drop every document and
47
+ # diagnostic trace and restart the version counter at zero.
48
+ def reset
49
+ @open_documents.clear
50
+ @document_contents.clear
51
+ @version_counter = 0
52
+ @diagnostic_mutex.synchronize do
53
+ @diagnostic_states.clear
54
+ @diagnostics.clear
55
+ end
56
+ end
57
+
58
+ def open?(path)
59
+ @open_documents.key?(path)
60
+ end
61
+
62
+ def content_for(path)
63
+ @document_contents[path]
64
+ end
65
+
66
+ def next_version
67
+ @version_counter += 1
68
+ end
69
+
70
+ # Forward mutation shared by didOpen (new document) and didChange (new
71
+ # version of an open document): publish the version/content pair and
72
+ # invalidate the diagnostic snapshot for the new version.
73
+ def stage_version(path, content, version)
74
+ @open_documents[path] = version
75
+ @document_contents[path] = content
76
+ @diagnostic_mutex.synchronize do
77
+ @diagnostic_states[path] = pending_diagnostics(version)
78
+ end
79
+ end
80
+
81
+ # Forward mutation of didClose: remove every trace of the document.
82
+ def evict(path)
83
+ @open_documents.delete(path)
84
+ @document_contents.delete(path)
85
+ @diagnostic_mutex.synchronize do
86
+ @diagnostic_states.delete(path)
87
+ @diagnostics.delete(path)
88
+ end
89
+ end
90
+
91
+ # The ONE transactional helper for document transitions (issue #406):
92
+ # capture the per-path snapshot, yield to the transport send, and on a
93
+ # falsy result restore the snapshot in place — failed sends never leave
94
+ # mutated document state behind. Returns the block's result. Must be
95
+ # called with the caller's document mutex held.
96
+ def commit(path)
97
+ before = snapshot(path)
98
+ sent = yield
99
+ restore(path, before) unless sent
100
+ sent
101
+ end
102
+
103
+ # Records a publishDiagnostics payload. Returns false when the payload
104
+ # is stale relative to the currently open version (the caller discards
105
+ # the message), true otherwise; waiters wake under the same
106
+ # diagnostics-mutex hold that installs the new state.
107
+ def record_diagnostics(path, published, version)
108
+ current = @open_documents[path]
109
+ return false if version.is_a?(Numeric) && current && version < current
110
+
111
+ @diagnostic_mutex.synchronize do
112
+ @diagnostics[path] = published
113
+ @diagnostic_states[path] = {
114
+ state: version.is_a?(Numeric) ? :fresh : :unversioned,
115
+ document_version: current,
116
+ published_version: version
117
+ }
118
+ @diagnostics_cv.broadcast
119
+ end
120
+ true
121
+ end
122
+
123
+ def all_diagnostics
124
+ @diagnostic_mutex.synchronize { @diagnostics.dup }
125
+ end
126
+
127
+ def diagnostic_snapshot(path)
128
+ @diagnostic_mutex.synchronize { build_snapshot(path) }
129
+ end
130
+
131
+ # Blocks while the document's diagnostic state is :pending, waking on
132
+ # each publish/broadcast, then returns the final snapshot.
133
+ def await_diagnostics(path, timeout)
134
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
135
+ @diagnostic_mutex.synchronize do
136
+ while @diagnostic_states[path]&.dig(:state) == :pending
137
+ remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
138
+ break if remaining <= 0
139
+
140
+ @diagnostics_cv.wait(@diagnostic_mutex, remaining)
141
+ end
142
+ build_snapshot(path)
143
+ end
144
+ end
145
+
146
+ def all_diagnostic_snapshots
147
+ @diagnostic_mutex.synchronize do
148
+ @diagnostic_states.each_with_object({}) do |(path, state), result|
149
+ result[path] = {
150
+ state: state[:state],
151
+ diagnostics: (@diagnostics[path] || []).dup,
152
+ document_version: state[:document_version],
153
+ published_version: state[:published_version]
154
+ }
155
+ end
156
+ end
157
+ end
158
+
159
+ private
160
+
161
+ # Reads the per-path state across all four hashes. open_documents /
162
+ # document_contents are read directly (the caller holds the document
163
+ # mutex); the diagnostic pair shares one diagnostics-mutex hold.
164
+ def snapshot(path)
165
+ state, published = @diagnostic_mutex.synchronize do
166
+ [@diagnostic_states[path], @diagnostics[path]]
167
+ end
168
+ Snapshot.new(@open_documents[path], @document_contents[path], state, published)
169
+ end
170
+
171
+ # Writes the captured pre-transition state back. nil fields mean the
172
+ # path was absent before, so the entry is deleted again.
173
+ def restore(path, before)
174
+ restore_entry(@open_documents, path, before.version)
175
+ restore_entry(@document_contents, path, before.content)
176
+ @diagnostic_mutex.synchronize do
177
+ restore_entry(@diagnostic_states, path, before.state)
178
+ restore_entry(@diagnostics, path, before.diagnostics)
179
+ end
180
+ end
181
+
182
+ def restore_entry(hash, path, value)
183
+ if value
184
+ hash[path] = value
185
+ else
186
+ hash.delete(path)
187
+ end
188
+ end
189
+
190
+ def build_snapshot(path)
191
+ state = @diagnostic_states[path]
192
+ {
193
+ state: state ? state[:state] : :unavailable,
194
+ diagnostics: (@diagnostics[path] || []).dup,
195
+ document_version: state && state[:document_version],
196
+ published_version: state && state[:published_version]
197
+ }
198
+ end
199
+
200
+ def pending_diagnostics(version)
201
+ {
202
+ state: :pending,
203
+ document_version: version,
204
+ published_version: nil
205
+ }
206
+ end
207
+ end
208
+ end
209
+ end
@@ -51,10 +51,11 @@ module Rubik
51
51
 
52
52
  attr_reader :name, :transport, :server_info, :server_capabilities, :protocol_version, :initialized
53
53
 
54
- def initialize(command, args: [], env: {}, name: "unnamed", root_dir: Dir.pwd, transport: nil, guard: nil,
55
- call_timeout: nil)
54
+ def initialize(command, args: [], env: {}, inherit_env: true, name: "unnamed", root_dir: Dir.pwd,
55
+ transport: nil, guard: nil, call_timeout: nil)
56
56
  @name = name.to_s
57
- @transport = transport || Transport::Stdio.new(command, args: args, env: env, root_dir: root_dir, guard: guard)
57
+ @transport = transport || Transport::Stdio.new(command, args: args, env: env, inherit_env: inherit_env,
58
+ root_dir: root_dir, guard: guard)
58
59
  # Session-wide tools/call timeout; nil (absent or not a positive
59
60
  # number) keeps the per-call DEFAULT_CALL_TIMEOUT fallback.
60
61
  @default_call_timeout = positive_number?(call_timeout) ? call_timeout : nil
@@ -80,6 +80,8 @@ module Rubik
80
80
  # so `!mcp add` output stays minimal (the knob is hand-written
81
81
  # config, not a CLI flag).
82
82
  server_hash["timeout"] = parsed_defn["timeout"] if parsed_defn["timeout"]
83
+ # Minimal round-trip rule (#429): only written when explicitly configured.
84
+ server_hash["inherit_env"] = parsed_defn["inherit_env"] if parsed_defn.key?("inherit_env")
83
85
  persist_res = @config.set("mcp.servers.#{name_str}", server_hash, persist: persist)
84
86
  end
85
87
 
@@ -120,6 +122,8 @@ module Rubik
120
122
  cmd = server_defn["command"] || server_defn[:command]
121
123
  args = server_defn["args"] || server_defn[:args] || []
122
124
  env = server_defn["env"] || server_defn[:env] || {}
125
+ inherit_env = server_defn.key?("inherit_env") ? server_defn["inherit_env"] : server_defn[:inherit_env] # credential policy (#429): only explicit false opts out
126
+ inherit_env_kwarg = inherit_env.nil? ? {} : { inherit_env: inherit_env } # strict-kwarg DI factories stay compatible
123
127
  base_ctx = @context || @runtime_manager&.context
124
128
  connect_timeout = resolve_connect_timeout(server_defn, timeout)
125
129
  # Config boundary (D2 mirror): validated for BOTH construction
@@ -129,13 +133,13 @@ module Rubik
129
133
 
130
134
  client =
131
135
  if @client_factory
132
- @client_factory.call(cmd, args: args, env: env, name: name_str, root_dir: @root_dir,
136
+ @client_factory.call(cmd, args: args, env: env, **inherit_env_kwarg, name: name_str, root_dir: @root_dir,
133
137
  guard: @guard || (base_ctx ? Rubik::Coeffects.get(base_ctx, Rubik::Coeffects::GUARD) : nil))
134
138
  else
135
139
  Rubik::MCP::Client.new(
136
140
  cmd,
137
141
  args: args,
138
- env: env,
142
+ env: env, inherit_env: inherit_env, # nil keeps the transport's inheriting default
139
143
  name: name_str,
140
144
  root_dir: @root_dir,
141
145
  # Resolved live at connect time: the composed kwarg wins, but a