rubik 0.13.0 → 0.15.0

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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +122 -1
  4. data/lib/rubik/adapter/node.rb +4 -1
  5. data/lib/rubik/ast/c_extractor.rb +1 -1
  6. data/lib/rubik/ast/fallback_cleaner.rb +6 -1
  7. data/lib/rubik/ast/graph.rb +38 -57
  8. data/lib/rubik/ast/graph_extensions.rb +57 -15
  9. data/lib/rubik/ast/ruby_extractor.rb +16 -6
  10. data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
  11. data/lib/rubik/buffer/manager.rb +38 -11
  12. data/lib/rubik/buffer/snapshot.rb +116 -28
  13. data/lib/rubik/cli/connect.rb +3 -5
  14. data/lib/rubik/config/defaults.rb +3 -7
  15. data/lib/rubik/config/env_guard.rb +43 -0
  16. data/lib/rubik/config/schema.rb +7 -1
  17. data/lib/rubik/config/scoped.rb +12 -5
  18. data/lib/rubik/config/secret_policy.rb +6 -5
  19. data/lib/rubik/config.rb +1 -1
  20. data/lib/rubik/connect/models_dev.rb +11 -0
  21. data/lib/rubik/connect/probes.rb +9 -2
  22. data/lib/rubik/connect/wizard.rb +117 -57
  23. data/lib/rubik/context/repo_context_reader.rb +8 -4
  24. data/lib/rubik/diagnostics/collector.rb +25 -2
  25. data/lib/rubik/llm/chunk_assembler.rb +12 -1
  26. data/lib/rubik/llm/client.rb +19 -0
  27. data/lib/rubik/llm/connection_pool.rb +7 -1
  28. data/lib/rubik/llm/formatter.rb +22 -2
  29. data/lib/rubik/llm/provider_key.rb +32 -0
  30. data/lib/rubik/llm/registry.rb +18 -2
  31. data/lib/rubik/llm/router.rb +27 -1
  32. data/lib/rubik/llm/session_header.rb +128 -0
  33. data/lib/rubik/llm/sse_parser.rb +57 -10
  34. data/lib/rubik/lsp/client.rb +244 -23
  35. data/lib/rubik/lsp/document_store.rb +6 -0
  36. data/lib/rubik/lsp/manager.rb +14 -0
  37. data/lib/rubik/lsp/transport.rb +34 -7
  38. data/lib/rubik/mcp/transport/stdio.rb +42 -4
  39. data/lib/rubik/orchestrator/continuation_manager.rb +68 -7
  40. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
  41. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
  42. data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
  43. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
  44. data/lib/rubik/orchestrator/goal_session.rb +14 -14
  45. data/lib/rubik/orchestrator/goal_wait.rb +15 -9
  46. data/lib/rubik/orchestrator/loop.rb +39 -39
  47. data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
  48. data/lib/rubik/orchestrator/step_executor.rb +17 -2
  49. data/lib/rubik/orchestrator/subagent/instance.rb +46 -5
  50. data/lib/rubik/orchestrator/subagent/lifecycle.rb +25 -25
  51. data/lib/rubik/orchestrator/subagent/messaging.rb +5 -1
  52. data/lib/rubik/orchestrator/subagent/narration.rb +2 -1
  53. data/lib/rubik/orchestrator/subagent/notices.rb +7 -3
  54. data/lib/rubik/orchestrator/subagent/questioner.rb +42 -4
  55. data/lib/rubik/orchestrator/subagent/reaper.rb +70 -20
  56. data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
  57. data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
  58. data/lib/rubik/orchestrator/subagent/supervisor.rb +21 -3
  59. data/lib/rubik/orchestrator/tool_call_id.rb +8 -4
  60. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +21 -5
  61. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +40 -5
  62. data/lib/rubik/orchestrator/turn_executor.rb +24 -8
  63. data/lib/rubik/platform/filesystem.rb +52 -24
  64. data/lib/rubik/platform/process.rb +32 -7
  65. data/lib/rubik/platform/sanitize.rb +17 -18
  66. data/lib/rubik/platform.rb +27 -0
  67. data/lib/rubik/plugin/storage.rb +34 -8
  68. data/lib/rubik/prompt/context_builder.rb +2 -1
  69. data/lib/rubik/prompt/doctrine.rb +3 -2
  70. data/lib/rubik/repair/loop.rb +22 -2
  71. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  72. data/lib/rubik/repl/commands/agent_execution.rb +20 -15
  73. data/lib/rubik/repl/commands/base.rb +7 -3
  74. data/lib/rubik/repl/commands/buffer.rb +21 -2
  75. data/lib/rubik/repl/commands/connect.rb +0 -1
  76. data/lib/rubik/repl/commands/lsp.rb +1 -1
  77. data/lib/rubik/repl/commands/plugins.rb +31 -31
  78. data/lib/rubik/repl/commands/session.rb +16 -6
  79. data/lib/rubik/repl/commands/workspace.rb +4 -4
  80. data/lib/rubik/repl/input_execution.rb +33 -32
  81. data/lib/rubik/repl/input_loop.rb +9 -4
  82. data/lib/rubik/repl/lifecycle.rb +1 -2
  83. data/lib/rubik/repl/notice_router.rb +11 -5
  84. data/lib/rubik/repl/paste_support.rb +1 -1
  85. data/lib/rubik/repl/prompt_grace.rb +3 -3
  86. data/lib/rubik/repl/service_catalog.rb +6 -2
  87. data/lib/rubik/repl/shell.rb +10 -2
  88. data/lib/rubik/repl/turn_recorder.rb +18 -4
  89. data/lib/rubik/runtime/context.rb +33 -27
  90. data/lib/rubik/runtime/manager.rb +23 -15
  91. data/lib/rubik/runtime/manifest_store.rb +7 -2
  92. data/lib/rubik/runtime/task_manager.rb +13 -10
  93. data/lib/rubik/sandbox/driver/base.rb +34 -0
  94. data/lib/rubik/sandbox/driver/bubblewrap.rb +60 -8
  95. data/lib/rubik/sandbox/driver/docker.rb +8 -0
  96. data/lib/rubik/sandbox/driver/podman.rb +8 -0
  97. data/lib/rubik/sandbox/driver/process.rb +29 -1
  98. data/lib/rubik/sandbox/runner.rb +9 -1
  99. data/lib/rubik/security/classifier/classifier.rb +147 -13
  100. data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
  101. data/lib/rubik/security/classifier/commands/git.yaml +27 -1
  102. data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
  103. data/lib/rubik/security/classifier/database.rb +2 -2
  104. data/lib/rubik/security/command_gate.rb +8 -1
  105. data/lib/rubik/security/path_policy.rb +28 -3
  106. data/lib/rubik/security/redactor.rb +62 -19
  107. data/lib/rubik/session/recorder.rb +27 -20
  108. data/lib/rubik/session/replayer.rb +29 -7
  109. data/lib/rubik/storage/store.rb +26 -5
  110. data/lib/rubik/tools/builtins/exec.rb +8 -2
  111. data/lib/rubik/tools/builtins/files.rb +8 -12
  112. data/lib/rubik/tools/builtins/grep.rb +3 -2
  113. data/lib/rubik/tools/builtins/interactive.rb +5 -2
  114. data/lib/rubik/tools/builtins/lsp.rb +59 -9
  115. data/lib/rubik/tools/builtins/send_message.rb +7 -2
  116. data/lib/rubik/tools/builtins/show_file.rb +1 -1
  117. data/lib/rubik/tools/builtins/synthesis.rb +4 -4
  118. data/lib/rubik/tools/preflight_runner.rb +4 -1
  119. data/lib/rubik/tools/result_normalizer.rb +1 -1
  120. data/lib/rubik/ui/banner.rb +3 -3
  121. data/lib/rubik/ui/dialog.rb +15 -8
  122. data/lib/rubik/ui/diff_viewer.rb +4 -4
  123. data/lib/rubik/ui/frame.rb +13 -3
  124. data/lib/rubik/ui/markdown_stream.rb +8 -2
  125. data/lib/rubik/ui/panel.rb +11 -11
  126. data/lib/rubik/ui/renderer.rb +10 -7
  127. data/lib/rubik/ui/spinner.rb +1 -1
  128. data/lib/rubik/ui/statusline.rb +3 -3
  129. data/lib/rubik/ui/theme.rb +1 -1
  130. data/lib/rubik/version.rb +1 -1
  131. metadata +4 -1
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "provider_key"
3
4
  require_relative "registry"
5
+ require_relative "session_header"
4
6
  require_relative "../storage/layout"
5
7
 
6
8
  module Rubik
@@ -44,7 +46,13 @@ module Rubik
44
46
  # Streaming variant. Resolves a provider via `#route` (or accepts an
45
47
  # explicit `provider:` opt) and delegates to its `#stream_chat`.
46
48
  # Yields Chunks. Without a block, returns an Enumerator.
47
- def stream_chat(messages, task_type: :auto, prompt: "", provider: nil, **opts, &block)
49
+ #
50
+ # `session_key:` is opt-in (issue #534): when given, the resolved
51
+ # provider's per-provider session header knob
52
+ # (`providers.<key>.session_header`) is applied with the same
53
+ # SessionHeader.apply! semantics as the orchestrator lanes. Without
54
+ # it, behavior is unchanged.
55
+ def stream_chat(messages, task_type: :auto, prompt: "", provider: nil, session_key: nil, **opts, &block)
48
56
  chosen_route = if provider
49
57
  resolved = provider.respond_to?(:stream_chat) ? provider : @registry.get(provider)
50
58
  { provider: resolved }
@@ -67,6 +75,16 @@ module Rubik
67
75
  )
68
76
  end
69
77
 
78
+ # Opt-in per-provider session-header injection for external callers
79
+ # (issue #534): same SessionHeader.apply! semantics as the orchestrator
80
+ # lanes — knob lookup, explicit-beats-automatic, and the key-absent-when-
81
+ # unset discipline all live there. Without the kwarg this is a no-op, so
82
+ # the pass-through stays byte-identical for existing callers.
83
+ if session_key
84
+ Rubik::LLM::SessionHeader.apply!(opts, provider_key: Rubik::LLM::ProviderKey.derive(chosen),
85
+ config: @config, session_key: session_key)
86
+ end
87
+
70
88
  return to_enum(:stream_chat, messages, task_type: task_type, prompt: prompt, provider: chosen, **opts) unless block # rubocop:disable Lint/ToEnumArguments -- provider: chosen bilinçli; enumerator routing anlik görüntüsünü tasir
71
89
 
72
90
  chosen.stream_chat(messages, opts, &block)
@@ -75,6 +93,14 @@ module Rubik
75
93
  private
76
94
 
77
95
  def determine_role(task_type, prompt, mode)
96
+ # router.mode fast/advanced (issue #588): schema-valid enum values
97
+ # that previously silently degraded to auto classification. A sensible
98
+ # forced mapping exists — each mode maps to the identically named role
99
+ # — so instead of restricting the schema, honor the mode as a forced
100
+ # role, bypassing keyword classification.
101
+ return :fast if mode == "fast"
102
+ return :advanced if mode == "advanced"
103
+
78
104
  case task_type.to_sym
79
105
  when :fast, :quick, :repair, :diagnose
80
106
  :fast
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require_relative "../coeffects"
5
+ require_relative "../log"
6
+ require_relative "../platform"
7
+ require_relative "../storage/layout"
8
+
9
+ module Rubik
10
+ module LLM
11
+ # Per-provider dynamic session header (providers.<name>.session_header):
12
+ # gateways like opencode's Go endpoint hard-400 every POST without a
13
+ # session header (MissingSessionID) and ask for ONE stable id per
14
+ # conversation carried on EVERY request — including auxiliary lanes
15
+ # (subagents, repair), where dropping it is the classic mid-conversation
16
+ # failure mode. The id is a UUID derived deterministically from the
17
+ # session recorder identity (Recorder#session_id), so resume
18
+ # (switch_session_file re-targets the same file) and restarts keep the
19
+ # same identity while distinct conversations stay distinct. Pure
20
+ # functions; knob and identity are re-read per request (config
21
+ # reactivity preserved) and the module holds no state. Lanes reach it
22
+ # through ProviderResolver#apply_session_header!, so the module itself
23
+ # stays lane-agnostic.
24
+ module SessionHeader
25
+ module_function
26
+
27
+ # Domain-separation salt: the same basename must never collide across
28
+ # purposes or deployments.
29
+ DERIVATION_SALT = "rubik.llm.session_header/v1"
30
+
31
+ # Conversation identity string -> RFC 4122-shaped UUID (SHA-256, first
32
+ # 16 bytes stamped with version-5 / RFC-variant bits). One-way: leaks
33
+ # nothing about the file name. nil for a blank identity.
34
+ def uuid_for(session_key)
35
+ key = session_key.to_s
36
+ return nil if key.empty?
37
+
38
+ bytes = Digest::SHA256.digest("#{DERIVATION_SALT}:#{key}").unpack("C16")
39
+ bytes[6] = (bytes[6] & 0x0F) | 0x50 # version 5
40
+ bytes[8] = (bytes[8] & 0x3F) | 0x80 # RFC 4122 variant
41
+ hex = bytes.pack("C*").unpack1("H*")
42
+ [hex[0, 8], hex[8, 4], hex[12, 4], hex[16, 4], hex[20, 12]].join("-")
43
+ end
44
+
45
+ # Conversation identity: the session recorder file basename without
46
+ # extension. Recorder#session_id is the SSOT for that spelling; the
47
+ # basename fallback covers plain test doubles. nil when no
48
+ # recorder-backed identity exists; callers then SKIP injection.
49
+ def session_key_from_recorder(recorder)
50
+ return recorder.session_id if recorder.respond_to?(:session_id)
51
+
52
+ return nil unless recorder.respond_to?(:session_file)
53
+
54
+ file = recorder.session_file
55
+ return nil if file.nil? || file.to_s.empty?
56
+
57
+ File.basename(file.to_s, ".jsonl")
58
+ end
59
+
60
+ # Identity from a runtime context: the SHELL coeffect (parent-chain
61
+ # walk) is THE conversation anchor — the root loop reads it through
62
+ # its runtime manager, subagents through their child context, so both
63
+ # lanes share one conversation id. nil-safe end to end.
64
+ def session_key_from_context(ctx)
65
+ shell = Rubik::Coeffects.get(ctx, Rubik::Coeffects::SHELL)
66
+ return nil unless shell.respond_to?(:recorder)
67
+
68
+ session_key_from_recorder(shell.recorder)
69
+ end
70
+
71
+ # Knob: providers.<key>.session_header — the header NAME to inject.
72
+ # Deliberately provider-scoped ONLY (no llm.session_header global
73
+ # spelling): a global would silently route every provider through one
74
+ # header name. Same two-arg Layout read as ProviderResolver#llm_config
75
+ # (safe_config's false-coercion / KeyError traps do not apply here).
76
+ def header_name(config, provider_key)
77
+ return nil unless provider_key.is_a?(String) && !provider_key.empty?
78
+
79
+ name = Rubik::Storage::Layout.config_get(config, "providers.#{provider_key}.session_header", nil)
80
+ name.is_a?(String) && !name.strip.empty? ? name.strip : nil
81
+ end
82
+
83
+ # Header names the operator configured EXPLICITLY (downcased) — both
84
+ # scopes the Registry bakes into the provider's @headers layer:
85
+ # providers.<key>.headers and the global llm.headers.
86
+ def explicit_header_names(config, provider_key)
87
+ names = []
88
+ if provider_key.is_a?(String) && !provider_key.empty?
89
+ scoped = Rubik::Storage::Layout.config_get(config, "providers.#{provider_key}.headers", nil)
90
+ names += scoped.keys if scoped.is_a?(Hash)
91
+ end
92
+ global = Rubik::Storage::Layout.config_get(config, "llm.headers", nil)
93
+ names += global.keys if global.is_a?(Hash)
94
+ names.map { |k| k.to_s.downcase }
95
+ end
96
+
97
+ # THE injection core. Mutates +opts+ in place and returns it. The
98
+ # :headers key stays ABSENT unless a header is actually added — the
99
+ # "key absent when unset" discipline of the reasoning_effort bridge.
100
+ # Explicit beats automatic: if the operator already set the same header
101
+ # name — in providers.<key>.headers, global llm.headers, or per-call
102
+ # opts[:headers] (adapters merge opts LAST, so blind injection would
103
+ # clobber their value) — auto-injection skips for that request.
104
+ def apply!(opts, provider_key:, config:, session_key:)
105
+ name = header_name(config, provider_key)
106
+ return opts if name.nil?
107
+
108
+ key = session_key.to_s
109
+ if key.empty?
110
+ # A set knob with an unresolvable identity is a STANDING
111
+ # misconfiguration — the gateway will hard-400 mid-conversation,
112
+ # so this must not stay silent (silence-forbidden doctrine).
113
+ Rubik::Log.warn(:llm, "[SessionHeader] providers.#{provider_key}.session_header=#{name} is set, " \
114
+ "but no session identity is available on this lane; header not injected")
115
+ return opts
116
+ end
117
+
118
+ return opts if explicit_header_names(config, provider_key).include?(name.downcase)
119
+
120
+ existing = opts[:headers].is_a?(Hash) ? opts[:headers] : {}
121
+ return opts if existing.any? { |k, _| k.to_s.downcase == name.downcase }
122
+
123
+ opts[:headers] = existing.merge(name => uuid_for(key))
124
+ opts
125
+ end
126
+ end
127
+ end
128
+ end
@@ -22,28 +22,46 @@ module Rubik
22
22
  @terminator_bin = terminator.is_a?(String) ? terminator.b : terminator
23
23
  @sse_mode = @terminator == SSE_TERMINATOR || @terminator == SSE_EVENT_SEP
24
24
  @buffer = String.new(encoding: Encoding::ASCII_8BIT)
25
+ # Full-body fallback (issue #556): a provider that ignores `stream:
26
+ # true` answers with one plain JSON body carrying no SSE fields.
27
+ # Remember that body until flush so the turn is not silently empty.
28
+ @raw_body = String.new(encoding: Encoding::ASCII_8BIT)
29
+ @any_event = false
25
30
  end
26
31
 
27
32
  # Feed raw bytes; returns an array of complete events parsed from this
28
33
  # chunk. Partial events stay in the buffer.
29
34
  def feed(chunk_bytes)
30
- @buffer << chunk_bytes.to_s.b
35
+ bytes = chunk_bytes.to_s.b
36
+ @buffer << bytes
37
+ # Stop remembering once a real event surfaced — from then on this is
38
+ # a genuine SSE stream and the fallback must never fire.
39
+ @raw_body << bytes if @sse_mode && !@any_event
31
40
  drain_complete_events(final: false)
32
41
  end
33
42
 
34
43
  # Flush any trailing event (no trailing terminator) and reset.
35
44
  def flush
36
- return [] if @buffer.empty?
45
+ events = []
46
+ unless @buffer.empty? && @raw_body.empty?
47
+ events = drain_complete_events(final: true)
37
48
 
38
- events = drain_complete_events(final: true)
49
+ unless @buffer.empty?
50
+ remaining = @buffer.dup
51
+ parsed = parse_event(remaining)
52
+ events << parsed if parsed
53
+ @any_event = true if parsed
54
+ end
39
55
 
40
- unless @buffer.empty?
41
- remaining = @buffer.dup
42
- @buffer = String.new(encoding: Encoding::ASCII_8BIT)
43
- parsed = parse_event(remaining)
44
- events << parsed if parsed
56
+ # No SSE event ever surfaced but the body carried real content: emit
57
+ # the whole body as one message event so formatters can parse it and
58
+ # the turn reports text instead of an empty response.
59
+ events << { type: "message", data: full_body_data(@raw_body) } if @sse_mode && !@any_event && substantive_body?(@raw_body)
45
60
  end
46
61
 
62
+ @buffer = String.new(encoding: Encoding::ASCII_8BIT)
63
+ @raw_body = String.new(encoding: Encoding::ASCII_8BIT)
64
+ @any_event = false
47
65
  events
48
66
  end
49
67
 
@@ -64,19 +82,48 @@ module Rubik
64
82
  event_block = @buffer.byteslice(0, m.begin(0))
65
83
  @buffer = @buffer.byteslice(m.end(0)..-1) || String.new(encoding: Encoding::ASCII_8BIT)
66
84
  parsed = parse_event(event_block)
67
- events << parsed if parsed
85
+ next unless parsed
86
+
87
+ events << parsed
88
+ @any_event = true
68
89
  end
69
90
  else
70
91
  while (idx = @buffer.index(@terminator_bin))
71
92
  event_block = @buffer.byteslice(0, idx)
72
93
  @buffer = @buffer.byteslice((idx + @terminator_bin.bytesize)..-1) || String.new(encoding: Encoding::ASCII_8BIT)
73
94
  parsed = parse_event(event_block)
74
- events << parsed if parsed
95
+ next unless parsed
96
+
97
+ events << parsed
98
+ @any_event = true
75
99
  end
76
100
  end
77
101
  events
78
102
  end
79
103
 
104
+ # True when the raw body holds at least one line that is neither blank
105
+ # nor an SSE comment. Pure keepalive/comment bodies must not trigger the
106
+ # full-body fallback (they are not a provider answer).
107
+ def substantive_body?(raw)
108
+ utf8 = decode_utf8(raw)
109
+ utf8.each_line do |line|
110
+ stripped = line.strip
111
+ next if stripped.empty? || stripped.start_with?(":")
112
+
113
+ return true
114
+ end
115
+ false
116
+ end
117
+
118
+ def full_body_data(raw)
119
+ decode_utf8(raw).sub(/\s+\z/, "")
120
+ end
121
+
122
+ def decode_utf8(bytes)
123
+ text = bytes.to_s.dup.force_encoding(Encoding::UTF_8)
124
+ text.valid_encoding? ? text : text.scrub
125
+ end
126
+
80
127
  def parse_event(block)
81
128
  return nil if block.nil?
82
129
 
@@ -33,6 +33,13 @@ module Rubik
33
33
  "vue" => "vue-language-server --stdio"
34
34
  }.freeze
35
35
 
36
+ # Bounds every textDocument/diagnostic pull. The reader thread of the
37
+ # pull answers through the transport's response correlation; if the
38
+ # server stalls, the pull times out here, the snapshot stays :pending,
39
+ # and waiters fall through their own bounded timeout. Keeps the
40
+ # lsp_diagnostics tool from ever hanging on a wedged server.
41
+ DIAGNOSTIC_PULL_TIMEOUT = 5
42
+
36
43
  attr_reader :language, :server_cmd, :root_dir, :server_capabilities,
37
44
  :sync_capabilities
38
45
  # The JSON-RPC transport delegate (issue #470). Exposed read-only as a
@@ -70,6 +77,17 @@ module Rubik
70
77
  source: :not_advertised, open_close: false, change: 0,
71
78
  save: false, include_text: false
72
79
  }.freeze
80
+ # Populated from the initialize response (see start): true when the
81
+ # server advertises `diagnosticProvider`, meaning diagnostics must be
82
+ # pulled via textDocument/diagnostic instead of (or in addition to)
83
+ # push publishDiagnostics.
84
+ @pull_diagnostics = false
85
+ # In-flight pull registry: path -> document version the outstanding
86
+ # textDocument/diagnostic request was issued for. Doubles as a
87
+ # dedupe guard (one pull per path+version) so polling loops cannot
88
+ # spawn unbounded threads.
89
+ @pull_in_flight = {}
90
+ @pull_mutex = Mutex.new
73
91
  end
74
92
 
75
93
  def binary_name
@@ -92,6 +110,7 @@ module Rubik
92
110
  @document_store.reset
93
111
  end
94
112
  @transport.reset_correlation
113
+ @pull_mutex.synchronize { @pull_in_flight.clear }
95
114
 
96
115
  cmd_parts = @server_argv.dup
97
116
  bin = cmd_parts.first
@@ -136,6 +155,7 @@ module Rubik
136
155
 
137
156
  @server_capabilities = init_res.is_a?(Hash) ? (init_res["capabilities"] || {}) : {}
138
157
  @sync_capabilities = normalize_sync_capabilities(@server_capabilities["textDocumentSync"])
158
+ @pull_diagnostics = pull_diagnostics_advertised?(@server_capabilities)
139
159
  unless send_notification("initialized", {})
140
160
  stop
141
161
  return false
@@ -177,13 +197,20 @@ module Rubik
177
197
  return unless running || @transport.resources?
178
198
 
179
199
  begin
180
- send_request("shutdown", {}, timeout: 2) if running
181
- send_notification("exit", {}) if running
200
+ if running
201
+ send_request("shutdown", {}, timeout: 2)
202
+ send_notification("exit", {})
203
+ end
182
204
  rescue StandardError
183
- # ignore
205
+ # A failed/wedged shutdown write is not a reason to leave the server
206
+ # alive — bounded writes make this a deadline rather than a hang.
207
+ ensure
208
+ # Always retract the transport, even if an Interrupt/non-StandardError
209
+ # unwinds the graceful shutdown: kill_tree is the only thing that can
210
+ # unblock a writer parked on a full stdin pipe.
211
+ @transport.shutdown
212
+ @pull_mutex.synchronize { @pull_in_flight.clear }
184
213
  end
185
-
186
- @transport.shutdown
187
214
  end
188
215
  alias dispose stop
189
216
 
@@ -216,14 +243,16 @@ module Rubik
216
243
  @document_store.commit(abs_path) do
217
244
  version = @document_store.next_version
218
245
  @document_store.stage_version(abs_path, content, version)
219
- send_notification("textDocument/didOpen", {
220
- textDocument: {
221
- uri: document_uri(abs_path),
222
- languageId: language_id.to_s,
223
- version: version,
224
- text: content
225
- }
226
- })
246
+ sent = send_notification("textDocument/didOpen", {
247
+ textDocument: {
248
+ uri: document_uri(abs_path),
249
+ languageId: language_id.to_s,
250
+ version: version,
251
+ text: content
252
+ }
253
+ })
254
+ send_pull_diagnostics_unlocked(abs_path, version) if sent && pull_diagnostics?
255
+ sent
227
256
  end
228
257
  end
229
258
  rescue StandardError => e
@@ -296,13 +325,22 @@ module Rubik
296
325
 
297
326
  def diagnostics_snapshot_for(file_path)
298
327
  abs_path = normalized_document_path(file_path)
299
- @document_store.diagnostic_snapshot(abs_path)
328
+ snapshot = @document_store.diagnostic_snapshot(abs_path)
329
+ refresh_diagnostics_via_pull(abs_path) if pull_diagnostics? && snapshot[:state] == :pending
330
+ snapshot
300
331
  rescue ArgumentError, TypeError
301
332
  { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
302
333
  end
303
334
 
304
335
  def wait_for_diagnostics(file_path, timeout: 0.2)
305
336
  abs_path = normalized_document_path(file_path)
337
+ # Issue a pull whenever a pull-capable server has the document open
338
+ # and we do not yet hold a versioned answer for its current version
339
+ # (:pending) — repeated calls for an already-answered version are
340
+ # deduplicated by the in-flight registry, and :fresh results from a
341
+ # combined push+pull server are returned as-is (freshest data wins).
342
+ refresh_diagnostics_via_pull(abs_path) if pull_diagnostics? && @document_store.open?(abs_path) &&
343
+ @document_store.diagnostic_snapshot(abs_path)[:state] == :pending
306
344
  @document_store.await_diagnostics(abs_path, timeout)
307
345
  rescue ArgumentError, TypeError
308
346
  { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
@@ -312,8 +350,147 @@ module Rubik
312
350
  @document_store.all_diagnostic_snapshots
313
351
  end
314
352
 
353
+ # Workspace-wide pull refresh (issue #583): re-issue pulls for every
354
+ # document whose snapshot is still :pending. Bounded by the pull
355
+ # timeout on the client side and by the caller's wait on the tool
356
+ # side; a wedged server leaves snapshots :pending rather than
357
+ # hanging the surface.
358
+ def refresh_pending_diagnostic_snapshots
359
+ return unless pull_diagnostics?
360
+
361
+ @document_store.all_diagnostic_snapshots.each do |path, snapshot|
362
+ refresh_diagnostics_via_pull(path) if snapshot[:state] == :pending
363
+ end
364
+ nil
365
+ end
366
+
315
367
  private
316
368
 
369
+ # ---- Pull diagnostics (issue #583). Servers that advertise
370
+ # ---- `diagnosticProvider` (e.g. typeprof, the DEFAULT ruby server,
371
+ # ---- with ruby-lsp the configured fallback) never push
372
+ # ---- publishDiagnostics; without an explicit textDocument/diagnostic
373
+ # ---- request the document stays :pending forever and every
374
+ # ---- diagnostics surface reports nothing. Pulls ride the
375
+ # ---- transport's own request correlation: send_request blocks its
376
+ # ---- own thread until the response is matched, so no response can
377
+ # ---- dead-letter, and results are version-guarded on apply so a
378
+ # ---- stale async pull can never clobber a newer snapshot.
379
+
380
+ def pull_diagnostics?
381
+ @pull_diagnostics == true
382
+ end
383
+
384
+ # diagnosticProvider may be a boolean or an options object.
385
+ def pull_diagnostics_advertised?(capabilities)
386
+ provider = capabilities["diagnosticProvider"]
387
+ provider == true || provider.is_a?(Hash)
388
+ end
389
+
390
+ # Fire-and-forget pull, called with @document_mutex held (didOpen /
391
+ # didChange paths). Deduplicated per (path, version) so tight polling
392
+ # loops (diagnostics_snapshot_for) cannot spawn unbounded threads.
393
+ def send_pull_diagnostics_unlocked(abs_path, version)
394
+ return false unless alive?
395
+ return false if @pull_mutex.synchronize { @pull_in_flight[abs_path] == version }
396
+
397
+ @pull_mutex.synchronize { @pull_in_flight[abs_path] = version }
398
+ uri = document_uri(abs_path)
399
+ Thread.new do
400
+ result = @transport.send_request(
401
+ "textDocument/diagnostic",
402
+ { textDocument: { uri: uri } },
403
+ timeout: DIAGNOSTIC_PULL_TIMEOUT
404
+ )
405
+ apply_pull_diagnostics_result(abs_path, version, result)
406
+ rescue StandardError => e
407
+ # Timeout / transport failure: the snapshot simply stays :pending
408
+ # and waiters fall through their bounded timeout. Never raise
409
+ # out of an unowned thread.
410
+ Rubik::Log.warn(:lsp, "diagnostic pull failed for #{abs_path}: #{e.class}: #{e.message}")
411
+ ensure
412
+ @pull_mutex.synchronize do
413
+ @pull_in_flight.delete(abs_path) if @pull_in_flight[abs_path] == version
414
+ end
415
+ end
416
+ true
417
+ end
418
+
419
+ # Refresh entry used by wait_for_diagnostics / diagnostics_snapshot_for
420
+ # when the snapshot is still :pending (covers lost or timed-out pulls).
421
+ def refresh_diagnostics_via_pull(abs_path)
422
+ @document_mutex.synchronize do
423
+ return unless @document_store.open?(abs_path)
424
+
425
+ version = @document_store.version_for(abs_path)
426
+ send_pull_diagnostics_unlocked(abs_path, version) if version
427
+ end
428
+ nil
429
+ rescue StandardError => e
430
+ Rubik::Log.warn(:lsp, "diagnostic refresh failed for #{abs_path}: #{e.class}: #{e.message}")
431
+ nil
432
+ end
433
+
434
+ # Applies a textDocument/diagnostic response. Non-hash results carry
435
+ # no items to install; `kind: "unchanged"` confirms previously recorded
436
+ # diagnostics remain valid for the current document version. Payloads are
437
+ # applied only when the document still sits at the pulled version, so
438
+ # stale async results (a slow server answering after a further edit) are
439
+ # discarded instead of clobbering the newer snapshot. relatedDocuments
440
+ # entries (cross-file diagnostics for OTHER documents, issue #585) are
441
+ # recorded through the same version-guarded path; entries for documents
442
+ # rubik never opened are skipped without erroring.
443
+ def apply_pull_diagnostics_result(abs_path, pulled_version, result)
444
+ return unless result.is_a?(Hash)
445
+
446
+ @document_mutex.synchronize do
447
+ current = @document_store.version_for(abs_path)
448
+ return unless current == pulled_version
449
+
450
+ if result["kind"] == "unchanged"
451
+ existing = @document_store.diagnostic_snapshot(abs_path)[:diagnostics]
452
+ @document_store.record_diagnostics(abs_path, existing, current)
453
+ else
454
+ items = result["items"].is_a?(Array) ? result["items"] : []
455
+ @document_store.record_diagnostics(abs_path, items, current)
456
+ end
457
+
458
+ record_related_documents(result["relatedDocuments"])
459
+ end
460
+ nil
461
+ rescue StandardError => e
462
+ Rubik::Log.warn(:lsp, "diagnostic pull result dropped for #{abs_path}: #{e.class}: #{e.message}")
463
+ nil
464
+ end
465
+
466
+ # Records cross-file diagnostics from a pull response's relatedDocuments
467
+ # map (DocumentUri -> diagnostic report, issue #585). Each entry goes
468
+ # through the same version-guarded record_diagnostics path as the pulled
469
+ # document; documents rubik never opened (or closed since) are skipped
470
+ # without erroring — a pull response may legitimately reference files
471
+ # outside this client's document store.
472
+ def record_related_documents(related)
473
+ return unless related.is_a?(Hash)
474
+
475
+ related.each do |uri, report|
476
+ next unless report.is_a?(Hash)
477
+
478
+ begin
479
+ raw_path = Rubik::Platform.from_file_uri(uri)
480
+ path = normalized_document_path(raw_path)
481
+ next unless @document_store.open?(path)
482
+
483
+ version = @document_store.version_for(path)
484
+ next unless version
485
+
486
+ items = report["items"].is_a?(Array) ? report["items"] : []
487
+ @document_store.record_diagnostics(path, items, version)
488
+ rescue StandardError => e
489
+ Rubik::Log.warn(:lsp, "relatedDocument entry skipped for #{uri}: #{e.class}: #{e.message}")
490
+ end
491
+ end
492
+ end
493
+
317
494
  # ---- Delegates to the transport (issue #470). These thin shims keep
318
495
  # ---- the Client's internal call sites and per-instance test stubs
319
496
  # ---- working unchanged; the implementations live on Transport.
@@ -427,21 +604,65 @@ module Rubik
427
604
  return false unless @document_store.open?(abs_path)
428
605
  return true if @document_store.content_for(abs_path) == content
429
606
 
607
+ old_content = @document_store.content_for(abs_path)
430
608
  @document_store.commit(abs_path) do
431
609
  version = @document_store.next_version
432
610
  @document_store.stage_version(abs_path, content, version)
433
- send_notification("textDocument/didChange", {
434
- textDocument: {
435
- uri: document_uri(abs_path),
436
- version: version
437
- },
438
- # A text-only event is a full-document replacement,
439
- # which is valid for both Full and Incremental sync.
440
- contentChanges: [{ text: content }]
441
- })
611
+ sent = send_notification("textDocument/didChange", {
612
+ textDocument: {
613
+ uri: document_uri(abs_path),
614
+ version: version
615
+ },
616
+ contentChanges: build_content_changes(old_content, content)
617
+ })
618
+ send_pull_diagnostics_unlocked(abs_path, version) if sent && pull_diagnostics?
619
+ sent
442
620
  end
443
621
  end
444
622
 
623
+ # Spec 3.17: Full sync (kind 1) replaces the whole document with a
624
+ # range-less change event. Incremental sync (kind 2) REQUIRES a range —
625
+ # a range-less event is invalid there, and servers that honor the spec
626
+ # (e.g. typeprof, the default ruby server) drop or misapply it. For
627
+ # incremental servers we send ONE edit spanning the entire OLD document,
628
+ # which is semantically a full replacement but structurally spec-valid.
629
+ # Character offsets are UTF-16 code units per the LSP spec.
630
+ def build_content_changes(old_content, new_content)
631
+ return [{ text: new_content }] unless @sync_capabilities[:change].to_i == 2
632
+ return [{ text: new_content }] if old_content.nil?
633
+
634
+ old_content = old_content.to_s
635
+ [{
636
+ range: {
637
+ start: { line: 0, character: 0 },
638
+ end: end_position_for(old_content)
639
+ },
640
+ text: new_content
641
+ }]
642
+ end
643
+
644
+ # LSP Position for the exclusive end of content: last line index and
645
+ # the line's length in UTF-16 code units (the spec's offset unit —
646
+ # Ruby String#length counts characters, which diverges for astral-plane
647
+ # code points that encode as surrogate pairs). Empty or nil content
648
+ # resolves to line 0 character 0 (preventing invalid negative line indices).
649
+ def end_position_for(content)
650
+ return { line: 0, character: 0 } if content.nil? || content.empty?
651
+
652
+ lines = content.split(/\r\n|\n|\r/, -1)
653
+ last_line = [lines.length - 1, 0].max
654
+ last_text = lines.last.to_s
655
+ { line: last_line, character: utf16_length(last_text) }
656
+ end
657
+
658
+ def utf16_length(text)
659
+ str = text.to_s
660
+ str = str.encode("UTF-16LE", invalid: :replace, undef: :replace) unless str.encoding == Encoding::UTF_16LE
661
+ str.bytesize / 2
662
+ rescue StandardError
663
+ text.to_s.length
664
+ end
665
+
445
666
  def normalize_sync_capabilities(value)
446
667
  case value
447
668
  when Numeric
@@ -63,6 +63,12 @@ module Rubik
63
63
  @document_contents[path]
64
64
  end
65
65
 
66
+ # The version the server currently knows for an open document, nil
67
+ # when the document is not open. Callers hold the document mutex.
68
+ def version_for(path)
69
+ @open_documents[path]
70
+ end
71
+
66
72
  def next_version
67
73
  @version_counter += 1
68
74
  end
@@ -301,6 +301,20 @@ module Rubik
301
301
  result
302
302
  end
303
303
 
304
+ # Re-issues textDocument/diagnostic pulls for every document still
305
+ # pending (issue #583). Delegates per client; bounded by the client's
306
+ # pull timeout so this can never hang the caller.
307
+ def refresh_pending_diagnostic_snapshots
308
+ @monitor.synchronize do
309
+ @clients.each_value do |client|
310
+ next unless client.running
311
+
312
+ client.refresh_pending_diagnostic_snapshots
313
+ end
314
+ end
315
+ nil
316
+ end
317
+
304
318
  def hover(file_path, line, character)
305
319
  client = client_for(file_path)
306
320
  return { ok: false, error: inactive_client_error(file_path, client) } unless client&.running