Skip to content

fix(sqlite): keep triggers whose declaration spells the table in another case - #2807

Merged
morluto merged 5 commits into
morluto:mainfrom
Dragolone:fix/sqlite-selected-table-trigger-case
Oct 13, 2026
Merged

morluto merged 5 commits into
morluto:mainfrom
Dragolone:fix/sqlite-selected-table-trigger-case

Conversation

@Dragolone

@Dragolone Dragolone commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In selected_table scope, inspect_sqlite_database omits triggers whose CREATE TRIGGER … ON <name> spells the selected table in a different ASCII case, while still reporting schema.completeness: "complete".

SQLite stores a trigger's target in sqlite_schema.tbl_name exactly as written in the declaration and resolves it by ASCII case folding; an index stores the table's own name. The selected-table query filtered tbl_name = ? COLLATE BINARY, so such triggers never reached the result.

No issue is linked: I searched issues and PRs for "sqlite trigger", "tbl_name", "selected_table trigger", "sqlite case-insensitive" and "inspect_sqlite_database" and found no report. Open PRs #2774 and #2701 touch nearby code (a column-selection error reason, and moving the trigger.table === input.table check) without changing this behavior.

Problem and expected behavior

Smallest trigger:

CREATE TABLE Users(id INTEGER PRIMARY KEY, email TEXT);
CREATE INDEX users_email ON users(email);
CREATE TRIGGER audit_insert AFTER INSERT ON users BEGIN SELECT 1; END;
CREATE TRIGGER audit_delete AFTER DELETE ON Users BEGIN SELECT 1; END;

rea inspect-sqlite-database case.db --table Users reports one trigger (audit_delete) with completeness: "complete". audit_insert fires on inserts into Users but is missing.

Violated contract: docs/sqlite-databases.md says selected_table "expands that table and its indexes and triggers", and schema.completeness refers to the reported scope.

Expected: both triggers, each attributed to Users.

Change and scope

  • In selected_table scope the filter uses COLLATE NOCASE only when the selection is an exact ordinary table that is not a possible shadow table, which are the only selections whose rows can be read. NOCASE folds only ASCII letters, the same rule SQLite applies when it resolves the table named by an index or trigger. Every other selection keeps the exact BINARY filter, so a selection that is refused later fails exactly as it does on main, whatever its dependents declare: a view, a virtual table, a possible shadow table, or a name that is not stored. A name absent from PRAGMA main.table_list is refused before the scan with the existing "does not exist with that exact name" selection failure, now shared with readRows. (Both refinements come from the Codex review.)
  • trigger.table now reports the stored table or view name. In selected_table scope that is the selected name, since the scan returns only its dependents. In other scopes it is the exact stored name when one matches. Otherwise the PRAGMA main.table_list names are folded once, lazily, with the existing sqliteIdentifierKey, so unrelated large identifiers are never folded for a selected-table inspection (Codex review). This is needed, not cosmetic: SqliteDatabaseService requires trigger.table === input.table in selected_table scope, so widening the filter alone would fail that check. It applies in every scope, so a trigger on a view declared ON Recent for view recent now reports recent. This matches how indexes are already reported.
  • Not included: case-insensitive table selection for rows, which stays exact as documented. Indexes are unchanged because SQLite already stores their table's own name.

Contribution scope: an ordinary format-inspection correctness fix within existing boundaries; a maintainer scope decision is not applicable.

Contract and boundary impact

  • Semantic owner and earliest changed stage: readSchema in src/sqlite/SqliteDatabaseInspection.ts (schema selection and trigger projection).
  • Contribution scope fit and linked maintainer decision: in scope (format inspection, evidence reporting); not applicable.
  • CLI and MCP/tool-catalog contract: no schema change. triggers[].table values change only when a declaration spelled the target in another ASCII case; they now name the stored object.
  • Provider, bridge, target-format, or platform compatibility: none established beyond the above; SQLite's NOCASE and identifier resolution both fold ASCII only, so non-ASCII names are matched exactly as before.
  • Evidence, artifact, provenance, or reconstruction contract: trigger.table is a resolved name rather than the declaration's spelling; the declaration text itself is still retained in sql outside inventory scope.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): not applicable; the snapshot is still opened read-only and no SQL from the database is executed.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none.

Expected effects (check known effects; use documentation-only on its own).
Unchecked items do not establish absence; leave uncertainties visible.

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: tests/boundary/sqlite/sqliteDatabaseInspection.test.ts — "attributes triggers declared with another ASCII case to the stored table name". It covers selected_table (both triggers, completeness: "complete", index unchanged) and inventory (triggers on a table and an INSTEAD OF trigger on a view report the stored names). A second, parameterized test, "refuses $selection before expanding declarations under another spelling", covers a differently cased table (users for Users) and an exact view (Recent with an INSTEAD OF trigger declared ON recent). In both cases the trigger's JSON-escaped SQL exceeds the 16 MiB reply budget, and the test expects the exact-name selection failure. Both cases pass on main, and both reported output-limit at intermediate heads of this PR.

  • Base reproduction: on 91830f1e0 the new test fails with AssertionError: expected [] to deeply equal [ Array(2) ] (no trigger is selected when the declarations spell users / USERS).

  • User-visible CLI output (rea inspect-sqlite-database case.db --table Users --format json, schema fields only):

    Before (91830f1e0):

    {"scope": "selected_table", "completeness": "complete", "indexes": [["users_email", "Users"]], "triggers": [["audit_delete", "Users"]]}

    After:

    {"scope": "selected_table", "completeness": "complete", "indexes": [["users_email", "Users"]], "triggers": [["audit_insert", "Users"], ["audit_delete", "Users"]]}
  • Remaining proof gaps: non-ASCII case variants are not separately tested (both SQLite and the new filter treat them as distinct names).

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

macOS (Apple silicon), Node 24.19.0, SQLite 3.53.3 via node:sqlite.

  • npx vitest run tests/boundary/sqlite/sqliteDatabaseInspection.test.ts -t "another ASCII case" on the base — fails as quoted above; passes with the fix.
  • npm run build:cached && npx vitest run on all 8 SQLite test files (git ls-files | grep -i sqlite | grep '\.test\.ts$') — 101 passed, 2 skipped.
  • rea inspect-sqlite-database before/after as shown above.
  • Cost check for the Codex scenario (a tiny selected table plus three 5.2 MiB view names, node --max-old-space-size=256, direct inspectSqliteDatabaseSnapshot call). Main: 144 ms, +22 MiB heap. An intermediate head of this PR that folded every name eagerly: 765 ms, +197 MiB. Final head: 145 ms, +22 MiB.
  • Cost check for the shadow-parent scan (a 1 MiB selected table name plus 20 fts5 tables made unresolvable with the boundary test's byte patch). Main: 58 ms. An intermediate head that folded the name once per unresolved parent: about 1,130 ms. Final head: 66–69 ms.
  • npm run typecheck — passed.
  • npx oxfmt --check on both changed files — passed.
  • npm run verify:module-boundaries — passed.
  • npm run lint — the only error is tests/boundary/providers/ghidra/ghidraLauncher.test.ts:431 (max-lines-per-function), which this PR does not touch; no errors in the changed files.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. Callers that matched triggers[].table against the declaration's spelling will now see the stored name, consistent with indexes[].table.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; tested on macOS only.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no change; the snapshot is still opened read-only and no database SQL is executed.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

…her case

SQLite stores a trigger's target in sqlite_schema.tbl_name exactly as
written in CREATE TRIGGER and resolves it by ASCII case folding, while an
index stores the table's own name. The selected_table scope filtered
tbl_name with COLLATE BINARY, so a trigger declared ON users for table
Users was omitted while the schema still reported complete. Match
dependents with COLLATE NOCASE, and report each trigger's table as the
stored table or view name so the selected-table contract still holds.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T13:57:55.405198Z 8523492 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d6250c1c4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/sqlite/SqliteDatabaseInspection.ts Outdated
// match exactly before rows are read.
const selection =
scope === "selected_table" ? " WHERE tbl_name = ? COLLATE BINARY" : "";
scope === "selected_table" ? " WHERE tbl_name = ? COLLATE NOCASE" : "";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Check the exact table before the NOCASE expansion

When the caller supplies a case-mismatched name (for example users for Users, with the triggers declared on Users), this predicate now expands the real table and all of its declarations before readRows can discover that selectedTable is undefined. On a valid database whose individually valid trigger definitions exceed 16 MiB in aggregate, OutputBudget.retain therefore throws output-limit, which is projected as AnalysisResourceConstraintError instead of the exact-table AnalysisInputError; the previous BINARY filter returned no rows and reached the selection check. Establish the exact metadata.has(selectedName) match before the NOCASE scan so the caller's invalid selection keeps its meaningful failure reason.

AGENTS.md reference: AGENTS.md:L29-L29

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. In 6f369b7 the exact name is checked against PRAGMA table_list before the NOCASE scan, sharing the existing selection failure with readRows. A new boundary test selects users for Users with a trigger whose escaped declaration exceeds the reply budget; it reported output-limit before this commit and now reports the exact-name selection error.

With the NOCASE expansion, a selection such as users for table Users
expanded the stored table's declarations before readRows reported the
missing exact name, so large trigger definitions could surface as an
output-limit failure instead of the selection error. Check the exact
table name against PRAGMA table_list first and share the selection
failure with readRows.
@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the latest changes preserve table selection and blocked row reads.

Summary

The PR includes triggers whose declarations spell an ordinary table’s name in another ASCII case. It reports each trigger against the stored table or view name.

  • Selected-table scans include triggers written with another letter case.

Reviews (4) · Last reviewed commit: "perf(sqlite): fold each table name once ..." · Reviewed by Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f369b7701

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/sqlite/SqliteDatabaseInspection.ts Outdated
Comment on lines +246 to +247
if (selectedName !== undefined && !metadata.has(selectedName))
throw missingSelectedTable(selectedName);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject exact views before the case-folded scan

The new exact-name guard still accepts an exact view because PRAGMA main.table_list places views in metadata. With table: "Recent" and an INSTEAD OF trigger declared ON recent whose individually valid SQL JSON-expands past 16 MiB (for example, about 3 MiB of control characters), the NOCASE scan retains that trigger and throws output-limit before readRows can reject the non-ordinary table; before this change the BINARY scan skipped the differently cased trigger and returned the table-selection error. Reject definitive views before broadening the scan so the invalid caller selection keeps its meaningful failure reason.

AGENTS.md reference: AGENTS.md:L29-L29

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and the same applied to virtual and possible shadow tables. In 8fae046 the scan folds case only when the selection is an ordinary table that is not a possible shadow table; any other selection keeps the exact BINARY filter, so a refused selection fails as it did on main. The boundary test now also selects an exact view Recent with an INSTEAD OF trigger declared ON recent whose escaped SQL exceeds the reply budget: it reported output-limit on the previous head and now reports the selection error.

An exact view, virtual or possible shadow table passed the exact-name
guard, so the NOCASE scan could still expand a differently cased trigger
past the reply budget before readRows refused the selection. Fold case
only when the selection is an ordinary table that is not a possible
shadow table, and keep the exact filter otherwise so every refused
selection fails as it did before the case-folding change.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8fae0466fd

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/sqlite/SqliteDatabaseInspection.ts Outdated
// A trigger stores its target as written in CREATE TRIGGER, which SQLite
// resolves by ASCII case folding; report the stored table or view name.
const storedNames = new Map(
[...metadata.keys()].map((name) => [sqliteIdentifierKey(name), name]),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid folding unrelated schema names

When a selected-table inspection opens a database with large unrelated uppercase table/view names, this eagerly case-folds every PRAGMA table_list name even though the following query filters those objects out. In a --max-old-space-size=256 process, three valid 5.2 MiB view identifiers (15.6 MiB combined, with every schema record accepted by the configured 16 MiB SQLite limit) made this step add about 5.4 seconds, raise heap use from about 37 MiB to 217 MiB, and raise RSS from about 96 MiB to 358 MiB while selecting a tiny unrelated table. Build the canonical lookup lazily from encountered trigger rows, or limit it to names relevant to the requested scope, so unrelated schema cannot consume most worker headroom.

AGENTS.md reference: AGENTS.md:L49-L50

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. In 8523492 a selected-table scan reports the selected name directly, because it only returns that table's dependents. Other scopes use the exact stored name when one matches and fold the stored names once, lazily, only when a trigger spells its target differently. With your scenario (a tiny selected table plus three 5.2 MiB view names, --max-old-space-size=256), inspectSqliteDatabaseSnapshot took about 760 ms with a 197 MiB heap increase on the previous head. It now takes 143 ms with a 22 MiB increase, the same as main (144 ms, 22 MiB).

The stored-name lookup case-folded every PRAGMA table_list name up front,
so a selected-table inspection of a tiny table paid for large unrelated
identifiers. A selected-table scan only returns the selected table's
dependents, so report the selected name directly; other scopes use an
exact stored name when one matches and fold the stored names once, only
when a trigger spells its target differently.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8523492740

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/sqlite/SqliteDatabaseInspection.ts Outdated
Comment on lines +268 to +269
? unresolvedVirtualTables.find((parent) =>
sqliteIdentifierKey(name).startsWith(parent.prefix),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Hoist identifier folding out of the parent scan

When PRAGMA table_list contains multiple virtual tables whose modules are unavailable and a selected ordinary table has a large uppercase name that matches none of their prefixes, this callback executes sqliteIdentifierKey(name) once per unresolved parent; the selected-table flow then calls shadowParentOf both before and during the schema scan. On Node 24.15, a 1 MiB name with 20 nonmatching parents took about 1.9 seconds here versus 0.09 seconds when folded once, so a multi-MiB identifier plus a few dozen unresolved parents can exhaust the documented 30-second worker deadline. Compute the folded identifier once before .find, as the pre-change implementation did.

AGENTS.md reference: AGENTS.md:L49-L50

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, that was a regression from extracting the helper. In 62b4c3f the name is folded once per call, before .find, as on main, and the selected table's result from the pre-scan check is reused in the scan, so the selected name is folded only once. Measured with a 1 MiB selected table name and 20 fts5 tables made unresolvable (the same byte patch as the boundary test), via inspectSqliteDatabaseSnapshot on Node 24.19: previous head about 1,130 ms, main 58 ms, this head 66–69 ms.

The extracted shadow-parent helper folded the table name inside the find
callback, once per unresolved virtual table, and the selected table was
checked both before and during the schema scan. Fold the name once per
call as before, and reuse the selected table's result in the scan.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@morluto morluto added bug Something isn't working area: evidence Evidence, artifacts, and reconstruction P2: medium Medium priority issue labels Oct 13, 2026
@morluto
morluto merged commit 9407236 into morluto:main Oct 13, 2026
4 checks passed
@morluto

morluto commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Thank you for #2807, @Dragolone. Matching trigger targets with SQLite's ASCII case-folding rule ensures selected-table inspection includes triggers declared with a different case and attributes them to the stored table name. The tests also protect the exact-name refusal behavior for selections that cannot be read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: evidence Evidence, artifacts, and reconstruction bug Something isn't working P2: medium Medium priority issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants