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
33 changes: 32 additions & 1 deletion packages/effect-codex-app-server/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,7 +162,8 @@ export class CodexAppServerProtocolParseError extends Schema.TaggedErrorClass<Co
operation: 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),
Expand Down Expand Up @@ -196,6 +202,31 @@ export class CodexAppServerProtocolParseError extends Schema.TaggedErrorClass<Co
cause,
});
}

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>()(
Expand Down
30 changes: 30 additions & 0 deletions packages/effect-codex-app-server/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions packages/effect-codex-app-server/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
return handleResponse(message);
}
return Effect.fail(
new CodexError.CodexAppServerProtocolParseError({
detail: "Received protocol message in an unknown shape",
operation: "route-wire-message",
}),
CodexError.CodexAppServerProtocolParseError.fromUnroutableMessage(message),
);
};

Expand Down
Loading