Skip to content
Merged
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
6 changes: 6 additions & 0 deletions apps/mobile/src/features/threads/ThreadQueueControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
queuedRunEditBlockedReason,
} from "@t3tools/client-runtime/queued-run-edit";
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
import { describeThreadFreeze } from "@t3tools/client-runtime/state/thread-workflows";
import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message";
import type { ChatAttachment, EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
import { Atom } from "effect/reactivity";
Expand Down Expand Up @@ -207,6 +208,11 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
contentContainerClassName="px-5 pb-6"
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }}
>
{workflow?.frozen != null ? (
<Text className="py-3 text-sm text-foreground-muted">
{describeThreadFreeze(workflow.frozen)}
</Text>
) : null}
{workflow?.isHeld && queuedRuns.length > 0 ? (
<View className="gap-2 py-3">
<Text className="text-sm text-foreground-muted">Queue paused</Text>
Expand Down
97 changes: 97 additions & 0 deletions apps/server/src/mcp/McpHttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Deferred from "effect/Deferred";
import * as Hash from "effect/Hash";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -1991,3 +1992,99 @@ it.effect.each(["capability_denied", "invalid_request"] as const)(
}).pipe(Effect.provide(layer));
},
);

it.effect(
"logs the connection of each call by its Mcp-Session-Id, over the real HTTP transport",
() =>
Effect.scoped(
Effect.gen(function* () {
const entries: Array<{ readonly connection?: string | undefined }> = [];
const scope: McpInvocationContext.McpThreadInvocationScope = {
environmentId,
requestNamespace: "shared-token",
thread: {
threadId: ThreadId.make("thread-shared"),
providerSessionId: "provider-session",
providerInstanceId: ProviderInstanceId.make("codex"),
},
client: undefined,
capabilities: new Set(["orchestration"]),
issuedAt: 1,
};
const toolkit = Toolkit.make(
Tool.make("probe", {
success: Schema.Struct({}),
failure: OrchestratorMcpFailure,
failureMode: "return",
}),
);
const serverLayer = McpHttpServer.layerMcpTransport(
McpHttpServer.toolkitRegistration(
toolkit,
McpToolAccess.toLayer(toolkit, {
probe: McpToolAccess.reads(() => Effect.succeed({})),
}),
),
).pipe(
Layer.provide(ServerSettings.layerTest()),
Layer.provide(McpToolAccessTestkit.liveThreadsLayer),
Layer.provide(
Layer.mock(RequestLog.McpRequestLog)({
record: (entry) => Effect.sync(() => void entries.push(entry)),
}),
),
Layer.provide(
Layer.mock(McpSessionRegistry.McpSessionRegistry)({
resolve: () => Effect.succeed(scope),
}),
),
);
yield* HttpRouter.serve(serverLayer, { disableListenLog: true, disableLogger: true }).pipe(
Layer.build,
);
const httpClient = yield* HttpClient.HttpClient;
const headers = {
accept: "application/json, text/event-stream",
authorization: "Bearer shared-token",
};
const connect = Effect.gen(function* () {
const initialize = yield* httpClient.post("/mcp", {
headers,
body: HttpBody.text(
`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1.0.0"}}}`,
"application/json",
),
});
const sessionId = initialize.headers["mcp-session-id"]!;
const call = httpClient
.post("/mcp", {
headers: {
...headers,
"mcp-session-id": sessionId,
"mcp-protocol-version": "2025-06-18",
},
body: HttpBody.text(
`{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"probe","arguments":{}}}`,
"application/json",
),
})
.pipe(Effect.flatMap((response) => response.text));
return { sessionId, call };
});
// Two agents on one sign-in, each with its own MCP session; one makes parallel calls.
const first = yield* connect;
const second = yield* connect;
yield* Effect.all([first.call, first.call, second.call], { concurrency: "unbounded" });
const [a1, a2, b1] = [...entries].toSorted((left, right) =>
String(left.connection).localeCompare(String(right.connection)),
);
expect(entries).toHaveLength(3);
expect(new Set(entries.map((entry) => entry.connection)).size).toBe(2);
expect(a1?.connection).toBeDefined();
expect([a1, a2, b1].every((entry) => entry?.connection !== undefined)).toBe(true);
expect(new Set(entries.map((entry) => entry.connection))).toEqual(
new Set([first.sessionId, second.sessionId].map((id) => Hash.string(id).toString(16))),
);
}),
).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
11 changes: 10 additions & 1 deletion apps/server/src/mcp/McpHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as McpRequestLog from "../persistence/McpRequestLog.ts";
import * as McpBridgeMetrics from "./McpBridgeMetrics.ts";
import * as BridgeStatus from "../persistence/McpBridgeStatus.ts";
import * as ThreadLaunchMcp from "./ThreadLaunchMcpService.ts";
import * as AgentRegistry from "../persistence/AgentRegistry.ts";
import * as McpClientThreads from "../persistence/McpClientThreads.ts";
import * as WorkLedger from "../persistence/WorkLedger.ts";
import { LedgerToolkit } from "./toolkits/ledger/tools.ts";
Expand All @@ -16,6 +17,7 @@ import * as Context from "effect/Context";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Hash from "effect/Hash";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
Expand Down Expand Up @@ -324,7 +326,12 @@ const makeMcpAuthMiddleware = Effect.gen(function* () {
const limited = McpInvocationContext.withRuntimeModeCeiling(attributed, runtimeModeCeiling);
const linkedEnvironments = yield* linksEnabled;
if (limited.client?.linked === true && !linkedEnvironments) return linkedEnvironmentsOff;
return { ...limited, linkedEnvironments };
const mcpSessionId = request.headers["mcp-session-id"];
return {
...limited,
linkedEnvironments,
...(mcpSessionId === undefined ? {} : { connection: Hash.string(mcpSessionId).toString(16) }),
};
});
// Untraced: a span here would wrap the tool call; the HTTP server span already covers it.
return Effect.fnUntraced(function* (httpEffect) {
Expand Down Expand Up @@ -1219,6 +1226,7 @@ const withToolAnalytics =
result,
exit._tag === "Failure" ? exit.cause : undefined,
),
connection: scope.connection,
})
.pipe(Effect.ignoreCause);
}
Expand Down Expand Up @@ -1560,4 +1568,5 @@ export const layer = Layer.merge(
Layer.provide(McpClientThreads.layer),
Layer.provide(McpBridgeMetrics.layer),
Layer.provide(McpRequestLog.layer),
Layer.provide(AgentRegistry.layer),
);
2 changes: 2 additions & 0 deletions apps/server/src/mcp/McpInvocationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export interface McpInvocationScope {
readonly client: McpClientCaller | undefined;
/** False while the Linked environments beta is off here; `/mcp` reads it per request. */
readonly linkedEnvironments?: boolean;
/** Short digest of the request's `Mcp-Session-Id`: one agent's connection, kept for the request log. */
readonly connection?: string;
}

export const messageSenderFor = (scope: McpInvocationScope) => {
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ function threadDetail(
archived: projection.thread.archivedAt !== null,
...threadSettlement(projection.thread),
...threadSnooze(shell, nowMs),
frozen: projection.thread.frozen ?? null,
createdAt: DateTime.formatIso(projection.thread.createdAt),
updatedAt: DateTime.formatIso(projection.thread.updatedAt),
};
Expand Down Expand Up @@ -3482,6 +3483,8 @@ const make = Effect.gen(function* () {
...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }),
status: result.run.status,
delivery: result.delivery,
...(result.run.queueHeld === true ? { held: true as const } : {}),
...(target.thread.frozen == null ? {} : { frozen: true as const }),
} satisfies OrchestratorMcpThreadSendResult;
}),
waitForThread: Effect.fn("OrchestratorMcpService.waitForThread")(function* (scope, input) {
Expand Down
155 changes: 155 additions & 0 deletions apps/server/src/mcp/threadFreeze.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { expect, it } from "@effect/vitest";
import {
EnvironmentId,
type OrchestrationV2Run,
type OrchestrationV2ServerCommand,
RunId,
ThreadId,
} from "@t3tools/contracts";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";

import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
import { ThreadFreezeGate } from "../orchestration-v2/ThreadFreeze.ts";
import * as McpInvocationContext from "./McpInvocationContext.ts";
import * as McpToolAccessTestkit from "./McpToolAccess.testkit.ts";
import { freezeThread, unfreezeThread } from "./threadFreeze.ts";

const target = ThreadId.make("thread:inbox");
const queuedRun = { id: RunId.make("run:queued"), status: "queued" } as OrchestrationV2Run;
const longLabel = "x".repeat(300);

const outside = (label = "Codex"): McpInvocationContext.McpInvocationScope => ({
environmentId: EnvironmentId.make("env"),
requestNamespace: "ns",
issuedAt: 0,
capabilities: new Set(["orchestration"]),
thread: undefined,
client: { sessionId: "963da7fc-7a35-4939-b709-dfd27d40d323", label, access: "full-access" },
});
const asThread = (
threadId: ThreadId,
subagent = false,
): McpInvocationContext.McpInvocationScope => ({
...outside(),
client: undefined,
thread: {
threadId,
providerSessionId: "provider-session",
providerInstanceId: McpToolAccessTestkit.liveThreadShell(threadId).providerInstanceId,
...(subagent ? { subagent: true } : {}),
},
});

const harness = (options: {
readonly frozen: boolean;
readonly runs: Array<OrchestrationV2Run>;
}) => {
const commands: Array<OrchestrationV2ServerCommand> = [];
const stopped: Array<ThreadId> = [];
const gates: Array<ThreadFreezeGate["Service"] | undefined> = [];
const shell = McpToolAccessTestkit.liveThreadShell(target);
const projection = McpToolAccessTestkit.idleThreadProjection(shell);
const layer = Layer.mock(ThreadManagement.ThreadManagementService)({
getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)),
getProjectThreadRecords: () =>
Effect.succeed({
...projection,
runs: options.runs,
thread: {
...projection.thread,
...(options.frozen
? { frozen: { at: "t", by: { kind: "owner", label: "Chris" }, reason: "r" } }
: {}),
},
}) as never,
dispatch: (command) =>
Effect.gen(function* () {
commands.push(command);
gates.push(Option.getOrUndefined(yield* Effect.serviceOption(ThreadFreezeGate)));
return { sequence: 1, storedEvents: [], replayed: false } as never;
}),
stopDelegatedTasks: ({ threadId }) => Effect.sync(() => void stopped.push(threadId)),
}).pipe(Layer.provideMerge(NodeCrypto.layer));
const run = <A, E, R>(
effect: Effect.Effect<A, E, R>,
scope: McpInvocationContext.McpInvocationScope,
) =>
effect.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provide(layer),
);
return { commands, gates, stopped, run };
};

it.effect("freeze records the authenticated caller, then stops and holds", () => {
const { commands, gates, stopped, run } = harness({ frozen: false, runs: [queuedRun] });
return Effect.gen(function* () {
const result = yield* run(
freezeThread({ threadId: target, reason: " misbehaving " }),
outside(longLabel),
);
expect(result).toMatchObject({ frozen: true, queuedMessages: 1 });
expect(commands.map((command) => command.type)).toEqual([
"thread.metadata.update",
"thread.stop",
]);
const marker = commands[0];
expect(marker?.type === "thread.metadata.update" ? marker.frozen : undefined).toMatchObject({
by: { kind: "outside-client", sessionId: "963da7fc", label: "x".repeat(128) },
reason: "misbehaving",
});
expect(gates).toEqual([{ op: "freeze" }, undefined]);
expect(stopped).toEqual([target]);
const missing = yield* run(freezeThread({ threadId: target, reason: " " }), outside()).pipe(
Effect.flip,
);
expect(missing.code).toBe("invalid_request");
});
});

it.effect("a frozen thread or its delegate cannot unfreeze it", () => {
const { commands, run } = harness({ frozen: true, runs: [] });
return Effect.gen(function* () {
for (const scope of [asThread(target), asThread(ThreadId.make("thread:child"), true)]) {
const refused = yield* run(
unfreezeThread({ threadId: target, freshContext: undefined }),
scope,
).pipe(Effect.flip);
expect(refused.code).toBe("capability_denied");
}
expect(commands).toEqual([]);
});
});

it.effect("unfreeze resumes the queue, and freshContext waits for an empty queue", () => {
const queued = harness({ frozen: true, runs: [queuedRun] });
const empty = harness({ frozen: true, runs: [] });
return Effect.gen(function* () {
const refused = yield* queued
.run(unfreezeThread({ threadId: target, freshContext: true }), outside())
.pipe(Effect.flip);
expect(refused.message).toContain("1 queued message");
expect(queued.commands).toEqual([]);

expect(
yield* queued.run(unfreezeThread({ threadId: target, freshContext: undefined }), outside()),
).toMatchObject({ frozen: false, wasFrozen: true, queuedMessages: 1 });
expect(queued.commands.map((command) => command.type)).toEqual(["queue.resume"]);
expect(queued.gates).toMatchObject([
{
op: "unfreeze",
freshContext: false,
by: { kind: "outside-client", sessionId: "963da7fc" },
},
]);

expect(
yield* empty.run(unfreezeThread({ threadId: target, freshContext: true }), outside()),
).toMatchObject({ frozen: false, freshContextNext: true });
expect(empty.commands.map((command) => command.type)).toEqual(["queue.resume"]);
expect(empty.gates).toMatchObject([{ op: "unfreeze", freshContext: true }]);
});
});
Loading
Loading