Skip to content

Commit 79ee520

Browse files
fix(server): "Always allow this session" stops Codex asking for every command
Codex's acceptForSession only skips the exact command or patch it answered, so a supervised Codex thread kept asking for every new command after the user chose "Always allow this session". The Codex adapter now remembers the allowed request kind per native thread and answers later requests of that kind itself, until the runtime mode changes or the thread unloads.
1 parent bf8ffe8 commit 79ee520

2 files changed

Lines changed: 241 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type OrchestrationV2ProviderTurn,
2020
type OrchestrationV2TurnItem,
2121
ProjectId,
22+
type ProviderApprovalDecision,
2223
ProviderInstanceId,
2324
ProviderSessionId,
2425
ProviderThreadId,
@@ -2786,6 +2787,184 @@ describe("CodexAdapterV2 post-settle continuation", () => {
27862787
),
27872788
);
27882789

2790+
it.effect("stops asking for commands the user allowed for the session", () =>
2791+
Effect.scoped(
2792+
Effect.gen(function* () {
2793+
const nativeThreadId = "session-approval-thread";
2794+
const firstTurnId = "session-approval-turn-1";
2795+
const secondTurnId = "session-approval-turn-2";
2796+
const approval = (input: {
2797+
readonly id: number;
2798+
readonly turnId: string;
2799+
readonly kind: "commandExecution" | "fileChange";
2800+
readonly itemId: string;
2801+
readonly decision: string;
2802+
}): ReadonlyArray<CodexReplay.CodexAppServerReplayEntry> => [
2803+
{
2804+
type: "emit_inbound",
2805+
label: `${input.itemId} approval`,
2806+
frame: {
2807+
id: input.id,
2808+
method: `item/${input.kind}/requestApproval`,
2809+
params: {
2810+
threadId: nativeThreadId,
2811+
turnId: input.turnId,
2812+
itemId: input.itemId,
2813+
startedAtMs: 1782622441000,
2814+
...(input.kind === "commandExecution" ? { command: `cat ${input.itemId}` } : {}),
2815+
},
2816+
},
2817+
},
2818+
{
2819+
type: "expect_outbound",
2820+
label: `${input.itemId} decision`,
2821+
frame: { id: input.id, result: { decision: input.decision } },
2822+
},
2823+
];
2824+
const turnEvent = (
2825+
method: "turn/started" | "turn/completed",
2826+
turnId: string,
2827+
): CodexReplay.CodexAppServerReplayEntry => ({
2828+
type: "emit_inbound",
2829+
label: `${turnId} ${method}`,
2830+
frame: {
2831+
method,
2832+
params: {
2833+
threadId: nativeThreadId,
2834+
turn: makeCodexReplayTurn({
2835+
id: turnId,
2836+
status: method === "turn/started" ? "inProgress" : "completed",
2837+
}),
2838+
},
2839+
},
2840+
});
2841+
const transcript = makeCodexReplayTranscript({
2842+
scenario: "codex-session-approvals",
2843+
entries: [
2844+
...codexReplayPreamble({
2845+
nativeThreadId,
2846+
nativeTurnId: firstTurnId,
2847+
prompt: "Review it.",
2848+
}),
2849+
...approval({
2850+
id: 100,
2851+
turnId: firstTurnId,
2852+
kind: "commandExecution",
2853+
itemId: "first-command",
2854+
decision: "acceptForSession",
2855+
}),
2856+
// Answered without asking: Codex alone would ask for every new command.
2857+
...approval({
2858+
id: 101,
2859+
turnId: firstTurnId,
2860+
kind: "commandExecution",
2861+
itemId: "second-command",
2862+
decision: "acceptForSession",
2863+
}),
2864+
// Only commands were allowed, so a file change still asks.
2865+
...approval({
2866+
id: 102,
2867+
turnId: firstTurnId,
2868+
kind: "fileChange",
2869+
itemId: "first-patch",
2870+
decision: "accept",
2871+
}),
2872+
turnEvent("turn/completed", firstTurnId),
2873+
{
2874+
type: "expect_outbound",
2875+
label: "supervised turn/start",
2876+
frame: {
2877+
id: 4,
2878+
method: "turn/start",
2879+
params: {
2880+
threadId: nativeThreadId,
2881+
input: [{ type: "text", text: "Again, supervised." }],
2882+
cwd: "/workspace",
2883+
model: "gpt-5.4",
2884+
approvalPolicy: "untrusted",
2885+
approvalsReviewer: "user",
2886+
sandboxPolicy: { type: "readOnly" },
2887+
summary: "detailed",
2888+
},
2889+
},
2890+
},
2891+
{
2892+
type: "emit_inbound",
2893+
label: "supervised turn/start",
2894+
frame: {
2895+
id: 4,
2896+
result: { turn: makeCodexReplayTurn({ id: secondTurnId, status: "inProgress" }) },
2897+
},
2898+
},
2899+
turnEvent("turn/started", secondTurnId),
2900+
// A runtime mode change takes the allowance back.
2901+
...approval({
2902+
id: 103,
2903+
turnId: secondTurnId,
2904+
kind: "commandExecution",
2905+
itemId: "third-command",
2906+
decision: "accept",
2907+
}),
2908+
turnEvent("turn/completed", secondTurnId),
2909+
],
2910+
});
2911+
const harness = yield* makeCodexReplayHarness(transcript);
2912+
const requests = () => [
2913+
...new Map(
2914+
harness.events.flatMap((event) =>
2915+
event.type === "runtime_request.updated"
2916+
? [[event.runtimeRequest.id, event.runtimeRequest] as const]
2917+
: [],
2918+
),
2919+
).values(),
2920+
];
2921+
const answer = (
2922+
count: number,
2923+
kind: "command" | "file-change",
2924+
decision: ProviderApprovalDecision,
2925+
) =>
2926+
Effect.gen(function* () {
2927+
yield* awaitUntil(() => requests().length === count, `approval request ${count}`);
2928+
const request = requests()[count - 1]!;
2929+
assert.equal(request.kind, kind);
2930+
yield* harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision });
2931+
});
2932+
2933+
yield* harness.runtime.startTurn(
2934+
makeCodexTestTurnInput({
2935+
threadId: harness.threadId,
2936+
providerThread: harness.providerThread,
2937+
now: yield* DateTime.now,
2938+
attemptId: RunAttemptId.make("session-approvals-1"),
2939+
text: "Review it.",
2940+
}),
2941+
);
2942+
yield* answer(1, "command", "acceptForSession");
2943+
yield* answer(2, "file-change", "accept");
2944+
yield* harness.firstTerminal;
2945+
yield* harness.runtime.startTurn({
2946+
...makeCodexTestTurnInput({
2947+
threadId: harness.threadId,
2948+
providerThread: harness.providerThread,
2949+
now: yield* DateTime.now,
2950+
attemptId: RunAttemptId.make("session-approvals-2"),
2951+
text: "Again, supervised.",
2952+
}),
2953+
runOrdinal: 2,
2954+
providerTurnOrdinal: 2,
2955+
runtimePolicy: { ...CODEX_TEST_RUNTIME_POLICY, runtimeMode: "approval-required" },
2956+
});
2957+
yield* answer(3, "command", "accept");
2958+
yield* awaitUntil(() => harness.terminalEvents().length === 2, "supervised turn");
2959+
assert.lengthOf(requests(), 3);
2960+
}).pipe(
2961+
Effect.provide(
2962+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2963+
),
2964+
),
2965+
),
2966+
);
2967+
27892968
it.effect("preserves T3 context on the wire and restores it after compaction", () =>
27902969
Effect.scoped(
27912970
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,43 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
18801880
const pendingRuntimeRequests = yield* Ref.make(
18811881
new Map<string, PendingCodexRuntimeRequest>(),
18821882
);
1883+
/**
1884+
* Approval kinds the user chose "Always allow this session" for, by
1885+
* native thread. Codex's own acceptForSession only skips the exact
1886+
* command or patch it answered, so every new command would ask again.
1887+
* The allowance holds for the runtime mode it was given in, and ends
1888+
* when the thread unloads, like Codex's own session approvals.
1889+
*/
1890+
const sessionApprovals = new Map<
1891+
string,
1892+
{ readonly runtimeMode: RuntimeMode; readonly kinds: Set<ProviderRequestKind> }
1893+
>();
1894+
const allowedForSession = (
1895+
nativeThreadId: string,
1896+
context: ActiveCodexTurnContext,
1897+
kind: ProviderRequestKind,
1898+
) => {
1899+
const allowed = sessionApprovals.get(nativeThreadId);
1900+
return (
1901+
allowed?.runtimeMode === context.input.runtimePolicy.runtimeMode &&
1902+
allowed.kinds.has(kind)
1903+
);
1904+
};
1905+
const rememberSessionApproval = (
1906+
nativeThreadId: string,
1907+
context: ActiveCodexTurnContext,
1908+
kind: ProviderRequestKind,
1909+
decision: ProviderApprovalDecision,
1910+
) => {
1911+
if (decision !== "acceptForSession" && decision !== "acceptAlways") return;
1912+
const runtimeMode = context.input.runtimePolicy.runtimeMode;
1913+
const allowed = sessionApprovals.get(nativeThreadId);
1914+
if (allowed?.runtimeMode === runtimeMode) {
1915+
allowed.kinds.add(kind);
1916+
} else {
1917+
sessionApprovals.set(nativeThreadId, { runtimeMode, kinds: new Set([kind]) });
1918+
}
1919+
};
18831920
/**
18841921
* Turn contexts retained past turn/completed while background command
18851922
* items started in that turn are still running, so late item events
@@ -5076,6 +5113,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
50765113
payload,
50775114
);
50785115
}
5116+
if (allowedForSession(payload.threadId, context, "command")) {
5117+
return {
5118+
decision: "acceptForSession",
5119+
} satisfies CodexSchema.CommandExecutionRequestApprovalResponse;
5120+
}
50795121

50805122
const nativeRequestId = payload.approvalId ?? payload.itemId;
50815123
const artifacts = yield* buildApprovalRequestArtifacts({
@@ -5124,6 +5166,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
51245166
}),
51255167
),
51265168
);
5169+
rememberSessionApproval(payload.threadId, context, "command", resolved);
51275170
return {
51285171
decision: resolved === "acceptAlways" ? "acceptForSession" : resolved,
51295172
} satisfies CodexSchema.CommandExecutionRequestApprovalResponse;
@@ -5139,6 +5182,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
51395182
payload,
51405183
);
51415184
}
5185+
if (allowedForSession(payload.threadId, context, "file-change")) {
5186+
return {
5187+
decision: "acceptForSession",
5188+
} satisfies CodexSchema.FileChangeRequestApprovalResponse;
5189+
}
51425190

51435191
const artifacts = yield* buildApprovalRequestArtifacts({
51445192
context,
@@ -5184,6 +5232,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
51845232
}),
51855233
),
51865234
);
5235+
rememberSessionApproval(payload.threadId, context, "file-change", resolved);
51875236
return {
51885237
decision: resolved === "acceptAlways" ? "acceptForSession" : resolved,
51895238
} satisfies CodexSchema.FileChangeRequestApprovalResponse;
@@ -5343,6 +5392,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
53435392
payload,
53445393
);
53455394
}
5395+
if (allowedForSession(payload.conversationId, context, "command")) {
5396+
return {
5397+
decision: "approved_for_session",
5398+
} satisfies CodexSchema.ExecCommandApprovalResponse;
5399+
}
53465400

53475401
const nativeRequestId = payload.approvalId ?? payload.callId;
53485402
const artifacts = yield* buildApprovalRequestArtifacts({
@@ -5389,6 +5443,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
53895443
}),
53905444
),
53915445
);
5446+
rememberSessionApproval(payload.conversationId, context, "command", resolved);
53925447
return {
53935448
decision: approvalDecisionToLegacyReviewDecision(resolved),
53945449
} satisfies CodexSchema.ExecCommandApprovalResponse;
@@ -5404,6 +5459,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
54045459
payload,
54055460
);
54065461
}
5462+
if (allowedForSession(payload.conversationId, context, "file-change")) {
5463+
return {
5464+
decision: "approved_for_session",
5465+
} satisfies CodexSchema.ApplyPatchApprovalResponse;
5466+
}
54075467

54085468
const artifacts = yield* buildApprovalRequestArtifacts({
54095469
context,
@@ -5449,6 +5509,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
54495509
}),
54505510
),
54515511
);
5512+
rememberSessionApproval(payload.conversationId, context, "file-change", resolved);
54525513
return {
54535514
decision: approvalDecisionToLegacyReviewDecision(resolved),
54545515
} satisfies CodexSchema.ApplyPatchApprovalResponse;
@@ -6706,6 +6767,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
67066767
unloadThread: (unloadInput) =>
67076768
Effect.gen(function* () {
67086769
const nativeThreadId = yield* getNativeThreadId(unloadInput.providerThread);
6770+
sessionApprovals.delete(nativeThreadId);
67096771
yield* client.request("thread/unsubscribe", { threadId: nativeThreadId });
67106772
}).pipe(
67116773
Effect.mapError((cause) =>

0 commit comments

Comments
 (0)