Skip to content

feat: add run subcommand and make diff results trustworthy - #43

Merged
mickamy merged 19 commits into
mainfrom
feat/run
Nov 23, 2026
Merged

mickamy merged 19 commits into
mainfrom
feat/run

Conversation

@mickamy

@mickamy mickamy commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add sql-tap run -- <command> to measure the SQL traffic of a single command, and tighten sql-tap diff (#42) so that its result can be trusted as evidence for a change. With this, an AI agent (or a human) can measure before and after a fix with one command each and attach the diff output to the PR.

sql-tap run -out=before.json localhost:9091 -- go test ./...
# ...apply the change...
sql-tap run -out=after.json localhost:9091 -- go test ./...
sql-tap diff -format=markdown before.json after.json

Behavior changes to diff from #42: reports measured with different commands are now rejected (exit 2), and slow query increases only warn by default instead of failing.

Changes

Not missing events at the start: feat(server): send subscribed header once Watch subscription is registered

  • sql-tapd sends a sql-tap-subscribed response header right after registering the Watch subscription. Previously the client could not tell when the subscription was in place, so queries issued right after connecting could be missed (the existing tests sleep 50ms for this reason).
  • Existing clients (TUI, -ci) ignore the header, so nothing changes for them.

feat(ci): add Subscribe to wait for sql-tapd subscription confirmation

  • ci.Subscribe(ctx, addr, timeout) returns only after the header arrives. An older sql-tapd never sends it, so Subscribe fails after the timeout with a hint that sql-tapd may be older than sql-tap.
  • ci.Run (used by -ci) does not wait for the header, so new sql-tap keeps working with older sql-tapd in CI mode.

Not missing events at the end: feat(ci): drain in-flight events after stop in Watcher.Collect

  • After the command exits, events may still be on their way over gRPC. Watcher.Collect(stop, Drain{Idle, Max}) keeps receiving until no event arrives for Idle, bounded by Max.
  • Hitting Max returns the result together with ErrDrainLimit, since the numbers may be incomplete (typically another application is using the same sql-tapd).

feat(ci): record measured command in CI results

  • Result.Command / JSON command (omitted when empty). Adding an optional field keeps the schema at version: 1; older reports decode with no command.

fix(ci): bound the whole Subscribe by its timeout

  • The timeout only covered waiting for the header, not connection setup. It now cancels the whole Subscribe.
  • conn.Close() can block for ~5s after a failed dial to localhost (127.0.0.1 returns immediately), so failed connections are closed in the background.

feat: add run subcommand to measure SQL traffic of a command

  • sql-tap run [flags] <addr> -- <command> [args...] with -format, -out, -subscribe-timeout (5s), -drain (200ms), -drain-max (5s).
  • The command keeps stdin / stdout / stderr; the report goes to stderr so it does not mix with test output. SIGINT / SIGTERM are forwarded to the command.
  • Exit codes, in priority order:
    1. the command's own exit code if it fails, so test failures are never hidden behind a clean SQL report
    2. 2 if no statements were captured (the app likely bypassed the proxy), the subscription was not confirmed, or draining hit -drain-max
    3. 1 if N+1 or slow queries were found, same as -ci
    4. 0 otherwise
  • The report and -out are written even when the command fails.

feat(diff): fail when reports were measured with different commands

  • Comparing go test ./... before with go test ./order/... after would look like a large improvement. When both reports record a command and they differ, diff exits 2. -allow-command-mismatch compares anyway with a warning.
  • When either report has no command (e.g. written by -ci), diff compares but warns that it cannot verify the scope.
  • Adds Comparison.Warnings, rendered in text, Markdown and JSON.

feat(diff): only warn on slow query increases by default

  • Slow flags depend on a time threshold, so a query at 99ms in one run and 101ms in the next would fail diff without any code change. Statement and N+1 counts are stable for the same code and stay strict.
  • Slow increases now go to Warnings; -fail-on-slow restores the previous behavior.

fix: stop HTML-escaping SQL in JSON reports

  • encoding/json escaped <, > and &, so WHERE total > ? came out as WHERE total > ? in -out, -format=json and diff -format=json. Both MarshalJSON and the outer encoders now disable HTML escaping.

fix(diff): mention command mismatch in exit code help

docs: document run and diff subcommands

  • README: usage, new "Measuring a command" and "Comparing reports" sections, -format / -out for CI mode, and the zero-statement warning.

Verification

  • go test -race ./... passes
  • golangci-lint run ./...: 0 issues
  • Each intermediate commit builds and passes go test -race ./ci/ . on its own

Copilot AI balanced review requested due to automatic review settings November 22, 2026 23:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Collection can end prematurely and report success while the command is still running, with additional unresolved issues in exit-status preservation, descendant signal forwarding, and warning rendering.

4 open issues
What changed in this PR

Adds sql-tap run to measure SQL traffic around a command and strengthens report comparison for before-and-after change verification.

Changes:

  • Confirms subscriptions before starting commands, then collects and drains SQL events.
  • Records commands in reports, rejects mismatched comparisons by default, and makes slow-query increases warnings unless explicitly enforced.
  • Preserves readable SQL in JSON output and adds documentation and tests.
File Description
server/​server.go Sends subscription-readiness metadata.
server/​server_test.go Tests readiness confirmation before events.
run.go Adds command execution, collection, reporting, and signal forwarding.
run_test.go Tests command outcomes, output, and usage errors.
README.md Documents run and diff workflows and flags.
main.go Routes the run subcommand and preserves JSON operators.
main_test.go Tests readable SQL operators in JSON.
diff.go Adds command-mismatch and slow-query comparison flags.
diff_test.go Tests updated diff behavior and output.
ci/​watcher.go Adds confirmed subscriptions and bounded draining.
ci/​watcher_test.go Tests confirmation, timeouts, and draining.
ci/​json.go Serializes command metadata without HTML escaping.
ci/​json_test.go Tests command metadata round-tripping.
ci/​diff.go Checks command consistency and generates warnings.
ci/​diff_test.go Tests command checks and slow-query policy.
ci/​diff_report.go Renders warnings in text, Markdown, and JSON.
ci/​diff_report_test.go Tests warning output across formats.
ci/​ci.go Shares connection setup and displays measured commands.
ci/​ci_test.go Tests command display in reports.

Review effort: Balanced

Comment thread run.go Resolved
Comment thread ci/diff_report.go Resolved
Comment thread run.go Resolved
Comment thread run.go Resolved
Copilot AI balanced review requested due to automatic review settings November 22, 2026 23:58
@mickamy
mickamy merged commit d8148a6 into main Nov 23, 2026
5 checks passed
@mickamy
mickamy deleted the feat/run branch November 23, 2026 00:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Incomplete reports can produce misleading clean improvements in Diff, while collection error handling can misreport successful completion or discard captured diagnostics.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Write partial reports when event collection fails

run.go:113

A receive error such as gRPC Unavailable returns before either report writer runs, so even a failed command produces no report and can leave an older -out file untouched. Watcher.Collect also discards captured events on this path (ci/watcher.go:106). Preserve the partial aggregate and write it as an incomplete report before returning the collection error, while retaining the command's exit-code priority.

4 resolved since last review

Review effort: Balanced

Comment thread run.go
Comment on lines +120 to +121
if out != "" {
if err := writeReportFile(out, result); err != nil {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in #44 (67e2a74): run records why a measurement may be incomplete in the JSON incomplete field, and diff refuses marked reports with exit 2. The report itself is still written for diagnosis.

Comment thread ci/watcher.go
Comment on lines +101 to +103
if stop != nil {
return a.result(), ErrStreamEnded
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in #44 (c16e9cc): the end-of-stream decision now checks whether stop is actually closed (streamEndErr), with a unit test for the closed-stop case that fails against the previous logic.

@mickamy

mickamy commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator Author

Re: "Write partial reports when event collection fails" from the review summary: fixed in #44 (5dbb406). Watcher.Collect now returns the partial result along with a receive error, and run writes the report marked incomplete for any collection error, so a stale -out file is overwritten. The command's exit code still takes priority.

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.

2 participants