mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The rule table mapped HOG_ -> FN_ ahead of any POSTHOG rule, so POSTHOG_FOO split into POST + HOG_ and came out POSTFN_FOO. Nothing sets that. The live pods set INSIGHTS_SKIP_MIGRATION_CHECKS, which bin/migrate-check reads on the container entrypoint, so the misspelling does not degrade a feature -- it runs migrate --check against a database whose history this tree does not share and the container never starts. Both replacements are measured against the shipped tree rather than chosen: every HOG_ name with a counterpart there is spelled INSIGHTS_ (HOG_QL -> INSIGHTS_QL, HOG_FUNCTION_TEMPLATES -> INSIGHTS_FUNCTION_TEMPLATES, 12 of 12 determinate cases), and FN_ appears in it nowhere. Upstream carries 95 HOG_ tokens and zero FN_ ones, so repairing FN_ back out is exactly the inverse of the bad rule and cannot collide with an upstream name. The rule is fixed for the next convergence, and the tree it already produced is repaired in the same commit -- the codemod is idempotent against its own output, so it cannot undo this itself.
68 lines
2.9 KiB
TypeScript
68 lines
2.9 KiB
TypeScript
import { createServer } from 'node:net'
|
|
import { unstable_dev, type Unstable_DevWorker } from 'wrangler'
|
|
|
|
import type { IntegrationEnv, IntegrationHarness } from './types'
|
|
|
|
// `vars` are baked into workerd at boot, so we need MCP_APPS_BASE_URL to know
|
|
// the listener's port up-front. Bind a Node TCP server to port 0, read the
|
|
// kernel-assigned port, then close — workerd grabs that port a moment later.
|
|
// Tiny race window vs hardcoding a fixed port; tests stay parallel-safe.
|
|
async function reserveFreePort(): Promise<number> {
|
|
return new Promise((resolve, reject) => {
|
|
const server = createServer()
|
|
server.unref()
|
|
server.once('error', reject)
|
|
server.listen(0, '127.0.0.1', () => {
|
|
const address = server.address()
|
|
if (typeof address === 'object' && address && 'port' in address) {
|
|
const port = address.port
|
|
server.close(() => resolve(port))
|
|
} else {
|
|
server.close(() => reject(new Error('Failed to read assigned port')))
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// Boots a real workerd via `wrangler unstable_dev` so the worker entry point
|
|
// (auth gate, routing, and `/mcp` proxy to the Hono runtime) runs end-to-end the
|
|
// same way it does in production. `local: true` keeps everything on the developer
|
|
// machine — no Cloudflare account / network required.
|
|
export async function startCfHarness(env: IntegrationEnv): Promise<IntegrationHarness> {
|
|
const port = await reserveFreePort()
|
|
const baseUrl = new URL(`http://127.0.0.1:${port}`)
|
|
|
|
const worker: Unstable_DevWorker = await unstable_dev('src/index.ts', {
|
|
config: 'wrangler.jsonc',
|
|
local: true,
|
|
ip: '127.0.0.1',
|
|
port,
|
|
// The CF Worker reads `INSIGHTS_API_BASE_URL` via the `cloudflare:workers`
|
|
// env binding, not `process.env`. Pass it through `vars` so the binding
|
|
// resolves to the local stack. `MCP_APPS_BASE_URL` overrides the value
|
|
// hard-coded in `wrangler.jsonc` (which points at the production worker).
|
|
vars: {
|
|
INSIGHTS_API_BASE_URL: env.apiBaseUrl,
|
|
MCP_APPS_BASE_URL: baseUrl.toString().replace(/\/$/, ''),
|
|
// Match the workers vitest config — empty values keep observability
|
|
// paths short-circuited and silence the analytics no-network warning.
|
|
INSIGHTS_ANALYTICS_API_KEY: '',
|
|
INSIGHTS_ANALYTICS_HOST: '',
|
|
INSIGHTS_MCP_APPS_ANALYTICS_BASE_URL: '',
|
|
INSIGHTS_UI_APPS_TOKEN: '',
|
|
INKEEP_API_KEY: '',
|
|
},
|
|
experimental: {
|
|
disableExperimentalWarning: true,
|
|
disableDevRegistry: true,
|
|
},
|
|
// Quiet the dev-server console output so test runs aren't drowned out.
|
|
logLevel: 'warn',
|
|
})
|
|
|
|
return {
|
|
baseUrl: new URL(`http://${worker.address}:${worker.port}`),
|
|
stop: () => worker.stop(),
|
|
}
|
|
}
|