From 6edb093c941a91e3851556b81e87ae6d08cb29ee Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" <29069505+gewenyu99@users.noreply.github.com> Date: Tue, 7 Jul 2026 09:54:06 -0400 Subject: [PATCH 1/4] fix(detection): stop generic Node claiming Vite apps and Android claiming Flutter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two misdetections from the 2026-07-07 triage (the "$8.29 bug" — wrong-framework integrations burning agent spend): - javascript-node's detect() matched on package.json alone, and it sits before javascript_web in the detection loop, so every Vite app was claimed as a server-side Node project and got posthog-node integrated into a browser codebase (2 users hit this). It now returns false when `vite` is a declared dependency or a vite.config.* exists, letting detection fall through to javascript_web. - android's detect() claimed Flutter projects via their android/ subtree (manifests + MainActivity.kt satisfy strategy 2 — the code even had a "we'll figure that out later" comment). It now bails when pubspec.yaml sits at the project root. A proper flutter framework config (integration-flutter already exists in the skill menu) is the follow-up; this stops the wrong integration today. Adds the first framework-detection test file, including a registry-order guard that language fallbacks stay after every framework in the Integration enum, and an end-to-end assertion that a Vite React app resolves to javascript_web. Generated-By: PostHog Code Task-Id: bcb96bcb-e37f-421d-841d-738647c73b8e --- .../android/android-wizard-agent.ts | 9 ++ .../javascript-node-wizard-agent.ts | 27 +++- src/lib/detection/__tests__/framework.test.ts | 121 ++++++++++++++++++ 3 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/lib/detection/__tests__/framework.test.ts diff --git a/src/frameworks/android/android-wizard-agent.ts b/src/frameworks/android/android-wizard-agent.ts index bf0f317b4..24026bee7 100644 --- a/src/frameworks/android/android-wizard-agent.ts +++ b/src/frameworks/android/android-wizard-agent.ts @@ -41,6 +41,15 @@ export const ANDROID_AGENT_CONFIG: FrameworkConfig = { detect: async (options) => { const { installDir } = options; + // A Flutter project ships a full android/ subtree (manifests, gradle + // wiring, a MainActivity.kt), so both strategies below would claim it + // and integrate posthog-android into the native shell instead of the + // Flutter app. pubspec.yaml at the root is the Flutter/Dart marker — + // bail out so this config never claims a Flutter project. + if (fs.existsSync(path.join(installDir, 'pubspec.yaml'))) { + return false; + } + // Strategy 1: Check for build.gradle(.kts) with Android plugin for (const name of ['build.gradle', 'build.gradle.kts']) { const buildGradlePath = path.join(installDir, name); diff --git a/src/frameworks/javascript-node/javascript-node-wizard-agent.ts b/src/frameworks/javascript-node/javascript-node-wizard-agent.ts index fb12fb38e..aeab1fa3f 100644 --- a/src/frameworks/javascript-node/javascript-node-wizard-agent.ts +++ b/src/frameworks/javascript-node/javascript-node-wizard-agent.ts @@ -1,9 +1,21 @@ /* Generic Node.js language wizard using posthog-agent with PostHog MCP */ +import * as fs from 'node:fs'; +import * as path from 'node:path'; import type { FrameworkConfig } from '@lib/framework-config'; import { Integration } from '@lib/constants'; import { tryGetPackageJson } from '@utils/setup-utils'; +import { hasDeclaredDependency } from '@utils/package-json'; import { detectNodePackageManagers } from '@lib/detection/package-manager'; +const VITE_CONFIG_FILES = [ + 'vite.config.js', + 'vite.config.ts', + 'vite.config.mjs', + 'vite.config.mts', + 'vite.config.cjs', + 'vite.config.cts', +]; + type JavaScriptNodeContext = Record; export const JAVASCRIPT_NODE_AGENT_CONFIG: FrameworkConfig = @@ -22,7 +34,20 @@ export const JAVASCRIPT_NODE_AGENT_CONFIG: FrameworkConfig { const packageJson = await tryGetPackageJson(options); - return !!packageJson; + if (!packageJson) return false; + // A Vite project is a frontend build, not a server-side Node app. + // This config sits before `javascript_web` in the detection loop and + // matches on package.json alone, so without this exclusion it claims + // every Vite app and integrates posthog-node into a browser project. + if (hasDeclaredDependency('vite', packageJson)) return false; + if ( + VITE_CONFIG_FILES.some((file) => + fs.existsSync(path.join(options.installDir, file)), + ) + ) { + return false; + } + return true; }, }, diff --git a/src/lib/detection/__tests__/framework.test.ts b/src/lib/detection/__tests__/framework.test.ts new file mode 100644 index 000000000..1a3a25ddd --- /dev/null +++ b/src/lib/detection/__tests__/framework.test.ts @@ -0,0 +1,121 @@ +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { detectFramework } from '@lib/detection/framework'; +import { Integration } from '@lib/constants'; +import { JAVASCRIPT_NODE_AGENT_CONFIG } from '../../../frameworks/javascript-node/javascript-node-wizard-agent'; +import { ANDROID_AGENT_CONFIG } from '../../../frameworks/android/android-wizard-agent'; + +/** A throwaway project dir seeded with the given files. */ +function makeProject(files: Record): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wizard-detect-')); + for (const [rel, content] of Object.entries(files)) { + const abs = path.join(dir, rel); + fs.mkdirSync(path.dirname(abs), { recursive: true }); + fs.writeFileSync(abs, content); + } + return dir; +} + +const tmpDirs: string[] = []; +function project(files: Record): { installDir: string } { + const dir = makeProject(files); + tmpDirs.push(dir); + return { installDir: dir }; +} + +afterAll(() => { + for (const dir of tmpDirs) { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); + +describe('detection registry order', () => { + test('language fallbacks come after every framework', () => { + // detectFramework loops Object.values(Integration) first-match-wins, so a + // generic fallback placed before a framework would shadow it. Guard the + // ordering: every language fallback must sit after the last framework. + const order = Object.values(Integration); + const fallbacks = [ + Integration.python, + Integration.ruby, + Integration.javascriptNode, + Integration.javascript_web, + ]; + const lastFramework = Math.max( + ...order + .filter((i) => !fallbacks.includes(i)) + .map((i) => order.indexOf(i)), + ); + for (const fallback of fallbacks) { + expect(order.indexOf(fallback)).toBeGreaterThan(lastFramework); + } + }); +}); + +describe('javascript-node detect (generic Node fallback)', () => { + const detect = JAVASCRIPT_NODE_AGENT_CONFIG.detection.detect; + + test('claims a plain Node project', async () => { + const opts = project({ + 'package.json': JSON.stringify({ dependencies: { express: '^4' } }), + }); + await expect(detect(opts)).resolves.toBe(true); + }); + + test('does not claim a project with vite in devDependencies', async () => { + const opts = project({ + 'package.json': JSON.stringify({ + dependencies: { react: '^19' }, + devDependencies: { vite: '^6' }, + }), + }); + await expect(detect(opts)).resolves.toBe(false); + }); + + test('does not claim a project with a vite config file', async () => { + for (const config of ['vite.config.ts', 'vite.config.mjs']) { + const opts = project({ + 'package.json': JSON.stringify({ dependencies: {} }), + [config]: 'export default {}', + }); + await expect(detect(opts)).resolves.toBe(false); + } + }); + + test('a Vite React app resolves to javascript_web, not javascript_node', async () => { + const opts = project({ + 'package.json': JSON.stringify({ + dependencies: { react: '^19', 'react-dom': '^19' }, + devDependencies: { vite: '^6' }, + }), + 'package-lock.json': '{}', + 'vite.config.ts': 'export default {}', + 'index.html': '', + }); + await expect(detectFramework(opts.installDir)).resolves.toBe( + Integration.javascript_web, + ); + }); +}); + +describe('android detect', () => { + const detect = ANDROID_AGENT_CONFIG.detection.detect; + + const androidGradle = `plugins { id 'com.android.application' }`; + + test('claims a native Android project', async () => { + const opts = project({ 'build.gradle': androidGradle }); + await expect(detect(opts)).resolves.toBe(true); + }); + + test('does not claim a Flutter project despite its android/ subtree', async () => { + const opts = project({ + 'pubspec.yaml': 'name: my_flutter_app\nenvironment:\n sdk: ^3.0.0\n', + 'android/build.gradle': androidGradle, + 'android/app/src/main/AndroidManifest.xml': '', + 'android/app/src/main/kotlin/MainActivity.kt': 'class MainActivity', + }); + await expect(detect(opts)).resolves.toBe(false); + }); +}); From ade921fef1cf32f12d4b617e7291660d21610246 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" <29069505+gewenyu99@users.noreply.github.com> Date: Tue, 7 Jul 2026 11:37:33 -0400 Subject: [PATCH 2/4] fix(detection): make language fallbacks an explicit failover phase, generic Node last MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per review: the structural problem was ordering, not just the Vite special case. detectFramework relied on the Integration enum's declaration order, which put javascriptNode (matches any package.json) BEFORE javascript_web (needs lockfile + a frontend signal) — so generic Node shadowed the web fallback for every frontend project that matched no specific framework, not only Vite apps. detectFramework now walks an explicit DETECTION_ORDER: every real framework first, then the language fallbacks (python, ruby, javascript_web, javascriptNode) as a second phase, with generic Node dead last. Built explicitly rather than trusting enum order so adding an Integration in the wrong enum position can't silently put a broad fallback ahead of a specific framework. Better tests: order invariants against the exported DETECTION_ORDER (full coverage/no dupes, fallbacks after every framework, web before node, node last overall) plus end-to-end fixtures — a Next.js app beats the fallbacks, a Vite React app and a plain browser app (lockfile + index.html, previously misclaimed by node) resolve to javascript_web, an express project still falls through to javascript_node, and a Flutter project is claimed by nothing. The vite/pubspec detect() exclusions from the previous commit stay as belt-and-suspenders for lockfile-less scaffolds. Generated-By: PostHog Code Task-Id: bcb96bcb-e37f-421d-841d-738647c73b8e --- src/lib/detection/__tests__/framework.test.ts | 137 +++++++++++++----- src/lib/detection/framework.ts | 37 ++++- 2 files changed, 137 insertions(+), 37 deletions(-) diff --git a/src/lib/detection/__tests__/framework.test.ts b/src/lib/detection/__tests__/framework.test.ts index 1a3a25ddd..eab4f5dcd 100644 --- a/src/lib/detection/__tests__/framework.test.ts +++ b/src/lib/detection/__tests__/framework.test.ts @@ -1,7 +1,7 @@ import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; -import { detectFramework } from '@lib/detection/framework'; +import { DETECTION_ORDER, detectFramework } from '@lib/detection/framework'; import { Integration } from '@lib/constants'; import { JAVASCRIPT_NODE_AGENT_CONFIG } from '../../../frameworks/javascript-node/javascript-node-wizard-agent'; import { ANDROID_AGENT_CONFIG } from '../../../frameworks/android/android-wizard-agent'; @@ -30,26 +30,112 @@ afterAll(() => { } }); -describe('detection registry order', () => { - test('language fallbacks come after every framework', () => { - // detectFramework loops Object.values(Integration) first-match-wins, so a - // generic fallback placed before a framework would shadow it. Guard the - // ordering: every language fallback must sit after the last framework. - const order = Object.values(Integration); - const fallbacks = [ - Integration.python, - Integration.ruby, +const FALLBACKS = [ + Integration.python, + Integration.ruby, + Integration.javascript_web, + Integration.javascriptNode, +]; + +describe('DETECTION_ORDER (fallback phase invariants)', () => { + test('covers every Integration exactly once', () => { + expect([...DETECTION_ORDER].sort()).toEqual( + Object.values(Integration).sort(), + ); + expect(new Set(DETECTION_ORDER).size).toBe(DETECTION_ORDER.length); + }); + + test('every language fallback comes after every framework', () => { + // Broad fallback predicates (any package.json, any .py file) would + // shadow specific frameworks if tried earlier. + const firstFallback = Math.min( + ...FALLBACKS.map((f) => DETECTION_ORDER.indexOf(f)), + ); + const lastFramework = Math.max( + ...DETECTION_ORDER.filter((i) => !FALLBACKS.includes(i)).map((i) => + DETECTION_ORDER.indexOf(i), + ), + ); + expect(firstFallback).toBeGreaterThan(lastFramework); + }); + + test('generic Node is the last resort of the entire detection', () => { + // javascriptNode matches on package.json alone — anything after it would + // be unreachable for any JS project. + expect(DETECTION_ORDER[DETECTION_ORDER.length - 1]).toBe( Integration.javascriptNode, + ); + }); + + test('javascript_web is tried before javascriptNode', () => { + // web requires a lockfile + a frontend signal; node requires only + // package.json. Specific before broad, or web is unreachable. + expect(DETECTION_ORDER.indexOf(Integration.javascript_web)).toBeLessThan( + DETECTION_ORDER.indexOf(Integration.javascriptNode), + ); + }); +}); + +describe('detectFramework (end-to-end over real project dirs)', () => { + test('a framework beats the fallbacks even though package.json matches Node', async () => { + const opts = project({ + 'package.json': JSON.stringify({ + dependencies: { next: '^16', react: '^19' }, + }), + 'package-lock.json': '{}', + }); + await expect(detectFramework(opts.installDir)).resolves.toBe( + Integration.nextjs, + ); + }); + + test('a Vite React app resolves to javascript_web, not javascript_node', async () => { + const opts = project({ + 'package.json': JSON.stringify({ + dependencies: { react: '^19', 'react-dom': '^19' }, + devDependencies: { vite: '^6' }, + }), + 'package-lock.json': '{}', + 'vite.config.ts': 'export default {}', + 'index.html': '', + }); + await expect(detectFramework(opts.installDir)).resolves.toBe( Integration.javascript_web, - ]; - const lastFramework = Math.max( - ...order - .filter((i) => !fallbacks.includes(i)) - .map((i) => order.indexOf(i)), ); - for (const fallback of fallbacks) { - expect(order.indexOf(fallback)).toBeGreaterThan(lastFramework); - } + }); + + test('a plain browser app (lockfile + index.html, no bundler) resolves to javascript_web', async () => { + // The ordering fix beyond Vite: before the explicit fallback phase, + // javascriptNode sat ahead of javascript_web and claimed every frontend + // project that matched no specific framework. + const opts = project({ + 'package.json': JSON.stringify({ dependencies: {} }), + 'package-lock.json': '{}', + 'index.html': '', + }); + await expect(detectFramework(opts.installDir)).resolves.toBe( + Integration.javascript_web, + ); + }); + + test('a server-side Node project still falls through to javascript_node', async () => { + const opts = project({ + 'package.json': JSON.stringify({ dependencies: { express: '^4' } }), + 'package-lock.json': '{}', + }); + await expect(detectFramework(opts.installDir)).resolves.toBe( + Integration.javascriptNode, + ); + }); + + test('a Flutter project is not claimed by anything', async () => { + const opts = project({ + 'pubspec.yaml': 'name: my_flutter_app\nenvironment:\n sdk: ^3.0.0\n', + 'android/build.gradle': `plugins { id 'com.android.application' }`, + 'android/app/src/main/AndroidManifest.xml': '', + 'android/app/src/main/kotlin/MainActivity.kt': 'class MainActivity', + }); + await expect(detectFramework(opts.installDir)).resolves.toBeUndefined(); }); }); @@ -82,21 +168,6 @@ describe('javascript-node detect (generic Node fallback)', () => { await expect(detect(opts)).resolves.toBe(false); } }); - - test('a Vite React app resolves to javascript_web, not javascript_node', async () => { - const opts = project({ - 'package.json': JSON.stringify({ - dependencies: { react: '^19', 'react-dom': '^19' }, - devDependencies: { vite: '^6' }, - }), - 'package-lock.json': '{}', - 'vite.config.ts': 'export default {}', - 'index.html': '', - }); - await expect(detectFramework(opts.installDir)).resolves.toBe( - Integration.javascript_web, - ); - }); }); describe('android detect', () => { diff --git a/src/lib/detection/framework.ts b/src/lib/detection/framework.ts index 4efd9f6bf..737008561 100644 --- a/src/lib/detection/framework.ts +++ b/src/lib/detection/framework.ts @@ -10,14 +10,43 @@ import { Integration, DETECTION_TIMEOUT_MS } from '@lib/constants'; import { FRAMEWORK_REGISTRY } from '@lib/registry'; /** - * Loop through all registered frameworks and return the first one - * whose `detect()` predicate matches the given directory. - * Returns undefined if no framework is detected or detection times out. + * Language fallbacks, tried ONLY after every real framework has failed to + * match — their predicates are broad (any package.json, any .py project), so + * running them earlier would shadow specific frameworks. Order within the + * phase matters too: `javascript_web` (lockfile + a frontend signal) is more + * specific than `javascriptNode` (any package.json at all), so web is tried + * first and generic Node is the last resort of the entire detection. + */ +const LANGUAGE_FALLBACKS: readonly Integration[] = [ + Integration.python, + Integration.ruby, + Integration.javascript_web, + Integration.javascriptNode, +]; + +/** + * The full detection order: every framework (enum order), then the language + * fallbacks. Built explicitly rather than trusting enum declaration order, + * so adding an Integration in the wrong place can't silently put a broad + * fallback ahead of a specific framework. Exported for the ordering tests. + */ +export const DETECTION_ORDER: readonly Integration[] = [ + ...Object.values(Integration).filter( + (integration) => !LANGUAGE_FALLBACKS.includes(integration), + ), + ...LANGUAGE_FALLBACKS, +]; + +/** + * Return the first integration in DETECTION_ORDER whose `detect()` predicate + * matches the given directory: real frameworks first, language fallbacks + * after, generic Node last of all. Returns undefined if nothing matches or + * detection times out. */ export async function detectFramework( installDir: string, ): Promise { - for (const integration of Object.values(Integration)) { + for (const integration of DETECTION_ORDER) { const config = FRAMEWORK_REGISTRY[integration]; try { const detected = await Promise.race([ From 775647ade5b627ea4d67fee0ab61c66fb6acf3e8 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" <29069505+gewenyu99@users.noreply.github.com> Date: Tue, 7 Jul 2026 11:55:58 -0400 Subject: [PATCH 3/4] =?UTF-8?q?fix(detection):=20reorder=20the=20Integrati?= =?UTF-8?q?on=20enum=20instead=20=E2=80=94=20web=20before=20node,=20no=20n?= =?UTF-8?q?ew=20machinery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per review: framework.ts's plain first-match loop over Object.values(Integration) was already the right mechanism — its doc comment even says "Detection order matters" — so the fix is a two-line enum reorder, not a DETECTION_ORDER layer. Reverted framework.ts and the javascript-node config to main (Vite knowledge doesn't belong in Node detection logic) and swapped javascript_web ahead of javascriptNode in the enum's fallback section, with a comment explaining why node must stay last. A Vite/browser app now falls to javascript_web via ordering alone (lockfile + frontend signal wins before node's any-package.json). The android pubspec.yaml exclusion stays — Flutter has no config of its own to reorder ahead, so the exclusion is the only lever there. Tests: enum-order invariants (fallbacks after every framework; javascriptNode last) + the same end-to-end fixtures (Next.js beats fallbacks, Vite React → web, plain browser app → web, express → node, Flutter → nothing) + the android pair. Generated-By: PostHog Code Task-Id: bcb96bcb-e37f-421d-841d-738647c73b8e --- .../javascript-node-wizard-agent.ts | 27 +----- src/lib/constants.ts | 7 +- src/lib/detection/__tests__/framework.test.ts | 89 +++++-------------- src/lib/detection/framework.ts | 37 +------- 4 files changed, 30 insertions(+), 130 deletions(-) diff --git a/src/frameworks/javascript-node/javascript-node-wizard-agent.ts b/src/frameworks/javascript-node/javascript-node-wizard-agent.ts index aeab1fa3f..fb12fb38e 100644 --- a/src/frameworks/javascript-node/javascript-node-wizard-agent.ts +++ b/src/frameworks/javascript-node/javascript-node-wizard-agent.ts @@ -1,21 +1,9 @@ /* Generic Node.js language wizard using posthog-agent with PostHog MCP */ -import * as fs from 'node:fs'; -import * as path from 'node:path'; import type { FrameworkConfig } from '@lib/framework-config'; import { Integration } from '@lib/constants'; import { tryGetPackageJson } from '@utils/setup-utils'; -import { hasDeclaredDependency } from '@utils/package-json'; import { detectNodePackageManagers } from '@lib/detection/package-manager'; -const VITE_CONFIG_FILES = [ - 'vite.config.js', - 'vite.config.ts', - 'vite.config.mjs', - 'vite.config.mts', - 'vite.config.cjs', - 'vite.config.cts', -]; - type JavaScriptNodeContext = Record; export const JAVASCRIPT_NODE_AGENT_CONFIG: FrameworkConfig = @@ -34,20 +22,7 @@ export const JAVASCRIPT_NODE_AGENT_CONFIG: FrameworkConfig { const packageJson = await tryGetPackageJson(options); - if (!packageJson) return false; - // A Vite project is a frontend build, not a server-side Node app. - // This config sits before `javascript_web` in the detection loop and - // matches on package.json alone, so without this exclusion it claims - // every Vite app and integrates posthog-node into a browser project. - if (hasDeclaredDependency('vite', packageJson)) return false; - if ( - VITE_CONFIG_FILES.some((file) => - fs.existsSync(path.join(options.installDir, file)), - ) - ) { - return false; - } - return true; + return !!packageJson; }, }, diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 251b166cf..a3d114444 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -91,11 +91,14 @@ export enum Integration { android = 'android', rails = 'rails', - // Language fallbacks + // Language fallbacks. Within these, javascript_web (lockfile + a frontend + // signal like index.html or a bundler) must come before javascriptNode + // (any package.json at all) — node is the last resort of the whole + // detection, or it claims every frameworkless frontend project. python = 'python', ruby = 'ruby', - javascriptNode = 'javascript_node', javascript_web = 'javascript_web', + javascriptNode = 'javascript_node', } export interface Args { diff --git a/src/lib/detection/__tests__/framework.test.ts b/src/lib/detection/__tests__/framework.test.ts index eab4f5dcd..142b11bf7 100644 --- a/src/lib/detection/__tests__/framework.test.ts +++ b/src/lib/detection/__tests__/framework.test.ts @@ -1,9 +1,8 @@ import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; -import { DETECTION_ORDER, detectFramework } from '@lib/detection/framework'; +import { detectFramework } from '@lib/detection/framework'; import { Integration } from '@lib/constants'; -import { JAVASCRIPT_NODE_AGENT_CONFIG } from '../../../frameworks/javascript-node/javascript-node-wizard-agent'; import { ANDROID_AGENT_CONFIG } from '../../../frameworks/android/android-wizard-agent'; /** A throwaway project dir seeded with the given files. */ @@ -30,49 +29,32 @@ afterAll(() => { } }); -const FALLBACKS = [ - Integration.python, - Integration.ruby, - Integration.javascript_web, - Integration.javascriptNode, -]; - -describe('DETECTION_ORDER (fallback phase invariants)', () => { - test('covers every Integration exactly once', () => { - expect([...DETECTION_ORDER].sort()).toEqual( - Object.values(Integration).sort(), - ); - expect(new Set(DETECTION_ORDER).size).toBe(DETECTION_ORDER.length); - }); +describe('Integration enum order (drives first-match detection)', () => { + const order = Object.values(Integration); + const fallbacks = [ + Integration.python, + Integration.ruby, + Integration.javascript_web, + Integration.javascriptNode, + ]; test('every language fallback comes after every framework', () => { // Broad fallback predicates (any package.json, any .py file) would - // shadow specific frameworks if tried earlier. - const firstFallback = Math.min( - ...FALLBACKS.map((f) => DETECTION_ORDER.indexOf(f)), - ); + // shadow specific frameworks if declared earlier. + const firstFallback = Math.min(...fallbacks.map((f) => order.indexOf(f))); const lastFramework = Math.max( - ...DETECTION_ORDER.filter((i) => !FALLBACKS.includes(i)).map((i) => - DETECTION_ORDER.indexOf(i), - ), + ...order + .filter((i) => !fallbacks.includes(i)) + .map((i) => order.indexOf(i)), ); expect(firstFallback).toBeGreaterThan(lastFramework); }); test('generic Node is the last resort of the entire detection', () => { // javascriptNode matches on package.json alone — anything after it would - // be unreachable for any JS project. - expect(DETECTION_ORDER[DETECTION_ORDER.length - 1]).toBe( - Integration.javascriptNode, - ); - }); - - test('javascript_web is tried before javascriptNode', () => { - // web requires a lockfile + a frontend signal; node requires only - // package.json. Specific before broad, or web is unreachable. - expect(DETECTION_ORDER.indexOf(Integration.javascript_web)).toBeLessThan( - DETECTION_ORDER.indexOf(Integration.javascriptNode), - ); + // be unreachable for any JS project. In particular javascript_web + // (lockfile + frontend signal) must be tried first. + expect(order[order.length - 1]).toBe(Integration.javascriptNode); }); }); @@ -105,9 +87,9 @@ describe('detectFramework (end-to-end over real project dirs)', () => { }); test('a plain browser app (lockfile + index.html, no bundler) resolves to javascript_web', async () => { - // The ordering fix beyond Vite: before the explicit fallback phase, - // javascriptNode sat ahead of javascript_web and claimed every frontend - // project that matched no specific framework. + // The bug beyond Vite: with javascriptNode declared before + // javascript_web, node claimed every frontend project that matched no + // specific framework. const opts = project({ 'package.json': JSON.stringify({ dependencies: {} }), 'package-lock.json': '{}', @@ -139,37 +121,6 @@ describe('detectFramework (end-to-end over real project dirs)', () => { }); }); -describe('javascript-node detect (generic Node fallback)', () => { - const detect = JAVASCRIPT_NODE_AGENT_CONFIG.detection.detect; - - test('claims a plain Node project', async () => { - const opts = project({ - 'package.json': JSON.stringify({ dependencies: { express: '^4' } }), - }); - await expect(detect(opts)).resolves.toBe(true); - }); - - test('does not claim a project with vite in devDependencies', async () => { - const opts = project({ - 'package.json': JSON.stringify({ - dependencies: { react: '^19' }, - devDependencies: { vite: '^6' }, - }), - }); - await expect(detect(opts)).resolves.toBe(false); - }); - - test('does not claim a project with a vite config file', async () => { - for (const config of ['vite.config.ts', 'vite.config.mjs']) { - const opts = project({ - 'package.json': JSON.stringify({ dependencies: {} }), - [config]: 'export default {}', - }); - await expect(detect(opts)).resolves.toBe(false); - } - }); -}); - describe('android detect', () => { const detect = ANDROID_AGENT_CONFIG.detection.detect; diff --git a/src/lib/detection/framework.ts b/src/lib/detection/framework.ts index 737008561..4efd9f6bf 100644 --- a/src/lib/detection/framework.ts +++ b/src/lib/detection/framework.ts @@ -10,43 +10,14 @@ import { Integration, DETECTION_TIMEOUT_MS } from '@lib/constants'; import { FRAMEWORK_REGISTRY } from '@lib/registry'; /** - * Language fallbacks, tried ONLY after every real framework has failed to - * match — their predicates are broad (any package.json, any .py project), so - * running them earlier would shadow specific frameworks. Order within the - * phase matters too: `javascript_web` (lockfile + a frontend signal) is more - * specific than `javascriptNode` (any package.json at all), so web is tried - * first and generic Node is the last resort of the entire detection. - */ -const LANGUAGE_FALLBACKS: readonly Integration[] = [ - Integration.python, - Integration.ruby, - Integration.javascript_web, - Integration.javascriptNode, -]; - -/** - * The full detection order: every framework (enum order), then the language - * fallbacks. Built explicitly rather than trusting enum declaration order, - * so adding an Integration in the wrong place can't silently put a broad - * fallback ahead of a specific framework. Exported for the ordering tests. - */ -export const DETECTION_ORDER: readonly Integration[] = [ - ...Object.values(Integration).filter( - (integration) => !LANGUAGE_FALLBACKS.includes(integration), - ), - ...LANGUAGE_FALLBACKS, -]; - -/** - * Return the first integration in DETECTION_ORDER whose `detect()` predicate - * matches the given directory: real frameworks first, language fallbacks - * after, generic Node last of all. Returns undefined if nothing matches or - * detection times out. + * Loop through all registered frameworks and return the first one + * whose `detect()` predicate matches the given directory. + * Returns undefined if no framework is detected or detection times out. */ export async function detectFramework( installDir: string, ): Promise { - for (const integration of DETECTION_ORDER) { + for (const integration of Object.values(Integration)) { const config = FRAMEWORK_REGISTRY[integration]; try { const detected = await Promise.race([ From b6baa0f46b0c48c31ab02acf804ba73c477d218b Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" <29069505+gewenyu99@users.noreply.github.com> Date: Tue, 7 Jul 2026 12:13:15 -0400 Subject: [PATCH 4/4] chore(detection): shorten code comments Cut the multi-line ordering explanations down to one line each. The enum docblock and the order tests already carry the invariant. Generated-By: PostHog Code Task-Id: bcb96bcb-e37f-421d-841d-738647c73b8e --- src/frameworks/android/android-wizard-agent.ts | 6 +----- src/lib/constants.ts | 5 +---- src/lib/detection/__tests__/framework.test.ts | 9 +-------- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/frameworks/android/android-wizard-agent.ts b/src/frameworks/android/android-wizard-agent.ts index 24026bee7..5b60a4a76 100644 --- a/src/frameworks/android/android-wizard-agent.ts +++ b/src/frameworks/android/android-wizard-agent.ts @@ -41,11 +41,7 @@ export const ANDROID_AGENT_CONFIG: FrameworkConfig = { detect: async (options) => { const { installDir } = options; - // A Flutter project ships a full android/ subtree (manifests, gradle - // wiring, a MainActivity.kt), so both strategies below would claim it - // and integrate posthog-android into the native shell instead of the - // Flutter app. pubspec.yaml at the root is the Flutter/Dart marker — - // bail out so this config never claims a Flutter project. + // pubspec.yaml means Flutter — its android/ subtree would match below. if (fs.existsSync(path.join(installDir, 'pubspec.yaml'))) { return false; } diff --git a/src/lib/constants.ts b/src/lib/constants.ts index a3d114444..c26c4fbb6 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -91,10 +91,7 @@ export enum Integration { android = 'android', rails = 'rails', - // Language fallbacks. Within these, javascript_web (lockfile + a frontend - // signal like index.html or a bundler) must come before javascriptNode - // (any package.json at all) — node is the last resort of the whole - // detection, or it claims every frameworkless frontend project. + // Language fallbacks. Keep javascriptNode last: it matches any package.json. python = 'python', ruby = 'ruby', javascript_web = 'javascript_web', diff --git a/src/lib/detection/__tests__/framework.test.ts b/src/lib/detection/__tests__/framework.test.ts index 142b11bf7..154613a35 100644 --- a/src/lib/detection/__tests__/framework.test.ts +++ b/src/lib/detection/__tests__/framework.test.ts @@ -39,8 +39,6 @@ describe('Integration enum order (drives first-match detection)', () => { ]; test('every language fallback comes after every framework', () => { - // Broad fallback predicates (any package.json, any .py file) would - // shadow specific frameworks if declared earlier. const firstFallback = Math.min(...fallbacks.map((f) => order.indexOf(f))); const lastFramework = Math.max( ...order @@ -51,9 +49,7 @@ describe('Integration enum order (drives first-match detection)', () => { }); test('generic Node is the last resort of the entire detection', () => { - // javascriptNode matches on package.json alone — anything after it would - // be unreachable for any JS project. In particular javascript_web - // (lockfile + frontend signal) must be tried first. + // javascriptNode matches any package.json; anything after it is unreachable. expect(order[order.length - 1]).toBe(Integration.javascriptNode); }); }); @@ -87,9 +83,6 @@ describe('detectFramework (end-to-end over real project dirs)', () => { }); test('a plain browser app (lockfile + index.html, no bundler) resolves to javascript_web', async () => { - // The bug beyond Vite: with javascriptNode declared before - // javascript_web, node claimed every frontend project that matched no - // specific framework. const opts = project({ 'package.json': JSON.stringify({ dependencies: {} }), 'package-lock.json': '{}',