Skip to content

Commit 1274876

Browse files
committed
fix(codex): log undecodable server notifications instead of dropping them silently
When a known notification method's payload fails schema decode — a newer app server sending shapes this client's schemas predate — the dispatch swallowed the error with Effect.ignore, so nothing anywhere recorded that a server message was dropped. The sibling effect-acp protocol logs a deduplicated warning for exactly this case because agents repeat bad payloads. Mirror that here: warn once per distinct failure (method + message), then drop as before. The mock peer gains an env-gated invalid delta and a regression test asserts the warning is captured and the client keeps serving requests afterwards.
1 parent dc84b09 commit 1274876

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

‎packages/effect-codex-app-server/src/client.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import * as Exit from "effect/Exit";
22
import * as Layer from "effect/Layer";
3+
import * as Logger from "effect/Logger";
34
import * as Path from "effect/Path";
5+
import * as Queue from "effect/Queue";
6+
import * as Deferred from "effect/Deferred";
47
import * as Effect from "effect/Effect";
58
import * as Ref from "effect/Ref";
69
import * as Scope from "effect/Scope";
710
import { ChildProcess, ChildProcessSpawner } from "effect/process";
811

912
import * as NodeServices from "@effect/platform-node/NodeServices";
1013
import { assert, it } from "@effect/vitest";
14+
import * as TestClock from "effect/testing/TestClock";
1115

1216
import * as CodexClient from "./client.ts";
1317

@@ -124,6 +128,57 @@ it.layer(NodeServices.layer)("effect-codex-app-server client", (it) => {
124128
]);
125129
}),
126130
);
131+
it.effect("warns once for an undecodable notification and keeps serving requests", () =>
132+
Effect.gen(function* () {
133+
const warnings = yield* Queue.unbounded<string>();
134+
const logger = Logger.make(({ logLevel, message }) => {
135+
if (logLevel === "Warn") Queue.offerUnsafe(warnings, String([message].flat()[0]));
136+
});
137+
const handle = yield* makeHandle({
138+
CODEX_APP_SERVER_TEST_INVALID_NOTIFICATION: "1",
139+
});
140+
const scope = yield* Scope.make();
141+
const layerClient = CodexClient.layerChildProcess(handle);
142+
// The transport fiber that logs the warning forks while the client layer
143+
// builds, so the logger must wrap the build itself (CurrentLoggers is a
144+
// FiberRef: a merged-in layer branch does not reach the client's fibers).
145+
const context = yield* Layer.buildWithScope(layerClient, scope).pipe(
146+
Effect.provide(Logger.layer([logger])),
147+
);
148+
const validDelta = yield* Deferred.make<void>();
149+
150+
const account = yield* Effect.gen(function* () {
151+
const client = yield* CodexClient.CodexAppServerClient;
152+
yield* client.handleServerNotification("item/agentMessage/delta", () =>
153+
Deferred.succeed(validDelta, undefined).pipe(Effect.asVoid),
154+
);
155+
yield* client.request("initialize", {
156+
clientInfo: {
157+
name: "effect-codex-app-server-test",
158+
title: "Effect Codex App Server Test",
159+
version: "0.0.0",
160+
},
161+
capabilities: {
162+
experimentalApi: true,
163+
optOutNotificationMethods: null,
164+
},
165+
});
166+
yield* client.notify("initialized", undefined);
167+
// The peer answers `initialized` with a valid delta followed by one the
168+
// payload schema rejects; the warning for the rejected one follows the
169+
// accepted one on the same processing fiber.
170+
yield* Deferred.await(validDelta);
171+
const warning = yield* TestClock.withLive(
172+
Queue.take(warnings).pipe(Effect.timeout("5 seconds")),
173+
);
174+
assert.equal(warning, "Dropped a Codex App Server notification that failed to decode");
175+
// The dropped notification must not have broken the client.
176+
return yield* client.request("account/read", {});
177+
}).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
178+
179+
assert.equal(account.requiresOpenaiAuth, false);
180+
}),
181+
);
127182
it.effect("drains child stderr so large diagnostics cannot block protocol responses", () =>
128183
Effect.gen(function* () {
129184
const handle = yield* makeHandle({

‎packages/effect-codex-app-server/src/client.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,30 @@ export const make = Effect.fn("effect-codex-app-server/CodexAppServerClient.make
101101
): Effect.fn.Return<CodexAppServerClient["Service"], never, Scope.Scope> {
102102
const requestHandlers = new Map<string, ServerRequestHandler>();
103103
const notificationHandlers = new Map<string, Array<ServerNotificationHandler>>();
104+
const warnedDecodeFailures = new Set<string>();
104105
let unknownRequestHandler:
105106
| ((method: string, params: unknown) => Effect.Effect<unknown, CodexError.CodexAppServerError>)
106107
| undefined;
107108
let unknownNotificationHandler:
108109
| ((method: string, params: unknown) => Effect.Effect<void, CodexError.CodexAppServerError>)
109110
| undefined;
110111

112+
// Drop undecodable notifications without ending the session; a newer app server may
113+
// send shapes this client's schemas predate. Warn once per distinct failure because
114+
// the server repeats bad payloads.
115+
const dropUndecodableNotification =
116+
(method: string) => (error: CodexError.CodexAppServerError) => {
117+
const key = `${method}\n${error.message}`;
118+
const firstOfItsKind = !warnedDecodeFailures.has(key);
119+
warnedDecodeFailures.add(key);
120+
return firstOfItsKind
121+
? Effect.logWarning("Dropped a Codex App Server notification that failed to decode").pipe(
122+
Effect.annotateLogs({ method, detail: error.message }),
123+
Effect.asVoid,
124+
)
125+
: Effect.void;
126+
};
127+
111128
const getServerRequestParamSchema = <M extends CodexRpc.ServerRequestMethod>(
112129
method: M,
113130
):
@@ -166,6 +183,7 @@ export const make = Effect.fn("effect-codex-app-server/CodexAppServerClient.make
166183
Effect.flatMap((decoded) =>
167184
Effect.forEach(handlers, (handler) => handler(decoded), { discard: true }),
168185
),
186+
Effect.catch(dropUndecodableNotification(notification.method)),
169187
Effect.ignore,
170188
);
171189
}

‎packages/effect-codex-app-server/test/fixtures/codex-app-server-mock-peer.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ const handleMethod = (message: Record<string, unknown>) => {
6868
turnId: "turn-1",
6969
},
7070
});
71+
// `item/agentMessage/delta` expects a string delta; the number fails the
72+
// payload schema, so the client must drop the notification with a warning.
73+
if (process.env.CODEX_APP_SERVER_TEST_INVALID_NOTIFICATION) {
74+
writeMessage({
75+
method: "item/agentMessage/delta",
76+
params: {
77+
delta: 42,
78+
itemId: "item-invalid",
79+
threadId: "thread-1",
80+
turnId: "turn-1",
81+
},
82+
});
83+
}
7184
return;
7285
}
7386
case "account/read": {

0 commit comments

Comments
 (0)