From 855ed9689e972903d3c6854907dac83207a00141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E4=BD=B3=E6=B5=A9Alben?= <35291372+XiaoBinGan@users.noreply.github.com> Date: Tue, 13 Oct 2026 23:51:44 +0800 Subject: [PATCH] fix(application): preserve a cancellation that races a completed provider run (#2710) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Squash merge of https://github.com/morluto/rea/pull/2710. Original pull request submitted by @XiaoBinGan. Co-authored-by: 吴佳浩 --- src/application/ProviderOperationExecution.ts | 20 ++++++++--- .../ApktoolResourceAnalysisService.test.ts | 34 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/application/ProviderOperationExecution.ts b/src/application/ProviderOperationExecution.ts index bf4ad4b0e..966a43f29 100644 --- a/src/application/ProviderOperationExecution.ts +++ b/src/application/ProviderOperationExecution.ts @@ -43,7 +43,7 @@ export const executeProviderOperation = async < provider: ProviderOperationPort, confidence: "observed" | "derived", ): Promise> => { - if (options?.signal?.aborted === true) + if (options?.signal?.aborted) return err(new AnalysisCancelledError(operation)); const request = schema.safeParse({ operation, input }); if (!request.success) @@ -51,8 +51,10 @@ export const executeProviderOperation = async < const executed = await provider.execute(request.data, options); if (!executed.ok) return executed; const execution = executed.value; - return ok( - createEvidence(execution.subject ?? undefined, execution.provider, { + const evidence = createEvidence( + execution.subject ?? undefined, + execution.provider, + { operation, parameters: jsonObjectSchema.parse(request.data.input), result: execution.result, @@ -60,6 +62,16 @@ export const executeProviderOperation = async < limitations: execution.limitations, locations: execution.locations, confidence, - }), + }, ); + // A provider that cannot observe the signal may finish its work after the + // caller cancelled, so re-check rather than reporting a completed operation + // the caller no longer wants. The observation it completed is still this + // provider's Evidence, so the cancellation carries it as the partial + // observation instead of discarding a finished result. + if (options?.signal?.aborted) + return err( + new AnalysisCancelledError(operation, { partialObservation: evidence }), + ); + return ok(evidence); }; diff --git a/src/application/apktool/ApktoolResourceAnalysisService.test.ts b/src/application/apktool/ApktoolResourceAnalysisService.test.ts index 6242bb54d..2220ce9e6 100644 --- a/src/application/apktool/ApktoolResourceAnalysisService.test.ts +++ b/src/application/apktool/ApktoolResourceAnalysisService.test.ts @@ -83,3 +83,37 @@ it("preserves cancellation before execution", async () => { if (result.ok) return; expect(result.error).toBeInstanceOf(AnalysisCancelledError); }); + +it("preserves a cancellation that races a completed observation", async () => { + const controller = new AbortController(); + const racingPort: ApktoolResourceAnalysisPort = { + async inspectAvailability() { + return { status: "available", code: null, reason: null, diagnostics: {} }; + }, + async close() {}, + async execute() { + // The provider may finish its work before the abort is delivered, so a + // successful result is not by itself proof the caller still wants it. + controller.abort(); + return ok(execution); + }, + }; + const result = await new ApktoolResourceAnalysisService(racingPort).execute( + "decode_android_resources", + { path: "/targets/app.apk", include_strings: true }, + { signal: controller.signal }, + ); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.error).toBeInstanceOf(AnalysisCancelledError); + // The observation the provider completed is retained on the cancellation, so + // a session still records the Evidence instead of losing finished work. + const retained = parseEvidence(result.error.partialObservation); + expect(retained.operation).toBe("decode_android_resources"); + expect(retained.provider).toEqual(identity); + expect(retained.parameters).toEqual({ + path: "/targets/app.apk", + include_strings: true, + }); + expect(retained.confidence).toBe("observed"); +});