') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); perf(core): cheapen the authz transport scan so a slow test stops aborting the Test Core shard by claude[bot] · Pull Request #13656 · objectstack-ai/objectstack · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 79 additions & 8 deletions packages/core/src/security/authz-store-unavailable.test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -192,39 +192,110 @@ const isScaffolding = (rel: string) =>
|| /\.fixtures?\.ts$/.test(rel) || rel.includes(`${sep}dogfood${sep}`);

function walk(dir: string, out: string[] = []): string[] {
for (const entry of readdirSync(dir)) {
if (entry === 'node_modules' || entry === 'dist' || entry === '.turbo') continue;
const full = join(dir, entry);
if (statSync(full).isDirectory()) walk(full, out);
// `withFileTypes` answers "is this a directory?" from the `readdir` result
// itself, so the walk costs one syscall per DIRECTORY instead of one per
// ENTRY (5,926 `statSync` calls on this tree). The symlink limb preserves the
// old `statSync` semantics exactly, and is not optional: `Dirent.isDirectory()`
// describes the LINK, not its target, so without it a symlinked directory
// would stop being descended and a transport behind one would drop out of the
// ledger's reach — the one silence this suite exists to prevent.
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name === 'dist' || entry.name === '.turbo') continue;
const full = join(dir, entry.name);
if (entry.isDirectory() || (entry.isSymbolicLink() && statSync(full).isDirectory())) walk(full, out);
else if (full.endsWith('.ts')) out.push(full);
}
return out;
}

/**
* The call this ledger tracks, as BYTES. `readFileSync(f)` hands back the
* file's bytes with no UTF-8 decode, and `Buffer.prototype.includes` searches
* them directly.
*
* The two spellings cannot disagree: the needle is pure ASCII, and an ASCII
* byte never occurs inside a multi-byte UTF-8 sequence (continuation bytes are
* all >= 0x80), so a byte hit and a decoded-string hit are the same hit — with
* no lossy-replacement step in between. Measured on this tree, decoding was
* 147 MB of transient JS strings per suite run and roughly half the scan's cost.
*/
const TRANSPORT_CALL = Buffer.from('resolveAuthzContext({');

/** Every PRODUCTION file that calls `resolveAuthzContext`, rebuilt from source. */
function discoverTransports(): string[] {
function scanTransports(): string[] {
return walk(join(REPO_ROOT, 'packages'))
.filter((f) => readFileSync(f, 'utf8').includes('resolveAuthzContext({'))
.map((f) => relative(REPO_ROOT, f).split(sep).join('/'))
// ⭐ The scaffolding filter runs BEFORE the read, not after it. A path
// belongs to the result iff it BOTH contains the call AND is not
// scaffolding; set intersection does not care which half is tested first,
// so the reordering is semantically free. What it removes is 2,979 of
// 5,076 files (56% of the bytes) that were read into memory in full and
// only then thrown away for their path.
.filter((rel) => !isScaffolding(rel.split('/').join(sep)))
.filter((rel) => readFileSync(join(REPO_ROOT, rel)).includes(TRANSPORT_CALL))
.sort();
}

/**
* ⛔ Computed once per PROCESS, and only ever FROM SOURCE.
*
* Both tests below need the whole enumeration, and deriving it twice walked and
* read the tree twice for one answer — the cost that timed this suite out at
* the default 5000 ms and aborted the shard around it.
*
* What this cache must NEVER become is a checked-in list, a snapshot fixture,
* or a cache keyed on anything that can outlive a commit. The enumeration is
* rebuilt from source on every run precisely so a transport added later cannot
* inherit the old silence unnoticed; a curated list would answer only "the
* doors I remembered". A fresh process — that is, every run of this suite —
* walks the tree again. The copy is returned so no caller can mutate the
* enumeration out from under the other test.
*/
let SCANNED: readonly string[] | undefined;
function discoverTransports(): string[] {
return [...(SCANNED ??= scanTransports())];
}

/**
* The budget for the two tests that SCAN, stated rather than inherited.
*
* vitest's default 5000 ms is the budget for a test that does no I/O, and this
* suite inherited it silently. It was not a decision, and it was measurably the
* wrong one: the scan blew it on CI while passing on a developer box, so the
* only signal anyone got was `Test timed out in 5000ms` on PRs that had not
* touched authorization — and because a timeout ABORTS THE SHARD, one slow test
* cost eleven other packages their entire run.
*
* That asymmetry is why this number is generous rather than tight. A budget set
* close to the observed cost buys nothing (the scan is not a thing we want to
* race) and risks the catastrophic, non-local failure again on a slow runner; a
* generous one costs nothing when the test passes. Measured on this tree with a
* COLD page cache, the scan is 255 ms, so this is ~118x its measured cost.
*
* ⛔ This is a BUDGET, not an assertion about speed — deliberately not
* `expect(elapsed).toBeLessThan(n)`, which on a shared CI runner is flaky by
* construction and would just re-file this card's successor. And it is not the
* repair: the repair is that the scan reads 2,097 files once instead of 5,076
* twice. If this budget is ever reached, the tree has outgrown a linear scan and
* the answer is to re-engineer it, ⛔ never to raise this number.
*/
const SCAN_BUDGET_MS = 30_000;

describe('[#13279] every transport that authorizes through resolveAuthzContext', () => {
it('CONTROL: the scanner finds transports at all, and finds THIS repo', () => {
// Without this, a broken walk would return [] and the set-equality audit
// below would be comparing two empty sets and passing.
const found = discoverTransports();
expect(found.length).toBeGreaterThanOrEqual(8);
expect(found).toContain('packages/rest/src/rest-server.ts');
});
}, SCAN_BUDGET_MS);

it('⭐ SET EQUALITY: the ledger names exactly the transports source contains', () => {
// A NEW transport is red here until it is classified — which is the whole
// point: the ruling is about every transport, including the ones written
// after it.
expect(discoverTransports()).toEqual(Object.keys(TRANSPORT_LEDGER).sort());
});
}, SCAN_BUDGET_MS);

it.each(Object.entries(TRANSPORT_LEDGER))(
'%s (%s) — a fail-closed catch re-raises the outage instead of degrading it',
Expand Down
Loading