Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
563808b6cf | ||
|
|
90f9c9a45a | ||
|
|
8c45cf1167 |
No files matched your search
@@ -8,13 +8,17 @@ import {
|
||||
TurnId,
|
||||
type OrchestrationEvent,
|
||||
} from "@t3tools/contracts";
|
||||
import { Effect, Layer, ManagedRuntime, Queue, Stream } from "effect";
|
||||
import { Effect, Layer, ManagedRuntime, Option, Queue, Stream } from "effect";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { PersistenceSqlError } from "../../persistence/Errors.ts";
|
||||
import { OrchestrationCommandReceiptRepositoryLive } from "../../persistence/Layers/OrchestrationCommandReceipts.ts";
|
||||
import { OrchestrationEventStoreLive } from "../../persistence/Layers/OrchestrationEventStore.ts";
|
||||
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
|
||||
import {
|
||||
OrchestrationCommandReceiptRepository,
|
||||
type OrchestrationCommandReceiptRepositoryShape,
|
||||
} from "../../persistence/Services/OrchestrationCommandReceipts.ts";
|
||||
import {
|
||||
OrchestrationEventStore,
|
||||
type OrchestrationEventStoreShape,
|
||||
@@ -488,6 +492,101 @@ describe("OrchestrationEngine", () => {
|
||||
await runtime.dispose();
|
||||
});
|
||||
|
||||
it("keeps processing queued commands after a receipt lookup failure", async () => {
|
||||
const failingReceiptRepository: OrchestrationCommandReceiptRepositoryShape = {
|
||||
upsert: () => Effect.void,
|
||||
getByCommandId: ({ commandId }) => {
|
||||
if (commandId === CommandId.makeUnsafe("cmd-receipt-fail")) {
|
||||
return Effect.fail(
|
||||
new PersistenceSqlError({
|
||||
operation: "test.getByCommandId",
|
||||
detail: "receipt lookup failed",
|
||||
}),
|
||||
);
|
||||
}
|
||||
return Effect.succeed(Option.none());
|
||||
},
|
||||
};
|
||||
|
||||
const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
|
||||
prefix: "t3-orchestration-engine-test-",
|
||||
});
|
||||
|
||||
const runtime = ManagedRuntime.make(
|
||||
OrchestrationEngineLive.pipe(
|
||||
Layer.provide(OrchestrationProjectionPipelineLive),
|
||||
Layer.provide(OrchestrationEventStoreLive),
|
||||
Layer.provide(
|
||||
Layer.succeed(OrchestrationCommandReceiptRepository, failingReceiptRepository),
|
||||
),
|
||||
Layer.provide(SqlitePersistenceMemory),
|
||||
Layer.provideMerge(ServerConfigLayer),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
),
|
||||
);
|
||||
const engine = await runtime.runPromise(Effect.service(OrchestrationEngineService));
|
||||
const createdAt = now();
|
||||
|
||||
await runtime.runPromise(
|
||||
engine.dispatch({
|
||||
type: "project.create",
|
||||
commandId: CommandId.makeUnsafe("cmd-project-receipt-create"),
|
||||
projectId: asProjectId("project-receipt"),
|
||||
title: "Receipt Project",
|
||||
workspaceRoot: "/tmp/project-receipt",
|
||||
defaultModelSelection: {
|
||||
provider: "codex",
|
||||
model: "gpt-5-codex",
|
||||
},
|
||||
createdAt,
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(
|
||||
runtime.runPromise(
|
||||
engine.dispatch({
|
||||
type: "thread.create",
|
||||
commandId: CommandId.makeUnsafe("cmd-receipt-fail"),
|
||||
threadId: ThreadId.makeUnsafe("thread-receipt-fail"),
|
||||
projectId: asProjectId("project-receipt"),
|
||||
title: "receipt-fail",
|
||||
modelSelection: {
|
||||
provider: "codex",
|
||||
model: "gpt-5-codex",
|
||||
},
|
||||
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
|
||||
runtimeMode: "approval-required",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
createdAt,
|
||||
}),
|
||||
),
|
||||
).rejects.toThrow("receipt lookup failed");
|
||||
|
||||
const result = await runtime.runPromise(
|
||||
engine.dispatch({
|
||||
type: "thread.create",
|
||||
commandId: CommandId.makeUnsafe("cmd-receipt-ok"),
|
||||
threadId: ThreadId.makeUnsafe("thread-receipt-ok"),
|
||||
projectId: asProjectId("project-receipt"),
|
||||
title: "receipt-ok",
|
||||
modelSelection: {
|
||||
provider: "codex",
|
||||
model: "gpt-5-codex",
|
||||
},
|
||||
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
|
||||
runtimeMode: "approval-required",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
createdAt,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(result.sequence).toBe(2);
|
||||
expect((await runtime.runPromise(engine.getReadModel())).snapshotSequence).toBe(2);
|
||||
await runtime.dispose();
|
||||
});
|
||||
|
||||
it("rolls back all events for a multi-event command when projection fails mid-dispatch", async () => {
|
||||
let shouldFailRequestedProjection = true;
|
||||
const flakyProjectionPipeline: OrchestrationProjectionPipelineShape = {
|
||||
|
||||
@@ -60,9 +60,13 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
const commandQueue = yield* Queue.unbounded<CommandEnvelope>();
|
||||
const eventPubSub = yield* PubSub.unbounded<OrchestrationEvent>();
|
||||
|
||||
const processEnvelope = (envelope: CommandEnvelope): Effect.Effect<void> => {
|
||||
const processEnvelope = Effect.fn("processEnvelope")(function* (
|
||||
envelope: CommandEnvelope,
|
||||
): Effect.fn.Return<void, OrchestrationDispatchError> {
|
||||
const dispatchStartSequence = readModel.snapshotSequence;
|
||||
const reconcileReadModelAfterDispatchFailure = Effect.gen(function* () {
|
||||
const reconcileReadModelAfterDispatchFailure = Effect.fn(
|
||||
"processEnvelope.reconcileReadModelAfterDispatchFailure",
|
||||
)(function* () {
|
||||
const persistedEvents = yield* Stream.runCollect(
|
||||
eventStore.readFromSequence(dispatchStartSequence),
|
||||
).pipe(Effect.map((chunk): OrchestrationEvent[] => Array.from(chunk)));
|
||||
@@ -81,7 +85,40 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
}
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const handleError = Effect.fn("processEnvelope.handleError")(function* (
|
||||
error: OrchestrationDispatchError,
|
||||
) {
|
||||
yield* reconcileReadModelAfterDispatchFailure().pipe(
|
||||
Effect.catch(() =>
|
||||
Effect.logWarning(
|
||||
"failed to reconcile orchestration read model after dispatch failure",
|
||||
).pipe(
|
||||
Effect.annotateLogs({
|
||||
commandId: envelope.command.commandId,
|
||||
snapshotSequence: readModel.snapshotSequence,
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (Schema.is(OrchestrationCommandInvariantError)(error)) {
|
||||
const aggregateRef = commandToAggregateRef(envelope.command);
|
||||
yield* commandReceiptRepository
|
||||
.upsert({
|
||||
commandId: envelope.command.commandId,
|
||||
aggregateKind: aggregateRef.aggregateKind,
|
||||
aggregateId: aggregateRef.aggregateId,
|
||||
acceptedAt: new Date().toISOString(),
|
||||
resultSequence: readModel.snapshotSequence,
|
||||
status: "rejected",
|
||||
error: error.message,
|
||||
})
|
||||
.pipe(Effect.catch(() => Effect.void));
|
||||
}
|
||||
yield* Deferred.fail(envelope.result, error);
|
||||
});
|
||||
|
||||
return yield* Effect.gen(function* () {
|
||||
const existingReceipt = yield* commandReceiptRepository.getByCommandId({
|
||||
commandId: envelope.command.commandId,
|
||||
});
|
||||
@@ -152,53 +189,24 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
),
|
||||
),
|
||||
);
|
||||
if (!committedCommand) {
|
||||
return;
|
||||
}
|
||||
|
||||
readModel = committedCommand.nextReadModel;
|
||||
for (const event of committedCommand.committedEvents) {
|
||||
yield* PubSub.publish(eventPubSub, event);
|
||||
}
|
||||
yield* Deferred.succeed(envelope.result, { sequence: committedCommand.lastSequence });
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.gen(function* () {
|
||||
yield* reconcileReadModelAfterDispatchFailure.pipe(
|
||||
Effect.catch(() =>
|
||||
Effect.logWarning(
|
||||
"failed to reconcile orchestration read model after dispatch failure",
|
||||
).pipe(
|
||||
Effect.annotateLogs({
|
||||
commandId: envelope.command.commandId,
|
||||
snapshotSequence: readModel.snapshotSequence,
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (Schema.is(OrchestrationCommandInvariantError)(error)) {
|
||||
const aggregateRef = commandToAggregateRef(envelope.command);
|
||||
yield* commandReceiptRepository
|
||||
.upsert({
|
||||
commandId: envelope.command.commandId,
|
||||
aggregateKind: aggregateRef.aggregateKind,
|
||||
aggregateId: aggregateRef.aggregateId,
|
||||
acceptedAt: new Date().toISOString(),
|
||||
resultSequence: readModel.snapshotSequence,
|
||||
status: "rejected",
|
||||
error: error.message,
|
||||
})
|
||||
.pipe(Effect.catch(() => Effect.void));
|
||||
}
|
||||
yield* Deferred.fail(envelope.result, error);
|
||||
}),
|
||||
),
|
||||
);
|
||||
};
|
||||
}).pipe(Effect.catch(handleError), Effect.asVoid);
|
||||
});
|
||||
|
||||
yield* projectionPipeline.bootstrap;
|
||||
|
||||
// bootstrap in-memory read model from event store
|
||||
yield* Stream.runForEach(eventStore.readAll(), (event) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Stream.runForEach(
|
||||
eventStore.readAll(),
|
||||
Effect.fn("bootstrapReadModel")(function* (event) {
|
||||
readModel = yield* projectEvent(readModel, event);
|
||||
}),
|
||||
);
|
||||
@@ -215,12 +223,11 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
const readEvents: OrchestrationEngineShape["readEvents"] = (fromSequenceExclusive) =>
|
||||
eventStore.readFromSequence(fromSequenceExclusive);
|
||||
|
||||
const dispatch: OrchestrationEngineShape["dispatch"] = (command) =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* Deferred.make<{ sequence: number }, OrchestrationDispatchError>();
|
||||
yield* Queue.offer(commandQueue, { command, result });
|
||||
return yield* Deferred.await(result);
|
||||
});
|
||||
const dispatch: OrchestrationEngineShape["dispatch"] = Effect.fn("dispatch")(function* (command) {
|
||||
const result = yield* Deferred.make<{ sequence: number }, OrchestrationDispatchError>();
|
||||
yield* Queue.offer(commandQueue, { command, result });
|
||||
return yield* Deferred.await(result);
|
||||
});
|
||||
|
||||
return {
|
||||
getReadModel,
|
||||
|
||||
Reference in new issue
Block a user