Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Result from "effect/Result";
import * as TestClock from "effect/testing/TestClock";
import * as SqlClient from "effect/sql/SqlClient";
import * as SqlitePersistence from "../persistence/Sqlite.ts";
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
Expand Down Expand Up @@ -960,3 +962,86 @@ it.effect("wakes the parent once when its delegated task asks a question", () =>
});
}).pipe(Effect.provide(DelegatedTaskQuestionReactor.layer.pipe(Layer.provideMerge(layerTest)))),
);

it.effect.each([false, true])("rejects thread.create for an existing id (deleted: %s)", (deleted) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const projections = yield* ProjectionStore.ProjectionStoreV2;
const threadId = ThreadId.make(`thread:duplicate-create:${deleted}`);
const create = {
type: "thread.create",
commandId: CommandId.make(`create:${threadId}`),
threadId,
projectId: ProjectId.make("project:duplicate-create"),
title: "Original",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
createdBy: "user",
creationSource: "web",
importedNativeThread: {
ref: { driver: adapter.driver, nativeId: "native-original", strength: "strong" },
},
} as const;
yield* orchestrator.dispatch(create);
if (deleted) {
yield* orchestrator.dispatch({
type: "thread.delete",
commandId: CommandId.make(`delete:${threadId}`),
threadId,
});
}
const before = yield* projections.getThread(threadId);
assert.isNotNull(before.activeProviderThreadId);
assert.equal(before.deletedAt !== null, deleted);
const sequence = yield* orchestrator.getThreadEventSequence(threadId);
yield* TestClock.adjust("1 second");
const duplicate = {
...create,
commandId: CommandId.make(`duplicate:${threadId}`),
title: "Replacement",
importedNativeThread: undefined,
};
const outcome = yield* orchestrator.dispatch(duplicate).pipe(Effect.result);
assert.ok(Result.isFailure(outcome), "duplicate create must be rejected");
const refused = outcome.failure;
assert.equal(refused._tag, "OrchestratorThreadAlreadyExistsError");
assert.equal(refused.message, `Thread ${threadId} already exists. Use a new thread id.`);
assert.deepEqual(yield* projections.getThread(threadId), before);
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);
const retried = yield* orchestrator.dispatch(duplicate).pipe(Effect.flip);
assert.equal(retried._tag, "OrchestratorCommandPreviouslyRejectedError");
assert.include(retried.message, refused.message);
}).pipe(Effect.provide(layerTest)),
);

it.effect("replays a thread.create receipt without changing the thread", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const projections = yield* ProjectionStore.ProjectionStoreV2;
const threadId = ThreadId.make("thread:create-receipt-replay");
const create = {
type: "thread.create",
commandId: CommandId.make("create:receipt-replay"),
threadId,
projectId: ProjectId.make("project:duplicate-create"),
title: "Original",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
createdBy: "user",
creationSource: "web",
} as const;
const original = yield* orchestrator.dispatch(create);
const before = yield* projections.getThread(threadId);
const replayed = yield* orchestrator.dispatch(create);
assert.equal(replayed.sequence, original.sequence);
assert.deepEqual(replayed, original);
assert.deepEqual(yield* projections.getThread(threadId), before);
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), original.sequence);
}).pipe(Effect.provide(layerTest)),
);
24 changes: 24 additions & 0 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ export class OrchestratorDispatchError extends Schema.TaggedError<OrchestratorDi
}
}

export class OrchestratorThreadAlreadyExistsError extends Schema.TaggedError<OrchestratorThreadAlreadyExistsError>()(
"OrchestratorThreadAlreadyExistsError",
{ commandId: CommandId, threadId: ThreadId },
) {
override get message(): string {
return `Thread ${this.threadId} already exists. Use a new thread id.`;
}
}

export class OrchestratorCommandRejectedError extends Schema.TaggedError<OrchestratorCommandRejectedError>()(
"OrchestratorCommandRejectedError",
{ commandId: CommandId, commandType: Schema.String, cause: Schema.optional(Schema.Defect()) },
Expand Down Expand Up @@ -262,6 +271,7 @@ export function canReplayCommandReceipt(

export const OrchestratorV2Error = Schema.Union([
OrchestratorDispatchError,
OrchestratorThreadAlreadyExistsError,
OrchestratorCommandRejectedError,
OrchestratorProjectionError,
OrchestratorDomainEventStreamError,
Expand Down Expand Up @@ -2250,6 +2260,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
"orchestration_v2.driver": command.modelSelection.instanceId,
});

// Tombstones reserve their ids too; recreating one would overwrite its history.
const existing = yield* projectionStore.getThread(command.threadId).pipe(
Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
Effect.mapError(
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
),
);
if (existing !== null) {
return yield* new OrchestratorThreadAlreadyExistsError({
commandId: command.commandId,
threadId: command.threadId,
});
}

const now = yield* DateTime.now;
const emitEvent = emit(events, command);
const thread: OrchestrationV2AppThread = {
Expand Down
14 changes: 14 additions & 0 deletions apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Result from "effect/Result";
import * as Stream from "effect/Stream";
import * as Schema from "effect/Schema";
import * as TestClock from "effect/testing/TestClock";
Expand Down Expand Up @@ -2308,6 +2309,19 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
);
assert.equal((yield* claimedFiles).length, 1);

const duplicate = yield* ThreadMessageIntake.launchThread({
...input,
commandId: CommandId.make("intake-duplicate-create"),
initialMessage: { ...input.initialMessage, messageId: MessageId.make("intake-duplicate") },
}).pipe(Effect.result);
assert.ok(Result.isFailure(duplicate));
assert.ok(duplicate.failure._tag === "ThreadLaunchError");
assert.equal(duplicate.failure.operation, "create-thread");
assert.propertyVal(duplicate.failure.cause, "_tag", "OrchestratorThreadAlreadyExistsError");
assert.equal((yield* claimedFiles).length, 1);
assert.deepEqual(yield* fs.readFile(storedPath), new Uint8Array([1, 2, 3, 4]));
assert.deepEqual(yield* fs.readFile(pendingPath), new Uint8Array([1, 2, 3, 4]));

const missingProject = yield* ThreadMessageIntake.launchThread({
...input,
commandId: CommandId.make("intake-no-project"),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/orchestration-v2/ThreadMessageIntake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function dispatchWasNotAccepted(
) {
switch (error._tag) {
case "OrchestratorCommandRejectedError":
case "OrchestratorThreadAlreadyExistsError":
case "OrchestratorProjectionError":
case "OrchestratorProviderAdapterError":
case "OrchestratorCommandPreviouslyRejectedError":
Expand Down
Loading