Skip to content

fix(wizard): parse detection JSON defensively with Zod + retry - #1036

Draft
gewenyu99 wants to merge 1 commit into
posthog-code/tolerant-detection-json-parsingfrom
posthog-code/wizard-defensive-detection-parse
Draft

fix(wizard): parse detection JSON defensively with Zod + retry#1036
gewenyu99 wants to merge 1 commit into
posthog-code/tolerant-detection-json-parsingfrom
posthog-code/wizard-defensive-detection-parse

Conversation

@gewenyu99

Copy link
Copy Markdown
Collaborator

Problem

The agentic project detector's extractJson used a strict JSON.parse, so any non-JSON or truncated agent reply threw a SyntaxError. In a headless monorepo run that exception propagated up to scopeInstallDirToProject, whose catch only logs and returns — so the run quietly abandoned project scoping and integrated against the repo root instead of the detected sub-project.

Why: Raised from a PostHog Signals inbox item flagging that detection's strict JSON parsing can crash the scan and, in the monorepo path, silently mis-target the wrong directory. This makes detection resilient to imperfect LLM output.

Changes

Replaced the single strict parse with a Zod-validated retry loop in detectProjectsWithAgent:

  • Run detection → locate and parse the reply → validate against a Zod schema.
  • On a malformed reply, re-prompt the agent with the specific validation problems.
  • Try up to 3 times, then fall through best-effort: coerce the last reply that at least parsed as JSON (empty report if none did) rather than throwing.

parseAgentReport never throws and is exported for unit testing. Existing security clamping (coerceAgenticReport: path clamping, targetId validation, recommended dedup) still runs on the validated output.

Test plan

  • New unit tests for parseAgentReport (well-formed, code-fenced, no-JSON, malformed-JSON, and shape-invalid-but-parseable cases).
  • pnpm build and the detection vitest suites pass locally (82 tests).

Created with PostHog from a Slack thread

@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci mcp-analytics
  • /wizard-ci revenue
  • /wizard-ci self-driving

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci revenue/stripe
  • /wizard-ci self-driving/astro
  • /wizard-ci self-driving/fastapi
  • /wizard-ci self-driving/nuxt
  • /wizard-ci self-driving/react-router
  • /wizard-ci self-driving/sveltekit

Results will be posted here when complete.

@gewenyu99
gewenyu99 changed the base branch from main to posthog-code/tolerant-detection-json-parsing July 30, 2026 13:28
The agentic detector's extractJson did a strict JSON.parse that threw a
SyntaxError on any non-JSON or truncated agent reply. In a headless monorepo
run that exception propagated to scopeInstallDirToProject, whose catch only
logs and returns — so the wizard silently integrated against the repo root
instead of the detected sub-project.

Replace the single strict parse with a Zod-validated retry loop:
- run detection, locate + JSON.parse the reply, validate against a Zod schema
- on a malformed reply, re-prompt the agent with the specific problems
- up to 3 attempts, then fall through best-effort (coerce the last reply that
  parsed as JSON; empty report if none did) instead of throwing

parseAgentReport never throws and is exported for testing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Generated-By: PostHog Code
Task-Id: ce1f323c-14f1-4ca2-85c4-072b9c0e2afb
@gewenyu99
gewenyu99 force-pushed the posthog-code/wizard-defensive-detection-parse branch from 4378de5 to 4aefff3 Compare July 30, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant