From a7fb782d3a361266a2b1c7adb64774e7cac5a972 Mon Sep 17 00:00:00 2001 From: Trevor Walker Date: Tue, 22 Sep 2026 07:04:25 -0600 Subject: [PATCH] feat(prime): support abort_and_send_queued capability with fallback to abort --- .../prime/PrimeAgentDaemonAdapter.test.ts | 4 + .../provider/prime/PrimeAgentDaemonBridge.ts | 1 + .../PrimeAgentDaemonSessionRuntime.test.ts | 113 ++++++++++++++++++ .../prime/PrimeAgentDaemonSessionRuntime.ts | 14 ++- 4 files changed, 131 insertions(+), 1 deletion(-) diff --git a/apps/server/src/provider/prime/PrimeAgentDaemonAdapter.test.ts b/apps/server/src/provider/prime/PrimeAgentDaemonAdapter.test.ts index ca3c2ae7d6..542d5ee2de 100644 --- a/apps/server/src/provider/prime/PrimeAgentDaemonAdapter.test.ts +++ b/apps/server/src/provider/prime/PrimeAgentDaemonAdapter.test.ts @@ -1110,6 +1110,10 @@ function fakeRuntimeFactory( captures.order.push("abort"); expect(captures.prompts.at(-1)?.signal?.aborted).toBe(true); }), + abortAndSendQueued: Effect.sync(() => { + captures.order.push("abortAndSendQueued"); + expect(captures.prompts.at(-1)?.signal?.aborted).toBe(true); + }), abortAndClearQueue: captures.abortClearFailure ? Effect.fail( new PrimeAgentDaemonSessionRuntimeError({ diff --git a/apps/server/src/provider/prime/PrimeAgentDaemonBridge.ts b/apps/server/src/provider/prime/PrimeAgentDaemonBridge.ts index c5dcc4c814..65e1260632 100644 --- a/apps/server/src/provider/prime/PrimeAgentDaemonBridge.ts +++ b/apps/server/src/provider/prime/PrimeAgentDaemonBridge.ts @@ -225,6 +225,7 @@ export interface PrimeAgentDaemonAgentConnection { ) => Promise; readonly abort: () => Promise; readonly abortAndClearQueue?: () => Promise; + readonly abortAndSendQueued?: () => Promise; readonly startSideQuestion?: (nativeId: string, question: string) => Promise; readonly abortSideQuestion?: (nativeId: string) => Promise; readonly cancelRlmChild?: (childId: string) => Promise; diff --git a/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.test.ts b/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.test.ts index c28abbde45..adcff2c694 100644 --- a/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.test.ts +++ b/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.test.ts @@ -283,6 +283,9 @@ function fixture(options?: { readonly setRlmImpl?: (maxDepth: number) => Promise; readonly cancelRlmImpl?: (agentId: string) => Promise; readonly sendAgentMessageImpl?: (activeSessionId: string, message: string) => Promise; + readonly abortAndSendQueuedImpl?: () => Promise; + readonly abortAndSendQueuedSupported?: boolean; + readonly omitAbortAndSendQueued?: boolean; readonly omitSendAgentMessage?: boolean; readonly omitWatchSession?: boolean; readonly watchSessionUndefined?: boolean; @@ -572,6 +575,9 @@ function fixture(options?: { if (options?.omitNegotiatedCapabilityAccessor === true) { Object.defineProperty(this, "supportsNegotiatedCapability", { value: undefined }); } + if (options?.omitAbortAndSendQueued === true) { + Object.defineProperty(this, "abortAndSendQueued", { value: undefined }); + } } static attach( _client: PrimeAgentDaemonClient, @@ -672,6 +678,9 @@ function fixture(options?: { return options?.getPromptLifecyclesImpl?.() ?? Promise.resolve({ records: [], expired: [] }); } supportsNegotiatedCapability(capability: string): boolean { + if (capability === "abort_and_send_queued_v1") { + return options?.abortAndSendQueuedSupported ?? false; + } return capability === "caller_owned_session_environment_cleanup_v1" ? true : capability === "correlated_prompt_lifecycle_v1" && @@ -730,6 +739,10 @@ function fixture(options?: { captures.connectionCalls.push({ method: "abort", args: [] }); return Promise.resolve(undefined); } + abortAndSendQueued(): Promise { + captures.connectionCalls.push({ method: "abortAndSendQueued", args: [] }); + return options?.abortAndSendQueuedImpl?.() ?? Promise.resolve(undefined); + } abortAndClearQueue(): Promise { captures.connectionCalls.push({ method: "abortAndClearQueue", args: [] }); queuedInputSuspended = true; @@ -14250,6 +14263,106 @@ describe("Prime Agent live activity privacy boundary", () => { ), ); + it.effect( + "invokes connection.abortAndSendQueued when abort_and_send_queued_v1 is negotiated", + () => + Effect.scoped( + Effect.gen(function* () { + const { captures, make } = fixture({ abortAndSendQueuedSupported: true }); + const runtime = yield* make(); + + yield* runtime.abort; + expect(captures.connectionCalls).toContainEqual({ + method: "abortAndSendQueued", + args: [], + }); + expect(captures.connectionCalls).not.toContainEqual({ + method: "abort", + args: [], + }); + + yield* runtime.abortAndSendQueued; + expect( + captures.connectionCalls.filter((call) => call.method === "abortAndSendQueued"), + ).toHaveLength(2); + }), + ), + ); + + it.effect("falls back to connection.abort when abort_and_send_queued_v1 is not negotiated", () => + Effect.scoped( + Effect.gen(function* () { + const { captures, make } = fixture({ abortAndSendQueuedSupported: false }); + const runtime = yield* make(); + + yield* runtime.abort; + expect(captures.connectionCalls).toContainEqual({ + method: "abort", + args: [], + }); + expect(captures.connectionCalls).not.toContainEqual({ + method: "abortAndSendQueued", + args: [], + }); + + yield* runtime.abortAndSendQueued; + expect(captures.connectionCalls.filter((call) => call.method === "abort")).toHaveLength(2); + expect(captures.connectionCalls).not.toContainEqual({ + method: "abortAndSendQueued", + args: [], + }); + }), + ), + ); + + it.effect( + "falls back to connection.abort when abort_and_send_queued_v1 is negotiated but method is missing", + () => + Effect.scoped( + Effect.gen(function* () { + const { captures, make } = fixture({ + abortAndSendQueuedSupported: true, + omitAbortAndSendQueued: true, + }); + const runtime = yield* make(); + + yield* runtime.abort; + expect(captures.connectionCalls).toContainEqual({ + method: "abort", + args: [], + }); + expect(captures.connectionCalls).not.toContainEqual({ + method: "abortAndSendQueued", + args: [], + }); + + yield* runtime.abortAndSendQueued; + expect(captures.connectionCalls.filter((call) => call.method === "abort")).toHaveLength( + 2, + ); + }), + ), + ); + + it.effect("propagates failure when abortAndSendQueued rejects", () => + Effect.scoped( + Effect.gen(function* () { + const { make } = fixture({ + abortAndSendQueuedSupported: true, + abortAndSendQueuedImpl: () => Promise.reject(new Error("abort_and_send_queued failed")), + }); + const runtime = yield* make(); + + const error = yield* runtime.abort.pipe(Effect.flip); + expect(error).toMatchObject({ + operation: "abort", + reason: "request-failed", + detail: "The daemon operation failed.", + }); + }), + ), + ); + it.effect("deduplicates explicit abort, dispose, and ask finalization", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.ts b/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.ts index 4dc5f1cce0..a49909cb5a 100644 --- a/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.ts +++ b/apps/server/src/provider/prime/PrimeAgentDaemonSessionRuntime.ts @@ -1262,6 +1262,7 @@ export interface PrimeAgentDaemonSessionRuntime { }) => Effect.Effect; readonly abort: Effect.Effect; readonly abortAndClearQueue: Effect.Effect; + readonly abortAndSendQueued: Effect.Effect; readonly sideQuestionsAvailable: boolean; /** Runs one requester-scoped unary question; native prompt/error/id fields never escape. */ readonly askSideQuestion: ( @@ -8199,9 +8200,19 @@ export const makePrimeAgentDaemonSessionRuntime = Effect.fn("makePrimeAgentDaemo if (correlatedPromptLifecycleAvailable) { yield* requireCorrelatedPromptLifecycleAdmission("abort"); } - yield* callVoid("abort", () => connection!.abort()); + const canAbortAndSendQueued = + Predicate.isFunction(connection?.supportsNegotiatedCapability) && + connection.supportsNegotiatedCapability("abort_and_send_queued_v1") === true && + Predicate.isFunction(connection?.abortAndSendQueued); + if (canAbortAndSendQueued) { + yield* callVoid("abort", () => connection!.abortAndSendQueued!()); + } else { + yield* callVoid("abort", () => connection!.abort()); + } }); + const abortAndSendQueued = abort; + const abortAndClearQueue = Effect.gen(function* () { yield* ensureOpen("abort-and-clear-queue"); if (correlatedPromptLifecycleAvailable) { @@ -9020,6 +9031,7 @@ export const makePrimeAgentDaemonSessionRuntime = Effect.fn("makePrimeAgentDaemo setInputQueueMode, abort, abortAndClearQueue, + abortAndSendQueued, sideQuestionsAvailable, askSideQuestion, abortSideQuestion,