fix: preserve thread management failure semantics
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
e77b08358d
commit
f5d80b1d16
6 files changed
+366
-112
No files matched your search
@@ -1,15 +1,32 @@
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import {
|
||||
CommandId,
|
||||
MessageId,
|
||||
NodeId,
|
||||
type OrchestrationV2Command,
|
||||
type OrchestrationV2ThreadProjection,
|
||||
ProjectId,
|
||||
ProviderInstanceId,
|
||||
RunId,
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
import { existingThreadIdsForCommand, withCreationProvenance } from "./ThreadManagementService.ts";
|
||||
import { OrchestratorProjectionError, OrchestratorV2 } from "./Orchestrator.ts";
|
||||
import {
|
||||
existingThreadIdsForCommand,
|
||||
layer,
|
||||
ThreadManagementDurableRunProjectionError,
|
||||
ThreadManagementProjectThreadsListError,
|
||||
ThreadManagementProjectionLoadError,
|
||||
ThreadManagementRunNotFoundError,
|
||||
ThreadManagementService,
|
||||
ThreadManagementThreadNotFoundError,
|
||||
ThreadManagementThreadNotInterruptibleError,
|
||||
ThreadManagementThreadNotSendableError,
|
||||
withCreationProvenance,
|
||||
} from "./ThreadManagementService.ts";
|
||||
|
||||
it("stamps authoritative provenance on commands that create threads or messages", () => {
|
||||
const command: OrchestrationV2Command = {
|
||||
@@ -161,3 +178,124 @@ it("identifies every existing thread that must be hydrated before dispatch", ()
|
||||
}),
|
||||
).toEqual([parentThreadId, targetThreadId]);
|
||||
});
|
||||
|
||||
it("derives thread management messages from structural error attributes", () => {
|
||||
const projectId = ProjectId.make("project:thread-management:errors");
|
||||
const threadId = ThreadId.make("thread:thread-management:errors");
|
||||
const runId = RunId.make("run:thread-management:errors");
|
||||
const messageId = MessageId.make("message:thread-management:errors");
|
||||
const infrastructureCause = new Error("private sqlite detail");
|
||||
|
||||
const threadNotFound = new ThreadManagementThreadNotFoundError({
|
||||
projectId,
|
||||
threadId,
|
||||
});
|
||||
expect(threadNotFound).toMatchObject({ projectId, threadId });
|
||||
expect(threadNotFound.message).toBe(`Thread ${threadId} was not found in project ${projectId}.`);
|
||||
|
||||
const runNotFound = new ThreadManagementRunNotFoundError({ threadId, runId });
|
||||
expect(runNotFound).toMatchObject({ threadId, runId });
|
||||
expect(runNotFound.message).toBe(`Run ${runId} does not belong to thread ${threadId}.`);
|
||||
|
||||
const archived = new ThreadManagementThreadNotSendableError({
|
||||
threadId,
|
||||
reason: { _tag: "Archived" },
|
||||
});
|
||||
expect(archived).toMatchObject({ threadId, reason: { _tag: "Archived" } });
|
||||
expect(archived.message).toBe(`Thread ${threadId} is archived and cannot receive messages.`);
|
||||
|
||||
const notSteerable = new ThreadManagementThreadNotSendableError({
|
||||
threadId,
|
||||
reason: { _tag: "NoSteerableRun", mode: "restart" },
|
||||
});
|
||||
expect(notSteerable).toMatchObject({
|
||||
threadId,
|
||||
reason: { _tag: "NoSteerableRun", mode: "restart" },
|
||||
});
|
||||
expect(notSteerable.message).toBe(
|
||||
`Thread ${threadId} has no running turn that can be restarted.`,
|
||||
);
|
||||
|
||||
const notInterruptible = new ThreadManagementThreadNotInterruptibleError({
|
||||
threadId,
|
||||
runId,
|
||||
});
|
||||
expect(notInterruptible).toMatchObject({ threadId, runId });
|
||||
expect(notInterruptible.message).toBe(`Run ${runId} is not currently interruptible.`);
|
||||
|
||||
const listFailure = new ThreadManagementProjectThreadsListError({
|
||||
projectId,
|
||||
cause: infrastructureCause,
|
||||
});
|
||||
expect(listFailure).toMatchObject({ projectId, cause: infrastructureCause });
|
||||
expect(listFailure.message).toBe(`Unable to list threads in project ${projectId}.`);
|
||||
expect(listFailure.message).not.toContain(infrastructureCause.message);
|
||||
|
||||
const durableProjectionFailure = new ThreadManagementDurableRunProjectionError({
|
||||
threadId,
|
||||
messageId,
|
||||
});
|
||||
expect(durableProjectionFailure).toMatchObject({ threadId, messageId });
|
||||
expect(durableProjectionFailure.message).toBe(
|
||||
`Message ${messageId} was accepted on thread ${threadId} without a durable run projection.`,
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("classifies projection infrastructure failures separately from a missing thread", () => {
|
||||
const projectId = ProjectId.make("project:thread-management:projection-failure");
|
||||
const threadId = ThreadId.make("thread:thread-management:projection-failure");
|
||||
const infrastructureCause = new Error("sqlite read failed");
|
||||
const projectionError = new OrchestratorProjectionError({
|
||||
threadId,
|
||||
cause: infrastructureCause,
|
||||
});
|
||||
const testLayer = layer.pipe(
|
||||
Layer.provide(
|
||||
Layer.mock(OrchestratorV2)({
|
||||
getThreadProjection: () => Effect.fail(projectionError),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const service = yield* ThreadManagementService;
|
||||
const error = yield* Effect.flip(service.getProjectThread({ projectId, threadId }));
|
||||
|
||||
expect(error).toBeInstanceOf(ThreadManagementProjectionLoadError);
|
||||
expect(error).toMatchObject({
|
||||
projectId,
|
||||
threadId,
|
||||
cause: projectionError,
|
||||
});
|
||||
expect(error.message).toBe(`Unable to load thread ${threadId} in project ${projectId}.`);
|
||||
}).pipe(Effect.provide(testLayer));
|
||||
});
|
||||
|
||||
it.effect("uses thread-not-found only after a projection loads outside the project", () => {
|
||||
const projectId = ProjectId.make("project:thread-management:requested");
|
||||
const otherProjectId = ProjectId.make("project:thread-management:other");
|
||||
const threadId = ThreadId.make("thread:thread-management:wrong-project");
|
||||
const projection = {
|
||||
thread: {
|
||||
id: threadId,
|
||||
projectId: otherProjectId,
|
||||
deletedAt: null,
|
||||
},
|
||||
} as OrchestrationV2ThreadProjection;
|
||||
const testLayer = layer.pipe(
|
||||
Layer.provide(
|
||||
Layer.mock(OrchestratorV2)({
|
||||
getThreadProjection: () => Effect.succeed(projection),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const service = yield* ThreadManagementService;
|
||||
const error = yield* Effect.flip(service.getProjectThread({ projectId, threadId }));
|
||||
|
||||
expect(error).toBeInstanceOf(ThreadManagementThreadNotFoundError);
|
||||
expect(error).toMatchObject({ projectId, threadId });
|
||||
expect("cause" in error).toBe(false);
|
||||
}).pipe(Effect.provide(testLayer));
|
||||
});
|
||||
Reference in new issue
Block a user