Skip to content
Closed
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
78 changes: 71 additions & 7 deletions apps/server/src/mcp/ConversationTransferMcpService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";

import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
import { CommandReceiptStoreV2 } from "../orchestration-v2/CommandReceiptStore.ts";
import { ThreadManagementService } from "../orchestration-v2/ThreadManagementService.ts";
import type { McpInvocationScope } from "./McpInvocationContext.ts";
import * as ConversationTransfer from "./ConversationTransferMcpService.ts";
Expand All @@ -31,6 +33,11 @@ function projection(input: {
projectId,
runtimeMode: input.runtimeMode,
interactionMode: input.interactionMode,
lineage: {
parentThreadId: null,
relationshipToParent: null,
rootThreadId: input.threadId,
},
},
runs: [],
contextTransfers: [],
Expand All @@ -52,15 +59,21 @@ function testLayer(input: {
readonly source: OrchestrationV2ThreadProjection;
readonly dispatch: ThreadManagementService["Service"]["dispatch"];
readonly getThreadProjection?: ThreadManagementService["Service"]["getThreadProjection"];
readonly getReceipt?: CommandReceiptStoreV2["Service"]["getByCommandId"];
}) {
return ConversationTransfer.layer.pipe(
Layer.provide(
Layer.mock(ThreadManagementService)({
getThreadProjection: input.getThreadProjection ?? (() => Effect.succeed(input.parent)),
getProjectThread: ({ threadId }) =>
Effect.succeed(threadId === parentThreadId ? input.parent : input.source),
dispatch: input.dispatch,
}),
Layer.mergeAll(
Layer.mock(ThreadManagementService)({
getThreadProjection: input.getThreadProjection ?? (() => Effect.succeed(input.parent)),
getProjectThread: ({ threadId }) =>
Effect.succeed(threadId === parentThreadId ? input.parent : input.source),
dispatch: input.dispatch,
}),
Layer.mock(CommandReceiptStoreV2)({
getByCommandId: input.getReceipt ?? (() => Effect.succeed(Option.none())),
}),
),
),
);
}
Expand Down Expand Up @@ -94,7 +107,7 @@ describe("ConversationTransferMcpService", () => {
);
});

it.effect("rejects missing stable runs and inherited permission escalation before dispatch", () =>
it.effect("rejects missing stable runs and inherited transfer escalation before dispatch", () =>
Effect.gen(function* () {
const dispatched = yield* Ref.make(0);
const parent = projection({
Expand Down Expand Up @@ -128,6 +141,29 @@ describe("ConversationTransferMcpService", () => {
);
assert.equal(escalation.code, "runtime_mode_escalation_denied");

const mergeEscalation = yield* Effect.gen(function* () {
const service = yield* ConversationTransfer.ConversationTransferMcpService;
return yield* service
.mergeBack(scope(), {
sourceThreadId,
sourcePoint: { type: "latest_stable" },
clientRequestId: "merge-permission-ceiling",
})
.pipe(Effect.flip);
}).pipe(
Effect.provide(
testLayer({
parent,
source,
dispatch: () =>
Ref.update(dispatched, (count) => count + 1).pipe(
Effect.andThen(Effect.die("merge-back dispatch should not run")),
),
}),
),
);
assert.equal(mergeEscalation.code, "runtime_mode_escalation_denied");

const noRun = yield* Effect.gen(function* () {
const service = yield* ConversationTransfer.ConversationTransferMcpService;
return yield* service
Expand Down Expand Up @@ -218,4 +254,32 @@ describe("ConversationTransferMcpService", () => {
assert.equal(yield* Ref.get(dispatched), 0);
}),
);

it.effect("rejects merge-back from a thread without fork provenance", () =>
Effect.gen(function* () {
const parent = projection({
threadId: parentThreadId,
runtimeMode: "full-access",
interactionMode: "default",
});
const error = yield* Effect.gen(function* () {
const service = yield* ConversationTransfer.ConversationTransferMcpService;
return yield* service
.mergeBack(scope(), {
sourcePoint: { type: "latest_stable" },
clientRequestId: "not-a-fork",
})
.pipe(Effect.flip);
}).pipe(
Effect.provide(
testLayer({
parent,
source: parent,
dispatch: () => Effect.die("dispatch should not run"),
}),
),
);
assert.equal(error.code, "invalid_request");
}),
);
});
Loading
Loading