Files
yidaima/RuoYi-Vue/docs/superpowers/plans/2026-06-18-business-blueprint-schema-repair.md

87 lines
3.9 KiB
Markdown
Raw Normal View History

# Business Blueprint Schema Repair Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Prevent fixed prompt examples from generating invalid target-table condition fields and allow one schema-guided correction attempt.
**Architecture:** `AiGenerateServiceImpl` will build its example from the saved database schema and route both initial and corrected AI responses through one parse-normalize-validate helper. Strict validation remains the authority; correction is bounded to one extra model call.
**Tech Stack:** Java 8, Spring Boot, Fastjson, Mockito, JUnit 4, Maven.
---
### Task 1: Add prompt and correction regression tests
**Files:**
- Modify: `ruoyi-generator/src/test/java/com/ruoyi/generator/service/front/AiGenerateServiceImplTest.java`
- [x] Add a valid response fixture whose rule targets `book_info` with `conditionFields: ["id"]`.
- [x] Add a test using `mockSavedTablesWithGenericBookPrimaryKey()` that captures the initial prompt and asserts the JSON example contains `"conditionFields":["id"]` and does not contain a `book_info` rule conditioned by `book_id`.
- [x] Add a test returning the invalid `book_id` response first and the valid `id` response second; assert two calls, a correction prompt containing the original validation error, and persisted `id`.
- [x] Add a test returning invalid responses twice; assert the strict error is propagated and the AI client is called exactly twice.
- [x] Run:
```powershell
mvn -pl ruoyi-generator -Dtest=AiGenerateServiceImplTest test
```
Expected: the new tests fail because the prompt is fixed and there is no correction call.
### Task 2: Generate a schema-valid example
**Files:**
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/AiGenerateServiceImpl.java`
- [x] Add a helper that selects the first saved table with columns.
- [x] Add a helper that selects that table's primary key, falling back to its first column.
- [x] Serialize a minimal action example using that table and field:
```json
{"businessActions":[{"code":"example_workflow","name":"示例流程","ownerTable":"<table>","method":"POST","path":"/workflow/example","transaction":true,"requestFields":["<field>"],"rules":["目标记录必须存在"],"ruleChecks":[{"type":"EXISTS","targetTable":"<table>","conditionFields":["<field>"],"message":"目标记录不存在"}],"effects":[]}]}
```
- [x] Replace the fixed `book_info.book_id` example in `buildBusinessBlueprintPrompt`.
- [x] Run the targeted test and confirm the prompt test passes.
### Task 3: Add one strict correction attempt
**Files:**
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/AiGenerateServiceImpl.java`
- [x] Extract a helper that parses, normalizes, and validates one AI response.
- [x] In `generateBusinessBlueprint`, call the helper for the first response.
- [x] Catch only `ServiceException` from that helper and build a correction prompt containing:
```text
Correct the business blueprint JSON below.
Return JSON only.
Validation error: <message>
Use only the saved target-table columns listed below.
<saved schema>
Original response:
<response>
```
- [x] Call the AI client once more and run the corrected response through the same helper without another retry.
- [x] Persist and record only the final validated response.
- [x] Run the targeted test and confirm all correction tests pass.
### Task 4: Verify the module
**Files:**
- Verify only.
- [x] Run:
```powershell
mvn -pl ruoyi-generator -Dtest=AiGenerateServiceImplTest,AiGenerationTaskWorkerTest,GenProjectServiceImplTest,QingTemplateSupportTest test
```
- [x] Run:
```powershell
mvn -pl ruoyi-generator test
```
- [x] Inspect `git diff` for the implementation and tests, preserving all unrelated user changes.