Skip to content

test(purchase): lex SQL before the idempotency_key writer scan - #1878

Merged
cristim merged 1 commit into
mainfrom
test/1853-writer-scan-lexer
Nov 9, 2026
Merged

cristim merged 1 commit into
mainfrom
test/1853-writer-scan-lexer

Conversation

@cristim

@cristim cristim commented Nov 9, 2026

Copy link
Copy Markdown
Member

Closes #1853.

Problem

The single-writer scan for purchase_executions.idempotency_key (TestIdempotencyKeyCopyHasSingleWriter) split a SET clause with ad hoc quote and paren toggles. A SQL comment before the target hid a write (SET /* k */ idempotency_key = $1), and $$...$$, $tag$...$tag$ and E'...' strings desynced it. A write hidden that way is a way for the guard to fail open. This was found by qa-ui-4 reviewing #1849 and documented there as a limitation.

Change (test-only, internal/purchase/idempotency_key_writers_test.go, plus known-issues.md)

maskSQL lexes the statement once and the existing scan runs on its output:

  • Comments (line and nested block) become a space; every string literal body becomes an empty literal; a double-quoted identifier is kept only for idempotency_key and otherwise emitted as "x".
  • Postgres lexing rules are exact: a dollar-quote tag opens only after a non-word, non-$ byte, so $1 and $1$2 are never tags; E/e is an escape-string prefix only after a non-word, non-$ byte, and only then does a backslash escape; a plain '...' takes a backslash literally.
  • Fail closed: an unterminated literal, comment or identifier, or a literal body that mentions purchase_executions (a DO $$ ... $$ block or an EXECUTE '...' string), makes the statement unverifiable. It counts as dynamic, which already fails the test.
  • 25 new fixtures; 14 of them fail on the previous scan, and the rest guard the lexing rules.
  • known-issues.md records what remains: schema-qualified or quoted table names, U&'...' strings, and standard_conforming_strings = off.

Verification

  • The plan went through 2 adversarial review passes and the implementation through 2, by the same cold reviewer. In pass 1 the reviewer also ran a 200k-statement differential fuzz for a hidden write (no hit) and a 300k-byte-string fuzz for panics (none), and confirmed that exactly the 14 listed fixtures fail on the previous scan.
  • Ten deletion probes on maskSQL (comments, nesting, E-string backslash, dollar quotes, -- inside a literal, unterminated input, $1 as a tag, quoted identifiers, the literal-body table check, the E prefix rule) each fail a fixture.
  • go test -run IdempotencyKey ./internal/purchase/ passes, and the module-wide scan still reports exactly 1 insert, 0 updates and 0 dynamic. No production code changes.

🤖 Generated with Claude Code

The single-writer scan read a SET clause with ad hoc quote and paren
toggles, so a SQL comment before the target hid a write
(`SET /* k */ idempotency_key = $1`), and $$...$$, $tag$...$tag$ and
E'...' strings desynced it. maskSQL now lexes the statement once and
the existing logic runs on its output.

- Comments (line and nested block) become a space; every literal body
  becomes an empty literal; a double-quoted identifier is kept only for
  idempotency_key and is otherwise emitted as "x".
- Fail closed: an unterminated literal, comment or identifier, or a
  literal body that mentions purchase_executions (a DO block or
  EXECUTE string), makes the statement unverifiable, which counts as
  dynamic and fails the test.
- 25 fixtures; 14 fail on the previous scan, the rest guard the lexing
  rules (dollar-quote and E-prefix start rules, a parameter is not a
  tag, quoted identifiers).
- known-issues.md records the limits that remain: schema-qualified or
  quoted table names, U&'...' strings, standard_conforming_strings off.

Closes #1853

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@cristim cristim added priority/p3 Polish / idea / may never ship type/bug Defect labels Nov 9, 2026
@coderabbitai

coderabbitai Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between bfb0654 and f85858c.

Files selected for processing: 2

  • internal/purchase/idempotency_key_writers_test.go
  • known-issues.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@cristim
cristim merged commit cf21cfb into main Nov 9, 2026
42 checks passed
@cristim
cristim deleted the test/1853-writer-scan-lexer branch November 9, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority/p3 Polish / idea / may never ship type/bug Defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Writer scan misses an idempotency_key assignment after a SQL comment

1 participant