luigidemasi commented on code in PR #27649:
URL: https://github.com/apache/camel/pull/27649#discussion_r4237165058
##########
components/camel-ai/camel-semantic/src/main/java/org/apache/camel/language/semantic/SemanticLanguage.java:
##########
@@ -713,20 +786,50 @@ private Object evaluate(Exchange exchange, boolean
asPredicate) {
return Collections.unmodifiableMap(ordered);
}
SemanticEvaluation evaluation =
current.evaluations.get(names.get(0));
+ active.add(invocations.get(names.get(0)));
SemanticResult result =
current.groups.get(0).expert.provider.evaluate(evaluation, state);
+ failureReason = "invalid_result";
Object decision = validateResult(names.get(0),
current.groups.get(0).expert,
current.operations.get(names.get(0)), result);
+ invocations.get(names.get(0)).complete("success",
"evaluation_completed", result);
exchange.setProperty(RESULT, result);
return decision;
} catch (InterruptedException e) {
+ finishFailed(invocations, active, "interrupted");
exchange.removeProperty(RESULT);
exchange.removeProperty(RESULTS);
Thread.currentThread().interrupt();
throw RuntimeCamelException.wrapRuntimeCamelException(e);
- } catch (Exception e) {
+ } catch (Exception | AssertionError e) {
+ if (invocations.isEmpty()) {
+ for (String name : names) {
+ SemanticEvaluation declaration = selected.get(name);
+ SemanticAudit.Invocation rejected =
audit.begin(exchange, name, declaration, null,
+ auditExpert(declaration), null, null, null);
+ invocations.put(name, rejected);
+ rejected.complete("failed", failureReason, null);
+ }
+ }
+ finishFailed(invocations, active, failureReason);
exchange.removeProperty(RESULT);
exchange.removeProperty(RESULTS);
+ if (e instanceof AssertionError error) {
+ throw error;
+ }
throw RuntimeCamelException.wrapRuntimeCamelException(e);
+ } finally {
+ if (!invocations.isEmpty()) {
+ exchange.setProperty(SemanticAudit.REFERENCES,
invocations.values().stream()
Review Comment:
Fixed in `fa99d7d1407991cf4c2e0bafbf9ce1e6f146a1aa`. Both reference updates
now leave the property absent when there are no event IDs, and clear any stale
references. The disabled-capture regression asserts that the property is null;
inference still executes normally.
_AI-generated by OpenAI Codex on behalf of @luigidemasi via
/oss-address-review._
##########
components/camel-ai/camel-semantic/src/main/java/org/apache/camel/semantic/SemanticAuditRecord.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.semantic;
+
+import java.time.Instant;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Immutable, size-bounded snapshot. It never contains the exchange, input or
provider exception. Schema version 1 uses
+ * UTC timestamps and keeps route actions separate from expert results.
+ */
+public final class SemanticAuditRecord {
+ private final Map<String, Object> fields;
+
+ SemanticAuditRecord(Map<String, Object> fields) {
+ this.fields = Map.copyOf(fields);
Review Comment:
Applied in `fa99d7d1407991cf4c2e0bafbf9ce1e6f146a1aa`: the record keeps an
unmodifiable LinkedHashMap copy. A regression verifies that reconstruction
preserves field order, alongside the existing immutability checks.
_AI-generated by OpenAI Codex on behalf of @luigidemasi via
/oss-address-review._
##########
components/camel-ai/camel-semantic/src/main/java/org/apache/camel/semantic/SemanticAudit.java:
##########
@@ -0,0 +1,515 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.semantic;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.ThreadPoolBuilder;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Context-owned audit dispatcher and independent semantic observation hooks.
*/
+public final class SemanticAudit extends ServiceSupport {
+ public static final String REFERENCES = "CamelSemanticAuditReferences";
+ public static final String REQUEST = "CamelSemanticAuditRequest";
+ private static final Logger LOG =
LoggerFactory.getLogger(SemanticAudit.class);
+ private static final Object CREATION_LOCK = new Object();
+ private final CamelContext context;
+ private volatile SemanticAuditConfiguration configuration =
SemanticAuditConfiguration.DISABLED;
+ private String configurationSource;
+ private final AtomicLong dropped = new AtomicLong();
+ private final AtomicLong observerErrors = new AtomicLong();
+ private final Map<String, AtomicLong> sinkErrors = new LinkedHashMap<>();
+ private final Map<String, SemanticAuditSink> sinks = new LinkedHashMap<>();
+ private volatile List<SemanticObserver> observers = List.of();
+ private volatile ExecutorService executor;
+ private MemorySemanticAuditStore memory;
+ private volatile SemanticAuditReader reader;
+ private volatile boolean activated;
+ private boolean frozen;
+ private final Invocation noop = new Invocation(null, false);
+
+ private SemanticAudit(CamelContext context) {
+ this.context = context;
+ }
+
+ public static SemanticAudit get(CamelContext context) {
+ var extension = context.getCamelContextExtension();
+ SemanticAudit audit = extension.getContextPlugin(SemanticAudit.class);
+ if (audit == null) {
+ synchronized (CREATION_LOCK) {
+ audit = extension.getContextPlugin(SemanticAudit.class);
+ if (audit == null) {
+ audit = new SemanticAudit(context);
+ extension.addContextPlugin(SemanticAudit.class, audit);
+ }
+ }
+ }
+ return audit;
+ }
+
+ public synchronized void validateConfiguration(String source,
SemanticAuditConfiguration candidate) {
+ if (configurationSource != null && !configurationSource.equals(source)
&& candidate != null) {
+ throw new IllegalArgumentException("Declare semantic audit
configuration in only one resource");
+ }
+ if (candidate == null && !source.equals(configurationSource)) {
+ return;
+ }
+ SemanticAuditConfiguration next = candidate == null ?
SemanticAuditConfiguration.DISABLED : candidate;
+ if (frozen && !configuration.equals(next)) {
+ throw new IllegalArgumentException("Changing semantic audit
configuration requires a context restart");
+ }
+ }
+
+ public synchronized void configure(String source,
SemanticAuditConfiguration candidate) {
+ validateConfiguration(source, candidate);
+ if (candidate != null || source.equals(configurationSource)) {
+ configuration = candidate == null ?
SemanticAuditConfiguration.DISABLED : candidate;
+ configurationSource = candidate == null ? null : source;
+ }
+ }
+
+ synchronized void removeConfigurationSource(String source) {
+ if (source.equals(configurationSource)) {
+ configurationSource = null;
+ if (frozen) {
+ LOG.warn("Semantic audit resource {} was deleted; retaining
the active configuration until context restart",
+ source);
+ } else {
+ configuration = SemanticAuditConfiguration.DISABLED;
+ }
+ }
+ }
+
+ public void activate() {
+ if (!activated) {
+ synchronized (this) {
+ if (!activated) {
+ try {
+ context.addService(this, true, true);
+ activated = true;
+ } catch (Exception e) {
+ throw
RuntimeCamelException.wrapRuntimeCamelException(e);
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ protected void doStart() throws Exception {
+ synchronized (this) {
+ frozen = true;
Review Comment:
Fixed in `fa99d7d1407991cf4c2e0bafbf9ce1e6f146a1aa`. Stopping releases the
configuration freeze; startup resolves the configured backends again and
rebuilds the memory store when capacity changes. Unchanged capacity preserves
retained history. The new context stop/reconfigure/start test switches sinks
and capacity and verifies the old sink receives no further records. The
lifecycle documentation now describes this behavior.
_AI-generated by OpenAI Codex on behalf of @luigidemasi via
/oss-address-review._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]