Files
391f08b801 fix(profiles): count wallets with a function that exists, and gate the class (#760)
The profile page counted a visitor-visible wallet by calling an RPC named
`get_entity_wallets`. That function appears in no migration and returns
PGRST202 in production — it has never existed. The call sat inside a
try/catch whose comment read "if it errs we just show 0", so the error
path was not a fallback, it was the only path, for as long as the code
has been there.

The count is not cosmetic. ProfileLayout hides the Wallets tab from
visitors unless walletCount > 0 or the profile still carries a legacy
bitcoin_address/lightning_address. A profile whose receive methods live
in the modern wallets table therefore showed a visitor no way to pay it.
Confirmed on the live site: testuser123 has an active on-chain wallet and
their public profile offers only Overview / Timeline / Info.

Measured 2026-08-26: 3 of 83 profiles have an active wallet and only 1
has a legacy address, so 2 profiles are affected today. Small only
because wallet adoption is small — it scales with exactly the number we
want to grow, and would have quietly blunted any push to grow it.

Counted through the same admin-client path GET /api/wallets already uses
rather than by writing the missing function. That route's comment states
the intent — "the API is the one public surface, so raw PostgREST can no
longer enumerate balances or other wallet internals" — and a SECURITY
DEFINER function would open a second public surface over a table holding
nwc_connection_uri, a write-only secret. Counting adds no surface: it
returns a number, never a row. Failures are logged rather than swallowed,
so a permanently-broken count cannot hide again.

Asking production which functions PostgREST exposes turned up three more
RPCs that do not exist: update_presence and set_typing_indicator (so
presence and typing indicators are inert), and create_timeline_event on a
fallback branch. Four instances is a class, so check:rpc-exists joins
`verify`: every `.rpc('name')` must have a CREATE FUNCTION in
supabase/migrations. The three remaining are baselined with their
consequence written down — each needs its own migration and design.

The gate is parser-based so CI needs no database, and `--verify` proves
the parser against the live one: 37 called names checked against 80 live
functions, migrations and database agreeing on every one. Proven by
mutation too — re-adding the removed call turns it red.

Its own first run said "RPCs called: 0" and passed. It had reached for
blankNonCode, which blanks string CONTENTS, so it had erased every name
it was looking for — green because blind, the exact failure it exists to
catch. The shared tokenizer now offers blankComments for the case where
the string literal IS the thing you are looking for.

Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 01:36:23 +00:00

116 lines
3.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Shared source tokenizer for the repo scripts that scan TypeScript by hand.
*
* Extracted from check-schema-columns.mjs when check-currency-units.mjs needed
* the same thing on its first run: that gate stripped only `//` comments, and
* the very first file it read was the doc comment DESCRIBING the bug it looks
* for, inside a block comment. One definition, so a third scanner cannot
* reinvent the naive version a fourth time.
*/
/**
* Blank out comments and string/template/regex contents, preserving length so
* every offset and bracket-depth outside them stays exact.
*
* This has to be one left-to-right pass, not a sequence of regex replaces,
* because the two constructs hide inside each other and order cannot fix that:
*
* - Stripping comments first eats real code. A validation message reading
* `'…URL (e.g., https://example.com)'` contains `//`, so a `//`-to-newline
* rule blanked the rest of that line INCLUDING the closing `)` and `}`.
* Depth never recovered, extraction stopped at that field, and projectSchema
* silently reported 9 fields instead of 15 — the gate went green on an
* injected phantom column. Caught by mutation testing, not by reading it.
* - Stripping strings first mis-parses an apostrophe in a comment.
*
* Regex literals are only recognised where a regex can legally begin (after
* `(,=:[!&|?{};` or a newline), so a division sign is never mistaken for one.
*/
function walk(src, { keepStrings = false } = {}) {
const out = src.split('');
const blank = (from, to) => {
for (let k = from; k < to && k < out.length; k++) if (out[k] !== '\n') out[k] = ' ';
};
let i = 0;
let prevSignificant = '';
while (i < src.length) {
const ch = src[i];
const next = src[i + 1];
if (ch === '/' && next === '/') {
const end = src.indexOf('\n', i);
blank(i, end === -1 ? src.length : end);
i = end === -1 ? src.length : end;
continue;
}
if (ch === '/' && next === '*') {
const end = src.indexOf('*/', i + 2);
blank(i, end === -1 ? src.length : end + 2);
i = end === -1 ? src.length : end + 2;
continue;
}
if (ch === "'" || ch === '"' || ch === '`') {
let j = i + 1;
while (j < src.length) {
if (src[j] === '\\') {
j += 2;
continue;
}
if (src[j] === ch) break;
j++;
}
if (!keepStrings) blank(i + 1, j);
i = j + 1;
prevSignificant = ch;
continue;
}
if (ch === '/' && '(,=:[!&|?{};\n'.includes(prevSignificant)) {
let j = i + 1;
let inClass = false;
while (j < src.length) {
if (src[j] === '\\') {
j += 2;
continue;
}
if (src[j] === '[') inClass = true;
else if (src[j] === ']') inClass = false;
else if (src[j] === '/' && !inClass) break;
else if (src[j] === '\n') break;
j++;
}
if (src[j] === '/') {
if (!keepStrings) blank(i + 1, j);
i = j + 1;
prevSignificant = '/';
continue;
}
}
if (!/\s/.test(ch)) prevSignificant = ch;
i++;
}
return out.join('');
}
/**
* Blank comments AND string/regex contents. Use when you are parsing STRUCTURE
* (bracket depth, property names) and the literals are noise.
*/
function blankNonCode(src) {
return walk(src);
}
/**
* Blank comments only, leaving string contents intact. Use when the thing you
* are looking for IS a string literal — an RPC name, a table name, a route.
*
* This exists because check-rpc-exists.mjs reached for blankNonCode first and
* reported "RPCs called: 0": every name it searched for lives inside quotes, so
* the tokenizer had erased them all. The gate was green because it was blind,
* which is the exact failure it was written to catch.
*/
function blankComments(src) {
return walk(src, { keepStrings: true });
}
export { blankNonCode, blankComments };