diff --git a/packages/effect-codex-app-server/src/_internal/stdio.test.ts b/packages/effect-codex-app-server/src/_internal/stdio.test.ts index cde2150ff66fa..dcb00247b36ac 100644 --- a/packages/effect-codex-app-server/src/_internal/stdio.test.ts +++ b/packages/effect-codex-app-server/src/_internal/stdio.test.ts @@ -166,6 +166,45 @@ describe("Codex App Server stderr excerpt", () => { stderr: "WARNING: proceeding without PATH aliases\nMissing OpenAI API key.\n", excerpt: "Missing OpenAI API key.", }, + { + name: "launcher exit summary after a DNS failure", + stderr: [ + "Starting provider", + 'проверить токен LLM: Get "https://llm-gateway.example/api/models": dial tcp: lookup llm-gateway.example: no such host', + "не удалось отправить метрики на сервер: no such host", + "Ошибка: ошибка запуска плагина: exit status 1", + ].join("\n"), + excerpt: + 'проверить токен LLM: Get "https://llm-gateway.example/api/models": dial tcp: lookup llm-gateway.example: no such host | не удалось отправить метрики на сервер: no such host | Ошибка: ошибка запуска плагина: exit status 1', + }, + { + name: "labelled launcher exit summary after an authentication failure", + stderr: [ + 'oauth2: "invalid_request" "Errors.OIDCSession.RefreshTokenInvalid"', + "Error: provider launcher exited with code 1.", + ].join("\n"), + excerpt: + 'oauth2: "invalid_request" "Errors.OIDCSession.RefreshTokenInvalid" | Error: provider launcher exited with code 1.', + }, + { + name: "final launcher summary takes precedence over an earlier error label", + stderr: [ + "Error: starting provider", + "lookup failed: no such host", + "Ошибка: exit status 1", + ].join("\n"), + excerpt: "Error: starting provider | lookup failed: no such host | Ошибка: exit status 1", + }, + { + name: "launcher summary alone", + stderr: "Error: command failed with exit code 1\n", + excerpt: "Error: command failed with exit code 1", + }, + { + name: "launcher diagnostics remain bounded", + stderr: `${"x".repeat(1_000)}\noauth2: invalid_request\nError: launcher exited with code 1\n`, + excerpt: `…${`${"x".repeat(1_000)} | oauth2: invalid_request | Error: launcher exited with code 1`.slice(-511)}`, + }, { name: "ANSI color, carriage returns and repeated whitespace", stderr: "\u001b[31m Error:\t config.toml is invalid \u001b[0m\r\n", diff --git a/packages/effect-codex-app-server/src/_internal/stdio.ts b/packages/effect-codex-app-server/src/_internal/stdio.ts index a25ac0e6124b3..a047d1aa804d4 100644 --- a/packages/effect-codex-app-server/src/_internal/stdio.ts +++ b/packages/effect-codex-app-server/src/_internal/stdio.ts @@ -56,6 +56,7 @@ const STDERR_TAIL_MAX_CHARS = 8_192; const STDERR_EXCERPT_MAX_CHARS = 512; // `Error: …`, `TypeError: …`, `Error [ERR_MODULE_NOT_FOUND]: …`, `error: …`, `ERROR: …`, `fatal: …` const ERROR_LINE_PATTERN = /^(?:[a-z]*error|fatal)(?: ?\[[^\]]+\])?:\s*\S/i; +const EXIT_SUMMARY_PATTERN = /\b(?:exit status|exit code|exited with code)\s+\d+\.?$/i; export const appendStderrTail = (current: string, chunk: string): string => { const next = `${current}${chunk}`; @@ -68,13 +69,25 @@ export const stderrExcerpt = (tail: string): string | undefined => { .split(/\r?\n/) .map((line) => line.trim().replace(/\s+/g, " ")) .filter((line) => line.length > 0); - const line = lines.findLast((candidate) => ERROR_LINE_PATTERN.test(candidate)) ?? lines.at(-1); + const errorIndex = lines.findLastIndex((candidate) => ERROR_LINE_PATTERN.test(candidate)); + const lineIndex = + errorIndex === -1 || EXIT_SUMMARY_PATTERN.test(lines.at(-1) ?? "") + ? lines.length - 1 + : errorIndex; + const line = lines[lineIndex]; if (line === undefined) { return undefined; } - return line.length <= STDERR_EXCERPT_MAX_CHARS - ? line - : `${line.slice(0, STDERR_EXCERPT_MAX_CHARS - 1)}…`; + // Wrappers can print the actual failure before their generic exit-status summary. + const excerpt = EXIT_SUMMARY_PATTERN.test(line) + ? lines.slice(Math.max(0, lineIndex - 2), lineIndex + 1).join(" | ") + : line; + if (excerpt.length <= STDERR_EXCERPT_MAX_CHARS) { + return excerpt; + } + return excerpt !== line + ? `…${excerpt.slice(-(STDERR_EXCERPT_MAX_CHARS - 1))}` + : `${excerpt.slice(0, STDERR_EXCERPT_MAX_CHARS - 1)}…`; }; export const makeStderrTail = Effect.fn("makeStderrTail")(function* (