mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 05:37:43 +01:00
* test(ai): clear every platform-chain key, not just Groq and OpenRouter liveness.test.ts and ai-assist-any-form.test.ts staged their providers by setting or deleting GROQ_API_KEY and OPENROUTER_API_KEY only. On a host that holds GEMINI_API_KEY (the bitbaum box does), Gemini joined the serving chain and answered: "no platform key configured" read 200 instead of 503, and the Groq-down fallback test never asked Groq. Both tests now clear every keyEnv servingChain() reads, plus PLATFORM_OLLAMA_URL, before staging their own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(ci): read each file once in the import-graph walks no-background-ai.test.ts and serverOnlyModules.test.ts walk the import graph from every entry point and re-read (and re-resolve) the shared tree from disk for each one. On the box's sandboxed filesystem that took 60-140s and timed out under a full verify run. Each file is now read once per run; the graph does not change mid-run, so results are identical. Both files: ~185s -> ~9s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: cut redundant filesystem work from seven tree-scanning tests The pre-push unit run timed out 8 tests at 30s on the box (38-132s each under the suite's parallel I/O; 2-11s alone). Assertions are unchanged. - capability-is-a-row: one grep for every consulted capability, shared, instead of a full-tree grep per capability per test (two tests, ~19s -> ~4s). - tailwind-v4-dead-utilities: the tree is listed once for both tests. - neutral-username, profile-column-grant-drift, noHardcodedRates, slugify: walk with the directory entry's own type instead of a stat per file (measured 1.9-2.8s -> 0.4-1.0s for src/). Each already reads every file exactly once, which cannot get cheaper, so they also get the 120s timeout serverOnlyModules already uses. - track-record-tool: not a scan; the end-to-end case imports the whole tool executor fresh (~7s alone), so it gets the same 120s timeout. src/ has no symlinks, so the Dirent walk lists the same files as statSync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
6.5 KiB
TypeScript
179 lines
6.5 KiB
TypeScript
/**
|
|
* `.server.ts` must actually stay on the server.
|
|
*
|
|
* The suffix is a convention, and conventions don't fail builds. The stakes are
|
|
* concrete: `services/currency/rateSource.server.ts` reaches a third party and
|
|
* kicks off a refresh the moment it loads. Bundled into a client chunk it ships
|
|
* an outbound CoinGecko call to every visitor — a CSP violation, a privacy
|
|
* leak, and one upstream request per person instead of one per minute for the
|
|
* whole platform. Browsers get rates from our own /api/rates instead.
|
|
*
|
|
* This checked only whether a `'use client'` file imported a `.server` module
|
|
* DIRECTLY, and that is not how bundling works. Anything a client component
|
|
* reaches, through however many hops, is compiled into the client bundle — and
|
|
* the intermediate files carry no directive to give the game away.
|
|
*
|
|
* It shipped. Measured in production 2026-08-28: the browser fetched
|
|
* `https://api.coingecko.com/api/v3/simple/price` directly on page load, 534ms
|
|
* in the critical path, and `api.coingecko.com` was present in a client chunk
|
|
* on disk. The chain was three hops and every file after the first looked
|
|
* innocent:
|
|
*
|
|
* dashboard/bookings/page.tsx 'use client'
|
|
* → services/bookings/index.ts (no directive)
|
|
* → services/currency/rates.server.ts
|
|
* → services/currency/rateSource.server.ts → CoinGecko
|
|
*
|
|
* So the walk is transitive now, and the failure names the whole chain — a bare
|
|
* "this file is an offender" is close to useless when the import that matters
|
|
* is three files away from the one you have to edit.
|
|
*/
|
|
|
|
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
import { dirname, join, resolve } from 'node:path';
|
|
|
|
const SRC = 'src';
|
|
|
|
/** `from '…'` and `import('…')`, capturing the specifier. */
|
|
const IMPORT_SPECIFIER = /(?:from|import\()\s*['"]([^'"]+)['"]/g;
|
|
|
|
function sourceFiles(dir: string, out: string[] = []): string[] {
|
|
for (const name of readdirSync(dir)) {
|
|
const path = join(dir, name);
|
|
if (statSync(path).isDirectory()) {
|
|
sourceFiles(path, out);
|
|
} else if (/\.tsx?$/.test(name)) {
|
|
out.push(path);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function isClientFile(path: string): boolean {
|
|
return /^\s*['"]use client['"]/m.test(readFileSync(path, 'utf8'));
|
|
}
|
|
|
|
/**
|
|
* Turn a specifier into a file we can keep walking, or null for anything that
|
|
* cannot pull our own server code in (node_modules, css, assets).
|
|
*/
|
|
function resolveSpecifier(fromFile: string, specifier: string): string | null {
|
|
let base: string;
|
|
if (specifier.startsWith('@/')) {
|
|
base = join(SRC, specifier.slice(2));
|
|
} else if (specifier.startsWith('.')) {
|
|
base = resolve(dirname(fromFile), specifier);
|
|
} else {
|
|
return null; // package import
|
|
}
|
|
|
|
for (const candidate of [
|
|
base,
|
|
`${base}.ts`,
|
|
`${base}.tsx`,
|
|
join(base, 'index.ts'),
|
|
join(base, 'index.tsx'),
|
|
]) {
|
|
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
return candidate;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Every client entry walks much of the same graph, so each file's imports are
|
|
// read and resolved once per run, not once per entry.
|
|
const importsCache = new Map<string, string[]>();
|
|
|
|
function importsOf(file: string): string[] {
|
|
const cached = importsCache.get(file);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
const source = readFileSync(file, 'utf8');
|
|
const out: string[] = [];
|
|
for (const match of source.matchAll(IMPORT_SPECIFIER)) {
|
|
const resolved = resolveSpecifier(file, match[1]);
|
|
if (resolved) {
|
|
out.push(resolved);
|
|
}
|
|
}
|
|
importsCache.set(file, out);
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* @returns the import chain from `entry` to the first `.server` module it can
|
|
* reach, or null. Depth-first and memoized per entry: the graph is small, and
|
|
* naming ONE complete chain per offending page is what makes this fixable.
|
|
*/
|
|
function chainToServerModule(entry: string): string[] | null {
|
|
const seen = new Set<string>();
|
|
|
|
function walk(file: string, trail: string[]): string[] | null {
|
|
if (seen.has(file)) {
|
|
return null;
|
|
}
|
|
seen.add(file);
|
|
|
|
for (const next of importsOf(file)) {
|
|
const nextTrail = [...trail, next];
|
|
if (/\.server\.tsx?$/.test(next)) {
|
|
return nextTrail;
|
|
}
|
|
const deeper = walk(next, nextTrail);
|
|
if (deeper) {
|
|
return deeper;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
return walk(entry, [entry]);
|
|
}
|
|
|
|
/**
|
|
* How many client entry points can currently reach a `.server` module.
|
|
*
|
|
* 431 when the transitive walk was first switched on, and deliberately left at
|
|
* exactly that: a ratchet, not a target. Demanding zero today would make this
|
|
* red about work nobody has scheduled, which is how a gate teaches people to
|
|
* ignore it. It may fall or hold, never rise.
|
|
*
|
|
* Nothing has been retired yet, and the number is not a to-do list to grind
|
|
* down one file at a time. The chains run through shared helpers —
|
|
* `config/cat-plans` → `services/cat/credit-metering` → `rates.server` accounts
|
|
* for a large share on its own — so cutting one edge retires dozens at once.
|
|
* Splitting those modules is a real refactor and belongs in its own change,
|
|
* not smuggled into the commit that first makes the problem visible.
|
|
*
|
|
* The concrete harm this was hiding is fixed separately and defensively, in
|
|
* rateSource.server.ts: even bundled into a client chunk it now refuses to call
|
|
* out from a browser.
|
|
*/
|
|
const CLIENT_TO_SERVER_BASELINE = 431;
|
|
|
|
describe("'use client' modules never reach server-only code", () => {
|
|
it('does not let more client entry points reach a *.server module', () => {
|
|
const offenders = sourceFiles(SRC)
|
|
.filter(isClientFile)
|
|
.map(file => chainToServerModule(file))
|
|
.filter((chain): chain is string[] => chain !== null)
|
|
.map(chain => chain.join('\n → '));
|
|
|
|
if (offenders.length > CLIENT_TO_SERVER_BASELINE) {
|
|
// Print one full chain so the failure is actionable: the import that
|
|
// matters is usually several files from the one you would think to open.
|
|
throw new Error(
|
|
`${offenders.length} client entry points reach a .server module, up from ` +
|
|
`${CLIENT_TO_SERVER_BASELINE}. One chain:\n\n ${offenders[0]}\n`
|
|
);
|
|
}
|
|
|
|
expect(offenders.length).toBeLessThanOrEqual(CLIENT_TO_SERVER_BASELINE);
|
|
// It walks every client file's import graph: ~29s on an idle laptop, so the
|
|
// default 30s timeout failed it whenever anything else was running (twice
|
|
// on 2026-09-25, blocking a push while the same run passed in isolation).
|
|
}, 120_000);
|
|
});
|