TypeScript
fix(server): stop threads without resolving a run (#22052)
Yash Singhfb990765a494
diff --git a/apps/server/src/environment/ServerEnvironment.ts b/apps/server/src/environment/ServerEnvironment.tsindex cef5d9358ca..d53d5420c3f 100644--- a/apps/server/src/environment/ServerEnvironment.ts+++ b/apps/server/src/environment/ServerEnvironment.ts@@ -255,6 +255,7 @@ export const make = Effect.gen(function* () {pullRequestStackActions: true,threadPullRequestLinking: true,serverResolvedCommandContext: true,+ threadStop: true,environmentIcon: true,projectCloneTracking: true,...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.tsindex ce54867c356..17d43f5c14a 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts@@ -4993,7 +4993,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {);},);- it.effect("interrupts a completed run's background command through orchestration", () => {+ it.effect("stops a completed run's background command without a client-supplied run", () => {const transcript = makeBackgroundStopTranscript(true);return Effect.scoped(Effect.gen(function* () {@@ -5091,10 +5091,9 @@ describe("CodexAdapterV2 post-settle continuation", () => {Effect.forkChild({ startImmediately: true }),);yield* orchestrator.dispatch({- type: "run.interrupt",+ type: "thread.stop",commandId: CommandId.make("stop-background-command"),threadId,- runId: run.id,});yield* worker.drain();yield* Fiber.join(stopped);diff --git a/apps/server/src/orchestration-v2/testkit/OrchestratorScenario.ts b/apps/server/src/orchestration-v2/testkit/OrchestratorScenario.tsindex 7588837610f..b52639824d1 100644--- a/apps/server/src/orchestration-v2/testkit/OrchestratorScenario.ts+++ b/apps/server/src/orchestration-v2/testkit/OrchestratorScenario.ts@@ -131,6 +131,7 @@ export class OrchestratorV2ScenarioStepError extends Schema.TaggedError<Orchestrfunction commandThreadIds(command: OrchestrationV2Command): ReadonlyArray<ThreadId> {switch (command.type) {case "thread.create":+ case "thread.stop":case "thread.archive":case "thread.unarchive":case "thread.delete":diff --git a/packages/client-runtime/src/operations/commands.test.ts b/packages/client-runtime/src/operations/commands.test.tsindex a02554bb0fb..eb8f91cf501 100644--- a/packages/client-runtime/src/operations/commands.test.ts+++ b/packages/client-runtime/src/operations/commands.test.ts@@ -79,6 +79,7 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(functreadonly projection?: OrchestrationV2ThreadProjection;readonly projectionRequests?: ThreadId[];readonly advertiseServerResolvedCommandContext?: boolean;+ readonly advertiseThreadStop?: boolean;}) {const client = {[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) =>@@ -126,6 +127,7 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(functenvironment: {capabilities: {repositoryIdentity: true,+ ...(input.advertiseThreadStop === true ? { threadStop: true } : {}),...(input.advertiseServerResolvedCommandContext === false? {}: { serverResolvedCommandContext: true }),@@ -1049,6 +1051,34 @@ describe("V2 environment commands", () => {}).pipe(Effect.provide(layerTestCrypto)),);+ it.effect("stops a thread without relying on background work in the history window", () =>+ Effect.gen(function* () {+ const commands: OrchestrationV2Command[] = [];+ const projectionRequests: ThreadId[] = [];+ const supervisor = yield* makeSupervisor({+ commands,+ projects: [],+ projectionRequests,+ advertiseThreadStop: true,+ });++ const result = yield* interruptThreadTurn({+ commandId: CommandId.make("stop-outside-history-window"),+ threadId: v2ThreadId,+ }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));++ expect(result).toEqual({ sequence: 1 });+ expect(projectionRequests).toEqual([]);+ expect(commands).toEqual([+ {+ type: "thread.stop",+ commandId: "stop-outside-history-window",+ threadId: v2ThreadId,+ },+ ]);+ }).pipe(Effect.provide(layerTestCrypto)),+ );+it.effect("interrupts a known run without fetching the full projection", () =>Effect.gen(function* () {const commands: OrchestrationV2Command[] = [];@@ -1057,6 +1087,7 @@ describe("V2 environment commands", () => {commands,projects: [],projectionRequests,+ advertiseThreadStop: true,});yield* interruptThreadTurn({diff --git a/packages/client-runtime/src/operations/commands.ts b/packages/client-runtime/src/operations/commands.tsindex b16a2f5b489..e839231e389 100644--- a/packages/client-runtime/src/operations/commands.ts+++ b/packages/client-runtime/src/operations/commands.ts@@ -782,6 +782,17 @@ export const interruptThreadTurn = Effect.fn("EnvironmentCommands.interruptThrea});}let runId = input.runId ?? (input.turnId as RunId | undefined);+ if (runId === undefined) {+ const config = yield* getInitialServerConfig();+ if (config.environment.capabilities.threadStop === true) {+ // A paginated projection can omit commands still running from earlier turns.+ return yield* dispatch({+ type: "thread.stop",+ commandId: yield* allocateCommandId(input),+ threadId: input.threadId,+ });+ }+ }if (runId === undefined) {const projection = yield* getProjection(input.threadId);runId = projection.runs.findLast(diff --git a/packages/contracts/src/environment.test.ts b/packages/contracts/src/environment.test.tsindex ed6a67d6272..1d12a5d2335 100644--- a/packages/contracts/src/environment.test.ts+++ b/packages/contracts/src/environment.test.ts@@ -110,4 +110,14 @@ describe("ExecutionEnvironmentDescriptor", () => {}).capabilities.serverResolvedCommandContext,).toBe(true);});++ it("enables thread Stop only when advertised by the server", () => {+ expect(decodeDescriptor(descriptor).capabilities.threadStop).toBeUndefined();+ expect(+ decodeDescriptor({+ ...descriptor,+ capabilities: { ...descriptor.capabilities, threadStop: true },+ }).capabilities.threadStop,+ ).toBe(true);+ });});diff --git a/packages/contracts/src/environment.ts b/packages/contracts/src/environment.tsindex b78cf0f9997..3f909fad708 100644--- a/packages/contracts/src/environment.ts+++ b/packages/contracts/src/environment.ts@@ -125,6 +125,7 @@ export const ExecutionEnvironmentCapabilities = Schema.Struct({threadVisitedTracking: Schema.optionalKey(Schema.Boolean),threadPullRequestLinking: Schema.optionalKey(Schema.Boolean),serverResolvedCommandContext: Schema.optionalKey(Schema.Boolean),+ threadStop: Schema.optionalKey(Schema.Boolean),threadPullRequests: Schema.optionalKey(Schema.Boolean),threadPullRequestWatch: Schema.optionalKey(Schema.Boolean),pullRequestStackActions: Schema.optionalKey(Schema.Boolean),diff --git a/packages/contracts/src/orchestrationV2.test.ts b/packages/contracts/src/orchestrationV2.test.tsindex 3f4be6092a7..626635ccfda 100644--- a/packages/contracts/src/orchestrationV2.test.ts+++ b/packages/contracts/src/orchestrationV2.test.ts@@ -625,6 +625,20 @@ describe("orchestration V2 contracts", () => {expect(event.payload.id).toBe(RunId.make("run-1"));});+ it("accepts a thread Stop without a client-resolved run", () => {+ expect(+ decodeOrchestrationV2Command({+ type: "thread.stop",+ commandId: "stop-thread",+ threadId: "thread-1",+ }),+ ).toEqual({+ type: "thread.stop",+ commandId: "stop-thread",+ threadId: "thread-1",+ });+ });+it("decodes app-owned delegated task commands", () => {const command = decodeOrchestrationV2Command({type: "delegated_task.request",diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.tsindex 52037ace794..65a7f9f16e9 100644--- a/packages/contracts/src/orchestrationV2.ts+++ b/packages/contracts/src/orchestrationV2.ts@@ -2553,6 +2553,12 @@ export const OrchestrationV2StoredEventJson = Schema.Struct({export type OrchestrationV2StoredEventJson = typeof OrchestrationV2StoredEventJson.Type;export const OrchestrationV2Command = Schema.Union([+ Schema.Struct({+ type: Schema.Literal("thread.stop"),+ commandId: CommandId,+ threadId: ThreadId,+ reason: Schema.optional(Schema.String),+ }),Schema.Struct({type: Schema.Literal("thread.create"),...OrchestrationV2CreationFields,@@ -3036,12 +3042,6 @@ const OrchestrationV2InternalCommand = Schema.Union([}),),}),- Schema.Struct({- type: Schema.Literal("thread.stop"),- commandId: CommandId,- threadId: ThreadId,- reason: Schema.optional(Schema.String),- }),/*** Internal so no client can mark a request saved without the value being* stored.