Class: DebugMcp::Tools::EvaluateCode

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/debug_mcp/tools/evaluate_code.rb

Class Method Summary collapse

Class Method Details

.call(code:, session_id: nil, acknowledge_mutations: nil, server_context:) ⇒ Object



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
# File 'lib/debug_mcp/tools/evaluate_code.rb', line 46

def call(code:, session_id: nil, acknowledge_mutations: nil, server_context:)
  manager = server_context[:session_manager]
  client = manager.client(session_id)
  client.auto_repause!

  # Acknowledge mutation warnings for this session if requested
  if acknowledge_mutations
    manager.acknowledge_warning(session_id, :mutation_operations)
  end

  # Layer 3: Code safety analysis — warn about dangerous operations
  safety_warnings = CodeSafetyAnalyzer.analyze(code)
  acknowledged = manager.acknowledged_warnings(session_id)
  safety_warnings = CodeSafetyAnalyzer.filter_acknowledged(safety_warnings, acknowledged)
  warning_text = CodeSafetyAnalyzer.format_warnings(safety_warnings)

  # In trap context (e.g., after SIGURG-based repause), `require` and
  # Mutex operations hang. Use a simplified evaluation path that avoids
  # stdout redirect (which needs `require "stringio"`).
  if client.trap_context
    return call_in_trap_context(client, code, warning_text: warning_text)
  end

  stdout_redirected = false
  suspended_catch_bps = []

  begin
    # Proactive recovery: if a previous timeout left $stdout redirected
    # to StringIO, restore it to the original STDOUT constant (immutable).
    begin
      client.send_command('$stdout = STDOUT if $stdout != STDOUT; nil')
    rescue DebugMcp::Error
      # Best-effort
    end

    # Proactive recovery: if a previous timeout left catch breakpoints
    # suspended (deleted but not restored), recreate them now.
    if client.suspended_catch_bps&.any?
      restore_catch_breakpoints(client, client.suspended_catch_bps)
      client.suspended_catch_bps = []
    end

    # Temporarily disable catch breakpoints to prevent them from
    # firing on exceptions raised during code evaluation
    suspended_catch_bps = suspend_catch_breakpoints(client)

    # Redirect $stdout to capture puts/print output.
    # Use StringIO directly (always available in debug gem sessions)
    # instead of `require "stringio"` which hangs in trap context.
    client.send_command(
      '$__debug_mcp_cap = StringIO.new; $stdout = $__debug_mcp_cap',
    )
    stdout_redirected = true

    # Evaluate user code (pp formats the return value)
    # The debug gem protocol is line-based, so multi-line code must be
    # encoded into a single line to avoid breaking the protocol.
    # The code is wrapped in begin/rescue to capture exceptions in
    # $__debug_mcp_err, allowing us to distinguish errors from normal nil.
    output = client.send_command(build_eval_command(code))

    # Restore $stdout and read captured output in a single round-trip
    captured = restore_and_read_stdout(client)
    stdout_redirected = false

    # Check if evaluation raised an exception
    err_info = read_eval_error(client)

    if err_info
      text = "Error: #{err_info}"
      text += "\n\nDebugger output:\n#{output}" if output && !output.strip.empty? && output.strip != "nil"
      text += "\n\nCaptured stdout:\n#{captured}" if captured
      if err_info.include?("ThreadError")
        text += "\n\nThis error occurs in signal trap context (common when connecting to Puma/Rails via SIGURG).\n" \
                "Thread operations (Mutex, DB queries, model autoloading) are not available here.\n\n" \
                "To escape trap context:\n" \
                "  1. set_breakpoint on a line in your controller/action\n" \
                "  2. trigger_request to send an HTTP request (this auto-resumes the process)\n" \
                "  3. Once stopped at the breakpoint, all operations work normally"
      end
    elsif captured
      # pp() writes to $stdout, so captured stdout often contains
      # just the pp output of the return value (identical content).
      # Only show "Captured stdout" when it has additional content
      # (e.g., from puts/print in the evaluated code).
      return_val = output.strip.sub(/\A=> /, "")
      if captured.strip == return_val.strip
        text = output
      else
        text = "Return value:\n#{output}\n\nCaptured stdout:\n#{captured}"
      end
    else
      text = output
    end
    text = append_frame_info(client, text)
    text = append_trap_context_note(client, text)
    text = append_pending_http_note(client, text)
    text = prepend_warning(text, warning_text)
    MCP::Tool::Response.new([{ type: "text", text: text }])
  rescue DebugMcp::TimeoutError => e
    MCP::Tool::Response.new([{ type: "text", text: "Error: #{e.message}\n\n" \
      "The code may be taking too long to execute. Consider:\n" \
      "- Breaking the expression into smaller parts\n" \
      "- Using 'run_debug_command' with a custom timeout" }])
  rescue DebugMcp::Error => e
    text = "Error: #{e.message}"
    if e.message.include?("ThreadError")
      text += "\n\nThis error occurs in signal trap context. " \
              "Use set_breakpoint + trigger_request to escape to normal context first."
    end
    MCP::Tool::Response.new([{ type: "text", text: text }])
  ensure
    if stdout_redirected
      client.send_command('$stdout = STDOUT') rescue nil
    end
    # Save suspended catch BPs to client so they can be proactively
    # restored on the next evaluate_code call if this restore fails.
    client.suspended_catch_bps = suspended_catch_bps if suspended_catch_bps.any?
    restore_catch_breakpoints(client, suspended_catch_bps)
    client.suspended_catch_bps = []
  end
end