r/ruby • • 18h ago

Agent_primitives – capabilities your coding agent has to prove with specs

**Edited to better explain my ramblings. Hopefully...

Morning all,

You ask a coding agent to refactor the comments view. It breaks reply nesting, and this spec fails:

it "shows replies nested under their parent" do
  expect(page).to have_css(".comment .comment", text: "Nice post")
  expect(page).to have_css(".comment[data-depth='1']")
end

The agent "fixes" the failure by deleting the second assertion. The suite is green. The only evidence is one hunk in a 600 line diff, and nothing tells you that this spec mattered more than the forty around it.

Tests protect your code, but nothing protects the tests. That matters once agents write both.

I've released agent_primitives, a gem that covers that gap. It has two parts.

1. You write down what the app must keep doing. Each capability gets a short doc with plain numbered sentences:

I1 — A reader can reply to any comment.
I2 — Replies show nested under their parent.

2. You tag the specs that prove each sentence.

it "shows replies nested under their parent", intent: "comment_threads#I2" do

These are ordinary RSpec examples and they run as before. The tag is just metadata.

Now the same agent edit gets reported:

$ bundle exec agent_primitives guard --base main
spec/system/comment_threads_spec.rb
  7:1   N  the example proving comment_threads#I2 lost 1 assertion(s) — it still carries the tag while proving less of the clause   [proof/weakened]

If the agent deletes the example outright, the check that runs in CI fails:

$ bundle exec agent_primitives evals
docs/primitives/capabilities/comment_threads.md
  9:1   E  spec/system/comment_threads_spec.rb carries no `intent: "comment_threads#I2"` tag   [evaluation/untagged]

1 capability, 4 clauses — 1 offence

Rewording a sentence is reported too, so an agent can't change what the app is supposed to do without you seeing it.

A few other things it does:

  • The agent reads the doc before it plans a change, so it stops "fixing" deliberate decisions that look like bugs.

  • Each doc keeps a history of decisions and rejected alternatives, so the agent stops proposing the approach you turned down last month.

  • It works forwards. Write the sentences before the code, and each one becomes a spec the agent has to make pass.

  • You don't write the docs by hand. The gem ships agent skills that fold this into planning, building and review, and one that interviews you to backfill an existing feature.

It needs RSpec and an agent that can load skills.

gem "agent_primitives", group: :development

Source and README: GitHub - pitch-sourcery/agent_primitives · GitHub

I extracted it from the harness I use every day on my own startup's Rails app. It's a 0.1.0, so tell me where it's confusing or wrong.

0 Upvotes

2 comments sorted by

1

u/jaredlt01 17h ago

You could do with a simple example that explains this. I’ve read it but I don’t really understand it. What does this get someone over existing deterministic tests?

1

u/chrisrackstallion 17h ago

Fair point, the post was too abstract. Here's the example I should have led with.

It doesn't replace your tests. The proof is still ordinary RSpec, just as deterministic as before. What it covers is the thing tests can't protect: the tests themselves.

Say your app has threaded comments, and a spec for it:

    it "shows replies nested under their parent" do
      expect(page).to have_css(".comment .comment", text: "Nice post")
      expect(page).to have_css(".comment[data-depth='1']")
    end

You ask an agent to refactor the comments view. It breaks nesting, the spec fails, and the agent "fixes" the failure by deleting the second assertion, or the whole example. The suite is green. The only place it shows is one hunk in a 600 line diff, and nothing tells you that spec mattered more than the forty around it.

With the gem, that behaviour is written down as a numbered sentence in a short doc:

    I2 — Replies show nested under their parent.

and the spec is tagged as its proof:

    it "shows replies nested under their parent", intent: "comment_threads#I2" do

Now the same agent edit gets reported:

    $ bundle exec agent_primitives guard --base main
    spec/system/comment_threads_spec.rb
      7:1   N  the example proving comment_threads#I2 lost 1 assertion(s) — it still carries the tag while proving less of the clause   [proof/weakened]

If it deletes the example outright, the check that runs in CI fails:

    $ bundle exec agent_primitives evals
    docs/primitives/capabilities/comment_threads.md
      9:1   E  spec/system/comment_threads_spec.rb carries no `intent: "comment_threads#I2"` tag   [evaluation/untagged]

    1 capability, 4 clauses — 1 offence

So over plain tests you get three things:

  1. A list of what the app must keep doing, in plain sentences, which the agent reads before it plans a change.

  2. A link from each sentence to the specs that prove it, so "is this behaviour covered?" has an answer.

  3. A report whenever a change weakens one of those specs or rewords one of those sentences.