:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): key the agent build cache by platform and fix x64 e2e (#112370)
Alessandro Pogliaghibc08a2f5f20a
diff --git a/.github/workflows/desktop-release.yml b/.github/workflows/desktop-release.ymlindex 17417a3766ab..0ec58809ca12 100644--- a/.github/workflows/desktop-release.yml+++ b/.github/workflows/desktop-release.yml@@ -324,7 +324,9 @@ jobs:- name: Checkoutuses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2with:- sparse-checkout: products/desktop+ sparse-checkout: |+ products/desktop+ packages/agentsparse-checkout-cone-mode: falsepersist-credentials: false@@ -348,10 +350,10 @@ jobs:package-manager-cache: false- name: Install e2e dependencies- run: pnpm install --frozen-lockfile --ignore-scripts --filter code --filter "@posthog/agent..."+ run: pnpm install --frozen-lockfile --ignore-scripts --filter code- - name: Build agent package- run: pnpm --filter "@posthog/agent..." run build+ - name: Build agent workspace+ run: node scripts/build-agent-workspace.mjs- name: Download the x64 appuses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0diff --git a/.github/workflows/desktop-test.yml b/.github/workflows/desktop-test.ymlindex 1874d1dd8960..b4f6d021ba80 100644--- a/.github/workflows/desktop-test.yml+++ b/.github/workflows/desktop-test.yml@@ -338,7 +338,7 @@ jobs:- name: Build agent dependenciesworking-directory: packages/agent- run: pnpm install --frozen-lockfile && pnpm exec turbo build --filter=@posthog/agent^...+ run: pnpm install --frozen-lockfile && node scripts/turbo.mjs build --filter=@posthog/agent^...- name: Download native codex binary# Non-fatal at the STEP so a failure surfaces as the fail-loud binary guarddiff --git a/packages/agent/.gitignore b/packages/agent/.gitignoreindex db66596ccaf3..83b026a3dfc1 100644--- a/packages/agent/.gitignore+++ b/packages/agent/.gitignore@@ -2,6 +2,7 @@ node_modules/.pnpm-store/dist/.turbo/+.platform-stampjunit.xml# tsup bundled config artifacts (temporary files left behind when bundling TS configs)diff --git a/packages/agent/package.json b/packages/agent/package.jsonindex 4799c67f026b..f503fa7155e6 100644--- a/packages/agent/package.json+++ b/packages/agent/package.json@@ -8,10 +8,10 @@},"packageManager": "[email protected]+sha512.21c4e5698002ade97e4efe8b8b4a89a8de3c85a37919f957e7a0f30f38fbc5bbdd05980ffe29179b2fb6e6e691242e098d945d1601772cad0fef5fb6411e2a4b","scripts": {- "build": "turbo build",- "dev": "turbo dev",- "typecheck": "turbo typecheck",- "test": "turbo test --concurrency=2",+ "build": "node scripts/turbo.mjs build",+ "dev": "node scripts/turbo.mjs dev",+ "typecheck": "node scripts/turbo.mjs typecheck",+ "test": "node scripts/turbo.mjs test --concurrency=2","lint": "biome check --write --unsafe","format": "biome format --write","clean": "pnpm -r clean",diff --git a/packages/agent/scripts/turbo.mjs b/packages/agent/scripts/turbo.mjsnew file mode 100644index 000000000000..019ba61f4096--- /dev/null+++ b/packages/agent/scripts/turbo.mjs@@ -0,0 +1,18 @@+import { spawnSync } from "node:child_process";+import { writeFileSync } from "node:fs";+import { createRequire } from "node:module";+import {+ targetArch,+ targetPlatform,+} from "../packages/agent/build/native-binary.mjs";++writeFileSync(+ new URL("../.platform-stamp", import.meta.url),+ `${targetPlatform()}-${targetArch()}\n`,+);++const turbo = createRequire(import.meta.url).resolve("turbo/bin/turbo");+const result = spawnSync(process.execPath, [turbo, ...process.argv.slice(2)], {+ stdio: "inherit",+});+process.exit(result.status ?? 1);diff --git a/packages/agent/turbo.json b/packages/agent/turbo.jsonindex 3a9e48325221..4f368d458914 100644--- a/packages/agent/turbo.json+++ b/packages/agent/turbo.json@@ -4,6 +4,7 @@"build": {"dependsOn": ["^build"],"env": ["SKIP_PRODUCT_ENGINEER_SKILLS_DOWNLOAD"],+ "inputs": ["$TURBO_DEFAULT$", "$TURBO_ROOT$/.platform-stamp"],"outputs": ["dist/**"]},"typecheck": {diff --git a/products/desktop/apps/code/vite-main-plugins.mts b/products/desktop/apps/code/vite-main-plugins.mtsindex 53dd6694f9c7..9be2261a01af 100644--- a/products/desktop/apps/code/vite-main-plugins.mts+++ b/products/desktop/apps/code/vite-main-plugins.mts@@ -23,7 +23,8 @@ import {CLAUDE_CLI_SUPPORT_DIRS,CLAUDE_CLI_SUPPORT_FILES,claudeBinName,- claudeExecutableCandidates as sdkClaudeExecutableCandidates,+ legacyCliCandidates,+ nativeBinaryCandidates,targetArch,targetPlatform,} from "../../../../packages/agent/packages/agent/build/native-binary.mjs";@@ -63,16 +64,15 @@ export function fixFilenameCircularRef(): Plugin {let claudeCliCopied = false;-function verifyBinaryArch(destPath: string): void {+function verifyBinaryTarget(destPath: string): void {// Best-effort: parse the binary's magic bytes and confirm the embedded arch// matches what we believe we're packaging for. `file(1)` is more portable- // but adds a subprocess; reading 20 bytes is enough for Mach-O / ELF / PE.- let header: Buffer;+ // but adds a subprocess; reading the headers is enough for Mach-O / ELF / PE.+ let actual: ReturnType<typeof detectBinaryFormat>;try {const fd = openSync(destPath, "r");try {- header = Buffer.alloc(20);- readSync(fd, header, 0, 20, 0);+ actual = detectBinaryFormat(fd);} finally {closeSync(fd);}@@ -83,39 +83,62 @@ function verifyBinaryArch(destPath: string): void {const arch = targetArch();const platform = targetPlatform();- const actual = detectBinaryArch(header, platform);- if (actual && actual !== arch) {+ if (!actual) {throw new Error(- `[copy-claude-executable] Architecture mismatch: copied binary is ${actual} but target is ${arch} (platform=${platform}). ` +- `Reinstall @anthropic-ai/claude-agent-sdk optional deps for the target arch, or set npm_config_arch=${arch} before building.`,+ `[copy-claude-executable] Unrecognized binary format: copied binary is not Mach-O, ELF or PE but target is ${platform}-${arch}.`,+ );+ }+ if (actual.platform !== platform) {+ throw new Error(+ `[copy-claude-executable] Platform mismatch: copied binary is ${actual.platform} but target is ${platform}.`,+ );+ }+ if (actual.arch !== arch) {+ throw new Error(+ `[copy-claude-executable] Architecture mismatch: copied binary is ${actual.arch} but target is ${arch}.`,);}}-function detectBinaryArch(- header: Buffer,- platform: string,-): "arm64" | "x64" | "ia32" | null {+function detectBinaryFormat(fd: number): {+ platform: "darwin" | "linux" | "win32";+ arch: "arm64" | "x64" | "ia32";+} | null {+ const header = Buffer.alloc(64);+ if (readSync(fd, header, 0, 64, 0) < 20) return null;// Mach-O 64-bit LE: magic 0xFEEDFACF, then cputype at offset 4 (LE).- if (platform === "darwin" && header.readUInt32LE(0) === 0xfeedfacf) {+ if (header.readUInt32LE(0) === 0xfeedfacf) {const cpuType = header.readUInt32LE(4);- if (cpuType === 0x0100000c) return "arm64";- if (cpuType === 0x01000007) return "x64";+ if (cpuType === 0x0100000c) return { platform: "darwin", arch: "arm64" };+ if (cpuType === 0x01000007) return { platform: "darwin", arch: "x64" };+ return null;}// ELF: \x7FELF, then e_machine at offset 18 (LE).if (- platform === "linux" &&header[0] === 0x7f &&header[1] === 0x45 &&header[2] === 0x4c &&header[3] === 0x46) {const eMachine = header.readUInt16LE(18);- if (eMachine === 0x3e) return "x64";- if (eMachine === 0xb7) return "arm64";- if (eMachine === 0x03) return "ia32";+ if (eMachine === 0x3e) return { platform: "linux", arch: "x64" };+ if (eMachine === 0xb7) return { platform: "linux", arch: "arm64" };+ if (eMachine === 0x03) return { platform: "linux", arch: "ia32" };+ return null;+ }+ // PE: MZ at 0, e_lfanew at 0x3C points to PE\0\0, then COFF Machine (LE).+ if (header[0] === 0x4d && header[1] === 0x5a) {+ const peHeader = Buffer.alloc(6);+ if (readSync(fd, peHeader, 0, 6, header.readUInt32LE(0x3c)) < 6) {+ return null;+ }+ if (peHeader.readUInt32LE(0) !== 0x00004550) return null;+ const machine = peHeader.readUInt16LE(4);+ if (machine === 0x8664) return { platform: "win32", arch: "x64" };+ if (machine === 0xaa64) return { platform: "win32", arch: "arm64" };+ if (machine === 0x14c) return { platform: "win32", arch: "ia32" };+ return null;}- // PE: MZ at 0, PE header offset at 0x3C — too long to inline; skip.return null;}@@ -259,6 +282,8 @@ export function copyClaudeExecutable(): Plugin {}const packageCandidates = [+ ...nativeBinaryCandidates(join(__dirname, "node_modules")),+ ...nativeBinaryCandidates(join(__dirname, "../../node_modules")),join(__dirname, "node_modules/@posthog/agent/dist/claude-cli", binName),join(__dirname,@@ -270,8 +295,8 @@ export function copyClaudeExecutable(): Plugin {"../../../../packages/agent/packages/agent/dist/claude-cli",binName,),- ...sdkClaudeExecutableCandidates(join(__dirname, "node_modules")),- ...sdkClaudeExecutableCandidates(join(__dirname, "../../node_modules")),+ ...legacyCliCandidates(join(__dirname, "node_modules")),+ ...legacyCliCandidates(join(__dirname, "../../node_modules")),];const source = packageCandidates.find((p: string) => existsSync(p));@@ -293,7 +318,7 @@ export function copyClaudeExecutable(): Plugin {execSync(`chmod +x "${destBinary}"`);}copyClaudeSupportAssets(source, destDir);- verifyBinaryArch(destBinary);+ verifyBinaryTarget(destBinary);signClaudeBinary(destBinary);claudeCliCopied = true;},