feat: enforce AI page recipe gate

This commit is contained in:
2026-06-17 12:33:09 +08:00
parent e55072598e
commit fcc5719bc6
12 changed files with 414 additions and 11 deletions

View File

@@ -61,6 +61,10 @@ import com.eip4.platform.metadata.schema.PageComponentRegistry;
import com.eip4.platform.metadata.schema.TypedPageSchemaIssue;
import com.eip4.platform.metadata.schema.TypedPageSchemaReport;
import com.eip4.platform.metadata.schema.TypedPageSchemaValidator;
import com.eip4.platform.metadata.schema.blueprint.PageBlueprint;
import com.eip4.platform.metadata.schema.blueprint.PageRecipeRegistry;
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidationReport;
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -86,6 +90,7 @@ public class AiAgentService {
private final ObjectMapper objectMapper;
private final AiToolRegistry toolRegistry;
private final TypedPageSchemaValidator typedSchemaValidator;
private final PageRecipeValidator pageRecipeValidator;
private static volatile SchemaGate sharedSchemaGate;
private volatile SchemaGate schemaGate;
@@ -122,6 +127,7 @@ public class AiAgentService {
this.typedSchemaValidator = typedSchemaValidator == null
? new TypedPageSchemaValidator(objectMapper, PageComponentRegistry.defaults())
: typedSchemaValidator;
this.pageRecipeValidator = new PageRecipeValidator(objectMapper, PageRecipeRegistry.defaults());
}
public AiTask createTask(String objective) {
@@ -154,7 +160,8 @@ public class AiAgentService {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "payload 不能为空");
}
List<AiGateResult> gates = validateDraft(artifactType, target, payload);
Map<String, Object> safeMetadata = metadata == null ? Map.of() : metadata;
List<AiGateResult> gates = validateDraft(artifactType, target, payload, safeMetadata);
AiDraft draft = new AiDraft(
"aid-" + UUID.randomUUID(),
taskId,
@@ -163,7 +170,7 @@ public class AiAgentService {
payload,
System.currentTimeMillis(),
gates,
metadata == null ? Map.of() : Map.copyOf(metadata));
Map.copyOf(safeMetadata));
store.saveDraft(draft);
audit(taskId, "draft.saved", "agent", Map.of(
"draftId", draft.id(),
@@ -781,6 +788,49 @@ public class AiAgentService {
return new AiGateResult("typed-page-schema", report.valid(), typedMessages(report));
}
private AiGateResult validatePageRecipeGate(String targetId, JsonNode schema, Map<String, Object> metadata) {
boolean hasRecipe = metadata.containsKey("pageRecipe");
boolean hasBlueprint = metadata.containsKey("pageBlueprint");
if (!hasRecipe && !hasBlueprint) {
return new AiGateResult("page-recipe", true, List.of("未声明 pageRecipe,按手工 PAGE_SCHEMA 草稿跳过 recipe 门禁"));
}
List<String> messages = new ArrayList<>();
Object recipeValue = metadata.get("pageRecipe");
Object blueprintValue = metadata.get("pageBlueprint");
Object versionValue = metadata.get("pageBlueprintVersion");
if (!(recipeValue instanceof String recipe) || recipe.isBlank()) {
messages.add("pageRecipe metadata 不能为空");
}
if (blueprintValue == null) {
messages.add("pageBlueprint metadata 不能为空");
}
if (!(versionValue instanceof Number)) {
messages.add("pageBlueprintVersion metadata 必须是数字");
}
if (!messages.isEmpty()) {
return new AiGateResult("page-recipe", false, messages);
}
try {
PageBlueprint blueprint = objectMapper.convertValue(blueprintValue, PageBlueprint.class);
if (!recipeValue.equals(blueprint.recipe())) {
messages.add("pageRecipe 与 pageBlueprint.recipe 不一致");
}
if (!targetId.equals(blueprint.templateId())) {
messages.add("targetId 与 pageBlueprint.templateId 不一致");
}
if (((Number) versionValue).intValue() != blueprint.schemaVersion()) {
messages.add("pageBlueprintVersion 与 pageBlueprint.schemaVersion 不一致");
}
if (!messages.isEmpty()) {
return new AiGateResult("page-recipe", false, messages);
}
PageRecipeValidationReport report = pageRecipeValidator.validate(blueprint, schema);
return new AiGateResult("page-recipe", report.valid(), report.messages());
} catch (IllegalArgumentException ex) {
return new AiGateResult("page-recipe", false, List.of("pageBlueprint metadata 解析失败: " + ex.getMessage()));
}
}
private TypedPageSchemaReport validateTypedPageSchema(JsonNode schema) {
try {
return typedSchemaValidator.validate(schema == null ? "null" : objectMapper.writeValueAsString(schema));
@@ -800,13 +850,15 @@ public class AiAgentService {
return issue.path() + " " + issue.code() + ": " + issue.message();
}
private List<AiGateResult> validateDraft(AiArtifactType type, String targetId, JsonNode payload) {
private List<AiGateResult> validateDraft(AiArtifactType type, String targetId, JsonNode payload,
Map<String, Object> metadata) {
List<AiGateResult> gates = new ArrayList<>();
gates.add(requiredFieldsGate(type, targetId, payload));
switch (type) {
case PAGE_SCHEMA -> {
gates.add(validateSchemaGate(payload));
gates.add(validateTypedPageSchemaGate(payload));
gates.add(validatePageRecipeGate(targetId, payload, metadata));
}
case ENTITY -> gates.add(validateEntityGate(payload));
case SERVICEFLOW -> gates.add(validateServiceFlowGate(payload));

View File

@@ -1,6 +1,7 @@
package com.eip4.platform.ai.service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -251,7 +252,7 @@ public class DeterministicAiAgentRepairExecutor implements AiAgentRepairExecutor
AiArtifactType.PAGE_SCHEMA,
draft.targetId(),
copy,
Map.of(
repairMetadata(draft,
"source", "deterministic-agent-repair",
"repairActionId", action.id(),
"supersedesDraftId", draft.id())));
@@ -318,4 +319,15 @@ public class DeterministicAiAgentRepairExecutor implements AiAgentRepairExecutor
Object value = action.details() == null ? null : action.details().get(key);
return value == null ? "" : String.valueOf(value);
}
private static Map<String, Object> repairMetadata(AiDraft draft, Object... entries) {
Map<String, Object> out = new LinkedHashMap<>();
if (draft.metadata() != null) {
out.putAll(draft.metadata());
}
for (int i = 0; i + 1 < entries.length; i += 2) {
out.put(String.valueOf(entries[i]), entries[i + 1]);
}
return Map.copyOf(out);
}
}

View File

@@ -43,6 +43,7 @@ fieldMappings:
datetime: input-datetime
boolean: switch
gates:
- page-recipe
- typed-page-schema
- amis-schema
- local-smoke

View File

@@ -21,6 +21,7 @@ import com.eip4.platform.ai.model.AiRepairDraftPatch;
import com.eip4.platform.ai.store.InMemoryAiAgentStore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
class AiAgentServiceTest {
@@ -123,6 +124,10 @@ class AiAgentServiceTest {
assertEquals(5, result.drafts().size());
assertTrue(result.drafts().stream().allMatch(d -> d.passed()), "自动生成草稿必须全部过基础门禁");
assertTrue(result.drafts().stream()
.filter(d -> d.artifactType() == AiArtifactType.PAGE_SCHEMA)
.flatMap(d -> d.gates().stream())
.anyMatch(g -> "page-recipe".equals(g.name()) && g.passed()));
assertEquals("READY_FOR_REVIEW", result.changeSet().status());
assertEquals(5, result.changeSet().draftIds().size());
assertEquals(5, service.listDrafts(result.task().id()).size());
@@ -170,6 +175,25 @@ class AiAgentServiceTest {
assertEquals(1, service.listRevisions(result.task().id()).size());
}
@Test
@DisplayName("声明 PageBlueprint 的页面草稿必须匹配 recipe 编译结果")
void pageRecipeGateRejectsTamperedSchema() {
AiAgentService service = newService();
var generated = new AiFeatureDraftGenerator(MAPPER).generate(contractTypeFeature()).stream()
.filter(d -> d.artifactType() == AiArtifactType.PAGE_SCHEMA)
.findFirst().orElseThrow();
ObjectNode tampered = generated.payload().deepCopy();
tampered.put("title", "被篡改标题");
var task = service.createTask("验证 page recipe 门禁");
var draft = service.addDraft(task.id(), "PAGE_SCHEMA", generated.targetId(), tampered, generated.metadata());
assertFalse(draft.passed());
assertTrue(draft.gates().stream()
.anyMatch(g -> "page-recipe".equals(g.name()) && !g.passed()
&& g.messages().toString().contains("$.title")));
}
@Test
@DisplayName("一键自动开发会串联草稿生成、本地应用、smoke 和部署包")
void autoDevelopFeatureRunsFullPipeline() {