Skip to content

Audit rails query invocations and tag agent-driven runs#154

Merged
lewispb merged 4 commits intomasterfrom
query-command-auditor
Apr 21, 2026
Merged

Audit rails query invocations and tag agent-driven runs#154
lewispb merged 4 commits intomasterfrom
query-command-auditor

Conversation

@lewispb
Copy link
Copy Markdown
Member

@lewispb lewispb commented Apr 20, 2026

Summary

Adds an audit subscriber for the rails query command introduced in rails/rails#57156 (Rails 8.2+). Each rails query invocation is recorded as a session in the configured session logger — same audit trail as rails console.

When a known coding-agent env var is present in the environment, its label is appended to the session reason so audit trails can distinguish human from agent-driven queries:

  • CLAUDECODE=1rails query (via Claude Code)
  • CODEX_THREAD_ID=<uuid>rails query (via Codex)

Both env markers were verified by inspecting a live agent session. The map is configurable:

Console1984::QueryAuditor.known_agents["CURSOR_AGENT"] = "Cursor"

The subscriber no-ops outside protected environments, so it's safe to register unconditionally from the engine.

image

Context

Haystack and Queenbee both ship a copy of this subscriber today as an app-level initializer. Upstreaming avoids the duplication and gives any console1984 consumer on Rails 8.2+ the audit trail for free.

Test plan

  • bundle exec rails test — 83 runs, 0 failures, 0 errors
  • Tests cover session creation, expression recording, and known-agent detection

Subscribes to the query.rails ActiveSupport::Notifications event emitted
by Rails 8.2's rails query command and records each invocation as a
session in the configured session logger, matching the audit trail
already produced for rails console.

When a known agent env var is present, include its label in the session
reason (e.g. 'rails query (via Claude Code)') so audit trails can
distinguish between human and agent-driven queries. Known agents and
the override via QUERY_AGENT are configurable on the auditor class.
Copilot AI review requested due to automatic review settings April 20, 2026 20:32
YAGNI — the known_agents map already covers the realistic detection
surface, and consumers can extend it without needing a separate
generic override.
@lewispb lewispb force-pushed the query-command-auditor branch from 13883f6 to fdb3d69 Compare April 20, 2026 20:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR upstreams auditing for the new rails query command by subscribing to query.rails notifications and recording each invocation as a Console1984 session, optionally tagging sessions as agent-driven based on known env vars (or a QUERY_AGENT override).

Changes:

  • Add Console1984::QueryAuditor subscriber to create sessions + record the query expression as a command.
  • Register the subscriber from the engine so it’s active automatically.
  • Add tests covering session creation, expression logging, and agent labeling/override behavior.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/console1984/query_auditor.rb Implements the query.rails subscriber, session reason construction, and agent detection.
lib/console1984/engine.rb Installs the subscriber via a Rails initializer.
test/query_auditor_test.rb Adds coverage for session creation, expression logging, and agent labeling behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/console1984/query_auditor.rb
Comment thread lib/console1984/query_auditor.rb
lewispb added 2 commits April 20, 2026 22:03
Other tests mutate the shared MutableUsernameEnvResolver, so pin it to
jorge in setup rather than relying on the dummy app default.
Copilot AI review requested due to automatic review settings April 20, 2026 21:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/console1984/query_auditor.rb
Comment thread lib/console1984/engine.rb
Comment thread test/query_auditor_test.rb
Comment thread test/query_auditor_test.rb
@lewispb lewispb requested a review from jorgemanrubia April 20, 2026 21:11
Copy link
Copy Markdown
Member

@jorgemanrubia jorgemanrubia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏👏

@lewispb lewispb merged commit 9e8deb1 into master Apr 21, 2026
34 checks passed
@lewispb lewispb deleted the query-command-auditor branch April 21, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants