From ff7610dce0f4196d81629244295b7ec3c70415b2 Mon Sep 17 00:00:00 2001 From: Sarah Sanders <88458517+sarahxsanders@users.noreply.github.com> Date: Wed, 22 Jul 2026 16:43:24 -0400 Subject: [PATCH 1/4] feat: add Rust framework support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds Rust as a supported framework, stacked on the Elixir framework PR: - Integration.rust enum entry in the frameworks block, before the language fallbacks - RUST_AGENT_CONFIG in src/frameworks/rust/ — claims a Cargo.toml with a [package] section (workspace-root-only manifests fall through; the agentic monorepo scan finds nested crates), gathers async-runtime presence (tokio/async-std/smol) to steer the agent between the default async client and the blocking one, installs via cargo add posthog-rs - cargoPackageManager helper + FRAMEWORK_REGISTRY entry - bash-fence allowlist for cargo: add/remove/build/check/fmt/clippy/metadata/tree/fetch; run/test stay denied (arbitrary code execution) and install/publish stay denied (outward-facing); the pinned cargo run assertion's reason changes from "no rust framework" to "arbitrary code execution" - Pins framework: rust on a new integration-rust entry in the variant-resolution contract test, matching context-mill PR #269 which must release before this merges - Marks Cargo.toml as a real framework target in the agentic manifest comment Generated-By: PostHog Code Task-Id: 9c949855-7611-48f2-b8b5-7aa4112543e5 --- src/frameworks/rust/rust-wizard-agent.ts | 106 ++++++++++++++++++ src/lib/__tests__/wizard-can-use-tool.test.ts | 17 ++- src/lib/agent/bash-fence.ts | 15 ++- .../__tests__/variant-resolution.test.ts | 1 + src/lib/constants.ts | 1 + src/lib/detection/__tests__/framework.test.ts | 34 ++++++ .../__tests__/package-manager.test.ts | 2 + src/lib/detection/agentic.ts | 5 +- src/lib/detection/package-manager.ts | 18 +++ src/lib/registry.ts | 2 + 10 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 src/frameworks/rust/rust-wizard-agent.ts diff --git a/src/frameworks/rust/rust-wizard-agent.ts b/src/frameworks/rust/rust-wizard-agent.ts new file mode 100644 index 000000000..6c0aef1e2 --- /dev/null +++ b/src/frameworks/rust/rust-wizard-agent.ts @@ -0,0 +1,106 @@ +/* Rust wizard using posthog-agent with PostHog MCP */ +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import type { WizardRunOptions } from '@utils/types'; +import type { FrameworkConfig } from '@lib/framework-config'; +import { cargoPackageManager } from '@lib/detection/package-manager'; +import { Integration } from '@lib/constants'; + +type RustContext = { + asyncRuntime?: boolean; +}; + +function readCargoToml(installDir: string): string | undefined { + const cargoTomlPath = path.join(installDir, 'Cargo.toml'); + if (!fs.existsSync(cargoTomlPath)) { + return undefined; + } + return fs.readFileSync(cargoTomlPath, 'utf-8'); +} + +/** posthog-rs defaults to its async client; projects without an async runtime need the blocking one. */ +function hasAsyncRuntime(installDir: string): boolean { + const cargoToml = readCargoToml(installDir); + return !!cargoToml && /^\s*(tokio|async-std|smol)\s*[=.]/m.test(cargoToml); +} + +export const RUST_AGENT_CONFIG: FrameworkConfig = { + metadata: { + name: 'Rust', + integration: Integration.rust, + docsUrl: 'https://posthog.com/docs/libraries/rust', + gatherContext: (options: WizardRunOptions) => { + const asyncRuntime = hasAsyncRuntime(options.installDir); + return Promise.resolve({ asyncRuntime }); + }, + }, + + detection: { + packageName: 'posthog-rs', + packageDisplayName: 'Rust', + usesPackageJson: false, + getVersion: () => undefined, + // A Cargo.toml with a [package] section marks a crate root. A + // workspace-root-only manifest ([workspace], no [package]) falls + // through — the agentic monorepo scan finds the nested crates. + detect: (options) => { + const cargoToml = readCargoToml(options.installDir); + return Promise.resolve(!!cargoToml && /^\s*\[package\]/m.test(cargoToml)); + }, + detectPackageManager: cargoPackageManager, + }, + + environment: { + uploadToHosting: false, + getEnvVars: (apiKey: string, host: string) => ({ + POSTHOG_API_KEY: apiKey, + POSTHOG_HOST: host, + }), + }, + + analytics: { + getTags: (context) => ({ + asyncRuntime: context.asyncRuntime ? 'true' : 'false', + }), + }, + + prompts: { + projectTypeDetection: + 'This is a Rust project. Look for Cargo.toml with a [package] section, src/main.rs or src/lib.rs, and Cargo.lock to confirm.', + packageInstallation: + 'Install the PostHog Rust SDK with `cargo add posthog-rs`. Do not manually edit Cargo.toml; cargo updates it automatically.', + getAdditionalContextLines: (context) => { + const lines = [ + `Framework docs ID: rust (use posthog://docs/frameworks/rust for documentation)`, + 'capture() hands events to a background worker — the client must flush() or shutdown() before the process exits or buffered events are lost.', + ]; + if (context.asyncRuntime) { + lines.push( + 'An async runtime is present — use the default async posthog-rs client.', + ); + } else { + lines.push( + 'No async runtime detected — install with `cargo add posthog-rs --no-default-features` and use the blocking client.', + ); + } + return lines; + }, + }, + + ui: { + successMessage: 'PostHog integration complete', + estimatedDurationMinutes: 5, + getOutroChanges: () => [ + 'Analyzed your Rust project structure', + 'Installed the posthog-rs crate via cargo add', + 'Initialized a shared PostHog client configured from environment variables', + 'Instrumented meaningful events with client.capture', + ], + getOutroNextSteps: () => [ + 'Run your Rust application and trigger the instrumented code paths', + 'Visit your PostHog dashboard to see incoming events', + 'Use Event::new(event_name, distinct_id) and client.capture to track custom events', + 'Keep flush()/shutdown() in your exit path so queued events are sent', + ], + }, +}; diff --git a/src/lib/__tests__/wizard-can-use-tool.test.ts b/src/lib/__tests__/wizard-can-use-tool.test.ts index 67afbeccc..bd5497351 100644 --- a/src/lib/__tests__/wizard-can-use-tool.test.ts +++ b/src/lib/__tests__/wizard-can-use-tool.test.ts @@ -139,6 +139,21 @@ describe('bash fence — allows real toolchain commands (from skills + field log expect(allow('carthage bootstrap')).toBe('allow'); }); + it('rust ecosystem', () => { + expect(allow('cargo add posthog-rs')).toBe('allow'); + expect(allow('cargo add posthog-rs --no-default-features')).toBe('allow'); + expect(allow('cargo build')).toBe('allow'); + expect(allow('cargo check --all-targets')).toBe('allow'); + expect(allow('cargo fmt')).toBe('allow'); + expect(allow('cargo clippy')).toBe('allow'); + expect(allow('cargo metadata --format-version 1')).toBe('allow'); + // run/test execute project code; install/publish are outward-facing. + expect(allow('cargo run')).toBe('deny'); + expect(allow('cargo test')).toBe('deny'); + expect(allow('cargo install evil-tool')).toBe('deny'); + expect(allow('cargo publish')).toBe('deny'); + }); + it('android/jvm ecosystem', () => { expect(allow('./gradlew assembleDebug')).toBe('allow'); expect(allow('./gradlew :app:assembleDebug')).toBe('allow'); @@ -193,7 +208,7 @@ describe('bash fence — attack corpus (one test per bypass vector)', () => { expect(allow('xcodebuild test-without-building')).toBe('deny'); expect(allow('bundle exec rspec')).toBe('deny'); expect(allow('composer run-script evil')).toBe('deny'); - expect(allow('cargo run')).toBe('deny'); // no rust framework -> whole binary denied + expect(allow('cargo run')).toBe('deny'); // arbitrary code execution expect(allow('go get github.com/x/y')).toBe('deny'); // no go framework }); diff --git a/src/lib/agent/bash-fence.ts b/src/lib/agent/bash-fence.ts index 9fe251eea..252c8e9b3 100644 --- a/src/lib/agent/bash-fence.ts +++ b/src/lib/agent/bash-fence.ts @@ -84,6 +84,18 @@ const SIMPLE_MANAGERS: Record = { swift: ['package', 'build'], pod: ['install', 'update', 'search'], carthage: ['bootstrap', 'update'], + // cargo run/test execute project code; install/publish are outward-facing. + cargo: [ + 'add', + 'remove', + 'build', + 'check', + 'fmt', + 'clippy', + 'metadata', + 'tree', + 'fetch', + ], }; // Gradle tasks are verb-anchored camelCase: assembleDebug yes, publishToMavenCentral no. @@ -102,7 +114,8 @@ const ALLOWED_TOOLS_SUMMARY = 'composer (install|require|update|remove|show), bundle (install|add|remove|update|show|exec ), ' + 'gem (install|uninstall|list|search), swift (package|build), pod (install|update|search), carthage (bootstrap|update), ' + 'xcodebuild (build/clean/archive actions), gradle/gradlew (build|clean|dependencies|assemble*/compile*/bundle*/lint* tasks), ' + - 'mvn (install|compile|package|verify|dependency:tree).'; + 'mvn (install|compile|package|verify|dependency:tree), ' + + 'cargo (add|remove|build|check|fmt|clippy|metadata|tree|fetch).'; function deny(analyticsReason: string, message: string): BashFenceDecision { return { allowed: false, message, analyticsReason }; diff --git a/src/lib/agent/runner/sequence/orchestrator/__tests__/variant-resolution.test.ts b/src/lib/agent/runner/sequence/orchestrator/__tests__/variant-resolution.test.ts index f878a468a..4e2513699 100644 --- a/src/lib/agent/runner/sequence/orchestrator/__tests__/variant-resolution.test.ts +++ b/src/lib/agent/runner/sequence/orchestrator/__tests__/variant-resolution.test.ts @@ -50,6 +50,7 @@ const INTEGRATION_ENTRIES = [ { id: 'integration-ruby', framework: 'ruby' }, { id: 'integration-elixir' }, { id: 'integration-go' }, + { id: 'integration-rust', framework: 'rust' }, { id: 'integration-swift', framework: 'swift' }, { id: 'integration-kmp', framework: 'kmp' }, { id: 'integration-flutter' }, diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 78353cb61..18850ae4f 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -100,6 +100,7 @@ export enum Integration { swift = 'swift', android = 'android', rails = 'rails', + rust = 'rust', // Language fallbacks. Keep javascriptNode last: it matches any package.json. python = 'python', diff --git a/src/lib/detection/__tests__/framework.test.ts b/src/lib/detection/__tests__/framework.test.ts index 075c46a29..aaae42036 100644 --- a/src/lib/detection/__tests__/framework.test.ts +++ b/src/lib/detection/__tests__/framework.test.ts @@ -5,6 +5,7 @@ import { detectFramework } from '@lib/detection/framework'; import { Integration } from '@lib/constants'; import { ANDROID_AGENT_CONFIG } from '../../../frameworks/android/android-wizard-agent'; import { KMP_AGENT_CONFIG } from '../../../frameworks/kmp/kmp-wizard-agent'; +import { RUST_AGENT_CONFIG } from '../../../frameworks/rust/rust-wizard-agent'; /** A throwaway project dir seeded with the given files. */ function makeProject(files: Record): string { @@ -137,6 +138,16 @@ describe('detectFramework (end-to-end over real project dirs)', () => { ); }); + test('a Rust crate resolves to rust', async () => { + const opts = project({ + 'Cargo.toml': '[package]\nname = "my-app"\nedition = "2021"\n', + 'src/main.rs': 'fn main() {}', + }); + await expect(detectFramework(opts.installDir)).resolves.toBe( + Integration.rust, + ); + }); + 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', @@ -169,6 +180,29 @@ describe('android detect', () => { }); }); +describe('rust detect', () => { + const detect = RUST_AGENT_CONFIG.detection.detect; + + test('claims a crate with a [package] section', async () => { + const opts = project({ + 'Cargo.toml': '[package]\nname = "my-app"\nedition = "2021"\n', + }); + await expect(detect(opts)).resolves.toBe(true); + }); + + test('does not claim a workspace-root-only manifest', async () => { + const opts = project({ + 'Cargo.toml': '[workspace]\nmembers = ["crates/*"]\n', + }); + await expect(detect(opts)).resolves.toBe(false); + }); + + test('does not claim a project without a Cargo.toml', async () => { + const opts = project({ 'src/main.rs': 'fn main() {}' }); + await expect(detect(opts)).resolves.toBe(false); + }); +}); + describe('kmp detect', () => { const detect = KMP_AGENT_CONFIG.detection.detect; diff --git a/src/lib/detection/__tests__/package-manager.test.ts b/src/lib/detection/__tests__/package-manager.test.ts index 27b468ad2..1e8c0aa76 100644 --- a/src/lib/detection/__tests__/package-manager.test.ts +++ b/src/lib/detection/__tests__/package-manager.test.ts @@ -7,6 +7,7 @@ import { composerPackageManager, swiftPackageManager, gradlePackageManager, + cargoPackageManager, } from '@lib/detection/package-manager'; vi.mock('../../../utils/debug'); @@ -188,6 +189,7 @@ describe('static package manager helpers', () => { { fn: composerPackageManager, name: 'composer' }, { fn: swiftPackageManager, name: 'spm' }, { fn: gradlePackageManager, name: 'gradle' }, + { fn: cargoPackageManager, name: 'cargo' }, ])('$name returns valid PackageManagerInfo', async ({ fn }) => { const result = await fn(); expect(result.detected).toHaveLength(1); diff --git a/src/lib/detection/agentic.ts b/src/lib/detection/agentic.ts index c808bf092..1f7096e60 100644 --- a/src/lib/detection/agentic.ts +++ b/src/lib/detection/agentic.ts @@ -74,9 +74,10 @@ export const PROJECT_MANIFESTS: readonly string[] = [ // Ruby / PHP 'Gemfile', 'composer.json', - // Rust / Go / Elixir / JVM / .NET: no framework targets yet, but found so - // an existing PostHog SDK is reported (feeds self-driving's "continue" path). + // Rust 'Cargo.toml', + // Go / Elixir / JVM / .NET: no framework targets yet, but found so + // an existing PostHog SDK is reported (feeds self-driving's "continue" path). 'go.mod', 'mix.exs', 'pom.xml', diff --git a/src/lib/detection/package-manager.ts b/src/lib/detection/package-manager.ts index add3cc49d..6cb413c6a 100644 --- a/src/lib/detection/package-manager.ts +++ b/src/lib/detection/package-manager.ts @@ -215,6 +215,24 @@ export function bundlerPackageManager(): Promise { }); } +// --------------------------------------------------------------------------- +// Rust (Cargo) helper +// --------------------------------------------------------------------------- + +const CARGO: DetectedPackageManager = { + name: 'cargo', + label: 'Cargo', + installCommand: 'cargo add', +}; + +export function cargoPackageManager(): Promise { + return Promise.resolve({ + detected: [CARGO], + primary: CARGO, + recommendation: 'Use Cargo (cargo add).', + }); +} + // --------------------------------------------------------------------------- // Android (Gradle) helper // --------------------------------------------------------------------------- diff --git a/src/lib/registry.ts b/src/lib/registry.ts index 2dfba061c..0305cf1d7 100644 --- a/src/lib/registry.ts +++ b/src/lib/registry.ts @@ -18,6 +18,7 @@ import { SWIFT_AGENT_CONFIG } from '@frameworks/swift/swift-wizard-agent'; import { KMP_AGENT_CONFIG } from '@frameworks/kmp/kmp-wizard-agent'; import { ANDROID_AGENT_CONFIG } from '@frameworks/android/android-wizard-agent'; import { RAILS_AGENT_CONFIG } from '@frameworks/rails/rails-wizard-agent'; +import { RUST_AGENT_CONFIG } from '@frameworks/rust/rust-wizard-agent'; import { PYTHON_AGENT_CONFIG } from '@frameworks/python/python-wizard-agent'; import { RUBY_AGENT_CONFIG } from '@frameworks/ruby/ruby-wizard-agent'; import { JAVASCRIPT_NODE_AGENT_CONFIG } from '@frameworks/javascript-node/javascript-node-wizard-agent'; @@ -42,6 +43,7 @@ export const FRAMEWORK_REGISTRY: Record = { [Integration.swift]: SWIFT_AGENT_CONFIG, [Integration.android]: ANDROID_AGENT_CONFIG, [Integration.rails]: RAILS_AGENT_CONFIG, + [Integration.rust]: RUST_AGENT_CONFIG, [Integration.python]: PYTHON_AGENT_CONFIG, [Integration.ruby]: RUBY_AGENT_CONFIG, [Integration.javascriptNode]: JAVASCRIPT_NODE_AGENT_CONFIG, From 693738139c16eff57da2e7f3128993e646b5bc0e Mon Sep 17 00:00:00 2001 From: sarahxsanders Date: Fri, 24 Jul 2026 09:53:31 -0400 Subject: [PATCH 2/4] docs(bash-fence): correct cargo build/check code-execution comment The comment implied only cargo run/test execute code. build/check also run build.rs and proc-macros at build time; they are permitted under the "builds are equivalent risk" model, not because they are code-free. Clarify. Co-Authored-By: Claude Opus 4.8 --- src/lib/agent/bash-fence.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/agent/bash-fence.ts b/src/lib/agent/bash-fence.ts index 252c8e9b3..bb22b221c 100644 --- a/src/lib/agent/bash-fence.ts +++ b/src/lib/agent/bash-fence.ts @@ -84,7 +84,10 @@ const SIMPLE_MANAGERS: Record = { swift: ['package', 'build'], pod: ['install', 'update', 'search'], carthage: ['bootstrap', 'update'], - // cargo run/test execute project code; install/publish are outward-facing. + // cargo build/check also execute code (build.rs build scripts + proc-macros), + // accepted under the "builds are equivalent risk" model in the file header. + // run/test are denied so the finished binary/tests aren't run; install/publish + // are outward-facing. cargo: [ 'add', 'remove', From 7833e7af7b26004db5803700816e80d17d38bfa0 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" Date: Thu, 20 Aug 2026 17:57:12 -0400 Subject: [PATCH 3/4] fix(e2e): read dependencies from every ecosystem manifest The result JSON parsed package.json and pubspec.yaml only, so Go, Rust, Java, and Elixir runs reported hasPosthogDep false; a *.env file like gotify's also failed the .env prefix check. Co-Authored-By: Claude Opus 5 --- scripts/tui-host.no-jest.ts | 42 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/scripts/tui-host.no-jest.ts b/scripts/tui-host.no-jest.ts index 572d33b36..5e3291b64 100644 --- a/scripts/tui-host.no-jest.ts +++ b/scripts/tui-host.no-jest.ts @@ -253,34 +253,36 @@ async function main() { // env file, and the screens walked. if (process.env.E2E_RESULT_JSON) { const appDir = process.env.APP_DIR!; - let deps: string[] = []; - try { - const pkg = JSON.parse( - fs.readFileSync(`${appDir}/package.json`, 'utf8'), - ); - deps = Object.keys({ ...pkg.dependencies, ...pkg.devDependencies }); - } catch { - /* some frameworks have no package.json */ - } - try { - // Dart/Flutter declares dependencies in pubspec.yaml, so a Flutter run - // reports no dependency at all when only package.json is read. - const pubspec = fs.readFileSync(`${appDir}/pubspec.yaml`, 'utf8'); - for (const line of pubspec.split('\n')) { - const dep = /^\s{2}([A-Za-z_][A-Za-z0-9_]*)\s*:/.exec(line); - if (dep) deps.push(dep[1]); + // One dependency-name pattern per ecosystem manifest. A run only needs + // the names, so a line-level scan beats per-format parsers. + const MANIFESTS: Array<[string, RegExp]> = [ + ['package.json', /"([^"]+)"\s*:\s*"[^"]*"/g], + ['pubspec.yaml', /^ {2}([A-Za-z_][A-Za-z0-9_]*)\s*:/gm], + ['go.mod', /^\s*([\w.\/-]+)\s+v[\w.-]+/gm], + ['Cargo.toml', /^([A-Za-z0-9_-]+)\s*=/gm], + ['pom.xml', /([^<]+)<\/artifactId>/g], + ['build.gradle', /['"]([\w.-]+:[\w.-]+)[:'"]/g], + ['mix.exs', /\{:([a-z_]+)\s*,/g], + ]; + const deps: string[] = []; + for (const [file, pattern] of MANIFESTS) { + try { + const text = fs.readFileSync(`${appDir}/${file}`, 'utf8'); + for (const match of text.matchAll(pattern)) deps.push(match[1]); + } catch { + /* app doesn't use this ecosystem */ } - } catch { - /* not a Dart project */ } - const posthogDeps = deps.filter((d) => d.includes('posthog')); + const posthogDeps = [ + ...new Set(deps.filter((d) => d.toLowerCase().includes('posthog'))), + ]; let envFile: string | null = null; try { const hit = fs .readdirSync(appDir) .find( (f) => - f.startsWith('.env') && + (f.startsWith('.env') || f.endsWith('.env')) && /posthog/i.test(fs.readFileSync(`${appDir}/${f}`, 'utf8')), ); envFile = hit ? `${appDir}/${hit}` : null; From 62e9e2a1ee7b2cc26b37a8b77aaf7e63400cacf1 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" Date: Thu, 20 Aug 2026 18:16:59 -0400 Subject: [PATCH 4/4] fix(e2e): parse package.json properly so script names cannot false-positive Co-Authored-By: Claude Opus 5 --- scripts/tui-host.no-jest.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/tui-host.no-jest.ts b/scripts/tui-host.no-jest.ts index 5e3291b64..de79b105b 100644 --- a/scripts/tui-host.no-jest.ts +++ b/scripts/tui-host.no-jest.ts @@ -256,7 +256,6 @@ async function main() { // One dependency-name pattern per ecosystem manifest. A run only needs // the names, so a line-level scan beats per-format parsers. const MANIFESTS: Array<[string, RegExp]> = [ - ['package.json', /"([^"]+)"\s*:\s*"[^"]*"/g], ['pubspec.yaml', /^ {2}([A-Za-z_][A-Za-z0-9_]*)\s*:/gm], ['go.mod', /^\s*([\w.\/-]+)\s+v[\w.-]+/gm], ['Cargo.toml', /^([A-Za-z0-9_-]+)\s*=/gm], @@ -265,6 +264,18 @@ async function main() { ['mix.exs', /\{:([a-z_]+)\s*,/g], ]; const deps: string[] = []; + try { + // package.json needs a real parse: a line scan would also match script + // names, and only the dependency blocks carry dependencies. + const pkg = JSON.parse( + fs.readFileSync(`${appDir}/package.json`, 'utf8'), + ); + deps.push( + ...Object.keys({ ...pkg.dependencies, ...pkg.devDependencies }), + ); + } catch { + /* not a JS project */ + } for (const [file, pattern] of MANIFESTS) { try { const text = fs.readFileSync(`${appDir}/${file}`, 'utf8');