fix(application): preserve a cancellation that races a completed provider run (#2710)
Squash merge of https://github.com/morluto/rea/pull/2710. Original pull request submitted by @XiaoBinGan. Co-authored-by: 吴佳浩 <wujiahao@supremind.com>
This commit is contained in:
@@ -43,7 +43,7 @@ export const executeProviderOperation = async <
|
||||
provider: ProviderOperationPort<TRequest>,
|
||||
confidence: "observed" | "derived",
|
||||
): Promise<Result<Evidence, AnalysisError>> => {
|
||||
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);
|
||||
};
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user