Skip to content

Commit 03db756

Browse files
committed
fix(server): block auto-settle during background work
1 parent 40cb892 commit 03db756

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function makeOrchestrationLayer() {
6060
),
6161
OrchestrationProjectionSnapshotQueryLive,
6262
).pipe(
63-
Layer.provide(ThreadBackgroundLiveness.layer),
63+
Layer.provideMerge(ThreadBackgroundLiveness.layer),
6464
Layer.provide(ThreadPlanProgress.layer),
6565
Layer.provide(OrchestrationEventStoreLive),
6666
Layer.provideMerge(OrchestrationCommandReceiptRepositoryLive),
@@ -224,6 +224,7 @@ describe("OrchestrationEngine", () => {
224224
} satisfies OrchestrationProjectionPipelineShape),
225225
),
226226
Layer.provide(Layer.succeed(OrchestrationEventStore, eventStore)),
227+
Layer.provide(ThreadBackgroundLiveness.layer),
227228
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
228229
Layer.provide(SqlitePersistenceMemory),
229230
Layer.provideMerge(NodeServices.layer),
@@ -322,15 +323,17 @@ describe("OrchestrationEngine", () => {
322323
);
323324

324325
effectIt.effect(
325-
"rejects same-millisecond thread changes without blocking on unrelated threads",
326+
"rejects persisted changes and live background work without blocking unrelated threads",
326327
() =>
327328
Effect.gen(function* () {
328329
yield* TestClock.setTime(Date.parse(now()));
329330
const engine = yield* OrchestrationEngineService;
330331
const snapshots = yield* ProjectionSnapshotQuery;
332+
const backgroundLiveness = yield* ThreadBackgroundLiveness.ThreadBackgroundLivenessService;
331333
const projectId = ProjectId.make("project-auto-settle-guard");
332334
const guardedThreadId = ThreadId.make("thread-auto-settle-guarded");
333335
const unrelatedThreadId = ThreadId.make("thread-auto-settle-unrelated");
336+
const liveThreadId = ThreadId.make("thread-auto-settle-live");
334337

335338
yield* engine.dispatch({
336339
type: "project.create",
@@ -340,7 +343,7 @@ describe("OrchestrationEngine", () => {
340343
workspaceRoot: "/tmp/project-auto-settle-guard",
341344
createdAt: now(),
342345
});
343-
for (const threadId of [guardedThreadId, unrelatedThreadId]) {
346+
for (const threadId of [guardedThreadId, unrelatedThreadId, liveThreadId]) {
344347
yield* engine.dispatch({
345348
type: "thread.create",
346349
commandId: CommandId.make(`cmd-create-${threadId}`),
@@ -385,6 +388,43 @@ describe("OrchestrationEngine", () => {
385388
.pipe(Effect.flip);
386389
expect(staleError._tag).toBe("OrchestrationCommandInvariantError");
387390

391+
const livenessSnapshotSequence = yield* engine.latestSequence;
392+
for (const [taskType, expectedLiveness] of [
393+
["subagent", "working"],
394+
["local_bash", "monitoring"],
395+
] as const) {
396+
backgroundLiveness.recordTaskLiveness({
397+
threadId: liveThreadId,
398+
taskId: `task-${expectedLiveness}`,
399+
taskType,
400+
status: undefined,
401+
kind: "started",
402+
});
403+
expect(backgroundLiveness.getThreadBackgroundLiveness(liveThreadId)).toBe(
404+
expectedLiveness,
405+
);
406+
expect(yield* engine.latestSequence).toBe(livenessSnapshotSequence);
407+
408+
const livenessError = yield* engine
409+
.dispatch({
410+
type: "thread.auto-settle",
411+
commandId: CommandId.make(`cmd-auto-settle-${expectedLiveness}`),
412+
threadId: liveThreadId,
413+
snapshotSequence: livenessSnapshotSequence,
414+
})
415+
.pipe(Effect.flip);
416+
expect(livenessError._tag).toBe("OrchestrationCommandInvariantError");
417+
expect(yield* engine.latestSequence).toBe(livenessSnapshotSequence);
418+
backgroundLiveness.clearThreadLiveness(liveThreadId);
419+
}
420+
421+
yield* engine.dispatch({
422+
type: "thread.auto-settle",
423+
commandId: CommandId.make("cmd-auto-settle-after-liveness-cleared"),
424+
threadId: liveThreadId,
425+
snapshotSequence: livenessSnapshotSequence,
426+
});
427+
388428
const freshSnapshotSequence = yield* engine.latestSequence;
389429
yield* engine.dispatch({
390430
type: "thread.meta.update",
@@ -403,6 +443,9 @@ describe("OrchestrationEngine", () => {
403443
expect(
404444
settled.threads.find((thread) => thread.id === guardedThreadId)?.settledOverride,
405445
).toBe("settled");
446+
expect(settled.threads.find((thread) => thread.id === liveThreadId)?.settledOverride).toBe(
447+
"settled",
448+
);
406449
}).pipe(Effect.provide(makeOrchestrationLayer())),
407450
);
408451

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { decideOrchestrationCommand } from "../decider.ts";
4444
import { createEmptyReadModel, projectEvent } from "../projector.ts";
4545
import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts";
4646
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
47+
import { ThreadBackgroundLivenessService } from "../ThreadBackgroundLiveness.ts";
4748
import {
4849
OrchestrationEngineService,
4950
type OrchestrationEngineShape,
@@ -86,6 +87,7 @@ const makeOrchestrationEngine = Effect.gen(function* () {
8687
const commandReceiptRepository = yield* OrchestrationCommandReceiptRepository;
8788
const projectionPipeline = yield* OrchestrationProjectionPipeline;
8889
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
90+
const threadBackgroundLiveness = yield* ThreadBackgroundLivenessService;
8991
const crypto = yield* Crypto.Crypto;
9092

9193
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
@@ -183,6 +185,16 @@ const makeOrchestrationEngine = Effect.gen(function* () {
183185
});
184186
}
185187

188+
if (
189+
envelope.command.type === "thread.auto-settle" &&
190+
threadBackgroundLiveness.getThreadBackgroundLiveness(envelope.command.threadId) !== null
191+
) {
192+
return yield* new OrchestrationCommandInvariantError({
193+
commandType: envelope.command.type,
194+
detail: `thread ${envelope.command.threadId} has live background work`,
195+
});
196+
}
197+
186198
const eventBase = yield* decideOrchestrationCommand({
187199
command: envelope.command,
188200
readModel: commandReadModel,

0 commit comments

Comments
 (0)