diff --git a/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts b/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts index 822ef5bae7..ec7fc206c6 100644 --- a/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts @@ -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), + ); +}); diff --git a/apps/server/src/mcp/toolkits/threadReference/handlers.ts b/apps/server/src/mcp/toolkits/threadReference/handlers.ts index bc646b7dbe..6a6f92d0f4 100644 --- a/apps/server/src/mcp/toolkits/threadReference/handlers.ts +++ b/apps/server/src/mcp/toolkits/threadReference/handlers.ts @@ -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 }); } diff --git a/packages/contracts/src/threadReference.ts b/packages/contracts/src/threadReference.ts index 3658e0027d..2207c2395c 100644 --- a/packages/contracts/src/threadReference.ts +++ b/packages/contracts/src/threadReference.ts @@ -79,7 +79,7 @@ export class ThreadReferenceInvalidCursorError extends Schema.TaggedErrorClass()( "ThreadReferenceUnavailableError", - { threadId: ThreadId }, + { threadId: ThreadId, cause: Schema.optional(Schema.Defect()) }, ) { override get message(): string { return `Referenced thread '${this.threadId}' could not be read.`;