Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
[codex] Structure unroutable app-server messages (#3463)
Julius Marminge6424197a19a2
diff --git a/packages/effect-codex-app-server/src/errors.ts b/packages/effect-codex-app-server/src/errors.tsindex 2559bba61..3826a0992 100644--- a/packages/effect-codex-app-server/src/errors.ts+++ b/packages/effect-codex-app-server/src/errors.ts@@ -82,6 +82,11 @@ const payloadKind = (payload: unknown): CodexAppServerPayloadKind => {return typeof payload;};+const protocolMessageFields = ["id", "method", "params", "result", "error"] as const;++export const CodexAppServerProtocolMessageField = Schema.Literals(protocolMessageFields);+export type CodexAppServerProtocolMessageField = typeof CodexAppServerProtocolMessageField.Type;+export interface CodexAppServerRequestDiagnostics {readonly method?: string;readonly requestId?: string;@@ -157,7 +162,8 @@ export class CodexAppServerProtocolParseError extends Schema.TaggedErrorClass<Cooperation: CodexAppServerProtocolParseOperation,method: Schema.optionalKey(Schema.String),requestId: Schema.optionalKey(Schema.String),- detail: Schema.optionalKey(Schema.String),+ payloadKind: Schema.optionalKey(CodexAppServerPayloadKind),+ presentFields: Schema.optionalKey(Schema.Array(CodexAppServerProtocolMessageField)),issueCount: Schema.optionalKey(Schema.Number),issueKinds: Schema.optionalKey(Schema.Array(CodexAppServerSchemaIssueKind)),maximumPathDepth: Schema.optionalKey(Schema.Number),@@ -196,6 +202,31 @@ export class CodexAppServerProtocolParseError extends Schema.TaggedErrorClass<Cocause,});}++ static fromUnroutableMessage(message: unknown) {+ const diagnostics = { payloadKind: payloadKind(message) };+ if (typeof message !== "object" || message === null || Array.isArray(message)) {+ return new CodexAppServerProtocolParseError({+ operation: "route-wire-message",+ ...diagnostics,+ });+ }++ const presentFields = protocolMessageFields.filter((field) => field in message);+ const method =+ "method" in message && typeof message.method === "string" ? message.method : undefined;+ const requestId =+ "id" in message && (typeof message.id === "string" || typeof message.id === "number")+ ? String(message.id)+ : undefined;+ return new CodexAppServerProtocolParseError({+ operation: "route-wire-message",+ ...diagnostics,+ presentFields,+ ...(method === undefined ? {} : { method }),+ ...(requestId === undefined ? {} : { requestId }),+ });+ }}export class CodexAppServerTransportError extends Schema.TaggedErrorClass<CodexAppServerTransportError>()(diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.tsindex f387ca382..0ed81b3b9 100644--- a/packages/effect-codex-app-server/src/protocol.test.ts+++ b/packages/effect-codex-app-server/src/protocol.test.ts@@ -311,6 +311,36 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {}),);+ it.effect("describes unroutable messages with safe structural diagnostics", () =>+ Effect.gen(function* () {+ const secret = "codex-unroutable-secret-sentinel";+ const { stdio, input } = yield* makeInMemoryStdio();+ const termination = yield* Deferred.make<CodexError.CodexAppServerError>();+ yield* CodexProtocol.makeCodexAppServerPatchedProtocol({+ stdio,+ onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),+ });++ yield* Queue.offer(+ input,+ encodeJsonl({ id: true, method: "thread/start", params: { token: secret } }),+ );++ const error = yield* Deferred.await(termination);+ assert.instanceOf(error, CodexError.CodexAppServerProtocolParseError);+ assert.deepInclude(error, {+ operation: "route-wire-message",+ method: "thread/start",+ payloadKind: "object",+ presentFields: ["id", "method", "params"],+ });+ assert.isUndefined(error.requestId);+ assert.notProperty(error, "detail");+ assert.notProperty(error, "cause");+ assert.notInclude(error.message, secret);+ }),+ );+it.effect("classifies an input stream ending without inventing a cause", () =>Effect.gen(function* () {const { stdio, input } = yield* makeInMemoryStdio();diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.tsindex fbf173cbc..825c59b9b 100644--- a/packages/effect-codex-app-server/src/protocol.ts+++ b/packages/effect-codex-app-server/src/protocol.ts@@ -310,10 +310,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPareturn handleResponse(message);}return Effect.fail(- new CodexError.CodexAppServerProtocolParseError({- detail: "Received protocol message in an unknown shape",- operation: "route-wire-message",- }),+ CodexError.CodexAppServerProtocolParseError.fromUnroutableMessage(message),);};