Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadMetadataMcpService.test.ts

168 lines5.0 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { expect, it } from "@effect/vitest";
3import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6​
7import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
8import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
9import type * as McpInvocationContext from "./McpInvocationContext.ts";
10import * as ThreadMetadataMcp from "./ThreadMetadataMcpService.ts";
11​
12const threadId = ThreadId.make("thread:metadata-caller");
13const scope: McpInvocationContext.McpInvocationScope = {
14 environmentId: EnvironmentId.make("environment:metadata-test"),
15 requestNamespace: "provider-session:metadata-test",
16 thread: {
17 threadId,
18 providerSessionId: "provider-session:metadata-test",
19 providerInstanceId: ProviderInstanceId.make("codex"),
20 },
21 client: undefined,
22 capabilities: new Set(["orchestration"]),
23 issuedAt: 1,
24};
25​
26function layerService(
27 getThreadShell: ThreadManagement.ThreadManagementService["Service"]["getThreadShell"],
28) {
29 return ThreadMetadataMcp.layer.pipe(
30 Layer.provide(
31 Layer.merge(
32 Layer.mock(ThreadManagement.ThreadManagementService)({
33 getThreadShell,
34 getThreadRecords: () => Effect.die("projection must not load after shell failure"),
35 } satisfies Partial<ThreadManagement.ThreadManagementService["Service"]>),
36 NodeCrypto.layer,
37 ),
38 ),
39 );
41​
42const updateCallingThread = Effect.gen(function* () {
43 const service = yield* ThreadMetadataMcp.ThreadMetadataMcpService;
44 return yield* service.update(scope, {
45 action: "rename",
46 title: "Renamed thread",
47 clientRequestId: "metadata-caller-classification",
48 });
49});
50​
51it.effect("reports an absent calling thread as thread_not_found", () =>
52 Effect.gen(function* () {
53 const error = yield* updateCallingThread.pipe(
54 Effect.provide(layerService(() => Effect.succeed(null))),
55 Effect.flip,
56 );
57​
58 expect(error.code).toBe("thread_not_found");
59 }),
60);
61​
62it.effect("keeps calling-thread storage failures as orchestration errors", () =>
63 Effect.gen(function* () {
64 const error = yield* updateCallingThread.pipe(
65 Effect.provide(
66 layerService(() =>
67 Effect.fail(
68 new OrchestratorProjectionError({
69 threadId,
70 cause: new Error("storage unavailable"),
71 }),
72 ),
73 ),
74 ),
75 Effect.flip,
76 );
77​
78 expect(error.code).toBe("orchestration_error");
79 }),
80);
81​
82it.effect("refuses to change a thread that runs above the caller's modes", () =>
83 Effect.gen(function* () {
84 const fullAccessThread = ThreadId.make("thread:metadata-full-access");
85 const shells = new Map([
86 [
87 threadId,
88 {
89 id: threadId,
90 projectId: "project",
91 runtimeMode: "auto",
92 interactionMode: "default",
93 activeRunId: "run-live",
94 archivedAt: null,
95 providerInstanceId: "codex",
96 deletedAt: null,
97 },
98 ],
99 [
100 fullAccessThread,
101 {
102 id: fullAccessThread,
103 projectId: "project",
104 runtimeMode: "full-access",
105 interactionMode: "default",
106 deletedAt: null,
107 },
108 ],
109 ]);
110 const error = yield* Effect.gen(function* () {
111 const service = yield* ThreadMetadataMcp.ThreadMetadataMcpService;
112 return yield* service.update(scope, {
113 threadId: fullAccessThread,
114 action: "rename",
115 title: "Renamed from a narrower thread",
116 });
117 }).pipe(
118 Effect.provide(layerService((id) => Effect.succeed((shells.get(id) ?? null) as never))),
119 Effect.flip,
120 );
121​
122 expect(error.code).toBe("runtime_mode_escalation_denied");
123 }),
124);
125​
126it.effect("refuses another thread's metadata to a thread caller whose run has ended", () =>
127 Effect.gen(function* () {
128 const otherThread = ThreadId.make("thread:metadata-other");
129 const shells = new Map([
130 [
131 threadId,
132 {
133 id: threadId,
134 projectId: "project",
135 runtimeMode: "full-access",
136 interactionMode: "default",
137 activeRunId: null,
138 archivedAt: null,
139 providerInstanceId: "codex",
140 deletedAt: null,
141 },
142 ],
143 [
144 otherThread,
145 {
146 id: otherThread,
147 projectId: "project",
148 runtimeMode: "approval-required",
149 interactionMode: "default",
150 deletedAt: null,
151 },
152 ],
153 ]);
154 const error = yield* Effect.gen(function* () {
155 const service = yield* ThreadMetadataMcp.ThreadMetadataMcpService;
156 return yield* service.update(scope, {
157 threadId: otherThread,
158 action: "rename",
159 title: "Renamed after the run ended",
160 });
161 }).pipe(
162 Effect.provide(layerService((id) => Effect.succeed((shells.get(id) ?? null) as never))),
163 Effect.flip,
164 );
165​
166 expect(error.code).toBe("parent_not_active");
167 }),
168);