Skip to content

Commit 003a5a6

Browse files
kakaponickclaude
andcommitted
fix(cli): keep the new thread when a turn start times out
A timeout or dropped connection does not mean the server rejected the turn; it may already be running. Roll back the created thread only on a declared server rejection. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 35fca9d commit 003a5a6

2 files changed

Lines changed: 104 additions & 13 deletions

File tree

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

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
2121
import { assert, it } from "@effect/vitest";
2222
import * as Effect from "effect/Effect";
2323
import * as DateTime from "effect/DateTime";
24+
import * as Deferred from "effect/Deferred";
25+
import * as Fiber from "effect/Fiber";
2426
import * as Layer from "effect/Layer";
2527
import * as Stdio from "effect/Stdio";
2628
import * as Stream from "effect/Stream";
@@ -29,6 +31,7 @@ import * as HttpServer from "effect/unstable/http/HttpServer";
2931
import * as HttpApi from "effect/unstable/httpapi/HttpApi";
3032
import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
3133
import * as CliError from "effect/unstable/cli/CliError";
34+
import * as TestClock from "effect/testing/TestClock";
3235
import * as TestConsole from "effect/testing/TestConsole";
3336
import { Command } from "effect/unstable/cli";
3437

@@ -43,6 +46,7 @@ import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
4346
import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
4447
import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
4548
import { OrchestrationLayerLive } from "./orchestration/runtimeLayer.ts";
49+
import { OrchestrationCommandInvariantError } from "./orchestration/Errors.ts";
4650
import { orchestrationHttpApiLayer } from "./orchestration/http.ts";
4751
import * as ProjectCloneTracker from "./project/ProjectCloneTracker.ts";
4852
import { layerConfig as SqlitePersistenceLayerLive } from "./persistence/Layers/Sqlite.ts";
@@ -366,9 +370,25 @@ it.layer(NodeServices.layer)("project lookup with unavailable workspaces", (it)
366370
);
367371
});
368372

369-
const withLiveProjectCliServer = <A, E, R>(baseDir: string, run: () => Effect.Effect<A, E, R>) =>
373+
type EngineDispatch = OrchestrationEngine.OrchestrationEngineService["Service"]["dispatch"];
374+
375+
const withLiveProjectCliServer = <A, E, R>(
376+
baseDir: string,
377+
run: () => Effect.Effect<A, E, R>,
378+
wrapDispatch?: (dispatch: EngineDispatch) => EngineDispatch,
379+
) =>
370380
Effect.gen(function* () {
371381
const config = yield* makeCliTestServerConfig(baseDir);
382+
const dispatchOverrideLayer =
383+
wrapDispatch === undefined
384+
? Layer.empty
385+
: Layer.effect(
386+
OrchestrationEngine.OrchestrationEngineService,
387+
Effect.map(OrchestrationEngine.OrchestrationEngineService, (engine) => ({
388+
...engine,
389+
dispatch: wrapDispatch(engine.dispatch),
390+
})),
391+
);
372392
const routesLayer = HttpApiBuilder.layer(ProjectCliHttpApi).pipe(
373393
Layer.provide(
374394
orchestrationHttpApiLayer.pipe(
@@ -378,6 +398,7 @@ const withLiveProjectCliServer = <A, E, R>(baseDir: string, run: () => Effect.Ef
378398
discard: () => Effect.void,
379399
}),
380400
),
401+
Layer.provide(dispatchOverrideLayer),
381402
),
382403
),
383404
Layer.provide(environmentAuthenticatedAuthLayer),
@@ -900,6 +921,7 @@ const withThreadStartFixture = <A, E, R>(
900921
readonly baseDir: string;
901922
readonly workspaceRoot: string;
902923
}) => Effect.Effect<A, E, R>,
924+
wrapDispatch?: (dispatch: EngineDispatch) => EngineDispatch,
903925
) =>
904926
Effect.gen(function* () {
905927
const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cli-thread-state-"));
@@ -936,7 +958,11 @@ const withThreadStartFixture = <A, E, R>(
936958
}
937959
}`,
938960
);
939-
return yield* withLiveProjectCliServer(baseDir, () => run({ baseDir, workspaceRoot }));
961+
return yield* withLiveProjectCliServer(
962+
baseDir,
963+
() => run({ baseDir, workspaceRoot }),
964+
wrapDispatch,
965+
);
940966
});
941967

942968
const readProjectThreads = (workspaceRoot: string) =>
@@ -1165,3 +1191,66 @@ it.layer(NodeServices.layer)("thread send", (it) => {
11651191
),
11661192
);
11671193
});
1194+
1195+
it.layer(NodeServices.layer)("thread start rollback", (it) => {
1196+
const startArgs = (baseDir: string, workspaceRoot: string) => [
1197+
"thread",
1198+
"start",
1199+
workspaceRoot,
1200+
"Fix the flaky test",
1201+
"--base-dir",
1202+
baseDir,
1203+
];
1204+
1205+
it.effect("deletes the new thread when the server rejects the turn", () =>
1206+
withThreadStartFixture(
1207+
({ baseDir, workspaceRoot }) =>
1208+
Effect.gen(function* () {
1209+
yield* runCliWithRuntime(startArgs(baseDir, workspaceRoot)).pipe(Effect.flip);
1210+
1211+
const threads = yield* readProjectThreads(workspaceRoot);
1212+
assert.lengthOf(
1213+
threads.filter((thread) => thread.deletedAt === null),
1214+
0,
1215+
);
1216+
}),
1217+
(dispatch) => (command, options) =>
1218+
command.type === "thread.turn.start"
1219+
? Effect.fail(
1220+
new OrchestrationCommandInvariantError({
1221+
commandType: command.type,
1222+
detail: "Rejected by test.",
1223+
}),
1224+
)
1225+
: dispatch(command, options),
1226+
),
1227+
);
1228+
1229+
it.effect("keeps the thread when the turn outcome is unknown", () =>
1230+
Effect.gen(function* () {
1231+
const turnAccepted = yield* Deferred.make<void>();
1232+
yield* withThreadStartFixture(
1233+
({ baseDir, workspaceRoot }) =>
1234+
Effect.gen(function* () {
1235+
const starting = yield* Effect.forkChild(
1236+
runCliWithRuntime(startArgs(baseDir, workspaceRoot)).pipe(Effect.flip),
1237+
);
1238+
yield* Deferred.await(turnAccepted);
1239+
yield* TestClock.adjust("10 seconds");
1240+
yield* Fiber.join(starting);
1241+
1242+
const [thread] = yield* readProjectThreads(workspaceRoot);
1243+
assert.isNull(thread?.deletedAt);
1244+
assert.equal(thread?.messages[0]?.text, "Fix the flaky test");
1245+
}),
1246+
(dispatch) => (command, options) =>
1247+
command.type === "thread.turn.start"
1248+
? dispatch(command, options).pipe(
1249+
Effect.tap(() => Deferred.succeed(turnAccepted, undefined)),
1250+
Effect.andThen(Effect.never),
1251+
)
1252+
: dispatch(command, options),
1253+
);
1254+
}),
1255+
);
1256+
});

‎apps/server/src/cli/thread.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,19 @@ const runThreadStart = Effect.fn("runThreadStart")(function* (flags: {
415415
createdAt,
416416
})
417417
.pipe(
418-
Effect.tapError(() =>
419-
threadCommandUuid.pipe(
420-
Effect.flatMap((commandId) =>
421-
server.dispatch({
422-
type: "thread.delete",
423-
commandId: CommandId.make(commandId),
424-
threadId,
425-
}),
426-
),
427-
Effect.ignore({ log: true }),
428-
),
418+
Effect.tapError((error) =>
419+
error._tag === "ProjectLiveServerDeclaredResponseError"
420+
? threadCommandUuid.pipe(
421+
Effect.flatMap((commandId) =>
422+
server.dispatch({
423+
type: "thread.delete",
424+
commandId: CommandId.make(commandId),
425+
threadId,
426+
}),
427+
),
428+
Effect.ignore({ log: true }),
429+
)
430+
: Effect.void,
429431
),
430432
);
431433

0 commit comments

Comments
 (0)