From c999a03d93eb14c1637b5f6ccd28a4af200c07c8 Mon Sep 17 00:00:00 2001 From: Manuel Polo Date: Sat, 24 Oct 2026 14:43:55 +0000 Subject: [PATCH 1/2] fix(mcp): register the types the native image could not deserialize list_exchanges failed with a bare "HTTP 0" and get_dataset_import answered "Dataset import not found" for every finished import, on the native binary only. - The API's segments and liveSegments are the only uniqueItems arrays of the contract, so they are the only Set fields; Jackson fills a Set with a HashSet it builds reflectively, and that was not registered. Every other collection is a List, which is why nothing else broke. - DatasetImportState was pinned in ReflectConfigTest but the DatasetVersion it holds was not, so its version could not be read and the exception surfaced as "not found". ReflectConfigTest now also walks the fields and member types of every pinned type, which found eight more unregistered types (the enums of Dataset and DatasetVersion, EquityCurveOptions and EquityCurveRequest), and requires HashSet/LinkedHashSet whenever a serialized type holds a Set. The JVM suite cannot run the native image: the registration is checked by name, and the behaviour is to be confirmed on the next tagged native build. Refs api-feedback#66, api-feedback#67. --- CHANGELOG.md | 11 +++ .../com.qtsurfer/mcp-java/reflect-config.json | 27 ++++++ .../com/qtsurfer/mcp/ReflectConfigTest.java | 82 ++++++++++++++++++- 3 files changed, 116 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2878c..493b42a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ All notable changes to `com.qtsurfer:mcp-java` are documented here. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed 🐛 + +- Native binary: register the types it could not deserialize. The API's `segments` and + `liveSegments` are sets, which Jackson fills with a `HashSet` the native image had no + registration for, so `list_exchanges` failed with a bare `HTTP 0`. The `version` a finished + dataset import carries was missing too, so `get_dataset_import` answered "Dataset import not + found". The enums of `Dataset` and `DatasetVersion` and the equity-curve request types were + missing as well. + ## [0.21.0] — 2026-10-23 ### Added ✨ diff --git a/src/main/resources/META-INF/native-image/com.qtsurfer/mcp-java/reflect-config.json b/src/main/resources/META-INF/native-image/com.qtsurfer/mcp-java/reflect-config.json index a5afd59..46d88a0 100644 --- a/src/main/resources/META-INF/native-image/com.qtsurfer/mcp-java/reflect-config.json +++ b/src/main/resources/META-INF/native-image/com.qtsurfer/mcp-java/reflect-config.json @@ -1000,7 +1000,16 @@ { "name": "com.qtsurfer.api.client.model.DatasetImportCreated$StatusEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, { "name": "com.qtsurfer.api.client.model.DatasetImportState", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, { "name": "com.qtsurfer.api.client.model.DatasetImportState$StatusEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.DatasetVersion", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.DatasetVersion$DataFormatEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.DatasetVersion$TimestampUnitEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, { "name": "com.qtsurfer.api.client.model.Dataset", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.Dataset$StatusEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.Dataset$TimestampUnitEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.Dataset$TypeEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.EquityCurveOptions", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.EquityCurveRequest", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, + { "name": "com.qtsurfer.api.client.model.EquityCurveRequest$ModeEnum", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, { "name": "com.qtsurfer.api.client.model.ListDatasets200Response", "allDeclaredConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, { "name": "com.qtsurfer.api.client.model.ResponseError", @@ -2293,6 +2302,15 @@ { "name": "java.util.Collection" }, + { + "name": "java.util.HashSet", + "methods": [ + { + "name": "", + "parameterTypes": [] + } + ] + }, { "name": "java.util.ImmutableCollections$AbstractImmutableCollection" }, @@ -2314,6 +2332,15 @@ { "name": "java.util.ImmutableCollections$MapN" }, + { + "name": "java.util.LinkedHashSet", + "methods": [ + { + "name": "", + "parameterTypes": [] + } + ] + }, { "name": "java.util.List" }, diff --git a/src/test/java/com/qtsurfer/mcp/ReflectConfigTest.java b/src/test/java/com/qtsurfer/mcp/ReflectConfigTest.java index 7d5480e..c3c9990 100644 --- a/src/test/java/com/qtsurfer/mcp/ReflectConfigTest.java +++ b/src/test/java/com/qtsurfer/mcp/ReflectConfigTest.java @@ -50,6 +50,9 @@ * from rotting: every pinned name must still resolve, no pinned name may have become derivable, * and no {@code com.qtsurfer} entry may sit in the config without belonging to one set or the * other. + * + *

A pin is a root, not a leaf: {@link #deriveFromPins()} walks the fields and member types of + * every pinned class the same way, so a type that is only held by a pinned one is expected too. */ class ReflectConfigTest { @@ -188,6 +191,40 @@ void everyPinnedWorkflowIntermediateIsRegistered() throws IOException { PINNED_WORKFLOW_INTERMEDIATES, readConfig()); } + /** + * A pin names one class, but Jackson builds that class's whole object graph: every type held + * in one of its fields, and every enum nested in it. The pin does not carry those along, so + * they were never checked — {@code DatasetImportState} was pinned while the + * {@code DatasetVersion} it holds was not, and the native binary read every successful import + * as "not found". + */ + @Test + void everyTypeHeldByAPinnedTypeIsRegistered() throws IOException { + assertMissing("held by a pinned type", deriveFromPins(), readConfig()); + } + + /** + * Jackson fills an abstract {@code Set} with a {@code HashSet} it constructs reflectively. Only + * the {@code uniqueItems} arrays of the API become sets (today {@code Exchange.segments} and + * {@code liveSegments}); every other collection is a {@code List}, so the missing registration + * showed up only once the API added those two fields — and made {@code list_exchanges} fail + * with {@code HTTP 0} for every caller. + */ + @Test + void setImplementationsAreRegisteredWhenAnyTypeHoldsASet() throws IOException { + Set holders = new TreeSet<>(); + for (String name : allReachable()) { + for (Field field : load(name).getDeclaredFields()) { + if (Set.class.isAssignableFrom(field.getType())) holders.add(name + "." + field.getName()); + } + } + Map> config = readConfig(); + if (holders.isEmpty()) return; + assertThat(config.keySet()) + .as("serialized fields typed Set %s need the concrete set Jackson instantiates", holders) + .contains("java.util.HashSet", "java.util.LinkedHashSet"); + } + // ---- keeping the pins honest ---------------------------------------------- @Test @@ -218,7 +255,7 @@ void noPinnedTypeHasBecomeDerivable() { @Test void noRegisteredQtsurferTypeIsUnaccountedFor() throws IOException { - Set accounted = new LinkedHashSet<>(derive()); + Set accounted = new LinkedHashSet<>(allReachable()); accounted.addAll(pinned()); Set stale = new TreeSet<>(); for (String name : readConfig().keySet()) { @@ -241,11 +278,48 @@ void noRegisteredQtsurferTypeIsUnaccountedFor() throws IOException { * @return the fully qualified names, in {@code $}-nested form as reflect-config spells them */ private static Set derive() { - Set> found = new LinkedHashSet<>(); - Deque pending = new ArrayDeque<>(); + List roots = new ArrayList<>(); for (Method method : BacktestingService.class.getMethods()) { - pending.add(method.getGenericReturnType()); + roots.add(method.getGenericReturnType()); } + return walk(roots); + } + + /** + * Every serialized type held by a pinned type: its fields and member types, transitively. The + * pinned types themselves are included, since they are walked as roots. + */ + private static Set deriveFromPins() { + List roots = new ArrayList<>(); + for (String name : pinned()) { + try { + roots.add(Class.forName(name)); + } catch (ClassNotFoundException e) { + // everyPinnedTypeStillExists reports it; nothing to walk here + } + } + return walk(roots); + } + + /** Everything a tool can return plus everything a pinned type holds. */ + private static Set allReachable() { + Set all = new TreeSet<>(derive()); + all.addAll(deriveFromPins()); + return all; + } + + private static Class load(String name) { + try { + return Class.forName(name); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(name, e); + } + } + + private static Set walk(Iterable roots) { + Set> found = new LinkedHashSet<>(); + Deque pending = new ArrayDeque<>(); + roots.forEach(pending::add); while (!pending.isEmpty()) { Class type = rawType(pending.poll(), pending); if (type == null || !isSerialized(type) || !found.add(type)) continue; From 8afff744b7eaece50b2c1cf132328bc68acf4a7d Mon Sep 17 00:00:00 2001 From: Manuel Polo Date: Sat, 24 Oct 2026 14:43:56 +0000 Subject: [PATCH 2/2] fix(mcp): report a failed or cancelled backtest as such, and say why a read failed The await future was consumed with exceptionally(), which logs the failure and lets the derived future complete normally. SessionJob.status() then read "done" as COMPLETED, so a run whose preparation the platform rejected showed COMPLETED on every poll with no results behind it. - The failure is now recorded and decides the status first (FAILED, or CANCELED for a cancellation). get_job_status shows the platform's reason as Detail, for runs submitted by this server and for runs read off the platform by exchangeId. The status rule is a pure function so it can be tested without a platform. - getDatasetImport no longer swallows every RuntimeException into "not found"; the tool reports the cause. - submit_backtest and the README no longer imply a kline run can be requested. The SDK's BacktestRequest has no type yet, so a dataset run still prepares ticker data; it now fails visibly instead of looking successful. Adding `type` needs sdk-java first. Refs api-feedback#67, api-feedback#68. --- CHANGELOG.md | 7 ++ README.md | 8 +- .../com/qtsurfer/mcp/model/JobSummary.java | 25 +++-- .../mcp/service/SdkBacktestingService.java | 98 ++++++++++++++----- .../java/com/qtsurfer/mcp/tool/McpTools.java | 11 ++- .../SdkBacktestingServiceStatusTest.java | 68 +++++++++++++ .../com/qtsurfer/mcp/tool/McpToolsTest.java | 33 +++++++ 7 files changed, 213 insertions(+), 37 deletions(-) create mode 100644 src/test/java/com/qtsurfer/mcp/service/SdkBacktestingServiceStatusTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 493b42a..0ac9249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), dataset import carries was missing too, so `get_dataset_import` answered "Dataset import not found". The enums of `Dataset` and `DatasetVersion` and the equity-curve request types were missing as well. +- `get_dataset_import` reports why a read failed instead of answering "Dataset import not found" + for every error. +- `get_job_status` no longer reports a failed or cancelled run as `COMPLETED`. The status now + reads the failure the run ended with and shows the platform's reason as `Detail`, for runs + submitted by this server and for runs read off the platform by `exchangeId`. +- `submit_backtest` says it prepares ticker data and cannot request a kline run; a strategy that + reads kline data ends `FAILED` with the platform's reason. ## [0.21.0] — 2026-10-23 diff --git a/README.md b/README.md index 75133fb..b713993 100644 --- a/README.md +++ b/README.md @@ -345,9 +345,11 @@ docker run -i --rm -e QTSURFER_APIKEY \ Once `get_dataset_upload` returns `READY`, inspect `type` with `get_dataset`, then submit a run with `datasetId`; omit `exchangeId` and `instrument`, because MCP derives the reserved `exchangeId=user`. -`datasetVersionId` pins a prior ready version. User-data kline backtests require the selected ready -version to have `type=klines`; ticker backtests accept either type. An uploaded version's type is -inferred from its rows when ingest completes. `equityCurve` is accepted by both run tools and is +`datasetVersionId` pins a prior ready version. A run is prepared as ticker data, which accepts a +dataset of either type; this tool cannot yet request a kline run, so a strategy that reads kline +data ends `FAILED` with the platform's reason (the API itself runs kline backtests only over a +version with `type=klines`). An uploaded version's type is inferred from its rows when ingest +completes. `equityCurve` is accepted by both run tools and is passed through to the API. ### Import market history diff --git a/src/main/java/com/qtsurfer/mcp/model/JobSummary.java b/src/main/java/com/qtsurfer/mcp/model/JobSummary.java index 7df5580..531f7a7 100644 --- a/src/main/java/com/qtsurfer/mcp/model/JobSummary.java +++ b/src/main/java/com/qtsurfer/mcp/model/JobSummary.java @@ -3,16 +3,24 @@ /** * Lightweight view of a backtest job returned by status / list queries. * - * @param jobId server-assigned execution identifier - * @param instrument CCXT-style instrument (e.g. {@code BTC/USDT}) - * @param exchangeId exchange (e.g. {@code binance}) - * @param status current lifecycle state - * @param submittedAt ISO-8601 submission timestamp - * @param result execution metrics, non-null only when {@code status} is COMPLETED + * @param jobId server-assigned execution identifier + * @param instrument CCXT-style instrument (e.g. {@code BTC/USDT}) + * @param exchangeId exchange (e.g. {@code binance}) + * @param status current lifecycle state + * @param submittedAt ISO-8601 submission timestamp + * @param result execution metrics, non-null only when {@code status} is COMPLETED + * @param retryable whether the same request can succeed on retry, when the API says so + * @param statusDetail why the run ended without a result, when it did and the platform said why */ public record JobSummary( String jobId, String instrument, String exchangeId, JobStatus status, - String submittedAt, JobResult result, Boolean retryable) { + String submittedAt, JobResult result, Boolean retryable, String statusDetail) { + + /** Preserve the constructor that predates {@code statusDetail}. */ + public JobSummary(String jobId, String instrument, String exchangeId, JobStatus status, + String submittedAt, JobResult result, Boolean retryable) { + this(jobId, instrument, exchangeId, status, submittedAt, result, retryable, null); + } /** Preserve the original completed-job constructor. */ public JobSummary(String jobId, String instrument, String exchangeId, JobStatus status, @@ -33,6 +41,7 @@ public String toString() { + " exchange=" + exchangeId + " status=" + status + " submitted=" + submittedAt - + (retryable == null ? "" : " retryable=" + retryable); + + (retryable == null ? "" : " retryable=" + retryable) + + (statusDetail == null ? "" : " detail=" + statusDetail); } } diff --git a/src/main/java/com/qtsurfer/mcp/service/SdkBacktestingService.java b/src/main/java/com/qtsurfer/mcp/service/SdkBacktestingService.java index fac07ef..012bea1 100644 --- a/src/main/java/com/qtsurfer/mcp/service/SdkBacktestingService.java +++ b/src/main/java/com/qtsurfer/mcp/service/SdkBacktestingService.java @@ -53,6 +53,7 @@ import com.qtsurfer.api.sdk.UpdateLiveParamsRequestBuilder; import com.qtsurfer.api.sdk.ValidationOutcome; import com.qtsurfer.api.sdk.auth.AuthenticatedClient; +import com.qtsurfer.api.sdk.errors.QTSCanceledError; import com.qtsurfer.mcp.model.EquityPoint; import com.qtsurfer.mcp.model.DatasetSummary; import com.qtsurfer.mcp.model.DatasetUploadResult; @@ -80,6 +81,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; @@ -197,19 +199,59 @@ private record SessionJob( String submittedAt, CompletableFuture future, Backtest backtest, - AtomicReference resultRef) { + AtomicReference resultRef, + AtomicReference failure) { JobStatus status() { - if (future.isCompletedExceptionally()) return JobStatus.FAILED; - if (future.isDone()) return JobStatus.COMPLETED; - if (backtest == null) return JobStatus.COMPILING; - return switch (backtest.state()) { - case EXECUTING -> JobStatus.EXECUTING; - case COMPLETED -> JobStatus.COMPLETED; - case FAILED -> JobStatus.FAILED; - case CANCELED -> JobStatus.CANCELED; - }; + return resolveStatus(failure.get(), future.isCompletedExceptionally(), future.isDone(), + backtest == null ? null : backtest.state()); } + + /** The platform's reason when the run ended badly, otherwise {@code null}. */ + String failureDetail() { + Failure f = failure.get(); + return f == null ? null : f.detail(); + } + } + + /** How a submitted run ended when it did not complete, and the reason the platform gave. */ + record Failure(JobStatus status, String detail) {} + + /** + * The lifecycle state of a submitted run. + * + *

The await future is not a reliable signal on its own: the failure is observed and recorded + * as a {@link Failure} so the future itself can complete normally, which made a run whose + * preparation was rejected read as {@link JobStatus#COMPLETED}. A recorded failure therefore + * decides first, then a cancellation the SDK has already marked, and only a future that is done + * with neither is a completed run. + * + * @param failure the failure recorded when the await future ended badly, or {@code null} + * @param exceptional whether the future itself completed exceptionally + * @param done whether the future is done + * @param state the SDK's local snapshot, or {@code null} while the strategy compiles + */ + static JobStatus resolveStatus( + Failure failure, boolean exceptional, boolean done, Backtest.State state) { + if (failure != null) return failure.status(); + if (exceptional) return JobStatus.FAILED; + if (state == Backtest.State.CANCELED) return JobStatus.CANCELED; + if (done) return JobStatus.COMPLETED; + if (state == null) return JobStatus.COMPILING; + return switch (state) { + case EXECUTING -> JobStatus.EXECUTING; + case COMPLETED -> JobStatus.COMPLETED; + case FAILED -> JobStatus.FAILED; + case CANCELED -> JobStatus.CANCELED; + }; + } + + /** Whether an await failure is the run being cancelled rather than failing. */ + static boolean isCancellation(Throwable error) { + for (Throwable t = error; t != null; t = t.getCause()) { + if (t instanceof QTSCanceledError || t instanceof CancellationException) return true; + } + return false; } /** Session provenance required to address a retained curve through the SDK. */ @@ -336,13 +378,12 @@ public DatasetImportResult importDataset(DatasetImportRequest request) { @Override public Optional getDatasetImport(String datasetId, String importId) { - try { - DatasetImportState state = qts.getDatasetImport(datasetId, importId); - return Optional.of(new DatasetImportStatus(datasetId, state.getImportId(), String.valueOf(state.getStatus()), - state.getJobId(), state.getError(), state.getVersion() == null ? null : state.getVersion().getId())); - } catch (RuntimeException e) { - return Optional.empty(); - } + // A failed read propagates: the tool reports its cause. Swallowing it here read every error, + // a body the client could not parse included, as an import that does not exist. + DatasetImportState state = qts.getDatasetImport(datasetId, importId); + if (state == null) return Optional.empty(); + return Optional.of(new DatasetImportStatus(datasetId, state.getImportId(), String.valueOf(state.getStatus()), + state.getJobId(), state.getError(), state.getVersion() == null ? null : state.getVersion().getId())); } private Path guardedUploadFile(String filePath) { @@ -481,20 +522,29 @@ public String submitBacktest(BacktestRequest sdkRequest) { String submittedAt = Instant.now().toString(); String finalJobId = jobId; AtomicReference resultRef = new AtomicReference<>(); + AtomicReference failureRef = new AtomicReference<>(); - // Track the await future; capture ResultMap when execution completes + // Track the await future; capture ResultMap when execution completes. A failure is kept, not + // just logged: this future completes normally either way, so the job's status reads it back. CompletableFuture future = backtest.await() .thenAccept(sdkResult -> { resultRef.set(sdkResult); log.info("Job {} completed — pnl={} trades={}", finalJobId, sdkResult.getPnlTotal(), sdkResult.getTotalTrades()); }) - .exceptionally(err -> { log.warn("Job {} failed: {}", finalJobId, rootMessage(err)); return null; }); + .exceptionally(err -> { + String detail = rootMessage(err); + failureRef.set(new Failure( + isCancellation(err) ? JobStatus.CANCELED : JobStatus.FAILED, detail)); + log.warn("Job {} ended without a result: {}", finalJobId, detail); + return null; + }); String source = sdkRequest.datasetId() == null ? sdkRequest.instrument() : "dataset:" + sdkRequest.datasetId() + (sdkRequest.datasetVersionId() == null ? "" : ":" + sdkRequest.datasetVersionId()); - jobs.put(jobId, new SessionJob(jobId, source, sdkRequest.exchangeId(), submittedAt, future, backtest, resultRef)); + jobs.put(jobId, new SessionJob( + jobId, source, sdkRequest.exchangeId(), submittedAt, future, backtest, resultRef, failureRef)); log.info("Submitted backtest {} ({} {} {} → {})", jobId, sdkRequest.exchangeId(), source, sdkRequest.from(), sdkRequest.to()); return jobId; @@ -510,7 +560,7 @@ public Optional getJobStatus(String jobId, String exchangeId) { ResultMap sdkResult = job.resultRef().get(); JobResult result = sdkResult != null ? toJobResult(sdkResult) : null; return Optional.of(new JobSummary(job.jobId(), job.instrument(), job.exchangeId(), - job.status(), job.submittedAt(), result)); + job.status(), job.submittedAt(), result, null, job.failureDetail())); } /** @@ -560,9 +610,13 @@ private Optional fromPlatform(String jobId, String exchangeId) { ? results.getInstrument() : "unknown"; String startedAt = state != null && state.getStartTime() != null ? state.getStartTime().toString() : "unknown"; + // The platform's reason only means something for a run that ended without a result; a + // completed one can carry an informational detail that would read as an error. + String detail = state != null && (status == JobStatus.FAILED || status == JobStatus.CANCELED) + ? state.getStatusDetail() : null; return Optional.of( new JobSummary(jobId, instrument, exchangeId, status, startedAt, result, - state == null ? null : state.getRetryable())); + state == null ? null : state.getRetryable(), detail)); } private static JobResult toJobResult(ResultMap r) { diff --git a/src/main/java/com/qtsurfer/mcp/tool/McpTools.java b/src/main/java/com/qtsurfer/mcp/tool/McpTools.java index 70713e6..13b959c 100644 --- a/src/main/java/com/qtsurfer/mcp/tool/McpTools.java +++ b/src/main/java/com/qtsurfer/mcp/tool/McpTools.java @@ -650,7 +650,8 @@ private static SyncToolSpecification submitBacktest(BacktestingService service) .description("Compile a Java strategy and queue a backtesting run on QTSurfer. " + "Returns the job ID immediately — poll with get_job_status. " + "Provide either exchangeId plus instrument, or datasetId (the server derives exchangeId=user). " - + "For a user-data kline run, the selected ready dataset version must have type=klines; ticker runs accept either type. " + + "A run is prepared as ticker data and a dataset of either type is accepted for it; this tool " + + "cannot request a kline run, so a strategy that reads kline data ends FAILED with the platform's reason. " + "Set storeSignals only for selected runs that need emitted signals: it is disabled by default " + "and retained signals consume the account's storage quota. " + "To try the same strategy at many parameter settings, use submit_sweep instead of " @@ -701,7 +702,8 @@ private static SyncToolSpecification getJobStatus(BacktestingService service) { Tool tool = Tool.builder() .name("get_job_status") .description("Get the current status of a backtesting job. " - + "A failed job may include retryable, indicating whether the same request can succeed on retry. " + + "A failed job may include retryable, indicating whether the same request can succeed on retry, " + + "and Detail, the reason the platform gave for ending the run without a result. " + "A job submitted by this server in this session is answered from its own state and " + "needs nothing but the jobId. Any other job id — from the web app, another session, " + "or this server before a restart — is read straight off the platform, which " @@ -729,7 +731,7 @@ private static SyncToolSpecification getJobStatus(BacktestingService service) { return service.getJobStatus(jobId, exchangeId) .map(j -> { String body = formatJobStatus(j.jobId(), j.instrument(), j.exchangeId(), - j.status(), j.submittedAt(), j.result(), j.retryable()); + j.status(), j.submittedAt(), j.result(), j.retryable(), j.statusDetail()); if (includeCurve && j.result() != null && j.result().equityCurve() != null && !j.result().equityCurve().isEmpty()) { body += "\n\nEquity curve:\n" @@ -2064,12 +2066,13 @@ private static WalkForwardSpec parseWalkForward(Object raw) { private static String formatJobStatus(String jobId, String instrument, String exchangeId, JobStatus status, String submittedAt, JobResult result, - Boolean retryable) { + Boolean retryable, String statusDetail) { StringBuilder sb = new StringBuilder(); sb.append("Job ").append(jobId).append(": ").append(status).append('\n'); sb.append("Exchange: ").append(exchangeId).append(" | Instrument: ").append(instrument).append('\n'); sb.append("Submitted: ").append(submittedAt); if (retryable != null) sb.append("\nRetryable: ").append(retryable); + if (statusDetail != null && !statusDetail.isBlank()) sb.append("\nDetail: ").append(statusDetail); if (result != null) { sb.append("\n\n=== Results ===\n"); diff --git a/src/test/java/com/qtsurfer/mcp/service/SdkBacktestingServiceStatusTest.java b/src/test/java/com/qtsurfer/mcp/service/SdkBacktestingServiceStatusTest.java new file mode 100644 index 0000000..20a115d --- /dev/null +++ b/src/test/java/com/qtsurfer/mcp/service/SdkBacktestingServiceStatusTest.java @@ -0,0 +1,68 @@ +package com.qtsurfer.mcp.service; + +import com.qtsurfer.api.sdk.Backtest; +import com.qtsurfer.mcp.model.JobStatus; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletionException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The status a submitted run reports. The await future is consumed so that it completes normally + * whether the run succeeded or not, which once made a run whose preparation the platform rejected + * read as COMPLETED — a green light with no results behind it. + */ +class SdkBacktestingServiceStatusTest { + + private static final SdkBacktestingService.Failure REJECTED = new SdkBacktestingService.Failure( + JobStatus.FAILED, "This strategy reads kline data, but the run was submitted against ticker data."); + + @Test + void aRecordedFailureIsFailedEvenThoughTheFutureCompletedNormally() { + assertThat(SdkBacktestingService.resolveStatus(REJECTED, false, true, Backtest.State.EXECUTING)) + .isEqualTo(JobStatus.FAILED); + } + + @Test + void aRecordedCancellationIsCanceledNotFailed() { + var canceled = new SdkBacktestingService.Failure(JobStatus.CANCELED, "cancelled"); + assertThat(SdkBacktestingService.resolveStatus(canceled, false, true, Backtest.State.EXECUTING)) + .isEqualTo(JobStatus.CANCELED); + } + + @Test + void aFutureThatFinishedWithNoFailureIsCompleted() { + assertThat(SdkBacktestingService.resolveStatus(null, false, true, Backtest.State.COMPLETED)) + .isEqualTo(JobStatus.COMPLETED); + } + + @Test + void aFutureThatCompletedExceptionallyIsFailed() { + assertThat(SdkBacktestingService.resolveStatus(null, true, true, Backtest.State.EXECUTING)) + .isEqualTo(JobStatus.FAILED); + } + + @Test + void cancelMarksTheRunBeforeItsFutureSettles() { + assertThat(SdkBacktestingService.resolveStatus(null, false, false, Backtest.State.CANCELED)) + .isEqualTo(JobStatus.CANCELED); + } + + @Test + void aRunStillInFlightFollowsTheSdkSnapshot() { + assertThat(SdkBacktestingService.resolveStatus(null, false, false, null)) + .isEqualTo(JobStatus.COMPILING); + assertThat(SdkBacktestingService.resolveStatus(null, false, false, Backtest.State.EXECUTING)) + .isEqualTo(JobStatus.EXECUTING); + } + + @Test + void aCancellationAnywhereInTheCauseChainIsNotAFailure() { + assertThat(SdkBacktestingService.isCancellation( + new CompletionException(new CancellationException("polling cancelled")))).isTrue(); + assertThat(SdkBacktestingService.isCancellation( + new CompletionException(new IllegalStateException("prepare rejected")))).isFalse(); + } +} diff --git a/src/test/java/com/qtsurfer/mcp/tool/McpToolsTest.java b/src/test/java/com/qtsurfer/mcp/tool/McpToolsTest.java index f231cd0..392cf31 100644 --- a/src/test/java/com/qtsurfer/mcp/tool/McpToolsTest.java +++ b/src/test/java/com/qtsurfer/mcp/tool/McpToolsTest.java @@ -5,6 +5,7 @@ import io.modelcontextprotocol.spec.McpSchema.CallToolResult; import io.modelcontextprotocol.spec.McpSchema.TextContent; import com.qtsurfer.mcp.model.EquityPoint; +import com.qtsurfer.mcp.model.DatasetImportStatus; import com.qtsurfer.mcp.model.DatasetSummary; import com.qtsurfer.mcp.model.JobResult; import com.qtsurfer.mcp.model.JobStatus; @@ -468,6 +469,38 @@ public Optional getJobStatus(String jobId, String exchangeId) { .contains("Retryable: true"); } + @Test + void getDatasetImportReportsWhyAReadFailedInsteadOfNotFound() { + BacktestingServiceStub stub = new BacktestingServiceStub() { + @Override + public Optional getDatasetImport(String datasetId, String importId) { + throw new RuntimeException("dataset import response could not be read"); + } + }; + String text = callText(McpTools.build(stub, "https://api.qtsurfer.net/v1"), + "get_dataset_import", Map.of("datasetId", "ds_1", "importId", "imp_1")); + + assertThat(text).contains("Failed to read dataset import").contains("could not be read") + .doesNotContain("not found"); + } + + @Test + void getJobStatusShowsWhyAFailedRunEndedWithoutResults() { + String reason = "This strategy reads kline data, but the run was submitted against ticker data."; + BacktestingServiceStub stub = new BacktestingServiceStub() { + @Override + public Optional getJobStatus(String jobId, String exchangeId) { + if (!"bt-failed".equals(jobId)) return super.getJobStatus(jobId, exchangeId); + return Optional.of(new JobSummary("bt-failed", "dataset:ds_1", "user", + JobStatus.FAILED, "2026-10-23T00:00:00Z", null, null, reason)); + } + }; + String text = callText(McpTools.build(stub, "https://api.qtsurfer.net/v1"), + "get_job_status", Map.of("jobId", "bt-failed")); + + assertThat(text).contains("Job bt-failed: FAILED").contains("Detail: " + reason); + } + @Test void getDatasetShowsTheResolvedTypeForBacktestSelection() { BacktestingServiceStub stub = new BacktestingServiceStub() {