Skip to content

Commit d3cc5b8

Browse files
committed
feat(server): add guarded idle session stops
1 parent 57aee3e commit d3cc5b8

13 files changed

Lines changed: 493 additions & 12 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
171171
expect(second.capabilities.threadTitleRegeneration).toBe(true);
172172
expect(second.capabilities.threadPullRequests).toBe(true);
173173
expect(second.capabilities.threadPullRequestLinking).toBe(true);
174+
expect(second.capabilities.guardedSessionStop).toBe(true);
174175
expect(second.capabilities.agentActivityPublishing).toBe(false);
175176
}),
176177
);

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export const make = Effect.gen(function* () {
222222
threadSettlement: true,
223223
threadAutoSettlement: true,
224224
threadRestartContinuation: true,
225+
guardedSessionStop: true,
225226
threadSnooze: true,
226227
environmentThemes: true,
227228
usageLimitSources: true,

‎apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
type OrchestrationCommand,
1717
type OrchestrationEvent,
1818
ProviderInstanceId,
19+
ProviderDriverKind,
1920
} from "@t3tools/contracts";
2021
import * as NodeServices from "@effect/platform-node/NodeServices";
2122
import { it as effectIt } from "@effect/vitest";
@@ -697,6 +698,98 @@ describe("OrchestrationEngine", () => {
697698
}).pipe(Effect.provide(makeOrchestrationLayer())),
698699
);
699700

701+
effectIt.effect("rejects a guarded session stop from a stale thread snapshot", () =>
702+
Effect.gen(function* () {
703+
const engine = yield* OrchestrationEngineService;
704+
const backgroundLiveness = yield* ThreadBackgroundLiveness.ThreadBackgroundLivenessService;
705+
const projectId = ProjectId.make("project-guarded-stop");
706+
const threadId = ThreadId.make("thread-guarded-stop");
707+
yield* engine.dispatch({
708+
type: "project.create",
709+
commandId: CommandId.make("cmd-guarded-stop-project"),
710+
projectId,
711+
title: "Project",
712+
workspaceRoot: "/tmp/project-guarded-stop",
713+
createdAt: now(),
714+
});
715+
yield* engine.dispatch({
716+
type: "thread.create",
717+
commandId: CommandId.make("cmd-guarded-stop-thread"),
718+
threadId,
719+
projectId,
720+
title: "Thread",
721+
modelSelection: {
722+
instanceId: ProviderInstanceId.make("codex"),
723+
model: "gpt-5-codex",
724+
},
725+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
726+
runtimeMode: "full-access",
727+
branch: null,
728+
worktreePath: null,
729+
createdAt: now(),
730+
});
731+
yield* engine.dispatch({
732+
type: "thread.session.set",
733+
commandId: CommandId.make("cmd-guarded-stop-session"),
734+
threadId,
735+
session: {
736+
threadId,
737+
status: "ready",
738+
providerName: "codex",
739+
providerInstanceId: ProviderInstanceId.make("codex"),
740+
runtimeMode: "full-access",
741+
activeTurnId: null,
742+
lastError: null,
743+
updatedAt: now(),
744+
},
745+
createdAt: now(),
746+
});
747+
const snapshotSequence = yield* engine.latestSequence;
748+
yield* engine.dispatch({
749+
type: "thread.meta.update",
750+
commandId: CommandId.make("cmd-guarded-stop-update"),
751+
threadId,
752+
title: "Changed",
753+
});
754+
755+
const error = yield* engine
756+
.dispatch({
757+
type: "thread.session.stop",
758+
commandId: CommandId.make("cmd-guarded-stop-stale"),
759+
threadId,
760+
createdAt: now(),
761+
onlyIfIdle: true,
762+
snapshotSequence,
763+
expectedProviderName: ProviderDriverKind.make("codex"),
764+
})
765+
.pipe(Effect.flip);
766+
767+
expect(error._tag).toBe("OrchestrationCommandInvariantError");
768+
769+
const freshSequence = yield* engine.latestSequence;
770+
backgroundLiveness.recordTaskLiveness({
771+
threadId,
772+
taskId: "guarded-stop-background-task",
773+
taskType: "subagent",
774+
status: undefined,
775+
kind: "started",
776+
});
777+
const backgroundError = yield* engine
778+
.dispatch({
779+
type: "thread.session.stop",
780+
commandId: CommandId.make("cmd-guarded-stop-background"),
781+
threadId,
782+
createdAt: now(),
783+
onlyIfIdle: true,
784+
snapshotSequence: freshSequence,
785+
expectedProviderName: ProviderDriverKind.make("codex"),
786+
})
787+
.pipe(Effect.flip);
788+
expect(backgroundError._tag).toBe("OrchestrationCommandInvariantError");
789+
backgroundLiveness.clearThreadLiveness(threadId);
790+
}).pipe(Effect.provide(makeOrchestrationLayer())),
791+
);
792+
700793
it("persists deterministic read models for repeated snapshot reads", async () => {
701794
const createdAt = now();
702795
const system = await createOrchestrationSystem();

‎apps/server/src/orchestration/Layers/OrchestrationEngine.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ const makeOrchestrationEngine = Effect.gen(function* () {
185185
});
186186
}
187187

188+
if (
189+
envelope.command.type === "thread.session.stop" &&
190+
envelope.command.onlyIfIdle === true &&
191+
envelope.command.snapshotSequence !== undefined &&
192+
(yield* eventStore.hasEventAfter({
193+
aggregateKind: "thread",
194+
aggregateId: envelope.command.threadId,
195+
sequenceExclusive: envelope.command.snapshotSequence,
196+
}))
197+
) {
198+
return yield* new OrchestrationCommandInvariantError({
199+
commandType: envelope.command.type,
200+
detail: `thread ${envelope.command.threadId} changed before guarded session stop`,
201+
});
202+
}
203+
188204
// The decider compares the lookup inputs. Only recreation needs an
189205
// event check, since it can reset a thread to the same field values.
190206
if (
@@ -211,6 +227,16 @@ const makeOrchestrationEngine = Effect.gen(function* () {
211227
detail: `thread ${envelope.command.threadId} has live background work`,
212228
});
213229
}
230+
if (
231+
envelope.command.type === "thread.session.stop" &&
232+
envelope.command.onlyIfIdle === true &&
233+
threadBackgroundLiveness.getThreadBackgroundLiveness(envelope.command.threadId) !== null
234+
) {
235+
return yield* new OrchestrationCommandInvariantError({
236+
commandType: envelope.command.type,
237+
detail: `thread ${envelope.command.threadId} has live background work`,
238+
});
239+
}
214240

215241
// New and moved projects do not carry a resolved identity in the event-derived
216242
// command model. Legacy PR edits need it to identify the link they replace.

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ describe("ProviderCommandReactor", () => {
173173
readonly unreadableHistory?: boolean;
174174
readonly titleRegenerationCompletionDispatchFailures?: number;
175175
readonly titleRegenerationBeforeStart?: "one" | "two";
176+
readonly startReactor?: boolean;
176177
readonly serverActivation?: Effect.Effect<void>;
177178
readonly beforeReadySessionDispatch?: () => Effect.Effect<void>;
178179
readonly beforeTurnStartDispatch?: () => Effect.Effect<void>;
@@ -191,6 +192,11 @@ describe("ProviderCommandReactor", () => {
191192
createdBaseDirs.add(baseDir);
192193
const { stateDir } = deriveServerPathsSync(baseDir, undefined);
193194
createdStateDirs.add(stateDir);
195+
const backgroundLiveness = ThreadBackgroundLiveness.make();
196+
const backgroundLivenessLayer = Layer.succeed(
197+
ThreadBackgroundLiveness.ThreadBackgroundLivenessService,
198+
backgroundLiveness,
199+
);
194200
const runtimeEventPubSub = Effect.runSync(PubSub.unbounded<ProviderRuntimeEvent>());
195201
const tryHandlePromptCommand = vi.fn<ProviderAuthService["Service"]["tryHandlePromptCommand"]>(
196202
input?.tryHandlePromptCommandEffect ?? (() => Effect.succeed(false)),
@@ -400,7 +406,7 @@ describe("ProviderCommandReactor", () => {
400406

401407
const orchestrationLayer = OrchestrationEngineLive.pipe(
402408
Layer.provide(OrchestrationProjectionSnapshotQueryLive),
403-
Layer.provide(ThreadBackgroundLiveness.layer),
409+
Layer.provide(backgroundLivenessLayer),
404410
Layer.provide(ThreadPlanProgress.layer),
405411
Layer.provide(OrchestrationProjectionPipelineLive),
406412
Layer.provide(OrchestrationEventStoreLive),
@@ -409,7 +415,7 @@ describe("ProviderCommandReactor", () => {
409415
Layer.provide(SqlitePersistenceMemory),
410416
);
411417
const projectionSnapshotLayer = OrchestrationProjectionSnapshotQueryLive.pipe(
412-
Layer.provide(ThreadBackgroundLiveness.layer),
418+
Layer.provide(backgroundLivenessLayer),
413419
Layer.provide(ThreadPlanProgress.layer),
414420
Layer.provide(RepositoryIdentityResolver.layer),
415421
Layer.provide(SqlitePersistenceMemory),
@@ -488,6 +494,7 @@ describe("ProviderCommandReactor", () => {
488494
}),
489495
),
490496
Layer.provideMerge(ServerSettingsService.layerTest()),
497+
Layer.provideMerge(backgroundLivenessLayer),
491498
Layer.provideMerge(SqlitePersistenceMemory),
492499
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)),
493500
Layer.provideMerge(NodeServices.layer),
@@ -579,14 +586,18 @@ describe("ProviderCommandReactor", () => {
579586
}
580587

581588
scope = await Effect.runPromise(Scope.make("sequential"));
582-
await Effect.runPromise(
583-
reactor
584-
.start()
585-
.pipe(
586-
Scope.provide(scope),
587-
Effect.provideService(ServerActivation, input?.serverActivation),
588-
),
589-
);
589+
const start = () =>
590+
Effect.runPromise(
591+
reactor
592+
.start()
593+
.pipe(
594+
Scope.provide(scope!),
595+
Effect.provideService(ServerActivation, input?.serverActivation),
596+
),
597+
);
598+
if (input?.startReactor !== false) {
599+
await start();
600+
}
590601
const drain = () => Effect.runPromise(reactor.drain);
591602

592603
return {
@@ -620,6 +631,8 @@ describe("ProviderCommandReactor", () => {
620631
generateThreadTitle,
621632
runtimeSessions,
622633
stateDir,
634+
backgroundLiveness,
635+
start,
623636
drain,
624637
runEffect,
625638
get titleRegenerationCompletionDispatchAttempts() {
@@ -4130,6 +4143,56 @@ describe("ProviderCommandReactor", () => {
41304143
}),
41314144
);
41324145

4146+
effectIt.effect("skips a guarded stop when background work appears before execution", () =>
4147+
Effect.gen(function* () {
4148+
const harness = yield* Effect.promise(() => createHarness({ startReactor: false }));
4149+
const threadId = ThreadId.make("thread-1");
4150+
const now = "2026-01-01T00:00:00.000Z";
4151+
yield* harness.engine.dispatch({
4152+
type: "thread.session.set",
4153+
commandId: CommandId.make("cmd-session-set-before-guarded-stop"),
4154+
threadId,
4155+
session: {
4156+
threadId,
4157+
status: "ready",
4158+
providerName: "codex",
4159+
providerInstanceId: ProviderInstanceId.make("codex"),
4160+
runtimeMode: "approval-required",
4161+
activeTurnId: null,
4162+
lastError: null,
4163+
updatedAt: now,
4164+
},
4165+
createdAt: now,
4166+
});
4167+
const snapshotSequence = yield* harness.engine.latestSequence;
4168+
yield* harness.engine.dispatch({
4169+
type: "thread.session.stop",
4170+
commandId: CommandId.make("cmd-guarded-stop-before-background"),
4171+
threadId,
4172+
createdAt: now,
4173+
onlyIfIdle: true,
4174+
snapshotSequence,
4175+
expectedProviderName: ProviderDriverKind.make("codex"),
4176+
});
4177+
harness.backgroundLiveness.recordTaskLiveness({
4178+
threadId,
4179+
taskId: "guarded-stop-background-task",
4180+
taskType: "subagent",
4181+
status: undefined,
4182+
kind: "started",
4183+
});
4184+
4185+
yield* Effect.promise(() => harness.start());
4186+
yield* Effect.promise(() => harness.drain());
4187+
4188+
expect(harness.stopSession).not.toHaveBeenCalled();
4189+
const thread = yield* harness.snapshotQuery
4190+
.getThreadShellById(threadId)
4191+
.pipe(Effect.map(Option.getOrThrow));
4192+
expect(thread.session?.status).toBe("ready");
4193+
}),
4194+
);
4195+
41334196
effectIt.effect("stops a ready provider session after automatic settlement", () =>
41344197
Effect.gen(function* () {
41354198
const sessionStopped = yield* Deferred.make<void>();

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import {
5050
} from "../Services/ProviderCommandReactor.ts";
5151
import { forkParked, ServerActivation } from "../../serverActivation.ts";
5252
import { canReplaceThreadTitle, DEFAULT_THREAD_TITLE } from "../threadTitles.ts";
53+
import { canStopThreadSessionIfIdle } from "../SessionStopPolicy.ts";
54+
import { threadHasQueuedTurnStart } from "../ThreadSettlementPolicy.ts";
5355
import {
5456
resolveSourceControlWriterModelSelection,
5557
ServerSettingsService,
@@ -1757,6 +1759,29 @@ const make = Effect.gen(function* () {
17571759
}
17581760

17591761
const now = event.payload.createdAt;
1762+
if (
1763+
event.payload.onlyIfIdle === true &&
1764+
!canStopThreadSessionIfIdle({
1765+
expectedProviderName: event.payload.expectedProviderName,
1766+
session: thread.session,
1767+
latestTurnState: thread.latestTurn?.state ?? null,
1768+
hasQueuedTurnStart: threadHasQueuedTurnStart(
1769+
thread,
1770+
DateTime.formatIso(yield* DateTime.now),
1771+
),
1772+
hasPendingRequests: thread.hasPendingApprovals || thread.hasPendingUserInput,
1773+
backgroundLiveness: thread.backgroundLiveness ?? null,
1774+
})
1775+
) {
1776+
if (thread.session !== null) {
1777+
yield* setThreadSession({
1778+
threadId: thread.id,
1779+
session: thread.session,
1780+
createdAt: DateTime.formatIso(yield* DateTime.now),
1781+
});
1782+
}
1783+
return;
1784+
}
17601785
const wasCompacting = compactingThreadIds.has(thread.id);
17611786
stoppingThreadIds.add(thread.id);
17621787
const clearStopping = Effect.sync(() => void stoppingThreadIds.delete(thread.id));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ProviderDriverKind, ProviderInstanceId, ThreadId, TurnId } from "@t3tools/contracts";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { canStopThreadSessionIfIdle } from "./SessionStopPolicy.ts";
5+
6+
const idle = {
7+
expectedProviderName: ProviderDriverKind.make("codex"),
8+
session: {
9+
threadId: ThreadId.make("thread-1"),
10+
status: "ready" as const,
11+
providerName: "codex",
12+
providerInstanceId: ProviderInstanceId.make("codex"),
13+
runtimeMode: "full-access" as const,
14+
activeTurnId: null,
15+
lastError: null,
16+
updatedAt: "2026-09-11T00:00:00.000Z",
17+
},
18+
latestTurnState: null,
19+
hasQueuedTurnStart: false,
20+
hasPendingRequests: false,
21+
backgroundLiveness: null,
22+
};
23+
24+
describe("guarded Codex session stop policy", () => {
25+
it("allows the four non-working session states", () => {
26+
for (const status of ["ready", "idle", "interrupted", "error"] as const) {
27+
expect(canStopThreadSessionIfIdle({ ...idle, session: { ...idle.session, status } })).toBe(
28+
true,
29+
);
30+
}
31+
});
32+
33+
it("rejects every signal that work is active or belongs to another provider", () => {
34+
const unsafe = [
35+
{ ...idle, expectedProviderName: ProviderDriverKind.make("claudeAgent") },
36+
{ ...idle, session: { ...idle.session, providerName: "claudeAgent" } },
37+
{ ...idle, session: { ...idle.session, status: "starting" as const } },
38+
{ ...idle, session: { ...idle.session, status: "running" as const } },
39+
{ ...idle, session: { ...idle.session, activeTurnId: TurnId.make("turn-1") } },
40+
{ ...idle, latestTurnState: "running" as const },
41+
{ ...idle, hasQueuedTurnStart: true },
42+
{ ...idle, hasPendingRequests: true },
43+
{ ...idle, backgroundLiveness: "working" as const },
44+
{ ...idle, backgroundLiveness: "monitoring" as const },
45+
];
46+
47+
for (const input of unsafe) {
48+
expect(canStopThreadSessionIfIdle(input)).toBe(false);
49+
}
50+
});
51+
});

0 commit comments

Comments
 (0)