Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/__tests__/architecture/known-violations.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"src/programs/task-stream/task-stream-push.ts -> src/ui/wizard-ui.ts",
"src/programs/task-stream/types.ts -> src/lib/wizard-session.ts",
"src/programs/task-stream/wizard-run-sync.ts -> src/lib/wizard-session.ts",
"src/programs/warehouse-scan-reporting.ts -> src/lib/wizard-session.ts",
"src/programs/warehouse-source/detect.ts -> src/lib/wizard-session.ts",
"src/programs/warehouse-source/index.ts -> src/lib/wizard-session.ts",
"src/programs/warehouse-source/index.ts -> src/ui/tui/decks/warehouse-source/index.tsx",
Expand Down Expand Up @@ -165,7 +166,6 @@
"src/ui/tui/screens/doctor/IssueTable.tsx -> src/programs/posthog-doctor/index.ts",
"src/ui/tui/screens/health/HealthCheckScreen.tsx -> src/agent/index.ts",
"src/ui/tui/services/mcp-suggested-prompts-services.ts -> src/agent/index.ts",
"src/ui/tui/store.ts -> src/programs/ai-opt-in-gate.ts",
"src/ui/tui/store.ts -> src/programs/posthog-integration/detect.ts"
"src/ui/tui/store.ts -> src/programs/ai-opt-in-gate.ts"
]
}
3 changes: 3 additions & 0 deletions src/lib/runners/run-non-interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ export function runNonInteractive(
setPosthogSdkDetected: (detected: boolean) => {
session.posthogSdkDetected = detected;
},
markScanReported: () => {
session.warehouseSourcesReported = true;
},
};
for (const step of config.steps) {
if (step.onReady) {
Expand Down
1 change: 1 addition & 0 deletions src/programs/__tests__/run-agent-legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ vi.mock('@utils/wizard-abort', async (original) => ({
}));
vi.mock('../posthog-integration/detect', () => ({
maybeStampAiSdkDetected: vi.fn(),
reportWarehouseSourcesDetected: vi.fn(),
}));
vi.mock('../posthog-integration/ai-sdk-stamp', () => ({
stampAiSdkDetected: vi.fn(),
Expand Down
11 changes: 8 additions & 3 deletions src/programs/posthog-integration/__tests__/detect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function makeCtx(session: WizardSession): ProgramReadyContext {
setUnsupportedVersion: vi.fn(),
addDiscoveredFeature: vi.fn(),
setDetectionComplete: vi.fn(),
markScanReported: vi.fn(),
};
}

Expand Down Expand Up @@ -293,9 +294,13 @@ describe('reportWarehouseSourcesDetected', () => {
const { detectWarehousePrerequisites } = await import(
'@programs/warehouse-source/detect'
);
detectWarehousePrerequisites(session, (key, value) => {
session.frameworkContext[key] = value;
});
detectWarehousePrerequisites(
session,
(key, value) => {
session.frameworkContext[key] = value;
},
() => undefined,
);
expect(
session.frameworkContext[DETECTED_WAREHOUSE_SOURCES_KEY],
).toBeDefined();
Expand Down
89 changes: 39 additions & 50 deletions src/programs/posthog-integration/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
*/

import type { ProgramReadyContext } from '@programs/program-step';
import {
mayReportScanResults,
ScanConsent,
type WizardSession,
} from '@lib/wizard-session';
import { mayReportScanResults, type WizardSession } from '@lib/wizard-session';
import { FRAMEWORK_REGISTRY } from '@programs/frameworks/registry';
import {
detectFramework,
Expand All @@ -24,6 +20,7 @@ import {
} from '@programs/detection/index';
import { analytics } from '@utils/analytics';
import { detectWarehouseSources } from '@programs/warehouse-sources/detect';
import { resolveScanReporting } from '@programs/warehouse-scan-reporting';
import {
DETECTED_WAREHOUSE_SOURCES_KEY,
getDetectedWarehouseSources,
Expand Down Expand Up @@ -95,13 +92,9 @@ export async function detectPostHogIntegration(
}

/**
* How the scan went, for the reporter to read later. Three states, and the
* absent one carries the most weight:
* How the scan went, for the reporter to read later. Three states:
*
* absent this program never scanned. Only `posthog-integration` runs the
* scan below, but every program's intro screen resolves consent
* through the same store method, so the reporter is reached on runs
* where no scan ever happened.
* absent this program never scanned.
* 'failed' the scan threw. A zero-count row would read as "scanned, found
* nothing", which is a different fact.
* 'ok' the scan ran. Zero sources is a real result and gets reported.
Expand Down Expand Up @@ -189,49 +182,45 @@ export function maybeStampAiSdkDetected(session: WizardSession): void {
* Returns true when consent has resolved and the caller should latch
* `warehouseSourcesReported`, which is not the same as "this sent something":
* a decline, a failed scan, and a program that never scanned all resolve
* without sending.
* without sending. Nothing fires on 'declined': saved insights read every row
* of this event as a scan that ran. Decline rate comes from 'intro menu
* selected' instead.
*/
export function reportWarehouseSourcesDetected(
session: WizardSession,
): boolean {
if (session.warehouseSourcesReported) return false;
// 'undecided' means come back later, not no.
if (session.scanConsent === ScanConsent.Undecided) return false;

if (mayReportScanResults(session)) {
const sources = getDetectedWarehouseSources(session);

// Only an 'ok' scan has something to say. Absent means this program never
// scanned, and reporting a zero count there would invent a scan that never
// ran; 'failed' already fired captureException.
const scanState = session.frameworkContext[WAREHOUSE_SCAN_STATE_KEY] as
| WarehouseScanState
| undefined;
if (scanState !== 'ok') return true;

// Captured on every run, including the empty case. The denominator is
// the whole point: without the zero rows, "20% of runs have a Postgres"
// is unanswerable.
analytics.wizardCapture('warehouse sources detected', {
warehouse_source_count: sources.length,
warehouse_source_kinds: sources.map((s) => s.kind),
warehouse_source_modes: sources.map((s) => s.mode),
});

if (sources.length > 0) {
// Tag subsequent events too, so any downstream funnel can slice on what the
// project had available without re-joining to the event above.
analytics.setTag(
'warehouse_source_kinds',
sources.map((s) => s.kind).join(','),
);
analytics.setTag('warehouse_source_count', sources.length);
}
}
// Nothing on 'declined': saved insights read every row of this event as a
// scan that ran. Decline rate comes from 'intro menu selected' instead.

return true;
return resolveScanReporting(
session,
getDetectedWarehouseSources(session),
(sources) => {
// Only an 'ok' scan has something to say. Absent means this program never
// scanned, and reporting a zero count there would invent a scan that never
// ran; 'failed' already fired captureException.
const scanState = session.frameworkContext[WAREHOUSE_SCAN_STATE_KEY] as
| WarehouseScanState
| undefined;
if (scanState !== 'ok') return;

// Captured on every run, including the empty case. The denominator is
// the whole point: without the zero rows, "20% of runs have a Postgres"
// is unanswerable.
analytics.wizardCapture('warehouse sources detected', {
warehouse_source_count: sources.length,
warehouse_source_kinds: sources.map((s) => s.kind),
warehouse_source_modes: sources.map((s) => s.mode),
});

if (sources.length > 0) {
// Tag subsequent events too, so any downstream funnel can slice on what the
// project had available without re-joining to the event above.
analytics.setTag(
'warehouse_source_kinds',
sources.map((s) => s.kind).join(','),
);
analytics.setTag('warehouse_source_count', sources.length);
}
},
);
}

/** Dependency-level signal, not a verified install. A failed scan reports false. */
Expand Down
2 changes: 2 additions & 0 deletions src/programs/posthog-integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { openTrackedLink, withUtm } from '@utils/links';
import type { HostResolution } from '@shared/host-resolution';
import { getDetectedWarehouseSources } from '@programs/warehouse-source/detect';
import { POSTHOG_INTEGRATION_PROGRAM } from './steps.js';
import { reportWarehouseSourcesDetected } from './detect.js';
import { getContentBlocks } from '../../ui/tui/decks/posthog-integration/index.js';
import { buildCodingAgentPrompt } from './handoff.js';
import { EVENT_PLAN_FILE } from './constants.js';
Expand Down Expand Up @@ -252,6 +253,7 @@ export const posthogIntegrationConfig: ProgramConfig = {
disallowedTools: [WIZARD_TOOL_NAMES.wizardAsk],

seedTasks: warehouseSeedTasks,
reportScanResults: reportWarehouseSourcesDetected,

// Kill switch over the shipped default: only an explicit 'false' excludes,
// so a failed flag fetch keeps AI Observability and Logs in the run.
Expand Down
12 changes: 12 additions & 0 deletions src/programs/program-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface ProgramReadyContext {
readonly addDiscoveredFeature: (feature: DiscoveredFeature) => void;
readonly setDetectionComplete: () => void;
readonly setPosthogSdkDetected: (detected: boolean) => void;
/** Latches `warehouseSourcesReported` once a scan has been reported or declined. */
readonly markScanReported: () => void;
}

export interface ProgramStep {
Expand Down Expand Up @@ -280,6 +282,16 @@ export interface ProgramConfig {
excludedTaskTypes?: (flags: Record<string, string>) => readonly string[];
/** Prerequisites: other program ids that must have run first */
requires?: string[];
/**
* Turn this program's warehouse-source scan into telemetry once scan
* consent resolves. `WizardStore` calls this generically (via
* `getProgramConfig`) rather than importing a specific program's reporting
* function — so the store stays unaware of which program is active.
* Returns whether reporting resolved (granted or declined), so the store
* can set its idempotency flag. Omit for a program that never scans for
* warehouse sources.
*/
reportScanResults?: (session: WizardSession) => boolean;
/**
* Path (relative to installDir) of the report file the program writes.
* Mirrors `run.reportFile` but lifted to the top level so UI screens can
Expand Down
35 changes: 35 additions & 0 deletions src/programs/warehouse-scan-reporting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Consent-gated telemetry for a completed warehouse-source scan.
*
* Both the `posthog-integration` and `warehouse-source` programs scan the
* same project for warehouse sources and want to report what they found —
* but only once the user has been asked. `resolveScanReporting` is the one
* place that question gets answered, so a future reporting site can't
* forget the check and report on an undecided or declined scan.
*/

import {
mayReportScanResults,
ScanConsent,
type WizardSession,
} from '@lib/wizard-session';
import type { DetectedSource } from '@programs/warehouse-sources/types';

/**
* The one consent check for scan telemetry. Callers pass `sources` because a
* caller mid-detection may hold a fresher value than `session` does, and own
* the `warehouseSourcesReported` flag themselves: 'declined' resolves (returns
* true) without emitting, 'undecided' does not resolve at all.
*/
export function resolveScanReporting(
session: WizardSession,
sources: DetectedSource[],
emit: (sources: DetectedSource[]) => void,
): boolean {
if (session.warehouseSourcesReported) return false;
if (session.scanConsent === ScanConsent.Undecided) return false;

if (mayReportScanResults(session)) emit(sources);

return true;
}
Loading
Loading