显示HN:LLM Rescuer – 修复 Ruby 中价值数十亿美元的错误
Show HN: LLM Rescuer – Fixing the billion dollar mistake in Ruby

原始链接: https://github.com/barodeur/llm_rescuer

## LLM 救援者:一种有趣的(且昂贵的)解决空值错误的方案 LLM 救援者是一个实验性的 Ruby gem,被设计成一个幽默的概念验证,旨在“修复”托尼·霍尔的“价值十亿美元的错误”——空引用。当遇到 `nil` 上的 `NoMethodError` 时,gem 不会崩溃,而是利用大型语言模型(如 GPT-5)来*猜测*预期的行为。 本质上,它会猴子补丁 `NilClass`,将上下文发送到 LLM,然后 LLM 会尝试提供一个合理的响应——可能导致不可预测且常常令人发笑的结果(例如,用户名为“🐱 威士卡先生”)。 虽然声称提供 73.2% 的成功率,但这种“救援”代价高昂——估计每次修复费用为 0.002 美元,对于典型的应用程序,每月可能达到数百甚至数千美元。 **此项目明确*不*适用于生产环境。** 它旨在用于教育目的,并展示将 AI 应用于运行时错误处理的潜力(和陷阱)。 预计会出现混乱、API 账单膨胀,以及可能具有感知能力的空值对象。

## LLM Rescuer:一个用于处理 `nil` 错误的 Ruby Gem 一个新的 Ruby gem,“LLM Rescuer”,以一种有趣的方式解决了代码中常见的 `nil` 错误问题,它尝试使用大型语言模型 (LLM) *猜测* 解决方案。该项目在 Hacker News 上分享,引发了关于依赖 LLM 动态修复代码的潜力以及有趣的弊端的讨论。 评论者设想未来 LLM 可以填充 API 请求中缺失的数据,可能导致不可预测的结果(例如,看到奶酪而不是 GitHub 内容!)。其他人分享了使用 LLM 进行动态代码更正的类似实验,尤其是在测试场景中。 一篇相关的 20 年前关于“容错计算”的研究论文也被提及,强调了类似的概念,即允许程序在出现错误时通过制造值继续运行。虽然许多人认为这个 gem 是一个“令人愉快但可怕的想法”,但也有人推测一些开发者可能会觉得它有用,甚至可能为未来的 LLM 训练数据做出贡献。
相关文章

原文

⚠️ EXPERIMENTAL PROJECT - NOT FOR PRODUCTION USE

This project is an experimental proof-of-concept developed for educational purposes and pure, unadulterated fun. It uses AI to dynamically handle runtime errors, which introduces unpredictable behavior and potential security risks that would make your security team cry. Never use this in production environments unless you enjoy explaining to your boss why the AI decided your user's name should be "🐱 Mr. Whiskers".

The Mission: Fixing the Billion Dollar Mistake... With a Billion Dollars Worth of LLM Tokens

In 1965, Tony Hoare invented the null reference and later called it his "billion-dollar mistake". Well, we're here to fix it by potentially spending a billion dollars in OpenAI API tokens! 💸

Because clearly, the best way to solve a problem caused by the absence of a value is to throw artificial intelligence at it until it hallucinates a reasonable response. What could possibly go wrong?

What This Abomination Does

LLM Rescuer is a Ruby gem that catches your NoMethodErrors when you call methods on nil and asks GPT to guess what you probably wanted. It's like having a very expensive, overly-confident intern who's read your entire codebase and thinks they know what you meant.

user = nil
puts user.name  # Instead of crashing, might return "John Doe" or "undefined" or "🎭"
                # depending on what the AI thinks is funny today

How This Beautiful Disaster Works

  1. The Trap: We monkey-patch NilClass because why not break fundamental assumptions?
  2. The Analysis: When you call a method on nil, we pause everything and ask an LLM to read your mind
  3. The Oracle: GPT-5 analyzes your code like a digital Sherlock Holmes with ADHD
  4. The Guess: It returns what it thinks you wanted, which is right about 73.2% of the time*
  5. The Magic: Your code continues running, probably doing something completely different than intended

*Statistics may be made up

Installation (AKA "How to Spend Your OpenAI Credits")

Add this line to your application's Gemfile (and say goodbye to your API budget):

Then execute:

bundle install
# Also install a second mortgage for your OpenAI bills
  1. Feed the AI overlords:

    export OPENAI_API_KEY="prepare-your-wallet"
  2. Set your project scope (so it only ruins your code):

    LlmRescuer.prefix = "/path/to/your/beautiful/disaster"
  3. Embrace the chaos:

    require 'llm_rescuer'
    LlmRescuer.setup
    # You have chosen... poorly

Real-World Usage Examples

# Classic nil safety
user = find_user(id: "nonexistent") # returns nil
puts user.email
# AI: "Analyzing context... user seems to need an email...
#      returning '[email protected]'"

# Shopping cart magic
cart = session[:cart] # nil because session expired
total = cart.total_price
# AI: "This looks like e-commerce. Based on similar patterns,
#      I'll return 0.0 to avoid charging $nil"

# The existential crisis
meaning_of_life = nil
answer = meaning_of_life.to_i
# AI: "Clearly this should be 42. I've read Douglas Adams."

What Could Go Wrong? 🤔

  • Your nil users might suddenly become named "ChatGPT's Best Friend"
  • Your shopping cart totals might be replaced with haikus about commerce
  • Your boolean flags might return philosophical treatises on truth
  • Your API might start responding with interpretive dance descriptions
  • You might accidentally achieve sentience in your nil objects
  • Traditional nil handling: Free, but your app crashes
  • LLM Rescuer: $0.002 per nil rescue (estimated)
  • Your sanity: Priceless (and rapidly depleting)

For a typical Rails app with healthy nil hygiene, expect to spend approximately:

  • Development: $50-100/month (because you test things, right?)
  • Production: $500-5000/month (depending on how nil-happy your code is)
  • Black Friday: Your firstborn child

Dependencies (The Usual Suspects)

  • ruby_llm (~> 1.8.2) - For talking to our AI overlords
  • ruby_llm-schema (~> 0.2.1) - For making sure AI responses are structured chaos
  • binding_of_caller (~> 1.0.1) - For context that the AI will probably ignore anyway
  • A sense of humor (>= 1.0.0, required)
  • A healthy bank account (recommended)

After checking out the repo and questioning your life choices:

bin/setup    # Install dependencies
rake spec    # Run tests (spoiler: they're as unpredictable as AI)
bin/console  # Interactive prompt for immediate regret

Found a bug? That might be a feature! Found a feature? That might be a bug!

Submit PRs, issues, or interpretive dance videos explaining your problems. We accept contributions in all forms except sensible ones.

"LLM Rescuer saved my app from crashing! Sure, all my users are now named 'Anonymous Penguin' and my prices are in Bitcoin, but it didn't crash!" - Definitely Real User

"I deployed this to production and my nil errors went down 100%! My AWS bill went up 400%, but correlation isn't causation, right?" - Another Real Person

Why fix your code when you can train an AI to guess what your code should have done? It's like pair programming, except your pair is a probabilistic text generator that's read the entire internet and has strong opinions about variable naming.

This project is licensed under the MIT License, which gives you the right to use, modify, and distribute this code. It does not give you the right to sue us when your production app starts writing poetry instead of processing payments.

Remember: null references were a billion-dollar mistake, but with today's AI API pricing, we can make it a trillion-dollar mistake! 🚀


"In a world without nil safety, one gem dares to ask: 'What if we just guessed?'" 🎬

联系我们 contact @ memedata.com