Skip to content

Commit f4802ad

Browse files
committed
fix(server): classify guarded stop rejections
1 parent d3cc5b8 commit f4802ad

4 files changed

Lines changed: 128 additions & 2 deletions

File tree

‎apps/server/src/orchestration/decider.settled.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,28 @@ it.layer(NodeServices.layer)("settled thread decider", (it) => {
898898
}),
899899
);
900900

901+
it.effect("rejects a guarded stop from a future snapshot", () =>
902+
Effect.gen(function* () {
903+
const error = yield* decideOrchestrationCommand({
904+
command: {
905+
type: "thread.session.stop",
906+
commandId: CommandId.make("cmd-stop-future-snapshot"),
907+
threadId: ThreadId.make("thread-1"),
908+
createdAt: NOW,
909+
onlyIfIdle: true,
910+
snapshotSequence: 1,
911+
expectedProviderName: ProviderDriverKind.make("codex"),
912+
},
913+
readModel: makeReadModel("settled", null, {
914+
...makeSession("ready"),
915+
providerName: "codex",
916+
}),
917+
}).pipe(Effect.flip);
918+
919+
expect(error._tag).toBe("OrchestrationCommandInvariantError");
920+
}),
921+
);
922+
901923
it.effect("rejects guarded stops for the wrong provider or an active turn", () =>
902924
Effect.gen(function* () {
903925
const command = {

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
16931693
if (command.onlyIfIdle === true) {
16941694
if (
16951695
command.snapshotSequence === undefined ||
1696+
command.snapshotSequence > readModel.snapshotSequence ||
16961697
!canStopThreadSessionIfIdle({
16971698
expectedProviderName: command.expectedProviderName,
16981699
session: thread.session,

‎apps/server/src/orchestration/http.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import {
22
AuthOrchestrationOperateScope,
33
AuthOrchestrationReadScope,
4+
EnvironmentInternalError,
45
EnvironmentHttpApi,
6+
EnvironmentRequestInvalidError,
57
} from "@t3tools/contracts";
68
import * as Effect from "effect/Effect";
79
import * as Option from "effect/Option";
10+
import * as Schema from "effect/Schema";
811
import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
912

1013
import { projectThreadDetailSnapshot } from "./ActivityPayloadProjection.ts";
@@ -18,6 +21,12 @@ import {
1821
} from "../auth/http.ts";
1922
import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";
2023
import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
24+
import {
25+
isOrchestrationCommandRejection,
26+
OrchestrationCommandPreviouslyRejectedError,
27+
} from "./Errors.ts";
28+
29+
const isPreviouslyRejectedCommand = Schema.is(OrchestrationCommandPreviouslyRejectedError);
2130

2231
export const orchestrationHttpApiLayer = HttpApiBuilder.group(
2332
EnvironmentHttpApi,
@@ -100,8 +109,22 @@ export const orchestrationHttpApiLayer = HttpApiBuilder.group(
100109
Effect.tapError(() =>
101110
cleanupFailedUploadedAttachments(args.payload, normalizedCommand),
102111
),
103-
Effect.catch((cause) =>
104-
failEnvironmentInternal("orchestration_dispatch_failed", cause),
112+
Effect.catch(
113+
(
114+
cause,
115+
): Effect.Effect<
116+
never,
117+
EnvironmentInternalError | EnvironmentRequestInvalidError
118+
> => {
119+
if (
120+
normalizedCommand.type === "thread.session.stop" &&
121+
normalizedCommand.onlyIfIdle === true &&
122+
(isOrchestrationCommandRejection(cause) || isPreviouslyRejectedCommand(cause))
123+
) {
124+
return failEnvironmentInvalidRequest("invalid_command");
125+
}
126+
return failEnvironmentInternal("orchestration_dispatch_failed", cause);
127+
},
105128
),
106129
);
107130
}),

‎apps/server/src/server.test.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ import * as ExternalLauncher from "./process/externalLauncher.ts";
114114
import * as RemoteOpenTargets from "./environment/RemoteOpenTargets.ts";
115115
import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
116116
import {
117+
OrchestrationCommandInvariantError,
118+
OrchestrationCommandPreviouslyRejectedError,
117119
OrchestrationListenerCallbackError,
118120
OrchestrationThreadSettleBlockedError,
119121
} from "./orchestration/Errors.ts";
@@ -2109,6 +2111,84 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
21092111
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
21102112
);
21112113

2114+
it.effect("reports guarded command rejections as invalid requests", () =>
2115+
Effect.gen(function* () {
2116+
yield* buildAppUnderTest({
2117+
layers: {
2118+
orchestrationEngine: {
2119+
dispatch: (command) =>
2120+
Effect.fail(
2121+
new OrchestrationCommandInvariantError({
2122+
commandType: command.type,
2123+
detail: "thread is no longer idle",
2124+
}),
2125+
),
2126+
},
2127+
},
2128+
});
2129+
2130+
const response = yield* fetchEffect(yield* getHttpServerUrl("/api/orchestration/dispatch"), {
2131+
method: "POST",
2132+
headers: {
2133+
cookie: yield* getAuthenticatedSessionCookieHeader(),
2134+
"content-type": "application/json",
2135+
},
2136+
body: jsonRequestBody({
2137+
type: "thread.session.stop",
2138+
commandId: "cmd-guarded-stop-rejected",
2139+
threadId: "thread-guarded-stop-rejected",
2140+
createdAt: "2026-09-11T10:01:00.000Z",
2141+
onlyIfIdle: true,
2142+
snapshotSequence: 7,
2143+
expectedProviderName: "codex",
2144+
}),
2145+
});
2146+
const body = yield* responseJsonEffect<{ readonly reason: string }>(response);
2147+
2148+
assert.equal(response.status, 400);
2149+
assert.equal(body.reason, "invalid_command");
2150+
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
2151+
);
2152+
2153+
it.effect("reports replayed guarded command rejections as invalid requests", () =>
2154+
Effect.gen(function* () {
2155+
yield* buildAppUnderTest({
2156+
layers: {
2157+
orchestrationEngine: {
2158+
dispatch: (command) =>
2159+
Effect.fail(
2160+
new OrchestrationCommandPreviouslyRejectedError({
2161+
commandId: command.commandId,
2162+
detail: "thread was no longer idle",
2163+
}),
2164+
),
2165+
},
2166+
},
2167+
});
2168+
2169+
const response = yield* fetchEffect(yield* getHttpServerUrl("/api/orchestration/dispatch"), {
2170+
method: "POST",
2171+
headers: {
2172+
cookie: yield* getAuthenticatedSessionCookieHeader(),
2173+
"content-type": "application/json",
2174+
},
2175+
body: jsonRequestBody({
2176+
type: "thread.session.stop",
2177+
commandId: "cmd-guarded-stop-replayed",
2178+
threadId: "thread-guarded-stop-replayed",
2179+
createdAt: "2026-09-11T10:01:00.000Z",
2180+
onlyIfIdle: true,
2181+
snapshotSequence: 7,
2182+
expectedProviderName: "codex",
2183+
}),
2184+
});
2185+
const body = yield* responseJsonEffect<{ readonly reason: string }>(response);
2186+
2187+
assert.equal(response.status, 400);
2188+
assert.equal(body.reason, "invalid_command");
2189+
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
2190+
);
2191+
21122192
it.effect("serves snapshots for MCP handoff thread IDs above the router default", () =>
21132193
Effect.gen(function* () {
21142194
const threadId = ThreadId.make(

0 commit comments

Comments
 (0)