All jobs

Fix "Psych::DisallowedClass: Tried to load unspecified class" on Ruby 3.1 and later

Yes. An agent finds the YAML.load that reads back your own objects, lists the classes the document carries, permits only those, and runs the specs on a current Ruby.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent starts from the failing load and what the document contains. In a classifier gem like oasic/nbayes, issue #14 reports Psych::DisallowedClass: Tried to load unspecified class: NBayes::Base after moving to Ruby 3.3. The call is lib/nbayes.rb line 315, nbayes = YAML.load(yml_data) inside Base.from_yml, which is followed by reset_after_import to restore the Hash defaults YAML does not keep; Base.from(yml_file) at line 321 reads a file into it, and dump at line 340 writes the object out with YAML.dump(self, f). So the gem serialises a trained model as a Ruby object graph and reads it back, and the document it writes carries three object tags, !ruby/object:NBayes::Base, !ruby/object:NBayes::Vocab and !ruby/object:NBayes::Data, plus symbol keys.

Then it reproduces with the gem's own specs. spec/nbayes_spec.rb has 34 examples, two of which round-trip a model through YAML: should save to yaml and load from yaml at line 164 and should dump to yaml string and load from yaml string at line 177. On Ruby 3.3, which ships Psych 5, rspec fails those two examples, both with Psych::DisallowedClass: Tried to load unspecified class: NBayes::Base at lib/nbayes.rb:315. On Ruby 3.1 with Psych 4 the same two fail the same way, which places the break at Psych 4, where load stopped accepting arbitrary objects, not at Ruby 3.3 where the reporter met it.

The fix names the classes instead of switching the safety off. YAML.safe_load(yml_data, permitted_classes: [NBayes::Base, NBayes::Data, NBayes::Vocab, Symbol]) loads the document; reset_after_import runs after it as before. The agent tries the variants and reports them: aliases: true is not needed because the dump contains no anchors, YAML.load with the same permitted_classes: keyword also passes on Psych 4 and later, and YAML.unsafe_load passes too but would accept any object tag in a file a user hands to Base.from. With the permitted list in place the whole suite passes on Ruby 3.3. The agent also checks that the spec run itself needs simplecov and rspec, which the Gemfile lists, and nothing else.

It lists what it did not change: rspec is declared as a runtime dependency in the gemspec as well as a development one, the Gemfile pulls jeweler, and the VERSION file reads 0.1.3. Each is one line in the pull request so the maintainer can tidy them separately.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Our Ruby gem raises \"Psych::DisallowedClass: Tried to load unspecified class\" when it reads back YAML it wrote itself. Find the YAML.load call, list every class tag and scalar type the dumped document carries, and change the load to permit exactly those classes rather than unsafe_load. Show the failing specs on a current Ruby, say which Psych version made the old call fail, and run the full spec suite after the fix. Open a pull request that quotes the error and explains the permitted list.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that changes line 315 of lib/nbayes.rb and nothing else. The description quotes the error, lists the three object tags and the symbols found in a dumped model, shows rspec with the two failures before and none after on Ruby 3.3, names Psych 4 as the version where YAML.load became safe by default (with the Ruby 3.1 run as evidence), and explains why unsafe_load was not used.

A reviewer checks that the permitted list is complete for every class the model graph can contain, that reset_after_import still runs after the load, and whether a file path a user passes to Base.from should be treated as trusted input at all.

Run it on every Ruby version bump in a service's Dockerfile or .ruby-version

The error appears the first time the code runs on a Ruby that ships Psych 4, so the trigger is the change that moves the Ruby version. An automation with a github trigger on pull_request.opened and pull_request.synchronize runs this prompt on each pull request that touches .ruby-version, Gemfile.lock or the Dockerfile, with the event appended after the prompt, and pushes the permitted-classes change when the specs fail on DisallowedClass.

For a fleet of services that persist objects as YAML, your server sends this request once per repository, each with its own UUID as request_id so a retry returns the run already started, and reads each run.completed webhook; a key starts at most max_concurrent runs at once and queues the rest.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The quick fix for this error is unsafe_load, which makes the message go away and reopens the hole Psych 4 closed. In a Rush sandbox the agent dumps a real model, reads the tags out of the document, and proves the narrow permitted list with the gem's own specs on two Ruby versions, with every command kept in the run.

Knowing which component changed matters for the rest of the codebase: the pull request shows the failure on Ruby 3.1 as well as 3.3, so a team does not pin Ruby below 3.3 and wonder why the error persists.

Frequently asked questions

Why not just use YAML.unsafe_load?

It would pass the specs, but Base.from reads a file path a caller supplies, and unsafe_load instantiates any object tag that file contains. Naming the three classes and Symbol loads the gem's own models and nothing else.

Which Ruby version introduced the error?

The change is in Psych 4, where YAML.load became safe_load by default. Ruby 3.1 ships Psych 4 and fails the same two specs as Ruby 3.3 with Psych 5, which the pull request shows side by side.

Do I need aliases: true?

Only if the document contains anchors and aliases. The agent checks the dumped model for them and finds none here, so the permitted list alone is enough; it says so in the pull request so you can add the option if your documents differ.

How does the agent know the permitted list is complete?

It dumps a trained model with the gem's own dump, reads every !ruby/object: tag and scalar type out of the YAML text, and then runs the round-trip specs, which load a full model back and classify with it.

More Ruby jobs

More jobs like this: Fix a bug from an alert

Keep reading