fix(shadow): recover completed read timeouts and pair issued observations

This commit is contained in:
zgw
2026-09-09 10:00:43 +08:00
parent af8b5f1830
commit edb13450a6
14 changed files with 948 additions and 37 deletions

View File

@@ -0,0 +1,205 @@
package com.ruoyi.shadow.evidence;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.ft546.protocol.Ft546EncodedCommand;
import com.ruoyi.shadow.facts.LegacyQueueProtocolProjector;
import com.ruoyi.shadow.facts.ShadowFactBatch;
import com.ruoyi.shadow.facts.ShadowLegacyQueueResult;
import com.ruoyi.shadow.facts.ShadowQueueDispatchEligibility;
import com.ruoyi.shadow.facts.ShadowQueueEndpoints;
import com.ruoyi.shadow.facts.ShadowQueueFact;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/** Compares adjacent committed polls. Queue observations are not PLC acknowledgements. */
final class DeferredShadowComparison {
private static final int MAX_PENDING = 500;
private static final Duration MAX_INTERVAL = Duration.ofSeconds(30);
private final ObjectMapper mapper;
private final LegacyQueueProtocolProjector projector;
private final ShadowComparisonService comparison;
private Map<String, Prediction> pending = Collections.emptyMap();
private Map<String, ShadowDifference> lastComparisons = Collections.emptyMap();
private String lastBatchKey;
private String lastMapsKey;
private Instant lastCapturedAt;
DeferredShadowComparison(ObjectMapper mapper, LegacyQueueProtocolProjector projector,
ShadowComparisonService comparison) {
this.mapper = mapper;
this.projector = projector;
this.comparison = comparison;
}
synchronized Map<String, ShadowDifference> prepare(ShadowFactBatch batch) {
String key = batchKey(batch);
if (key.equals(lastBatchKey)) return lastComparisons;
if (lastCapturedAt == null || batch.getDatabaseCaptureStartedAt() == null
|| batch.getDatabaseCapturedAt() == null
|| !batch.getDatabaseCaptureStartedAt().isAfter(lastCapturedAt)
|| Duration.between(lastCapturedAt, batch.getDatabaseCapturedAt()).compareTo(MAX_INTERVAL) > 0
|| !mapsKey(batch).equals(lastMapsKey)) return Collections.emptyMap();
Map<String, ShadowDifference> result = new LinkedHashMap<String, ShadowDifference>();
for (ShadowQueueFact queue : uniqueActiveQueues(batch).values()) {
Prediction prediction = pending.get(queue.getGuid());
if (prediction == null || !Integer.valueOf(1).equals(queue.getIssued())
|| !Integer.valueOf(3).equals(queue.getExecutionStatus())) continue;
ShadowLegacyQueueResult legacy = projector.project(queue, batch);
String unavailable = null;
if (!prediction.phaseKey.equals(phaseKey(queue))) unavailable = "PHASE_IDENTITY_CHANGED";
else if (legacy.getStatus() != ShadowLegacyQueueResult.Status.AVAILABLE) unavailable = legacy.getReasonCode();
else if (legacy.getPathPoints().isEmpty() || !sameSite(prediction.sourceSite, legacy.getPathPoints().get(0)))
unavailable = "PHASE_SOURCE_POSITION_CHANGED";
List<ShadowDifference> compared = unavailable == null
? comparison.compare(legacy, prediction.decision, prediction.command)
: Collections.emptyList();
boolean match = compared.size() == 1 && compared.get(0).getCategory() == ShadowDifferenceCategory.MATCH;
String reason = unavailable != null ? "DEFERRED_UNAVAILABLE" : match ? "DEFERRED_MATCH" : "DEFERRED_DIFFERENCE";
Map<String, Object> trace = new LinkedHashMap<String, Object>();
trace.put("pairingBasis", "ADJACENT_COMMITTED_PENDING_TO_ISSUED_QUEUE");
trace.put("comparisonScope", "OBSERVED_QUEUE_ONLY_NOT_PLC_ACKNOWLEDGEMENT");
trace.put("predictionCycleId", prediction.cycleId);
trace.put("predictionInputHash", prediction.inputHash);
trace.put("predictionEvidenceHash", prediction.evidenceHash);
trace.put("predictionCapturedAt", prediction.capturedAt);
trace.put("predictionCompletedAt", prediction.completedAt);
trace.put("predictionDecision", prediction.decision);
trace.put("predictionQueueHash", prediction.queueHash);
trace.put("phaseIdentity", prediction.phaseKey);
trace.put("observedQueueHash", queue.getContentHash());
trace.put("observedCapturedAt", batch.getDatabaseCapturedAt());
trace.put("intervalMillis", Duration.between(prediction.capturedAt, batch.getDatabaseCapturedAt()).toMillis());
trace.put("unavailableReason", unavailable);
trace.put("comparisons", compared);
result.put(queue.getGuid(), new ShadowDifference(ShadowDifferenceCategory.DEFERRED_COMPARISON,
reason, json(legacy), json(trace)));
}
return Collections.unmodifiableMap(result);
}
/** Called only after the outer evidence transaction has committed successfully. */
synchronized void committed(ShadowFactBatch batch, List<ShadowCycleEvidence> evidence,
List<ShadowEvidenceRepository.SavedCycle> saved,
Map<String, ShadowDifference> comparisons) {
String key = batchKey(batch);
if (key.equals(lastBatchKey)) return; // Exact replay retains the original pairing and persisted references.
if (saved.size() != evidence.size()) throw new ShadowEvidenceException("SAVED_CYCLE_COUNT_MISMATCH");
if (batch.getDatabaseCapturedAt() == null
|| (lastCapturedAt != null && !batch.getDatabaseCapturedAt().isAfter(lastCapturedAt))) {
clear();
return;
}
Map<String, List<Integer>> byPhase = new LinkedHashMap<String, List<Integer>>();
for (int i = 0; i < evidence.size(); i++) {
ShadowCycleEvidence value = evidence.get(i);
String phase = json(Arrays.asList(value.getParentTaskId(), value.getPhase()));
byPhase.computeIfAbsent(phase, ignored -> new ArrayList<Integer>()).add(i);
}
Map<String, Prediction> next = new LinkedHashMap<String, Prediction>();
for (ShadowQueueFact queue : uniqueActiveQueues(batch).values()) {
if (next.size() >= MAX_PENDING) break;
if (ShadowQueueDispatchEligibility.observationReason(queue, batch) != null
|| queue.getCreatedAt() == null || blank(queue.getTaskNo()) || blank(queue.getAgvId())) continue;
String phase = "1".equals(queue.getTaskType()) ? "EMPTY_TO_PICKUP"
: "2".equals(queue.getTaskType()) ? "LOADED_TO_PLACE" : "LEGACY_PHASE_" + trim(queue.getTaskType());
List<Integer> indices = byPhase.get(json(Arrays.asList(trim(queue.getTaskNo()), phase)));
if (indices == null || indices.size() != 1) continue;
int index = indices.get(0);
ShadowCycleEvidence value = evidence.get(index);
ShadowEvidenceRepository.SavedCycle stored = saved.get(index);
if (stored == null || stored.getCycleId() == null
|| !value.getEvidenceHash().equals(stored.getEvidenceHash())) continue;
ShadowDecision.Type type = value.prediction().getType();
if (type != ShadowDecision.Type.GRANTED && type != ShadowDecision.Type.HELD
&& type != ShadowDecision.Type.YIELDED) continue;
next.put(queue.getGuid(), new Prediction(phaseKey(queue), queue.getContentHash(),
ShadowQueueEndpoints.dispatchStart(queue, batch), stored.getCycleId(), stored.getEvidenceHash(), value));
}
pending = Collections.unmodifiableMap(next);
lastComparisons = comparisons;
lastBatchKey = key;
lastMapsKey = mapsKey(batch);
lastCapturedAt = batch.getDatabaseCapturedAt();
}
synchronized void clear() {
pending = Collections.emptyMap();
lastComparisons = Collections.emptyMap();
lastBatchKey = null;
lastMapsKey = null;
lastCapturedAt = null;
}
private Map<String, ShadowQueueFact> uniqueActiveQueues(ShadowFactBatch batch) {
Map<String, ShadowQueueFact> unique = new LinkedHashMap<String, ShadowQueueFact>();
Map<String, Integer> counts = new LinkedHashMap<String, Integer>();
for (ShadowQueueFact queue : batch.getSourceQueues()) {
if (!queue.isActive()) continue;
counts.merge(queue.getGuid(), 1, Integer::sum);
unique.put(queue.getGuid(), queue);
}
unique.entrySet().removeIf(entry -> counts.get(entry.getKey()) != 1);
return unique;
}
private String phaseKey(ShadowQueueFact q) {
return json(Arrays.asList(q.getGuid(), q.getCreatedAt(), trim(q.getTaskNo()), trim(q.getTaskType()),
trim(q.getStartSite()), trim(q.getEndSite()), trim(q.getTheFloor()), trim(q.getAgvId()),
trim(q.getCargoPickSite()), trim(q.getCargoPlaceSite()), q.getPriority(),
trim(q.getDefault02()), trim(q.getDefault03()), trim(q.getDefault04()), trim(q.getDefault05()),
trim(q.getDefault06()), trim(q.getDefault07()), trim(q.getDefault08())));
}
private String batchKey(ShadowFactBatch batch) {
List<String> queues = new ArrayList<String>();
batch.getSourceQueues().forEach(q -> queues.add(q.getContentHash()));
List<String> vehicles = new ArrayList<String>();
batch.getVehicles().forEach(v -> vehicles.add(v.getContentHash()));
return ShadowEvidenceDigest.sha256(json(Arrays.asList(batch.getDatabaseCaptureStartedAt(),
batch.getDatabaseCapturedAt(), queues, vehicles, mapsKey(batch))));
}
private String mapsKey(ShadowFactBatch batch) { return ShadowEvidenceDigest.sha256(json(batch.getMaps())); }
private String json(Object value) { return ShadowEvidenceDigest.canonicalJson(mapper, value); }
private static boolean blank(String value) { return trim(value).isEmpty(); }
private static String trim(String value) { return value == null ? "" : value.trim(); }
private static boolean sameSite(String value, Integer site) {
try { return Integer.valueOf(value).equals(site); }
catch (NumberFormatException invalid) { return false; }
}
private static final class Prediction {
final String phaseKey;
final String queueHash;
final String sourceSite;
final UUID cycleId;
final String evidenceHash;
final String inputHash;
final Instant capturedAt;
final Instant completedAt;
final ShadowDecision decision;
final Ft546EncodedCommand command;
Prediction(String phaseKey, String queueHash, String sourceSite, UUID cycleId,
String evidenceHash, ShadowCycleEvidence evidence) {
this.phaseKey = phaseKey;
this.queueHash = queueHash;
this.sourceSite = sourceSite;
this.cycleId = cycleId;
this.evidenceHash = evidenceHash;
this.inputHash = evidence.getInputBatchHash();
this.capturedAt = evidence.getDatabaseCapturedAt();
this.completedAt = evidence.getCompletedAt();
this.decision = evidence.prediction();
this.command = evidence.command();
}
}
}

View File

@@ -44,6 +44,9 @@ public final class ShadowCycleEvidence {
private final List<Ft546RegisterWrite> encodedWrites;
private final String encodedHash;
private final List<ShadowDifference> differences;
// Kept only for bounded, post-commit pairing. Not part of the persisted payload or digest.
private final ShadowDecision prediction;
private final Ft546EncodedCommand command;
private ShadowCycleEvidence(UUID runId, ShadowFactBatch batch,
ShadowLegacyQueueResult legacy,
@@ -53,6 +56,8 @@ public final class ShadowCycleEvidence {
Instant completedAt,
ObjectMapper objectMapper) {
this.cycleId = UUID.randomUUID();
this.prediction = decision;
this.command = encoded;
this.runId = Objects.requireNonNull(runId, "runId");
this.parentTaskId = decision.getParentTaskId();
this.phase = decision.getPhase();
@@ -152,6 +157,8 @@ public final class ShadowCycleEvidence {
}
public String getEncodedHash() { return encodedHash; }
public List<ShadowDifference> getDifferences() { return differences; }
ShadowDecision prediction() { return prediction; }
Ft546EncodedCommand command() { return command; }
private static Map<String, Object> inputPayload(
ShadowFactBatch batch, ShadowDecision decision) {

View File

@@ -14,6 +14,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
@@ -26,6 +27,7 @@ public final class ShadowCycleEvidenceCollector {
private final ObjectMapper objectMapper;
private final LegacyQueueProtocolProjector legacyProjector;
private final ShadowComparisonService comparison;
private final DeferredShadowComparison deferred;
public ShadowCycleEvidenceCollector(UUID runId, long routeVersion,
ObjectMapper objectMapper) {
@@ -49,11 +51,19 @@ public final class ShadowCycleEvidenceCollector {
this.legacyProjector = Objects.requireNonNull(legacyProjector,
"legacyProjector");
this.comparison = Objects.requireNonNull(comparison, "comparison");
this.deferred = new DeferredShadowComparison(objectMapper, legacyProjector, comparison);
}
/** Pure evidence preview; persistent callers use prepare() and acknowledge the outer commit. */
public List<ShadowCycleEvidence> collect(
ShadowFactBatch batch, DispatchCycleResult decisions,
List<Ft546EncodedCommand> encodedCommands, Instant completedAt) {
return prepare(batch, decisions, encodedCommands, completedAt).getEvidence();
}
public CollectedEvidence prepare(
ShadowFactBatch batch, DispatchCycleResult decisions,
List<Ft546EncodedCommand> encodedCommands, Instant completedAt) {
Objects.requireNonNull(batch, "batch");
Objects.requireNonNull(decisions, "decisions");
Objects.requireNonNull(encodedCommands, "encodedCommands");
@@ -63,6 +73,7 @@ public final class ShadowCycleEvidenceCollector {
}
List<ShadowCycleEvidence> evidence =
new ArrayList<ShadowCycleEvidence>();
Map<String, ShadowDifference> deferredComparisons = deferred.prepare(batch);
for (int index = 0; index < decisions.getGranted().size(); index++) {
GrantedDispatch grant = decisions.getGranted().get(index);
Ft546EncodedCommand encoded = encodedCommands.get(index);
@@ -81,11 +92,39 @@ public final class ShadowCycleEvidenceCollector {
if (!queue.isActive()) continue;
String reason = com.ruoyi.shadow.facts.ShadowQueueDispatchEligibility.observationReason(queue, batch);
if (reason != null) {
evidence.add(complete(batch, legacy(queue, batch), ShadowDecision.observation(
parent(queue), phase(queue), routeVersion, queue.getAgvId(), reason), null, completedAt));
ShadowLegacyQueueResult legacy = legacy(queue, batch);
ShadowDecision observation = ShadowDecision.observation(
parent(queue), phase(queue), routeVersion, queue.getAgvId(), reason);
List<ShadowDifference> differences = new ArrayList<ShadowDifference>(comparison.compare(legacy, observation, null));
ShadowDifference paired = deferredComparisons.get(queue.getGuid());
if (paired != null) differences.add(paired);
evidence.add(ShadowCycleEvidence.complete(runId, batch, legacy, observation, null,
differences, completedAt, objectMapper));
}
}
return Collections.unmodifiableList(evidence);
return new CollectedEvidence(batch, Collections.unmodifiableList(evidence), deferredComparisons);
}
public void invalidatePendingComparisons() { deferred.clear(); }
public final class CollectedEvidence {
private final ShadowFactBatch batch;
private final List<ShadowCycleEvidence> evidence;
private final Map<String, ShadowDifference> comparisons;
private CollectedEvidence(ShadowFactBatch batch, List<ShadowCycleEvidence> evidence,
Map<String, ShadowDifference> comparisons) {
this.batch = batch;
this.evidence = evidence;
this.comparisons = comparisons;
}
public List<ShadowCycleEvidence> getEvidence() { return evidence; }
/** The caller must wait until its outer evidence transaction has committed. */
public void committed(List<ShadowEvidenceRepository.SavedCycle> saved) {
deferred.committed(batch, evidence, saved, comparisons);
}
}
public List<ShadowCycleEvidence> insufficient(

View File

@@ -12,5 +12,6 @@ public enum ShadowDifferenceCategory {
ALGORITHM_HOLD,
INSUFFICIENT_FACTS,
SOURCE_GAP,
OBSERVATION_ONLY
OBSERVATION_ONLY,
DEFERRED_COMPARISON
}

View File

@@ -3,6 +3,7 @@ package com.ruoyi.shadow.facts;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.TransactionTemplate;
import java.sql.PreparedStatement;
@@ -84,6 +85,9 @@ public final class SqlServerShadowFactReader {
throw failed(failure.getReasonCode(), failure);
} catch (DataAccessException failure) {
throw failed(classify(failure), failure);
} catch (TransactionException failure) {
// A failed commit/rollback cannot be treated as a completed query timeout.
throw failed("SOURCE_TRANSACTION_FAILED", failure);
} catch (RuntimeException failure) {
throw failed("SOURCE_READ_FAILED", failure);
} finally {

View File

@@ -18,6 +18,7 @@ import com.ruoyi.wcs.application.dispatch.DispatchSnapshot;
import java.time.Instant;
import java.time.Clock;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
@@ -110,14 +111,16 @@ public final class Ft546ShadowCycle {
return degrade(health.getReasonCode(), clock.instant());
}
try {
List<Runnable> afterCommit = new ArrayList<Runnable>();
progress.enter("TRANSACTION_BEGIN");
ShadowCycleResult result = evidenceRepository.inTransaction(
() -> {
ShadowCycleResult saved = runAndPersist(batch, now, progress);
ShadowCycleResult saved = runAndPersist(batch, now, progress, afterCommit);
progress.enter("TRANSACTION_COMMIT");
return saved;
});
progress.enter("RECORDING");
afterCommit.forEach(Runnable::run);
commandGateway.rememberAll(result.getEncodedCommands());
Instant completedAt = clock.instant();
Long lag = batch.getDatabaseCapturedAt() == null ? null
@@ -127,12 +130,13 @@ public final class Ft546ShadowCycle {
statusReport.markReady(completedAt, lag);
return result;
} catch (RuntimeException evidenceFailure) {
invalidatePendingComparisons();
return degrade(failureReason(evidenceFailure), clock.instant());
}
}
private ShadowCycleResult runAndPersist(ShadowFactBatch batch,
Instant now, ShadowCycleDiagnostics progress) {
Instant now, ShadowCycleDiagnostics progress, List<Runnable> afterCommit) {
progress.enter("VALIDATION");
ShadowFactValidation validation = validator.validate(batch, now);
if (validation.getStatus()
@@ -169,16 +173,18 @@ public final class Ft546ShadowCycle {
}
progress.enter("EVIDENCE_BUILD");
progress.dispatch(batch, snapshot, result.getDecisions());
List<ShadowCycleEvidence> evidence = evidenceCollector.collect(batch,
ShadowCycleEvidenceCollector.CollectedEvidence evidence = evidenceCollector.prepare(batch,
result.getDecisions(), encoded, clock.instant());
progress.enter("EVIDENCE_SAVE");
evidenceRepository.saveAll(evidence);
List<ShadowEvidenceRepository.SavedCycle> saved = evidenceRepository.saveAll(evidence.getEvidence());
afterCommit.add(() -> evidence.committed(saved));
return ShadowCycleResult.comparable(result.getDecisions(),
result.getIntents(), encoded);
}
private ShadowCycleResult persistInsufficient(
ShadowFactBatch batch, String reasonCode, ShadowCycleDiagnostics progress) {
invalidatePendingComparisons();
progress.enter("EVIDENCE_BUILD");
List<ShadowCycleEvidence> evidence = evidenceCollector.insufficient(batch,
reasonCode, clock.instant());
@@ -188,6 +194,7 @@ public final class Ft546ShadowCycle {
}
private ShadowCycleResult degrade(String reasonCode, Instant now) {
invalidatePendingComparisons();
degradedReason.compareAndSet(null, reasonCode);
String stoppedReason = degradedReason.get();
statusReport.markDegraded(stoppedReason, now);
@@ -209,6 +216,10 @@ public final class Ft546ShadowCycle {
return "EVIDENCE_STORAGE_UNAVAILABLE";
}
public void invalidatePendingComparisons() {
if (evidenceCollector != null) evidenceCollector.invalidatePendingComparisons();
}
private ShadowCycleResult runWithoutEvidence(ShadowFactBatch batch,
Instant now, ShadowCycleDiagnostics progress) {
progress.enter("VALIDATION");

View File

@@ -114,6 +114,7 @@ public final class Ft546ShadowScheduler implements AutoCloseable {
}
public boolean runOnce() {
long startedNanos = System.nanoTime();
Instant now = clock.instant();
if (permanentlyStopped.get() || now.isBefore(nextAttemptAt)
|| !inFlight.compareAndSet(false, true)) {
@@ -121,6 +122,7 @@ public final class Ft546ShadowScheduler implements AutoCloseable {
}
ShadowCycleDiagnostics diagnostics = new ShadowCycleDiagnostics();
lastAttemptAt = now;
diagnostics.enter("RUNTIME_PUBLICATION");
if (!publishRuntimeState()) {
inFlight.set(false);
return false;
@@ -130,9 +132,13 @@ public final class Ft546ShadowScheduler implements AutoCloseable {
Future<CycleAttempt> future = null;
try {
if (permanentlyStopped.get()) return false;
future = cycleExecutor.submit(() -> executeCycle(now, diagnostics));
CycleAttempt attempt = future.get(cycleDeadline.toMillis(),
TimeUnit.MILLISECONDS);
Instant overallDeadline = now.plus(cycleDeadline);
long remainingNanos = Math.min(cycleDeadline.toNanos() - (System.nanoTime() - startedNanos),
Duration.between(clock.instant(), overallDeadline).toNanos());
if (remainingNanos <= 0L) throw new TimeoutException("runtime publication exhausted cycle budget");
diagnostics.enter("QUEUED");
future = cycleExecutor.submit(() -> executeCycle(overallDeadline, diagnostics));
CycleAttempt attempt = future.get(remainingNanos, TimeUnit.NANOSECONDS);
outcome = attempt.result.getStatusCode();
statusReport.recordCycleDiagnostics(diagnostics.finish(outcome, null));
if ("DEGRADED".equals(attempt.result.getStatusCode())) {
@@ -212,32 +218,37 @@ public final class Ft546ShadowScheduler implements AutoCloseable {
}
}
private CycleAttempt executeCycle(Instant startedAt, ShadowCycleDiagnostics diagnostics) {
private CycleAttempt executeCycle(Instant overallDeadline, ShadowCycleDiagnostics diagnostics) {
cycleWorker = Thread.currentThread();
activeStage = CycleStage.SOURCE_READ;
diagnostics.enter("SOURCE_READ");
ShadowFactBatch batch = factReader.read(cursor,
startedAt.plus(sourceReadDeadline));
Instant sourceDeadline = clock.instant().plus(sourceReadDeadline);
if (sourceDeadline.isAfter(overallDeadline)) sourceDeadline = overallDeadline;
ShadowFactBatch batch = factReader.read(cursor, sourceDeadline);
activeStage = CycleStage.SHADOW_CYCLE;
return new CycleAttempt(batch,
shadowCycle.run(batch, clock.instant(), diagnostics));
}
private boolean handleFailure(Throwable failure) {
shadowCycle.invalidatePendingComparisons();
if (failure instanceof ShadowFactReadException) {
ShadowFactReadException readFailure =
(ShadowFactReadException) failure;
String timeoutReason = sourceTimeoutReason(readFailure);
if (timeoutReason != null) {
return stopPermanently(timeoutReason, readFailure,
CycleStage.SOURCE_READ);
if ("SOURCE_TRANSACTION_FAILED".equals(readFailure.getReasonCode())
|| "SOURCE_CYCLE_ALREADY_IN_FLIGHT".equals(readFailure.getReasonCode())) {
return stopPermanently(readFailure.getReasonCode(), readFailure, CycleStage.SOURCE_READ);
}
// ExecutionException is delivered only after the reader and its transaction have unwound.
// A watchdog timeout still stops permanently because cancellation does not prove termination.
String timeoutReason = sourceTimeoutReason(readFailure);
String reason = timeoutReason == null ? readFailure.getReasonCode() : timeoutReason;
Instant failedAt = clock.instant();
statusReport.markDegraded(readFailure.getReasonCode(), failedAt);
nextAttemptAt = failedAt.plus(readFailure.getRetryAfter());
Duration backoff = readFailure.getRetryAfter().isZero() ? CRASH_BACKOFF : readFailure.getRetryAfter();
statusReport.markDegraded(reason, failedAt);
nextAttemptAt = failedAt.plus(backoff);
LOGGER.warn("FT546 shadow source read failed: {}; retry after {} ms",
readFailure.getReasonCode(),
readFailure.getRetryAfter().toMillis());
reason, backoff.toMillis());
if (readFailure.getCause() != null) {
LOGGER.warn("FT546 shadow source read root cause",
readFailure.getCause());
@@ -278,6 +289,7 @@ public final class Ft546ShadowScheduler implements AutoCloseable {
private boolean stopPermanently(String reasonCode, Exception failure,
CycleStage stage) {
shadowCycle.invalidatePendingComparisons();
permanentlyStopped.set(true);
nextAttemptAt = Instant.EPOCH;
statusReport.markPermanentlyDegraded(reasonCode, clock.instant());

View File

@@ -14,7 +14,7 @@ import java.util.TreeMap;
/** Identifies this source revision and the nonsecret dispatch/storage settings, not a built artifact. */
public final class ShadowRuntimeIdentity {
public static final String BUILD_ID = "ft546-shadow-20260908-phase-storage-v2";
public static final String BUILD_ID = "ft546-shadow-20260909-recovery-pairing-v3";
private ShadowRuntimeIdentity() { }
public static Map<String, Object> describe(ShadowProperties properties, Environment env) {

View File

@@ -139,7 +139,7 @@ class Ft546ShadowApplicationTest {
.andExpect(jsonPath("$.status").value("STARTING"))
.andExpect(jsonPath("$.checkedAt").exists())
.andExpect(jsonPath("$.evidenceBacklog").value(0))
.andExpect(jsonPath("$.runtimeIdentity.buildId").value("ft546-shadow-20260908-phase-storage-v2"))
.andExpect(jsonPath("$.runtimeIdentity.buildId").value("ft546-shadow-20260909-recovery-pairing-v3"))
.andExpect(jsonPath("$.runtimeIdentity.effectiveSettingsSha256").exists())
.andExpect(jsonPath("$.runtimeIdentity.safePointConfiguration").value("UNCONFIGURED"))
.andExpect(jsonPath("$.startupChecks.SHADOW_ENABLED")

View File

@@ -116,7 +116,7 @@ class Ft546ShadowFaultIsolationTest {
}
@Test
void retryAndPermanentFailurePublishDifferentCollectionStates() {
void completedSourceFailuresPublishRetryableCollectionState() {
for (boolean timeout : new boolean[] {false, true}) {
MutableClock clock = new MutableClock(NOW);
AtomicInteger calls = new AtomicInteger();
@@ -137,7 +137,7 @@ class Ft546ShadowFaultIsolationTest {
assertFalse(scheduler.runOnce());
Map<String, Object> failed = heartbeats.get(heartbeats.size() - 1);
assertEquals("DEGRADED", failed.get("status"));
assertEquals(Boolean.valueOf(timeout), failed.get("collectionStopped"));
assertEquals(Boolean.FALSE, failed.get("collectionStopped"));
assertEquals(null, failed.get("lastCompletedCycle"));
clock.advance(Duration.ofMillis(100));
scheduler.runScheduledCycle();
@@ -172,7 +172,7 @@ class Ft546ShadowFaultIsolationTest {
}
@Test
void sourceQueryTimeoutPermanentlyStopsCollectionBeforeCycle() {
void completedSourceQueryTimeoutBacksOffWithoutRunningCycle() {
MutableClock clock = new MutableClock(NOW);
AtomicInteger sourceCalls = new AtomicInteger();
SqlServerShadowFactReader reader = reader(timeoutSource(sourceCalls), clock);
@@ -197,17 +197,18 @@ class Ft546ShadowFaultIsolationTest {
clock.advance(Duration.ofMinutes(1));
assertFalse(scheduler.runOnce());
assertEquals(1, sourceCalls.get());
assertEquals(2, sourceCalls.get());
assertEquals(0, evidence.transactionCalls.get());
assertEquals(Collections.emptyList(), gateway.getRecorded());
assertEquals(ShadowStartupReport.Status.DEGRADED,
report.getStatus());
assertEquals("SOURCE_QUERY_TIMEOUT", report.getReasonCode());
assertTrue(scheduler.isPermanentlyStopped());
assertFalse(scheduler.isPermanentlyStopped());
assertEquals(Duration.ofSeconds(2), scheduler.getRemainingBackoff());
assertTrue(logAppender.list.stream().anyMatch(event ->
event.getFormattedMessage()
.contains("stage=SOURCE_READ")),
"source timeout log must identify the blocked stage");
.contains("retry after")),
"completed source timeout must report the retry delay");
} finally {
schedulerLogger.detachAppender(logAppender);
logAppender.stop();
@@ -216,7 +217,7 @@ class Ft546ShadowFaultIsolationTest {
}
@Test
void sourceBatchDeadlinePermanentlyStopsCollection() {
void completedSourceBatchDeadlineBacksOff() {
MutableClock clock = new MutableClock(NOW);
AtomicInteger sourceCalls = new AtomicInteger();
SqlServerShadowFactReader.FactSource source = new FactSourceAdapter() {
@@ -241,17 +242,18 @@ class Ft546ShadowFaultIsolationTest {
clock.advance(Duration.ofMinutes(1));
assertFalse(scheduler.runOnce());
assertEquals(1, sourceCalls.get());
assertEquals(2, sourceCalls.get());
assertEquals("SOURCE_BATCH_DEADLINE_EXCEEDED",
report.getReasonCode());
assertTrue(scheduler.isPermanentlyStopped());
assertFalse(scheduler.isPermanentlyStopped());
assertEquals(Duration.ofSeconds(2), scheduler.getRemainingBackoff());
} finally {
scheduler.close();
}
}
@Test
void wrappedSourceQueryTimeoutPermanentlyStopsCollection() {
void completedWrappedSourceQueryTimeoutBacksOff() {
MutableClock clock = new MutableClock(NOW);
AtomicInteger sourceCalls = new AtomicInteger();
SqlServerShadowFactReader.FactSource source = new FactSourceAdapter() {
@@ -277,9 +279,10 @@ class Ft546ShadowFaultIsolationTest {
clock.advance(Duration.ofMinutes(1));
assertFalse(scheduler.runOnce());
assertEquals(1, sourceCalls.get());
assertEquals(2, sourceCalls.get());
assertEquals("SOURCE_QUERY_TIMEOUT", report.getReasonCode());
assertTrue(scheduler.isPermanentlyStopped());
assertFalse(scheduler.isPermanentlyStopped());
assertEquals(Duration.ofSeconds(2), scheduler.getRemainingBackoff());
} finally {
scheduler.close();
}
@@ -511,6 +514,39 @@ class Ft546ShadowFaultIsolationTest {
Ft546ShadowFaultIsolationTest::isOutboundEndpointKey));
}
@Test
void sourceWorkerThatIgnoresCancellationIsNeverRetried() {
MutableClock clock = new MutableClock(NOW);
AtomicInteger calls = new AtomicInteger();
CountDownLatch release = new CountDownLatch(1);
SqlServerShadowFactReader.FactSource source = new FactSourceAdapter() {
@Override public Instant readDatabaseClock(Instant deadline) {
calls.incrementAndGet();
boolean interrupted = false;
while (release.getCount() != 0) {
try { release.await(); }
catch (InterruptedException ignored) { interrupted = true; }
}
if (interrupted) Thread.currentThread().interrupt();
return clock.instant();
}
};
ShadowStartupReport report = report();
try (Ft546ShadowScheduler scheduler = new Ft546ShadowScheduler(reader(source, clock),
evidenceCycle(report, new RecordingEvidenceRepository(), gateway(),
() -> ShadowEvidenceStorageHealth.Check.available(Long.MAX_VALUE)),
report, clock, Duration.ofSeconds(1))) {
assertFalse(scheduler.runOnce());
assertEquals("SHADOW_CYCLE_TIMEOUT", report.getReasonCode());
assertTrue(scheduler.isPermanentlyStopped());
clock.advance(Duration.ofMinutes(1));
assertFalse(scheduler.runOnce());
assertEquals(1, calls.get());
} finally {
release.countDown();
}
}
@Test
void longCycleDeadlineKeepsSourceReadDeadlineCappedAtFourSeconds() {
MutableClock clock = new MutableClock(NOW);
@@ -578,6 +614,93 @@ class Ft546ShadowFaultIsolationTest {
assertEquals("", observed.get(1).getLastGuid());
}
@Test
void completedTimeoutRecoversOnlyAfterBackoffAndResetsFailureCount() {
MutableClock clock = new MutableClock(NOW);
AtomicInteger calls = new AtomicInteger();
ShadowStartupReport report = report();
SqlServerShadowFactReader.FactSource source = new FactSourceAdapter() {
@Override public Instant readDatabaseClock(Instant deadline) {
if (calls.incrementAndGet() == 1) throw new QueryTimeoutException("completed timeout");
return clock.instant();
}
};
try (Ft546ShadowScheduler scheduler = new Ft546ShadowScheduler(reader(source, clock),
evidenceCycle(report, new RecordingEvidenceRepository(), gateway(),
() -> ShadowEvidenceStorageHealth.Check.available(Long.MAX_VALUE)), report, clock)) {
assertFalse(scheduler.runOnce());
assertFalse(scheduler.runOnce());
assertEquals(1, calls.get());
clock.advance(Duration.ofSeconds(1));
assertTrue(scheduler.runOnce());
assertEquals(3, calls.get());
assertEquals(ShadowStartupReport.Status.READY, report.getStatus());
assertEquals(null, report.getReasonCode());
assertFalse(scheduler.isPermanentlyStopped());
}
}
@Test
void failedSourceRollbackPermanentlyStopsEvenWhenOriginalCauseWasTimeout() {
MutableClock clock = new MutableClock(NOW);
AtomicInteger calls = new AtomicInteger();
DriverManagerDataSource dataSource = new DriverManagerDataSource(
"jdbc:h2:mem:" + UUID.randomUUID(), "sa", "");
DataSourceTransactionManager transactions = new DataSourceTransactionManager(dataSource) {
@Override protected void doRollback(
org.springframework.transaction.support.DefaultTransactionStatus status) {
super.doRollback(status);
throw new org.springframework.transaction.TransactionSystemException("rollback failed");
}
};
SqlServerShadowFactReader source = new SqlServerShadowFactReader(timeoutSource(calls),
new TransactionTemplate(transactions), clock);
ShadowStartupReport report = report();
try (Ft546ShadowScheduler scheduler = new Ft546ShadowScheduler(source,
evidenceCycle(report, new RecordingEvidenceRepository(), gateway(),
() -> ShadowEvidenceStorageHealth.Check.available(Long.MAX_VALUE)), report, clock)) {
assertFalse(scheduler.runOnce());
assertEquals("SOURCE_TRANSACTION_FAILED", report.getReasonCode());
assertTrue(scheduler.isPermanentlyStopped());
clock.advance(Duration.ofMinutes(1));
assertFalse(scheduler.runOnce());
assertEquals(1, calls.get());
}
}
@Test
void sourceBudgetStartsAfterRuntimePublicationAndIsCappedByWholeCycle() {
for (long publicationMillis : new long[] {3230L, 7000L, 11000L}) {
MutableClock clock = new MutableClock(NOW);
AtomicInteger publications = new AtomicInteger();
AtomicReference<Instant> deadlineSeen = new AtomicReference<Instant>();
SqlServerShadowFactReader.FactSource source = new FactSourceAdapter() {
@Override public Instant readDatabaseClock(Instant deadline) {
deadlineSeen.set(deadline);
return clock.instant();
}
};
ShadowStartupReport report = report();
try (Ft546ShadowScheduler scheduler = new Ft546ShadowScheduler(reader(source, clock),
evidenceCycle(report, new RecordingEvidenceRepository(), gateway(),
() -> ShadowEvidenceStorageHealth.Check.available(Long.MAX_VALUE)),
report, clock, Duration.ofSeconds(10), state -> {
if (publications.incrementAndGet() == 1) clock.advance(Duration.ofMillis(publicationMillis));
})) {
if (publicationMillis > 10000L) {
assertFalse(scheduler.runOnce());
assertEquals(null, deadlineSeen.get());
assertEquals("SHADOW_CYCLE_TIMEOUT", report.getReasonCode());
} else {
assertTrue(scheduler.runOnce());
assertEquals(NOW.plusMillis(Math.min(publicationMillis + 4000L, 10000L)), deadlineSeen.get());
Map<?, ?> phases = (Map<?, ?>) report.getCycleDiagnostics().get("phaseMillis");
assertTrue(phases.containsKey("RUNTIME_PUBLICATION"));
}
}
}
}
private static SqlServerShadowFactReader reader(
SqlServerShadowFactReader.FactSource source, Clock clock) {
DriverManagerDataSource dataSource = new DriverManagerDataSource(

View File

@@ -285,6 +285,33 @@ class JdbcShadowEvidenceRepositoryTest {
assertEquals(1, count("shadow_cycle"));
}
@Test
void deferredComparisonPersistsAlongsideObservationUsingExistingSchema() {
ShadowCycleEvidence prediction = matchingCycle();
ShadowEvidenceRepository.SavedCycle pending = repository.save(prediction);
Map<String, Object> trace = new LinkedHashMap<String, Object>();
trace.put("predictionCycleId", pending.getCycleId());
trace.put("predictionEvidenceHash", pending.getEvidenceHash());
trace.put("predictionInputHash", prediction.getInputBatchHash());
String actual = ShadowEvidenceDigest.canonicalJson(objectMapper, trace);
ShadowCycleEvidence observation = ShadowCycleEvidence.complete(run.getRunId(),
ShadowFactBatch.create(NOW.plusSeconds(5), NOW.plusSeconds(5),
Collections.emptyList(), Collections.emptyList(), Collections.emptyList()), legacyFields(),
ShadowDecision.observation("PARENT-1", "LOADED_TO_PLACE", 3L, "VEH-1", "QUEUE_PHASE_NOT_PENDING"),
null, Arrays.asList(new ShadowDifference(ShadowDifferenceCategory.OBSERVATION_ONLY,
"QUEUE_PHASE_NOT_PENDING", null, null), new ShadowDifference(ShadowDifferenceCategory.DEFERRED_COMPARISON,
"DEFERRED_MATCH", "{}", actual)), NOW.plusSeconds(5), objectMapper);
repository.save(observation);
assertEquals(actual, jdbc.queryForObject("SELECT actual_json FROM shadow_difference WHERE cycle_id=? AND category=?",
String.class, observation.getCycleId(), "DEFERRED_COMPARISON"));
assertEquals(0, jdbc.queryForObject("SELECT COUNT(*) FROM shadow_encoded_write WHERE cycle_id=?",
Integer.class, observation.getCycleId()));
assertEquals(pending.getEvidenceHash(), repository.findCycle(run.getRunId(), prediction.getInputBatchHash(),
prediction.getParentTaskId(), prediction.getPhase(), prediction.getRouteVersion()).getEvidenceHash());
assertEquals(Long.valueOf(1), repository.dailyDigest(LocalDate.of(2026, 8, 14), ZoneOffset.UTC)
.getCategoryCounts().get(ShadowDifferenceCategory.DEFERRED_COMPARISON));
}
@Test
void sameIdentityWithDifferentHashFailsClosed() {
ShadowCycleEvidence first = matchingCycle();

View File

@@ -0,0 +1,311 @@
package com.ruoyi.shadow.evidence;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.ft546.protocol.*;
import com.ruoyi.shadow.facts.*;
import com.ruoyi.shadow.kernel.*;
import com.ruoyi.shadow.startup.ShadowStartupReport;
import com.ruoyi.wcs.application.ReadOnlyDispatchCycle;
import com.ruoyi.wcs.application.dispatch.DispatchCoordinator;
import com.ruoyi.wcs.domain.dispatch.DefaultVehicleAssignmentPolicy;
import com.ruoyi.wcs.domain.traffic.*;
import com.ruoyi.wcs.port.out.DispatchPolicy;
import org.junit.jupiter.api.Test;
import java.time.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.*;
class ShadowDeferredComparisonTest {
private static final Instant NOW = Instant.parse("2026-09-09T01:00:00Z");
private static final ObjectMapper JSON = new ObjectMapper().findAndRegisterModules();
@Test
void committedPendingPredictionPairsWithLaterIssuedPhaseWithoutChangingOldEvidence() throws Exception {
Harness h = new Harness();
ShadowCycleEvidence pending = h.run(0, queue(0, null));
String originalHash = pending.getEvidenceHash();
assertEquals("GRANTED", pending.getDecision());
assertEquals("SOURCE_GAP", pending.getDifferences().get(0).getCategory().name());
ShadowCycleEvidence observed = h.run(5, queue(1, "70501,70502"));
ShadowDifference paired = deferred(observed);
assertEquals("DEFERRED_MATCH", paired.getReasonCode());
JsonNode trace = JSON.readTree(paired.getActualJson());
assertEquals(pending.getCycleId().toString(), trace.get("predictionCycleId").asText());
assertEquals(pending.getInputBatchHash(), trace.get("predictionInputHash").asText());
assertEquals(originalHash, trace.get("predictionEvidenceHash").asText());
assertEquals("OBSERVATION_ONLY", observed.getDecision());
assertTrue(observed.getEncodedWrites().isEmpty());
assertEquals(originalHash, pending.getEvidenceHash());
assertEquals(1, h.gateway.getRecorded().size());
assertNull(deferredOrNull(h.run(10, queue(1, "70501,70502"))));
}
@Test
void usesLastCommittedPredictionAndActualPersistedId() throws Exception {
Harness h = new Harness();
h.run(0, queue(0, null));
h.repository.returnedId = UUID.randomUUID();
ShadowCycleEvidence last = h.run(5, queue(0, null));
UUID persisted = h.repository.returnedId;
h.repository.returnedId = null;
JsonNode trace = JSON.readTree(deferred(h.run(10, queue(1, "70501,70502"))).getActualJson());
assertEquals(persisted.toString(), trace.get("predictionCycleId").asText());
assertEquals(last.getEvidenceHash(), trace.get("predictionEvidenceHash").asText());
}
@Test
void changedRouteIsADifferenceInsteadOfFalseMatch() {
Harness h = new Harness();
h.run(0, queue(0, null));
ShadowQueueFact changed = builder(1, "70501,70502").turnPath("70503").build();
ShadowDifference difference = deferred(h.run(5, changed));
assertEquals("DEFERRED_DIFFERENCE", difference.getReasonCode());
assertTrue(difference.getActualJson().contains("ALGORITHM_ROUTE"));
}
@Test
void neverMatchesChangedPhaseTaskIdentityVehicleOrSpecialHandoff() {
List<ShadowQueueFact> changed = Arrays.asList(
builder(1, "70501,70502").taskType("1").build(),
builder(1, "70501,70502").taskNo("30002").build(),
builder(1, "70501,70502").createdAt(NOW.minusSeconds(1)).build(),
builder(1, "70501,70502").agvId("VEH-8").build(),
builder(1, "70501,70502").startSite("70503").build(),
builder(1, "70501,70502").default02("1").build(),
builder(1, "70501,70502").default08("2").build());
for (ShadowQueueFact row : changed) {
Harness h = new Harness();
h.run(0, queue(0, null));
ShadowDifference result = deferredOrNull(h.run(5, row));
assertTrue(result == null || "DEFERRED_UNAVAILABLE".equals(result.getReasonCode()), row.getContentHash());
}
}
@Test
void missingOrStaleLegacyPathRemainsUnavailable() {
for (String path : new String[] {null, "70502,70501"}) {
Harness h = new Harness();
h.run(0, queue(0, null));
ShadowDifference result = deferred(h.run(5, queue(1, path)));
assertEquals("DEFERRED_UNAVAILABLE", result.getReasonCode());
}
}
@Test
void noPredictionAcrossRestartMissingPollExpiredIntervalOrInvalidBatch() {
Harness fresh = new Harness();
assertNull(deferredOrNull(fresh.run(5, queue(1, "70501,70502"))));
Harness expired = new Harness();
expired.run(0, queue(0, null));
assertNull(deferredOrNull(expired.run(120, queue(1, "70501,70502"))));
Harness gap = new Harness();
gap.run(0, queue(0, null));
gap.runBatch(gap.batch(5, Collections.emptyList(), true));
assertNull(deferredOrNull(gap.run(10, queue(1, "70501,70502"))));
Harness invalid = new Harness();
invalid.run(0, queue(0, null));
invalid.runBatch(invalid.batch(5, Collections.singletonList(queue(0, null)), false));
assertNull(deferredOrNull(invalid.run(10, queue(1, "70501,70502"))));
}
@Test
void failedOuterCommitCannotCreateAPairablePrediction() {
Harness h = new Harness();
h.repository.failCommit = true;
assertEquals("DEGRADED", h.runBatch(h.batch(0, Collections.singletonList(queue(0, null)), true)).getStatusCode());
h.repository.failCommit = false;
// Reuse the collector deliberately: even an in-process reconstruction must not retain a rolled-back prediction.
h.cycle = h.newCycle();
assertNull(deferredOrNull(h.run(5, queue(1, "70501,70502"))));
}
@Test
void replayingSameObservedBatchKeepsIdenticalEvidenceHash() {
Harness h = new Harness();
h.run(0, queue(0, null));
ShadowCycleEvidence first = h.run(5, queue(1, "70501,70502"));
assertNotNull(deferred(first));
ShadowCycleEvidence replay = h.run(5, queue(1, "70501,70502"));
assertEquals(first.getEvidenceHash(), replay.getEvidenceHash());
}
@Test
void sourceTimeoutBreaksPairingBeforeSuccessfulRecovery() {
Harness h = new Harness();
AtomicReference<ShadowFactBatch> facts = new AtomicReference<ShadowFactBatch>(
h.batch(0, Collections.singletonList(queue(0, null)), true));
java.util.concurrent.atomic.AtomicBoolean fail = new java.util.concurrent.atomic.AtomicBoolean();
SqlServerShadowFactReader.FactSource source = new SqlServerShadowFactReader.FactSource() {
public Instant readDatabaseClock(Instant deadline) {
if (fail.get()) throw new org.springframework.dao.QueryTimeoutException("completed timeout");
return facts.get().getDatabaseCapturedAt();
}
public List<ShadowQueueFact> readQueues(ShadowReadCursor cursor, Instant deadline) { return facts.get().getSourceQueues(); }
public List<ShadowVehicleFact> readVehicles(Instant deadline) { return facts.get().getVehicles(); }
public List<ShadowMapFact> readMaps(Instant deadline) { return facts.get().getMaps(); }
};
org.springframework.jdbc.datasource.DriverManagerDataSource dataSource =
new org.springframework.jdbc.datasource.DriverManagerDataSource("jdbc:h2:mem:" + UUID.randomUUID(), "sa", "");
SqlServerShadowFactReader reader = new SqlServerShadowFactReader(source,
new org.springframework.transaction.support.TransactionTemplate(
new org.springframework.jdbc.datasource.DataSourceTransactionManager(dataSource)), h.clock);
ShadowStartupReport status = ShadowStartupReport.starting(NOW, Collections.emptyMap());
try (Ft546ShadowScheduler scheduler = new Ft546ShadowScheduler(reader, h.cycle, status, h.clock)) {
assertTrue(scheduler.runOnce());
fail.set(true);
h.now.set(NOW.plusSeconds(5));
assertFalse(scheduler.runOnce());
fail.set(false);
h.now.set(NOW.plusSeconds(10));
facts.set(h.batch(10, Collections.singletonList(queue(1, "70501,70502")), true));
assertTrue(scheduler.runOnce());
assertNull(deferredOrNull(h.repository.saved.get(h.repository.saved.size() - 1)));
}
}
@Test
void changedMapAndDuplicateQueueObservationsCannotMatch() {
Harness h = new Harness();
h.run(0, queue(0, null));
ShadowFactBatch observed = h.batch(5, Collections.singletonList(queue(1, "70501,70502")), true);
ShadowFactBatch changedMap = ShadowFactBatch.create(observed.getDatabaseCaptureStartedAt(),
observed.getDatabaseCapturedAt(), observed.getSourceQueues(), observed.getVehicles(),
Arrays.asList(new ShadowMapFact("MAP-1", "70501", 0, 0, 1, 1),
new ShadowMapFact("MAP-2", "70502", 2, 0, 1, 1)));
List<ShadowCycleEvidence> evidence = h.collector.collect(changedMap,
com.ruoyi.wcs.application.dispatch.DispatchCycleResult.committed(Collections.emptyList(),
Collections.emptyList(), Collections.emptyList(), Collections.emptyList()),
Collections.emptyList(), NOW.plusSeconds(5));
assertNull(deferredOrNull(evidence.get(0)));
ShadowFactBatch duplicate = h.batch(5, Arrays.asList(queue(1, "70501,70502"), queue(1, "70501,70502")), true);
evidence = h.collector.collect(duplicate,
com.ruoyi.wcs.application.dispatch.DispatchCycleResult.committed(Collections.emptyList(),
Collections.emptyList(), Collections.emptyList(), Collections.emptyList()),
Collections.emptyList(), NOW.plusSeconds(5));
for (ShadowCycleEvidence value : evidence) assertNull(deferredOrNull(value));
}
@Test
void preparingSameObservationBeforeCommitIsDeterministicAndDoesNotConsumePrediction() {
Harness h = new Harness();
h.run(0, queue(0, null));
ShadowFactBatch observed = h.batch(5, Collections.singletonList(queue(1, "70501,70502")), true);
com.ruoyi.wcs.application.dispatch.DispatchCycleResult noDispatch =
com.ruoyi.wcs.application.dispatch.DispatchCycleResult.committed(Collections.emptyList(),
Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
ShadowCycleEvidence first = h.collector.collect(observed, noDispatch, Collections.emptyList(), NOW.plusSeconds(5)).get(0);
ShadowCycleEvidence retry = h.collector.collect(observed, noDispatch, Collections.emptyList(), NOW.plusSeconds(6)).get(0);
assertNotNull(deferred(first));
assertNotNull(deferred(retry));
assertEquals(first.getEvidenceHash(), retry.getEvidenceHash());
assertEquals(first.getEvidenceHash(), h.run(5, queue(1, "70501,70502")).getEvidenceHash());
}
private static ShadowDifference deferred(ShadowCycleEvidence value) {
ShadowDifference result = deferredOrNull(value);
assertNotNull(result, "same-stage pending->issued observation needs traced comparison evidence");
return result;
}
private static ShadowDifference deferredOrNull(ShadowCycleEvidence value) {
return value.getDifferences().stream().filter(d -> "DEFERRED_COMPARISON".equals(d.getCategory().name()))
.findFirst().orElse(null);
}
private static ShadowQueueFact queue(int issued, String path) { return builder(issued, path).build(); }
private static ShadowQueueFact.Builder builder(int issued, String path) {
return ShadowQueueFact.builder("shadow_ro.v_wcs_srgv_queue", "ACTIVE")
.issued(issued).executionStatus(issued == 0 ? 1 : 3)
.taskNo("30001").taskType("2").priority(2L).theFloor("1")
.cargoPickSite("70501").cargoPlaceSite("70502").startSite("70501").endSite("70502")
.createdAt(NOW.minusSeconds(10)).agvId("VEH-9").pathInfo(path).default02("2");
}
private static final class Harness {
final Repository repository = new Repository();
final ShadowCycleEvidenceCollector collector = new ShadowCycleEvidenceCollector(UUID.randomUUID(), 3L, JSON);
final AtomicReference<Instant> now = new AtomicReference<Instant>(NOW);
final Clock clock = new Clock() {
public ZoneId getZone() { return ZoneOffset.UTC; }
public Clock withZone(ZoneId zone) { return this; }
public Instant instant() { return now.get(); }
};
final RecordingFt546CommandGateway gateway;
Ft546ShadowCycle cycle;
Harness() {
Ft546DeviceProfile profile = new Ft546DeviceProfile();
Map<String, String> registers = new LinkedHashMap<String, String>();
registers.put("taskNo", "4000"); registers.put("taskType", "4002");
registers.put("path", "4004"); registers.put("trigger", "4021");
profile.setRegisters(registers);
Set<String> capabilities = Collections.singleton("POINT_TO_POINT_COMMAND");
profile.setCapabilities(capabilities);
gateway = new RecordingFt546CommandGateway(new Ft546SrgvCommandEncoder(), profile, capabilities);
cycle = newCycle();
}
Ft546ShadowCycle newCycle() {
RoutePlanner routes = new AStarRoutePlanner();
DispatchCoordinator coordinator = new DispatchCoordinator(
new ShadowVehicleAssignmentPolicy(new DefaultVehicleAssignmentPolicy(routes, DispatchPolicy.NONE)),
new DeadlockGuard(new WaitForGraph(), routes), commit -> true, () -> UUID.randomUUID().toString());
ShadowDispatchSnapshotAssembler assembler = new ShadowDispatchSnapshotAssembler("FT546", 3L,
topology -> new ShadowTrafficState(new ResourceLedger(topology), new WaitForGraph(), 0L,
Collections.emptyMap()), clock);
return new Ft546ShadowCycle(new ShadowFactBatchValidator(Duration.ofSeconds(4), Duration.ofSeconds(30)),
assembler::assemble, new ReadOnlyDispatchCycle<Ft546SrgvCommandIntent>(coordinator::dispatch,
new ShadowCommandIntentFactory()::create), gateway,
() -> ShadowEvidenceStorageHealth.Check.available(Long.MAX_VALUE), repository, collector,
ShadowStartupReport.starting(now.get(), Collections.emptyMap()), clock);
}
ShadowFactBatch batch(long seconds, List<ShadowQueueFact> queues, boolean vehicles) {
Instant at = NOW.plusSeconds(seconds);
return ShadowFactBatch.create(at.minusMillis(10), at, queues,
vehicles ? Collections.singletonList(ShadowVehicleFact.builder("VEH-GUID", "VEH-9")
.agvStatus("1").currentSite("70501").battery(80).workMode(5).alarm(0).status(1)
.errorCode(0).allowSend(1).currentFloor(1).capturedAt(at).build()) : Collections.emptyList(),
Arrays.asList(new ShadowMapFact("MAP-1", "70501", 0, 0, 1, 1),
new ShadowMapFact("MAP-2", "70502", 1, 0, 1, 1)));
}
ShadowCycleResult runBatch(ShadowFactBatch batch) {
now.set(batch.getDatabaseCapturedAt());
return cycle.run(batch, now.get());
}
ShadowCycleEvidence run(long seconds, ShadowQueueFact queue) {
assertEquals("COMPARABLE", runBatch(batch(seconds, Collections.singletonList(queue), true)).getStatusCode());
return repository.saved.get(repository.saved.size() - 1);
}
}
private static final class Repository implements ShadowEvidenceRepository {
final List<ShadowCycleEvidence> saved = new ArrayList<ShadowCycleEvidence>();
boolean failCommit;
UUID returnedId;
public SavedCycle save(ShadowCycleEvidence value) { return saveAll(Collections.singletonList(value)).get(0); }
public List<SavedCycle> saveAll(List<ShadowCycleEvidence> values) {
saved.addAll(values);
List<SavedCycle> result = new ArrayList<SavedCycle>();
for (ShadowCycleEvidence value : values) result.add(new SavedCycle(
returnedId == null ? value.getCycleId() : returnedId,
value.getEvidenceHash(), value.getStatus(), value.getReasonCode()));
return result;
}
public <T> T inTransaction(Supplier<T> work) {
T result = work.get();
if (failCommit) throw new ShadowEvidenceException("EVIDENCE_WRITE_FAILED");
return result;
}
public SavedCycle findCycle(UUID run, String hash, String parent, String phase, long version) { return null; }
public ShadowEvidenceDigest dailyDigest(LocalDate date, ZoneId zone) { throw new UnsupportedOperationException(); }
public boolean isHealthy() { return true; }
}
}

View File

@@ -0,0 +1,91 @@
package com.ruoyi.shadow.kernel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.ft546.protocol.*;
import com.ruoyi.shadow.evidence.*;
import com.ruoyi.shadow.facts.*;
import com.ruoyi.wcs.application.ReadOnlyDispatchCycle;
import com.ruoyi.wcs.application.ShadowDispatchResult;
import com.ruoyi.wcs.application.dispatch.DispatchCoordinator;
import com.ruoyi.wcs.application.dispatch.DispatchSnapshot;
import com.ruoyi.wcs.domain.dispatch.DefaultVehicleAssignmentPolicy;
import com.ruoyi.wcs.domain.traffic.*;
import com.ruoyi.wcs.port.out.DispatchPolicy;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import java.nio.file.*;
import java.time.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
/** Private representative samples are independent inputs, never a reconstructed continuous history. */
@EnabledIfSystemProperty(named = "shadow.replay.current-directory", matches = ".+")
class ShadowCurrentFieldReplayTest {
@Test
void currentFieldInputsKeepHashesAndPredictionBoundaries() throws Exception {
ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
int count = 0, grants = 0, holds = 0, observations = 0;
try (DirectoryStream<Path> paths = Files.newDirectoryStream(
Paths.get(System.getProperty("shadow.replay.current-directory")), "input-*.json")) {
for (Path path : paths) {
JsonNode export = mapper.readTree(path.toFile());
String raw = export.get("capturedJson").asText();
ShadowFactBatch batch = ShadowCapturedInputReplayTest.batch(mapper.readTree(raw));
ShadowCycleEvidenceCollector collector = new ShadowCycleEvidenceCollector(UUID.randomUUID(), 1L, mapper);
ShadowCycleEvidence original = collector.insufficient(batch, "INDEPENDENT_INPUT_REPLAY",
batch.getDatabaseCapturedAt()).get(0);
assertEquals(raw, original.getInputJson());
assertEquals(export.get("contentHash").asText(), original.getInputBatchHash());
Clock clock = Clock.fixed(batch.getDatabaseCapturedAt(), ZoneOffset.UTC);
ShadowDispatchSnapshotAssembler assembler = new ShadowDispatchSnapshotAssembler("FT546", 1L,
topology -> new ShadowTrafficState(new ResourceLedger(topology), new WaitForGraph(),
0L, Collections.emptyMap()), clock);
DispatchSnapshot snapshot = assembler.assemble(batch);
RoutePlanner routes = new AStarRoutePlanner();
DispatchCoordinator coordinator = new DispatchCoordinator(new ShadowVehicleAssignmentPolicy(
new DefaultVehicleAssignmentPolicy(routes, DispatchPolicy.NONE)),
new DeadlockGuard(new WaitForGraph(), routes), commit -> true, () -> UUID.randomUUID().toString());
ShadowDispatchResult<Ft546SrgvCommandIntent> result = new ReadOnlyDispatchCycle<Ft546SrgvCommandIntent>(
coordinator::dispatch, new ShadowCommandIntentFactory()::create).run(snapshot);
Ft546DeviceProfile profile = new Ft546DeviceProfile();
Map<String, String> registers = new LinkedHashMap<String, String>();
registers.put("taskNo", "4000"); registers.put("taskType", "4002");
registers.put("path", "4004"); registers.put("trigger", "4021");
profile.setRegisters(registers);
Set<String> capabilities = Collections.singleton("POINT_TO_POINT_COMMAND");
profile.setCapabilities(capabilities);
List<Ft546EncodedCommand> encoded = new RecordingFt546CommandGateway(
new Ft546SrgvCommandEncoder(), profile, capabilities).previewAll(result.getIntents());
for (ShadowCycleEvidence value : collector.collect(batch, result.getDecisions(), encoded, clock.instant())) {
assertEquals(original.getInputBatchHash(), value.getInputBatchHash());
assertTrue(value.getDifferences().stream().noneMatch(d -> d.getCategory() == ShadowDifferenceCategory.DEFERRED_COMPARISON));
if ("OBSERVATION_ONLY".equals(value.getDecision())) {
observations++;
assertTrue(value.getEncodedWrites().isEmpty());
}
}
result.getDecisions().getGranted().forEach(grant -> {
ShadowQueueFact queue = batch.queue(grant.getStepId());
assertNull(ShadowQueueDispatchEligibility.observationReason(queue, batch));
if (queue.getAgvId() != null && !queue.getAgvId().trim().isEmpty())
assertEquals(queue.getAgvId(), grant.getVehicleId());
});
if (export.get("cycleId").asText().equals("33d2f0b4-fa10-4908-a31c-c681bc6ded06")) {
ShadowQueueFact pending = batch.getQueues().stream().filter(q -> q.isActive()
&& "60906".equals(q.getTaskNo())).findFirst().get();
assertEquals("LEGACY_RESULT_UNAVAILABLE:INVALID_MAIN_PATH",
new LegacyQueueProtocolProjector().project(pending, batch).getReasonCode());
}
grants += result.getDecisions().getGranted().size();
holds += result.getDecisions().getHeld().size();
count++;
}
}
assertEquals(11, count, "2026-09-09 representative export must be complete");
System.out.println("CURRENT_FIELD_REPLAY independentInputs=" + count + " grants=" + grants
+ " holds=" + holds + " observations=" + observations + " historicalPairingNotInferred=true");
}
}