Validate thread reference cursors
This commit is contained in:
1 parent
dba8c741ab
commit
78517b59b0
3 files changed
+53
-5
No files matched your search
@@ -14,10 +14,12 @@ import * as Option from "effect/Option";
|
||||
import { McpSchema, McpServer } from "effect/unstable/ai";
|
||||
|
||||
import * as McpInvocationContext from "../../McpInvocationContext.ts";
|
||||
import { PersistenceSqlError } from "../../../persistence/Errors.ts";
|
||||
import { ProjectionSnapshotQuery } from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";
|
||||
import {
|
||||
buildThreadReferencePage,
|
||||
hasUserThreadReference,
|
||||
threadRead,
|
||||
ThreadReferenceToolkitHandlersLive,
|
||||
} from "./handlers.ts";
|
||||
import { ThreadReferenceToolkit } from "./tools.ts";
|
||||
@@ -143,6 +145,19 @@ it("continues pagination across an empty message at a page boundary", () => {
|
||||
expect(second.nextCursor).toBeNull();
|
||||
});
|
||||
|
||||
it.each([":", "0:", ":0", "0:0:garbage"])("rejects malformed cursor %s", (cursor) => {
|
||||
expect(
|
||||
buildThreadReferencePage(thread, {
|
||||
threadId: thread.id,
|
||||
cursor,
|
||||
maxChars: 1_000,
|
||||
} as never),
|
||||
).toMatchObject({
|
||||
_tag: "ThreadReferenceInvalidCursorError",
|
||||
cursor,
|
||||
});
|
||||
});
|
||||
|
||||
it("only authorizes user-supplied references from the invoking environment", () => {
|
||||
expect(hasUserThreadReference(currentThread, environmentId, thread.id)).toBe(true);
|
||||
expect(
|
||||
@@ -220,3 +235,26 @@ it.effect("rejects a thread that was not referenced by the invoking user", () =>
|
||||
Effect.provide(TestLayer),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("preserves projection failures when a referenced thread cannot be loaded", () => {
|
||||
const repositoryError = new PersistenceSqlError({
|
||||
operation: "getThreadDetailById",
|
||||
detail: "database unavailable",
|
||||
});
|
||||
return Effect.gen(function* () {
|
||||
const error = yield* Effect.flip(threadRead({ threadId: thread.id }));
|
||||
expect(error).toMatchObject({
|
||||
_tag: "ThreadReferenceUnavailableError",
|
||||
threadId: thread.id,
|
||||
});
|
||||
expect(error.cause).toBe(repositoryError);
|
||||
}).pipe(
|
||||
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
|
||||
Effect.provideService(ProjectionSnapshotQuery, {
|
||||
getThreadDetailById: (threadId: ThreadId) =>
|
||||
threadId === currentThread.id
|
||||
? Effect.succeed(Option.some(currentThread))
|
||||
: Effect.fail(repositoryError),
|
||||
} as never),
|
||||
);
|
||||
});
|
||||
@@ -28,7 +28,9 @@ function decodeCursor(
|
||||
thread: OrchestrationThread,
|
||||
): TranscriptPosition | null {
|
||||
if (!input.cursor) return { messageIndex: 0, textOffset: 0 };
|
||||
const [messageIndexText, textOffsetText] = input.cursor.split(":");
|
||||
const cursorMatch = /^(\d+):(\d+)$/.exec(input.cursor);
|
||||
if (!cursorMatch) return null;
|
||||
const [, messageIndexText, textOffsetText] = cursorMatch;
|
||||
const messageIndex = Number(messageIndexText);
|
||||
const textOffset = Number(textOffsetText);
|
||||
const message = thread.messages[messageIndex];
|
||||
@@ -114,7 +116,7 @@ export function hasUserThreadReference(
|
||||
);
|
||||
}
|
||||
|
||||
const threadRead = Effect.fn("ThreadReferenceToolkit.threadRead")(function* (
|
||||
export const threadRead = Effect.fn("ThreadReferenceToolkit.threadRead")(function* (
|
||||
input: ThreadReferenceReadInput,
|
||||
) {
|
||||
const invocation = yield* McpInvocationContext.McpInvocationContext;
|
||||
@@ -124,7 +126,11 @@ const threadRead = Effect.fn("ThreadReferenceToolkit.threadRead")(function* (
|
||||
const snapshotQuery = yield* ProjectionSnapshotQuery;
|
||||
const sourceThreadOption = yield* snapshotQuery
|
||||
.getThreadDetailById(invocation.threadId)
|
||||
.pipe(Effect.mapError(() => new ThreadReferenceUnavailableError({ threadId: input.threadId })));
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ThreadReferenceUnavailableError({ threadId: input.threadId, cause }),
|
||||
),
|
||||
);
|
||||
if (
|
||||
Option.isNone(sourceThreadOption) ||
|
||||
!hasUserThreadReference(sourceThreadOption.value, invocation.environmentId, input.threadId)
|
||||
@@ -133,7 +139,11 @@ const threadRead = Effect.fn("ThreadReferenceToolkit.threadRead")(function* (
|
||||
}
|
||||
const threadOption = yield* snapshotQuery
|
||||
.getThreadDetailById(input.threadId)
|
||||
.pipe(Effect.mapError(() => new ThreadReferenceUnavailableError({ threadId: input.threadId })));
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ThreadReferenceUnavailableError({ threadId: input.threadId, cause }),
|
||||
),
|
||||
);
|
||||
if (Option.isNone(threadOption)) {
|
||||
return yield* new ThreadReferenceNotFoundError({ threadId: input.threadId });
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ export class ThreadReferenceInvalidCursorError extends Schema.TaggedErrorClass<T
|
||||
|
||||
export class ThreadReferenceUnavailableError extends Schema.TaggedErrorClass<ThreadReferenceUnavailableError>()(
|
||||
"ThreadReferenceUnavailableError",
|
||||
{ threadId: ThreadId },
|
||||
{ threadId: ThreadId, cause: Schema.optional(Schema.Defect()) },
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Referenced thread '${this.threadId}' could not be read.`;
|
||||
|
||||
Reference in new issue
Block a user