Repository navigation
fix(pi): Pi crash errors now include the stderr cause #36597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5837ac6
006543b
522faca
dd43464
2ecde29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import { | |
| MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH, | ||
| } from "@t3tools/shared/toolOutput"; | ||
|
|
||
| import { redactProviderFailureText } from "@t3tools/provider-core/server/failure"; | ||
| import { signalProcessGroup } from "@t3tools/provider-core/server/processGroup"; | ||
|
|
||
| export class PiRpcError extends Schema.TaggedError<PiRpcError>()("PiRpcError", { | ||
|
|
@@ -172,12 +173,39 @@ function summarizePiError(error: unknown): string { | |
| : text; | ||
| } | ||
|
|
||
| const PI_STDERR_TAIL_MAX_CHARS = 8_192; | ||
| const PI_STDERR_LINE_MAX_CHARS = 240; | ||
| const PI_STDERR_ERROR_LINE = /^[\w$.]*(?:Error|Exception)(?: \[[\w-]+\])?:/; | ||
| // The shared redactor covers `key=value`; extension errors can also echo `--api-key value` argv. | ||
| const PI_STDERR_CREDENTIAL_FLAG = | ||
| /((?<![\w-])--?[\w-]*(?:access[_-]?token|api[_-]?key|authorization|credential|password|secret|token)\s+)(?:"[^"]*"|'[^']*'|\S+)/giu; | ||
|
|
||
| // Only the last error line and its first stack frame: free-form stderr may echo prompts. | ||
| function piStderrCause(stderrTail: string): string | undefined { | ||
| const lines = stderrTail.split(/\r?\n/).map((line) => line.trim()); | ||
| for (let index = lines.length - 1; index >= 0; index -= 1) { | ||
| if (!PI_STDERR_ERROR_LINE.test(lines[index]!)) continue; | ||
| const frame = lines[index + 1]; | ||
| return [lines[index]!, ...(frame?.startsWith("at ") ? [frame] : [])] | ||
| .map((line) => | ||
| // Redact before truncating so a cut cannot split a quoted value out of its match. | ||
| redactProviderFailureText(line.replace(PI_STDERR_CREDENTIAL_FLAG, "$1[REDACTED]")).slice( | ||
| 0, | ||
| PI_STDERR_LINE_MAX_CHARS, | ||
| ), | ||
| ) | ||
| .join(" "); | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| // Pi 0.84+ may exit 1 during import without any protocol output. | ||
| function describePiStdoutClosure(exitCode: number | undefined): string { | ||
| function describePiStdoutClosure(exitCode: number | undefined, stderrTail: string): string { | ||
| if (exitCode === undefined || !Number.isFinite(exitCode)) { | ||
| return "pi process closed stdout"; | ||
| } | ||
| return `pi process exited with code ${exitCode}`; | ||
| const cause = exitCode === 0 ? undefined : piStderrCause(stderrTail); | ||
| return `pi process exited with code ${exitCode}${cause === undefined ? "" : `: ${cause}`}`; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep stderr text out of
The Effect service error rules keep attributes bounded. They prohibit command output and arbitrary defect text in attributes, and they require the exact value to live only in Keep the stderr excerpt in
|
||
| } | ||
|
|
||
| function parsePiRecord(line: string): PiRpcRecord | undefined { | ||
|
|
@@ -290,6 +318,8 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp | |
| const outgoing = yield* Queue.unbounded<Uint8Array, PiRpcError>(); | ||
| const transportDown = yield* Deferred.make<never, PiRpcError>(); | ||
| const exitDeferred = yield* Deferred.make<number, PiRpcError>(); | ||
| const stderrDrained = yield* Deferred.make<void>(); | ||
| let stderrTail = ""; | ||
| let nextRequestId = 0; | ||
|
|
||
| const failTransport = Effect.fn("PiRpc.failTransport")(function* (error: PiRpcError) { | ||
|
|
@@ -388,11 +418,17 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp | |
| Effect.provideService(Clock.Clock, Clock.Clock.defaultValue()), | ||
| Effect.orElseSucceed(() => Option.none<number>()), | ||
| ); | ||
| // Extension children that inherit stderr can hold it open after Pi exits. | ||
| yield* Deferred.await(stderrDrained).pipe( | ||
| Effect.timeoutOption(Duration.millis(250)), | ||
| Effect.provideService(Clock.Clock, Clock.Clock.defaultValue()), | ||
| ); | ||
| return yield* failTransport( | ||
| new PiRpcError({ | ||
| operation: "read", | ||
| detail: describePiStdoutClosure( | ||
| Option.isSome(maybeExitCode) ? maybeExitCode.value : undefined, | ||
| stderrTail, | ||
| ), | ||
| }), | ||
| ); | ||
|
|
@@ -403,13 +439,15 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp | |
|
|
||
| yield* child.stderr.pipe( | ||
| Stream.decodeText(), | ||
| Stream.runForEach((chunk) => | ||
| chunk.trim().length === 0 | ||
| Stream.runForEach((chunk) => { | ||
| stderrTail = `${stderrTail}${chunk}`.slice(-PI_STDERR_TAIL_MAX_CHARS); | ||
| return chunk.trim().length === 0 | ||
| ? Effect.void | ||
| : // Log stderr length only; unbounded text may contain credentials or prompts. | ||
| Effect.logDebug("pi stderr", { stderrLength: chunk.length }), | ||
| ), | ||
| Effect.logDebug("pi stderr", { stderrLength: chunk.length }); | ||
| }), | ||
| Effect.ignore, | ||
| Effect.ensuring(Deferred.succeed(stderrDrained, undefined)), | ||
| Effect.forkIn(scope), | ||
| ); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle escaped quotes in credential flag values.
For
Error: auth failed --password "first\" second-secret", the quoted branch stops at the escaped quote. It replaces only"first\"and leavessecond-secretinPiRpcError.message. The shared redactor does not match the remaining space-separated flag value.Make quoted-value matching escape-aware. Extend the credential regression case with an escaped quote and assert that the complete value is absent. This failure occurs before display truncation.