Skip to content

Commit 9e1ea83

Browse files
t3dotggclaude
andcommitted
refactor(server): settle policy lives in ThreadManagementService
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ca9a5e0 commit 9e1ea83

4 files changed

Lines changed: 52 additions & 21 deletions

File tree

‎apps/server/src/mcp/toolkits/thread/handlers.ts‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,12 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
293293
t3_thread_organize: writesThread((input) =>
294294
Effect.gen(function* () {
295295
const { threads, projection, caller } = yield* readThread(input.threadId);
296-
// An agent settling its own thread is always mid-turn, and settling
297-
// stops the provider session. Its thread settles when the turn completes.
298-
if (
299-
input.action === "settle" &&
300-
caller?.id === projection.thread.id &&
301-
caller.activeRunId !== null
302-
) {
303-
yield* threads
304-
.settleAfterRun({
305-
projectId: caller.projectId,
306-
threadId: caller.id,
307-
runId: caller.activeRunId,
308-
})
309-
.pipe(Effect.ignoreCause({ log: true }), Effect.forkDetach);
310-
return { settlesWhenTurnEnds: true as const };
311-
}
312296
const common = { commandId: yield* newCommandId(), threadId: projection.thread.id };
297+
if (input.action === "settle") {
298+
return yield* threads
299+
.settleThread({ ...common, byOwnAgent: caller?.id === projection.thread.id })
300+
.pipe(Effect.mapError(dispatchFailure));
301+
}
313302
let command: OrchestrationV2Command;
314303
switch (input.action) {
315304
case "snooze":

‎apps/server/src/orchestration-v2/ThreadManagementService.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ export interface ThreadManagementServiceShape {
338338
readonly threadId: ThreadId;
339339
readonly runId: RunId;
340340
}) => Effect.Effect<void, ThreadManagementFailure>;
341+
/**
342+
* Settles a thread. When the thread's own agent asks, it is mid-turn, so
343+
* the thread settles through `settleAfterRun` once that turn ends.
344+
*/
345+
readonly settleThread: (input: {
346+
readonly threadId: ThreadId;
347+
readonly commandId: CommandId;
348+
readonly byOwnAgent: boolean;
349+
}) => Effect.Effect<
350+
{ readonly sequence: number } | { readonly settlesWhenTurnEnds: true },
351+
Orchestrator.OrchestratorV2Error
352+
>;
341353
readonly interruptThread: (
342354
input: ThreadManagementInterruptInput,
343355
) => Effect.Effect<ThreadManagementInterruptResult, ThreadManagementFailure>;
@@ -418,6 +430,7 @@ const SETTLE_AFTER_RUN_WAIT_MS = 24 * 60 * 60 * 1_000;
418430

419431
const make = Effect.gen(function* () {
420432
const orchestrator = yield* Orchestrator.OrchestratorV2;
433+
const layerScope = yield* Effect.scope;
421434
const legacyImporter = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
422435

423436
const ensureLegacyTranscript = Effect.fn(
@@ -751,6 +764,26 @@ const make = Effect.gen(function* () {
751764
});
752765
});
753766

767+
const settleThread: ThreadManagementServiceShape["settleThread"] = Effect.fn(
768+
"orchestrationV2.threadManagement.settleThread",
769+
)(function* (input) {
770+
const shell = input.byOwnAgent ? yield* orchestrator.getThreadShell(input.threadId) : null;
771+
if (shell != null && shell.activeRunId !== null) {
772+
yield* settleAfterRun({
773+
projectId: shell.projectId,
774+
threadId: shell.id,
775+
runId: shell.activeRunId,
776+
}).pipe(Effect.ignoreCause({ log: true }), Effect.forkIn(layerScope));
777+
return { settlesWhenTurnEnds: true } as const;
778+
}
779+
const result = yield* dispatch({
780+
type: "thread.settle",
781+
commandId: input.commandId,
782+
threadId: input.threadId,
783+
});
784+
return { sequence: result.sequence };
785+
});
786+
754787
const interruptThread: ThreadManagementServiceShape["interruptThread"] = (input) =>
755788
Effect.gen(function* () {
756789
const target = yield* getProjectThreadRecords(input, ["runs", "providerTurns"]);
@@ -857,6 +890,7 @@ const make = Effect.gen(function* () {
857890
sendToThread,
858891
waitForThread,
859892
settleAfterRun,
893+
settleThread,
860894
interruptThread,
861895
stopDelegatedTasks,
862896
getThreadEventSequence: orchestrator.getThreadEventSequence,

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
33733373
}),
33743374
);
33753375

3376-
it.effect("settles a thread after the run it waits on completes", () =>
3376+
it.effect("settles a thread its own agent settled once the turn completes", () =>
33773377
Effect.gen(function* () {
33783378
const orchestrator = yield* Orchestrator.OrchestratorV2;
33793379
const eventSink = yield* EventSink.EventSinkV2;
@@ -3410,9 +3410,16 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
34103410
const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0];
34113411
if (run === undefined) return yield* Effect.die(new Error("Run missing."));
34123412

3413-
const settling = yield* threadManagement
3414-
.settleAfterRun({ projectId, threadId, runId: run.id })
3415-
.pipe(Effect.forkChild);
3413+
const settled = yield* orchestrator
3414+
.streamStoredEventsFrom({ threadId, afterSequence: 0, eventType: "thread.settled" })
3415+
.pipe(Stream.runHead, Effect.forkChild);
3416+
const result = yield* threadManagement.settleThread({
3417+
threadId,
3418+
commandId: CommandId.make("runtime-layer-settle-after-run-settle"),
3419+
byOwnAgent: true,
3420+
});
3421+
assert.deepEqual(result, { settlesWhenTurnEnds: true });
3422+
assert.isNull((yield* orchestrator.getThreadProjection(threadId)).thread.settledOverride);
34163423
const now = yield* DateTime.now;
34173424
yield* eventSink.write({
34183425
commandId: CommandId.make("runtime-layer-settle-after-run-completed"),
@@ -3427,7 +3434,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
34273434
},
34283435
],
34293436
});
3430-
yield* Fiber.join(settling);
3437+
yield* Fiber.join(settled);
34313438

34323439
const projection = yield* orchestrator.getThreadProjection(threadId);
34333440
assert.equal(projection.thread.settledOverride, "settled");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
207207
sendToThread: unused,
208208
waitForThread: unused,
209209
settleAfterRun: unused,
210+
settleThread: unused,
210211
interruptThread: unused,
211212
stopDelegatedTasks: unused,
212213
getThreadEventSequence: unused,

0 commit comments

Comments
 (0)