From 770fa98fd2b01ddca8a9921260da6257a234545e Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Sat, 21 Nov 2026 20:37:31 +0800 Subject: [PATCH] fix(prompt): omit template content from Nacos update diagnostics --- .../argi-autoconfigure-nacos-prompt/README.md | 10 ++++ prompt/argi-prompt-nacos/pom.xml | 6 +++ .../ConfigurablePromptTemplateFactory.java | 25 +++++---- ...onfigurablePromptTemplateFactoryTests.java | 53 +++++++++++++++++++ 4 files changed, 84 insertions(+), 10 deletions(-) diff --git a/auto-configurations/prompt/argi-autoconfigure-nacos-prompt/README.md b/auto-configurations/prompt/argi-autoconfigure-nacos-prompt/README.md index 4f7d048b5..711e931da 100644 --- a/auto-configurations/prompt/argi-autoconfigure-nacos-prompt/README.md +++ b/auto-configurations/prompt/argi-autoconfigure-nacos-prompt/README.md @@ -22,3 +22,13 @@ must not hide Nacos Config configuration or connection failures. The regression tests cover the Prompt auto-configuration offline. They do not verify connections to a live Nacos server. + +### Dynamic prompt update logs + +Queries for `OnPromptTemplateConfigChange` can still filter on `templateName`. +Use `result:applied` (INFO) or `result:failed` (WARN) instead of the former +`template` and `model` fields. Neither prompt text nor variable values are logged +by the factory. A failure identifies the exception type, without forwarding its +message or cause. Each result refers to one template: if a later update fails, +processing stops and earlier updates remain applied. An applied entry does not +indicate that the whole batch succeeded. diff --git a/prompt/argi-prompt-nacos/pom.xml b/prompt/argi-prompt-nacos/pom.xml index 5701f5e21..455ebcb39 100644 --- a/prompt/argi-prompt-nacos/pom.xml +++ b/prompt/argi-prompt-nacos/pom.xml @@ -79,6 +79,12 @@ + + ch.qos.logback + logback-classic + test + + org.springframework.ai spring-ai-test diff --git a/prompt/argi-prompt-nacos/src/main/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactory.java b/prompt/argi-prompt-nacos/src/main/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactory.java index d973d5aa9..30eca6084 100644 --- a/prompt/argi-prompt-nacos/src/main/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactory.java +++ b/prompt/argi-prompt-nacos/src/main/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactory.java @@ -73,16 +73,21 @@ protected void onConfigChange(List configList) if (!StringUtils.hasText(configuration.name()) || !StringUtils.hasText(configuration.template())) { continue; } - PromptTemplate.Builder promptTemplateBuilder = promptTemplateBuilderConfigure - .configure(PromptTemplate.builder() - .template(configuration.template()) - .variables(configuration.model() == null ? new HashMap<>() : configuration.model())); - - templates.put(configuration.name(), - new ConfigurablePromptTemplate(configuration.name(), promptTemplateBuilder.build())); - - logger.info("OnPromptTemplateConfigChange,templateName:{},template:{},model:{}", configuration.name(), - configuration.template(), configuration.model()); + String name = configuration.name(); + try { + PromptTemplate.Builder promptTemplateBuilder = promptTemplateBuilderConfigure + .configure(PromptTemplate.builder() + .template(configuration.template()) + .variables(configuration.model() == null ? new HashMap<>() : configuration.model())); + templates.put(name, new ConfigurablePromptTemplate(name, promptTemplateBuilder.build())); + } + catch (RuntimeException ex) { + // Builders and customizers can include prompt content in exception messages or causes. + String errorType = ex.getClass().getSimpleName(); + logger.warn("OnPromptTemplateConfigChange,templateName:{},result:failed,errorType:{}", name, errorType); + throw new IllegalStateException("Failed to update prompt template " + name + " (" + errorType + ")"); + } + logger.info("OnPromptTemplateConfigChange,templateName:{},result:applied", name); } } diff --git a/prompt/argi-prompt-nacos/src/test/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactoryTests.java b/prompt/argi-prompt-nacos/src/test/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactoryTests.java index f7bbc3dae..f5efecd1f 100644 --- a/prompt/argi-prompt-nacos/src/test/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactoryTests.java +++ b/prompt/argi-prompt-nacos/src/test/java/io/github/agentic/ai/prompt/ConfigurablePromptTemplateFactoryTests.java @@ -17,14 +17,20 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import org.slf4j.LoggerFactory; import org.springframework.ai.chat.prompt.PromptTemplate; import org.springframework.ai.template.st.StTemplateRenderer; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; /** * Test cases for ConfigurablePromptTemplateFactory. Tests template creation, retrieval, @@ -183,6 +189,53 @@ void testOnConfigChangeWithInvalidConfigs() { assertThat(factory.getTemplate("")).isNull(); } + @Test + void updateDiagnosticsDoNotExposeConfigOnSuccessOrPartialFailure() { + String body = "template-marker {value}"; + var variables = Map.of("value", "variable-marker"); + var configure = new PromptTemplateBuilderConfigure(); + var count = new AtomicInteger(); + configure.setPromptTemplateBuilderCustomizers(List.of(builder -> { + if (count.incrementAndGet() == 2) { + throw new IllegalArgumentException("exception-marker", new RuntimeException("cause-marker")); + } + })); + factory = new ConfigurablePromptTemplateFactory(configure); + var first = new ConfigurablePromptTemplateFactory.ConfigurablePromptTemplateModel("first", body, variables); + var second = new ConfigurablePromptTemplateFactory.ConfigurablePromptTemplateModel("second", body, variables); + var third = new ConfigurablePromptTemplateFactory.ConfigurablePromptTemplateModel("third", body, variables); + + Logger logger = (Logger) LoggerFactory.getLogger(ConfigurablePromptTemplateFactory.class); + ListAppender logs = new ListAppender<>(); + logs.start(); + logger.addAppender(logs); + Throwable failure; + try { + failure = catchThrowable(() -> factory.onConfigChange(List.of(first, second, third))); + } + finally { + logger.detachAppender(logs); + logs.stop(); + } + assertThat(logs.list).isNotEmpty().allSatisfy(event -> { + assertThat(event.getFormattedMessage() + Arrays.deepToString(event.getArgumentArray())) + .doesNotContain("template-marker", "variable-marker", "exception-marker", "cause-marker"); + assertThat(event.getThrowableProxy()).isNull(); + }); + assertThat(logs.list).extracting(ILoggingEvent::getFormattedMessage) + .anySatisfy(line -> assertThat(line).contains("templateName:first,result:applied")) + .anySatisfy(line -> assertThat(line).contains("templateName:second,result:failed", + "IllegalArgumentException")); + assertThat(failure).isInstanceOf(IllegalStateException.class) + .hasMessageContaining("second") + .hasMessageContaining("IllegalArgumentException") + .hasMessageNotContaining("exception-marker") + .hasNoCause(); + assertThat(factory.getTemplate("first").render()).isEqualTo("template-marker variable-marker"); + assertThat(factory.getTemplate("second")).isNull(); + assertThat(factory.getTemplate("third")).isNull(); + } + @Test void testTemplateReuse() { // Test that same name returns same template instance