fix(server): enforce ACP auth and preserve fork provenance

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-14 11:23:27 +02:00
1 parent 64d2faccc7
commit 1d7aed4354
4 files changed
+150 -25

No files matched your search

+40 -22
View File
@@ -333,11 +333,15 @@ const program = Effect.gen(function* () {
}),
);
const requireAuthentication = Effect.fn("acpMockAgent.requireAuthentication")(function* () {
if (!authenticated) {
return yield* AcpError.AcpRequestError.authRequired();
}
});
yield* agent.handleCreateSession(() =>
Effect.gen(function* () {
if (!authenticated) {
return yield* AcpError.AcpRequestError.authRequired();
}
yield* requireAuthentication();
return {
sessionId,
modes: modeState(),
@@ -410,36 +414,50 @@ const program = Effect.gen(function* () {
);
yield* agent.handleListSessions((request) =>
Effect.succeed({
sessions: [
{
sessionId,
cwd: request.cwd ?? process.cwd(),
title: "Mock session",
updatedAt: "1970-01-01T00:00:00.000Z",
},
],
Effect.gen(function* () {
yield* requireAuthentication();
return {
sessions: [
{
sessionId,
cwd: request.cwd ?? process.cwd(),
title: "Mock session",
updatedAt: "1970-01-01T00:00:00.000Z",
},
],
};
}),
);
yield* agent.handleForkSession((request) =>
Effect.succeed({
sessionId: `${request.sessionId}-fork`,
modes: modeState(),
models: modelState(),
configOptions: configOptions(),
Effect.gen(function* () {
yield* requireAuthentication();
return {
sessionId: `${request.sessionId}-fork`,
modes: modeState(),
models: modelState(),
configOptions: configOptions(),
};
}),
);
yield* agent.handleResumeSession(() =>
Effect.succeed({
modes: modeState(),
models: modelState(),
configOptions: configOptions(),
Effect.gen(function* () {
yield* requireAuthentication();
return {
modes: modeState(),
models: modelState(),
configOptions: configOptions(),
};
}),
);
yield* agent.handleCloseSession(() => Effect.succeed({}));
yield* agent.handleCloseSession(() =>
Effect.gen(function* () {
yield* requireAuthentication();
return {};
}),
);
yield* agent.handleSetSessionModel((request) =>
Effect.gen(function* () {
+26 -3
View File
@@ -553,7 +553,7 @@ function turnItemText(item: OrchestrationV2TurnItem): string | null {
function timelineItem(input: {
readonly row: OrchestrationV2ThreadProjection["visibleTurnItems"][number];
readonly maxChars: number;
readonly projection: OrchestrationV2ThreadProjection;
readonly messagesByThreadId: ReadonlyMap<ThreadId, OrchestrationV2ThreadProjection["messages"]>;
}): OrchestratorMcpThreadTimelineItem {
const text = turnItemText(input.row.item);
const textTruncated = text !== null && text.length > input.maxChars;
@@ -564,7 +564,9 @@ function timelineItem(input: {
const message =
messageId === null
? undefined
: input.projection.messages.find((candidate) => candidate.id === messageId);
: input.messagesByThreadId
.get(input.row.sourceThreadId)
?.find((candidate) => candidate.id === messageId);
return {
position: input.row.position,
visibility: input.row.visibility,
@@ -1323,13 +1325,34 @@ const make = Effect.gen(function* () {
row.item.type === "proposed_plan",
);
const page = matching.slice(0, limit);
const sourceThreadIds = [
...new Set(
page
.filter(
(row) => row.item.type === "user_message" || row.item.type === "assistant_message",
)
.map((row) => row.sourceThreadId)
.filter((threadId) => threadId !== target.thread.id),
),
];
const sourceProjections = yield* Effect.forEach(
sourceThreadIds,
(sourceThreadId) => loadProjectThread(target.thread.projectId, sourceThreadId),
{ concurrency: 8 },
);
const messagesByThreadId = new Map<ThreadId, OrchestrationV2ThreadProjection["messages"]>([
[target.thread.id, target.messages],
...sourceProjections.map(
(projection) => [projection.thread.id, projection.messages] as const,
),
]);
return {
thread: threadDetail(target),
recentRuns: target.runs
.toSorted((left, right) => right.ordinal - left.ordinal)
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT)
.map(threadRun),
items: page.map((row) => timelineItem({ row, maxChars, projection: target })),
items: page.map((row) => timelineItem({ row, maxChars, messagesByThreadId })),
nextPosition: page.at(-1)?.position ?? null,
hasMore: page.length < matching.length,
} satisfies OrchestratorMcpThreadReadResult;
@@ -1023,6 +1023,41 @@ describe("orchestrator MCP toolkit", () => {
`Claude completed: ${createdThreadPrompt}`,
);
const forkedThreadId = ThreadId.make("thread:mcp-orchestrator-inherited-read");
yield* orchestrator.dispatch({
type: "thread.fork",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make("command:mcp-orchestrator-inherited-read"),
sourceThreadId: promptedThread.threadId,
targetThreadId: forkedThreadId,
sourcePoint: { type: "latest_stable" },
title: "Inherited read thread",
});
const forkedProjection = yield* orchestrator.getThreadProjection(forkedThreadId);
expect(forkedProjection.messages).toEqual([]);
expect(
forkedProjection.visibleTurnItems.some(
(row) =>
row.sourceThreadId === promptedThread.threadId &&
row.item.type === "user_message",
),
).toBe(true);
const forkedReadCall = yield* invoke("t3_thread_read", {
threadId: forkedThreadId,
});
const forkedRead = yield* decodeThreadReadResult(forkedReadCall.structuredContent).pipe(
Effect.orDie,
);
expect(
forkedRead.items.find((item) => item.text === createdThreadPrompt),
).toMatchObject({
sourceThreadId: promptedThread.threadId,
createdBy: "agent",
creationSource: "mcp",
});
const ordinaryLoopPrompt = "Run an ordinary thread loop iteration.";
const sendCall = yield* invoke("t3_thread_send", {
threadId: emptyThread.threadId,
@@ -590,6 +590,55 @@ describe("AcpSessionRuntime", () => {
);
});
it.effect("rejects unauthenticated ACP session lifecycle requests", () =>
Effect.gen(function* () {
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;
yield* runtime.start();
const listError = yield* runtime.listSessions().pipe(Effect.flip);
expect(listError._tag).toBe("AcpRequestError");
if (listError._tag === "AcpRequestError") {
expect(listError.code).toBe(-32000);
}
const forkError = yield* runtime.forkSession("mock-session-1").pipe(Effect.flip);
expect(forkError._tag).toBe("AcpRequestError");
if (forkError._tag === "AcpRequestError") {
expect(forkError.code).toBe(-32000);
}
const resumeError = yield* runtime.resumeSession("mock-session-1").pipe(Effect.flip);
expect(resumeError._tag).toBe("AcpRequestError");
if (resumeError._tag === "AcpRequestError") {
expect(resumeError.code).toBe(-32000);
}
const closeError = yield* runtime.closeSession().pipe(Effect.flip);
expect(closeError._tag).toBe("AcpRequestError");
if (closeError._tag === "AcpRequestError") {
expect(closeError.code).toBe(-32000);
}
}).pipe(
Effect.provide(
AcpSessionRuntime.layer({
spawn: {
command: mockAgentCommand,
args: mockAgentArgs,
env: {
T3_ACP_REQUIRE_AUTH: "1",
T3_ACP_SESSION_LIFECYCLE: "1",
},
},
cwd: process.cwd(),
resumeSessionId: "mock-session-1",
clientInfo: { name: "t3-test", version: "0.0.0" },
}),
),
Effect.scoped,
Effect.provide(NodeServices.layer),
),
);
it.effect("skips no-op session config writes when the requested value is already active", () => {
const requestEvents: Array<AcpSessionRequestLogEvent> = [];
return Effect.gen(function* () {