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
84 changes: 84 additions & 0 deletions apps/server/src/codexAppServerManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function createSendTurnHarness(runtimeMode: "approval-required" | "full-access"
collabReceiverTurns: new Map(),
collabReceiverParents: new Map(),
reviewTurnIds: new Set<string>(),
lastSentTurnParams: undefined,
};

const requireSession = vi
Expand Down Expand Up @@ -924,6 +925,89 @@ describe("sendTurn", () => {
});
});

describe("sendTurn turn-param deduplication", () => {
it("first turn (lastSentTurnParams undefined) sends collaborationMode, runtimeOverrides, model, and effort", async () => {
const { manager, sendRequest } = createSendTurnHarness("full-access");
await manager.sendTurn({
threadId: asThreadId("thread_1"),
input: "hello",
interactionMode: "default",
effort: "medium",
});
const params = sendRequest.mock.calls[0]![2] as Record<string, unknown>;
expect(params).toHaveProperty("collaborationMode");
expect(params).toHaveProperty("approvalPolicy", "never");
expect(params).toHaveProperty("sandboxPolicy");
expect(params).toHaveProperty("model");
expect(params).toHaveProperty("effort", "medium");
});

it("second turn with identical params omits collaborationMode, runtimeOverrides, model, and effort", async () => {
const { manager, context, sendRequest } = createSendTurnHarness("full-access");
(context as Record<string, unknown>).lastSentTurnParams = {
interactionMode: "default",
runtimeMode: "full-access",
model: "gpt-5.3-codex",
effort: "medium",
};
await manager.sendTurn({
threadId: asThreadId("thread_1"),
input: "follow-up",
interactionMode: "default",
effort: "medium",
});
const params = sendRequest.mock.calls[0]![2] as Record<string, unknown>;
expect(params).not.toHaveProperty("collaborationMode");
expect(params).not.toHaveProperty("approvalPolicy");
expect(params).not.toHaveProperty("sandboxPolicy");
expect(params).not.toHaveProperty("effort");
expect(params).not.toHaveProperty("model");
});

it("turn after interactionMode change re-sends collaborationMode", async () => {
const { manager, context, sendRequest } = createSendTurnHarness("full-access");
(context as Record<string, unknown>).lastSentTurnParams = {
interactionMode: "default",
runtimeMode: "full-access",
model: "gpt-5.3-codex",
effort: "medium",
};
await manager.sendTurn({
threadId: asThreadId("thread_1"),
input: "plan this",
interactionMode: "plan",
effort: "medium",
});
const params = sendRequest.mock.calls[0]![2] as Record<string, unknown>;
expect(params).toHaveProperty("collaborationMode");
const collab = params.collaborationMode as { mode: string };
expect(collab.mode).toBe("plan");
expect(params).not.toHaveProperty("approvalPolicy");
expect(params).not.toHaveProperty("sandboxPolicy");
});

it("turn after runtimeMode change re-sends approvalPolicy and sandboxPolicy", async () => {
const { manager, context, sendRequest } = createSendTurnHarness("full-access");
(context as Record<string, unknown>).lastSentTurnParams = {
interactionMode: "default",
runtimeMode: "full-access",
model: "gpt-5.3-codex",
effort: "medium",
};
(context.session as Record<string, unknown>).runtimeMode = "approval-required";
await manager.sendTurn({
threadId: asThreadId("thread_1"),
input: "next turn",
interactionMode: "default",
effort: "medium",
});
const params = sendRequest.mock.calls[0]![2] as Record<string, unknown>;
expect(params).toHaveProperty("approvalPolicy", "untrusted");
expect(params).toHaveProperty("sandboxPolicy", { type: "readOnly" });
expect(params).not.toHaveProperty("collaborationMode");
});
});

describe("steerTurn", () => {
it("steers the active Codex turn when the session is already running", async () => {
const { manager, context, sendRequest } = createSendTurnHarness();
Expand Down
71 changes: 54 additions & 17 deletions apps/server/src/codexAppServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ interface CodexSessionContext {
nextRequestId: number;
stopping: boolean;
discovery?: boolean;
lastSentTurnParams: CodexLastSentTurnParams | undefined;
}

interface CodexLastSentTurnParams {
interactionMode: "default" | "plan" | undefined;
runtimeMode: RuntimeMode | undefined;
model: string | undefined;
effort: string | undefined;
}

interface CodexSkillListInput {
Expand Down Expand Up @@ -728,6 +736,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
reviewTurnIds: new Set(),
nextRequestId: 1,
stopping: false,
lastSentTurnParams: undefined,
};

this.sessions.set(threadId, context);
Expand Down Expand Up @@ -833,6 +842,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
threadOpenMethod = "thread/start";
threadOpenResponse = await this.sendRequest(context, "thread/start", threadStartParams);
}
context.lastSentTurnParams = undefined;

const threadOpenRecord = this.readObject(threadOpenResponse);
const threadIdRaw =
Expand Down Expand Up @@ -938,6 +948,20 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
if (!providerThreadId) {
throw new Error("Session is missing provider resume thread id.");
}
const normalizedModel = resolveCodexModelForAccount(
normalizeCodexModelSlug(input.model ?? context.session.model),
context.account,
);
const normalizedEffort = input.effort;
const interactionMode = input.interactionMode;

const last = context.lastSentTurnParams;
const isFirstTurn = last === undefined;
const runtimeModeChanged = isFirstTurn || last.runtimeMode !== context.session.runtimeMode;
const interactionModeChanged = isFirstTurn || last.interactionMode !== interactionMode;
const modelChanged = isFirstTurn || last.model !== normalizedModel;
const effortChanged = isFirstTurn || last.effort !== normalizedEffort;

const turnStartParams: {
threadId: string;
input: Array<
Expand All @@ -962,34 +986,45 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
} = {
threadId: providerThreadId,
input: turnInput,
...mapCodexRuntimeModeToTurnOverrides(context.session.runtimeMode),
};
const normalizedModel = resolveCodexModelForAccount(
normalizeCodexModelSlug(input.model ?? context.session.model),
context.account,
);
if (normalizedModel) {

if (runtimeModeChanged) {
Object.assign(turnStartParams, mapCodexRuntimeModeToTurnOverrides(context.session.runtimeMode));
}

if (normalizedModel && modelChanged) {
turnStartParams.model = normalizedModel;
}

if (input.serviceTier !== undefined) {
turnStartParams.serviceTier = input.serviceTier;
}
if (input.effort) {
turnStartParams.effort = input.effort;

if (normalizedEffort && effortChanged) {
turnStartParams.effort = normalizedEffort;
}
const collaborationMode = buildCodexCollaborationMode({
...(input.interactionMode !== undefined ? { interactionMode: input.interactionMode } : {}),
...(normalizedModel !== undefined ? { model: normalizedModel } : {}),
...(input.effort !== undefined ? { effort: input.effort } : {}),
});
if (collaborationMode) {
if (!turnStartParams.model) {
turnStartParams.model = collaborationMode.settings.model;

if (interactionModeChanged) {
const collaborationMode = buildCodexCollaborationMode({
...(interactionMode !== undefined ? { interactionMode } : {}),
...(normalizedModel !== undefined ? { model: normalizedModel } : {}),
...(normalizedEffort !== undefined ? { effort: normalizedEffort } : {}),
});
if (collaborationMode) {
if (!turnStartParams.model) {
turnStartParams.model = collaborationMode.settings.model;
}
turnStartParams.collaborationMode = collaborationMode;
}
turnStartParams.collaborationMode = collaborationMode;
}

const response = await this.sendRequest(context, "turn/start", turnStartParams);
context.lastSentTurnParams = {
interactionMode,
runtimeMode: context.session.runtimeMode,
model: normalizedModel,
effort: normalizedEffort,
};
const turnIdRaw = this.readString(this.readObject(this.readObject(response), "turn"), "id");
if (!turnIdRaw) {
throw new Error("turn/start response did not include a turn id.");
Expand Down Expand Up @@ -1346,6 +1381,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
reviewTurnIds: new Set(),
nextRequestId: 1,
stopping: false,
lastSentTurnParams: undefined,
};

this.sessions.set(threadId, context);
Expand Down Expand Up @@ -1915,6 +1951,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
nextRequestId: 1,
stopping: false,
discovery: true,
lastSentTurnParams: undefined,
};

this.discoverySessions.set(normalizedCwd, context);
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/provider/Layers/ClaudeAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe("ClaudeAdapterLive", () => {
});

const createInput = harness.getLastCreateQueryInput();
assert.deepEqual(createInput?.options.settingSources, ["user", "project", "local"]);
assert.deepEqual(createInput?.options.settingSources, ["user", "project"]);
assert.equal(createInput?.options.permissionMode, "bypassPermissions");
assert.equal(createInput?.options.allowDangerouslySkipPermissions, true);
}).pipe(
Expand All @@ -306,7 +306,7 @@ describe("ClaudeAdapterLive", () => {
});

const createInput = harness.getLastCreateQueryInput();
assert.deepEqual(createInput?.options.settingSources, ["user", "project", "local"]);
assert.deepEqual(createInput?.options.settingSources, ["user", "project"]);
assert.equal(createInput?.options.permissionMode, undefined);
assert.equal(createInput?.options.allowDangerouslySkipPermissions, undefined);
assert.deepEqual(createInput?.options.systemPrompt, {
Expand Down
34 changes: 19 additions & 15 deletions apps/server/src/provider/Layers/ClaudeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ interface ClaudeSessionContext {
streamFiber: Fiber.Fiber<void, Error> | undefined;
readonly startedAt: string;
readonly basePermissionMode: PermissionMode | undefined;
lastSentPermissionMode: PermissionMode | undefined;
currentApiModelId: string | undefined;
resumeSessionId: string | undefined;
readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>;
Expand Down Expand Up @@ -730,11 +731,7 @@ const SUPPORTED_CLAUDE_IMAGE_MIME_TYPES = new Set([
"image/png",
"image/webp",
]);
const CLAUDE_SETTING_SOURCES = [
"user",
"project",
"local",
] as const satisfies ReadonlyArray<SettingSource>;
const CLAUDE_SETTING_SOURCES = ["user", "project"] as const satisfies ReadonlyArray<SettingSource>;
const EMBEDDED_CLAUDE_SYSTEM_PROMPT_APPEND = [
"You are running inside Peak Code, a coding app that embeds the Claude Agent SDK.",
"Do not present the host app as Claude Code unless the user is explicitly asking about Claude Code.",
Expand Down Expand Up @@ -3318,6 +3315,7 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
streamFiber: undefined,
startedAt,
basePermissionMode: permissionMode,
lastSentPermissionMode: undefined,
currentApiModelId: apiModelId,
resumeSessionId: sessionId,
pendingApprovals,
Expand Down Expand Up @@ -3433,16 +3431,22 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
// "plan" maps directly to the SDK's "plan" permission mode;
// "default" restores the session's original permission mode.
// When interactionMode is absent we leave the current mode unchanged.
if (input.interactionMode === "plan") {
yield* Effect.tryPromise({
try: () => context.query.setPermissionMode("plan"),
catch: (cause) => toRequestError(input.threadId, "turn/setPermissionMode", cause),
});
} else if (input.interactionMode === "default") {
yield* Effect.tryPromise({
try: () => context.query.setPermissionMode(context.basePermissionMode ?? "default"),
catch: (cause) => toRequestError(input.threadId, "turn/setPermissionMode", cause),
});
const targetPermissionMode: PermissionMode =
input.interactionMode === "plan"
? "plan"
: (context.basePermissionMode ?? "default");

if (
input.interactionMode === "plan" ||
input.interactionMode === "default"
) {
if (context.lastSentPermissionMode !== targetPermissionMode) {
yield* Effect.tryPromise({
try: () => context.query.setPermissionMode(targetPermissionMode),
catch: (cause) => toRequestError(input.threadId, "turn/setPermissionMode", cause),
});
context.lastSentPermissionMode = targetPermissionMode;
}
}

const turnId = TurnId.makeUnsafe(yield* Random.nextUUIDv4);
Expand Down